linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Amir Goldstein <amir73il@gmail.com>
To: Miklos Szeredi <miklos@szeredi.hu>
Cc: Bernd Schubert <bernd.schubert@fastmail.fm>,
	linux-fsdevel@vger.kernel.org
Subject: [PATCH v15 8/9] fuse: implement passthrough for mmap
Date: Tue,  6 Feb 2024 16:24:52 +0200	[thread overview]
Message-ID: <20240206142453.1906268-9-amir73il@gmail.com> (raw)
In-Reply-To: <20240206142453.1906268-1-amir73il@gmail.com>

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 --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))
-- 
2.34.1


  parent reply	other threads:[~2024-02-06 14:25 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-06 14:24 [PATCH v15 0/9] FUSE passthrough for file io Amir Goldstein
2024-02-06 14:24 ` [PATCH v15 1/9] fuse: factor out helper for FUSE_DEV_IOC_CLONE Amir Goldstein
2024-02-06 14:24 ` [PATCH v15 2/9] fuse: introduce FUSE_PASSTHROUGH capability Amir Goldstein
2024-02-06 14:24 ` [PATCH v15 3/9] fuse: implement ioctls to manage backing files Amir Goldstein
2024-02-28 10:50   ` Jingbo Xu
2024-02-28 11:07     ` Amir Goldstein
2024-02-28 11:14       ` Miklos Szeredi
2024-02-28 11:28         ` Amir Goldstein
2024-02-28 14:32           ` Jens Axboe
2024-02-28 15:01             ` Miklos Szeredi
2024-02-28 15:05               ` Jens Axboe
2024-02-28 16:21               ` Amir Goldstein
2024-02-29 10:15               ` Christian Brauner
2024-02-29 10:17                 ` Christian Brauner
2024-03-05 10:57                   ` Miklos Szeredi
2024-02-28 13:22         ` Bernd Schubert
2024-02-06 14:24 ` [PATCH v15 4/9] fuse: prepare for opening file in passthrough mode Amir Goldstein
2024-02-06 14:24 ` [PATCH v15 5/9] fuse: implement open " Amir Goldstein
2024-02-06 14:24 ` [PATCH v15 6/9] fuse: implement read/write passthrough Amir Goldstein
2024-02-06 14:24 ` [PATCH v15 7/9] fuse: implement splice " Amir Goldstein
2024-02-06 14:24 ` Amir Goldstein [this message]
2024-02-06 14:24 ` [PATCH v15 9/9] fuse: auto-invalidate inode attributes in passthrough mode Amir Goldstein
2024-04-02 20:13   ` Sweet Tea Dorminy
2024-04-02 21:18     ` Bernd Schubert
2024-04-03  8:18       ` Amir Goldstein
2024-04-04 14:07         ` Sweet Tea Dorminy
2024-05-09 14:32           ` Amir Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240206142453.1906268-9-amir73il@gmail.com \
    --to=amir73il@gmail.com \
    --cc=bernd.schubert@fastmail.fm \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).