Skip to contentSkip to conversation

FUSE BPF: A Stacked Filesystem Extension for FUSE

RFC PATCH In review Daniel Rosenberg 7 participants 59 messages Apr 18, 2023 – May 17, 2023

Subsystems (9)
Documentation VFS Kselftest BPF +5 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover FUSE BPF: A Stacked Filesystem Extension for FUSE

These patches extend FUSE to be able to act as a stacked filesystem. This allows pure passthrough, where the fuse file system simply reflects the lower filesystem, and also allows optional pre and post filtering in BPF and/or the userspace daemon as needed. This can dramatically reduce or even eliminate transitions to and from userspace.

In this patch set, I've reworked the bpf code to add a new struct_op type instead of a new program type, and used new kfuncs in place of new helpers. Additionally, it now uses dynptrs for variable sized buffers. The first three patches are repeats of a previous patch set which I have not yet adjusted for comments. I plan to adjust those and submit them separately with fixes, but wanted to have the current fuse-bpf code visible before then.

Patches 4-7 mostly rearrange existing code to remove noise from the main patch. Patch 8 contains the main sections of fuse-bpf
Patches 9-25 implementing most FUSE functions as operations on a lower filesystem. From patch 25, you can run fuse as a passthrough filesystem. Patches 26-32 provide bpf functionality so that you can alter fuse parameters via fuse_op programs.
Patch 33 extends this to userspace, and patches 34-37 add some testing functionality.

There's definitely a lot of cleanup and some restructuring I would like to do. In the current form, I could get rid of the large macro in place of a function that takes a struct that groups a bunch of function pointers, although I'm not sure a function that takes three void*'s is much better than the macro... I'm definitely open to suggestions on how to clean that up.

This changes the format of adding a backing file/bpf slightly from v2. fuse_op programs are specified by name, limited to 15 characters. The block added to fuse_bpf_entires has been increased to compensate. This adds one more unused field when specifying the backing file.

Lookups responses that add a backing file must go through an ioctl interface. This is to prevent any attempts at fooling priveledged programs with fd trickery.

Currently, there are two types of fuse_bpf_entry. One for passing the fuse_op program you wish to use, specified by name, and one for passing the fd of the backing file you'd like to associate with the given lookup. In the future, this may be extended to a more complicated system allowing for multiple bpf programs or backing files. This would come with kfuncs for bpf to indicate which backing file should be acted upon. Multiple bpf programs would allow chaining existing programs to extend functionality without requiring an entirely new program set.

You can run this without needing to set up a userspace daemon by adding these mount options: root_dir=[fd],no_daemon where fd is an open file descriptor pointing to the folder you'd like to use as the root directory. The fd can be immediately closed after mounting. You may also set a root_bpf program by setting root_bpf=[fuse_op name] after registering a fuse_op program. This is useful for running various fs tests.

This patch set is against bpf-next

The main changes for v3:
Restructured around struct_op programs
Using dynptrs instead of packets
Using kfuncs instead of new helpers
Selftests now use skel for loading

Alessio Balsini (1):
fs: Generic function to convert iocb to rw flags

Daniel Rosenberg (36):
bpf: verifier: Accept dynptr mem as mem in herlpers
bpf: Allow NULL buffers in bpf_dynptr_slice(_rw) selftests/bpf: Test allowing NULL buffer in dynptr slice
fuse-bpf: Update fuse side uapi
fuse-bpf: Add data structures for fuse-bpf
fuse-bpf: Prepare for fuse-bpf patch
fuse: Add fuse-bpf, a stacked fs extension for FUSE
fuse-bpf: Add ioctl interface for /dev/fuse
fuse-bpf: Don't support export_operations
fuse-bpf: Add support for access
fuse-bpf: Partially add mapping support
fuse-bpf: Add lseek support
fuse-bpf: Add support for fallocate
fuse-bpf: Support file/dir open/close
fuse-bpf: Support mknod/unlink/mkdir/rmdir
fuse-bpf: Add support for read/write iter
fuse-bpf: support readdir
fuse-bpf: Add support for sync operations
fuse-bpf: Add Rename support
fuse-bpf: Add attr support
fuse-bpf: Add support for FUSE_COPY_FILE_RANGE
fuse-bpf: Add xattr support
fuse-bpf: Add symlink/link support
fuse-bpf: allow mounting with no userspace daemon
bpf: Increase struct_op limits
fuse-bpf: Add fuse-bpf constants
WIP: bpf: Add fuse_ops struct_op programs
fuse-bpf: Export Functions
fuse: Provide registration functions for fuse-bpf
fuse-bpf: Set fuse_ops at mount or lookup time
fuse-bpf: Call bpf for pre/post filters
fuse-bpf: Add userspace pre/post filters
WIP: fuse-bpf: add error_out
tools: Add FUSE, update bpf includes
fuse-bpf: Add selftests
fuse: Provide easy way to test fuse struct_op call

 Documentation/bpf/kfuncs.rst                  |   23 +-
 fs/fuse/Kconfig                               |    8 +
 fs/fuse/Makefile                              |    1 +
 fs/fuse/backing.c                             | 4241 +++++++++++++++++
 fs/fuse/bpf_register.c                        |  209 +
 fs/fuse/control.c                             |    2 +-
 fs/fuse/dev.c                                 |   85 +-
 fs/fuse/dir.c                                 |  344 +-
 fs/fuse/file.c                                |   63 +-
 fs/fuse/fuse_i.h                              |  495 +-
 fs/fuse/inode.c                               |  360 +-
 fs/fuse/ioctl.c                               |    2 +-
 fs/fuse/readdir.c                             |    5 +
 fs/fuse/xattr.c                               |   18 +
 fs/overlayfs/file.c                           |   23 +-
 include/linux/bpf.h                           |    2 +-
 include/linux/bpf_fuse.h                      |  283 ++
 include/linux/fs.h                            |    5 +
 include/uapi/linux/bpf.h                      |   12 +
 include/uapi/linux/fuse.h                     |   41 +
 kernel/bpf/Makefile                           |    4 +
 kernel/bpf/bpf_fuse.c                         |  241 +
 kernel/bpf/bpf_struct_ops.c                   |    6 +-
 kernel/bpf/bpf_struct_ops_types.h             |    4 +
 kernel/bpf/btf.c                              |    1 +
 kernel/bpf/helpers.c                          |   32 +-
 kernel/bpf/verifier.c                         |   32 +
 tools/include/uapi/linux/bpf.h                |   12 +
 tools/include/uapi/linux/fuse.h               | 1135 +++++
 .../testing/selftests/bpf/prog_tests/dynptr.c |    1 +
 .../selftests/bpf/progs/dynptr_success.c      |   21 +
 .../selftests/filesystems/fuse/.gitignore     |    2 +
 .../selftests/filesystems/fuse/Makefile       |  189 +
 .../testing/selftests/filesystems/fuse/OWNERS |    2 +
 .../selftests/filesystems/fuse/bpf_common.h   |   51 +
 .../selftests/filesystems/fuse/bpf_loader.c   |  597 +++
 .../testing/selftests/filesystems/fuse/fd.txt |   21 +
 .../selftests/filesystems/fuse/fd_bpf.bpf.c   |  397 ++
 .../selftests/filesystems/fuse/fuse_daemon.c  |  300 ++
 .../selftests/filesystems/fuse/fuse_test.c    | 2412 ++++++++++
 .../filesystems/fuse/struct_op_test.bpf.c     |  642 +++
 .../selftests/filesystems/fuse/test.bpf.c     |  996 ++++
 .../filesystems/fuse/test_framework.h         |  172 +
 .../selftests/filesystems/fuse/test_fuse.h    |  494 ++
 44 files changed, 13755 insertions(+), 231 deletions(-)
 create mode 100644 fs/fuse/backing.c
 create mode 100644 fs/fuse/bpf_register.c
 create mode 100644 include/linux/bpf_fuse.h
 create mode 100644 kernel/bpf/bpf_fuse.c
 create mode 100644 tools/include/uapi/linux/fuse.h
 create mode 100644 tools/testing/selftests/filesystems/fuse/.gitignore
 create mode 100644 tools/testing/selftests/filesystems/fuse/Makefile
 create mode 100644 tools/testing/selftests/filesystems/fuse/OWNERS
 create mode 100644 tools/testing/selftests/filesystems/fuse/bpf_common.h
 create mode 100644 tools/testing/selftests/filesystems/fuse/bpf_loader.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/fd.txt
 create mode 100644 tools/testing/selftests/filesystems/fuse/fd_bpf.bpf.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_daemon.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_test.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/struct_op_test.bpf.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/test.bpf.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/test_framework.h
 create mode 100644 tools/testing/selftests/filesystems/fuse/test_fuse.h

base-commit: 49859de997c3115b85544bce6b6ceab60a7fabc4

Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#1 lore raw

RFC PATCH v3 1/37 bpf: verifier: Accept dynptr mem as mem in herlpers

This allows using memory retrieved from dynptrs with helper functions that accept ARG_PTR_TO_MEM. For instance, results from bpf_dynptr_data can be passed along to bpf_strncmp.

---
 kernel/bpf/verifier.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/bpf/verifier.c +4 -0
index 1e05355facdc..ebc638bfed87 100644
@@ -7128,12 +7128,16 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,
 	 * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL,
 	 * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL
 	 *
+	 * ARG_PTR_TO_MEM is compatible with PTR_TO_MEM that is tagged with a dynptr type.
+	 *
 	 * Therefore we fold these flags depending on the arg_type before comparison.
 	 */
 	if (arg_type & MEM_RDONLY)
 		type &= ~MEM_RDONLY;
 	if (arg_type & PTR_MAYBE_NULL)
 		type &= ~PTR_MAYBE_NULL;
+	if (base_type(arg_type) == ARG_PTR_TO_MEM)
+		type &= ~DYNPTR_TYPE_FLAG_MASK;
 
 	if (meta->func_id == BPF_FUNC_kptr_xchg && type & MEM_ALLOC)
 		type &= ~MEM_ALLOC;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#2 lore raw

RFC PATCH v3 2/37 bpf: Allow NULL buffers in bpf_dynptr_slice(_rw)

In reply to Daniel Rosenberg

bpf_dynptr_slice(_rw) uses a user provided buffer if it can not provide a pointer to a block of contiguous memory. This buffer is unused in the case of local dynptrs, and may be unused in other cases as well. There is no need to require the buffer, as the kfunc can just return NULL if it was needed and not provided.

This adds another kfunc annotation, __opt, which combines with __sz and __szk to allow the buffer associated with the size to be NULL. If the buffer is NULL, the verifier does not check that the buffer is of sufficient size.

---
 Documentation/bpf/kfuncs.rst | 23 ++++++++++++++++++++++-
 kernel/bpf/helpers.c         | 32 ++++++++++++++++++++------------
 kernel/bpf/verifier.c        | 19 +++++++++++++++++++
 3 files changed, 61 insertions(+), 13 deletions(-)
Documentation/bpf/kfuncs.rst +22 -1
index ea2516374d92..7a3d9de5f315 100644
@@ -100,7 +100,7 @@ Hence, whenever a constant scalar argument is accepted by a kfunc which is not a
 size parameter, and the value of the constant matters for program safety, __k
 suffix should be used.
 
-2.2.2 __uninit Annotation
+2.2.3 __uninit Annotation
 -------------------------
 
 This annotation is used to indicate that the argument will be treated as
@@ -117,6 +117,27 @@ Here, the dynptr will be treated as an uninitialized dynptr. Without this
 annotation, the verifier will reject the program if the dynptr passed in is
 not initialized.
 
+2.2.4 __opt Annotation
+-------------------------
+
+This annotation is used to indicate that the buffer associated with an __sz or __szk
+argument may be null. If the function is passed a nullptr in place of the buffer,
+the verifier will not check that length is appropriate for the buffer. The kfunc is
+responsible for checking if this buffer is null before using it.
+
+An example is given below::
+
+        __bpf_kfunc void *bpf_dynptr_slice(..., void *buffer__opt, u32 buffer__szk)
+        {
+        ...
+        }
+
+Here, the buffer may be null. If buffer is not null, it at least of size buffer_szk.
+Either way, the returned buffer is either NULL, or of size buffer_szk. Without this
+annotation, the verifier will reject the program if a null pointer is passed in with
+a nonzero size.
+
+
 .. _BPF_kfunc_nodef:
 
 2.3 Using an existing kernel function
kernel/bpf/helpers.c +20 -12
index 00e5fb0682ac..bfb75ecacb76 100644
@@ -2167,13 +2167,15 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)
  * bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data.
  * @ptr: The dynptr whose data slice to retrieve
  * @offset: Offset into the dynptr
- * @buffer: User-provided buffer to copy contents into
- * @buffer__szk: Size (in bytes) of the buffer. This is the length of the
- *		 requested slice. This must be a constant.
+ * @buffer__opt: User-provided buffer to copy contents into.  May be NULL
+ * @buffer__szk: Size (in bytes) of the buffer if present. This is the
+ *               length of the requested slice. This must be a constant.
  *
  * For non-skb and non-xdp type dynptrs, there is no difference between
  * bpf_dynptr_slice and bpf_dynptr_data.
  *
+ *  If buffer__opt is NULL, the call will fail if buffer_opt was needed.
+ *
  * If the intention is to write to the data slice, please use
  * bpf_dynptr_slice_rdwr.
  *
@@ -2190,7 +2192,7 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)
  * direct pointer)
  */
 __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset,
-				   void *buffer, u32 buffer__szk)
+				   void *buffer__opt, u32 buffer__szk)
 {
 	enum bpf_dynptr_type type;
 	u32 len = buffer__szk;
@@ -2210,15 +2212,19 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
 	case BPF_DYNPTR_TYPE_RINGBUF:
 		return ptr->data + ptr->offset + offset;
 	case BPF_DYNPTR_TYPE_SKB:
-		return skb_header_pointer(ptr->data, ptr->offset + offset, len, buffer);
+		if (!buffer__opt)
+			return NULL;
+		return skb_header_pointer(ptr->data, ptr->offset + offset, len, buffer__opt);
 	case BPF_DYNPTR_TYPE_XDP:
 	{
 		void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len);
 		if (xdp_ptr)
 			return xdp_ptr;
 
-		bpf_xdp_copy_buf(ptr->data, ptr->offset + offset, buffer, len, false);
-		return buffer;
+		if (!buffer__opt)
+			return NULL;
+		bpf_xdp_copy_buf(ptr->data, ptr->offset + offset, buffer__opt, len, false);
+		return buffer__opt;
 	}
 	default:
 		WARN_ONCE(true, "unknown dynptr type %d\n", type);
@@ -2230,13 +2236,15 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
  * bpf_dynptr_slice_rdwr() - Obtain a writable pointer to the dynptr data.
  * @ptr: The dynptr whose data slice to retrieve
  * @offset: Offset into the dynptr
- * @buffer: User-provided buffer to copy contents into
- * @buffer__szk: Size (in bytes) of the buffer. This is the length of the
- *		 requested slice. This must be a constant.
+ * @buffer__opt: User-provided buffer to copy contents into. May be NULL
+ * @buffer__szk: Size (in bytes) of the buffer if present. This is the
+ *               length of the requested slice. This must be a constant.
  *
  * For non-skb and non-xdp type dynptrs, there is no difference between
  * bpf_dynptr_slice and bpf_dynptr_data.
  *
+ * If buffer__opt is NULL, the call will fail if buffer_opt was needed.
+ *
  * The returned pointer is writable and may point to either directly the dynptr
  * data at the requested offset or to the buffer if unable to obtain a direct
  * data pointer to (example: the requested slice is to the paged area of an skb
@@ -2267,7 +2275,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
  * direct pointer)
  */
 __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,
-					void *buffer, u32 buffer__szk)
+					void *buffer__opt, u32 buffer__szk)
 {
 	if (!ptr->data || bpf_dynptr_is_rdonly(ptr))
 		return NULL;
@@ -2294,7 +2302,7 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
 	 * will be copied out into the buffer and the user will need to call
 	 * bpf_dynptr_write() to commit changes.
 	 */
-	return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
+	return bpf_dynptr_slice(ptr, offset, buffer__opt, buffer__szk);
 }
 
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
kernel/bpf/verifier.c +19 -0
index ebc638bfed87..fd959824469d 100644
@@ -9387,6 +9387,19 @@ static bool is_kfunc_arg_const_mem_size(const struct btf *btf,
 	return __kfunc_param_match_suffix(btf, arg, "__szk");
 }
 
+static bool is_kfunc_arg_optional(const struct btf *btf,
+		  const struct btf_param *arg,
+		  const struct bpf_reg_state *reg)
+{
+	const struct btf_type *t;
+
+	t = btf_type_skip_modifiers(btf, arg->type, NULL);
+	if (!btf_type_is_ptr(t) || reg->type != SCALAR_VALUE || reg->umax_value > 0)
+		return false;
+
+	return __kfunc_param_match_suffix(btf, arg, "__opt");
+}
+
 static bool is_kfunc_arg_constant(const struct btf *btf, const struct btf_param *arg)
 {
 	return __kfunc_param_match_suffix(btf, arg, "__k");
@@ -10453,10 +10466,16 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
 			break;
 		case KF_ARG_PTR_TO_MEM_SIZE:
 		{
+			struct bpf_reg_state *buff_reg = &regs[regno];
+			const struct btf_param *buff_arg = &args[i];
 			struct bpf_reg_state *size_reg = &regs[regno + 1];
 			const struct btf_param *size_arg = &args[i + 1];
 
 			ret = check_kfunc_mem_size_reg(env, size_reg, regno + 1);
+			if (ret < 0 && is_kfunc_arg_optional(meta->btf, buff_arg, buff_reg)) {
+				verbose(env, "error was %d", ret);
+				ret = 0;
+			}
 			if (ret < 0) {
 				verbose(env, "arg#%d arg#%d memory, len pair leads to invalid memory access\n", i, i + 1);
 				return ret;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#3 lore raw

RFC PATCH v3 3/37 selftests/bpf: Test allowing NULL buffer in dynptr slice

In reply to Daniel Rosenberg

bpf_dynptr_slice(_rw) no longer requires a buffer for verification. If the buffer is needed, but not present, the function will return NULL.

---
 .../testing/selftests/bpf/prog_tests/dynptr.c |  1 +
 .../selftests/bpf/progs/dynptr_success.c      | 21 +++++++++++++++++++
 2 files changed, 22 insertions(+)
tools/testing/selftests/bpf/prog_tests/dynptr.c +1 -0
index d176c34a7d2e..db22cad32657 100644
@@ -20,6 +20,7 @@ static struct {
 	{"test_ringbuf", SETUP_SYSCALL_SLEEP},
 	{"test_skb_readonly", SETUP_SKB_PROG},
 	{"test_dynptr_skb_data", SETUP_SKB_PROG},
+	{"test_dynptr_skb_nobuff", SETUP_SKB_PROG},
 };
 
 static void verify_success(const char *prog_name, enum test_setup_type setup_type)
tools/testing/selftests/bpf/progs/dynptr_success.c +21 -0
index b2fa6c47ecc0..a059ed8d4590 100644
@@ -207,3 +207,24 @@ int test_dynptr_skb_data(struct __sk_buff *skb)
 
 	return 1;
 }
+
+SEC("?cgroup_skb/egress")
+int test_dynptr_skb_no_buff(struct __sk_buff *skb)
+{
+	struct bpf_dynptr ptr;
+	__u64 *data;
+
+	if (bpf_dynptr_from_skb(skb, 0, &ptr)) {
+		err = 1;
+		return 1;
+	}
+
+	/* This should return NULL. SKB may require a buffer */
+	data = bpf_dynptr_slice(&ptr, 0, NULL, 1);
+	if (data) {
+		err = 2;
+		return 1;
+	}
+
+	return 1;
+}
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#4 lore raw

RFC PATCH v3 4/37 fs: Generic function to convert iocb to rw flags

In reply to Daniel Rosenberg

From: Alessio Balsini <[email protected]>

OverlayFS implements its own function to translate iocb flags into rw flags, so that they can be passed into another vfs call. With commit ce71bfea207b4 ("fs: align IOCB_* flags with RWF_* flags") Jens created a 1:1 matching between the iocb flags and rw flags, simplifying the conversion.

Reduce the OverlayFS code by making the flag conversion function generic and reusable.

---
 fs/overlayfs/file.c | 23 +++++------------------
 include/linux/fs.h  |  5 +++++
 2 files changed, 10 insertions(+), 18 deletions(-)
fs/overlayfs/file.c +5 -18
index 7c04f033aadd..759893e4da04 100644
@@ -15,6 +15,8 @@
 #include <linux/fs.h>
 #include "overlayfs.h"
 
+#define OVL_IOCB_MASK (IOCB_DSYNC | IOCB_HIPRI | IOCB_NOWAIT | IOCB_SYNC)
+
 struct ovl_aio_req {
 	struct kiocb iocb;
 	refcount_t ref;
@@ -241,22 +243,6 @@ static void ovl_file_accessed(struct file *file)
 	touch_atime(&file->f_path);
 }
 
-static rwf_t ovl_iocb_to_rwf(int ifl)
-{
-	rwf_t flags = 0;
-
-	if (ifl & IOCB_NOWAIT)
-		flags |= RWF_NOWAIT;
-	if (ifl & IOCB_HIPRI)
-		flags |= RWF_HIPRI;
-	if (ifl & IOCB_DSYNC)
-		flags |= RWF_DSYNC;
-	if (ifl & IOCB_SYNC)
-		flags |= RWF_SYNC;
-
-	return flags;
-}
-
 static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
 {
 	if (refcount_dec_and_test(&aio_req->ref)) {
@@ -316,7 +302,8 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
 	if (is_sync_kiocb(iocb)) {
 		ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
-				    ovl_iocb_to_rwf(iocb->ki_flags));
+				    iocb_to_rw_flags(iocb->ki_flags,
+						     OVL_IOCB_MASK));
 	} else {
 		struct ovl_aio_req *aio_req;
 
@@ -380,7 +367,7 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
 	if (is_sync_kiocb(iocb)) {
 		file_start_write(real.file);
 		ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
-				     ovl_iocb_to_rwf(ifl));
+				     iocb_to_rw_flags(ifl, OVL_IOCB_MASK));
 		file_end_write(real.file);
 		/* Update size */
 		ovl_copyattr(inode);
include/linux/fs.h +5 -0
index c85916e9f7db..c849074f44b7 100644
@@ -3009,6 +3009,11 @@ static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags)
 	return 0;
 }
 
+static inline rwf_t iocb_to_rw_flags(int ifl, int iocb_mask)
+{
+	return ifl & iocb_mask;
+}
+
 static inline ino_t parent_ino(struct dentry *dentry)
 {
 	ino_t res;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#5 lore raw

RFC PATCH v3 5/37 fuse-bpf: Update fuse side uapi

In reply to Daniel Rosenberg

Adds structures which will be used to inform fuse about what it is being stacked on top of. Once filters are in place, error_in will inform the post filter if the backing call returned an error.

---
 include/uapi/linux/fuse.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
include/uapi/linux/fuse.h +23 -0
index 1b9d0dfae72d..04d96f34e9a1 100644
@@ -607,6 +607,29 @@ struct fuse_entry_out {
 	struct fuse_attr attr;
 };
 
+#define FUSE_BPF_MAX_ENTRIES	2
+
+enum fuse_bpf_type {
+	FUSE_ENTRY_BACKING		= 1,
+	FUSE_ENTRY_BPF			= 2,
+	FUSE_ENTRY_REMOVE_BACKING	= 3,
+	FUSE_ENTRY_REMOVE_BPF		= 4,
+};
+
+#define BPF_FUSE_NAME_MAX 15
+
+struct fuse_bpf_entry_out {
+	uint32_t	entry_type;
+	uint32_t	unused;
+	union {
+		struct {
+			uint64_t unused2;
+			uint64_t fd;
+		};
+		char name[BPF_FUSE_NAME_MAX + 1];
+	};
+};
+
 struct fuse_forget_in {
 	uint64_t	nlookup;
 };
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#6 lore raw

RFC PATCH v3 6/37 fuse-bpf: Add data structures for fuse-bpf

In reply to Daniel Rosenberg

These structures will be used to interact between the fuse bpf calls and normal userspace calls

---
 include/linux/bpf_fuse.h | 84 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
 create mode 100644 include/linux/bpf_fuse.h
include/linux/bpf_fuse.h new file +84 -0
new file mode 100644
index 000000000000..ce8b1b347496
@@ -0,0 +1,84 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright 2022 Google LLC.
+ */
+
+#ifndef _BPF_FUSE_H
+#define _BPF_FUSE_H
+
+#include <linux/types.h>
+#include <linux/fuse.h>
+
+struct fuse_buffer {
+	void *data;
+	unsigned size;
+	unsigned alloc_size;
+	unsigned max_size;
+	int flags;
+};
+
+/* These flags are used internally to track information about the fuse buffers.
+ * Fuse sets some of the flags in init. The helper functions sets others, depending on what
+ * was requested by the bpf program.
+ */
+// Flags set by FUSE
+#define BPF_FUSE_IMMUTABLE	(1 << 0) // Buffer may not be written to
+#define BPF_FUSE_VARIABLE_SIZE	(1 << 1) // Buffer length may be changed (growth requires alloc)
+#define BPF_FUSE_MUST_ALLOCATE	(1 << 2) // Buffer must be re allocated before allowing writes
+
+// Flags set by helper function
+#define BPF_FUSE_MODIFIED	(1 << 3) // The helper function allowed writes to the buffer
+#define BPF_FUSE_ALLOCATED	(1 << 4) // The helper function allocated the buffer
+
+/*
+ * BPF Fuse Args
+ *
+ * Used to translate between bpf program parameters and their userspace equivalent calls.
+ * Variable sized arguments are held in fuse_buffers. To access these, bpf programs must
+ * use kfuncs to access them as dynptrs.
+ *
+ */
+
+#define FUSE_MAX_ARGS_IN 3
+#define FUSE_MAX_ARGS_OUT 2
+
+struct bpf_fuse_arg {
+	union {
+		void *value;
+		struct fuse_buffer *buffer;
+	};
+	unsigned size;
+	bool is_buffer;
+};
+
+struct bpf_fuse_meta_info {
+	uint64_t nodeid;
+	uint32_t opcode;
+	uint32_t error_in;
+};
+
+struct bpf_fuse_args {
+	struct bpf_fuse_meta_info info;
+	uint32_t in_numargs;
+	uint32_t out_numargs;
+	uint32_t flags;
+	struct bpf_fuse_arg in_args[FUSE_MAX_ARGS_IN];
+	struct bpf_fuse_arg out_args[FUSE_MAX_ARGS_OUT];
+};
+
+// Mirrors for struct fuse_args flags
+#define FUSE_BPF_FORCE (1 << 0)
+#define FUSE_BPF_OUT_ARGVAR (1 << 6)
+#define FUSE_BPF_IS_LOOKUP (1 << 11)
+
+static inline void *bpf_fuse_arg_value(const struct bpf_fuse_arg *arg)
+{
+	return arg->is_buffer ? arg->buffer : arg->value;
+}
+
+static inline unsigned bpf_fuse_arg_size(const struct bpf_fuse_arg *arg)
+{
+	return arg->is_buffer ? arg->buffer->size : arg->size;
+}
+
+#endif /* _BPF_FUSE_H */
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#7 lore raw

RFC PATCH v3 7/37 fuse-bpf: Prepare for fuse-bpf patch

In reply to Daniel Rosenberg

This moves some functions and structs around to make the following patch easier to read.

---
 fs/fuse/dir.c    | 30 ------------------------------
 fs/fuse/fuse_i.h | 35 +++++++++++++++++++++++++++++++++++
 fs/fuse/inode.c  | 44 ++++++++++++++++++++++----------------------
 3 files changed, 57 insertions(+), 52 deletions(-)
fs/fuse/dir.c +0 -30
index 35bc174f9ba2..55dd6e8b2e43 100644
@@ -46,10 +46,6 @@ static inline u64 fuse_dentry_time(const struct dentry *entry)
 }
 
 #else
-union fuse_dentry {
-	u64 time;
-	struct rcu_head rcu;
-};
 
 static inline void __fuse_dentry_settime(struct dentry *dentry, u64 time)
 {
@@ -83,27 +79,6 @@ static void fuse_dentry_settime(struct dentry *dentry, u64 time)
 	__fuse_dentry_settime(dentry, time);
 }
 
-/*
- * FUSE caches dentries and attributes with separate timeout.  The
- * time in jiffies until the dentry/attributes are valid is stored in
- * dentry->d_fsdata and fuse_inode->i_time respectively.
- */
-
-/*
- * Calculate the time in jiffies until a dentry/attributes are valid
- */
-static u64 time_to_jiffies(u64 sec, u32 nsec)
-{
-	if (sec || nsec) {
-		struct timespec64 ts = {
-			sec,
-			min_t(u32, nsec, NSEC_PER_SEC - 1)
-		};
-
-		return get_jiffies_64() + timespec64_to_jiffies(&ts);
-	} else
-		return 0;
-}
 
 /*
  * Set dentry and possibly attribute timeouts from the lookup/mk*
@@ -115,11 +90,6 @@ void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o)
 		time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
 }
 
-static u64 attr_timeout(struct fuse_attr_out *o)
-{
-	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
-}
-
 u64 entry_attr_timeout(struct fuse_entry_out *o)
 {
 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
fs/fuse/fuse_i.h +35 -0
index 9b7fc7d3c7f1..01ca8bb87b4f 100644
@@ -63,6 +63,14 @@ struct fuse_forget_link {
 	struct fuse_forget_link *next;
 };
 
+/** FUSE specific dentry data */
+#if BITS_PER_LONG < 64
+union fuse_dentry {
+	u64 time;
+	struct rcu_head rcu;
+};
+#endif
+
 /** FUSE inode */
 struct fuse_inode {
 	/** Inode data */
@@ -1324,4 +1332,31 @@ 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);
 
+/*
+ * FUSE caches dentries and attributes with separate timeout.  The
+ * time in jiffies until the dentry/attributes are valid is stored in
+ * dentry->d_fsdata and fuse_inode->i_time respectively.
+ */
+
+/*
+ * Calculate the time in jiffies until a dentry/attributes are valid
+ */
+static inline u64 time_to_jiffies(u64 sec, u32 nsec)
+{
+	if (sec || nsec) {
+		struct timespec64 ts = {
+			sec,
+			min_t(u32, nsec, NSEC_PER_SEC - 1)
+		};
+
+		return get_jiffies_64() + timespec64_to_jiffies(&ts);
+	} else
+		return 0;
+}
+
+static inline u64 attr_timeout(struct fuse_attr_out *o)
+{
+	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
+}
+
 #endif /* _FS_FUSE_I_H */
fs/fuse/inode.c +22 -22

56 lines not on this page · raw message

Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#8 lore raw

RFC PATCH v3 8/37 fuse: Add fuse-bpf, a stacked fs extension for FUSE

In reply to Daniel Rosenberg

Fuse-bpf provides a short circuit path for Fuse implementations that act as a stacked filesystem. For cases that are directly unchanged, operations are passed directly to the backing filesystem. Small adjustments can be handled by bpf prefilters or postfilters, with the option to fall back to userspace as needed.

Fuse implementations may supply backing node information, as well as bpf programs via an optional add on to the lookup structure.

This has been split over the next set of patches for readability. Clusters of fuse ops have been split into their own patches, as well as the actual bpf calls and userspace calls for filters.

---
 fs/fuse/Kconfig   |   8 +
 fs/fuse/Makefile  |   1 +
 fs/fuse/backing.c | 419 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dev.c     |  41 ++++-
 fs/fuse/dir.c     | 187 +++++++++++++++++----
 fs/fuse/file.c    |  25 ++-
 fs/fuse/fuse_i.h  |  99 ++++++++++-
 fs/fuse/inode.c   | 189 +++++++++++++++++----
 fs/fuse/ioctl.c   |   2 +-
 9 files changed, 888 insertions(+), 83 deletions(-)
 create mode 100644 fs/fuse/backing.c
fs/fuse/Kconfig +8 -0
index 038ed0b9aaa5..3a64fa73e591 100644
@@ -52,3 +52,11 @@ config FUSE_DAX
 
 	  If you want to allow mounting a Virtio Filesystem with the "dax"
 	  option, answer Y.
+
+config FUSE_BPF
+	bool "Adds BPF to fuse"
+	depends on FUSE_FS
+	depends on BPF
+	help
+	  Extends FUSE by adding BPF to prefilter calls and potentially pass to a
+	  backing file system
fs/fuse/Makefile +1 -0
index 0c48b35c058d..a0853c439db2 100644
@@ -9,5 +9,6 @@ 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-$(CONFIG_FUSE_DAX) += dax.o
+fuse-$(CONFIG_FUSE_BPF) += backing.o
 
 virtiofs-y := virtio_fs.o
fs/fuse/backing.c new file +419 -0

419 lines not on this page · raw message

fs/fuse/dev.c +38 -3

74 lines not on this page · raw message

fs/fuse/dir.c +152 -35

324 lines not on this page · raw message

fs/fuse/file.c +22 -3
index de37a3a06a71..25fb49f0a9f7 100644
@@ -8,6 +8,7 @@
 
 #include "fuse_i.h"
 
+#include <linux/filter.h>
 #include <linux/pagemap.h>
 #include <linux/slab.h>
 #include <linux/kernel.h>
@@ -127,13 +128,18 @@ static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
 }
 
 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
-				 unsigned int open_flags, bool isdir)
+				 unsigned int open_flags, bool isdir, struct file *file)
 {
 	struct fuse_conn *fc = fm->fc;
 	struct fuse_file *ff;
 	int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
 
-	ff = fuse_file_alloc(fm);
+	if (file && file->private_data) {
+		ff = file->private_data;
+		file->private_data = NULL;
+	} else {
+		ff = fuse_file_alloc(fm);
+	}
 	if (!ff)
 		return ERR_PTR(-ENOMEM);
 
@@ -171,7 +177,7 @@ struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
 		 bool isdir)
 {
-	struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir);
+	struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir, file);
 
 	if (!IS_ERR(ff))
 		file->private_data = ff;
@@ -1948,6 +1954,19 @@ int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
 	 */
 	WARN_ON(wbc->for_reclaim);
 
+	/**
+	 * TODO - fully understand why this is necessary
+	 *
+	 * With fuse-bpf, fsstress fails if rename is enabled without this
+	 *
+	 * We are getting writes here on directory inodes, which do not have an
+	 * initialized file list so crash.
+	 *
+	 * The question is why we are getting those writes
+	 */
+	if (!S_ISREG(inode->i_mode))
+		return 0;
+
 	ff = __fuse_write_file_get(fi);
 	err = fuse_flush_times(inode, ff);
 	if (ff)
fs/fuse/fuse_i.h +93 -6

174 lines not on this page · raw message

fs/fuse/inode.c +154 -35

334 lines not on this page · raw message

fs/fuse/ioctl.c +1 -1
index 8e01bfdfc430..3542d992bde6 100644
@@ -428,7 +428,7 @@ static struct fuse_file *fuse_priv_ioctl_prepare(struct inode *inode)
 	if (!S_ISREG(inode->i_mode) && !isdir)
 		return ERR_PTR(-ENOTTY);
 
-	return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir);
+	return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir, NULL);
 }
 
 static void fuse_priv_ioctl_cleanup(struct inode *inode, struct fuse_file *ff)
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#9 lore raw

Re: [RFC PATCH v3 08/37] fuse: Add fuse-bpf, a stacked fs extension for FUSE

Quoted 5 lines · Daniel Rosenberg

Fuse-bpf provides a short circuit path for Fuse implementations that act as a stacked filesystem. For cases that are directly unchanged, operations are passed directly to the backing filesystem. Small adjustments can be handled by bpf prefilters or postfilters, with the option to fall back to userspace as needed.

Here is my understanding of fuse-bpf design:
- bpf progs can mostly read-only access fuse_args before and after proper vfs operation on a backing path/file/inode.
- args are unconditionally prepared for bpf prog consumption, but progs won't be doing anything with them most of the time.
- progs unfortunately cannot do any real work. they're nothing but simple filters. They can give 'green light' for a fuse_FOO op to be delegated to proper vfs_FOO in backing file. The logic in this patch keeps track of backing_path/file/inode.
- in other words bpf side is "dumb", but it's telling kernel what to do with real things like path/file/inode and the kernel is doing real work and calling vfs_*.

This design adds non-negligible overhead to fuse when CONFIG_FUSE_BPF is set. Comparing to trip to user space it's close to zero, but the cost of initialize_in/out + backing + finalize is not free.
The patch 33 is especially odd.
fuse has a traditional mechanism to upcall to user space with fuse_simple_request. The patch 33 allows bpf prog to return special return value and trigger two more fuse_bpf_simple_request-s to user space. Not clear why.
It seems to me that the main assumption of the fuse bpf design is that bpf prog has to stay short and simple. It cannot do much other than reading and comparing strings with the help of dynptr.
How about we allow bpf attach to fuse_simple_request and nothing else? All fuse ops call it anyway and cmd is already encoded in the args. Then let bpf prog read fuse_args as-is (without converting them to bpf_fuse_args) and avoid doing actual fuse_req to user space.
Also allow bpf prog acquire and remember path/file/inode.
The verifier is already smart enough to track that the prog is doing it safely without leaking references and what not.
And, of course, allow bpf prog call vfs_* via kfuncs.
In other words, instead of hard coding

 +#define bpf_fuse_backing(inode, io, out,                             \
 +                      initialize_in, initialize_out,                 \
 +                      backing, finalize, args...)                    \

one for each fuse_ops in the kernel let bpf prog do the same but on demand. The biggest advantage is that this patch set instead of 95% on fuse side and 5% on bpf will become 5% addition to fuse code. All the logic will be handled purely by bpf. Right now you're limiting it to one backing_file per fuse_file.
With bpf prog driving it the prog can keep multiple backing_files and shuffle access to them as prog decides.
Instead of doing 'return BPF_FUSE_CONTINUE' the bpf progs will
pass 'path' to kfunc bpf_vfs_open, than stash 'struct bpf_file*', etc. Probably will be easier to white board this idea during lsfmmbpf.

First 3 patches look fine. Thank you for resending them separately.

maintainer
#10 lore raw

Re: [RFC PATCH v3 08/37] fuse: Add fuse-bpf, a stacked fs extension for FUSE

Quoted 41 lines · Alexei Starovoitov
Quoted 5 lines · Daniel Rosenberg

Fuse-bpf provides a short circuit path for Fuse implementations that act as a stacked filesystem. For cases that are directly unchanged, operations are passed directly to the backing filesystem. Small adjustments can be handled by bpf prefilters or postfilters, with the option to fall back to userspace as needed.

Here is my understanding of fuse-bpf design:
- bpf progs can mostly read-only access fuse_args before and after proper vfs operation on a backing path/file/inode.
- args are unconditionally prepared for bpf prog consumption, but progs won't be doing anything with them most of the time.
- progs unfortunately cannot do any real work. they're nothing but simple filters. They can give 'green light' for a fuse_FOO op to be delegated to proper vfs_FOO in backing file. The logic in this patch keeps track of backing_path/file/inode.
- in other words bpf side is "dumb", but it's telling kernel what to do with real things like path/file/inode and the kernel is doing real work and calling vfs_*.

This design adds non-negligible overhead to fuse when CONFIG_FUSE_BPF is set. Comparing to trip to user space it's close to zero, but the cost of initialize_in/out + backing + finalize is not free.
The patch 33 is especially odd.
fuse has a traditional mechanism to upcall to user space with fuse_simple_request. The patch 33 allows bpf prog to return special return value and trigger two more fuse_bpf_simple_request-s to user space. Not clear why.
It seems to me that the main assumption of the fuse bpf design is that bpf prog has to stay short and simple. It cannot do much other than reading and comparing strings with the help of dynptr.
How about we allow bpf attach to fuse_simple_request and nothing else? All fuse ops call it anyway and cmd is already encoded in the args. Then let bpf prog read fuse_args as-is (without converting them to bpf_fuse_args) and avoid doing actual fuse_req to user space.
Also allow bpf prog acquire and remember path/file/inode.
The verifier is already smart enough to track that the prog is doing it safely without leaking references and what not.
And, of course, allow bpf prog call vfs_* via kfuncs.
In other words, instead of hard coding

 +#define bpf_fuse_backing(inode, io, out,                             \
 +                      initialize_in, initialize_out,                 \
 +                      backing, finalize, args...)                    \
Quoted 10 lines

one for each fuse_ops in the kernel let bpf prog do the same but on demand. The biggest advantage is that this patch set instead of 95% on fuse side and 5% on bpf will become 5% addition to fuse code. All the logic will be handled purely by bpf. Right now you're limiting it to one backing_file per fuse_file.
With bpf prog driving it the prog can keep multiple backing_files and shuffle access to them as prog decides.
Instead of doing 'return BPF_FUSE_CONTINUE' the bpf progs will
pass 'path' to kfunc bpf_vfs_open, than stash 'struct bpf_file*', etc. Probably will be easier to white board this idea during lsfmmbpf.

I have to admit that sounds a bit challenging, but I'm up for sitting in front of that whiteboard :)

BTW, thanks Daniel (Borkmann) for sorting out the cross track sessions for FS-BFP.
We have another FS only session on FUSE-BFP, but I feel there is plenty to discuss on the FUSE-bypass part, as well as on the BPF part. Same goes for BFP iterators for filesystems session.

Thanks,
Amir.

#11 lore raw

RFC PATCH v3 9/37 fuse-bpf: Add ioctl interface for /dev/fuse

In reply to Daniel Rosenberg

This introduces an alternative method of responding to fuse requests. Lookups supplying a backing fd or bpf will need to call through the ioctl to ensure there can be no attempts to fool priveledged processes into inadvertantly performing other actions.

---
 fs/fuse/dev.c             | 56 ++++++++++++++++++++++++++++++++-------
 fs/fuse/fuse_i.h          |  1 +
 include/uapi/linux/fuse.h |  1 +
 3 files changed, 48 insertions(+), 10 deletions(-)
fs/fuse/dev.c +46 -10
index a3029824c24f..ad7d9d1e6da5 100644
@@ -1016,18 +1016,19 @@ static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
 
 /* Copy the fuse-bpf lookup args and verify them */
 #ifdef CONFIG_FUSE_BPF
-static int fuse_copy_lookup(struct fuse_copy_state *cs, void *val, unsigned size)
+static int fuse_copy_lookup(struct fuse_copy_state *cs, unsigned via_ioctl, void *val, unsigned size)
 {
 	struct fuse_bpf_entry_out *fbeo = (struct fuse_bpf_entry_out *)val;
 	struct fuse_bpf_entry *feb = container_of(fbeo, struct fuse_bpf_entry, out[0]);
 	int num_entries = size / sizeof(*fbeo);
 	int err;
 
-	if (size && size % sizeof(*fbeo) != 0)
+	if (size && (size % sizeof(*fbeo) != 0 || !via_ioctl))
 		return -EINVAL;
 
 	if (num_entries > FUSE_BPF_MAX_ENTRIES)
 		return -EINVAL;
+
 	err = fuse_copy_one(cs, val, size);
 	if (err)
 		return err;
@@ -1036,7 +1037,7 @@ static int fuse_copy_lookup(struct fuse_copy_state *cs, void *val, unsigned size
 	return err;
 }
 #else
-static int fuse_copy_lookup(struct fuse_copy_state *cs, void *val, unsigned size)
+static int fuse_copy_lookup(struct fuse_copy_state *cs, unsigned via_ioctl, void *val, unsigned size)
 {
 	return fuse_copy_one(cs, val, size);
 }
@@ -1045,7 +1046,7 @@ static int fuse_copy_lookup(struct fuse_copy_state *cs, void *val, unsigned size
 /* Copy request arguments to/from userspace buffer */
 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
 			  unsigned argpages, struct fuse_arg *args,
-			  int zeroing, unsigned is_lookup)
+			  int zeroing, unsigned is_lookup, unsigned via_ioct)
 {
 	int err = 0;
 	unsigned i;
@@ -1055,7 +1056,7 @@ static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
 		if (i == numargs - 1 && argpages)
 			err = fuse_copy_pages(cs, arg->size, zeroing);
 		else if (i == numargs - 1 && is_lookup)
-			err = fuse_copy_lookup(cs, arg->value, arg->size);
+			err = fuse_copy_lookup(cs, via_ioct, arg->value, arg->size);
 		else
 			err = fuse_copy_one(cs, arg->value, arg->size);
 	}
@@ -1333,7 +1334,7 @@ static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
 	err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h));
 	if (!err)
 		err = fuse_copy_args(cs, args->in_numargs, args->in_pages,
-				     (struct fuse_arg *) args->in_args, 0, 0);
+				     (struct fuse_arg *) args->in_args, 0, 0, 0);
 	fuse_copy_finish(cs);
 	spin_lock(&fpq->lock);
 	clear_bit(FR_LOCKED, &req->flags);
@@ -1872,7 +1873,8 @@ static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args,
 		lastarg->size -= diffsize;
 	}
 	return fuse_copy_args(cs, args->out_numargs, args->out_pages,
-			      args->out_args, args->page_zeroing, args->is_lookup);
+			      args->out_args, args->page_zeroing, args->is_lookup,
+			      args->via_ioctl);
 }
 
 /*
@@ -1882,7 +1884,7 @@ static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args,
  * it from the list and copy the rest of the buffer to the request.
  * The request is finished by calling fuse_request_end().
  */
-static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
+static ssize_t fuse_dev_do_write(struct fuse_dev *fud, bool from_ioctl,
 				 struct fuse_copy_state *cs, size_t nbytes)
 {
 	int err;
@@ -1954,6 +1956,7 @@ static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
 	if (!req->args->page_replace)
 		cs->move_pages = 0;
 
+	req->args->via_ioctl = from_ioctl;
 	if (oh.error)
 		err = nbytes != sizeof(oh) ? -EINVAL : 0;
 	else
@@ -1992,7 +1995,7 @@ static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
 
 	fuse_copy_init(&cs, 0, from);
 
-	return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
+	return fuse_dev_do_write(fud, false, &cs, iov_iter_count(from));
 }
 
 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
@@ -2073,7 +2076,7 @@ static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
 	if (flags & SPLICE_F_MOVE)
 		cs.move_pages = 1;
 
-	ret = fuse_dev_do_write(fud, &cs, len);
+	ret = fuse_dev_do_write(fud, false, &cs, len);
 
 	pipe_lock(pipe);
 out_free:
@@ -2286,6 +2289,33 @@ static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
 	return 0;
 }
 
+// Provides an alternate means to respond to a fuse request
+static int fuse_handle_ioc_response(struct fuse_dev *dev, void *buff, uint32_t size)
+{
+	struct fuse_copy_state cs;
+	struct iovec *iov = NULL;
+	struct iov_iter iter;
+	int res;
+
+	if (size > PAGE_SIZE)
+		return -EINVAL;
+	iov = (struct iovec *) __get_free_page(GFP_KERNEL);
+	if (!iov)
+		return -ENOMEM;
+
+	iov->iov_base = buff;
+	iov->iov_len = size;
+
+	iov_iter_init(&iter, READ, iov, 1, size);
+	fuse_copy_init(&cs, 0, &iter);
+
+
+	res = fuse_dev_do_write(dev, true, &cs, size);
+	free_page((unsigned long) iov);
+
+	return res;
+}
+
 static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
 			   unsigned long arg)
 {
@@ -2318,6 +2348,12 @@ static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
 		}
 		break;
 	default:
+		if (_IOC_TYPE(cmd) == FUSE_DEV_IOC_MAGIC
+				&& _IOC_NR(cmd) == _IOC_NR(FUSE_DEV_IOC_BPF_RESPONSE(0))
+				&& _IOC_DIR(cmd) == _IOC_WRITE) {
+			res = fuse_handle_ioc_response(fuse_get_dev(file), (void *) arg, _IOC_SIZE(cmd));
+			break;
+		}
 		res = -ENOTTY;
 		break;
 	}
fs/fuse/fuse_i.h +1 -0
index c24878f4a89f..39a9fdf2a752 100644
@@ -316,6 +316,7 @@ struct fuse_args {
 	bool may_block:1;
 	bool is_ext:1;
 	bool is_lookup:1;
+	bool via_ioctl:1;
 	struct fuse_in_arg in_args[3];
 	struct fuse_arg out_args[2];
 	void (*end)(struct fuse_mount *fm, struct fuse_args *args, int error);
include/uapi/linux/fuse.h +1 -0
index 04d96f34e9a1..3ad725a3e968 100644
@@ -1012,6 +1012,7 @@ struct fuse_notify_retrieve_in {
 /* 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_BPF_RESPONSE(N) _IOW(FUSE_DEV_IOC_MAGIC, 125, char[N])
 
 struct fuse_lseek_in {
 	uint64_t	fh;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#12 lore raw

RFC PATCH v3 10/37 fuse-bpf: Don't support export_operations

In reply to Daniel Rosenberg

In the future, we may choose to support these, but it poses some challenges. In order to create a disconnected dentry/inode, we'll need to encode the mountpoint and bpf into the file_handle, which means we'd need a stable representation of them. This also won't hold up to cases where the bpf is not stateless. One possibility is registering bpf programs and mounts in a specific order, so they can be assigned consistent ids we can use in the file_handle. We can defer to the lower filesystem for the lower inode's representation in the file_handle.

---
 fs/fuse/inode.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/fuse/inode.c +8 -0
index b71e8758fab5..fe80984f099a 100644
@@ -1107,6 +1107,14 @@ static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
 	nodeid = get_fuse_inode(inode)->nodeid;
 	generation = inode->i_generation;
 
+#ifdef CONFIG_FUSE_BPF
+	/* TODO: Does it make sense to support this in some cases? */
+	if (!nodeid && get_fuse_inode(inode)->backing_inode) {
+		*max_len = 0;
+		return FILEID_INVALID;
+	}
+#endif
+
 	fh[0] = (u32)(nodeid >> 32);
 	fh[1] = (u32)(nodeid & 0xffffffff);
 	fh[2] = generation;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#13 lore raw

RFC PATCH v3 11/37 fuse-bpf: Add support for access

In reply to Daniel Rosenberg

This adds backing support for FUSE_ACCESS

---
 fs/fuse/backing.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dir.c     |  6 ++++++
 fs/fuse/fuse_i.h  |  6 ++++++
 3 files changed, 59 insertions(+)
fs/fuse/backing.c +47 -0
index 3d895957b5ce..e42622584037 100644
@@ -417,3 +417,50 @@ int fuse_revalidate_backing(struct dentry *entry, unsigned int flags)
 		return backing_entry->d_op->d_revalidate(backing_entry, flags);
 	return 1;
 }
+
+static int fuse_access_initialize_in(struct bpf_fuse_args *fa, struct fuse_access_in *in,
+				     struct inode *inode, int mask)
+{
+	*in = (struct fuse_access_in) {
+		.mask = mask,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.opcode = FUSE_ACCESS,
+			.nodeid = get_node_id(inode),
+		},
+		.in_numargs = 1,
+		.in_args[0].size = sizeof(*in),
+		.in_args[0].value = in,
+	};
+
+	return 0;
+}
+
+static int fuse_access_initialize_out(struct bpf_fuse_args *fa, struct fuse_access_in *in,
+				      struct inode *inode, int mask)
+{
+	return 0;
+}
+
+static int fuse_access_backing(struct bpf_fuse_args *fa, int *out, struct inode *inode, int mask)
+{
+	struct fuse_inode *fi = get_fuse_inode(inode);
+	const struct fuse_access_in *fai = fa->in_args[0].value;
+
+	*out = inode_permission(&nop_mnt_idmap, fi->backing_inode, fai->mask);
+	return 0;
+}
+
+static int fuse_access_finalize(struct bpf_fuse_args *fa, int *out, struct inode *inode, int mask)
+{
+	return 0;
+}
+
+int fuse_bpf_access(int *out, struct inode *inode, int mask)
+{
+	return bpf_fuse_backing(inode, struct fuse_access_in, out,
+				fuse_access_initialize_in, fuse_access_initialize_out,
+				fuse_access_backing, fuse_access_finalize, inode, mask);
+}
fs/fuse/dir.c +6 -0
index 73ebe3498fb9..535e6cf9e970 100644
@@ -1439,6 +1439,9 @@ static int fuse_access(struct inode *inode, int mask)
 	struct fuse_access_in inarg;
 	int err;
 
+	if (fuse_bpf_access(&err, inode, mask))
+		return err;
+
 	BUG_ON(mask & MAY_NOT_BLOCK);
 
 	if (fm->fc->no_access)
@@ -1495,6 +1498,9 @@ static int fuse_permission(struct mnt_idmap *idmap,
 	if (!fuse_allow_current_process(fc))
 		return -EACCES;
 
+	if (fuse_bpf_access(&err, inode, mask))
+		return err;
+
 	/*
 	 * If attributes are needed, refresh them before proceeding
 	 */
fs/fuse/fuse_i.h +6 -0
index 39a9fdf2a752..cb166168f9c2 100644
@@ -1406,6 +1406,7 @@ int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num_entries);
 #ifdef CONFIG_FUSE_BPF
 
 int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags);
+int fuse_bpf_access(int *out, struct inode *inode, int mask);
 
 #else
 
@@ -1414,6 +1415,11 @@ static inline int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct
 	return 0;
 }
 
+static inline int fuse_bpf_access(int *out, struct inode *inode, int mask)
+{
+	return 0;
+}
+
 #endif // CONFIG_FUSE_BPF
 
 int fuse_handle_backing(struct fuse_bpf_entry *feb, struct path *backing_path);
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#14 lore raw

RFC PATCH v3 12/37 fuse-bpf: Partially add mapping support

In reply to Daniel Rosenberg

This adds a backing implementation for mapping, but is not currently hooked into the infrastructure that will call the bpf programs.

---
 fs/fuse/backing.c | 37 +++++++++++++++++++++++++++++++++++++
 fs/fuse/file.c    |  6 ++++++
 fs/fuse/fuse_i.h  |  4 +++-
 3 files changed, 46 insertions(+), 1 deletion(-)
fs/fuse/backing.c +37 -0
index e42622584037..930aa370e376 100644
@@ -207,6 +207,43 @@ static void fuse_stat_to_attr(struct fuse_conn *fc, struct inode *inode,
 	attr->blksize = 1 << blkbits;
 }
 
+ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma)
+{
+	int ret;
+	struct fuse_file *ff = file->private_data;
+	struct inode *fuse_inode = file_inode(file);
+	struct file *backing_file = ff->backing_file;
+	struct inode *backing_inode = file_inode(backing_file);
+
+	if (!backing_file->f_op->mmap)
+		return -ENODEV;
+
+	if (WARN_ON(file != vma->vm_file))
+		return -EIO;
+
+	vma->vm_file = get_file(backing_file);
+
+	ret = call_mmap(vma->vm_file, vma);
+
+	if (ret)
+		fput(backing_file);
+	else
+		fput(file);
+
+	if (file->f_flags & O_NOATIME)
+		return ret;
+
+	if ((!timespec64_equal(&fuse_inode->i_mtime, &backing_inode->i_mtime) ||
+	     !timespec64_equal(&fuse_inode->i_ctime,
+			       &backing_inode->i_ctime))) {
+		fuse_inode->i_mtime = backing_inode->i_mtime;
+		fuse_inode->i_ctime = backing_inode->i_ctime;
+	}
+	touch_atime(&file->f_path);
+
+	return ret;
+}
+
 /*******************************************************************************
  * Directory operations after here                                             *
  ******************************************************************************/
fs/fuse/file.c +6 -0
index 25fb49f0a9f7..865167a80d35 100644
@@ -2527,6 +2527,12 @@ static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
 	if (FUSE_IS_DAX(file_inode(file)))
 		return fuse_dax_mmap(file, vma);
 
+#ifdef CONFIG_FUSE_BPF
+	/* TODO - this is simply passthrough, not a proper BPF filter */
+	if (ff->backing_file)
+		return fuse_backing_mmap(file, vma);
+#endif
+
 	if (ff->open_flags & FOPEN_DIRECT_IO) {
 		/* Can't provide the coherency needed for MAP_SHARED */
 		if (vma->vm_flags & VM_MAYSHARE)
fs/fuse/fuse_i.h +3 -1
index cb166168f9c2..5eb357f482dc 100644
@@ -1422,7 +1422,9 @@ static inline int fuse_bpf_access(int *out, struct inode *inode, int mask)
 
 #endif // CONFIG_FUSE_BPF
 
-int fuse_handle_backing(struct fuse_bpf_entry *feb, struct path *backing_path);
+ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma);
+
+int fuse_handle_backing(struct fuse_bpf_entry *fbe, struct path *backing_path);
 
 int fuse_revalidate_backing(struct dentry *entry, unsigned int flags);
 
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#15 lore raw

RFC PATCH v3 13/37 fuse-bpf: Add lseek support

In reply to Daniel Rosenberg

This adds backing support for FUSE_LSEEK

---
 fs/fuse/backing.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/file.c    |  3 ++
 fs/fuse/fuse_i.h  |  6 ++++
 3 files changed, 98 insertions(+)
fs/fuse/backing.c +89 -0
index 930aa370e376..c4916dde48c8 100644
@@ -207,6 +207,95 @@ static void fuse_stat_to_attr(struct fuse_conn *fc, struct inode *inode,
 	attr->blksize = 1 << blkbits;
 }
 
+struct fuse_lseek_args {
+	struct fuse_lseek_in in;
+	struct fuse_lseek_out out;
+};
+
+static int fuse_lseek_initialize_in(struct bpf_fuse_args *fa, struct fuse_lseek_args *args,
+				    struct file *file, loff_t offset, int whence)
+{
+	struct fuse_file *fuse_file = file->private_data;
+
+	args->in = (struct fuse_lseek_in) {
+		.fh = fuse_file->fh,
+		.offset = offset,
+		.whence = whence,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(file->f_inode),
+			.opcode = FUSE_LSEEK,
+		},
+		.in_numargs = 1,
+		.in_args[0].size = sizeof(args->in),
+		.in_args[0].value = &args->in,
+	};
+
+	return 0;
+}
+
+static int fuse_lseek_initialize_out(struct bpf_fuse_args *fa, struct fuse_lseek_args *args,
+				     struct file *file, loff_t offset, int whence)
+{
+	fa->out_numargs = 1;
+	fa->out_args[0].size = sizeof(args->out);
+	fa->out_args[0].value = &args->out;
+
+	return 0;
+}
+
+static int fuse_lseek_backing(struct bpf_fuse_args *fa, loff_t *out,
+			      struct file *file, loff_t offset, int whence)
+{
+	const struct fuse_lseek_in *fli = fa->in_args[0].value;
+	struct fuse_lseek_out *flo = fa->out_args[0].value;
+	struct fuse_file *fuse_file = file->private_data;
+	struct file *backing_file = fuse_file->backing_file;
+
+	/* TODO: Handle changing of the file handle */
+	if (offset == 0) {
+		if (whence == SEEK_CUR) {
+			flo->offset = file->f_pos;
+			*out = flo->offset;
+			return 0;
+		}
+
+		if (whence == SEEK_SET) {
+			flo->offset = vfs_setpos(file, 0, 0);
+			*out = flo->offset;
+			return 0;
+		}
+	}
+
+	inode_lock(file->f_inode);
+	backing_file->f_pos = file->f_pos;
+	*out = vfs_llseek(backing_file, fli->offset, fli->whence);
+	flo->offset = *out;
+	inode_unlock(file->f_inode);
+	return 0;
+}
+
+static int fuse_lseek_finalize(struct bpf_fuse_args *fa, loff_t *out,
+			       struct file *file, loff_t offset, int whence)
+{
+	struct fuse_lseek_out *flo = fa->out_args[0].value;
+
+	if (!fa->info.error_in)
+		file->f_pos = flo->offset;
+	*out = flo->offset;
+	return 0;
+}
+
+int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence)
+{
+	return bpf_fuse_backing(inode, struct fuse_lseek_args, out,
+				fuse_lseek_initialize_in, fuse_lseek_initialize_out,
+				fuse_lseek_backing, fuse_lseek_finalize,
+				file, offset, whence);
+}
+
 ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	int ret;
fs/fuse/file.c +3 -0
index 865167a80d35..9758bd1665a6 100644
@@ -2779,6 +2779,9 @@ static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
 	loff_t retval;
 	struct inode *inode = file_inode(file);
 
+	if (fuse_bpf_lseek(&retval, inode, file, offset, whence))
+		return retval;
+
 	switch (whence) {
 	case SEEK_SET:
 	case SEEK_CUR:
fs/fuse/fuse_i.h +6 -0
index 5eb357f482dc..624d0cebd287 100644
@@ -1405,11 +1405,17 @@ int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num_entries);
 
 #ifdef CONFIG_FUSE_BPF
 
+int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence);
 int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags);
 int fuse_bpf_access(int *out, struct inode *inode, int mask);
 
 #else
 
+static inline int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags)
 {
 	return 0;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#16 lore raw

RFC PATCH v3 14/37 fuse-bpf: Add support for fallocate

In reply to Daniel Rosenberg

This adds backing support for FUSE_FALLOCATE

---
 fs/fuse/backing.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/file.c    |  3 +++
 fs/fuse/fuse_i.h  |  6 +++++
 3 files changed, 69 insertions(+)
fs/fuse/backing.c +60 -0
index c4916dde48c8..ee315598bc3f 100644
@@ -333,6 +333,66 @@ ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma)
 	return ret;
 }
 
+static int fuse_file_fallocate_initialize_in(struct bpf_fuse_args *fa,
+					     struct fuse_fallocate_in *in,
+					     struct file *file, int mode, loff_t offset, loff_t length)
+{
+	struct fuse_file *ff = file->private_data;
+
+	*in = (struct fuse_fallocate_in) {
+		.fh = ff->fh,
+		.offset = offset,
+		.length = length,
+		.mode = mode,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.opcode = FUSE_FALLOCATE,
+			.nodeid = ff->nodeid,
+		},
+		.in_numargs = 1,
+		.in_args[0].size = sizeof(*in),
+		.in_args[0].value = in,
+	};
+
+	return 0;
+}
+
+static int fuse_file_fallocate_initialize_out(struct bpf_fuse_args *fa,
+					      struct fuse_fallocate_in *in,
+					      struct file *file, int mode, loff_t offset, loff_t length)
+{
+	return 0;
+}
+
+static int fuse_file_fallocate_backing(struct bpf_fuse_args *fa, int *out,
+				       struct file *file, int mode, loff_t offset, loff_t length)
+{
+	const struct fuse_fallocate_in *ffi = fa->in_args[0].value;
+	struct fuse_file *ff = file->private_data;
+
+	*out = vfs_fallocate(ff->backing_file, ffi->mode, ffi->offset,
+			     ffi->length);
+	return 0;
+}
+
+static int fuse_file_fallocate_finalize(struct bpf_fuse_args *fa, int *out,
+					struct file *file, int mode, loff_t offset, loff_t length)
+{
+	return 0;
+}
+
+int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, int mode, loff_t offset, loff_t length)
+{
+	return bpf_fuse_backing(inode, struct fuse_fallocate_in, out,
+				fuse_file_fallocate_initialize_in,
+				fuse_file_fallocate_initialize_out,
+				fuse_file_fallocate_backing,
+				fuse_file_fallocate_finalize,
+				file, mode, offset, length);
+}
+
 /*******************************************************************************
  * Directory operations after here                                             *
  ******************************************************************************/
fs/fuse/file.c +3 -0
index 9758bd1665a6..58cff04660db 100644
@@ -3071,6 +3071,9 @@ static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
 		(!(mode & FALLOC_FL_KEEP_SIZE) ||
 		 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)));
 
+	if (fuse_bpf_file_fallocate(&err, inode, file, mode, offset, length))
+		return err;
+
 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
 		     FALLOC_FL_ZERO_RANGE))
 		return -EOPNOTSUPP;
fs/fuse/fuse_i.h +6 -0
index 624d0cebd287..1dd9cc9720df 100644
@@ -1406,6 +1406,7 @@ int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num_entries);
 #ifdef CONFIG_FUSE_BPF
 
 int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence);
+int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, int mode, loff_t offset, loff_t length);
 int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags);
 int fuse_bpf_access(int *out, struct inode *inode, int mask);
 
@@ -1416,6 +1417,11 @@ static inline int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *
 	return 0;
 }
 
+static inline int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, int mode, loff_t offset, loff_t length)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags)
 {
 	return 0;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#17 lore raw

RFC PATCH v3 15/37 fuse-bpf: Support file/dir open/close

In reply to Daniel Rosenberg

This adds backing support for FUSE_OPEN, FUSE_OPENDIR, FUSE_CREATE, FUSE_RELEASE, and FUSE_RELEASEDIR

---
 fs/fuse/backing.c | 368 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dir.c     |   8 +
 fs/fuse/file.c    |   7 +
 fs/fuse/fuse_i.h  |  26 ++++
 4 files changed, 409 insertions(+)
fs/fuse/backing.c +368 -0
index ee315598bc3f..d4a214cadc15 100644
@@ -207,6 +207,374 @@ static void fuse_stat_to_attr(struct fuse_conn *fc, struct inode *inode,
 	attr->blksize = 1 << blkbits;
 }
 
+struct fuse_open_args {
+	struct fuse_open_in in;
+	struct fuse_open_out out;
+};
+
+static int fuse_open_initialize_in(struct bpf_fuse_args *fa, struct fuse_open_args *args,
+				   struct inode *inode, struct file *file, bool isdir)
+{
+	args->in = (struct fuse_open_in) {
+		.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY),
+	};
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_fuse_inode(inode)->nodeid,
+			.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN,
+		},
+		.in_numargs = 1,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_open_initialize_out(struct bpf_fuse_args *fa, struct fuse_open_args *args,
+				    struct inode *inode, struct file *file, bool isdir)
+{
+	args->out = (struct fuse_open_out) { 0 };
+
+	fa->out_numargs = 1;
+	fa->out_args[0] = (struct bpf_fuse_arg) {
+		.size = sizeof(args->out),
+		.value = &args->out,
+	};
+
+	return 0;
+}
+
+static int fuse_open_backing(struct bpf_fuse_args *fa, int *out,
+			     struct inode *inode, struct file *file, bool isdir)
+{
+	struct fuse_mount *fm = get_fuse_mount(inode);
+	const struct fuse_open_in *foi = fa->in_args[0].value;
+	struct fuse_file *ff;
+	int mask;
+	struct fuse_dentry *fd = get_fuse_dentry(file->f_path.dentry);
+	struct file *backing_file;
+
+	ff = fuse_file_alloc(fm);
+	if (!ff)
+		return -ENOMEM;
+	file->private_data = ff;
+
+	switch (foi->flags & O_ACCMODE) {
+	case O_RDONLY:
+		mask = MAY_READ;
+		break;
+
+	case O_WRONLY:
+		mask = MAY_WRITE;
+		break;
+
+	case O_RDWR:
+		mask = MAY_READ | MAY_WRITE;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	*out = inode_permission(&nop_mnt_idmap,
+				get_fuse_inode(inode)->backing_inode, mask);
+	if (*out)
+		return *out;
+
+	backing_file =
+		dentry_open(&fd->backing_path, foi->flags, current_cred());
+
+	if (IS_ERR(backing_file)) {
+		fuse_file_free(ff);
+		file->private_data = NULL;
+		return PTR_ERR(backing_file);
+	}
+	ff->backing_file = backing_file;
+
+	*out = 0;
+	return 0;
+}
+
+static int fuse_open_finalize(struct bpf_fuse_args *fa, int *out,
+			      struct inode *inode, struct file *file, bool isdir)
+{
+	struct fuse_file *ff = file->private_data;
+	struct fuse_open_out *foo = fa->out_args[0].value;
+
+	if (ff) {
+		ff->fh = foo->fh;
+		ff->nodeid = get_fuse_inode(inode)->nodeid;
+	}
+	return 0;
+}
+
+int fuse_bpf_open(int *out, struct inode *inode, struct file *file, bool isdir)
+{
+	return bpf_fuse_backing(inode, struct fuse_open_args, out,
+				fuse_open_initialize_in, fuse_open_initialize_out,
+				fuse_open_backing, fuse_open_finalize,
+				inode, file, isdir);
+}
+
+struct fuse_create_open_args {
+	struct fuse_create_in in;
+	struct fuse_buffer name;
+	struct fuse_entry_out entry_out;
+	struct fuse_open_out open_out;
+};
+
+static int fuse_create_open_initialize_in(struct bpf_fuse_args *fa, struct fuse_create_open_args *args,
+					  struct inode *dir, struct dentry *entry,
+					  struct file *file, unsigned int flags, umode_t mode)
+{
+	args->in = (struct fuse_create_in) {
+		.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY),
+		.mode = mode,
+	};
+
+	args->name = (struct fuse_buffer) {
+		.data = (void *) entry->d_name.name,
+		.size = entry->d_name.len + 1,
+		.flags = BPF_FUSE_IMMUTABLE,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(dir),
+			.opcode = FUSE_CREATE,
+		},
+		.in_numargs = 2,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+		.in_args[1] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->name,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_create_open_initialize_out(struct bpf_fuse_args *fa, struct fuse_create_open_args *args,
+					   struct inode *dir, struct dentry *entry,
+					   struct file *file, unsigned int flags, umode_t mode)
+{
+	args->entry_out = (struct fuse_entry_out) { 0 };
+	args->open_out = (struct fuse_open_out) { 0 };
+
+	fa->out_numargs = 2;
+	fa->out_args[0] = (struct bpf_fuse_arg) {
+		.size = sizeof(args->entry_out),
+		.value = &args->entry_out,
+	};
+	fa->out_args[1] = (struct bpf_fuse_arg) {
+		.size = sizeof(args->open_out),
+		.value = &args->open_out,
+	};
+
+	return 0;
+}
+
+static int fuse_open_file_backing(struct inode *inode, struct file *file)
+{
+	struct fuse_mount *fm = get_fuse_mount(inode);
+	struct dentry *entry = file->f_path.dentry;
+	struct fuse_dentry *fuse_dentry = get_fuse_dentry(entry);
+	struct fuse_file *fuse_file;
+	struct file *backing_file;
+
+	fuse_file = fuse_file_alloc(fm);
+	if (!fuse_file)
+		return -ENOMEM;
+	file->private_data = fuse_file;
+
+	backing_file = dentry_open(&fuse_dentry->backing_path, file->f_flags,
+				   current_cred());
+	if (IS_ERR(backing_file)) {
+		fuse_file_free(fuse_file);
+		file->private_data = NULL;
+		return PTR_ERR(backing_file);
+	}
+	fuse_file->backing_file = backing_file;
+
+	return 0;
+}
+
+static int fuse_create_open_backing(struct bpf_fuse_args *fa, int *out,
+				    struct inode *dir, struct dentry *entry,
+				    struct file *file, unsigned int flags, umode_t mode)
+{
+	struct fuse_inode *dir_fuse_inode = get_fuse_inode(dir);
+	struct path backing_path;
+	struct inode *inode = NULL;
+	struct dentry *backing_parent;
+	struct dentry *newent;
+	const struct fuse_create_in *fci = fa->in_args[0].value;
+
+	get_fuse_backing_path(entry, &backing_path);
+	if (!backing_path.dentry)
+		return -EBADF;
+
+	if (IS_ERR(backing_path.dentry))
+		return PTR_ERR(backing_path.dentry);
+
+	if (d_really_is_positive(backing_path.dentry)) {
+		*out = -EIO;
+		goto out;
+	}
+
+	backing_parent = dget_parent(backing_path.dentry);
+	inode_lock_nested(dir_fuse_inode->backing_inode, I_MUTEX_PARENT);
+	*out = vfs_create(&nop_mnt_idmap, d_inode(backing_parent),
+			backing_path.dentry, fci->mode, true);
+	inode_unlock(d_inode(backing_parent));
+	dput(backing_parent);
+	if (*out)
+		goto out;
+
+	inode = fuse_iget_backing(dir->i_sb, 0, backing_path.dentry->d_inode);
+	if (IS_ERR(inode)) {
+		*out = PTR_ERR(inode);
+		goto out;
+	}
+
+	newent = d_splice_alias(inode, entry);
+	if (IS_ERR(newent)) {
+		*out = PTR_ERR(newent);
+		goto out;
+	}
+
+	entry = newent ? newent : entry;
+	*out = finish_open(file, entry, fuse_open_file_backing);
+
+out:
+	path_put(&backing_path);
+	return *out;
+}
+
+static int fuse_create_open_finalize(struct bpf_fuse_args *fa, int *out,
+				     struct inode *dir, struct dentry *entry,
+				     struct file *file, unsigned int flags, umode_t mode)
+{
+	struct fuse_file *ff = file->private_data;
+	struct fuse_inode *fi = get_fuse_inode(file->f_inode);
+	struct fuse_entry_out *feo = fa->out_args[0].value;
+	struct fuse_open_out *foo = fa->out_args[1].value;
+
+	if (fi)
+		fi->nodeid = feo->nodeid;
+	if (ff)
+		ff->fh = foo->fh;
+	return 0;
+}
+
+int fuse_bpf_create_open(int *out, struct inode *dir, struct dentry *entry,
+			 struct file *file, unsigned int flags, umode_t mode)
+{
+	return bpf_fuse_backing(dir, struct fuse_create_open_args, out,
+				fuse_create_open_initialize_in,
+				fuse_create_open_initialize_out,
+				fuse_create_open_backing,
+				fuse_create_open_finalize,
+				dir, entry, file, flags, mode);
+}
+
+static int fuse_release_initialize_in(struct bpf_fuse_args *fa, struct fuse_release_in *fri,
+				      struct inode *inode, struct file *file)
+{
+	struct fuse_file *fuse_file = file->private_data;
+
+	/* Always put backing file whatever bpf/userspace says */
+	fput(fuse_file->backing_file);
+
+	*fri = (struct fuse_release_in) {
+		.fh = ((struct fuse_file *)(file->private_data))->fh,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_fuse_inode(inode)->nodeid,
+			.opcode = FUSE_RELEASE,
+		},		.in_numargs = 1,
+		.in_args[0].size = sizeof(*fri),
+		.in_args[0].value = fri,
+	};
+
+	return 0;
+}
+
+static int fuse_release_initialize_out(struct bpf_fuse_args *fa, struct fuse_release_in *fri,
+				       struct inode *inode, struct file *file)
+{
+	return 0;
+}
+
+static int fuse_releasedir_initialize_in(struct bpf_fuse_args *fa,
+					 struct fuse_release_in *fri,
+					 struct inode *inode, struct file *file)
+{
+	struct fuse_file *fuse_file = file->private_data;
+
+	/* Always put backing file whatever bpf/userspace says */
+	fput(fuse_file->backing_file);
+
+	*fri = (struct fuse_release_in) {
+		.fh = ((struct fuse_file *)(file->private_data))->fh,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_fuse_inode(inode)->nodeid,
+			.opcode = FUSE_RELEASEDIR,
+		},		.in_numargs = 1,
+		.in_args[0].size = sizeof(*fri),
+		.in_args[0].value = fri,
+	};
+
+	return 0;
+}
+
+static int fuse_releasedir_initialize_out(struct bpf_fuse_args *fa,
+					  struct fuse_release_in *fri,
+					  struct inode *inode, struct file *file)
+{
+	return 0;
+}
+
+static int fuse_release_backing(struct bpf_fuse_args *fa, int *out,
+				struct inode *inode, struct file *file)
+{
+	return 0;
+}
+
+static int fuse_release_finalize(struct bpf_fuse_args *fa, int *out,
+				 struct inode *inode, struct file *file)
+{
+	fuse_file_free(file->private_data);
+	*out = 0;
+	return 0;
+}
+
+int fuse_bpf_release(int *out, struct inode *inode, struct file *file)
+{
+	return bpf_fuse_backing(inode, struct fuse_release_in, out,
+				fuse_release_initialize_in, fuse_release_initialize_out,
+				fuse_release_backing, fuse_release_finalize,
+				inode, file);
+}
+
+int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file)
+{
+	return bpf_fuse_backing(inode, struct fuse_release_in, out,
+				fuse_releasedir_initialize_in, fuse_releasedir_initialize_out,
+				fuse_release_backing, fuse_release_finalize, inode, file);
+}
+
 struct fuse_lseek_args {
 	struct fuse_lseek_in in;
 	struct fuse_lseek_out out;
fs/fuse/dir.c +8 -0
index 535e6cf9e970..1df2bbc72396 100644
@@ -719,6 +719,9 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry,
 	/* Userspace expects S_IFREG in create mode */
 	BUG_ON((mode & S_IFMT) != S_IFREG);
 
+	if (fuse_bpf_create_open(&err, dir, entry, file, flags, mode))
+		return err;
+
 	forget = fuse_alloc_forget();
 	err = -ENOMEM;
 	if (!forget)
@@ -1629,6 +1632,11 @@ static int fuse_dir_open(struct inode *inode, struct file *file)
 
 static int fuse_dir_release(struct inode *inode, struct file *file)
 {
+	int err = 0;
+
+	if (fuse_bpf_releasedir(&err, inode, file))
+		return err;
+
 	fuse_release_common(file, true);
 
 	return 0;
fs/fuse/file.c +7 -0
index 58cff04660db..1836d09d9ce3 100644
@@ -243,6 +243,9 @@ int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
 	if (err)
 		return err;
 
+	if (fuse_bpf_open(&err, inode, file, isdir))
+		return err;
+
 	if (is_wb_truncate || dax_truncate)
 		inode_lock(inode);
 
@@ -351,6 +354,10 @@ static int fuse_open(struct inode *inode, struct file *file)
 static int fuse_release(struct inode *inode, struct file *file)
 {
 	struct fuse_conn *fc = get_fuse_conn(inode);
+	int err;
+
+	if (fuse_bpf_release(&err, inode, file))
+		return err;
 
 	/*
 	 * Dirty pages might remain despite write_inode_now() call from
fs/fuse/fuse_i.h +26 -0
index 1dd9cc9720df..feecc1ebfdda 100644
@@ -1405,6 +1405,11 @@ int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num_entries);
 
 #ifdef CONFIG_FUSE_BPF
 
+int fuse_bpf_open(int *err, struct inode *inode, struct file *file, bool isdir);
+int fuse_bpf_create_open(int *out, struct inode *dir, struct dentry *entry,
+			 struct file *file, unsigned int flags, umode_t mode);
+int fuse_bpf_release(int *out, struct inode *inode, struct file *file);
+int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence);
 int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, int mode, loff_t offset, loff_t length);
 int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags);
@@ -1412,6 +1417,27 @@ int fuse_bpf_access(int *out, struct inode *inode, int mask);
 
 #else
 
+static inline int fuse_bpf_open(int *err, struct inode *inode, struct file *file, bool isdir)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_create_open(int *out, struct inode *dir, struct dentry *entry,
+				       struct file *file, unsigned int flags, umode_t mode)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_release(int *out, struct inode *inode, struct file *file)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence)
 {
 	return 0;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#18 lore raw

RFC PATCH v3 16/37 fuse-bpf: Support mknod/unlink/mkdir/rmdir

In reply to Daniel Rosenberg

This adds backing support for FUSE_MKNOD, FUSE_MKDIR, FUSE_RMDIR, and FUSE_UNLINK

---
 fs/fuse/backing.c | 342 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dir.c     |  14 ++
 fs/fuse/fuse_i.h  |  24 ++++
 3 files changed, 380 insertions(+)
fs/fuse/backing.c +342 -0
index d4a214cadc15..c6ef10aeec15 100644
@@ -972,6 +972,348 @@ int fuse_revalidate_backing(struct dentry *entry, unsigned int flags)
 	return 1;
 }
 
+struct fuse_mknod_args {
+	struct fuse_mknod_in in;
+	struct fuse_buffer name;
+};
+
+static int fuse_mknod_initialize_in(struct bpf_fuse_args *fa, struct fuse_mknod_args *args,
+				    struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev)
+{
+	*args = (struct fuse_mknod_args) {
+		.in = (struct fuse_mknod_in) {
+			.mode = mode,
+			.rdev = new_encode_dev(rdev),
+			.umask = current_umask(),
+		},
+		.name = (struct fuse_buffer) {
+			.data = (void *) entry->d_name.name,
+			.size = entry->d_name.len + 1,
+			.flags = BPF_FUSE_IMMUTABLE,
+		},
+	};
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(dir),
+			.opcode = FUSE_MKNOD,
+		},
+		.in_numargs = 2,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+		.in_args[1] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->name,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_mknod_initialize_out(struct bpf_fuse_args *fa, struct fuse_mknod_args *args,
+				     struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev)
+{
+	return 0;
+}
+
+static int fuse_mknod_backing(struct bpf_fuse_args *fa, int *out,
+			      struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev)
+{
+	const struct fuse_mknod_in *fmi = fa->in_args[0].value;
+	struct fuse_inode *fuse_inode = get_fuse_inode(dir);
+	struct inode *backing_inode = fuse_inode->backing_inode;
+	struct path backing_path;
+	struct inode *inode = NULL;
+
+	get_fuse_backing_path(entry, &backing_path);
+	if (!backing_path.dentry)
+		return -EBADF;
+
+	inode_lock_nested(backing_inode, I_MUTEX_PARENT);
+	mode = fmi->mode;
+	if (!IS_POSIXACL(backing_inode))
+		mode &= ~fmi->umask;
+	*out = vfs_mknod(&nop_mnt_idmap, backing_inode, backing_path.dentry, mode,
+			new_decode_dev(fmi->rdev));
+	inode_unlock(backing_inode);
+	if (*out)
+		goto out;
+	if (d_really_is_negative(backing_path.dentry) ||
+	    unlikely(d_unhashed(backing_path.dentry))) {
+		*out = -EINVAL;
+		/**
+		 * TODO: overlayfs responds to this situation with a
+		 * lookupOneLen. Should we do that too?
+		 */
+		goto out;
+	}
+	inode = fuse_iget_backing(dir->i_sb, fuse_inode->nodeid, backing_inode);
+	if (IS_ERR(inode)) {
+		*out = PTR_ERR(inode);
+		goto out;
+	}
+	d_instantiate(entry, inode);
+out:
+	path_put(&backing_path);
+	return *out;
+}
+
+static int fuse_mknod_finalize(struct bpf_fuse_args *fa, int *out,
+			       struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev)
+{
+	return 0;
+}
+
+int fuse_bpf_mknod(int *out, struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev)
+{
+	return bpf_fuse_backing(dir, struct fuse_mknod_args, out,
+				fuse_mknod_initialize_in, fuse_mknod_initialize_out,
+				fuse_mknod_backing, fuse_mknod_finalize,
+				dir, entry, mode, rdev);
+}
+
+struct fuse_mkdir_args {
+	struct fuse_mkdir_in in;
+	struct fuse_buffer name;
+};
+
+static int fuse_mkdir_initialize_in(struct bpf_fuse_args *fa, struct fuse_mkdir_args *args,
+				    struct inode *dir, struct dentry *entry, umode_t mode)
+{
+	*args = (struct fuse_mkdir_args) {
+		.in = (struct fuse_mkdir_in) {
+			.mode = mode,
+			.umask = current_umask(),
+		},
+		.name = (struct fuse_buffer) {
+			.data = (void *) entry->d_name.name,
+			.size = entry->d_name.len + 1,
+			.flags = BPF_FUSE_IMMUTABLE,
+		},
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(dir),
+			.opcode = FUSE_MKDIR,
+		},
+		.in_numargs = 2,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+		.in_args[1] = (struct bpf_fuse_arg) {
+			.value = &args->name,
+			.is_buffer = true,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_mkdir_initialize_out(struct bpf_fuse_args *fa, struct fuse_mkdir_args *args,
+				     struct inode *dir, struct dentry *entry, umode_t mode)
+{
+	return 0;
+}
+
+static int fuse_mkdir_backing(struct bpf_fuse_args *fa, int *out,
+			      struct inode *dir, struct dentry *entry, umode_t mode)
+{
+	const struct fuse_mkdir_in *fmi = fa->in_args[0].value;
+	struct fuse_inode *fuse_inode = get_fuse_inode(dir);
+	struct inode *backing_inode = fuse_inode->backing_inode;
+	struct path backing_path;
+	struct inode *inode = NULL;
+	struct dentry *d;
+
+	get_fuse_backing_path(entry, &backing_path);
+	if (!backing_path.dentry)
+		return -EBADF;
+
+	inode_lock_nested(backing_inode, I_MUTEX_PARENT);
+	mode = fmi->mode;
+	if (!IS_POSIXACL(backing_inode))
+		mode &= ~fmi->umask;
+	*out = vfs_mkdir(&nop_mnt_idmap, backing_inode, backing_path.dentry,
+			mode);
+	if (*out)
+		goto out;
+	if (d_really_is_negative(backing_path.dentry) ||
+	    unlikely(d_unhashed(backing_path.dentry))) {
+		d = lookup_one_len(entry->d_name.name,
+				   backing_path.dentry->d_parent,
+				   entry->d_name.len);
+		if (IS_ERR(d)) {
+			*out = PTR_ERR(d);
+			goto out;
+		}
+		dput(backing_path.dentry);
+		backing_path.dentry = d;
+	}
+	inode = fuse_iget_backing(dir->i_sb, fuse_inode->nodeid, backing_inode);
+	if (IS_ERR(inode)) {
+		*out = PTR_ERR(inode);
+		goto out;
+	}
+	d_instantiate(entry, inode);
+out:
+	inode_unlock(backing_inode);
+	path_put(&backing_path);
+	return *out;
+}
+
+static int fuse_mkdir_finalize(struct bpf_fuse_args *fa, int *out,
+			       struct inode *dir, struct dentry *entry, umode_t mode)
+{
+	return 0;
+}
+
+int fuse_bpf_mkdir(int *out, struct inode *dir, struct dentry *entry, umode_t mode)
+{
+	return bpf_fuse_backing(dir, struct fuse_mkdir_args, out,
+				fuse_mkdir_initialize_in, fuse_mkdir_initialize_out,
+				fuse_mkdir_backing, fuse_mkdir_finalize,
+				dir, entry, mode);
+}
+
+static int fuse_rmdir_initialize_in(struct bpf_fuse_args *fa, struct fuse_buffer *name,
+				    struct inode *dir, struct dentry *entry)
+{
+	*name = (struct fuse_buffer) {
+		.data = (void *) entry->d_name.name,
+		.size = entry->d_name.len + 1,
+		.flags = BPF_FUSE_IMMUTABLE,
+	};
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(dir),
+			.opcode = FUSE_RMDIR,
+		},
+		.in_numargs = 1,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = name,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_rmdir_initialize_out(struct bpf_fuse_args *fa, struct fuse_buffer *name,
+				     struct inode *dir, struct dentry *entry)
+{
+	return 0;
+}
+
+static int fuse_rmdir_backing(struct bpf_fuse_args *fa, int *out,
+			      struct inode *dir, struct dentry *entry)
+{
+	struct path backing_path;
+	struct dentry *backing_parent_dentry;
+	struct inode *backing_inode;
+
+	get_fuse_backing_path(entry, &backing_path);
+	if (!backing_path.dentry)
+		return -EBADF;
+
+	backing_parent_dentry = dget_parent(backing_path.dentry);
+	backing_inode = d_inode(backing_parent_dentry);
+
+	inode_lock_nested(backing_inode, I_MUTEX_PARENT);
+	*out = vfs_rmdir(&nop_mnt_idmap, backing_inode, backing_path.dentry);
+	inode_unlock(backing_inode);
+
+	dput(backing_parent_dentry);
+	if (!*out)
+		d_drop(entry);
+	path_put(&backing_path);
+	return *out;
+}
+
+static int fuse_rmdir_finalize(struct bpf_fuse_args *fa, int *out, struct inode *dir, struct dentry *entry)
+{
+	return 0;
+}
+
+int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *entry)
+{
+	return bpf_fuse_backing(dir, struct fuse_buffer, out,
+				fuse_rmdir_initialize_in, fuse_rmdir_initialize_out,
+				fuse_rmdir_backing, fuse_rmdir_finalize,
+				dir, entry);
+}
+
+static int fuse_unlink_initialize_in(struct bpf_fuse_args *fa, struct fuse_buffer *name,
+				     struct inode *dir, struct dentry *entry)
+{
+	*name = (struct fuse_buffer) {
+		.data = (void *) entry->d_name.name,
+		.size = entry->d_name.len + 1,
+		.flags = BPF_FUSE_IMMUTABLE,
+	};
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(dir),
+			.opcode = FUSE_UNLINK,
+		},
+		.in_numargs = 1,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = name,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_unlink_initialize_out(struct bpf_fuse_args *fa, struct fuse_buffer *name,
+				      struct inode *dir, struct dentry *entry)
+{
+	return 0;
+}
+
+static int fuse_unlink_backing(struct bpf_fuse_args *fa, int *out, struct inode *dir, struct dentry *entry)
+{
+	struct path backing_path;
+	struct dentry *backing_parent_dentry;
+	struct inode *backing_inode;
+
+	get_fuse_backing_path(entry, &backing_path);
+	if (!backing_path.dentry)
+		return -EBADF;
+
+	/* TODO Not sure if we should reverify like overlayfs, or get inode from d_parent */
+	backing_parent_dentry = dget_parent(backing_path.dentry);
+	backing_inode = d_inode(backing_parent_dentry);
+
+	inode_lock_nested(backing_inode, I_MUTEX_PARENT);
+	*out = vfs_unlink(&nop_mnt_idmap, backing_inode, backing_path.dentry,
+			 NULL);
+	inode_unlock(backing_inode);
+
+	dput(backing_parent_dentry);
+	if (!*out)
+		d_drop(entry);
+	path_put(&backing_path);
+	return *out;
+}
+
+static int fuse_unlink_finalize(struct bpf_fuse_args *fa, int *out,
+				struct inode *dir, struct dentry *entry)
+{
+	return 0;
+}
+
+int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry)
+{
+	return bpf_fuse_backing(dir, struct fuse_buffer, out,
+				fuse_unlink_initialize_in, fuse_unlink_initialize_out,
+				fuse_unlink_backing, fuse_unlink_finalize,
+				dir, entry);
+}
+
 static int fuse_access_initialize_in(struct bpf_fuse_args *fa, struct fuse_access_in *in,
 				     struct inode *inode, int mask)
 {
fs/fuse/dir.c +14 -0
index 1df2bbc72396..a763a45fa973 100644
@@ -937,6 +937,10 @@ static int fuse_mknod(struct mnt_idmap *idmap, struct inode *dir,
 	struct fuse_mknod_in inarg;
 	struct fuse_mount *fm = get_fuse_mount(dir);
 	FUSE_ARGS(args);
+	int err;
+
+	if (fuse_bpf_mknod(&err, dir, entry, mode, rdev))
+		return err;
 
 	if (!fm->fc->dont_mask)
 		mode &= ~current_umask();
@@ -983,6 +987,10 @@ static int fuse_mkdir(struct mnt_idmap *idmap, struct inode *dir,
 	struct fuse_mkdir_in inarg;
 	struct fuse_mount *fm = get_fuse_mount(dir);
 	FUSE_ARGS(args);
+	int err;
+
+	if (fuse_bpf_mkdir(&err, dir, entry, mode))
+		return err;
 
 	if (!fm->fc->dont_mask)
 		mode &= ~current_umask();
@@ -1069,6 +1077,9 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)
 	if (fuse_is_bad(dir))
 		return -EIO;
 
+	if (fuse_bpf_unlink(&err, dir, entry))
+		return err;
+
 	args.opcode = FUSE_UNLINK;
 	args.nodeid = get_node_id(dir);
 	args.in_numargs = 1;
@@ -1092,6 +1103,9 @@ static int fuse_rmdir(struct inode *dir, struct dentry *entry)
 	if (fuse_is_bad(dir))
 		return -EIO;
 
+	if (fuse_bpf_rmdir(&err, dir, entry))
+		return err;
+
 	args.opcode = FUSE_RMDIR;
 	args.nodeid = get_node_id(dir);
 	args.in_numargs = 1;
fs/fuse/fuse_i.h +24 -0
index feecc1ebfdda..2cbe232c1048 100644
@@ -1408,6 +1408,10 @@ int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num_entries);
 int fuse_bpf_open(int *err, struct inode *inode, struct file *file, bool isdir);
 int fuse_bpf_create_open(int *out, struct inode *dir, struct dentry *entry,
 			 struct file *file, unsigned int flags, umode_t mode);
+int fuse_bpf_mknod(int *out, struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
+int fuse_bpf_mkdir(int *out, struct inode *dir, struct dentry *entry, umode_t mode);
+int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *entry);
+int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry);
 int fuse_bpf_release(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence);
@@ -1428,6 +1432,26 @@ static inline int fuse_bpf_create_open(int *out, struct inode *dir, struct dentr
 	return 0;
 }
 
+static inline int fuse_bpf_mknod(int *out, struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_mkdir(int *out, struct inode *dir, struct dentry *entry, umode_t mode)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *entry)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_release(int *out, struct inode *inode, struct file *file)
 {
 	return 0;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#19 lore raw

RFC PATCH v3 17/37 fuse-bpf: Add support for read/write iter

In reply to Daniel Rosenberg

Adds backing support for FUSE_READ and FUSE_WRITE

This includes adjustments from Amir Goldstein's patch to FUSE Passthrough

---
 fs/fuse/backing.c         | 371 ++++++++++++++++++++++++++++++++++++++
 fs/fuse/control.c         |   2 +-
 fs/fuse/file.c            |   8 +
 fs/fuse/fuse_i.h          |  19 +-
 fs/fuse/inode.c           |  13 ++
 include/uapi/linux/fuse.h |  10 +
 6 files changed, 421 insertions(+), 2 deletions(-)
fs/fuse/backing.c +371 -0
index c6ef10aeec15..c7709a880e9c 100644
@@ -11,6 +11,7 @@
 #include <linux/file.h>
 #include <linux/fs_stack.h>
 #include <linux/namei.h>
+#include <linux/uio.h>
 
 /*
  * expression statement to wrap the backing filter logic
@@ -76,6 +77,89 @@
 	handled;							\
 })
 
+#define FUSE_BPF_IOCB_MASK (IOCB_APPEND | IOCB_DSYNC | IOCB_HIPRI | IOCB_NOWAIT | IOCB_SYNC)
+
+struct fuse_bpf_aio_req {
+	struct kiocb iocb;
+	refcount_t ref;
+	struct kiocb *iocb_orig;
+	struct timespec64 pre_atime;
+};
+
+static struct kmem_cache *fuse_bpf_aio_request_cachep;
+
+static void fuse_file_accessed(struct file *dst_file, struct file *src_file)
+{
+	struct inode *dst_inode;
+	struct inode *src_inode;
+
+	if (dst_file->f_flags & O_NOATIME)
+		return;
+
+	dst_inode = file_inode(dst_file);
+	src_inode = file_inode(src_file);
+
+	if ((!timespec64_equal(&dst_inode->i_mtime, &src_inode->i_mtime) ||
+	     !timespec64_equal(&dst_inode->i_ctime, &src_inode->i_ctime))) {
+		dst_inode->i_mtime = src_inode->i_mtime;
+		dst_inode->i_ctime = src_inode->i_ctime;
+	}
+
+	touch_atime(&dst_file->f_path);
+}
+
+static void fuse_copyattr(struct file *dst_file, struct file *src_file)
+{
+	struct inode *dst = file_inode(dst_file);
+	struct inode *src = file_inode(src_file);
+
+	dst->i_atime = src->i_atime;
+	dst->i_mtime = src->i_mtime;
+	dst->i_ctime = src->i_ctime;
+	i_size_write(dst, i_size_read(src));
+	fuse_invalidate_attr(dst);
+}
+
+static void fuse_file_start_write(struct file *fuse_file, struct file *backing_file,
+				  loff_t pos, size_t count)
+{
+	struct inode *inode = file_inode(fuse_file);
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
+	if (inode->i_size < pos + count)
+		set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
+
+	file_start_write(backing_file);
+}
+
+static void fuse_file_end_write(struct file *fuse_file, struct file *backing_file,
+				loff_t pos, size_t res)
+{
+	struct inode *inode = file_inode(fuse_file);
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
+	file_end_write(backing_file);
+
+	if (res > 0)
+		fuse_write_update_attr(inode, pos, res);
+
+	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
+	fuse_invalidate_attr(inode);
+}
+
+static void fuse_file_start_read(struct file *backing_file, struct timespec64 *pre_atime)
+{
+	*pre_atime = file_inode(backing_file)->i_atime;
+}
+
+static void fuse_file_end_read(struct file *fuse_file, struct file *backing_file,
+			  struct timespec64 *pre_atime)
+{
+	/* Mimic atime update policy of passthrough inode, not the value */
+	if (!timespec64_equal(&file_inode(backing_file)->i_atime, pre_atime))
+		fuse_invalidate_atime(file_inode(fuse_file));
+}
+
 static void fuse_get_backing_path(struct file *file, struct path *path)
 {
 	path_get(&file->f_path);
@@ -664,6 +748,277 @@ int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t o
 				file, offset, whence);
 }
 
+static inline void fuse_bpf_aio_put(struct fuse_bpf_aio_req *aio_req)
+{
+	if (refcount_dec_and_test(&aio_req->ref))
+		kmem_cache_free(fuse_bpf_aio_request_cachep, aio_req);
+}
+
+static void fuse_bpf_aio_cleanup_handler(struct fuse_bpf_aio_req *aio_req, long res)
+{
+	struct kiocb *iocb = &aio_req->iocb;
+	struct kiocb *iocb_orig = aio_req->iocb_orig;
+	struct file *filp = iocb->ki_filp;
+	struct file *fuse_filp = iocb_orig->ki_filp;
+
+	if (iocb->ki_flags & IOCB_WRITE) {
+		__sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
+				      SB_FREEZE_WRITE);
+		fuse_file_end_write(iocb_orig->ki_filp, iocb->ki_filp, iocb->ki_pos, res);
+	} else {
+		fuse_file_end_read(fuse_filp, filp, &aio_req->pre_atime);
+	}
+	iocb_orig->ki_pos = iocb->ki_pos;
+	fuse_bpf_aio_put(aio_req);
+}
+
+static void fuse_bpf_aio_rw_complete(struct kiocb *iocb, long res)
+{
+	struct fuse_bpf_aio_req *aio_req =
+		container_of(iocb, struct fuse_bpf_aio_req, iocb);
+	struct kiocb *iocb_orig = aio_req->iocb_orig;
+
+	fuse_bpf_aio_cleanup_handler(aio_req, res);
+	iocb_orig->ki_complete(iocb_orig, res);
+}
+
+struct fuse_file_read_iter_args {
+	struct fuse_read_in in;
+	struct fuse_read_iter_out out;
+};
+
+static int fuse_file_read_iter_initialize_in(struct bpf_fuse_args *fa, struct fuse_file_read_iter_args *args,
+					     struct kiocb *iocb, struct iov_iter *to)
+{
+	struct file *file = iocb->ki_filp;
+	struct fuse_file *ff = file->private_data;
+
+	args->in = (struct fuse_read_in) {
+		.fh = ff->fh,
+		.offset = iocb->ki_pos,
+		.size = to->count,
+	};
+
+	/* TODO we can't assume 'to' is a kvec */
+	/* TODO we also can't assume the vector has only one component */
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.opcode = FUSE_READ,
+			.nodeid = ff->nodeid,
+		},		.in_numargs = 1,
+		.in_args[0].size = sizeof(args->in),
+		.in_args[0].value = &args->in,
+		/*
+		 * TODO Design this properly.
+		 * Possible approach: do not pass buf to bpf
+		 * If going to userland, do a deep copy
+		 * For extra credit, do that to/from the vector, rather than
+		 * making an extra copy in the kernel
+		 */
+	};
+
+	return 0;
+}
+
+static int fuse_file_read_iter_initialize_out(struct bpf_fuse_args *fa, struct fuse_file_read_iter_args *args,
+					      struct kiocb *iocb, struct iov_iter *to)
+{
+	args->out = (struct fuse_read_iter_out) {
+		.ret = args->in.size,
+	};
+
+	fa->out_numargs = 1;
+	fa->out_args[0].size = sizeof(args->out);
+	fa->out_args[0].value = &args->out;
+
+	return 0;
+}
+
+static int fuse_file_read_iter_backing(struct bpf_fuse_args *fa, ssize_t *out,
+				       struct kiocb *iocb, struct iov_iter *to)
+{
+	struct fuse_read_iter_out *frio = fa->out_args[0].value;
+	struct file *file = iocb->ki_filp;
+	struct fuse_file *ff = file->private_data;
+
+	if (!iov_iter_count(to))
+		return 0;
+
+	if ((iocb->ki_flags & IOCB_DIRECT) &&
+	    (!ff->backing_file->f_mapping->a_ops ||
+	     !ff->backing_file->f_mapping->a_ops->direct_IO))
+		return -EINVAL;
+
+	/* TODO This just plain ignores any change to fuse_read_in */
+	if (is_sync_kiocb(iocb)) {
+		struct timespec64 pre_atime;
+
+		fuse_file_start_read(ff->backing_file, &pre_atime);
+		*out = vfs_iter_read(ff->backing_file, to, &iocb->ki_pos,
+				iocb_to_rw_flags(iocb->ki_flags, FUSE_BPF_IOCB_MASK));
+		fuse_file_end_read(file, ff->backing_file, &pre_atime);
+	} else {
+		struct fuse_bpf_aio_req *aio_req;
+
+		*out = -ENOMEM;
+		aio_req = kmem_cache_zalloc(fuse_bpf_aio_request_cachep, GFP_KERNEL);
+		if (!aio_req)
+			goto out;
+
+		aio_req->iocb_orig = iocb;
+		fuse_file_start_read(ff->backing_file, &aio_req->pre_atime);
+		kiocb_clone(&aio_req->iocb, iocb, ff->backing_file);
+		aio_req->iocb.ki_complete = fuse_bpf_aio_rw_complete;
+		refcount_set(&aio_req->ref, 2);
+		*out = vfs_iocb_iter_read(ff->backing_file, &aio_req->iocb, to);
+		fuse_bpf_aio_put(aio_req);
+		if (*out != -EIOCBQUEUED)
+			fuse_bpf_aio_cleanup_handler(aio_req, *out);
+	}
+
+	frio->ret = *out;
+
+	/* TODO Need to point value at the buffer for post-modification */
+
+out:
+	fuse_file_accessed(file, ff->backing_file);
+
+	return *out;
+}
+
+static int fuse_file_read_iter_finalize(struct bpf_fuse_args *fa, ssize_t *out,
+					struct kiocb *iocb, struct iov_iter *to)
+{
+	struct fuse_read_iter_out *frio = fa->out_args[0].value;
+
+	*out = frio->ret;
+
+	return 0;
+}
+
+int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *to)
+{
+	return bpf_fuse_backing(inode, struct fuse_file_read_iter_args, out,
+				fuse_file_read_iter_initialize_in,
+				fuse_file_read_iter_initialize_out,
+				fuse_file_read_iter_backing,
+				fuse_file_read_iter_finalize,
+				iocb, to);
+}
+
+struct fuse_file_write_iter_args {
+	struct fuse_write_in in;
+	struct fuse_write_iter_out out;
+};
+
+static int fuse_file_write_iter_initialize_in(struct bpf_fuse_args *fa,
+					      struct fuse_file_write_iter_args *args,
+					      struct kiocb *iocb, struct iov_iter *from)
+{
+	struct file *file = iocb->ki_filp;
+	struct fuse_file *ff = file->private_data;
+
+	*args = (struct fuse_file_write_iter_args) {
+		.in.fh = ff->fh,
+		.in.offset = iocb->ki_pos,
+		.in.size = from->count,
+	};
+
+	/* TODO we can't assume 'from' is a kvec */
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.opcode = FUSE_WRITE,
+			.nodeid = ff->nodeid,
+		},
+		.in_numargs = 1,
+		.in_args[0].size = sizeof(args->in),
+		.in_args[0].value = &args->in,
+	};
+
+	return 0;
+}
+
+static int fuse_file_write_iter_initialize_out(struct bpf_fuse_args *fa,
+					       struct fuse_file_write_iter_args *args,
+					       struct kiocb *iocb, struct iov_iter *from)
+{
+	/* TODO we can't assume 'from' is a kvec */
+	fa->out_numargs = 1;
+	fa->out_args[0].size = sizeof(args->out);
+	fa->out_args[0].value = &args->out;
+
+	return 0;
+}
+
+static int fuse_file_write_iter_backing(struct bpf_fuse_args *fa, ssize_t *out,
+					struct kiocb *iocb, struct iov_iter *from)
+{
+	struct file *file = iocb->ki_filp;
+	struct fuse_file *ff = file->private_data;
+	struct fuse_write_iter_out *fwio = fa->out_args[0].value;
+	ssize_t count = iov_iter_count(from);
+
+	if (!count)
+		return 0;
+
+	/* TODO This just plain ignores any change to fuse_write_in */
+	/* TODO uint32_t seems smaller than ssize_t.... right? */
+	inode_lock(file_inode(file));
+
+	fuse_copyattr(file, ff->backing_file);
+
+	if (is_sync_kiocb(iocb)) {
+		fuse_file_start_write(file, ff->backing_file, iocb->ki_pos, count);
+		*out = vfs_iter_write(ff->backing_file, from, &iocb->ki_pos,
+					   iocb_to_rw_flags(iocb->ki_flags, FUSE_BPF_IOCB_MASK));
+		fuse_file_end_write(file, ff->backing_file, iocb->ki_pos, *out);
+	} else {
+		struct fuse_bpf_aio_req *aio_req;
+
+		*out = -ENOMEM;
+		aio_req = kmem_cache_zalloc(fuse_bpf_aio_request_cachep, GFP_KERNEL);
+		if (!aio_req)
+			goto out;
+
+		fuse_file_start_write(file, ff->backing_file, iocb->ki_pos, count);
+		__sb_writers_release(file_inode(ff->backing_file)->i_sb, SB_FREEZE_WRITE);
+		aio_req->iocb_orig = iocb;
+		kiocb_clone(&aio_req->iocb, iocb, ff->backing_file);
+		aio_req->iocb.ki_complete = fuse_bpf_aio_rw_complete;
+		refcount_set(&aio_req->ref, 2);
+		*out = vfs_iocb_iter_write(ff->backing_file, &aio_req->iocb, from);
+		fuse_bpf_aio_put(aio_req);
+		if (*out != -EIOCBQUEUED)
+			fuse_bpf_aio_cleanup_handler(aio_req, *out);
+	}
+
+out:
+	inode_unlock(file_inode(file));
+	fwio->ret = *out;
+	if (*out < 0)
+		return *out;
+	return 0;
+}
+
+static int fuse_file_write_iter_finalize(struct bpf_fuse_args *fa, ssize_t *out,
+					 struct kiocb *iocb, struct iov_iter *from)
+{
+	struct fuse_write_iter_out *fwio = fa->out_args[0].value;
+
+	*out = fwio->ret;
+	return 0;
+}
+
+int fuse_bpf_file_write_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *from)
+{
+	return bpf_fuse_backing(inode, struct fuse_file_write_iter_args, out,
+				fuse_file_write_iter_initialize_in,
+				fuse_file_write_iter_initialize_out,
+				fuse_file_write_iter_backing,
+				fuse_file_write_iter_finalize,
+				iocb, from);
+}
+
 ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	int ret;
@@ -1360,3 +1715,19 @@ int fuse_bpf_access(int *out, struct inode *inode, int mask)
 				fuse_access_initialize_in, fuse_access_initialize_out,
 				fuse_access_backing, fuse_access_finalize, inode, mask);
 }
+
+int __init fuse_bpf_init(void)
+{
+	fuse_bpf_aio_request_cachep = kmem_cache_create("fuse_bpf_aio_req",
+						   sizeof(struct fuse_bpf_aio_req),
+						   0, SLAB_HWCACHE_ALIGN, NULL);
+	if (!fuse_bpf_aio_request_cachep)
+		return -ENOMEM;
+
+	return 0;
+}
+
+void __exit fuse_bpf_cleanup(void)
+{
+	kmem_cache_destroy(fuse_bpf_aio_request_cachep);
+}
fs/fuse/control.c +1 -1
index 247ef4f76761..685552453751 100644
@@ -378,7 +378,7 @@ int __init fuse_ctl_init(void)
 	return register_filesystem(&fuse_ctl_fs_type);
 }
 
-void __exit fuse_ctl_cleanup(void)
+void fuse_ctl_cleanup(void)
 {
 	unregister_filesystem(&fuse_ctl_fs_type);
 }
fs/fuse/file.c +8 -0
index 1836d09d9ce3..5f19ef5bf124 100644
@@ -1679,6 +1679,7 @@ 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;
@@ -1686,6 +1687,9 @@ static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
 	if (FUSE_IS_DAX(inode))
 		return fuse_dax_read_iter(iocb, to);
 
+	if (fuse_bpf_file_read_iter(&ret, inode, iocb, to))
+		return ret;
+
 	if (!(ff->open_flags & FOPEN_DIRECT_IO))
 		return fuse_cache_read_iter(iocb, to);
 	else
@@ -1697,6 +1701,7 @@ static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	struct file *file = iocb->ki_filp;
 	struct fuse_file *ff = file->private_data;
 	struct inode *inode = file_inode(file);
+	ssize_t ret = 0;
 
 	if (fuse_is_bad(inode))
 		return -EIO;
@@ -1704,6 +1709,9 @@ static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	if (FUSE_IS_DAX(inode))
 		return fuse_dax_write_iter(iocb, from);
 
+	if (fuse_bpf_file_write_iter(&ret, inode, iocb, from))
+		return ret;
+
 	if (!(ff->open_flags & FOPEN_DIRECT_IO))
 		return fuse_cache_write_iter(iocb, from);
 	else
fs/fuse/fuse_i.h +18 -1
index 2cbe232c1048..4bc070b81ac2 100644
@@ -1135,7 +1135,7 @@ int fuse_dev_init(void);
 void fuse_dev_cleanup(void);
 
 int fuse_ctl_init(void);
-void __exit fuse_ctl_cleanup(void);
+void fuse_ctl_cleanup(void);
 
 /**
  * Simple request sending that does request allocation and freeing
@@ -1415,6 +1415,8 @@ int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry);
 int fuse_bpf_release(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence);
+int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *to);
+int fuse_bpf_file_write_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *from);
 int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, int mode, loff_t offset, loff_t length);
 int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags);
 int fuse_bpf_access(int *out, struct inode *inode, int mask);
@@ -1467,6 +1469,16 @@ static inline int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *
 	return 0;
 }
 
+static inline int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *to)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_file_write_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *from)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, int mode, loff_t offset, loff_t length)
 {
 	return 0;
@@ -1517,4 +1529,9 @@ static inline u64 attr_timeout(struct fuse_attr_out *o)
 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
 }
 
+#ifdef CONFIG_FUSE_BPF
+int __init fuse_bpf_init(void);
+void __exit fuse_bpf_cleanup(void);
+#endif /* CONFIG_FUSE_BPF */
+
 #endif /* _FS_FUSE_I_H */
fs/fuse/inode.c +13 -0
index fe80984f099a..fd00910f1eb1 100644
@@ -2095,11 +2095,21 @@ static int __init fuse_init(void)
 	if (res)
 		goto err_sysfs_cleanup;
 
+#ifdef CONFIG_FUSE_BPF
+	res = fuse_bpf_init();
+	if (res)
+		goto err_ctl_cleanup;
+#endif
+
 	sanitize_global_limit(&max_user_bgreq);
 	sanitize_global_limit(&max_user_congthresh);
 
 	return 0;
 
+#ifdef CONFIG_FUSE_BPF
+ err_ctl_cleanup:
+	fuse_ctl_cleanup();
+#endif
  err_sysfs_cleanup:
 	fuse_sysfs_cleanup();
  err_dev_cleanup:
@@ -2117,6 +2127,9 @@ static void __exit fuse_exit(void)
 	fuse_ctl_cleanup();
 	fuse_sysfs_cleanup();
 	fuse_fs_cleanup();
+#ifdef CONFIG_FUSE_BPF
+	fuse_bpf_cleanup();
+#endif
 	fuse_dev_cleanup();
 }
 
include/uapi/linux/fuse.h +10 -0
index 3ad725a3e968..dbfc8d501bcb 100644
@@ -748,6 +748,11 @@ struct fuse_read_in {
 	uint32_t	padding;
 };
 
+// This is likely not what we want
+struct fuse_read_iter_out {
+	uint64_t ret;
+};
+
 #define FUSE_COMPAT_WRITE_IN_SIZE 24
 
 struct fuse_write_in {
@@ -765,6 +770,11 @@ struct fuse_write_out {
 	uint32_t	padding;
 };
 
+// This is likely not what we want
+struct fuse_write_iter_out {
+	uint64_t ret;
+};
+
 #define FUSE_COMPAT_STATFS_SIZE 48
 
 struct fuse_statfs_out {
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#20 lore raw

Re: [RFC PATCH v3 17/37] fuse-bpf: Add support for read/write iter

kernel test robot noticed the following build warnings:

Hi Daniel,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on 49859de997c3115b85544bce6b6ceab60a7fabc4]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Rosenberg/bpf-verifier-Accept-dynptr-mem-as-mem-in-herlpers/20230418-094717
base:   49859de997c3115b85544bce6b6ceab60a7fabc4
patch link:    https://lore.kernel.org/r/20230418014037.2412394-18-drosen%40google.com
patch subject: [RFC PATCH v3 17/37] fuse-bpf: Add support for read/write iter
config: x86_64-randconfig-a011-20230417 (https://download.01.org/0day-ci/archive/20230418/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/9a6d8f43790d559390e1045c81a27203963bf109
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Daniel-Rosenberg/bpf-verifier-Accept-dynptr-mem-as-mem-in-herlpers/20230418-094717 git checkout 9a6d8f43790d559390e1045c81a27203963bf109
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from <built-in>:1:

>> ./usr/include/linux/fuse.h:747:1: warning: // comments are not allowed in this language [-Wcomment]

// This is likely not what we want
^
1 warning generated.

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
maintainer
#21 lore raw

Re: [RFC PATCH v3 17/37] fuse-bpf: Add support for read/write iter

In reply to Daniel Rosenberg
Quoted 91 lines · Daniel Rosenberg

Adds backing support for FUSE_READ and FUSE_WRITE

This includes adjustments from Amir Goldstein's patch to FUSE Passthrough

---
 fs/fuse/backing.c         | 371 ++++++++++++++++++++++++++++++++++++++
 fs/fuse/control.c         |   2 +-
 fs/fuse/file.c            |   8 +
 fs/fuse/fuse_i.h          |  19 +-
 fs/fuse/inode.c           |  13 ++
 include/uapi/linux/fuse.h |  10 +
 6 files changed, 421 insertions(+), 2 deletions(-)
fs/fuse/backing.c +59 -0
index c6ef10aeec15..c7709a880e9c 100644
@@ -11,6 +11,7 @@
 #include <linux/file.h>
 #include <linux/fs_stack.h>
 #include <linux/namei.h>
+#include <linux/uio.h>
 
 /*
  * expression statement to wrap the backing filter logic
@@ -76,6 +77,89 @@
        handled;                                                        \
 })
 
+#define FUSE_BPF_IOCB_MASK (IOCB_APPEND | IOCB_DSYNC | IOCB_HIPRI | IOCB_NOWAIT | IOCB_SYNC)
+
+struct fuse_bpf_aio_req {
+       struct kiocb iocb;
+       refcount_t ref;
+       struct kiocb *iocb_orig;
+       struct timespec64 pre_atime;
+};
+
+static struct kmem_cache *fuse_bpf_aio_request_cachep;
+
+static void fuse_file_accessed(struct file *dst_file, struct file *src_file)
+{
+       struct inode *dst_inode;
+       struct inode *src_inode;
+
+       if (dst_file->f_flags & O_NOATIME)
+               return;
+
+       dst_inode = file_inode(dst_file);
+       src_inode = file_inode(src_file);
+
+       if ((!timespec64_equal(&dst_inode->i_mtime, &src_inode->i_mtime) ||
+            !timespec64_equal(&dst_inode->i_ctime, &src_inode->i_ctime))) {
+               dst_inode->i_mtime = src_inode->i_mtime;
+               dst_inode->i_ctime = src_inode->i_ctime;
+       }
+
+       touch_atime(&dst_file->f_path);
+}
+
+static void fuse_copyattr(struct file *dst_file, struct file *src_file)
+{
+       struct inode *dst = file_inode(dst_file);
+       struct inode *src = file_inode(src_file);
+
+       dst->i_atime = src->i_atime;
+       dst->i_mtime = src->i_mtime;
+       dst->i_ctime = src->i_ctime;
+       i_size_write(dst, i_size_read(src));
+       fuse_invalidate_attr(dst);
+}
+
+static void fuse_file_start_write(struct file *fuse_file, struct file *backing_file,
+                                 loff_t pos, size_t count)
+{
+       struct inode *inode = file_inode(fuse_file);
+       struct fuse_inode *fi = get_fuse_inode(inode);
+
+       if (inode->i_size < pos + count)
+               set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
+
+       file_start_write(backing_file);
+}
+
+static void fuse_file_end_write(struct file *fuse_file, struct file *backing_file,
+                               loff_t pos, size_t res)
+{

fs/fuse/backing.c line 147

Quoted 10 lines
fs/fuse/backing.c +10 -0
+       struct inode *inode = file_inode(fuse_file);
+       struct fuse_inode *fi = get_fuse_inode(inode);
+
+       file_end_write(backing_file);
+
+       if (res > 0)
+               fuse_write_update_attr(inode, pos, res);
+
+       clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
+       fuse_invalidate_attr(inode);

This part is a bit out-of-date (was taken from my old branch) FWIW, I pushed a more recent version of these patches to:
https://github.com/amir73il/linux/commits/fuse-passthrough-fd
(only compile tested)

Quoted 146 lines
fs/fuse/backing.c +139 -0
+}
+
+static void fuse_file_start_read(struct file *backing_file, struct timespec64 *pre_atime)
+{
+       *pre_atime = file_inode(backing_file)->i_atime;
+}
+
+static void fuse_file_end_read(struct file *fuse_file, struct file *backing_file,
+                         struct timespec64 *pre_atime)
+{
+       /* Mimic atime update policy of passthrough inode, not the value */
+       if (!timespec64_equal(&file_inode(backing_file)->i_atime, pre_atime))
+               fuse_invalidate_atime(file_inode(fuse_file));
+}
+
 static void fuse_get_backing_path(struct file *file, struct path *path)
 {
        path_get(&file->f_path);
@@ -664,6 +748,277 @@ int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t o
                                file, offset, whence);
 }
 
+static inline void fuse_bpf_aio_put(struct fuse_bpf_aio_req *aio_req)
+{
+       if (refcount_dec_and_test(&aio_req->ref))
+               kmem_cache_free(fuse_bpf_aio_request_cachep, aio_req);
+}
+
+static void fuse_bpf_aio_cleanup_handler(struct fuse_bpf_aio_req *aio_req, long res)
+{
+       struct kiocb *iocb = &aio_req->iocb;
+       struct kiocb *iocb_orig = aio_req->iocb_orig;
+       struct file *filp = iocb->ki_filp;
+       struct file *fuse_filp = iocb_orig->ki_filp;
+
+       if (iocb->ki_flags & IOCB_WRITE) {
+               __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
+                                     SB_FREEZE_WRITE);
+               fuse_file_end_write(iocb_orig->ki_filp, iocb->ki_filp, iocb->ki_pos, res);
+       } else {
+               fuse_file_end_read(fuse_filp, filp, &aio_req->pre_atime);
+       }
+       iocb_orig->ki_pos = iocb->ki_pos;
+       fuse_bpf_aio_put(aio_req);
+}
+
+static void fuse_bpf_aio_rw_complete(struct kiocb *iocb, long res)
+{
+       struct fuse_bpf_aio_req *aio_req =
+               container_of(iocb, struct fuse_bpf_aio_req, iocb);
+       struct kiocb *iocb_orig = aio_req->iocb_orig;
+
+       fuse_bpf_aio_cleanup_handler(aio_req, res);
+       iocb_orig->ki_complete(iocb_orig, res);
+}
+
+struct fuse_file_read_iter_args {
+       struct fuse_read_in in;
+       struct fuse_read_iter_out out;
+};
+
+static int fuse_file_read_iter_initialize_in(struct bpf_fuse_args *fa, struct fuse_file_read_iter_args *args,
+                                            struct kiocb *iocb, struct iov_iter *to)
+{
+       struct file *file = iocb->ki_filp;
+       struct fuse_file *ff = file->private_data;
+
+       args->in = (struct fuse_read_in) {
+               .fh = ff->fh,
+               .offset = iocb->ki_pos,
+               .size = to->count,
+       };
+
+       /* TODO we can't assume 'to' is a kvec */
+       /* TODO we also can't assume the vector has only one component */
+       *fa = (struct bpf_fuse_args) {
+               .info = (struct bpf_fuse_meta_info) {
+                       .opcode = FUSE_READ,
+                       .nodeid = ff->nodeid,
+               },              .in_numargs = 1,
+               .in_args[0].size = sizeof(args->in),
+               .in_args[0].value = &args->in,
+               /*
+                * TODO Design this properly.
+                * Possible approach: do not pass buf to bpf
+                * If going to userland, do a deep copy
+                * For extra credit, do that to/from the vector, rather than
+                * making an extra copy in the kernel
+                */
+       };
+
+       return 0;
+}
+
+static int fuse_file_read_iter_initialize_out(struct bpf_fuse_args *fa, struct fuse_file_read_iter_args *args,
+                                             struct kiocb *iocb, struct iov_iter *to)
+{
+       args->out = (struct fuse_read_iter_out) {
+               .ret = args->in.size,
+       };
+
+       fa->out_numargs = 1;
+       fa->out_args[0].size = sizeof(args->out);
+       fa->out_args[0].value = &args->out;
+
+       return 0;
+}
+
+static int fuse_file_read_iter_backing(struct bpf_fuse_args *fa, ssize_t *out,
+                                      struct kiocb *iocb, struct iov_iter *to)
+{
+       struct fuse_read_iter_out *frio = fa->out_args[0].value;
+       struct file *file = iocb->ki_filp;
+       struct fuse_file *ff = file->private_data;
+
+       if (!iov_iter_count(to))
+               return 0;
+
+       if ((iocb->ki_flags & IOCB_DIRECT) &&
+           (!ff->backing_file->f_mapping->a_ops ||
+            !ff->backing_file->f_mapping->a_ops->direct_IO))
+               return -EINVAL;
+
+       /* TODO This just plain ignores any change to fuse_read_in */
+       if (is_sync_kiocb(iocb)) {
+               struct timespec64 pre_atime;
+
+               fuse_file_start_read(ff->backing_file, &pre_atime);
+               *out = vfs_iter_read(ff->backing_file, to, &iocb->ki_pos,
+                               iocb_to_rw_flags(iocb->ki_flags, FUSE_BPF_IOCB_MASK));
+               fuse_file_end_read(file, ff->backing_file, &pre_atime);
+       } else {
+               struct fuse_bpf_aio_req *aio_req;
+
+               *out = -ENOMEM;
+               aio_req = kmem_cache_zalloc(fuse_bpf_aio_request_cachep, GFP_KERNEL);
+               if (!aio_req)
+                       goto out;
+
+               aio_req->iocb_orig = iocb;
+               fuse_file_start_read(ff->backing_file, &aio_req->pre_atime);
+               kiocb_clone(&aio_req->iocb, iocb, ff->backing_file);
+               aio_req->iocb.ki_complete = fuse_bpf_aio_rw_complete;
+               refcount_set(&aio_req->ref, 2);
+               *out = vfs_iocb_iter_read(ff->backing_file, &aio_req->iocb, to);
+               fuse_bpf_aio_put(aio_req);

fs/fuse/backing.c int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t o · line 884

Quoted 10 lines
fs/fuse/backing.c +10 -0
+               if (*out != -EIOCBQUEUED)
+                       fuse_bpf_aio_cleanup_handler(aio_req, *out);
+       }
+
+       frio->ret = *out;
+
+       /* TODO Need to point value at the buffer for post-modification */
+
+out:
+       fuse_file_accessed(file, ff->backing_file);

fuse_file_accessed() looks redundant and less subtle what fuse_file_end_read() already does.

Quoted 74 lines
fs/fuse/backing.c +74 -0
+
+       return *out;
+}
+
+static int fuse_file_read_iter_finalize(struct bpf_fuse_args *fa, ssize_t *out,
+                                       struct kiocb *iocb, struct iov_iter *to)
+{
+       struct fuse_read_iter_out *frio = fa->out_args[0].value;
+
+       *out = frio->ret;
+
+       return 0;
+}
+
+int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *to)
+{
+       return bpf_fuse_backing(inode, struct fuse_file_read_iter_args, out,
+                               fuse_file_read_iter_initialize_in,
+                               fuse_file_read_iter_initialize_out,
+                               fuse_file_read_iter_backing,
+                               fuse_file_read_iter_finalize,
+                               iocb, to);
+}
+
+struct fuse_file_write_iter_args {
+       struct fuse_write_in in;
+       struct fuse_write_iter_out out;
+};
+
+static int fuse_file_write_iter_initialize_in(struct bpf_fuse_args *fa,
+                                             struct fuse_file_write_iter_args *args,
+                                             struct kiocb *iocb, struct iov_iter *from)
+{
+       struct file *file = iocb->ki_filp;
+       struct fuse_file *ff = file->private_data;
+
+       *args = (struct fuse_file_write_iter_args) {
+               .in.fh = ff->fh,
+               .in.offset = iocb->ki_pos,
+               .in.size = from->count,
+       };
+
+       /* TODO we can't assume 'from' is a kvec */
+       *fa = (struct bpf_fuse_args) {
+               .info = (struct bpf_fuse_meta_info) {
+                       .opcode = FUSE_WRITE,
+                       .nodeid = ff->nodeid,
+               },
+               .in_numargs = 1,
+               .in_args[0].size = sizeof(args->in),
+               .in_args[0].value = &args->in,
+       };
+
+       return 0;
+}
+
+static int fuse_file_write_iter_initialize_out(struct bpf_fuse_args *fa,
+                                              struct fuse_file_write_iter_args *args,
+                                              struct kiocb *iocb, struct iov_iter *from)
+{
+       /* TODO we can't assume 'from' is a kvec */
+       fa->out_numargs = 1;
+       fa->out_args[0].size = sizeof(args->out);
+       fa->out_args[0].value = &args->out;
+
+       return 0;
+}
+
+static int fuse_file_write_iter_backing(struct bpf_fuse_args *fa, ssize_t *out,
+                                       struct kiocb *iocb, struct iov_iter *from)
+{
+       struct file *file = iocb->ki_filp;
+       struct fuse_file *ff = file->private_data;
+       struct fuse_write_iter_out *fwio = fa->out_args[0].value;

fs/fuse/backing.c int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t o · line 968

Quoted 10 lines
fs/fuse/backing.c +10 -0
+       ssize_t count = iov_iter_count(from);
+
+       if (!count)
+               return 0;
+
+       /* TODO This just plain ignores any change to fuse_write_in */
+       /* TODO uint32_t seems smaller than ssize_t.... right? */
+       inode_lock(file_inode(file));
+
+       fuse_copyattr(file, ff->backing_file);

fuse_copyattr() looks redundant and less subtle than what fuse_file_end_write() already does.

Thanks,
Amir.

#22 lore raw

RFC PATCH v3 18/37 fuse-bpf: support readdir

In reply to Daniel Rosenberg

This adds backing support for FUSE_READDIR

---
 fs/fuse/backing.c         | 194 ++++++++++++++++++++++++++++++++++++++
 fs/fuse/fuse_i.h          |   6 ++
 fs/fuse/readdir.c         |   5 +
 include/uapi/linux/fuse.h |   6 ++
 4 files changed, 211 insertions(+)
fs/fuse/backing.c +194 -0
index c7709a880e9c..2908c231a695 100644
@@ -1669,6 +1669,200 @@ int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry)
 				dir, entry);
 }
 
+struct fuse_read_args {
+	struct fuse_read_in in;
+	struct fuse_read_out out;
+	struct fuse_buffer buffer;
+};
+
+static int fuse_readdir_initialize_in(struct bpf_fuse_args *fa, struct fuse_read_args *args,
+				      struct file *file, struct dir_context *ctx,
+				      bool *force_again, bool *allow_force, bool is_continued)
+{
+	struct fuse_file *ff = file->private_data;
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = ff->nodeid,
+			.opcode = FUSE_READDIR,
+		},
+		.in_numargs = 1,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+	};
+
+	args->in = (struct fuse_read_in) {
+		.fh = ff->fh,
+		.offset = ctx->pos,
+		.size = PAGE_SIZE,
+	};
+
+	*force_again = false;
+	*allow_force = true;
+	return 0;
+}
+
+static int fuse_readdir_initialize_out(struct bpf_fuse_args *fa, struct fuse_read_args *args,
+				       struct file *file, struct dir_context *ctx,
+				       bool *force_again, bool *allow_force, bool is_continued)
+{
+	u8 *page = (u8 *)__get_free_page(GFP_KERNEL);
+
+	if (!page)
+		return -ENOMEM;
+
+	fa->flags = FUSE_BPF_OUT_ARGVAR;
+	fa->out_numargs = 2;
+	fa->out_args[0] = (struct bpf_fuse_arg) {
+		.size = sizeof(args->out),
+		.value = &args->out,
+	};
+	fa->out_args[1] = (struct bpf_fuse_arg) {
+		.is_buffer = true,
+		.buffer = &args->buffer,
+	};
+	args->out = (struct fuse_read_out) {
+		.again = 0,
+		.offset = 0,
+	};
+	args->buffer = (struct fuse_buffer) {
+		.data = page,
+		.size = PAGE_SIZE,
+		.alloc_size = PAGE_SIZE,
+		.max_size = PAGE_SIZE,
+		.flags = BPF_FUSE_VARIABLE_SIZE,
+	};
+
+	return 0;
+}
+
+struct fusebpf_ctx {
+	struct dir_context ctx;
+	u8 *addr;
+	size_t offset;
+};
+
+static bool filldir(struct dir_context *ctx, const char *name, int namelen,
+		   loff_t offset, u64 ino, unsigned int d_type)
+{
+	struct fusebpf_ctx *ec = container_of(ctx, struct fusebpf_ctx, ctx);
+	struct fuse_dirent *fd = (struct fuse_dirent *)(ec->addr + ec->offset);
+
+	if (ec->offset + sizeof(struct fuse_dirent) + namelen > PAGE_SIZE)
+		return false;
+
+	*fd = (struct fuse_dirent) {
+		.ino = ino,
+		.off = offset,
+		.namelen = namelen,
+		.type = d_type,
+	};
+
+	memcpy(fd->name, name, namelen);
+	ec->offset += FUSE_DIRENT_SIZE(fd);
+
+	return true;
+}
+
+static int parse_dirfile(char *buf, size_t nbytes, struct dir_context *ctx)
+{
+	while (nbytes >= FUSE_NAME_OFFSET) {
+		struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
+		size_t reclen = FUSE_DIRENT_SIZE(dirent);
+
+		if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
+			return -EIO;
+		if (reclen > nbytes)
+			break;
+		if (memchr(dirent->name, '/', dirent->namelen) != NULL)
+			return -EIO;
+
+		ctx->pos = dirent->off;
+		if (!dir_emit(ctx, dirent->name, dirent->namelen, dirent->ino,
+				dirent->type))
+			break;
+
+		buf += reclen;
+		nbytes -= reclen;
+	}
+
+	return 0;
+}
+
+static int fuse_readdir_backing(struct bpf_fuse_args *fa, int *out,
+				struct file *file, struct dir_context *ctx,
+				bool *force_again, bool *allow_force, bool is_continued)
+{
+	struct fuse_file *ff = file->private_data;
+	struct file *backing_dir = ff->backing_file;
+	struct fuse_read_out *fro = fa->out_args[0].value;
+	struct fusebpf_ctx ec;
+
+	ec = (struct fusebpf_ctx) {
+		.ctx.actor = filldir,
+		.ctx.pos = ctx->pos,
+		.addr = fa->out_args[1].buffer->data,
+	};
+
+	if (!ec.addr)
+		return -ENOMEM;
+
+	if (!is_continued)
+		backing_dir->f_pos = file->f_pos;
+
+	*out = iterate_dir(backing_dir, &ec.ctx);
+	if (ec.offset == 0)
+		*allow_force = false;
+	fa->out_args[1].buffer->size = ec.offset;
+
+	fro->offset = ec.ctx.pos;
+	fro->again = false;
+
+	return *out;
+}
+
+static int fuse_readdir_finalize(struct bpf_fuse_args *fa, int *out,
+				 struct file *file, struct dir_context *ctx,
+				 bool *force_again, bool *allow_force, bool is_continued)
+{
+	struct fuse_read_out *fro = fa->out_args[0].value;
+	struct fuse_file *ff = file->private_data;
+	struct file *backing_dir = ff->backing_file;
+
+	*out = parse_dirfile(fa->out_args[1].buffer->data, fa->out_args[1].buffer->size, ctx);
+	*force_again = !!fro->again;
+	if (*force_again && !*allow_force)
+		*out = -EINVAL;
+
+	ctx->pos = fro->offset;
+	backing_dir->f_pos = fro->offset;
+
+	free_page((unsigned long)fa->out_args[1].buffer->data);
+	return *out;
+}
+
+int fuse_bpf_readdir(int *out, struct inode *inode, struct file *file, struct dir_context *ctx)
+{
+	int ret;
+	bool allow_force;
+	bool force_again = false;
+	bool is_continued = false;
+
+again:
+	ret = bpf_fuse_backing(inode, struct fuse_read_args, out,
+			       fuse_readdir_initialize_in, fuse_readdir_initialize_out,
+			       fuse_readdir_backing, fuse_readdir_finalize,
+			       file, ctx, &force_again, &allow_force, is_continued);
+	if (force_again && *out >= 0) {
+		is_continued = true;
+		goto again;
+	}
+
+	return ret;
+}
+
 static int fuse_access_initialize_in(struct bpf_fuse_args *fa, struct fuse_access_in *in,
 				     struct inode *inode, int mask)
 {
fs/fuse/fuse_i.h +6 -0
index 4bc070b81ac2..fb3a77b79b0f 100644
@@ -1419,6 +1419,7 @@ int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *ioc
 int fuse_bpf_file_write_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *from);
 int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, int mode, loff_t offset, loff_t length);
 int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry, unsigned int flags);
+int fuse_bpf_readdir(int *out, struct inode *inode, struct file *file, struct dir_context *ctx);
 int fuse_bpf_access(int *out, struct inode *inode, int mask);
 
 #else
@@ -1489,6 +1490,11 @@ static inline int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct
 	return 0;
 }
 
+static inline int fuse_bpf_readdir(int *out, struct inode *inode, struct file *file, struct dir_context *ctx)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_access(int *out, struct inode *inode, int mask)
 {
 	return 0;
fs/fuse/readdir.c +5 -0
index dc603479b30e..cc6548f314f2 100644
@@ -20,6 +20,8 @@ static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
 
 	if (!fc->do_readdirplus)
 		return false;
+	if (fi->nodeid == 0)
+		return false;
 	if (!fc->readdirplus_auto)
 		return true;
 	if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
@@ -582,6 +584,9 @@ int fuse_readdir(struct file *file, struct dir_context *ctx)
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_bpf_readdir(&err, inode, file, ctx))
+		return err;
+
 	mutex_lock(&ff->readdir.lock);
 
 	err = UNCACHED;
include/uapi/linux/fuse.h +6 -0
index dbfc8d501bcb..e779064f5fad 100644
@@ -748,6 +748,12 @@ struct fuse_read_in {
 	uint32_t	padding;
 };
 
+struct fuse_read_out {
+	uint64_t	offset;
+	uint32_t	again;
+	uint32_t	padding;
+};
+
 // This is likely not what we want
 struct fuse_read_iter_out {
 	uint64_t ret;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#23 lore raw

RFC PATCH v3 19/37 fuse-bpf: Add support for sync operations

In reply to Daniel Rosenberg

This adds backing support for FUSE_FLUSH, FUSE_FSYNC, and FUSE_FSYNCDIR.

---
 fs/fuse/backing.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dir.c     |   3 +
 fs/fuse/file.c    |   7 +++
 fs/fuse/fuse_i.h  |  18 ++++++
 4 files changed, 175 insertions(+)
fs/fuse/backing.c +147 -0
index 2908c231a695..30492f7b2a05 100644
@@ -659,6 +659,59 @@ int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file)
 				fuse_release_backing, fuse_release_finalize, inode, file);
 }
 
+static int fuse_flush_initialize_in(struct bpf_fuse_args *fa, struct fuse_flush_in *ffi,
+				    struct file *file, fl_owner_t id)
+{
+	struct fuse_file *fuse_file = file->private_data;
+
+	*ffi = (struct fuse_flush_in) {
+		.fh = fuse_file->fh,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(file->f_inode),
+			.opcode = FUSE_FLUSH,
+		},
+		.in_numargs = 1,
+		.in_args[0].size = sizeof(*ffi),
+		.in_args[0].value = ffi,
+		.flags = FUSE_BPF_FORCE,
+	};
+
+	return 0;
+}
+
+static int fuse_flush_initialize_out(struct bpf_fuse_args *fa, struct fuse_flush_in *ffi,
+				     struct file *file, fl_owner_t id)
+{
+	return 0;
+}
+
+static int fuse_flush_backing(struct bpf_fuse_args *fa, int *out, struct file *file, fl_owner_t id)
+{
+	struct fuse_file *fuse_file = file->private_data;
+	struct file *backing_file = fuse_file->backing_file;
+
+	*out = 0;
+	if (backing_file->f_op->flush)
+		*out = backing_file->f_op->flush(backing_file, id);
+	return *out;
+}
+
+static int fuse_flush_finalize(struct bpf_fuse_args *fa, int *out, struct file *file, fl_owner_t id)
+{
+	return 0;
+}
+
+int fuse_bpf_flush(int *out, struct inode *inode, struct file *file, fl_owner_t id)
+{
+	return bpf_fuse_backing(inode, struct fuse_flush_in, out,
+				fuse_flush_initialize_in, fuse_flush_initialize_out,
+				fuse_flush_backing, fuse_flush_finalize,
+				file, id);
+}
+
 struct fuse_lseek_args {
 	struct fuse_lseek_in in;
 	struct fuse_lseek_out out;
@@ -748,6 +801,100 @@ int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t o
 				file, offset, whence);
 }
 
+static int fuse_fsync_initialize_in(struct bpf_fuse_args *fa, struct fuse_fsync_in *in,
+				    struct file *file, loff_t start, loff_t end, int datasync)
+{
+	struct fuse_file *fuse_file = file->private_data;
+
+	*in = (struct fuse_fsync_in) {
+		.fh = fuse_file->fh,
+		.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_fuse_inode(file->f_inode)->nodeid,
+			.opcode = FUSE_FSYNC,
+		},
+		.in_numargs = 1,
+		.in_args[0].size = sizeof(*in),
+		.in_args[0].value = in,
+		.flags = FUSE_BPF_FORCE,
+	};
+
+	return 0;
+}
+
+static int fuse_fsync_initialize_out(struct bpf_fuse_args *fa, struct fuse_fsync_in *ffi,
+				     struct file *file, loff_t start, loff_t end, int datasync)
+{
+	return 0;
+}
+
+static int fuse_fsync_backing(struct bpf_fuse_args *fa, int *out,
+			      struct file *file, loff_t start, loff_t end, int datasync)
+{
+	struct fuse_file *fuse_file = file->private_data;
+	struct file *backing_file = fuse_file->backing_file;
+	const struct fuse_fsync_in *ffi = fa->in_args[0].value;
+	int new_datasync = (ffi->fsync_flags & FUSE_FSYNC_FDATASYNC) ? 1 : 0;
+
+	*out = vfs_fsync(backing_file, new_datasync);
+	return 0;
+}
+
+static int fuse_fsync_finalize(struct bpf_fuse_args *fa, int *out,
+			       struct file *file, loff_t start, loff_t end, int datasync)
+{
+	return 0;
+}
+
+int fuse_bpf_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync)
+{
+	return bpf_fuse_backing(inode, struct fuse_fsync_in, out,
+				fuse_fsync_initialize_in, fuse_fsync_initialize_out,
+				fuse_fsync_backing, fuse_fsync_finalize,
+				file, start, end, datasync);
+}
+
+static int fuse_dir_fsync_initialize_in(struct bpf_fuse_args *fa, struct fuse_fsync_in *in,
+					struct file *file, loff_t start, loff_t end, int datasync)
+{
+	struct fuse_file *fuse_file = file->private_data;
+
+	*in = (struct fuse_fsync_in) {
+		.fh = fuse_file->fh,
+		.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_fuse_inode(file->f_inode)->nodeid,
+			.opcode = FUSE_FSYNCDIR,
+		},
+		.in_numargs = 1,
+		.in_args[0].size = sizeof(*in),
+		.in_args[0].value = in,
+		.flags = FUSE_BPF_FORCE,
+	};
+
+	return 0;
+}
+
+static int fuse_dir_fsync_initialize_out(struct bpf_fuse_args *fa, struct fuse_fsync_in *ffi,
+					 struct file *file, loff_t start, loff_t end, int datasync)
+{
+	return 0;
+}
+
+int fuse_bpf_dir_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync)
+{
+	return bpf_fuse_backing(inode, struct fuse_fsync_in, out,
+				fuse_dir_fsync_initialize_in, fuse_dir_fsync_initialize_out,
+				fuse_fsync_backing, fuse_fsync_finalize,
+				file, start, end, datasync);
+}
+
 static inline void fuse_bpf_aio_put(struct fuse_bpf_aio_req *aio_req)
 {
 	if (refcount_dec_and_test(&aio_req->ref))
fs/fuse/dir.c +3 -0
index a763a45fa973..5ce65f696980 100644
@@ -1666,6 +1666,9 @@ static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_bpf_dir_fsync(&err, inode, file, start, end, datasync))
+		return err;
+
 	if (fc->no_fsyncdir)
 		return 0;
 
fs/fuse/file.c +7 -0
index 5f19ef5bf124..a4a0aeb28e4a 100644
@@ -554,10 +554,14 @@ static int fuse_flush(struct file *file, fl_owner_t id)
 	struct inode *inode = file_inode(file);
 	struct fuse_mount *fm = get_fuse_mount(inode);
 	struct fuse_file *ff = file->private_data;
+	int err;
 
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_bpf_flush(&err, file_inode(file), file, id))
+		return err;
+
 	if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache)
 		return 0;
 
@@ -615,6 +619,9 @@ static int fuse_fsync(struct file *file, loff_t start, loff_t end,
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_bpf_fsync(&err, inode, file, start, end, datasync))
+		return err;
+
 	inode_lock(inode);
 
 	/*
fs/fuse/fuse_i.h +18 -0
index fb3a77b79b0f..e60207bf66de 100644
@@ -1414,7 +1414,10 @@ int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *entry);
 int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry);
 int fuse_bpf_release(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file);
+int fuse_bpf_flush(int *out, struct inode *inode, struct file *file, fl_owner_t id);
 int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence);
+int fuse_bpf_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync);
+int fuse_bpf_dir_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync);
 int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *to);
 int fuse_bpf_file_write_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *from);
 int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, int mode, loff_t offset, loff_t length);
@@ -1465,11 +1468,26 @@ static inline int fuse_bpf_releasedir(int *out, struct inode *inode, struct file
 	return 0;
 }
 
+static inline int fuse_bpf_flush(int *out, struct inode *inode, struct file *file, fl_owner_t id)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence)
 {
 	return 0;
 }
 
+static inline int fuse_bpf_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_dir_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *to)
 {
 	return 0;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#24 lore raw

RFC PATCH v3 20/37 fuse-bpf: Add Rename support

In reply to Daniel Rosenberg

This adds backing support for FUSE_RENAME and FUSE_RENAME2

---
 fs/fuse/backing.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dir.c     |   7 ++
 fs/fuse/fuse_i.h  |  18 ++++
 3 files changed, 275 insertions(+)
fs/fuse/backing.c +250 -0
index 30492f7b2a05..d3a706b55905 100644
@@ -1747,6 +1747,256 @@ int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *entry)
 				dir, entry);
 }
 
+static int fuse_rename_backing_common(struct inode *olddir,
+				      struct dentry *oldent,
+				      struct inode *newdir,
+				      struct dentry *newent, unsigned int flags)
+{
+	int err = 0;
+	struct path old_backing_path;
+	struct path new_backing_path;
+	struct dentry *old_backing_dir_dentry;
+	struct dentry *old_backing_dentry;
+	struct dentry *new_backing_dir_dentry;
+	struct dentry *new_backing_dentry;
+	struct dentry *trap = NULL;
+	struct inode *target_inode;
+	struct renamedata rd;
+
+	//TODO Actually deal with changing anything that isn't a flag
+	get_fuse_backing_path(oldent, &old_backing_path);
+	if (!old_backing_path.dentry)
+		return -EBADF;
+	get_fuse_backing_path(newent, &new_backing_path);
+	if (!new_backing_path.dentry) {
+		/*
+		 * TODO A file being moved from a backing path to another
+		 * backing path which is not yet instrumented with FUSE-BPF.
+		 * This may be slow and should be substituted with something
+		 * more clever.
+		 */
+		err = -EXDEV;
+		goto put_old_path;
+	}
+	if (new_backing_path.mnt != old_backing_path.mnt) {
+		err = -EXDEV;
+		goto put_new_path;
+	}
+	old_backing_dentry = old_backing_path.dentry;
+	new_backing_dentry = new_backing_path.dentry;
+	old_backing_dir_dentry = dget_parent(old_backing_dentry);
+	new_backing_dir_dentry = dget_parent(new_backing_dentry);
+	target_inode = d_inode(newent);
+
+	trap = lock_rename(old_backing_dir_dentry, new_backing_dir_dentry);
+	if (trap == old_backing_dentry) {
+		err = -EINVAL;
+		goto put_parents;
+	}
+	if (trap == new_backing_dentry) {
+		err = -ENOTEMPTY;
+		goto put_parents;
+	}
+
+	rd = (struct renamedata) {
+		.old_mnt_idmap = &nop_mnt_idmap,
+		.old_dir = d_inode(old_backing_dir_dentry),
+		.old_dentry = old_backing_dentry,
+		.new_mnt_idmap = &nop_mnt_idmap,
+		.new_dir = d_inode(new_backing_dir_dentry),
+		.new_dentry = new_backing_dentry,
+		.flags = flags,
+	};
+	err = vfs_rename(&rd);
+	if (err)
+		goto unlock;
+	if (target_inode)
+		fsstack_copy_attr_all(target_inode,
+				get_fuse_inode(target_inode)->backing_inode);
+	fsstack_copy_attr_all(d_inode(oldent), d_inode(old_backing_dentry));
+unlock:
+	unlock_rename(old_backing_dir_dentry, new_backing_dir_dentry);
+put_parents:
+	dput(new_backing_dir_dentry);
+	dput(old_backing_dir_dentry);
+put_new_path:
+	path_put(&new_backing_path);
+put_old_path:
+	path_put(&old_backing_path);
+	return err;
+}
+
+struct fuse_rename2_args {
+	struct fuse_rename2_in in;
+	struct fuse_buffer old_name;
+	struct fuse_buffer new_name;
+};
+
+static int fuse_rename2_initialize_in(struct bpf_fuse_args *fa, struct fuse_rename2_args *args,
+				      struct inode *olddir, struct dentry *oldent,
+				      struct inode *newdir, struct dentry *newent,
+				      unsigned int flags)
+{
+	*args = (struct fuse_rename2_args) {
+		.in = (struct fuse_rename2_in) {
+			.newdir = get_node_id(newdir),
+			.flags = flags,
+		},
+		.old_name = (struct fuse_buffer) {
+			.data = (void *) oldent->d_name.name,
+			.size = oldent->d_name.len + 1,
+			.flags = BPF_FUSE_IMMUTABLE,
+		},
+		.new_name = (struct fuse_buffer) {
+			.data = (void *) newent->d_name.name,
+			.size = newent->d_name.len + 1,
+			.flags = BPF_FUSE_IMMUTABLE,
+		},
+
+	};
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(olddir),
+			.opcode = FUSE_RENAME2,
+		},
+		.in_numargs = 3,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+		.in_args[1] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->old_name,
+		},
+		.in_args[2] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->new_name,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_rename2_initialize_out(struct bpf_fuse_args *fa, struct fuse_rename2_args *args,
+				       struct inode *olddir, struct dentry *oldent,
+				       struct inode *newdir, struct dentry *newent,
+				       unsigned int flags)
+{
+	return 0;
+}
+
+static int fuse_rename2_backing(struct bpf_fuse_args *fa, int *out,
+				struct inode *olddir, struct dentry *oldent,
+				struct inode *newdir, struct dentry *newent,
+				unsigned int flags)
+{
+	const struct fuse_rename2_args *fri = fa->in_args[0].value;
+
+	/* TODO: deal with changing dirs/ents */
+	*out = fuse_rename_backing_common(olddir, oldent, newdir, newent,
+					  fri->in.flags);
+	return *out;
+}
+
+static int fuse_rename2_finalize(struct bpf_fuse_args *fa, int *out,
+				 struct inode *olddir, struct dentry *oldent,
+				 struct inode *newdir, struct dentry *newent,
+				 unsigned int flags)
+{
+	return 0;
+}
+
+int fuse_bpf_rename2(int *out, struct inode *olddir, struct dentry *oldent,
+		     struct inode *newdir, struct dentry *newent,
+		     unsigned int flags)
+{
+	return bpf_fuse_backing(olddir, struct fuse_rename2_args, out,
+				fuse_rename2_initialize_in, fuse_rename2_initialize_out,
+				fuse_rename2_backing, fuse_rename2_finalize,
+				olddir, oldent, newdir, newent, flags);
+}
+
+struct fuse_rename_args {
+	struct fuse_rename_in in;
+	struct fuse_buffer old_name;
+	struct fuse_buffer new_name;
+};
+
+static int fuse_rename_initialize_in(struct bpf_fuse_args *fa, struct fuse_rename_args *args,
+				      struct inode *olddir, struct dentry *oldent,
+				      struct inode *newdir, struct dentry *newent)
+{
+	*args = (struct fuse_rename_args) {
+		.in = (struct fuse_rename_in) {
+			.newdir = get_node_id(newdir),
+		},
+		.old_name = (struct fuse_buffer) {
+			.data = (void *) oldent->d_name.name,
+			.size = oldent->d_name.len + 1,
+			.flags = BPF_FUSE_IMMUTABLE,
+		},
+		.new_name = (struct fuse_buffer) {
+			.data = (void *) newent->d_name.name,
+			.size = newent->d_name.len + 1,
+			.flags = BPF_FUSE_IMMUTABLE,
+		},
+
+	};
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(olddir),
+			.opcode = FUSE_RENAME,
+		},
+		.in_numargs = 3,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+		.in_args[1] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->old_name,
+		},
+		.in_args[2] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->new_name,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_rename_initialize_out(struct bpf_fuse_args *fa, struct fuse_rename_args *args,
+				      struct inode *olddir, struct dentry *oldent,
+				      struct inode *newdir, struct dentry *newent)
+{
+	return 0;
+}
+
+static int fuse_rename_backing(struct bpf_fuse_args *fa, int *out,
+			       struct inode *olddir, struct dentry *oldent,
+			       struct inode *newdir, struct dentry *newent)
+{
+	/* TODO: deal with changing dirs/ents */
+	*out = fuse_rename_backing_common(olddir, oldent, newdir, newent, 0);
+	return *out;
+}
+
+static int fuse_rename_finalize(struct bpf_fuse_args *fa, int *out,
+				struct inode *olddir, struct dentry *oldent,
+				struct inode *newdir, struct dentry *newent)
+{
+	return 0;
+}
+
+int fuse_bpf_rename(int *out, struct inode *olddir, struct dentry *oldent,
+		    struct inode *newdir, struct dentry *newent)
+{
+	return bpf_fuse_backing(olddir, struct fuse_rename_args, out,
+				fuse_rename_initialize_in, fuse_rename_initialize_out,
+				fuse_rename_backing, fuse_rename_finalize,
+				olddir, oldent, newdir, newent);
+}
+
 static int fuse_unlink_initialize_in(struct bpf_fuse_args *fa, struct fuse_buffer *name,
 				     struct inode *dir, struct dentry *entry)
 {
fs/fuse/dir.c +7 -0
index 5ce65f696980..086e3ecada19 100644
@@ -1184,6 +1184,10 @@ static int fuse_rename2(struct mnt_idmap *idmap, struct inode *olddir,
 		return -EINVAL;
 
 	if (flags) {
+		if (fuse_bpf_rename2(&err, olddir, oldent, newdir, newent, flags))
+			return err;
+
+		/* TODO: how should this go with bpfs involved? */
 		if (fc->no_rename2 || fc->minor < 23)
 			return -EINVAL;
 
@@ -1195,6 +1199,9 @@ static int fuse_rename2(struct mnt_idmap *idmap, struct inode *olddir,
 			err = -EINVAL;
 		}
 	} else {
+		if (fuse_bpf_rename(&err, olddir, oldent, newdir, newent))
+			return err;
+
 		err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
 					 FUSE_RENAME,
 					 sizeof(struct fuse_rename_in));
fs/fuse/fuse_i.h +18 -0
index e60207bf66de..5c8bd2f76fb9 100644
@@ -1411,6 +1411,11 @@ int fuse_bpf_create_open(int *out, struct inode *dir, struct dentry *entry,
 int fuse_bpf_mknod(int *out, struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
 int fuse_bpf_mkdir(int *out, struct inode *dir, struct dentry *entry, umode_t mode);
 int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *entry);
+int fuse_bpf_rename2(int *out, struct inode *olddir, struct dentry *oldent,
+		     struct inode *newdir, struct dentry *newent,
+		     unsigned int flags);
+int fuse_bpf_rename(int *out, struct inode *olddir, struct dentry *oldent,
+		    struct inode *newdir, struct dentry *newent);
 int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry);
 int fuse_bpf_release(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file);
@@ -1453,6 +1458,19 @@ static inline int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *ent
 	return 0;
 }
 
+static inline int fuse_bpf_rename2(int *out, struct inode *olddir, struct dentry *oldent,
+				   struct inode *newdir, struct dentry *newent,
+				   unsigned int flags)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_rename(int *out, struct inode *olddir, struct dentry *oldent,
+				  struct inode *newdir, struct dentry *newent)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry)
 {
 	return 0;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#25 lore raw

RFC PATCH v3 21/37 fuse-bpf: Add attr support

In reply to Daniel Rosenberg

This adds backing support for FUSE_GETATTR, FUSE_SETATTR, and FUSE_STATFS

---
 fs/fuse/backing.c | 288 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dir.c     |  68 ++---------
 fs/fuse/fuse_i.h  | 102 ++++++++++++++++
 fs/fuse/inode.c   |  17 +--
 4 files changed, 405 insertions(+), 70 deletions(-)
fs/fuse/backing.c +288 -0
index d3a706b55905..6a6130a16d2b 100644
@@ -2066,6 +2066,294 @@ int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry)
 				dir, entry);
 }
 
+struct fuse_getattr_args {
+	struct fuse_getattr_in in;
+	struct fuse_attr_out out;
+};
+
+static int fuse_getattr_initialize_in(struct bpf_fuse_args *fa, struct fuse_getattr_args *args,
+				      const struct dentry *entry, struct kstat *stat,
+				      u32 request_mask, unsigned int flags)
+{
+	args->in = (struct fuse_getattr_in) {
+		.getattr_flags = flags,
+		.fh = -1, /* TODO is this OK? */
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(entry->d_inode),
+			.opcode = FUSE_GETATTR,
+		},
+		.in_numargs = 1,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_getattr_initialize_out(struct bpf_fuse_args *fa, struct fuse_getattr_args *args,
+				       const struct dentry *entry, struct kstat *stat,
+				       u32 request_mask, unsigned int flags)
+{
+	args->out = (struct fuse_attr_out) { 0 };
+
+	fa->out_numargs = 1;
+	fa->out_args[0] = (struct bpf_fuse_arg) {
+		.size = sizeof(args->out),
+		.value = &args->out,
+	};
+
+	return 0;
+}
+
+static int fuse_getattr_backing(struct bpf_fuse_args *fa, int *out,
+				const struct dentry *entry, struct kstat *stat,
+				u32 request_mask, unsigned int flags)
+{
+	struct path *backing_path = &get_fuse_dentry(entry)->backing_path;
+	struct inode *backing_inode = backing_path->dentry->d_inode;
+	struct fuse_attr_out *fao = fa->out_args[0].value;
+	struct kstat tmp;
+
+	if (!stat)
+		stat = &tmp;
+
+	*out = vfs_getattr(backing_path, stat, request_mask, flags);
+
+	if (!*out)
+		fuse_stat_to_attr(get_fuse_conn(entry->d_inode), backing_inode,
+				  stat, &fao->attr);
+
+	return 0;
+}
+
+static int finalize_attr(struct inode *inode, struct fuse_attr_out *outarg,
+				u64 attr_version, struct kstat *stat)
+{
+	int err = 0;
+
+	if (fuse_invalid_attr(&outarg->attr) ||
+	    ((inode->i_mode ^ outarg->attr.mode) & S_IFMT)) {
+		fuse_make_bad(inode);
+		err = -EIO;
+	} else {
+		fuse_change_attributes(inode, &outarg->attr,
+				       attr_timeout(outarg),
+				       attr_version);
+		if (stat)
+			fuse_fillattr(inode, &outarg->attr, stat);
+	}
+	return err;
+}
+
+static int fuse_getattr_finalize(struct bpf_fuse_args *fa, int *out,
+				 const struct dentry *entry, struct kstat *stat,
+				 u32 request_mask, unsigned int flags)
+{
+	struct fuse_attr_out *outarg = fa->out_args[0].value;
+	struct inode *inode = entry->d_inode;
+	u64 attr_version = fuse_get_attr_version(get_fuse_mount(inode)->fc);
+
+	/* TODO: Ensure this doesn't happen if we had an error getting attrs in
+	 * backing.
+	 */
+	*out = finalize_attr(inode, outarg, attr_version, stat);
+	return 0;
+}
+
+int fuse_bpf_getattr(int *out, struct inode *inode, const struct dentry *entry, struct kstat *stat,
+		     u32 request_mask, unsigned int flags)
+{
+	return bpf_fuse_backing(inode, struct fuse_getattr_args, out,
+				fuse_getattr_initialize_in, fuse_getattr_initialize_out,
+				fuse_getattr_backing, fuse_getattr_finalize,
+				entry, stat, request_mask, flags);
+}
+
+static void fattr_to_iattr(struct fuse_conn *fc,
+			   const struct fuse_setattr_in *arg,
+			   struct iattr *iattr)
+{
+	unsigned int fvalid = arg->valid;
+
+	if (fvalid & FATTR_MODE)
+		iattr->ia_valid |= ATTR_MODE, iattr->ia_mode = arg->mode;
+	if (fvalid & FATTR_UID) {
+		iattr->ia_valid |= ATTR_UID;
+		iattr->ia_uid = make_kuid(fc->user_ns, arg->uid);
+	}
+	if (fvalid & FATTR_GID) {
+		iattr->ia_valid |= ATTR_GID;
+		iattr->ia_gid = make_kgid(fc->user_ns, arg->gid);
+	}
+	if (fvalid & FATTR_SIZE)
+		iattr->ia_valid |= ATTR_SIZE, iattr->ia_size = arg->size;
+	if (fvalid & FATTR_ATIME) {
+		iattr->ia_valid |= ATTR_ATIME;
+		iattr->ia_atime.tv_sec = arg->atime;
+		iattr->ia_atime.tv_nsec = arg->atimensec;
+		if (!(fvalid & FATTR_ATIME_NOW))
+			iattr->ia_valid |= ATTR_ATIME_SET;
+	}
+	if (fvalid & FATTR_MTIME) {
+		iattr->ia_valid |= ATTR_MTIME;
+		iattr->ia_mtime.tv_sec = arg->mtime;
+		iattr->ia_mtime.tv_nsec = arg->mtimensec;
+		if (!(fvalid & FATTR_MTIME_NOW))
+			iattr->ia_valid |= ATTR_MTIME_SET;
+	}
+	if (fvalid & FATTR_CTIME) {
+		iattr->ia_valid |= ATTR_CTIME;
+		iattr->ia_ctime.tv_sec = arg->ctime;
+		iattr->ia_ctime.tv_nsec = arg->ctimensec;
+	}
+}
+
+struct fuse_setattr_args {
+	struct fuse_setattr_in in;
+	struct fuse_attr_out out;
+};
+
+static int fuse_setattr_initialize_in(struct bpf_fuse_args *fa, struct fuse_setattr_args *args,
+				      struct dentry *dentry, struct iattr *attr, struct file *file)
+{
+	struct fuse_conn *fc = get_fuse_conn(dentry->d_inode);
+
+	*args = (struct fuse_setattr_args) { 0 };
+	iattr_to_fattr(fc, attr, &args->in, true);
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.opcode = FUSE_SETATTR,
+			.nodeid = get_node_id(dentry->d_inode),
+		},
+		.in_numargs = 1,
+		.in_args[0].size = sizeof(args->in),
+		.in_args[0].value = &args->in,
+	};
+
+	return 0;
+}
+
+static int fuse_setattr_initialize_out(struct bpf_fuse_args *fa, struct fuse_setattr_args *args,
+				       struct dentry *dentry, struct iattr *attr, struct file *file)
+{
+	fa->out_numargs = 1;
+	fa->out_args[0].size = sizeof(args->out);
+	fa->out_args[0].value = &args->out;
+
+	return 0;
+}
+
+static int fuse_setattr_backing(struct bpf_fuse_args *fa, int *out,
+				struct dentry *dentry, struct iattr *attr, struct file *file)
+{
+	struct fuse_conn *fc = get_fuse_conn(dentry->d_inode);
+	const struct fuse_setattr_in *fsi = fa->in_args[0].value;
+	struct iattr new_attr = { 0 };
+	struct path *backing_path = &get_fuse_dentry(dentry)->backing_path;
+
+	fattr_to_iattr(fc, fsi, &new_attr);
+	/* TODO: Some info doesn't get saved by the attr->fattr->attr transition
+	 * When we actually allow the bpf to change these, we may have to consider
+	 * the extra flags more, or pass more info into the bpf. Until then we can
+	 * keep everything except for ATTR_FILE, since we'd need a file on the
+	 * lower fs. For what it's worth, neither f2fs nor ext4 make use of that
+	 * even if it is present.
+	 */
+	new_attr.ia_valid = attr->ia_valid & ~ATTR_FILE;
+	inode_lock(d_inode(backing_path->dentry));
+	*out = notify_change(&nop_mnt_idmap, backing_path->dentry, &new_attr,
+			    NULL);
+	inode_unlock(d_inode(backing_path->dentry));
+
+	if (*out == 0 && (new_attr.ia_valid & ATTR_SIZE))
+		i_size_write(dentry->d_inode, new_attr.ia_size);
+	return 0;
+}
+
+static int fuse_setattr_finalize(struct bpf_fuse_args *fa, int *out,
+				 struct dentry *dentry, struct iattr *attr, struct file *file)
+{
+	return 0;
+}
+
+int fuse_bpf_setattr(int *out, struct inode *inode, struct dentry *dentry, struct iattr *attr, struct file *file)
+{
+	return bpf_fuse_backing(inode, struct fuse_setattr_args, out,
+				fuse_setattr_initialize_in, fuse_setattr_initialize_out,
+				fuse_setattr_backing, fuse_setattr_finalize,
+				dentry, attr, file);
+}
+
+static int fuse_statfs_initialize_in(struct bpf_fuse_args *fa, struct fuse_statfs_out *out,
+				     struct dentry *dentry, struct kstatfs *buf)
+{
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(d_inode(dentry)),
+			.opcode = FUSE_STATFS,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_statfs_initialize_out(struct bpf_fuse_args *fa, struct fuse_statfs_out *out,
+				      struct dentry *dentry, struct kstatfs *buf)
+{
+	*out = (struct fuse_statfs_out) { 0 };
+
+	fa->out_numargs = 1;
+	fa->out_args[0].size = sizeof(*out);
+	fa->out_args[0].value = out;
+
+	return 0;
+}
+
+static int fuse_statfs_backing(struct bpf_fuse_args *fa, int *out,
+			       struct dentry *dentry, struct kstatfs *buf)
+{
+	struct path backing_path;
+	struct fuse_statfs_out *fso = fa->out_args[0].value;
+
+	*out = 0;
+	get_fuse_backing_path(dentry, &backing_path);
+	if (!backing_path.dentry)
+		return -EBADF;
+	*out = vfs_statfs(&backing_path, buf);
+	path_put(&backing_path);
+	buf->f_type = FUSE_SUPER_MAGIC;
+
+	//TODO Provide postfilter opportunity to modify
+	if (!*out)
+		convert_statfs_to_fuse(&fso->st, buf);
+
+	return 0;
+}
+
+static int fuse_statfs_finalize(struct bpf_fuse_args *fa, int *out,
+				struct dentry *dentry, struct kstatfs *buf)
+{
+	struct fuse_statfs_out *fso = fa->out_args[0].value;
+
+	if (!fa->info.error_in)
+		convert_fuse_statfs(buf, &fso->st);
+	return 0;
+}
+
+int fuse_bpf_statfs(int *out, struct inode *inode, struct dentry *dentry, struct kstatfs *buf)
+{
+	return bpf_fuse_backing(dentry->d_inode, struct fuse_statfs_out, out,
+				fuse_statfs_initialize_in, fuse_statfs_initialize_out,
+				fuse_statfs_backing, fuse_statfs_finalize,
+				dentry, buf);
+}
+
 struct fuse_read_args {
 	struct fuse_read_in in;
 	struct fuse_read_out out;
fs/fuse/dir.c +12 -56

116 lines not on this page · raw message

fs/fuse/fuse_i.h +102 -0

120 lines not on this page · raw message

fs/fuse/inode.c +3 -14
index fd00910f1eb1..3dfb9cfb6e73 100644
@@ -623,20 +623,6 @@ static void fuse_send_destroy(struct fuse_mount *fm)
 	}
 }
 
-static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
-{
-	stbuf->f_type    = FUSE_SUPER_MAGIC;
-	stbuf->f_bsize   = attr->bsize;
-	stbuf->f_frsize  = attr->frsize;
-	stbuf->f_blocks  = attr->blocks;
-	stbuf->f_bfree   = attr->bfree;
-	stbuf->f_bavail  = attr->bavail;
-	stbuf->f_files   = attr->files;
-	stbuf->f_ffree   = attr->ffree;
-	stbuf->f_namelen = attr->namelen;
-	/* fsid is left zero */
-}
-
 static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
 	struct super_block *sb = dentry->d_sb;
@@ -650,6 +636,9 @@ static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
 		return 0;
 	}
 
+	if (fuse_bpf_statfs(&err, dentry->d_inode, dentry, buf))
+		return err;
+
 	memset(&outarg, 0, sizeof(outarg));
 	args.in_numargs = 0;
 	args.opcode = FUSE_STATFS;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#26 lore raw

RFC PATCH v3 22/37 fuse-bpf: Add support for FUSE_COPY_FILE_RANGE

In reply to Daniel Rosenberg

This adds backing support for FUSE_COPY_FILE_RANGE

---
 fs/fuse/backing.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/file.c    |  4 +++
 fs/fuse/fuse_i.h  | 10 ++++++
 3 files changed, 101 insertions(+)
fs/fuse/backing.c +87 -0
index 6a6130a16d2b..928b24db2303 100644
@@ -801,6 +801,93 @@ int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t o
 				file, offset, whence);
 }
 
+struct fuse_copy_file_range_args {
+	struct fuse_copy_file_range_in in;
+	struct fuse_write_out out;
+};
+
+static int fuse_copy_file_range_initialize_in(struct bpf_fuse_args *fa,
+					      struct fuse_copy_file_range_args *args,
+					      struct file *file_in, loff_t pos_in, struct file *file_out,
+					      loff_t pos_out, size_t len, unsigned int flags)
+{
+	struct fuse_file *fuse_file_in = file_in->private_data;
+	struct fuse_file *fuse_file_out = file_out->private_data;
+
+	args->in = (struct fuse_copy_file_range_in) {
+		.fh_in = fuse_file_in->fh,
+		.off_in = pos_in,
+		.nodeid_out = fuse_file_out->nodeid,
+		.fh_out = fuse_file_out->fh,
+		.off_out = pos_out,
+		.len = len,
+		.flags = flags,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(file_in->f_inode),
+			.opcode = FUSE_COPY_FILE_RANGE,
+		},
+		.in_numargs = 1,
+		.in_args[0].size = sizeof(args->in),
+		.in_args[0].value = &args->in,
+	};
+
+	return 0;
+}
+
+static int fuse_copy_file_range_initialize_out(struct bpf_fuse_args *fa,
+					       struct fuse_copy_file_range_args *args,
+					       struct file *file_in, loff_t pos_in, struct file *file_out,
+					       loff_t pos_out, size_t len, unsigned int flags)
+{
+	fa->out_numargs = 1;
+	fa->out_args[0].size = sizeof(args->out);
+	fa->out_args[0].value = &args->out;
+
+	return 0;
+}
+
+static int fuse_copy_file_range_backing(struct bpf_fuse_args *fa, ssize_t *out, struct file *file_in,
+					loff_t pos_in, struct file *file_out, loff_t pos_out, size_t len,
+					unsigned int flags)
+{
+	const struct fuse_copy_file_range_in *fci = fa->in_args[0].value;
+	struct fuse_file *fuse_file_in = file_in->private_data;
+	struct file *backing_file_in = fuse_file_in->backing_file;
+	struct fuse_file *fuse_file_out = file_out->private_data;
+	struct file *backing_file_out = fuse_file_out->backing_file;
+
+	/* TODO: Handle changing of in/out files */
+	if (backing_file_out)
+		*out = vfs_copy_file_range(backing_file_in, fci->off_in, backing_file_out,
+					   fci->off_out, fci->len, fci->flags);
+	else
+		*out = generic_copy_file_range(file_in, pos_in, file_out, pos_out, len,
+					       flags);
+	return 0;
+}
+
+static int fuse_copy_file_range_finalize(struct bpf_fuse_args *fa, ssize_t *out, struct file *file_in,
+					 loff_t pos_in, struct file *file_out, loff_t pos_out, size_t len,
+					 unsigned int flags)
+{
+	return 0;
+}
+
+int fuse_bpf_copy_file_range(ssize_t *out, struct inode *inode, struct file *file_in,
+			     loff_t pos_in, struct file *file_out, loff_t pos_out, size_t len,
+			     unsigned int flags)
+{
+	return bpf_fuse_backing(inode, struct fuse_copy_file_range_args, out,
+				fuse_copy_file_range_initialize_in,
+				fuse_copy_file_range_initialize_out,
+				fuse_copy_file_range_backing,
+				fuse_copy_file_range_finalize,
+				file_in, pos_in, file_out, pos_out, len, flags);
+}
+
 static int fuse_fsync_initialize_in(struct bpf_fuse_args *fa, struct fuse_fsync_in *in,
 				    struct file *file, loff_t start, loff_t end, int datasync)
 {
fs/fuse/file.c +4 -0
index a4a0aeb28e4a..8179afe28c6f 100644
@@ -3199,6 +3199,10 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
 	bool is_unstable = (!fc->writeback_cache) &&
 			   ((pos_out + len) > inode_out->i_size);
 
+	if (fuse_bpf_copy_file_range(&err, file_inode(file_in), file_in, pos_in,
+				     file_out, pos_out, len, flags))
+		return err;
+
 	if (fc->no_copy_file_range)
 		return -EOPNOTSUPP;
 
fs/fuse/fuse_i.h +10 -0
index 17899a1fe885..74540f308636 100644
@@ -1421,6 +1421,9 @@ int fuse_bpf_release(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_flush(int *out, struct inode *inode, struct file *file, fl_owner_t id);
 int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t offset, int whence);
+int fuse_bpf_copy_file_range(ssize_t *out, struct inode *inode, struct file *file_in, loff_t pos_in,
+			     struct file *file_out, loff_t pos_out,
+			     size_t len, unsigned int flags);
 int fuse_bpf_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync);
 int fuse_bpf_dir_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync);
 int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *to);
@@ -1500,6 +1503,13 @@ static inline int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *
 	return 0;
 }
 
+static inline int fuse_bpf_copy_file_range(ssize_t *out, struct inode *inode, struct file *file_in, loff_t pos_in,
+					   struct file *file_out, loff_t pos_out,
+					   size_t len, unsigned int flags)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync)
 {
 	return 0;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#27 lore raw

RFC PATCH v3 23/37 fuse-bpf: Add xattr support

In reply to Daniel Rosenberg

This adds support for FUSE_GETXATTR, FUSE_LISTXATTR, FUSE_SETXATTR, and FUSE_REMOVEXATTR

---
 fs/fuse/backing.c | 349 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/fuse_i.h  |  30 ++++
 fs/fuse/xattr.c   |  18 +++
 3 files changed, 397 insertions(+)
fs/fuse/backing.c +349 -0
index 928b24db2303..eb3eb184c867 100644
@@ -982,6 +982,355 @@ int fuse_bpf_dir_fsync(int *out, struct inode *inode, struct file *file, loff_t
 				file, start, end, datasync);
 }
 
+struct fuse_getxattr_args {
+	struct fuse_getxattr_in in;
+	struct fuse_buffer name;
+	struct fuse_buffer value;
+	struct fuse_getxattr_out out;
+};
+
+static int fuse_getxattr_initialize_in(struct bpf_fuse_args *fa,
+				       struct fuse_getxattr_args *args,
+				       struct dentry *dentry, const char *name, void *value,
+				       size_t size)
+{
+	*args = (struct fuse_getxattr_args) {
+		.in.size = size,
+		.name = (struct fuse_buffer) {
+			.data =  (void *) name,
+			.size = strlen(name) + 1,
+			.max_size = XATTR_NAME_MAX + 1,
+			.flags = BPF_FUSE_MUST_ALLOCATE | BPF_FUSE_VARIABLE_SIZE,
+		},
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_fuse_inode(dentry->d_inode)->nodeid,
+			.opcode = FUSE_GETXATTR,
+		},
+		.in_numargs = 2,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+		.in_args[1] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->name,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_getxattr_initialize_out(struct bpf_fuse_args *fa,
+					struct fuse_getxattr_args *args,
+					struct dentry *dentry, const char *name, void *value,
+					size_t size)
+{
+	fa->flags = size ? FUSE_BPF_OUT_ARGVAR : 0;
+	fa->out_numargs = 1;
+	if (size) {
+		args->value = (struct fuse_buffer) {
+			.data =  (void *) value,
+			.size = size,
+			.alloc_size = size,
+			.max_size = size,
+			.flags = BPF_FUSE_VARIABLE_SIZE,
+		};
+		fa->out_args[0].is_buffer = true;
+		fa->out_args[0].buffer = &args->value;
+	} else {
+		fa->out_args[0].size = sizeof(args->out);
+		fa->out_args[0].value = &args->out;
+	}
+	return 0;
+}
+
+static int fuse_getxattr_backing(struct bpf_fuse_args *fa, int *out,
+				 struct dentry *dentry, const char *name, void *value,
+				 size_t size)
+{
+	ssize_t ret;
+
+	if (fa->in_args[1].buffer->flags & BPF_FUSE_MODIFIED) {
+		// Ensure bpf provided string is null terminated
+		char *new_name = fa->in_args[1].buffer->data;
+		new_name[fa->in_args[1].buffer->size - 1] = 0;
+	}
+	ret = vfs_getxattr(&nop_mnt_idmap,
+				   get_fuse_dentry(dentry)->backing_path.dentry,
+				   fa->in_args[1].buffer->data, value, size);
+
+	if (fa->flags & FUSE_BPF_OUT_ARGVAR)
+		fa->out_args[0].buffer->size = ret;
+	else
+		((struct fuse_getxattr_out *)fa->out_args[0].value)->size = ret;
+
+	return 0;
+}
+
+static int fuse_getxattr_finalize(struct bpf_fuse_args *fa, int *out,
+				  struct dentry *dentry, const char *name, void *value,
+				  size_t size)
+{
+	struct fuse_getxattr_out *fgo;
+
+	if (fa->flags & FUSE_BPF_OUT_ARGVAR) {
+		*out = fa->out_args[0].buffer->size;
+		return 0;
+	}
+
+	fgo = fa->out_args[0].value;
+
+	*out = fgo->size;
+	return 0;
+}
+
+int fuse_bpf_getxattr(int *out, struct inode *inode, struct dentry *dentry, const char *name,
+		      void *value, size_t size)
+{
+	return bpf_fuse_backing(inode, struct fuse_getxattr_args, out,
+				fuse_getxattr_initialize_in, fuse_getxattr_initialize_out,
+				fuse_getxattr_backing, fuse_getxattr_finalize,
+				dentry, name, value, size);
+}
+
+static int fuse_listxattr_initialize_in(struct bpf_fuse_args *fa,
+					struct fuse_getxattr_args *args,
+					struct dentry *dentry, char *list, size_t size)
+{
+	*args = (struct fuse_getxattr_args) {
+		.in.size = size,
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_fuse_inode(dentry->d_inode)->nodeid,
+			.opcode = FUSE_LISTXATTR,
+		},
+		.in_numargs = 1,
+		.in_args[0] =
+			(struct bpf_fuse_arg) {
+				.size = sizeof(args->in),
+				.value = &args->in,
+			},
+	};
+
+	return 0;
+}
+
+static int fuse_listxattr_initialize_out(struct bpf_fuse_args *fa,
+					 struct fuse_getxattr_args *args,
+					 struct dentry *dentry, char *list, size_t size)
+{
+	fa->out_numargs = 1;
+
+	if (size) {
+		args->value = (struct fuse_buffer) {
+			.data = list,
+			.size = size,
+			.alloc_size = size,
+			.max_size = size,
+			.flags = BPF_FUSE_VARIABLE_SIZE,
+		};
+		fa->flags = FUSE_BPF_OUT_ARGVAR;
+		fa->out_args[0].is_buffer = true;
+		fa->out_args[0].buffer = &args->value;
+	} else {
+		fa->out_args[0].size = sizeof(args->out);
+		fa->out_args[0].value = &args->out;
+	}
+	return 0;
+}
+
+static int fuse_listxattr_backing(struct bpf_fuse_args *fa, ssize_t *out, struct dentry *dentry,
+				  char *list, size_t size)
+{
+	*out = vfs_listxattr(get_fuse_dentry(dentry)->backing_path.dentry, list, size);
+
+	if (*out < 0)
+		return *out;
+
+	if (fa->flags & FUSE_BPF_OUT_ARGVAR)
+		fa->out_args[0].buffer->size = *out;
+	else
+		((struct fuse_getxattr_out *)fa->out_args[0].value)->size = *out;
+
+	return 0;
+}
+
+static int fuse_listxattr_finalize(struct bpf_fuse_args *fa, ssize_t *out, struct dentry *dentry,
+				   char *list, size_t size)
+{
+	struct fuse_getxattr_out *fgo;
+
+	if (fa->info.error_in)
+		return 0;
+
+	if (fa->flags & FUSE_BPF_OUT_ARGVAR) {
+		*out = fa->out_args[0].buffer->size;
+		return 0;
+	}
+
+	fgo = fa->out_args[0].value;
+	*out = fgo->size;
+	return 0;
+}
+
+int fuse_bpf_listxattr(ssize_t *out, struct inode *inode, struct dentry *dentry,
+		       char *list, size_t size)
+{
+	return bpf_fuse_backing(inode, struct fuse_getxattr_args, out,
+				fuse_listxattr_initialize_in, fuse_listxattr_initialize_out,
+				fuse_listxattr_backing, fuse_listxattr_finalize,
+				dentry, list, size);
+}
+
+struct fuse_setxattr_args {
+	struct fuse_setxattr_in in;
+	struct fuse_buffer name;
+	struct fuse_buffer value;
+};
+
+static int fuse_setxattr_initialize_in(struct bpf_fuse_args *fa,
+				       struct fuse_setxattr_args *args,
+				       struct dentry *dentry, const char *name,
+				       const void *value, size_t size, int flags)
+{
+	*args = (struct fuse_setxattr_args) {
+		.in = (struct fuse_setxattr_in) {
+			.size = size,
+			.flags = flags,
+		},
+		.name = (struct fuse_buffer) {
+			.data = (void *) name,
+			.size = strlen(name) + 1,
+			.max_size = XATTR_NAME_MAX + 1,
+			.flags = BPF_FUSE_VARIABLE_SIZE | BPF_FUSE_MUST_ALLOCATE,
+		},
+		.value =(struct fuse_buffer) {
+			.data = (void *) value,
+			.size = size,
+			.max_size = XATTR_SIZE_MAX,
+			.flags = BPF_FUSE_VARIABLE_SIZE | BPF_FUSE_MUST_ALLOCATE,
+		},
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_fuse_inode(dentry->d_inode)->nodeid,
+			.opcode = FUSE_SETXATTR,
+		},
+		.in_numargs = 3,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+		.in_args[1] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->name,
+		},
+		.in_args[2] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->value,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_setxattr_initialize_out(struct bpf_fuse_args *fa,
+					struct fuse_setxattr_args *args,
+					struct dentry *dentry, const char *name,
+					const void *value, size_t size, int flags)
+{
+	return 0;
+}
+
+static int fuse_setxattr_backing(struct bpf_fuse_args *fa, int *out, struct dentry *dentry,
+				 const char *name, const void *value, size_t size,
+				 int flags)
+{
+	// TODO Ensure we actually use filter values
+	*out = vfs_setxattr(&nop_mnt_idmap,
+			    get_fuse_dentry(dentry)->backing_path.dentry, name,
+			    value, size, flags);
+	return 0;
+}
+
+static int fuse_setxattr_finalize(struct bpf_fuse_args *fa, int *out, struct dentry *dentry,
+				  const char *name, const void *value, size_t size,
+				  int flags)
+{
+	return 0;
+}
+
+int fuse_bpf_setxattr(int *out, struct inode *inode, struct dentry *dentry,
+		      const char *name, const void *value, size_t size, int flags)
+{
+	return bpf_fuse_backing(inode, struct fuse_setxattr_args, out,
+			       fuse_setxattr_initialize_in, fuse_setxattr_initialize_out,
+			       fuse_setxattr_backing, fuse_setxattr_finalize,
+			       dentry, name, value, size, flags);
+}
+
+static int fuse_removexattr_initialize_in(struct bpf_fuse_args *fa,
+					  struct fuse_buffer *in,
+					  struct dentry *dentry, const char *name)
+{
+	*in = (struct fuse_buffer) {
+		.data = (void *) name,
+		.size = strlen(name) + 1,
+		.max_size = XATTR_NAME_MAX + 1,
+		.flags = BPF_FUSE_VARIABLE_SIZE | BPF_FUSE_MUST_ALLOCATE,
+	};
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_fuse_inode(dentry->d_inode)->nodeid,
+			.opcode = FUSE_REMOVEXATTR,
+		},
+		.in_numargs = 1,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = in,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_removexattr_initialize_out(struct bpf_fuse_args *fa,
+					   struct fuse_buffer *in,
+					   struct dentry *dentry, const char *name)
+{
+	return 0;
+}
+
+static int fuse_removexattr_backing(struct bpf_fuse_args *fa, int *out,
+				    struct dentry *dentry, const char *name)
+{
+	struct path *backing_path = &get_fuse_dentry(dentry)->backing_path;
+
+	/* TODO account for changes of the name by prefilter */
+	*out = vfs_removexattr(&nop_mnt_idmap, backing_path->dentry, name);
+	return 0;
+}
+
+static int fuse_removexattr_finalize(struct bpf_fuse_args *fa, int *out,
+				     struct dentry *dentry, const char *name)
+{
+	return 0;
+}
+
+int fuse_bpf_removexattr(int *out, struct inode *inode, struct dentry *dentry, const char *name)
+{
+	return bpf_fuse_backing(inode, struct fuse_buffer, out,
+				fuse_removexattr_initialize_in, fuse_removexattr_initialize_out,
+				fuse_removexattr_backing, fuse_removexattr_finalize,
+				dentry, name);
+}
+
 static inline void fuse_bpf_aio_put(struct fuse_bpf_aio_req *aio_req)
 {
 	if (refcount_dec_and_test(&aio_req->ref))
fs/fuse/fuse_i.h +30 -0
index 74540f308636..243a8fe0c343 100644
@@ -1426,6 +1426,13 @@ int fuse_bpf_copy_file_range(ssize_t *out, struct inode *inode, struct file *fil
 			     size_t len, unsigned int flags);
 int fuse_bpf_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync);
 int fuse_bpf_dir_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync);
+int fuse_bpf_getxattr(int *out, struct inode *inode, struct dentry *dentry,
+		      const char *name, void *value, size_t size);
+int fuse_bpf_listxattr(ssize_t *out, struct inode *inode, struct dentry *dentry, char *list, size_t size);
+int fuse_bpf_setxattr(int *out, struct inode *inode, struct dentry *dentry,
+		      const char *name, const void *value, size_t size,
+		      int flags);
+int fuse_bpf_removexattr(int *out, struct inode *inode, struct dentry *dentry, const char *name);
 int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *to);
 int fuse_bpf_file_write_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *from);
 int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, int mode, loff_t offset, loff_t length);
@@ -1520,6 +1527,29 @@ static inline int fuse_bpf_dir_fsync(int *out, struct inode *inode, struct file
 	return 0;
 }
 
+static inline int fuse_bpf_getxattr(int *out, struct inode *inode, struct dentry *dentry,
+				    const char *name, void *value, size_t size)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_listxattr(ssize_t *out, struct inode *inode, struct dentry *dentry, char *list, size_t size)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_setxattr(int *out, struct inode *inode, struct dentry *dentry,
+				    const char *name, const void *value, size_t size,
+				    int flags)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_removexattr(int *out, struct inode *inode, struct dentry *dentry, const char *name)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *iocb, struct iov_iter *to)
 {
 	return 0;
fs/fuse/xattr.c +18 -0
index 49c01559580f..d00f7dc50038 100644
@@ -118,6 +118,9 @@ ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_bpf_listxattr(&ret, inode, entry, list, size))
+		return ret;
+
 	if (!fuse_allow_current_process(fm->fc))
 		return -EACCES;
 
@@ -182,9 +185,14 @@ static int fuse_xattr_get(const struct xattr_handler *handler,
 			 struct dentry *dentry, struct inode *inode,
 			 const char *name, void *value, size_t size)
 {
+	int err;
+
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (fuse_bpf_getxattr(&err, inode, dentry, name, value, size))
+		return err;
+
 	return fuse_getxattr(inode, name, value, size);
 }
 
@@ -194,9 +202,19 @@ static int fuse_xattr_set(const struct xattr_handler *handler,
 			  const char *name, const void *value, size_t size,
 			  int flags)
 {
+	int err;
+	bool handled;
+
 	if (fuse_is_bad(inode))
 		return -EIO;
 
+	if (value)
+		handled = fuse_bpf_setxattr(&err, inode, dentry, name, value, size, flags);
+	else
+		handled = fuse_bpf_removexattr(&err, inode, dentry, name);
+	if (handled)
+		return err;
+
 	if (!value)
 		return fuse_removexattr(inode, name);
 
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#28 lore raw

RFC PATCH v3 24/37 fuse-bpf: Add symlink/link support

In reply to Daniel Rosenberg

This adds backing support for FUSE_LINK, FUSE_READLINK, and FUSE_SYMLINK

---
 fs/fuse/backing.c | 327 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dir.c     |  11 ++
 fs/fuse/fuse_i.h  |  20 +++
 3 files changed, 358 insertions(+)
fs/fuse/backing.c +327 -0
index eb3eb184c867..e807ae4f6f53 100644
@@ -2502,6 +2502,125 @@ int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry)
 				dir, entry);
 }
 
+struct fuse_link_args {
+	struct fuse_link_in in;
+	struct fuse_buffer name;
+};
+
+static int fuse_link_initialize_in(struct bpf_fuse_args *fa, struct fuse_link_args *args,
+				   struct dentry *entry, struct inode *dir,
+				   struct dentry *newent)
+{
+	struct inode *src_inode = entry->d_inode;
+
+	*args = (struct fuse_link_args) {
+		.in = (struct fuse_link_in) {
+			.oldnodeid = get_node_id(src_inode),
+		},
+		.name = (struct fuse_buffer) {
+			.data = (void *) newent->d_name.name,
+			.size = newent->d_name.len + 1,
+			.max_size = NAME_MAX + 1,
+			.flags = BPF_FUSE_VARIABLE_SIZE | BPF_FUSE_MUST_ALLOCATE,
+		},
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.opcode = FUSE_LINK,
+		},
+		.in_numargs = 2,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.size = sizeof(args->in),
+			.value = &args->in,
+		},
+		.in_args[1] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->name,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_link_initialize_out(struct bpf_fuse_args *fa, struct fuse_link_args *args,
+				    struct dentry *entry, struct inode *dir,
+				    struct dentry *newent)
+{
+	return 0;
+}
+
+static int fuse_link_backing(struct bpf_fuse_args *fa, int *out, struct dentry *entry,
+			     struct inode *dir, struct dentry *newent)
+{
+	struct path backing_old_path;
+	struct path backing_new_path;
+	struct dentry *backing_dir_dentry;
+	struct inode *fuse_new_inode = NULL;
+	struct fuse_inode *fuse_dir_inode = get_fuse_inode(dir);
+	struct inode *backing_dir_inode = fuse_dir_inode->backing_inode;
+
+	*out = 0;
+	get_fuse_backing_path(entry, &backing_old_path);
+	if (!backing_old_path.dentry)
+		return -EBADF;
+
+	get_fuse_backing_path(newent, &backing_new_path);
+	if (!backing_new_path.dentry) {
+		*out = -EBADF;
+		goto err_dst_path;
+	}
+
+	backing_dir_dentry = dget_parent(backing_new_path.dentry);
+	backing_dir_inode = d_inode(backing_dir_dentry);
+
+	inode_lock_nested(backing_dir_inode, I_MUTEX_PARENT);
+	*out = vfs_link(backing_old_path.dentry, &nop_mnt_idmap,
+		       backing_dir_inode, backing_new_path.dentry, NULL);
+	inode_unlock(backing_dir_inode);
+	if (*out)
+		goto out;
+
+	if (d_really_is_negative(backing_new_path.dentry) ||
+	    unlikely(d_unhashed(backing_new_path.dentry))) {
+		*out = -EINVAL;
+		/**
+		 * TODO: overlayfs responds to this situation with a
+		 * lookupOneLen. Should we do that too?
+		 */
+		goto out;
+	}
+
+	fuse_new_inode = fuse_iget_backing(dir->i_sb, fuse_dir_inode->nodeid, backing_dir_inode);
+	if (IS_ERR(fuse_new_inode)) {
+		*out = PTR_ERR(fuse_new_inode);
+		goto out;
+	}
+	d_instantiate(newent, fuse_new_inode);
+
+out:
+	dput(backing_dir_dentry);
+	path_put(&backing_new_path);
+err_dst_path:
+	path_put(&backing_old_path);
+	return *out;
+}
+
+static int fuse_link_finalize(struct bpf_fuse_args *fa, int *out, struct dentry *entry,
+			      struct inode *dir, struct dentry *newent)
+{
+	return 0;
+}
+
+int fuse_bpf_link(int *out, struct inode *inode, struct dentry *entry,
+		  struct inode *newdir, struct dentry *newent)
+{
+	return bpf_fuse_backing(inode, struct fuse_link_args, out,
+				fuse_link_initialize_in, fuse_link_initialize_out,
+				fuse_link_backing, fuse_link_finalize,
+				entry, newdir, newent);
+}
+
 struct fuse_getattr_args {
 	struct fuse_getattr_in in;
 	struct fuse_attr_out out;
@@ -2790,6 +2909,214 @@ int fuse_bpf_statfs(int *out, struct inode *inode, struct dentry *dentry, struct
 				dentry, buf);
 }
 
+struct fuse_get_link_args {
+	struct fuse_buffer name;
+	struct fuse_buffer path;
+};
+
+static int fuse_get_link_initialize_in(struct bpf_fuse_args *fa, struct fuse_get_link_args *args,
+				       struct inode *inode, struct dentry *dentry,
+				       struct delayed_call *callback)
+{
+	/*
+	 * TODO
+	 * If we want to handle changing these things, we'll need to copy
+	 * the lower fs's data into our own buffer, and provide our own callback
+	 * to free that buffer.
+	 *
+	 * Pre could change the name we're looking at
+	 * postfilter can change the name we return
+	 *
+	 * We ought to only make that buffer if it's been requested, so leaving
+	 * this unimplemented for the moment
+	 */
+	*args = (struct fuse_get_link_args) {
+		.name = (struct fuse_buffer) {
+			.data =  (void *) dentry->d_name.name,
+			.size = dentry->d_name.len + 1,
+			.max_size = NAME_MAX + 1,
+			.flags = BPF_FUSE_VARIABLE_SIZE | BPF_FUSE_MUST_ALLOCATE,
+		},
+	};
+
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.opcode = FUSE_READLINK,
+			.nodeid = get_node_id(inode),
+		},
+		.in_numargs = 1,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->name,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_get_link_initialize_out(struct bpf_fuse_args *fa, struct fuse_get_link_args *args,
+					struct inode *inode, struct dentry *dentry,
+					struct delayed_call *callback)
+{
+	// TODO
+#if 0
+	args->path = (struct fuse_buffer) {
+		.data =  NULL,
+		.size = 0,
+		.max_size = PATH_MAX,
+		.flags = BPF_FUSE_VARIABLE_SIZE | BPF_FUSE_MUST_ALLOCATE,
+	};
+	fa->out_numargs = 1;
+	fa->out_args[0].is_buffer = true;
+	fa->out_args[0].buffer = &args->path;
+#endif
+
+	return 0;
+}
+
+static int fuse_get_link_backing(struct bpf_fuse_args *fa, const char **out,
+				 struct inode *inode, struct dentry *dentry,
+				 struct delayed_call *callback)
+{
+	struct path backing_path;
+
+	if (!dentry) {
+		*out = ERR_PTR(-ECHILD);
+		return PTR_ERR(*out);
+	}
+
+	get_fuse_backing_path(dentry, &backing_path);
+	if (!backing_path.dentry) {
+		*out = ERR_PTR(-ECHILD);
+		return PTR_ERR(*out);
+	}
+
+	/*
+	 * TODO: If we want to do our own thing, copy the data and then call the
+	 * callback
+	 */
+	*out = vfs_get_link(backing_path.dentry, callback);
+
+	path_put(&backing_path);
+	return 0;
+}
+
+static int fuse_get_link_finalize(struct bpf_fuse_args *fa, const char **out,
+				  struct inode *inode, struct dentry *dentry,
+				  struct delayed_call *callback)
+{
+	return 0;
+}
+
+int fuse_bpf_get_link(const char **out, struct inode *inode, struct dentry *dentry,
+		      struct delayed_call *callback)
+{
+	return bpf_fuse_backing(inode, struct fuse_get_link_args, out,
+				fuse_get_link_initialize_in, fuse_get_link_initialize_out,
+				fuse_get_link_backing, fuse_get_link_finalize,
+				inode, dentry, callback);
+}
+
+struct fuse_symlink_args {
+	struct fuse_buffer name;
+	struct fuse_buffer path;
+};
+
+static int fuse_symlink_initialize_in(struct bpf_fuse_args *fa, struct fuse_symlink_args *args,
+				      struct inode *dir, struct dentry *entry, const char *link, int len)
+{
+	*args = (struct fuse_symlink_args) {
+		.name = (struct fuse_buffer) {
+			.data =  (void *) entry->d_name.name,
+			.size = entry->d_name.len + 1,
+			.flags = BPF_FUSE_IMMUTABLE,
+		},
+		.path = (struct fuse_buffer) {
+			.data = (void *) link,
+			.size = len,
+			.max_size = PATH_MAX,
+			.flags = BPF_FUSE_VARIABLE_SIZE | BPF_FUSE_MUST_ALLOCATE,
+		},
+	};
+	*fa = (struct bpf_fuse_args) {
+		.info = (struct bpf_fuse_meta_info) {
+			.nodeid = get_node_id(dir),
+			.opcode = FUSE_SYMLINK,
+		},
+		.in_numargs = 2,
+		.in_args[0] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->name,
+		},
+		.in_args[1] = (struct bpf_fuse_arg) {
+			.is_buffer = true,
+			.buffer = &args->path,
+		},
+	};
+
+	return 0;
+}
+
+static int fuse_symlink_initialize_out(struct bpf_fuse_args *fa, struct fuse_symlink_args *args,
+				       struct inode *dir, struct dentry *entry, const char *link, int len)
+{
+	return 0;
+}
+
+static int fuse_symlink_backing(struct bpf_fuse_args *fa, int *out,
+				struct inode *dir, struct dentry *entry, const char *link, int len)
+{
+	struct fuse_inode *fuse_inode = get_fuse_inode(dir);
+	struct inode *backing_inode = fuse_inode->backing_inode;
+	struct path backing_path;
+	struct inode *inode = NULL;
+
+	*out = 0;
+	//TODO Actually deal with changing the backing entry in symlink
+	get_fuse_backing_path(entry, &backing_path);
+	if (!backing_path.dentry)
+		return -EBADF;
+
+	inode_lock_nested(backing_inode, I_MUTEX_PARENT);
+	*out = vfs_symlink(&nop_mnt_idmap, backing_inode, backing_path.dentry,
+			  link);
+	inode_unlock(backing_inode);
+	if (*out)
+		goto out;
+	if (d_really_is_negative(backing_path.dentry) ||
+	    unlikely(d_unhashed(backing_path.dentry))) {
+		*out = -EINVAL;
+		/**
+		 * TODO: overlayfs responds to this situation with a
+		 * lookupOneLen. Should we do that too?
+		 */
+		goto out;
+	}
+	inode = fuse_iget_backing(dir->i_sb, fuse_inode->nodeid, backing_inode);
+	if (IS_ERR(inode)) {
+		*out = PTR_ERR(inode);
+		goto out;
+	}
+	d_instantiate(entry, inode);
+out:
+	path_put(&backing_path);
+	return *out;
+}
+
+static int  fuse_symlink_finalize(struct bpf_fuse_args *fa, int *out,
+				  struct inode *dir, struct dentry *entry, const char *link, int len)
+{
+	return 0;
+}
+
+int  fuse_bpf_symlink(int *out, struct inode *dir, struct dentry *entry, const char *link, int len)
+{
+	return bpf_fuse_backing(dir, struct fuse_symlink_args, out,
+				fuse_symlink_initialize_in, fuse_symlink_initialize_out,
+				fuse_symlink_backing, fuse_symlink_finalize,
+				dir, entry, link, len);
+}
+
 struct fuse_read_args {
 	struct fuse_read_in in;
 	struct fuse_read_out out;
fs/fuse/dir.c +11 -0
index 7d589241c9b0..d1c3b2bfb0b1 100644
@@ -1013,6 +1013,10 @@ static int fuse_symlink(struct mnt_idmap *idmap, struct inode *dir,
 	struct fuse_mount *fm = get_fuse_mount(dir);
 	unsigned len = strlen(link) + 1;
 	FUSE_ARGS(args);
+	int err;
+
+	if (fuse_bpf_symlink(&err, dir, entry, link, len))
+		return err;
 
 	args.opcode = FUSE_SYMLINK;
 	args.in_numargs = 2;
@@ -1219,6 +1223,9 @@ static int fuse_link(struct dentry *entry, struct inode *newdir,
 	struct fuse_mount *fm = get_fuse_mount(inode);
 	FUSE_ARGS(args);
 
+	if (fuse_bpf_link(&err, inode, entry, newdir, newent))
+		return err;
+
 	memset(&inarg, 0, sizeof(inarg));
 	inarg.oldnodeid = get_node_id(inode);
 	args.opcode = FUSE_LINK;
@@ -1618,12 +1625,16 @@ static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
 {
 	struct fuse_conn *fc = get_fuse_conn(inode);
 	struct page *page;
+	const char *out = NULL;
 	int err;
 
 	err = -EIO;
 	if (fuse_is_bad(inode))
 		goto out_err;
 
+	if (fuse_bpf_get_link(&out, inode, dentry, callback))
+		return out;
+
 	if (fc->cache_symlinks)
 		return page_get_link(dentry, inode, callback);
 
fs/fuse/fuse_i.h +20 -0
index 243a8fe0c343..121d31a04e79 100644
@@ -1417,6 +1417,7 @@ int fuse_bpf_rename2(int *out, struct inode *olddir, struct dentry *oldent,
 int fuse_bpf_rename(int *out, struct inode *olddir, struct dentry *oldent,
 		    struct inode *newdir, struct dentry *newent);
 int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry);
+int fuse_bpf_link(int *out, struct inode *inode, struct dentry *entry, struct inode *dir, struct dentry *newent);
 int fuse_bpf_release(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file);
 int fuse_bpf_flush(int *out, struct inode *inode, struct file *file, fl_owner_t id);
@@ -1441,6 +1442,9 @@ int fuse_bpf_getattr(int *out, struct inode *inode, const struct dentry *entry,
 		     u32 request_mask, unsigned int flags);
 int fuse_bpf_setattr(int *out, struct inode *inode, struct dentry *dentry, struct iattr *attr, struct file *file);
 int fuse_bpf_statfs(int *out, struct inode *inode, struct dentry *dentry, struct kstatfs *buf);
+int fuse_bpf_get_link(const char **out, struct inode *inode, struct dentry *dentry,
+		      struct delayed_call *callback);
+int fuse_bpf_symlink(int *out, struct inode *dir, struct dentry *entry, const char *link, int len);
 int fuse_bpf_readdir(int *out, struct inode *inode, struct file *file, struct dir_context *ctx);
 int fuse_bpf_access(int *out, struct inode *inode, int mask);
 
@@ -1490,6 +1494,11 @@ static inline int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *en
 	return 0;
 }
 
+static inline int fuse_bpf_link(int *out, struct inode *inode, struct dentry *entry, struct inode *dir, struct dentry *newent)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_release(int *out, struct inode *inode, struct file *file)
 {
 	return 0;
@@ -1586,6 +1595,17 @@ static inline int fuse_bpf_statfs(int *out, struct inode *inode, struct dentry *
 	return 0;
 }
 
+static inline int fuse_bpf_get_link(const char **out, struct inode *inode, struct dentry *dentry,
+		      struct delayed_call *callback)
+{
+	return 0;
+}
+
+static inline int fuse_bpf_symlink(int *out, struct inode *dir, struct dentry *entry, const char *link, int len)
+{
+	return 0;
+}
+
 static inline int fuse_bpf_readdir(int *out, struct inode *inode, struct file *file, struct dir_context *ctx)
 {
 	return 0;
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#29 lore raw

RFC PATCH v3 25/37 fuse-bpf: allow mounting with no userspace daemon

In reply to Daniel Rosenberg

When using fuse-bpf in pure passthrough mode, we don't explicitly need a userspace daemon. This allows simple testing of the backing operations.

---
 fs/fuse/fuse_i.h |  4 ++++
 fs/fuse/inode.c  | 25 +++++++++++++++++++------
 2 files changed, 23 insertions(+), 6 deletions(-)
fs/fuse/fuse_i.h +4 -0
index 121d31a04e79..2bd45c8658e8 100644
@@ -566,6 +566,7 @@ struct fuse_fs_context {
 	bool no_control:1;
 	bool no_force_umount:1;
 	bool legacy_opts_show:1;
+	bool no_daemon:1;
 	enum fuse_dax_mode dax_mode;
 	unsigned int max_read;
 	unsigned int blksize;
@@ -847,6 +848,9 @@ struct fuse_conn {
 	/* Is tmpfile not implemented by fs? */
 	unsigned int no_tmpfile:1;
 
+	/** BPF Only, no Daemon running */
+	unsigned int no_daemon:1;
+
 	/** The number of requests waiting for completion */
 	atomic_t num_waiting;
 
fs/fuse/inode.c +19 -6
index 3dfb9cfb6e73..31f34962bc9b 100644
@@ -756,6 +756,7 @@ enum {
 	OPT_MAX_READ,
 	OPT_BLKSIZE,
 	OPT_ROOT_DIR,
+	OPT_NO_DAEMON,
 	OPT_ERR
 };
 
@@ -771,6 +772,7 @@ static const struct fs_parameter_spec fuse_fs_parameters[] = {
 	fsparam_u32	("blksize",		OPT_BLKSIZE),
 	fsparam_string	("subtype",		OPT_SUBTYPE),
 	fsparam_u32	("root_dir",		OPT_ROOT_DIR),
+	fsparam_flag	("no_daemon",		OPT_NO_DAEMON),
 	{}
 };
 
@@ -860,6 +862,11 @@ static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param)
 			return invalfc(fsc, "Unable to open root directory");
 		break;
 
+	case OPT_NO_DAEMON:
+		ctx->no_daemon = true;
+		ctx->fd_present = true;
+		break;
+
 	default:
 		return -EINVAL;
 	}
@@ -1419,7 +1426,7 @@ void fuse_send_init(struct fuse_mount *fm)
 	ia->args.nocreds = true;
 	ia->args.end = process_init_reply;
 
-	if (fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
+	if (unlikely(fm->fc->no_daemon) || fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
 		process_init_reply(fm, &ia->args, -ENOTCONN);
 }
 EXPORT_SYMBOL_GPL(fuse_send_init);
@@ -1694,6 +1701,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
 	fc->destroy = ctx->destroy;
 	fc->no_control = ctx->no_control;
 	fc->no_force_umount = ctx->no_force_umount;
+	fc->no_daemon = ctx->no_daemon;
 
 	err = -ENOMEM;
 	root = fuse_get_root_inode(sb, ctx->rootmode, ctx->root_dir);
@@ -1740,7 +1748,7 @@ static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
 	struct fuse_fs_context *ctx = fsc->fs_private;
 	int err;
 
-	if (!ctx->file || !ctx->rootmode_present ||
+	if (!!ctx->file == ctx->no_daemon || !ctx->rootmode_present ||
 	    !ctx->user_id_present || !ctx->group_id_present)
 		return -EINVAL;
 
@@ -1748,10 +1756,12 @@ static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
 	 * Require mount to happen from the same user namespace which
 	 * opened /dev/fuse to prevent potential attacks.
 	 */
-	if ((ctx->file->f_op != &fuse_dev_operations) ||
-	    (ctx->file->f_cred->user_ns != sb->s_user_ns))
-		return -EINVAL;
-	ctx->fudptr = &ctx->file->private_data;
+	if (ctx->file) {
+		if ((ctx->file->f_op != &fuse_dev_operations) ||
+		    (ctx->file->f_cred->user_ns != sb->s_user_ns))
+			return -EINVAL;
+		ctx->fudptr = &ctx->file->private_data;
+	}
 
 	err = fuse_fill_super_common(sb, ctx);
 	if (err)
@@ -1801,6 +1811,9 @@ static int fuse_get_tree(struct fs_context *fsc)
 
 	fsc->s_fs_info = fm;
 
+	if (ctx->no_daemon)
+		return get_tree_nodev(fsc, fuse_fill_super);;
+
 	if (ctx->fd_present)
 		ctx->file = fget(ctx->fd);
 
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#30 lore raw

RFC PATCH v3 26/37 bpf: Increase struct_op limits

In reply to Daniel Rosenberg

Fuse bpf goes a bit past the '64' limit here, although in reality, this limit seems to be more like 37. After 37, we start overrunning the safety checks while setting up the trampoline.

This simply doubles some of these values. This will have the same issue, as we'll run out of space way before hitting the 128 limit, but for now that unblocks fuse-bpf.

---
 include/linux/bpf.h         | 2 +-
 kernel/bpf/bpf_struct_ops.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/bpf.h +1 -1
index 18b592fde896..c006f823e634 100644
@@ -1537,7 +1537,7 @@ struct bpf_link_primer {
 struct bpf_struct_ops_value;
 struct btf_member;
 
-#define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
+#define BPF_STRUCT_OPS_MAX_NR_MEMBERS 128
 struct bpf_struct_ops {
 	const struct bpf_verifier_ops *verifier_ops;
 	int (*init)(struct btf *btf);
kernel/bpf/bpf_struct_ops.c +2 -2
index d3f0a4825fa6..deb9eecaf1e4 100644
@@ -417,7 +417,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
 	udata = &uvalue->data;
 	kdata = &kvalue->data;
 	image = st_map->image;
-	image_end = st_map->image + PAGE_SIZE;
+	image_end = st_map->image + 2 * PAGE_SIZE;
 
 	for_each_member(i, t, member) {
 		const struct btf_type *mtype, *ptype;
@@ -688,7 +688,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
 	st_map->links =
 		bpf_map_area_alloc(btf_type_vlen(t) * sizeof(struct bpf_links *),
 				   NUMA_NO_NODE);
-	st_map->image = bpf_jit_alloc_exec(PAGE_SIZE);
+	st_map->image = bpf_jit_alloc_exec(2 * PAGE_SIZE);
 	if (!st_map->uvalue || !st_map->links || !st_map->image) {
 		__bpf_struct_ops_map_free(map);
 		return ERR_PTR(-ENOMEM);
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#31 lore raw

RFC PATCH v3 27/37 fuse-bpf: Add fuse-bpf constants

In reply to Daniel Rosenberg

This adds constants that fuse_op programs will rely on for communicating what action fuse should take next.

---
 include/uapi/linux/bpf.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/uapi/linux/bpf.h +12 -0
index 4b20a7269bee..6521c40875c7 100644
@@ -7155,4 +7155,16 @@ struct bpf_iter_num {
 	__u64 __opaque[1];
 } __attribute__((aligned(8)));
 
+/* Return Codes for Fuse BPF struct_op programs */
+#define BPF_FUSE_CONTINUE		0
+#define BPF_FUSE_USER			1
+#define BPF_FUSE_USER_PREFILTER		2
+#define BPF_FUSE_POSTFILTER		3
+#define BPF_FUSE_USER_POSTFILTER	4
+
+/* Op Code Filter values for BPF Programs */
+#define FUSE_OPCODE_FILTER	0x0ffff
+#define FUSE_PREFILTER		0x10000
+#define FUSE_POSTFILTER		0x20000
+
 #endif /* _UAPI__LINUX_BPF_H__ */
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#32 lore raw

RFC PATCH v3 28/37 WIP: bpf: Add fuse_ops struct_op programs

In reply to Daniel Rosenberg

This introduces a new struct_op type: fuse_ops. This program set provides pre and post filters to run around fuse-bpf calls that act directly on the lower filesystem.

The inputs are either fixed structures, or struct fuse_buffer's.

These programs are not permitted to make any changes to these fuse_buffers unless they create a dynptr wrapper using the supplied kfunc helpers.

Fuse_buffers maintain additional state information that FUSE uses to manage memory and determine if additional set up or checks are needed.

---
 include/linux/bpf_fuse.h          | 189 +++++++++++++++++++++++
 kernel/bpf/Makefile               |   4 +
 kernel/bpf/bpf_fuse.c             | 241 ++++++++++++++++++++++++++++++
 kernel/bpf/bpf_struct_ops_types.h |   4 +
 kernel/bpf/btf.c                  |   1 +
 kernel/bpf/verifier.c             |   9 ++
 6 files changed, 448 insertions(+)
 create mode 100644 kernel/bpf/bpf_fuse.c
include/linux/bpf_fuse.h +189 -0
index ce8b1b347496..780a7889aea2 100644
@@ -30,6 +30,8 @@ struct fuse_buffer {
 #define BPF_FUSE_MODIFIED	(1 << 3) // The helper function allowed writes to the buffer
 #define BPF_FUSE_ALLOCATED	(1 << 4) // The helper function allocated the buffer
 
+extern void *bpf_fuse_get_writeable(struct fuse_buffer *arg, u64 size, bool copy);
+
 /*
  * BPF Fuse Args
  *
@@ -81,4 +83,191 @@ static inline unsigned bpf_fuse_arg_size(const struct bpf_fuse_arg *arg)
 	return arg->is_buffer ? arg->buffer->size : arg->size;
 }
 
+struct fuse_ops {
+	uint32_t (*open_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_open_in *in);
+	uint32_t (*open_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_open_in *in,
+				struct fuse_open_out *out);
+
+	uint32_t (*opendir_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_open_in *in);
+	uint32_t (*opendir_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_open_in *in,
+				struct fuse_open_out *out);
+
+	uint32_t (*create_open_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_create_in *in, struct fuse_buffer *name);
+	uint32_t (*create_open_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_create_in *in, const struct fuse_buffer *name,
+				struct fuse_entry_out *entry_out, struct fuse_open_out *out);
+
+	uint32_t (*release_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_release_in *in);
+	uint32_t (*release_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_release_in *in);
+
+	uint32_t (*releasedir_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_release_in *in);
+	uint32_t (*releasedir_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_release_in *in);
+
+	uint32_t (*flush_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_flush_in *in);
+	uint32_t (*flush_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_flush_in *in);
+
+	uint32_t (*lseek_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_lseek_in *in);
+	uint32_t (*lseek_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_lseek_in *in,
+				struct fuse_lseek_out *out);
+
+	uint32_t (*copy_file_range_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_copy_file_range_in *in);
+	uint32_t (*copy_file_range_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_copy_file_range_in *in,
+				struct fuse_write_out *out);
+
+	uint32_t (*fsync_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_fsync_in *in);
+	uint32_t (*fsync_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_fsync_in *in);
+
+	uint32_t (*dir_fsync_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_fsync_in *in);
+	uint32_t (*dir_fsync_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_fsync_in *in);
+
+	uint32_t (*getxattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_getxattr_in *in, struct fuse_buffer *name);
+	// if in->size > 0, use value. If in->size == 0, use out.
+	uint32_t (*getxattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_getxattr_in *in, const struct fuse_buffer *name,
+				struct fuse_buffer *value, struct fuse_getxattr_out *out);
+
+	uint32_t (*listxattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_getxattr_in *in);
+	// if in->size > 0, use value. If in->size == 0, use out.
+	uint32_t (*listxattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_getxattr_in *in,
+				struct fuse_buffer *value, struct fuse_getxattr_out *out);
+
+	uint32_t (*setxattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_setxattr_in *in, struct fuse_buffer *name,
+					struct fuse_buffer *value);
+	uint32_t (*setxattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_setxattr_in *in, const struct fuse_buffer *name,
+					const struct fuse_buffer *value);
+
+	uint32_t (*removexattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_buffer *name);
+	uint32_t (*removexattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_buffer *name);
+
+	/* Read and Write iter will likely undergo some sort of change/addition to handle changing
+	 * the data buffer passed in/out. */
+	uint32_t (*read_iter_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_read_in *in);
+	uint32_t (*read_iter_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_read_in *in,
+				struct fuse_read_iter_out *out);
+
+	uint32_t (*write_iter_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_write_in *in);
+	uint32_t (*write_iter_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_write_in *in,
+				struct fuse_write_iter_out *out);
+
+	uint32_t (*file_fallocate_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_fallocate_in *in);
+	uint32_t (*file_fallocate_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_fallocate_in *in);
+
+	uint32_t (*lookup_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_buffer *name);
+	uint32_t (*lookup_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_buffer *name,
+				struct fuse_entry_out *out, struct fuse_buffer *entries);
+
+	uint32_t (*mknod_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_mknod_in *in, struct fuse_buffer *name);
+	uint32_t (*mknod_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_mknod_in *in, const struct fuse_buffer *name);
+
+	uint32_t (*mkdir_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_mkdir_in *in, struct fuse_buffer *name);
+	uint32_t (*mkdir_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_mkdir_in *in, const struct fuse_buffer *name);
+
+	uint32_t (*rmdir_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_buffer *name);
+	uint32_t (*rmdir_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_buffer *name);
+
+	uint32_t (*rename2_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_rename2_in *in, struct fuse_buffer *old_name,
+				struct fuse_buffer *new_name);
+	uint32_t (*rename2_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_rename2_in *in, const struct fuse_buffer *old_name,
+				const struct fuse_buffer *new_name);
+
+	uint32_t (*rename_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_rename_in *in, struct fuse_buffer *old_name,
+				struct fuse_buffer *new_name);
+	uint32_t (*rename_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_rename_in *in, const struct fuse_buffer *old_name,
+				const struct fuse_buffer *new_name);
+
+	uint32_t (*unlink_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_buffer *name);
+	uint32_t (*unlink_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_buffer *name);
+
+	uint32_t (*link_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_link_in *in, struct fuse_buffer *name);
+	uint32_t (*link_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_link_in *in, const struct fuse_buffer *name);
+
+	uint32_t (*getattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_getattr_in *in);
+	uint32_t (*getattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_getattr_in *in,
+				struct fuse_attr_out *out);
+
+	uint32_t (*setattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_setattr_in *in);
+	uint32_t (*setattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_setattr_in *in,
+				struct fuse_attr_out *out);
+
+	uint32_t (*statfs_prefilter)(const struct bpf_fuse_meta_info *meta);
+	uint32_t (*statfs_postfilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_statfs_out *out);
+
+	//TODO: This does not allow doing anything with path
+	uint32_t (*get_link_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_buffer *name);
+	uint32_t (*get_link_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_buffer *name);
+
+	uint32_t (*symlink_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_buffer *name, struct fuse_buffer *path);
+	uint32_t (*symlink_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_buffer *name, const struct fuse_buffer *path);
+
+	uint32_t (*readdir_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_read_in *in);
+	uint32_t (*readdir_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_read_in *in,
+				struct fuse_read_out *out, struct fuse_buffer *buffer);
+
+	uint32_t (*access_prefilter)(const struct bpf_fuse_meta_info *meta,
+				struct fuse_access_in *in);
+	uint32_t (*access_postfilter)(const struct bpf_fuse_meta_info *meta,
+				const struct fuse_access_in *in);
+
+	char name[BPF_FUSE_NAME_MAX];
+};
+
 #endif /* _BPF_FUSE_H */
kernel/bpf/Makefile +4 -0
index 1d3892168d32..26a2e741ef61 100644
@@ -45,3 +45,7 @@ obj-$(CONFIG_BPF_PRELOAD) += preload/
 obj-$(CONFIG_BPF_SYSCALL) += relo_core.o
 $(obj)/relo_core.o: $(srctree)/tools/lib/bpf/relo_core.c FORCE
 	$(call if_changed_rule,cc_o_c)
+
+ifeq ($(CONFIG_FUSE_BPF),y)
+obj-$(CONFIG_BPF_SYSCALL) += bpf_fuse.o
+endif
kernel/bpf/bpf_fuse.c new file +241 -0

241 lines not on this page · raw message

kernel/bpf/bpf_struct_ops_types.h +4 -0
index 5678a9ddf817..fabb2c1a9482 100644
@@ -5,6 +5,10 @@
 #ifdef CONFIG_NET
 BPF_STRUCT_OPS_TYPE(bpf_dummy_ops)
 #endif
+#ifdef CONFIG_FUSE_BPF
+#include <linux/bpf_fuse.h>
+BPF_STRUCT_OPS_TYPE(fuse_ops)
+#endif
 #ifdef CONFIG_INET
 #include <net/tcp.h>
 BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)
kernel/bpf/btf.c +1 -0
index 027f9f8a3551..c34fd9e70039 100644
@@ -25,6 +25,7 @@
 #include <linux/bsearch.h>
 #include <linux/kobject.h>
 #include <linux/sysfs.h>
+#include <linux/bpf_fuse.h>
 #include <net/sock.h>
 #include "../tools/lib/bpf/relo_core.h"
 
kernel/bpf/verifier.c +9 -0
index fd959824469d..b3bda15283c0 100644
@@ -9597,6 +9597,8 @@ enum special_kfunc_type {
 	KF_bpf_dynptr_from_xdp,
 	KF_bpf_dynptr_slice,
 	KF_bpf_dynptr_slice_rdwr,
+	KF_bpf_fuse_get_rw_dynptr,
+	KF_bpf_fuse_get_ro_dynptr,
 };
 
 BTF_SET_START(special_kfunc_set)
@@ -9616,6 +9618,8 @@ BTF_ID(func, bpf_dynptr_from_skb)
 BTF_ID(func, bpf_dynptr_from_xdp)
 BTF_ID(func, bpf_dynptr_slice)
 BTF_ID(func, bpf_dynptr_slice_rdwr)
+BTF_ID(func, bpf_fuse_get_rw_dynptr)
+BTF_ID(func, bpf_fuse_get_ro_dynptr)
 BTF_SET_END(special_kfunc_set)
 
 BTF_ID_LIST(special_kfunc_list)
@@ -9637,6 +9641,8 @@ BTF_ID(func, bpf_dynptr_from_skb)
 BTF_ID(func, bpf_dynptr_from_xdp)
 BTF_ID(func, bpf_dynptr_slice)
 BTF_ID(func, bpf_dynptr_slice_rdwr)
+BTF_ID(func, bpf_fuse_get_rw_dynptr)
+BTF_ID(func, bpf_fuse_get_ro_dynptr)
 
 static bool is_kfunc_bpf_rcu_read_lock(struct bpf_kfunc_call_arg_meta *meta)
 {
@@ -10349,6 +10355,9 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
 				dynptr_arg_type |= DYNPTR_TYPE_SKB;
 			else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_from_xdp])
 				dynptr_arg_type |= DYNPTR_TYPE_XDP;
+			else if (meta->func_id == special_kfunc_list[KF_bpf_fuse_get_rw_dynptr] ||
+					meta->func_id == special_kfunc_list[KF_bpf_fuse_get_ro_dynptr])
+				dynptr_arg_type |= DYNPTR_TYPE_LOCAL;
 
 			ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type);
 			if (ret < 0)
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#33 lore raw

Re: [RFC PATCH v3 28/37] WIP: bpf: Add fuse_ops struct_op programs

Quoted 217 lines · Daniel Rosenberg

This introduces a new struct_op type: fuse_ops. This program set provides pre and post filters to run around fuse-bpf calls that act directly on the lower filesystem.

The inputs are either fixed structures, or struct fuse_buffer's.

These programs are not permitted to make any changes to these fuse_buffers unless they create a dynptr wrapper using the supplied kfunc helpers.

Fuse_buffers maintain additional state information that FUSE uses to manage memory and determine if additional set up or checks are needed.

---
 include/linux/bpf_fuse.h          | 189 +++++++++++++++++++++++
 kernel/bpf/Makefile               |   4 +
 kernel/bpf/bpf_fuse.c             | 241 ++++++++++++++++++++++++++++++
 kernel/bpf/bpf_struct_ops_types.h |   4 +
 kernel/bpf/btf.c                  |   1 +
 kernel/bpf/verifier.c             |   9 ++
 6 files changed, 448 insertions(+)
 create mode 100644 kernel/bpf/bpf_fuse.c
include/linux/bpf_fuse.h +178 -0
index ce8b1b347496..780a7889aea2 100644
@@ -30,6 +30,8 @@ struct fuse_buffer {
 #define BPF_FUSE_MODIFIED      (1 << 3) // The helper function allowed writes to the buffer
 #define BPF_FUSE_ALLOCATED     (1 << 4) // The helper function allocated the buffer
 
+extern void *bpf_fuse_get_writeable(struct fuse_buffer *arg, u64 size, bool copy);
+
 /*
  * BPF Fuse Args
  *
@@ -81,4 +83,191 @@ static inline unsigned bpf_fuse_arg_size(const struct bpf_fuse_arg *arg)
        return arg->is_buffer ? arg->buffer->size : arg->size;
 }
 
+struct fuse_ops {
+       uint32_t (*open_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_open_in *in);
+       uint32_t (*open_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_open_in *in,
+                               struct fuse_open_out *out);
+
+       uint32_t (*opendir_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_open_in *in);
+       uint32_t (*opendir_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_open_in *in,
+                               struct fuse_open_out *out);
+
+       uint32_t (*create_open_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_create_in *in, struct fuse_buffer *name);
+       uint32_t (*create_open_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_create_in *in, const struct fuse_buffer *name,
+                               struct fuse_entry_out *entry_out, struct fuse_open_out *out);
+
+       uint32_t (*release_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_release_in *in);
+       uint32_t (*release_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_release_in *in);
+
+       uint32_t (*releasedir_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_release_in *in);
+       uint32_t (*releasedir_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_release_in *in);
+
+       uint32_t (*flush_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_flush_in *in);
+       uint32_t (*flush_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_flush_in *in);
+
+       uint32_t (*lseek_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_lseek_in *in);
+       uint32_t (*lseek_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_lseek_in *in,
+                               struct fuse_lseek_out *out);
+
+       uint32_t (*copy_file_range_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_copy_file_range_in *in);
+       uint32_t (*copy_file_range_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_copy_file_range_in *in,
+                               struct fuse_write_out *out);
+
+       uint32_t (*fsync_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_fsync_in *in);
+       uint32_t (*fsync_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_fsync_in *in);
+
+       uint32_t (*dir_fsync_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_fsync_in *in);
+       uint32_t (*dir_fsync_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_fsync_in *in);
+
+       uint32_t (*getxattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_getxattr_in *in, struct fuse_buffer *name);
+       // if in->size > 0, use value. If in->size == 0, use out.
+       uint32_t (*getxattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_getxattr_in *in, const struct fuse_buffer *name,
+                               struct fuse_buffer *value, struct fuse_getxattr_out *out);
+
+       uint32_t (*listxattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_getxattr_in *in);
+       // if in->size > 0, use value. If in->size == 0, use out.
+       uint32_t (*listxattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_getxattr_in *in,
+                               struct fuse_buffer *value, struct fuse_getxattr_out *out);
+
+       uint32_t (*setxattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_setxattr_in *in, struct fuse_buffer *name,
+                                       struct fuse_buffer *value);
+       uint32_t (*setxattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_setxattr_in *in, const struct fuse_buffer *name,
+                                       const struct fuse_buffer *value);
+
+       uint32_t (*removexattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_buffer *name);
+       uint32_t (*removexattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_buffer *name);
+
+       /* Read and Write iter will likely undergo some sort of change/addition to handle changing
+        * the data buffer passed in/out. */
+       uint32_t (*read_iter_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_read_in *in);
+       uint32_t (*read_iter_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_read_in *in,
+                               struct fuse_read_iter_out *out);
+
+       uint32_t (*write_iter_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_write_in *in);
+       uint32_t (*write_iter_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_write_in *in,
+                               struct fuse_write_iter_out *out);
+
+       uint32_t (*file_fallocate_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_fallocate_in *in);
+       uint32_t (*file_fallocate_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_fallocate_in *in);
+
+       uint32_t (*lookup_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_buffer *name);
+       uint32_t (*lookup_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_buffer *name,
+                               struct fuse_entry_out *out, struct fuse_buffer *entries);
+
+       uint32_t (*mknod_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_mknod_in *in, struct fuse_buffer *name);
+       uint32_t (*mknod_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_mknod_in *in, const struct fuse_buffer *name);
+
+       uint32_t (*mkdir_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_mkdir_in *in, struct fuse_buffer *name);
+       uint32_t (*mkdir_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_mkdir_in *in, const struct fuse_buffer *name);
+
+       uint32_t (*rmdir_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_buffer *name);
+       uint32_t (*rmdir_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_buffer *name);
+
+       uint32_t (*rename2_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_rename2_in *in, struct fuse_buffer *old_name,
+                               struct fuse_buffer *new_name);
+       uint32_t (*rename2_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_rename2_in *in, const struct fuse_buffer *old_name,
+                               const struct fuse_buffer *new_name);
+
+       uint32_t (*rename_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_rename_in *in, struct fuse_buffer *old_name,
+                               struct fuse_buffer *new_name);
+       uint32_t (*rename_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_rename_in *in, const struct fuse_buffer *old_name,
+                               const struct fuse_buffer *new_name);
+
+       uint32_t (*unlink_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_buffer *name);
+       uint32_t (*unlink_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_buffer *name);
+
+       uint32_t (*link_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_link_in *in, struct fuse_buffer *name);
+       uint32_t (*link_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_link_in *in, const struct fuse_buffer *name);
+
+       uint32_t (*getattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_getattr_in *in);
+       uint32_t (*getattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_getattr_in *in,
+                               struct fuse_attr_out *out);
+
+       uint32_t (*setattr_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_setattr_in *in);
+       uint32_t (*setattr_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_setattr_in *in,
+                               struct fuse_attr_out *out);
+
+       uint32_t (*statfs_prefilter)(const struct bpf_fuse_meta_info *meta);
+       uint32_t (*statfs_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_statfs_out *out);
+
+       //TODO: This does not allow doing anything with path
+       uint32_t (*get_link_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_buffer *name);
+       uint32_t (*get_link_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_buffer *name);
+
+       uint32_t (*symlink_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_buffer *name, struct fuse_buffer *path);
+       uint32_t (*symlink_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_buffer *name, const struct fuse_buffer *path);
+
+       uint32_t (*readdir_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_read_in *in);
+       uint32_t (*readdir_postfilter)(const struct bpf_fuse_meta_info *meta,

include/linux/bpf_fuse.h static inline unsigned bpf_fuse_arg_size(const struct bpf_fuse_arg *arg) · line 271

Quoted 10 lines
include/linux/bpf_fuse.h +10 -0
+                               const struct fuse_read_in *in,
+                               struct fuse_read_out *out, struct fuse_buffer *buffer);
+
+       uint32_t (*access_prefilter)(const struct bpf_fuse_meta_info *meta,
+                               struct fuse_access_in *in);
+       uint32_t (*access_postfilter)(const struct bpf_fuse_meta_info *meta,
+                               const struct fuse_access_in *in);
+
+       char name[BPF_FUSE_NAME_MAX];
+};

Have you considered grouping this huge amount of callbacks into a smaller set of more generic callbacks where each callback would get enum argument specifying what sort of operation it is called for? This has many advantages, starting from not having to deal with struct_ops limits, ending with not needing to instantiate dozens of individual BPF programs.

E.g., for a lot of operations the difference between pre- and post-filter is in having in argument as read-only and maybe having extra out argument for post-filter. One way to unify such post/pre filters into one callback would be to record whether in has to be read-only or read-write and not allow to create r/w dynptr for the former case. Pass bool or enum specifying if it's post or pre filter. For that optional out argument, you can simulate effectively the same by always supplying it, but making sure that out parameter is read-only and zero-sized, for example.

That would cut the number of callbacks in two, which I'd say still is not great :) I think it would be better still to have even larger groups of callbacks for whole families of operations with the same (or "unifiable") interface (domain experts like you would need to do an analysis here to see what makes sense to group, of course).

We'll probably touch on that tomorrow at BPF office hours, but I wanted to point this out beforehand, so that you have time to think about it.

Quoted 6 lines
include/linux/bpf_fuse.h +1 -0
+
 #endif /* _BPF_FUSE_H */
kernel/bpf/Makefile +0 -0
index 1d3892168d32..26a2e741ef61 100644

[...]

kernel/bpf/Makefile

Quoted 4 lines
kernel/bpf/Makefile +4 -0
+__diag_push();
+__diag_ignore_all("-Wmissing-prototypes",
+                  "Global kfuncs as their definitions will be in BTF");
+void bpf_fuse_get_rw_dynptr(struct fuse_buffer *buffer, struct bpf_dynptr_kern *dynptr__uninit, u64 size, bool copy)

not clear why size is passed from outside instead of instantiating dynptr with buffer->size? See [0] for bpf_dynptr_adjust and bpf_dynptr_clone that allow you to adjust buffer as necessary.

As for the copy parameter, can you elaborate on the idea behind it?

[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=741584&state=*

kernel/bpf/Makefile

Quoted 6 lines
kernel/bpf/Makefile +6 -0
+{
+       buffer->data = bpf_fuse_get_writeable(buffer, size, copy);
+       bpf_dynptr_init(dynptr__uninit, buffer->data, BPF_DYNPTR_TYPE_LOCAL, 0, buffer->size);
+}
+
+void bpf_fuse_get_ro_dynptr(const struct fuse_buffer *buffer, struct bpf_dynptr_kern *dynptr__uninit)

these kfuncs probably should be more consistently named as bpf_dynptr_from_fuse_buffer_{ro,rw}() ?

kernel/bpf/Makefile

Quoted 8 lines
kernel/bpf/Makefile +8 -0
+{
+       bpf_dynptr_init(dynptr__uninit, buffer->data, BPF_DYNPTR_TYPE_LOCAL, 0, buffer->size);
+       bpf_dynptr_set_rdonly(dynptr__uninit);
+}
+
+uint32_t bpf_fuse_return_len(struct fuse_buffer *buffer)
+{
+       return buffer->size;

you should be able to get this with bpf_dynptr_size() (once you create it from fuse_buffer).

Quoted 48 lines
kernel/bpf/Makefile +48 -0
+}
+__diag_pop();
+BTF_SET8_START(fuse_kfunc_set)
+BTF_ID_FLAGS(func, bpf_fuse_get_rw_dynptr)
+BTF_ID_FLAGS(func, bpf_fuse_get_ro_dynptr)
+BTF_ID_FLAGS(func, bpf_fuse_return_len)
+BTF_SET8_END(fuse_kfunc_set)
+
+static const struct btf_kfunc_id_set bpf_fuse_kfunc_set = {
+       .owner = THIS_MODULE,
+       .set = &fuse_kfunc_set,
+};
+
+static int __init bpf_fuse_kfuncs_init(void)
+{
+       return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
+                                        &bpf_fuse_kfunc_set);
+}
+
+late_initcall(bpf_fuse_kfuncs_init);
+
+static const struct bpf_func_proto *bpf_fuse_get_func_proto(enum bpf_func_id func_id,
+                                                             const struct bpf_prog *prog)
+{
+       switch (func_id) {
+       default:
+               return bpf_base_func_proto(func_id);
+       }
+}
+
+static bool bpf_fuse_is_valid_access(int off, int size,
+                                   enum bpf_access_type type,
+                                   const struct bpf_prog *prog,
+                                   struct bpf_insn_access_aux *info)
+{
+       return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
+}
+
+const struct btf_type *fuse_buffer_struct_type;
+
+static int bpf_fuse_btf_struct_access(struct bpf_verifier_log *log,
+                                       const struct bpf_reg_state *reg,
+                                       int off, int size)
+{
+       const struct btf_type *t;
+
+       t = btf_type_by_id(reg->btf, reg->btf_id);
+       if (t == fuse_buffer_struct_type) {

kernel/bpf/Makefile

Quoted 10 lines
kernel/bpf/Makefile +10 -0
+               bpf_log(log,
+                       "direct access to fuse_buffer is disallowed\n");
+               return -EACCES;
+       }
+
+       return 0;
+}
+
+static const struct bpf_verifier_ops bpf_fuse_verifier_ops = {
+       .get_func_proto         = bpf_fuse_get_func_proto,

you probably should be fine with just using bpf_tracing_func_proto as is

Quoted 1 line

+ .is_valid_access = bpf_fuse_is_valid_access,

similarly, why custom no-op callback?

Quoted 36 lines
kernel/bpf/Makefile +36 -0
+       .btf_struct_access      = bpf_fuse_btf_struct_access,
+};
+
+static int bpf_fuse_check_member(const struct btf_type *t,
+                                  const struct btf_member *member,
+                                  const struct bpf_prog *prog)
+{
+       //if (is_unsupported(__btf_member_bit_offset(t, member) / 8))
+       //      return -ENOTSUPP;
+       return 0;
+}
+
+static int bpf_fuse_init_member(const struct btf_type *t,
+                                 const struct btf_member *member,
+                                 void *kdata, const void *udata)
+{
+       const struct fuse_ops *uf_ops;
+       struct fuse_ops *f_ops;
+       u32 moff;
+
+       uf_ops = (const struct fuse_ops *)udata;
+       f_ops = (struct fuse_ops *)kdata;
+
+       moff = __btf_member_bit_offset(t, member) / 8;
+       switch (moff) {
+       case offsetof(struct fuse_ops, name):
+               if (bpf_obj_name_cpy(f_ops->name, uf_ops->name,
+                                    sizeof(f_ops->name)) <= 0)
+                       return -EINVAL;
+               //if (tcp_ca_find(utcp_ca->name))
+               //      return -EEXIST;
+               return 1;
+       }
+
+       return 0;
+}

kernel/bpf/Makefile

Quoted 10 lines
kernel/bpf/Makefile +10 -0
+
+static int bpf_fuse_init(struct btf *btf)
+{
+       s32 type_id;
+
+       type_id = btf_find_by_name_kind(btf, "fuse_buffer", BTF_KIND_STRUCT);
+       if (type_id < 0)
+               return -EINVAL;
+       fuse_buffer_struct_type = btf_type_by_id(btf, type_id);
+

see BTF_ID and BTF_ID_LIST uses for how to get ID for your custom well-known type

Quoted 5 lines
kernel/bpf/Makefile +5 -0
+       return 0;
+}
+
+static struct bpf_fuse_ops_attach *fuse_reg = NULL;
+

[...]

#34 lore raw

Re: [RFC PATCH v3 28/37] WIP: bpf: Add fuse_ops struct_op programs

Quoted 24 lines · Andrii Nakryiko

Have you considered grouping this huge amount of callbacks into a smaller set of more generic callbacks where each callback would get enum argument specifying what sort of operation it is called for? This has many advantages, starting from not having to deal with struct_ops limits, ending with not needing to instantiate dozens of individual BPF programs.

E.g., for a lot of operations the difference between pre- and post-filter is in having in argument as read-only and maybe having extra out argument for post-filter. One way to unify such post/pre filters into one callback would be to record whether in has to be read-only or read-write and not allow to create r/w dynptr for the former case. Pass bool or enum specifying if it's post or pre filter. For that optional out argument, you can simulate effectively the same by always supplying it, but making sure that out parameter is read-only and zero-sized, for example.

That would cut the number of callbacks in two, which I'd say still is not great :) I think it would be better still to have even larger groups of callbacks for whole families of operations with the same (or "unifiable") interface (domain experts like you would need to do an analysis here to see what makes sense to group, of course).

Quoted 4 lines

We'll probably touch on that tomorrow at BPF office hours, but I wanted to point this out beforehand, so that you have time to think about it.

The meta info struct we pass in includes the opcode which contains whether it is a prefilter or postfilter, although I guess that may be less accessible to the verifier than a separate bool. In the v1 version, we handled all op codes in a single program, although I think we were running into some slowdowns when we had every opcode in a giant switch statement, plus we were incurring the cost of the bpf program even when we didn't need to do anything in it. The struct_op version lets us entirely skip calling the bpf for opcodes we don't need to handle.

Many of the arguments we pass currently are structs. If they were all dynptrs, we could set the output related ones to empty/readonly, but that removes one of the other strengths of the struct_op setup, where we can actually label the inputs as the structs they are instead of a void* equivalent. There are definitely some cases where we could easily merge opcode callbacks, like FUSE_FSYNCDIR/FUSE_FSYNC and FUSE_OPEN/FUSE_OPENDIR. I set them up as separate since it's easy to assign the same program to both callbacks in the case where you want both to be handled the same, while maintaining flexibility to handle them separately.

Quoted 10 lines

+void bpf_fuse_get_rw_dynptr(struct fuse_buffer *buffer, struct bpf_dynptr_kern *dynptr__uninit, u64 size, bool copy)

not clear why size is passed from outside instead of instantiating dynptr with buffer->size? See [0] for bpf_dynptr_adjust and bpf_dynptr_clone that allow you to adjust buffer as necessary.

As for the copy parameter, can you elaborate on the idea behind it?

[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=741584&state=*

We're storing these buffers as fuse_buffers initially because of the additional metadata we're carrying. Some fields have variable lengths, or are backed by const data. For instance, names. If you wanted to alter the name we use on the lower filesystem, you cannot change it directly since it's being backed by the dentry name. If you wanted to adjust something, like perhaps adding an extension, you would pass bpf_fuse_get_rw_dynptr the size you'd want for the new buffer, and copy=true to get the preexisting data. Fuse_buffer tracks that data was allocated so Fuse can clean up after the call. Additionally, say you wanted to trim half the data returned by an xattr for some reason. You would give it a size less than the buffer size to inform fuse that it should ignore the second half of the data. That part could be handled by bpf_dynptr_adjust if we didn't also need to handle the allocation case.
Say you wanted to have the lower file name be the hash of the one you created. In that case, you could get bpf_fuse_get_ro_dynptr to get access to compute the hash, and then bpf_fuse_get_rw_dynptr to get a buffer to write the hash to. Since the data is not directly related to the original data, there would be no benefit to getting a copy.

I initially intended for bpf_fuse_get_ro_dynptr/bpf_fuse_get_rw_dynptr to be called at most once for each field, but that may be too restrictive. At the moment, if you make two calls that require reallocating, any pointers to the old buffer would be invalid. This is not the case for the original name, as we aren't touching the original source. There are two possible approaches here. I could either refcount the buffer and have a put kfunc, or I could invalidate old dynpointers when bpf_fuse_get_rw_dynptr is called, similar to what skb/xdp do. I'm leaning towards the latter to disallow having many allocations active at once by calling bpf_fuse_get_rw_dynptr for increasing sizes, though I could also just disallow reallocating a buffer that already was reallocated.

The new dynptr helpers are pretty exciting since they'll make it much easier to deal with chunks of data, which we may end up doing in read/write filters. I haven't fully set those up since I was waiting to see what the dynptr helpers ended up looking like.

Quoted 5 lines
Quoted 1 line

+void bpf_fuse_get_ro_dynptr(const struct fuse_buffer *buffer, struct bpf_dynptr_kern *dynptr__uninit)

these kfuncs probably should be more consistently named as bpf_dynptr_from_fuse_buffer_{ro,rw}() ?

Yeah, that fits in much better with the skb/xdp functions.

Quoted 8 lines

kernel/bpf/bpf_fuse.c line 88

Quoted 4 lines
kernel/bpf/bpf_fuse.c new file +4 -0
+
+uint32_t bpf_fuse_return_len(struct fuse_buffer *buffer)
+{
+       return buffer->size;

you should be able to get this with bpf_dynptr_size() (once you create it from fuse_buffer).

Yes, this might be unnecessary. I added it while testing kfuncs, and had intended to use it with a fuse_buffer strncmp before I saw that there's now a bpf_strncmp :) I had tried using it with bpf_dynptr_slice, but that requires a known constant at verification time, which may make using it in real cases a bit difficult... bpf_strncmp also has some restrictions around the second string being a fixed map, or something like that.

Quoted 7 lines

you probably should be fine with just using bpf_tracing_func_proto as is

Quoted 1 line

+ .is_valid_access = bpf_fuse_is_valid_access,

similarly, why custom no-op callback?

Those are largely carried over from iterations when I was less sure what I would need. A lot of the work I was doing in the v1 code is handled by default with the struct_op setup now, or is otherwise unnecessary. This area in particular needs a lot of cleanup.

Quoted 4 lines
Quoted 4 lines
kernel/bpf/bpf_fuse.c new file +4 -0
+static int bpf_fuse_init(struct btf *btf)
+{
+       s32 type_id;
+
Quoted 9 lines

kernel/bpf/bpf_fuse.c line 193

Quoted 5 lines
kernel/bpf/bpf_fuse.c new file +5 -0
+       type_id = btf_find_by_name_kind(btf, "fuse_buffer", BTF_KIND_STRUCT);
+       if (type_id < 0)
+               return -EINVAL;
+       fuse_buffer_struct_type = btf_type_by_id(btf, type_id);
+

see BTF_ID and BTF_ID_LIST uses for how to get ID for your custom well-known type

Thanks, I'll look into those.

#35 lore raw

Re: [RFC PATCH v3 28/37] WIP: bpf: Add fuse_ops struct_op programs

Quoted 42 lines · Daniel Rosenberg
Quoted 24 lines · Andrii Nakryiko

Have you considered grouping this huge amount of callbacks into a smaller set of more generic callbacks where each callback would get enum argument specifying what sort of operation it is called for? This has many advantages, starting from not having to deal with struct_ops limits, ending with not needing to instantiate dozens of individual BPF programs.

E.g., for a lot of operations the difference between pre- and post-filter is in having in argument as read-only and maybe having extra out argument for post-filter. One way to unify such post/pre filters into one callback would be to record whether in has to be read-only or read-write and not allow to create r/w dynptr for the former case. Pass bool or enum specifying if it's post or pre filter. For that optional out argument, you can simulate effectively the same by always supplying it, but making sure that out parameter is read-only and zero-sized, for example.

That would cut the number of callbacks in two, which I'd say still is not great :) I think it would be better still to have even larger groups of callbacks for whole families of operations with the same (or "unifiable") interface (domain experts like you would need to do an analysis here to see what makes sense to group, of course).

Quoted 4 lines

We'll probably touch on that tomorrow at BPF office hours, but I wanted to point this out beforehand, so that you have time to think about it.

The meta info struct we pass in includes the opcode which contains whether it is a prefilter or postfilter, although I guess that may be less accessible to the verifier than a separate bool. In the v1 version, we handled all op codes in a single program, although I think we were running into some slowdowns when we had every opcode in a giant switch statement, plus we were incurring the cost of the bpf program even when we didn't need to do anything in it. The struct_op version lets us entirely skip calling the bpf for opcodes we don't need to handle.

Quoted 10 lines

Many of the arguments we pass currently are structs. If they were all dynptrs, we could set the output related ones to empty/readonly, but that removes one of the other strengths of the struct_op setup, where we can actually label the inputs as the structs they are instead of a void* equivalent. There are definitely some cases where we could easily merge opcode callbacks, like FUSE_FSYNCDIR/FUSE_FSYNC and FUSE_OPEN/FUSE_OPENDIR. I set them up as separate since it's easy to assign the same program to both callbacks in the case where you want both to be handled the same, while maintaining flexibility to handle them separately.

If combining hooks doesn't bring any value and simplification, I think it's fine to keep it as is. I was mostly probing if there is an equally convenient, but more succinct API that could be exposed through struct_ops. If there is none, then it's fine.

Quoted 12 lines
Quoted 11 lines

+void bpf_fuse_get_rw_dynptr(struct fuse_buffer *buffer, struct bpf_dynptr_kern *dynptr__uninit, u64 size, bool copy)

not clear why size is passed from outside instead of instantiating dynptr with buffer->size? See [0] for bpf_dynptr_adjust and bpf_dynptr_clone that allow you to adjust buffer as necessary.

As for the copy parameter, can you elaborate on the idea behind it?

[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=741584&state=*

Quoted 14 lines

We're storing these buffers as fuse_buffers initially because of the additional metadata we're carrying. Some fields have variable lengths, or are backed by const data. For instance, names. If you wanted to alter the name we use on the lower filesystem, you cannot change it directly since it's being backed by the dentry name. If you wanted to adjust something, like perhaps adding an extension, you would pass bpf_fuse_get_rw_dynptr the size you'd want for the new buffer, and copy=true to get the preexisting data. Fuse_buffer tracks that data was allocated so Fuse can clean up after the call. Additionally, say you wanted to trim half the data returned by an xattr for some reason. You would give it a size less than the buffer size to inform fuse that it should ignore the second half of the data. That part could be handled by bpf_dynptr_adjust if we didn't also need to handle the allocation case.

Interesting point about allocations and needing to realloc names. But I wonder if it makes more sense to split the copy/reallocation part and do it with separate kfunc. And leave dynptr only as means to work with that data. So you'd do something like below for read/write case:

bpf_fuse_buf_clone(&buffer, new_size);
bpf_fuse_dynptr_from_buf_rw(&buffer, &dynptr);

But would skip bpf_fuse_buf_clone() if you only ever read:

bpf_fuse_dynptr_from_buf_ro(&buffer, &dynptr);

If fuse_buffer was never cloned/realloced, then bpf_fuse_dynptr_from_buf_rw() should just fail and return invalid dynptr.

Quoted 6 lines

Say you wanted to have the lower file name be the hash of the one you created. In that case, you could get bpf_fuse_get_ro_dynptr to get access to compute the hash, and then bpf_fuse_get_rw_dynptr to get a buffer to write the hash to. Since the data is not directly related to the original data, there would be no benefit to getting a copy.

Quoted 12 lines

I initially intended for bpf_fuse_get_ro_dynptr/bpf_fuse_get_rw_dynptr to be called at most once for each field, but that may be too restrictive. At the moment, if you make two calls that require reallocating, any pointers to the old buffer would be invalid. This is not the case for the original name, as we aren't touching the original source. There are two possible approaches here. I could either refcount the buffer and have a put kfunc, or I could invalidate old dynpointers when bpf_fuse_get_rw_dynptr is called, similar to what skb/xdp do. I'm leaning towards the latter to disallow having many allocations active at once by calling bpf_fuse_get_rw_dynptr for increasing sizes, though I could also just disallow reallocating a buffer that already was reallocated.

Yes, invalidating dynptrs sounds like a way to go. But I think instead of bundling all that into dynptr constructor for fuse_buffer, it's better to have a separate kfunc that would be doing realloc/cloning *and* invalidating. Other than that, neither from_buf_rw nor from_buf_ro should be doing invalidation, because they can't cause realloc. WDYT?

Quoted 6 lines

The new dynptr helpers are pretty exciting since they'll make it much easier to deal with chunks of data, which we may end up doing in read/write filters. I haven't fully set those up since I was waiting to see what the dynptr helpers ended up looking like.

Great, let us know how it goes in practice to start using them.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

+void bpf_fuse_get_ro_dynptr(const struct fuse_buffer *buffer, struct bpf_dynptr_kern *dynptr__uninit)

these kfuncs probably should be more consistently named as bpf_dynptr_from_fuse_buffer_{ro,rw}() ?

Yeah, that fits in much better with the skb/xdp functions.

great

Quoted 10 lines
Quoted 9 lines

kernel/bpf/bpf_fuse.c line 88

Quoted 5 lines
kernel/bpf/bpf_fuse.c new file +4 -0
+
+uint32_t bpf_fuse_return_len(struct fuse_buffer *buffer)
+{
+       return buffer->size;

you should be able to get this with bpf_dynptr_size() (once you create it from fuse_buffer).

Quoted 7 lines

Yes, this might be unnecessary. I added it while testing kfuncs, and had intended to use it with a fuse_buffer strncmp before I saw that there's now a bpf_strncmp :) I had tried using it with bpf_dynptr_slice, but that requires a known constant at verification time, which may make using it in real cases a bit difficult... bpf_strncmp also has some restrictions around the second string being a fixed map, or something like that.

right, we might need a more flexible strncmp version working with two dynptrs and not assuming a fixed string. We didn't have dynptr abstraction for working with variable-sized memory when we were adding bpf_strncmp.

Quoted 6 lines
Quoted 6 lines

you probably should be fine with just using bpf_tracing_func_proto as is

Quoted 1 line

+ .is_valid_access = bpf_fuse_is_valid_access,

Quoted 8 lines
Quoted 2 lines

similarly, why custom no-op callback?

Those are largely carried over from iterations when I was less sure what I would need. A lot of the work I was doing in the v1 code is handled by default with the struct_op setup now, or is otherwise unnecessary. This area in particular needs a lot of cleanup.

ok

Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
kernel/bpf/bpf_fuse.c new file +4 -0
+static int bpf_fuse_init(struct btf *btf)
+{
+       s32 type_id;
+
Quoted 9 lines

kernel/bpf/bpf_fuse.c line 193

Quoted 5 lines
kernel/bpf/bpf_fuse.c new file +5 -0
+       type_id = btf_find_by_name_kind(btf, "fuse_buffer", BTF_KIND_STRUCT);
+       if (type_id < 0)
+               return -EINVAL;
+       fuse_buffer_struct_type = btf_type_by_id(btf, type_id);
+

see BTF_ID and BTF_ID_LIST uses for how to get ID for your custom well-known type

Thanks, I'll look into those.

#36 lore raw

RFC PATCH v3 29/37 fuse-bpf: Export Functions

In reply to Daniel Rosenberg

These functions needed to be exported to build fuse as a module

---
 kernel/bpf/bpf_struct_ops.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/bpf/bpf_struct_ops.c +2 -0
index deb9eecaf1e4..0bf727996a08 100644
@@ -745,6 +745,7 @@ bool bpf_struct_ops_get(const void *kdata)
 	map = __bpf_map_inc_not_zero(&st_map->map, false);
 	return !IS_ERR(map);
 }
+EXPORT_SYMBOL_GPL(bpf_struct_ops_get);
 
 void bpf_struct_ops_put(const void *kdata)
 {
@@ -756,6 +757,7 @@ void bpf_struct_ops_put(const void *kdata)
 
 	bpf_map_put(&st_map->map);
 }
+EXPORT_SYMBOL_GPL(bpf_struct_ops_put);
 
 static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
 {
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#37 lore raw

RFC PATCH v3 30/37 fuse: Provide registration functions for fuse-bpf

In reply to Daniel Rosenberg

Fuse may be built as a module, but verifier components are not. This provides a means for fuse-bpf to handle struct op programs once the module is loaded.

---
 fs/fuse/Makefile         |   2 +-
 fs/fuse/backing.c        |   2 +
 fs/fuse/bpf_register.c   | 209 +++++++++++++++++++++++++++++++++++++++
 fs/fuse/fuse_i.h         |  26 +++++
 include/linux/bpf_fuse.h |   8 ++
 5 files changed, 246 insertions(+), 1 deletion(-)
 create mode 100644 fs/fuse/bpf_register.c
fs/fuse/Makefile +1 -1
index a0853c439db2..903253db7285 100644
@@ -9,6 +9,6 @@ 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-$(CONFIG_FUSE_DAX) += dax.o
-fuse-$(CONFIG_FUSE_BPF) += backing.o
+fuse-$(CONFIG_FUSE_BPF) += backing.o bpf_register.o
 
 virtiofs-y := virtio_fs.o
fs/fuse/backing.c +2 -0
index e807ae4f6f53..898ef9e05e9d 100644
@@ -3360,6 +3360,7 @@ int fuse_bpf_access(int *out, struct inode *inode, int mask)
 
 int __init fuse_bpf_init(void)
 {
+	init_fuse_bpf();
 	fuse_bpf_aio_request_cachep = kmem_cache_create("fuse_bpf_aio_req",
 						   sizeof(struct fuse_bpf_aio_req),
 						   0, SLAB_HWCACHE_ALIGN, NULL);
@@ -3371,5 +3372,6 @@ int __init fuse_bpf_init(void)
 
 void __exit fuse_bpf_cleanup(void)
 {
+	uninit_fuse_bpf();
 	kmem_cache_destroy(fuse_bpf_aio_request_cachep);
 }
fs/fuse/bpf_register.c new file +209 -0

209 lines not on this page · raw message

fs/fuse/fuse_i.h +26 -0
index 2bd45c8658e8..84c591d02e43 100644
@@ -1390,6 +1390,32 @@ 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_BPF
+struct fuse_ops *find_fuse_ops(const char *key);
+struct fuse_ops *get_fuse_ops(struct fuse_ops *ops);
+void put_fuse_ops(struct fuse_ops *ops);
+int init_fuse_bpf(void);
+void uninit_fuse_bpf(void);
+#else
+int init_fuse_bpf(void)
+{
+	return -EOPNOTSUPP;
+}
+void uninit_fuse_bpf(void)
+{
+}
+struct fuse_ops *find_fuse_ops(const char *key)
+{
+	return NULL;
+}
+struct fuse_ops *get_fuse_ops(struct fuse_ops *ops)
+{
+	return NULL;
+}
+void put_fuse_ops(struct fuse_ops *ops)
+{
+}
+#endif
 
 enum fuse_bpf_set {
 	FUSE_BPF_UNCHANGED = 0,
include/linux/bpf_fuse.h +8 -0
index 780a7889aea2..2183a7a45c92 100644
@@ -270,4 +270,12 @@ struct fuse_ops {
 	char name[BPF_FUSE_NAME_MAX];
 };
 
+struct bpf_fuse_ops_attach {
+	int (*fuse_register_bpf)(struct fuse_ops *f_ops);
+	void (*fuse_unregister_bpf)(struct fuse_ops *f_ops);
+};
+
+int register_fuse_bpf(struct bpf_fuse_ops_attach *reg_ops);
+void unregister_fuse_bpf(struct bpf_fuse_ops_attach *reg_ops);
+
 #endif /* _BPF_FUSE_H */
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#38 lore raw

Re: [RFC PATCH v3 30/37] fuse: Provide registration functions for fuse-bpf

kernel test robot noticed the following build errors:

Hi Daniel,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on 49859de997c3115b85544bce6b6ceab60a7fabc4]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Rosenberg/bpf-verifier-Accept-dynptr-mem-as-mem-in-herlpers/20230418-094717
base:   49859de997c3115b85544bce6b6ceab60a7fabc4
patch link:    https://lore.kernel.org/r/20230418014037.2412394-31-drosen%40google.com
patch subject: [RFC PATCH v3 30/37] fuse: Provide registration functions for fuse-bpf
config: m68k-randconfig-r016-20230418 (https://download.01.org/0day-ci/archive/20230418/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/ff2c02ca675655ec88e525c51defda64d7774b0d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Daniel-Rosenberg/bpf-verifier-Accept-dynptr-mem-as-mem-in-herlpers/20230418-094717 git checkout ff2c02ca675655ec88e525c51defda64d7774b0d
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=m68k olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=m68k SHELL=/bin/bash fs/fuse/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

fs/fuse/bpf_register.c: In function 'qstr_eq':

>> fs/fuse/bpf_register.c:52:26: warning: variable 'r3' set but not used [-Wunused-but-set-variable]
      52 |         int res, r1, r2, r3;
         |                          ^~
>> fs/fuse/bpf_register.c:52:22: warning: variable 'r2' set but not used [-Wunused-but-set-variable]
      52 |         int res, r1, r2, r3;
         |                      ^~
>> fs/fuse/bpf_register.c:52:18: warning: variable 'r1' set but not used [-Wunused-but-set-variable]
      52 |         int res, r1, r2, r3;
         |                  ^~
   fs/fuse/bpf_register.c: In function 'get_fuse_ops':
>> fs/fuse/bpf_register.c:82:37: error: 'BPF_MODULE_OWNER' undeclared (first use in this function)
      82 |         if (bpf_try_module_get(ops, BPF_MODULE_OWNER))
         |                                     ^~~~~~~~~~~~~~~~
   fs/fuse/bpf_register.c:82:37: note: each undeclared identifier is reported only once for each function it appears in
   fs/fuse/bpf_register.c: In function 'put_fuse_ops':
   fs/fuse/bpf_register.c:91:37: error: 'BPF_MODULE_OWNER' undeclared (first use in this function)
      91 |                 bpf_module_put(ops, BPF_MODULE_OWNER);
         |                                     ^~~~~~~~~~~~~~~~
   fs/fuse/bpf_register.c: In function 'register_fuse_op':
   fs/fuse/bpf_register.c:145:39: error: 'BPF_MODULE_OWNER' undeclared (first use in this function)
     145 |         if (bpf_try_module_get(value, BPF_MODULE_OWNER))
         |                                       ^~~~~~~~~~~~~~~~
   fs/fuse/bpf_register.c: In function 'unregister_fuse_op':
   fs/fuse/bpf_register.c:171:31: error: 'BPF_MODULE_OWNER' undeclared (first use in this function)
     171 |         bpf_module_put(value, BPF_MODULE_OWNER);
         |                               ^~~~~~~~~~~~~~~~
   fs/fuse/bpf_register.c: In function 'get_fuse_ops':
   fs/fuse/bpf_register.c:86:1: error: control reaches end of non-void function [-Werror=return-type]
      86 | }
         | ^

cc1: some warnings being treated as errors

vim +/BPF_MODULE_OWNER +82 fs/fuse/bpf_register.c

    49	
    50	static inline int qstr_eq(const struct qstr *s1, const struct qstr *s2)
    51	{
  > 52		int res, r1, r2, r3;
    53	
    54		r1 = s1->len == s2->len;
    55		r2 = s1->hash == s2->hash;
    56		r3 = memcmp(s1->name, s2->name, s1->len);
    57		res = (s1->len == s2->len && s1->hash == s2->hash && !memcmp(s1->name, s2->name, s1->len));
    58		return res;
    59	}
    60	
    61	static struct fuse_ops *__find_fuse_ops(const struct qstr *key)
    62	{
    63		struct hashtable_entry *hash_cur;
    64		unsigned int hash = key->hash;
    65		struct fuse_ops *ret_ops;
    66	
    67		rcu_read_lock();
    68		hash_for_each_possible_rcu(name_to_ops, hash_cur, hlist, hash) {
    69			if (qstr_eq(key, &hash_cur->key)) {
    70				ret_ops = hash_cur->ops;
    71				ret_ops = get_fuse_ops(ret_ops);
    72				rcu_read_unlock();
    73				return ret_ops;
    74			}
    75		}
    76		rcu_read_unlock();
    77		return NULL;
    78	}
    79	
    80	struct fuse_ops *get_fuse_ops(struct fuse_ops *ops)
    81	{
  > 82		if (bpf_try_module_get(ops, BPF_MODULE_OWNER))
    83			return ops;
    84		else
    85			return NULL;
    86	}
    87	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#39 lore raw

Re: [RFC PATCH v3 30/37] fuse: Provide registration functions for fuse-bpf

In reply to Daniel Rosenberg
kernel test robot noticed the following build warnings:

Hi Daniel,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on 49859de997c3115b85544bce6b6ceab60a7fabc4]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Rosenberg/bpf-verifier-Accept-dynptr-mem-as-mem-in-herlpers/20230418-094717
base:   49859de997c3115b85544bce6b6ceab60a7fabc4
patch link:    https://lore.kernel.org/r/20230418014037.2412394-31-drosen%40google.com
patch subject: [RFC PATCH v3 30/37] fuse: Provide registration functions for fuse-bpf
config: x86_64-randconfig-a012-20230417 (https://download.01.org/0day-ci/archive/20230418/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/ff2c02ca675655ec88e525c51defda64d7774b0d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Daniel-Rosenberg/bpf-verifier-Accept-dynptr-mem-as-mem-in-herlpers/20230418-094717 git checkout ff2c02ca675655ec88e525c51defda64d7774b0d
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash fs/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from fs/fuse/dev.c:9:

>> fs/fuse/fuse_i.h:1400:5: warning: no previous prototype for function 'init_fuse_bpf' [-Wmissing-prototypes]
   int init_fuse_bpf(void)
       ^
   fs/fuse/fuse_i.h:1400:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int init_fuse_bpf(void)
   ^
   static 
>> fs/fuse/fuse_i.h:1404:6: warning: no previous prototype for function 'uninit_fuse_bpf' [-Wmissing-prototypes]
   void uninit_fuse_bpf(void)
        ^
   fs/fuse/fuse_i.h:1404:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void uninit_fuse_bpf(void)
   ^
   static 
>> fs/fuse/fuse_i.h:1407:18: warning: no previous prototype for function 'find_fuse_ops' [-Wmissing-prototypes]
   struct fuse_ops *find_fuse_ops(const char *key)
                    ^
   fs/fuse/fuse_i.h:1407:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   struct fuse_ops *find_fuse_ops(const char *key)
   ^
   static 
>> fs/fuse/fuse_i.h:1411:18: warning: no previous prototype for function 'get_fuse_ops' [-Wmissing-prototypes]
   struct fuse_ops *get_fuse_ops(struct fuse_ops *ops)
                    ^
   fs/fuse/fuse_i.h:1411:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   struct fuse_ops *get_fuse_ops(struct fuse_ops *ops)
   ^
   static 
>> fs/fuse/fuse_i.h:1415:6: warning: no previous prototype for function 'put_fuse_ops' [-Wmissing-prototypes]
   void put_fuse_ops(struct fuse_ops *ops)
        ^

fs/fuse/fuse_i.h:1415:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void put_fuse_ops(struct fuse_ops *ops)
^
static
5 warnings generated.

vim +/init_fuse_bpf +1400 fs/fuse/fuse_i.h

1385
1386 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
1387 unsigned int open_flags, bool isdir,
1388 struct file *file);
1389 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
1390 unsigned int open_flags, fl_owner_t id, bool isdir);
1391
1392 /* backing.c */
1393 #ifdef CONFIG_FUSE_BPF
1394 struct fuse_ops *find_fuse_ops(const char *key);
1395 struct fuse_ops *get_fuse_ops(struct fuse_ops *ops);
1396 void put_fuse_ops(struct fuse_ops *ops);
1397 int init_fuse_bpf(void);
1398 void uninit_fuse_bpf(void);
1399 #else

> 1400	int init_fuse_bpf(void)
  1401	{
  1402		return -EOPNOTSUPP;
  1403	}
> 1404	void uninit_fuse_bpf(void)
  1405	{
  1406	}
> 1407	struct fuse_ops *find_fuse_ops(const char *key)
  1408	{
  1409		return NULL;
  1410	}
> 1411	struct fuse_ops *get_fuse_ops(struct fuse_ops *ops)
  1412	{
  1413		return NULL;
  1414	}
> 1415	void put_fuse_ops(struct fuse_ops *ops)

1416 {
1417 }
1418 #endif
1419

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#40 lore raw

Re: [RFC PATCH v3 30/37] fuse: Provide registration functions for fuse-bpf

In reply to Daniel Rosenberg
kernel test robot noticed the following build errors:

Hi Daniel,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on 49859de997c3115b85544bce6b6ceab60a7fabc4]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Rosenberg/bpf-verifier-Accept-dynptr-mem-as-mem-in-herlpers/20230418-094717
base:   49859de997c3115b85544bce6b6ceab60a7fabc4
patch link:    https://lore.kernel.org/r/20230418014037.2412394-31-drosen%40google.com
patch subject: [RFC PATCH v3 30/37] fuse: Provide registration functions for fuse-bpf
config: i386-randconfig-a004-20230417 (https://download.01.org/0day-ci/archive/20230418/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0

reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/ff2c02ca675655ec88e525c51defda64d7774b0d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Daniel-Rosenberg/bpf-verifier-Accept-dynptr-mem-as-mem-in-herlpers/20230418-094717 git checkout ff2c02ca675655ec88e525c51defda64d7774b0d
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=i386 olddefconfig
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

ld: fs/fuse/dir.o: in function `init_fuse_bpf':

>> fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
   ld: fs/fuse/dir.o: in function `uninit_fuse_bpf':
>> fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
   ld: fs/fuse/dir.o: in function `find_fuse_ops':
>> fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
   ld: fs/fuse/dir.o: in function `get_fuse_ops':
>> fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
   ld: fs/fuse/dir.o: in function `put_fuse_ops':
>> fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here
   ld: fs/fuse/file.o: in function `init_fuse_bpf':
>> fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
   ld: fs/fuse/file.o: in function `uninit_fuse_bpf':
>> fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
   ld: fs/fuse/file.o: in function `find_fuse_ops':
>> fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
   ld: fs/fuse/file.o: in function `get_fuse_ops':
>> fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
   ld: fs/fuse/file.o: in function `put_fuse_ops':
>> fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here
   ld: fs/fuse/inode.o: in function `init_fuse_bpf':
>> fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
   ld: fs/fuse/inode.o: in function `uninit_fuse_bpf':
>> fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
   ld: fs/fuse/inode.o: in function `find_fuse_ops':
>> fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
   ld: fs/fuse/inode.o: in function `get_fuse_ops':
>> fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
   ld: fs/fuse/inode.o: in function `put_fuse_ops':
>> fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here
   ld: fs/fuse/control.o: in function `init_fuse_bpf':
>> fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
   ld: fs/fuse/control.o: in function `uninit_fuse_bpf':
>> fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
   ld: fs/fuse/control.o: in function `find_fuse_ops':
>> fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
   ld: fs/fuse/control.o: in function `get_fuse_ops':
>> fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
   ld: fs/fuse/control.o: in function `put_fuse_ops':
>> fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here

ld: fs/fuse/xattr.o: in function `init_fuse_bpf':
fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
ld: fs/fuse/xattr.o: in function `uninit_fuse_bpf':
fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
ld: fs/fuse/xattr.o: in function `find_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/xattr.o: in function `get_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/xattr.o: in function `put_fuse_ops':
fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here
ld: fs/fuse/acl.o: in function `init_fuse_bpf':
fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
ld: fs/fuse/acl.o: in function `uninit_fuse_bpf':
fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
ld: fs/fuse/acl.o: in function `find_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/acl.o: in function `get_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/acl.o: in function `put_fuse_ops':
fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here
ld: fs/fuse/readdir.o: in function `init_fuse_bpf':
fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
ld: fs/fuse/readdir.o: in function `uninit_fuse_bpf':
fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
ld: fs/fuse/readdir.o: in function `find_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/readdir.o: in function `get_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/readdir.o: in function `put_fuse_ops':
fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here
ld: fs/fuse/ioctl.o: in function `init_fuse_bpf':
fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
ld: fs/fuse/ioctl.o: in function `uninit_fuse_bpf':
fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
ld: fs/fuse/ioctl.o: in function `find_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/ioctl.o: in function `get_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/ioctl.o: in function `put_fuse_ops':
fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here
ld: fs/fuse/cuse.o: in function `init_fuse_bpf':
fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
ld: fs/fuse/cuse.o: in function `uninit_fuse_bpf':
fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
ld: fs/fuse/cuse.o: in function `find_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/cuse.o: in function `get_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/cuse.o: in function `put_fuse_ops':
fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here
ld: fs/fuse/virtio_fs.o: in function `init_fuse_bpf':
fs/fuse/fuse_i.h:1403: multiple definition of `init_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1403: first defined here
ld: fs/fuse/virtio_fs.o: in function `uninit_fuse_bpf':
fs/fuse/fuse_i.h:1406: multiple definition of `uninit_fuse_bpf'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1406: first defined here
ld: fs/fuse/virtio_fs.o: in function `find_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `find_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/virtio_fs.o: in function `get_fuse_ops':
fs/fuse/fuse_i.h:1410: multiple definition of `get_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1410: first defined here
ld: fs/fuse/virtio_fs.o: in function `put_fuse_ops':
fs/fuse/fuse_i.h:1417: multiple definition of `put_fuse_ops'; fs/fuse/dev.o:fs/fuse/fuse_i.h:1417: first defined here
--
In file included from fs/fuse/inode.c:9:

>> fs/fuse/fuse_i.h:1400:5: warning: no previous prototype for 'init_fuse_bpf' [-Wmissing-prototypes]
    1400 | int init_fuse_bpf(void)
         |     ^~~~~~~~~~~~~
>> fs/fuse/fuse_i.h:1404:6: warning: no previous prototype for 'uninit_fuse_bpf' [-Wmissing-prototypes]
    1404 | void uninit_fuse_bpf(void)
         |      ^~~~~~~~~~~~~~~
>> fs/fuse/fuse_i.h:1407:18: warning: no previous prototype for 'find_fuse_ops' [-Wmissing-prototypes]
    1407 | struct fuse_ops *find_fuse_ops(const char *key)
         |                  ^~~~~~~~~~~~~
>> fs/fuse/fuse_i.h:1411:18: warning: no previous prototype for 'get_fuse_ops' [-Wmissing-prototypes]
    1411 | struct fuse_ops *get_fuse_ops(struct fuse_ops *ops)
         |                  ^~~~~~~~~~~~
>> fs/fuse/fuse_i.h:1415:6: warning: no previous prototype for 'put_fuse_ops' [-Wmissing-prototypes]

1415 | void put_fuse_ops(struct fuse_ops *ops)
| ^~~~~~~~~~~~

vim +1403 fs/fuse/fuse_i.h

1385
1386 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
1387 unsigned int open_flags, bool isdir,
1388 struct file *file);
1389 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
1390 unsigned int open_flags, fl_owner_t id, bool isdir);
1391
1392 /* backing.c */
1393 #ifdef CONFIG_FUSE_BPF
1394 struct fuse_ops *find_fuse_ops(const char *key);
1395 struct fuse_ops *get_fuse_ops(struct fuse_ops *ops);
1396 void put_fuse_ops(struct fuse_ops *ops);
1397 int init_fuse_bpf(void);
1398 void uninit_fuse_bpf(void);
1399 #else

> 1400	int init_fuse_bpf(void)
  1401	{
  1402		return -EOPNOTSUPP;
> 1403	}
> 1404	void uninit_fuse_bpf(void)
  1405	{
> 1406	}
> 1407	struct fuse_ops *find_fuse_ops(const char *key)
  1408	{
  1409		return NULL;
> 1410	}
> 1411	struct fuse_ops *get_fuse_ops(struct fuse_ops *ops)
  1412	{
  1413		return NULL;
  1414	}
> 1415	void put_fuse_ops(struct fuse_ops *ops)
  1416	{
> 1417	}

1418 #endif
1419

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#41 lore raw

RFC PATCH v3 31/37 fuse-bpf: Set fuse_ops at mount or lookup time

In reply to Daniel Rosenberg

This adds the ability to associate a fuse_op struct_op program with inodes in fuse. This can be done at mount time at the root level, or by inode at lookup time.

---
 fs/fuse/backing.c | 91 +++++++++++++++++++++++++++++++++++++++++++----
 fs/fuse/dir.c     | 16 +++++++--
 fs/fuse/fuse_i.h  | 12 +++++++
 fs/fuse/inode.c   | 28 ++++++++++++++-
 4 files changed, 138 insertions(+), 9 deletions(-)
fs/fuse/backing.c +85 -6
index 898ef9e05e9d..d5ba1e334e69 100644
@@ -6,6 +6,7 @@
 
 #include "fuse_i.h"
 
+#include <linux/bpf.h>
 #include <linux/bpf_fuse.h>
 #include <linux/fdtable.h>
 #include <linux/file.h>
@@ -168,12 +169,13 @@ static void fuse_get_backing_path(struct file *file, struct path *path)
 
 static bool has_file(int type)
 {
-	return type == FUSE_ENTRY_BACKING;
+	return (type == FUSE_ENTRY_BACKING);
 }
 
 /*
- * The optional fuse bpf entry lists the backing file for a particular
- * lookup. These are inherited by default.
+ * The optional fuse bpf entry lists the bpf and backing files for a particular
+ * lookup. These are inherited by default. A Bpf requires a backing file to be
+ * meaningful.
  *
  * In the future, we may support multiple bpfs, and multiple backing files for
  * the bpf to choose between.
@@ -182,14 +184,14 @@ static bool has_file(int type)
  * file. Changing only the bpf is valid, though meaningless if there isn't an
  * inherited backing file.
  *
- * Support for the bpf program will be added in a later patch
- *
  */
 int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num)
 {
 	struct fuse_bpf_entry_out *fbeo;
+	struct fuse_ops *ops;
 	struct file *file;
 	bool has_backing = false;
+	bool has_bpf_ops = false;
 	int num_entries;
 	int err = -EINVAL;
 	int i;
@@ -227,6 +229,11 @@ int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num)
 				goto out_err;
 			fbe->backing_action = FUSE_BPF_REMOVE;
 			break;
+		case FUSE_ENTRY_REMOVE_BPF:
+			if (fbe->bpf_action || i == 2)
+				goto out_err;
+			fbe->bpf_action = FUSE_BPF_REMOVE;
+			break;
 		case FUSE_ENTRY_BACKING:
 			if (fbe->backing_action)
 				goto out_err;
@@ -234,8 +241,17 @@ int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num)
 			fbe->backing_action = FUSE_BPF_SET;
 			has_backing = true;
 			break;
+		case FUSE_ENTRY_BPF:
+			if (fbe->bpf_action || i == 2)
+				goto out_err;
+			ops = find_fuse_ops(fbeo->name);
+			if (!ops)
+				goto out_err;
+			has_bpf_ops = true;
+			fbe->bpf_action = FUSE_BPF_SET;
+			fbe->ops = ops;
+			break;
 		default:
-			err = -EINVAL;
 			goto out_err;
 		}
 		if (has_file(fbeo->entry_type)) {
@@ -252,6 +268,10 @@ int parse_fuse_bpf_entry(struct fuse_bpf_entry *fbe, int num)
 		fput(file);
 	if (has_backing)
 		path_put_init(&fbe->backing_path);
+	if (has_bpf_ops) {
+		put_fuse_ops(fbe->ops);
+		fbe->ops = NULL;
+	}
 	return err;
 }
 
@@ -527,6 +547,15 @@ static int fuse_create_open_backing(struct bpf_fuse_args *fa, int *out,
 		goto out;
 	}
 
+	if (get_fuse_inode(inode)->bpf_ops)
+		put_fuse_ops(get_fuse_inode(inode)->bpf_ops);
+	get_fuse_inode(inode)->bpf_ops = dir_fuse_inode->bpf_ops;
+	if (get_fuse_inode(inode)->bpf_ops)
+		if (!get_fuse_ops(get_fuse_inode(inode)->bpf_ops)) {
+			*out = -EINVAL;
+			goto out;
+		}
+
 	newent = d_splice_alias(inode, entry);
 	if (IS_ERR(newent)) {
 		*out = PTR_ERR(newent);
@@ -1842,6 +1871,52 @@ int fuse_handle_backing(struct fuse_bpf_entry *fbe, struct path *backing_path)
 	return 0;
 }
 
+int fuse_handle_bpf_ops(struct fuse_bpf_entry *fbe, struct inode *parent,
+			 struct fuse_ops **ops)
+{
+	struct fuse_ops *new_ops;
+
+	/* Parent isn't presented, but we want to keep
+	 * Don't touch bpf program at all in this case
+	 */
+	if (fbe->bpf_action == FUSE_BPF_UNCHANGED && !parent)
+		return 0;
+
+	switch (fbe->bpf_action) {
+	case FUSE_BPF_UNCHANGED: {
+		struct fuse_inode *pi = get_fuse_inode(parent);
+
+		new_ops = pi->bpf_ops;
+		if (new_ops && !get_fuse_ops(new_ops))
+			return -EINVAL;
+		break;
+	}
+
+	case FUSE_BPF_REMOVE:
+		new_ops = NULL;
+		break;
+
+	case FUSE_BPF_SET:
+		new_ops = fbe->ops;
+
+		if (!new_ops)
+			return -EINVAL;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	/* Cannot change existing program */
+	if (*ops) {
+		put_fuse_ops(new_ops);
+		return new_ops == *ops ? 0 : -EINVAL;
+	}
+
+	*ops = new_ops;
+	return 0;
+}
+
 static int fuse_lookup_finalize(struct bpf_fuse_args *fa, struct dentry **out,
 				struct inode *dir, struct dentry *entry, unsigned int flags)
 {
@@ -1879,6 +1954,10 @@ static int fuse_lookup_finalize(struct bpf_fuse_args *fa, struct dentry **out,
 	if (IS_ERR(inode))
 		return PTR_ERR(inode);
 
+	error = fuse_handle_bpf_ops(fbe, dir, &get_fuse_inode(inode)->bpf_ops);
+	if (error)
+		return error;
+
 	get_fuse_inode(inode)->nodeid = feo->nodeid;
 
 	*out = d_splice_alias(inode, entry);
fs/fuse/dir.c +14 -2
index d1c3b2bfb0b1..b7bc8260a537 100644
@@ -10,6 +10,7 @@
 
 #include <linux/pagemap.h>
 #include <linux/file.h>
+#include <linux/filter.h>
 #include <linux/fs_context.h>
 #include <linux/moduleparam.h>
 #include <linux/sched.h>
@@ -185,6 +186,7 @@ static bool backing_data_changed(struct fuse_inode *fi, struct dentry *entry,
 {
 	struct path new_backing_path;
 	struct inode *new_backing_inode;
+	struct fuse_ops *ops = NULL;
 	int err;
 	bool ret = true;
 
@@ -199,9 +201,15 @@ static bool backing_data_changed(struct fuse_inode *fi, struct dentry *entry,
 	if (err)
 		goto put_inode;
 
-	ret = (fi->backing_inode != new_backing_inode ||
-			!path_equal(&get_fuse_dentry(entry)->backing_path, &new_backing_path));
+	err = fuse_handle_bpf_ops(bpf_arg, entry->d_parent->d_inode, &ops);
+	if (err)
+		goto put_bpf;
 
+	ret = (ops != fi->bpf_ops || fi->backing_inode != new_backing_inode ||
+			!path_equal(&get_fuse_dentry(entry)->backing_path, &new_backing_path));
+put_bpf:
+	if (ops)
+		put_fuse_ops(ops);
 put_inode:
 	path_put(&new_backing_path);
 	return ret;
@@ -466,6 +474,10 @@ int fuse_lookup_name(struct super_block *sb, u64 nodeid,
 		*inode = fuse_iget_backing(sb, outarg->nodeid, backing_inode);
 		if (!*inode)
 			goto out_queue_forget;
+
+		err = fuse_handle_bpf_ops(&bpf_arg, NULL, &get_fuse_inode(*inode)->bpf_ops);
+		if (err)
+			goto out;
 	} else
 #endif
 	{
fs/fuse/fuse_i.h +12 -0
index 84c591d02e43..15962ab3b381 100644
@@ -33,6 +33,7 @@
 #include <linux/pid_namespace.h>
 #include <linux/refcount.h>
 #include <linux/user_namespace.h>
+#include <linux/bpf_fuse.h>
 #include <linux/magic.h>
 
 /** Default max number of pages that can be used in a single read request */
@@ -109,6 +110,12 @@ struct fuse_inode {
 	 * If this is set, nodeid is 0.
 	 */
 	struct inode *backing_inode;
+
+	/**
+	 * fuse_ops, provides handlers to run on all operations to determine
+	 * whether to pass through or handle in place
+	 */
+	struct fuse_ops *bpf_ops;
 #endif
 
 	/** Unique ID, which identifies the inode between userspace
@@ -571,6 +578,7 @@ struct fuse_fs_context {
 	unsigned int max_read;
 	unsigned int blksize;
 	const char *subtype;
+	struct fuse_ops *root_ops;
 	struct file *root_dir;
 
 	/* DAX device, may be NULL */
@@ -1428,6 +1436,8 @@ struct fuse_bpf_entry {
 
 	enum fuse_bpf_set backing_action;
 	struct path backing_path;
+	enum fuse_bpf_set bpf_action;
+	struct fuse_ops *ops;
 	bool is_used;
 };
 
@@ -1651,6 +1661,8 @@ static inline int fuse_bpf_access(int *out, struct inode *inode, int mask)
 ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma);
 
 int fuse_handle_backing(struct fuse_bpf_entry *fbe, struct path *backing_path);
+int fuse_handle_bpf_ops(struct fuse_bpf_entry *fbe, struct inode *parent,
+			 struct fuse_ops **ops);
 
 int fuse_revalidate_backing(struct dentry *entry, unsigned int flags);
 
fs/fuse/inode.c +27 -1
index 31f34962bc9b..7fd79efbdac1 100644
@@ -80,6 +80,7 @@ static struct inode *fuse_alloc_inode(struct super_block *sb)
 	fi->inval_mask = 0;
 #ifdef CONFIG_FUSE_BPF
 	fi->backing_inode = NULL;
+	fi->bpf_ops = NULL;
 #endif
 	fi->nodeid = 0;
 	fi->nlookup = 0;
@@ -125,6 +126,9 @@ static void fuse_evict_inode(struct inode *inode)
 
 #ifdef CONFIG_FUSE_BPF
 	iput(fi->backing_inode);
+	if (fi->bpf_ops)
+		put_fuse_ops(fi->bpf_ops);
+	fi->bpf_ops = NULL;
 #endif
 
 	truncate_inode_pages_final(&inode->i_data);
@@ -755,6 +759,7 @@ enum {
 	OPT_ALLOW_OTHER,
 	OPT_MAX_READ,
 	OPT_BLKSIZE,
+	OPT_ROOT_BPF,
 	OPT_ROOT_DIR,
 	OPT_NO_DAEMON,
 	OPT_ERR
@@ -771,6 +776,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_string	("root_bpf",		OPT_ROOT_BPF),
 	fsparam_u32	("root_dir",		OPT_ROOT_DIR),
 	fsparam_flag	("no_daemon",		OPT_NO_DAEMON),
 	{}
@@ -856,6 +862,18 @@ static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param)
 		ctx->blksize = result.uint_32;
 		break;
 
+	case OPT_ROOT_BPF:
+		if (strnlen(param->string, BPF_FUSE_NAME_MAX + 1) > BPF_FUSE_NAME_MAX) {
+			return invalfc(fsc, "root_bpf name too long. Max length is %d", BPF_FUSE_NAME_MAX);
+		}
+
+		ctx->root_ops = find_fuse_ops(param->string);
+		if (IS_ERR_OR_NULL(ctx->root_ops)) {
+			ctx->root_ops = NULL;
+			return invalfc(fsc, "Unable to find bpf program");
+		}
+		break;
+
 	case OPT_ROOT_DIR:
 		ctx->root_dir = fget(result.uint_32);
 		if (!ctx->root_dir)
@@ -881,6 +899,8 @@ static void fuse_free_fsc(struct fs_context *fsc)
 	if (ctx) {
 		if (ctx->root_dir)
 			fput(ctx->root_dir);
+		if (ctx->root_ops)
+			put_fuse_ops(ctx->root_ops);
 		kfree(ctx->subtype);
 		kfree(ctx);
 	}
@@ -1010,6 +1030,7 @@ EXPORT_SYMBOL_GPL(fuse_conn_get);
 
 static struct inode *fuse_get_root_inode(struct super_block *sb,
 					 unsigned int mode,
+					 struct fuse_ops *root_bpf_ops,
 					 struct file *backing_fd)
 {
 	struct fuse_attr attr;
@@ -1024,6 +1045,10 @@ static struct inode *fuse_get_root_inode(struct super_block *sb,
 		return NULL;
 
 #ifdef CONFIG_FUSE_BPF
+	get_fuse_inode(inode)->bpf_ops = root_bpf_ops;
+	if (root_bpf_ops)
+		get_fuse_ops(root_bpf_ops);
+
 	if (backing_fd) {
 		get_fuse_inode(inode)->backing_inode = backing_fd->f_inode;
 		ihold(backing_fd->f_inode);
@@ -1704,7 +1729,8 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
 	fc->no_daemon = ctx->no_daemon;
 
 	err = -ENOMEM;
-	root = fuse_get_root_inode(sb, ctx->rootmode, ctx->root_dir);
+	root = fuse_get_root_inode(sb, ctx->rootmode, ctx->root_ops,
+				   ctx->root_dir);
 	sb->s_d_op = &fuse_root_dentry_operations;
 	root_dentry = d_make_root(root);
 	if (!root_dentry)
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#42 lore raw

RFC PATCH v3 32/37 fuse-bpf: Call bpf for pre/post filters

In reply to Daniel Rosenberg

This allows altering input or output parameters to fuse calls that will be handled directly by the backing filesystems. BPF programs can signal whether the entire operation should instead go through regular fuse, or if a postfilter call is needed.

---
 fs/fuse/backing.c | 606 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 606 insertions(+)
fs/fuse/backing.c +606 -0
index d5ba1e334e69..9217e9f83d98 100644
@@ -14,6 +14,27 @@
 #include <linux/namei.h>
 #include <linux/uio.h>
 
+static inline void bpf_fuse_set_in_immutable(struct bpf_fuse_args *fa)
+{
+	int i;
+
+	for (i = 0; i < FUSE_MAX_ARGS_IN; i++)
+		if (fa->in_args[i].is_buffer)
+			fa->in_args[i].buffer->flags |= BPF_FUSE_IMMUTABLE;
+}
+
+static inline void bpf_fuse_free_alloced(struct bpf_fuse_args *fa)
+{
+	int i;
+
+	for (i = 0; i < FUSE_MAX_ARGS_IN; i++)
+		if (fa->in_args[i].is_buffer && (fa->in_args[i].buffer->flags & BPF_FUSE_ALLOCATED))
+			kfree(fa->in_args[i].buffer->data);
+	for (i = 0; i < FUSE_MAX_ARGS_OUT; i++)
+		if (fa->out_args[i].is_buffer && (fa->out_args[i].buffer->flags & BPF_FUSE_ALLOCATED))
+			kfree(fa->out_args[i].buffer->data);
+}
+
 /*
  * expression statement to wrap the backing filter logic
  * struct inode *inode: inode with bpf and backing inode
@@ -23,6 +44,10 @@
  *     up fa and io based on args
  * void initialize_out(struct bpf_fuse_args *fa, io *in_out, args...): function that sets
  *     up fa and io based on args
+ * int call_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta, io *in_out): Calls
+ *     the struct_op prefilter function for the given fuse op
+ * int call_prostfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta, io *in_out): Calls
+ *     the struct_op postfilter function for the given fuse op
  * int backing(struct fuse_bpf_args_internal *fa, args...): function that actually performs
  *     the backing io operation
  * void *finalize(struct fuse_bpf_args *, args...): function that performs any final
@@ -30,13 +55,16 @@
  */
 #define bpf_fuse_backing(inode, io, out,				\
 			 initialize_in, initialize_out,			\
+			 call_prefilter, call_postfilter,		\
 			 backing, finalize, args...)			\
 ({									\
 	struct fuse_inode *fuse_inode = get_fuse_inode(inode);		\
+	struct fuse_ops *fuse_ops = fuse_inode->bpf_ops;		\
 	struct bpf_fuse_args fa = { 0 };				\
 	bool initialized = false;					\
 	bool handled = false;						\
 	ssize_t res;							\
+	int bpf_next;							\
 	io feo = { 0 };							\
 	int error = 0;							\
 									\
@@ -49,16 +77,46 @@
 		if (error)						\
 			break;						\
 									\
+		fa.info.opcode |= FUSE_PREFILTER;			\
+		if (fuse_ops)						\
+			bpf_next = call_prefilter(fuse_ops,		\
+						&fa.info, &feo);	\
+		else							\
+			bpf_next = BPF_FUSE_CONTINUE;			\
+		if (bpf_next < 0) {					\
+			error = bpf_next;				\
+			break;						\
+		}							\
+									\
+		bpf_fuse_set_in_immutable(&fa);				\
+									\
 		error = initialize_out(&fa, &feo, args);		\
 		if (error)						\
 			break;						\
 									\
 		initialized = true;					\
+		if (bpf_next == BPF_FUSE_USER) {			\
+			handled = false;				\
+			break;						\
+		}							\
+									\
+		fa.info.opcode &= ~FUSE_PREFILTER;			\
 									\
 		error = backing(&fa, out, args);			\
 		if (error < 0)						\
 			fa.info.error_in = error;			\
 									\
+		if (bpf_next == BPF_FUSE_CONTINUE)			\
+			break;						\
+									\
+		fa.info.opcode |= FUSE_POSTFILTER;			\
+		if (bpf_next == BPF_FUSE_POSTFILTER)			\
+			bpf_next = call_postfilter(fuse_ops, &fa.info, &feo);\
+		if (bpf_next < 0) {					\
+			error = bpf_next;				\
+			break;						\
+		}							\
+									\
 	} while (false);						\
 									\
 	if (initialized && handled) {					\
@@ -66,6 +124,7 @@
 		if (res)						\
 			error = res;					\
 	}								\
+	bpf_fuse_free_alloced(&fa);					\
 									\
 	*out = error ? _Generic((*out),					\
 			default :					\
@@ -351,6 +410,34 @@ static int fuse_open_initialize_out(struct bpf_fuse_args *fa, struct fuse_open_a
 	return 0;
 }
 
+static int fuse_open_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_open_args *open)
+{
+	if (meta->opcode == (FUSE_OPEN | FUSE_PREFILTER)) {
+		if (ops->open_prefilter)
+			return ops->open_prefilter(meta, &open->in);
+	}
+	if (meta->opcode == (FUSE_OPENDIR | FUSE_PREFILTER)) {
+		if (ops->opendir_prefilter)
+			return ops->opendir_prefilter(meta, &open->in);
+	}
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_open_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_open_args *open)
+{
+	if (meta->opcode == (FUSE_OPEN | FUSE_POSTFILTER)) {
+		if (ops->open_postfilter)
+			return ops->open_postfilter(meta, &open->in, &open->out);
+	}
+	if (meta->opcode == (FUSE_OPENDIR | FUSE_POSTFILTER)) {
+		if (ops->opendir_postfilter)
+			return ops->opendir_postfilter(meta, &open->in, &open->out);
+	}
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_open_backing(struct bpf_fuse_args *fa, int *out,
 			     struct inode *inode, struct file *file, bool isdir)
 {
@@ -419,6 +506,7 @@ int fuse_bpf_open(int *out, struct inode *inode, struct file *file, bool isdir)
 {
 	return bpf_fuse_backing(inode, struct fuse_open_args, out,
 				fuse_open_initialize_in, fuse_open_initialize_out,
+				fuse_open_prefilter, fuse_open_postfilter,
 				fuse_open_backing, fuse_open_finalize,
 				inode, file, isdir);
 }
@@ -484,6 +572,22 @@ static int fuse_create_open_initialize_out(struct bpf_fuse_args *fa, struct fuse
 	return 0;
 }
 
+static int fuse_create_open_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_create_open_args *args)
+{
+	if (ops->create_open_prefilter)
+		return ops->create_open_prefilter(meta, &args->in, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_create_open_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_create_open_args *args)
+{
+	if (ops->create_open_postfilter)
+		return ops->create_open_postfilter(meta, &args->in, &args->name, &args->entry_out, &args->open_out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_open_file_backing(struct inode *inode, struct file *file)
 {
 	struct fuse_mount *fm = get_fuse_mount(inode);
@@ -586,12 +690,16 @@ static int fuse_create_open_finalize(struct bpf_fuse_args *fa, int *out,
 	return 0;
 }
 
+
+
 int fuse_bpf_create_open(int *out, struct inode *dir, struct dentry *entry,
 			 struct file *file, unsigned int flags, umode_t mode)
 {
 	return bpf_fuse_backing(dir, struct fuse_create_open_args, out,
 				fuse_create_open_initialize_in,
 				fuse_create_open_initialize_out,
+				fuse_create_open_prefilter,
+				fuse_create_open_postfilter,
 				fuse_create_open_backing,
 				fuse_create_open_finalize,
 				dir, entry, file, flags, mode);
@@ -652,6 +760,38 @@ static int fuse_releasedir_initialize_in(struct bpf_fuse_args *fa,
 	return 0;
 }
 
+static int fuse_release_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_release_in *args)
+{
+	if (ops->release_prefilter)
+		return ops->release_prefilter(meta, args);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_release_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_release_in *args)
+{
+	if (ops->release_postfilter)
+		return ops->release_postfilter(meta, args);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_releasedir_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_release_in *args)
+{
+	if (ops->releasedir_prefilter)
+		return ops->releasedir_prefilter(meta, args);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_releasedir_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_release_in *args)
+{
+	if (ops->releasedir_postfilter)
+		return ops->releasedir_postfilter(meta, args);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_releasedir_initialize_out(struct bpf_fuse_args *fa,
 					  struct fuse_release_in *fri,
 					  struct inode *inode, struct file *file)
@@ -677,6 +817,7 @@ int fuse_bpf_release(int *out, struct inode *inode, struct file *file)
 {
 	return bpf_fuse_backing(inode, struct fuse_release_in, out,
 				fuse_release_initialize_in, fuse_release_initialize_out,
+				fuse_release_prefilter, fuse_release_postfilter,
 				fuse_release_backing, fuse_release_finalize,
 				inode, file);
 }
@@ -685,6 +826,7 @@ int fuse_bpf_releasedir(int *out, struct inode *inode, struct file *file)
 {
 	return bpf_fuse_backing(inode, struct fuse_release_in, out,
 				fuse_releasedir_initialize_in, fuse_releasedir_initialize_out,
+				fuse_releasedir_prefilter, fuse_releasedir_postfilter,
 				fuse_release_backing, fuse_release_finalize, inode, file);
 }
 
@@ -717,6 +859,22 @@ static int fuse_flush_initialize_out(struct bpf_fuse_args *fa, struct fuse_flush
 	return 0;
 }
 
+static int fuse_flush_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_flush_in *args)
+{
+	if (ops->flush_prefilter)
+		return ops->flush_prefilter(meta, args);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_flush_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_flush_in *args)
+{
+	if (ops->flush_postfilter)
+		return ops->flush_postfilter(meta, args);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_flush_backing(struct bpf_fuse_args *fa, int *out, struct file *file, fl_owner_t id)
 {
 	struct fuse_file *fuse_file = file->private_data;
@@ -737,6 +895,7 @@ int fuse_bpf_flush(int *out, struct inode *inode, struct file *file, fl_owner_t
 {
 	return bpf_fuse_backing(inode, struct fuse_flush_in, out,
 				fuse_flush_initialize_in, fuse_flush_initialize_out,
+				fuse_flush_prefilter, fuse_flush_postfilter,
 				fuse_flush_backing, fuse_flush_finalize,
 				file, id);
 }
@@ -780,6 +939,22 @@ static int fuse_lseek_initialize_out(struct bpf_fuse_args *fa, struct fuse_lseek
 	return 0;
 }
 
+static int fuse_lseek_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_lseek_args *args)
+{
+	if (ops->lseek_prefilter)
+		return ops->lseek_prefilter(meta, &args->in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_lseek_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_lseek_args *args)
+{
+	if (ops->lseek_postfilter)
+		return ops->lseek_postfilter(meta, &args->in, &args->out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_lseek_backing(struct bpf_fuse_args *fa, loff_t *out,
 			      struct file *file, loff_t offset, int whence)
 {
@@ -826,6 +1001,7 @@ int fuse_bpf_lseek(loff_t *out, struct inode *inode, struct file *file, loff_t o
 {
 	return bpf_fuse_backing(inode, struct fuse_lseek_args, out,
 				fuse_lseek_initialize_in, fuse_lseek_initialize_out,
+				fuse_lseek_prefilter, fuse_lseek_postfilter,
 				fuse_lseek_backing, fuse_lseek_finalize,
 				file, offset, whence);
 }
@@ -878,6 +1054,22 @@ static int fuse_copy_file_range_initialize_out(struct bpf_fuse_args *fa,
 	return 0;
 }
 
+static int fuse_copy_file_range_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_copy_file_range_args *args)
+{
+	if (ops->copy_file_range_prefilter)
+		return ops->copy_file_range_prefilter(meta, &args->in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_copy_file_range_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_copy_file_range_args *args)
+{
+	if (ops->copy_file_range_postfilter)
+		return ops->copy_file_range_postfilter(meta, &args->in, &args->out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_copy_file_range_backing(struct bpf_fuse_args *fa, ssize_t *out, struct file *file_in,
 					loff_t pos_in, struct file *file_out, loff_t pos_out, size_t len,
 					unsigned int flags)
@@ -912,6 +1104,8 @@ int fuse_bpf_copy_file_range(ssize_t *out, struct inode *inode, struct file *fil
 	return bpf_fuse_backing(inode, struct fuse_copy_file_range_args, out,
 				fuse_copy_file_range_initialize_in,
 				fuse_copy_file_range_initialize_out,
+				fuse_copy_file_range_prefilter,
+				fuse_copy_file_range_postfilter,
 				fuse_copy_file_range_backing,
 				fuse_copy_file_range_finalize,
 				file_in, pos_in, file_out, pos_out, len, flags);
@@ -947,6 +1141,22 @@ static int fuse_fsync_initialize_out(struct bpf_fuse_args *fa, struct fuse_fsync
 	return 0;
 }
 
+static int fuse_fsync_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_fsync_in *in)
+{
+	if (ops->fsync_prefilter)
+		return ops->fsync_prefilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_fsync_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_fsync_in *in)
+{
+	if (ops->fsync_postfilter)
+		return ops->fsync_postfilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_fsync_backing(struct bpf_fuse_args *fa, int *out,
 			      struct file *file, loff_t start, loff_t end, int datasync)
 {
@@ -969,6 +1179,7 @@ int fuse_bpf_fsync(int *out, struct inode *inode, struct file *file, loff_t star
 {
 	return bpf_fuse_backing(inode, struct fuse_fsync_in, out,
 				fuse_fsync_initialize_in, fuse_fsync_initialize_out,
+				fuse_fsync_prefilter, fuse_fsync_postfilter,
 				fuse_fsync_backing, fuse_fsync_finalize,
 				file, start, end, datasync);
 }
@@ -1003,10 +1214,27 @@ static int fuse_dir_fsync_initialize_out(struct bpf_fuse_args *fa, struct fuse_f
 	return 0;
 }
 
+static int fuse_dir_fsync_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_fsync_in *in)
+{
+	if (ops->dir_fsync_prefilter)
+		return ops->fsync_prefilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_dir_fsync_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_fsync_in *in)
+{
+	if (ops->dir_fsync_postfilter)
+		return ops->dir_fsync_postfilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
 int fuse_bpf_dir_fsync(int *out, struct inode *inode, struct file *file, loff_t start, loff_t end, int datasync)
 {
 	return bpf_fuse_backing(inode, struct fuse_fsync_in, out,
 				fuse_dir_fsync_initialize_in, fuse_dir_fsync_initialize_out,
+				fuse_dir_fsync_prefilter, fuse_dir_fsync_postfilter,
 				fuse_fsync_backing, fuse_fsync_finalize,
 				file, start, end, datasync);
 }
@@ -1076,6 +1304,22 @@ static int fuse_getxattr_initialize_out(struct bpf_fuse_args *fa,
 	return 0;
 }
 
+static int fuse_getxattr_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_getxattr_args *args)
+{
+	if (ops->getxattr_prefilter)
+		return ops->getxattr_prefilter(meta, &args->in, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_getxattr_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_getxattr_args *args)
+{
+	if (ops->getxattr_postfilter)
+		return ops->getxattr_postfilter(meta, &args->in, &args->name, &args->value, &args->out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_getxattr_backing(struct bpf_fuse_args *fa, int *out,
 				 struct dentry *dentry, const char *name, void *value,
 				 size_t size)
@@ -1121,6 +1365,7 @@ int fuse_bpf_getxattr(int *out, struct inode *inode, struct dentry *dentry, cons
 {
 	return bpf_fuse_backing(inode, struct fuse_getxattr_args, out,
 				fuse_getxattr_initialize_in, fuse_getxattr_initialize_out,
+				fuse_getxattr_prefilter, fuse_getxattr_postfilter,
 				fuse_getxattr_backing, fuse_getxattr_finalize,
 				dentry, name, value, size);
 }
@@ -1173,6 +1418,22 @@ static int fuse_listxattr_initialize_out(struct bpf_fuse_args *fa,
 	return 0;
 }
 
+static int fuse_listxattr_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_getxattr_args *args)
+{
+	if (ops->listxattr_prefilter)
+		return ops->listxattr_prefilter(meta, &args->in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_listxattr_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_getxattr_args *args)
+{
+	if (ops->listxattr_postfilter)
+		return ops->listxattr_postfilter(meta, &args->in, &args->value, &args->out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_listxattr_backing(struct bpf_fuse_args *fa, ssize_t *out, struct dentry *dentry,
 				  char *list, size_t size)
 {
@@ -1212,6 +1473,7 @@ int fuse_bpf_listxattr(ssize_t *out, struct inode *inode, struct dentry *dentry,
 {
 	return bpf_fuse_backing(inode, struct fuse_getxattr_args, out,
 				fuse_listxattr_initialize_in, fuse_listxattr_initialize_out,
+				fuse_listxattr_prefilter, fuse_listxattr_postfilter,
 				fuse_listxattr_backing, fuse_listxattr_finalize,
 				dentry, list, size);
 }
@@ -1277,6 +1539,22 @@ static int fuse_setxattr_initialize_out(struct bpf_fuse_args *fa,
 	return 0;
 }
 
+static int fuse_setxattr_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_setxattr_args *args)
+{
+	if (ops->setxattr_prefilter)
+		return ops->setxattr_prefilter(meta, &args->in, &args->name, &args->value);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_setxattr_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_setxattr_args *args)
+{
+	if (ops->setxattr_postfilter)
+		return ops->setxattr_postfilter(meta, &args->in, &args->name, &args->value);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_setxattr_backing(struct bpf_fuse_args *fa, int *out, struct dentry *dentry,
 				 const char *name, const void *value, size_t size,
 				 int flags)
@@ -1300,6 +1578,7 @@ int fuse_bpf_setxattr(int *out, struct inode *inode, struct dentry *dentry,
 {
 	return bpf_fuse_backing(inode, struct fuse_setxattr_args, out,
 			       fuse_setxattr_initialize_in, fuse_setxattr_initialize_out,
+			       fuse_setxattr_prefilter, fuse_setxattr_postfilter,
 			       fuse_setxattr_backing, fuse_setxattr_finalize,
 			       dentry, name, value, size, flags);
 }
@@ -1336,6 +1615,22 @@ static int fuse_removexattr_initialize_out(struct bpf_fuse_args *fa,
 	return 0;
 }
 
+static int fuse_removexattr_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				      struct fuse_buffer *in)
+{
+	if (ops->removexattr_prefilter)
+		return ops->removexattr_prefilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_removexattr_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				       struct fuse_buffer *in)
+{
+	if (ops->removexattr_postfilter)
+		return ops->removexattr_postfilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_removexattr_backing(struct bpf_fuse_args *fa, int *out,
 				    struct dentry *dentry, const char *name)
 {
@@ -1356,6 +1651,7 @@ int fuse_bpf_removexattr(int *out, struct inode *inode, struct dentry *dentry, c
 {
 	return bpf_fuse_backing(inode, struct fuse_buffer, out,
 				fuse_removexattr_initialize_in, fuse_removexattr_initialize_out,
+				fuse_removexattr_prefilter, fuse_removexattr_postfilter,
 				fuse_removexattr_backing, fuse_removexattr_finalize,
 				dentry, name);
 }
@@ -1446,6 +1742,22 @@ static int fuse_file_read_iter_initialize_out(struct bpf_fuse_args *fa, struct f
 	return 0;
 }
 
+static int fuse_file_read_iter_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_file_read_iter_args *args)
+{
+	if (ops->read_iter_prefilter)
+		return ops->read_iter_prefilter(meta, &args->in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_file_read_iter_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_file_read_iter_args *args)
+{
+	if (ops->read_iter_postfilter)
+		return ops->read_iter_postfilter(meta, &args->in, &args->out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_file_read_iter_backing(struct bpf_fuse_args *fa, ssize_t *out,
 				       struct kiocb *iocb, struct iov_iter *to)
 {
@@ -1513,6 +1825,8 @@ int fuse_bpf_file_read_iter(ssize_t *out, struct inode *inode, struct kiocb *ioc
 	return bpf_fuse_backing(inode, struct fuse_file_read_iter_args, out,
 				fuse_file_read_iter_initialize_in,
 				fuse_file_read_iter_initialize_out,
+				fuse_file_read_iter_prefilter,
+				fuse_file_read_iter_postfilter,
 				fuse_file_read_iter_backing,
 				fuse_file_read_iter_finalize,
 				iocb, to);
@@ -1562,6 +1876,22 @@ static int fuse_file_write_iter_initialize_out(struct bpf_fuse_args *fa,
 	return 0;
 }
 
+static int fuse_write_iter_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_file_write_iter_args *args)
+{
+	if (ops->write_iter_prefilter)
+		return ops->write_iter_prefilter(meta, &args->in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_write_iter_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_file_write_iter_args *args)
+{
+	if (ops->write_iter_postfilter)
+		return ops->write_iter_postfilter(meta, &args->in, &args->out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_file_write_iter_backing(struct bpf_fuse_args *fa, ssize_t *out,
 					struct kiocb *iocb, struct iov_iter *from)
 {
@@ -1626,6 +1956,8 @@ int fuse_bpf_file_write_iter(ssize_t *out, struct inode *inode, struct kiocb *io
 	return bpf_fuse_backing(inode, struct fuse_file_write_iter_args, out,
 				fuse_file_write_iter_initialize_in,
 				fuse_file_write_iter_initialize_out,
+				fuse_write_iter_prefilter,
+				fuse_write_iter_postfilter,
 				fuse_file_write_iter_backing,
 				fuse_file_write_iter_finalize,
 				iocb, from);
@@ -1701,6 +2033,22 @@ static int fuse_file_fallocate_initialize_out(struct bpf_fuse_args *fa,
 	return 0;
 }
 
+static int fuse_file_fallocate_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_fallocate_in *in)
+{
+	if (ops->file_fallocate_prefilter)
+		return ops->file_fallocate_prefilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_file_fallocate_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_fallocate_in *in)
+{
+	if (ops->file_fallocate_postfilter)
+		return ops->file_fallocate_postfilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_file_fallocate_backing(struct bpf_fuse_args *fa, int *out,
 				       struct file *file, int mode, loff_t offset, loff_t length)
 {
@@ -1723,6 +2071,8 @@ int fuse_bpf_file_fallocate(int *out, struct inode *inode, struct file *file, in
 	return bpf_fuse_backing(inode, struct fuse_fallocate_in, out,
 				fuse_file_fallocate_initialize_in,
 				fuse_file_fallocate_initialize_out,
+				fuse_file_fallocate_prefilter,
+				fuse_file_fallocate_postfilter,
 				fuse_file_fallocate_backing,
 				fuse_file_fallocate_finalize,
 				file, mode, offset, length);
@@ -1790,6 +2140,22 @@ static int fuse_lookup_initialize_out(struct bpf_fuse_args *fa, struct fuse_look
 	return 0;
 }
 
+static int fuse_lookup_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_lookup_args *args)
+{
+	if (ops->lookup_prefilter)
+		return ops->lookup_prefilter(meta, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_lookup_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_lookup_args *args)
+{
+	if (ops->lookup_postfilter)
+		return ops->lookup_postfilter(meta, &args->name, &args->out, &args->bpf_entries);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_lookup_backing(struct bpf_fuse_args *fa, struct dentry **out, struct inode *dir,
 			       struct dentry *entry, unsigned int flags)
 {
@@ -1968,6 +2334,7 @@ int fuse_bpf_lookup(struct dentry **out, struct inode *dir, struct dentry *entry
 {
 	return bpf_fuse_backing(dir, struct fuse_lookup_args, out,
 				fuse_lookup_initialize_in, fuse_lookup_initialize_out,
+				fuse_lookup_prefilter, fuse_lookup_postfilter,
 				fuse_lookup_backing, fuse_lookup_finalize,
 				dir, entry, flags);
 }
@@ -2034,6 +2401,22 @@ static int fuse_mknod_initialize_out(struct bpf_fuse_args *fa, struct fuse_mknod
 	return 0;
 }
 
+static int fuse_mknod_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_mknod_args *args)
+{
+	if (ops->mknod_prefilter)
+		return ops->mknod_prefilter(meta, &args->in, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_mknod_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_mknod_args *args)
+{
+	if (ops->mknod_postfilter)
+		return ops->mknod_postfilter(meta, &args->in, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_mknod_backing(struct bpf_fuse_args *fa, int *out,
 			      struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev)
 {
@@ -2086,6 +2469,7 @@ int fuse_bpf_mknod(int *out, struct inode *dir, struct dentry *entry, umode_t mo
 {
 	return bpf_fuse_backing(dir, struct fuse_mknod_args, out,
 				fuse_mknod_initialize_in, fuse_mknod_initialize_out,
+				fuse_mknod_prefilter, fuse_mknod_postfilter,
 				fuse_mknod_backing, fuse_mknod_finalize,
 				dir, entry, mode, rdev);
 }
@@ -2187,10 +2571,27 @@ static int fuse_mkdir_finalize(struct bpf_fuse_args *fa, int *out,
 	return 0;
 }
 
+static int fuse_mkdir_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_mkdir_args *args)
+{
+	if (ops->mkdir_prefilter)
+		return ops->mkdir_prefilter(meta, &args->in, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_mkdir_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				 struct fuse_mkdir_args *args)
+{
+	if (ops->mkdir_prefilter)
+		return ops->mkdir_postfilter(meta, &args->in, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
 int fuse_bpf_mkdir(int *out, struct inode *dir, struct dentry *entry, umode_t mode)
 {
 	return bpf_fuse_backing(dir, struct fuse_mkdir_args, out,
 				fuse_mkdir_initialize_in, fuse_mkdir_initialize_out,
+				fuse_mkdir_prefilter, fuse_mkdir_postfilter,
 				fuse_mkdir_backing, fuse_mkdir_finalize,
 				dir, entry, mode);
 }
@@ -2224,6 +2625,22 @@ static int fuse_rmdir_initialize_out(struct bpf_fuse_args *fa, struct fuse_buffe
 	return 0;
 }
 
+static int fuse_rmdir_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_buffer *name)
+{
+	if (ops->rmdir_prefilter)
+		return ops->rmdir_prefilter(meta, name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_rmdir_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_buffer *name)
+{
+	if (ops->rmdir_postfilter)
+		return ops->rmdir_postfilter(meta, name);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_rmdir_backing(struct bpf_fuse_args *fa, int *out,
 			      struct inode *dir, struct dentry *entry)
 {
@@ -2258,6 +2675,7 @@ int fuse_bpf_rmdir(int *out, struct inode *dir, struct dentry *entry)
 {
 	return bpf_fuse_backing(dir, struct fuse_buffer, out,
 				fuse_rmdir_initialize_in, fuse_rmdir_initialize_out,
+				fuse_rmdir_prefilter, fuse_rmdir_postfilter,
 				fuse_rmdir_backing, fuse_rmdir_finalize,
 				dir, entry);
 }
@@ -2400,6 +2818,22 @@ static int fuse_rename2_initialize_out(struct bpf_fuse_args *fa, struct fuse_ren
 	return 0;
 }
 
+static int fuse_rename2_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_rename2_args *args)
+{
+	if (ops->rename2_prefilter)
+		return ops->rename2_prefilter(meta, &args->in, &args->old_name, &args->new_name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_rename2_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_rename2_args *args)
+{
+	if (ops->rename2_postfilter)
+		return ops->rename2_postfilter(meta, &args->in, &args->old_name, &args->new_name);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_rename2_backing(struct bpf_fuse_args *fa, int *out,
 				struct inode *olddir, struct dentry *oldent,
 				struct inode *newdir, struct dentry *newent,
@@ -2427,6 +2861,7 @@ int fuse_bpf_rename2(int *out, struct inode *olddir, struct dentry *oldent,
 {
 	return bpf_fuse_backing(olddir, struct fuse_rename2_args, out,
 				fuse_rename2_initialize_in, fuse_rename2_initialize_out,
+				fuse_rename2_prefilter, fuse_rename2_postfilter,
 				fuse_rename2_backing, fuse_rename2_finalize,
 				olddir, oldent, newdir, newent, flags);
 }
@@ -2487,6 +2922,22 @@ static int fuse_rename_initialize_out(struct bpf_fuse_args *fa, struct fuse_rena
 	return 0;
 }
 
+static int fuse_rename_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_rename_args *args)
+{
+	if (ops->rename_prefilter)
+		return ops->rename_prefilter(meta, &args->in, &args->old_name, &args->new_name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_rename_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_rename_args *args)
+{
+	if (ops->rename_postfilter)
+		return ops->rename_postfilter(meta, &args->in, &args->old_name, &args->new_name);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_rename_backing(struct bpf_fuse_args *fa, int *out,
 			       struct inode *olddir, struct dentry *oldent,
 			       struct inode *newdir, struct dentry *newent)
@@ -2508,6 +2959,7 @@ int fuse_bpf_rename(int *out, struct inode *olddir, struct dentry *oldent,
 {
 	return bpf_fuse_backing(olddir, struct fuse_rename_args, out,
 				fuse_rename_initialize_in, fuse_rename_initialize_out,
+				fuse_rename_prefilter, fuse_rename_postfilter,
 				fuse_rename_backing, fuse_rename_finalize,
 				olddir, oldent, newdir, newent);
 }
@@ -2541,6 +2993,22 @@ static int fuse_unlink_initialize_out(struct bpf_fuse_args *fa, struct fuse_buff
 	return 0;
 }
 
+static int fuse_unlink_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_buffer *name)
+{
+	if (ops->unlink_prefilter)
+		return ops->unlink_prefilter(meta, name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_unlink_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_buffer *name)
+{
+	if (ops->unlink_postfilter)
+		return ops->unlink_postfilter(meta, name);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_unlink_backing(struct bpf_fuse_args *fa, int *out, struct inode *dir, struct dentry *entry)
 {
 	struct path backing_path;
@@ -2577,6 +3045,7 @@ int fuse_bpf_unlink(int *out, struct inode *dir, struct dentry *entry)
 {
 	return bpf_fuse_backing(dir, struct fuse_buffer, out,
 				fuse_unlink_initialize_in, fuse_unlink_initialize_out,
+				fuse_unlink_prefilter, fuse_unlink_postfilter,
 				fuse_unlink_backing, fuse_unlink_finalize,
 				dir, entry);
 }
@@ -2629,6 +3098,23 @@ static int fuse_link_initialize_out(struct bpf_fuse_args *fa, struct fuse_link_a
 	return 0;
 }
 
+static int fuse_link_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_link_args *args)
+{
+	if (ops->link_prefilter)
+		return ops->link_prefilter(meta, &args->in, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_link_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_link_args *args)
+{
+	if (ops->link_postfilter)
+		return ops->link_postfilter(meta, &args->in, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
+
 static int fuse_link_backing(struct bpf_fuse_args *fa, int *out, struct dentry *entry,
 			     struct inode *dir, struct dentry *newent)
 {
@@ -2696,6 +3182,7 @@ int fuse_bpf_link(int *out, struct inode *inode, struct dentry *entry,
 {
 	return bpf_fuse_backing(inode, struct fuse_link_args, out,
 				fuse_link_initialize_in, fuse_link_initialize_out,
+				fuse_link_prefilter, fuse_link_postfilter,
 				fuse_link_backing, fuse_link_finalize,
 				entry, newdir, newent);
 }
@@ -2744,6 +3231,22 @@ static int fuse_getattr_initialize_out(struct bpf_fuse_args *fa, struct fuse_get
 	return 0;
 }
 
+static int fuse_getattr_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_getattr_args *args)
+{
+	if (ops->getattr_prefilter)
+		return ops->getattr_prefilter(meta, &args->in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_getattr_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_getattr_args *args)
+{
+	if (ops->getattr_postfilter)
+		return ops->getattr_postfilter(meta, &args->in, &args->out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_getattr_backing(struct bpf_fuse_args *fa, int *out,
 				const struct dentry *entry, struct kstat *stat,
 				u32 request_mask, unsigned int flags)
@@ -2804,6 +3307,7 @@ int fuse_bpf_getattr(int *out, struct inode *inode, const struct dentry *entry,
 {
 	return bpf_fuse_backing(inode, struct fuse_getattr_args, out,
 				fuse_getattr_initialize_in, fuse_getattr_initialize_out,
+				fuse_getattr_prefilter, fuse_getattr_postfilter,
 				fuse_getattr_backing, fuse_getattr_finalize,
 				entry, stat, request_mask, flags);
 }
@@ -2883,6 +3387,22 @@ static int fuse_setattr_initialize_out(struct bpf_fuse_args *fa, struct fuse_set
 	return 0;
 }
 
+static int fuse_setattr_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_setattr_args *args)
+{
+	if (ops->setattr_prefilter)
+		return ops->setattr_prefilter(meta, &args->in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_setattr_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_setattr_args *args)
+{
+	if (ops->setattr_postfilter)
+		return ops->setattr_postfilter(meta, &args->in, &args->out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_setattr_backing(struct bpf_fuse_args *fa, int *out,
 				struct dentry *dentry, struct iattr *attr, struct file *file)
 {
@@ -2920,6 +3440,7 @@ int fuse_bpf_setattr(int *out, struct inode *inode, struct dentry *dentry, struc
 {
 	return bpf_fuse_backing(inode, struct fuse_setattr_args, out,
 				fuse_setattr_initialize_in, fuse_setattr_initialize_out,
+				fuse_setattr_prefilter, fuse_setattr_postfilter,
 				fuse_setattr_backing, fuse_setattr_finalize,
 				dentry, attr, file);
 }
@@ -2949,6 +3470,22 @@ static int fuse_statfs_initialize_out(struct bpf_fuse_args *fa, struct fuse_stat
 	return 0;
 }
 
+static int fuse_statfs_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_statfs_out *out)
+{
+	if (ops->statfs_prefilter)
+		return ops->statfs_prefilter(meta);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_statfs_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_statfs_out *out)
+{
+	if (ops->statfs_postfilter)
+		return ops->statfs_postfilter(meta, out);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_statfs_backing(struct bpf_fuse_args *fa, int *out,
 			       struct dentry *dentry, struct kstatfs *buf)
 {
@@ -2984,6 +3521,7 @@ int fuse_bpf_statfs(int *out, struct inode *inode, struct dentry *dentry, struct
 {
 	return bpf_fuse_backing(dentry->d_inode, struct fuse_statfs_out, out,
 				fuse_statfs_initialize_in, fuse_statfs_initialize_out,
+				fuse_statfs_prefilter, fuse_statfs_postfilter,
 				fuse_statfs_backing, fuse_statfs_finalize,
 				dentry, buf);
 }
@@ -3053,6 +3591,22 @@ static int fuse_get_link_initialize_out(struct bpf_fuse_args *fa, struct fuse_ge
 	return 0;
 }
 
+static int fuse_get_link_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_get_link_args *args)
+{
+	if (ops->get_link_prefilter)
+		return ops->get_link_prefilter(meta, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_get_link_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_get_link_args *args)
+{
+	if (ops->get_link_postfilter)
+		return ops->get_link_postfilter(meta, &args->name);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_get_link_backing(struct bpf_fuse_args *fa, const char **out,
 				 struct inode *inode, struct dentry *dentry,
 				 struct delayed_call *callback)
@@ -3092,6 +3646,7 @@ int fuse_bpf_get_link(const char **out, struct inode *inode, struct dentry *dent
 {
 	return bpf_fuse_backing(inode, struct fuse_get_link_args, out,
 				fuse_get_link_initialize_in, fuse_get_link_initialize_out,
+				fuse_get_link_prefilter, fuse_get_link_postfilter,
 				fuse_get_link_backing, fuse_get_link_finalize,
 				inode, dentry, callback);
 }
@@ -3142,6 +3697,22 @@ static int fuse_symlink_initialize_out(struct bpf_fuse_args *fa, struct fuse_sym
 	return 0;
 }
 
+static int fuse_symlink_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_symlink_args *args)
+{
+	if (ops->symlink_prefilter)
+		return ops->symlink_prefilter(meta, &args->name, &args->path);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_symlink_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_symlink_args *args)
+{
+	if (ops->symlink_postfilter)
+		return ops->symlink_postfilter(meta, &args->name, &args->path);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_symlink_backing(struct bpf_fuse_args *fa, int *out,
 				struct inode *dir, struct dentry *entry, const char *link, int len)
 {
@@ -3192,6 +3763,7 @@ int  fuse_bpf_symlink(int *out, struct inode *dir, struct dentry *entry, const c
 {
 	return bpf_fuse_backing(dir, struct fuse_symlink_args, out,
 				fuse_symlink_initialize_in, fuse_symlink_initialize_out,
+				fuse_symlink_prefilter, fuse_symlink_postfilter,
 				fuse_symlink_backing, fuse_symlink_finalize,
 				dir, entry, link, len);
 }
@@ -3265,6 +3837,22 @@ static int fuse_readdir_initialize_out(struct bpf_fuse_args *fa, struct fuse_rea
 	return 0;
 }
 
+static int fuse_readdir_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_read_args *args)
+{
+	if (ops->readdir_prefilter)
+		return ops->readdir_prefilter(meta, &args->in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_readdir_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_read_args *args)
+{
+	if (ops->readdir_postfilter)
+		return ops->readdir_postfilter(meta, &args->in, &args->out, &args->buffer);
+	return BPF_FUSE_CONTINUE;
+}
+
 struct fusebpf_ctx {
 	struct dir_context ctx;
 	u8 *addr;
@@ -3380,6 +3968,7 @@ int fuse_bpf_readdir(int *out, struct inode *inode, struct file *file, struct di
 again:
 	ret = bpf_fuse_backing(inode, struct fuse_read_args, out,
 			       fuse_readdir_initialize_in, fuse_readdir_initialize_out,
+			       fuse_readdir_prefilter, fuse_readdir_postfilter,
 			       fuse_readdir_backing, fuse_readdir_finalize,
 			       file, ctx, &force_again, &allow_force, is_continued);
 	if (force_again && *out >= 0) {
@@ -3416,6 +4005,22 @@ static int fuse_access_initialize_out(struct bpf_fuse_args *fa, struct fuse_acce
 	return 0;
 }
 
+static int fuse_access_prefilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+			       struct fuse_access_in *in)
+{
+	if (ops->access_prefilter)
+		return ops->access_prefilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
+static int fuse_access_postfilter(struct fuse_ops *ops, struct bpf_fuse_meta_info *meta,
+				struct fuse_access_in *in)
+{
+	if (ops->access_postfilter)
+		return ops->access_postfilter(meta, in);
+	return BPF_FUSE_CONTINUE;
+}
+
 static int fuse_access_backing(struct bpf_fuse_args *fa, int *out, struct inode *inode, int mask)
 {
 	struct fuse_inode *fi = get_fuse_inode(inode);
@@ -3434,6 +4039,7 @@ int fuse_bpf_access(int *out, struct inode *inode, int mask)
 {
 	return bpf_fuse_backing(inode, struct fuse_access_in, out,
 				fuse_access_initialize_in, fuse_access_initialize_out,
+				fuse_access_prefilter, fuse_access_postfilter,
 				fuse_access_backing, fuse_access_finalize, inode, mask);
 }
 
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#43 lore raw

RFC PATCH v3 33/37 fuse-bpf: Add userspace pre/post filters

In reply to Daniel Rosenberg

This allows fuse-bpf to call out to userspace to handle pre and post filters. Any of the inputs may be changed by the prefilter, so we must handle up to 3 outputs. For the postfilter, our inputs include the output arguments, so we must handle up to 5 inputs.

Additionally, we add an extension for passing the return code of the backing call to the postfilter, adding one additional possible output bringing the total to 4.

As long as you don't request both pre-filter and post-filter in userspace, we will end up doing fewer round trips to userspace.

---
 fs/fuse/backing.c         | 179 ++++++++++++++++++++++++++++++++++++++
 fs/fuse/dev.c             |   2 +
 fs/fuse/dir.c             |   6 +-
 fs/fuse/fuse_i.h          |  33 ++++++-
 include/linux/bpf_fuse.h  |   1 +
 include/uapi/linux/fuse.h |   1 +
 6 files changed, 217 insertions(+), 5 deletions(-)
fs/fuse/backing.c +179 -0
index 9217e9f83d98..1de302fc91b6 100644
@@ -14,6 +14,163 @@
 #include <linux/namei.h>
 #include <linux/uio.h>
 
+static void set_in_args(struct fuse_in_arg *dst, struct bpf_fuse_arg *src)
+{
+	if (src->is_buffer) {
+		struct fuse_buffer *buffer = src->buffer;
+
+		*dst = (struct fuse_in_arg) {
+			.size = buffer->size,
+			.value = buffer->data,
+		};
+	} else {
+		*dst = (struct fuse_in_arg) {
+			.size = src->size,
+			.value = src->value,
+		};
+	}
+}
+
+static void set_out_args(struct fuse_arg *dst, struct bpf_fuse_arg *src)
+{
+	if (src->is_buffer) {
+		struct fuse_buffer *buffer = src->buffer;
+
+		// Userspace out args presents as much space as needed
+		*dst = (struct fuse_arg) {
+			.size = buffer->max_size,
+			.value = buffer->data,
+		};
+	} else {
+		*dst = (struct fuse_arg) {
+			.size = src->size,
+			.value = src->value,
+		};
+	}
+}
+
+static int get_err_in(uint32_t error, struct fuse_in_arg *ext)
+{
+	struct fuse_ext_header *xh;
+	uint32_t *err_in;
+	uint32_t err_in_size = fuse_ext_size(sizeof(*err_in));
+
+	xh = extend_arg(ext, err_in_size);
+	if (!xh)
+		return -ENOMEM;
+	xh->size = err_in_size;
+	xh->type = FUSE_ERROR_IN;
+
+	err_in = (uint32_t *)&xh[1];
+	*err_in = error;
+	return 0;
+}
+
+static int get_filter_ext(struct fuse_args *args)
+{
+	struct fuse_in_arg ext = { .size  = 0, .value = NULL };
+	int err = 0;
+
+	if (args->is_filter)
+		err = get_err_in(args->error_in, &ext);
+	if (!err && ext.size) {
+		WARN_ON(args->in_numargs >= ARRAY_SIZE(args->in_args));
+		args->is_ext = true;
+		args->ext_idx = args->in_numargs++;
+		args->in_args[args->ext_idx] = ext;
+	} else {
+		kfree(ext.value);
+	}
+	return err;
+}
+
+static ssize_t fuse_bpf_simple_request(struct fuse_mount *fm, struct bpf_fuse_args *fa,
+				       unsigned short in_numargs, unsigned short out_numargs,
+				       struct bpf_fuse_arg *out_arg_array, bool add_out_to_in)
+{
+	int i;
+	ssize_t res;
+
+	struct fuse_args args = {
+		.nodeid = fa->info.nodeid,
+		.opcode = fa->info.opcode,
+		.error_in = fa->info.error_in,
+		.in_numargs = in_numargs,
+		.out_numargs = out_numargs,
+		.force = !!(fa->flags & FUSE_BPF_FORCE),
+		.out_argvar = !!(fa->flags & FUSE_BPF_OUT_ARGVAR),
+		.is_lookup = !!(fa->flags & FUSE_BPF_IS_LOOKUP),
+		.is_filter = true,
+	};
+
+	/* All out args must be writeable */
+	for (i = 0; i < out_numargs; ++i) {
+		struct fuse_buffer *buffer;
+
+		if (!out_arg_array[i].is_buffer)
+			continue;
+		buffer = out_arg_array[i].buffer;
+		if (!bpf_fuse_get_writeable(buffer, buffer->max_size, true))
+			return -ENOMEM;
+	}
+
+	/* Set in args */
+	for (i = 0; i < fa->in_numargs; ++i)
+		set_in_args(&args.in_args[i], &fa->in_args[i]);
+	if (add_out_to_in) {
+		for (i = 0; i < fa->out_numargs; ++i) {
+			set_in_args(&args.in_args[fa->in_numargs + i], &fa->out_args[i]);
+		}
+	}
+
+	/* Set out args */
+	for (i = 0; i < out_numargs; ++i)
+		set_out_args(&args.out_args[i], &out_arg_array[i]);
+
+	if (out_arg_array[out_numargs - 1].is_buffer) {
+		struct fuse_buffer *buff = out_arg_array[out_numargs - 1].buffer;
+
+		if (buff->flags & BPF_FUSE_VARIABLE_SIZE)
+			args.out_argvar = true;
+	}
+	if (add_out_to_in) {
+		res = get_filter_ext(&args);
+		if (res)
+			return res;
+	}
+	res = fuse_simple_request(fm, &args);
+
+	/* update used areas of buffers */
+	for (i = 0; i < out_numargs; ++i)
+		if (out_arg_array[i].is_buffer &&
+				(out_arg_array[i].buffer->flags & BPF_FUSE_VARIABLE_SIZE))
+			out_arg_array[i].buffer->size = args.out_args[i].size;
+	fa->ret = args.ret;
+
+	free_ext_value(&args);
+
+	return res;
+}
+
+static ssize_t fuse_prefilter_simple_request(struct fuse_mount *fm, struct bpf_fuse_args *fa)
+{
+	uint32_t out_args = fa->in_numargs;
+
+	// mkdir and company are not permitted to change the name. This should be done at lookup
+	// Thus, these can't be set by the userspace prefilter
+	if (fa->in_args[fa->in_numargs - 1].is_buffer &&
+			(fa->in_args[fa->in_numargs - 1].buffer->flags & BPF_FUSE_IMMUTABLE))
+		out_args--;
+	return fuse_bpf_simple_request(fm, fa, fa->in_numargs, out_args,
+				       fa->in_args, false);
+}
+
+static ssize_t fuse_postfilter_simple_request(struct fuse_mount *fm, struct bpf_fuse_args *fa)
+{
+	return fuse_bpf_simple_request(fm, fa, fa->in_numargs + fa->out_numargs, fa->out_numargs,
+				       fa->out_args, true);
+}
+
 static inline void bpf_fuse_set_in_immutable(struct bpf_fuse_args *fa)
 {
 	int i;
@@ -60,9 +217,11 @@ static inline void bpf_fuse_free_alloced(struct bpf_fuse_args *fa)
 ({									\
 	struct fuse_inode *fuse_inode = get_fuse_inode(inode);		\
 	struct fuse_ops *fuse_ops = fuse_inode->bpf_ops;		\
+	struct fuse_mount *fm = get_fuse_mount(inode);			\
 	struct bpf_fuse_args fa = { 0 };				\
 	bool initialized = false;					\
 	bool handled = false;						\
+	bool locked;							\
 	ssize_t res;							\
 	int bpf_next;							\
 	io feo = { 0 };							\
@@ -88,6 +247,16 @@ static inline void bpf_fuse_free_alloced(struct bpf_fuse_args *fa)
 			break;						\
 		}							\
 									\
+		if (bpf_next == BPF_FUSE_USER_PREFILTER) {		\
+			locked = fuse_lock_inode(inode);		\
+			res = fuse_prefilter_simple_request(fm, &fa);	\
+			fuse_unlock_inode(inode, locked);		\
+			if (res < 0) {					\
+				error = res;				\
+				break;					\
+			}						\
+			bpf_next = fa.ret;				\
+		}							\
 		bpf_fuse_set_in_immutable(&fa);				\
 									\
 		error = initialize_out(&fa, &feo, args);		\
@@ -117,6 +286,16 @@ static inline void bpf_fuse_free_alloced(struct bpf_fuse_args *fa)
 			break;						\
 		}							\
 									\
+		if (!(bpf_next == BPF_FUSE_USER_POSTFILTER))		\
+			break;						\
+									\
+		locked = fuse_lock_inode(inode);			\
+		res = fuse_postfilter_simple_request(fm, &fa);		\
+		fuse_unlock_inode(inode, locked);			\
+		if (res < 0) {						\
+			error = res;					\
+			break;						\
+		}							\
 	} while (false);						\
 									\
 	if (initialized && handled) {					\
fs/fuse/dev.c +2 -0
index ad7d9d1e6da5..139f40b70228 100644
@@ -521,6 +521,8 @@ ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args)
 		BUG_ON(args->out_numargs == 0);
 		ret = args->out_args[args->out_numargs - 1].size;
 	}
+	if (args->is_filter && args->is_ext)
+		args->ret = req->out.h.error;
 	fuse_put_request(req);
 
 	return ret;
fs/fuse/dir.c +3 -3
index b7bc8260a537..bea5f1698127 100644
@@ -620,7 +620,7 @@ static int get_security_context(struct dentry *entry, umode_t mode,
 	return err;
 }
 
-static void *extend_arg(struct fuse_in_arg *buf, u32 bytes)
+void *extend_arg(struct fuse_in_arg *buf, u32 bytes)
 {
 	void *p;
 	u32 newlen = buf->size + bytes;
@@ -640,7 +640,7 @@ static void *extend_arg(struct fuse_in_arg *buf, u32 bytes)
 	return p + newlen - bytes;
 }
 
-static u32 fuse_ext_size(size_t size)
+u32 fuse_ext_size(size_t size)
 {
 	return FUSE_REC_ALIGN(sizeof(struct fuse_ext_header) + size);
 }
@@ -700,7 +700,7 @@ static int get_create_ext(struct fuse_args *args,
 	return err;
 }
 
-static void free_ext_value(struct fuse_args *args)
+void free_ext_value(struct fuse_args *args)
 {
 	if (args->is_ext)
 		kfree(args->in_args[args->ext_idx].value);
fs/fuse/fuse_i.h +31 -2
index 15962ab3b381..0504c136632d 100644
@@ -304,6 +304,17 @@ struct fuse_page_desc {
 	unsigned int offset;
 };
 
+/* To deal with bpf pre and post filters in userspace calls, we must support
+ * passing the inputs and outputs as inputs, and we must have enough space in
+ * outputs to handle all of the inputs. Plus one more for extensions.
+ */
+#define FUSE_EXTENDED_MAX_ARGS_IN (FUSE_MAX_ARGS_IN + FUSE_MAX_ARGS_OUT + 1)
+#if FUSE_MAX_ARGS_IN > FUSE_MAX_ARGS_OUT
+#define FUSE_EXTENDED_MAX_ARGS_OUT FUSE_MAX_ARGS_IN
+#else
+#define FUSE_EXTENDED_MAX_ARGS_OUT FUSE_MAX_ARGS_OUT
+#endif
+
 struct fuse_args {
 	uint64_t nodeid;
 	uint32_t opcode;
@@ -322,10 +333,12 @@ struct fuse_args {
 	bool page_replace:1;
 	bool may_block:1;
 	bool is_ext:1;
+	bool is_filter:1;
 	bool is_lookup:1;
 	bool via_ioctl:1;
-	struct fuse_in_arg in_args[3];
-	struct fuse_arg out_args[2];
+	uint32_t ret;
+	struct fuse_in_arg in_args[FUSE_EXTENDED_MAX_ARGS_IN];
+	struct fuse_arg out_args[FUSE_EXTENDED_MAX_ARGS_OUT];
 	void (*end)(struct fuse_mount *fm, struct fuse_args *args, int error);
 };
 
@@ -1165,6 +1178,22 @@ void fuse_request_end(struct fuse_req *req);
 void fuse_abort_conn(struct fuse_conn *fc);
 void fuse_wait_aborted(struct fuse_conn *fc);
 
+/**
+ * Allocated/Reallocate extended header information
+ * Returns pointer to start of most recent allocation
+ */
+void *extend_arg(struct fuse_in_arg *buf, u32 bytes);
+
+/**
+ * Returns adjusted size field for extensions
+ */
+u32 fuse_ext_size(size_t size);
+
+/**
+ * Free allocated extended header information
+ */
+void free_ext_value(struct fuse_args *args);
+
 /**
  * Invalidate inode attributes
  */
include/linux/bpf_fuse.h +1 -0
index 2183a7a45c92..159b850e1b46 100644
@@ -64,6 +64,7 @@ struct bpf_fuse_args {
 	uint32_t in_numargs;
 	uint32_t out_numargs;
 	uint32_t flags;
+	uint32_t ret;
 	struct bpf_fuse_arg in_args[FUSE_MAX_ARGS_IN];
 	struct bpf_fuse_arg out_args[FUSE_MAX_ARGS_OUT];
 };
include/uapi/linux/fuse.h +1 -0
index e779064f5fad..bbcda421ee8e 100644
@@ -520,6 +520,7 @@ enum fuse_ext_type {
 	/* Types 0..31 are reserved for fuse_secctx_header */
 	FUSE_MAX_NR_SECCTX	= 31,
 	FUSE_EXT_GROUPS		= 32,
+	FUSE_ERROR_IN		= 33,
 };
 
 enum fuse_opcode {
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#44 lore raw

RFC PATCH v3 34/37 WIP: fuse-bpf: add error_out

In reply to Daniel Rosenberg

error_out field will allow differentiating between altering error code from bpf programs, and the bpf program returning an error. TODO

---
 include/linux/bpf_fuse.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/bpf_fuse.h +1 -0
index 159b850e1b46..15646ba59c41 100644
@@ -57,6 +57,7 @@ struct bpf_fuse_meta_info {
 	uint64_t nodeid;
 	uint32_t opcode;
 	uint32_t error_in;
+	uint32_t error_out; // TODO: struct_op programs may set this to alter reported error code
 };
 
 struct bpf_fuse_args {
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#45 lore raw

RFC PATCH v3 35/37 tools: Add FUSE, update bpf includes

In reply to Daniel Rosenberg

Updates the bpf includes under tools, and adds fuse

---
 tools/include/uapi/linux/bpf.h  |   12 +
 tools/include/uapi/linux/fuse.h | 1135 +++++++++++++++++++++++++++++++
 2 files changed, 1147 insertions(+)
 create mode 100644 tools/include/uapi/linux/fuse.h
tools/include/uapi/linux/bpf.h +12 -0
index 4b20a7269bee..6521c40875c7 100644
@@ -7155,4 +7155,16 @@ struct bpf_iter_num {
 	__u64 __opaque[1];
 } __attribute__((aligned(8)));
 
+/* Return Codes for Fuse BPF struct_op programs */
+#define BPF_FUSE_CONTINUE		0
+#define BPF_FUSE_USER			1
+#define BPF_FUSE_USER_PREFILTER		2
+#define BPF_FUSE_POSTFILTER		3
+#define BPF_FUSE_USER_POSTFILTER	4
+
+/* Op Code Filter values for BPF Programs */
+#define FUSE_OPCODE_FILTER	0x0ffff
+#define FUSE_PREFILTER		0x10000
+#define FUSE_POSTFILTER		0x20000
+
 #endif /* _UAPI__LINUX_BPF_H__ */
tools/include/uapi/linux/fuse.h new file +1135 -0

1,135 lines not on this page · raw message

Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
#46 lore raw

Re: [RFC PATCH v3 35/37] tools: Add FUSE, update bpf includes

Quoted 15 lines · Daniel Rosenberg

Updates the bpf includes under tools, and adds fuse

---
 tools/include/uapi/linux/bpf.h  |   12 +
 tools/include/uapi/linux/fuse.h | 1135 +++++++++++++++++++++++++++++++
 2 files changed, 1147 insertions(+)
 create mode 100644 tools/include/uapi/linux/fuse.h
tools/include/uapi/linux/bpf.h +0 -0
index 4b20a7269bee..6521c40875c7 100644

tools/include/uapi/linux/bpf.h struct bpf_iter_num { · line 7163

Quoted 9 lines
tools/include/uapi/linux/bpf.h +6 -0
@@ -7155,4 +7155,16 @@ struct bpf_iter_num {
        __u64 __opaque[1];
 } __attribute__((aligned(8)));
 
+/* Return Codes for Fuse BPF struct_op programs */
+#define BPF_FUSE_CONTINUE              0
+#define BPF_FUSE_USER                  1
+#define BPF_FUSE_USER_PREFILTER                2
+#define BPF_FUSE_POSTFILTER            3
+#define BPF_FUSE_USER_POSTFILTER       4

nit: can this be an enum instead? It would be more self-documenting, IMO. At given it's FUSE BPF-specific, why is it not in uapi/linux/fuse.h?

Quoted 6 lines
tools/include/uapi/linux/bpf.h +6 -0
+
+/* Op Code Filter values for BPF Programs */
+#define FUSE_OPCODE_FILTER     0x0ffff
+#define FUSE_PREFILTER         0x10000
+#define FUSE_POSTFILTER                0x20000
+

[...]

#47 lore raw

Re: [RFC PATCH v3 35/37] tools: Add FUSE, update bpf includes

Quoted 14 lines · Andrii Nakryiko

tools/include/uapi/linux/bpf.h struct bpf_iter_num { · line 7163

Quoted 7 lines · Daniel Rosenberg
tools/include/uapi/linux/bpf.h +6 -0
+/* Return Codes for Fuse BPF struct_op programs */
+#define BPF_FUSE_CONTINUE              0
+#define BPF_FUSE_USER                  1
+#define BPF_FUSE_USER_PREFILTER                2
+#define BPF_FUSE_POSTFILTER            3
+#define BPF_FUSE_USER_POSTFILTER       4

nit: can this be an enum instead? It would be more self-documenting, IMO. At given it's FUSE BPF-specific, why is it not in uapi/linux/fuse.h?

An enum would be nicer. And I'm sure there are plenty of things that are probably in the wrong place right now. I'll be moving most of the changes in bpf specific areas over to fuse specific areas when struct_ops can handle modules. This particular one can move now though.

#48 lore raw

RFC PATCH v3 36/37 fuse-bpf: Add selftests

In reply to Daniel Rosenberg

Adds basic selftests for fuse. These check that you can add fuse_op programs, and perform basic operations

---
 .../selftests/filesystems/fuse/.gitignore     |    2 +
 .../selftests/filesystems/fuse/Makefile       |  188 ++
 .../testing/selftests/filesystems/fuse/OWNERS |    2 +
 .../selftests/filesystems/fuse/bpf_common.h   |   51 +
 .../selftests/filesystems/fuse/bpf_loader.c   |  597 ++++
 .../testing/selftests/filesystems/fuse/fd.txt |   21 +
 .../selftests/filesystems/fuse/fd_bpf.bpf.c   |  397 +++
 .../selftests/filesystems/fuse/fuse_daemon.c  |  300 ++
 .../selftests/filesystems/fuse/fuse_test.c    | 2412 +++++++++++++++++
 .../selftests/filesystems/fuse/test.bpf.c     |  996 +++++++
 .../filesystems/fuse/test_framework.h         |  172 ++
 .../selftests/filesystems/fuse/test_fuse.h    |  494 ++++
 12 files changed, 5632 insertions(+)
 create mode 100644 tools/testing/selftests/filesystems/fuse/.gitignore
 create mode 100644 tools/testing/selftests/filesystems/fuse/Makefile
 create mode 100644 tools/testing/selftests/filesystems/fuse/OWNERS
 create mode 100644 tools/testing/selftests/filesystems/fuse/bpf_common.h
 create mode 100644 tools/testing/selftests/filesystems/fuse/bpf_loader.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/fd.txt
 create mode 100644 tools/testing/selftests/filesystems/fuse/fd_bpf.bpf.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_daemon.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_test.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/test.bpf.c
 create mode 100644 tools/testing/selftests/filesystems/fuse/test_framework.h
 create mode 100644 tools/testing/selftests/filesystems/fuse/test_fuse.h
tools/testing/selftests/filesystems/fuse/.gitignore new file +2 -0
new file mode 100644
index 000000000000..3ee9a27fe66a
@@ -0,0 +1,2 @@
+fuse_test
+*.raw
tools/testing/selftests/filesystems/fuse/Makefile new file +188 -0

188 lines not on this page · raw message

tools/testing/selftests/filesystems/fuse/OWNERS new file +2 -0
new file mode 100644
index 000000000000..5eb371e1a5a3
@@ -0,0 +1,2 @@
+# include OWNERS from the authoritative android-mainline branch
+include kernel/common:android-mainline:/tools/testing/selftests/filesystems/incfs/OWNERS
tools/testing/selftests/filesystems/fuse/bpf_common.h new file +51 -0

51 lines not on this page · raw message

tools/testing/selftests/filesystems/fuse/bpf_loader.c new file +597 -0

597 lines not on this page · raw message

tools/testing/selftests/filesystems/fuse/fd.txt new file +21 -0
new file mode 100644
index 000000000000..15ce77180d55
@@ -0,0 +1,21 @@
+fuse_daemon $*
+cd fd-dst
+ls
+cd show
+ls
+fsstress -s 123 -d . -p 4 -n 100 -l5
+echo test > wibble
+ls
+cat wibble
+fallocate -l 1000 wobble
+mkdir testdir
+mkdir tmpdir
+rmdir tmpdir
+touch tmp
+mv tmp tmp2
+rm tmp2
+
+# FUSE_LINK
+echo "ln_src contents" > ln_src
+ln ln_src ln_link
+cat ln_link
tools/testing/selftests/filesystems/fuse/fd_bpf.bpf.c new file +397 -0

397 lines not on this page · raw message

tools/testing/selftests/filesystems/fuse/fuse_daemon.c new file +300 -0

300 lines not on this page · raw message

tools/testing/selftests/filesystems/fuse/fuse_test.c new file +2412 -0

1,442 lines not on this page · raw message

tools/testing/selftests/filesystems/fuse/test.bpf.c new file +996 -0

996 lines not on this page · raw message

tools/testing/selftests/filesystems/fuse/test_framework.h new file +172 -0

172 lines not on this page · raw message

tools/testing/selftests/filesystems/fuse/test_fuse.h new file +494 -0

494 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.40.0.634.g4ca3ef3211-goog

Part of the diff is not shown here. View the raw message.

#49 lore raw

RFC PATCH v3 37/37 fuse: Provide easy way to test fuse struct_op call

In reply to Daniel Rosenberg

This is useful for quickly testing a struct_op program. I've been using this set up to test verifier changes.

I'll eventually move those sorts of tests to bpf selftests

---
 fs/fuse/inode.c                               |  70 ++
 .../selftests/filesystems/fuse/Makefile       |   1 +
 .../filesystems/fuse/struct_op_test.bpf.c     | 642 ++++++++++++++++++
 3 files changed, 713 insertions(+)
 create mode 100644 tools/testing/selftests/filesystems/fuse/struct_op_test.bpf.c
fs/fuse/inode.c +70 -0
index 7fd79efbdac1..d80c7282c91c 100644
@@ -2071,16 +2071,83 @@ static void fuse_fs_cleanup(void)
 
 static struct kobject *fuse_kobj;
 
+static char struct_op_name[BPF_FUSE_NAME_MAX];
+static struct fuse_ops *fop = NULL;
+
+static ssize_t struct_op_store(struct kobject *kobj,
+		  struct kobj_attribute *attr,
+		  const char *buf, size_t count)
+{
+	size_t max = count;
+
+	if (max > BPF_FUSE_NAME_MAX) max = BPF_FUSE_NAME_MAX;
+	strncpy(struct_op_name, buf, max);
+	if (struct_op_name[max-1] == '\n')
+		struct_op_name[max-1] = 0;
+	put_fuse_ops(fop);
+	fop = find_fuse_ops(struct_op_name);
+	if (!fop)
+		printk("No struct op named %s found", struct_op_name);
+
+	return count;
+}
+
+static ssize_t struct_op_show(struct kobject *kobj,
+			    struct kobj_attribute *attr, char *buf)
+{
+	struct fuse_ops *op;
+	uint32_t result = 0;
+	struct bpf_fuse_meta_info meta;
+	struct fuse_mkdir_in in;
+	struct fuse_buffer name;
+	char name_buff[10] = "test";
+
+	name.data = &name_buff[0];
+	name.flags = BPF_FUSE_VARIABLE_SIZE;
+	name.max_size = 10;
+	name.size = 5;
+
+	op = fop;
+	if (!op) {
+		printk("Could not find fuse_op for %s", struct_op_name);
+		return 0;
+	}
+
+	if (op->mkdir_prefilter)
+		result = op->mkdir_prefilter(&meta, &in, &name);
+	else
+		printk("No func!!");
+
+	printk("in->mode:%d, name:%s result:%d", in.mode, (char *)name.data, result);
+	return sprintf(buf, "%d dyn:%s\n", result, (char *)name.data);
+}
+
+static struct kobj_attribute test_attr = __ATTR_RW(struct_op);
+
+static struct attribute *test_attrs[] = {
+	&test_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group test_attr_group = {
+	.attrs = test_attrs,
+};
+
 static int fuse_sysfs_init(void)
 {
 	int err;
 
+	memset(struct_op_name, 0, BPF_FUSE_NAME_MAX);
 	fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
 	if (!fuse_kobj) {
 		err = -ENOMEM;
 		goto out_err;
 	}
 
+	err = sysfs_create_group(fuse_kobj, &test_attr_group);
+	if (err)
+		goto tmp;
+
 	err = sysfs_create_mount_point(fuse_kobj, "connections");
 	if (err)
 		goto out_fuse_unregister;
@@ -2089,6 +2156,8 @@ static int fuse_sysfs_init(void)
 
  out_fuse_unregister:
 	kobject_put(fuse_kobj);
+tmp:
+	sysfs_remove_group(fuse_kobj, &test_attr_group);
  out_err:
 	return err;
 }
@@ -2096,6 +2165,7 @@ static int fuse_sysfs_init(void)
 static void fuse_sysfs_cleanup(void)
 {
 	sysfs_remove_mount_point(fuse_kobj, "connections");
+	sysfs_remove_group(fuse_kobj, &test_attr_group);
 	kobject_put(fuse_kobj);
 }
 
tools/testing/selftests/filesystems/fuse/Makefile +1 -0
index b2df4dec0651..ff28859f3268 100644
@@ -52,6 +52,7 @@ SELFTESTS:=$(TOOLSDIR)/testing/selftests/
 LDLIBS := -lpthread -lelf -lz
 TEST_GEN_PROGS := fuse_test fuse_daemon
 TEST_GEN_FILES := \
+  struct_op_test.bpf.o \
 	test.skel.h \
 	fd.sh \
 
tools/testing/selftests/filesystems/fuse/struct_op_test.bpf.c new file +642 -0

642 lines not on this page · raw message

Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
maintainer
#50 lore raw
In reply to Daniel Rosenberg
Quoted 14 lines · Daniel Rosenberg

These patches extend FUSE to be able to act as a stacked filesystem. This allows pure passthrough, where the fuse file system simply reflects the lower filesystem, and also allows optional pre and post filtering in BPF and/or the userspace daemon as needed. This can dramatically reduce or even eliminate transitions to and from userspace.

In this patch set, I've reworked the bpf code to add a new struct_op type instead of a new program type, and used new kfuncs in place of new helpers. Additionally, it now uses dynptrs for variable sized buffers. The first three patches are repeats of a previous patch set which I have not yet adjusted for comments. I plan to adjust those and submit them separately with fixes, but wanted to have the current fuse-bpf code visible before then.

Quoted 9 lines

Patches 4-7 mostly rearrange existing code to remove noise from the main patch. Patch 8 contains the main sections of fuse-bpf
Patches 9-25 implementing most FUSE functions as operations on a lower filesystem. From patch 25, you can run fuse as a passthrough filesystem. Patches 26-32 provide bpf functionality so that you can alter fuse parameters via fuse_op programs.
Patch 33 extends this to userspace, and patches 34-37 add some testing functionality.

That's a nice logical breakup for review.

I feel there is so much subtle code in those patches that the only sane path forward is to review and merge them in phases.

Your patches adds this config:

+config FUSE_BPF

+       bool "Adds BPF to fuse"
+       depends on FUSE_FS
+       depends on BPF
+       help

+ Extends FUSE by adding BPF to prefilter calls and
potentially pass to a
+ backing file system

Since your patches add the PASSTHROUGH functionality before adding BPF functionality, would it make sense to review and merge the PASSTHROUGH functionality strictly before the BPF functionality?

Alternatively, you could aim to merge support for some PASSTHROUGH ops then support for some BPF functionality and then slowly add ops to both.

Which brings me to my biggest concern.
I still do not see how these patches replace Allesio's FUSE_DEV_IOC_PASSTHROUGH_OPEN patches.

Is the idea here that ioctl needs to be done at FUSE_LOOKUP instead or in addition to the ioctl on FUSE_OPEN to setup the read/write passthrough on the backing file?

I am missing things like the FILESYSTEM_MAX_STACK_DEPTH check that was added as a result of review on Allesio's patches.

The reason I am concerned about this is that we are using the FUSE_DEV_IOC_PASSTHROUGH_OPEN patches and I would like
to upstream their functionality sooner rather than later. These patches have already been running in production for a while I believe that they are running in Android as well and there is value in upsteaming well tested patches.

The API does not need to stay FUSE_DEV_IOC_PASSTHROUGH_OPEN it should be an API that is extendable to FUSE-BPF, but it would be useful if the read/write passthrough could be the goal for first merge.

Does any of this make sense to you?
Can you draw a roadmap for merging FUSE-BPF that starts with a first (hopefully short term) phase that adds the read/write passthrough functionality?

I can help with review and testing of that part if needed. I was planning to discuss this with you on LSFMM anyway, but better start the discussion beforehand.

Thanks,
Amir.

#51 lore raw
Quoted 10 lines · Amir Goldstein

Which brings me to my biggest concern.
I still do not see how these patches replace Allesio's FUSE_DEV_IOC_PASSTHROUGH_OPEN patches.

Is the idea here that ioctl needs to be done at FUSE_LOOKUP instead or in addition to the ioctl on FUSE_OPEN to setup the read/write passthrough on the backing file?

In these patches, the fuse daemon responds to the lookup request via an ioctl, essentially in the same way it would have to the /dev/fuse node. It just flags the write as coming from an ioctl and calls fuse_dev_do_write. An additional block in the lookup response gives the backing file and what bpf_ops to use. The main difference is that fuse-bpf uses backing inodes, while passthrough uses a file. Fuse-bpf's read/write support currently isn't complete, but it does allow for direct passthrough. You could set ops to default to userspace in every case that Allesio's passthrough code does and it should have about the same effect. With the struct_op change, I did notice that doing something like that is more annoying, and am planning to add a default op which only takes the meta info and runs if the opcode specific op is not present.

Quoted 3 lines

I am missing things like the FILESYSTEM_MAX_STACK_DEPTH check that was added as a result of review on Allesio's patches.

I'd definitely want to fix any issues that were fixed there. There's a lot of common code between fuse-bpf and fuse passthrough, so many of the suggestions there will apply here.

Quoted 16 lines

The reason I am concerned about this is that we are using the FUSE_DEV_IOC_PASSTHROUGH_OPEN patches and I would like
to upstream their functionality sooner rather than later. These patches have already been running in production for a while I believe that they are running in Android as well and there is value in upsteaming well tested patches.

The API does not need to stay FUSE_DEV_IOC_PASSTHROUGH_OPEN it should be an API that is extendable to FUSE-BPF, but it would be useful if the read/write passthrough could be the goal for first merge.

Does any of this make sense to you?
Can you draw a roadmap for merging FUSE-BPF that starts with a first (hopefully short term) phase that adds the read/write passthrough functionality?

Quoted 6 lines

I can help with review and testing of that part if needed. I was planning to discuss this with you on LSFMM anyway, but better start the discussion beforehand.

Thanks,
Amir.

We've been using an earlier version of fuse-bpf on Android, closer to the V1 patches. They fit our current needs but don't cover everything we intend to. The V3 patches switch to a new style of bpf program, which I'm hoping to get some feedback on before I spend too much time fixing up the details. The backing calls themselves can be reviewed separately from that though.

Without bpf, we're essentially enabling complete passthrough at a directory or file. By default, once you set a backing file fuse-bpf calls by the backing filesystem by default, with no additional userspace interaction apart from if an installed bpf program says otherwise. If we had some commands without others, we'd have behavior changes as we introduce support for additional calls. We'd need a way to set default behavior. Perhaps something like a u64 flag field extension in FUSE_INIT for indicating which opcodes support backing, and a response for what those should default to doing. If there's a bpf_op present for a given opcode, it would be able to override that default. If we had something like that, we'd be able to add support for a subset of opcodes in a sensible way.

maintainer
#52 lore raw
Quoted 13 lines · Daniel Rosenberg
Quoted 10 lines · Amir Goldstein

Which brings me to my biggest concern.
I still do not see how these patches replace Allesio's FUSE_DEV_IOC_PASSTHROUGH_OPEN patches.

Is the idea here that ioctl needs to be done at FUSE_LOOKUP instead or in addition to the ioctl on FUSE_OPEN to setup the read/write passthrough on the backing file?

Quoted 6 lines

In these patches, the fuse daemon responds to the lookup request via an ioctl, essentially in the same way it would have to the /dev/fuse node. It just flags the write as coming from an ioctl and calls fuse_dev_do_write. An additional block in the lookup response gives the backing file and what bpf_ops to use. The main difference is that fuse-bpf uses backing inodes, while passthrough uses a file.

Ah right. I wonder if there is benefit in both APIs or if backing inode is sufficient to impelelent everything the could be interesting to implement with a backing file.

Quoted 4 lines

Fuse-bpf's read/write support currently isn't complete, but it does allow for direct passthrough. You could set ops to default to userspace in every case that Allesio's passthrough code does and it should have about the same effect.

What are the subtle differences then?

Quoted 5 lines

With the struct_op change, I did
notice that doing something like that is more annoying, and am planning to add a default op which only takes the meta info and runs if the opcode specific op is not present.

Sounds interesting. I'll wait to see what you propose.

Quoted 9 lines
Quoted 4 lines

I am missing things like the FILESYSTEM_MAX_STACK_DEPTH check that was added as a result of review on Allesio's patches.

I'd definitely want to fix any issues that were fixed there. There's a lot of common code between fuse-bpf and fuse passthrough, so many of the suggestions there will apply here.

That's why I suggested trying to implement the passthough file ioctl functionality first to make sure that none of the review comments in the first round were missed.

But if we need functionality of both ioctls, we can collaborate the work on merging them separately.

Quoted 30 lines
Quoted 16 lines

The reason I am concerned about this is that we are using the FUSE_DEV_IOC_PASSTHROUGH_OPEN patches and I would like
to upstream their functionality sooner rather than later. These patches have already been running in production for a while I believe that they are running in Android as well and there is value in upsteaming well tested patches.

The API does not need to stay FUSE_DEV_IOC_PASSTHROUGH_OPEN it should be an API that is extendable to FUSE-BPF, but it would be useful if the read/write passthrough could be the goal for first merge.

Does any of this make sense to you?
Can you draw a roadmap for merging FUSE-BPF that starts with a first (hopefully short term) phase that adds the read/write passthrough functionality?

Quoted 6 lines

I can help with review and testing of that part if needed. I was planning to discuss this with you on LSFMM anyway, but better start the discussion beforehand.

Thanks,
Amir.

We've been using an earlier version of fuse-bpf on Android, closer to the V1 patches. They fit our current needs but don't cover everything we intend to. The V3 patches switch to a new style of bpf program, which I'm hoping to get some feedback on before I spend too much time fixing up the details. The backing calls themselves can be reviewed separately from that though.

Quoted 12 lines

Without bpf, we're essentially enabling complete passthrough at a directory or file. By default, once you set a backing file fuse-bpf calls by the backing filesystem by default, with no additional userspace interaction apart from if an installed bpf program says otherwise. If we had some commands without others, we'd have behavior changes as we introduce support for additional calls. We'd need a way to set default behavior. Perhaps something like a u64 flag field extension in FUSE_INIT for indicating which opcodes support backing, and a response for what those should default to doing. If there's a bpf_op present for a given opcode, it would be able to override that default. If we had something like that, we'd be able to add support for a subset of opcodes in a sensible way.

So maybe this is something to consider.

Thanks,
Amir.

maintainer
#53 lore raw
In reply to Daniel Rosenberg
Quoted 6 lines · Daniel Rosenberg

These patches extend FUSE to be able to act as a stacked filesystem. This allows pure passthrough, where the fuse file system simply reflects the lower filesystem, and also allows optional pre and post filtering in BPF and/or the userspace daemon as needed. This can dramatically reduce or even eliminate transitions to and from userspace.

I'll ignore BPF for now and concentrate on the passthrough aspect, which I understand better.

The security model needs to be thought about and documented. Think about this: the fuse server now delegates operations it would itself perform to the passthrough code in fuse. The permissions that would have been checked in the context of the fuse server are now checked in the context of the task performing the operation. The server may be able to bypass seccomp restrictions. Files that are open on the backing filesystem are now hidden (e.g. lsof won't find these), which allows the server to obfuscate accesses to backing files. Etc.

These are not particularly worrying if the server is privileged, but fuse comes with the history of supporting unprivileged servers, so we should look at supporting passthrough with unprivileged servers as well.

My other generic comment is that you should add justification for doing this in the first place. I guess it's mainly performance. So
how performance can be won in real life cases? It would also be good
to measure the contribution of individual ops to that win. Is there
another reason for this besides performance?

Thanks,
Miklos

#54 lore raw
Quoted 11 lines · Miklos Szeredi

The security model needs to be thought about and documented. Think about this: the fuse server now delegates operations it would itself perform to the passthrough code in fuse. The permissions that would have been checked in the context of the fuse server are now checked in the context of the task performing the operation. The server may be able to bypass seccomp restrictions. Files that are open on the backing filesystem are now hidden (e.g. lsof won't find these), which allows the server to obfuscate accesses to backing files. Etc.

Quoted 5 lines

These are not particularly worrying if the server is privileged, but fuse comes with the history of supporting unprivileged servers, so we should look at supporting passthrough with unprivileged servers as well.

This is on my todo list. My current plan is to grab the creds that the daemon uses to respond to FUSE_INIT. That should keep behavior fairly similar. I'm not sure if there are cases where the fuse server is operating under multiple contexts.
I don't currently have a plan for exposing open files via lsof. Every such file should relate to one that will show up though. I haven't dug into how that's set up, but I'm open to suggestions.

Quoted 8 lines

My other generic comment is that you should add justification for doing this in the first place. I guess it's mainly performance. So
how performance can be won in real life cases? It would also be good
to measure the contribution of individual ops to that win. Is there
another reason for this besides performance?

Thanks,
Miklos

Our main concern with it is performance. We have some preliminary numbers looking at the pure passthrough case. We've been testing using a ramdrive on a somewhat slow machine, as that should highlight differences more. We ran fio for sequential reads, and random read/write. For sequential reads, we were seeing libfuse's passthrough_hp take about a 50% hit, with fuse-bpf not being detectably slower. For random read/write, we were seeing a roughly 90% drop in performance from passthrough_hp, while fuse-bpf has about a 7% drop in read and write speed. When we use a bpf that traces every opcode, that performance hit increases to a roughly 1% drop in sequential read performance, and a 20% drop in both read and write performance for random read/write. We plan to make more complex bpf examples, with fuse daemon equivalents to compare against.

We have not looked closely at the impact of individual opcodes yet.

There's also a potential ease of use for fuse-bpf. If you're implementing a fuse daemon that is largely mirroring a backing filesystem, you only need to write code for the differences in behavior. For instance, say you want to remove image metadata like location. You could give bpf information on what range of data is metadata, and zero out that section without having to handle any other operations.

-Daniel

#55 lore raw
Quoted 51 lines · Daniel Rosenberg
Quoted 11 lines · Miklos Szeredi

The security model needs to be thought about and documented. Think about this: the fuse server now delegates operations it would itself perform to the passthrough code in fuse. The permissions that would have been checked in the context of the fuse server are now checked in the context of the task performing the operation. The server may be able to bypass seccomp restrictions. Files that are open on the backing filesystem are now hidden (e.g. lsof won't find these), which allows the server to obfuscate accesses to backing files. Etc.

Quoted 5 lines

These are not particularly worrying if the server is privileged, but fuse comes with the history of supporting unprivileged servers, so we should look at supporting passthrough with unprivileged servers as well.

This is on my todo list. My current plan is to grab the creds that the daemon uses to respond to FUSE_INIT. That should keep behavior fairly similar. I'm not sure if there are cases where the fuse server is operating under multiple contexts.
I don't currently have a plan for exposing open files via lsof. Every such file should relate to one that will show up though. I haven't dug into how that's set up, but I'm open to suggestions.

Quoted 8 lines

My other generic comment is that you should add justification for doing this in the first place. I guess it's mainly performance. So
how performance can be won in real life cases? It would also be good
to measure the contribution of individual ops to that win. Is there
another reason for this besides performance?

Thanks,
Miklos

Our main concern with it is performance. We have some preliminary numbers looking at the pure passthrough case. We've been testing using a ramdrive on a somewhat slow machine, as that should highlight differences more. We ran fio for sequential reads, and random read/write. For sequential reads, we were seeing libfuse's passthrough_hp take about a 50% hit, with fuse-bpf not being detectably slower. For random read/write, we were seeing a roughly 90% drop in performance from passthrough_hp, while fuse-bpf has about a 7% drop in read and write speed. When we use a bpf that traces every opcode, that performance hit increases to a roughly 1% drop in sequential read performance, and a 20% drop in both read and write performance for random read/write. We plan to make more complex bpf examples, with fuse daemon equivalents to compare against.

We have not looked closely at the impact of individual opcodes yet.

Quoted 7 lines

There's also a potential ease of use for fuse-bpf. If you're implementing a fuse daemon that is largely mirroring a backing filesystem, you only need to write code for the differences in behavior. For instance, say you want to remove image metadata like location. You could give bpf information on what range of data is metadata, and zero out that section without having to handle any other operations.

A bit out of topic (although I'm not quite look into FUSE BPF internals) After roughly listening to this topic in FS track last week, I'm not quite sure (at least in the long term) if it might be better if ebpf-related filter/redirect stuffs could be landed in vfs or in a somewhat stackable fs so that we could redirect/filter any sub-fstree
in principle? It's just an open question and I have no real tendency
of this but do we really need a BPF-filter functionality for each individual fs?

It sounds much like
https://learn.microsoft.com/en-us/windows-hardware/drivers/ifs/about-file-system-filter-drivers

Thanks,
Gao Xiang

Quoted 2 lines

-Daniel

maintainer
#56 lore raw
Quoted 63 lines · Gao Xiang
Quoted 51 lines · Daniel Rosenberg
Quoted 11 lines · Miklos Szeredi

The security model needs to be thought about and documented. Think about this: the fuse server now delegates operations it would itself perform to the passthrough code in fuse. The permissions that would have been checked in the context of the fuse server are now checked in the context of the task performing the operation. The server may be able to bypass seccomp restrictions. Files that are open on the backing filesystem are now hidden (e.g. lsof won't find these), which allows the server to obfuscate accesses to backing files. Etc.

Quoted 5 lines

These are not particularly worrying if the server is privileged, but fuse comes with the history of supporting unprivileged servers, so we should look at supporting passthrough with unprivileged servers as well.

This is on my todo list. My current plan is to grab the creds that the daemon uses to respond to FUSE_INIT. That should keep behavior fairly similar. I'm not sure if there are cases where the fuse server is operating under multiple contexts.
I don't currently have a plan for exposing open files via lsof. Every such file should relate to one that will show up though. I haven't dug into how that's set up, but I'm open to suggestions.

Quoted 8 lines

My other generic comment is that you should add justification for doing this in the first place. I guess it's mainly performance. So
how performance can be won in real life cases? It would also be good
to measure the contribution of individual ops to that win. Is there
another reason for this besides performance?

Thanks,
Miklos

Our main concern with it is performance. We have some preliminary numbers looking at the pure passthrough case. We've been testing using a ramdrive on a somewhat slow machine, as that should highlight differences more. We ran fio for sequential reads, and random read/write. For sequential reads, we were seeing libfuse's passthrough_hp take about a 50% hit, with fuse-bpf not being detectably slower. For random read/write, we were seeing a roughly 90% drop in performance from passthrough_hp, while fuse-bpf has about a 7% drop in read and write speed. When we use a bpf that traces every opcode, that performance hit increases to a roughly 1% drop in sequential read performance, and a 20% drop in both read and write performance for random read/write. We plan to make more complex bpf examples, with fuse daemon equivalents to compare against.

We have not looked closely at the impact of individual opcodes yet.

Quoted 7 lines

There's also a potential ease of use for fuse-bpf. If you're implementing a fuse daemon that is largely mirroring a backing filesystem, you only need to write code for the differences in behavior. For instance, say you want to remove image metadata like location. You could give bpf information on what range of data is metadata, and zero out that section without having to handle any other operations.

Quoted 8 lines

A bit out of topic (although I'm not quite look into FUSE BPF internals) After roughly listening to this topic in FS track last week, I'm not quite sure (at least in the long term) if it might be better if ebpf-related filter/redirect stuffs could be landed in vfs or in a somewhat stackable fs so that we could redirect/filter any sub-fstree
in principle? It's just an open question and I have no real tendency
of this but do we really need a BPF-filter functionality for each individual fs?

I think that is a valid question, but the answer is that even if it makes sense, doing something like this in vfs would be a much bigger project with larger consequences on performance and security and whatnot, so even if (and a very big if) this ever happens, using FUSE-BPF as a playground for this sort of stuff would be a good idea.

This reminds me of union mounts - it made sense to have union mount functionality in vfs, but after a long winding road, a stacked fs (overlayfs) turned out to be a much more practical solution.

Quoted 4 lines

It sounds much like
https://learn.microsoft.com/en-us/windows-hardware/drivers/ifs/about-file-system-filter-drivers

Nice reference.
I must admit that I found it hard to understand what Windows filter drivers can do compared to FUSE-BPF design.
It'd be nice to get some comparison from what is planned for FUSE-BPF.

Interesting to note that there is a "legacy" Windows filter driver API, so Windows didn't get everything right for the first API - that is especially interesting to look at as repeating other people's mistakes would be a shame.

Thanks,
Amir.

#57 lore raw

Hi Amir,

Quoted 73 lines · Amir Goldstein
Quoted 63 lines · Gao Xiang
Quoted 51 lines · Daniel Rosenberg
Quoted 11 lines · Miklos Szeredi

The security model needs to be thought about and documented. Think about this: the fuse server now delegates operations it would itself perform to the passthrough code in fuse. The permissions that would have been checked in the context of the fuse server are now checked in the context of the task performing the operation. The server may be able to bypass seccomp restrictions. Files that are open on the backing filesystem are now hidden (e.g. lsof won't find these), which allows the server to obfuscate accesses to backing files. Etc.

Quoted 5 lines

These are not particularly worrying if the server is privileged, but fuse comes with the history of supporting unprivileged servers, so we should look at supporting passthrough with unprivileged servers as well.

This is on my todo list. My current plan is to grab the creds that the daemon uses to respond to FUSE_INIT. That should keep behavior fairly similar. I'm not sure if there are cases where the fuse server is operating under multiple contexts.
I don't currently have a plan for exposing open files via lsof. Every such file should relate to one that will show up though. I haven't dug into how that's set up, but I'm open to suggestions.

Quoted 8 lines

My other generic comment is that you should add justification for doing this in the first place. I guess it's mainly performance. So
how performance can be won in real life cases? It would also be good
to measure the contribution of individual ops to that win. Is there
another reason for this besides performance?

Thanks,
Miklos

Our main concern with it is performance. We have some preliminary numbers looking at the pure passthrough case. We've been testing using a ramdrive on a somewhat slow machine, as that should highlight differences more. We ran fio for sequential reads, and random read/write. For sequential reads, we were seeing libfuse's passthrough_hp take about a 50% hit, with fuse-bpf not being detectably slower. For random read/write, we were seeing a roughly 90% drop in performance from passthrough_hp, while fuse-bpf has about a 7% drop in read and write speed. When we use a bpf that traces every opcode, that performance hit increases to a roughly 1% drop in sequential read performance, and a 20% drop in both read and write performance for random read/write. We plan to make more complex bpf examples, with fuse daemon equivalents to compare against.

We have not looked closely at the impact of individual opcodes yet.

Quoted 7 lines

There's also a potential ease of use for fuse-bpf. If you're implementing a fuse daemon that is largely mirroring a backing filesystem, you only need to write code for the differences in behavior. For instance, say you want to remove image metadata like location. You could give bpf information on what range of data is metadata, and zero out that section without having to handle any other operations.

Quoted 8 lines

A bit out of topic (although I'm not quite look into FUSE BPF internals) After roughly listening to this topic in FS track last week, I'm not quite sure (at least in the long term) if it might be better if ebpf-related filter/redirect stuffs could be landed in vfs or in a somewhat stackable fs so that we could redirect/filter any sub-fstree
in principle? It's just an open question and I have no real tendency
of this but do we really need a BPF-filter functionality for each individual fs?

Quoted 5 lines

I think that is a valid question, but the answer is that even if it makes sense, doing something like this in vfs would be a much bigger project with larger consequences on performance and security and whatnot, so even if (and a very big if) this ever happens, using FUSE-BPF as a playground for this sort of stuff would be a good idea.

My current observation is that the total Fuse-BPF LoC is already beyond the whole FUSE itself. In addition, it almost hooks all fs operations which impacts something to me.

Quoted 4 lines

This reminds me of union mounts - it made sense to have union mount functionality in vfs, but after a long winding road, a stacked fs (overlayfs) turned out to be a much more practical solution.

Yeah, I agree. So it was just a pure hint on my side.

Quoted 10 lines
Quoted 5 lines

It sounds much like
https://learn.microsoft.com/en-us/windows-hardware/drivers/ifs/about-file-system-filter-drivers

Nice reference.
I must admit that I found it hard to understand what Windows filter drivers can do compared to FUSE-BPF design.
It'd be nice to get some comparison from what is planned for FUSE-BPF.

At least some investigation/analysis first might be better in the long term development.

Quoted 4 lines

Interesting to note that there is a "legacy" Windows filter driver API, so Windows didn't get everything right for the first API - that is especially interesting to look at as repeating other people's mistakes would be a shame.

I'm not familiar with that details as well, yet I saw that they have a filesystem filter subsystem, so I mentioned it here.

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Amir.

#58 lore raw
Quoted 76 lines · Gao Xiang

Hi Amir,

Quoted 73 lines · Amir Goldstein
Quoted 71 lines · Gao Xiang
Quoted 51 lines · Daniel Rosenberg
Quoted 11 lines · Miklos Szeredi

The security model needs to be thought about and documented.  Think about this: the fuse server now delegates operations it would itself perform to the passthrough code in fuse.  The permissions that would have been checked in the context of the fuse server are now checked in the context of the task performing the operation.  The server may be able to bypass seccomp restrictions.  Files that are open on the backing filesystem are now hidden (e.g. lsof won't find these), which allows the server to obfuscate accesses to backing files.  Etc.

Quoted 5 lines

These are not particularly worrying if the server is privileged, but fuse comes with the history of supporting unprivileged servers, so we should look at supporting passthrough with unprivileged servers as well.

This is on my todo list. My current plan is to grab the creds that the daemon uses to respond to FUSE_INIT. That should keep behavior fairly similar. I'm not sure if there are cases where the fuse server is operating under multiple contexts.
I don't currently have a plan for exposing open files via lsof. Every such file should relate to one that will show up though. I haven't dug into how that's set up, but I'm open to suggestions.

Quoted 8 lines

My other generic comment is that you should add justification for doing this in the first place.  I guess it's mainly performance.  So how performance can be won in real life cases?   It would also be good to measure the contribution of individual ops to that win.   Is there another reason for this besides performance?

Thanks,
Miklos

Our main concern with it is performance. We have some preliminary numbers looking at the pure passthrough case. We've been testing using a ramdrive on a somewhat slow machine, as that should highlight differences more. We ran fio for sequential reads, and random read/write. For sequential reads, we were seeing libfuse's passthrough_hp take about a 50% hit, with fuse-bpf not being detectably slower. For random read/write, we were seeing a roughly 90% drop in performance from passthrough_hp, while fuse-bpf has about a 7% drop in read and write speed. When we use a bpf that traces every opcode, that performance hit increases to a roughly 1% drop in sequential read performance, and a 20% drop in both read and write performance for random read/write. We plan to make more complex bpf examples, with fuse daemon equivalents to compare against.

We have not looked closely at the impact of individual opcodes yet.

Quoted 7 lines

There's also a potential ease of use for fuse-bpf. If you're implementing a fuse daemon that is largely mirroring a backing filesystem, you only need to write code for the differences in behavior. For instance, say you want to remove image metadata like location. You could give bpf information on what range of data is metadata, and zero out that section without having to handle any other operations.

A bit out of topic (although I'm not quite look into FUSE BPF internals) After roughly listening to this topic in FS track last week, I'm not quite sure (at least in the long term) if it might be better if ebpf-related filter/redirect stuffs could be landed in vfs or in a somewhat stackable fs so that we could redirect/filter any sub-fstree in principle?    It's just an open question and I have no real tendency of this but do we really need a BPF-filter functionality for each individual fs?

Quoted 7 lines
Quoted 5 lines

I think that is a valid question, but the answer is that even if it makes sense, doing something like this in vfs would be a much bigger project with larger consequences on performance and security and whatnot, so even if (and a very big if) this ever happens, using FUSE-BPF as a playground for this sort of stuff would be a good idea.

My current observation is that the total Fuse-BPF LoC is already beyond the

^ sorry I double-checked now I was wrong, forget about it.

Quoted 38 lines

whole FUSE itself.  In addition, it almost hooks all fs operations which impacts something to me.

Quoted 4 lines

This reminds me of union mounts - it made sense to have union mount functionality in vfs, but after a long winding road, a stacked fs (overlayfs) turned out to be a much more practical solution.

Yeah, I agree.  So it was just a pure hint on my side.

Quoted 10 lines
Quoted 5 lines

It sounds much like
https://learn.microsoft.com/en-us/windows-hardware/drivers/ifs/about-file-system-filter-drivers

Nice reference.
I must admit that I found it hard to understand what Windows filter drivers can do compared to FUSE-BPF design.
It'd be nice to get some comparison from what is planned for FUSE-BPF.

At least some investigation/analysis first might be better in the long term development.

Quoted 4 lines

Interesting to note that there is a "legacy" Windows filter driver API, so Windows didn't get everything right for the first API - that is especially interesting to look at as repeating other people's mistakes would be a shame.

I'm not familiar with that details as well, yet I saw that they have a filesystem filter subsystem, so I mentioned it here.

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Amir.