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