write streams and xfs support
A message that is not in the archive (sent off-list or not yet received): CGME20261125083532epcas5p452c938702f2d2e1c4ac4e3efce23e39c@epcas5p4.samsung.com
v8 cover write streams and xfs support
This series introduces a generic interface [2,3] for write stream management on files.
It enables spatial isolation (at sw and hw level) and concurrency improvments [1] in xfs by
(a) steering each write stream to its own set of allocation groups (patch #6) and realtime groups (patch #7).
(b) connecting xfs write streams to block write streams (FDP capable NVMe).
Write streams allow the abstraction provider (fs, block, raid etc.) to leverage application's intent (file relationships/lifecycle).
- application: reserves a stream and sets it on the files it wants placed together.
- xfs: maps streams to AGs/RGs; allocates without interleaving; gains higher concurrency due to reduced lock contention.
- hardware: maps streams to underlying allocation unit; reduces device internal write amplification, improved life, predictable QoS.
Also
- A stream is handed out using an fd. The filesystem is free to map streams onto its own geometry, and the reservation/fd machinery is generic (fs/write_streams.c) so other filesystems can reuse it.
- Since high-level write stream (in xfs) and logical placement can work without the low-level write streams (in block device), series has a general value beyond the hardware that provides spatial isolation. Patches 1-8 are software only and work on any block device; patch 9 aligns the stream count to the hardware streams when the device has them.
- write-stream is different from existing 'filestream' allocator which maintains directory-to-AG associations in a global MRU cache. That requires state managment and memory (and its reclaim). Proposed AG-set based steering relies on simple, statless/lockless airthmatic that aligns more with the default allocator heuristics.
[1]
### Benchmarks
1. On regular NVMe
a. Inter-stream concurrency
---------------------------
fio: 4k write, direct IO, 16 jobs, 1 directory, 16 files * 8GiB, iodepth 32
xfs: 16 AGs, 4 write-streams
base: 41 KIOPS
write-stream AG-set: 227 KIOPS (+453%)
here, 16 files are assigned 4 distinct write-streams (4 files/stream)
b. Intra-stream concurrency
----------------------------
fio: 4k write, direct IO, 4 jobs, 1 directory, 4 files * 8GiB, iodepth 32
xfs: 16 AGs, 4 write-streams, write-stream AG-set size = 4
base: 59 KIOPS
write-stream AG-set: 112 KIOPS (+89%)
here, 4 files are assigned single write-stream
2. On FDP-capable NVMe:
RocksDB YCSB
WAF (base vs write-stream): 35% Reduction
base does not use write life time hints; on non-zoned xfs they change neither the allocation nor what the device sees, as the NVMe driver places FDP data by write stream only.
3. On Zoned Backend:
Hans has posted zoned xfs suppport and benchmarks [5] on top of this interface. It needs no changes to the interface.
[2]
### Application interface
Three new ioctls:
FS_IOC_WRITE_STREAM_GET_MAX number of streams the filesystem offers
FS_IOC_WRITE_STREAM_ALLOC reserve a stream, returns fd for that
FS_IOC_WRITE_STREAM_SET set stream, given its fd, on a fileALLOC requires CAP_SYS_ADMIN. A per-user limit on the number of streams, suggested by Christoph [4], is the planned follow-up to relax this; it does not change the uapi.
### Comparison with Write Hints (RWH_WRITE_LIFE_*)
- Semantics: Write Hints describe 'data temperature' (e.g.,short/long/extreme), implying a lifetime. Write Streams describe 'data placement' (e.g., Bin 1/Bin 2), implying only separation.
- Scalability: Write Hints are limited to a small, fixed enum (6 values). Write streams are dynamic, provider-dependent values that can scale much higher.
- Discovery: The existing write-hint interface is advisory and decoupled from underlying capabilties; application has no way to probe support and cannot deterministically know which hints are valid. OTOH, write-streams provide explicit discovery.
- Usage model: application needs to get a handle (fd) for a write stream before being able to use it. This avoids two unrelated applications ending up on the same stream.
Note: within the kernel, the separation between two constructs (write-hint and write-stream) had started from 6.16 itself.
### Changelog
since v7:
https://lore.kernel.org/linux-xfs/[email protected]/
- handle the three ioctls in common code: new patch 4 (Hans)
- make directory with realtime inherit flag report and allocate from the realtime pool (Hans)
- refuse FS_IOC_WRITE_STREAM_SET on an immutable file with EPERM, as F_SET_RW_HINT does.
- honor stream AG based steering for a file on inode32 mounts, earlier the allocator rotor used to override it (Sashiko)
- rebase on vfs-7.5.misc
Hans has posted zoned xfs support on top of this series [5]; it needs no change to the generic interface.
since v6:
https://lore.kernel.org/linux-xfs/[email protected]/
- add a VFS helper for write-stream/write-hint exclusion (Darrick)
- use WRITE_STREAM_NOT_SET for an unset stream (Darrick)
- do not pass software stream ids to devices without hardware streams (Sashiko)
- rebase on 7.4-rc1
since v5:
https://lore.kernel.org/linux-xfs/[email protected]/
Fixes for issues found by Sashiko review; no changes in UAPI
- fix 32-bit build: use do_div() for the inode-number hash in AG and rtgroup selection
- bound the stream pool to U16_MAX, the range of the stream id
- cap the hardware stream count at U8_MAX, the most streams the block layer can tag
- tag the iomap_dio_zero() bio with the write stream, so sub-block direct writes are not split across streams
- document that the stream pool must outlive the mount a stream pins; kernel-doc fixes
- rebase on 7.3-rc7
since v4:
https://lore.kernel.org/linux-xfs/[email protected]/
- stream reservation and fd handling moved to generic code, and the stream id moved to struct inode from xfs inode (Darrick, Christoph)
- stream id no longer shows in uapi: OPEN becomes ALLOC and returns fd for any (unreserved) stream; OPEN_EXACT and GET are gone (Christoph)
- ALLOC requires CAP_SYS_ADMIN (Christoph)
- write stream and write life time hint are mutually exclusive at both setters
- stream pool lives on the buftarg (Christoph), which gives the realtime device streams of its own: new patch to enable RT support (Christoph, Darrick)
- software streams come first and work on any device; hardware streams are enabled in the last (Christoph)
since v3:
https://lore.kernel.org/linux-block/[email protected]/
- add fd-based interface to open/set the write stream (Christoph)
- move from single multiplexed ioctl to 4 distinct ioctls (Christoph)
- add mutual exclusion checks against existing write-hint, filestream (Christoph)
- uint16_t for write-stream within iomap and other streamlining (Darrick)
since v2:
https://lore.kernel.org/linux-fsdevel/[email protected]/
- xfs default allocator optimization using fixed-size generic AG set (Dave)
- reuse the above to simplify the write-stream AG set handling
- streamline the uapi; Use union for GET_MAX and GET/SET (Darrick)
- uint16_t for write-stream within xfs inode and other cleanups (Darrick)
since v1:
https://lore.kernel.org/linux-fsdevel/[email protected]/
- swich from fcntl based to ioctl-based interface (Christian)
- new patch (#4) that makes xfs allocator use the write streams for AG selection
- new patch (#5) that introduces software write streams in xfs.
[3]
### Interface example
/*
* Minimal write-stream example.
*
* cc -Wall -o ws-example ws-example.c * sudo ./ws-example /mnt/xfs # data device * sudo ./ws-example /mnt/xfs rt # realtime volume
*
* Reserves two streams. a.dat gets the first; b.dat and c.dat share the
* second. Each file then takes one 4K write, so a.dat is placed apart from
* b.dat and c.dat, which are placed together.
*
* With "rt" the files are made realtime first.
*/
#include <sys/ioctl.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/fs.h>
struct fs_write_stream_set {
__s32 stream_fd; /* IN: stream to set, -1 to clear */
__u32 flags; /* IN: FS_WRITE_STREAM_SET_* */};
#define FS_WRITE_STREAM_SET_CLEAR (1U << 0)
#define FS_IOC_WRITE_STREAM_GET_MAX _IOR(0x15, 3, __u32) #define FS_IOC_WRITE_STREAM_ALLOC _IO(0x15, 4) /* returns an fd */ #define FS_IOC_WRITE_STREAM_SET _IOW(0x15, 5, struct fs_write_stream_set)
static char buf[4096];
/* create @name; with @rt, put it on the realtime volume */ static int create_file(const char *dir, const char *name, int rt)
{
struct fsxattr fa;
char path[512];
int fd;
snprintf(path, sizeof(path), "%s/%s", dir, name);
fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644);
if (fd < 0)
err(1, "%s", path);
if (!rt)
return fd;
if (ioctl(fd, FS_IOC_FSGETXATTR, &fa))
err(1, "FSGETXATTR %s", name);
fa.fsx_xflags |= FS_XFLAG_REALTIME;
if (ioctl(fd, FS_IOC_FSSETXATTR, &fa))
err(1, "FSSETXATTR %s (no realtime volume?)", name);
return fd;
}/* set @stream_fd on @fd, write 4K */
static void write_on_stream(int fd, const char *name, int stream_fd)
{
struct fs_write_stream_set set = { .stream_fd = stream_fd, .flags = 0 };
if (ioctl(fd, FS_IOC_WRITE_STREAM_SET, &set))
err(1, "SET %s", name);
if (write(fd, buf, sizeof(buf)) != sizeof(buf))
err(1, "write %s", name);
fsync(fd);
close(fd);
}int main(int argc, char **argv)
{
__u32 max = 0;
int rt, fa, fb, fc, s1, s2;
if (argc < 2 || argc > 3 || (argc == 3 && strcmp(argv[2], "rt")))
errx(2, "usage: %s <dir-on-xfs> [rt]", argv[0]);
rt = argc == 3;
fa = create_file(argv[1], "a.dat", rt);
fb = create_file(argv[1], "b.dat", rt);
fc = create_file(argv[1], "c.dat", rt);
/* query and reserve through a file on the device being placed on */
if (ioctl(fa, FS_IOC_WRITE_STREAM_GET_MAX, &max))
err(1, "GET_MAX");
printf("max streams: %u\n", max);
if (max < 2)
errx(1, "need at least 2 streams");
/* ALLOC returns the stream fd as the ioctl return value */
s1 = ioctl(fa, FS_IOC_WRITE_STREAM_ALLOC);
if (s1 < 0)
err(1, "ALLOC");
s2 = ioctl(fa, FS_IOC_WRITE_STREAM_ALLOC);
if (s2 < 0)
err(1, "ALLOC");
write_on_stream(fa, "a.dat", s1);
write_on_stream(fb, "b.dat", s2);
write_on_stream(fc, "c.dat", s2);
/* closing a stream fd releases the stream */
close(s1);
close(s2);
puts("a.dat on stream 1; b.dat and c.dat on stream 2");
puts("confirm AG/RG steering using xfs_bmap");
return 0;
}[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/linux-xfs/[email protected]/
Anuj Gupta (4):
fs: add write-stream management ioctls
fs: add generic write-stream management
xfs: implement software write-stream management
xfs: support write streams on realtime volumes
Kanchan Joshi (5):
fs: add i_write_stream, exclusive with the write life time hint
fs: handle the write-stream ioctls in common code
xfs: write stream based AG placement
iomap: introduce and propagate write_stream
xfs: support hardware write streams
fs/Makefile | 2 +- fs/fcntl.c | 7 + fs/inode.c | 2 + fs/internal.h | 9 ++ fs/iomap/direct-io.c | 2 + fs/iomap/ioend.c | 3 + fs/write_streams.c | 279 ++++++++++++++++++++++++++++++++++ fs/xfs/libxfs/xfs_bmap.c | 61 +++++++- fs/xfs/xfs_buf.c | 37 +++++ fs/xfs/xfs_buf.h | 6 + fs/xfs/xfs_ioctl.c | 42 +++++ fs/xfs/xfs_iomap.c | 5 + fs/xfs/xfs_rtalloc.c | 28 ++++ fs/xfs/xfs_super.c | 12 ++ include/linux/fs.h | 2 +- include/linux/iomap.h | 1 + include/linux/write_streams.h | 38 +++++ include/uapi/linux/fs.h | 15 ++ 18 files changed, 547 insertions(+), 4 deletions(-) create mode 100644 fs/write_streams.c create mode 100644 include/linux/write_streams.h
Signature · 1 line
2.25.1
PATCH v8 1/9 fs: add write-stream management ioctls
From: Anuj Gupta <[email protected]>
Add three ioctls for write stream management:
FS_IOC_WRITE_STREAM_GET_MAX number of streams the filesystem offers FS_IOC_WRITE_STREAM_ALLOC reserve a stream, returns an fd FS_IOC_WRITE_STREAM_SET set or clear a stream on a file
Write streams are a resource the kernel manages and applications ask for.
ALLOC reserves a stream and returns a stream fd; the stream stays reserved as long as that fd is open. ALLOC fails once all streams are reserved.
SET assigns the stream named by the stream_fd in struct fs_write_stream_set to an open file; the same stream fd can be used to set the stream on any number of files. SET with FS_WRITE_STREAM_SET_CLEAR and a stream_fd of -1 clears whatever stream the file carries.
--- include/uapi/linux/fs.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
include/uapi/linux/fs.h +15 -0
@@ -354,6 +354,21 @@ struct file_attr {/* Get logical block metadata capability details */ #define FS_IOC_GETLBMD_CAP _IOWR(0x15, 2, struct logical_block_metadata_cap) +struct fs_write_stream_set { +__s32 stream_fd;/* IN: stream to attach, -1 to clear */ +__u32 flags;/* IN: FS_WRITE_STREAM_SET_* */ +}; + +/* Clear the file's write stream. Requires stream_fd to be -1. */ +#define FS_WRITE_STREAM_SET_CLEAR (1U << 0) + +/* GET_MAX returns the count through its argument; ALLOC returns a stream fd +* as the ioctl return value and takes none. +*/ +#define FS_IOC_WRITE_STREAM_GET_MAX _IOR(0x15, 3, __u32) +#define FS_IOC_WRITE_STREAM_ALLOC _IO(0x15, 4) +#define FS_IOC_WRITE_STREAM_SET _IOW(0x15, 5, struct fs_write_stream_set) +/* * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS) *
Signature · 1 line
2.25.1
Re: [PATCH v8 1/9] fs: add write-stream management ioctls
Quoted 8 lines · Kanchan Joshi
From: Anuj Gupta <[email protected]>
Add three ioctls for write stream management:
FS_IOC_WRITE_STREAM_GET_MAX number of streams the filesystem offers FS_IOC_WRITE_STREAM_ALLOC reserve a stream, returns an fd FS_IOC_WRITE_STREAM_SET set or clear a stream on a file
Quoted 6 lines
Write streams are a resource the kernel manages and applications ask for.
ALLOC reserves a stream and returns a stream fd; the stream stays reserved as long as that fd is open. ALLOC fails once all streams are reserved.
We'll need proper UABI documentation for this, and I think it's worth starting to draft it now. Some things that need to be clear:
- Separation from other applications is only provided while the stream fd is held. Once it is closed, the stream may be handed out again by the next ALLOC.
- Closing the stream fd does not detach the stream from the files it was set on. For example: if a file is assigned a stream and the stream fd is closed without fsyncing the written data, there are no guarantees for separation. The same applies to anything written to the file after the stream fd has been closed, so an application needs to keep the stream fd open while writing, or fsync and clear the files before closing it.
Quoted 18 lines
SET assigns the stream named by the
stream_fdin structfs_write_stream_setto an open file; the same stream fd can be used to set the stream on any number of files. SET withFS_WRITE_STREAM_SET_CLEARand astream_fdof -1 clears whatever stream the file carries.Suggested-by Christoph Hellwig <[email protected]>Signed-off-by Anuj Gupta <[email protected]>Signed-off-by Kanchan Joshi <[email protected]>--- include/uapi/linux/fs.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)include/uapi/linux/fs.h +0
-0
include/uapi/linux/fs.h struct file_attr { · line 358
Quoted 5 lines
include/uapi/linux/fs.h +2
-0@@ -354,6 +354,21 @@ struct file_attr {/* Get logical block metadata capability details */ #define FS_IOC_GETLBMD_CAP _IOWR(0x15, 2, struct logical_block_metadata_cap) +struct fs_write_stream_set { +__s32 stream_fd;/* IN: stream to attach, -1 to clear */
"-1 when clearing" would be better I think, as -1 alone is not enough (FS_WRITE_STREAM_SET_CLEAR needs to be used), e.g.:
__s32 stream_fd; /* IN: stream fd, -1 when clearing */
Quoted 16 lines
include/uapi/linux/fs.h +13
-0+__u32 flags;/* IN: FS_WRITE_STREAM_SET_* */ +}; + +/* Clear the file's write stream. Requires stream_fd to be -1. */ +#define FS_WRITE_STREAM_SET_CLEAR (1U << 0) + +/* GET_MAX returns the count through its argument; ALLOC returns a stream fd +* as the ioctl return value and takes none. +*/ +#define FS_IOC_WRITE_STREAM_GET_MAX _IOR(0x15, 3, __u32) +#define FS_IOC_WRITE_STREAM_ALLOC _IO(0x15, 4) +#define FS_IOC_WRITE_STREAM_SET _IOW(0x15, 5, struct fs_write_stream_set) +/* * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS) *
PATCH v8 2/9 fs: add generic write-stream management
In reply to Kanchan JoshiFrom: Anuj Gupta <[email protected]>
Add a bitmap-based stream pool and anonymous fds for reserved streams. Helpers initialize and destroy a pool, reserve a stream as an fd, and resolve a stream fd back to its id.
--- fs/Makefile | 2 +- fs/internal.h | 8 ++ fs/write_streams.c | 142 ++++++++++++++++++++++++++++++++++ include/linux/write_streams.h | 29 +++++++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 fs/write_streams.c create mode 100644 include/linux/write_streams.h
fs/Makefile +1 -1
@@ -23,7 +23,7 @@ obj-$(CONFIG_PROC_FS) += proc_namespace.o obj-$(CONFIG_LEGACY_DIRECT_IO) += direct-io.o obj-y += notify/ obj-$(CONFIG_EPOLL) += eventpoll.o -obj-y += anon_inodes.o +obj-y += anon_inodes.o write_streams.o obj-$(CONFIG_SIGNALFD) += signalfd.o obj-$(CONFIG_TIMERFD) += timerfd.o obj-$(CONFIG_EVENTFD) += eventfd.o
fs/internal.h +8 -0
@@ -368,3 +368,11 @@ void failfs_get_root(struct path *path);void __init failfs_init (void );bool failfs_mnt (const struct vfsmount *mnt);int failfs_current_chdir (void ); + +/* +* fs/write_streams.c +*/ +struct write_stream_pool ; +int write_stream_alloc_fd (struct write_stream_pool *pool,struct file *file); +int write_stream_get_id (struct file *file, +const struct write_stream_pool *pool);
fs/write_streams.c new file +142 -0
142 lines not on this page · raw message
include/linux/write_streams.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_WRITE_STREAMS_H +#define _LINUX_WRITE_STREAMS_H + +#include <linux/spinlock.h> +#include <linux/types.h> + +struct file ; + +/* +* Write stream slot pool, embedded in whatever filesystem object scopes +* an id space. +*/ +struct write_stream_pool { +unsigned int nr_streams; +unsigned long *streams_in_use; +spinlock_t lock; +}; + +static inline unsigned int +write_stream_pool_count (const struct write_stream_pool *pool) +{ +return pool->nr_streams; +} + +int write_stream_pool_init (struct write_stream_pool *pool,unsigned int nr); +void write_stream_pool_destroy (struct write_stream_pool *pool); + +#endif /* _LINUX_WRITE_STREAMS_H */
Signature · 1 line
2.25.1
Re: [PATCH v8 2/9] fs: add generic write-stream management
Quoted 129 lines · Kanchan Joshi
From: Anuj Gupta <[email protected]>
Add a bitmap-based stream pool and anonymous fds for reserved streams. Helpers initialize and destroy a pool, reserve a stream as an fd, and resolve a stream fd back to its id.
Suggested-by Christoph Hellwig <[email protected]>Suggested-by Darrick J. Wong <[email protected]>Signed-off-by Anuj Gupta <[email protected]>Signed-off-by Kanchan Joshi <[email protected]>--- fs/Makefile | 2 +- fs/internal.h | 8 ++ fs/write_streams.c | 142 ++++++++++++++++++++++++++++++++++ include/linux/write_streams.h | 29 +++++++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 fs/write_streams.c create mode 100644 include/linux/write_streams.hfs/Makefile +1
-1@@ -23,7 +23,7 @@ obj-$(CONFIG_PROC_FS) += proc_namespace.o obj-$(CONFIG_LEGACY_DIRECT_IO) += direct-io.o obj-y += notify/ obj-$(CONFIG_EPOLL) += eventpoll.o -obj-y += anon_inodes.o +obj-y += anon_inodes.o write_streams.o obj-$(CONFIG_SIGNALFD) += signalfd.o obj-$(CONFIG_TIMERFD) += timerfd.o obj-$(CONFIG_EVENTFD) += eventfd.ofs/internal.h +8
-0@@ -368,3 +368,11 @@ void failfs_get_root(struct path *path);void __init failfs_init (void );bool failfs_mnt (const struct vfsmount *mnt);int failfs_current_chdir (void ); + +/* +* fs/write_streams.c +*/ +struct write_stream_pool ; +int write_stream_alloc_fd (struct write_stream_pool *pool,struct file *file); +int write_stream_get_id (struct file *file, +const struct write_stream_pool *pool);fs/write_streams.c new file +75
-0@@ -0,0 +1,142 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Generic write-stream fd management. */ +#include <linux/anon_inodes.h> +#include <linux/bitmap.h> +#include <linux/err.h> +#include <linux/file.h> +#include <linux/fs.h> +#include <linux/mount.h> +#include <linux/slab.h> +#include <linux/write_streams.h> + +#include "internal.h" + +struct write_stream { +struct write_stream_pool *pool; +struct vfsmount *mnt;/* pins the mount */ +u16 id;/* 1-based slot number */ +}; + +static int write_stream_release (struct inode *inode,struct file *file) +{ +struct write_stream *ws = file->private_data; + +/* ws->mnt keeps the pool alive until the mntput() below */ +spin_lock (&ws->pool->lock); +__clear_bit (ws->id -1 , ws->pool->streams_in_use); +spin_unlock (&ws->pool->lock); +mntput (ws->mnt); +kfree (ws); +return 0 ; +} + +static const struct file_operations write_stream_fops = { + .release = write_stream_release, + .llseek = noop_llseek, +}; + +/** +* write_stream_pool_init - size a write stream pool +* @pool: pool to initialise +* @nr: number of slots, may be zero, at most U16_MAX +* +* Return: 0, -EINVAL if @nr is too large, or -ENOMEM if the bitmap cannot be +* allocated. +*/ +int write_stream_pool_init (struct write_stream_pool *pool,unsigned int nr) +{ +spin_lock_init (&pool->lock); + pool->streams_in_use =NULL ; + pool->nr_streams =0 ; + +if (nr >U16_MAX ) +return -EINVAL ; +if (!nr) +return 0 ; + + pool->streams_in_use =bitmap_zalloc (nr,GFP_KERNEL ); +if (!pool->streams_in_use) +return -ENOMEM ; + pool->nr_streams = nr; +return 0 ; +} +EXPORT_SYMBOL_GPL (write_stream_pool_init); + +/** +* write_stream_pool_destroy - free a write stream pool +* @pool: pool to release +*/ +void write_stream_pool_destroy (struct write_stream_pool *pool) +{ +bitmap_free (pool->streams_in_use); + pool->streams_in_use =NULL ; + pool->nr_streams =0 ; +} +EXPORT_SYMBOL_GPL (write_stream_pool_destroy);
fs/write_streams.c line 85
Quoted 10 lines
fs/write_streams.c new file +10
-0+ +/** +* write_stream_alloc_fd - reserve a slot and return an fd naming it +* @pool: pool to reserve from; must live as long as the mount of @file +* @file: file whose mount is pinned for the life of the stream +* +* Return: an O_RDONLY | O_CLOEXEC fd, -EOPNOTSUPP if the pool has no slots, +* or -EBUSY if they are all taken. +*/ +int write_stream_alloc_fd (struct write_stream_pool *pool,struct file *file)
Only users in this file, so this should be static.
Quoted 37 lines
fs/write_streams.c new file +37
-0+{ +struct write_stream *ws; +unsigned int slot; +int fd; + +if (!pool->nr_streams) +return -EOPNOTSUPP ; + + ws =kzalloc_obj (*ws,GFP_KERNEL ); +if (!ws) +return -ENOMEM ; + +spin_lock (&pool->lock); + slot =find_first_zero_bit (pool->streams_in_use, pool->nr_streams); +if (slot >= pool->nr_streams) { +spin_unlock (&pool->lock); +kfree (ws); +return -EBUSY ; + } +__set_bit (slot, pool->streams_in_use); +spin_unlock (&pool->lock); + + ws->pool = pool; + ws->mnt =mntget (file->f_path.mnt); + ws->id = slot +1 ; + + fd =anon_inode_getfd ("[write_stream]" , &write_stream_fops, ws, +O_RDONLY |O_CLOEXEC ); +if (fd <0 ) { +spin_lock (&pool->lock); +__clear_bit (slot, pool->streams_in_use); +spin_unlock (&pool->lock); +mntput (ws->mnt); +kfree (ws); + } +return fd; +}
fs/write_streams.c line 132
Quoted 10 lines
fs/write_streams.c new file +10
-0+ +/** +* write_stream_get_id - the id a stream file names +* @file: candidate stream file +* @pool: pool the stream must belong to, or NULL to accept any +* +* Return: the 1-based id, or -EINVAL if @file is not a stream file, or is +* a stream of a different pool. +*/ +int write_stream_get_id (struct file *file,const struct write_stream_pool *pool)
Same here, should be static. And *pool == NULL is not used anywhere
Quoted 45 lines
fs/write_streams.c new file +10
-0+{ +struct write_stream *ws; + +if (file->f_op != &write_stream_fops) +return -EINVAL ; + ws = file->private_data; +if (pool && ws->pool != pool) +return -EINVAL ; +return ws->id; +}include/linux/write_streams.h new file +29
-0@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_WRITE_STREAMS_H +#define _LINUX_WRITE_STREAMS_H + +#include <linux/spinlock.h> +#include <linux/types.h> + +struct file ; + +/* +* Write stream slot pool, embedded in whatever filesystem object scopes +* an id space. +*/ +struct write_stream_pool { +unsigned int nr_streams; +unsigned long *streams_in_use; +spinlock_t lock; +}; + +static inline unsigned int +write_stream_pool_count (const struct write_stream_pool *pool) +{ +return pool->nr_streams; +} + +int write_stream_pool_init (struct write_stream_pool *pool,unsigned int nr); +void write_stream_pool_destroy (struct write_stream_pool *pool); + +#endif /* _LINUX_WRITE_STREAMS_H */
PATCH v8 3/9 fs: add i_write_stream, exclusive with the write life time hint
In reply to Kanchan JoshiKeep the attached stream id in the VFS inode rather than the filesystem's own, so that fcntl_set_rw_hint() can see it. It is a u16 in the 32-bit hole after i_state, so struct inode does not grow on 64-bit.
Add inode_set_write_stream() to set or clear it. It refuses a stream while a write life time hint is set, under i_lock as fcntl_set_rw_hint() does, so both sides of the exclusion are enforced in the VFS.
--- fs/fcntl.c | 7 +++++++ fs/inode.c | 2 ++ fs/internal.h | 1 + fs/write_streams.c | 22 ++++++++++++++++++++++ include/linux/fs.h | 2 +- include/linux/write_streams.h | 3 +++ 6 files changed, 36 insertions(+), 1 deletion(-)
fs/fcntl.c +7 -0
@@ -383,7 +383,14 @@ static long fcntl_set_rw_hint(struct file *file, unsigned long arg)if (!rw_hint_valid (hint))return -EINVAL ; +/* keep write stream and write life time hint mutually exclusive */ +spin_lock (&inode->i_lock); +if (hint !=WRITE_LIFE_NOT_SET &&READ_ONCE (inode->i_write_stream)) { +spin_unlock (&inode->i_lock); +return -EBUSY ; + }WRITE_ONCE (inode->i_write_hint, hint); +spin_unlock (&inode->i_lock);/* * file->f_mapping->host may differ from inode. As an example,
fs/inode.c +2 -0
@@ -21,6 +21,7 @@#include <linux/list_lru.h> #include <linux/iversion.h> #include <linux/rw_hint.h> +#include <linux/write_streams.h> #include <linux/seq_file.h> #include <linux/debugfs.h> #include <trace/events/writeback.h>
@@ -246,6 +247,7 @@ int inode_init_always_gfp(struct super_block *sb, struct inode *inode, gfp_t gfpatomic_set (&inode->i_writecount,0 ); inode->i_size =0 ; inode->i_write_hint =WRITE_LIFE_NOT_SET ; + inode->i_write_stream =WRITE_STREAM_NOT_SET ; inode->i_blocks =0 ; inode->i_bytes =0 ; inode->i_generation =0 ;
fs/internal.h +1 -0
@@ -376,3 +376,4 @@ struct write_stream_pool;int write_stream_alloc_fd (struct write_stream_pool *pool,struct file *file);int write_stream_get_id (struct file *file,const struct write_stream_pool *pool); +int inode_set_write_stream (struct inode *inode,u16 id);
fs/write_streams.c +22 -0
@@ -140,3 +140,25 @@ int write_stream_get_id(struct file *file, const struct write_stream_pool *pool)return -EINVAL ;return ws->id; } + +/** +* inode_set_write_stream - attach or clear the write stream of an inode +* @inode: inode to update +* @id: stream id, or WRITE_STREAM_NOT_SET to clear +* +* A write stream and a write life time hint are mutually exclusive. +* +* Return: 0, or -EBUSY if @inode has a write life time hint. +*/ +int inode_set_write_stream (struct inode *inode,u16 id) +{ +spin_lock (&inode->i_lock); +if (id !=WRITE_STREAM_NOT_SET && + inode->i_write_hint !=WRITE_LIFE_NOT_SET ) { +spin_unlock (&inode->i_lock); +return -EBUSY ; + } +WRITE_ONCE (inode->i_write_stream, id); +spin_unlock (&inode->i_lock); +return 0 ; +}
include/linux/fs.h +1 -1
@@ -812,7 +812,7 @@ struct inode {/* Misc */ struct inode_state_flags i_state; -/* 32-bit hole */ +u16 i_write_stream;struct rw_semaphore i_rwsem;/* Timestamp for writeback list ordering and age checks (jiffies). */
include/linux/write_streams.h +3 -0
@@ -7,6 +7,9 @@struct file ; +/* i_write_stream value of an inode that has no write stream */ +#define WRITE_STREAM_NOT_SET 0 +/* * Write stream slot pool, embedded in whatever filesystem object scopes * an id space.
Signature · 1 line
2.25.1
Re: [PATCH v8 3/9] fs: add i_write_stream, exclusive with the write life time hint
Quoted 78 lines · Kanchan Joshi
Keep the attached stream id in the VFS inode rather than the filesystem's own, so that
fcntl_set_rw_hint()can see it. It is a u16 in the 32-bit hole afteri_state, so struct inode does not grow on 64-bit.Add
inode_set_write_stream()to set or clear it. It refuses a stream while a write life time hint is set, underi_lockasfcntl_set_rw_hint()does, so both sides of the exclusion are enforced in the VFS.Suggested-by Christoph Hellwig <[email protected]>Suggested-by Darrick J. Wong <[email protected]>Signed-off-by Kanchan Joshi <[email protected]>Signed-off-by Anuj Gupta <[email protected]>--- fs/fcntl.c | 7 +++++++ fs/inode.c | 2 ++ fs/internal.h | 1 + fs/write_streams.c | 22 ++++++++++++++++++++++ include/linux/fs.h | 2 +- include/linux/write_streams.h | 3 +++ 6 files changed, 36 insertions(+), 1 deletion(-)fs/fcntl.c +7
-0@@ -383,7 +383,14 @@ static long fcntl_set_rw_hint(struct file *file, unsigned long arg)if (!rw_hint_valid (hint))return -EINVAL ; +/* keep write stream and write life time hint mutually exclusive */ +spin_lock (&inode->i_lock); +if (hint !=WRITE_LIFE_NOT_SET &&READ_ONCE (inode->i_write_stream)) { +spin_unlock (&inode->i_lock); +return -EBUSY ; + }WRITE_ONCE (inode->i_write_hint, hint); +spin_unlock (&inode->i_lock);/* * file->f_mapping->host may differ from inode. As an example, fs/inode.c +2
-0@@ -21,6 +21,7 @@#include <linux/list_lru.h> #include <linux/iversion.h> #include <linux/rw_hint.h> +#include <linux/write_streams.h> #include <linux/seq_file.h> #include <linux/debugfs.h> #include <trace/events/writeback.h> @@ -246,6 +247,7 @@ int inode_init_always_gfp(struct super_block *sb, struct inode *inode, gfp_t gfpatomic_set (&inode->i_writecount,0 ); inode->i_size =0 ; inode->i_write_hint =WRITE_LIFE_NOT_SET ; + inode->i_write_stream =WRITE_STREAM_NOT_SET ; inode->i_blocks =0 ; inode->i_bytes =0 ; inode->i_generation =0 ;fs/internal.h +1
-0@@ -376,3 +376,4 @@ struct write_stream_pool;int write_stream_alloc_fd (struct write_stream_pool *pool,struct file *file);int write_stream_get_id (struct file *file,const struct write_stream_pool *pool); +int inode_set_write_stream (struct inode *inode,u16 id);fs/write_streams.c +1
-0@@ -140,3 +140,25 @@ int write_stream_get_id(struct file *file, const struct write_stream_pool *pool)return -EINVAL ;return ws->id; } +
fs/write_streams.c int write_stream_get_id(struct file *file, const struct write_stream_pool *pool) · line 153
Quoted 10 lines
fs/write_streams.c +10
-0+/** +* inode_set_write_stream - attach or clear the write stream of an inode +* @inode: inode to update +* @id: stream id, or WRITE_STREAM_NOT_SET to clear +* +* A write stream and a write life time hint are mutually exclusive. +* +* Return: 0, or -EBUSY if @inode has a write life time hint. +*/ +int inode_set_write_stream (struct inode *inode,u16 id)
This will only be used internally in this file later on, so make static and introduce along with the user?
Quoted 38 lines
fs/write_streams.c +11
-0+{ +spin_lock (&inode->i_lock); +if (id !=WRITE_STREAM_NOT_SET && + inode->i_write_hint !=WRITE_LIFE_NOT_SET ) { +spin_unlock (&inode->i_lock); +return -EBUSY ; + } +WRITE_ONCE (inode->i_write_stream, id); +spin_unlock (&inode->i_lock); +return 0 ; +}include/linux/fs.h +1
-1@@ -812,7 +812,7 @@ struct inode {/* Misc */ struct inode_state_flags i_state; -/* 32-bit hole */ +u16 i_write_stream;struct rw_semaphore i_rwsem;/* Timestamp for writeback list ordering and age checks (jiffies). */ include/linux/write_streams.h +3
-0@@ -7,6 +7,9 @@struct file ; +/* i_write_stream value of an inode that has no write stream */ +#define WRITE_STREAM_NOT_SET 0 +/* * Write stream slot pool, embedded in whatever filesystem object scopes * an id space.
PATCH v8 4/9 fs: handle the write-stream ioctls in common code
In reply to Kanchan JoshiFS_IOC_WRITE_STREAM_* ioctls involve common handling, such as argument validation, permission and uapi semantics checks etc. Centralize the common handling so that each filesystem does not have to reimplement.
--- fs/write_streams.c | 115 ++++++++++++++++++++++++++++++++++ include/linux/write_streams.h | 6 ++ 2 files changed, 121 insertions(+)
fs/write_streams.c +115 -0
@@ -2,11 +2,13 @@/* Generic write-stream fd management. */ #include <linux/anon_inodes.h> #include <linux/bitmap.h> +#include <linux/capability.h> #include <linux/err.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/mount.h> #include <linux/slab.h> +#include <linux/uaccess.h> #include <linux/write_streams.h> #include "internal.h"
@@ -162,3 +164,116 @@ int inode_set_write_stream(struct inode *inode, u16 id)spin_unlock (&inode->i_lock);return 0 ; } + +static int ioctl_write_stream_get_max (struct file *file,__u32 __user *argp, +write_stream_pool_fn pool_fn) +{ +struct write_stream_pool *pool =pool_fn (file_inode (file)); +__u32 max =0 ; + +if (pool) + max =write_stream_pool_count (pool); +return put_user (max, argp); +} + +static int ioctl_write_stream_alloc (struct file *file, +write_stream_pool_fn pool_fn) +{ +struct write_stream_pool *pool; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + pool =pool_fn (file_inode (file)); +if (!pool) +return -EOPNOTSUPP ; +return write_stream_alloc_fd (pool, file); +} + +static int ioctl_write_stream_attach (struct inode *inode,int stream_fd, +write_stream_pool_fn pool_fn) +{ +CLASS (fd, f)(stream_fd); +struct write_stream_pool *pool; +int id, error; + +if (fd_empty (f)) +return -EBADF ; + +/* +* Serialize against vfs_fileattr_set(), which takes the same lock to +* change the flags that select the pool. +*/ +inode_lock (inode); + pool =pool_fn (inode); +if (!pool) { + error = -EINVAL ; + goto out_unlock; + } + id =write_stream_get_id (fd_file (f), pool); +if (id <0 ) { + error = id; + goto out_unlock; + } + error =inode_set_write_stream (inode, id); +out_unlock: +inode_unlock (inode); +return error; +} + +static int ioctl_write_stream_set (struct file *file, +struct fs_write_stream_set __user *argp, +write_stream_pool_fn pool_fn) +{ +struct inode *inode =file_inode (file); +struct fs_write_stream_set set; + +if (!(file->f_mode &FMODE_WRITE )) +return -EBADF ; +if (copy_from_user (&set, argp,sizeof (set))) +return -EFAULT ; +if (set.flags & ~FS_WRITE_STREAM_SET_CLEAR ) +return -EINVAL ; +if (!S_ISREG (inode->i_mode)) +return -EINVAL ; +if (IS_IMMUTABLE (inode)) +return -EPERM ; +if (!inode_owner_or_capable (file_mnt_idmap (file), inode)) +return -EPERM ; + +if (!(set.flags &FS_WRITE_STREAM_SET_CLEAR )) +return ioctl_write_stream_attach (inode, set.stream_fd, pool_fn); +if (set.stream_fd !=-1 ) +return -EINVAL ; + +inode_lock (inode); +inode_set_write_stream (inode,WRITE_STREAM_NOT_SET ); +inode_unlock (inode); +return 0 ; +} + +/** +* write_stream_ioctl - handle the FS_IOC_WRITE_STREAM_* ioctls +* @file: file the ioctl is issued on +* @cmd: ioctl command +* @argp: ioctl argument +* @pool_fn: callback that returns the pool serving an inode, or +* NULL if that inode cannot use write streams; may sleep, and is +* called with the inode lock held for FS_IOC_WRITE_STREAM_SET +* +* Called by a filesystem from its ioctl handler. GET_MAX and ALLOC work on +* any file the filesystem passes in, SET on regular files only. +*/ +long write_stream_ioctl (struct file *file,unsigned int cmd, +void __user *argp,write_stream_pool_fn pool_fn) +{ +switch (cmd) { +case FS_IOC_WRITE_STREAM_GET_MAX : +return ioctl_write_stream_get_max (file, argp, pool_fn); +case FS_IOC_WRITE_STREAM_ALLOC : +return ioctl_write_stream_alloc (file, pool_fn); +case FS_IOC_WRITE_STREAM_SET : +return ioctl_write_stream_set (file, argp, pool_fn); + } +return -ENOTTY ; +} +EXPORT_SYMBOL_GPL (write_stream_ioctl);
include/linux/write_streams.h +6 -0
@@ -6,6 +6,7 @@#include <linux/types.h> struct file ; +struct inode ;/* i_write_stream value of an inode that has no write stream */ #define WRITE_STREAM_NOT_SET 0
@@ -29,4 +30,9 @@ write_stream_pool_count(const struct write_stream_pool *pool)int write_stream_pool_init (struct write_stream_pool *pool,unsigned int nr);void write_stream_pool_destroy (struct write_stream_pool *pool); +typedef struct write_stream_pool *(*write_stream_pool_fn )(struct inode *inode); + +long write_stream_ioctl (struct file *file,unsigned int cmd, +void __user *argp,write_stream_pool_fn pool_fn); +#endif /* _LINUX_WRITE_STREAMS_H */
Signature · 1 line
2.25.1
PATCH v8 5/9 xfs: implement software write-stream management
In reply to Kanchan JoshiFrom: Anuj Gupta <[email protected]>
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.
--- 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(+)
fs/xfs/xfs_buf.c +31 -0
@@ -1648,6 +1648,7 @@ voidxfs_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(
fs/xfs/xfs_buf.h +6 -0
@@ -15,6 +15,7 @@#include <linux/uio.h> #include <linux/list_lru.h> #include <linux/lockref.h> +#include <linux/write_streams.h> 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)
fs/xfs/xfs_ioctl.c +26 -0
@@ -46,6 +46,7 @@#include "xfs_verify_media.h" #include "xfs_zone_priv.h" #include "xfs_zone_alloc.h" +#include "xfs_filestream.h" #include <linux/mount.h> #include <linux/fileattr.h>
@@ -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 ; }
fs/xfs/xfs_super.c +5 -0
@@ -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 ;
Signature · 1 line
2.25.1
Re: [PATCH v8 5/9] xfs: implement software write-stream management
Quoted 36 lines · Kanchan Joshi
From: Anuj Gupta <[email protected]>
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 <[email protected]>Suggested-by Darrick J. Wong <[email protected]>Signed-off-by Anuj Gupta <[email protected]>Signed-off-by Kanchan Joshi <[email protected]>--- 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(+)fs/xfs/xfs_buf.c +1
-0@@ -1648,6 +1648,7 @@ voidxfs_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 */
fs/xfs/xfs_buf.c xfs_configure_buftarg_atomic_writes( · line 1692
Quoted 7 lines
fs/xfs/xfs_buf.c +4
-0@@ -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 */
Could you explain the rationale behind this heuristic?
Quoted 134 lines
fs/xfs/xfs_buf.c +26
-0+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(fs/xfs/xfs_buf.h +6
-0@@ -15,6 +15,7 @@#include <linux/uio.h> #include <linux/list_lru.h> #include <linux/lockref.h> +#include <linux/write_streams.h> 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)fs/xfs/xfs_ioctl.c +26
-0@@ -46,6 +46,7 @@#include "xfs_verify_media.h" #include "xfs_zone_priv.h" #include "xfs_zone_alloc.h" +#include "xfs_filestream.h" #include <linux/mount.h> #include <linux/fileattr.h> @@ -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 ; }fs/xfs/xfs_super.c +5
-0@@ -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 ;
PATCH v8 6/9 xfs: write stream based AG placement
In reply to Kanchan JoshiChoose the starting AG from the write stream set on the file.
Isolating streams into separate allocation groups reduces block interleaving between concurrent writers, AGF lock contention and logical file fragmentation.
AGs are partitioned among the streams. The stream value selects the AG set and the inode number selects the AG within it, so intra-stream concurrency comes from the AG set size.
Example: 8 Allocation Groups, 4 write streams
AG set size = 2 AGs per write stream
Stream 1 (ID: 1) Stream 2 (ID: 2) Streams 3 & 4
+---------+---------+ +---------+---------+ +-------------
| AG0 | AG1 | | AG2 | AG3 | | AG4...AG7
+---------+---------+ +---------+---------+ +-------------
^ ^ ^ ^
| | | |
| File B (ino: 101) | File D (ino: 201)
| 101 % 2 = 1 -> AG 1 | 201 % 2 = 1 -> AG 3
| |
File A (ino: 100) File C (ino: 200)
100 % 2 = 0 -> AG 0 200 % 2 = 0 -> AG 2If the AGs do not divide evenly, the last stream absorbs the remainder.
Set boundaries are a hint, not a partition: file contiguity is preserved and the full space stays usable with a single stream.
--- fs/xfs/libxfs/xfs_bmap.c | 61 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_bmap.c +59 -2
@@ -3581,8 +3581,32 @@ xfs_bmap_btalloc_filestreams(return xfs_bmap_btalloc_low_space (ap, args); } +static xfs_agnumber_t +xfs_bmap_write_stream_agno ( +struct xfs_inode *ip, +unsigned int stream_id) +{ +struct xfs_mount *mp = ip->i_mount; +xfs_agnumber_t nr_ags = mp->m_sb.sb_agcount; +unsigned int nr_streams = +write_stream_pool_count (&mp->m_ddev_targp->bt_stream_pool); +xfs_agnumber_t ag_set_size, start_agno; +xfs_ino_t ino =I_INO (ip); + + stream_id -=1 ;/* convert from 1-based to 0-based */ + ag_set_size = nr_ags / nr_streams; + start_agno = stream_id * ag_set_size; + +/* last stream absorbs any uneven remainder */ +if (stream_id == nr_streams -1 ) + ag_set_size = nr_ags - start_agno; + +return start_agno +do_div (ino, ag_set_size); +} + +/* Core AG allocator. The caller sets ap->blkno to the target AG start. */ static int -xfs_bmap_btalloc_best_length ( +xfs_bmap_btalloc_from_blkno (struct xfs_bmalloca *ap,struct xfs_alloc_arg *args,int stripe_align)
@@ -3590,7 +3614,6 @@ xfs_bmap_btalloc_best_length(xfs_extlen_t blen =0 ;int error; - ap->blkno =XFS_INODE_TO_FSB (ap->ip);if (!xfs_bmap_adjacent (ap)) ap->eof = false;
@@ -3623,6 +3646,34 @@ xfs_bmap_btalloc_best_length(return xfs_bmap_btalloc_low_space (ap, args); } +/* Start a write-stream file in the AG set that backs its stream. */ +static int +xfs_bmap_btalloc_write_stream ( +struct xfs_bmalloca *ap, +struct xfs_alloc_arg *args, +int stripe_align, +unsigned int stream_id) +{ +struct xfs_mount *mp = ap->ip->i_mount; +xfs_agnumber_t agno; + + agno =xfs_bmap_write_stream_agno (ap->ip, stream_id); + ap->blkno =XFS_AGB_TO_FSB (mp, agno,0 ); +/* the stream picks the AG; don't let the inode32 rotor override it */ + args->datatype &= ~XFS_ALLOC_INITIAL_USER_DATA ; +return xfs_bmap_btalloc_from_blkno (ap, args, stripe_align); +} + +static int +xfs_bmap_btalloc_best_length ( +struct xfs_bmalloca *ap, +struct xfs_alloc_arg *args, +int stripe_align) +{ + ap->blkno =XFS_INODE_TO_FSB (ap->ip); +return xfs_bmap_btalloc_from_blkno (ap, args, stripe_align); +} +static int xfs_bmap_btalloc (struct xfs_bmalloca *ap)
@@ -3642,6 +3693,7 @@ xfs_bmap_btalloc( };xfs_fileoff_t orig_offset;xfs_extlen_t orig_length; +unsigned int stream_id;int error;int stripe_align;
@@ -3654,11 +3706,16 @@ xfs_bmap_btalloc(/* Trim the allocation back to the maximum an AG can fit. */ args.maxlen =min (ap->length, mp->m_ag_max_usable); + stream_id =READ_ONCE (VFS_I (ap->ip)->i_write_stream); +if (unlikely (XFS_TEST_ERROR (mp,XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT ))) error =xfs_bmap_exact_minlen_extent_alloc (ap, &args);else if ((ap->datatype &XFS_ALLOC_USERDATA ) &&xfs_inode_is_filestream (ap->ip)) error =xfs_bmap_btalloc_filestreams (ap, &args, stripe_align); +else if ((ap->datatype &XFS_ALLOC_USERDATA ) && stream_id) + error =xfs_bmap_btalloc_write_stream (ap, &args, stripe_align, + stream_id);else error =xfs_bmap_btalloc_best_length (ap, &args, stripe_align);if (error)
Signature · 1 line
2.25.1
PATCH v8 7/9 xfs: support write streams on realtime volumes
In reply to Kanchan JoshiFrom: Anuj Gupta <[email protected]>
Enable write streams on non-zoned realtime volumes that have realtime groups, partitioning RTGs among the streams as the AG side does.
Stream based RTG selection applies to the first allocation of a file; later ones keep using the adjacent-allocation hint so the file stays contiguous.
Directory with realtime inherit flag reports and allocates from the realtime pool, so that streams queried or allocated through a directory can be used for the files created in it.
--- fs/xfs/xfs_ioctl.c | 24 ++++++++++++++++++++---- fs/xfs/xfs_rtalloc.c | 28 ++++++++++++++++++++++++++++ fs/xfs/xfs_super.c | 7 +++++++ 3 files changed, 55 insertions(+), 4 deletions(-)
fs/xfs/xfs_ioctl.c +20 -4
@@ -560,7 +560,8 @@ xfs_ioctl_setattr_xflags(/* 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)) + ((fa->fsx_xflags &FS_XFLAG_FILESTREAM ) || + rtflag !=XFS_IS_REALTIME_INODE (ip)))return -EINVAL ;if (rtflag !=XFS_IS_REALTIME_INODE (ip)) {
@@ -1442,17 +1443,32 @@ 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. */ +/* +* Pool for the data of @inode, or NULL if it cannot use write streams. A +* directory that passes the realtime flag on to new files uses the realtime +* pool, so that streams allocated through it can be set on those files. +*/ static struct write_stream_pool *xfs_write_stream_pool (struct inode *inode) {struct xfs_inode *ip =XFS_I (inode); +struct xfs_mount *mp = ip->i_mount;struct write_stream_pool *pool =NULL ; +bool rt;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; +if (xfs_inode_is_filestream (ip)) + goto out_unlock; + + rt =XFS_IS_REALTIME_INODE (ip) || + (S_ISDIR (inode->i_mode) && + (ip->i_diflags &XFS_DIFLAG_RTINHERIT ) && mp->m_rtdev_targp); +if (!rt) + pool = &mp->m_ddev_targp->bt_stream_pool; +else if (!xfs_has_zoned (mp)) + pool = &mp->m_rtdev_targp->bt_stream_pool; +out_unlock:xfs_iunlock (ip,XFS_ILOCK_SHARED );return pool; }
fs/xfs/xfs_rtalloc.c +28 -0
@@ -2160,6 +2160,29 @@ xfs_rtallocate_align(return 0 ; } +static xfs_rtblock_t +xfs_bmap_write_stream_rtbno ( +struct xfs_inode *ip, +unsigned int stream_id) +{ +struct xfs_mount *mp = ip->i_mount; +unsigned int nr_rtgs = mp->m_sb.sb_rgcount; +unsigned int nr_streams = +write_stream_pool_count (&mp->m_rtdev_targp->bt_stream_pool); +unsigned int set_size = nr_rtgs / nr_streams; +xfs_rgnumber_t start_rgno, target_rgno; +xfs_ino_t ino =I_INO (ip); + + stream_id -=1 ;/* convert from 1-based to 0-based */ + start_rgno = stream_id * set_size; +if (stream_id == nr_streams -1 ) + set_size = nr_rtgs - start_rgno; + target_rgno = start_rgno +do_div (ino, set_size); + +return (xfs_rtblock_t )target_rgno << + mp->m_groups[XG_TYPE_RTG ].blklog; +} +int xfs_bmap_rtalloc (struct xfs_bmalloca *ap)
@@ -2174,10 +2197,13 @@ xfs_bmap_rtalloc(bool noalign = false;bool initial_user_data = ap->datatype &XFS_ALLOC_INITIAL_USER_DATA ; +unsigned int stream_id;int error;ASSERT (!xfs_has_zoned (ap->tp->t_mountp)); + stream_id =READ_ONCE (VFS_I (ap->ip)->i_write_stream); + retry: error =xfs_rtallocate_align (ap, &ralen, &raminlen, &prod, &noalign);if (error)
@@ -2185,6 +2211,8 @@ xfs_bmap_rtalloc(if (xfs_bmap_adjacent (ap)) bno_hint = ap->blkno; +else if (stream_id &&xfs_has_rtgroups (ap->ip->i_mount)) + bno_hint =xfs_bmap_write_stream_rtbno (ap->ip, stream_id);if (xfs_has_rtgroups (ap->ip->i_mount)) { error = xfs_rtallocate_rtgs(ap->tp, bno_hint, raminlen, ralen,
fs/xfs/xfs_super.c +7 -0
@@ -637,6 +637,13 @@ xfs_setup_devices(if (error)return error;xfs_update_bdi_rahead (mp); + +if (xfs_has_rtgroups (mp) && !xfs_has_zoned (mp)) { + error =xfs_buftarg_init_streams (mp->m_rtdev_targp, + mp->m_sb.sb_rgcount); +if (error) +return error; + } }return 0 ;
Signature · 1 line
2.25.1
PATCH v8 8/9 iomap: introduce and propagate write_stream
In reply to Kanchan JoshiAdd a new write_stream field to struct iomap. Propagate write_stream from iomap to bio in both direct I/O and buffered writeback paths.
--- fs/iomap/direct-io.c | 2 ++ fs/iomap/ioend.c | 3 +++ include/linux/iomap.h | 1 + 3 files changed, 6 insertions(+)
fs/iomap/direct-io.c +2 -0
@@ -326,6 +326,7 @@ static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio, REQ_OP_WRITE | REQ_SYNC | REQ_IDLE);fscrypt_set_bio_crypt_ctx (bio, inode, pos,GFP_KERNEL ); bio->bi_iter.bi_sector =iomap_sector (&iter->iomap, pos); + bio->bi_write_stream = iter->iomap.write_stream; bio->bi_private = dio; bio->bi_end_io = iomap_dio_bio_end_io;
@@ -356,6 +357,7 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,fscrypt_set_bio_crypt_ctx (bio, iter->inode, pos,GFP_KERNEL ); bio->bi_iter.bi_sector =iomap_sector (&iter->iomap, pos); bio->bi_write_hint = iter->inode->i_write_hint; + bio->bi_write_stream = iter->iomap.write_stream; bio->bi_ioprio = dio->iocb->ki_ioprio; bio->bi_private = dio; bio->bi_end_io = iomap_dio_bio_end_io;
fs/iomap/ioend.c +3 -0
@@ -123,6 +123,7 @@ static struct iomap_ioend *iomap_alloc_ioend(struct iomap_writepage_ctx *wpc, GFP_NOFS, &iomap_ioend_bioset); bio->bi_iter.bi_sector =iomap_sector (&wpc->iomap, pos); bio->bi_write_hint = wpc->inode->i_write_hint; + bio->bi_write_stream = wpc->iomap.write_stream;wbc_init_bio (wpc->wbc, bio); wpc->nr_folios =0 ;return iomap_init_ioend (wpc->inode, bio, pos, ioend_flags);
@@ -145,6 +146,8 @@ static bool iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t pos,if (!(wpc->iomap.flags &IOMAP_F_ANON_WRITE ) &&iomap_sector (&wpc->iomap, pos) !=bio_end_sector (&ioend->io_bio))return false; +if (wpc->iomap.write_stream != ioend->io_bio.bi_write_stream) +return false;/* * Limit ioend bio chain lengths to minimise IO completion latency. This * also prevents long tight loops ending page writeback on all the
include/linux/iomap.h +1 -0
@@ -133,6 +133,7 @@ struct iomap {u64 length;/* length of mapping, bytes */ u16 type;/* type of mapping */ u16 flags;/* flags for mapping */ +u16 write_stream;/* write stream for I/O */ struct block_device *bdev;/* block device for I/O */ struct dax_device *dax_dev;/* dax_dev for dax operations */ void *inline_data;
Signature · 1 line
2.25.1
PATCH v8 9/9 xfs: support hardware write streams
In reply to Kanchan JoshiUse the stream count reported by the data and realtime block devices where they have one, falling back to the geometry-derived count. The count is capped at the group count so that every stream backs onto at least one group; a device with more streams than the filesystem groups loses the excess, so mkfs should create at least as many groups as the device has streams.
It is also capped at U8_MAX, the most streams the block layer can tag.
Set iomap->write_stream from the inode so that the id reaches the device, but only when the device has write streams.
--- fs/xfs/xfs_buf.c | 8 +++++++- fs/xfs/xfs_iomap.c | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-)
fs/xfs/xfs_buf.c +7 -1
@@ -1711,8 +1711,14 @@ xfs_buftarg_init_streams( unsigned int nr_groups) {unsigned int nr_streams; +unsigned int hw_streams; - nr_streams =xfs_sw_write_stream_count (nr_groups); + hw_streams =bdev_max_write_streams (btp->bt_bdev); +if (hw_streams) + nr_streams =min3 (hw_streams, nr_groups, +XFS_MAX_WRITE_STREAMS ); +else + nr_streams =xfs_sw_write_stream_count (nr_groups);return write_stream_pool_init (&btp->bt_stream_pool, nr_streams); }
fs/xfs/xfs_iomap.c +5 -0
@@ -144,6 +144,11 @@ xfs_bmbt_to_iomap( } iomap->offset =XFS_FSB_TO_B (mp, imap->br_startoff); iomap->length =XFS_FSB_TO_B (mp, imap->br_blockcount); +/* software stream ids must not reach a device without streams */ +if (bdev_max_write_streams (target->bt_bdev)) + iomap->write_stream =READ_ONCE (VFS_I (ip)->i_write_stream); +else + iomap->write_stream =WRITE_STREAM_NOT_SET ;if (mapping_flags &IOMAP_DAX ) { iomap->dax_dev = target->bt_daxdev; }else {
Signature · 1 line
2.25.1