diff mbox series

[v3,2/8] vfs: support STATX_DIOALIGN on block devices

Message ID 20220616201506.124209-3-ebiggers@kernel.org (mailing list archive)
State New, archived
Headers show
Series make statx() return DIO alignment information | expand

Commit Message

Eric Biggers June 16, 2022, 8:15 p.m. UTC
From: Eric Biggers <ebiggers@google.com>

Add support for STATX_DIOALIGN to block devices, so that direct I/O
alignment restrictions are exposed to userspace in a generic way.

Note that this breaks the tradition of stat operating only on the block
device node, not the block device itself.  However, it was felt that
doing this is preferable, in order to make the interface useful and
avoid needing separate interfaces for regular files and block devices.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/stat.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

Comments

Christoph Hellwig June 26, 2022, 7:48 a.m. UTC | #1
On Thu, Jun 16, 2022 at 01:15:00PM -0700, Eric Biggers wrote:
> +/* Handle STATX_DIOALIGN for block devices. */
> +static inline void handle_bdev_dioalign(struct path *path, u32 request_mask,
> +					struct kstat *stat)
> +{
> +#ifdef CONFIG_BLOCK
> +	struct inode *inode;
> +	struct block_device *bdev;
> +	unsigned int lbs;
> +
> +	if (likely(!(request_mask & STATX_DIOALIGN)))
> +		return;
> +
> +	inode = d_backing_inode(path->dentry);
> +	if (!S_ISBLK(inode->i_mode))
> +		return;
> +
> +	bdev = blkdev_get_no_open(inode->i_rdev);
> +	if (!bdev)
> +		return;
> +
> +	lbs = bdev_logical_block_size(bdev);
> +	stat->dio_mem_align = lbs;
> +	stat->dio_offset_align = lbs;
> +	stat->result_mask |= STATX_DIOALIGN;
> +
> +	blkdev_put_no_open(bdev);
> +#endif /* CONFIG_BLOCK */
> +}

This helper should go into block/bdev.c with the STATX_DIOALIGN and
S_ISBLK checks lifted into the caller.  I'd also pass just the inode
here.

Note that this also needs to account for the reduced memory alignment
that landed in the block tree eventually.
diff mbox series

Patch

diff --git a/fs/stat.c b/fs/stat.c
index a7930d7444830..c1ce447c1a383 100644
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -5,6 +5,7 @@ 
  *  Copyright (C) 1991, 1992  Linus Torvalds
  */
 
+#include <linux/blkdev.h>
 #include <linux/export.h>
 #include <linux/mm.h>
 #include <linux/errno.h>
@@ -198,6 +199,35 @@  int getname_statx_lookup_flags(int flags)
 	return lookup_flags;
 }
 
+/* Handle STATX_DIOALIGN for block devices. */
+static inline void handle_bdev_dioalign(struct path *path, u32 request_mask,
+					struct kstat *stat)
+{
+#ifdef CONFIG_BLOCK
+	struct inode *inode;
+	struct block_device *bdev;
+	unsigned int lbs;
+
+	if (likely(!(request_mask & STATX_DIOALIGN)))
+		return;
+
+	inode = d_backing_inode(path->dentry);
+	if (!S_ISBLK(inode->i_mode))
+		return;
+
+	bdev = blkdev_get_no_open(inode->i_rdev);
+	if (!bdev)
+		return;
+
+	lbs = bdev_logical_block_size(bdev);
+	stat->dio_mem_align = lbs;
+	stat->dio_offset_align = lbs;
+	stat->result_mask |= STATX_DIOALIGN;
+
+	blkdev_put_no_open(bdev);
+#endif /* CONFIG_BLOCK */
+}
+
 /**
  * vfs_statx - Get basic and extra attributes by filename
  * @dfd: A file descriptor representing the base dir for a relative filename
@@ -230,11 +260,16 @@  static int vfs_statx(int dfd, struct filename *filename, int flags,
 		goto out;
 
 	error = vfs_getattr(&path, stat, request_mask, flags);
+
 	stat->mnt_id = real_mount(path.mnt)->mnt_id;
 	stat->result_mask |= STATX_MNT_ID;
+
 	if (path.mnt->mnt_root == path.dentry)
 		stat->attributes |= STATX_ATTR_MOUNT_ROOT;
 	stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
+
+	handle_bdev_dioalign(&path, request_mask, stat);
+
 	path_put(&path);
 	if (retry_estale(error, lookup_flags)) {
 		lookup_flags |= LOOKUP_REVAL;