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