Whamcloud - gitweb
LU-14345 e2fsck: fix check of directories over 4GB 85/41385/2
authorAndreas Dilger <adilger@whamcloud.com>
Mon, 1 Feb 2021 21:55:06 +0000 (14:55 -0700)
committerAndreas Dilger <adilger@whamcloud.com>
Wed, 3 Feb 2021 07:43:01 +0000 (07:43 +0000)
If directories grow larger than 4GB in size with the large_dir
feature, e2fsck will consider them to be corrupted and clear
the high bits of the size.  Previous checking appears to have
been done only beyond the prior 2GB directory size limit.

Since it isn't very common to have directories this large, and
unlike sparse files that don't have any ill effect if the size
is wrong, an overly-large directory will have all of the sparse
blocks filled in by e2fsck, so such directories should still
be viewed with suspicion.  Check for consistency between two of
the three block count, inode size, and superblock large_dir flag
before deciding whether the directory should be fixed or cleared,
or if the large_dir feature should be set in the superblock.

Update the f_recnect_bad test case to match new output.

Fixes: 49f28a06b738 ("e2fsck: allow to check >2GB sized directory")
Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Change-Id: I1b898cdab95d239ba1a7b37eb96255acadce7057
Reviewed-on: https://review.whamcloud.com/41385
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Wang Shilong <wshilong@whamcloud.com>
Reviewed-by: Artem Blagodarenko <artem.blagodarenko@hpe.com>
12 files changed:
e2fsck/e2fsck.c
e2fsck/e2fsck.h
e2fsck/message.c
e2fsck/pass1.c
e2fsck/pass2.c
e2fsck/problem.c
e2fsck/problem.h
lib/ext2fs/blknum.c
lib/ext2fs/expanddir.c
lib/ext2fs/punch.c
lib/ext2fs/res_gdt.c
tests/f_recnect_bad/expect.1

index 277d9c8..c3a98b0 100644 (file)
@@ -189,6 +189,7 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx)
        ctx->fs_fragmented = 0;
        ctx->fs_fragmented_dir = 0;
        ctx->large_files = 0;
+       ctx->large_dirs = 0;
        ctx->fs_unexpanded_inodes = 0;
 #ifdef CONFIG_PFSCK
        ctx->fs_need_locking = 0;
index 6ef58b1..a7d0884 100644 (file)
@@ -491,6 +491,7 @@ struct e2fsck_struct {
        __u32                   fs_fragmented;
        __u32                   fs_fragmented_dir;
        __u32                   large_files;
+       __u32                   large_dirs;
        __u32                   extent_depth_count[MAX_EXTENT_DEPTH_COUNT];
 
 #ifdef CONFIG_PFSCK
index eca280a..1ab0271 100644 (file)
@@ -282,10 +282,7 @@ static _INLINE_ void expand_inode_expression(FILE *f, ext2_filsys fs, char ch,
 
        switch (ch) {
        case 's':
-               if (LINUX_S_ISDIR(inode->i_mode))
-                       fprintf(f, "%u", inode->i_size);
-               else
-                       fprintf(f, "%llu", EXT2_I_SIZE(inode));
+               fprintf(f, "%llu", EXT2_I_SIZE(inode));
                break;
        case 'S':
                fprintf(f, "%u", large_inode->i_extra_isize);
index 4e8d099..696c411 100644 (file)
@@ -1660,6 +1660,21 @@ static void e2fsck_pass1_post(e2fsck_t ctx)
                return;
        }
 
+       if (ctx->large_dirs && !ext2fs_has_feature_largedir(ctx->fs->super)) {
+               ext2_filsys fs = ctx->fs;
+
+               if (fix_problem(ctx, PR_2_FEATURE_LARGE_DIRS, &pctx)) {
+                       ext2fs_set_feature_largedir(fs->super);
+                       fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
+                       ext2fs_mark_super_dirty(fs);
+               }
+               if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
+                   fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
+                       ext2fs_update_dynamic_rev(fs);
+                       ext2fs_mark_super_dirty(fs);
+               }
+       }
+
        if (ctx->block_dup_map) {
                if (!(ctx->flags & E2F_FLAG_DUP_BLOCK)) {
                        ext2fs_free_mem(&block_buf);
@@ -3254,6 +3269,7 @@ static errcode_t e2fsck_pass1_merge_context(e2fsck_t global_ctx,
        global_ctx->fs_fragmented += thread_ctx->fs_fragmented;
        global_ctx->fs_fragmented_dir += thread_ctx->fs_fragmented_dir;
        global_ctx->large_files += thread_ctx->large_files;
+       global_ctx->large_dirs += thread_ctx->large_dirs;
        /* threads might enable E2F_OPT_YES */
        global_ctx->options |= thread_ctx->options;
        global_ctx->flags |= thread_ctx->flags;
@@ -4271,10 +4287,30 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
                return 1;
 
        pctx->num = root->indirect_levels;
-       if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
+       /* if htree level is clearly too high, consider it to be broken */
+       if (root->indirect_levels > EXT4_HTREE_LEVEL &&
            fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
                return 1;
 
+       /* if level is only maybe too high, LARGE_DIR feature could be unset */
+       if (root->indirect_levels > ext2_dir_htree_level(fs) &&
+           !ext2fs_has_feature_largedir(fs->super)) {
+               int blockbits = EXT2_BLOCK_SIZE_BITS(fs->super) + 10;
+               int idx_pb = 1 << (blockbits - 3);
+
+               /* compare inode size/blocks vs. max-sized 2-level htree */
+               if (EXT2_I_SIZE(pctx->inode) <
+                   (idx_pb - 1) * (idx_pb - 2) << blockbits &&
+                   pctx->inode->i_blocks <
+                   (idx_pb - 1) * (idx_pb - 2) << (blockbits - 9) &&
+                   fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
+                       return 1;
+       }
+
+       if (root->indirect_levels > EXT4_HTREE_LEVEL_COMPAT ||
+           ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
+               ctx->large_dirs++;
+
        return 0;
 }
 
@@ -4421,7 +4457,8 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                         (extent.e_pblk + extent.e_len) >
                         ext2fs_blocks_count(ctx->fs->super))
                        problem = PR_1_EXTENT_ENDS_BEYOND;
-               else if (is_leaf && is_dir &&
+               else if (is_leaf && is_dir && !pctx->inode->i_size_high &&
+                        !ext2fs_has_feature_largedir(ctx->fs->super) &&
                         ((extent.e_lblk + extent.e_len) >
                          (1U << (21 - ctx->fs->super->s_log_block_size))))
                        problem = PR_1_TOOBIG_DIR;
@@ -5039,8 +5076,9 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
               ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
               pb.num_blocks);
 #endif
+       size = EXT2_I_SIZE(inode);
        if (pb.is_dir) {
-               unsigned nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
+               unsigned nblock = size >> EXT2_BLOCK_SIZE_BITS(fs->super);
                if (inode->i_flags & EXT4_INLINE_DATA_FL) {
                        int flags;
                        size_t sz = 0;
@@ -5054,11 +5092,11 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                                          EXT2_FLAG_IGNORE_CSUM_ERRORS) |
                                         (ctx->fs->flags &
                                          ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
-                       if (err || sz != inode->i_size) {
+                       if (err || sz != size) {
                                bad_size = 7;
                                pctx->num = sz;
                        }
-               } else if (inode->i_size & (fs->blocksize - 1))
+               } else if (size & (fs->blocksize - 1))
                        bad_size = 5;
                else if (nblock > (pb.last_block + 1))
                        bad_size = 1;
@@ -5068,7 +5106,6 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                                bad_size = 2;
                }
        } else {
-               size = EXT2_I_SIZE(inode);
                if ((pb.last_init_lblock >= 0) &&
                    /* Do not allow initialized allocated blocks past i_size*/
                    (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
@@ -5090,10 +5127,9 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                if (bad_size != 7)
                        pctx->num = (pb.last_block + 1) * fs->blocksize;
                pctx->group = bad_size;
-               e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
+               e2fsck_mark_inode_bad(ctx, ino, pb.is_dir ? BADNESS_HIGH :
+                                                           BADNESS_NORMAL);
                if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
-                       if (LINUX_S_ISDIR(inode->i_mode))
-                               pctx->num &= 0xFFFFFFFFULL;
                        ext2fs_inode_size_set(fs, inode, pctx->num);
                        if (EXT2_I_SIZE(inode) == 0 &&
                            (inode->i_flags & EXT4_INLINE_DATA_FL)) {
@@ -5273,6 +5309,7 @@ static int process_block(ext2_filsys fs,
        }
 
        if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
+           !pctx->inode->i_size_high &&
            blockcnt > (1 << (21 - fs->super->s_log_block_size)))
                problem = PR_1_TOOBIG_DIR;
        if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
index cf89d11..c589a4e 100644 (file)
@@ -1176,6 +1176,9 @@ inline_read_fail:
                        root = get_ext2_dx_root_info(fs, buf);
                        dx_db->type = DX_DIRBLOCK_ROOT;
                        dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
+
+                       /* large_dir was set in pass1 if large dirs were found,
+                        * ext2_dir_htree_levels() should now be correct */
                        if ((root->reserved_zero ||
                             root->info_length < 8 ||
                             root->indirect_levels >=
@@ -1798,9 +1801,12 @@ static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
        if (inode.i_flags & EXT4_INLINE_DATA_FL)
                goto clear_inode;
 
-       if (LINUX_S_ISREG(inode.i_mode) &&
-           ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
-               ctx->large_files--;
+       if (ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode))) {
+               if (LINUX_S_ISREG(inode.i_mode))
+                   ctx->large_files--;
+               else if (LINUX_S_ISDIR(inode.i_mode))
+                   ctx->large_dirs--;
+       }
 
        del_block.ctx = ctx;
        del_block.num = 0;
@@ -1992,6 +1998,7 @@ int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                badness += BADNESS_NORMAL;
        }
        if (inode.i_size_high && !ext2fs_has_feature_largedir(fs->super) &&
+           inode.i_blocks < 1ULL << (29 - EXT2_BLOCK_SIZE_BITS(fs->super)) &&
            LINUX_S_ISDIR(inode.i_mode)) {
                if (fix_problem(ctx, PR_2_DIR_SIZE_HIGH_ZERO, &pctx)) {
                        inode.i_size_high = 0;
index 1930429..ab94a1b 100644 (file)
@@ -1701,7 +1701,7 @@ static struct e2fsck_problem problem_table[] = {
        /* Filesystem contains large files, but has no such flag in sb */
        { PR_2_FEATURE_LARGE_FILES,
          N_("@f contains large files, but lacks LARGE_FILE flag in @S.\n"),
-         PROMPT_FIX, 0, 0, 0, 0 },
+         PROMPT_FIX, PR_PREEN_OK, 0, 0, 0 },
 
        /* Node in HTREE directory not referenced */
        { PR_2_HTREE_NOTREF,
@@ -1727,6 +1727,11 @@ static struct e2fsck_problem problem_table[] = {
        { PR_2_HTREE_CLEAR,
          N_("@n @h %d (%q).  "), PROMPT_CLEAR_HTREE, 0, 0, 0, 0 },
 
+       /* Filesystem has large directories, but has no such flag in sb */
+       { PR_2_FEATURE_LARGE_DIRS,
+         N_("@f has large directories, but lacks LARGE_DIR flag in @S.\n"),
+         PROMPT_FIX, PR_PREEN_OK, 0, 0, 0 },
+
        /* Bad block in htree interior node */
        { PR_2_HTREE_BADBLK,
          N_("@p @h %d (%q): bad @b number %b.\n"),
@@ -1737,7 +1742,7 @@ static struct e2fsck_problem problem_table[] = {
          N_("Error adjusting refcount for @a @b %b (@i %i): %m\n"),
          PROMPT_NONE, PR_FATAL, 0, 0, 0 },
 
-       /* Invalid HTREE root node */
+       /* Problem in HTREE directory inode: root node is invalid */
        { PR_2_HTREE_BAD_ROOT,
          /* xgettext:no-c-format */
          N_("@p @h %d: root node is @n\n"),
index 2b2083f..d060d2c 100644 (file)
@@ -991,8 +991,8 @@ struct problem_context {
 /* Clear invalid HTREE directory */
 #define PR_2_HTREE_CLEAR       0x020038
 
-/* Clear the htree flag forcibly */
-/* #define PR_2_HTREE_FCLR     0x020039 */
+/* Filesystem has large directories, but has no such flag in superblock */
+#define PR_2_FEATURE_LARGE_DIRS        0x020039
 
 /* Bad block in htree interior node */
 #define PR_2_HTREE_BADBLK      0x02003A
index 7c3c6b5..3458b12 100644 (file)
@@ -573,18 +573,29 @@ errcode_t ext2fs_inode_size_set(ext2_filsys fs, struct ext2_inode *inode,
        if (size < 0)
                return EINVAL;
 
-       /* Only regular files get to be larger than 4GB */
-       if (!LINUX_S_ISREG(inode->i_mode) && (size >> 32))
-               return EXT2_ET_FILE_TOO_BIG;
-
-       /* If we're writing a large file, set the large_file flag */
-       if (LINUX_S_ISREG(inode->i_mode) &&
-           ext2fs_needs_large_file_feature(size) &&
-           (!ext2fs_has_feature_large_file(fs->super) ||
-            fs->super->s_rev_level == EXT2_GOOD_OLD_REV)) {
-               ext2fs_set_feature_large_file(fs->super);
-               ext2fs_update_dynamic_rev(fs);
-               ext2fs_mark_super_dirty(fs);
+       /* If writing a large inode, set the large_file or large_dir flag */
+       if (ext2fs_needs_large_file_feature(size)) {
+               int dirty_sb = 0;
+
+               if (LINUX_S_ISREG(inode->i_mode)) {
+                       if (!ext2fs_has_feature_large_file(fs->super)) {
+                               ext2fs_set_feature_large_file(fs->super);
+                               dirty_sb = 1;
+                       }
+               } else if (LINUX_S_ISDIR(inode->i_mode)) {
+                       if (!ext2fs_has_feature_largedir(fs->super)) {
+                               ext2fs_set_feature_largedir(fs->super);
+                               dirty_sb = 1;
+                       }
+               } else {
+                       /* Only regular files get to be larger than 4GB */
+                       return EXT2_ET_FILE_TOO_BIG;
+               }
+               if (dirty_sb) {
+                       if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV)
+                               ext2fs_update_dynamic_rev(fs);
+                       ext2fs_mark_super_dirty(fs);
+               }
        }
 
        inode->i_size = size & 0xffffffff;
index 9f02312..b5d5abd 100644 (file)
@@ -129,7 +129,10 @@ errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir)
        if (retval)
                return retval;
 
-       inode.i_size += fs->blocksize;
+       retval = ext2fs_inode_size_set(fs, &inode,
+                                      EXT2_I_SIZE(&inode) + fs->blocksize);
+       if (retval)
+               return retval;
        ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
 
        retval = ext2fs_write_inode(fs, dir, &inode);
index c704bf3..effa1e2 100644 (file)
@@ -502,8 +502,8 @@ errcode_t ext2fs_punch(ext2_filsys fs, ext2_ino_t ino,
                return retval;
 
 #ifdef PUNCH_DEBUG
-       printf("%u: write inode size now %u blocks %u\n",
-               ino, inode->i_size, inode->i_blocks);
+       printf("%u: write inode size now %lu blocks %u\n",
+               ino, EXT2_I_SIZE(inode), inode->i_blocks);
 #endif
        return ext2fs_write_inode(fs, ino, inode);
 }
index 6bcf01e..fa8d8d6 100644 (file)
@@ -223,8 +223,8 @@ out_dindir:
        }
 out_inode:
 #ifdef RES_GDT_DEBUG
-       printf("inode.i_blocks = %u, i_size = %u\n", inode.i_blocks,
-              inode.i_size);
+       printf("inode.i_blocks = %u, i_size = %lu\n", inode.i_blocks,
+              EXT2_I_SIZE(&inode));
 #endif
        if (inode_dirty) {
                inode.i_atime = inode.i_mtime = fs->now ? fs->now : time(0);
index d4f72a1..97ffcc5 100644 (file)
@@ -1,11 +1,10 @@
 Pass 1: Checking inodes, blocks, and sizes
+Inode 15, i_size is 51539608576, should be 1024.  Fix? yes
+
 Pass 2: Checking directory structure
 i_faddr for inode 15 (/test/quux) is 23, should be zero.
 Clear? yes
 
-i_size_high for inode 15 (/test/quux) is 12, should be zero.
-Clear? yes
-
 i_file_acl for inode 13 (/test/???) is 12, should be zero.
 Clear? yes