Home | History | Annotate | Line # | Download | only in puffs
puffs_msgif.c revision 1.40.4.5
      1  1.40.4.5  jmcneill /*	$NetBSD: puffs_msgif.c,v 1.40.4.5 2007/11/04 21:03:31 jmcneill Exp $	*/
      2       1.1     pooka 
      3       1.1     pooka /*
      4      1.16     pooka  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
      5       1.1     pooka  *
      6       1.1     pooka  * Development of this software was supported by the
      7       1.1     pooka  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8       1.1     pooka  * The Google SoC project was mentored by Bill Studenmund.
      9       1.1     pooka  *
     10       1.1     pooka  * Redistribution and use in source and binary forms, with or without
     11       1.1     pooka  * modification, are permitted provided that the following conditions
     12       1.1     pooka  * are met:
     13       1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     14       1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     15       1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     pooka  *    documentation and/or other materials provided with the distribution.
     18       1.1     pooka  *
     19       1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20       1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21       1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22       1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23       1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25       1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1     pooka  * SUCH DAMAGE.
     30       1.1     pooka  */
     31       1.1     pooka 
     32       1.1     pooka #include <sys/cdefs.h>
     33  1.40.4.5  jmcneill __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.40.4.5 2007/11/04 21:03:31 jmcneill Exp $");
     34       1.1     pooka 
     35       1.1     pooka #include <sys/param.h>
     36      1.16     pooka #include <sys/fstrans.h>
     37  1.40.4.3     joerg #include <sys/kmem.h>
     38       1.1     pooka #include <sys/malloc.h>
     39       1.1     pooka #include <sys/mount.h>
     40       1.1     pooka #include <sys/vnode.h>
     41       1.1     pooka #include <sys/lock.h>
     42      1.39        ad #include <sys/proc.h>
     43       1.1     pooka 
     44       1.1     pooka #include <fs/puffs/puffs_msgif.h>
     45       1.1     pooka #include <fs/puffs/puffs_sys.h>
     46       1.1     pooka 
     47      1.22     pooka /*
     48      1.22     pooka  * waitq data structures
     49      1.22     pooka  */
     50      1.22     pooka 
     51      1.22     pooka /*
     52      1.22     pooka  * While a request is going to userspace, park the caller within the
     53      1.22     pooka  * kernel.  This is the kernel counterpart of "struct puffs_req".
     54      1.22     pooka  */
     55  1.40.4.3     joerg struct puffs_msgpark {
     56      1.22     pooka 	struct puffs_req	*park_preq;	/* req followed by buf	*/
     57      1.22     pooka 
     58      1.22     pooka 	size_t			park_copylen;	/* userspace copylength	*/
     59      1.22     pooka 	size_t			park_maxlen;	/* max size in comeback */
     60      1.24     pooka 
     61  1.40.4.3     joerg 	parkdone_fn		park_done;	/* "biodone" a'la puffs	*/
     62      1.24     pooka 	void			*park_donearg;
     63      1.22     pooka 
     64      1.22     pooka 	int			park_flags;
     65      1.26     pooka 	int			park_refcount;
     66      1.22     pooka 
     67      1.22     pooka 	kcondvar_t		park_cv;
     68      1.26     pooka 	kmutex_t		park_mtx;
     69      1.26     pooka 
     70  1.40.4.3     joerg 	TAILQ_ENTRY(puffs_msgpark) park_entries;
     71      1.22     pooka };
     72      1.22     pooka #define PARKFLAG_WAITERGONE	0x01
     73      1.26     pooka #define PARKFLAG_DONE		0x02
     74      1.26     pooka #define PARKFLAG_ONQUEUE1	0x04
     75      1.26     pooka #define PARKFLAG_ONQUEUE2	0x08
     76      1.26     pooka #define PARKFLAG_CALL		0x10
     77      1.31     pooka #define PARKFLAG_WANTREPLY	0x20
     78      1.22     pooka 
     79      1.22     pooka static struct pool_cache parkpc;
     80      1.22     pooka static struct pool parkpool;
     81      1.22     pooka 
     82      1.22     pooka static int
     83      1.22     pooka makepark(void *arg, void *obj, int flags)
     84      1.22     pooka {
     85  1.40.4.3     joerg 	struct puffs_msgpark *park = obj;
     86      1.22     pooka 
     87      1.26     pooka 	mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
     88      1.22     pooka 	cv_init(&park->park_cv, "puffsrpl");
     89      1.22     pooka 
     90      1.22     pooka 	return 0;
     91      1.22     pooka }
     92      1.22     pooka 
     93      1.22     pooka static void
     94      1.22     pooka nukepark(void *arg, void *obj)
     95      1.22     pooka {
     96  1.40.4.3     joerg 	struct puffs_msgpark *park = obj;
     97      1.22     pooka 
     98      1.22     pooka 	cv_destroy(&park->park_cv);
     99      1.26     pooka 	mutex_destroy(&park->park_mtx);
    100      1.22     pooka }
    101      1.22     pooka 
    102      1.22     pooka void
    103      1.22     pooka puffs_msgif_init()
    104      1.22     pooka {
    105      1.22     pooka 
    106  1.40.4.3     joerg 	pool_init(&parkpool, sizeof(struct puffs_msgpark), 0, 0, 0,
    107      1.22     pooka 	    "puffprkl", &pool_allocator_nointr, IPL_NONE);
    108      1.22     pooka 	pool_cache_init(&parkpc, &parkpool, makepark, nukepark, NULL);
    109      1.22     pooka }
    110      1.22     pooka 
    111      1.22     pooka void
    112      1.22     pooka puffs_msgif_destroy()
    113      1.22     pooka {
    114      1.22     pooka 
    115      1.22     pooka 	pool_cache_destroy(&parkpc);
    116      1.22     pooka 	pool_destroy(&parkpool);
    117      1.22     pooka }
    118      1.22     pooka 
    119  1.40.4.3     joerg static int alloced;
    120  1.40.4.3     joerg 
    121  1.40.4.3     joerg static struct puffs_msgpark *
    122  1.40.4.3     joerg puffs_msgpark_alloc(int waitok)
    123      1.26     pooka {
    124  1.40.4.3     joerg 	struct puffs_msgpark *park;
    125      1.26     pooka 
    126      1.26     pooka 	park = pool_cache_get(&parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
    127  1.40.4.3     joerg 	if (park == NULL)
    128  1.40.4.3     joerg 		return park;
    129  1.40.4.3     joerg 
    130  1.40.4.3     joerg 	park->park_refcount = 1;
    131  1.40.4.3     joerg 	park->park_preq = NULL;
    132  1.40.4.3     joerg 	park->park_flags = PARKFLAG_WANTREPLY;
    133      1.26     pooka 
    134      1.26     pooka 	return park;
    135      1.26     pooka }
    136      1.26     pooka 
    137      1.26     pooka static void
    138  1.40.4.3     joerg puffs_msgpark_reference(struct puffs_msgpark *park)
    139      1.22     pooka {
    140      1.22     pooka 
    141  1.40.4.3     joerg 	KASSERT(mutex_owned(&park->park_mtx));
    142      1.26     pooka 	park->park_refcount++;
    143      1.22     pooka }
    144      1.22     pooka 
    145  1.40.4.3     joerg /*
    146  1.40.4.3     joerg  * Release reference to park structure.
    147  1.40.4.3     joerg  */
    148  1.40.4.3     joerg static void
    149  1.40.4.3     joerg puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
    150      1.22     pooka {
    151  1.40.4.3     joerg 	struct puffs_req *preq = park->park_preq;
    152  1.40.4.3     joerg 	int refcnt;
    153      1.22     pooka 
    154      1.26     pooka 	KASSERT(mutex_owned(&park->park_mtx));
    155  1.40.4.3     joerg 	refcnt = park->park_refcount -= howmany;
    156      1.26     pooka 	mutex_exit(&park->park_mtx);
    157  1.40.4.3     joerg 
    158  1.40.4.3     joerg 	KASSERT(refcnt >= 0);
    159  1.40.4.3     joerg 
    160  1.40.4.3     joerg 	if (refcnt == 0) {
    161  1.40.4.3     joerg 		alloced--;
    162  1.40.4.3     joerg 		if (preq)
    163  1.40.4.3     joerg 			kmem_free(preq, park->park_maxlen);
    164      1.26     pooka 		pool_cache_put(&parkpc, park);
    165  1.40.4.3     joerg 	}
    166      1.22     pooka }
    167  1.40.4.3     joerg #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
    168      1.22     pooka 
    169      1.26     pooka #ifdef PUFFSDEBUG
    170      1.26     pooka static void
    171  1.40.4.3     joerg parkdump(struct puffs_msgpark *park)
    172      1.26     pooka {
    173      1.26     pooka 
    174      1.26     pooka 	DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
    175      1.26     pooka 	    "\tcopy %zu, max %zu - done: %p/%p\n"
    176      1.26     pooka 	    "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
    177  1.40.4.3     joerg 	    park, park->park_preq, park->park_preq->preq_id,
    178      1.26     pooka 	    park->park_copylen, park->park_maxlen,
    179      1.26     pooka 	    park->park_done, park->park_donearg,
    180      1.26     pooka 	    park->park_flags, park->park_refcount,
    181      1.26     pooka 	    &park->park_cv, &park->park_mtx));
    182      1.26     pooka }
    183      1.26     pooka 
    184      1.26     pooka static void
    185      1.26     pooka parkqdump(struct puffs_wq *q, int dumpall)
    186      1.26     pooka {
    187  1.40.4.3     joerg 	struct puffs_msgpark *park;
    188      1.26     pooka 	int total = 0;
    189      1.26     pooka 
    190      1.26     pooka 	TAILQ_FOREACH(park, q, park_entries) {
    191      1.26     pooka 		if (dumpall)
    192      1.26     pooka 			parkdump(park);
    193      1.26     pooka 		total++;
    194      1.26     pooka 	}
    195      1.29     pooka 	DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
    196      1.26     pooka 
    197      1.26     pooka }
    198      1.26     pooka #endif /* PUFFSDEBUG */
    199      1.22     pooka 
    200      1.22     pooka /*
    201  1.40.4.3     joerg  * A word about locking in the park structures: the lock protects the
    202  1.40.4.3     joerg  * fields of the *park* structure (not preq) and acts as an interlock
    203  1.40.4.3     joerg  * in cv operations.  The lock is always internal to this module and
    204  1.40.4.3     joerg  * callers do not need to worry about it.
    205      1.22     pooka  */
    206  1.40.4.3     joerg 
    207  1.40.4.3     joerg int
    208  1.40.4.3     joerg puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
    209  1.40.4.3     joerg 	int cansleep)
    210      1.22     pooka {
    211  1.40.4.3     joerg 	struct puffs_msgpark *park;
    212  1.40.4.3     joerg 	void *m;
    213      1.22     pooka 
    214  1.40.4.3     joerg 	m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
    215  1.40.4.3     joerg 	if (m == NULL) {
    216  1.40.4.3     joerg 		KASSERT(cansleep == 0);
    217  1.40.4.3     joerg 		return ENOMEM;
    218  1.40.4.3     joerg 	}
    219      1.22     pooka 
    220  1.40.4.3     joerg 	park = puffs_msgpark_alloc(cansleep);
    221  1.40.4.3     joerg 	if (park == NULL) {
    222  1.40.4.3     joerg 		KASSERT(cansleep == 0);
    223  1.40.4.3     joerg 		kmem_free(m, len);
    224  1.40.4.3     joerg 		return ENOMEM;
    225  1.40.4.3     joerg 	}
    226      1.22     pooka 
    227  1.40.4.3     joerg 	park->park_preq = m;
    228  1.40.4.3     joerg 	park->park_maxlen = len;
    229      1.22     pooka 
    230  1.40.4.3     joerg 	*ppark = park;
    231  1.40.4.3     joerg 	*mem = m;
    232  1.40.4.3     joerg 
    233  1.40.4.3     joerg 	return 0;
    234      1.22     pooka }
    235      1.22     pooka 
    236  1.40.4.3     joerg void
    237  1.40.4.3     joerg puffs_msgmem_release(struct puffs_msgpark *park)
    238  1.40.4.3     joerg {
    239  1.40.4.3     joerg 
    240  1.40.4.3     joerg 	if (park == NULL)
    241  1.40.4.3     joerg 		return;
    242  1.40.4.3     joerg 
    243  1.40.4.3     joerg 	mutex_enter(&park->park_mtx);
    244  1.40.4.3     joerg 	puffs_msgpark_release(park);
    245  1.40.4.3     joerg }
    246  1.40.4.3     joerg 
    247  1.40.4.3     joerg void
    248  1.40.4.3     joerg puffs_msg_setfaf(struct puffs_msgpark *park)
    249  1.40.4.3     joerg {
    250  1.40.4.3     joerg 
    251  1.40.4.3     joerg 	park->park_flags &= ~PARKFLAG_WANTREPLY;
    252  1.40.4.3     joerg }
    253       1.1     pooka 
    254       1.1     pooka /*
    255       1.1     pooka  * kernel-user-kernel waitqueues
    256       1.1     pooka  */
    257       1.1     pooka 
    258  1.40.4.3     joerg static int touser(struct puffs_mount *, struct puffs_msgpark *);
    259       1.1     pooka 
    260  1.40.4.3     joerg static uint64_t
    261  1.40.4.3     joerg puffs_getmsgid(struct puffs_mount *pmp)
    262       1.1     pooka {
    263      1.14     pooka 	uint64_t rv;
    264       1.1     pooka 
    265      1.22     pooka 	mutex_enter(&pmp->pmp_lock);
    266  1.40.4.3     joerg 	rv = pmp->pmp_nextmsgid++;
    267      1.22     pooka 	mutex_exit(&pmp->pmp_lock);
    268       1.1     pooka 
    269       1.1     pooka 	return rv;
    270       1.1     pooka }
    271       1.1     pooka 
    272       1.1     pooka /* vfs request */
    273       1.1     pooka int
    274  1.40.4.3     joerg puffs_msg_vfs(struct puffs_mount *pmp, struct puffs_msgpark *park, int optype)
    275       1.1     pooka {
    276       1.1     pooka 
    277      1.25     pooka 	park->park_preq->preq_opclass = PUFFSOP_VFS;
    278      1.25     pooka 	park->park_preq->preq_optype = optype;
    279       1.1     pooka 
    280  1.40.4.3     joerg 	park->park_copylen = park->park_maxlen;
    281       1.1     pooka 
    282  1.40.4.3     joerg 	return touser(pmp, park);
    283      1.16     pooka }
    284      1.16     pooka 
    285       1.1     pooka /*
    286       1.1     pooka  * vnode level request
    287       1.1     pooka  */
    288       1.1     pooka int
    289  1.40.4.3     joerg puffs_msg_vn(struct puffs_mount *pmp, struct puffs_msgpark *park,
    290  1.40.4.3     joerg 	int optype, size_t delta, struct vnode *vp_opc, struct vnode *vp_aux)
    291       1.1     pooka {
    292      1.35     pooka 	struct puffs_req *preq;
    293      1.35     pooka 	void *cookie = VPTOPNC(vp_opc);
    294      1.35     pooka 	struct puffs_node *pnode;
    295      1.35     pooka 	int rv;
    296       1.1     pooka 
    297      1.25     pooka 	park->park_preq->preq_opclass = PUFFSOP_VN;
    298      1.25     pooka 	park->park_preq->preq_optype = optype;
    299      1.25     pooka 	park->park_preq->preq_cookie = cookie;
    300      1.25     pooka 
    301  1.40.4.3     joerg 	KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
    302  1.40.4.3     joerg 	park->park_copylen = park->park_maxlen - delta;
    303       1.1     pooka 
    304  1.40.4.3     joerg 	rv = touser(pmp, park);
    305      1.35     pooka 
    306      1.35     pooka 	/*
    307      1.35     pooka 	 * Check if the user server requests that inactive be called
    308      1.35     pooka 	 * when the time is right.
    309      1.35     pooka 	 */
    310      1.35     pooka 	preq = park->park_preq;
    311      1.35     pooka 	if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N1) {
    312      1.35     pooka 		pnode = vp_opc->v_data;
    313      1.35     pooka 		pnode->pn_stat |= PNODE_DOINACT;
    314      1.35     pooka 	}
    315      1.35     pooka 	if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N2) {
    316      1.35     pooka 		/* if no vp_aux, just ignore */
    317      1.35     pooka 		if (vp_aux) {
    318      1.35     pooka 			pnode = vp_aux->v_data;
    319      1.35     pooka 			pnode->pn_stat |= PNODE_DOINACT;
    320      1.35     pooka 		}
    321      1.35     pooka 	}
    322      1.37     pooka 	if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1) {
    323      1.37     pooka 		pnode = vp_opc->v_data;
    324      1.37     pooka 		pnode->pn_stat |= PNODE_NOREFS;
    325      1.37     pooka 	}
    326      1.37     pooka 	if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2) {
    327      1.37     pooka 		/* if no vp_aux, just ignore */
    328      1.37     pooka 		if (vp_aux) {
    329      1.37     pooka 			pnode = vp_aux->v_data;
    330      1.37     pooka 			pnode->pn_stat |= PNODE_NOREFS;
    331      1.37     pooka 		}
    332      1.37     pooka 	}
    333      1.35     pooka 
    334      1.35     pooka 	return rv;
    335       1.1     pooka }
    336       1.1     pooka 
    337      1.24     pooka void
    338  1.40.4.3     joerg puffs_msg_vncall(struct puffs_mount *pmp, struct puffs_msgpark *park,
    339  1.40.4.3     joerg 	int optype, size_t delta, parkdone_fn donefn, void *donearg,
    340  1.40.4.3     joerg 	struct vnode *vp_opc)
    341       1.1     pooka {
    342      1.35     pooka 	void *cookie = VPTOPNC(vp_opc);
    343       1.1     pooka 
    344      1.25     pooka 	park->park_preq->preq_opclass = PUFFSOP_VN;
    345      1.25     pooka 	park->park_preq->preq_optype = optype;
    346      1.25     pooka 	park->park_preq->preq_cookie = cookie;
    347      1.25     pooka 
    348  1.40.4.3     joerg 	KASSERT(delta < park->park_maxlen);
    349  1.40.4.3     joerg 	park->park_copylen = park->park_maxlen - delta;
    350      1.25     pooka 	park->park_done = donefn;
    351      1.25     pooka 	park->park_donearg = donearg;
    352  1.40.4.3     joerg 	park->park_flags |= PARKFLAG_CALL;
    353       1.4     pooka 
    354  1.40.4.3     joerg 	(void) touser(pmp, park);
    355       1.4     pooka }
    356       1.4     pooka 
    357  1.40.4.3     joerg int
    358  1.40.4.3     joerg puffs_msg_raw(struct puffs_mount *pmp, struct puffs_msgpark *park)
    359      1.21     pooka {
    360      1.21     pooka 
    361  1.40.4.3     joerg 	park->park_copylen = park->park_maxlen;
    362      1.21     pooka 
    363  1.40.4.3     joerg 	return touser(pmp, park);
    364      1.21     pooka }
    365      1.21     pooka 
    366  1.40.4.1     joerg void
    367  1.40.4.3     joerg puffs_msg_errnotify(struct puffs_mount *pmp, uint8_t type, int error,
    368  1.40.4.1     joerg 	const char *str, void *cookie)
    369  1.40.4.1     joerg {
    370  1.40.4.3     joerg 	struct puffs_msgpark *park;
    371  1.40.4.1     joerg 	struct puffs_error *perr;
    372  1.40.4.1     joerg 
    373  1.40.4.3     joerg 	puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void **)&perr,1);
    374  1.40.4.1     joerg 
    375  1.40.4.1     joerg 	perr->perr_error = error;
    376  1.40.4.1     joerg 	strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
    377  1.40.4.1     joerg 
    378  1.40.4.3     joerg 	park->park_preq->preq_opclass |= PUFFSOP_ERROR | PUFFSOPFLAG_FAF;
    379  1.40.4.1     joerg 	park->park_preq->preq_optype = type;
    380  1.40.4.1     joerg 	park->park_preq->preq_cookie = cookie;
    381  1.40.4.1     joerg 
    382  1.40.4.3     joerg 	park->park_copylen = park->park_maxlen;
    383  1.40.4.1     joerg 
    384  1.40.4.3     joerg 	(void)touser(pmp, park);
    385  1.40.4.1     joerg }
    386  1.40.4.1     joerg 
    387       1.4     pooka /*
    388  1.40.4.5  jmcneill  * Wait for the userspace ping-pong game in calling process context,
    389  1.40.4.5  jmcneill  * unless a FAF / async call, in which case just enqueues the request
    390  1.40.4.5  jmcneill  * and return immediately.
    391       1.1     pooka  */
    392       1.1     pooka static int
    393  1.40.4.3     joerg touser(struct puffs_mount *pmp, struct puffs_msgpark *park)
    394       1.1     pooka {
    395      1.26     pooka 	struct lwp *l = curlwp;
    396      1.16     pooka 	struct mount *mp;
    397       1.9     pooka 	struct puffs_req *preq;
    398      1.19     pooka 	int rv = 0;
    399       1.1     pooka 
    400      1.16     pooka 	mp = PMPTOMP(pmp);
    401      1.25     pooka 	preq = park->park_preq;
    402  1.40.4.3     joerg 	preq->preq_buflen = park->park_maxlen;
    403  1.40.4.3     joerg 	KASSERT(preq->preq_id == 0);
    404      1.19     pooka 
    405  1.40.4.3     joerg 	if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
    406  1.40.4.3     joerg 		preq->preq_opclass |= PUFFSOPFLAG_FAF;
    407  1.40.4.3     joerg 	else
    408  1.40.4.3     joerg 		preq->preq_id = puffs_getmsgid(pmp);
    409  1.40.4.3     joerg 
    410  1.40.4.3     joerg 	/* fill in caller information */
    411  1.40.4.3     joerg 	preq->preq_pid = l->l_proc->p_pid;
    412  1.40.4.3     joerg 	preq->preq_lid = l->l_lid;
    413      1.31     pooka 
    414      1.19     pooka 	/*
    415  1.40.4.5  jmcneill 	 * To support cv_sig, yet another movie: check if there are signals
    416      1.19     pooka 	 * pending and we are issueing a non-FAF.  If so, return an error
    417      1.19     pooka 	 * directly UNLESS we are issueing INACTIVE.  In that case, convert
    418      1.19     pooka 	 * it to a FAF, fire off to the file server and return an error.
    419      1.19     pooka 	 * Yes, this is bordering disgusting.  Barfbags are on me.
    420      1.19     pooka 	 */
    421      1.31     pooka 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    422      1.25     pooka 	   && (park->park_flags & PARKFLAG_CALL) == 0
    423      1.19     pooka 	   && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    424      1.19     pooka 		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
    425      1.19     pooka 		    && preq->preq_optype == PUFFS_VN_INACTIVE) {
    426  1.40.4.3     joerg 			park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
    427  1.40.4.3     joerg 			park->park_flags &= ~PARKFLAG_WANTREPLY;
    428      1.25     pooka 			DPRINTF(("puffs touser: converted to FAF %p\n", park));
    429      1.19     pooka 			rv = EINTR;
    430      1.19     pooka 		} else {
    431      1.19     pooka 			return EINTR;
    432      1.19     pooka 		}
    433      1.19     pooka 	}
    434      1.16     pooka 
    435      1.16     pooka 	/*
    436      1.16     pooka 	 * test for suspension lock.
    437      1.16     pooka 	 *
    438      1.16     pooka 	 * Note that we *DO NOT* keep the lock, since that might block
    439      1.16     pooka 	 * lock acquiring PLUS it would give userlandia control over
    440      1.16     pooka 	 * the lock.  The operation queue enforces a strict ordering:
    441      1.16     pooka 	 * when the fs server gets in the op stream, it knows things
    442      1.16     pooka 	 * are in order.  The kernel locks can't guarantee that for
    443      1.16     pooka 	 * userspace, in any case.
    444      1.16     pooka 	 *
    445      1.16     pooka 	 * BUT: this presents a problem for ops which have a consistency
    446      1.16     pooka 	 * clause based on more than one operation.  Unfortunately such
    447      1.16     pooka 	 * operations (read, write) do not reliably work yet.
    448      1.16     pooka 	 *
    449      1.16     pooka 	 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
    450      1.18     pooka 	 *
    451      1.18     pooka 	 * XXX: and there is one more problem.  We sometimes need to
    452      1.18     pooka 	 * take a lazy lock in case the fs is suspending and we are
    453      1.18     pooka 	 * executing as the fs server context.  This might happen
    454      1.18     pooka 	 * e.g. in the case that the user server triggers a reclaim
    455      1.18     pooka 	 * in the kernel while the fs is suspending.  It's not a very
    456      1.18     pooka 	 * likely event, but it needs to be fixed some day.
    457      1.16     pooka 	 */
    458      1.22     pooka 
    459      1.22     pooka 	/*
    460      1.22     pooka 	 * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
    461      1.22     pooka 	 * the mutex here, since getpages() might be called locked.
    462      1.22     pooka 	 */
    463      1.18     pooka 	fstrans_start(mp, FSTRANS_NORMAL);
    464      1.22     pooka 	mutex_enter(&pmp->pmp_lock);
    465      1.16     pooka 	fstrans_done(mp);
    466      1.16     pooka 
    467      1.13     pooka 	if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    468      1.22     pooka 		mutex_exit(&pmp->pmp_lock);
    469       1.1     pooka 		return ENXIO;
    470       1.1     pooka 	}
    471       1.1     pooka 
    472      1.26     pooka #ifdef PUFFSDEBUG
    473  1.40.4.3     joerg 	parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
    474  1.40.4.3     joerg 	parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
    475      1.26     pooka #endif
    476      1.26     pooka 
    477  1.40.4.3     joerg 	mutex_enter(&park->park_mtx);
    478  1.40.4.3     joerg 	TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
    479      1.26     pooka 	park->park_flags |= PARKFLAG_ONQUEUE1;
    480      1.34     pooka 	puffs_mp_reference(pmp);
    481  1.40.4.3     joerg 	pmp->pmp_msg_touser_count++;
    482      1.26     pooka 	mutex_exit(&pmp->pmp_lock);
    483       1.1     pooka 
    484      1.20     pooka 	DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
    485      1.25     pooka 	    "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
    486      1.25     pooka 	    preq->preq_opclass, preq->preq_optype, park->park_flags));
    487      1.15     pooka 
    488  1.40.4.3     joerg 	cv_broadcast(&pmp->pmp_msg_waiter_cv);
    489       1.1     pooka 	selnotify(pmp->pmp_sel, 0);
    490       1.1     pooka 
    491      1.31     pooka 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    492      1.25     pooka 	    && (park->park_flags & PARKFLAG_CALL) == 0) {
    493      1.19     pooka 		int error;
    494      1.19     pooka 
    495      1.26     pooka 		error = cv_wait_sig(&park->park_cv, &park->park_mtx);
    496  1.40.4.3     joerg 		DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
    497  1.40.4.3     joerg 		    park, error));
    498      1.19     pooka 		if (error) {
    499      1.25     pooka 			park->park_flags |= PARKFLAG_WAITERGONE;
    500      1.26     pooka 			if (park->park_flags & PARKFLAG_DONE) {
    501      1.22     pooka 				rv = preq->preq_rv;
    502      1.19     pooka 			} else {
    503      1.26     pooka 				/*
    504      1.26     pooka 				 * ok, we marked it as going away, but
    505      1.26     pooka 				 * still need to do queue ops.  take locks
    506      1.26     pooka 				 * in correct order.
    507      1.26     pooka 				 *
    508      1.26     pooka 				 * We don't want to release our reference
    509      1.26     pooka 				 * if it's on replywait queue to avoid error
    510      1.26     pooka 				 * to file server.  putop() code will DTRT.
    511      1.26     pooka 				 */
    512      1.26     pooka 				mutex_exit(&park->park_mtx);
    513      1.26     pooka 				mutex_enter(&pmp->pmp_lock);
    514      1.26     pooka 				mutex_enter(&park->park_mtx);
    515  1.40.4.3     joerg 
    516  1.40.4.3     joerg 				/* remove from queue1 */
    517      1.36     pooka 				if (park->park_flags & PARKFLAG_ONQUEUE1) {
    518  1.40.4.3     joerg 					TAILQ_REMOVE(&pmp->pmp_msg_touser,
    519      1.26     pooka 					    park, park_entries);
    520  1.40.4.3     joerg 					pmp->pmp_msg_touser_count--;
    521      1.36     pooka 					park->park_flags &= ~PARKFLAG_ONQUEUE1;
    522      1.36     pooka 				}
    523  1.40.4.3     joerg 
    524  1.40.4.3     joerg 				/*
    525  1.40.4.3     joerg 				 * If it's waiting for a response already,
    526  1.40.4.3     joerg 				 * boost reference count.  Park will get
    527  1.40.4.3     joerg 				 * nuked once the response arrives from
    528  1.40.4.3     joerg 				 * the file server.
    529  1.40.4.3     joerg 				 */
    530  1.40.4.3     joerg 				if (park->park_flags & PARKFLAG_ONQUEUE2)
    531  1.40.4.3     joerg 					puffs_msgpark_reference(park);
    532  1.40.4.3     joerg 
    533      1.26     pooka 				mutex_exit(&pmp->pmp_lock);
    534      1.26     pooka 
    535      1.22     pooka 				rv = error;
    536      1.19     pooka 			}
    537      1.22     pooka 		} else {
    538      1.22     pooka 			rv = preq->preq_rv;
    539      1.19     pooka 		}
    540      1.22     pooka 
    541      1.16     pooka 		/*
    542      1.16     pooka 		 * retake the lock and release.  This makes sure (haha,
    543      1.16     pooka 		 * I'm humorous) that we don't process the same vnode in
    544      1.16     pooka 		 * multiple threads due to the locks hacks we have in
    545      1.16     pooka 		 * puffs_lock().  In reality this is well protected by
    546      1.16     pooka 		 * the biglock, but once that's gone, well, hopefully
    547      1.16     pooka 		 * this will be fixed for real.  (and when you read this
    548      1.16     pooka 		 * comment in 2017 and subsequently barf, my condolences ;).
    549      1.16     pooka 		 */
    550      1.19     pooka 		if (rv == 0 && !fstrans_is_owner(mp)) {
    551      1.17   hannken 			fstrans_start(mp, FSTRANS_NORMAL);
    552      1.16     pooka 			fstrans_done(mp);
    553      1.16     pooka 		}
    554  1.40.4.5  jmcneill 
    555      1.22     pooka 	} else {
    556  1.40.4.3     joerg 		/*
    557  1.40.4.3     joerg 		 * Take extra reference for FAF, i.e. don't free us
    558  1.40.4.3     joerg 		 * immediately upon return to the caller, but rather
    559  1.40.4.3     joerg 		 * only when the message has been transported.
    560  1.40.4.3     joerg 		 */
    561  1.40.4.3     joerg 		puffs_msgpark_reference(park);
    562      1.16     pooka 	}
    563      1.16     pooka 
    564  1.40.4.3     joerg 	mutex_exit(&park->park_mtx);
    565  1.40.4.3     joerg 
    566      1.22     pooka 	mutex_enter(&pmp->pmp_lock);
    567      1.34     pooka 	puffs_mp_release(pmp);
    568      1.22     pooka 	mutex_exit(&pmp->pmp_lock);
    569      1.16     pooka 
    570      1.19     pooka 	return rv;
    571       1.1     pooka }
    572       1.1     pooka 
    573       1.9     pooka /*
    574  1.40.4.3     joerg  * Get next request in the outgoing queue.  "maxsize" controls the
    575  1.40.4.3     joerg  * size the caller can accommodate and "nonblock" signals if this
    576  1.40.4.3     joerg  * should block while waiting for input.  Handles all locking internally.
    577       1.9     pooka  */
    578      1.10     pooka int
    579  1.40.4.3     joerg puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
    580  1.40.4.3     joerg 	uint8_t **data, size_t *dlen, void **parkptr)
    581       1.1     pooka {
    582  1.40.4.3     joerg 	struct puffs_mount *pmp = this;
    583  1.40.4.3     joerg 	struct puffs_msgpark *park;
    584       1.9     pooka 	struct puffs_req *preq;
    585  1.40.4.3     joerg 	int error;
    586       1.1     pooka 
    587  1.40.4.3     joerg 	error = 0;
    588      1.22     pooka 	mutex_enter(&pmp->pmp_lock);
    589  1.40.4.4     joerg 	puffs_mp_reference(pmp);
    590  1.40.4.3     joerg 	for (;;) {
    591  1.40.4.3     joerg 		/* RIP? */
    592       1.9     pooka 		if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    593       1.9     pooka 			error = ENXIO;
    594  1.40.4.3     joerg 			break;
    595       1.9     pooka 		}
    596      1.12     pooka 
    597  1.40.4.3     joerg 		/* need platinum yendorian express card? */
    598  1.40.4.3     joerg 		if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
    599  1.40.4.3     joerg 			DPRINTF(("puffs_getout: no outgoing op, "));
    600      1.12     pooka 			if (nonblock) {
    601  1.40.4.3     joerg 				DPRINTF(("returning EWOULDBLOCK\n"));
    602      1.12     pooka 				error = EWOULDBLOCK;
    603  1.40.4.3     joerg 				break;
    604       1.9     pooka 			}
    605  1.40.4.3     joerg 			DPRINTF(("waiting ...\n"));
    606       1.9     pooka 
    607  1.40.4.3     joerg 			error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
    608      1.22     pooka 			    &pmp->pmp_lock);
    609      1.11     pooka 			if (error)
    610  1.40.4.3     joerg 				break;
    611      1.11     pooka 			else
    612  1.40.4.3     joerg 				continue;
    613       1.9     pooka 		}
    614       1.9     pooka 
    615  1.40.4.3     joerg 		park = TAILQ_FIRST(&pmp->pmp_msg_touser);
    616  1.40.4.4     joerg 		if (park == NULL)
    617  1.40.4.4     joerg 			continue;
    618  1.40.4.4     joerg 
    619  1.40.4.3     joerg 		mutex_enter(&park->park_mtx);
    620  1.40.4.3     joerg 		puffs_msgpark_reference(park);
    621  1.40.4.3     joerg 
    622  1.40.4.3     joerg 		DPRINTF(("puffs_getout: found park at %p, ", park));
    623      1.22     pooka 
    624      1.22     pooka 		/* If it's a goner, don't process any furher */
    625      1.22     pooka 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    626  1.40.4.3     joerg 			DPRINTF(("waitergone!\n"));
    627  1.40.4.3     joerg 			puffs_msgpark_release(park);
    628      1.22     pooka 			continue;
    629      1.22     pooka 		}
    630      1.22     pooka 
    631  1.40.4.3     joerg 		/* check size */
    632      1.26     pooka 		preq = park->park_preq;
    633  1.40.4.3     joerg 		if (maxsize < preq->preq_frhdr.pfr_len) {
    634  1.40.4.3     joerg 			DPRINTF(("buffer too small\n"));
    635  1.40.4.3     joerg 			puffs_msgpark_release(park);
    636  1.40.4.3     joerg 			error = E2BIG;
    637  1.40.4.3     joerg 			break;
    638      1.26     pooka 		}
    639      1.28     pooka 
    640  1.40.4.3     joerg 		DPRINTF(("returning\n"));
    641  1.40.4.3     joerg 
    642  1.40.4.3     joerg 		/*
    643  1.40.4.3     joerg 		 * Ok, we found what we came for.  Release it from the
    644  1.40.4.3     joerg 		 * outgoing queue but do not unlock.  We will unlock
    645  1.40.4.3     joerg 		 * only after we "releaseout" it to avoid complications:
    646  1.40.4.3     joerg 		 * otherwise it is (theoretically) possible for userland
    647  1.40.4.3     joerg 		 * to race us into "put" before we have a change to put
    648  1.40.4.3     joerg 		 * this baby on the receiving queue.
    649  1.40.4.3     joerg 		 */
    650  1.40.4.3     joerg 		TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
    651      1.28     pooka 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    652      1.28     pooka 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    653  1.40.4.3     joerg 		mutex_exit(&park->park_mtx);
    654      1.26     pooka 
    655  1.40.4.3     joerg 		pmp->pmp_msg_touser_count--;
    656  1.40.4.3     joerg 		KASSERT(pmp->pmp_msg_touser_count >= 0);
    657       1.9     pooka 
    658  1.40.4.3     joerg 		break;
    659  1.40.4.3     joerg 	}
    660  1.40.4.4     joerg 	puffs_mp_release(pmp);
    661  1.40.4.3     joerg 	mutex_exit(&pmp->pmp_lock);
    662       1.9     pooka 
    663  1.40.4.3     joerg 	if (error == 0) {
    664  1.40.4.3     joerg 		*data = (uint8_t *)preq;
    665  1.40.4.3     joerg 		preq->preq_frhdr.pfr_len = park->park_copylen;
    666  1.40.4.3     joerg 		preq->preq_frhdr.pfr_alloclen = park->park_maxlen;
    667  1.40.4.3     joerg 		preq->preq_frhdr.pfr_type = preq->preq_opclass; /* yay! */
    668  1.40.4.3     joerg 		*dlen = preq->preq_frhdr.pfr_len;
    669  1.40.4.3     joerg 		*parkptr = park;
    670  1.40.4.3     joerg 	}
    671  1.40.4.5  jmcneill 
    672  1.40.4.3     joerg 	return error;
    673  1.40.4.3     joerg }
    674  1.40.4.3     joerg 
    675  1.40.4.3     joerg /*
    676  1.40.4.3     joerg  * Release outgoing structure.  Now, depending on the success of the
    677  1.40.4.3     joerg  * outgoing send, it is either going onto the result waiting queue
    678  1.40.4.3     joerg  * or the death chamber.
    679  1.40.4.3     joerg  */
    680  1.40.4.3     joerg void
    681  1.40.4.3     joerg puffs_msgif_releaseout(void *this, void *parkptr, int status)
    682  1.40.4.3     joerg {
    683  1.40.4.3     joerg 	struct puffs_mount *pmp = this;
    684  1.40.4.3     joerg 	struct puffs_msgpark *park = parkptr;
    685      1.32     pooka 
    686  1.40.4.3     joerg 	DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
    687  1.40.4.3     joerg 	    park, status));
    688  1.40.4.3     joerg 	mutex_enter(&pmp->pmp_lock);
    689  1.40.4.3     joerg 	mutex_enter(&park->park_mtx);
    690  1.40.4.3     joerg 	if (park->park_flags & PARKFLAG_WANTREPLY) {
    691  1.40.4.3     joerg 		if (status == 0) {
    692  1.40.4.3     joerg 			DPRINTF(("enqueue replywait\n"));
    693  1.40.4.3     joerg 			TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
    694      1.22     pooka 			    park_entries);
    695      1.26     pooka 			park->park_flags |= PARKFLAG_ONQUEUE2;
    696       1.9     pooka 		} else {
    697  1.40.4.3     joerg 			DPRINTF(("error path!\n"));
    698  1.40.4.3     joerg 			park->park_preq->preq_rv = status;
    699  1.40.4.3     joerg 			park->park_flags |= PARKFLAG_DONE;
    700  1.40.4.3     joerg 			cv_signal(&park->park_cv);
    701       1.1     pooka 		}
    702  1.40.4.3     joerg 		puffs_msgpark_release(park);
    703  1.40.4.3     joerg 	} else {
    704  1.40.4.3     joerg 		DPRINTF(("release\n"));
    705  1.40.4.3     joerg 		puffs_msgpark_release1(park, 2);
    706       1.1     pooka 	}
    707      1.22     pooka 	mutex_exit(&pmp->pmp_lock);
    708       1.1     pooka }
    709       1.1     pooka 
    710  1.40.4.4     joerg /*
    711  1.40.4.4     joerg  * XXX: locking with this one?
    712  1.40.4.4     joerg  */
    713  1.40.4.3     joerg void
    714  1.40.4.3     joerg puffs_msgif_incoming(void *this, void *buf)
    715       1.1     pooka {
    716  1.40.4.3     joerg 	struct puffs_mount *pmp = this;
    717  1.40.4.3     joerg 	struct puffs_req *preq = buf;
    718  1.40.4.3     joerg 	struct puffs_frame *pfr = &preq->preq_frhdr;
    719  1.40.4.3     joerg 	struct puffs_msgpark *park;
    720  1.40.4.3     joerg 	int release, wgone;
    721  1.40.4.3     joerg 
    722  1.40.4.3     joerg 	/* XXX */
    723  1.40.4.3     joerg 	if (PUFFSOP_OPCLASS(preq->preq_opclass) != PUFFSOP_VN
    724  1.40.4.3     joerg 	    && PUFFSOP_OPCLASS(preq->preq_opclass) != PUFFSOP_VFS)
    725  1.40.4.3     joerg 		return;
    726       1.1     pooka 
    727      1.22     pooka 	mutex_enter(&pmp->pmp_lock);
    728      1.24     pooka 
    729  1.40.4.3     joerg 	/* Locate waiter */
    730  1.40.4.3     joerg 	TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
    731  1.40.4.3     joerg 		if (park->park_preq->preq_id == preq->preq_id)
    732      1.24     pooka 			break;
    733  1.40.4.3     joerg 	}
    734  1.40.4.3     joerg 	if (park == NULL) {
    735  1.40.4.3     joerg 		DPRINTF(("puffs_msgif_income: no request: %" PRIu64 "\n",
    736  1.40.4.3     joerg 		    preq->preq_id));
    737  1.40.4.3     joerg 		mutex_exit(&pmp->pmp_lock);
    738  1.40.4.3     joerg 		return; /* XXX send error */
    739  1.40.4.3     joerg 	}
    740      1.26     pooka 
    741  1.40.4.3     joerg 	mutex_enter(&park->park_mtx);
    742  1.40.4.3     joerg 	puffs_msgpark_reference(park);
    743  1.40.4.3     joerg 	if (pfr->pfr_len > park->park_maxlen) {
    744  1.40.4.3     joerg 		DPRINTF(("puffs_msgif_income: invalid buffer length: "
    745  1.40.4.3     joerg 		    "%zu (req %" PRIu64 ", \n", pfr->pfr_len, preq->preq_id));
    746  1.40.4.3     joerg 		park->park_preq->preq_rv = EPROTO;
    747  1.40.4.3     joerg 		cv_signal(&park->park_cv);
    748  1.40.4.3     joerg 		puffs_msgpark_release(park);
    749      1.22     pooka 		mutex_exit(&pmp->pmp_lock);
    750  1.40.4.3     joerg 		return; /* XXX: error */
    751  1.40.4.3     joerg 	}
    752  1.40.4.3     joerg 	wgone = park->park_flags & PARKFLAG_WAITERGONE;
    753       1.9     pooka 
    754  1.40.4.3     joerg 	KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
    755  1.40.4.3     joerg 	TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
    756  1.40.4.3     joerg 	park->park_flags &= ~PARKFLAG_ONQUEUE2;
    757  1.40.4.3     joerg 	mutex_exit(&pmp->pmp_lock);
    758      1.24     pooka 
    759  1.40.4.3     joerg 	if (wgone) {
    760  1.40.4.3     joerg 		DPRINTF(("puffs_putop: bad service - waiter gone for "
    761  1.40.4.3     joerg 		    "park %p\n", park));
    762  1.40.4.3     joerg 		release = 2;
    763  1.40.4.3     joerg 	} else {
    764      1.24     pooka 		if (park->park_flags & PARKFLAG_CALL) {
    765  1.40.4.3     joerg 			DPRINTF(("puffs_msgif_income: call for %p, arg %p\n",
    766      1.40     pooka 			    park->park_preq, park->park_donearg));
    767  1.40.4.3     joerg 			park->park_done(pmp, buf, park->park_donearg);
    768  1.40.4.3     joerg 			release = 2;
    769  1.40.4.3     joerg 		} else {
    770  1.40.4.3     joerg 			/* XXX: yes, I know */
    771  1.40.4.3     joerg 			memcpy(park->park_preq, buf, pfr->pfr_len);
    772      1.26     pooka 			release = 1;
    773      1.20     pooka 		}
    774       1.1     pooka 	}
    775       1.1     pooka 
    776  1.40.4.3     joerg 	if (!wgone) {
    777  1.40.4.3     joerg 		DPRINTF(("puffs_putop: flagging done for "
    778  1.40.4.3     joerg 		    "park %p\n", park));
    779  1.40.4.3     joerg 		cv_signal(&park->park_cv);
    780  1.40.4.3     joerg 	}
    781       1.1     pooka 
    782  1.40.4.3     joerg 	park->park_flags |= PARKFLAG_DONE;
    783  1.40.4.3     joerg 	puffs_msgpark_release1(park, release);
    784       1.1     pooka }
    785       1.1     pooka 
    786      1.22     pooka /*
    787      1.22     pooka  * We're dead, kaput, RIP, slightly more than merely pining for the
    788      1.22     pooka  * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
    789      1.22     pooka  * our maker, ceased to be, etcetc.  YASD.  It's a dead FS!
    790      1.22     pooka  *
    791      1.22     pooka  * Caller must hold puffs mutex.
    792      1.22     pooka  */
    793      1.22     pooka void
    794      1.22     pooka puffs_userdead(struct puffs_mount *pmp)
    795      1.22     pooka {
    796  1.40.4.3     joerg 	struct puffs_msgpark *park, *park_next;
    797      1.22     pooka 
    798      1.22     pooka 	/*
    799      1.22     pooka 	 * Mark filesystem status as dying so that operations don't
    800      1.22     pooka 	 * attempt to march to userspace any longer.
    801      1.22     pooka 	 */
    802      1.22     pooka 	pmp->pmp_status = PUFFSTAT_DYING;
    803      1.22     pooka 
    804      1.22     pooka 	/* signal waiters on REQUEST TO file server queue */
    805  1.40.4.3     joerg 	for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
    806      1.24     pooka 		uint8_t opclass;
    807      1.24     pooka 
    808  1.40.4.3     joerg 		mutex_enter(&park->park_mtx);
    809  1.40.4.3     joerg 		puffs_msgpark_reference(park);
    810      1.32     pooka 		park_next = TAILQ_NEXT(park, park_entries);
    811      1.26     pooka 
    812      1.26     pooka 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    813  1.40.4.3     joerg 		TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
    814      1.26     pooka 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    815  1.40.4.3     joerg 		pmp->pmp_msg_touser_count--;
    816      1.22     pooka 
    817      1.31     pooka 		/*
    818  1.40.4.5  jmcneill 		 * Even though waiters on QUEUE1 are removed in touser()
    819  1.40.4.5  jmcneill 		 * in case of WAITERGONE, it is still possible for us to
    820  1.40.4.5  jmcneill 		 * get raced here due to having to retake locks in said
    821  1.40.4.5  jmcneill 		 * touser().  In the race case simply "ignore" the item
    822  1.40.4.5  jmcneill 		 * on the queue and move on to the next one.
    823      1.31     pooka 		 */
    824      1.31     pooka 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    825      1.31     pooka 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    826      1.31     pooka 			KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    827  1.40.4.3     joerg 			puffs_msgpark_release(park);
    828  1.40.4.5  jmcneill 
    829      1.22     pooka 		} else {
    830      1.31     pooka 			opclass = park->park_preq->preq_opclass;
    831      1.23     pooka 			park->park_preq->preq_rv = ENXIO;
    832      1.31     pooka 
    833      1.31     pooka 			if (park->park_flags & PARKFLAG_CALL) {
    834  1.40.4.1     joerg 				park->park_done(pmp, park->park_preq,
    835      1.31     pooka 				    park->park_donearg);
    836  1.40.4.3     joerg 				puffs_msgpark_release1(park, 2);
    837      1.31     pooka 			} else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
    838  1.40.4.3     joerg 				puffs_msgpark_release1(park, 2);
    839      1.31     pooka 			} else {
    840      1.31     pooka 				park->park_preq->preq_rv = ENXIO;
    841      1.31     pooka 				cv_signal(&park->park_cv);
    842  1.40.4.3     joerg 				puffs_msgpark_release(park);
    843      1.31     pooka 			}
    844      1.22     pooka 		}
    845      1.22     pooka 	}
    846      1.22     pooka 
    847      1.22     pooka 	/* signal waiters on RESPONSE FROM file server queue */
    848  1.40.4.3     joerg 	for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
    849  1.40.4.3     joerg 		mutex_enter(&park->park_mtx);
    850  1.40.4.3     joerg 		puffs_msgpark_reference(park);
    851      1.32     pooka 		park_next = TAILQ_NEXT(park, park_entries);
    852      1.26     pooka 
    853      1.26     pooka 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
    854      1.31     pooka 		KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    855      1.26     pooka 
    856  1.40.4.3     joerg 		TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
    857      1.26     pooka 		park->park_flags &= ~PARKFLAG_ONQUEUE2;
    858      1.26     pooka 
    859      1.31     pooka 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    860      1.31     pooka 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    861  1.40.4.3     joerg 			puffs_msgpark_release(park);
    862      1.22     pooka 		} else {
    863      1.31     pooka 			park->park_preq->preq_rv = ENXIO;
    864      1.31     pooka 			if (park->park_flags & PARKFLAG_CALL) {
    865  1.40.4.1     joerg 				park->park_done(pmp, park->park_preq,
    866      1.31     pooka 				    park->park_donearg);
    867  1.40.4.3     joerg 				puffs_msgpark_release1(park, 2);
    868      1.31     pooka 			} else {
    869      1.31     pooka 				cv_signal(&park->park_cv);
    870  1.40.4.3     joerg 				puffs_msgpark_release(park);
    871      1.31     pooka 			}
    872      1.22     pooka 		}
    873      1.22     pooka 	}
    874  1.40.4.4     joerg 
    875  1.40.4.4     joerg 	cv_broadcast(&pmp->pmp_msg_waiter_cv);
    876      1.22     pooka }
    877