xref: /netbsd-src/tests/fs/common/fstest_nfs.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$NetBSD: fstest_nfs.c,v 1.11 2019/10/05 22:06:29 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/mount.h>
30 #include <sys/socket.h>
31 #include <sys/statvfs.h>
32 #include <sys/wait.h>
33 
34 #include <assert.h>
35 #include <atf-c.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <libgen.h>
40 #include <pthread.h>
41 #include <puffs.h>
42 #include <puffsdump.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <stdlib.h>
47 
48 #include <rump/rump.h>
49 #include <rump/rump_syscalls.h>
50 
51 #include "h_fsmacros.h"
52 #include "mount_nfs.h"
53 #include "../../net/config/netconfig.c"
54 
55 #define SERVERADDR "10.3.2.1"
56 #define SERVERROADDR "10.4.2.1"
57 #define CLIENTADDR "10.3.2.2"
58 #define CLIENTROADDR "10.4.2.2"
59 #define NETNETMASK "255.255.255.0"
60 #define EXPORTPATH "/myexport"
61 
62 static void
63 childfail(int status)
64 {
65 
66 	atf_tc_fail("child died");
67 }
68 
69 /* fork rump nfsd, configure interface */
70 static int
71 donewfs(const atf_tc_t *tc, void **argp,
72 	const char *image, off_t size, void *fspriv)
73 {
74 	const char *srcdir;
75 	char *nfsdargv[16];
76 	char nfsdpath[MAXPATHLEN];
77 	char imagepath[MAXPATHLEN];
78 	char ethername[MAXPATHLEN], ethername_ro[MAXPATHLEN];
79 	char ifname[IFNAMSIZ], ifname_ro[IFNAMSIZ];
80 	char cwd[MAXPATHLEN];
81 	struct nfstestargs *args;
82 	pid_t childpid;
83 	int pipes[2];
84 	int devnull;
85 
86 	/*
87 	 * First, we start the nfs service.
88 	 */
89 	srcdir = atf_tc_get_config_var(tc, "srcdir");
90 	snprintf(nfsdpath, sizeof nfsdpath,
91 	    "%s/../nfs/nfsservice/rumpnfsd", srcdir);
92 	snprintf(ethername, sizeof ethername,
93 	    "/%s/%s.etherbus", getcwd(cwd, sizeof(cwd)), image);
94 	snprintf(ethername_ro, sizeof ethername_ro, "%s_ro", ethername);
95 	snprintf(imagepath, sizeof imagepath, "/%s/%s", cwd, image);
96 
97 	nfsdargv[0] = nfsdpath;
98 	nfsdargv[1] = ethername;
99 	nfsdargv[2] = ethername_ro;
100 	nfsdargv[3] = __UNCONST(SERVERADDR);
101 	nfsdargv[4] = __UNCONST(SERVERROADDR);
102 	nfsdargv[5] = __UNCONST(NETNETMASK);
103 	nfsdargv[6] = __UNCONST(EXPORTPATH);
104 	nfsdargv[7] = imagepath;
105 	nfsdargv[8] = NULL;
106 
107 	signal(SIGCHLD, childfail);
108 	if (pipe(pipes) == -1)
109 		return errno;
110 
111 	switch ((childpid = fork())) {
112 	case 0:
113 		if (chdir(dirname(nfsdpath)) == -1)
114 			err(1, "chdir");
115 		close(pipes[0]);
116 		if (dup2(pipes[1], 3) == -1)
117 			err(1, "dup2");
118 		execvp(nfsdargv[0], nfsdargv);
119 		err(1, "execvp");
120 	case -1:
121 		return errno;
122 	default:
123 		close(pipes[1]);
124 		break;
125 	}
126 
127 	/*
128 	 * Ok, nfsd has been run.  The following sleep helps with the
129 	 * theoretical problem that nfsd can't start fast enough to
130 	 * process our mount request and we end up doing a timeout
131 	 * before the mount.  This would take several seconds.  So
132 	 * try to make sure nfsd is up&running already at this stage.
133 	 */
134 	if (read(pipes[0], &devnull, 4) == -1)
135 		return errno;
136 
137 	/*
138 	 * Configure our networking interface.
139 	 */
140 	rump_init();
141 	netcfg_rump_makeshmif(ethername, ifname);
142 	netcfg_rump_if(ifname, CLIENTADDR, NETNETMASK);
143 	netcfg_rump_makeshmif(ethername_ro, ifname_ro);
144 	netcfg_rump_if(ifname_ro, CLIENTROADDR, NETNETMASK);
145 
146 	/*
147 	 * That's it.  The rest is done in mount, since we don't have
148 	 * the mountpath available here.
149 	 */
150 	args = malloc(sizeof(*args));
151 	if (args == NULL)
152 		return errno;
153 	memset(args, 0, sizeof(*args));
154 	args->ta_childpid = childpid;
155 	strcpy(args->ta_ethername, ethername);
156 
157 	*argp = args;
158 
159 	return 0;
160 }
161 
162 int
163 nfs_fstest_newfs(const atf_tc_t *tc, void **argp,
164 	const char *image, off_t size, void *fspriv)
165 {
166 
167 	return donewfs(tc, argp, image, size, fspriv);
168 }
169 
170 int
171 nfsro_fstest_newfs(const atf_tc_t *tc, void **argp,
172 	const char *image, off_t size, void *fspriv)
173 {
174 
175 	return donewfs(tc, argp, image, size, fspriv);
176 }
177 
178 /* mount the file system */
179 static int
180 domount(const atf_tc_t *tc, void *arg, const char *serverpath,
181 	const char *path, int flags)
182 {
183 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
184 	const char *nfscliargs[] = {
185 		"nfsclient",
186 		serverpath,
187 		path,
188 		NULL,
189 	};
190 	struct nfs_args args;
191 	int mntflags;
192 
193 	if (rump_sys_mkdir(path, 0777) == -1)
194 		return errno;
195 
196 	/* XXX: atf does not reset values */
197 	optind = 1;
198 	opterr = 1;
199 
200 	/*
201 	 * We use nfs parseargs here, since as a side effect it
202 	 * takes care of the RPC hulabaloo.
203 	 */
204 	mount_nfs_parseargs(__arraycount(nfscliargs)-1, __UNCONST(nfscliargs),
205 	    &args, &mntflags, canon_dev, canon_dir);
206 
207 	if (rump_sys_mount(MOUNT_NFS, path, flags, &args, sizeof(args)) == -1) {
208 		return errno;
209 	}
210 
211 	return 0;
212 }
213 
214 int
215 nfs_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
216 {
217 
218 	return domount(tc, arg, SERVERADDR ":" EXPORTPATH, path, flags);
219 }
220 
221 /*
222  * This is where the magic happens!
223  *
224  * If we are mounting r/w, do the normal thing.  However, if we are
225  * doing a r/o mount, switch use the r/o server export address
226  * and do a r/w mount.  This way we end up testing the r/o export policy
227  * of the server! (yes, slightly questionable semantics, but at least
228  * we notice very quickly if our assumption is broken in the future ;)
229  */
230 int
231 nfsro_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
232 {
233 
234 	if (flags & MNT_RDONLY) {
235 		flags &= ~MNT_RDONLY;
236 		return domount(tc, arg, SERVERROADDR":"EXPORTPATH, path, flags);
237 	} else {
238 		return domount(tc, arg, SERVERADDR":"EXPORTPATH, path, flags);
239 	}
240 }
241 
242 static int
243 dodelfs(const atf_tc_t *tc, void *arg)
244 {
245 
246 	/*
247 	 * XXX: no access to "args" since we're called from "cleanup".
248 	 * Trust atf to kill nfsd process and remove etherfile.
249 	 */
250 #if 0
251 	/*
252 	 * It's highly expected that the child will die next, so we
253 	 * don't need that information anymore thank you very many.
254 	 */
255 	signal(SIGCHLD, SIG_IGN);
256 
257 	/*
258 	 * Just KILL it.  Sending it SIGTERM first causes it to try
259 	 * to send some unmount RPCs, leading to sticky situations.
260 	 */
261 	kill(args->ta_childpid, SIGKILL);
262 	wait(&status);
263 
264 	/* remove ethernet bus */
265 	if (unlink(args->ta_ethername) == -1)
266 		atf_tc_fail_errno("unlink ethername");
267 #endif
268 
269 	return 0;
270 }
271 
272 int
273 nfs_fstest_delfs(const atf_tc_t *tc, void *arg)
274 {
275 
276 	return dodelfs(tc, arg);
277 }
278 
279 int
280 nfsro_fstest_delfs(const atf_tc_t *tc, void *arg)
281 {
282 
283 	return dodelfs(tc, arg);
284 }
285 
286 static int
287 dounmount(const atf_tc_t *tc, const char *path, int flags)
288 {
289 	int status, i, sverrno;
290 
291 	/*
292 	 * NFS handles sillyrenames in an workqueue.  Some of them might
293 	 * be still in the queue even if all user activity has ceased.
294 	 * We try to unmount for 2 seconds to give them a chance
295 	 * to flush out.
296 	 *
297 	 * PR kern/43799
298 	 */
299 	for (i = 0; i < 20; i++) {
300 		if ((status = rump_sys_unmount(path, flags)) == 0)
301 			break;
302 		sverrno = errno;
303 		if (sverrno != EBUSY)
304 			break;
305 		usleep(100000);
306 	}
307 	if (status == -1)
308 		return sverrno;
309 
310 	if (rump_sys_rmdir(path) == -1)
311 		return errno;
312 
313 	return 0;
314 }
315 
316 int
317 nfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
318 {
319 
320 	return dounmount(tc, path, flags);
321 }
322 
323 int
324 nfsro_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
325 {
326 
327 	return dounmount(tc, path, flags);
328 }
329