From: Amir Goldstein <amir73il@gmail.com>
To: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>, John Hubbard <jhubbard@nvidia.com>,
Miklos Szeredi <miklos@szeredi.hu>,
linux-fsdevel@vger.kernel.org
Subject: [PATCH v2 6/8] selftests/filesystems: create get_unique_mnt_id() helper
Date: Fri, 9 May 2025 15:32:38 +0200 [thread overview]
Message-ID: <20250509133240.529330-7-amir73il@gmail.com> (raw)
In-Reply-To: <20250509133240.529330-1-amir73il@gmail.com>
Add helper to utils.c and use it in mount-notify and statmount tests.
Linking with utils.c drags in a dependecy with libcap, so add it to the
Makefile of the tests.
Reviewed-by: John Hubbard <jhubbard@nvidia.com>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
.../filesystems/mount-notify/Makefile | 3 ++
.../mount-notify/mount-notify_test.c | 13 ++-------
.../selftests/filesystems/statmount/Makefile | 3 ++
.../filesystems/statmount/statmount_test_ns.c | 28 +++----------------
tools/testing/selftests/filesystems/utils.c | 22 +++++++++++++++
tools/testing/selftests/filesystems/utils.h | 2 ++
6 files changed, 36 insertions(+), 35 deletions(-)
diff --git a/tools/testing/selftests/filesystems/mount-notify/Makefile b/tools/testing/selftests/filesystems/mount-notify/Makefile
index 41ebfe558a0a..55a2e5399e8a 100644
--- a/tools/testing/selftests/filesystems/mount-notify/Makefile
+++ b/tools/testing/selftests/filesystems/mount-notify/Makefile
@@ -1,7 +1,10 @@
# SPDX-License-Identifier: GPL-2.0-or-later
CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
+LDLIBS += -lcap
TEST_GEN_PROGS := mount-notify_test
include ../../lib.mk
+
+$(OUTPUT)/mount-notify_test: ../utils.c
diff --git a/tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c b/tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c
index 4f0f325379b5..63ce708d93ed 100644
--- a/tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c
+++ b/tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c
@@ -13,6 +13,7 @@
#include "../../kselftest_harness.h"
#include "../statmount/statmount.h"
+#include "../utils.h"
// Needed for linux/fanotify.h
#ifndef __kernel_fsid_t
@@ -23,16 +24,6 @@ typedef struct {
#include <sys/fanotify.h>
-static uint64_t get_mnt_id(struct __test_metadata *const _metadata,
- const char *path)
-{
- struct statx sx;
-
- ASSERT_EQ(statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx), 0);
- ASSERT_TRUE(!!(sx.stx_mask & STATX_MNT_ID_UNIQUE));
- return sx.stx_mnt_id;
-}
-
static const char root_mntpoint_templ[] = "/tmp/mount-notify_test_root.XXXXXX";
static const int mark_cmds[] = {
@@ -81,7 +72,7 @@ FIXTURE_SETUP(fanotify)
ASSERT_EQ(mkdir("b", 0700), 0);
- self->root_id = get_mnt_id(_metadata, "/");
+ self->root_id = get_unique_mnt_id("/");
ASSERT_NE(self->root_id, 0);
for (i = 0; i < NUM_FAN_FDS; i++) {
diff --git a/tools/testing/selftests/filesystems/statmount/Makefile b/tools/testing/selftests/filesystems/statmount/Makefile
index 19adebfc2620..8e354fe99b44 100644
--- a/tools/testing/selftests/filesystems/statmount/Makefile
+++ b/tools/testing/selftests/filesystems/statmount/Makefile
@@ -1,7 +1,10 @@
# SPDX-License-Identifier: GPL-2.0-or-later
CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
+LDLIBS += -lcap
TEST_GEN_PROGS := statmount_test statmount_test_ns listmount_test
include ../../lib.mk
+
+$(OUTPUT)/statmount_test_ns: ../utils.c
diff --git a/tools/testing/selftests/filesystems/statmount/statmount_test_ns.c b/tools/testing/selftests/filesystems/statmount/statmount_test_ns.c
index 70cb0c8b21cf..375a52101d08 100644
--- a/tools/testing/selftests/filesystems/statmount/statmount_test_ns.c
+++ b/tools/testing/selftests/filesystems/statmount/statmount_test_ns.c
@@ -14,6 +14,7 @@
#include <linux/stat.h>
#include "statmount.h"
+#include "../utils.h"
#include "../../kselftest.h"
#define NSID_PASS 0
@@ -78,27 +79,6 @@ static int get_mnt_ns_id(const char *mnt_ns, uint64_t *mnt_ns_id)
return NSID_PASS;
}
-static int get_mnt_id(const char *path, uint64_t *mnt_id)
-{
- struct statx sx;
- int ret;
-
- ret = statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx);
- if (ret == -1) {
- ksft_print_msg("retrieving unique mount ID for %s: %s\n", path,
- strerror(errno));
- return NSID_ERROR;
- }
-
- if (!(sx.stx_mask & STATX_MNT_ID_UNIQUE)) {
- ksft_print_msg("no unique mount ID available for %s\n", path);
- return NSID_ERROR;
- }
-
- *mnt_id = sx.stx_mnt_id;
- return NSID_PASS;
-}
-
static int write_file(const char *path, const char *val)
{
int fd = open(path, O_WRONLY);
@@ -174,9 +154,9 @@ static int _test_statmount_mnt_ns_id(void)
if (ret != NSID_PASS)
return ret;
- ret = get_mnt_id("/", &root_id);
- if (ret != NSID_PASS)
- return ret;
+ root_id = get_unique_mnt_id("/");
+ if (!root_id)
+ return NSID_ERROR;
ret = statmount(root_id, 0, STATMOUNT_MNT_NS_ID, &sm, sizeof(sm), 0);
if (ret == -1) {
diff --git a/tools/testing/selftests/filesystems/utils.c b/tools/testing/selftests/filesystems/utils.c
index e553c89c5b19..5a114af822af 100644
--- a/tools/testing/selftests/filesystems/utils.c
+++ b/tools/testing/selftests/filesystems/utils.c
@@ -19,6 +19,8 @@
#include <sys/wait.h>
#include <sys/xattr.h>
+#include "../kselftest.h"
+#include "wrappers.h"
#include "utils.h"
#define MAX_USERNS_LEVEL 32
@@ -499,3 +501,23 @@ int cap_down(cap_value_t down)
cap_free(caps);
return fret;
}
+
+uint64_t get_unique_mnt_id(const char *path)
+{
+ struct statx sx;
+ int ret;
+
+ ret = statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx);
+ if (ret == -1) {
+ ksft_print_msg("retrieving unique mount ID for %s: %s\n", path,
+ strerror(errno));
+ return 0;
+ }
+
+ if (!(sx.stx_mask & STATX_MNT_ID_UNIQUE)) {
+ ksft_print_msg("no unique mount ID available for %s\n", path);
+ return 0;
+ }
+
+ return sx.stx_mnt_id;
+}
diff --git a/tools/testing/selftests/filesystems/utils.h b/tools/testing/selftests/filesystems/utils.h
index 7f1df2a3e94c..d9cf145b321a 100644
--- a/tools/testing/selftests/filesystems/utils.h
+++ b/tools/testing/selftests/filesystems/utils.h
@@ -42,4 +42,6 @@ static inline bool switch_userns(int fd, uid_t uid, gid_t gid, bool drop_caps)
return true;
}
+extern uint64_t get_unique_mnt_id(const char *path);
+
#endif /* __IDMAP_UTILS_H */
--
2.34.1
next prev parent reply other threads:[~2025-05-09 13:32 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-09 13:32 [PATCH v2 0/8] filesystems selftests cleanups and fanotify test Amir Goldstein
2025-05-09 13:32 ` [PATCH v2 1/8] selftests/filesystems: move wrapper.h out of overlayfs subdir Amir Goldstein
2025-05-12 9:28 ` Christian Brauner
2025-05-09 13:32 ` [PATCH v2 2/8] selftests/fs/statmount: build with tools include dir Amir Goldstein
2025-05-12 9:29 ` Christian Brauner
2025-05-09 13:32 ` [PATCH v2 3/8] selftests/pidfd: move syscall definitions into wrappers.h Amir Goldstein
2025-05-12 9:29 ` Christian Brauner
2025-05-09 13:32 ` [PATCH v2 4/8] selftests/mount_settattr: remove duplicate syscall definitions Amir Goldstein
2025-05-12 9:30 ` Christian Brauner
2025-05-09 13:32 ` [PATCH v2 5/8] selftests/fs/mount-notify: build with tools include dir Amir Goldstein
2025-05-12 9:31 ` Christian Brauner
2025-05-09 13:32 ` Amir Goldstein [this message]
2025-05-12 9:31 ` [PATCH v2 6/8] selftests/filesystems: create get_unique_mnt_id() helper Christian Brauner
2025-05-09 13:32 ` [PATCH v2 7/8] selftests/filesystems: create setup_userns() helper Amir Goldstein
2025-05-12 9:37 ` Christian Brauner
2025-05-09 13:32 ` [PATCH v2 8/8] selftests/fs/mount-notify: add a test variant running inside userns Amir Goldstein
2025-05-12 9:33 ` Christian Brauner
2025-05-12 9:35 ` [PATCH v2 0/8] filesystems selftests cleanups and fanotify test Christian Brauner
2025-05-12 9:40 ` Christian Brauner
2025-05-14 8:25 ` Amir Goldstein
2025-05-15 9:13 ` Christian Brauner
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=20250509133240.529330-7-amir73il@gmail.com \
--to=amir73il@gmail.com \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=jhubbard@nvidia.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=miklos@szeredi.hu \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).