Skip to contentSkip to conversation

add generic/795

PATCH Superseded ChenXiaoSong 2 participants 8 messages Jul 20 – Jul 22

Subsystems (2)
lore
Share & tools
lore

ChenXiaoSong

author reviewer
#0 lore raw

v2 cover add generic/795

From: ChenXiaoSong <[email protected]>

v1->v2:
- Add patch#01 patch#02.
- Patch#03: some cleanups.

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

ChenXiaoSong (3):
src: factor out common stat code
generic/002: clean up the test
generic/795: check nlink returned by fstat()

 src/Makefile          |  13 ++-
 src/fstat.c           |  42 ++++++++++
 src/lstat64.c         | 166 +------------------------------------
 src/stat_common.c     | 185 ++++++++++++++++++++++++++++++++++++++++++
 src/stat_common.h     |  16 ++++
 tests/generic/002     |  16 ++--
 tests/generic/002.out |   2 +-
 tests/generic/795     |  60 ++++++++++++++
 tests/generic/795.out |   2 +
 9 files changed, 324 insertions(+), 178 deletions(-)
 create mode 100644 src/fstat.c
 create mode 100644 src/stat_common.c
 create mode 100644 src/stat_common.h
 create mode 100755 tests/generic/795
 create mode 100644 tests/generic/795.out
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH v2 1/3 src: factor out common stat code

From: ChenXiaoSong <[email protected]>

To see only the differences in the code copied from lstat64.c to stat_common.c, run:

  git show --find-copies --diff-filter=C <commit-id>

Move command-line parsing and stat output formatting from lstat64 into stat_common so the implementation can be shared by other stat helpers.

No functional change.

---
 src/Makefile      |  11 ++-
 src/lstat64.c     | 166 +----------------------------------------
 src/stat_common.c | 185 ++++++++++++++++++++++++++++++++++++++++++++++
 src/stat_common.h |  16 ++++
 4 files changed, 212 insertions(+), 166 deletions(-)
 create mode 100644 src/stat_common.c
 create mode 100644 src/stat_common.h
src/Makefile +9 -2
index 31ac43b2..7fab5d05 100644
@@ -115,7 +115,8 @@ ifeq ($(NEED_INTERNAL_XFS_IOC_EXCHANGE_RANGE),yes)
 LCFLAGS += -DNEED_INTERNAL_XFS_IOC_EXCHANGE_RANGE
 endif
 
-CFILES = $(TARGETS:=.c)
+CFILES = $(TARGETS:=.c) stat_common.c
+HFILES = stat_common.h
 LDIRT = $(TARGETS) fssum
 
 
@@ -129,7 +130,13 @@ fssum: fssum.c md5.c
 	@echo "    [CC]    $@"
 	$(Q)$(LTLINK) fssum.c md5.c -o $@ $(CFLAGS) $(LDFLAGS) $(LDLIBS)
 
-$(TARGETS): $(LIBTEST)
+STAT_TARGETS = lstat64
+
+$(STAT_TARGETS): %: %.c stat_common.c stat_common.h $(LIBTEST)
+	@echo "    [CC]    $@"
+	$(Q)$(LTLINK) [email protected] stat_common.c -o $@ $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(LIBTEST)
+
+$(filter-out $(STAT_TARGETS),$(TARGETS)): $(LIBTEST)
 	@echo "    [CC]    $@"
 	$(Q)$(LTLINK) [email protected] -o $@ $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(LIBTEST)
 
src/lstat64.c +2 -164

174 lines not on this page · raw message

src/stat_common.c new file +185 -0

185 lines not on this page · raw message

src/stat_common.h new file +16 -0
new file mode 100644
index 00000000..290643d7
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2000-2002 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ */
+
+#ifndef STAT_COMMON_H
+#define STAT_COMMON_H
+
+#include <sys/stat.h>
+
+typedef int (*get_stat_fn)(const char *path, struct stat64 *sbuf);
+
+int handle_stat(int argc, char **argv, const char *program, get_stat_fn get_stat);
+
+#endif /* STAT_COMMON_H */
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

PATCH v2 2/3 generic/002: clean up the test

In reply to ChenXiaoSong

From: ChenXiaoSong <[email protected]>

Add the hardlink group and use shorter loops. Use _exit() and change the success text to "Silence is golden".

---
 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 v2 3/3 generic/795: 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.

---
 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
src/Makefile +2 -2
index 7fab5d05..fd2622e8 100644
@@ -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]    $@"
src/fstat.c new file +42 -0

42 lines not on this page · raw message

tests/generic/795 new file +60 -0

60 lines not on this page · raw message

tests/generic/795.out new file +2 -0
new file mode 100644
index 00000000..cb357003
@@ -0,0 +1,2 @@
+QA output created by 795
+Silence is golden
Signature · 1 line
2.43.0
#4 lore raw
In reply to ChenXiaoSong
Quoted 13 lines · ChenXiaoSong

From: ChenXiaoSong <[email protected]>

v1->v2:
- Add patch#01 patch#02.
- Patch#03: some cleanups.

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

ChenXiaoSong (3):
src: factor out common stat code
generic/002: clean up the test
generic/795: check nlink returned by fstat()

Quoted 7 lines
 src/Makefile          |  13 ++-
 src/fstat.c           |  42 ++++++++++
 src/lstat64.c         | 166 +------------------------------------
 src/stat_common.c     | 185 ++++++++++++++++++++++++++++++++++++++++++
 src/stat_common.h     |  16 ++++
 tests/generic/002     |  16 ++--
 tests/generic/002.out |   2 +-

Hi,

I might not have explained clearly during the review. I wasn't asking you to rewrite g/002 and lstat64.c. I was suggesting checking whether src/fstat.c in your previous patch could be replaced by the stat command in xfs_io [1]. Check man xfs_io for the "stat" command to see if it can fulfill your test requirement.

Due to I noticed you need information like st_nlink from struct stat, so xfs_io -c "stat -r" ... should meet your requirements. Please give it a try, if it works, it will make the whole patch much cleaner.

P.S. For a new test case, please don't use a specific case number (like generic/795) in the subject or commit log, as it might get assigned a different number when merged. You can just use something like:
"[PATCH] generic: new test to ..."

Thanks,
Zorro

[1]
https://lore.kernel.org/fstests/[email protected]/T/#mff79f3e74e8cefbdda71e6a22f6a20f2dd985661
"I'm wondering if a specific src/fstat.c is needed. Can we replace the whole src/fstat.c with xfs_io -c stat ? Can you give it a try?"

Quoted 12 lines
 tests/generic/795     |  60 ++++++++++++++
 tests/generic/795.out |   2 +
 9 files changed, 324 insertions(+), 178 deletions(-)
 create mode 100644 src/fstat.c
 create mode 100644 src/stat_common.c
 create mode 100644 src/stat_common.h
 create mode 100755 tests/generic/795
 create mode 100644 tests/generic/795.out
Signature · 1 line
2.43.0
author reviewer
#5 lore raw

Hi Zorro,

The stat command uses the statx() syscall rather than fstat(), so we have to use a C program to call fstat().

Quoted 14 lines · Zorro Lang

I might not have explained clearly during the review. I wasn't asking you to rewrite g/002 and lstat64.c. I was suggesting checking whether src/fstat.c in your previous patch could be replaced by the stat command in xfs_io [1]. Check man xfs_io for the "stat" command to see if it can fulfill your test requirement.

Due to I noticed you need information like st_nlink from struct stat, so xfs_io -c "stat -r" ... should meet your requirements. Please give it a try, if it works, it will make the whole patch much cleaner.

P.S. For a new test case, please don't use a specific case number (like generic/795) in the subject or commit log, as it might get assigned a different number when merged. You can just use something like:
"[PATCH] generic: new test to ..."

Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en
#6 lore raw
Quoted 4 lines · ChenXiaoSong

Hi Zorro,

The stat command uses the statx() syscall rather than fstat(), so we have to use a C program to call fstat().

I'm not sure how you got at that conclusion.

Quoted 10 lines · Zorro Lang
Quoted 8 lines

I might not have explained clearly during the review. I wasn't asking you to rewrite g/002 and lstat64.c. I was suggesting checking whether src/fstat.c in your previous patch could be replaced by the stat command in xfs_io [1]. Check man xfs_io for the "stat" command to see if it can fulfill your test requirement.

Due to I noticed you need information like st_nlink from struct stat, so xfs_io -c "stat -r" ... should meet your requirements. Please

     ^^^^^^^^^^^^^^^^^^

Please note that I have been talking about xfs_io stat subcommand all along, rather than the coreutils' stat command. I suspect there might be a misunderstanding here.

xfstests inherently depends on xfsprogs, particularly the xfs_io command. I just checked this for you, the stat subcommand in xfs_io does indeed use the fstat system call [1][2], not statx, and its -r option will print out fields like st_nlink.

Please double-check this on your end, make sure it works for you.

Thanks,
Zorro

[1]
xfsprogs/io/stat.c:stat_f() :
int
stat_f(
int argc,
char **argv)

  {
          struct stat     st;
          int             c, verbose = 0, raw = 0;

          while ((c = getopt(argc, argv, "rv")) != EOF) {
                  switch (c) {
                  case 'r':
                          raw = 1;
                          break;
                  case 'v':
                          verbose = 1;
                          break;
                  default:
                          exitcode = 1;
                          return command_usage(&stat_cmd);
                  }
          }

          if (fstat(file->fd, &st) < 0) {
                  perror("fstat");
                  exitcode = 1;
                  return 0;
          }

          if (raw)
                  return dump_raw_stat(&st);

...

[2]

$ strace xfs_io -c "stat -r" README 2>&1|grep -E "statx|fstat"
fstat(3, {st_mode=S_IFREG|0644, st_size=106979, ...}) = 0
fstat(3, {st_mode=S_IFREG|0755, st_size=36680, ...}) = 0
fstat(3, {st_mode=S_IFREG|0755, st_size=236720, ...}) = 0
fstat(3, {st_mode=S_IFREG|0755, st_size=2482096, ...}) = 0
fstat(3, {st_mode=S_IFREG|0755, st_size=187232, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=233380912, ...}) = 0
fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
newfstatat(AT_FDCWD, "/", {st_mode=S_IFDIR|0555, st_size=154, ...}, 0) = 0
newfstatat(AT_FDCWD, "/", {st_mode=S_IFDIR|0555, st_size=154, ...}, 0) = 0
newfstatat(AT_FDCWD, "/home", {st_mode=S_IFDIR|0755, st_size=10, ...}, 0) = 0
newfstatat(AT_FDCWD, "/home", {st_mode=S_IFDIR|0755, st_size=10, ...}, 0) = 0
newfstatat(AT_FDCWD, "/boot", {st_mode=S_IFDIR|0555, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "/boot", {st_mode=S_IFDIR|0555, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "/boot/efi", {st_mode=S_IFDIR|0700, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "/boot/efi", {st_mode=S_IFDIR|0700, st_size=4096, ...}, 0) = 0
newfstatat(AT_FDCWD, "README", {st_mode=S_IFREG|0644, st_size=27145, ...}, 0) = 0
fstatfs(3, {f_type=BTRFS_SUPER_MAGIC, f_bsize=4096, f_blocks=249372928, f_bfree=196292374, f_bavail=195356006, f_files=0, f_ffree=0, f_fsid={val=[0xfb10ffbe, 0x278a3f4f]}, f_namelen=255, f_frsize=4096, f_flags=ST_VALID|ST_RELATIME}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=27145, ...}) = 0
newfstatat(AT_FDCWD, "README", {st_mode=S_IFREG|0644, st_size=27145, ...}, 0) = 0
fstatfs(3, {f_type=BTRFS_SUPER_MAGIC, f_bsize=4096, f_blocks=249372928, f_bfree=196292374, f_bavail=195356006, f_files=0, f_ffree=0, f_fsid={val=[0xfb10ffbe, 0x278a3f4f]}, f_namelen=255, f_frsize=4096, f_flags=ST_VALID|ST_RELATIME}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=27145, ...}) = 0
fstat(4, {st_mode=S_IFREG|0644, st_size=2998, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=27145, ...}) = 0
fstat(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
Quoted 12 lines
Quoted 6 lines

give it a try, if it works, it will make the whole patch much cleaner.

P.S. For a new test case, please don't use a specific case number (like generic/795) in the subject or commit log, as it might get assigned a different number when merged. You can just use something like:
"[PATCH] generic: new test to ..."

Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en
author reviewer
#7 lore raw

Yes, I checked the xfs_io source code, and it does indeed call the fstat() system call. Thanks for your suggestion! I will send the next version soon.

https://git.kernel.org/pub/scm/fs/xfs/xfsprogs-dev.git/tree/io/stat.c#n552
https://git.kernel.org/pub/scm/fs/xfs/xfsprogs-dev.git/tree/io/stat.c#n166

Quoted 9 lines · Zorro Lang

Please note that I have been talking about xfs_io stat subcommand all along, rather than the coreutils' stat command. I suspect there might be a misunderstanding here.

xfstests inherently depends on xfsprogs, particularly the xfs_io command. I just checked this for you, the stat subcommand in xfs_io does indeed use the fstat system call [1][2], not statx, and its -r option will print out fields like st_nlink.

Please double-check this on your end, make sure it works for you.

Signature · 3 lines
ChenXiaoSong <[email protected]>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en