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;
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 */
379 pctx.blk = pctx.blk2 = NO_BLK;
380 for (i = B2C(fs->super->s_first_data_block);
381 i < ext2fs_blocks_count(fs->super);
382 i += EXT2FS_CLUSTER_RATIO(fs)) {
383 int first_block_in_bg = (B2C(i) -
384 B2C(fs->super->s_first_data_block)) %
385 fs->super->s_clusters_per_group == 0;
386 int n, nbytes = fs->super->s_clusters_per_group / 8;
388 actual = ext2fs_fast_test_block_bitmap2(ctx->block_found_map, i);
391 * Try to optimize pass5 by extracting a bitmap block
392 * as expected from what we have on disk, and then
393 * comparing the two. If they are identical, then
394 * update the free block counts and go on to the next
395 * block group. This is much faster than doing the
396 * individual bit-by-bit comparison. The one downside
397 * is that this doesn't work if we are asking e2fsck
398 * to do a discard operation.
400 if (!first_block_in_bg ||
401 (group == (int)fs->group_desc_count - 1) ||
402 (ctx->options & E2F_OPT_DISCARD))
405 retval = ext2fs_get_block_bitmap_range2(ctx->block_found_map,
406 B2C(i), fs->super->s_clusters_per_group,
410 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
411 B2C(i), fs->super->s_clusters_per_group,
415 if (memcmp(actual_buf, bitmap_buf, nbytes) != 0)
417 n = ext2fs_bitcount(actual_buf, nbytes);
418 group_free = fs->super->s_clusters_per_group - n;
419 free_blocks += group_free;
420 i += EXT2FS_C2B(fs, fs->super->s_clusters_per_group - 1);
427 bitmap = ext2fs_fast_test_block_bitmap2(fs->block_map, i);
429 if (!actual == !bitmap)
432 if (!actual && bitmap) {
434 * Block not used, but marked in use in the bitmap.
436 problem = PR_5_BLOCK_UNUSED;
439 * Block used, but not marked in use in the bitmap.
441 problem = PR_5_BLOCK_USED;
443 if (ext2fs_bg_flags_test(fs, group,
444 EXT2_BG_BLOCK_UNINIT)) {
445 struct problem_context pctx2;
448 if (fix_problem(ctx, PR_5_BLOCK_UNINIT,
450 ext2fs_bg_flags_clear(fs, group,
451 EXT2_BG_BLOCK_UNINIT);
454 if (pctx.blk == NO_BLK) {
455 pctx.blk = pctx.blk2 = i;
456 save_problem = problem;
458 if ((problem == save_problem) &&
459 (pctx.blk2 == i - EXT2FS_CLUSTER_RATIO(fs)))
460 pctx.blk2 += EXT2FS_CLUSTER_RATIO(fs);
462 print_bitmap_problem(ctx, save_problem, &pctx);
463 pctx.blk = pctx.blk2 = i;
464 save_problem = problem;
467 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
471 * If there a problem we should turn off the discard so we
472 * do not compromise the filesystem.
474 ctx->options &= ~E2F_OPT_DISCARD;
482 } else if (i > first_free) {
483 e2fsck_discard_blocks(ctx, first_free,
485 first_free = ext2fs_blocks_count(fs->super);
488 if ((blocks == fs->super->s_clusters_per_group) ||
489 (EXT2FS_B2C(fs, i) ==
490 EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1))) {
492 * If the last block of this group is free, then we can
493 * discard it as well.
495 if (!bitmap && i >= first_free)
496 e2fsck_discard_blocks(ctx, first_free,
497 (i - first_free) + 1);
499 first_free = ext2fs_blocks_count(fs->super);
501 free_array[group] = group_free;
506 if ((ctx->progress)(ctx, 5, group,
507 fs->group_desc_count*2))
511 if (pctx.blk != NO_BLK)
512 print_bitmap_problem(ctx, save_problem, &pctx);
514 fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP);
517 ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
520 ext2fs_free_block_bitmap(fs->block_map);
521 retval = ext2fs_copy_bitmap(ctx->block_found_map,
524 clear_problem_context(&pctx);
525 fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx);
526 ctx->flags |= E2F_FLAG_ABORT;
529 ext2fs_set_bitmap_padding(fs->block_map);
530 ext2fs_mark_bb_dirty(fs);
532 /* Redo the counts */
533 blocks = 0; free_blocks = 0; group_free = 0; group = 0;
534 memset(free_array, 0, fs->group_desc_count * sizeof(int));
537 } else if (fixit == 0)
538 ext2fs_unmark_valid(fs);
540 for (g = 0; g < fs->group_desc_count; g++) {
541 if (free_array[g] != ext2fs_bg_free_blocks_count(fs, g)) {
543 pctx.blk = ext2fs_bg_free_blocks_count(fs, g);
544 pctx.blk2 = free_array[g];
546 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP,
548 ext2fs_bg_free_blocks_count_set(fs, g, free_array[g]);
549 ext2fs_mark_super_dirty(fs);
551 ext2fs_unmark_valid(fs);
554 free_blocks = EXT2FS_C2B(fs, free_blocks);
555 if (free_blocks != ext2fs_free_blocks_count(fs->super)) {
557 pctx.blk = ext2fs_free_blocks_count(fs->super);
558 pctx.blk2 = free_blocks;
560 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) {
561 ext2fs_free_blocks_count_set(fs->super, free_blocks);
562 ext2fs_mark_super_dirty(fs);
566 ext2fs_free_mem(&free_array);
567 ext2fs_free_mem(&actual_buf);
568 ext2fs_free_mem(&bitmap_buf);
571 static void check_inode_bitmaps(e2fsck_t ctx)
573 ext2_filsys fs = ctx->fs;
575 unsigned int free_inodes = 0;
579 unsigned int inodes = 0;
580 ext2_ino_t *free_array;
581 ext2_ino_t *dir_array;
584 struct problem_context pctx;
585 problem_t problem, save_problem;
586 int fixit, had_problem;
590 ext2_ino_t first_free = fs->super->s_inodes_per_group + 1;
592 clear_problem_context(&pctx);
593 free_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
594 fs->group_desc_count * sizeof(ext2_ino_t), "free inode count array");
596 dir_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
597 fs->group_desc_count * sizeof(ext2_ino_t), "directory count array");
599 if ((1 < ext2fs_get_inode_bitmap_start2(ctx->inode_used_map)) ||
600 (fs->super->s_inodes_count >
601 ext2fs_get_inode_bitmap_end2(ctx->inode_used_map))) {
604 pctx.blk2 = fs->super->s_inodes_count;
605 pctx.ino = ext2fs_get_inode_bitmap_start2(ctx->inode_used_map);
606 pctx.ino2 = ext2fs_get_inode_bitmap_end2(ctx->inode_used_map);
607 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
609 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
612 if ((1 < ext2fs_get_inode_bitmap_start2(fs->inode_map)) ||
613 (fs->super->s_inodes_count >
614 ext2fs_get_inode_bitmap_end2(fs->inode_map))) {
617 pctx.blk2 = fs->super->s_inodes_count;
618 pctx.ino = ext2fs_get_inode_bitmap_start2(fs->inode_map);
619 pctx.ino2 = ext2fs_get_inode_bitmap_end2(fs->inode_map);
620 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
622 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
626 csum_flag = ext2fs_has_group_desc_csum(fs);
630 pctx.ino = pctx.ino2 = 0;
632 (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
635 /* Protect loop from wrap-around if inodes_count is maxed */
636 for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
639 i % fs->super->s_inodes_per_group == 1) {
641 * Current inode is the first inode
642 * in the current block group.
644 if (ext2fs_test_inode_bitmap_range(
645 ctx->inode_used_map, i,
646 fs->super->s_inodes_per_group)) {
648 * When the compared inodes in inodes bitmap
649 * are 0, count the free inode,
650 * skip the current block group.
653 inodes = fs->super->s_inodes_per_group - 1;
655 free_inodes += inodes;
662 actual = ext2fs_fast_test_inode_bitmap2(ctx->inode_used_map, i);
665 else if (!skip_group)
666 bitmap = ext2fs_fast_test_inode_bitmap2(fs->inode_map, i);
667 if (!actual == !bitmap)
670 if (!actual && bitmap) {
672 * Inode wasn't used, but marked in bitmap
674 problem = PR_5_INODE_UNUSED;
675 } else /* if (actual && !bitmap) */ {
677 * Inode used, but not in bitmap
679 problem = PR_5_INODE_USED;
681 /* We should never hit this, because it means that
682 * inodes were marked in use that weren't noticed
683 * in pass1 or pass 2. It is easier to fix the problem
684 * than to kill e2fsck and leave the user stuck. */
686 struct problem_context pctx2;
689 if (fix_problem(ctx, PR_5_INODE_UNINIT,&pctx2)){
690 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
696 pctx.ino = pctx.ino2 = i;
697 save_problem = problem;
699 if ((problem == save_problem) &&
703 print_bitmap_problem(ctx, save_problem, &pctx);
704 pctx.ino = pctx.ino2 = i;
705 save_problem = problem;
708 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
711 * If there a problem we should turn off the discard so we
712 * do not compromise the filesystem.
714 ctx->options &= ~E2F_OPT_DISCARD;
719 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i))
721 if (inodes > first_free) {
722 e2fsck_discard_inodes(ctx, group, first_free,
723 inodes - first_free);
724 first_free = fs->super->s_inodes_per_group + 1;
729 if (first_free > inodes)
733 if ((inodes == fs->super->s_inodes_per_group) ||
734 (i == fs->super->s_inodes_count)) {
736 * If the last inode is free, we can discard it as well.
738 if (!bitmap && inodes >= first_free)
739 e2fsck_discard_inodes(ctx, group, first_free,
740 inodes - first_free + 1);
742 * If discard zeroes data and the group inode table
743 * was not zeroed yet, set itable as zeroed
745 if ((ctx->options & E2F_OPT_DISCARD) &&
746 io_channel_discard_zeroes_data(fs->io) &&
747 !(ext2fs_bg_flags_test(fs, group,
748 EXT2_BG_INODE_ZEROED))) {
749 ext2fs_bg_flags_set(fs, group,
750 EXT2_BG_INODE_ZEROED);
751 ext2fs_group_desc_csum_set(fs, group);
754 first_free = fs->super->s_inodes_per_group + 1;
755 free_array[group] = group_free;
756 dir_array[group] = dirs_count;
763 if ((ctx->progress)(ctx, 5,
764 group + fs->group_desc_count,
765 fs->group_desc_count*2))
768 (i != fs->super->s_inodes_count) &&
769 (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)
775 print_bitmap_problem(ctx, save_problem, &pctx);
778 fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
781 ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
784 ext2fs_free_inode_bitmap(fs->inode_map);
785 retval = ext2fs_copy_bitmap(ctx->inode_used_map,
788 clear_problem_context(&pctx);
789 fix_problem(ctx, PR_5_COPY_IBITMAP_ERROR, &pctx);
790 ctx->flags |= E2F_FLAG_ABORT;
793 ext2fs_set_bitmap_padding(fs->inode_map);
794 ext2fs_mark_ib_dirty(fs);
797 inodes = 0; free_inodes = 0; group_free = 0;
798 dirs_count = 0; group = 0;
799 memset(free_array, 0, fs->group_desc_count * sizeof(int));
800 memset(dir_array, 0, fs->group_desc_count * sizeof(int));
803 } else if (fixit == 0)
804 ext2fs_unmark_valid(fs);
806 for (i = 0; i < fs->group_desc_count; i++) {
807 if (free_array[i] != ext2fs_bg_free_inodes_count(fs, i)) {
809 pctx.ino = ext2fs_bg_free_inodes_count(fs, i);
810 pctx.ino2 = free_array[i];
811 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP,
813 ext2fs_bg_free_inodes_count_set(fs, i, free_array[i]);
814 ext2fs_mark_super_dirty(fs);
816 ext2fs_unmark_valid(fs);
818 if (dir_array[i] != ext2fs_bg_used_dirs_count(fs, i)) {
820 pctx.ino = ext2fs_bg_used_dirs_count(fs, i);
821 pctx.ino2 = dir_array[i];
823 if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP,
825 ext2fs_bg_used_dirs_count_set(fs, i, dir_array[i]);
826 ext2fs_mark_super_dirty(fs);
828 ext2fs_unmark_valid(fs);
831 if (free_inodes != fs->super->s_free_inodes_count) {
833 pctx.ino = fs->super->s_free_inodes_count;
834 pctx.ino2 = free_inodes;
836 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT, &pctx)) {
837 fs->super->s_free_inodes_count = free_inodes;
838 ext2fs_mark_super_dirty(fs);
842 ext2fs_free_mem(&free_array);
843 ext2fs_free_mem(&dir_array);
846 static void check_inode_end(e2fsck_t ctx)
848 ext2_filsys fs = ctx->fs;
849 ext2_ino_t end, save_inodes_count, i;
850 struct problem_context pctx;
852 clear_problem_context(&pctx);
854 end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
855 pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, end,
859 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
860 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
863 if (save_inodes_count == end)
866 /* protect loop from wrap-around if end is maxed */
867 for (i = save_inodes_count + 1; i <= end && i > save_inodes_count; i++) {
868 if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) {
869 if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) {
870 for (; i <= end; i++)
871 ext2fs_mark_inode_bitmap(fs->inode_map,
873 ext2fs_mark_ib_dirty(fs);
875 ext2fs_unmark_valid(fs);
880 pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map,
881 save_inodes_count, 0);
884 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
885 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
890 static void check_block_end(e2fsck_t ctx)
892 ext2_filsys fs = ctx->fs;
893 blk64_t end, save_blocks_count, i;
894 struct problem_context pctx;
896 clear_problem_context(&pctx);
898 end = ext2fs_get_block_bitmap_start2(fs->block_map) +
899 EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count) - 1;
900 pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, end,
904 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
905 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
908 if (save_blocks_count == end)
911 /* Protect loop from wrap-around if end is maxed */
912 for (i = save_blocks_count + 1; i <= end && i > save_blocks_count; i++) {
913 if (!ext2fs_test_block_bitmap2(fs->block_map,
914 EXT2FS_C2B(fs, i))) {
915 if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) {
916 for (; i <= end; i++)
917 ext2fs_mark_block_bitmap2(fs->block_map,
919 ext2fs_mark_bb_dirty(fs);
921 ext2fs_unmark_valid(fs);
926 pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map,
927 save_blocks_count, 0);
930 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
931 ctx->flags |= E2F_FLAG_ABORT; /* fatal */