From: Jan Kara Date: Tue, 12 Jun 2018 09:53:23 +0000 (+0200) Subject: e2fsck: allow to fix some superblock corruptions X-Git-Tag: v1.44.3-rc1~60 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35b459ffd7b8b60671debe7233d5c558db1afafd;p=thirdparty%2Fe2fsprogs.git e2fsck: allow to fix some superblock corruptions Add a flag to ext2fs_open() which allows to open a filesystem even if superblock is somewhat inconsistent. Use this flag from e2fsck as a last resort to try to fix the superblok. Currently, the flag does nothing. We'll relax checks in ext2fs_open() once e2fsck is able to handle corresponding corruption gracefully. Reviewed-by: Andreas Dilger Signed-off-by: Jan Kara Signed-off-by: Theodore Ts'o --- diff --git a/e2fsck/unix.c b/e2fsck/unix.c index cbe5ec503..55e21ea0a 100644 --- a/e2fsck/unix.c +++ b/e2fsck/unix.c @@ -1364,7 +1364,7 @@ int main (int argc, char *argv[]) const char *lib_ver_date; int my_ver, lib_ver; e2fsck_t ctx; - blk64_t orig_superblock; + blk64_t orig_superblock = ~(blk64_t)0; struct problem_context pctx; int flags, run_result, was_changed; int journal_size; @@ -1571,6 +1571,26 @@ failure: "check of the device.\n")); #endif else { + /* + * Let's try once more will less consistency checking + * so that we are able to recover from more errors + * (e.g. some tool messing up some value in the sb). + */ + if (!(flags & EXT2_FLAG_IGNORE_SB_ERRORS)) { + if (fs) + ext2fs_close_free(&fs); + log_out(ctx, _("%s: Trying to load superblock " + "despite errors...\n"), + ctx->program_name); + flags |= EXT2_FLAG_IGNORE_SB_ERRORS; + /* + * If we tried backup sb, revert to the + * original one now. + */ + if (orig_superblock != ~(blk64_t)0) + ctx->superblock = orig_superblock; + goto restart; + } fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); if (retval == EXT2_ET_BAD_MAGIC) check_plausibility(ctx->filesystem_name, diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h index 6774e32c9..250fd174c 100644 --- a/lib/ext2fs/ext2fs.h +++ b/lib/ext2fs/ext2fs.h @@ -195,6 +195,7 @@ typedef struct ext2_file *ext2_file_t; #define EXT2_FLAG_DIRECT_IO 0x80000 #define EXT2_FLAG_SKIP_MMP 0x100000 #define EXT2_FLAG_IGNORE_CSUM_ERRORS 0x200000 +#define EXT2_FLAG_IGNORE_SB_ERRORS 0x400000 /* * Special flag in the ext2 inode i_flag field that means that this is diff --git a/tests/f_crashdisk/expect.1 b/tests/f_crashdisk/expect.1 index 977a469f0..689803051 100644 --- a/tests/f_crashdisk/expect.1 +++ b/tests/f_crashdisk/expect.1 @@ -1,6 +1,10 @@ ext2fs_open2: The ext2 superblock is corrupt ../e2fsck/e2fsck: Superblock invalid, trying backup blocks... ../e2fsck/e2fsck: The ext2 superblock is corrupt while trying to open test.img +../e2fsck/e2fsck: Trying to load superblock despite errors... +ext2fs_open2: The ext2 superblock is corrupt +../e2fsck/e2fsck: Superblock invalid, trying backup blocks... +../e2fsck/e2fsck: The ext2 superblock is corrupt while trying to open test.img The superblock could not be read or does not describe a valid ext2/ext3/ext4 filesystem. If the device is valid and it really contains an ext2/ext3/ext4 diff --git a/tests/f_detect_xfs/expect b/tests/f_detect_xfs/expect index d0ece6a0c..06de99b99 100644 --- a/tests/f_detect_xfs/expect +++ b/tests/f_detect_xfs/expect @@ -2,6 +2,10 @@ ext2fs_open2: Bad magic number in super-block ../e2fsck/e2fsck: Superblock invalid, trying backup blocks... ../e2fsck/e2fsck: Bad magic number in super-block while trying to open test.img +../e2fsck/e2fsck: Trying to load superblock despite errors... +ext2fs_open2: Bad magic number in super-block +../e2fsck/e2fsck: Superblock invalid, trying backup blocks... +../e2fsck/e2fsck: Bad magic number in super-block while trying to open test.img The superblock could not be read or does not describe a valid ext2/ext3/ext4 filesystem. If the device is valid and it really contains an ext2/ext3/ext4 diff --git a/tests/f_detect_xfs/expect.nodebugfs b/tests/f_detect_xfs/expect.nodebugfs index d3b793580..1a76952ed 100644 --- a/tests/f_detect_xfs/expect.nodebugfs +++ b/tests/f_detect_xfs/expect.nodebugfs @@ -1,6 +1,8 @@ *** e2fsck ext2fs_open2: Bad magic number in super-block ../e2fsck/e2fsck: Superblock invalid, trying backup blocks... +ext2fs_open2: Bad magic number in super-block +../e2fsck/e2fsck: Superblock invalid, trying backup blocks... ../e2fsck/e2fsck: Bad magic number in super-block while trying to open test.img The superblock could not be read or does not describe a valid ext2/ext3/ext4 diff --git a/tests/j_ext_dumpe2fs/expect b/tests/j_ext_dumpe2fs/expect index db77a36dc..a703bcf82 100644 --- a/tests/j_ext_dumpe2fs/expect +++ b/tests/j_ext_dumpe2fs/expect @@ -1,5 +1,7 @@ e2fsck external journal ../e2fsck/e2fsck: Filesystem has unsupported feature(s) while trying to open test.img +../e2fsck/e2fsck: Trying to load superblock despite errors... +../e2fsck/e2fsck: Filesystem has unsupported feature(s) while trying to open test.img The superblock could not be read or does not describe a valid ext2/ext3/ext4 filesystem. If the device is valid and it really contains an ext2/ext3/ext4