Skip to contentSkip to conversation

xfs write streams

PATCH Superseded Kanchan Joshi 4 participants 35 messages Jul 17 – Sep 2 1 message not in the archive

Subsystems (4)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20260717125624epcas5p3fefc5b8ff274260bf8fb2f1b225b4f9a@epcas5p3.samsung.com

#1 lore raw

v4 cover xfs write streams

This series introduces a generic interface [1] for write stream management on files. It enables spatial isolation and concurrency improvments [3] in xfs using
- generic AG-set (patch #4)
- write-stream based AG-set (patch #5)

In LSFMMBPF'26, we discussed write-stream as a mechanism to reduce the filesystem allocator bottlenecks and improving direct/buffered IO scalability.

Write streams allow the abstraction provider (fs, block, raid etc.) to leverage application's intent (file relationships/lifecycle).
- application: sends grouping/isolation intent with a stream id.
- xfs: maps streams to AGs; 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:
- 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 hardware that provides spatial isolation.

- For hardware-only spatial isolation, first 3 patches are needed.

- 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.

[3]
### Performance

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
generic AG-set: 93 KIOPS (+126%)
write-stream AG-set: 227 KIOPS (+453%)
here, 16 files are assigned 4 unique 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, generic AG-set size = 2, write-stream AG-set size = 4

base: 59 KIOPS
generic AG-set: 94 KIOPS (+59%)
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

[1]
### Application interface

Four new ioctls:

      FS_IOC_WRITE_STREAM_GET_MAX  query the max streams supported
      FS_IOC_WRITE_STREAM_OPEN     open a stream id, returns a stream fd
      FS_IOC_WRITE_STREAM_SET      attach the stream fd to an open file
      FS_IOC_WRITE_STREAM_GET      query the stream id value set on a file

### 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 (kernel limit: up to 255 due to u8 field).

- 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 multi-application conflicts.

Note: within the kernel, the separation between two constructs (write-hint and write-stream) had started from 6.16 itself.

### Changelog

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.

### Interface example

/* FD-based write-stream ioctl */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <linux/types.h>

/* Duplicate the kernel UAPI definitions */

struct fs_write_stream_open {
	uint32_t	stream_id;	/* IN: desired id if OPEN_EXACT set; OUT: assigned id */
	uint32_t	flags;		/* IN: FS_WRITE_STREAM_OPEN_* */
};

#define FS_WRITE_STREAM_OPEN_EXACT (1 << 0)

#define FS_IOC_WRITE_STREAM_GET_MAX     _IOR('f', 135, __u32)
#define FS_IOC_WRITE_STREAM_OPEN        _IOWR('f', 136, struct fs_write_stream_open)
#define FS_IOC_WRITE_STREAM_SET         _IOW('f', 137, __s32)
#define FS_IOC_WRITE_STREAM_GET         _IOR('f', 138, __u32)

static void usage(const char *prog)

{
	fprintf(stderr, "Usage:\n");
	fprintf(stderr, "  %s <file> max        - get max supported streams\n", prog);
	fprintf(stderr, "  %s <file> get        - get stream id set on file\n", prog);
	fprintf(stderr, "  %s <file> open [id]  - open a stream (EXACT if id given) and bind it to file\n", prog);
	exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])

{
	const char *filepath, *cmd;
	int fd;

	if (argc < 3)
		usage(argv[0]);

	filepath = argv[1];
	cmd = argv[2];

	fd = open(filepath, O_RDWR);
	if (fd < 0) {
		perror("open(file)");
		return EXIT_FAILURE;
	}

	if (!strcmp(cmd, "max")) {
		uint32_t max;

		if (ioctl(fd, FS_IOC_WRITE_STREAM_GET_MAX, &max) < 0) {
			perror("ioctl(GET_MAX)");
			return EXIT_FAILURE;
		}
		printf("Max streams supported: %u\n", max);
	} else if (!strcmp(cmd, "get")) {
		uint32_t stream_id;

		if (ioctl(fd, FS_IOC_WRITE_STREAM_GET, &stream_id) < 0) {
			perror("ioctl(GET)");
			return EXIT_FAILURE;
		}
		printf("Stream id on file: %u\n", stream_id);
	} else if (!strcmp(cmd, "open")) {
		struct fs_write_stream_open wso = { 0 };
		int stream_fd;

		if (argc == 4) {
			wso.flags = FS_WRITE_STREAM_OPEN_EXACT;
			wso.stream_id = atoi(argv[3]);
		}

		/* OPEN can be called through any fd on the target filesystem. */
		stream_fd = ioctl(fd, FS_IOC_WRITE_STREAM_OPEN, &wso);
		if (stream_fd < 0) {
			perror("ioctl(OPEN)");
			return EXIT_FAILURE;
		}
		printf("Opened stream id %u (fd %d)\n", wso.stream_id, stream_fd);

		/* SET takes the stream fd directly as the ioctl argument. */
		if (ioctl(fd, FS_IOC_WRITE_STREAM_SET, (unsigned long)stream_fd) < 0) {
			perror("ioctl(SET)");
			close(stream_fd);
			return EXIT_FAILURE;
		}
		printf("Bound stream %u to %s\n", wso.stream_id, filepath);

		close(stream_fd);
	} else {
		fprintf(stderr, "Unknown command: %s\n", cmd);
		usage(argv[0]);
	}

	close(fd);
	return EXIT_SUCCESS;
}

Anuj Gupta (2):
fs: add write-stream management ioctls
xfs: implement write-stream management support

Kanchan Joshi (4):
iomap: introduce and propagate write_stream
xfs: generic AG set based steering
xfs: write stream based AG placement
xfs: introduce software write streams

 fs/iomap/direct-io.c     |   1 +
 fs/iomap/ioend.c         |   3 +
 fs/xfs/libxfs/xfs_bmap.c |  74 +++++++++++++++++
 fs/xfs/xfs_icache.c      |   1 +
 fs/xfs/xfs_inode.c       | 175 +++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_inode.h       |   8 ++
 fs/xfs/xfs_ioctl.c       |  69 +++++++++++++++
 fs/xfs/xfs_iomap.c       |   1 +
 fs/xfs/xfs_mount.h       |   5 ++
 fs/xfs/xfs_super.c       |  12 +++
 include/linux/iomap.h    |   2 +
 include/uapi/linux/fs.h  |  16 ++++
 12 files changed, 367 insertions(+)
Signature · 1 line
2.25.1
#2 lore raw

PATCH v4 1/6 fs: add write-stream management ioctls

From: Anuj Gupta <[email protected]>

Wire up the userspace interface for write stream management via four ioctls:

  FS_IOC_WRITE_STREAM_GET_MAX  query the max streams supported
  FS_IOC_WRITE_STREAM_OPEN     open a stream id, returns a stream fd
  FS_IOC_WRITE_STREAM_SET      attach the stream fd to an open file
  FS_IOC_WRITE_STREAM_GET      query the stream id value set on a file

Application should query the available streams by using FS_IOC_WRITE_STREAM_GET_MAX. If returned value is N, valid stream id for the file are 1 to N.

Application calls FS_IOC_WRITE_STREAM_OPEN to get fd for a stream-id. By default, kernel picks an available stream-id and returns the fd for it. The flag FS_WRITE_STREAM_OPEN_EXACT can be used to request a specific stream_id. This is useful if application cares about keeping a stable stream-id-to-spatial-isolation-bucket mapping across restarts.

---
 include/uapi/linux/fs.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/uapi/linux/fs.h +16 -0
index bd87262f2e34..a756d27bdf84 100644
@@ -345,6 +345,22 @@ 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_open {
+	__u32		stream_id;	/* IN: requested id if OPEN_EXACT set; OUT: assigned id */
+	__u32		flags;		/* IN: FS_WRITE_STREAM_OPEN_* */
+};
+
+/*
+ * Flag to ask for specific stream_id, otherwise kernel can return any.
+ * Fails with -EBUSY if that stream_id is already held
+ */
+#define FS_WRITE_STREAM_OPEN_EXACT	(1 << 0)
+
+#define FS_IOC_WRITE_STREAM_GET_MAX	_IOR('f', 135, __u32)
+#define FS_IOC_WRITE_STREAM_OPEN	_IOWR('f', 136, struct fs_write_stream_open)
+#define FS_IOC_WRITE_STREAM_SET		_IOW('f', 137, __s32)
+#define FS_IOC_WRITE_STREAM_GET		_IOR('f', 138, __u32)
+
 /*
  * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS)
  *
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

Quoted 14 lines · Kanchan Joshi

From: Anuj Gupta <[email protected]>

Wire up the userspace interface for write stream management via four ioctls:

  FS_IOC_WRITE_STREAM_GET_MAX  query the max streams supported
  FS_IOC_WRITE_STREAM_OPEN     open a stream id, returns a stream fd
  FS_IOC_WRITE_STREAM_SET      attach the stream fd to an open file
  FS_IOC_WRITE_STREAM_GET      query the stream id value set on a file

Application should query the available streams by using FS_IOC_WRITE_STREAM_GET_MAX. If returned value is N, valid stream id for the file are 1 to N.

Quoted 5 lines

Application calls FS_IOC_WRITE_STREAM_OPEN to get fd for a stream-id. By default, kernel picks an available stream-id and returns the fd for it. The flag FS_WRITE_STREAM_OPEN_EXACT can be used to request a specific stream_id. This is useful if application cares about keeping a stable stream-id-to-spatial-isolation-bucket mapping across restarts.

Hmm. If FS_IOC_WRITE_STREAM_OPEN returns an fd that represents an open stream id, then why does STREAM_SET below take a pointer to a signed s32? And why does STREAM_GET take a pointer to a u32?

IOWs it'd be much easier to distinguish these things if the ioctls took pointers to structs instead of u32/s32 pointers directly.

Also it's a little weird that STREAM_SET associates an open file with a stream fd, but STREAM_GET returns the stream *id* (not the fd) associated with an open file.

Can you extract the stream id from the fd that FS_IOC_WRITE_STREAM_OPEN returns?

--D

Quoted 39 lines
---
 include/uapi/linux/fs.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/uapi/linux/fs.h +16 -0
index bd87262f2e34..a756d27bdf84 100644
@@ -345,6 +345,22 @@ 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_open {
+	__u32		stream_id;	/* IN: requested id if OPEN_EXACT set; OUT: assigned id */
+	__u32		flags;		/* IN: FS_WRITE_STREAM_OPEN_* */
+};
+
+/*
+ * Flag to ask for specific stream_id, otherwise kernel can return any.
+ * Fails with -EBUSY if that stream_id is already held
+ */
+#define FS_WRITE_STREAM_OPEN_EXACT	(1 << 0)
+
+#define FS_IOC_WRITE_STREAM_GET_MAX	_IOR('f', 135, __u32)
+#define FS_IOC_WRITE_STREAM_OPEN	_IOWR('f', 136, struct fs_write_stream_open)
+#define FS_IOC_WRITE_STREAM_SET		_IOW('f', 137, __s32)
+#define FS_IOC_WRITE_STREAM_GET		_IOR('f', 138, __u32)
+
 /*
  * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS)
  *
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

Quoted 15 lines · Darrick J. Wong
Quoted 14 lines · Kanchan Joshi

From: Anuj Gupta<[email protected]>

Wire up the userspace interface for write stream management via four ioctls:

   FS_IOC_WRITE_STREAM_GET_MAX  query the max streams supported
   FS_IOC_WRITE_STREAM_OPEN     open a stream id, returns a stream fd
   FS_IOC_WRITE_STREAM_SET      attach the stream fd to an open file
   FS_IOC_WRITE_STREAM_GET      query the stream id value set on a file

Application should query the available streams by using FS_IOC_WRITE_STREAM_GET_MAX. If returned value is N, valid stream id for the file are 1 to N.

Quoted 8 lines
Quoted 5 lines

Application calls FS_IOC_WRITE_STREAM_OPEN to get fd for a stream-id. By default, kernel picks an available stream-id and returns the fd for it. The flag FS_WRITE_STREAM_OPEN_EXACT can be used to request a specific stream_id. This is useful if application cares about keeping a stable stream-id-to-spatial-isolation-bucket mapping across restarts.

Hmm. If FS_IOC_WRITE_STREAM_OPEN returns an fd that represents an open stream id, then why does STREAM_SET below take a pointer to a signed s32?

Not a pointer, that s32 is to pass the stream fd.

Quoted 1 line

And why does STREAM_GET take a pointer to a u32?

This also is a scalar value and not a pointer. The returned stream-id is a u32.

Quoted 2 lines

IOWs it'd be much easier to distinguish these things if the ioctls took pointers to structs instead of u32/s32 pointers directly.

Maybe IOCTL naming is the source of confusion, because we introduced stream-fds alongside stream-ids. More on this below.

Quoted 3 lines

Also it's a little weird that STREAM_SET associates an open file with a stream fd, but STREAM_GET returns the stream*id* (not the fd) associated with an open file.

The confusion stems from keeping symmetric names for different operations. Let me explain the intent and propose revised names below.

Christoph's idea was to use FD for a write-stream so that it remains exclusive to the application and unrelated applications don't collide.

So, in this version, write-stream ids are implemented as independent resource:
- To set a file's write-stream ID to X, the app must first obtain the fd for stream X.
- That stream/fd will be unavailable if some other application has already opened it.
- If applications gets the stream fd, it can use that to set stream id for multiple files.
- It can close the stream-fd and that does not change anything for the file resource (i.e, its inode continue to carry the stream-id value that was set).

Christoph - does this match?

Here are the revised names (suggestions?):

1. FS_IOC_QUERY_MAX_WRITE_STREAM_IDS _IOR('f', 135, __u32)

Returns (in __u32) number of supported stream-ids.

2. FS_IOC_OPEN_WRITE_STREAM_ID _IOWR('f', 136, struct
fs_write_stream_open)

Open the specific write-stream id (or any available one via a flag) and return its FD. This gives right to use that stream-id.

And the following manage setting/clearing/querying write-stream on the file:

3. FS_IOC_SET_FILE_WRITE_STREAM_BY_FD _IOW('f', 137, __s32)

Sets a file's write-stream value (in inode) to whatever the stream fd (passed as __s32) carries. So one never sets scalar values into a file directly.

4. FS_IOC_QUERY_FILE_WRITE_STREAM_ID _IOR('f', 138, __u32)

Queries the write-stream value (__u32) currently assigned to the file (its inode).

5. FS_IOC_CLEAR_FILE_WRITE_STREAM_ID _IO('f', 139)

To clear (zeroing) the write-stream value on the file's inode.

Quoted 2 lines

Can you extract the stream id from the fd that FS_IOC_WRITE_STREAM_OPEN returns?

Can be done by implementing the ioctl handler for that stream fd, but did not feel the need of it. Do you see the utility? If yes, name can potentially be FS_IOC_QUERY_WRITE_STREAM_ID_FROM_FD.

#5 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

Quoted 21 lines · Kanchan Joshi
Quoted 20 lines · Darrick J. Wong
Quoted 19 lines · Kanchan Joshi

From: Anuj Gupta<[email protected]>

Wire up the userspace interface for write stream management via four ioctls:

   FS_IOC_WRITE_STREAM_GET_MAX  query the max streams supported
   FS_IOC_WRITE_STREAM_OPEN     open a stream id, returns a stream fd
   FS_IOC_WRITE_STREAM_SET      attach the stream fd to an open file
   FS_IOC_WRITE_STREAM_GET      query the stream id value set on a file

Application should query the available streams by using FS_IOC_WRITE_STREAM_GET_MAX. If returned value is N, valid stream id for the file are 1 to N.

Application calls FS_IOC_WRITE_STREAM_OPEN to get fd for a stream-id. By default, kernel picks an available stream-id and returns the fd for it. The flag FS_WRITE_STREAM_OPEN_EXACT can be used to request a specific stream_id. This is useful if application cares about keeping a stable stream-id-to-spatial-isolation-bucket mapping across restarts.

Quoted 5 lines
Quoted 3 lines

Hmm. If FS_IOC_WRITE_STREAM_OPEN returns an fd that represents an open stream id, then why does STREAM_SET below take a pointer to a signed s32?

Not a pointer, that s32 is to pass the stream fd.

Oh, but that third argument to ioctl() /is/ supposed to be a pointer to some data. From ioctl(2):

"The third argument is an untyped pointer to memory. It's traditionally char *argp (from the days before void * was valid C), and will be so named for this discussion."

You can also see this reflected in the discussion of the R/W/WR variants of _IO:
https://docs.kernel.org/driver-api/ioctl.html

"[data_type] may be a pointer to data to be passed into the kernel (_IOW), out of the kernel (_IOR), or both (_IOWR)"

Granted your kernel code can treat that argp as an integer and not a __user pointer if it wants to, but that goes against most ioctl implementations. Further, subsystems that marshal ioctl information for passing through to another layer (e.g. fuse) assume that the third argument is a pointer and that it can copy sizeof(data_type) bytes at that address in and out of the kernel as part of marshalling.

Quoted 64 lines
Quoted 1 line

And why does STREAM_GET take a pointer to a u32?

This also is a scalar value and not a pointer. The returned stream-id is a u32.

Quoted 2 lines

IOWs it'd be much easier to distinguish these things if the ioctls took pointers to structs instead of u32/s32 pointers directly.

Maybe IOCTL naming is the source of confusion, because we introduced stream-fds alongside stream-ids. More on this below.

Quoted 3 lines

Also it's a little weird that STREAM_SET associates an open file with a stream fd, but STREAM_GET returns the stream*id* (not the fd) associated with an open file.

The confusion stems from keeping symmetric names for different operations. Let me explain the intent and propose revised names below.

Christoph's idea was to use FD for a write-stream so that it remains exclusive to the application and unrelated applications don't collide.

So, in this version, write-stream ids are implemented as independent resource:
- To set a file's write-stream ID to X, the app must first obtain the fd for stream X.
- That stream/fd will be unavailable if some other application has already opened it.
- If applications gets the stream fd, it can use that to set stream id for multiple files.
- It can close the stream-fd and that does not change anything for the file resource (i.e, its inode continue to carry the stream-id value that was set).

Christoph - does this match?

Here are the revised names (suggestions?):

1. FS_IOC_QUERY_MAX_WRITE_STREAM_IDS _IOR('f', 135, __u32)

Returns (in __u32) number of supported stream-ids.

2. FS_IOC_OPEN_WRITE_STREAM_ID _IOWR('f', 136, struct
fs_write_stream_open)

Open the specific write-stream id (or any available one via a flag) and return its FD. This gives right to use that stream-id.

And the following manage setting/clearing/querying write-stream on the file:

3. FS_IOC_SET_FILE_WRITE_STREAM_BY_FD _IOW('f', 137, __s32)

Sets a file's write-stream value (in inode) to whatever the stream fd (passed as __s32) carries. So one never sets scalar values into a file directly.

4. FS_IOC_QUERY_FILE_WRITE_STREAM_ID _IOR('f', 138, __u32)

Queries the write-stream value (__u32) currently assigned to the file (its inode).

5. FS_IOC_CLEAR_FILE_WRITE_STREAM_ID _IO('f', 139)

To clear (zeroing) the write-stream value on the file's inode.

Quoted 6 lines
Quoted 2 lines

Can you extract the stream id from the fd that FS_IOC_WRITE_STREAM_OPEN returns?

Can be done by implementing the ioctl handler for that stream fd, but did not feel the need of it. Do you see the utility? If yes, name can potentially be FS_IOC_QUERY_WRITE_STREAM_ID_FROM_FD.

Why not reuse FS_IOC_QUERY_FILE_WRITE_STREAM_ID?

--D

#6 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

Quoted 40 lines · Darrick J. Wong
Quoted 21 lines · Kanchan Joshi
Quoted 20 lines · Darrick J. Wong
Quoted 19 lines · Kanchan Joshi

From: Anuj Gupta<[email protected]>

Wire up the userspace interface for write stream management via four ioctls:

    FS_IOC_WRITE_STREAM_GET_MAX  query the max streams supported
    FS_IOC_WRITE_STREAM_OPEN     open a stream id, returns a stream fd
    FS_IOC_WRITE_STREAM_SET      attach the stream fd to an open file
    FS_IOC_WRITE_STREAM_GET      query the stream id value set on a file

Application should query the available streams by using FS_IOC_WRITE_STREAM_GET_MAX. If returned value is N, valid stream id for the file are 1 to N.

Application calls FS_IOC_WRITE_STREAM_OPEN to get fd for a stream-id. By default, kernel picks an available stream-id and returns the fd for it. The flag FS_WRITE_STREAM_OPEN_EXACT can be used to request a specific stream_id. This is useful if application cares about keeping a stable stream-id-to-spatial-isolation-bucket mapping across restarts.

Quoted 4 lines
Quoted 3 lines

Hmm. If FS_IOC_WRITE_STREAM_OPEN returns an fd that represents an open stream id, then why does STREAM_SET below take a pointer to a signed s32?

Not a pointer, that s32 is to pass the stream fd.

Oh, but that third argument to ioctl()/is/ supposed to be a pointer to some data. From ioctl(2):

"The third argument is an untyped pointer to memory. It's traditionally char *argp (from the days before void * was valid C), and will be so named for this discussion."

You can also see this reflected in the discussion of the R/W/WR variants of _IO:
https://docs.kernel.org/driver-api/ioctl.html

"[data_type] may be a pointer to data to be passed into the kernel (_IOW), out of the kernel (_IOR), or both (_IOWR)"

Quoted 6 lines

Granted your kernel code can treat that argp as an integer and not a __user pointer if it wants to, but that goes against most ioctl implementations. Further, subsystems that marshal ioctl information for passing through to another layer (e.g. fuse) assume that the third argument is a pointer and that it can copy sizeof(data_type) bytes at that address in and out of the kernel as part of marshalling.

Next version will refactor all ioctls to use pointer to struct.

Quoted 61 lines
Quoted 61 lines
Quoted 1 line

And why does STREAM_GET take a pointer to a u32?

This also is a scalar value and not a pointer. The returned stream-id is a u32.

Quoted 2 lines

IOWs it'd be much easier to distinguish these things if the ioctls took pointers to structs instead of u32/s32 pointers directly.

Maybe IOCTL naming is the source of confusion, because we introduced stream-fds alongside stream-ids. More on this below.

Quoted 3 lines

Also it's a little weird that STREAM_SET associates an open file with a stream fd, but STREAM_GET returns the stream*id* (not the fd) associated with an open file.

The confusion stems from keeping symmetric names for different operations. Let me explain the intent and propose revised names below.

Christoph's idea was to use FD for a write-stream so that it remains exclusive to the application and unrelated applications don't collide.

So, in this version, write-stream ids are implemented as independent resource:
- To set a file's write-stream ID to X, the app must first obtain the fd for stream X.
- That stream/fd will be unavailable if some other application has already opened it.
- If applications gets the stream fd, it can use that to set stream id for multiple files.
- It can close the stream-fd and that does not change anything for the file resource (i.e, its inode continue to carry the stream-id value that was set).

Christoph - does this match?

Here are the revised names (suggestions?):

1. FS_IOC_QUERY_MAX_WRITE_STREAM_IDS _IOR('f', 135, __u32)

Returns (in __u32) number of supported stream-ids.

2. FS_IOC_OPEN_WRITE_STREAM_ID _IOWR('f', 136, struct
fs_write_stream_open)

Open the specific write-stream id (or any available one via a flag) and return its FD. This gives right to use that stream-id.

And the following manage setting/clearing/querying write-stream on the file:

3. FS_IOC_SET_FILE_WRITE_STREAM_BY_FD _IOW('f', 137, __s32)

Sets a file's write-stream value (in inode) to whatever the stream fd (passed as __s32) carries. So one never sets scalar values into a file directly.

4. FS_IOC_QUERY_FILE_WRITE_STREAM_ID _IOR('f', 138, __u32)

Queries the write-stream value (__u32) currently assigned to the file (its inode).

5. FS_IOC_CLEAR_FILE_WRITE_STREAM_ID _IO('f', 139)

To clear (zeroing) the write-stream value on the file's inode.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Can you extract the stream id from the fd that FS_IOC_WRITE_STREAM_OPEN returns?

Can be done by implementing the ioctl handler for that stream fd, but did not feel the need of it. Do you see the utility? If yes, name can potentially be FS_IOC_QUERY_WRITE_STREAM_ID_FROM_FD.

Why not reuse FS_IOC_QUERY_FILE_WRITE_STREAM_ID?

That's for the file (with data), it requires regular file-fd. It's not working on the stream-fd. You asked for knowing the stream-id associated to stream-fd.

#7 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

In reply to Darrick J. Wong
Quoted 3 lines · Darrick J. Wong

"The third argument is an untyped pointer to memory. It's traditionally char *argp (from the days before void * was valid C), and will be so named for this discussion."

That documentation is wrong or at least very incomplete. A pointer is the typical use, but there are plenty of ioctls passing scalar values <= sizeof(long), going all the way back to early UNIX days.

Quoted 3 lines

You can also see this reflected in the discussion of the R/W/WR variants of _IO:
https://docs.kernel.org/driver-api/ioctl.html

_IO* are optional helpers, and plenty especially older ioctls are defined without using them.

Quoted 6 lines

Granted your kernel code can treat that argp as an integer and not a __user pointer if it wants to, but that goes against most ioctl implementations. Further, subsystems that marshal ioctl information for passing through to another layer (e.g. fuse) assume that the third argument is a pointer and that it can copy sizeof(data_type) bytes at that address in and out of the kernel as part of marshalling.

That is not a good assumption and things will break.

#8 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Darrick J. Wong

"The third argument is an untyped pointer to memory. It's traditionally char *argp (from the days before void * was valid C), and will be so named for this discussion."

That documentation is wrong or at least very incomplete. A pointer is the typical use, but there are plenty of ioctls passing scalar values <= sizeof(long), going all the way back to early UNIX days.

Quoted 6 lines
Quoted 3 lines

You can also see this reflected in the discussion of the R/W/WR variants of _IO:
https://docs.kernel.org/driver-api/ioctl.html

_IO* are optional helpers, and plenty especially older ioctls are defined without using them.

Well, yeah, we don't force anyone to use the _IOR/_IOW/_IOWR helpers. But since they were used here, I thought it worth pointing out.

Quoted 8 lines
Quoted 6 lines

Granted your kernel code can treat that argp as an integer and not a __user pointer if it wants to, but that goes against most ioctl implementations. Further, subsystems that marshal ioctl information for passing through to another layer (e.g. fuse) assume that the third argument is a pointer and that it can copy sizeof(data_type) bytes at that address in and out of the kernel as part of marshalling.

That is not a good assumption and things will break.

I know; fuse is a total mess w.r.t. ioctl handling because it assumes that it can just go ahead and do the userspace accesses.

--D

#9 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Darrick J. Wong

passing through to another layer (e.g. fuse) assume that the third argument is a pointer and that it can copy sizeof(data_type) bytes at that address in and out of the kernel as part of marshalling.

That is not a good assumption and things will break.

Right. Fuse server should send a whitelist of ioctl numbers it can handle.

But ioctl's are a bad interface anyway. Why not add proper syscalls instead?

Thanks,
Miklos

#10 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

In reply to Kanchan Joshi
Quoted 15 lines · Kanchan Joshi

Christoph's idea was to use FD for a write-stream so that it remains exclusive to the application and unrelated applications don't collide.

So, in this version, write-stream ids are implemented as independent resource:
- To set a file's write-stream ID to X, the app must first obtain the fd for stream X.
- That stream/fd will be unavailable if some other application has already opened it.
- If applications gets the stream fd, it can use that to set stream id for multiple files.
- It can close the stream-fd and that does not change anything for the file resource (i.e, its inode continue to carry the stream-id value that was set).

Quoted 1 line

Christoph - does this match?

Almost. I didn't really think of your last point there. If the inodes keeps using it when it is dropped that breaks the model a bit, but revoking it might make things a bit ugly and slow. Urgg.

Quoted 11 lines

Here are the revised names (suggestions?):

1. FS_IOC_QUERY_MAX_WRITE_STREAM_IDS _IOR('f', 135, __u32)

Returns (in __u32) number of supported stream-ids.

2. FS_IOC_OPEN_WRITE_STREAM_ID _IOWR('f', 136, struct
fs_write_stream_open)

Open the specific write-stream id (or any available one via a flag) and return its FD. This gives right to use that stream-id.

maybe s/OPEN/ALLOC(ATA)/ ?

Quoted 7 lines

And the following manage setting/clearing/querying write-stream on the file:

3. FS_IOC_SET_FILE_WRITE_STREAM_BY_FD _IOW('f', 137, __s32)

Sets a file's write-stream value (in inode) to whatever the stream fd (passed as __s32) carries. So one never sets scalar values into a file directly.

I'd drop the _BY_FD.

Quoted 5 lines

4. FS_IOC_QUERY_FILE_WRITE_STREAM_ID _IOR('f', 138, __u32)

Queries the write-stream value (__u32) currently assigned to the file (its inode).

I'm not sure exposing the actual ID to the user space application is a that good idea, same for passing in the wanted ID when allocating. That leaks a lot of internal details.

#11 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

Quoted 16 lines · Christoph Hellwig
Quoted 15 lines · Kanchan Joshi

Christoph's idea was to use FD for a write-stream so that it remains exclusive to the application and unrelated applications don't collide.

So, in this version, write-stream ids are implemented as independent resource:
- To set a file's write-stream ID to X, the app must first obtain the fd for stream X.
- That stream/fd will be unavailable if some other application has already opened it.
- If applications gets the stream fd, it can use that to set stream id for multiple files.
- It can close the stream-fd and that does not change anything for the file resource (i.e, its inode continue to carry the stream-id value that was set).

Quoted 5 lines
Quoted 1 line

Christoph - does this match?

Almost. I didn't really think of your last point there. If the inodes keeps using it when it is dropped that breaks the model a bit, but revoking it might make things a bit ugly and slow. Urgg.

I think it is consistent to the model that says stream fd is a distinct resource and it is a necessary lease/license to set the particular stream(-id) across files.
If the stream-fd is held, that stream-id remains exclusive to the application.
Closing means, applications is fine to release the license and underlying stream-id becomes available to other users. Application may use stream-fd license and set corresponding write-stream id into N files. We don't need to track those N files and negate the effect (clear stream-id in those N inodes) when the license was released.

Quoted 6 lines
Quoted 6 lines

Here are the revised names (suggestions?):

1. FS_IOC_QUERY_MAX_WRITE_STREAM_IDS _IOR('f', 135, __u32)

Returns (in __u32) number of supported stream-ids.

Quoted 7 lines
Quoted 5 lines

2. FS_IOC_OPEN_WRITE_STREAM_ID _IOWR('f', 136, struct
fs_write_stream_open)

Open the specific write-stream id (or any available one via a flag) and return its FD. This gives right to use that stream-id.

maybe s/OPEN/ALLOC(ATA)/ ?

Yes.
If we are doing away with stream-id, ALLOC is better. If we retain (as this version does), OPEN may still be better.

Quoted 9 lines
Quoted 7 lines

And the following manage setting/clearing/querying write-stream on the file:

3. FS_IOC_SET_FILE_WRITE_STREAM_BY_FD _IOW('f', 137, __s32)

Sets a file's write-stream value (in inode) to whatever the stream fd (passed as __s32) carries. So one never sets scalar values into a file directly.

I'd drop the _BY_FD.

Sure, I will drop.

That said, we may need a way to clear the stream-id of a file. We don't need stream-fd for that. An explicit flag here can convey that intent.

Quoted 8 lines
Quoted 5 lines

4. FS_IOC_QUERY_FILE_WRITE_STREAM_ID _IOR('f', 138, __u32)

Queries the write-stream value (__u32) currently assigned to the file (its inode).

I'm not sure exposing the actual ID to the user space application is a that good idea,

But don't we need a way to know if stream id/value is set on a file or not? This ioctl is more about observability; GET equivalent of SET. Also write stream value is not going to be there if/when in-core inode gets evicted.

Quoted 1 line

same for passing in the wanted ID when allocating.

Can drop OPEN_EXACT flag if you prefer that.
But this was for the case if application restarted and wanted its new data to go into the bucket where it was previously writing.

Quoted 1 line

That leaks a lot of internal details.

I viewed stream-id similar to how we view file-name. Application can list file names, but need the fd to do operations on that file. So, I could not understand internal details part. Could you explain? We have FIEMAP and unprivileged XFS_IOC_FSGEOMETRY already expose the placement of the file.

#12 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

Quoted 14 lines · Kanchan Joshi
Quoted 8 lines
Quoted 8 lines

- That stream/fd will be unavailable if some other application has already opened it.
- If applications gets the stream fd, it can use that to set stream id for multiple files.
- It can close the stream-fd and that does not change anything for the file resource (i.e, its inode continue to carry the stream-id value that was set).

Quoted 5 lines
Quoted 1 line

Christoph - does this match?

Almost. I didn't really think of your last point there. If the inodes keeps using it when it is dropped that breaks the model a bit, but revoking it might make things a bit ugly and slow. Urgg.

Quoted 7 lines

I think it is consistent to the model that says stream fd is a distinct resource and it is a necessary lease/license to set the particular stream(-id) across files.
If the stream-fd is held, that stream-id remains exclusive to the application.
Closing means, applications is fine to release the license and underlying stream-id becomes available to other users.

But that also means it can be reallocated to other users, which now mix data. I guess the answer might be "don't do this", but I'd love to have interfaces that aren't easy to abuse. OTOH implementing this might be quite an overhead.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

4. FS_IOC_QUERY_FILE_WRITE_STREAM_ID _IOR('f', 138, __u32)

Queries the write-stream value (__u32) currently assigned to the file (its inode).

Quoted 7 lines
Quoted 2 lines

I'm not sure exposing the actual ID to the user space application is a that good idea,

But don't we need a way to know if stream id/value is set on a file or not? This ioctl is more about observability; GET equivalent of SET. Also write stream value is not going to be there if/when in-core inode gets evicted.

Yes, but the application needs to basically set it after open anyway. Keeping any kind of state over a file descriptor that has been closed doesn't make much sense.

Quoted 5 lines
Quoted 1 line

same for passing in the wanted ID when allocating.

Can drop OPEN_EXACT flag if you prefer that.
But this was for the case if application restarted and wanted its new data to go into the bucket where it was previously writing.

But that assumes that the bucket hasn't otherwise been reused, which might not be the case.

Quoted 7 lines
Quoted 1 line

That leaks a lot of internal details.

I viewed stream-id similar to how we view file-name. Application can list file names, but need the fd to do operations on that file. So, I could not understand internal details part. Could you explain? We have FIEMAP and unprivileged XFS_IOC_FSGEOMETRY already expose the placement of the file.

There are a lot of operations on filenames.. XFS_IOC_FSGEOMETRY and are in a different register than normal file operations. If we want to expose the mapped stream IDs I'd also rather have them in a different register than the usual APIs.

#13 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

Quoted 62 lines · Christoph Hellwig
Quoted 14 lines · Kanchan Joshi
Quoted 8 lines
Quoted 8 lines

- That stream/fd will be unavailable if some other application has already opened it.
- If applications gets the stream fd, it can use that to set stream id for multiple files.
- It can close the stream-fd and that does not change anything for the file resource (i.e, its inode continue to carry the stream-id value that was set).

Quoted 5 lines
Quoted 1 line

Christoph - does this match?

Almost. I didn't really think of your last point there. If the inodes keeps using it when it is dropped that breaks the model a bit, but revoking it might make things a bit ugly and slow. Urgg.

Quoted 7 lines

I think it is consistent to the model that says stream fd is a distinct resource and it is a necessary lease/license to set the particular stream(-id) across files.
If the stream-fd is held, that stream-id remains exclusive to the application.
Closing means, applications is fine to release the license and underlying stream-id becomes available to other users.

But that also means it can be reallocated to other users, which now mix data. I guess the answer might be "don't do this", but I'd love to have interfaces that aren't easy to abuse. OTOH implementing this might be quite an overhead.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

4. FS_IOC_QUERY_FILE_WRITE_STREAM_ID _IOR('f', 138, __u32)

Queries the write-stream value (__u32) currently assigned to the file (its inode).

Quoted 7 lines
Quoted 2 lines

I'm not sure exposing the actual ID to the user space application is a that good idea,

But don't we need a way to know if stream id/value is set on a file or not? This ioctl is more about observability; GET equivalent of SET. Also write stream value is not going to be there if/when in-core inode gets evicted.

Yes, but the application needs to basically set it after open anyway. Keeping any kind of state over a file descriptor that has been closed doesn't make much sense.

Quoted 5 lines
Quoted 1 line

same for passing in the wanted ID when allocating.

Can drop OPEN_EXACT flag if you prefer that.
But this was for the case if application restarted and wanted its new data to go into the bucket where it was previously writing.

But that assumes that the bucket hasn't otherwise been reused, which might not be the case.

Quoted 7 lines
Quoted 1 line

That leaks a lot of internal details.

I viewed stream-id similar to how we view file-name. Application can list file names, but need the fd to do operations on that file. So, I could not understand internal details part. Could you explain? We have FIEMAP and unprivileged XFS_IOC_FSGEOMETRY already expose the placement of the file.

Quoted 5 lines

There are a lot of operations on filenames.. XFS_IOC_FSGEOMETRY and are in a different register than normal file operations. If we want to expose the mapped stream IDs I'd also rather have them in a different register than the usual APIs.

So based on the above discussion, do you see this as the right interface?

1. FS_IOC_WRITE_STREAM_GET_MAX : query max write streams
2. FS_IOC_WRITE_STREAM_ALLOC: allocate any write stream (if available) and return stream fd. No flag whatsoever.
3. FS_IOC_WRITE_STREAM_SET: set write stream into a file using stream-fd

#14 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

Quoted 6 lines · Kanchan Joshi

So based on the above discussion, do you see this as the right interface?

1. FS_IOC_WRITE_STREAM_GET_MAX : query max write streams
2. FS_IOC_WRITE_STREAM_ALLOC: allocate any write stream (if available) and return stream fd. No flag whatsoever.
3. FS_IOC_WRITE_STREAM_SET: set write stream into a file using stream-fd

Yes.

#15 lore raw

Re: [PATCH v4 1/6] fs: add write-stream management ioctls

In reply to Kanchan Joshi

Beside the issue pointed out in the comment there is another one that came to my mind:

Right now there is no control what application can use the write streams. Tis means a user could monopolize them, negatively affecting others, even more so when hardware write stream support is added later.

I see two ways out of this:

1) require privileges to use write streams
2) have a rlimit for the numebr of streams per user

2) is by far nicer, but 1) might be easier to get things going to then move to 2).

#16 lore raw

PATCH v4 2/6 iomap: introduce and propagate write_stream

In reply to Kanchan Joshi

Add a new write_stream field to struct iomap. Existing hole is used to place the new field.
Propagate write_stream from iomap to bio in both direct I/O and buffered writeback paths.

---
 fs/iomap/direct-io.c  | 1 +
 fs/iomap/ioend.c      | 3 +++
 include/linux/iomap.h | 2 ++
 3 files changed, 6 insertions(+)
fs/iomap/direct-io.c +1 -0
index e2cd5f92babe..b7771a25c1c7 100644
@@ -348,6 +348,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
index 0565328764c1..5ea2c68993cf 100644
@@ -165,6 +165,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);
@@ -188,6 +189,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 +2 -0
index 56b43d594e6e..4dc71da598f9 100644
@@ -132,6 +132,8 @@ 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 */
+	/* 2 bytes padding hole here */
 	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
Reviewed-by on v4 2/6
#17 lore raw

Re: [PATCH v4 2/6] iomap: introduce and propagate write_stream

include/linux/iomap.h struct iomap { · line 136

Quoted 1 line · Kanchan Joshi
include/linux/iomap.h +1 -0
+	/* 2 bytes padding hole here */

I'd drop this comment.

Otherwise looks good:

#18 lore raw

PATCH v4 3/6 xfs: implement write-stream management support

In reply to Kanchan Joshi

From: Anuj Gupta <[email protected]>

Implement FS_IOC_WRITE_STREAM_{GET_MAX,OPEN,SET,GET} handlers.

GET_MAX reports the max write stream ids.
OPEN allocates an fd for the unused write-stream and returns that. SET binds that fd to an open file.
GET reports the stream-id value set on the file.

To track used write-streams, a per-mount bitmap is kept. The stream fd's release handler clears the bit when the last reference drops.

A new i_write_stream field on xfs_inode holds the bound stream id and is propagated to the iomap during block mapping.

Write streams, filestreams, and write-life-time hints are mutually exclusive; combining any two of them returns -EINVAL:
- GET_MAX reports 0 whenever xfs_inode_is_filestream() is true, covering both mount-wide filestreams and the per-inode chattr flag. Also when the file is on the realtime device.
- SET refuses to bind a stream to a file that already has a write-life-time hint (fcntl F_SET_RW_HINT), is filestream, or is on the realtime device.
- chattr refuses to set the filestream or realtime flag on a file that already has a write stream set.

---
 fs/xfs/xfs_icache.c |   1 +
 fs/xfs/xfs_inode.c  | 155 ++++++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_inode.h  |   8 +++
 fs/xfs/xfs_ioctl.c  |  69 ++++++++++++++++++++
 fs/xfs/xfs_iomap.c  |   1 +
 fs/xfs/xfs_mount.h  |   3 +
 fs/xfs/xfs_super.c  |  12 ++++
 7 files changed, 249 insertions(+)
fs/xfs/xfs_icache.c +1 -0
index 9d8dd30bd927..7b9dda74122f 100644
@@ -129,6 +129,7 @@ xfs_inode_alloc(
 	spin_lock_init(&ip->i_ioend_lock);
 	ip->i_next_unlinked = NULLAGINO;
 	ip->i_prev_unlinked = 0;
+	ip->i_write_stream = 0;
 
 	return ip;
 }
fs/xfs/xfs_inode.c +155 -0

167 lines not on this page · raw message

fs/xfs/xfs_inode.h +8 -0
index 34c1038ebfcd..6abf82ffbf82 100644
@@ -37,6 +37,9 @@ typedef struct xfs_inode {
 	struct xfs_ifork	i_df;		/* data fork */
 	struct xfs_ifork	i_af;		/* attribute fork */
 
+	/* Write stream information */
+	uint16_t		i_write_stream;
+
 	/* Transaction and locking information. */
 	struct xfs_inode_log_item *i_itemp;	/* logging information */
 	struct rw_semaphore	i_lock;		/* inode lock */
@@ -673,4 +676,9 @@ int xfs_icreate_dqalloc(const struct xfs_icreate_args *args,
 		struct xfs_dquot **udqpp, struct xfs_dquot **gdqpp,
 		struct xfs_dquot **pdqpp);
 
+int xfs_inode_max_write_streams(struct xfs_inode *ip);
+uint16_t xfs_inode_get_write_stream(struct xfs_inode *ip);
+int xfs_inode_write_stream_open(struct xfs_inode *ip, u32 flags,
+		u32 *stream_idp);
+int xfs_inode_set_write_stream(struct xfs_inode *ip, int stream_fd);
 #endif	/* __XFS_INODE_H__ */
fs/xfs/xfs_ioctl.c +69 -0

87 lines not on this page · raw message

fs/xfs/xfs_iomap.c +1 -0
index 225c3de88d03..bf423897d916 100644
@@ -144,6 +144,7 @@ xfs_bmbt_to_iomap(
 	}
 	iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
 	iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
+	iomap->write_stream = ip->i_write_stream;
 	if (mapping_flags & IOMAP_DAX) {
 		iomap->dax_dev = target->bt_daxdev;
 	} else {
fs/xfs/xfs_mount.h +3 -0
index 66a02d1b9ad7..1376963fb7e8 100644
@@ -349,6 +349,9 @@ typedef struct xfs_mount {
 
 	/* Index of uuid record in the uuid xarray. */
 	unsigned int		m_uuid_table_index;
+
+	unsigned long		*m_streams_in_use;
+	spinlock_t		m_streams_lock;
 } xfs_mount_t;
 
 #define M_IGEO(mp)		(&(mp)->m_ino_geo)
fs/xfs/xfs_super.c +12 -0
index 8531d526fc44..0e2bf6f7b378 100644
@@ -805,6 +805,7 @@ xfs_mount_free(
 #ifdef DEBUG
 	kfree(mp->m_errortag);
 #endif
+	bitmap_free(mp->m_streams_in_use);
 	kfree(mp);
 }
 
@@ -1659,6 +1660,7 @@ xfs_fs_fill_super(
 	struct xfs_mount	*mp = sb->s_fs_info;
 	struct inode		*root;
 	int			flags = 0, error;
+	int			nr_streams;
 
 	mp->m_super = sb;
 
@@ -1708,6 +1710,15 @@ xfs_fs_fill_super(
 	if (error)
 		return error;
 
+	nr_streams = bdev_max_write_streams(mp->m_ddev_targp->bt_bdev);
+	if (nr_streams) {
+		mp->m_streams_in_use = bitmap_zalloc(nr_streams, GFP_KERNEL);
+		if (!mp->m_streams_in_use) {
+			error = -ENOMEM;
+			goto out_shutdown_devices;
+		}
+	}
+
 	if (xfs_debugfs) {
 		mp->m_debugfs = xfs_debugfs_mkdir(mp->m_super->s_id,
 						  xfs_debugfs);
@@ -2249,6 +2260,7 @@ xfs_init_fs_context(
 #endif
 
 	spin_lock_init(&mp->m_sb_lock);
+	spin_lock_init(&mp->m_streams_lock);
 	for (i = 0; i < XG_TYPE_MAX; i++)
 		xa_init(&mp->m_groups[i].xa);
 	mutex_init(&mp->m_growlock);
Signature · 1 line
2.25.1
#19 lore raw

Re: [PATCH v4 3/6] xfs: implement write-stream management support

Quoted 16 lines · Kanchan Joshi

From: Anuj Gupta <[email protected]>

Implement FS_IOC_WRITE_STREAM_{GET_MAX,OPEN,SET,GET} handlers.

GET_MAX reports the max write stream ids.
OPEN allocates an fd for the unused write-stream and returns that. SET binds that fd to an open file.
GET reports the stream-id value set on the file.

To track used write-streams, a per-mount bitmap is kept. The stream fd's release handler clears the bit when the last reference drops.

A new i_write_stream field on xfs_inode holds the bound stream id and is propagated to the iomap during block mapping.

Quoted 10 lines

Write streams, filestreams, and write-life-time hints are mutually exclusive; combining any two of them returns -EINVAL:
- GET_MAX reports 0 whenever xfs_inode_is_filestream() is true, covering both mount-wide filestreams and the per-inode chattr flag. Also when the file is on the realtime device.
- SET refuses to bind a stream to a file that already has a write-life-time hint (fcntl F_SET_RW_HINT), is filestream, or is on the realtime device.
- chattr refuses to set the filestream or realtime flag on a file that already has a write stream set.

These special "files" that represent stream ids could be generic code instaed of in xfs. AFAICT the only thing you need from xfs is a pointer from struct xfs_inode to struct (xfs_)write_stream, right?

Quoted 46 lines
---
 fs/xfs/xfs_icache.c |   1 +
 fs/xfs/xfs_inode.c  | 155 ++++++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_inode.h  |   8 +++
 fs/xfs/xfs_ioctl.c  |  69 ++++++++++++++++++++
 fs/xfs/xfs_iomap.c  |   1 +
 fs/xfs/xfs_mount.h  |   3 +
 fs/xfs/xfs_super.c  |  12 ++++
 7 files changed, 249 insertions(+)
fs/xfs/xfs_icache.c +1 -0
index 9d8dd30bd927..7b9dda74122f 100644
@@ -129,6 +129,7 @@ xfs_inode_alloc(
 	spin_lock_init(&ip->i_ioend_lock);
 	ip->i_next_unlinked = NULLAGINO;
 	ip->i_prev_unlinked = 0;
+	ip->i_write_stream = 0;
 
 	return ip;
 }
fs/xfs/xfs_inode.c +5 -0
index 15279d22a894..aafc3ffa6e0a 100644
@@ -4,6 +4,7 @@
  * All Rights Reserved.
  */
 #include <linux/iversion.h>
+#include <linux/anon_inodes.h>
 
 #include "xfs_platform.h"
 #include "xfs_fs.h"
@@ -47,6 +48,160 @@
 
 struct kmem_cache *xfs_inode_cache;
 
+int
+xfs_inode_max_write_streams(
+	struct xfs_inode	*ip)
+{

fs/xfs/xfs_inode.c line 64

Quoted 10 lines
fs/xfs/xfs_inode.c +10 -0
+	struct block_device	*bdev;
+	bool			is_filestream, is_realtime;
+
+	xfs_ilock(ip, XFS_ILOCK_SHARED);
+	is_filestream = xfs_inode_is_filestream(ip);
+	is_realtime = XFS_IS_REALTIME_INODE(ip);
+	bdev = xfs_inode_buftarg(ip)->bt_bdev;
+	xfs_iunlock(ip, XFS_ILOCK_SHARED);
+
+	if (!bdev || is_filestream || is_realtime)

Would be nice if realtime worked, or someone at least adds a comment about why it isn't (e.g. "we have something more exciting for rt/zoned filesystems") etc.

--D

Quoted 335 lines
fs/xfs/xfs_inode.c +140 -0
+		return 0;
+
+	return bdev_max_write_streams(bdev);
+}
+
+uint16_t
+xfs_inode_get_write_stream(
+	struct xfs_inode	*ip)
+{
+	uint16_t	stream_id;
+
+	xfs_ilock(ip, XFS_ILOCK_SHARED);
+	stream_id = ip->i_write_stream;
+	xfs_iunlock(ip, XFS_ILOCK_SHARED);
+
+	return stream_id;
+}
+
+struct xfs_write_stream {
+	struct xfs_mount	*mp;
+	uint16_t		stream_id;	/* 1-based */
+};
+
+static int
+xfs_write_stream_release(
+	struct inode		*inode,
+	struct file		*file)
+{
+	struct xfs_write_stream	*ws = file->private_data;
+	struct xfs_mount	*mp = ws->mp;
+
+	spin_lock(&mp->m_streams_lock);
+	clear_bit(ws->stream_id - 1, mp->m_streams_in_use);
+	spin_unlock(&mp->m_streams_lock);
+	kfree(ws);
+	return 0;
+}
+
+static const struct file_operations xfs_write_stream_fops = {
+	.release	= xfs_write_stream_release,
+	.llseek		= noop_llseek,
+};
+
+int
+xfs_inode_write_stream_open(
+	struct xfs_inode	*ip,
+	u32			flags,
+	u32			*stream_idp)
+{
+	struct xfs_mount	*mp = ip->i_mount;
+	struct xfs_write_stream	*ws;
+	int			max, slot, fd, ret;
+
+	if (flags & ~FS_WRITE_STREAM_OPEN_EXACT)
+		return -EINVAL;
+
+	max = xfs_inode_max_write_streams(ip);
+	if (!max)
+		return -EOPNOTSUPP;
+	ASSERT(mp->m_streams_in_use);
+
+	ws = kmalloc(sizeof(*ws), GFP_KERNEL);
+	if (!ws)
+		return -ENOMEM;
+
+	spin_lock(&mp->m_streams_lock);
+	if (flags & FS_WRITE_STREAM_OPEN_EXACT) {
+		if (!*stream_idp || *stream_idp > max) {
+			ret = -EINVAL;
+			goto out_unlock;
+		}
+		slot = *stream_idp - 1;
+		if (test_bit(slot, mp->m_streams_in_use)) {
+			ret = -EBUSY;
+			goto out_unlock;
+		}
+	} else {
+		slot = find_first_zero_bit(mp->m_streams_in_use, max);
+		if (slot >= max) {
+			ret = -EBUSY;
+			goto out_unlock;
+		}
+	}
+	set_bit(slot, mp->m_streams_in_use);
+	spin_unlock(&mp->m_streams_lock);
+
+	ws->mp = mp;
+	ws->stream_id = slot + 1;	/* convert to 1-based */
+
+	fd = anon_inode_getfd("[xfs_write_stream]", &xfs_write_stream_fops, ws,
+			      O_RDONLY | O_CLOEXEC);
+	if (fd < 0) {
+		spin_lock(&mp->m_streams_lock);
+		clear_bit(slot, mp->m_streams_in_use);
+		spin_unlock(&mp->m_streams_lock);
+		kfree(ws);
+		return fd;
+	}
+
+	*stream_idp = ws->stream_id;
+	return fd;
+
+out_unlock:
+	spin_unlock(&mp->m_streams_lock);
+	kfree(ws);
+	return ret;
+}
+
+int
+xfs_inode_set_write_stream(
+	struct xfs_inode	*ip,
+	int			stream_fd)
+{
+	CLASS(fd, f)(stream_fd);
+	struct xfs_write_stream	*ws;
+	int			ret = 0;
+
+	if (!fd_file(f))
+		return -EBADF;
+	if (fd_file(f)->f_op != &xfs_write_stream_fops)
+		return -EINVAL;
+
+	ws = fd_file(f)->private_data;
+	if (ws->mp != ip->i_mount)
+		return -EINVAL;
+
+	xfs_ilock(ip, XFS_ILOCK_EXCL);
+
+	if (XFS_IS_REALTIME_INODE(ip) || xfs_inode_is_filestream(ip) ||
+	    VFS_I(ip)->i_write_hint != WRITE_LIFE_NOT_SET) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	ip->i_write_stream = ws->stream_id;
+out_unlock:
+	xfs_iunlock(ip, XFS_ILOCK_EXCL);
+	return ret;
+}
+
 /*
  * These two are wrapper routines around the xfs_ilock() routine used to
  * centralize some grungy code.  They are used in places that wish to lock the
fs/xfs/xfs_inode.h +8 -0
index 34c1038ebfcd..6abf82ffbf82 100644
@@ -37,6 +37,9 @@ typedef struct xfs_inode {
 	struct xfs_ifork	i_df;		/* data fork */
 	struct xfs_ifork	i_af;		/* attribute fork */
 
+	/* Write stream information */
+	uint16_t		i_write_stream;
+
 	/* Transaction and locking information. */
 	struct xfs_inode_log_item *i_itemp;	/* logging information */
 	struct rw_semaphore	i_lock;		/* inode lock */
@@ -673,4 +676,9 @@ int xfs_icreate_dqalloc(const struct xfs_icreate_args *args,
 		struct xfs_dquot **udqpp, struct xfs_dquot **gdqpp,
 		struct xfs_dquot **pdqpp);
 
+int xfs_inode_max_write_streams(struct xfs_inode *ip);
+uint16_t xfs_inode_get_write_stream(struct xfs_inode *ip);
+int xfs_inode_write_stream_open(struct xfs_inode *ip, u32 flags,
+		u32 *stream_idp);
+int xfs_inode_set_write_stream(struct xfs_inode *ip, int stream_fd);
 #endif	/* __XFS_INODE_H__ */
fs/xfs/xfs_ioctl.c +69 -0
index 1b53701bebea..8640e6389768 100644
@@ -557,6 +557,12 @@ xfs_ioctl_setattr_xflags(
 	bool			rtflag = (fa->fsx_xflags & FS_XFLAG_REALTIME);
 	uint64_t		i_flags2;
 
+	if ((fa->fsx_xflags & FS_XFLAG_FILESTREAM) && ip->i_write_stream)
+		return -EINVAL;
+
+	if (rtflag && ip->i_write_stream)
+		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))
@@ -1200,6 +1206,59 @@ xfs_ioctl_fs_counts(
 	return 0;
 }
 
+static int
+xfs_ioc_write_stream_open(
+	struct file		*filp,
+	void __user		*arg)
+{
+	struct xfs_inode	*ip = XFS_I(file_inode(filp));
+	struct fs_write_stream_open wso;
+	int			fd;
+
+	if (copy_from_user(&wso, arg, sizeof(wso)))
+		return -EFAULT;
+
+	fd = xfs_inode_write_stream_open(ip, wso.flags, &wso.stream_id);
+	if (fd < 0)
+		return fd;
+
+	if (copy_to_user(arg, &wso, sizeof(wso)))
+		return -EFAULT;
+	return fd;
+}
+
+static int
+xfs_ioc_write_stream_set(
+	struct file		*filp,
+	unsigned long		arg)
+{
+	struct xfs_inode	*ip = XFS_I(file_inode(filp));
+
+	if (!(filp->f_mode & FMODE_WRITE))
+		return -EBADF;
+	return xfs_inode_set_write_stream(ip, (int)arg);
+}
+
+static int
+xfs_ioc_write_stream_get(
+	struct xfs_inode	*ip,
+	void __user		*arg)
+{
+	__u32 stream_id = xfs_inode_get_write_stream(ip);
+
+	return put_user(stream_id, (__u32 __user *)arg);
+}
+
+static int
+xfs_ioc_write_stream_get_max(
+	struct xfs_inode	*ip,
+	void __user		*arg)
+{
+	__u32 nr_streams = xfs_inode_max_write_streams(ip);
+
+	return put_user(nr_streams, (__u32 __user *)arg);
+}
+
 /*
  * 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.
@@ -1465,6 +1524,16 @@ xfs_file_ioctl(
 		return xfs_ioc_health_monitor(filp, arg);
 	case XFS_IOC_VERIFY_MEDIA:
 		return xfs_ioc_verify_media(filp, arg);
+	case FS_IOC_WRITE_STREAM_OPEN:
+		return xfs_ioc_write_stream_open(filp, (void __user *)arg);
+	case FS_IOC_WRITE_STREAM_SET:
+		return xfs_ioc_write_stream_set(filp, p);
+	case FS_IOC_WRITE_STREAM_GET:
+		return xfs_ioc_write_stream_get(XFS_I(file_inode(filp)),
+						(void __user *)arg);
+	case FS_IOC_WRITE_STREAM_GET_MAX:
+		return xfs_ioc_write_stream_get_max(XFS_I(file_inode(filp)),
+						    (void __user *)arg);
 
 	default:
 		return -ENOTTY;
fs/xfs/xfs_iomap.c +1 -0
index 225c3de88d03..bf423897d916 100644
@@ -144,6 +144,7 @@ xfs_bmbt_to_iomap(
 	}
 	iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
 	iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
+	iomap->write_stream = ip->i_write_stream;
 	if (mapping_flags & IOMAP_DAX) {
 		iomap->dax_dev = target->bt_daxdev;
 	} else {
fs/xfs/xfs_mount.h +3 -0
index 66a02d1b9ad7..1376963fb7e8 100644
@@ -349,6 +349,9 @@ typedef struct xfs_mount {
 
 	/* Index of uuid record in the uuid xarray. */
 	unsigned int		m_uuid_table_index;
+
+	unsigned long		*m_streams_in_use;
+	spinlock_t		m_streams_lock;
 } xfs_mount_t;
 
 #define M_IGEO(mp)		(&(mp)->m_ino_geo)
fs/xfs/xfs_super.c +12 -0
index 8531d526fc44..0e2bf6f7b378 100644
@@ -805,6 +805,7 @@ xfs_mount_free(
 #ifdef DEBUG
 	kfree(mp->m_errortag);
 #endif
+	bitmap_free(mp->m_streams_in_use);
 	kfree(mp);
 }
 
@@ -1659,6 +1660,7 @@ xfs_fs_fill_super(
 	struct xfs_mount	*mp = sb->s_fs_info;
 	struct inode		*root;
 	int			flags = 0, error;
+	int			nr_streams;
 
 	mp->m_super = sb;
 
@@ -1708,6 +1710,15 @@ xfs_fs_fill_super(
 	if (error)
 		return error;
 
+	nr_streams = bdev_max_write_streams(mp->m_ddev_targp->bt_bdev);
+	if (nr_streams) {
+		mp->m_streams_in_use = bitmap_zalloc(nr_streams, GFP_KERNEL);
+		if (!mp->m_streams_in_use) {
+			error = -ENOMEM;
+			goto out_shutdown_devices;
+		}
+	}
+
 	if (xfs_debugfs) {
 		mp->m_debugfs = xfs_debugfs_mkdir(mp->m_super->s_id,
 						  xfs_debugfs);
@@ -2249,6 +2260,7 @@ xfs_init_fs_context(
 #endif
 
 	spin_lock_init(&mp->m_sb_lock);
+	spin_lock_init(&mp->m_streams_lock);
 	for (i = 0; i < XG_TYPE_MAX; i++)
 		xa_init(&mp->m_groups[i].xa);
 	mutex_init(&mp->m_growlock);
Signature · 1 line
2.25.1
#20 lore raw

Re: [PATCH v4 3/6] xfs: implement write-stream management support

Quoted 10 lines · Darrick J. Wong
Quoted 10 lines

Write streams, filestreams, and write-life-time hints are mutually exclusive; combining any two of them returns -EINVAL:
- GET_MAX reports 0 whenever xfs_inode_is_filestream() is true, covering both mount-wide filestreams and the per-inode chattr flag. Also when the file is on the realtime device.
- SET refuses to bind a stream to a file that already has a write-life-time hint (fcntl F_SET_RW_HINT), is filestream, or is on the realtime device.
- chattr refuses to set the filestream or realtime flag on a file that already has a write stream set.

Quoted 3 lines

These special "files" that represent stream ids could be generic code instaed of in xfs. AFAICT the only thing you need from xfs is a pointer from struct xfs_inode to struct (xfs_)write_stream, right?

Right. Is it fine if we come to it when everything else is settled. I was hoping to lift common things up when write-stream is applied on another FS.

Quoted 52 lines
Quoted 52 lines
---
  fs/xfs/xfs_icache.c |   1 +
  fs/xfs/xfs_inode.c  | 155 ++++++++++++++++++++++++++++++++++++++++++++
  fs/xfs/xfs_inode.h  |   8 +++
  fs/xfs/xfs_ioctl.c  |  69 ++++++++++++++++++++
  fs/xfs/xfs_iomap.c  |   1 +
  fs/xfs/xfs_mount.h  |   3 +
  fs/xfs/xfs_super.c  |  12 ++++
  7 files changed, 249 insertions(+)
fs/xfs/xfs_icache.c +1 -0
index 9d8dd30bd927..7b9dda74122f 100644
@@ -129,6 +129,7 @@ xfs_inode_alloc(
  	spin_lock_init(&ip->i_ioend_lock);
  	ip->i_next_unlinked = NULLAGINO;
  	ip->i_prev_unlinked = 0;
+	ip->i_write_stream = 0;
 
  	return ip;
  }
fs/xfs/xfs_inode.c +10 -0
index 15279d22a894..aafc3ffa6e0a 100644
@@ -4,6 +4,7 @@
   * All Rights Reserved.
   */
  #include <linux/iversion.h>
+#include <linux/anon_inodes.h>
 
  #include "xfs_platform.h"
  #include "xfs_fs.h"
@@ -47,6 +48,160 @@
 
  struct kmem_cache *xfs_inode_cache;
 
+int
+xfs_inode_max_write_streams(
+	struct xfs_inode	*ip)
+{
+	struct block_device	*bdev;
+	bool			is_filestream, is_realtime;
+
+	xfs_ilock(ip, XFS_ILOCK_SHARED);
+	is_filestream = xfs_inode_is_filestream(ip);
Quoted 8 lines

fs/xfs/xfs_inode.c line 64

Quoted 5 lines
fs/xfs/xfs_inode.c +5 -0
+	is_realtime = XFS_IS_REALTIME_INODE(ip);
+	bdev = xfs_inode_buftarg(ip)->bt_bdev;
+	xfs_iunlock(ip, XFS_ILOCK_SHARED);
+
+	if (!bdev || is_filestream || is_realtime)

Would be nice if realtime worked, or someone at least adds a comment about why it isn't (e.g. "we have something more exciting for rt/zoned filesystems") etc.

Will add, and that possibility is there.

Since each allocator (default,filestream, rt, zoned) has a different logical placement, a single write-hint based scheme does not fit all.

For non-zoned RT device: write-stream based physical placement can just work, it's mostly about lifting the checks that we have now. And adding logical placement (use write-stream to choose RTG similar to how we pick AG) is straight. But would you prefer that over default round-robin rotor?

For zoned RT device: two options
- Do nothing as write-hint based zone selection is already in place.
- Or publish N write-streams (corresponds to N isolation buckets over >N open zones), and use file's write-stream value for zone selction.

Current write-hint based scheme, based on xfs_zoned_hint_score[][] matrix, supports 4 isolation buckets. An application may need more, say
6. And two such applications will require 12. Zoned-RT can care more isolation buckets, but write-hints don't allow to publish that.

Also, two unrelated applications that both pick WRITE_LIFE_SHORT bucket may land in the same zone. This goes back to FD based interface for write-steam.

Not having enough buckets, and having multi-tenant mixing - both can improved with the write-stream.
But I might be missing many zoned-xfs details; Maybe Christoph could weigh in?

#21 lore raw

Re: [PATCH v4 3/6] xfs: implement write-stream management support

Quoted 11 lines · Kanchan Joshi
Quoted 10 lines · Darrick J. Wong
Quoted 10 lines

Write streams, filestreams, and write-life-time hints are mutually exclusive; combining any two of them returns -EINVAL:
- GET_MAX reports 0 whenever xfs_inode_is_filestream() is true, covering both mount-wide filestreams and the per-inode chattr flag. Also when the file is on the realtime device.
- SET refuses to bind a stream to a file that already has a write-life-time hint (fcntl F_SET_RW_HINT), is filestream, or is on the realtime device.
- chattr refuses to set the filestream or realtime flag on a file that already has a write stream set.

Quoted 7 lines
Quoted 3 lines

These special "files" that represent stream ids could be generic code instaed of in xfs. AFAICT the only thing you need from xfs is a pointer from struct xfs_inode to struct (xfs_)write_stream, right?

Right. Is it fine if we come to it when everything else is settled. I was hoping to lift common things up when write-stream is applied on another FS.

Yes, I think this really needs to move to common code. If we can find a hole in struct inode, moving the write_stream id to that would probably clean that up a lot.

#22 lore raw

Re: [PATCH v4 3/6] xfs: implement write-stream management support

In reply to Kanchan Joshi
Quoted 5 lines
fs/xfs/xfs_inode.c +5 -0
+int
+xfs_inode_max_write_streams(
+	struct xfs_inode	*ip)
+{
+	struct block_device	*bdev;

fs/xfs/xfs_inode.c line 65

Quoted 10 lines
fs/xfs/xfs_inode.c +10 -0
+	bool			is_filestream, is_realtime;
+
+	xfs_ilock(ip, XFS_ILOCK_SHARED);
+	is_filestream = xfs_inode_is_filestream(ip);
+	is_realtime = XFS_IS_REALTIME_INODE(ip);
+	bdev = xfs_inode_buftarg(ip)->bt_bdev;
+	xfs_iunlock(ip, XFS_ILOCK_SHARED);
+
+	if (!bdev || is_filestream || is_realtime)
+		return 0;

All this information is stale as soon as soon as the lock is dropped. Also a NULL bdev can only happen for the in-memory buftarg used by repair, so no need to check this.

fs/xfs/xfs_inode.c line 78

Quoted 9 lines
fs/xfs/xfs_inode.c +9 -0
+uint16_t
+xfs_inode_get_write_stream(
+	struct xfs_inode	*ip)
+{
+	uint16_t	stream_id;
+
+	xfs_ilock(ip, XFS_ILOCK_SHARED);
+	stream_id = ip->i_write_stream;
+	xfs_iunlock(ip, XFS_ILOCK_SHARED);

Same here. READ_ONCE/WRITE_ONCE might be a better idea to simply avoid the lock at read time.

fs/xfs/xfs_inode.c line 97

Quoted 6 lines
fs/xfs/xfs_inode.c +6 -0
+{
+	struct xfs_write_stream	*ws = file->private_data;
+	struct xfs_mount	*mp = ws->mp;
+
+	spin_lock(&mp->m_streams_lock);
+	clear_bit(ws->stream_id - 1, mp->m_streams_in_use);

Given that all accesses to the m_streams_in_use bitmap use a lock, there is no need for the atomic bitops. We could use the __-versions or just code the logic. Or not bother because none of this is performance critical :)

fs/xfs/xfs_inode.c line 194

Quoted 8 lines
fs/xfs/xfs_inode.c +8 -0
+	ws = fd_file(f)->private_data;
+	if (ws->mp != ip->i_mount)
+		return -EINVAL;
+
+	xfs_ilock(ip, XFS_ILOCK_EXCL);
+
+	if (XFS_IS_REALTIME_INODE(ip) || xfs_inode_is_filestream(ip) ||
+	    VFS_I(ip)->i_write_hint != WRITE_LIFE_NOT_SET) {

If we want to enforce a hint vs stream exlusion we'd also need to do this when setting the hints. Another approach might be to allow them to coexist, and set a lifetime hint on each stream, which the inode must match. Although I'm not sure this would be all that useful.

#23 lore raw

Re: [PATCH v4 3/6] xfs: implement write-stream management support

In reply to Kanchan Joshi

fs/xfs/xfs_super.c xfs_fs_fill_super( · line 1713

Quoted 1 line · Kanchan Joshi
fs/xfs/xfs_super.c +1 -0
+	nr_streams = bdev_max_write_streams(mp->m_ddev_targp->bt_bdev);

Returning to this. Given that we are talking about pure software write streams, does this make sense? This limits the use to devices that do report hardware write streams despite not using those. Most devices will not report those.

#24 lore raw

Re: [PATCH v4 3/6] xfs: implement write-stream management support

Quoted 6 lines · Christoph Hellwig

fs/xfs/xfs_super.c xfs_fs_fill_super( · line 1713

Quoted 1 line
fs/xfs/xfs_super.c +1 -0
+	nr_streams = bdev_max_write_streams(mp->m_ddev_targp->bt_bdev);

Returning to this. Given that we are talking about pure software write streams, does this make sense? This limits the use to devices that do report hardware write streams despite not using those. Most devices will not report those.

Yeah, I enabled hardware streams first and define software streams as a fallback when the block device has none. I can swap the order if that reads better.

#25 lore raw

PATCH v4 4/6 xfs: generic AG set based steering

In reply to Kanchan Joshi

Improve allocator concurrency and reduce interleaving by introducing fixed sized AG set.
Use low bits of the inode as a hash to select AG within the AG set. Overall, a file will try to use the same AG (and contiguity is maintained), but multiple files will be spread across all AGs in the target AG set.

---
 fs/xfs/libxfs/xfs_bmap.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
fs/xfs/libxfs/xfs_bmap.c +38 -0
index d64defeda645..fd1a3aa4ad3f 100644
@@ -3192,6 +3192,36 @@ xfs_bmap_select_minlen(
 	return args->maxlen;
 }
 
+#define	GENERIC_AG_SET_SZ	(2)
+
+static inline xfs_agnumber_t
+xfs_default_ag_set_size(
+	struct xfs_inode	*ip)
+{
+	struct xfs_mount	*mp = ip->i_mount;
+
+	return min_t(xfs_agnumber_t, GENERIC_AG_SET_SZ, mp->m_sb.sb_agcount);
+}
+
+static xfs_agnumber_t
+xfs_ag_to_ag_set(
+	struct xfs_bmalloca	*ap,
+	xfs_agnumber_t		base_agno)
+{
+	struct xfs_inode	*ip = ap->ip;
+	struct xfs_mount	*mp = ip->i_mount;
+	xfs_agnumber_t		set_size;
+
+	/* Apply fanning only for regular file data */
+	if (!(ap->datatype & XFS_ALLOC_USERDATA))
+		return base_agno;
+
+	set_size = xfs_default_ag_set_size(ip);
+	/* Fan out within the AG set using low bits of the inode */
+	return (base_agno + (XFS_INO_TO_AGINO(mp, I_INO(ip)) % set_size)) %
+		mp->m_sb.sb_agcount;
+}
+
 static int
 xfs_bmap_btalloc_select_lengths(
 	struct xfs_bmalloca	*ap,
@@ -3587,8 +3617,16 @@ xfs_bmap_btalloc_best_length(
 {
 	xfs_extlen_t		blen = 0;
 	int			error;
+	xfs_agnumber_t		target_ag, start_ag;
 
 	ap->blkno = XFS_INODE_TO_FSB(ap->ip);
+
+	/* fan out initial AG across the generic AG set */
+	start_ag = XFS_FSB_TO_AGNO(args->mp, ap->blkno);
+	target_ag = xfs_ag_to_ag_set(ap, start_ag);
+	if (target_ag != start_ag)
+		ap->blkno = XFS_AGB_TO_FSB(args->mp, target_ag, 0);
+
 	if (!xfs_bmap_adjacent(ap))
 		ap->eof = false;
 
Signature · 1 line
2.25.1
#26 lore raw

Re: [PATCH v4 4/6] xfs: generic AG set based steering

Quoted 18 lines · Kanchan Joshi

Improve allocator concurrency and reduce interleaving by introducing fixed sized AG set.
Use low bits of the inode as a hash to select AG within the AG set. Overall, a file will try to use the same AG (and contiguity is maintained), but multiple files will be spread across all AGs in the target AG set.

---
 fs/xfs/libxfs/xfs_bmap.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
fs/xfs/libxfs/xfs_bmap.c +0 -0
index d64defeda645..fd1a3aa4ad3f 100644

fs/xfs/libxfs/xfs_bmap.c xfs_bmap_select_minlen( · line 3195

Quoted 4 lines
fs/xfs/libxfs/xfs_bmap.c +1 -0
@@ -3192,6 +3192,36 @@ xfs_bmap_select_minlen(
 	return args->maxlen;
 }
 
+#define	GENERIC_AG_SET_SZ	(2)

What does this define?

fs/xfs/libxfs/xfs_bmap.c xfs_bmap_select_minlen( · line 3203

Quoted 8 lines
fs/xfs/libxfs/xfs_bmap.c +8 -0
+
+static inline xfs_agnumber_t
+xfs_default_ag_set_size(
+	struct xfs_inode	*ip)
+{
+	struct xfs_mount	*mp = ip->i_mount;
+
+	return min_t(xfs_agnumber_t, GENERIC_AG_SET_SZ, mp->m_sb.sb_agcount);

Because I'm not sure what it means on a single-AG filesystem.

Quoted 27 lines
fs/xfs/libxfs/xfs_bmap.c +21 -0
+}
+
+static xfs_agnumber_t
+xfs_ag_to_ag_set(
+	struct xfs_bmalloca	*ap,
+	xfs_agnumber_t		base_agno)
+{
+	struct xfs_inode	*ip = ap->ip;
+	struct xfs_mount	*mp = ip->i_mount;
+	xfs_agnumber_t		set_size;
+
+	/* Apply fanning only for regular file data */
+	if (!(ap->datatype & XFS_ALLOC_USERDATA))
+		return base_agno;
+
+	set_size = xfs_default_ag_set_size(ip);
+	/* Fan out within the AG set using low bits of the inode */
+	return (base_agno + (XFS_INO_TO_AGINO(mp, I_INO(ip)) % set_size)) %
+		mp->m_sb.sb_agcount;
+}
+
 static int
 xfs_bmap_btalloc_select_lengths(
 	struct xfs_bmalloca	*ap,
@@ -3587,8 +3617,16 @@ xfs_bmap_btalloc_best_length(
 {
 	xfs_extlen_t		blen = 0;

fs/xfs/libxfs/xfs_bmap.c xfs_bmap_btalloc_best_length( · line 3628

Quoted 10 lines
fs/xfs/libxfs/xfs_bmap.c +7 -0
 	int			error;
+	xfs_agnumber_t		target_ag, start_ag;
 
 	ap->blkno = XFS_INODE_TO_FSB(ap->ip);
+
+	/* fan out initial AG across the generic AG set */
+	start_ag = XFS_FSB_TO_AGNO(args->mp, ap->blkno);
+	target_ag = xfs_ag_to_ag_set(ap, start_ag);
+	if (target_ag != start_ag)
+		ap->blkno = XFS_AGB_TO_FSB(args->mp, target_ag, 0);

/me wonders, if xfs_bmap_rtalloc looked at ap->blkno for a hint the way that the data device allocator does, then would it be trivial to have write streams on the rt device too?

I guess the tricky part would be figuring out what to do if you ever want to switch a file between rt and data devices -- presumably you'd just reset the write stream id to the default, but I guess you could reject such a switch if the id had been set explicitly.

--D

Quoted 8 lines
fs/xfs/libxfs/xfs_bmap.c +1 -0
+
 	if (!xfs_bmap_adjacent(ap))
 		ap->eof = false;
 
Signature · 1 line
2.25.1
#27 lore raw

Re: [PATCH v4 4/6] xfs: generic AG set based steering

Quoted 22 lines · Darrick J. Wong
Quoted 21 lines · Kanchan Joshi

Improve allocator concurrency and reduce interleaving by introducing fixed sized AG set.
Use low bits of the inode as a hash to select AG within the AG set. Overall, a file will try to use the same AG (and contiguity is maintained), but multiple files will be spread across all AGs in the target AG set.

---
  fs/xfs/libxfs/xfs_bmap.c | 38 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 38 insertions(+)
fs/xfs/libxfs/xfs_bmap.c +0 -0
index d64defeda645..fd1a3aa4ad3f 100644
@@ -3192,6 +3192,36 @@ xfs_bmap_select_minlen(
  	return args->maxlen;
  }
 
Quoted 2 lines

fs/xfs/libxfs/xfs_bmap.c xfs_bmap_select_minlen( · line 3195

Quoted 1 line
fs/xfs/libxfs/xfs_bmap.c +1 -0
+#define	GENERIC_AG_SET_SZ	(2)

What does this define?

We need more than 1 AG to do inode-based fanning of files and improve concurrency (AGF lock). This is the minimum required to do any improvement, in the default allocator.
This 'minimum fan-out' is what Dave suggested to extract out of write-stream and apply to two other allocators- default (which is what you see now), and filestream (future work).

And this can be reviewed as an independent patch as it does not require any other work done in the series. No application input is required, this is automatic optimization.

The write-stream allocator (next patch), uses a different AG-set of size NR_AGs/NR_WRITE_STREAMs. And that is an explicit (application input based) allocator.

Quoted 9 lines

fs/xfs/libxfs/xfs_bmap.c xfs_bmap_select_minlen( · line 3203

Quoted 8 lines
fs/xfs/libxfs/xfs_bmap.c +8 -0
+
+static inline xfs_agnumber_t
+xfs_default_ag_set_size(
+	struct xfs_inode	*ip)
+{
+	struct xfs_mount	*mp = ip->i_mount;
+
+	return min_t(xfs_agnumber_t, GENERIC_AG_SET_SZ, mp->m_sb.sb_agcount);

Because I'm not sure what it means on a single-AG filesystem.

On a single-AG filesystem, this will do nothing. The base-AG input given by the default allocator will not get changed.

But we get improvements on multi-AG filsystem; not sure if you saw the numbers in the cover letter. Corresponding to the 'generic AG-set' below:

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
generic AG-set: 93 KIOPS (+126%)
write-stream AG-set: 227 KIOPS (+453%)
here, 16 files are assigned 4 unique 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, generic AG-set size = 2, write-stream AG-set size = 4

base: 59 KIOPS
generic AG-set: 94 KIOPS (+59%)
write-stream AG-set: 112 KIOPS (+89%)
here, 4 files are assigned single write-stream

Quoted 30 lines
Quoted 30 lines
fs/xfs/libxfs/xfs_bmap.c +22 -0
+}
+
+static xfs_agnumber_t
+xfs_ag_to_ag_set(
+	struct xfs_bmalloca	*ap,
+	xfs_agnumber_t		base_agno)
+{
+	struct xfs_inode	*ip = ap->ip;
+	struct xfs_mount	*mp = ip->i_mount;
+	xfs_agnumber_t		set_size;
+
+	/* Apply fanning only for regular file data */
+	if (!(ap->datatype & XFS_ALLOC_USERDATA))
+		return base_agno;
+
+	set_size = xfs_default_ag_set_size(ip);
+	/* Fan out within the AG set using low bits of the inode */
+	return (base_agno + (XFS_INO_TO_AGINO(mp, I_INO(ip)) % set_size)) %
+		mp->m_sb.sb_agcount;
+}
+
  static int
  xfs_bmap_btalloc_select_lengths(
  	struct xfs_bmalloca	*ap,
@@ -3587,8 +3617,16 @@ xfs_bmap_btalloc_best_length(
  {
  	xfs_extlen_t		blen = 0;
  	int			error;
+	xfs_agnumber_t		target_ag, start_ag;
 
Quoted 10 lines

fs/xfs/libxfs/xfs_bmap.c xfs_bmap_btalloc_best_length( · line 3628

Quoted 7 lines
fs/xfs/libxfs/xfs_bmap.c +6 -0
  	ap->blkno = XFS_INODE_TO_FSB(ap->ip);
+
+	/* fan out initial AG across the generic AG set */
+	start_ag = XFS_FSB_TO_AGNO(args->mp, ap->blkno);
+	target_ag = xfs_ag_to_ag_set(ap, start_ag);
+	if (target_ag != start_ag)
+		ap->blkno = XFS_AGB_TO_FSB(args->mp, target_ag, 0);

/me wonders, if xfs_bmap_rtalloc looked at ap->blkno for a hint the way that the data device allocator does, then would it be trivial to have write streams on the rt device too?

Trying to understand if this kind of fanning out (this patch) or the logical placement (patch 5 with write-stream based AG placement) help us solve anything for rt device?
For regular RT device, the round-robin rotor will distribute files into multiple RTGs. So we don't have AGF like contention.
And using write-stream to explicitly send multiple files to a single RTG will do more harm than good?

And for zoned RT device, the placement is happening via write-hint.

Am I missing something that you have in mind?

All the above is for logical isolation. For physical isolation, your comments already help to establish that write-stream can be made to work. For the regular RT device sitting on top of NVMe (FDP-enabled). That needs few changes, but in previous patch (#3).

Quoted 4 lines

I guess the tricky part would be figuring out what to do if you ever want to switch a file between rt and data devices -- presumably you'd just reset the write stream id to the default, but I guess you could reject such a switch if the id had been set explicitly.

Yes, we do such checks in patch 3 now also.

#28 lore raw

Re: [PATCH v4 4/6] xfs: generic AG set based steering

In reply to Kanchan Joshi
Quoted 5 lines · Kanchan Joshi

Improve allocator concurrency and reduce interleaving by introducing fixed sized AG set.
Use low bits of the inode as a hash to select AG within the AG set. Overall, a file will try to use the same AG (and contiguity is maintained), but multiple files will be spread across all AGs in the target AG set.

I'm a bit worried this kind of magic - the inodes close to each other might not all have data or very different amounts of data.

I suspect the root cause this helps with is that the XFS initial data placement algorithm that tries to place file data next to the inode is actively harmful these days. So doing an initial AG selection similar to how the RT device does the initial RTG selection might be more useful here (and thinking about moving to an inode32-like scheme where small metadata with a lot of churn is constrained to a few AGs).

#29 lore raw

Re: [PATCH v4 4/6] xfs: generic AG set based steering

Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Kanchan Joshi

Improve allocator concurrency and reduce interleaving by introducing fixed sized AG set.
Use low bits of the inode as a hash to select AG within the AG set. Overall, a file will try to use the same AG (and contiguity is maintained), but multiple files will be spread across all AGs in the target AG set.

I'm a bit worried this kind of magic - the inodes close to each other might not all have data or very different amounts of data.

Quoted 7 lines

I suspect the root cause this helps with is that the XFS initial data placement algorithm that tries to place file data next to the inode is actively harmful these days. So doing an initial AG selection similar to how the RT device does the initial RTG selection might be more useful here (and thinking about moving to an inode32-like scheme where small metadata with a lot of churn is constrained to a few AGs).

Thinking to drop this for now, as this may need a distinct series.

This patch was here because stream based AG-set extended what this patch was doing (i.e., the minimal fixed-size AG-set). That relationship goes with the change you mentioned.

Replacing default AG allocator with an automatic RG-style rotor (no user-supplied write-stream) might be a radical change in terms of impact.

#30 lore raw

PATCH v4 5/6 xfs: write stream based AG placement

In reply to Kanchan Joshi

When write stream is set on the file, choose the AG set based on the write stream value.

Isolating distinct write streams into dedicated allocation groups helps in reducing the block interleaving of concurrent writers. Keeping these streams spatially separated reduces AGF lock contention and logical file fragmentation.

If AGs are fewer than write streams, write streams are distributed into available AGs in round robin fashion.
If not, available AGs are partitioned into write streams. The write-stream value is used to derive the AG set, and low bits of the inode is used to select the AG within the AG set.

While each stream provides the isolation, the 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 2

If AGs can not be evenly distributed among streams, the last stream will absorb the remaining AGs.

Note that there are no hard boundaries; write-stream only provides explicit routing hint to xfs allocator. We still preserve file contiguity, and the full space can be utilized even with a single stream.

---
 fs/xfs/libxfs/xfs_bmap.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_bmap.c +37 -1
index fd1a3aa4ad3f..872a4abd2315 100644
@@ -3203,6 +3203,38 @@ xfs_default_ag_set_size(
 	return min_t(xfs_agnumber_t, GENERIC_AG_SET_SZ, mp->m_sb.sb_agcount);
 }
 
+static xfs_agnumber_t
+xfs_inode_write_stream_ag_set(
+	struct xfs_inode	*ip,
+	xfs_agnumber_t		*target_agno)
+{
+	struct xfs_mount	*mp = ip->i_mount;
+	uint32_t		nr_streams = xfs_inode_max_write_streams(ip);
+	uint32_t		stream_id = ip->i_write_stream;
+	uint32_t		nr_ags = mp->m_sb.sb_agcount;
+	xfs_agnumber_t		set_size;
+
+
+	if (!nr_streams)
+		return xfs_default_ag_set_size(ip);
+
+	stream_id -= 1; /* For 0-based math; stream-ids are 1-based */
+	set_size = nr_ags / nr_streams;
+
+	if (set_size) {
+		*target_agno = stream_id * set_size;
+		/* uneven distribution, last stream will cover extra AGs */
+		if (stream_id == nr_streams - 1)
+			set_size = nr_ags - *target_agno;
+	} else {
+		/* for the case when we have fewer AGs than streams */
+		*target_agno = stream_id % nr_ags;
+		set_size = 1;
+	}
+
+	return set_size;
+}
+
 static xfs_agnumber_t
 xfs_ag_to_ag_set(
 	struct xfs_bmalloca	*ap,
@@ -3216,7 +3248,11 @@ xfs_ag_to_ag_set(
 	if (!(ap->datatype & XFS_ALLOC_USERDATA))
 		return base_agno;
 
-	set_size = xfs_default_ag_set_size(ip);
+	if (ip->i_write_stream)
+		set_size = xfs_inode_write_stream_ag_set(ip, &base_agno);
+	else
+		set_size = xfs_default_ag_set_size(ip);
+
 	/* Fan out within the AG set using low bits of the inode */
 	return (base_agno + (XFS_INO_TO_AGINO(mp, I_INO(ip)) % set_size)) %
 		mp->m_sb.sb_agcount;
Signature · 1 line
2.25.1
#31 lore raw

Re: [PATCH v4 5/6] xfs: write stream based AG placement

Quoted 2 lines · Kanchan Joshi

If AGs are fewer than write streams, write streams are distributed into available AGs in round robin fashion.

I think we should just limit the number of available write streams to the number of AG, as otherwise we'll get into a bit of a mess.

And for the case of the hardware write streams query them in mkfs to align the AG geometry to that.

fs/xfs/libxfs/xfs_bmap.c xfs_default_ag_set_size( · line 3218

Quoted 7 lines
fs/xfs/libxfs/xfs_bmap.c +7 -0
+	uint32_t		nr_streams = xfs_inode_max_write_streams(ip);
+	uint32_t		stream_id = ip->i_write_stream;
+	uint32_t		nr_ags = mp->m_sb.sb_agcount;
+	xfs_agnumber_t		set_size;
+
+
+	if (!nr_streams)

Double empty line.

#32 lore raw

Re: [PATCH v4 5/6] xfs: write stream based AG placement

Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Kanchan Joshi

If AGs are fewer than write streams, write streams are distributed into available AGs in round robin fashion.

I think we should just limit the number of available write streams to the number of AG, as otherwise we'll get into a bit of a mess.

To confirm: given above quoting, for the other two cases (i.e., Streams <= AGs), are you open to stream-based AG-sets modulo inode hashing (that you suggested to skip in previous patch)?
And is this point only for the case when Streams > AGs?

This case can happen for hw streams, but for pure software streams, we will not hit this case because the filesystem determines the stream count based on the AG count.

Quoted 2 lines

And for the case of the hardware write streams query them in mkfs to align the AG geometry to that.

If this is only for the streams > AGs case, the filesystem can just clamp down to match, turning it into a 'streams = AGs' case. And that means no changes in mkfs. Is that fine?
Related benefit is to skip tricky mkfs AG calculations right for future device sizes and varying stream counts.

#33 lore raw

Re: [PATCH v4 5/6] xfs: write stream based AG placement

Quoted 11 lines · Kanchan Joshi
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Kanchan Joshi

If AGs are fewer than write streams, write streams are distributed into available AGs in round robin fashion.

I think we should just limit the number of available write streams to the number of AG, as otherwise we'll get into a bit of a mess.

To confirm: given above quoting, for the other two cases (i.e., Streams <= AGs), are you open to stream-based AG-sets modulo inode hashing (that you suggested to skip in previous patch)?

Yes. Even inode hashing might be ok, but I think it mostly papers over other issues.

Quoted 1 line

And is this point only for the case when Streams > AGs?

Yes.

Quoted 3 lines

This case can happen for hw streams, but for pure software streams, we will not hit this case because the filesystem determines the stream count based on the AG count.

Right.

Quoted 7 lines
Quoted 3 lines

And for the case of the hardware write streams query them in mkfs to align the AG geometry to that.

If this is only for the streams > AGs case, the filesystem can just clamp down to match, turning it into a 'streams = AGs' case. And that means no changes in mkfs. Is that fine?

Well, it is fine. But number of available write streams might be a very useful hint to mkfs as mkfs tries to guess the available concurrent, and the write streams are how the device exposes that.

#34 lore raw

PATCH v4 6/6 xfs: introduce software write streams

In reply to Kanchan Joshi

Even when the underlying block device does not advertise write streams, XFS can choose do so, as that enables logical spatial isolation and dynamic AG-set based concurrency for the standard storage, excluding rtvolume.

Use AG count based heuristic to derive AG set size and software streams. Larger filesystem (i.e., more AGs) get wider fanout (i.e., larger AG-set).

XFS_SW_WRITE_STREAMS_MAX bounds the software fallback. The mount-time write stream bitmap allocation now also falls back to it when the data device reports no hardware streams.

---
 fs/xfs/xfs_inode.c | 22 +++++++++++++++++++++-
 fs/xfs/xfs_mount.h |  2 ++
 fs/xfs/xfs_super.c | 12 ++++++------
 3 files changed, 29 insertions(+), 7 deletions(-)
fs/xfs/xfs_inode.c +21 -1
index aafc3ffa6e0a..638bee3e4ac2 100644
@@ -53,6 +53,9 @@ xfs_inode_max_write_streams(
 	struct xfs_inode	*ip)
 {
 	struct block_device	*bdev;
+	struct xfs_mount	*mp = ip->i_mount;
+	int nr_streams;
+	xfs_agnumber_t nr_ags, ag_set_size;
 	bool			is_filestream, is_realtime;
 
 	xfs_ilock(ip, XFS_ILOCK_SHARED);
@@ -64,7 +67,24 @@ xfs_inode_max_write_streams(
 	if (!bdev || is_filestream || is_realtime)
 		return 0;
 
-	return bdev_max_write_streams(bdev);
+	nr_streams = bdev_max_write_streams(bdev);
+	if (nr_streams > 0)
+		return nr_streams;
+	/*
+	 * Enable software-only streams if hardware streams are not available.
+	 * This helps to
+	 * - improve isolation; reduce allocation interleaving.
+	 * - improve concurrency using AG-set based steering within and across streams.
+	 */
+	nr_ags = mp->m_sb.sb_agcount;
+	if (nr_ags >= 16)
+		ag_set_size = 4;
+	else if (nr_ags >= 8)
+		ag_set_size = 2;
+	else
+		ag_set_size = 1;
+	nr_streams = nr_ags / ag_set_size;
+	return min_t(uint16_t, nr_streams, XFS_SW_WRITE_STREAMS_MAX);
 }
 
 uint16_t
fs/xfs/xfs_mount.h +2 -0
index 1376963fb7e8..ad51115594d3 100644
@@ -45,6 +45,8 @@ enum {
 
 #define XFS_ERR_RETRY_FOREVER	-1
 
+#define XFS_SW_WRITE_STREAMS_MAX	16
+
 /*
  * Although retry_timeout is in jiffies which is normally an unsigned long,
  * we limit the retry timeout to 86400 seconds, or one day.  So even a
fs/xfs/xfs_super.c +6 -6
index 0e2bf6f7b378..d698be504c45 100644
@@ -1711,12 +1711,12 @@ xfs_fs_fill_super(
 		return error;
 
 	nr_streams = bdev_max_write_streams(mp->m_ddev_targp->bt_bdev);
-	if (nr_streams) {
-		mp->m_streams_in_use = bitmap_zalloc(nr_streams, GFP_KERNEL);
-		if (!mp->m_streams_in_use) {
-			error = -ENOMEM;
-			goto out_shutdown_devices;
-		}
+	if (!nr_streams)
+		nr_streams = XFS_SW_WRITE_STREAMS_MAX;
+	mp->m_streams_in_use = bitmap_zalloc(nr_streams, GFP_KERNEL);
+	if (!mp->m_streams_in_use) {
+		error = -ENOMEM;
+		goto out_shutdown_devices;
 	}
 
 	if (xfs_debugfs) {
Signature · 1 line
2.25.1
#35 lore raw

Re: [PATCH v4 6/6] xfs: introduce software write streams

Quoted 4 lines · Kanchan Joshi

Even when the underlying block device does not advertise write streams, XFS can choose do so, as that enables logical spatial isolation and dynamic AG-set based concurrency for the standard storage, excluding rtvolume.

Ok, this gets back to my previous point. Pleadse just ignore the hardware write streams in the initial patch, and then add the aligning to hardware write streams later.

fs/xfs/xfs_inode.c xfs_inode_max_write_streams( · line 77

Quoted 8 lines
fs/xfs/xfs_inode.c +8 -0
+	nr_streams = bdev_max_write_streams(bdev);
+	if (nr_streams > 0)
+		return nr_streams;
+	/*
+	 * Enable software-only streams if hardware streams are not available.
+	 * This helps to
+	 * - improve isolation; reduce allocation interleaving.
+	 * - improve concurrency using AG-set based steering within and across streams.

Overly long line.

fs/xfs/xfs_inode.c xfs_inode_max_write_streams( · line 87

Quoted 10 lines
fs/xfs/xfs_inode.c +10 -0
+	 */
+	nr_ags = mp->m_sb.sb_agcount;
+	if (nr_ags >= 16)
+		ag_set_size = 4;
+	else if (nr_ags >= 8)
+		ag_set_size = 2;
+	else
+		ag_set_size = 1;
+	nr_streams = nr_ags / ag_set_size;
+	return min_t(uint16_t, nr_streams, XFS_SW_WRITE_STREAMS_MAX);

.. and this value should be cached somewhere.

Quoted 4 lines
fs/xfs/xfs_super.c +0 -3
 	nr_streams = bdev_max_write_streams(mp->m_ddev_targp->bt_bdev);
-	if (nr_streams) {
-		mp->m_streams_in_use = bitmap_zalloc(nr_streams, GFP_KERNEL);
-		if (!mp->m_streams_in_use) {

fs/xfs/xfs_super.c xfs_fs_fill_super( · line 1720

Quoted 10 lines
fs/xfs/xfs_super.c +6 -3
-			error = -ENOMEM;
-			goto out_shutdown_devices;
-		}
+	if (!nr_streams)
+		nr_streams = XFS_SW_WRITE_STREAMS_MAX;
+	mp->m_streams_in_use = bitmap_zalloc(nr_streams, GFP_KERNEL);
+	if (!mp->m_streams_in_use) {
+		error = -ENOMEM;
+		goto out_shutdown_devices;
 	}

and also be used here?

While we're at it, the stream allocator should probably go into the buftarg to prepare for also adding it for the RT volume.