BPF List
 help / color / mirror / Atom feed
* [PATCH 0/6] xfs: BPF data placement policy for zoned file systems
@ 2026-12-01  4:34 Naohiro Aota
  2026-12-01  4:34 ` [PATCH 1/6] fs: add fs_dp BPF struct_ops for file data placement Naohiro Aota
                   ` (7 more replies)
  0 siblings, 8 replies; 12+ messages in thread
From: Naohiro Aota @ 2026-12-01  4:34 UTC (permalink / raw)
  To: viro, brauner, cem, hch
  Cc: ast, djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf,
	linux-kernel, dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi,
	Naohiro Aota

When files with different lifetimes are colocated in the same zone,
zoned XFS suffers from GC write amplification: once the short-lived
files are deleted or overwritten, the zone still holds live data of the
long-lived files, which GC has to move out before the zone can be
reset. This costs throughput and latency.

We can help GC by setting the file's lifetime hint, as zoned XFS
separates file data into zones by the hint. But most applications do
not set the hint, and changing the application code itself is often not
realistic.

This series adds a struct_ops, fs_dp_ops, whose file_open callback picks
the hint when a file is opened, e.g. based on its name, path, or project
ID.

Results of a micro-benchmark on a 32TB SMR HDD (fill to 80% with 256MiB and
1MiB files, then overwrite) where large and small files are separated
by setting the WRITE_LIFE_SHORT hint on the small ones, either by a BPF
policy or by fcntl in the application:

                     baseline     fcntl hint   BPF policy
  overwrite tput     157 MiB/s    179 MiB/s    179 MiB/s
  large write p99    17.3 s       13.3 s       13.2 s
  GC moved           14.55 GB     0            0

The BPF policy gives the same result as setting the hint with fcntl,
without changing the application.

* Interface

The struct_ops and its registry are in VFS core (fs/fs_dp.c,
CONFIG_FS_DP_BPF), so that other filesystems can use them too. A
filesystem looks up a policy by name with fs_dp_ops_get() and drops it
with fs_dp_ops_put().

** struct_ops

An eBPF program for the policy implements fs_dp_ops. file_open is called
from xfs_file_open() when a file is opened for writing, with the file
being opened, the inode number of its parent directory, and the file's
attributes as a struct file_kattr (project ID, xflags, ...; may be NULL).
It returns the write hint to set on the inode in struct
fs_dp_file_open_res; an invalid hint is ignored.

	struct fs_dp_ops {
		char name[FS_DP_OPS_NAME_MAX];
		int (*file_open)(const struct file *file,
				 unsigned long parent_ino,
				 const struct file_kattr *fa,
				 struct fs_dp_file_open_res *res);
	};

	struct fs_dp_file_open_res {
		enum rw_hint i_write_hint;
	};

fs_dp_file_open_res has only i_write_hint for now. It is a struct so
that it can carry more placement results, e.g. a write stream as in the
reference patches described below.

** Binding to a filesystem

A registered policy is bound with XFS-specific ioctls for now.
XFS_IOC_SET_DP_OPS binds the policy of the given name, or unbinds the
current one if the name is empty; XFS_IOC_GET_DP_OPS returns the name of
the bound policy. Without a flag they act mount-wide and must be issued
on the root directory. With XFS_DP_OPS_F_PROJID they act on the project
ID of the ioctl'd file instead; per-project-ID bindings override the
mount-wide one. (GET is _IOWR so it can read the flag in.) The filesystem
holds a reference on each bound policy until it is replaced or the
filesystem is unmounted.

	struct xfs_dp_ops_name {
		char	name[XFS_DP_OPS_NAME_MAX];	/* NUL-terminated */
		__u32	flags;				/* XFS_DP_OPS_F_* */
		__u32	pad[3];
	};

	#define XFS_DP_OPS_F_PROJID	(1u << 0)
	#define XFS_IOC_SET_DP_OPS	_IOW ('X', 71, struct xfs_dp_ops_name)
	#define XFS_IOC_GET_DP_OPS	_IOWR('X', 72, struct xfs_dp_ops_name)

** Alternative binding interface

A VFS-level hook attached by a BPF link with a target fd is likely the
final form, but needs more VFS and BPF core changes. We keep the binding
XFS-specific for now to get feedback on the idea first.

** Known limitation: FS kfuncs need CONFIG_BPF_LSM

A policy can call FS kfuncs such as bpf_path_d_path() (patch 2), but
only when CONFIG_BPF_LSM is enabled, because fs/bpf_fs_kfuncs.c is built
only with it. binfmt_misc struct_ops programs have the same limitation.
Most of these kfuncs are not tied to BPF LSM, so this should eventually
be fixed by building the file without CONFIG_BPF_LSM (e.g. under a
hidden CONFIG_BPF_FS_KFUNCS selected by BPF_LSM, BINFMT_MISC_BPF and
FS_DP_BPF), keeping only the LSM-specific parts under it. We left that
out of this series to keep it focused, and can send it separately.

* Write streams

The XFS write streams series [1] and its zoned XFS support [2] let user
space reserve streams and set them on files. As a reference, two more
patches on top of them, sent as replies to this mail, let the policy
pick a stream as well: the stream files are bound along with the policy
by XFS_IOC_SET_DP_OPS, and file_open() returns an index into them in
fs_dp_file_open_res.write_stream.

[1] https://lore.kernel.org/linux-xfs/20261103110640.1000095-1-anuj20.g@samsung.com/
[2] https://lore.kernel.org/linux-xfs/20261110103540.46876-1-hans.holmberg@wdc.com/

* Open question

Unregistering a policy only removes its name from the registry; file
systems that use it keep running it until they unbind it, as with
binfmt_misc. Should unregistering stop them instead?

This series is based on v7.4-rc5.

Naohiro Aota (6):
  fs: add fs_dp BPF struct_ops for file data placement
  bpf: allow fs_dp struct_ops programs to call FS kfuncs
  xfs: add BPF data placement hook at file open
  xfs: add ioctls to select a BPF data placement policy
  xfs: bind a BPF data placement policy to a project ID
  xfs: let XFS_IOC_{SET,GET}_DP_OPS act on a project ID

 fs/Kconfig             |  10 ++
 fs/Makefile            |   1 +
 fs/bpf_fs_kfuncs.c     |   6 +-
 fs/fs_dp.c             | 209 +++++++++++++++++++++++++++++++++++++++++
 fs/xfs/Makefile        |   2 +
 fs/xfs/libxfs/xfs_fs.h |  17 ++++
 fs/xfs/xfs_bpf.c       | 197 ++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_bpf.h       |  32 +++++++
 fs/xfs/xfs_file.c      |   2 +
 fs/xfs/xfs_ioctl.c     |   6 ++
 fs/xfs/xfs_mount.h     |   5 +
 fs/xfs/xfs_super.c     |   3 +
 include/linux/fs_dp.h  |  58 ++++++++++++
 13 files changed, 546 insertions(+), 2 deletions(-)
 create mode 100644 fs/fs_dp.c
 create mode 100644 fs/xfs/xfs_bpf.c
 create mode 100644 fs/xfs/xfs_bpf.h
 create mode 100644 include/linux/fs_dp.h


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.56.1


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/6] fs: add fs_dp BPF struct_ops for file data placement
  2026-12-01  4:34 [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Naohiro Aota
@ 2026-12-01  4:34 ` Naohiro Aota
  2026-12-01  4:45   ` sashiko-bot
  2026-12-01  4:34 ` [PATCH 2/6] bpf: allow fs_dp struct_ops programs to call FS kfuncs Naohiro Aota
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 12+ messages in thread
From: Naohiro Aota @ 2026-12-01  4:34 UTC (permalink / raw)
  To: viro, brauner, cem, hch
  Cc: ast, djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf,
	linux-kernel, dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi,
	Naohiro Aota

Add a BPF struct_ops, fs_dp_ops, that lets a BPF program pick the write
lifetime hint of a file when it is opened. The struct_ops has a single
file_open() callback that is called when a file is opened for writing. It
receives the file being opened, the inode number of its parent directory
and the file's attributes (struct file_kattr), and returns the hint in
struct fs_dp_file_open_res. The caller side is responsible for reflecting
the hint to proper in-kernel structures. Filesystems call file_open() under
rcu_read_lock(), so reject sleepable programs.

Registered policies are kept in a named registry. fs_dp_ops_get() looks one
up by name and takes a reference on it; fs_dp_ops_put() drops it. A
filesystem uses these to attach a policy and keep it alive while in use.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/Kconfig            |  10 +++
 fs/Makefile           |   1 +
 fs/fs_dp.c            | 203 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs_dp.h |  52 +++++++++++
 4 files changed, 266 insertions(+)
 create mode 100644 fs/fs_dp.c
 create mode 100644 include/linux/fs_dp.h

diff --git a/fs/Kconfig b/fs/Kconfig
index 70a1d0760d28..99a9624698cf 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -30,6 +30,16 @@ config LEGACY_DIRECT_IO
 	depends on BUFFER_HEAD
 	bool
 
+config FS_DP_BPF
+	bool "BPF data placement policies"
+	depends on BPF_SYSCALL && BPF_JIT && DEBUG_INFO_BTF
+	help
+	  Allow a BPF struct_ops program to pick the data placement hint of a
+	  file, so that a filesystem can place file data according to a policy.
+	  Filesystems that support this attach a registered policy.
+
+	  If you don't know what to answer at this point, say N.
+
 if BLOCK
 
 source "fs/ext2/Kconfig"
diff --git a/fs/Makefile b/fs/Makefile
index 16f1108b64e1..1bb8ca967ca8 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -18,6 +18,7 @@ obj-y :=	open.o read_write.o file_table.o super.o \
 		kernel_read_file.o mnt_idmapping.o remap_range.o pidfs.o \
 		file_attr.o fserror.o nullfs.o failfs.o
 
+obj-$(CONFIG_FS_DP_BPF)		+= fs_dp.o
 obj-$(CONFIG_BUFFER_HEAD)	+= buffer.o mpage.o
 obj-$(CONFIG_PROC_FS)		+= proc_namespace.o
 obj-$(CONFIG_LEGACY_DIRECT_IO)	+= direct-io.o
diff --git a/fs/fs_dp.c b/fs/fs_dp.c
new file mode 100644
index 000000000000..1c370f6ffa42
--- /dev/null
+++ b/fs/fs_dp.c
@@ -0,0 +1,203 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * BPF struct_ops for file data placement policies.
+ *
+ * A fs_dp_ops struct_ops program picks the data placement hint of a file.
+ * A filesystem looks up a registered policy by name with
+ * fs_dp_ops_get() and attaches it to a mount/directory/file.
+ *
+ * Copyright (C) 2025 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/bpf_verifier.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/cleanup.h>
+#include <linux/fs.h>
+#include <linux/fs_dp.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+
+/* A registered fs_dp_ops. */
+struct fs_dp_ops_reg {
+	struct list_head	list;
+	struct fs_dp_ops	*ops;
+	struct bpf_link		*link;
+};
+
+static DEFINE_SPINLOCK(fs_dp_ops_lock);
+static LIST_HEAD(fs_dp_ops_list);
+
+static int bpf_fs_dp_btf_struct_access(struct bpf_verifier_log *log,
+				       const struct bpf_reg_state *reg, int off, int size)
+{
+	const struct btf_type *t;
+	s32 res_type_id;
+	const struct btf_type *res_type;
+
+	res_type_id = btf_find_by_name_kind(reg->btf, "fs_dp_file_open_res",
+					    BTF_KIND_STRUCT);
+	if (res_type_id < 0)
+		return -EINVAL;
+	res_type = btf_type_by_id(reg->btf, res_type_id);
+
+	t = btf_type_by_id(reg->btf, reg->btf_id);
+	if (t != res_type)
+		return -EACCES;
+
+	if (off != offsetof(struct fs_dp_file_open_res, i_write_hint) ||
+	    size != sizeof_field(struct fs_dp_file_open_res, i_write_hint))
+		return -EACCES;
+
+	return SCALAR_VALUE;
+}
+
+static const struct bpf_verifier_ops bpf_fs_dp_verifier_ops = {
+	.get_func_proto = bpf_base_func_proto,
+	.is_valid_access = bpf_tracing_btf_ctx_access,
+	.btf_struct_access = bpf_fs_dp_btf_struct_access,
+};
+
+static int bpf_fs_dp_init(struct btf *btf)
+{
+	return 0;
+}
+
+static int bpf_fs_dp_check_member(const struct btf_type *t,
+				  const struct btf_member *member,
+				  const struct bpf_prog *prog)
+{
+	if (prog->sleepable)
+		return -EINVAL;
+	return 0;
+}
+
+static int bpf_fs_dp_init_member(const struct btf_type *t,
+				 const struct btf_member *member, void *kdata,
+				 const void *udata)
+{
+	const struct fs_dp_ops *uops = udata;
+	struct fs_dp_ops *ops = kdata;
+	u32 moff = __btf_member_bit_offset(t, member) / 8;
+
+	switch (moff) {
+	case offsetof(struct fs_dp_ops, name):
+		if (bpf_obj_name_cpy(ops->name, uops->name, sizeof(ops->name)) <= 0)
+			return -EINVAL;
+		return 1;
+	}
+	return 0;
+}
+
+static int bpf_fs_dp_validate(void *kdata)
+{
+	struct fs_dp_ops *ops = kdata;
+
+	if (!ops->file_open)
+		return -EINVAL;
+	return 0;
+}
+
+static struct fs_dp_ops_reg *fs_dp_ops_find(const char *name)
+{
+	struct fs_dp_ops_reg *reg;
+
+	lockdep_assert_held(&fs_dp_ops_lock);
+
+	list_for_each_entry(reg, &fs_dp_ops_list, list) {
+		if (!strcmp(reg->ops->name, name))
+			return reg;
+	}
+	return NULL;
+}
+
+static int bpf_fs_dp_reg(void *kdata, struct bpf_link *link)
+{
+	struct fs_dp_ops *ops = kdata;
+	struct fs_dp_ops_reg *reg;
+
+	reg = kzalloc_obj(*reg, GFP_KERNEL_ACCOUNT);
+	if (!reg)
+		return -ENOMEM;
+	reg->ops = ops;
+	reg->link = link;
+
+	guard(spinlock)(&fs_dp_ops_lock);
+	if (fs_dp_ops_find(ops->name)) {
+		kfree(reg);
+		return -EEXIST;
+	}
+	list_add(&reg->list, &fs_dp_ops_list);
+	return 0;
+}
+
+static void bpf_fs_dp_unreg(void *kdata, struct bpf_link *link)
+{
+	struct fs_dp_ops_reg *reg;
+
+	guard(spinlock)(&fs_dp_ops_lock);
+	list_for_each_entry(reg, &fs_dp_ops_list, list) {
+		if (reg->ops == kdata && reg->link == link) {
+			list_del(&reg->list);
+			kfree(reg);
+			return;
+		}
+	}
+}
+
+/* Look up a registered ops by name and take a reference on it. */
+struct fs_dp_ops *fs_dp_ops_get(const char *name)
+{
+	struct fs_dp_ops_reg *reg;
+
+	guard(spinlock)(&fs_dp_ops_lock);
+	reg = fs_dp_ops_find(name);
+	if (!reg)
+		return NULL;
+	if (!bpf_struct_ops_get(reg->ops))
+		return NULL;
+	return reg->ops;
+}
+EXPORT_SYMBOL_GPL(fs_dp_ops_get);
+
+void fs_dp_ops_put(struct fs_dp_ops *ops)
+{
+	bpf_struct_ops_put(ops);
+}
+EXPORT_SYMBOL_GPL(fs_dp_ops_put);
+
+static int __fs_dp_file_open(const struct file *file, unsigned long parent_ino,
+			     const struct file_kattr *fa,
+			     struct fs_dp_file_open_res *res)
+{
+	return 0;
+}
+
+static struct fs_dp_ops __bpf_ops_fs_dp_ops = {
+	.file_open = __fs_dp_file_open,
+};
+
+static struct bpf_struct_ops bpf_fs_dp_ops = {
+	.verifier_ops = &bpf_fs_dp_verifier_ops,
+	.init = bpf_fs_dp_init,
+	.check_member = bpf_fs_dp_check_member,
+	.init_member = bpf_fs_dp_init_member,
+	.validate = bpf_fs_dp_validate,
+	.reg = bpf_fs_dp_reg,
+	.unreg = bpf_fs_dp_unreg,
+	.name = "fs_dp_ops",
+	.cfi_stubs = &__bpf_ops_fs_dp_ops,
+	.owner = THIS_MODULE,
+};
+
+static int __init fs_dp_init(void)
+{
+	int ret;
+
+	ret = register_bpf_struct_ops(&bpf_fs_dp_ops, fs_dp_ops);
+	if (ret)
+		pr_err("fs_dp: failed to register struct_ops (%d)\n", ret);
+	return ret;
+}
+late_initcall(fs_dp_init);
diff --git a/include/linux/fs_dp.h b/include/linux/fs_dp.h
new file mode 100644
index 000000000000..ec918c316751
--- /dev/null
+++ b/include/linux/fs_dp.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * BPF struct_ops for file data placement policies.
+ *
+ * Copyright (C) 2025 Western Digital Corporation or its affiliates.
+ */
+#ifndef _LINUX_FS_DP_H
+#define _LINUX_FS_DP_H
+
+#include <linux/fs.h>
+
+#define FS_DP_OPS_NAME_MAX	16
+
+struct file;
+struct file_kattr;
+
+/* Result a data placement policy returns from its file_open() callback. */
+struct fs_dp_file_open_res {
+	enum rw_hint i_write_hint;
+};
+
+/*
+ * A data placement policy, implemented as a BPF struct_ops. @name identifies
+ * a registered policy for fs_dp_ops_get().
+ *
+ * file_open() is called when a regular file is opened for writing
+ * (FMODE_WRITE), from the filesystem's ->open().
+ *
+ * @file is the file being opened.
+ * @parent_ino is the inode number of the parent directory.
+ * @fa carries the file's attributes (project ID, xflags, ...) and may be NULL
+ * if the filesystem does not provide them.
+ */
+struct fs_dp_ops {
+	char name[FS_DP_OPS_NAME_MAX];
+	int (*file_open)(const struct file *file, unsigned long parent_ino,
+			 const struct file_kattr *fa,
+			 struct fs_dp_file_open_res *res);
+};
+
+#ifdef CONFIG_FS_DP_BPF
+struct fs_dp_ops *fs_dp_ops_get(const char *name);
+void fs_dp_ops_put(struct fs_dp_ops *ops);
+#else
+static inline struct fs_dp_ops *fs_dp_ops_get(const char *name)
+{
+	return NULL;
+}
+static inline void fs_dp_ops_put(struct fs_dp_ops *ops) { }
+#endif
+
+#endif /* _LINUX_FS_DP_H */
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 2/6] bpf: allow fs_dp struct_ops programs to call FS kfuncs
  2026-12-01  4:34 [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Naohiro Aota
  2026-12-01  4:34 ` [PATCH 1/6] fs: add fs_dp BPF struct_ops for file data placement Naohiro Aota
@ 2026-12-01  4:34 ` Naohiro Aota
  2026-12-01  4:34 ` [PATCH 3/6] xfs: add BPF data placement hook at file open Naohiro Aota
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Naohiro Aota @ 2026-12-01  4:34 UTC (permalink / raw)
  To: viro, brauner, cem, hch
  Cc: ast, djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf,
	linux-kernel, dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi,
	Naohiro Aota

A data placement policy BPF program may want more than the name of the file
being opened, e.g. its full path from bpf_path_d_path(). However, the FS
kfuncs are registered for struct_ops programs only with
CONFIG_BINFMT_MISC_BPF, and bpf_fs_kfuncs_filter() only lets binfmt_misc
programs call them.

Register the kfunc set for struct_ops programs with CONFIG_FS_DP_BPF as
well, and allow fs_dp_ops programs in the filter.

As with binfmt_misc, the kfuncs are available only when CONFIG_BPF_LSM is
enabled, since fs/bpf_fs_kfuncs.c is built only with it. Most of them are
not tied to BPF LSM, so this should eventually be fixed by building the
file without CONFIG_BPF_LSM and keeping only the LSM-specific parts under
it. That is left for a separate change.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/bpf_fs_kfuncs.c    | 6 ++++--
 fs/fs_dp.c            | 6 ++++++
 include/linux/fs_dp.h | 6 ++++++
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c
index 35ccd98a4de6..fdb5b5f796fe 100644
--- a/fs/bpf_fs_kfuncs.c
+++ b/fs/bpf_fs_kfuncs.c
@@ -8,6 +8,7 @@
 #include <linux/btf_ids.h>
 #include <linux/dcache.h>
 #include <linux/fs.h>
+#include <linux/fs_dp.h>
 #include <linux/fsnotify.h>
 #include <linux/file.h>
 #include <linux/kernfs.h>
@@ -546,7 +547,7 @@ static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
 	/* ->st_ops is unset during the cfg pass; enforced once it is set. */
 	if (!prog->aux->st_ops)
 		return 0;
-	if (bpf_prog_is_binfmt_misc_ops(prog) &&
+	if ((bpf_prog_is_binfmt_misc_ops(prog) || bpf_prog_is_fs_dp_ops(prog)) &&
 	    !btf_id_set_contains(&bpf_fs_kfunc_lsm_only_ids, kfunc_id))
 		return 0;
 	return -EACCES;
@@ -589,7 +590,8 @@ static int __init bpf_fs_kfuncs_init(void)
 	int ret;
 
 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_fs_kfunc_set);
-	if (ret || !IS_ENABLED(CONFIG_BINFMT_MISC_BPF))
+	if (ret ||
+	    (!IS_ENABLED(CONFIG_BINFMT_MISC_BPF) && !IS_ENABLED(CONFIG_FS_DP_BPF)))
 		return ret;
 	return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
 					 &bpf_fs_kfunc_set);
diff --git a/fs/fs_dp.c b/fs/fs_dp.c
index 1c370f6ffa42..5b0aba2c182a 100644
--- a/fs/fs_dp.c
+++ b/fs/fs_dp.c
@@ -191,6 +191,12 @@ static struct bpf_struct_ops bpf_fs_dp_ops = {
 	.owner = THIS_MODULE,
 };
 
+bool bpf_prog_is_fs_dp_ops(const struct bpf_prog *prog)
+{
+	return prog->type == BPF_PROG_TYPE_STRUCT_OPS &&
+	       prog->aux->st_ops == &bpf_fs_dp_ops;
+}
+
 static int __init fs_dp_init(void)
 {
 	int ret;
diff --git a/include/linux/fs_dp.h b/include/linux/fs_dp.h
index ec918c316751..e3a65dedd940 100644
--- a/include/linux/fs_dp.h
+++ b/include/linux/fs_dp.h
@@ -11,6 +11,7 @@
 
 #define FS_DP_OPS_NAME_MAX	16
 
+struct bpf_prog;
 struct file;
 struct file_kattr;
 
@@ -41,12 +42,17 @@ struct fs_dp_ops {
 #ifdef CONFIG_FS_DP_BPF
 struct fs_dp_ops *fs_dp_ops_get(const char *name);
 void fs_dp_ops_put(struct fs_dp_ops *ops);
+bool bpf_prog_is_fs_dp_ops(const struct bpf_prog *prog);
 #else
 static inline struct fs_dp_ops *fs_dp_ops_get(const char *name)
 {
 	return NULL;
 }
 static inline void fs_dp_ops_put(struct fs_dp_ops *ops) { }
+static inline bool bpf_prog_is_fs_dp_ops(const struct bpf_prog *prog)
+{
+	return false;
+}
 #endif
 
 #endif /* _LINUX_FS_DP_H */
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 3/6] xfs: add BPF data placement hook at file open
  2026-12-01  4:34 [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Naohiro Aota
  2026-12-01  4:34 ` [PATCH 1/6] fs: add fs_dp BPF struct_ops for file data placement Naohiro Aota
  2026-12-01  4:34 ` [PATCH 2/6] bpf: allow fs_dp struct_ops programs to call FS kfuncs Naohiro Aota
@ 2026-12-01  4:34 ` Naohiro Aota
  2026-12-01  4:49   ` sashiko-bot
  2026-12-01  4:34 ` [PATCH 4/6] xfs: add ioctls to select a BPF data placement policy Naohiro Aota
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 12+ messages in thread
From: Naohiro Aota @ 2026-12-01  4:34 UTC (permalink / raw)
  To: viro, brauner, cem, hch
  Cc: ast, djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf,
	linux-kernel, dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi,
	Naohiro Aota

On zoned XFS, we can set the write lifetime hint on files, so that XFS
separates file data into zones by hint. This helps garbage collection by
reducing the amount of data that needs to be moved.

However, we need to tweak each application to set the hint, which is often
cumbersome and hard to adapt to workload changes. Instead, let a BPF
program set the hint.

Attach an fs_dp_ops data placement policy to an xfs_mount, and call its
file_open() callback from xfs_file_open() when the file is opened for
writing. The mount holds a reference on the policy, so it keeps working
until the mount stops using it or is unmounted.

This is built when CONFIG_FS_DP_BPF is enabled.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/xfs/Makefile    |  2 ++
 fs/xfs/xfs_bpf.c   | 81 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_bpf.h   | 22 +++++++++++++
 fs/xfs/xfs_file.c  |  2 ++
 fs/xfs/xfs_mount.h |  4 +++
 fs/xfs/xfs_super.c |  2 ++
 6 files changed, 113 insertions(+)
 create mode 100644 fs/xfs/xfs_bpf.c
 create mode 100644 fs/xfs/xfs_bpf.h

diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile
index 399a207f2d0e..ed3476d43640 100644
--- a/fs/xfs/Makefile
+++ b/fs/xfs/Makefile
@@ -161,6 +161,8 @@ xfs-$(CONFIG_XFS_LIVE_HOOKS)	+= xfs_hooks.o
 xfs-$(CONFIG_XFS_MEMORY_BUFS)	+= xfs_buf_mem.o
 xfs-$(CONFIG_XFS_BTREE_IN_MEM)	+= libxfs/xfs_btree_mem.o
 
+xfs-$(CONFIG_FS_DP_BPF)		+= xfs_bpf.o
+
 # online scrub/repair
 ifeq ($(CONFIG_XFS_ONLINE_SCRUB),y)
 
diff --git a/fs/xfs/xfs_bpf.c b/fs/xfs/xfs_bpf.c
new file mode 100644
index 000000000000..0e7ef40d98d9
--- /dev/null
+++ b/fs/xfs/xfs_bpf.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2025 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/fs.h>
+#include <linux/fileattr.h>
+#include <linux/fs_dp.h>
+
+#include "xfs_platform.h"
+#include "xfs_shared.h"
+#include "xfs_format.h"
+#include "xfs_trans_resv.h"
+#include "xfs_mount.h"
+#include "xfs_ioctl.h"
+#include "xfs_bpf.h"
+
+/* Protect xfs_mount->m_dp_ops. */
+static DEFINE_MUTEX(xfs_dp_set_lock);
+
+/* Make @mp use @ops, whose reference it takes over, or no ops if NULL. */
+static void
+xfs_dp_ops_set(
+	struct xfs_mount	*mp,
+	struct fs_dp_ops	*ops)
+{
+	struct fs_dp_ops *old;
+
+	mutex_lock(&xfs_dp_set_lock);
+	old = rcu_replace_pointer(mp->m_dp_ops, ops, lockdep_is_held(&xfs_dp_set_lock));
+	mutex_unlock(&xfs_dp_set_lock);
+	if (old) {
+		/* Synchronize with xfs_bpf_file_open(). */
+		synchronize_rcu();
+		fs_dp_ops_put(old);
+	}
+}
+
+void
+xfs_bpf_file_open(
+	struct inode	*inode,
+	struct file	*file)
+{
+	struct xfs_mount	*mp = XFS_M(inode->i_sb);
+	struct fs_dp_file_open_res res = {
+		.i_write_hint	= READ_ONCE(inode->i_write_hint),
+	};
+	struct file_kattr	fa = {};
+	struct fs_dp_ops	*ops;
+	struct dentry		*parent;
+	unsigned long		parent_ino;
+
+	if (!(file->f_mode & FMODE_WRITE))
+		return;
+	if (!rcu_access_pointer(mp->m_dp_ops))
+		return;
+
+	xfs_fileattr_get(file->f_path.dentry, &fa);
+
+	parent = dget_parent(file->f_path.dentry);
+	parent_ino = d_inode(parent)->i_ino;
+	dput(parent);
+
+	rcu_read_lock();
+	ops = rcu_dereference(mp->m_dp_ops);
+	if (ops)
+		ops->file_open(file, parent_ino, &fa, &res);
+	rcu_read_unlock();
+
+	if (res.i_write_hint >= WRITE_LIFE_HINT_NR)
+		return;
+	WRITE_ONCE(inode->i_write_hint, res.i_write_hint);
+}
+
+/* Stop using the data placement ops when unmounting. */
+void
+xfs_bpf_unmount(
+	struct xfs_mount	*mp)
+{
+	xfs_dp_ops_set(mp, NULL);
+}
diff --git a/fs/xfs/xfs_bpf.h b/fs/xfs/xfs_bpf.h
new file mode 100644
index 000000000000..1dd7f179f75d
--- /dev/null
+++ b/fs/xfs/xfs_bpf.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2025 Western Digital Corporation or its affiliates.
+ */
+#ifndef __XFS_BPF_H__
+#define __XFS_BPF_H__
+
+#include <linux/fs.h>
+
+struct xfs_mount;
+
+#ifdef CONFIG_FS_DP_BPF
+void xfs_bpf_file_open(
+	struct inode *inode,
+	struct file *file);
+void xfs_bpf_unmount(struct xfs_mount *mp);
+#else
+static inline void xfs_bpf_file_open(struct inode *inode, struct file *file) { }
+static inline void xfs_bpf_unmount(struct xfs_mount *mp) { }
+#endif
+
+#endif /* __XFS_BPF_H__ */
diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c
index ad1ce4f99ad8..189021973a4f 100644
--- a/fs/xfs/xfs_file.c
+++ b/fs/xfs/xfs_file.c
@@ -29,6 +29,7 @@
 #include "xfs_zone_alloc.h"
 #include "xfs_error.h"
 #include "xfs_errortag.h"
+#include "xfs_bpf.h"
 
 #include <linux/dax.h>
 #include <linux/falloc.h>
@@ -1796,6 +1797,7 @@ xfs_file_open(
 	file->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
 	if (xfs_get_atomic_write_min(XFS_I(inode)) > 0)
 		file->f_mode |= FMODE_CAN_ATOMIC_WRITE;
+	xfs_bpf_file_open(inode, file);
 	return generic_file_open(inode, file);
 }
 
diff --git a/fs/xfs/xfs_mount.h b/fs/xfs/xfs_mount.h
index cbcfb89c67a4..055454c6a86a 100644
--- a/fs/xfs/xfs_mount.h
+++ b/fs/xfs/xfs_mount.h
@@ -14,6 +14,7 @@ struct xfs_quotainfo;
 struct xfs_da_geometry;
 struct xfs_perag;
 struct xfs_healthmon;
+struct fs_dp_ops;
 
 /* dynamic preallocation free space thresholds, 5% down to 1% */
 enum {
@@ -355,6 +356,9 @@ typedef struct xfs_mount {
 	/* Private data referring to a health monitor object. */
 	struct xfs_healthmon __rcu	*m_healthmon;
 
+	/* BPF data placement policy in use. */
+	struct fs_dp_ops __rcu	*m_dp_ops;
+
 	/* Index of uuid record in the uuid xarray. */
 	unsigned int		m_uuid_table_index;
 
diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
index 2f6999b6947e..ff7cb09532e7 100644
--- a/fs/xfs/xfs_super.c
+++ b/fs/xfs/xfs_super.c
@@ -50,6 +50,7 @@
 #include "xfs_rtalloc.h"
 #include "xfs_zone_alloc.h"
 #include "xfs_healthmon.h"
+#include "xfs_bpf.h"
 #include "scrub/stats.h"
 #include "scrub/rcbag_btree.h"
 
@@ -1271,6 +1272,7 @@ xfs_fs_put_super(
 	struct xfs_mount	*mp = XFS_M(sb);
 
 	xfs_notice(mp, "Unmounting Filesystem %pU", &mp->m_sb.sb_uuid);
+	xfs_bpf_unmount(mp);
 	xfs_filestream_unmount(mp);
 	xfs_unmountfs(mp);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 4/6] xfs: add ioctls to select a BPF data placement policy
  2026-12-01  4:34 [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Naohiro Aota
                   ` (2 preceding siblings ...)
  2026-12-01  4:34 ` [PATCH 3/6] xfs: add BPF data placement hook at file open Naohiro Aota
@ 2026-12-01  4:34 ` Naohiro Aota
  2026-12-01  4:34 ` [PATCH 5/6] xfs: bind a BPF data placement policy to a project ID Naohiro Aota
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Naohiro Aota @ 2026-12-01  4:34 UTC (permalink / raw)
  To: viro, brauner, cem, hch
  Cc: ast, djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf,
	linux-kernel, dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi,
	Naohiro Aota

Introduce XFS_IOC_SET_DP_OPS and XFS_IOC_GET_DP_OPS to select a BPF data
placement policy for a file system. XFS_IOC_SET_DP_OPS makes the file
system use the policy of that name, or stop using one if the name is empty.
XFS_IOC_GET_DP_OPS returns the name of the policy in use. Setting a policy
requires CAP_SYS_ADMIN.

struct xfs_dp_ops_name carries a flags field for future extension.
XFS_IOC_GET_DP_OPS is _IOWR so that it can read the flags in. The ioctls
must be issued on the root directory of the file system for now. This
leaves room for per-directory or per-project-ID policies later.

The file system holds a reference on the policy via fs_dp_ops_get(), so it
keeps working until the file system stops using it or is unmounted, even if
the struct_ops is unregistered in the meantime.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/xfs/libxfs/xfs_fs.h | 14 ++++++++++
 fs/xfs/xfs_bpf.c       | 63 ++++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_bpf.h       | 10 +++++++
 fs/xfs/xfs_ioctl.c     |  6 ++++
 4 files changed, 93 insertions(+)

diff --git a/fs/xfs/libxfs/xfs_fs.h b/fs/xfs/libxfs/xfs_fs.h
index 185f09f327c0..a6c05ce9bc6d 100644
--- a/fs/xfs/libxfs/xfs_fs.h
+++ b/fs/xfs/libxfs/xfs_fs.h
@@ -1191,6 +1191,18 @@ struct xfs_verify_media {
 
 #define XFS_VERIFY_MEDIA_FLAGS	(XFS_VERIFY_MEDIA_REPORT)
 
+#define XFS_DP_OPS_NAME_MAX	16
+
+/*
+ * Name of a BPF data placement policy, i.e. a registered struct fs_dp_ops
+ * struct_ops, for XFS_IOC_{SET,GET}_DP_OPS.
+ */
+struct xfs_dp_ops_name {
+	char	name[XFS_DP_OPS_NAME_MAX];	/* NUL-terminated */
+	__u32	flags;				/* XFS_DP_OPS_F_* */
+	__u32	pad[3];				/* zero */
+};
+
 /*
  * ioctl commands that are used by Linux filesystems
  */
@@ -1234,6 +1246,8 @@ struct xfs_verify_media {
 #define XFS_IOC_HEALTH_FD_ON_MONITORED_FS \
 				_IOW ('X', 69, struct xfs_health_file_on_monitored_fs)
 #define XFS_IOC_VERIFY_MEDIA	_IOWR('X', 70, struct xfs_verify_media)
+#define XFS_IOC_SET_DP_OPS	_IOW ('X', 71, struct xfs_dp_ops_name)
+#define XFS_IOC_GET_DP_OPS	_IOWR('X', 72, struct xfs_dp_ops_name)
 
 /*
  * ioctl commands that replace IRIX syssgi()'s
diff --git a/fs/xfs/xfs_bpf.c b/fs/xfs/xfs_bpf.c
index 0e7ef40d98d9..71f7e3a463e3 100644
--- a/fs/xfs/xfs_bpf.c
+++ b/fs/xfs/xfs_bpf.c
@@ -3,9 +3,12 @@
  * Copyright (C) 2025 Western Digital Corporation or its affiliates.
  */
 
+#include <linux/capability.h>
 #include <linux/fs.h>
 #include <linux/fileattr.h>
 #include <linux/fs_dp.h>
+#include <linux/string.h>
+#include <linux/uaccess.h>
 
 #include "xfs_platform.h"
 #include "xfs_shared.h"
@@ -72,6 +75,66 @@ xfs_bpf_file_open(
 	WRITE_ONCE(inode->i_write_hint, res.i_write_hint);
 }
 
+int
+xfs_ioc_set_dp_ops(
+	struct file		*filp,
+	void __user		*arg)
+{
+	struct xfs_mount	*mp = XFS_M(file_inode(filp)->i_sb);
+	struct xfs_dp_ops_name	dn;
+	struct fs_dp_ops	*ops = NULL;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+	if (copy_from_user(&dn, arg, sizeof(dn)))
+		return -EFAULT;
+	/* No flags defined yet; reject non-zero for forward compatibility. */
+	if (dn.flags ||
+	    memchr_inv(dn.pad, 0, sizeof(dn.pad)) ||
+	    !memchr(dn.name, 0, sizeof(dn.name)))
+		return -EINVAL;
+	/* Only for the whole file system, via its root directory. */
+	if (filp->f_path.dentry != mp->m_super->s_root)
+		return -EINVAL;
+
+	if (dn.name[0]) {
+		ops = fs_dp_ops_get(dn.name);
+		if (!ops)
+			return -ENOENT;
+	}
+	xfs_dp_ops_set(mp, ops);
+	return 0;
+}
+
+int
+xfs_ioc_get_dp_ops(
+	struct file		*filp,
+	void __user		*arg)
+{
+	struct xfs_mount	*mp = XFS_M(file_inode(filp)->i_sb);
+	struct xfs_dp_ops_name	dn;
+	struct fs_dp_ops	*ops;
+
+	if (copy_from_user(&dn, arg, sizeof(dn)))
+		return -EFAULT;
+	/* No flags defined yet; reject non-zero for forward compatibility. */
+	if (dn.flags || memchr_inv(dn.pad, 0, sizeof(dn.pad)))
+		return -EINVAL;
+	if (filp->f_path.dentry != mp->m_super->s_root)
+		return -EINVAL;
+
+	memset(dn.name, 0, sizeof(dn.name));
+	rcu_read_lock();
+	ops = rcu_dereference(mp->m_dp_ops);
+	if (ops)
+		strscpy(dn.name, ops->name);
+	rcu_read_unlock();
+
+	if (copy_to_user(arg, &dn, sizeof(dn)))
+		return -EFAULT;
+	return 0;
+}
+
 /* Stop using the data placement ops when unmounting. */
 void
 xfs_bpf_unmount(
diff --git a/fs/xfs/xfs_bpf.h b/fs/xfs/xfs_bpf.h
index 1dd7f179f75d..52a6ae8f4952 100644
--- a/fs/xfs/xfs_bpf.h
+++ b/fs/xfs/xfs_bpf.h
@@ -13,9 +13,19 @@ struct xfs_mount;
 void xfs_bpf_file_open(
 	struct inode *inode,
 	struct file *file);
+int xfs_ioc_set_dp_ops(struct file *filp, void __user *arg);
+int xfs_ioc_get_dp_ops(struct file *filp, void __user *arg);
 void xfs_bpf_unmount(struct xfs_mount *mp);
 #else
 static inline void xfs_bpf_file_open(struct inode *inode, struct file *file) { }
+static inline int xfs_ioc_set_dp_ops(struct file *filp, void __user *arg)
+{
+	return -EOPNOTSUPP;
+}
+static inline int xfs_ioc_get_dp_ops(struct file *filp, void __user *arg)
+{
+	return -EOPNOTSUPP;
+}
 static inline void xfs_bpf_unmount(struct xfs_mount *mp) { }
 #endif
 
diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c
index c76e36dadf95..40c6380e5129 100644
--- a/fs/xfs/xfs_ioctl.c
+++ b/fs/xfs/xfs_ioctl.c
@@ -46,6 +46,7 @@
 #include "xfs_verify_media.h"
 #include "xfs_zone_priv.h"
 #include "xfs_zone_alloc.h"
+#include "xfs_bpf.h"
 
 #include <linux/mount.h>
 #include <linux/fileattr.h>
@@ -1590,6 +1591,11 @@ xfs_file_ioctl(
 	case XFS_IOC_VERIFY_MEDIA:
 		return xfs_ioc_verify_media(filp, arg);
 
+	case XFS_IOC_SET_DP_OPS:
+		return xfs_ioc_set_dp_ops(filp, arg);
+	case XFS_IOC_GET_DP_OPS:
+		return xfs_ioc_get_dp_ops(filp, arg);
+
 	default:
 		return -ENOTTY;
 	}
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 5/6] xfs: bind a BPF data placement policy to a project ID
  2026-12-01  4:34 [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Naohiro Aota
                   ` (3 preceding siblings ...)
  2026-12-01  4:34 ` [PATCH 4/6] xfs: add ioctls to select a BPF data placement policy Naohiro Aota
@ 2026-12-01  4:34 ` Naohiro Aota
  2026-12-01  4:34 ` [PATCH 6/6] xfs: let XFS_IOC_{SET,GET}_DP_OPS act on " Naohiro Aota
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Naohiro Aota @ 2026-12-01  4:34 UTC (permalink / raw)
  To: viro, brauner, cem, hch
  Cc: ast, djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf,
	linux-kernel, dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi,
	Naohiro Aota

So far a data placement policy can only be bound to a whole mount. Allow
binding a policy to a project ID as well, so that files of different
projects can use different policies without the BPF program having to
filter on the project ID itself.

Keep per-project-ID policies in an xarray on the mount, indexed by project
ID. In xfs_file_open(), look up the file's project ID first and fall back
to the mount-wide policy if none is bound for it. Each bound policy holds a
reference, dropped when it is replaced or at unmount.

The ioctl to actually bind a per-project-ID policy comes in the next patch.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/xfs/xfs_bpf.c   | 14 ++++++++++++--
 fs/xfs/xfs_mount.h |  3 ++-
 fs/xfs/xfs_super.c |  1 +
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/fs/xfs/xfs_bpf.c b/fs/xfs/xfs_bpf.c
index 71f7e3a463e3..ced985c219c6 100644
--- a/fs/xfs/xfs_bpf.c
+++ b/fs/xfs/xfs_bpf.c
@@ -55,7 +55,7 @@ xfs_bpf_file_open(
 
 	if (!(file->f_mode & FMODE_WRITE))
 		return;
-	if (!rcu_access_pointer(mp->m_dp_ops))
+	if (!rcu_access_pointer(mp->m_dp_ops) && xa_empty(&mp->m_dp_ops_by_projid))
 		return;
 
 	xfs_fileattr_get(file->f_path.dentry, &fa);
@@ -65,7 +65,10 @@ xfs_bpf_file_open(
 	dput(parent);
 
 	rcu_read_lock();
-	ops = rcu_dereference(mp->m_dp_ops);
+	/* A per-project-ID policy overrides the mount-wide one. */
+	ops = xa_load(&mp->m_dp_ops_by_projid, fa.fsx_projid);
+	if (!ops)
+		ops = rcu_dereference(mp->m_dp_ops);
 	if (ops)
 		ops->file_open(file, parent_ino, &fa, &res);
 	rcu_read_unlock();
@@ -140,5 +143,12 @@ void
 xfs_bpf_unmount(
 	struct xfs_mount	*mp)
 {
+	struct fs_dp_ops	*ops;
+	unsigned long		projid;
+
 	xfs_dp_ops_set(mp, NULL);
+
+	xa_for_each(&mp->m_dp_ops_by_projid, projid, ops)
+		fs_dp_ops_put(ops);
+	xa_destroy(&mp->m_dp_ops_by_projid);
 }
diff --git a/fs/xfs/xfs_mount.h b/fs/xfs/xfs_mount.h
index 055454c6a86a..ce07d92158c2 100644
--- a/fs/xfs/xfs_mount.h
+++ b/fs/xfs/xfs_mount.h
@@ -356,8 +356,9 @@ typedef struct xfs_mount {
 	/* Private data referring to a health monitor object. */
 	struct xfs_healthmon __rcu	*m_healthmon;
 
-	/* BPF data placement policy in use. */
+	/* BPF data placement policy in use, and per-project-ID overrides. */
 	struct fs_dp_ops __rcu	*m_dp_ops;
+	struct xarray		m_dp_ops_by_projid;
 
 	/* Index of uuid record in the uuid xarray. */
 	unsigned int		m_uuid_table_index;
diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
index ff7cb09532e7..f1c2e5b13a03 100644
--- a/fs/xfs/xfs_super.c
+++ b/fs/xfs/xfs_super.c
@@ -2300,6 +2300,7 @@ xfs_init_fs_context(
 	spin_lock_init(&mp->m_sb_lock);
 	for (i = 0; i < XG_TYPE_MAX; i++)
 		xa_init(&mp->m_groups[i].xa);
+	xa_init(&mp->m_dp_ops_by_projid);
 	mutex_init(&mp->m_growlock);
 	mutex_init(&mp->m_metafile_resv_lock);
 	INIT_WORK(&mp->m_flush_inodes_work, xfs_flush_inodes_worker);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 6/6] xfs: let XFS_IOC_{SET,GET}_DP_OPS act on a project ID
  2026-12-01  4:34 [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Naohiro Aota
                   ` (4 preceding siblings ...)
  2026-12-01  4:34 ` [PATCH 5/6] xfs: bind a BPF data placement policy to a project ID Naohiro Aota
@ 2026-12-01  4:34 ` Naohiro Aota
  2026-12-01  4:37 ` [PATCH 1/2] fs_dp: let a policy pick a write stream Naohiro Aota
  2026-12-01  4:56 ` [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Alexei Starovoitov
  7 siblings, 0 replies; 12+ messages in thread
From: Naohiro Aota @ 2026-12-01  4:34 UTC (permalink / raw)
  To: viro, brauner, cem, hch
  Cc: ast, djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf,
	linux-kernel, dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi,
	Naohiro Aota

Define XFS_DP_OPS_F_PROJID in the flags field of struct xfs_dp_ops_name.
When set, XFS_IOC_SET_DP_OPS binds the named policy to the project ID of
the file the ioctl is issued on instead of the whole file system (an empty
name unbinds it), and XFS_IOC_GET_DP_OPS returns the policy bound to that
project ID. Per-project-ID bindings override the mount-wide one.

Without the flag the behaviour is unchanged: the policy is bound to / read
from the whole file system and the ioctl must be issued on its root
directory.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/xfs/libxfs/xfs_fs.h |  3 +++
 fs/xfs/xfs_bpf.c       | 59 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 54 insertions(+), 8 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_fs.h b/fs/xfs/libxfs/xfs_fs.h
index a6c05ce9bc6d..868704a17e10 100644
--- a/fs/xfs/libxfs/xfs_fs.h
+++ b/fs/xfs/libxfs/xfs_fs.h
@@ -1193,6 +1193,9 @@ struct xfs_verify_media {
 
 #define XFS_DP_OPS_NAME_MAX	16
 
+/* Bind to the project ID of the ioctl'd file, not the whole file system. */
+#define XFS_DP_OPS_F_PROJID	(1u << 0)
+
 /*
  * Name of a BPF data placement policy, i.e. a registered struct fs_dp_ops
  * struct_ops, for XFS_IOC_{SET,GET}_DP_OPS.
diff --git a/fs/xfs/xfs_bpf.c b/fs/xfs/xfs_bpf.c
index ced985c219c6..e730161337dc 100644
--- a/fs/xfs/xfs_bpf.c
+++ b/fs/xfs/xfs_bpf.c
@@ -15,6 +15,7 @@
 #include "xfs_format.h"
 #include "xfs_trans_resv.h"
 #include "xfs_mount.h"
+#include "xfs_inode.h"
 #include "xfs_ioctl.h"
 #include "xfs_bpf.h"
 
@@ -39,6 +40,35 @@ xfs_dp_ops_set(
 	}
 }
 
+/* Make @mp use @ops for files of @projid, taking over its reference. */
+static int
+xfs_dp_ops_set_projid(
+	struct xfs_mount	*mp,
+	prid_t			projid,
+	struct fs_dp_ops	*ops)
+{
+	struct fs_dp_ops	*old;
+
+	mutex_lock(&xfs_dp_set_lock);
+	if (ops)
+		old = xa_store(&mp->m_dp_ops_by_projid, projid, ops, GFP_KERNEL);
+	else
+		old = xa_erase(&mp->m_dp_ops_by_projid, projid);
+	mutex_unlock(&xfs_dp_set_lock);
+
+	if (xa_is_err(old)) {
+		if (ops)
+			fs_dp_ops_put(ops);
+		return xa_err(old);
+	}
+	if (old) {
+		/* Synchronize with xfs_bpf_file_open(). */
+		synchronize_rcu();
+		fs_dp_ops_put(old);
+	}
+	return 0;
+}
+
 void
 xfs_bpf_file_open(
 	struct inode	*inode,
@@ -91,13 +121,17 @@ xfs_ioc_set_dp_ops(
 		return -EPERM;
 	if (copy_from_user(&dn, arg, sizeof(dn)))
 		return -EFAULT;
-	/* No flags defined yet; reject non-zero for forward compatibility. */
-	if (dn.flags ||
+	if ((dn.flags & ~XFS_DP_OPS_F_PROJID) ||
 	    memchr_inv(dn.pad, 0, sizeof(dn.pad)) ||
 	    !memchr(dn.name, 0, sizeof(dn.name)))
 		return -EINVAL;
-	/* Only for the whole file system, via its root directory. */
-	if (filp->f_path.dentry != mp->m_super->s_root)
+	/*
+	 * Binding to the whole file system is only allowed via its root
+	 * directory. A per-project-ID binding uses the file's own project ID,
+	 * so any file of the file system will do.
+	 */
+	if (!(dn.flags & XFS_DP_OPS_F_PROJID) &&
+	    filp->f_path.dentry != mp->m_super->s_root)
 		return -EINVAL;
 
 	if (dn.name[0]) {
@@ -105,6 +139,9 @@ xfs_ioc_set_dp_ops(
 		if (!ops)
 			return -ENOENT;
 	}
+	if (dn.flags & XFS_DP_OPS_F_PROJID)
+		return xfs_dp_ops_set_projid(mp,
+				XFS_I(file_inode(filp))->i_projid, ops);
 	xfs_dp_ops_set(mp, ops);
 	return 0;
 }
@@ -120,15 +157,21 @@ xfs_ioc_get_dp_ops(
 
 	if (copy_from_user(&dn, arg, sizeof(dn)))
 		return -EFAULT;
-	/* No flags defined yet; reject non-zero for forward compatibility. */
-	if (dn.flags || memchr_inv(dn.pad, 0, sizeof(dn.pad)))
+	if ((dn.flags & ~XFS_DP_OPS_F_PROJID) ||
+	    memchr_inv(dn.pad, 0, sizeof(dn.pad)))
 		return -EINVAL;
-	if (filp->f_path.dentry != mp->m_super->s_root)
+	/* The whole file system, via its root directory, unless F_PROJID. */
+	if (!(dn.flags & XFS_DP_OPS_F_PROJID) &&
+	    filp->f_path.dentry != mp->m_super->s_root)
 		return -EINVAL;
 
 	memset(dn.name, 0, sizeof(dn.name));
 	rcu_read_lock();
-	ops = rcu_dereference(mp->m_dp_ops);
+	if (dn.flags & XFS_DP_OPS_F_PROJID)
+		ops = xa_load(&mp->m_dp_ops_by_projid,
+			      XFS_I(file_inode(filp))->i_projid);
+	else
+		ops = rcu_dereference(mp->m_dp_ops);
 	if (ops)
 		strscpy(dn.name, ops->name);
 	rcu_read_unlock();
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 1/2] fs_dp: let a policy pick a write stream
  2026-12-01  4:34 [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Naohiro Aota
                   ` (5 preceding siblings ...)
  2026-12-01  4:34 ` [PATCH 6/6] xfs: let XFS_IOC_{SET,GET}_DP_OPS act on " Naohiro Aota
@ 2026-12-01  4:37 ` Naohiro Aota
  2026-12-01  4:37   ` [PATCH 2/2] xfs: bind write streams along with a data placement policy Naohiro Aota
  2026-12-01  4:56 ` [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Alexei Starovoitov
  7 siblings, 1 reply; 12+ messages in thread
From: Naohiro Aota @ 2026-12-01  4:37 UTC (permalink / raw)
  To: viro, brauner, cem, hch
  Cc: ast, djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf,
	linux-kernel, dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi,
	Naohiro Aota

An unmodified application cannot use write streams: a stream is
reserved through FS_IOC_WRITE_STREAM_ALLOC, which needs CAP_SYS_ADMIN,
and has to be set on each file with FS_IOC_WRITE_STREAM_SET.

Let a data placement policy assign them instead. file_open() picks a
stream by setting fs_dp_file_open_res.write_stream to a slot number.
Which stream a slot names is up to the filesystem, which binds a set of
streams along with the policy: streams belong to a filesystem, while one
policy can be bound to several filesystems.

As for the hint, write_stream holds the file's current stream on entry,
so that a policy that leaves it alone keeps it. A file can also be on a
stream that is not bound with the policy, set through
FS_IOC_WRITE_STREAM_SET; that is FS_DP_WRITE_STREAM_OTHER, which is not
a slot a policy can pick. A file on no stream has
FS_DP_WRITE_STREAM_NONE. A stream and a write lifetime hint are
exclusive, so a policy may set one or the other.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/fs_dp.c            | 14 ++++++++++++--
 include/linux/fs_dp.h | 12 ++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/fs/fs_dp.c b/fs/fs_dp.c
index 5b0aba2c182a..41bf58884457 100644
--- a/fs/fs_dp.c
+++ b/fs/fs_dp.c
@@ -46,9 +46,18 @@ static int bpf_fs_dp_btf_struct_access(struct bpf_verifier_log *log,
 	if (t != res_type)
 		return -EACCES;
 
-	if (off != offsetof(struct fs_dp_file_open_res, i_write_hint) ||
-	    size != sizeof_field(struct fs_dp_file_open_res, i_write_hint))
+	switch (off) {
+	case offsetof(struct fs_dp_file_open_res, i_write_hint):
+		if (size != sizeof_field(struct fs_dp_file_open_res, i_write_hint))
+			return -EACCES;
+		break;
+	case offsetof(struct fs_dp_file_open_res, write_stream):
+		if (size != sizeof_field(struct fs_dp_file_open_res, write_stream))
+			return -EACCES;
+		break;
+	default:
 		return -EACCES;
+	}
 
 	return SCALAR_VALUE;
 }
@@ -61,6 +70,7 @@ static const struct bpf_verifier_ops bpf_fs_dp_verifier_ops = {
 
 static int bpf_fs_dp_init(struct btf *btf)
 {
+	BTF_TYPE_EMIT_ENUM(FS_DP_WRITE_STREAM_OTHER);
 	return 0;
 }
 
diff --git a/include/linux/fs_dp.h b/include/linux/fs_dp.h
index e3a65dedd940..9304365646c1 100644
--- a/include/linux/fs_dp.h
+++ b/include/linux/fs_dp.h
@@ -18,6 +18,13 @@ struct file_kattr;
 /* Result a data placement policy returns from its file_open() callback. */
 struct fs_dp_file_open_res {
 	enum rw_hint i_write_hint;
+	u16 write_stream;
+};
+
+/* write_stream values other than a slot */
+enum {
+	FS_DP_WRITE_STREAM_NONE = (u16)-1,	/* not on a write stream */
+	FS_DP_WRITE_STREAM_OTHER = (u16)-2,	/* on a stream not bound */
 };
 
 /*
@@ -31,6 +38,11 @@ struct fs_dp_file_open_res {
  * @parent_ino is the inode number of the parent directory.
  * @fa carries the file's attributes (project ID, xflags, ...) and may be NULL
  * if the filesystem does not provide them.
+ * @res holds the file's current placement on entry: its write lifetime hint,
+ * and as write_stream the slot of its write stream among those the filesystem
+ * bound along with the policy, FS_DP_WRITE_STREAM_OTHER if it is on another
+ * one, or FS_DP_WRITE_STREAM_NONE. A policy may set a hint or a bound stream,
+ * not both.
  */
 struct fs_dp_ops {
 	char name[FS_DP_OPS_NAME_MAX];
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 2/2] xfs: bind write streams along with a data placement policy
  2026-12-01  4:37 ` [PATCH 1/2] fs_dp: let a policy pick a write stream Naohiro Aota
@ 2026-12-01  4:37   ` Naohiro Aota
  0 siblings, 0 replies; 12+ messages in thread
From: Naohiro Aota @ 2026-12-01  4:37 UTC (permalink / raw)
  To: viro, brauner, cem, hch
  Cc: ast, djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf,
	linux-kernel, dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi,
	Naohiro Aota

Let XFS_IOC_SET_DP_OPS take write stream fds (FS_IOC_WRITE_STREAM_ALLOC)
along with the policy name. The binding holds the stream files until it
is replaced, which keeps the streams reserved. When the policy picks
write stream n for a file being opened, set stream_fds[n] on the inode.

The streams are bound per file system or project ID rather than held by
the struct_ops: a stream belongs to one file system, while a policy can be
bound to several, each with its own streams.

A stream file pins the mount it was allocated on, so the policy has to be
unbound before the file system can be unmounted.

Pass the policy the slot of the inode's current stream,
FS_DP_WRITE_STREAM_OTHER if it is on a stream that is not bound, or
FS_DP_WRITE_STREAM_NONE. Do not update the inode if the policy changes
nothing, and ignore an invalid result (e.g. one with both a hint and a
stream, or with a slot that is not bound). Report ignored results and
streams that cannot be set through the xfs_bpf_file_open_reject
tracepoint.

Set the hint and the stream in one update under the ILOCK and i_lock,
so that a concurrent FS_IOC_WRITE_STREAM_SET or F_SET_RW_HINT cannot
leave the inode with both.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/xfs/libxfs/xfs_fs.h |   8 +-
 fs/xfs/xfs_bpf.c       | 221 ++++++++++++++++++++++++++++++++++++-----
 fs/xfs/xfs_mount.h     |   4 +-
 fs/xfs/xfs_trace.h     |  26 +++++
 4 files changed, 230 insertions(+), 29 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_fs.h b/fs/xfs/libxfs/xfs_fs.h
index 868704a17e10..186128598573 100644
--- a/fs/xfs/libxfs/xfs_fs.h
+++ b/fs/xfs/libxfs/xfs_fs.h
@@ -1192,6 +1192,7 @@ struct xfs_verify_media {
 #define XFS_VERIFY_MEDIA_FLAGS	(XFS_VERIFY_MEDIA_REPORT)
 
 #define XFS_DP_OPS_NAME_MAX	16
+#define XFS_DP_OPS_MAX_STREAMS	16
 
 /* Bind to the project ID of the ioctl'd file, not the whole file system. */
 #define XFS_DP_OPS_F_PROJID	(1u << 0)
@@ -1199,11 +1200,16 @@ struct xfs_verify_media {
 /*
  * Name of a BPF data placement policy, i.e. a registered struct fs_dp_ops
  * struct_ops, for XFS_IOC_{SET,GET}_DP_OPS.
+ *
+ * SET binds the write streams (FS_IOC_WRITE_STREAM_ALLOC) in stream_fds along
+ * with the policy, and holds them while bound. The policy picks
+ * stream_fds[n] by returning write stream n. GET returns nr_streams.
  */
 struct xfs_dp_ops_name {
 	char	name[XFS_DP_OPS_NAME_MAX];	/* NUL-terminated */
 	__u32	flags;				/* XFS_DP_OPS_F_* */
-	__u32	pad[3];				/* zero */
+	__u32	nr_streams;			/* <= XFS_DP_OPS_MAX_STREAMS */
+	__u64	stream_fds;			/* user pointer to __s32[nr_streams] */
 };
 
 /*
diff --git a/fs/xfs/xfs_bpf.c b/fs/xfs/xfs_bpf.c
index e730161337dc..78b49d5d342e 100644
--- a/fs/xfs/xfs_bpf.c
+++ b/fs/xfs/xfs_bpf.c
@@ -4,11 +4,13 @@
  */
 
 #include <linux/capability.h>
+#include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/fileattr.h>
 #include <linux/fs_dp.h>
 #include <linux/string.h>
 #include <linux/uaccess.h>
+#include <linux/write_streams.h>
 
 #include "xfs_platform.h"
 #include "xfs_shared.h"
@@ -16,19 +18,81 @@
 #include "xfs_trans_resv.h"
 #include "xfs_mount.h"
 #include "xfs_inode.h"
+#include "xfs_filestream.h"
 #include "xfs_ioctl.h"
+#include "xfs_trace.h"
 #include "xfs_bpf.h"
 
+/* A data placement policy and the write streams bound along with it. */
+struct xfs_dp_ops {
+	struct fs_dp_ops	*fs_ops;
+	unsigned int		nr_streams;
+	struct file		*streams[] __counted_by(nr_streams);
+};
+
 /* Protect xfs_mount->m_dp_ops. */
 static DEFINE_MUTEX(xfs_dp_set_lock);
 
+static void
+xfs_dp_ops_free(
+	struct xfs_dp_ops	*ops)
+{
+	for (unsigned int i = 0; i < ops->nr_streams; i++) {
+		if (ops->streams[i])
+			fput(ops->streams[i]);
+	}
+	fs_dp_ops_put(ops->fs_ops);
+	kfree(ops);
+}
+
+/*
+ * Bind the policy named @name and the write streams in @stream_fds. Each slot
+ * holds its stream until the ops are replaced.
+ */
+static struct xfs_dp_ops *
+xfs_dp_ops_alloc(
+	const char		*name,
+	const s32		*stream_fds,
+	unsigned int		nr_streams)
+{
+	struct xfs_dp_ops	*ops;
+
+	ops = kzalloc_flex(*ops, streams, nr_streams);
+	if (!ops)
+		return ERR_PTR(-ENOMEM);
+	ops->nr_streams = nr_streams;
+
+	ops->fs_ops = fs_dp_ops_get(name);
+	if (!ops->fs_ops) {
+		kfree(ops);
+		return ERR_PTR(-ENOENT);
+	}
+
+	for (unsigned int i = 0; i < nr_streams; i++) {
+		struct file	*stream = fget(stream_fds[i]);
+
+		if (!stream)
+			goto out_inval;
+		if (write_stream_get_id(stream, NULL) < 0) {
+			fput(stream);
+			goto out_inval;
+		}
+		ops->streams[i] = stream;
+	}
+	return ops;
+
+out_inval:
+	xfs_dp_ops_free(ops);
+	return ERR_PTR(-EINVAL);
+}
+
 /* Make @mp use @ops, whose reference it takes over, or no ops if NULL. */
 static void
 xfs_dp_ops_set(
 	struct xfs_mount	*mp,
-	struct fs_dp_ops	*ops)
+	struct xfs_dp_ops	*ops)
 {
-	struct fs_dp_ops *old;
+	struct xfs_dp_ops	*old;
 
 	mutex_lock(&xfs_dp_set_lock);
 	old = rcu_replace_pointer(mp->m_dp_ops, ops, lockdep_is_held(&xfs_dp_set_lock));
@@ -36,7 +100,7 @@ xfs_dp_ops_set(
 	if (old) {
 		/* Synchronize with xfs_bpf_file_open(). */
 		synchronize_rcu();
-		fs_dp_ops_put(old);
+		xfs_dp_ops_free(old);
 	}
 }
 
@@ -45,9 +109,9 @@ static int
 xfs_dp_ops_set_projid(
 	struct xfs_mount	*mp,
 	prid_t			projid,
-	struct fs_dp_ops	*ops)
+	struct xfs_dp_ops	*ops)
 {
-	struct fs_dp_ops	*old;
+	struct xfs_dp_ops	*old;
 
 	mutex_lock(&xfs_dp_set_lock);
 	if (ops)
@@ -58,30 +122,108 @@ xfs_dp_ops_set_projid(
 
 	if (xa_is_err(old)) {
 		if (ops)
-			fs_dp_ops_put(ops);
+			xfs_dp_ops_free(ops);
 		return xa_err(old);
 	}
 	if (old) {
 		/* Synchronize with xfs_bpf_file_open(). */
 		synchronize_rcu();
-		fs_dp_ops_put(old);
+		xfs_dp_ops_free(old);
 	}
 	return 0;
 }
 
+/* The slot of @ops that holds the write stream of @ip, as a policy sees it. */
+static u16
+xfs_dp_ops_stream_slot(
+	struct xfs_dp_ops	*ops,
+	struct xfs_inode	*ip)
+{
+	u16			id = READ_ONCE(VFS_I(ip)->i_write_stream);
+	struct write_stream_pool *pool;
+
+	if (id == WRITE_STREAM_NOT_SET)
+		return FS_DP_WRITE_STREAM_NONE;
+	pool = xfs_inode_write_stream_pool(ip);
+	for (unsigned int i = 0; i < ops->nr_streams; i++) {
+		if (write_stream_get_id(ops->streams[i], pool) == id)
+			return i;
+	}
+	return FS_DP_WRITE_STREAM_OTHER;
+}
+
+static bool
+xfs_dp_res_valid(
+	struct xfs_dp_ops			*ops,
+	const struct fs_dp_file_open_res	*res)
+{
+	bool	has_hint = res->i_write_hint != WRITE_LIFE_NOT_SET;
+	bool	has_stream = res->write_stream != FS_DP_WRITE_STREAM_NONE;
+
+	if (res->i_write_hint >= WRITE_LIFE_HINT_NR)
+		return false;
+	if (has_hint && has_stream)
+		return false;
+	/* Also rejects FS_DP_WRITE_STREAM_OTHER, which is not a slot. */
+	if (has_stream && res->write_stream >= ops->nr_streams)
+		return false;
+	return true;
+}
+
+/*
+ * Set the placement the policy picked, as one update of both the hint and the
+ * stream, so that a concurrent FS_IOC_WRITE_STREAM_SET or F_SET_RW_HINT cannot
+ * leave the inode with both.
+ */
+static int
+xfs_bpf_set_placement(
+	struct xfs_inode	*ip,
+	enum rw_hint		hint,
+	struct file		*stream)
+{
+	struct inode		*inode = VFS_I(ip);
+	int			id = WRITE_STREAM_NOT_SET;
+	int			error = 0;
+
+	xfs_ilock(ip, XFS_ILOCK_EXCL);
+	if (stream) {
+		id = write_stream_get_id(stream,
+				xfs_inode_write_stream_pool(ip));
+		if (id < 0) {
+			error = id;
+			goto out_unlock;
+		}
+		if (xfs_inode_is_filestream(ip)) {
+			error = -EINVAL;
+			goto out_unlock;
+		}
+	}
+	spin_lock(&inode->i_lock);
+	WRITE_ONCE(inode->i_write_hint, hint);
+	WRITE_ONCE(inode->i_write_stream, id);
+	spin_unlock(&inode->i_lock);
+out_unlock:
+	xfs_iunlock(ip, XFS_ILOCK_EXCL);
+	return error;
+}
+
 void
 xfs_bpf_file_open(
 	struct inode	*inode,
 	struct file	*file)
 {
+	struct xfs_inode	*ip = XFS_I(inode);
 	struct xfs_mount	*mp = XFS_M(inode->i_sb);
 	struct fs_dp_file_open_res res = {
 		.i_write_hint	= READ_ONCE(inode->i_write_hint),
 	};
+	struct fs_dp_file_open_res cur;
 	struct file_kattr	fa = {};
-	struct fs_dp_ops	*ops;
+	struct xfs_dp_ops	*ops;
 	struct dentry		*parent;
 	unsigned long		parent_ino;
+	struct file		*stream = NULL;
+	int			error;
 
 	if (!(file->f_mode & FMODE_WRITE))
 		return;
@@ -99,13 +241,34 @@ xfs_bpf_file_open(
 	ops = xa_load(&mp->m_dp_ops_by_projid, fa.fsx_projid);
 	if (!ops)
 		ops = rcu_dereference(mp->m_dp_ops);
-	if (ops)
-		ops->file_open(file, parent_ino, &fa, &res);
+	if (!ops) {
+		rcu_read_unlock();
+		return;
+	}
+	res.write_stream = xfs_dp_ops_stream_slot(ops, ip);
+	cur = res;
+	ops->fs_ops->file_open(file, parent_ino, &fa, &res);
+	if (res.i_write_hint == cur.i_write_hint &&
+	    res.write_stream == cur.write_stream) {
+		rcu_read_unlock();
+		return;
+	}
+	if (!xfs_dp_res_valid(ops, &res)) {
+		rcu_read_unlock();
+		trace_xfs_bpf_file_open_reject(ip, res.i_write_hint,
+				res.write_stream, -EINVAL);
+		return;
+	}
+	if (res.write_stream != FS_DP_WRITE_STREAM_NONE)
+		stream = get_file(ops->streams[res.write_stream]);
 	rcu_read_unlock();
 
-	if (res.i_write_hint >= WRITE_LIFE_HINT_NR)
-		return;
-	WRITE_ONCE(inode->i_write_hint, res.i_write_hint);
+	error = xfs_bpf_set_placement(ip, res.i_write_hint, stream);
+	if (stream)
+		fput(stream);
+	if (error)
+		trace_xfs_bpf_file_open_reject(ip, res.i_write_hint,
+				res.write_stream, error);
 }
 
 int
@@ -115,15 +278,17 @@ xfs_ioc_set_dp_ops(
 {
 	struct xfs_mount	*mp = XFS_M(file_inode(filp)->i_sb);
 	struct xfs_dp_ops_name	dn;
-	struct fs_dp_ops	*ops = NULL;
+	struct xfs_dp_ops	*ops = NULL;
+	s32			stream_fds[XFS_DP_OPS_MAX_STREAMS];
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 	if (copy_from_user(&dn, arg, sizeof(dn)))
 		return -EFAULT;
 	if ((dn.flags & ~XFS_DP_OPS_F_PROJID) ||
-	    memchr_inv(dn.pad, 0, sizeof(dn.pad)) ||
-	    !memchr(dn.name, 0, sizeof(dn.name)))
+	    !memchr(dn.name, 0, sizeof(dn.name)) ||
+	    dn.nr_streams > XFS_DP_OPS_MAX_STREAMS ||
+	    (!dn.name[0] && (dn.nr_streams || dn.stream_fds)))
 		return -EINVAL;
 	/*
 	 * Binding to the whole file system is only allowed via its root
@@ -135,9 +300,12 @@ xfs_ioc_set_dp_ops(
 		return -EINVAL;
 
 	if (dn.name[0]) {
-		ops = fs_dp_ops_get(dn.name);
-		if (!ops)
-			return -ENOENT;
+		if (copy_from_user(stream_fds, u64_to_user_ptr(dn.stream_fds),
+				   dn.nr_streams * sizeof(*stream_fds)))
+			return -EFAULT;
+		ops = xfs_dp_ops_alloc(dn.name, stream_fds, dn.nr_streams);
+		if (IS_ERR(ops))
+			return PTR_ERR(ops);
 	}
 	if (dn.flags & XFS_DP_OPS_F_PROJID)
 		return xfs_dp_ops_set_projid(mp,
@@ -153,12 +321,11 @@ xfs_ioc_get_dp_ops(
 {
 	struct xfs_mount	*mp = XFS_M(file_inode(filp)->i_sb);
 	struct xfs_dp_ops_name	dn;
-	struct fs_dp_ops	*ops;
+	struct xfs_dp_ops	*ops;
 
 	if (copy_from_user(&dn, arg, sizeof(dn)))
 		return -EFAULT;
-	if ((dn.flags & ~XFS_DP_OPS_F_PROJID) ||
-	    memchr_inv(dn.pad, 0, sizeof(dn.pad)))
+	if ((dn.flags & ~XFS_DP_OPS_F_PROJID) || dn.nr_streams || dn.stream_fds)
 		return -EINVAL;
 	/* The whole file system, via its root directory, unless F_PROJID. */
 	if (!(dn.flags & XFS_DP_OPS_F_PROJID) &&
@@ -172,8 +339,10 @@ xfs_ioc_get_dp_ops(
 			      XFS_I(file_inode(filp))->i_projid);
 	else
 		ops = rcu_dereference(mp->m_dp_ops);
-	if (ops)
-		strscpy(dn.name, ops->name);
+	if (ops) {
+		strscpy(dn.name, ops->fs_ops->name);
+		dn.nr_streams = ops->nr_streams;
+	}
 	rcu_read_unlock();
 
 	if (copy_to_user(arg, &dn, sizeof(dn)))
@@ -186,12 +355,12 @@ void
 xfs_bpf_unmount(
 	struct xfs_mount	*mp)
 {
-	struct fs_dp_ops	*ops;
+	struct xfs_dp_ops	*ops;
 	unsigned long		projid;
 
 	xfs_dp_ops_set(mp, NULL);
 
 	xa_for_each(&mp->m_dp_ops_by_projid, projid, ops)
-		fs_dp_ops_put(ops);
+		xfs_dp_ops_free(ops);
 	xa_destroy(&mp->m_dp_ops_by_projid);
 }
diff --git a/fs/xfs/xfs_mount.h b/fs/xfs/xfs_mount.h
index f29e9ee16136..197094171327 100644
--- a/fs/xfs/xfs_mount.h
+++ b/fs/xfs/xfs_mount.h
@@ -14,7 +14,7 @@ struct xfs_quotainfo;
 struct xfs_da_geometry;
 struct xfs_perag;
 struct xfs_healthmon;
-struct fs_dp_ops;
+struct xfs_dp_ops;
 
 /* dynamic preallocation free space thresholds, 5% down to 1% */
 enum {
@@ -357,7 +357,7 @@ typedef struct xfs_mount {
 	struct xfs_healthmon __rcu	*m_healthmon;
 
 	/* BPF data placement policy in use, and per-project-ID overrides. */
-	struct fs_dp_ops __rcu	*m_dp_ops;
+	struct xfs_dp_ops __rcu	*m_dp_ops;
 	struct xarray		m_dp_ops_by_projid;
 
 	/* Index of uuid record in the uuid xarray. */
diff --git a/fs/xfs/xfs_trace.h b/fs/xfs/xfs_trace.h
index 28d49f158b22..67812441a87f 100644
--- a/fs/xfs/xfs_trace.h
+++ b/fs/xfs/xfs_trace.h
@@ -6503,6 +6503,32 @@ TRACE_EVENT(xfs_bmap_replace_cow_mapping,
 		  __entry->new_state)
 );
 
+TRACE_EVENT(xfs_bpf_file_open_reject,
+	TP_PROTO(struct xfs_inode *ip, enum rw_hint hint, u16 write_stream,
+		 int error),
+	TP_ARGS(ip, hint, write_stream, error),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(xfs_ino_t, ino)
+		__field(int, hint)
+		__field(u16, write_stream)
+		__field(int, error)
+	),
+	TP_fast_assign(
+		__entry->dev = ip->i_mount->m_super->s_dev;
+		__entry->ino = I_INO(ip);
+		__entry->hint = hint;
+		__entry->write_stream = write_stream;
+		__entry->error = error;
+	),
+	TP_printk("dev %d:%d ino 0x%llx hint %d write_stream %u error %d",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino,
+		  __entry->hint,
+		  __entry->write_stream,
+		  __entry->error)
+);
+
 #endif /* _TRACE_XFS_H */
 
 #undef TRACE_INCLUDE_PATH
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH 1/6] fs: add fs_dp BPF struct_ops for file data placement
  2026-12-01  4:34 ` [PATCH 1/6] fs: add fs_dp BPF struct_ops for file data placement Naohiro Aota
@ 2026-12-01  4:45   ` sashiko-bot
  0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-12-01  4:45 UTC (permalink / raw)
  To: Naohiro Aota; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] fs: missing __nullable suffix on fa parameter in
  __fs_dp_file_open()
--

commit 70c18d0e6fb82f627357fe30e49bdb298b3c5a44
Author: Naohiro Aota <naohiro.aota@wdc.com>

fs: add fs_dp BPF struct_ops for file data placement

This commit adds the fs_dp_ops BPF struct_ops interface and registry so a
BPF program can select a write lifetime hint when a file is opened for
writing.

> diff --git a/fs/fs_dp.c b/fs/fs_dp.c
> new file mode 100644
> index 0000000000000..1c370f6ffa42b
> --- /dev/null
> +++ b/fs/fs_dp.c
[ ... ]
> +static int __fs_dp_file_open(const struct file *file, unsigned long parent_ino,
> +			     const struct file_kattr *fa,
> +			     struct fs_dp_file_open_res *res)
> +{
> +	return 0;
> +}

[Severity: High]
Should the fa parameter in __fs_dp_file_open() (and struct fs_dp_ops) be
named fa__nullable?

The comment on struct fs_dp_ops in include/linux/fs_dp.h states that fa
may be NULL if the filesystem does not provide file attributes.  However,
prepare_arg_info() and btf_ctx_access() check the BTF parameter name on
the cfi_stubs function for the __nullable suffix.  Without fa__nullable,
the verifier marks fa as a non-NULL PTR_TO_BTF_ID | PTR_TRUSTED pointer
instead of adding PTR_MAYBE_NULL, allowing a BPF program to dereference
fa without a NULL check and trigger a kernel NULL pointer dereference when
a filesystem passes NULL.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201043452.3866039-1-naohiro.aota@wdc.com?part=1

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 3/6] xfs: add BPF data placement hook at file open
  2026-12-01  4:34 ` [PATCH 3/6] xfs: add BPF data placement hook at file open Naohiro Aota
@ 2026-12-01  4:49   ` sashiko-bot
  0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-12-01  4:49 UTC (permalink / raw)
  To: Naohiro Aota; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] xfs: unnecessary parent dentry refcount atomic ops in
  xfs_bpf_file_open()
--

commit 0cdaf5aa12590d62fbbcba19ad54035936a3eaaf
Author: Naohiro Aota <naohiro.aota@wdc.com>

xfs: add BPF data placement hook at file open

This commit adds a hook in xfs_file_open() to invoke an attached BPF data
placement policy when a file is opened for writing, allowing the policy to
update the inode's write lifetime hint.

> diff --git a/fs/xfs/xfs_bpf.c b/fs/xfs/xfs_bpf.c
> new file mode 100644
> index 0000000000000..0e7ef40d98d95
> --- /dev/null
> +++ b/fs/xfs/xfs_bpf.c
> @@ -0,0 +1,81 @@
[ ... ]
> +void
> +xfs_bpf_file_open(
> +	struct inode	*inode,
> +	struct file	*file)
> +{
> +	struct xfs_mount	*mp = XFS_M(inode->i_sb);
> +	struct fs_dp_file_open_res res = {
> +		.i_write_hint	= READ_ONCE(inode->i_write_hint),
> +	};
> +	struct file_kattr	fa = {};
> +	struct fs_dp_ops	*ops;
> +	struct dentry		*parent;
> +	unsigned long		parent_ino;
> +
> +	if (!(file->f_mode & FMODE_WRITE))
> +		return;
> +	if (!rcu_access_pointer(mp->m_dp_ops))
> +		return;
> +
> +	xfs_fileattr_get(file->f_path.dentry, &fa);
> +
> +	parent = dget_parent(file->f_path.dentry);
> +	parent_ino = d_inode(parent)->i_ino;
> +	dput(parent);

[Severity: Low]
Could this use d_parent_ino(file->f_path.dentry) instead of dget_parent()
and dput()?

Taking and dropping a reference on the parent dentry updates
parent->d_lockref on every writable file open when a policy is active,
causing cacheline and lockref contention across concurrent opens in the
same directory.  d_parent_ino() reads the parent inode number locklessly
under RCU and dentry->d_seq.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201043452.3866039-1-naohiro.aota@wdc.com?part=3

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/6] xfs: BPF data placement policy for zoned file systems
  2026-12-01  4:34 [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Naohiro Aota
                   ` (6 preceding siblings ...)
  2026-12-01  4:37 ` [PATCH 1/2] fs_dp: let a policy pick a write stream Naohiro Aota
@ 2026-12-01  4:56 ` Alexei Starovoitov
  7 siblings, 0 replies; 12+ messages in thread
From: Alexei Starovoitov @ 2026-12-01  4:56 UTC (permalink / raw)
  To: Naohiro Aota, viro, brauner, cem, hch
  Cc: djwong, dgc, jack, linux-xfs, linux-fsdevel, bpf, linux-kernel,
	dlemoal, Hans Holmberg, Anuj Gupta, Kanchan Joshi

On Mon, Nov 30, 2026 at 08:34 PM Naohiro Aota <naohiro.aota@wdc.com> wrote:
> We can help GC by setting the file's lifetime hint, as zoned XFS
> separates file data into zones by the hint. But most applications do
> not set the hint, and changing the application code itself is often not
> realistic.
>
> This series adds a struct_ops, fs_dp_ops, whose file_open callback picks
> the hint when a file is opened, e.g. based on its name, path, or project
> ID.

You waited since ALPSS to send this?
Sorry, but the whole thing is an overkill if 'hint' is the only goal. 
Here is a much simpler alternative:

  struct key {
          u64 dev;
          u64 dir;
  };

  struct {
          __uint(type, BPF_MAP_TYPE_HASH);
          __uint(max_entries, 1024);
          __type(key, struct key);
          __type(value, u64);
  } hints SEC(".maps");

  SEC("lsm/file_open")
  int BPF_PROG(set_hint, struct file *file)
  {
          struct dentry *dentry = file->f_path.dentry;
          struct key key = {
                  .dev = dentry->d_sb->s_dev,
                  .dir = dentry->d_parent->d_inode->i_ino,
          };
          u64 *hint;

          if (!(file->f_mode & FMODE_WRITE))
                  return 0;
          hint = bpf_map_lookup_elem(&hints, &key);
          if (hint)
                  bpf_file_set_write_hint(file, *hint);
          return 0;
  }


and the kernel bit:

diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c
index 35ccd98..937c278 100644
--- a/fs/bpf_fs_kfuncs.c
+++ b/fs/bpf_fs_kfuncs.c
@@ -498,6 +498,24 @@ __bpf_kfunc struct inode *bpf_real_data_inode(struct file *file)
 	return d_real_inode(file_dentry(file));
 }

+/**
+ * bpf_file_set_write_hint - set the write lifetime hint of a file's inode
+ * @file: file to set the hint on
+ * @hint: one of RWH_WRITE_LIFE_*
+ *
+ * The in-kernel equivalent of fcntl(F_SET_RW_HINT).
+ *
+ * Return: 0 on success, -EINVAL if @hint is not a valid hint.
+ */
+__bpf_kfunc int bpf_file_set_write_hint(struct file *file, u64 hint)
+{
+	if (hint >= WRITE_LIFE_HINT_NR)
+		return -EINVAL;
+
+	WRITE_ONCE(file_inode(file)->i_write_hint, hint);
+	return 0;
+}
+
 __bpf_kfunc_end_defs();

 BTF_KFUNCS_START(bpf_fs_kfunc_set_ids)
@@ -509,6 +527,7 @@ BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_real_data_inode, KF_SLEEPABLE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_file_set_write_hint)
 BTF_ID_FLAGS(func, bpf_inode_init_xattr)
 #ifdef CONFIG_NET
 BTF_ID_FLAGS(func, bpf_sock_read_xattr, KF_RCU)
@@ -519,6 +538,7 @@ BTF_KFUNCS_END(bpf_fs_kfunc_set_ids)
 BTF_SET_START(bpf_fs_kfunc_lsm_only_ids)
 BTF_ID(func, bpf_set_dentry_xattr)
 BTF_ID(func, bpf_remove_dentry_xattr)
+BTF_ID(func, bpf_file_set_write_hint)
 BTF_SET_END(bpf_fs_kfunc_lsm_only_ids)

 BTF_ID_LIST_SINGLE(bpf_inode_init_xattr_ids, func, bpf_inode_init_xattr)

pw-bot: cr

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-12-01  4:56 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  4:34 [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Naohiro Aota
2026-12-01  4:34 ` [PATCH 1/6] fs: add fs_dp BPF struct_ops for file data placement Naohiro Aota
2026-12-01  4:45   ` sashiko-bot
2026-12-01  4:34 ` [PATCH 2/6] bpf: allow fs_dp struct_ops programs to call FS kfuncs Naohiro Aota
2026-12-01  4:34 ` [PATCH 3/6] xfs: add BPF data placement hook at file open Naohiro Aota
2026-12-01  4:49   ` sashiko-bot
2026-12-01  4:34 ` [PATCH 4/6] xfs: add ioctls to select a BPF data placement policy Naohiro Aota
2026-12-01  4:34 ` [PATCH 5/6] xfs: bind a BPF data placement policy to a project ID Naohiro Aota
2026-12-01  4:34 ` [PATCH 6/6] xfs: let XFS_IOC_{SET,GET}_DP_OPS act on " Naohiro Aota
2026-12-01  4:37 ` [PATCH 1/2] fs_dp: let a policy pick a write stream Naohiro Aota
2026-12-01  4:37   ` [PATCH 2/2] xfs: bind write streams along with a data placement policy Naohiro Aota
2026-12-01  4:56 ` [PATCH 0/6] xfs: BPF data placement policy for zoned file systems Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox