2 * e2fsck.c - superblock checks
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
17 #ifndef EXT2_SKIP_UUID
18 #include "uuid/uuid.h"
27 static void check_super_value(e2fsck_t ctx
, const char *descr
,
28 unsigned long value
, int flags
,
29 unsigned long min_val
, unsigned long max_val
)
31 struct problem_context pctx
;
33 if ((flags
& MIN_CHECK
&& value
< min_val
) ||
34 (flags
& MAX_CHECK
&& value
> max_val
) ||
35 (flags
& LOG2_CHECK
&& (value
& (value
- 1)) != 0)) {
36 clear_problem_context(&pctx
);
39 fix_problem(ctx
, PR_0_MISC_CORRUPT_SUPER
, &pctx
);
40 ctx
->flags
|= E2F_FLAG_ABORT
; /* never get here! */
45 * helper function to release an inode
47 struct process_block_struct
{
50 struct problem_context
*pctx
;
53 e2_blkcnt_t truncate_block
;
59 static int release_inode_block(ext2_filsys fs
,
62 blk64_t ref_blk
EXT2FS_ATTR((unused
)),
63 int ref_offset
EXT2FS_ATTR((unused
)),
66 struct process_block_struct
*pb
;
68 struct problem_context
*pctx
;
69 blk64_t blk
= *block_nr
;
72 pb
= (struct process_block_struct
*) priv_data
;
77 pctx
->blkcount
= blockcnt
;
79 if (HOLE_BLKADDR(blk
))
82 if ((blk
< fs
->super
->s_first_data_block
) ||
83 (blk
>= ext2fs_blocks_count(fs
->super
))) {
84 fix_problem(ctx
, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM
, pctx
);
90 if (!ext2fs_test_block_bitmap2(fs
->block_map
, blk
)) {
91 fix_problem(ctx
, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK
, pctx
);
96 * If we are deleting an orphan, then we leave the fields alone.
97 * If we are truncating an orphan, then update the inode fields
98 * and clean up any partial block data.
100 if (pb
->truncating
) {
102 * We only remove indirect blocks if they are
109 pb
->errcode
= io_channel_read_blk64(fs
->io
, blk
, 1,
114 limit
= fs
->blocksize
>> 2;
115 for (i
= 0, bp
= (blk_t
*) pb
->buf
;
116 i
< limit
; i
++, bp
++)
121 * We don't remove direct blocks until we've reached
122 * the truncation block.
124 if (blockcnt
>= 0 && blockcnt
< pb
->truncate_block
)
127 * If part of the last block needs truncating, we do
130 if ((blockcnt
== pb
->truncate_block
) && pb
->truncate_offset
) {
131 pb
->errcode
= io_channel_read_blk64(fs
->io
, blk
, 1,
135 memset(pb
->buf
+ pb
->truncate_offset
, 0,
136 fs
->blocksize
- pb
->truncate_offset
);
137 pb
->errcode
= io_channel_write_blk64(fs
->io
, blk
, 1,
142 pb
->truncated_blocks
++;
144 retval
|= BLOCK_CHANGED
;
147 ext2fs_block_alloc_stats2(fs
, blk
, -1);
153 * This function releases an inode. Returns 1 if an inconsistency was
154 * found. If the inode has a link count, then it is being truncated and
157 static int release_inode_blocks(e2fsck_t ctx
, ext2_ino_t ino
,
158 struct ext2_inode
*inode
, char *block_buf
,
159 struct problem_context
*pctx
)
161 struct process_block_struct pb
;
162 ext2_filsys fs
= ctx
->fs
;
166 if (!ext2fs_inode_has_valid_blocks2(fs
, inode
))
169 pb
.buf
= block_buf
+ 3 * ctx
->fs
->blocksize
;
174 if (inode
->i_links_count
) {
176 pb
.truncate_block
= (e2_blkcnt_t
)
177 ((EXT2_I_SIZE(inode
) + fs
->blocksize
- 1) /
179 pb
.truncate_offset
= inode
->i_size
% fs
->blocksize
;
182 pb
.truncate_block
= 0;
183 pb
.truncate_offset
= 0;
185 pb
.truncated_blocks
= 0;
186 retval
= ext2fs_block_iterate3(fs
, ino
, BLOCK_FLAG_DEPTH_TRAVERSE
,
187 block_buf
, release_inode_block
, &pb
);
189 com_err("release_inode_blocks", retval
,
190 _("while calling ext2fs_block_iterate for inode %d"),
197 /* Refresh the inode since ext2fs_block_iterate may have changed it */
198 e2fsck_read_inode(ctx
, ino
, inode
, "release_inode_blocks");
200 if (pb
.truncated_blocks
)
201 ext2fs_iblk_sub_blocks(fs
, inode
, pb
.truncated_blocks
);
203 if (ext2fs_file_acl_block(fs
, inode
)) {
204 retval
= ext2fs_adjust_ea_refcount3(fs
,
205 ext2fs_file_acl_block(fs
, inode
),
206 block_buf
, -1, &count
, ino
);
207 if (retval
== EXT2_ET_BAD_EA_BLOCK_NUM
) {
212 com_err("release_inode_blocks", retval
,
213 _("while calling ext2fs_adjust_ea_refcount2 for inode %d"),
218 ext2fs_block_alloc_stats2(fs
,
219 ext2fs_file_acl_block(fs
, inode
), -1);
222 ext2fs_file_acl_block_set(fs
, inode
, 0);
228 * This function releases all of the orphan inodes. It returns 1 if
229 * it hit some error, and 0 on success.
231 static int release_orphan_inodes(e2fsck_t ctx
)
233 ext2_filsys fs
= ctx
->fs
;
234 ext2_ino_t ino
, next_ino
;
235 struct ext2_inode inode
;
236 struct problem_context pctx
;
239 if ((ino
= fs
->super
->s_last_orphan
) == 0)
243 * Win or lose, we won't be using the head of the orphan inode
246 fs
->super
->s_last_orphan
= 0;
247 ext2fs_mark_super_dirty(fs
);
250 * If the filesystem contains errors, don't run the orphan
251 * list, since the orphan list can't be trusted; and we're
252 * going to be running a full e2fsck run anyway...
254 if (fs
->super
->s_state
& EXT2_ERROR_FS
)
257 if ((ino
< EXT2_FIRST_INODE(fs
->super
)) ||
258 (ino
> fs
->super
->s_inodes_count
)) {
259 clear_problem_context(&pctx
);
261 fix_problem(ctx
, PR_0_ORPHAN_ILLEGAL_HEAD_INODE
, &pctx
);
265 block_buf
= (char *) e2fsck_allocate_memory(ctx
, fs
->blocksize
* 4,
266 "block iterate buffer");
267 e2fsck_read_bitmaps(ctx
);
270 e2fsck_read_inode(ctx
, ino
, &inode
, "release_orphan_inodes");
271 clear_problem_context(&pctx
);
274 pctx
.str
= inode
.i_links_count
? _("Truncating") :
277 fix_problem(ctx
, PR_0_ORPHAN_CLEAR_INODE
, &pctx
);
279 next_ino
= inode
.i_dtime
;
281 ((next_ino
< EXT2_FIRST_INODE(fs
->super
)) ||
282 (next_ino
> fs
->super
->s_inodes_count
))) {
284 fix_problem(ctx
, PR_0_ORPHAN_ILLEGAL_INODE
, &pctx
);
288 if (release_inode_blocks(ctx
, ino
, &inode
, block_buf
, &pctx
))
291 if (!inode
.i_links_count
) {
292 ext2fs_inode_alloc_stats2(fs
, ino
, -1,
293 LINUX_S_ISDIR(inode
.i_mode
));
295 inode
.i_dtime
= ctx
->now
;
299 e2fsck_write_inode(ctx
, ino
, &inode
, "delete_file");
302 ext2fs_free_mem(&block_buf
);
305 ext2fs_free_mem(&block_buf
);
310 * Check the resize inode to make sure it is sane. We check both for
311 * the case where on-line resizing is not enabled (in which case the
312 * resize inode should be cleared) as well as the case where on-line
313 * resizing is enabled.
315 void check_resize_inode(e2fsck_t ctx
)
317 ext2_filsys fs
= ctx
->fs
;
318 struct ext2_inode inode
;
319 struct problem_context pctx
;
320 int i
, gdt_off
, ind_off
;
323 blk_t expect
; /* for resize inode, which is 32-bit only */
324 __u32
*dind_buf
= 0, *ind_buf
;
327 clear_problem_context(&pctx
);
330 * If the resize inode feature isn't set, then
331 * s_reserved_gdt_blocks must be zero.
333 if (!(fs
->super
->s_feature_compat
&
334 EXT2_FEATURE_COMPAT_RESIZE_INODE
)) {
335 if (fs
->super
->s_reserved_gdt_blocks
) {
336 pctx
.num
= fs
->super
->s_reserved_gdt_blocks
;
337 if (fix_problem(ctx
, PR_0_NONZERO_RESERVED_GDT_BLOCKS
,
339 fs
->super
->s_reserved_gdt_blocks
= 0;
340 ext2fs_mark_super_dirty(fs
);
345 /* Read the resize inode */
346 pctx
.ino
= EXT2_RESIZE_INO
;
347 retval
= ext2fs_read_inode(fs
, EXT2_RESIZE_INO
, &inode
);
349 if (fs
->super
->s_feature_compat
&
350 EXT2_FEATURE_COMPAT_RESIZE_INODE
)
351 ctx
->flags
|= E2F_FLAG_RESIZE_INODE
;
356 * If the resize inode feature isn't set, check to make sure
357 * the resize inode is cleared; then we're done.
359 if (!(fs
->super
->s_feature_compat
&
360 EXT2_FEATURE_COMPAT_RESIZE_INODE
)) {
361 for (i
=0; i
< EXT2_N_BLOCKS
; i
++) {
362 if (inode
.i_block
[i
])
365 if ((i
< EXT2_N_BLOCKS
) &&
366 fix_problem(ctx
, PR_0_CLEAR_RESIZE_INODE
, &pctx
)) {
367 memset(&inode
, 0, sizeof(inode
));
368 e2fsck_write_inode(ctx
, EXT2_RESIZE_INO
, &inode
,
375 * The resize inode feature is enabled; check to make sure the
376 * only block in use is the double indirect block
378 blk
= inode
.i_block
[EXT2_DIND_BLOCK
];
379 for (i
=0; i
< EXT2_N_BLOCKS
; i
++) {
380 if (i
!= EXT2_DIND_BLOCK
&& inode
.i_block
[i
])
383 if ((i
< EXT2_N_BLOCKS
) || !blk
|| !inode
.i_links_count
||
384 !(inode
.i_mode
& LINUX_S_IFREG
) ||
385 (blk
< fs
->super
->s_first_data_block
||
386 blk
>= ext2fs_blocks_count(fs
->super
))) {
387 resize_inode_invalid
:
388 if (fix_problem(ctx
, PR_0_RESIZE_INODE_INVALID
, &pctx
)) {
389 memset(&inode
, 0, sizeof(inode
));
390 e2fsck_write_inode(ctx
, EXT2_RESIZE_INO
, &inode
,
392 ctx
->flags
|= E2F_FLAG_RESIZE_INODE
;
394 if (!(ctx
->options
& E2F_OPT_READONLY
)) {
395 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
396 ext2fs_mark_super_dirty(fs
);
400 dind_buf
= (__u32
*) e2fsck_allocate_memory(ctx
, fs
->blocksize
* 2,
401 "resize dind buffer");
402 ind_buf
= (__u32
*) ((char *) dind_buf
+ fs
->blocksize
);
404 retval
= ext2fs_read_ind_block(fs
, blk
, dind_buf
);
406 goto resize_inode_invalid
;
408 gdt_off
= fs
->desc_blocks
;
409 pblk
= fs
->super
->s_first_data_block
+ 1 + fs
->desc_blocks
;
410 if (fs
->blocksize
== 1024 && fs
->super
->s_first_data_block
== 0)
411 pblk
++; /* Deal with 1024 blocksize bigalloc fs */
412 for (i
= 0; i
< fs
->super
->s_reserved_gdt_blocks
/ 4;
413 i
++, gdt_off
++, pblk
++) {
414 gdt_off
%= fs
->blocksize
/4;
415 if (dind_buf
[gdt_off
] != pblk
)
416 goto resize_inode_invalid
;
417 retval
= ext2fs_read_ind_block(fs
, pblk
, ind_buf
);
419 goto resize_inode_invalid
;
421 for (j
= 1; j
< fs
->group_desc_count
; j
++) {
422 if (!ext2fs_bg_has_super(fs
, j
))
424 expect
= pblk
+ EXT2_GROUPS_TO_BLOCKS(fs
->super
, j
);
425 if (ind_buf
[ind_off
] != expect
)
426 goto resize_inode_invalid
;
433 ext2fs_free_mem(&dind_buf
);
438 * This function checks the dirhash signed/unsigned hint if necessary.
440 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx
)
442 struct ext2_super_block
*sb
= ctx
->fs
->super
;
443 struct problem_context pctx
;
446 if ((ctx
->options
& E2F_OPT_READONLY
) ||
447 !(sb
->s_feature_compat
& EXT2_FEATURE_COMPAT_DIR_INDEX
) ||
448 (sb
->s_flags
& (EXT2_FLAGS_SIGNED_HASH
|EXT2_FLAGS_UNSIGNED_HASH
)))
453 clear_problem_context(&pctx
);
454 if (fix_problem(ctx
, PR_0_DIRHASH_HINT
, &pctx
)) {
455 if (((int) c
) == -1) {
456 sb
->s_flags
|= EXT2_FLAGS_SIGNED_HASH
;
458 sb
->s_flags
|= EXT2_FLAGS_UNSIGNED_HASH
;
460 ext2fs_mark_super_dirty(ctx
->fs
);
465 void check_super_block(e2fsck_t ctx
)
467 ext2_filsys fs
= ctx
->fs
;
468 blk64_t first_block
, last_block
;
469 struct ext2_super_block
*sb
= fs
->super
;
470 unsigned int ipg_max
;
472 blk64_t blocks_per_group
= fs
->super
->s_blocks_per_group
;
473 __u32 bpg_max
, cpg_max
;
474 int inodes_per_block
;
476 int accept_time_fudge
;
477 int broken_system_clock
;
480 struct problem_context pctx
;
481 blk64_t free_blocks
= 0;
482 ino_t free_inodes
= 0;
483 int csum_flag
, clear_test_fs_flag
;
485 inodes_per_block
= EXT2_INODES_PER_BLOCK(fs
->super
);
486 ipg_max
= inodes_per_block
* (blocks_per_group
- 4);
487 if (ipg_max
> EXT2_MAX_INODES_PER_GROUP(sb
))
488 ipg_max
= EXT2_MAX_INODES_PER_GROUP(sb
);
489 cpg_max
= 8 * EXT2_BLOCK_SIZE(sb
);
490 if (cpg_max
> EXT2_MAX_CLUSTERS_PER_GROUP(sb
))
491 cpg_max
= EXT2_MAX_CLUSTERS_PER_GROUP(sb
);
492 bpg_max
= 8 * EXT2_BLOCK_SIZE(sb
) * EXT2FS_CLUSTER_RATIO(fs
);
493 if (bpg_max
> EXT2_MAX_BLOCKS_PER_GROUP(sb
))
494 bpg_max
= EXT2_MAX_BLOCKS_PER_GROUP(sb
);
496 ctx
->invalid_inode_bitmap_flag
= (int *) e2fsck_allocate_memory(ctx
,
497 sizeof(int) * fs
->group_desc_count
, "invalid_inode_bitmap");
498 ctx
->invalid_block_bitmap_flag
= (int *) e2fsck_allocate_memory(ctx
,
499 sizeof(int) * fs
->group_desc_count
, "invalid_block_bitmap");
500 ctx
->invalid_inode_table_flag
= (int *) e2fsck_allocate_memory(ctx
,
501 sizeof(int) * fs
->group_desc_count
, "invalid_inode_table");
503 clear_problem_context(&pctx
);
506 * Verify the super block constants...
508 check_super_value(ctx
, "inodes_count", sb
->s_inodes_count
,
510 check_super_value(ctx
, "blocks_count", ext2fs_blocks_count(sb
),
512 check_super_value(ctx
, "first_data_block", sb
->s_first_data_block
,
513 MAX_CHECK
, 0, ext2fs_blocks_count(sb
));
514 check_super_value(ctx
, "log_block_size", sb
->s_log_block_size
,
515 MIN_CHECK
| MAX_CHECK
, 0,
516 EXT2_MAX_BLOCK_LOG_SIZE
- EXT2_MIN_BLOCK_LOG_SIZE
);
517 check_super_value(ctx
, "log_cluster_size",
518 sb
->s_log_cluster_size
,
519 MIN_CHECK
| MAX_CHECK
, sb
->s_log_block_size
,
520 (EXT2_MAX_CLUSTER_LOG_SIZE
-
521 EXT2_MIN_CLUSTER_LOG_SIZE
));
522 check_super_value(ctx
, "clusters_per_group", sb
->s_clusters_per_group
,
523 MIN_CHECK
| MAX_CHECK
, 8, cpg_max
);
524 check_super_value(ctx
, "blocks_per_group", sb
->s_blocks_per_group
,
525 MIN_CHECK
| MAX_CHECK
, 8, bpg_max
);
526 check_super_value(ctx
, "inodes_per_group", sb
->s_inodes_per_group
,
527 MIN_CHECK
| MAX_CHECK
, inodes_per_block
, ipg_max
);
528 check_super_value(ctx
, "r_blocks_count", ext2fs_r_blocks_count(sb
),
529 MAX_CHECK
, 0, ext2fs_blocks_count(sb
) / 2);
530 check_super_value(ctx
, "reserved_gdt_blocks",
531 sb
->s_reserved_gdt_blocks
, MAX_CHECK
, 0,
532 fs
->blocksize
/ sizeof(__u32
));
533 check_super_value(ctx
, "desc_size",
534 sb
->s_desc_size
, MAX_CHECK
| LOG2_CHECK
, 0,
536 if (sb
->s_rev_level
> EXT2_GOOD_OLD_REV
)
537 check_super_value(ctx
, "first_ino", sb
->s_first_ino
,
538 MIN_CHECK
| MAX_CHECK
,
539 EXT2_GOOD_OLD_FIRST_INO
, sb
->s_inodes_count
);
540 inode_size
= EXT2_INODE_SIZE(sb
);
541 check_super_value(ctx
, "inode_size",
542 inode_size
, MIN_CHECK
| MAX_CHECK
| LOG2_CHECK
,
543 EXT2_GOOD_OLD_INODE_SIZE
, fs
->blocksize
);
544 if (sb
->s_blocks_per_group
!= (sb
->s_clusters_per_group
*
545 EXT2FS_CLUSTER_RATIO(fs
))) {
546 pctx
.num
= sb
->s_clusters_per_group
* EXT2FS_CLUSTER_RATIO(fs
);
547 pctx
.str
= "block_size";
548 fix_problem(ctx
, PR_0_MISC_CORRUPT_SUPER
, &pctx
);
549 ctx
->flags
|= E2F_FLAG_ABORT
; /* never get here! */
553 if ((ctx
->flags
& E2F_FLAG_GOT_DEVSIZE
) &&
554 (ctx
->num_blocks
< ext2fs_blocks_count(sb
))) {
555 pctx
.blk
= ext2fs_blocks_count(sb
);
556 pctx
.blk2
= ctx
->num_blocks
;
557 if (fix_problem(ctx
, PR_0_FS_SIZE_WRONG
, &pctx
)) {
558 ctx
->flags
|= E2F_FLAG_ABORT
;
563 should_be
= (sb
->s_log_block_size
== 0 &&
564 EXT2FS_CLUSTER_RATIO(fs
) == 1) ? 1 : 0;
565 if (sb
->s_first_data_block
!= should_be
) {
566 pctx
.blk
= sb
->s_first_data_block
;
567 pctx
.blk2
= should_be
;
568 fix_problem(ctx
, PR_0_FIRST_DATA_BLOCK
, &pctx
);
569 ctx
->flags
|= E2F_FLAG_ABORT
;
573 should_be
= (blk64_t
)sb
->s_inodes_per_group
* fs
->group_desc_count
;
574 if (should_be
> UINT_MAX
)
575 should_be
= UINT_MAX
;
576 if (sb
->s_inodes_count
!= should_be
) {
577 pctx
.ino
= sb
->s_inodes_count
;
578 pctx
.ino2
= should_be
;
579 if (fix_problem(ctx
, PR_0_INODE_COUNT_WRONG
, &pctx
)) {
580 sb
->s_inodes_count
= should_be
;
581 ext2fs_mark_super_dirty(fs
);
585 /* Are metadata_csum and uninit_bg both set? */
586 if (EXT2_HAS_RO_COMPAT_FEATURE(fs
->super
,
587 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) &&
588 EXT2_HAS_RO_COMPAT_FEATURE(fs
->super
,
589 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
) &&
590 fix_problem(ctx
, PR_0_META_AND_GDT_CSUM_SET
, &pctx
)) {
591 fs
->super
->s_feature_ro_compat
&=
592 ~EXT4_FEATURE_RO_COMPAT_GDT_CSUM
;
593 ext2fs_mark_super_dirty(fs
);
594 for (i
= 0; i
< fs
->group_desc_count
; i
++)
595 ext2fs_group_desc_csum_set(fs
, i
);
598 /* Is 64bit set and extents unset? */
599 if (EXT2_HAS_INCOMPAT_FEATURE(fs
->super
,
600 EXT4_FEATURE_INCOMPAT_64BIT
) &&
601 !EXT2_HAS_INCOMPAT_FEATURE(fs
->super
,
602 EXT3_FEATURE_INCOMPAT_EXTENTS
) &&
603 fix_problem(ctx
, PR_0_64BIT_WITHOUT_EXTENTS
, &pctx
)) {
604 fs
->super
->s_feature_incompat
|=
605 EXT3_FEATURE_INCOMPAT_EXTENTS
;
606 ext2fs_mark_super_dirty(fs
);
609 if ((fs
->super
->s_feature_incompat
& EXT2_FEATURE_INCOMPAT_META_BG
) &&
610 (fs
->super
->s_first_meta_bg
> fs
->desc_blocks
)) {
611 pctx
.group
= fs
->desc_blocks
;
612 pctx
.num
= fs
->super
->s_first_meta_bg
;
613 if (fix_problem(ctx
, PR_0_FIRST_META_BG_TOO_BIG
, &pctx
)) {
614 fs
->super
->s_feature_incompat
&=
615 ~EXT2_FEATURE_INCOMPAT_META_BG
;
616 fs
->super
->s_first_meta_bg
= 0;
617 ext2fs_mark_super_dirty(fs
);
622 * Verify the group descriptors....
624 first_block
= sb
->s_first_data_block
;
625 last_block
= ext2fs_blocks_count(sb
)-1;
627 csum_flag
= ext2fs_has_group_desc_csum(fs
);
628 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
631 if (!EXT2_HAS_INCOMPAT_FEATURE(fs
->super
,
632 EXT4_FEATURE_INCOMPAT_FLEX_BG
)) {
633 first_block
= ext2fs_group_first_block2(fs
, i
);
634 last_block
= ext2fs_group_last_block2(fs
, i
);
637 if ((ext2fs_block_bitmap_loc(fs
, i
) < first_block
) ||
638 (ext2fs_block_bitmap_loc(fs
, i
) > last_block
)) {
639 pctx
.blk
= ext2fs_block_bitmap_loc(fs
, i
);
640 if (fix_problem(ctx
, PR_0_BB_NOT_GROUP
, &pctx
))
641 ext2fs_block_bitmap_loc_set(fs
, i
, 0);
643 if (ext2fs_block_bitmap_loc(fs
, i
) == 0) {
644 ctx
->invalid_block_bitmap_flag
[i
]++;
645 ctx
->invalid_bitmaps
++;
647 if ((ext2fs_inode_bitmap_loc(fs
, i
) < first_block
) ||
648 (ext2fs_inode_bitmap_loc(fs
, i
) > last_block
)) {
649 pctx
.blk
= ext2fs_inode_bitmap_loc(fs
, i
);
650 if (fix_problem(ctx
, PR_0_IB_NOT_GROUP
, &pctx
))
651 ext2fs_inode_bitmap_loc_set(fs
, i
, 0);
653 if (ext2fs_inode_bitmap_loc(fs
, i
) == 0) {
654 ctx
->invalid_inode_bitmap_flag
[i
]++;
655 ctx
->invalid_bitmaps
++;
657 if ((ext2fs_inode_table_loc(fs
, i
) < first_block
) ||
658 ((ext2fs_inode_table_loc(fs
, i
) +
659 fs
->inode_blocks_per_group
- 1) > last_block
)) {
660 pctx
.blk
= ext2fs_inode_table_loc(fs
, i
);
661 if (fix_problem(ctx
, PR_0_ITABLE_NOT_GROUP
, &pctx
))
662 ext2fs_inode_table_loc_set(fs
, i
, 0);
664 if (ext2fs_inode_table_loc(fs
, i
) == 0) {
665 ctx
->invalid_inode_table_flag
[i
]++;
666 ctx
->invalid_bitmaps
++;
668 free_blocks
+= ext2fs_bg_free_blocks_count(fs
, i
);
669 free_inodes
+= ext2fs_bg_free_inodes_count(fs
, i
);
671 if ((ext2fs_bg_free_blocks_count(fs
, i
) > sb
->s_blocks_per_group
) ||
672 (ext2fs_bg_free_inodes_count(fs
, i
) > sb
->s_inodes_per_group
) ||
673 (ext2fs_bg_used_dirs_count(fs
, i
) > sb
->s_inodes_per_group
))
674 ext2fs_unmark_valid(fs
);
677 if (!ext2fs_group_desc_csum_verify(fs
, i
)) {
678 pctx
.csum1
= ext2fs_bg_checksum(fs
, i
);
679 pctx
.csum2
= ext2fs_group_desc_csum(fs
, i
);
680 if (fix_problem(ctx
, PR_0_GDT_CSUM
, &pctx
)) {
681 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_BLOCK_UNINIT
);
682 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_INODE_UNINIT
);
683 ext2fs_bg_itable_unused_set(fs
, i
, 0);
686 ext2fs_unmark_valid(fs
);
690 (ext2fs_bg_flags_test(fs
, i
, EXT2_BG_BLOCK_UNINIT
) ||
691 ext2fs_bg_flags_test(fs
, i
, EXT2_BG_INODE_UNINIT
) ||
692 ext2fs_bg_itable_unused(fs
, i
) != 0)) {
693 if (fix_problem(ctx
, PR_0_GDT_UNINIT
, &pctx
)) {
694 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_BLOCK_UNINIT
);
695 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_INODE_UNINIT
);
696 ext2fs_bg_itable_unused_set(fs
, i
, 0);
699 ext2fs_unmark_valid(fs
);
702 if (i
== fs
->group_desc_count
- 1 &&
703 ext2fs_bg_flags_test(fs
, i
, EXT2_BG_BLOCK_UNINIT
)) {
704 if (fix_problem(ctx
, PR_0_BB_UNINIT_LAST
, &pctx
)) {
705 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_BLOCK_UNINIT
);
708 ext2fs_unmark_valid(fs
);
712 (ext2fs_bg_itable_unused(fs
, i
) > ext2fs_bg_free_inodes_count(fs
, i
) ||
713 ext2fs_bg_itable_unused(fs
, i
) > sb
->s_inodes_per_group
)) {
714 pctx
.blk
= ext2fs_bg_itable_unused(fs
, i
);
715 if (fix_problem(ctx
, PR_0_GDT_ITABLE_UNUSED
, &pctx
)) {
716 ext2fs_bg_itable_unused_set(fs
, i
, 0);
719 ext2fs_unmark_valid(fs
);
723 ext2fs_group_desc_csum_set(fs
, i
);
724 /* If the user aborts e2fsck by typing ^C, stop right away */
725 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
729 ctx
->free_blocks
= EXT2FS_C2B(fs
, free_blocks
);
730 ctx
->free_inodes
= free_inodes
;
732 if ((ext2fs_free_blocks_count(sb
) > ext2fs_blocks_count(sb
)) ||
733 (sb
->s_free_inodes_count
> sb
->s_inodes_count
))
734 ext2fs_unmark_valid(fs
);
738 * If we have invalid bitmaps, set the error state of the
741 if (ctx
->invalid_bitmaps
&& !(ctx
->options
& E2F_OPT_READONLY
)) {
742 sb
->s_state
&= ~EXT2_VALID_FS
;
743 ext2fs_mark_super_dirty(fs
);
746 clear_problem_context(&pctx
);
748 #ifndef EXT2_SKIP_UUID
750 * If the UUID field isn't assigned, assign it.
751 * Skip if checksums are enabled and the filesystem is mounted,
752 * if the id changes under the kernel remounting rw may fail.
754 if (!(ctx
->options
& E2F_OPT_READONLY
) && uuid_is_null(sb
->s_uuid
) &&
755 (!csum_flag
|| !(ctx
->mount_flags
& EXT2_MF_MOUNTED
))) {
756 if (fix_problem(ctx
, PR_0_ADD_UUID
, &pctx
)) {
757 uuid_generate(sb
->s_uuid
);
758 ext2fs_init_csum_seed(fs
);
759 fs
->flags
|= EXT2_FLAG_DIRTY
;
760 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
766 * Check to see if we should disable the test_fs flag
768 profile_get_boolean(ctx
->profile
, "options",
769 "clear_test_fs_flag", 0, 1,
770 &clear_test_fs_flag
);
771 if (!(ctx
->options
& E2F_OPT_READONLY
) &&
772 clear_test_fs_flag
&&
773 (fs
->super
->s_flags
& EXT2_FLAGS_TEST_FILESYS
) &&
774 (fs_proc_check("ext4") || check_for_modules("ext4"))) {
775 if (fix_problem(ctx
, PR_0_CLEAR_TESTFS_FLAG
, &pctx
)) {
776 fs
->super
->s_flags
&= ~EXT2_FLAGS_TEST_FILESYS
;
777 fs
->flags
|= EXT2_FLAG_DIRTY
;
778 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
783 * For the Hurd, check to see if the filetype option is set,
784 * since it doesn't support it.
786 if (!(ctx
->options
& E2F_OPT_READONLY
) &&
787 fs
->super
->s_creator_os
== EXT2_OS_HURD
&&
788 (fs
->super
->s_feature_incompat
&
789 EXT2_FEATURE_INCOMPAT_FILETYPE
)) {
790 if (fix_problem(ctx
, PR_0_HURD_CLEAR_FILETYPE
, &pctx
)) {
791 fs
->super
->s_feature_incompat
&=
792 ~EXT2_FEATURE_INCOMPAT_FILETYPE
;
793 ext2fs_mark_super_dirty(fs
);
794 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
799 * If we have any of the compatibility flags set, we need to have a
800 * revision 1 filesystem. Most kernels will not check the flags on
801 * a rev 0 filesystem and we may have corruption issues because of
802 * the incompatible changes to the filesystem.
804 if (!(ctx
->options
& E2F_OPT_READONLY
) &&
805 fs
->super
->s_rev_level
== EXT2_GOOD_OLD_REV
&&
806 (fs
->super
->s_feature_compat
||
807 fs
->super
->s_feature_ro_compat
||
808 fs
->super
->s_feature_incompat
) &&
809 fix_problem(ctx
, PR_0_FS_REV_LEVEL
, &pctx
)) {
810 ext2fs_update_dynamic_rev(fs
);
811 ext2fs_mark_super_dirty(fs
);
812 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
816 * Clean up any orphan inodes, if present.
818 if (!(ctx
->options
& E2F_OPT_READONLY
) && release_orphan_inodes(ctx
)) {
819 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
820 ext2fs_mark_super_dirty(fs
);
824 * Unfortunately, due to Windows' unfortunate design decision
825 * to configure the hardware clock to tick localtime, instead
826 * of the more proper and less error-prone UTC time, many
827 * users end up in the situation where the system clock is
828 * incorrectly set at the time when e2fsck is run.
830 * Historically this was usually due to some distributions
831 * having buggy init scripts and/or installers that didn't
832 * correctly detect this case and take appropriate
833 * countermeasures. However, it's still possible, despite the
834 * best efforts of init script and installer authors to not be
835 * able to detect this misconfiguration, usually due to a
836 * buggy or misconfigured virtualization manager or the
837 * installer not having access to a network time server during
838 * the installation process. So by default, we allow the
839 * superblock times to be fudged by up to 24 hours. This can
840 * be disabled by setting options.accept_time_fudge to the
841 * boolean value of false in e2fsck.conf. We also support
842 * options.buggy_init_scripts for backwards compatibility.
844 profile_get_boolean(ctx
->profile
, "options", "accept_time_fudge",
845 0, 1, &accept_time_fudge
);
846 profile_get_boolean(ctx
->profile
, "options", "buggy_init_scripts",
847 0, accept_time_fudge
, &accept_time_fudge
);
848 ctx
->time_fudge
= accept_time_fudge
? 86400 : 0;
850 profile_get_boolean(ctx
->profile
, "options", "broken_system_clock",
851 0, 0, &broken_system_clock
);
854 * Check to see if the superblock last mount time or last
855 * write time is in the future.
857 if (!broken_system_clock
&&
858 !(ctx
->flags
& E2F_FLAG_TIME_INSANE
) &&
859 fs
->super
->s_mtime
> (__u32
) ctx
->now
) {
860 pctx
.num
= fs
->super
->s_mtime
;
861 problem
= PR_0_FUTURE_SB_LAST_MOUNT
;
862 if (fs
->super
->s_mtime
<= (__u32
) ctx
->now
+ ctx
->time_fudge
)
863 problem
= PR_0_FUTURE_SB_LAST_MOUNT_FUDGED
;
864 if (fix_problem(ctx
, problem
, &pctx
)) {
865 fs
->super
->s_mtime
= ctx
->now
;
866 fs
->flags
|= EXT2_FLAG_DIRTY
;
869 if (!broken_system_clock
&&
870 !(ctx
->flags
& E2F_FLAG_TIME_INSANE
) &&
871 fs
->super
->s_wtime
> (__u32
) ctx
->now
) {
872 pctx
.num
= fs
->super
->s_wtime
;
873 problem
= PR_0_FUTURE_SB_LAST_WRITE
;
874 if (fs
->super
->s_wtime
<= (__u32
) ctx
->now
+ ctx
->time_fudge
)
875 problem
= PR_0_FUTURE_SB_LAST_WRITE_FUDGED
;
876 if (fix_problem(ctx
, problem
, &pctx
)) {
877 fs
->super
->s_wtime
= ctx
->now
;
878 fs
->flags
|= EXT2_FLAG_DIRTY
;
883 * Move the ext3 journal file, if necessary.
885 e2fsck_move_ext3_journal(ctx
);
888 * Fix journal hint, if necessary
890 e2fsck_fix_ext3_journal_hint(ctx
);
893 * Add dirhash hint if necessary
895 e2fsck_fix_dirhash_hint(ctx
);
898 * Hide quota inodes if necessary.
900 e2fsck_hide_quota(ctx
);
906 * Check to see if we should backup the master sb to the backup super
907 * blocks. Returns non-zero if the sb should be backed up.
911 * A few flags are set on the fly by the kernel, but only in the
912 * primary superblock. This is actually a bad thing, and we should
913 * try to discourage it in the future. In particular, for the newer
914 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
915 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
916 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
917 * copying the primary superblock during online resize.
919 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
920 * unfortunately, we shouldn't ignore it since if it's not set in the
921 * backup, the extended attributes in the filesystem will be stripped
924 #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
925 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
926 #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \
927 EXT3_FEATURE_INCOMPAT_RECOVER)
929 int check_backup_super_block(e2fsck_t ctx
)
931 ext2_filsys fs
= ctx
->fs
;
936 char buf
[SUPERBLOCK_SIZE
];
937 struct ext2_super_block
*backup_sb
;
940 * If we are already writing out the backup blocks, then we
941 * don't need to test. Also, if the filesystem is invalid, or
942 * the check was aborted or cancelled, we also don't want to
943 * do the backup. If the filesystem was opened read-only then
944 * we can't do the backup.
946 if (((fs
->flags
& EXT2_FLAG_MASTER_SB_ONLY
) == 0) ||
947 !ext2fs_test_valid(fs
) ||
948 (fs
->super
->s_state
& EXT2_ERROR_FS
) ||
949 (ctx
->flags
& (E2F_FLAG_ABORT
| E2F_FLAG_CANCEL
)) ||
950 (ctx
->options
& E2F_OPT_READONLY
))
953 for (g
= 1; g
< fs
->group_desc_count
; g
++) {
954 if (!ext2fs_bg_has_super(fs
, g
))
957 sb
= ext2fs_group_first_block2(fs
, g
);
959 retval
= io_channel_read_blk(fs
->io
, sb
, -SUPERBLOCK_SIZE
,
963 backup_sb
= (struct ext2_super_block
*) buf
;
964 #ifdef WORDS_BIGENDIAN
965 ext2fs_swap_super(backup_sb
);
967 if ((backup_sb
->s_magic
!= EXT2_SUPER_MAGIC
) ||
968 (backup_sb
->s_rev_level
> EXT2_LIB_CURRENT_REV
) ||
969 ((backup_sb
->s_log_block_size
+ EXT2_MIN_BLOCK_LOG_SIZE
) >
970 EXT2_MAX_BLOCK_LOG_SIZE
) ||
971 (EXT2_INODE_SIZE(backup_sb
) < EXT2_GOOD_OLD_INODE_SIZE
))
974 #define SUPER_INCOMPAT_DIFFERENT(x) \
975 ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
976 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
977 #define SUPER_RO_COMPAT_DIFFERENT(x) \
978 ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
979 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
980 #define SUPER_DIFFERENT(x) \
981 (fs->super->x != backup_sb->x)
983 if (SUPER_DIFFERENT(s_feature_compat
) ||
984 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat
) ||
985 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat
) ||
986 SUPER_DIFFERENT(s_blocks_count
) ||
987 SUPER_DIFFERENT(s_blocks_count_hi
) ||
988 SUPER_DIFFERENT(s_inodes_count
) ||
989 memcmp(fs
->super
->s_uuid
, backup_sb
->s_uuid
,
990 sizeof(fs
->super
->s_uuid
)))