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