1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
4 */
5
6 /*\
7 * [Description]
8 *
9 * This test verifies that statmount() is correctly reading mount information
10 * (mount id, parent mount id, mount attributes etc.) using STATMOUNT_MNT_BASIC.
11 *
12 * [Algorithm]
13 *
14 * - create a mount point
15 * - create a new parent folder inside the mount point and obtain its mount info
16 * - create the new "/" mount folder and obtain its mount info
17 * - run statmount() on the mount point using STATMOUNT_MNT_BASIC
18 * - read results and check if mount info are correct
19 */
20
21 #define _GNU_SOURCE
22
23 #include "statmount.h"
24 #include "lapi/stat.h"
25 #include "lapi/sched.h"
26
27 #define DIR_A "LTP_DIR_A"
28 #define DIR_B "LTP_DIR_B"
29
30 static uint64_t mnt_id;
31 static uint64_t mnt_id_old;
32 static uint64_t parent_id;
33 static uint64_t parent_id_old;
34 static uint64_t mnt_peer_group;
35 static uint64_t mnt_master;
36 static struct statmount *st_mount;
37
read_mnt_id(const char * path,uint64_t * mnt_id,uint64_t * mnt_id_unique)38 static void read_mnt_id(
39 const char *path,
40 uint64_t *mnt_id,
41 uint64_t *mnt_id_unique)
42 {
43 struct ltp_statx sx;
44
45 if (mnt_id) {
46 sx.data.stx_mask = STATX_MNT_ID;
47
48 SAFE_STATX(AT_FDCWD, path, 0, STATX_MNT_ID, &sx);
49 *mnt_id = sx.data.stx_mnt_id;
50 }
51
52 if (mnt_id_unique) {
53 sx.data.stx_mask = STATX_MNT_ID_UNIQUE;
54
55 SAFE_STATX(AT_FDCWD, path, 0, STATX_MNT_ID_UNIQUE, &sx);
56 *mnt_id_unique = sx.data.stx_mnt_id;
57 }
58 }
59
60 static struct tcase {
61 uint64_t prop_type;
62 char *msg;
63 } tcases[] = {
64 { MS_PRIVATE, TST_TO_STR_(MS_PRIVATE) },
65 { MS_SHARED, TST_TO_STR_(MS_SHARED) },
66 { MS_SLAVE, TST_TO_STR_(MS_SLAVE) },
67 { MS_UNBINDABLE, TST_TO_STR_(MS_UNBINDABLE) },
68 };
69
run(unsigned int i)70 static void run(unsigned int i)
71 {
72 struct tcase *tc = &tcases[i];
73
74 tst_res(TINFO, "Testing statmount() on %s", tc->msg);
75
76 SAFE_MOUNT(DIR_B, DIR_A, "none", MS_BIND, NULL);
77 SAFE_MOUNT("none", DIR_A, "none", tc->prop_type, NULL);
78 read_mnt_id(DIR_A, &mnt_id_old, &mnt_id);
79
80 memset(st_mount, 0, sizeof(struct statmount));
81
82 TST_EXP_PASS(statmount(mnt_id, STATMOUNT_MNT_BASIC, st_mount,
83 sizeof(struct statmount), 0));
84
85 SAFE_UMOUNT(DIR_A);
86
87 if (!TST_PASS)
88 return;
89
90 mnt_peer_group = tc->prop_type == MS_SHARED ? read_peer_group(DIR_A) : 0;
91 mnt_master = tc->prop_type == MS_SLAVE ? read_peer_group(DIR_A) : 0;
92
93 TST_EXP_EQ_LI(st_mount->mask, STATMOUNT_MNT_BASIC);
94 TST_EXP_EQ_LI(st_mount->size, sizeof(struct statmount));
95 TST_EXP_EQ_LI(st_mount->mnt_id, mnt_id);
96 TST_EXP_EQ_LI(st_mount->mnt_id_old, mnt_id_old);
97 TST_EXP_EQ_LI(st_mount->mnt_parent_id, parent_id);
98 TST_EXP_EQ_LI(st_mount->mnt_parent_id_old, parent_id_old);
99 TST_EXP_EQ_LU(st_mount->mnt_propagation & tc->prop_type, tc->prop_type);
100 TST_EXP_EQ_LI(st_mount->mnt_master, mnt_master);
101 TST_EXP_EQ_LI(st_mount->mnt_peer_group, mnt_peer_group);
102 }
103
setup(void)104 static void setup(void)
105 {
106 SAFE_MKDIR(DIR_A, 0700);
107 SAFE_MKDIR(DIR_B, 0700);
108
109 SAFE_UNSHARE(CLONE_NEWNS);
110 SAFE_MOUNT("none", "/", "none", MS_REC | MS_PRIVATE, NULL);
111
112 SAFE_MOUNT(DIR_B, DIR_B, "none", MS_BIND, NULL);
113 SAFE_MOUNT("none", DIR_B, "none", MS_SHARED, NULL);
114
115 read_mnt_id(DIR_A, &parent_id_old, &parent_id);
116 }
117
cleanup(void)118 static void cleanup(void)
119 {
120 if (tst_is_mounted(DIR_B))
121 SAFE_UMOUNT(DIR_B);
122
123 if (tst_is_mounted(DIR_A))
124 SAFE_UMOUNT(DIR_A);
125 }
126
127 static struct tst_test test = {
128 .test = run,
129 .tcnt = ARRAY_SIZE(tcases),
130 .setup = setup,
131 .cleanup = cleanup,
132 .min_kver = "6.8",
133 .needs_tmpdir = 1,
134 .bufs = (struct tst_buffers []) {
135 {&st_mount, .size = sizeof(struct statmount)},
136 {}
137 }
138 };
139