[PATCH 3/3] fuse2fs: hoist unmount code from main

Darrick J. Wong
2025-11-06T22:39:20Z

Hoist the unmount code into a separate function so that we can reduce
the complexity of main(). This also sets us up for unmounting the
filesystem from op_destroy, which we'll need for fuse2fs+iomap mode to
maintain the expected behavior that the block device is free when
umount(8) returns.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
misc/fuse2fs.c | 36 ++++++++++++++++++++++--------------
1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index cefc6442ae45ec..b096c3f496d740 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -1095,6 +1095,27 @@ static void fuse2fs_release_lockfile(struct fuse2fs *ff)
free(ff->lockfile);
}

+static void fuse2fs_unmount(struct fuse2fs *ff)
+{
+ char uuid[UUID_STR_SIZE];
+ errcode_t err;
+
+ if (ff->fs) {
+ uuid_unparse(ff->fs->super->s_uuid, uuid);
+ err = ext2fs_close_free(&ff->fs);
+ if (err)
+ err_printf(ff, "%s: %s\n", _("while closing fs"),
+ error_message(err));
+
+ if (ff->kernel)
+ log_printf(ff, "%s %s.\n", _("unmounted filesystem"),
+ uuid);
+ }
+
+ if (ff->lockfile)
+ fuse2fs_release_lockfile(ff);
+}
+
static void op_destroy(void *p EXT2FS_ATTR((unused)))
{
struct fuse2fs *ff = fuse2fs_get();
@@ -1133,13 +1154,6 @@ static void op_destroy(void *p EXT2FS_ATTR((unused)))
(stats->cache_hits + stats->cache_misses));
}

- if (ff->kernel) {
- char uuid[UUID_STR_SIZE];
-
- uuid_unparse(fs->super->s_uuid, uuid);
- log_printf(ff, "%s %s.\n", _("unmounting filesystem"), uuid);
- }
-
fuse2fs_finish(ff, 0);
}

@@ -5561,13 +5575,7 @@ int main(int argc, char *argv[])
fflush(orig_stderr);
}
fuse2fs_mmp_destroy(&fctx);
- if (fctx.fs) {
- err = ext2fs_close_free(&fctx.fs);
- if (err)
- com_err(argv[0], err, "while closing fs");
- }
- if (fctx.lockfile)
- fuse2fs_release_lockfile(&fctx);
+ fuse2fs_unmount(&fctx);
if (fctx.device)
free(fctx.device);
pthread_mutex_destroy(&fctx.bfl);