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 fsconfig() 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_cistatic int fd;
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_cistatic void cleanup(void)
16f08c3bdfSopenharmony_ci{
17f08c3bdfSopenharmony_ci	if (fd != -1)
18f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
19f08c3bdfSopenharmony_ci}
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic void run(void)
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	int fsmfd;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	TEST(fd = fsopen(tst_device->fs_type, 0));
26f08c3bdfSopenharmony_ci	if (fd == -1)
27f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "fsopen() failed");
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_SET_FLAG, "rw", NULL, 0));
30f08c3bdfSopenharmony_ci	if (TST_RET == -1)
31f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO, "fsconfig(FSCONFIG_SET_FLAG) failed");
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_SET_STRING, "source", tst_device->dev, 0));
34f08c3bdfSopenharmony_ci	if (TST_RET == -1)
35f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO, "fsconfig(FSCONFIG_SET_STRING) failed");
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_SET_PATH, "sync", tst_device->dev, 0));
38f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
39f08c3bdfSopenharmony_ci		if (TST_ERR == EOPNOTSUPP)
40f08c3bdfSopenharmony_ci			tst_res(TCONF, "fsconfig(FSCONFIG_SET_PATH) not supported");
41f08c3bdfSopenharmony_ci		else
42f08c3bdfSopenharmony_ci			tst_brk(TFAIL | TTERRNO, "fsconfig(FSCONFIG_SET_PATH) failed");
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_SET_PATH_EMPTY, "sync", tst_device->dev, 0));
46f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
47f08c3bdfSopenharmony_ci		if (TST_ERR == EOPNOTSUPP)
48f08c3bdfSopenharmony_ci			tst_res(TCONF, "fsconfig(FSCONFIG_SET_PATH_EMPTY) not supported");
49f08c3bdfSopenharmony_ci		else
50f08c3bdfSopenharmony_ci			tst_brk(TFAIL | TTERRNO, "fsconfig(FSCONFIG_SET_PATH_EMPTY) failed");
51f08c3bdfSopenharmony_ci	}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_SET_FD, "sync", NULL, 0));
54f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
55f08c3bdfSopenharmony_ci		if (TST_ERR == EOPNOTSUPP)
56f08c3bdfSopenharmony_ci			tst_res(TCONF, "fsconfig(FSCONFIG_SET_FD) not supported");
57f08c3bdfSopenharmony_ci		else
58f08c3bdfSopenharmony_ci			tst_brk(TFAIL | TTERRNO, "fsconfig(FSCONFIG_SET_FD) failed");
59f08c3bdfSopenharmony_ci	}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	TEST(fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0));
62f08c3bdfSopenharmony_ci	if (TST_RET == -1)
63f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO, "fsconfig(FSCONFIG_CMD_CREATE) failed");
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	TEST(fsmfd = fsmount(fd, 0, 0));
66f08c3bdfSopenharmony_ci	if (fsmfd == -1)
67f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "fsmount() failed");
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	TEST(move_mount(fsmfd, "", AT_FDCWD, MNTPOINT,
70f08c3bdfSopenharmony_ci			MOVE_MOUNT_F_EMPTY_PATH));
71f08c3bdfSopenharmony_ci	SAFE_CLOSE(fsmfd);
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	if (TST_RET == -1)
74f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "move_mount() failed");
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci	if (tst_is_mounted_at_tmpdir(MNTPOINT)) {
77f08c3bdfSopenharmony_ci		SAFE_UMOUNT(MNTPOINT);
78f08c3bdfSopenharmony_ci		tst_res(TPASS, "fsconfig() passed");
79f08c3bdfSopenharmony_ci	}
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
82f08c3bdfSopenharmony_ci}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_cistatic struct tst_test test = {
85f08c3bdfSopenharmony_ci	.test_all = run,
86f08c3bdfSopenharmony_ci	.setup = fsopen_supported_by_kernel,
87f08c3bdfSopenharmony_ci	.cleanup = cleanup,
88f08c3bdfSopenharmony_ci	.needs_root = 1,
89f08c3bdfSopenharmony_ci	.format_device = 1,
90f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
91f08c3bdfSopenharmony_ci	.all_filesystems = 1,
92f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const []){"fuse", NULL},
93f08c3bdfSopenharmony_ci};
94