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