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! */
44 static void check_super_value64(e2fsck_t ctx, const char *descr,
45 __u64 value, int flags,
46 __u64 min_val, __u64 max_val)
48 struct problem_context pctx;
50 if ((flags & MIN_CHECK && value < min_val) ||
51 (flags & MAX_CHECK && value > max_val) ||
52 (flags & LOG2_CHECK && (value & (value - 1)) != 0)) {
53 clear_problem_context(&pctx);
56 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
57 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
62 * helper function to release an inode
64 struct process_block_struct {
67 struct problem_context *pctx;
70 e2_blkcnt_t truncate_block;
75 struct ext2_inode_large *inode;
78 static int release_inode_block(ext2_filsys fs,
81 blk64_t ref_blk EXT2FS_ATTR((unused)),
82 int ref_offset EXT2FS_ATTR((unused)),
85 struct process_block_struct *pb;
87 struct problem_context *pctx;
88 blk64_t blk = *block_nr;
89 blk64_t cluster = EXT2FS_B2C(fs, *block_nr);
92 pb = (struct process_block_struct *) priv_data;
97 pctx->blkcount = blockcnt;
102 if (pb->last_cluster == cluster)
105 pb->last_cluster = cluster;
107 if ((blk < fs->super->s_first_data_block) ||
108 (blk >= ext2fs_blocks_count(fs->super))) {
109 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
115 if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) {
116 fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
121 * If we are deleting an orphan, then we leave the fields alone.
122 * If we are truncating an orphan, then update the inode fields
123 * and clean up any partial block data.
125 if (pb->truncating) {
127 * We only remove indirect blocks if they are
134 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
139 limit = fs->blocksize >> 2;
140 for (i = 0, bp = (blk_t *) pb->buf;
141 i < limit; i++, bp++)
146 * We don't remove direct blocks until we've reached
147 * the truncation block.
149 if (blockcnt >= 0 && blockcnt < pb->truncate_block)
152 * If part of the last block needs truncating, we do
155 if ((blockcnt == pb->truncate_block) && pb->truncate_offset) {
156 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
160 memset(pb->buf + pb->truncate_offset, 0,
161 fs->blocksize - pb->truncate_offset);
162 pb->errcode = io_channel_write_blk64(fs->io, blk, 1,
167 pb->truncated_blocks++;
169 retval |= BLOCK_CHANGED;
173 quota_data_sub(ctx->qctx, pb->inode, 0, ctx->fs->blocksize);
174 ext2fs_block_alloc_stats2(fs, blk, -1);
180 * This function releases an inode. Returns 1 if an inconsistency was
181 * found. If the inode has a link count, then it is being truncated and
184 static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
185 struct ext2_inode_large *inode, char *block_buf,
186 struct problem_context *pctx)
188 struct process_block_struct pb;
189 ext2_filsys fs = ctx->fs;
194 if (!ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(inode)))
197 pb.buf = block_buf + 3 * ctx->fs->blocksize;
204 if (inode->i_links_count) {
206 pb.truncate_block = (e2_blkcnt_t)
207 ((EXT2_I_SIZE(inode) + fs->blocksize - 1) /
209 pb.truncate_offset = inode->i_size % fs->blocksize;
212 pb.truncate_block = 0;
213 pb.truncate_offset = 0;
215 pb.truncated_blocks = 0;
216 retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
217 block_buf, release_inode_block, &pb);
219 com_err("release_inode_blocks", retval,
220 _("while calling ext2fs_block_iterate for inode %u"),
227 /* Refresh the inode since ext2fs_block_iterate may have changed it */
228 e2fsck_read_inode_full(ctx, ino, EXT2_INODE(inode), sizeof(*inode),
229 "release_inode_blocks");
231 if (pb.truncated_blocks)
232 ext2fs_iblk_sub_blocks(fs, EXT2_INODE(inode),
233 pb.truncated_blocks);
235 blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
237 retval = ext2fs_adjust_ea_refcount3(fs, blk, block_buf, -1,
239 if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
244 com_err("release_inode_blocks", retval,
245 _("while calling ext2fs_adjust_ea_refcount2 for inode %u"),
251 quota_data_sub(ctx->qctx, inode, 0,
253 ext2fs_block_alloc_stats2(fs, blk, -1);
256 ext2fs_file_acl_block_set(fs, EXT2_INODE(inode), 0);
261 /* Load all quota data in preparation for orphan clearing. */
262 static errcode_t e2fsck_read_all_quotas(e2fsck_t ctx)
265 enum quota_type qtype;
266 errcode_t retval = 0;
268 if (!ext2fs_has_feature_quota(ctx->fs->super))
271 retval = quota_init_context(&ctx->qctx, ctx->fs, 0);
275 for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
276 qf_ino = *quota_sb_inump(ctx->fs->super, qtype);
280 retval = quota_update_limits(ctx->qctx, qf_ino, qtype);
285 quota_release_context(&ctx->qctx);
289 /* Write all the quota info to disk. */
290 static errcode_t e2fsck_write_all_quotas(e2fsck_t ctx)
292 struct problem_context pctx;
293 enum quota_type qtype;
295 if (!ext2fs_has_feature_quota(ctx->fs->super))
298 clear_problem_context(&pctx);
299 for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
301 pctx.errcode = quota_write_inode(ctx->qctx, 1 << qtype);
303 fix_problem(ctx, PR_6_WRITE_QUOTAS, &pctx);
308 quota_release_context(&ctx->qctx);
313 * This function releases all of the orphan inodes. It returns 1 if
314 * it hit some error, and 0 on success.
316 static int release_orphan_inodes(e2fsck_t ctx)
318 ext2_filsys fs = ctx->fs;
319 ext2_ino_t ino, next_ino;
320 struct ext2_inode_large inode;
321 struct problem_context pctx;
324 if ((ino = fs->super->s_last_orphan) == 0)
327 clear_problem_context(&pctx);
328 pctx.errcode = e2fsck_read_all_quotas(ctx);
330 fix_problem(ctx, PR_0_QUOTA_INIT_CTX, &pctx);
335 * Win or lose, we won't be using the head of the orphan inode
338 fs->super->s_last_orphan = 0;
339 ext2fs_mark_super_dirty(fs);
342 * If the filesystem contains errors, don't run the orphan
343 * list, since the orphan list can't be trusted; and we're
344 * going to be running a full e2fsck run anyway...
346 if (fs->super->s_state & EXT2_ERROR_FS) {
348 quota_release_context(&ctx->qctx);
352 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
353 (ino > fs->super->s_inodes_count)) {
354 clear_problem_context(&pctx);
356 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
360 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
361 "block iterate buffer");
362 e2fsck_read_bitmaps(ctx);
365 e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&inode),
366 sizeof(inode), "release_orphan_inodes");
367 clear_problem_context(&pctx);
369 pctx.inode = EXT2_INODE(&inode);
370 pctx.str = inode.i_links_count ? _("Truncating") :
373 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
375 next_ino = inode.i_dtime;
377 ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
378 (next_ino > fs->super->s_inodes_count))) {
380 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
384 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
387 if (!inode.i_links_count) {
389 quota_data_inodes(ctx->qctx, &inode, ino, -1);
390 ext2fs_inode_alloc_stats2(fs, ino, -1,
391 LINUX_S_ISDIR(inode.i_mode));
393 inode.i_dtime = ctx->now;
397 e2fsck_write_inode_full(ctx, ino, EXT2_INODE(&inode),
398 sizeof(inode), "delete_file");
401 ext2fs_free_mem(&block_buf);
402 pctx.errcode = e2fsck_write_all_quotas(ctx);
407 ext2fs_free_mem(&block_buf);
410 quota_release_context(&ctx->qctx);
416 * Check the resize inode to make sure it is sane. We check both for
417 * the case where on-line resizing is not enabled (in which case the
418 * resize inode should be cleared) as well as the case where on-line
419 * resizing is enabled.
421 void check_resize_inode(e2fsck_t ctx)
423 ext2_filsys fs = ctx->fs;
424 struct ext2_inode inode;
425 struct problem_context pctx;
426 int i, gdt_off, ind_off;
429 blk_t expect; /* for resize inode, which is 32-bit only */
430 __u32 *dind_buf = 0, *ind_buf;
433 clear_problem_context(&pctx);
436 * If the resize inode feature isn't set, then
437 * s_reserved_gdt_blocks must be zero.
439 if (!ext2fs_has_feature_resize_inode(fs->super)) {
440 if (fs->super->s_reserved_gdt_blocks) {
441 pctx.num = fs->super->s_reserved_gdt_blocks;
442 if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
444 fs->super->s_reserved_gdt_blocks = 0;
445 ext2fs_mark_super_dirty(fs);
450 /* Read the resize inode */
451 pctx.ino = EXT2_RESIZE_INO;
452 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
454 if (ext2fs_has_feature_resize_inode(fs->super))
455 ctx->flags |= E2F_FLAG_RESIZE_INODE;
460 * If the resize inode feature isn't set, check to make sure
461 * the resize inode is cleared; then we're done.
463 if (!ext2fs_has_feature_resize_inode(fs->super)) {
464 for (i=0; i < EXT2_N_BLOCKS; i++) {
465 if (inode.i_block[i])
468 if ((i < EXT2_N_BLOCKS) &&
469 fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
470 memset(&inode, 0, sizeof(inode));
471 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
478 * The resize inode feature is enabled; check to make sure the
479 * only block in use is the double indirect block
481 blk = inode.i_block[EXT2_DIND_BLOCK];
482 for (i=0; i < EXT2_N_BLOCKS; i++) {
483 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
486 if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
487 !(inode.i_mode & LINUX_S_IFREG) ||
488 (blk < fs->super->s_first_data_block ||
489 blk >= ext2fs_blocks_count(fs->super))) {
490 resize_inode_invalid:
491 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
492 memset(&inode, 0, sizeof(inode));
493 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
495 ctx->flags |= E2F_FLAG_RESIZE_INODE;
497 if (!(ctx->options & E2F_OPT_READONLY)) {
498 fs->super->s_state &= ~EXT2_VALID_FS;
499 ext2fs_mark_super_dirty(fs);
503 dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
504 "resize dind buffer");
505 ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
507 retval = ext2fs_read_ind_block(fs, blk, dind_buf);
509 goto resize_inode_invalid;
511 gdt_off = fs->desc_blocks;
512 pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
513 if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0)
514 pblk++; /* Deal with 1024 blocksize bigalloc fs */
515 for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
516 i++, gdt_off++, pblk++) {
517 gdt_off %= fs->blocksize/4;
518 if (dind_buf[gdt_off] != pblk)
519 goto resize_inode_invalid;
520 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
522 goto resize_inode_invalid;
524 for (j = 1; j < fs->group_desc_count; j++) {
525 if (!ext2fs_bg_has_super(fs, j))
527 expect = pblk + EXT2_GROUPS_TO_BLOCKS(fs->super, j);
528 if (ind_buf[ind_off] != expect)
529 goto resize_inode_invalid;
536 ext2fs_free_mem(&dind_buf);
541 * This function checks the dirhash signed/unsigned hint if necessary.
543 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
545 struct ext2_super_block *sb = ctx->fs->super;
546 struct problem_context pctx;
549 if ((ctx->options & E2F_OPT_READONLY) ||
550 !ext2fs_has_feature_dir_index(sb) ||
551 (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
556 clear_problem_context(&pctx);
557 if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
558 if (((int) c) == -1) {
559 sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
561 sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
563 ext2fs_mark_super_dirty(ctx->fs);
568 void check_super_block(e2fsck_t ctx)
570 ext2_filsys fs = ctx->fs;
571 blk64_t first_block, last_block;
572 struct ext2_super_block *sb = fs->super;
573 unsigned int ipg_max;
575 blk64_t blocks_per_group = fs->super->s_blocks_per_group;
576 __u32 bpg_max, cpg_max;
578 int inodes_per_block;
580 int accept_time_fudge;
581 int broken_system_clock;
584 struct problem_context pctx;
585 blk64_t free_blocks = 0;
586 ino_t free_inodes = 0;
587 int csum_flag, clear_test_fs_flag;
589 inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
590 ipg_max = inodes_per_block * (blocks_per_group - 4);
591 if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
592 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
593 cpg_max = 8 * EXT2_BLOCK_SIZE(sb);
594 if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb))
595 cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb);
596 bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs);
597 if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
598 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
600 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
601 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
602 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
603 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
604 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
605 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
607 blks_max = (1ULL << 32) * EXT2_MAX_BLOCKS_PER_GROUP(fs->super);
608 if (ext2fs_has_feature_64bit(fs->super)) {
609 if (blks_max > ((1ULL << 48) - 1))
610 blks_max = (1ULL << 48) - 1;
612 if (blks_max > ((1ULL << 32) - 1))
613 blks_max = (1ULL << 32) - 1;
616 clear_problem_context(&pctx);
619 * Verify the super block constants...
621 check_super_value(ctx, "inodes_count", sb->s_inodes_count,
623 check_super_value64(ctx, "blocks_count", ext2fs_blocks_count(sb),
624 MIN_CHECK | MAX_CHECK, 1, blks_max);
625 check_super_value(ctx, "first_data_block", sb->s_first_data_block,
626 MAX_CHECK, 0, ext2fs_blocks_count(sb));
627 check_super_value(ctx, "log_block_size", sb->s_log_block_size,
628 MIN_CHECK | MAX_CHECK, 0,
629 EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
630 check_super_value(ctx, "log_cluster_size",
631 sb->s_log_cluster_size,
632 MIN_CHECK | MAX_CHECK, sb->s_log_block_size,
633 (EXT2_MAX_CLUSTER_LOG_SIZE -
634 EXT2_MIN_CLUSTER_LOG_SIZE));
635 check_super_value(ctx, "clusters_per_group", sb->s_clusters_per_group,
636 MIN_CHECK | MAX_CHECK, 8, cpg_max);
637 check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
638 MIN_CHECK | MAX_CHECK, 8, bpg_max);
639 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
640 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
641 check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb),
642 MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2);
643 check_super_value(ctx, "reserved_gdt_blocks",
644 sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
645 fs->blocksize / sizeof(__u32));
646 check_super_value(ctx, "desc_size",
647 sb->s_desc_size, MAX_CHECK | LOG2_CHECK, 0,
650 should_be = (__u64)sb->s_inodes_per_group * fs->group_desc_count;
651 if (should_be > ~0U) {
652 pctx.num = should_be;
653 fix_problem(ctx, PR_0_INODE_COUNT_BIG, &pctx);
654 ctx->flags |= E2F_FLAG_ABORT;
657 if (sb->s_inodes_count != should_be) {
658 pctx.ino = sb->s_inodes_count;
659 pctx.ino2 = should_be;
660 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
661 sb->s_inodes_count = should_be;
662 ext2fs_mark_super_dirty(fs);
665 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
666 check_super_value(ctx, "first_ino", sb->s_first_ino,
667 MIN_CHECK | MAX_CHECK,
668 EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count);
669 inode_size = EXT2_INODE_SIZE(sb);
670 check_super_value(ctx, "inode_size",
671 inode_size, MIN_CHECK | MAX_CHECK | LOG2_CHECK,
672 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
673 if (sb->s_blocks_per_group != (sb->s_clusters_per_group *
674 EXT2FS_CLUSTER_RATIO(fs))) {
675 pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs);
676 pctx.str = "block_size";
677 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
678 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
682 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
683 (ctx->num_blocks < ext2fs_blocks_count(sb))) {
684 pctx.blk = ext2fs_blocks_count(sb);
685 pctx.blk2 = ctx->num_blocks;
686 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
687 ctx->flags |= E2F_FLAG_ABORT;
692 should_be = (sb->s_log_block_size == 0 &&
693 EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0;
694 if (sb->s_first_data_block != should_be) {
695 pctx.blk = sb->s_first_data_block;
696 pctx.blk2 = should_be;
697 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
698 ctx->flags |= E2F_FLAG_ABORT;
702 if (EXT2_INODE_SIZE(sb) > EXT2_GOOD_OLD_INODE_SIZE) {
704 sizeof(((struct ext2_inode_large *) 0)->i_extra_isize) +
705 sizeof(((struct ext2_inode_large *) 0)->i_checksum_hi);
706 unsigned max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
707 pctx.num = sb->s_min_extra_isize;
708 if (sb->s_min_extra_isize &&
709 (sb->s_min_extra_isize < min ||
710 sb->s_min_extra_isize > max ||
711 sb->s_min_extra_isize & 3) &&
712 fix_problem(ctx, PR_0_BAD_MIN_EXTRA_ISIZE, &pctx)) {
713 sb->s_min_extra_isize =
714 (sizeof(struct ext2_inode_large) -
715 EXT2_GOOD_OLD_INODE_SIZE);
716 ext2fs_mark_super_dirty(fs);
718 pctx.num = sb->s_want_extra_isize;
719 if (sb->s_want_extra_isize &&
720 (sb->s_want_extra_isize < min ||
721 sb->s_want_extra_isize > max ||
722 sb->s_want_extra_isize & 3) &&
723 fix_problem(ctx, PR_0_BAD_WANT_EXTRA_ISIZE, &pctx)) {
724 sb->s_want_extra_isize =
725 (sizeof(struct ext2_inode_large) -
726 EXT2_GOOD_OLD_INODE_SIZE);
727 ext2fs_mark_super_dirty(fs);
731 /* Are metadata_csum and uninit_bg both set? */
732 if (ext2fs_has_feature_metadata_csum(fs->super) &&
733 ext2fs_has_feature_gdt_csum(fs->super) &&
734 fix_problem(ctx, PR_0_META_AND_GDT_CSUM_SET, &pctx)) {
735 ext2fs_clear_feature_gdt_csum(fs->super);
736 ext2fs_mark_super_dirty(fs);
737 for (i = 0; i < fs->group_desc_count; i++)
738 ext2fs_group_desc_csum_set(fs, i);
741 /* We can't have ^metadata_csum,metadata_csum_seed */
742 if (!ext2fs_has_feature_metadata_csum(fs->super) &&
743 ext2fs_has_feature_csum_seed(fs->super) &&
744 fix_problem(ctx, PR_0_CSUM_SEED_WITHOUT_META_CSUM, &pctx)) {
745 ext2fs_clear_feature_csum_seed(fs->super);
746 fs->super->s_checksum_seed = 0;
747 ext2fs_mark_super_dirty(fs);
750 /* Is 64bit set and extents unset? */
751 if (ext2fs_has_feature_64bit(fs->super) &&
752 !ext2fs_has_feature_extents(fs->super) &&
753 fix_problem(ctx, PR_0_64BIT_WITHOUT_EXTENTS, &pctx)) {
754 ext2fs_set_feature_extents(fs->super);
755 ext2fs_mark_super_dirty(fs);
758 /* Did user ask us to convert files to extents? */
759 if (ctx->options & E2F_OPT_CONVERT_BMAP) {
760 ext2fs_set_feature_extents(fs->super);
761 ext2fs_mark_super_dirty(fs);
764 if (ext2fs_has_feature_meta_bg(fs->super) &&
765 (fs->super->s_first_meta_bg > fs->desc_blocks)) {
766 pctx.group = fs->desc_blocks;
767 pctx.num = fs->super->s_first_meta_bg;
768 if (fix_problem(ctx, PR_0_FIRST_META_BG_TOO_BIG, &pctx)) {
769 ext2fs_clear_feature_meta_bg(fs->super);
770 fs->super->s_first_meta_bg = 0;
771 ext2fs_mark_super_dirty(fs);
776 * Verify the group descriptors....
778 first_block = sb->s_first_data_block;
779 last_block = ext2fs_blocks_count(sb)-1;
781 csum_flag = ext2fs_has_group_desc_csum(fs);
782 for (i = 0; i < fs->group_desc_count; i++) {
785 if (!ext2fs_has_feature_flex_bg(fs->super)) {
786 first_block = ext2fs_group_first_block2(fs, i);
787 last_block = ext2fs_group_last_block2(fs, i);
790 if ((ext2fs_block_bitmap_loc(fs, i) < first_block) ||
791 (ext2fs_block_bitmap_loc(fs, i) > last_block)) {
792 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
793 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
794 ext2fs_block_bitmap_loc_set(fs, i, 0);
796 if (ext2fs_block_bitmap_loc(fs, i) == 0) {
797 ctx->invalid_block_bitmap_flag[i]++;
798 ctx->invalid_bitmaps++;
800 if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) ||
801 (ext2fs_inode_bitmap_loc(fs, i) > last_block)) {
802 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
803 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
804 ext2fs_inode_bitmap_loc_set(fs, i, 0);
806 if (ext2fs_inode_bitmap_loc(fs, i) == 0) {
807 ctx->invalid_inode_bitmap_flag[i]++;
808 ctx->invalid_bitmaps++;
810 if ((ext2fs_inode_table_loc(fs, i) < first_block) ||
811 ((ext2fs_inode_table_loc(fs, i) +
812 fs->inode_blocks_per_group - 1) > last_block)) {
813 pctx.blk = ext2fs_inode_table_loc(fs, i);
814 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
815 ext2fs_inode_table_loc_set(fs, i, 0);
817 if (ext2fs_inode_table_loc(fs, i) == 0) {
818 ctx->invalid_inode_table_flag[i]++;
819 ctx->invalid_bitmaps++;
821 free_blocks += ext2fs_bg_free_blocks_count(fs, i);
822 free_inodes += ext2fs_bg_free_inodes_count(fs, i);
824 if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) ||
825 (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) ||
826 (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group))
827 ext2fs_unmark_valid(fs);
830 if (!ext2fs_group_desc_csum_verify(fs, i)) {
831 pctx.csum1 = ext2fs_bg_checksum(fs, i);
832 pctx.csum2 = ext2fs_group_desc_csum(fs, i);
833 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
834 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
835 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
836 ext2fs_bg_itable_unused_set(fs, i, 0);
839 ext2fs_unmark_valid(fs);
843 (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ||
844 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ||
845 ext2fs_bg_itable_unused(fs, i) != 0)) {
846 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
847 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
848 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
849 ext2fs_bg_itable_unused_set(fs, i, 0);
852 ext2fs_unmark_valid(fs);
855 if (i == fs->group_desc_count - 1 &&
856 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) {
857 if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
858 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
861 ext2fs_unmark_valid(fs);
865 (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) ||
866 ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) {
867 pctx.blk = ext2fs_bg_itable_unused(fs, i);
868 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
869 ext2fs_bg_itable_unused_set(fs, i, 0);
872 ext2fs_unmark_valid(fs);
876 ext2fs_group_desc_csum_set(fs, i);
877 /* If the user aborts e2fsck by typing ^C, stop right away */
878 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
882 ctx->free_blocks = EXT2FS_C2B(fs, free_blocks);
883 ctx->free_inodes = free_inodes;
885 if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
886 (sb->s_free_inodes_count > sb->s_inodes_count))
887 ext2fs_unmark_valid(fs);
891 * If we have invalid bitmaps, set the error state of the
894 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
895 sb->s_state &= ~EXT2_VALID_FS;
896 ext2fs_mark_super_dirty(fs);
899 clear_problem_context(&pctx);
901 #ifndef EXT2_SKIP_UUID
903 * If the UUID field isn't assigned, assign it.
904 * Skip if checksums are enabled and the filesystem is mounted,
905 * if the id changes under the kernel remounting rw may fail.
907 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid) &&
908 !ext2fs_has_feature_metadata_csum(ctx->fs->super) &&
909 (!csum_flag || !(ctx->mount_flags & EXT2_MF_MOUNTED))) {
910 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
911 uuid_generate(sb->s_uuid);
912 ext2fs_init_csum_seed(fs);
913 fs->flags |= EXT2_FLAG_DIRTY;
914 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
920 * Check to see if we should disable the test_fs flag
922 profile_get_boolean(ctx->profile, "options",
923 "clear_test_fs_flag", 0, 1,
924 &clear_test_fs_flag);
925 if (!(ctx->options & E2F_OPT_READONLY) &&
926 clear_test_fs_flag &&
927 (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) &&
928 (fs_proc_check("ext4") || check_for_modules("ext4"))) {
929 if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
930 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
931 fs->flags |= EXT2_FLAG_DIRTY;
932 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
937 * For the Hurd, check to see if the filetype option is set,
938 * since it doesn't support it.
940 if (!(ctx->options & E2F_OPT_READONLY) &&
941 fs->super->s_creator_os == EXT2_OS_HURD &&
942 ext2fs_has_feature_filetype(fs->super)) {
943 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
944 ext2fs_clear_feature_filetype(fs->super);
945 ext2fs_mark_super_dirty(fs);
946 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
951 * If we have any of the compatibility flags set, we need to have a
952 * revision 1 filesystem. Most kernels will not check the flags on
953 * a rev 0 filesystem and we may have corruption issues because of
954 * the incompatible changes to the filesystem.
956 if (!(ctx->options & E2F_OPT_READONLY) &&
957 fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
958 (fs->super->s_feature_compat ||
959 fs->super->s_feature_ro_compat ||
960 fs->super->s_feature_incompat) &&
961 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
962 ext2fs_update_dynamic_rev(fs);
963 ext2fs_mark_super_dirty(fs);
964 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
968 * Clean up any orphan inodes, if present.
970 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
971 fs->super->s_state &= ~EXT2_VALID_FS;
972 ext2fs_mark_super_dirty(fs);
976 * Unfortunately, due to Windows' unfortunate design decision
977 * to configure the hardware clock to tick localtime, instead
978 * of the more proper and less error-prone UTC time, many
979 * users end up in the situation where the system clock is
980 * incorrectly set at the time when e2fsck is run.
982 * Historically this was usually due to some distributions
983 * having buggy init scripts and/or installers that didn't
984 * correctly detect this case and take appropriate
985 * countermeasures. However, it's still possible, despite the
986 * best efforts of init script and installer authors to not be
987 * able to detect this misconfiguration, usually due to a
988 * buggy or misconfigured virtualization manager or the
989 * installer not having access to a network time server during
990 * the installation process. So by default, we allow the
991 * superblock times to be fudged by up to 24 hours. This can
992 * be disabled by setting options.accept_time_fudge to the
993 * boolean value of false in e2fsck.conf. We also support
994 * options.buggy_init_scripts for backwards compatibility.
996 profile_get_boolean(ctx->profile, "options", "accept_time_fudge",
997 0, 1, &accept_time_fudge);
998 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
999 0, accept_time_fudge, &accept_time_fudge);
1000 ctx->time_fudge = accept_time_fudge ? 86400 : 0;
1002 profile_get_boolean(ctx->profile, "options", "broken_system_clock",
1003 0, 0, &broken_system_clock);
1006 * Check to see if the superblock last mount time or last
1007 * write time is in the future.
1009 if (!broken_system_clock &&
1010 !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
1011 fs->super->s_mtime > (__u32) ctx->now) {
1012 pctx.num = fs->super->s_mtime;
1013 problem = PR_0_FUTURE_SB_LAST_MOUNT;
1014 if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
1015 problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
1016 if (fix_problem(ctx, problem, &pctx)) {
1017 fs->super->s_mtime = ctx->now;
1018 fs->flags |= EXT2_FLAG_DIRTY;
1021 if (!broken_system_clock &&
1022 !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
1023 fs->super->s_wtime > (__u32) ctx->now) {
1024 pctx.num = fs->super->s_wtime;
1025 problem = PR_0_FUTURE_SB_LAST_WRITE;
1026 if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
1027 problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
1028 if (fix_problem(ctx, problem, &pctx)) {
1029 fs->super->s_wtime = ctx->now;
1030 fs->flags |= EXT2_FLAG_DIRTY;
1034 e2fsck_validate_quota_inodes(ctx);
1037 * Move the ext3 journal file, if necessary.
1039 e2fsck_move_ext3_journal(ctx);
1042 * Fix journal hint, if necessary
1044 e2fsck_fix_ext3_journal_hint(ctx);
1047 * Add dirhash hint if necessary
1049 e2fsck_fix_dirhash_hint(ctx);
1052 * Hide quota inodes if necessary.
1054 e2fsck_hide_quota(ctx);
1060 * Check to see if we should backup the master sb to the backup super
1061 * blocks. Returns non-zero if the sb should be backed up.
1065 * A few flags are set on the fly by the kernel, but only in the
1066 * primary superblock. This is actually a bad thing, and we should
1067 * try to discourage it in the future. In particular, for the newer
1068 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
1069 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
1070 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
1071 * copying the primary superblock during online resize.
1073 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
1074 * unfortunately, we shouldn't ignore it since if it's not set in the
1075 * backup, the extended attributes in the filesystem will be stripped
1078 #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
1079 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
1080 #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \
1081 EXT3_FEATURE_INCOMPAT_RECOVER)
1083 int check_backup_super_block(e2fsck_t ctx)
1085 ext2_filsys fs = ctx->fs;
1090 char buf[SUPERBLOCK_SIZE];
1091 struct ext2_super_block *backup_sb;
1094 * If we are already writing out the backup blocks, then we
1095 * don't need to test. Also, if the filesystem is invalid, or
1096 * the check was aborted or cancelled, we also don't want to
1097 * do the backup. If the filesystem was opened read-only then
1098 * we can't do the backup.
1100 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
1101 !ext2fs_test_valid(fs) ||
1102 (fs->super->s_state & EXT2_ERROR_FS) ||
1103 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
1104 (ctx->options & E2F_OPT_READONLY))
1107 for (g = 1; g < fs->group_desc_count; g++) {
1108 if (!ext2fs_bg_has_super(fs, g))
1111 sb = ext2fs_group_first_block2(fs, g);
1113 retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE,
1117 backup_sb = (struct ext2_super_block *) buf;
1118 #ifdef WORDS_BIGENDIAN
1119 ext2fs_swap_super(backup_sb);
1121 if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) ||
1122 (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) ||
1123 ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
1124 EXT2_MAX_BLOCK_LOG_SIZE) ||
1125 (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE))
1128 #define SUPER_INCOMPAT_DIFFERENT(x) \
1129 ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
1130 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
1131 #define SUPER_RO_COMPAT_DIFFERENT(x) \
1132 ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
1133 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
1134 #define SUPER_DIFFERENT(x) \
1135 (fs->super->x != backup_sb->x)
1137 if (SUPER_DIFFERENT(s_feature_compat) ||
1138 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
1139 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
1140 SUPER_DIFFERENT(s_blocks_count) ||
1141 SUPER_DIFFERENT(s_blocks_count_hi) ||
1142 SUPER_DIFFERENT(s_inodes_count) ||
1143 memcmp(fs->super->s_uuid, backup_sb->s_uuid,
1144 sizeof(fs->super->s_uuid)))