1edb65ee5SAlexei Starovoitov // SPDX-License-Identifier: GPL-2.0
2edb65ee5SAlexei Starovoitov /* Copyright (c) 2020 Facebook */
3edb65ee5SAlexei Starovoitov #define _GNU_SOURCE
47e5ad817SLorenz Bauer #include <stdio.h>
5edb65ee5SAlexei Starovoitov #include <sched.h>
6edb65ee5SAlexei Starovoitov #include <sys/mount.h>
7edb65ee5SAlexei Starovoitov #include <sys/stat.h>
8edb65ee5SAlexei Starovoitov #include <sys/types.h>
9edb65ee5SAlexei Starovoitov #include <test_progs.h>
10edb65ee5SAlexei Starovoitov
11*cd60f410SManu Bretelle /* TDIR must be in a location we can create a directory in. */
12*cd60f410SManu Bretelle #define TDIR "/tmp/test_bpffs_testdir"
13edb65ee5SAlexei Starovoitov
read_iter(char * file)14edb65ee5SAlexei Starovoitov static int read_iter(char *file)
15edb65ee5SAlexei Starovoitov {
16edb65ee5SAlexei Starovoitov /* 1024 should be enough to get contiguous 4 "iter" letters at some point */
17edb65ee5SAlexei Starovoitov char buf[1024];
18edb65ee5SAlexei Starovoitov int fd, len;
19edb65ee5SAlexei Starovoitov
20edb65ee5SAlexei Starovoitov fd = open(file, 0);
21edb65ee5SAlexei Starovoitov if (fd < 0)
22edb65ee5SAlexei Starovoitov return -1;
2357428298SAndrii Nakryiko while ((len = read(fd, buf, sizeof(buf))) > 0) {
2457428298SAndrii Nakryiko buf[sizeof(buf) - 1] = '\0';
25edb65ee5SAlexei Starovoitov if (strstr(buf, "iter")) {
26edb65ee5SAlexei Starovoitov close(fd);
27edb65ee5SAlexei Starovoitov return 0;
28edb65ee5SAlexei Starovoitov }
2957428298SAndrii Nakryiko }
30edb65ee5SAlexei Starovoitov close(fd);
31edb65ee5SAlexei Starovoitov return -1;
32edb65ee5SAlexei Starovoitov }
33edb65ee5SAlexei Starovoitov
fn(void)34edb65ee5SAlexei Starovoitov static int fn(void)
35edb65ee5SAlexei Starovoitov {
367e5ad817SLorenz Bauer struct stat a, b, c;
377e5ad817SLorenz Bauer int err, map;
38edb65ee5SAlexei Starovoitov
39edb65ee5SAlexei Starovoitov err = unshare(CLONE_NEWNS);
409fc23c22SLorenz Bauer if (!ASSERT_OK(err, "unshare"))
41edb65ee5SAlexei Starovoitov goto out;
42edb65ee5SAlexei Starovoitov
43edb65ee5SAlexei Starovoitov err = mount("", "/", "", MS_REC | MS_PRIVATE, NULL);
449fc23c22SLorenz Bauer if (!ASSERT_OK(err, "mount /"))
45edb65ee5SAlexei Starovoitov goto out;
46edb65ee5SAlexei Starovoitov
47*cd60f410SManu Bretelle err = mkdir(TDIR, 0777);
48*cd60f410SManu Bretelle /* If the directory already exists we can carry on. It may be left over
49*cd60f410SManu Bretelle * from a previous run.
50*cd60f410SManu Bretelle */
51*cd60f410SManu Bretelle if ((err && errno != EEXIST) && !ASSERT_OK(err, "mkdir " TDIR))
52edb65ee5SAlexei Starovoitov goto out;
53edb65ee5SAlexei Starovoitov
54edb65ee5SAlexei Starovoitov err = mount("none", TDIR, "tmpfs", 0, NULL);
559fc23c22SLorenz Bauer if (!ASSERT_OK(err, "mount tmpfs"))
56edb65ee5SAlexei Starovoitov goto out;
57edb65ee5SAlexei Starovoitov
58edb65ee5SAlexei Starovoitov err = mkdir(TDIR "/fs1", 0777);
599fc23c22SLorenz Bauer if (!ASSERT_OK(err, "mkdir " TDIR "/fs1"))
60edb65ee5SAlexei Starovoitov goto out;
61edb65ee5SAlexei Starovoitov err = mkdir(TDIR "/fs2", 0777);
629fc23c22SLorenz Bauer if (!ASSERT_OK(err, "mkdir " TDIR "/fs2"))
63edb65ee5SAlexei Starovoitov goto out;
64edb65ee5SAlexei Starovoitov
65edb65ee5SAlexei Starovoitov err = mount("bpf", TDIR "/fs1", "bpf", 0, NULL);
669fc23c22SLorenz Bauer if (!ASSERT_OK(err, "mount bpffs " TDIR "/fs1"))
67edb65ee5SAlexei Starovoitov goto out;
68edb65ee5SAlexei Starovoitov err = mount("bpf", TDIR "/fs2", "bpf", 0, NULL);
699fc23c22SLorenz Bauer if (!ASSERT_OK(err, "mount bpffs " TDIR "/fs2"))
70edb65ee5SAlexei Starovoitov goto out;
71edb65ee5SAlexei Starovoitov
72edb65ee5SAlexei Starovoitov err = read_iter(TDIR "/fs1/maps.debug");
739fc23c22SLorenz Bauer if (!ASSERT_OK(err, "reading " TDIR "/fs1/maps.debug"))
74edb65ee5SAlexei Starovoitov goto out;
75edb65ee5SAlexei Starovoitov err = read_iter(TDIR "/fs2/progs.debug");
769fc23c22SLorenz Bauer if (!ASSERT_OK(err, "reading " TDIR "/fs2/progs.debug"))
77edb65ee5SAlexei Starovoitov goto out;
787e5ad817SLorenz Bauer
797e5ad817SLorenz Bauer err = mkdir(TDIR "/fs1/a", 0777);
807e5ad817SLorenz Bauer if (!ASSERT_OK(err, "creating " TDIR "/fs1/a"))
817e5ad817SLorenz Bauer goto out;
827e5ad817SLorenz Bauer err = mkdir(TDIR "/fs1/a/1", 0777);
837e5ad817SLorenz Bauer if (!ASSERT_OK(err, "creating " TDIR "/fs1/a/1"))
847e5ad817SLorenz Bauer goto out;
857e5ad817SLorenz Bauer err = mkdir(TDIR "/fs1/b", 0777);
867e5ad817SLorenz Bauer if (!ASSERT_OK(err, "creating " TDIR "/fs1/b"))
877e5ad817SLorenz Bauer goto out;
887e5ad817SLorenz Bauer
892fe256a4SAndrii Nakryiko map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 4, 1, NULL);
907e5ad817SLorenz Bauer if (!ASSERT_GT(map, 0, "create_map(ARRAY)"))
917e5ad817SLorenz Bauer goto out;
927e5ad817SLorenz Bauer err = bpf_obj_pin(map, TDIR "/fs1/c");
937e5ad817SLorenz Bauer if (!ASSERT_OK(err, "pin map"))
947e5ad817SLorenz Bauer goto out;
957e5ad817SLorenz Bauer close(map);
967e5ad817SLorenz Bauer
977e5ad817SLorenz Bauer /* Check that RENAME_EXCHANGE works for directories. */
987e5ad817SLorenz Bauer err = stat(TDIR "/fs1/a", &a);
997e5ad817SLorenz Bauer if (!ASSERT_OK(err, "stat(" TDIR "/fs1/a)"))
1007e5ad817SLorenz Bauer goto out;
1017e5ad817SLorenz Bauer err = renameat2(0, TDIR "/fs1/a", 0, TDIR "/fs1/b", RENAME_EXCHANGE);
1027e5ad817SLorenz Bauer if (!ASSERT_OK(err, "renameat2(/fs1/a, /fs1/b, RENAME_EXCHANGE)"))
1037e5ad817SLorenz Bauer goto out;
1047e5ad817SLorenz Bauer err = stat(TDIR "/fs1/b", &b);
1057e5ad817SLorenz Bauer if (!ASSERT_OK(err, "stat(" TDIR "/fs1/b)"))
1067e5ad817SLorenz Bauer goto out;
1077e5ad817SLorenz Bauer if (!ASSERT_EQ(a.st_ino, b.st_ino, "b should have a's inode"))
1087e5ad817SLorenz Bauer goto out;
1097e5ad817SLorenz Bauer err = access(TDIR "/fs1/b/1", F_OK);
1107e5ad817SLorenz Bauer if (!ASSERT_OK(err, "access(" TDIR "/fs1/b/1)"))
1117e5ad817SLorenz Bauer goto out;
1127e5ad817SLorenz Bauer
1137e5ad817SLorenz Bauer /* Check that RENAME_EXCHANGE works for mixed file types. */
1147e5ad817SLorenz Bauer err = stat(TDIR "/fs1/c", &c);
1157e5ad817SLorenz Bauer if (!ASSERT_OK(err, "stat(" TDIR "/fs1/map)"))
1167e5ad817SLorenz Bauer goto out;
1177e5ad817SLorenz Bauer err = renameat2(0, TDIR "/fs1/c", 0, TDIR "/fs1/b", RENAME_EXCHANGE);
1187e5ad817SLorenz Bauer if (!ASSERT_OK(err, "renameat2(/fs1/c, /fs1/b, RENAME_EXCHANGE)"))
1197e5ad817SLorenz Bauer goto out;
1207e5ad817SLorenz Bauer err = stat(TDIR "/fs1/b", &b);
1217e5ad817SLorenz Bauer if (!ASSERT_OK(err, "stat(" TDIR "/fs1/b)"))
1227e5ad817SLorenz Bauer goto out;
1237e5ad817SLorenz Bauer if (!ASSERT_EQ(c.st_ino, b.st_ino, "b should have c's inode"))
1247e5ad817SLorenz Bauer goto out;
1257e5ad817SLorenz Bauer err = access(TDIR "/fs1/c/1", F_OK);
1267e5ad817SLorenz Bauer if (!ASSERT_OK(err, "access(" TDIR "/fs1/c/1)"))
1277e5ad817SLorenz Bauer goto out;
1287e5ad817SLorenz Bauer
1297e5ad817SLorenz Bauer /* Check that RENAME_NOREPLACE works. */
1307e5ad817SLorenz Bauer err = renameat2(0, TDIR "/fs1/b", 0, TDIR "/fs1/a", RENAME_NOREPLACE);
1317e5ad817SLorenz Bauer if (!ASSERT_ERR(err, "renameat2(RENAME_NOREPLACE)")) {
1327e5ad817SLorenz Bauer err = -EINVAL;
1337e5ad817SLorenz Bauer goto out;
1347e5ad817SLorenz Bauer }
1357e5ad817SLorenz Bauer err = access(TDIR "/fs1/b", F_OK);
1367e5ad817SLorenz Bauer if (!ASSERT_OK(err, "access(" TDIR "/fs1/b)"))
1377e5ad817SLorenz Bauer goto out;
1387e5ad817SLorenz Bauer
139edb65ee5SAlexei Starovoitov out:
140edb65ee5SAlexei Starovoitov umount(TDIR "/fs1");
141edb65ee5SAlexei Starovoitov umount(TDIR "/fs2");
142edb65ee5SAlexei Starovoitov rmdir(TDIR "/fs1");
143edb65ee5SAlexei Starovoitov rmdir(TDIR "/fs2");
144edb65ee5SAlexei Starovoitov umount(TDIR);
145*cd60f410SManu Bretelle rmdir(TDIR);
146edb65ee5SAlexei Starovoitov exit(err);
147edb65ee5SAlexei Starovoitov }
148edb65ee5SAlexei Starovoitov
test_test_bpffs(void)149edb65ee5SAlexei Starovoitov void test_test_bpffs(void)
150edb65ee5SAlexei Starovoitov {
151edb65ee5SAlexei Starovoitov int err, duration = 0, status = 0;
152edb65ee5SAlexei Starovoitov pid_t pid;
153edb65ee5SAlexei Starovoitov
154edb65ee5SAlexei Starovoitov pid = fork();
155edb65ee5SAlexei Starovoitov if (CHECK(pid == -1, "clone", "clone failed %d", errno))
156edb65ee5SAlexei Starovoitov return;
157edb65ee5SAlexei Starovoitov if (pid == 0)
158edb65ee5SAlexei Starovoitov fn();
159edb65ee5SAlexei Starovoitov err = waitpid(pid, &status, 0);
160edb65ee5SAlexei Starovoitov if (CHECK(err == -1 && errno != ECHILD, "waitpid", "failed %d", errno))
161edb65ee5SAlexei Starovoitov return;
162edb65ee5SAlexei Starovoitov if (CHECK(WEXITSTATUS(status), "bpffs test ", "failed %d", WEXITSTATUS(status)))
163edb65ee5SAlexei Starovoitov return;
164edb65ee5SAlexei Starovoitov }
165