Whamcloud - gitweb
Minor correction to e2fsck.conf man page
[tools/e2fsprogs.git] / e2fsck / pass5.c
index be2edd9..7467854 100644 (file)
@@ -18,7 +18,7 @@ static void check_inode_bitmaps(e2fsck_t ctx);
 static void check_inode_end(e2fsck_t ctx);
 static void check_block_end(e2fsck_t ctx);
 
-void pass5(e2fsck_t ctx)
+void e2fsck_pass5(e2fsck_t ctx)
 {
 #ifdef RESOURCE_TRACK
        struct resource_track   rtrack;
@@ -38,12 +38,24 @@ void pass5(e2fsck_t ctx)
        if (!(ctx->options & E2F_OPT_PREEN))
                fix_problem(ctx, PR_5_PASS_HEADER, &pctx);
 
-       read_bitmaps(ctx);
+       if (ctx->progress)
+               if ((ctx->progress)(ctx, 5, 0, ctx->fs->group_desc_count*2))
+                       return;
+
+       e2fsck_read_bitmaps(ctx);
 
        check_block_bitmaps(ctx);
+       if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
+               return;
        check_inode_bitmaps(ctx);
+       if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
+               return;
        check_inode_end(ctx);
+       if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
+               return;
        check_block_end(ctx);
+       if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
+               return;
 
        ext2fs_free_inode_bitmap(ctx->inode_used_map);
        ctx->inode_used_map = 0;
@@ -53,28 +65,68 @@ void pass5(e2fsck_t ctx)
        ctx->block_found_map = 0;
 
 #ifdef RESOURCE_TRACK
-       if (ctx->options & E2F_OPT_TIME2)
-               print_resource_track("Pass 5", &rtrack);
+       if (ctx->options & E2F_OPT_TIME2) {
+               e2fsck_clear_progbar(ctx);
+               print_resource_track(_("Pass 5"), &rtrack);
+       }
 #endif
 }
 
+#define NO_BLK ((blk_t) -1)
+
+static void print_bitmap_problem(e2fsck_t ctx, int problem,
+                           struct problem_context *pctx)
+{
+       switch (problem) {
+       case PR_5_BLOCK_UNUSED:
+               if (pctx->blk == pctx->blk2)
+                       pctx->blk2 = 0;
+               else
+                       problem = PR_5_BLOCK_RANGE_UNUSED;
+               break;
+       case PR_5_BLOCK_USED:
+               if (pctx->blk == pctx->blk2)
+                       pctx->blk2 = 0;
+               else
+                       problem = PR_5_BLOCK_RANGE_USED;
+               break;
+       case PR_5_INODE_UNUSED:
+               if (pctx->ino == pctx->ino2)
+                       pctx->ino2 = 0;
+               else
+                       problem = PR_5_INODE_RANGE_UNUSED;
+               break;
+       case PR_5_INODE_USED:
+               if (pctx->ino == pctx->ino2)
+                       pctx->ino2 = 0;
+               else
+                       problem = PR_5_INODE_RANGE_USED;
+               break;
+       }
+       fix_problem(ctx, problem, pctx);
+       pctx->blk = pctx->blk2 = NO_BLK;
+       pctx->ino = pctx->ino2 = 0;
+}
+       
 static void check_block_bitmaps(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t   i;
+       blk_t   i, super;
        int     *free_array;
        int     group = 0;
-       int     blocks = 0;
-       int     free_blocks = 0;
+       unsigned int    blocks = 0;
+       unsigned int    free_blocks = 0;
        int     group_free = 0;
        int     actual, bitmap;
        struct problem_context  pctx;
-       int     problem, fixit;
+       int     problem, save_problem, fixit, had_problem;
        errcode_t       retval;
+       int             lazy_bg = 0;
+       int             skip_group = 0;
        
        clear_problem_context(&pctx);
-       free_array = allocate_memory(fs->group_desc_count * sizeof(int),
-                                    "free block count array");
+       free_array = (int *) e2fsck_allocate_memory(ctx,
+           fs->group_desc_count * sizeof(int), "free block count array");
 
        if ((fs->super->s_first_data_block <
             ext2fs_get_block_bitmap_start(ctx->block_found_map)) ||
@@ -86,8 +138,9 @@ static void check_block_bitmaps(e2fsck_t ctx)
                pctx.ino = ext2fs_get_block_bitmap_start(ctx->block_found_map);
                pctx.ino2 = ext2fs_get_block_bitmap_end(ctx->block_found_map);
                fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
-               /* fatal */
-               fatal_error(0);
+
+               ctx->flags |= E2F_FLAG_ABORT; /* fatal */
+               return;
        }
                       
        if ((fs->super->s_first_data_block <
@@ -100,36 +153,79 @@ static void check_block_bitmaps(e2fsck_t ctx)
                pctx.ino = ext2fs_get_block_bitmap_start(fs->block_map);
                pctx.ino2 = ext2fs_get_block_bitmap_end(fs->block_map);
                fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
-               /* fatal */
-               fatal_error(0);
+
+               ctx->flags |= E2F_FLAG_ABORT; /* fatal */
+               return;
        }
                       
-redo_counts:                  
+       if (EXT2_HAS_COMPAT_FEATURE(fs->super, 
+                                   EXT2_FEATURE_COMPAT_LAZY_BG))
+               lazy_bg++;
+
+redo_counts:
+       had_problem = 0;
+       save_problem = 0;
+       pctx.blk = pctx.blk2 = NO_BLK;
+       if (lazy_bg && (fs->group_desc[group].bg_flags &
+                       EXT2_BG_BLOCK_UNINIT))
+               skip_group++;
+       super = fs->super->s_first_data_block;
        for (i = fs->super->s_first_data_block;
             i < fs->super->s_blocks_count;
             i++) {
                actual = ext2fs_fast_test_block_bitmap(ctx->block_found_map, i);
-               bitmap = ext2fs_fast_test_block_bitmap(fs->block_map, i);
+
+               if (skip_group) {
+                       if ((i >= super) &&
+                           (i <= super + fs->desc_blocks) &&
+                           ext2fs_bg_has_super(fs, group))
+                               bitmap = 1;
+                       else if (i == fs->group_desc[group].bg_block_bitmap)
+                               bitmap = 1;
+                       else if (i == fs->group_desc[group].bg_inode_bitmap)
+                               bitmap = 1;
+                       else if (i >= fs->group_desc[group].bg_inode_table &&
+                                (i < fs->group_desc[group].bg_inode_table
+                                 + fs->inode_blocks_per_group))
+                               bitmap = 1;
+                       else
+                               bitmap = 0;
+                       actual = (actual != 0);
+               } else
+                       bitmap = ext2fs_fast_test_block_bitmap(fs->block_map, i);
                
                if (actual == bitmap)
                        goto do_counts;
-               
+
                if (!actual && bitmap) {
                        /*
                         * Block not used, but marked in use in the bitmap.
                         */
-                       problem = PR_5_UNUSED_BLOCK;
+                       problem = PR_5_BLOCK_UNUSED;
                } else {
                        /*
                         * Block used, but not marked in use in the bitmap.
                         */
                        problem = PR_5_BLOCK_USED;
                }
-               pctx.blk = i;
-               fix_problem(ctx, problem, &pctx);
+               if (pctx.blk == NO_BLK) {
+                       pctx.blk = pctx.blk2 = i;
+                       save_problem = problem;
+               } else {
+                       if ((problem == save_problem) &&
+                           (pctx.blk2 == i-1))
+                               pctx.blk2++;
+                       else {
+                               print_bitmap_problem(ctx, save_problem, &pctx);
+                               pctx.blk = pctx.blk2 = i;
+                               save_problem = problem;
+                       }
+               }
+               ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
+               had_problem++;
                
        do_counts:
-               if (!bitmap) {
+               if (!bitmap && !skip_group) {
                        group_free++;
                        free_blocks++;
                }
@@ -140,14 +236,37 @@ redo_counts:
                        group ++;
                        blocks = 0;
                        group_free = 0;
+                       skip_group = 0;
+                       super += fs->super->s_blocks_per_group;
+                       if (ctx->progress)
+                               if ((ctx->progress)(ctx, 5, group,
+                                                   fs->group_desc_count*2))
+                                       return;
+                       if (lazy_bg &&
+                           (i != fs->super->s_blocks_count-1) &&
+                           (fs->group_desc[group].bg_flags &
+                            EXT2_BG_BLOCK_UNINIT))
+                               skip_group++;
                }
        }
-       fixit = end_problem_latch(ctx,  PR_LATCH_BBITMAP);
+       if (pctx.blk != NO_BLK)
+               print_bitmap_problem(ctx, save_problem, &pctx);
+       if (had_problem)
+               fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP);
+       else
+               fixit = -1;
+       ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
+       
        if (fixit == 1) {
                ext2fs_free_block_bitmap(fs->block_map);
                retval = ext2fs_copy_bitmap(ctx->block_found_map,
                                                  &fs->block_map);
-               /* XXX check retval --- should never fail! */
+               if (retval) {
+                       clear_problem_context(&pctx);
+                       fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx);
+                       ctx->flags |= E2F_FLAG_ABORT;
+                       return;
+               }
                ext2fs_set_bitmap_padding(fs->block_map);
                ext2fs_mark_bb_dirty(fs);
                
@@ -184,31 +303,33 @@ redo_counts:
                } else
                        ext2fs_unmark_valid(fs);
        }
-       free(free_array);
+       ext2fs_free_mem(&free_array);
 }
                        
 static void check_inode_bitmaps(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       ino_t   i;
-       int     free_inodes = 0;
-       int     group_free = 0;
-       int     dirs_count = 0;
-       int     group = 0;
-       int     inodes = 0;
-       int     *free_array;
-       int     *dir_array;
-       int     actual, bitmap;
+       ext2_ino_t      i;
+       unsigned int    free_inodes = 0;
+       int             group_free = 0;
+       int             dirs_count = 0;
+       int             group = 0;
+       unsigned int    inodes = 0;
+       int             *free_array;
+       int             *dir_array;
+       int             actual, bitmap;
        errcode_t       retval;
        struct problem_context  pctx;
-       int     problem, fixit;
+       int             problem, save_problem, fixit, had_problem;
+       int             lazy_bg = 0;
+       int             skip_group = 0;
        
        clear_problem_context(&pctx);
-       free_array = allocate_memory(fs->group_desc_count * sizeof(int),
-                                    "free inode count array");
+       free_array = (int *) e2fsck_allocate_memory(ctx,
+           fs->group_desc_count * sizeof(int), "free inode count array");
                                     
-       dir_array = allocate_memory(fs->group_desc_count * sizeof(int),
-                                   "directory count array");
+       dir_array = (int *) e2fsck_allocate_memory(ctx,
+          fs->group_desc_count * sizeof(int), "directory count array");
                                     
        if ((1 < ext2fs_get_inode_bitmap_start(ctx->inode_used_map)) ||
            (fs->super->s_inodes_count > 
@@ -219,8 +340,9 @@ static void check_inode_bitmaps(e2fsck_t ctx)
                pctx.ino = ext2fs_get_inode_bitmap_start(ctx->inode_used_map);
                pctx.ino2 = ext2fs_get_inode_bitmap_end(ctx->inode_used_map);
                fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
-               /* fatal */
-               fatal_error(0);
+
+               ctx->flags |= E2F_FLAG_ABORT; /* fatal */
+               return;
        }
        if ((1 < ext2fs_get_inode_bitmap_start(fs->inode_map)) ||
            (fs->super->s_inodes_count > 
@@ -231,15 +353,30 @@ static void check_inode_bitmaps(e2fsck_t ctx)
                pctx.ino = ext2fs_get_inode_bitmap_start(fs->inode_map);
                pctx.ino2 = ext2fs_get_inode_bitmap_end(fs->inode_map);
                fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
-               /* fatal */
-               fatal_error(0);
+
+               ctx->flags |= E2F_FLAG_ABORT; /* fatal */
+               return;
        }
 
+       if (EXT2_HAS_COMPAT_FEATURE(fs->super, 
+                                   EXT2_FEATURE_COMPAT_LAZY_BG))
+               lazy_bg++;
+
 redo_counts:
-       for (i = 1; i <= fs->super->s_inodes_count; i++) {
+       had_problem = 0;
+       save_problem = 0;
+       pctx.ino = pctx.ino2 = 0;
+       if (lazy_bg && (fs->group_desc[group].bg_flags &
+                       EXT2_BG_INODE_UNINIT))
+               skip_group++;
+
+       /* Protect loop from wrap-around if inodes_count is maxed */
+       for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
                actual = ext2fs_fast_test_inode_bitmap(ctx->inode_used_map, i);
-               bitmap = ext2fs_fast_test_inode_bitmap(fs->inode_map, i);
-               
+               if (skip_group) 
+                       bitmap = 0;
+               else
+                       bitmap = ext2fs_fast_test_inode_bitmap(fs->inode_map, i);
                if (actual == bitmap)
                        goto do_counts;
                
@@ -247,23 +384,36 @@ redo_counts:
                        /*
                         * Inode wasn't used, but marked in bitmap
                         */
-                       problem = PR_5_UNUSED_INODE;
+                       problem = PR_5_INODE_UNUSED;
                } else /* if (actual && !bitmap) */ {
                        /*
                         * Inode used, but not in bitmap
                         */
                        problem = PR_5_INODE_USED;
                }
-               pctx.ino = i;
-               fix_problem(ctx, problem, &pctx);
+               if (pctx.ino == 0) {
+                       pctx.ino = pctx.ino2 = i;
+                       save_problem = problem;
+               } else {
+                       if ((problem == save_problem) &&
+                           (pctx.ino2 == i-1))
+                               pctx.ino2++;
+                       else {
+                               print_bitmap_problem(ctx, save_problem, &pctx);
+                               pctx.ino = pctx.ino2 = i;
+                               save_problem = problem;
+                       }
+               }
+               ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
+               had_problem++;
                
 do_counts:
-               if (!bitmap) {
-                       group_free++;
-                       free_inodes++;
-               } else {
+               if (bitmap) {
                        if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, i))
                                dirs_count++;
+               } else if (!skip_group) {
+                       group_free++;
+                       free_inodes++;
                }
                inodes++;
                if ((inodes == fs->super->s_inodes_per_group) ||
@@ -272,16 +422,40 @@ do_counts:
                        dir_array[group] = dirs_count;
                        group ++;
                        inodes = 0;
+                       skip_group = 0;
                        group_free = 0;
                        dirs_count = 0;
+                       if (ctx->progress)
+                               if ((ctx->progress)(ctx, 5,
+                                           group + fs->group_desc_count,
+                                           fs->group_desc_count*2))
+                                       return;
+                       if (lazy_bg &&
+                           (i != fs->super->s_inodes_count) &&
+                           (fs->group_desc[group].bg_flags &
+                            EXT2_BG_INODE_UNINIT))
+                               skip_group++;
                }
        }
-       fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
+       if (pctx.ino)
+               print_bitmap_problem(ctx, save_problem, &pctx);
+       
+       if (had_problem)
+               fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
+       else
+               fixit = -1;
+       ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
+       
        if (fixit == 1) {
                ext2fs_free_inode_bitmap(fs->inode_map);
                retval = ext2fs_copy_bitmap(ctx->inode_used_map,
                                                  &fs->inode_map);
-               /* XXX check retval --- should never fail! */
+               if (retval) {
+                       clear_problem_context(&pctx);
+                       fix_problem(ctx, PR_5_COPY_IBITMAP_ERROR, &pctx);
+                       ctx->flags |= E2F_FLAG_ABORT;
+                       return;
+               }
                ext2fs_set_bitmap_padding(fs->inode_map);
                ext2fs_mark_ib_dirty(fs);
 
@@ -332,14 +506,14 @@ do_counts:
                } else
                        ext2fs_unmark_valid(fs);
        }
-       free(free_array);
-       free(dir_array);
+       ext2fs_free_mem(&free_array);
+       ext2fs_free_mem(&dir_array);
 }
 
 static void check_inode_end(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       ino_t   end, save_inodes_count, i;
+       ext2_ino_t      end, save_inodes_count, i;
        struct problem_context  pctx;
 
        clear_problem_context(&pctx);
@@ -350,12 +524,14 @@ static void check_inode_end(e2fsck_t ctx)
        if (pctx.errcode) {
                pctx.num = 1;
                fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
-               fatal_error(0);
+               ctx->flags |= E2F_FLAG_ABORT; /* fatal */
+               return;
        }
        if (save_inodes_count == end)
                return;
-       
-       for (i = save_inodes_count + 1; i <= end; i++) {
+
+       /* protect loop from wrap-around if end is maxed */     
+       for (i = save_inodes_count + 1; i <= end && i > save_inodes_count; i++) {
                if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) {
                        if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) {
                                for (i = save_inodes_count + 1; i <= end; i++)
@@ -373,7 +549,8 @@ static void check_inode_end(e2fsck_t ctx)
        if (pctx.errcode) {
                pctx.num = 2;
                fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
-               fatal_error(0);
+               ctx->flags |= E2F_FLAG_ABORT; /* fatal */
+               return;
        }
 }
 
@@ -392,15 +569,17 @@ static void check_block_end(e2fsck_t ctx)
        if (pctx.errcode) {
                pctx.num = 3;
                fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
-               fatal_error(0);
+               ctx->flags |= E2F_FLAG_ABORT; /* fatal */
+               return;
        }
        if (save_blocks_count == end)
                return;
-       
-       for (i = save_blocks_count + 1; i <= end; i++) {
+
+       /* Protect loop from wrap-around if end is maxed */     
+       for (i = save_blocks_count + 1; i <= end && i > save_blocks_count; i++) {
                if (!ext2fs_test_block_bitmap(fs->block_map, i)) {
                        if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) {
-                               for (i = save_blocks_count + 1; i < end; i++)
+                               for (i = save_blocks_count + 1; i <= end; i++)
                                        ext2fs_mark_block_bitmap(fs->block_map,
                                                                 i);
                                ext2fs_mark_bb_dirty(fs);
@@ -415,7 +594,8 @@ static void check_block_end(e2fsck_t ctx)
        if (pctx.errcode) {
                pctx.num = 4;
                fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
-               fatal_error(0);
+               ctx->flags |= E2F_FLAG_ABORT; /* fatal */
+               return;
        }
 }