[PATCH 09/10] fuse4fs: make MMP work correctly in safe service mode

Darrick J. Wong
2026-04-22T23:25:56Z

Normally, the libext2fs MMP code open()s a complete separate file
descriptor to read and write the MMP block so that it can have its own
private open file with its own access mode and file position. However,
if the unixfd IO manager is in use, it will reuse the io channel, which
means that MMP and the unixfd share the same open file and hence the
access mode and file position.

MMP requires directio access to block devices so that changes are
immediately visible on other nodes. Therefore, we need the IO channel
(and thus the filesystem) to be running in directio mode if MMP is in
use.

To make this work correctly with the sole unixfd IO manager user
(fuse4fs in unprivileged service mode), we must set O_DIRECT on the
bdev fd and mount the filesystem in directio mode.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
fuse4fs/fuse4fs.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 48 insertions(+), 3 deletions(-)

diff --git a/fuse4fs/fuse4fs.c b/fuse4fs/fuse4fs.c
index bf7c8ef9102a7a..dc5a0ede9f5072 100644
--- a/fuse4fs/fuse4fs.c
+++ b/fuse4fs/fuse4fs.c
@@ -1371,12 +1371,57 @@ static int fuse4fs_service_get_config(struct fuse4fs *ff)
}

static errcode_t fuse4fs_service_openfs(struct fuse4fs *ff, char *options,
- int flags)
+ int *flags)
{
+ struct stat statbuf;
char path[64];
+ errcode_t retval;
+ int ret;

+ ret = fstat(ff->bdev_fd, &statbuf);
+ if (ret)
+ return errno;
+
+ /*
+ * Open the filesystem with SKIP_MMP so that we can find out if the
+ * filesystem actually has MMP.
+ */
snprintf(path, sizeof(path), "/dev/fd/%d", ff->bdev_fd);
- return ext2fs_open2(path, options, flags, 0, 0, unixfd_io_manager,
+ retval = ext2fs_open2(path, options, *flags | EXT2_FLAG_SKIP_MMP, 0, 0,
+ unixfd_io_manager, &ff->fs);
+ if (retval)
+ return retval;
+
+ /*
+ * If the fs doesn't have MMP then we're good to go. Otherwise close
+ * the filesystem so that we can reopen it with MMP enabled.
+ */
+ if (!ext2fs_has_feature_mmp(ff->fs->super))
+ return 0;
+
+ retval = ext2fs_close_free(&ff->fs);
+ if (retval)
+ return retval;
+
+ /*
+ * If the filesystem is not on a regular file, MMP will share the same
+ * fd as the unixfd IO channel. We need to set O_DIRECT on the bdev_fd
+ * and open the filesystem in directio mode.
+ */
+ if (!S_ISREG(statbuf.st_mode)) {
+ int fflags = fcntl(ff->bdev_fd, F_GETFL);
+
+ if (!(fflags & O_DIRECT)) {
+ ret = fcntl(ff->bdev_fd, F_SETFL, fflags | O_DIRECT);
+ if (ret)
+ return EXT2_ET_MMP_OPEN_DIRECT;
+ }
+
+ ff->directio = 1;
+ *flags |= EXT2_FLAG_DIRECT_IO;
+ }
+
+ return ext2fs_open2(path, options, *flags, 0, 0, unixfd_io_manager,
&ff->fs);
}
#else
@@ -1516,7 +1561,7 @@ static errcode_t fuse4fs_open(struct fuse4fs *ff)
deadline = init_deadline(FUSE4FS_OPEN_TIMEOUT);
do {
if (fuse4fs_is_service(ff))
- err = fuse4fs_service_openfs(ff, options, flags);
+ err = fuse4fs_service_openfs(ff, options, &flags);
else
err = ext2fs_open2(ff->device, options, flags, 0, 0,
unix_io_manager, &ff->fs);