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 int old_desc_blocks
= 0;
335 blk64_t super_blk
, old_desc_blk
, new_desc_blk
;
336 char *actual_buf
, *bitmap_buf
;
338 actual_buf
= (char *) e2fsck_allocate_memory(ctx
, fs
->blocksize
,
339 "actual bitmap buffer");
340 bitmap_buf
= (char *) e2fsck_allocate_memory(ctx
, fs
->blocksize
,
341 "bitmap block buffer");
343 clear_problem_context(&pctx
);
344 free_array
= (unsigned int *) e2fsck_allocate_memory(ctx
,
345 fs
->group_desc_count
* sizeof(unsigned int), "free block count array");
347 if ((B2C(fs
->super
->s_first_data_block
) <
348 ext2fs_get_block_bitmap_start2(ctx
->block_found_map
)) ||
349 (B2C(ext2fs_blocks_count(fs
->super
)-1) >
350 ext2fs_get_block_bitmap_end2(ctx
->block_found_map
))) {
352 pctx
.blk
= B2C(fs
->super
->s_first_data_block
);
353 pctx
.blk2
= B2C(ext2fs_blocks_count(fs
->super
) - 1);
354 pctx
.ino
= ext2fs_get_block_bitmap_start2(ctx
->block_found_map
);
355 pctx
.ino2
= ext2fs_get_block_bitmap_end2(ctx
->block_found_map
);
356 fix_problem(ctx
, PR_5_BMAP_ENDPOINTS
, &pctx
);
358 ctx
->flags
|= E2F_FLAG_ABORT
; /* fatal */
362 if ((B2C(fs
->super
->s_first_data_block
) <
363 ext2fs_get_block_bitmap_start2(fs
->block_map
)) ||
364 (B2C(ext2fs_blocks_count(fs
->super
)-1) >
365 ext2fs_get_block_bitmap_end2(fs
->block_map
))) {
367 pctx
.blk
= B2C(fs
->super
->s_first_data_block
);
368 pctx
.blk2
= B2C(ext2fs_blocks_count(fs
->super
) - 1);
369 pctx
.ino
= ext2fs_get_block_bitmap_start2(fs
->block_map
);
370 pctx
.ino2
= ext2fs_get_block_bitmap_end2(fs
->block_map
);
371 fix_problem(ctx
, PR_5_BMAP_ENDPOINTS
, &pctx
);
373 ctx
->flags
|= E2F_FLAG_ABORT
; /* fatal */
377 csum_flag
= ext2fs_has_group_desc_csum(fs
);
381 pctx
.blk
= pctx
.blk2
= NO_BLK
;
382 for (i
= B2C(fs
->super
->s_first_data_block
);
383 i
< ext2fs_blocks_count(fs
->super
);
384 i
+= EXT2FS_CLUSTER_RATIO(fs
)) {
385 int first_block_in_bg
= (B2C(i
) -
386 B2C(fs
->super
->s_first_data_block
)) %
387 fs
->super
->s_clusters_per_group
== 0;
388 int n
, nbytes
= fs
->super
->s_clusters_per_group
/ 8;
390 actual
= ext2fs_fast_test_block_bitmap2(ctx
->block_found_map
, i
);
393 * Try to optimize pass5 by extracting a bitmap block
394 * as expected from what we have on disk, and then
395 * comparing the two. If they are identical, then
396 * update the free block counts and go on to the next
397 * block group. This is much faster than doing the
398 * individual bit-by-bit comparison. The one downside
399 * is that this doesn't work if we are asking e2fsck
400 * to do a discard operation.
402 if (!first_block_in_bg
||
403 (group
== (int)fs
->group_desc_count
- 1) ||
404 (ctx
->options
& E2F_OPT_DISCARD
))
407 retval
= ext2fs_get_block_bitmap_range2(ctx
->block_found_map
,
408 B2C(i
), fs
->super
->s_clusters_per_group
,
412 retval
= ext2fs_get_block_bitmap_range2(fs
->block_map
,
413 B2C(i
), fs
->super
->s_clusters_per_group
,
417 if (memcmp(actual_buf
, bitmap_buf
, nbytes
) != 0)
419 n
= ext2fs_bitcount(actual_buf
, nbytes
);
420 group_free
= fs
->super
->s_clusters_per_group
- n
;
421 free_blocks
+= group_free
;
422 i
+= EXT2FS_C2B(fs
, fs
->super
->s_clusters_per_group
- 1);
429 bitmap
= ext2fs_fast_test_block_bitmap2(fs
->block_map
, i
);
431 if (!actual
== !bitmap
)
434 if (!actual
&& bitmap
) {
436 * Block not used, but marked in use in the bitmap.
438 problem
= PR_5_BLOCK_UNUSED
;
441 * Block used, but not marked in use in the bitmap.
443 problem
= PR_5_BLOCK_USED
;
445 if (ext2fs_bg_flags_test(fs
, group
,
446 EXT2_BG_BLOCK_UNINIT
)) {
447 struct problem_context pctx2
;
450 if (fix_problem(ctx
, PR_5_BLOCK_UNINIT
,
452 ext2fs_bg_flags_clear(fs
, group
,
453 EXT2_BG_BLOCK_UNINIT
);
456 if (pctx
.blk
== NO_BLK
) {
457 pctx
.blk
= pctx
.blk2
= i
;
458 save_problem
= problem
;
460 if ((problem
== save_problem
) &&
461 (pctx
.blk2
== i
- EXT2FS_CLUSTER_RATIO(fs
)))
462 pctx
.blk2
+= EXT2FS_CLUSTER_RATIO(fs
);
464 print_bitmap_problem(ctx
, save_problem
, &pctx
);
465 pctx
.blk
= pctx
.blk2
= i
;
466 save_problem
= problem
;
469 ctx
->flags
|= E2F_FLAG_PROG_SUPPRESS
;
473 * If there a problem we should turn off the discard so we
474 * do not compromise the filesystem.
476 ctx
->options
&= ~E2F_OPT_DISCARD
;
484 } else if (i
> first_free
) {
485 e2fsck_discard_blocks(ctx
, first_free
,
487 first_free
= ext2fs_blocks_count(fs
->super
);
490 if ((blocks
== fs
->super
->s_clusters_per_group
) ||
491 (EXT2FS_B2C(fs
, i
) ==
492 EXT2FS_B2C(fs
, ext2fs_blocks_count(fs
->super
)-1))) {
494 * If the last block of this group is free, then we can
495 * discard it as well.
497 if (!bitmap
&& i
>= first_free
)
498 e2fsck_discard_blocks(ctx
, first_free
,
499 (i
- first_free
) + 1);
501 first_free
= ext2fs_blocks_count(fs
->super
);
503 free_array
[group
] = group_free
;
508 if ((ctx
->progress
)(ctx
, 5, group
,
509 fs
->group_desc_count
*2))
513 if (pctx
.blk
!= NO_BLK
)
514 print_bitmap_problem(ctx
, save_problem
, &pctx
);
516 fixit
= end_problem_latch(ctx
, PR_LATCH_BBITMAP
);
519 ctx
->flags
&= ~E2F_FLAG_PROG_SUPPRESS
;
522 ext2fs_free_block_bitmap(fs
->block_map
);
523 retval
= ext2fs_copy_bitmap(ctx
->block_found_map
,
526 clear_problem_context(&pctx
);
527 fix_problem(ctx
, PR_5_COPY_BBITMAP_ERROR
, &pctx
);
528 ctx
->flags
|= E2F_FLAG_ABORT
;
531 ext2fs_set_bitmap_padding(fs
->block_map
);
532 ext2fs_mark_bb_dirty(fs
);
534 /* Redo the counts */
535 blocks
= 0; free_blocks
= 0; group_free
= 0; group
= 0;
536 memset(free_array
, 0, fs
->group_desc_count
* sizeof(int));
539 } else if (fixit
== 0)
540 ext2fs_unmark_valid(fs
);
542 for (g
= 0; g
< fs
->group_desc_count
; g
++) {
543 if (free_array
[g
] != ext2fs_bg_free_blocks_count(fs
, g
)) {
545 pctx
.blk
= ext2fs_bg_free_blocks_count(fs
, g
);
546 pctx
.blk2
= free_array
[g
];
548 if (fix_problem(ctx
, PR_5_FREE_BLOCK_COUNT_GROUP
,
550 ext2fs_bg_free_blocks_count_set(fs
, g
, free_array
[g
]);
551 ext2fs_mark_super_dirty(fs
);
553 ext2fs_unmark_valid(fs
);
556 free_blocks
= EXT2FS_C2B(fs
, free_blocks
);
557 if (free_blocks
!= ext2fs_free_blocks_count(fs
->super
)) {
559 pctx
.blk
= ext2fs_free_blocks_count(fs
->super
);
560 pctx
.blk2
= free_blocks
;
562 if (fix_problem(ctx
, PR_5_FREE_BLOCK_COUNT
, &pctx
)) {
563 ext2fs_free_blocks_count_set(fs
->super
, free_blocks
);
564 ext2fs_mark_super_dirty(fs
);
568 ext2fs_free_mem(&free_array
);
569 ext2fs_free_mem(&actual_buf
);
570 ext2fs_free_mem(&bitmap_buf
);
573 static void check_inode_bitmaps(e2fsck_t ctx
)
575 ext2_filsys fs
= ctx
->fs
;
577 unsigned int free_inodes
= 0;
581 unsigned int inodes
= 0;
582 ext2_ino_t
*free_array
;
583 ext2_ino_t
*dir_array
;
586 struct problem_context pctx
;
587 problem_t problem
, save_problem
;
588 int fixit
, had_problem
;
592 ext2_ino_t first_free
= fs
->super
->s_inodes_per_group
+ 1;
594 clear_problem_context(&pctx
);
595 free_array
= (ext2_ino_t
*) e2fsck_allocate_memory(ctx
,
596 fs
->group_desc_count
* sizeof(ext2_ino_t
), "free inode count array");
598 dir_array
= (ext2_ino_t
*) e2fsck_allocate_memory(ctx
,
599 fs
->group_desc_count
* sizeof(ext2_ino_t
), "directory count array");
601 if ((1 < ext2fs_get_inode_bitmap_start2(ctx
->inode_used_map
)) ||
602 (fs
->super
->s_inodes_count
>
603 ext2fs_get_inode_bitmap_end2(ctx
->inode_used_map
))) {
606 pctx
.blk2
= fs
->super
->s_inodes_count
;
607 pctx
.ino
= ext2fs_get_inode_bitmap_start2(ctx
->inode_used_map
);
608 pctx
.ino2
= ext2fs_get_inode_bitmap_end2(ctx
->inode_used_map
);
609 fix_problem(ctx
, PR_5_BMAP_ENDPOINTS
, &pctx
);
611 ctx
->flags
|= E2F_FLAG_ABORT
; /* fatal */
614 if ((1 < ext2fs_get_inode_bitmap_start2(fs
->inode_map
)) ||
615 (fs
->super
->s_inodes_count
>
616 ext2fs_get_inode_bitmap_end2(fs
->inode_map
))) {
619 pctx
.blk2
= fs
->super
->s_inodes_count
;
620 pctx
.ino
= ext2fs_get_inode_bitmap_start2(fs
->inode_map
);
621 pctx
.ino2
= ext2fs_get_inode_bitmap_end2(fs
->inode_map
);
622 fix_problem(ctx
, PR_5_BMAP_ENDPOINTS
, &pctx
);
624 ctx
->flags
|= E2F_FLAG_ABORT
; /* fatal */
628 csum_flag
= ext2fs_has_group_desc_csum(fs
);
632 pctx
.ino
= pctx
.ino2
= 0;
634 (ext2fs_bg_flags_test(fs
, group
, EXT2_BG_INODE_UNINIT
)))
637 /* Protect loop from wrap-around if inodes_count is maxed */
638 for (i
= 1; i
<= fs
->super
->s_inodes_count
&& i
> 0; i
++) {
641 i
% fs
->super
->s_inodes_per_group
== 1) {
643 * Current inode is the first inode
644 * in the current block group.
646 if (ext2fs_test_inode_bitmap_range(
647 ctx
->inode_used_map
, i
,
648 fs
->super
->s_inodes_per_group
)) {
650 * When the compared inodes in inodes bitmap
651 * are 0, count the free inode,
652 * skip the current block group.
655 inodes
= fs
->super
->s_inodes_per_group
- 1;
657 free_inodes
+= inodes
;
664 actual
= ext2fs_fast_test_inode_bitmap2(ctx
->inode_used_map
, i
);
667 else if (!skip_group
)
668 bitmap
= ext2fs_fast_test_inode_bitmap2(fs
->inode_map
, i
);
669 if (!actual
== !bitmap
)
672 if (!actual
&& bitmap
) {
674 * Inode wasn't used, but marked in bitmap
676 problem
= PR_5_INODE_UNUSED
;
677 } else /* if (actual && !bitmap) */ {
679 * Inode used, but not in bitmap
681 problem
= PR_5_INODE_USED
;
683 /* We should never hit this, because it means that
684 * inodes were marked in use that weren't noticed
685 * in pass1 or pass 2. It is easier to fix the problem
686 * than to kill e2fsck and leave the user stuck. */
688 struct problem_context pctx2
;
691 if (fix_problem(ctx
, PR_5_INODE_UNINIT
,&pctx2
)){
692 ext2fs_bg_flags_clear(fs
, group
, EXT2_BG_INODE_UNINIT
);
698 pctx
.ino
= pctx
.ino2
= i
;
699 save_problem
= problem
;
701 if ((problem
== save_problem
) &&
705 print_bitmap_problem(ctx
, save_problem
, &pctx
);
706 pctx
.ino
= pctx
.ino2
= i
;
707 save_problem
= problem
;
710 ctx
->flags
|= E2F_FLAG_PROG_SUPPRESS
;
713 * If there a problem we should turn off the discard so we
714 * do not compromise the filesystem.
716 ctx
->options
&= ~E2F_OPT_DISCARD
;
721 if (ext2fs_test_inode_bitmap2(ctx
->inode_dir_map
, i
))
723 if (inodes
> first_free
) {
724 e2fsck_discard_inodes(ctx
, group
, first_free
,
725 inodes
- first_free
);
726 first_free
= fs
->super
->s_inodes_per_group
+ 1;
731 if (first_free
> inodes
)
735 if ((inodes
== fs
->super
->s_inodes_per_group
) ||
736 (i
== fs
->super
->s_inodes_count
)) {
738 * If the last inode is free, we can discard it as well.
740 if (!bitmap
&& inodes
>= first_free
)
741 e2fsck_discard_inodes(ctx
, group
, first_free
,
742 inodes
- first_free
+ 1);
744 * If discard zeroes data and the group inode table
745 * was not zeroed yet, set itable as zeroed
747 if ((ctx
->options
& E2F_OPT_DISCARD
) &&
748 io_channel_discard_zeroes_data(fs
->io
) &&
749 !(ext2fs_bg_flags_test(fs
, group
,
750 EXT2_BG_INODE_ZEROED
))) {
751 ext2fs_bg_flags_set(fs
, group
,
752 EXT2_BG_INODE_ZEROED
);
753 ext2fs_group_desc_csum_set(fs
, group
);
756 first_free
= fs
->super
->s_inodes_per_group
+ 1;
757 free_array
[group
] = group_free
;
758 dir_array
[group
] = dirs_count
;
765 if ((ctx
->progress
)(ctx
, 5,
766 group
+ fs
->group_desc_count
,
767 fs
->group_desc_count
*2))
770 (i
!= fs
->super
->s_inodes_count
) &&
771 (ext2fs_bg_flags_test(fs
, group
, EXT2_BG_INODE_UNINIT
)
777 print_bitmap_problem(ctx
, save_problem
, &pctx
);
780 fixit
= end_problem_latch(ctx
, PR_LATCH_IBITMAP
);
783 ctx
->flags
&= ~E2F_FLAG_PROG_SUPPRESS
;
786 ext2fs_free_inode_bitmap(fs
->inode_map
);
787 retval
= ext2fs_copy_bitmap(ctx
->inode_used_map
,
790 clear_problem_context(&pctx
);
791 fix_problem(ctx
, PR_5_COPY_IBITMAP_ERROR
, &pctx
);
792 ctx
->flags
|= E2F_FLAG_ABORT
;
795 ext2fs_set_bitmap_padding(fs
->inode_map
);
796 ext2fs_mark_ib_dirty(fs
);
799 inodes
= 0; free_inodes
= 0; group_free
= 0;
800 dirs_count
= 0; group
= 0;
801 memset(free_array
, 0, fs
->group_desc_count
* sizeof(int));
802 memset(dir_array
, 0, fs
->group_desc_count
* sizeof(int));
805 } else if (fixit
== 0)
806 ext2fs_unmark_valid(fs
);
808 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
809 if (free_array
[i
] != ext2fs_bg_free_inodes_count(fs
, i
)) {
811 pctx
.ino
= ext2fs_bg_free_inodes_count(fs
, i
);
812 pctx
.ino2
= free_array
[i
];
813 if (fix_problem(ctx
, PR_5_FREE_INODE_COUNT_GROUP
,
815 ext2fs_bg_free_inodes_count_set(fs
, i
, free_array
[i
]);
816 ext2fs_mark_super_dirty(fs
);
818 ext2fs_unmark_valid(fs
);
820 if (dir_array
[i
] != ext2fs_bg_used_dirs_count(fs
, i
)) {
822 pctx
.ino
= ext2fs_bg_used_dirs_count(fs
, i
);
823 pctx
.ino2
= dir_array
[i
];
825 if (fix_problem(ctx
, PR_5_FREE_DIR_COUNT_GROUP
,
827 ext2fs_bg_used_dirs_count_set(fs
, i
, dir_array
[i
]);
828 ext2fs_mark_super_dirty(fs
);
830 ext2fs_unmark_valid(fs
);
833 if (free_inodes
!= fs
->super
->s_free_inodes_count
) {
835 pctx
.ino
= fs
->super
->s_free_inodes_count
;
836 pctx
.ino2
= free_inodes
;
838 if (fix_problem(ctx
, PR_5_FREE_INODE_COUNT
, &pctx
)) {
839 fs
->super
->s_free_inodes_count
= free_inodes
;
840 ext2fs_mark_super_dirty(fs
);
844 ext2fs_free_mem(&free_array
);
845 ext2fs_free_mem(&dir_array
);
848 static void check_inode_end(e2fsck_t ctx
)
850 ext2_filsys fs
= ctx
->fs
;
851 ext2_ino_t end
, save_inodes_count
, i
;
852 struct problem_context pctx
;
854 clear_problem_context(&pctx
);
856 end
= EXT2_INODES_PER_GROUP(fs
->super
) * fs
->group_desc_count
;
857 pctx
.errcode
= ext2fs_fudge_inode_bitmap_end(fs
->inode_map
, end
,
861 fix_problem(ctx
, PR_5_FUDGE_BITMAP_ERROR
, &pctx
);
862 ctx
->flags
|= E2F_FLAG_ABORT
; /* fatal */
865 if (save_inodes_count
== end
)
868 /* protect loop from wrap-around if end is maxed */
869 for (i
= save_inodes_count
+ 1; i
<= end
&& i
> save_inodes_count
; i
++) {
870 if (!ext2fs_test_inode_bitmap(fs
->inode_map
, i
)) {
871 if (fix_problem(ctx
, PR_5_INODE_BMAP_PADDING
, &pctx
)) {
872 for (; i
<= end
; i
++)
873 ext2fs_mark_inode_bitmap(fs
->inode_map
,
875 ext2fs_mark_ib_dirty(fs
);
877 ext2fs_unmark_valid(fs
);
882 pctx
.errcode
= ext2fs_fudge_inode_bitmap_end(fs
->inode_map
,
883 save_inodes_count
, 0);
886 fix_problem(ctx
, PR_5_FUDGE_BITMAP_ERROR
, &pctx
);
887 ctx
->flags
|= E2F_FLAG_ABORT
; /* fatal */
892 static void check_block_end(e2fsck_t ctx
)
894 ext2_filsys fs
= ctx
->fs
;
895 blk64_t end
, save_blocks_count
, i
;
896 struct problem_context pctx
;
898 clear_problem_context(&pctx
);
900 end
= ext2fs_get_block_bitmap_start2(fs
->block_map
) +
901 ((blk64_t
)EXT2_CLUSTERS_PER_GROUP(fs
->super
) * fs
->group_desc_count
) - 1;
902 pctx
.errcode
= ext2fs_fudge_block_bitmap_end2(fs
->block_map
, end
,
906 fix_problem(ctx
, PR_5_FUDGE_BITMAP_ERROR
, &pctx
);
907 ctx
->flags
|= E2F_FLAG_ABORT
; /* fatal */
910 if (save_blocks_count
== end
)
913 /* Protect loop from wrap-around if end is maxed */
914 for (i
= save_blocks_count
+ 1; i
<= end
&& i
> save_blocks_count
; i
++) {
915 if (!ext2fs_test_block_bitmap2(fs
->block_map
,
916 EXT2FS_C2B(fs
, i
))) {
917 if (fix_problem(ctx
, PR_5_BLOCK_BMAP_PADDING
, &pctx
)) {
918 for (; i
<= end
; i
++)
919 ext2fs_mark_block_bitmap2(fs
->block_map
,
921 ext2fs_mark_bb_dirty(fs
);
923 ext2fs_unmark_valid(fs
);
928 pctx
.errcode
= ext2fs_fudge_block_bitmap_end2(fs
->block_map
,
929 save_blocks_count
, 0);
932 fix_problem(ctx
, PR_5_FUDGE_BITMAP_ERROR
, &pctx
);
933 ctx
->flags
|= E2F_FLAG_ABORT
; /* fatal */