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