Skip to contentSkip to conversation

fs: report direct io constraints through file_getattr

PATCH In review Keith Busch via Linux-f2fs-devel 6 participants 11 messages Jul 8 – Jul 10

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

PATCH 1/1 fs: report direct io constraints through file_getattr

From: Keith Busch <[email protected]>

Memory alignment constraints for direct io can vary depending on the backing storage hardware. Provide support through file_getattr to report the attributes necessary for applications to know how to construct valid read and write requests.

---
 block/bdev.c             | 27 +++++++++++++++++++++++++++
 fs/ext4/ioctl.c          | 22 ++++++++++++++++++++++
 fs/f2fs/file.c           | 16 ++++++++++++++++
 fs/file_attr.c           | 16 ++++++++++++++--
 fs/xfs/xfs_ioctl.c       | 16 ++++++++++++++++
 include/linux/blkdev.h   | 12 ++++++++++++
 include/linux/fileattr.h | 10 ++++++++--
 include/uapi/linux/fs.h  | 10 +++++++++-
 8 files changed, 124 insertions(+), 5 deletions(-)
block/bdev.c +27 -0
index 85ce57bd2ae4f..5b3e27339854e 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,32 @@ void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask)
 	blkdev_put_no_open(bdev);
 }
 
+/*
+ * Handle DIO alignment for block devices via fileattr.
+ */
+void bdev_fileattr(const struct inode *inode, struct file_kattr *fa)
+{
+	struct block_device *bdev;
+
+	memset(fa, 0, sizeof(*fa));
+	fa->fsx_valid = true;
+	fa->flags_valid = true;
+
+	bdev = blkdev_get_no_open(inode->i_rdev, false);
+	if (!bdev)
+		return;
+
+	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_max_segments = bdev_max_segments(bdev);
+	fa->fsx_xflags |= FS_XFLAG_DIO;
+
+	blkdev_put_no_open(bdev);
+}
+EXPORT_SYMBOL_GPL(bdev_fileattr);
+
 bool disk_live(struct gendisk *disk)
 {
 	return !inode_unhashed(BD_INODE(disk->part0));
fs/ext4/ioctl.c +22 -0
index c8387e6a2c6e9..70b25f9e0ad5a 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>
@@ -1005,6 +1006,27 @@ 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);
 
+	if (S_ISREG(inode->i_mode)) {
+		u32 dio_align = ext4_dio_alignment(inode);
+
+		if (dio_align != 0) {
+			struct block_device *bdev = inode->i_sb->s_bdev;
+
+			if (dio_align == 1) {
+				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);
+			} else {
+				fa->fsx_dio_mem_align = dio_align;
+				fa->fsx_dio_offset_align = dio_align;
+				fa->fsx_dio_read_offset_align = dio_align;
+			}
+			fa->fsx_dio_virt_boundary_align = bdev_virt_boundary_alignment(bdev);
+			fa->fsx_max_segments = bdev_max_segments(bdev);
+			fa->fsx_xflags |= FS_XFLAG_DIO;
+		}
+	}
+
 	return 0;
 }
 
fs/f2fs/file.c +16 -0
index 4b52c56d71f07..72036ef1d7a8a 100644
@@ -3472,6 +3472,22 @@ 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);
 
+	if (S_ISREG(inode->i_mode)) {
+		unsigned int bsize = i_blocksize(inode);
+		struct block_device *bdev = inode->i_sb->s_bdev;
+
+		if (!f2fs_force_buffered_io(inode, WRITE)) {
+			fa->fsx_dio_mem_align = bsize;
+			fa->fsx_dio_offset_align = bsize;
+			fa->fsx_dio_read_offset_align = bsize;
+			if (bdev) {
+				fa->fsx_dio_virt_boundary_align = bdev_virt_boundary_alignment(bdev);
+				fa->fsx_max_segments = bdev_max_segments(bdev);
+			}
+			fa->fsx_xflags |= FS_XFLAG_DIO;
+		}
+	}
+
 	return 0;
 }
 
fs/file_attr.c +14 -2
index bfb00d256dd56..09804ef9901b8 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,8 +89,12 @@ 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)
-		return -ENOIOCTLCMD;
+	if (!inode->i_op->fileattr_get) {
+		if (!S_ISBLK(inode->i_mode))
+			return -ENOIOCTLCMD;
+		bdev_fileattr(inode, fa);
+		return 0;
+	}
 
 	error = security_inode_file_getattr(dentry, fa);
 	if (error)
@@ -110,6 +115,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_max_segments = fa->fsx_max_segments;
 }
 
 /**
@@ -145,6 +155,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;
fs/xfs/xfs_ioctl.c +16 -0
index 1b53701bebea1..15e14d1525281 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
@@ -517,6 +518,21 @@ xfs_fill_fsxattr(
 		fa->fsx_nextents = xfs_iext_count(ifp);
 	else
 		fa->fsx_nextents = xfs_ifork_nextents(ifp);
+
+	if (whichfork == XFS_DATA_FORK && S_ISREG(VFS_I(ip)->i_mode)) {
+		struct xfs_buftarg *target = xfs_inode_buftarg(ip);
+		struct block_device *bdev = target->bt_bdev;
+
+		fa->fsx_dio_mem_align = bdev_dma_alignment(bdev) + 1;
+		fa->fsx_dio_read_offset_align = bdev_logical_block_size(bdev);
+		if (xfs_is_cow_inode(ip))
+			fa->fsx_dio_offset_align = xfs_inode_alloc_unitsize(ip);
+		else
+			fa->fsx_dio_offset_align = fa->fsx_dio_read_offset_align;
+		fa->fsx_dio_virt_boundary_align = bdev_virt_boundary_alignment(bdev);
+		fa->fsx_max_segments = bdev_max_segments(bdev);
+		fa->fsx_xflags |= FS_XFLAG_DIO;
+	}
 }
 
 STATIC int
include/linux/blkdev.h +12 -0
index 9213a5716f95a..5e83572d15286 100644
@@ -1607,6 +1607,16 @@ 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 +1815,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 file_kattr;
+void 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
include/linux/fileattr.h +8 -2
index 58044b5980162..9eca31b8289fe 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_max_segments;
 	/* selectors: */
 	bool	flags_valid:1;
 	bool	fsx_valid:1;
include/uapi/linux/fs.h +9 -1
index bd87262f2e349..2716fc6fefd6b 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;	/* memory buffer alignment for direct I/O (get) */
+	__u32 fa_dio_offset_align;	/* file offset alignment for direct I/O (get) */
+	__u32 fa_dio_read_offset_align;	/* file offset alignment for direct I/O reads (get) */
+	__u32 fa_dio_virt_boundary_align;	/* virtual boundary alignment for direct I/O (get) */
+	__u32 fa_max_segments;	/* max number of segments for direct I/O (get) */
+	__u32 fa_pad;		/* padding for 8-byte alignment */
 };
 
 #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 · 8 lines
2.52.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw
Quoted 6 lines · Keith Busch

From: Keith Busch <[email protected]>

Memory alignment constraints for direct io can vary depending on the backing storage hardware. Provide support through file_getattr to report the attributes necessary for applications to know how to construct valid read and write requests.

This probably wants to be split in one patch for the new UAPI, one for the helper and one for each user.

And especially the UAPI one needs a much more detailed commit log explaining it, including why this duplicates some of the informastion already in statx and documenting the semantics for all the fields.

Andrey, is there a man page or other official documentation for file_setattr/file_getattr?

Quoted 3 lines

+/*
+ * Handle DIO alignment for block devices via fileattr.
+ */

Maybe note that this purely about the block device constraints, and file systems may expose additional ones?

block/bdev.c void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask) · line 1366

Quoted 7 lines
block/bdev.c +7 -0
+void bdev_fileattr(const struct inode *inode, struct file_kattr *fa)
+{
+	struct block_device *bdev;
+
+	memset(fa, 0, sizeof(*fa));
+	fa->fsx_valid = true;
+	fa->flags_valid = true;

Doing the basic file_kattr initialization here feels dangerous if we want to be able to call this from file system implementations. I'd rather leave the initialization to the caller.

block/bdev.c void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask) · line 1370

Quoted 4 lines
block/bdev.c +4 -0
+
+	bdev = blkdev_get_no_open(inode->i_rdev, false);
+	if (!bdev)
+		return;

.. and explicitly pass in the block device. ->i_rdev always is i_sb->s_dev, which might not be the relevant backing device, e.g. for XFS it could be that or the block device in m_rtdev_targp.

If i_rdev is i_sb->s_dev we can just use sb->s_bdev without needing a new open.

block/bdev.c void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask) · line 1376

Quoted 5 lines
block/bdev.c +5 -0
+	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_max_segments = bdev_max_segments(bdev);

How is the max_segments value defined in a way that is meaningful to userspace?

fs/ext4/ioctl.c int ext4_fileattr_get(struct dentry *dentry, struct file_kattr *fa) · line 1009

Quoted 5 lines
fs/ext4/ioctl.c +1 -0
@@ -1005,6 +1006,27 @@ 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);
 
+	if (S_ISREG(inode->i_mode)) {

You'll probably want to split this into a helper to keep it easily readable.

Quoted 6 lines
fs/ext4/ioctl.c +4 -0
+		u32 dio_align = ext4_dio_alignment(inode);
+
+		if (dio_align != 0) {
 
 
+			struct block_device *bdev = inode->i_sb->s_bdev;

fs/ext4/ioctl.c int ext4_fileattr_get(struct dentry *dentry, struct file_kattr *fa) · line 1023

Quoted 10 lines
fs/ext4/ioctl.c +10 -0
+
+			if (dio_align == 1) {
+				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);
+			} else {
+				fa->fsx_dio_mem_align = dio_align;
+				fa->fsx_dio_offset_align = dio_align;
+				fa->fsx_dio_read_offset_align = dio_align;
+			}

Call bdev_fileattr and override the relevant field as needed?

Question to the ext4 maintainers: why does ext4_dio_alignment affect the in-memory alignment? If it does so, it should probably also affect the virt boundry alignment..

fs/f2fs/file.c int f2fs_fileattr_get(struct dentry *dentry, struct file_kattr *fa) · line 3479

Quoted 5 lines
fs/f2fs/file.c +5 -0
+	if (S_ISREG(inode->i_mode)) {
+		unsigned int bsize = i_blocksize(inode);
+		struct block_device *bdev = inode->i_sb->s_bdev;
+
+		if (!f2fs_force_buffered_io(inode, WRITE)) {

Same comments as for ext4. Also f2fs does support multiple devices, but I'm not sure how data is placed on them, or if a file can be on multiple devices. We'll need input from the f2fs maintainers/contributors here.

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

Quoted 11 lines
fs/file_attr.c +5 -2
@@ -88,8 +89,12 @@ 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)
-		return -ENOIOCTLCMD;
+	if (!inode->i_op->fileattr_get) {
+		if (!S_ISBLK(inode->i_mode))
+			return -ENOIOCTLCMD;
+		bdev_fileattr(inode, fa);
+		return 0;

Don't we also want to fill out the attributes for block devices
on file systems that provide a ->fileattr_get?. Also if we fill
out something, we need the security_inode_file_getattr as well.

fs/file_attr.c static int file_attr_to_fileattr(const struct file_attr *fattr, · line 159

Quoted 7 lines
fs/file_attr.c +2 -0
 /**
@@ -145,6 +155,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;

How is this related?

fs/xfs/xfs_ioctl.c xfs_fill_fsxattr( · line 522

Quoted 1 line
fs/xfs/xfs_ioctl.c +1 -0
+	if (whichfork == XFS_DATA_FORK && S_ISREG(VFS_I(ip)->i_mode)) {

This probably wants a separate helper.

fs/xfs/xfs_ioctl.c xfs_fill_fsxattr( · line 524

Quoted 2 lines
fs/xfs/xfs_ioctl.c +2 -0
+		struct xfs_buftarg *target = xfs_inode_buftarg(ip);
+		struct block_device *bdev = target->bt_bdev;

.. and if the generic helpers gets an explicitl bdev we can use it here and just override one value for for xfs_is_cow_inode().

Quoted 1 line

+static inline unsigned int bdev_virt_boundary_alignment(struct block_device *bdev)

Overly long line.

#2 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 6 lines · Keith Busch

From: Keith Busch <[email protected]>

Memory alignment constraints for direct io can vary depending on the backing storage hardware. Provide support through file_getattr to report the attributes necessary for applications to know how to construct valid read and write requests.

This probably wants to be split in one patch for the new UAPI, one for the helper and one for each user.

Quoted 6 lines

And especially the UAPI one needs a much more detailed commit log explaining it, including why this duplicates some of the informastion already in statx and documenting the semantics for all the fields.

Andrey, is there a man page or other official documentation for file_setattr/file_getattr?

No, I had a draft in cover letter but haven't got to sending it to man-pages. I will prepare a man page.

Signature · 1 line
- Andrey
#3 lore raw
Quoted 5 lines · On Thu, Jul 09, 2026 at 10:51:39AM +0200, Andrey Albershteyn via…
Quoted 2 lines

Andrey, is there a man page or other official documentation for file_setattr/file_getattr?

No, I had a draft in cover letter but haven't got to sending it to man-pages. I will prepare a man page.

Or just abandon this series, and read the statx(2) man page which documents the already-existing UAPI for getting the DIO alignments?

- Eric

reviewer
#4 lore raw
In reply to Christoph Hellwig
Quoted 20 lines · Christoph Hellwig
Quoted 6 lines · Keith Busch
fs/ext4/ioctl.c +4 -0
+		u32 dio_align = ext4_dio_alignment(inode);
+
+		if (dio_align != 0) {
 
 
+			struct block_device *bdev = inode->i_sb->s_bdev;

fs/ext4/ioctl.c int ext4_fileattr_get(struct dentry *dentry, struct file_kattr *fa) · line 1023

Quoted 10 lines
fs/ext4/ioctl.c +10 -0
+
+			if (dio_align == 1) {
+				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);
+			} else {
+				fa->fsx_dio_mem_align = dio_align;
+				fa->fsx_dio_offset_align = dio_align;
+				fa->fsx_dio_read_offset_align = dio_align;
+			}

Call bdev_fileattr and override the relevant field as needed?

Quoted 3 lines

Question to the ext4 maintainers: why does ext4_dio_alignment affect the in-memory alignment? If it does so, it should probably also affect the virt boundry alignment..

I guess that is mostly a historical accident. ext4_dio_alignment() returns 1 (iomap alignment is used and that's different for memory and file offset alignment), 0 (dio not supported, memory and file offset alignment is indeed the same), and blocksize (a special case which can happen for fscrypt if it supports dio and where I believe memory alignment requirements may be in fact different). I'm adding Eric to CC to answer what actual requirements fscrypt has for memory buffers for direct IO. I'd expect with inline encryption we would have the same requirements as ordinary iomap direct IO and for other code paths I'm not sure... Eric?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw
Quoted 25 lines · Jan Kara
Quoted 20 lines · Christoph Hellwig
Quoted 6 lines · Keith Busch
fs/ext4/ioctl.c +4 -0
+		u32 dio_align = ext4_dio_alignment(inode);
+
+		if (dio_align != 0) {
 
 
+			struct block_device *bdev = inode->i_sb->s_bdev;

fs/ext4/ioctl.c int ext4_fileattr_get(struct dentry *dentry, struct file_kattr *fa) · line 1023

Quoted 10 lines
fs/ext4/ioctl.c +10 -0
+
+			if (dio_align == 1) {
+				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);
+			} else {
+				fa->fsx_dio_mem_align = dio_align;
+				fa->fsx_dio_offset_align = dio_align;
+				fa->fsx_dio_read_offset_align = dio_align;
+			}

Call bdev_fileattr and override the relevant field as needed?

Quoted 3 lines

Question to the ext4 maintainers: why does ext4_dio_alignment affect the in-memory alignment? If it does so, it should probably also affect the virt boundry alignment..

Quoted 9 lines

I guess that is mostly a historical accident. ext4_dio_alignment() returns 1 (iomap alignment is used and that's different for memory and file offset alignment), 0 (dio not supported, memory and file offset alignment is indeed the same), and blocksize (a special case which can happen for fscrypt if it supports dio and where I believe memory alignment requirements may be in fact different). I'm adding Eric to CC to answer what actual requirements fscrypt has for memory buffers for direct IO. I'd expect with inline encryption we would have the same requirements as ordinary iomap direct IO and for other code paths I'm not sure... Eric?

Please see Documentation/filesystems/fscrypt.rst:

The I/O request must be fully aligned to the filesystem block size. This means that the file position the I/O is targeting, the lengths of all I/O segments, and the memory addresses of all I/O buffers must be multiples of this value. Note that the filesystem block size may be greater than the logical block size of the block device.

We go over this about once a year, whenever someone suggests that the memory alignment requirement is not needed. blk-crypto-fallback needs it, the block layer itself needs it, and at least some storage drivers need it.

To support less memory alignment, we'd need to:

- Make some fairly complex updates to blk-crypto-fallback to support en/decrypting data units split across pages. Note that there's no way to do this with zero overhead on other requests.

- Update the block layer to not split crypto data units across bvecs, regardless of memory alignment. This also would add more overhead to all requests.

- Test the hardware support on each eMMC and UFS host controller individually and opt in the ones that actually correctly handle crypto data units split across DMA segments. We already know it does *not* work on at least one.

- And finally update the filesystems as the last step, not the first.

So far I haven't seen the point. Yes, applications can benefit from the lower alignment in theory. But especially with encryption/decryption, it isn't at all easy to support. This has apparently been getting learned the hard way, as (for example) alignment was initially relaxed for dm-crypt without testing it, and it had to be reverted
(https://lore.kernel.org/dm-devel/[email protected]/).

- Eric

#6 lore raw
Quoted 6 lines · Eric Biggers

So far I haven't seen the point. Yes, applications can benefit from the lower alignment in theory. But especially with encryption/decryption, it isn't at all easy to support. This has apparently been getting learned the hard way, as (for example) alignment was initially relaxed for dm-crypt without testing it, and it had to be reverted
(https://lore.kernel.org/dm-devel/[email protected]/).

To be fair, that was most certainly tested in production, and it was reverted for a bug not related to alignment. It was just a mishandled early exit corner case accessing uninitialized fields; a trivial fixup.

#7 lore raw
In reply to Christoph Hellwig
Quoted 10 lines · Christoph Hellwig

block/bdev.c void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask) · line 1376

Quoted 5 lines · Keith Busch
block/bdev.c +5 -0
+	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_max_segments = bdev_max_segments(bdev);

How is the max_segments value defined in a way that is meaningful to userspace?

It tells you how many sub-sector vectors you can submit in your readv/writev before it needs to add up to a logical block size.

Ex: 4k logical block size, 4 byte DMA, 256 max segments. You can define 4-byte iov's in your command, but you'll hit the max segment count before you have a valid IO if they're all that small.

Quoted 8 lines

fs/file_attr.c static int file_attr_to_fileattr(const struct file_attr *fattr, · line 159

Quoted 6 lines
fs/file_attr.c +2 -0
@@ -145,6 +155,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;

How is this related?

I had to add a padding field to the struct to account for the implicit hole in 64-bit and to ensure the struct is the same size for 32-bit. It's a reserved field, so we have to ensure the current kernel doesn't support any value here in case we define this field for something else in the future.

#8 lore raw
Quoted 12 lines · Keith Busch
Quoted 10 lines · Christoph Hellwig

block/bdev.c void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask) · line 1376

Quoted 5 lines · Keith Busch
block/bdev.c +5 -0
+	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_max_segments = bdev_max_segments(bdev);

How is the max_segments value defined in a way that is meaningful to userspace?

Quoted 6 lines

It tells you how many sub-sector vectors you can submit in your readv/writev before it needs to add up to a logical block size.

Ex: 4k logical block size, 4 byte DMA, 256 max segments. You can define 4-byte iov's in your command, but you'll hit the max segment count before you have a valid IO if they're all that small.

Ah, makes sense. But besides the missing documentation I think max_segments is a bit of a misleading name for that.

Something like max_vecs_per_block (although we don't expose blocks in the UAPI) or max_vecs_per_granularity (I think grammar wants a word with me for that, though...) might be a bit more suitable.

Quoted 9 lines
Quoted 8 lines

fs/file_attr.c static int file_attr_to_fileattr(const struct file_attr *fattr, · line 159

Quoted 6 lines
fs/file_attr.c +2 -0
@@ -145,6 +155,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;

How is this related?

Quoted 5 lines

I had to add a padding field to the struct to account for the implicit hole in 64-bit and to ensure the struct is the same size for 32-bit. It's a reserved field, so we have to ensure the current kernel doesn't support any value here in case we define this field for something else in the future.

Ah, right.

#9 lore raw
Quoted 20 lines · Christoph Hellwig
Quoted 12 lines · Keith Busch
Quoted 10 lines · Christoph Hellwig

block/bdev.c void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask) · line 1376

Quoted 5 lines · Keith Busch
block/bdev.c +5 -0
+	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_max_segments = bdev_max_segments(bdev);

How is the max_segments value defined in a way that is meaningful to userspace?

Quoted 6 lines

It tells you how many sub-sector vectors you can submit in your readv/writev before it needs to add up to a logical block size.

Ex: 4k logical block size, 4 byte DMA, 256 max segments. You can define 4-byte iov's in your command, but you'll hit the max segment count before you have a valid IO if they're all that small.

Quoted 6 lines

Ah, makes sense. But besides the missing documentation I think max_segments is a bit of a misleading name for that.

Something like max_vecs_per_block (although we don't expose blocks in the UAPI) or max_vecs_per_granularity (I think grammar wants a word with me for that, though...) might be a bit more suitable.

The granularity it has to add up to is defined by the fsx_dio_offset_align attribute. This is a bit long, but to make that relationship clear, how about:

fsx_dio_max_vecs_per_offset_align

#10 lore raw
In reply to Keith Busch via Linux-f2fs-devel
Quoted 9 lines · On Tue, Jul 07, 2026 at 06:18:43PM -0700, Keith Busch via Linux-f2fs-devel…

From: Keith Busch <[email protected]>

Memory alignment constraints for direct io can vary depending on the backing storage hardware. Provide support through file_getattr to report the attributes necessary for applications to know how to construct valid read and write requests.

This seems to be reinventing STATX_DIOALIGN and STATX_DIO_READ_ALIGN. Any reason for this?

- Eric