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 inode->i_blocks -= pb.truncated_blocks *
199 (fs->blocksize / 512);
201 if (inode->i_file_acl) {
202 retval = ext2fs_adjust_ea_refcount(fs, inode->i_file_acl,
203 block_buf, -1, &count);
204 if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
209 com_err("release_inode_blocks", retval,
210 _("while calling ext2fs_adjust_ea_refcount for inode %d"),
215 ext2fs_block_alloc_stats(fs, inode->i_file_acl, -1);
216 inode->i_file_acl = 0;
222 * This function releases all of the orphan inodes. It returns 1 if
223 * it hit some error, and 0 on success.
225 static int release_orphan_inodes(e2fsck_t ctx)
227 ext2_filsys fs = ctx->fs;
228 ext2_ino_t ino, next_ino;
229 struct ext2_inode inode;
230 struct problem_context pctx;
233 if ((ino = fs->super->s_last_orphan) == 0)
237 * Win or lose, we won't be using the head of the orphan inode
240 fs->super->s_last_orphan = 0;
241 ext2fs_mark_super_dirty(fs);
244 * If the filesystem contains errors, don't run the orphan
245 * list, since the orphan list can't be trusted; and we're
246 * going to be running a full e2fsck run anyway...
248 if (fs->super->s_state & EXT2_ERROR_FS)
251 if ((ino < EXT2_FIRST_INODE(fs->super)) ||
252 (ino > fs->super->s_inodes_count)) {
253 clear_problem_context(&pctx);
255 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
259 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
260 "block iterate buffer");
261 e2fsck_read_bitmaps(ctx);
264 e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes");
265 clear_problem_context(&pctx);
268 pctx.str = inode.i_links_count ? _("Truncating") :
271 fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
273 next_ino = inode.i_dtime;
275 ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
276 (next_ino > fs->super->s_inodes_count))) {
278 fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
282 if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
285 if (!inode.i_links_count) {
286 ext2fs_inode_alloc_stats2(fs, ino, -1,
287 LINUX_S_ISDIR(inode.i_mode));
288 inode.i_dtime = ctx->now;
292 e2fsck_write_inode(ctx, ino, &inode, "delete_file");
295 ext2fs_free_mem(&block_buf);
298 ext2fs_free_mem(&block_buf);
303 * Check the resize inode to make sure it is sane. We check both for
304 * the case where on-line resizing is not enabled (in which case the
305 * resize inode should be cleared) as well as the case where on-line
306 * resizing is enabled.
308 static void check_resize_inode(e2fsck_t ctx)
310 ext2_filsys fs = ctx->fs;
311 struct ext2_inode inode;
312 struct problem_context pctx;
313 int i, gdt_off, ind_off;
315 blk_t blk, pblk, expect;
316 __u32 *dind_buf = 0, *ind_buf;
319 clear_problem_context(&pctx);
322 * If the resize inode feature isn't set, then
323 * s_reserved_gdt_blocks must be zero.
325 if (!(fs->super->s_feature_compat &
326 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
327 if (fs->super->s_reserved_gdt_blocks) {
328 pctx.num = fs->super->s_reserved_gdt_blocks;
329 if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
331 fs->super->s_reserved_gdt_blocks = 0;
332 ext2fs_mark_super_dirty(fs);
337 /* Read the resize inode */
338 pctx.ino = EXT2_RESIZE_INO;
339 retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
341 if (fs->super->s_feature_compat &
342 EXT2_FEATURE_COMPAT_RESIZE_INODE)
343 ctx->flags |= E2F_FLAG_RESIZE_INODE;
348 * If the resize inode feature isn't set, check to make sure
349 * the resize inode is cleared; then we're done.
351 if (!(fs->super->s_feature_compat &
352 EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
353 for (i=0; i < EXT2_N_BLOCKS; i++) {
354 if (inode.i_block[i])
357 if ((i < EXT2_N_BLOCKS) &&
358 fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
359 memset(&inode, 0, sizeof(inode));
360 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
367 * The resize inode feature is enabled; check to make sure the
368 * only block in use is the double indirect block
370 blk = inode.i_block[EXT2_DIND_BLOCK];
371 for (i=0; i < EXT2_N_BLOCKS; i++) {
372 if (i != EXT2_DIND_BLOCK && inode.i_block[i])
375 if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
376 !(inode.i_mode & LINUX_S_IFREG) ||
377 (blk < fs->super->s_first_data_block ||
378 blk >= fs->super->s_blocks_count)) {
379 resize_inode_invalid:
380 if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
381 memset(&inode, 0, sizeof(inode));
382 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
384 ctx->flags |= E2F_FLAG_RESIZE_INODE;
386 if (!(ctx->options & E2F_OPT_READONLY)) {
387 fs->super->s_state &= ~EXT2_VALID_FS;
388 ext2fs_mark_super_dirty(fs);
392 dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
393 "resize dind buffer");
394 ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
396 retval = ext2fs_read_ind_block(fs, blk, dind_buf);
398 goto resize_inode_invalid;
400 gdt_off = fs->desc_blocks;
401 pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
402 for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
403 i++, gdt_off++, pblk++) {
404 gdt_off %= fs->blocksize/4;
405 if (dind_buf[gdt_off] != pblk)
406 goto resize_inode_invalid;
407 retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
409 goto resize_inode_invalid;
411 for (j = 1; j < fs->group_desc_count; j++) {
412 if (!ext2fs_bg_has_super(fs, j))
414 expect = pblk + (j * fs->super->s_blocks_per_group);
415 if (ind_buf[ind_off] != expect)
416 goto resize_inode_invalid;
423 ext2fs_free_mem(&dind_buf);
428 * This function checks the dirhash signed/unsigned hint if necessary.
430 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
432 struct ext2_super_block *sb = ctx->fs->super;
433 struct problem_context pctx;
436 if ((ctx->options & E2F_OPT_READONLY) ||
437 !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
438 (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
443 clear_problem_context(&pctx);
444 if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
445 if (((int) c) == -1) {
446 sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
448 sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
450 ext2fs_mark_super_dirty(ctx->fs);
455 void check_super_block(e2fsck_t ctx)
457 ext2_filsys fs = ctx->fs;
458 blk_t first_block, last_block;
459 struct ext2_super_block *sb = fs->super;
460 struct ext2_group_desc *gd;
461 blk_t blocks_per_group = fs->super->s_blocks_per_group;
463 int inodes_per_block;
466 int buggy_init_scripts;
469 struct problem_context pctx;
470 blk_t free_blocks = 0;
471 ino_t free_inodes = 0;
472 int lazy_flag, csum_flag;
474 inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
475 ipg_max = inodes_per_block * (blocks_per_group - 4);
476 if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
477 ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
478 bpg_max = 8 * EXT2_BLOCK_SIZE(sb);
479 if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
480 bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
482 ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
483 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
484 ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
485 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
486 ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
487 sizeof(int) * fs->group_desc_count, "invalid_inode_table");
489 clear_problem_context(&pctx);
492 * Verify the super block constants...
494 check_super_value(ctx, "inodes_count", sb->s_inodes_count,
496 check_super_value(ctx, "blocks_count", sb->s_blocks_count,
498 check_super_value(ctx, "first_data_block", sb->s_first_data_block,
499 MAX_CHECK, 0, sb->s_blocks_count);
500 check_super_value(ctx, "log_block_size", sb->s_log_block_size,
501 MIN_CHECK | MAX_CHECK, 0,
502 EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE);
503 check_super_value(ctx, "log_frag_size", sb->s_log_frag_size,
504 MIN_CHECK | MAX_CHECK, 0, sb->s_log_block_size);
505 check_super_value(ctx, "frags_per_group", sb->s_frags_per_group,
506 MIN_CHECK | MAX_CHECK, sb->s_blocks_per_group,
508 check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
509 MIN_CHECK | MAX_CHECK, 8, bpg_max);
510 check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
511 MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max);
512 check_super_value(ctx, "r_blocks_count", sb->s_r_blocks_count,
513 MAX_CHECK, 0, sb->s_blocks_count / 2);
514 check_super_value(ctx, "reserved_gdt_blocks",
515 sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
517 inode_size = EXT2_INODE_SIZE(sb);
518 check_super_value(ctx, "inode_size",
519 inode_size, MIN_CHECK | MAX_CHECK,
520 EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize);
521 if (inode_size & (inode_size - 1)) {
522 pctx.num = inode_size;
523 pctx.str = "inode_size";
524 fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
525 ctx->flags |= E2F_FLAG_ABORT; /* never get here! */
529 if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
530 (ctx->num_blocks < sb->s_blocks_count)) {
531 pctx.blk = sb->s_blocks_count;
532 pctx.blk2 = ctx->num_blocks;
533 if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
534 ctx->flags |= E2F_FLAG_ABORT;
539 if (sb->s_log_block_size != (__u32) sb->s_log_frag_size) {
540 pctx.blk = EXT2_BLOCK_SIZE(sb);
541 pctx.blk2 = EXT2_FRAG_SIZE(sb);
542 fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx);
543 ctx->flags |= E2F_FLAG_ABORT;
547 should_be = sb->s_frags_per_group >>
548 (sb->s_log_block_size - sb->s_log_frag_size);
549 if (sb->s_blocks_per_group != should_be) {
550 pctx.blk = sb->s_blocks_per_group;
551 pctx.blk2 = should_be;
552 fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx);
553 ctx->flags |= E2F_FLAG_ABORT;
557 should_be = (sb->s_log_block_size == 0) ? 1 : 0;
558 if (sb->s_first_data_block != should_be) {
559 pctx.blk = sb->s_first_data_block;
560 pctx.blk2 = should_be;
561 fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
562 ctx->flags |= E2F_FLAG_ABORT;
566 should_be = sb->s_inodes_per_group * fs->group_desc_count;
567 if (sb->s_inodes_count != should_be) {
568 pctx.ino = sb->s_inodes_count;
569 pctx.ino2 = should_be;
570 if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
571 sb->s_inodes_count = should_be;
572 ext2fs_mark_super_dirty(fs);
577 * Verify the group descriptors....
579 first_block = sb->s_first_data_block;
580 last_block = sb->s_blocks_count-1;
582 lazy_flag = EXT2_HAS_COMPAT_FEATURE(fs->super,
583 EXT2_FEATURE_COMPAT_LAZY_BG);
584 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
585 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
586 for (i = 0, gd=fs->group_desc; i < fs->group_desc_count; i++, gd++) {
589 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
590 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
591 first_block = ext2fs_group_first_block(fs, i);
592 last_block = ext2fs_group_last_block(fs, i);
595 if ((gd->bg_block_bitmap < first_block) ||
596 (gd->bg_block_bitmap > last_block)) {
597 pctx.blk = gd->bg_block_bitmap;
598 if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
599 gd->bg_block_bitmap = 0;
601 if (gd->bg_block_bitmap == 0) {
602 ctx->invalid_block_bitmap_flag[i]++;
603 ctx->invalid_bitmaps++;
605 if ((gd->bg_inode_bitmap < first_block) ||
606 (gd->bg_inode_bitmap > last_block)) {
607 pctx.blk = gd->bg_inode_bitmap;
608 if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
609 gd->bg_inode_bitmap = 0;
611 if (gd->bg_inode_bitmap == 0) {
612 ctx->invalid_inode_bitmap_flag[i]++;
613 ctx->invalid_bitmaps++;
615 if ((gd->bg_inode_table < first_block) ||
616 ((gd->bg_inode_table +
617 fs->inode_blocks_per_group - 1) > last_block)) {
618 pctx.blk = gd->bg_inode_table;
619 if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
620 gd->bg_inode_table = 0;
622 if (gd->bg_inode_table == 0) {
623 ctx->invalid_inode_table_flag[i]++;
624 ctx->invalid_bitmaps++;
626 free_blocks += gd->bg_free_blocks_count;
627 free_inodes += gd->bg_free_inodes_count;
629 if ((gd->bg_free_blocks_count > sb->s_blocks_per_group) ||
630 (gd->bg_free_inodes_count > sb->s_inodes_per_group) ||
631 (gd->bg_used_dirs_count > sb->s_inodes_per_group))
632 ext2fs_unmark_valid(fs);
634 if (!ext2fs_group_desc_csum_verify(fs, i)) {
635 if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
636 gd->bg_flags &= ~(EXT2_BG_BLOCK_UNINIT |
637 EXT2_BG_INODE_UNINIT);
638 gd->bg_itable_unused = 0;
640 ext2fs_unmark_valid(fs);
643 if (!lazy_flag && !csum_flag &&
644 (gd->bg_flags &(EXT2_BG_BLOCK_UNINIT|EXT2_BG_INODE_UNINIT)||
645 gd->bg_itable_unused != 0)){
646 if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
647 gd->bg_flags &= ~(EXT2_BG_BLOCK_UNINIT |
648 EXT2_BG_INODE_UNINIT);
649 gd->bg_itable_unused = 0;
651 ext2fs_unmark_valid(fs);
653 if (gd->bg_flags & EXT2_BG_BLOCK_UNINIT &&
654 !(gd->bg_flags & EXT2_BG_INODE_UNINIT)) {
655 if (fix_problem(ctx, PR_0_BB_UNINIT_IB_INIT, &pctx))
656 gd->bg_flags &= ~EXT2_BG_BLOCK_UNINIT;
657 ext2fs_unmark_valid(fs);
660 (gd->bg_itable_unused > gd->bg_free_inodes_count ||
661 gd->bg_itable_unused > sb->s_inodes_per_group)) {
662 pctx.blk = gd->bg_itable_unused;
663 if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx))
664 gd->bg_itable_unused = 0;
665 ext2fs_unmark_valid(fs);
670 * Update the global counts from the block group counts. This
671 * is needed for an experimental patch which eliminates
672 * locking the entire filesystem when allocating blocks or
673 * inodes; if the filesystem is not unmounted cleanly, the
674 * global counts may not be accurate.
676 if ((free_blocks != sb->s_free_blocks_count) ||
677 (free_inodes != sb->s_free_inodes_count)) {
678 if (ctx->options & E2F_OPT_READONLY)
679 ext2fs_unmark_valid(fs);
681 sb->s_free_blocks_count = free_blocks;
682 sb->s_free_inodes_count = free_inodes;
683 ext2fs_mark_super_dirty(fs);
687 if ((sb->s_free_blocks_count > sb->s_blocks_count) ||
688 (sb->s_free_inodes_count > sb->s_inodes_count))
689 ext2fs_unmark_valid(fs);
693 * If we have invalid bitmaps, set the error state of the
696 if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
697 sb->s_state &= ~EXT2_VALID_FS;
698 ext2fs_mark_super_dirty(fs);
701 clear_problem_context(&pctx);
703 #ifndef EXT2_SKIP_UUID
705 * If the UUID field isn't assigned, assign it.
707 if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) {
708 if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
709 uuid_generate(sb->s_uuid);
710 ext2fs_mark_super_dirty(fs);
711 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
717 * For the Hurd, check to see if the filetype option is set,
718 * since it doesn't support it.
720 if (!(ctx->options & E2F_OPT_READONLY) &&
721 fs->super->s_creator_os == EXT2_OS_HURD &&
722 (fs->super->s_feature_incompat &
723 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
724 if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
725 fs->super->s_feature_incompat &=
726 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
727 ext2fs_mark_super_dirty(fs);
728 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
733 * If we have any of the compatibility flags set, we need to have a
734 * revision 1 filesystem. Most kernels will not check the flags on
735 * a rev 0 filesystem and we may have corruption issues because of
736 * the incompatible changes to the filesystem.
738 if (!(ctx->options & E2F_OPT_READONLY) &&
739 fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
740 (fs->super->s_feature_compat ||
741 fs->super->s_feature_ro_compat ||
742 fs->super->s_feature_incompat) &&
743 fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
744 ext2fs_update_dynamic_rev(fs);
745 ext2fs_mark_super_dirty(fs);
746 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
749 check_resize_inode(ctx);
752 * Clean up any orphan inodes, if present.
754 if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
755 fs->super->s_state &= ~EXT2_VALID_FS;
756 ext2fs_mark_super_dirty(fs);
760 * Some buggy distributions (such as Ubuntu) have init scripts
761 * and/or installers which fail to correctly set the system
762 * clock before running e2fsck and/or formatting the
763 * filesystem initially. Normally this happens because the
764 * hardware clock is ticking localtime, instead of the more
765 * proper and less error-prone UTC time. So while the kernel
766 * is booting, the system time (which in Linux systems always
767 * ticks in UTC time) is set from the hardware clock, but
768 * since the hardware clock is ticking localtime, the system
769 * time is incorrect. Unfortunately, some buggy distributions
770 * do not correct this before running e2fsck. If this option
771 * is set to a boolean value of true, we attempt to work
772 * around this situation by allowing the superblock last write
773 * time, last mount time, and last check time to be in the
774 * future by up to 24 hours.
776 profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
777 0, 0, &buggy_init_scripts);
778 ctx->time_fudge = buggy_init_scripts ? 86400 : 0;
781 * Check to see if the superblock last mount time or last
782 * write time is in the future.
784 if (fs->super->s_mtime > (__u32) ctx->now + ctx->time_fudge) {
785 pctx.num = fs->super->s_mtime;
786 if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_MOUNT, &pctx)) {
787 fs->super->s_mtime = ctx->now;
788 ext2fs_mark_super_dirty(fs);
791 if (fs->super->s_wtime > (__u32) ctx->now + ctx->time_fudge) {
792 pctx.num = fs->super->s_wtime;
793 if (fix_problem(ctx, PR_0_FUTURE_SB_LAST_WRITE, &pctx)) {
794 fs->super->s_wtime = ctx->now;
795 ext2fs_mark_super_dirty(fs);
800 * Move the ext3 journal file, if necessary.
802 e2fsck_move_ext3_journal(ctx);
805 * Fix journal hint, if necessary
807 e2fsck_fix_ext3_journal_hint(ctx);
810 * Add dirhash hint if necessary
812 e2fsck_fix_dirhash_hint(ctx);
818 * Check to see if we should backup the master sb to the backup super
819 * blocks. Returns non-zero if the sb should be backed up.
823 * A few flags are set on the fly by the kernel, but only in the
824 * primary superblock. This is actually a bad thing, and we should
825 * try to discourage it in the future. In particular, for the newer
826 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
827 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
830 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
831 * unfortunately, we shouldn't ignore it since if it's not set in the
832 * backup, the extended attributes in the filesystem will be stripped
835 #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
836 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
837 #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS)
839 int check_backup_super_block(e2fsck_t ctx)
841 ext2_filsys fs = ctx->fs;
849 * If we are already writing out the backup blocks, then we
850 * don't need to test. Also, if the filesystem is invalid, or
851 * the check was aborted or cancelled, we also don't want to
852 * do the backup. If the filesystem was opened read-only then
853 * we can't do the backup.
855 if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
856 !ext2fs_test_valid(fs) ||
857 (fs->super->s_state & EXT2_ERROR_FS) ||
858 (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
859 (ctx->options & E2F_OPT_READONLY))
862 for (g = 1; g < fs->group_desc_count; g++) {
863 if (!ext2fs_bg_has_super(fs, g))
866 sb = fs->super->s_first_data_block +
867 (g * fs->super->s_blocks_per_group);
869 retval = ext2fs_open(ctx->filesystem_name, 0,
871 fs->io->manager, &tfs);
877 #define SUPER_INCOMPAT_DIFFERENT(x) \
878 (( fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
879 (tfs->super->x & ~FEATURE_INCOMPAT_IGNORE))
880 #define SUPER_RO_COMPAT_DIFFERENT(x) \
881 (( fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
882 (tfs->super->x & ~FEATURE_RO_COMPAT_IGNORE))
883 #define SUPER_DIFFERENT(x) \
884 (fs->super->x != tfs->super->x)
886 if (SUPER_DIFFERENT(s_feature_compat) ||
887 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
888 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
889 SUPER_DIFFERENT(s_blocks_count) ||
890 SUPER_DIFFERENT(s_inodes_count) ||
891 memcmp(fs->super->s_uuid, tfs->super->s_uuid,
892 sizeof(fs->super->s_uuid)))