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 >= fs->super->s_blocks_count)) {
81 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
87 if (!ext2fs_test_block_bitmap(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_blk(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_blk(fs->io, blk, 1,
132 memset(pb->buf + pb->truncate_offset, 0,
133 fs->blocksize - pb->truncate_offset);
134 pb->errcode = io_channel_write_blk(fs->io, blk, 1,
139 pb->truncated_blocks++;
141 retval |= BLOCK_CHANGED;
144 ext2fs_block_alloc_stats(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 (inode->i_file_acl) {
201 retval = ext2fs_adjust_ea_refcount(fs, inode->i_file_acl,
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_stats(fs, inode->i_file_acl, -1);
215 inode->i_file_acl = 0;
221 * This function releases all of the orphan inodes. It returns 1 if
222 * it hit some error, and 0 on success.
224 static int release_orphan_inodes(e2fsck_t ctx)
226 ext2_filsys fs = ctx->fs;
227 ext2_ino_t ino, next_ino;
228 struct ext2_inode inode;
229 struct problem_context pctx;
232 if ((ino = fs->super->s_last_orphan) == 0)
236 * Win or lose, we won't be using the head of the orphan inode
239 fs->super->s_last_orphan = 0;
240 ext2fs_mark_super_dirty(fs);
243 * If the filesystem contains errors, don't run the orphan
244 * list, since the orphan list can't be trusted; and we're
245 * going to be running a full e2fsck run anyway...
247 if (fs->super->s_state & EXT2_ERROR_FS)
250 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
251 (ino > fs->super->s_inodes_count)) {
252 clear_problem_context(&pctx);
254 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
258 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
259 "block iterate buffer");
260 e2fsck_read_bitmaps(ctx);
263 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
264 clear_problem_context(&pctx);
267 pctx.str = inode.i_links_count ? _("Truncating") :
270 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
272 next_ino = inode.i_dtime;
274 ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
275 (next_ino > fs->super->s_inodes_count))) {
277 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
281 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
284 if (!inode.i_links_count) {
285 ext2fs_inode_alloc_stats2(fs, ino, -1,
286 LINUX_S_ISDIR(inode.i_mode));
287 inode.i_dtime = ctx->now;
291 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
294 ext2fs_free_mem(&block_buf);
297 ext2fs_free_mem(&block_buf);
302 * Check the resize inode to make sure it is sane. We check both for
303 * the case where on-line resizing is not enabled (in which case the
304 * resize inode should be cleared) as well as the case where on-line
305 * resizing is enabled.
307 void check_resize_inode(e2fsck_t ctx)
309 ext2_filsys fs = ctx->fs;
310 struct ext2_inode inode;
311 struct problem_context pctx;
312 int i, gdt_off, ind_off;
314 blk_t blk, pblk, expect;
315 __u32 *dind_buf = 0, *ind_buf;
318 clear_problem_context(&pctx);
321 * If the resize inode feature isn't set, then
322 * s_reserved_gdt_blocks must be zero.
324 if (!(fs->super->s_feature_compat &
325 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
326 if (fs->super->s_reserved_gdt_blocks) {
327 pctx.num = fs->super->s_reserved_gdt_blocks;
328 if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
330 fs->super->s_reserved_gdt_blocks = 0;
331 ext2fs_mark_super_dirty(fs);
336 /* Read the resize inode */
337 pctx.ino = EXT2_RESIZE_INO;
338 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
340 if (fs->super->s_feature_compat &
341 EXT2_FEATURE_COMPAT_RESIZE_INODE)
342 ctx->flags |= E2F_FLAG_RESIZE_INODE;
347 * If the resize inode feature isn't set, check to make sure
348 * the resize inode is cleared; then we're done.
350 if (!(fs->super->s_feature_compat &
351 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
352 for (i=0; i < EXT2_N_BLOCKS; i++) {
353 if (inode.i_block[i])
356 if ((i < EXT2_N_BLOCKS) &&
357 fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
358 memset(&inode, 0, sizeof(inode));
359 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
366 * The resize inode feature is enabled; check to make sure the
367 * only block in use is the double indirect block
369 blk = inode.i_block[EXT2_DIND_BLOCK];
370 for (i=0; i < EXT2_N_BLOCKS; i++) {
371 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
374 if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
375 !(inode.i_mode & LINUX_S_IFREG) ||
376 (blk < fs->super->s_first_data_block ||
377 blk >= fs->super->s_blocks_count)) {
378 resize_inode_invalid:
379 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
380 memset(&inode, 0, sizeof(inode));
381 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
383 ctx->flags |= E2F_FLAG_RESIZE_INODE;
385 if (!(ctx->options & E2F_OPT_READONLY)) {
386 fs->super->s_state &= ~EXT2_VALID_FS;
387 ext2fs_mark_super_dirty(fs);
391 dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
392 "resize dind buffer");
393 ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
395 retval = ext2fs_read_ind_block(fs, blk, dind_buf);
397 goto resize_inode_invalid;
399 gdt_off = fs->desc_blocks;
400 pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
401 for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
402 i++, gdt_off++, pblk++) {
403 gdt_off %= fs->blocksize/4;
404 if (dind_buf[gdt_off] != pblk)
405 goto resize_inode_invalid;
406 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
408 goto resize_inode_invalid;
410 for (j = 1; j < fs->group_desc_count; j++) {
411 if (!ext2fs_bg_has_super(fs, j))
413 expect = pblk + (j * fs->super->s_blocks_per_group);
414 if (ind_buf[ind_off] != expect)
415 goto resize_inode_invalid;
422 ext2fs_free_mem(&dind_buf);
427 * This function checks the dirhash signed/unsigned hint if necessary.
429 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
431 struct ext2_super_block *sb = ctx->fs->super;
432 struct problem_context pctx;
435 if ((ctx->options & E2F_OPT_READONLY) ||
436 !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
437 (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
442 clear_problem_context(&pctx);
443 if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
444 if (((int) c) == -1) {
445 sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
447 sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
449 ext2fs_mark_super_dirty(ctx->fs);
454 void check_super_block(e2fsck_t ctx)
456 ext2_filsys fs = ctx->fs;
457 blk_t first_block, last_block;
458 struct ext2_super_block *sb = fs->super;
459 struct ext2_group_desc *gd;
460 blk_t blocks_per_group = fs->super->s_blocks_per_group;
462 int inodes_per_block;
465 int buggy_init_scripts;
468 struct problem_context pctx;
469 blk_t free_blocks = 0;
470 ino_t free_inodes = 0;
471 int csum_flag, clear_test_fs_flag;
473 inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
474 ipg_max = inodes_per_block * (blocks_per_group - 4);
475 if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
476 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
477 bpg_max = 8 * EXT2_BLOCK_SIZE(sb);
478 if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
479 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
481 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
482 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
483 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
484 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
485 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
486 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
488 clear_problem_context(&pctx);
491 * Verify the super block constants...
493 check_super_value(ctx, "inodes_count", sb->s_inodes_count,
495 check_super_value(ctx, "blocks_count", sb->s_blocks_count,
497 check_super_value(ctx, "first_data_block", sb->s_first_data_block,
498 MAX_CHECK, 0, sb->s_blocks_count);
499 check_super_value(ctx, "log_block_size", sb->s_log_block_size,
500 MIN_CHECK | MAX_CHECK, 0,
501 EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
502 check_super_value(ctx, "log_frag_size", sb->s_log_frag_size,
503 MIN_CHECK | MAX_CHECK, 0, sb->s_log_block_size);
504 check_super_value(ctx, "frags_per_group", sb->s_frags_per_group,
505 MIN_CHECK | MAX_CHECK, sb->s_blocks_per_group,
507 check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
508 MIN_CHECK | MAX_CHECK, 8, bpg_max);
509 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
510 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
511 check_super_value(ctx, "r_blocks_count", sb->s_r_blocks_count,
512 MAX_CHECK, 0, sb->s_blocks_count / 2);
513 check_super_value(ctx, "reserved_gdt_blocks",
514 sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
516 if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
517 check_super_value(ctx, "first_ino", sb->s_first_ino,
518 MIN_CHECK | MAX_CHECK,
519 EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count);
520 inode_size = EXT2_INODE_SIZE(sb);
521 check_super_value(ctx, "inode_size",
522 inode_size, MIN_CHECK | MAX_CHECK,
523 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
524 if (inode_size & (inode_size - 1)) {
525 pctx.num = inode_size;
526 pctx.str = "inode_size";
527 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
528 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
532 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
533 (ctx->num_blocks < sb->s_blocks_count)) {
534 pctx.blk = sb->s_blocks_count;
535 pctx.blk2 = ctx->num_blocks;
536 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
537 ctx->flags |= E2F_FLAG_ABORT;
542 if (sb->s_log_block_size != (__u32) sb->s_log_frag_size) {
543 pctx.blk = EXT2_BLOCK_SIZE(sb);
544 pctx.blk2 = EXT2_FRAG_SIZE(sb);
545 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
546 ctx->flags |= E2F_FLAG_ABORT;
550 should_be = sb->s_frags_per_group >>
551 (sb->s_log_block_size - sb->s_log_frag_size);
552 if (sb->s_blocks_per_group != should_be) {
553 pctx.blk = sb->s_blocks_per_group;
554 pctx.blk2 = should_be;
555 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
556 ctx->flags |= E2F_FLAG_ABORT;
560 should_be = (sb->s_log_block_size == 0) ? 1 : 0;
561 if (sb->s_first_data_block != should_be) {
562 pctx.blk = sb->s_first_data_block;
563 pctx.blk2 = should_be;
564 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
565 ctx->flags |= E2F_FLAG_ABORT;
569 should_be = sb->s_inodes_per_group * fs->group_desc_count;
570 if (sb->s_inodes_count != should_be) {
571 pctx.ino = sb->s_inodes_count;
572 pctx.ino2 = should_be;
573 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
574 sb->s_inodes_count = should_be;
575 ext2fs_mark_super_dirty(fs);
580 * Verify the group descriptors....
582 first_block = sb->s_first_data_block;
583 last_block = sb->s_blocks_count-1;
585 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
586 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
587 for (i = 0, gd=fs->group_desc; i < fs->group_desc_count; i++, gd++) {
590 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
591 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
592 first_block = ext2fs_group_first_block(fs, i);
593 last_block = ext2fs_group_last_block(fs, i);
596 if ((gd->bg_block_bitmap < first_block) ||
597 (gd->bg_block_bitmap > last_block)) {
598 pctx.blk = gd->bg_block_bitmap;
599 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
600 gd->bg_block_bitmap = 0;
602 if (gd->bg_block_bitmap == 0) {
603 ctx->invalid_block_bitmap_flag[i]++;
604 ctx->invalid_bitmaps++;
606 if ((gd->bg_inode_bitmap < first_block) ||
607 (gd->bg_inode_bitmap > last_block)) {
608 pctx.blk = gd->bg_inode_bitmap;
609 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
610 gd->bg_inode_bitmap = 0;
612 if (gd->bg_inode_bitmap == 0) {
613 ctx->invalid_inode_bitmap_flag[i]++;
614 ctx->invalid_bitmaps++;
616 if ((gd->bg_inode_table < first_block) ||
617 ((gd->bg_inode_table +
618 fs->inode_blocks_per_group - 1) > last_block)) {
619 pctx.blk = gd->bg_inode_table;
620 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
621 gd->bg_inode_table = 0;
623 if (gd->bg_inode_table == 0) {
624 ctx->invalid_inode_table_flag[i]++;
625 ctx->invalid_bitmaps++;
627 free_blocks += gd->bg_free_blocks_count;
628 free_inodes += gd->bg_free_inodes_count;
630 if ((gd->bg_free_blocks_count > sb->s_blocks_per_group) ||
631 (gd->bg_free_inodes_count > sb->s_inodes_per_group) ||
632 (gd->bg_used_dirs_count > sb->s_inodes_per_group))
633 ext2fs_unmark_valid(fs);
636 if (!ext2fs_group_desc_csum_verify(fs, i)) {
637 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
638 gd->bg_flags &= ~(EXT2_BG_BLOCK_UNINIT |
639 EXT2_BG_INODE_UNINIT);
640 gd->bg_itable_unused = 0;
643 ext2fs_unmark_valid(fs);
647 (gd->bg_flags &(EXT2_BG_BLOCK_UNINIT|EXT2_BG_INODE_UNINIT)||
648 gd->bg_itable_unused != 0)){
649 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
650 gd->bg_flags &= ~(EXT2_BG_BLOCK_UNINIT |
651 EXT2_BG_INODE_UNINIT);
652 gd->bg_itable_unused = 0;
655 ext2fs_unmark_valid(fs);
658 if (i == fs->group_desc_count - 1 &&
659 gd->bg_flags & EXT2_BG_BLOCK_UNINIT) {
660 if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
661 gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
664 ext2fs_unmark_valid(fs);
667 if (gd->bg_flags & EXT2_BG_BLOCK_UNINIT &&
668 !(gd->bg_flags & EXT2_BG_INODE_UNINIT)) {
669 if (fix_problem(ctx, PR_0_BB_UNINIT_IB_INIT, &pctx)) {
670 gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
673 ext2fs_unmark_valid(fs);
677 (gd->bg_itable_unused > gd->bg_free_inodes_count ||
678 gd->bg_itable_unused > sb->s_inodes_per_group)) {
679 pctx.blk = gd->bg_itable_unused;
680 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
681 gd->bg_itable_unused = 0;
684 ext2fs_unmark_valid(fs);
688 ext2fs_group_desc_csum_set(fs, i);
692 * Update the global counts from the block group counts. This
693 * is needed for an experimental patch which eliminates
694 * locking the entire filesystem when allocating blocks or
695 * inodes; if the filesystem is not unmounted cleanly, the
696 * global counts may not be accurate.
698 if ((free_blocks != sb->s_free_blocks_count) ||
699 (free_inodes != sb->s_free_inodes_count)) {
700 if (ctx->options & E2F_OPT_READONLY)
701 ext2fs_unmark_valid(fs);
703 sb->s_free_blocks_count = free_blocks;
704 sb->s_free_inodes_count = free_inodes;
705 ext2fs_mark_super_dirty(fs);
709 if ((sb->s_free_blocks_count > sb->s_blocks_count) ||
710 (sb->s_free_inodes_count > sb->s_inodes_count))
711 ext2fs_unmark_valid(fs);
715 * If we have invalid bitmaps, set the error state of the
718 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
719 sb->s_state &= ~EXT2_VALID_FS;
720 ext2fs_mark_super_dirty(fs);
723 clear_problem_context(&pctx);
725 #ifndef EXT2_SKIP_UUID
727 * If the UUID field isn't assigned, assign it.
729 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
730 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
731 uuid_generate(sb->s_uuid);
732 ext2fs_mark_super_dirty(fs);
733 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
739 * Check to see if we should disable the test_fs flag
741 profile_get_boolean(ctx->profile, "options",
742 "clear_test_fs_flag", 0, 1,
743 &clear_test_fs_flag);
744 if (!(ctx->options & E2F_OPT_READONLY) &&
745 clear_test_fs_flag &&
746 (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) &&
747 (fs_proc_check("ext4") || check_for_modules("ext4"))) {
748 if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
749 fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
750 ext2fs_mark_super_dirty(fs);
751 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
756 * For the Hurd, check to see if the filetype option is set,
757 * since it doesn't support it.
759 if (!(ctx->options & E2F_OPT_READONLY) &&
760 fs->super->s_creator_os == EXT2_OS_HURD &&
761 (fs->super->s_feature_incompat &
762 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
763 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
764 fs->super->s_feature_incompat &=
765 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
766 ext2fs_mark_super_dirty(fs);
767 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
772 * If we have any of the compatibility flags set, we need to have a
773 * revision 1 filesystem. Most kernels will not check the flags on
774 * a rev 0 filesystem and we may have corruption issues because of
775 * the incompatible changes to the filesystem.
777 if (!(ctx->options & E2F_OPT_READONLY) &&
778 fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
779 (fs->super->s_feature_compat ||
780 fs->super->s_feature_ro_compat ||
781 fs->super->s_feature_incompat) &&
782 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
783 ext2fs_update_dynamic_rev(fs);
784 ext2fs_mark_super_dirty(fs);
785 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
789 * Clean up any orphan inodes, if present.
791 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
792 fs->super->s_state &= ~EXT2_VALID_FS;
793 ext2fs_mark_super_dirty(fs);
797 * Some buggy distributions (such as Ubuntu) have init scripts
798 * and/or installers which fail to correctly set the system
799 * clock before running e2fsck and/or formatting the
800 * filesystem initially. Normally this happens because the
801 * hardware clock is ticking localtime, instead of the more
802 * proper and less error-prone UTC time. So while the kernel
803 * is booting, the system time (which in Linux systems always
804 * ticks in UTC time) is set from the hardware clock, but
805 * since the hardware clock is ticking localtime, the system
806 * time is incorrect. Unfortunately, some buggy distributions
807 * do not correct this before running e2fsck. If this option
808 * is set to a boolean value of true, we attempt to work
809 * around this situation by allowing the superblock last write
810 * time, last mount time, and last check time to be in the
811 * future by up to 24 hours.
813 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
814 0, 0, &buggy_init_scripts);
815 ctx->time_fudge = buggy_init_scripts ? 86400 : 0;
818 * Check to see if the superblock last mount time or last
819 * write time is in the future.
821 if (fs->super->s_mtime > (__u32) ctx->now + ctx->time_fudge) {
822 pctx.num = fs->super->s_mtime;
823 if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_MOUNT, &pctx)) {
824 fs->super->s_mtime = ctx->now;
825 ext2fs_mark_super_dirty(fs);
828 if (fs->super->s_wtime > (__u32) ctx->now + ctx->time_fudge) {
829 pctx.num = fs->super->s_wtime;
830 if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_WRITE, &pctx)) {
831 fs->super->s_wtime = ctx->now;
832 ext2fs_mark_super_dirty(fs);
837 * Move the ext3 journal file, if necessary.
839 e2fsck_move_ext3_journal(ctx);
842 * Fix journal hint, if necessary
844 e2fsck_fix_ext3_journal_hint(ctx);
847 * Add dirhash hint if necessary
849 e2fsck_fix_dirhash_hint(ctx);
855 * Check to see if we should backup the master sb to the backup super
856 * blocks. Returns non-zero if the sb should be backed up.
860 * A few flags are set on the fly by the kernel, but only in the
861 * primary superblock. This is actually a bad thing, and we should
862 * try to discourage it in the future. In particular, for the newer
863 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
864 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
865 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
866 * copying the primary superblock during online resize.
868 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
869 * unfortunately, we shouldn't ignore it since if it's not set in the
870 * backup, the extended attributes in the filesystem will be stripped
873 #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
874 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
875 #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \
876 EXT3_FEATURE_INCOMPAT_RECOVER)
878 int check_backup_super_block(e2fsck_t ctx)
880 ext2_filsys fs = ctx->fs;
888 * If we are already writing out the backup blocks, then we
889 * don't need to test. Also, if the filesystem is invalid, or
890 * the check was aborted or cancelled, we also don't want to
891 * do the backup. If the filesystem was opened read-only then
892 * we can't do the backup.
894 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
895 !ext2fs_test_valid(fs) ||
896 (fs->super->s_state & EXT2_ERROR_FS) ||
897 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
898 (ctx->options & E2F_OPT_READONLY))
901 for (g = 1; g < fs->group_desc_count; g++) {
902 if (!ext2fs_bg_has_super(fs, g))
905 sb = fs->super->s_first_data_block +
906 (g * fs->super->s_blocks_per_group);
908 retval = ext2fs_open(ctx->filesystem_name, 0,
910 fs->io->manager, &tfs);
916 #define SUPER_INCOMPAT_DIFFERENT(x) \
917 (( fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
918 (tfs->super->x & ~FEATURE_INCOMPAT_IGNORE))
919 #define SUPER_RO_COMPAT_DIFFERENT(x) \
920 (( fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
921 (tfs->super->x & ~FEATURE_RO_COMPAT_IGNORE))
922 #define SUPER_DIFFERENT(x) \
923 (fs->super->x != tfs->super->x)
925 if (SUPER_DIFFERENT(s_feature_compat) ||
926 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
927 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
928 SUPER_DIFFERENT(s_blocks_count) ||
929 SUPER_DIFFERENT(s_inodes_count) ||
930 memcmp(fs->super->s_uuid, tfs->super->s_uuid,
931 sizeof(fs->super->s_uuid)))