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