]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
fuse2fs: clean up operation startup
authorDarrick J. Wong <djwong@kernel.org>
Thu, 28 Aug 2025 17:30:41 +0000 (10:30 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Mon, 9 Mar 2026 02:14:04 +0000 (19:14 -0700)
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

index 9d98b5652e0e4d14528a583fbef3cc7a1aeeb672..c85f292db4b8318147ea96ab554d4af377223b69 100644 (file)
@@ -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;
@@ -3146,7 +3142,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;
@@ -3170,8 +3166,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);
@@ -3227,8 +3222,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;
@@ -3296,8 +3290,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) &&
@@ -3330,8 +3323,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);
@@ -3354,8 +3346,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;
 
@@ -3430,8 +3421,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;
@@ -3502,8 +3492,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;
@@ -3585,8 +3574,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;
@@ -3683,8 +3671,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;
@@ -3873,8 +3860,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);
@@ -3904,8 +3890,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);
@@ -3951,8 +3936,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;
@@ -4069,8 +4053,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;
@@ -4120,8 +4103,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);
 
@@ -4146,8 +4128,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;
@@ -4559,7 +4540,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:
@@ -4609,8 +4590,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);
@@ -4869,8 +4849,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;