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