[PATCH 1/6] fs: add fs_dp BPF struct_ops for file data placement

From: Naohiro Aota

Date: Mon Nov 30 2026 - 23:35:28 EST


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@xxxxxxx>
---
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