Whamcloud - gitweb
e2fsck: fix check of directories over 4GB
authorAndreas Dilger <adilger@whamcloud.com>
Tue, 2 Feb 2021 08:25:49 +0000 (01:25 -0700)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 9 Feb 2021 03:40:42 +0000 (22:40 -0500)
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.

Since it isn't very common to have directories this large, and
unlike sparse files that don't have ill effects if the size is
too large, an too-large directory will have all of the sparse
blocks filled in by e2fsck, so huge directories should still
be viewed with suspicion.  Check for consistency between two of
the three among block count, inode size, and superblock large_dir
flag before deciding whether the directory inode should be fixed
or cleared, or if large_dir 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>
Lustre-bug-id: https://jira.whamcloud.com/browse/LU-14345
Change-Id: I1b898cdab95d239ba1a7b37eb96255acadce7057
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
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 51fc2a9..1e295e3 100644 (file)
@@ -186,6 +186,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;
 
        for (i=0; i < MAX_EXTENT_DEPTH_COUNT; i++)
                ctx->extent_depth_count[i] = 0;
index b5b16f0..15d043e 100644 (file)
@@ -415,6 +415,7 @@ struct e2fsck_struct {
        __u32 fs_fragmented;
        __u32 fs_fragmented_dir;
        __u32 large_files;
+       __u32 large_dirs;
        __u32 fs_ext_attr_inodes;
        __u32 fs_ext_attr_blocks;
        __u32 extent_depth_count[MAX_EXTENT_DEPTH_COUNT];
index 727f71d..05d914d 100644 (file)
@@ -281,10 +281,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 b866cc8..7d39097 100644 (file)
@@ -2058,6 +2058,21 @@ void e2fsck_pass1(e2fsck_t ctx)
                goto endit;
        }
 
+       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->options & E2F_OPT_PREEN) {
                        clear_problem_context(&pctx);
@@ -2704,10 +2719,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;
 }
 
@@ -2854,7 +2889,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;
@@ -3462,8 +3498,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;
@@ -3477,11 +3514,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;
@@ -3491,7 +3528,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) &&
@@ -3514,8 +3550,6 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                        pctx->num = (pb.last_block + 1) * fs->blocksize;
                pctx->group = bad_size;
                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)) {
@@ -3694,6 +3728,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 8b7f84a..9d682a9 100644 (file)
@@ -1217,6 +1217,9 @@ inline_read_fail:
                        root = (struct ext2_dx_root_info *) (buf + 24);
                        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,
+                        * so ext2_dir_htree_level() should now be correct */
                        if ((root->reserved_zero ||
                             root->info_length < 8 ||
                             root->indirect_levels >=
@@ -1857,9 +1860,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;
@@ -2019,6 +2025,7 @@ int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
                        not_fixed++;
        }
        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 fe3b06a..8d90739 100644 (file)
@@ -1666,7 +1666,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,
@@ -1692,6 +1692,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"),
@@ -1702,7 +1707,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 922c99d..24cdcf9 100644 (file)
@@ -963,8 +963,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