[PATCH 2/2] xfs: bind write streams along with a data placement policy

From: Naohiro Aota

Date: Mon Nov 30 2026 - 23:37:41 EST


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