1 // SPDX-License-Identifier: GPL-2.0-or-later
10 #include <sys/mount.h>
12 #include <sys/statfs.h>
13 #include <linux/stat.h>
15 #include "statmount.h"
16 #include "../../kselftest.h"
18 static const char *const known_fs[] = {
19 "9p", "adfs", "affs", "afs", "aio", "anon_inodefs", "apparmorfs",
20 "autofs", "bcachefs", "bdev", "befs", "bfs", "binder", "binfmt_misc",
21 "bpf", "btrfs", "btrfs_test_fs", "ceph", "cgroup", "cgroup2", "cifs",
22 "coda", "configfs", "cpuset", "cramfs", "cxl", "dax", "debugfs",
23 "devpts", "devtmpfs", "dmabuf", "drm", "ecryptfs", "efivarfs", "efs",
24 "erofs", "exfat", "ext2", "ext3", "ext4", "f2fs", "functionfs",
25 "fuse", "fuseblk", "fusectl", "gadgetfs", "gfs2", "gfs2meta", "hfs",
26 "hfsplus", "hostfs", "hpfs", "hugetlbfs", "ibmasmfs", "iomem",
27 "ipathfs", "iso9660", "jffs2", "jfs", "minix", "mqueue", "msdos",
28 "nfs", "nfs4", "nfsd", "nilfs2", "nsfs", "ntfs", "ntfs3", "ocfs2",
29 "ocfs2_dlmfs", "ocxlflash", "omfs", "openpromfs", "overlay", "pipefs",
30 "proc", "pstore", "pvfs2", "qnx4", "qnx6", "ramfs",
31 "resctrl", "romfs", "rootfs", "rpc_pipefs", "s390_hypfs", "secretmem",
32 "securityfs", "selinuxfs", "smackfs", "smb3", "sockfs", "spufs",
33 "squashfs", "sysfs", "sysv", "tmpfs", "tracefs", "ubifs", "udf",
34 "ufs", "v7", "vboxsf", "vfat", "virtiofs", "vxfs", "xenfs", "xfs",
37 static struct statmount *statmount_alloc(uint64_t mnt_id, uint64_t mask, unsigned int flags)
39 size_t bufsize = 1 << 15;
40 struct statmount *buf = NULL, *tmp = alloca(bufsize);
45 ret = statmount(mnt_id, 0, mask, tmp, bufsize, flags);
50 if (errno != EOVERFLOW)
54 tmp = malloc(bufsize);
58 buf = malloc(tmp->size);
60 memcpy(buf, tmp, tmp->size);
67 static void write_file(const char *path, const char *val)
69 int fd = open(path, O_WRONLY);
70 size_t len = strlen(val);
74 ksft_exit_fail_msg("opening %s for write: %s\n", path, strerror(errno));
76 ret = write(fd, val, len);
78 ksft_exit_fail_msg("writing to %s: %s\n", path, strerror(errno));
80 ksft_exit_fail_msg("short write to %s\n", path);
84 ksft_exit_fail_msg("closing %s\n", path);
87 static uint64_t get_mnt_id(const char *name, const char *path, uint64_t mask)
92 ret = statx(AT_FDCWD, path, 0, mask, &sx);
94 ksft_exit_fail_msg("retrieving %s mount ID for %s: %s\n",
95 mask & STATX_MNT_ID_UNIQUE ? "unique" : "old",
96 name, strerror(errno));
97 if (!(sx.stx_mask & mask))
98 ksft_exit_fail_msg("no %s mount ID available for %s\n",
99 mask & STATX_MNT_ID_UNIQUE ? "unique" : "old",
102 return sx.stx_mnt_id;
106 static char root_mntpoint[] = "/tmp/statmount_test_root.XXXXXX";
107 static int orig_root;
108 static uint64_t root_id, parent_id;
109 static uint32_t old_root_id, old_parent_id;
110 static FILE *f_mountinfo;
112 static void cleanup_namespace(void)
116 ret = fchdir(orig_root);
118 ksft_perror("fchdir to original root");
122 ksft_perror("chroot to original root");
124 umount2(root_mntpoint, MNT_DETACH);
125 rmdir(root_mntpoint);
128 static void setup_namespace(void)
132 uid_t uid = getuid();
133 gid_t gid = getgid();
135 ret = unshare(CLONE_NEWNS|CLONE_NEWUSER|CLONE_NEWPID);
137 ksft_exit_fail_msg("unsharing mountns and userns: %s\n",
140 sprintf(buf, "0 %d 1", uid);
141 write_file("/proc/self/uid_map", buf);
142 write_file("/proc/self/setgroups", "deny");
143 sprintf(buf, "0 %d 1", gid);
144 write_file("/proc/self/gid_map", buf);
146 f_mountinfo = fopen("/proc/self/mountinfo", "re");
148 ksft_exit_fail_msg("failed to open mountinfo: %s\n",
151 ret = mount("", "/", NULL, MS_REC|MS_PRIVATE, NULL);
153 ksft_exit_fail_msg("making mount tree private: %s\n",
156 if (!mkdtemp(root_mntpoint))
157 ksft_exit_fail_msg("creating temporary directory %s: %s\n",
158 root_mntpoint, strerror(errno));
160 old_parent_id = get_mnt_id("parent", root_mntpoint, STATX_MNT_ID);
161 parent_id = get_mnt_id("parent", root_mntpoint, STATX_MNT_ID_UNIQUE);
163 orig_root = open("/", O_PATH);
165 ksft_exit_fail_msg("opening root directory: %s",
168 atexit(cleanup_namespace);
170 ret = mount(root_mntpoint, root_mntpoint, NULL, MS_BIND, NULL);
172 ksft_exit_fail_msg("mounting temp root %s: %s\n",
173 root_mntpoint, strerror(errno));
175 ret = chroot(root_mntpoint);
177 ksft_exit_fail_msg("chroot to temp root %s: %s\n",
178 root_mntpoint, strerror(errno));
182 ksft_exit_fail_msg("chdir to root: %s\n", strerror(errno));
184 old_root_id = get_mnt_id("root", "/", STATX_MNT_ID);
185 root_id = get_mnt_id("root", "/", STATX_MNT_ID_UNIQUE);
188 static int setup_mount_tree(int log2_num)
192 ret = mount("", "/", NULL, MS_REC|MS_SHARED, NULL);
194 ksft_test_result_fail("making mount tree shared: %s\n",
199 for (i = 0; i < log2_num; i++) {
200 ret = mount("/", "/", NULL, MS_BIND, NULL);
202 ksft_test_result_fail("mounting submount %s: %s\n",
203 root_mntpoint, strerror(errno));
210 static void test_listmount_empty_root(void)
213 const unsigned int size = 32;
216 res = listmount(LSMT_ROOT, 0, 0, list, size, 0);
218 ksft_test_result_fail("listmount: %s\n", strerror(errno));
222 ksft_test_result_fail("listmount result is %zi != 1\n", res);
226 if (list[0] != root_id) {
227 ksft_test_result_fail("listmount ID doesn't match 0x%llx != 0x%llx\n",
228 (unsigned long long) list[0],
229 (unsigned long long) root_id);
233 ksft_test_result_pass("listmount empty root\n");
236 static void test_statmount_zero_mask(void)
241 ret = statmount(root_id, 0, 0, &sm, sizeof(sm), 0);
243 ksft_test_result_fail("statmount zero mask: %s\n",
247 if (sm.size != sizeof(sm)) {
248 ksft_test_result_fail("unexpected size: %u != %u\n",
249 sm.size, (uint32_t) sizeof(sm));
253 ksft_test_result_fail("unexpected mask: 0x%llx != 0x0\n",
254 (unsigned long long) sm.mask);
258 ksft_test_result_pass("statmount zero mask\n");
261 static void test_statmount_mnt_basic(void)
265 uint64_t mask = STATMOUNT_MNT_BASIC;
267 ret = statmount(root_id, 0, mask, &sm, sizeof(sm), 0);
269 ksft_test_result_fail("statmount mnt basic: %s\n",
273 if (sm.size != sizeof(sm)) {
274 ksft_test_result_fail("unexpected size: %u != %u\n",
275 sm.size, (uint32_t) sizeof(sm));
278 if (sm.mask != mask) {
279 ksft_test_result_skip("statmount mnt basic unavailable\n");
283 if (sm.mnt_id != root_id) {
284 ksft_test_result_fail("unexpected root ID: 0x%llx != 0x%llx\n",
285 (unsigned long long) sm.mnt_id,
286 (unsigned long long) root_id);
290 if (sm.mnt_id_old != old_root_id) {
291 ksft_test_result_fail("unexpected old root ID: %u != %u\n",
292 sm.mnt_id_old, old_root_id);
296 if (sm.mnt_parent_id != parent_id) {
297 ksft_test_result_fail("unexpected parent ID: 0x%llx != 0x%llx\n",
298 (unsigned long long) sm.mnt_parent_id,
299 (unsigned long long) parent_id);
303 if (sm.mnt_parent_id_old != old_parent_id) {
304 ksft_test_result_fail("unexpected old parent ID: %u != %u\n",
305 sm.mnt_parent_id_old, old_parent_id);
309 if (sm.mnt_propagation != MS_PRIVATE) {
310 ksft_test_result_fail("unexpected propagation: 0x%llx\n",
311 (unsigned long long) sm.mnt_propagation);
315 ksft_test_result_pass("statmount mnt basic\n");
319 static void test_statmount_sb_basic(void)
323 uint64_t mask = STATMOUNT_SB_BASIC;
327 ret = statmount(root_id, 0, mask, &sm, sizeof(sm), 0);
329 ksft_test_result_fail("statmount sb basic: %s\n",
333 if (sm.size != sizeof(sm)) {
334 ksft_test_result_fail("unexpected size: %u != %u\n",
335 sm.size, (uint32_t) sizeof(sm));
338 if (sm.mask != mask) {
339 ksft_test_result_skip("statmount sb basic unavailable\n");
343 ret = statx(AT_FDCWD, "/", 0, 0, &sx);
345 ksft_test_result_fail("stat root failed: %s\n",
350 if (sm.sb_dev_major != sx.stx_dev_major ||
351 sm.sb_dev_minor != sx.stx_dev_minor) {
352 ksft_test_result_fail("unexpected sb dev %u:%u != %u:%u\n",
353 sm.sb_dev_major, sm.sb_dev_minor,
354 sx.stx_dev_major, sx.stx_dev_minor);
358 ret = statfs("/", &sf);
360 ksft_test_result_fail("statfs root failed: %s\n",
365 if (sm.sb_magic != sf.f_type) {
366 ksft_test_result_fail("unexpected sb magic: 0x%llx != 0x%lx\n",
367 (unsigned long long) sm.sb_magic,
372 ksft_test_result_pass("statmount sb basic\n");
375 static void test_statmount_mnt_point(void)
377 struct statmount *sm;
379 sm = statmount_alloc(root_id, STATMOUNT_MNT_POINT, 0);
381 ksft_test_result_fail("statmount mount point: %s\n",
386 if (strcmp(sm->str + sm->mnt_point, "/") != 0) {
387 ksft_test_result_fail("unexpected mount point: '%s' != '/'\n",
388 sm->str + sm->mnt_point);
391 ksft_test_result_pass("statmount mount point\n");
396 static void test_statmount_mnt_root(void)
398 struct statmount *sm;
399 const char *mnt_root, *last_dir, *last_root;
401 last_dir = strrchr(root_mntpoint, '/');
405 sm = statmount_alloc(root_id, STATMOUNT_MNT_ROOT, 0);
407 ksft_test_result_fail("statmount mount root: %s\n",
411 mnt_root = sm->str + sm->mnt_root;
412 last_root = strrchr(mnt_root, '/');
416 last_root = mnt_root;
418 if (strcmp(last_dir, last_root) != 0) {
419 ksft_test_result_fail("unexpected mount root last component: '%s' != '%s'\n",
420 last_root, last_dir);
423 ksft_test_result_pass("statmount mount root\n");
428 static void test_statmount_fs_type(void)
430 struct statmount *sm;
432 const char *const *s;
434 sm = statmount_alloc(root_id, STATMOUNT_FS_TYPE, 0);
436 ksft_test_result_fail("statmount fs type: %s\n",
440 fs_type = sm->str + sm->fs_type;
441 for (s = known_fs; s != NULL; s++) {
442 if (strcmp(fs_type, *s) == 0)
446 ksft_print_msg("unknown filesystem type: %s\n", fs_type);
448 ksft_test_result_pass("statmount fs type\n");
452 static void test_statmount_mnt_opts(void)
454 struct statmount *sm;
455 const char *statmount_opts;
459 sm = statmount_alloc(root_id, STATMOUNT_MNT_BASIC | STATMOUNT_MNT_OPTS,
462 ksft_test_result_fail("statmount mnt opts: %s\n",
467 while (getline(&line, &len, f_mountinfo) != -1) {
470 unsigned int old_mnt_id;
472 old_mnt_id = atoi(line);
473 if (old_mnt_id != sm->mnt_id_old)
476 for (p = line, i = 0; p && i < 5; i++)
477 p = strchr(p + 1, ' ');
481 p2 = strchr(p + 1, ' ');
485 p = strchr(p2 + 1, '-');
488 for (p++, i = 0; p && i < 2; i++)
489 p = strchr(p + 1, ' ');
494 /* skip generic superblock options */
495 if (strncmp(p, "ro", 2) == 0)
497 else if (strncmp(p, "rw", 2) == 0)
501 if (strncmp(p, "sync", 4) == 0)
505 if (strncmp(p, "dirsync", 7) == 0)
509 if (strncmp(p, "lazytime", 8) == 0)
513 p2 = strrchr(p, '\n');
517 statmount_opts = sm->str + sm->mnt_opts;
518 if (strcmp(statmount_opts, p) != 0)
519 ksft_test_result_fail(
520 "unexpected mount options: '%s' != '%s'\n",
523 ksft_test_result_pass("statmount mount options\n");
529 ksft_test_result_fail("didnt't find mount entry\n");
534 static void test_statmount_string(uint64_t mask, size_t off, const char *name)
536 struct statmount *sm;
537 size_t len, shortsize, exactsize;
541 sm = statmount_alloc(root_id, mask, 0);
543 ksft_test_result_fail("statmount %s: %s\n", name,
547 if (sm->size < sizeof(*sm)) {
548 ksft_test_result_fail("unexpected size: %u < %u\n",
549 sm->size, (uint32_t) sizeof(*sm));
552 if (sm->mask != mask) {
553 ksft_test_result_skip("statmount %s unavailable\n", name);
556 len = sm->size - sizeof(*sm);
557 start = ((uint32_t *) sm)[off];
559 for (i = start;; i++) {
561 ksft_test_result_fail("string out of bounds\n");
567 exactsize = sm->size;
568 shortsize = sizeof(*sm) + i;
570 ret = statmount(root_id, 0, mask, sm, exactsize, 0);
572 ksft_test_result_fail("statmount exact size: %s\n",
577 ret = statmount(root_id, 0, mask, sm, shortsize, 0);
578 if (ret != -1 || errno != EOVERFLOW) {
579 ksft_test_result_fail("should have failed with EOVERFLOW: %s\n",
584 ksft_test_result_pass("statmount string %s\n", name);
589 static void test_listmount_tree(void)
592 const unsigned int log2_num = 4;
593 const unsigned int step = 3;
594 const unsigned int size = (1 << log2_num) + step + 1;
595 size_t num, expect = 1 << log2_num;
597 uint64_t list2[size];
601 res = setup_mount_tree(log2_num);
605 num = res = listmount(LSMT_ROOT, 0, 0, list, size, 0);
607 ksft_test_result_fail("listmount: %s\n", strerror(errno));
611 ksft_test_result_fail("listmount result is %zi != %zi\n",
616 for (i = 0; i < size - step;) {
617 res = listmount(LSMT_ROOT, 0, i ? list2[i - 1] : 0, list2 + i, step, 0);
619 ksft_test_result_fail("short listmount: %s\n",
626 ksft_test_result_fail("different number of entries: %zu != %zu\n",
630 for (i = 0; i < num; i++) {
631 if (list2[i] != list[i]) {
632 ksft_test_result_fail("different value for entry %zu: 0x%llx != 0x%llx\n",
634 (unsigned long long) list2[i],
635 (unsigned long long) list[i]);
639 ksft_test_result_pass("listmount tree\n");
642 #define str_off(memb) (offsetof(struct statmount, memb) / sizeof(uint32_t))
647 uint64_t all_mask = STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC |
648 STATMOUNT_PROPAGATE_FROM | STATMOUNT_MNT_ROOT |
649 STATMOUNT_MNT_POINT | STATMOUNT_FS_TYPE | STATMOUNT_MNT_NS_ID;
653 ret = statmount(0, 0, 0, NULL, 0, 0);
656 ksft_exit_skip("statmount() syscall not supported\n");
661 test_listmount_empty_root();
662 test_statmount_zero_mask();
663 test_statmount_mnt_basic();
664 test_statmount_sb_basic();
665 test_statmount_mnt_root();
666 test_statmount_mnt_point();
667 test_statmount_fs_type();
668 test_statmount_mnt_opts();
669 test_statmount_string(STATMOUNT_MNT_ROOT, str_off(mnt_root), "mount root");
670 test_statmount_string(STATMOUNT_MNT_POINT, str_off(mnt_point), "mount point");
671 test_statmount_string(STATMOUNT_FS_TYPE, str_off(fs_type), "fs type");
672 test_statmount_string(all_mask, str_off(mnt_root), "mount root & all");
673 test_statmount_string(all_mask, str_off(mnt_point), "mount point & all");
674 test_statmount_string(all_mask, str_off(fs_type), "fs type & all");
676 test_listmount_tree();
679 if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)