xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mount/mount02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4  *               Nirmala Devi Dhanasekar <[email protected]>
5  * Copyright (c) Linux Test Project, 2005-2023
6  * Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Check for basic errors returned by mount(2) system call.
13  *
14  * - ENODEV if filesystem type not configured
15  * - ENOTBLK if specialfile is not a block device
16  * - EBUSY if specialfile is already mounted or it  cannot  be remounted
17  *   read-only, because it still holds files open for writing.
18  * - EINVAL if specialfile or device is invalid or a remount was attempted,
19  *   while source was not already mounted on target.
20  * - EFAULT if special file or device file points to invalid address space.
21  * - ENAMETOOLONG if pathname was longer than MAXPATHLEN.
22  * - ENOENT if pathname was empty or has a nonexistent component.
23  * - ENOTDIR if not a directory.
24  */
25 
26 #include "tst_test.h"
27 #include <sys/mount.h>
28 
29 #define MNTPOINT "mntpoint"
30 #define TEST_FILE MNTPOINT"/file"
31 
32 static char path[PATH_MAX + 2];
33 static const char *long_path = path;
34 static const char *device;
35 static const char *fs_type;
36 static const char *null;
37 static const char *wrong_fs_type = "error";
38 static const char *mntpoint = MNTPOINT;
39 static const char *fault;
40 static const char *nonexistent = "nonexistent";
41 static const char *char_dev = "char_device";
42 static const char *file = "filename";
43 static int fd;
44 
45 static void pre_mount(void);
46 static void post_umount(void);
47 static void pre_create_file(void);
48 static void post_delete_file(void);
49 
50 static struct test_case {
51 	const char **device;
52 	const char **mntpoint;
53 	const char **fs_type;
54 	const char *desc;
55 	unsigned long flag;
56 	int exp_errno;
57 	void (*setup)(void);
58 	void (*cleanup)(void);
59 } test_cases[] = {
60 	{.fs_type = &wrong_fs_type, .desc = "wrong FS type", .exp_errno = ENODEV},
61 	{.device = &char_dev, .desc = "char device", .exp_errno = ENOTBLK},
62 	{.desc = "mounted folder", .exp_errno = EBUSY, .setup = pre_mount, .cleanup = post_umount},
63 	{.desc = "mounted folder containing file", .flag = MS_REMOUNT | MS_RDONLY,
64 		.exp_errno = EBUSY, .setup = pre_create_file, .cleanup = post_delete_file},
65 	{.device = &null, .desc = "invalid device", .exp_errno = EINVAL},
66 	{.fs_type = &null, .desc = "invalid device type", .exp_errno = EINVAL},
67 	{.desc = "mounted folder", .flag = MS_REMOUNT, .exp_errno = EINVAL},
68 	{.device = &fault, .desc = "fault device", .exp_errno = EFAULT},
69 	{.fs_type = &fault, .desc = "fault device type", .exp_errno = EFAULT},
70 	{.mntpoint = &long_path, .desc = "long name", .exp_errno = ENAMETOOLONG},
71 	{.mntpoint = &nonexistent, .desc = "non existant folder", .exp_errno = ENOENT},
72 	{.device = &device, .mntpoint = &file, .desc = "file", .exp_errno = ENOTDIR},
73 };
74 
pre_mount(void)75 static void pre_mount(void)
76 {
77 	SAFE_MOUNT(device, mntpoint, fs_type, 0, NULL);
78 }
79 
post_umount(void)80 static void post_umount(void)
81 {
82 	if (tst_is_mounted(MNTPOINT))
83 		SAFE_UMOUNT(MNTPOINT);
84 }
85 
pre_create_file(void)86 static void pre_create_file(void)
87 {
88 	pre_mount();
89 	fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR, 0700);
90 }
91 
post_delete_file(void)92 static void post_delete_file(void)
93 {
94 	SAFE_CLOSE(fd);
95 	post_umount();
96 }
97 
setup(void)98 static void setup(void)
99 {
100 	fault = tst_get_bad_addr(NULL);
101 
102 	device = tst_device->dev;
103 	fs_type = tst_device->fs_type;
104 
105 	memset(path, 'a', PATH_MAX + 1);
106 
107 	SAFE_MKNOD(char_dev, S_IFCHR | 0777, 0);
108 	SAFE_TOUCH(file, 0777, 0);
109 }
110 
cleanup(void)111 static void cleanup(void)
112 {
113 	if (tst_is_mounted(MNTPOINT))
114 		SAFE_UMOUNT(MNTPOINT);
115 }
116 
run(unsigned int i)117 static void run(unsigned int i)
118 {
119 	struct test_case *tc = &test_cases[i];
120 
121 	if (!tc->device)
122 		tc->device = &device;
123 
124 	if (!tc->mntpoint)
125 		tc->mntpoint = &mntpoint;
126 
127 	if (!tc->fs_type)
128 		tc->fs_type = &fs_type;
129 
130 	if (tc->setup)
131 		tc->setup();
132 
133 	TST_EXP_FAIL(mount(*tc->device, *tc->mntpoint, *tc->fs_type, tc->flag, NULL),
134 		tc->exp_errno,
135 		"mounting %s",
136 		tc->desc);
137 
138 	if (tc->cleanup)
139 		tc->cleanup();
140 }
141 
142 static struct tst_test test = {
143 	.tcnt = ARRAY_SIZE(test_cases),
144 	.test = run,
145 	.setup = setup,
146 	.cleanup = cleanup,
147 	.needs_root = 1,
148 	.format_device = 1,
149 	.mntpoint = MNTPOINT,
150 };
151