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