Home | History | Annotate | Line # | Download | only in puffs
puffs_msgif.c revision 1.24
      1 /*	$NetBSD: puffs_msgif.c,v 1.24 2007/03/30 17:48:57 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  * 3. The name of the company nor the name of the author may be used to
     19  *    endorse or promote products derived from this software without specific
     20  *    prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.24 2007/03/30 17:48:57 pooka Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/fstrans.h>
     40 #include <sys/malloc.h>
     41 #include <sys/mount.h>
     42 #include <sys/vnode.h>
     43 #include <sys/lock.h>
     44 
     45 #include <fs/puffs/puffs_msgif.h>
     46 #include <fs/puffs/puffs_sys.h>
     47 
     48 /*
     49  * waitq data structures
     50  */
     51 
     52 /*
     53  * While a request is going to userspace, park the caller within the
     54  * kernel.  This is the kernel counterpart of "struct puffs_req".
     55  */
     56 struct puffs_park {
     57 	struct puffs_req	*park_preq;	/* req followed by buf	*/
     58 	uint64_t		park_id;	/* duplicate of preq_id */
     59 
     60 	size_t			park_copylen;	/* userspace copylength	*/
     61 	size_t			park_maxlen;	/* max size in comeback */
     62 
     63 	parkdone_fn		park_done;
     64 	void			*park_donearg;
     65 
     66 	int			park_flags;
     67 
     68 	kcondvar_t		park_cv;
     69 	TAILQ_ENTRY(puffs_park) park_entries;
     70 };
     71 #define PARKFLAG_WAITERGONE	0x01
     72 #define PARKFLAG_PROCESSING	0x02
     73 #define PARKFLAG_CALL		0x04
     74 
     75 static struct pool_cache parkpc;
     76 static struct pool parkpool;
     77 
     78 static int
     79 makepark(void *arg, void *obj, int flags)
     80 {
     81 	struct puffs_park *park = obj;
     82 
     83 	cv_init(&park->park_cv, "puffsrpl");
     84 
     85 	return 0;
     86 }
     87 
     88 static void
     89 nukepark(void *arg, void *obj)
     90 {
     91 	struct puffs_park *park = obj;
     92 
     93 	cv_destroy(&park->park_cv);
     94 }
     95 
     96 void
     97 puffs_msgif_init()
     98 {
     99 
    100 	pool_init(&parkpool, sizeof(struct puffs_park), 0, 0, 0,
    101 	    "puffprkl", &pool_allocator_nointr, IPL_NONE);
    102 	pool_cache_init(&parkpc, &parkpool, makepark, nukepark, NULL);
    103 }
    104 
    105 void
    106 puffs_msgif_destroy()
    107 {
    108 
    109 	pool_cache_destroy(&parkpc);
    110 	pool_destroy(&parkpool);
    111 }
    112 
    113 void *
    114 puffs_parkmem_alloc(int waitok)
    115 {
    116 
    117 	return pool_cache_get(&parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
    118 }
    119 
    120 void
    121 puffs_parkmem_free(void *ppark)
    122 {
    123 
    124 	pool_cache_put(&parkpc, ppark);
    125 }
    126 
    127 
    128 /*
    129  * Converts a non-FAF op to a FAF.  This simply involves making copies
    130  * of the park and request structures and tagging the request as a FAF.
    131  * It is safe to block here, since the original op is not a FAF.
    132  */
    133 #if 0
    134 static void
    135 puffs_reqtofaf(struct puffs_park *ppark)
    136 {
    137 	struct puffs_req *newpreq;
    138 
    139 	KASSERT((ppark->park_preq->preq_opclass & PUFFSOPFLAG_FAF) == 0);
    140 
    141 	MALLOC(newpreq, struct puffs_req *, ppark->park_copylen,
    142 	    M_PUFFS, M_ZERO | M_WAITOK);
    143 
    144 	memcpy(newpreq, ppark->park_preq, ppark->park_copylen);
    145 
    146 	ppark->park_preq = newpreq;
    147 	ppark->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
    148 }
    149 #endif
    150 
    151 
    152 /*
    153  * kernel-user-kernel waitqueues
    154  */
    155 
    156 static int touser(struct puffs_mount *, struct puffs_park *, uint64_t,
    157 		  struct vnode *, struct vnode *);
    158 
    159 uint64_t
    160 puffs_getreqid(struct puffs_mount *pmp)
    161 {
    162 	uint64_t rv;
    163 
    164 	mutex_enter(&pmp->pmp_lock);
    165 	rv = pmp->pmp_nextreq++;
    166 	mutex_exit(&pmp->pmp_lock);
    167 
    168 	return rv;
    169 }
    170 
    171 /* vfs request */
    172 int
    173 puffs_vfstouser(struct puffs_mount *pmp, int optype, void *kbuf, size_t buflen)
    174 {
    175 	struct puffs_park *ppark;
    176 
    177 	ppark = puffs_parkmem_alloc(1);
    178 	ppark->park_preq = kbuf;
    179 
    180 	ppark->park_preq->preq_opclass = PUFFSOP_VFS;
    181 	ppark->park_preq->preq_optype = optype;
    182 
    183 	ppark->park_maxlen = ppark->park_copylen = buflen;
    184 	ppark->park_flags = 0;
    185 
    186 	return touser(pmp, ppark, puffs_getreqid(pmp), NULL, NULL);
    187 }
    188 
    189 void
    190 puffs_suspendtouser(struct puffs_mount *pmp, int status)
    191 {
    192 	struct puffs_vfsreq_suspend *pvfsr_susp;
    193 	struct puffs_park *ppark;
    194 
    195 	pvfsr_susp = malloc(sizeof(struct puffs_vfsreq_suspend),
    196 	    M_PUFFS, M_WAITOK | M_ZERO);
    197 	ppark = puffs_parkmem_alloc(1);
    198 
    199 	pvfsr_susp->pvfsr_status = status;
    200 	ppark->park_preq = (struct puffs_req *)pvfsr_susp;
    201 
    202 	ppark->park_preq->preq_opclass = PUFFSOP_VFS | PUFFSOPFLAG_FAF;
    203 	ppark->park_preq->preq_optype = PUFFS_VFS_SUSPEND;
    204 
    205 	ppark->park_maxlen = ppark->park_copylen
    206 	    = sizeof(struct puffs_vfsreq_suspend);
    207 	ppark->park_flags = 0;
    208 
    209 	(void)touser(pmp, ppark, 0, NULL, NULL);
    210 }
    211 
    212 /*
    213  * vnode level request
    214  */
    215 int
    216 puffs_vntouser(struct puffs_mount *pmp, int optype,
    217 	void *kbuf, size_t buflen, size_t maxdelta, void *cookie,
    218 	struct vnode *vp1, struct vnode *vp2)
    219 {
    220 	struct puffs_park *ppark;
    221 
    222 	ppark = puffs_parkmem_alloc(1);
    223 	ppark->park_preq = kbuf;
    224 
    225 	ppark->park_preq->preq_opclass = PUFFSOP_VN;
    226 	ppark->park_preq->preq_optype = optype;
    227 	ppark->park_preq->preq_cookie = cookie;
    228 
    229 	ppark->park_copylen = buflen;
    230 	ppark->park_maxlen = buflen + maxdelta;
    231 	ppark->park_flags = 0;
    232 
    233 	return touser(pmp, ppark, puffs_getreqid(pmp), vp1, vp2);
    234 }
    235 
    236 /*
    237  * vnode level request, caller-controller req id
    238  */
    239 int
    240 puffs_vntouser_req(struct puffs_mount *pmp, int optype,
    241 	void *kbuf, size_t buflen, size_t maxdelta, void *cookie,
    242 	uint64_t reqid, struct vnode *vp1, struct vnode *vp2)
    243 {
    244 	struct puffs_park *ppark;
    245 
    246 	ppark = puffs_parkmem_alloc(1);
    247 	ppark->park_preq = kbuf;
    248 
    249 	ppark->park_preq->preq_opclass = PUFFSOP_VN;
    250 	ppark->park_preq->preq_optype = optype;
    251 	ppark->park_preq->preq_cookie = cookie;
    252 
    253 	ppark->park_copylen = buflen;
    254 	ppark->park_maxlen = buflen + maxdelta;
    255 	ppark->park_flags = 0;
    256 
    257 	return touser(pmp, ppark, reqid, vp1, vp2);
    258 }
    259 
    260 void
    261 puffs_vntouser_call(struct puffs_mount *pmp, int optype,
    262 	void *kbuf, size_t buflen, size_t maxdelta, void *cookie,
    263 	parkdone_fn donefn, void *donearg,
    264 	struct vnode *vp1, struct vnode *vp2)
    265 {
    266 	struct puffs_park *ppark;
    267 
    268 	ppark = puffs_parkmem_alloc(1);
    269 	ppark->park_preq = kbuf;
    270 
    271 	ppark->park_preq->preq_opclass = PUFFSOP_VN;
    272 	ppark->park_preq->preq_optype = optype;
    273 	ppark->park_preq->preq_cookie = cookie;
    274 
    275 	ppark->park_copylen = buflen;
    276 	ppark->park_maxlen = buflen + maxdelta;
    277 	ppark->park_done = donefn;
    278 	ppark->park_donearg = donearg;
    279 	ppark->park_flags = PARKFLAG_CALL;
    280 
    281 	(void) touser(pmp, ppark, puffs_getreqid(pmp), vp1, vp2);
    282 }
    283 
    284 /*
    285  * Notice: kbuf will be free'd later.  I must be allocated from the
    286  * kernel heap and it's ownership is shifted to this function from
    287  * now on, i.e. the caller is not allowed to use it anymore!
    288  */
    289 void
    290 puffs_vntouser_faf(struct puffs_mount *pmp, int optype,
    291 	void *kbuf, size_t buflen, void *cookie)
    292 {
    293 	struct puffs_park *ppark;
    294 
    295 	/* XXX: is it allowable to sleep here? */
    296 	ppark = puffs_parkmem_alloc(0);
    297 	if (ppark == NULL)
    298 		return; /* 2bad */
    299 
    300 	ppark->park_preq = kbuf;
    301 
    302 	ppark->park_preq->preq_opclass = PUFFSOP_VN | PUFFSOPFLAG_FAF;
    303 	ppark->park_preq->preq_optype = optype;
    304 	ppark->park_preq->preq_cookie = cookie;
    305 
    306 	ppark->park_maxlen = ppark->park_copylen = buflen;
    307 	ppark->park_flags = 0;
    308 
    309 	(void)touser(pmp, ppark, 0, NULL, NULL);
    310 }
    311 
    312 void
    313 puffs_cacheop(struct puffs_mount *pmp, struct puffs_park *ppark,
    314 	struct puffs_cacheinfo *pcinfo, size_t pcilen, void *cookie)
    315 {
    316 
    317 	ppark->park_preq = (struct puffs_req *)pcinfo;
    318 	ppark->park_preq->preq_opclass = PUFFSOP_CACHE | PUFFSOPFLAG_FAF;
    319 	ppark->park_preq->preq_optype = PCACHE_TYPE_WRITE; /* XXX */
    320 	ppark->park_preq->preq_cookie = cookie;
    321 
    322 	ppark->park_maxlen = ppark->park_copylen = pcilen;
    323 
    324 	(void)touser(pmp, ppark, 0, NULL, NULL);
    325 }
    326 
    327 /*
    328  * Wait for the userspace ping-pong game in calling process context.
    329  *
    330  * This unlocks vnodes if they are supplied.  vp1 is the vnode
    331  * before in the locking order, i.e. the one which must be locked
    332  * before accessing vp2.  This is done here so that operations are
    333  * already ordered in the queue when vnodes are unlocked (I'm not
    334  * sure if that's really necessary, but it can't hurt).  Okok, maybe
    335  * there's a slight ugly-factor also, but let's not worry about that.
    336  */
    337 static int
    338 touser(struct puffs_mount *pmp, struct puffs_park *ppark, uint64_t reqid,
    339 	struct vnode *vp1, struct vnode *vp2)
    340 {
    341 	struct mount *mp;
    342 	struct puffs_req *preq;
    343 	int rv = 0;
    344 
    345 	mp = PMPTOMP(pmp);
    346 	preq = ppark->park_preq;
    347 	preq->preq_id = ppark->park_id = reqid;
    348 	preq->preq_buflen = ALIGN(ppark->park_maxlen);
    349 
    350 #if 0
    351 	/*
    352 	 * We don't trap signals currently
    353 	 */
    354 	struct lwp *l = curlwp;
    355 
    356 	/*
    357 	 * To support PCATCH, yet another movie: check if there are signals
    358 	 * pending and we are issueing a non-FAF.  If so, return an error
    359 	 * directly UNLESS we are issueing INACTIVE.  In that case, convert
    360 	 * it to a FAF, fire off to the file server and return an error.
    361 	 * Yes, this is bordering disgusting.  Barfbags are on me.
    362 	 */
    363 	if (PUFFSOP_WANTREPLY(preq->preq_opclass)
    364 	   && (ppark->park_flags & PARKFLAG_CALL) == 0
    365 	   && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    366 		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
    367 		    && preq->preq_optype == PUFFS_VN_INACTIVE) {
    368 			puffs_reqtofaf(ppark);
    369 			DPRINTF(("puffs touser: converted to FAF %p\n", ppark));
    370 			rv = EINTR;
    371 		} else {
    372 			return EINTR;
    373 		}
    374 	}
    375 #endif
    376 
    377 	/*
    378 	 * test for suspension lock.
    379 	 *
    380 	 * Note that we *DO NOT* keep the lock, since that might block
    381 	 * lock acquiring PLUS it would give userlandia control over
    382 	 * the lock.  The operation queue enforces a strict ordering:
    383 	 * when the fs server gets in the op stream, it knows things
    384 	 * are in order.  The kernel locks can't guarantee that for
    385 	 * userspace, in any case.
    386 	 *
    387 	 * BUT: this presents a problem for ops which have a consistency
    388 	 * clause based on more than one operation.  Unfortunately such
    389 	 * operations (read, write) do not reliably work yet.
    390 	 *
    391 	 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
    392 	 *
    393 	 * XXX: and there is one more problem.  We sometimes need to
    394 	 * take a lazy lock in case the fs is suspending and we are
    395 	 * executing as the fs server context.  This might happen
    396 	 * e.g. in the case that the user server triggers a reclaim
    397 	 * in the kernel while the fs is suspending.  It's not a very
    398 	 * likely event, but it needs to be fixed some day.
    399 	 */
    400 
    401 	/*
    402 	 * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
    403 	 * the mutex here, since getpages() might be called locked.
    404 	 */
    405 	fstrans_start(mp, FSTRANS_NORMAL);
    406 	mutex_enter(&pmp->pmp_lock);
    407 	fstrans_done(mp);
    408 
    409 	if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    410 		mutex_exit(&pmp->pmp_lock);
    411 		puffs_parkmem_free(ppark);
    412 		return ENXIO;
    413 	}
    414 
    415 	TAILQ_INSERT_TAIL(&pmp->pmp_req_touser, ppark, park_entries);
    416 	pmp->pmp_req_waiters++;
    417 
    418 #if 0
    419 	/*
    420 	 * Don't do unlock-relock dance yet.  There are a couple of
    421 	 * unsolved issues with it.  If we don't unlock, we can have
    422 	 * processes wanting vn_lock in case userspace hangs.  But
    423 	 * that can be "solved" by killing the userspace process.  It
    424 	 * would of course be nicer to have antilocking in the userspace
    425 	 * interface protocol itself.. your patience will be rewarded.
    426 	 */
    427 	/* unlock */
    428 	if (vp2)
    429 		VOP_UNLOCK(vp2, 0);
    430 	if (vp1)
    431 		VOP_UNLOCK(vp1, 0);
    432 #endif
    433 
    434 	DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
    435 	    "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, ppark,
    436 	    preq->preq_opclass, preq->preq_optype, ppark->park_flags));
    437 
    438 	cv_broadcast(&pmp->pmp_req_waiter_cv);
    439 	selnotify(pmp->pmp_sel, 0);
    440 
    441 	if (PUFFSOP_WANTREPLY(preq->preq_opclass)
    442 	    && (ppark->park_flags & PARKFLAG_CALL) == 0) {
    443 		int error;
    444 
    445 		error = 0; /* XXX: no interrupt for now */
    446 
    447 		cv_wait(&ppark->park_cv, &pmp->pmp_lock);
    448 		if (error) {
    449 			ppark->park_flags |= PARKFLAG_WAITERGONE;
    450 			if (ppark->park_flags & PARKFLAG_PROCESSING) {
    451 				cv_wait(&ppark->park_cv, &pmp->pmp_lock);
    452 				rv = preq->preq_rv;
    453 			} else {
    454 				rv = error;
    455 			}
    456 		} else {
    457 			rv = preq->preq_rv;
    458 		}
    459 		mutex_exit(&pmp->pmp_lock);
    460 		puffs_parkmem_free(ppark);
    461 
    462 		/*
    463 		 * retake the lock and release.  This makes sure (haha,
    464 		 * I'm humorous) that we don't process the same vnode in
    465 		 * multiple threads due to the locks hacks we have in
    466 		 * puffs_lock().  In reality this is well protected by
    467 		 * the biglock, but once that's gone, well, hopefully
    468 		 * this will be fixed for real.  (and when you read this
    469 		 * comment in 2017 and subsequently barf, my condolences ;).
    470 		 */
    471 		if (rv == 0 && !fstrans_is_owner(mp)) {
    472 			fstrans_start(mp, FSTRANS_NORMAL);
    473 			fstrans_done(mp);
    474 		}
    475 	} else {
    476 		mutex_exit(&pmp->pmp_lock);
    477 	}
    478 
    479 #if 0
    480 	/* relock */
    481 	if (vp1)
    482 		KASSERT(vn_lock(vp1, LK_EXCLUSIVE | LK_RETRY) == 0);
    483 	if (vp2)
    484 		KASSERT(vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY) == 0);
    485 #endif
    486 
    487 	mutex_enter(&pmp->pmp_lock);
    488 	if (--pmp->pmp_req_waiters == 0) {
    489 		KASSERT(cv_has_waiters(&pmp->pmp_req_waitersink_cv) <= 1);
    490 		cv_signal(&pmp->pmp_req_waitersink_cv);
    491 	}
    492 	mutex_exit(&pmp->pmp_lock);
    493 
    494 	return rv;
    495 }
    496 
    497 
    498 /*
    499  * getop: scan through queued requests until:
    500  *  1) max number of requests satisfied
    501  *     OR
    502  *  2) buffer runs out of space
    503  *     OR
    504  *  3) nonblocking is set AND there are no operations available
    505  *     OR
    506  *  4) at least one operation was transferred AND there are no more waiting
    507  */
    508 int
    509 puffs_getop(struct puffs_mount *pmp, struct puffs_reqh_get *phg, int nonblock)
    510 {
    511 	struct puffs_park *park;
    512 	struct puffs_req *preq;
    513 	uint8_t *bufpos;
    514 	int error, donesome;
    515 
    516 	donesome = error = 0;
    517 	bufpos = phg->phg_buf;
    518 
    519 	mutex_enter(&pmp->pmp_lock);
    520 	while (phg->phg_nops == 0 || donesome != phg->phg_nops) {
    521  again:
    522 		if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    523 			/* if we got some, they don't really matter anymore */
    524 			error = ENXIO;
    525 			goto out;
    526 		}
    527 		if (TAILQ_EMPTY(&pmp->pmp_req_touser)) {
    528 			if (donesome)
    529 				goto out;
    530 
    531 			if (nonblock) {
    532 				error = EWOULDBLOCK;
    533 				goto out;
    534 			}
    535 
    536 			error = cv_wait_sig(&pmp->pmp_req_waiter_cv,
    537 			    &pmp->pmp_lock);
    538 			if (error)
    539 				goto out;
    540 			else
    541 				goto again;
    542 		}
    543 
    544 		park = TAILQ_FIRST(&pmp->pmp_req_touser);
    545 		preq = park->park_preq;
    546 		if (phg->phg_buflen < preq->preq_buflen) {
    547 			if (!donesome)
    548 				error = E2BIG;
    549 			goto out;
    550 		}
    551 		TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
    552 
    553 		/* If it's a goner, don't process any furher */
    554 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    555 			panic("impossible for now");
    556 			puffs_parkmem_free(park);
    557 			continue;
    558 		}
    559 
    560 		mutex_exit(&pmp->pmp_lock);
    561 
    562 		DPRINTF(("puffsgetop: get op %" PRIu64 " (%d.), from %p "
    563 		    "len %zu (buflen %zu), target %p\n", preq->preq_id,
    564 		    donesome, preq, park->park_copylen, preq->preq_buflen,
    565 		    bufpos));
    566 
    567 		if ((error = copyout(preq, bufpos, park->park_copylen)) != 0) {
    568 			DPRINTF(("puffs_getop: copyout failed\n"));
    569 			/*
    570 			 * ok, user server is probably trying to cheat.
    571 			 * stuff op back & return error to user
    572 			 */
    573 			 mutex_enter(&pmp->pmp_lock);
    574 			 TAILQ_INSERT_HEAD(&pmp->pmp_req_touser, park,
    575 			     park_entries);
    576 
    577 			 if (donesome)
    578 				error = 0;
    579 			 goto out;
    580 		}
    581 		bufpos += preq->preq_buflen;
    582 		phg->phg_buflen -= preq->preq_buflen;
    583 		donesome++;
    584 
    585 		mutex_enter(&pmp->pmp_lock);
    586 		if (PUFFSOP_WANTREPLY(preq->preq_opclass)) {
    587 			TAILQ_INSERT_TAIL(&pmp->pmp_req_replywait, park,
    588 			    park_entries);
    589 		} else {
    590 			free(preq, M_PUFFS);
    591 			puffs_parkmem_free(park);
    592 		}
    593 	}
    594 
    595  out:
    596 	phg->phg_more = pmp->pmp_req_waiters;
    597 	mutex_exit(&pmp->pmp_lock);
    598 
    599 	phg->phg_nops = donesome;
    600 
    601 	return error;
    602 }
    603 
    604 int
    605 puffs_putop(struct puffs_mount *pmp, struct puffs_reqh_put *php)
    606 {
    607 	struct puffs_park *park;
    608 	struct puffs_req tmpreq;
    609 	struct puffs_req *nextpreq;
    610 	void *userbuf;
    611 	uint64_t id;
    612 	size_t reqlen;
    613 	int donesome, error, wgone;
    614 
    615 	donesome = error = wgone = 0;
    616 
    617 	id = php->php_id;
    618 	userbuf = php->php_buf;
    619 	reqlen = php->php_buflen;
    620 
    621 	mutex_enter(&pmp->pmp_lock);
    622 	while (donesome != php->php_nops) {
    623 #ifdef PUFFSDEBUG
    624 		DPRINTF(("puffsputop: searching for %" PRIu64 ", ubuf: %p, "
    625 		    "len %zu\n", id, userbuf, reqlen));
    626 #endif
    627 		TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
    628 			if (park->park_id == id)
    629 				break;
    630 		}
    631 
    632 		if (park == NULL) {
    633 			DPRINTF(("puffsputop: no request: %" PRIu64 "\n", id));
    634 			error = EINVAL;
    635 			break;
    636 		}
    637 
    638 		if (reqlen == 0 || reqlen > park->park_maxlen) {
    639 			DPRINTF(("puffsputop: invalid buffer length: "
    640 			    "%zu\n", reqlen));
    641 			error = E2BIG;
    642 			break;
    643 		}
    644 		TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
    645 		wgone = park->park_flags & PARKFLAG_WAITERGONE;
    646 		park->park_flags |= PARKFLAG_PROCESSING;
    647 		mutex_exit(&pmp->pmp_lock);
    648 
    649 		/*
    650 		 * If the caller has gone south, go to next, collect
    651 		 * $200 and free the structure there instead of wakeup.
    652 		 * We also need to copyin the
    653 		 */
    654 		if (wgone) {
    655 			panic("puffs: wgone impossible for now\n");
    656 			DPRINTF(("puffs_putop: bad service - waiter gone for "
    657 			    "park %p\n", park));
    658 			error = copyin(userbuf, &tmpreq,
    659 			    sizeof(struct puffs_req));
    660 			if (error)
    661 				goto loopout;
    662 			nextpreq = &tmpreq;
    663 			goto next;
    664 		}
    665 
    666 		DPRINTF(("puffsputpop: copyin from %p to %p, len %zu\n",
    667 		    userbuf, park->park_preq, reqlen));
    668 		error = copyin(userbuf, park->park_preq, reqlen);
    669 		if (error)
    670 			goto loopout;
    671 		nextpreq = park->park_preq;
    672 
    673  next:
    674 		/* all's well, prepare for next op */
    675 		id = nextpreq->preq_id;
    676 		reqlen = nextpreq->preq_buflen;
    677 		userbuf = nextpreq->preq_nextbuf;
    678 		donesome++;
    679 
    680  loopout:
    681 		if (error)
    682 			park->park_preq->preq_rv = error;
    683 
    684 		if (park->park_flags & PARKFLAG_CALL) {
    685 			park->park_done(park->park_preq, park->park_donearg);
    686 			puffs_parkmem_free(park);
    687 		}
    688 
    689 		mutex_enter(&pmp->pmp_lock);
    690 		if (!wgone) {
    691 			DPRINTF(("puffs_putop: flagging done for "
    692 			    "park %p\n", park));
    693 
    694 			cv_signal(&park->park_cv);
    695 		}
    696 
    697 		if (error)
    698 			break;
    699 		wgone = 0;
    700 	}
    701 
    702 	mutex_exit(&pmp->pmp_lock);
    703 	php->php_nops -= donesome;
    704 
    705 	return error;
    706 }
    707 
    708 /*
    709  * We're dead, kaput, RIP, slightly more than merely pining for the
    710  * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
    711  * our maker, ceased to be, etcetc.  YASD.  It's a dead FS!
    712  *
    713  * Caller must hold puffs mutex.
    714  */
    715 void
    716 puffs_userdead(struct puffs_mount *pmp)
    717 {
    718 	struct puffs_park *park;
    719 
    720 	/*
    721 	 * Mark filesystem status as dying so that operations don't
    722 	 * attempt to march to userspace any longer.
    723 	 */
    724 	pmp->pmp_status = PUFFSTAT_DYING;
    725 
    726 	/* signal waiters on REQUEST TO file server queue */
    727 	TAILQ_FOREACH(park, &pmp->pmp_req_touser, park_entries) {
    728 		uint8_t opclass;
    729 
    730 		opclass = park->park_preq->preq_opclass;
    731 		park->park_preq->preq_rv = ENXIO;
    732 		TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
    733 
    734 		if (park->park_flags & PARKFLAG_CALL) {
    735 			park->park_done(park->park_preq, park->park_donearg);
    736 			puffs_parkmem_free(park);
    737 		} else if (!PUFFSOP_WANTREPLY(opclass)) {
    738 			free(park->park_preq, M_PUFFS);
    739 			puffs_parkmem_free(park);
    740 		} else {
    741 			park->park_preq->preq_rv = ENXIO;
    742 			cv_signal(&park->park_cv);
    743 		}
    744 	}
    745 
    746 	/* signal waiters on RESPONSE FROM file server queue */
    747 	TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
    748 		TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
    749 		KASSERT(PUFFSOP_WANTREPLY(park->park_preq->preq_opclass));
    750 
    751 		park->park_preq->preq_rv = ENXIO;
    752 		if (park->park_flags & PARKFLAG_CALL) {
    753 			park->park_done(park->park_preq, park->park_donearg);
    754 			puffs_parkmem_free(park);
    755 		} else {
    756 			cv_signal(&park->park_cv);
    757 		}
    758 	}
    759 }
    760 
    761 /* this is probably going to die away at some point? */
    762 /*
    763  * XXX: currently bitrotted
    764  */
    765 #if 0
    766 static int
    767 puffssizeop(struct puffs_mount *pmp, struct puffs_sizeop *psop_user)
    768 {
    769 	struct puffs_sizepark *pspark;
    770 	void *kernbuf;
    771 	size_t copylen;
    772 	int error;
    773 
    774 	/* locate correct op */
    775 	mutex_enter(&pmp->pmp_lock);
    776 	TAILQ_FOREACH(pspark, &pmp->pmp_req_sizepark, pkso_entries) {
    777 		if (pspark->pkso_reqid == psop_user->pso_reqid) {
    778 			TAILQ_REMOVE(&pmp->pmp_req_sizepark, pspark,
    779 			    pkso_entries);
    780 			break;
    781 		}
    782 	}
    783 	mutex_exit(&pmp->pmp_lock);
    784 
    785 	if (pspark == NULL)
    786 		return EINVAL;
    787 
    788 	error = 0;
    789 	copylen = MIN(pspark->pkso_bufsize, psop_user->pso_bufsize);
    790 
    791 	/*
    792 	 * XXX: uvm stuff to avoid bouncy-bouncy copying?
    793 	 */
    794 	if (PUFFS_SIZEOP_UIO(pspark->pkso_reqtype)) {
    795 		kernbuf = malloc(copylen, M_PUFFS, M_WAITOK | M_ZERO);
    796 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_IN) {
    797 			error = copyin(psop_user->pso_userbuf,
    798 			    kernbuf, copylen);
    799 			if (error) {
    800 				printf("psop ERROR1 %d\n", error);
    801 				goto escape;
    802 			}
    803 		}
    804 		error = uiomove(kernbuf, copylen, pspark->pkso_uio);
    805 		if (error) {
    806 			printf("uiomove from kernel %p, len %d failed: %d\n",
    807 			    kernbuf, (int)copylen, error);
    808 			goto escape;
    809 		}
    810 
    811 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_OUT) {
    812 			error = copyout(kernbuf,
    813 			    psop_user->pso_userbuf, copylen);
    814 			if (error) {
    815 				printf("psop ERROR2 %d\n", error);
    816 				goto escape;
    817 			}
    818 		}
    819  escape:
    820 		free(kernbuf, M_PUFFS);
    821 	} else if (PUFFS_SIZEOP_BUF(pspark->pkso_reqtype)) {
    822 		copylen = MAX(pspark->pkso_bufsize, psop_user->pso_bufsize);
    823 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_BUF_IN) {
    824 			error = copyin(psop_user->pso_userbuf,
    825 			pspark->pkso_copybuf, copylen);
    826 		} else {
    827 			error = copyout(pspark->pkso_copybuf,
    828 			    psop_user->pso_userbuf, copylen);
    829 		}
    830 	}
    831 #ifdef DIAGNOSTIC
    832 	else
    833 		panic("puffssizeop: invalid reqtype %d\n",
    834 		    pspark->pkso_reqtype);
    835 #endif /* DIAGNOSTIC */
    836 
    837 	return error;
    838 }
    839 #endif
    840