Skip to contentSkip to conversation

generic: add test for garbled stat output from negative nanoseconds

PATCH Superseded koraynilay 2 participants 2 messages Nov 20 – Nov 21

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

koraynilay

author
#0 lore raw

PATCH 1/1 generic: add test for garbled stat output from negative nanoseconds

Some filesystems don't use set_normalized_timespec64() to handle their timestamps format conversion, leading to a garbled stat command output for timestamps.

For example:

     from cache:   1960-01-01 01:01:01.123456789 +0000

after remount: 1960-01-01 01:01:02.()*+,--00 +0000

This bug also violates a struct timespec64 invariant, as documented in the set_normalized_timespec64() kernel-doc:
[...]
* Note: The tv_nsec part is always in the range of 0 <= tv_nsec <
* NSEC_PER_SEC.
* For negative values only the tv_sec field is negative ! */

Example patches that fix the problem[1][2].

---
Thanks.

 tests/generic/812     | 43 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/812.out |  3 +++
 2 files changed, 46 insertions(+)
 create mode 100755 tests/generic/812
 create mode 100644 tests/generic/812.out
tests/generic/812 new file +43 -0
new file mode 100755
index 00000000..de57fab6
@@ -0,0 +1,43 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 koraynilay.  All Rights Reserved.
+#
+# FS QA Test No. 812
+#
+# Based on generic/258, but tests timestamps prior to epoch with sub-second precision
+#
+# ksmbd, cifs and other filesystem drivers violate timespec64 non-negative
+# nanoseconds constraint, garbling output of stat
+# See commit 6a75ce0866bb1efe67947fc5536a476381d7a9d9 ("smb: client: fix negative timestamps being read from smb")
+# See commit f2f961e0983c8f339f842e1a8c351e6963793868 ("ksmbd: fix negative timestamps being saved to disk")
+#
+. ./common/preamble
+_begin_fstest auto quick bigtime
+
+# Import common functions.
+
+_require_test
+_require_negative_timestamps
+
+TESTFILE=$TEST_DIR/timestamp-test.txt
+# we use 123456700 instead of 123456789 because some filesystems support
+# only 100ns precision
+DATE="1960-01-01 01:01:01.123456700 UTC"
+
+_cleanup()
+{
+	cd /
+	rm -f $tmp.* "$TESTFILE"
+}
+
+# Create a file with a timestamp prior to the epoch with nanosecond precision
+touch -d "$DATE" "$TESTFILE"
+
+TZ=UTC stat -c "%y" "$TESTFILE"
+
+# unmount, remount, and check the timestamp
+_test_cycle_mount
+
+TZ=UTC stat -c "%y" "$TESTFILE"
+
+_exit 0
tests/generic/812.out new file +3 -0
new file mode 100644
index 00000000..f0267fbb
@@ -0,0 +1,3 @@
+QA output created by 812
+1960-01-01 01:01:01.123456700 +0000
+1960-01-01 01:01:01.123456700 +0000

base-commit: 71641219a506ac62bfa231592394265ca812670e

Signature · 1 line
2.56.0
#1 lore raw
Quoted 38 lines · koraynilay

Some filesystems don't use set_normalized_timespec64() to handle their timestamps format conversion, leading to a garbled stat command output for timestamps.

For example:

     from cache:   1960-01-01 01:01:01.123456789 +0000

after remount: 1960-01-01 01:01:02.()*+,--00 +0000

This bug also violates a struct timespec64 invariant, as documented in the set_normalized_timespec64() kernel-doc:
[...]
* Note: The tv_nsec part is always in the range of 0 <= tv_nsec <
* NSEC_PER_SEC.
* For negative values only the tv_sec field is negative ! */

Example patches that fix the problem[1][2].

---
Thanks.

 tests/generic/812     | 43 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/812.out |  3 +++
 2 files changed, 46 insertions(+)
 create mode 100755 tests/generic/812
 create mode 100644 tests/generic/812.out
tests/generic/812 new file +2 -0
new file mode 100755
index 00000000..de57fab6
@@ -0,0 +1,43 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0

tests/generic/812 line 12

Quoted 10 lines
tests/generic/812 new file +10 -0
+# Copyright (c) 2026 koraynilay.  All Rights Reserved.
+#
+# FS QA Test No. 812
+#
+# Based on generic/258, but tests timestamps prior to epoch with sub-second precision
+#
+# ksmbd, cifs and other filesystem drivers violate timespec64 non-negative
+# nanoseconds constraint, garbling output of stat
+# See commit 6a75ce0866bb1efe67947fc5536a476381d7a9d9 ("smb: client: fix negative timestamps being read from smb")
+# See commit f2f961e0983c8f339f842e1a8c351e6963793868 ("ksmbd: fix negative timestamps being saved to disk")

A 12-character commit ID is enough. Also, please wrap the lines so they aren't too long。

_fixed_by_fs_commit cifs 6a75ce0866bb \
"smb: client: fix negative timestamps being read from smb"
_fixed_by_fs_commit cifs f2f961e0983c \
"ksmbd: fix negative timestamps being saved to disk"

tests/generic/812 line 17

Quoted 5 lines
tests/generic/812 new file +5 -0
+#
+. ./common/preamble
+_begin_fstest auto quick bigtime
+
+# Import common functions.

Useless ^^

Quoted 5 lines
tests/generic/812 new file +5 -0
+
+_require_test
+_require_negative_timestamps
+
+TESTFILE=$TEST_DIR/timestamp-test.txt

tests/generic/812 line 32

Quoted 10 lines
tests/generic/812 new file +10 -0
+# we use 123456700 instead of 123456789 because some filesystems support
+# only 100ns precision
+DATE="1960-01-01 01:01:01.123456700 UTC"
+
+_cleanup()
+{
+	cd /
+	rm -f $tmp.* "$TESTFILE"
+}
+

rm -f "$TESTFILE"

Quoted 25 lines
tests/generic/812 new file +11 -0
+# Create a file with a timestamp prior to the epoch with nanosecond precision
+touch -d "$DATE" "$TESTFILE"
+
+TZ=UTC stat -c "%y" "$TESTFILE"
+
+# unmount, remount, and check the timestamp
+_test_cycle_mount
+
+TZ=UTC stat -c "%y" "$TESTFILE"
+
+_exit 0
tests/generic/812.out new file +3 -0
new file mode 100644
index 00000000..f0267fbb
@@ -0,0 +1,3 @@
+QA output created by 812
+1960-01-01 01:01:01.123456700 +0000
+1960-01-01 01:01:01.123456700 +0000

base-commit: 71641219a506ac62bfa231592394265ca812670e

Signature · 1 line
2.56.0