Home | History | Annotate | Line # | Download | only in libpuffs
puffs.c revision 1.98
      1 /*	$NetBSD: puffs.c,v 1.98 2009/01/08 02:28:08 lukem 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.98 2009/01/08 02:28:08 lukem 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, uint8_t *opmask)
     74 {
     75 
     76 	memset(opmask, 0, PUFFS_VN_MAX);
     77 
     78 	FILLOP(create,   CREATE);
     79 	FILLOP(mknod,    MKNOD);
     80 	FILLOP(open,     OPEN);
     81 	FILLOP(close,    CLOSE);
     82 	FILLOP(access,   ACCESS);
     83 	FILLOP(getattr,  GETATTR);
     84 	FILLOP(setattr,  SETATTR);
     85 	FILLOP(poll,     POLL); /* XXX: not ready in kernel */
     86 	FILLOP(mmap,     MMAP);
     87 	FILLOP(fsync,    FSYNC);
     88 	FILLOP(seek,     SEEK);
     89 	FILLOP(remove,   REMOVE);
     90 	FILLOP(link,     LINK);
     91 	FILLOP(rename,   RENAME);
     92 	FILLOP(mkdir,    MKDIR);
     93 	FILLOP(rmdir,    RMDIR);
     94 	FILLOP(symlink,  SYMLINK);
     95 	FILLOP(readdir,  READDIR);
     96 	FILLOP(readlink, READLINK);
     97 	FILLOP(reclaim,  RECLAIM);
     98 	FILLOP(inactive, INACTIVE);
     99 	FILLOP(print,    PRINT);
    100 	FILLOP(read,     READ);
    101 	FILLOP(write,    WRITE);
    102 }
    103 #undef FILLOP
    104 
    105 /*
    106  * Go over all framev entries and write everything we can.  This is
    107  * mostly for the benefit of delivering "unmount" to the kernel.
    108  */
    109 static void
    110 finalpush(struct puffs_usermount *pu)
    111 {
    112 	struct puffs_fctrl_io *fio;
    113 
    114 	LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
    115 		if (fio->stat & FIO_WRGONE)
    116 			continue;
    117 
    118 		puffs__framev_output(pu, fio->fctrl, fio);
    119 	}
    120 }
    121 
    122 /*ARGSUSED*/
    123 static void
    124 puffs_defaulterror(struct puffs_usermount *pu, uint8_t type,
    125 	int error, const char *str, puffs_cookie_t cookie)
    126 {
    127 
    128 	fprintf(stderr, "abort: type %d, error %d, cookie %p (%s)\n",
    129 	    type, error, cookie, str);
    130 	abort();
    131 }
    132 
    133 int
    134 puffs_getselectable(struct puffs_usermount *pu)
    135 {
    136 
    137 	return pu->pu_fd;
    138 }
    139 
    140 uint64_t
    141 puffs__nextreq(struct puffs_usermount *pu)
    142 {
    143 	uint64_t rv;
    144 
    145 	PU_LOCK();
    146 	rv = pu->pu_nextreq++;
    147 	PU_UNLOCK();
    148 
    149 	return rv;
    150 }
    151 
    152 int
    153 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
    154 {
    155 	int rv, x;
    156 
    157 	assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
    158 
    159 	if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
    160 		errno = EINVAL;
    161 		return -1;
    162 	}
    163 
    164 	x = mode;
    165 	rv = ioctl(pu->pu_fd, FIONBIO, &x);
    166 
    167 	if (rv == 0) {
    168 		if (mode == PUFFSDEV_BLOCK)
    169 			pu->pu_state &= ~PU_ASYNCFD;
    170 		else
    171 			pu->pu_state |= PU_ASYNCFD;
    172 	}
    173 
    174 	return rv;
    175 }
    176 
    177 int
    178 puffs_getstate(struct puffs_usermount *pu)
    179 {
    180 
    181 	return pu->pu_state & PU_STATEMASK;
    182 }
    183 
    184 void
    185 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
    186 {
    187 	long psize, minsize;
    188 	int stackshift;
    189 	int bonus;
    190 
    191 	assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
    192 
    193 	psize = sysconf(_SC_PAGESIZE);
    194 	minsize = 4*psize;
    195 	if (ss < (size_t)minsize || ss == PUFFS_STACKSIZE_MIN) {
    196 		if (ss != PUFFS_STACKSIZE_MIN)
    197 			fprintf(stderr, "puffs_setstacksize: adjusting "
    198 			    "stacksize to minimum %ld\n", minsize);
    199 		ss = 4*psize;
    200 	}
    201 
    202 	stackshift = -1;
    203 	bonus = 0;
    204 	while (ss) {
    205 		if (ss & 0x1)
    206 			bonus++;
    207 		ss >>= 1;
    208 		stackshift++;
    209 	}
    210 	if (bonus > 1) {
    211 		stackshift++;
    212 		fprintf(stderr, "puffs_setstacksize: using next power of two: "
    213 		    "%d\n", 1<<stackshift);
    214 	}
    215 
    216 	pu->pu_cc_stackshift = stackshift;
    217 }
    218 
    219 struct puffs_pathobj *
    220 puffs_getrootpathobj(struct puffs_usermount *pu)
    221 {
    222 	struct puffs_node *pnr;
    223 
    224 	pnr = pu->pu_pn_root;
    225 	if (pnr == NULL) {
    226 		errno = ENOENT;
    227 		return NULL;
    228 	}
    229 
    230 	return &pnr->pn_po;
    231 }
    232 
    233 void
    234 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
    235 {
    236 
    237 	pu->pu_pn_root = pn;
    238 }
    239 
    240 struct puffs_node *
    241 puffs_getroot(struct puffs_usermount *pu)
    242 {
    243 
    244 	return pu->pu_pn_root;
    245 }
    246 
    247 void
    248 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
    249 	vsize_t vsize, dev_t rdev)
    250 {
    251 	struct puffs_kargs *pargs = pu->pu_kargp;
    252 
    253 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
    254 		warnx("puffs_setrootinfo: call has effect only "
    255 		    "before mount\n");
    256 		return;
    257 	}
    258 
    259 	pargs->pa_root_vtype = vt;
    260 	pargs->pa_root_vsize = vsize;
    261 	pargs->pa_root_rdev = rdev;
    262 }
    263 
    264 void *
    265 puffs_getspecific(struct puffs_usermount *pu)
    266 {
    267 
    268 	return pu->pu_privdata;
    269 }
    270 
    271 void
    272 puffs_setspecific(struct puffs_usermount *pu, void *privdata)
    273 {
    274 
    275 	pu->pu_privdata = privdata;
    276 }
    277 
    278 size_t
    279 puffs_getmaxreqlen(struct puffs_usermount *pu)
    280 {
    281 
    282 	return pu->pu_maxreqlen;
    283 }
    284 
    285 void
    286 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
    287 {
    288 
    289 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    290 		warnx("puffs_setmaxreqlen: call has effect only "
    291 		    "before mount\n");
    292 
    293 	pu->pu_kargp->pa_maxmsglen = reqlen;
    294 }
    295 
    296 void
    297 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
    298 {
    299 
    300 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    301 		warnx("puffs_setfhsize: call has effect only before mount\n");
    302 
    303 	pu->pu_kargp->pa_fhsize = fhsize;
    304 	pu->pu_kargp->pa_fhflags = flags;
    305 }
    306 
    307 void
    308 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
    309 {
    310 
    311 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    312 		warnx("puffs_setfhsize: call has effect only before mount\n");
    313 
    314 	pu->pu_kargp->pa_nhashbuckets = nhash;
    315 }
    316 
    317 void
    318 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
    319 {
    320 
    321 	pu->pu_pathbuild = fn;
    322 }
    323 
    324 void
    325 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
    326 {
    327 
    328 	pu->pu_pathtransform = fn;
    329 }
    330 
    331 void
    332 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
    333 {
    334 
    335 	pu->pu_pathcmp = fn;
    336 }
    337 
    338 void
    339 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
    340 {
    341 
    342 	pu->pu_pathfree = fn;
    343 }
    344 
    345 void
    346 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
    347 {
    348 
    349 	pu->pu_namemod = fn;
    350 }
    351 
    352 void
    353 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
    354 {
    355 
    356 	pu->pu_errnotify = fn;
    357 }
    358 
    359 void
    360 puffs_set_cmap(struct puffs_usermount *pu, pu_cmap_fn fn)
    361 {
    362 
    363 	pu->pu_cmap = fn;
    364 }
    365 
    366 void
    367 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
    368 {
    369 
    370 	pu->pu_ml_lfn = lfn;
    371 }
    372 
    373 void
    374 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
    375 {
    376 
    377 	if (ts == NULL) {
    378 		pu->pu_ml_timep = NULL;
    379 	} else {
    380 		pu->pu_ml_timeout = *ts;
    381 		pu->pu_ml_timep = &pu->pu_ml_timeout;
    382 	}
    383 }
    384 
    385 void
    386 puffs_set_prepost(struct puffs_usermount *pu,
    387 	pu_prepost_fn pre, pu_prepost_fn pst)
    388 {
    389 
    390 	pu->pu_oppre = pre;
    391 	pu->pu_oppost = pst;
    392 }
    393 
    394 void
    395 puffs_setback(struct puffs_cc *pcc, int whatback)
    396 {
    397 	struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
    398 
    399 	assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
    400 	    preq->preq_optype == PUFFS_VN_OPEN ||
    401 	    preq->preq_optype == PUFFS_VN_MMAP ||
    402 	    preq->preq_optype == PUFFS_VN_REMOVE ||
    403 	    preq->preq_optype == PUFFS_VN_RMDIR ||
    404 	    preq->preq_optype == PUFFS_VN_INACTIVE));
    405 
    406 	preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
    407 }
    408 
    409 int
    410 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
    411 {
    412 	long int n;
    413 	int parent, value, fd;
    414 
    415 	if (pipe(pu->pu_dpipe) == -1)
    416 		return -1;
    417 
    418 	switch (fork()) {
    419 	case -1:
    420 		return -1;
    421 	case 0:
    422 		parent = 0;
    423 		break;
    424 	default:
    425 		parent = 1;
    426 		break;
    427 	}
    428 	pu->pu_state |= PU_PUFFSDAEMON;
    429 
    430 	if (parent) {
    431 		close(pu->pu_dpipe[1]);
    432 		n = read(pu->pu_dpipe[0], &value, sizeof(int));
    433 		if (n == -1)
    434 			err(1, "puffs_daemon");
    435 		if (n != sizeof(value))
    436 			errx(1, "puffs_daemon got %ld bytes", n);
    437 		if (value) {
    438 			errno = value;
    439 			err(1, "puffs_daemon");
    440 		}
    441 		exit(0);
    442 	} else {
    443 		if (setsid() == -1)
    444 			goto fail;
    445 
    446 		if (!nochdir)
    447 			chdir("/");
    448 
    449 		if (!noclose) {
    450 			fd = open(_PATH_DEVNULL, O_RDWR, 0);
    451 			if (fd == -1)
    452 				goto fail;
    453 			dup2(fd, STDIN_FILENO);
    454 			dup2(fd, STDOUT_FILENO);
    455 			dup2(fd, STDERR_FILENO);
    456 			if (fd > STDERR_FILENO)
    457 				close(fd);
    458 		}
    459 		return 0;
    460 	}
    461 
    462  fail:
    463 	n = write(pu->pu_dpipe[1], &errno, sizeof(int));
    464 	assert(n == 4);
    465 	return -1;
    466 }
    467 
    468 static void
    469 shutdaemon(struct puffs_usermount *pu, int error)
    470 {
    471 	ssize_t n;
    472 
    473 	n = write(pu->pu_dpipe[1], &error, sizeof(int));
    474 	assert(n == 4);
    475 	close(pu->pu_dpipe[0]);
    476 	close(pu->pu_dpipe[1]);
    477 	pu->pu_state &= ~PU_PUFFSDAEMON;
    478 }
    479 
    480 int
    481 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
    482 	puffs_cookie_t cookie)
    483 {
    484 	char rp[MAXPATHLEN];
    485 	int rv, fd, sverrno;
    486 	char *comfd;
    487 
    488 	pu->pu_kargp->pa_root_cookie = cookie;
    489 
    490 	/* XXXkludgehere */
    491 	/* kauth doesn't provide this service any longer */
    492 	if (geteuid() != 0)
    493 		mntflags |= MNT_NOSUID | MNT_NODEV;
    494 
    495 	if (realpath(dir, rp) == NULL) {
    496 		rv = -1;
    497 		goto out;
    498 	}
    499 
    500 	if (strcmp(dir, rp) != 0) {
    501 		warnx("puffs_mount: \"%s\" is a relative path.", dir);
    502 		warnx("puffs_mount: using \"%s\" instead.", rp);
    503 	}
    504 
    505 	/*
    506 	 * Undocumented...  Well, documented only here.
    507 	 *
    508 	 * This is used for imaginative purposes.  If the env variable is
    509 	 * set, puffs_mount() doesn't do the regular mount procedure.
    510 	 * Rather, it crams the mount data down the comfd and sets comfd as
    511 	 * the puffs descriptor.
    512 	 *
    513 	 * This shouldn't be used unless you can read my mind ( ... or write
    514 	 * it, not to mention execute it, but that's starting to get silly).
    515 	 */
    516 	if ((comfd = getenv("PUFFS_COMFD")) != NULL) {
    517 		size_t len;
    518 
    519 		if (sscanf(comfd, "%d", &pu->pu_fd) != 1) {
    520 			errno = EINVAL;
    521 			rv = -1;
    522 			goto out;
    523 		}
    524 		/* check that what we got at least resembles an fd */
    525 		if (fcntl(pu->pu_fd, F_GETFL) == -1) {
    526 			rv = -1;
    527 			goto out;
    528 		}
    529 
    530 		len = strlen(dir)+1;
    531 
    532 #define allwrite(buf, len)						\
    533 do {									\
    534 	ssize_t al_rv;							\
    535 	al_rv = write(pu->pu_fd, buf, len);				\
    536 	if ((size_t)al_rv != len) {					\
    537 		if (al_rv != -1)					\
    538 			errno = EIO;					\
    539 		rv = -1;						\
    540 		abort();\
    541 		goto out;						\
    542 	}								\
    543 } while (/*CONSTCOND*/0)
    544 		allwrite(&len, sizeof(len));
    545 		allwrite(dir, len);
    546 		len = strlen(pu->pu_kargp->pa_mntfromname)+1;
    547 		allwrite(&len, sizeof(len));
    548 		allwrite(pu->pu_kargp->pa_mntfromname, len);
    549 		allwrite(&mntflags, sizeof(mntflags));
    550 		allwrite(pu->pu_kargp, sizeof(*pu->pu_kargp));
    551 		allwrite(&pu->pu_flags, sizeof(pu->pu_flags));
    552 #undef allwrite
    553 
    554 		rv = 0;
    555 	} else {
    556 		fd = open(_PATH_PUFFS, O_RDWR);
    557 		if (fd == -1) {
    558 			warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
    559 			rv = -1;
    560 			goto out;
    561 		}
    562 		if (fd <= 2)
    563 			warnx("puffs_mount: device fd %d (<= 2), sure this is "
    564 			    "what you want?", fd);
    565 
    566 		pu->pu_kargp->pa_fd = pu->pu_fd = fd;
    567 		if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
    568 		    pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
    569 			goto out;
    570 	}
    571 
    572 	PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
    573 
    574  out:
    575 	if (rv != 0)
    576 		sverrno = errno;
    577 	else
    578 		sverrno = 0;
    579 	free(pu->pu_kargp);
    580 	pu->pu_kargp = NULL;
    581 
    582 	if (pu->pu_state & PU_PUFFSDAEMON)
    583 		shutdaemon(pu, sverrno);
    584 
    585 	errno = sverrno;
    586 	return rv;
    587 }
    588 
    589 struct puffs_usermount *
    590 _puffs_init(int develv, struct puffs_ops *pops, const char *mntfromname,
    591 	const char *puffsname, void *priv, uint32_t pflags)
    592 {
    593 	struct puffs_usermount *pu;
    594 	struct puffs_kargs *pargs;
    595 	int sverrno;
    596 
    597 	if (develv != PUFFS_DEVEL_LIBVERSION) {
    598 		warnx("puffs_init: mounting with lib version %d, need %d",
    599 		    develv, PUFFS_DEVEL_LIBVERSION);
    600 		errno = EINVAL;
    601 		return NULL;
    602 	}
    603 
    604 	pu = malloc(sizeof(struct puffs_usermount));
    605 	if (pu == NULL)
    606 		goto failfree;
    607 	memset(pu, 0, sizeof(struct puffs_usermount));
    608 
    609 	pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
    610 	if (pargs == NULL)
    611 		goto failfree;
    612 	memset(pargs, 0, sizeof(struct puffs_kargs));
    613 
    614 	pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
    615 	pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
    616 	fillvnopmask(pops, pargs->pa_vnopmask);
    617 	(void)strlcpy(pargs->pa_typename, puffsname,
    618 	    sizeof(pargs->pa_typename));
    619 	(void)strlcpy(pargs->pa_mntfromname, mntfromname,
    620 	    sizeof(pargs->pa_mntfromname));
    621 
    622 	puffs_zerostatvfs(&pargs->pa_svfsb);
    623 	pargs->pa_root_cookie = NULL;
    624 	pargs->pa_root_vtype = VDIR;
    625 	pargs->pa_root_vsize = 0;
    626 	pargs->pa_root_rdev = 0;
    627 	pargs->pa_maxmsglen = 0;
    628 
    629 	pu->pu_flags = pflags;
    630 	pu->pu_ops = *pops;
    631 	free(pops); /* XXX */
    632 
    633 	pu->pu_privdata = priv;
    634 	pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
    635 	LIST_INIT(&pu->pu_pnodelst);
    636 	LIST_INIT(&pu->pu_ios);
    637 	LIST_INIT(&pu->pu_ios_rmlist);
    638 	LIST_INIT(&pu->pu_ccmagazin);
    639 	TAILQ_INIT(&pu->pu_sched);
    640 
    641 	pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs__fsframe_read;
    642 	pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs__fsframe_write;
    643 	pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs__fsframe_cmp;
    644 	pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs__fsframe_gotframe;
    645 	pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
    646 
    647 	/* defaults for some user-settable translation functions */
    648 	pu->pu_cmap = NULL; /* identity translation */
    649 
    650 	pu->pu_pathbuild = puffs_stdpath_buildpath;
    651 	pu->pu_pathfree = puffs_stdpath_freepath;
    652 	pu->pu_pathcmp = puffs_stdpath_cmppath;
    653 	pu->pu_pathtransform = NULL;
    654 	pu->pu_namemod = NULL;
    655 
    656 	pu->pu_errnotify = puffs_defaulterror;
    657 
    658 	PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
    659 
    660 	return pu;
    661 
    662  failfree:
    663 	/* can't unmount() from here for obvious reasons */
    664 	sverrno = errno;
    665 	free(pu);
    666 	errno = sverrno;
    667 	return NULL;
    668 }
    669 
    670 void
    671 puffs_cancel(struct puffs_usermount *pu, int error)
    672 {
    673 
    674 	assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
    675 	shutdaemon(pu, error);
    676 	free(pu);
    677 }
    678 
    679 /*
    680  * XXX: there's currently no clean way to request unmount from
    681  * within the user server, so be very brutal about it.
    682  */
    683 /*ARGSUSED1*/
    684 int
    685 puffs_exit(struct puffs_usermount *pu, int force)
    686 {
    687 	struct puffs_node *pn;
    688 
    689 	force = 1; /* currently */
    690 	assert((pu->pu_state & PU_PUFFSDAEMON) == 0);
    691 
    692 	if (pu->pu_fd)
    693 		close(pu->pu_fd);
    694 
    695 	while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
    696 		puffs_pn_put(pn);
    697 
    698 	finalpush(pu);
    699 	puffs__framev_exit(pu);
    700 	puffs__cc_exit(pu);
    701 	if (pu->pu_state & PU_HASKQ)
    702 		close(pu->pu_kq);
    703 	free(pu);
    704 
    705 	return 0; /* always succesful for now, WILL CHANGE */
    706 }
    707 
    708 /*
    709  * Actual mainloop.  This is called from a context which can block.
    710  * It is called either from puffs_mainloop (indirectly, via
    711  * puffs_cc_continue() or from puffs_cc_yield()).
    712  */
    713 void
    714 puffs__theloop(struct puffs_cc *pcc)
    715 {
    716 	struct puffs_usermount *pu = pcc->pcc_pu;
    717 	struct puffs_framectrl *pfctrl;
    718 	struct puffs_fctrl_io *fio;
    719 	struct kevent *curev;
    720 	size_t nchanges;
    721 	int ndone;
    722 
    723 	while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
    724 		/*
    725 		 * Schedule existing requests.
    726 		 */
    727 		while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
    728 			TAILQ_REMOVE(&pu->pu_sched, pcc, pcc_schedent);
    729 			puffs__goto(pcc);
    730 		}
    731 
    732 		if (pu->pu_ml_lfn)
    733 			pu->pu_ml_lfn(pu);
    734 
    735 		/* XXX: can we still do these optimizations? */
    736 #if 0
    737 		/*
    738 		 * Do this here, because:
    739 		 *  a) loopfunc might generate some results
    740 		 *  b) it's still "after" event handling (except for round 1)
    741 		 */
    742 		if (puffs_req_putput(ppr) == -1)
    743 			goto out;
    744 		puffs_req_resetput(ppr);
    745 
    746 		/* micro optimization: skip kevent syscall if possible */
    747 		if (pu->pu_nfds == 1 && pu->pu_ml_timep == NULL
    748 		    && (pu->pu_state & PU_ASYNCFD) == 0) {
    749 			pfctrl = XXX->fctrl;
    750 			puffs_framev_input(pu, pfctrl, XXX);
    751 			continue;
    752 		}
    753 #endif
    754 
    755 		/* else: do full processing */
    756 		/* Don't bother worrying about O(n) for now */
    757 		LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
    758 			if (fio->stat & FIO_WRGONE)
    759 				continue;
    760 
    761 			pfctrl = fio->fctrl;
    762 
    763 			/*
    764 			 * Try to write out everything to avoid the
    765 			 * need for enabling EVFILT_WRITE.  The likely
    766 			 * case is that we can fit everything into the
    767 			 * socket buffer.
    768 			 */
    769 			puffs__framev_output(pu, pfctrl, fio);
    770 		}
    771 
    772 		/*
    773 		 * Build list of which to enable/disable in writecheck.
    774 		 */
    775 		nchanges = 0;
    776 		LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
    777 			if (fio->stat & FIO_WRGONE)
    778 				continue;
    779 
    780 			/* en/disable write checks for kqueue as needed */
    781 			assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
    782 			if (FIO_EN_WRITE(fio)) {
    783 				EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
    784 				    EVFILT_WRITE, EV_ENABLE, 0, 0,
    785 				    (uintptr_t)fio);
    786 				fio->stat |= FIO_WR;
    787 				nchanges++;
    788 			}
    789 			if (FIO_RM_WRITE(fio)) {
    790 				EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
    791 				    EVFILT_WRITE, EV_DISABLE, 0, 0,
    792 				    (uintptr_t)fio);
    793 				fio->stat &= ~FIO_WR;
    794 				nchanges++;
    795 			}
    796 			assert(nchanges <= pu->pu_nfds);
    797 		}
    798 
    799 		ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
    800 		    pu->pu_evs, 2*pu->pu_nfds, pu->pu_ml_timep);
    801 
    802 		if (ndone == -1) {
    803 			if (errno != EINTR)
    804 				break;
    805 			else
    806 				continue;
    807 		}
    808 
    809 		/* uoptimize */
    810 		if (ndone == 0)
    811 			continue;
    812 
    813 		/* iterate over the results */
    814 		for (curev = pu->pu_evs; ndone--; curev++) {
    815 			int what;
    816 
    817 #if 0
    818 			/* get & possibly dispatch events from kernel */
    819 			if (curev->ident == puffsfd) {
    820 				if (puffs_req_handle(pgr, ppr, 0) == -1)
    821 					goto out;
    822 				continue;
    823 			}
    824 #endif
    825 
    826 			fio = (void *)curev->udata;
    827 			pfctrl = fio->fctrl;
    828 			if (curev->flags & EV_ERROR) {
    829 				assert(curev->filter == EVFILT_WRITE);
    830 				fio->stat &= ~FIO_WR;
    831 
    832 				/* XXX: how to know if it's a transient error */
    833 				puffs__framev_writeclose(pu, fio,
    834 				    (int)curev->data);
    835 				puffs__framev_notify(fio, PUFFS_FBIO_ERROR);
    836 				continue;
    837 			}
    838 
    839 			what = 0;
    840 			if (curev->filter == EVFILT_READ) {
    841 				puffs__framev_input(pu, pfctrl, fio);
    842 				what |= PUFFS_FBIO_READ;
    843 			}
    844 
    845 			else if (curev->filter == EVFILT_WRITE) {
    846 				puffs__framev_output(pu, pfctrl, fio);
    847 				what |= PUFFS_FBIO_WRITE;
    848 			}
    849 			if (what)
    850 				puffs__framev_notify(fio, what);
    851 		}
    852 
    853 		/*
    854 		 * Really free fd's now that we don't have references
    855 		 * to them.
    856 		 */
    857 		while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
    858 			LIST_REMOVE(fio, fio_entries);
    859 			free(fio);
    860 		}
    861 	}
    862 
    863 	if (puffs__cc_restoremain(pu) == -1)
    864 		warn("cannot restore main context.  impending doom");
    865 }
    866 
    867 int
    868 puffs_mainloop(struct puffs_usermount *pu)
    869 {
    870 	struct puffs_fctrl_io *fio;
    871 	struct puffs_cc *pcc;
    872 	struct kevent *curev;
    873 	int sverrno;
    874 
    875 	assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
    876 
    877 	pu->pu_kq = kqueue();
    878 	if (pu->pu_kq == -1)
    879 		goto out;
    880 	pu->pu_state |= PU_HASKQ;
    881 
    882 	puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
    883 	if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
    884 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
    885 	    &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
    886 		goto out;
    887 
    888 	curev = realloc(pu->pu_evs, (2*pu->pu_nfds)*sizeof(struct kevent));
    889 	if (curev == NULL)
    890 		goto out;
    891 	pu->pu_evs = curev;
    892 
    893 	LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
    894 		EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
    895 		    0, 0, (uintptr_t)fio);
    896 		curev++;
    897 		EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
    898 		    0, 0, (uintptr_t)fio);
    899 		curev++;
    900 	}
    901 	if (kevent(pu->pu_kq, pu->pu_evs, 2*pu->pu_nfds, NULL, 0, NULL) == -1)
    902 		goto out;
    903 
    904 	pu->pu_state |= PU_INLOOP;
    905 
    906 	/*
    907 	 * Create alternate execution context and jump to it.  Note
    908 	 * that we come "out" of savemain twice.  Where we come out
    909 	 * of it depends on the architecture.  If the return address is
    910 	 * stored on the stack, we jump out from puffs_cc_continue(),
    911 	 * for a register return address from puffs__cc_savemain().
    912 	 * PU_MAINRESTORE makes sure we DTRT in both cases.
    913 	 */
    914 	if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
    915 		goto out;
    916 	}
    917 	if (puffs__cc_savemain(pu) == -1) {
    918 		goto out;
    919 	}
    920 	if ((pu->pu_state & PU_MAINRESTORE) == 0)
    921 		puffs_cc_continue(pcc);
    922 
    923 	finalpush(pu);
    924 	errno = 0;
    925 
    926  out:
    927 	/* store the real error for a while */
    928 	sverrno = errno;
    929 
    930 	errno = sverrno;
    931 	if (errno)
    932 		return -1;
    933 	else
    934 		return 0;
    935 }
    936