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"
27 static int check_super_value(e2fsck_t ctx
, const char *descr
,
28 unsigned long value
, int flags
,
29 unsigned long min_val
, unsigned long max_val
)
31 struct problem_context pctx
;
33 if ((flags
& MIN_CHECK
&& value
< min_val
) ||
34 (flags
& MAX_CHECK
&& value
> max_val
) ||
35 (flags
& LOG2_CHECK
&& (value
& (value
- 1)) != 0)) {
36 clear_problem_context(&pctx
);
39 fix_problem(ctx
, PR_0_MISC_CORRUPT_SUPER
, &pctx
);
40 ctx
->flags
|= E2F_FLAG_ABORT
;
46 static int check_super_value64(e2fsck_t ctx
, const char *descr
,
47 __u64 value
, int flags
,
48 __u64 min_val
, __u64 max_val
)
50 struct problem_context pctx
;
52 if ((flags
& MIN_CHECK
&& value
< min_val
) ||
53 (flags
& MAX_CHECK
&& value
> max_val
) ||
54 (flags
& LOG2_CHECK
&& (value
& (value
- 1)) != 0)) {
55 clear_problem_context(&pctx
);
58 fix_problem(ctx
, PR_0_MISC_CORRUPT_SUPER
, &pctx
);
59 ctx
->flags
|= E2F_FLAG_ABORT
;
66 * helper function to release an inode
68 struct process_block_struct
{
71 struct problem_context
*pctx
;
74 e2_blkcnt_t truncate_block
;
79 struct ext2_inode_large
*inode
;
82 static int release_inode_block(ext2_filsys fs
,
85 blk64_t ref_blk
EXT2FS_ATTR((unused
)),
86 int ref_offset
EXT2FS_ATTR((unused
)),
89 struct process_block_struct
*pb
;
91 struct problem_context
*pctx
;
92 blk64_t blk
= *block_nr
;
93 blk64_t cluster
= EXT2FS_B2C(fs
, *block_nr
);
96 pb
= (struct process_block_struct
*) priv_data
;
101 pctx
->blkcount
= blockcnt
;
106 if (pb
->last_cluster
== cluster
)
109 pb
->last_cluster
= cluster
;
111 if ((blk
< fs
->super
->s_first_data_block
) ||
112 (blk
>= ext2fs_blocks_count(fs
->super
))) {
113 fix_problem(ctx
, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM
, pctx
);
119 if (!ext2fs_test_block_bitmap2(fs
->block_map
, blk
)) {
120 fix_problem(ctx
, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK
, pctx
);
125 * If we are deleting an orphan, then we leave the fields alone.
126 * If we are truncating an orphan, then update the inode fields
127 * and clean up any partial block data.
129 if (pb
->truncating
) {
131 * We only remove indirect blocks if they are
138 pb
->errcode
= io_channel_read_blk64(fs
->io
, blk
, 1,
143 limit
= fs
->blocksize
>> 2;
144 for (i
= 0, bp
= (blk_t
*) pb
->buf
;
145 i
< limit
; i
++, bp
++)
150 * We don't remove direct blocks until we've reached
151 * the truncation block.
153 if (blockcnt
>= 0 && blockcnt
< pb
->truncate_block
)
156 * If part of the last block needs truncating, we do
159 if ((blockcnt
== pb
->truncate_block
) && pb
->truncate_offset
) {
160 pb
->errcode
= io_channel_read_blk64(fs
->io
, blk
, 1,
164 memset(pb
->buf
+ pb
->truncate_offset
, 0,
165 fs
->blocksize
- pb
->truncate_offset
);
166 pb
->errcode
= io_channel_write_blk64(fs
->io
, blk
, 1,
171 pb
->truncated_blocks
++;
173 retval
|= BLOCK_CHANGED
;
177 quota_data_sub(ctx
->qctx
, pb
->inode
, 0, ctx
->fs
->blocksize
);
178 ext2fs_block_alloc_stats2(fs
, blk
, -1);
184 * This function releases an inode. Returns 1 if an inconsistency was
185 * found. If the inode has a link count, then it is being truncated and
188 static int release_inode_blocks(e2fsck_t ctx
, ext2_ino_t ino
,
189 struct ext2_inode_large
*inode
, char *block_buf
,
190 struct problem_context
*pctx
)
192 struct process_block_struct pb
;
193 ext2_filsys fs
= ctx
->fs
;
198 if (!ext2fs_inode_has_valid_blocks2(fs
, EXT2_INODE(inode
)))
201 pb
.buf
= block_buf
+ 3 * ctx
->fs
->blocksize
;
208 if (inode
->i_links_count
) {
210 pb
.truncate_block
= (e2_blkcnt_t
)
211 ((EXT2_I_SIZE(inode
) + fs
->blocksize
- 1) /
213 pb
.truncate_offset
= inode
->i_size
% fs
->blocksize
;
216 pb
.truncate_block
= 0;
217 pb
.truncate_offset
= 0;
219 pb
.truncated_blocks
= 0;
220 retval
= ext2fs_block_iterate3(fs
, ino
, BLOCK_FLAG_DEPTH_TRAVERSE
,
221 block_buf
, release_inode_block
, &pb
);
223 com_err("release_inode_blocks", retval
,
224 _("while calling ext2fs_block_iterate for inode %u"),
231 /* Refresh the inode since ext2fs_block_iterate may have changed it */
232 e2fsck_read_inode_full(ctx
, ino
, EXT2_INODE(inode
), sizeof(*inode
),
233 "release_inode_blocks");
235 if (pb
.truncated_blocks
)
236 ext2fs_iblk_sub_blocks(fs
, EXT2_INODE(inode
),
237 pb
.truncated_blocks
);
239 blk
= ext2fs_file_acl_block(fs
, EXT2_INODE(inode
));
241 retval
= ext2fs_adjust_ea_refcount3(fs
, blk
, block_buf
, -1,
243 if (retval
== EXT2_ET_BAD_EA_BLOCK_NUM
) {
248 com_err("release_inode_blocks", retval
,
249 _("while calling ext2fs_adjust_ea_refcount2 for inode %u"),
255 quota_data_sub(ctx
->qctx
, inode
, 0,
257 ext2fs_block_alloc_stats2(fs
, blk
, -1);
260 ext2fs_file_acl_block_set(fs
, EXT2_INODE(inode
), 0);
265 /* Load all quota data in preparation for orphan clearing. */
266 static errcode_t
e2fsck_read_all_quotas(e2fsck_t ctx
)
269 enum quota_type qtype
;
270 errcode_t retval
= 0;
272 if (!ext2fs_has_feature_quota(ctx
->fs
->super
))
275 retval
= quota_init_context(&ctx
->qctx
, ctx
->fs
, 0);
279 for (qtype
= 0 ; qtype
< MAXQUOTAS
; qtype
++) {
280 qf_ino
= *quota_sb_inump(ctx
->fs
->super
, qtype
);
284 retval
= quota_read_all_dquots(ctx
->qctx
, qf_ino
, qtype
,
285 QREAD_USAGE
| QREAD_LIMITS
);
290 quota_release_context(&ctx
->qctx
);
294 /* Write all the quota info to disk. */
295 static errcode_t
e2fsck_write_all_quotas(e2fsck_t ctx
)
297 struct problem_context pctx
;
298 enum quota_type qtype
;
300 if (!ext2fs_has_feature_quota(ctx
->fs
->super
))
303 clear_problem_context(&pctx
);
304 for (qtype
= 0 ; qtype
< MAXQUOTAS
; qtype
++) {
306 pctx
.errcode
= quota_write_inode(ctx
->qctx
, 1 << qtype
);
308 fix_problem(ctx
, PR_6_WRITE_QUOTAS
, &pctx
);
313 quota_release_context(&ctx
->qctx
);
317 static int release_orphan_inode(e2fsck_t ctx
, ext2_ino_t
*ino
, char *block_buf
)
319 ext2_filsys fs
= ctx
->fs
;
320 struct problem_context pctx
;
321 struct ext2_inode_large inode
;
324 e2fsck_read_inode_full(ctx
, *ino
, EXT2_INODE(&inode
),
325 sizeof(inode
), "release_orphan_inode");
326 clear_problem_context(&pctx
);
328 pctx
.inode
= EXT2_INODE(&inode
);
329 pctx
.str
= inode
.i_links_count
? _("Truncating") : _("Clearing");
331 fix_problem(ctx
, PR_0_ORPHAN_CLEAR_INODE
, &pctx
);
333 next_ino
= inode
.i_dtime
;
335 ((next_ino
< EXT2_FIRST_INODE(fs
->super
)) ||
336 (next_ino
> fs
->super
->s_inodes_count
))) {
338 fix_problem(ctx
, PR_0_ORPHAN_ILLEGAL_INODE
, &pctx
);
342 if (release_inode_blocks(ctx
, *ino
, &inode
, block_buf
, &pctx
))
345 if (!inode
.i_links_count
) {
347 quota_data_inodes(ctx
->qctx
, &inode
, *ino
, -1);
348 ext2fs_inode_alloc_stats2(fs
, *ino
, -1,
349 LINUX_S_ISDIR(inode
.i_mode
));
351 inode
.i_dtime
= ctx
->now
;
355 e2fsck_write_inode_full(ctx
, *ino
, EXT2_INODE(&inode
),
356 sizeof(inode
), "delete_file");
361 struct process_orphan_block_data
{
373 static int process_orphan_block(ext2_filsys fs
,
375 e2_blkcnt_t blockcnt
,
376 blk64_t ref_blk
EXT2FS_ATTR((unused
)),
377 int ref_offset
EXT2FS_ATTR((unused
)),
380 struct process_orphan_block_data
*pd
;
382 struct problem_context pctx
;
383 blk64_t blk
= *block_nr
;
384 struct ext4_orphan_block_tail
*tail
;
392 clear_problem_context(&pctx
);
393 pctx
.ino
= fs
->super
->s_orphan_file_inum
;
396 /* Orphan file must not have holes */
398 if (blockcnt
== pd
->blocks
)
400 fix_problem(ctx
, PR_0_ORPHAN_FILE_HOLE
, &pctx
);
405 inodes_per_ob
= ext2fs_inodes_per_orphan_block(fs
);
406 pd
->errcode
= io_channel_read_blk64(fs
->io
, blk
, 1, pd
->buf
);
409 tail
= ext2fs_orphan_block_tail(fs
, pd
->buf
);
410 if (ext2fs_le32_to_cpu(tail
->ob_magic
) !=
411 EXT4_ORPHAN_BLOCK_MAGIC
) {
412 fix_problem(ctx
, PR_0_ORPHAN_FILE_BAD_MAGIC
, &pctx
);
415 if (!ext2fs_orphan_file_block_csum_verify(fs
,
416 fs
->super
->s_orphan_file_inum
, blk
, pd
->buf
)) {
417 fix_problem(ctx
, PR_0_ORPHAN_FILE_BAD_CHECKSUM
, &pctx
);
420 bdata
= (__u32
*)pd
->buf
;
421 for (j
= 0; j
< inodes_per_ob
; j
++) {
424 ino
= ext2fs_le32_to_cpu(bdata
[j
]);
425 if (release_orphan_inode(ctx
, &ino
, pd
->block_buf
))
431 static int process_orphan_file(e2fsck_t ctx
, char *block_buf
)
433 ext2_filsys fs
= ctx
->fs
;
435 struct process_orphan_block_data pd
;
437 ext2_ino_t orphan_inum
= fs
->super
->s_orphan_file_inum
;
438 struct ext2_inode orphan_inode
;
439 struct problem_context pctx
;
442 if (!ext2fs_has_feature_orphan_file(fs
->super
))
445 clear_problem_context(&pctx
);
446 pctx
.ino
= orphan_inum
;
448 orphan_buf
= (char *) e2fsck_allocate_memory(ctx
, fs
->blocksize
* 4,
449 "orphan block buffer");
450 retval
= ext2fs_read_inode(fs
, orphan_inum
, &orphan_inode
);
452 com_err("process_orphan_file", retval
,
453 _("while reading inode %d"), orphan_inum
);
457 if (EXT2_I_SIZE(&orphan_inode
) & (fs
->blocksize
- 1)) {
458 fix_problem(ctx
, PR_0_ORPHAN_FILE_WRONG_SIZE
, &pctx
);
462 pd
.buf
= orphan_buf
+ 3 * fs
->blocksize
;
463 pd
.block_buf
= block_buf
;
464 pd
.blocks
= EXT2_I_SIZE(&orphan_inode
) / fs
->blocksize
;
468 retval
= ext2fs_block_iterate3(fs
, orphan_inum
,
469 BLOCK_FLAG_DATA_ONLY
| BLOCK_FLAG_HOLE
,
470 orphan_buf
, process_orphan_block
, &pd
);
472 com_err("process_orphan_block", retval
,
473 _("while calling ext2fs_block_iterate for inode %d"),
480 com_err("process_orphan_block", pd
.errcode
,
481 _("while reading blocks of inode %d"),
487 ext2fs_free_mem(&orphan_buf
);
492 * This function releases all of the orphan inodes. It returns 1 if
493 * it hit some error, and 0 on success.
495 static int release_orphan_inodes(e2fsck_t ctx
)
497 ext2_filsys fs
= ctx
->fs
;
499 struct problem_context pctx
;
502 if (fs
->super
->s_last_orphan
== 0 &&
503 !ext2fs_has_feature_orphan_present(fs
->super
))
506 clear_problem_context(&pctx
);
507 ino
= fs
->super
->s_last_orphan
;
509 pctx
.errcode
= e2fsck_read_all_quotas(ctx
);
511 fix_problem(ctx
, PR_0_QUOTA_INIT_CTX
, &pctx
);
516 * Win or lose, we won't be using the head of the orphan inode
519 fs
->super
->s_last_orphan
= 0;
520 ext2fs_mark_super_dirty(fs
);
523 * If the filesystem contains errors, don't process the orphan list
524 * or orphan file, since neither can be trusted; and we're going to
525 * be running a full e2fsck run anyway... We clear orphan file contents
526 * after filesystem is checked to avoid clearing someone else's data.
528 if (fs
->super
->s_state
& EXT2_ERROR_FS
) {
530 quota_release_context(&ctx
->qctx
);
534 if (ino
&& ((ino
< EXT2_FIRST_INODE(fs
->super
)) ||
535 (ino
> fs
->super
->s_inodes_count
))) {
536 fix_problem(ctx
, PR_0_ORPHAN_ILLEGAL_HEAD_INODE
, &pctx
);
540 block_buf
= (char *) e2fsck_allocate_memory(ctx
, fs
->blocksize
* 4,
541 "block iterate buffer");
542 e2fsck_read_bitmaps(ctx
);
544 /* First process orphan list */
546 if (release_orphan_inode(ctx
, &ino
, block_buf
))
550 /* Next process orphan file */
551 if (ext2fs_has_feature_orphan_present(fs
->super
) &&
552 !ext2fs_has_feature_orphan_file(fs
->super
))
554 if (process_orphan_file(ctx
, block_buf
))
557 ext2fs_free_mem(&block_buf
);
558 pctx
.errcode
= e2fsck_write_all_quotas(ctx
);
563 ext2fs_free_mem(&block_buf
);
566 quota_release_context(&ctx
->qctx
);
571 static int reinit_orphan_block(ext2_filsys fs
,
573 e2_blkcnt_t blockcnt
,
574 blk64_t ref_blk
EXT2FS_ATTR((unused
)),
575 int ref_offset
EXT2FS_ATTR((unused
)),
578 struct process_orphan_block_data
*pd
;
580 blk64_t blk
= *block_nr
;
581 struct problem_context pctx
;
586 /* Orphan file must not have holes */
588 if (blockcnt
== pd
->blocks
)
591 clear_problem_context(&pctx
);
592 pctx
.ino
= fs
->super
->s_orphan_file_inum
;
594 fix_problem(ctx
, PR_6_ORPHAN_FILE_HOLE
, &pctx
);
600 if (ext2fs_has_feature_metadata_csum(fs
->super
)) {
601 struct ext4_orphan_block_tail
*tail
;
603 tail
= ext2fs_orphan_block_tail(fs
, pd
->buf
);
605 * Update checksum to match expected buffer contents with
606 * appropriate block number.
608 tail
->ob_checksum
= ext2fs_do_orphan_file_block_csum(fs
,
609 pd
->ino
, pd
->generation
, blk
, pd
->buf
);
612 pd
->errcode
= io_channel_read_blk64(fs
->io
, blk
, 1,
614 /* Block is already cleanly initialized? */
615 if (!memcmp(pd
->block_buf
, pd
->buf
, fs
->blocksize
))
618 clear_problem_context(&pctx
);
619 pctx
.ino
= fs
->super
->s_orphan_file_inum
;
621 if (!fix_problem(ctx
, PR_6_ORPHAN_BLOCK_DIRTY
, &pctx
))
625 pd
->errcode
= io_channel_write_blk64(fs
->io
, blk
, 1, pd
->buf
);
632 * Check and clear orphan file. We just return non-zero if we hit some
633 * inconsistency. Caller will truncate & recreate new orphan file.
635 int check_init_orphan_file(e2fsck_t ctx
)
637 ext2_filsys fs
= ctx
->fs
;
639 struct process_orphan_block_data pd
;
640 struct ext4_orphan_block_tail
*tail
;
641 ext2_ino_t orphan_inum
= fs
->super
->s_orphan_file_inum
;
642 struct ext2_inode orphan_inode
;
646 orphan_buf
= (char *) e2fsck_allocate_memory(ctx
, fs
->blocksize
* 5,
647 "orphan block buffer");
648 e2fsck_read_inode(ctx
, orphan_inum
, &orphan_inode
, "orphan inode");
649 if (EXT2_I_SIZE(&orphan_inode
) & (fs
->blocksize
- 1)) {
650 struct problem_context pctx
;
652 clear_problem_context(&pctx
);
653 pctx
.ino
= orphan_inum
;
654 fix_problem(ctx
, PR_6_ORPHAN_FILE_WRONG_SIZE
, &pctx
);
658 pd
.buf
= orphan_buf
+ 3 * fs
->blocksize
;
659 pd
.block_buf
= orphan_buf
+ 4 * fs
->blocksize
;
660 pd
.blocks
= EXT2_I_SIZE(&orphan_inode
) / fs
->blocksize
;
665 pd
.ino
= orphan_inum
;
666 pd
.generation
= orphan_inode
.i_generation
;
667 /* Initialize buffer to write */
668 memset(pd
.buf
, 0, fs
->blocksize
);
669 tail
= ext2fs_orphan_block_tail(fs
, pd
.buf
);
670 tail
->ob_magic
= ext2fs_cpu_to_le32(EXT4_ORPHAN_BLOCK_MAGIC
);
672 retval
= ext2fs_block_iterate3(fs
, orphan_inum
,
673 BLOCK_FLAG_DATA_ONLY
| BLOCK_FLAG_HOLE
,
674 orphan_buf
, reinit_orphan_block
, &pd
);
676 com_err("reinit_orphan_block", retval
,
677 _("while calling ext2fs_block_iterate for inode %d"),
684 com_err("process_orphan_block", pd
.errcode
,
685 _("while reading blocks of inode %d"),
691 /* We had to clear some blocks. Report it up. */
692 if (ret
== 0 && pd
.clear
)
695 ext2fs_free_mem(&orphan_buf
);
700 * Check the resize inode to make sure it is sane. We check both for
701 * the case where on-line resizing is not enabled (in which case the
702 * resize inode should be cleared) as well as the case where on-line
703 * resizing is enabled.
705 void check_resize_inode(e2fsck_t ctx
)
707 ext2_filsys fs
= ctx
->fs
;
708 struct ext2_inode inode
;
709 struct problem_context pctx
;
710 int i
, gdt_off
, ind_off
;
713 blk_t expect
; /* for resize inode, which is 32-bit only */
714 __u32
*dind_buf
= 0, *ind_buf
;
717 clear_problem_context(&pctx
);
719 if (ext2fs_has_feature_resize_inode(fs
->super
) &&
720 ext2fs_has_feature_meta_bg(fs
->super
) &&
721 fix_problem(ctx
, PR_0_DISABLE_RESIZE_INODE
, &pctx
)) {
722 ext2fs_clear_feature_resize_inode(fs
->super
);
723 fs
->super
->s_reserved_gdt_blocks
= 0;
724 ext2fs_mark_super_dirty(fs
);
728 * If the resize inode feature isn't set, then
729 * s_reserved_gdt_blocks must be zero.
731 if (!ext2fs_has_feature_resize_inode(fs
->super
)) {
732 if (fs
->super
->s_reserved_gdt_blocks
) {
733 pctx
.num
= fs
->super
->s_reserved_gdt_blocks
;
734 if (fix_problem(ctx
, PR_0_NONZERO_RESERVED_GDT_BLOCKS
,
736 fs
->super
->s_reserved_gdt_blocks
= 0;
737 ext2fs_mark_super_dirty(fs
);
742 /* Read the resize inode */
743 pctx
.ino
= EXT2_RESIZE_INO
;
744 retval
= ext2fs_read_inode(fs
, EXT2_RESIZE_INO
, &inode
);
746 if (ext2fs_has_feature_resize_inode(fs
->super
))
747 ctx
->flags
|= E2F_FLAG_RESIZE_INODE
;
752 * If the resize inode feature isn't set, check to make sure
753 * the resize inode is cleared; then we're done.
755 if (!ext2fs_has_feature_resize_inode(fs
->super
)) {
756 for (i
=0; i
< EXT2_N_BLOCKS
; i
++) {
757 if (inode
.i_block
[i
])
760 if ((i
< EXT2_N_BLOCKS
) &&
761 fix_problem(ctx
, PR_0_CLEAR_RESIZE_INODE
, &pctx
)) {
762 memset(&inode
, 0, sizeof(inode
));
763 e2fsck_write_inode(ctx
, EXT2_RESIZE_INO
, &inode
,
770 * The resize inode feature is enabled; check to make sure the
771 * only block in use is the double indirect block
773 blk
= inode
.i_block
[EXT2_DIND_BLOCK
];
774 for (i
=0; i
< EXT2_N_BLOCKS
; i
++) {
775 if (i
!= EXT2_DIND_BLOCK
&& inode
.i_block
[i
])
778 if ((i
< EXT2_N_BLOCKS
) || !blk
|| !inode
.i_links_count
||
779 !(inode
.i_mode
& LINUX_S_IFREG
) ||
780 (blk
< fs
->super
->s_first_data_block
||
781 blk
>= ext2fs_blocks_count(fs
->super
))) {
782 resize_inode_invalid
:
783 if (fix_problem(ctx
, PR_0_RESIZE_INODE_INVALID
, &pctx
)) {
784 memset(&inode
, 0, sizeof(inode
));
785 e2fsck_write_inode(ctx
, EXT2_RESIZE_INO
, &inode
,
787 ctx
->flags
|= E2F_FLAG_RESIZE_INODE
;
789 if (!(ctx
->options
& E2F_OPT_READONLY
)) {
790 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
791 ext2fs_mark_super_dirty(fs
);
795 dind_buf
= (__u32
*) e2fsck_allocate_memory(ctx
, fs
->blocksize
* 2,
796 "resize dind buffer");
797 ind_buf
= (__u32
*) ((char *) dind_buf
+ fs
->blocksize
);
799 retval
= ext2fs_read_ind_block(fs
, blk
, dind_buf
);
801 goto resize_inode_invalid
;
803 gdt_off
= fs
->desc_blocks
;
804 pblk
= fs
->super
->s_first_data_block
+ 1 + fs
->desc_blocks
;
805 if (fs
->blocksize
== 1024 && fs
->super
->s_first_data_block
== 0)
806 pblk
++; /* Deal with 1024 blocksize bigalloc fs */
807 for (i
= 0; i
< fs
->super
->s_reserved_gdt_blocks
/ 4;
808 i
++, gdt_off
++, pblk
++) {
809 gdt_off
%= fs
->blocksize
/4;
810 if (dind_buf
[gdt_off
] != pblk
)
811 goto resize_inode_invalid
;
812 retval
= ext2fs_read_ind_block(fs
, pblk
, ind_buf
);
814 goto resize_inode_invalid
;
816 for (j
= 1; j
< fs
->group_desc_count
; j
++) {
817 if (!ext2fs_bg_has_super(fs
, j
))
819 expect
= pblk
+ EXT2_GROUPS_TO_BLOCKS(fs
->super
, j
);
820 if (ind_buf
[ind_off
] != expect
)
821 goto resize_inode_invalid
;
828 ext2fs_free_mem(&dind_buf
);
833 * This function checks the dirhash signed/unsigned hint if necessary.
835 static void e2fsck_fix_dirhash_hint(e2fsck_t ctx
)
837 struct ext2_super_block
*sb
= ctx
->fs
->super
;
838 struct problem_context pctx
;
841 if ((ctx
->options
& E2F_OPT_READONLY
) ||
842 !ext2fs_has_feature_dir_index(sb
) ||
843 (sb
->s_flags
& (EXT2_FLAGS_SIGNED_HASH
|EXT2_FLAGS_UNSIGNED_HASH
)))
848 clear_problem_context(&pctx
);
849 if (fix_problem(ctx
, PR_0_DIRHASH_HINT
, &pctx
)) {
850 if (((int) c
) == -1) {
851 sb
->s_flags
|= EXT2_FLAGS_SIGNED_HASH
;
853 sb
->s_flags
|= EXT2_FLAGS_UNSIGNED_HASH
;
855 ext2fs_mark_super_dirty(ctx
->fs
);
860 void check_super_block(e2fsck_t ctx
)
862 ext2_filsys fs
= ctx
->fs
;
863 blk64_t first_block
, last_block
;
864 struct ext2_super_block
*sb
= fs
->super
;
865 unsigned int ipg_max
;
867 blk64_t blocks_per_group
= fs
->super
->s_blocks_per_group
;
868 __u32 bpg_max
, cpg_max
;
870 int inodes_per_block
;
872 int accept_time_fudge
;
873 int broken_system_clock
;
876 struct problem_context pctx
;
877 blk64_t free_blocks
= 0;
878 ext2_ino_t free_inodes
= 0;
879 int csum_flag
, clear_test_fs_flag
;
881 inodes_per_block
= EXT2_INODES_PER_BLOCK(fs
->super
);
882 ipg_max
= inodes_per_block
* (blocks_per_group
- 4);
883 if (ipg_max
> EXT2_MAX_INODES_PER_GROUP(sb
))
884 ipg_max
= EXT2_MAX_INODES_PER_GROUP(sb
);
885 cpg_max
= 8 * EXT2_BLOCK_SIZE(sb
);
886 if (cpg_max
> EXT2_MAX_CLUSTERS_PER_GROUP(sb
))
887 cpg_max
= EXT2_MAX_CLUSTERS_PER_GROUP(sb
);
888 bpg_max
= 8 * EXT2_BLOCK_SIZE(sb
) * EXT2FS_CLUSTER_RATIO(fs
);
889 if (bpg_max
> EXT2_MAX_BLOCKS_PER_GROUP(sb
))
890 bpg_max
= EXT2_MAX_BLOCKS_PER_GROUP(sb
);
892 ctx
->invalid_inode_bitmap_flag
= (int *) e2fsck_allocate_memory(ctx
,
893 sizeof(int) * fs
->group_desc_count
, "invalid_inode_bitmap");
894 ctx
->invalid_block_bitmap_flag
= (int *) e2fsck_allocate_memory(ctx
,
895 sizeof(int) * fs
->group_desc_count
, "invalid_block_bitmap");
896 ctx
->invalid_inode_table_flag
= (int *) e2fsck_allocate_memory(ctx
,
897 sizeof(int) * fs
->group_desc_count
, "invalid_inode_table");
899 blks_max
= (1ULL << 32) * EXT2_MAX_BLOCKS_PER_GROUP(fs
->super
);
900 if (ext2fs_has_feature_64bit(fs
->super
)) {
901 if (blks_max
> ((1ULL << 48) - 1))
902 blks_max
= (1ULL << 48) - 1;
904 if (blks_max
> ((1ULL << 32) - 1))
905 blks_max
= (1ULL << 32) - 1;
908 clear_problem_context(&pctx
);
911 * Verify the super block constants...
913 if (!check_super_value(ctx
, "inodes_count", sb
->s_inodes_count
,
916 if (!check_super_value64(ctx
, "blocks_count", ext2fs_blocks_count(sb
),
917 MIN_CHECK
| MAX_CHECK
, 1, blks_max
))
919 if (!check_super_value(ctx
, "first_data_block", sb
->s_first_data_block
,
920 MAX_CHECK
, 0, ext2fs_blocks_count(sb
)))
922 if (!check_super_value(ctx
, "log_block_size", sb
->s_log_block_size
,
923 MIN_CHECK
| MAX_CHECK
, 0,
924 EXT2_MAX_BLOCK_LOG_SIZE
- EXT2_MIN_BLOCK_LOG_SIZE
))
926 if (!check_super_value(ctx
, "log_cluster_size",
927 sb
->s_log_cluster_size
,
928 MIN_CHECK
| MAX_CHECK
, sb
->s_log_block_size
,
929 (EXT2_MAX_CLUSTER_LOG_SIZE
-
930 EXT2_MIN_CLUSTER_LOG_SIZE
)))
932 if (!check_super_value(ctx
, "clusters_per_group",
933 sb
->s_clusters_per_group
,
934 MIN_CHECK
| MAX_CHECK
, 8, cpg_max
))
936 if (!check_super_value(ctx
, "blocks_per_group", sb
->s_blocks_per_group
,
937 MIN_CHECK
| MAX_CHECK
, 8, bpg_max
))
939 if (!check_super_value(ctx
, "inodes_per_group", sb
->s_inodes_per_group
,
940 MIN_CHECK
| MAX_CHECK
, inodes_per_block
, ipg_max
))
942 if (!check_super_value(ctx
, "r_blocks_count", ext2fs_r_blocks_count(sb
),
943 MAX_CHECK
, 0, ext2fs_blocks_count(sb
) / 2))
945 if (!check_super_value(ctx
, "reserved_gdt_blocks",
946 sb
->s_reserved_gdt_blocks
, MAX_CHECK
, 0,
947 fs
->blocksize
/ sizeof(__u32
)))
949 if (!check_super_value(ctx
, "desc_size",
950 sb
->s_desc_size
, MAX_CHECK
| LOG2_CHECK
, 0,
954 should_be
= (__u64
)sb
->s_inodes_per_group
* fs
->group_desc_count
;
955 if (should_be
> ~0U) {
956 pctx
.num
= should_be
;
957 fix_problem(ctx
, PR_0_INODE_COUNT_BIG
, &pctx
);
958 ctx
->flags
|= E2F_FLAG_ABORT
;
961 if (sb
->s_inodes_count
!= should_be
) {
962 pctx
.ino
= sb
->s_inodes_count
;
963 pctx
.ino2
= should_be
;
964 if (fix_problem(ctx
, PR_0_INODE_COUNT_WRONG
, &pctx
)) {
965 sb
->s_inodes_count
= should_be
;
966 ext2fs_mark_super_dirty(fs
);
968 pctx
.num
= sb
->s_inodes_count
;
969 pctx
.str
= "inodes_count";
970 fix_problem(ctx
, PR_0_MISC_CORRUPT_SUPER
, &pctx
);
971 ctx
->flags
|= E2F_FLAG_ABORT
;
975 if (sb
->s_rev_level
> EXT2_GOOD_OLD_REV
&&
976 !check_super_value(ctx
, "first_ino", sb
->s_first_ino
,
977 MIN_CHECK
| MAX_CHECK
,
978 EXT2_GOOD_OLD_FIRST_INO
, sb
->s_inodes_count
))
980 inode_size
= EXT2_INODE_SIZE(sb
);
981 if (!check_super_value(ctx
, "inode_size",
982 inode_size
, MIN_CHECK
| MAX_CHECK
| LOG2_CHECK
,
983 EXT2_GOOD_OLD_INODE_SIZE
, fs
->blocksize
))
985 if (sb
->s_blocks_per_group
!= (sb
->s_clusters_per_group
*
986 EXT2FS_CLUSTER_RATIO(fs
))) {
987 pctx
.num
= sb
->s_clusters_per_group
* EXT2FS_CLUSTER_RATIO(fs
);
988 pctx
.str
= "block_size";
989 fix_problem(ctx
, PR_0_MISC_CORRUPT_SUPER
, &pctx
);
990 ctx
->flags
|= E2F_FLAG_ABORT
;
994 if ((ctx
->flags
& E2F_FLAG_GOT_DEVSIZE
) &&
995 (ctx
->num_blocks
< ext2fs_blocks_count(sb
))) {
996 pctx
.blk
= ext2fs_blocks_count(sb
);
997 pctx
.blk2
= ctx
->num_blocks
;
998 if (fix_problem(ctx
, PR_0_FS_SIZE_WRONG
, &pctx
)) {
999 ctx
->flags
|= E2F_FLAG_ABORT
;
1004 should_be
= (sb
->s_log_block_size
== 0 &&
1005 EXT2FS_CLUSTER_RATIO(fs
) == 1) ? 1 : 0;
1006 if (sb
->s_first_data_block
!= should_be
) {
1007 pctx
.blk
= sb
->s_first_data_block
;
1008 pctx
.blk2
= should_be
;
1009 fix_problem(ctx
, PR_0_FIRST_DATA_BLOCK
, &pctx
);
1010 ctx
->flags
|= E2F_FLAG_ABORT
;
1014 if (EXT2_INODE_SIZE(sb
) > EXT2_GOOD_OLD_INODE_SIZE
) {
1016 sizeof(((struct ext2_inode_large
*) 0)->i_extra_isize
) +
1017 sizeof(((struct ext2_inode_large
*) 0)->i_checksum_hi
);
1018 unsigned max
= EXT2_INODE_SIZE(sb
) - EXT2_GOOD_OLD_INODE_SIZE
;
1019 pctx
.num
= sb
->s_min_extra_isize
;
1020 if (sb
->s_min_extra_isize
&&
1021 (sb
->s_min_extra_isize
< min
||
1022 sb
->s_min_extra_isize
> max
||
1023 sb
->s_min_extra_isize
& 3) &&
1024 fix_problem(ctx
, PR_0_BAD_MIN_EXTRA_ISIZE
, &pctx
)) {
1025 sb
->s_min_extra_isize
=
1026 (sizeof(struct ext2_inode_large
) -
1027 EXT2_GOOD_OLD_INODE_SIZE
);
1028 ext2fs_mark_super_dirty(fs
);
1030 pctx
.num
= sb
->s_want_extra_isize
;
1031 if (sb
->s_want_extra_isize
&&
1032 (sb
->s_want_extra_isize
< min
||
1033 sb
->s_want_extra_isize
> max
||
1034 sb
->s_want_extra_isize
& 3) &&
1035 fix_problem(ctx
, PR_0_BAD_WANT_EXTRA_ISIZE
, &pctx
)) {
1036 sb
->s_want_extra_isize
=
1037 (sizeof(struct ext2_inode_large
) -
1038 EXT2_GOOD_OLD_INODE_SIZE
);
1039 ext2fs_mark_super_dirty(fs
);
1043 /* Are metadata_csum and uninit_bg both set? */
1044 if (ext2fs_has_feature_metadata_csum(fs
->super
) &&
1045 ext2fs_has_feature_gdt_csum(fs
->super
) &&
1046 fix_problem(ctx
, PR_0_META_AND_GDT_CSUM_SET
, &pctx
)) {
1047 ext2fs_clear_feature_gdt_csum(fs
->super
);
1048 ext2fs_mark_super_dirty(fs
);
1049 for (i
= 0; i
< fs
->group_desc_count
; i
++)
1050 ext2fs_group_desc_csum_set(fs
, i
);
1053 /* We can't have ^metadata_csum,metadata_csum_seed */
1054 if (!ext2fs_has_feature_metadata_csum(fs
->super
) &&
1055 ext2fs_has_feature_csum_seed(fs
->super
) &&
1056 fix_problem(ctx
, PR_0_CSUM_SEED_WITHOUT_META_CSUM
, &pctx
)) {
1057 ext2fs_clear_feature_csum_seed(fs
->super
);
1058 fs
->super
->s_checksum_seed
= 0;
1059 ext2fs_mark_super_dirty(fs
);
1062 /* Is 64bit set and extents unset? */
1063 if (ext2fs_has_feature_64bit(fs
->super
) &&
1064 !ext2fs_has_feature_extents(fs
->super
) &&
1065 fix_problem(ctx
, PR_0_64BIT_WITHOUT_EXTENTS
, &pctx
)) {
1066 ext2fs_set_feature_extents(fs
->super
);
1067 ext2fs_mark_super_dirty(fs
);
1070 /* Did user ask us to convert files to extents? */
1071 if (ctx
->options
& E2F_OPT_CONVERT_BMAP
) {
1072 ext2fs_set_feature_extents(fs
->super
);
1073 ext2fs_mark_super_dirty(fs
);
1076 if (ext2fs_has_feature_meta_bg(fs
->super
) &&
1077 (fs
->super
->s_first_meta_bg
> fs
->desc_blocks
)) {
1078 pctx
.group
= fs
->desc_blocks
;
1079 pctx
.num
= fs
->super
->s_first_meta_bg
;
1080 if (fix_problem(ctx
, PR_0_FIRST_META_BG_TOO_BIG
, &pctx
)) {
1081 ext2fs_clear_feature_meta_bg(fs
->super
);
1082 fs
->super
->s_first_meta_bg
= 0;
1083 ext2fs_mark_super_dirty(fs
);
1088 * Verify the group descriptors....
1090 first_block
= sb
->s_first_data_block
;
1091 last_block
= ext2fs_blocks_count(sb
)-1;
1093 csum_flag
= ext2fs_has_group_desc_csum(fs
);
1094 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
1097 if (!ext2fs_has_feature_flex_bg(fs
->super
)) {
1098 first_block
= ext2fs_group_first_block2(fs
, i
);
1099 last_block
= ext2fs_group_last_block2(fs
, i
);
1102 if ((ext2fs_block_bitmap_loc(fs
, i
) < first_block
) ||
1103 (ext2fs_block_bitmap_loc(fs
, i
) > last_block
)) {
1104 pctx
.blk
= ext2fs_block_bitmap_loc(fs
, i
);
1105 if (fix_problem(ctx
, PR_0_BB_NOT_GROUP
, &pctx
))
1106 ext2fs_block_bitmap_loc_set(fs
, i
, 0);
1108 if (ext2fs_block_bitmap_loc(fs
, i
) == 0) {
1109 ctx
->invalid_block_bitmap_flag
[i
]++;
1110 ctx
->invalid_bitmaps
++;
1112 if ((ext2fs_inode_bitmap_loc(fs
, i
) < first_block
) ||
1113 (ext2fs_inode_bitmap_loc(fs
, i
) > last_block
)) {
1114 pctx
.blk
= ext2fs_inode_bitmap_loc(fs
, i
);
1115 if (fix_problem(ctx
, PR_0_IB_NOT_GROUP
, &pctx
))
1116 ext2fs_inode_bitmap_loc_set(fs
, i
, 0);
1118 if (ext2fs_inode_bitmap_loc(fs
, i
) == 0) {
1119 ctx
->invalid_inode_bitmap_flag
[i
]++;
1120 ctx
->invalid_bitmaps
++;
1122 if ((ext2fs_inode_table_loc(fs
, i
) < first_block
) ||
1123 ((ext2fs_inode_table_loc(fs
, i
) +
1124 fs
->inode_blocks_per_group
- 1) > last_block
)) {
1125 pctx
.blk
= ext2fs_inode_table_loc(fs
, i
);
1126 if (fix_problem(ctx
, PR_0_ITABLE_NOT_GROUP
, &pctx
))
1127 ext2fs_inode_table_loc_set(fs
, i
, 0);
1129 if (ext2fs_inode_table_loc(fs
, i
) == 0) {
1130 ctx
->invalid_inode_table_flag
[i
]++;
1131 ctx
->invalid_bitmaps
++;
1133 free_blocks
+= ext2fs_bg_free_blocks_count(fs
, i
);
1134 free_inodes
+= ext2fs_bg_free_inodes_count(fs
, i
);
1136 if ((ext2fs_bg_free_blocks_count(fs
, i
) > sb
->s_blocks_per_group
) ||
1137 (ext2fs_bg_free_inodes_count(fs
, i
) > sb
->s_inodes_per_group
) ||
1138 (ext2fs_bg_used_dirs_count(fs
, i
) > sb
->s_inodes_per_group
))
1139 ext2fs_unmark_valid(fs
);
1142 if (!ext2fs_group_desc_csum_verify(fs
, i
)) {
1143 pctx
.csum1
= ext2fs_bg_checksum(fs
, i
);
1144 pctx
.csum2
= ext2fs_group_desc_csum(fs
, i
);
1145 if (fix_problem(ctx
, PR_0_GDT_CSUM
, &pctx
)) {
1146 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_BLOCK_UNINIT
);
1147 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_INODE_UNINIT
);
1148 ext2fs_bg_itable_unused_set(fs
, i
, 0);
1151 ext2fs_unmark_valid(fs
);
1155 (ext2fs_bg_flags_test(fs
, i
, EXT2_BG_BLOCK_UNINIT
) ||
1156 ext2fs_bg_flags_test(fs
, i
, EXT2_BG_INODE_UNINIT
) ||
1157 ext2fs_bg_itable_unused(fs
, i
) != 0)) {
1158 if (fix_problem(ctx
, PR_0_GDT_UNINIT
, &pctx
)) {
1159 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_BLOCK_UNINIT
);
1160 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_INODE_UNINIT
);
1161 ext2fs_bg_itable_unused_set(fs
, i
, 0);
1164 ext2fs_unmark_valid(fs
);
1167 if (i
== fs
->group_desc_count
- 1 &&
1168 ext2fs_bg_flags_test(fs
, i
, EXT2_BG_BLOCK_UNINIT
)) {
1169 if (fix_problem(ctx
, PR_0_BB_UNINIT_LAST
, &pctx
)) {
1170 ext2fs_bg_flags_clear(fs
, i
, EXT2_BG_BLOCK_UNINIT
);
1173 ext2fs_unmark_valid(fs
);
1177 (ext2fs_bg_itable_unused(fs
, i
) > ext2fs_bg_free_inodes_count(fs
, i
) ||
1178 ext2fs_bg_itable_unused(fs
, i
) > sb
->s_inodes_per_group
)) {
1179 pctx
.blk
= ext2fs_bg_itable_unused(fs
, i
);
1180 if (fix_problem(ctx
, PR_0_GDT_ITABLE_UNUSED
, &pctx
)) {
1181 ext2fs_bg_itable_unused_set(fs
, i
, 0);
1184 ext2fs_unmark_valid(fs
);
1188 ext2fs_group_desc_csum_set(fs
, i
);
1189 /* If the user aborts e2fsck by typing ^C, stop right away */
1190 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1194 ctx
->free_blocks
= EXT2FS_C2B(fs
, free_blocks
);
1195 ctx
->free_inodes
= free_inodes
;
1197 if ((ext2fs_free_blocks_count(sb
) > ext2fs_blocks_count(sb
)) ||
1198 (sb
->s_free_inodes_count
> sb
->s_inodes_count
))
1199 ext2fs_unmark_valid(fs
);
1203 * If we have invalid bitmaps, set the error state of the
1206 if (ctx
->invalid_bitmaps
&& !(ctx
->options
& E2F_OPT_READONLY
)) {
1207 sb
->s_state
&= ~EXT2_VALID_FS
;
1208 ext2fs_mark_super_dirty(fs
);
1211 clear_problem_context(&pctx
);
1213 #ifndef EXT2_SKIP_UUID
1215 * If the UUID field isn't assigned, assign it.
1216 * Skip if checksums are enabled and the filesystem is mounted,
1217 * if the id changes under the kernel remounting rw may fail.
1219 if (!(ctx
->options
& E2F_OPT_READONLY
) && uuid_is_null(sb
->s_uuid
) &&
1220 !ext2fs_has_feature_metadata_csum(ctx
->fs
->super
) &&
1221 (!csum_flag
|| !(ctx
->mount_flags
& EXT2_MF_MOUNTED
))) {
1222 if (fix_problem(ctx
, PR_0_ADD_UUID
, &pctx
)) {
1223 uuid_generate(sb
->s_uuid
);
1224 ext2fs_init_csum_seed(fs
);
1225 fs
->flags
|= EXT2_FLAG_DIRTY
;
1226 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
1232 * Check to see if we should disable the test_fs flag
1234 profile_get_boolean(ctx
->profile
, "options",
1235 "clear_test_fs_flag", 0, 1,
1236 &clear_test_fs_flag
);
1237 if (!(ctx
->options
& E2F_OPT_READONLY
) &&
1238 clear_test_fs_flag
&&
1239 (fs
->super
->s_flags
& EXT2_FLAGS_TEST_FILESYS
) &&
1240 (fs_proc_check("ext4") || check_for_modules("ext4"))) {
1241 if (fix_problem(ctx
, PR_0_CLEAR_TESTFS_FLAG
, &pctx
)) {
1242 fs
->super
->s_flags
&= ~EXT2_FLAGS_TEST_FILESYS
;
1243 fs
->flags
|= EXT2_FLAG_DIRTY
;
1244 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
1249 * For the Hurd, check to see if the filetype option is set,
1250 * since it doesn't support it.
1252 if (!(ctx
->options
& E2F_OPT_READONLY
) &&
1253 fs
->super
->s_creator_os
== EXT2_OS_HURD
&&
1254 ext2fs_has_feature_filetype(fs
->super
)) {
1255 if (fix_problem(ctx
, PR_0_HURD_CLEAR_FILETYPE
, &pctx
)) {
1256 ext2fs_clear_feature_filetype(fs
->super
);
1257 ext2fs_mark_super_dirty(fs
);
1258 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
1263 * If we have any of the compatibility flags set, we need to have a
1264 * revision 1 filesystem. Most kernels will not check the flags on
1265 * a rev 0 filesystem and we may have corruption issues because of
1266 * the incompatible changes to the filesystem.
1268 if (!(ctx
->options
& E2F_OPT_READONLY
) &&
1269 fs
->super
->s_rev_level
== EXT2_GOOD_OLD_REV
&&
1270 (fs
->super
->s_feature_compat
||
1271 fs
->super
->s_feature_ro_compat
||
1272 fs
->super
->s_feature_incompat
) &&
1273 fix_problem(ctx
, PR_0_FS_REV_LEVEL
, &pctx
)) {
1274 ext2fs_update_dynamic_rev(fs
);
1275 ext2fs_mark_super_dirty(fs
);
1276 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
1280 * Clean up any orphan inodes, if present.
1282 if (!(ctx
->options
& E2F_OPT_READONLY
) && release_orphan_inodes(ctx
)) {
1283 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
1284 ext2fs_mark_super_dirty(fs
);
1288 * Unfortunately, due to Windows' unfortunate design decision
1289 * to configure the hardware clock to tick localtime, instead
1290 * of the more proper and less error-prone UTC time, many
1291 * users end up in the situation where the system clock is
1292 * incorrectly set at the time when e2fsck is run.
1294 * Historically this was usually due to some distributions
1295 * having buggy init scripts and/or installers that didn't
1296 * correctly detect this case and take appropriate
1297 * countermeasures. However, it's still possible, despite the
1298 * best efforts of init script and installer authors to not be
1299 * able to detect this misconfiguration, usually due to a
1300 * buggy or misconfigured virtualization manager or the
1301 * installer not having access to a network time server during
1302 * the installation process. So by default, we allow the
1303 * superblock times to be fudged by up to 24 hours. This can
1304 * be disabled by setting options.accept_time_fudge to the
1305 * boolean value of false in e2fsck.conf. We also support
1306 * options.buggy_init_scripts for backwards compatibility.
1308 profile_get_boolean(ctx
->profile
, "options", "accept_time_fudge",
1309 0, 1, &accept_time_fudge
);
1310 profile_get_boolean(ctx
->profile
, "options", "buggy_init_scripts",
1311 0, accept_time_fudge
, &accept_time_fudge
);
1312 ctx
->time_fudge
= accept_time_fudge
? 86400 : 0;
1314 profile_get_boolean(ctx
->profile
, "options", "broken_system_clock",
1315 0, 0, &broken_system_clock
);
1318 * Check to see if the superblock last mount time or last
1319 * write time is in the future.
1321 if (((ctx
->options
& E2F_OPT_FORCE
) || fs
->super
->s_checkinterval
) &&
1322 !broken_system_clock
&& !(ctx
->flags
& E2F_FLAG_TIME_INSANE
) &&
1323 (fs
->super
->s_mtime
> (__u32
) ctx
->now
)) {
1324 pctx
.num
= fs
->super
->s_mtime
;
1325 problem
= PR_0_FUTURE_SB_LAST_MOUNT
;
1326 if (fs
->super
->s_mtime
<= (__u32
) ctx
->now
+ ctx
->time_fudge
)
1327 problem
= PR_0_FUTURE_SB_LAST_MOUNT_FUDGED
;
1328 if (fix_problem(ctx
, problem
, &pctx
)) {
1329 fs
->super
->s_mtime
= ctx
->now
;
1330 fs
->flags
|= EXT2_FLAG_DIRTY
;
1333 if (((ctx
->options
& E2F_OPT_FORCE
) || fs
->super
->s_checkinterval
) &&
1334 !broken_system_clock
&& !(ctx
->flags
& E2F_FLAG_TIME_INSANE
) &&
1335 (fs
->super
->s_wtime
> (__u32
) ctx
->now
)) {
1336 pctx
.num
= fs
->super
->s_wtime
;
1337 problem
= PR_0_FUTURE_SB_LAST_WRITE
;
1338 if (fs
->super
->s_wtime
<= (__u32
) ctx
->now
+ ctx
->time_fudge
)
1339 problem
= PR_0_FUTURE_SB_LAST_WRITE_FUDGED
;
1340 if (fix_problem(ctx
, problem
, &pctx
)) {
1341 fs
->super
->s_wtime
= ctx
->now
;
1342 fs
->flags
|= EXT2_FLAG_DIRTY
;
1346 e2fsck_validate_quota_inodes(ctx
);
1349 * Move the ext3 journal file, if necessary.
1351 e2fsck_move_ext3_journal(ctx
);
1354 * Fix journal hint, if necessary
1356 e2fsck_fix_ext3_journal_hint(ctx
);
1359 * Add dirhash hint if necessary
1361 e2fsck_fix_dirhash_hint(ctx
);
1364 * Hide quota inodes if necessary.
1366 e2fsck_hide_quota(ctx
);
1372 * Check to see if we should backup the master sb to the backup super
1373 * blocks. Returns non-zero if the sb should be backed up.
1377 * A few flags are set on the fly by the kernel, but only in the
1378 * primary superblock. This is actually a bad thing, and we should
1379 * try to discourage it in the future. In particular, for the newer
1380 * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
1381 * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the
1382 * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
1383 * copying the primary superblock during online resize.
1385 * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
1386 * unfortunately, we shouldn't ignore it since if it's not set in the
1387 * backup, the extended attributes in the filesystem will be stripped
1390 #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
1391 EXT4_FEATURE_RO_COMPAT_DIR_NLINK| \
1392 EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT)
1393 #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \
1394 EXT3_FEATURE_INCOMPAT_RECOVER)
1396 int check_backup_super_block(e2fsck_t ctx
)
1398 ext2_filsys fs
= ctx
->fs
;
1403 char buf
[SUPERBLOCK_SIZE
];
1404 struct ext2_super_block
*backup_sb
;
1407 * If we are already writing out the backup blocks, then we
1408 * don't need to test. Also, if the filesystem is invalid, or
1409 * the check was aborted or cancelled, we also don't want to
1410 * do the backup. If the filesystem was opened read-only then
1411 * we can't do the backup.
1413 if (((fs
->flags
& EXT2_FLAG_MASTER_SB_ONLY
) == 0) ||
1414 !ext2fs_test_valid(fs
) ||
1415 (fs
->super
->s_state
& EXT2_ERROR_FS
) ||
1416 (ctx
->flags
& (E2F_FLAG_ABORT
| E2F_FLAG_CANCEL
)) ||
1417 (ctx
->options
& E2F_OPT_READONLY
))
1420 for (g
= 1; g
< fs
->group_desc_count
; g
++) {
1421 if (!ext2fs_bg_has_super(fs
, g
))
1424 sb
= ext2fs_group_first_block2(fs
, g
);
1426 retval
= io_channel_read_blk(fs
->io
, sb
, -SUPERBLOCK_SIZE
,
1430 backup_sb
= (struct ext2_super_block
*) buf
;
1431 #ifdef WORDS_BIGENDIAN
1432 ext2fs_swap_super(backup_sb
);
1434 if ((backup_sb
->s_magic
!= EXT2_SUPER_MAGIC
) ||
1435 (backup_sb
->s_rev_level
> EXT2_LIB_CURRENT_REV
) ||
1436 ((backup_sb
->s_log_block_size
+ EXT2_MIN_BLOCK_LOG_SIZE
) >
1437 EXT2_MAX_BLOCK_LOG_SIZE
) ||
1438 (EXT2_INODE_SIZE(backup_sb
) < EXT2_GOOD_OLD_INODE_SIZE
))
1441 #define SUPER_INCOMPAT_DIFFERENT(x) \
1442 ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \
1443 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
1444 #define SUPER_RO_COMPAT_DIFFERENT(x) \
1445 ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \
1446 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
1447 #define SUPER_DIFFERENT(x) \
1448 (fs->super->x != backup_sb->x)
1450 if (SUPER_DIFFERENT(s_feature_compat
) ||
1451 SUPER_INCOMPAT_DIFFERENT(s_feature_incompat
) ||
1452 SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat
) ||
1453 SUPER_DIFFERENT(s_blocks_count
) ||
1454 SUPER_DIFFERENT(s_blocks_count_hi
) ||
1455 SUPER_DIFFERENT(s_inodes_count
) ||
1456 memcmp(fs
->super
->s_uuid
, backup_sb
->s_uuid
,
1457 sizeof(fs
->super
->s_uuid
)))