Home | History | Annotate | Line # | Download | only in common
fstest_nfs.c revision 1.5
      1  1.5  pooka /*	$NetBSD: fstest_nfs.c,v 1.5 2010/12/31 18:11:27 pooka 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.1  pooka 	args->ta_childpid = childpid;
    153  1.1  pooka 	strcpy(args->ta_ethername, ethername);
    154  1.1  pooka 
    155  1.1  pooka 	*argp = args;
    156  1.1  pooka 	theargs = args;
    157  1.1  pooka 
    158  1.1  pooka 	return 0;
    159  1.1  pooka }
    160  1.1  pooka 
    161  1.5  pooka int
    162  1.5  pooka nfs_fstest_newfs(const atf_tc_t *tc, void **argp,
    163  1.5  pooka 	const char *image, off_t size, void *fspriv)
    164  1.5  pooka {
    165  1.5  pooka 
    166  1.5  pooka 	return donewfs(tc, argp, image, size, fspriv);
    167  1.5  pooka }
    168  1.5  pooka 
    169  1.5  pooka int
    170  1.5  pooka nfsro_fstest_newfs(const atf_tc_t *tc, void **argp,
    171  1.5  pooka 	const char *image, off_t size, void *fspriv)
    172  1.5  pooka {
    173  1.5  pooka 
    174  1.5  pooka 	return donewfs(tc, argp, image, size, fspriv);
    175  1.5  pooka }
    176  1.5  pooka 
    177  1.1  pooka /* mount the file system */
    178  1.5  pooka static int
    179  1.5  pooka domount(const atf_tc_t *tc, void *arg, const char *serverpath,
    180  1.5  pooka 	const char *path, int flags)
    181  1.1  pooka {
    182  1.1  pooka 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
    183  1.1  pooka 	const char *nfscliargs[] = {
    184  1.1  pooka 		"nfsclient",
    185  1.5  pooka 		serverpath,
    186  1.1  pooka 		path,
    187  1.1  pooka 		NULL,
    188  1.1  pooka 	};
    189  1.1  pooka 	struct nfs_args args;
    190  1.1  pooka 	int mntflags;
    191  1.1  pooka 
    192  1.1  pooka 	if (rump_sys_mkdir(path, 0777) == -1)
    193  1.1  pooka 		return errno;
    194  1.1  pooka 
    195  1.1  pooka 	/* XXX: atf does not reset values */
    196  1.1  pooka 	optind = 1;
    197  1.1  pooka 	opterr = 1;
    198  1.1  pooka 
    199  1.5  pooka 	/*
    200  1.5  pooka 	 * We use nfs parseargs here, since as a side effect it
    201  1.5  pooka 	 * takes care of the RPC hulabaloo.
    202  1.5  pooka 	 */
    203  1.1  pooka 	mount_nfs_parseargs(__arraycount(nfscliargs)-1, __UNCONST(nfscliargs),
    204  1.1  pooka 	    &args, &mntflags, canon_dev, canon_dir);
    205  1.1  pooka 
    206  1.1  pooka 	if (rump_sys_mount(MOUNT_NFS, path, flags, &args, sizeof(args)) == -1) {
    207  1.1  pooka 		return errno;
    208  1.1  pooka 	}
    209  1.1  pooka 
    210  1.1  pooka 	return 0;
    211  1.1  pooka }
    212  1.1  pooka 
    213  1.1  pooka int
    214  1.5  pooka nfs_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
    215  1.5  pooka {
    216  1.5  pooka 
    217  1.5  pooka 	return domount(tc, arg, SERVERADDR ":" EXPORTPATH, path, flags);
    218  1.5  pooka }
    219  1.5  pooka 
    220  1.5  pooka /*
    221  1.5  pooka  * This is where the magic happens!
    222  1.5  pooka  *
    223  1.5  pooka  * If we are mounting r/w, do the normal thing.  However, if we are
    224  1.5  pooka  * doing a r/o mount, switch use the r/o server export address
    225  1.5  pooka  * and do a r/w mount.  This way we end up testing the r/o export policy
    226  1.5  pooka  * of the server! (yes, slightly questionable semantics, but at least
    227  1.5  pooka  * we notice very quickly if our assumption is broken in the future ;)
    228  1.5  pooka  */
    229  1.5  pooka int
    230  1.5  pooka nfsro_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
    231  1.5  pooka {
    232  1.5  pooka 
    233  1.5  pooka 	if (flags & MNT_RDONLY) {
    234  1.5  pooka 		flags &= ~MNT_RDONLY;
    235  1.5  pooka 		return domount(tc, arg, SERVERROADDR":"EXPORTPATH, path, flags);
    236  1.5  pooka 	} else {
    237  1.5  pooka 		return domount(tc, arg, SERVERADDR":"EXPORTPATH, path, flags);
    238  1.5  pooka 	}
    239  1.5  pooka }
    240  1.5  pooka 
    241  1.5  pooka static int
    242  1.5  pooka dodelfs(const atf_tc_t *tc, void *arg)
    243  1.5  pooka {
    244  1.5  pooka 
    245  1.5  pooka 	/*
    246  1.5  pooka 	 * XXX: no access to "args" since we're called from "cleanup".
    247  1.5  pooka 	 * Trust atf to kill nfsd process and remove etherfile.
    248  1.5  pooka 	 */
    249  1.5  pooka #if 0
    250  1.5  pooka 	/*
    251  1.5  pooka 	 * It's highly expected that the child will die next, so we
    252  1.5  pooka 	 * don't need that information anymore thank you very many.
    253  1.5  pooka 	 */
    254  1.5  pooka 	signal(SIGCHLD, SIG_IGN);
    255  1.5  pooka 
    256  1.5  pooka 	/*
    257  1.5  pooka 	 * Just KILL it.  Sending it SIGTERM first causes it to try
    258  1.5  pooka 	 * to send some unmount RPCs, leading to sticky situations.
    259  1.5  pooka 	 */
    260  1.5  pooka 	kill(args->ta_childpid, SIGKILL);
    261  1.5  pooka 	wait(&status);
    262  1.5  pooka 
    263  1.5  pooka 	/* remove ethernet bus */
    264  1.5  pooka 	if (unlink(args->ta_ethername) == -1)
    265  1.5  pooka 		atf_tc_fail_errno("unlink ethername");
    266  1.5  pooka #endif
    267  1.5  pooka 
    268  1.5  pooka 	return 0;
    269  1.5  pooka }
    270  1.5  pooka 
    271  1.5  pooka int
    272  1.1  pooka nfs_fstest_delfs(const atf_tc_t *tc, void *arg)
    273  1.1  pooka {
    274  1.1  pooka 
    275  1.5  pooka 	return dodelfs(tc, arg);
    276  1.1  pooka }
    277  1.1  pooka 
    278  1.1  pooka int
    279  1.5  pooka nfsro_fstest_delfs(const atf_tc_t *tc, void *arg)
    280  1.5  pooka {
    281  1.5  pooka 
    282  1.5  pooka 	return dodelfs(tc, arg);
    283  1.5  pooka }
    284  1.5  pooka 
    285  1.5  pooka static int
    286  1.5  pooka dounmount(const atf_tc_t *tc, const char *path, int flags)
    287  1.1  pooka {
    288  1.4  pooka 	int status, i, sverrno;
    289  1.1  pooka 
    290  1.4  pooka 	/*
    291  1.4  pooka 	 * NFS handles sillyrenames in an workqueue.  Some of them might
    292  1.4  pooka 	 * be still in the queue even if all user activity has ceased.
    293  1.4  pooka 	 * We try to unmount for 2 seconds to give them a chance
    294  1.4  pooka 	 * to flush out.
    295  1.4  pooka 	 *
    296  1.4  pooka 	 * PR kern/43799
    297  1.4  pooka 	 */
    298  1.4  pooka 	for (i = 0; i < 20; i++) {
    299  1.4  pooka 		if ((status = rump_sys_unmount(path, flags)) == 0)
    300  1.4  pooka 			break;
    301  1.4  pooka 		sverrno = errno;
    302  1.4  pooka 		if (sverrno != EBUSY)
    303  1.4  pooka 			break;
    304  1.4  pooka 		usleep(100000);
    305  1.1  pooka 	}
    306  1.4  pooka 	if (status == -1)
    307  1.4  pooka 		return sverrno;
    308  1.1  pooka 
    309  1.5  pooka 	if (rump_sys_rmdir(path) == -1)
    310  1.5  pooka 		return errno;
    311  1.5  pooka 
    312  1.5  pooka 	return 0;
    313  1.5  pooka }
    314  1.5  pooka 
    315  1.5  pooka int
    316  1.5  pooka nfs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
    317  1.5  pooka {
    318  1.1  pooka 
    319  1.5  pooka 	return dounmount(tc, path, flags);
    320  1.5  pooka }
    321  1.1  pooka 
    322  1.5  pooka int
    323  1.5  pooka nfsro_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
    324  1.5  pooka {
    325  1.1  pooka 
    326  1.5  pooka 	return dounmount(tc, path, flags);
    327  1.1  pooka }
    328