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"
25 static void check_super_value(e2fsck_t ctx, const char *descr,
26 unsigned long value, int flags,
27 unsigned long min_val, unsigned long max_val)
29 struct problem_context pctx;
31 if (((flags & MIN_CHECK) && (value < min_val)) ||
32 ((flags & MAX_CHECK) && (value > max_val))) {
33 clear_problem_context(&pctx);
36 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
37 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
42 * helper function to release an inode
44 struct process_block_struct {
47 struct problem_context *pctx;
50 e2_blkcnt_t truncate_block;
56 static int release_inode_block(ext2_filsys fs,
59 blk_t ref_blk EXT2FS_ATTR((unused)),
60 int ref_offset EXT2FS_ATTR((unused)),
63 struct process_block_struct *pb;
65 struct problem_context *pctx;
66 blk_t blk = *block_nr;
69 pb = (struct process_block_struct *) priv_data;
74 pctx->blkcount = blockcnt;
76 if (HOLE_BLKADDR(blk))
79 if ((blk < fs->super->s_first_data_block) ||
80 (blk >= ext2fs_blocks_count(fs->super))) {
81 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
87 if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) {
88 fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
93 * If we are deleting an orphan, then we leave the fields alone.
94 * If we are truncating an orphan, then update the inode fields
95 * and clean up any partial block data.
99 * We only remove indirect blocks if they are
106 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
111 limit = fs->blocksize >> 2;
112 for (i = 0, bp = (blk_t *) pb->buf;
113 i < limit; i++, bp++)
118 * We don't remove direct blocks until we've reached
119 * the truncation block.
121 if (blockcnt >= 0 && blockcnt < pb->truncate_block)
124 * If part of the last block needs truncating, we do
127 if ((blockcnt == pb->truncate_block) && pb->truncate_offset) {
128 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
132 memset(pb->buf + pb->truncate_offset, 0,
133 fs->blocksize - pb->truncate_offset);
134 pb->errcode = io_channel_write_blk64(fs->io, blk, 1,
139 pb->truncated_blocks++;
141 retval |= BLOCK_CHANGED;
144 ext2fs_block_alloc_stats2(fs, blk, -1);
149 * This function releases an inode. Returns 1 if an inconsistency was
150 * found. If the inode has a link count, then it is being truncated and
153 static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
154 struct ext2_inode *inode, char *block_buf,
155 struct problem_context *pctx)
157 struct process_block_struct pb;
158 ext2_filsys fs = ctx->fs;
162 if (!ext2fs_inode_has_valid_blocks(inode))
165 pb.buf = block_buf + 3 * ctx->fs->blocksize;
170 if (inode->i_links_count) {
172 pb.truncate_block = (e2_blkcnt_t)
173 ((((long long)inode->i_size_high << 32) +
174 inode->i_size + fs->blocksize - 1) /
176 pb.truncate_offset = inode->i_size % fs->blocksize;
179 pb.truncate_block = 0;
180 pb.truncate_offset = 0;
182 pb.truncated_blocks = 0;
183 retval = ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
184 block_buf, release_inode_block, &pb);
186 com_err("release_inode_blocks", retval,
187 _("while calling ext2fs_block_iterate for inode %d"),
194 /* Refresh the inode since ext2fs_block_iterate may have changed it */
195 e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks");
197 if (pb.truncated_blocks)
198 ext2fs_iblk_sub_blocks(fs, inode, pb.truncated_blocks);
200 if (ext2fs_file_acl_block(inode)) {
201 retval = ext2fs_adjust_ea_refcount(fs, ext2fs_file_acl_block(inode),
202 block_buf, -1, &count);
203 if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
208 com_err("release_inode_blocks", retval,
209 _("while calling ext2fs_adjust_ea_refcount for inode %d"),
214 ext2fs_block_alloc_stats2(fs,
215 ext2fs_file_acl_block(inode),
217 ext2fs_file_acl_block_set(inode, 0);
223 * This function releases all of the orphan inodes. It returns 1 if
224 * it hit some error, and 0 on success.
226 static int release_orphan_inodes(e2fsck_t ctx)
228 ext2_filsys fs = ctx->fs;
229 ext2_ino_t ino, next_ino;
230 struct ext2_inode inode;
231 struct problem_context pctx;
234 if ((ino = fs->super->s_last_orphan) == 0)
238 * Win or lose, we won't be using the head of the orphan inode
241 fs->super->s_last_orphan = 0;
242 ext2fs_mark_super_dirty(fs);
245 * If the filesystem contains errors, don't run the orphan
246 * list, since the orphan list can't be trusted; and we're
247 * going to be running a full e2fsck run anyway...
249 if (fs->super->s_state & EXT2_ERROR_FS)
252 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
253 (ino > fs->super->s_inodes_count)) {
254 clear_problem_context(&pctx);
256 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
260 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
261 "block iterate buffer");
262 e2fsck_read_bitmaps(ctx);
265 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
266 clear_problem_context(&pctx);
269 pctx.str = inode.i_links_count ? _("Truncating") :
272 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
274 next_ino = inode.i_dtime;
276 ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
277 (next_ino > fs->super->s_inodes_count))) {
279 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
283 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
286 if (!inode.i_links_count) {
287 ext2fs_inode_alloc_stats2(fs, ino, -1,
288 LINUX_S_ISDIR(inode.i_mode));
289 inode.i_dtime = ctx->now;
293 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
296 ext2fs_free_mem(&block_buf);
299 ext2fs_free_mem(&block_buf);
304 * Check the resize inode to make sure it is sane. We check both for
305 * the case where on-line resizing is not enabled (in which case the
306 * resize inode should be cleared) as well as the case where on-line
307 * resizing is enabled.
309 void check_resize_inode(e2fsck_t ctx)
311 ext2_filsys fs = ctx->fs;
312 struct ext2_inode inode;
313 struct problem_context pctx;
314 int i, gdt_off, ind_off;
316 blk_t blk, pblk, expect;
317 __u32 *dind_buf = 0, *ind_buf;
320 clear_problem_context(&pctx);
323 * If the resize inode feature isn't set, then
324 * s_reserved_gdt_blocks must be zero.
326 if (!(fs->super->s_feature_compat &
327 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
328 if (fs->super->s_reserved_gdt_blocks) {
329 pctx.num = fs->super->s_reserved_gdt_blocks;
330 if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
332 fs->super->s_reserved_gdt_blocks = 0;
333 ext2fs_mark_super_dirty(fs);
338 /* Read the resize inode */
339 pctx.ino = EXT2_RESIZE_INO;
340 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
342 if (fs->super->s_feature_compat &
343 EXT2_FEATURE_COMPAT_RESIZE_INODE)
344 ctx->flags |= E2F_FLAG_RESIZE_INODE;
349 * If the resize inode feature isn't set, check to make sure
350 * the resize inode is cleared; then we're done.
352 if (!(fs->super->s_feature_compat &
353 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
354 for (i=0; i < EXT2_N_BLOCKS; i++) {
355 if (inode.i_block[i])
358 if ((i < EXT2_N_BLOCKS) &&
359 fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
360 memset(&inode, 0, sizeof(inode));
361 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
368 * The resize inode feature is enabled; check to make sure the
369 * only block in use is the double indirect block
371 blk = inode.i_block[EXT2_DIND_BLOCK];
372 for (i=0; i < EXT2_N_BLOCKS; i++) {
373 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
376 if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
377 !(inode.i_mode & LINUX_S_IFREG) ||
378 (blk < fs->super->s_first_data_block ||
379 blk >= ext2fs_blocks_count(fs->super))) {
380 resize_inode_invalid:
381 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
382 memset(&inode, 0, sizeof(inode));
383 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
385 ctx->flags |= E2F_FLAG_RESIZE_INODE;
387 if (!(ctx->options & E2F_OPT_READONLY)) {
388 fs->super->s_state &= ~EXT2_VALID_FS;
389 ext2fs_mark_super_dirty(fs);
393 dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
394 "resize dind buffer");
395 ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
397 retval = ext2fs_read_ind_block(fs, blk, dind_buf);
399 goto resize_inode_invalid;
401 gdt_off = fs->desc_blocks;
402 pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
403 for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
404 i++, gdt_off++, pblk++) {
405 gdt_off %= fs->blocksize/4;
406 if (dind_buf[gdt_off] != pblk)
407 goto resize_inode_invalid;
408 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
410 goto resize_inode_invalid;
412 for (j = 1; j < fs->group_desc_count; j++) {
413 if (!ext2fs_bg_has_super(fs, j))
415 expect = pblk + (j * fs->super->s_blocks_per_group);
416 if (ind_buf[ind_off] != expect)
417 goto resize_inode_invalid;
424 ext2fs_free_mem(&dind_buf);
429 * This function checks the dirhash signed/unsigned hint if necessary.
431 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
433 struct ext2_super_block *sb = ctx->fs->super;
434 struct problem_context pctx;
437 if ((ctx->options & E2F_OPT_READONLY) ||
438 !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
439 (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
444 clear_problem_context(&pctx);
445 if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
446 if (((int) c) == -1) {
447 sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
449 sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
451 ext2fs_mark_super_dirty(ctx->fs);
456 void check_super_block(e2fsck_t ctx)
458 ext2_filsys fs = ctx->fs;
459 blk_t first_block, last_block;
460 struct ext2_super_block *sb = fs->super;
462 blk_t blocks_per_group = fs->super->s_blocks_per_group;
464 int inodes_per_block;
467 int accept_time_fudge;
470 struct problem_context pctx;
471 blk_t free_blocks = 0;
472 ino_t free_inodes = 0;
473 int csum_flag, clear_test_fs_flag;
475 inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
476 ipg_max = inodes_per_block * (blocks_per_group - 4);
477 if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
478 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
479 bpg_max = 8 * EXT2_BLOCK_SIZE(sb);
480 if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
481 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
483 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
484 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
485 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
486 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
487 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
488 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
490 clear_problem_context(&pctx);
493 * Verify the super block constants...
495 check_super_value(ctx, "inodes_count", sb->s_inodes_count,
497 check_super_value(ctx, "blocks_count", ext2fs_blocks_count(sb),
499 check_super_value(ctx, "first_data_block", sb->s_first_data_block,
500 MAX_CHECK, 0, ext2fs_blocks_count(sb));
501 check_super_value(ctx, "log_block_size", sb->s_log_block_size,
502 MIN_CHECK | MAX_CHECK, 0,
503 EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
504 check_super_value(ctx, "log_frag_size", sb->s_log_frag_size,
505 MIN_CHECK | MAX_CHECK, 0, sb->s_log_block_size);
506 check_super_value(ctx, "frags_per_group", sb->s_frags_per_group,
507 MIN_CHECK | MAX_CHECK, sb->s_blocks_per_group,
509 check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
510 MIN_CHECK | MAX_CHECK, 8, bpg_max);
511 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
512 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
513 check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb),
514 MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2);
515 check_super_value(ctx, "reserved_gdt_blocks",
516 sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
518 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
519 check_super_value(ctx, "first_ino", sb->s_first_ino,
520 MIN_CHECK | MAX_CHECK,
521 EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count);
522 inode_size = EXT2_INODE_SIZE(sb);
523 check_super_value(ctx, "inode_size",
524 inode_size, MIN_CHECK | MAX_CHECK,
525 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
526 if (inode_size & (inode_size - 1)) {
527 pctx.num = inode_size;
528 pctx.str = "inode_size";
529 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
530 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
534 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
535 (ctx->num_blocks < ext2fs_blocks_count(sb))) {
536 pctx.blk = ext2fs_blocks_count(sb);
537 pctx.blk2 = ctx->num_blocks;
538 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
539 ctx->flags |= E2F_FLAG_ABORT;
544 if (sb->s_log_block_size != (__u32) sb->s_log_frag_size) {
545 pctx.blk = EXT2_BLOCK_SIZE(sb);
546 pctx.blk2 = EXT2_FRAG_SIZE(sb);
547 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
548 ctx->flags |= E2F_FLAG_ABORT;
552 should_be = sb->s_frags_per_group >>
553 (sb->s_log_block_size - sb->s_log_frag_size);
554 if (sb->s_blocks_per_group != should_be) {
555 pctx.blk = sb->s_blocks_per_group;
556 pctx.blk2 = should_be;
557 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
558 ctx->flags |= E2F_FLAG_ABORT;
562 should_be = (sb->s_log_block_size == 0) ? 1 : 0;
563 if (sb->s_first_data_block != should_be) {
564 pctx.blk = sb->s_first_data_block;
565 pctx.blk2 = should_be;
566 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
567 ctx->flags |= E2F_FLAG_ABORT;
571 should_be = sb->s_inodes_per_group * fs->group_desc_count;
572 if (sb->s_inodes_count != should_be) {
573 pctx.ino = sb->s_inodes_count;
574 pctx.ino2 = should_be;
575 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
576 sb->s_inodes_count = should_be;
577 ext2fs_mark_super_dirty(fs);
582 * Verify the group descriptors....
584 first_block = sb->s_first_data_block;
585 last_block = ext2fs_blocks_count(sb)-1;
587 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
588 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
589 for (i = 0; i < fs->group_desc_count; i++) {
592 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
593 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
594 first_block = ext2fs_group_first_block2(fs, i);
595 last_block = ext2fs_group_last_block2(fs, i);
598 if ((ext2fs_block_bitmap_loc(fs, i) < first_block) ||
599 (ext2fs_block_bitmap_loc(fs, i) > last_block)) {
600 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
601 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
602 ext2fs_block_bitmap_loc_set(fs, i, 0);
604 if (ext2fs_block_bitmap_loc(fs, i) == 0) {
605 ctx->invalid_block_bitmap_flag[i]++;
606 ctx->invalid_bitmaps++;
608 if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) ||
609 (ext2fs_inode_bitmap_loc(fs, i) > last_block)) {
610 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
611 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
612 ext2fs_inode_bitmap_loc_set(fs, i, 0);
614 if (ext2fs_inode_bitmap_loc(fs, i) == 0) {
615 ctx->invalid_inode_bitmap_flag[i]++;
616 ctx->invalid_bitmaps++;
618 if ((ext2fs_inode_table_loc(fs, i) < first_block) ||
619 ((ext2fs_inode_table_loc(fs, i) +
620 fs->inode_blocks_per_group - 1) > last_block)) {
621 pctx.blk = ext2fs_inode_table_loc(fs, i);
622 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
623 ext2fs_inode_table_loc_set(fs, i, 0);
625 if (ext2fs_inode_table_loc(fs, i) == 0) {
626 ctx->invalid_inode_table_flag[i]++;
627 ctx->invalid_bitmaps++;
629 free_blocks += ext2fs_bg_free_blocks_count(fs, i);
630 free_inodes += ext2fs_bg_free_inodes_count(fs, i);
632 if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) ||
633 (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) ||
634 (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group))
635 ext2fs_unmark_valid(fs);
638 if (!ext2fs_group_desc_csum_verify(fs, i)) {
639 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
640 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
641 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
642 ext2fs_bg_itable_unused_set(fs, i, 0);
645 ext2fs_unmark_valid(fs);
649 (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ||
650 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ||
651 ext2fs_bg_itable_unused(fs, i) != 0)) {
652 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
653 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
654 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
655 ext2fs_bg_itable_unused_set(fs, i, 0);
658 ext2fs_unmark_valid(fs);
661 if (i == fs->group_desc_count - 1 &&
662 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) {
663 if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
664 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
667 ext2fs_unmark_valid(fs);
670 if (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
671 !ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT)) {
672 if (fix_problem(ctx, PR_0_BB_UNINIT_IB_INIT, &pctx)) {
673 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
676 ext2fs_unmark_valid(fs);
680 (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) ||
681 ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) {
682 pctx.blk = ext2fs_bg_itable_unused(fs, i);
683 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
684 ext2fs_bg_itable_unused_set(fs, i, 0);
687 ext2fs_unmark_valid(fs);
691 ext2fs_group_desc_csum_set(fs, i);
692 /* If the user aborts e2fsck by typing ^C, stop right away */
693 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
698 * Update the global counts from the block group counts. This
699 * is needed for an experimental patch which eliminates
700 * locking the entire filesystem when allocating blocks or
701 * inodes; if the filesystem is not unmounted cleanly, the
702 * global counts may not be accurate.
704 if ((free_blocks != ext2fs_free_blocks_count(sb)) ||
705 (free_inodes != sb->s_free_inodes_count)) {
706 if (ctx->options & E2F_OPT_READONLY)
707 ext2fs_unmark_valid(fs);
709 ext2fs_free_blocks_count_set(sb, free_blocks);
710 sb->s_free_inodes_count = free_inodes;
711 ext2fs_mark_super_dirty(fs);
715 if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
716 (sb->s_free_inodes_count > sb->s_inodes_count))
717 ext2fs_unmark_valid(fs);
721 * If we have invalid bitmaps, set the error state of the
724 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
725 sb->s_state &= ~EXT2_VALID_FS;
726 ext2fs_mark_super_dirty(fs);
729 clear_problem_context(&pctx);
731 #ifndef EXT2_SKIP_UUID
733 * If the UUID field isn't assigned, assign it.
735 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
736 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
737 uuid_generate(sb->s_uuid);
738 ext2fs_mark_super_dirty(fs);
739 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
745 * Check to see if we should disable the test_fs flag
747 profile_get_boolean(ctx->profile, "options",
748 "clear_test_fs_flag", 0, 1,
749 &clear_test_fs_flag);
750 if (!(ctx->options & E2F_OPT_READONLY) &&
751 clear_test_fs_flag &&
752 (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) &&
753 (fs_proc_check("ext4") || check_for_modules("ext4"))) {
754 if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
755 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
756 ext2fs_mark_super_dirty(fs);
757 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
762 * For the Hurd, check to see if the filetype option is set,
763 * since it doesn't support it.
765 if (!(ctx->options & E2F_OPT_READONLY) &&
766 fs->super->s_creator_os == EXT2_OS_HURD &&
767 (fs->super->s_feature_incompat &
768 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
769 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
770 fs->super->s_feature_incompat &=
771 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
772 ext2fs_mark_super_dirty(fs);
773 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
778 * If we have any of the compatibility flags set, we need to have a
779 * revision 1 filesystem. Most kernels will not check the flags on
780 * a rev 0 filesystem and we may have corruption issues because of
781 * the incompatible changes to the filesystem.
783 if (!(ctx->options & E2F_OPT_READONLY) &&
784 fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
785 (fs->super->s_feature_compat ||
786 fs->super->s_feature_ro_compat ||
787 fs->super->s_feature_incompat) &&
788 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
789 ext2fs_update_dynamic_rev(fs);
790 ext2fs_mark_super_dirty(fs);
791 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
795 * Clean up any orphan inodes, if present.
797 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
798 fs->super->s_state &= ~EXT2_VALID_FS;
799 ext2fs_mark_super_dirty(fs);
803 * Unfortunately, due to Windows' unfortunate design decision
804 * to configure the hardware clock to tick localtime, instead
805 * of the more proper and less error-prone UTC time, many
806 * users end up in the situation where the system clock is
807 * incorrectly set at the time when e2fsck is run.
809 * Historically this was usually due to some distributions
810 * having buggy init scripts and/or installers that didn't
811 * correctly detect this case and take appropriate
812 * countermeasures. However, it's still possible, despite the
813 * best efforts of init script and installer authors to not be
814 * able to detect this misconfiguration, usually due to a
815 * buggy or misconfigured virtualization manager or the
816 * installer not having access to a network time server during
817 * the installation process. So by default, we allow the
818 * superblock times to be fudged by up to 24 hours. This can
819 * be disabled by setting options.accept_time_fudge to the
820 * boolean value of false in e2fsck.conf. We also support
821 * options.buggy_init_scripts for backwards compatibility.
823 profile_get_boolean(ctx->profile, "options", "accept_time_fudge",
824 0, 1, &accept_time_fudge);
825 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
826 0, accept_time_fudge, &accept_time_fudge);
827 ctx->time_fudge = accept_time_fudge ? 86400 : 0;
830 * Check to see if the superblock last mount time or last
831 * write time is in the future.
833 if (fs->super->s_mtime > (__u32) ctx->now) {
834 pctx.num = fs->super->s_mtime;
835 problem = PR_0_FUTURE_SB_LAST_MOUNT;
836 if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
837 problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
838 if (fix_problem(ctx, problem, &pctx)) {
839 fs->super->s_mtime = ctx->now;
840 ext2fs_mark_super_dirty(fs);
843 if (fs->super->s_wtime > (__u32) ctx->now) {
844 pctx.num = fs->super->s_wtime;
845 problem = PR_0_FUTURE_SB_LAST_WRITE;
846 if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
847 problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
848 if (fix_problem(ctx, problem, &pctx)) {
849 fs->super->s_wtime = ctx->now;
850 ext2fs_mark_super_dirty(fs);
855 * Move the ext3 journal file, if necessary.
857 e2fsck_move_ext3_journal(ctx);
860 * Fix journal hint, if necessary
862 e2fsck_fix_ext3_journal_hint(ctx);
865 * Add dirhash hint if necessary
867 e2fsck_fix_dirhash_hint(ctx);
873 * Check to see if we should backup the master sb to the backup super
874 * blocks. Returns non-zero if the sb should be backed up.
878 * A few flags are set on the fly by the kernel, but only in the
879 * primary superblock. This is actually a bad thing, and we should
880 * try to discourage it in the future. In particular, for the newer
881 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
882 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
883 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
884 * copying the primary superblock during online resize.
886 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
887 * unfortunately, we shouldn't ignore it since if it's not set in the
888 * backup, the extended attributes in the filesystem will be stripped
891 #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
892 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
893 #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \
894 EXT3_FEATURE_INCOMPAT_RECOVER)
896 int check_backup_super_block(e2fsck_t ctx)
898 ext2_filsys fs = ctx->fs;
903 char buf[SUPERBLOCK_SIZE];
904 struct ext2_super_block *backup_sb;
907 * If we are already writing out the backup blocks, then we
908 * don't need to test. Also, if the filesystem is invalid, or
909 * the check was aborted or cancelled, we also don't want to
910 * do the backup. If the filesystem was opened read-only then
911 * we can't do the backup.
913 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
914 !ext2fs_test_valid(fs) ||
915 (fs->super->s_state & EXT2_ERROR_FS) ||
916 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
917 (ctx->options & E2F_OPT_READONLY))
920 for (g = 1; g < fs->group_desc_count; g++) {
921 if (!ext2fs_bg_has_super(fs, g))
924 sb = fs->super->s_first_data_block +
925 (g * fs->super->s_blocks_per_group);
927 retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE,
931 backup_sb = (struct ext2_super_block *) buf;
932 #ifdef WORDS_BIGENDIAN
933 ext2fs_swap_super(backup_sb);
935 if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) ||
936 (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) ||
937 ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
938 EXT2_MAX_BLOCK_LOG_SIZE) ||
939 (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE))
942 #define SUPER_INCOMPAT_DIFFERENT(x) \
943 ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
944 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
945 #define SUPER_RO_COMPAT_DIFFERENT(x) \
946 ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
947 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
948 #define SUPER_DIFFERENT(x) \
949 (fs->super->x != backup_sb->x)
951 if (SUPER_DIFFERENT(s_feature_compat) ||
952 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
953 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
954 SUPER_DIFFERENT(s_blocks_count) ||
955 SUPER_DIFFERENT(s_inodes_count) ||
956 memcmp(fs->super->s_uuid, backup_sb->s_uuid,
957 sizeof(fs->super->s_uuid)))