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
16 #ifndef EXT2_SKIP_UUID
17 #include "uuid/uuid.h"
26 static void check_super_value(e2fsck_t ctx, const char *descr,
27 unsigned long value, int flags,
28 unsigned long min_val, unsigned long max_val)
30 struct problem_context pctx;
32 if (((flags & MIN_CHECK) && (value < min_val)) ||
33 ((flags & MAX_CHECK) && (value > max_val)) ||
34 ((flags & LOG2_CHECK) && (value & (value - 1) != 0))) {
35 clear_problem_context(&pctx);
38 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
39 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
44 * helper function to release an inode
46 struct process_block_struct {
49 struct problem_context *pctx;
52 e2_blkcnt_t truncate_block;
58 static int release_inode_block(ext2_filsys fs,
61 blk64_t ref_blk EXT2FS_ATTR((unused)),
62 int ref_offset EXT2FS_ATTR((unused)),
65 struct process_block_struct *pb;
67 struct problem_context *pctx;
68 blk64_t blk = *block_nr;
71 pb = (struct process_block_struct *) priv_data;
76 pctx->blkcount = blockcnt;
78 if (HOLE_BLKADDR(blk))
81 if ((blk < fs->super->s_first_data_block) ||
82 (blk >= ext2fs_blocks_count(fs->super))) {
83 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
89 if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) {
90 fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
95 * If we are deleting an orphan, then we leave the fields alone.
96 * If we are truncating an orphan, then update the inode fields
97 * and clean up any partial block data.
101 * We only remove indirect blocks if they are
108 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
113 limit = fs->blocksize >> 2;
114 for (i = 0, bp = (blk_t *) pb->buf;
115 i < limit; i++, bp++)
120 * We don't remove direct blocks until we've reached
121 * the truncation block.
123 if (blockcnt >= 0 && blockcnt < pb->truncate_block)
126 * If part of the last block needs truncating, we do
129 if ((blockcnt == pb->truncate_block) && pb->truncate_offset) {
130 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
134 memset(pb->buf + pb->truncate_offset, 0,
135 fs->blocksize - pb->truncate_offset);
136 pb->errcode = io_channel_write_blk64(fs->io, blk, 1,
141 pb->truncated_blocks++;
143 retval |= BLOCK_CHANGED;
146 ext2fs_block_alloc_stats2(fs, blk, -1);
152 * This function releases an inode. Returns 1 if an inconsistency was
153 * found. If the inode has a link count, then it is being truncated and
156 static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
157 struct ext2_inode *inode, char *block_buf,
158 struct problem_context *pctx)
160 struct process_block_struct pb;
161 ext2_filsys fs = ctx->fs;
165 if (!ext2fs_inode_has_valid_blocks2(fs, inode))
168 pb.buf = block_buf + 3 * ctx->fs->blocksize;
173 if (inode->i_links_count) {
175 pb.truncate_block = (e2_blkcnt_t)
176 ((EXT2_I_SIZE(inode) + fs->blocksize - 1) /
178 pb.truncate_offset = inode->i_size % fs->blocksize;
181 pb.truncate_block = 0;
182 pb.truncate_offset = 0;
184 pb.truncated_blocks = 0;
185 retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
186 block_buf, release_inode_block, &pb);
188 com_err("release_inode_blocks", retval,
189 _("while calling ext2fs_block_iterate for inode %d"),
196 /* Refresh the inode since ext2fs_block_iterate may have changed it */
197 e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks");
199 if (pb.truncated_blocks)
200 ext2fs_iblk_sub_blocks(fs, inode, pb.truncated_blocks);
202 if (ext2fs_file_acl_block(fs, inode)) {
203 retval = ext2fs_adjust_ea_refcount2(fs,
204 ext2fs_file_acl_block(fs, inode),
205 block_buf, -1, &count);
206 if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
211 com_err("release_inode_blocks", retval,
212 _("while calling ext2fs_adjust_ea_refcount2 for inode %d"),
217 ext2fs_block_alloc_stats2(fs,
218 ext2fs_file_acl_block(fs, inode), -1);
221 ext2fs_file_acl_block_set(fs, inode, 0);
227 * This function releases all of the orphan inodes. It returns 1 if
228 * it hit some error, and 0 on success.
230 static int release_orphan_inodes(e2fsck_t ctx)
232 ext2_filsys fs = ctx->fs;
233 ext2_ino_t ino, next_ino;
234 struct ext2_inode inode;
235 struct problem_context pctx;
238 if ((ino = fs->super->s_last_orphan) == 0)
242 * Win or lose, we won't be using the head of the orphan inode
245 fs->super->s_last_orphan = 0;
246 ext2fs_mark_super_dirty(fs);
249 * If the filesystem contains errors, don't run the orphan
250 * list, since the orphan list can't be trusted; and we're
251 * going to be running a full e2fsck run anyway...
253 if (fs->super->s_state & EXT2_ERROR_FS)
256 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
257 (ino > fs->super->s_inodes_count)) {
258 clear_problem_context(&pctx);
260 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
264 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
265 "block iterate buffer");
266 e2fsck_read_bitmaps(ctx);
269 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
270 clear_problem_context(&pctx);
273 pctx.str = inode.i_links_count ? _("Truncating") :
276 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
278 next_ino = inode.i_dtime;
280 ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
281 (next_ino > fs->super->s_inodes_count))) {
283 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
287 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
290 if (!inode.i_links_count) {
291 ext2fs_inode_alloc_stats2(fs, ino, -1,
292 LINUX_S_ISDIR(inode.i_mode));
294 inode.i_dtime = ctx->now;
298 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
301 ext2fs_free_mem(&block_buf);
304 ext2fs_free_mem(&block_buf);
309 * Check the resize inode to make sure it is sane. We check both for
310 * the case where on-line resizing is not enabled (in which case the
311 * resize inode should be cleared) as well as the case where on-line
312 * resizing is enabled.
314 void check_resize_inode(e2fsck_t ctx)
316 ext2_filsys fs = ctx->fs;
317 struct ext2_inode inode;
318 struct problem_context pctx;
319 int i, gdt_off, ind_off;
322 blk_t expect; /* for resize inode, which is 32-bit only */
323 __u32 *dind_buf = 0, *ind_buf;
326 clear_problem_context(&pctx);
329 * If the resize inode feature isn't set, then
330 * s_reserved_gdt_blocks must be zero.
332 if (!(fs->super->s_feature_compat &
333 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
334 if (fs->super->s_reserved_gdt_blocks) {
335 pctx.num = fs->super->s_reserved_gdt_blocks;
336 if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
338 fs->super->s_reserved_gdt_blocks = 0;
339 ext2fs_mark_super_dirty(fs);
344 /* Read the resize inode */
345 pctx.ino = EXT2_RESIZE_INO;
346 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
348 if (fs->super->s_feature_compat &
349 EXT2_FEATURE_COMPAT_RESIZE_INODE)
350 ctx->flags |= E2F_FLAG_RESIZE_INODE;
355 * If the resize inode feature isn't set, check to make sure
356 * the resize inode is cleared; then we're done.
358 if (!(fs->super->s_feature_compat &
359 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
360 for (i=0; i < EXT2_N_BLOCKS; i++) {
361 if (inode.i_block[i])
364 if ((i < EXT2_N_BLOCKS) &&
365 fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
366 memset(&inode, 0, sizeof(inode));
367 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
374 * The resize inode feature is enabled; check to make sure the
375 * only block in use is the double indirect block
377 blk = inode.i_block[EXT2_DIND_BLOCK];
378 for (i=0; i < EXT2_N_BLOCKS; i++) {
379 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
382 if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
383 !(inode.i_mode & LINUX_S_IFREG) ||
384 (blk < fs->super->s_first_data_block ||
385 blk >= ext2fs_blocks_count(fs->super))) {
386 resize_inode_invalid:
387 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
388 memset(&inode, 0, sizeof(inode));
389 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
391 ctx->flags |= E2F_FLAG_RESIZE_INODE;
393 if (!(ctx->options & E2F_OPT_READONLY)) {
394 fs->super->s_state &= ~EXT2_VALID_FS;
395 ext2fs_mark_super_dirty(fs);
399 dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
400 "resize dind buffer");
401 ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
403 retval = ext2fs_read_ind_block(fs, blk, dind_buf);
405 goto resize_inode_invalid;
407 gdt_off = fs->desc_blocks;
408 pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
409 if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0)
410 pblk++; /* Deal with 1024 blocksize bigalloc fs */
411 for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
412 i++, gdt_off++, pblk++) {
413 gdt_off %= fs->blocksize/4;
414 if (dind_buf[gdt_off] != pblk)
415 goto resize_inode_invalid;
416 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
418 goto resize_inode_invalid;
420 for (j = 1; j < fs->group_desc_count; j++) {
421 if (!ext2fs_bg_has_super(fs, j))
423 expect = pblk + (j * fs->super->s_blocks_per_group);
424 if (ind_buf[ind_off] != expect)
425 goto resize_inode_invalid;
432 ext2fs_free_mem(&dind_buf);
437 * This function checks the dirhash signed/unsigned hint if necessary.
439 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
441 struct ext2_super_block *sb = ctx->fs->super;
442 struct problem_context pctx;
445 if ((ctx->options & E2F_OPT_READONLY) ||
446 !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
447 (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
452 clear_problem_context(&pctx);
453 if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
454 if (((int) c) == -1) {
455 sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
457 sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
459 ext2fs_mark_super_dirty(ctx->fs);
464 void check_super_block(e2fsck_t ctx)
466 ext2_filsys fs = ctx->fs;
467 blk64_t first_block, last_block;
468 struct ext2_super_block *sb = fs->super;
469 unsigned int ipg_max;
471 blk64_t blocks_per_group = fs->super->s_blocks_per_group;
472 __u32 bpg_max, cpg_max;
473 int inodes_per_block;
475 int accept_time_fudge;
476 int broken_system_clock;
479 struct problem_context pctx;
480 blk64_t free_blocks = 0;
481 ino_t free_inodes = 0;
482 int csum_flag, clear_test_fs_flag;
484 inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
485 ipg_max = inodes_per_block * (blocks_per_group - 4);
486 if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
487 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
488 cpg_max = 8 * EXT2_BLOCK_SIZE(sb);
489 if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb))
490 cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb);
491 bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs);
492 if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
493 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
495 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
496 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
497 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
498 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
499 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
500 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
502 clear_problem_context(&pctx);
505 * Verify the super block constants...
507 check_super_value(ctx, "inodes_count", sb->s_inodes_count,
509 check_super_value(ctx, "blocks_count", ext2fs_blocks_count(sb),
511 check_super_value(ctx, "first_data_block", sb->s_first_data_block,
512 MAX_CHECK, 0, ext2fs_blocks_count(sb));
513 check_super_value(ctx, "log_block_size", sb->s_log_block_size,
514 MIN_CHECK | MAX_CHECK, 0,
515 EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
516 check_super_value(ctx, "log_cluster_size",
517 sb->s_log_cluster_size,
518 MIN_CHECK | MAX_CHECK, sb->s_log_block_size,
519 (EXT2_MAX_CLUSTER_LOG_SIZE -
520 EXT2_MIN_CLUSTER_LOG_SIZE));
521 check_super_value(ctx, "clusters_per_group", sb->s_clusters_per_group,
522 MIN_CHECK | MAX_CHECK, 8, cpg_max);
523 check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
524 MIN_CHECK | MAX_CHECK, 8, bpg_max);
525 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
526 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
527 check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb),
528 MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2);
529 check_super_value(ctx, "reserved_gdt_blocks",
530 sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
531 fs->blocksize / sizeof(__u32));
532 check_super_value(ctx, "desc_size",
533 sb->s_desc_size, MAX_CHECK | LOG2_CHECK, 0,
535 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
536 check_super_value(ctx, "first_ino", sb->s_first_ino,
537 MIN_CHECK | MAX_CHECK,
538 EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count);
539 inode_size = EXT2_INODE_SIZE(sb);
540 check_super_value(ctx, "inode_size",
541 inode_size, MIN_CHECK | MAX_CHECK | LOG2_CHECK,
542 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
543 if (sb->s_blocks_per_group != (sb->s_clusters_per_group *
544 EXT2FS_CLUSTER_RATIO(fs))) {
545 pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs);
546 pctx.str = "block_size";
547 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
548 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
552 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
553 (ctx->num_blocks < ext2fs_blocks_count(sb))) {
554 pctx.blk = ext2fs_blocks_count(sb);
555 pctx.blk2 = ctx->num_blocks;
556 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
557 ctx->flags |= E2F_FLAG_ABORT;
562 should_be = (sb->s_log_block_size == 0 &&
563 EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0;
564 if (sb->s_first_data_block != should_be) {
565 pctx.blk = sb->s_first_data_block;
566 pctx.blk2 = should_be;
567 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
568 ctx->flags |= E2F_FLAG_ABORT;
572 should_be = sb->s_inodes_per_group * fs->group_desc_count;
573 if (sb->s_inodes_count != should_be) {
574 pctx.ino = sb->s_inodes_count;
575 pctx.ino2 = should_be;
576 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
577 sb->s_inodes_count = should_be;
578 ext2fs_mark_super_dirty(fs);
582 /* Is 64bit set and extents unset? */
583 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
584 EXT4_FEATURE_INCOMPAT_64BIT) &&
585 !EXT2_HAS_INCOMPAT_FEATURE(fs->super,
586 EXT3_FEATURE_INCOMPAT_EXTENTS) &&
587 fix_problem(ctx, PR_0_64BIT_WITHOUT_EXTENTS, &pctx)) {
588 fs->super->s_feature_incompat |=
589 EXT3_FEATURE_INCOMPAT_EXTENTS;
590 ext2fs_mark_super_dirty(fs);
594 * Verify the group descriptors....
596 first_block = sb->s_first_data_block;
597 last_block = ext2fs_blocks_count(sb)-1;
599 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
600 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
601 for (i = 0; i < fs->group_desc_count; i++) {
604 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
605 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
606 first_block = ext2fs_group_first_block2(fs, i);
607 last_block = ext2fs_group_last_block2(fs, i);
610 if ((ext2fs_block_bitmap_loc(fs, i) < first_block) ||
611 (ext2fs_block_bitmap_loc(fs, i) > last_block)) {
612 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
613 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
614 ext2fs_block_bitmap_loc_set(fs, i, 0);
616 if (ext2fs_block_bitmap_loc(fs, i) == 0) {
617 ctx->invalid_block_bitmap_flag[i]++;
618 ctx->invalid_bitmaps++;
620 if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) ||
621 (ext2fs_inode_bitmap_loc(fs, i) > last_block)) {
622 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
623 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
624 ext2fs_inode_bitmap_loc_set(fs, i, 0);
626 if (ext2fs_inode_bitmap_loc(fs, i) == 0) {
627 ctx->invalid_inode_bitmap_flag[i]++;
628 ctx->invalid_bitmaps++;
630 if ((ext2fs_inode_table_loc(fs, i) < first_block) ||
631 ((ext2fs_inode_table_loc(fs, i) +
632 fs->inode_blocks_per_group - 1) > last_block)) {
633 pctx.blk = ext2fs_inode_table_loc(fs, i);
634 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
635 ext2fs_inode_table_loc_set(fs, i, 0);
637 if (ext2fs_inode_table_loc(fs, i) == 0) {
638 ctx->invalid_inode_table_flag[i]++;
639 ctx->invalid_bitmaps++;
641 free_blocks += ext2fs_bg_free_blocks_count(fs, i);
642 free_inodes += ext2fs_bg_free_inodes_count(fs, i);
644 if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) ||
645 (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) ||
646 (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group))
647 ext2fs_unmark_valid(fs);
650 if (!ext2fs_group_desc_csum_verify(fs, i)) {
651 pctx.csum1 = ext2fs_bg_checksum(fs, i);
652 pctx.csum2 = ext2fs_group_desc_csum(fs, i);
653 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
654 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
655 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
656 ext2fs_bg_itable_unused_set(fs, i, 0);
659 ext2fs_unmark_valid(fs);
663 (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ||
664 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ||
665 ext2fs_bg_itable_unused(fs, i) != 0)) {
666 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
667 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
668 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
669 ext2fs_bg_itable_unused_set(fs, i, 0);
672 ext2fs_unmark_valid(fs);
675 if (i == fs->group_desc_count - 1 &&
676 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) {
677 if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
678 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
681 ext2fs_unmark_valid(fs);
685 (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) ||
686 ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) {
687 pctx.blk = ext2fs_bg_itable_unused(fs, i);
688 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
689 ext2fs_bg_itable_unused_set(fs, i, 0);
692 ext2fs_unmark_valid(fs);
696 ext2fs_group_desc_csum_set(fs, i);
697 /* If the user aborts e2fsck by typing ^C, stop right away */
698 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
702 ctx->free_blocks = EXT2FS_C2B(fs, free_blocks);
703 ctx->free_inodes = free_inodes;
705 if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
706 (sb->s_free_inodes_count > sb->s_inodes_count))
707 ext2fs_unmark_valid(fs);
711 * If we have invalid bitmaps, set the error state of the
714 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
715 sb->s_state &= ~EXT2_VALID_FS;
716 ext2fs_mark_super_dirty(fs);
719 clear_problem_context(&pctx);
721 #ifndef EXT2_SKIP_UUID
723 * If the UUID field isn't assigned, assign it.
725 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
726 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
727 uuid_generate(sb->s_uuid);
728 fs->flags |= EXT2_FLAG_DIRTY;
729 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
735 * Check to see if we should disable the test_fs flag
737 profile_get_boolean(ctx->profile, "options",
738 "clear_test_fs_flag", 0, 1,
739 &clear_test_fs_flag);
740 if (!(ctx->options & E2F_OPT_READONLY) &&
741 clear_test_fs_flag &&
742 (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) &&
743 (fs_proc_check("ext4") || check_for_modules("ext4"))) {
744 if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
745 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
746 fs->flags |= EXT2_FLAG_DIRTY;
747 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
752 * For the Hurd, check to see if the filetype option is set,
753 * since it doesn't support it.
755 if (!(ctx->options & E2F_OPT_READONLY) &&
756 fs->super->s_creator_os == EXT2_OS_HURD &&
757 (fs->super->s_feature_incompat &
758 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
759 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
760 fs->super->s_feature_incompat &=
761 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
762 ext2fs_mark_super_dirty(fs);
763 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
768 * If we have any of the compatibility flags set, we need to have a
769 * revision 1 filesystem. Most kernels will not check the flags on
770 * a rev 0 filesystem and we may have corruption issues because of
771 * the incompatible changes to the filesystem.
773 if (!(ctx->options & E2F_OPT_READONLY) &&
774 fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
775 (fs->super->s_feature_compat ||
776 fs->super->s_feature_ro_compat ||
777 fs->super->s_feature_incompat) &&
778 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
779 ext2fs_update_dynamic_rev(fs);
780 ext2fs_mark_super_dirty(fs);
781 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
785 * Clean up any orphan inodes, if present.
787 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
788 fs->super->s_state &= ~EXT2_VALID_FS;
789 ext2fs_mark_super_dirty(fs);
793 * Unfortunately, due to Windows' unfortunate design decision
794 * to configure the hardware clock to tick localtime, instead
795 * of the more proper and less error-prone UTC time, many
796 * users end up in the situation where the system clock is
797 * incorrectly set at the time when e2fsck is run.
799 * Historically this was usually due to some distributions
800 * having buggy init scripts and/or installers that didn't
801 * correctly detect this case and take appropriate
802 * countermeasures. However, it's still possible, despite the
803 * best efforts of init script and installer authors to not be
804 * able to detect this misconfiguration, usually due to a
805 * buggy or misconfigured virtualization manager or the
806 * installer not having access to a network time server during
807 * the installation process. So by default, we allow the
808 * superblock times to be fudged by up to 24 hours. This can
809 * be disabled by setting options.accept_time_fudge to the
810 * boolean value of false in e2fsck.conf. We also support
811 * options.buggy_init_scripts for backwards compatibility.
813 profile_get_boolean(ctx->profile, "options", "accept_time_fudge",
814 0, 1, &accept_time_fudge);
815 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
816 0, accept_time_fudge, &accept_time_fudge);
817 ctx->time_fudge = accept_time_fudge ? 86400 : 0;
819 profile_get_boolean(ctx->profile, "options", "broken_system_clock",
820 0, 0, &broken_system_clock);
823 * Check to see if the superblock last mount time or last
824 * write time is in the future.
826 if (!broken_system_clock &&
827 !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
828 fs->super->s_mtime > (__u32) ctx->now) {
829 pctx.num = fs->super->s_mtime;
830 problem = PR_0_FUTURE_SB_LAST_MOUNT;
831 if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
832 problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
833 if (fix_problem(ctx, problem, &pctx)) {
834 fs->super->s_mtime = ctx->now;
835 fs->flags |= EXT2_FLAG_DIRTY;
838 if (!broken_system_clock &&
839 !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
840 fs->super->s_wtime > (__u32) ctx->now) {
841 pctx.num = fs->super->s_wtime;
842 problem = PR_0_FUTURE_SB_LAST_WRITE;
843 if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
844 problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
845 if (fix_problem(ctx, problem, &pctx)) {
846 fs->super->s_wtime = ctx->now;
847 fs->flags |= EXT2_FLAG_DIRTY;
852 * Move the ext3 journal file, if necessary.
854 e2fsck_move_ext3_journal(ctx);
857 * Fix journal hint, if necessary
859 e2fsck_fix_ext3_journal_hint(ctx);
862 * Add dirhash hint if necessary
864 e2fsck_fix_dirhash_hint(ctx);
867 * Hide quota inodes if necessary.
869 e2fsck_hide_quota(ctx);
875 * Check to see if we should backup the master sb to the backup super
876 * blocks. Returns non-zero if the sb should be backed up.
880 * A few flags are set on the fly by the kernel, but only in the
881 * primary superblock. This is actually a bad thing, and we should
882 * try to discourage it in the future. In particular, for the newer
883 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
884 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
885 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
886 * copying the primary superblock during online resize.
888 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
889 * unfortunately, we shouldn't ignore it since if it's not set in the
890 * backup, the extended attributes in the filesystem will be stripped
893 #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
894 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
895 #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \
896 EXT3_FEATURE_INCOMPAT_RECOVER)
898 int check_backup_super_block(e2fsck_t ctx)
900 ext2_filsys fs = ctx->fs;
905 char buf[SUPERBLOCK_SIZE];
906 struct ext2_super_block *backup_sb;
909 * If we are already writing out the backup blocks, then we
910 * don't need to test. Also, if the filesystem is invalid, or
911 * the check was aborted or cancelled, we also don't want to
912 * do the backup. If the filesystem was opened read-only then
913 * we can't do the backup.
915 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
916 !ext2fs_test_valid(fs) ||
917 (fs->super->s_state & EXT2_ERROR_FS) ||
918 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
919 (ctx->options & E2F_OPT_READONLY))
922 for (g = 1; g < fs->group_desc_count; g++) {
923 if (!ext2fs_bg_has_super(fs, g))
926 sb = ext2fs_group_first_block2(fs, g);
928 retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE,
932 backup_sb = (struct ext2_super_block *) buf;
933 #ifdef WORDS_BIGENDIAN
934 ext2fs_swap_super(backup_sb);
936 if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) ||
937 (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) ||
938 ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
939 EXT2_MAX_BLOCK_LOG_SIZE) ||
940 (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE))
943 #define SUPER_INCOMPAT_DIFFERENT(x) \
944 ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
945 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
946 #define SUPER_RO_COMPAT_DIFFERENT(x) \
947 ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
948 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
949 #define SUPER_DIFFERENT(x) \
950 (fs->super->x != backup_sb->x)
952 if (SUPER_DIFFERENT(s_feature_compat) ||
953 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
954 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
955 SUPER_DIFFERENT(s_blocks_count) ||
956 SUPER_DIFFERENT(s_blocks_count_hi) ||
957 SUPER_DIFFERENT(s_inodes_count) ||
958 memcmp(fs->super->s_uuid, backup_sb->s_uuid,
959 sizeof(fs->super->s_uuid)))