Skip to contentSkip to conversation

fuse2fs: open read-only when ro option and image non-writable

PATCH In review Dave Dykstra 3 participants 6 messages Oct 16, 2025 – Oct 17, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fuse2fs: open read-only when ro option and image non-writable

This opens the image read-only when an ro option is given and the image is not writable. If it is then found that a journal recovery is needed, an error is returned then.

The ret value is set to 4 after the option checks so that if there's an error resulting in "goto out" it won't print an error about unrecognized options.

Also submitted as PR https://github.com/tytso/e2fsprogs/pull/250
for the issue https://github.com/tytso/e2fsprogs/issues/244.

Replaces

    https://lore.kernel.org/linux-ext4/[email protected]/T/#u
    https://lore.kernel.org/linux-ext4/175798064776.350013.6744611652039454651.stgit@frogsfrogsfrogs/#t
---
 misc/fuse2fs.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
misc/fuse2fs.c +23 -4
index cb5620c7..2ae2fc1a 100644
@@ -4696,9 +4696,19 @@ int main(int argc, char *argv[])
 	err = ext2fs_open2(fctx.device, options, flags, 0, 0, unix_io_manager,
 			   &global_fs);
 	if (err) {
-		err_printf(&fctx, "%s.\n", error_message(err));
-		err_printf(&fctx, "%s\n", _("Please run e2fsck -fy."));
-		goto out;
+		if (((err == EACCES) || (err == EPERM)) && fctx.ro) {
+			// read-only requested and don't have write access
+			dbg_printf(&fctx, "%s: %s\n", __func__,
+ _("Permission denied with writable, trying without.\n"));
+			flags &= ~EXT2_FLAG_RW;
+			err = ext2fs_open2(fctx.device, options, flags, 0, 0, 
+					   unix_io_manager, &global_fs);
+		}
+		if (err) {
+			err_printf(&fctx, "%s.\n", error_message(err));
+			err_printf(&fctx, "%s\n", _("Please run e2fsck -fy."));
+			goto out;
+		}
 	}
 	fctx.fs = global_fs;
 	global_fs->priv_data = &fctx;
@@ -4741,6 +4751,8 @@ int main(int argc, char *argv[])
 		goto out;
 	}
 
+	ret = 4;
+
 	if (global_fs->super->s_state & EXT2_ERROR_FS) {
 		err_printf(&fctx, "%s\n",
  _("Errors detected; running e2fsck is required."));
@@ -4760,6 +4772,11 @@ int main(int argc, char *argv[])
  _("Mounting read-only without recovering journal."));
 			fctx.ro = 1;
 			global_fs->flags &= ~EXT2_FLAG_RW;
+		} else if (fctx.ro && !(flags & EXT2_FLAG_RW)) {
+			err_printf(&fctx, "%s\n",
+ _("Journal needs recovery but filesystem could not be opened read-write."));
+			err_printf(&fctx, "%s\n", _("Please run e2fsck -fy."));
+			goto out;
 		} else {
 			log_printf(&fctx, "%s\n", _("Recovering journal."));
 			err = ext2fs_run_ext3_journal(&global_fs);
@@ -4833,8 +4850,10 @@ int main(int argc, char *argv[])
 	if (fctx.no_default_opts == 0)
 		fuse_opt_add_arg(&args, extra_args);
 
-	if (fctx.ro)
+	if (fctx.ro) {
+		/* This is in case ro was implied above and not passed in */
 		fuse_opt_add_arg(&args, "-oro");
+	}
 
 	if (fctx.fakeroot) {
 #ifdef HAVE_MOUNT_NODEV
Signature · 1 line
2.43.5
#1 lore raw
Quoted 25 lines · Dave Dykstra

This opens the image read-only when an ro option is given and the image is not writable. If it is then found that a journal recovery is needed, an error is returned then.

The ret value is set to 4 after the option checks so that if there's an error resulting in "goto out" it won't print an error about unrecognized options.

Also submitted as PR https://github.com/tytso/e2fsprogs/pull/250
for the issue https://github.com/tytso/e2fsprogs/issues/244.

Replaces

    https://lore.kernel.org/linux-ext4/[email protected]/T/#u
    https://lore.kernel.org/linux-ext4/175798064776.350013.6744611652039454651.stgit@frogsfrogsfrogs/#t
---
 misc/fuse2fs.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
misc/fuse2fs.c +0 -0
index cb5620c7..2ae2fc1a 100644

misc/fuse2fs.c int main(int argc, char *argv[]) · line 4699

Quoted 7 lines
misc/fuse2fs.c +1 -3
@@ -4696,9 +4696,19 @@ int main(int argc, char *argv[])
 	err = ext2fs_open2(fctx.device, options, flags, 0, 0, unix_io_manager,
 			   &global_fs);
 	if (err) {
-		err_printf(&fctx, "%s.\n", error_message(err));
-		err_printf(&fctx, "%s\n", _("Please run e2fsck -fy."));
-		goto out;
+		if (((err == EACCES) || (err == EPERM)) && fctx.ro) {

This is not correct. mount(8) for the kernel ext4 driver responds to the block device being readonly by retrying with an ro mount. The user is not required to specify 'ro':

# blockdev --setro/ dev/sda
# strace -e mount mount /dev/sda /mnt
40677<mount> 12:19:36 (+     0.000102) mount("/dev/sda", "/mnt", "ext4", 0, NULL) = -1 EACCES (Permission denied)
40677<mount> 12:19:36 (+     0.000285) mount("/dev/sda", "/mnt", "ext4", MS_RDONLY, NULL) = 0
Quoted 16 lines
misc/fuse2fs.c +12 -0
+			// read-only requested and don't have write access
+			dbg_printf(&fctx, "%s: %s\n", __func__,
+ _("Permission denied with writable, trying without.\n"));
+			flags &= ~EXT2_FLAG_RW;
+			err = ext2fs_open2(fctx.device, options, flags, 0, 0, 
+					   unix_io_manager, &global_fs);
+		}
+		if (err) {
+			err_printf(&fctx, "%s.\n", error_message(err));
+			err_printf(&fctx, "%s\n", _("Please run e2fsck -fy."));
+			goto out;
+		}
 	}
 	fctx.fs = global_fs;
 	global_fs->priv_data = &fctx;

misc/fuse2fs.c int main(int argc, char *argv[]) · line 4754

Quoted 4 lines
misc/fuse2fs.c +1 -0
@@ -4741,6 +4751,8 @@ int main(int argc, char *argv[])
 		goto out;
 	}
 
+	ret = 4;

Why 4? Is this an internal mount bug?

--D

Quoted 32 lines
misc/fuse2fs.c +9 -1
+
 	if (global_fs->super->s_state & EXT2_ERROR_FS) {
 		err_printf(&fctx, "%s\n",
  _("Errors detected; running e2fsck is required."));
@@ -4760,6 +4772,11 @@ int main(int argc, char *argv[])
  _("Mounting read-only without recovering journal."));
 			fctx.ro = 1;
 			global_fs->flags &= ~EXT2_FLAG_RW;
+		} else if (fctx.ro && !(flags & EXT2_FLAG_RW)) {
+			err_printf(&fctx, "%s\n",
+ _("Journal needs recovery but filesystem could not be opened read-write."));
+			err_printf(&fctx, "%s\n", _("Please run e2fsck -fy."));
+			goto out;
 		} else {
 			log_printf(&fctx, "%s\n", _("Recovering journal."));
 			err = ext2fs_run_ext3_journal(&global_fs);
@@ -4833,8 +4850,10 @@ int main(int argc, char *argv[])
 	if (fctx.no_default_opts == 0)
 		fuse_opt_add_arg(&args, extra_args);
 
-	if (fctx.ro)
+	if (fctx.ro) {
+		/* This is in case ro was implied above and not passed in */
 		fuse_opt_add_arg(&args, "-oro");
+	}
 
 	if (fctx.fakeroot) {
 #ifdef HAVE_MOUNT_NODEV
Signature · 1 line
2.43.5
#2 lore raw
Quoted 1 line · Darrick J. Wong

On Thu, Oct 16, 2025 at 03:02:06PM -0500, Dave Dykstra wrote:

...

Quoted 17 lines

misc/fuse2fs.c int main(int argc, char *argv[]) · line 4699

Quoted 12 lines
misc/fuse2fs.c +1 -3
index cb5620c7..2ae2fc1a 100644
@@ -4696,9 +4696,19 @@ int main(int argc, char *argv[])
 	err = ext2fs_open2(fctx.device, options, flags, 0, 0, unix_io_manager,
 			   &global_fs);
 	if (err) {
-		err_printf(&fctx, "%s.\n", error_message(err));
-		err_printf(&fctx, "%s\n", _("Please run e2fsck -fy."));
-		goto out;
+		if (((err == EACCES) || (err == EPERM)) && fctx.ro) {

This is not correct. mount(8) for the kernel ext4 driver responds to the block device being readonly by retrying with an ro mount. The user is not required to specify 'ro':

Quoted 4 lines
# blockdev --setro/ dev/sda
# strace -e mount mount /dev/sda /mnt
40677<mount> 12:19:36 (+     0.000102) mount("/dev/sda", "/mnt", "ext4", 0, NULL) = -1 EACCES (Permission denied)
40677<mount> 12:19:36 (+     0.000285) mount("/dev/sda", "/mnt", "ext4", MS_RDONLY, NULL) = 0

Ok then, I'll change that.

...

Quoted 9 lines

misc/fuse2fs.c int main(int argc, char *argv[]) · line 4754

Quoted 7 lines
misc/fuse2fs.c +1 -0
 	fctx.fs = global_fs;
 	global_fs->priv_data = &fctx;
@@ -4741,6 +4751,8 @@ int main(int argc, char *argv[])
 		goto out;
 	}
 
+	ret = 4;

Why 4? Is this an internal mount bug?

I was just guessing; I didn't know what the bits meant. Where are they documented? All I knew was that it needed to not include the "& 1" bit which was included with "ret = 3" because that printed an erroneous error just after the "out" label.

Dave

#3 lore raw
Quoted 1 line · Dave Dykstra

On Fri, Oct 17, 2025 at 12:24:56PM -0700, Darrick J. Wong wrote:

...

Quoted 8 lines
Quoted 3 lines

misc/fuse2fs.c int main(int argc, char *argv[]) · line 4754

Quoted 1 line
misc/fuse2fs.c +1 -0
+	ret = 4;

Why 4? Is this an internal mount bug?

I was just guessing; I didn't know what the bits meant. Where are they documented? All I knew was that it needed to not include the "& 1" bit which was included with "ret = 3" because that printed an erroneous error just after the "out" label.

Oh, it's supposed to follow the return codes from mount(8). I think ret = 2 is most appropriate then.

Dave

#5 lore raw
In reply to Dave Dykstra
Quoted 7 lines · Dave Dykstra
Quoted 1 line · Dave Dykstra

On Fri, Oct 17, 2025 at 12:24:56PM -0700, Darrick J. Wong wrote:

...

Quoted 4 lines
Quoted 3 lines

misc/fuse2fs.c int main(int argc, char *argv[]) · line 4754

Quoted 1 line
misc/fuse2fs.c +1 -0
+	ret = 4;

Why 4? Is this an internal mount bug?

Quoted 7 lines
Quoted 4 lines

I was just guessing; I didn't know what the bits meant. Where are they documented? All I knew was that it needed to not include the "& 1" bit which was included with "ret = 3" because that printed an erroneous error just after the "out" label.

Oh, it's supposed to follow the return codes from mount(8). I think ret = 2 is most appropriate then.

Could you please spend more time understanding the context in which fuse servers operate, particularly when they're called as mount(8) helpers?

--D

Quoted 3 lines

Dave