1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2020 Red Hat, Inc.  All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Zorro Lang <zlang@redhat.com>
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * Basic fsmount() test.
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci#include "tst_test.h"
10f08c3bdfSopenharmony_ci#include "lapi/fsmount.h"
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#define MNTPOINT	"mntpoint"
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#define TCASE_ENTRY(_flags, _attrs)	{.name = "Flag " #_flags ", Attr " #_attrs, .flags = _flags, .attrs = _attrs}
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic struct tcase {
17f08c3bdfSopenharmony_ci	char *name;
18f08c3bdfSopenharmony_ci	unsigned int flags;
19f08c3bdfSopenharmony_ci	unsigned int attrs;
20f08c3bdfSopenharmony_ci} tcases[] = {
21f08c3bdfSopenharmony_ci	TCASE_ENTRY(0, MOUNT_ATTR_RDONLY),
22f08c3bdfSopenharmony_ci	TCASE_ENTRY(0, MOUNT_ATTR_NOSUID),
23f08c3bdfSopenharmony_ci	TCASE_ENTRY(0, MOUNT_ATTR_NODEV),
24f08c3bdfSopenharmony_ci	TCASE_ENTRY(0, MOUNT_ATTR_NOEXEC),
25f08c3bdfSopenharmony_ci	TCASE_ENTRY(0, MOUNT_ATTR_RELATIME),
26f08c3bdfSopenharmony_ci	TCASE_ENTRY(0, MOUNT_ATTR_NOATIME),
27f08c3bdfSopenharmony_ci	TCASE_ENTRY(0, MOUNT_ATTR_STRICTATIME),
28f08c3bdfSopenharmony_ci	TCASE_ENTRY(0, MOUNT_ATTR_NODIRATIME),
29f08c3bdfSopenharmony_ci	TCASE_ENTRY(FSMOUNT_CLOEXEC, MOUNT_ATTR_RDONLY),
30f08c3bdfSopenharmony_ci	TCASE_ENTRY(FSMOUNT_CLOEXEC, MOUNT_ATTR_NOSUID),
31f08c3bdfSopenharmony_ci	TCASE_ENTRY(FSMOUNT_CLOEXEC, MOUNT_ATTR_NODEV),
32f08c3bdfSopenharmony_ci	TCASE_ENTRY(FSMOUNT_CLOEXEC, MOUNT_ATTR_NOEXEC),
33f08c3bdfSopenharmony_ci	TCASE_ENTRY(FSMOUNT_CLOEXEC, MOUNT_ATTR_RELATIME),
34f08c3bdfSopenharmony_ci	TCASE_ENTRY(FSMOUNT_CLOEXEC, MOUNT_ATTR_NOATIME),
35f08c3bdfSopenharmony_ci	TCASE_ENTRY(FSMOUNT_CLOEXEC, MOUNT_ATTR_STRICTATIME),
36f08c3bdfSopenharmony_ci	TCASE_ENTRY(FSMOUNT_CLOEXEC, MOUNT_ATTR_NODIRATIME),
37f08c3bdfSopenharmony_ci};
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void run(unsigned int n)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
42f08c3bdfSopenharmony_ci	int sfd, mfd;
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	TEST(sfd = fsopen(tst_device->fs_type, FSOPEN_CLOEXEC));
45f08c3bdfSopenharmony_ci	if (sfd == -1) {
46f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "fsopen() on %s failed",
47f08c3bdfSopenharmony_ci			tst_device->fs_type);
48f08c3bdfSopenharmony_ci		return;
49f08c3bdfSopenharmony_ci	}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	TEST(fsconfig(sfd, FSCONFIG_SET_STRING, "source", tst_device->dev, 0));
52f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
53f08c3bdfSopenharmony_ci		SAFE_CLOSE(sfd);
54f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
55f08c3bdfSopenharmony_ci			"fsconfig(FSCONFIG_SET_STRING) failed to set source to %s", tst_device->dev);
56f08c3bdfSopenharmony_ci		return;
57f08c3bdfSopenharmony_ci	}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	TEST(fsconfig(sfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0));
60f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
61f08c3bdfSopenharmony_ci		SAFE_CLOSE(sfd);
62f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "fsconfig(FSCONFIG_CMD_CREATE) failed");
63f08c3bdfSopenharmony_ci		return;
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	TEST(mfd = fsmount(sfd, tc->flags, tc->attrs));
67f08c3bdfSopenharmony_ci	SAFE_CLOSE(sfd);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	if (mfd == -1) {
70f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
71f08c3bdfSopenharmony_ci			"fsmount() failed to create a mount object");
72f08c3bdfSopenharmony_ci		return;
73f08c3bdfSopenharmony_ci	}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	TEST(move_mount(mfd, "", AT_FDCWD, MNTPOINT, MOVE_MOUNT_F_EMPTY_PATH));
76f08c3bdfSopenharmony_ci	SAFE_CLOSE(mfd);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
79f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
80f08c3bdfSopenharmony_ci			"move_mount() failed to attach to the mount point");
81f08c3bdfSopenharmony_ci		return;
82f08c3bdfSopenharmony_ci	}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	if (tst_is_mounted_at_tmpdir(MNTPOINT)) {
85f08c3bdfSopenharmony_ci		SAFE_UMOUNT(MNTPOINT);
86f08c3bdfSopenharmony_ci		tst_res(TPASS, "%s: fsmount() passed", tc->name);
87f08c3bdfSopenharmony_ci	}
88f08c3bdfSopenharmony_ci}
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_cistatic struct tst_test test = {
91f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
92f08c3bdfSopenharmony_ci	.test = run,
93f08c3bdfSopenharmony_ci	.setup = fsopen_supported_by_kernel,
94f08c3bdfSopenharmony_ci	.needs_root = 1,
95f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
96f08c3bdfSopenharmony_ci	.format_device = 1,
97f08c3bdfSopenharmony_ci	.all_filesystems = 1,
98f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const []){"fuse", NULL},
99f08c3bdfSopenharmony_ci};
100