xref: /freebsd/contrib/netbsd-tests/fs/vfs/t_rwtoro.c (revision 61e21613)
1 /*	$NetBSD: t_rwtoro.c,v 1.1 2017/01/27 10:45:11 hannken Exp $	*/
2 
3 /*-
4  * Copyright (c) 2017 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/mount.h>
31 #include <sys/stat.h>
32 #include <sys/statvfs.h>
33 
34 #include <atf-c.h>
35 #include <fcntl.h>
36 #include <libgen.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 
40 #include <rump/rump_syscalls.h>
41 #include <rump/rump.h>
42 
43 #include <miscfs/nullfs/null.h>
44 #include <fs/tmpfs/tmpfs_args.h>
45 
46 #include "../common/h_fsmacros.h"
47 #include "../../h_macros.h"
48 
49 static const char *unsupported = "fs does not support r/o remount";
50 static char file_path[MAXPATHLEN];
51 static int file_fd;
52 
53 /*
54  * Remount the filesystem read-only and test errno.
55  * Skip filesystems that don't implement read-write -> read-only.
56  */
57 static void
58 remount_ro(const atf_tc_t *tc, const char *mp, int expected_errno)
59 {
60 	int error;
61 	union {
62 		struct tmpfs_args tmpfs;
63 		char data[4095];
64 	} mount_args;
65 	int mount_args_length;
66 	struct statvfs sbuf;
67 
68 	if (FSTYPE_ZFS(tc))
69 		atf_tc_skip("%s", unsupported);
70 
71 	/* Prepare mount arguments. */
72 	RL(rump_sys_statvfs1(mp, &sbuf, ST_WAIT));
73 	mount_args_length = sizeof(mount_args);
74 	memset(&mount_args, 0, mount_args_length);
75 	if (FSTYPE_TMPFS(tc))
76 		mount_args.tmpfs.ta_version = TMPFS_ARGS_VERSION;
77 	mount_args_length = rump_sys_mount(sbuf.f_fstypename, mp, MNT_GETARGS,
78 	    &mount_args, mount_args_length);
79 	ATF_CHECK(mount_args_length >= 0);
80 
81 	/* Remount and test result. */
82 	error = rump_sys_mount(sbuf.f_fstypename, mp, MNT_UPDATE | MNT_RDONLY,
83 	    &mount_args, mount_args_length);
84 	if (errno == EOPNOTSUPP)
85 		atf_tc_skip("%s", unsupported);
86 	if (expected_errno == 0)
87 		ATF_CHECK(error == 0);
88 	else
89 		ATF_CHECK_ERRNO(expected_errno, error == -1);
90 }
91 
92 static void
93 open_file_ro(const char *prefix)
94 {
95 
96 	snprintf(file_path, sizeof(file_path), "%s/file", prefix);
97 	RL(file_fd = rump_sys_open(file_path, O_CREAT | O_RDWR, 0777));
98 	RL(rump_sys_close(file_fd));
99 	RL(file_fd = rump_sys_open(file_path, O_RDONLY));
100 }
101 
102 static void
103 open_file_ro_unlink(const char *prefix)
104 {
105 
106 	snprintf(file_path, sizeof(file_path), "%s/file", prefix);
107 	RL(file_fd = rump_sys_open(file_path, O_CREAT | O_RDWR, 0777));
108 	RL(rump_sys_close(file_fd));
109 	RL(file_fd = rump_sys_open(file_path, O_RDONLY));
110 	RL(rump_sys_unlink(file_path));
111 }
112 
113 static void
114 open_file_rw(const char *prefix)
115 {
116 
117 	snprintf(file_path, sizeof(file_path), "%s/file", prefix);
118 	RL(file_fd = rump_sys_open(file_path, O_CREAT | O_RDWR, 0777));
119 }
120 
121 static void
122 close_file(const char *unused)
123 {
124 
125 	RL(rump_sys_close(file_fd));
126 }
127 
128 static void
129 basic_test(const atf_tc_t *tc, const char *mp, int expected_errno,
130     bool use_layer, void (*pre)(const char *), void (*post)(const char *))
131 {
132 	const char *null_mount = "/nullm";
133 	struct null_args nargs;
134 
135 	if (use_layer) {
136 		RL(rump_sys_mkdir(null_mount, 0777));
137 		memset(&nargs, 0, sizeof(nargs));
138 		nargs.nulla_target = __UNCONST(mp);;
139 		RL(rump_sys_mount(MOUNT_NULL, null_mount, 0,
140 		    &nargs, sizeof(nargs)));
141 	}
142 	if (pre)
143 		(*pre)(use_layer ? null_mount : mp);
144 	remount_ro(tc, mp, expected_errno);
145 	if (post)
146 		(*post)(use_layer ? null_mount : mp);
147 	if (use_layer)
148 		RL(rump_sys_unmount(null_mount, 0));
149 }
150 
151 static void
152 noneopen(const atf_tc_t *tc, const char *mp)
153 {
154 
155 	basic_test(tc, mp, 0, false, NULL, NULL);
156 }
157 
158 static void
159 readopen(const atf_tc_t *tc, const char *mp)
160 {
161 
162 	basic_test(tc, mp, 0, false, open_file_ro, close_file);
163 }
164 
165 static void
166 writeopen(const atf_tc_t *tc, const char *mp)
167 {
168 
169 	basic_test(tc, mp, EBUSY, false, open_file_rw, close_file);
170 }
171 
172 static void
173 read_unlinked(const atf_tc_t *tc, const char *mp)
174 {
175 
176 	basic_test(tc, mp, EBUSY, false, open_file_ro_unlink, close_file);
177 }
178 
179 static void
180 layer_noneopen(const atf_tc_t *tc, const char *mp)
181 {
182 
183 	basic_test(tc, mp, 0, true, NULL, NULL);
184 }
185 
186 static void
187 layer_readopen(const atf_tc_t *tc, const char *mp)
188 {
189 
190 	basic_test(tc, mp, 0, true, open_file_ro, close_file);
191 }
192 
193 static void
194 layer_writeopen(const atf_tc_t *tc, const char *mp)
195 {
196 
197 	basic_test(tc, mp, EBUSY, true, open_file_rw, close_file);
198 }
199 
200 static void
201 layer_read_unlinked(const atf_tc_t *tc, const char *mp)
202 {
203 
204 	basic_test(tc, mp, EBUSY, true, open_file_ro_unlink, close_file);
205 }
206 
207 ATF_TC_FSAPPLY(noneopen, "remount r/o with no file open");
208 ATF_TC_FSAPPLY(readopen, "remount r/o with file open for reading");
209 ATF_TC_FSAPPLY(writeopen, "remount r/o with file open for writing");
210 ATF_TC_FSAPPLY(read_unlinked,
211     "remount r/o with unlinked file open for reading");
212 ATF_TC_FSAPPLY(layer_noneopen, "remount r/o with no file open on layer");
213 ATF_TC_FSAPPLY(layer_readopen,
214     "remount r/o with file open for reading on layer");
215 ATF_TC_FSAPPLY(layer_writeopen,
216     "remount r/o with file open for writing on layer");
217 ATF_TC_FSAPPLY(layer_read_unlinked,
218     "remount r/o with unlinked file open for reading on layer");
219 
220 ATF_TP_ADD_TCS(tp)
221 {
222 
223 	ATF_TP_FSAPPLY(noneopen);
224 	ATF_TP_FSAPPLY(readopen);
225 	ATF_TP_FSAPPLY(writeopen);
226 	ATF_TP_FSAPPLY(read_unlinked);
227 	ATF_TP_FSAPPLY(layer_noneopen);
228 	ATF_TP_FSAPPLY(layer_readopen);
229 	ATF_TP_FSAPPLY(layer_writeopen);
230 	ATF_TP_FSAPPLY(layer_read_unlinked);
231 
232 	return atf_no_error();
233 }
234