[PATCH 7/9] fuse2fs: clean up operation completion

Darrick J. Wong
2025-11-06T22:38:00Z

Create a helper to release the BFL and log any errors. This cuts down
on the boilerplate.

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

diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index 3baeec67c2b4b7..77d0eddbe805dc 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -498,6 +498,15 @@ static inline ext2_filsys fuse2fs_start(struct fuse2fs *ff)
return ff->fs;
}

+static inline void __fuse2fs_finish(struct fuse2fs *ff, int ret,
+ const char *func)
+{
+ if (ret)
+ dbg_printf(ff, "%s: libfuse ret=%d\n", func, ret);
+ pthread_mutex_unlock(&ff->bfl);
+}
+#define fuse2fs_finish(ff, ret) __fuse2fs_finish((ff), (ret), __func__)
+
#ifdef CONFIG_MMP
static bool fuse2fs_mmp_wanted(const struct fuse2fs *ff)
{
@@ -552,7 +561,7 @@ static void fuse2fs_mmp_bthread(void *data)
fuse2fs_start(ff);
if (fuse2fs_mmp_wanted(ff) && !bthread_cancelled(ff->mmp_thread))
fuse2fs_mmp_touch(ff, false);
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, 0);
}

static void fuse2fs_mmp_start(struct fuse2fs *ff)
@@ -1029,7 +1038,7 @@ static void op_destroy(void *p EXT2FS_ATTR((unused)))
log_printf(ff, "%s %s.\n", _("unmounting filesystem"), uuid);
}

- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, 0);
}

/* Reopen @stream with @fileno */
@@ -1325,7 +1334,7 @@ static int op_getattr(const char *path, struct stat *statbuf
goto out;
ret = stat_inode(fs, ino, statbuf);
out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -1398,7 +1407,7 @@ static int op_readlink(const char *path, char *buf, size_t len)
}

out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -1707,7 +1716,7 @@ static int op_mknod(const char *path, mode_t mode, dev_t dev)
goto out2;

out2:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
out:
free(temp_path);
return ret;
@@ -1842,7 +1851,7 @@ static int op_mkdir(const char *path, mode_t mode)
out3:
ext2fs_free_mem(&block);
out2:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
out:
free(temp_path);
return ret;
@@ -2055,7 +2064,7 @@ static int op_unlink(const char *path)
FUSE2FS_CHECK_CONTEXT(ff);
fuse2fs_start(ff);
ret = __op_unlink(ff, path);
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -2174,7 +2183,7 @@ static int op_rmdir(const char *path)
FUSE2FS_CHECK_CONTEXT(ff);
fuse2fs_start(ff);
ret = __op_rmdir(ff, path);
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -2282,7 +2291,7 @@ static int op_symlink(const char *src, const char *dest)
goto out2;

out2:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
out:
free(temp_path);
return ret;
@@ -2587,7 +2596,7 @@ static int op_rename(const char *from, const char *to
free(temp_from);
free(temp_to);
out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -2687,7 +2696,7 @@ static int op_link(const char *src, const char *dest)
goto out2;

out2:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
out:
free(temp_path);
return ret;
@@ -2847,7 +2856,7 @@ static int op_chmod(const char *path, mode_t mode
}

out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -2917,7 +2926,7 @@ static int op_chown(const char *path, uid_t owner, gid_t group
}

out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -3020,7 +3029,7 @@ static int op_truncate(const char *path, off_t len
goto out;

out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -3137,7 +3146,7 @@ static int op_open(const char *path, struct fuse_file_info *fp)
FUSE2FS_CHECK_CONTEXT(ff);
fuse2fs_start(ff);
ret = __op_open(ff, path, fp);
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -3193,7 +3202,7 @@ static int op_read(const char *path EXT2FS_ATTR((unused)), char *buf,
goto out;
}
out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return got ? (int) got : ret;
}

@@ -3265,7 +3274,7 @@ static int op_write(const char *path EXT2FS_ATTR((unused)),
goto out;

out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return got ? (int) got : ret;
}

@@ -3294,7 +3303,7 @@ static int op_release(const char *path EXT2FS_ATTR((unused)),
}

fp->fh = 0;
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);

ext2fs_free_mem(&fh);

@@ -3323,7 +3332,7 @@ static int op_fsync(const char *path EXT2FS_ATTR((unused)),
if (err)
ret = translate_error(fs, fh->ino, err);
}
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);

return ret;
}
@@ -3372,7 +3381,7 @@ static int op_statfs(const char *path EXT2FS_ATTR((unused)),
if (!(fs->flags & EXT2_FLAG_RW))
buf->f_flag |= ST_RDONLY;
buf->f_namemax = EXT2_NAME_LEN;
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, 0);

return 0;
}
@@ -3446,7 +3455,7 @@ static int op_getxattr(const char *path, const char *key, char *value,

ext2fs_free_mem(&ptr);
out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);

return ret;
}
@@ -3543,7 +3552,7 @@ static int op_listxattr(const char *path, char *names, size_t len)
if (err && !ret)
ret = translate_error(fs, ino, err);
out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);

return ret;
}
@@ -3636,7 +3645,7 @@ static int op_setxattr(const char *path EXT2FS_ATTR((unused)),
if (!ret && err)
ret = translate_error(fs, ino, err);
out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);

return ret;
}
@@ -3731,7 +3740,7 @@ static int op_removexattr(const char *path, const char *key)
if (err && !ret)
ret = translate_error(fs, ino, err);
out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);

return ret;
}
@@ -3868,7 +3877,7 @@ static int op_readdir(const char *path EXT2FS_ATTR((unused)),
goto out;
}
out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -3895,7 +3904,7 @@ static int op_access(const char *path, int mask)
goto out;

out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -4023,7 +4032,7 @@ static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp)
goto out2;

out2:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
out:
free(temp_path);
return ret;
@@ -4078,7 +4087,7 @@ static int op_ftruncate(const char *path EXT2FS_ATTR((unused)),
goto out;

out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -4098,7 +4107,7 @@ static int op_fgetattr(const char *path EXT2FS_ATTR((unused)),
dbg_printf(ff, "%s: ino=%d\n", __func__, fh->ino);
fs = fuse2fs_start(ff);
ret = stat_inode(fs, fh->ino, statbuf);
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);

return ret;
}
@@ -4171,7 +4180,7 @@ static int op_utimens(const char *path, const struct timespec ctv[2]
}

out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -4566,7 +4575,7 @@ static int op_ioctl(const char *path EXT2FS_ATTR((unused)),
dbg_printf(ff, "%s: Unknown ioctl %d\n", __func__, cmd);
ret = -ENOTTY;
}
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);

return ret;
}
@@ -4598,7 +4607,7 @@ static int op_bmap(const char *path, size_t blocksize EXT2FS_ATTR((unused)),
}

out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);
return ret;
}

@@ -4854,7 +4863,7 @@ static int op_fallocate(const char *path EXT2FS_ATTR((unused)), int mode,
else
ret = fuse2fs_allocate_range(ff, fh, mode, offset, len);
out:
- pthread_mutex_unlock(&ff->bfl);
+ fuse2fs_finish(ff, ret);

return ret;
}