[PATCH 2/3] fuse2fs: hoist lockfile code

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

Hoist the lockfile handling code into separate helpers before we start
rearranging the code that opens and closes filesystems.

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

diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index 0020d149949835..cefc6442ae45ec 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -1047,6 +1047,54 @@ static errcode_t fuse2fs_check_support(struct fuse2fs *ff)
return 0;
}

+static errcode_t fuse2fs_acquire_lockfile(struct fuse2fs *ff)
+{
+ char *resolved;
+ int lockfd;
+ errcode_t err;
+
+ lockfd = open(ff->lockfile, O_RDWR | O_CREAT | O_EXCL, 0400);
+ if (lockfd < 0) {
+ if (errno == EEXIST)
+ err = EWOULDBLOCK;
+ else
+ err = errno;
+ err_printf(ff, "%s: %s: %s\n", ff->lockfile,
+ _("opening lockfile failed"),
+ strerror(err));
+ ff->lockfile = NULL;
+ return err;
+ }
+ close(lockfd);
+
+ resolved = realpath(ff->lockfile, NULL);
+ if (!resolved) {
+ err = errno;
+ err_printf(ff, "%s: %s: %s\n", ff->lockfile,
+ _("resolving lockfile failed"),
+ strerror(err));
+ unlink(ff->lockfile);
+ ff->lockfile = NULL;
+ return err;
+ }
+ free(ff->lockfile);
+ ff->lockfile = resolved;
+
+ return 0;
+}
+
+static void fuse2fs_release_lockfile(struct fuse2fs *ff)
+{
+ if (unlink(ff->lockfile)) {
+ errcode_t err = errno;
+
+ err_printf(ff, "%s: %s: %s\n", ff->lockfile,
+ _("removing lockfile failed"),
+ strerror(err));
+ }
+ free(ff->lockfile);
+}
+
static void op_destroy(void *p EXT2FS_ATTR((unused)))
{
struct fuse2fs *ff = fuse2fs_get();
@@ -5212,38 +5260,9 @@ int main(int argc, char *argv[])
fctx.alloc_all_blocks = 1;
}

- if (fctx.lockfile) {
- char *resolved;
- int lockfd;
-
- lockfd = open(fctx.lockfile, O_RDWR | O_CREAT | O_EXCL, 0400);
- if (lockfd < 0) {
- if (errno == EEXIST)
- err = EWOULDBLOCK;
- else
- err = errno;
- err_printf(&fctx, "%s: %s: %s\n", fctx.lockfile,
- _("opening lockfile failed"),
- strerror(err));
- fctx.lockfile = NULL;
- ret |= 32;
- goto out;
- }
- close(lockfd);
-
- resolved = realpath(fctx.lockfile, NULL);
- if (!resolved) {
- err = errno;
- err_printf(&fctx, "%s: %s: %s\n", fctx.lockfile,
- _("resolving lockfile failed"),
- strerror(err));
- unlink(fctx.lockfile);
- fctx.lockfile = NULL;
- ret |= 32;
- goto out;
- }
- free(fctx.lockfile);
- fctx.lockfile = resolved;
+ if (fctx.lockfile && fuse2fs_acquire_lockfile(&fctx)) {
+ ret |= 32;
+ goto out;
}

/* Start up the fs (while we still can use stdout) */
@@ -5547,15 +5566,8 @@ int main(int argc, char *argv[])
if (err)
com_err(argv[0], err, "while closing fs");
}
- if (fctx.lockfile) {
- if (unlink(fctx.lockfile)) {
- err = errno;
- err_printf(&fctx, "%s: %s: %s\n", fctx.lockfile,
- _("removing lockfile failed"),
- strerror(err));
- }
- free(fctx.lockfile);
- }
+ if (fctx.lockfile)
+ fuse2fs_release_lockfile(&fctx);
if (fctx.device)
free(fctx.device);
pthread_mutex_destroy(&fctx.bfl);