Skip to contentSkip to conversation

fuse: use fs-iomap for better performance so we can containerize ext4

RFC Superseded Darrick J. Wong 7 participants 212 messages Aug 21, 2025 – Sep 12, 2025 Page 1 of 2

Subsystems (8)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

Hi everyone,

Do not merge this, still!!

This is the fourth request for comments of a prototype to connect the Linux fuse driver to fs-iomap for regular file IO operations to and from files whose contents persist to locally attached storage devices.

Why would you want to do that? Most filesystem drivers are seriously vulnerable to metadata parsing attacks, as syzbot has shown repeatedly over almost a decade of its existence. Faulty code can lead to total kernel compromise, and I think there's a very strong incentive to move all that parsing out to userspace where we can containerize the fuse server process.

willy's folios conversion project (and to a certain degree RH's new mount API) have also demonstrated that treewide changes to the core mm/pagecache/fs code are very very difficult to pull off and take years because you have to understand every filesystem's bespoke use of that core code. Eeeugh.

The fuse command plumbing is very simple -- the ->iomap_begin, ->iomap_end, and iomap ->ioend calls within iomap are turned into upcalls to the fuse server via a trio of new fuse commands. Pagecache writeback is now a directio write. The fuse server is now able to upsert mappings into the kernel for cached access (== zero upcalls for rereads and pure overwrites!) and the iomap cache revalidation code works.

With this RFC, I am able to show that it's possible to build a fuse server for a real filesystem (ext4) that runs entirely in userspace yet maintains most of its performance. At this stage I still get about 95% of the kernel ext4 driver's streaming directio performance on streaming IO, and 110% of its streaming buffered IO performance. Random buffered IO is about 85% as fast as the kernel. Random direct IO is about 80% as fast as the kernel; see the cover letter for the fuse2fs iomap changes for more details. Unwritten extent conversions on random direct writes are especially painful for fuse+iomap (~90% more overhead) due to upcall overhead. And that's with (now dynamic) debugging turned on!

These items have been addressed since the third RFC:

1. fuse2fs has been forked into fuse4fs, which now talks to the low level fuse interface. This avoids all the path walking that the high level fuse library provides, which dramatically improves the performance of fuse4fs. fstests runs in half the time now. Many thanks to Amir Goldstein for giving me a rough draft of the conversion!

2. I simplified the configuration protocols -- now there's a per-fs bit to enable any iomap, and a per-inode bit to enable iomap on a specific file. Registration of iomap devices now uses the backing fd registration interface.

3. You can now specify the root nodeid for any fuse mount.

4. Atomic writes are working, at least for single fsblocks.

5. I've ported the cache implementation from xfsprogs to e2fsprogs libsupport, so the inode and buffer caches can now dynamically grow to support larger working sets. No more fixed-size caches!

6. Cleaned up the kernel/libfuse ABI quite a bit.

7. fstests passes 97% of the tests that run, when iomap is enabled! Only 93% pass when iomap is disabled, and I think that's due to some bugs in the ACL and mode handling code.

There are some major warts remaining:

a. I've a /much/ clearer picture of how one might containerize a
filesystem server, thanks to a lot of input from Christian Brauner in response to v3. I think I have enough pieces to try setting up a fd-passing interface into a systemd service ... but I haven't actually written any of it yet.

b. fsdax isn't implemented. I think I'm going to work on this for
RFC v5 to see if we can simplify the file mapping handling in famfs. If not, then everyone else gets fsdax for free.

c. ext4 doesn't support out of place writes so I don't know if that
actually works correctly.

d. I've not yet consolidated struct fuse_inode, so the iomap gunk still
eats rather a lot of space per inode.

e. fuse2fs doesn't support the ext4 journal. Urk.

f. There's a VERY large quantity of fuse2fs improvements that need to be
applied before we get to the fuse-iomap parts. I'm not sending these (or the fstests changes) to keep the size of the patchbomb at "unreasonably large". :P

I'll work on these in August/Steptember, but for now here's an unmergeable RFC to start some discussion.

--Darrick

#1 lore raw

RFC PATCH v4 1/4 fuse: general bug fixes

Hi all,

Here's a collection of fixes that I *think* are bugs in fuse, along with some scattered improvements.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-fixes
---
Commits in this patchset:
* fuse: fix livelock in synchronous file put from fuseblk workers
* fuse: flush pending fuse events before aborting the connection
* fuse: capture the unique id of fuse commands being sent
* fuse: implement file attributes mask for statx
* fuse: update file mode when updating acls
* fuse: propagate default and file acls on creation
* fuse: enable FUSE_SYNCFS for all servers

---
 fs/fuse/fuse_i.h    |   14 +++++++
 fs/fuse/acl.c       |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dev.c       |   44 +++++++++++++++++++++++
 fs/fuse/dev_uring.c |    8 ++++
 fs/fuse/dir.c       |   96 +++++++++++++++++++++++++++++++++++++++------------
 fs/fuse/file.c      |   10 +++++
 fs/fuse/inode.c     |    5 +++
 7 files changed, 245 insertions(+), 27 deletions(-)
#2 lore raw

PATCH 1/7 fuse: fix livelock in synchronous file put from fuseblk workers

From: Darrick J. Wong <[email protected]>

I observed a hang when running generic/323 against a fuseblk server. This test opens a file, initiates a lot of AIO writes to that file descriptor, and closes the file descriptor before the writes complete. Unsurprisingly, the AIO exerciser threads are mostly stuck waiting for responses from the fuseblk server:

# cat /proc/372265/task/372313/stack
[<0>] request_wait_answer+0x1fe/0x2a0 [fuse]
[<0>] __fuse_simple_request+0xd3/0x2b0 [fuse]
[<0>] fuse_do_getattr+0xfc/0x1f0 [fuse]
[<0>] fuse_file_read_iter+0xbe/0x1c0 [fuse]
[<0>] aio_read+0x130/0x1e0
[<0>] io_submit_one+0x542/0x860
[<0>] __x64_sys_io_submit+0x98/0x1a0
[<0>] do_syscall_64+0x37/0xf0
[<0>] entry_SYSCALL_64_after_hwframe+0x4b/0x53

But the /weird/ part is that the fuseblk server threads are waiting for responses from itself:

# cat /proc/372210/task/372232/stack
[<0>] request_wait_answer+0x1fe/0x2a0 [fuse]
[<0>] __fuse_simple_request+0xd3/0x2b0 [fuse]
[<0>] fuse_file_put+0x9a/0xd0 [fuse]
[<0>] fuse_release+0x36/0x50 [fuse]
[<0>] __fput+0xec/0x2b0
[<0>] task_work_run+0x55/0x90
[<0>] syscall_exit_to_user_mode+0xe9/0x100
[<0>] do_syscall_64+0x43/0xf0
[<0>] entry_SYSCALL_64_after_hwframe+0x4b/0x53

The fuseblk server is fuse2fs so there's nothing all that exciting in the server itself. So why is the fuse server calling fuse_file_put? The commit message for the fstest sheds some light on that:

"By closing the file descriptor before calling io_destroy, you pretty much guarantee that the last put on the ioctx will be done in interrupt context (during I/O completion).

Aha. AIO fgets a new struct file from the fd when it queues the ioctx. The completion of the FUSE_WRITE command from userspace causes the fuse server to call the AIO completion function. The completion puts the struct file, queuing a delayed fput to the fuse server task. When the fuse server task returns to userspace, it has to run the delayed fput, which in the case of a fuseblk server, it does synchronously.

Sending the FUSE_RELEASE command sychronously from fuse server threads is a bad idea because a client program can initiate enough simultaneous AIOs such that all the fuse server threads end up in delayed_fput, and now there aren't any threads left to handle the queued fuse commands.

Fix this by only using synchronous fputs for fuseblk servers if the process doesn't have PF_LOCAL_THROTTLE. Hopefully the fuseblk server had the good sense to call PR_SET_IO_FLUSHER to mark itself as a filesystem server.

Cc <[email protected]> # v2.6.38
Fixes 5a18ec176c934c ("fuse: fix hang of single threaded fuseblk filesystem")
---
 fs/fuse/file.c |   10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/fuse/file.c +9 -1
index 5525a4520b0f89..0ba2b62e06679e 100644
@@ -356,8 +356,16 @@ void fuse_file_release(struct inode *inode, struct fuse_file *ff,
 	 * Make the release synchronous if this is a fuseblk mount,
 	 * synchronous RELEASE is allowed (and desirable) in this case
 	 * because the server can be trusted not to screw up.
+	 *
+	 * If we're a LOCAL_THROTTLE thread, use the asynchronous put
+	 * because the current thread might be a fuse server.  This can
+	 * happen if a process starts some aio and closes the fd before
+	 * the aio completes.  Since aio takes its own ref to the file,
+	 * the IO completion has to drop the ref, which is how the fuse
+	 * server can end up closing its own clients' files.
 	 */
-	fuse_file_put(ff, ff->fm->fc->destroy);
+	fuse_file_put(ff, ff->fm->fc->destroy &&
+			  (current->flags & PF_LOCAL_THROTTLE) == 0);
 }
 
 void fuse_release_common(struct file *file, bool isdir)
maintainer
#3 lore raw

Re: [PATCH 1/7] fuse: fix livelock in synchronous file put from fuseblk workers

Quoted 4 lines · Darrick J. Wong

Fix this by only using synchronous fputs for fuseblk servers if the process doesn't have PF_LOCAL_THROTTLE. Hopefully the fuseblk server had the good sense to call PR_SET_IO_FLUSHER to mark itself as a filesystem server.

I'm still not convinced. This patch adds complexity and depends on
the server doing some magic, which makes it unreliable.

Doing it async unconditionally removes complexity and fixes the issue reliably.

Thanks,
Miklos

#4 lore raw

Re: [PATCH 1/7] fuse: fix livelock in synchronous file put from fuseblk workers

Quoted 11 lines · Miklos Szeredi
Quoted 4 lines · Darrick J. Wong

Fix this by only using synchronous fputs for fuseblk servers if the process doesn't have PF_LOCAL_THROTTLE. Hopefully the fuseblk server had the good sense to call PR_SET_IO_FLUSHER to mark itself as a filesystem server.

I'm still not convinced. This patch adds complexity and depends on
the server doing some magic, which makes it unreliable.

Doing it async unconditionally removes complexity and fixes the issue reliably.

Works for me, I'll make it use async mode unconditionally.

--D

Quoted 2 lines

Thanks,
Miklos

#5 lore raw

PATCH 2/7 fuse: flush pending fuse events before aborting the connection

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

generic/488 fails with fuse2fs in the following fashion:

generic/488 _check_generic_filesystem: filesystem on /dev/sdf is inconsistent
(see /var/tmp/fstests/generic/488.full for details)

This test opens a large number of files, unlinks them (which really just renames them to fuse hidden files), closes the program, unmounts the filesystem, and runs fsck to check that there aren't any inconsistencies in the filesystem.

Unfortunately, the 488.full file shows that there are a lot of hidden files left over in the filesystem, with incorrect link counts. Tracing fuse_request_* shows that there are a large number of FUSE_RELEASE commands that are queued up on behalf of the unlinked files at the time that fuse_conn_destroy calls fuse_abort_conn. Had the connection not aborted, the fuse server would have responded to the RELEASE commands by removing the hidden files; instead they stick around.

Create a function to push all the background requests to the queue and then wait for the number of pending events to hit zero, and call this before fuse_abort_conn. That way, all the pending events are processed by the fuse server and we don't end up with a corrupt filesystem.

---
 fs/fuse/fuse_i.h |    6 ++++++
 fs/fuse/dev.c    |   38 ++++++++++++++++++++++++++++++++++++++
 fs/fuse/inode.c  |    1 +
 3 files changed, 45 insertions(+)
fs/fuse/fuse_i.h +6 -0
index ec248d13c8bfd9..2b5d56e3cb4eaf 100644
@@ -1249,6 +1249,12 @@ void fuse_request_end(struct fuse_req *req);
 void fuse_abort_conn(struct fuse_conn *fc);
 void fuse_wait_aborted(struct fuse_conn *fc);
 
+/**
+ * Flush all pending requests and wait for them.  Takes an optional timeout
+ * in jiffies.
+ */
+void fuse_flush_requests_and_wait(struct fuse_conn *fc, unsigned long timeout);
+
 /* Check if any requests timed out */
 void fuse_check_timeout(struct work_struct *work);
 
fs/fuse/dev.c +38 -0
index e80cd8f2c049f9..6f2b277973ca7d 100644
@@ -24,6 +24,7 @@
 #include <linux/splice.h>
 #include <linux/sched.h>
 #include <linux/seq_file.h>
+#include <linux/nmi.h>
 
 #define CREATE_TRACE_POINTS
 #include "fuse_trace.h"
@@ -2385,6 +2386,43 @@ static void end_polls(struct fuse_conn *fc)
 	}
 }
 
+/*
+ * Flush all pending requests and wait for them.  Only call this function when
+ * it is no longer possible for other threads to add requests.
+ */
+void fuse_flush_requests_and_wait(struct fuse_conn *fc, unsigned long timeout)
+{
+	unsigned long deadline;
+
+	spin_lock(&fc->lock);
+	if (!fc->connected) {
+		spin_unlock(&fc->lock);
+		return;
+	}
+
+	/* Push all the background requests to the queue. */
+	spin_lock(&fc->bg_lock);
+	fc->blocked = 0;
+	fc->max_background = UINT_MAX;
+	flush_bg_queue(fc);
+	spin_unlock(&fc->bg_lock);
+	spin_unlock(&fc->lock);
+
+	/*
+	 * Wait 30s for all the events to complete or abort.  Touch the
+	 * watchdog once per second so that we don't trip the hangcheck timer
+	 * while waiting for the fuse server.
+	 */
+	deadline = jiffies + timeout;
+	smp_mb();
+	while (fc->connected &&
+	       (!timeout || time_before(jiffies, deadline)) &&
+	       wait_event_timeout(fc->blocked_waitq,
+			!fc->connected || atomic_read(&fc->num_waiting) == 0,
+			HZ) == 0)
+		touch_softlockup_watchdog();
+}
+
 /*
  * Abort all requests.
  *
fs/fuse/inode.c +1 -0
index ecb869e895ab1d..b3b0c0f5598b4a 100644
@@ -2045,6 +2045,7 @@ void fuse_conn_destroy(struct fuse_mount *fm)
 {
 	struct fuse_conn *fc = fm->fc;
 
+	fuse_flush_requests_and_wait(fc, secs_to_jiffies(30));
 	if (fc->destroy)
 		fuse_send_destroy(fm);
 
maintainer
#6 lore raw

Re: [PATCH 2/7] fuse: flush pending fuse events before aborting the connection

Quoted 4 lines · Darrick J. Wong

Create a function to push all the background requests to the queue and then wait for the number of pending events to hit zero, and call this before fuse_abort_conn. That way, all the pending events are processed by the fuse server and we don't end up with a corrupt filesystem.

The flushing should be dependent on fc->destroy. Without that we really don't want server to block umount, not even for 30s.

I hate timeout based solutions, so my preference would be to remove the timeout completely. It wouldn't really make a difference anyway, since FUSE_DESTROY is sent synchronously without a timeout.

Thinking about blocking umount: if we did this in a private user/mount
ns, then it wouldn't be a problem. But how can we be sure? Is
checking sb->s_user_ns != &init_user_ns sufficient?

Thanks,
Miklos

#7 lore raw

Re: [PATCH 2/7] fuse: flush pending fuse events before aborting the connection

Quoted 9 lines · Miklos Szeredi
Quoted 4 lines · Darrick J. Wong

Create a function to push all the background requests to the queue and then wait for the number of pending events to hit zero, and call this before fuse_abort_conn. That way, all the pending events are processed by the fuse server and we don't end up with a corrupt filesystem.

The flushing should be dependent on fc->destroy. Without that we really don't want server to block umount, not even for 30s.

<nod> I once thought it was crucial to flush all the FUSE_RELEASE requests to the fuse server prior to the server's ->destroy method being called, but it turns out that's not true -- all the open unlinked files created by generic/488 actually do get cleaned up even in the !fuseblk case.

It's just libext2fs that's somewhat stupid and leaves dead dirents all over the root directory, which (mis)lead me into thinking that the unlinked files weren't being cleaned up correctly.

Quoted 3 lines

I hate timeout based solutions, so my preference would be to remove the timeout completely. It wouldn't really make a difference anyway, since FUSE_DESTROY is sent synchronously without a timeout.

Hrmm, the timeouts waiting for FUSE_RELEASE might not be so useful anyway. If someone configured a request_timeout then the requests will automatically cancel if the fuse server wedges itself.

OTOH I don't set any request_timeout in fuse[24]fs because they use FUSE_RELEASE to free an open-but-unlinked file, and that can take 45min if (say) you have a file with ten million extents to free as part of freeing the file.

I think the problem here is that there's no way for a fuse server to report back to the kernel that it's making progress on a very long running request; and that the kernel probably shouldn't trust that. In the default case there's no request timeout so the kernel will wait forever.

In any case, I think I agree that the time_after check isn't necessary. Either we trust the server to be making progress (and do not have a request timeout) or we notice the connection died and move on to aborting all the requests.

Quoted 3 lines

Thinking about blocking umount: if we did this in a private user/mount
ns, then it wouldn't be a problem. But how can we be sure? Is
checking sb->s_user_ns != &init_user_ns sufficient?

I'm not sure we can -- what if you mount a filesystem in a private mount ns, but then some supervisor process adds another mount to the same sb in the init_user_ns?

--D

Quoted 3 lines

Thanks,
Miklos

maintainer
#8 lore raw

Re: [PATCH 2/7] fuse: flush pending fuse events before aborting the connection

Quoted 10 lines · Darrick J. Wong
Quoted 3 lines · Miklos Szeredi

Thinking about blocking umount: if we did this in a private user/mount
ns, then it wouldn't be a problem. But how can we be sure? Is
checking sb->s_user_ns != &init_user_ns sufficient?

I'm not sure we can -- what if you mount a filesystem in a private mount ns, but then some supervisor process adds another mount to the same sb in the init_user_ns?

Right. What I don't see is whether this scenario happens in real life
or not. But I guess if something can be done it will be done, sooner
or later, so better not tempt fate.

Thanks,
Miklos

Quoted 6 lines

--D

Quoted 3 lines

Thanks,
Miklos

#9 lore raw

PATCH 3/7 fuse: capture the unique id of fuse commands being sent

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

The fuse_request_{send,end} tracepoints capture the value of req->in.h.unique in the trace output. It would be really nice if we could use this to match a request to its response for debugging and latency analysis, but the call to trace_fuse_request_send occurs before the unique id has been set:

fuse_request_send:    connection 8388608 req 0 opcode 1 (FUSE_LOOKUP) len 107
fuse_request_end:     connection 8388608 req 6 len 16 error -2

Move the callsites to trace_fuse_request_send to after the unique id has been set, or right before we decide to cancel a request having not set one.

---
 fs/fuse/dev.c       |    6 +++++-
 fs/fuse/dev_uring.c |    8 +++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
fs/fuse/dev.c +5 -1
index 6f2b277973ca7d..05d6e7779387a4 100644
@@ -376,10 +376,15 @@ static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)
 	if (fiq->connected) {
 		if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
 			req->in.h.unique = fuse_get_unique_locked(fiq);
+
+		/* tracepoint captures in.h.unique */
+		trace_fuse_request_send(req);
+
 		list_add_tail(&req->list, &fiq->pending);
 		fuse_dev_wake_and_unlock(fiq);
 	} else {
 		spin_unlock(&fiq->lock);
+		trace_fuse_request_send(req);
 		req->out.h.error = -ENOTCONN;
 		clear_bit(FR_PENDING, &req->flags);
 		fuse_request_end(req);
@@ -398,7 +403,6 @@ static void fuse_send_one(struct fuse_iqueue *fiq, struct fuse_req *req)
 	req->in.h.len = sizeof(struct fuse_in_header) +
 		fuse_len_args(req->args->in_numargs,
 			      (struct fuse_arg *) req->args->in_args);
-	trace_fuse_request_send(req);
 	fiq->ops->send_req(fiq, req);
 }
 
fs/fuse/dev_uring.c +7 -1
index 249b210becb1cc..14f263d4419392 100644
@@ -7,6 +7,7 @@
 #include "fuse_i.h"
 #include "dev_uring_i.h"
 #include "fuse_dev_i.h"
+#include "fuse_trace.h"
 
 #include <linux/fs.h>
 #include <linux/io_uring/cmd.h>
@@ -1265,12 +1266,17 @@ void fuse_uring_queue_fuse_req(struct fuse_iqueue *fiq, struct fuse_req *req)
 
 	err = -EINVAL;
 	queue = fuse_uring_task_to_queue(ring);
-	if (!queue)
+	if (!queue) {
+		trace_fuse_request_send(req);
 		goto err;
+	}
 
 	if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
 		req->in.h.unique = fuse_get_unique(fiq);
 
+	/* tracepoint captures in.h.unique */
+	trace_fuse_request_send(req);
+
 	spin_lock(&queue->lock);
 	err = -ENOTCONN;
 	if (unlikely(queue->stopped))
maintainer
#10 lore raw

Re: [PATCH 3/7] fuse: capture the unique id of fuse commands being sent

Quoted 12 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

The fuse_request_{send,end} tracepoints capture the value of req->in.h.unique in the trace output. It would be really nice if we could use this to match a request to its response for debugging and latency analysis, but the call to trace_fuse_request_send occurs before the unique id has been set:

fuse_request_send:    connection 8388608 req 0 opcode 1 (FUSE_LOOKUP) len 107
fuse_request_end:     connection 8388608 req 6 len 16 error -2
Quoted 8 lines

Move the callsites to trace_fuse_request_send to after the unique id has been set, or right before we decide to cancel a request having not set one.

---
 fs/fuse/dev.c       |    6 +++++-
 fs/fuse/dev_uring.c |    8 +++++++-

I think we'll also need to do the equivalent for virtio.

Quoted 10 lines
 2 files changed, 12 insertions(+), 2 deletions(-)
fs/fuse/dev.c +0 -0
index 6f2b277973ca7d..05d6e7779387a4 100644
@@ -376,10 +376,15 @@ static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)
        if (fiq->connected) {
                if (req->in.h.opcode != FUSE_NOTIFY_REPLY)

fs/fuse/dev.c static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req) · line 387

Quoted 10 lines
fs/fuse/dev.c +5 -0
                        req->in.h.unique = fuse_get_unique_locked(fiq);
+
+               /* tracepoint captures in.h.unique */
+               trace_fuse_request_send(req);
+
                list_add_tail(&req->list, &fiq->pending);
                fuse_dev_wake_and_unlock(fiq);
        } else {
                spin_unlock(&fiq->lock);
+               trace_fuse_request_send(req);

Should this request still show up in the trace even though the request doesn't actually get sent to the server? imo that makes it misleading/confusing unless the trace also indicates -ENOTCONN.

Quoted 24 lines
fs/fuse/dev.c +0 -1
                req->out.h.error = -ENOTCONN;
                clear_bit(FR_PENDING, &req->flags);
                fuse_request_end(req);
@@ -398,7 +403,6 @@ static void fuse_send_one(struct fuse_iqueue *fiq, struct fuse_req *req)
        req->in.h.len = sizeof(struct fuse_in_header) +
                fuse_len_args(req->args->in_numargs,
                              (struct fuse_arg *) req->args->in_args);
-       trace_fuse_request_send(req);
        fiq->ops->send_req(fiq, req);
 }
 
fs/fuse/dev_uring.c +1 -0
index 249b210becb1cc..14f263d4419392 100644
@@ -7,6 +7,7 @@
 #include "fuse_i.h"
 #include "dev_uring_i.h"
 #include "fuse_dev_i.h"
+#include "fuse_trace.h"
 
 #include <linux/fs.h>
 #include <linux/io_uring/cmd.h>

fs/fuse/dev_uring.c void fuse_uring_queue_fuse_req(struct fuse_iqueue *fiq, struct fuse_req *req) · line 1270

Quoted 6 lines
fs/fuse/dev_uring.c +2 -1
@@ -1265,12 +1266,17 @@ void fuse_uring_queue_fuse_req(struct fuse_iqueue *fiq, struct fuse_req *req)
 
        err = -EINVAL;
        queue = fuse_uring_task_to_queue(ring);
-       if (!queue)
+       if (!queue) {
+               trace_fuse_request_send(req);

Same question here.

Thanks,
Joanne

#11 lore raw

Re: [PATCH 3/7] fuse: capture the unique id of fuse commands being sent

Quoted 17 lines · Joanne Koong
Quoted 16 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

The fuse_request_{send,end} tracepoints capture the value of req->in.h.unique in the trace output. It would be really nice if we could use this to match a request to its response for debugging and latency analysis, but the call to trace_fuse_request_send occurs before the unique id has been set:

fuse_request_send:    connection 8388608 req 0 opcode 1 (FUSE_LOOKUP) len 107
fuse_request_end:     connection 8388608 req 6 len 16 error -2

Move the callsites to trace_fuse_request_send to after the unique id has been set, or right before we decide to cancel a request having not set one.

Quoted 6 lines
Quoted 4 lines
---
 fs/fuse/dev.c       |    6 +++++-
 fs/fuse/dev_uring.c |    8 +++++++-

I think we'll also need to do the equivalent for virtio.

Ackpth, virtio sends commands too??

Oh, yes, it does -- judging from the fuse_get_unique calls, at least virtio_fs_send_req and maybe virtio_fs_send_forget need to add a call to trace_fuse_request_send?

Quoted 16 lines
Quoted 16 lines
 2 files changed, 12 insertions(+), 2 deletions(-)
fs/fuse/dev.c +4 -0
index 6f2b277973ca7d..05d6e7779387a4 100644
@@ -376,10 +376,15 @@ static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)
        if (fiq->connected) {
                if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
                        req->in.h.unique = fuse_get_unique_locked(fiq);
+
+               /* tracepoint captures in.h.unique */
+               trace_fuse_request_send(req);
+
                list_add_tail(&req->list, &fiq->pending);
Quoted 8 lines

fs/fuse/dev.c static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req) · line 387

Quoted 4 lines
fs/fuse/dev.c +1 -0
                fuse_dev_wake_and_unlock(fiq);
        } else {
                spin_unlock(&fiq->lock);
+               trace_fuse_request_send(req);

Should this request still show up in the trace even though the request doesn't actually get sent to the server? imo that makes it misleading/confusing unless the trace also indicates -ENOTCONN.

Hrmm. I was thinking that it would be very nice to have fuse_request_{send,end} bracket the start and end of a fuse request, even if we kill it immediately.

OTOH from a tracing "efficiency" perspective it's probably ok for never-sent requests only to ever hit the fuse_request_end tracepoint since the id will not get reused for quite some time.

<shrug> Thoughts?

--D

Quoted 36 lines
Quoted 24 lines
fs/fuse/dev.c +0 -1
                req->out.h.error = -ENOTCONN;
                clear_bit(FR_PENDING, &req->flags);
                fuse_request_end(req);
@@ -398,7 +403,6 @@ static void fuse_send_one(struct fuse_iqueue *fiq, struct fuse_req *req)
        req->in.h.len = sizeof(struct fuse_in_header) +
                fuse_len_args(req->args->in_numargs,
                              (struct fuse_arg *) req->args->in_args);
-       trace_fuse_request_send(req);
        fiq->ops->send_req(fiq, req);
 }
 
fs/fuse/dev_uring.c +1 -0
index 249b210becb1cc..14f263d4419392 100644
@@ -7,6 +7,7 @@
 #include "fuse_i.h"
 #include "dev_uring_i.h"
 #include "fuse_dev_i.h"
+#include "fuse_trace.h"
 
 #include <linux/fs.h>
 #include <linux/io_uring/cmd.h>

fs/fuse/dev_uring.c void fuse_uring_queue_fuse_req(struct fuse_iqueue *fiq, struct fuse_req *req) · line 1270

Quoted 6 lines
fs/fuse/dev_uring.c +2 -1
@@ -1265,12 +1266,17 @@ void fuse_uring_queue_fuse_req(struct fuse_iqueue *fiq, struct fuse_req *req)
 
        err = -EINVAL;
        queue = fuse_uring_task_to_queue(ring);
-       if (!queue)
+       if (!queue) {
+               trace_fuse_request_send(req);

Same question here.

Thanks,
Joanne

maintainer
#12 lore raw

Re: [PATCH 3/7] fuse: capture the unique id of fuse commands being sent

Quoted 3 lines · Darrick J. Wong

Hrmm. I was thinking that it would be very nice to have fuse_request_{send,end} bracket the start and end of a fuse request, even if we kill it immediately.

I'm fine with that, and would possibly simplify some code that checks for an error and calls ->end manually. But that makes it a non-trivial change unfortunately.

Thanks,
Miklos

#13 lore raw

Re: [PATCH 3/7] fuse: capture the unique id of fuse commands being sent

Quoted 9 lines · Miklos Szeredi
Quoted 3 lines · Darrick J. Wong

Hrmm. I was thinking that it would be very nice to have fuse_request_{send,end} bracket the start and end of a fuse request, even if we kill it immediately.

I'm fine with that, and would possibly simplify some code that checks for an error and calls ->end manually. But that makes it a non-trivial change unfortunately.

Yes, and then you have to poke the idr structure for a request id even if that caller already knows that the connection's dead. That seems like a waste of cycles, but OTOH maybe we just don't care?

(Though I suppose seeing more than one request id of zero in the trace output implies very strongly that the connection is really dead)

--D

Quoted 3 lines

Thanks,
Miklos

#14 lore raw

Re: [PATCH 3/7] fuse: capture the unique id of fuse commands being sent

Quoted 11 lines · Darrick J. Wong
Quoted 9 lines · Miklos Szeredi
Quoted 3 lines · Darrick J. Wong

Hrmm. I was thinking that it would be very nice to have fuse_request_{send,end} bracket the start and end of a fuse request, even if we kill it immediately.

I'm fine with that, and would possibly simplify some code that checks for an error and calls ->end manually. But that makes it a non-trivial change unfortunately.

Quoted 6 lines

Yes, and then you have to poke the idr structure for a request id even if that caller already knows that the connection's dead. That seems like a waste of cycles, but OTOH maybe we just don't care?

(Though I suppose seeing more than one request id of zero in the trace output implies very strongly that the connection is really dead)

Well.... given the fuse_iqueue::reqctr usage, the first request gets a unique id of 2 and increments by two thereafter. So it's a pretty safe bet that unique==0 means the request isn't actually being sent, or that your very lucky in that your fuse server has been running for a /very/ long time.

I think I just won't call trace_fuse_request_send for requests that are immediately ended; and I'll refactor the req->in.h.unique assignment into a helper so that virtiofs and friends can call the helper and get the tracepoint automatically.

For example, fuse_dev_queue_req now becomes:

static inline void fuse_request_assign_unique_locked(struct fuse_iqueue *fiq,
struct fuse_req *req)

{
	if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
		req->in.h.unique = fuse_get_unique_locked(fiq);

	/* tracepoint captures in.h.unique and in.h.len */
	trace_fuse_request_send(req);
}

static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)

{
	spin_lock(&fiq->lock);
	if (fiq->connected) {
		fuse_request_assign_unique_locked(fiq, req);
		list_add_tail(&req->list, &fiq->pending);
		fuse_dev_wake_and_unlock(fiq);
	} else {
		spin_unlock(&fiq->lock);
		req->out.h.error = -ENOTCONN;
		clear_bit(FR_PENDING, &req->flags);
		fuse_request_end(req);
	}
}

--D

maintainer
#15 lore raw

Re: [PATCH 3/7] fuse: capture the unique id of fuse commands being sent

Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Darrick J. Wong
Quoted 2 lines · Miklos Szeredi

On Tue, 26 Aug 2025 at 20:52, Darrick J. Wong <[email protected]> wrote:

Sorry for the late reply on this.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Hrmm. I was thinking that it would be very nice to have fuse_request_{send,end} bracket the start and end of a fuse request, even if we kill it immediately.

Oh interesting, I didn't realize there was a trace_fuse_request_end(). I get now why you wanted the trace_fuse_request_send() for the !fiq->connected case, for symmetry. I was thinking of it from the client userspace side (one idea I have, which idk if it is actually that useful or not, is building some sort of observability "wireshark for fuse" tool that gives more visibility into the requests being sent to/from the server like their associated kernel vs libfuse timestamps to know where the latency is happening. this issue has come up in prod a few times when debugging slow requests); from this perspective, it seemed confusing to see requests show up that were never in good faith attempted to be sent to the server.

If you want to preserve the symmetry, maybe one idea is only doing the trace_fuse_request_end() if the req.in.h.unique code is valid? That would skip doing the trace for the !fiq->connected case.

Thanks,
Joanne

Quoted 52 lines
Quoted 11 lines
Quoted 4 lines

I'm fine with that, and would possibly simplify some code that checks for an error and calls ->end manually. But that makes it a non-trivial change unfortunately.

Yes, and then you have to poke the idr structure for a request id even if that caller already knows that the connection's dead. That seems like a waste of cycles, but OTOH maybe we just don't care?

(Though I suppose seeing more than one request id of zero in the trace output implies very strongly that the connection is really dead)

Well.... given the fuse_iqueue::reqctr usage, the first request gets a unique id of 2 and increments by two thereafter. So it's a pretty safe bet that unique==0 means the request isn't actually being sent, or that your very lucky in that your fuse server has been running for a /very/ long time.

I think I just won't call trace_fuse_request_send for requests that are immediately ended; and I'll refactor the req->in.h.unique assignment into a helper so that virtiofs and friends can call the helper and get the tracepoint automatically.

For example, fuse_dev_queue_req now becomes:

static inline void fuse_request_assign_unique_locked(struct fuse_iqueue *fiq,
struct fuse_req *req)

{
        if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
                req->in.h.unique = fuse_get_unique_locked(fiq);

        /* tracepoint captures in.h.unique and in.h.len */
        trace_fuse_request_send(req);
}

static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)

{
        spin_lock(&fiq->lock);
        if (fiq->connected) {
                fuse_request_assign_unique_locked(fiq, req);
                list_add_tail(&req->list, &fiq->pending);
                fuse_dev_wake_and_unlock(fiq);
        } else {
                spin_unlock(&fiq->lock);
                req->out.h.error = -ENOTCONN;
                clear_bit(FR_PENDING, &req->flags);
                fuse_request_end(req);
        }
}

--D

#16 lore raw

Re: [PATCH 3/7] fuse: capture the unique id of fuse commands being sent

Quoted 13 lines · Joanne Koong
Quoted 5 lines · Darrick J. Wong
Quoted 3 lines · Darrick J. Wong
Quoted 2 lines · Miklos Szeredi

On Tue, 26 Aug 2025 at 20:52, Darrick J. Wong <[email protected]> wrote:

Sorry for the late reply on this.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Hrmm. I was thinking that it would be very nice to have fuse_request_{send,end} bracket the start and end of a fuse request, even if we kill it immediately.

Quoted 11 lines

Oh interesting, I didn't realize there was a trace_fuse_request_end(). I get now why you wanted the trace_fuse_request_send() for the !fiq->connected case, for symmetry. I was thinking of it from the client userspace side (one idea I have, which idk if it is actually that useful or not, is building some sort of observability "wireshark for fuse" tool that gives more visibility into the requests being sent to/from the server like their associated kernel vs libfuse timestamps to know where the latency is happening. this issue has come up in prod a few times when debugging slow requests); from this perspective, it seemed confusing to see requests show up that were never in good faith attempted to be sent to the server.

Well at first I was all "Ehhh, why are all the request ids zero??".

Later when I started debugging corruption problems in fuse2fs I realized that it would be really helpful to be able to match the start and end of a particular fuse request to all the stuff that happened in between.

*So far* I can mostly just watch the raw ftrace feed to see what's going on without going crazy, but yes, a wireshark plugin would be nice. Or someone writing fuseslow. ;)

Quoted 3 lines

If you want to preserve the symmetry, maybe one idea is only doing the trace_fuse_request_end() if the req.in.h.unique code is valid? That would skip doing the trace for the !fiq->connected case.

That will actually cause some loss of trace data -- trace_fuse_request_end also captures the error code of the aborted commands, so you can tell from the -103 error code that something really bad happened.

--D

Quoted 55 lines

Thanks,
Joanne

Quoted 52 lines
Quoted 11 lines
Quoted 4 lines

I'm fine with that, and would possibly simplify some code that checks for an error and calls ->end manually. But that makes it a non-trivial change unfortunately.

Yes, and then you have to poke the idr structure for a request id even if that caller already knows that the connection's dead. That seems like a waste of cycles, but OTOH maybe we just don't care?

(Though I suppose seeing more than one request id of zero in the trace output implies very strongly that the connection is really dead)

Well.... given the fuse_iqueue::reqctr usage, the first request gets a unique id of 2 and increments by two thereafter. So it's a pretty safe bet that unique==0 means the request isn't actually being sent, or that your very lucky in that your fuse server has been running for a /very/ long time.

I think I just won't call trace_fuse_request_send for requests that are immediately ended; and I'll refactor the req->in.h.unique assignment into a helper so that virtiofs and friends can call the helper and get the tracepoint automatically.

For example, fuse_dev_queue_req now becomes:

static inline void fuse_request_assign_unique_locked(struct fuse_iqueue *fiq,
struct fuse_req *req)

{
        if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
                req->in.h.unique = fuse_get_unique_locked(fiq);

        /* tracepoint captures in.h.unique and in.h.len */
        trace_fuse_request_send(req);
}

static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)

{
        spin_lock(&fiq->lock);
        if (fiq->connected) {
                fuse_request_assign_unique_locked(fiq, req);
                list_add_tail(&req->list, &fiq->pending);
                fuse_dev_wake_and_unlock(fiq);
        } else {
                spin_unlock(&fiq->lock);
                req->out.h.error = -ENOTCONN;
                clear_bit(FR_PENDING, &req->flags);
                fuse_request_end(req);
        }
}

--D

maintainer
#17 lore raw

Re: [PATCH 3/7] fuse: capture the unique id of fuse commands being sent

In reply to Darrick J. Wong
Quoted 60 lines · Darrick J. Wong
Quoted 17 lines · Joanne Koong
Quoted 16 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

The fuse_request_{send,end} tracepoints capture the value of req->in.h.unique in the trace output. It would be really nice if we could use this to match a request to its response for debugging and latency analysis, but the call to trace_fuse_request_send occurs before the unique id has been set:

fuse_request_send:    connection 8388608 req 0 opcode 1 (FUSE_LOOKUP) len 107
fuse_request_end:     connection 8388608 req 6 len 16 error -2

Move the callsites to trace_fuse_request_send to after the unique id has been set, or right before we decide to cancel a request having not set one.

Quoted 6 lines
Quoted 4 lines
---
 fs/fuse/dev.c       |    6 +++++-
 fs/fuse/dev_uring.c |    8 +++++++-

I think we'll also need to do the equivalent for virtio.

Ackpth, virtio sends commands too??

Oh, yes, it does -- judging from the fuse_get_unique calls, at least virtio_fs_send_req and maybe virtio_fs_send_forget need to add a call to trace_fuse_request_send?

Quoted 16 lines
Quoted 16 lines
 2 files changed, 12 insertions(+), 2 deletions(-)
fs/fuse/dev.c +4 -0
index 6f2b277973ca7d..05d6e7779387a4 100644
@@ -376,10 +376,15 @@ static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)
        if (fiq->connected) {
                if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
                        req->in.h.unique = fuse_get_unique_locked(fiq);
+
+               /* tracepoint captures in.h.unique */
+               trace_fuse_request_send(req);
+
                list_add_tail(&req->list, &fiq->pending);
Quoted 8 lines

fs/fuse/dev.c static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req) · line 387

Quoted 4 lines
fs/fuse/dev.c +1 -0
                fuse_dev_wake_and_unlock(fiq);
        } else {
                spin_unlock(&fiq->lock);
+               trace_fuse_request_send(req);

Should this request still show up in the trace even though the request doesn't actually get sent to the server? imo that makes it misleading/confusing unless the trace also indicates -ENOTCONN.

Hrmm. I was thinking that it would be very nice to have fuse_request_{send,end} bracket the start and end of a fuse request, even if we kill it immediately.

Quoted 44 lines

OTOH from a tracing "efficiency" perspective it's probably ok for never-sent requests only to ever hit the fuse_request_end tracepoint since the id will not get reused for quite some time.

<shrug> Thoughts?

--D

Quoted 36 lines
Quoted 24 lines
fs/fuse/dev.c +0 -1
                req->out.h.error = -ENOTCONN;
                clear_bit(FR_PENDING, &req->flags);
                fuse_request_end(req);
@@ -398,7 +403,6 @@ static void fuse_send_one(struct fuse_iqueue *fiq, struct fuse_req *req)
        req->in.h.len = sizeof(struct fuse_in_header) +
                fuse_len_args(req->args->in_numargs,
                              (struct fuse_arg *) req->args->in_args);
-       trace_fuse_request_send(req);
        fiq->ops->send_req(fiq, req);
 }
 
fs/fuse/dev_uring.c +1 -0
index 249b210becb1cc..14f263d4419392 100644
@@ -7,6 +7,7 @@
 #include "fuse_i.h"
 #include "dev_uring_i.h"
 #include "fuse_dev_i.h"
+#include "fuse_trace.h"
 
 #include <linux/fs.h>
 #include <linux/io_uring/cmd.h>

fs/fuse/dev_uring.c void fuse_uring_queue_fuse_req(struct fuse_iqueue *fiq, struct fuse_req *req) · line 1270

Quoted 6 lines
fs/fuse/dev_uring.c +2 -1
@@ -1265,12 +1266,17 @@ void fuse_uring_queue_fuse_req(struct fuse_iqueue *fiq, struct fuse_req *req)
 
        err = -EINVAL;
        queue = fuse_uring_task_to_queue(ring);
-       if (!queue)
+       if (!queue) {
+               trace_fuse_request_send(req);

Same question here.

Thanks,
Joanne

I really need to find time to update my related branch - as I wrote before, I already have all of that, I think.

Thanks,
Bernd

#18 lore raw

PATCH 4/7 fuse: implement file attributes mask for statx

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Actually copy the attributes/attributes_mask from userspace.

---
 fs/fuse/fuse_i.h |    4 ++++
 fs/fuse/dir.c    |    4 ++++
 fs/fuse/inode.c  |    3 +++
 3 files changed, 11 insertions(+)
fs/fuse/fuse_i.h +4 -0
index 2b5d56e3cb4eaf..bb1fdae0bbc906 100644
@@ -140,6 +140,10 @@ struct fuse_inode {
 	/** Version of last attribute change */
 	u64 attr_version;
 
+	/** statx file attributes */
+	u64 statx_attributes;
+	u64 statx_attributes_mask;
+
 	union {
 		/* read/write io cache (regular file only) */
 		struct {
fs/fuse/dir.c +4 -0
index 2d817d7cab2649..2e4d1131ab8cbe 100644
@@ -1278,6 +1278,8 @@ static int fuse_do_statx(struct mnt_idmap *idmap, struct inode *inode,
 		stat->result_mask = sx->mask & (STATX_BASIC_STATS | STATX_BTIME);
 		stat->btime.tv_sec = sx->btime.tv_sec;
 		stat->btime.tv_nsec = min_t(u32, sx->btime.tv_nsec, NSEC_PER_SEC - 1);
+		stat->attributes = sx->attributes;
+		stat->attributes_mask = sx->attributes_mask;
 		fuse_fillattr(idmap, inode, &attr, stat);
 		stat->result_mask |= STATX_TYPE;
 	}
@@ -1381,6 +1383,8 @@ static int fuse_update_get_attr(struct mnt_idmap *idmap, struct inode *inode,
 			stat->btime = fi->i_btime;
 			stat->result_mask |= STATX_BTIME;
 		}
+		stat->attributes = fi->statx_attributes;
+		stat->attributes_mask = fi->statx_attributes_mask;
 	}
 
 	return err;
fs/fuse/inode.c +3 -0
index b3b0c0f5598b4a..463879830ecf34 100644
@@ -287,6 +287,9 @@ void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
 			fi->i_btime.tv_sec = sx->btime.tv_sec;
 			fi->i_btime.tv_nsec = sx->btime.tv_nsec;
 		}
+
+		fi->statx_attributes = sx->attributes;
+		fi->statx_attributes_mask = sx->attributes_mask;
 	}
 
 	if (attr->blksize != 0)
maintainer Reviewed-by on v1 4/7
#19 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 4 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Actually copy the attributes/attributes_mask from userspace.

This makes sense to me.

Quoted 8 lines
---
 fs/fuse/fuse_i.h |    4 ++++
 fs/fuse/dir.c    |    4 ++++
 fs/fuse/inode.c  |    3 +++
 3 files changed, 11 insertions(+)
#20 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 9 lines · Joanne Koong
Quoted 4 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Actually copy the attributes/attributes_mask from userspace.

This makes sense to me.

Thanks!

--d

Quoted 9 lines
Quoted 8 lines
---
 fs/fuse/fuse_i.h |    4 ++++
 fs/fuse/dir.c    |    4 ++++
 fs/fuse/inode.c  |    3 +++
 3 files changed, 11 insertions(+)
maintainer
#21 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

In reply to Darrick J. Wong
Quoted 4 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Actually copy the attributes/attributes_mask from userspace.

Some attributes should definitely not be copied (like MOUNT_ROOT, AUTOMOUNT). This should probably be VFS responsibility to prevent messing with these.

I guess the others are okay, they can already be queried through one of the fileattr intefaces. But think we should still have an explicit mask to prevent the server setting anything other than the currently defined attributes.

Thanks,
Miklos

#22 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 6 lines · Miklos Szeredi
Quoted 4 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Actually copy the attributes/attributes_mask from userspace.

Quoted 8 lines

Some attributes should definitely not be copied (like MOUNT_ROOT, AUTOMOUNT). This should probably be VFS responsibility to prevent messing with these.

I guess the others are okay, they can already be queried through one of the fileattr intefaces. But think we should still have an explicit mask to prevent the server setting anything other than the currently defined attributes.

Ok, will do. Thanks for the feedback!

Though unfortunately there isn't a pre-existing mask for "flags the vfs will set for you" other than grepping:

fs/stat.c:121: * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the

fs/stat.c:127:          stat->attributes |= STATX_ATTR_IMMUTABLE;
fs/stat.c:129:          stat->attributes |= STATX_ATTR_APPEND;
fs/stat.c:153:  stat->attributes_mask |= STATX_ATTR_WRITE_ATOMIC;
fs/stat.c:163:          stat->attributes |= STATX_ATTR_WRITE_ATOMIC;
fs/stat.c:201:          stat->attributes |= STATX_ATTR_AUTOMOUNT;
fs/stat.c:204:          stat->attributes |= STATX_ATTR_DAX;

fs/stat.c:206: stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
fs/stat.c:207: STATX_ATTR_DAX);
fs/stat.c:312: stat->attributes |= STATX_ATTR_MOUNT_ROOT;
fs/stat.c:313: stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;

So I guess that's (IMMUTABLE | APPEND | AUTOMOUNT | DAX | MOUNT_ROOT) ?

IMMUTABLE | APPEND seem to be captured in KSTAT_ATTR_VFS_FLAGS, so maybe that just needs to include the last three, and then we can use it to clear those bits from the fuse server's reply.

--D

Quoted 3 lines

Thanks,
Miklos

maintainer
#23 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 3 lines · Darrick J. Wong

IMMUTABLE | APPEND seem to be captured in KSTAT_ATTR_VFS_FLAGS, so maybe that just needs to include the last three, and then we can use it to clear those bits from the fuse server's reply.

Hmm. Fuse kernel module passes IMMUTABLE, APPEND and DAX through the fileattr interfaces. I.e. it doesn't query the respective VFS flags not does it try to set them.

For IMMUTABLE and APPEND I can imagine the server being able to handle these mostly (i.e. reject ops should be rejected). It would be nice
if the VFS was also aware. I wonder if we can fix this at this
point.

As for DAX, I don't see how the current behavior makes any sense, but again not seeing clearly what the best solution is. Currently fuse doesn't support DAX in the traditional sense, but does have DAX functionality in virtiofs and in will in famfs. Is this flag useful in that case?

I also fell that all unknown flags should also be masked off, but maybe that's too paranoid.

Thanks,
Miklos

#24 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 6 lines · Miklos Szeredi
Quoted 3 lines · Darrick J. Wong

IMMUTABLE | APPEND seem to be captured in KSTAT_ATTR_VFS_FLAGS, so maybe that just needs to include the last three, and then we can use it to clear those bits from the fuse server's reply.

Quoted 8 lines

Hmm. Fuse kernel module passes IMMUTABLE, APPEND and DAX through the fileattr interfaces. I.e. it doesn't query the respective VFS flags not does it try to set them.

For IMMUTABLE and APPEND I can imagine the server being able to handle these mostly (i.e. reject ops should be rejected). It would be nice
if the VFS was also aware. I wonder if we can fix this at this
point.

You can, kind of -- either send the server FS_IOC_FSGETXATTR or FS_IOC_GETFLAGS right after igetting an inode and set the VFS immutable/append flags from that; or we could add a couple of flag bits to fuse_attr::flags to avoid the extra upcall. You'd also have to update the S_IMMUTABLE/S_APPEND flags based on the results of FS_IOC_FSSETXATTR/FS_IOC_SETFLAGS.

https://git.kernel.org/pub/scm/linux/kernel/git/djwong/xfs-linux.git/commit/?h=djwong-wtf&id=16584b3fcdaaeb789f22847e9f82964957493a18

(I didn't enable any of that for non-iomap files to avoid changing expected behaviors)

Quoted 5 lines

As for DAX, I don't see how the current behavior makes any sense, but again not seeing clearly what the best solution is. Currently fuse doesn't support DAX in the traditional sense, but does have DAX functionality in virtiofs and in will in famfs. Is this flag useful in that case?

At this point, STATX_ATTR_DAX means that S_DAX is set on the VFS inode, and no other code is allowed to set that statx file attribute bit, per dax.rst:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/dax.rst

The flag is very much needed for virtiofs/famfs (and any future fuse+iomap+fsdax combination), because that's how application programs are supposed to detect that they can use load/store to mmap file regions without needing fsync/msync.

Quoted 2 lines

I also fell that all unknown flags should also be masked off, but maybe that's too paranoid.

That isn't a terrible idea.

--D

Quoted 3 lines

Thanks,
Miklos

maintainer
#25 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 4 lines · Darrick J. Wong

You can, kind of -- either send the server FS_IOC_FSGETXATTR or FS_IOC_GETFLAGS right after igetting an inode and set the VFS immutable/append flags from that; or we could add a couple of flag bits to fuse_attr::flags to avoid the extra upcall.

How about a new FUSE_LOOKUPX that uses fuse_statx instead of fuse_attr to initialize the inode?

Quoted 4 lines

The flag is very much needed for virtiofs/famfs (and any future fuse+iomap+fsdax combination), because that's how application programs are supposed to detect that they can use load/store to mmap file regions without needing fsync/msync.

Makes sense.

Quoted 4 lines
Quoted 2 lines

I also fell that all unknown flags should also be masked off, but maybe that's too paranoid.

That isn't a terrible idea.

So in conclusion, the following can be passed through from the fuse server to the statx syscall (directly or cached):

COMPRESSED
NODUMP
ENCRYPTED
VERITY
WRITE_ATOMIC

The following should be set (cached) in the relevant inode flag:

IMMUTABLE
APPEND

The following should be ignored and the VFS flag be used instead:

AUTOMOUNT
MOUNT_ROOT
DAX

And other attributes should just be ignored.

Agree?

Thanks,
Miklos

#26 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 9 lines · Miklos Szeredi
Quoted 4 lines · Darrick J. Wong

You can, kind of -- either send the server FS_IOC_FSGETXATTR or FS_IOC_GETFLAGS right after igetting an inode and set the VFS immutable/append flags from that; or we could add a couple of flag bits to fuse_attr::flags to avoid the extra upcall.

How about a new FUSE_LOOKUPX that uses fuse_statx instead of fuse_attr to initialize the inode?

Or what if we enlarged fuse_attr? Its fields mostly duplicate what was already in struct stat (and now struct statx):

struct fuse_attrx {
	struct fuse_statx	statx;
	uint32_t		flags; /* fuse_attr::flags */
};

Hrmm, fuse_attr is embedded in structs fuse_entry_out and fuse_attr_out. FUSE_{LOOKUP,OPEN,CREATE,SETATTR,GETATTR} (and direntplus) would need to be rototilled to support the new structure, and either you need new command codes for all that, or I guess one could set out_argvar = true and switch the out-struct decoding based on the size returned.

That sounds like a project in and of itself.

Quoted 12 lines
Quoted 4 lines

The flag is very much needed for virtiofs/famfs (and any future fuse+iomap+fsdax combination), because that's how application programs are supposed to detect that they can use load/store to mmap file regions without needing fsync/msync.

Makes sense.

Quoted 4 lines
Quoted 2 lines

I also fell that all unknown flags should also be masked off, but maybe that's too paranoid.

That isn't a terrible idea.

Quoted 8 lines

So in conclusion, the following can be passed through from the fuse server to the statx syscall (directly or cached):

COMPRESSED
NODUMP
ENCRYPTED
VERITY
WRITE_ATOMIC

Right.

Quoted 4 lines

The following should be set (cached) in the relevant inode flag:

IMMUTABLE
APPEND

Right, S_IMMUTABLE and S_APPEND.

Quoted 5 lines

The following should be ignored and the VFS flag be used instead:

AUTOMOUNT
MOUNT_ROOT
DAX

Yes to the first two.

As for /setting/ S_DAX, I think we just keep using FUSE_ATTR_DAX like we do now, since that's baked into the kernel <-> libfuse interface and can't go away. That would fit nicely with how the other filesystems do it.

Quoted 1 line

And other attributes should just be ignored.

I prefer to say "Other attribute bits are undefined and should just be ignored." :)

Quoted 1 line

Agree?

I think we do, except maybe the difficult first point. :)

--D

Quoted 3 lines

Thanks,
Miklos

maintainer
#27 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 6 lines · Darrick J. Wong
Quoted 1 line · Miklos Szeredi

Agree?

I think we do, except maybe the difficult first point. :)

Let's then defer the LOOKUPX thing ;) I'm fine with adding IMMUTABLE
and APPEND to fuse_attr::flags.

Thanks,
Miklos

#28 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 10 lines · Miklos Szeredi
Quoted 6 lines · Darrick J. Wong
Quoted 1 line · Miklos Szeredi

Agree?

I think we do, except maybe the difficult first point. :)

Let's then defer the LOOKUPX thing ;) I'm fine with adding IMMUTABLE
and APPEND to fuse_attr::flags.

OK. Should I hide that behind the fuse mount having iomap turned on? Or fc->is_local_fs == true? Or let any server set those bits?

One thing occurred to me -- for a plain old fuse server that is the client for some network filesystem, the other end might have its own immutable/append bits, in which case we actually *do* want to let those bits through from the FUSE_STATX replies.

--D

Quoted 3 lines

Thanks,
Miklos

maintainer
#29 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 13 lines · Darrick J. Wong
Quoted 10 lines · Miklos Szeredi
Quoted 6 lines · Darrick J. Wong
Quoted 1 line · Miklos Szeredi

Agree?

I think we do, except maybe the difficult first point. :)

Let's then defer the LOOKUPX thing ;) I'm fine with adding IMMUTABLE
and APPEND to fuse_attr::flags.

Quoted 7 lines

OK. Should I hide that behind the fuse mount having iomap turned on? Or fc->is_local_fs == true? Or let any server set those bits?

One thing occurred to me -- for a plain old fuse server that is the client for some network filesystem, the other end might have its own immutable/append bits, in which case we actually *do* want to let those bits through from the FUSE_STATX replies.

Right, as I said this might have worked without VFS help, but having it consistently in the VFS as well would be nicer.

In that spirit, putting those bits in the inode is safe only in the local fs case, so I guess that's what we should do. And for consistency, in the local fs case inode->flags should be the authoritative source and all the userspace API's should be looking at that, instead of what the server sent in the IOCTL or STATX replies.

Thanks,
Miklos

#30 lore raw

Re: [PATCH 4/7] fuse: implement file attributes mask for statx

Quoted 22 lines · Miklos Szeredi
Quoted 13 lines · Darrick J. Wong
Quoted 10 lines · Miklos Szeredi
Quoted 6 lines · Darrick J. Wong
Quoted 1 line · Miklos Szeredi

Agree?

I think we do, except maybe the difficult first point. :)

Let's then defer the LOOKUPX thing ;) I'm fine with adding IMMUTABLE
and APPEND to fuse_attr::flags.

Quoted 7 lines

OK. Should I hide that behind the fuse mount having iomap turned on? Or fc->is_local_fs == true? Or let any server set those bits?

One thing occurred to me -- for a plain old fuse server that is the client for some network filesystem, the other end might have its own immutable/append bits, in which case we actually *do* want to let those bits through from the FUSE_STATX replies.

Quoted 8 lines

Right, as I said this might have worked without VFS help, but having it consistently in the VFS as well would be nicer.

In that spirit, putting those bits in the inode is safe only in the local fs case, so I guess that's what we should do. And for consistency, in the local fs case inode->flags should be the authoritative source and all the userspace API's should be looking at that, instead of what the server sent in the IOCTL or STATX replies.

Ok, I'll meld that all together today.

--D

Quoted 3 lines

Thanks,
Miklos

#31 lore raw

PATCH 5/7 fuse: update file mode when updating acls

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If someone sets ACLs on a file that can be expressed fully as Unix DAC mode bits, most filesystems will then update the mode bits and drop the ACL xattr to reduce inefficiency in the file access paths. Let's do that too. Note that means that we can setacl and end up with no ACL xattrs, so we also need to tolerate ENODATA returns from fuse_removexattr.

---
 fs/fuse/acl.c |   30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
fs/fuse/acl.c +29 -1
index 8f484b105f13ab..63df349dee1caf 100644
@@ -98,6 +98,7 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 	struct inode *inode = d_inode(dentry);
 	struct fuse_conn *fc = get_fuse_conn(inode);
 	const char *name;
+	umode_t mode = inode->i_mode;
 	int ret;
 
 	if (fuse_is_bad(inode))
@@ -113,6 +114,17 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 	else
 		return -EINVAL;
 
+	/*
+	 * If the ACL can be represented entirely with changes to the mode
+	 * bits, then most filesystems will update the mode bits and delete
+	 * the ACL xattr.
+	 */
+	if (acl && type == ACL_TYPE_ACCESS && fc->posix_acl) {
+		ret = posix_acl_update_mode(idmap, inode, &mode, &acl);
+		if (ret)
+			return ret;
+	}
+
 	if (acl) {
 		unsigned int extra_flags = 0;
 		/*
@@ -143,7 +155,7 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 		 * through POSIX ACLs. Such daemons don't expect setgid bits to
 		 * be stripped.
 		 */
-		if (fc->posix_acl &&
+		if (fc->posix_acl && mode == inode->i_mode &&
 		    !in_group_or_capable(idmap, inode,
 					 i_gid_into_vfsgid(idmap, inode)))
 			extra_flags |= FUSE_SETXATTR_ACL_KILL_SGID;
@@ -152,6 +164,22 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 		kfree(value);
 	} else {
 		ret = fuse_removexattr(inode, name);
+		/* If the acl didn't exist to start with that's fine. */
+		if (ret == -ENODATA)
+			ret = 0;
+	}
+
+	/* If we scheduled a mode update above, push that to userspace now. */
+	if (!ret) {
+		struct iattr attr = { };
+
+		if (mode != inode->i_mode) {
+			attr.ia_valid |= ATTR_MODE;
+			attr.ia_mode = mode;
+		}
+
+		if (attr.ia_valid)
+			ret = fuse_do_setattr(idmap, dentry, &attr, NULL);
 	}
 
 	if (fc->posix_acl) {
maintainer
#32 lore raw

Re: [PATCH 5/7] fuse: update file mode when updating acls

Quoted 9 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If someone sets ACLs on a file that can be expressed fully as Unix DAC mode bits, most filesystems will then update the mode bits and drop the ACL xattr to reduce inefficiency in the file access paths. Let's do that too. Note that means that we can setacl and end up with no ACL xattrs, so we also need to tolerate ENODATA returns from fuse_removexattr.

This goes against the model of leaving this sort of task to the server. I understand your desire to do it in the kernel, since that
simplifies your server. But fuse is often used in passthrough mode,
where this will be done by the kernel, just one layer down the stack. In that case splitting a setxattr into a removexattr + chmod makes little sense.

Maybe extend the meaning of fc->default_permissions to mean: userspace
doesn't want to deal with any mode related stuff. Thoughts?

Thanks,
Miklos

#33 lore raw

Re: [PATCH 5/7] fuse: update file mode when updating acls

Quoted 11 lines · Miklos Szeredi
Quoted 9 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If someone sets ACLs on a file that can be expressed fully as Unix DAC mode bits, most filesystems will then update the mode bits and drop the ACL xattr to reduce inefficiency in the file access paths. Let's do that too. Note that means that we can setacl and end up with no ACL xattrs, so we also need to tolerate ENODATA returns from fuse_removexattr.

Quoted 6 lines

This goes against the model of leaving this sort of task to the server. I understand your desire to do it in the kernel, since that
simplifies your server. But fuse is often used in passthrough mode,
where this will be done by the kernel, just one layer down the stack. In that case splitting a setxattr into a removexattr + chmod makes little sense.

Ah, right. I temporarily forgot about network/cluster filesystems where the local kernel isn't necessarily in charge of the file metadata and permissions.

Quoted 2 lines

Maybe extend the meaning of fc->default_permissions to mean: userspace
doesn't want to deal with any mode related stuff. Thoughts?

As suggested in the thread for the next patch, maybe I should just hide this new acl behavior behind (fc->iomap || sb->s_bdev != NULL)?

--D

Quoted 3 lines

Thanks,
Miklos

maintainer
#34 lore raw

Re: [PATCH 5/7] fuse: update file mode when updating acls

Quoted 2 lines · Darrick J. Wong

As suggested in the thread for the next patch, maybe I should just hide this new acl behavior behind (fc->iomap || sb->s_bdev != NULL)?

Okay, but please create a fc->is_local_fs that is set on the above condition to make this more readable.

Thanks,
Miklos

#35 lore raw

Re: [PATCH 5/7] fuse: update file mode when updating acls

Quoted 7 lines · Miklos Szeredi
Quoted 2 lines · Darrick J. Wong

As suggested in the thread for the next patch, maybe I should just hide this new acl behavior behind (fc->iomap || sb->s_bdev != NULL)?

Okay, but please create a fc->is_local_fs that is set on the above condition to make this more readable.

Done.

--D

Quoted 3 lines

Thanks,
Miklos

#36 lore raw

PATCH 6/7 fuse: propagate default and file acls on creation

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Propagate the default and file access ACLs to new children when creating them, just like the other kernel filesystems.

---
 fs/fuse/fuse_i.h |    4 ++
 fs/fuse/acl.c    |   65 ++++++++++++++++++++++++++++++++++++++
 fs/fuse/dir.c    |   92 +++++++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 138 insertions(+), 23 deletions(-)
fs/fuse/fuse_i.h +4 -0
index bb1fdae0bbc906..b80505f5431e0b 100644
@@ -1452,6 +1452,10 @@ struct posix_acl *fuse_get_acl(struct mnt_idmap *idmap,
 			       struct dentry *dentry, int type);
 int fuse_set_acl(struct mnt_idmap *, struct dentry *dentry,
 		 struct posix_acl *acl, int type);
+int fuse_acl_create(struct inode *dir, umode_t *mode,
+		    struct posix_acl **default_acl, struct posix_acl **acl);
+int fuse_init_acls(struct inode *inode, const struct posix_acl *default_acl,
+		   const struct posix_acl *acl);
 
 /* readdir.c */
 int fuse_readdir(struct file *file, struct dir_context *ctx);
fs/fuse/acl.c +65 -0

68 lines not on this page · raw message

fs/fuse/dir.c +69 -23

226 lines not on this page · raw message

maintainer
#37 lore raw

Re: [PATCH 6/7] fuse: propagate default and file acls on creation

Quoted 5 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Propagate the default and file access ACLs to new children when creating them, just like the other kernel filesystems.

Another problem of this and the previous patch is being racy. Not "real" filesystems like fuse2fs, but this is going to trip network fs up badly, where such races would be really difficult to test.

We could add a new feature flag, but we seem to have proliferation of this sort. We have default_permissions, then handle_killpriv, then handle_killpriv_v2. Seems like we need a flag to tell the kernel to treat this as a local fs, where it can do all the local fs'y things without fear of breaking remote fs.

Does that make sense?

Thanks,
Miklos

#38 lore raw

Re: [PATCH 6/7] fuse: propagate default and file acls on creation

Quoted 10 lines · Miklos Szeredi
Quoted 5 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Propagate the default and file access ACLs to new children when creating them, just like the other kernel filesystems.

Another problem of this and the previous patch is being racy. Not "real" filesystems like fuse2fs, but this is going to trip network fs up badly, where such races would be really difficult to test.

Ahh, right -- I neglected that the fuse interface is more or less what you'd need for a client node of a network/cluster filesystem.

Quoted 7 lines

We could add a new feature flag, but we seem to have proliferation of this sort. We have default_permissions, then handle_killpriv, then handle_killpriv_v2. Seems like we need a flag to tell the kernel to treat this as a local fs, where it can do all the local fs'y things without fear of breaking remote fs.

Does that make sense?

Yeah.

How about I hide the functionality of this ACL patch and the previous one behind (fc->iomap || sb->s_bdev != NULL)? The iomap functionality that I'm working on is only useful for filesystems that want to behave like a local fs, including all the "I went out to lunch DoS" warts. AFAICT the other fuse developers seem to accept that fuseblk servers can do that too. Does that sound ok?

If anyone ever wanted to use fuse+iomap for a cluster fs, I guess I'd have to go back to issuing FUSE_READ/WRITE requests to userspace for permission checking and resource acquisition. But so far no cluster filesystems use fs/iomap/ so it's just unsupported.

(And to make this explicit to anyone watching on the list -- all of my work is completely separate from Joanne's efforts to adapt fuse to use iomap for tracking pagecache dirty state.)

--D

Quoted 2 lines

Thanks,
Miklos

#39 lore raw

PATCH 7/7 fuse: enable FUSE_SYNCFS for all servers

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Turn on syncfs for all fuse servers so that the ones in the know can flush cached intermediate data and logs to disk.

---
 fs/fuse/inode.c |    1 +
 1 file changed, 1 insertion(+)
fs/fuse/inode.c +1 -0
index 463879830ecf34..b05510799f93e1 100644
@@ -1814,6 +1814,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
 		if (!sb_set_blocksize(sb, ctx->blksize))
 			goto err;
 #endif
+		fc->sync_fs = 1;
 	} else {
 		sb->s_blocksize = PAGE_SIZE;
 		sb->s_blocksize_bits = PAGE_SHIFT;
maintainer
#40 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 17 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Turn on syncfs for all fuse servers so that the ones in the know can flush cached intermediate data and logs to disk.

---
 fs/fuse/inode.c |    1 +
 1 file changed, 1 insertion(+)
fs/fuse/inode.c +0 -0
index 463879830ecf34..b05510799f93e1 100644

fs/fuse/inode.c int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) · line 1817

Quoted 4 lines
fs/fuse/inode.c +1 -0
@@ -1814,6 +1814,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
                if (!sb_set_blocksize(sb, ctx->blksize))
                        goto err;
 #endif
+               fc->sync_fs = 1;

AFAICT, this enables syncfs only for fuseblk servers. Is this what you intended?

Thanks,
Joanne

Quoted 4 lines
fs/fuse/inode.c +0 -0
        } else {
                sb->s_blocksize = PAGE_SIZE;
                sb->s_blocksize_bits = PAGE_SHIFT;
#41 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 13 lines · Joanne Koong
Quoted 12 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Turn on syncfs for all fuse servers so that the ones in the know can flush cached intermediate data and logs to disk.

---
 fs/fuse/inode.c |    1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

fs/fuse/inode.c int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) · line 1817

Quoted 9 lines
fs/fuse/inode.c +1 -0
index 463879830ecf34..b05510799f93e1 100644
@@ -1814,6 +1814,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
                if (!sb_set_blocksize(sb, ctx->blksize))
                        goto err;
 #endif
+               fc->sync_fs = 1;

AFAICT, this enables syncfs only for fuseblk servers. Is this what you intended?

I meant to say for all fuseblk servers, but TBH I can't see why you wouldn't want to enable it for non-fuseblk servers too?

(Maybe I was being overly cautious ;))

--D

Quoted 7 lines

Thanks,
Joanne

Quoted 4 lines
fs/fuse/inode.c +0 -0
        } else {
                sb->s_blocksize = PAGE_SIZE;
                sb->s_blocksize_bits = PAGE_SHIFT;
maintainer
#42 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 27 lines · Darrick J. Wong
Quoted 13 lines · Joanne Koong
Quoted 12 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Turn on syncfs for all fuse servers so that the ones in the know can flush cached intermediate data and logs to disk.

---
 fs/fuse/inode.c |    1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

fs/fuse/inode.c int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) · line 1817

Quoted 9 lines
fs/fuse/inode.c +1 -0
index 463879830ecf34..b05510799f93e1 100644
@@ -1814,6 +1814,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
                if (!sb_set_blocksize(sb, ctx->blksize))
                        goto err;
 #endif
+               fc->sync_fs = 1;

AFAICT, this enables syncfs only for fuseblk servers. Is this what you intended?

Quoted 4 lines

I meant to say for all fuseblk servers, but TBH I can't see why you wouldn't want to enable it for non-fuseblk servers too?

(Maybe I was being overly cautious ;))

Just checked, the initial commit message has

<quote 2d82ab251ef0f6e7716279b04e9b5a01a86ca530>
Note that such an operation allows the file server to DoS sync(). Since a typical FUSE file server is an untrusted piece of software running in userspace, this is disabled by default. Only enable it with virtiofs for now since virtiofsd is supposedly trusted by the guest kernel. </quote>

With that we could at least enable for all privileged servers? And for non-privileged this could be an async?

Thanks,
Bernd

maintainer
#43 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 44 lines · Bernd Schubert
Quoted 27 lines · Darrick J. Wong
Quoted 13 lines · Joanne Koong
Quoted 12 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Turn on syncfs for all fuse servers so that the ones in the know can flush cached intermediate data and logs to disk.

---
 fs/fuse/inode.c |    1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

fs/fuse/inode.c int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) · line 1817

Quoted 9 lines
fs/fuse/inode.c +1 -0
index 463879830ecf34..b05510799f93e1 100644
@@ -1814,6 +1814,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
                if (!sb_set_blocksize(sb, ctx->blksize))
                        goto err;
 #endif
+               fc->sync_fs = 1;

AFAICT, this enables syncfs only for fuseblk servers. Is this what you intended?

Quoted 4 lines

I meant to say for all fuseblk servers, but TBH I can't see why you wouldn't want to enable it for non-fuseblk servers too?

(Maybe I was being overly cautious ;))

Just checked, the initial commit message has

<quote 2d82ab251ef0f6e7716279b04e9b5a01a86ca530>
Note that such an operation allows the file server to DoS sync(). Since a typical FUSE file server is an untrusted piece of software running in userspace, this is disabled by default. Only enable it with virtiofs for now since virtiofsd is supposedly trusted by the guest kernel. </quote>

Quoted 2 lines

With that we could at least enable for all privileged servers? And for non-privileged this could be an async?

This sounds reasonable to me.

Thanks,
Joanne

Quoted 6 lines

Thanks,
Bernd

#44 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

In reply to Bernd Schubert

To the best of my understanding, there are two code paths which may yield FUSE_SYNCFS: one from user-space syscall syncfs(2) and the other from within the kernel itself. Unfortunately, there is no way to distinguish between the two at sb->s_op->sync_fs level, and the DoS argument refers to the second (kernel) case. If we could somehow propagate this info all the way down to the fuse layer then I see no reason for preventing (non-privileged) user-space programs from calling syncfs(2) over FUSE mounted file-systems.

Please correct me if I am wrong with my analysis.

- Shachar.

Quoted 56 lines · Bernd Schubert
Quoted 27 lines · Darrick J. Wong
Quoted 13 lines · Joanne Koong
Quoted 12 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Turn on syncfs for all fuse servers so that the ones in the know can flush cached intermediate data and logs to disk.

---
 fs/fuse/inode.c |    1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

fs/fuse/inode.c int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) · line 1817

Quoted 9 lines
fs/fuse/inode.c +1 -0
index 463879830ecf34..b05510799f93e1 100644
@@ -1814,6 +1814,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
                if (!sb_set_blocksize(sb, ctx->blksize))
                        goto err;
 #endif
+               fc->sync_fs = 1;

AFAICT, this enables syncfs only for fuseblk servers. Is this what you intended?

Quoted 4 lines

I meant to say for all fuseblk servers, but TBH I can't see why you wouldn't want to enable it for non-fuseblk servers too?

(Maybe I was being overly cautious ;))

Just checked, the initial commit message has

<quote 2d82ab251ef0f6e7716279b04e9b5a01a86ca530>
Note that such an operation allows the file server to DoS sync(). Since a typical FUSE file server is an untrusted piece of software running in userspace, this is disabled by default. Only enable it with virtiofs for now since virtiofsd is supposedly trusted by the guest kernel. </quote>

With that we could at least enable for all privileged servers? And for non-privileged this could be an async?

Thanks,
Bernd

maintainer
#45 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 9 lines · Shachar Sharon

To the best of my understanding, there are two code paths which may yield FUSE_SYNCFS: one from user-space syscall syncfs(2) and the other from within the kernel itself. Unfortunately, there is no way to distinguish between the two at sb->s_op->sync_fs level, and the DoS argument refers to the second (kernel) case. If we could somehow propagate this info all the way down to the fuse layer then I see no reason for preventing (non-privileged) user-space programs from calling syncfs(2) over FUSE mounted file-systems.

I interpreted the DoS comment as referring to the scenario where a userspace program calls generic sync() and if an untrusted fuse server deliberately hangs on servicing that request then it'll hang sync forever. I think if this only affected the syncfs() syscall then it wouldn't be a problem since the caller is directly invoking it on a fuse fd, but if it affects generic sync() that seems like a big issue to me. Or at least that's my understanding of the code with ksys_sync() -> iterate_supers(sync_fs_one_sb, &wait).

Thanks,
Joanne

Quoted 64 lines

Please correct me if I am wrong with my analysis.

- Shachar.

Quoted 56 lines · Bernd Schubert
Quoted 27 lines · Darrick J. Wong
Quoted 13 lines · Joanne Koong
Quoted 12 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Turn on syncfs for all fuse servers so that the ones in the know can flush cached intermediate data and logs to disk.

---
 fs/fuse/inode.c |    1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

fs/fuse/inode.c int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) · line 1817

Quoted 9 lines
fs/fuse/inode.c +1 -0
index 463879830ecf34..b05510799f93e1 100644
@@ -1814,6 +1814,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
                if (!sb_set_blocksize(sb, ctx->blksize))
                        goto err;
 #endif
+               fc->sync_fs = 1;

AFAICT, this enables syncfs only for fuseblk servers. Is this what you intended?

Quoted 4 lines

I meant to say for all fuseblk servers, but TBH I can't see why you wouldn't want to enable it for non-fuseblk servers too?

(Maybe I was being overly cautious ;))

Just checked, the initial commit message has

<quote 2d82ab251ef0f6e7716279b04e9b5a01a86ca530>
Note that such an operation allows the file server to DoS sync(). Since a typical FUSE file server is an untrusted piece of software running in userspace, this is disabled by default. Only enable it with virtiofs for now since virtiofsd is supposedly trusted by the guest kernel. </quote>

With that we could at least enable for all privileged servers? And for non-privileged this could be an async?

Thanks,
Bernd

#46 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 11 lines · Joanne Koong
Quoted 9 lines · Shachar Sharon

To the best of my understanding, there are two code paths which may yield FUSE_SYNCFS: one from user-space syscall syncfs(2) and the other from within the kernel itself. Unfortunately, there is no way to distinguish between the two at sb->s_op->sync_fs level, and the DoS argument refers to the second (kernel) case. If we could somehow propagate this info all the way down to the fuse layer then I see no reason for preventing (non-privileged) user-space programs from calling syncfs(2) over FUSE mounted file-systems.

Quoted 8 lines

I interpreted the DoS comment as referring to the scenario where a userspace program calls generic sync() and if an untrusted fuse server deliberately hangs on servicing that request then it'll hang sync forever. I think if this only affected the syncfs() syscall then it wouldn't be a problem since the caller is directly invoking it on a fuse fd, but if it affects generic sync() that seems like a big issue to me. Or at least that's my understanding of the code with ksys_sync() -> iterate_supers(sync_fs_one_sb, &wait).

<shrug> I think you can already DoS sync() (and by extension any other place in the kernel where we try to flush out all filesystems in one go) by dropping a FUSE_SETATTR call on the floor, because that's how we flush dirty inodes to disk? Or by doing the same for an FUSE_FSYNC call?

--D

Quoted 67 lines

Thanks,
Joanne

Quoted 64 lines

Please correct me if I am wrong with my analysis.

- Shachar.

Quoted 56 lines · Bernd Schubert
Quoted 27 lines · Darrick J. Wong
Quoted 13 lines · Joanne Koong
Quoted 12 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Turn on syncfs for all fuse servers so that the ones in the know can flush cached intermediate data and logs to disk.

---
 fs/fuse/inode.c |    1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

fs/fuse/inode.c int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) · line 1817

Quoted 9 lines
fs/fuse/inode.c +1 -0
index 463879830ecf34..b05510799f93e1 100644
@@ -1814,6 +1814,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
                if (!sb_set_blocksize(sb, ctx->blksize))
                        goto err;
 #endif
+               fc->sync_fs = 1;

AFAICT, this enables syncfs only for fuseblk servers. Is this what you intended?

Quoted 4 lines

I meant to say for all fuseblk servers, but TBH I can't see why you wouldn't want to enable it for non-fuseblk servers too?

(Maybe I was being overly cautious ;))

Just checked, the initial commit message has

<quote 2d82ab251ef0f6e7716279b04e9b5a01a86ca530>
Note that such an operation allows the file server to DoS sync(). Since a typical FUSE file server is an untrusted piece of software running in userspace, this is disabled by default. Only enable it with virtiofs for now since virtiofsd is supposedly trusted by the guest kernel. </quote>

With that we could at least enable for all privileged servers? And for non-privileged this could be an async?

Thanks,
Bernd

maintainer
#47 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 22 lines · Darrick J. Wong
Quoted 11 lines · Joanne Koong
Quoted 9 lines · Shachar Sharon

To the best of my understanding, there are two code paths which may yield FUSE_SYNCFS: one from user-space syscall syncfs(2) and the other from within the kernel itself. Unfortunately, there is no way to distinguish between the two at sb->s_op->sync_fs level, and the DoS argument refers to the second (kernel) case. If we could somehow propagate this info all the way down to the fuse layer then I see no reason for preventing (non-privileged) user-space programs from calling syncfs(2) over FUSE mounted file-systems.

Quoted 8 lines

I interpreted the DoS comment as referring to the scenario where a userspace program calls generic sync() and if an untrusted fuse server deliberately hangs on servicing that request then it'll hang sync forever. I think if this only affected the syncfs() syscall then it wouldn't be a problem since the caller is directly invoking it on a fuse fd, but if it affects generic sync() that seems like a big issue to me. Or at least that's my understanding of the code with ksys_sync() -> iterate_supers(sync_fs_one_sb, &wait).

Quoted 5 lines

<shrug> I think you can already DoS sync() (and by extension any other place in the kernel where we try to flush out all filesystems in one go) by dropping a FUSE_SETATTR call on the floor, because that's how we flush dirty inodes to disk? Or by doing the same for an FUSE_FSYNC call?

Isn't the sync() in fuse right now gated by fc->sync_fs (which is only set to true for virtiofsd)? I don't see where FUSE_SETATTR or FUSE_FSYNC get sent in the sync() path to untrusted servers.

Thanks,
Joanne

Quoted 3 lines

--D

maintainer
#48 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 3 lines · Joanne Koong

Isn't the sync() in fuse right now gated by fc->sync_fs (which is only set to true for virtiofsd)? I don't see where FUSE_SETATTR or FUSE_FSYNC get sent in the sync() path to untrusted servers.

Hmm, it's through sync_inodes_one_sb() that fuse_write_inode() could get called, which then would trigger a FUSE_SETATTR.

Does anyone know how useful sync() is in practice? I guess most
applications have switched to syncfs() which is more specific.

In any case, I don't remember a complaint about sync(2) ignoring fuse filesystems.

Thanks,
Miklos

#49 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 8 lines · Miklos Szeredi
Quoted 3 lines · Joanne Koong

Isn't the sync() in fuse right now gated by fc->sync_fs (which is only set to true for virtiofsd)? I don't see where FUSE_SETATTR or FUSE_FSYNC get sent in the sync() path to untrusted servers.

Hmm, it's through sync_inodes_one_sb() that fuse_write_inode() could get called, which then would trigger a FUSE_SETATTR.

<nod> So SETATTR is a theoretical DoS vector, but that's already a property of most filesystems that write to an off-cpu device such as a disk or another computer. ;)

Quoted 2 lines

Does anyone know how useful sync() is in practice? I guess most
applications have switched to syncfs() which is more specific.

Well old greybeards such as myself reboot busted systems with

$ sync
$ sync
$ sync
<sysrq-b>

because that's what you'd type after "startx &" fscked up the display. It's 2025 and ... that still happens. :(

Debian codesearch shows a few thousand hits for sync(), some of which are in things like LibreOffice.

Quoted 2 lines

In any case, I don't remember a complaint about sync(2) ignoring fuse filesystems.

Well sync() will poke all the fuse filesystems, right?

--D

Quoted 2 lines

Thanks,
Miklos

maintainer
#50 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 1 line · Darrick J. Wong

Well sync() will poke all the fuse filesystems, right?

Only those with writeback_cache enabled. But yeah, apparently this was overlooked when dealing with "don't allow DoS-ing sync(2)".

Can't see a good way out of this.

Thanks,
Miklos

maintainer
#51 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 6 lines · Miklos Szeredi
Quoted 1 line · Darrick J. Wong

Well sync() will poke all the fuse filesystems, right?

Only those with writeback_cache enabled.

And when servicing shared write mmaps...

Thanks,
Miklos

#52 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

In reply to Miklos Szeredi
Quoted 8 lines · Miklos Szeredi
Quoted 1 line · Darrick J. Wong

Well sync() will poke all the fuse filesystems, right?

Only those with writeback_cache enabled. But yeah, apparently this was overlooked when dealing with "don't allow DoS-ing sync(2)".

Can't see a good way out of this.

I wonder, is it possible to shift a fuse_simple_request to behave like a fuse_simple_background request? For certain DOS-happy requests, one could use wait_event_interruptible_timeout(&req->waitq...) with a really high timeout.

If the wait times out, we shift the completion to asynchronous and return -ETIMEDOUT to the (blocked) caller. That would allow the system to make progress though you'd probably have to take some drastic action if the fuse server sends back a failure (e.g. setting FUSE_I_BAD).

(The problem with timeouts is that I tried setting a 60s timeout on fuse2fs and discovered that certain horrid fstests actually create monster files that take 45min to FUSE_RELEASE and so I don't know what a reasonable timeout is...)

--D

Quoted 2 lines

Thanks,
Miklos

maintainer
#53 lore raw

Re: [PATCH 7/7] fuse: enable FUSE_SYNCFS for all servers

Quoted 21 lines · Darrick J. Wong
Quoted 8 lines · Miklos Szeredi
Quoted 1 line · Darrick J. Wong

Well sync() will poke all the fuse filesystems, right?

Only those with writeback_cache enabled. But yeah, apparently this was overlooked when dealing with "don't allow DoS-ing sync(2)".

Can't see a good way out of this.

I wonder, is it possible to shift a fuse_simple_request to behave like a fuse_simple_background request? For certain DOS-happy requests, one could use wait_event_interruptible_timeout(&req->waitq...) with a really high timeout.

If the wait times out, we shift the completion to asynchronous and return -ETIMEDOUT to the (blocked) caller. That would allow the system to make progress though you'd probably have to take some drastic action if the fuse server sends back a failure (e.g. setting FUSE_I_BAD).

Quoted 4 lines

(The problem with timeouts is that I tried setting a 60s timeout on fuse2fs and discovered that certain horrid fstests actually create monster files that take 45min to FUSE_RELEASE and so I don't know what a reasonable timeout is...)

Why not just send the setattr request in fuse_write_inode() as a background request instead of first sending it synchronously with a timeout? for the sync() case, the only DoS path is (as Miklos pointed out to me in his earlier comment) the sync_inodes_one_sb() -> fuse_write_inode(). But the only thing fuse_write_inode() does is call fuse_flush_times() to send the inode i_mtime to the server. Is this i_mtime info even that important? It seems fine to me to have that relayed always as a background request.

Thanks,
Joanne

Quoted 5 lines

--D

Quoted 2 lines

Thanks,
Miklos

#54 lore raw

RFC PATCH v4 1/4 libfuse: general bug fixes

In reply to Darrick J. Wong

Hi all,

Here's a collection of fixes that I *think* are bugs in libfuse.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-fixes
---
Commits in this patchset:
* libfuse: don't put HAVE_STATX in a public header

---
 include/fuse.h           |    2 --
 include/fuse_lowlevel.h  |    2 --
 example/memfs_ll.cc      |    2 +-
 example/passthrough.c    |    2 +-
 example/passthrough_fh.c |    2 +-
 example/passthrough_ll.c |    2 +-
 6 files changed, 4 insertions(+), 8 deletions(-)
#55 lore raw

PATCH 1/1 libfuse: don't put HAVE_STATX in a public header

From: Darrick J. Wong <[email protected]>

fuse.h and fuse_lowlevel.h are public headers, don't expose internal build system config variables to downstream clients. This can also lead to function pointer ordering issues if (say) libfuse gets built with HAVE_STATX but the client program doesn't define a HAVE_STATX.

Get rid of the conditionals in the public header files to fix this.

---
 include/fuse.h           |    2 --
 include/fuse_lowlevel.h  |    2 --
 example/memfs_ll.cc      |    2 +-
 example/passthrough.c    |    2 +-
 example/passthrough_fh.c |    2 +-
 example/passthrough_ll.c |    2 +-
 6 files changed, 4 insertions(+), 8 deletions(-)
include/fuse.h +0 -2
index 06feacb070fbfb..209102651e9454 100644
@@ -854,7 +854,6 @@ struct fuse_operations {
 	 */
 	off_t (*lseek) (const char *, off_t off, int whence, struct fuse_file_info *);
 
-#ifdef HAVE_STATX
 	/**
 	 * Get extended file attributes.
 	 *
@@ -865,7 +864,6 @@ struct fuse_operations {
 	 */
 	int (*statx)(const char *path, int flags, int mask, struct statx *stxbuf,
 		     struct fuse_file_info *fi);
-#endif
 };
 
 /** Extra context that may be needed by some filesystems
include/fuse_lowlevel.h +0 -2
index 844ee710295973..8d87be413bfe37 100644
@@ -1327,7 +1327,6 @@ struct fuse_lowlevel_ops {
 	void (*tmpfile) (fuse_req_t req, fuse_ino_t parent,
 			mode_t mode, struct fuse_file_info *fi);
 
-#ifdef HAVE_STATX
 	/**
 	 * Get extended file attributes.
 	 *
@@ -1343,7 +1342,6 @@ struct fuse_lowlevel_ops {
 	 */
 	void (*statx)(fuse_req_t req, fuse_ino_t ino, int flags, int mask,
 		      struct fuse_file_info *fi);
-#endif
 };
 
 /**
example/memfs_ll.cc +1 -1
index edda34b4e43d39..7055a434a439cd 100644
@@ -6,7 +6,7 @@
   See the file GPL2.txt.
 */
 
-#define FUSE_USE_VERSION 317
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #include <algorithm>
 #include <stdio.h>
example/passthrough.c +1 -1
index fdaa19e331a17d..1f09c2dc05df1e 100644
@@ -23,7 +23,7 @@
  */
 
 
-#define FUSE_USE_VERSION 31
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #define _GNU_SOURCE
 
example/passthrough_fh.c +1 -1
index 0d4fb5bd4df0d6..6403fbb74c7759 100644
@@ -23,7 +23,7 @@
  * \include passthrough_fh.c
  */
 
-#define FUSE_USE_VERSION 31
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #define _GNU_SOURCE
 
example/passthrough_ll.c +1 -1
index 5ca6efa2300abe..8a5ac2e9226b59 100644
@@ -35,7 +35,7 @@
  */
 
 #define _GNU_SOURCE
-#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #include <fuse_lowlevel.h>
 #include <unistd.h>
maintainer
#56 lore raw

Re: [PATCH 1/1] libfuse: don't put HAVE_STATX in a public header

Quoted 105 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

fuse.h and fuse_lowlevel.h are public headers, don't expose internal build system config variables to downstream clients. This can also lead to function pointer ordering issues if (say) libfuse gets built with HAVE_STATX but the client program doesn't define a HAVE_STATX.

Get rid of the conditionals in the public header files to fix this.

---
 include/fuse.h           |    2 --
 include/fuse_lowlevel.h  |    2 --
 example/memfs_ll.cc      |    2 +-
 example/passthrough.c    |    2 +-
 example/passthrough_fh.c |    2 +-
 example/passthrough_ll.c |    2 +-
 6 files changed, 4 insertions(+), 8 deletions(-)
include/fuse.h +0 -2
index 06feacb070fbfb..209102651e9454 100644
@@ -854,7 +854,6 @@ struct fuse_operations {
 	 */
 	off_t (*lseek) (const char *, off_t off, int whence, struct fuse_file_info *);
 
-#ifdef HAVE_STATX
 	/**
 	 * Get extended file attributes.
 	 *
@@ -865,7 +864,6 @@ struct fuse_operations {
 	 */
 	int (*statx)(const char *path, int flags, int mask, struct statx *stxbuf,
 		     struct fuse_file_info *fi);
-#endif
 };
 
 /** Extra context that may be needed by some filesystems
include/fuse_lowlevel.h +0 -2
index 844ee710295973..8d87be413bfe37 100644
@@ -1327,7 +1327,6 @@ struct fuse_lowlevel_ops {
 	void (*tmpfile) (fuse_req_t req, fuse_ino_t parent,
 			mode_t mode, struct fuse_file_info *fi);
 
-#ifdef HAVE_STATX
 	/**
 	 * Get extended file attributes.
 	 *
@@ -1343,7 +1342,6 @@ struct fuse_lowlevel_ops {
 	 */
 	void (*statx)(fuse_req_t req, fuse_ino_t ino, int flags, int mask,
 		      struct fuse_file_info *fi);
-#endif
 };
 
 /**
example/memfs_ll.cc +1 -1
index edda34b4e43d39..7055a434a439cd 100644
@@ -6,7 +6,7 @@
   See the file GPL2.txt.
 */
 
-#define FUSE_USE_VERSION 317
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #include <algorithm>
 #include <stdio.h>
example/passthrough.c +1 -1
index fdaa19e331a17d..1f09c2dc05df1e 100644
@@ -23,7 +23,7 @@
  */
 
 
-#define FUSE_USE_VERSION 31
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #define _GNU_SOURCE
 
example/passthrough_fh.c +1 -1
index 0d4fb5bd4df0d6..6403fbb74c7759 100644
@@ -23,7 +23,7 @@
  * \include passthrough_fh.c
  */
 
-#define FUSE_USE_VERSION 31
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #define _GNU_SOURCE
 
example/passthrough_ll.c +0 -0
index 5ca6efa2300abe..8a5ac2e9226b59 100644

example/passthrough_ll.c line 41

Quoted 8 lines
example/passthrough_ll.c +1 -1
@@ -35,7 +35,7 @@
  */
 
 #define _GNU_SOURCE
-#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #include <fuse_lowlevel.h>
 #include <unistd.h>

Thanks, I'm going to apply it to libfuse tomorrow. I think the version update in the examples is not strictly needed, but doesn't hurt either.

Thanks,
Bernd

#57 lore raw

Re: [PATCH 1/1] libfuse: don't put HAVE_STATX in a public header

Quoted 113 lines · Bernd Schubert
Quoted 110 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

fuse.h and fuse_lowlevel.h are public headers, don't expose internal build system config variables to downstream clients. This can also lead to function pointer ordering issues if (say) libfuse gets built with HAVE_STATX but the client program doesn't define a HAVE_STATX.

Get rid of the conditionals in the public header files to fix this.

---
 include/fuse.h           |    2 --
 include/fuse_lowlevel.h  |    2 --
 example/memfs_ll.cc      |    2 +-
 example/passthrough.c    |    2 +-
 example/passthrough_fh.c |    2 +-
 example/passthrough_ll.c |    2 +-
 6 files changed, 4 insertions(+), 8 deletions(-)
include/fuse.h +0 -2
index 06feacb070fbfb..209102651e9454 100644
@@ -854,7 +854,6 @@ struct fuse_operations {
 	 */
 	off_t (*lseek) (const char *, off_t off, int whence, struct fuse_file_info *);
 
-#ifdef HAVE_STATX
 	/**
 	 * Get extended file attributes.
 	 *
@@ -865,7 +864,6 @@ struct fuse_operations {
 	 */
 	int (*statx)(const char *path, int flags, int mask, struct statx *stxbuf,
 		     struct fuse_file_info *fi);
-#endif
 };
 
 /** Extra context that may be needed by some filesystems
include/fuse_lowlevel.h +0 -2
index 844ee710295973..8d87be413bfe37 100644
@@ -1327,7 +1327,6 @@ struct fuse_lowlevel_ops {
 	void (*tmpfile) (fuse_req_t req, fuse_ino_t parent,
 			mode_t mode, struct fuse_file_info *fi);
 
-#ifdef HAVE_STATX
 	/**
 	 * Get extended file attributes.
 	 *
@@ -1343,7 +1342,6 @@ struct fuse_lowlevel_ops {
 	 */
 	void (*statx)(fuse_req_t req, fuse_ino_t ino, int flags, int mask,
 		      struct fuse_file_info *fi);
-#endif
 };
 
 /**
example/memfs_ll.cc +1 -1
index edda34b4e43d39..7055a434a439cd 100644
@@ -6,7 +6,7 @@
   See the file GPL2.txt.
 */
 
-#define FUSE_USE_VERSION 317
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #include <algorithm>
 #include <stdio.h>
example/passthrough.c +1 -1
index fdaa19e331a17d..1f09c2dc05df1e 100644
@@ -23,7 +23,7 @@
  */
 
 
-#define FUSE_USE_VERSION 31
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #define _GNU_SOURCE
 
example/passthrough_fh.c +1 -1
index 0d4fb5bd4df0d6..6403fbb74c7759 100644
@@ -23,7 +23,7 @@
  * \include passthrough_fh.c
  */
 
-#define FUSE_USE_VERSION 31
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
 #define _GNU_SOURCE
 
example/passthrough_ll.c +1 -1
index 5ca6efa2300abe..8a5ac2e9226b59 100644
@@ -35,7 +35,7 @@
  */
 
 #define _GNU_SOURCE
-#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
+#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 18)
 
Quoted 7 lines

example/passthrough_ll.c line 41

Quoted 3 lines
example/passthrough_ll.c +0 -0
 #include <fuse_lowlevel.h>
 #include <unistd.h>

Thanks, I'm going to apply it to libfuse tomorrow. I think the version update in the examples is not strictly needed, but doesn't hurt either.

Thank you!

Yeah, I don't think the examples updates are strictly necessary either, but the examples might as well give full access to someone who wants to copy-paste them into a new server.

--D

Quoted 3 lines

Thanks,
Bernd

maintainer
#58 lore raw

Re: [PATCH 1/1] libfuse: don't put HAVE_STATX in a public header

In reply to Darrick J. Wong
Quoted 34 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

fuse.h and fuse_lowlevel.h are public headers, don't expose internal build system config variables to downstream clients. This can also lead to function pointer ordering issues if (say) libfuse gets built with HAVE_STATX but the client program doesn't define a HAVE_STATX.

Get rid of the conditionals in the public header files to fix this.

---
 include/fuse.h           |    2 --
 include/fuse_lowlevel.h  |    2 --
 example/memfs_ll.cc      |    2 +-
 example/passthrough.c    |    2 +-
 example/passthrough_fh.c |    2 +-
 example/passthrough_ll.c |    2 +-
 6 files changed, 4 insertions(+), 8 deletions(-)
include/fuse.h +0 -1
index 06feacb070fbfb..209102651e9454 100644
@@ -854,7 +854,6 @@ struct fuse_operations {
         */
        off_t (*lseek) (const char *, off_t off, int whence, struct fuse_file_info *);
 
-#ifdef HAVE_STATX
        /**
         * Get extended file attributes.
         *

include/fuse.h struct fuse_operations { · line 867

Quoted 5 lines
include/fuse.h +0 -1
@@ -865,7 +864,6 @@ struct fuse_operations {
         */
        int (*statx)(const char *path, int flags, int mask, struct statx *stxbuf,
                     struct fuse_file_info *fi);
-#endif
 };

Are we able to just remove this ifdef? Won't this break compilation on old systems that don't recognize "struct statx"?

Thanks,
Joanne

#59 lore raw

Re: [PATCH 1/1] libfuse: don't put HAVE_STATX in a public header

Quoted 30 lines · Joanne Koong
Quoted 29 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

fuse.h and fuse_lowlevel.h are public headers, don't expose internal build system config variables to downstream clients. This can also lead to function pointer ordering issues if (say) libfuse gets built with HAVE_STATX but the client program doesn't define a HAVE_STATX.

Get rid of the conditionals in the public header files to fix this.

---
 include/fuse.h           |    2 --
 include/fuse_lowlevel.h  |    2 --
 example/memfs_ll.cc      |    2 +-
 example/passthrough.c    |    2 +-
 example/passthrough_fh.c |    2 +-
 example/passthrough_ll.c |    2 +-
 6 files changed, 4 insertions(+), 8 deletions(-)
include/fuse.h +0 -0
index 06feacb070fbfb..209102651e9454 100644
@@ -854,7 +854,6 @@ struct fuse_operations {
         */
        off_t (*lseek) (const char *, off_t off, int whence, struct fuse_file_info *);
 
Quoted 13 lines

include/fuse.h struct fuse_operations { · line 867

Quoted 10 lines
include/fuse.h +0 -2
-#ifdef HAVE_STATX
        /**
         * Get extended file attributes.
         *
@@ -865,7 +864,6 @@ struct fuse_operations {
         */
        int (*statx)(const char *path, int flags, int mask, struct statx *stxbuf,
                     struct fuse_file_info *fi);
-#endif
 };

Are we able to just remove this ifdef? Won't this break compilation on old systems that don't recognize "struct statx"?

Yeah, you had added forward declaration actually. Slipped through in my review that we don't need the HAVE_STATX anymore.

We can also extend the patch a bit to remove HAVE_STATX from the public config.
Another alternative for this patch would be to replace HAVE_STATX by HAVE_FUSE_STATX.
The commit message is also not entirely right, as it says

Quoted 2 lines

to function pointer ordering issues if (say) libfuse gets built with HAVE_STATX but the client program doesn't define a HAVE_STATX.

Actually not, because /usr/include/fuse3/libfuse_config.h defines HAVE_STATX. I'm more worried that there might be a conflict of HAVE_STATX from libfuse with HAVE_STATX from the application.

Thanks,
Bernd

#60 lore raw

Re: [PATCH 1/1] libfuse: don't put HAVE_STATX in a public header

Quoted 45 lines · Bernd Schubert
Quoted 30 lines · Joanne Koong
Quoted 29 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

fuse.h and fuse_lowlevel.h are public headers, don't expose internal build system config variables to downstream clients. This can also lead to function pointer ordering issues if (say) libfuse gets built with HAVE_STATX but the client program doesn't define a HAVE_STATX.

Get rid of the conditionals in the public header files to fix this.

---
 include/fuse.h           |    2 --
 include/fuse_lowlevel.h  |    2 --
 example/memfs_ll.cc      |    2 +-
 example/passthrough.c    |    2 +-
 example/passthrough_fh.c |    2 +-
 example/passthrough_ll.c |    2 +-
 6 files changed, 4 insertions(+), 8 deletions(-)
include/fuse.h +0 -0
index 06feacb070fbfb..209102651e9454 100644
@@ -854,7 +854,6 @@ struct fuse_operations {
         */
        off_t (*lseek) (const char *, off_t off, int whence, struct fuse_file_info *);
 
Quoted 13 lines

include/fuse.h struct fuse_operations { · line 867

Quoted 10 lines
include/fuse.h +0 -2
-#ifdef HAVE_STATX
        /**
         * Get extended file attributes.
         *
@@ -865,7 +864,6 @@ struct fuse_operations {
         */
        int (*statx)(const char *path, int flags, int mask, struct statx *stxbuf,
                     struct fuse_file_info *fi);
-#endif
 };

Are we able to just remove this ifdef? Won't this break compilation on old systems that don't recognize "struct statx"?

Quoted 8 lines

Yeah, you had added forward declaration actually. Slipped through in my review that we don't need the HAVE_STATX anymore.

We can also extend the patch a bit to remove HAVE_STATX from the public config.
Another alternative for this patch would be to replace HAVE_STATX by HAVE_FUSE_STATX.
The commit message is also not entirely right, as it says

<shrug> libfuse itself doesn't define a struct statx, so what does it have, aside from the incomplete struct declaration? TBH I wonder what will happen when struct statx grows, but everybody gets to deal with that problem because we didn't explicitly encode the size in either the syscall or the struct definition.

Presumably fuse servers will detect and set their own HAVE_STATX, and only supply a ->statx function if they HAVE_STATX. They don't have to know if libfuse itself got built with statx support; if it didn't, then nothing will ever call ->statx, afaict.

Quoted 6 lines
Quoted 2 lines

to function pointer ordering issues if (say) libfuse gets built with HAVE_STATX but the client program doesn't define a HAVE_STATX.

Actually not, because /usr/include/fuse3/libfuse_config.h defines HAVE_STATX. I'm more worried that there might be a conflict of HAVE_STATX from libfuse with HAVE_STATX from the application.

<nod>

--D

Quoted 3 lines

Thanks,
Bernd

#61 lore raw

RFC PATCH v4 2/4 fuse: allow servers to use iomap for better file IO performance

In reply to Darrick J. Wong

Hi all,

This series connects fuse (the userspace filesystem layer) to fs-iomap to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap
---
Commits in this patchset:
* fuse: move CREATE_TRACE_POINTS to a separate file
* fuse: implement the basic iomap mechanisms
* fuse: make debugging configurable at runtime
* fuse: move the backing file idr and code into a new source file
* fuse: move the passthrough-specific code back to passthrough.c
* fuse: add an ioctl to add new iomap devices
* fuse: flush events and send FUSE_SYNCFS and FUSE_DESTROY on unmount
* fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}
* fuse: implement direct IO with iomap
* fuse: implement buffered IO with iomap
* fuse: enable caching of timestamps
* fuse: implement large folios for iomap pagecache files
* fuse: use an unrestricted backing device with iomap pagecache io
* fuse: advertise support for iomap
* fuse: query filesystem geometry when using iomap
* fuse: implement fadvise for iomap files
* fuse: make the root nodeid dynamic
* fuse: allow setting of root nodeid
* fuse: invalidate ranges of block devices being used for iomap
* fuse: implement inline data file IO via iomap
* fuse: allow more statx fields
* fuse: support atomic writes with iomap
* fuse: enable iomap

---
 fs/fuse/fuse_i.h          |  249 +++++
 fs/fuse/fuse_trace.h      |  996 +++++++++++++++++++++
 fs/fuse/iomap_priv.h      |   52 +
 include/uapi/linux/fuse.h |  195 ++++
 fs/fuse/Kconfig           |   45 +
 fs/fuse/Makefile          |    5 
 fs/fuse/backing.c         |  237 +++++
 fs/fuse/dev.c             |   35 +
 fs/fuse/dir.c             |  117 ++
 fs/fuse/file.c            |  133 ++-
 fs/fuse/file_iomap.c      | 2183 +++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/inode.c           |   92 +-
 fs/fuse/passthrough.c     |  199 +---
 fs/fuse/readdir.c         |   10 
 fs/fuse/trace.c           |   15 
 15 files changed, 4316 insertions(+), 247 deletions(-)
 create mode 100644 fs/fuse/iomap_priv.h
 create mode 100644 fs/fuse/backing.c
 create mode 100644 fs/fuse/file_iomap.c
 create mode 100644 fs/fuse/trace.c
#62 lore raw

PATCH 1/23 fuse: move CREATE_TRACE_POINTS to a separate file

From: Darrick J. Wong <[email protected]>

Before we start adding new tracepoints for fuse+iomap, move the tracepoint creation itself to a separate source file so that we don't have to start pulling iomap dependencies into dev.c just for the iomap structures.

---
 fs/fuse/Makefile |    3 ++-
 fs/fuse/dev.c    |    1 -
 fs/fuse/trace.c  |   13 +++++++++++++
 3 files changed, 15 insertions(+), 2 deletions(-)
 create mode 100644 fs/fuse/trace.c
fs/fuse/Makefile +2 -1
index 3f0f312a31c1cc..f3a273131a6cd1 100644
@@ -10,7 +10,8 @@ obj-$(CONFIG_FUSE_FS) += fuse.o
 obj-$(CONFIG_CUSE) += cuse.o
 obj-$(CONFIG_VIRTIO_FS) += virtiofs.o
 
-fuse-y := dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o
+fuse-y := trace.o	# put trace.o first so we see ftrace errors sooner
+fuse-y += dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o
 fuse-y += iomode.o
 fuse-$(CONFIG_FUSE_DAX) += dax.o
 fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o
fs/fuse/dev.c +0 -1
index 05d6e7779387a4..dbde17fff0cda9 100644
@@ -26,7 +26,6 @@
 #include <linux/seq_file.h>
 #include <linux/nmi.h>
 
-#define CREATE_TRACE_POINTS
 #include "fuse_trace.h"
 
 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
fs/fuse/trace.c new file +13 -0
new file mode 100644
index 00000000000000..93bd72efc98cd0
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2025 Oracle.  All Rights Reserved.
+ * Author: Darrick J. Wong <[email protected]>
+ */
+#include "dev_uring_i.h"
+#include "fuse_i.h"
+#include "fuse_dev_i.h"
+
+#include <linux/pagemap.h>
+
+#define CREATE_TRACE_POINTS
+#include "fuse_trace.h"
#63 lore raw

PATCH 2/23 fuse: implement the basic iomap mechanisms

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Implement functions to enable upcalling of iomap_begin and iomap_end to userspace fuse servers.

---
 fs/fuse/fuse_i.h          |   35 ++++
 fs/fuse/fuse_trace.h      |  295 ++++++++++++++++++++++++++++++
 fs/fuse/iomap_priv.h      |   42 ++++
 include/uapi/linux/fuse.h |   92 +++++++++
 fs/fuse/Kconfig           |   25 +++
 fs/fuse/Makefile          |    1 
 fs/fuse/file_iomap.c      |  444 +++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/inode.c           |    5 +
 8 files changed, 938 insertions(+), 1 deletion(-)
 create mode 100644 fs/fuse/iomap_priv.h
 create mode 100644 fs/fuse/file_iomap.c
fs/fuse/fuse_i.h +35 -0
index b80505f5431e0b..b28054c254f866 100644
@@ -899,6 +899,9 @@ struct fuse_conn {
 	/* Is link not implemented by fs? */
 	unsigned int no_link:1;
 
+	/* Use fs/iomap for FIEMAP and SEEK_{DATA,HOLE} file operations */
+	unsigned int iomap:1;
+
 	/* Use io_uring for communication */
 	unsigned int io_uring;
 
@@ -1015,6 +1018,11 @@ static inline struct fuse_mount *get_fuse_mount_super(struct super_block *sb)
 	return sb->s_fs_info;
 }
 
+static inline const struct fuse_mount *get_fuse_mount_super_c(const struct super_block *sb)
+{
+	return sb->s_fs_info;
+}
+
 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
 {
 	return get_fuse_mount_super(sb)->fc;
@@ -1025,16 +1033,31 @@ static inline struct fuse_mount *get_fuse_mount(struct inode *inode)
 	return get_fuse_mount_super(inode->i_sb);
 }
 
+static inline const struct fuse_mount *get_fuse_mount_c(const struct inode *inode)
+{
+	return get_fuse_mount_super_c(inode->i_sb);
+}
+
 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
 {
 	return get_fuse_mount_super(inode->i_sb)->fc;
 }
 
+static inline const struct fuse_conn *get_fuse_conn_c(const struct inode *inode)
+{
+	return get_fuse_mount_super_c(inode->i_sb)->fc;
+}
+
 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
 {
 	return container_of(inode, struct fuse_inode, inode);
 }
 
+static inline const struct fuse_inode *get_fuse_inode_c(const struct inode *inode)
+{
+	return container_of(inode, struct fuse_inode, inode);
+}
+
 static inline u64 get_node_id(struct inode *inode)
 {
 	return get_fuse_inode(inode)->nodeid;
@@ -1584,4 +1607,16 @@ extern void fuse_sysctl_unregister(void);
 #define fuse_sysctl_unregister()	do { } while (0)
 #endif /* CONFIG_SYSCTL */
 
+#if IS_ENABLED(CONFIG_FUSE_IOMAP)
+bool fuse_iomap_enabled(void);
+
+static inline bool fuse_has_iomap(const struct inode *inode)
+{
+	return get_fuse_conn_c(inode)->iomap;
+}
+#else
+# define fuse_iomap_enabled(...)		(false)
+# define fuse_has_iomap(...)			(false)
+#endif
+
 #endif /* _FS_FUSE_I_H */
fs/fuse/fuse_trace.h +295 -0

313 lines not on this page · raw message

fs/fuse/iomap_priv.h new file +42 -0

42 lines not on this page · raw message

include/uapi/linux/fuse.h +91 -1

126 lines not on this page · raw message

fs/fuse/Kconfig +25 -0
index a774166264de69..e0bcbd42431344 100644
@@ -65,6 +65,31 @@ config FUSE_PASSTHROUGH
 
 	  If you want to allow passthrough operations, answer Y.
 
+config FUSE_IOMAP
+	bool "FUSE file IO over iomap"
+	default y
+	depends on FUSE_FS
+	depends on BLOCK
+	select FS_IOMAP
+	help
+	  For supported fuseblk servers, this allows the file IO path to run
+	  through the kernel.
+
+config FUSE_IOMAP_BY_DEFAULT
+	bool "FUSE file I/O over iomap by default"
+	default n
+	depends on FUSE_IOMAP
+	help
+	  Enable sending FUSE file I/O over iomap by default.
+
+config FUSE_IOMAP_DEBUG
+	bool "Debug FUSE file IO over iomap"
+	default n
+	depends on FUSE_IOMAP
+	help
+	  Enable debugging assertions for the fuse iomap code paths and logging
+	  of bad iomap file mapping data being sent to the kernel.
+
 config FUSE_IO_URING
 	bool "FUSE communication over io-uring"
 	default y
fs/fuse/Makefile +1 -0
index f3a273131a6cd1..70709a7a3f9523 100644
@@ -17,5 +17,6 @@ fuse-$(CONFIG_FUSE_DAX) += dax.o
 fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o
 fuse-$(CONFIG_SYSCTL) += sysctl.o
 fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
+fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o
 
 virtiofs-y := virtio_fs.o
fs/fuse/file_iomap.c new file +444 -0

444 lines not on this page · raw message

fs/fuse/inode.c +5 -0
index b05510799f93e1..82e074642e8e9b 100644
@@ -1446,6 +1446,9 @@ static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
 
 			if (flags & FUSE_REQUEST_TIMEOUT)
 				timeout = arg->request_timeout;
+
+			if ((flags & FUSE_IOMAP) && fuse_iomap_enabled())
+				fc->iomap = 1;
 		} else {
 			ra_pages = fc->max_read / PAGE_SIZE;
 			fc->no_lock = 1;
@@ -1514,6 +1517,8 @@ void fuse_send_init(struct fuse_mount *fm)
 	 */
 	if (fuse_uring_enabled())
 		flags |= FUSE_OVER_IO_URING;
+	if (fuse_iomap_enabled())
+		flags |= FUSE_IOMAP;
 
 	ia->in.flags = flags;
 	ia->in.flags2 = flags >> 32;
maintainer
#64 lore raw

Re: [PATCH 02/23] fuse: implement the basic iomap mechanisms

fs/fuse/fuse_trace.h

Quoted 5 lines · Darrick J. Wong

#endif /* _FS_FUSE_I_H */

fs/fuse/fuse_trace.h +0 -0
index bbe9ddd8c71696..2389072b734636 100644

One general thing I'd like to ask is that debugging and tracing be as much separated from actual functionality as possible. Not just on the source tree level but on the patch or even patchset level, please.

Thanks,
Miklos

#65 lore raw

Re: [PATCH 02/23] fuse: implement the basic iomap mechanisms

Quoted 11 lines · Miklos Szeredi

fs/fuse/fuse_trace.h

Quoted 5 lines · Darrick J. Wong

#endif /* _FS_FUSE_I_H */

fs/fuse/fuse_trace.h +0 -0
index bbe9ddd8c71696..2389072b734636 100644

One general thing I'd like to ask is that debugging and tracing be as much separated from actual functionality as possible. Not just on the source tree level but on the patch or even patchset level, please.

How would that work in practice for this patch? One way I can think of to break up this patch would be:

Patch 1 introduces all the #defines that later get used by the #define FOO_STRINGS that get passed to __print_symbolic/__print_flags macros.

Patch 2 defines the tracepoints and ASSERT/BAD_DATA

Patch 3 adds all the new code that actually uses ASSERT/BAD_DATA and calls trace_fuse_iomap_XXX()

...or would you prefer patch 3 to be code only, and a patch 4 would dump in all the assertions and tracepoint callsites separately?

For subsequent patches that add new iomap flags (and hence extend FOO_STRINGS) and tracepoints, should those also go in a separate pre-patch ahead of the actual code using it?

Or do you prefer the first N patches to include only code and no debugging stuff at all, with a megapatch at the end to add all that instrumentation?

(Or would you rather I sorted the diff chunks to put fuse_trace.h at the very end of each patch, because AFAICT reviewers rarely spend much time looking at the tracepoints and at least then you don't have to scroll through that boilerplate to get to the real code...)

Yeah, I know, the ftrace macros are painful to look at and wish there was a better way to do that.

--D

Quoted 3 lines

Thanks,
Miklos

maintainer
#66 lore raw

Re: [PATCH 02/23] fuse: implement the basic iomap mechanisms

Quoted 3 lines · Darrick J. Wong

Or do you prefer the first N patches to include only code and no debugging stuff at all, with a megapatch at the end to add all that instrumentation?

It doesn't have to be a megapatch, could be a nicely broken up series. But separate from the main patchset, if possible.

Thanks,
Miklos

#67 lore raw

Re: [PATCH 02/23] fuse: implement the basic iomap mechanisms

Quoted 8 lines · Miklos Szeredi
Quoted 3 lines · Darrick J. Wong

Or do you prefer the first N patches to include only code and no debugging stuff at all, with a megapatch at the end to add all that instrumentation?

It doesn't have to be a megapatch, could be a nicely broken up series. But separate from the main patchset, if possible.

I'll give it a try.

--D

Quoted 3 lines

Thanks,
Miklos

#68 lore raw

Re: [PATCH 02/23] fuse: implement the basic iomap mechanisms

Quoted 11 lines · Darrick J. Wong
Quoted 8 lines · Miklos Szeredi
Quoted 3 lines · Darrick J. Wong

Or do you prefer the first N patches to include only code and no debugging stuff at all, with a megapatch at the end to add all that instrumentation?

It doesn't have to be a megapatch, could be a nicely broken up series. But separate from the main patchset, if possible.

I'll give it a try.

Hi Miklos!

I gave it a try for V5 and the results were ... not fun.

I. Moving all the tracepoint definitions to come before the beginning of code changes breaks compilation in three critical ways:

(a) The tracepoints take as arguments pointers to structs that only get defined later in the patchset.

(b) Tracepoints that extract data from those structs cannot do so because (obviously) they are not actually defined yet.

(b) The flag constants to strings decoding mechanism requires those constants to be defined.

II. I fixed /that/ by moving the tracepoint definitions to come after the all code change, but that brings its own problems:

(1) Now I have to move all the callsites from the code patches to this new patch at the end.

(2) I've found that oftentimes, if I need to change one of the code patches to fix a bug, there will be a merge conflict in the tracepoint patch at the end because some whitespace moved, etc. That's annoying to have to fix up every time I make a change, the suggested conflict resolution puts the trace_() call in completely the wrong place because well, textually it looked right.

(3) If I change a structure in a code patch, I'm not going to find out if it breaks the tracepoint until I get to that megapatch. In the good case I can just fix the tracepoint, but in the bad case I have to go all the way back to the code patch to fix that, then rebase everything back to the tracepoint patch. Right now, that kind of breakage is immediately obvious when building each code patch.

III. A middle ground that I devised is to have a patch N that adds code, and patch N+1 adds the tracepoints. That avoids most of the problems of the first two approaches, but now there are twice as many patches, e.g.:

* fuse: implement direct IO with iomap
* fuse: add tracepoints for implement direct IO with iomap

Coming from XFS, I encountered a lot of friction from some reviewers for putting too many things in a single patch, and from other reviewers for sending too many patches once I split things up. I'd like to avoid that here.

So I think the third approach is the way to go, but I'd like your input before I commit to a particular approach. Just to be clear, I'm trying to avoid landing any of this in 6.18 (aka presumed 2025 LTS), so there's plenty of time.

--D

maintainer
#69 lore raw

Re: [PATCH 02/23] fuse: implement the basic iomap mechanisms

Quoted 2 lines · Darrick J. Wong

So I think the third approach is the way to go, but I'd like your input before I commit to a particular approach.

Okay.

I'm not going to rant against tracepoints, because I'm sure you have a good reason to bear the pain of dealing with them. I'd just like to minimise the pain for myself and other reviewers. If I can just ignore clearly marked tracing patches, that seems good enough.

Thanks,
Miklos

#70 lore raw

Re: [PATCH 02/23] fuse: implement the basic iomap mechanisms

Quoted 11 lines · Miklos Szeredi
Quoted 2 lines · Darrick J. Wong

So I think the third approach is the way to go, but I'd like your input before I commit to a particular approach.

Okay.

I'm not going to rant against tracepoints, because I'm sure you have a good reason to bear the pain of dealing with them. I'd just like to minimise the pain for myself and other reviewers. If I can just ignore clearly marked tracing patches, that seems good enough.

Ah, ok. Knowing your motivation helps me a lot, thanks for letting me know. I'll keep the tracing code as separate as possible, then.

<begin rant>
FYI: I don't like the way tracepoints are composed either. Nested C preprocessor macros are both too clever and require too much cleverness for my taste, but I'm addicted to tracing data flows, so I'm stuck with their current pain until someone makes it as easy as:

trace_fuse_iomap_begin(const struct inode *inode, loff_t pos,

		       loff_t count, const struct iomap *iomap) =>
	"{inode->ino} {pos} {count} {iomap->offset}..."

and have it generate the necessary data structures and trace printk strings from that. But you can only do such things if macros can perform AST transformations in the compiler, and /that/ wasn't around in 1969. :(
<end rant>

--D

Quoted 2 lines

Thanks,
Miklos

#71 lore raw

PATCH 3/23 fuse: make debugging configurable at runtime

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Use static keys so that we can configure debugging assertions and dmesg warnings at runtime. By default this is turned off so the cost is merely scanning a nop sled. However, fuse server developers can turn it on for their debugging systems.

---
 fs/fuse/fuse_i.h     |    8 +++++
 fs/fuse/iomap_priv.h |   16 ++++++++--
 fs/fuse/Kconfig      |   15 +++++++++
 fs/fuse/file_iomap.c |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/inode.c      |    7 ++++
 5 files changed, 124 insertions(+), 3 deletions(-)
fs/fuse/fuse_i.h +8 -0
index b28054c254f866..2cd9f4cdc6a7ef 100644
@@ -1607,6 +1607,14 @@ extern void fuse_sysctl_unregister(void);
 #define fuse_sysctl_unregister()	do { } while (0)
 #endif /* CONFIG_SYSCTL */
 
+#if IS_ENABLED(CONFIG_FUSE_IOMAP_DEBUG)
+int fuse_iomap_sysfs_init(struct kobject *kobj);
+void fuse_iomap_sysfs_cleanup(struct kobject *kobj);
+#else
+# define fuse_iomap_sysfs_init(...)		(0)
+# define fuse_iomap_sysfs_cleanup(...)		((void)0)
+#endif
+
 #if IS_ENABLED(CONFIG_FUSE_IOMAP)
 bool fuse_iomap_enabled(void);
 
fs/fuse/iomap_priv.h +13 -3
index ca8544a95a4267..7002eb38f87fe1 100644
@@ -6,19 +6,29 @@
 #ifndef _FS_FUSE_IOMAP_PRIV_H
 #define _FS_FUSE_IOMAP_PRIV_H
 
+#if IS_ENABLED(CONFIG_FUSE_IOMAP_DEBUG_DEFAULT)
+DECLARE_STATIC_KEY_TRUE(fuse_iomap_debug);
+#else
+DECLARE_STATIC_KEY_FALSE(fuse_iomap_debug);
+#endif
+
 #if IS_ENABLED(CONFIG_FUSE_IOMAP)
 #if IS_ENABLED(CONFIG_FUSE_IOMAP_DEBUG)
-# define ASSERT(condition) do {						\
+# define ASSERT(condition) \
+while (static_branch_unlikely(&fuse_iomap_debug)) {			\
 	int __cond = !!(condition);					\
 	if (unlikely(!__cond))						\
 		trace_fuse_iomap_assert(__func__, __LINE__, #condition); \
 	WARN(!__cond, "Assertion failed: %s, func: %s, line: %d", #condition, __func__, __LINE__); \
-} while (0)
+	break;								\
+}
 # define BAD_DATA(condition) ({						\
 	int __cond = !!(condition);					\
 	if (unlikely(__cond))						\
 		trace_fuse_iomap_bad_data(__func__, __LINE__, #condition); \
-	WARN(__cond, "Bad mapping: %s, func: %s, line: %d", #condition, __func__, __LINE__); \
+	if (static_branch_unlikely(&fuse_iomap_debug))			\
+		WARN(__cond, "Bad mapping: %s, func: %s, line: %d", #condition, __func__, __LINE__); \
+	unlikely(__cond);								\
 })
 #else
 # define ASSERT(condition)
fs/fuse/Kconfig +15 -0
index e0bcbd42431344..6be74396ef5198 100644
@@ -90,6 +90,21 @@ config FUSE_IOMAP_DEBUG
 	  Enable debugging assertions for the fuse iomap code paths and logging
 	  of bad iomap file mapping data being sent to the kernel.
 
+	  Say N here if you don't want any debugging code code compiled in at
+	  all.
+
+config FUSE_IOMAP_DEBUG_BY_DEFAULT
+	bool "Debug FUSE file IO over iomap at boot time"
+	default n
+	depends on FUSE_IOMAP_DEBUG
+	help
+	  At boot time, enable debugging assertions for the fuse iomap code
+	  paths and warnings about bad iomap file mapping data.  This enables
+	  fuse server authors to control debugging at runtime even on a
+	  distribution kernel while avoiding most of the overhead on production
+	  systems.  The setting can be changed at runtime via
+	  /sys/fs/fuse/iomap/debug.
+
 config FUSE_IO_URING
 	bool "FUSE communication over io-uring"
 	default y
fs/fuse/file_iomap.c +81 -0

93 lines not on this page · raw message

fs/fuse/inode.c +7 -0
index 82e074642e8e9b..9448a11c828fef 100644
@@ -2217,8 +2217,14 @@ static int fuse_sysfs_init(void)
 	if (err)
 		goto out_fuse_unregister;
 
+	err = fuse_iomap_sysfs_init(fuse_kobj);
+	if (err)
+		goto out_fuse_connections;
+
 	return 0;
 
+ out_fuse_connections:
+	sysfs_remove_mount_point(fuse_kobj, "connections");
  out_fuse_unregister:
 	kobject_put(fuse_kobj);
  out_err:
@@ -2227,6 +2233,7 @@ static int fuse_sysfs_init(void)
 
 static void fuse_sysfs_cleanup(void)
 {
+	fuse_iomap_sysfs_cleanup(fuse_kobj);
 	sysfs_remove_mount_point(fuse_kobj, "connections");
 	kobject_put(fuse_kobj);
 }
#72 lore raw

PATCH 4/23 fuse: move the backing file idr and code into a new source file

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

iomap support for fuse is also going to want the ability to attach backing files to a fuse filesystem. Move the fuse_backing code into a separate file so that both can use it.

---
 fs/fuse/fuse_i.h      |   47 ++++++++-----
 fs/fuse/Makefile      |    2 -
 fs/fuse/backing.c     |  174 +++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/passthrough.c |  158 --------------------------------------------
 4 files changed, 203 insertions(+), 178 deletions(-)
 create mode 100644 fs/fuse/backing.c
fs/fuse/fuse_i.h +27 -20
index 2cd9f4cdc6a7ef..2be2cbdf060536 100644
@@ -1535,29 +1535,11 @@ struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
 		       unsigned int open_flags, fl_owner_t id, bool isdir);
 
-/* passthrough.c */
-static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi)
-{
-#ifdef CONFIG_FUSE_PASSTHROUGH
-	return READ_ONCE(fi->fb);
-#else
-	return NULL;
-#endif
-}
-
-static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi,
-							  struct fuse_backing *fb)
-{
-#ifdef CONFIG_FUSE_PASSTHROUGH
-	return xchg(&fi->fb, fb);
-#else
-	return NULL;
-#endif
-}
-
+/* backing.c */
 #ifdef CONFIG_FUSE_PASSTHROUGH
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb);
 void fuse_backing_put(struct fuse_backing *fb);
+struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id);
 #else
 
 static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
@@ -1568,6 +1550,11 @@ static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 static inline void fuse_backing_put(struct fuse_backing *fb)
 {
 }
+static inline struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc,
+						       int backing_id)
+{
+	return NULL;
+}
 #endif
 
 void fuse_backing_files_init(struct fuse_conn *fc);
@@ -1575,6 +1562,26 @@ void fuse_backing_files_free(struct fuse_conn *fc);
 int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map);
 int fuse_backing_close(struct fuse_conn *fc, int backing_id);
 
+/* passthrough.c */
+static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi)
+{
+#ifdef CONFIG_FUSE_PASSTHROUGH
+	return READ_ONCE(fi->fb);
+#else
+	return NULL;
+#endif
+}
+
+static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi,
+							  struct fuse_backing *fb)
+{
+#ifdef CONFIG_FUSE_PASSTHROUGH
+	return xchg(&fi->fb, fb);
+#else
+	return NULL;
+#endif
+}
+
 struct fuse_backing *fuse_passthrough_open(struct file *file,
 					   struct inode *inode,
 					   int backing_id);
fs/fuse/Makefile +1 -1
index 70709a7a3f9523..c79f786d0c90c3 100644
@@ -14,7 +14,7 @@ fuse-y := trace.o	# put trace.o first so we see ftrace errors sooner
 fuse-y += dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o
 fuse-y += iomode.o
 fuse-$(CONFIG_FUSE_DAX) += dax.o
-fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o
+fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o backing.o
 fuse-$(CONFIG_SYSCTL) += sysctl.o
 fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
 fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o
fs/fuse/backing.c new file +174 -0

174 lines not on this page · raw message

fs/fuse/passthrough.c +1 -157

171 lines not on this page · raw message

maintainer Reviewed-by on v1 4/23
#73 lore raw

Re: [PATCH 04/23] fuse: move the backing file idr and code into a new source file

Quoted 8 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

iomap support for fuse is also going to want the ability to attach backing files to a fuse filesystem. Move the fuse_backing code into a separate file so that both can use it.

Are you going to make FUSE_IOMAP depend on FUSE_PASSTHROUGH later on? I can't think of a reason why not.

Thanks,
Amir.

Quoted 456 lines
---
 fs/fuse/fuse_i.h      |   47 ++++++++-----
 fs/fuse/Makefile      |    2 -
 fs/fuse/backing.c     |  174 +++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/passthrough.c |  158 --------------------------------------------
 4 files changed, 203 insertions(+), 178 deletions(-)
 create mode 100644 fs/fuse/backing.c
fs/fuse/fuse_i.h +27 -20
index 2cd9f4cdc6a7ef..2be2cbdf060536 100644
@@ -1535,29 +1535,11 @@ struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
                       unsigned int open_flags, fl_owner_t id, bool isdir);
 
-/* passthrough.c */
-static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi)
-{
-#ifdef CONFIG_FUSE_PASSTHROUGH
-       return READ_ONCE(fi->fb);
-#else
-       return NULL;
-#endif
-}
-
-static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi,
-                                                         struct fuse_backing *fb)
-{
-#ifdef CONFIG_FUSE_PASSTHROUGH
-       return xchg(&fi->fb, fb);
-#else
-       return NULL;
-#endif
-}
-
+/* backing.c */
 #ifdef CONFIG_FUSE_PASSTHROUGH
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb);
 void fuse_backing_put(struct fuse_backing *fb);
+struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id);
 #else
 
 static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
@@ -1568,6 +1550,11 @@ static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 static inline void fuse_backing_put(struct fuse_backing *fb)
 {
 }
+static inline struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc,
+                                                      int backing_id)
+{
+       return NULL;
+}
 #endif
 
 void fuse_backing_files_init(struct fuse_conn *fc);
@@ -1575,6 +1562,26 @@ void fuse_backing_files_free(struct fuse_conn *fc);
 int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map);
 int fuse_backing_close(struct fuse_conn *fc, int backing_id);
 
+/* passthrough.c */
+static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi)
+{
+#ifdef CONFIG_FUSE_PASSTHROUGH
+       return READ_ONCE(fi->fb);
+#else
+       return NULL;
+#endif
+}
+
+static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi,
+                                                         struct fuse_backing *fb)
+{
+#ifdef CONFIG_FUSE_PASSTHROUGH
+       return xchg(&fi->fb, fb);
+#else
+       return NULL;
+#endif
+}
+
 struct fuse_backing *fuse_passthrough_open(struct file *file,
                                           struct inode *inode,
                                           int backing_id);
fs/fuse/Makefile +1 -1
index 70709a7a3f9523..c79f786d0c90c3 100644
@@ -14,7 +14,7 @@ fuse-y := trace.o     # put trace.o first so we see ftrace errors sooner
 fuse-y += dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o
 fuse-y += iomode.o
 fuse-$(CONFIG_FUSE_DAX) += dax.o
-fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o
+fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o backing.o
 fuse-$(CONFIG_SYSCTL) += sysctl.o
 fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
 fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o
fs/fuse/backing.c new file +174 -0
new file mode 100644
index 00000000000000..ddb23b7400fc72
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * FUSE passthrough to backing file.
+ *
+ * Copyright (c) 2023 CTERA Networks.
+ */
+
+#include "fuse_i.h"
+
+#include <linux/file.h>
+
+struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
+{
+       if (fb && refcount_inc_not_zero(&fb->count))
+               return fb;
+       return NULL;
+}
+
+static void fuse_backing_free(struct fuse_backing *fb)
+{
+       pr_debug("%s: fb=0x%p\n", __func__, fb);
+
+       if (fb->file)
+               fput(fb->file);
+       put_cred(fb->cred);
+       kfree_rcu(fb, rcu);
+}
+
+void fuse_backing_put(struct fuse_backing *fb)
+{
+       if (fb && refcount_dec_and_test(&fb->count))
+               fuse_backing_free(fb);
+}
+
+void fuse_backing_files_init(struct fuse_conn *fc)
+{
+       idr_init(&fc->backing_files_map);
+}
+
+static int fuse_backing_id_alloc(struct fuse_conn *fc, struct fuse_backing *fb)
+{
+       int id;
+
+       idr_preload(GFP_KERNEL);
+       spin_lock(&fc->lock);
+       /* FIXME: xarray might be space inefficient */
+       id = idr_alloc_cyclic(&fc->backing_files_map, fb, 1, 0, GFP_ATOMIC);
+       spin_unlock(&fc->lock);
+       idr_preload_end();
+
+       WARN_ON_ONCE(id == 0);
+       return id;
+}
+
+static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
+                                                  int id)
+{
+       struct fuse_backing *fb;
+
+       spin_lock(&fc->lock);
+       fb = idr_remove(&fc->backing_files_map, id);
+       spin_unlock(&fc->lock);
+
+       return fb;
+}
+
+static int fuse_backing_id_free(int id, void *p, void *data)
+{
+       struct fuse_backing *fb = p;
+
+       WARN_ON_ONCE(refcount_read(&fb->count) != 1);
+       fuse_backing_free(fb);
+       return 0;
+}
+
+void fuse_backing_files_free(struct fuse_conn *fc)
+{
+       idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
+       idr_destroy(&fc->backing_files_map);
+}
+
+int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
+{
+       struct file *file;
+       struct super_block *backing_sb;
+       struct fuse_backing *fb = NULL;
+       int res;
+
+       pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
+
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+       res = -EPERM;
+       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
+               goto out;
+
+       res = -EINVAL;
+       if (map->flags || map->padding)
+               goto out;
+
+       file = fget_raw(map->fd);
+       res = -EBADF;
+       if (!file)
+               goto out;
+
+       backing_sb = file_inode(file)->i_sb;
+       res = -ELOOP;
+       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
+               goto out_fput;
+
+       fb = kmalloc(sizeof(struct fuse_backing), GFP_KERNEL);
+       res = -ENOMEM;
+       if (!fb)
+               goto out_fput;
+
+       fb->file = file;
+       fb->cred = prepare_creds();
+       refcount_set(&fb->count, 1);
+
+       res = fuse_backing_id_alloc(fc, fb);
+       if (res < 0) {
+               fuse_backing_free(fb);
+               fb = NULL;
+       }
+
+out:
+       pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
+
+       return res;
+
+out_fput:
+       fput(file);
+       goto out;
+}
+
+int fuse_backing_close(struct fuse_conn *fc, int backing_id)
+{
+       struct fuse_backing *fb = NULL;
+       int err;
+
+       pr_debug("%s: backing_id=%d\n", __func__, backing_id);
+
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+       err = -EPERM;
+       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
+               goto out;
+
+       err = -EINVAL;
+       if (backing_id <= 0)
+               goto out;
+
+       err = -ENOENT;
+       fb = fuse_backing_id_remove(fc, backing_id);
+       if (!fb)
+               goto out;
+
+       fuse_backing_put(fb);
+       err = 0;
+out:
+       pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err);
+
+       return err;
+}
+
+struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id)
+{
+       struct fuse_backing *fb;
+
+       rcu_read_lock();
+       fb = idr_find(&fc->backing_files_map, backing_id);
+       fb = fuse_backing_get(fb);
+       rcu_read_unlock();
+
+       return fb;
+}
fs/fuse/passthrough.c +1 -157
index 607ef735ad4ab3..e0b8d885bc81f3 100644
@@ -144,158 +144,6 @@ ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
        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))
-               return fb;
-       return NULL;
-}
-
-static void fuse_backing_free(struct fuse_backing *fb)
-{
-       pr_debug("%s: fb=0x%p\n", __func__, fb);
-
-       if (fb->file)
-               fput(fb->file);
-       put_cred(fb->cred);
-       kfree_rcu(fb, rcu);
-}
-
-void fuse_backing_put(struct fuse_backing *fb)
-{
-       if (fb && refcount_dec_and_test(&fb->count))
-               fuse_backing_free(fb);
-}
-
-void fuse_backing_files_init(struct fuse_conn *fc)
-{
-       idr_init(&fc->backing_files_map);
-}
-
-static int fuse_backing_id_alloc(struct fuse_conn *fc, struct fuse_backing *fb)
-{
-       int id;
-
-       idr_preload(GFP_KERNEL);
-       spin_lock(&fc->lock);
-       /* FIXME: xarray might be space inefficient */
-       id = idr_alloc_cyclic(&fc->backing_files_map, fb, 1, 0, GFP_ATOMIC);
-       spin_unlock(&fc->lock);
-       idr_preload_end();
-
-       WARN_ON_ONCE(id == 0);
-       return id;
-}
-
-static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
-                                                  int id)
-{
-       struct fuse_backing *fb;
-
-       spin_lock(&fc->lock);
-       fb = idr_remove(&fc->backing_files_map, id);
-       spin_unlock(&fc->lock);
-
-       return fb;
-}
-
-static int fuse_backing_id_free(int id, void *p, void *data)
-{
-       struct fuse_backing *fb = p;
-
-       WARN_ON_ONCE(refcount_read(&fb->count) != 1);
-       fuse_backing_free(fb);
-       return 0;
-}
-
-void fuse_backing_files_free(struct fuse_conn *fc)
-{
-       idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
-       idr_destroy(&fc->backing_files_map);
-}
-
-int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
-{
-       struct file *file;
-       struct super_block *backing_sb;
-       struct fuse_backing *fb = NULL;
-       int res;
-
-       pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
-
-       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
-       res = -EPERM;
-       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
-               goto out;
-
-       res = -EINVAL;
-       if (map->flags || map->padding)
-               goto out;
-
-       file = fget_raw(map->fd);
-       res = -EBADF;
-       if (!file)
-               goto out;
-
-       backing_sb = file_inode(file)->i_sb;
-       res = -ELOOP;
-       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
-               goto out_fput;
-
-       fb = kmalloc(sizeof(struct fuse_backing), GFP_KERNEL);
-       res = -ENOMEM;
-       if (!fb)
-               goto out_fput;
-
-       fb->file = file;
-       fb->cred = prepare_creds();
-       refcount_set(&fb->count, 1);
-
-       res = fuse_backing_id_alloc(fc, fb);
-       if (res < 0) {
-               fuse_backing_free(fb);
-               fb = NULL;
-       }
-
-out:
-       pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
-
-       return res;
-
-out_fput:
-       fput(file);
-       goto out;
-}
-
-int fuse_backing_close(struct fuse_conn *fc, int backing_id)
-{
-       struct fuse_backing *fb = NULL;
-       int err;
-
-       pr_debug("%s: backing_id=%d\n", __func__, backing_id);
-
-       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
-       err = -EPERM;
-       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
-               goto out;
-
-       err = -EINVAL;
-       if (backing_id <= 0)
-               goto out;
-
-       err = -ENOENT;
-       fb = fuse_backing_id_remove(fc, backing_id);
-       if (!fb)
-               goto out;
-
-       fuse_backing_put(fb);
-       err = 0;
-out:
-       pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err);
-
-       return err;
-}
-
 /*
  * Setup passthrough to a backing file.
  *
@@ -315,12 +163,8 @@ struct fuse_backing *fuse_passthrough_open(struct file *file,
        if (backing_id <= 0)
                goto out;
 
-       rcu_read_lock();
-       fb = idr_find(&fc->backing_files_map, backing_id);
-       fb = fuse_backing_get(fb);
-       rcu_read_unlock();
-
        err = -ENOENT;
+       fb = fuse_backing_lookup(fc, backing_id);
        if (!fb)
                goto out;
 
maintainer
#74 lore raw

Re: [PATCH 04/23] fuse: move the backing file idr and code into a new source file

Quoted 9 lines · Amir Goldstein
Quoted 7 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

iomap support for fuse is also going to want the ability to attach backing files to a fuse filesystem. Move the fuse_backing code into a separate file so that both can use it.

Quoted 5 lines
Quoted 1 line

Are you going to make FUSE_IOMAP depend on FUSE_PASSTHROUGH later on? I can't think of a reason why not.

Ah I see. They will both depend on FUSE_BACKING
cool

Quoted 460 lines

Thanks,
Amir.

Quoted 456 lines
---
 fs/fuse/fuse_i.h      |   47 ++++++++-----
 fs/fuse/Makefile      |    2 -
 fs/fuse/backing.c     |  174 +++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/passthrough.c |  158 --------------------------------------------
 4 files changed, 203 insertions(+), 178 deletions(-)
 create mode 100644 fs/fuse/backing.c
fs/fuse/fuse_i.h +27 -20
index 2cd9f4cdc6a7ef..2be2cbdf060536 100644
@@ -1535,29 +1535,11 @@ struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
                       unsigned int open_flags, fl_owner_t id, bool isdir);
 
-/* passthrough.c */
-static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi)
-{
-#ifdef CONFIG_FUSE_PASSTHROUGH
-       return READ_ONCE(fi->fb);
-#else
-       return NULL;
-#endif
-}
-
-static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi,
-                                                         struct fuse_backing *fb)
-{
-#ifdef CONFIG_FUSE_PASSTHROUGH
-       return xchg(&fi->fb, fb);
-#else
-       return NULL;
-#endif
-}
-
+/* backing.c */
 #ifdef CONFIG_FUSE_PASSTHROUGH
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb);
 void fuse_backing_put(struct fuse_backing *fb);
+struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id);
 #else
 
 static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
@@ -1568,6 +1550,11 @@ static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 static inline void fuse_backing_put(struct fuse_backing *fb)
 {
 }
+static inline struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc,
+                                                      int backing_id)
+{
+       return NULL;
+}
 #endif
 
 void fuse_backing_files_init(struct fuse_conn *fc);
@@ -1575,6 +1562,26 @@ void fuse_backing_files_free(struct fuse_conn *fc);
 int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map);
 int fuse_backing_close(struct fuse_conn *fc, int backing_id);
 
+/* passthrough.c */
+static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi)
+{
+#ifdef CONFIG_FUSE_PASSTHROUGH
+       return READ_ONCE(fi->fb);
+#else
+       return NULL;
+#endif
+}
+
+static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi,
+                                                         struct fuse_backing *fb)
+{
+#ifdef CONFIG_FUSE_PASSTHROUGH
+       return xchg(&fi->fb, fb);
+#else
+       return NULL;
+#endif
+}
+
 struct fuse_backing *fuse_passthrough_open(struct file *file,
                                           struct inode *inode,
                                           int backing_id);
fs/fuse/Makefile +1 -1
index 70709a7a3f9523..c79f786d0c90c3 100644
@@ -14,7 +14,7 @@ fuse-y := trace.o     # put trace.o first so we see ftrace errors sooner
 fuse-y += dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o
 fuse-y += iomode.o
 fuse-$(CONFIG_FUSE_DAX) += dax.o
-fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o
+fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o backing.o
 fuse-$(CONFIG_SYSCTL) += sysctl.o
 fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
 fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o
fs/fuse/backing.c new file +174 -0
new file mode 100644
index 00000000000000..ddb23b7400fc72
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * FUSE passthrough to backing file.
+ *
+ * Copyright (c) 2023 CTERA Networks.
+ */
+
+#include "fuse_i.h"
+
+#include <linux/file.h>
+
+struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
+{
+       if (fb && refcount_inc_not_zero(&fb->count))
+               return fb;
+       return NULL;
+}
+
+static void fuse_backing_free(struct fuse_backing *fb)
+{
+       pr_debug("%s: fb=0x%p\n", __func__, fb);
+
+       if (fb->file)
+               fput(fb->file);
+       put_cred(fb->cred);
+       kfree_rcu(fb, rcu);
+}
+
+void fuse_backing_put(struct fuse_backing *fb)
+{
+       if (fb && refcount_dec_and_test(&fb->count))
+               fuse_backing_free(fb);
+}
+
+void fuse_backing_files_init(struct fuse_conn *fc)
+{
+       idr_init(&fc->backing_files_map);
+}
+
+static int fuse_backing_id_alloc(struct fuse_conn *fc, struct fuse_backing *fb)
+{
+       int id;
+
+       idr_preload(GFP_KERNEL);
+       spin_lock(&fc->lock);
+       /* FIXME: xarray might be space inefficient */
+       id = idr_alloc_cyclic(&fc->backing_files_map, fb, 1, 0, GFP_ATOMIC);
+       spin_unlock(&fc->lock);
+       idr_preload_end();
+
+       WARN_ON_ONCE(id == 0);
+       return id;
+}
+
+static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
+                                                  int id)
+{
+       struct fuse_backing *fb;
+
+       spin_lock(&fc->lock);
+       fb = idr_remove(&fc->backing_files_map, id);
+       spin_unlock(&fc->lock);
+
+       return fb;
+}
+
+static int fuse_backing_id_free(int id, void *p, void *data)
+{
+       struct fuse_backing *fb = p;
+
+       WARN_ON_ONCE(refcount_read(&fb->count) != 1);
+       fuse_backing_free(fb);
+       return 0;
+}
+
+void fuse_backing_files_free(struct fuse_conn *fc)
+{
+       idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
+       idr_destroy(&fc->backing_files_map);
+}
+
+int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
+{
+       struct file *file;
+       struct super_block *backing_sb;
+       struct fuse_backing *fb = NULL;
+       int res;
+
+       pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
+
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+       res = -EPERM;
+       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
+               goto out;
+
+       res = -EINVAL;
+       if (map->flags || map->padding)
+               goto out;
+
+       file = fget_raw(map->fd);
+       res = -EBADF;
+       if (!file)
+               goto out;
+
+       backing_sb = file_inode(file)->i_sb;
+       res = -ELOOP;
+       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
+               goto out_fput;
+
+       fb = kmalloc(sizeof(struct fuse_backing), GFP_KERNEL);
+       res = -ENOMEM;
+       if (!fb)
+               goto out_fput;
+
+       fb->file = file;
+       fb->cred = prepare_creds();
+       refcount_set(&fb->count, 1);
+
+       res = fuse_backing_id_alloc(fc, fb);
+       if (res < 0) {
+               fuse_backing_free(fb);
+               fb = NULL;
+       }
+
+out:
+       pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
+
+       return res;
+
+out_fput:
+       fput(file);
+       goto out;
+}
+
+int fuse_backing_close(struct fuse_conn *fc, int backing_id)
+{
+       struct fuse_backing *fb = NULL;
+       int err;
+
+       pr_debug("%s: backing_id=%d\n", __func__, backing_id);
+
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+       err = -EPERM;
+       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
+               goto out;
+
+       err = -EINVAL;
+       if (backing_id <= 0)
+               goto out;
+
+       err = -ENOENT;
+       fb = fuse_backing_id_remove(fc, backing_id);
+       if (!fb)
+               goto out;
+
+       fuse_backing_put(fb);
+       err = 0;
+out:
+       pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err);
+
+       return err;
+}
+
+struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id)
+{
+       struct fuse_backing *fb;
+
+       rcu_read_lock();
+       fb = idr_find(&fc->backing_files_map, backing_id);
+       fb = fuse_backing_get(fb);
+       rcu_read_unlock();
+
+       return fb;
+}
fs/fuse/passthrough.c +1 -157
index 607ef735ad4ab3..e0b8d885bc81f3 100644
@@ -144,158 +144,6 @@ ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
        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))
-               return fb;
-       return NULL;
-}
-
-static void fuse_backing_free(struct fuse_backing *fb)
-{
-       pr_debug("%s: fb=0x%p\n", __func__, fb);
-
-       if (fb->file)
-               fput(fb->file);
-       put_cred(fb->cred);
-       kfree_rcu(fb, rcu);
-}
-
-void fuse_backing_put(struct fuse_backing *fb)
-{
-       if (fb && refcount_dec_and_test(&fb->count))
-               fuse_backing_free(fb);
-}
-
-void fuse_backing_files_init(struct fuse_conn *fc)
-{
-       idr_init(&fc->backing_files_map);
-}
-
-static int fuse_backing_id_alloc(struct fuse_conn *fc, struct fuse_backing *fb)
-{
-       int id;
-
-       idr_preload(GFP_KERNEL);
-       spin_lock(&fc->lock);
-       /* FIXME: xarray might be space inefficient */
-       id = idr_alloc_cyclic(&fc->backing_files_map, fb, 1, 0, GFP_ATOMIC);
-       spin_unlock(&fc->lock);
-       idr_preload_end();
-
-       WARN_ON_ONCE(id == 0);
-       return id;
-}
-
-static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
-                                                  int id)
-{
-       struct fuse_backing *fb;
-
-       spin_lock(&fc->lock);
-       fb = idr_remove(&fc->backing_files_map, id);
-       spin_unlock(&fc->lock);
-
-       return fb;
-}
-
-static int fuse_backing_id_free(int id, void *p, void *data)
-{
-       struct fuse_backing *fb = p;
-
-       WARN_ON_ONCE(refcount_read(&fb->count) != 1);
-       fuse_backing_free(fb);
-       return 0;
-}
-
-void fuse_backing_files_free(struct fuse_conn *fc)
-{
-       idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
-       idr_destroy(&fc->backing_files_map);
-}
-
-int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
-{
-       struct file *file;
-       struct super_block *backing_sb;
-       struct fuse_backing *fb = NULL;
-       int res;
-
-       pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
-
-       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
-       res = -EPERM;
-       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
-               goto out;
-
-       res = -EINVAL;
-       if (map->flags || map->padding)
-               goto out;
-
-       file = fget_raw(map->fd);
-       res = -EBADF;
-       if (!file)
-               goto out;
-
-       backing_sb = file_inode(file)->i_sb;
-       res = -ELOOP;
-       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
-               goto out_fput;
-
-       fb = kmalloc(sizeof(struct fuse_backing), GFP_KERNEL);
-       res = -ENOMEM;
-       if (!fb)
-               goto out_fput;
-
-       fb->file = file;
-       fb->cred = prepare_creds();
-       refcount_set(&fb->count, 1);
-
-       res = fuse_backing_id_alloc(fc, fb);
-       if (res < 0) {
-               fuse_backing_free(fb);
-               fb = NULL;
-       }
-
-out:
-       pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
-
-       return res;
-
-out_fput:
-       fput(file);
-       goto out;
-}
-
-int fuse_backing_close(struct fuse_conn *fc, int backing_id)
-{
-       struct fuse_backing *fb = NULL;
-       int err;
-
-       pr_debug("%s: backing_id=%d\n", __func__, backing_id);
-
-       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
-       err = -EPERM;
-       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
-               goto out;
-
-       err = -EINVAL;
-       if (backing_id <= 0)
-               goto out;
-
-       err = -ENOENT;
-       fb = fuse_backing_id_remove(fc, backing_id);
-       if (!fb)
-               goto out;
-
-       fuse_backing_put(fb);
-       err = 0;
-out:
-       pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err);
-
-       return err;
-}
-
 /*
  * Setup passthrough to a backing file.
  *
@@ -315,12 +163,8 @@ struct fuse_backing *fuse_passthrough_open(struct file *file,
        if (backing_id <= 0)
                goto out;
 
-       rcu_read_lock();
-       fb = idr_find(&fc->backing_files_map, backing_id);
-       fb = fuse_backing_get(fb);
-       rcu_read_unlock();
-
        err = -ENOENT;
+       fb = fuse_backing_lookup(fc, backing_id);
        if (!fb)
                goto out;
 
#75 lore raw

Re: [PATCH 04/23] fuse: move the backing file idr and code into a new source file

Quoted 10 lines · Amir Goldstein
Quoted 9 lines · Amir Goldstein
Quoted 7 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

iomap support for fuse is also going to want the ability to attach backing files to a fuse filesystem. Move the fuse_backing code into a separate file so that both can use it.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Are you going to make FUSE_IOMAP depend on FUSE_PASSTHROUGH later on? I can't think of a reason why not.

Ah I see. They will both depend on FUSE_BACKING
cool

Yep. Thanks for your feedback! :)

--D

Quoted 461 lines
Quoted 460 lines

Thanks,
Amir.

Quoted 456 lines
---
 fs/fuse/fuse_i.h      |   47 ++++++++-----
 fs/fuse/Makefile      |    2 -
 fs/fuse/backing.c     |  174 +++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/passthrough.c |  158 --------------------------------------------
 4 files changed, 203 insertions(+), 178 deletions(-)
 create mode 100644 fs/fuse/backing.c
fs/fuse/fuse_i.h +27 -20
index 2cd9f4cdc6a7ef..2be2cbdf060536 100644
@@ -1535,29 +1535,11 @@ struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
                       unsigned int open_flags, fl_owner_t id, bool isdir);
 
-/* passthrough.c */
-static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi)
-{
-#ifdef CONFIG_FUSE_PASSTHROUGH
-       return READ_ONCE(fi->fb);
-#else
-       return NULL;
-#endif
-}
-
-static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi,
-                                                         struct fuse_backing *fb)
-{
-#ifdef CONFIG_FUSE_PASSTHROUGH
-       return xchg(&fi->fb, fb);
-#else
-       return NULL;
-#endif
-}
-
+/* backing.c */
 #ifdef CONFIG_FUSE_PASSTHROUGH
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb);
 void fuse_backing_put(struct fuse_backing *fb);
+struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id);
 #else
 
 static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
@@ -1568,6 +1550,11 @@ static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 static inline void fuse_backing_put(struct fuse_backing *fb)
 {
 }
+static inline struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc,
+                                                      int backing_id)
+{
+       return NULL;
+}
 #endif
 
 void fuse_backing_files_init(struct fuse_conn *fc);
@@ -1575,6 +1562,26 @@ void fuse_backing_files_free(struct fuse_conn *fc);
 int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map);
 int fuse_backing_close(struct fuse_conn *fc, int backing_id);
 
+/* passthrough.c */
+static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi)
+{
+#ifdef CONFIG_FUSE_PASSTHROUGH
+       return READ_ONCE(fi->fb);
+#else
+       return NULL;
+#endif
+}
+
+static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi,
+                                                         struct fuse_backing *fb)
+{
+#ifdef CONFIG_FUSE_PASSTHROUGH
+       return xchg(&fi->fb, fb);
+#else
+       return NULL;
+#endif
+}
+
 struct fuse_backing *fuse_passthrough_open(struct file *file,
                                           struct inode *inode,
                                           int backing_id);
fs/fuse/Makefile +1 -1
index 70709a7a3f9523..c79f786d0c90c3 100644
@@ -14,7 +14,7 @@ fuse-y := trace.o     # put trace.o first so we see ftrace errors sooner
 fuse-y += dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o
 fuse-y += iomode.o
 fuse-$(CONFIG_FUSE_DAX) += dax.o
-fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o
+fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o backing.o
 fuse-$(CONFIG_SYSCTL) += sysctl.o
 fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
 fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o
fs/fuse/backing.c new file +174 -0
new file mode 100644
index 00000000000000..ddb23b7400fc72
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * FUSE passthrough to backing file.
+ *
+ * Copyright (c) 2023 CTERA Networks.
+ */
+
+#include "fuse_i.h"
+
+#include <linux/file.h>
+
+struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
+{
+       if (fb && refcount_inc_not_zero(&fb->count))
+               return fb;
+       return NULL;
+}
+
+static void fuse_backing_free(struct fuse_backing *fb)
+{
+       pr_debug("%s: fb=0x%p\n", __func__, fb);
+
+       if (fb->file)
+               fput(fb->file);
+       put_cred(fb->cred);
+       kfree_rcu(fb, rcu);
+}
+
+void fuse_backing_put(struct fuse_backing *fb)
+{
+       if (fb && refcount_dec_and_test(&fb->count))
+               fuse_backing_free(fb);
+}
+
+void fuse_backing_files_init(struct fuse_conn *fc)
+{
+       idr_init(&fc->backing_files_map);
+}
+
+static int fuse_backing_id_alloc(struct fuse_conn *fc, struct fuse_backing *fb)
+{
+       int id;
+
+       idr_preload(GFP_KERNEL);
+       spin_lock(&fc->lock);
+       /* FIXME: xarray might be space inefficient */
+       id = idr_alloc_cyclic(&fc->backing_files_map, fb, 1, 0, GFP_ATOMIC);
+       spin_unlock(&fc->lock);
+       idr_preload_end();
+
+       WARN_ON_ONCE(id == 0);
+       return id;
+}
+
+static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
+                                                  int id)
+{
+       struct fuse_backing *fb;
+
+       spin_lock(&fc->lock);
+       fb = idr_remove(&fc->backing_files_map, id);
+       spin_unlock(&fc->lock);
+
+       return fb;
+}
+
+static int fuse_backing_id_free(int id, void *p, void *data)
+{
+       struct fuse_backing *fb = p;
+
+       WARN_ON_ONCE(refcount_read(&fb->count) != 1);
+       fuse_backing_free(fb);
+       return 0;
+}
+
+void fuse_backing_files_free(struct fuse_conn *fc)
+{
+       idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
+       idr_destroy(&fc->backing_files_map);
+}
+
+int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
+{
+       struct file *file;
+       struct super_block *backing_sb;
+       struct fuse_backing *fb = NULL;
+       int res;
+
+       pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
+
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+       res = -EPERM;
+       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
+               goto out;
+
+       res = -EINVAL;
+       if (map->flags || map->padding)
+               goto out;
+
+       file = fget_raw(map->fd);
+       res = -EBADF;
+       if (!file)
+               goto out;
+
+       backing_sb = file_inode(file)->i_sb;
+       res = -ELOOP;
+       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
+               goto out_fput;
+
+       fb = kmalloc(sizeof(struct fuse_backing), GFP_KERNEL);
+       res = -ENOMEM;
+       if (!fb)
+               goto out_fput;
+
+       fb->file = file;
+       fb->cred = prepare_creds();
+       refcount_set(&fb->count, 1);
+
+       res = fuse_backing_id_alloc(fc, fb);
+       if (res < 0) {
+               fuse_backing_free(fb);
+               fb = NULL;
+       }
+
+out:
+       pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
+
+       return res;
+
+out_fput:
+       fput(file);
+       goto out;
+}
+
+int fuse_backing_close(struct fuse_conn *fc, int backing_id)
+{
+       struct fuse_backing *fb = NULL;
+       int err;
+
+       pr_debug("%s: backing_id=%d\n", __func__, backing_id);
+
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+       err = -EPERM;
+       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
+               goto out;
+
+       err = -EINVAL;
+       if (backing_id <= 0)
+               goto out;
+
+       err = -ENOENT;
+       fb = fuse_backing_id_remove(fc, backing_id);
+       if (!fb)
+               goto out;
+
+       fuse_backing_put(fb);
+       err = 0;
+out:
+       pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err);
+
+       return err;
+}
+
+struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id)
+{
+       struct fuse_backing *fb;
+
+       rcu_read_lock();
+       fb = idr_find(&fc->backing_files_map, backing_id);
+       fb = fuse_backing_get(fb);
+       rcu_read_unlock();
+
+       return fb;
+}
fs/fuse/passthrough.c +1 -157
index 607ef735ad4ab3..e0b8d885bc81f3 100644
@@ -144,158 +144,6 @@ ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
        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))
-               return fb;
-       return NULL;
-}
-
-static void fuse_backing_free(struct fuse_backing *fb)
-{
-       pr_debug("%s: fb=0x%p\n", __func__, fb);
-
-       if (fb->file)
-               fput(fb->file);
-       put_cred(fb->cred);
-       kfree_rcu(fb, rcu);
-}
-
-void fuse_backing_put(struct fuse_backing *fb)
-{
-       if (fb && refcount_dec_and_test(&fb->count))
-               fuse_backing_free(fb);
-}
-
-void fuse_backing_files_init(struct fuse_conn *fc)
-{
-       idr_init(&fc->backing_files_map);
-}
-
-static int fuse_backing_id_alloc(struct fuse_conn *fc, struct fuse_backing *fb)
-{
-       int id;
-
-       idr_preload(GFP_KERNEL);
-       spin_lock(&fc->lock);
-       /* FIXME: xarray might be space inefficient */
-       id = idr_alloc_cyclic(&fc->backing_files_map, fb, 1, 0, GFP_ATOMIC);
-       spin_unlock(&fc->lock);
-       idr_preload_end();
-
-       WARN_ON_ONCE(id == 0);
-       return id;
-}
-
-static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
-                                                  int id)
-{
-       struct fuse_backing *fb;
-
-       spin_lock(&fc->lock);
-       fb = idr_remove(&fc->backing_files_map, id);
-       spin_unlock(&fc->lock);
-
-       return fb;
-}
-
-static int fuse_backing_id_free(int id, void *p, void *data)
-{
-       struct fuse_backing *fb = p;
-
-       WARN_ON_ONCE(refcount_read(&fb->count) != 1);
-       fuse_backing_free(fb);
-       return 0;
-}
-
-void fuse_backing_files_free(struct fuse_conn *fc)
-{
-       idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
-       idr_destroy(&fc->backing_files_map);
-}
-
-int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
-{
-       struct file *file;
-       struct super_block *backing_sb;
-       struct fuse_backing *fb = NULL;
-       int res;
-
-       pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
-
-       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
-       res = -EPERM;
-       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
-               goto out;
-
-       res = -EINVAL;
-       if (map->flags || map->padding)
-               goto out;
-
-       file = fget_raw(map->fd);
-       res = -EBADF;
-       if (!file)
-               goto out;
-
-       backing_sb = file_inode(file)->i_sb;
-       res = -ELOOP;
-       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
-               goto out_fput;
-
-       fb = kmalloc(sizeof(struct fuse_backing), GFP_KERNEL);
-       res = -ENOMEM;
-       if (!fb)
-               goto out_fput;
-
-       fb->file = file;
-       fb->cred = prepare_creds();
-       refcount_set(&fb->count, 1);
-
-       res = fuse_backing_id_alloc(fc, fb);
-       if (res < 0) {
-               fuse_backing_free(fb);
-               fb = NULL;
-       }
-
-out:
-       pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
-
-       return res;
-
-out_fput:
-       fput(file);
-       goto out;
-}
-
-int fuse_backing_close(struct fuse_conn *fc, int backing_id)
-{
-       struct fuse_backing *fb = NULL;
-       int err;
-
-       pr_debug("%s: backing_id=%d\n", __func__, backing_id);
-
-       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
-       err = -EPERM;
-       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
-               goto out;
-
-       err = -EINVAL;
-       if (backing_id <= 0)
-               goto out;
-
-       err = -ENOENT;
-       fb = fuse_backing_id_remove(fc, backing_id);
-       if (!fb)
-               goto out;
-
-       fuse_backing_put(fb);
-       err = 0;
-out:
-       pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err);
-
-       return err;
-}
-
 /*
  * Setup passthrough to a backing file.
  *
@@ -315,12 +163,8 @@ struct fuse_backing *fuse_passthrough_open(struct file *file,
        if (backing_id <= 0)
                goto out;
 
-       rcu_read_lock();
-       fb = idr_find(&fc->backing_files_map, backing_id);
-       fb = fuse_backing_get(fb);
-       rcu_read_unlock();
-
        err = -ENOENT;
+       fb = fuse_backing_lookup(fc, backing_id);
        if (!fb)
                goto out;
 
#76 lore raw

PATCH 5/23 fuse: move the passthrough-specific code back to passthrough.c

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In preparation for iomap, move the passthrough-specific validation code back to passthrough.c and create a new Kconfig item for conditional compilation of backing.c. In the next patch, iomap will share the backing structures.

---
 fs/fuse/fuse_i.h      |   14 ++++++
 fs/fuse/fuse_trace.h  |   35 ++++++++++++++++
 fs/fuse/Kconfig       |    4 ++
 fs/fuse/Makefile      |    3 +
 fs/fuse/backing.c     |  106 +++++++++++++++++++++++++++++++++++++------------
 fs/fuse/dev.c         |    4 +-
 fs/fuse/inode.c       |    4 +-
 fs/fuse/passthrough.c |   28 +++++++++++++
 8 files changed, 165 insertions(+), 33 deletions(-)
fs/fuse/fuse_i.h +12 -2
index 2be2cbdf060536..1762517a1b99c8 100644
@@ -958,7 +958,7 @@ struct fuse_conn {
 	/* New writepages go into this bucket */
 	struct fuse_sync_bucket __rcu *curr_bucket;
 
-#ifdef CONFIG_FUSE_PASSTHROUGH
+#ifdef CONFIG_FUSE_BACKING
 	/** IDR for backing files ids */
 	struct idr backing_files_map;
 #endif
@@ -1536,7 +1536,7 @@ void fuse_file_release(struct inode *inode, struct fuse_file *ff,
 		       unsigned int open_flags, fl_owner_t id, bool isdir);
 
 /* backing.c */
-#ifdef CONFIG_FUSE_PASSTHROUGH
+#ifdef CONFIG_FUSE_BACKING
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb);
 void fuse_backing_put(struct fuse_backing *fb);
 struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id);
@@ -1596,6 +1596,16 @@ static inline struct file *fuse_file_passthrough(struct fuse_file *ff)
 #endif
 }
 
+#ifdef CONFIG_FUSE_PASSTHROUGH
+int fuse_passthrough_backing_open(struct fuse_conn *fc,
+				  struct fuse_backing *fb);
+int fuse_passthrough_backing_close(struct fuse_conn *fc,
+				   struct fuse_backing *fb);
+#else
+# define fuse_passthrough_backing_open(...)	(-EOPNOTSUPP)
+# define fuse_passthrough_backing_close(...)	(-EOPNOTSUPP)
+#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);
 ssize_t fuse_passthrough_splice_read(struct file *in, loff_t *ppos,
fs/fuse/fuse_trace.h +35 -0
index 2389072b734636..660d9b5206a175 100644
@@ -174,6 +174,41 @@ TRACE_EVENT(fuse_request_end,
 		  __entry->unique, __entry->len, __entry->error)
 );
 
+#ifdef CONFIG_FUSE_BACKING
+TRACE_EVENT(fuse_backing_class,
+	TP_PROTO(const struct fuse_conn *fc, unsigned int idx,
+		 const struct fuse_backing *fb),
+
+	TP_ARGS(fc, idx, fb),
+
+	TP_STRUCT__entry(
+		__field(dev_t,			connection)
+		__field(unsigned int,		idx)
+		__field(unsigned long,		ino)
+	),
+
+	TP_fast_assign(
+		struct inode *inode = file_inode(fb->file);
+
+		__entry->connection	=	fc->dev;
+		__entry->idx		=	idx;
+		__entry->ino		=	inode->i_ino;
+	),
+
+	TP_printk("connection %u idx %u ino 0x%lx",
+		  __entry->connection,
+		  __entry->idx,
+		  __entry->ino)
+);
+#define DEFINE_FUSE_BACKING_EVENT(name)		\
+DEFINE_EVENT(fuse_backing_class, name,		\
+	TP_PROTO(const struct fuse_conn *fc, unsigned int idx, \
+		 const struct fuse_backing *fb), \
+	TP_ARGS(fc, idx, fb))
+DEFINE_FUSE_BACKING_EVENT(fuse_backing_open);
+DEFINE_FUSE_BACKING_EVENT(fuse_backing_close);
+#endif
+
 #if IS_ENABLED(CONFIG_FUSE_IOMAP)
 
 /* tracepoint boilerplate so we don't have to keep doing this */
fs/fuse/Kconfig +4 -0
index 6be74396ef5198..ebb9a2d76b532e 100644
@@ -59,12 +59,16 @@ config FUSE_PASSTHROUGH
 	default y
 	depends on FUSE_FS
 	select FS_STACK
+	select FUSE_BACKING
 	help
 	  This allows bypassing FUSE server by mapping specific FUSE operations
 	  to be performed directly on a backing file.
 
 	  If you want to allow passthrough operations, answer Y.
 
+config FUSE_BACKING
+	bool
+
 config FUSE_IOMAP
 	bool "FUSE file IO over iomap"
 	default y
fs/fuse/Makefile +2 -1
index c79f786d0c90c3..27be39317701d6 100644
@@ -14,7 +14,8 @@ fuse-y := trace.o	# put trace.o first so we see ftrace errors sooner
 fuse-y += dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o
 fuse-y += iomode.o
 fuse-$(CONFIG_FUSE_DAX) += dax.o
-fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o backing.o
+fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o
+fuse-$(CONFIG_FUSE_BACKING) += backing.o
 fuse-$(CONFIG_SYSCTL) += sysctl.o
 fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
 fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o
fs/fuse/backing.c +80 -26

160 lines not on this page · raw message

fs/fuse/dev.c +2 -2
index dbde17fff0cda9..31d9f006836ac1 100644
@@ -2623,7 +2623,7 @@ static long fuse_dev_ioctl_backing_open(struct file *file,
 	if (!fud)
 		return -EPERM;
 
-	if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+	if (!IS_ENABLED(CONFIG_FUSE_BACKING))
 		return -EOPNOTSUPP;
 
 	if (copy_from_user(&map, argp, sizeof(map)))
@@ -2640,7 +2640,7 @@ static long fuse_dev_ioctl_backing_close(struct file *file, __u32 __user *argp)
 	if (!fud)
 		return -EPERM;
 
-	if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+	if (!IS_ENABLED(CONFIG_FUSE_BACKING))
 		return -EOPNOTSUPP;
 
 	if (get_user(backing_id, argp))
fs/fuse/inode.c +2 -2
index 9448a11c828fef..1f3f91981410aa 100644
@@ -993,7 +993,7 @@ void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
 	fc->name_max = FUSE_NAME_LOW_MAX;
 	fc->timeout.req_timeout = 0;
 
-	if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+	if (IS_ENABLED(CONFIG_FUSE_BACKING))
 		fuse_backing_files_init(fc);
 
 	INIT_LIST_HEAD(&fc->mounts);
@@ -1030,7 +1030,7 @@ void fuse_conn_put(struct fuse_conn *fc)
 			WARN_ON(atomic_read(&bucket->count) != 1);
 			kfree(bucket);
 		}
-		if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+		if (IS_ENABLED(CONFIG_FUSE_BACKING))
 			fuse_backing_files_free(fc);
 		call_rcu(&fc->rcu, delayed_release);
 	}
fs/fuse/passthrough.c +28 -0
index e0b8d885bc81f3..dfc61cc4bd21af 100644
@@ -197,3 +197,31 @@ void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb)
 	put_cred(ff->cred);
 	ff->cred = NULL;
 }
+
+int fuse_passthrough_backing_open(struct fuse_conn *fc,
+				  struct fuse_backing *fb)
+{
+	struct super_block *backing_sb;
+
+	/* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	backing_sb = file_inode(fb->file)->i_sb;
+	if (backing_sb->s_stack_depth >= fc->max_stack_depth)
+		return -ELOOP;
+
+	fuse_backing_get(fb);
+	return 0;
+}
+
+int fuse_passthrough_backing_close(struct fuse_conn *fc,
+				   struct fuse_backing *fb)
+{
+	/* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	fuse_backing_put(fb);
+	return 0;
+}
maintainer
#77 lore raw

Re: [PATCH 05/23] fuse: move the passthrough-specific code back to passthrough.c

Quoted 270 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In preparation for iomap, move the passthrough-specific validation code back to passthrough.c and create a new Kconfig item for conditional compilation of backing.c. In the next patch, iomap will share the backing structures.

---
 fs/fuse/fuse_i.h      |   14 ++++++
 fs/fuse/fuse_trace.h  |   35 ++++++++++++++++
 fs/fuse/Kconfig       |    4 ++
 fs/fuse/Makefile      |    3 +
 fs/fuse/backing.c     |  106 +++++++++++++++++++++++++++++++++++++------------
 fs/fuse/dev.c         |    4 +-
 fs/fuse/inode.c       |    4 +-
 fs/fuse/passthrough.c |   28 +++++++++++++
 8 files changed, 165 insertions(+), 33 deletions(-)
fs/fuse/fuse_i.h +12 -2
index 2be2cbdf060536..1762517a1b99c8 100644
@@ -958,7 +958,7 @@ struct fuse_conn {
        /* New writepages go into this bucket */
        struct fuse_sync_bucket __rcu *curr_bucket;
 
-#ifdef CONFIG_FUSE_PASSTHROUGH
+#ifdef CONFIG_FUSE_BACKING
        /** IDR for backing files ids */
        struct idr backing_files_map;
 #endif
@@ -1536,7 +1536,7 @@ void fuse_file_release(struct inode *inode, struct fuse_file *ff,
                       unsigned int open_flags, fl_owner_t id, bool isdir);
 
 /* backing.c */
-#ifdef CONFIG_FUSE_PASSTHROUGH
+#ifdef CONFIG_FUSE_BACKING
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb);
 void fuse_backing_put(struct fuse_backing *fb);
 struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, int backing_id);
@@ -1596,6 +1596,16 @@ static inline struct file *fuse_file_passthrough(struct fuse_file *ff)
 #endif
 }
 
+#ifdef CONFIG_FUSE_PASSTHROUGH
+int fuse_passthrough_backing_open(struct fuse_conn *fc,
+                                 struct fuse_backing *fb);
+int fuse_passthrough_backing_close(struct fuse_conn *fc,
+                                  struct fuse_backing *fb);
+#else
+# define fuse_passthrough_backing_open(...)    (-EOPNOTSUPP)
+# define fuse_passthrough_backing_close(...)   (-EOPNOTSUPP)
+#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);
 ssize_t fuse_passthrough_splice_read(struct file *in, loff_t *ppos,
fs/fuse/fuse_trace.h +35 -0
index 2389072b734636..660d9b5206a175 100644
@@ -174,6 +174,41 @@ TRACE_EVENT(fuse_request_end,
                  __entry->unique, __entry->len, __entry->error)
 );
 
+#ifdef CONFIG_FUSE_BACKING
+TRACE_EVENT(fuse_backing_class,
+       TP_PROTO(const struct fuse_conn *fc, unsigned int idx,
+                const struct fuse_backing *fb),
+
+       TP_ARGS(fc, idx, fb),
+
+       TP_STRUCT__entry(
+               __field(dev_t,                  connection)
+               __field(unsigned int,           idx)
+               __field(unsigned long,          ino)
+       ),
+
+       TP_fast_assign(
+               struct inode *inode = file_inode(fb->file);
+
+               __entry->connection     =       fc->dev;
+               __entry->idx            =       idx;
+               __entry->ino            =       inode->i_ino;
+       ),
+
+       TP_printk("connection %u idx %u ino 0x%lx",
+                 __entry->connection,
+                 __entry->idx,
+                 __entry->ino)
+);
+#define DEFINE_FUSE_BACKING_EVENT(name)                \
+DEFINE_EVENT(fuse_backing_class, name,         \
+       TP_PROTO(const struct fuse_conn *fc, unsigned int idx, \
+                const struct fuse_backing *fb), \
+       TP_ARGS(fc, idx, fb))
+DEFINE_FUSE_BACKING_EVENT(fuse_backing_open);
+DEFINE_FUSE_BACKING_EVENT(fuse_backing_close);
+#endif
+
 #if IS_ENABLED(CONFIG_FUSE_IOMAP)
 
 /* tracepoint boilerplate so we don't have to keep doing this */
fs/fuse/Kconfig +4 -0
index 6be74396ef5198..ebb9a2d76b532e 100644
@@ -59,12 +59,16 @@ config FUSE_PASSTHROUGH
        default y
        depends on FUSE_FS
        select FS_STACK
+       select FUSE_BACKING
        help
          This allows bypassing FUSE server by mapping specific FUSE operations
          to be performed directly on a backing file.
 
          If you want to allow passthrough operations, answer Y.
 
+config FUSE_BACKING
+       bool
+
 config FUSE_IOMAP
        bool "FUSE file IO over iomap"
        default y
fs/fuse/Makefile +2 -1
index c79f786d0c90c3..27be39317701d6 100644
@@ -14,7 +14,8 @@ fuse-y := trace.o     # put trace.o first so we see ftrace errors sooner
 fuse-y += dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o ioctl.o
 fuse-y += iomode.o
 fuse-$(CONFIG_FUSE_DAX) += dax.o
-fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o backing.o
+fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o
+fuse-$(CONFIG_FUSE_BACKING) += backing.o
 fuse-$(CONFIG_SYSCTL) += sysctl.o
 fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
 fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o
fs/fuse/backing.c +46 -25
index ddb23b7400fc72..c128bed95a76b8 100644
@@ -6,6 +6,7 @@
  */
 
 #include "fuse_i.h"
+#include "fuse_trace.h"
 
 #include <linux/file.h>
 
@@ -81,16 +82,14 @@ void fuse_backing_files_free(struct fuse_conn *fc)
 
 int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
 {
-       struct file *file;
-       struct super_block *backing_sb;
+       struct file *file = NULL;
        struct fuse_backing *fb = NULL;
-       int res;
+       int res, passthrough_res;
 
        pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
 
-       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
        res = -EPERM;
-       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
+       if (!fc->passthrough)
                goto out;
 
        res = -EINVAL;
@@ -102,46 +101,68 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
        if (!file)
                goto out;
 
-       backing_sb = file_inode(file)->i_sb;
-       res = -ELOOP;
-       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
-               goto out_fput;
-
        fb = kmalloc(sizeof(struct fuse_backing), GFP_KERNEL);
        res = -ENOMEM;
        if (!fb)
-               goto out_fput;
+               goto out_file;
 
+       /* fb now owns file */
        fb->file = file;
+       file = NULL;
        fb->cred = prepare_creds();
        refcount_set(&fb->count, 1);
 
+       /*
+        * Each _backing_open function should either:
+        *
+        * 1. Take a ref to fb if it wants the file and return 0.
+        * 2. Return 0 without taking a ref if the backing file isn't needed.
+        * 3. Return an errno explaining why it couldn't attach.
+        *
+        * If at least one subsystem bumps the reference count to open it,
+        * we'll install it into the index and return the index.  If nobody
+        * opens the file, the error code will be passed up.  EPERM is the
+        * default.
+        */
+       passthrough_res = fuse_passthrough_backing_open(fc, fb);
+
+       if (refcount_read(&fb->count) < 2) {
+               if (passthrough_res)
+                       res = passthrough_res;
+               if (!res)
+                       res = -EPERM;
+               goto out_fb;
+       }
+
        res = fuse_backing_id_alloc(fc, fb);
-       if (res < 0) {
-               fuse_backing_free(fb);
-               fb = NULL;
-       }
+       if (res < 0)
+               goto out_fb;
+
+       trace_fuse_backing_open(fc, res, fb);
 
-out:
        pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
-
+       fuse_backing_put(fb);
        return res;
 
-out_fput:
-       fput(file);
-       goto out;
+out_fb:
+       fuse_backing_free(fb);
+out_file:
+       if (file)
+               fput(file);
+out:
+       pr_debug("%s: ret=%i\n", __func__, res);
+       return res;
 }
 
 int fuse_backing_close(struct fuse_conn *fc, int backing_id)
 {
-       struct fuse_backing *fb = NULL;
-       int err;
+       struct fuse_backing *fb = NULL, *test_fb;
+       int err, passthrough_err;
 
        pr_debug("%s: backing_id=%d\n", __func__, backing_id);
 
-       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
        err = -EPERM;
-       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
+       if (!fc->passthrough)
                goto out;
 
        err = -EINVAL;
@@ -149,12 +170,45 @@ int fuse_backing_close(struct fuse_conn *fc, int backing_id)
                goto out;
 
        err = -ENOENT;
-       fb = fuse_backing_id_remove(fc, backing_id);
+       fb = fuse_backing_lookup(fc, backing_id);
        if (!fb)
                goto out;
 

fs/fuse/backing.c int fuse_backing_close(struct fuse_conn *fc, int backing_id) · line 186

Quoted 10 lines
fs/fuse/backing.c +10 -0
+       /*
+        * Each _backing_close function should either:
+        *
+        * 1. Release the ref that it took in _backing_open and return 0.
+        * 2. Don't release the ref if the backing file is busy, and return 0.
+        * 2. Return an errno explaining why it couldn't detach.
+        *
+        * If there are no more active references to the backing file, it will
+        * be closed and removed from the index.  If there are still active
+        * references to the backing file other than the one we just took, the

That does not look right.
The fuse_backing object can often outliive the backing_id mapping
1. fuse server attached backing fd to backing id 1
2. fuse server opens a file with passthrough to backing id 1
3. fuse inode holds a refcount to the fuse_backing object
4. fuse server closes backing id 1 mapping
5. fuse server closes file, drops last reference to fuse_backing object

IOW, fb->count is not about being in the index.
With your code the fuse server call in #4 above will end up leaving the fuse_backing object in the index and after #5 it will remain a dangling object in the index.

TBH, I don't understand why we need any of the complexity of two subsystems claiming the same fuse_backing object for two different purposes.

Also, I think that an explicit statement from the server about the purpose of the backing file is due (like your commit message implies) This could be easily done with the backing open flags member:

fs/fuse/backing.c +0 -0
index c63990254649c..e5a675fca7505 100644
@@ -96,7 +96,7 @@ int fuse_backing_open(struct fuse_conn *fc, struct

fuse_backing_map *map)

                goto out;

        res = -EINVAL;
-       if (map->flags || map->padding)
+       if (map->flags & ~FUSE_BACKING_VALID_FLAGS || map->padding)
                goto out;

        file = fget_raw(map->fd);

@@ -127,8 +127,10 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
* opens the file, the error code will be passed up. EPERM is the
* default.
*/

-       passthrough_res = fuse_passthrough_backing_open(fc, fb);
-       iomap_res = fuse_iomap_backing_open(fc, fb);
+       if (map->flags & FUSE_BACKING_IOMAP)

+ iomap_res = fuse_iomap_backing_open(fc, fb);
+ else
+ passthrough_res = fuse_passthrough_backing_open(fc, fb);

        if (refcount_read(&fb->count) < 2) {
                if (passthrough_res)

@@ -192,8 +194,10 @@ int fuse_backing_close(struct fuse_conn *fc, int backing_id)
* references to the backing file other than the one we just took, the
* error code will be passed up. EBUSY is the default. */

-       passthrough_err = fuse_passthrough_backing_close(fc, fb);
-       iomap_err = fuse_iomap_backing_close(fc, fb);
+       if (fb->bdev)

+ iomap_err = fuse_iomap_backing_close(fc, fb);
+ else
+ passthrough_err = fuse_passthrough_backing_close(fc, fb);

        if (refcount_read(&fb->count) > 1) {
                if (passthrough_err)
include/uapi/linux/fuse.h +4 -0
index 70b5530e587d4..ee81903ad2f98 100644
@@ -1148,6 +1148,10 @@ struct fuse_notify_retrieve_in {
        uint64_t        dummy4;
 };
 
+/* basic file I/O functionality through iomap */
+#define FUSE_BACKING_IOMAP             (1 << 0)
+#define FUSE_BACKING_VALID_FLAGS       (FUSE_BACKING_IOMAP)
+
 struct fuse_backing_map {
        int32_t         fd;
        uint32_t        flags;
Quoted 80 lines
fs/fuse/backing.c +24 -1
+        * error code will be passed up.  EBUSY is the default.
+        */
+       passthrough_err = fuse_passthrough_backing_close(fc, fb);
+
+       if (refcount_read(&fb->count) > 1) {
+               if (passthrough_err)
+                       err = passthrough_err;
+               if (!err)
+                       err = -EBUSY;
+               goto out_fb;
+       }
+
+       trace_fuse_backing_close(fc, backing_id, fb);
+
+       err = -ENOENT;
+       test_fb = fuse_backing_id_remove(fc, backing_id);
+       if (!test_fb)
+               goto out_fb;
+
+       WARN_ON(fb != test_fb);
+       pr_debug("%s: fb=0x%p, err=0\n", __func__, fb);
+       fuse_backing_put(fb);
+       return 0;
+out_fb:
        fuse_backing_put(fb);
-       err = 0;
 out:
        pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err);
 
fs/fuse/dev.c +2 -2
index dbde17fff0cda9..31d9f006836ac1 100644
@@ -2623,7 +2623,7 @@ static long fuse_dev_ioctl_backing_open(struct file *file,
        if (!fud)
                return -EPERM;
 
-       if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+       if (!IS_ENABLED(CONFIG_FUSE_BACKING))
                return -EOPNOTSUPP;
 
        if (copy_from_user(&map, argp, sizeof(map)))
@@ -2640,7 +2640,7 @@ static long fuse_dev_ioctl_backing_close(struct file *file, __u32 __user *argp)
        if (!fud)
                return -EPERM;
 
-       if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+       if (!IS_ENABLED(CONFIG_FUSE_BACKING))
                return -EOPNOTSUPP;
 
        if (get_user(backing_id, argp))
fs/fuse/inode.c +2 -2
index 9448a11c828fef..1f3f91981410aa 100644
@@ -993,7 +993,7 @@ void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
        fc->name_max = FUSE_NAME_LOW_MAX;
        fc->timeout.req_timeout = 0;
 
-       if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+       if (IS_ENABLED(CONFIG_FUSE_BACKING))
                fuse_backing_files_init(fc);
 
        INIT_LIST_HEAD(&fc->mounts);
@@ -1030,7 +1030,7 @@ void fuse_conn_put(struct fuse_conn *fc)
                        WARN_ON(atomic_read(&bucket->count) != 1);
                        kfree(bucket);
                }
-               if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+               if (IS_ENABLED(CONFIG_FUSE_BACKING))
                        fuse_backing_files_free(fc);
                call_rcu(&fc->rcu, delayed_release);
        }
fs/fuse/passthrough.c +0 -0
index e0b8d885bc81f3..dfc61cc4bd21af 100644
@@ -197,3 +197,31 @@ void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb)
        put_cred(ff->cred);
        ff->cred = NULL;

fs/fuse/passthrough.c void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb) · line 208

Quoted 10 lines
fs/fuse/passthrough.c +9 -0
 }
+
+int fuse_passthrough_backing_open(struct fuse_conn *fc,
+                                 struct fuse_backing *fb)
+{
+       struct super_block *backing_sb;
+
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;

This limitation is not specific to fuse passthrough.
While the fuse passthrough use case is likely to request many fuse backing files,
the limitation is here to protect from malicious actors and the same ioctl used by the iomap fuse server can just as well open many "lsof invisible" files, so the limitation should be in the generic function.

Quoted 6 lines
fs/fuse/passthrough.c +6 -0
+
+       backing_sb = file_inode(fb->file)->i_sb;
+       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
+               return -ELOOP;
+
+       fuse_backing_get(fb);

fs/fuse/passthrough.c void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb) · line 224

Quoted 10 lines
fs/fuse/passthrough.c +10 -0
+       return 0;
+}
+
+int fuse_passthrough_backing_close(struct fuse_conn *fc,
+                                  struct fuse_backing *fb)
+{
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+

Probably this comment in upstream is not very accurate because there is no harm done in closing the backing files, but sure for symmetry. Same comment as above through, unless there are reasons to relax CAP_SYS_ADMIN for file iomap, would leave this in the genetic code.

And then there is not much justification left for the close helpers IMO, especially given that the implementation wrt removing from index is incorrect, I would keep it simple:

@@ -175,11 +177,19 @@ int fuse_backing_close(struct fuse_conn *fc, int backing_id)

        if (backing_id <= 0)
                goto out;
-       err = -ENOENT;
-       fb = fuse_backing_lookup(fc, backing_id);
-       if (!fb)
+       err = -EPERM;
+       if (!capable(CAP_SYS_ADMIN))
                goto out;

+ err = -EBUSY;
+ if (fb->bdev)
+ goto out;
+
+ fb = fuse_backing_id_remove(fc, backing_id);
+ if (!fb)
+ err = -ENOENT;
+ goto out_fb;
+

Thanks,
Amir.

#78 lore raw

Re: [PATCH 05/23] fuse: move the passthrough-specific code back to passthrough.c

Quoted 9 lines · Amir Goldstein
Quoted 8 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In preparation for iomap, move the passthrough-specific validation code back to passthrough.c and create a new Kconfig item for conditional compilation of backing.c. In the next patch, iomap will share the backing structures.

Quoted 13 lines
Quoted 13 lines
---
 fs/fuse/fuse_i.h      |   14 ++++++
 fs/fuse/fuse_trace.h  |   35 ++++++++++++++++
 fs/fuse/Kconfig       |    4 ++
 fs/fuse/Makefile      |    3 +
 fs/fuse/backing.c     |  106 +++++++++++++++++++++++++++++++++++++------------
 fs/fuse/dev.c         |    4 +-
 fs/fuse/inode.c       |    4 +-
 fs/fuse/passthrough.c |   28 +++++++++++++
 8 files changed, 165 insertions(+), 33 deletions(-)

<snip to the relevant parts>

Quoted 112 lines
Quoted 101 lines
fs/fuse/backing.c +42 -20
index ddb23b7400fc72..c128bed95a76b8 100644
@@ -102,46 +101,68 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
        if (!file)
                goto out;
 
-       backing_sb = file_inode(file)->i_sb;
-       res = -ELOOP;
-       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
-               goto out_fput;
-
        fb = kmalloc(sizeof(struct fuse_backing), GFP_KERNEL);
        res = -ENOMEM;
        if (!fb)
-               goto out_fput;
+               goto out_file;
 
+       /* fb now owns file */
        fb->file = file;
+       file = NULL;
        fb->cred = prepare_creds();
        refcount_set(&fb->count, 1);
 
+       /*
+        * Each _backing_open function should either:
+        *
+        * 1. Take a ref to fb if it wants the file and return 0.
+        * 2. Return 0 without taking a ref if the backing file isn't needed.
+        * 3. Return an errno explaining why it couldn't attach.
+        *
+        * If at least one subsystem bumps the reference count to open it,
+        * we'll install it into the index and return the index.  If nobody
+        * opens the file, the error code will be passed up.  EPERM is the
+        * default.
+        */
+       passthrough_res = fuse_passthrough_backing_open(fc, fb);
+
+       if (refcount_read(&fb->count) < 2) {
+               if (passthrough_res)
+                       res = passthrough_res;
+               if (!res)
+                       res = -EPERM;
+               goto out_fb;
+       }
+
        res = fuse_backing_id_alloc(fc, fb);
-       if (res < 0) {
-               fuse_backing_free(fb);
-               fb = NULL;
-       }
+       if (res < 0)
+               goto out_fb;
+
+       trace_fuse_backing_open(fc, res, fb);
 
-out:
        pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
-
+       fuse_backing_put(fb);
        return res;
 
-out_fput:
-       fput(file);
-       goto out;
+out_fb:
+       fuse_backing_free(fb);
+out_file:
+       if (file)
+               fput(file);
+out:
+       pr_debug("%s: ret=%i\n", __func__, res);
+       return res;
 }
 
 int fuse_backing_close(struct fuse_conn *fc, int backing_id)
 {
-       struct fuse_backing *fb = NULL;
-       int err;
+       struct fuse_backing *fb = NULL, *test_fb;
+       int err, passthrough_err;
 
        pr_debug("%s: backing_id=%d\n", __func__, backing_id);
 
-       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
        err = -EPERM;
-       if (!fc->passthrough || !capable(CAP_SYS_ADMIN))
+       if (!fc->passthrough)
                goto out;
 
        err = -EINVAL;
@@ -149,12 +170,45 @@ int fuse_backing_close(struct fuse_conn *fc, int backing_id)
                goto out;
 
        err = -ENOENT;
-       fb = fuse_backing_id_remove(fc, backing_id);
+       fb = fuse_backing_lookup(fc, backing_id);
        if (!fb)
                goto out;
 

fs/fuse/backing.c int fuse_backing_close(struct fuse_conn *fc, int backing_id) · line 186

Quoted 10 lines
fs/fuse/backing.c +10 -0
+       /*
+        * Each _backing_close function should either:
+        *
+        * 1. Release the ref that it took in _backing_open and return 0.
+        * 2. Don't release the ref if the backing file is busy, and return 0.
+        * 2. Return an errno explaining why it couldn't detach.
+        *
+        * If there are no more active references to the backing file, it will
+        * be closed and removed from the index.  If there are still active
+        * references to the backing file other than the one we just took, the
Quoted 7 lines

That does not look right.
The fuse_backing object can often outliive the backing_id mapping
1. fuse server attached backing fd to backing id 1
2. fuse server opens a file with passthrough to backing id 1
3. fuse inode holds a refcount to the fuse_backing object
4. fuse server closes backing id 1 mapping
5. fuse server closes file, drops last reference to fuse_backing object

Ah, I didn't account for backing files needing to outlive being registered in the index. Ok, my whole approach above is wrong. :)

Quoted 8 lines

IOW, fb->count is not about being in the index.
With your code the fuse server call in #4 above will end up leaving the fuse_backing object in the index and after #5 it will remain a dangling object in the index.

TBH, I don't understand why we need any of the complexity of two subsystems claiming the same fuse_backing object for two different purposes.

I decided I should explore your suggestion from v2:
https://lore.kernel.org/linux-fsdevel/CAOQ4uxiZTTEOs4HYD0vGi3XtihyDiQbDFXBCuGKoJyFPQv_+Lw@mail.gmail.com/
...and it didn't occur to me that, well, there's plenty of device_id address space so if some weird server has to register the same fd twice for two subsystems to use it, that's completely ok. :)

Quoted 3 lines

Also, I think that an explicit statement from the server about the purpose of the backing file is due (like your commit message implies) This could be easily done with the backing open flags member:

Hrm. That /would/ eliminate all the stupid {iomap,passthrough}_res juggling if you were only allowed to register a backing id with a single subsystem. Worst case, a hybrid iomap+passthrough fs ends up with the same file registered with multiple ids.

Yeah, let's do that.

Quoted 29 lines
fs/fuse/backing.c +1 -1
index c63990254649c..e5a675fca7505 100644
@@ -96,7 +96,7 @@ int fuse_backing_open(struct fuse_conn *fc, struct
 fuse_backing_map *map)
                goto out;
 
        res = -EINVAL;
-       if (map->flags || map->padding)
+       if (map->flags & ~FUSE_BACKING_VALID_FLAGS || map->padding)
                goto out;
 
        file = fget_raw(map->fd);
fs/fuse/backing.c +4 -2
@@ -127,8 +127,10 @@ int fuse_backing_open(struct fuse_conn *fc,
 struct fuse_backing_map *map)
         * opens the file, the error code will be passed up.  EPERM is the
         * default.
         */
-       passthrough_res = fuse_passthrough_backing_open(fc, fb);
-       iomap_res = fuse_iomap_backing_open(fc, fb);
+       if (map->flags & FUSE_BACKING_IOMAP)
+               iomap_res = fuse_iomap_backing_open(fc, fb);
+       else
+               passthrough_res = fuse_passthrough_backing_open(fc, fb);
 
        if (refcount_read(&fb->count) < 2) {

if (passthrough_res)

fs/fuse/backing.c int fuse_backing_close(struct fuse_conn *fc, int · line 201

Quoted 10 lines
fs/fuse/backing.c +4 -2
@@ -192,8 +194,10 @@ int fuse_backing_close(struct fuse_conn *fc, int
 backing_id)
         * references to the backing file other than the one we just took, the
         * error code will be passed up.  EBUSY is the default.
         */
-       passthrough_err = fuse_passthrough_backing_close(fc, fb);
-       iomap_err = fuse_iomap_backing_close(fc, fb);
+       if (fb->bdev)
+               iomap_err = fuse_iomap_backing_close(fc, fb);
+       else
+               passthrough_err = fuse_passthrough_backing_close(fc, fb);

Yes, that's a lot better, thanks. :D

Quoted 109 lines
fs/fuse/backing.c +0 -0
        if (refcount_read(&fb->count) > 1) {
                if (passthrough_err)
include/uapi/linux/fuse.h +4 -0
index 70b5530e587d4..ee81903ad2f98 100644
@@ -1148,6 +1148,10 @@ struct fuse_notify_retrieve_in {
        uint64_t        dummy4;
 };
 
+/* basic file I/O functionality through iomap */
+#define FUSE_BACKING_IOMAP             (1 << 0)
+#define FUSE_BACKING_VALID_FLAGS       (FUSE_BACKING_IOMAP)
+
 struct fuse_backing_map {
        int32_t         fd;
        uint32_t        flags;
Quoted 91 lines
fs/fuse/backing.c +24 -1
+        * error code will be passed up.  EBUSY is the default.
+        */
+       passthrough_err = fuse_passthrough_backing_close(fc, fb);
+
+       if (refcount_read(&fb->count) > 1) {
+               if (passthrough_err)
+                       err = passthrough_err;
+               if (!err)
+                       err = -EBUSY;
+               goto out_fb;
+       }
+
+       trace_fuse_backing_close(fc, backing_id, fb);
+
+       err = -ENOENT;
+       test_fb = fuse_backing_id_remove(fc, backing_id);
+       if (!test_fb)
+               goto out_fb;
+
+       WARN_ON(fb != test_fb);
+       pr_debug("%s: fb=0x%p, err=0\n", __func__, fb);
+       fuse_backing_put(fb);
+       return 0;
+out_fb:
        fuse_backing_put(fb);
-       err = 0;
 out:
        pr_debug("%s: fb=0x%p, err=%i\n", __func__, fb, err);
 
fs/fuse/dev.c +2 -2
index dbde17fff0cda9..31d9f006836ac1 100644
@@ -2623,7 +2623,7 @@ static long fuse_dev_ioctl_backing_open(struct file *file,
        if (!fud)
                return -EPERM;
 
-       if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+       if (!IS_ENABLED(CONFIG_FUSE_BACKING))
                return -EOPNOTSUPP;
 
        if (copy_from_user(&map, argp, sizeof(map)))
@@ -2640,7 +2640,7 @@ static long fuse_dev_ioctl_backing_close(struct file *file, __u32 __user *argp)
        if (!fud)
                return -EPERM;
 
-       if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+       if (!IS_ENABLED(CONFIG_FUSE_BACKING))
                return -EOPNOTSUPP;
 
        if (get_user(backing_id, argp))
fs/fuse/inode.c +2 -2
index 9448a11c828fef..1f3f91981410aa 100644
@@ -993,7 +993,7 @@ void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
        fc->name_max = FUSE_NAME_LOW_MAX;
        fc->timeout.req_timeout = 0;
 
-       if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+       if (IS_ENABLED(CONFIG_FUSE_BACKING))
                fuse_backing_files_init(fc);
 
        INIT_LIST_HEAD(&fc->mounts);
@@ -1030,7 +1030,7 @@ void fuse_conn_put(struct fuse_conn *fc)
                        WARN_ON(atomic_read(&bucket->count) != 1);
                        kfree(bucket);
                }
-               if (IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
+               if (IS_ENABLED(CONFIG_FUSE_BACKING))
                        fuse_backing_files_free(fc);
                call_rcu(&fc->rcu, delayed_release);
        }
fs/fuse/passthrough.c +8 -0
index e0b8d885bc81f3..dfc61cc4bd21af 100644
@@ -197,3 +197,31 @@ void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb)
        put_cred(ff->cred);
        ff->cred = NULL;
 }
+
+int fuse_passthrough_backing_open(struct fuse_conn *fc,
+                                 struct fuse_backing *fb)
+{
+       struct super_block *backing_sb;
+
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
+       if (!capable(CAP_SYS_ADMIN))
Quoted 8 lines

fs/fuse/passthrough.c void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb) · line 208

Quoted 1 line
fs/fuse/passthrough.c +1 -0
+               return -EPERM;

This limitation is not specific to fuse passthrough.
While the fuse passthrough use case is likely to request many fuse backing files,
the limitation is here to protect from malicious actors and the same ioctl used by the iomap fuse server can just as well open many "lsof invisible" files, so the limitation should be in the generic function.

Hrmm. Well for iomap block devices I'm not as worried because (a) you sort of need privileges to open them, (b) there aren't that many block devices, and (c) to use fuse-iomap at all you need CAP_SYS_RAWIO.

As for the invisibility problem, I wonder if I could just make /sys/block/XXX/holder have a symlink to the fuse mount? For fuse2fs we need to maintain the open fd to /dev/XXX, but I suppose that's not necessarily true for a fuse server.

Quoted 13 lines
Quoted 13 lines
fs/fuse/passthrough.c +13 -0
+
+       backing_sb = file_inode(fb->file)->i_sb;
+       if (backing_sb->s_stack_depth >= fc->max_stack_depth)
+               return -ELOOP;
+
+       fuse_backing_get(fb);
+       return 0;
+}
+
+int fuse_passthrough_backing_close(struct fuse_conn *fc,
+                                  struct fuse_backing *fb)
+{
+       /* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
Quoted 8 lines

fs/fuse/passthrough.c void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb) · line 224

Quoted 3 lines
fs/fuse/passthrough.c +3 -0
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+

Probably this comment in upstream is not very accurate because there is no harm done in closing the backing files, but sure for symmetry. Same comment as above through, unless there are reasons to relax CAP_SYS_ADMIN for file iomap, would leave this in the genetic code.

<nod>

Quoted 14 lines

And then there is not much justification left for the close helpers IMO, especially given that the implementation wrt removing from index is incorrect, I would keep it simple:

include/uapi/linux/fuse.h +2 -3
@@ -175,11 +177,19 @@ int fuse_backing_close(struct fuse_conn *fc, int
 backing_id)
        if (backing_id <= 0)
                goto out;
 
-       err = -ENOENT;
-       fb = fuse_backing_lookup(fc, backing_id);
-       if (!fb)
+       err = -EPERM;
+       if (!capable(CAP_SYS_ADMIN))

include/uapi/linux/fuse.h int fuse_backing_close(struct fuse_conn *fc, int · line 192

Quoted 10 lines
include/uapi/linux/fuse.h +8 -0
                goto out;
 
+       err = -EBUSY;
+       if (fb->bdev)
+               goto out;
+
+       fb = fuse_backing_id_remove(fc, backing_id);
+       if (!fb)
+               err = -ENOENT;
+       goto out_fb;

I'll think about this, though I don't know how much of the security checking would need to be relaxed to enable the completely locked down fuse4fs systemd service that I was imagining. My guess is that I'll have to establish iomap capability (aka CAP_SYS_RAWIO) when /dev/fuse is first opened by the mount helper, then the mount helper opens all the block devices required, and finally it passes all these fds into the contained service.

<shrug> As I said, food for thought for v5 :)

--D

Quoted 5 lines
include/uapi/linux/fuse.h +1 -0
+
 
 Thanks,

Amir.

#79 lore raw

PATCH 6/23 fuse: add an ioctl to add new iomap devices

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add an ioctl that allows fuse servers to register block devices for use with iomap. This is (for now) separate from the backing file open/close ioctl (despite using the same struct) to keep the codepaths separate.

---
 fs/fuse/fuse_i.h      |    9 +++++
 fs/fuse/fuse_trace.h  |   49 ++++++++++++++++++++++++++-
 fs/fuse/Kconfig       |    1 +
 fs/fuse/backing.c     |   19 ++++++++---
 fs/fuse/file_iomap.c  |   88 ++++++++++++++++++++++++++++++++++++++++++++-----
 fs/fuse/passthrough.c |   13 +++++++
 fs/fuse/trace.c       |    1 +
 7 files changed, 163 insertions(+), 17 deletions(-)
fs/fuse/fuse_i.h +9 -0
index 1762517a1b99c8..f4834a02d16c98 100644
@@ -100,6 +100,10 @@ struct fuse_submount_lookup {
 struct fuse_backing {
 	struct file *file;
 	struct cred *cred;
+	struct block_device *bdev;
+
+	unsigned int passthrough:1;
+	unsigned int iomap:1;
 
 	/** refcount */
 	refcount_t count;
@@ -1639,9 +1643,14 @@ static inline bool fuse_has_iomap(const struct inode *inode)
 {
 	return get_fuse_conn_c(inode)->iomap;
 }
+
+int fuse_iomap_backing_open(struct fuse_conn *fc, struct fuse_backing *fb);
+int fuse_iomap_backing_close(struct fuse_conn *fc, struct fuse_backing *fb);
 #else
 # define fuse_iomap_enabled(...)		(false)
 # define fuse_has_iomap(...)			(false)
+# define fuse_iomap_backing_open(...)		(-EOPNOTSUPP)
+# define fuse_iomap_backing_close(...)		(-EOPNOTSUPP)
 #endif
 
 #endif /* _FS_FUSE_I_H */
fs/fuse/fuse_trace.h +46 -3

84 lines not on this page · raw message

fs/fuse/Kconfig +1 -0
index ebb9a2d76b532e..1ab3d3604c07d0 100644
@@ -75,6 +75,7 @@ config FUSE_IOMAP
 	depends on FUSE_FS
 	depends on BLOCK
 	select FS_IOMAP
+	select FUSE_BACKING
 	help
 	  For supported fuseblk servers, this allows the file IO path to run
 	  through the kernel.
fs/fuse/backing.c +14 -5
index c128bed95a76b8..c63990254649ca 100644
@@ -67,16 +67,19 @@ static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
 
 static int fuse_backing_id_free(int id, void *p, void *data)
 {
+	struct fuse_conn *fc = data;
 	struct fuse_backing *fb = p;
 
 	WARN_ON_ONCE(refcount_read(&fb->count) != 1);
+
+	trace_fuse_backing_close(fc, id, fb);
 	fuse_backing_free(fb);
 	return 0;
 }
 
 void fuse_backing_files_free(struct fuse_conn *fc)
 {
-	idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
+	idr_for_each(&fc->backing_files_map, fuse_backing_id_free, fc);
 	idr_destroy(&fc->backing_files_map);
 }
 
@@ -84,12 +87,12 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
 {
 	struct file *file = NULL;
 	struct fuse_backing *fb = NULL;
-	int res, passthrough_res;
+	int res, passthrough_res, iomap_res;
 
 	pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
 
 	res = -EPERM;
-	if (!fc->passthrough)
+	if (!fc->passthrough && !fc->iomap)
 		goto out;
 
 	res = -EINVAL;
@@ -125,10 +128,13 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
 	 * default.
 	 */
 	passthrough_res = fuse_passthrough_backing_open(fc, fb);
+	iomap_res = fuse_iomap_backing_open(fc, fb);
 
 	if (refcount_read(&fb->count) < 2) {
 		if (passthrough_res)
 			res = passthrough_res;
+		if (!res && iomap_res)
+			res = iomap_res;
 		if (!res)
 			res = -EPERM;
 		goto out_fb;
@@ -157,12 +163,12 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
 int fuse_backing_close(struct fuse_conn *fc, int backing_id)
 {
 	struct fuse_backing *fb = NULL, *test_fb;
-	int err, passthrough_err;
+	int err, passthrough_err, iomap_err;
 
 	pr_debug("%s: backing_id=%d\n", __func__, backing_id);
 
 	err = -EPERM;
-	if (!fc->passthrough)
+	if (!fc->passthrough && !fc->iomap)
 		goto out;
 
 	err = -EINVAL;
@@ -187,10 +193,13 @@ int fuse_backing_close(struct fuse_conn *fc, int backing_id)
 	 * error code will be passed up.  EBUSY is the default.
 	 */
 	passthrough_err = fuse_passthrough_backing_close(fc, fb);
+	iomap_err = fuse_iomap_backing_close(fc, fb);
 
 	if (refcount_read(&fb->count) > 1) {
 		if (passthrough_err)
 			err = passthrough_err;
+		if (!err && iomap_err)
+			err = iomap_err;
 		if (!err)
 			err = -EBUSY;
 		goto out_fb;
fs/fuse/file_iomap.c +79 -9

141 lines not on this page · raw message

fs/fuse/passthrough.c +13 -0
index dfc61cc4bd21af..29de6de9f4b59b 100644
@@ -168,6 +168,11 @@ struct fuse_backing *fuse_passthrough_open(struct file *file,
 	if (!fb)
 		goto out;
 
+	if (!fb->passthrough) {
+		fuse_backing_put(fb);
+		goto out;
+	}
+
 	/* Allocate backing file per fuse file to store fuse path */
 	backing_file = backing_file_open(&file->f_path, file->f_flags,
 					 &fb->file->f_path, fb->cred);
@@ -203,6 +208,9 @@ int fuse_passthrough_backing_open(struct fuse_conn *fc,
 {
 	struct super_block *backing_sb;
 
+	if (!fc->passthrough)
+		return 0;
+
 	/* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
@@ -211,6 +219,7 @@ int fuse_passthrough_backing_open(struct fuse_conn *fc,
 	if (backing_sb->s_stack_depth >= fc->max_stack_depth)
 		return -ELOOP;
 
+	fb->passthrough = 1;
 	fuse_backing_get(fb);
 	return 0;
 }
@@ -218,10 +227,14 @@ int fuse_passthrough_backing_open(struct fuse_conn *fc,
 int fuse_passthrough_backing_close(struct fuse_conn *fc,
 				   struct fuse_backing *fb)
 {
+	if (!fb->passthrough)
+		return 0;
+
 	/* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
+	fb->passthrough = 0;
 	fuse_backing_put(fb);
 	return 0;
 }
fs/fuse/trace.c +1 -0
index 93bd72efc98cd0..3b54f639a5423e 100644
@@ -6,6 +6,7 @@
 #include "dev_uring_i.h"
 #include "fuse_i.h"
 #include "fuse_dev_i.h"
+#include "iomap_priv.h"
 
 #include <linux/pagemap.h>
 
maintainer
#80 lore raw

Re: [PATCH 06/23] fuse: add an ioctl to add new iomap devices

Quoted 6 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add an ioctl that allows fuse servers to register block devices for use with iomap. This is (for now) separate from the backing file open/close ioctl (despite using the same struct) to keep the codepaths separate.

Is it though? I'm pretty sure this commit does not add a new ioctl and reuses the same one (which is fine by me).

Quoted 221 lines
---
 fs/fuse/fuse_i.h      |    9 +++++
 fs/fuse/fuse_trace.h  |   49 ++++++++++++++++++++++++++-
 fs/fuse/Kconfig       |    1 +
 fs/fuse/backing.c     |   19 ++++++++---
 fs/fuse/file_iomap.c  |   88 ++++++++++++++++++++++++++++++++++++++++++++-----
 fs/fuse/passthrough.c |   13 +++++++
 fs/fuse/trace.c       |    1 +
 7 files changed, 163 insertions(+), 17 deletions(-)
fs/fuse/fuse_i.h +9 -0
index 1762517a1b99c8..f4834a02d16c98 100644
@@ -100,6 +100,10 @@ struct fuse_submount_lookup {
 struct fuse_backing {
        struct file *file;
        struct cred *cred;
+       struct block_device *bdev;
+
+       unsigned int passthrough:1;
+       unsigned int iomap:1;
 
        /** refcount */
        refcount_t count;
@@ -1639,9 +1643,14 @@ static inline bool fuse_has_iomap(const struct inode *inode)
 {
        return get_fuse_conn_c(inode)->iomap;
 }
+
+int fuse_iomap_backing_open(struct fuse_conn *fc, struct fuse_backing *fb);
+int fuse_iomap_backing_close(struct fuse_conn *fc, struct fuse_backing *fb);
 #else
 # define fuse_iomap_enabled(...)               (false)
 # define fuse_has_iomap(...)                   (false)
+# define fuse_iomap_backing_open(...)          (-EOPNOTSUPP)
+# define fuse_iomap_backing_close(...)         (-EOPNOTSUPP)
 #endif
 
 #endif /* _FS_FUSE_I_H */
fs/fuse/fuse_trace.h +46 -3
index 660d9b5206a175..c3671a605a32f6 100644
@@ -175,6 +175,13 @@ TRACE_EVENT(fuse_request_end,
 );
 
 #ifdef CONFIG_FUSE_BACKING
+#define FUSE_BACKING_PASSTHROUGH       (1U << 0)
+#define FUSE_BACKING_IOMAP             (1U << 1)
+
+#define FUSE_BACKING_FLAG_STRINGS \
+       { FUSE_BACKING_PASSTHROUGH,             "pass" }, \
+       { FUSE_BACKING_IOMAP,                   "iomap" }
+
 TRACE_EVENT(fuse_backing_class,
        TP_PROTO(const struct fuse_conn *fc, unsigned int idx,
                 const struct fuse_backing *fb),
@@ -184,7 +191,9 @@ TRACE_EVENT(fuse_backing_class,
        TP_STRUCT__entry(
                __field(dev_t,                  connection)
                __field(unsigned int,           idx)
+               __field(unsigned int,           flags)
                __field(unsigned long,          ino)
+               __field(dev_t,                  rdev)
        ),
 
        TP_fast_assign(
@@ -193,12 +202,23 @@ TRACE_EVENT(fuse_backing_class,
                __entry->connection     =       fc->dev;
                __entry->idx            =       idx;
                __entry->ino            =       inode->i_ino;
+               __entry->flags          =       0;
+               if (fb->passthrough)
+                       __entry->flags  |=      FUSE_BACKING_PASSTHROUGH;
+               if (fb->iomap) {
+                       __entry->rdev   =       inode->i_rdev;
+                       __entry->flags  |=      FUSE_BACKING_IOMAP;
+               } else {
+                       __entry->rdev   =       0;
+               }
        ),
 
-       TP_printk("connection %u idx %u ino 0x%lx",
+       TP_printk("connection %u idx %u flags (%s) ino 0x%lx rdev %u:%u",
                  __entry->connection,
                  __entry->idx,
-                 __entry->ino)
+                 __print_flags(__entry->flags, "|", FUSE_BACKING_FLAG_STRINGS),
+                 __entry->ino,
+                 MAJOR(__entry->rdev), MINOR(__entry->rdev))
 );
 #define DEFINE_FUSE_BACKING_EVENT(name)                \
 DEFINE_EVENT(fuse_backing_class, name,         \
@@ -210,7 +230,6 @@ DEFINE_FUSE_BACKING_EVENT(fuse_backing_close);
 #endif
 
 #if IS_ENABLED(CONFIG_FUSE_IOMAP)
-
 /* tracepoint boilerplate so we don't have to keep doing this */
 #define FUSE_IOMAP_OPFLAGS_FIELD \
                __field(unsigned,               opflags)
@@ -452,6 +471,30 @@ TRACE_EVENT(fuse_iomap_end_error,
                  __entry->written,
                  __entry->error)
 );
+
+TRACE_EVENT(fuse_iomap_dev_add,
+       TP_PROTO(const struct fuse_conn *fc,
+                const struct fuse_backing_map *map),
+
+       TP_ARGS(fc, map),
+
+       TP_STRUCT__entry(
+               __field(dev_t,                  connection)
+               __field(int,                    fd)
+               __field(unsigned int,           flags)
+       ),
+
+       TP_fast_assign(
+               __entry->connection     =       fc->dev;
+               __entry->fd             =       map->fd;
+               __entry->flags          =       map->flags;
+       ),
+
+       TP_printk("connection %u fd %d flags 0x%x",
+                 __entry->connection,
+                 __entry->fd,
+                 __entry->flags)
+);
 #endif /* CONFIG_FUSE_IOMAP */
 
 #endif /* _TRACE_FUSE_H */
fs/fuse/Kconfig +1 -0
index ebb9a2d76b532e..1ab3d3604c07d0 100644
@@ -75,6 +75,7 @@ config FUSE_IOMAP
        depends on FUSE_FS
        depends on BLOCK
        select FS_IOMAP
+       select FUSE_BACKING
        help
          For supported fuseblk servers, this allows the file IO path to run
          through the kernel.
fs/fuse/backing.c +11 -5
index c128bed95a76b8..c63990254649ca 100644
@@ -67,16 +67,19 @@ static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
 
 static int fuse_backing_id_free(int id, void *p, void *data)
 {
+       struct fuse_conn *fc = data;
        struct fuse_backing *fb = p;
 
        WARN_ON_ONCE(refcount_read(&fb->count) != 1);
+
+       trace_fuse_backing_close(fc, id, fb);
        fuse_backing_free(fb);
        return 0;
 }
 
 void fuse_backing_files_free(struct fuse_conn *fc)
 {
-       idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
+       idr_for_each(&fc->backing_files_map, fuse_backing_id_free, fc);
        idr_destroy(&fc->backing_files_map);
 }
 
@@ -84,12 +87,12 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
 {
        struct file *file = NULL;
        struct fuse_backing *fb = NULL;
-       int res, passthrough_res;
+       int res, passthrough_res, iomap_res;
 
        pr_debug("%s: fd=%d flags=0x%x\n", __func__, map->fd, map->flags);
 
        res = -EPERM;
-       if (!fc->passthrough)
+       if (!fc->passthrough && !fc->iomap)
                goto out;
 
        res = -EINVAL;
@@ -125,10 +128,13 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
         * default.
         */
        passthrough_res = fuse_passthrough_backing_open(fc, fb);
+       iomap_res = fuse_iomap_backing_open(fc, fb);
 
        if (refcount_read(&fb->count) < 2) {
                if (passthrough_res)
                        res = passthrough_res;
+               if (!res && iomap_res)
+                       res = iomap_res;
                if (!res)
                        res = -EPERM;
                goto out_fb;
@@ -157,12 +163,12 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
 int fuse_backing_close(struct fuse_conn *fc, int backing_id)
 {
        struct fuse_backing *fb = NULL, *test_fb;
-       int err, passthrough_err;
+       int err, passthrough_err, iomap_err;
 
        pr_debug("%s: backing_id=%d\n", __func__, backing_id);
 
        err = -EPERM;
-       if (!fc->passthrough)
+       if (!fc->passthrough && !fc->iomap)
                goto out;
 
        err = -EINVAL;
@@ -187,10 +193,13 @@ int fuse_backing_close(struct fuse_conn *fc, int backing_id)
         * error code will be passed up.  EBUSY is the default.
         */
        passthrough_err = fuse_passthrough_backing_close(fc, fb);

fs/fuse/backing.c int fuse_backing_close(struct fuse_conn *fc, int backing_id) · line 205

Quoted 10 lines
fs/fuse/backing.c +3 -0
+       iomap_err = fuse_iomap_backing_close(fc, fb);
 
        if (refcount_read(&fb->count) > 1) {
                if (passthrough_err)
                        err = passthrough_err;
+               if (!err && iomap_err)
+                       err = iomap_err;
                if (!err)
                        err = -EBUSY;
                goto out_fb;

Do you really think that we need to support both file passthrough and file iomap on the same fuse filesystem?

Unless you have a specific use case in mind, it looks like over design to me We could enforce either fc->passthrough or fc->iomap on init.

Put it in other words: unless you intend to test a combination of file passthrough
and file iomap, I think you should leave this configuration out of the config possibilities.

Thanks,
Amir.

#81 lore raw

Re: [PATCH 06/23] fuse: add an ioctl to add new iomap devices

Quoted 10 lines · Amir Goldstein
Quoted 6 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add an ioctl that allows fuse servers to register block devices for use with iomap. This is (for now) separate from the backing file open/close ioctl (despite using the same struct) to keep the codepaths separate.

Is it though? I'm pretty sure this commit does not add a new ioctl and reuses the same one (which is fine by me).

Oops, stale message. :(

<snip>

Quoted 10 lines
Quoted 10 lines
fs/fuse/backing.c +1 -0
index c128bed95a76b8..c63990254649ca 100644
@@ -187,10 +193,13 @@ int fuse_backing_close(struct fuse_conn *fc, int backing_id)
         * error code will be passed up.  EBUSY is the default.
         */
        passthrough_err = fuse_passthrough_backing_close(fc, fb);
+       iomap_err = fuse_iomap_backing_close(fc, fb);
 
Quoted 11 lines

fs/fuse/backing.c int fuse_backing_close(struct fuse_conn *fc, int backing_id) · line 205

Quoted 8 lines
fs/fuse/backing.c +2 -0
        if (refcount_read(&fb->count) > 1) {
                if (passthrough_err)
                        err = passthrough_err;
+               if (!err && iomap_err)
+                       err = iomap_err;
                if (!err)
                        err = -EBUSY;
                goto out_fb;

Do you really think that we need to support both file passthrough and file iomap on the same fuse filesystem?

Probably not.

Quoted 7 lines

Unless you have a specific use case in mind, it looks like over design to me We could enforce either fc->passthrough or fc->iomap on init.

Put it in other words: unless you intend to test a combination of file passthrough
and file iomap, I think you should leave this configuration out of the config possibilities.

Nah, one subsystem per backing device_id is ok with me. If someday someone builds a hybrid filesystem then ... hopefully they don't need more than INT_MAX backing files to be in the index.

--D

Quoted 3 lines

Thanks,
Amir.

#82 lore raw

PATCH 7/23 fuse: flush events and send FUSE_SYNCFS and FUSE_DESTROY on unmount

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

At unmount time, there are a few things that we need to ask the fuse server to do.

First, we need to flush queued events to userspace to give the fuse server a chance to process the events. This is how we make sure that the server processes FUSE_RELEASE events before the connection goes down.

Second, to ensure that all those metadata updates are persisted to disk before tell the fuse server to destroy itself, send FUSE_SYNCFS after waiting for the queued events.

Finally, we need to send FUSE_DESTROY to the fuse server so that it closes the filesystem and the device fds before unmount returns. That way, a script that does something like "umount /dev/sda ; e2fsck -fn /dev/sda" will not fail the e2fsck because the fd closure races with e2fsck startup. Obviously, we need to wait for FUSE_SYNCFS.

This is a major behavior change and who knows what might break existing code, so we hide it behind iomap mode.

---
 fs/fuse/fuse_i.h     |    7 +++++++
 fs/fuse/file_iomap.c |   29 +++++++++++++++++++++++++++++
 fs/fuse/inode.c      |    9 +++++++--
 3 files changed, 43 insertions(+), 2 deletions(-)
fs/fuse/fuse_i.h +7 -0
index f4834a02d16c98..6a155bdd389af6 100644
@@ -1364,6 +1364,9 @@ int fuse_init_fs_context_submount(struct fs_context *fsc);
  */
 void fuse_conn_destroy(struct fuse_mount *fm);
 
+/* Send the FUSE_DESTROY command. */
+void fuse_send_destroy(struct fuse_mount *fm);
+
 /* Drop the connection and free the fuse mount */
 void fuse_mount_destroy(struct fuse_mount *fm);
 
@@ -1646,11 +1649,15 @@ static inline bool fuse_has_iomap(const struct inode *inode)
 
 int fuse_iomap_backing_open(struct fuse_conn *fc, struct fuse_backing *fb);
 int fuse_iomap_backing_close(struct fuse_conn *fc, struct fuse_backing *fb);
+void fuse_iomap_mount(struct fuse_mount *fm);
+void fuse_iomap_unmount(struct fuse_mount *fm);
 #else
 # define fuse_iomap_enabled(...)		(false)
 # define fuse_has_iomap(...)			(false)
 # define fuse_iomap_backing_open(...)		(-EOPNOTSUPP)
 # define fuse_iomap_backing_close(...)		(-EOPNOTSUPP)
+# define fuse_iomap_mount(...)			((void)0)
+# define fuse_iomap_unmount(...)		((void)0)
 #endif
 
 #endif /* _FS_FUSE_I_H */
fs/fuse/file_iomap.c +29 -0
index 154c99399f48d2..6e0e222da3046c 100644
@@ -593,3 +593,32 @@ int fuse_iomap_backing_close(struct fuse_conn *fc, struct fuse_backing *fb)
 	/* We only support closing iomap block devices at unmount */
 	return -EBUSY;
 }
+
+void fuse_iomap_mount(struct fuse_mount *fm)
+{
+	struct fuse_conn *fc = fm->fc;
+
+	/*
+	 * Enable syncfs for iomap fuse servers so that we can send a final
+	 * flush at unmount time.  This also means that we can support
+	 * freeze/thaw properly.
+	 */
+	fc->sync_fs = true;
+}
+
+void fuse_iomap_unmount(struct fuse_mount *fm)
+{
+	struct fuse_conn *fc = fm->fc;
+
+	/*
+	 * Flush all pending commands, syncfs, flush that, and send a destroy
+	 * command.  This gives the fuse server a chance to process all the
+	 * pending releases, write the last bits of metadata changes to disk,
+	 * and close the iomap block devices before we return from the umount
+	 * call.  The caller already flushed previously pending requests, so we
+	 * only need the flush to wait for syncfs.
+	 */
+	sync_filesystem(fm->sb);
+	fuse_flush_requests_and_wait(fc, secs_to_jiffies(60));
+	fuse_send_destroy(fm);
+}
fs/fuse/inode.c +7 -2
index 1f3f91981410aa..3274ee1c31b62b 100644
@@ -621,7 +621,7 @@ static void fuse_umount_begin(struct super_block *sb)
 		retire_super(sb);
 }
 
-static void fuse_send_destroy(struct fuse_mount *fm)
+void fuse_send_destroy(struct fuse_mount *fm)
 {
 	if (fm->fc->conn_init) {
 		FUSE_ARGS(args);
@@ -1457,6 +1457,9 @@ static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
 
 		init_server_timeout(fc, timeout);
 
+		if (fc->iomap)
+			fuse_iomap_mount(fm);
+
 		fm->sb->s_bdi->ra_pages =
 				min(fm->sb->s_bdi->ra_pages, ra_pages);
 		fc->minor = arg->minor;
@@ -2055,7 +2058,9 @@ void fuse_conn_destroy(struct fuse_mount *fm)
 	struct fuse_conn *fc = fm->fc;
 
 	fuse_flush_requests_and_wait(fc, secs_to_jiffies(30));
-	if (fc->destroy)
+	if (fc->iomap)
+		fuse_iomap_unmount(fm);
+	else if (fc->destroy)
 		fuse_send_destroy(fm);
 
 	fuse_abort_conn(fc);
#83 lore raw

PATCH 8/23 fuse: implement basic iomap reporting such as FIEMAP and SEEK_{DATA,HOLE}

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Implement the basic file mapping reporting functions like FIEMAP, BMAP, and SEEK_DATA/HOLE.

---
 fs/fuse/fuse_i.h     |    8 ++++++
 fs/fuse/fuse_trace.h |   46 ++++++++++++++++++++++++++++++++
 fs/fuse/dir.c        |    1 +
 fs/fuse/file.c       |   13 +++++++++
 fs/fuse/file_iomap.c |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 139 insertions(+)
fs/fuse/fuse_i.h +8 -0
index 6a155bdd389af6..e7dc8229bcc5e7 100644
@@ -1651,6 +1651,11 @@ int fuse_iomap_backing_open(struct fuse_conn *fc, struct fuse_backing *fb);
 int fuse_iomap_backing_close(struct fuse_conn *fc, struct fuse_backing *fb);
 void fuse_iomap_mount(struct fuse_mount *fm);
 void fuse_iomap_unmount(struct fuse_mount *fm);
+
+int fuse_iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
+		      u64 start, u64 length);
+loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence);
+sector_t fuse_iomap_bmap(struct address_space *mapping, sector_t block);
 #else
 # define fuse_iomap_enabled(...)		(false)
 # define fuse_has_iomap(...)			(false)
@@ -1658,6 +1663,9 @@ void fuse_iomap_unmount(struct fuse_mount *fm);
 # define fuse_iomap_backing_close(...)		(-EOPNOTSUPP)
 # define fuse_iomap_mount(...)			((void)0)
 # define fuse_iomap_unmount(...)		((void)0)
+# define fuse_iomap_fiemap			NULL
+# define fuse_iomap_lseek(...)			(-ENOSYS)
+# define fuse_iomap_bmap(...)			(-ENOSYS)
 #endif
 
 #endif /* _FS_FUSE_I_H */
fs/fuse/fuse_trace.h +46 -0

52 lines not on this page · raw message

fs/fuse/dir.c +1 -0
index 8e922dcadb8675..4ea763699c1bae 100644
@@ -2298,6 +2298,7 @@ static const struct inode_operations fuse_common_inode_operations = {
 	.set_acl	= fuse_set_acl,
 	.fileattr_get	= fuse_fileattr_get,
 	.fileattr_set	= fuse_fileattr_set,
+	.fiemap		= fuse_iomap_fiemap,
 };
 
 static const struct inode_operations fuse_symlink_inode_operations = {
fs/fuse/file.c +13 -0
index 0ba2b62e06679e..54432cf0be82ba 100644
@@ -2516,6 +2516,12 @@ static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
 	struct fuse_bmap_out outarg;
 	int err;
 
+	if (fuse_has_iomap(inode)) {
+		sector_t alt_sec = fuse_iomap_bmap(mapping, block);
+		if (alt_sec > 0)
+			return alt_sec;
+	}
+
 	if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
 		return 0;
 
@@ -2551,6 +2557,13 @@ static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
 	struct fuse_lseek_out outarg;
 	int err;
 
+	if (fuse_has_iomap(inode)) {
+		loff_t alt_pos = fuse_iomap_lseek(file, offset, whence);
+
+		if (alt_pos >= 0 || (alt_pos < 0 && alt_pos != -ENOSYS))
+			return alt_pos;
+	}
+
 	if (fm->fc->no_lseek)
 		goto fallback;
 
fs/fuse/file_iomap.c +71 -0

80 lines not on this page · raw message

#84 lore raw

PATCH 9/23 fuse: implement direct IO with iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Start implementing the fuse-iomap file I/O paths by adding direct I/O support and all the signalling flags that come with it. Buffered I/O is much more complicated, so we leave that to a subsequent patch.

---
 fs/fuse/fuse_i.h          |   50 +++++++
 fs/fuse/fuse_trace.h      |  186 +++++++++++++++++++++++++
 include/uapi/linux/fuse.h |   29 ++++
 fs/fuse/dir.c             |    7 +
 fs/fuse/file.c            |   17 ++
 fs/fuse/file_iomap.c      |  338 +++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/inode.c           |    2 
 fs/fuse/trace.c           |    1 
 8 files changed, 624 insertions(+), 6 deletions(-)
fs/fuse/fuse_i.h +49 -1
index e7dc8229bcc5e7..1415db4ebf47b1 100644
@@ -234,6 +234,8 @@ enum {
 	FUSE_I_BTIME,
 	/* Wants or already has page cache IO */
 	FUSE_I_CACHE_IO_MODE,
+	/* Use iomap for this inode */
+	FUSE_I_IOMAP,
 };
 
 struct fuse_conn;
@@ -624,6 +626,16 @@ struct fuse_sync_bucket {
 	struct rcu_head rcu;
 };
 
+#ifdef CONFIG_FUSE_IOMAP
+struct fuse_iomap_conn {
+	/* fuse server doesn't implement iomap_end */
+	unsigned int no_end:1;
+
+	/* fuse server doesn't implement iomap_ioend */
+	unsigned int no_ioend:1;
+};
+#endif
+
 /**
  * A Fuse connection.
  *
@@ -903,7 +915,10 @@ struct fuse_conn {
 	/* Is link not implemented by fs? */
 	unsigned int no_link:1;
 
-	/* Use fs/iomap for FIEMAP and SEEK_{DATA,HOLE} file operations */
+	/*
+	 * Use fs/iomap for FIEMAP and SEEK_{DATA,HOLE} file operations and
+	 * direct I/O.
+	 */
 	unsigned int iomap:1;
 
 	/* Use io_uring for communication */
@@ -967,6 +982,11 @@ struct fuse_conn {
 	struct idr backing_files_map;
 #endif
 
+#ifdef CONFIG_FUSE_IOMAP
+	/** iomap information */
+	struct fuse_iomap_conn iomap_conn;
+#endif
+
 #ifdef CONFIG_FUSE_IO_URING
 	/**  uring connection information*/
 	struct fuse_ring *ring;
@@ -1656,6 +1676,27 @@ int fuse_iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 		      u64 start, u64 length);
 loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence);
 sector_t fuse_iomap_bmap(struct address_space *mapping, sector_t block);
+
+void fuse_iomap_open(struct inode *inode, struct file *file);
+
+void fuse_iomap_init_inode(struct inode *inode, unsigned attr_flags);
+void fuse_iomap_evict_inode(struct inode *inode);
+
+static inline bool fuse_inode_has_iomap(const struct inode *inode)
+{
+	const struct fuse_inode *fi = get_fuse_inode_c(inode);
+
+	return test_bit(FUSE_I_IOMAP, &fi->state);
+}
+
+static inline bool fuse_want_iomap_directio(const struct kiocb *iocb)
+{
+	return (iocb->ki_flags & IOCB_DIRECT) &&
+		fuse_inode_has_iomap(file_inode(iocb->ki_filp));
+}
+
+ssize_t fuse_iomap_direct_read(struct kiocb *iocb, struct iov_iter *to);
+ssize_t fuse_iomap_direct_write(struct kiocb *iocb, struct iov_iter *from);
 #else
 # define fuse_iomap_enabled(...)		(false)
 # define fuse_has_iomap(...)			(false)
@@ -1666,6 +1707,13 @@ sector_t fuse_iomap_bmap(struct address_space *mapping, sector_t block);
 # define fuse_iomap_fiemap			NULL
 # define fuse_iomap_lseek(...)			(-ENOSYS)
 # define fuse_iomap_bmap(...)			(-ENOSYS)
+# define fuse_iomap_open(...)			((void)0)
+# define fuse_iomap_init_inode(...)		((void)0)
+# define fuse_iomap_evict_inode(...)		((void)0)
+# define fuse_inode_has_iomap(...)		(false)
+# define fuse_want_iomap_directio(...)		(false)
+# define fuse_iomap_direct_read(...)		(-ENOSYS)
+# define fuse_iomap_direct_write(...)		(-ENOSYS)
 #endif
 
 #endif /* _FS_FUSE_I_H */
fs/fuse/fuse_trace.h +186 -0

210 lines not on this page · raw message

include/uapi/linux/fuse.h +27 -2
index 3b9e337119d792..10882fa1452e49 100644
@@ -238,7 +238,8 @@
  *
  *  7.99
  *  - add FUSE_IOMAP and iomap_{begin,end,ioend} handlers for FIEMAP and
- *    SEEK_{DATA,HOLE}
+ *    SEEK_{DATA,HOLE}, and direct I/O
+ *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  */
 
 #ifndef _LINUX_FUSE_H
@@ -448,7 +449,7 @@ struct fuse_file_lock {
  * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests.
  *			 init_out.request_timeout contains the timeout (in secs)
  * FUSE_IOMAP: Client supports iomap for FIEMAP and SEEK_{DATA,HOLE} file
- *	       operations.
+ *	       operations and direct I/O.
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
@@ -580,9 +581,11 @@ struct fuse_file_lock {
  *
  * FUSE_ATTR_SUBMOUNT: Object is a submount root
  * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode
+ * FUSE_ATTR_IOMAP: Use iomap for this inode
  */
 #define FUSE_ATTR_SUBMOUNT      (1 << 0)
 #define FUSE_ATTR_DAX		(1 << 1)
+#define FUSE_ATTR_IOMAP		(1 << 2)
 
 /**
  * Open flags
@@ -665,6 +668,7 @@ enum fuse_opcode {
 	FUSE_TMPFILE		= 51,
 	FUSE_STATX		= 52,
 
+	FUSE_IOMAP_IOEND	= 4093,
 	FUSE_IOMAP_BEGIN	= 4094,
 	FUSE_IOMAP_END		= 4095,
 
@@ -1380,4 +1384,25 @@ struct fuse_iomap_end_in {
 	struct fuse_iomap_io	map;
 };
 
+/* out of place write extent */
+#define FUSE_IOMAP_IOEND_SHARED		(1U << 0)
+/* unwritten extent */
+#define FUSE_IOMAP_IOEND_UNWRITTEN	(1U << 1)
+/* don't merge into previous ioend */
+#define FUSE_IOMAP_IOEND_BOUNDARY	(1U << 2)
+/* is direct I/O */
+#define FUSE_IOMAP_IOEND_DIRECT		(1U << 3)
+/* is append ioend */
+#define FUSE_IOMAP_IOEND_APPEND		(1U << 4)
+
+struct fuse_iomap_ioend_in {
+	uint32_t ioendflags;	/* FUSE_IOMAP_IOEND_* */
+	int32_t error;		/* negative errno or 0 */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t new_addr;	/* disk offset of new mapping, in bytes */
+	uint32_t written;	/* bytes processed */
+	uint32_t reserved1;	/* zero */
+};
+
 #endif /* _LINUX_FUSE_H */
fs/fuse/dir.c +7 -0
index 4ea763699c1bae..04e1242014c9c9 100644
@@ -712,6 +712,10 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,
 	if (err)
 		goto out_acl_release;
 	fuse_dir_changed(dir);
+
+	if (fuse_has_iomap(inode))
+		fuse_iomap_open(inode, file);
+
 	err = generic_file_open(inode, file);
 	if (!err) {
 		file->private_data = ff;
@@ -1749,6 +1753,9 @@ static int fuse_dir_open(struct inode *inode, struct file *file)
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_has_iomap(inode))
+		fuse_iomap_open(inode, file);
+
 	err = generic_file_open(inode, file);
 	if (err)
 		return err;
fs/fuse/file.c +17 -0
index 54432cf0be82ba..f01a9346d4f8bc 100644
@@ -245,6 +245,9 @@ static int fuse_open(struct inode *inode, struct file *file)
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_has_iomap(inode))
+		fuse_iomap_open(inode, file);
+
 	err = generic_file_open(inode, file);
 	if (err)
 		return err;
@@ -1751,10 +1754,17 @@ static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 	struct file *file = iocb->ki_filp;
 	struct fuse_file *ff = file->private_data;
 	struct inode *inode = file_inode(file);
+	ssize_t ret;
 
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_want_iomap_directio(iocb)) {
+		ret = fuse_iomap_direct_read(iocb, to);
+		if (ret != -ENOSYS)
+			return ret;
+	}
+
 	if (FUSE_IS_DAX(inode))
 		return fuse_dax_read_iter(iocb, to);
 
@@ -1776,6 +1786,12 @@ static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_want_iomap_directio(iocb)) {
+		ssize_t ret = fuse_iomap_direct_write(iocb, from);
+		if (ret != -ENOSYS)
+			return ret;
+	}
+
 	if (FUSE_IS_DAX(inode))
 		return fuse_dax_write_iter(iocb, from);
 
@@ -3139,4 +3155,5 @@ void fuse_init_file_inode(struct inode *inode, unsigned int flags)
 
 	if (IS_ENABLED(CONFIG_FUSE_DAX))
 		fuse_dax_inode_init(inode, flags);
+	fuse_iomap_init_inode(inode, flags);
 }
fs/fuse/file_iomap.c +335 -3

378 lines not on this page · raw message

fs/fuse/inode.c +2 -0
index 3274ee1c31b62b..3d54fabbd64b0c 100644
@@ -197,6 +197,8 @@ static void fuse_evict_inode(struct inode *inode)
 		WARN_ON(!list_empty(&fi->write_files));
 		WARN_ON(!list_empty(&fi->queued_writes));
 	}
+
+	fuse_iomap_evict_inode(inode);
 }
 
 static int fuse_reconfigure(struct fs_context *fsc)
fs/fuse/trace.c +1 -0
index 3b54f639a5423e..9de407148c867d 100644
@@ -9,6 +9,7 @@
 #include "iomap_priv.h"
 
 #include <linux/pagemap.h>
+#include <linux/iomap.h>
 
 #define CREATE_TRACE_POINTS
 #include "fuse_trace.h"
#85 lore raw

PATCH 10/23 fuse: implement buffered IO with iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Implement pagecache IO with iomap, complete with hooks into truncate and fallocate so that the fuse server needn't implement disk block zeroing of post-EOF and unaligned punch/zero regions.

---
 fs/fuse/fuse_i.h          |   36 ++
 fs/fuse/fuse_trace.h      |  268 +++++++++++++++++
 include/uapi/linux/fuse.h |    4 
 fs/fuse/dir.c             |   23 +
 fs/fuse/file.c            |   84 ++++-
 fs/fuse/file_iomap.c      |  718 ++++++++++++++++++++++++++++++++++++++++++++-
 fs/fuse/inode.c           |   10 +
 7 files changed, 1113 insertions(+), 30 deletions(-)
fs/fuse/fuse_i.h +34 -2
index 1415db4ebf47b1..74fb5971f8fec7 100644
@@ -169,6 +169,13 @@ struct fuse_inode {
 
 			/* waitq for direct-io completion */
 			wait_queue_head_t direct_io_waitq;
+
+#ifdef CONFIG_FUSE_IOMAP
+			/* pending io completions */
+			spinlock_t ioend_lock;
+			struct work_struct ioend_work;
+			struct list_head ioend_list;
+#endif
 		};
 
 		/* readdir cache (directory only) */
@@ -916,8 +923,8 @@ struct fuse_conn {
 	unsigned int no_link:1;
 
 	/*
-	 * Use fs/iomap for FIEMAP and SEEK_{DATA,HOLE} file operations and
-	 * direct I/O.
+	 * Use fs/iomap for FIEMAP and SEEK_{DATA,HOLE} file operations,
+	 * buffered I/O, and direct I/O.
 	 */
 	unsigned int iomap:1;
 
@@ -1659,6 +1666,8 @@ void fuse_iomap_sysfs_cleanup(struct kobject *kobj);
 # define fuse_iomap_sysfs_cleanup(...)		((void)0)
 #endif
 
+sector_t fuse_bmap(struct address_space *mapping, sector_t block);
+
 #if IS_ENABLED(CONFIG_FUSE_IOMAP)
 bool fuse_iomap_enabled(void);
 
@@ -1697,6 +1706,21 @@ static inline bool fuse_want_iomap_directio(const struct kiocb *iocb)
 
 ssize_t fuse_iomap_direct_read(struct kiocb *iocb, struct iov_iter *to);
 ssize_t fuse_iomap_direct_write(struct kiocb *iocb, struct iov_iter *from);
+
+static inline bool fuse_want_iomap_buffered_io(const struct kiocb *iocb)
+{
+	return fuse_inode_has_iomap(file_inode(iocb->ki_filp));
+}
+
+int fuse_iomap_mmap(struct file *file, struct vm_area_struct *vma);
+ssize_t fuse_iomap_buffered_read(struct kiocb *iocb, struct iov_iter *to);
+ssize_t fuse_iomap_buffered_write(struct kiocb *iocb, struct iov_iter *from);
+int fuse_iomap_setsize_start(struct inode *inode, loff_t newsize);
+void fuse_iomap_set_i_blkbits(struct inode *inode, u8 new_blkbits);
+int fuse_iomap_fallocate(struct file *file, int mode, loff_t offset,
+			 loff_t length, loff_t new_size);
+int fuse_iomap_flush_unmap_range(struct inode *inode, loff_t pos,
+				 loff_t endpos);
 #else
 # define fuse_iomap_enabled(...)		(false)
 # define fuse_has_iomap(...)			(false)
@@ -1714,6 +1738,14 @@ ssize_t fuse_iomap_direct_write(struct kiocb *iocb, struct iov_iter *from);
 # define fuse_want_iomap_directio(...)		(false)
 # define fuse_iomap_direct_read(...)		(-ENOSYS)
 # define fuse_iomap_direct_write(...)		(-ENOSYS)
+# define fuse_want_iomap_buffered_io(...)	(false)
+# define fuse_iomap_mmap(...)			(-ENOSYS)
+# define fuse_iomap_buffered_read(...)		(-ENOSYS)
+# define fuse_iomap_buffered_write(...)		(-ENOSYS)
+# define fuse_iomap_setsize_start(...)		(-ENOSYS)
+# define fuse_iomap_set_i_blkbits(...)		((void)0)
+# define fuse_iomap_fallocate(...)		(-ENOSYS)
+# define fuse_iomap_flush_unmap_range(...)	(-ENOSYS)
 #endif
 
 #endif /* _FS_FUSE_I_H */
fs/fuse/fuse_trace.h +268 -0

298 lines not on this page · raw message

include/uapi/linux/fuse.h +2 -2
index 10882fa1452e49..12d15c186256f3 100644
@@ -238,7 +238,7 @@
  *
  *  7.99
  *  - add FUSE_IOMAP and iomap_{begin,end,ioend} handlers for FIEMAP and
- *    SEEK_{DATA,HOLE}, and direct I/O
+ *    SEEK_{DATA,HOLE}, buffered I/O, and direct I/O
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  */
 
@@ -449,7 +449,7 @@ struct fuse_file_lock {
  * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests.
  *			 init_out.request_timeout contains the timeout (in secs)
  * FUSE_IOMAP: Client supports iomap for FIEMAP and SEEK_{DATA,HOLE} file
- *	       operations and direct I/O.
+ *	       operations, buffered I/O, and direct I/O.
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
fs/fuse/dir.c +22 -1
index 04e1242014c9c9..6195ac3232ff22 100644
@@ -2025,7 +2025,10 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		is_truncate = true;
 	}
 
-	if (FUSE_IS_DAX(inode) && is_truncate) {
+	if (fuse_inode_has_iomap(inode) && is_truncate) {
+		filemap_invalidate_lock(mapping);
+		fault_blocked = true;
+	} else if (FUSE_IS_DAX(inode) && is_truncate) {
 		filemap_invalidate_lock(mapping);
 		fault_blocked = true;
 		err = fuse_dax_break_layouts(inode, 0, -1);
@@ -2040,6 +2043,18 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		WARN_ON(!(attr->ia_valid & ATTR_SIZE));
 		WARN_ON(attr->ia_size != 0);
 		if (fc->atomic_o_trunc) {
+			if (fuse_inode_has_iomap(inode)) {
+				/*
+				 * fuse_open already set the size to zero and
+				 * truncated the pagecache, and we've since
+				 * cycled the inode locks.  Another thread
+				 * could have performed an appending write, so
+				 * we don't want to touch the file further.
+				 */
+				filemap_invalidate_unlock(mapping);
+				return 0;
+			}
+
 			/*
 			 * No need to send request to userspace, since actual
 			 * truncation has already been done by OPEN.  But still
@@ -2070,6 +2085,12 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
 		if (trust_local_cmtime && attr->ia_size != inode->i_size)
 			attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
+
+		if (fuse_inode_has_iomap(inode)) {
+			err = fuse_iomap_setsize_start(inode, attr->ia_size);
+			if (err)
+				goto error;
+		}
 	}
 
 	memset(&inarg, 0, sizeof(inarg));
fs/fuse/file.c +71 -13

166 lines not on this page · raw message

fs/fuse/file_iomap.c +708 -10

751 lines not on this page · raw message

fs/fuse/inode.c +8 -2
index 3d54fabbd64b0c..72ba71c609a248 100644
@@ -231,6 +231,7 @@ void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
 {
 	struct fuse_conn *fc = get_fuse_conn(inode);
 	struct fuse_inode *fi = get_fuse_inode(inode);
+	u8 new_blkbits;
 
 	lockdep_assert_held(&fi->lock);
 
@@ -295,9 +296,14 @@ void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
 	}
 
 	if (attr->blksize != 0)
-		inode->i_blkbits = ilog2(attr->blksize);
+		new_blkbits = ilog2(attr->blksize);
 	else
-		inode->i_blkbits = inode->i_sb->s_blocksize_bits;
+		new_blkbits = inode->i_sb->s_blocksize_bits;
+
+	if (fuse_inode_has_iomap(inode))
+		fuse_iomap_set_i_blkbits(inode, new_blkbits);
+	else
+		inode->i_blkbits = new_blkbits;
 
 	/*
 	 * Don't set the sticky bit in i_mode, unless we want the VFS
#86 lore raw

PATCH 11/23 fuse: enable caching of timestamps

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Cache the timestamps in the kernel so that the kernel sends FUSE_SETATTR calls to the fuse server after writes, because the iomap infrastructure won't do that for us.

---
 fs/fuse/dir.c        |    3 ++-
 fs/fuse/file.c       |   20 ++++++++++++++------
 fs/fuse/file_iomap.c |    6 ++++++
 fs/fuse/inode.c      |   13 +++++++------
 4 files changed, 29 insertions(+), 13 deletions(-)
fs/fuse/dir.c +2 -1
index 6195ac3232ff22..07aa338208b5cc 100644
@@ -2005,7 +2005,8 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	struct fuse_setattr_in inarg;
 	struct fuse_attr_out outarg;
 	bool is_truncate = false;
-	bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode);
+	bool is_wb = S_ISREG(inode->i_mode) &&
+			(fuse_inode_has_iomap(inode) || fc->writeback_cache);
 	loff_t oldsize;
 	int err;
 	bool trust_local_cmtime = is_wb;
fs/fuse/file.c +14 -6
index 43f3e2d4eacb8e..825b7ac9158d08 100644
@@ -239,7 +239,8 @@ static int fuse_open(struct inode *inode, struct file *file)
 	struct fuse_file *ff;
 	int err;
 	bool is_truncate = (file->f_flags & O_TRUNC) && fc->atomic_o_trunc;
-	bool is_wb_truncate = is_truncate && fc->writeback_cache;
+	bool is_wb_truncate = is_truncate && (fuse_inode_has_iomap(inode) ||
+					      fc->writeback_cache);
 	bool dax_truncate = is_truncate && FUSE_IS_DAX(inode);
 
 	if (fuse_is_bad(inode))
@@ -459,7 +460,9 @@ static int fuse_flush(struct file *file, fl_owner_t id)
 	if (fuse_is_bad(inode))
 		return -EIO;
 
-	if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache)
+	if ((ff->open_flags & FOPEN_NOFLUSH) &&
+	    !fm->fc->writeback_cache &&
+	    !fuse_inode_has_iomap(inode))
 		return 0;
 
 	err = write_inode_now(inode, 1);
@@ -495,7 +498,7 @@ static int fuse_flush(struct file *file, fl_owner_t id)
 	 * In memory i_blocks is not maintained by fuse, if writeback cache is
 	 * enabled, i_blocks from cached attr may not be accurate.
 	 */
-	if (!err && fm->fc->writeback_cache)
+	if (!err && (fuse_inode_has_iomap(inode) || fm->fc->writeback_cache))
 		fuse_invalidate_attr_mask(inode, STATX_BLOCKS);
 	return err;
 }
@@ -793,8 +796,10 @@ static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
 	 * If writeback_cache is enabled, a short read means there's a hole in
 	 * the file.  Some data after the hole is in page cache, but has not
 	 * reached the client fs yet.  So the hole is not present there.
+	 * If iomap is enabled, a short read means we hit EOF so there's
+	 * nothing to adjust.
 	 */
-	if (!fc->writeback_cache) {
+	if (!fc->writeback_cache && !fuse_inode_has_iomap(inode)) {
 		loff_t pos = folio_pos(ap->folios[0]) + num_read;
 		fuse_read_update_size(inode, pos, attr_ver);
 	}
@@ -1409,6 +1414,8 @@ static int fuse_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 			    unsigned int flags, struct iomap *iomap,
 			    struct iomap *srcmap)
 {
+	WARN_ON(fuse_inode_has_iomap(inode));
+
 	iomap->type = IOMAP_MAPPED;
 	iomap->length = length;
 	iomap->offset = offset;
@@ -1976,7 +1983,7 @@ static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
 	 * Do this only if writeback_cache is not enabled.  If writeback_cache
 	 * is enabled, we trust local ctime/mtime.
 	 */
-	if (!fc->writeback_cache)
+	if (!fc->writeback_cache && !fuse_inode_has_iomap(inode))
 		fuse_invalidate_attr_mask(inode, FUSE_STATX_MODIFY);
 	spin_lock(&fi->lock);
 	fi->writectr--;
@@ -3057,7 +3064,8 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
 	ssize_t err;
 	/* mark unstable when write-back is not used, and file_out gets
 	 * extended */
-	bool is_unstable = (!fc->writeback_cache) &&
+	bool is_unstable = (!fc->writeback_cache &&
+			    !fuse_inode_has_iomap(inode_out)) &&
 			   ((pos_out + len) > inode_out->i_size);
 
 	if (fc->no_copy_file_range)
fs/fuse/file_iomap.c +6 -0
index ff9298de193a26..6aa9269b504713 100644
@@ -1389,6 +1389,12 @@ static inline void fuse_inode_set_iomap(struct inode *inode)
 
 	ASSERT(fuse_has_iomap(inode));
 
+	/*
+	 * Manage timestamps ourselves, don't make the fuse server do it.  This
+	 * is critical for mtime updates to work correctly with page_mkwrite.
+	 */
+	inode->i_flags &= ~S_NOCMTIME;
+	inode->i_flags &= ~S_NOATIME;
 	inode->i_data.a_ops = &fuse_iomap_aops;
 
 	INIT_WORK(&fi->ioend_work, fuse_iomap_end_io);
fs/fuse/inode.c +7 -6
index 72ba71c609a248..b08b1961d03b3e 100644
@@ -331,10 +331,11 @@ u32 fuse_get_cache_mask(struct inode *inode)
 {
 	struct fuse_conn *fc = get_fuse_conn(inode);
 
-	if (!fc->writeback_cache || !S_ISREG(inode->i_mode))
-		return 0;
+	if (S_ISREG(inode->i_mode) &&
+	    (fuse_inode_has_iomap(inode) || fc->writeback_cache))
+		return STATX_MTIME | STATX_CTIME | STATX_SIZE;
 
-	return STATX_MTIME | STATX_CTIME | STATX_SIZE;
+	return 0;
 }
 
 static void fuse_change_attributes_i(struct inode *inode, struct fuse_attr *attr,
@@ -349,9 +350,9 @@ static void fuse_change_attributes_i(struct inode *inode, struct fuse_attr *attr
 
 	spin_lock(&fi->lock);
 	/*
-	 * In case of writeback_cache enabled, writes update mtime, ctime and
-	 * may update i_size.  In these cases trust the cached value in the
-	 * inode.
+	 * In case of writeback_cache or iomap enabled, writes update mtime,
+	 * ctime and may update i_size.  In these cases trust the cached value
+	 * in the inode.
 	 */
 	cache_mask = fuse_get_cache_mask(inode);
 	if (cache_mask & STATX_SIZE)
#87 lore raw

PATCH 12/23 fuse: implement large folios for iomap pagecache files

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Use large folios when we're using iomap.

---
 fs/fuse/file_iomap.c |    6 ++++++
 1 file changed, 6 insertions(+)
fs/fuse/file_iomap.c +6 -0
index 6aa9269b504713..92cc85b5b8a8b5 100644
@@ -1386,6 +1386,7 @@ static const struct address_space_operations fuse_iomap_aops = {
 static inline void fuse_inode_set_iomap(struct inode *inode)
 {
 	struct fuse_inode *fi = get_fuse_inode(inode);
+	unsigned int min_order = 0;
 
 	ASSERT(fuse_has_iomap(inode));
 
@@ -1400,6 +1401,11 @@ static inline void fuse_inode_set_iomap(struct inode *inode)
 	INIT_WORK(&fi->ioend_work, fuse_iomap_end_io);
 	INIT_LIST_HEAD(&fi->ioend_list);
 	spin_lock_init(&fi->ioend_lock);
+
+	if (inode->i_blkbits > PAGE_SHIFT)
+		min_order = inode->i_blkbits - PAGE_SHIFT;
+
+	mapping_set_folio_min_order(inode->i_mapping, min_order);
 	set_bit(FUSE_I_IOMAP, &fi->state);
 }
 
#88 lore raw

PATCH 13/23 fuse: use an unrestricted backing device with iomap pagecache io

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

With iomap support turned on for the pagecache, the kernel issues writeback to directly to block devices and we no longer have to push all those pages through the fuse device to userspace. Therefore, we don't need the tight dirty limits (~1M) that are used for regular fuse. This dramatically increases the performance of fuse's pagecache IO.

---
 fs/fuse/file_iomap.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
fs/fuse/file_iomap.c +21 -0
index 92cc85b5b8a8b5..701df0d34067ee 100644
@@ -699,6 +699,27 @@ int fuse_iomap_backing_close(struct fuse_conn *fc, struct fuse_backing *fb)
 void fuse_iomap_mount(struct fuse_mount *fm)
 {
 	struct fuse_conn *fc = fm->fc;
+	struct super_block *sb = fm->sb;
+	struct backing_dev_info *old_bdi = sb->s_bdi;
+	char *suffix = sb->s_bdev ? "-fuseblk" : "-fuse";
+	int res;
+
+	/*
+	 * sb->s_bdi points to the initial private bdi.  However, we want to
+	 * redirect it to a new private bdi with default dirty and readahead
+	 * settings because iomap writeback won't be pushing a ton of dirty
+	 * data through the fuse device.  If this fails we fall back to the
+	 * initial fuse bdi.
+	 */
+	sb->s_bdi = &noop_backing_dev_info;
+	res = super_setup_bdi_name(sb, "%u:%u%s.iomap", MAJOR(fc->dev),
+				   MINOR(fc->dev), suffix);
+	if (res) {
+		sb->s_bdi = old_bdi;
+	} else {
+		bdi_unregister(old_bdi);
+		bdi_put(old_bdi);
+	}
 
 	/*
 	 * Enable syncfs for iomap fuse servers so that we can send a final
#89 lore raw

PATCH 14/23 fuse: advertise support for iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Advertise our new IO paths programmatically by creating an ioctl that can return the capabilities of the kernel.

---
 fs/fuse/fuse_i.h          |    4 ++++
 include/uapi/linux/fuse.h |    9 +++++++++
 fs/fuse/dev.c             |    3 +++
 fs/fuse/file_iomap.c      |   13 +++++++++++++
 4 files changed, 29 insertions(+)
fs/fuse/fuse_i.h +4 -0
index 74fb5971f8fec7..5380a220741014 100644
@@ -1721,6 +1721,9 @@ int fuse_iomap_fallocate(struct file *file, int mode, loff_t offset,
 			 loff_t length, loff_t new_size);
 int fuse_iomap_flush_unmap_range(struct inode *inode, loff_t pos,
 				 loff_t endpos);
+
+int fuse_dev_ioctl_iomap_support(struct file *file,
+				 struct fuse_iomap_support __user *argp);
 #else
 # define fuse_iomap_enabled(...)		(false)
 # define fuse_has_iomap(...)			(false)
@@ -1746,6 +1749,7 @@ int fuse_iomap_flush_unmap_range(struct inode *inode, loff_t pos,
 # define fuse_iomap_set_i_blkbits(...)		((void)0)
 # define fuse_iomap_fallocate(...)		(-ENOSYS)
 # define fuse_iomap_flush_unmap_range(...)	(-ENOSYS)
+# define fuse_dev_ioctl_iomap_support(...)	(-EOPNOTSUPP)
 #endif
 
 #endif /* _FS_FUSE_I_H */
include/uapi/linux/fuse.h +9 -0
index 12d15c186256f3..d0f71136837068 100644
@@ -1134,12 +1134,21 @@ struct fuse_backing_map {
 	uint64_t	padding;
 };
 
+/* basic file I/O functionality through iomap */
+#define FUSE_IOMAP_SUPPORT_FILEIO	(1ULL << 0)
+struct fuse_iomap_support {
+	uint64_t	flags;
+	uint64_t	padding;
+};
+
 /* Device ioctls: */
 #define FUSE_DEV_IOC_MAGIC		229
 #define FUSE_DEV_IOC_CLONE		_IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t)
 #define FUSE_DEV_IOC_BACKING_OPEN	_IOW(FUSE_DEV_IOC_MAGIC, 1, \
 					     struct fuse_backing_map)
 #define FUSE_DEV_IOC_BACKING_CLOSE	_IOW(FUSE_DEV_IOC_MAGIC, 2, uint32_t)
+#define FUSE_DEV_IOC_IOMAP_SUPPORT	_IOR(FUSE_DEV_IOC_MAGIC, 3, \
+					     struct fuse_iomap_support)
 
 struct fuse_lseek_in {
 	uint64_t	fh;
fs/fuse/dev.c +3 -0
index 31d9f006836ac1..d239946a46c463 100644
@@ -2664,6 +2664,9 @@ static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
 	case FUSE_DEV_IOC_BACKING_CLOSE:
 		return fuse_dev_ioctl_backing_close(file, argp);
 
+	case FUSE_DEV_IOC_IOMAP_SUPPORT:
+		return fuse_dev_ioctl_iomap_support(file, argp);
+
 	default:
 		return -ENOTTY;
 	}
fs/fuse/file_iomap.c +13 -0
index 701df0d34067ee..5a2910919ba209 100644
@@ -1756,3 +1756,16 @@ fuse_iomap_fallocate(
 	file_update_time(file);
 	return 0;
 }
+
+int fuse_dev_ioctl_iomap_support(struct file *file,
+				 struct fuse_iomap_support __user *argp)
+{
+	struct fuse_iomap_support ios = { };
+
+	if (fuse_iomap_enabled())
+		ios.flags = FUSE_IOMAP_SUPPORT_FILEIO;
+
+	if (copy_to_user(argp, &ios, sizeof(ios)))
+		return -EFAULT;
+	return 0;
+}
#90 lore raw

PATCH 15/23 fuse: query filesystem geometry when using iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add a new upcall to the fuse server so that the kernel can request filesystem geometry bits when iomap mode is in use.

---
 fs/fuse/fuse_i.h          |    4 +
 fs/fuse/fuse_trace.h      |   48 ++++++++++++++++++
 include/uapi/linux/fuse.h |   39 ++++++++++++++
 fs/fuse/file_iomap.c      |  122 +++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/inode.c           |   26 +++++++---
 5 files changed, 231 insertions(+), 8 deletions(-)
fs/fuse/fuse_i.h +4 -0
index 5380a220741014..2572eab6100fe4 100644
@@ -999,6 +999,9 @@ struct fuse_conn {
 	struct fuse_ring *ring;
 #endif
 
+	/** How many subsystems still need initialization? */
+	atomic_t need_init;
+
 	/** Only used if the connection opts into request timeouts */
 	struct {
 		/* Worker for checking if any requests have timed out */
@@ -1366,6 +1369,7 @@ struct fuse_dev *fuse_dev_alloc(void);
 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc);
 void fuse_dev_free(struct fuse_dev *fud);
 void fuse_send_init(struct fuse_mount *fm);
+void fuse_finish_init(struct fuse_conn *fc, bool ok);
 
 /**
  * Fill in superblock and initialize fuse connection
fs/fuse/fuse_trace.h +48 -0

66 lines not on this page · raw message

include/uapi/linux/fuse.h +39 -0
index d0f71136837068..1a677e807c2846 100644
@@ -240,6 +240,7 @@
  *  - add FUSE_IOMAP and iomap_{begin,end,ioend} handlers for FIEMAP and
  *    SEEK_{DATA,HOLE}, buffered I/O, and direct I/O
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
+ *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
  */
 
 #ifndef _LINUX_FUSE_H
@@ -668,6 +669,7 @@ enum fuse_opcode {
 	FUSE_TMPFILE		= 51,
 	FUSE_STATX		= 52,
 
+	FUSE_IOMAP_CONFIG	= 4092,
 	FUSE_IOMAP_IOEND	= 4093,
 	FUSE_IOMAP_BEGIN	= 4094,
 	FUSE_IOMAP_END		= 4095,
@@ -1414,4 +1416,41 @@ struct fuse_iomap_ioend_in {
 	uint32_t reserved1;	/* zero */
 };
 
+struct fuse_iomap_config_in {
+	uint64_t flags;		/* supported FUSE_IOMAP_CONFIG_* flags */
+	int64_t maxbytes;	/* maximum supported file size */
+	uint64_t padding[6];	/* zero */
+};
+
+/* Which fields are set in fuse_iomap_config_out? */
+#define FUSE_IOMAP_CONFIG_SID		(1 << 0ULL)
+#define FUSE_IOMAP_CONFIG_UUID		(1 << 1ULL)
+#define FUSE_IOMAP_CONFIG_BLOCKSIZE	(1 << 2ULL)
+#define FUSE_IOMAP_CONFIG_MAX_LINKS	(1 << 3ULL)
+#define FUSE_IOMAP_CONFIG_TIME		(1 << 4ULL)
+#define FUSE_IOMAP_CONFIG_MAXBYTES	(1 << 5ULL)
+
+struct fuse_iomap_config_out {
+	uint64_t flags;		/* FUSE_IOMAP_CONFIG_* */
+
+	char s_id[32];		/* Informational name */
+	char s_uuid[16];	/* UUID */
+
+	uint8_t s_uuid_len;	/* length of s_uuid */
+
+	uint8_t s_pad[3];	/* must be zeroes */
+
+	uint32_t s_blocksize;	/* fs block size */
+	uint32_t s_max_links;	/* max hard links */
+
+	/* Granularity of c/m/atime in ns (cannot be worse than a second) */
+	uint32_t s_time_gran;
+
+	/* Time limits for c/m/atime in seconds */
+	int64_t s_time_min;
+	int64_t s_time_max;
+
+	int64_t s_maxbytes;	/* max file size */
+};
+
 #endif /* _LINUX_FUSE_H */
fs/fuse/file_iomap.c +121 -1

141 lines not on this page · raw message

fs/fuse/inode.c +19 -7
index b08b1961d03b3e..abb2beef3cfe1f 100644
@@ -1323,6 +1323,8 @@ static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
 	struct fuse_init_out *arg = &ia->out;
 	bool ok = true;
 
+	atomic_inc(&fc->need_init);
+
 	if (error || arg->major != FUSE_KERNEL_VERSION)
 		ok = false;
 	else {
@@ -1466,9 +1468,6 @@ static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
 
 		init_server_timeout(fc, timeout);
 
-		if (fc->iomap)
-			fuse_iomap_mount(fm);
-
 		fm->sb->s_bdi->ra_pages =
 				min(fm->sb->s_bdi->ra_pages, ra_pages);
 		fc->minor = arg->minor;
@@ -1478,13 +1477,26 @@ static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
 	}
 	kfree(ia);
 
-	if (!ok) {
+	if (!ok)
 		fc->conn_init = 0;
+
+	if (ok && fc->iomap) {
+		atomic_inc(&fc->need_init);
+		fuse_iomap_mount(fm);
+	}
+
+	fuse_finish_init(fc, ok);
+}
+
+void fuse_finish_init(struct fuse_conn *fc, bool ok)
+{
+	if (!ok)
 		fc->conn_error = 1;
-	}
 
-	fuse_set_initialized(fc);
-	wake_up_all(&fc->blocked_waitq);
+	if (atomic_dec_and_test(&fc->need_init)) {
+		fuse_set_initialized(fc);
+		wake_up_all(&fc->blocked_waitq);
+	}
 }
 
 void fuse_send_init(struct fuse_mount *fm)
#91 lore raw

PATCH 16/23 fuse: implement fadvise for iomap files

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If userspace asks us to perform readahead on a file, take i_rwsem so that it can't race with hole punching or writes.

---
 fs/fuse/fuse_i.h     |    3 +++
 fs/fuse/file.c       |    1 +
 fs/fuse/file_iomap.c |   20 ++++++++++++++++++++
 3 files changed, 24 insertions(+)
fs/fuse/fuse_i.h +3 -0
index 2572eab6100fe4..63ce9ddb96477c 100644
@@ -1728,6 +1728,8 @@ int fuse_iomap_flush_unmap_range(struct inode *inode, loff_t pos,
 
 int fuse_dev_ioctl_iomap_support(struct file *file,
 				 struct fuse_iomap_support __user *argp);
+
+int fuse_iomap_fadvise(struct file *file, loff_t start, loff_t end, int advice);
 #else
 # define fuse_iomap_enabled(...)		(false)
 # define fuse_has_iomap(...)			(false)
@@ -1754,6 +1756,7 @@ int fuse_dev_ioctl_iomap_support(struct file *file,
 # define fuse_iomap_fallocate(...)		(-ENOSYS)
 # define fuse_iomap_flush_unmap_range(...)	(-ENOSYS)
 # define fuse_dev_ioctl_iomap_support(...)	(-EOPNOTSUPP)
+# define fuse_iomap_fadvise			NULL
 #endif
 
 #endif /* _FS_FUSE_I_H */
fs/fuse/file.c +1 -0
index 825b7ac9158d08..6575deae7e65f6 100644
@@ -3186,6 +3186,7 @@ static const struct file_operations fuse_file_operations = {
 	.poll		= fuse_file_poll,
 	.fallocate	= fuse_file_fallocate,
 	.copy_file_range = fuse_copy_file_range,
+	.fadvise	= fuse_iomap_fadvise,
 };
 
 static const struct address_space_operations fuse_file_aops  = {
fs/fuse/file_iomap.c +20 -0
index d2b918521b7395..c740fb1420bee0 100644
@@ -7,6 +7,7 @@
 #include <linux/fiemap.h>
 #include <linux/pagemap.h>
 #include <linux/falloc.h>
+#include <linux/fadvise.h>
 #include "fuse_i.h"
 #include "fuse_trace.h"
 #include "iomap_priv.h"
@@ -1889,3 +1890,22 @@ int fuse_dev_ioctl_iomap_support(struct file *file,
 		return -EFAULT;
 	return 0;
 }
+
+int fuse_iomap_fadvise(struct file *file, loff_t start, loff_t end, int advice)
+{
+	struct inode *inode = file_inode(file);
+	bool needlock = advice == POSIX_FADV_WILLNEED &&
+			fuse_inode_has_iomap(inode);
+	int ret;
+
+	/*
+	 * Operations creating pages in page cache need protection from hole
+	 * punching and similar ops
+	 */
+	if (needlock)
+		inode_lock_shared(inode);
+	ret = generic_fadvise(file, start, end, advice);
+	if (needlock)
+		inode_unlock_shared(inode);
+	return ret;
+}
#92 lore raw

PATCH 17/23 fuse: make the root nodeid dynamic

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Change this from a hardcoded constant to a dynamic field so that fuse servers don't need to translate.

---
 fs/fuse/fuse_i.h     |    7 +++++--
 fs/fuse/fuse_trace.h |    6 ++++--
 fs/fuse/dir.c        |   10 ++++++----
 fs/fuse/inode.c      |   11 +++++++----
 fs/fuse/readdir.c    |   10 +++++-----
 5 files changed, 27 insertions(+), 17 deletions(-)
fs/fuse/fuse_i.h +5 -2
index 63ce9ddb96477c..66cf8dcf9216e7 100644
@@ -665,6 +665,9 @@ struct fuse_conn {
 
 	struct rcu_head rcu;
 
+	/* node id of the root directory */
+	u64 root_nodeid;
+
 	/** The user id for this mount */
 	kuid_t user_id;
 
@@ -1097,9 +1100,9 @@ static inline u64 get_node_id(struct inode *inode)
 	return get_fuse_inode(inode)->nodeid;
 }
 
-static inline int invalid_nodeid(u64 nodeid)
+static inline int invalid_nodeid(const struct fuse_conn *fc, u64 nodeid)
 {
-	return !nodeid || nodeid == FUSE_ROOT_ID;
+	return !nodeid || nodeid == fc->root_nodeid;
 }
 
 static inline u64 fuse_get_attr_version(struct fuse_conn *fc)
fs/fuse/fuse_trace.h +4 -2
index d3a0bd066370f5..1f2ff30bececd4 100644
@@ -1012,6 +1012,7 @@ TRACE_EVENT(fuse_iomap_config,
 
 	TP_STRUCT__entry(
 		__field(dev_t,			connection)
+		__field(uint64_t,		root_nodeid)
 
 		__field(uint32_t,		flags)
 		__field(uint32_t,		blocksize)
@@ -1026,6 +1027,7 @@ TRACE_EVENT(fuse_iomap_config,
 
 	TP_fast_assign(
 		__entry->connection	=	fm->fc->dev;
+		__entry->root_nodeid	=	fm->fc->root_nodeid;
 		__entry->flags		=	outarg->flags;
 		__entry->blocksize	=	outarg->s_blocksize;
 		__entry->max_links	=	outarg->s_max_links;
@@ -1036,8 +1038,8 @@ TRACE_EVENT(fuse_iomap_config,
 		__entry->uuid_len	=	outarg->s_uuid_len;
 	),
 
-	TP_printk("connection %u flags (%s) blocksize 0x%x max_links %u time_gran %u time_min %lld time_max %lld maxbytes 0x%llx uuid_len %u",
-		  __entry->connection,
+	TP_printk("connection %u root_ino 0x%llx flags (%s) blocksize 0x%x max_links %u time_gran %u time_min %lld time_max %lld maxbytes 0x%llx uuid_len %u",
+		  __entry->connection, __entry->root_nodeid,
 		  __print_flags(__entry->flags, "|", FUSE_IOMAP_CONFIG_STRINGS),
 		  __entry->blocksize, __entry->max_links, __entry->time_gran,
 		  __entry->time_min, __entry->time_max, __entry->maxbytes,
fs/fuse/dir.c +6 -4
index 07aa338208b5cc..02c8e705af1e35 100644
@@ -386,7 +386,7 @@ int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name
 	err = -EIO;
 	if (fuse_invalid_attr(&outarg->attr))
 		goto out_put_forget;
-	if (outarg->nodeid == FUSE_ROOT_ID && outarg->generation != 0) {
+	if (outarg->nodeid == fm->fc->root_nodeid && outarg->generation != 0) {
 		pr_warn_once("root generation should be zero\n");
 		outarg->generation = 0;
 	}
@@ -436,7 +436,7 @@ static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
 		goto out_err;
 
 	err = -EIO;
-	if (inode && get_node_id(inode) == FUSE_ROOT_ID)
+	if (inode && get_node_id(inode) == fc->root_nodeid)
 		goto out_iput;
 
 	newent = d_splice_alias(inode, entry);
@@ -687,7 +687,8 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,
 		goto out_free_ff;
 
 	err = -EIO;
-	if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
+	if (!S_ISREG(outentry.attr.mode) ||
+	    invalid_nodeid(fm->fc, outentry.nodeid) ||
 	    fuse_invalid_attr(&outentry.attr))
 		goto out_free_ff;
 
@@ -838,7 +839,8 @@ static struct dentry *create_new_entry(struct mnt_idmap *idmap, struct fuse_moun
 		goto out_put_forget_req;
 
 	err = -EIO;
-	if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
+	if (invalid_nodeid(fm->fc, outarg.nodeid) ||
+	    fuse_invalid_attr(&outarg.attr))
 		goto out_put_forget_req;
 
 	if ((outarg.attr.mode ^ mode) & S_IFMT)
fs/fuse/inode.c +7 -4
index abb2beef3cfe1f..f2d519c0f737e6 100644
@@ -1001,6 +1001,7 @@ void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
 	fc->max_pages_limit = fuse_max_pages_limit;
 	fc->name_max = FUSE_NAME_LOW_MAX;
 	fc->timeout.req_timeout = 0;
+	fc->root_nodeid = FUSE_ROOT_ID;
 
 	if (IS_ENABLED(CONFIG_FUSE_BACKING))
 		fuse_backing_files_init(fc);
@@ -1056,12 +1057,14 @@ EXPORT_SYMBOL_GPL(fuse_conn_get);
 static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned int mode)
 {
 	struct fuse_attr attr;
+	struct fuse_conn *fc = get_fuse_conn_super(sb);
+
 	memset(&attr, 0, sizeof(attr));
 
 	attr.mode = mode;
-	attr.ino = FUSE_ROOT_ID;
+	attr.ino = fc->root_nodeid;
 	attr.nlink = 1;
-	return fuse_iget(sb, FUSE_ROOT_ID, 0, &attr, 0, 0, 0);
+	return fuse_iget(sb, fc->root_nodeid, 0, &attr, 0, 0, 0);
 }
 
 struct fuse_inode_handle {
@@ -1105,7 +1108,7 @@ static struct dentry *fuse_get_dentry(struct super_block *sb,
 		goto out_iput;
 
 	entry = d_obtain_alias(inode);
-	if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
+	if (!IS_ERR(entry) && get_node_id(inode) != fc->root_nodeid)
 		fuse_invalidate_entry_cache(entry);
 
 	return entry;
@@ -1198,7 +1201,7 @@ static struct dentry *fuse_get_parent(struct dentry *child)
 	}
 
 	parent = d_obtain_alias(inode);
-	if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
+	if (!IS_ERR(parent) && get_node_id(inode) != fc->root_nodeid)
 		fuse_invalidate_entry_cache(parent);
 
 	return parent;
fs/fuse/readdir.c +5 -5
index c2aae2eef0868b..45dd932eb03a5e 100644
@@ -185,12 +185,12 @@ static int fuse_direntplus_link(struct file *file,
 			return 0;
 	}
 
-	if (invalid_nodeid(o->nodeid))
-		return -EIO;
-	if (fuse_invalid_attr(&o->attr))
-		return -EIO;
-
 	fc = get_fuse_conn(dir);
+	if (invalid_nodeid(fc, o->nodeid))
+		return -EIO;
+	if (fuse_invalid_attr(&o->attr))
+		return -EIO;
+
 	epoch = atomic_read(&fc->epoch);
 
 	name.hash = full_name_hash(parent, name.name, name.len);
#93 lore raw

PATCH 18/23 fuse: allow setting of root nodeid

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Provide a new mount option so that fuse servers can actually set the root nodeid.

---
 fs/fuse/fuse_i.h |    2 ++
 fs/fuse/inode.c  |   11 +++++++++++
 2 files changed, 13 insertions(+)
fs/fuse/fuse_i.h +2 -0
index 66cf8dcf9216e7..a81138da1e55f6 100644
@@ -601,6 +601,7 @@ struct fuse_fs_context {
 	int fd;
 	struct file *file;
 	unsigned int rootmode;
+	u64 root_nodeid;
 	kuid_t user_id;
 	kgid_t group_id;
 	bool is_bdev:1;
@@ -614,6 +615,7 @@ struct fuse_fs_context {
 	bool no_control:1;
 	bool no_force_umount:1;
 	bool legacy_opts_show:1;
+	bool root_nodeid_present:1;
 	enum fuse_dax_mode dax_mode;
 	unsigned int max_read;
 	unsigned int blksize;
fs/fuse/inode.c +11 -0
index f2d519c0f737e6..18dc9492d19174 100644
@@ -785,6 +785,7 @@ enum {
 	OPT_ALLOW_OTHER,
 	OPT_MAX_READ,
 	OPT_BLKSIZE,
+	OPT_ROOT_NODEID,
 	OPT_ERR
 };
 
@@ -799,6 +800,7 @@ static const struct fs_parameter_spec fuse_fs_parameters[] = {
 	fsparam_u32	("max_read",		OPT_MAX_READ),
 	fsparam_u32	("blksize",		OPT_BLKSIZE),
 	fsparam_string	("subtype",		OPT_SUBTYPE),
+	fsparam_u64	("root_nodeid",		OPT_ROOT_NODEID),
 	{}
 };
 
@@ -894,6 +896,11 @@ static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param)
 		ctx->blksize = result.uint_32;
 		break;
 
+	case OPT_ROOT_NODEID:
+		ctx->root_nodeid = result.uint_64;
+		ctx->root_nodeid_present = true;
+		break;
+
 	default:
 		return -EINVAL;
 	}
@@ -929,6 +936,8 @@ static int fuse_show_options(struct seq_file *m, struct dentry *root)
 			seq_printf(m, ",max_read=%u", fc->max_read);
 		if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
 			seq_printf(m, ",blksize=%lu", sb->s_blocksize);
+		if (fc->root_nodeid && fc->root_nodeid != FUSE_ROOT_ID)
+			seq_printf(m, ",root_nodeid=%llu", fc->root_nodeid);
 	}
 #ifdef CONFIG_FUSE_DAX
 	if (fc->dax_mode == FUSE_DAX_ALWAYS)
@@ -1879,6 +1888,8 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
 	sb->s_flags |= SB_POSIXACL;
 
 	fc->default_permissions = ctx->default_permissions;
+	if (ctx->root_nodeid_present)
+		fc->root_nodeid = ctx->root_nodeid;
 	fc->allow_other = ctx->allow_other;
 	fc->user_id = ctx->user_id;
 	fc->group_id = ctx->group_id;
#94 lore raw

PATCH 19/23 fuse: invalidate ranges of block devices being used for iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it easier to invalidate the page cache for a block device that is being used in conjunction with iomap. This allows a fuse server to kill all cached data for a block that is being freed, so that block reuse doesn't result in file corruption. Right now, the only way to do this is with fadvise, which ignores and doesn't wait for pages undergoing writeback.

---
 fs/fuse/fuse_i.h          |    4 ++++
 fs/fuse/fuse_trace.h      |   26 +++++++++++++++++++++++++
 include/uapi/linux/fuse.h |   10 ++++++++++
 fs/fuse/dev.c             |   27 ++++++++++++++++++++++++++
 fs/fuse/file_iomap.c      |   47 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 114 insertions(+)
fs/fuse/fuse_i.h +4 -0
index a81138da1e55f6..362fa87241ac70 100644
@@ -1690,6 +1690,9 @@ int fuse_iomap_backing_close(struct fuse_conn *fc, struct fuse_backing *fb);
 void fuse_iomap_mount(struct fuse_mount *fm);
 void fuse_iomap_unmount(struct fuse_mount *fm);
 
+int fuse_iomap_dev_inval(struct fuse_conn *fc,
+			 const struct fuse_iomap_dev_inval_out *arg);
+
 int fuse_iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 		      u64 start, u64 length);
 loff_t fuse_iomap_lseek(struct file *file, loff_t offset, int whence);
@@ -1742,6 +1745,7 @@ int fuse_iomap_fadvise(struct file *file, loff_t start, loff_t end, int advice);
 # define fuse_iomap_backing_close(...)		(-EOPNOTSUPP)
 # define fuse_iomap_mount(...)			((void)0)
 # define fuse_iomap_unmount(...)		((void)0)
+# define fuse_iomap_dev_inval(...)		(-ENOSYS)
 # define fuse_iomap_fiemap			NULL
 # define fuse_iomap_lseek(...)			(-ENOSYS)
 # define fuse_iomap_bmap(...)			(-ENOSYS)
fs/fuse/fuse_trace.h +26 -0
index 1f2ff30bececd4..2f4c78ba498177 100644
@@ -1045,6 +1045,32 @@ TRACE_EVENT(fuse_iomap_config,
 		  __entry->time_min, __entry->time_max, __entry->maxbytes,
 		  __entry->uuid_len)
 );
+
+TRACE_EVENT(fuse_iomap_dev_inval,
+	TP_PROTO(const struct fuse_conn *fc,
+		 const struct fuse_iomap_dev_inval_out *arg),
+	TP_ARGS(fc, arg),
+
+	TP_STRUCT__entry(
+		__field(dev_t,			connection)
+		__field(int,			dev)
+		__field(unsigned long long,	offset)
+		__field(unsigned long long,	length)
+	),
+
+	TP_fast_assign(
+		__entry->connection	=	fc->dev;
+		__entry->dev		=	arg->dev;
+		__entry->offset		=	arg->offset;
+		__entry->length		=	arg->length;
+	),
+
+	TP_printk("connection %u dev %d offset 0x%llx length 0x%llx",
+		  __entry->connection,
+		  __entry->dev,
+		  __entry->offset,
+		  __entry->length)
+);
 #endif /* CONFIG_FUSE_IOMAP */
 
 #endif /* _TRACE_FUSE_H */
include/uapi/linux/fuse.h +10 -0
index 1a677e807c2846..1f8e3ba60e7ec5 100644
@@ -241,6 +241,7 @@
  *    SEEK_{DATA,HOLE}, buffered I/O, and direct I/O
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
+ *  - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges
  */
 
 #ifndef _LINUX_FUSE_H
@@ -691,6 +692,7 @@ enum fuse_notify_code {
 	FUSE_NOTIFY_DELETE = 6,
 	FUSE_NOTIFY_RESEND = 7,
 	FUSE_NOTIFY_INC_EPOCH = 8,
+	FUSE_NOTIFY_IOMAP_DEV_INVAL = 9,
 	FUSE_NOTIFY_CODE_MAX,
 };
 
@@ -1453,4 +1455,12 @@ struct fuse_iomap_config_out {
 	int64_t s_maxbytes;	/* max file size */
 };
 
+struct fuse_iomap_dev_inval_out {
+	uint32_t dev;		/* device cookie */
+	uint32_t reserved;	/* zero */
+
+	uint64_t offset;	/* range to invalidate pagecache, bytes */
+	uint64_t length;
+};
+
 #endif /* _LINUX_FUSE_H */
fs/fuse/dev.c +27 -0
index d239946a46c463..575cb6e15d84d5 100644
@@ -1833,6 +1833,30 @@ static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
 	return err;
 }
 
+static int fuse_notify_iomap_dev_inval(struct fuse_conn *fc, unsigned int size,
+				       struct fuse_copy_state *cs)
+{
+	struct fuse_iomap_dev_inval_out outarg;
+	int err = -EINVAL;
+
+	if (size != sizeof(outarg))
+		goto err;
+
+	err = fuse_copy_one(cs, &outarg, sizeof(outarg));
+	if (err)
+		goto err;
+	if (outarg.reserved) {
+		err = -EINVAL;
+		goto err;
+	}
+	fuse_copy_finish(cs);
+
+	return fuse_iomap_dev_inval(fc, &outarg);
+err:
+	fuse_copy_finish(cs);
+	return err;
+}
+
 struct fuse_retrieve_args {
 	struct fuse_args_pages ap;
 	struct fuse_notify_retrieve_in inarg;
@@ -2079,6 +2103,9 @@ static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
 	case FUSE_NOTIFY_INC_EPOCH:
 		return fuse_notify_inc_epoch(fc);
 
+	case FUSE_NOTIFY_IOMAP_DEV_INVAL:
+		return fuse_notify_iomap_dev_inval(fc, size, cs);
+
 	default:
 		fuse_copy_finish(cs);
 		return -EINVAL;
fs/fuse/file_iomap.c +47 -0

50 lines not on this page · raw message

#95 lore raw

PATCH 20/23 fuse: implement inline data file IO via iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Implement inline data file IO by issuing FUSE_READ/FUSE_WRITE commands in response to an inline data mapping.

---
 fs/fuse/fuse_trace.h |   45 +++++++++++++
 fs/fuse/file_iomap.c |  179 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 224 insertions(+)
fs/fuse/fuse_trace.h +45 -0
index 2f4c78ba498177..4ebd9a9e697ce2 100644
@@ -234,6 +234,7 @@ DEFINE_FUSE_BACKING_EVENT(fuse_backing_close);
 #if IS_ENABLED(CONFIG_FUSE_IOMAP)
 struct iomap_writepage_ctx;
 struct iomap_ioend;
+struct iomap;
 
 /* tracepoint boilerplate so we don't have to keep doing this */
 #define FUSE_IOMAP_OPFLAGS_FIELD \
@@ -1071,6 +1072,50 @@ TRACE_EVENT(fuse_iomap_dev_inval,
 		  __entry->offset,
 		  __entry->length)
 );
+
+DECLARE_EVENT_CLASS(fuse_iomap_inline_class,
+	TP_PROTO(const struct inode *inode, loff_t pos, uint64_t count,
+		 const struct iomap *map),
+	TP_ARGS(inode, pos, count, map),
+
+	TP_STRUCT__entry(
+		FUSE_IO_RANGE_FIELDS()
+		FUSE_IOMAP_MAP_FIELDS(map)
+		__field(bool,			has_buf)
+		__field(uint64_t,		validity_cookie)
+	),
+
+	TP_fast_assign(
+		FUSE_INODE_ASSIGN(inode, fi, fm);
+		__entry->offset		=	pos;
+		__entry->length		=	count;
+
+		__entry->mapdev		=	FUSE_IOMAP_DEV_NULL;
+		__entry->mapaddr	=	map->addr;
+		__entry->mapoffset	=	map->offset;
+		__entry->maplength	=	map->length;
+		__entry->maptype	=	map->type;
+		__entry->mapflags	=	map->flags;
+
+		__entry->has_buf	=	map->inline_data != NULL;
+		__entry->validity_cookie=	map->validity_cookie;
+	),
+
+	TP_printk(FUSE_IO_RANGE_FMT() FUSE_IOMAP_MAP_FMT() " has_buf? %d cookie 0x%llx",
+		  FUSE_IO_RANGE_PRINTK_ARGS(),
+		  FUSE_IOMAP_MAP_PRINTK_ARGS(map),
+		  __entry->has_buf,
+		  __entry->validity_cookie)
+);
+#define DEFINE_FUSE_IOMAP_INLINE_EVENT(name)	\
+DEFINE_EVENT(fuse_iomap_inline_class, name,	\
+	TP_PROTO(const struct inode *inode, loff_t pos, uint64_t count, \
+		 const struct iomap *map), \
+	TP_ARGS(inode, pos, count, map))
+DEFINE_FUSE_IOMAP_INLINE_EVENT(fuse_iomap_inline_read);
+DEFINE_FUSE_IOMAP_INLINE_EVENT(fuse_iomap_inline_write);
+DEFINE_FUSE_IOMAP_INLINE_EVENT(fuse_iomap_set_inline_iomap);
+DEFINE_FUSE_IOMAP_INLINE_EVENT(fuse_iomap_set_inline_srcmap);
 #endif /* CONFIG_FUSE_IOMAP */
 
 #endif /* _TRACE_FUSE_H */
fs/fuse/file_iomap.c +179 -0

205 lines not on this page · raw message

#96 lore raw

PATCH 21/23 fuse: allow more statx fields

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Allow the fuse server to supply us with the more recently added fields of struct statx.

---
 fs/fuse/fuse_i.h          |    8 +++++
 include/uapi/linux/fuse.h |   15 +++++++++
 fs/fuse/dir.c             |   73 ++++++++++++++++++++++++++++++++++++++-------
 3 files changed, 84 insertions(+), 12 deletions(-)
fs/fuse/fuse_i.h +8 -0
index 362fa87241ac70..4ca29315b2a434 100644
@@ -1677,6 +1677,14 @@ void fuse_iomap_sysfs_cleanup(struct kobject *kobj);
 
 sector_t fuse_bmap(struct address_space *mapping, sector_t block);
 
+#if IS_ENABLED(CONFIG_FUSE_IOMAP_DEBUG)
+int fuse_iomap_sysfs_init(struct kobject *kobj);
+void fuse_iomap_sysfs_cleanup(struct kobject *kobj);
+#else
+# define fuse_iomap_sysfs_init(...)		(0)
+# define fuse_iomap_sysfs_cleanup(...)		((void)0)
+#endif
+
 #if IS_ENABLED(CONFIG_FUSE_IOMAP)
 bool fuse_iomap_enabled(void);
 
include/uapi/linux/fuse.h +14 -1
index 1f8e3ba60e7ec5..cfeee8a280896a 100644
@@ -335,7 +335,20 @@ struct fuse_statx {
 	uint32_t	rdev_minor;
 	uint32_t	dev_major;
 	uint32_t	dev_minor;
-	uint64_t	__spare2[14];
+
+	uint64_t	mnt_id;
+	uint32_t	dio_mem_align;
+	uint32_t	dio_offset_align;
+	uint64_t	subvol;
+
+	uint32_t	atomic_write_unit_min;
+	uint32_t	atomic_write_unit_max;
+	uint32_t	atomic_write_segments_max;
+	uint32_t	dio_read_offset_align;
+	uint32_t	atomic_write_unit_max_opt;
+	uint32_t	__spare2[1];
+
+	uint64_t	__spare3[8];
 };
 
 struct fuse_kstatfs {
fs/fuse/dir.c +62 -11

109 lines not on this page · raw message

#97 lore raw

PATCH 22/23 fuse: support atomic writes with iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

One whole block!

---
 fs/fuse/fuse_i.h          |    9 ++++++++
 fs/fuse/fuse_trace.h      |    4 +++-
 include/uapi/linux/fuse.h |    5 ++++
 fs/fuse/file_iomap.c      |   51 ++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 67 insertions(+), 2 deletions(-)
fs/fuse/fuse_i.h +9 -0
index 4ca29315b2a434..e72cc25c564048 100644
@@ -243,6 +243,8 @@ enum {
 	FUSE_I_CACHE_IO_MODE,
 	/* Use iomap for this inode */
 	FUSE_I_IOMAP,
+	/* Enable untorn writes */
+	FUSE_I_ATOMIC,
 };
 
 struct fuse_conn;
@@ -1718,6 +1720,13 @@ static inline bool fuse_inode_has_iomap(const struct inode *inode)
 	return test_bit(FUSE_I_IOMAP, &fi->state);
 }
 
+static inline bool fuse_inode_has_atomic(const struct inode *inode)
+{
+	const struct fuse_inode *fi = get_fuse_inode_c(inode);
+
+	return test_bit(FUSE_I_ATOMIC, &fi->state);
+}
+
 static inline bool fuse_want_iomap_directio(const struct kiocb *iocb)
 {
 	return (iocb->ki_flags & IOCB_DIRECT) &&
fs/fuse/fuse_trace.h +3 -1
index 4ebd9a9e697ce2..79de0e65608360 100644
@@ -331,6 +331,7 @@ TRACE_DEFINE_ENUM(FUSE_I_BAD);
 TRACE_DEFINE_ENUM(FUSE_I_BTIME);
 TRACE_DEFINE_ENUM(FUSE_I_CACHE_IO_MODE);
 TRACE_DEFINE_ENUM(FUSE_I_IOMAP);
+TRACE_DEFINE_ENUM(FUSE_I_ATOMIC);
 
 #define FUSE_IFLAG_STRINGS \
 	{ 1 << FUSE_I_ADVISE_RDPLUS,		"advise_rdplus" }, \
@@ -339,7 +340,8 @@ TRACE_DEFINE_ENUM(FUSE_I_IOMAP);
 	{ 1 << FUSE_I_BAD,			"bad" }, \
 	{ 1 << FUSE_I_BTIME,			"btime" }, \
 	{ 1 << FUSE_I_CACHE_IO_MODE,		"cacheio" }, \
-	{ 1 << FUSE_I_IOMAP,			"iomap" }
+	{ 1 << FUSE_I_IOMAP,			"iomap" }, \
+	{ 1 << FUSE_I_ATOMIC,			"atomic" }
 
 #define IOMAP_IOEND_STRINGS \
 	{ IOMAP_IOEND_SHARED,			"shared" }, \
include/uapi/linux/fuse.h +5 -0
index cfeee8a280896a..70b5530e587d48 100644
@@ -242,6 +242,7 @@
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
  *  - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges
+ *  - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support
  */
 
 #ifndef _LINUX_FUSE_H
@@ -597,10 +598,12 @@ struct fuse_file_lock {
  * FUSE_ATTR_SUBMOUNT: Object is a submount root
  * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode
  * FUSE_ATTR_IOMAP: Use iomap for this inode
+ * FUSE_ATTR_ATOMIC: Enable untorn writes
  */
 #define FUSE_ATTR_SUBMOUNT      (1 << 0)
 #define FUSE_ATTR_DAX		(1 << 1)
 #define FUSE_ATTR_IOMAP		(1 << 2)
+#define FUSE_ATTR_ATOMIC	(1 << 3)
 
 /**
  * Open flags
@@ -1153,6 +1156,8 @@ struct fuse_backing_map {
 
 /* basic file I/O functionality through iomap */
 #define FUSE_IOMAP_SUPPORT_FILEIO	(1ULL << 0)
+/* untorn writes through iomap */
+#define FUSE_IOMAP_SUPPORT_ATOMIC	(1ULL << 1)
 struct fuse_iomap_support {
 	uint64_t	flags;
 	uint64_t	padding;
fs/fuse/file_iomap.c +50 -1

99 lines not on this page · raw message

#98 lore raw

PATCH 23/23 fuse: enable iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Remove the guard that we used to avoid bisection problems.

---
 fs/fuse/file_iomap.c |    3 ---
 1 file changed, 3 deletions(-)
fs/fuse/file_iomap.c +0 -3
index ee199c1fd27b1f..3141518cc6e67d 100644
@@ -104,9 +104,6 @@ void fuse_iomap_sysfs_cleanup(struct kobject *fuse_kobj)
 
 bool fuse_iomap_enabled(void)
 {
-	/* Don't let anyone touch iomap until the end of the patchset. */
-	return false;
-
 	/*
 	 * There are fears that a fuse+iomap server could somehow DoS the
 	 * system by doing things like going out to lunch during a writeback
#99 lore raw

RFC PATCH v4 2/4 libfuse: allow servers to use iomap for better file IO performance

In reply to Darrick J. Wong

Hi all,

This series connects libfuse to the iomap-enabled fuse driver in Linux to get fuse servers out of the business of handling file I/O themselves. By keeping the IO path mostly within the kernel, we can dramatically improve the speed of disk-based filesystems. This enables us to move all the filesystem metadata parsing code out of the kernel and into userspace, which means that we can containerize them for security without losing a lot of performance.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap
---
Commits in this patchset:
* libfuse: bump kernel and library ABI versions
* libfuse: add kernel gates for FUSE_IOMAP
* libfuse: add fuse commands for iomap_begin and end
* libfuse: add upper level iomap commands
* libfuse: add a lowlevel notification to add a new device to iomap
* libfuse: add upper-level iomap add device function
* libfuse: add iomap ioend low level handler
* libfuse: add upper level iomap ioend commands
* libfuse: add a reply function to send FUSE_ATTR_* to the kernel
* libfuse: connect high level fuse library to fuse_reply_attr_iflags
* libfuse: support direct I/O through iomap
* libfuse: support buffered I/O through iomap
* libfuse: don't allow hardlinking of iomap files in the upper level fuse library
* libfuse: allow discovery of the kernel's iomap capabilities
* libfuse: add lower level iomap_config implementation
* libfuse: add upper level iomap_config implementation
* libfuse: allow root_nodeid mount option
* libfuse: add low level code to invalidate iomap block device ranges
* libfuse: add upper-level API to invalidate parts of an iomap block device
* libfuse: add strictatime/lazytime mount options
* libfuse: add atomic write support

---
 include/fuse.h          |   86 ++++++++
 include/fuse_common.h   |  131 +++++++++++++
 include/fuse_kernel.h   |  113 +++++++++++
 include/fuse_lowlevel.h |  238 +++++++++++++++++++++++
 ChangeLog.rst           |   12 +
 lib/fuse.c              |  484 ++++++++++++++++++++++++++++++++++++++++++-----
 lib/fuse_lowlevel.c     |  370 ++++++++++++++++++++++++++++++++++--
 lib/fuse_versionscript  |   20 ++
 lib/meson.build         |    2 
 lib/mount.c             |   19 ++
 meson.build             |    2 
 11 files changed, 1400 insertions(+), 77 deletions(-)
#100 lore raw

PATCH 1/21 libfuse: bump kernel and library ABI versions

From: Darrick J. Wong <[email protected]>

Bump the kernel ABI version to 7.99 and the libfuse ABI version to 3.99 to start our development. This patch exists to avoid confusion during the prototyping stage.

---
 include/fuse_kernel.h  |    4 +++-
 ChangeLog.rst          |   12 +++++++++++-
 lib/fuse_versionscript |    3 +++
 lib/meson.build        |    2 +-
 meson.build            |    2 +-
 5 files changed, 19 insertions(+), 4 deletions(-)
include/fuse_kernel.h +3 -1
index 122d6586e8d4da..4d68c4e8a71d5f 100644
@@ -235,6 +235,8 @@
  *
  *  7.44
  *  - add FUSE_NOTIFY_INC_EPOCH
+ *
+ *  7.99
  */
 
 #ifndef _LINUX_FUSE_H
@@ -270,7 +272,7 @@
 #define FUSE_KERNEL_VERSION 7
 
 /** Minor version number of this interface */
-#define FUSE_KERNEL_MINOR_VERSION 44
+#define FUSE_KERNEL_MINOR_VERSION 99
 
 /** The node ID of the root inode */
 #define FUSE_ROOT_ID 1
ChangeLog.rst +11 -1
index 505d9dba84100f..bdb133a5f7db74 100644
@@ -1,4 +1,14 @@
-libfuse 3.18
+libfuse 3.99
+
+libfuse 3.99-rc0 (2025-07-18)
+===============================
+
+* Add prototypes of iomap and syncfs (djwong)
+
+libfuse 3.18-rc0 (2025-07-18)
+===============================
+
+* Add statx, among other things (djwong)
 
 libfuse 3.17.1-rc0 (2024-02.10)
 ===============================
lib/fuse_versionscript +3 -0
index 0e581f1711412c..ba8f3b00478b30 100644
@@ -217,6 +217,9 @@ FUSE_3.18 {
 		fuse_fs_statx;
 } FUSE_3.17;
 
+FUSE_3.99 {
+} FUSE_3.18;
+
 # Local Variables:
 # indent-tabs-mode: t
 # End:
lib/meson.build +1 -1
index fcd95741c9d374..8efe71abfabc9e 100644
@@ -49,7 +49,7 @@ libfuse = library('fuse3',
                   dependencies: deps,
                   install: true,
                   link_depends: 'fuse_versionscript',
-                  c_args: [ '-DFUSE_USE_VERSION=317',
+                  c_args: [ '-DFUSE_USE_VERSION=399',
                             '-DFUSERMOUNT_DIR="@0@"'.format(fusermount_path) ],
                   link_args: ['-Wl,--version-script,' + meson.current_source_dir()
                               + '/fuse_versionscript' ])
meson.build +1 -1
index f98ef8a6d60f33..0abb2cf0be5563 100644
@@ -1,5 +1,5 @@
 project('libfuse3', ['c'],
-        version: '3.18.0-rc0',  # Version with RC suffix
+        version: '3.99.0-rc0',  # Version with RC suffix
         meson_version: '>= 0.60.0',
         default_options: [
             'buildtype=debugoptimized',
#101 lore raw

PATCH 2/21 libfuse: add kernel gates for FUSE_IOMAP

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add some flags to query and request kernel support for filesystem iomap for regular files.

---
 include/fuse_common.h |    5 +++++
 include/fuse_kernel.h |    5 +++++
 lib/fuse_lowlevel.c   |   10 +++++++++-
 3 files changed, 19 insertions(+), 1 deletion(-)
include/fuse_common.h +5 -0
index b82f2c41deb30c..8f87263d78f999 100644
@@ -520,6 +520,11 @@ struct fuse_loop_config_v1 {
  */
 #define FUSE_CAP_OVER_IO_URING (1UL << 31)
 
+/**
+ * Client supports using iomap for FIEMAP and SEEK_{DATA,HOLE}
+ */
+#define FUSE_CAP_IOMAP (1ULL << 32)
+
 /**
  * Ioctl flags
  *
include/fuse_kernel.h +5 -0
index 4d68c4e8a71d5f..6779b9c69bb9e2 100644
@@ -237,6 +237,8 @@
  *  - add FUSE_NOTIFY_INC_EPOCH
  *
  *  7.99
+ *  - add FUSE_IOMAP and iomap_{begin,end,ioend} handlers for FIEMAP and
+ *    SEEK_{DATA,HOLE}
  */
 
 #ifndef _LINUX_FUSE_H
@@ -445,6 +447,8 @@ struct fuse_file_lock {
  * FUSE_OVER_IO_URING: Indicate that client supports io-uring
  * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests.
  *			 init_out.request_timeout contains the timeout (in secs)
+ * FUSE_IOMAP: Client supports iomap for FIEMAP and SEEK_{DATA,HOLE} file
+ *	       operations.
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
@@ -492,6 +496,7 @@ struct fuse_file_lock {
 #define FUSE_ALLOW_IDMAP	(1ULL << 40)
 #define FUSE_OVER_IO_URING	(1ULL << 41)
 #define FUSE_REQUEST_TIMEOUT	(1ULL << 42)
+#define FUSE_IOMAP		(1ULL << 43)
 
 /**
  * CUSE INIT request/reply flags
lib/fuse_lowlevel.c +9 -1
index 6afcecd3bdda96..33c71ba216679c 100644
@@ -2686,7 +2686,8 @@ _do_init(fuse_req_t req, const fuse_ino_t nodeid, const void *op_in,
 			se->conn.capable_ext |= FUSE_CAP_NO_EXPORT_SUPPORT;
 		if (inargflags & FUSE_OVER_IO_URING)
 			se->conn.capable_ext |= FUSE_CAP_OVER_IO_URING;
-
+		if (inargflags & FUSE_IOMAP)
+			se->conn.capable_ext |= FUSE_CAP_IOMAP;
 	} else {
 		se->conn.max_readahead = 0;
 	}
@@ -2732,6 +2733,9 @@ _do_init(fuse_req_t req, const fuse_ino_t nodeid, const void *op_in,
 		       FUSE_CAP_READDIRPLUS_AUTO);
 	LL_SET_DEFAULT(1, FUSE_CAP_OVER_IO_URING);
 
+	/* servers need to opt-in to iomap explicitly */
+	LL_SET_DEFAULT(0, FUSE_CAP_IOMAP);
+
 	/* This could safely become default, but libfuse needs an API extension
 	 * to support it
 	 * LL_SET_DEFAULT(1, FUSE_CAP_SETXATTR_EXT);
@@ -2850,6 +2854,8 @@ _do_init(fuse_req_t req, const fuse_ino_t nodeid, const void *op_in,
 		outargflags |= FUSE_REQUEST_TIMEOUT;
 		outarg.request_timeout = se->conn.request_timeout;
 	}
+	if (se->conn.want_ext & FUSE_CAP_IOMAP)
+		outargflags |= FUSE_IOMAP;
 
 	if (inargflags & FUSE_INIT_EXT) {
 		outargflags |= FUSE_INIT_EXT;
@@ -2891,6 +2897,8 @@ _do_init(fuse_req_t req, const fuse_ino_t nodeid, const void *op_in,
 		if (se->conn.want_ext & FUSE_CAP_PASSTHROUGH)
 			fuse_log(FUSE_LOG_DEBUG, "   max_stack_depth=%u\n",
 				outarg.max_stack_depth);
+		if (se->conn.want_ext & FUSE_CAP_IOMAP)
+			fuse_log(FUSE_LOG_DEBUG, "   iomap=1\n");
 	}
 	if (arg->minor < 5)
 		outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
#102 lore raw

PATCH 3/21 libfuse: add fuse commands for iomap_begin and end

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Teach the low level API how to handle iomap begin and end commands that we get from the kernel.

---
 include/fuse_common.h   |   68 +++++++++++++++++++++++++++++++
 include/fuse_kernel.h   |   40 ++++++++++++++++++
 include/fuse_lowlevel.h |   59 +++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |  102 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    3 +
 5 files changed, 272 insertions(+)
include/fuse_common.h +68 -0
index 8f87263d78f999..d10364a077f31d 100644
@@ -1145,7 +1145,75 @@ bool fuse_get_feature_flag(struct fuse_conn_info *conn, uint64_t flag);
  */
 int fuse_convert_to_conn_want_ext(struct fuse_conn_info *conn);
 
+/**
+ * iomap operations.
+ * These APIs are introduced in version 399 (FUSE_MAKE_VERSION(3, 99)).
+ */
 
+/* mapping types; see corresponding IOMAP_TYPE_ */
+#define FUSE_IOMAP_TYPE_HOLE		(0)
+#define FUSE_IOMAP_TYPE_DELALLOC	(1)
+#define FUSE_IOMAP_TYPE_MAPPED		(2)
+#define FUSE_IOMAP_TYPE_UNWRITTEN	(3)
+#define FUSE_IOMAP_TYPE_INLINE		(4)
+
+/* fuse-specific mapping type indicating that writes use the read mapping */
+#define FUSE_IOMAP_TYPE_PURE_OVERWRITE	(255)
+
+#define FUSE_IOMAP_DEV_NULL		(0U)	/* null device cookie */
+
+/* mapping flags passed back from iomap_begin; see corresponding IOMAP_F_ */
+#define FUSE_IOMAP_F_NEW		(1U << 0)
+#define FUSE_IOMAP_F_DIRTY		(1U << 1)
+#define FUSE_IOMAP_F_SHARED		(1U << 2)
+#define FUSE_IOMAP_F_MERGED		(1U << 3)
+#define FUSE_IOMAP_F_BOUNDARY		(1U << 4)
+#define FUSE_IOMAP_F_ANON_WRITE		(1U << 5)
+#define FUSE_IOMAP_F_ATOMIC_BIO		(1U << 6)
+
+/* fuse-specific mapping flag asking for ->iomap_end call */
+#define FUSE_IOMAP_F_WANT_IOMAP_END	(1U << 7)
+
+/* mapping flags passed to iomap_end */
+#define FUSE_IOMAP_F_SIZE_CHANGED	(1U << 8)
+#define FUSE_IOMAP_F_STALE		(1U << 9)
+
+/* operation flags from iomap; see corresponding IOMAP_* */
+#define FUSE_IOMAP_OP_WRITE		(1U << 0)
+#define FUSE_IOMAP_OP_ZERO		(1U << 1)
+#define FUSE_IOMAP_OP_REPORT		(1U << 2)
+#define FUSE_IOMAP_OP_FAULT		(1U << 3)
+#define FUSE_IOMAP_OP_DIRECT		(1U << 4)
+#define FUSE_IOMAP_OP_NOWAIT		(1U << 5)
+#define FUSE_IOMAP_OP_OVERWRITE_ONLY	(1U << 6)
+#define FUSE_IOMAP_OP_UNSHARE		(1U << 7)
+#define FUSE_IOMAP_OP_DAX		(1U << 8)
+#define FUSE_IOMAP_OP_ATOMIC		(1U << 9)
+#define FUSE_IOMAP_OP_DONTCACHE		(1U << 10)
+
+#define FUSE_IOMAP_NULL_ADDR		(-1ULL)	/* addr is not valid */
+
+struct fuse_file_iomap {
+	uint64_t addr;		/* disk offset of mapping, bytes */
+	uint64_t offset;	/* file offset of mapping, bytes */
+	uint64_t length;	/* length of mapping, bytes */
+	uint16_t type;		/* FUSE_IOMAP_TYPE_* */
+	uint16_t flags;		/* FUSE_IOMAP_F_* */
+	uint32_t dev;		/* device cookie */
+};
+
+static inline bool fuse_iomap_is_write(unsigned int opflags)
+{
+	return opflags & (FUSE_IOMAP_OP_WRITE | FUSE_IOMAP_OP_ZERO |
+			  FUSE_IOMAP_OP_UNSHARE);
+}
+
+static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
+					const struct fuse_file_iomap *map)
+{
+	return map->type == FUSE_IOMAP_TYPE_HOLE &&
+		!(opflags & FUSE_IOMAP_OP_ZERO);
+}
 
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
include/fuse_kernel.h +40 -0
index 6779b9c69bb9e2..2bcb3b394c0169 100644
@@ -665,6 +665,9 @@ enum fuse_opcode {
 	FUSE_TMPFILE		= 51,
 	FUSE_STATX		= 52,
 
+	FUSE_IOMAP_BEGIN	= 4094,
+	FUSE_IOMAP_END		= 4095,
+
 	/* CUSE specific operations */
 	CUSE_INIT		= 4096,
 
@@ -1297,4 +1300,41 @@ struct fuse_uring_cmd_req {
 	uint8_t padding[6];
 };
 
+struct fuse_iomap_io {
+	uint64_t offset;	/* file offset of mapping, bytes */
+	uint64_t length;	/* length of mapping, bytes */
+	uint64_t addr;		/* disk offset of mapping, bytes */
+	uint16_t type;		/* FUSE_IOMAP_TYPE_* */
+	uint16_t flags;		/* FUSE_IOMAP_F_* */
+	uint32_t dev;		/* device cookie */
+};
+
+struct fuse_iomap_begin_in {
+	uint32_t opflags;	/* FUSE_IOMAP_OP_* */
+	uint32_t reserved;	/* zero */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t count;		/* operation length, in bytes */
+};
+
+struct fuse_iomap_begin_out {
+	/* read file data from here */
+	struct fuse_iomap_io	read;
+
+	/* write file data to here, if applicable */
+	struct fuse_iomap_io	write;
+};
+
+struct fuse_iomap_end_in {
+	uint32_t opflags;	/* FUSE_IOMAP_OP_* */
+	uint32_t reserved;	/* zero */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t count;		/* operation length, in bytes */
+	int64_t written;	/* bytes processed */
+
+	/* mapping that the kernel acted upon */
+	struct fuse_iomap_io	map;
+};
+
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +59 -0

71 lines not on this page · raw message

lib/fuse_lowlevel.c +102 -0

120 lines not on this page · raw message

lib/fuse_versionscript +3 -0
index ba8f3b00478b30..17c9e538a67bfa 100644
@@ -218,6 +218,9 @@ FUSE_3.18 {
 } FUSE_3.17;
 
 FUSE_3.99 {
+	global:
+		fuse_iomap_pure_overwrite;
+		fuse_reply_iomap_begin;
 } FUSE_3.18;
 
 # Local Variables:
#103 lore raw

PATCH 4/21 libfuse: add upper level iomap commands

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Teach the upper level fuse library about the iomap begin and end operations, and connect it to the lower level. This is needed for fuse2fs to start using iomap.

---
 include/fuse.h |   17 ++++++++++
 lib/fuse.c     |   98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+)
include/fuse.h +17 -0
index 209102651e9454..958034a539abe6 100644
@@ -864,6 +864,23 @@ struct fuse_operations {
 	 */
 	int (*statx)(const char *path, int flags, int mask, struct statx *stxbuf,
 		     struct fuse_file_info *fi);
+
+	/**
+	 * Send a mapping to the kernel so that a file IO operation can run.
+	 */
+	int (*iomap_begin) (const char *path, uint64_t nodeid,
+			    uint64_t attr_ino, off_t pos_in,
+			    uint64_t length_in, uint32_t opflags_in,
+			    struct fuse_file_iomap *read_out,
+			    struct fuse_file_iomap *write_out);
+
+	/**
+	 * Respond to the outcome of a previous file mapping operation.
+	 */
+	int (*iomap_end) (const char *path, uint64_t nodeid, uint64_t attr_ino,
+			  off_t pos_in, uint64_t length_in,
+			  uint32_t opflags_in, ssize_t written_in,
+			  const struct fuse_file_iomap *iomap);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +98 -0

116 lines not on this page · raw message

#104 lore raw

PATCH 5/21 libfuse: add a lowlevel notification to add a new device to iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Plumb in the pieces needed to attach block devices to a fuse+iomap mount for use with iomap operations. This enables us to have filesystems where the metadata could live somewhere else, but the actual file IO goes to locally attached storage.

---
 include/fuse_lowlevel.h |   29 ++++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   48 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    2 ++
 3 files changed, 79 insertions(+)
include/fuse_lowlevel.h +29 -0
index f9704533b5276d..45655781e510a0 100644
@@ -1998,6 +1998,35 @@ int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
 int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino,
 				  size_t size, off_t offset, void *cookie);
 
+/**
+ * Attach an open file descriptor to a fuse+iomap mount.  Currently must be
+ * a block device.
+ *
+ * Added in FUSE protocol version 7.99. If the kernel does not support
+ * this (or a newer) version, the function will return -ENOSYS and do
+ * nothing.
+ *
+ * @param se the session object
+ * @param fd file descriptor of an open block device
+ * @param flags flags for the operation; none defined so far
+ * @return positive nonzero device id on success, or negative errno on failure
+ */
+int fuse_lowlevel_iomap_device_add(struct fuse_session *se, int fd,
+				   unsigned int flags);
+
+/**
+ * Detach an open file from a fuse+iomap mount.  Must be a device id returned
+ * by fuse_lowlevel_iomap_device_add.
+ *
+ * Added in FUSE protocol version 7.99. If the kernel does not support
+ * this (or a newer) version, the function will return -ENOSYS and do
+ * nothing.
+ *
+ * @param se the session object
+ * @param device_id device index as returned by fuse_lowlevel_iomap_device_add
+ * @return 0 on success, or negative errno on failure
+ */
+int fuse_lowlevel_iomap_device_remove(struct fuse_session *se, int device_id);
 
 /* ----------------------------------------------------------- *
  * Utility functions					       *
lib/fuse_lowlevel.c +48 -0

54 lines not on this page · raw message

lib/fuse_versionscript +2 -0
index 17c9e538a67bfa..d785303bab99ea 100644
@@ -221,6 +221,8 @@ FUSE_3.99 {
 	global:
 		fuse_iomap_pure_overwrite;
 		fuse_reply_iomap_begin;
+		fuse_lowlevel_iomap_device_add;
+		fuse_lowlevel_iomap_device_remove;
 } FUSE_3.18;
 
 # Local Variables:
#105 lore raw

PATCH 6/21 libfuse: add upper-level iomap add device function

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it so that the upper level fuse library can add iomap devices too.

---
 include/fuse.h         |   19 +++++++++++++++++++
 lib/fuse.c             |   16 ++++++++++++++++
 lib/fuse_versionscript |    2 ++
 3 files changed, 37 insertions(+)
include/fuse.h +19 -0
index 958034a539abe6..524b77b5d7bbd0 100644
@@ -1381,6 +1381,25 @@ void fuse_fs_init(struct fuse_fs *fs, struct fuse_conn_info *conn,
 		struct fuse_config *cfg);
 void fuse_fs_destroy(struct fuse_fs *fs);
 
+/**
+ * Attach an open file descriptor to a fuse+iomap mount.  Currently must be
+ * a block device.
+ *
+ * @param fd file descriptor of an open block device
+ * @param flags flags for the operation; none defined so far
+ * @return positive nonzero device id on success, or negative errno on failure
+ */
+int fuse_fs_iomap_device_add(int fd, unsigned int flags);
+
+/**
+ * Detach an open file from a fuse+iomap mount.  Must be a device id returned
+ * by fuse_lowlevel_iomap_device_add.
+ *
+ * @param device_id device index as returned by fuse_lowlevel_iomap_device_add
+ * @return 0 on success, or negative errno on failure
+ */
+int fuse_fs_iomap_device_remove(int device_id);
+
 int fuse_notify_poll(struct fuse_pollhandle *ph);
 
 /**
lib/fuse.c +16 -0
index eef0967f796ed6..632e935b8dff3e 100644
@@ -2832,6 +2832,22 @@ static int fuse_fs_iomap_end(struct fuse_fs *fs, const char *path,
 				written, iomap);
 }
 
+int fuse_fs_iomap_device_add(int fd, unsigned int flags)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+
+	return fuse_lowlevel_iomap_device_add(se, fd, flags);
+}
+
+int fuse_fs_iomap_device_remove(int device_id)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+
+	return fuse_lowlevel_iomap_device_remove(se, device_id);
+}
+
 static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
 			     int valid, struct fuse_file_info *fi)
 {
lib/fuse_versionscript +2 -0
index d785303bab99ea..03cce1f0f184c3 100644
@@ -223,6 +223,8 @@ FUSE_3.99 {
 		fuse_reply_iomap_begin;
 		fuse_lowlevel_iomap_device_add;
 		fuse_lowlevel_iomap_device_remove;
+		fuse_fs_iomap_device_add;
+		fuse_fs_iomap_device_remove;
 } FUSE_3.18;
 
 # Local Variables:
#106 lore raw

PATCH 7/21 libfuse: add iomap ioend low level handler

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Teach the low level library about the iomap ioend handler, which gets called by the kernel when we finish a file write that isn't a pure overwrite operation.

---
 include/fuse_common.h   |   11 +++++++++++
 include/fuse_kernel.h   |   11 +++++++++++
 include/fuse_lowlevel.h |   20 ++++++++++++++++++++
 lib/fuse_lowlevel.c     |   23 +++++++++++++++++++++++
 4 files changed, 65 insertions(+)
include/fuse_common.h +11 -0
index d10364a077f31d..77e971c3fed17d 100644
@@ -1215,6 +1215,17 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 		!(opflags & FUSE_IOMAP_OP_ZERO);
 }
 
+/* out of place write extent */
+#define FUSE_IOMAP_IOEND_SHARED		(1U << 0)
+/* unwritten extent */
+#define FUSE_IOMAP_IOEND_UNWRITTEN	(1U << 1)
+/* don't merge into previous ioend */
+#define FUSE_IOMAP_IOEND_BOUNDARY	(1U << 2)
+/* is direct I/O */
+#define FUSE_IOMAP_IOEND_DIRECT		(1U << 3)
+/* is append ioend */
+#define FUSE_IOMAP_IOEND_APPEND		(1U << 4)
+
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
  * ----------------------------------------------------------- */
include/fuse_kernel.h +11 -0
index 2bcb3b394c0169..849238c17baf5e 100644
@@ -665,6 +665,7 @@ enum fuse_opcode {
 	FUSE_TMPFILE		= 51,
 	FUSE_STATX		= 52,
 
+	FUSE_IOMAP_IOEND	= 4093,
 	FUSE_IOMAP_BEGIN	= 4094,
 	FUSE_IOMAP_END		= 4095,
 
@@ -1337,4 +1338,14 @@ struct fuse_iomap_end_in {
 	struct fuse_iomap_io	map;
 };
 
+struct fuse_iomap_ioend_in {
+	uint32_t ioendflags;	/* FUSE_IOMAP_IOEND_* */
+	int32_t error;		/* negative errno or 0 */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+	uint64_t pos;		/* file position, in bytes */
+	uint64_t new_addr;	/* disk offset of new mapping, in bytes */
+	uint32_t written;	/* bytes processed */
+	uint32_t reserved1;	/* zero */
+};
+
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +20 -0
index 45655781e510a0..7f7f418b281601 100644
@@ -1379,6 +1379,26 @@ struct fuse_lowlevel_ops {
 	void (*iomap_end) (fuse_req_t req, fuse_ino_t nodeid, uint64_t attr_ino,
 			   off_t pos, uint64_t count, uint32_t opflags,
 			   ssize_t written, const struct fuse_file_iomap *iomap);
+
+	/**
+	 * Complete an iomap IO operation
+	 *
+	 * Valid replies:
+	 *   fuse_reply_err
+	 *
+	 * @param req request handle
+	 * @param nodeid the inode number
+	 * @param attr_ino inode number as told by fuse_attr::ino
+	 * @param pos position in file, in bytes
+	 * @param written number of bytes processed, or a negative errno
+	 * @param ioendflags mask of FUSE_IOMAP_IOEND_ flags specifying operation
+	 * @param error errno code of what went wrong
+	 * @param new_addr disk address of new mapping, in bytes
+	 */
+	void (*iomap_ioend) (fuse_req_t req, fuse_ino_t nodeid,
+			     uint64_t attr_ino, off_t pos, size_t written,
+			     uint32_t ioendflags, int error,
+			     uint64_t new_addr);
 };
 
 /**
lib/fuse_lowlevel.c +23 -0
index fec4e3265e53c1..ce7971a23be94b 100644
@@ -2637,6 +2637,27 @@ static void do_iomap_end(fuse_req_t req, const fuse_ino_t nodeid,
 	_do_iomap_end(req, nodeid, inarg, NULL);
 }
 
+static void _do_iomap_ioend(fuse_req_t req, const fuse_ino_t nodeid,
+			    const void *op_in, const void *in_payload)
+{
+	const struct fuse_iomap_ioend_in *arg = op_in;
+	(void)in_payload;
+	(void)nodeid;
+
+	if (req->se->op.iomap_ioend)
+		req->se->op.iomap_ioend(req, nodeid, arg->attr_ino, arg->pos,
+					arg->written, arg->ioendflags,
+					arg->error, arg->new_addr);
+	else
+		fuse_reply_err(req, ENOSYS);
+}
+
+static void do_iomap_ioend(fuse_req_t req, const fuse_ino_t nodeid,
+			   const void *inarg)
+{
+	_do_iomap_ioend(req, nodeid, inarg, NULL);
+}
+
 static bool want_flags_valid(uint64_t capable, uint64_t want)
 {
 	uint64_t unknown_flags = want & (~capable);
@@ -3524,6 +3545,7 @@ static struct {
 	[FUSE_STATX]	   = { do_statx,       "STATX"	     },
 	[FUSE_IOMAP_BEGIN] = { do_iomap_begin,	"IOMAP_BEGIN" },
 	[FUSE_IOMAP_END]   = { do_iomap_end,	"IOMAP_END" },
+	[FUSE_IOMAP_IOEND] = { do_iomap_ioend,	"IOMAP_IOEND" },
 	[CUSE_INIT]	   = { cuse_lowlevel_init, "CUSE_INIT"   },
 };
 
@@ -3581,6 +3603,7 @@ static struct {
 	[FUSE_STATX]		= { _do_statx,		"STATX" },
 	[FUSE_IOMAP_BEGIN]	= { _do_iomap_begin,	"IOMAP_BEGIN" },
 	[FUSE_IOMAP_END]	= { _do_iomap_end,	"IOMAP_END" },
+	[FUSE_IOMAP_IOEND]	= { _do_iomap_ioend,	"IOMAP_IOEND" },
 	[CUSE_INIT]		= { _cuse_lowlevel_init, "CUSE_INIT" },
 };
 
#107 lore raw

PATCH 8/21 libfuse: add upper level iomap ioend commands

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Teach the upper level fuse library about iomap ioend events, which happen when a write that isn't a pure overwrite completes.

---
 include/fuse.h |    8 ++++++++
 lib/fuse.c     |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
include/fuse.h +8 -0
index 524b77b5d7bbd0..1357f4319bcc21 100644
@@ -881,6 +881,14 @@ struct fuse_operations {
 			  off_t pos_in, uint64_t length_in,
 			  uint32_t opflags_in, ssize_t written_in,
 			  const struct fuse_file_iomap *iomap);
+
+	/**
+	 * Respond to the outcome of a file IO operation.
+	 */
+	int (*iomap_ioend) (const char *path, uint64_t nodeid,
+			    uint64_t attr_ino, off_t pos_in, size_t written_in,
+			    uint32_t ioendflags_in, int error_in,
+			    uint64_t new_addr_in);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +45 -0

63 lines not on this page · raw message

#108 lore raw

PATCH 9/21 libfuse: add a reply function to send FUSE_ATTR_* to the kernel

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Create new fuse_reply_{attr,create,entry}_iflags functions so that we can send FUSE_ATTR_* flags to the kernel when instantiating an inode. Servers are expected to send FUSE_IFLAG_* values, which will be translated into what the kernel can understand.

---
 include/fuse_common.h   |    3 ++
 include/fuse_lowlevel.h |   83 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   64 ++++++++++++++++++++++++++++--------
 lib/fuse_versionscript  |    4 ++
 4 files changed, 139 insertions(+), 15 deletions(-)
include/fuse_common.h +3 -0
index 77e971c3fed17d..9181ec6cb5e5e9 100644
@@ -1226,6 +1226,9 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 /* is append ioend */
 #define FUSE_IOMAP_IOEND_APPEND		(1U << 4)
 
+/* enable fsdax */
+#define FUSE_IFLAG_DAX			(1U << 0)
+
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
  * ----------------------------------------------------------- */
include/fuse_lowlevel.h +83 -0

161 lines not on this page · raw message

lib/fuse_lowlevel.c +49 -15

140 lines not on this page · raw message

lib/fuse_versionscript +4 -0
index 03cce1f0f184c3..df78723e0f2518 100644
@@ -225,6 +225,10 @@ FUSE_3.99 {
 		fuse_lowlevel_iomap_device_remove;
 		fuse_fs_iomap_device_add;
 		fuse_fs_iomap_device_remove;
+		fuse_reply_attr_iflags;
+		fuse_reply_create_iflags;
+		fuse_reply_entry_iflags;
+		fuse_add_direntry_plus_iflags;
 } FUSE_3.18;
 
 # Local Variables:
#109 lore raw

PATCH 10/21 libfuse: connect high level fuse library to fuse_reply_attr_iflags

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Create a new ->getattr_iflags function so that iomap filesystems can set the appropriate in-kernel inode flags on instantiation.

---
 include/fuse.h |    7 ++
 lib/fuse.c     |  191 ++++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 151 insertions(+), 47 deletions(-)
include/fuse.h +7 -0
index 1357f4319bcc21..7256f43fd5c39a 100644
@@ -889,6 +889,13 @@ struct fuse_operations {
 			    uint64_t attr_ino, off_t pos_in, size_t written_in,
 			    uint32_t ioendflags_in, int error_in,
 			    uint64_t new_addr_in);
+
+	/**
+	 * Get file attributes and FUSE_IFLAG_* flags.  Otherwise the same as
+	 * getattr.
+	 */
+	int (*getattr_iflags) (const char *path, struct stat *buf,
+			       unsigned int *iflags, struct fuse_file_info *fi);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +144 -47

447 lines not on this page · raw message

#110 lore raw

PATCH 11/21 libfuse: support direct I/O through iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it so that fuse servers can ask the kernel fuse driver to use iomap to support direct IO.

---
 include/fuse_common.h |    2 ++
 include/fuse_kernel.h |    7 +++++--
 lib/fuse_lowlevel.c   |    2 ++
 3 files changed, 9 insertions(+), 2 deletions(-)
include/fuse_common.h +2 -0
index 9181ec6cb5e5e9..6e8b2958373258 100644
@@ -1228,6 +1228,8 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 
 /* enable fsdax */
 #define FUSE_IFLAG_DAX			(1U << 0)
+/* use iomap for this inode */
+#define FUSE_IFLAG_IOMAP		(1U << 1)
 
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
include/fuse_kernel.h +5 -2
index 849238c17baf5e..86c81871ca2b37 100644
@@ -238,7 +238,8 @@
  *
  *  7.99
  *  - add FUSE_IOMAP and iomap_{begin,end,ioend} handlers for FIEMAP and
- *    SEEK_{DATA,HOLE}
+ *    SEEK_{DATA,HOLE}, and direct I/O
+ *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  */
 
 #ifndef _LINUX_FUSE_H
@@ -448,7 +449,7 @@ struct fuse_file_lock {
  * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests.
  *			 init_out.request_timeout contains the timeout (in secs)
  * FUSE_IOMAP: Client supports iomap for FIEMAP and SEEK_{DATA,HOLE} file
- *	       operations.
+ *	       operations and direct I/O.
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
@@ -580,9 +581,11 @@ struct fuse_file_lock {
  *
  * FUSE_ATTR_SUBMOUNT: Object is a submount root
  * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode
+ * FUSE_ATTR_IOMAP: Use iomap for this inode
  */
 #define FUSE_ATTR_SUBMOUNT      (1 << 0)
 #define FUSE_ATTR_DAX		(1 << 1)
+#define FUSE_ATTR_IOMAP		(1 << 2)
 
 /**
  * Open flags
lib/fuse_lowlevel.c +2 -0
index 04bc858f54d01f..6a96c0f62d5884 100644
@@ -124,6 +124,8 @@ static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr,
 	attr->flags	= 0;
 	if (iflags & FUSE_IFLAG_DAX)
 		attr->flags |= FUSE_ATTR_DAX;
+	if (iflags & FUSE_IFLAG_IOMAP)
+		attr->flags |= FUSE_ATTR_IOMAP;
 }
 
 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
#111 lore raw

PATCH 12/21 libfuse: support buffered I/O through iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it so that fuse servers can ask the kernel fuse driver to use iomap to support buffered IO.

---
 include/fuse_kernel.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/fuse_kernel.h +2 -2
index 86c81871ca2b37..eafad773a1fd5f 100644
@@ -238,7 +238,7 @@
  *
  *  7.99
  *  - add FUSE_IOMAP and iomap_{begin,end,ioend} handlers for FIEMAP and
- *    SEEK_{DATA,HOLE}, and direct I/O
+ *    SEEK_{DATA,HOLE}, buffered I/O, and direct I/O
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  */
 
@@ -449,7 +449,7 @@ struct fuse_file_lock {
  * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests.
  *			 init_out.request_timeout contains the timeout (in secs)
  * FUSE_IOMAP: Client supports iomap for FIEMAP and SEEK_{DATA,HOLE} file
- *	       operations and direct I/O.
+ *	       operations, buffered I/O, and direct I/O.
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
#112 lore raw

PATCH 13/21 libfuse: don't allow hardlinking of iomap files in the upper level fuse library

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

The upper level fuse library creates a separate node object for every (i)node referenced by a directory entry. Unfortunately, it doesn't account for the possibility of hardlinks, which means that we can create multiple nodeids that refer to the same hardlinked inode. Inode locking in iomap mode in the kernel relies there only being one inode object for a hardlinked file, so we cannot allow anyone to hardlink an iomap file. The client had better not turn on iomap for an existing hardlinked file.

---
 include/fuse.h         |   18 ++++++++++
 lib/fuse.c             |   90 +++++++++++++++++++++++++++++++++++++++++++-----
 lib/fuse_versionscript |    2 +
 3 files changed, 101 insertions(+), 9 deletions(-)
include/fuse.h +18 -0
index 7256f43fd5c39a..4c4fff837437c8 100644
@@ -1415,6 +1415,24 @@ int fuse_fs_iomap_device_add(int fd, unsigned int flags);
  */
 int fuse_fs_iomap_device_remove(int device_id);
 
+/**
+ * Decide if we can enable iomap mode for a particular file for an upper-level
+ * fuse server.
+ *
+ * @param statbuf stat information for the file.
+ * @return true if it can be enabled, false if not.
+ */
+bool fuse_fs_can_enable_iomap(const struct stat *statbuf);
+
+/**
+ * Decide if we can enable iomap mode for a particular file for an upper-level
+ * fuse server.
+ *
+ * @param statxbuf statx information for the file.
+ * @return true if it can be enabled, false if not.
+ */
+bool fuse_fs_can_enable_iomapx(const struct statx *statxbuf);
+
 int fuse_notify_poll(struct fuse_pollhandle *ph);
 
 /**
lib/fuse.c +81 -9

108 lines not on this page · raw message

lib/fuse_versionscript +2 -0
index df78723e0f2518..aa16efdd8a9879 100644
@@ -229,6 +229,8 @@ FUSE_3.99 {
 		fuse_reply_create_iflags;
 		fuse_reply_entry_iflags;
 		fuse_add_direntry_plus_iflags;
+		fuse_fs_can_enable_iomap;
+		fuse_fs_can_enable_iomapx;
 } FUSE_3.18;
 
 # Local Variables:
#113 lore raw

PATCH 14/21 libfuse: allow discovery of the kernel's iomap capabilities

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Create a library function so that we can discover the kernel's iomap capabilities ahead of time.

---
 include/fuse_common.h   |    7 +++++++
 include/fuse_kernel.h   |    7 +++++++
 include/fuse_lowlevel.h |    5 +++++
 lib/fuse_lowlevel.c     |   15 +++++++++++++++
 lib/fuse_versionscript  |    1 +
 5 files changed, 35 insertions(+)
include/fuse_common.h +7 -0
index 6e8b2958373258..f9cc3702411680 100644
@@ -542,6 +542,13 @@ struct fuse_loop_config_v1 {
 
 #define FUSE_IOCTL_MAX_IOV	256
 
+/**
+ * iomap discovery flags
+ *
+ * FUSE_IOMAP_SUPPORT_FILEIO: basic file I/O functionality through iomap
+ */
+#define FUSE_IOMAP_SUPPORT_FILEIO	(1ULL << 0)
+
 /**
  * Connection information, passed to the ->init() method
  *
include/fuse_kernel.h +7 -0
index eafad773a1fd5f..dbd2ce1fbbe6ed 100644
@@ -1134,12 +1134,19 @@ struct fuse_backing_map {
 	uint64_t	padding;
 };
 
+struct fuse_iomap_support {
+	uint64_t	flags;
+	uint64_t	padding;
+};
+
 /* Device ioctls: */
 #define FUSE_DEV_IOC_MAGIC		229
 #define FUSE_DEV_IOC_CLONE		_IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t)
 #define FUSE_DEV_IOC_BACKING_OPEN	_IOW(FUSE_DEV_IOC_MAGIC, 1, \
 					     struct fuse_backing_map)
 #define FUSE_DEV_IOC_BACKING_CLOSE	_IOW(FUSE_DEV_IOC_MAGIC, 2, uint32_t)
+#define FUSE_DEV_IOC_IOMAP_SUPPORT	_IOR(FUSE_DEV_IOC_MAGIC, 3, \
+					     struct fuse_iomap_support)
 
 struct fuse_lseek_in {
 	uint64_t	fh;
include/fuse_lowlevel.h +5 -0
index e0642032127686..2931a57ec4079b 100644
@@ -2556,6 +2556,11 @@ int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf);
  */
 bool fuse_req_is_uring(fuse_req_t req);
 
+/**
+ * Discover the kernel's iomap capabilities.  Returns FUSE_CAP_IOMAP_* flags.
+ */
+uint64_t fuse_lowlevel_discover_iomap(void);
+
 #ifdef __cplusplus
 }
 #endif
lib/fuse_lowlevel.c +15 -0
index 6a96c0f62d5884..ab10204c8042d9 100644
@@ -4603,3 +4603,18 @@ int fuse_session_exited(struct fuse_session *se)
 
 	return exited ? 1 : 0;
 }
+
+uint64_t fuse_lowlevel_discover_iomap(void)
+{
+	struct fuse_iomap_support ios = { };
+	int fd;
+
+	fd = open("/dev/fuse", O_RDONLY | O_CLOEXEC);
+	if (fd < 0)
+		return 0;
+
+	ioctl(fd, FUSE_DEV_IOC_IOMAP_SUPPORT, &ios);
+	close(fd);
+
+	return ios.flags;
+}
lib/fuse_versionscript +1 -0
index aa16efdd8a9879..5275a17ba1ed51 100644
@@ -231,6 +231,7 @@ FUSE_3.99 {
 		fuse_add_direntry_plus_iflags;
 		fuse_fs_can_enable_iomap;
 		fuse_fs_can_enable_iomapx;
+		fuse_lowlevel_discover_iomap;
 } FUSE_3.18;
 
 # Local Variables:
#114 lore raw

PATCH 15/21 libfuse: add lower level iomap_config implementation

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add FUSE_IOMAP_CONFIG helpers to the low level fuse library.

---
 include/fuse_common.h   |   31 ++++++++++++++++++
 include/fuse_kernel.h   |   31 ++++++++++++++++++
 include/fuse_lowlevel.h |   27 +++++++++++++++
 lib/fuse_lowlevel.c     |   82 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    1 +
 5 files changed, 172 insertions(+)
include/fuse_common.h +31 -0
index f9cc3702411680..8e585cc7483643 100644
@@ -1238,6 +1238,37 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 /* use iomap for this inode */
 #define FUSE_IFLAG_IOMAP		(1U << 1)
 
+/* Which fields are set in fuse_iomap_config_out? */
+#define FUSE_IOMAP_CONFIG_SID		(1 << 0ULL)
+#define FUSE_IOMAP_CONFIG_UUID		(1 << 1ULL)
+#define FUSE_IOMAP_CONFIG_BLOCKSIZE	(1 << 2ULL)
+#define FUSE_IOMAP_CONFIG_MAX_LINKS	(1 << 3ULL)
+#define FUSE_IOMAP_CONFIG_TIME		(1 << 4ULL)
+#define FUSE_IOMAP_CONFIG_MAXBYTES	(1 << 5ULL)
+
+struct fuse_iomap_config{
+	uint64_t flags;		/* FUSE_IOMAP_CONFIG_* */
+
+	char s_id[32];		/* Informational name */
+	char s_uuid[16];	/* UUID */
+
+	uint8_t s_uuid_len;	/* length of s_uuid */
+
+	uint8_t s_pad[3];	/* must be zeroes */
+
+	uint32_t s_blocksize;	/* fs block size */
+	uint32_t s_max_links;	/* max hard links */
+
+	/* Granularity of c/m/atime in ns (cannot be worse than a second) */
+	uint32_t s_time_gran;
+
+	/* Time limits for c/m/atime in seconds */
+	int64_t s_time_min;
+	int64_t s_time_max;
+
+	int64_t s_maxbytes;	/* max file size */
+};
+
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
  * ----------------------------------------------------------- */
include/fuse_kernel.h +31 -0
index dbd2ce1fbbe6ed..46960711691d99 100644
@@ -240,6 +240,7 @@
  *  - add FUSE_IOMAP and iomap_{begin,end,ioend} handlers for FIEMAP and
  *    SEEK_{DATA,HOLE}, buffered I/O, and direct I/O
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
+ *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
  */
 
 #ifndef _LINUX_FUSE_H
@@ -668,6 +669,7 @@ enum fuse_opcode {
 	FUSE_TMPFILE		= 51,
 	FUSE_STATX		= 52,
 
+	FUSE_IOMAP_CONFIG	= 4092,
 	FUSE_IOMAP_IOEND	= 4093,
 	FUSE_IOMAP_BEGIN	= 4094,
 	FUSE_IOMAP_END		= 4095,
@@ -1358,4 +1360,33 @@ struct fuse_iomap_ioend_in {
 	uint32_t reserved1;	/* zero */
 };
 
+struct fuse_iomap_config_in {
+	uint64_t flags;		/* supported FUSE_IOMAP_CONFIG_* flags */
+	int64_t maxbytes;	/* max supported file size */
+	uint64_t padding[6];	/* zero */
+};
+
+struct fuse_iomap_config_out {
+	uint64_t flags;		/* FUSE_IOMAP_CONFIG_* */
+
+	char s_id[32];		/* Informational name */
+	char s_uuid[16];	/* UUID */
+
+	uint8_t s_uuid_len;	/* length of s_uuid */
+
+	uint8_t s_pad[3];	/* must be zeroes */
+
+	uint32_t s_blocksize;	/* fs block size */
+	uint32_t s_max_links;	/* max hard links */
+
+	/* Granularity of c/m/atime in ns (cannot be worse than a second) */
+	uint32_t s_time_gran;
+
+	/* Time limits for c/m/atime in seconds */
+	int64_t s_time_min;
+	int64_t s_time_max;
+
+	int64_t s_maxbytes;	/* max file size */
+};
+
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +27 -0
index 2931a57ec4079b..1b2a6c00d0f9dc 100644
@@ -1408,6 +1408,20 @@ struct fuse_lowlevel_ops {
 			     uint64_t attr_ino, off_t pos, size_t written,
 			     uint32_t ioendflags, int error,
 			     uint64_t new_addr);
+
+	/**
+	 * Configure the filesystem geometry for iomap mode
+	 *
+	 * Valid replies:
+	 *   fuse_reply_iomap_config
+	 *   fuse_reply_err
+	 *
+	 * @param req request handle
+	 * @param flags FUSE_IOMAP_CONFIG_* flags that can be passed back
+	 * @param maxbytes maximum supported file size
+	 */
+	void (*iomap_config) (fuse_req_t req, uint64_t flags,
+			      uint64_t maxbytes);
 };
 
 /**
@@ -1898,6 +1912,19 @@ void fuse_iomap_pure_overwrite(struct fuse_file_iomap *write,
 int fuse_reply_iomap_begin(fuse_req_t req, const struct fuse_file_iomap *read,
 			   const struct fuse_file_iomap *write);
 
+/**
+ * Reply with iomap configuration
+ *
+ * Possible requests:
+ *   iomap_config
+ *
+ * @param req request handle
+ * @param cfg iomap configuration
+ * @return zero for success, -errno for failure to send reply
+ */
+int fuse_reply_iomap_config(fuse_req_t req,
+			    const struct fuse_iomap_config *cfg);
+
 /* ----------------------------------------------------------- *
  * Notification						       *
  * ----------------------------------------------------------- */
lib/fuse_lowlevel.c +82 -0

100 lines not on this page · raw message

lib/fuse_versionscript +1 -0
index 5275a17ba1ed51..f886d268c8a99f 100644
@@ -232,6 +232,7 @@ FUSE_3.99 {
 		fuse_fs_can_enable_iomap;
 		fuse_fs_can_enable_iomapx;
 		fuse_lowlevel_discover_iomap;
+		fuse_reply_iomap_config;
 } FUSE_3.18;
 
 # Local Variables:
#115 lore raw

PATCH 16/21 libfuse: add upper level iomap_config implementation

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add FUSE_IOMAP_CONFIG helpers to the upper level fuse library.

---
 include/fuse.h |    7 +++++++
 lib/fuse.c     |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/fuse.h +7 -0
index 4c4fff837437c8..74b86e8d27fb35 100644
@@ -896,6 +896,13 @@ struct fuse_operations {
 	 */
 	int (*getattr_iflags) (const char *path, struct stat *buf,
 			       unsigned int *iflags, struct fuse_file_info *fi);
+
+	/**
+	 * Configure the filesystem geometry that will be used by iomap
+	 * files.
+	 */
+	int (*iomap_config) (uint64_t supported_flags, off_t maxbytes,
+			     struct fuse_iomap_config *cfg);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +37 -0
index cbf2c5d3a67895..177c524eff736b 100644
@@ -2937,6 +2937,23 @@ static int fuse_fs_iomap_ioend(struct fuse_fs *fs, const char *path,
 				  ioendflags, error, new_addr);
 }
 
+static int fuse_fs_iomap_config(struct fuse_fs *fs, uint64_t flags,
+				uint64_t maxbytes,
+				struct fuse_iomap_config *cfg)
+{
+	fuse_get_context()->private_data = fs->user_data;
+	if (!fs->op.iomap_config)
+		return -ENOSYS;
+
+	if (fs->debug) {
+		fuse_log(FUSE_LOG_DEBUG,
+			 "iomap_config flags 0x%x maxbytes %lld\n",
+			 flags, (long long)maxbytes);
+	}
+
+	return fs->op.iomap_config(flags, maxbytes, cfg);
+}
+
 static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
 			     int valid, struct fuse_file_info *fi)
 {
@@ -4791,6 +4808,25 @@ static void fuse_lib_iomap_ioend(fuse_req_t req, fuse_ino_t nodeid,
 	reply_err(req, err);
 }
 
+static void fuse_lib_iomap_config(fuse_req_t req, uint64_t flags,
+				  uint64_t maxbytes)
+{
+	struct fuse_iomap_config cfg = { };
+	struct fuse *f = req_fuse_prepare(req);
+	struct fuse_intr_data d;
+	int err;
+
+	fuse_prepare_interrupt(f, req, &d);
+	err = fuse_fs_iomap_config(f->fs, flags, maxbytes, &cfg);
+	fuse_finish_interrupt(f, req, &d);
+	if (err) {
+		reply_err(req, err);
+		return;
+	}
+
+	fuse_reply_iomap_config(req, &cfg);
+}
+
 static int clean_delay(struct fuse *f)
 {
 	/*
@@ -4895,6 +4931,7 @@ static struct fuse_lowlevel_ops fuse_path_ops = {
 	.iomap_begin = fuse_lib_iomap_begin,
 	.iomap_end = fuse_lib_iomap_end,
 	.iomap_ioend = fuse_lib_iomap_ioend,
+	.iomap_config = fuse_lib_iomap_config,
 };
 
 int fuse_notify_poll(struct fuse_pollhandle *ph)
#116 lore raw

PATCH 17/21 libfuse: allow root_nodeid mount option

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Allow this mount option so that fuse servers can configure the root nodeid if they want to.

---
 lib/mount.c |    1 +
 1 file changed, 1 insertion(+)
lib/mount.c +1 -0
index 2eb967399c9606..140489fa74bb55 100644
@@ -100,6 +100,7 @@ static const struct fuse_opt fuse_mount_opts[] = {
 	FUSE_OPT_KEY("defcontext=",		KEY_KERN_OPT),
 	FUSE_OPT_KEY("rootcontext=",		KEY_KERN_OPT),
 	FUSE_OPT_KEY("max_read=",		KEY_KERN_OPT),
+	FUSE_OPT_KEY("root_nodeid=",		KEY_KERN_OPT),
 	FUSE_OPT_KEY("user=",			KEY_MTAB_OPT),
 	FUSE_OPT_KEY("-n",			KEY_MTAB_OPT),
 	FUSE_OPT_KEY("-r",			KEY_RO),
#117 lore raw

PATCH 18/21 libfuse: add low level code to invalidate iomap block device ranges

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it easier to invalidate the page cache for a block device that is being used in conjunction with iomap. This allows a fuse server to kill all cached data for a block that is being freed, so that block reuse doesn't result in file corruption.

---
 include/fuse_kernel.h   |    9 +++++++++
 include/fuse_lowlevel.h |   15 +++++++++++++++
 lib/fuse_lowlevel.c     |   22 ++++++++++++++++++++++
 lib/fuse_versionscript  |    1 +
 4 files changed, 47 insertions(+)
include/fuse_kernel.h +9 -0
index 46960711691d99..1470b59d742165 100644
@@ -241,6 +241,7 @@
  *    SEEK_{DATA,HOLE}, buffered I/O, and direct I/O
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
+ *  - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges
  */
 
 #ifndef _LINUX_FUSE_H
@@ -691,6 +692,7 @@ enum fuse_notify_code {
 	FUSE_NOTIFY_DELETE = 6,
 	FUSE_NOTIFY_RESEND = 7,
 	FUSE_NOTIFY_INC_EPOCH = 8,
+	FUSE_NOTIFY_IOMAP_DEV_INVAL = 9,
 	FUSE_NOTIFY_CODE_MAX,
 };
 
@@ -1389,4 +1391,11 @@ struct fuse_iomap_config_out {
 	int64_t s_maxbytes;	/* max file size */
 };
 
+struct fuse_iomap_dev_inval {
+	uint32_t dev;		/* device cookie */
+	uint32_t reserved;	/* zero */
+
+	uint64_t offset;	/* range to invalidate pagecache, bytes */
+	uint64_t length;
+};
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +15 -0
index 1b2a6c00d0f9dc..b7a099bea6921e 100644
@@ -2158,6 +2158,21 @@ int fuse_lowlevel_iomap_device_add(struct fuse_session *se, int fd,
  */
 int fuse_lowlevel_iomap_device_remove(struct fuse_session *se, int device_id);
 
+/*
+ * Invalidate the page cache of a block device opened for use with iomap.
+ *
+ * Added in FUSE protocol version 7.99. If the kernel does not support
+ * this (or a newer) version, the function will return -ENOSYS and do
+ * nothing.
+ *
+ * @param se the session object
+ * @param dev device cookie returned by fuse_lowlevel_iomap_add_device
+ * @param offset start of the range to invalidate, in bytes
+ * @return length length of the range to invalidate, in bytes
+ */
+int fuse_lowlevel_iomap_device_invalidate(struct fuse_session *se, int dev,
+					  off_t offset, off_t length);
+
 /* ----------------------------------------------------------- *
  * Utility functions					       *
  * ----------------------------------------------------------- */
lib/fuse_lowlevel.c +22 -0
index 60627ec35cd367..f730a7fd4ead09 100644
@@ -3480,6 +3480,28 @@ int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
 	return res;
 }
 
+int fuse_lowlevel_iomap_device_invalidate(struct fuse_session *se, int dev,
+					  off_t offset, off_t length)
+{
+	struct fuse_iomap_dev_inval arg = {
+		.dev = dev,
+		.offset = offset,
+		.length = length,
+	};
+	struct iovec iov[2];
+
+	if (!se)
+		return -EINVAL;
+
+	if (!(se->conn.want_ext & FUSE_CAP_IOMAP))
+		return -ENOSYS;
+
+	iov[1].iov_base = &arg;
+	iov[1].iov_len = sizeof(arg);
+
+	return send_notify_iov(se, FUSE_NOTIFY_IOMAP_DEV_INVAL, iov, 2);
+}
+
 struct fuse_retrieve_req {
 	struct fuse_notify_req nreq;
 	void *cookie;
lib/fuse_versionscript +1 -0
index f886d268c8a99f..65ce70649b031c 100644
@@ -233,6 +233,7 @@ FUSE_3.99 {
 		fuse_fs_can_enable_iomapx;
 		fuse_lowlevel_discover_iomap;
 		fuse_reply_iomap_config;
+		fuse_lowlevel_iomap_device_invalidate;
 } FUSE_3.18;
 
 # Local Variables:
#118 lore raw

PATCH 19/21 libfuse: add upper-level API to invalidate parts of an iomap block device

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Wire up the upper-level wrappers to
fuse_lowlevel_iomap_invalidate_device.

---
 include/fuse.h         |   10 ++++++++++
 lib/fuse.c             |    9 +++++++++
 lib/fuse_versionscript |    1 +
 3 files changed, 20 insertions(+)
include/fuse.h +10 -0
index 74b86e8d27fb35..e53e92786cea08 100644
@@ -1422,6 +1422,16 @@ int fuse_fs_iomap_device_add(int fd, unsigned int flags);
  */
 int fuse_fs_iomap_device_remove(int device_id);
 
+/**
+ * Invalidate any pagecache for the given iomap (block) device.
+ *
+ * @param device_id device index as returned by fuse_lowlevel_iomap_device_add
+ * @param offset starting offset of the range to invalidate
+ * @param length length of the range to invalidate
+ * @return 0 on success, or negative errno on failure
+ */
+int fuse_fs_iomap_device_invalidate(int device_id, off_t offset, off_t length);
+
 /**
  * Decide if we can enable iomap mode for a particular file for an upper-level
  * fuse server.
lib/fuse.c +9 -0
index 177c524eff736b..1c813ec5a697a0 100644
@@ -2917,6 +2917,15 @@ int fuse_fs_iomap_device_remove(int device_id)
 	return fuse_lowlevel_iomap_device_remove(se, device_id);
 }
 
+int fuse_fs_iomap_device_invalidate(int device_id, off_t offset, off_t length)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+
+	return fuse_lowlevel_iomap_device_invalidate(se, device_id, offset,
+						     length);
+}
+
 static int fuse_fs_iomap_ioend(struct fuse_fs *fs, const char *path,
 			       uint64_t nodeid, uint64_t attr_ino, off_t pos,
 			       size_t written, uint32_t ioendflags, int error,
lib/fuse_versionscript +1 -0
index 65ce70649b031c..102f449d28a0be 100644
@@ -234,6 +234,7 @@ FUSE_3.99 {
 		fuse_lowlevel_discover_iomap;
 		fuse_reply_iomap_config;
 		fuse_lowlevel_iomap_device_invalidate;
+		fuse_fs_iomap_device_invalidate;
 } FUSE_3.18;
 
 # Local Variables:
#119 lore raw

PATCH 20/21 libfuse: add strictatime/lazytime mount options

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

fuse+iomap leaves the kernel completely in charge of handling timestamps. Add the lazytime and strictatime mount options so that fuse+iomap filesystems can take advantage of those options.

---
 lib/mount.c |   18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
lib/mount.c +16 -2
index 140489fa74bb55..01d473902d50d7 100644
@@ -117,9 +117,16 @@ static const struct fuse_opt fuse_mount_opts[] = {
 	FUSE_OPT_KEY("dirsync",			KEY_KERN_FLAG),
 	FUSE_OPT_KEY("noatime",			KEY_KERN_FLAG),
 	FUSE_OPT_KEY("nodiratime",		KEY_KERN_FLAG),
-	FUSE_OPT_KEY("nostrictatime",		KEY_KERN_FLAG),
 	FUSE_OPT_KEY("symfollow",		KEY_KERN_FLAG),
 	FUSE_OPT_KEY("nosymfollow",		KEY_KERN_FLAG),
+#ifdef MS_LAZYTIME
+	FUSE_OPT_KEY("lazytime",		KEY_KERN_FLAG),
+	FUSE_OPT_KEY("nolazytime",		KEY_KERN_FLAG),
+#endif
+#ifdef MS_STRICTATIME
+	FUSE_OPT_KEY("strictatime",		KEY_KERN_FLAG),
+	FUSE_OPT_KEY("nostrictatime",		KEY_KERN_FLAG),
+#endif
 	FUSE_OPT_END
 };
 
@@ -190,11 +197,18 @@ static const struct mount_flags mount_flags[] = {
 	{"noatime", MS_NOATIME,	    1},
 	{"nodiratime",	    MS_NODIRATIME,	1},
 	{"norelatime",	    MS_RELATIME,	0},
-	{"nostrictatime",   MS_STRICTATIME,	0},
 	{"symfollow",	    MS_NOSYMFOLLOW,	0},
 	{"nosymfollow",	    MS_NOSYMFOLLOW,	1},
 #ifndef __NetBSD__
 	{"dirsync", MS_DIRSYNC,	    1},
+#endif
+#ifdef MS_LAZYTIME
+	{"lazytime",	    MS_LAZYTIME,	1},
+	{"nolazytime",	    MS_LAZYTIME,	0},
+#endif
+#ifdef MS_STRICTATIME
+	{"strictatime",	    MS_STRICTATIME,	1},
+	{"nostrictatime",   MS_STRICTATIME,	0},
 #endif
 	{NULL,	    0,		    0}
 };
#120 lore raw

PATCH 21/21 libfuse: add atomic write support

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Add the single flag that we need to turn on atomic write support in fuse.

---
 include/fuse_common.h |    4 ++++
 include/fuse_kernel.h |    3 +++
 lib/fuse_lowlevel.c   |    2 ++
 3 files changed, 9 insertions(+)
include/fuse_common.h +4 -0
index 8e585cc7483643..19770262c4b518 100644
@@ -548,6 +548,8 @@ struct fuse_loop_config_v1 {
  * FUSE_IOMAP_SUPPORT_FILEIO: basic file I/O functionality through iomap
  */
 #define FUSE_IOMAP_SUPPORT_FILEIO	(1ULL << 0)
+/* untorn writes through iomap */
+#define FUSE_IOMAP_SUPPORT_ATOMIC	(1ULL << 1)
 
 /**
  * Connection information, passed to the ->init() method
@@ -1237,6 +1239,8 @@ static inline bool fuse_iomap_need_write_allocate(unsigned int opflags,
 #define FUSE_IFLAG_DAX			(1U << 0)
 /* use iomap for this inode */
 #define FUSE_IFLAG_IOMAP		(1U << 1)
+/* enable untorn writes */
+#define FUSE_IFLAG_ATOMIC		(1U << 2)
 
 /* Which fields are set in fuse_iomap_config_out? */
 #define FUSE_IOMAP_CONFIG_SID		(1 << 0ULL)
include/fuse_kernel.h +3 -0
index 1470b59d742165..fcf02c9371ba3a 100644
@@ -242,6 +242,7 @@
  *  - add FUSE_ATTR_IOMAP to enable iomap for specific inodes
  *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
  *  - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges
+ *  - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support
  */
 
 #ifndef _LINUX_FUSE_H
@@ -584,10 +585,12 @@ struct fuse_file_lock {
  * FUSE_ATTR_SUBMOUNT: Object is a submount root
  * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode
  * FUSE_ATTR_IOMAP: Use iomap for this inode
+ * FUSE_ATTR_ATOMIC: Enable untorn writes
  */
 #define FUSE_ATTR_SUBMOUNT      (1 << 0)
 #define FUSE_ATTR_DAX		(1 << 1)
 #define FUSE_ATTR_IOMAP		(1 << 2)
+#define FUSE_ATTR_ATOMIC	(1 << 3)
 
 /**
  * Open flags
lib/fuse_lowlevel.c +2 -0
index f730a7fd4ead09..ee73de4a8950be 100644
@@ -126,6 +126,8 @@ static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr,
 		attr->flags |= FUSE_ATTR_DAX;
 	if (iflags & FUSE_IFLAG_IOMAP)
 		attr->flags |= FUSE_ATTR_IOMAP;
+	if (iflags & FUSE_IFLAG_ATOMIC)
+		attr->flags |= FUSE_ATTR_ATOMIC;
 }
 
 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
#121 lore raw

RFC PATCH v4 3/4 fuse: cache iomap mappings for even better file IO performance

In reply to Darrick J. Wong

Hi all,

This series improves the performance (and correctness for some filesystems) by adding the ability to cache iomap mappings in the kernel. For filesystems that can change mapping states during pagecache writeback (e.g. unwritten extent conversion) this is absolutely necessary to deal with races with writes to the pagecache because writeback does not take i_rwsem. For everyone else, it simply eliminates roundtrips to userspace.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
---
Commits in this patchset:
* fuse: cache iomaps
* fuse: use the iomap cache for iomap_begin
* fuse: invalidate iomap cache after file updates
* fuse: enable iomap cache management

---
 fs/fuse/fuse_i.h          |   51 +
 fs/fuse/fuse_trace.h      |  434 ++++++++++++
 fs/fuse/iomap_priv.h      |  149 ++++
 include/uapi/linux/fuse.h |   33 +
 fs/fuse/Makefile          |    2 
 fs/fuse/dev.c             |   44 +
 fs/fuse/dir.c             |    6 
 fs/fuse/file.c            |   10 
 fs/fuse/file_iomap.c      |  527 ++++++++++++++
 fs/fuse/iomap_cache.c     | 1693 +++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 2934 insertions(+), 15 deletions(-)
 create mode 100644 fs/fuse/iomap_cache.c
#122 lore raw

PATCH 1/4 fuse: cache iomaps

From: Darrick J. Wong <[email protected]>

Cache iomaps to a file so that we don't have to upcall the server.

---
 fs/fuse/fuse_i.h          |   37 +
 fs/fuse/fuse_trace.h      |  295 ++++++++
 fs/fuse/iomap_priv.h      |  135 ++++
 include/uapi/linux/fuse.h |    5 
 fs/fuse/Makefile          |    2 
 fs/fuse/file_iomap.c      |   23 +
 fs/fuse/iomap_cache.c     | 1660 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 2150 insertions(+), 7 deletions(-)
 create mode 100644 fs/fuse/iomap_cache.c
fs/fuse/fuse_i.h +37 -0
index e72cc25c564048..54b8aab94a9cd5 100644
@@ -110,6 +110,24 @@ struct fuse_backing {
 	struct rcu_head rcu;
 };
 
+#if IS_ENABLED(CONFIG_FUSE_IOMAP)
+/*
+ * File incore extent information, present for each of data & attr forks.
+ */
+struct fuse_ifork {
+	int64_t			if_bytes;	/* bytes in if_data */
+	void			*if_data;	/* extent tree root */
+	int			if_height;	/* height of the extent tree */
+};
+
+struct fuse_iomap_cache {
+	struct fuse_ifork	im_read;
+	struct fuse_ifork	*im_write;
+	uint64_t		im_seq;		/* validity counter */
+	struct rw_semaphore	im_lock;	/* mapping lock */
+};
+#endif
+
 /** FUSE inode */
 struct fuse_inode {
 	/** Inode data */
@@ -175,6 +193,7 @@ struct fuse_inode {
 			spinlock_t ioend_lock;
 			struct work_struct ioend_work;
 			struct list_head ioend_list;
+			struct fuse_iomap_cache cache;
 #endif
 		};
 
@@ -245,6 +264,11 @@ enum {
 	FUSE_I_IOMAP,
 	/* Enable untorn writes */
 	FUSE_I_ATOMIC,
+	/*
+	 * Cache iomaps in the kernel.  This is required for any filesystem
+	 * that needs to synchronize pagecache write and writeback.
+	 */
+	FUSE_I_IOMAP_CACHE,
 };
 
 struct fuse_conn;
@@ -1755,6 +1779,18 @@ int fuse_dev_ioctl_iomap_support(struct file *file,
 				 struct fuse_iomap_support __user *argp);
 
 int fuse_iomap_fadvise(struct file *file, loff_t start, loff_t end, int advice);
+
+static inline bool fuse_inode_caches_iomaps(const struct inode *inode)
+{
+	const struct fuse_inode *fi = get_fuse_inode_c(inode);
+
+	return test_bit(FUSE_I_IOMAP_CACHE, &fi->state);
+}
+
+enum fuse_iomap_iodir {
+	READ_MAPPING,
+	WRITE_MAPPING,
+};
 #else
 # define fuse_iomap_enabled(...)		(false)
 # define fuse_has_iomap(...)			(false)
@@ -1783,6 +1819,7 @@ int fuse_iomap_fadvise(struct file *file, loff_t start, loff_t end, int advice);
 # define fuse_iomap_flush_unmap_range(...)	(-ENOSYS)
 # define fuse_dev_ioctl_iomap_support(...)	(-EOPNOTSUPP)
 # define fuse_iomap_fadvise			NULL
+# define fuse_inode_caches_iomaps(...)		(false)
 #endif
 
 #endif /* _FS_FUSE_I_H */
fs/fuse/fuse_trace.h +294 -1

331 lines not on this page · raw message

fs/fuse/iomap_priv.h +130 -5

148 lines not on this page · raw message

include/uapi/linux/fuse.h +5 -0
index 70b5530e587d48..df23eb65f0b497 100644
@@ -1344,6 +1344,8 @@ struct fuse_uring_cmd_req {
 
 /* fuse-specific mapping type indicating that writes use the read mapping */
 #define FUSE_IOMAP_TYPE_PURE_OVERWRITE	(255)
+/* fuse-specific mapping type saying the server has populated the cache */
+#define FUSE_IOMAP_TYPE_RETRY_CACHE	(254)
 
 #define FUSE_IOMAP_DEV_NULL		(0U)	/* null device cookie */
 
@@ -1481,4 +1483,7 @@ struct fuse_iomap_dev_inval_out {
 	uint64_t length;
 };
 
+/* invalidate all cached iomap mappings up to EOF */
+#define FUSE_IOMAP_INVAL_TO_EOF		(~0ULL)
+
 #endif /* _LINUX_FUSE_H */
fs/fuse/Makefile +1 -1
index 27be39317701d6..e3ed1da6cfb6e7 100644
@@ -18,6 +18,6 @@ fuse-$(CONFIG_FUSE_PASSTHROUGH) += passthrough.o
 fuse-$(CONFIG_FUSE_BACKING) += backing.o
 fuse-$(CONFIG_SYSCTL) += sysctl.o
 fuse-$(CONFIG_FUSE_IO_URING) += dev_uring.o
-fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o
+fuse-$(CONFIG_FUSE_IOMAP) += file_iomap.o iomap_cache.o
 
 virtiofs-y := virtio_fs.o
fs/fuse/file_iomap.c +23 -0
index 3141518cc6e67d..545798f0d915a1 100644
@@ -1190,6 +1190,21 @@ static inline void fuse_inode_clear_atomic(struct inode *inode)
 	clear_bit(FUSE_I_ATOMIC, &fi->state);
 }
 
+static inline void fuse_iomap_clear_cache(struct inode *inode)
+{
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
+	ASSERT(fuse_has_iomap(inode));
+
+	clear_bit(FUSE_I_IOMAP_CACHE, &fi->state);
+
+	fuse_iext_destroy(&fi->cache.im_read);
+	if (fi->cache.im_write) {
+		fuse_iext_destroy(fi->cache.im_write);
+		kfree(fi->cache.im_write);
+	}
+}
+
 void fuse_iomap_init_inode(struct inode *inode, unsigned attr_flags)
 {
 	struct fuse_conn *conn = get_fuse_conn(inode);
@@ -1207,6 +1222,8 @@ void fuse_iomap_evict_inode(struct inode *inode)
 {
 	trace_fuse_iomap_evict_inode(inode);
 
+	if (fuse_inode_caches_iomaps(inode))
+		fuse_iomap_clear_cache(inode);
 	if (fuse_inode_has_iomap(inode))
 		fuse_inode_clear_iomap(inode);
 	if (fuse_inode_has_atomic(inode))
@@ -1766,6 +1783,12 @@ static inline void fuse_inode_set_iomap(struct inode *inode)
 		min_order = inode->i_blkbits - PAGE_SHIFT;
 
 	mapping_set_folio_min_order(inode->i_mapping, min_order);
+
+	memset(&fi->cache.im_read, 0, sizeof(fi->cache.im_read));
+	fi->cache.im_seq = 0;
+	fi->cache.im_write = NULL;
+
+	init_rwsem(&fi->cache.im_lock);
 	set_bit(FUSE_I_IOMAP, &fi->state);
 }
 
fs/fuse/iomap_cache.c new file +1660 -0

1,660 lines not on this page · raw message

#123 lore raw

PATCH 2/4 fuse: use the iomap cache for iomap_begin

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Look inside the iomap cache to try to satisfy iomap_begin.

---
 fs/fuse/fuse_trace.h  |   34 ++++++++
 fs/fuse/iomap_priv.h  |    5 +
 fs/fuse/file_iomap.c  |  221 ++++++++++++++++++++++++++++++++++++++++++++++++-
 fs/fuse/iomap_cache.c |    6 +
 4 files changed, 260 insertions(+), 6 deletions(-)
fs/fuse/fuse_trace.h +34 -0
index eb604eaf3bafad..94e7a4222d2ac2 100644
@@ -319,6 +319,7 @@ struct fuse_iomap_lookup;
 
 #define FUSE_IOMAP_TYPE_STRINGS \
 	{ FUSE_IOMAP_TYPE_PURE_OVERWRITE,	"overwrite" }, \
+	{ FUSE_IOMAP_TYPE_RETRY_CACHE,		"retry" }, \
 	{ FUSE_IOMAP_TYPE_HOLE,			"hole" }, \
 	{ FUSE_IOMAP_TYPE_DELALLOC,		"delalloc" }, \
 	{ FUSE_IOMAP_TYPE_MAPPED,		"mapped" }, \
@@ -1411,6 +1412,39 @@ TRACE_EVENT(fuse_iomap_cache_lookup_result,
 		  FUSE_IOMAP_MAP_PRINTK_ARGS(got),
 		  __entry->validity_cookie)
 );
+
+TRACE_EVENT(fuse_iomap_invalid,
+	TP_PROTO(const struct inode *inode, const struct iomap *map,
+		 uint64_t validity_cookie),
+	TP_ARGS(inode, map, validity_cookie),
+
+	TP_STRUCT__entry(
+		FUSE_INODE_FIELDS
+		FUSE_IOMAP_MAP_FIELDS(map)
+		__field(uint64_t,		old_validity_cookie)
+		__field(uint64_t,		validity_cookie)
+	),
+
+	TP_fast_assign(
+		FUSE_INODE_ASSIGN(inode, fi, fm);
+
+		__entry->mapoffset	=	map->offset;
+		__entry->maplength	=	map->length;
+		__entry->maptype	=	map->type;
+		__entry->mapflags	=	map->flags;
+		__entry->mapaddr	=	map->addr;
+		__entry->mapdev		=	FUSE_IOMAP_DEV_NULL;
+
+		__entry->old_validity_cookie=	map->validity_cookie;
+		__entry->validity_cookie=	validity_cookie;
+	),
+
+	TP_printk(FUSE_INODE_FMT FUSE_IOMAP_MAP_FMT() " old_cookie 0x%llx new_cookie 0x%llx",
+		  FUSE_INODE_PRINTK_ARGS,
+		  FUSE_IOMAP_MAP_PRINTK_ARGS(map),
+		  __entry->old_validity_cookie,
+		  __entry->validity_cookie)
+);
 #endif /* CONFIG_FUSE_IOMAP */
 
 #endif /* _TRACE_FUSE_H */
fs/fuse/iomap_priv.h +5 -0
index 8e4a32879025a4..8f1aef381942b6 100644
@@ -145,6 +145,11 @@ static inline bool fuse_iext_peek_prev_extent(struct fuse_ifork *ifp,
 	     fuse_iext_get_extent((ifp), (ext), (got));	\
 	     fuse_iext_next((ifp), (ext)))
 
+/* iomaps that come direct from the fuse server are presumed to be valid */
+#define FUSE_IOMAP_ALWAYS_VALID	((uint64_t)0)
+/* set initial iomap cookie value to avoid ALWAYS_VALID */
+#define FUSE_IOMAP_INIT_COOKIE	((uint64_t)1)
+
 static inline uint64_t fuse_iext_read_seq(struct fuse_iomap_cache *ip)
 {
 	return (uint64_t)READ_ONCE(ip->im_seq);
fs/fuse/file_iomap.c +216 -5

296 lines not on this page · raw message

fs/fuse/iomap_cache.c +5 -1
index 5bfa0e26346d1f..572bccf99a97a8 100644
@@ -660,7 +660,11 @@ fuse_iext_realloc_root(
  */
 static inline void fuse_iext_inc_seq(struct fuse_iomap_cache *ip)
 {
-	WRITE_ONCE(ip->im_seq, READ_ONCE(ip->im_seq) + 1);
+	uint64_t new_val = READ_ONCE(ip->im_seq) + 1;
+
+	if (new_val == FUSE_IOMAP_ALWAYS_VALID)
+		new_val++;
+	WRITE_ONCE(ip->im_seq, new_val);
 }
 
 void
#124 lore raw

PATCH 3/4 fuse: invalidate iomap cache after file updates

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

The kernel doesn't know what the fuse server might have done in response to truncate, fallocate, or ioend events. Therefore, it must invalidate the mapping cache after those operations to ensure cache coherency.

---
 fs/fuse/fuse_i.h      |    7 +++++++
 fs/fuse/fuse_trace.h  |   37 +++++++++++++++++++++++++++++++++++++
 fs/fuse/iomap_priv.h  |    9 +++++++++
 fs/fuse/dir.c         |    6 ++++++
 fs/fuse/file.c        |   10 +++++++---
 fs/fuse/file_iomap.c  |   49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 fs/fuse/iomap_cache.c |   29 +++++++++++++++++++++++++++++
 7 files changed, 143 insertions(+), 4 deletions(-)
fs/fuse/fuse_i.h +7 -0
index 54b8aab94a9cd5..0a7192b633dd3a 100644
@@ -1769,11 +1769,15 @@ int fuse_iomap_mmap(struct file *file, struct vm_area_struct *vma);
 ssize_t fuse_iomap_buffered_read(struct kiocb *iocb, struct iov_iter *to);
 ssize_t fuse_iomap_buffered_write(struct kiocb *iocb, struct iov_iter *from);
 int fuse_iomap_setsize_start(struct inode *inode, loff_t newsize);
+int fuse_iomap_setsize_finish(struct inode *inode, loff_t newsize);
 void fuse_iomap_set_i_blkbits(struct inode *inode, u8 new_blkbits);
 int fuse_iomap_fallocate(struct file *file, int mode, loff_t offset,
 			 loff_t length, loff_t new_size);
 int fuse_iomap_flush_unmap_range(struct inode *inode, loff_t pos,
 				 loff_t endpos);
+void fuse_iomap_open_truncate(struct inode *inode);
+void fuse_iomap_copied_file_range(struct inode *inode, loff_t offset,
+				  size_t written);
 
 int fuse_dev_ioctl_iomap_support(struct file *file,
 				 struct fuse_iomap_support __user *argp);
@@ -1814,9 +1818,12 @@ enum fuse_iomap_iodir {
 # define fuse_iomap_buffered_read(...)		(-ENOSYS)
 # define fuse_iomap_buffered_write(...)		(-ENOSYS)
 # define fuse_iomap_setsize_start(...)		(-ENOSYS)
+# define fuse_iomap_setsize_finish(...)		(-ENOSYS)
 # define fuse_iomap_set_i_blkbits(...)		((void)0)
 # define fuse_iomap_fallocate(...)		(-ENOSYS)
 # define fuse_iomap_flush_unmap_range(...)	(-ENOSYS)
+# define fuse_iomap_open_truncate(...)		((void)0)
+# define fuse_iomap_copied_file_range(...)	((void)0)
 # define fuse_dev_ioctl_iomap_support(...)	(-EOPNOTSUPP)
 # define fuse_iomap_fadvise			NULL
 # define fuse_inode_caches_iomaps(...)		(false)
fs/fuse/fuse_trace.h +37 -0
index 94e7a4222d2ac2..cd8aa9e0633eee 100644
@@ -991,6 +991,7 @@ DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT(fuse_iomap_truncate_down);
 DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT(fuse_iomap_punch_range);
 DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT(fuse_iomap_setsize);
 DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT(fuse_iomap_flush_unmap_range);
+DEFINE_FUSE_IOMAP_FILE_RANGE_EVENT(fuse_iomap_cache_invalidate_range);
 
 TRACE_EVENT(fuse_iomap_set_i_blkbits,
 	TP_PROTO(const struct inode *inode, u8 new_blkbits),
@@ -1150,6 +1151,42 @@ DEFINE_FUSE_IOMAP_INLINE_EVENT(fuse_iomap_inline_write);
 DEFINE_FUSE_IOMAP_INLINE_EVENT(fuse_iomap_set_inline_iomap);
 DEFINE_FUSE_IOMAP_INLINE_EVENT(fuse_iomap_set_inline_srcmap);
 
+TRACE_EVENT(fuse_iomap_open_truncate,
+	TP_PROTO(const struct inode *inode),
+
+	TP_ARGS(inode),
+
+	TP_STRUCT__entry(
+		FUSE_INODE_FIELDS
+	),
+
+	TP_fast_assign(
+		FUSE_INODE_ASSIGN(inode, fi, fm);
+	),
+
+	TP_printk(FUSE_INODE_FMT,
+		  FUSE_INODE_PRINTK_ARGS)
+);
+
+TRACE_EVENT(fuse_iomap_copied_file_range,
+	TP_PROTO(const struct inode *inode, loff_t offset,
+		 size_t written),
+	TP_ARGS(inode, offset, written),
+
+	TP_STRUCT__entry(
+		FUSE_IO_RANGE_FIELDS()
+	),
+
+	TP_fast_assign(
+		FUSE_INODE_ASSIGN(inode, fi, fm);
+		__entry->offset		=	offset;
+		__entry->length		=	written;
+	),
+
+	TP_printk(FUSE_IO_RANGE_FMT(),
+		  FUSE_IO_RANGE_PRINTK_ARGS())
+);
+
 DECLARE_EVENT_CLASS(fuse_iext_class,
 	TP_PROTO(const struct inode *inode, const struct fuse_iext_cursor *cur,
 		 int state, unsigned long caller_ip),
fs/fuse/iomap_priv.h +9 -0
index 8f1aef381942b6..e78c49af638e0f 100644
@@ -177,6 +177,15 @@ fuse_iomap_cache_lookup(struct inode *inode, enum fuse_iomap_iodir iodir,
 			loff_t off, uint64_t len,
 			struct fuse_iomap_lookup *mval);
 
+int fuse_iomap_cache_invalidate_range(struct inode *inode, loff_t offset,
+				      uint64_t length);
+static inline int fuse_iomap_cache_invalidate(struct inode *inode,
+					      loff_t offset)
+{
+	return fuse_iomap_cache_invalidate_range(inode, offset,
+						 FUSE_IOMAP_INVAL_TO_EOF);
+}
+
 #endif /* CONFIG_FUSE_IOMAP */
 
 #endif /* _FS_FUSE_IOMAP_PRIV_H */
fs/fuse/dir.c +6 -0
index 305b926b4a589a..05cb79beb8e426 100644
@@ -2187,6 +2187,12 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		goto error;
 	}
 
+	if (fuse_inode_has_iomap(inode) && is_truncate) {
+		err = fuse_iomap_setsize_finish(inode, outarg.attr.size);
+		if (err)
+			goto error;
+	}
+
 	spin_lock(&fi->lock);
 	/* the kernel maintains i_mtime locally */
 	if (trust_local_cmtime) {
fs/fuse/file.c +7 -3
index 6575deae7e65f6..701042c04ab733 100644
@@ -279,9 +279,11 @@ static int fuse_open(struct inode *inode, struct file *file)
 	if (is_wb_truncate || dax_truncate)
 		fuse_release_nowrite(inode);
 	if (!err) {
-		if (is_truncate)
+		if (is_truncate) {
 			truncate_pagecache(inode, 0);
-		else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
+			if (fuse_inode_has_iomap(inode))
+				fuse_iomap_open_truncate(inode);
+		} else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
 			invalidate_inode_pages2(inode->i_mapping);
 	}
 	if (dax_truncate)
@@ -3131,7 +3133,9 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (err)
 		goto out;
 
-	if (!fuse_inode_has_iomap(inode_out))
+	if (fuse_inode_has_iomap(inode_out))
+		fuse_iomap_copied_file_range(inode_out, pos_out, outarg.size);
+	else
 		truncate_inode_pages_range(inode_out->i_mapping,
 				   ALIGN_DOWN(pos_out, PAGE_SIZE),
 				   ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
fs/fuse/file_iomap.c +48 -1

84 lines not on this page · raw message

fs/fuse/iomap_cache.c +29 -0
index 572bccf99a97a8..a13eb5eec72415 100644
@@ -1412,6 +1412,35 @@ fuse_iomap_cache_remove(
 	return ret;
 }
 
+int fuse_iomap_cache_invalidate_range(struct inode *inode, loff_t offset,
+				      uint64_t length)
+{
+	loff_t aligned_offset;
+	const unsigned int blocksize = i_blocksize(inode);
+	int ret, ret2;
+
+	if (!fuse_inode_caches_iomaps(inode))
+		return 0;
+
+	trace_fuse_iomap_cache_invalidate_range(inode, offset, length);
+
+	aligned_offset = round_down(offset, blocksize);
+	if (length != FUSE_IOMAP_INVAL_TO_EOF) {
+		length += offset - aligned_offset;
+		length = round_up(length, blocksize);
+	}
+
+	fuse_iomap_cache_lock(inode);
+	ret = fuse_iomap_cache_remove(inode, READ_MAPPING,
+				      aligned_offset, length);
+	ret2 = fuse_iomap_cache_remove(inode, WRITE_MAPPING,
+				       aligned_offset, length);
+	fuse_iomap_cache_unlock(inode);
+	if (ret)
+		return ret;
+	return ret2;
+}
+
 static void
 fuse_iext_add_mapping(
 	struct fuse_iomap_cache		*ip,
#125 lore raw

PATCH 4/4 fuse: enable iomap cache management

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Provide a means for the fuse server to upload iomappings to the kernel and invalidate them. This is how we enable iomap caching for better performance. This is also required for correct synchronization between pagecache writes and writeback.

---
 fs/fuse/fuse_i.h          |    7 +
 fs/fuse/fuse_trace.h      |   68 +++++++++++++
 include/uapi/linux/fuse.h |   28 +++++
 fs/fuse/dev.c             |   44 ++++++++
 fs/fuse/file_iomap.c      |  244 ++++++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 387 insertions(+), 4 deletions(-)
fs/fuse/fuse_i.h +7 -0
index 0a7192b633dd3a..a710c56b205e30 100644
@@ -1795,6 +1795,11 @@ enum fuse_iomap_iodir {
 	READ_MAPPING,
 	WRITE_MAPPING,
 };
+
+int fuse_iomap_upsert(struct fuse_conn *fc,
+		      const struct fuse_iomap_upsert_out *outarg);
+int fuse_iomap_inval(struct fuse_conn *fc,
+		     const struct fuse_iomap_inval_out *outarg);
 #else
 # define fuse_iomap_enabled(...)		(false)
 # define fuse_has_iomap(...)			(false)
@@ -1827,6 +1832,8 @@ enum fuse_iomap_iodir {
 # define fuse_dev_ioctl_iomap_support(...)	(-EOPNOTSUPP)
 # define fuse_iomap_fadvise			NULL
 # define fuse_inode_caches_iomaps(...)		(false)
+# define fuse_iomap_upsert(...)			(-ENOSYS)
+# define fuse_iomap_inval(...)			(-ENOSYS)
 #endif
 
 #endif /* _FS_FUSE_I_H */
fs/fuse/fuse_trace.h +68 -0

86 lines not on this page · raw message

include/uapi/linux/fuse.h +28 -0
index df23eb65f0b497..41f07513bbdd9d 100644
@@ -243,6 +243,8 @@
  *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
  *  - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges
  *  - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support
+ *  - add FUSE_NOTIFY_IOMAP_UPSERT and FUSE_NOTIFY_IOMAP_INVAL so fuse servers
+ *    can cache iomappings in the kernel
  */
 
 #ifndef _LINUX_FUSE_H
@@ -709,6 +711,8 @@ enum fuse_notify_code {
 	FUSE_NOTIFY_RESEND = 7,
 	FUSE_NOTIFY_INC_EPOCH = 8,
 	FUSE_NOTIFY_IOMAP_DEV_INVAL = 9,
+	FUSE_NOTIFY_IOMAP_UPSERT = 10,
+	FUSE_NOTIFY_IOMAP_INVAL = 11,
 	FUSE_NOTIFY_CODE_MAX,
 };
 
@@ -1346,6 +1350,8 @@ struct fuse_uring_cmd_req {
 #define FUSE_IOMAP_TYPE_PURE_OVERWRITE	(255)
 /* fuse-specific mapping type saying the server has populated the cache */
 #define FUSE_IOMAP_TYPE_RETRY_CACHE	(254)
+/* do not upsert this mapping */
+#define FUSE_IOMAP_TYPE_NOCACHE		(253)
 
 #define FUSE_IOMAP_DEV_NULL		(0U)	/* null device cookie */
 
@@ -1486,4 +1492,26 @@ struct fuse_iomap_dev_inval_out {
 /* invalidate all cached iomap mappings up to EOF */
 #define FUSE_IOMAP_INVAL_TO_EOF		(~0ULL)
 
+struct fuse_iomap_inval_out {
+	uint64_t nodeid;	/* Inode ID */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+
+	uint64_t read_offset;	/* range to invalidate read iomaps, bytes */
+	uint64_t read_length;	/* can be FUSE_IOMAP_INVAL_TO_EOF */
+
+	uint64_t write_offset;	/* range to invalidate write iomaps, bytes */
+	uint64_t write_length;	/* can be FUSE_IOMAP_INVAL_TO_EOF */
+};
+
+struct fuse_iomap_upsert_out {
+	uint64_t nodeid;	/* Inode ID */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+
+	/* read file data from here */
+	struct fuse_iomap_io	read;
+
+	/* write file data to here, if applicable */
+	struct fuse_iomap_io	write;
+};
+
 #endif /* _LINUX_FUSE_H */
fs/fuse/dev.c +44 -0

56 lines not on this page · raw message

fs/fuse/file_iomap.c +240 -4

282 lines not on this page · raw message

#126 lore raw

RFC PATCH v4 3/4 libfuse: cache iomap mappings for even better file IO performance

In reply to Darrick J. Wong

Hi all,

This series improves the performance (and correctness for some filesystems) by adding the ability to cache iomap mappings in the kernel. For filesystems that can change mapping states during pagecache writeback (e.g. unwritten extent conversion) this is absolutely necessary to deal with races with writes to the pagecache because writeback does not take i_rwsem. For everyone else, it simply eliminates roundtrips to userspace.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-cache
---
Commits in this patchset:
* libfuse: enable iomap cache management for lowlevel fuse
* libfuse: add upper-level iomap cache management

---
 include/fuse.h          |   31 ++++++++++++++++++++
 include/fuse_common.h   |   12 ++++++++
 include/fuse_kernel.h   |   26 +++++++++++++++++
 include/fuse_lowlevel.h |   41 ++++++++++++++++++++++++++
 lib/fuse.c              |   30 +++++++++++++++++++
 lib/fuse_lowlevel.c     |   73 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    4 +++
 7 files changed, 217 insertions(+)
#127 lore raw

PATCH 1/2 libfuse: enable iomap cache management for lowlevel fuse

From: Darrick J. Wong <[email protected]>

Add the library methods so that fuse servers can manage an in-kernel iomap cache. This enables better performance on small IOs and is required if the filesystem needs synchronization between pagecache writes and writeback.

---
 include/fuse_common.h   |   12 ++++++++
 include/fuse_kernel.h   |   26 +++++++++++++++++
 include/fuse_lowlevel.h |   41 ++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   73 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/fuse_versionscript  |    2 +
 5 files changed, 154 insertions(+)
include/fuse_common.h +12 -0
index 19770262c4b518..a1c5199f2cb4ee 100644
@@ -1168,6 +1168,10 @@ int fuse_convert_to_conn_want_ext(struct fuse_conn_info *conn);
 
 /* fuse-specific mapping type indicating that writes use the read mapping */
 #define FUSE_IOMAP_TYPE_PURE_OVERWRITE	(255)
+/* fuse-specific mapping type saying the server has populated the cache */
+#define FUSE_IOMAP_TYPE_RETRY_CACHE	(254)
+/* do not upsert this mapping */
+#define FUSE_IOMAP_TYPE_NOCACHE		(253)
 
 #define FUSE_IOMAP_DEV_NULL		(0U)	/* null device cookie */
 
@@ -1273,6 +1277,14 @@ struct fuse_iomap_config{
 	int64_t s_maxbytes;	/* max file size */
 };
 
+/* invalidate to end of file */
+#define FUSE_IOMAP_INVAL_TO_EOF		(~0ULL)
+
+struct fuse_iomap_inval {
+	uint64_t offset;	/* file offset to invalidate, bytes */
+	uint64_t length;	/* length to invalidate, bytes */
+};
+
 /* ----------------------------------------------------------- *
  * Compatibility stuff					       *
  * ----------------------------------------------------------- */
include/fuse_kernel.h +26 -0
index fcf02c9371ba3a..0c30aebaf95c32 100644
@@ -243,6 +243,8 @@
  *  - add FUSE_IOMAP_CONFIG so the fuse server can configure more fs geometry
  *  - add FUSE_NOTIFY_IOMAP_DEV_INVAL to invalidate iomap bdev ranges
  *  - add FUSE_ATTR_ATOMIC for single-fsblock atomic write support
+ *  - add FUSE_NOTIFY_IOMAP_UPSERT and FUSE_NOTIFY_IOMAP_INVAL so fuse servers
+ *    can cache iomappings in the kernel
  */
 
 #ifndef _LINUX_FUSE_H
@@ -696,6 +698,8 @@ enum fuse_notify_code {
 	FUSE_NOTIFY_RESEND = 7,
 	FUSE_NOTIFY_INC_EPOCH = 8,
 	FUSE_NOTIFY_IOMAP_DEV_INVAL = 9,
+	FUSE_NOTIFY_IOMAP_UPSERT = 10,
+	FUSE_NOTIFY_IOMAP_INVAL = 11,
 	FUSE_NOTIFY_CODE_MAX,
 };
 
@@ -1401,4 +1405,26 @@ struct fuse_iomap_dev_inval {
 	uint64_t offset;	/* range to invalidate pagecache, bytes */
 	uint64_t length;
 };
+
+struct fuse_iomap_inval_out {
+	uint64_t nodeid;	/* Inode ID */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+
+	uint64_t read_offset;	/* range to invalidate read iomaps, bytes */
+	uint64_t read_length;	/* can be FUSE_IOMAP_INVAL_TO_EOF */
+
+	uint64_t write_offset;	/* range to invalidate write iomaps, bytes */
+	uint64_t write_length;	/* can be FUSE_IOMAP_INVAL_TO_EOF */
+};
+
+struct fuse_iomap_upsert_out {
+	uint64_t nodeid;	/* Inode ID */
+	uint64_t attr_ino;	/* matches fuse_attr:ino */
+
+	/* read file data from here */
+	struct fuse_iomap_io	read;
+
+	/* write file data to here, if applicable */
+	struct fuse_iomap_io	write;
+};
 #endif /* _LINUX_FUSE_H */
include/fuse_lowlevel.h +41 -0

47 lines not on this page · raw message

lib/fuse_lowlevel.c +73 -0

79 lines not on this page · raw message

lib/fuse_versionscript +2 -0
index 102f449d28a0be..a83966b9e48018 100644
@@ -235,6 +235,8 @@ FUSE_3.99 {
 		fuse_reply_iomap_config;
 		fuse_lowlevel_iomap_device_invalidate;
 		fuse_fs_iomap_device_invalidate;
+		fuse_lowlevel_notify_iomap_upsert;
+		fuse_lowlevel_notify_iomap_inval;
 } FUSE_3.18;
 
 # Local Variables:
#128 lore raw

PATCH 2/2 libfuse: add upper-level iomap cache management

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make it so that upper-level fuse servers can use the iomap cache too.

---
 include/fuse.h         |   31 +++++++++++++++++++++++++++++++
 lib/fuse.c             |   30 ++++++++++++++++++++++++++++++
 lib/fuse_versionscript |    2 ++
 3 files changed, 63 insertions(+)
include/fuse.h +31 -0
index e53e92786cea08..f8a57154017a2a 100644
@@ -1450,6 +1450,37 @@ bool fuse_fs_can_enable_iomap(const struct stat *statbuf);
  */
 bool fuse_fs_can_enable_iomapx(const struct statx *statxbuf);
 
+/*
+ * Upsert some file mapping information into the kernel.  This is necessary
+ * for filesystems that require coordination of mapping state changes between
+ * buffered writes and writeback, and desirable for better performance
+ * elsewhere.
+ *
+ * @param nodeid the inode number
+ * @param attr_ino inode number as told by fuse_attr::ino
+ * @param read mapping information for file reads
+ * @param write mapping information for file writes
+ * @return zero for success, -errno for failure
+ */
+int fuse_fs_iomap_upsert(uint64_t nodeid, uint64_t attr_ino,
+			 const struct fuse_file_iomap *read,
+			 const struct fuse_file_iomap *write);
+
+/**
+ * Invalidate some file mapping information in the kernel.
+ *
+ * @param nodeid the inode number
+ * @param attr_ino inode number as told by fuse_attr::ino
+ * @param read_off start of the range of read mappings to invalidate
+ * @param read_len length of the range of read mappings to invalidate
+ * @param write_off start of the range of write mappings to invalidate
+ * @param write_len length of the range of write mappings to invalidate
+ * @return zero for success, -errno for failure
+ */
+int fuse_fs_iomap_inval(uint64_t nodeid, uint64_t attr_ino, loff_t read_off,
+			uint64_t read_len, loff_t write_off,
+			uint64_t write_len);
+
 int fuse_notify_poll(struct fuse_pollhandle *ph);
 
 /**
lib/fuse.c +30 -0
index 1c813ec5a697a0..7b28f848116abb 100644
@@ -2963,6 +2963,36 @@ static int fuse_fs_iomap_config(struct fuse_fs *fs, uint64_t flags,
 	return fs->op.iomap_config(flags, maxbytes, cfg);
 }
 
+int fuse_fs_iomap_upsert(uint64_t nodeid, uint64_t attr_ino,
+			 const struct fuse_file_iomap *read,
+			 const struct fuse_file_iomap *write)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+
+	return fuse_lowlevel_notify_iomap_upsert(se, nodeid, attr_ino,
+						 read, write);
+}
+
+int fuse_fs_iomap_inval(uint64_t nodeid, uint64_t attr_ino, loff_t read_off,
+			uint64_t read_len, loff_t write_off,
+			uint64_t write_len)
+{
+	struct fuse_context *ctxt = fuse_get_context();
+	struct fuse_session *se = fuse_get_session(ctxt->fuse);
+	struct fuse_iomap_inval read = {
+		.offset = read_off,
+		.length = read_len,
+	};
+	struct fuse_iomap_inval write = {
+		.offset = write_off,
+		.length = write_len,
+	};
+
+	return fuse_lowlevel_notify_iomap_inval(se, nodeid, attr_ino, &read,
+						&write);
+}
+
 static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
 			     int valid, struct fuse_file_info *fi)
 {
lib/fuse_versionscript +2 -0
index a83966b9e48018..9a4baed32bc477 100644
@@ -237,6 +237,8 @@ FUSE_3.99 {
 		fuse_fs_iomap_device_invalidate;
 		fuse_lowlevel_notify_iomap_upsert;
 		fuse_lowlevel_notify_iomap_inval;
+		fuse_fs_iomap_upsert;
+		fuse_fs_iomap_inval;
 } FUSE_3.18;
 
 # Local Variables:
#129 lore raw

RFC PATCH v4 4/4 fuse: handle timestamps and ACLs correctly when iomap is enabled

In reply to Darrick J. Wong

Hi all,

When iomap is enabled for a fuse file, we try to keep as much of the file IO path in the kernel as we possibly can. That means no calling out to the fuse server in the IO path when we can avoid it. However, the existing FUSE architecture defers all file attributes to the fuse server -- [cm]time updates, ACL metadata management, set[ug]id removal, and permissions checking thereof, etc.

We'd really rather do all these attribute updates in the kernel, and only push them to the fuse server when it's actually necessary (e.g. fsync). Furthermore, the POSIX ACL code has the weird behavior that if the access ACL can be represented entirely by i_mode bits, it will change the mode and delete the ACL, which fuse servers generally don't seem to implement.

IOWs, we want consistent and correct (as defined by fstests) behavior of file attributes in iomap mode. Let's make the kernel manage all that and push the results to userspace as needed. This improves performance even further, since it's sort of like writeback_cache mode but more aggressive.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

This has been running on the djcloud for months with no problems. Enjoy! Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-attrs
---
Commits in this patchset:
* fuse: force a ctime update after a fileattr_set call when in iomap mode
* fuse: synchronize inode->i_flags after fileattr_[gs]et
* fuse: cache atime when in iomap mode
* fuse: let the kernel handle KILL_SUID/KILL_SGID for iomap filesystems
* fuse: update ctime when updating acls on an iomap inode
* fuse: always cache ACLs when using iomap

---
 fs/fuse/fuse_i.h     |    1 
 fs/fuse/fuse_trace.h |   81 ++++++++++++++++++++++++++++++++++++++++
 fs/fuse/acl.c        |   24 ++++++++++--
 fs/fuse/dir.c        |   32 +++++++++++++---
 fs/fuse/inode.c      |   20 ++++++++--
 fs/fuse/ioctl.c      |  101 ++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/readdir.c    |    3 +
 7 files changed, 249 insertions(+), 13 deletions(-)
#130 lore raw

PATCH 1/6 fuse: force a ctime update after a fileattr_set call when in iomap mode

From: Darrick J. Wong <[email protected]>

In iomap mode, the kernel is in charge of driving ctime updates to the fuse server and ignores updates coming from the fuse server. Therefore, when someone calls fileattr_set to change file attributes, we must force a ctime update.

Found by generic/277.

---
 fs/fuse/ioctl.c |   11 +++++++++++
 1 file changed, 11 insertions(+)
fs/fuse/ioctl.c +11 -0
index 57032eadca6c27..f5f7d806262cdf 100644
@@ -548,8 +548,13 @@ int fuse_fileattr_set(struct mnt_idmap *idmap,
 	struct fuse_file *ff;
 	unsigned int flags = fa->flags;
 	struct fsxattr xfa;
+	struct file_kattr old_ma = { };
+	bool is_wb = (fuse_get_cache_mask(inode) & STATX_CTIME);
 	int err;
 
+	if (is_wb)
+		vfs_fileattr_get(dentry, &old_ma);
+
 	ff = fuse_priv_ioctl_prepare(inode);
 	if (IS_ERR(ff))
 		return PTR_ERR(ff);
@@ -573,6 +578,12 @@ int fuse_fileattr_set(struct mnt_idmap *idmap,
 
 cleanup:
 	fuse_priv_ioctl_cleanup(inode, ff);
+	/*
+	 * If we cache ctime updates and the fileattr changed, then force a
+	 * ctime update.
+	 */
+	if (is_wb && memcmp(&old_ma, fa, sizeof(old_ma)))
+		fuse_update_ctime(inode);
 
 	if (err == -ENOTTY)
 		err = -EOPNOTSUPP;
#131 lore raw

PATCH 2/6 fuse: synchronize inode->i_flags after fileattr_[gs]et

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

There are three inode flags (immutable, append, sync) that are enforced by the VFS. Whenever we go around setting iflags, let's update the VFS state so that they actually work.

---
 fs/fuse/fuse_i.h     |    1 +
 fs/fuse/fuse_trace.h |   23 +++++++++++++
 fs/fuse/dir.c        |    1 +
 fs/fuse/inode.c      |    1 +
 fs/fuse/ioctl.c      |   90 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 116 insertions(+)
fs/fuse/fuse_i.h +1 -0
index a710c56b205e30..f7a7d8ad641d5b 100644
@@ -1588,6 +1588,7 @@ long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
 int fuse_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
 int fuse_fileattr_set(struct mnt_idmap *idmap,
 		      struct dentry *dentry, struct file_kattr *fa);
+void fuse_fileattr_init(struct inode *inode, const struct fuse_attr *attr);
 
 /* iomode.c */
 int fuse_file_cached_io_open(struct inode *inode, struct fuse_file *ff);
fs/fuse/fuse_trace.h +23 -0
index 80af541a54c5bd..aea9ea0835d497 100644
@@ -176,6 +176,29 @@ TRACE_EVENT(fuse_request_end,
 		  __entry->unique, __entry->len, __entry->error)
 );
 
+TRACE_EVENT(fuse_fileattr_update_inode,
+	TP_PROTO(const struct inode *inode, unsigned int old_iflags),
+
+	TP_ARGS(inode, old_iflags),
+
+	TP_STRUCT__entry(
+		FUSE_INODE_FIELDS
+		__field(unsigned int,		old_iflags)
+		__field(unsigned int,		new_iflags)
+	),
+
+	TP_fast_assign(
+		FUSE_INODE_ASSIGN(inode, fi, fm);
+		__entry->old_iflags	=	old_iflags;
+		__entry->new_iflags	=	inode->i_flags;
+	),
+
+	TP_printk(FUSE_INODE_FMT " old_iflags 0x%x iflags 0x%x",
+		  FUSE_INODE_PRINTK_ARGS,
+		  __entry->old_iflags,
+		  __entry->new_iflags)
+);
+
 #ifdef CONFIG_FUSE_BACKING
 #define FUSE_BACKING_PASSTHROUGH	(1U << 0)
 #define FUSE_BACKING_IOMAP		(1U << 1)
fs/fuse/dir.c +1 -0
index 05cb79beb8e426..d2f9bcccd776f0 100644
@@ -1254,6 +1254,7 @@ static void fuse_fillattr(struct mnt_idmap *idmap, struct inode *inode,
 		blkbits = inode->i_sb->s_blocksize_bits;
 
 	stat->blksize = 1 << blkbits;
+	generic_fill_statx_attr(inode, stat);
 }
 
 static void fuse_statx_to_attr(struct fuse_statx *sx, struct fuse_attr *attr)
fs/fuse/inode.c +1 -0
index 18dc9492d19174..b1793df3cbbd1a 100644
@@ -524,6 +524,7 @@ struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
 			inode->i_flags |= S_NOCMTIME;
 		inode->i_generation = generation;
 		fuse_init_inode(inode, attr, fc);
+		fuse_fileattr_init(inode, attr);
 		unlock_new_inode(inode);
 	} else if (fuse_stale_inode(inode, generation, attr)) {
 		/* nodeid was reused, any I/O on the old inode should fail */
fs/fuse/ioctl.c +90 -0

109 lines not on this page · raw message

#132 lore raw

PATCH 3/6 fuse: cache atime when in iomap mode

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

When we're running in iomap mode, allow the kernel to cache the access timestamp to further reduce the number of roundtrips to the fuse server.

---
 fs/fuse/dir.c   |    5 +++++
 fs/fuse/inode.c |   19 ++++++++++++++++---
 2 files changed, 21 insertions(+), 3 deletions(-)
fs/fuse/dir.c +5 -0
index d2f9bcccd776f0..a3ea50b99054ff 100644
@@ -2030,6 +2030,11 @@ int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
 		inarg.ctime = inode_get_ctime_sec(inode);
 		inarg.ctimensec = inode_get_ctime_nsec(inode);
 	}
+	if (fuse_has_iomap(inode)) {
+		inarg.valid |= FATTR_ATIME;
+		inarg.atime = inode_get_atime_sec(inode);
+		inarg.atimensec = inode_get_atime_nsec(inode);
+	}
 	if (ff) {
 		inarg.valid |= FATTR_FH;
 		inarg.fh = ff->fh;
fs/fuse/inode.c +16 -3
index b1793df3cbbd1a..91143845c615c4 100644
@@ -264,7 +264,8 @@ void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
 	attr->mtimensec = min_t(u32, attr->mtimensec, NSEC_PER_SEC - 1);
 	attr->ctimensec = min_t(u32, attr->ctimensec, NSEC_PER_SEC - 1);
 
-	inode_set_atime(inode, attr->atime, attr->atimensec);
+	if (!(cache_mask & STATX_ATIME))
+		inode_set_atime(inode, attr->atime, attr->atimensec);
 	/* mtime from server may be stale due to local buffered write */
 	if (!(cache_mask & STATX_MTIME)) {
 		inode_set_mtime(inode, attr->mtime, attr->mtimensec);
@@ -331,8 +332,12 @@ u32 fuse_get_cache_mask(struct inode *inode)
 {
 	struct fuse_conn *fc = get_fuse_conn(inode);
 
-	if (S_ISREG(inode->i_mode) &&
-	    (fuse_inode_has_iomap(inode) || fc->writeback_cache))
+	if (!S_ISREG(inode->i_mode))
+		return 0;
+
+	if (fuse_inode_has_iomap(inode))
+		return STATX_MTIME | STATX_CTIME | STATX_ATIME | STATX_SIZE;
+	if (fc->writeback_cache)
 		return STATX_MTIME | STATX_CTIME | STATX_SIZE;
 
 	return 0;
@@ -451,6 +456,14 @@ static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr,
 				   new_decode_dev(attr->rdev));
 	} else
 		BUG();
+
+	/*
+	 * iomap caches atime too, so we must load it from the fuse server
+	 * at instantiation time.
+	 */
+	if (fuse_has_iomap(inode))
+		inode_set_atime(inode, attr->atime, attr->atimensec);
+
 	/*
 	 * Ensure that we don't cache acls for daemons without FUSE_POSIX_ACL
 	 * so they see the exact same behavior as before.
#133 lore raw

PATCH 4/6 fuse: let the kernel handle KILL_SUID/KILL_SGID for iomap filesystems

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Let the kernel handle killing the suid/sgid bits because the write/falloc/truncate/chown code already does this, and we don't have to worry about external modifications that are only visible to the fuse server (i.e. we're not a cluster fs).

---
 fs/fuse/fuse_trace.h |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dir.c        |   15 ++++++++++---
 2 files changed, 70 insertions(+), 3 deletions(-)
fs/fuse/fuse_trace.h +58 -0
index aea9ea0835d497..18606eb0bf8dd7 100644
@@ -199,6 +199,64 @@ TRACE_EVENT(fuse_fileattr_update_inode,
 		  __entry->new_iflags)
 );
 
+TRACE_EVENT(fuse_setattr_fill,
+	TP_PROTO(const struct inode *inode,
+		 const struct fuse_setattr_in *inarg),
+	TP_ARGS(inode, inarg),
+
+	TP_STRUCT__entry(
+		FUSE_INODE_FIELDS
+		__field(umode_t,		mode)
+		__field(uint32_t,		valid)
+		__field(umode_t,		new_mode)
+		__field(uint64_t,		new_size)
+	),
+
+	TP_fast_assign(
+		FUSE_INODE_ASSIGN(inode, fi, fm);
+		__entry->mode		=	inode->i_mode;
+		__entry->valid		=	inarg->valid;
+		__entry->new_mode	=	inarg->mode;
+		__entry->new_size	=	inarg->size;
+	),
+
+	TP_printk(FUSE_INODE_FMT " mode 0%o valid 0x%x new_mode 0%o new_size 0x%llx",
+		  FUSE_INODE_PRINTK_ARGS,
+		  __entry->mode,
+		  __entry->valid,
+		  __entry->new_mode,
+		  __entry->new_size)
+);
+
+TRACE_EVENT(fuse_setattr,
+	TP_PROTO(const struct inode *inode,
+		 const struct iattr *inarg),
+	TP_ARGS(inode, inarg),
+
+	TP_STRUCT__entry(
+		FUSE_INODE_FIELDS
+		__field(umode_t,		mode)
+		__field(uint32_t,		valid)
+		__field(umode_t,		new_mode)
+		__field(uint64_t,		new_size)
+	),
+
+	TP_fast_assign(
+		FUSE_INODE_ASSIGN(inode, fi, fm);
+		__entry->mode		=	inode->i_mode;
+		__entry->valid		=	inarg->ia_valid;
+		__entry->new_mode	=	inarg->ia_mode;
+		__entry->new_size	=	inarg->ia_size;
+	),
+
+	TP_printk(FUSE_INODE_FMT " mode 0%o valid 0x%x new_mode 0%o new_size 0x%llx",
+		  FUSE_INODE_PRINTK_ARGS,
+		  __entry->mode,
+		  __entry->valid,
+		  __entry->new_mode,
+		  __entry->new_size)
+);
+
 #ifdef CONFIG_FUSE_BACKING
 #define FUSE_BACKING_PASSTHROUGH	(1U << 0)
 #define FUSE_BACKING_IOMAP		(1U << 1)
fs/fuse/dir.c +12 -3
index a3ea50b99054ff..e8eef46d8e1b52 100644
@@ -7,6 +7,7 @@
 */
 
 #include "fuse_i.h"
+#include "fuse_trace.h"
 
 #include <linux/pagemap.h>
 #include <linux/file.h>
@@ -1999,6 +2000,8 @@ static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
 			      struct fuse_setattr_in *inarg_p,
 			      struct fuse_attr_out *outarg_p)
 {
+	trace_fuse_setattr_fill(inode, inarg_p);
+
 	args->opcode = FUSE_SETATTR;
 	args->nodeid = get_node_id(inode);
 	args->in_numargs = 1;
@@ -2273,15 +2276,21 @@ static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,
 	if (!fuse_allow_current_process(get_fuse_conn(inode)))
 		return -EACCES;
 
-	if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
+	trace_fuse_setattr(inode, attr);
+
+	if (!fuse_has_iomap(inode) &&
+	    (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))) {
 		attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
 				    ATTR_MODE);
 
 		/*
 		 * The only sane way to reliably kill suid/sgid is to do it in
-		 * the userspace filesystem
+		 * the userspace filesystem if this isn't an iomap file.  For
+		 * iomap filesystems we let the kernel kill the setuid/setgid
+		 * bits.
 		 *
-		 * This should be done on write(), truncate() and chown().
+		 * This should be done on write(), truncate(), chown(), and
+		 * fallocate().
 		 */
 		if (!fc->handle_killpriv && !fc->handle_killpriv_v2) {
 			/*
#134 lore raw

PATCH 5/6 fuse: update ctime when updating acls on an iomap inode

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In iomap mode, the fuse kernel driver is in charge of updating file attributes, so we need to update ctime after an ACL change.

---
 fs/fuse/acl.c |   16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
fs/fuse/acl.c +15 -1
index 4f37390e3f3ce7..efab333415131c 100644
@@ -169,10 +169,24 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 			ret = 0;
 	}
 
-	/* If we scheduled a mode update above, push that to userspace now. */
 	if (!ret) {
 		struct iattr attr = { };
 
+		/*
+		 * When we're running in iomap mode, we need to update mode and
+		 * ctime ourselves instead of letting the fuse server figure
+		 * that out.
+		 */
+		if (fuse_has_iomap(inode)) {
+			attr.ia_valid |= ATTR_CTIME;
+			inode_set_ctime_current(inode);
+			attr.ia_ctime = inode_get_ctime(inode);
+		}
+
+		/*
+		 * If we scheduled a mode update above, push that to userspace
+		 * now.
+		 */
 		if (mode != inode->i_mode) {
 			attr.ia_valid |= ATTR_MODE;
 			attr.ia_mode = mode;
#135 lore raw

PATCH 6/6 fuse: always cache ACLs when using iomap

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Keep ACLs cached in memory when we're using iomap, so that we don't have to make a round trip to the fuse server. This might want to become a FUSE_ATTR_ flag.

---
 fs/fuse/acl.c     |    8 ++++++--
 fs/fuse/dir.c     |   11 ++++++++---
 fs/fuse/readdir.c |    3 ++-
 3 files changed, 16 insertions(+), 6 deletions(-)
fs/fuse/acl.c +6 -2
index efab333415131c..404c96ad68ea66 100644
@@ -201,8 +201,12 @@ int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 		 * Fuse daemons without FUSE_POSIX_ACL never cached POSIX ACLs
 		 * and didn't invalidate attributes. Retain that behavior.
 		 */
-		forget_all_cached_acls(inode);
-		fuse_invalidate_attr(inode);
+		if (!ret && fuse_has_iomap(inode)) {
+			set_cached_acl(inode, type, acl);
+		} else {
+			forget_all_cached_acls(inode);
+			fuse_invalidate_attr(inode);
+		}
 	}
 
 	return ret;
fs/fuse/dir.c +8 -3
index e8eef46d8e1b52..d317b7965a8259 100644
@@ -261,7 +261,8 @@ static int fuse_dentry_revalidate(struct inode *dir, const struct qstr *name,
 		    fuse_stale_inode(inode, outarg.generation, &outarg.attr))
 			goto invalid;
 
-		forget_all_cached_acls(inode);
+		if (!fuse_inode_has_iomap(inode))
+			forget_all_cached_acls(inode);
 		fuse_change_attributes(inode, &outarg.attr, NULL,
 				       ATTR_TIMEOUT(&outarg),
 				       attr_version);
@@ -1468,7 +1469,8 @@ static int fuse_update_get_attr(struct mnt_idmap *idmap, struct inode *inode,
 		sync = time_before64(fi->i_time, get_jiffies_64());
 
 	if (sync) {
-		forget_all_cached_acls(inode);
+		if (!fuse_inode_has_iomap(inode))
+			forget_all_cached_acls(inode);
 		/* Try statx if a field not covered by regular stat is wanted */
 		if (!fc->no_statx && (request_mask & ~STATX_BASIC_STATS)) {
 			err = fuse_do_statx(idmap, inode, file, stat);
@@ -1645,6 +1647,9 @@ static int fuse_access(struct inode *inode, int mask)
 
 static int fuse_perm_getattr(struct inode *inode, int mask)
 {
+	if (fuse_inode_has_iomap(inode))
+		return 0;
+
 	if (mask & MAY_NOT_BLOCK)
 		return -ECHILD;
 
@@ -2321,7 +2326,7 @@ static int fuse_setattr(struct mnt_idmap *idmap, struct dentry *entry,
 		 * If filesystem supports acls it may have updated acl xattrs in
 		 * the filesystem, so forget cached acls for the inode.
 		 */
-		if (fc->posix_acl)
+		if (fc->posix_acl && !fuse_inode_has_iomap(inode))
 			forget_all_cached_acls(inode);
 
 		/* Directory mode changed, may need to revalidate access */
fs/fuse/readdir.c +2 -1
index 45dd932eb03a5e..f7c2a45f23678e 100644
@@ -224,7 +224,8 @@ static int fuse_direntplus_link(struct file *file,
 		fi->nlookup++;
 		spin_unlock(&fi->lock);
 
-		forget_all_cached_acls(inode);
+		if (!fuse_inode_has_iomap(inode))
+			forget_all_cached_acls(inode);
 		fuse_change_attributes(inode, &o->attr, NULL,
 				       ATTR_TIMEOUT(o),
 				       attr_version);
#136 lore raw

RFC PATCH v4 4/4 libfuse: implement syncfs

In reply to Darrick J. Wong

Hi all,

Implement syncfs in libfuse so that iomap-compatible fuse servers can receive syncfs commands.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-attrs
---
Commits in this patchset:
* libfuse: wire up FUSE_SYNCFS to the low level library
* libfuse: add syncfs support to the upper library

---
 include/fuse.h          |    5 +++++
 include/fuse_lowlevel.h |   16 ++++++++++++++++
 lib/fuse.c              |   31 +++++++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   19 +++++++++++++++++++
 4 files changed, 71 insertions(+)
#137 lore raw

PATCH 1/2 libfuse: wire up FUSE_SYNCFS to the low level library

From: Darrick J. Wong <[email protected]>

Create hooks in the lowlevel library for syncfs.

---
 include/fuse_lowlevel.h |   16 ++++++++++++++++
 lib/fuse_lowlevel.c     |   19 +++++++++++++++++++
 2 files changed, 35 insertions(+)
include/fuse_lowlevel.h +16 -0
index 326c8f061aecfa..90a09b066c71f0 100644
@@ -1422,6 +1422,22 @@ struct fuse_lowlevel_ops {
 	 */
 	void (*iomap_config) (fuse_req_t req, uint64_t flags,
 			      uint64_t maxbytes);
+
+	/**
+	 * Flush the entire filesystem to disk.
+	 *
+	 * If this request is answered with an error code of ENOSYS, this is
+	 * treated as a permanent failure, i.e. all future syncfs() requests
+	 * will fail with the same error code without being sent to the
+	 * filesystem process.
+	 *
+	 * Valid replies:
+	 *   fuse_reply_err
+	 *
+	 * @param req request handle
+	 * @param ino the inode number
+	 */
+	void (*syncfs) (fuse_req_t req, fuse_ino_t ino);
 };
 
 /**
lib/fuse_lowlevel.c +19 -0
index 721abe2686d9c4..e5c7c4487cef8c 100644
@@ -2776,6 +2776,23 @@ static void do_iomap_config(fuse_req_t req, const fuse_ino_t nodeid,
 	_do_iomap_config(req, nodeid, inarg, NULL);
 }
 
+static void _do_syncfs(fuse_req_t req, const fuse_ino_t nodeid,
+		      const void *op_in, const void *in_payload)
+{
+	(void)op_in;
+	(void)in_payload;
+
+	if (req->se->op.syncfs)
+		req->se->op.syncfs(req, nodeid);
+	else
+		fuse_reply_err(req, ENOSYS);
+}
+
+static void do_syncfs(fuse_req_t req, const fuse_ino_t nodeid, const void *inarg)
+{
+	_do_syncfs(req, nodeid, inarg, NULL);
+}
+
 static bool want_flags_valid(uint64_t capable, uint64_t want)
 {
 	uint64_t unknown_flags = want & (~capable);
@@ -3756,6 +3773,7 @@ static struct {
 	[FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
 	[FUSE_LSEEK]	   = { do_lseek,       "LSEEK"	     },
 	[FUSE_STATX]	   = { do_statx,       "STATX"	     },
+	[FUSE_SYNCFS]	   = { do_syncfs,	"SYNCFS"     },
 	[FUSE_IOMAP_CONFIG]= { do_iomap_config, "IOMAP_CONFIG" },
 	[FUSE_IOMAP_BEGIN] = { do_iomap_begin,	"IOMAP_BEGIN" },
 	[FUSE_IOMAP_END]   = { do_iomap_end,	"IOMAP_END" },
@@ -3815,6 +3833,7 @@ static struct {
 	[FUSE_COPY_FILE_RANGE]	= { _do_copy_file_range, "COPY_FILE_RANGE" },
 	[FUSE_LSEEK]		= { _do_lseek,		"LSEEK" },
 	[FUSE_STATX]		= { _do_statx,		"STATX" },
+	[FUSE_SYNCFS]		= { _do_syncfs,		"SYNCFS" },
 	[FUSE_IOMAP_CONFIG]	= { _do_iomap_config,	"IOMAP_CONFIG" },
 	[FUSE_IOMAP_BEGIN]	= { _do_iomap_begin,	"IOMAP_BEGIN" },
 	[FUSE_IOMAP_END]	= { _do_iomap_end,	"IOMAP_END" },
#138 lore raw

PATCH 2/2 libfuse: add syncfs support to the upper library

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Support syncfs in the upper level library.

---
 include/fuse.h |    5 +++++
 lib/fuse.c     |   31 +++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
include/fuse.h +5 -0
index f8a57154017a2a..baf7a2e90af5e7 100644
@@ -903,6 +903,11 @@ struct fuse_operations {
 	 */
 	int (*iomap_config) (uint64_t supported_flags, off_t maxbytes,
 			     struct fuse_iomap_config *cfg);
+
+	/**
+	 * Flush the entire filesystem to disk.
+	 */
+	int (*syncfs) (const char *path);
 };
 
 /** Extra context that may be needed by some filesystems
lib/fuse.c +31 -0
index 7b28f848116abb..4e207491532e8b 100644
@@ -2993,6 +2993,16 @@ int fuse_fs_iomap_inval(uint64_t nodeid, uint64_t attr_ino, loff_t read_off,
 						&write);
 }
 
+static int fuse_fs_syncfs(struct fuse_fs *fs, const char *path)
+{
+	fuse_get_context()->private_data = fs->user_data;
+	if (!fs->op.syncfs)
+		return -ENOSYS;
+	if (fs->debug)
+		fuse_log(FUSE_LOG_DEBUG, "syncfs[%s]\n", path);
+	return fs->op.syncfs(path);
+}
+
 static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
 			     int valid, struct fuse_file_info *fi)
 {
@@ -4866,6 +4876,26 @@ static void fuse_lib_iomap_config(fuse_req_t req, uint64_t flags,
 	fuse_reply_iomap_config(req, &cfg);
 }
 
+static void fuse_lib_syncfs(fuse_req_t req, fuse_ino_t ino)
+{
+	struct fuse *f = req_fuse_prepare(req);
+	struct fuse_intr_data d;
+	char *path;
+	int err;
+
+	err = get_path(f, ino, &path);
+	if (err) {
+		reply_err(req, err);
+		return;
+	}
+
+	fuse_prepare_interrupt(f, req, &d);
+	err = fuse_fs_syncfs(f->fs, path);
+	fuse_finish_interrupt(f, req, &d);
+	free_path(f, ino, path);
+	reply_err(req, err);
+}
+
 static int clean_delay(struct fuse *f)
 {
 	/*
@@ -4967,6 +4997,7 @@ static struct fuse_lowlevel_ops fuse_path_ops = {
 #ifdef HAVE_STATX
 	.statx = fuse_lib_statx,
 #endif
+	.syncfs = fuse_lib_syncfs,
 	.iomap_begin = fuse_lib_iomap_begin,
 	.iomap_end = fuse_lib_iomap_end,
 	.iomap_ioend = fuse_lib_iomap_ioend,
maintainer
#139 lore raw

Re: [PATCHSET RFC v4 4/4] libfuse: implement syncfs

In reply to Darrick J. Wong
Quoted 19 lines · Darrick J. Wong

Hi all,

Implement syncfs in libfuse so that iomap-compatible fuse servers can receive syncfs commands.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-attrs
---
Commits in this patchset:
* libfuse: wire up FUSE_SYNCFS to the low level library
* libfuse: add syncfs support to the upper library

Quoted 7 lines
---
 include/fuse.h          |    5 +++++
 include/fuse_lowlevel.h |   16 ++++++++++++++++
 lib/fuse.c              |   31 +++++++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   19 +++++++++++++++++++
 4 files changed, 71 insertions(+)

Thank you, both look good to me. This is independent of io-map - we can apply it immediately?

Thanks,
Bernd

#140 lore raw

Re: [PATCHSET RFC v4 4/4] libfuse: implement syncfs

Quoted 30 lines · Bernd Schubert
Quoted 19 lines · Darrick J. Wong

Hi all,

Implement syncfs in libfuse so that iomap-compatible fuse servers can receive syncfs commands.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

With a bit of luck, this should all go splendidly. Comments and questions are, as always, welcome.

--D

kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=fuse-iomap-attrs
---
Commits in this patchset:
* libfuse: wire up FUSE_SYNCFS to the low level library
* libfuse: add syncfs support to the upper library

Quoted 7 lines
---
 include/fuse.h          |    5 +++++
 include/fuse_lowlevel.h |   16 ++++++++++++++++
 lib/fuse.c              |   31 +++++++++++++++++++++++++++++++
 lib/fuse_lowlevel.c     |   19 +++++++++++++++++++
 4 files changed, 71 insertions(+)
Quoted 2 lines

Thank you, both look good to me. This is independent of io-map - we can apply it immediately?

Yes, please! Note that we'll have to decide if the kernel is going to enable sending syncfs for all servers, not just the virtiofs ones.

--D

Quoted 3 lines

Thanks,
Bernd

#141 lore raw

RFC PATCH v4 1/6 fuse4fs: fork a low level fuse server

In reply to Darrick J. Wong

Hi all,

Whilst developing the fuse2fs+iomap prototype, I discovered a fundamental design limitation of the upper-level libfuse API: hardlinks. The upper level fuse library really wants to communicate with the fuse server with file paths, instead of using inode numbers. This works great for filesystems that don't have inodes, create files dynamically at runtime, or lack stable inode numbers.

Unfortunately, the libfuse path abstraction assigns a unique nodeid to every child file in the entire filesystem, without regard to hard links. In other words, a hardlinked regular file may have one ondisk inode number but multiple kernel inodes. For classic fuse2fs this isn't a problem because all file access goes through the fuse server and the big library lock protects us from corruption.

For fuse2fs + iomap this is a disaster because we rely on the kernel to coordinate access to inodes. For hardlinked files, we *require* that there only be one in-kernel inode for each ondisk inode.

The path based mechanism is also very inefficient for fuse2fs. Every time a file is accessed, the upper level libfuse passes a new nodeid to the kernel, and on every file access the kernel passes that same nodeid back to libfuse. libfuse then walks its internal directory entry cache to construct a path string for that nodeid and hands it to fuse2fs. fuse2fs then walks the ondisk directory structure to find the ext2 inode number. Every time.

Create a new fuse4fs server from fuse2fs that uses the lowlevel fuse API. This affords us direct control over nodeids and eliminates the path wrangling. Hardlinks can be supported when iomap is turned on, and metadata-heavy workloads run twice as fast.

If you're going to start using this code, I strongly recommend pulling from my git trees, which are linked below.

Comments and questions are, as always, welcome.

e2fsprogs git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/e2fsprogs.git/log/?h=fuse4fs-fork
---
Commits in this patchset:
* fuse2fs: port fuse2fs to lowlevel libfuse API
* fuse4fs: drop fuse 2.x support code
* fuse4fs: namespace some helpers
* fuse4fs: convert to low level API
* libsupport: port the kernel list.h to libsupport
* libsupport: add a cache
* cache: disable debugging
* cache: use modern list iterator macros
* cache: embed struct cache in the owner
* cache: pass cache pointer to callbacks
* cache: pass a private data pointer through cache_walk
* cache: add a helper to grab a new refcount for a cache_node
* cache: return results of a cache flush
* cache: add a "get only if incore" flag to cache_node_get
* cache: support gradual expansion
* cache: implement automatic shrinking
* fuse4fs: add cache to track open files
* fuse4fs: use the orphaned inode list
* fuse4fs: implement FUSE_TMPFILE
* fuse4fs: create incore reverse orphan list

---
 lib/ext2fs/jfs_compat.h  |    2 
 lib/ext2fs/kernel-list.h |  111 -
 lib/support/cache.h      |  177 +
 lib/support/list.h       |  901 +++++++
 lib/support/xbitops.h    |  128 +
 configure                |   50 
 configure.ac             |   31 
 debugfs/Makefile.in      |   12 
 e2fsck/Makefile.in       |   56 
 lib/config.h.in          |    3 
 lib/e2p/Makefile.in      |    4 
 lib/ext2fs/Makefile.in   |   14 
 lib/support/Makefile.in  |    8 
 lib/support/cache.c      |  853 ++++++
 misc/Makefile.in         |   35 
 misc/fuse4fs.c           | 6098 ++++++++++++++++++++++++++++++++++++++++++++++
 misc/tune2fs.c           |    4 
 17 files changed, 8319 insertions(+), 168 deletions(-)
 delete mode 100644 lib/ext2fs/kernel-list.h
 create mode 100644 lib/support/cache.h
 create mode 100644 lib/support/list.h
 create mode 100644 lib/support/xbitops.h
 create mode 100644 lib/support/cache.c
 create mode 100644 misc/fuse4fs.c