Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / e2fsck / pass1.c
index b5d3f12..bd89b96 100644 (file)
@@ -274,16 +274,15 @@ static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
        struct ext2_super_block *sb = ctx->fs->super;
        struct ext2_inode_large *inode;
        struct ext2_ext_attr_entry *entry;
-       char *start, *end;
+       char *start;
        unsigned int storage_size, remain;
-       int problem = 0;
+       problem_t problem = 0;
 
        inode = (struct ext2_inode_large *) pctx->inode;
        storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
                inode->i_extra_isize;
        start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
                inode->i_extra_isize + sizeof(__u32);
-       end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super);
        entry = (struct ext2_ext_attr_entry *) start;
 
        /* scan all entry's headers first */
@@ -308,7 +307,7 @@ static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
                remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
 
                /* check value size */
-               if (entry->e_value_size == 0 || entry->e_value_size > remain) {
+               if (entry->e_value_size > remain) {
                        pctx->num = entry->e_value_size;
                        problem = PR_1_ATTR_VALUE_SIZE;
                        goto fix;
@@ -483,7 +482,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
        retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
        if (retval)
                return;
-       if (((dirent->name_len & 0xFF) != 1) ||
+       if ((ext2fs_dirent_name_len(dirent) != 1) ||
            (dirent->name[0] != '.') ||
            (dirent->inode != pctx->ino) ||
            (rec_len < 12) ||
@@ -495,7 +494,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
        retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
        if (retval)
                return;
-       if (((dirent->name_len & 0xFF) != 2) ||
+       if ((ext2fs_dirent_name_len(dirent) != 2) ||
            (dirent->name[0] != '.') ||
            (dirent->name[1] != '.') ||
            (rec_len < 12) ||
@@ -510,8 +509,8 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
        }
 }
 
-extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
-                                   ext2_icount_t *ret)
+void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
+                            ext2_icount_t *ret)
 {
        unsigned int            threshold;
        ext2_ino_t              num_dirs;
@@ -753,8 +752,8 @@ void e2fsck_pass1(e2fsck_t ctx)
                busted_fs_time = 1;
 
        if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
-           !(fs->super->s_mmp_block <= fs->super->s_first_data_block ||
-             fs->super->s_mmp_block >= fs->super->s_blocks_count))
+           fs->super->s_mmp_block > fs->super->s_first_data_block &&
+           fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
                ext2fs_mark_block_bitmap2(ctx->block_found_map,
                                          fs->super->s_mmp_block);
 
@@ -999,7 +998,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                                                        inode_size, "pass1");
                        }
                } else if (ino < EXT2_FIRST_INODE(fs->super)) {
-                       int     problem = 0;
+                       problem_t problem = 0;
 
                        ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                        if (ino == EXT2_BOOT_LOADER_INO) {
@@ -1849,15 +1848,16 @@ void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
 
 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                             struct process_block_struct *pb,
-                            blk64_t start_block,
+                            blk64_t start_block, blk64_t end_block,
+                            blk64_t eof_block,
                             ext2_extent_handle_t ehandle)
 {
        struct ext2fs_extent    extent;
-       blk64_t                 blk;
+       blk64_t                 blk, last_lblk;
        e2_blkcnt_t             blockcnt;
        unsigned int            i;
        int                     is_dir, is_leaf;
-       errcode_t               problem;
+       problem_t               problem;
        struct ext2_extent_info info;
        int                     failed_csum;
 
@@ -1873,6 +1873,7 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                failed_csum = 0;
                is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
                is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
+               last_lblk = extent.e_lblk + extent.e_len - 1;
 
                problem = 0;
                /* Ask to clear a corrupt extent block */
@@ -1892,12 +1893,20 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                        problem = PR_1_EXTENT_BAD_START_BLK;
                else if (extent.e_lblk < start_block)
                        problem = PR_1_OUT_OF_ORDER_EXTENTS;
+               else if ((end_block && last_lblk > end_block) &&
+                        (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
+                               last_lblk > eof_block)))
+                       problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
                else if (is_leaf && extent.e_len == 0)
                        problem = PR_1_EXTENT_LENGTH_ZERO;
                else if (is_leaf &&
                         (extent.e_pblk + extent.e_len) >
                         ext2fs_blocks_count(ctx->fs->super))
                        problem = PR_1_EXTENT_ENDS_BEYOND;
+               else if (is_leaf && is_dir &&
+                        ((extent.e_lblk + extent.e_len) >
+                         (1 << (21 - ctx->fs->super->s_log_block_size))))
+                       problem = PR_1_TOOBIG_DIR;
 
                /* Corrupt but passes checks?  Ask to fix checksum. */
                if (failed_csum) {
@@ -1911,10 +1920,11 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                }
 
                if (problem) {
-               report_problem:
+report_problem:
                        pctx->blk = extent.e_pblk;
                        pctx->blk2 = extent.e_lblk;
                        pctx->num = extent.e_len;
+                       pctx->blkcount = extent.e_lblk + extent.e_len;
                        if (fix_problem(ctx, problem, pctx)) {
 fix_problem_now:
                                e2fsck_read_bitmaps(ctx);
@@ -1924,6 +1934,7 @@ fix_problem_now:
                                        pctx->str = "ext2fs_extent_delete";
                                        return;
                                }
+                               ext2fs_extent_fix_parents(ehandle);
                                pctx->errcode = ext2fs_extent_get(ehandle,
                                                                  EXT2_EXTENT_CURRENT,
                                                                  &extent);
@@ -1937,6 +1948,8 @@ fix_problem_now:
                }
 
                if (!is_leaf) {
+                       blk64_t lblk = extent.e_lblk;
+
                        blk = extent.e_pblk;
                        pctx->errcode = ext2fs_extent_get(ehandle,
                                                  EXT2_EXTENT_DOWN, &extent);
@@ -1950,7 +1963,20 @@ fix_problem_now:
                                        goto report_problem;
                                return;
                        }
-                       scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
+                       /* The next extent should match this index's logical start */
+                       if (extent.e_lblk != lblk) {
+                               struct ext2_extent_info e_info;
+
+                               ext2fs_extent_get_info(ehandle, &e_info);
+                               pctx->blk = lblk;
+                               pctx->blk2 = extent.e_lblk;
+                               pctx->num = e_info.curr_level - 1;
+                               problem = PR_1_EXTENT_INDEX_START_INVALID;
+                               if (fix_problem(ctx, problem, pctx))
+                                       ext2fs_extent_fix_parents(ehandle);
+                       }
+                       scan_extent_node(ctx, pctx, pb, extent.e_lblk,
+                                        last_lblk, eof_block, ehandle);
                        if (pctx->errcode)
                                return;
                        pctx->errcode = ext2fs_extent_get(ehandle,
@@ -1985,7 +2011,8 @@ fix_problem_now:
                        }
                        pb->fragmented = 1;
                }
-               while (is_dir && ++pb->last_db_block < extent.e_lblk) {
+               while (is_dir && (++pb->last_db_block <
+                                 (e2_blkcnt_t) extent.e_lblk)) {
                        pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
                                                              pb->ino, 0,
                                                              pb->last_db_block);
@@ -2007,7 +2034,7 @@ fix_problem_now:
                              (EXT2FS_B2C(ctx->fs, blk) ==
                               EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
                              (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
-                             (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
+                             ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
                                mark_block_used(ctx, blk);
                                pb->num_blocks++;
                        }
@@ -2030,10 +2057,10 @@ fix_problem_now:
                if (is_dir && extent.e_len > 0)
                        pb->last_db_block = blockcnt - 1;
                pb->previous_block = extent.e_pblk + extent.e_len - 1;
-               start_block = pb->last_block = extent.e_lblk + extent.e_len - 1;
+               start_block = pb->last_block = last_lblk;
                if (is_leaf && !is_dir &&
                    !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
-                       pb->last_init_lblock = extent.e_lblk + extent.e_len - 1;
+                       pb->last_init_lblock = last_lblk;
        next:
                pctx->errcode = ext2fs_extent_get(ehandle,
                                                  EXT2_EXTENT_NEXT_SIB,
@@ -2052,6 +2079,7 @@ static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
        ext2_filsys             fs = ctx->fs;
        ext2_ino_t              ino = pctx->ino;
        errcode_t               retval;
+       blk64_t                 eof_lblk;
 
        pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
        if (pctx->errcode) {
@@ -2069,7 +2097,9 @@ static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
                ctx->extent_depth_count[info.max_depth]++;
        }
 
-       scan_extent_node(ctx, pctx, pb, 0, ehandle);
+       eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
+               EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
+       scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle);
        if (pctx->errcode &&
            fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
                pb->num_blocks = 0;
@@ -2092,7 +2122,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        struct process_block_struct pb;
        ext2_ino_t      ino = pctx->ino;
        struct ext2_inode *inode = pctx->inode;
-       int             bad_size = 0;
+       unsigned        bad_size = 0;
        int             dirty_inode = 0;
        int             extent_fs;
        __u64           size;
@@ -2251,7 +2281,8 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
                pctx->num = 0;
        }
-       if (LINUX_S_ISREG(inode->i_mode) && EXT2_I_SIZE(inode) >= 0x80000000UL)
+       if (LINUX_S_ISREG(inode->i_mode) &&
+           ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
                ctx->large_files++;
        if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
            ((fs->super->s_feature_ro_compat &
@@ -2344,7 +2375,7 @@ static int process_block(ext2_filsys fs,
        struct problem_context *pctx;
        blk64_t blk = *block_nr;
        int     ret_code = 0;
-       int     problem = 0;
+       problem_t       problem = 0;
        e2fsck_t        ctx;
 
        p = (struct process_block_struct *) priv_data;
@@ -2457,7 +2488,7 @@ static int process_block(ext2_filsys fs,
                     (EXT2FS_B2C(ctx->fs, blk) ==
                      EXT2FS_B2C(ctx->fs, p->previous_block)) &&
                     (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
-                    (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
+                    ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
                mark_block_used(ctx, blk);
                p->num_blocks++;
        }
@@ -2655,14 +2686,16 @@ static int process_bad_block(ext2_filsys fs,
        return 0;
 }
 
-static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
+static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
                            const char *name, int num, blk64_t *new_block)
 {
        ext2_filsys fs = ctx->fs;
        dgrp_t          last_grp;
        blk64_t         old_block = *new_block;
        blk64_t         last_block;
-       int             i, is_flexbg, flexbg, flexbg_size;
+       dgrp_t          flexbg;
+       unsigned        flexbg_size;
+       int             i, is_flexbg;
        char            *buf;
        struct problem_context  pctx;
 
@@ -2788,7 +2821,7 @@ static void mark_table_blocks(e2fsck_t ctx)
        ext2_filsys fs = ctx->fs;
        blk64_t b;
        dgrp_t  i;
-       int     j;
+       unsigned int    j;
        struct problem_context pctx;
 
        clear_problem_context(&pctx);