Home | History | Annotate | Line # | Download | only in puffs
puffs_msgif.c revision 1.75
      1  1.75   pooka /*	$NetBSD: puffs_msgif.c,v 1.75 2009/12/07 15:51:52 pooka Exp $	*/
      2   1.1   pooka 
      3   1.1   pooka /*
      4  1.16   pooka  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
      5   1.1   pooka  *
      6   1.1   pooka  * Development of this software was supported by the
      7   1.1   pooka  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8   1.1   pooka  * The Google SoC project was mentored by Bill Studenmund.
      9   1.1   pooka  *
     10   1.1   pooka  * Redistribution and use in source and binary forms, with or without
     11   1.1   pooka  * modification, are permitted provided that the following conditions
     12   1.1   pooka  * are met:
     13   1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     14   1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     15   1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   pooka  *    documentation and/or other materials provided with the distribution.
     18   1.1   pooka  *
     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.75   pooka __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.75 2009/12/07 15:51:52 pooka Exp $");
     34   1.1   pooka 
     35   1.1   pooka #include <sys/param.h>
     36  1.68     tnn #include <sys/atomic.h>
     37  1.46   pooka #include <sys/kmem.h>
     38  1.53   pooka #include <sys/kthread.h>
     39  1.53   pooka #include <sys/lock.h>
     40   1.1   pooka #include <sys/malloc.h>
     41   1.1   pooka #include <sys/mount.h>
     42  1.53   pooka #include <sys/namei.h>
     43  1.53   pooka #include <sys/proc.h>
     44   1.1   pooka #include <sys/vnode.h>
     45  1.71      ad #include <sys/atomic.h>
     46  1.53   pooka 
     47  1.55   pooka #include <dev/putter/putter_sys.h>
     48   1.1   pooka 
     49   1.1   pooka #include <fs/puffs/puffs_msgif.h>
     50   1.1   pooka #include <fs/puffs/puffs_sys.h>
     51   1.1   pooka 
     52  1.53   pooka #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
     53  1.53   pooka 
     54  1.22   pooka /*
     55  1.22   pooka  * waitq data structures
     56  1.22   pooka  */
     57  1.22   pooka 
     58  1.22   pooka /*
     59  1.22   pooka  * While a request is going to userspace, park the caller within the
     60  1.22   pooka  * kernel.  This is the kernel counterpart of "struct puffs_req".
     61  1.22   pooka  */
     62  1.46   pooka struct puffs_msgpark {
     63  1.22   pooka 	struct puffs_req	*park_preq;	/* req followed by buf	*/
     64  1.22   pooka 
     65  1.22   pooka 	size_t			park_copylen;	/* userspace copylength	*/
     66  1.22   pooka 	size_t			park_maxlen;	/* max size in comeback */
     67  1.24   pooka 
     68  1.46   pooka 	parkdone_fn		park_done;	/* "biodone" a'la puffs	*/
     69  1.24   pooka 	void			*park_donearg;
     70  1.22   pooka 
     71  1.22   pooka 	int			park_flags;
     72  1.26   pooka 	int			park_refcount;
     73  1.22   pooka 
     74  1.22   pooka 	kcondvar_t		park_cv;
     75  1.26   pooka 	kmutex_t		park_mtx;
     76  1.26   pooka 
     77  1.46   pooka 	TAILQ_ENTRY(puffs_msgpark) park_entries;
     78  1.22   pooka };
     79  1.22   pooka #define PARKFLAG_WAITERGONE	0x01
     80  1.26   pooka #define PARKFLAG_DONE		0x02
     81  1.26   pooka #define PARKFLAG_ONQUEUE1	0x04
     82  1.26   pooka #define PARKFLAG_ONQUEUE2	0x08
     83  1.26   pooka #define PARKFLAG_CALL		0x10
     84  1.31   pooka #define PARKFLAG_WANTREPLY	0x20
     85  1.57   pooka #define	PARKFLAG_HASERROR	0x40
     86  1.22   pooka 
     87  1.52      ad static pool_cache_t parkpc;
     88  1.57   pooka #ifdef PUFFSDEBUG
     89  1.57   pooka static int totalpark;
     90  1.57   pooka #endif
     91  1.22   pooka 
     92  1.22   pooka static int
     93  1.22   pooka makepark(void *arg, void *obj, int flags)
     94  1.22   pooka {
     95  1.46   pooka 	struct puffs_msgpark *park = obj;
     96  1.22   pooka 
     97  1.26   pooka 	mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
     98  1.22   pooka 	cv_init(&park->park_cv, "puffsrpl");
     99  1.22   pooka 
    100  1.22   pooka 	return 0;
    101  1.22   pooka }
    102  1.22   pooka 
    103  1.22   pooka static void
    104  1.22   pooka nukepark(void *arg, void *obj)
    105  1.22   pooka {
    106  1.46   pooka 	struct puffs_msgpark *park = obj;
    107  1.22   pooka 
    108  1.22   pooka 	cv_destroy(&park->park_cv);
    109  1.26   pooka 	mutex_destroy(&park->park_mtx);
    110  1.22   pooka }
    111  1.22   pooka 
    112  1.22   pooka void
    113  1.73  cegger puffs_msgif_init(void)
    114  1.22   pooka {
    115  1.22   pooka 
    116  1.52      ad 	parkpc = pool_cache_init(sizeof(struct puffs_msgpark), 0, 0, 0,
    117  1.52      ad 	    "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
    118  1.22   pooka }
    119  1.22   pooka 
    120  1.22   pooka void
    121  1.73  cegger puffs_msgif_destroy(void)
    122  1.22   pooka {
    123  1.22   pooka 
    124  1.52      ad 	pool_cache_destroy(parkpc);
    125  1.22   pooka }
    126  1.22   pooka 
    127  1.46   pooka static int alloced;
    128  1.46   pooka 
    129  1.46   pooka static struct puffs_msgpark *
    130  1.46   pooka puffs_msgpark_alloc(int waitok)
    131  1.26   pooka {
    132  1.46   pooka 	struct puffs_msgpark *park;
    133  1.26   pooka 
    134  1.52      ad 	park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
    135  1.46   pooka 	if (park == NULL)
    136  1.46   pooka 		return park;
    137  1.46   pooka 
    138  1.46   pooka 	park->park_refcount = 1;
    139  1.46   pooka 	park->park_preq = NULL;
    140  1.46   pooka 	park->park_flags = PARKFLAG_WANTREPLY;
    141  1.26   pooka 
    142  1.57   pooka #ifdef PUFFSDEBUG
    143  1.57   pooka 	totalpark++;
    144  1.57   pooka #endif
    145  1.57   pooka 
    146  1.26   pooka 	return park;
    147  1.26   pooka }
    148  1.26   pooka 
    149  1.26   pooka static void
    150  1.46   pooka puffs_msgpark_reference(struct puffs_msgpark *park)
    151  1.22   pooka {
    152  1.22   pooka 
    153  1.46   pooka 	KASSERT(mutex_owned(&park->park_mtx));
    154  1.26   pooka 	park->park_refcount++;
    155  1.22   pooka }
    156  1.22   pooka 
    157  1.46   pooka /*
    158  1.46   pooka  * Release reference to park structure.
    159  1.46   pooka  */
    160  1.46   pooka static void
    161  1.46   pooka puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
    162  1.22   pooka {
    163  1.46   pooka 	struct puffs_req *preq = park->park_preq;
    164  1.46   pooka 	int refcnt;
    165  1.22   pooka 
    166  1.26   pooka 	KASSERT(mutex_owned(&park->park_mtx));
    167  1.46   pooka 	refcnt = park->park_refcount -= howmany;
    168  1.46   pooka 	mutex_exit(&park->park_mtx);
    169  1.46   pooka 
    170  1.46   pooka 	KASSERT(refcnt >= 0);
    171  1.26   pooka 
    172  1.46   pooka 	if (refcnt == 0) {
    173  1.46   pooka 		alloced--;
    174  1.46   pooka 		if (preq)
    175  1.46   pooka 			kmem_free(preq, park->park_maxlen);
    176  1.52      ad 		pool_cache_put(parkpc, park);
    177  1.57   pooka 
    178  1.57   pooka #ifdef PUFFSDEBUG
    179  1.57   pooka 		totalpark--;
    180  1.57   pooka #endif
    181  1.46   pooka 	}
    182  1.22   pooka }
    183  1.46   pooka #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
    184  1.22   pooka 
    185  1.26   pooka #ifdef PUFFSDEBUG
    186  1.26   pooka static void
    187  1.46   pooka parkdump(struct puffs_msgpark *park)
    188  1.26   pooka {
    189  1.26   pooka 
    190  1.26   pooka 	DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
    191  1.26   pooka 	    "\tcopy %zu, max %zu - done: %p/%p\n"
    192  1.26   pooka 	    "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
    193  1.46   pooka 	    park, park->park_preq, park->park_preq->preq_id,
    194  1.26   pooka 	    park->park_copylen, park->park_maxlen,
    195  1.26   pooka 	    park->park_done, park->park_donearg,
    196  1.26   pooka 	    park->park_flags, park->park_refcount,
    197  1.26   pooka 	    &park->park_cv, &park->park_mtx));
    198  1.26   pooka }
    199  1.26   pooka 
    200  1.26   pooka static void
    201  1.26   pooka parkqdump(struct puffs_wq *q, int dumpall)
    202  1.26   pooka {
    203  1.46   pooka 	struct puffs_msgpark *park;
    204  1.26   pooka 	int total = 0;
    205  1.26   pooka 
    206  1.26   pooka 	TAILQ_FOREACH(park, q, park_entries) {
    207  1.26   pooka 		if (dumpall)
    208  1.26   pooka 			parkdump(park);
    209  1.26   pooka 		total++;
    210  1.26   pooka 	}
    211  1.29   pooka 	DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
    212  1.26   pooka 
    213  1.26   pooka }
    214  1.26   pooka #endif /* PUFFSDEBUG */
    215  1.22   pooka 
    216  1.22   pooka /*
    217  1.46   pooka  * A word about locking in the park structures: the lock protects the
    218  1.46   pooka  * fields of the *park* structure (not preq) and acts as an interlock
    219  1.46   pooka  * in cv operations.  The lock is always internal to this module and
    220  1.46   pooka  * callers do not need to worry about it.
    221  1.22   pooka  */
    222  1.46   pooka 
    223  1.46   pooka int
    224  1.46   pooka puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
    225  1.46   pooka 	int cansleep)
    226  1.46   pooka {
    227  1.46   pooka 	struct puffs_msgpark *park;
    228  1.46   pooka 	void *m;
    229  1.46   pooka 
    230  1.46   pooka 	m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
    231  1.46   pooka 	if (m == NULL) {
    232  1.46   pooka 		KASSERT(cansleep == 0);
    233  1.46   pooka 		return ENOMEM;
    234  1.46   pooka 	}
    235  1.46   pooka 
    236  1.46   pooka 	park = puffs_msgpark_alloc(cansleep);
    237  1.46   pooka 	if (park == NULL) {
    238  1.46   pooka 		KASSERT(cansleep == 0);
    239  1.46   pooka 		kmem_free(m, len);
    240  1.46   pooka 		return ENOMEM;
    241  1.46   pooka 	}
    242  1.46   pooka 
    243  1.46   pooka 	park->park_preq = m;
    244  1.57   pooka 	park->park_maxlen = park->park_copylen = len;
    245  1.46   pooka 
    246  1.46   pooka 	*ppark = park;
    247  1.46   pooka 	*mem = m;
    248  1.46   pooka 
    249  1.46   pooka 	return 0;
    250  1.46   pooka }
    251  1.46   pooka 
    252  1.46   pooka void
    253  1.46   pooka puffs_msgmem_release(struct puffs_msgpark *park)
    254  1.22   pooka {
    255  1.22   pooka 
    256  1.46   pooka 	if (park == NULL)
    257  1.46   pooka 		return;
    258  1.22   pooka 
    259  1.46   pooka 	mutex_enter(&park->park_mtx);
    260  1.46   pooka 	puffs_msgpark_release(park);
    261  1.46   pooka }
    262  1.22   pooka 
    263  1.46   pooka void
    264  1.46   pooka puffs_msg_setfaf(struct puffs_msgpark *park)
    265  1.46   pooka {
    266  1.22   pooka 
    267  1.57   pooka 	KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    268  1.36   pooka 	park->park_flags &= ~PARKFLAG_WANTREPLY;
    269  1.22   pooka }
    270  1.22   pooka 
    271  1.57   pooka void
    272  1.57   pooka puffs_msg_setdelta(struct puffs_msgpark *park, size_t delta)
    273   1.1   pooka {
    274   1.1   pooka 
    275  1.57   pooka 	KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
    276  1.57   pooka 	park->park_copylen = park->park_maxlen - delta;
    277   1.1   pooka }
    278   1.1   pooka 
    279  1.57   pooka void
    280  1.64   pooka puffs_msg_setinfo(struct puffs_msgpark *park, int class, int type,
    281  1.64   pooka 	puffs_cookie_t ck)
    282   1.1   pooka {
    283   1.1   pooka 
    284  1.57   pooka 	park->park_preq->preq_opclass = PUFFSOP_OPCLASS(class);
    285  1.57   pooka 	park->park_preq->preq_optype = type;
    286  1.64   pooka 	park->park_preq->preq_cookie = ck;
    287   1.1   pooka }
    288   1.1   pooka 
    289  1.24   pooka void
    290  1.57   pooka puffs_msg_setcall(struct puffs_msgpark *park, parkdone_fn donefn, void *donearg)
    291   1.1   pooka {
    292  1.25   pooka 
    293  1.57   pooka 	KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    294  1.25   pooka 	park->park_done = donefn;
    295  1.25   pooka 	park->park_donearg = donearg;
    296  1.46   pooka 	park->park_flags |= PARKFLAG_CALL;
    297  1.20   pooka }
    298  1.20   pooka 
    299  1.57   pooka /*
    300  1.57   pooka  * kernel-user-kernel waitqueues
    301  1.57   pooka  */
    302   1.4   pooka 
    303  1.57   pooka static uint64_t
    304  1.57   pooka puffs_getmsgid(struct puffs_mount *pmp)
    305  1.41   pooka {
    306  1.57   pooka 	uint64_t rv;
    307  1.41   pooka 
    308  1.57   pooka 	mutex_enter(&pmp->pmp_lock);
    309  1.57   pooka 	rv = pmp->pmp_nextmsgid++;
    310  1.57   pooka 	mutex_exit(&pmp->pmp_lock);
    311  1.41   pooka 
    312  1.57   pooka 	return rv;
    313  1.41   pooka }
    314  1.41   pooka 
    315   1.4   pooka /*
    316  1.57   pooka  * A word about reference counting of parks.  A reference must be taken
    317  1.57   pooka  * when accessing a park and additionally when it is on a queue.  So
    318  1.57   pooka  * when taking it off a queue and releasing the access reference, the
    319  1.57   pooka  * reference count is generally decremented by 2.
    320   1.1   pooka  */
    321  1.57   pooka 
    322  1.57   pooka void
    323  1.57   pooka puffs_msg_enqueue(struct puffs_mount *pmp, struct puffs_msgpark *park)
    324   1.1   pooka {
    325  1.26   pooka 	struct lwp *l = curlwp;
    326  1.16   pooka 	struct mount *mp;
    327   1.9   pooka 	struct puffs_req *preq;
    328   1.1   pooka 
    329  1.16   pooka 	mp = PMPTOMP(pmp);
    330  1.25   pooka 	preq = park->park_preq;
    331  1.46   pooka 	preq->preq_buflen = park->park_maxlen;
    332  1.61   pooka 	KASSERT(preq->preq_id == 0
    333  1.61   pooka 	    || (preq->preq_opclass & PUFFSOPFLAG_ISRESPONSE));
    334  1.46   pooka 
    335  1.46   pooka 	if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
    336  1.46   pooka 		preq->preq_opclass |= PUFFSOPFLAG_FAF;
    337  1.46   pooka 	else
    338  1.46   pooka 		preq->preq_id = puffs_getmsgid(pmp);
    339  1.31   pooka 
    340  1.49   pooka 	/* fill in caller information */
    341  1.49   pooka 	preq->preq_pid = l->l_proc->p_pid;
    342  1.49   pooka 	preq->preq_lid = l->l_lid;
    343  1.49   pooka 
    344  1.19   pooka 	/*
    345  1.51   pooka 	 * To support cv_sig, yet another movie: check if there are signals
    346  1.19   pooka 	 * pending and we are issueing a non-FAF.  If so, return an error
    347  1.57   pooka 	 * directly UNLESS we are issueing INACTIVE/RECLAIM.  In that case,
    348  1.57   pooka 	 * convert it to a FAF, fire off to the file server and return
    349  1.57   pooka 	 * an error.  Yes, this is bordering disgusting.  Barfbags are on me.
    350  1.19   pooka 	 */
    351  1.57   pooka 	if (__predict_false((park->park_flags & PARKFLAG_WANTREPLY)
    352  1.25   pooka 	   && (park->park_flags & PARKFLAG_CALL) == 0
    353  1.57   pooka 	   && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))) {
    354  1.57   pooka 		park->park_flags |= PARKFLAG_HASERROR;
    355  1.57   pooka 		preq->preq_rv = EINTR;
    356  1.19   pooka 		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
    357  1.57   pooka 		    && (preq->preq_optype == PUFFS_VN_INACTIVE
    358  1.57   pooka 		     || preq->preq_optype == PUFFS_VN_RECLAIM)) {
    359  1.46   pooka 			park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
    360  1.46   pooka 			park->park_flags &= ~PARKFLAG_WANTREPLY;
    361  1.57   pooka 			DPRINTF(("puffs_msg_enqueue: converted to FAF %p\n",
    362  1.57   pooka 			    park));
    363  1.19   pooka 		} else {
    364  1.57   pooka 			return;
    365  1.19   pooka 		}
    366  1.19   pooka 	}
    367  1.16   pooka 
    368  1.22   pooka 	mutex_enter(&pmp->pmp_lock);
    369  1.13   pooka 	if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    370  1.22   pooka 		mutex_exit(&pmp->pmp_lock);
    371  1.57   pooka 		park->park_flags |= PARKFLAG_HASERROR;
    372  1.57   pooka 		preq->preq_rv = ENXIO;
    373  1.57   pooka 		return;
    374   1.1   pooka 	}
    375   1.1   pooka 
    376  1.26   pooka #ifdef PUFFSDEBUG
    377  1.46   pooka 	parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
    378  1.46   pooka 	parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
    379  1.26   pooka #endif
    380  1.26   pooka 
    381  1.57   pooka 	/*
    382  1.57   pooka 	 * Note: we don't need to lock park since we have the only
    383  1.57   pooka 	 * reference to it at this point.
    384  1.57   pooka 	 */
    385  1.46   pooka 	TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
    386  1.26   pooka 	park->park_flags |= PARKFLAG_ONQUEUE1;
    387  1.46   pooka 	pmp->pmp_msg_touser_count++;
    388  1.57   pooka 	park->park_refcount++;
    389  1.26   pooka 	mutex_exit(&pmp->pmp_lock);
    390   1.1   pooka 
    391  1.57   pooka 	cv_broadcast(&pmp->pmp_msg_waiter_cv);
    392  1.57   pooka 	putter_notify(pmp->pmp_pi);
    393  1.57   pooka 
    394  1.20   pooka 	DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
    395  1.25   pooka 	    "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
    396  1.25   pooka 	    preq->preq_opclass, preq->preq_optype, park->park_flags));
    397  1.57   pooka }
    398  1.15   pooka 
    399  1.57   pooka int
    400  1.57   pooka puffs_msg_wait(struct puffs_mount *pmp, struct puffs_msgpark *park)
    401  1.57   pooka {
    402  1.57   pooka 	struct puffs_req *preq = park->park_preq; /* XXX: hmmm */
    403  1.57   pooka 	int error = 0;
    404  1.57   pooka 	int rv;
    405   1.1   pooka 
    406  1.57   pooka 	mutex_enter(&pmp->pmp_lock);
    407  1.57   pooka 	puffs_mp_reference(pmp);
    408  1.57   pooka 	mutex_exit(&pmp->pmp_lock);
    409  1.46   pooka 
    410  1.57   pooka 	mutex_enter(&park->park_mtx);
    411  1.57   pooka 	if ((park->park_flags & PARKFLAG_WANTREPLY) == 0
    412  1.57   pooka 	    || (park->park_flags & PARKFLAG_CALL)) {
    413  1.57   pooka 		mutex_exit(&park->park_mtx);
    414  1.57   pooka 		rv = 0;
    415  1.57   pooka 		goto skipwait;
    416  1.57   pooka 	}
    417  1.46   pooka 
    418  1.57   pooka 	/* did the response beat us to the wait? */
    419  1.57   pooka 	if (__predict_false((park->park_flags & PARKFLAG_DONE)
    420  1.57   pooka 	    || (park->park_flags & PARKFLAG_HASERROR))) {
    421  1.57   pooka 		rv = park->park_preq->preq_rv;
    422  1.57   pooka 		mutex_exit(&park->park_mtx);
    423  1.57   pooka 		goto skipwait;
    424  1.57   pooka 	}
    425  1.26   pooka 
    426  1.57   pooka 	error = cv_wait_sig(&park->park_cv, &park->park_mtx);
    427  1.57   pooka 	DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
    428  1.57   pooka 	    park, error));
    429  1.57   pooka 	if (error) {
    430  1.57   pooka 		park->park_flags |= PARKFLAG_WAITERGONE;
    431  1.57   pooka 		if (park->park_flags & PARKFLAG_DONE) {
    432  1.57   pooka 			rv = preq->preq_rv;
    433  1.57   pooka 			mutex_exit(&park->park_mtx);
    434  1.57   pooka 		} else {
    435  1.57   pooka 			/*
    436  1.57   pooka 			 * ok, we marked it as going away, but
    437  1.57   pooka 			 * still need to do queue ops.  take locks
    438  1.57   pooka 			 * in correct order.
    439  1.57   pooka 			 *
    440  1.57   pooka 			 * We don't want to release our reference
    441  1.57   pooka 			 * if it's on replywait queue to avoid error
    442  1.57   pooka 			 * to file server.  putop() code will DTRT.
    443  1.57   pooka 			 */
    444  1.57   pooka 			mutex_exit(&park->park_mtx);
    445  1.57   pooka 			mutex_enter(&pmp->pmp_lock);
    446  1.57   pooka 			mutex_enter(&park->park_mtx);
    447  1.57   pooka 
    448  1.57   pooka 			/*
    449  1.57   pooka 			 * Still on queue1?  We can safely remove it
    450  1.57   pooka 			 * without any consequences since the file
    451  1.57   pooka 			 * server hasn't seen it.  "else" we need to
    452  1.57   pooka 			 * wait for the response and just ignore it
    453  1.57   pooka 			 * to avoid signalling an incorrect error to
    454  1.57   pooka 			 * the file server.
    455  1.57   pooka 			 */
    456  1.57   pooka 			if (park->park_flags & PARKFLAG_ONQUEUE1) {
    457  1.57   pooka 				TAILQ_REMOVE(&pmp->pmp_msg_touser,
    458  1.57   pooka 				    park, park_entries);
    459  1.57   pooka 				puffs_msgpark_release(park);
    460  1.57   pooka 				pmp->pmp_msg_touser_count--;
    461  1.57   pooka 				park->park_flags &= ~PARKFLAG_ONQUEUE1;
    462  1.57   pooka 			} else {
    463  1.57   pooka 				mutex_exit(&park->park_mtx);
    464  1.19   pooka 			}
    465  1.57   pooka 			mutex_exit(&pmp->pmp_lock);
    466  1.22   pooka 
    467  1.60   pooka 			rv = EINTR;
    468  1.16   pooka 		}
    469  1.22   pooka 	} else {
    470  1.57   pooka 		rv = preq->preq_rv;
    471  1.57   pooka 		mutex_exit(&park->park_mtx);
    472  1.16   pooka 	}
    473  1.16   pooka 
    474  1.57   pooka  skipwait:
    475  1.22   pooka 	mutex_enter(&pmp->pmp_lock);
    476  1.34   pooka 	puffs_mp_release(pmp);
    477  1.22   pooka 	mutex_exit(&pmp->pmp_lock);
    478  1.16   pooka 
    479  1.19   pooka 	return rv;
    480   1.1   pooka }
    481   1.1   pooka 
    482   1.9   pooka /*
    483  1.57   pooka  * XXX: this suuuucks.  Hopefully I'll get rid of this lossage once
    484  1.57   pooka  * the whole setback-nonsense gets fixed.
    485  1.57   pooka  */
    486  1.57   pooka int
    487  1.57   pooka puffs_msg_wait2(struct puffs_mount *pmp, struct puffs_msgpark *park,
    488  1.57   pooka 	struct puffs_node *pn1, struct puffs_node *pn2)
    489  1.57   pooka {
    490  1.57   pooka 	struct puffs_req *preq;
    491  1.57   pooka 	int rv;
    492  1.57   pooka 
    493  1.57   pooka 	rv = puffs_msg_wait(pmp, park);
    494  1.57   pooka 
    495  1.57   pooka 	preq = park->park_preq;
    496  1.57   pooka 	if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N1)
    497  1.57   pooka 		pn1->pn_stat |= PNODE_DOINACT;
    498  1.57   pooka 	if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N2)
    499  1.57   pooka 		pn2->pn_stat |= PNODE_DOINACT;
    500  1.57   pooka 
    501  1.57   pooka 	if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1)
    502  1.57   pooka 		pn1->pn_stat |= PNODE_NOREFS;
    503  1.57   pooka 	if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2)
    504  1.57   pooka 		pn2->pn_stat |= PNODE_NOREFS;
    505  1.57   pooka 
    506  1.57   pooka 	return rv;
    507  1.57   pooka 
    508  1.57   pooka }
    509  1.57   pooka 
    510  1.57   pooka /*
    511  1.61   pooka  * XXX: lazy bum.  please, for the love of foie gras, fix me.
    512  1.61   pooka  * This should *NOT* depend on setfaf.  Also "memcpy" could
    513  1.61   pooka  * be done more nicely.
    514  1.61   pooka  */
    515  1.61   pooka void
    516  1.61   pooka puffs_msg_sendresp(struct puffs_mount *pmp, struct puffs_req *origpreq, int rv)
    517  1.61   pooka {
    518  1.61   pooka 	struct puffs_msgpark *park;
    519  1.61   pooka 	struct puffs_req *preq;
    520  1.61   pooka 
    521  1.63   pooka 	puffs_msgmem_alloc(sizeof(struct puffs_req), &park, (void *)&preq, 1);
    522  1.61   pooka 	puffs_msg_setfaf(park); /* XXXXXX: avoids reqid override */
    523  1.61   pooka 
    524  1.61   pooka 	memcpy(preq, origpreq, sizeof(struct puffs_req));
    525  1.61   pooka 	preq->preq_rv = rv;
    526  1.61   pooka 	preq->preq_opclass |= PUFFSOPFLAG_ISRESPONSE;
    527  1.61   pooka 
    528  1.61   pooka 	puffs_msg_enqueue(pmp, park);
    529  1.61   pooka 	puffs_msgmem_release(park);
    530  1.61   pooka }
    531  1.61   pooka 
    532  1.61   pooka /*
    533  1.46   pooka  * Get next request in the outgoing queue.  "maxsize" controls the
    534  1.46   pooka  * size the caller can accommodate and "nonblock" signals if this
    535  1.46   pooka  * should block while waiting for input.  Handles all locking internally.
    536   1.9   pooka  */
    537  1.10   pooka int
    538  1.46   pooka puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
    539  1.46   pooka 	uint8_t **data, size_t *dlen, void **parkptr)
    540   1.1   pooka {
    541  1.46   pooka 	struct puffs_mount *pmp = this;
    542  1.46   pooka 	struct puffs_msgpark *park;
    543   1.9   pooka 	struct puffs_req *preq;
    544  1.46   pooka 	int error;
    545   1.1   pooka 
    546  1.46   pooka 	error = 0;
    547  1.22   pooka 	mutex_enter(&pmp->pmp_lock);
    548  1.50   pooka 	puffs_mp_reference(pmp);
    549  1.46   pooka 	for (;;) {
    550  1.46   pooka 		/* RIP? */
    551   1.9   pooka 		if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    552   1.9   pooka 			error = ENXIO;
    553  1.46   pooka 			break;
    554   1.9   pooka 		}
    555  1.12   pooka 
    556  1.46   pooka 		/* need platinum yendorian express card? */
    557  1.46   pooka 		if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
    558  1.46   pooka 			DPRINTF(("puffs_getout: no outgoing op, "));
    559  1.12   pooka 			if (nonblock) {
    560  1.46   pooka 				DPRINTF(("returning EWOULDBLOCK\n"));
    561  1.12   pooka 				error = EWOULDBLOCK;
    562  1.46   pooka 				break;
    563   1.9   pooka 			}
    564  1.46   pooka 			DPRINTF(("waiting ...\n"));
    565   1.9   pooka 
    566  1.46   pooka 			error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
    567  1.22   pooka 			    &pmp->pmp_lock);
    568  1.11   pooka 			if (error)
    569  1.46   pooka 				break;
    570  1.11   pooka 			else
    571  1.46   pooka 				continue;
    572   1.9   pooka 		}
    573   1.9   pooka 
    574  1.46   pooka 		park = TAILQ_FIRST(&pmp->pmp_msg_touser);
    575  1.50   pooka 		if (park == NULL)
    576  1.50   pooka 			continue;
    577  1.50   pooka 
    578  1.46   pooka 		mutex_enter(&park->park_mtx);
    579  1.46   pooka 		puffs_msgpark_reference(park);
    580  1.46   pooka 
    581  1.46   pooka 		DPRINTF(("puffs_getout: found park at %p, ", park));
    582  1.22   pooka 
    583  1.22   pooka 		/* If it's a goner, don't process any furher */
    584  1.22   pooka 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    585  1.46   pooka 			DPRINTF(("waitergone!\n"));
    586  1.46   pooka 			puffs_msgpark_release(park);
    587  1.22   pooka 			continue;
    588  1.22   pooka 		}
    589  1.55   pooka 		preq = park->park_preq;
    590  1.22   pooka 
    591  1.55   pooka #if 0
    592  1.46   pooka 		/* check size */
    593  1.55   pooka 		/*
    594  1.55   pooka 		 * XXX: this check is not valid for now, we don't know
    595  1.55   pooka 		 * the size of the caller's input buffer.  i.e. this
    596  1.55   pooka 		 * will most likely go away
    597  1.55   pooka 		 */
    598  1.46   pooka 		if (maxsize < preq->preq_frhdr.pfr_len) {
    599  1.46   pooka 			DPRINTF(("buffer too small\n"));
    600  1.46   pooka 			puffs_msgpark_release(park);
    601  1.46   pooka 			error = E2BIG;
    602  1.46   pooka 			break;
    603  1.26   pooka 		}
    604  1.55   pooka #endif
    605  1.28   pooka 
    606  1.46   pooka 		DPRINTF(("returning\n"));
    607  1.46   pooka 
    608  1.46   pooka 		/*
    609  1.46   pooka 		 * Ok, we found what we came for.  Release it from the
    610  1.46   pooka 		 * outgoing queue but do not unlock.  We will unlock
    611  1.46   pooka 		 * only after we "releaseout" it to avoid complications:
    612  1.46   pooka 		 * otherwise it is (theoretically) possible for userland
    613  1.46   pooka 		 * to race us into "put" before we have a change to put
    614  1.46   pooka 		 * this baby on the receiving queue.
    615  1.46   pooka 		 */
    616  1.46   pooka 		TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
    617  1.28   pooka 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    618  1.28   pooka 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    619  1.46   pooka 		mutex_exit(&park->park_mtx);
    620  1.46   pooka 
    621  1.46   pooka 		pmp->pmp_msg_touser_count--;
    622  1.46   pooka 		KASSERT(pmp->pmp_msg_touser_count >= 0);
    623  1.26   pooka 
    624  1.46   pooka 		break;
    625  1.46   pooka 	}
    626  1.50   pooka 	puffs_mp_release(pmp);
    627  1.46   pooka 	mutex_exit(&pmp->pmp_lock);
    628   1.9   pooka 
    629  1.46   pooka 	if (error == 0) {
    630  1.46   pooka 		*data = (uint8_t *)preq;
    631  1.55   pooka 		preq->preq_pth.pth_framelen = park->park_copylen;
    632  1.55   pooka 		*dlen = preq->preq_pth.pth_framelen;
    633  1.46   pooka 		*parkptr = park;
    634  1.46   pooka 	}
    635  1.51   pooka 
    636  1.46   pooka 	return error;
    637  1.46   pooka }
    638   1.9   pooka 
    639  1.46   pooka /*
    640  1.46   pooka  * Release outgoing structure.  Now, depending on the success of the
    641  1.46   pooka  * outgoing send, it is either going onto the result waiting queue
    642  1.46   pooka  * or the death chamber.
    643  1.46   pooka  */
    644  1.46   pooka void
    645  1.46   pooka puffs_msgif_releaseout(void *this, void *parkptr, int status)
    646  1.46   pooka {
    647  1.46   pooka 	struct puffs_mount *pmp = this;
    648  1.46   pooka 	struct puffs_msgpark *park = parkptr;
    649  1.32   pooka 
    650  1.46   pooka 	DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
    651  1.46   pooka 	    park, status));
    652  1.46   pooka 	mutex_enter(&pmp->pmp_lock);
    653  1.46   pooka 	mutex_enter(&park->park_mtx);
    654  1.46   pooka 	if (park->park_flags & PARKFLAG_WANTREPLY) {
    655  1.46   pooka 		if (status == 0) {
    656  1.46   pooka 			DPRINTF(("enqueue replywait\n"));
    657  1.46   pooka 			TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
    658  1.22   pooka 			    park_entries);
    659  1.26   pooka 			park->park_flags |= PARKFLAG_ONQUEUE2;
    660   1.9   pooka 		} else {
    661  1.46   pooka 			DPRINTF(("error path!\n"));
    662  1.46   pooka 			park->park_preq->preq_rv = status;
    663  1.46   pooka 			park->park_flags |= PARKFLAG_DONE;
    664  1.46   pooka 			cv_signal(&park->park_cv);
    665   1.1   pooka 		}
    666  1.46   pooka 		puffs_msgpark_release(park);
    667  1.46   pooka 	} else {
    668  1.46   pooka 		DPRINTF(("release\n"));
    669  1.46   pooka 		puffs_msgpark_release1(park, 2);
    670   1.1   pooka 	}
    671  1.22   pooka 	mutex_exit(&pmp->pmp_lock);
    672   1.1   pooka }
    673   1.1   pooka 
    674  1.53   pooka size_t
    675  1.53   pooka puffs_msgif_waitcount(void *this)
    676  1.53   pooka {
    677  1.53   pooka 	struct puffs_mount *pmp = this;
    678  1.53   pooka 	size_t rv;
    679  1.53   pooka 
    680  1.53   pooka 	mutex_enter(&pmp->pmp_lock);
    681  1.53   pooka 	rv = pmp->pmp_msg_touser_count;
    682  1.53   pooka 	mutex_exit(&pmp->pmp_lock);
    683  1.53   pooka 
    684  1.53   pooka 	return rv;
    685  1.53   pooka }
    686  1.53   pooka 
    687  1.50   pooka /*
    688  1.50   pooka  * XXX: locking with this one?
    689  1.50   pooka  */
    690  1.53   pooka static void
    691  1.56   pooka puffsop_msg(void *this, struct puffs_req *preq)
    692   1.1   pooka {
    693  1.46   pooka 	struct puffs_mount *pmp = this;
    694  1.55   pooka 	struct putter_hdr *pth = &preq->preq_pth;
    695  1.46   pooka 	struct puffs_msgpark *park;
    696  1.57   pooka 	int wgone;
    697   1.1   pooka 
    698  1.22   pooka 	mutex_enter(&pmp->pmp_lock);
    699   1.9   pooka 
    700  1.46   pooka 	/* Locate waiter */
    701  1.46   pooka 	TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
    702  1.46   pooka 		if (park->park_preq->preq_id == preq->preq_id)
    703   1.1   pooka 			break;
    704  1.46   pooka 	}
    705  1.46   pooka 	if (park == NULL) {
    706  1.58   pooka 		DPRINTF(("puffsop_msg: no request: %" PRIu64 "\n",
    707  1.46   pooka 		    preq->preq_id));
    708  1.46   pooka 		mutex_exit(&pmp->pmp_lock);
    709  1.46   pooka 		return; /* XXX send error */
    710  1.46   pooka 	}
    711  1.26   pooka 
    712  1.46   pooka 	mutex_enter(&park->park_mtx);
    713  1.46   pooka 	puffs_msgpark_reference(park);
    714  1.55   pooka 	if (pth->pth_framelen > park->park_maxlen) {
    715  1.58   pooka 		DPRINTF(("puffsop_msg: invalid buffer length: "
    716  1.55   pooka 		    "%" PRIu64 " (req %" PRIu64 ", \n", pth->pth_framelen,
    717  1.55   pooka 		    preq->preq_id));
    718  1.46   pooka 		park->park_preq->preq_rv = EPROTO;
    719  1.46   pooka 		cv_signal(&park->park_cv);
    720  1.57   pooka 		puffs_msgpark_release1(park, 2);
    721  1.22   pooka 		mutex_exit(&pmp->pmp_lock);
    722  1.46   pooka 		return; /* XXX: error */
    723  1.46   pooka 	}
    724  1.46   pooka 	wgone = park->park_flags & PARKFLAG_WAITERGONE;
    725   1.9   pooka 
    726  1.46   pooka 	KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
    727  1.46   pooka 	TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
    728  1.46   pooka 	park->park_flags &= ~PARKFLAG_ONQUEUE2;
    729  1.46   pooka 	mutex_exit(&pmp->pmp_lock);
    730  1.24   pooka 
    731  1.46   pooka 	if (wgone) {
    732  1.58   pooka 		DPRINTF(("puffsop_msg: bad service - waiter gone for "
    733  1.46   pooka 		    "park %p\n", park));
    734  1.46   pooka 	} else {
    735  1.24   pooka 		if (park->park_flags & PARKFLAG_CALL) {
    736  1.58   pooka 			DPRINTF(("puffsop_msg: call for %p, arg %p\n",
    737  1.40   pooka 			    park->park_preq, park->park_donearg));
    738  1.55   pooka 			park->park_done(pmp, preq, park->park_donearg);
    739  1.46   pooka 		} else {
    740  1.46   pooka 			/* XXX: yes, I know */
    741  1.55   pooka 			memcpy(park->park_preq, preq, pth->pth_framelen);
    742  1.20   pooka 		}
    743  1.46   pooka 	}
    744   1.1   pooka 
    745  1.46   pooka 	if (!wgone) {
    746  1.46   pooka 		DPRINTF(("puffs_putop: flagging done for "
    747  1.46   pooka 		    "park %p\n", park));
    748  1.46   pooka 		cv_signal(&park->park_cv);
    749   1.1   pooka 	}
    750   1.1   pooka 
    751  1.46   pooka 	park->park_flags |= PARKFLAG_DONE;
    752  1.57   pooka 	puffs_msgpark_release1(park, 2);
    753   1.1   pooka }
    754   1.1   pooka 
    755  1.61   pooka static void
    756  1.53   pooka puffsop_flush(struct puffs_mount *pmp, struct puffs_flush *pf)
    757  1.53   pooka {
    758  1.53   pooka 	struct vnode *vp;
    759  1.53   pooka 	voff_t offlo, offhi;
    760  1.53   pooka 	int rv, flags = 0;
    761  1.53   pooka 
    762  1.61   pooka 	if (pf->pf_req.preq_pth.pth_framelen != sizeof(struct puffs_flush)) {
    763  1.61   pooka 		rv = EINVAL;
    764  1.61   pooka 		goto out;
    765  1.61   pooka 	}
    766  1.61   pooka 
    767  1.53   pooka 	/* XXX: slurry */
    768  1.53   pooka 	if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
    769  1.53   pooka 		cache_purgevfs(PMPTOMP(pmp));
    770  1.61   pooka 		rv = 0;
    771  1.61   pooka 		goto out;
    772  1.53   pooka 	}
    773  1.53   pooka 
    774  1.53   pooka 	/*
    775  1.53   pooka 	 * Get vnode, don't lock it.  Namecache is protected by its own lock
    776  1.53   pooka 	 * and we have a reference to protect against premature harvesting.
    777  1.53   pooka 	 *
    778  1.53   pooka 	 * The node we want here might be locked and the op is in
    779  1.53   pooka 	 * userspace waiting for us to complete ==> deadlock.  Another
    780  1.53   pooka 	 * reason we need to eventually bump locking to userspace, as we
    781  1.53   pooka 	 * will need to lock the node if we wish to do flushes.
    782  1.53   pooka 	 */
    783  1.53   pooka 	rv = puffs_cookie2vnode(pmp, pf->pf_cookie, 0, 0, &vp);
    784  1.53   pooka 	if (rv) {
    785  1.53   pooka 		if (rv == PUFFS_NOSUCHCOOKIE)
    786  1.61   pooka 			rv = ENOENT;
    787  1.61   pooka 		goto out;
    788  1.53   pooka 	}
    789  1.53   pooka 
    790  1.53   pooka 	switch (pf->pf_op) {
    791  1.53   pooka #if 0
    792  1.53   pooka 	/* not quite ready, yet */
    793  1.53   pooka 	case PUFFS_INVAL_NAMECACHE_NODE:
    794  1.53   pooka 	struct componentname *pf_cn;
    795  1.53   pooka 	char *name;
    796  1.53   pooka 		/* get comfortab^Wcomponentname */
    797  1.59   pooka 		pf_cn = kmem_alloc(componentname);
    798  1.53   pooka 		memset(pf_cn, 0, sizeof(struct componentname));
    799  1.53   pooka 		break;
    800  1.53   pooka 
    801  1.53   pooka #endif
    802  1.53   pooka 	case PUFFS_INVAL_NAMECACHE_DIR:
    803  1.53   pooka 		if (vp->v_type != VDIR) {
    804  1.53   pooka 			rv = EINVAL;
    805  1.53   pooka 			break;
    806  1.53   pooka 		}
    807  1.53   pooka 		cache_purge1(vp, NULL, PURGE_CHILDREN);
    808  1.53   pooka 		break;
    809  1.53   pooka 
    810  1.53   pooka 	case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
    811  1.53   pooka 		flags = PGO_FREE;
    812  1.53   pooka 		/*FALLTHROUGH*/
    813  1.53   pooka 	case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
    814  1.53   pooka 		if (flags == 0)
    815  1.53   pooka 			flags = PGO_CLEANIT;
    816  1.53   pooka 
    817  1.53   pooka 		if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
    818  1.53   pooka 			rv = EINVAL;
    819  1.53   pooka 			break;
    820  1.53   pooka 		}
    821  1.53   pooka 
    822  1.53   pooka 		offlo = trunc_page(pf->pf_start);
    823  1.53   pooka 		offhi = round_page(pf->pf_end);
    824  1.53   pooka 		if (offhi != 0 && offlo >= offhi) {
    825  1.53   pooka 			rv = EINVAL;
    826  1.53   pooka 			break;
    827  1.53   pooka 		}
    828  1.53   pooka 
    829  1.62      ad 		mutex_enter(&vp->v_uobj.vmobjlock);
    830  1.53   pooka 		rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
    831  1.53   pooka 		break;
    832  1.53   pooka 
    833  1.53   pooka 	default:
    834  1.53   pooka 		rv = EINVAL;
    835  1.53   pooka 	}
    836  1.53   pooka 
    837  1.53   pooka 	vrele(vp);
    838  1.53   pooka 
    839  1.61   pooka  out:
    840  1.61   pooka 	puffs_msg_sendresp(pmp, &pf->pf_req, rv);
    841  1.53   pooka }
    842  1.53   pooka 
    843  1.53   pooka int
    844  1.56   pooka puffs_msgif_dispatch(void *this, struct putter_hdr *pth)
    845  1.53   pooka {
    846  1.53   pooka 	struct puffs_mount *pmp = this;
    847  1.56   pooka 	struct puffs_req *preq = (struct puffs_req *)pth;
    848  1.74   pooka 	int rv = 0;
    849  1.56   pooka 
    850  1.56   pooka 	/* XXX: need to send error to userspace */
    851  1.61   pooka 	if (pth->pth_framelen < sizeof(struct puffs_req)) {
    852  1.61   pooka 		puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
    853  1.61   pooka 		return 0;
    854  1.61   pooka 	}
    855  1.53   pooka 
    856  1.55   pooka 	switch (PUFFSOP_OPCLASS(preq->preq_opclass)) {
    857  1.53   pooka 	case PUFFSOP_VN:
    858  1.53   pooka 	case PUFFSOP_VFS:
    859  1.61   pooka 		DPRINTF(("dispatch: vn/vfs message 0x%x\n", preq->preq_optype));
    860  1.56   pooka 		puffsop_msg(pmp, preq);
    861  1.53   pooka 		break;
    862  1.53   pooka 	case PUFFSOP_FLUSH:
    863  1.61   pooka 		DPRINTF(("dispatch: flush 0x%x\n", preq->preq_optype));
    864  1.56   pooka 		puffsop_flush(pmp, (struct puffs_flush *)preq);
    865  1.53   pooka 		break;
    866  1.53   pooka 	default:
    867  1.61   pooka 		DPRINTF(("dispatch: invalid class 0x%x\n", preq->preq_opclass));
    868  1.75   pooka 		puffs_msg_sendresp(pmp, preq, EOPNOTSUPP);
    869  1.53   pooka 		break;
    870  1.53   pooka 	}
    871  1.53   pooka 
    872  1.74   pooka 	return rv;
    873  1.53   pooka }
    874  1.53   pooka 
    875  1.53   pooka int
    876  1.53   pooka puffs_msgif_close(void *this)
    877  1.53   pooka {
    878  1.53   pooka 	struct puffs_mount *pmp = this;
    879  1.53   pooka 	struct mount *mp = PMPTOMP(pmp);
    880  1.53   pooka 
    881  1.53   pooka 	mutex_enter(&pmp->pmp_lock);
    882  1.53   pooka 	puffs_mp_reference(pmp);
    883  1.53   pooka 
    884  1.53   pooka 	/*
    885  1.53   pooka 	 * Free the waiting callers before proceeding any further.
    886  1.53   pooka 	 * The syncer might be jogging around in this file system
    887  1.53   pooka 	 * currently.  If we allow it to go to the userspace of no
    888  1.53   pooka 	 * return while trying to get the syncer lock, well ...
    889  1.53   pooka 	 */
    890  1.53   pooka 	puffs_userdead(pmp);
    891  1.53   pooka 
    892  1.53   pooka 	/*
    893  1.53   pooka 	 * Make sure someone from puffs_unmount() isn't currently in
    894  1.53   pooka 	 * userspace.  If we don't take this precautionary step,
    895  1.53   pooka 	 * they might notice that the mountpoint has disappeared
    896  1.53   pooka 	 * from under them once they return.  Especially note that we
    897  1.53   pooka 	 * cannot simply test for an unmounter before calling
    898  1.53   pooka 	 * dounmount(), since it might be possible that that particular
    899  1.53   pooka 	 * invocation of unmount was called without MNT_FORCE.  Here we
    900  1.53   pooka 	 * *must* make sure unmount succeeds.  Also, restart is necessary
    901  1.53   pooka 	 * since pmp isn't locked.  We might end up with PUTTER_DEAD after
    902  1.53   pooka 	 * restart and exit from there.
    903  1.53   pooka 	 */
    904  1.53   pooka 	if (pmp->pmp_unmounting) {
    905  1.53   pooka 		cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
    906  1.53   pooka 		puffs_mp_release(pmp);
    907  1.53   pooka 		mutex_exit(&pmp->pmp_lock);
    908  1.53   pooka 		DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
    909  1.53   pooka 		    "restart\n", pmp));
    910  1.53   pooka 		return ERESTART;
    911  1.53   pooka 	}
    912  1.53   pooka 
    913  1.53   pooka 	/* Won't access pmp from here anymore */
    914  1.72      ad 	atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
    915  1.53   pooka 	puffs_mp_release(pmp);
    916  1.53   pooka 	mutex_exit(&pmp->pmp_lock);
    917  1.53   pooka 
    918  1.72      ad 	/* Detach from VFS. */
    919  1.71      ad 	(void)dounmount(mp, MNT_FORCE, curlwp);
    920  1.72      ad 	vfs_destroy(mp);
    921  1.53   pooka 
    922  1.53   pooka 	return 0;
    923  1.53   pooka }
    924  1.53   pooka 
    925  1.53   pooka /*
    926  1.22   pooka  * We're dead, kaput, RIP, slightly more than merely pining for the
    927  1.22   pooka  * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
    928  1.22   pooka  * our maker, ceased to be, etcetc.  YASD.  It's a dead FS!
    929  1.22   pooka  *
    930  1.22   pooka  * Caller must hold puffs mutex.
    931  1.22   pooka  */
    932  1.22   pooka void
    933  1.22   pooka puffs_userdead(struct puffs_mount *pmp)
    934  1.22   pooka {
    935  1.46   pooka 	struct puffs_msgpark *park, *park_next;
    936  1.22   pooka 
    937  1.22   pooka 	/*
    938  1.22   pooka 	 * Mark filesystem status as dying so that operations don't
    939  1.22   pooka 	 * attempt to march to userspace any longer.
    940  1.22   pooka 	 */
    941  1.22   pooka 	pmp->pmp_status = PUFFSTAT_DYING;
    942  1.22   pooka 
    943  1.22   pooka 	/* signal waiters on REQUEST TO file server queue */
    944  1.46   pooka 	for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
    945  1.24   pooka 		uint8_t opclass;
    946  1.24   pooka 
    947  1.46   pooka 		mutex_enter(&park->park_mtx);
    948  1.46   pooka 		puffs_msgpark_reference(park);
    949  1.32   pooka 		park_next = TAILQ_NEXT(park, park_entries);
    950  1.26   pooka 
    951  1.26   pooka 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    952  1.46   pooka 		TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
    953  1.26   pooka 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    954  1.46   pooka 		pmp->pmp_msg_touser_count--;
    955  1.22   pooka 
    956  1.31   pooka 		/*
    957  1.51   pooka 		 * Even though waiters on QUEUE1 are removed in touser()
    958  1.51   pooka 		 * in case of WAITERGONE, it is still possible for us to
    959  1.51   pooka 		 * get raced here due to having to retake locks in said
    960  1.51   pooka 		 * touser().  In the race case simply "ignore" the item
    961  1.51   pooka 		 * on the queue and move on to the next one.
    962  1.31   pooka 		 */
    963  1.31   pooka 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    964  1.31   pooka 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    965  1.31   pooka 			KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    966  1.46   pooka 			puffs_msgpark_release(park);
    967  1.51   pooka 
    968  1.22   pooka 		} else {
    969  1.31   pooka 			opclass = park->park_preq->preq_opclass;
    970  1.23   pooka 			park->park_preq->preq_rv = ENXIO;
    971  1.31   pooka 
    972  1.31   pooka 			if (park->park_flags & PARKFLAG_CALL) {
    973  1.42   pooka 				park->park_done(pmp, park->park_preq,
    974  1.31   pooka 				    park->park_donearg);
    975  1.46   pooka 				puffs_msgpark_release1(park, 2);
    976  1.31   pooka 			} else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
    977  1.46   pooka 				puffs_msgpark_release1(park, 2);
    978  1.31   pooka 			} else {
    979  1.31   pooka 				park->park_preq->preq_rv = ENXIO;
    980  1.31   pooka 				cv_signal(&park->park_cv);
    981  1.46   pooka 				puffs_msgpark_release(park);
    982  1.31   pooka 			}
    983  1.22   pooka 		}
    984  1.22   pooka 	}
    985  1.22   pooka 
    986  1.22   pooka 	/* signal waiters on RESPONSE FROM file server queue */
    987  1.46   pooka 	for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
    988  1.46   pooka 		mutex_enter(&park->park_mtx);
    989  1.46   pooka 		puffs_msgpark_reference(park);
    990  1.32   pooka 		park_next = TAILQ_NEXT(park, park_entries);
    991  1.26   pooka 
    992  1.26   pooka 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
    993  1.31   pooka 		KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    994  1.26   pooka 
    995  1.46   pooka 		TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
    996  1.26   pooka 		park->park_flags &= ~PARKFLAG_ONQUEUE2;
    997  1.26   pooka 
    998  1.31   pooka 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    999  1.31   pooka 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
   1000  1.46   pooka 			puffs_msgpark_release(park);
   1001  1.22   pooka 		} else {
   1002  1.31   pooka 			park->park_preq->preq_rv = ENXIO;
   1003  1.31   pooka 			if (park->park_flags & PARKFLAG_CALL) {
   1004  1.42   pooka 				park->park_done(pmp, park->park_preq,
   1005  1.31   pooka 				    park->park_donearg);
   1006  1.46   pooka 				puffs_msgpark_release1(park, 2);
   1007  1.31   pooka 			} else {
   1008  1.31   pooka 				cv_signal(&park->park_cv);
   1009  1.46   pooka 				puffs_msgpark_release(park);
   1010  1.31   pooka 			}
   1011  1.22   pooka 		}
   1012  1.22   pooka 	}
   1013  1.50   pooka 
   1014  1.50   pooka 	cv_broadcast(&pmp->pmp_msg_waiter_cv);
   1015  1.22   pooka }
   1016