Skip to content

Commit e897b9b

Browse files
amir73ilbrauner
authored andcommitted
selftests/filesystems: create get_unique_mnt_id() helper
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 <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> Link: https://lore.kernel.org/[email protected] Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent c6d9775 commit e897b9b

File tree

6 files changed

+36
-35
lines changed

6 files changed

+36
-35
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# SPDX-License-Identifier: GPL-2.0-or-later
22

33
CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
4+
LDLIBS += -lcap
45

56
TEST_GEN_PROGS := mount-notify_test
67

78
include ../../lib.mk
9+
10+
$(OUTPUT)/mount-notify_test: ../utils.c

tools/testing/selftests/filesystems/mount-notify/mount-notify_test.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "../../kselftest_harness.h"
1515
#include "../statmount/statmount.h"
16+
#include "../utils.h"
1617

1718
// Needed for linux/fanotify.h
1819
#ifndef __kernel_fsid_t
@@ -23,16 +24,6 @@ typedef struct {
2324

2425
#include <sys/fanotify.h>
2526

26-
static uint64_t get_mnt_id(struct __test_metadata *const _metadata,
27-
const char *path)
28-
{
29-
struct statx sx;
30-
31-
ASSERT_EQ(statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx), 0);
32-
ASSERT_TRUE(!!(sx.stx_mask & STATX_MNT_ID_UNIQUE));
33-
return sx.stx_mnt_id;
34-
}
35-
3627
static const char root_mntpoint_templ[] = "/tmp/mount-notify_test_root.XXXXXX";
3728

3829
static const int mark_cmds[] = {
@@ -81,7 +72,7 @@ FIXTURE_SETUP(fanotify)
8172

8273
ASSERT_EQ(mkdir("b", 0700), 0);
8374

84-
self->root_id = get_mnt_id(_metadata, "/");
75+
self->root_id = get_unique_mnt_id("/");
8576
ASSERT_NE(self->root_id, 0);
8677

8778
for (i = 0; i < NUM_FAN_FDS; i++) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# SPDX-License-Identifier: GPL-2.0-or-later
22

33
CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES)
4+
LDLIBS += -lcap
45

56
TEST_GEN_PROGS := statmount_test statmount_test_ns listmount_test
67

78
include ../../lib.mk
9+
10+
$(OUTPUT)/statmount_test_ns: ../utils.c

tools/testing/selftests/filesystems/statmount/statmount_test_ns.c

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/stat.h>
1515

1616
#include "statmount.h"
17+
#include "../utils.h"
1718
#include "../../kselftest.h"
1819

1920
#define NSID_PASS 0
@@ -78,27 +79,6 @@ static int get_mnt_ns_id(const char *mnt_ns, uint64_t *mnt_ns_id)
7879
return NSID_PASS;
7980
}
8081

81-
static int get_mnt_id(const char *path, uint64_t *mnt_id)
82-
{
83-
struct statx sx;
84-
int ret;
85-
86-
ret = statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx);
87-
if (ret == -1) {
88-
ksft_print_msg("retrieving unique mount ID for %s: %s\n", path,
89-
strerror(errno));
90-
return NSID_ERROR;
91-
}
92-
93-
if (!(sx.stx_mask & STATX_MNT_ID_UNIQUE)) {
94-
ksft_print_msg("no unique mount ID available for %s\n", path);
95-
return NSID_ERROR;
96-
}
97-
98-
*mnt_id = sx.stx_mnt_id;
99-
return NSID_PASS;
100-
}
101-
10282
static int write_file(const char *path, const char *val)
10383
{
10484
int fd = open(path, O_WRONLY);
@@ -174,9 +154,9 @@ static int _test_statmount_mnt_ns_id(void)
174154
if (ret != NSID_PASS)
175155
return ret;
176156

177-
ret = get_mnt_id("/", &root_id);
178-
if (ret != NSID_PASS)
179-
return ret;
157+
root_id = get_unique_mnt_id("/");
158+
if (!root_id)
159+
return NSID_ERROR;
180160

181161
ret = statmount(root_id, 0, STATMOUNT_MNT_NS_ID, &sm, sizeof(sm), 0);
182162
if (ret == -1) {

tools/testing/selftests/filesystems/utils.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <sys/wait.h>
2020
#include <sys/xattr.h>
2121

22+
#include "../kselftest.h"
23+
#include "wrappers.h"
2224
#include "utils.h"
2325

2426
#define MAX_USERNS_LEVEL 32
@@ -499,3 +501,23 @@ int cap_down(cap_value_t down)
499501
cap_free(caps);
500502
return fret;
501503
}
504+
505+
uint64_t get_unique_mnt_id(const char *path)
506+
{
507+
struct statx sx;
508+
int ret;
509+
510+
ret = statx(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx);
511+
if (ret == -1) {
512+
ksft_print_msg("retrieving unique mount ID for %s: %s\n", path,
513+
strerror(errno));
514+
return 0;
515+
}
516+
517+
if (!(sx.stx_mask & STATX_MNT_ID_UNIQUE)) {
518+
ksft_print_msg("no unique mount ID available for %s\n", path);
519+
return 0;
520+
}
521+
522+
return sx.stx_mnt_id;
523+
}

tools/testing/selftests/filesystems/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ static inline bool switch_userns(int fd, uid_t uid, gid_t gid, bool drop_caps)
4242
return true;
4343
}
4444

45+
extern uint64_t get_unique_mnt_id(const char *path);
46+
4547
#endif /* __IDMAP_UTILS_H */

0 commit comments

Comments
 (0)