]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
fuse: implement read/write passthrough
authorAmir Goldstein <amir73il@gmail.com>
Wed, 22 Nov 2023 16:26:04 +0000 (18:26 +0200)
committerMiklos Szeredi <mszeredi@redhat.com>
Tue, 5 Mar 2024 12:40:42 +0000 (13:40 +0100)
Use the backing file read/write helpers to implement read/write
passthrough to a backing file.

After read/write, we invalidate a/c/mtime/size attributes.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
fs/fuse/file.c
fs/fuse/fuse_i.h
fs/fuse/passthrough.c

index 6fad381f3beb510316b981a6a9a62cbcf3243413..903bb71aac6dc38259f6bbcfeec26b423011fe5c 100644 (file)
@@ -1693,10 +1693,13 @@ static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
        if (FUSE_IS_DAX(inode))
                return fuse_dax_read_iter(iocb, to);
 
-       if (!(ff->open_flags & FOPEN_DIRECT_IO))
-               return fuse_cache_read_iter(iocb, to);
-       else
+       /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
+       if (ff->open_flags & FOPEN_DIRECT_IO)
                return fuse_direct_read_iter(iocb, to);
+       else if (fuse_file_passthrough(ff))
+               return fuse_passthrough_read_iter(iocb, to);
+       else
+               return fuse_cache_read_iter(iocb, to);
 }
 
 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
@@ -1711,10 +1714,13 @@ static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
        if (FUSE_IS_DAX(inode))
                return fuse_dax_write_iter(iocb, from);
 
-       if (!(ff->open_flags & FOPEN_DIRECT_IO))
-               return fuse_cache_write_iter(iocb, from);
-       else
+       /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
+       if (ff->open_flags & FOPEN_DIRECT_IO)
                return fuse_direct_write_iter(iocb, from);
+       else if (fuse_file_passthrough(ff))
+               return fuse_passthrough_write_iter(iocb, from);
+       else
+               return fuse_cache_write_iter(iocb, from);
 }
 
 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
index 8fbf30fe3c3d1c2ce043912380467ed3d60a29fc..6cc7d0752f9fd7bef70e73a7b96de45a90c1ee2c 100644 (file)
@@ -1466,4 +1466,7 @@ static inline struct file *fuse_file_passthrough(struct fuse_file *ff)
 #endif
 }
 
+ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *iter);
+ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, struct iov_iter *iter);
+
 #endif /* _FS_FUSE_I_H */
index dc054d2ab13e62d4732a01ac44e170b86c1ae090..0e5d316bdad35fe8765f242571c9616e084d57b0 100644 (file)
 #include <linux/file.h>
 #include <linux/backing-file.h>
 
+static void fuse_file_accessed(struct file *file)
+{
+       struct inode *inode = file_inode(file);
+
+       fuse_invalidate_atime(inode);
+}
+
+static void fuse_file_modified(struct file *file)
+{
+       struct inode *inode = file_inode(file);
+
+       fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
+}
+
+ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *iter)
+{
+       struct file *file = iocb->ki_filp;
+       struct fuse_file *ff = file->private_data;
+       struct file *backing_file = fuse_file_passthrough(ff);
+       size_t count = iov_iter_count(iter);
+       ssize_t ret;
+       struct backing_file_ctx ctx = {
+               .cred = ff->cred,
+               .user_file = file,
+               .accessed = fuse_file_accessed,
+       };
+
+
+       pr_debug("%s: backing_file=0x%p, pos=%lld, len=%zu\n", __func__,
+                backing_file, iocb->ki_pos, count);
+
+       if (!count)
+               return 0;
+
+       ret = backing_file_read_iter(backing_file, iter, iocb, iocb->ki_flags,
+                                    &ctx);
+
+       return ret;
+}
+
+ssize_t fuse_passthrough_write_iter(struct kiocb *iocb,
+                                   struct iov_iter *iter)
+{
+       struct file *file = iocb->ki_filp;
+       struct inode *inode = file_inode(file);
+       struct fuse_file *ff = file->private_data;
+       struct file *backing_file = fuse_file_passthrough(ff);
+       size_t count = iov_iter_count(iter);
+       ssize_t ret;
+       struct backing_file_ctx ctx = {
+               .cred = ff->cred,
+               .user_file = file,
+               .end_write = fuse_file_modified,
+       };
+
+       pr_debug("%s: backing_file=0x%p, pos=%lld, len=%zu\n", __func__,
+                backing_file, iocb->ki_pos, count);
+
+       if (!count)
+               return 0;
+
+       inode_lock(inode);
+       ret = backing_file_write_iter(backing_file, iter, iocb, iocb->ki_flags,
+                                     &ctx);
+       inode_unlock(inode);
+
+       return ret;
+}
+
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 {
        if (fb && refcount_inc_not_zero(&fb->count))