[05/10] e2fsck: Allow to fix some superblock corruptions

Message ID 20180612095328.5215-6-jack@suse.cz
State Accepted
Headers show
Series
  • e2fsprogs: Handle s_inodes_count overflow better
Related show

Commit Message

Jan Kara June 12, 2018, 9:53 a.m.
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 <adilger@dilger.ca>
Signed-off-by: Jan Kara <jack@suse.cz>
---
 e2fsck/unix.c       | 22 +++++++++++++++++++++-
 lib/ext2fs/ext2fs.h |  1 +
 2 files changed, 22 insertions(+), 1 deletion(-)

Comments

Theodore Y. Ts'o June 19, 2018, 3:10 p.m. | #1
On Tue, Jun 12, 2018 at 11:53:23AM +0200, Jan Kara wrote:
> 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 <adilger@dilger.ca>
> Signed-off-by: Jan Kara <jack@suse.cz>

This patch, alas, causes test failures:

	f_crashdisk f_detect_xfs j_ext_dumpe2fs 

I strongly commend running "make check" when making changes to
e2fsprogs.


						- Ted
Theodore Y. Ts'o June 19, 2018, 3:23 p.m. | #2
On Tue, Jun 12, 2018 at 11:53:23AM +0200, Jan Kara wrote:
> 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 <adilger@dilger.ca>
> Signed-off-by: Jan Kara <jack@suse.cz>

Applied with the following fix-up patch.

						- Ted

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
Jan Kara June 20, 2018, 8 a.m. | #3
On Tue 19-06-18 11:10:02, Theodore Y. Ts'o wrote:
> On Tue, Jun 12, 2018 at 11:53:23AM +0200, Jan Kara wrote:
> > 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 <adilger@dilger.ca>
> > Signed-off-by: Jan Kara <jack@suse.cz>
> 
> This patch, alas, causes test failures:
> 
> 	f_crashdisk f_detect_xfs j_ext_dumpe2fs 
> 
> I strongly commend running "make check" when making changes to
> e2fsprogs.

Ah, sorry, forgot about that. Will do it next time.

								Honza

Patch

diff --git a/e2fsck/unix.c b/e2fsck/unix.c
index cbe5ec5034cb..55e21ea0aeac 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 6774e32c91c2..250fd174cd48 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