Skip to contentSkip to conversation

generic: new test to check nlink returned by fstat()

PATCH Superseded ChenXiaoSong 3 participants 6 messages Jul 24 – Jul 28

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

ChenXiaoSong

author reviewer
#0 lore raw

v3 cover generic: new test to check nlink returned by fstat()

From: ChenXiaoSong <[email protected]>

v2->v3: use xfs_io -c 'stat -r' to call fstat().

v2: https://lore.kernel.org/linux-cifs/[email protected]/

ChenXiaoSong (2):
generic/002: simplify the hardlink count test
generic: new test to check nlink returned by fstat()

 tests/generic/002     | 16 +++++-------
 tests/generic/002.out |  2 +-
 tests/generic/798     | 60 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/798.out |  2 ++
 4 files changed, 69 insertions(+), 11 deletions(-)
 create mode 100755 tests/generic/798
 create mode 100644 tests/generic/798.out
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH v3 1/2 generic/002: simplify the hardlink count test

From: ChenXiaoSong <[email protected]>

Add the test to the hardlink group and replace the enumerated link counts with arithmetic loops.

Clean up the test header, use "Silence is golden" success message, and return the test status explicitly.

---
 tests/generic/002     | 16 ++++++----------
 tests/generic/002.out |  2 +-
 2 files changed, 7 insertions(+), 11 deletions(-)
tests/generic/002 +6 -10
index 6df57a7a..9b210705 100755
@@ -2,12 +2,12 @@
 # SPDX-License-Identifier: GPL-2.0
 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
 #
-# FS QA Test No. 002
+# FS QA Test 002
 #
 # simple inode link count test for a regular file
 #
 . ./common/preamble
-_begin_fstest metadata udf auto quick
+_begin_fstest metadata udf auto quick hardlink
 
 # Import common functions.
 . ./common/filter
@@ -17,14 +17,11 @@ status=0	# success is the default!
 _require_test
 _require_hardlinks
 
-echo "Silence is goodness ..."
-
 # ensure target directory exists
 mkdir `dirname $TEST_DIR/tmp` 2>/dev/null
 
 touch $TEST_DIR/tmp.1
-for l in 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
-do
+for ((l = 2; l <= 20; l++)); do
     ln $TEST_DIR/tmp.1 $TEST_DIR/tmp.$l
     x=`$here/src/lstat64 $TEST_DIR/tmp.1 | sed -n -e '/ Links: /s/.*Links: *//p'`
     if [ "$l" -ne $x ]
@@ -35,8 +32,7 @@ do
     fi
 done
 
-for l in 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
-do
+for ((l = 20; l >= 1; l--)); do
     x=`$here/src/lstat64 $TEST_DIR/tmp.1 | sed -n -e '/ Links: /s/.*Links: *//p'`
     if [ "$l" -ne $x ]
     then
@@ -47,5 +43,5 @@ do
     rm -f $TEST_DIR/tmp.$l
 done
 
-# success, all done
-exit
+echo "Silence is golden"
+exit $status
tests/generic/002.out +1 -1
index 11426b54..61705c7c 100644
@@ -1,2 +1,2 @@
 QA output created by 002
-Silence is goodness ...
+Silence is golden
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 xfstests 1/2] generic/002: simplify the hardlink count test

Quoted 11 lines · ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Add the test to the hardlink group and replace the enumerated link counts with arithmetic loops.

Clean up the test header, use "Silence is golden" success message, and return the test status explicitly.

---

generic/002 is quite an old test case, and some of its code differs from the current standard conventions in fstests. If you just want to add it to the "hardlink" group, updating the group is sufficient.

However, if you'd like to take this opportunity to refactor some of the code, besides your changes, you can refer to the review points on [PATCH 2/2] as well as using _require_test_program "lstat64"

Thanks,
Zorro

Quoted 68 lines
 tests/generic/002     | 16 ++++++----------
 tests/generic/002.out |  2 +-
 2 files changed, 7 insertions(+), 11 deletions(-)
tests/generic/002 +6 -10
index 6df57a7a..9b210705 100755
@@ -2,12 +2,12 @@
 # SPDX-License-Identifier: GPL-2.0
 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
 #
-# FS QA Test No. 002
+# FS QA Test 002
 #
 # simple inode link count test for a regular file
 #
 . ./common/preamble
-_begin_fstest metadata udf auto quick
+_begin_fstest metadata udf auto quick hardlink
 
 # Import common functions.
 . ./common/filter
@@ -17,14 +17,11 @@ status=0	# success is the default!
 _require_test
 _require_hardlinks
 
-echo "Silence is goodness ..."
-
 # ensure target directory exists
 mkdir `dirname $TEST_DIR/tmp` 2>/dev/null
 
 touch $TEST_DIR/tmp.1
-for l in 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
-do
+for ((l = 2; l <= 20; l++)); do
     ln $TEST_DIR/tmp.1 $TEST_DIR/tmp.$l
     x=`$here/src/lstat64 $TEST_DIR/tmp.1 | sed -n -e '/ Links: /s/.*Links: *//p'`
     if [ "$l" -ne $x ]
@@ -35,8 +32,7 @@ do
     fi
 done
 
-for l in 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
-do
+for ((l = 20; l >= 1; l--)); do
     x=`$here/src/lstat64 $TEST_DIR/tmp.1 | sed -n -e '/ Links: /s/.*Links: *//p'`
     if [ "$l" -ne $x ]
     then
@@ -47,5 +43,5 @@ do
     rm -f $TEST_DIR/tmp.$l
 done
 
-# success, all done
-exit
+echo "Silence is golden"
+exit $status
tests/generic/002.out +1 -1
index 11426b54..61705c7c 100644
@@ -1,2 +1,2 @@
 QA output created by 002
-Silence is goodness ...
+Silence is golden
Signature · 1 line
2.43.0
author reviewer
#3 lore raw

PATCH v3 2/2 generic: new test to check nlink returned by fstat()

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Add a test to verify that fstat(2) returns the expected st_nlink value as hardlinks are created and removed.

Regression test for kernel commit:
9dd1964ac59d ("smb/client: fix incorrect nlink returned by fstat()")

---
 tests/generic/798     | 60 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/798.out |  2 ++
 2 files changed, 62 insertions(+)
 create mode 100755 tests/generic/798
 create mode 100644 tests/generic/798.out
tests/generic/798 new file +60 -0
new file mode 100755
index 00000000..e44bb10a
@@ -0,0 +1,60 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved.
+# Author(s): ChenXiaoSong <[email protected]>
+#
+# FS QA Test 798
+#
+# Check that fstat(2) returns the correct hard link count for a regular file.
+#
+#  from
+#  tests/generic/002
+#  Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
+#
+. ./common/preamble
+_begin_fstest metadata auto quick hardlink
+
+_cleanup()
+{
+	cd /
+	rm -rf "$tmp".* "$testdir"
+}
+
+_require_test
+_require_hardlinks
+
+_fixed_by_fs_commit cifs 9dd1964ac59d \
+	"smb/client: fix incorrect nlink returned by fstat()"
+
+_get_nlink()
+{
+	$XFS_IO_PROG -c 'stat -r' "$1" |
+		sed -n 's/^stat\.nlink = //p'
+}
+
+status=0
+testdir=$TEST_DIR/$seq
+rm -rf "$testdir"
+mkdir "$testdir"
+
+touch "$testdir/tmp.1"
+for ((l = 2; l <= 20; l++)); do
+	ln "$testdir/tmp.1" "$testdir/tmp.$l"
+	nlink=$(_get_nlink "$testdir/tmp.1")
+	if [ "$nlink" != "$l" ]; then
+		echo "Expected nlink $l after creating link $l, got ${nlink:-nothing}"
+		status=1
+	fi
+done
+
+for ((l = 20; l >= 1; l--)); do
+	nlink=$(_get_nlink "$testdir/tmp.1")
+	if [ "$nlink" != "$l" ]; then
+		echo "Expected nlink $l before removing link $l, got ${nlink:-nothing}"
+		status=1
+	fi
+	rm -f "$testdir/tmp.$l"
+done
+
+echo "Silence is golden"
+exit $status
tests/generic/798.out new file +2 -0
new file mode 100644
index 00000000..216d6e93
@@ -0,0 +1,2 @@
+QA output created by 798
+Silence is golden
Signature · 1 line
2.43.0
#4 lore raw
Quoted 48 lines · ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Add a test to verify that fstat(2) returns the expected st_nlink value as hardlinks are created and removed.

Regression test for kernel commit:
9dd1964ac59d ("smb/client: fix incorrect nlink returned by fstat()")

---
 tests/generic/798     | 60 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/798.out |  2 ++
 2 files changed, 62 insertions(+)
 create mode 100755 tests/generic/798
 create mode 100644 tests/generic/798.out
tests/generic/798 new file +25 -0
new file mode 100755
index 00000000..e44bb10a
@@ -0,0 +1,60 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved.
+# Author(s): ChenXiaoSong <[email protected]>
+#
+# FS QA Test 798
+#
+# Check that fstat(2) returns the correct hard link count for a regular file.
+#
+#  from
+#  tests/generic/002
+#  Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
+#
+. ./common/preamble
+_begin_fstest metadata auto quick hardlink
+
+_cleanup()
+{
+	cd /
+	rm -rf "$tmp".* "$testdir"
+}
+
+_require_test
+_require_hardlinks
+

tests/generic/798 line 35

Quoted 10 lines
tests/generic/798 new file +10 -0
+_fixed_by_fs_commit cifs 9dd1964ac59d \
+	"smb/client: fix incorrect nlink returned by fstat()"
+
+_get_nlink()
+{
+	$XFS_IO_PROG -c 'stat -r' "$1" |
+		sed -n 's/^stat\.nlink = //p'
+}
+
+status=0

We set "status=1" in _begin_fstest. You can remove this line, and then ...

tests/generic/798 line 46

Quoted 11 lines
tests/generic/798 new file +11 -0
+testdir=$TEST_DIR/$seq
+rm -rf "$testdir"
+mkdir "$testdir"
+
+touch "$testdir/tmp.1"
+for ((l = 2; l <= 20; l++)); do
+	ln "$testdir/tmp.1" "$testdir/tmp.$l"
+	nlink=$(_get_nlink "$testdir/tmp.1")
+	if [ "$nlink" != "$l" ]; then
+		echo "Expected nlink $l after creating link $l, got ${nlink:-nothing}"
+		status=1

... The current "echo" output is already sufficient to break the golden image and cause the test to fail, so set "status=1" isn't necessary. Alternatively, if you feel there's no need to continue the rest of the loop once a failure is detected, you can use "_fail" to replace "echo" instead.

tests/generic/798 line 54

Quoted 8 lines
tests/generic/798 new file +8 -0
+	fi
+done
+
+for ((l = 20; l >= 1; l--)); do
+	nlink=$(_get_nlink "$testdir/tmp.1")
+	if [ "$nlink" != "$l" ]; then
+		echo "Expected nlink $l before removing link $l, got ${nlink:-nothing}"
+		status=1

Same here

tests/generic/798 line 60

Quoted 6 lines
tests/generic/798 new file +6 -0
+	fi
+	rm -f "$testdir/tmp.$l"
+done
+
+echo "Silence is golden"
+exit $status

then you can write "_exit 0" at here.

Thanks,
Zorro

Quoted 11 lines
tests/generic/798.out new file +2 -0
new file mode 100644
index 00000000..216d6e93
@@ -0,0 +1,2 @@
+QA output created by 798
+Silence is golden
Signature · 1 line
2.43.0