Skip to contentSkip to conversation

Improve e2fsck heuristics for detecting corrupted inodes

PATCH In review Girish Shilamkar 2 participants 2 messages Jun 7, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improve e2fsck heuristics for detecting corrupted inodes

Hi,
The present e2fsck code checks the inode, per field basis. It
doesn't take into consideration to total sanity of the inode. This may cause e2fsck turning a garbage inode into a sane inode.
The following patch adds a heuristics to detect the degree of badness
of an inode. icount mechanism is used to keep track of the badness of every inode. The badness is increased as various fields in inode are found to be corrupt. Badness above a certain threshold value results in deletion of the inode. The default threshold value is 7, it can be specified to e2fsck using "-E inode_badness_threshold=<value>"

Any suggestions/comments are welcome.

Thanks & Regards,
Girish

diffstat patches/e2fsprogs-badness-counter.patch

 e2fsck/e2fsck.8.in                      |    7 +
 e2fsck/e2fsck.c                         |    4 
 e2fsck/e2fsck.h                         |   19 +++
 e2fsck/pass1.c                          |  155
++++++++++++++++++++++++--------
 e2fsck/pass1b.c                         |    4 
 e2fsck/pass2.c                          |   83 +++++++++++++----
 e2fsck/pass4.c                          |    1 
 e2fsck/problem.c                        |    5 +
 e2fsck/problem.h                        |    3 
 e2fsck/unix.c                           |   16 ++-
 lib/ext2fs/icount.c                     |   18 +++
 tests/f_bad_disconnected_inode/expect.1 |   14 --
 tests/f_bad_disconnected_inode/expect.2 |    2 
 13 files changed, 253 insertions(+), 78 deletions(-)
#1 lore raw

Oops....

Quoted 39 lines · Girish Shilamkar

Hi,
The present e2fsck code checks the inode, per field basis. It
doesn't take into consideration to total sanity of the inode. This may cause e2fsck turning a garbage inode into a sane inode.
The following patch adds a heuristics to detect the degree of badness
of an inode. icount mechanism is used to keep track of the badness of every inode. The badness is increased as various fields in inode are found to be corrupt. Badness above a certain threshold value results in deletion of the inode. The default threshold value is 7, it can be specified to e2fsck using "-E inode_badness_threshold=<value>"

Any suggestions/comments are welcome.

Thanks & Regards,
Girish

diffstat patches/e2fsprogs-badness-counter.patch

 e2fsck/e2fsck.8.in                      |    7 +
 e2fsck/e2fsck.c                         |    4 
 e2fsck/e2fsck.h                         |   19 +++
 e2fsck/pass1.c                          |  155
++++++++++++++++++++++++--------
 e2fsck/pass1b.c                         |    4 
 e2fsck/pass2.c                          |   83 +++++++++++++----
 e2fsck/pass4.c                          |    1 
 e2fsck/problem.c                        |    5 +
 e2fsck/problem.h                        |    3 
 e2fsck/unix.c                           |   16 ++-
 lib/ext2fs/icount.c                     |   18 +++
 tests/f_bad_disconnected_inode/expect.1 |   14 --
 tests/f_bad_disconnected_inode/expect.2 |    2 
 13 files changed, 253 insertions(+), 78 deletions(-)
Quoted 4 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

e2fsck/e2fsck.h +18 -1
Index: e2fsprogs-1.39/e2fsck/e2fsck.h
===================================================================
@@ -11,6 +11,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <stddef.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -194,6 +195,18 @@ typedef enum {
 	E2F_CLONE_ZERO
 } clone_opt_t;
 
+#define EXT4_FITS_IN_INODE(ext4_inode, einode, field)	\
+	((offsetof(typeof(*ext4_inode), field) +	\
+	  sizeof(ext4_inode->field))			\
+	<= (EXT2_GOOD_OLD_INODE_SIZE +			\
+	    (einode)->i_extra_isize))			\
+
+#define BADNESS_NORMAL 		1
+#define BADNESS_HIGH		2
+#define BADNESS_THRESHOLD	7
+#define BADNESS_BAD_MODE	100
+#define BADNESS_LARGE_FILE 	2199023255552ULL
+
 /*
  * Define the extended attribute refcount structure
  */
@@ -228,7 +241,6 @@ struct e2fsck_struct {
 			unsigned long max);
 
 	ext2fs_inode_bitmap inode_used_map; /* Inodes which are in use */
-	ext2fs_inode_bitmap inode_bad_map; /* Inodes which are bad somehow */
 	ext2fs_inode_bitmap inode_dir_map; /* Inodes which are directories */
 	ext2fs_inode_bitmap inode_bb_map; /* Inodes which are in bad blocks */
 	ext2fs_inode_bitmap inode_imagic_map; /* AFS inodes */
@@ -243,6 +255,8 @@ struct e2fsck_struct {
 	 */
 	ext2_icount_t	inode_count;
 	ext2_icount_t inode_link_info;
+	ext2_icount_t inode_badness;
+	int inode_badness_threshold;
 
 	ext2_refcount_t	refcount;
 	ext2_refcount_t refcount_extra;
@@ -340,6 +354,7 @@ struct e2fsck_struct {
 	__u32 fs_ext_attr_blocks;
 	__u32 extent_files;
 
+	time_t now_tolerance_val;
 	time_t now;
 
 	int ext_attr_ver;
@@ -452,6 +467,8 @@ extern int e2fsck_pass1_check_device_ino
 					   struct ext2_inode *inode);
 extern int e2fsck_pass1_check_symlink(ext2_filsys fs,
 				      struct ext2_inode *inode, char *buf);
+extern void e2fsck_mark_inode_bad(e2fsck_t ctx, ino_t ino, int count);
+extern int is_inode_bad(e2fsck_t ctx, ino_t ino);
 
 /* pass2.c */
 extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
e2fsck/pass1.c +117 -38

340 lines not on this page · raw message

e2fsck/pass4.c +1 -0
Index: e2fsprogs-1.39/e2fsck/pass4.c
===================================================================
@@ -185,6 +185,7 @@ void e2fsck_pass4(e2fsck_t ctx)
 	}
 	ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
 	ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
+	ext2fs_free_icount(ctx->inode_badness); ctx->inode_badness = 0;
 	ext2fs_free_inode_bitmap(ctx->inode_bb_map);
 	ctx->inode_bb_map = 0;
 	ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
e2fsck/pass2.c +63 -20

182 lines not on this page · raw message

e2fsck/problem.c +5 -0
Index: e2fsprogs-1.39/e2fsck/problem.c
===================================================================
@@ -1316,6 +1316,11 @@ static struct e2fsck_problem problem_tab
 	  N_("@i %i found in @g %g unused inodes area.  "),
 	  PROMPT_FIX, PR_PREEN_OK },
 
+	/* Inode too bad */
+	{ PR_2_INODE_TOOBAD,
+          N_("@i %i is badly corrupt (badness value = %N).  "),
+	  PROMPT_CLEAR, PR_PREEN_OK },
+
 	/* Pass 3 errors */
 
 	/* Pass 3: Checking directory connectivity */
e2fsck/problem.h +3 -0
Index: e2fsprogs-1.39/e2fsck/problem.h
===================================================================
@@ -792,6 +792,9 @@ struct problem_context {
 /* Inode found in group unused inodes area */
 #define PR_2_INOREF_IN_UNUSED		0x020046
 
+/* Inode completely corrupt */
+#define PR_2_INODE_TOOBAD		0x020047
+
 /*
  * Pass 3 errors
  */
lib/ext2fs/icount.c +18 -0
Index: e2fsprogs-1.39/lib/ext2fs/icount.c
===================================================================
@@ -461,6 +461,23 @@ static errcode_t get_inode_count(ext2_ic
 	return 0;
 }
 
+int ext2fs_icount_is_set(ext2_icount_t icount, ext2_ino_t ino)
+{
+	__u16 result;
+
+	if (ext2fs_test_inode_bitmap(icount->single, ino))
+		return 1;
+	else if (icount->multiple) {
+		if (ext2fs_test_inode_bitmap(icount->multiple, ino))
+			return 1;
+		return 0;
+	}
+	ext2fs_icount_fetch(icount, ino, &result);
+	if (result)
+		return 1;
+	return 0;
+}
+
 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
 {
 	errcode_t	ret = 0;
@@ -500,6 +517,7 @@ errcode_t ext2fs_icount_fetch32(ext2_ico
 		*ret = 0;
 		return 0;
 	}
+
 	get_inode_count(icount, ino, ret);
 	return 0;
 }
e2fsck/pass1b.c +2 -2
Index: e2fsprogs-1.39/e2fsck/pass1b.c
===================================================================
@@ -613,8 +613,8 @@ static void delete_file(e2fsck_t ctx, ex
 		fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
 	ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
 	ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
-	if (ctx->inode_bad_map)
-		ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
+ 	if (ctx->inode_badness)
+ 		e2fsck_mark_inode_bad(ctx, ino, 0);
 	ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
 
 	/* Inode may have changed by block_iterate, so reread it */
e2fsck/unix.c +14 -2
Index: e2fsprogs-1.39/e2fsck/unix.c
===================================================================
@@ -557,8 +557,9 @@ static void parse_extended_opts(e2fsck_t
 {
 	char	*buf, *token, *next, *p, *arg;
 	int	ea_ver;
+	int 	ino_bad;
 	int	extended_usage = 0;
-
+	
 	buf = string_copy(ctx, opts, 0);
 	for (token = buf; token && *token; token = next) {
 		p = strchr(token, ',');
@@ -619,6 +620,13 @@ static void parse_extended_opts(e2fsck_t
 		/* -E expand_extra_isize - enable EXTRA_ISIZE feature */
 		} else if (strcmp(token, "expand_extra_isize") == 0) {
 			ctx->flags |= E2F_FLAG_EXPAND_EISIZE;
+		/* -E inode_badness_threshold=<value> */
+ 		} else if (strcmp(token, "inode_badness_threshold") == 0) {
+ 			if (!arg) {
+ 				extended_usage++;
+ 				continue;
+ 			}
+ 			ctx->inode_badness_threshold = strtoul(arg, &p, 0);
 		} else {
 			fprintf(stderr, _("Unknown extended option: %s\n"),
 				token);
@@ -634,7 +642,8 @@ static void parse_extended_opts(e2fsck_t
 			"Valid extended options are:\n"
 		       "\tshared=<preserve|lost+found|delete>\n"
 		       "\tclone=<dup|zero>\n"
-		       "\tea_ver=<ea_version (1 or 2)>\n\n"), stderr);
+		       "\tea_ver=<ea_version (1 or 2)>\n"
+ 		       "\tinode_badness_threhold=(value)\n\n"), stderr);
 		exit(1);
 	}
 }
@@ -694,6 +703,9 @@ static errcode_t PRS(int argc, char *arg
 	profile_init(config_fn, &ctx->profile);
 	initialize_profile_options(ctx);
 
+	ctx->inode_badness_threshold = BADNESS_THRESHOLD;
+	ctx->now_tolerance_val = 172800; /* Two days */
+
 	while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
 		switch (c) {
 		case 'C':
e2fsck/e2fsck.c +0 -4
Index: e2fsprogs-1.39/e2fsck/e2fsck.c
===================================================================
@@ -104,10 +104,6 @@ errcode_t e2fsck_reset_context(e2fsck_t 
 		ext2fs_free_inode_bitmap(ctx->inode_bb_map);
 		ctx->inode_bb_map = 0;
 	}
-	if (ctx->inode_bad_map) {
-		ext2fs_free_inode_bitmap(ctx->inode_bad_map);
-		ctx->inode_bad_map = 0;
-	}
 	if (ctx->inode_imagic_map) {
 		ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
 		ctx->inode_imagic_map = 0;
e2fsck/e2fsck.8.in +7 -0
Index: e2fsprogs-1.39/e2fsck/e2fsck.8.in
===================================================================
@@ -178,6 +178,13 @@ in place (preserve); 
 cloned and then disconnected from their parent directory,
 then reconnected to /lost+found in pass 3 (lost+found); 
 or simply deleted (delete).  The default is preserve.
+.TP
+.BI inode_badness_threshold= threshold_value
+A badness counter is associated with every inode, which determines the degree
+of inode corruption. Each error found in the inode will increase the badness by
+1 or 2, and inodes with a badness at or above
+.I threshold_value will be prompted for deletion. The default
+.I threshold_value is 7.
 .RE
 .TP
 .B \-f
tests/f_bad_disconnected_inode/expect.1 +4 -10
Index: e2fsprogs-1.39/tests/f_bad_disconnected_inode/expect.1
===================================================================
@@ -39,10 +39,7 @@ Clear? yes
 i_blocks_hi for inode 16 (...) is 62762, should be zero.
 Clear? yes
 
-Unattached inode 16
-Connect to /lost+found? yes
-
-Inode 16 ref count is 5925, should be 1.  Fix? yes
+Inode 16 is badly corrupt (badness value = 9).  Clear? yes
 
 Pass 5: Checking group summary information
 Block bitmap differences:  -(9--19)
@@ -54,19 +51,16 @@ Fix? yes
 Free blocks count wrong (79, counted=91).
 Fix? yes
 
-Inode bitmap differences:  +16
-Fix? yes
-
-Free inodes count wrong for group #0 (7, counted=4).
+Free inodes count wrong for group #0 (8, counted=5).
 Fix? yes
 
 Directories count wrong for group #0 (3, counted=2).
 Fix? yes
 
-Free inodes count wrong (7, counted=4).
+Free inodes count wrong (8, counted=5).
 Fix? yes
 
 
 test_filesys: ***** FILE SYSTEM WAS MODIFIED *****
-test_filesys: 12/16 files (0.0% non-contiguous), 9/100 blocks
+test_filesys: 11/16 files (0.0% non-contiguous), 9/100 blocks
 Exit status is 1
tests/f_bad_disconnected_inode/expect.2 +1 -1
Index: e2fsprogs-1.39/tests/f_bad_disconnected_inode/expect.2
===================================================================
@@ -3,5 +3,5 @@ Pass 2: Checking directory structure
 Pass 3: Checking directory connectivity
 Pass 4: Checking reference counts
 Pass 5: Checking group summary information
-test_filesys: 12/16 files (0.0% non-contiguous), 9/100 blocks
+test_filesys: 11/16 files (0.0% non-contiguous), 9/100 blocks
 Exit status is 0