1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4 *
5 * Basic fsopen() test which tries to configure and mount the filesystem as
6 * well.
7 */
8 #include "tst_test.h"
9 #include "lapi/fsmount.h"
10
11 #define MNTPOINT "mntpoint"
12
13 #define TCASE_ENTRY(_flags) {.name = "Flag " #_flags, .flags = _flags}
14
15 static struct tcase {
16 char *name;
17 unsigned int flags;
18 } tcases[] = {
19 TCASE_ENTRY(0),
20 TCASE_ENTRY(FSOPEN_CLOEXEC),
21 };
22
run(unsigned int n)23 static void run(unsigned int n)
24 {
25 struct tcase *tc = &tcases[n];
26 int fd, fsmfd;
27
28 TEST(fd = fsopen(tst_device->fs_type, tc->flags));
29 if (fd == -1) {
30 tst_res(TFAIL | TTERRNO, "fsopen() failed");
31 return;
32 }
33
34 TEST(fsconfig(fd, FSCONFIG_SET_STRING, "source", tst_device->dev, 0));
35 if (TST_RET == -1) {
36 tst_res(TFAIL | TTERRNO, "fsconfig(FSCONFIG_SET_STRING) failed");
37 goto out;
38 }
39
40 TEST(fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0));
41 if (TST_RET == -1) {
42 tst_res(TFAIL | TTERRNO, "fsconfig(FSCONFIG_CMD_CREATE) failed");
43 goto out;
44 }
45
46 TEST(fsmfd = fsmount(fd, 0, 0));
47 if (fsmfd == -1) {
48 tst_res(TFAIL | TTERRNO, "fsmount() failed");
49 goto out;
50 }
51
52 TEST(move_mount(fsmfd, "", AT_FDCWD, MNTPOINT,
53 MOVE_MOUNT_F_EMPTY_PATH));
54
55 SAFE_CLOSE(fsmfd);
56
57 if (TST_RET == -1) {
58 tst_res(TFAIL | TTERRNO, "move_mount() failed");
59 goto out;
60 }
61
62 if (tst_is_mounted_at_tmpdir(MNTPOINT)) {
63 SAFE_UMOUNT(MNTPOINT);
64 tst_res(TPASS, "%s: fsopen() passed", tc->name);
65 }
66
67 out:
68 SAFE_CLOSE(fd);
69 }
70
71 static struct tst_test test = {
72 .tcnt = ARRAY_SIZE(tcases),
73 .test = run,
74 .setup = fsopen_supported_by_kernel,
75 .needs_root = 1,
76 .format_device = 1,
77 .mntpoint = MNTPOINT,
78 .all_filesystems = 1,
79 .skip_filesystems = (const char *const []){"fuse", NULL},
80 };
81