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"
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 clear_problem_context(&pctx);
37 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
38 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
43 * helper function to release an inode
45 struct process_block_struct {
48 struct problem_context *pctx;
51 e2_blkcnt_t truncate_block;
57 static int release_inode_block(ext2_filsys fs,
60 blk64_t ref_blk EXT2FS_ATTR((unused)),
61 int ref_offset EXT2FS_ATTR((unused)),
64 struct process_block_struct *pb;
66 struct problem_context *pctx;
67 blk64_t blk = *block_nr;
70 pb = (struct process_block_struct *) priv_data;
75 pctx->blkcount = blockcnt;
77 if (HOLE_BLKADDR(blk))
80 if ((blk < fs->super->s_first_data_block) ||
81 (blk >= ext2fs_blocks_count(fs->super))) {
82 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
88 if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) {
89 fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
94 * If we are deleting an orphan, then we leave the fields alone.
95 * If we are truncating an orphan, then update the inode fields
96 * and clean up any partial block data.
100 * We only remove indirect blocks if they are
107 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
112 limit = fs->blocksize >> 2;
113 for (i = 0, bp = (blk_t *) pb->buf;
114 i < limit; i++, bp++)
119 * We don't remove direct blocks until we've reached
120 * the truncation block.
122 if (blockcnt >= 0 && blockcnt < pb->truncate_block)
125 * If part of the last block needs truncating, we do
128 if ((blockcnt == pb->truncate_block) && pb->truncate_offset) {
129 pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
133 memset(pb->buf + pb->truncate_offset, 0,
134 fs->blocksize - pb->truncate_offset);
135 pb->errcode = io_channel_write_blk64(fs->io, blk, 1,
140 pb->truncated_blocks++;
142 retval |= BLOCK_CHANGED;
145 ext2fs_block_alloc_stats2(fs, blk, -1);
151 * This function releases an inode. Returns 1 if an inconsistency was
152 * found. If the inode has a link count, then it is being truncated and
155 static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
156 struct ext2_inode *inode, char *block_buf,
157 struct problem_context *pctx)
159 struct process_block_struct pb;
160 ext2_filsys fs = ctx->fs;
164 if (!ext2fs_inode_has_valid_blocks2(fs, inode))
167 pb.buf = block_buf + 3 * ctx->fs->blocksize;
172 if (inode->i_links_count) {
174 pb.truncate_block = (e2_blkcnt_t)
175 ((EXT2_I_SIZE(inode) + fs->blocksize - 1) /
177 pb.truncate_offset = inode->i_size % fs->blocksize;
180 pb.truncate_block = 0;
181 pb.truncate_offset = 0;
183 pb.truncated_blocks = 0;
184 retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
185 block_buf, release_inode_block, &pb);
187 com_err("release_inode_blocks", retval,
188 _("while calling ext2fs_block_iterate for inode %d"),
195 /* Refresh the inode since ext2fs_block_iterate may have changed it */
196 e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks");
198 if (pb.truncated_blocks)
199 ext2fs_iblk_sub_blocks(fs, inode, pb.truncated_blocks);
201 if (ext2fs_file_acl_block(fs, inode)) {
202 retval = ext2fs_adjust_ea_refcount2(fs,
203 ext2fs_file_acl_block(fs, inode),
204 block_buf, -1, &count);
205 if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
210 com_err("release_inode_blocks", retval,
211 _("while calling ext2fs_adjust_ea_refcount2 for inode %d"),
216 ext2fs_block_alloc_stats2(fs,
217 ext2fs_file_acl_block(fs, inode), -1);
220 ext2fs_file_acl_block_set(fs, inode, 0);
226 * This function releases all of the orphan inodes. It returns 1 if
227 * it hit some error, and 0 on success.
229 static int release_orphan_inodes(e2fsck_t ctx)
231 ext2_filsys fs = ctx->fs;
232 ext2_ino_t ino, next_ino;
233 struct ext2_inode inode;
234 struct problem_context pctx;
237 if ((ino = fs->super->s_last_orphan) == 0)
241 * Win or lose, we won't be using the head of the orphan inode
244 fs->super->s_last_orphan = 0;
245 ext2fs_mark_super_dirty(fs);
248 * If the filesystem contains errors, don't run the orphan
249 * list, since the orphan list can't be trusted; and we're
250 * going to be running a full e2fsck run anyway...
252 if (fs->super->s_state & EXT2_ERROR_FS)
255 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
256 (ino > fs->super->s_inodes_count)) {
257 clear_problem_context(&pctx);
259 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
263 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
264 "block iterate buffer");
265 e2fsck_read_bitmaps(ctx);
268 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
269 clear_problem_context(&pctx);
272 pctx.str = inode.i_links_count ? _("Truncating") :
275 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
277 next_ino = inode.i_dtime;
279 ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
280 (next_ino > fs->super->s_inodes_count))) {
282 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
286 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
289 if (!inode.i_links_count) {
290 ext2fs_inode_alloc_stats2(fs, ino, -1,
291 LINUX_S_ISDIR(inode.i_mode));
293 inode.i_dtime = ctx->now;
297 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
300 ext2fs_free_mem(&block_buf);
303 ext2fs_free_mem(&block_buf);
308 * Check the resize inode to make sure it is sane. We check both for
309 * the case where on-line resizing is not enabled (in which case the
310 * resize inode should be cleared) as well as the case where on-line
311 * resizing is enabled.
313 void check_resize_inode(e2fsck_t ctx)
315 ext2_filsys fs = ctx->fs;
316 struct ext2_inode inode;
317 struct problem_context pctx;
318 int i, gdt_off, ind_off;
320 blk64_t blk, pblk, expect;
321 __u32 *dind_buf = 0, *ind_buf;
324 clear_problem_context(&pctx);
327 * If the resize inode feature isn't set, then
328 * s_reserved_gdt_blocks must be zero.
330 if (!(fs->super->s_feature_compat &
331 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
332 if (fs->super->s_reserved_gdt_blocks) {
333 pctx.num = fs->super->s_reserved_gdt_blocks;
334 if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
336 fs->super->s_reserved_gdt_blocks = 0;
337 ext2fs_mark_super_dirty(fs);
342 /* Read the resize inode */
343 pctx.ino = EXT2_RESIZE_INO;
344 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
346 if (fs->super->s_feature_compat &
347 EXT2_FEATURE_COMPAT_RESIZE_INODE)
348 ctx->flags |= E2F_FLAG_RESIZE_INODE;
353 * If the resize inode feature isn't set, check to make sure
354 * the resize inode is cleared; then we're done.
356 if (!(fs->super->s_feature_compat &
357 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
358 for (i=0; i < EXT2_N_BLOCKS; i++) {
359 if (inode.i_block[i])
362 if ((i < EXT2_N_BLOCKS) &&
363 fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
364 memset(&inode, 0, sizeof(inode));
365 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
372 * The resize inode feature is enabled; check to make sure the
373 * only block in use is the double indirect block
375 blk = inode.i_block[EXT2_DIND_BLOCK];
376 for (i=0; i < EXT2_N_BLOCKS; i++) {
377 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
380 if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
381 !(inode.i_mode & LINUX_S_IFREG) ||
382 (blk < fs->super->s_first_data_block ||
383 blk >= ext2fs_blocks_count(fs->super))) {
384 resize_inode_invalid:
385 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
386 memset(&inode, 0, sizeof(inode));
387 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
389 ctx->flags |= E2F_FLAG_RESIZE_INODE;
391 if (!(ctx->options & E2F_OPT_READONLY)) {
392 fs->super->s_state &= ~EXT2_VALID_FS;
393 ext2fs_mark_super_dirty(fs);
397 dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
398 "resize dind buffer");
399 ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
401 retval = ext2fs_read_ind_block(fs, blk, dind_buf);
403 goto resize_inode_invalid;
405 gdt_off = fs->desc_blocks;
406 pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
407 if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0)
408 pblk++; /* Deal with 1024 blocksize bigalloc fs */
409 for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
410 i++, gdt_off++, pblk++) {
411 gdt_off %= fs->blocksize/4;
412 if (dind_buf[gdt_off] != pblk)
413 goto resize_inode_invalid;
414 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
416 goto resize_inode_invalid;
418 for (j = 1; j < fs->group_desc_count; j++) {
419 if (!ext2fs_bg_has_super(fs, j))
421 expect = pblk + (j * fs->super->s_blocks_per_group);
422 if (ind_buf[ind_off] != expect)
423 goto resize_inode_invalid;
430 ext2fs_free_mem(&dind_buf);
435 * This function checks the dirhash signed/unsigned hint if necessary.
437 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
439 struct ext2_super_block *sb = ctx->fs->super;
440 struct problem_context pctx;
443 if ((ctx->options & E2F_OPT_READONLY) ||
444 !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
445 (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
450 clear_problem_context(&pctx);
451 if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
452 if (((int) c) == -1) {
453 sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
455 sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
457 ext2fs_mark_super_dirty(ctx->fs);
462 void check_super_block(e2fsck_t ctx)
464 ext2_filsys fs = ctx->fs;
465 blk64_t first_block, last_block;
466 struct ext2_super_block *sb = fs->super;
468 blk64_t blocks_per_group = fs->super->s_blocks_per_group;
469 __u32 bpg_max, cpg_max;
470 int inodes_per_block;
473 int accept_time_fudge;
474 int broken_system_clock;
477 struct problem_context pctx;
478 blk64_t free_blocks = 0;
479 ino_t free_inodes = 0;
480 int csum_flag, clear_test_fs_flag;
482 inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
483 ipg_max = inodes_per_block * (blocks_per_group - 4);
484 if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
485 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
486 cpg_max = 8 * EXT2_BLOCK_SIZE(sb);
487 if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb))
488 cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb);
489 bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs);
490 if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
491 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
493 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
494 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
495 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
496 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
497 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
498 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
500 clear_problem_context(&pctx);
503 * Verify the super block constants...
505 check_super_value(ctx, "inodes_count", sb->s_inodes_count,
507 check_super_value(ctx, "blocks_count", ext2fs_blocks_count(sb),
509 check_super_value(ctx, "first_data_block", sb->s_first_data_block,
510 MAX_CHECK, 0, ext2fs_blocks_count(sb));
511 check_super_value(ctx, "log_block_size", sb->s_log_block_size,
512 MIN_CHECK | MAX_CHECK, 0,
513 EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
514 check_super_value(ctx, "log_cluster_size",
515 sb->s_log_cluster_size,
516 MIN_CHECK | MAX_CHECK, sb->s_log_block_size,
517 (EXT2_MAX_CLUSTER_LOG_SIZE -
518 EXT2_MIN_CLUSTER_LOG_SIZE));
519 check_super_value(ctx, "clusters_per_group", sb->s_clusters_per_group,
520 MIN_CHECK | MAX_CHECK, 8, cpg_max);
521 check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
522 MIN_CHECK | MAX_CHECK, 8, bpg_max);
523 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
524 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
525 check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb),
526 MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2);
527 check_super_value(ctx, "reserved_gdt_blocks",
528 sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
530 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
531 check_super_value(ctx, "first_ino", sb->s_first_ino,
532 MIN_CHECK | MAX_CHECK,
533 EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count);
534 inode_size = EXT2_INODE_SIZE(sb);
535 check_super_value(ctx, "inode_size",
536 inode_size, MIN_CHECK | MAX_CHECK,
537 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
538 if (sb->s_blocks_per_group != (sb->s_clusters_per_group *
539 EXT2FS_CLUSTER_RATIO(fs))) {
540 pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs);
541 pctx.str = "block_size";
542 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
543 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
546 if (inode_size & (inode_size - 1)) {
547 pctx.num = inode_size;
548 pctx.str = "inode_size";
549 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
550 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
554 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
555 (ctx->num_blocks < ext2fs_blocks_count(sb))) {
556 pctx.blk = ext2fs_blocks_count(sb);
557 pctx.blk2 = ctx->num_blocks;
558 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
559 ctx->flags |= E2F_FLAG_ABORT;
564 should_be = (sb->s_log_block_size == 0 &&
565 EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0;
566 if (sb->s_first_data_block != should_be) {
567 pctx.blk = sb->s_first_data_block;
568 pctx.blk2 = should_be;
569 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
570 ctx->flags |= E2F_FLAG_ABORT;
574 should_be = sb->s_inodes_per_group * fs->group_desc_count;
575 if (sb->s_inodes_count != should_be) {
576 pctx.ino = sb->s_inodes_count;
577 pctx.ino2 = should_be;
578 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
579 sb->s_inodes_count = should_be;
580 ext2fs_mark_super_dirty(fs);
585 * Verify the group descriptors....
587 first_block = sb->s_first_data_block;
588 last_block = ext2fs_blocks_count(sb)-1;
590 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
591 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
592 for (i = 0; i < fs->group_desc_count; i++) {
595 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
596 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
597 first_block = ext2fs_group_first_block2(fs, i);
598 last_block = ext2fs_group_last_block2(fs, i);
601 if ((ext2fs_block_bitmap_loc(fs, i) < first_block) ||
602 (ext2fs_block_bitmap_loc(fs, i) > last_block)) {
603 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
604 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
605 ext2fs_block_bitmap_loc_set(fs, i, 0);
607 if (ext2fs_block_bitmap_loc(fs, i) == 0) {
608 ctx->invalid_block_bitmap_flag[i]++;
609 ctx->invalid_bitmaps++;
611 if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) ||
612 (ext2fs_inode_bitmap_loc(fs, i) > last_block)) {
613 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
614 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
615 ext2fs_inode_bitmap_loc_set(fs, i, 0);
617 if (ext2fs_inode_bitmap_loc(fs, i) == 0) {
618 ctx->invalid_inode_bitmap_flag[i]++;
619 ctx->invalid_bitmaps++;
621 if ((ext2fs_inode_table_loc(fs, i) < first_block) ||
622 ((ext2fs_inode_table_loc(fs, i) +
623 fs->inode_blocks_per_group - 1) > last_block)) {
624 pctx.blk = ext2fs_inode_table_loc(fs, i);
625 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
626 ext2fs_inode_table_loc_set(fs, i, 0);
628 if (ext2fs_inode_table_loc(fs, i) == 0) {
629 ctx->invalid_inode_table_flag[i]++;
630 ctx->invalid_bitmaps++;
632 free_blocks += ext2fs_bg_free_blocks_count(fs, i);
633 free_inodes += ext2fs_bg_free_inodes_count(fs, i);
635 if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) ||
636 (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) ||
637 (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group))
638 ext2fs_unmark_valid(fs);
641 if (!ext2fs_group_desc_csum_verify(fs, i)) {
642 pctx.csum1 = ext2fs_bg_checksum(fs, i);
643 pctx.csum2 = ext2fs_group_desc_csum(fs, i);
644 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
645 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
646 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
647 ext2fs_bg_itable_unused_set(fs, i, 0);
650 ext2fs_unmark_valid(fs);
654 (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ||
655 ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ||
656 ext2fs_bg_itable_unused(fs, i) != 0)) {
657 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
658 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
659 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
660 ext2fs_bg_itable_unused_set(fs, i, 0);
663 ext2fs_unmark_valid(fs);
666 if (i == fs->group_desc_count - 1 &&
667 ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) {
668 if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
669 ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
672 ext2fs_unmark_valid(fs);
676 (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) ||
677 ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) {
678 pctx.blk = ext2fs_bg_itable_unused(fs, i);
679 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
680 ext2fs_bg_itable_unused_set(fs, i, 0);
683 ext2fs_unmark_valid(fs);
687 ext2fs_group_desc_csum_set(fs, i);
688 /* If the user aborts e2fsck by typing ^C, stop right away */
689 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
693 ctx->free_blocks = EXT2FS_C2B(fs, free_blocks);
694 ctx->free_inodes = free_inodes;
696 if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
697 (sb->s_free_inodes_count > sb->s_inodes_count))
698 ext2fs_unmark_valid(fs);
702 * If we have invalid bitmaps, set the error state of the
705 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
706 sb->s_state &= ~EXT2_VALID_FS;
707 ext2fs_mark_super_dirty(fs);
710 clear_problem_context(&pctx);
712 #ifndef EXT2_SKIP_UUID
714 * If the UUID field isn't assigned, assign it.
716 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
717 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
718 uuid_generate(sb->s_uuid);
719 ext2fs_init_csum_seed(fs);
720 fs->flags |= EXT2_FLAG_DIRTY;
721 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
727 * Check to see if we should disable the test_fs flag
729 profile_get_boolean(ctx->profile, "options",
730 "clear_test_fs_flag", 0, 1,
731 &clear_test_fs_flag);
732 if (!(ctx->options & E2F_OPT_READONLY) &&
733 clear_test_fs_flag &&
734 (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) &&
735 (fs_proc_check("ext4") || check_for_modules("ext4"))) {
736 if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
737 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
738 fs->flags |= EXT2_FLAG_DIRTY;
739 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
744 * For the Hurd, check to see if the filetype option is set,
745 * since it doesn't support it.
747 if (!(ctx->options & E2F_OPT_READONLY) &&
748 fs->super->s_creator_os == EXT2_OS_HURD &&
749 (fs->super->s_feature_incompat &
750 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
751 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
752 fs->super->s_feature_incompat &=
753 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
754 ext2fs_mark_super_dirty(fs);
755 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
760 * If we have any of the compatibility flags set, we need to have a
761 * revision 1 filesystem. Most kernels will not check the flags on
762 * a rev 0 filesystem and we may have corruption issues because of
763 * the incompatible changes to the filesystem.
765 if (!(ctx->options & E2F_OPT_READONLY) &&
766 fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
767 (fs->super->s_feature_compat ||
768 fs->super->s_feature_ro_compat ||
769 fs->super->s_feature_incompat) &&
770 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
771 ext2fs_update_dynamic_rev(fs);
772 ext2fs_mark_super_dirty(fs);
773 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
777 * Clean up any orphan inodes, if present.
779 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
780 fs->super->s_state &= ~EXT2_VALID_FS;
781 ext2fs_mark_super_dirty(fs);
785 * Unfortunately, due to Windows' unfortunate design decision
786 * to configure the hardware clock to tick localtime, instead
787 * of the more proper and less error-prone UTC time, many
788 * users end up in the situation where the system clock is
789 * incorrectly set at the time when e2fsck is run.
791 * Historically this was usually due to some distributions
792 * having buggy init scripts and/or installers that didn't
793 * correctly detect this case and take appropriate
794 * countermeasures. However, it's still possible, despite the
795 * best efforts of init script and installer authors to not be
796 * able to detect this misconfiguration, usually due to a
797 * buggy or misconfigured virtualization manager or the
798 * installer not having access to a network time server during
799 * the installation process. So by default, we allow the
800 * superblock times to be fudged by up to 24 hours. This can
801 * be disabled by setting options.accept_time_fudge to the
802 * boolean value of false in e2fsck.conf. We also support
803 * options.buggy_init_scripts for backwards compatibility.
805 profile_get_boolean(ctx->profile, "options", "accept_time_fudge",
806 0, 1, &accept_time_fudge);
807 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
808 0, accept_time_fudge, &accept_time_fudge);
809 ctx->time_fudge = accept_time_fudge ? 86400 : 0;
811 profile_get_boolean(ctx->profile, "options", "broken_system_clock",
812 0, 0, &broken_system_clock);
815 * Check to see if the superblock last mount time or last
816 * write time is in the future.
818 if (!broken_system_clock &&
819 !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
820 fs->super->s_mtime > (__u32) ctx->now) {
821 pctx.num = fs->super->s_mtime;
822 problem = PR_0_FUTURE_SB_LAST_MOUNT;
823 if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
824 problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
825 if (fix_problem(ctx, problem, &pctx)) {
826 fs->super->s_mtime = ctx->now;
827 fs->flags |= EXT2_FLAG_DIRTY;
830 if (!broken_system_clock &&
831 !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
832 fs->super->s_wtime > (__u32) ctx->now) {
833 pctx.num = fs->super->s_wtime;
834 problem = PR_0_FUTURE_SB_LAST_WRITE;
835 if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
836 problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
837 if (fix_problem(ctx, problem, &pctx)) {
838 fs->super->s_wtime = ctx->now;
839 fs->flags |= EXT2_FLAG_DIRTY;
844 * Move the ext3 journal file, if necessary.
846 e2fsck_move_ext3_journal(ctx);
849 * Fix journal hint, if necessary
851 e2fsck_fix_ext3_journal_hint(ctx);
854 * Add dirhash hint if necessary
856 e2fsck_fix_dirhash_hint(ctx);
859 * Hide quota inodes if necessary.
861 e2fsck_hide_quota(ctx);
867 * Check to see if we should backup the master sb to the backup super
868 * blocks. Returns non-zero if the sb should be backed up.
872 * A few flags are set on the fly by the kernel, but only in the
873 * primary superblock. This is actually a bad thing, and we should
874 * try to discourage it in the future. In particular, for the newer
875 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
876 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
877 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
878 * copying the primary superblock during online resize.
880 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
881 * unfortunately, we shouldn't ignore it since if it's not set in the
882 * backup, the extended attributes in the filesystem will be stripped
885 #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
886 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
887 #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \
888 EXT3_FEATURE_INCOMPAT_RECOVER)
890 int check_backup_super_block(e2fsck_t ctx)
892 ext2_filsys fs = ctx->fs;
897 char buf[SUPERBLOCK_SIZE];
898 struct ext2_super_block *backup_sb;
901 * If we are already writing out the backup blocks, then we
902 * don't need to test. Also, if the filesystem is invalid, or
903 * the check was aborted or cancelled, we also don't want to
904 * do the backup. If the filesystem was opened read-only then
905 * we can't do the backup.
907 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
908 !ext2fs_test_valid(fs) ||
909 (fs->super->s_state & EXT2_ERROR_FS) ||
910 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
911 (ctx->options & E2F_OPT_READONLY))
914 for (g = 1; g < fs->group_desc_count; g++) {
915 if (!ext2fs_bg_has_super(fs, g))
918 sb = fs->super->s_first_data_block +
919 (g * fs->super->s_blocks_per_group);
921 retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE,
925 backup_sb = (struct ext2_super_block *) buf;
926 #ifdef WORDS_BIGENDIAN
927 ext2fs_swap_super(backup_sb);
929 if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) ||
930 (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) ||
931 ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
932 EXT2_MAX_BLOCK_LOG_SIZE) ||
933 (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE))
936 #define SUPER_INCOMPAT_DIFFERENT(x) \
937 ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
938 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
939 #define SUPER_RO_COMPAT_DIFFERENT(x) \
940 ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
941 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
942 #define SUPER_DIFFERENT(x) \
943 (fs->super->x != backup_sb->x)
945 if (SUPER_DIFFERENT(s_feature_compat) ||
946 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
947 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
948 SUPER_DIFFERENT(s_blocks_count) ||
949 SUPER_DIFFERENT(s_inodes_count) ||
950 memcmp(fs->super->s_uuid, backup_sb->s_uuid,
951 sizeof(fs->super->s_uuid)))