Home | History | Annotate | Line # | Download | only in common
fstest_nfs.c revision 1.6
      1 /*	$NetBSD: fstest_nfs.c,v 1.6 2011/02/10 16:35:01 njoly 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 struct nfstestargs *theargs;
     70 
     71 
     72 /* fork rump nfsd, configure interface */
     73 static int
     74 donewfs(const atf_tc_t *tc, void **argp,
     75 	const char *image, off_t size, void *fspriv)
     76 {
     77 	const char *srcdir;
     78 	char *nfsdargv[7];
     79 	char nfsdpath[MAXPATHLEN];
     80 	char imagepath[MAXPATHLEN];
     81 	char ethername[MAXPATHLEN], ethername_ro[MAXPATHLEN];
     82 	char ifname[IFNAMSIZ], ifname_ro[IFNAMSIZ];
     83 	char cwd[MAXPATHLEN];
     84 	struct nfstestargs *args;
     85 	pid_t childpid;
     86 	int pipes[2];
     87 	int devnull;
     88 
     89 	/*
     90 	 * First, we start the nfs service.
     91 	 */
     92 	srcdir = atf_tc_get_config_var(tc, "srcdir");
     93 	sprintf(nfsdpath, "%s/../nfs/nfsservice/rumpnfsd", srcdir);
     94 	sprintf(ethername, "/%s/%s.etherbus", getcwd(cwd, sizeof(cwd)), image);
     95 	sprintf(ethername_ro, "%s_ro", ethername);
     96 	sprintf(imagepath, "/%s/%s", cwd, image);
     97 
     98 	nfsdargv[0] = nfsdpath;
     99 	nfsdargv[1] = ethername;
    100 	nfsdargv[2] = ethername_ro;
    101 	nfsdargv[3] = __UNCONST(SERVERADDR);
    102 	nfsdargv[4] = __UNCONST(SERVERROADDR);
    103 	nfsdargv[5] = __UNCONST(NETNETMASK);
    104 	nfsdargv[6] = __UNCONST(EXPORTPATH);
    105 	nfsdargv[7] = imagepath;
    106 	nfsdargv[8] = NULL;
    107 
    108 	signal(SIGCHLD, childfail);
    109 	if (pipe(pipes) == -1)
    110 		return errno;
    111 
    112 	switch ((childpid = fork())) {
    113 	case 0:
    114 		if (chdir(dirname(nfsdpath)) == -1)
    115 			err(1, "chdir");
    116 		close(pipes[0]);
    117 		if (dup2(pipes[1], 3) == -1)
    118 			err(1, "dup2");
    119 		if (execvp(nfsdargv[0], nfsdargv) == -1)
    120 			err(1, "execvp");
    121 	case -1:
    122 		return errno;
    123 	default:
    124 		close(pipes[1]);
    125 		break;
    126 	}
    127 
    128 	/*
    129 	 * Ok, nfsd has been run.  The following sleep helps with the
    130 	 * theoretical problem that nfsd can't start fast enough to
    131 	 * process our mount request and we end up doing a timeout
    132 	 * before the mount.  This would take several seconds.  So
    133 	 * try to make sure nfsd is up&running already at this stage.
    134 	 */
    135 	if (read(pipes[0], &devnull, 4) == -1)
    136 		return errno;
    137 
    138 	/*
    139 	 * Configure our networking interface.
    140 	 */
    141 	rump_init();
    142 	netcfg_rump_makeshmif(ethername, ifname);
    143 	netcfg_rump_if(ifname, CLIENTADDR, NETNETMASK);
    144 	netcfg_rump_makeshmif(ethername_ro, ifname_ro);
    145 	netcfg_rump_if(ifname_ro, CLIENTROADDR, NETNETMASK);
    146 
    147 	/*
    148 	 * That's it.  The rest is done in mount, since we don't have
    149 	 * the mountpath available here.
    150 	 */
    151 	args = malloc(sizeof(*args));
    152 	if (args == NULL)
    153 		return errno;
    154 	memset(args, 0, sizeof(*args));
    155 	args->ta_childpid = childpid;
    156 	strcpy(args->ta_ethername, ethername);
    157 
    158 	*argp = args;
    159 	theargs = args;
    160 
    161 	return 0;
    162 }
    163 
    164 int
    165 nfs_fstest_newfs(const atf_tc_t *tc, void **argp,
    166 	const char *image, off_t size, void *fspriv)
    167 {
    168 
    169 	return donewfs(tc, argp, image, size, fspriv);
    170 }
    171 
    172 int
    173 nfsro_fstest_newfs(const atf_tc_t *tc, void **argp,
    174 	const char *image, off_t size, void *fspriv)
    175 {
    176 
    177 	return donewfs(tc, argp, image, size, fspriv);
    178 }
    179 
    180 /* mount the file system */
    181 static int
    182 domount(const atf_tc_t *tc, void *arg, const char *serverpath,
    183 	const char *path, int flags)
    184 {
    185 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
    186 	const char *nfscliargs[] = {
    187 		"nfsclient",
    188 		serverpath,
    189 		path,
    190 		NULL,
    191 	};
    192 	struct nfs_args args;
    193 	int mntflags;
    194 
    195 	if (rump_sys_mkdir(path, 0777) == -1)
    196 		return errno;
    197 
    198 	/* XXX: atf does not reset values */
    199 	optind = 1;
    200 	opterr = 1;
    201 
    202 	/*
    203 	 * We use nfs parseargs here, since as a side effect it
    204 	 * takes care of the RPC hulabaloo.
    205 	 */
    206 	mount_nfs_parseargs(__arraycount(nfscliargs)-1, __UNCONST(nfscliargs),
    207 	    &args, &mntflags, canon_dev, canon_dir);
    208 
    209 	if (rump_sys_mount(MOUNT_NFS, path, flags, &args, sizeof(args)) == -1) {
    210 		return errno;
    211 	}
    212 
    213 	return 0;
    214 }
    215 
    216 int
    217 nfs_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
    218 {
    219 
    220 	return domount(tc, arg, SERVERADDR ":" EXPORTPATH, path, flags);
    221 }
    222 
    223 /*
    224  * This is where the magic happens!
    225  *
    226  * If we are mounting r/w, do the normal thing.  However, if we are
    227  * doing a r/o mount, switch use the r/o server export address
    228  * and do a r/w mount.  This way we end up testing the r/o export policy
    229  * of the server! (yes, slightly questionable semantics, but at least
    230  * we notice very quickly if our assumption is broken in the future ;)
    231  */
    232 int
    233 nfsro_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
    234 {
    235 
    236 	if (flags & MNT_RDONLY) {
    237 		flags &= ~MNT_RDONLY;
    238 		return domount(tc, arg, SERVERROADDR":"EXPORTPATH, path, flags);
    239 	} else {
    240 		return domount(tc, arg, SERVERADDR":"EXPORTPATH, path, flags);
    241 	}
    242 }
    243 
    244 static int
    245 dodelfs(const atf_tc_t *tc, void *arg)
    246 {
    247 
    248 	/*
    249 	 * XXX: no access to "args" since we're called from "cleanup".
    250 	 * Trust atf to kill nfsd process and remove etherfile.
    251 	 */
    252 #if 0
    253 	/*
    254 	 * It's highly expected that the child will die next, so we
    255 	 * don't need that information anymore thank you very many.
    256 	 */
    257 	signal(SIGCHLD, SIG_IGN);
    258 
    259 	/*
    260 	 * Just KILL it.  Sending it SIGTERM first causes it to try
    261 	 * to send some unmount RPCs, leading to sticky situations.
    262 	 */
    263 	kill(args->ta_childpid, SIGKILL);
    264 	wait(&status);
    265 
    266 	/* remove ethernet bus */
    267 	if (unlink(args->ta_ethername) == -1)
    268 		atf_tc_fail_errno("unlink ethername");
    269 #endif
    270 
    271 	return 0;
    272 }
    273 
    274 int
    275 nfs_fstest_delfs(const atf_tc_t *tc, void *arg)
    276 {
    277 
    278 	return dodelfs(tc, arg);
    279 }
    280 
    281 int
    282 nfsro_fstest_delfs(const atf_tc_t *tc, void *arg)
    283 {
    284 
    285 	return dodelfs(tc, arg);
    286 }
    287 
    288 static int
    289 dounmount(const atf_tc_t *tc, const char *path, int flags)
    290 {
    291 	int status, i, sverrno;
    292 
    293 	/*
    294 	 * NFS handles sillyrenames in an workqueue.  Some of them might
    295 	 * be still in the queue even if all user activity has ceased.
    296 	 * We try to unmount for 2 seconds to give them a chance
    297 	 * to flush out.
    298 	 *
    299 	 * PR kern/43799
    300 	 */
    301 	for (i = 0; i < 20; i++) {
    302 		if ((status = rump_sys_unmount(path, flags)) == 0)
    303 			break;
    304 		sverrno = errno;
    305 		if (sverrno != EBUSY)
    306 			break;
    307 		usleep(100000);
    308 	}
    309 	if (status == -1)
    310 		return sverrno;
    311 
    312 	if (rump_sys_rmdir(path) == -1)
    313 		return errno;
    314 
    315 	return 0;
    316 }
    317 
    318 int
    319 nfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
    320 {
    321 
    322 	return dounmount(tc, path, flags);
    323 }
    324 
    325 int
    326 nfsro_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
    327 {
    328 
    329 	return dounmount(tc, path, flags);
    330 }
    331