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