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