Home | History | Annotate | Line # | Download | only in puffs
puffs_msgif.c revision 1.13.2.6
      1  1.13.2.6  yamt /*	$NetBSD: puffs_msgif.c,v 1.13.2.6 2007/11/15 11:44:35 yamt Exp $	*/
      2  1.13.2.2  yamt 
      3  1.13.2.2  yamt /*
      4  1.13.2.3  yamt  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
      5  1.13.2.2  yamt  *
      6  1.13.2.2  yamt  * Development of this software was supported by the
      7  1.13.2.2  yamt  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8  1.13.2.2  yamt  * The Google SoC project was mentored by Bill Studenmund.
      9  1.13.2.2  yamt  *
     10  1.13.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.13.2.2  yamt  * modification, are permitted provided that the following conditions
     12  1.13.2.2  yamt  * are met:
     13  1.13.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.13.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.13.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.13.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.13.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.13.2.2  yamt  *
     19  1.13.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20  1.13.2.2  yamt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  1.13.2.2  yamt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  1.13.2.2  yamt  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  1.13.2.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.13.2.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  1.13.2.2  yamt  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.13.2.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.13.2.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.13.2.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.13.2.2  yamt  * SUCH DAMAGE.
     30  1.13.2.2  yamt  */
     31  1.13.2.2  yamt 
     32  1.13.2.2  yamt #include <sys/cdefs.h>
     33  1.13.2.6  yamt __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.13.2.6 2007/11/15 11:44:35 yamt Exp $");
     34  1.13.2.2  yamt 
     35  1.13.2.2  yamt #include <sys/param.h>
     36  1.13.2.3  yamt #include <sys/fstrans.h>
     37  1.13.2.5  yamt #include <sys/kmem.h>
     38  1.13.2.6  yamt #include <sys/kthread.h>
     39  1.13.2.6  yamt #include <sys/lock.h>
     40  1.13.2.2  yamt #include <sys/malloc.h>
     41  1.13.2.2  yamt #include <sys/mount.h>
     42  1.13.2.6  yamt #include <sys/namei.h>
     43  1.13.2.4  yamt #include <sys/proc.h>
     44  1.13.2.6  yamt #include <sys/vnode.h>
     45  1.13.2.6  yamt 
     46  1.13.2.6  yamt #include <dev/putter/putter_sys.h>
     47  1.13.2.2  yamt 
     48  1.13.2.2  yamt #include <fs/puffs/puffs_msgif.h>
     49  1.13.2.2  yamt #include <fs/puffs/puffs_sys.h>
     50  1.13.2.2  yamt 
     51  1.13.2.6  yamt #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
     52  1.13.2.6  yamt 
     53  1.13.2.4  yamt /*
     54  1.13.2.4  yamt  * waitq data structures
     55  1.13.2.4  yamt  */
     56  1.13.2.4  yamt 
     57  1.13.2.4  yamt /*
     58  1.13.2.4  yamt  * While a request is going to userspace, park the caller within the
     59  1.13.2.4  yamt  * kernel.  This is the kernel counterpart of "struct puffs_req".
     60  1.13.2.4  yamt  */
     61  1.13.2.5  yamt struct puffs_msgpark {
     62  1.13.2.4  yamt 	struct puffs_req	*park_preq;	/* req followed by buf	*/
     63  1.13.2.4  yamt 
     64  1.13.2.4  yamt 	size_t			park_copylen;	/* userspace copylength	*/
     65  1.13.2.4  yamt 	size_t			park_maxlen;	/* max size in comeback */
     66  1.13.2.4  yamt 
     67  1.13.2.5  yamt 	parkdone_fn		park_done;	/* "biodone" a'la puffs	*/
     68  1.13.2.4  yamt 	void			*park_donearg;
     69  1.13.2.4  yamt 
     70  1.13.2.4  yamt 	int			park_flags;
     71  1.13.2.4  yamt 	int			park_refcount;
     72  1.13.2.4  yamt 
     73  1.13.2.4  yamt 	kcondvar_t		park_cv;
     74  1.13.2.4  yamt 	kmutex_t		park_mtx;
     75  1.13.2.4  yamt 
     76  1.13.2.5  yamt 	TAILQ_ENTRY(puffs_msgpark) park_entries;
     77  1.13.2.4  yamt };
     78  1.13.2.4  yamt #define PARKFLAG_WAITERGONE	0x01
     79  1.13.2.4  yamt #define PARKFLAG_DONE		0x02
     80  1.13.2.4  yamt #define PARKFLAG_ONQUEUE1	0x04
     81  1.13.2.4  yamt #define PARKFLAG_ONQUEUE2	0x08
     82  1.13.2.4  yamt #define PARKFLAG_CALL		0x10
     83  1.13.2.4  yamt #define PARKFLAG_WANTREPLY	0x20
     84  1.13.2.4  yamt 
     85  1.13.2.6  yamt static pool_cache_t parkpc;
     86  1.13.2.4  yamt 
     87  1.13.2.4  yamt static int
     88  1.13.2.4  yamt makepark(void *arg, void *obj, int flags)
     89  1.13.2.4  yamt {
     90  1.13.2.5  yamt 	struct puffs_msgpark *park = obj;
     91  1.13.2.4  yamt 
     92  1.13.2.4  yamt 	mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
     93  1.13.2.4  yamt 	cv_init(&park->park_cv, "puffsrpl");
     94  1.13.2.4  yamt 
     95  1.13.2.4  yamt 	return 0;
     96  1.13.2.4  yamt }
     97  1.13.2.4  yamt 
     98  1.13.2.4  yamt static void
     99  1.13.2.4  yamt nukepark(void *arg, void *obj)
    100  1.13.2.4  yamt {
    101  1.13.2.5  yamt 	struct puffs_msgpark *park = obj;
    102  1.13.2.4  yamt 
    103  1.13.2.4  yamt 	cv_destroy(&park->park_cv);
    104  1.13.2.4  yamt 	mutex_destroy(&park->park_mtx);
    105  1.13.2.4  yamt }
    106  1.13.2.4  yamt 
    107  1.13.2.4  yamt void
    108  1.13.2.4  yamt puffs_msgif_init()
    109  1.13.2.4  yamt {
    110  1.13.2.4  yamt 
    111  1.13.2.6  yamt 	parkpc = pool_cache_init(sizeof(struct puffs_msgpark), 0, 0, 0,
    112  1.13.2.6  yamt 	    "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
    113  1.13.2.4  yamt }
    114  1.13.2.4  yamt 
    115  1.13.2.4  yamt void
    116  1.13.2.4  yamt puffs_msgif_destroy()
    117  1.13.2.4  yamt {
    118  1.13.2.4  yamt 
    119  1.13.2.6  yamt 	pool_cache_destroy(parkpc);
    120  1.13.2.4  yamt }
    121  1.13.2.4  yamt 
    122  1.13.2.5  yamt static int alloced;
    123  1.13.2.5  yamt 
    124  1.13.2.5  yamt static struct puffs_msgpark *
    125  1.13.2.5  yamt puffs_msgpark_alloc(int waitok)
    126  1.13.2.4  yamt {
    127  1.13.2.5  yamt 	struct puffs_msgpark *park;
    128  1.13.2.4  yamt 
    129  1.13.2.6  yamt 	park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
    130  1.13.2.5  yamt 	if (park == NULL)
    131  1.13.2.5  yamt 		return park;
    132  1.13.2.5  yamt 
    133  1.13.2.5  yamt 	park->park_refcount = 1;
    134  1.13.2.5  yamt 	park->park_preq = NULL;
    135  1.13.2.5  yamt 	park->park_flags = PARKFLAG_WANTREPLY;
    136  1.13.2.4  yamt 
    137  1.13.2.4  yamt 	return park;
    138  1.13.2.4  yamt }
    139  1.13.2.4  yamt 
    140  1.13.2.4  yamt static void
    141  1.13.2.5  yamt puffs_msgpark_reference(struct puffs_msgpark *park)
    142  1.13.2.4  yamt {
    143  1.13.2.4  yamt 
    144  1.13.2.5  yamt 	KASSERT(mutex_owned(&park->park_mtx));
    145  1.13.2.4  yamt 	park->park_refcount++;
    146  1.13.2.4  yamt }
    147  1.13.2.4  yamt 
    148  1.13.2.5  yamt /*
    149  1.13.2.5  yamt  * Release reference to park structure.
    150  1.13.2.5  yamt  */
    151  1.13.2.5  yamt static void
    152  1.13.2.5  yamt puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
    153  1.13.2.4  yamt {
    154  1.13.2.5  yamt 	struct puffs_req *preq = park->park_preq;
    155  1.13.2.5  yamt 	int refcnt;
    156  1.13.2.4  yamt 
    157  1.13.2.4  yamt 	KASSERT(mutex_owned(&park->park_mtx));
    158  1.13.2.5  yamt 	refcnt = park->park_refcount -= howmany;
    159  1.13.2.4  yamt 	mutex_exit(&park->park_mtx);
    160  1.13.2.5  yamt 
    161  1.13.2.5  yamt 	KASSERT(refcnt >= 0);
    162  1.13.2.5  yamt 
    163  1.13.2.5  yamt 	if (refcnt == 0) {
    164  1.13.2.5  yamt 		alloced--;
    165  1.13.2.5  yamt 		if (preq)
    166  1.13.2.5  yamt 			kmem_free(preq, park->park_maxlen);
    167  1.13.2.6  yamt 		pool_cache_put(parkpc, park);
    168  1.13.2.5  yamt 	}
    169  1.13.2.4  yamt }
    170  1.13.2.5  yamt #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
    171  1.13.2.4  yamt 
    172  1.13.2.4  yamt #ifdef PUFFSDEBUG
    173  1.13.2.4  yamt static void
    174  1.13.2.5  yamt parkdump(struct puffs_msgpark *park)
    175  1.13.2.4  yamt {
    176  1.13.2.4  yamt 
    177  1.13.2.4  yamt 	DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
    178  1.13.2.4  yamt 	    "\tcopy %zu, max %zu - done: %p/%p\n"
    179  1.13.2.4  yamt 	    "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
    180  1.13.2.5  yamt 	    park, park->park_preq, park->park_preq->preq_id,
    181  1.13.2.4  yamt 	    park->park_copylen, park->park_maxlen,
    182  1.13.2.4  yamt 	    park->park_done, park->park_donearg,
    183  1.13.2.4  yamt 	    park->park_flags, park->park_refcount,
    184  1.13.2.4  yamt 	    &park->park_cv, &park->park_mtx));
    185  1.13.2.4  yamt }
    186  1.13.2.4  yamt 
    187  1.13.2.4  yamt static void
    188  1.13.2.4  yamt parkqdump(struct puffs_wq *q, int dumpall)
    189  1.13.2.4  yamt {
    190  1.13.2.5  yamt 	struct puffs_msgpark *park;
    191  1.13.2.4  yamt 	int total = 0;
    192  1.13.2.4  yamt 
    193  1.13.2.4  yamt 	TAILQ_FOREACH(park, q, park_entries) {
    194  1.13.2.4  yamt 		if (dumpall)
    195  1.13.2.4  yamt 			parkdump(park);
    196  1.13.2.4  yamt 		total++;
    197  1.13.2.4  yamt 	}
    198  1.13.2.4  yamt 	DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
    199  1.13.2.4  yamt 
    200  1.13.2.4  yamt }
    201  1.13.2.4  yamt #endif /* PUFFSDEBUG */
    202  1.13.2.4  yamt 
    203  1.13.2.4  yamt /*
    204  1.13.2.5  yamt  * A word about locking in the park structures: the lock protects the
    205  1.13.2.5  yamt  * fields of the *park* structure (not preq) and acts as an interlock
    206  1.13.2.5  yamt  * in cv operations.  The lock is always internal to this module and
    207  1.13.2.5  yamt  * callers do not need to worry about it.
    208  1.13.2.4  yamt  */
    209  1.13.2.5  yamt 
    210  1.13.2.5  yamt int
    211  1.13.2.5  yamt puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
    212  1.13.2.5  yamt 	int cansleep)
    213  1.13.2.4  yamt {
    214  1.13.2.5  yamt 	struct puffs_msgpark *park;
    215  1.13.2.5  yamt 	void *m;
    216  1.13.2.5  yamt 
    217  1.13.2.5  yamt 	m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
    218  1.13.2.5  yamt 	if (m == NULL) {
    219  1.13.2.5  yamt 		KASSERT(cansleep == 0);
    220  1.13.2.5  yamt 		return ENOMEM;
    221  1.13.2.5  yamt 	}
    222  1.13.2.4  yamt 
    223  1.13.2.5  yamt 	park = puffs_msgpark_alloc(cansleep);
    224  1.13.2.5  yamt 	if (park == NULL) {
    225  1.13.2.5  yamt 		KASSERT(cansleep == 0);
    226  1.13.2.5  yamt 		kmem_free(m, len);
    227  1.13.2.5  yamt 		return ENOMEM;
    228  1.13.2.5  yamt 	}
    229  1.13.2.4  yamt 
    230  1.13.2.5  yamt 	park->park_preq = m;
    231  1.13.2.5  yamt 	park->park_maxlen = len;
    232  1.13.2.4  yamt 
    233  1.13.2.5  yamt 	*ppark = park;
    234  1.13.2.5  yamt 	*mem = m;
    235  1.13.2.4  yamt 
    236  1.13.2.5  yamt 	return 0;
    237  1.13.2.5  yamt }
    238  1.13.2.5  yamt 
    239  1.13.2.5  yamt void
    240  1.13.2.5  yamt puffs_msgmem_release(struct puffs_msgpark *park)
    241  1.13.2.5  yamt {
    242  1.13.2.5  yamt 
    243  1.13.2.5  yamt 	if (park == NULL)
    244  1.13.2.5  yamt 		return;
    245  1.13.2.5  yamt 
    246  1.13.2.5  yamt 	mutex_enter(&park->park_mtx);
    247  1.13.2.5  yamt 	puffs_msgpark_release(park);
    248  1.13.2.4  yamt }
    249  1.13.2.4  yamt 
    250  1.13.2.5  yamt void
    251  1.13.2.5  yamt puffs_msg_setfaf(struct puffs_msgpark *park)
    252  1.13.2.5  yamt {
    253  1.13.2.5  yamt 
    254  1.13.2.5  yamt 	park->park_flags &= ~PARKFLAG_WANTREPLY;
    255  1.13.2.5  yamt }
    256  1.13.2.2  yamt 
    257  1.13.2.2  yamt /*
    258  1.13.2.2  yamt  * kernel-user-kernel waitqueues
    259  1.13.2.2  yamt  */
    260  1.13.2.2  yamt 
    261  1.13.2.5  yamt static int touser(struct puffs_mount *, struct puffs_msgpark *);
    262  1.13.2.2  yamt 
    263  1.13.2.5  yamt static uint64_t
    264  1.13.2.5  yamt puffs_getmsgid(struct puffs_mount *pmp)
    265  1.13.2.2  yamt {
    266  1.13.2.3  yamt 	uint64_t rv;
    267  1.13.2.2  yamt 
    268  1.13.2.4  yamt 	mutex_enter(&pmp->pmp_lock);
    269  1.13.2.5  yamt 	rv = pmp->pmp_nextmsgid++;
    270  1.13.2.4  yamt 	mutex_exit(&pmp->pmp_lock);
    271  1.13.2.2  yamt 
    272  1.13.2.2  yamt 	return rv;
    273  1.13.2.2  yamt }
    274  1.13.2.2  yamt 
    275  1.13.2.2  yamt /* vfs request */
    276  1.13.2.2  yamt int
    277  1.13.2.5  yamt puffs_msg_vfs(struct puffs_mount *pmp, struct puffs_msgpark *park, int optype)
    278  1.13.2.2  yamt {
    279  1.13.2.2  yamt 
    280  1.13.2.4  yamt 	park->park_preq->preq_opclass = PUFFSOP_VFS;
    281  1.13.2.4  yamt 	park->park_preq->preq_optype = optype;
    282  1.13.2.2  yamt 
    283  1.13.2.5  yamt 	park->park_copylen = park->park_maxlen;
    284  1.13.2.2  yamt 
    285  1.13.2.5  yamt 	return touser(pmp, park);
    286  1.13.2.3  yamt }
    287  1.13.2.3  yamt 
    288  1.13.2.2  yamt /*
    289  1.13.2.2  yamt  * vnode level request
    290  1.13.2.2  yamt  */
    291  1.13.2.2  yamt int
    292  1.13.2.5  yamt puffs_msg_vn(struct puffs_mount *pmp, struct puffs_msgpark *park,
    293  1.13.2.5  yamt 	int optype, size_t delta, struct vnode *vp_opc, struct vnode *vp_aux)
    294  1.13.2.2  yamt {
    295  1.13.2.4  yamt 	struct puffs_req *preq;
    296  1.13.2.4  yamt 	void *cookie = VPTOPNC(vp_opc);
    297  1.13.2.4  yamt 	struct puffs_node *pnode;
    298  1.13.2.4  yamt 	int rv;
    299  1.13.2.4  yamt 
    300  1.13.2.4  yamt 	park->park_preq->preq_opclass = PUFFSOP_VN;
    301  1.13.2.4  yamt 	park->park_preq->preq_optype = optype;
    302  1.13.2.4  yamt 	park->park_preq->preq_cookie = cookie;
    303  1.13.2.4  yamt 
    304  1.13.2.5  yamt 	KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
    305  1.13.2.5  yamt 	park->park_copylen = park->park_maxlen - delta;
    306  1.13.2.2  yamt 
    307  1.13.2.5  yamt 	rv = touser(pmp, park);
    308  1.13.2.2  yamt 
    309  1.13.2.4  yamt 	/*
    310  1.13.2.4  yamt 	 * Check if the user server requests that inactive be called
    311  1.13.2.4  yamt 	 * when the time is right.
    312  1.13.2.4  yamt 	 */
    313  1.13.2.4  yamt 	preq = park->park_preq;
    314  1.13.2.4  yamt 	if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N1) {
    315  1.13.2.4  yamt 		pnode = vp_opc->v_data;
    316  1.13.2.4  yamt 		pnode->pn_stat |= PNODE_DOINACT;
    317  1.13.2.4  yamt 	}
    318  1.13.2.4  yamt 	if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N2) {
    319  1.13.2.4  yamt 		/* if no vp_aux, just ignore */
    320  1.13.2.4  yamt 		if (vp_aux) {
    321  1.13.2.4  yamt 			pnode = vp_aux->v_data;
    322  1.13.2.4  yamt 			pnode->pn_stat |= PNODE_DOINACT;
    323  1.13.2.4  yamt 		}
    324  1.13.2.4  yamt 	}
    325  1.13.2.4  yamt 	if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1) {
    326  1.13.2.4  yamt 		pnode = vp_opc->v_data;
    327  1.13.2.4  yamt 		pnode->pn_stat |= PNODE_NOREFS;
    328  1.13.2.4  yamt 	}
    329  1.13.2.4  yamt 	if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2) {
    330  1.13.2.4  yamt 		/* if no vp_aux, just ignore */
    331  1.13.2.4  yamt 		if (vp_aux) {
    332  1.13.2.4  yamt 			pnode = vp_aux->v_data;
    333  1.13.2.4  yamt 			pnode->pn_stat |= PNODE_NOREFS;
    334  1.13.2.4  yamt 		}
    335  1.13.2.4  yamt 	}
    336  1.13.2.2  yamt 
    337  1.13.2.4  yamt 	return rv;
    338  1.13.2.2  yamt }
    339  1.13.2.2  yamt 
    340  1.13.2.4  yamt void
    341  1.13.2.5  yamt puffs_msg_vncall(struct puffs_mount *pmp, struct puffs_msgpark *park,
    342  1.13.2.5  yamt 	int optype, size_t delta, parkdone_fn donefn, void *donearg,
    343  1.13.2.5  yamt 	struct vnode *vp_opc)
    344  1.13.2.2  yamt {
    345  1.13.2.4  yamt 	void *cookie = VPTOPNC(vp_opc);
    346  1.13.2.2  yamt 
    347  1.13.2.4  yamt 	park->park_preq->preq_opclass = PUFFSOP_VN;
    348  1.13.2.4  yamt 	park->park_preq->preq_optype = optype;
    349  1.13.2.4  yamt 	park->park_preq->preq_cookie = cookie;
    350  1.13.2.4  yamt 
    351  1.13.2.5  yamt 	KASSERT(delta < park->park_maxlen);
    352  1.13.2.5  yamt 	park->park_copylen = park->park_maxlen - delta;
    353  1.13.2.4  yamt 	park->park_done = donefn;
    354  1.13.2.4  yamt 	park->park_donearg = donearg;
    355  1.13.2.5  yamt 	park->park_flags |= PARKFLAG_CALL;
    356  1.13.2.2  yamt 
    357  1.13.2.5  yamt 	(void) touser(pmp, park);
    358  1.13.2.2  yamt }
    359  1.13.2.2  yamt 
    360  1.13.2.5  yamt int
    361  1.13.2.5  yamt puffs_msg_raw(struct puffs_mount *pmp, struct puffs_msgpark *park)
    362  1.13.2.2  yamt {
    363  1.13.2.2  yamt 
    364  1.13.2.5  yamt 	park->park_copylen = park->park_maxlen;
    365  1.13.2.4  yamt 
    366  1.13.2.5  yamt 	return touser(pmp, park);
    367  1.13.2.4  yamt }
    368  1.13.2.4  yamt 
    369  1.13.2.4  yamt void
    370  1.13.2.5  yamt puffs_msg_errnotify(struct puffs_mount *pmp, uint8_t type, int error,
    371  1.13.2.5  yamt 	const char *str, void *cookie)
    372  1.13.2.4  yamt {
    373  1.13.2.5  yamt 	struct puffs_msgpark *park;
    374  1.13.2.5  yamt 	struct puffs_error *perr;
    375  1.13.2.5  yamt 
    376  1.13.2.5  yamt 	puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void **)&perr,1);
    377  1.13.2.5  yamt 
    378  1.13.2.5  yamt 	perr->perr_error = error;
    379  1.13.2.5  yamt 	strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
    380  1.13.2.2  yamt 
    381  1.13.2.5  yamt 	park->park_preq->preq_opclass |= PUFFSOP_ERROR | PUFFSOPFLAG_FAF;
    382  1.13.2.5  yamt 	park->park_preq->preq_optype = type;
    383  1.13.2.4  yamt 	park->park_preq->preq_cookie = cookie;
    384  1.13.2.2  yamt 
    385  1.13.2.5  yamt 	park->park_copylen = park->park_maxlen;
    386  1.13.2.2  yamt 
    387  1.13.2.5  yamt 	(void)touser(pmp, park);
    388  1.13.2.2  yamt }
    389  1.13.2.2  yamt 
    390  1.13.2.2  yamt /*
    391  1.13.2.6  yamt  * Wait for the userspace ping-pong game in calling process context,
    392  1.13.2.6  yamt  * unless a FAF / async call, in which case just enqueues the request
    393  1.13.2.6  yamt  * and return immediately.
    394  1.13.2.2  yamt  */
    395  1.13.2.2  yamt static int
    396  1.13.2.5  yamt touser(struct puffs_mount *pmp, struct puffs_msgpark *park)
    397  1.13.2.2  yamt {
    398  1.13.2.4  yamt 	struct lwp *l = curlwp;
    399  1.13.2.3  yamt 	struct mount *mp;
    400  1.13.2.2  yamt 	struct puffs_req *preq;
    401  1.13.2.4  yamt 	int rv = 0;
    402  1.13.2.2  yamt 
    403  1.13.2.3  yamt 	mp = PMPTOMP(pmp);
    404  1.13.2.4  yamt 	preq = park->park_preq;
    405  1.13.2.5  yamt 	preq->preq_buflen = park->park_maxlen;
    406  1.13.2.5  yamt 	KASSERT(preq->preq_id == 0);
    407  1.13.2.4  yamt 
    408  1.13.2.5  yamt 	if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
    409  1.13.2.5  yamt 		preq->preq_opclass |= PUFFSOPFLAG_FAF;
    410  1.13.2.5  yamt 	else
    411  1.13.2.5  yamt 		preq->preq_id = puffs_getmsgid(pmp);
    412  1.13.2.5  yamt 
    413  1.13.2.5  yamt 	/* fill in caller information */
    414  1.13.2.5  yamt 	preq->preq_pid = l->l_proc->p_pid;
    415  1.13.2.5  yamt 	preq->preq_lid = l->l_lid;
    416  1.13.2.4  yamt 
    417  1.13.2.4  yamt 	/*
    418  1.13.2.6  yamt 	 * To support cv_sig, yet another movie: check if there are signals
    419  1.13.2.4  yamt 	 * pending and we are issueing a non-FAF.  If so, return an error
    420  1.13.2.4  yamt 	 * directly UNLESS we are issueing INACTIVE.  In that case, convert
    421  1.13.2.4  yamt 	 * it to a FAF, fire off to the file server and return an error.
    422  1.13.2.4  yamt 	 * Yes, this is bordering disgusting.  Barfbags are on me.
    423  1.13.2.4  yamt 	 */
    424  1.13.2.4  yamt 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    425  1.13.2.4  yamt 	   && (park->park_flags & PARKFLAG_CALL) == 0
    426  1.13.2.4  yamt 	   && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    427  1.13.2.4  yamt 		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
    428  1.13.2.4  yamt 		    && preq->preq_optype == PUFFS_VN_INACTIVE) {
    429  1.13.2.5  yamt 			park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
    430  1.13.2.5  yamt 			park->park_flags &= ~PARKFLAG_WANTREPLY;
    431  1.13.2.4  yamt 			DPRINTF(("puffs touser: converted to FAF %p\n", park));
    432  1.13.2.4  yamt 			rv = EINTR;
    433  1.13.2.4  yamt 		} else {
    434  1.13.2.4  yamt 			return EINTR;
    435  1.13.2.4  yamt 		}
    436  1.13.2.4  yamt 	}
    437  1.13.2.3  yamt 
    438  1.13.2.3  yamt 	/*
    439  1.13.2.3  yamt 	 * test for suspension lock.
    440  1.13.2.3  yamt 	 *
    441  1.13.2.3  yamt 	 * Note that we *DO NOT* keep the lock, since that might block
    442  1.13.2.3  yamt 	 * lock acquiring PLUS it would give userlandia control over
    443  1.13.2.3  yamt 	 * the lock.  The operation queue enforces a strict ordering:
    444  1.13.2.3  yamt 	 * when the fs server gets in the op stream, it knows things
    445  1.13.2.3  yamt 	 * are in order.  The kernel locks can't guarantee that for
    446  1.13.2.3  yamt 	 * userspace, in any case.
    447  1.13.2.3  yamt 	 *
    448  1.13.2.3  yamt 	 * BUT: this presents a problem for ops which have a consistency
    449  1.13.2.3  yamt 	 * clause based on more than one operation.  Unfortunately such
    450  1.13.2.3  yamt 	 * operations (read, write) do not reliably work yet.
    451  1.13.2.3  yamt 	 *
    452  1.13.2.3  yamt 	 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
    453  1.13.2.3  yamt 	 *
    454  1.13.2.3  yamt 	 * XXX: and there is one more problem.  We sometimes need to
    455  1.13.2.3  yamt 	 * take a lazy lock in case the fs is suspending and we are
    456  1.13.2.3  yamt 	 * executing as the fs server context.  This might happen
    457  1.13.2.3  yamt 	 * e.g. in the case that the user server triggers a reclaim
    458  1.13.2.3  yamt 	 * in the kernel while the fs is suspending.  It's not a very
    459  1.13.2.3  yamt 	 * likely event, but it needs to be fixed some day.
    460  1.13.2.3  yamt 	 */
    461  1.13.2.4  yamt 
    462  1.13.2.4  yamt 	/*
    463  1.13.2.4  yamt 	 * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
    464  1.13.2.4  yamt 	 * the mutex here, since getpages() might be called locked.
    465  1.13.2.4  yamt 	 */
    466  1.13.2.3  yamt 	fstrans_start(mp, FSTRANS_NORMAL);
    467  1.13.2.4  yamt 	mutex_enter(&pmp->pmp_lock);
    468  1.13.2.3  yamt 	fstrans_done(mp);
    469  1.13.2.3  yamt 
    470  1.13.2.2  yamt 	if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    471  1.13.2.4  yamt 		mutex_exit(&pmp->pmp_lock);
    472  1.13.2.2  yamt 		return ENXIO;
    473  1.13.2.2  yamt 	}
    474  1.13.2.2  yamt 
    475  1.13.2.4  yamt #ifdef PUFFSDEBUG
    476  1.13.2.5  yamt 	parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
    477  1.13.2.5  yamt 	parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
    478  1.13.2.2  yamt #endif
    479  1.13.2.2  yamt 
    480  1.13.2.5  yamt 	mutex_enter(&park->park_mtx);
    481  1.13.2.5  yamt 	TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
    482  1.13.2.4  yamt 	park->park_flags |= PARKFLAG_ONQUEUE1;
    483  1.13.2.4  yamt 	puffs_mp_reference(pmp);
    484  1.13.2.5  yamt 	pmp->pmp_msg_touser_count++;
    485  1.13.2.4  yamt 	mutex_exit(&pmp->pmp_lock);
    486  1.13.2.4  yamt 
    487  1.13.2.4  yamt 	DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
    488  1.13.2.4  yamt 	    "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
    489  1.13.2.4  yamt 	    preq->preq_opclass, preq->preq_optype, park->park_flags));
    490  1.13.2.3  yamt 
    491  1.13.2.5  yamt 	cv_broadcast(&pmp->pmp_msg_waiter_cv);
    492  1.13.2.6  yamt 	putter_notify(pmp->pmp_pi);
    493  1.13.2.2  yamt 
    494  1.13.2.4  yamt 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    495  1.13.2.4  yamt 	    && (park->park_flags & PARKFLAG_CALL) == 0) {
    496  1.13.2.4  yamt 		int error;
    497  1.13.2.4  yamt 
    498  1.13.2.4  yamt 		error = cv_wait_sig(&park->park_cv, &park->park_mtx);
    499  1.13.2.5  yamt 		DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
    500  1.13.2.5  yamt 		    park, error));
    501  1.13.2.4  yamt 		if (error) {
    502  1.13.2.4  yamt 			park->park_flags |= PARKFLAG_WAITERGONE;
    503  1.13.2.4  yamt 			if (park->park_flags & PARKFLAG_DONE) {
    504  1.13.2.4  yamt 				rv = preq->preq_rv;
    505  1.13.2.4  yamt 			} else {
    506  1.13.2.4  yamt 				/*
    507  1.13.2.4  yamt 				 * ok, we marked it as going away, but
    508  1.13.2.4  yamt 				 * still need to do queue ops.  take locks
    509  1.13.2.4  yamt 				 * in correct order.
    510  1.13.2.4  yamt 				 *
    511  1.13.2.4  yamt 				 * We don't want to release our reference
    512  1.13.2.4  yamt 				 * if it's on replywait queue to avoid error
    513  1.13.2.4  yamt 				 * to file server.  putop() code will DTRT.
    514  1.13.2.4  yamt 				 */
    515  1.13.2.4  yamt 				mutex_exit(&park->park_mtx);
    516  1.13.2.4  yamt 				mutex_enter(&pmp->pmp_lock);
    517  1.13.2.4  yamt 				mutex_enter(&park->park_mtx);
    518  1.13.2.5  yamt 
    519  1.13.2.5  yamt 				/* remove from queue1 */
    520  1.13.2.4  yamt 				if (park->park_flags & PARKFLAG_ONQUEUE1) {
    521  1.13.2.5  yamt 					TAILQ_REMOVE(&pmp->pmp_msg_touser,
    522  1.13.2.4  yamt 					    park, park_entries);
    523  1.13.2.5  yamt 					pmp->pmp_msg_touser_count--;
    524  1.13.2.4  yamt 					park->park_flags &= ~PARKFLAG_ONQUEUE1;
    525  1.13.2.4  yamt 				}
    526  1.13.2.5  yamt 
    527  1.13.2.5  yamt 				/*
    528  1.13.2.5  yamt 				 * If it's waiting for a response already,
    529  1.13.2.5  yamt 				 * boost reference count.  Park will get
    530  1.13.2.5  yamt 				 * nuked once the response arrives from
    531  1.13.2.5  yamt 				 * the file server.
    532  1.13.2.5  yamt 				 */
    533  1.13.2.5  yamt 				if (park->park_flags & PARKFLAG_ONQUEUE2)
    534  1.13.2.5  yamt 					puffs_msgpark_reference(park);
    535  1.13.2.5  yamt 
    536  1.13.2.4  yamt 				mutex_exit(&pmp->pmp_lock);
    537  1.13.2.4  yamt 
    538  1.13.2.4  yamt 				rv = error;
    539  1.13.2.4  yamt 			}
    540  1.13.2.4  yamt 		} else {
    541  1.13.2.4  yamt 			rv = preq->preq_rv;
    542  1.13.2.4  yamt 		}
    543  1.13.2.2  yamt 
    544  1.13.2.3  yamt 		/*
    545  1.13.2.3  yamt 		 * retake the lock and release.  This makes sure (haha,
    546  1.13.2.3  yamt 		 * I'm humorous) that we don't process the same vnode in
    547  1.13.2.3  yamt 		 * multiple threads due to the locks hacks we have in
    548  1.13.2.3  yamt 		 * puffs_lock().  In reality this is well protected by
    549  1.13.2.3  yamt 		 * the biglock, but once that's gone, well, hopefully
    550  1.13.2.3  yamt 		 * this will be fixed for real.  (and when you read this
    551  1.13.2.3  yamt 		 * comment in 2017 and subsequently barf, my condolences ;).
    552  1.13.2.3  yamt 		 */
    553  1.13.2.4  yamt 		if (rv == 0 && !fstrans_is_owner(mp)) {
    554  1.13.2.3  yamt 			fstrans_start(mp, FSTRANS_NORMAL);
    555  1.13.2.3  yamt 			fstrans_done(mp);
    556  1.13.2.3  yamt 		}
    557  1.13.2.6  yamt 
    558  1.13.2.4  yamt 	} else {
    559  1.13.2.5  yamt 		/*
    560  1.13.2.5  yamt 		 * Take extra reference for FAF, i.e. don't free us
    561  1.13.2.5  yamt 		 * immediately upon return to the caller, but rather
    562  1.13.2.5  yamt 		 * only when the message has been transported.
    563  1.13.2.5  yamt 		 */
    564  1.13.2.5  yamt 		puffs_msgpark_reference(park);
    565  1.13.2.3  yamt 	}
    566  1.13.2.3  yamt 
    567  1.13.2.5  yamt 	mutex_exit(&park->park_mtx);
    568  1.13.2.5  yamt 
    569  1.13.2.4  yamt 	mutex_enter(&pmp->pmp_lock);
    570  1.13.2.4  yamt 	puffs_mp_release(pmp);
    571  1.13.2.4  yamt 	mutex_exit(&pmp->pmp_lock);
    572  1.13.2.3  yamt 
    573  1.13.2.4  yamt 	return rv;
    574  1.13.2.2  yamt }
    575  1.13.2.2  yamt 
    576  1.13.2.2  yamt /*
    577  1.13.2.5  yamt  * Get next request in the outgoing queue.  "maxsize" controls the
    578  1.13.2.5  yamt  * size the caller can accommodate and "nonblock" signals if this
    579  1.13.2.5  yamt  * should block while waiting for input.  Handles all locking internally.
    580  1.13.2.2  yamt  */
    581  1.13.2.2  yamt int
    582  1.13.2.5  yamt puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
    583  1.13.2.5  yamt 	uint8_t **data, size_t *dlen, void **parkptr)
    584  1.13.2.2  yamt {
    585  1.13.2.5  yamt 	struct puffs_mount *pmp = this;
    586  1.13.2.5  yamt 	struct puffs_msgpark *park;
    587  1.13.2.2  yamt 	struct puffs_req *preq;
    588  1.13.2.5  yamt 	int error;
    589  1.13.2.2  yamt 
    590  1.13.2.5  yamt 	error = 0;
    591  1.13.2.4  yamt 	mutex_enter(&pmp->pmp_lock);
    592  1.13.2.5  yamt 	puffs_mp_reference(pmp);
    593  1.13.2.5  yamt 	for (;;) {
    594  1.13.2.5  yamt 		/* RIP? */
    595  1.13.2.2  yamt 		if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    596  1.13.2.2  yamt 			error = ENXIO;
    597  1.13.2.5  yamt 			break;
    598  1.13.2.2  yamt 		}
    599  1.13.2.2  yamt 
    600  1.13.2.5  yamt 		/* need platinum yendorian express card? */
    601  1.13.2.5  yamt 		if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
    602  1.13.2.5  yamt 			DPRINTF(("puffs_getout: no outgoing op, "));
    603  1.13.2.2  yamt 			if (nonblock) {
    604  1.13.2.5  yamt 				DPRINTF(("returning EWOULDBLOCK\n"));
    605  1.13.2.2  yamt 				error = EWOULDBLOCK;
    606  1.13.2.5  yamt 				break;
    607  1.13.2.2  yamt 			}
    608  1.13.2.5  yamt 			DPRINTF(("waiting ...\n"));
    609  1.13.2.2  yamt 
    610  1.13.2.5  yamt 			error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
    611  1.13.2.4  yamt 			    &pmp->pmp_lock);
    612  1.13.2.2  yamt 			if (error)
    613  1.13.2.5  yamt 				break;
    614  1.13.2.2  yamt 			else
    615  1.13.2.5  yamt 				continue;
    616  1.13.2.2  yamt 		}
    617  1.13.2.2  yamt 
    618  1.13.2.5  yamt 		park = TAILQ_FIRST(&pmp->pmp_msg_touser);
    619  1.13.2.5  yamt 		if (park == NULL)
    620  1.13.2.5  yamt 			continue;
    621  1.13.2.5  yamt 
    622  1.13.2.5  yamt 		mutex_enter(&park->park_mtx);
    623  1.13.2.5  yamt 		puffs_msgpark_reference(park);
    624  1.13.2.5  yamt 
    625  1.13.2.5  yamt 		DPRINTF(("puffs_getout: found park at %p, ", park));
    626  1.13.2.2  yamt 
    627  1.13.2.4  yamt 		/* If it's a goner, don't process any furher */
    628  1.13.2.4  yamt 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    629  1.13.2.5  yamt 			DPRINTF(("waitergone!\n"));
    630  1.13.2.5  yamt 			puffs_msgpark_release(park);
    631  1.13.2.4  yamt 			continue;
    632  1.13.2.4  yamt 		}
    633  1.13.2.6  yamt 		preq = park->park_preq;
    634  1.13.2.4  yamt 
    635  1.13.2.6  yamt #if 0
    636  1.13.2.5  yamt 		/* check size */
    637  1.13.2.6  yamt 		/*
    638  1.13.2.6  yamt 		 * XXX: this check is not valid for now, we don't know
    639  1.13.2.6  yamt 		 * the size of the caller's input buffer.  i.e. this
    640  1.13.2.6  yamt 		 * will most likely go away
    641  1.13.2.6  yamt 		 */
    642  1.13.2.5  yamt 		if (maxsize < preq->preq_frhdr.pfr_len) {
    643  1.13.2.5  yamt 			DPRINTF(("buffer too small\n"));
    644  1.13.2.5  yamt 			puffs_msgpark_release(park);
    645  1.13.2.5  yamt 			error = E2BIG;
    646  1.13.2.5  yamt 			break;
    647  1.13.2.2  yamt 		}
    648  1.13.2.6  yamt #endif
    649  1.13.2.4  yamt 
    650  1.13.2.5  yamt 		DPRINTF(("returning\n"));
    651  1.13.2.5  yamt 
    652  1.13.2.5  yamt 		/*
    653  1.13.2.5  yamt 		 * Ok, we found what we came for.  Release it from the
    654  1.13.2.5  yamt 		 * outgoing queue but do not unlock.  We will unlock
    655  1.13.2.5  yamt 		 * only after we "releaseout" it to avoid complications:
    656  1.13.2.5  yamt 		 * otherwise it is (theoretically) possible for userland
    657  1.13.2.5  yamt 		 * to race us into "put" before we have a change to put
    658  1.13.2.5  yamt 		 * this baby on the receiving queue.
    659  1.13.2.5  yamt 		 */
    660  1.13.2.5  yamt 		TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
    661  1.13.2.4  yamt 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    662  1.13.2.4  yamt 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    663  1.13.2.5  yamt 		mutex_exit(&park->park_mtx);
    664  1.13.2.2  yamt 
    665  1.13.2.5  yamt 		pmp->pmp_msg_touser_count--;
    666  1.13.2.5  yamt 		KASSERT(pmp->pmp_msg_touser_count >= 0);
    667  1.13.2.4  yamt 
    668  1.13.2.5  yamt 		break;
    669  1.13.2.5  yamt 	}
    670  1.13.2.5  yamt 	puffs_mp_release(pmp);
    671  1.13.2.5  yamt 	mutex_exit(&pmp->pmp_lock);
    672  1.13.2.2  yamt 
    673  1.13.2.5  yamt 	if (error == 0) {
    674  1.13.2.5  yamt 		*data = (uint8_t *)preq;
    675  1.13.2.6  yamt 		preq->preq_pth.pth_framelen = park->park_copylen;
    676  1.13.2.6  yamt 		*dlen = preq->preq_pth.pth_framelen;
    677  1.13.2.5  yamt 		*parkptr = park;
    678  1.13.2.5  yamt 	}
    679  1.13.2.6  yamt 
    680  1.13.2.5  yamt 	return error;
    681  1.13.2.5  yamt }
    682  1.13.2.5  yamt 
    683  1.13.2.5  yamt /*
    684  1.13.2.5  yamt  * Release outgoing structure.  Now, depending on the success of the
    685  1.13.2.5  yamt  * outgoing send, it is either going onto the result waiting queue
    686  1.13.2.5  yamt  * or the death chamber.
    687  1.13.2.5  yamt  */
    688  1.13.2.5  yamt void
    689  1.13.2.5  yamt puffs_msgif_releaseout(void *this, void *parkptr, int status)
    690  1.13.2.5  yamt {
    691  1.13.2.5  yamt 	struct puffs_mount *pmp = this;
    692  1.13.2.5  yamt 	struct puffs_msgpark *park = parkptr;
    693  1.13.2.4  yamt 
    694  1.13.2.5  yamt 	DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
    695  1.13.2.5  yamt 	    park, status));
    696  1.13.2.5  yamt 	mutex_enter(&pmp->pmp_lock);
    697  1.13.2.5  yamt 	mutex_enter(&park->park_mtx);
    698  1.13.2.5  yamt 	if (park->park_flags & PARKFLAG_WANTREPLY) {
    699  1.13.2.5  yamt 		if (status == 0) {
    700  1.13.2.5  yamt 			DPRINTF(("enqueue replywait\n"));
    701  1.13.2.5  yamt 			TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
    702  1.13.2.2  yamt 			    park_entries);
    703  1.13.2.4  yamt 			park->park_flags |= PARKFLAG_ONQUEUE2;
    704  1.13.2.2  yamt 		} else {
    705  1.13.2.5  yamt 			DPRINTF(("error path!\n"));
    706  1.13.2.5  yamt 			park->park_preq->preq_rv = status;
    707  1.13.2.5  yamt 			park->park_flags |= PARKFLAG_DONE;
    708  1.13.2.5  yamt 			cv_signal(&park->park_cv);
    709  1.13.2.2  yamt 		}
    710  1.13.2.5  yamt 		puffs_msgpark_release(park);
    711  1.13.2.5  yamt 	} else {
    712  1.13.2.5  yamt 		DPRINTF(("release\n"));
    713  1.13.2.5  yamt 		puffs_msgpark_release1(park, 2);
    714  1.13.2.2  yamt 	}
    715  1.13.2.4  yamt 	mutex_exit(&pmp->pmp_lock);
    716  1.13.2.2  yamt }
    717  1.13.2.2  yamt 
    718  1.13.2.6  yamt size_t
    719  1.13.2.6  yamt puffs_msgif_waitcount(void *this)
    720  1.13.2.6  yamt {
    721  1.13.2.6  yamt 	struct puffs_mount *pmp = this;
    722  1.13.2.6  yamt 	size_t rv;
    723  1.13.2.6  yamt 
    724  1.13.2.6  yamt 	mutex_enter(&pmp->pmp_lock);
    725  1.13.2.6  yamt 	rv = pmp->pmp_msg_touser_count;
    726  1.13.2.6  yamt 	mutex_exit(&pmp->pmp_lock);
    727  1.13.2.6  yamt 
    728  1.13.2.6  yamt 	return rv;
    729  1.13.2.6  yamt }
    730  1.13.2.6  yamt 
    731  1.13.2.5  yamt /*
    732  1.13.2.5  yamt  * XXX: locking with this one?
    733  1.13.2.5  yamt  */
    734  1.13.2.6  yamt static void
    735  1.13.2.6  yamt puffsop_msg(void *this, struct puffs_req *preq)
    736  1.13.2.2  yamt {
    737  1.13.2.5  yamt 	struct puffs_mount *pmp = this;
    738  1.13.2.6  yamt 	struct putter_hdr *pth = &preq->preq_pth;
    739  1.13.2.5  yamt 	struct puffs_msgpark *park;
    740  1.13.2.5  yamt 	int release, wgone;
    741  1.13.2.5  yamt 
    742  1.13.2.5  yamt 	/* XXX */
    743  1.13.2.5  yamt 	if (PUFFSOP_OPCLASS(preq->preq_opclass) != PUFFSOP_VN
    744  1.13.2.5  yamt 	    && PUFFSOP_OPCLASS(preq->preq_opclass) != PUFFSOP_VFS)
    745  1.13.2.5  yamt 		return;
    746  1.13.2.2  yamt 
    747  1.13.2.4  yamt 	mutex_enter(&pmp->pmp_lock);
    748  1.13.2.2  yamt 
    749  1.13.2.5  yamt 	/* Locate waiter */
    750  1.13.2.5  yamt 	TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
    751  1.13.2.5  yamt 		if (park->park_preq->preq_id == preq->preq_id)
    752  1.13.2.2  yamt 			break;
    753  1.13.2.5  yamt 	}
    754  1.13.2.5  yamt 	if (park == NULL) {
    755  1.13.2.5  yamt 		DPRINTF(("puffs_msgif_income: no request: %" PRIu64 "\n",
    756  1.13.2.5  yamt 		    preq->preq_id));
    757  1.13.2.5  yamt 		mutex_exit(&pmp->pmp_lock);
    758  1.13.2.5  yamt 		return; /* XXX send error */
    759  1.13.2.5  yamt 	}
    760  1.13.2.2  yamt 
    761  1.13.2.5  yamt 	mutex_enter(&park->park_mtx);
    762  1.13.2.5  yamt 	puffs_msgpark_reference(park);
    763  1.13.2.6  yamt 	if (pth->pth_framelen > park->park_maxlen) {
    764  1.13.2.5  yamt 		DPRINTF(("puffs_msgif_income: invalid buffer length: "
    765  1.13.2.6  yamt 		    "%" PRIu64 " (req %" PRIu64 ", \n", pth->pth_framelen,
    766  1.13.2.6  yamt 		    preq->preq_id));
    767  1.13.2.5  yamt 		park->park_preq->preq_rv = EPROTO;
    768  1.13.2.5  yamt 		cv_signal(&park->park_cv);
    769  1.13.2.5  yamt 		puffs_msgpark_release(park);
    770  1.13.2.4  yamt 		mutex_exit(&pmp->pmp_lock);
    771  1.13.2.5  yamt 		return; /* XXX: error */
    772  1.13.2.5  yamt 	}
    773  1.13.2.5  yamt 	wgone = park->park_flags & PARKFLAG_WAITERGONE;
    774  1.13.2.4  yamt 
    775  1.13.2.5  yamt 	KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
    776  1.13.2.5  yamt 	TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
    777  1.13.2.5  yamt 	park->park_flags &= ~PARKFLAG_ONQUEUE2;
    778  1.13.2.5  yamt 	mutex_exit(&pmp->pmp_lock);
    779  1.13.2.2  yamt 
    780  1.13.2.5  yamt 	if (wgone) {
    781  1.13.2.5  yamt 		DPRINTF(("puffs_putop: bad service - waiter gone for "
    782  1.13.2.5  yamt 		    "park %p\n", park));
    783  1.13.2.5  yamt 		release = 2;
    784  1.13.2.5  yamt 	} else {
    785  1.13.2.4  yamt 		if (park->park_flags & PARKFLAG_CALL) {
    786  1.13.2.5  yamt 			DPRINTF(("puffs_msgif_income: call for %p, arg %p\n",
    787  1.13.2.4  yamt 			    park->park_preq, park->park_donearg));
    788  1.13.2.6  yamt 			park->park_done(pmp, preq, park->park_donearg);
    789  1.13.2.5  yamt 			release = 2;
    790  1.13.2.5  yamt 		} else {
    791  1.13.2.5  yamt 			/* XXX: yes, I know */
    792  1.13.2.6  yamt 			memcpy(park->park_preq, preq, pth->pth_framelen);
    793  1.13.2.4  yamt 			release = 1;
    794  1.13.2.4  yamt 		}
    795  1.13.2.2  yamt 	}
    796  1.13.2.2  yamt 
    797  1.13.2.5  yamt 	if (!wgone) {
    798  1.13.2.5  yamt 		DPRINTF(("puffs_putop: flagging done for "
    799  1.13.2.5  yamt 		    "park %p\n", park));
    800  1.13.2.5  yamt 		cv_signal(&park->park_cv);
    801  1.13.2.5  yamt 	}
    802  1.13.2.2  yamt 
    803  1.13.2.5  yamt 	park->park_flags |= PARKFLAG_DONE;
    804  1.13.2.5  yamt 	puffs_msgpark_release1(park, release);
    805  1.13.2.2  yamt }
    806  1.13.2.2  yamt 
    807  1.13.2.4  yamt /*
    808  1.13.2.6  yamt  * helpers
    809  1.13.2.6  yamt  */
    810  1.13.2.6  yamt static void
    811  1.13.2.6  yamt dosuspendresume(void *arg)
    812  1.13.2.6  yamt {
    813  1.13.2.6  yamt 	struct puffs_mount *pmp = arg;
    814  1.13.2.6  yamt 	struct mount *mp;
    815  1.13.2.6  yamt 	int rv;
    816  1.13.2.6  yamt 
    817  1.13.2.6  yamt 	mp = PMPTOMP(pmp);
    818  1.13.2.6  yamt 	/*
    819  1.13.2.6  yamt 	 * XXX?  does this really do any good or is it just
    820  1.13.2.6  yamt 	 * paranoid stupidity?  or stupid paranoia?
    821  1.13.2.6  yamt 	 */
    822  1.13.2.6  yamt 	if (mp->mnt_iflag & IMNT_UNMOUNT) {
    823  1.13.2.6  yamt 		printf("puffs dosuspendresume(): detected suspend on "
    824  1.13.2.6  yamt 		    "unmounting fs\n");
    825  1.13.2.6  yamt 		goto out;
    826  1.13.2.6  yamt 	}
    827  1.13.2.6  yamt 
    828  1.13.2.6  yamt 	/* Do the dance.  Allow only one concurrent suspend */
    829  1.13.2.6  yamt 	rv = vfs_suspend(PMPTOMP(pmp), 1);
    830  1.13.2.6  yamt 	if (rv == 0)
    831  1.13.2.6  yamt 		vfs_resume(PMPTOMP(pmp));
    832  1.13.2.6  yamt 
    833  1.13.2.6  yamt  out:
    834  1.13.2.6  yamt 	mutex_enter(&pmp->pmp_lock);
    835  1.13.2.6  yamt 	KASSERT(pmp->pmp_suspend == 1);
    836  1.13.2.6  yamt 	pmp->pmp_suspend = 0;
    837  1.13.2.6  yamt 	puffs_mp_release(pmp);
    838  1.13.2.6  yamt 	mutex_exit(&pmp->pmp_lock);
    839  1.13.2.6  yamt 
    840  1.13.2.6  yamt 	kthread_exit(0);
    841  1.13.2.6  yamt }
    842  1.13.2.6  yamt 
    843  1.13.2.6  yamt static void
    844  1.13.2.6  yamt puffsop_suspend(struct puffs_mount *pmp)
    845  1.13.2.6  yamt {
    846  1.13.2.6  yamt 	int rv = 0;
    847  1.13.2.6  yamt 
    848  1.13.2.6  yamt 	mutex_enter(&pmp->pmp_lock);
    849  1.13.2.6  yamt 	if (pmp->pmp_suspend || pmp->pmp_status != PUFFSTAT_RUNNING) {
    850  1.13.2.6  yamt 		rv = EBUSY;
    851  1.13.2.6  yamt 	} else {
    852  1.13.2.6  yamt 		puffs_mp_reference(pmp);
    853  1.13.2.6  yamt 		pmp->pmp_suspend = 1;
    854  1.13.2.6  yamt 	}
    855  1.13.2.6  yamt 	mutex_exit(&pmp->pmp_lock);
    856  1.13.2.6  yamt 	if (rv)
    857  1.13.2.6  yamt 		return;
    858  1.13.2.6  yamt 	rv = kthread_create(PRI_NONE, 0, NULL, dosuspendresume,
    859  1.13.2.6  yamt 	    pmp, NULL, "puffsusp");
    860  1.13.2.6  yamt 
    861  1.13.2.6  yamt 	/* XXX: "return" rv */
    862  1.13.2.6  yamt }
    863  1.13.2.6  yamt 
    864  1.13.2.6  yamt static int
    865  1.13.2.6  yamt puffsop_flush(struct puffs_mount *pmp, struct puffs_flush *pf)
    866  1.13.2.6  yamt {
    867  1.13.2.6  yamt 	struct vnode *vp;
    868  1.13.2.6  yamt 	voff_t offlo, offhi;
    869  1.13.2.6  yamt 	int rv, flags = 0;
    870  1.13.2.6  yamt 
    871  1.13.2.6  yamt 	/* XXX: slurry */
    872  1.13.2.6  yamt 	if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
    873  1.13.2.6  yamt 		cache_purgevfs(PMPTOMP(pmp));
    874  1.13.2.6  yamt 		return 0;
    875  1.13.2.6  yamt 	}
    876  1.13.2.6  yamt 
    877  1.13.2.6  yamt 	/*
    878  1.13.2.6  yamt 	 * Get vnode, don't lock it.  Namecache is protected by its own lock
    879  1.13.2.6  yamt 	 * and we have a reference to protect against premature harvesting.
    880  1.13.2.6  yamt 	 *
    881  1.13.2.6  yamt 	 * The node we want here might be locked and the op is in
    882  1.13.2.6  yamt 	 * userspace waiting for us to complete ==> deadlock.  Another
    883  1.13.2.6  yamt 	 * reason we need to eventually bump locking to userspace, as we
    884  1.13.2.6  yamt 	 * will need to lock the node if we wish to do flushes.
    885  1.13.2.6  yamt 	 */
    886  1.13.2.6  yamt 	rv = puffs_cookie2vnode(pmp, pf->pf_cookie, 0, 0, &vp);
    887  1.13.2.6  yamt 	if (rv) {
    888  1.13.2.6  yamt 		if (rv == PUFFS_NOSUCHCOOKIE)
    889  1.13.2.6  yamt 			return ENOENT;
    890  1.13.2.6  yamt 		return rv;
    891  1.13.2.6  yamt 	}
    892  1.13.2.6  yamt 
    893  1.13.2.6  yamt 	switch (pf->pf_op) {
    894  1.13.2.6  yamt #if 0
    895  1.13.2.6  yamt 	/* not quite ready, yet */
    896  1.13.2.6  yamt 	case PUFFS_INVAL_NAMECACHE_NODE:
    897  1.13.2.6  yamt 	struct componentname *pf_cn;
    898  1.13.2.6  yamt 	char *name;
    899  1.13.2.6  yamt 		/* get comfortab^Wcomponentname */
    900  1.13.2.6  yamt 		MALLOC(pf_cn, struct componentname *,
    901  1.13.2.6  yamt 		    sizeof(struct componentname), M_PUFFS, M_WAITOK | M_ZERO);
    902  1.13.2.6  yamt 		memset(pf_cn, 0, sizeof(struct componentname));
    903  1.13.2.6  yamt 		break;
    904  1.13.2.6  yamt 
    905  1.13.2.6  yamt #endif
    906  1.13.2.6  yamt 	case PUFFS_INVAL_NAMECACHE_DIR:
    907  1.13.2.6  yamt 		if (vp->v_type != VDIR) {
    908  1.13.2.6  yamt 			rv = EINVAL;
    909  1.13.2.6  yamt 			break;
    910  1.13.2.6  yamt 		}
    911  1.13.2.6  yamt 		cache_purge1(vp, NULL, PURGE_CHILDREN);
    912  1.13.2.6  yamt 		break;
    913  1.13.2.6  yamt 
    914  1.13.2.6  yamt 	case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
    915  1.13.2.6  yamt 		flags = PGO_FREE;
    916  1.13.2.6  yamt 		/*FALLTHROUGH*/
    917  1.13.2.6  yamt 	case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
    918  1.13.2.6  yamt 		if (flags == 0)
    919  1.13.2.6  yamt 			flags = PGO_CLEANIT;
    920  1.13.2.6  yamt 
    921  1.13.2.6  yamt 		if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
    922  1.13.2.6  yamt 			rv = EINVAL;
    923  1.13.2.6  yamt 			break;
    924  1.13.2.6  yamt 		}
    925  1.13.2.6  yamt 
    926  1.13.2.6  yamt 		offlo = trunc_page(pf->pf_start);
    927  1.13.2.6  yamt 		offhi = round_page(pf->pf_end);
    928  1.13.2.6  yamt 		if (offhi != 0 && offlo >= offhi) {
    929  1.13.2.6  yamt 			rv = EINVAL;
    930  1.13.2.6  yamt 			break;
    931  1.13.2.6  yamt 		}
    932  1.13.2.6  yamt 
    933  1.13.2.6  yamt 		simple_lock(&vp->v_uobj.vmobjlock);
    934  1.13.2.6  yamt 		rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
    935  1.13.2.6  yamt 		break;
    936  1.13.2.6  yamt 
    937  1.13.2.6  yamt 	default:
    938  1.13.2.6  yamt 		rv = EINVAL;
    939  1.13.2.6  yamt 	}
    940  1.13.2.6  yamt 
    941  1.13.2.6  yamt 	vrele(vp);
    942  1.13.2.6  yamt 
    943  1.13.2.6  yamt 	return rv;
    944  1.13.2.6  yamt }
    945  1.13.2.6  yamt 
    946  1.13.2.6  yamt int
    947  1.13.2.6  yamt puffs_msgif_dispatch(void *this, struct putter_hdr *pth)
    948  1.13.2.6  yamt {
    949  1.13.2.6  yamt 	struct puffs_mount *pmp = this;
    950  1.13.2.6  yamt 	struct puffs_req *preq = (struct puffs_req *)pth;
    951  1.13.2.6  yamt 
    952  1.13.2.6  yamt 	/* XXX: need to send error to userspace */
    953  1.13.2.6  yamt 	if (pth->pth_framelen < sizeof(struct puffs_req))
    954  1.13.2.6  yamt 		return EINVAL; /* E2SMALL */
    955  1.13.2.6  yamt 
    956  1.13.2.6  yamt 	switch (PUFFSOP_OPCLASS(preq->preq_opclass)) {
    957  1.13.2.6  yamt 	case PUFFSOP_VN:
    958  1.13.2.6  yamt 	case PUFFSOP_VFS:
    959  1.13.2.6  yamt 		puffsop_msg(pmp, preq);
    960  1.13.2.6  yamt 		break;
    961  1.13.2.6  yamt 	case PUFFSOP_FLUSH:
    962  1.13.2.6  yamt 		if (pth->pth_framelen != sizeof(struct puffs_flush))
    963  1.13.2.6  yamt 			return EINVAL;
    964  1.13.2.6  yamt 		puffsop_flush(pmp, (struct puffs_flush *)preq);
    965  1.13.2.6  yamt 		break;
    966  1.13.2.6  yamt 	case PUFFSOP_SUSPEND:
    967  1.13.2.6  yamt 		puffsop_suspend(pmp);
    968  1.13.2.6  yamt 		break;
    969  1.13.2.6  yamt 	default:
    970  1.13.2.6  yamt 		/* XXX: send error */
    971  1.13.2.6  yamt 		break;
    972  1.13.2.6  yamt 	}
    973  1.13.2.6  yamt 
    974  1.13.2.6  yamt 	return 0;
    975  1.13.2.6  yamt }
    976  1.13.2.6  yamt 
    977  1.13.2.6  yamt int
    978  1.13.2.6  yamt puffs_msgif_close(void *this)
    979  1.13.2.6  yamt {
    980  1.13.2.6  yamt 	struct puffs_mount *pmp = this;
    981  1.13.2.6  yamt 	struct mount *mp = PMPTOMP(pmp);
    982  1.13.2.6  yamt 	int gone, rv;
    983  1.13.2.6  yamt 
    984  1.13.2.6  yamt 	mutex_enter(&pmp->pmp_lock);
    985  1.13.2.6  yamt 	puffs_mp_reference(pmp);
    986  1.13.2.6  yamt 
    987  1.13.2.6  yamt 	/*
    988  1.13.2.6  yamt 	 * Free the waiting callers before proceeding any further.
    989  1.13.2.6  yamt 	 * The syncer might be jogging around in this file system
    990  1.13.2.6  yamt 	 * currently.  If we allow it to go to the userspace of no
    991  1.13.2.6  yamt 	 * return while trying to get the syncer lock, well ...
    992  1.13.2.6  yamt 	 * synclk: I feel happy, I feel fine.
    993  1.13.2.6  yamt 	 * lockmgr: You're not fooling anyone, you know.
    994  1.13.2.6  yamt 	 */
    995  1.13.2.6  yamt 	puffs_userdead(pmp);
    996  1.13.2.6  yamt 
    997  1.13.2.6  yamt 	/*
    998  1.13.2.6  yamt 	 * Make sure someone from puffs_unmount() isn't currently in
    999  1.13.2.6  yamt 	 * userspace.  If we don't take this precautionary step,
   1000  1.13.2.6  yamt 	 * they might notice that the mountpoint has disappeared
   1001  1.13.2.6  yamt 	 * from under them once they return.  Especially note that we
   1002  1.13.2.6  yamt 	 * cannot simply test for an unmounter before calling
   1003  1.13.2.6  yamt 	 * dounmount(), since it might be possible that that particular
   1004  1.13.2.6  yamt 	 * invocation of unmount was called without MNT_FORCE.  Here we
   1005  1.13.2.6  yamt 	 * *must* make sure unmount succeeds.  Also, restart is necessary
   1006  1.13.2.6  yamt 	 * since pmp isn't locked.  We might end up with PUTTER_DEAD after
   1007  1.13.2.6  yamt 	 * restart and exit from there.
   1008  1.13.2.6  yamt 	 */
   1009  1.13.2.6  yamt 	if (pmp->pmp_unmounting) {
   1010  1.13.2.6  yamt 		cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
   1011  1.13.2.6  yamt 		puffs_mp_release(pmp);
   1012  1.13.2.6  yamt 		mutex_exit(&pmp->pmp_lock);
   1013  1.13.2.6  yamt 		DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
   1014  1.13.2.6  yamt 		    "restart\n", pmp));
   1015  1.13.2.6  yamt 		return ERESTART;
   1016  1.13.2.6  yamt 	}
   1017  1.13.2.6  yamt 
   1018  1.13.2.6  yamt 	/* Won't access pmp from here anymore */
   1019  1.13.2.6  yamt 	puffs_mp_release(pmp);
   1020  1.13.2.6  yamt 	mutex_exit(&pmp->pmp_lock);
   1021  1.13.2.6  yamt 
   1022  1.13.2.6  yamt 	/*
   1023  1.13.2.6  yamt 	 * Detach from VFS.  First do necessary XXX-dance (from
   1024  1.13.2.6  yamt 	 * sys_unmount() & other callers of dounmount()
   1025  1.13.2.6  yamt 	 *
   1026  1.13.2.6  yamt 	 * XXX Freeze syncer.  Must do this before locking the
   1027  1.13.2.6  yamt 	 * mount point.  See dounmount() for details.
   1028  1.13.2.6  yamt 	 *
   1029  1.13.2.6  yamt 	 * XXX2: take a reference to the mountpoint before starting to
   1030  1.13.2.6  yamt 	 * wait for syncer_mutex.  Otherwise the mointpoint can be
   1031  1.13.2.6  yamt 	 * wiped out while we wait.
   1032  1.13.2.6  yamt 	 */
   1033  1.13.2.6  yamt 	simple_lock(&mp->mnt_slock);
   1034  1.13.2.6  yamt 	mp->mnt_wcnt++;
   1035  1.13.2.6  yamt 	simple_unlock(&mp->mnt_slock);
   1036  1.13.2.6  yamt 
   1037  1.13.2.6  yamt 	mutex_enter(&syncer_mutex);
   1038  1.13.2.6  yamt 
   1039  1.13.2.6  yamt 	simple_lock(&mp->mnt_slock);
   1040  1.13.2.6  yamt 	mp->mnt_wcnt--;
   1041  1.13.2.6  yamt 	if (mp->mnt_wcnt == 0)
   1042  1.13.2.6  yamt 		wakeup(&mp->mnt_wcnt);
   1043  1.13.2.6  yamt 	gone = mp->mnt_iflag & IMNT_GONE;
   1044  1.13.2.6  yamt 	simple_unlock(&mp->mnt_slock);
   1045  1.13.2.6  yamt 	if (gone) {
   1046  1.13.2.6  yamt 		mutex_exit(&syncer_mutex);
   1047  1.13.2.6  yamt 		return 0;
   1048  1.13.2.6  yamt 	}
   1049  1.13.2.6  yamt 
   1050  1.13.2.6  yamt 	/*
   1051  1.13.2.6  yamt 	 * microscopic race condition here (although not with the current
   1052  1.13.2.6  yamt 	 * kernel), but can't really fix it without starting a crusade
   1053  1.13.2.6  yamt 	 * against vfs_busy(), so let it be, let it be, let it be
   1054  1.13.2.6  yamt 	 */
   1055  1.13.2.6  yamt 
   1056  1.13.2.6  yamt 	/*
   1057  1.13.2.6  yamt 	 * The only way vfs_busy() will fail for us is if the filesystem
   1058  1.13.2.6  yamt 	 * is already a goner.
   1059  1.13.2.6  yamt 	 * XXX: skating on the thin ice of modern calling conventions ...
   1060  1.13.2.6  yamt 	 */
   1061  1.13.2.6  yamt 	if (vfs_busy(mp, 0, 0)) {
   1062  1.13.2.6  yamt 		mutex_exit(&syncer_mutex);
   1063  1.13.2.6  yamt 		return 0;
   1064  1.13.2.6  yamt 	}
   1065  1.13.2.6  yamt 
   1066  1.13.2.6  yamt 	/*
   1067  1.13.2.6  yamt 	 * Once we have the mount point, unmount() can't interfere..
   1068  1.13.2.6  yamt 	 * or at least in theory it shouldn't.  dounmount() reentracy
   1069  1.13.2.6  yamt 	 * might require some visiting at some point.
   1070  1.13.2.6  yamt 	 */
   1071  1.13.2.6  yamt 	rv = dounmount(mp, MNT_FORCE, curlwp);
   1072  1.13.2.6  yamt 	KASSERT(rv == 0);
   1073  1.13.2.6  yamt 
   1074  1.13.2.6  yamt 	return 0;
   1075  1.13.2.6  yamt }
   1076  1.13.2.6  yamt 
   1077  1.13.2.6  yamt /*
   1078  1.13.2.4  yamt  * We're dead, kaput, RIP, slightly more than merely pining for the
   1079  1.13.2.4  yamt  * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
   1080  1.13.2.4  yamt  * our maker, ceased to be, etcetc.  YASD.  It's a dead FS!
   1081  1.13.2.4  yamt  *
   1082  1.13.2.4  yamt  * Caller must hold puffs mutex.
   1083  1.13.2.4  yamt  */
   1084  1.13.2.4  yamt void
   1085  1.13.2.4  yamt puffs_userdead(struct puffs_mount *pmp)
   1086  1.13.2.4  yamt {
   1087  1.13.2.5  yamt 	struct puffs_msgpark *park, *park_next;
   1088  1.13.2.4  yamt 
   1089  1.13.2.4  yamt 	/*
   1090  1.13.2.4  yamt 	 * Mark filesystem status as dying so that operations don't
   1091  1.13.2.4  yamt 	 * attempt to march to userspace any longer.
   1092  1.13.2.4  yamt 	 */
   1093  1.13.2.4  yamt 	pmp->pmp_status = PUFFSTAT_DYING;
   1094  1.13.2.4  yamt 
   1095  1.13.2.4  yamt 	/* signal waiters on REQUEST TO file server queue */
   1096  1.13.2.5  yamt 	for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
   1097  1.13.2.4  yamt 		uint8_t opclass;
   1098  1.13.2.4  yamt 
   1099  1.13.2.5  yamt 		mutex_enter(&park->park_mtx);
   1100  1.13.2.5  yamt 		puffs_msgpark_reference(park);
   1101  1.13.2.4  yamt 		park_next = TAILQ_NEXT(park, park_entries);
   1102  1.13.2.4  yamt 
   1103  1.13.2.4  yamt 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
   1104  1.13.2.5  yamt 		TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
   1105  1.13.2.4  yamt 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
   1106  1.13.2.5  yamt 		pmp->pmp_msg_touser_count--;
   1107  1.13.2.4  yamt 
   1108  1.13.2.4  yamt 		/*
   1109  1.13.2.6  yamt 		 * Even though waiters on QUEUE1 are removed in touser()
   1110  1.13.2.6  yamt 		 * in case of WAITERGONE, it is still possible for us to
   1111  1.13.2.6  yamt 		 * get raced here due to having to retake locks in said
   1112  1.13.2.6  yamt 		 * touser().  In the race case simply "ignore" the item
   1113  1.13.2.6  yamt 		 * on the queue and move on to the next one.
   1114  1.13.2.4  yamt 		 */
   1115  1.13.2.4  yamt 		if (park->park_flags & PARKFLAG_WAITERGONE) {
   1116  1.13.2.4  yamt 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
   1117  1.13.2.4  yamt 			KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
   1118  1.13.2.5  yamt 			puffs_msgpark_release(park);
   1119  1.13.2.6  yamt 
   1120  1.13.2.4  yamt 		} else {
   1121  1.13.2.4  yamt 			opclass = park->park_preq->preq_opclass;
   1122  1.13.2.4  yamt 			park->park_preq->preq_rv = ENXIO;
   1123  1.13.2.4  yamt 
   1124  1.13.2.4  yamt 			if (park->park_flags & PARKFLAG_CALL) {
   1125  1.13.2.5  yamt 				park->park_done(pmp, park->park_preq,
   1126  1.13.2.4  yamt 				    park->park_donearg);
   1127  1.13.2.5  yamt 				puffs_msgpark_release1(park, 2);
   1128  1.13.2.4  yamt 			} else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
   1129  1.13.2.5  yamt 				puffs_msgpark_release1(park, 2);
   1130  1.13.2.4  yamt 			} else {
   1131  1.13.2.4  yamt 				park->park_preq->preq_rv = ENXIO;
   1132  1.13.2.4  yamt 				cv_signal(&park->park_cv);
   1133  1.13.2.5  yamt 				puffs_msgpark_release(park);
   1134  1.13.2.4  yamt 			}
   1135  1.13.2.4  yamt 		}
   1136  1.13.2.4  yamt 	}
   1137  1.13.2.4  yamt 
   1138  1.13.2.4  yamt 	/* signal waiters on RESPONSE FROM file server queue */
   1139  1.13.2.5  yamt 	for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
   1140  1.13.2.5  yamt 		mutex_enter(&park->park_mtx);
   1141  1.13.2.5  yamt 		puffs_msgpark_reference(park);
   1142  1.13.2.4  yamt 		park_next = TAILQ_NEXT(park, park_entries);
   1143  1.13.2.4  yamt 
   1144  1.13.2.4  yamt 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
   1145  1.13.2.4  yamt 		KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
   1146  1.13.2.4  yamt 
   1147  1.13.2.5  yamt 		TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
   1148  1.13.2.4  yamt 		park->park_flags &= ~PARKFLAG_ONQUEUE2;
   1149  1.13.2.4  yamt 
   1150  1.13.2.4  yamt 		if (park->park_flags & PARKFLAG_WAITERGONE) {
   1151  1.13.2.4  yamt 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
   1152  1.13.2.5  yamt 			puffs_msgpark_release(park);
   1153  1.13.2.4  yamt 		} else {
   1154  1.13.2.4  yamt 			park->park_preq->preq_rv = ENXIO;
   1155  1.13.2.4  yamt 			if (park->park_flags & PARKFLAG_CALL) {
   1156  1.13.2.5  yamt 				park->park_done(pmp, park->park_preq,
   1157  1.13.2.4  yamt 				    park->park_donearg);
   1158  1.13.2.5  yamt 				puffs_msgpark_release1(park, 2);
   1159  1.13.2.4  yamt 			} else {
   1160  1.13.2.4  yamt 				cv_signal(&park->park_cv);
   1161  1.13.2.5  yamt 				puffs_msgpark_release(park);
   1162  1.13.2.4  yamt 			}
   1163  1.13.2.4  yamt 		}
   1164  1.13.2.4  yamt 	}
   1165  1.13.2.4  yamt 
   1166  1.13.2.5  yamt 	cv_broadcast(&pmp->pmp_msg_waiter_cv);
   1167  1.13.2.2  yamt }
   1168