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