1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4 *
5 * Basic move_mount() failure tests.
6 */
7 #include "tst_test.h"
8 #include "lapi/fsmount.h"
9
10 #define MNTPOINT "mntpoint"
11
12 int invalid_fd = -1, fsmfd;
13
14 static struct tcase {
15 char *name;
16 int *from_dirfd;
17 const char *from_pathname;
18 int to_dirfd;
19 const char *to_pathname;
20 unsigned int flags;
21 int exp_errno;
22 } tcases[] = {
23 {"invalid-from-fd", &invalid_fd, "", AT_FDCWD, MNTPOINT, MOVE_MOUNT_F_EMPTY_PATH, EBADF},
24 {"invalid-from-path", &fsmfd, "invalid", AT_FDCWD, MNTPOINT, MOVE_MOUNT_F_EMPTY_PATH, ENOENT},
25 {"invalid-to-fd", &fsmfd, "", -1, MNTPOINT, MOVE_MOUNT_F_EMPTY_PATH, EBADF},
26 {"invalid-to-path", &fsmfd, "", AT_FDCWD, "invalid", MOVE_MOUNT_F_EMPTY_PATH, ENOENT},
27 {"invalid-flags", &fsmfd, "", AT_FDCWD, MNTPOINT, 0x08, EINVAL},
28 };
29
run(unsigned int n)30 static void run(unsigned int n)
31 {
32 struct tcase *tc = &tcases[n];
33 int fd;
34
35 TEST(fd = fsopen(tst_device->fs_type, 0));
36 if (fd == -1) {
37 tst_res(TFAIL | TTERRNO, "fsopen() failed");
38 return;
39 }
40
41 TEST(fsconfig(fd, FSCONFIG_SET_STRING, "source", tst_device->dev, 0));
42 if (TST_RET == -1) {
43 SAFE_CLOSE(fd);
44 tst_res(TFAIL | TTERRNO, "fsconfig(FSCONFIG_SET_STRING) failed");
45 return;
46 }
47
48 TEST(fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0));
49 if (TST_RET == -1) {
50 SAFE_CLOSE(fd);
51 tst_res(TFAIL | TTERRNO, "fsconfig(FSCONFIG_CMD_CREATE) failed");
52 return;
53 }
54
55 TEST(fsmfd = fsmount(fd, 0, 0));
56 SAFE_CLOSE(fd);
57
58 if (fsmfd == -1) {
59 tst_res(TFAIL | TTERRNO, "fsmount() failed");
60 return;
61 }
62
63 TEST(move_mount(*tc->from_dirfd, tc->from_pathname, tc->to_dirfd,
64 tc->to_pathname, tc->flags));
65 SAFE_CLOSE(fsmfd);
66
67 if (TST_RET != -1) {
68 SAFE_UMOUNT(MNTPOINT);
69 tst_res(TFAIL, "%s: move_mount() succeeded unexpectedly (index: %d)",
70 tc->name, n);
71 return;
72 }
73
74 if (tc->exp_errno != TST_ERR) {
75 tst_res(TFAIL | TTERRNO, "%s: move_mount() should fail with %s",
76 tc->name, tst_strerrno(tc->exp_errno));
77 return;
78 }
79
80 tst_res(TPASS | TTERRNO, "%s: move_mount() failed as expected", tc->name);
81 }
82
83 static struct tst_test test = {
84 .tcnt = ARRAY_SIZE(tcases),
85 .test = run,
86 .setup = fsopen_supported_by_kernel,
87 .needs_root = 1,
88 .format_device = 1,
89 .mntpoint = MNTPOINT,
90 .all_filesystems = 1,
91 .skip_filesystems = (const char *const []){"fuse", NULL},
92 };
93