Whamcloud - gitweb
e2fsck: track errors/badness found for each inode
authorAndreas Dilger <adilger@whamcloud.com>
Fri, 13 Apr 2012 07:13:58 +0000 (01:13 -0600)
committerAndreas Dilger <adilger@whamcloud.com>
Tue, 29 May 2012 08:09:25 +0000 (02:09 -0600)
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 an apparently
sane inode ("It is a vessel of fertilizer, and none may abide
its strength.").

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
badness threshold value is 7, it can be specified to e2fsck
using "-E inode_badness_threshold=<value>"

This can avoid lengthy pass1b shared block processing, where a
corrupt chunk of the inode table has resulted in a bunch of
garbage inodes suddenly having shared blocks with a lot of good
inodes (or each other).

Signed-off-by: Girish Shilamkar <girish@clusterfs.com>
Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
16 files changed:
e2fsck/e2fsck.8.in
e2fsck/e2fsck.c
e2fsck/e2fsck.h
e2fsck/pass1.c
e2fsck/pass1b.c
e2fsck/pass2.c
e2fsck/pass4.c
e2fsck/problem.c
e2fsck/problem.h
e2fsck/super.c
e2fsck/unix.c
lib/ext2fs/ext2fs.h
lib/ext2fs/icount.c
tests/f_messy_inode/expect.1
tests/f_messy_inode/expect.2
tests/f_messy_inode/script [new file with mode: 0644]

index 817ffe0..f64afcf 100644 (file)
@@ -203,6 +203,15 @@ be 1 or 2.  The default extended attribute version format is 2.
 Only replay the journal if required, but do not perform any further checks
 or repairs.
 .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.
+.TP
 .BI fragcheck
 During pass 1, print a detailed report of any discontiguous blocks for
 files in the filesystem.
index a11e6fe..d8609d6 100644 (file)
@@ -112,10 +112,6 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx)
                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;
index 9324cb5..99ab9a5 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <stddef.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -214,6 +215,24 @@ enum clone_opt {
        E2F_CLONE_ZERO
 };
 
+#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 EXT4_XTIME_FUTURE(ctx, sb, xtime, margin)      \
+       (!((ctx)->flags & E2F_FLAG_TIME_INSANE) &&      \
+        (xtime) > (ctx)->now + (margin))
+#define EXT4_XTIME_ANCIENT(ctx, sb, xtime, margin)     \
+       ((sb)->s_mkfs_time > (margin) && (xtime) < (sb)->s_mkfs_time - (margin))
+
+#define BADNESS_NORMAL         1
+#define BADNESS_HIGH           2
+#define BADNESS_THRESHOLD      8
+#define BADNESS_BAD_MODE       100
+#define BADNESS_LARGE_FILE     2199023255552ULL
+
 /*
  * Define the extended attribute refcount structure
  */
@@ -254,7 +273,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 */
@@ -269,6 +287,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;
@@ -497,6 +517,11 @@ extern int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
 extern void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
                               struct ext2_inode *inode, int restart_flag,
                               const char *source);
+#define e2fsck_mark_inode_bad(ctx, ino, count) \
+               e2fsck_mark_inode_bad_loc(ctx, ino, count, __func__, __LINE__)
+extern void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
+                                     const char *func, const int line);
+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,
index df0bc9a..b43c5d9 100644 (file)
@@ -20,7 +20,8 @@
  *     - A bitmap of which inodes are in use.          (inode_used_map)
  *     - A bitmap of which inodes are directories.     (inode_dir_map)
  *     - A bitmap of which inodes are regular files.   (inode_reg_map)
- *     - A bitmap of which inodes have bad fields.     (inode_bad_map)
+ *     - An icount mechanism is used to keep track of
+ *       inodes with bad fields and its badness        (ctx->inode_badness)
  *     - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
  *     - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
  *     - A bitmap of which inodes need to be expanded  (expand_eisize_map)
@@ -68,7 +69,6 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
 static void mark_table_blocks(e2fsck_t ctx);
 static void alloc_bb_map(e2fsck_t ctx);
 static void alloc_imagic_map(e2fsck_t ctx);
-static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
 static void handle_fs_bad_blocks(e2fsck_t ctx);
 static void process_inodes(e2fsck_t ctx, char *block_buf);
 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
@@ -244,6 +244,7 @@ static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
        if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
                return;
 
+       e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
        if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
                return;
 
@@ -262,6 +263,7 @@ static void check_size(e2fsck_t ctx, struct problem_context *pctx)
        if (EXT2_I_SIZE(inode) == 0)
                return;
 
+       e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
        if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
                return;
 
@@ -362,7 +364,7 @@ static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
        struct ext2_super_block *sb = ctx->fs->super;
        struct ext2_inode_large *inode;
        __u32 *eamagic;
-       int min, max;
+       int min, max, dirty = 0;
 
        inode = (struct ext2_inode_large *) pctx->inode;
        if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
@@ -383,12 +385,25 @@ static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
         */
        if (inode->i_extra_isize &&
            (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
+               e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
                if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
                        return;
                inode->i_extra_isize = ctx->want_extra_isize;
-               e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
-                                       EXT2_INODE_SIZE(sb), "pass1");
-               return;
+               dirty = 1;
+
+               goto out;
+       }
+
+       if (EXT4_FITS_IN_INODE(inode, inode, i_crtime) &&
+           inode->i_crtime != 0 &&
+           (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, 2*ctx->time_fudge) ||
+            EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, 2*ctx->time_fudge))) {
+               pctx->num = inode->i_crtime;
+               if (fix_problem(ctx, PR_1_CRTIME_BAD, pctx)) {
+                       inode->i_crtime = 0;
+                       dirty = 1;
+               }
+               e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_HIGH);
        }
 
        eamagic = IHDR(inode);
@@ -399,16 +414,17 @@ static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
                memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
                        EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
                inode->i_extra_isize = ctx->want_extra_isize;
-               e2fsck_write_inode_full(ctx, pctx->ino,
-                                       (struct ext2_inode *)inode,
-                                       EXT2_INODE_SIZE(sb),
-                                       "check_inode_extra_space");
+               dirty = 1;
                if (inode->i_extra_isize < ctx->min_extra_isize)
                        ctx->min_extra_isize = inode->i_extra_isize;
        }
 
        if (*eamagic == EXT2_EXT_ATTR_MAGIC)
                check_ea_in_inode(ctx, pctx);
+out:
+       if (dirty)
+               e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
+                                       EXT2_INODE_SIZE(sb), "pass1");
 }
 
 /*
@@ -522,6 +538,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
            (rec_len % 4))
                return;
 
+       e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
        if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
                inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
                e2fsck_write_inode_full(ctx, pctx->ino, inode,
@@ -986,14 +1003,18 @@ void e2fsck_pass1(e2fsck_t ctx)
                        ehp = inode->i_block;
 #endif
                        if ((ext2fs_extent_header_verify(ehp,
-                                        sizeof(inode->i_block)) == 0) &&
-                           (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
-                               inode->i_flags |= EXT4_EXTENTS_FL;
+                                        sizeof(inode->i_block)) == 0)) {
+                               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
+                               if (fix_problem(ctx, PR_1_UNSET_EXTENT_FL,
+                                               &pctx)) {
+                                       inode->i_flags |= EXT4_EXTENTS_FL;
 #ifdef WORDS_BIGENDIAN
-                               memcpy(inode->i_block, tmp_block,
-                                      sizeof(inode->i_block));
+                                       memcpy(inode->i_block, tmp_block,
+                                              sizeof(inode->i_block));
 #endif
-                               e2fsck_write_inode(ctx, ino, inode, "pass1");
+                                       e2fsck_write_inode(ctx, ino, inode,
+                                                          "pass1");
+                               }
                        }
                }
 
@@ -1061,6 +1082,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                         * as a special case.
                         */
                        if (inode->i_dtime && inode->i_links_count) {
+                               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                                if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
                                        inode->i_dtime = 0;
                                        e2fsck_write_inode(ctx, ino, inode,
@@ -1196,6 +1218,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                 *
                 */
                if (inode->i_dtime) {
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                        if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
                                inode->i_dtime = 0;
                                e2fsck_write_inode(ctx, ino, inode, "pass1");
@@ -1212,18 +1235,19 @@ void e2fsck_pass1(e2fsck_t ctx)
                        frag = fsize = 0;
                }
 
+               /* Fixed in pass2, e2fsck_process_bad_inode(). */
                if (inode->i_faddr || frag || fsize ||
                    (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
-                       mark_inode_bad(ctx, ino);
-               if (!(fs->super->s_feature_incompat & 
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
+               if (!(fs->super->s_feature_incompat &
                      EXT4_FEATURE_INCOMPAT_64BIT) &&
                    inode->osd2.linux2.l_i_file_acl_high != 0)
-                       mark_inode_bad(ctx, ino);
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
                    !(fs->super->s_feature_ro_compat &
                      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
                    (inode->osd2.linux2.l_i_blocks_hi != 0))
-                       mark_inode_bad(ctx, ino);
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                if (inode->i_flags & EXT2_IMAGIC_FL) {
                        if (imagic_fs) {
                                if (!ctx->inode_imagic_map)
@@ -1231,6 +1255,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                                ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
                                                         ino);
                        } else {
+                               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                                if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
                                        inode->i_flags &= ~EXT2_IMAGIC_FL;
                                        e2fsck_write_inode(ctx, ino,
@@ -1292,8 +1317,24 @@ void e2fsck_pass1(e2fsck_t ctx)
                        check_immutable(ctx, &pctx);
                        check_size(ctx, &pctx);
                        ctx->fs_sockets_count++;
-               } else
-                       mark_inode_bad(ctx, ino);
+               } else {
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
+               }
+
+               if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
+               else if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime,
+                                          ctx->time_fudge))
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
+
+               if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
+               else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
+                                           ctx->time_fudge))
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
+
+               /* i_crtime is checked in check_inode_extra_space() */
+
                if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
                        if (inode->i_block[EXT2_IND_BLOCK])
                                ctx->fs_ind_count++;
@@ -1517,27 +1558,31 @@ static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
 }
 
 /*
- * Mark an inode as being bad in some what
+ * Mark an inode as being bad and increment its badness counter.
  */
-static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
+void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
+                              const char *func, const int line)
 {
        struct          problem_context pctx;
+       __u16           result;
 
-       if (!ctx->inode_bad_map) {
+       if (!ctx->inode_badness) {
                clear_problem_context(&pctx);
 
-               pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
-                               _("bad inode map"), EXT2FS_BMAP64_RBTREE,
-                               "inode_bad_map", &ctx->inode_bad_map);
+               pctx.errcode = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
+                                                    &ctx->inode_badness);
                if (pctx.errcode) {
-                       pctx.num = 3;
-                       fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
-                       /* Should never get here */
+                       fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
                        ctx->flags |= E2F_FLAG_ABORT;
                        return;
                }
        }
-       ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
+       ext2fs_icount_fetch(ctx->inode_badness, ino, &result);
+       ext2fs_icount_store(ctx->inode_badness, ino, count + result);
+
+       if (ctx->options & E2F_OPT_DEBUG)
+               fprintf(stderr, "%s:%d: increase inode %lu badness %u to %u\n",
+                       func, line, (unsigned long)ino, result, count + result);
 }
 
 
@@ -1697,7 +1742,8 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
            (blk < fs->super->s_first_data_block) ||
            (blk >= ext2fs_blocks_count(fs->super))) {
-               mark_inode_bad(ctx, ino);
+               /* Fixed in pass2, e2fsck_process_bad_inode(). */
+               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                return 0;
        }
 
@@ -1864,9 +1910,11 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
 
        if ((!LINUX_S_ISDIR(inode->i_mode) &&
             fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
-           (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
-            fix_problem(ctx, PR_1_HTREE_SET, pctx)))
-               return 1;
+           (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX))) {
+               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
+               if (fix_problem(ctx, PR_1_HTREE_SET, pctx))
+                       return 1;
+       }
 
        pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
 
@@ -1874,6 +1922,7 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
            (blk == 0) ||
            (blk < fs->super->s_first_data_block) ||
            (blk >= ext2fs_blocks_count(fs->super))) {
+               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
                        return 1;
                else
@@ -1881,8 +1930,11 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
        }
 
        retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
-       if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
-               return 1;
+       if (retval) {
+               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
+               if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
+                       return 1;
+       }
 
        /* XXX should check that beginning matches a directory */
        root = (struct ext2_dx_root_info *) (block_buf + 24);
@@ -1923,8 +1975,8 @@ void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
        ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
        if (ctx->inode_reg_map)
                ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
-       if (ctx->inode_bad_map)
-               ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
+       if (ctx->inode_badness)
+               ext2fs_icount_store(ctx->inode_badness, ino, 0);
 
        /*
         * If the inode was partially accounted for before processing
@@ -1976,6 +2028,11 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                        problem = PR_1_EXTENT_ENDS_BEYOND;
 
                if (problem) {
+                       /* To ensure that extent is in inode */
+                       if (info.curr_level == 0)
+                               e2fsck_mark_inode_bad(ctx, pctx->ino,
+                                                     BADNESS_HIGH);
+
                report_problem:
                        pctx->blk = extent.e_pblk;
                        pctx->blk2 = extent.e_lblk;
@@ -2181,6 +2238,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                    EXT2_FEATURE_INCOMPAT_COMPRESSION)
                        pb.compressed = 1;
                else {
+                       e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                        if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
                                inode->i_flags &= ~EXT2_COMPRBLK_FL;
                                dirty_inode++;
@@ -2241,6 +2299,11 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        }
 
        if (!pb.num_blocks && pb.is_dir) {
+               /*
+                * The mode might be in-correct. Increasing the badness by
+                * small amount won't hurt much.
+                */
+               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
                        e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
                        ctx->fs_directory_count--;
@@ -2300,6 +2363,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
                pctx->num = (pb.last_block+1) * fs->blocksize;
                pctx->group = bad_size;
+               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
                        inode->i_size = pctx->num;
                        if (!LINUX_S_ISDIR(inode->i_mode))
@@ -2316,6 +2380,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
             (inode->i_flags & EXT4_HUGE_FILE_FL) &&
             (inode->osd2.linux2.l_i_blocks_hi != 0))) {
                pctx->num = pb.num_blocks;
+               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
                if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
                        inode->i_blocks = pb.num_blocks;
                        inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
@@ -2471,8 +2536,10 @@ static int process_block(ext2_filsys fs,
                problem = PR_1_TOOBIG_SYMLINK;
 
        if (blk < fs->super->s_first_data_block ||
-           blk >= ext2fs_blocks_count(fs->super))
+           blk >= ext2fs_blocks_count(fs->super)) {
                problem = PR_1_ILLEGAL_BLOCK_NUM;
+               e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
+       }
 
        if (problem) {
                p->num_illegal_blocks++;
index 49dc0dd..63e2f55 100644 (file)
@@ -658,8 +658,8 @@ static void delete_file(e2fsck_t ctx, ext2_ino_t ino,
                                                     block_buf, delete_file_block, &pb);
        if (pctx.errcode)
                fix_problem(ctx, PR_1B_BLOCK_ITERATE, &pctx);
-       if (ctx->inode_bad_map)
-               ext2fs_unmark_inode_bitmap2(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));
        quota_data_sub(ctx->qctx, &inode, ino, pb.dup_blocks * fs->blocksize);
        quota_data_inodes(ctx->qctx, &inode, ino, -1);
index 882950d..b0e83d5 100644 (file)
  * Pass 2 relies on the following information from previous passes:
  *     - The directory information collected in pass 1.
  *     - The inode_used_map bitmap
- *     - The inode_bad_map bitmap
+ *     - The inode_badness bitmap
  *     - The inode_dir_map bitmap
  *
  * Pass 2 frees the following data structures
- *     - The inode_bad_map bitmap
  *     - The inode_reg_map bitmap
  */
 
@@ -254,10 +253,6 @@ void e2fsck_pass2(e2fsck_t ctx)
        ext2fs_free_mem(&buf);
        ext2fs_free_dblist(fs->dblist);
 
-       if (ctx->inode_bad_map) {
-               ext2fs_free_inode_bitmap(ctx->inode_bad_map);
-               ctx->inode_bad_map = 0;
-       }
        if (ctx->inode_reg_map) {
                ext2fs_free_inode_bitmap(ctx->inode_reg_map);
                ctx->inode_reg_map = 0;
@@ -485,6 +480,7 @@ static _INLINE_ int check_filetype(e2fsck_t ctx,
 {
        int     filetype = dirent->name_len >> 8;
        int     should_be = EXT2_FT_UNKNOWN;
+       __u16   result;
        struct ext2_inode       inode;
 
        if (!(ctx->fs->super->s_feature_incompat &
@@ -496,16 +492,18 @@ static _INLINE_ int check_filetype(e2fsck_t ctx,
                return 1;
        }
 
+       if (ctx->inode_badness)
+               ext2fs_icount_fetch(ctx->inode_badness, dirent->inode,
+                                       &result);
+
        if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
                should_be = EXT2_FT_DIR;
        } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
                                            dirent->inode)) {
                should_be = EXT2_FT_REG_FILE;
-       } else if (ctx->inode_bad_map &&
-                  ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
-                                           dirent->inode))
+       } else if (ctx->inode_badness && result >= BADNESS_BAD_MODE) {
                should_be = 0;
-       else {
+       else {
                e2fsck_read_inode(ctx, dirent->inode, &inode,
                                  "check_filetype");
                should_be = ext2_file_type(inode.i_mode);
@@ -953,11 +951,9 @@ out_htree:
                 * (We wait until now so that we can display the
                 * pathname to the user.)
                 */
-               if (ctx->inode_bad_map &&
-                   ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
-                                            dirent->inode)) {
-                       if (e2fsck_process_bad_inode(ctx, ino,
-                                                    dirent->inode,
+               if (ctx->inode_badness &&
+                   ext2fs_icount_is_set(ctx->inode_badness, dirent->inode)) {
+                       if (e2fsck_process_bad_inode(ctx, ino, dirent->inode,
                                                     buf + fs->blocksize)) {
                                dirent->inode = 0;
                                dir_modified++;
@@ -1187,9 +1183,17 @@ static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
        struct problem_context  pctx;
        __u32                   count;
        struct del_block        del_block;
+       int extent_fs = 0;
 
        e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
+       /* ext2fs_block_iterate2() depends on the extents flags */
+       if (inode.i_flags & EXT4_EXTENTS_FL)
+               extent_fs = 1;
        e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
+       if (extent_fs) {
+               inode.i_flags |= EXT4_EXTENTS_FL;
+               e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
+       }
        clear_problem_context(&pctx);
        pctx.ino = ino;
 
@@ -1220,6 +1224,8 @@ static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
                        ext2fs_block_alloc_stats2(fs,
                                  ext2fs_file_acl_block(fs, &inode), -1);
                }
+               if (ctx->inode_badness)
+                       ext2fs_icount_store(ctx->inode_badness, ino, 0);
                ext2fs_file_acl_block_set(fs, &inode, 0);
        }
 
@@ -1265,8 +1271,11 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
        int                     not_fixed = 0;
        unsigned char           *frag, *fsize;
        struct problem_context  pctx;
-       int     problem = 0;
+       int                     problem = 0;
+       __u16                   badness;
 
+       if (ctx->inode_badness)
+               ext2fs_icount_fetch(ctx->inode_badness, ino, &badness);
        e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
 
        clear_problem_context(&pctx);
@@ -1281,6 +1290,7 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                        inode_modified++;
                } else
                        not_fixed++;
+               badness += BADNESS_NORMAL;
        }
 
        if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
@@ -1314,6 +1324,11 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                } else
                        not_fixed++;
                problem = 0;
+               /*
+                * A high value is associated with bad mode in order to detect
+                * that mode was corrupt in check_filetype()
+                */
+               badness += BADNESS_BAD_MODE;
        }
 
        if (inode.i_faddr) {
@@ -1322,6 +1337,7 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                        inode_modified++;
                } else
                        not_fixed++;
+               badness += BADNESS_NORMAL;
        }
 
        switch (fs->super->s_creator_os) {
@@ -1339,6 +1355,7 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                        inode_modified++;
                } else
                        not_fixed++;
+               badness += BADNESS_NORMAL;
                pctx.num = 0;
        }
        if (fsize && *fsize) {
@@ -1348,9 +1365,26 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                        inode_modified++;
                } else
                        not_fixed++;
+               badness += BADNESS_NORMAL;
                pctx.num = 0;
        }
 
+       /* In pass1 these conditions were used to mark inode bad so that
+        * it calls e2fsck_process_bad_inode and make an extensive check
+        * plus prompt for action to be taken. To compensate for badness
+        * incremented in pass1 by this condition, decrease it.
+        */
+       if ((inode.i_faddr || frag || fsize ||
+            (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl)) ||
+           (inode.i_file_acl &&
+            (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
+             (inode.i_file_acl < fs->super->s_first_data_block) ||
+             (inode.i_file_acl >= fs->super->s_blocks_count)))) {
+               /* badness can be 0 if called from pass4. */
+               if (badness)
+                       badness -= BADNESS_NORMAL;
+       }
+
        if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
            !(fs->super->s_feature_ro_compat &
              EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
@@ -1360,6 +1394,8 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                        inode.osd2.linux2.l_i_blocks_hi = 0;
                        inode_modified++;
                }
+               /* Badness was increased in pass1 for this condition */
+               /* badness += BADNESS_NORMAL; */
        }
 
        if (!(fs->super->s_feature_incompat & 
@@ -1371,6 +1407,7 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                        inode_modified++;
                } else
                        not_fixed++;
+               badness += BADNESS_NORMAL;
        }
 
        if (ext2fs_file_acl_block(fs, &inode) &&
@@ -1381,6 +1418,7 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                        inode_modified++;
                } else
                        not_fixed++;
+               badness += BADNESS_NORMAL;
        }
        if (inode.i_dir_acl &&
            LINUX_S_ISDIR(inode.i_mode)) {
@@ -1389,12 +1427,29 @@ extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                        inode_modified++;
                } else
                        not_fixed++;
+               badness += BADNESS_NORMAL;
+       }
+
+       /*
+        * The high value due to BADNESS_BAD_MODE should not delete the inode.
+        */
+       if (ctx->inode_badness && (badness - (badness >= BADNESS_BAD_MODE ?
+                                             BADNESS_BAD_MODE : 0)) >=
+           ctx->inode_badness_threshold) {
+               pctx.num = badness;
+               if (fix_problem(ctx, PR_2_INODE_TOOBAD, &pctx)) {
+                       deallocate_inode(ctx, ino, 0);
+                       if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
+                               return 0;
+                       return 1;
+               }
+               not_fixed++;
        }
 
        if (inode_modified)
                e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
-       if (!not_fixed && ctx->inode_bad_map)
-               ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
+       if (ctx->inode_badness)
+               ext2fs_icount_store(ctx->inode_badness, ino, 0);
        return 0;
 }
 
index 1e63280..874e07f 100644 (file)
@@ -183,6 +183,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);
index 25df7ed..f1a270d 100644 (file)
@@ -995,6 +995,11 @@ static struct e2fsck_problem problem_table[] = {
             "without deletion of an EA.\n"),
          PROMPT_FIX, 0 },
 
+       /* invalid inode creation time */
+       { PR_1_CRTIME_BAD,
+         N_("@i %i creation time (%t) invalid.\n"),
+         PROMPT_CLEAR, PR_PREEN_OK | PR_NO_OK },
+
 
        /* Pass 1b errors */
 
@@ -1426,6 +1431,11 @@ static struct e2fsck_problem problem_table[] = {
          N_("i_file_acl_hi @F %N, @s zero.\n"),
          PROMPT_CLEAR, 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 */
index f3d1bb9..a1493bc 100644 (file)
@@ -590,6 +590,9 @@ struct problem_context {
  */
 #define PR_1_CLEAR_EXTRA_ISIZE         0x010078
 
+/* invalid inode creation time */
+#define PR_1_CRTIME_BAD                        0x010079
+
 
 /*
  * Pass 1b errors
@@ -863,6 +866,9 @@ struct problem_context {
 /* i_file_acl_hi should be zero */
 #define PR_2_I_FILE_ACL_HI_ZERO                0x020048
 
+/* Inode completely corrupt */
+#define PR_2_INODE_TOOBAD              0x020049
+
 /*
  * Pass 3 errors
  */
index 7b7c9c6..62c4fc4 100644 (file)
@@ -811,8 +811,7 @@ void check_super_block(e2fsck_t ctx)
         * write time is in the future.
         */
        if (!broken_system_clock &&
-           !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
-           fs->super->s_mtime > (__u32) ctx->now) {
+           EXT4_XTIME_FUTURE(ctx, fs->super, fs->super->s_mtime, 0)) {
                pctx.num = fs->super->s_mtime;
                problem = PR_0_FUTURE_SB_LAST_MOUNT;
                if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
@@ -823,8 +822,7 @@ void check_super_block(e2fsck_t ctx)
                }
        }
        if (!broken_system_clock &&
-           !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
-           fs->super->s_wtime > (__u32) ctx->now) {
+           EXT4_XTIME_FUTURE(ctx, fs->super, fs->super->s_wtime, 0)) {
                pctx.num = fs->super->s_wtime;
                problem = PR_0_FUTURE_SB_LAST_WRITE;
                if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
index 6ecfbb5..75b82d3 100644 (file)
@@ -729,6 +729,18 @@ static void parse_extended_opts(e2fsck_t ctx, const char *opts)
                                extended_usage++;
                                continue;
                        }
+               /* -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);
+                       if (*p != '\0' || ctx->inode_badness_threshold > 200) {
+                               fprintf(stderr, _("Invalid badness value.\n"));
+                               extended_usage++;
+                               continue;
+                       }
                } else if (strcmp(token, "journal_only") == 0) {
                        if (arg) {
                                extended_usage++;
@@ -768,6 +780,7 @@ static void parse_extended_opts(e2fsck_t ctx, const char *opts)
                fputs(("\tshared=<preserve|lost+found|delete>\n"), stderr);
                fputs(("\tclone=<dup|zero>\n"), stderr);
                fputs(("\texpand_extra_isize\n"), stderr);
+               fputs(("\tinode_badness_threhold=(value)\n"), stderr);
                fputc('\n', stderr);
                exit(1);
        }
@@ -834,6 +847,8 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
 
        initialize_profile_options(ctx);
 
+       ctx->inode_badness_threshold = BADNESS_THRESHOLD;
+
        while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
                switch (c) {
                case 'C':
index 949fe7c..a0fed17 100644 (file)
@@ -1245,6 +1245,7 @@ extern errcode_t ext2fs_initialize(const char *name, int flags,
 
 /* icount.c */
 extern void ext2fs_free_icount(ext2_icount_t icount);
+extern int ext2fs_icount_is_set(ext2_icount_t icount, ext2_ino_t ino);
 extern errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
                                          int flags, ext2_icount_t *ret);
 extern errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags,
index 84b74a9..22dd0c9 100644 (file)
@@ -113,8 +113,9 @@ static errcode_t alloc_icount(ext2_filsys fs, int flags, ext2_icount_t *ret)
                                                      &icount->multiple);
                if (retval)
                        goto errout;
-       } else
+       } else {
                icount->multiple = 0;
+       }
 
        icount->magic = EXT2_ET_MAGIC_ICOUNT;
        icount->num_inodes = fs->super->s_inodes_count;
@@ -453,6 +454,23 @@ static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
        return 0;
 }
 
+int ext2fs_icount_is_set(ext2_icount_t icount, ext2_ino_t ino)
+{
+       __u16 result;
+
+       if (ext2fs_test_inode_bitmap2(icount->single, ino))
+               return 1;
+       else if (icount->multiple) {
+               if (ext2fs_test_inode_bitmap2(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;
index 708f1da..cc4819e 100644 (file)
@@ -1,38 +1,52 @@
 Filesystem did not have a UUID; generating one.
 
 Pass 1: Checking inodes, blocks, and sizes
+check_ext_attr:: increase inode 14 badness 0 to 1
+process_block:: increase inode 14 badness 1 to 2
 Inode 14 has illegal block(s).  Clear? yes
 
 Illegal block #2 (4294901760) in inode 14.  CLEARED.
+process_block:: increase inode 14 badness 2 to 3
 Illegal block #3 (4294901760) in inode 14.  CLEARED.
+process_block:: increase inode 14 badness 3 to 4
 Illegal block #4 (4294901760) in inode 14.  CLEARED.
+process_block:: increase inode 14 badness 4 to 5
 Illegal block #5 (4294901760) in inode 14.  CLEARED.
+process_block:: increase inode 14 badness 5 to 6
 Illegal block #6 (4294901760) in inode 14.  CLEARED.
+process_block:: increase inode 14 badness 6 to 7
 Illegal block #7 (4294901760) in inode 14.  CLEARED.
+process_block:: increase inode 14 badness 7 to 8
 Illegal block #8 (4294901760) in inode 14.  CLEARED.
+process_block:: increase inode 14 badness 8 to 9
 Illegal block #9 (4294901760) in inode 14.  CLEARED.
+process_block:: increase inode 14 badness 9 to 10
 Illegal block #10 (4294901760) in inode 14.  CLEARED.
+check_blocks:: increase inode 14 badness 10 to 11
 Inode 14, i_size is 18446462598732849291, should be 2048.  Fix? yes
 
+check_blocks:: increase inode 14 badness 11 to 12
 Inode 14, i_blocks is 18, should be 4.  Fix? yes
 
 Pass 2: Checking directory structure
 i_file_acl for inode 14 (/MAKEDEV) is 4294901760, should be zero.
 Clear? yes
 
+Inode 14 is badly corrupt (badness value = 13).  Clear? yes
+
 Pass 3: Checking directory connectivity
 Pass 4: Checking reference counts
 Pass 5: Checking group summary information
 Block bitmap differences:  -(43--49)
 Fix? yes
 
-Free blocks count wrong for group #0 (68, counted=75).
+Free blocks count wrong for group #0 (70, counted=77).
 Fix? yes
 
-Free blocks count wrong (68, counted=75).
+Free blocks count wrong (70, counted=77).
 Fix? yes
 
 
 test_filesys: ***** FILE SYSTEM WAS MODIFIED *****
-test_filesys: 29/32 files (3.4% non-contiguous), 25/100 blocks
+test_filesys: 28/32 files (3.6% non-contiguous), 23/100 blocks
 Exit status is 1
index 1fffb02..fb4e83a 100644 (file)
@@ -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: 29/32 files (0.0% non-contiguous), 25/100 blocks
+test_filesys: 28/32 files (0.0% non-contiguous), 23/100 blocks
 Exit status is 0
diff --git a/tests/f_messy_inode/script b/tests/f_messy_inode/script
new file mode 100644 (file)
index 0000000..fe3a2c5
--- /dev/null
@@ -0,0 +1,5 @@
+FSCK_OPT="-fy -d"
+OUT1=$test_name.1.log
+AFTER_CMD="sed -i -e 's/:[0-9]\{4\}:/::/' $OUT1"
+
+. $cmd_dir/run_e2fsck