Home | History | Annotate | Line # | Download | only in common
fstest_puffs.c revision 1.7
      1 /*	$NetBSD: fstest_puffs.c,v 1.7 2010/12/29 22:56:59 yamt 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 <pthread.h>
     40 #include <puffs.h>
     41 #include <puffsdump.h>
     42 #include <signal.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 
     53 #define BUFSIZE (128*1024)
     54 #define DTFS_DUMP "-o","dump"
     55 
     56 static bool mayquit = false;
     57 
     58 static ssize_t
     59 xread(int fd, void *vp, size_t n)
     60 {
     61 	size_t left;
     62 
     63 	left = n;
     64 	do {
     65 		ssize_t ssz;
     66 
     67 		ssz = read(fd, vp, left);
     68 		if (ssz == -1) {
     69 			return ssz;
     70 		}
     71 		left -= ssz;
     72 		vp = (char *)vp + ssz;
     73 	} while (left > 0);
     74 	return n;
     75 }
     76 
     77 static ssize_t
     78 xwrite(int fd, const void *vp, size_t n)
     79 {
     80 	size_t left;
     81 
     82 	left = n;
     83 	do {
     84 		ssize_t ssz;
     85 
     86 		ssz = write(fd, vp, left);
     87 		if (ssz == -1) {
     88 			return ssz;
     89 		}
     90 		left -= ssz;
     91 		vp = (const char *)vp + ssz;
     92 	} while (left > 0);
     93 	return n;
     94 }
     95 
     96 /*
     97  * Threads which shovel data between comfd and /dev/puffs.
     98  * (cannot use polling since fd's are in different namespaces)
     99  */
    100 static void *
    101 readshovel(void *arg)
    102 {
    103 	struct putter_hdr *phdr;
    104 	struct puffs_req *preq;
    105 	struct puffstestargs *args = arg;
    106 	char buf[BUFSIZE];
    107 	ssize_t n;
    108 	int comfd, puffsfd;
    109 
    110 	comfd = args->pta_servfd;
    111 	puffsfd = args->pta_rumpfd;
    112 
    113 	phdr = (void *)buf;
    114 	preq = (void *)buf;
    115 
    116 	rump_pub_lwproc_newlwp(1);
    117 
    118 	for (;;) {
    119 		n = rump_sys_read(puffsfd, buf, sizeof(*phdr));
    120 		if (n <= 0) {
    121 			fprintf(stderr, "readshovel r1 %zd / %d\n", n, errno);
    122 			break;
    123 		}
    124 
    125 		assert(phdr->pth_framelen < BUFSIZE);
    126 		n = rump_sys_read(puffsfd, buf+sizeof(*phdr),
    127 		    phdr->pth_framelen - sizeof(*phdr));
    128 		if (n <= 0) {
    129 			fprintf(stderr, "readshovel r2 %zd / %d\n", n, errno);
    130 			break;
    131 		}
    132 
    133 		/* Analyze request */
    134 		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS) {
    135 			assert(preq->preq_optype < PUFFS_VFS_MAX);
    136 			args->pta_vfs_toserv_ops[preq->preq_optype]++;
    137 		} else if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN) {
    138 			assert(preq->preq_optype < PUFFS_VN_MAX);
    139 			args->pta_vn_toserv_ops[preq->preq_optype]++;
    140 		}
    141 
    142 		n = phdr->pth_framelen;
    143 		if (xwrite(comfd, buf, n) != n) {
    144 			fprintf(stderr, "readshovel write %zd / %d\n", n, errno);
    145 			break;
    146 		}
    147 	}
    148 
    149 	if (n != 0 && mayquit == false)
    150 		abort();
    151 	return NULL;
    152 }
    153 
    154 static void *
    155 writeshovel(void *arg)
    156 {
    157 	struct puffstestargs *args = arg;
    158 	struct putter_hdr *phdr;
    159 	struct puffs_req *preq;
    160 	char buf[BUFSIZE];
    161 	size_t toread;
    162 	ssize_t n;
    163 	int comfd, puffsfd;
    164 
    165 	rump_pub_lwproc_newlwp(1);
    166 
    167 	comfd = args->pta_servfd;
    168 	puffsfd = args->pta_rumpfd;
    169 
    170 	phdr = (struct putter_hdr *)buf;
    171 	preq = (void *)buf;
    172 
    173 	for (;;) {
    174 		uint64_t off;
    175 
    176 		/*
    177 		 * Need to write everything to the "kernel" in one chunk,
    178 		 * so make sure we have it here.
    179 		 */
    180 		off = 0;
    181 		toread = sizeof(struct putter_hdr);
    182 		assert(toread < BUFSIZE);
    183 		do {
    184 			n = xread(comfd, buf+off, toread);
    185 			if (n <= 0) {
    186 				fprintf(stderr, "writeshovel read %zd / %d\n",
    187 				    n, errno);
    188 				goto out;
    189 			}
    190 			off += n;
    191 			if (off >= sizeof(struct putter_hdr))
    192 				toread = phdr->pth_framelen - off;
    193 			else
    194 				toread = off - sizeof(struct putter_hdr);
    195 		} while (toread);
    196 
    197 		if (__predict_false(
    198 		    PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS
    199 		    && preq->preq_optype == PUFFS_VFS_UNMOUNT)) {
    200 			if (preq->preq_rv == 0)
    201 				mayquit = true;
    202 		}
    203 
    204 		n = rump_sys_write(puffsfd, buf, phdr->pth_framelen);
    205 		if ((size_t)n != phdr->pth_framelen) {
    206 			fprintf(stderr, "writeshovel wr %zd / %d\n", n, errno);
    207 			break;
    208 		}
    209 	}
    210 
    211  out:
    212 	if (n != 0)
    213 		abort();
    214 	return NULL;
    215 }
    216 
    217 static void
    218 rumpshovels(struct puffstestargs *args)
    219 {
    220 	pthread_t pt;
    221 	int rv;
    222 
    223 	if ((rv = rump_init()) == -1)
    224 		err(1, "rump_init");
    225 
    226 	if (pthread_create(&pt, NULL, readshovel, args) == -1)
    227 		err(1, "read shovel");
    228 	pthread_detach(pt);
    229 
    230 	if (pthread_create(&pt, NULL, writeshovel, args) == -1)
    231 		err(1, "write shovel");
    232 	pthread_detach(pt);
    233 }
    234 
    235 static void
    236 childfail(int sign)
    237 {
    238 
    239 	atf_tc_fail("child died"); /* almost signal-safe */
    240 }
    241 
    242 struct puffstestargs *theargs; /* XXX */
    243 
    244 /* XXX: we don't support size */
    245 int
    246 puffs_fstest_newfs(const atf_tc_t *tc, void **argp,
    247 	const char *image, off_t size, void *fspriv)
    248 {
    249 	struct puffstestargs *args;
    250 	char dtfs_path[MAXPATHLEN];
    251 	char *dtfsargv[6];
    252 	char **theargv;
    253 	pid_t childpid;
    254 	int *pflags;
    255 	char comfd[16];
    256 	int sv[2];
    257 	int mntflags;
    258 	size_t len;
    259 	ssize_t n;
    260 
    261 	*argp = NULL;
    262 
    263 	args = malloc(sizeof(*args));
    264 	if (args == NULL)
    265 		return errno;
    266 
    267 	pflags = &args->pta_pflags;
    268 
    269 	/* build dtfs exec path from atf test dir */
    270 	sprintf(dtfs_path, "%s/../puffs/h_dtfs/h_dtfs",
    271 	    atf_tc_get_config_var(tc, "srcdir"));
    272 
    273 	if (fspriv) {
    274 		theargv = fspriv;
    275 		theargv[0] = dtfs_path;
    276 	} else {
    277 		dtfsargv[0] = dtfs_path;
    278 		dtfsargv[1] = __UNCONST("-i");
    279 		dtfsargv[2] = __UNCONST("-s");
    280 		dtfsargv[3] = __UNCONST("dtfs");
    281 		dtfsargv[4] = __UNCONST("fictional");
    282 		dtfsargv[5] = NULL;
    283 
    284 		theargv = dtfsargv;
    285 	}
    286 
    287 	/* Create sucketpair for communication with the real file server */
    288 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) == -1)
    289 		return errno;
    290 
    291 	signal(SIGCHLD, childfail);
    292 
    293 	switch ((childpid = fork())) {
    294 	case 0:
    295 		close(sv[1]);
    296 		snprintf(comfd, sizeof(sv[0]), "%d", sv[0]);
    297 		if (setenv("PUFFS_COMFD", comfd, 1) == -1)
    298 			return errno;
    299 
    300 		if (execvp(theargv[0], theargv) == -1)
    301 			return errno;
    302 	case -1:
    303 		return errno;
    304 	default:
    305 		close(sv[0]);
    306 		break;
    307 	}
    308 
    309 	/* read args */
    310 	if ((n = xread(sv[1], &len, sizeof(len))) != sizeof(len))
    311 		err(1, "mp 1 %zd", n);
    312 	if (len > MAXPATHLEN)
    313 		err(1, "mntpath > MAXPATHLEN");
    314 	if ((size_t)xread(sv[1], args->pta_dir, len) != len)
    315 		err(1, "mp 2");
    316 	if (xread(sv[1], &len, sizeof(len)) != sizeof(len))
    317 		err(1, "fn 1");
    318 	if (len > MAXPATHLEN)
    319 		err(1, "devpath > MAXPATHLEN");
    320 	if ((size_t)xread(sv[1], args->pta_dev, len) != len)
    321 		err(1, "fn 2");
    322 	if (xread(sv[1], &mntflags, sizeof(mntflags)) != sizeof(mntflags))
    323 		err(1, "mntflags");
    324 	if (xread(sv[1], &args->pta_pargslen, sizeof(args->pta_pargslen))
    325 	    != sizeof(args->pta_pargslen))
    326 		err(1, "puffstest_args len");
    327 	args->pta_pargs = malloc(args->pta_pargslen);
    328 	if (args->pta_pargs == NULL)
    329 		err(1, "malloc");
    330 	if (xread(sv[1], args->pta_pargs, args->pta_pargslen)
    331 	    != (ssize_t)args->pta_pargslen)
    332 		err(1, "puffstest_args");
    333 	if (xread(sv[1], pflags, sizeof(*pflags)) != sizeof(*pflags))
    334 		err(1, "pflags");
    335 
    336 	args->pta_childpid = childpid;
    337 	args->pta_servfd = sv[1];
    338 	strlcpy(args->pta_dev, image, sizeof(args->pta_dev));
    339 
    340 	*argp = theargs = args;
    341 
    342 	return 0;
    343 }
    344 
    345 int
    346 puffs_fstest_mount(const atf_tc_t *tc, void *arg, const char *path, int flags)
    347 {
    348 	struct puffstestargs *pargs = arg;
    349 	int fd;
    350 
    351 	rump_init();
    352 	fd = rump_sys_open("/dev/puffs", O_RDWR);
    353 	if (fd == -1)
    354 		return fd;
    355 
    356 #if 0
    357 	pa->pa_fd = fd;
    358 #else
    359 	assert(fd == 0); /* XXX: FIXME */
    360 #endif
    361 
    362 	if (rump_sys_mkdir(path, 0777) == -1)
    363 		return -1;
    364 
    365 	if (rump_sys_mount(MOUNT_PUFFS, path, flags,
    366 	    pargs->pta_pargs, pargs->pta_pargslen) == -1) {
    367 		/* apply "to kill a child" to avoid atf hang (kludge) */
    368 		kill(pargs->pta_childpid, SIGKILL);
    369 		return -1;
    370 	}
    371 
    372 	pargs->pta_rumpfd = fd;
    373 	rumpshovels(pargs);
    374 
    375 	return 0;
    376 }
    377 
    378 int
    379 puffs_fstest_delfs(const atf_tc_t *tc, void *arg)
    380 {
    381 
    382 	/* useless ... */
    383 	return 0;
    384 }
    385 
    386 int
    387 puffs_fstest_unmount(const atf_tc_t *tc, const char *path, int flags)
    388 {
    389 	struct puffstestargs *pargs = theargs;
    390 	int status;
    391 	int rv;
    392 
    393 	/* ok, child might exit here */
    394 	signal(SIGCHLD, SIG_IGN);
    395 
    396 	rv = rump_sys_unmount(path, flags);
    397 	if (rv)
    398 		return rv;
    399 
    400 	if ((rv = rump_sys_rmdir(path)) != 0)
    401 		return rv;
    402 
    403 	if (waitpid(pargs->pta_childpid, &status, WNOHANG) > 0)
    404 		return 0;
    405 	kill(pargs->pta_childpid, SIGTERM);
    406 	usleep(10);
    407 	if (waitpid(pargs->pta_childpid, &status, WNOHANG) > 0)
    408 		return 0;
    409 	kill(pargs->pta_childpid, SIGKILL);
    410 	usleep(500);
    411 	wait(&status);
    412 
    413 	return 0;
    414 }
    415