generic: add a test for multigrain timestamps
RFC PATCH 1/1 generic: add a test for multigrain timestamps
Ensure that the mtime and ctime apparently change, even when there are multiple writes in quick succession. Since this is opt-in for in-kernel filesystems and there is no way to test for this feature in userland, limit it to just ext4, f2fs, btrfs, tmpfs and xfs for now.
---
I'd like to start testing this to make sure it doesn't break, but it's not universally supported on all filesystems. Is an explicit allowlist like this the right approach?
--- src/Makefile | 2 +- src/mgtime.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/generic/999 | 35 ++++++++++++++++ tests/generic/999.out | 1 + 4 files changed, 145 insertions(+), 1 deletion(-)
src/Makefile +1 -1
@@ -36,7 +36,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ 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 min_dio_alignment \ - rw_hint btrfs_ioctl + rw_hint btrfs_ioctl mgtime EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \ btrfs_crc32c_forged_name.py popdir.pl popattr.py \
src/mgtime.c new file +108 -0
108 lines not on this page · raw message
tests/generic/999 new file +35 -0
@@ -0,0 +1,35 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2026, Jeff Layton <[email protected]> +# +# FS QA Test No. 999 +# +# Multigrain time test +# +# Open a file, and do 1 byte writes to it, and statx the file for +# the ctime and mtime after each write. Ensure that they have changed +# since the previous write. +# +. ./common/preamble +_begin_fstest auto quick + +# Override the default cleanup function. +_require_test_program mgtime + +# Multigrain timestamps are opt-in per filesystem; only allowlist the ones +# known to support them. +case "$FSTYP" in +ext4|btrfs|tmpfs|xfs|f2fs) + ;; +*) + _notrun "multigrain timestamps not supported on $FSTYP" + ;; +esac + +testfile="$TEST_DIR/test_mgtime_file.$$" + +$here/src/mgtime $testfile + +# success, all done +status=0 +exit
tests/generic/999.out new file +1 -0
@@ -0,0 +1 @@ +QA output created by 999
---
base-commit: 333c067ccf2f389f5558860336a7fa64157cd46e
change-id: 20261027-mgtime-64194b4e22b4
Best regards,
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 10 lines · Jeff Layton
Ensure that the mtime and ctime apparently change, even when there are multiple writes in quick succession. Since this is opt-in for in-kernel filesystems and there is no way to test for this feature in userland, limit it to just ext4, f2fs, btrfs, tmpfs and xfs for now.
Signed-off-by Jeff Layton <[email protected]>---
I'd like to start testing this to make sure it doesn't break, but it's not universally supported on all filesystems. Is an explicit allowlist like this the right approach?
Can we please expose the fact that multi-grain timestamp are supported from the kernel? This seems like it would be just one bit we'd need in sttx or something similar.
Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Jeff Layton
Ensure that the mtime and ctime apparently change, even when there are multiple writes in quick succession. Since this is opt-in for in-kernel filesystems and there is no way to test for this feature in userland, limit it to just ext4, f2fs, btrfs, tmpfs and xfs for now.
Signed-off-by Jeff Layton <[email protected]>---
I'd like to start testing this to make sure it doesn't break, but it's not universally supported on all filesystems. Is an explicit allowlist like this the right approach?
Quoted 3 lines
Can we please expose the fact that multi-grain timestamp are supported from the kernel? This seems like it would be just one bit we'd need in sttx or something similar.
I'd been resisting that, but maybe it's a good idea. I'll see what I can come up with. We can probably just add a flag to stx_attributes?
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 13 lines · Jeff Layton
Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Jeff Layton
Ensure that the mtime and ctime apparently change, even when there are multiple writes in quick succession. Since this is opt-in for in-kernel filesystems and there is no way to test for this feature in userland, limit it to just ext4, f2fs, btrfs, tmpfs and xfs for now.
Signed-off-by Jeff Layton <[email protected]>---
I'd like to start testing this to make sure it doesn't break, but it's not universally supported on all filesystems. Is an explicit allowlist like this the right approach?
Quoted 6 lines
Quoted 3 lines
Can we please expose the fact that multi-grain timestamp are supported from the kernel? This seems like it would be just one bit we'd need in sttx or something similar.
I'd been resisting that, but maybe it's a good idea. I'll see what I can come up with. We can probably just add a flag to
stx_attributes?
Yes, ever since STATX_ATTR_DAX/STATX_ATTR_WRITE_ATOMIC these don't seem to be tied to actual inode flags any more.
Quoted 5 lines · Jeff Layton
Ensure that the mtime and ctime apparently change, even when there are multiple writes in quick succession. Since this is opt-in for in-kernel filesystems and there is no way to test for this feature in userland, limit it to just ext4, f2fs, btrfs, tmpfs and xfs for now.
Quoted 8 lines
Signed-off-by Jeff Layton <[email protected]>---
I'd like to start testing this to make sure it doesn't break, but it's not universally supported on all filesystems. Is an explicit allowlist like this the right approach?--- src/Makefile | 2 +- src/mgtime.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++
Add src/mgtime to .gitignore too.
Quoted 27 lines
tests/generic/999 | 35 ++++++++++++++++ tests/generic/999.out | 1 + 4 files changed, 145 insertions(+), 1 deletion(-)src/Makefile +1
-1@@ -36,7 +36,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ 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 min_dio_alignment \ - rw_hint btrfs_ioctl + rw_hint btrfs_ioctl mgtime EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \ btrfs_crc32c_forged_name.py popdir.pl popattr.py \src/mgtime.c new file +4
-0@@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Older Linux kernels always use coarse-grained timestamps, with a +* resolution of around 1 jiffy. Writes that are done in quick succession
src/mgtime.c line 14
Quoted 10 lines
src/mgtime.c new file +10
-0+* on those kernels apparent change to the ctime or mtime. +* +* Newer kernels attempt to ensure that fine-grained timestamps are used +* when the mtime or ctime are queried from userland. +* +* Open a file and do a 1 byte write to it and then statx the mtime and ctime. +* Do that in a loop 1000 times and ensure that the value is different from +* the last. +* +* Copyright (c) 2023: Jeff Layton <[email protected]>
^^^^
2026 ?Quoted 37 lines
src/mgtime.c new file +37
-0+*/ +#define _GNU_SOURCE 1 +#include <stdio.h> +#include <fcntl.h> +#include <errno.h> +#include <sys/stat.h> +#include <unistd.h> +#include <stdint.h> +#include <getopt.h> +#include <stdbool.h> + +#define NUM_WRITES 1000 + +static int64_t statx_ts_cmp (struct statx_timestamp *ts1,struct statx_timestamp *ts2) +{ +int64_t ret = ts2->tv_sec - ts1->tv_sec; + +if (ret) +return ret; + + ret = ts2->tv_nsec; + ret -= ts1->tv_nsec; + +return ret; +} + +int main (int argc,char **argv) +{ +int ret, fd, i; +struct statx stx = { }; +struct statx_timestamp ctime, mtime; +bool verbose = false; + +while ((i =getopt (argc, argv,"v" )) !=-1 ) { +switch (i) { +case 'v' : + verbose = true;
src/mgtime.c line 61
Quoted 10 lines
src/mgtime.c new file +10
-0+break ; + } + } + +if (argc <2 ) { + errno = -EINVAL ; +perror ("usage" ); + } + + fd =open (argv[1 ],O_WRONLY |O_CREAT |O_TRUNC ,0644 );
If you use "-v" option, won't argv[1] be "-v"? Maybe you should check optind, and use argv[optind].
src/mgtime.c line 67
Quoted 6 lines
src/mgtime.c new file +6
-0+if (fd <0 ) { +perror ("open" ); +return 1 ; + } + + ret =statx (fd,"" ,AT_EMPTY_PATH ,STATX_MTIME |STATX_CTIME , &stx);
statx() is a relatively new system call that might not be supported on older systems. Perhaps you could refer to src/stat_test.c and use xfstests_statx() ?
Quoted 17 lines
src/mgtime.c new file +17
-0+if (ret <0 ) { +perror ("stat" ); +return 1 ; + } + + ctime = stx.stx_ctime; + mtime = stx.stx_mtime; + +for (i =0 ; i <NUM_WRITES ; ++i) { +ssize_t written; + + written =write (fd,"a" ,1 ); +if (written <0 ) { +perror ("write" ); +return 1 ; + } +
src/mgtime.c line 94
Quoted 10 lines
src/mgtime.c new file +10
-0+ ret =statx (fd,"" ,AT_EMPTY_PATH ,STATX_MTIME |STATX_CTIME , &stx); +if (ret <0 ) { +perror ("stat" ); +return 1 ; + } + +if (verbose) +printf ("%d: %llu.%u\n" , i, stx.stx_ctime.tv_sec, stx.stx_ctime.tv_nsec); + +if (!statx_ts_cmp (&ctime, &stx.stx_ctime)) {
It looks like this currently only checks whether the timestamp changed. Changing the condition from checking non-zero to "<= 0" might can also catch clock rollback bugs (even if that scenario seems unlikely:)
Quoted 24 lines
src/mgtime.c new file +14
-0+printf ("Duplicate ctime after write!\n" ); +return 1 ; + } + +if (!statx_ts_cmp (&mtime, &stx.stx_mtime)) { +printf ("Duplicate mtime after write!\n" ); +return 1 ; + } + + ctime = stx.stx_ctime; + mtime = stx.stx_mtime; + } +return 0 ; +}tests/generic/999 new file +4
-0@@ -0,0 +1,35 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright (c) 2026, Jeff Layton <[email protected]> +#
tests/generic/999 line 14
Quoted 10 lines
tests/generic/999 new file +10
-0+# FS QA Test No. 999 +# +# Multigrain time test +# +# Open a file, and do 1 byte writes to it, and statx the file for +# the ctime and mtime after each write. Ensure that they have changed +# since the previous write. +# +. ./common/preamble +_begin_fstest auto quick
I just checked doc/group-names.txt and noticed that it only lists the 'atime' group. I think it would make sense to introduce a 'timestamp' group. There are already quite a few tests covering atime, ctime, and mtime in xfstests, but they all seem to be lumped under the 'atime' group for now.
Of course, if you'd rather not deal with introducing a new group in this patch, sticking with the 'atime' group for now is totally fine.
tests/generic/999 line 16
Quoted 2 lines
tests/generic/999 new file +2
-0+ +# Override the default cleanup function.
If you want to clean up "$TEST_DIR/test_mgtime_file.$$" in _cleanup, you can define a local _cleanup function here. Otherwise, you can drop the stale comment above.
tests/generic/999 line 17
Quoted 1 line
tests/generic/999 new file +1
-0+_require_test_program mgtime
_require_test
tests/generic/999 line 27
Quoted 10 lines
tests/generic/999 new file +10
-0+ +# Multigrain timestamps are opt-in per filesystem; only allowlist the ones +# known to support them. +case "$FSTYP" in +ext4|btrfs|tmpfs|xfs|f2fs) + ;; +*) + _notrun "multigrain timestamps not supported on $FSTYP" + ;; +esac
We could wrap this logic into a _require_multigrain_timestamps() helper. If there is a better way than probing FSTYP, we can always discuss and update it later.
tests/generic/999 line 30
Quoted 3 lines
tests/generic/999 new file +3
-0+ +testfile="$TEST_DIR/test_mgtime_file.$$" +
rm -f $testfile
tests/generic/999 line 31
Quoted 1 line
tests/generic/999 new file +1
-0+$here/src/mgtime $testfile
echo "Silence is golden"
tests/generic/999 line 35
Quoted 4 lines
tests/generic/999 new file +4
-0+ +# success, all done +status=0 +exit
_exit 0
Thanks,
Zorro
Quoted 16 lines
tests/generic/999.out new file +1
-1@@ -0,0 +1 @@ +QA output created by 999 ---base-commit:
333c067ccf2f389f5558860336a7fa64157cd46e
change-id: 20261027-mgtime-64194b4e22b4Best regards,
Signature · 1 line
Jeff Layton <[email protected]>