linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] e2fsck: fix checks done for mounted vs. read-only
@ 2012-05-24 21:34 Andreas Dilger
  2012-05-28 14:51 ` Ted Ts'o
  2012-05-30 18:39 ` [PATCH] e2fsck: allow checking on mounted root filesystem Andreas Dilger
  0 siblings, 2 replies; 7+ messages in thread
From: Andreas Dilger @ 2012-05-24 21:34 UTC (permalink / raw)
  To: tytso, linux-ext4; +Cc: Andreas Dilger

Currently, if e2fsck is run without the "-n" flag (i.e. it
might modify the filesystem), there is no guarantee that it will
open the filesystem with the EXCLUSIVE flag (i.e. O_EXCL) to
prevent the block device from being checked (in most cases this
means mounted, but it could also be an MD/LVM member device).

Conversely, if e2fsck is run with "-n" (i.e. read-only), and
/etc/mtab or /proc/mounts does not report the block device as
mounted then e2fsck thinks the filesystem is unmounted.  In this
case, e2fsck incorrectly sets the EXCLUSIVE flag, which causes
the check to fail, even though e2fsck is running read-only.

To fix this, do not open with EXCLUSIVE if it is a read-only check,
and always open with EXCLUSIVE if the filesystem might be changed.
This also prevents filesystem mounts while e2fsck is running.

Also refuse allow e2fsck to run at all if the filesystem is BUSY.
The e2fsck check_mount() was checking for MOUNTED, but not BUSY,
and it should refuse to run outright if the block device is BUSY.
The previous MOUNTED heuristics pre-date the O_EXCL reservation
by the kernel, so there could be uncertainty due to stale /etc/mtab
data, but with newer kernels a busy device should never be modified.

Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
---
 e2fsck/unix.c |   17 +++++++++--------
 1 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/e2fsck/unix.c b/e2fsck/unix.c
index 53fcd04..6161e46 100644
--- a/e2fsck/unix.c
+++ b/e2fsck/unix.c
@@ -219,7 +219,7 @@ static void check_mount(e2fsck_t ctx)
 	 * filesystem and it's mounted read-only, and we're not doing
 	 * a read/write check, then everything's fine.
 	 */
-	if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
+	if ((!(ctx->mount_flags & (EXT2_MF_MOUNTED | EXT2_MF_BUSY))) ||
 	    ((ctx->mount_flags & EXT2_MF_ISROOT) &&
 	     (ctx->mount_flags & EXT2_MF_READONLY) &&
 	     !(ctx->options & E2F_OPT_WRITECHECK)))
@@ -227,13 +227,16 @@ static void check_mount(e2fsck_t ctx)
 
 	if ((ctx->options & E2F_OPT_READONLY) &&
 	    !(ctx->options & E2F_OPT_WRITECHECK)) {
-		log_out(ctx, _("Warning!  %s is mounted.\n"),
-			ctx->filesystem_name);
+		log_out(ctx, _("Warning!  %s is %s.\n"),
+			ctx->filesystem_name,
+			ctx->mount_flags & EXT2_MF_MOUNTED ?
+				"mounted" : "in use");
 		return;
 	}
 
-	log_out(ctx, _("%s is mounted.  "), ctx->filesystem_name);
-	if (!ctx->interactive)
+	log_out(ctx, _("%s is %s.\n"), ctx->filesystem_name,
+		ctx->mount_flags & EXT2_MF_MOUNTED ? "mounted" : "in use");
+	if (!ctx->interactive || ctx->mount_flags & EXT2_MF_BUSY)
 		fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
 	puts("\007\007\007\007");
 	log_out(ctx, _("\n\nWARNING!!!  "
@@ -1219,9 +1222,7 @@ restart:
 	if (!old_bitmaps)
 		flags |= EXT2_FLAG_64BITS;
 	if ((ctx->options & E2F_OPT_READONLY) == 0)
-		flags |= EXT2_FLAG_RW;
-	if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0)
-		flags |= EXT2_FLAG_EXCLUSIVE;
+		flags |= EXT2_FLAG_RW | EXT2_FLAG_EXCLUSIVE;
 
 	retval = try_open_fs(ctx, flags, io_ptr, &fs);
 
-- 
1.7.3.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-05-31 21:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-24 21:34 [PATCH] e2fsck: fix checks done for mounted vs. read-only Andreas Dilger
2012-05-28 14:51 ` Ted Ts'o
2012-05-30 17:57   ` Andreas Dilger
2012-05-30 18:39 ` [PATCH] e2fsck: allow checking on mounted root filesystem Andreas Dilger
2012-05-30 22:55   ` [PATCH] e2fsck: allow checking on mounted root fs (v2) Andreas Dilger
2012-05-31 21:35     ` Ted Ts'o
2012-05-30 22:55   ` [PATCH] e2fsck: allow checking on mounted root filesystem Andreas Dilger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).