Home | History | Annotate | Line # | Download | only in libpuffs
puffs.c revision 1.109
      1 /*	$NetBSD: puffs.c,v 1.109 2010/06/06 23:11:05 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8  * The Google SoC project was mentored by Bill Studenmund.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if !defined(lint)
     34 __RCSID("$NetBSD: puffs.c,v 1.109 2010/06/06 23:11:05 pooka Exp $");
     35 #endif /* !lint */
     36 
     37 #include <sys/param.h>
     38 #include <sys/mount.h>
     39 
     40 #include <assert.h>
     41 #include <err.h>
     42 #include <errno.h>
     43 #include <fcntl.h>
     44 #include <mntopts.h>
     45 #include <paths.h>
     46 #include <puffs.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <syslog.h>
     51 #include <unistd.h>
     52 
     53 #include "puffs_priv.h"
     54 
     55 /* Most file systems want this for opts, so just give it to them */
     56 const struct mntopt puffsmopts[] = {
     57 	MOPT_STDOPTS,
     58 	PUFFSMOPT_STD,
     59 	MOPT_NULL,
     60 };
     61 
     62 #ifdef PUFFS_WITH_THREADS
     63 #include <pthread.h>
     64 pthread_mutex_t pu_lock = PTHREAD_MUTEX_INITIALIZER;
     65 #endif
     66 
     67 #define FILLOP(lower, upper)						\
     68 do {									\
     69 	if (pops->puffs_node_##lower)					\
     70 		opmask[PUFFS_VN_##upper] = 1;				\
     71 } while (/*CONSTCOND*/0)
     72 static void
     73 fillvnopmask(struct puffs_ops *pops, struct puffs_kargs *pa)
     74 {
     75 	uint8_t *opmask = pa->pa_vnopmask;
     76 
     77 	memset(opmask, 0, sizeof(pa->pa_vnopmask));
     78 
     79 	FILLOP(create,   CREATE);
     80 	FILLOP(mknod,    MKNOD);
     81 	FILLOP(open,     OPEN);
     82 	FILLOP(close,    CLOSE);
     83 	FILLOP(access,   ACCESS);
     84 	FILLOP(getattr,  GETATTR);
     85 	FILLOP(setattr,  SETATTR);
     86 	FILLOP(poll,     POLL);
     87 	FILLOP(mmap,     MMAP);
     88 	FILLOP(fsync,    FSYNC);
     89 	FILLOP(seek,     SEEK);
     90 	FILLOP(remove,   REMOVE);
     91 	FILLOP(link,     LINK);
     92 	FILLOP(rename,   RENAME);
     93 	FILLOP(mkdir,    MKDIR);
     94 	FILLOP(rmdir,    RMDIR);
     95 	FILLOP(symlink,  SYMLINK);
     96 	FILLOP(readdir,  READDIR);
     97 	FILLOP(readlink, READLINK);
     98 	FILLOP(reclaim,  RECLAIM);
     99 	FILLOP(inactive, INACTIVE);
    100 	FILLOP(print,    PRINT);
    101 	FILLOP(read,     READ);
    102 	FILLOP(write,    WRITE);
    103 	FILLOP(abortop,  ABORTOP);
    104 #if 0
    105 	/* make pvnr_retval container sensible */
    106 	FILLOP(pathconf, PATHCONF);
    107 #endif
    108 
    109 	FILLOP(getextattr,  GETEXTATTR);
    110 	FILLOP(setextattr,  SETEXTATTR);
    111 	FILLOP(listextattr, LISTEXTATTR);
    112 	FILLOP(deleteextattr, DELETEEXTATTR);
    113 }
    114 #undef FILLOP
    115 
    116 /*
    117  * Go over all framev entries and write everything we can.  This is
    118  * mostly for the benefit of delivering "unmount" to the kernel.
    119  */
    120 static void
    121 finalpush(struct puffs_usermount *pu)
    122 {
    123 	struct puffs_fctrl_io *fio;
    124 
    125 	LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
    126 		if (fio->stat & FIO_WRGONE)
    127 			continue;
    128 
    129 		puffs__framev_output(pu, fio->fctrl, fio);
    130 	}
    131 }
    132 
    133 /*ARGSUSED*/
    134 void
    135 puffs_kernerr_abort(struct puffs_usermount *pu, uint8_t type,
    136 	int error, const char *str, puffs_cookie_t cookie)
    137 {
    138 
    139 	fprintf(stderr, "abort: type %d, error %d, cookie %p (%s)\n",
    140 	    type, error, cookie, str);
    141 	abort();
    142 }
    143 
    144 /*ARGSUSED*/
    145 void
    146 puffs_kernerr_log(struct puffs_usermount *pu, uint8_t type,
    147 	int error, const char *str, puffs_cookie_t cookie)
    148 {
    149 
    150 	syslog(LOG_WARNING, "kernel: type %d, error %d, cookie %p (%s)\n",
    151 	    type, error, cookie, str);
    152 }
    153 
    154 int
    155 puffs_getselectable(struct puffs_usermount *pu)
    156 {
    157 
    158 	return pu->pu_fd;
    159 }
    160 
    161 uint64_t
    162 puffs__nextreq(struct puffs_usermount *pu)
    163 {
    164 	uint64_t rv;
    165 
    166 	PU_LOCK();
    167 	rv = pu->pu_nextreq++ | (uint64_t)1<<63;
    168 	PU_UNLOCK();
    169 
    170 	return rv;
    171 }
    172 
    173 int
    174 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
    175 {
    176 	int rv, x;
    177 
    178 	assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
    179 
    180 	if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
    181 		errno = EINVAL;
    182 		return -1;
    183 	}
    184 
    185 	x = mode;
    186 	rv = ioctl(pu->pu_fd, FIONBIO, &x);
    187 
    188 	if (rv == 0) {
    189 		if (mode == PUFFSDEV_BLOCK)
    190 			pu->pu_state &= ~PU_ASYNCFD;
    191 		else
    192 			pu->pu_state |= PU_ASYNCFD;
    193 	}
    194 
    195 	return rv;
    196 }
    197 
    198 int
    199 puffs_getstate(struct puffs_usermount *pu)
    200 {
    201 
    202 	return pu->pu_state & PU_STATEMASK;
    203 }
    204 
    205 void
    206 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
    207 {
    208 	long psize, minsize;
    209 	int stackshift;
    210 	int bonus;
    211 
    212 	assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
    213 
    214 	psize = sysconf(_SC_PAGESIZE);
    215 	minsize = 4*psize;
    216 	if (ss < (size_t)minsize || ss == PUFFS_STACKSIZE_MIN) {
    217 		if (ss != PUFFS_STACKSIZE_MIN)
    218 			fprintf(stderr, "puffs_setstacksize: adjusting "
    219 			    "stacksize to minimum %ld\n", minsize);
    220 		ss = 4*psize;
    221 	}
    222 
    223 	stackshift = -1;
    224 	bonus = 0;
    225 	while (ss) {
    226 		if (ss & 0x1)
    227 			bonus++;
    228 		ss >>= 1;
    229 		stackshift++;
    230 	}
    231 	if (bonus > 1) {
    232 		stackshift++;
    233 		fprintf(stderr, "puffs_setstacksize: using next power of two: "
    234 		    "%d\n", 1<<stackshift);
    235 	}
    236 
    237 	pu->pu_cc_stackshift = stackshift;
    238 }
    239 
    240 struct puffs_pathobj *
    241 puffs_getrootpathobj(struct puffs_usermount *pu)
    242 {
    243 	struct puffs_node *pnr;
    244 
    245 	pnr = pu->pu_pn_root;
    246 	if (pnr == NULL) {
    247 		errno = ENOENT;
    248 		return NULL;
    249 	}
    250 
    251 	return &pnr->pn_po;
    252 }
    253 
    254 void
    255 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
    256 {
    257 
    258 	pu->pu_pn_root = pn;
    259 }
    260 
    261 struct puffs_node *
    262 puffs_getroot(struct puffs_usermount *pu)
    263 {
    264 
    265 	return pu->pu_pn_root;
    266 }
    267 
    268 void
    269 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
    270 	vsize_t vsize, dev_t rdev)
    271 {
    272 	struct puffs_kargs *pargs = pu->pu_kargp;
    273 
    274 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
    275 		warnx("puffs_setrootinfo: call has effect only "
    276 		    "before mount\n");
    277 		return;
    278 	}
    279 
    280 	pargs->pa_root_vtype = vt;
    281 	pargs->pa_root_vsize = vsize;
    282 	pargs->pa_root_rdev = rdev;
    283 }
    284 
    285 void *
    286 puffs_getspecific(struct puffs_usermount *pu)
    287 {
    288 
    289 	return pu->pu_privdata;
    290 }
    291 
    292 void
    293 puffs_setspecific(struct puffs_usermount *pu, void *privdata)
    294 {
    295 
    296 	pu->pu_privdata = privdata;
    297 }
    298 
    299 void
    300 puffs_setmntinfo(struct puffs_usermount *pu,
    301 	const char *mntfromname, const char *puffsname)
    302 {
    303 	struct puffs_kargs *pargs = pu->pu_kargp;
    304 
    305 	(void)strlcpy(pargs->pa_mntfromname, mntfromname,
    306 	    sizeof(pargs->pa_mntfromname));
    307 	(void)strlcpy(pargs->pa_typename, puffsname,
    308 	    sizeof(pargs->pa_typename));
    309 }
    310 
    311 size_t
    312 puffs_getmaxreqlen(struct puffs_usermount *pu)
    313 {
    314 
    315 	return pu->pu_maxreqlen;
    316 }
    317 
    318 void
    319 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
    320 {
    321 
    322 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    323 		warnx("puffs_setmaxreqlen: call has effect only "
    324 		    "before mount\n");
    325 
    326 	pu->pu_kargp->pa_maxmsglen = reqlen;
    327 }
    328 
    329 void
    330 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
    331 {
    332 
    333 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    334 		warnx("puffs_setfhsize: call has effect only before mount\n");
    335 
    336 	pu->pu_kargp->pa_fhsize = fhsize;
    337 	pu->pu_kargp->pa_fhflags = flags;
    338 }
    339 
    340 void
    341 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
    342 {
    343 
    344 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    345 		warnx("puffs_setfhsize: call has effect only before mount\n");
    346 
    347 	pu->pu_kargp->pa_nhashbuckets = nhash;
    348 }
    349 
    350 void
    351 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
    352 {
    353 
    354 	pu->pu_pathbuild = fn;
    355 }
    356 
    357 void
    358 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
    359 {
    360 
    361 	pu->pu_pathtransform = fn;
    362 }
    363 
    364 void
    365 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
    366 {
    367 
    368 	pu->pu_pathcmp = fn;
    369 }
    370 
    371 void
    372 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
    373 {
    374 
    375 	pu->pu_pathfree = fn;
    376 }
    377 
    378 void
    379 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
    380 {
    381 
    382 	pu->pu_namemod = fn;
    383 }
    384 
    385 void
    386 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
    387 {
    388 
    389 	pu->pu_errnotify = fn;
    390 }
    391 
    392 void
    393 puffs_set_cmap(struct puffs_usermount *pu, pu_cmap_fn fn)
    394 {
    395 
    396 	pu->pu_cmap = fn;
    397 }
    398 
    399 void
    400 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
    401 {
    402 
    403 	pu->pu_ml_lfn = lfn;
    404 }
    405 
    406 void
    407 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
    408 {
    409 
    410 	if (ts == NULL) {
    411 		pu->pu_ml_timep = NULL;
    412 	} else {
    413 		pu->pu_ml_timeout = *ts;
    414 		pu->pu_ml_timep = &pu->pu_ml_timeout;
    415 	}
    416 }
    417 
    418 void
    419 puffs_set_prepost(struct puffs_usermount *pu,
    420 	pu_prepost_fn pre, pu_prepost_fn pst)
    421 {
    422 
    423 	pu->pu_oppre = pre;
    424 	pu->pu_oppost = pst;
    425 }
    426 
    427 void
    428 puffs_setback(struct puffs_cc *pcc, int whatback)
    429 {
    430 	struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
    431 
    432 	assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
    433 	    preq->preq_optype == PUFFS_VN_OPEN ||
    434 	    preq->preq_optype == PUFFS_VN_MMAP ||
    435 	    preq->preq_optype == PUFFS_VN_REMOVE ||
    436 	    preq->preq_optype == PUFFS_VN_RMDIR ||
    437 	    preq->preq_optype == PUFFS_VN_INACTIVE));
    438 
    439 	preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
    440 }
    441 
    442 int
    443 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
    444 {
    445 	long int n;
    446 	int parent, value, fd;
    447 
    448 	if (pipe(pu->pu_dpipe) == -1)
    449 		return -1;
    450 
    451 	switch (fork()) {
    452 	case -1:
    453 		return -1;
    454 	case 0:
    455 		parent = 0;
    456 		break;
    457 	default:
    458 		parent = 1;
    459 		break;
    460 	}
    461 	pu->pu_state |= PU_PUFFSDAEMON;
    462 
    463 	if (parent) {
    464 		close(pu->pu_dpipe[1]);
    465 		n = read(pu->pu_dpipe[0], &value, sizeof(int));
    466 		if (n == -1)
    467 			err(1, "puffs_daemon");
    468 		if (n != sizeof(value))
    469 			errx(1, "puffs_daemon got %ld bytes", n);
    470 		if (value) {
    471 			errno = value;
    472 			err(1, "puffs_daemon");
    473 		}
    474 		exit(0);
    475 	} else {
    476 		if (setsid() == -1)
    477 			goto fail;
    478 
    479 		if (!nochdir)
    480 			chdir("/");
    481 
    482 		if (!noclose) {
    483 			fd = open(_PATH_DEVNULL, O_RDWR, 0);
    484 			if (fd == -1)
    485 				goto fail;
    486 			dup2(fd, STDIN_FILENO);
    487 			dup2(fd, STDOUT_FILENO);
    488 			dup2(fd, STDERR_FILENO);
    489 			if (fd > STDERR_FILENO)
    490 				close(fd);
    491 		}
    492 		return 0;
    493 	}
    494 
    495  fail:
    496 	n = write(pu->pu_dpipe[1], &errno, sizeof(int));
    497 	assert(n == 4);
    498 	return -1;
    499 }
    500 
    501 static void
    502 shutdaemon(struct puffs_usermount *pu, int error)
    503 {
    504 	ssize_t n;
    505 
    506 	n = write(pu->pu_dpipe[1], &error, sizeof(int));
    507 	assert(n == 4);
    508 	close(pu->pu_dpipe[0]);
    509 	close(pu->pu_dpipe[1]);
    510 	pu->pu_state &= ~PU_PUFFSDAEMON;
    511 }
    512 
    513 int
    514 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
    515 	puffs_cookie_t cookie)
    516 {
    517 	char rp[MAXPATHLEN];
    518 	int rv, fd, sverrno;
    519 	char *comfd;
    520 
    521 	pu->pu_kargp->pa_root_cookie = cookie;
    522 
    523 	/* XXXkludgehere */
    524 	/* kauth doesn't provide this service any longer */
    525 	if (geteuid() != 0)
    526 		mntflags |= MNT_NOSUID | MNT_NODEV;
    527 
    528 	if (realpath(dir, rp) == NULL) {
    529 		rv = -1;
    530 		goto out;
    531 	}
    532 
    533 	if (strcmp(dir, rp) != 0) {
    534 		warnx("puffs_mount: \"%s\" is a relative path.", dir);
    535 		warnx("puffs_mount: using \"%s\" instead.", rp);
    536 	}
    537 
    538 	/*
    539 	 * Undocumented...  Well, documented only here.
    540 	 *
    541 	 * This is used for imaginative purposes.  If the env variable is
    542 	 * set, puffs_mount() doesn't do the regular mount procedure.
    543 	 * Rather, it crams the mount data down the comfd and sets comfd as
    544 	 * the puffs descriptor.
    545 	 *
    546 	 * This shouldn't be used unless you can read my mind ( ... or write
    547 	 * it, not to mention execute it, but that's starting to get silly).
    548 	 */
    549 	if ((comfd = getenv("PUFFS_COMFD")) != NULL) {
    550 		size_t len;
    551 
    552 		if (sscanf(comfd, "%d", &pu->pu_fd) != 1) {
    553 			errno = EINVAL;
    554 			rv = -1;
    555 			goto out;
    556 		}
    557 		/* check that what we got at least resembles an fd */
    558 		if (fcntl(pu->pu_fd, F_GETFL) == -1) {
    559 			rv = -1;
    560 			goto out;
    561 		}
    562 
    563 		len = strlen(dir)+1;
    564 
    565 #define allwrite(buf, len)						\
    566 do {									\
    567 	ssize_t al_rv;							\
    568 	al_rv = write(pu->pu_fd, buf, len);				\
    569 	if ((size_t)al_rv != len) {					\
    570 		if (al_rv != -1)					\
    571 			errno = EIO;					\
    572 		rv = -1;						\
    573 		abort();\
    574 		goto out;						\
    575 	}								\
    576 } while (/*CONSTCOND*/0)
    577 		allwrite(&len, sizeof(len));
    578 		allwrite(dir, len);
    579 		len = strlen(pu->pu_kargp->pa_mntfromname)+1;
    580 		allwrite(&len, sizeof(len));
    581 		allwrite(pu->pu_kargp->pa_mntfromname, len);
    582 		allwrite(&mntflags, sizeof(mntflags));
    583 		allwrite(pu->pu_kargp, sizeof(*pu->pu_kargp));
    584 		allwrite(&pu->pu_flags, sizeof(pu->pu_flags));
    585 #undef allwrite
    586 
    587 		rv = 0;
    588 	} else {
    589 		fd = open(_PATH_PUFFS, O_RDWR);
    590 		if (fd == -1) {
    591 			warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
    592 			rv = -1;
    593 			goto out;
    594 		}
    595 		if (fd <= 2)
    596 			warnx("puffs_mount: device fd %d (<= 2), sure this is "
    597 			    "what you want?", fd);
    598 
    599 		pu->pu_kargp->pa_fd = pu->pu_fd = fd;
    600 		if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
    601 		    pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
    602 			goto out;
    603 	}
    604 
    605 	PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
    606 
    607  out:
    608 	if (rv != 0)
    609 		sverrno = errno;
    610 	else
    611 		sverrno = 0;
    612 	free(pu->pu_kargp);
    613 	pu->pu_kargp = NULL;
    614 
    615 	if (pu->pu_state & PU_PUFFSDAEMON)
    616 		shutdaemon(pu, sverrno);
    617 
    618 	errno = sverrno;
    619 	return rv;
    620 }
    621 
    622 struct puffs_usermount *
    623 puffs_init(struct puffs_ops *pops, const char *mntfromname,
    624 	const char *puffsname, void *priv, uint32_t pflags)
    625 {
    626 	struct puffs_usermount *pu;
    627 	struct puffs_kargs *pargs;
    628 	int sverrno;
    629 
    630 	if (puffsname == PUFFS_DEFER)
    631 		puffsname = "n/a";
    632 	if (mntfromname == PUFFS_DEFER)
    633 		mntfromname = "n/a";
    634 	if (priv == PUFFS_DEFER)
    635 		priv = NULL;
    636 
    637 	pu = malloc(sizeof(struct puffs_usermount));
    638 	if (pu == NULL)
    639 		goto failfree;
    640 	memset(pu, 0, sizeof(struct puffs_usermount));
    641 
    642 	pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
    643 	if (pargs == NULL)
    644 		goto failfree;
    645 	memset(pargs, 0, sizeof(struct puffs_kargs));
    646 
    647 	pargs->pa_vers = PUFFSVERSION;
    648 	pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
    649 	fillvnopmask(pops, pargs);
    650 	puffs_setmntinfo(pu, mntfromname, puffsname);
    651 
    652 	puffs_zerostatvfs(&pargs->pa_svfsb);
    653 	pargs->pa_root_cookie = NULL;
    654 	pargs->pa_root_vtype = VDIR;
    655 	pargs->pa_root_vsize = 0;
    656 	pargs->pa_root_rdev = 0;
    657 	pargs->pa_maxmsglen = 0;
    658 
    659 	pu->pu_flags = pflags;
    660 	pu->pu_ops = *pops;
    661 	free(pops); /* XXX */
    662 
    663 	pu->pu_privdata = priv;
    664 	pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
    665 	LIST_INIT(&pu->pu_pnodelst);
    666 	LIST_INIT(&pu->pu_ios);
    667 	LIST_INIT(&pu->pu_ios_rmlist);
    668 	LIST_INIT(&pu->pu_ccmagazin);
    669 	TAILQ_INIT(&pu->pu_sched);
    670 
    671 	pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs__fsframe_read;
    672 	pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs__fsframe_write;
    673 	pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs__fsframe_cmp;
    674 	pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs__fsframe_gotframe;
    675 	pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
    676 
    677 	/* defaults for some user-settable translation functions */
    678 	pu->pu_cmap = NULL; /* identity translation */
    679 
    680 	pu->pu_pathbuild = puffs_stdpath_buildpath;
    681 	pu->pu_pathfree = puffs_stdpath_freepath;
    682 	pu->pu_pathcmp = puffs_stdpath_cmppath;
    683 	pu->pu_pathtransform = NULL;
    684 	pu->pu_namemod = NULL;
    685 
    686 	pu->pu_errnotify = puffs_kernerr_log;
    687 
    688 	PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
    689 
    690 	return pu;
    691 
    692  failfree:
    693 	/* can't unmount() from here for obvious reasons */
    694 	sverrno = errno;
    695 	free(pu);
    696 	errno = sverrno;
    697 	return NULL;
    698 }
    699 
    700 void
    701 puffs_cancel(struct puffs_usermount *pu, int error)
    702 {
    703 
    704 	assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
    705 	shutdaemon(pu, error);
    706 	free(pu);
    707 }
    708 
    709 /*ARGSUSED1*/
    710 int
    711 puffs_exit(struct puffs_usermount *pu, int unused /* strict compat */)
    712 {
    713 	struct puffs_framebuf *pb;
    714 	struct puffs_req *preq;
    715 	void *winp;
    716 	size_t winlen;
    717 	int sverrno;
    718 
    719 	pb = puffs_framebuf_make();
    720 	if (pb == NULL) {
    721 		errno = ENOMEM;
    722 		return -1;
    723 	}
    724 
    725 	winlen = sizeof(struct puffs_req);
    726 	if (puffs_framebuf_getwindow(pb, 0, &winp, &winlen) == -1) {
    727 		sverrno = errno;
    728 		puffs_framebuf_destroy(pb);
    729 		errno = sverrno;
    730 		return -1;
    731 	}
    732 	preq = winp;
    733 
    734 	preq->preq_buflen = sizeof(struct puffs_req);
    735 	preq->preq_opclass = PUFFSOP_UNMOUNT;
    736 	preq->preq_id = puffs__nextreq(pu);
    737 
    738 	puffs_framev_enqueue_justsend(pu, puffs_getselectable(pu), pb, 1, 0);
    739 
    740 	return 0;
    741 }
    742 
    743 /* no sigset_t static intializer */
    744 static int sigs[NSIG] = { 0, };
    745 static int sigcatch = 0;
    746 
    747 int
    748 puffs_unmountonsignal(int sig, bool sigignore)
    749 {
    750 
    751 	if (sig < 0 || sig >= (int)NSIG) {
    752 		errno = EINVAL;
    753 		return -1;
    754 	}
    755 	if (sigignore)
    756 		if (signal(sig, SIG_IGN) == SIG_ERR)
    757 			return -1;
    758 
    759 	if (!sigs[sig])
    760 		sigcatch++;
    761 	sigs[sig] = 1;
    762 
    763 	return 0;
    764 }
    765 
    766 /*
    767  * Actual mainloop.  This is called from a context which can block.
    768  * It is called either from puffs_mainloop (indirectly, via
    769  * puffs_cc_continue() or from puffs_cc_yield()).
    770  */
    771 void
    772 puffs__theloop(struct puffs_cc *pcc)
    773 {
    774 	struct puffs_usermount *pu = pcc->pcc_pu;
    775 	struct puffs_framectrl *pfctrl;
    776 	struct puffs_fctrl_io *fio;
    777 	struct kevent *curev;
    778 	size_t nchanges;
    779 	int ndone;
    780 
    781 	while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
    782 
    783 		/*
    784 		 * Schedule existing requests.
    785 		 */
    786 		while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
    787 			TAILQ_REMOVE(&pu->pu_sched, pcc, pcc_schedent);
    788 			puffs__goto(pcc);
    789 		}
    790 
    791 		if (pu->pu_ml_lfn)
    792 			pu->pu_ml_lfn(pu);
    793 
    794 		/* XXX: can we still do these optimizations? */
    795 #if 0
    796 		/*
    797 		 * Do this here, because:
    798 		 *  a) loopfunc might generate some results
    799 		 *  b) it's still "after" event handling (except for round 1)
    800 		 */
    801 		if (puffs_req_putput(ppr) == -1)
    802 			goto out;
    803 		puffs_req_resetput(ppr);
    804 
    805 		/* micro optimization: skip kevent syscall if possible */
    806 		if (pu->pu_nfds == 1 && pu->pu_ml_timep == NULL
    807 		    && (pu->pu_state & PU_ASYNCFD) == 0) {
    808 			pfctrl = XXX->fctrl;
    809 			puffs_framev_input(pu, pfctrl, XXX);
    810 			continue;
    811 		}
    812 #endif
    813 
    814 		/* else: do full processing */
    815 		/* Don't bother worrying about O(n) for now */
    816 		LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
    817 			if (fio->stat & FIO_WRGONE)
    818 				continue;
    819 
    820 			pfctrl = fio->fctrl;
    821 
    822 			/*
    823 			 * Try to write out everything to avoid the
    824 			 * need for enabling EVFILT_WRITE.  The likely
    825 			 * case is that we can fit everything into the
    826 			 * socket buffer.
    827 			 */
    828 			puffs__framev_output(pu, pfctrl, fio);
    829 		}
    830 
    831 		/*
    832 		 * Build list of which to enable/disable in writecheck.
    833 		 */
    834 		nchanges = 0;
    835 		LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
    836 			if (fio->stat & FIO_WRGONE)
    837 				continue;
    838 
    839 			/* en/disable write checks for kqueue as needed */
    840 			assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
    841 			if (FIO_EN_WRITE(fio)) {
    842 				EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
    843 				    EVFILT_WRITE, EV_ENABLE, 0, 0,
    844 				    (uintptr_t)fio);
    845 				fio->stat |= FIO_WR;
    846 				nchanges++;
    847 			}
    848 			if (FIO_RM_WRITE(fio)) {
    849 				EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
    850 				    EVFILT_WRITE, EV_DISABLE, 0, 0,
    851 				    (uintptr_t)fio);
    852 				fio->stat &= ~FIO_WR;
    853 				nchanges++;
    854 			}
    855 		}
    856 
    857 		ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
    858 		    pu->pu_evs, pu->pu_nevs, pu->pu_ml_timep);
    859 
    860 		if (ndone == -1) {
    861 			if (errno != EINTR)
    862 				break;
    863 			else
    864 				continue;
    865 		}
    866 
    867 		/* uoptimize */
    868 		if (ndone == 0)
    869 			continue;
    870 
    871 		/* iterate over the results */
    872 		for (curev = pu->pu_evs; ndone--; curev++) {
    873 			int what;
    874 
    875 #if 0
    876 			/* get & possibly dispatch events from kernel */
    877 			if (curev->ident == puffsfd) {
    878 				if (puffs_req_handle(pgr, ppr, 0) == -1)
    879 					goto out;
    880 				continue;
    881 			}
    882 #endif
    883 
    884 			fio = (void *)curev->udata;
    885 			if (__predict_true(fio))
    886 				pfctrl = fio->fctrl;
    887 			else
    888 				pfctrl = NULL;
    889 			if (curev->flags & EV_ERROR) {
    890 				assert(curev->filter == EVFILT_WRITE);
    891 				fio->stat &= ~FIO_WR;
    892 
    893 				/* XXX: how to know if it's a transient error */
    894 				puffs__framev_writeclose(pu, fio,
    895 				    (int)curev->data);
    896 				puffs__framev_notify(fio, PUFFS_FBIO_ERROR);
    897 				continue;
    898 			}
    899 
    900 			what = 0;
    901 			if (curev->filter == EVFILT_READ) {
    902 				puffs__framev_input(pu, pfctrl, fio);
    903 				what |= PUFFS_FBIO_READ;
    904 			}
    905 
    906 			else if (curev->filter == EVFILT_WRITE) {
    907 				puffs__framev_output(pu, pfctrl, fio);
    908 				what |= PUFFS_FBIO_WRITE;
    909 			}
    910 
    911 			else if (__predict_false(curev->filter==EVFILT_SIGNAL)){
    912 				if ((pu->pu_state & PU_DONEXIT) == 0) {
    913 					PU_SETSFLAG(pu, PU_DONEXIT);
    914 					puffs_exit(pu, 0);
    915 				}
    916 			}
    917 			if (what)
    918 				puffs__framev_notify(fio, what);
    919 		}
    920 
    921 		/*
    922 		 * Really free fd's now that we don't have references
    923 		 * to them.
    924 		 */
    925 		while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
    926 			LIST_REMOVE(fio, fio_entries);
    927 			free(fio);
    928 		}
    929 	}
    930 
    931 	if (puffs__cc_restoremain(pu) == -1)
    932 		warn("cannot restore main context.  impending doom");
    933 }
    934 int
    935 puffs_mainloop(struct puffs_usermount *pu)
    936 {
    937 	struct puffs_fctrl_io *fio;
    938 	struct puffs_cc *pcc;
    939 	struct kevent *curev;
    940 	size_t nevs;
    941 	int sverrno, i;
    942 
    943 	assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
    944 
    945 	pu->pu_kq = kqueue();
    946 	if (pu->pu_kq == -1)
    947 		goto out;
    948 	pu->pu_state |= PU_HASKQ;
    949 
    950 	puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
    951 	if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
    952 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
    953 	    &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
    954 		goto out;
    955 
    956 	nevs = pu->pu_nevs + sigcatch;
    957 	curev = realloc(pu->pu_evs, nevs * sizeof(struct kevent));
    958 	if (curev == NULL)
    959 		goto out;
    960 	pu->pu_evs = curev;
    961 	pu->pu_nevs = nevs;
    962 
    963 	LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
    964 		EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
    965 		    0, 0, (uintptr_t)fio);
    966 		curev++;
    967 		EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
    968 		    0, 0, (uintptr_t)fio);
    969 		curev++;
    970 	}
    971 	for (i = 0; i < NSIG; i++) {
    972 		if (sigs[i]) {
    973 			EV_SET(curev, i, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
    974 			    0, 0, 0);
    975 			curev++;
    976 		}
    977 	}
    978 	assert(curev - pu->pu_evs == (ssize_t)pu->pu_nevs);
    979 	if (kevent(pu->pu_kq, pu->pu_evs, pu->pu_nevs, NULL, 0, NULL) == -1)
    980 		goto out;
    981 
    982 	pu->pu_state |= PU_INLOOP;
    983 
    984 	/*
    985 	 * Create alternate execution context and jump to it.  Note
    986 	 * that we come "out" of savemain twice.  Where we come out
    987 	 * of it depends on the architecture.  If the return address is
    988 	 * stored on the stack, we jump out from puffs_cc_continue(),
    989 	 * for a register return address from puffs__cc_savemain().
    990 	 * PU_MAINRESTORE makes sure we DTRT in both cases.
    991 	 */
    992 	if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
    993 		goto out;
    994 	}
    995 	if (puffs__cc_savemain(pu) == -1) {
    996 		goto out;
    997 	}
    998 	if ((pu->pu_state & PU_MAINRESTORE) == 0)
    999 		puffs_cc_continue(pcc);
   1000 
   1001 	finalpush(pu);
   1002 	errno = 0;
   1003 
   1004  out:
   1005 	/* store the real error for a while */
   1006 	sverrno = errno;
   1007 
   1008 	errno = sverrno;
   1009 	if (errno)
   1010 		return -1;
   1011 	else
   1012 		return 0;
   1013 }
   1014