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;
78 ext2fs_free_block_bitmap(ctx->block_metadata_map);
79 ctx->block_metadata_map = 0;
81 print_resource_track(ctx, _("Pass 5"), &rtrack, ctx->fs->io);
84 static void check_inode_bitmap_checksum(e2fsck_t ctx)
86 struct problem_context pctx;
93 if (!EXT2_HAS_RO_COMPAT_FEATURE(ctx->fs->super,
94 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
97 /* If bitmap is dirty from being fixed, checksum will be corrected */
98 if (ext2fs_test_ib_dirty(ctx->fs))
101 nbytes = (size_t)(EXT2_INODES_PER_GROUP(ctx->fs->super) / 8);
102 retval = ext2fs_get_memalign(ctx->fs->blocksize, ctx->fs->blocksize,
105 com_err(ctx->program_name, 0, "%s",
106 _("check_inode_bitmap_checksum: Memory allocation error"));
110 clear_problem_context(&pctx);
111 for (i = 0; i < ctx->fs->group_desc_count; i++) {
112 if (ext2fs_bg_flags_test(ctx->fs, i, EXT2_BG_INODE_UNINIT))
115 ino_itr = 1 + (i * (nbytes << 3));
116 retval = ext2fs_get_inode_bitmap_range2(ctx->fs->inode_map,
117 ino_itr, nbytes << 3,
122 if (ext2fs_inode_bitmap_csum_verify(ctx->fs, i, buf, nbytes))
125 if (!fix_problem(ctx, PR_5_INODE_BITMAP_CSUM_INVALID, &pctx))
129 * Fixing one checksum will rewrite all of them. The bitmap
130 * will be checked against the one we made during pass1 for
131 * discrepancies, and fixed if need be.
133 ext2fs_mark_ib_dirty(ctx->fs);
137 ext2fs_free_mem(&buf);
140 static void check_block_bitmap_checksum(e2fsck_t ctx)
142 struct problem_context pctx;
149 if (!EXT2_HAS_RO_COMPAT_FEATURE(ctx->fs->super,
150 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
153 /* If bitmap is dirty from being fixed, checksum will be corrected */
154 if (ext2fs_test_bb_dirty(ctx->fs))
157 nbytes = (size_t)(EXT2_CLUSTERS_PER_GROUP(ctx->fs->super) / 8);
158 retval = ext2fs_get_memalign(ctx->fs->blocksize, ctx->fs->blocksize,
161 com_err(ctx->program_name, 0, "%s",
162 _("check_block_bitmap_checksum: Memory allocation error"));
166 clear_problem_context(&pctx);
167 for (i = 0; i < ctx->fs->group_desc_count; i++) {
168 if (ext2fs_bg_flags_test(ctx->fs, i, EXT2_BG_BLOCK_UNINIT))
171 blk_itr = EXT2FS_B2C(ctx->fs,
172 ctx->fs->super->s_first_data_block) +
173 ((blk64_t) i * (nbytes << 3));
174 retval = ext2fs_get_block_bitmap_range2(ctx->fs->block_map,
175 blk_itr, nbytes << 3,
180 if (ext2fs_block_bitmap_csum_verify(ctx->fs, i, buf, nbytes))
183 if (!fix_problem(ctx, PR_5_BLOCK_BITMAP_CSUM_INVALID, &pctx))
187 * Fixing one checksum will rewrite all of them. The bitmap
188 * will be checked against the one we made during pass1 for
189 * discrepancies, and fixed if need be.
191 ext2fs_mark_bb_dirty(ctx->fs);
195 ext2fs_free_mem(&buf);
198 static void e2fsck_discard_blocks(e2fsck_t ctx, blk64_t start,
201 ext2_filsys fs = ctx->fs;
204 * If the filesystem has changed it means that there was an corruption
205 * which should be repaired, but in some cases just one e2fsck run is
206 * not enough to fix the problem, hence it is not safe to run discard
209 if (ext2fs_test_changed(fs))
210 ctx->options &= ~E2F_OPT_DISCARD;
212 if ((ctx->options & E2F_OPT_DISCARD) &&
213 (io_channel_discard(fs->io, start, count)))
214 ctx->options &= ~E2F_OPT_DISCARD;
218 * This will try to discard number 'count' inodes starting at
219 * inode number 'start' within the 'group'. Note that 'start'
220 * is 1-based, it means that we need to adjust it by -1 in this
221 * function to compute right offset in the particular inode table.
223 static void e2fsck_discard_inodes(e2fsck_t ctx, dgrp_t group,
224 ext2_ino_t start, int count)
226 ext2_filsys fs = ctx->fs;
230 * Sanity check for 'start'
232 if ((start < 1) || (start > EXT2_INODES_PER_GROUP(fs->super))) {
233 printf("PROGRAMMING ERROR: Got start %d outside of group %d!"
234 " Disabling discard\n",
236 ctx->options &= ~E2F_OPT_DISCARD;
240 * Do not attempt to discard if E2F_OPT_DISCARD is not set. And also
241 * skip the discard on this group if discard does not zero data.
242 * The reason is that if the inode table is not zeroed discard would
243 * no help us since we need to zero it anyway, or if the inode table
244 * is zeroed then the read after discard would not be deterministic
245 * anyway and we would not be able to assume that this inode table
246 * was zeroed anymore so we would have to zero it again, which does
247 * not really make sense.
249 if (!(ctx->options & E2F_OPT_DISCARD) ||
250 !io_channel_discard_zeroes_data(fs->io))
254 * Start is inode number within the group which starts
255 * counting from 1, so we need to adjust it.
260 * We can discard only blocks containing only unused
261 * inodes in the table.
263 blk = DIV_ROUND_UP(start,
264 EXT2_INODES_PER_BLOCK(fs->super));
265 count -= (blk * EXT2_INODES_PER_BLOCK(fs->super) - start);
266 blk += ext2fs_inode_table_loc(fs, group);
267 num = count / EXT2_INODES_PER_BLOCK(fs->super);
270 e2fsck_discard_blocks(ctx, blk, num);
273 #define NO_BLK ((blk64_t) -1)
275 static void print_bitmap_problem(e2fsck_t ctx, problem_t problem,
276 struct problem_context *pctx)
279 case PR_5_BLOCK_UNUSED:
280 if (pctx->blk == pctx->blk2)
283 problem = PR_5_BLOCK_RANGE_UNUSED;
285 case PR_5_BLOCK_USED:
286 if (pctx->blk == pctx->blk2)
289 problem = PR_5_BLOCK_RANGE_USED;
291 case PR_5_INODE_UNUSED:
292 if (pctx->ino == pctx->ino2)
295 problem = PR_5_INODE_RANGE_UNUSED;
297 case PR_5_INODE_USED:
298 if (pctx->ino == pctx->ino2)
301 problem = PR_5_INODE_RANGE_USED;
304 fix_problem(ctx, problem, pctx);
305 pctx->blk = pctx->blk2 = NO_BLK;
306 pctx->ino = pctx->ino2 = 0;
309 /* Just to be more succint */
310 #define B2C(x) EXT2FS_B2C(fs, (x))
311 #define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
312 #define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
313 #define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
315 static void check_block_bitmaps(e2fsck_t ctx)
317 ext2_filsys fs = ctx->fs;
319 unsigned int *free_array;
321 unsigned int blocks = 0;
322 blk64_t free_blocks = 0;
323 blk64_t first_free = ext2fs_blocks_count(fs->super);
324 unsigned int group_free = 0;
326 struct problem_context pctx;
327 problem_t problem, save_problem;
328 int fixit, had_problem;
331 char *actual_buf, *bitmap_buf;
333 actual_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
334 "actual bitmap buffer");
335 bitmap_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
336 "bitmap block buffer");
338 clear_problem_context(&pctx);
339 free_array = (unsigned int *) e2fsck_allocate_memory(ctx,
340 fs->group_desc_count * sizeof(unsigned int), "free block count array");
342 if ((B2C(fs->super->s_first_data_block) <
343 ext2fs_get_block_bitmap_start2(ctx->block_found_map)) ||
344 (B2C(ext2fs_blocks_count(fs->super)-1) >
345 ext2fs_get_block_bitmap_end2(ctx->block_found_map))) {
347 pctx.blk = B2C(fs->super->s_first_data_block);
348 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
349 pctx.ino = ext2fs_get_block_bitmap_start2(ctx->block_found_map);
350 pctx.ino2 = ext2fs_get_block_bitmap_end2(ctx->block_found_map);
351 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
353 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
357 if ((B2C(fs->super->s_first_data_block) <
358 ext2fs_get_block_bitmap_start2(fs->block_map)) ||
359 (B2C(ext2fs_blocks_count(fs->super)-1) >
360 ext2fs_get_block_bitmap_end2(fs->block_map))) {
362 pctx.blk = B2C(fs->super->s_first_data_block);
363 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
364 pctx.ino = ext2fs_get_block_bitmap_start2(fs->block_map);
365 pctx.ino2 = ext2fs_get_block_bitmap_end2(fs->block_map);
366 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
368 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
375 pctx.blk = pctx.blk2 = NO_BLK;
376 for (i = B2C(fs->super->s_first_data_block);
377 i < ext2fs_blocks_count(fs->super);
378 i += EXT2FS_CLUSTER_RATIO(fs)) {
379 int first_block_in_bg = (B2C(i) -
380 B2C(fs->super->s_first_data_block)) %
381 fs->super->s_clusters_per_group == 0;
382 int n, nbytes = fs->super->s_clusters_per_group / 8;
384 actual = ext2fs_fast_test_block_bitmap2(ctx->block_found_map, i);
387 * Try to optimize pass5 by extracting a bitmap block
388 * as expected from what we have on disk, and then
389 * comparing the two. If they are identical, then
390 * update the free block counts and go on to the next
391 * block group. This is much faster than doing the
392 * individual bit-by-bit comparison. The one downside
393 * is that this doesn't work if we are asking e2fsck
394 * to do a discard operation.
396 if (!first_block_in_bg ||
397 (group == (int)fs->group_desc_count - 1) ||
398 (ctx->options & E2F_OPT_DISCARD))
401 retval = ext2fs_get_block_bitmap_range2(ctx->block_found_map,
402 B2C(i), fs->super->s_clusters_per_group,
406 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
407 B2C(i), fs->super->s_clusters_per_group,
411 if (memcmp(actual_buf, bitmap_buf, nbytes) != 0)
413 n = ext2fs_bitcount(actual_buf, nbytes);
414 group_free = fs->super->s_clusters_per_group - n;
415 free_blocks += group_free;
416 i += EXT2FS_C2B(fs, fs->super->s_clusters_per_group - 1);
423 bitmap = ext2fs_fast_test_block_bitmap2(fs->block_map, i);
425 if (!actual == !bitmap)
428 if (!actual && bitmap) {
430 * Block not used, but marked in use in the bitmap.
432 problem = PR_5_BLOCK_UNUSED;
435 * Block used, but not marked in use in the bitmap.
437 problem = PR_5_BLOCK_USED;
439 if (ext2fs_bg_flags_test(fs, group,
440 EXT2_BG_BLOCK_UNINIT)) {
441 struct problem_context pctx2;
444 if (fix_problem(ctx, PR_5_BLOCK_UNINIT,
446 ext2fs_bg_flags_clear(fs, group,
447 EXT2_BG_BLOCK_UNINIT);
450 if (pctx.blk == NO_BLK) {
451 pctx.blk = pctx.blk2 = i;
452 save_problem = problem;
454 if ((problem == save_problem) &&
455 (pctx.blk2 == i - EXT2FS_CLUSTER_RATIO(fs)))
456 pctx.blk2 += EXT2FS_CLUSTER_RATIO(fs);
458 print_bitmap_problem(ctx, save_problem, &pctx);
459 pctx.blk = pctx.blk2 = i;
460 save_problem = problem;
463 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
467 * If there a problem we should turn off the discard so we
468 * do not compromise the filesystem.
470 ctx->options &= ~E2F_OPT_DISCARD;
478 } else if (i > first_free) {
479 e2fsck_discard_blocks(ctx, first_free,
481 first_free = ext2fs_blocks_count(fs->super);
484 if ((blocks == fs->super->s_clusters_per_group) ||
485 (EXT2FS_B2C(fs, i) ==
486 EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1))) {
488 * If the last block of this group is free, then we can
489 * discard it as well.
491 if (!bitmap && i >= first_free)
492 e2fsck_discard_blocks(ctx, first_free,
493 (i - first_free) + 1);
495 first_free = ext2fs_blocks_count(fs->super);
497 free_array[group] = group_free;
502 if ((ctx->progress)(ctx, 5, group,
503 fs->group_desc_count*2))
507 if (pctx.blk != NO_BLK)
508 print_bitmap_problem(ctx, save_problem, &pctx);
510 fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP);
513 ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
516 ext2fs_free_block_bitmap(fs->block_map);
517 retval = ext2fs_copy_bitmap(ctx->block_found_map,
520 clear_problem_context(&pctx);
521 fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx);
522 ctx->flags |= E2F_FLAG_ABORT;
525 ext2fs_set_bitmap_padding(fs->block_map);
526 ext2fs_mark_bb_dirty(fs);
528 /* Redo the counts */
529 blocks = 0; free_blocks = 0; group_free = 0; group = 0;
530 memset(free_array, 0, fs->group_desc_count * sizeof(int));
533 } else if (fixit == 0)
534 ext2fs_unmark_valid(fs);
536 for (g = 0; g < fs->group_desc_count; g++) {
537 if (free_array[g] != ext2fs_bg_free_blocks_count(fs, g)) {
539 pctx.blk = ext2fs_bg_free_blocks_count(fs, g);
540 pctx.blk2 = free_array[g];
542 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP,
544 ext2fs_bg_free_blocks_count_set(fs, g, free_array[g]);
545 ext2fs_mark_super_dirty(fs);
547 ext2fs_unmark_valid(fs);
550 free_blocks = EXT2FS_C2B(fs, free_blocks);
551 if (free_blocks != ext2fs_free_blocks_count(fs->super)) {
553 pctx.blk = ext2fs_free_blocks_count(fs->super);
554 pctx.blk2 = free_blocks;
556 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) {
557 ext2fs_free_blocks_count_set(fs->super, free_blocks);
558 ext2fs_mark_super_dirty(fs);
562 ext2fs_free_mem(&free_array);
563 ext2fs_free_mem(&actual_buf);
564 ext2fs_free_mem(&bitmap_buf);
567 static void check_inode_bitmaps(e2fsck_t ctx)
569 ext2_filsys fs = ctx->fs;
571 unsigned int free_inodes = 0;
575 unsigned int inodes = 0;
576 ext2_ino_t *free_array;
577 ext2_ino_t *dir_array;
580 struct problem_context pctx;
581 problem_t problem, save_problem;
582 int fixit, had_problem;
586 ext2_ino_t first_free = fs->super->s_inodes_per_group + 1;
588 clear_problem_context(&pctx);
589 free_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
590 fs->group_desc_count * sizeof(ext2_ino_t), "free inode count array");
592 dir_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
593 fs->group_desc_count * sizeof(ext2_ino_t), "directory count array");
595 if ((1 < ext2fs_get_inode_bitmap_start2(ctx->inode_used_map)) ||
596 (fs->super->s_inodes_count >
597 ext2fs_get_inode_bitmap_end2(ctx->inode_used_map))) {
600 pctx.blk2 = fs->super->s_inodes_count;
601 pctx.ino = ext2fs_get_inode_bitmap_start2(ctx->inode_used_map);
602 pctx.ino2 = ext2fs_get_inode_bitmap_end2(ctx->inode_used_map);
603 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
605 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
608 if ((1 < ext2fs_get_inode_bitmap_start2(fs->inode_map)) ||
609 (fs->super->s_inodes_count >
610 ext2fs_get_inode_bitmap_end2(fs->inode_map))) {
613 pctx.blk2 = fs->super->s_inodes_count;
614 pctx.ino = ext2fs_get_inode_bitmap_start2(fs->inode_map);
615 pctx.ino2 = ext2fs_get_inode_bitmap_end2(fs->inode_map);
616 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
618 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
622 csum_flag = ext2fs_has_group_desc_csum(fs);
626 pctx.ino = pctx.ino2 = 0;
628 (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
631 /* Protect loop from wrap-around if inodes_count is maxed */
632 for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
635 i % fs->super->s_inodes_per_group == 1) {
637 * Current inode is the first inode
638 * in the current block group.
640 if (ext2fs_test_inode_bitmap_range(
641 ctx->inode_used_map, i,
642 fs->super->s_inodes_per_group)) {
644 * When the compared inodes in inodes bitmap
645 * are 0, count the free inode,
646 * skip the current block group.
649 inodes = fs->super->s_inodes_per_group - 1;
651 free_inodes += inodes;
658 actual = ext2fs_fast_test_inode_bitmap2(ctx->inode_used_map, i);
661 else if (!skip_group)
662 bitmap = ext2fs_fast_test_inode_bitmap2(fs->inode_map, i);
663 if (!actual == !bitmap)
666 if (!actual && bitmap) {
668 * Inode wasn't used, but marked in bitmap
670 problem = PR_5_INODE_UNUSED;
671 } else /* if (actual && !bitmap) */ {
673 * Inode used, but not in bitmap
675 problem = PR_5_INODE_USED;
677 /* We should never hit this, because it means that
678 * inodes were marked in use that weren't noticed
679 * in pass1 or pass 2. It is easier to fix the problem
680 * than to kill e2fsck and leave the user stuck. */
682 struct problem_context pctx2;
685 if (fix_problem(ctx, PR_5_INODE_UNINIT,&pctx2)){
686 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
692 pctx.ino = pctx.ino2 = i;
693 save_problem = problem;
695 if ((problem == save_problem) &&
699 print_bitmap_problem(ctx, save_problem, &pctx);
700 pctx.ino = pctx.ino2 = i;
701 save_problem = problem;
704 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
707 * If there a problem we should turn off the discard so we
708 * do not compromise the filesystem.
710 ctx->options &= ~E2F_OPT_DISCARD;
715 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i))
717 if (inodes > first_free) {
718 e2fsck_discard_inodes(ctx, group, first_free,
719 inodes - first_free);
720 first_free = fs->super->s_inodes_per_group + 1;
725 if (first_free > inodes)
729 if ((inodes == fs->super->s_inodes_per_group) ||
730 (i == fs->super->s_inodes_count)) {
732 * If the last inode is free, we can discard it as well.
734 if (!bitmap && inodes >= first_free)
735 e2fsck_discard_inodes(ctx, group, first_free,
736 inodes - first_free + 1);
738 * If discard zeroes data and the group inode table
739 * was not zeroed yet, set itable as zeroed
741 if ((ctx->options & E2F_OPT_DISCARD) &&
742 io_channel_discard_zeroes_data(fs->io) &&
743 !(ext2fs_bg_flags_test(fs, group,
744 EXT2_BG_INODE_ZEROED))) {
745 ext2fs_bg_flags_set(fs, group,
746 EXT2_BG_INODE_ZEROED);
747 ext2fs_group_desc_csum_set(fs, group);
750 first_free = fs->super->s_inodes_per_group + 1;
751 free_array[group] = group_free;
752 dir_array[group] = dirs_count;
759 if ((ctx->progress)(ctx, 5,
760 group + fs->group_desc_count,
761 fs->group_desc_count*2))
764 (i != fs->super->s_inodes_count) &&
765 (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)
771 print_bitmap_problem(ctx, save_problem, &pctx);
774 fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
777 ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
780 ext2fs_free_inode_bitmap(fs->inode_map);
781 retval = ext2fs_copy_bitmap(ctx->inode_used_map,
784 clear_problem_context(&pctx);
785 fix_problem(ctx, PR_5_COPY_IBITMAP_ERROR, &pctx);
786 ctx->flags |= E2F_FLAG_ABORT;
789 ext2fs_set_bitmap_padding(fs->inode_map);
790 ext2fs_mark_ib_dirty(fs);
793 inodes = 0; free_inodes = 0; group_free = 0;
794 dirs_count = 0; group = 0;
795 memset(free_array, 0, fs->group_desc_count * sizeof(int));
796 memset(dir_array, 0, fs->group_desc_count * sizeof(int));
799 } else if (fixit == 0)
800 ext2fs_unmark_valid(fs);
802 for (i = 0; i < fs->group_desc_count; i++) {
803 if (free_array[i] != ext2fs_bg_free_inodes_count(fs, i)) {
805 pctx.ino = ext2fs_bg_free_inodes_count(fs, i);
806 pctx.ino2 = free_array[i];
807 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP,
809 ext2fs_bg_free_inodes_count_set(fs, i, free_array[i]);
810 ext2fs_mark_super_dirty(fs);
812 ext2fs_unmark_valid(fs);
814 if (dir_array[i] != ext2fs_bg_used_dirs_count(fs, i)) {
816 pctx.ino = ext2fs_bg_used_dirs_count(fs, i);
817 pctx.ino2 = dir_array[i];
819 if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP,
821 ext2fs_bg_used_dirs_count_set(fs, i, dir_array[i]);
822 ext2fs_mark_super_dirty(fs);
824 ext2fs_unmark_valid(fs);
827 if (free_inodes != fs->super->s_free_inodes_count) {
829 pctx.ino = fs->super->s_free_inodes_count;
830 pctx.ino2 = free_inodes;
832 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT, &pctx)) {
833 fs->super->s_free_inodes_count = free_inodes;
834 ext2fs_mark_super_dirty(fs);
838 ext2fs_free_mem(&free_array);
839 ext2fs_free_mem(&dir_array);
842 static void check_inode_end(e2fsck_t ctx)
844 ext2_filsys fs = ctx->fs;
845 ext2_ino_t end, save_inodes_count, i;
846 struct problem_context pctx;
848 clear_problem_context(&pctx);
850 end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
851 pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, end,
855 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
856 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
859 if (save_inodes_count == end)
862 /* protect loop from wrap-around if end is maxed */
863 for (i = save_inodes_count + 1; i <= end && i > save_inodes_count; i++) {
864 if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) {
865 if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) {
866 for (; i <= end; i++)
867 ext2fs_mark_inode_bitmap(fs->inode_map,
869 ext2fs_mark_ib_dirty(fs);
871 ext2fs_unmark_valid(fs);
876 pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map,
877 save_inodes_count, 0);
880 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
881 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
886 static void check_block_end(e2fsck_t ctx)
888 ext2_filsys fs = ctx->fs;
889 blk64_t end, save_blocks_count, i;
890 struct problem_context pctx;
892 clear_problem_context(&pctx);
894 end = ext2fs_get_block_bitmap_start2(fs->block_map) +
895 EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count) - 1;
896 pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, end,
900 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
901 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
904 if (save_blocks_count == end)
907 /* Protect loop from wrap-around if end is maxed */
908 for (i = save_blocks_count + 1; i <= end && i > save_blocks_count; i++) {
909 if (!ext2fs_test_block_bitmap2(fs->block_map,
910 EXT2FS_C2B(fs, i))) {
911 if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) {
912 for (; i <= end; i++)
913 ext2fs_mark_block_bitmap2(fs->block_map,
915 ext2fs_mark_bb_dirty(fs);
917 ext2fs_unmark_valid(fs);
922 pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map,
923 save_blocks_count, 0);
926 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
927 ctx->flags |= E2F_FLAG_ABORT; /* fatal */