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