diff mbox series

[v15,8/9] fuse: implement passthrough for mmap

Message ID 20240206142453.1906268-9-amir73il@gmail.com (mailing list archive)
State New, archived
Headers show
Series FUSE passthrough for file io | expand

Commit Message

Amir Goldstein Feb. 6, 2024, 2:24 p.m. UTC
An mmap request for a file open in passthrough mode, maps the memory
directly to the backing file.

An mmap of a file in direct io mode, usually uses cached mmap and puts
the inode in caching io mode, which denies new passthrough opens of that
inode, because caching io mode is conflicting with passthrough io mode.

For the same reason, trying to mmap a direct io file, while there is
a passthrough file open on the same inode will fail with -ENODEV.

An mmap of a file in direct io mode, also needs to wait for parallel
dio writes in-progress to complete.

If a passthrough file is opened, while an mmap of another direct io
file is waiting for parallel dio writes to complete, the wait is aborted
and mmap fails with -ENODEV.

A FUSE server that uses passthrough and direct io opens on the same inode
that may also be mmaped, is advised to provide a backing fd also for the
files that are open in direct io mode (i.e. use the flags combination
FOPEN_DIRECT_IO | FOPEN_PASSTHROUGH), so that mmap will always use the
backing file, even if read/write do not passthrough.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
 fs/fuse/file.c        | 13 ++++++++++---
 fs/fuse/fuse_i.h      |  1 +
 fs/fuse/iomode.c      |  9 ++++++++-
 fs/fuse/passthrough.c | 16 ++++++++++++++++
 4 files changed, 35 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 80f98affbe7d..edbcb9ceb7e7 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -2550,14 +2550,21 @@  static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	struct fuse_file *ff = file->private_data;
 	struct fuse_conn *fc = ff->fm->fc;
+	struct inode *inode = file_inode(file);
 	int rc;
 
 	/* DAX mmap is superior to direct_io mmap */
-	if (FUSE_IS_DAX(file_inode(file)))
+	if (FUSE_IS_DAX(inode))
 		return fuse_dax_mmap(file, vma);
 
-	/* TODO: implement mmap to backing file */
+	/*
+	 * If inode is in passthrough io mode, because it has some file open
+	 * in passthrough mode, either mmap to backing file or fail mmap,
+	 * because mixing cached mmap and passthrough io mode is not allowed.
+	 */
 	if (fuse_file_passthrough(ff))
+		return fuse_passthrough_mmap(file, vma);
+	else if (fuse_inode_backing(get_fuse_inode(inode)))
 		return -ENODEV;
 
 	/*
@@ -2579,7 +2586,7 @@  static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
 		 * Also waits for parallel dio writers to go into serial mode
 		 * (exclusive instead of shared lock).
 		 */
-		rc = fuse_file_io_mmap(ff, file_inode(file));
+		rc = fuse_file_io_mmap(ff, inode);
 		if (rc)
 			return rc;
 
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index c495eaa11b49..98f878a52af1 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -1479,5 +1479,6 @@  ssize_t fuse_passthrough_splice_read(struct file *in, loff_t *ppos,
 ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe,
 				      struct file *out, loff_t *ppos,
 				      size_t len, unsigned int flags);
+ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
 
 #endif /* _FS_FUSE_I_H */
diff --git a/fs/fuse/iomode.c b/fs/fuse/iomode.c
index dce369f3b201..c545058a01e1 100644
--- a/fs/fuse/iomode.c
+++ b/fs/fuse/iomode.c
@@ -17,7 +17,7 @@ 
  */
 static inline bool fuse_is_io_cache_wait(struct fuse_inode *fi)
 {
-	return READ_ONCE(fi->iocachectr) < 0;
+	return READ_ONCE(fi->iocachectr) < 0 && !fuse_inode_backing(fi);
 }
 
 /*
@@ -42,6 +42,13 @@  static int fuse_inode_get_io_cache(struct fuse_inode *fi)
 					  !fuse_is_io_cache_wait(fi));
 		spin_lock(&fi->lock);
 	}
+	/*
+	 * Check if inode entered passthrough io mode while waiting for parallel
+	 * dio write completion.
+	 */
+	if (fuse_inode_backing(fi))
+		err = -ETXTBSY;
+
 	/*
 	 * Enter caching mode or clear the FUSE_I_CACHE_IO_MODE bit if we
 	 * failed to enter caching mode and no other caching open exists.
diff --git a/fs/fuse/passthrough.c b/fs/fuse/passthrough.c
index df8343cebd0a..260e76fc72d5 100644
--- a/fs/fuse/passthrough.c
+++ b/fs/fuse/passthrough.c
@@ -141,6 +141,22 @@  ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe,
 	return ret;
 }
 
+ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	struct fuse_file *ff = file->private_data;
+	struct file *backing_file = fuse_file_passthrough(ff);
+	struct backing_file_ctx ctx = {
+		.cred = ff->cred,
+		.user_file = file,
+		.accessed = fuse_file_accessed,
+	};
+
+	pr_debug("%s: backing_file=0x%p, start=%lu, end=%lu\n", __func__,
+		 backing_file, vma->vm_start, vma->vm_end);
+
+	return backing_file_mmap(backing_file, vma, &ctx);
+}
+
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 {
 	if (fb && refcount_inc_not_zero(&fb->count))