diff --git a/fs/fuse/backing.c b/fs/fuse/backing.c index 42ae8188028c..931c3397133c 100644 --- a/fs/fuse/backing.c +++ b/fs/fuse/backing.c @@ -966,6 +966,20 @@ void *fuse_file_write_iter_finalize(struct fuse_bpf_args *fa, return ERR_PTR(fwio->ret); } +int fuse_file_flock_backing(struct file *file, int cmd, struct file_lock *fl) +{ + struct fuse_file *ff = file->private_data; + struct file *backing_file = ff->backing_file; + int error; + + fl->fl_file = backing_file; + if (backing_file->f_op->flock) + error = backing_file->f_op->flock(backing_file, cmd, fl); + else + error = locks_lock_file_wait(backing_file, fl); + return error; +} + ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma) { int ret; diff --git a/fs/fuse/file.c b/fs/fuse/file.c index 923a0fbc698f..6608de8bea12 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -2695,12 +2695,18 @@ static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) { struct inode *inode = file_inode(file); struct fuse_conn *fc = get_fuse_conn(inode); + struct fuse_file *ff = file->private_data; int err; +#ifdef CONFIG_FUSE_BPF + /* TODO - this is simply passthrough, not a proper BPF filter */ + if (ff->backing_file) + return fuse_file_flock_backing(file, cmd, fl); +#endif + if (fc->no_flock) { err = locks_lock_file_wait(file, fl); } else { - struct fuse_file *ff = file->private_data; /* emulate flock with POSIX locks */ ff->flock = true; diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index af20800db6c2..475442c9ad7e 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -1664,6 +1664,7 @@ int fuse_file_write_iter_backing(struct fuse_bpf_args *fa, void *fuse_file_write_iter_finalize(struct fuse_bpf_args *fa, struct kiocb *iocb, struct iov_iter *from); +int fuse_file_flock_backing(struct file *file, int cmd, struct file_lock *fl); ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma); int fuse_file_fallocate_initialize(struct fuse_bpf_args *fa, diff --git a/tools/testing/selftests/filesystems/fuse/fuse_test.c b/tools/testing/selftests/filesystems/fuse/fuse_test.c index a52a9db1014c..bdb70e23b349 100644 --- a/tools/testing/selftests/filesystems/fuse/fuse_test.c +++ b/tools/testing/selftests/filesystems/fuse/fuse_test.c @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -1336,6 +1337,50 @@ out: return result; } +static int flock_test(const char *mount_dir) +{ + const char *file = "file"; + int result = TEST_FAILURE; + int src_fd = -1; + int fuse_dev = -1; + int fd = -1, fd2 = -1; + int backing_fd = -1; + char *addr = NULL; + + TEST(src_fd = open(ft_src, O_DIRECTORY | O_RDONLY | O_CLOEXEC), + src_fd != -1); + TESTEQUAL(mount_fuse(mount_dir, -1, src_fd, &fuse_dev), 0); + TEST(fd = s_open(s_path(s(mount_dir), s(file)), + O_CREAT | O_RDWR | O_CLOEXEC, 0777), + fd != -1); + TEST(fd2 = s_open(s_path(s(mount_dir), s(file)), + O_RDWR | O_CLOEXEC, 0777), + fd2 != -1); + TESTSYSCALL(flock(fd, LOCK_EX | LOCK_NB)); + TESTCONDERR((flock(fd2, LOCK_EX | LOCK_NB)) == -1); + TESTCOND(errno == EAGAIN); + TESTSYSCALL(flock(fd, LOCK_UN)); + TESTSYSCALL(flock(fd2, LOCK_EX | LOCK_NB)); + TEST(backing_fd = s_open(s_path(s(ft_src), s(file)), + O_RDONLY | O_CLOEXEC), + backing_fd != -1); + TESTCONDERR((flock(backing_fd, LOCK_EX | LOCK_NB)) == -1); + TESTCOND(errno == EAGAIN); + close(fd2); + fd2 = 0; + TESTSYSCALL(flock(backing_fd, LOCK_EX | LOCK_NB)); + + result = TEST_SUCCESS; +out: + close(fd); + close(fd2); + close(backing_fd); + umount(mount_dir); + close(fuse_dev); + close(src_fd); + return result; +} + static int readdir_perms_test(const char *mount_dir) { int result = TEST_FAILURE; @@ -2091,6 +2136,7 @@ int main(int argc, char *argv[]) MAKE_TEST(bpf_test_no_readdirplus_without_nodeid), MAKE_TEST(bpf_test_revalidate_handle_backing_fd), MAKE_TEST(bpf_test_lookup_postfilter), + MAKE_TEST(flock_test), }; #undef MAKE_TEST