Home | History | Annotate | Line # | Download | only in puffs
puffs_msgif.c revision 1.19.2.7
      1  1.19.2.7       ad /*	$NetBSD: puffs_msgif.c,v 1.19.2.7 2007/10/09 13:44:18 ad Exp $	*/
      2       1.1    pooka 
      3       1.1    pooka /*
      4      1.16    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.19.2.7       ad __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.19.2.7 2007/10/09 13:44:18 ad Exp $");
     34       1.1    pooka 
     35       1.1    pooka #include <sys/param.h>
     36      1.16    pooka #include <sys/fstrans.h>
     37       1.1    pooka #include <sys/malloc.h>
     38       1.1    pooka #include <sys/mount.h>
     39       1.1    pooka #include <sys/vnode.h>
     40       1.1    pooka #include <sys/lock.h>
     41  1.19.2.1       ad #include <sys/proc.h>
     42       1.1    pooka 
     43       1.1    pooka #include <fs/puffs/puffs_msgif.h>
     44       1.1    pooka #include <fs/puffs/puffs_sys.h>
     45       1.1    pooka 
     46  1.19.2.2       ad /*
     47  1.19.2.2       ad  * waitq data structures
     48  1.19.2.2       ad  */
     49  1.19.2.2       ad 
     50  1.19.2.2       ad /*
     51  1.19.2.2       ad  * While a request is going to userspace, park the caller within the
     52  1.19.2.2       ad  * kernel.  This is the kernel counterpart of "struct puffs_req".
     53  1.19.2.2       ad  */
     54  1.19.2.2       ad struct puffs_park {
     55  1.19.2.2       ad 	struct puffs_req	*park_preq;	/* req followed by buf	*/
     56  1.19.2.2       ad 	uint64_t		park_id;	/* duplicate of preq_id */
     57  1.19.2.2       ad 
     58  1.19.2.2       ad 	size_t			park_copylen;	/* userspace copylength	*/
     59  1.19.2.2       ad 	size_t			park_maxlen;	/* max size in comeback */
     60  1.19.2.2       ad 
     61  1.19.2.2       ad 	parkdone_fn		park_done;
     62  1.19.2.2       ad 	void			*park_donearg;
     63  1.19.2.2       ad 
     64  1.19.2.2       ad 	int			park_flags;
     65  1.19.2.2       ad 	int			park_refcount;
     66  1.19.2.2       ad 
     67  1.19.2.2       ad 	kcondvar_t		park_cv;
     68  1.19.2.2       ad 	kmutex_t		park_mtx;
     69  1.19.2.2       ad 
     70  1.19.2.2       ad 	TAILQ_ENTRY(puffs_park) park_entries;
     71  1.19.2.2       ad };
     72  1.19.2.2       ad #define PARKFLAG_WAITERGONE	0x01
     73  1.19.2.2       ad #define PARKFLAG_DONE		0x02
     74  1.19.2.2       ad #define PARKFLAG_ONQUEUE1	0x04
     75  1.19.2.2       ad #define PARKFLAG_ONQUEUE2	0x08
     76  1.19.2.2       ad #define PARKFLAG_CALL		0x10
     77  1.19.2.3       ad #define PARKFLAG_WANTREPLY	0x20
     78  1.19.2.2       ad 
     79  1.19.2.6       ad static pool_cache_t parkpc;
     80  1.19.2.2       ad 
     81  1.19.2.2       ad static int
     82  1.19.2.2       ad makepark(void *arg, void *obj, int flags)
     83  1.19.2.2       ad {
     84  1.19.2.2       ad 	struct puffs_park *park = obj;
     85  1.19.2.2       ad 
     86  1.19.2.2       ad 	mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
     87  1.19.2.2       ad 	cv_init(&park->park_cv, "puffsrpl");
     88  1.19.2.2       ad 
     89  1.19.2.2       ad 	return 0;
     90  1.19.2.2       ad }
     91  1.19.2.2       ad 
     92  1.19.2.2       ad static void
     93  1.19.2.2       ad nukepark(void *arg, void *obj)
     94  1.19.2.2       ad {
     95  1.19.2.2       ad 	struct puffs_park *park = obj;
     96  1.19.2.2       ad 
     97  1.19.2.2       ad 	cv_destroy(&park->park_cv);
     98  1.19.2.2       ad 	mutex_destroy(&park->park_mtx);
     99  1.19.2.2       ad }
    100  1.19.2.2       ad 
    101  1.19.2.2       ad void
    102  1.19.2.2       ad puffs_msgif_init()
    103  1.19.2.2       ad {
    104  1.19.2.2       ad 
    105  1.19.2.6       ad 	parkpc = pool_cache_init(sizeof(struct puffs_park), 0, 0, 0,
    106  1.19.2.6       ad 	    "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
    107  1.19.2.6       ad 	KASSERT(parkpc != NULL);	/* XXX */
    108  1.19.2.2       ad }
    109  1.19.2.2       ad 
    110  1.19.2.2       ad void
    111  1.19.2.2       ad puffs_msgif_destroy()
    112  1.19.2.2       ad {
    113  1.19.2.2       ad 
    114  1.19.2.6       ad 	pool_cache_destroy(parkpc);
    115  1.19.2.2       ad }
    116  1.19.2.2       ad 
    117  1.19.2.2       ad void *
    118  1.19.2.2       ad puffs_park_alloc(int waitok)
    119  1.19.2.2       ad {
    120  1.19.2.2       ad 	struct puffs_park *park;
    121  1.19.2.2       ad 
    122  1.19.2.6       ad 	park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
    123  1.19.2.2       ad 	if (park) {
    124  1.19.2.2       ad 		park->park_refcount = 1;
    125  1.19.2.2       ad 		mutex_enter(&park->park_mtx);
    126  1.19.2.2       ad 	}
    127  1.19.2.2       ad 
    128  1.19.2.2       ad 	return park;
    129  1.19.2.2       ad }
    130  1.19.2.2       ad 
    131  1.19.2.2       ad static void
    132  1.19.2.2       ad puffs_park_reference(struct puffs_park *park)
    133  1.19.2.2       ad {
    134  1.19.2.2       ad 
    135  1.19.2.2       ad 	mutex_enter(&park->park_mtx);
    136  1.19.2.2       ad 	park->park_refcount++;
    137  1.19.2.2       ad }
    138  1.19.2.2       ad 
    139  1.19.2.2       ad void
    140  1.19.2.2       ad puffs_park_release(void *arg, int fullnuke)
    141  1.19.2.2       ad {
    142  1.19.2.2       ad 	struct puffs_park *park = arg;
    143  1.19.2.2       ad 
    144  1.19.2.2       ad 	KASSERT(mutex_owned(&park->park_mtx));
    145  1.19.2.2       ad 	--park->park_refcount;
    146  1.19.2.2       ad 
    147  1.19.2.2       ad 	mutex_exit(&park->park_mtx);
    148  1.19.2.2       ad 	if (park->park_refcount == 0 || fullnuke)
    149  1.19.2.6       ad 		pool_cache_put(parkpc, park);
    150  1.19.2.2       ad }
    151  1.19.2.2       ad 
    152  1.19.2.2       ad #ifdef PUFFSDEBUG
    153  1.19.2.2       ad static void
    154  1.19.2.2       ad parkdump(struct puffs_park *park)
    155  1.19.2.2       ad {
    156  1.19.2.2       ad 
    157  1.19.2.2       ad 	DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
    158  1.19.2.2       ad 	    "\tcopy %zu, max %zu - done: %p/%p\n"
    159  1.19.2.2       ad 	    "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
    160  1.19.2.2       ad 	    park, park->park_preq, park->park_id,
    161  1.19.2.2       ad 	    park->park_copylen, park->park_maxlen,
    162  1.19.2.2       ad 	    park->park_done, park->park_donearg,
    163  1.19.2.2       ad 	    park->park_flags, park->park_refcount,
    164  1.19.2.2       ad 	    &park->park_cv, &park->park_mtx));
    165  1.19.2.2       ad }
    166  1.19.2.2       ad 
    167  1.19.2.2       ad static void
    168  1.19.2.2       ad parkqdump(struct puffs_wq *q, int dumpall)
    169  1.19.2.2       ad {
    170  1.19.2.2       ad 	struct puffs_park *park;
    171  1.19.2.2       ad 	int total = 0;
    172  1.19.2.2       ad 
    173  1.19.2.2       ad 	TAILQ_FOREACH(park, q, park_entries) {
    174  1.19.2.2       ad 		if (dumpall)
    175  1.19.2.2       ad 			parkdump(park);
    176  1.19.2.2       ad 		total++;
    177  1.19.2.2       ad 	}
    178  1.19.2.3       ad 	DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
    179  1.19.2.2       ad 
    180  1.19.2.2       ad }
    181  1.19.2.2       ad #endif /* PUFFSDEBUG */
    182  1.19.2.2       ad 
    183  1.19.2.2       ad /*
    184  1.19.2.2       ad  * Converts a non-FAF op to a FAF.  This simply involves making copies
    185  1.19.2.2       ad  * of the park and request structures and tagging the request as a FAF.
    186  1.19.2.2       ad  * It is safe to block here, since the original op is not a FAF.
    187  1.19.2.2       ad  */
    188  1.19.2.2       ad static void
    189  1.19.2.2       ad puffs_reqtofaf(struct puffs_park *park)
    190  1.19.2.2       ad {
    191  1.19.2.2       ad 	struct puffs_req *newpreq;
    192  1.19.2.2       ad 
    193  1.19.2.2       ad 	KASSERT((park->park_preq->preq_opclass & PUFFSOPFLAG_FAF) == 0);
    194  1.19.2.2       ad 
    195  1.19.2.2       ad 	MALLOC(newpreq, struct puffs_req *, park->park_copylen,
    196  1.19.2.2       ad 	    M_PUFFS, M_ZERO | M_WAITOK);
    197  1.19.2.2       ad 
    198  1.19.2.2       ad 	memcpy(newpreq, park->park_preq, park->park_copylen);
    199  1.19.2.2       ad 
    200  1.19.2.2       ad 	park->park_preq = newpreq;
    201  1.19.2.2       ad 	park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
    202  1.19.2.3       ad 	park->park_flags &= ~PARKFLAG_WANTREPLY;
    203  1.19.2.2       ad }
    204  1.19.2.2       ad 
    205       1.1    pooka 
    206       1.1    pooka /*
    207       1.1    pooka  * kernel-user-kernel waitqueues
    208       1.1    pooka  */
    209       1.1    pooka 
    210  1.19.2.3       ad static int touser(struct puffs_mount *, struct puffs_park *, uint64_t);
    211       1.1    pooka 
    212       1.4    pooka uint64_t
    213       1.1    pooka puffs_getreqid(struct puffs_mount *pmp)
    214       1.1    pooka {
    215      1.14    pooka 	uint64_t rv;
    216       1.1    pooka 
    217  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    218       1.1    pooka 	rv = pmp->pmp_nextreq++;
    219  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    220       1.1    pooka 
    221       1.1    pooka 	return rv;
    222       1.1    pooka }
    223       1.1    pooka 
    224       1.1    pooka /* vfs request */
    225       1.1    pooka int
    226       1.1    pooka puffs_vfstouser(struct puffs_mount *pmp, int optype, void *kbuf, size_t buflen)
    227       1.1    pooka {
    228  1.19.2.2       ad 	struct puffs_park *park;
    229       1.1    pooka 
    230  1.19.2.2       ad 	park = puffs_park_alloc(1);
    231  1.19.2.2       ad 	park->park_preq = kbuf;
    232       1.1    pooka 
    233  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_VFS;
    234  1.19.2.2       ad 	park->park_preq->preq_optype = optype;
    235       1.1    pooka 
    236  1.19.2.2       ad 	park->park_maxlen = park->park_copylen = buflen;
    237  1.19.2.2       ad 	park->park_flags = 0;
    238       1.1    pooka 
    239  1.19.2.3       ad 	return touser(pmp, park, puffs_getreqid(pmp));
    240       1.1    pooka }
    241       1.1    pooka 
    242      1.16    pooka void
    243      1.16    pooka puffs_suspendtouser(struct puffs_mount *pmp, int status)
    244      1.16    pooka {
    245      1.16    pooka 	struct puffs_vfsreq_suspend *pvfsr_susp;
    246  1.19.2.2       ad 	struct puffs_park *park;
    247      1.16    pooka 
    248      1.16    pooka 	pvfsr_susp = malloc(sizeof(struct puffs_vfsreq_suspend),
    249      1.16    pooka 	    M_PUFFS, M_WAITOK | M_ZERO);
    250  1.19.2.2       ad 	park = puffs_park_alloc(1);
    251      1.16    pooka 
    252      1.16    pooka 	pvfsr_susp->pvfsr_status = status;
    253  1.19.2.2       ad 	park->park_preq = (struct puffs_req *)pvfsr_susp;
    254      1.16    pooka 
    255  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_VFS | PUFFSOPFLAG_FAF;
    256  1.19.2.2       ad 	park->park_preq->preq_optype = PUFFS_VFS_SUSPEND;
    257      1.16    pooka 
    258  1.19.2.2       ad 	park->park_maxlen = park->park_copylen
    259      1.16    pooka 	    = sizeof(struct puffs_vfsreq_suspend);
    260  1.19.2.2       ad 	park->park_flags = 0;
    261      1.16    pooka 
    262  1.19.2.3       ad 	(void)touser(pmp, park, 0);
    263      1.16    pooka }
    264      1.16    pooka 
    265       1.1    pooka /*
    266       1.1    pooka  * vnode level request
    267       1.1    pooka  */
    268       1.1    pooka int
    269       1.1    pooka puffs_vntouser(struct puffs_mount *pmp, int optype,
    270  1.19.2.3       ad 	void *kbuf, size_t buflen, size_t maxdelta,
    271  1.19.2.3       ad 	struct vnode *vp_opc, struct vnode *vp_aux)
    272       1.1    pooka {
    273  1.19.2.2       ad 	struct puffs_park *park;
    274  1.19.2.3       ad 	struct puffs_req *preq;
    275  1.19.2.3       ad 	void *cookie = VPTOPNC(vp_opc);
    276  1.19.2.3       ad 	struct puffs_node *pnode;
    277  1.19.2.3       ad 	int rv;
    278       1.1    pooka 
    279  1.19.2.2       ad 	park = puffs_park_alloc(1);
    280  1.19.2.2       ad 	park->park_preq = kbuf;
    281       1.9    pooka 
    282  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_VN;
    283  1.19.2.2       ad 	park->park_preq->preq_optype = optype;
    284  1.19.2.2       ad 	park->park_preq->preq_cookie = cookie;
    285  1.19.2.2       ad 
    286  1.19.2.2       ad 	park->park_copylen = buflen;
    287  1.19.2.2       ad 	park->park_maxlen = buflen + maxdelta;
    288  1.19.2.2       ad 	park->park_flags = 0;
    289       1.1    pooka 
    290  1.19.2.3       ad 	rv = touser(pmp, park, puffs_getreqid(pmp));
    291  1.19.2.3       ad 
    292  1.19.2.3       ad 	/*
    293  1.19.2.3       ad 	 * Check if the user server requests that inactive be called
    294  1.19.2.3       ad 	 * when the time is right.
    295  1.19.2.3       ad 	 */
    296  1.19.2.3       ad 	preq = park->park_preq;
    297  1.19.2.3       ad 	if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N1) {
    298  1.19.2.3       ad 		pnode = vp_opc->v_data;
    299  1.19.2.3       ad 		pnode->pn_stat |= PNODE_DOINACT;
    300  1.19.2.3       ad 	}
    301  1.19.2.3       ad 	if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N2) {
    302  1.19.2.3       ad 		/* if no vp_aux, just ignore */
    303  1.19.2.3       ad 		if (vp_aux) {
    304  1.19.2.3       ad 			pnode = vp_aux->v_data;
    305  1.19.2.3       ad 			pnode->pn_stat |= PNODE_DOINACT;
    306  1.19.2.3       ad 		}
    307  1.19.2.3       ad 	}
    308  1.19.2.3       ad 	if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1) {
    309  1.19.2.3       ad 		pnode = vp_opc->v_data;
    310  1.19.2.3       ad 		pnode->pn_stat |= PNODE_NOREFS;
    311  1.19.2.3       ad 	}
    312  1.19.2.3       ad 	if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2) {
    313  1.19.2.3       ad 		/* if no vp_aux, just ignore */
    314  1.19.2.3       ad 		if (vp_aux) {
    315  1.19.2.3       ad 			pnode = vp_aux->v_data;
    316  1.19.2.3       ad 			pnode->pn_stat |= PNODE_NOREFS;
    317  1.19.2.3       ad 		}
    318  1.19.2.3       ad 	}
    319  1.19.2.3       ad 
    320  1.19.2.3       ad 	return rv;
    321       1.1    pooka }
    322       1.1    pooka 
    323  1.19.2.7       ad int
    324  1.19.2.7       ad puffs_cookietouser(struct puffs_mount *pmp, int optype,
    325  1.19.2.7       ad 	void *kbuf, size_t buflen, void *cookie, int faf)
    326  1.19.2.7       ad {
    327  1.19.2.7       ad 	struct puffs_park *park;
    328  1.19.2.7       ad 
    329  1.19.2.7       ad 	park = puffs_park_alloc(1);
    330  1.19.2.7       ad 	park->park_preq = kbuf;
    331  1.19.2.7       ad 
    332  1.19.2.7       ad 	park->park_preq->preq_opclass = PUFFSOP_VN |(faf ? PUFFSOPFLAG_FAF : 0);
    333  1.19.2.7       ad 	park->park_preq->preq_optype = optype;
    334  1.19.2.7       ad 	park->park_preq->preq_cookie = cookie;
    335  1.19.2.7       ad 
    336  1.19.2.7       ad 	park->park_copylen = park->park_maxlen = buflen;
    337  1.19.2.7       ad 	park->park_flags = 0;
    338  1.19.2.7       ad 
    339  1.19.2.7       ad 	return touser(pmp, park, puffs_getreqid(pmp));
    340  1.19.2.7       ad }
    341  1.19.2.7       ad 
    342       1.1    pooka /*
    343       1.1    pooka  * vnode level request, caller-controller req id
    344       1.1    pooka  */
    345       1.1    pooka int
    346       1.1    pooka puffs_vntouser_req(struct puffs_mount *pmp, int optype,
    347  1.19.2.3       ad 	void *kbuf, size_t buflen, size_t maxdelta,
    348  1.19.2.3       ad 	uint64_t reqid, struct vnode *vp_opc, struct vnode *vp_aux)
    349       1.1    pooka {
    350  1.19.2.2       ad 	struct puffs_park *park;
    351  1.19.2.3       ad 	void *cookie = VPTOPNC(vp_opc);
    352       1.9    pooka 
    353  1.19.2.2       ad 	park = puffs_park_alloc(1);
    354  1.19.2.2       ad 	park->park_preq = kbuf;
    355       1.1    pooka 
    356  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_VN;
    357  1.19.2.2       ad 	park->park_preq->preq_optype = optype;
    358  1.19.2.2       ad 	park->park_preq->preq_cookie = cookie;
    359  1.19.2.2       ad 
    360  1.19.2.2       ad 	park->park_copylen = buflen;
    361  1.19.2.2       ad 	park->park_maxlen = buflen + maxdelta;
    362  1.19.2.2       ad 	park->park_flags = 0;
    363       1.1    pooka 
    364  1.19.2.3       ad 	return touser(pmp, park, reqid);
    365       1.1    pooka }
    366       1.1    pooka 
    367  1.19.2.2       ad void
    368  1.19.2.2       ad puffs_vntouser_call(struct puffs_mount *pmp, int optype,
    369  1.19.2.3       ad 	void *kbuf, size_t buflen, size_t maxdelta,
    370  1.19.2.2       ad 	parkdone_fn donefn, void *donearg,
    371  1.19.2.3       ad 	struct vnode *vp_opc, struct vnode *vp_aux)
    372       1.1    pooka {
    373  1.19.2.2       ad 	struct puffs_park *park;
    374  1.19.2.3       ad 	void *cookie = VPTOPNC(vp_opc);
    375       1.1    pooka 
    376  1.19.2.2       ad 	park = puffs_park_alloc(1);
    377  1.19.2.2       ad 	park->park_preq = kbuf;
    378       1.9    pooka 
    379  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_VN;
    380  1.19.2.2       ad 	park->park_preq->preq_optype = optype;
    381  1.19.2.2       ad 	park->park_preq->preq_cookie = cookie;
    382  1.19.2.2       ad 
    383  1.19.2.2       ad 	park->park_copylen = buflen;
    384  1.19.2.2       ad 	park->park_maxlen = buflen + maxdelta;
    385  1.19.2.2       ad 	park->park_done = donefn;
    386  1.19.2.2       ad 	park->park_donearg = donearg;
    387  1.19.2.2       ad 	park->park_flags = PARKFLAG_CALL;
    388       1.1    pooka 
    389  1.19.2.3       ad 	(void) touser(pmp, park, puffs_getreqid(pmp));
    390       1.1    pooka }
    391       1.1    pooka 
    392       1.1    pooka /*
    393       1.4    pooka  * Notice: kbuf will be free'd later.  I must be allocated from the
    394       1.4    pooka  * kernel heap and it's ownership is shifted to this function from
    395       1.4    pooka  * now on, i.e. the caller is not allowed to use it anymore!
    396       1.4    pooka  */
    397       1.4    pooka void
    398       1.4    pooka puffs_vntouser_faf(struct puffs_mount *pmp, int optype,
    399  1.19.2.3       ad 	void *kbuf, size_t buflen, struct vnode *vp_opc)
    400       1.4    pooka {
    401  1.19.2.2       ad 	struct puffs_park *park;
    402  1.19.2.3       ad 	void *cookie = VPTOPNC(vp_opc);
    403       1.4    pooka 
    404       1.4    pooka 	/* XXX: is it allowable to sleep here? */
    405  1.19.2.2       ad 	park = puffs_park_alloc(0);
    406  1.19.2.2       ad 	if (park == NULL)
    407       1.4    pooka 		return; /* 2bad */
    408       1.4    pooka 
    409  1.19.2.2       ad 	park->park_preq = kbuf;
    410       1.9    pooka 
    411  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_VN | PUFFSOPFLAG_FAF;
    412  1.19.2.2       ad 	park->park_preq->preq_optype = optype;
    413  1.19.2.2       ad 	park->park_preq->preq_cookie = cookie;
    414       1.9    pooka 
    415  1.19.2.2       ad 	park->park_maxlen = park->park_copylen = buflen;
    416  1.19.2.2       ad 	park->park_flags = 0;
    417       1.4    pooka 
    418  1.19.2.3       ad 	(void)touser(pmp, park, 0);
    419  1.19.2.2       ad }
    420  1.19.2.2       ad 
    421  1.19.2.2       ad void
    422  1.19.2.2       ad puffs_cacheop(struct puffs_mount *pmp, struct puffs_park *park,
    423  1.19.2.2       ad 	struct puffs_cacheinfo *pcinfo, size_t pcilen, void *cookie)
    424  1.19.2.2       ad {
    425  1.19.2.2       ad 
    426  1.19.2.2       ad 	park->park_preq = (struct puffs_req *)pcinfo;
    427  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_CACHE | PUFFSOPFLAG_FAF;
    428  1.19.2.2       ad 	park->park_preq->preq_optype = PCACHE_TYPE_WRITE; /* XXX */
    429  1.19.2.2       ad 	park->park_preq->preq_cookie = cookie;
    430  1.19.2.2       ad 
    431  1.19.2.2       ad 	park->park_maxlen = park->park_copylen = pcilen;
    432  1.19.2.2       ad 	park->park_flags = 0;
    433  1.19.2.2       ad 
    434  1.19.2.3       ad 	(void)touser(pmp, park, 0);
    435       1.4    pooka }
    436       1.4    pooka 
    437  1.19.2.7       ad void
    438  1.19.2.7       ad puffs_errnotify(struct puffs_mount *pmp, uint8_t type, int error,
    439  1.19.2.7       ad 	const char *str, void *cookie)
    440  1.19.2.7       ad {
    441  1.19.2.7       ad 	struct puffs_park *park;
    442  1.19.2.7       ad 	struct puffs_error *perr;
    443  1.19.2.7       ad 
    444  1.19.2.7       ad 	park = puffs_park_alloc(1);
    445  1.19.2.7       ad 	MALLOC(perr, struct puffs_error *, sizeof(struct puffs_error),
    446  1.19.2.7       ad 	    M_PUFFS, M_ZERO | M_WAITOK);
    447  1.19.2.7       ad 
    448  1.19.2.7       ad 	perr->perr_error = error;
    449  1.19.2.7       ad 	strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
    450  1.19.2.7       ad 
    451  1.19.2.7       ad 	park->park_preq = (struct puffs_req *)perr;
    452  1.19.2.7       ad 	park->park_preq->preq_opclass = PUFFSOP_ERROR | PUFFSOPFLAG_FAF;
    453  1.19.2.7       ad 	park->park_preq->preq_optype = type;
    454  1.19.2.7       ad 	park->park_preq->preq_cookie = cookie;
    455  1.19.2.7       ad 
    456  1.19.2.7       ad 	park->park_maxlen = park->park_copylen = sizeof(struct puffs_error);
    457  1.19.2.7       ad 	park->park_flags = 0;
    458  1.19.2.7       ad 
    459  1.19.2.7       ad 	(void)touser(pmp, park, 0);
    460  1.19.2.7       ad }
    461  1.19.2.7       ad 
    462       1.4    pooka /*
    463       1.1    pooka  * Wait for the userspace ping-pong game in calling process context.
    464       1.1    pooka  *
    465       1.1    pooka  * This unlocks vnodes if they are supplied.  vp1 is the vnode
    466       1.1    pooka  * before in the locking order, i.e. the one which must be locked
    467       1.1    pooka  * before accessing vp2.  This is done here so that operations are
    468       1.1    pooka  * already ordered in the queue when vnodes are unlocked (I'm not
    469       1.1    pooka  * sure if that's really necessary, but it can't hurt).  Okok, maybe
    470       1.1    pooka  * there's a slight ugly-factor also, but let's not worry about that.
    471       1.1    pooka  */
    472       1.1    pooka static int
    473  1.19.2.3       ad touser(struct puffs_mount *pmp, struct puffs_park *park, uint64_t reqid)
    474       1.1    pooka {
    475      1.19    pooka 	struct lwp *l = curlwp;
    476      1.16    pooka 	struct mount *mp;
    477       1.9    pooka 	struct puffs_req *preq;
    478      1.19    pooka 	int rv = 0;
    479       1.1    pooka 
    480      1.16    pooka 	mp = PMPTOMP(pmp);
    481  1.19.2.2       ad 	preq = park->park_preq;
    482  1.19.2.2       ad 	preq->preq_id = park->park_id = reqid;
    483  1.19.2.2       ad 	preq->preq_buflen = ALIGN(park->park_maxlen);
    484      1.19    pooka 
    485  1.19.2.3       ad 	if (PUFFSOP_WANTREPLY(preq->preq_opclass))
    486  1.19.2.3       ad 		park->park_flags |= PARKFLAG_WANTREPLY;
    487  1.19.2.3       ad 
    488      1.19    pooka 	/*
    489      1.19    pooka 	 * To support PCATCH, yet another movie: check if there are signals
    490      1.19    pooka 	 * pending and we are issueing a non-FAF.  If so, return an error
    491      1.19    pooka 	 * directly UNLESS we are issueing INACTIVE.  In that case, convert
    492      1.19    pooka 	 * it to a FAF, fire off to the file server and return an error.
    493      1.19    pooka 	 * Yes, this is bordering disgusting.  Barfbags are on me.
    494      1.19    pooka 	 */
    495  1.19.2.3       ad 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    496  1.19.2.2       ad 	   && (park->park_flags & PARKFLAG_CALL) == 0
    497      1.19    pooka 	   && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    498      1.19    pooka 		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
    499      1.19    pooka 		    && preq->preq_optype == PUFFS_VN_INACTIVE) {
    500  1.19.2.2       ad 			puffs_reqtofaf(park);
    501  1.19.2.2       ad 			DPRINTF(("puffs touser: converted to FAF %p\n", park));
    502      1.19    pooka 			rv = EINTR;
    503      1.19    pooka 		} else {
    504  1.19.2.2       ad 			puffs_park_release(park, 0);
    505      1.19    pooka 			return EINTR;
    506      1.19    pooka 		}
    507      1.19    pooka 	}
    508      1.16    pooka 
    509      1.16    pooka 	/*
    510      1.16    pooka 	 * test for suspension lock.
    511      1.16    pooka 	 *
    512      1.16    pooka 	 * Note that we *DO NOT* keep the lock, since that might block
    513      1.16    pooka 	 * lock acquiring PLUS it would give userlandia control over
    514      1.16    pooka 	 * the lock.  The operation queue enforces a strict ordering:
    515      1.16    pooka 	 * when the fs server gets in the op stream, it knows things
    516      1.16    pooka 	 * are in order.  The kernel locks can't guarantee that for
    517      1.16    pooka 	 * userspace, in any case.
    518      1.16    pooka 	 *
    519      1.16    pooka 	 * BUT: this presents a problem for ops which have a consistency
    520      1.16    pooka 	 * clause based on more than one operation.  Unfortunately such
    521      1.16    pooka 	 * operations (read, write) do not reliably work yet.
    522      1.16    pooka 	 *
    523      1.16    pooka 	 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
    524      1.18    pooka 	 *
    525      1.18    pooka 	 * XXX: and there is one more problem.  We sometimes need to
    526      1.18    pooka 	 * take a lazy lock in case the fs is suspending and we are
    527      1.18    pooka 	 * executing as the fs server context.  This might happen
    528      1.18    pooka 	 * e.g. in the case that the user server triggers a reclaim
    529      1.18    pooka 	 * in the kernel while the fs is suspending.  It's not a very
    530      1.18    pooka 	 * likely event, but it needs to be fixed some day.
    531      1.16    pooka 	 */
    532  1.19.2.2       ad 
    533  1.19.2.2       ad 	/*
    534  1.19.2.2       ad 	 * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
    535  1.19.2.2       ad 	 * the mutex here, since getpages() might be called locked.
    536  1.19.2.2       ad 	 */
    537      1.18    pooka 	fstrans_start(mp, FSTRANS_NORMAL);
    538  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    539      1.16    pooka 	fstrans_done(mp);
    540      1.16    pooka 
    541      1.13    pooka 	if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    542  1.19.2.2       ad 		mutex_exit(&pmp->pmp_lock);
    543  1.19.2.2       ad 		puffs_park_release(park, 0);
    544       1.1    pooka 		return ENXIO;
    545       1.1    pooka 	}
    546       1.1    pooka 
    547  1.19.2.2       ad #ifdef PUFFSDEBUG
    548  1.19.2.2       ad 	parkqdump(&pmp->pmp_req_touser, puffsdebug > 1);
    549  1.19.2.2       ad 	parkqdump(&pmp->pmp_req_replywait, puffsdebug > 1);
    550  1.19.2.2       ad #endif
    551  1.19.2.2       ad 
    552  1.19.2.2       ad 	TAILQ_INSERT_TAIL(&pmp->pmp_req_touser, park, park_entries);
    553  1.19.2.2       ad 	park->park_flags |= PARKFLAG_ONQUEUE1;
    554  1.19.2.3       ad 	puffs_mp_reference(pmp);
    555  1.19.2.3       ad 	pmp->pmp_req_touser_count++;
    556  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    557       1.1    pooka 
    558  1.19.2.2       ad 	DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
    559  1.19.2.2       ad 	    "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
    560  1.19.2.2       ad 	    preq->preq_opclass, preq->preq_optype, park->park_flags));
    561      1.15    pooka 
    562  1.19.2.2       ad 	cv_broadcast(&pmp->pmp_req_waiter_cv);
    563       1.1    pooka 	selnotify(pmp->pmp_sel, 0);
    564       1.1    pooka 
    565  1.19.2.3       ad 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    566  1.19.2.2       ad 	    && (park->park_flags & PARKFLAG_CALL) == 0) {
    567      1.19    pooka 		int error;
    568      1.19    pooka 
    569  1.19.2.2       ad 		error = cv_wait_sig(&park->park_cv, &park->park_mtx);
    570      1.19    pooka 		if (error) {
    571  1.19.2.2       ad 			park->park_flags |= PARKFLAG_WAITERGONE;
    572  1.19.2.2       ad 			if (park->park_flags & PARKFLAG_DONE) {
    573  1.19.2.2       ad 				rv = preq->preq_rv;
    574  1.19.2.2       ad 				puffs_park_release(park, 0);
    575      1.19    pooka 			} else {
    576  1.19.2.2       ad 				/*
    577  1.19.2.2       ad 				 * ok, we marked it as going away, but
    578  1.19.2.2       ad 				 * still need to do queue ops.  take locks
    579  1.19.2.2       ad 				 * in correct order.
    580  1.19.2.2       ad 				 *
    581  1.19.2.2       ad 				 * We don't want to release our reference
    582  1.19.2.2       ad 				 * if it's on replywait queue to avoid error
    583  1.19.2.2       ad 				 * to file server.  putop() code will DTRT.
    584  1.19.2.2       ad 				 */
    585  1.19.2.2       ad 				KASSERT(park->park_flags &
    586  1.19.2.2       ad 				    (PARKFLAG_ONQUEUE1 | PARKFLAG_ONQUEUE2));
    587  1.19.2.2       ad 				mutex_exit(&park->park_mtx);
    588  1.19.2.2       ad 
    589  1.19.2.2       ad 				mutex_enter(&pmp->pmp_lock);
    590  1.19.2.2       ad 				mutex_enter(&park->park_mtx);
    591  1.19.2.3       ad 				if (park->park_flags & PARKFLAG_ONQUEUE1) {
    592  1.19.2.2       ad 					TAILQ_REMOVE(&pmp->pmp_req_touser,
    593  1.19.2.2       ad 					    park, park_entries);
    594  1.19.2.3       ad 					pmp->pmp_req_touser_count--;
    595  1.19.2.3       ad 					park->park_flags &= ~PARKFLAG_ONQUEUE1;
    596  1.19.2.3       ad 				}
    597  1.19.2.2       ad 				if ((park->park_flags & PARKFLAG_ONQUEUE2) == 0)
    598  1.19.2.2       ad 					puffs_park_release(park, 0);
    599  1.19.2.2       ad 				else
    600  1.19.2.2       ad 					mutex_exit(&park->park_mtx);
    601  1.19.2.2       ad 				mutex_exit(&pmp->pmp_lock);
    602  1.19.2.2       ad 
    603  1.19.2.2       ad 				rv = error;
    604      1.19    pooka 			}
    605  1.19.2.2       ad 		} else {
    606  1.19.2.2       ad 			rv = preq->preq_rv;
    607  1.19.2.2       ad 			puffs_park_release(park, 0);
    608      1.19    pooka 		}
    609  1.19.2.2       ad 
    610      1.16    pooka 		/*
    611      1.16    pooka 		 * retake the lock and release.  This makes sure (haha,
    612      1.16    pooka 		 * I'm humorous) that we don't process the same vnode in
    613      1.16    pooka 		 * multiple threads due to the locks hacks we have in
    614      1.16    pooka 		 * puffs_lock().  In reality this is well protected by
    615      1.16    pooka 		 * the biglock, but once that's gone, well, hopefully
    616      1.16    pooka 		 * this will be fixed for real.  (and when you read this
    617      1.16    pooka 		 * comment in 2017 and subsequently barf, my condolences ;).
    618      1.16    pooka 		 */
    619      1.19    pooka 		if (rv == 0 && !fstrans_is_owner(mp)) {
    620      1.17  hannken 			fstrans_start(mp, FSTRANS_NORMAL);
    621      1.16    pooka 			fstrans_done(mp);
    622      1.16    pooka 		}
    623  1.19.2.2       ad 	} else {
    624  1.19.2.2       ad 		mutex_exit(&park->park_mtx);
    625      1.16    pooka 	}
    626      1.16    pooka 
    627  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    628  1.19.2.3       ad 	puffs_mp_release(pmp);
    629  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    630      1.16    pooka 
    631      1.19    pooka 	return rv;
    632       1.1    pooka }
    633       1.1    pooka 
    634       1.1    pooka 
    635       1.9    pooka /*
    636       1.9    pooka  * getop: scan through queued requests until:
    637       1.9    pooka  *  1) max number of requests satisfied
    638       1.9    pooka  *     OR
    639       1.9    pooka  *  2) buffer runs out of space
    640       1.9    pooka  *     OR
    641       1.9    pooka  *  3) nonblocking is set AND there are no operations available
    642       1.9    pooka  *     OR
    643       1.9    pooka  *  4) at least one operation was transferred AND there are no more waiting
    644       1.9    pooka  */
    645      1.10    pooka int
    646      1.10    pooka puffs_getop(struct puffs_mount *pmp, struct puffs_reqh_get *phg, int nonblock)
    647       1.1    pooka {
    648       1.1    pooka 	struct puffs_park *park;
    649       1.9    pooka 	struct puffs_req *preq;
    650       1.9    pooka 	uint8_t *bufpos;
    651       1.9    pooka 	int error, donesome;
    652       1.9    pooka 
    653       1.9    pooka 	donesome = error = 0;
    654       1.9    pooka 	bufpos = phg->phg_buf;
    655       1.1    pooka 
    656  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    657       1.9    pooka 	while (phg->phg_nops == 0 || donesome != phg->phg_nops) {
    658       1.1    pooka  again:
    659       1.9    pooka 		if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    660       1.9    pooka 			/* if we got some, they don't really matter anymore */
    661       1.9    pooka 			error = ENXIO;
    662       1.9    pooka 			goto out;
    663       1.9    pooka 		}
    664       1.9    pooka 		if (TAILQ_EMPTY(&pmp->pmp_req_touser)) {
    665      1.12    pooka 			if (donesome)
    666      1.12    pooka 				goto out;
    667      1.12    pooka 
    668      1.12    pooka 			if (nonblock) {
    669      1.12    pooka 				error = EWOULDBLOCK;
    670       1.9    pooka 				goto out;
    671       1.9    pooka 			}
    672       1.9    pooka 
    673  1.19.2.2       ad 			error = cv_wait_sig(&pmp->pmp_req_waiter_cv,
    674  1.19.2.2       ad 			    &pmp->pmp_lock);
    675      1.11    pooka 			if (error)
    676      1.11    pooka 				goto out;
    677      1.11    pooka 			else
    678      1.11    pooka 				goto again;
    679       1.9    pooka 		}
    680       1.9    pooka 
    681       1.9    pooka 		park = TAILQ_FIRST(&pmp->pmp_req_touser);
    682  1.19.2.2       ad 		puffs_park_reference(park);
    683       1.9    pooka 
    684  1.19.2.2       ad 		/* If it's a goner, don't process any furher */
    685  1.19.2.2       ad 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    686  1.19.2.2       ad 			puffs_park_release(park, 0);
    687  1.19.2.2       ad 			continue;
    688  1.19.2.2       ad 		}
    689  1.19.2.2       ad 
    690  1.19.2.2       ad 		preq = park->park_preq;
    691       1.9    pooka 		if (phg->phg_buflen < preq->preq_buflen) {
    692       1.9    pooka 			if (!donesome)
    693       1.9    pooka 				error = E2BIG;
    694  1.19.2.2       ad 			puffs_park_release(park, 0);
    695       1.9    pooka 			goto out;
    696       1.9    pooka 		}
    697  1.19.2.2       ad 
    698       1.9    pooka 		TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
    699  1.19.2.2       ad 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    700  1.19.2.2       ad 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    701  1.19.2.3       ad 		pmp->pmp_req_touser_count--;
    702  1.19.2.3       ad 		KASSERT(pmp->pmp_req_touser_count >= 0);
    703  1.19.2.2       ad 		mutex_exit(&pmp->pmp_lock);
    704       1.9    pooka 
    705       1.9    pooka 		DPRINTF(("puffsgetop: get op %" PRIu64 " (%d.), from %p "
    706       1.9    pooka 		    "len %zu (buflen %zu), target %p\n", preq->preq_id,
    707       1.9    pooka 		    donesome, preq, park->park_copylen, preq->preq_buflen,
    708       1.9    pooka 		    bufpos));
    709       1.9    pooka 
    710       1.9    pooka 		if ((error = copyout(preq, bufpos, park->park_copylen)) != 0) {
    711  1.19.2.2       ad 			DPRINTF(("puffs_getop: copyout failed: %d\n", error));
    712       1.9    pooka 			/*
    713       1.9    pooka 			 * ok, user server is probably trying to cheat.
    714  1.19.2.2       ad 			 * stuff op back & return error to user.  We need
    715  1.19.2.2       ad 			 * to take locks in the correct order.
    716       1.9    pooka 			 */
    717  1.19.2.2       ad 			mutex_exit(&park->park_mtx);
    718  1.19.2.3       ad 
    719  1.19.2.3       ad 			/*
    720  1.19.2.3       ad 			 * XXX: ONQUEUE1 | ONQUEUE2 invariant doesn't
    721  1.19.2.3       ad 			 * hold here
    722  1.19.2.3       ad 			 */
    723  1.19.2.3       ad 
    724  1.19.2.2       ad 			mutex_enter(&pmp->pmp_lock);
    725  1.19.2.2       ad 			mutex_enter(&park->park_mtx);
    726  1.19.2.2       ad 			if ((park->park_flags & PARKFLAG_WAITERGONE) == 0) {
    727  1.19.2.2       ad 				 TAILQ_INSERT_HEAD(&pmp->pmp_req_touser, park,
    728  1.19.2.2       ad 				     park_entries);
    729  1.19.2.2       ad 				 park->park_flags |= PARKFLAG_ONQUEUE1;
    730  1.19.2.3       ad 				 pmp->pmp_req_touser_count++;
    731  1.19.2.2       ad 			}
    732       1.9    pooka 
    733  1.19.2.2       ad 			if (donesome)
    734       1.9    pooka 				error = 0;
    735  1.19.2.2       ad 			puffs_park_release(park, 0);
    736  1.19.2.2       ad 			goto out;
    737       1.9    pooka 		}
    738       1.9    pooka 		bufpos += preq->preq_buflen;
    739       1.9    pooka 		phg->phg_buflen -= preq->preq_buflen;
    740       1.9    pooka 		donesome++;
    741       1.9    pooka 
    742  1.19.2.3       ad 		/* XXXfixme: taking this lock in the wrong order */
    743  1.19.2.2       ad 		mutex_enter(&pmp->pmp_lock);
    744  1.19.2.3       ad 
    745  1.19.2.3       ad 		if (park->park_flags & PARKFLAG_WANTREPLY) {
    746  1.19.2.2       ad 			TAILQ_INSERT_TAIL(&pmp->pmp_req_replywait, park,
    747  1.19.2.2       ad 			    park_entries);
    748  1.19.2.2       ad 			park->park_flags |= PARKFLAG_ONQUEUE2;
    749  1.19.2.2       ad 			puffs_park_release(park, 0);
    750       1.9    pooka 		} else {
    751       1.9    pooka 			free(preq, M_PUFFS);
    752  1.19.2.2       ad 			puffs_park_release(park, 1);
    753       1.1    pooka 		}
    754       1.1    pooka 	}
    755       1.1    pooka 
    756       1.9    pooka  out:
    757  1.19.2.3       ad 	phg->phg_more = pmp->pmp_req_touser_count;
    758  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    759       1.1    pooka 
    760       1.9    pooka 	phg->phg_nops = donesome;
    761       1.4    pooka 
    762       1.9    pooka 	return error;
    763       1.1    pooka }
    764       1.1    pooka 
    765      1.10    pooka int
    766      1.10    pooka puffs_putop(struct puffs_mount *pmp, struct puffs_reqh_put *php)
    767       1.1    pooka {
    768       1.1    pooka 	struct puffs_park *park;
    769      1.19    pooka 	struct puffs_req tmpreq;
    770      1.19    pooka 	struct puffs_req *nextpreq;
    771       1.9    pooka 	void *userbuf;
    772       1.9    pooka 	uint64_t id;
    773       1.9    pooka 	size_t reqlen;
    774  1.19.2.2       ad 	int donesome, error, wgone, release;
    775       1.9    pooka 
    776      1.19    pooka 	donesome = error = wgone = 0;
    777       1.9    pooka 
    778       1.9    pooka 	id = php->php_id;
    779       1.9    pooka 	userbuf = php->php_buf;
    780       1.9    pooka 	reqlen = php->php_buflen;
    781       1.1    pooka 
    782  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    783       1.9    pooka 	while (donesome != php->php_nops) {
    784  1.19.2.2       ad 		release = 0;
    785      1.19    pooka #ifdef PUFFSDEBUG
    786       1.9    pooka 		DPRINTF(("puffsputop: searching for %" PRIu64 ", ubuf: %p, "
    787       1.9    pooka 		    "len %zu\n", id, userbuf, reqlen));
    788       1.9    pooka #endif
    789       1.9    pooka 		TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
    790      1.19    pooka 			if (park->park_id == id)
    791       1.9    pooka 				break;
    792       1.9    pooka 		}
    793       1.9    pooka 
    794       1.9    pooka 		if (park == NULL) {
    795  1.19.2.2       ad 			DPRINTF(("puffsputop: no request: %" PRIu64 "\n", id));
    796       1.9    pooka 			error = EINVAL;
    797       1.1    pooka 			break;
    798       1.1    pooka 		}
    799  1.19.2.2       ad 
    800  1.19.2.2       ad 		puffs_park_reference(park);
    801  1.19.2.2       ad 		if (reqlen == 0 || reqlen > park->park_maxlen) {
    802  1.19.2.2       ad 			DPRINTF(("puffsputop: invalid buffer length: "
    803  1.19.2.2       ad 			    "%zu\n", reqlen));
    804  1.19.2.2       ad 			error = E2BIG;
    805  1.19.2.2       ad 			puffs_park_release(park, 0);
    806  1.19.2.2       ad 			break;
    807  1.19.2.2       ad 		}
    808  1.19.2.2       ad 		wgone = park->park_flags & PARKFLAG_WAITERGONE;
    809  1.19.2.2       ad 
    810  1.19.2.2       ad 		/* check if it's still on the queue after acquiring lock */
    811  1.19.2.2       ad 		if (park->park_flags & PARKFLAG_ONQUEUE2) {
    812  1.19.2.2       ad 			TAILQ_REMOVE(&pmp->pmp_req_replywait, park,
    813  1.19.2.2       ad 			    park_entries);
    814  1.19.2.2       ad 			park->park_flags &= ~PARKFLAG_ONQUEUE2;
    815  1.19.2.2       ad 		}
    816  1.19.2.2       ad 
    817  1.19.2.2       ad 		mutex_exit(&pmp->pmp_lock);
    818       1.9    pooka 
    819      1.19    pooka 		/*
    820      1.19    pooka 		 * If the caller has gone south, go to next, collect
    821      1.19    pooka 		 * $200 and free the structure there instead of wakeup.
    822  1.19.2.2       ad 		 * We also need to copyin the header info.  Flag structure
    823  1.19.2.2       ad 		 * release to mode total and utter destruction.
    824      1.19    pooka 		 */
    825  1.19.2.2       ad 		if (wgone) {
    826      1.19    pooka 			DPRINTF(("puffs_putop: bad service - waiter gone for "
    827      1.19    pooka 			    "park %p\n", park));
    828      1.19    pooka 			error = copyin(userbuf, &tmpreq,
    829      1.19    pooka 			    sizeof(struct puffs_req));
    830  1.19.2.2       ad 			release = 1;
    831      1.19    pooka 			if (error)
    832      1.19    pooka 				goto loopout;
    833      1.19    pooka 			nextpreq = &tmpreq;
    834      1.19    pooka 			goto next;
    835      1.19    pooka 		}
    836      1.19    pooka 
    837       1.9    pooka 		DPRINTF(("puffsputpop: copyin from %p to %p, len %zu\n",
    838       1.9    pooka 		    userbuf, park->park_preq, reqlen));
    839       1.9    pooka 		error = copyin(userbuf, park->park_preq, reqlen);
    840       1.9    pooka 		if (error)
    841       1.9    pooka 			goto loopout;
    842      1.19    pooka 		nextpreq = park->park_preq;
    843       1.9    pooka 
    844      1.19    pooka  next:
    845       1.9    pooka 		/* all's well, prepare for next op */
    846      1.19    pooka 		id = nextpreq->preq_id;
    847      1.19    pooka 		reqlen = nextpreq->preq_buflen;
    848      1.19    pooka 		userbuf = nextpreq->preq_nextbuf;
    849       1.9    pooka 		donesome++;
    850       1.9    pooka 
    851       1.9    pooka  loopout:
    852  1.19.2.3       ad 		if (error && !wgone)
    853       1.9    pooka 			park->park_preq->preq_rv = error;
    854       1.1    pooka 
    855  1.19.2.2       ad 		if (park->park_flags & PARKFLAG_CALL) {
    856  1.19.2.5       ad 			DPRINTF(("puffsputopt: call for %p, arg %p\n",
    857  1.19.2.5       ad 			    park->park_preq, park->park_donearg));
    858  1.19.2.7       ad 			park->park_done(pmp,park->park_preq,park->park_donearg);
    859  1.19.2.2       ad 			release = 1;
    860      1.19    pooka 		}
    861      1.19    pooka 
    862  1.19.2.2       ad 		if (!wgone) {
    863  1.19.2.2       ad 			DPRINTF(("puffs_putop: flagging done for "
    864  1.19.2.2       ad 			    "park %p\n", park));
    865  1.19.2.2       ad 
    866  1.19.2.2       ad 			cv_signal(&park->park_cv);
    867  1.19.2.2       ad 		}
    868  1.19.2.3       ad 		park->park_flags |= PARKFLAG_DONE;
    869  1.19.2.2       ad 		puffs_park_release(park, release);
    870  1.19.2.2       ad 
    871  1.19.2.2       ad 		mutex_enter(&pmp->pmp_lock);
    872       1.9    pooka 		if (error)
    873       1.9    pooka 			break;
    874      1.19    pooka 		wgone = 0;
    875       1.1    pooka 	}
    876       1.1    pooka 
    877  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    878       1.9    pooka 	php->php_nops -= donesome;
    879       1.1    pooka 
    880       1.1    pooka 	return error;
    881       1.1    pooka }
    882       1.1    pooka 
    883  1.19.2.2       ad /*
    884  1.19.2.2       ad  * We're dead, kaput, RIP, slightly more than merely pining for the
    885  1.19.2.2       ad  * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
    886  1.19.2.2       ad  * our maker, ceased to be, etcetc.  YASD.  It's a dead FS!
    887  1.19.2.2       ad  *
    888  1.19.2.2       ad  * Caller must hold puffs mutex.
    889  1.19.2.2       ad  */
    890  1.19.2.2       ad void
    891  1.19.2.2       ad puffs_userdead(struct puffs_mount *pmp)
    892  1.19.2.2       ad {
    893  1.19.2.3       ad 	struct puffs_park *park, *park_next;
    894  1.19.2.2       ad 
    895  1.19.2.2       ad 	/*
    896  1.19.2.2       ad 	 * Mark filesystem status as dying so that operations don't
    897  1.19.2.2       ad 	 * attempt to march to userspace any longer.
    898  1.19.2.2       ad 	 */
    899  1.19.2.2       ad 	pmp->pmp_status = PUFFSTAT_DYING;
    900  1.19.2.2       ad 
    901  1.19.2.2       ad 	/* signal waiters on REQUEST TO file server queue */
    902  1.19.2.3       ad 	for (park = TAILQ_FIRST(&pmp->pmp_req_touser); park; park = park_next) {
    903  1.19.2.2       ad 		uint8_t opclass;
    904  1.19.2.2       ad 
    905  1.19.2.2       ad 		puffs_park_reference(park);
    906  1.19.2.3       ad 		park_next = TAILQ_NEXT(park, park_entries);
    907  1.19.2.2       ad 
    908  1.19.2.2       ad 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    909  1.19.2.2       ad 		TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
    910  1.19.2.2       ad 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    911  1.19.2.3       ad 		pmp->pmp_req_touser_count--;
    912  1.19.2.2       ad 
    913  1.19.2.3       ad 		/*
    914  1.19.2.3       ad 		 * If the waiter is gone, we may *NOT* access preq anymore.
    915  1.19.2.3       ad 		 */
    916  1.19.2.3       ad 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    917  1.19.2.3       ad 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    918  1.19.2.3       ad 			KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    919  1.19.2.3       ad 			puffs_park_release(park, 0);
    920  1.19.2.2       ad 		} else {
    921  1.19.2.3       ad 			opclass = park->park_preq->preq_opclass;
    922  1.19.2.2       ad 			park->park_preq->preq_rv = ENXIO;
    923  1.19.2.3       ad 
    924  1.19.2.3       ad 			if (park->park_flags & PARKFLAG_CALL) {
    925  1.19.2.7       ad 				park->park_done(pmp, park->park_preq,
    926  1.19.2.3       ad 				    park->park_donearg);
    927  1.19.2.3       ad 				puffs_park_release(park, 1);
    928  1.19.2.3       ad 			} else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
    929  1.19.2.3       ad 				free(park->park_preq, M_PUFFS);
    930  1.19.2.3       ad 				puffs_park_release(park, 1);
    931  1.19.2.3       ad 			} else {
    932  1.19.2.3       ad 				park->park_preq->preq_rv = ENXIO;
    933  1.19.2.3       ad 				cv_signal(&park->park_cv);
    934  1.19.2.3       ad 				puffs_park_release(park, 0);
    935  1.19.2.3       ad 			}
    936  1.19.2.2       ad 		}
    937  1.19.2.2       ad 	}
    938  1.19.2.2       ad 
    939  1.19.2.2       ad 	/* signal waiters on RESPONSE FROM file server queue */
    940  1.19.2.3       ad 	for (park=TAILQ_FIRST(&pmp->pmp_req_replywait); park; park=park_next) {
    941  1.19.2.2       ad 		puffs_park_reference(park);
    942  1.19.2.3       ad 		park_next = TAILQ_NEXT(park, park_entries);
    943  1.19.2.2       ad 
    944  1.19.2.2       ad 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
    945  1.19.2.3       ad 		KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    946  1.19.2.2       ad 
    947  1.19.2.2       ad 		TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
    948  1.19.2.2       ad 		park->park_flags &= ~PARKFLAG_ONQUEUE2;
    949  1.19.2.2       ad 
    950  1.19.2.3       ad 		/*
    951  1.19.2.3       ad 		 * If the waiter is gone, we may *NOT* access preq anymore.
    952  1.19.2.3       ad 		 */
    953  1.19.2.3       ad 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    954  1.19.2.3       ad 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    955  1.19.2.2       ad 			puffs_park_release(park, 0);
    956  1.19.2.3       ad 		} else {
    957  1.19.2.3       ad 			park->park_preq->preq_rv = ENXIO;
    958  1.19.2.3       ad 			if (park->park_flags & PARKFLAG_CALL) {
    959  1.19.2.7       ad 				park->park_done(pmp, park->park_preq,
    960  1.19.2.3       ad 				    park->park_donearg);
    961  1.19.2.3       ad 				puffs_park_release(park, 1);
    962  1.19.2.3       ad 			} else {
    963  1.19.2.3       ad 				cv_signal(&park->park_cv);
    964  1.19.2.3       ad 				puffs_park_release(park, 0);
    965  1.19.2.3       ad 			}
    966  1.19.2.2       ad 		}
    967  1.19.2.2       ad 	}
    968  1.19.2.2       ad }
    969