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