From: ChenXiaoSong <chenxiaosong@chenxiaosong.com>
To: smfrench@gmail.com, linkinjeon@kernel.org, pc@manguebit.org,
ronniesahlberg@gmail.com, sprasad@microsoft.com, tom@talpey.com,
bharathsm@microsoft.com, senozhatsky@chromium.org,
dhowells@redhat.com, metze@samba.org, zlang@kernel.org,
lukas@herbolt.com
Cc: linux-cifs@vger.kernel.org, fstests@vger.kernel.org,
ChenXiaoSong <chenxiaosong@kylinos.cn>
Subject: [PATCH v2 xfstests resend 3/3] generic/795: check nlink returned by fstat()
Date: Mon, 20 Jul 2026 16:42:07 +0000 [thread overview]
Message-ID: <20260720164207.732522-4-chenxiaosong@chenxiaosong.com> (raw)
In-Reply-To: <20260720164207.732522-1-chenxiaosong@chenxiaosong.com>
From: ChenXiaoSong <chenxiaosong@kylinos.cn>
Add a test to verify that fstat(2) returns the expected
st_nlink value as hardlinks are created and removed.
Suggested-by: Zorro Lang <zlang@kernel.org>
Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
src/Makefile | 4 +--
src/fstat.c | 42 ++++++++++++++++++++++++++++++
tests/generic/795 | 60 +++++++++++++++++++++++++++++++++++++++++++
tests/generic/795.out | 2 ++
4 files changed, 106 insertions(+), 2 deletions(-)
create mode 100644 src/fstat.c
create mode 100755 tests/generic/795
create mode 100644 tests/generic/795.out
diff --git a/src/Makefile b/src/Makefile
index 7fab5d05..fd2622e8 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -6,7 +6,7 @@
TOPDIR = ..
include $(TOPDIR)/include/builddefs
-TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
+TARGETS = dirstress fill fill2 getpagesize holes lstat64 fstat \
nametest permname randholes runas truncfile usemem \
mmapcat append_reader append_writer dirperf metaperf \
devzero feature alloc fault fstest t_access_root \
@@ -130,7 +130,7 @@ fssum: fssum.c md5.c
@echo " [CC] $@"
$(Q)$(LTLINK) fssum.c md5.c -o $@ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
-STAT_TARGETS = lstat64
+STAT_TARGETS = lstat64 fstat
$(STAT_TARGETS): %: %.c stat_common.c stat_common.h $(LIBTEST)
@echo " [CC] $@"
diff --git a/src/fstat.c b/src/fstat.c
new file mode 100644
index 00000000..fb878f77
--- /dev/null
+++ b/src/fstat.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved.
+ * Author(s): ChenXiaoSong <chenxiaosong@kylinos.cn>
+ *
+ * from
+ * src/lstat64.c
+ * Copyright (c) 2000-2002 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "stat_common.h"
+
+static int
+get_fstat(const char *path, struct stat64 *sbuf)
+{
+ int fd;
+ int ret;
+ int saved_errno;
+
+ fd = open(path, O_RDONLY | O_NONBLOCK);
+ if (fd < 0)
+ return -1;
+
+ ret = fstat64(fd, sbuf);
+ saved_errno = errno;
+ close(fd);
+ errno = saved_errno;
+
+ return ret;
+}
+
+int
+main(int argc, char **argv)
+{
+ return handle_stat(argc, argv, "fstat", get_fstat);
+}
diff --git a/tests/generic/795 b/tests/generic/795
new file mode 100755
index 00000000..0fc9a9bc
--- /dev/null
+++ b/tests/generic/795
@@ -0,0 +1,60 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved.
+# Author(s): ChenXiaoSong <chenxiaosong@kylinos.cn>
+#
+# FS QA Test 795
+#
+# Check that fstat(2) returns the correct hard link count for a regular file.
+# Regression test for kernel commit:
+# 9dd1964ac59d ("smb/client: fix incorrect nlink returned by fstat()")
+#
+# from
+# tests/generic/002
+# Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
+#
+. ./common/preamble
+_begin_fstest metadata auto quick hardlink
+
+# Override the default cleanup function.
+_cleanup()
+{
+ cd /
+ [ -d "$testdir" ] && rm -rf $testdir
+}
+
+status=0 # success is the default!
+
+_require_test
+_require_hardlinks
+_require_test_program fstat
+
+_fixed_by_fs_commit cifs 9dd1964ac59d \
+ "smb/client: fix incorrect nlink returned by fstat()"
+
+testdir=$TEST_DIR/$$
+mkdir -p $testdir
+
+touch $testdir/tmp.1
+for ((l = 2; l <= 20; l++)); do
+ ln $testdir/tmp.1 $testdir/tmp.$l
+ x=`$here/src/fstat $testdir/tmp.1 | sed -n -e '/ Links: /s/.*Links: *//p'`
+ if [ "$l" -ne $x ]; then
+ echo "Arrgh, created link #$l and fstat looks like ..."
+ $here/src/fstat $testdir/tmp.1
+ status=1
+ fi
+done
+
+for ((l = 20; l >= 1; l--)); do
+ x=`$here/src/fstat $testdir/tmp.1 | sed -n -e '/ Links: /s/.*Links: *//p'`
+ if [ "$l" -ne $x ]; then
+ echo "Arrgh, about to remove link #$l and fstat looks like ..."
+ $here/src/fstat $testdir/tmp.1
+ status=1
+ fi
+ rm -f $testdir/tmp.$l
+done
+
+echo "Silence is golden"
+exit $status
diff --git a/tests/generic/795.out b/tests/generic/795.out
new file mode 100644
index 00000000..cb357003
--- /dev/null
+++ b/tests/generic/795.out
@@ -0,0 +1,2 @@
+QA output created by 795
+Silence is golden
--
2.43.0
next prev parent reply other threads:[~2026-07-20 16:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-20 16:42 [PATCH v2 xfstests resend 0/3] add generic/795 ChenXiaoSong
2026-07-20 16:42 ` [PATCH v2 xfstests resend 1/3] src: factor out common stat code ChenXiaoSong
2026-07-20 16:42 ` [PATCH v2 xfstests resend 2/3] generic/002: clean up the test ChenXiaoSong
2026-07-20 16:42 ` ChenXiaoSong [this message]
2026-07-28 3:28 ` [PATCH v2 xfstests resend 0/3] add generic/795 Christoph Hellwig
2026-07-28 3:35 ` ChenXiaoSong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260720164207.732522-4-chenxiaosong@chenxiaosong.com \
--to=chenxiaosong@chenxiaosong.com \
--cc=bharathsm@microsoft.com \
--cc=chenxiaosong@kylinos.cn \
--cc=dhowells@redhat.com \
--cc=fstests@vger.kernel.org \
--cc=linkinjeon@kernel.org \
--cc=linux-cifs@vger.kernel.org \
--cc=lukas@herbolt.com \
--cc=metze@samba.org \
--cc=pc@manguebit.org \
--cc=ronniesahlberg@gmail.com \
--cc=senozhatsky@chromium.org \
--cc=smfrench@gmail.com \
--cc=sprasad@microsoft.com \
--cc=tom@talpey.com \
--cc=zlang@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.