diff mbox series

[2/5] add a new min_dio_alignment helper

Message ID 20240814045232.21189-3-hch@lst.de (mailing list archive)
State New, archived
Headers show
Series [1/5] statx.h: update to latest kernel UAPI | expand

Commit Message

Christoph Hellwig Aug. 14, 2024, 4:52 a.m. UTC
Add a new C program to find the minimum direct I/O alignment.  This
uses the statx stx_dio_offset_align field if provided, then falls
back to the BLKSSZGET ioctl for block backed file systems and finally
the page size.  It is intended as a more capable replacement for the
_min_dio_alignment bash helper.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 .gitignore              |  1 +
 src/Makefile            |  2 +-
 src/min_dio_alignment.c | 66 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 src/min_dio_alignment.c

Comments

Darrick J. Wong Aug. 14, 2024, 5:12 a.m. UTC | #1
On Wed, Aug 14, 2024 at 06:52:11AM +0200, Christoph Hellwig wrote:
> Add a new C program to find the minimum direct I/O alignment.  This
> uses the statx stx_dio_offset_align field if provided, then falls
> back to the BLKSSZGET ioctl for block backed file systems and finally
> the page size.  It is intended as a more capable replacement for the
> _min_dio_alignment bash helper.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

Looks good now,
Reviewed-by: Darrick J. Wong <djwong@kernel.org>

--D

> ---
>  .gitignore              |  1 +
>  src/Makefile            |  2 +-
>  src/min_dio_alignment.c | 66 +++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 68 insertions(+), 1 deletion(-)
>  create mode 100644 src/min_dio_alignment.c
> 
> diff --git a/.gitignore b/.gitignore
> index 97c7e0014..36083e9d3 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -207,6 +207,7 @@ tags
>  /src/log-writes/replay-log
>  /src/perf/*.pyc
>  /src/fiemap-fault
> +/src/min_dio_alignment
>  
>  # Symlinked files
>  /tests/generic/035.out
> diff --git a/src/Makefile b/src/Makefile
> index 559209be9..b3da59a0e 100644
> --- a/src/Makefile
> +++ b/src/Makefile
> @@ -34,7 +34,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
>  	attr_replace_test swapon mkswap t_attr_corruption t_open_tmpfiles \
>  	fscrypt-crypt-util bulkstat_null_ocount splice-test chprojid_fail \
>  	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
> -	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault
> +	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment
>  
>  EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
>  	      btrfs_crc32c_forged_name.py popdir.pl popattr.py \
> diff --git a/src/min_dio_alignment.c b/src/min_dio_alignment.c
> new file mode 100644
> index 000000000..131f60236
> --- /dev/null
> +++ b/src/min_dio_alignment.c
> @@ -0,0 +1,66 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (c) 2024 Christoph Hellwig
> + */
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <sys/mount.h>
> +#include <sys/ioctl.h>
> +#include <sys/stat.h>
> +#include "statx.h"
> +
> +static int min_dio_alignment(const char *mntpnt, const char *devname)
> +{
> +	struct statx stx = { };
> +	struct stat st;
> +	int fd;
> +
> +	/*
> +	 * If the file system supports STATX_DIOALIGN, use the dio_offset_align
> +	 * member, as that reports exactly the information that we are asking
> +	 * for.
> +	 *
> +	 * STATX_DIOALIGN is only reported on regular files, so use O_TMPFILE
> +	 * to create one without leaving a trace.
> +	 */
> +	fd = open(mntpnt, O_TMPFILE | O_RDWR | O_EXCL, 0600);
> +	if (fd >= 0 &&
> +	    xfstests_statx(fd, "", AT_EMPTY_PATH, STATX_DIOALIGN, &stx) == 0 &&
> +	    (stx.stx_mask & STATX_DIOALIGN))
> +		return stx.stx_dio_offset_align;
> +
> +	/*
> +	 * If we are on a block device and no explicit aligned is reported, use
> +	 * the logical block size as a guestimate.
> +	 */
> +	if (stat(devname, &st) == 0 && S_ISBLK(st.st_mode)) {
> +		int dev_fd = open(devname, O_RDONLY);
> +		int logical_block_size;
> +
> +		if (dev_fd > 0 &&
> +		    fstat(dev_fd, &st) == 0 &&
> +		    S_ISBLK(st.st_mode) &&
> +		    ioctl(dev_fd, BLKSSZGET, &logical_block_size)) {
> +			return logical_block_size;
> +		}
> +	}
> +
> +	/*
> +	 * No support for STATX_DIOALIGN and not a block device:
> +	 * default to PAGE_SIZE.
> +	 */
> +	return getpagesize();
> +}
> +
> +int main(int argc, char **argv)
> +{
> +	if (argc != 3) {
> +		fprintf(stderr, "usage: %s mountpoint devicename\n", argv[0]);
> +		exit(1);
> +	}
> +
> +	printf("%d\n", min_dio_alignment(argv[1], argv[2]));
> +	exit(0);
> +}
> -- 
> 2.43.0
> 
>
Eric Biggers Sept. 10, 2024, 5:09 p.m. UTC | #2
On Wed, Aug 14, 2024 at 06:52:11AM +0200, Christoph Hellwig wrote:
> Add a new C program to find the minimum direct I/O alignment.  This
> uses the statx stx_dio_offset_align field if provided, then falls
> back to the BLKSSZGET ioctl for block backed file systems and finally
> the page size.  It is intended as a more capable replacement for the
> _min_dio_alignment bash helper.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  .gitignore              |  1 +
>  src/Makefile            |  2 +-
>  src/min_dio_alignment.c | 66 +++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 68 insertions(+), 1 deletion(-)
>  create mode 100644 src/min_dio_alignment.c
> 
> diff --git a/.gitignore b/.gitignore
> index 97c7e0014..36083e9d3 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -207,6 +207,7 @@ tags
>  /src/log-writes/replay-log
>  /src/perf/*.pyc
>  /src/fiemap-fault
> +/src/min_dio_alignment
>  
>  # Symlinked files
>  /tests/generic/035.out
> diff --git a/src/Makefile b/src/Makefile
> index 559209be9..b3da59a0e 100644
> --- a/src/Makefile
> +++ b/src/Makefile
> @@ -34,7 +34,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
>  	attr_replace_test swapon mkswap t_attr_corruption t_open_tmpfiles \
>  	fscrypt-crypt-util bulkstat_null_ocount splice-test chprojid_fail \
>  	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
> -	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault
> +	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment
>  
>  EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
>  	      btrfs_crc32c_forged_name.py popdir.pl popattr.py \
> diff --git a/src/min_dio_alignment.c b/src/min_dio_alignment.c
> new file mode 100644
> index 000000000..131f60236
> --- /dev/null
> +++ b/src/min_dio_alignment.c
> @@ -0,0 +1,66 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (c) 2024 Christoph Hellwig
> + */
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <sys/mount.h>
> +#include <sys/ioctl.h>
> +#include <sys/stat.h>
> +#include "statx.h"
> +
> +static int min_dio_alignment(const char *mntpnt, const char *devname)
> +{
> +	struct statx stx = { };
> +	struct stat st;
> +	int fd;
> +
> +	/*
> +	 * If the file system supports STATX_DIOALIGN, use the dio_offset_align
> +	 * member, as that reports exactly the information that we are asking
> +	 * for.
> +	 *
> +	 * STATX_DIOALIGN is only reported on regular files, so use O_TMPFILE
> +	 * to create one without leaving a trace.
> +	 */
> +	fd = open(mntpnt, O_TMPFILE | O_RDWR | O_EXCL, 0600);
> +	if (fd >= 0 &&
> +	    xfstests_statx(fd, "", AT_EMPTY_PATH, STATX_DIOALIGN, &stx) == 0 &&
> +	    (stx.stx_mask & STATX_DIOALIGN))
> +		return stx.stx_dio_offset_align;

If the file doesn't support DIO then this returns 0.

How did you intend to handle this case?

The callers of this program in xfstests don't check for 0.

- Eric
diff mbox series

Patch

diff --git a/.gitignore b/.gitignore
index 97c7e0014..36083e9d3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -207,6 +207,7 @@  tags
 /src/log-writes/replay-log
 /src/perf/*.pyc
 /src/fiemap-fault
+/src/min_dio_alignment
 
 # Symlinked files
 /tests/generic/035.out
diff --git a/src/Makefile b/src/Makefile
index 559209be9..b3da59a0e 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -34,7 +34,7 @@  LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	attr_replace_test swapon mkswap t_attr_corruption t_open_tmpfiles \
 	fscrypt-crypt-util bulkstat_null_ocount splice-test chprojid_fail \
 	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
-	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault
+	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
 	      btrfs_crc32c_forged_name.py popdir.pl popattr.py \
diff --git a/src/min_dio_alignment.c b/src/min_dio_alignment.c
new file mode 100644
index 000000000..131f60236
--- /dev/null
+++ b/src/min_dio_alignment.c
@@ -0,0 +1,66 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2024 Christoph Hellwig
+ */
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/mount.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include "statx.h"
+
+static int min_dio_alignment(const char *mntpnt, const char *devname)
+{
+	struct statx stx = { };
+	struct stat st;
+	int fd;
+
+	/*
+	 * If the file system supports STATX_DIOALIGN, use the dio_offset_align
+	 * member, as that reports exactly the information that we are asking
+	 * for.
+	 *
+	 * STATX_DIOALIGN is only reported on regular files, so use O_TMPFILE
+	 * to create one without leaving a trace.
+	 */
+	fd = open(mntpnt, O_TMPFILE | O_RDWR | O_EXCL, 0600);
+	if (fd >= 0 &&
+	    xfstests_statx(fd, "", AT_EMPTY_PATH, STATX_DIOALIGN, &stx) == 0 &&
+	    (stx.stx_mask & STATX_DIOALIGN))
+		return stx.stx_dio_offset_align;
+
+	/*
+	 * If we are on a block device and no explicit aligned is reported, use
+	 * the logical block size as a guestimate.
+	 */
+	if (stat(devname, &st) == 0 && S_ISBLK(st.st_mode)) {
+		int dev_fd = open(devname, O_RDONLY);
+		int logical_block_size;
+
+		if (dev_fd > 0 &&
+		    fstat(dev_fd, &st) == 0 &&
+		    S_ISBLK(st.st_mode) &&
+		    ioctl(dev_fd, BLKSSZGET, &logical_block_size)) {
+			return logical_block_size;
+		}
+	}
+
+	/*
+	 * No support for STATX_DIOALIGN and not a block device:
+	 * default to PAGE_SIZE.
+	 */
+	return getpagesize();
+}
+
+int main(int argc, char **argv)
+{
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s mountpoint devicename\n", argv[0]);
+		exit(1);
+	}
+
+	printf("%d\n", min_dio_alignment(argv[1], argv[2]));
+	exit(0);
+}