Skip to contentSkip to conversation

direct-io file extended attributes

PATCH In review Keith Busch 4 participants 15 messages Jul 10 – Jul 15

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover direct-io file extended attributes

From: Keith Busch <[email protected]>

The attributes reported through statx are incomplete for applications to fully know exactly how IO construction is valid or not. The statx call can report minimum memory alignment and total granularity, but it doesn't show the underlying gap boundary requirements or max segments per granule.

This series adds the minimum to the extended file attributes through file_getattr. I hear this is the preferred interface for reporting such things over adding more fields to statx. In order to get everything under a single syscall, some of the attributes are duplicated from statx.

Keith Busch (5):
fs: add direct io attributes to file_getattr
block: report direct io attributes through file_getattr
xfs: report direct io attributes through file_getattr
ext4: report direct io attributes through file_getattr
f2fs: report direct io attributes through file_getattr

 block/bdev.c             | 37 +++++++++++++++++++++++++++++++++++++
 fs/ext4/ioctl.c          | 23 +++++++++++++++++++++++
 fs/f2fs/file.c           | 20 ++++++++++++++++++++
 fs/file_attr.c           | 13 ++++++++++++-
 fs/xfs/xfs_ioctl.c       | 26 ++++++++++++++++++++++++++
 include/linux/blkdev.h   | 14 ++++++++++++++
 include/linux/fileattr.h | 10 ++++++++--
 include/uapi/linux/fs.h  | 10 +++++++++-
 8 files changed, 149 insertions(+), 4 deletions(-)
Signature · 1 line
2.53.0-Meta
#1 lore raw

PATCH v2 1/5 fs: add direct io attributes to file_getattr

From: Keith Busch <[email protected]>

Direct I/O imposes alignment and layout constraints that come from both the filesystem and its backing storage. statx() reports some of these, but not all, and it can no longer be extended. Report the complete set through file_getattr() in new struct file_attr fields so applications can know the constraints they have to work with.

The fields are valid only when FS_XFLAG_DIO is set in fa_xflags. When it is clear the file does not report direct I/O geometry, for example, because the filesystem configuration does not allow direct I/O, does not support file_getatter, or is using the legacy block sized alignment constraints.

The new fields are defined as follows:

- fa_dio_mem_align: byte alignment required for each I/O memory buffer. The starting address and length of each buffer vector must be a multiple of this value.

- fa_dio_offset_align: alignment required for the file offset and total length of a write.

- fa_dio_read_offset_align: the same, but for reads. It may be smaller than the write alignment allowing reads at a finer granularity, but will be at most the same as writes.

- fa_dio_virt_boundary_align: alignment required at the boundary between adjacent memory segments of a multi-segment transfer. The previous segment must end on that boundary and the next must start on it; otherwise, the kernel will split the command at that vector if possible, or return an error if not (the accumulated length is shorter than "offset_align"). A value of 1 means there is no such constraint.

- fa_dio_offset_align_max_vecs: the maximum number of io vectors that may compose a single fa_dio_offset_align unit. Buffers too small to reach that unit within this many vectors can not form a valid I/O and will be rejected by the kernel.

The fields are read only; file_setattr() ignores them.

The fa_pad field is introduced as a reserved field to account for an implicit padding on 64-bit architectures and to make the struct the same size for 32-bit. It must be zero to keep the space available for future use.

---
 fs/file_attr.c           |  7 +++++++
 include/linux/fileattr.h | 10 ++++++++--
 include/uapi/linux/fs.h  | 10 +++++++++-
 3 files changed, 24 insertions(+), 3 deletions(-)
fs/file_attr.c +7 -0
index bfb00d256dd56..b37a55f54a449 100644
@@ -110,6 +110,11 @@ static void fileattr_to_file_attr(const struct file_kattr *fa,
 	fattr->fa_nextents = fa->fsx_nextents;
 	fattr->fa_projid = fa->fsx_projid;
 	fattr->fa_cowextsize = fa->fsx_cowextsize;
+	fattr->fa_dio_mem_align = fa->fsx_dio_mem_align;
+	fattr->fa_dio_offset_align = fa->fsx_dio_offset_align;
+	fattr->fa_dio_read_offset_align = fa->fsx_dio_read_offset_align;
+	fattr->fa_dio_virt_boundary_align = fa->fsx_dio_virt_boundary_align;
+	fattr->fa_dio_offset_align_max_vecs = fa->fsx_dio_offset_align_max_vecs;
 }
 
 /**
@@ -145,6 +150,8 @@ static int file_attr_to_fileattr(const struct file_attr *fattr,
 
 	if (fattr->fa_xflags & ~mask)
 		return -EINVAL;
+	if (fattr->fa_pad)
+		return -EINVAL;
 
 	fileattr_fill_xflags(fa, fattr->fa_xflags & ~FS_XFLAG_RDONLY_MASK);
 	fa->fsx_extsize = fattr->fa_extsize;
include/linux/fileattr.h +8 -2
index 58044b5980162..3d5384c79398e 100644
@@ -17,11 +17,11 @@
 /* Read-only inode flags */
 #define FS_XFLAG_RDONLY_MASK \
 	(FS_XFLAG_PREALLOC | FS_XFLAG_HASATTR | FS_XFLAG_VERITY | \
-	 FS_XFLAG_CASEFOLD | FS_XFLAG_CASENONPRESERVING)
+	 FS_XFLAG_CASEFOLD | FS_XFLAG_CASENONPRESERVING | FS_XFLAG_DIO)
 
 /* Flags to indicate valid value of fsx_ fields */
 #define FS_XFLAG_VALUES_MASK \
-	(FS_XFLAG_EXTSIZE | FS_XFLAG_COWEXTSIZE)
+	(FS_XFLAG_EXTSIZE | FS_XFLAG_COWEXTSIZE | FS_XFLAG_DIO)
 
 /* Flags for directories */
 #define FS_XFLAG_DIRONLY_MASK \
@@ -49,6 +49,12 @@ struct file_kattr {
 	u32	fsx_nextents;	/* nextents field value (get)	*/
 	u32	fsx_projid;	/* project identifier (get/set) */
 	u32	fsx_cowextsize;	/* CoW extsize field value (get/set)*/
+	/* struct file_attr dio alignment: */
+	u32	fsx_dio_mem_align;
+	u32	fsx_dio_offset_align;
+	u32	fsx_dio_read_offset_align;
+	u32	fsx_dio_virt_boundary_align;
+	u32	fsx_dio_offset_align_max_vecs;
 	/* selectors: */
 	bool	flags_valid:1;
 	bool	fsx_valid:1;
include/uapi/linux/fs.h +9 -1
index bd87262f2e349..a49572fab3726 100644
@@ -229,10 +229,17 @@ struct file_attr {
 	__u32 fa_nextents;	/* nextents field value (get)   */
 	__u32 fa_projid;	/* project identifier (get/set) */
 	__u32 fa_cowextsize;	/* CoW extsize field value (get/set) */
+	__u32 fa_dio_mem_align;	/* DIO memory buffer align (get) */
+	__u32 fa_dio_offset_align;	/* DIO file offset align (get) */
+	__u32 fa_dio_read_offset_align;	/* DIO read offset align (get) */
+	__u32 fa_dio_virt_boundary_align;	/* DIO virt boundary (get) */
+	__u32 fa_dio_offset_align_max_vecs;	/* DIO vecs per unit (get) */
+	__u32 fa_pad;		/* alignment padding, reserved for future use */
 };
 
 #define FILE_ATTR_SIZE_VER0 24
-#define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER0
+#define FILE_ATTR_SIZE_VER1 48
+#define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER1
 
 /*
  * Flags for the fsx_xflags field
@@ -261,6 +268,7 @@ struct file_attr {
  */
 #define FS_XFLAG_CASEFOLD	0x00040000	/* case-insensitive lookups */
 #define FS_XFLAG_CASENONPRESERVING 0x00080000	/* case not preserved */
+#define FS_XFLAG_DIO		0x00100000	/* DIO alignment info valid */
 #define FS_XFLAG_HASATTR	0x80000000	/* no DIFLAG for this	*/
 
 /* the read-only stuff doesn't really belong here, but any other place is
Signature · 1 line
2.53.0-Meta
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCHv2 1/5] fs: add direct io attributes to file_getattr

Quoted 5 lines · Keith Busch

From: Keith Busch <[email protected]>

Direct I/O imposes alignment and layout constraints that come from both the filesystem and its backing storage. statx() reports some of these, but not all, and it can no longer be extended.

Well, it could be extended. Although it would require a new flag, and there's been a lot of pushback to adding non-fastpath attributes
to it. So maybe update this blurb ad bit and also mention which
attributes are duplicated?

Otherwise looks good:

#3 lore raw

PATCH v2 2/5 block: report direct io attributes through file_getattr

In reply to Keith Busch

From: Keith Busch <[email protected]>

Add bdev_fill_dio_attr() to fill the file_attr direct io alignment fields from a block device's queue limits, so filesystems can share the derivation. Use it to report the attributes for block devices opened directly.

---
 block/bdev.c           | 37 +++++++++++++++++++++++++++++++++++++
 fs/file_attr.c         |  6 +++++-
 include/linux/blkdev.h | 14 ++++++++++++++
 3 files changed, 56 insertions(+), 1 deletion(-)
block/bdev.c +37 -0
index 85ce57bd2ae4f..3b56f6696d3a3 100644
@@ -28,6 +28,7 @@
 #include <linux/part_stat.h>
 #include <linux/uaccess.h>
 #include <linux/stat.h>
+#include <linux/fileattr.h>
 #include "../fs/internal.h"
 #include "blk.h"
 
@@ -1353,6 +1354,42 @@ void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask)
 	blkdev_put_no_open(bdev);
 }
 
+/*
+ * Fill the direct I/O alignment attributes derived from a block device's
+ * queue limits.  Filesystems override the offset alignments as needed and
+ * set FS_XFLAG_DIO once they have decided direct I/O is supported.
+ */
+void bdev_fill_dio_attr(struct block_device *bdev, struct file_kattr *fa)
+{
+	fa->fsx_dio_mem_align = bdev_dma_alignment(bdev) + 1;
+	fa->fsx_dio_offset_align = bdev_logical_block_size(bdev);
+	fa->fsx_dio_read_offset_align = bdev_logical_block_size(bdev);
+	fa->fsx_dio_virt_boundary_align = bdev_virt_boundary_alignment(bdev);
+	fa->fsx_dio_offset_align_max_vecs = bdev_max_segments(bdev);
+}
+EXPORT_SYMBOL_GPL(bdev_fill_dio_attr);
+
+/*
+ * Handle DIO alignment for block devices via fileattr.
+ */
+int bdev_fileattr(const struct inode *inode, struct file_kattr *fa)
+{
+	struct block_device *bdev;
+
+	bdev = blkdev_get_no_open(inode->i_rdev, false);
+	if (!bdev)
+		return -ENODEV;
+
+	memset(fa, 0, sizeof(*fa));
+	bdev_fill_dio_attr(bdev, fa);
+	fa->fsx_valid = true;
+	fa->flags_valid = true;
+	fa->fsx_xflags |= FS_XFLAG_DIO;
+
+	blkdev_put_no_open(bdev);
+	return 0;
+}
+
 bool disk_live(struct gendisk *disk)
 {
 	return !inode_unhashed(BD_INODE(disk->part0));
fs/file_attr.c +5 -1
index b37a55f54a449..b637eff9081aa 100644
@@ -7,6 +7,7 @@
 #include <linux/export.h>
 #include <linux/syscalls.h>
 #include <linux/namei.h>
+#include <linux/blkdev.h>
 
 #include "internal.h"
 
@@ -88,13 +89,16 @@ int vfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 	struct inode *inode = d_inode(dentry);
 	int error;
 
-	if (!inode->i_op->fileattr_get)
+	if (!inode->i_op->fileattr_get && !S_ISBLK(inode->i_mode))
 		return -ENOIOCTLCMD;
 
 	error = security_inode_file_getattr(dentry, fa);
 	if (error)
 		return error;
 
+	if (!inode->i_op->fileattr_get)
+		return bdev_fileattr(inode, fa);
+
 	return inode->i_op->fileattr_get(dentry, fa);
 }
 EXPORT_SYMBOL(vfs_fileattr_get);
include/linux/blkdev.h +14 -0
index 9213a5716f95a..2d435fadfdcb9 100644
@@ -1607,6 +1607,17 @@ static inline unsigned int bdev_dma_alignment(struct block_device *bdev)
 	return queue_dma_alignment(bdev_get_queue(bdev));
 }
 
+static inline unsigned long bdev_virt_boundary_mask(struct block_device *bdev)
+{
+	return bdev_get_queue(bdev)->limits.virt_boundary_mask;
+}
+
+static inline unsigned int
+bdev_virt_boundary_alignment(struct block_device *bdev)
+{
+	return bdev_virt_boundary_mask(bdev) + 1;
+}
+
 static inline unsigned int
 blk_lim_dma_alignment_and_pad(struct queue_limits *lim)
 {
@@ -1805,6 +1816,9 @@ int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend);
 int sync_blockdev_nowait(struct block_device *bdev);
 void sync_bdevs(bool wait);
 void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask);
+struct file_kattr;
+void bdev_fill_dio_attr(struct block_device *bdev, struct file_kattr *fa);
+int bdev_fileattr(const struct inode *inode, struct file_kattr *fa);
 void printk_all_partitions(void);
 int __init early_lookup_bdev(const char *pathname, dev_t *dev);
 #else
Signature · 1 line
2.53.0-Meta
#4 lore raw

Re: [PATCHv2 2/5] block: report direct io attributes through file_getattr

Quoted 8 lines

#include "internal.h"

fs/file_attr.c +1 -1
@@ -88,13 +89,16 @@ int vfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 	struct inode *inode = d_inode(dentry);
 	int error;
 
-	if (!inode->i_op->fileattr_get)
+	if (!inode->i_op->fileattr_get && !S_ISBLK(inode->i_mode))

fs/file_attr.c int vfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa) · line 102

Quoted 10 lines
fs/file_attr.c +3 -0
 		return -ENOIOCTLCMD;
 
 	error = security_inode_file_getattr(dentry, fa);
 	if (error)
 		return error;
 
+	if (!inode->i_op->fileattr_get)
+		return bdev_fileattr(inode, fa);
+
 	return inode->i_op->fileattr_get(dentry, fa);

While this is correct, it reads a bit odd as the check above required the S_ISBLK above. I'd be tempted to move the -ENOIOCTLCMD below the seurity check to mae it more obvious:

  	error = security_inode_file_getattr(dentry, fa);
  	if (error)
  		return error;
	if (!inode->i_op->fileattr_get) {
		if (!S_ISBLK(inode->i_mode))
			return -ENOIOCTLCMD;
		return bdev_fileattr(inode, fa);
	}

...

#5 lore raw

PATCH v2 3/5 xfs: report direct io attributes through file_getattr

In reply to Keith Busch

From: Keith Busch <[email protected]>

Fill in the direct io alignment attributes for regular files, reporting the larger write alignment for CoW inodes.

---
 fs/xfs/xfs_ioctl.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
fs/xfs/xfs_ioctl.c +26 -0
index 1b53701bebea1..d585242a8fe9f 100644
@@ -49,6 +49,7 @@
 
 #include <linux/mount.h>
 #include <linux/fileattr.h>
+#include <linux/blkdev.h>
 
 /* Return 0 on success or positive error */
 int
@@ -468,6 +469,29 @@ xfs_ioc_rtgroup_geometry(
  * Linux extended inode flags interface.
  */
 
+static void
+xfs_fill_fsxattr_dio(
+	struct xfs_inode	*ip,
+	int			whichfork,
+	struct file_kattr	*fa)
+{
+	struct xfs_buftarg *target;
+
+	if (whichfork != XFS_DATA_FORK || !S_ISREG(VFS_I(ip)->i_mode))
+		return;
+
+	target = xfs_inode_buftarg(ip);
+	bdev_fill_dio_attr(target->bt_bdev, fa);
+	/*
+	 * CoW inodes must write whole allocation units out of place, so report
+	 * the larger write alignment while leaving the smaller read alignment
+	 * from the queue limits in place.
+	 */
+	if (xfs_is_cow_inode(ip))
+		fa->fsx_dio_offset_align = xfs_inode_alloc_unitsize(ip);
+	fa->fsx_xflags |= FS_XFLAG_DIO;
+}
+
 static void
 xfs_fill_fsxattr(
 	struct xfs_inode	*ip,
@@ -517,6 +541,8 @@ xfs_fill_fsxattr(
 		fa->fsx_nextents = xfs_iext_count(ifp);
 	else
 		fa->fsx_nextents = xfs_ifork_nextents(ifp);
+
+	xfs_fill_fsxattr_dio(ip, whichfork, fa);
 }
 
 STATIC int
Signature · 1 line
2.53.0-Meta
#6 lore raw

Re: [PATCHv2 3/5] xfs: report direct io attributes through file_getattr

fs/xfs/xfs_ioctl.c xfs_ioc_rtgroup_geometry( · line 478

Quoted 7 lines · Keith Busch
fs/xfs/xfs_ioctl.c +7 -0
+static void
+xfs_fill_fsxattr_dio(
+	struct xfs_inode	*ip,
+	int			whichfork,
+	struct file_kattr	*fa)
+{
+	struct xfs_buftarg *target;

Same alignment for the variables as for the arguments.

fs/xfs/xfs_ioctl.c xfs_ioc_rtgroup_geometry( · line 481

Quoted 3 lines
fs/xfs/xfs_ioctl.c +3 -0
+
+	if (whichfork != XFS_DATA_FORK || !S_ISREG(VFS_I(ip)->i_mode))
+		return;

Move this check to th caller? Although this and similar check in the other file system patches miss block device nodes, for which we'd need a special case, or one in the caller to override the attrs.

Or maybe using the file attr for this isn't actually a good idea, and we should do an ioctl instead which automatically gets routed to the block device fops?

fs/xfs/xfs_ioctl.c xfs_ioc_rtgroup_geometry( · line 483

Quoted 1 line
fs/xfs/xfs_ioctl.c +1 -0
+	target = xfs_inode_buftarg(ip);

.. and initialize the variable at declaration time?

#7 lore raw

Re: [PATCHv2 3/5] xfs: report direct io attributes through file_getattr

Quoted 3 lines · Christoph Hellwig

Or maybe using the file attr for this isn't actually a good idea, and we should do an ioctl instead which automatically gets routed to the block device fops?

Something like this instead?

---

block/ioctl.c +16 -0
index 3d4ea1537457d..6de2f87bcb8bd 100644
@@ -642,6 +642,21 @@ static int blkdev_bszset(struct file *file, blk_mode_t mode,
 	return ret;
 }
 
+void bdev_dio_align(struct block_device *bdev, struct fs_dio_align *align)
+{
+	align->dio_mem_align = bdev_dma_alignment(bdev) + 1;
+	align->dio_offset_align = bdev_logical_block_size(bdev);
+	align->dio_read_offset_align = bdev_logical_block_size(bdev);
+	align->dio_virt_boundary_align = bdev_virt_boundary_alignment(bdev);
+	align->dio_offset_align_max_vecs = bdev_max_segments(bdev);
+}
+EXPORT_SYMBOL_GPL(bdev_dio_align);
+
 /*
  * Common commands that are handled the same way on native and compat
  * user space. Note the separate arg/argp parameters that are needed
@@ -690,6 +705,12 @@ static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode,
 		return put_uint(argp, bdev_io_opt(bdev));
 	case BLKALIGNOFF:
 		return put_int(argp, bdev_alignment_offset(bdev));
+	case FS_IOC_GETDIOALIGN: {
+		struct fs_dio_align align = {};
+
+		bdev_dio_align(bdev, &align);
+		return copy_to_user(argp, &align, sizeof(align)) ? -EFAULT : 0;
+	}
 	case BLKDISCARDZEROES:
 		return put_uint(argp, 0);
 	case BLKSECTGET:
fs/xfs/xfs_ioctl.c +13 -0
index 1b53701bebea1..e980522fa7539 100644
@@ -49,6 +49,7 @@
 
 #include <linux/mount.h>
 #include <linux/fileattr.h>
+#include <linux/blkdev.h>
 
 /* Return 0 on success or positive error */
 int
@@ -1244,6 +1245,23 @@ xfs_file_ioctl(
 	"%s should use fallocate; XFS_IOC_{ALLOC,FREE}SP ioctl unsupported",
 				current->comm);
 		return -ENOTTY;
+	case FS_IOC_GETDIOALIGN: {
+		struct fs_dio_align	align = {};
+
+		if (!S_ISREG(inode->i_mode))
+			return -ENOTTY;
+		bdev_dio_align(xfs_inode_buftarg(ip)->bt_bdev, &align);
+		if (xfs_is_cow_inode(ip))
+			align.dio_offset_align = xfs_inode_alloc_unitsize(ip);
+		if (copy_to_user(arg, &align, sizeof(align)))
+			return -EFAULT;
+		return 0;
+	}
 	case XFS_IOC_DIOINFO: {
 		struct kstat		st;
 		struct dioattr		da;
include/linux/blkdev.h +13 -0
index 9213a5716f95a..dc32acf696e40 100644
@@ -1607,6 +1607,17 @@ static inline unsigned int bdev_dma_alignment(struct block_device *bdev)
 	return queue_dma_alignment(bdev_get_queue(bdev));
 }
 
+static inline unsigned long bdev_virt_boundary_mask(struct block_device *bdev)
+{
+	return bdev_get_queue(bdev)->limits.virt_boundary_mask;
+}
+
+static inline unsigned int
+bdev_virt_boundary_alignment(struct block_device *bdev)
+{
+	return bdev_virt_boundary_mask(bdev) + 1;
+}
+
 static inline unsigned int
 blk_lim_dma_alignment_and_pad(struct queue_limits *lim)
 {
@@ -1805,6 +1816,8 @@ int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend);
 int sync_blockdev_nowait(struct block_device *bdev);
 void sync_bdevs(bool wait);
 void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask);
+struct fs_dio_align;
+void bdev_dio_align(struct block_device *bdev, struct fs_dio_align *align);
 void printk_all_partitions(void);
 int __init early_lookup_bdev(const char *pathname, dev_t *dev);
 #else
include/uapi/linux/fs.h +11 -0
index bd87262f2e349..81b0c81e17282 100644
@@ -234,6 +234,21 @@ struct file_attr {
 #define FILE_ATTR_SIZE_VER0 24
 #define FILE_ATTR_SIZE_LATEST FILE_ATTR_SIZE_VER0
 
+struct fs_dio_align {
+	__u32 dio_mem_align;		/* buffer alignment */
+	__u32 dio_offset_align;		/* write offset alignment */
+	__u32 dio_read_offset_align;	/* read offset alignment */
+	__u32 dio_virt_boundary_align;	/* segment boundary */
+	__u32 dio_offset_align_max_vecs;	/* max vecs per unit */
+	__u32 dio_reserved[3];		/* must be zero */
+};
+
 /*
  * Flags for the fsx_xflags field
  */
@@ -344,6 +359,8 @@ struct file_attr {
 #define FS_IOC_GETFSSYSFSPATH		_IOR(0x15, 1, struct fs_sysfs_path)
 /* Get logical block metadata capability details */
 #define FS_IOC_GETLBMD_CAP		_IOWR(0x15, 2, struct logical_block_metadata_cap)
+/* Get direct I/O alignment and layout constraints */
+#define FS_IOC_GETDIOALIGN		_IOR(0x15, 3, struct fs_dio_align)
 
 /*
  * Inode flags (FS_IOC_GETFLAGS / FS_IOC_SETFLAGS)

--

#8 lore raw

Re: [PATCHv2 3/5] xfs: report direct io attributes through file_getattr

Quoted 1 line · Keith Busch

+void bdev_dio_align(struct block_device *bdev, struct fs_dio_align *align)

Nit: I'd usually pass the filled in structure before the source of the information.

block/ioctl.c static int blkdev_common_ioctl(struct block_device *bdev, blk_mode_t mode, · line 713

Quoted 6 lines
block/ioctl.c +6 -0
+	case FS_IOC_GETDIOALIGN: {
+		struct fs_dio_align align = {};
+
+		bdev_dio_align(bdev, &align);
+		return copy_to_user(argp, &align, sizeof(align)) ? -EFAULT : 0;
+	}

... and I'd probably split this into a little helper function as the ioctl handlers sooner or later become a mess otherwise.

fs/xfs/xfs_ioctl.c xfs_file_ioctl( · line 1257

Quoted 10 lines
fs/xfs/xfs_ioctl.c +10 -0
+	case FS_IOC_GETDIOALIGN: {
+		struct fs_dio_align	align = {};
+
+		if (!S_ISREG(inode->i_mode))
+			return -ENOTTY;
+		bdev_dio_align(xfs_inode_buftarg(ip)->bt_bdev, &align);
+		if (xfs_is_cow_inode(ip))
+			align.dio_offset_align = xfs_inode_alloc_unitsize(ip);
+		if (copy_to_user(arg, &align, sizeof(align)))
+			return -EFAULT;

Same here.

Otherwise this looks good to me.

#9 lore raw

PATCH v2 4/5 ext4: report direct io attributes through file_getattr

In reply to Keith Busch

From: Keith Busch <[email protected]>

Fill in the direct io alignment attributes for regular files, using the block helper and overriding the offset alignment for the cases where ext4 requires filesystem block alignment.

---
 fs/ext4/ioctl.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
fs/ext4/ioctl.c +23 -0
index c8387e6a2c6e9..01617edff17a8 100644
@@ -21,6 +21,7 @@
 #include <linux/iversion.h>
 #include <linux/fileattr.h>
 #include <linux/uuid.h>
+#include <linux/blkdev.h>
 #include "ext4_jbd2.h"
 #include "ext4.h"
 #include <linux/fsmap.h>
@@ -992,6 +993,27 @@ static long ext4_ioctl_group_add(struct file *file,
 	return err;
 }
 
+static void ext4_fileattr_get_dio(struct inode *inode, struct file_kattr *fa)
+{
+	u32 dio_align;
+
+	if (!S_ISREG(inode->i_mode))
+		return;
+
+	dio_align = ext4_dio_alignment(inode);
+	if (!dio_align)
+		return;
+
+	bdev_fill_dio_attr(inode->i_sb->s_bdev, fa);
+	if (dio_align != 1) {
+		fa->fsx_dio_mem_align = dio_align;
+		fa->fsx_dio_offset_align = dio_align;
+		fa->fsx_dio_read_offset_align = dio_align;
+	}
+
+	fa->fsx_xflags |= FS_XFLAG_DIO;
+}
+
 int ext4_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 {
 	struct inode *inode = d_inode(dentry);
@@ -1005,6 +1027,7 @@ int ext4_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 	if (ext4_has_feature_project(inode->i_sb))
 		fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid);
 
+	ext4_fileattr_get_dio(inode, fa);
 	return 0;
 }
 
Signature · 1 line
2.53.0-Meta
#10 lore raw

PATCH v2 5/5 f2fs: report direct io attributes through file_getattr

In reply to Keith Busch

From: Keith Busch <[email protected]>

Fill in the direct io alignment attributes for regular files that support direct writes.

---
 fs/f2fs/file.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/f2fs/file.c +20 -0
index 4b52c56d71f07..230b0dca06251 100644
@@ -3452,6 +3452,25 @@ static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
 }
 #endif
 
+static void f2fs_fileattr_get_dio(struct inode *inode, struct file_kattr *fa)
+{
+	struct block_device *bdev;
+	unsigned int bsize;
+
+	if (!S_ISREG(inode->i_mode) || f2fs_force_buffered_io(inode, WRITE))
+		return;
+
+	bdev = inode->i_sb->s_bdev;
+	if (bdev)
+		bdev_fill_dio_attr(bdev, fa);
+
+	bsize = i_blocksize(inode);
+	fa->fsx_dio_mem_align = bsize;
+	fa->fsx_dio_offset_align = bsize;
+	fa->fsx_dio_read_offset_align = bsize;
+	fa->fsx_xflags |= FS_XFLAG_DIO;
+}
+
 int f2fs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 {
 	struct inode *inode = d_inode(dentry);
@@ -3472,6 +3491,7 @@ int f2fs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
 	if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
 		fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
 
+	f2fs_fileattr_get_dio(inode, fa);
 	return 0;
 }
 
Signature · 1 line
2.53.0-Meta
#11 lore raw
In reply to Keith Busch
Quoted 8 lines · On Fri, Jul 10, 2026 at 02:06:41PM -0700, Keith Busch via Linux-f2fs-devel…

From: Keith Busch <[email protected]>

The attributes reported through statx are incomplete for applications to fully know exactly how IO construction is valid or not. The statx call can report minimum memory alignment and total granularity, but it doesn't show the underlying gap boundary requirements or max segments per granule.

Quoted 5 lines

This series adds the minimum to the extended file attributes through file_getattr. I hear this is the preferred interface for reporting such things over adding more fields to statx. In order to get everything under a single syscall, some of the attributes are duplicated from statx.

Okay, in v2 we at least now know that the existing statx UAPI was considered. Could you give a specific real-world example (with the actual values of each parameter) where it's not sufficient? Without that there isn't really any way to evaluate this proposal.

- Eric

#12 lore raw
Quoted 15 lines · Eric Biggers
Quoted 8 lines · On Fri, Jul 10, 2026 at 02:06:41PM -0700, Keith Busch via Linux-f2fs-devel…

From: Keith Busch <[email protected]>

The attributes reported through statx are incomplete for applications to fully know exactly how IO construction is valid or not. The statx call can report minimum memory alignment and total granularity, but it doesn't show the underlying gap boundary requirements or max segments per granule.

Quoted 5 lines

This series adds the minimum to the extended file attributes through file_getattr. I hear this is the preferred interface for reporting such things over adding more fields to statx. In order to get everything under a single syscall, some of the attributes are duplicated from statx.

Quoted 4 lines

Okay, in v2 we at least now know that the existing statx UAPI was considered. Could you give a specific real-world example (with the actual values of each parameter) where it's not sufficient? Without that there isn't really any way to evaluate this proposal.

Yes, we can consider nvme. This protocol supports two different transfer modes called PRP and SGL. PRP requires 4k aligned segments, though you can have an arbitrary 4-byte aligned offset at the start. SGL on the other hand allows completely arbitrary size and alignments for each segment.

statx reports information sufficient to know that you can have dword aligned page offsets for a virtually contiguous buffer, but it doesn't report PRP's boundary gap requirement, so applications can't tell if the file follows PRP or SGL rules for direct-io.

And if you have a device using SGL, statx doesn't report the max number of sub-sector segments you can submit in a single command.

This series provides both limits so user space has the complete picture.

A typical nvme that supports only PRP has a DMA alignment of 4 bytes, a dio offset alignment of 4k, and a virtual boundary of 4k.

If SGL were supported, there would be no virtual boundary gap, and max segments is 256.

#13 lore raw
Quoted 32 lines · Keith Busch
Quoted 15 lines · Eric Biggers
Quoted 8 lines · On Fri, Jul 10, 2026 at 02:06:41PM -0700, Keith Busch via Linux-f2fs-devel…

From: Keith Busch <[email protected]>

The attributes reported through statx are incomplete for applications to fully know exactly how IO construction is valid or not. The statx call can report minimum memory alignment and total granularity, but it doesn't show the underlying gap boundary requirements or max segments per granule.

Quoted 5 lines

This series adds the minimum to the extended file attributes through file_getattr. I hear this is the preferred interface for reporting such things over adding more fields to statx. In order to get everything under a single syscall, some of the attributes are duplicated from statx.

Quoted 4 lines

Okay, in v2 we at least now know that the existing statx UAPI was considered. Could you give a specific real-world example (with the actual values of each parameter) where it's not sufficient? Without that there isn't really any way to evaluate this proposal.

Yes, we can consider nvme. This protocol supports two different transfer modes called PRP and SGL. PRP requires 4k aligned segments, though you can have an arbitrary 4-byte aligned offset at the start. SGL on the other hand allows completely arbitrary size and alignments for each segment.

statx reports information sufficient to know that you can have dword aligned page offsets for a virtually contiguous buffer, but it doesn't report PRP's boundary gap requirement, so applications can't tell if the file follows PRP or SGL rules for direct-io.

Quoted 7 lines

And if you have a device using SGL, statx doesn't report the max number of sub-sector segments you can submit in a single command.

This series provides both limits so user space has the complete picture.

A typical nvme that supports only PRP has a DMA alignment of 4 bytes, a dio offset alignment of 4k, and a virtual boundary of 4k.

So each segment's length has to be a multiple of 4k, *and* it has to end on a 4k aligned memory address? That implies the segment begins at a 4k aligned memory address as well, which is just stx_dio_mem_align=4k.

What am I missing?

What is a specific example of an I/O request that you'd like to be able to submit that the existing UAPI can't declare support for?

Quoted 2 lines

If SGL were supported, there would be no virtual boundary gap, and max segments is 256.

Can you elaborate on why DIO users need to know max_segments?

I'm worried about the UAPI duplication, as well as it going to be very difficult for userspace to correctly use this information. With just the two alignments there's at least a chance of them getting it right. If we throw virt_boundary_mask and max_segments into the mix, I don't think there's much chance.

- Eric

#14 lore raw
Quoted 8 lines · Eric Biggers
Quoted 1 line · Keith Busch

dio offset alignment of 4k, and a virtual boundary of 4k.

So each segment's length has to be a multiple of 4k, *and* it has to end on a 4k aligned memory address? That implies the segment begins at a 4k aligned memory address as well, which is just stx_dio_mem_align=4k.

What am I missing?

I'm apparently poorly explaining PRPs, but I hear this is a common experience.

Simply put, any virtually contiguous buffer that starts at a dword aligned address is a valid io vector, no matter how many pages it spans. It doesn't matter where it starts or where it ends, but every page in the middle obviously starts and ends on their page boundary.

Simple case: pread/pwrite. You can consult statx to know you can provide any dword aligned buffer with an aligned length, and that's a valid direct IO.

What I'm trying to enable here is the vectored preadv/pwritev type paths for hardware that don't need to subscribe to PRP constraints.

Quoted 2 lines

What is a specific example of an I/O request that you'd like to be able to submit that the existing UAPI can't declare support for?

I want to support NVMe SGL. This allows virtually *discontiguous* segments that we currently can't distinguish with what statx reports. I'm trying to report limits that let applications know what constraints they're dealing with.

Quoted 4 lines
Quoted 2 lines

If SGL were supported, there would be no virtual boundary gap, and max segments is 256.

Can you elaborate on why DIO users need to know max_segments?

* Logical block is 4k.

* DMA granule is 4 bytes.

* Max segments is 256.

That's very typical NVMe device contraints under SGL capabilities with the linux driver.

You can provide 4 byte vectors as needed, but you'll hit the max segments limit before you have a valid IO if they're all that small. The average size needs to be larger, so we need to communicate that somehow.

To be clear, I'm not interested in trying to enable applications dispatching thousands of 4-byte vectors to do an IO. That's a stupid application. The applications I'm trying to enable have unpredictable offsets such that a tiny fraction of vectors are indeed that small, but that's not a typical vector for the payload. But I can't enable just some without generically enabling all.

Quoted 5 lines

I'm worried about the UAPI duplication, as well as it going to be very difficult for userspace to correctly use this information. With just the two alignments there's at least a chance of them getting it right. If we throw virt_boundary_mask and max_segments into the mix, I don't think there's much chance.

The blktests framework test case "block/043" does this with great success with these exact parameters, but it takes these paramters from the sysfs attributes. The same test works with filesystems too, but I haven't gotten around to porting it to fstests because it's gating on having this series.