2 * pass5.c --- check block and inode bitmaps against on-disk bitmaps
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
15 #include <sys/types.h>
17 #include <sys/ioctl.h>
24 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
26 static void check_block_bitmaps(e2fsck_t ctx);
27 static void check_inode_bitmaps(e2fsck_t ctx);
28 static void check_inode_end(e2fsck_t ctx);
29 static void check_block_end(e2fsck_t ctx);
30 static void check_inode_bitmap_checksum(e2fsck_t ctx);
31 static void check_block_bitmap_checksum(e2fsck_t ctx);
33 void e2fsck_pass5(e2fsck_t ctx)
36 struct resource_track rtrack;
38 struct problem_context pctx;
41 mtrace_print("Pass 5");
44 init_resource_track(&rtrack, ctx->fs->io);
45 clear_problem_context(&pctx);
47 if (!(ctx->options & E2F_OPT_PREEN))
48 fix_problem(ctx, PR_5_PASS_HEADER, &pctx);
51 if ((ctx->progress)(ctx, 5, 0, ctx->fs->group_desc_count*2))
54 e2fsck_read_bitmaps(ctx);
56 check_block_bitmaps(ctx);
57 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
59 check_inode_bitmaps(ctx);
60 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
63 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
66 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
69 check_inode_bitmap_checksum(ctx);
70 check_block_bitmap_checksum(ctx);
72 ext2fs_free_inode_bitmap(ctx->inode_used_map);
73 ctx->inode_used_map = 0;
74 ext2fs_free_inode_bitmap(ctx->inode_dir_map);
75 ctx->inode_dir_map = 0;
76 ext2fs_free_block_bitmap(ctx->block_found_map);
77 ctx->block_found_map = 0;
79 print_resource_track(ctx, _("Pass 5"), &rtrack, ctx->fs->io);
82 static void check_inode_bitmap_checksum(e2fsck_t ctx)
84 struct problem_context pctx;
91 if (!EXT2_HAS_RO_COMPAT_FEATURE(ctx->fs->super,
92 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
95 /* If bitmap is dirty from being fixed, checksum will be corrected */
96 if (ext2fs_test_ib_dirty(ctx->fs))
99 nbytes = (size_t)(EXT2_INODES_PER_GROUP(ctx->fs->super) / 8);
100 retval = ext2fs_get_memalign(ctx->fs->blocksize, ctx->fs->blocksize,
103 com_err(ctx->program_name, 0,
104 _("check_inode_bitmap_checksum: Memory allocation error"));
108 clear_problem_context(&pctx);
109 for (i = 0; i < ctx->fs->group_desc_count; i++) {
110 if (ext2fs_bg_flags_test(ctx->fs, i, EXT2_BG_INODE_UNINIT))
113 ino_itr = 1 + (i * (nbytes << 3));
114 retval = ext2fs_get_inode_bitmap_range2(ctx->fs->inode_map,
115 ino_itr, nbytes << 3,
120 if (ext2fs_inode_bitmap_csum_verify(ctx->fs, i, buf, nbytes))
123 if (!fix_problem(ctx, PR_5_INODE_BITMAP_CSUM_INVALID, &pctx))
127 * Fixing one checksum will rewrite all of them. The bitmap
128 * will be checked against the one we made during pass1 for
129 * discrepancies, and fixed if need be.
131 ext2fs_mark_ib_dirty(ctx->fs);
135 ext2fs_free_mem(&buf);
138 static void check_block_bitmap_checksum(e2fsck_t ctx)
140 struct problem_context pctx;
147 if (!EXT2_HAS_RO_COMPAT_FEATURE(ctx->fs->super,
148 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
151 /* If bitmap is dirty from being fixed, checksum will be corrected */
152 if (ext2fs_test_bb_dirty(ctx->fs))
155 nbytes = (size_t)(EXT2_CLUSTERS_PER_GROUP(ctx->fs->super) / 8);
156 retval = ext2fs_get_memalign(ctx->fs->blocksize, ctx->fs->blocksize,
159 com_err(ctx->program_name, 0,
160 _("check_block_bitmap_checksum: Memory allocation error"));
164 clear_problem_context(&pctx);
165 for (i = 0; i < ctx->fs->group_desc_count; i++) {
166 if (ext2fs_bg_flags_test(ctx->fs, i, EXT2_BG_BLOCK_UNINIT))
169 blk_itr = EXT2FS_B2C(ctx->fs,
170 ctx->fs->super->s_first_data_block) +
171 ((blk64_t) i * (nbytes << 3));
172 retval = ext2fs_get_block_bitmap_range2(ctx->fs->block_map,
173 blk_itr, nbytes << 3,
178 if (ext2fs_block_bitmap_csum_verify(ctx->fs, i, buf, nbytes))
181 if (!fix_problem(ctx, PR_5_BLOCK_BITMAP_CSUM_INVALID, &pctx))
185 * Fixing one checksum will rewrite all of them. The bitmap
186 * will be checked against the one we made during pass1 for
187 * discrepancies, and fixed if need be.
189 ext2fs_mark_bb_dirty(ctx->fs);
193 ext2fs_free_mem(&buf);
196 static void e2fsck_discard_blocks(e2fsck_t ctx, blk64_t start,
199 ext2_filsys fs = ctx->fs;
202 * If the filesystem has changed it means that there was an corruption
203 * which should be repaired, but in some cases just one e2fsck run is
204 * not enough to fix the problem, hence it is not safe to run discard
207 if (ext2fs_test_changed(fs))
208 ctx->options &= ~E2F_OPT_DISCARD;
210 if ((ctx->options & E2F_OPT_DISCARD) &&
211 (io_channel_discard(fs->io, start, count)))
212 ctx->options &= ~E2F_OPT_DISCARD;
216 * This will try to discard number 'count' inodes starting at
217 * inode number 'start' within the 'group'. Note that 'start'
218 * is 1-based, it means that we need to adjust it by -1 in this
219 * function to compute right offset in the particular inode table.
221 static void e2fsck_discard_inodes(e2fsck_t ctx, dgrp_t group,
222 ext2_ino_t start, int count)
224 ext2_filsys fs = ctx->fs;
228 * Sanity check for 'start'
230 if ((start < 1) || (start > EXT2_INODES_PER_GROUP(fs->super))) {
231 printf("PROGRAMMING ERROR: Got start %d outside of group %d!"
232 " Disabling discard\n",
234 ctx->options &= ~E2F_OPT_DISCARD;
238 * Do not attempt to discard if E2F_OPT_DISCARD is not set. And also
239 * skip the discard on this group if discard does not zero data.
240 * The reason is that if the inode table is not zeroed discard would
241 * no help us since we need to zero it anyway, or if the inode table
242 * is zeroed then the read after discard would not be deterministic
243 * anyway and we would not be able to assume that this inode table
244 * was zeroed anymore so we would have to zero it again, which does
245 * not really make sense.
247 if (!(ctx->options & E2F_OPT_DISCARD) ||
248 !io_channel_discard_zeroes_data(fs->io))
252 * Start is inode number within the group which starts
253 * counting from 1, so we need to adjust it.
258 * We can discard only blocks containing only unused
259 * inodes in the table.
261 blk = DIV_ROUND_UP(start,
262 EXT2_INODES_PER_BLOCK(fs->super));
263 count -= (blk * EXT2_INODES_PER_BLOCK(fs->super) - start);
264 blk += ext2fs_inode_table_loc(fs, group);
265 num = count / EXT2_INODES_PER_BLOCK(fs->super);
268 e2fsck_discard_blocks(ctx, blk, num);
271 #define NO_BLK ((blk64_t) -1)
273 static void print_bitmap_problem(e2fsck_t ctx, problem_t problem,
274 struct problem_context *pctx)
277 case PR_5_BLOCK_UNUSED:
278 if (pctx->blk == pctx->blk2)
281 problem = PR_5_BLOCK_RANGE_UNUSED;
283 case PR_5_BLOCK_USED:
284 if (pctx->blk == pctx->blk2)
287 problem = PR_5_BLOCK_RANGE_USED;
289 case PR_5_INODE_UNUSED:
290 if (pctx->ino == pctx->ino2)
293 problem = PR_5_INODE_RANGE_UNUSED;
295 case PR_5_INODE_USED:
296 if (pctx->ino == pctx->ino2)
299 problem = PR_5_INODE_RANGE_USED;
302 fix_problem(ctx, problem, pctx);
303 pctx->blk = pctx->blk2 = NO_BLK;
304 pctx->ino = pctx->ino2 = 0;
307 /* Just to be more succint */
308 #define B2C(x) EXT2FS_B2C(fs, (x))
309 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
310 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
311 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
313 static void check_block_bitmaps(e2fsck_t ctx)
315 ext2_filsys fs = ctx->fs;
317 unsigned int *free_array;
319 unsigned int blocks = 0;
320 blk64_t free_blocks = 0;
321 blk64_t first_free = ext2fs_blocks_count(fs->super);
322 unsigned int group_free = 0;
324 struct problem_context pctx;
325 problem_t problem, save_problem;
326 int fixit, had_problem;
330 int old_desc_blocks = 0;
334 blk64_t super_blk, old_desc_blk, new_desc_blk;
335 char *actual_buf, *bitmap_buf;
337 actual_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
338 "actual bitmap buffer");
339 bitmap_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
340 "bitmap block buffer");
342 clear_problem_context(&pctx);
343 free_array = (unsigned int *) e2fsck_allocate_memory(ctx,
344 fs->group_desc_count * sizeof(unsigned int), "free block count array");
346 if ((B2C(fs->super->s_first_data_block) <
347 ext2fs_get_block_bitmap_start2(ctx->block_found_map)) ||
348 (B2C(ext2fs_blocks_count(fs->super)-1) >
349 ext2fs_get_block_bitmap_end2(ctx->block_found_map))) {
351 pctx.blk = B2C(fs->super->s_first_data_block);
352 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
353 pctx.ino = ext2fs_get_block_bitmap_start2(ctx->block_found_map);
354 pctx.ino2 = ext2fs_get_block_bitmap_end2(ctx->block_found_map);
355 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
357 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
361 if ((B2C(fs->super->s_first_data_block) <
362 ext2fs_get_block_bitmap_start2(fs->block_map)) ||
363 (B2C(ext2fs_blocks_count(fs->super)-1) >
364 ext2fs_get_block_bitmap_end2(fs->block_map))) {
366 pctx.blk = B2C(fs->super->s_first_data_block);
367 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
368 pctx.ino = ext2fs_get_block_bitmap_start2(fs->block_map);
369 pctx.ino2 = ext2fs_get_block_bitmap_end2(fs->block_map);
370 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
372 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
376 csum_flag = ext2fs_has_group_desc_csum(fs);
380 pctx.blk = pctx.blk2 = NO_BLK;
382 (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
384 for (i = B2C(fs->super->s_first_data_block);
385 i < ext2fs_blocks_count(fs->super);
386 i += EXT2FS_CLUSTER_RATIO(fs)) {
387 int first_block_in_bg = (B2C(i) -
388 B2C(fs->super->s_first_data_block)) %
389 fs->super->s_clusters_per_group == 0;
390 int n, nbytes = fs->super->s_clusters_per_group / 8;
392 actual = ext2fs_fast_test_block_bitmap2(ctx->block_found_map, i);
395 * Try to optimize pass5 by extracting a bitmap block
396 * as expected from what we have on disk, and then
397 * comparing the two. If they are identical, then
398 * update the free block counts and go on to the next
399 * block group. This is much faster than doing the
400 * individual bit-by-bit comparison. The one downside
401 * is that this doesn't work if we are asking e2fsck
402 * to do a discard operation.
404 if (!first_block_in_bg ||
405 (group == (int)fs->group_desc_count - 1) ||
406 (ctx->options & E2F_OPT_DISCARD))
409 retval = ext2fs_get_block_bitmap_range2(ctx->block_found_map,
410 B2C(i), fs->super->s_clusters_per_group,
414 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT))
415 memset(bitmap_buf, 0, nbytes);
417 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
418 B2C(i), fs->super->s_clusters_per_group,
423 if (memcmp(actual_buf, bitmap_buf, nbytes) != 0)
425 n = ext2fs_bitcount(actual_buf, nbytes);
426 group_free = fs->super->s_clusters_per_group - n;
427 free_blocks += group_free;
428 i += EXT2FS_C2B(fs, fs->super->s_clusters_per_group - 1);
433 if (first_block_in_bg) {
437 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
438 &old_desc_blk, &new_desc_blk, 0);
440 if (fs->super->s_feature_incompat &
441 EXT2_FEATURE_INCOMPAT_META_BG)
443 fs->super->s_first_meta_bg;
445 old_desc_blocks = fs->desc_blocks +
446 fs->super->s_reserved_gdt_blocks;
449 cmp_block = fs->super->s_clusters_per_group;
450 if (group == (int)fs->group_desc_count - 1)
451 cmp_block = EXT2FS_NUM_B2C(fs,
452 ext2fs_group_blocks_count(fs, group));
456 if (EQ_CLSTR(i, super_blk) ||
457 (old_desc_blk && old_desc_blocks &&
458 GE_CLSTR(i, old_desc_blk) &&
459 LE_CLSTR(i, old_desc_blk + old_desc_blocks-1)) ||
460 (new_desc_blk && EQ_CLSTR(i, new_desc_blk)) ||
461 EQ_CLSTR(i, ext2fs_block_bitmap_loc(fs, group)) ||
462 EQ_CLSTR(i, ext2fs_inode_bitmap_loc(fs, group)) ||
463 (GE_CLSTR(i, ext2fs_inode_table_loc(fs, group)) &&
464 LE_CLSTR(i, (ext2fs_inode_table_loc(fs, group) +
465 fs->inode_blocks_per_group - 1)))) {
467 actual = (actual != 0);
470 } else if ((EXT2FS_B2C(fs, i) - count -
471 EXT2FS_B2C(fs, fs->super->s_first_data_block)) %
472 fs->super->s_clusters_per_group == 0) {
474 * When the compare data blocks in block bitmap
475 * are 0, count the free block,
476 * skip the current block group.
478 if (ext2fs_test_block_bitmap_range2(
479 ctx->block_found_map,
483 * -1 means to skip the current block
486 blocks = fs->super->s_clusters_per_group - 1;
487 group_free = cmp_block;
488 free_blocks += cmp_block;
490 * The current block group's last block
493 i += EXT2FS_C2B(fs, cmp_block - 1);
498 } else if (redo_flag)
501 bitmap = ext2fs_fast_test_block_bitmap2(fs->block_map, i);
503 if (!actual == !bitmap)
506 if (!actual && bitmap) {
508 * Block not used, but marked in use in the bitmap.
510 problem = PR_5_BLOCK_UNUSED;
513 * Block used, but not marked in use in the bitmap.
515 problem = PR_5_BLOCK_USED;
518 struct problem_context pctx2;
521 if (fix_problem(ctx, PR_5_BLOCK_UNINIT,&pctx2)){
522 ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
527 if (pctx.blk == NO_BLK) {
528 pctx.blk = pctx.blk2 = i;
529 save_problem = problem;
531 if ((problem == save_problem) &&
535 print_bitmap_problem(ctx, save_problem, &pctx);
536 pctx.blk = pctx.blk2 = i;
537 save_problem = problem;
540 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
544 * If there a problem we should turn off the discard so we
545 * do not compromise the filesystem.
547 ctx->options &= ~E2F_OPT_DISCARD;
555 } else if (i > first_free) {
556 e2fsck_discard_blocks(ctx, first_free,
558 first_free = ext2fs_blocks_count(fs->super);
561 if ((blocks == fs->super->s_clusters_per_group) ||
562 (EXT2FS_B2C(fs, i) ==
563 EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1))) {
565 * If the last block of this group is free, then we can
566 * discard it as well.
568 if (!bitmap && i >= first_free)
569 e2fsck_discard_blocks(ctx, first_free,
570 (i - first_free) + 1);
572 first_free = ext2fs_blocks_count(fs->super);
574 free_array[group] = group_free;
580 if ((ctx->progress)(ctx, 5, group,
581 fs->group_desc_count*2))
584 (i != ext2fs_blocks_count(fs->super)-1) &&
585 ext2fs_bg_flags_test(fs, group,
586 EXT2_BG_BLOCK_UNINIT))
590 if (pctx.blk != NO_BLK)
591 print_bitmap_problem(ctx, save_problem, &pctx);
593 fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP);
596 ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
599 ext2fs_free_block_bitmap(fs->block_map);
600 retval = ext2fs_copy_bitmap(ctx->block_found_map,
603 clear_problem_context(&pctx);
604 fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx);
605 ctx->flags |= E2F_FLAG_ABORT;
608 ext2fs_set_bitmap_padding(fs->block_map);
609 ext2fs_mark_bb_dirty(fs);
611 /* Redo the counts */
612 blocks = 0; free_blocks = 0; group_free = 0; group = 0;
613 memset(free_array, 0, fs->group_desc_count * sizeof(int));
616 } else if (fixit == 0)
617 ext2fs_unmark_valid(fs);
619 for (g = 0; g < fs->group_desc_count; g++) {
620 if (free_array[g] != ext2fs_bg_free_blocks_count(fs, g)) {
622 pctx.blk = ext2fs_bg_free_blocks_count(fs, g);
623 pctx.blk2 = free_array[g];
625 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP,
627 ext2fs_bg_free_blocks_count_set(fs, g, free_array[g]);
628 ext2fs_mark_super_dirty(fs);
630 ext2fs_unmark_valid(fs);
633 free_blocks = EXT2FS_C2B(fs, free_blocks);
634 if (free_blocks != ext2fs_free_blocks_count(fs->super)) {
636 pctx.blk = ext2fs_free_blocks_count(fs->super);
637 pctx.blk2 = free_blocks;
639 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) {
640 ext2fs_free_blocks_count_set(fs->super, free_blocks);
641 ext2fs_mark_super_dirty(fs);
645 ext2fs_free_mem(&free_array);
646 ext2fs_free_mem(&actual_buf);
647 ext2fs_free_mem(&bitmap_buf);
650 static void check_inode_bitmaps(e2fsck_t ctx)
652 ext2_filsys fs = ctx->fs;
654 unsigned int free_inodes = 0;
658 unsigned int inodes = 0;
659 ext2_ino_t *free_array;
660 ext2_ino_t *dir_array;
663 struct problem_context pctx;
664 problem_t problem, save_problem;
665 int fixit, had_problem;
669 ext2_ino_t first_free = fs->super->s_inodes_per_group + 1;
671 clear_problem_context(&pctx);
672 free_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
673 fs->group_desc_count * sizeof(ext2_ino_t), "free inode count array");
675 dir_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
676 fs->group_desc_count * sizeof(ext2_ino_t), "directory count array");
678 if ((1 < ext2fs_get_inode_bitmap_start2(ctx->inode_used_map)) ||
679 (fs->super->s_inodes_count >
680 ext2fs_get_inode_bitmap_end2(ctx->inode_used_map))) {
683 pctx.blk2 = fs->super->s_inodes_count;
684 pctx.ino = ext2fs_get_inode_bitmap_start2(ctx->inode_used_map);
685 pctx.ino2 = ext2fs_get_inode_bitmap_end2(ctx->inode_used_map);
686 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
688 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
691 if ((1 < ext2fs_get_inode_bitmap_start2(fs->inode_map)) ||
692 (fs->super->s_inodes_count >
693 ext2fs_get_inode_bitmap_end2(fs->inode_map))) {
696 pctx.blk2 = fs->super->s_inodes_count;
697 pctx.ino = ext2fs_get_inode_bitmap_start2(fs->inode_map);
698 pctx.ino2 = ext2fs_get_inode_bitmap_end2(fs->inode_map);
699 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
701 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
705 csum_flag = ext2fs_has_group_desc_csum(fs);
709 pctx.ino = pctx.ino2 = 0;
711 (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
714 /* Protect loop from wrap-around if inodes_count is maxed */
715 for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
718 i % fs->super->s_inodes_per_group == 1) {
720 * Current inode is the first inode
721 * in the current block group.
723 if (ext2fs_test_inode_bitmap_range(
724 ctx->inode_used_map, i,
725 fs->super->s_inodes_per_group)) {
727 * When the compared inodes in inodes bitmap
728 * are 0, count the free inode,
729 * skip the current block group.
732 inodes = fs->super->s_inodes_per_group - 1;
734 free_inodes += inodes;
741 actual = ext2fs_fast_test_inode_bitmap2(ctx->inode_used_map, i);
744 else if (!skip_group)
745 bitmap = ext2fs_fast_test_inode_bitmap2(fs->inode_map, i);
746 if (!actual == !bitmap)
749 if (!actual && bitmap) {
751 * Inode wasn't used, but marked in bitmap
753 problem = PR_5_INODE_UNUSED;
754 } else /* if (actual && !bitmap) */ {
756 * Inode used, but not in bitmap
758 problem = PR_5_INODE_USED;
760 /* We should never hit this, because it means that
761 * inodes were marked in use that weren't noticed
762 * in pass1 or pass 2. It is easier to fix the problem
763 * than to kill e2fsck and leave the user stuck. */
765 struct problem_context pctx2;
768 if (fix_problem(ctx, PR_5_INODE_UNINIT,&pctx2)){
769 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
775 pctx.ino = pctx.ino2 = i;
776 save_problem = problem;
778 if ((problem == save_problem) &&
782 print_bitmap_problem(ctx, save_problem, &pctx);
783 pctx.ino = pctx.ino2 = i;
784 save_problem = problem;
787 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
790 * If there a problem we should turn off the discard so we
791 * do not compromise the filesystem.
793 ctx->options &= ~E2F_OPT_DISCARD;
798 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i))
800 if (inodes > first_free) {
801 e2fsck_discard_inodes(ctx, group, first_free,
802 inodes - first_free);
803 first_free = fs->super->s_inodes_per_group + 1;
808 if (first_free > inodes)
812 if ((inodes == fs->super->s_inodes_per_group) ||
813 (i == fs->super->s_inodes_count)) {
815 * If the last inode is free, we can discard it as well.
817 if (!bitmap && inodes >= first_free)
818 e2fsck_discard_inodes(ctx, group, first_free,
819 inodes - first_free + 1);
821 * If discard zeroes data and the group inode table
822 * was not zeroed yet, set itable as zeroed
824 if ((ctx->options & E2F_OPT_DISCARD) &&
825 io_channel_discard_zeroes_data(fs->io) &&
826 !(ext2fs_bg_flags_test(fs, group,
827 EXT2_BG_INODE_ZEROED))) {
828 ext2fs_bg_flags_set(fs, group,
829 EXT2_BG_INODE_ZEROED);
830 ext2fs_group_desc_csum_set(fs, group);
833 first_free = fs->super->s_inodes_per_group + 1;
834 free_array[group] = group_free;
835 dir_array[group] = dirs_count;
842 if ((ctx->progress)(ctx, 5,
843 group + fs->group_desc_count,
844 fs->group_desc_count*2))
847 (i != fs->super->s_inodes_count) &&
848 (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)
854 print_bitmap_problem(ctx, save_problem, &pctx);
857 fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
860 ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
863 ext2fs_free_inode_bitmap(fs->inode_map);
864 retval = ext2fs_copy_bitmap(ctx->inode_used_map,
867 clear_problem_context(&pctx);
868 fix_problem(ctx, PR_5_COPY_IBITMAP_ERROR, &pctx);
869 ctx->flags |= E2F_FLAG_ABORT;
872 ext2fs_set_bitmap_padding(fs->inode_map);
873 ext2fs_mark_ib_dirty(fs);
876 inodes = 0; free_inodes = 0; group_free = 0;
877 dirs_count = 0; group = 0;
878 memset(free_array, 0, fs->group_desc_count * sizeof(int));
879 memset(dir_array, 0, fs->group_desc_count * sizeof(int));
882 } else if (fixit == 0)
883 ext2fs_unmark_valid(fs);
885 for (i = 0; i < fs->group_desc_count; i++) {
886 if (free_array[i] != ext2fs_bg_free_inodes_count(fs, i)) {
888 pctx.ino = ext2fs_bg_free_inodes_count(fs, i);
889 pctx.ino2 = free_array[i];
890 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP,
892 ext2fs_bg_free_inodes_count_set(fs, i, free_array[i]);
893 ext2fs_mark_super_dirty(fs);
895 ext2fs_unmark_valid(fs);
897 if (dir_array[i] != ext2fs_bg_used_dirs_count(fs, i)) {
899 pctx.ino = ext2fs_bg_used_dirs_count(fs, i);
900 pctx.ino2 = dir_array[i];
902 if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP,
904 ext2fs_bg_used_dirs_count_set(fs, i, dir_array[i]);
905 ext2fs_mark_super_dirty(fs);
907 ext2fs_unmark_valid(fs);
910 if (free_inodes != fs->super->s_free_inodes_count) {
912 pctx.ino = fs->super->s_free_inodes_count;
913 pctx.ino2 = free_inodes;
915 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT, &pctx)) {
916 fs->super->s_free_inodes_count = free_inodes;
917 ext2fs_mark_super_dirty(fs);
921 ext2fs_free_mem(&free_array);
922 ext2fs_free_mem(&dir_array);
925 static void check_inode_end(e2fsck_t ctx)
927 ext2_filsys fs = ctx->fs;
928 ext2_ino_t end, save_inodes_count, i;
929 struct problem_context pctx;
931 clear_problem_context(&pctx);
933 end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
934 pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, end,
938 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
939 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
942 if (save_inodes_count == end)
945 /* protect loop from wrap-around if end is maxed */
946 for (i = save_inodes_count + 1; i <= end && i > save_inodes_count; i++) {
947 if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) {
948 if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) {
949 for (; i <= end; i++)
950 ext2fs_mark_inode_bitmap(fs->inode_map,
952 ext2fs_mark_ib_dirty(fs);
954 ext2fs_unmark_valid(fs);
959 pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map,
960 save_inodes_count, 0);
963 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
964 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
969 static void check_block_end(e2fsck_t ctx)
971 ext2_filsys fs = ctx->fs;
972 blk64_t end, save_blocks_count, i;
973 struct problem_context pctx;
975 clear_problem_context(&pctx);
977 end = ext2fs_get_block_bitmap_start2(fs->block_map) +
978 ((blk64_t)EXT2_CLUSTERS_PER_GROUP(fs->super) * fs->group_desc_count) - 1;
979 pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, end,
983 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
984 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
987 if (save_blocks_count == end)
990 /* Protect loop from wrap-around if end is maxed */
991 for (i = save_blocks_count + 1; i <= end && i > save_blocks_count; i++) {
992 if (!ext2fs_test_block_bitmap2(fs->block_map,
993 EXT2FS_C2B(fs, i))) {
994 if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) {
995 for (; i <= end; i++)
996 ext2fs_mark_block_bitmap2(fs->block_map,
998 ext2fs_mark_bb_dirty(fs);
1000 ext2fs_unmark_valid(fs);
1005 pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map,
1006 save_blocks_count, 0);
1009 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
1010 ctx->flags |= E2F_FLAG_ABORT; /* fatal */