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 --- 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 +#include +#include + +#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 + +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 #include @@ -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