Home | History | Annotate | Line # | Download | only in puffs
puffs_msgif.c revision 1.93
      1  1.93  dholland /*	$NetBSD: puffs_msgif.c,v 1.93 2012/11/05 17:27:38 dholland 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.93  dholland __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.93 2012/11/05 17:27:38 dholland Exp $");
     34   1.1     pooka 
     35   1.1     pooka #include <sys/param.h>
     36  1.90      manu #include <sys/kernel.h>
     37  1.68       tnn #include <sys/atomic.h>
     38  1.46     pooka #include <sys/kmem.h>
     39  1.53     pooka #include <sys/kthread.h>
     40  1.53     pooka #include <sys/lock.h>
     41   1.1     pooka #include <sys/malloc.h>
     42   1.1     pooka #include <sys/mount.h>
     43  1.53     pooka #include <sys/namei.h>
     44  1.53     pooka #include <sys/proc.h>
     45   1.1     pooka #include <sys/vnode.h>
     46  1.71        ad #include <sys/atomic.h>
     47  1.53     pooka 
     48  1.88      manu #include <uvm/uvm.h>
     49  1.88      manu 
     50  1.55     pooka #include <dev/putter/putter_sys.h>
     51   1.1     pooka 
     52   1.1     pooka #include <fs/puffs/puffs_msgif.h>
     53   1.1     pooka #include <fs/puffs/puffs_sys.h>
     54   1.1     pooka 
     55  1.53     pooka #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
     56  1.53     pooka 
     57  1.22     pooka /*
     58  1.22     pooka  * waitq data structures
     59  1.22     pooka  */
     60  1.22     pooka 
     61  1.22     pooka /*
     62  1.22     pooka  * While a request is going to userspace, park the caller within the
     63  1.22     pooka  * kernel.  This is the kernel counterpart of "struct puffs_req".
     64  1.22     pooka  */
     65  1.46     pooka struct puffs_msgpark {
     66  1.22     pooka 	struct puffs_req	*park_preq;	/* req followed by buf	*/
     67  1.22     pooka 
     68  1.22     pooka 	size_t			park_copylen;	/* userspace copylength	*/
     69  1.22     pooka 	size_t			park_maxlen;	/* max size in comeback */
     70  1.24     pooka 
     71  1.81     pooka 	struct puffs_req	*park_creq;	/* non-compat preq	*/
     72  1.81     pooka 	size_t			park_creqlen;	/* non-compat preq len	*/
     73  1.81     pooka 
     74  1.46     pooka 	parkdone_fn		park_done;	/* "biodone" a'la puffs	*/
     75  1.24     pooka 	void			*park_donearg;
     76  1.22     pooka 
     77  1.22     pooka 	int			park_flags;
     78  1.26     pooka 	int			park_refcount;
     79  1.22     pooka 
     80  1.22     pooka 	kcondvar_t		park_cv;
     81  1.26     pooka 	kmutex_t		park_mtx;
     82  1.26     pooka 
     83  1.46     pooka 	TAILQ_ENTRY(puffs_msgpark) park_entries;
     84  1.22     pooka };
     85  1.22     pooka #define PARKFLAG_WAITERGONE	0x01
     86  1.26     pooka #define PARKFLAG_DONE		0x02
     87  1.26     pooka #define PARKFLAG_ONQUEUE1	0x04
     88  1.26     pooka #define PARKFLAG_ONQUEUE2	0x08
     89  1.26     pooka #define PARKFLAG_CALL		0x10
     90  1.31     pooka #define PARKFLAG_WANTREPLY	0x20
     91  1.57     pooka #define	PARKFLAG_HASERROR	0x40
     92  1.22     pooka 
     93  1.52        ad static pool_cache_t parkpc;
     94  1.57     pooka #ifdef PUFFSDEBUG
     95  1.57     pooka static int totalpark;
     96  1.57     pooka #endif
     97  1.22     pooka 
     98  1.90      manu int puffs_sopreq_expire_timeout = PUFFS_SOPREQ_EXPIRE_TIMEOUT;
     99  1.90      manu 
    100  1.22     pooka static int
    101  1.22     pooka makepark(void *arg, void *obj, int flags)
    102  1.22     pooka {
    103  1.46     pooka 	struct puffs_msgpark *park = obj;
    104  1.22     pooka 
    105  1.26     pooka 	mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
    106  1.22     pooka 	cv_init(&park->park_cv, "puffsrpl");
    107  1.22     pooka 
    108  1.22     pooka 	return 0;
    109  1.22     pooka }
    110  1.22     pooka 
    111  1.22     pooka static void
    112  1.22     pooka nukepark(void *arg, void *obj)
    113  1.22     pooka {
    114  1.46     pooka 	struct puffs_msgpark *park = obj;
    115  1.22     pooka 
    116  1.22     pooka 	cv_destroy(&park->park_cv);
    117  1.26     pooka 	mutex_destroy(&park->park_mtx);
    118  1.22     pooka }
    119  1.22     pooka 
    120  1.22     pooka void
    121  1.73    cegger puffs_msgif_init(void)
    122  1.22     pooka {
    123  1.22     pooka 
    124  1.52        ad 	parkpc = pool_cache_init(sizeof(struct puffs_msgpark), 0, 0, 0,
    125  1.52        ad 	    "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
    126  1.22     pooka }
    127  1.22     pooka 
    128  1.22     pooka void
    129  1.73    cegger puffs_msgif_destroy(void)
    130  1.22     pooka {
    131  1.22     pooka 
    132  1.52        ad 	pool_cache_destroy(parkpc);
    133  1.22     pooka }
    134  1.22     pooka 
    135  1.46     pooka static struct puffs_msgpark *
    136  1.46     pooka puffs_msgpark_alloc(int waitok)
    137  1.26     pooka {
    138  1.46     pooka 	struct puffs_msgpark *park;
    139  1.26     pooka 
    140  1.89      manu 	KASSERT(curlwp != uvm.pagedaemon_lwp || !waitok);
    141  1.89      manu 
    142  1.52        ad 	park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
    143  1.46     pooka 	if (park == NULL)
    144  1.46     pooka 		return park;
    145  1.46     pooka 
    146  1.46     pooka 	park->park_refcount = 1;
    147  1.81     pooka 	park->park_preq = park->park_creq = NULL;
    148  1.46     pooka 	park->park_flags = PARKFLAG_WANTREPLY;
    149  1.26     pooka 
    150  1.57     pooka #ifdef PUFFSDEBUG
    151  1.57     pooka 	totalpark++;
    152  1.57     pooka #endif
    153  1.57     pooka 
    154  1.26     pooka 	return park;
    155  1.26     pooka }
    156  1.26     pooka 
    157  1.26     pooka static void
    158  1.46     pooka puffs_msgpark_reference(struct puffs_msgpark *park)
    159  1.22     pooka {
    160  1.22     pooka 
    161  1.46     pooka 	KASSERT(mutex_owned(&park->park_mtx));
    162  1.26     pooka 	park->park_refcount++;
    163  1.22     pooka }
    164  1.22     pooka 
    165  1.46     pooka /*
    166  1.46     pooka  * Release reference to park structure.
    167  1.46     pooka  */
    168  1.46     pooka static void
    169  1.46     pooka puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
    170  1.22     pooka {
    171  1.46     pooka 	struct puffs_req *preq = park->park_preq;
    172  1.81     pooka 	struct puffs_req *creq = park->park_creq;
    173  1.46     pooka 	int refcnt;
    174  1.22     pooka 
    175  1.26     pooka 	KASSERT(mutex_owned(&park->park_mtx));
    176  1.46     pooka 	refcnt = park->park_refcount -= howmany;
    177  1.46     pooka 	mutex_exit(&park->park_mtx);
    178  1.46     pooka 
    179  1.46     pooka 	KASSERT(refcnt >= 0);
    180  1.26     pooka 
    181  1.46     pooka 	if (refcnt == 0) {
    182  1.46     pooka 		if (preq)
    183  1.46     pooka 			kmem_free(preq, park->park_maxlen);
    184  1.81     pooka #if 1
    185  1.81     pooka 		if (creq)
    186  1.81     pooka 			kmem_free(creq, park->park_creqlen);
    187  1.81     pooka #endif
    188  1.52        ad 		pool_cache_put(parkpc, park);
    189  1.57     pooka 
    190  1.57     pooka #ifdef PUFFSDEBUG
    191  1.57     pooka 		totalpark--;
    192  1.57     pooka #endif
    193  1.46     pooka 	}
    194  1.22     pooka }
    195  1.46     pooka #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
    196  1.22     pooka 
    197  1.26     pooka #ifdef PUFFSDEBUG
    198  1.26     pooka static void
    199  1.46     pooka parkdump(struct puffs_msgpark *park)
    200  1.26     pooka {
    201  1.26     pooka 
    202  1.26     pooka 	DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
    203  1.26     pooka 	    "\tcopy %zu, max %zu - done: %p/%p\n"
    204  1.26     pooka 	    "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
    205  1.46     pooka 	    park, park->park_preq, park->park_preq->preq_id,
    206  1.26     pooka 	    park->park_copylen, park->park_maxlen,
    207  1.26     pooka 	    park->park_done, park->park_donearg,
    208  1.26     pooka 	    park->park_flags, park->park_refcount,
    209  1.26     pooka 	    &park->park_cv, &park->park_mtx));
    210  1.26     pooka }
    211  1.26     pooka 
    212  1.26     pooka static void
    213  1.26     pooka parkqdump(struct puffs_wq *q, int dumpall)
    214  1.26     pooka {
    215  1.46     pooka 	struct puffs_msgpark *park;
    216  1.26     pooka 	int total = 0;
    217  1.26     pooka 
    218  1.26     pooka 	TAILQ_FOREACH(park, q, park_entries) {
    219  1.26     pooka 		if (dumpall)
    220  1.26     pooka 			parkdump(park);
    221  1.26     pooka 		total++;
    222  1.26     pooka 	}
    223  1.29     pooka 	DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
    224  1.26     pooka 
    225  1.26     pooka }
    226  1.26     pooka #endif /* PUFFSDEBUG */
    227  1.22     pooka 
    228  1.22     pooka /*
    229  1.46     pooka  * A word about locking in the park structures: the lock protects the
    230  1.46     pooka  * fields of the *park* structure (not preq) and acts as an interlock
    231  1.46     pooka  * in cv operations.  The lock is always internal to this module and
    232  1.46     pooka  * callers do not need to worry about it.
    233  1.22     pooka  */
    234  1.46     pooka 
    235  1.46     pooka int
    236  1.46     pooka puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
    237  1.46     pooka 	int cansleep)
    238  1.46     pooka {
    239  1.46     pooka 	struct puffs_msgpark *park;
    240  1.46     pooka 	void *m;
    241  1.46     pooka 
    242  1.89      manu 	KASSERT(curlwp != uvm.pagedaemon_lwp || !cansleep);
    243  1.46     pooka 	m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
    244  1.46     pooka 	if (m == NULL) {
    245  1.46     pooka 		KASSERT(cansleep == 0);
    246  1.46     pooka 		return ENOMEM;
    247  1.46     pooka 	}
    248  1.46     pooka 
    249  1.46     pooka 	park = puffs_msgpark_alloc(cansleep);
    250  1.46     pooka 	if (park == NULL) {
    251  1.46     pooka 		KASSERT(cansleep == 0);
    252  1.46     pooka 		kmem_free(m, len);
    253  1.46     pooka 		return ENOMEM;
    254  1.46     pooka 	}
    255  1.46     pooka 
    256  1.46     pooka 	park->park_preq = m;
    257  1.57     pooka 	park->park_maxlen = park->park_copylen = len;
    258  1.46     pooka 
    259  1.46     pooka 	*ppark = park;
    260  1.46     pooka 	*mem = m;
    261  1.46     pooka 
    262  1.46     pooka 	return 0;
    263  1.46     pooka }
    264  1.46     pooka 
    265  1.46     pooka void
    266  1.46     pooka puffs_msgmem_release(struct puffs_msgpark *park)
    267  1.22     pooka {
    268  1.22     pooka 
    269  1.46     pooka 	if (park == NULL)
    270  1.46     pooka 		return;
    271  1.22     pooka 
    272  1.46     pooka 	mutex_enter(&park->park_mtx);
    273  1.46     pooka 	puffs_msgpark_release(park);
    274  1.46     pooka }
    275  1.22     pooka 
    276  1.46     pooka void
    277  1.46     pooka puffs_msg_setfaf(struct puffs_msgpark *park)
    278  1.46     pooka {
    279  1.22     pooka 
    280  1.57     pooka 	KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    281  1.36     pooka 	park->park_flags &= ~PARKFLAG_WANTREPLY;
    282  1.22     pooka }
    283  1.22     pooka 
    284  1.57     pooka void
    285  1.57     pooka puffs_msg_setdelta(struct puffs_msgpark *park, size_t delta)
    286   1.1     pooka {
    287   1.1     pooka 
    288  1.57     pooka 	KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
    289  1.57     pooka 	park->park_copylen = park->park_maxlen - delta;
    290   1.1     pooka }
    291   1.1     pooka 
    292  1.57     pooka void
    293  1.64     pooka puffs_msg_setinfo(struct puffs_msgpark *park, int class, int type,
    294  1.64     pooka 	puffs_cookie_t ck)
    295   1.1     pooka {
    296   1.1     pooka 
    297  1.57     pooka 	park->park_preq->preq_opclass = PUFFSOP_OPCLASS(class);
    298  1.57     pooka 	park->park_preq->preq_optype = type;
    299  1.64     pooka 	park->park_preq->preq_cookie = ck;
    300   1.1     pooka }
    301   1.1     pooka 
    302  1.24     pooka void
    303  1.57     pooka puffs_msg_setcall(struct puffs_msgpark *park, parkdone_fn donefn, void *donearg)
    304   1.1     pooka {
    305  1.25     pooka 
    306  1.57     pooka 	KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    307  1.25     pooka 	park->park_done = donefn;
    308  1.25     pooka 	park->park_donearg = donearg;
    309  1.46     pooka 	park->park_flags |= PARKFLAG_CALL;
    310  1.20     pooka }
    311  1.20     pooka 
    312  1.57     pooka /*
    313  1.57     pooka  * kernel-user-kernel waitqueues
    314  1.57     pooka  */
    315   1.4     pooka 
    316  1.57     pooka static uint64_t
    317  1.57     pooka puffs_getmsgid(struct puffs_mount *pmp)
    318  1.41     pooka {
    319  1.57     pooka 	uint64_t rv;
    320  1.41     pooka 
    321  1.57     pooka 	mutex_enter(&pmp->pmp_lock);
    322  1.57     pooka 	rv = pmp->pmp_nextmsgid++;
    323  1.57     pooka 	mutex_exit(&pmp->pmp_lock);
    324  1.41     pooka 
    325  1.57     pooka 	return rv;
    326  1.41     pooka }
    327  1.41     pooka 
    328   1.4     pooka /*
    329  1.57     pooka  * A word about reference counting of parks.  A reference must be taken
    330  1.57     pooka  * when accessing a park and additionally when it is on a queue.  So
    331  1.57     pooka  * when taking it off a queue and releasing the access reference, the
    332  1.57     pooka  * reference count is generally decremented by 2.
    333   1.1     pooka  */
    334  1.57     pooka 
    335  1.57     pooka void
    336  1.57     pooka puffs_msg_enqueue(struct puffs_mount *pmp, struct puffs_msgpark *park)
    337   1.1     pooka {
    338  1.26     pooka 	struct lwp *l = curlwp;
    339  1.16     pooka 	struct mount *mp;
    340  1.81     pooka 	struct puffs_req *preq, *creq;
    341  1.81     pooka 	ssize_t delta;
    342   1.1     pooka 
    343  1.83     pooka 	/*
    344  1.83     pooka 	 * Some clients reuse a park, so reset some flags.  We might
    345  1.83     pooka 	 * want to provide a caller-side interface for this and add
    346  1.83     pooka 	 * a few more invariant checks here, but this will do for now.
    347  1.83     pooka 	 */
    348  1.83     pooka 	park->park_flags &= ~(PARKFLAG_DONE | PARKFLAG_HASERROR);
    349  1.83     pooka 	KASSERT((park->park_flags & PARKFLAG_WAITERGONE) == 0);
    350  1.83     pooka 
    351  1.16     pooka 	mp = PMPTOMP(pmp);
    352  1.25     pooka 	preq = park->park_preq;
    353  1.81     pooka 
    354  1.81     pooka #if 1
    355  1.81     pooka 	/* check if we do compat adjustments */
    356  1.81     pooka 	if (pmp->pmp_docompat && puffs_compat_outgoing(preq, &creq, &delta)) {
    357  1.81     pooka 		park->park_creq = park->park_preq;
    358  1.81     pooka 		park->park_creqlen = park->park_maxlen;
    359  1.81     pooka 
    360  1.81     pooka 		park->park_maxlen += delta;
    361  1.81     pooka 		park->park_copylen += delta;
    362  1.81     pooka 		park->park_preq = preq = creq;
    363  1.81     pooka 	}
    364  1.81     pooka #endif
    365  1.81     pooka 
    366  1.46     pooka 	preq->preq_buflen = park->park_maxlen;
    367  1.61     pooka 	KASSERT(preq->preq_id == 0
    368  1.61     pooka 	    || (preq->preq_opclass & PUFFSOPFLAG_ISRESPONSE));
    369  1.46     pooka 
    370  1.46     pooka 	if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
    371  1.46     pooka 		preq->preq_opclass |= PUFFSOPFLAG_FAF;
    372  1.46     pooka 	else
    373  1.46     pooka 		preq->preq_id = puffs_getmsgid(pmp);
    374  1.31     pooka 
    375  1.49     pooka 	/* fill in caller information */
    376  1.49     pooka 	preq->preq_pid = l->l_proc->p_pid;
    377  1.49     pooka 	preq->preq_lid = l->l_lid;
    378  1.49     pooka 
    379  1.19     pooka 	/*
    380  1.51     pooka 	 * To support cv_sig, yet another movie: check if there are signals
    381  1.19     pooka 	 * pending and we are issueing a non-FAF.  If so, return an error
    382  1.57     pooka 	 * directly UNLESS we are issueing INACTIVE/RECLAIM.  In that case,
    383  1.57     pooka 	 * convert it to a FAF, fire off to the file server and return
    384  1.57     pooka 	 * an error.  Yes, this is bordering disgusting.  Barfbags are on me.
    385  1.19     pooka 	 */
    386  1.57     pooka 	if (__predict_false((park->park_flags & PARKFLAG_WANTREPLY)
    387  1.25     pooka 	   && (park->park_flags & PARKFLAG_CALL) == 0
    388  1.57     pooka 	   && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))) {
    389  1.84     pooka 		sigset_t ss;
    390  1.84     pooka 
    391  1.84     pooka 		/*
    392  1.84     pooka 		 * see the comment about signals in puffs_msg_wait.
    393  1.84     pooka 		 */
    394  1.84     pooka 		sigpending1(l, &ss);
    395  1.84     pooka 		if (sigismember(&ss, SIGINT) ||
    396  1.84     pooka 		    sigismember(&ss, SIGTERM) ||
    397  1.84     pooka 		    sigismember(&ss, SIGKILL) ||
    398  1.84     pooka 		    sigismember(&ss, SIGHUP) ||
    399  1.84     pooka 		    sigismember(&ss, SIGQUIT)) {
    400  1.84     pooka 			park->park_flags |= PARKFLAG_HASERROR;
    401  1.84     pooka 			preq->preq_rv = EINTR;
    402  1.84     pooka 			if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
    403  1.84     pooka 			    && (preq->preq_optype == PUFFS_VN_INACTIVE
    404  1.84     pooka 			     || preq->preq_optype == PUFFS_VN_RECLAIM)) {
    405  1.84     pooka 				park->park_preq->preq_opclass |=
    406  1.84     pooka 				    PUFFSOPFLAG_FAF;
    407  1.84     pooka 				park->park_flags &= ~PARKFLAG_WANTREPLY;
    408  1.84     pooka 				DPRINTF(("puffs_msg_enqueue: "
    409  1.84     pooka 				    "converted to FAF %p\n", park));
    410  1.84     pooka 			} else {
    411  1.84     pooka 				return;
    412  1.84     pooka 			}
    413  1.19     pooka 		}
    414  1.19     pooka 	}
    415  1.16     pooka 
    416  1.22     pooka 	mutex_enter(&pmp->pmp_lock);
    417  1.13     pooka 	if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    418  1.22     pooka 		mutex_exit(&pmp->pmp_lock);
    419  1.57     pooka 		park->park_flags |= PARKFLAG_HASERROR;
    420  1.57     pooka 		preq->preq_rv = ENXIO;
    421  1.57     pooka 		return;
    422   1.1     pooka 	}
    423   1.1     pooka 
    424  1.26     pooka #ifdef PUFFSDEBUG
    425  1.46     pooka 	parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
    426  1.46     pooka 	parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
    427  1.26     pooka #endif
    428  1.26     pooka 
    429  1.57     pooka 	/*
    430  1.57     pooka 	 * Note: we don't need to lock park since we have the only
    431  1.57     pooka 	 * reference to it at this point.
    432  1.57     pooka 	 */
    433  1.46     pooka 	TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
    434  1.26     pooka 	park->park_flags |= PARKFLAG_ONQUEUE1;
    435  1.46     pooka 	pmp->pmp_msg_touser_count++;
    436  1.57     pooka 	park->park_refcount++;
    437  1.26     pooka 	mutex_exit(&pmp->pmp_lock);
    438   1.1     pooka 
    439  1.57     pooka 	cv_broadcast(&pmp->pmp_msg_waiter_cv);
    440  1.57     pooka 	putter_notify(pmp->pmp_pi);
    441  1.57     pooka 
    442  1.20     pooka 	DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
    443  1.25     pooka 	    "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
    444  1.25     pooka 	    preq->preq_opclass, preq->preq_optype, park->park_flags));
    445  1.57     pooka }
    446  1.15     pooka 
    447  1.57     pooka int
    448  1.57     pooka puffs_msg_wait(struct puffs_mount *pmp, struct puffs_msgpark *park)
    449  1.57     pooka {
    450  1.84     pooka 	lwp_t *l = curlwp;
    451  1.84     pooka 	proc_t *p = l->l_proc;
    452  1.57     pooka 	struct puffs_req *preq = park->park_preq; /* XXX: hmmm */
    453  1.84     pooka 	sigset_t ss;
    454  1.84     pooka 	sigset_t oss;
    455  1.57     pooka 	int error = 0;
    456  1.57     pooka 	int rv;
    457   1.1     pooka 
    458  1.84     pooka 	/*
    459  1.84     pooka 	 * block unimportant signals.
    460  1.84     pooka 	 *
    461  1.84     pooka 	 * The set of "important" signals here was chosen to be same as
    462  1.84     pooka 	 * nfs interruptible mount.
    463  1.84     pooka 	 */
    464  1.84     pooka 	sigfillset(&ss);
    465  1.84     pooka 	sigdelset(&ss, SIGINT);
    466  1.84     pooka 	sigdelset(&ss, SIGTERM);
    467  1.84     pooka 	sigdelset(&ss, SIGKILL);
    468  1.84     pooka 	sigdelset(&ss, SIGHUP);
    469  1.84     pooka 	sigdelset(&ss, SIGQUIT);
    470  1.84     pooka 	mutex_enter(p->p_lock);
    471  1.84     pooka 	sigprocmask1(l, SIG_BLOCK, &ss, &oss);
    472  1.84     pooka 	mutex_exit(p->p_lock);
    473  1.84     pooka 
    474  1.57     pooka 	mutex_enter(&pmp->pmp_lock);
    475  1.57     pooka 	puffs_mp_reference(pmp);
    476  1.57     pooka 	mutex_exit(&pmp->pmp_lock);
    477  1.46     pooka 
    478  1.57     pooka 	mutex_enter(&park->park_mtx);
    479  1.85      yamt 	/* did the response beat us to the wait? */
    480  1.85      yamt 	if (__predict_false((park->park_flags & PARKFLAG_DONE)
    481  1.85      yamt 	    || (park->park_flags & PARKFLAG_HASERROR))) {
    482  1.85      yamt 		rv = park->park_preq->preq_rv;
    483  1.57     pooka 		mutex_exit(&park->park_mtx);
    484  1.57     pooka 		goto skipwait;
    485  1.57     pooka 	}
    486  1.46     pooka 
    487  1.85      yamt 	if ((park->park_flags & PARKFLAG_WANTREPLY) == 0
    488  1.85      yamt 	    || (park->park_flags & PARKFLAG_CALL)) {
    489  1.57     pooka 		mutex_exit(&park->park_mtx);
    490  1.85      yamt 		rv = 0;
    491  1.57     pooka 		goto skipwait;
    492  1.57     pooka 	}
    493  1.26     pooka 
    494  1.57     pooka 	error = cv_wait_sig(&park->park_cv, &park->park_mtx);
    495  1.57     pooka 	DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
    496  1.57     pooka 	    park, error));
    497  1.57     pooka 	if (error) {
    498  1.57     pooka 		park->park_flags |= PARKFLAG_WAITERGONE;
    499  1.57     pooka 		if (park->park_flags & PARKFLAG_DONE) {
    500  1.57     pooka 			rv = preq->preq_rv;
    501  1.57     pooka 			mutex_exit(&park->park_mtx);
    502  1.57     pooka 		} else {
    503  1.57     pooka 			/*
    504  1.57     pooka 			 * ok, we marked it as going away, but
    505  1.57     pooka 			 * still need to do queue ops.  take locks
    506  1.57     pooka 			 * in correct order.
    507  1.57     pooka 			 *
    508  1.57     pooka 			 * We don't want to release our reference
    509  1.57     pooka 			 * if it's on replywait queue to avoid error
    510  1.57     pooka 			 * to file server.  putop() code will DTRT.
    511  1.57     pooka 			 */
    512  1.57     pooka 			mutex_exit(&park->park_mtx);
    513  1.57     pooka 			mutex_enter(&pmp->pmp_lock);
    514  1.57     pooka 			mutex_enter(&park->park_mtx);
    515  1.57     pooka 
    516  1.57     pooka 			/*
    517  1.57     pooka 			 * Still on queue1?  We can safely remove it
    518  1.57     pooka 			 * without any consequences since the file
    519  1.57     pooka 			 * server hasn't seen it.  "else" we need to
    520  1.57     pooka 			 * wait for the response and just ignore it
    521  1.57     pooka 			 * to avoid signalling an incorrect error to
    522  1.57     pooka 			 * the file server.
    523  1.57     pooka 			 */
    524  1.57     pooka 			if (park->park_flags & PARKFLAG_ONQUEUE1) {
    525  1.57     pooka 				TAILQ_REMOVE(&pmp->pmp_msg_touser,
    526  1.57     pooka 				    park, park_entries);
    527  1.57     pooka 				puffs_msgpark_release(park);
    528  1.57     pooka 				pmp->pmp_msg_touser_count--;
    529  1.57     pooka 				park->park_flags &= ~PARKFLAG_ONQUEUE1;
    530  1.57     pooka 			} else {
    531  1.57     pooka 				mutex_exit(&park->park_mtx);
    532  1.19     pooka 			}
    533  1.57     pooka 			mutex_exit(&pmp->pmp_lock);
    534  1.22     pooka 
    535  1.60     pooka 			rv = EINTR;
    536  1.16     pooka 		}
    537  1.22     pooka 	} else {
    538  1.57     pooka 		rv = preq->preq_rv;
    539  1.57     pooka 		mutex_exit(&park->park_mtx);
    540  1.16     pooka 	}
    541  1.16     pooka 
    542  1.57     pooka  skipwait:
    543  1.22     pooka 	mutex_enter(&pmp->pmp_lock);
    544  1.34     pooka 	puffs_mp_release(pmp);
    545  1.22     pooka 	mutex_exit(&pmp->pmp_lock);
    546  1.16     pooka 
    547  1.84     pooka 	mutex_enter(p->p_lock);
    548  1.84     pooka 	sigprocmask1(l, SIG_SETMASK, &oss, NULL);
    549  1.84     pooka 	mutex_exit(p->p_lock);
    550  1.84     pooka 
    551  1.19     pooka 	return rv;
    552   1.1     pooka }
    553   1.1     pooka 
    554   1.9     pooka /*
    555  1.57     pooka  * XXX: this suuuucks.  Hopefully I'll get rid of this lossage once
    556  1.57     pooka  * the whole setback-nonsense gets fixed.
    557  1.57     pooka  */
    558  1.57     pooka int
    559  1.57     pooka puffs_msg_wait2(struct puffs_mount *pmp, struct puffs_msgpark *park,
    560  1.57     pooka 	struct puffs_node *pn1, struct puffs_node *pn2)
    561  1.57     pooka {
    562  1.57     pooka 	struct puffs_req *preq;
    563  1.57     pooka 	int rv;
    564  1.57     pooka 
    565  1.57     pooka 	rv = puffs_msg_wait(pmp, park);
    566  1.57     pooka 
    567  1.57     pooka 	preq = park->park_preq;
    568  1.57     pooka 	if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N1)
    569  1.57     pooka 		pn1->pn_stat |= PNODE_DOINACT;
    570  1.57     pooka 	if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N2)
    571  1.57     pooka 		pn2->pn_stat |= PNODE_DOINACT;
    572  1.57     pooka 
    573  1.57     pooka 	if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1)
    574  1.57     pooka 		pn1->pn_stat |= PNODE_NOREFS;
    575  1.57     pooka 	if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2)
    576  1.57     pooka 		pn2->pn_stat |= PNODE_NOREFS;
    577  1.57     pooka 
    578  1.57     pooka 	return rv;
    579  1.57     pooka 
    580  1.57     pooka }
    581  1.57     pooka 
    582  1.57     pooka /*
    583  1.61     pooka  * XXX: lazy bum.  please, for the love of foie gras, fix me.
    584  1.61     pooka  * This should *NOT* depend on setfaf.  Also "memcpy" could
    585  1.61     pooka  * be done more nicely.
    586  1.61     pooka  */
    587  1.61     pooka void
    588  1.61     pooka puffs_msg_sendresp(struct puffs_mount *pmp, struct puffs_req *origpreq, int rv)
    589  1.61     pooka {
    590  1.61     pooka 	struct puffs_msgpark *park;
    591  1.61     pooka 	struct puffs_req *preq;
    592  1.61     pooka 
    593  1.63     pooka 	puffs_msgmem_alloc(sizeof(struct puffs_req), &park, (void *)&preq, 1);
    594  1.61     pooka 	puffs_msg_setfaf(park); /* XXXXXX: avoids reqid override */
    595  1.61     pooka 
    596  1.61     pooka 	memcpy(preq, origpreq, sizeof(struct puffs_req));
    597  1.61     pooka 	preq->preq_rv = rv;
    598  1.61     pooka 	preq->preq_opclass |= PUFFSOPFLAG_ISRESPONSE;
    599  1.61     pooka 
    600  1.61     pooka 	puffs_msg_enqueue(pmp, park);
    601  1.61     pooka 	puffs_msgmem_release(park);
    602  1.61     pooka }
    603  1.61     pooka 
    604  1.61     pooka /*
    605  1.46     pooka  * Get next request in the outgoing queue.  "maxsize" controls the
    606  1.46     pooka  * size the caller can accommodate and "nonblock" signals if this
    607  1.46     pooka  * should block while waiting for input.  Handles all locking internally.
    608   1.9     pooka  */
    609  1.10     pooka int
    610  1.46     pooka puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
    611  1.46     pooka 	uint8_t **data, size_t *dlen, void **parkptr)
    612   1.1     pooka {
    613  1.46     pooka 	struct puffs_mount *pmp = this;
    614  1.87       mrg 	struct puffs_msgpark *park = NULL;
    615  1.87       mrg 	struct puffs_req *preq = NULL;
    616  1.46     pooka 	int error;
    617   1.1     pooka 
    618  1.46     pooka 	error = 0;
    619  1.22     pooka 	mutex_enter(&pmp->pmp_lock);
    620  1.50     pooka 	puffs_mp_reference(pmp);
    621  1.46     pooka 	for (;;) {
    622  1.46     pooka 		/* RIP? */
    623   1.9     pooka 		if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    624   1.9     pooka 			error = ENXIO;
    625  1.46     pooka 			break;
    626   1.9     pooka 		}
    627  1.12     pooka 
    628  1.46     pooka 		/* need platinum yendorian express card? */
    629  1.46     pooka 		if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
    630  1.46     pooka 			DPRINTF(("puffs_getout: no outgoing op, "));
    631  1.12     pooka 			if (nonblock) {
    632  1.46     pooka 				DPRINTF(("returning EWOULDBLOCK\n"));
    633  1.12     pooka 				error = EWOULDBLOCK;
    634  1.46     pooka 				break;
    635   1.9     pooka 			}
    636  1.46     pooka 			DPRINTF(("waiting ...\n"));
    637   1.9     pooka 
    638  1.46     pooka 			error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
    639  1.22     pooka 			    &pmp->pmp_lock);
    640  1.11     pooka 			if (error)
    641  1.46     pooka 				break;
    642  1.11     pooka 			else
    643  1.46     pooka 				continue;
    644   1.9     pooka 		}
    645   1.9     pooka 
    646  1.46     pooka 		park = TAILQ_FIRST(&pmp->pmp_msg_touser);
    647  1.50     pooka 		if (park == NULL)
    648  1.50     pooka 			continue;
    649  1.50     pooka 
    650  1.46     pooka 		mutex_enter(&park->park_mtx);
    651  1.46     pooka 		puffs_msgpark_reference(park);
    652  1.46     pooka 
    653  1.46     pooka 		DPRINTF(("puffs_getout: found park at %p, ", park));
    654  1.22     pooka 
    655  1.22     pooka 		/* If it's a goner, don't process any furher */
    656  1.22     pooka 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    657  1.46     pooka 			DPRINTF(("waitergone!\n"));
    658  1.46     pooka 			puffs_msgpark_release(park);
    659  1.22     pooka 			continue;
    660  1.22     pooka 		}
    661  1.55     pooka 		preq = park->park_preq;
    662  1.22     pooka 
    663  1.55     pooka #if 0
    664  1.46     pooka 		/* check size */
    665  1.55     pooka 		/*
    666  1.55     pooka 		 * XXX: this check is not valid for now, we don't know
    667  1.55     pooka 		 * the size of the caller's input buffer.  i.e. this
    668  1.55     pooka 		 * will most likely go away
    669  1.55     pooka 		 */
    670  1.46     pooka 		if (maxsize < preq->preq_frhdr.pfr_len) {
    671  1.46     pooka 			DPRINTF(("buffer too small\n"));
    672  1.46     pooka 			puffs_msgpark_release(park);
    673  1.46     pooka 			error = E2BIG;
    674  1.46     pooka 			break;
    675  1.26     pooka 		}
    676  1.55     pooka #endif
    677  1.28     pooka 
    678  1.46     pooka 		DPRINTF(("returning\n"));
    679  1.46     pooka 
    680  1.46     pooka 		/*
    681  1.46     pooka 		 * Ok, we found what we came for.  Release it from the
    682  1.46     pooka 		 * outgoing queue but do not unlock.  We will unlock
    683  1.46     pooka 		 * only after we "releaseout" it to avoid complications:
    684  1.46     pooka 		 * otherwise it is (theoretically) possible for userland
    685  1.46     pooka 		 * to race us into "put" before we have a change to put
    686  1.46     pooka 		 * this baby on the receiving queue.
    687  1.46     pooka 		 */
    688  1.46     pooka 		TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
    689  1.28     pooka 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    690  1.28     pooka 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    691  1.46     pooka 		mutex_exit(&park->park_mtx);
    692  1.46     pooka 
    693  1.46     pooka 		pmp->pmp_msg_touser_count--;
    694  1.46     pooka 		KASSERT(pmp->pmp_msg_touser_count >= 0);
    695  1.26     pooka 
    696  1.46     pooka 		break;
    697  1.46     pooka 	}
    698  1.50     pooka 	puffs_mp_release(pmp);
    699  1.46     pooka 	mutex_exit(&pmp->pmp_lock);
    700   1.9     pooka 
    701  1.46     pooka 	if (error == 0) {
    702  1.46     pooka 		*data = (uint8_t *)preq;
    703  1.55     pooka 		preq->preq_pth.pth_framelen = park->park_copylen;
    704  1.55     pooka 		*dlen = preq->preq_pth.pth_framelen;
    705  1.46     pooka 		*parkptr = park;
    706  1.46     pooka 	}
    707  1.51     pooka 
    708  1.46     pooka 	return error;
    709  1.46     pooka }
    710   1.9     pooka 
    711  1.46     pooka /*
    712  1.46     pooka  * Release outgoing structure.  Now, depending on the success of the
    713  1.46     pooka  * outgoing send, it is either going onto the result waiting queue
    714  1.46     pooka  * or the death chamber.
    715  1.46     pooka  */
    716  1.46     pooka void
    717  1.46     pooka puffs_msgif_releaseout(void *this, void *parkptr, int status)
    718  1.46     pooka {
    719  1.46     pooka 	struct puffs_mount *pmp = this;
    720  1.46     pooka 	struct puffs_msgpark *park = parkptr;
    721  1.32     pooka 
    722  1.46     pooka 	DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
    723  1.46     pooka 	    park, status));
    724  1.46     pooka 	mutex_enter(&pmp->pmp_lock);
    725  1.46     pooka 	mutex_enter(&park->park_mtx);
    726  1.46     pooka 	if (park->park_flags & PARKFLAG_WANTREPLY) {
    727  1.46     pooka 		if (status == 0) {
    728  1.46     pooka 			DPRINTF(("enqueue replywait\n"));
    729  1.46     pooka 			TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
    730  1.22     pooka 			    park_entries);
    731  1.26     pooka 			park->park_flags |= PARKFLAG_ONQUEUE2;
    732   1.9     pooka 		} else {
    733  1.46     pooka 			DPRINTF(("error path!\n"));
    734  1.46     pooka 			park->park_preq->preq_rv = status;
    735  1.46     pooka 			park->park_flags |= PARKFLAG_DONE;
    736  1.46     pooka 			cv_signal(&park->park_cv);
    737   1.1     pooka 		}
    738  1.46     pooka 		puffs_msgpark_release(park);
    739  1.46     pooka 	} else {
    740  1.46     pooka 		DPRINTF(("release\n"));
    741  1.46     pooka 		puffs_msgpark_release1(park, 2);
    742   1.1     pooka 	}
    743  1.22     pooka 	mutex_exit(&pmp->pmp_lock);
    744   1.1     pooka }
    745   1.1     pooka 
    746  1.53     pooka size_t
    747  1.53     pooka puffs_msgif_waitcount(void *this)
    748  1.53     pooka {
    749  1.53     pooka 	struct puffs_mount *pmp = this;
    750  1.53     pooka 	size_t rv;
    751  1.53     pooka 
    752  1.53     pooka 	mutex_enter(&pmp->pmp_lock);
    753  1.53     pooka 	rv = pmp->pmp_msg_touser_count;
    754  1.53     pooka 	mutex_exit(&pmp->pmp_lock);
    755  1.53     pooka 
    756  1.53     pooka 	return rv;
    757  1.53     pooka }
    758  1.53     pooka 
    759  1.50     pooka /*
    760  1.50     pooka  * XXX: locking with this one?
    761  1.50     pooka  */
    762  1.53     pooka static void
    763  1.56     pooka puffsop_msg(void *this, struct puffs_req *preq)
    764   1.1     pooka {
    765  1.46     pooka 	struct puffs_mount *pmp = this;
    766  1.55     pooka 	struct putter_hdr *pth = &preq->preq_pth;
    767  1.46     pooka 	struct puffs_msgpark *park;
    768  1.57     pooka 	int wgone;
    769   1.1     pooka 
    770  1.22     pooka 	mutex_enter(&pmp->pmp_lock);
    771   1.9     pooka 
    772  1.46     pooka 	/* Locate waiter */
    773  1.46     pooka 	TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
    774  1.46     pooka 		if (park->park_preq->preq_id == preq->preq_id)
    775   1.1     pooka 			break;
    776  1.46     pooka 	}
    777  1.46     pooka 	if (park == NULL) {
    778  1.58     pooka 		DPRINTF(("puffsop_msg: no request: %" PRIu64 "\n",
    779  1.46     pooka 		    preq->preq_id));
    780  1.46     pooka 		mutex_exit(&pmp->pmp_lock);
    781  1.46     pooka 		return; /* XXX send error */
    782  1.46     pooka 	}
    783  1.26     pooka 
    784  1.46     pooka 	mutex_enter(&park->park_mtx);
    785  1.46     pooka 	puffs_msgpark_reference(park);
    786  1.55     pooka 	if (pth->pth_framelen > park->park_maxlen) {
    787  1.58     pooka 		DPRINTF(("puffsop_msg: invalid buffer length: "
    788  1.55     pooka 		    "%" PRIu64 " (req %" PRIu64 ", \n", pth->pth_framelen,
    789  1.55     pooka 		    preq->preq_id));
    790  1.46     pooka 		park->park_preq->preq_rv = EPROTO;
    791  1.46     pooka 		cv_signal(&park->park_cv);
    792  1.57     pooka 		puffs_msgpark_release1(park, 2);
    793  1.22     pooka 		mutex_exit(&pmp->pmp_lock);
    794  1.46     pooka 		return; /* XXX: error */
    795  1.46     pooka 	}
    796  1.46     pooka 	wgone = park->park_flags & PARKFLAG_WAITERGONE;
    797   1.9     pooka 
    798  1.46     pooka 	KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
    799  1.46     pooka 	TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
    800  1.46     pooka 	park->park_flags &= ~PARKFLAG_ONQUEUE2;
    801  1.46     pooka 	mutex_exit(&pmp->pmp_lock);
    802  1.24     pooka 
    803  1.46     pooka 	if (wgone) {
    804  1.58     pooka 		DPRINTF(("puffsop_msg: bad service - waiter gone for "
    805  1.46     pooka 		    "park %p\n", park));
    806  1.46     pooka 	} else {
    807  1.81     pooka #if 1
    808  1.81     pooka 		if (park->park_creq) {
    809  1.81     pooka 			struct puffs_req *creq;
    810  1.81     pooka 			size_t csize;
    811  1.81     pooka 
    812  1.81     pooka 			KASSERT(pmp->pmp_docompat);
    813  1.81     pooka 			puffs_compat_incoming(preq, park->park_creq);
    814  1.81     pooka 			creq = park->park_creq;
    815  1.81     pooka 			csize = park->park_creqlen;
    816  1.81     pooka 			park->park_creq = park->park_preq;
    817  1.81     pooka 			park->park_creqlen = park->park_maxlen;
    818  1.81     pooka 
    819  1.81     pooka 			park->park_preq = creq;
    820  1.81     pooka 			park->park_maxlen = csize;
    821  1.81     pooka 
    822  1.81     pooka 			memcpy(park->park_creq, preq, pth->pth_framelen);
    823  1.81     pooka 		} else {
    824  1.81     pooka #endif
    825  1.81     pooka 			memcpy(park->park_preq, preq, pth->pth_framelen);
    826  1.81     pooka 		}
    827  1.81     pooka 
    828  1.24     pooka 		if (park->park_flags & PARKFLAG_CALL) {
    829  1.58     pooka 			DPRINTF(("puffsop_msg: call for %p, arg %p\n",
    830  1.40     pooka 			    park->park_preq, park->park_donearg));
    831  1.55     pooka 			park->park_done(pmp, preq, park->park_donearg);
    832  1.20     pooka 		}
    833  1.46     pooka 	}
    834   1.1     pooka 
    835  1.46     pooka 	if (!wgone) {
    836  1.46     pooka 		DPRINTF(("puffs_putop: flagging done for "
    837  1.46     pooka 		    "park %p\n", park));
    838  1.46     pooka 		cv_signal(&park->park_cv);
    839   1.1     pooka 	}
    840   1.1     pooka 
    841  1.46     pooka 	park->park_flags |= PARKFLAG_DONE;
    842  1.57     pooka 	puffs_msgpark_release1(park, 2);
    843   1.1     pooka }
    844   1.1     pooka 
    845  1.90      manu /*
    846  1.90      manu  * Node expiry. We come here after an inactive on an unexpired node.
    847  1.90      manu  * The expiry has been queued and is done in sop thread.
    848  1.90      manu  */
    849  1.92      manu static void
    850  1.90      manu puffsop_expire(struct puffs_mount *pmp, puffs_cookie_t cookie)
    851  1.90      manu {
    852  1.90      manu 	struct vnode *vp;
    853  1.90      manu 
    854  1.90      manu 	KASSERT(PUFFS_USE_FS_TTL(pmp));
    855  1.90      manu 
    856  1.90      manu 	/*
    857  1.90      manu 	 * If it still exists and has no reference,
    858  1.90      manu 	 * vrele should cause it to be reclaimed.
    859  1.90      manu 	 * Otherwise, we have nothing to do.
    860  1.90      manu 	 */
    861  1.90      manu 	if (puffs_cookie2vnode(pmp, cookie, 0, 0, &vp) == 0) {
    862  1.90      manu 		VPTOPP(vp)->pn_stat &= ~PNODE_SOPEXP;
    863  1.90      manu 		vrele(vp);
    864  1.90      manu 	}
    865  1.90      manu 
    866  1.92      manu 	return;
    867  1.90      manu }
    868  1.90      manu 
    869  1.61     pooka static void
    870  1.53     pooka puffsop_flush(struct puffs_mount *pmp, struct puffs_flush *pf)
    871  1.53     pooka {
    872  1.53     pooka 	struct vnode *vp;
    873  1.53     pooka 	voff_t offlo, offhi;
    874  1.53     pooka 	int rv, flags = 0;
    875  1.53     pooka 
    876  1.76     pooka 	KASSERT(pf->pf_req.preq_pth.pth_framelen == sizeof(struct puffs_flush));
    877  1.61     pooka 
    878  1.53     pooka 	/* XXX: slurry */
    879  1.53     pooka 	if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
    880  1.53     pooka 		cache_purgevfs(PMPTOMP(pmp));
    881  1.61     pooka 		rv = 0;
    882  1.61     pooka 		goto out;
    883  1.53     pooka 	}
    884  1.53     pooka 
    885  1.53     pooka 	/*
    886  1.53     pooka 	 * Get vnode, don't lock it.  Namecache is protected by its own lock
    887  1.53     pooka 	 * and we have a reference to protect against premature harvesting.
    888  1.53     pooka 	 *
    889  1.53     pooka 	 * The node we want here might be locked and the op is in
    890  1.53     pooka 	 * userspace waiting for us to complete ==> deadlock.  Another
    891  1.53     pooka 	 * reason we need to eventually bump locking to userspace, as we
    892  1.53     pooka 	 * will need to lock the node if we wish to do flushes.
    893  1.53     pooka 	 */
    894  1.53     pooka 	rv = puffs_cookie2vnode(pmp, pf->pf_cookie, 0, 0, &vp);
    895  1.53     pooka 	if (rv) {
    896  1.53     pooka 		if (rv == PUFFS_NOSUCHCOOKIE)
    897  1.61     pooka 			rv = ENOENT;
    898  1.61     pooka 		goto out;
    899  1.53     pooka 	}
    900  1.53     pooka 
    901  1.53     pooka 	switch (pf->pf_op) {
    902  1.53     pooka #if 0
    903  1.53     pooka 	/* not quite ready, yet */
    904  1.53     pooka 	case PUFFS_INVAL_NAMECACHE_NODE:
    905  1.53     pooka 	struct componentname *pf_cn;
    906  1.53     pooka 	char *name;
    907  1.53     pooka 		/* get comfortab^Wcomponentname */
    908  1.59     pooka 		pf_cn = kmem_alloc(componentname);
    909  1.53     pooka 		memset(pf_cn, 0, sizeof(struct componentname));
    910  1.53     pooka 		break;
    911  1.53     pooka 
    912  1.53     pooka #endif
    913  1.53     pooka 	case PUFFS_INVAL_NAMECACHE_DIR:
    914  1.53     pooka 		if (vp->v_type != VDIR) {
    915  1.53     pooka 			rv = EINVAL;
    916  1.53     pooka 			break;
    917  1.53     pooka 		}
    918  1.93  dholland 		cache_purge1(vp, NULL, 0, PURGE_CHILDREN);
    919  1.53     pooka 		break;
    920  1.53     pooka 
    921  1.53     pooka 	case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
    922  1.53     pooka 		flags = PGO_FREE;
    923  1.53     pooka 		/*FALLTHROUGH*/
    924  1.53     pooka 	case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
    925  1.53     pooka 		if (flags == 0)
    926  1.53     pooka 			flags = PGO_CLEANIT;
    927  1.53     pooka 
    928  1.53     pooka 		if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
    929  1.53     pooka 			rv = EINVAL;
    930  1.53     pooka 			break;
    931  1.53     pooka 		}
    932  1.53     pooka 
    933  1.53     pooka 		offlo = trunc_page(pf->pf_start);
    934  1.53     pooka 		offhi = round_page(pf->pf_end);
    935  1.53     pooka 		if (offhi != 0 && offlo >= offhi) {
    936  1.53     pooka 			rv = EINVAL;
    937  1.53     pooka 			break;
    938  1.53     pooka 		}
    939  1.53     pooka 
    940  1.86     rmind 		mutex_enter(vp->v_uobj.vmobjlock);
    941  1.53     pooka 		rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
    942  1.53     pooka 		break;
    943  1.53     pooka 
    944  1.53     pooka 	default:
    945  1.53     pooka 		rv = EINVAL;
    946  1.53     pooka 	}
    947  1.53     pooka 
    948  1.53     pooka 	vrele(vp);
    949  1.53     pooka 
    950  1.61     pooka  out:
    951  1.61     pooka 	puffs_msg_sendresp(pmp, &pf->pf_req, rv);
    952  1.53     pooka }
    953  1.53     pooka 
    954  1.53     pooka int
    955  1.56     pooka puffs_msgif_dispatch(void *this, struct putter_hdr *pth)
    956  1.53     pooka {
    957  1.53     pooka 	struct puffs_mount *pmp = this;
    958  1.56     pooka 	struct puffs_req *preq = (struct puffs_req *)pth;
    959  1.76     pooka 	struct puffs_sopreq *psopr;
    960  1.56     pooka 
    961  1.61     pooka 	if (pth->pth_framelen < sizeof(struct puffs_req)) {
    962  1.61     pooka 		puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
    963  1.61     pooka 		return 0;
    964  1.61     pooka 	}
    965  1.53     pooka 
    966  1.55     pooka 	switch (PUFFSOP_OPCLASS(preq->preq_opclass)) {
    967  1.53     pooka 	case PUFFSOP_VN:
    968  1.53     pooka 	case PUFFSOP_VFS:
    969  1.61     pooka 		DPRINTF(("dispatch: vn/vfs message 0x%x\n", preq->preq_optype));
    970  1.56     pooka 		puffsop_msg(pmp, preq);
    971  1.53     pooka 		break;
    972  1.77     pooka 
    973  1.76     pooka 	case PUFFSOP_FLUSH: /* process in sop thread */
    974  1.76     pooka 	{
    975  1.76     pooka 		struct puffs_flush *pf;
    976  1.76     pooka 
    977  1.61     pooka 		DPRINTF(("dispatch: flush 0x%x\n", preq->preq_optype));
    978  1.76     pooka 
    979  1.76     pooka 		if (preq->preq_pth.pth_framelen != sizeof(struct puffs_flush)) {
    980  1.76     pooka 			puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
    981  1.76     pooka 			break;
    982  1.76     pooka 		}
    983  1.76     pooka 		pf = (struct puffs_flush *)preq;
    984  1.76     pooka 
    985  1.88      manu 		KASSERT(curlwp != uvm.pagedaemon_lwp);
    986  1.76     pooka 		psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
    987  1.76     pooka 		memcpy(&psopr->psopr_pf, pf, sizeof(*pf));
    988  1.76     pooka 		psopr->psopr_sopreq = PUFFS_SOPREQ_FLUSH;
    989  1.76     pooka 
    990  1.76     pooka 		mutex_enter(&pmp->pmp_sopmtx);
    991  1.80     pooka 		if (pmp->pmp_sopthrcount == 0) {
    992  1.80     pooka 			mutex_exit(&pmp->pmp_sopmtx);
    993  1.80     pooka 			kmem_free(psopr, sizeof(*psopr));
    994  1.80     pooka 			puffs_msg_sendresp(pmp, preq, ENXIO);
    995  1.80     pooka 		} else {
    996  1.90      manu 			TAILQ_INSERT_TAIL(&pmp->pmp_sopfastreqs,
    997  1.80     pooka 			    psopr, psopr_entries);
    998  1.80     pooka 			cv_signal(&pmp->pmp_sopcv);
    999  1.80     pooka 			mutex_exit(&pmp->pmp_sopmtx);
   1000  1.80     pooka 		}
   1001  1.53     pooka 		break;
   1002  1.76     pooka 	}
   1003  1.77     pooka 
   1004  1.77     pooka 	case PUFFSOP_UNMOUNT: /* process in sop thread */
   1005  1.77     pooka 	{
   1006  1.77     pooka 
   1007  1.77     pooka 		DPRINTF(("dispatch: unmount 0x%x\n", preq->preq_optype));
   1008  1.77     pooka 
   1009  1.88      manu 		KASSERT(curlwp != uvm.pagedaemon_lwp);
   1010  1.77     pooka 		psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
   1011  1.77     pooka 		psopr->psopr_preq = *preq;
   1012  1.77     pooka 		psopr->psopr_sopreq = PUFFS_SOPREQ_UNMOUNT;
   1013  1.77     pooka 
   1014  1.77     pooka 		mutex_enter(&pmp->pmp_sopmtx);
   1015  1.80     pooka 		if (pmp->pmp_sopthrcount == 0) {
   1016  1.80     pooka 			mutex_exit(&pmp->pmp_sopmtx);
   1017  1.80     pooka 			kmem_free(psopr, sizeof(*psopr));
   1018  1.80     pooka 			puffs_msg_sendresp(pmp, preq, ENXIO);
   1019  1.80     pooka 		} else {
   1020  1.90      manu 			TAILQ_INSERT_TAIL(&pmp->pmp_sopfastreqs,
   1021  1.80     pooka 			    psopr, psopr_entries);
   1022  1.80     pooka 			cv_signal(&pmp->pmp_sopcv);
   1023  1.80     pooka 			mutex_exit(&pmp->pmp_sopmtx);
   1024  1.80     pooka 		}
   1025  1.77     pooka 		break;
   1026  1.77     pooka 	}
   1027  1.77     pooka 
   1028  1.53     pooka 	default:
   1029  1.61     pooka 		DPRINTF(("dispatch: invalid class 0x%x\n", preq->preq_opclass));
   1030  1.75     pooka 		puffs_msg_sendresp(pmp, preq, EOPNOTSUPP);
   1031  1.53     pooka 		break;
   1032  1.53     pooka 	}
   1033  1.53     pooka 
   1034  1.76     pooka 	return 0;
   1035  1.76     pooka }
   1036  1.76     pooka 
   1037  1.76     pooka /*
   1038  1.76     pooka  * Work loop for thread processing all ops from server which
   1039  1.76     pooka  * cannot safely be handled in caller context.  This includes
   1040  1.76     pooka  * everything which might need a lock currently "held" by the file
   1041  1.76     pooka  * server, i.e. a long-term kernel lock which will be released only
   1042  1.76     pooka  * once the file server acknowledges a request
   1043  1.76     pooka  */
   1044  1.90      manu #define TIMED_OUT(expire) \
   1045  1.90      manu     ((int)((unsigned int)hardclock_ticks - (unsigned int)expire) > 0)
   1046  1.76     pooka void
   1047  1.76     pooka puffs_sop_thread(void *arg)
   1048  1.76     pooka {
   1049  1.76     pooka 	struct puffs_mount *pmp = arg;
   1050  1.77     pooka 	struct mount *mp = PMPTOMP(pmp);
   1051  1.76     pooka 	struct puffs_sopreq *psopr;
   1052  1.76     pooka 	bool keeprunning;
   1053  1.77     pooka 	bool unmountme = false;
   1054  1.90      manu 	int timeo;
   1055  1.90      manu 
   1056  1.90      manu 	timeo = PUFFS_USE_FS_TTL(pmp) ? puffs_sopreq_expire_timeout : 0;
   1057  1.76     pooka 
   1058  1.76     pooka 	mutex_enter(&pmp->pmp_sopmtx);
   1059  1.76     pooka 	for (keeprunning = true; keeprunning; ) {
   1060  1.90      manu 		/*
   1061  1.92      manu 		 * We have a fast queue for flush and umount, and a node
   1062  1.92      manu 		 * queue for delayes node reclaims. Requests on node queue 			 * are not honoured before clock reaches psopr_at. This
   1063  1.92      manu 		 * code assumes that requests are ordered by psopr_at.
   1064  1.90      manu 		 */
   1065  1.90      manu 		do {
   1066  1.90      manu 			psopr = TAILQ_FIRST(&pmp->pmp_sopfastreqs);
   1067  1.91      manu 			if (psopr != NULL) {
   1068  1.90      manu 				TAILQ_REMOVE(&pmp->pmp_sopfastreqs,
   1069  1.90      manu 					     psopr, psopr_entries);
   1070  1.90      manu 				break;
   1071  1.90      manu 			}
   1072  1.90      manu 
   1073  1.92      manu 			psopr = TAILQ_FIRST(&pmp->pmp_sopnodereqs);
   1074  1.90      manu 			if ((psopr != NULL) && TIMED_OUT(psopr->psopr_at)) {
   1075  1.92      manu 				TAILQ_REMOVE(&pmp->pmp_sopnodereqs,
   1076  1.90      manu 					     psopr, psopr_entries);
   1077  1.90      manu 				break;
   1078  1.90      manu 			}
   1079  1.90      manu 
   1080  1.90      manu 			cv_timedwait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx, timeo);
   1081  1.90      manu 		} while (1 /* CONSTCOND */);
   1082  1.90      manu 
   1083  1.76     pooka 		mutex_exit(&pmp->pmp_sopmtx);
   1084  1.76     pooka 
   1085  1.76     pooka 		switch (psopr->psopr_sopreq) {
   1086  1.79     pooka 		case PUFFS_SOPREQSYS_EXIT:
   1087  1.76     pooka 			keeprunning = false;
   1088  1.76     pooka 			break;
   1089  1.76     pooka 		case PUFFS_SOPREQ_FLUSH:
   1090  1.76     pooka 			puffsop_flush(pmp, &psopr->psopr_pf);
   1091  1.76     pooka 			break;
   1092  1.90      manu 		case PUFFS_SOPREQ_EXPIRE:
   1093  1.90      manu 			puffsop_expire(pmp, psopr->psopr_ck);
   1094  1.90      manu 			break;
   1095  1.77     pooka 		case PUFFS_SOPREQ_UNMOUNT:
   1096  1.78     pooka 			puffs_msg_sendresp(pmp, &psopr->psopr_preq, 0);
   1097  1.77     pooka 
   1098  1.77     pooka 			unmountme = true;
   1099  1.77     pooka 			keeprunning = false;
   1100  1.77     pooka 
   1101  1.77     pooka 			/*
   1102  1.77     pooka 			 * We know the mountpoint is still alive because
   1103  1.77     pooka 			 * the thread that is us (poetic?) is still alive.
   1104  1.77     pooka 			 */
   1105  1.77     pooka 			atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
   1106  1.77     pooka 			break;
   1107  1.76     pooka 		}
   1108  1.76     pooka 
   1109  1.76     pooka 		kmem_free(psopr, sizeof(*psopr));
   1110  1.76     pooka 		mutex_enter(&pmp->pmp_sopmtx);
   1111  1.76     pooka 	}
   1112  1.76     pooka 
   1113  1.76     pooka 	/*
   1114  1.80     pooka 	 * Purge remaining ops.
   1115  1.76     pooka 	 */
   1116  1.90      manu 	while ((psopr = TAILQ_FIRST(&pmp->pmp_sopfastreqs)) != NULL) {
   1117  1.90      manu 		TAILQ_REMOVE(&pmp->pmp_sopfastreqs, psopr, psopr_entries);
   1118  1.90      manu 		mutex_exit(&pmp->pmp_sopmtx);
   1119  1.90      manu 		puffs_msg_sendresp(pmp, &psopr->psopr_preq, ENXIO);
   1120  1.90      manu 		kmem_free(psopr, sizeof(*psopr));
   1121  1.90      manu 		mutex_enter(&pmp->pmp_sopmtx);
   1122  1.90      manu 	}
   1123  1.90      manu 
   1124  1.92      manu 	while ((psopr = TAILQ_FIRST(&pmp->pmp_sopnodereqs)) != NULL) {
   1125  1.92      manu 		TAILQ_REMOVE(&pmp->pmp_sopnodereqs, psopr, psopr_entries);
   1126  1.76     pooka 		mutex_exit(&pmp->pmp_sopmtx);
   1127  1.92      manu 		KASSERT(psopr->psopr_sopreq == PUFFS_SOPREQ_EXPIRE);
   1128  1.76     pooka 		kmem_free(psopr, sizeof(*psopr));
   1129  1.76     pooka 		mutex_enter(&pmp->pmp_sopmtx);
   1130  1.76     pooka 	}
   1131  1.76     pooka 
   1132  1.76     pooka 	pmp->pmp_sopthrcount--;
   1133  1.77     pooka 	cv_broadcast(&pmp->pmp_sopcv);
   1134  1.76     pooka 	mutex_exit(&pmp->pmp_sopmtx); /* not allowed to access fs after this */
   1135  1.76     pooka 
   1136  1.77     pooka 	/*
   1137  1.77     pooka 	 * If unmount was requested, we can now safely do it here, since
   1138  1.77     pooka 	 * our context is dead from the point-of-view of puffs_unmount()
   1139  1.77     pooka 	 * and we are just another thread.  dounmount() makes internally
   1140  1.77     pooka 	 * sure that VFS_UNMOUNT() isn't called reentrantly and that it
   1141  1.77     pooka 	 * is eventually completed.
   1142  1.77     pooka 	 */
   1143  1.77     pooka 	if (unmountme) {
   1144  1.77     pooka 		(void)dounmount(mp, MNT_FORCE, curlwp);
   1145  1.77     pooka 		vfs_destroy(mp);
   1146  1.77     pooka 	}
   1147  1.77     pooka 
   1148  1.76     pooka 	kthread_exit(0);
   1149  1.53     pooka }
   1150  1.53     pooka 
   1151  1.53     pooka int
   1152  1.53     pooka puffs_msgif_close(void *this)
   1153  1.53     pooka {
   1154  1.53     pooka 	struct puffs_mount *pmp = this;
   1155  1.53     pooka 	struct mount *mp = PMPTOMP(pmp);
   1156  1.53     pooka 
   1157  1.53     pooka 	mutex_enter(&pmp->pmp_lock);
   1158  1.53     pooka 	puffs_mp_reference(pmp);
   1159  1.53     pooka 
   1160  1.53     pooka 	/*
   1161  1.53     pooka 	 * Free the waiting callers before proceeding any further.
   1162  1.53     pooka 	 * The syncer might be jogging around in this file system
   1163  1.53     pooka 	 * currently.  If we allow it to go to the userspace of no
   1164  1.53     pooka 	 * return while trying to get the syncer lock, well ...
   1165  1.53     pooka 	 */
   1166  1.53     pooka 	puffs_userdead(pmp);
   1167  1.53     pooka 
   1168  1.53     pooka 	/*
   1169  1.53     pooka 	 * Make sure someone from puffs_unmount() isn't currently in
   1170  1.53     pooka 	 * userspace.  If we don't take this precautionary step,
   1171  1.53     pooka 	 * they might notice that the mountpoint has disappeared
   1172  1.53     pooka 	 * from under them once they return.  Especially note that we
   1173  1.53     pooka 	 * cannot simply test for an unmounter before calling
   1174  1.53     pooka 	 * dounmount(), since it might be possible that that particular
   1175  1.53     pooka 	 * invocation of unmount was called without MNT_FORCE.  Here we
   1176  1.53     pooka 	 * *must* make sure unmount succeeds.  Also, restart is necessary
   1177  1.53     pooka 	 * since pmp isn't locked.  We might end up with PUTTER_DEAD after
   1178  1.53     pooka 	 * restart and exit from there.
   1179  1.53     pooka 	 */
   1180  1.53     pooka 	if (pmp->pmp_unmounting) {
   1181  1.53     pooka 		cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
   1182  1.53     pooka 		puffs_mp_release(pmp);
   1183  1.53     pooka 		mutex_exit(&pmp->pmp_lock);
   1184  1.53     pooka 		DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
   1185  1.53     pooka 		    "restart\n", pmp));
   1186  1.53     pooka 		return ERESTART;
   1187  1.53     pooka 	}
   1188  1.53     pooka 
   1189  1.53     pooka 	/* Won't access pmp from here anymore */
   1190  1.72        ad 	atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
   1191  1.53     pooka 	puffs_mp_release(pmp);
   1192  1.53     pooka 	mutex_exit(&pmp->pmp_lock);
   1193  1.53     pooka 
   1194  1.72        ad 	/* Detach from VFS. */
   1195  1.71        ad 	(void)dounmount(mp, MNT_FORCE, curlwp);
   1196  1.72        ad 	vfs_destroy(mp);
   1197  1.53     pooka 
   1198  1.53     pooka 	return 0;
   1199  1.53     pooka }
   1200  1.53     pooka 
   1201  1.53     pooka /*
   1202  1.22     pooka  * We're dead, kaput, RIP, slightly more than merely pining for the
   1203  1.22     pooka  * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
   1204  1.22     pooka  * our maker, ceased to be, etcetc.  YASD.  It's a dead FS!
   1205  1.22     pooka  *
   1206  1.22     pooka  * Caller must hold puffs mutex.
   1207  1.22     pooka  */
   1208  1.22     pooka void
   1209  1.22     pooka puffs_userdead(struct puffs_mount *pmp)
   1210  1.22     pooka {
   1211  1.46     pooka 	struct puffs_msgpark *park, *park_next;
   1212  1.22     pooka 
   1213  1.22     pooka 	/*
   1214  1.22     pooka 	 * Mark filesystem status as dying so that operations don't
   1215  1.22     pooka 	 * attempt to march to userspace any longer.
   1216  1.22     pooka 	 */
   1217  1.22     pooka 	pmp->pmp_status = PUFFSTAT_DYING;
   1218  1.22     pooka 
   1219  1.22     pooka 	/* signal waiters on REQUEST TO file server queue */
   1220  1.46     pooka 	for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
   1221  1.24     pooka 		uint8_t opclass;
   1222  1.24     pooka 
   1223  1.46     pooka 		mutex_enter(&park->park_mtx);
   1224  1.46     pooka 		puffs_msgpark_reference(park);
   1225  1.32     pooka 		park_next = TAILQ_NEXT(park, park_entries);
   1226  1.26     pooka 
   1227  1.26     pooka 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
   1228  1.46     pooka 		TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
   1229  1.26     pooka 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
   1230  1.46     pooka 		pmp->pmp_msg_touser_count--;
   1231  1.22     pooka 
   1232  1.31     pooka 		/*
   1233  1.51     pooka 		 * Even though waiters on QUEUE1 are removed in touser()
   1234  1.51     pooka 		 * in case of WAITERGONE, it is still possible for us to
   1235  1.51     pooka 		 * get raced here due to having to retake locks in said
   1236  1.51     pooka 		 * touser().  In the race case simply "ignore" the item
   1237  1.51     pooka 		 * on the queue and move on to the next one.
   1238  1.31     pooka 		 */
   1239  1.31     pooka 		if (park->park_flags & PARKFLAG_WAITERGONE) {
   1240  1.31     pooka 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
   1241  1.31     pooka 			KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
   1242  1.46     pooka 			puffs_msgpark_release(park);
   1243  1.51     pooka 
   1244  1.22     pooka 		} else {
   1245  1.31     pooka 			opclass = park->park_preq->preq_opclass;
   1246  1.23     pooka 			park->park_preq->preq_rv = ENXIO;
   1247  1.31     pooka 
   1248  1.31     pooka 			if (park->park_flags & PARKFLAG_CALL) {
   1249  1.42     pooka 				park->park_done(pmp, park->park_preq,
   1250  1.31     pooka 				    park->park_donearg);
   1251  1.46     pooka 				puffs_msgpark_release1(park, 2);
   1252  1.31     pooka 			} else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
   1253  1.46     pooka 				puffs_msgpark_release1(park, 2);
   1254  1.31     pooka 			} else {
   1255  1.31     pooka 				park->park_preq->preq_rv = ENXIO;
   1256  1.31     pooka 				cv_signal(&park->park_cv);
   1257  1.46     pooka 				puffs_msgpark_release(park);
   1258  1.31     pooka 			}
   1259  1.22     pooka 		}
   1260  1.22     pooka 	}
   1261  1.22     pooka 
   1262  1.22     pooka 	/* signal waiters on RESPONSE FROM file server queue */
   1263  1.46     pooka 	for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
   1264  1.46     pooka 		mutex_enter(&park->park_mtx);
   1265  1.46     pooka 		puffs_msgpark_reference(park);
   1266  1.32     pooka 		park_next = TAILQ_NEXT(park, park_entries);
   1267  1.26     pooka 
   1268  1.26     pooka 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
   1269  1.31     pooka 		KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
   1270  1.26     pooka 
   1271  1.46     pooka 		TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
   1272  1.26     pooka 		park->park_flags &= ~PARKFLAG_ONQUEUE2;
   1273  1.26     pooka 
   1274  1.31     pooka 		if (park->park_flags & PARKFLAG_WAITERGONE) {
   1275  1.31     pooka 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
   1276  1.46     pooka 			puffs_msgpark_release(park);
   1277  1.22     pooka 		} else {
   1278  1.31     pooka 			park->park_preq->preq_rv = ENXIO;
   1279  1.31     pooka 			if (park->park_flags & PARKFLAG_CALL) {
   1280  1.42     pooka 				park->park_done(pmp, park->park_preq,
   1281  1.31     pooka 				    park->park_donearg);
   1282  1.46     pooka 				puffs_msgpark_release1(park, 2);
   1283  1.31     pooka 			} else {
   1284  1.31     pooka 				cv_signal(&park->park_cv);
   1285  1.46     pooka 				puffs_msgpark_release(park);
   1286  1.31     pooka 			}
   1287  1.22     pooka 		}
   1288  1.22     pooka 	}
   1289  1.50     pooka 
   1290  1.50     pooka 	cv_broadcast(&pmp->pmp_msg_waiter_cv);
   1291  1.22     pooka }
   1292