puffs_msgif.c revision 1.19.2.6       1  1.19.2.6       ad /*	$NetBSD: puffs_msgif.c,v 1.19.2.6 2007/09/01 12:56:47 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.6       ad __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.19.2.6 2007/09/01 12:56:47 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.1    pooka /*
    324       1.1    pooka  * vnode level request, caller-controller req id
    325       1.1    pooka  */
    326       1.1    pooka int
    327       1.1    pooka puffs_vntouser_req(struct puffs_mount *pmp, int optype,
    328  1.19.2.3       ad 	void *kbuf, size_t buflen, size_t maxdelta,
    329  1.19.2.3       ad 	uint64_t reqid, struct vnode *vp_opc, struct vnode *vp_aux)
    330       1.1    pooka {
    331  1.19.2.2       ad 	struct puffs_park *park;
    332  1.19.2.3       ad 	void *cookie = VPTOPNC(vp_opc);
    333       1.9    pooka 
    334  1.19.2.2       ad 	park = puffs_park_alloc(1);
    335  1.19.2.2       ad 	park->park_preq = kbuf;
    336       1.1    pooka 
    337  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_VN;
    338  1.19.2.2       ad 	park->park_preq->preq_optype = optype;
    339  1.19.2.2       ad 	park->park_preq->preq_cookie = cookie;
    340  1.19.2.2       ad 
    341  1.19.2.2       ad 	park->park_copylen = buflen;
    342  1.19.2.2       ad 	park->park_maxlen = buflen + maxdelta;
    343  1.19.2.2       ad 	park->park_flags = 0;
    344       1.1    pooka 
    345  1.19.2.3       ad 	return touser(pmp, park, reqid);
    346       1.1    pooka }
    347       1.1    pooka 
    348  1.19.2.2       ad void
    349  1.19.2.2       ad puffs_vntouser_call(struct puffs_mount *pmp, int optype,
    350  1.19.2.3       ad 	void *kbuf, size_t buflen, size_t maxdelta,
    351  1.19.2.2       ad 	parkdone_fn donefn, void *donearg,
    352  1.19.2.3       ad 	struct vnode *vp_opc, struct vnode *vp_aux)
    353       1.1    pooka {
    354  1.19.2.2       ad 	struct puffs_park *park;
    355  1.19.2.3       ad 	void *cookie = VPTOPNC(vp_opc);
    356       1.1    pooka 
    357  1.19.2.2       ad 	park = puffs_park_alloc(1);
    358  1.19.2.2       ad 	park->park_preq = kbuf;
    359       1.9    pooka 
    360  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_VN;
    361  1.19.2.2       ad 	park->park_preq->preq_optype = optype;
    362  1.19.2.2       ad 	park->park_preq->preq_cookie = cookie;
    363  1.19.2.2       ad 
    364  1.19.2.2       ad 	park->park_copylen = buflen;
    365  1.19.2.2       ad 	park->park_maxlen = buflen + maxdelta;
    366  1.19.2.2       ad 	park->park_done = donefn;
    367  1.19.2.2       ad 	park->park_donearg = donearg;
    368  1.19.2.2       ad 	park->park_flags = PARKFLAG_CALL;
    369       1.1    pooka 
    370  1.19.2.3       ad 	(void) touser(pmp, park, puffs_getreqid(pmp));
    371       1.1    pooka }
    372       1.1    pooka 
    373       1.1    pooka /*
    374       1.4    pooka  * Notice: kbuf will be free'd later.  I must be allocated from the
    375       1.4    pooka  * kernel heap and it's ownership is shifted to this function from
    376       1.4    pooka  * now on, i.e. the caller is not allowed to use it anymore!
    377       1.4    pooka  */
    378       1.4    pooka void
    379       1.4    pooka puffs_vntouser_faf(struct puffs_mount *pmp, int optype,
    380  1.19.2.3       ad 	void *kbuf, size_t buflen, struct vnode *vp_opc)
    381       1.4    pooka {
    382  1.19.2.2       ad 	struct puffs_park *park;
    383  1.19.2.3       ad 	void *cookie = VPTOPNC(vp_opc);
    384       1.4    pooka 
    385       1.4    pooka 	/* XXX: is it allowable to sleep here? */
    386  1.19.2.2       ad 	park = puffs_park_alloc(0);
    387  1.19.2.2       ad 	if (park == NULL)
    388       1.4    pooka 		return; /* 2bad */
    389       1.4    pooka 
    390  1.19.2.2       ad 	park->park_preq = kbuf;
    391       1.9    pooka 
    392  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_VN | PUFFSOPFLAG_FAF;
    393  1.19.2.2       ad 	park->park_preq->preq_optype = optype;
    394  1.19.2.2       ad 	park->park_preq->preq_cookie = cookie;
    395       1.9    pooka 
    396  1.19.2.2       ad 	park->park_maxlen = park->park_copylen = buflen;
    397  1.19.2.2       ad 	park->park_flags = 0;
    398       1.4    pooka 
    399  1.19.2.3       ad 	(void)touser(pmp, park, 0);
    400  1.19.2.2       ad }
    401  1.19.2.2       ad 
    402  1.19.2.2       ad void
    403  1.19.2.2       ad puffs_cacheop(struct puffs_mount *pmp, struct puffs_park *park,
    404  1.19.2.2       ad 	struct puffs_cacheinfo *pcinfo, size_t pcilen, void *cookie)
    405  1.19.2.2       ad {
    406  1.19.2.2       ad 
    407  1.19.2.2       ad 	park->park_preq = (struct puffs_req *)pcinfo;
    408  1.19.2.2       ad 	park->park_preq->preq_opclass = PUFFSOP_CACHE | PUFFSOPFLAG_FAF;
    409  1.19.2.2       ad 	park->park_preq->preq_optype = PCACHE_TYPE_WRITE; /* XXX */
    410  1.19.2.2       ad 	park->park_preq->preq_cookie = cookie;
    411  1.19.2.2       ad 
    412  1.19.2.2       ad 	park->park_maxlen = park->park_copylen = pcilen;
    413  1.19.2.2       ad 	park->park_flags = 0;
    414  1.19.2.2       ad 
    415  1.19.2.3       ad 	(void)touser(pmp, park, 0);
    416       1.4    pooka }
    417       1.4    pooka 
    418       1.4    pooka /*
    419       1.1    pooka  * Wait for the userspace ping-pong game in calling process context.
    420       1.1    pooka  *
    421       1.1    pooka  * This unlocks vnodes if they are supplied.  vp1 is the vnode
    422       1.1    pooka  * before in the locking order, i.e. the one which must be locked
    423       1.1    pooka  * before accessing vp2.  This is done here so that operations are
    424       1.1    pooka  * already ordered in the queue when vnodes are unlocked (I'm not
    425       1.1    pooka  * sure if that's really necessary, but it can't hurt).  Okok, maybe
    426       1.1    pooka  * there's a slight ugly-factor also, but let's not worry about that.
    427       1.1    pooka  */
    428       1.1    pooka static int
    429  1.19.2.3       ad touser(struct puffs_mount *pmp, struct puffs_park *park, uint64_t reqid)
    430       1.1    pooka {
    431      1.19    pooka 	struct lwp *l = curlwp;
    432      1.16    pooka 	struct mount *mp;
    433       1.9    pooka 	struct puffs_req *preq;
    434      1.19    pooka 	int rv = 0;
    435       1.1    pooka 
    436      1.16    pooka 	mp = PMPTOMP(pmp);
    437  1.19.2.2       ad 	preq = park->park_preq;
    438  1.19.2.2       ad 	preq->preq_id = park->park_id = reqid;
    439  1.19.2.2       ad 	preq->preq_buflen = ALIGN(park->park_maxlen);
    440      1.19    pooka 
    441  1.19.2.3       ad 	if (PUFFSOP_WANTREPLY(preq->preq_opclass))
    442  1.19.2.3       ad 		park->park_flags |= PARKFLAG_WANTREPLY;
    443  1.19.2.3       ad 
    444      1.19    pooka 	/*
    445      1.19    pooka 	 * To support PCATCH, yet another movie: check if there are signals
    446      1.19    pooka 	 * pending and we are issueing a non-FAF.  If so, return an error
    447      1.19    pooka 	 * directly UNLESS we are issueing INACTIVE.  In that case, convert
    448      1.19    pooka 	 * it to a FAF, fire off to the file server and return an error.
    449      1.19    pooka 	 * Yes, this is bordering disgusting.  Barfbags are on me.
    450      1.19    pooka 	 */
    451  1.19.2.3       ad 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    452  1.19.2.2       ad 	   && (park->park_flags & PARKFLAG_CALL) == 0
    453      1.19    pooka 	   && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    454      1.19    pooka 		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
    455      1.19    pooka 		    && preq->preq_optype == PUFFS_VN_INACTIVE) {
    456  1.19.2.2       ad 			puffs_reqtofaf(park);
    457  1.19.2.2       ad 			DPRINTF(("puffs touser: converted to FAF %p\n", park));
    458      1.19    pooka 			rv = EINTR;
    459      1.19    pooka 		} else {
    460  1.19.2.2       ad 			puffs_park_release(park, 0);
    461      1.19    pooka 			return EINTR;
    462      1.19    pooka 		}
    463      1.19    pooka 	}
    464      1.16    pooka 
    465      1.16    pooka 	/*
    466      1.16    pooka 	 * test for suspension lock.
    467      1.16    pooka 	 *
    468      1.16    pooka 	 * Note that we *DO NOT* keep the lock, since that might block
    469      1.16    pooka 	 * lock acquiring PLUS it would give userlandia control over
    470      1.16    pooka 	 * the lock.  The operation queue enforces a strict ordering:
    471      1.16    pooka 	 * when the fs server gets in the op stream, it knows things
    472      1.16    pooka 	 * are in order.  The kernel locks can't guarantee that for
    473      1.16    pooka 	 * userspace, in any case.
    474      1.16    pooka 	 *
    475      1.16    pooka 	 * BUT: this presents a problem for ops which have a consistency
    476      1.16    pooka 	 * clause based on more than one operation.  Unfortunately such
    477      1.16    pooka 	 * operations (read, write) do not reliably work yet.
    478      1.16    pooka 	 *
    479      1.16    pooka 	 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
    480      1.18    pooka 	 *
    481      1.18    pooka 	 * XXX: and there is one more problem.  We sometimes need to
    482      1.18    pooka 	 * take a lazy lock in case the fs is suspending and we are
    483      1.18    pooka 	 * executing as the fs server context.  This might happen
    484      1.18    pooka 	 * e.g. in the case that the user server triggers a reclaim
    485      1.18    pooka 	 * in the kernel while the fs is suspending.  It's not a very
    486      1.18    pooka 	 * likely event, but it needs to be fixed some day.
    487      1.16    pooka 	 */
    488  1.19.2.2       ad 
    489  1.19.2.2       ad 	/*
    490  1.19.2.2       ad 	 * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
    491  1.19.2.2       ad 	 * the mutex here, since getpages() might be called locked.
    492  1.19.2.2       ad 	 */
    493      1.18    pooka 	fstrans_start(mp, FSTRANS_NORMAL);
    494  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    495      1.16    pooka 	fstrans_done(mp);
    496      1.16    pooka 
    497      1.13    pooka 	if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    498  1.19.2.2       ad 		mutex_exit(&pmp->pmp_lock);
    499  1.19.2.2       ad 		puffs_park_release(park, 0);
    500       1.1    pooka 		return ENXIO;
    501       1.1    pooka 	}
    502       1.1    pooka 
    503  1.19.2.2       ad #ifdef PUFFSDEBUG
    504  1.19.2.2       ad 	parkqdump(&pmp->pmp_req_touser, puffsdebug > 1);
    505  1.19.2.2       ad 	parkqdump(&pmp->pmp_req_replywait, puffsdebug > 1);
    506  1.19.2.2       ad #endif
    507  1.19.2.2       ad 
    508  1.19.2.2       ad 	TAILQ_INSERT_TAIL(&pmp->pmp_req_touser, park, park_entries);
    509  1.19.2.2       ad 	park->park_flags |= PARKFLAG_ONQUEUE1;
    510  1.19.2.3       ad 	puffs_mp_reference(pmp);
    511  1.19.2.3       ad 	pmp->pmp_req_touser_count++;
    512  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    513       1.1    pooka 
    514  1.19.2.2       ad 	DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
    515  1.19.2.2       ad 	    "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
    516  1.19.2.2       ad 	    preq->preq_opclass, preq->preq_optype, park->park_flags));
    517      1.15    pooka 
    518  1.19.2.2       ad 	cv_broadcast(&pmp->pmp_req_waiter_cv);
    519       1.1    pooka 	selnotify(pmp->pmp_sel, 0);
    520       1.1    pooka 
    521  1.19.2.3       ad 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    522  1.19.2.2       ad 	    && (park->park_flags & PARKFLAG_CALL) == 0) {
    523      1.19    pooka 		int error;
    524      1.19    pooka 
    525  1.19.2.2       ad 		error = cv_wait_sig(&park->park_cv, &park->park_mtx);
    526      1.19    pooka 		if (error) {
    527  1.19.2.2       ad 			park->park_flags |= PARKFLAG_WAITERGONE;
    528  1.19.2.2       ad 			if (park->park_flags & PARKFLAG_DONE) {
    529  1.19.2.2       ad 				rv = preq->preq_rv;
    530  1.19.2.2       ad 				puffs_park_release(park, 0);
    531      1.19    pooka 			} else {
    532  1.19.2.2       ad 				/*
    533  1.19.2.2       ad 				 * ok, we marked it as going away, but
    534  1.19.2.2       ad 				 * still need to do queue ops.  take locks
    535  1.19.2.2       ad 				 * in correct order.
    536  1.19.2.2       ad 				 *
    537  1.19.2.2       ad 				 * We don't want to release our reference
    538  1.19.2.2       ad 				 * if it's on replywait queue to avoid error
    539  1.19.2.2       ad 				 * to file server.  putop() code will DTRT.
    540  1.19.2.2       ad 				 */
    541  1.19.2.2       ad 				KASSERT(park->park_flags &
    542  1.19.2.2       ad 				    (PARKFLAG_ONQUEUE1 | PARKFLAG_ONQUEUE2));
    543  1.19.2.2       ad 				mutex_exit(&park->park_mtx);
    544  1.19.2.2       ad 
    545  1.19.2.2       ad 				mutex_enter(&pmp->pmp_lock);
    546  1.19.2.2       ad 				mutex_enter(&park->park_mtx);
    547  1.19.2.3       ad 				if (park->park_flags & PARKFLAG_ONQUEUE1) {
    548  1.19.2.2       ad 					TAILQ_REMOVE(&pmp->pmp_req_touser,
    549  1.19.2.2       ad 					    park, park_entries);
    550  1.19.2.3       ad 					pmp->pmp_req_touser_count--;
    551  1.19.2.3       ad 					park->park_flags &= ~PARKFLAG_ONQUEUE1;
    552  1.19.2.3       ad 				}
    553  1.19.2.2       ad 				if ((park->park_flags & PARKFLAG_ONQUEUE2) == 0)
    554  1.19.2.2       ad 					puffs_park_release(park, 0);
    555  1.19.2.2       ad 				else
    556  1.19.2.2       ad 					mutex_exit(&park->park_mtx);
    557  1.19.2.2       ad 				mutex_exit(&pmp->pmp_lock);
    558  1.19.2.2       ad 
    559  1.19.2.2       ad 				rv = error;
    560      1.19    pooka 			}
    561  1.19.2.2       ad 		} else {
    562  1.19.2.2       ad 			rv = preq->preq_rv;
    563  1.19.2.2       ad 			puffs_park_release(park, 0);
    564      1.19    pooka 		}
    565  1.19.2.2       ad 
    566      1.16    pooka 		/*
    567      1.16    pooka 		 * retake the lock and release.  This makes sure (haha,
    568      1.16    pooka 		 * I'm humorous) that we don't process the same vnode in
    569      1.16    pooka 		 * multiple threads due to the locks hacks we have in
    570      1.16    pooka 		 * puffs_lock().  In reality this is well protected by
    571      1.16    pooka 		 * the biglock, but once that's gone, well, hopefully
    572      1.16    pooka 		 * this will be fixed for real.  (and when you read this
    573      1.16    pooka 		 * comment in 2017 and subsequently barf, my condolences ;).
    574      1.16    pooka 		 */
    575      1.19    pooka 		if (rv == 0 && !fstrans_is_owner(mp)) {
    576      1.17  hannken 			fstrans_start(mp, FSTRANS_NORMAL);
    577      1.16    pooka 			fstrans_done(mp);
    578      1.16    pooka 		}
    579  1.19.2.2       ad 	} else {
    580  1.19.2.2       ad 		mutex_exit(&park->park_mtx);
    581      1.16    pooka 	}
    582      1.16    pooka 
    583  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    584  1.19.2.3       ad 	puffs_mp_release(pmp);
    585  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    586      1.16    pooka 
    587      1.19    pooka 	return rv;
    588       1.1    pooka }
    589       1.1    pooka 
    590       1.1    pooka 
    591       1.9    pooka /*
    592       1.9    pooka  * getop: scan through queued requests until:
    593       1.9    pooka  *  1) max number of requests satisfied
    594       1.9    pooka  *     OR
    595       1.9    pooka  *  2) buffer runs out of space
    596       1.9    pooka  *     OR
    597       1.9    pooka  *  3) nonblocking is set AND there are no operations available
    598       1.9    pooka  *     OR
    599       1.9    pooka  *  4) at least one operation was transferred AND there are no more waiting
    600       1.9    pooka  */
    601      1.10    pooka int
    602      1.10    pooka puffs_getop(struct puffs_mount *pmp, struct puffs_reqh_get *phg, int nonblock)
    603       1.1    pooka {
    604       1.1    pooka 	struct puffs_park *park;
    605       1.9    pooka 	struct puffs_req *preq;
    606       1.9    pooka 	uint8_t *bufpos;
    607       1.9    pooka 	int error, donesome;
    608       1.9    pooka 
    609       1.9    pooka 	donesome = error = 0;
    610       1.9    pooka 	bufpos = phg->phg_buf;
    611       1.1    pooka 
    612  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    613       1.9    pooka 	while (phg->phg_nops == 0 || donesome != phg->phg_nops) {
    614       1.1    pooka  again:
    615       1.9    pooka 		if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    616       1.9    pooka 			/* if we got some, they don't really matter anymore */
    617       1.9    pooka 			error = ENXIO;
    618       1.9    pooka 			goto out;
    619       1.9    pooka 		}
    620       1.9    pooka 		if (TAILQ_EMPTY(&pmp->pmp_req_touser)) {
    621      1.12    pooka 			if (donesome)
    622      1.12    pooka 				goto out;
    623      1.12    pooka 
    624      1.12    pooka 			if (nonblock) {
    625      1.12    pooka 				error = EWOULDBLOCK;
    626       1.9    pooka 				goto out;
    627       1.9    pooka 			}
    628       1.9    pooka 
    629  1.19.2.2       ad 			error = cv_wait_sig(&pmp->pmp_req_waiter_cv,
    630  1.19.2.2       ad 			    &pmp->pmp_lock);
    631      1.11    pooka 			if (error)
    632      1.11    pooka 				goto out;
    633      1.11    pooka 			else
    634      1.11    pooka 				goto again;
    635       1.9    pooka 		}
    636       1.9    pooka 
    637       1.9    pooka 		park = TAILQ_FIRST(&pmp->pmp_req_touser);
    638  1.19.2.2       ad 		puffs_park_reference(park);
    639       1.9    pooka 
    640  1.19.2.2       ad 		/* If it's a goner, don't process any furher */
    641  1.19.2.2       ad 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    642  1.19.2.2       ad 			puffs_park_release(park, 0);
    643  1.19.2.2       ad 			continue;
    644  1.19.2.2       ad 		}
    645  1.19.2.2       ad 
    646  1.19.2.2       ad 		preq = park->park_preq;
    647       1.9    pooka 		if (phg->phg_buflen < preq->preq_buflen) {
    648       1.9    pooka 			if (!donesome)
    649       1.9    pooka 				error = E2BIG;
    650  1.19.2.2       ad 			puffs_park_release(park, 0);
    651       1.9    pooka 			goto out;
    652       1.9    pooka 		}
    653  1.19.2.2       ad 
    654       1.9    pooka 		TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
    655  1.19.2.2       ad 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    656  1.19.2.2       ad 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    657  1.19.2.3       ad 		pmp->pmp_req_touser_count--;
    658  1.19.2.3       ad 		KASSERT(pmp->pmp_req_touser_count >= 0);
    659  1.19.2.2       ad 		mutex_exit(&pmp->pmp_lock);
    660       1.9    pooka 
    661       1.9    pooka 		DPRINTF(("puffsgetop: get op %" PRIu64 " (%d.), from %p "
    662       1.9    pooka 		    "len %zu (buflen %zu), target %p\n", preq->preq_id,
    663       1.9    pooka 		    donesome, preq, park->park_copylen, preq->preq_buflen,
    664       1.9    pooka 		    bufpos));
    665       1.9    pooka 
    666       1.9    pooka 		if ((error = copyout(preq, bufpos, park->park_copylen)) != 0) {
    667  1.19.2.2       ad 			DPRINTF(("puffs_getop: copyout failed: %d\n", error));
    668       1.9    pooka 			/*
    669       1.9    pooka 			 * ok, user server is probably trying to cheat.
    670  1.19.2.2       ad 			 * stuff op back & return error to user.  We need
    671  1.19.2.2       ad 			 * to take locks in the correct order.
    672       1.9    pooka 			 */
    673  1.19.2.2       ad 			mutex_exit(&park->park_mtx);
    674  1.19.2.3       ad 
    675  1.19.2.3       ad 			/*
    676  1.19.2.3       ad 			 * XXX: ONQUEUE1 | ONQUEUE2 invariant doesn't
    677  1.19.2.3       ad 			 * hold here
    678  1.19.2.3       ad 			 */
    679  1.19.2.3       ad 
    680  1.19.2.2       ad 			mutex_enter(&pmp->pmp_lock);
    681  1.19.2.2       ad 			mutex_enter(&park->park_mtx);
    682  1.19.2.2       ad 			if ((park->park_flags & PARKFLAG_WAITERGONE) == 0) {
    683  1.19.2.2       ad 				 TAILQ_INSERT_HEAD(&pmp->pmp_req_touser, park,
    684  1.19.2.2       ad 				     park_entries);
    685  1.19.2.2       ad 				 park->park_flags |= PARKFLAG_ONQUEUE1;
    686  1.19.2.3       ad 				 pmp->pmp_req_touser_count++;
    687  1.19.2.2       ad 			}
    688       1.9    pooka 
    689  1.19.2.2       ad 			if (donesome)
    690       1.9    pooka 				error = 0;
    691  1.19.2.2       ad 			puffs_park_release(park, 0);
    692  1.19.2.2       ad 			goto out;
    693       1.9    pooka 		}
    694       1.9    pooka 		bufpos += preq->preq_buflen;
    695       1.9    pooka 		phg->phg_buflen -= preq->preq_buflen;
    696       1.9    pooka 		donesome++;
    697       1.9    pooka 
    698  1.19.2.3       ad 		/* XXXfixme: taking this lock in the wrong order */
    699  1.19.2.2       ad 		mutex_enter(&pmp->pmp_lock);
    700  1.19.2.3       ad 
    701  1.19.2.3       ad 		if (park->park_flags & PARKFLAG_WANTREPLY) {
    702  1.19.2.2       ad 			TAILQ_INSERT_TAIL(&pmp->pmp_req_replywait, park,
    703  1.19.2.2       ad 			    park_entries);
    704  1.19.2.2       ad 			park->park_flags |= PARKFLAG_ONQUEUE2;
    705  1.19.2.2       ad 			puffs_park_release(park, 0);
    706       1.9    pooka 		} else {
    707       1.9    pooka 			free(preq, M_PUFFS);
    708  1.19.2.2       ad 			puffs_park_release(park, 1);
    709       1.1    pooka 		}
    710       1.1    pooka 	}
    711       1.1    pooka 
    712       1.9    pooka  out:
    713  1.19.2.3       ad 	phg->phg_more = pmp->pmp_req_touser_count;
    714  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    715       1.1    pooka 
    716       1.9    pooka 	phg->phg_nops = donesome;
    717       1.4    pooka 
    718       1.9    pooka 	return error;
    719       1.1    pooka }
    720       1.1    pooka 
    721      1.10    pooka int
    722      1.10    pooka puffs_putop(struct puffs_mount *pmp, struct puffs_reqh_put *php)
    723       1.1    pooka {
    724       1.1    pooka 	struct puffs_park *park;
    725      1.19    pooka 	struct puffs_req tmpreq;
    726      1.19    pooka 	struct puffs_req *nextpreq;
    727       1.9    pooka 	void *userbuf;
    728       1.9    pooka 	uint64_t id;
    729       1.9    pooka 	size_t reqlen;
    730  1.19.2.2       ad 	int donesome, error, wgone, release;
    731       1.9    pooka 
    732      1.19    pooka 	donesome = error = wgone = 0;
    733       1.9    pooka 
    734       1.9    pooka 	id = php->php_id;
    735       1.9    pooka 	userbuf = php->php_buf;
    736       1.9    pooka 	reqlen = php->php_buflen;
    737       1.1    pooka 
    738  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    739       1.9    pooka 	while (donesome != php->php_nops) {
    740  1.19.2.2       ad 		release = 0;
    741      1.19    pooka #ifdef PUFFSDEBUG
    742       1.9    pooka 		DPRINTF(("puffsputop: searching for %" PRIu64 ", ubuf: %p, "
    743       1.9    pooka 		    "len %zu\n", id, userbuf, reqlen));
    744       1.9    pooka #endif
    745       1.9    pooka 		TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
    746      1.19    pooka 			if (park->park_id == id)
    747       1.9    pooka 				break;
    748       1.9    pooka 		}
    749       1.9    pooka 
    750       1.9    pooka 		if (park == NULL) {
    751  1.19.2.2       ad 			DPRINTF(("puffsputop: no request: %" PRIu64 "\n", id));
    752       1.9    pooka 			error = EINVAL;
    753       1.1    pooka 			break;
    754       1.1    pooka 		}
    755  1.19.2.2       ad 
    756  1.19.2.2       ad 		puffs_park_reference(park);
    757  1.19.2.2       ad 		if (reqlen == 0 || reqlen > park->park_maxlen) {
    758  1.19.2.2       ad 			DPRINTF(("puffsputop: invalid buffer length: "
    759  1.19.2.2       ad 			    "%zu\n", reqlen));
    760  1.19.2.2       ad 			error = E2BIG;
    761  1.19.2.2       ad 			puffs_park_release(park, 0);
    762  1.19.2.2       ad 			break;
    763  1.19.2.2       ad 		}
    764  1.19.2.2       ad 		wgone = park->park_flags & PARKFLAG_WAITERGONE;
    765  1.19.2.2       ad 
    766  1.19.2.2       ad 		/* check if it's still on the queue after acquiring lock */
    767  1.19.2.2       ad 		if (park->park_flags & PARKFLAG_ONQUEUE2) {
    768  1.19.2.2       ad 			TAILQ_REMOVE(&pmp->pmp_req_replywait, park,
    769  1.19.2.2       ad 			    park_entries);
    770  1.19.2.2       ad 			park->park_flags &= ~PARKFLAG_ONQUEUE2;
    771  1.19.2.2       ad 		}
    772  1.19.2.2       ad 
    773  1.19.2.2       ad 		mutex_exit(&pmp->pmp_lock);
    774       1.9    pooka 
    775      1.19    pooka 		/*
    776      1.19    pooka 		 * If the caller has gone south, go to next, collect
    777      1.19    pooka 		 * $200 and free the structure there instead of wakeup.
    778  1.19.2.2       ad 		 * We also need to copyin the header info.  Flag structure
    779  1.19.2.2       ad 		 * release to mode total and utter destruction.
    780      1.19    pooka 		 */
    781  1.19.2.2       ad 		if (wgone) {
    782      1.19    pooka 			DPRINTF(("puffs_putop: bad service - waiter gone for "
    783      1.19    pooka 			    "park %p\n", park));
    784      1.19    pooka 			error = copyin(userbuf, &tmpreq,
    785      1.19    pooka 			    sizeof(struct puffs_req));
    786  1.19.2.2       ad 			release = 1;
    787      1.19    pooka 			if (error)
    788      1.19    pooka 				goto loopout;
    789      1.19    pooka 			nextpreq = &tmpreq;
    790      1.19    pooka 			goto next;
    791      1.19    pooka 		}
    792      1.19    pooka 
    793       1.9    pooka 		DPRINTF(("puffsputpop: copyin from %p to %p, len %zu\n",
    794       1.9    pooka 		    userbuf, park->park_preq, reqlen));
    795       1.9    pooka 		error = copyin(userbuf, park->park_preq, reqlen);
    796       1.9    pooka 		if (error)
    797       1.9    pooka 			goto loopout;
    798      1.19    pooka 		nextpreq = park->park_preq;
    799       1.9    pooka 
    800      1.19    pooka  next:
    801       1.9    pooka 		/* all's well, prepare for next op */
    802      1.19    pooka 		id = nextpreq->preq_id;
    803      1.19    pooka 		reqlen = nextpreq->preq_buflen;
    804      1.19    pooka 		userbuf = nextpreq->preq_nextbuf;
    805       1.9    pooka 		donesome++;
    806       1.9    pooka 
    807       1.9    pooka  loopout:
    808  1.19.2.3       ad 		if (error && !wgone)
    809       1.9    pooka 			park->park_preq->preq_rv = error;
    810       1.1    pooka 
    811  1.19.2.2       ad 		if (park->park_flags & PARKFLAG_CALL) {
    812  1.19.2.5       ad 			DPRINTF(("puffsputopt: call for %p, arg %p\n",
    813  1.19.2.5       ad 			    park->park_preq, park->park_donearg));
    814  1.19.2.2       ad 			park->park_done(park->park_preq, park->park_donearg);
    815  1.19.2.2       ad 			release = 1;
    816      1.19    pooka 		}
    817      1.19    pooka 
    818  1.19.2.2       ad 		if (!wgone) {
    819  1.19.2.2       ad 			DPRINTF(("puffs_putop: flagging done for "
    820  1.19.2.2       ad 			    "park %p\n", park));
    821  1.19.2.2       ad 
    822  1.19.2.2       ad 			cv_signal(&park->park_cv);
    823  1.19.2.2       ad 		}
    824  1.19.2.3       ad 		park->park_flags |= PARKFLAG_DONE;
    825  1.19.2.2       ad 		puffs_park_release(park, release);
    826  1.19.2.2       ad 
    827  1.19.2.2       ad 		mutex_enter(&pmp->pmp_lock);
    828       1.9    pooka 		if (error)
    829       1.9    pooka 			break;
    830      1.19    pooka 		wgone = 0;
    831       1.1    pooka 	}
    832       1.1    pooka 
    833  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    834       1.9    pooka 	php->php_nops -= donesome;
    835       1.1    pooka 
    836       1.1    pooka 	return error;
    837       1.1    pooka }
    838       1.1    pooka 
    839  1.19.2.2       ad /*
    840  1.19.2.2       ad  * We're dead, kaput, RIP, slightly more than merely pining for the
    841  1.19.2.2       ad  * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
    842  1.19.2.2       ad  * our maker, ceased to be, etcetc.  YASD.  It's a dead FS!
    843  1.19.2.2       ad  *
    844  1.19.2.2       ad  * Caller must hold puffs mutex.
    845  1.19.2.2       ad  */
    846  1.19.2.2       ad void
    847  1.19.2.2       ad puffs_userdead(struct puffs_mount *pmp)
    848  1.19.2.2       ad {
    849  1.19.2.3       ad 	struct puffs_park *park, *park_next;
    850  1.19.2.2       ad 
    851  1.19.2.2       ad 	/*
    852  1.19.2.2       ad 	 * Mark filesystem status as dying so that operations don't
    853  1.19.2.2       ad 	 * attempt to march to userspace any longer.
    854  1.19.2.2       ad 	 */
    855  1.19.2.2       ad 	pmp->pmp_status = PUFFSTAT_DYING;
    856  1.19.2.2       ad 
    857  1.19.2.2       ad 	/* signal waiters on REQUEST TO file server queue */
    858  1.19.2.3       ad 	for (park = TAILQ_FIRST(&pmp->pmp_req_touser); park; park = park_next) {
    859  1.19.2.2       ad 		uint8_t opclass;
    860  1.19.2.2       ad 
    861  1.19.2.2       ad 		puffs_park_reference(park);
    862  1.19.2.3       ad 		park_next = TAILQ_NEXT(park, park_entries);
    863  1.19.2.2       ad 
    864  1.19.2.2       ad 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    865  1.19.2.2       ad 		TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
    866  1.19.2.2       ad 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    867  1.19.2.3       ad 		pmp->pmp_req_touser_count--;
    868  1.19.2.2       ad 
    869  1.19.2.3       ad 		/*
    870  1.19.2.3       ad 		 * If the waiter is gone, we may *NOT* access preq anymore.
    871  1.19.2.3       ad 		 */
    872  1.19.2.3       ad 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    873  1.19.2.3       ad 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    874  1.19.2.3       ad 			KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    875  1.19.2.3       ad 			puffs_park_release(park, 0);
    876  1.19.2.2       ad 		} else {
    877  1.19.2.3       ad 			opclass = park->park_preq->preq_opclass;
    878  1.19.2.2       ad 			park->park_preq->preq_rv = ENXIO;
    879  1.19.2.3       ad 
    880  1.19.2.3       ad 			if (park->park_flags & PARKFLAG_CALL) {
    881  1.19.2.3       ad 				park->park_done(park->park_preq,
    882  1.19.2.3       ad 				    park->park_donearg);
    883  1.19.2.3       ad 				puffs_park_release(park, 1);
    884  1.19.2.3       ad 			} else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
    885  1.19.2.3       ad 				free(park->park_preq, M_PUFFS);
    886  1.19.2.3       ad 				puffs_park_release(park, 1);
    887  1.19.2.3       ad 			} else {
    888  1.19.2.3       ad 				park->park_preq->preq_rv = ENXIO;
    889  1.19.2.3       ad 				cv_signal(&park->park_cv);
    890  1.19.2.3       ad 				puffs_park_release(park, 0);
    891  1.19.2.3       ad 			}
    892  1.19.2.2       ad 		}
    893  1.19.2.2       ad 	}
    894  1.19.2.2       ad 
    895  1.19.2.2       ad 	/* signal waiters on RESPONSE FROM file server queue */
    896  1.19.2.3       ad 	for (park=TAILQ_FIRST(&pmp->pmp_req_replywait); park; park=park_next) {
    897  1.19.2.2       ad 		puffs_park_reference(park);
    898  1.19.2.3       ad 		park_next = TAILQ_NEXT(park, park_entries);
    899  1.19.2.2       ad 
    900  1.19.2.2       ad 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
    901  1.19.2.3       ad 		KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    902  1.19.2.2       ad 
    903  1.19.2.2       ad 		TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
    904  1.19.2.2       ad 		park->park_flags &= ~PARKFLAG_ONQUEUE2;
    905  1.19.2.2       ad 
    906  1.19.2.3       ad 		/*
    907  1.19.2.3       ad 		 * If the waiter is gone, we may *NOT* access preq anymore.
    908  1.19.2.3       ad 		 */
    909  1.19.2.3       ad 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    910  1.19.2.3       ad 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    911  1.19.2.2       ad 			puffs_park_release(park, 0);
    912  1.19.2.3       ad 		} else {
    913  1.19.2.3       ad 			park->park_preq->preq_rv = ENXIO;
    914  1.19.2.3       ad 			if (park->park_flags & PARKFLAG_CALL) {
    915  1.19.2.3       ad 				park->park_done(park->park_preq,
    916  1.19.2.3       ad 				    park->park_donearg);
    917  1.19.2.3       ad 				puffs_park_release(park, 1);
    918  1.19.2.3       ad 			} else {
    919  1.19.2.3       ad 				cv_signal(&park->park_cv);
    920  1.19.2.3       ad 				puffs_park_release(park, 0);
    921  1.19.2.3       ad 			}
    922  1.19.2.2       ad 		}
    923  1.19.2.2       ad 	}
    924  1.19.2.2       ad }
    925  1.19.2.2       ad 
    926       1.1    pooka /* this is probably going to die away at some point? */
    927       1.9    pooka /*
    928       1.9    pooka  * XXX: currently bitrotted
    929       1.9    pooka  */
    930       1.9    pooka #if 0
    931       1.1    pooka static int
    932       1.1    pooka puffssizeop(struct puffs_mount *pmp, struct puffs_sizeop *psop_user)
    933       1.1    pooka {
    934       1.1    pooka 	struct puffs_sizepark *pspark;
    935       1.1    pooka 	void *kernbuf;
    936       1.1    pooka 	size_t copylen;
    937       1.1    pooka 	int error;
    938       1.1    pooka 
    939       1.1    pooka 	/* locate correct op */
    940  1.19.2.2       ad 	mutex_enter(&pmp->pmp_lock);
    941       1.1    pooka 	TAILQ_FOREACH(pspark, &pmp->pmp_req_sizepark, pkso_entries) {
    942       1.1    pooka 		if (pspark->pkso_reqid == psop_user->pso_reqid) {
    943       1.1    pooka 			TAILQ_REMOVE(&pmp->pmp_req_sizepark, pspark,
    944       1.1    pooka 			    pkso_entries);
    945       1.1    pooka 			break;
    946       1.1    pooka 		}
    947       1.1    pooka 	}
    948  1.19.2.2       ad 	mutex_exit(&pmp->pmp_lock);
    949       1.1    pooka 
    950       1.1    pooka 	if (pspark == NULL)
    951       1.1    pooka 		return EINVAL;
    952       1.1    pooka 
    953       1.1    pooka 	error = 0;
    954       1.1    pooka 	copylen = MIN(pspark->pkso_bufsize, psop_user->pso_bufsize);
    955       1.1    pooka 
    956       1.1    pooka 	/*
    957       1.1    pooka 	 * XXX: uvm stuff to avoid bouncy-bouncy copying?
    958       1.1    pooka 	 */
    959       1.1    pooka 	if (PUFFS_SIZEOP_UIO(pspark->pkso_reqtype)) {
    960       1.1    pooka 		kernbuf = malloc(copylen, M_PUFFS, M_WAITOK | M_ZERO);
    961       1.1    pooka 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_IN) {
    962       1.1    pooka 			error = copyin(psop_user->pso_userbuf,
    963       1.1    pooka 			    kernbuf, copylen);
    964       1.1    pooka 			if (error) {
    965       1.1    pooka 				printf("psop ERROR1 %d\n", error);
    966       1.1    pooka 				goto escape;
    967       1.1    pooka 			}
    968       1.1    pooka 		}
    969       1.1    pooka 		error = uiomove(kernbuf, copylen, pspark->pkso_uio);
    970       1.1    pooka 		if (error) {
    971       1.1    pooka 			printf("uiomove from kernel %p, len %d failed: %d\n",
    972       1.1    pooka 			    kernbuf, (int)copylen, error);
    973       1.1    pooka 			goto escape;
    974       1.1    pooka 		}
    975       1.1    pooka 
    976       1.1    pooka 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_OUT) {
    977       1.1    pooka 			error = copyout(kernbuf,
    978       1.1    pooka 			    psop_user->pso_userbuf, copylen);
    979       1.1    pooka 			if (error) {
    980       1.1    pooka 				printf("psop ERROR2 %d\n", error);
    981       1.1    pooka 				goto escape;
    982       1.1    pooka 			}
    983       1.1    pooka 		}
    984       1.1    pooka  escape:
    985       1.1    pooka 		free(kernbuf, M_PUFFS);
    986       1.1    pooka 	} else if (PUFFS_SIZEOP_BUF(pspark->pkso_reqtype)) {
    987       1.1    pooka 		copylen = MAX(pspark->pkso_bufsize, psop_user->pso_bufsize);
    988       1.1    pooka 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_BUF_IN) {
    989       1.1    pooka 			error = copyin(psop_user->pso_userbuf,
    990       1.1    pooka 			pspark->pkso_copybuf, copylen);
    991       1.1    pooka 		} else {
    992       1.1    pooka 			error = copyout(pspark->pkso_copybuf,
    993       1.1    pooka 			    psop_user->pso_userbuf, copylen);
    994       1.1    pooka 		}
    995       1.1    pooka 	}
    996       1.1    pooka #ifdef DIAGNOSTIC
    997       1.1    pooka 	else
    998       1.1    pooka 		panic("puffssizeop: invalid reqtype %d\n",
    999       1.1    pooka 		    pspark->pkso_reqtype);
   1000       1.1    pooka #endif /* DIAGNOSTIC */
   1001       1.1    pooka 
   1002       1.1    pooka 	return error;
   1003       1.1    pooka }
   1004       1.9    pooka #endif
   1005