Skip to contentSkip to conversation

improve minalign handling v2

PATCH In review Christoph Hellwig 3 participants 9 messages Aug 14, 2024 – Sep 10, 2024

lore
Share & tools
lore
#0 lore raw

Hi all,

this series improves how xfstests detects minimum direct I/O alignment by adding a C utility that checks the kernel provided value in statx before falling back to the existing strategy.

Changes since v1:
- add a comment about the namespace swizzling in statx.h
- spell a function name correctly
- update .gitignore

#1 lore raw

PATCH 1/5 statx.h: update to latest kernel UAPI

Update the localy provided statx definition to the latest kernel UAPI, and use it unconditionally instead only if no kernel version is provided.

This allows using more recent additions than provided in the system headers.

---
 src/statx.h | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)
src/statx.h +32 -6
index 3f239d791..bae1c86f6 100644
@@ -5,6 +5,14 @@
 #include <sys/syscall.h>
 #include <linux/types.h>
 
+/*
+ * Swizzle the symbol namespace so that we can provide our own version
+ * overriding the system one that might now have all the latest fields
+ * under the standard names even when <sys/stat.h> is included.
+ */
+#define statx_timestamp statx_timestamp_fstests
+#define statx statx_fstests
+
 #ifndef AT_STATX_SYNC_TYPE
 #define AT_STATX_SYNC_TYPE      0x6000  /* Type of synchronisation required from statx() */
 #define AT_STATX_SYNC_AS_STAT   0x0000  /* - Do whatever stat() does */
@@ -28,8 +36,6 @@
 # endif
 #endif
 
-#ifndef STATX_TYPE
-
 /*
  * Timestamp structure for the timestamps in struct statx.
  *
@@ -102,7 +108,8 @@ struct statx {
 	__u64	stx_ino;	/* Inode number */
 	__u64	stx_size;	/* File size */
 	__u64	stx_blocks;	/* Number of 512-byte blocks allocated */
-	__u64	__spare1[1];
+	__u64	stx_attributes_mask; /* Mask to show what's supported in stx_attributes */
+
 	/* 0x40 */
 	struct statx_timestamp	stx_atime;	/* Last access time */
 	struct statx_timestamp	stx_btime;	/* File creation time */
@@ -114,7 +121,18 @@ struct statx {
 	__u32	stx_dev_major;	/* ID of device containing file [uncond] */
 	__u32	stx_dev_minor;
 	/* 0x90 */
-	__u64	__spare2[14];	/* Spare space for future expansion */
+	__u64	stx_mnt_id;
+	__u32	stx_dio_mem_align;	/* Memory buffer alignment for direct I/O */
+	__u32	stx_dio_offset_align;	/* File offset alignment for direct I/O */
+	/* 0xa0 */
+	__u64	stx_subvol;	/* Subvolume identifier */
+	__u32	stx_atomic_write_unit_min;	/* Min atomic write unit in bytes */
+	__u32	stx_atomic_write_unit_max;	/* Max atomic write unit in bytes */
+	/* 0xb0 */
+	__u32   stx_atomic_write_segments_max;	/* Max atomic write segment count */
+	__u32   __spare1[1];
+	/* 0xb8 */
+	__u64	__spare3[9];	/* Spare space for future expansion */
 	/* 0x100 */
 };
 
@@ -139,6 +157,12 @@ struct statx {
 #define STATX_BLOCKS		0x00000400U	/* Want/got stx_blocks */
 #define STATX_BASIC_STATS	0x000007ffU	/* The stuff in the normal stat struct */
 #define STATX_BTIME		0x00000800U	/* Want/got stx_btime */
+#define STATX_MNT_ID		0x00001000U	/* Got stx_mnt_id */
+#define STATX_DIOALIGN		0x00002000U	/* Want/got direct I/O alignment info */
+#define STATX_MNT_ID_UNIQUE	0x00004000U	/* Want/got extended stx_mount_id */
+#define STATX_SUBVOL		0x00008000U	/* Want/got stx_subvol */
+#define STATX_WRITE_ATOMIC	0x00010000U	/* Want/got atomic_write_* fields */
+
 #define STATX_ALL		0x00000fffU	/* All currently supported flags */
 
 /*
@@ -157,9 +181,11 @@ struct statx {
 #define STATX_ATTR_APPEND		0x00000020 /* [I] File is append-only */
 #define STATX_ATTR_NODUMP		0x00000040 /* [I] File is not to be dumped */
 #define STATX_ATTR_ENCRYPTED		0x00000800 /* [I] File requires key to decrypt in fs */
-
 #define STATX_ATTR_AUTOMOUNT		0x00001000 /* Dir: Automount trigger */
-#endif /* STATX_TYPE */
+#define STATX_ATTR_MOUNT_ROOT		0x00002000 /* Root of a mount */
+#define STATX_ATTR_VERITY		0x00100000 /* [I] Verity protected file */
+#define STATX_ATTR_DAX			0x00200000 /* File is currently in DAX state */
+#define STATX_ATTR_WRITE_ATOMIC		0x00400000 /* File supports atomic write operations */
 
 static inline
 int xfstests_statx(int dfd, const char *filename, unsigned flags,
Signature · 1 line
2.43.0
#3 lore raw

PATCH 2/5 add a new min_dio_alignment helper

In reply to Christoph Hellwig

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.

---
 .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
.gitignore +1 -0
index 97c7e0014..36083e9d3 100644
@@ -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
src/Makefile +1 -1
index 559209be9..b3da59a0e 100644
@@ -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 \
src/min_dio_alignment.c new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.43.0
#6 lore raw

PATCH 3/5 xfs/424: don't use _min_dio_alignment

In reply to Christoph Hellwig

xfs/424 tests xfs_db and not the kernel xfs code, and really wants the device sector size and not the minimum direct I/O alignment.

Switch to a direct call of the blockdev utility.

---
 tests/xfs/424 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/xfs/424 +1 -1
index 71d48bec1..6078d3489 100755
@@ -50,7 +50,7 @@ echo "Silence is golden."
 # NOTE: skip attr3, bmapbta, bmapbtd, dir3, dqblk, inodata, symlink
 # rtbitmap, rtsummary, log
 #
-sec_sz=`_min_dio_alignment $SCRATCH_DEV`
+sec_sz=`blockdev --getss $SCRATCH_DEV`
 while [ $sec_sz -le 4096 ]; do
 	sector_sizes="$sector_sizes $sec_sz"
 	sec_sz=$((sec_sz * 2))
Signature · 1 line
2.43.0
#7 lore raw

PATCH 4/5 generic: don't use _min_dio_alignment without a device argument

In reply to Christoph Hellwig

Replace calls to _min_dio_alignment that do not provide a device to check with calls to the feature utility to query the page size, as that is what these calls actually do.

---
 tests/generic/521 | 2 +-
 tests/generic/617 | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tests/generic/521 +1 -1
index 24eab8342..5f3aac570 100755
@@ -22,7 +22,7 @@ nr_ops=$((1000000 * TIME_FACTOR))
 op_sz=$((128000 * LOAD_FACTOR))
 file_sz=$((600000 * LOAD_FACTOR))
 fsx_file=$TEST_DIR/fsx.$seq
-min_dio_sz=$(_min_dio_alignment)
+min_dio_sz=$($here/src/feature -s)
 
 fsx_args=(-q)
 fsx_args+=(-N $nr_ops)
tests/generic/617 +1 -1
index eb50a2da3..297d75538 100755
@@ -24,7 +24,7 @@ nr_ops=$((20000 * TIME_FACTOR))
 op_sz=$((128000 * LOAD_FACTOR))
 file_sz=$((600000 * LOAD_FACTOR))
 fsx_file=$TEST_DIR/fsx.$seq
-min_dio_sz=$(_min_dio_alignment)
+min_dio_sz=$($here/src/feature -s)
 
 fsx_args=(-S 0)
 fsx_args+=(-U)
Signature · 1 line
2.43.0
#8 lore raw

PATCH 5/5 replace _min_dio_alignment with calls to src/min_dio_alignment

In reply to Christoph Hellwig

Use the min_dio_alignment C tool to check the minimum alignment. This allows using the values obtained from statx instead of just guessing based on the sector size and page size.

For tests using the scratch device this sometimes required moving code around a bit to ensure the scratch device is actually mounted before querying the alignment.

---
 common/rc         | 15 ---------------
 tests/generic/091 |  2 +-
 tests/generic/095 |  7 ++++---
 tests/generic/114 |  2 +-
 tests/generic/240 |  2 +-
 tests/generic/252 |  2 +-
 tests/generic/263 |  2 +-
 tests/generic/329 |  2 +-
 tests/generic/330 |  2 +-
 tests/generic/450 |  2 +-
 tests/generic/465 |  2 +-
 tests/generic/538 |  2 +-
 tests/generic/551 |  2 +-
 tests/generic/591 |  2 +-
 tests/xfs/194     | 11 ++++++-----
 tests/xfs/201     | 47 ++++++++++++++++++++++++-----------------------
 tests/xfs/237     |  2 +-
 tests/xfs/239     |  2 +-
 tests/xfs/556     |  2 +-
 19 files changed, 49 insertions(+), 61 deletions(-)
common/rc +0 -15
index afc33bbc2..449ac9fbf 100644
@@ -4296,21 +4296,6 @@ _scale_fsstress_args()
     printf '%s\n' "$args"
 }
 
-#
-# Return the logical block size if running on a block device,
-# else substitute the page size.
-#
-_min_dio_alignment()
-{
-    local dev=$1
-
-    if [ -b "$dev" ]; then
-        blockdev --getss $dev
-    else
-        $here/src/feature -s
-    fi
-}
-
 run_check()
 {
 	echo "# $@" >> $seqres.full 2>&1
tests/generic/091 +1 -1
index 8f7c13da8..5cdf04890 100755
@@ -16,7 +16,7 @@ _require_test
 _require_odirect
 
 psize=`$here/src/feature -s`
-bsize=`_min_dio_alignment $TEST_DEV`
+bsize=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 
 # fsx usage:
 # 
tests/generic/095 +4 -3
index 7a0adf880..47e3b1e61 100755
@@ -16,12 +16,15 @@ _require_scratch
 _require_odirect
 _require_aio
 
+_scratch_mkfs >>$seqres.full 2>&1
+_scratch_mount
+
 iodepth=$((16 * LOAD_FACTOR))
 iodepth_batch=$((8 * LOAD_FACTOR))
 numjobs=$((5 * LOAD_FACTOR))
 fio_config=$tmp.fio
 fio_out=$tmp.fio.out
-blksz=$(_min_dio_alignment $SCRATCH_DEV)
+blksz=`$here/src/min_dio_alignment $SCRATCH_MNT $SCRATCH_DEV`
 cat >$fio_config <<EOF
 [global]
 bs=8k
@@ -82,8 +85,6 @@ EOF
 echo 'bs=$pagesize' >> $fio_config
 
 _require_fio $fio_config
-_scratch_mkfs >>$seqres.full 2>&1
-_scratch_mount
 
 # There's a known EIO failure to report collisions between directio and buffered
 # writes to userspace, refer to upstream linux 5a9d929d6e13. So ignore EIO error
tests/generic/114 +1 -1
index 068ed9e26..e0696ad92 100755
@@ -25,7 +25,7 @@ _require_sparse_files
 _require_aiodio aio-dio-eof-race
 
 # Test does 512 byte DIO, so make sure that'll work
-logical_block_size=`_min_dio_alignment $TEST_DEV`
+logical_block_size=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 
 if [ "$logical_block_size" -gt "512" ]; then
 	_notrun "device block size: $logical_block_size greater than 512"
tests/generic/240 +1 -1
index a333873ec..66a2ff74c 100755
@@ -29,7 +29,7 @@ echo "Silence is golden."
 
 rm -f $TEST_DIR/aiodio_sparse
 
-logical_block_size=`_min_dio_alignment $TEST_DEV`
+logical_block_size=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 fs_block_size=`_get_block_size $TEST_DIR`
 file_size=$((8 * $fs_block_size))
 
tests/generic/252 +1 -1
index 39fa5531f..3ee2b0a67 100755
@@ -49,7 +49,7 @@ nr=640
 bufnr=128
 filesize=$((blksz * nr))
 bufsize=$((blksz * bufnr))
-alignment=`_min_dio_alignment $TEST_DEV`
+alignment=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 
 _require_fs_space $SCRATCH_MNT $((filesize / 1024 * 5 / 4))
 
tests/generic/263 +1 -1
index 62eaec1d7..91cfbe525 100755
@@ -16,7 +16,7 @@ _require_test
 _require_odirect
 
 psize=`$here/src/feature -s`
-bsize=`_min_dio_alignment $TEST_DEV`
+bsize=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 
 run_fsx -N 10000  -o 8192   -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z
 run_fsx -N 10000  -o 128000 -l 500000 -r PSIZE -t BSIZE -w BSIZE -Z
tests/generic/329 +1 -1
index e29a8ca4c..ab37e047f 100755
@@ -40,7 +40,7 @@ nr=640
 bufnr=128
 filesize=$((blksz * nr))
 bufsize=$((blksz * bufnr))
-alignment=`_min_dio_alignment $TEST_DEV`
+alignment=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 
 _require_fs_space $SCRATCH_MNT $((filesize / 1024 * 3 * 5 / 4))
 
tests/generic/330 +1 -1
index 83e1459fa..4fa81f991 100755
@@ -36,7 +36,7 @@ nr=640
 bufnr=128
 filesize=$((blksz * nr))
 bufsize=$((blksz * bufnr))
-alignment=`_min_dio_alignment $TEST_DEV`
+alignment=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 
 _require_fs_space $SCRATCH_MNT $((filesize / 1024 * 3 * 5 / 4))
 
tests/generic/450 +1 -1
index 96e559da6..689f1051e 100755
@@ -31,7 +31,7 @@ _require_test
 _require_odirect
 
 tfile=$TEST_DIR/testfile_${seq}
-ssize=`_min_dio_alignment $TEST_DEV`
+ssize=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 bsize=`_get_block_size $TEST_DIR`
 
 # let's focus on the specific bug that only happens when $ssize <= $bsize
tests/generic/465 +1 -1
index eba3629ab..f8c4ea967 100755
@@ -26,7 +26,7 @@ _require_aiodio aio-dio-append-write-read-race
 _require_test_program "feature"
 
 testfile=$TEST_DIR/$seq.$$
-min_dio_align=`_min_dio_alignment $TEST_DEV`
+min_dio_align=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 page_size=`$here/src/feature -s`
 
 echo "non-aio dio test"
tests/generic/538 +1 -1
index d6933cbb9..b9cf05de1 100755
@@ -28,7 +28,7 @@ _require_test
 _require_aiodio aio-dio-write-verify
 
 localfile=$TEST_DIR/${seq}-aio-dio-write-verify-testfile
-diosize=`_min_dio_alignment $TEST_DEV`
+diosize=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 blocksize=`_get_block_size $TEST_DIR`
 bufsize=$((blocksize * 2))
 truncsize=$((bufsize+diosize))
tests/generic/551 +1 -1
index f2907ac23..4a7f0a638 100755
@@ -19,7 +19,7 @@ _scratch_mkfs > $seqres.full 2>&1
 _scratch_mount
 
 localfile=$SCRATCH_MNT/testfile
-diosize=`_min_dio_alignment $SCRATCH_DEV`
+diosize=`$here/src/min_dio_alignment $SCRATCH_MNT $SCRATCH_DEV`
 
 # The maximum write size and offset are both 32k diosize. So the maximum
 # file size will be (32 * 2)k
tests/generic/591 +1 -1
index c22dc701b..f2fcd6162 100755
@@ -22,7 +22,7 @@ _require_test
 _require_odirect
 _require_test_program "splice-test"
 
-diosize=`_min_dio_alignment $TEST_DEV`
+diosize=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 
 $here/src/splice-test -s $diosize -r $TEST_DIR/a
 $here/src/splice-test -rd $TEST_DIR/a
tests/xfs/194 +6 -5
index 9abd2c321..1f83d534c 100755
@@ -43,11 +43,6 @@ _scratch_mkfs_xfs >/dev/null 2>&1
 # For this test we use block size = 1/8 page size
 pgsize=`$here/src/feature -s`
 blksize=`expr $pgsize / 8`
-secsize=`_min_dio_alignment $SCRATCH_DEV`
-
-if [ $secsize -gt $blksize ];then
-	_notrun "sector size($secsize) too large for platform page size($pgsize)"
-fi
 
 # Filter out file mountpoint and physical location info
 # Input:
@@ -84,6 +79,12 @@ unset XFS_MKFS_OPTIONS
 # we need 512 byte block size, so crc's are turned off
 _scratch_mkfs_xfs -m crc=0 -b size=$blksize >/dev/null 2>&1
 _scratch_mount
+
+secsize=`$here/src/min_dio_alignment $SCRATCH_MNT $SCRATCH_DEV`
+if [ $secsize -gt $blksize ];then
+	_notrun "sector size($secsize) too large for platform page size($pgsize)"
+fi
+
 test "$(_get_block_size $SCRATCH_MNT)" = $blksize || \
 	_notrun "Could not get $blksize-byte blocks"
 
tests/xfs/201 +24 -23

62 lines not on this page · raw message

tests/xfs/237 +1 -1
index 5f264ff44..194cd0459 100755
@@ -41,7 +41,7 @@ nr=640
 bufnr=128
 filesize=$((blksz * nr))
 bufsize=$((blksz * bufnr))
-alignment=`_min_dio_alignment $TEST_DEV`
+alignment=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 
 _require_fs_space $SCRATCH_MNT $((filesize / 1024 * 3 * 5 / 4))
 _require_congruent_file_oplen $SCRATCH_MNT $blksz
tests/xfs/239 +1 -1
index 277bd4548..bfe722c0a 100755
@@ -40,7 +40,7 @@ filesize=$((blksz * nr))
 bufsize=$((blksz * bufnr))
 filesize=$filesize
 bufsize=$bufsize
-alignment=`_min_dio_alignment $TEST_DEV`
+alignment=`$here/src/min_dio_alignment $TEST_DIR $TEST_DEV`
 
 _require_fs_space $SCRATCH_MNT $((filesize / 1024 * 3 * 5 / 4))
 
tests/xfs/556 +1 -1
index 5a2e7fd6d..79e03caf4 100755
@@ -82,7 +82,7 @@ ENDL
 
 # All sector numbers that we feed to the kernel must be in units of 512b, but
 # they also must be aligned to the device's logical block size.
-logical_block_size=$(_min_dio_alignment $SCRATCH_DEV)
+logical_block_size=`$here/src/min_dio_alignment $SCRATCH_MNT $SCRATCH_DEV`
 kernel_sectors_per_device_lba=$((logical_block_size / 512))
 
 # Mark as bad one of the device LBAs in the middle of the extent.  Target the
Signature · 1 line
2.43.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] statx.h: update to latest kernel UAPI

In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig

Update the localy provided statx definition to the latest kernel UAPI, and use it unconditionally instead only if no kernel version is provided.

This allows using more recent additions than provided in the system headers.

Looks good to me,

--D

Quoted 93 lines
---
 src/statx.h | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)
src/statx.h +32 -6
index 3f239d791..bae1c86f6 100644
@@ -5,6 +5,14 @@
 #include <sys/syscall.h>
 #include <linux/types.h>
 
+/*
+ * Swizzle the symbol namespace so that we can provide our own version
+ * overriding the system one that might now have all the latest fields
+ * under the standard names even when <sys/stat.h> is included.
+ */
+#define statx_timestamp statx_timestamp_fstests
+#define statx statx_fstests
+
 #ifndef AT_STATX_SYNC_TYPE
 #define AT_STATX_SYNC_TYPE      0x6000  /* Type of synchronisation required from statx() */
 #define AT_STATX_SYNC_AS_STAT   0x0000  /* - Do whatever stat() does */
@@ -28,8 +36,6 @@
 # endif
 #endif
 
-#ifndef STATX_TYPE
-
 /*
  * Timestamp structure for the timestamps in struct statx.
  *
@@ -102,7 +108,8 @@ struct statx {
 	__u64	stx_ino;	/* Inode number */
 	__u64	stx_size;	/* File size */
 	__u64	stx_blocks;	/* Number of 512-byte blocks allocated */
-	__u64	__spare1[1];
+	__u64	stx_attributes_mask; /* Mask to show what's supported in stx_attributes */
+
 	/* 0x40 */
 	struct statx_timestamp	stx_atime;	/* Last access time */
 	struct statx_timestamp	stx_btime;	/* File creation time */
@@ -114,7 +121,18 @@ struct statx {
 	__u32	stx_dev_major;	/* ID of device containing file [uncond] */
 	__u32	stx_dev_minor;
 	/* 0x90 */
-	__u64	__spare2[14];	/* Spare space for future expansion */
+	__u64	stx_mnt_id;
+	__u32	stx_dio_mem_align;	/* Memory buffer alignment for direct I/O */
+	__u32	stx_dio_offset_align;	/* File offset alignment for direct I/O */
+	/* 0xa0 */
+	__u64	stx_subvol;	/* Subvolume identifier */
+	__u32	stx_atomic_write_unit_min;	/* Min atomic write unit in bytes */
+	__u32	stx_atomic_write_unit_max;	/* Max atomic write unit in bytes */
+	/* 0xb0 */
+	__u32   stx_atomic_write_segments_max;	/* Max atomic write segment count */
+	__u32   __spare1[1];
+	/* 0xb8 */
+	__u64	__spare3[9];	/* Spare space for future expansion */
 	/* 0x100 */
 };
 
@@ -139,6 +157,12 @@ struct statx {
 #define STATX_BLOCKS		0x00000400U	/* Want/got stx_blocks */
 #define STATX_BASIC_STATS	0x000007ffU	/* The stuff in the normal stat struct */
 #define STATX_BTIME		0x00000800U	/* Want/got stx_btime */
+#define STATX_MNT_ID		0x00001000U	/* Got stx_mnt_id */
+#define STATX_DIOALIGN		0x00002000U	/* Want/got direct I/O alignment info */
+#define STATX_MNT_ID_UNIQUE	0x00004000U	/* Want/got extended stx_mount_id */
+#define STATX_SUBVOL		0x00008000U	/* Want/got stx_subvol */
+#define STATX_WRITE_ATOMIC	0x00010000U	/* Want/got atomic_write_* fields */
+
 #define STATX_ALL		0x00000fffU	/* All currently supported flags */
 
 /*
@@ -157,9 +181,11 @@ struct statx {
 #define STATX_ATTR_APPEND		0x00000020 /* [I] File is append-only */
 #define STATX_ATTR_NODUMP		0x00000040 /* [I] File is not to be dumped */
 #define STATX_ATTR_ENCRYPTED		0x00000800 /* [I] File requires key to decrypt in fs */
-
 #define STATX_ATTR_AUTOMOUNT		0x00001000 /* Dir: Automount trigger */
-#endif /* STATX_TYPE */
+#define STATX_ATTR_MOUNT_ROOT		0x00002000 /* Root of a mount */
+#define STATX_ATTR_VERITY		0x00100000 /* [I] Verity protected file */
+#define STATX_ATTR_DAX			0x00200000 /* File is currently in DAX state */
+#define STATX_ATTR_WRITE_ATOMIC		0x00400000 /* File supports atomic write operations */
 
 static inline
 int xfstests_statx(int dfd, const char *filename, unsigned flags,
Signature · 1 line
2.43.0
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] add a new min_dio_alignment helper

In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig

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.

Looks good now,

--D

Quoted 108 lines
---
 .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
.gitignore +1 -0
index 97c7e0014..36083e9d3 100644
@@ -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
src/Makefile +1 -1
index 559209be9..b3da59a0e 100644
@@ -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 \
src/min_dio_alignment.c new file +66 -0
new file mode 100644
index 000000000..131f60236
@@ -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);
+}
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/5] add a new min_dio_alignment helper

In reply to Christoph Hellwig
Quoted 67 lines · Christoph Hellwig

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.

---
 .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
.gitignore +1 -0
index 97c7e0014..36083e9d3 100644
@@ -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
src/Makefile +1 -1
index 559209be9..b3da59a0e 100644
@@ -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 \
src/min_dio_alignment.c new file +22 -0
new file mode 100644
index 000000000..131f60236
@@ -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

src/min_dio_alignment.c line 32

Quoted 10 lines
src/min_dio_alignment.c new file +10 -0
+	 * 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