[PATCH 6/9] fuse2fs: clean up operation startup

Darrick J. Wong
2025-11-06T22:37:44Z

Create a helper to take the BFL and give us a reference to the
ext2_filsys that we're protecting with the BFL. This eliminates a
theoretical race with any code that sets or clears fuse2fs:fs. But
really it just cuts down on the boilerplate.

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

diff --git a/misc/fuse2fs.c b/misc/fuse2fs.c
index 63c1227faa0d7a..3baeec67c2b4b7 100644
--- a/misc/fuse2fs.c
+++ b/misc/fuse2fs.c
@@ -492,6 +492,12 @@ static inline errcode_t fuse2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
sizeof(*inode));
}

+static inline ext2_filsys fuse2fs_start(struct fuse2fs *ff)
+{
+ pthread_mutex_lock(&ff->bfl);
+ return ff->fs;
+}
+
#ifdef CONFIG_MMP
static bool fuse2fs_mmp_wanted(const struct fuse2fs *ff)
{
@@ -543,7 +549,7 @@ static void fuse2fs_mmp_bthread(void *data)
{
struct fuse2fs *ff = data;

- pthread_mutex_lock(&ff->bfl);
+ fuse2fs_start(ff);
if (fuse2fs_mmp_wanted(ff) && !bthread_cancelled(ff->mmp_thread))
fuse2fs_mmp_touch(ff, false);
pthread_mutex_unlock(&ff->bfl);
@@ -986,8 +992,7 @@ static void op_destroy(void *p EXT2FS_ATTR((unused)))

FUSE2FS_CHECK_CONTEXT_DESTROY(ff);

- pthread_mutex_lock(&ff->bfl);
- fs = ff->fs;
+ fs = fuse2fs_start(ff);

dbg_printf(ff, "%s: dev=%s\n", __func__, fs->device_name);
if (fs->flags & EXT2_FLAG_RW) {
@@ -1314,8 +1319,7 @@ static int op_getattr(const char *path, struct stat *statbuf
int ret = 0;

FUSE2FS_CHECK_CONTEXT(ff);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
ret = fuse2fs_file_ino(ff, path, fi, &ino);
if (ret)
goto out;
@@ -1339,8 +1343,7 @@ static int op_readlink(const char *path, char *buf, size_t len)

FUSE2FS_CHECK_CONTEXT(ff);
dbg_printf(ff, "%s: path=%s\n", __func__, path);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
@@ -1612,8 +1615,7 @@ static int op_mknod(const char *path, mode_t mode, dev_t dev)
a = *node_name;
*node_name = 0;

- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!fs_can_allocate(ff, 2)) {
ret = -ENOSPC;
goto out2;
@@ -1743,8 +1745,7 @@ static int op_mkdir(const char *path, mode_t mode)
a = *node_name;
*node_name = 0;

- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!fs_can_allocate(ff, 1)) {
ret = -ENOSPC;
goto out2;
@@ -2052,7 +2053,7 @@ static int op_unlink(const char *path)
int ret;

FUSE2FS_CHECK_CONTEXT(ff);
- pthread_mutex_lock(&ff->bfl);
+ fuse2fs_start(ff);
ret = __op_unlink(ff, path);
pthread_mutex_unlock(&ff->bfl);
return ret;
@@ -2171,7 +2172,7 @@ static int op_rmdir(const char *path)
int ret;

FUSE2FS_CHECK_CONTEXT(ff);
- pthread_mutex_lock(&ff->bfl);
+ fuse2fs_start(ff);
ret = __op_rmdir(ff, path);
pthread_mutex_unlock(&ff->bfl);
return ret;
@@ -2206,8 +2207,7 @@ static int op_symlink(const char *src, const char *dest)
a = *node_name;
*node_name = 0;

- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!fs_can_allocate(ff, 1)) {
ret = -ENOSPC;
goto out2;
@@ -2372,8 +2372,7 @@ static int op_rename(const char *from, const char *to

FUSE2FS_CHECK_CONTEXT(ff);
dbg_printf(ff, "%s: renaming %s to %s\n", __func__, from, to);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!fs_can_allocate(ff, 5)) {
ret = -ENOSPC;
goto out;
@@ -2620,8 +2619,7 @@ static int op_link(const char *src, const char *dest)
a = *node_name;
*node_name = 0;

- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!fs_can_allocate(ff, 2)) {
ret = -ENOSPC;
goto out2;
@@ -2797,8 +2795,7 @@ static int op_chmod(const char *path, mode_t mode
int ret = 0;

FUSE2FS_CHECK_CONTEXT(ff);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
ret = fuse2fs_file_ino(ff, path, fi, &ino);
if (ret)
goto out;
@@ -2869,8 +2866,7 @@ static int op_chown(const char *path, uid_t owner, gid_t group
int ret = 0;

FUSE2FS_CHECK_CONTEXT(ff);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
ret = fuse2fs_file_ino(ff, path, fi, &ino);
if (ret)
goto out;
@@ -3009,7 +3005,7 @@ static int op_truncate(const char *path, off_t len
int ret = 0;

FUSE2FS_CHECK_CONTEXT(ff);
- pthread_mutex_lock(&ff->bfl);
+ fuse2fs_start(ff);
ret = fuse2fs_file_ino(ff, path, fi, &ino);
if (ret)
goto out;
@@ -3139,7 +3135,7 @@ static int op_open(const char *path, struct fuse_file_info *fp)
int ret;

FUSE2FS_CHECK_CONTEXT(ff);
- pthread_mutex_lock(&ff->bfl);
+ fuse2fs_start(ff);
ret = __op_open(ff, path, fp);
pthread_mutex_unlock(&ff->bfl);
return ret;
@@ -3163,8 +3159,7 @@ static int op_read(const char *path EXT2FS_ATTR((unused)), char *buf,
FUSE2FS_CHECK_HANDLE(ff, fh);
dbg_printf(ff, "%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino,
(intmax_t) offset, len);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
if (err) {
ret = translate_error(fs, fh->ino, err);
@@ -3220,8 +3215,7 @@ static int op_write(const char *path EXT2FS_ATTR((unused)),
FUSE2FS_CHECK_HANDLE(ff, fh);
dbg_printf(ff, "%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino,
(intmax_t) offset, (intmax_t) len);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!fs_writeable(fs)) {
ret = -EROFS;
goto out;
@@ -3289,8 +3283,7 @@ static int op_release(const char *path EXT2FS_ATTR((unused)),
FUSE2FS_CHECK_CONTEXT(ff);
FUSE2FS_CHECK_HANDLE(ff, fh);
dbg_printf(ff, "%s: ino=%d\n", __func__, fh->ino);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);

if ((fp->flags & O_SYNC) &&
fs_writeable(fs) &&
@@ -3323,8 +3316,7 @@ static int op_fsync(const char *path EXT2FS_ATTR((unused)),
FUSE2FS_CHECK_CONTEXT(ff);
FUSE2FS_CHECK_HANDLE(ff, fh);
dbg_printf(ff, "%s: ino=%d\n", __func__, fh->ino);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
/* For now, flush everything, even if it's slow */
if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) {
err = ext2fs_flush2(fs, 0);
@@ -3347,8 +3339,7 @@ static int op_statfs(const char *path EXT2FS_ATTR((unused)),

FUSE2FS_CHECK_CONTEXT(ff);
dbg_printf(ff, "%s: path=%s\n", __func__, path);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
buf->f_bsize = fs->blocksize;
buf->f_frsize = 0;

@@ -3423,8 +3414,7 @@ static int op_getxattr(const char *path, const char *key, char *value,
return -ENODATA;

FUSE2FS_CHECK_CONTEXT(ff);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!ext2fs_has_feature_xattr(fs->super)) {
ret = -ENOTSUP;
goto out;
@@ -3495,8 +3485,7 @@ static int op_listxattr(const char *path, char *names, size_t len)
int ret = 0;

FUSE2FS_CHECK_CONTEXT(ff);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!ext2fs_has_feature_xattr(fs->super)) {
ret = -ENOTSUP;
goto out;
@@ -3578,8 +3567,7 @@ static int op_setxattr(const char *path EXT2FS_ATTR((unused)),
return -EINVAL;

FUSE2FS_CHECK_CONTEXT(ff);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!ext2fs_has_feature_xattr(fs->super)) {
ret = -ENOTSUP;
goto out;
@@ -3676,8 +3664,7 @@ static int op_removexattr(const char *path, const char *key)
return -ENODATA;

FUSE2FS_CHECK_CONTEXT(ff);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!ext2fs_has_feature_xattr(fs->super)) {
ret = -ENOTSUP;
goto out;
@@ -3866,8 +3853,7 @@ static int op_readdir(const char *path EXT2FS_ATTR((unused)),
FUSE2FS_CHECK_HANDLE(ff, fh);
dbg_printf(ff, "%s: ino=%d offset=%llu\n", __func__, fh->ino,
(unsigned long long)offset);
- i.fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ i.fs = fuse2fs_start(ff);
i.buf = buf;
i.func = fill_func;
err = ext2fs_dir_iterate2(i.fs, fh->ino, 0, NULL, op_readdir_iter, &i);
@@ -3897,8 +3883,7 @@ static int op_access(const char *path, int mask)

FUSE2FS_CHECK_CONTEXT(ff);
dbg_printf(ff, "%s: path=%s mask=0x%x\n", __func__, path, mask);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
@@ -3944,8 +3929,7 @@ static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp)
a = *node_name;
*node_name = 0;

- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!fs_can_allocate(ff, 1)) {
ret = -ENOSPC;
goto out2;
@@ -4062,8 +4046,7 @@ static int op_ftruncate(const char *path EXT2FS_ATTR((unused)),
FUSE2FS_CHECK_HANDLE(ff, fh);
dbg_printf(ff, "%s: ino=%d len=%jd\n", __func__, fh->ino,
(intmax_t) len);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!fs_writeable(fs)) {
ret = -EROFS;
goto out;
@@ -4113,8 +4096,7 @@ static int op_fgetattr(const char *path EXT2FS_ATTR((unused)),
FUSE2FS_CHECK_CONTEXT(ff);
FUSE2FS_CHECK_HANDLE(ff, fh);
dbg_printf(ff, "%s: ino=%d\n", __func__, fh->ino);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
ret = stat_inode(fs, fh->ino, statbuf);
pthread_mutex_unlock(&ff->bfl);

@@ -4139,8 +4121,7 @@ static int op_utimens(const char *path, const struct timespec ctv[2]
int ret = 0;

FUSE2FS_CHECK_CONTEXT(ff);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
ret = fuse2fs_file_ino(ff, path, fi, &ino);
if (ret)
goto out;
@@ -4552,7 +4533,7 @@ static int op_ioctl(const char *path EXT2FS_ATTR((unused)),

FUSE2FS_CHECK_CONTEXT(ff);
FUSE2FS_CHECK_HANDLE(ff, fh);
- pthread_mutex_lock(&ff->bfl);
+ fuse2fs_start(ff);
switch ((unsigned long) cmd) {
#ifdef SUPPORT_I_FLAGS
case EXT2_IOC_GETFLAGS:
@@ -4602,8 +4583,7 @@ static int op_bmap(const char *path, size_t blocksize EXT2FS_ATTR((unused)),
int ret = 0;

FUSE2FS_CHECK_CONTEXT(ff);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err) {
ret = translate_error(fs, 0, err);
@@ -4862,8 +4842,7 @@ static int op_fallocate(const char *path EXT2FS_ATTR((unused)), int mode,

FUSE2FS_CHECK_CONTEXT(ff);
FUSE2FS_CHECK_HANDLE(ff, fh);
- fs = ff->fs;
- pthread_mutex_lock(&ff->bfl);
+ fs = fuse2fs_start(ff);
if (!fs_writeable(fs)) {
ret = -EROFS;
goto out;