From: Anuj Gupta Give xfs write streams on any block device: keep a stream pool in the data device buftarg, sized from the AG count, and pass the FS_IOC_WRITE_STREAM_* ioctls to write_stream_ioctl() with a callback that returns this pool. Write streams and filestreams are mutually exclusive: filestream inodes get no pool, and setattr refuses to turn on filestream placement, or to change the realtime flag, while a stream is attached. Realtime inodes get no pool for now. Suggested-by: Christoph Hellwig Suggested-by: Darrick J. Wong Signed-off-by: Anuj Gupta Signed-off-by: Kanchan Joshi --- fs/xfs/xfs_buf.c | 31 +++++++++++++++++++++++++++++++ fs/xfs/xfs_buf.h | 6 ++++++ fs/xfs/xfs_ioctl.c | 26 ++++++++++++++++++++++++++ fs/xfs/xfs_super.c | 5 +++++ 4 files changed, 68 insertions(+) diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c index 6c93b4f5629c..d8ce03a9f49c 100644 --- a/fs/xfs/xfs_buf.c +++ b/fs/xfs/xfs_buf.c @@ -1648,6 +1648,7 @@ void xfs_free_buftarg( struct xfs_buftarg *btp) { + write_stream_pool_destroy(&btp->bt_stream_pool); xfs_destroy_buftarg(btp); fs_put_dax(btp->bt_daxdev, btp->bt_mount); /* the main block device is closed by kill_block_super */ @@ -1685,6 +1686,36 @@ xfs_configure_buftarg_atomic_writes( btp->bt_awu_max = max_bytes; } +/* the block layer can tag I/O with at most U8_MAX write streams */ +#define XFS_MAX_WRITE_STREAMS U8_MAX + +/* Heuristic to derive software write stream count from a group topology */ +static unsigned int +xfs_sw_write_stream_count( + unsigned int nr_groups) +{ + unsigned int group_set_size; + + if (nr_groups >= 16) + group_set_size = 4; + else if (nr_groups >= 8) + group_set_size = 2; + else + group_set_size = 1; + return min(nr_groups / group_set_size, XFS_MAX_WRITE_STREAMS); +} + +int +xfs_buftarg_init_streams( + struct xfs_buftarg *btp, + unsigned int nr_groups) +{ + unsigned int nr_streams; + + nr_streams = xfs_sw_write_stream_count(nr_groups); + return write_stream_pool_init(&btp->bt_stream_pool, nr_streams); +} + /* Configure a buffer target that abstracts a block device. */ int xfs_configure_buftarg( diff --git a/fs/xfs/xfs_buf.h b/fs/xfs/xfs_buf.h index a4729253b56f..b5292e5731e3 100644 --- a/fs/xfs/xfs_buf.h +++ b/fs/xfs/xfs_buf.h @@ -15,6 +15,7 @@ #include #include #include +#include extern struct kmem_cache *xfs_buf_cache; @@ -102,6 +103,9 @@ struct xfs_buftarg { unsigned int bt_awu_min; unsigned int bt_awu_max; + /* slot pool for stream fds on this device */ + struct write_stream_pool bt_stream_pool; + struct rhashtable bt_hash; }; @@ -360,6 +364,8 @@ extern void xfs_buftarg_wait(struct xfs_buftarg *); extern void xfs_buftarg_drain(struct xfs_buftarg *); int xfs_configure_buftarg(struct xfs_buftarg *btp, unsigned int sectorsize, xfs_fsblock_t nr_blocks); +int xfs_buftarg_init_streams(struct xfs_buftarg *btp, + unsigned int nr_groups); #define xfs_readonly_buftarg(buftarg) bdev_read_only((buftarg)->bt_bdev) diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c index f81b6e52ac40..48195143b812 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_filestream.h" #include #include @@ -557,6 +558,11 @@ xfs_ioctl_setattr_xflags( bool rtflag = (fa->fsx_xflags & FS_XFLAG_REALTIME); uint64_t i_flags2; + /* refuse a filestream/realtime flag change while a stream is attached */ + if (READ_ONCE(VFS_I(ip)->i_write_stream) && + ((fa->fsx_xflags & FS_XFLAG_FILESTREAM) || rtflag)) + return -EINVAL; + if (rtflag != XFS_IS_REALTIME_INODE(ip)) { /* Can't change realtime flag if any extents are allocated. */ if (xfs_inode_has_filedata(ip)) @@ -1436,6 +1442,21 @@ xfs_ioc_free_eofblocks( return xfs_blockgc_free_space(mp, &icw); } +/* Pool for the data of @inode, or NULL if it cannot use write streams. */ +static struct write_stream_pool * +xfs_write_stream_pool( + struct inode *inode) +{ + struct xfs_inode *ip = XFS_I(inode); + struct write_stream_pool *pool = NULL; + + xfs_ilock(ip, XFS_ILOCK_SHARED); + if (!xfs_inode_is_filestream(ip) && !XFS_IS_REALTIME_INODE(ip)) + pool = &ip->i_mount->m_ddev_targp->bt_stream_pool; + xfs_iunlock(ip, XFS_ILOCK_SHARED); + return pool; +} + /* * These long-unused ioctls were removed from the official ioctl API in 5.17, * but retain these definitions so that we can log warnings about them. @@ -1575,6 +1596,11 @@ xfs_file_ioctl( case XFS_IOC_VERIFY_MEDIA: return xfs_ioc_verify_media(filp, arg); + case FS_IOC_WRITE_STREAM_GET_MAX: + case FS_IOC_WRITE_STREAM_ALLOC: + case FS_IOC_WRITE_STREAM_SET: + return write_stream_ioctl(filp, cmd, arg, xfs_write_stream_pool); + default: return -ENOTTY; } diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c index 5a06132aa384..a541e97b580f 100644 --- a/fs/xfs/xfs_super.c +++ b/fs/xfs/xfs_super.c @@ -608,6 +608,11 @@ xfs_setup_devices( if (error) return error; + error = xfs_buftarg_init_streams(mp->m_ddev_targp, + mp->m_sb.sb_agcount); + if (error) + return error; + if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) { unsigned int log_sector_size = BBSIZE; -- 2.25.1