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