Home | History | Annotate | Line # | Download | only in puffs
puffs_msgif.c revision 1.19.2.3
      1 /*	$NetBSD: puffs_msgif.c,v 1.19.2.3 2007/06/08 14:15:00 ad 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.19.2.3 2007/06/08 14:15:00 ad 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 #include <sys/proc.h>
     45 
     46 #include <fs/puffs/puffs_msgif.h>
     47 #include <fs/puffs/puffs_sys.h>
     48 
     49 /*
     50  * waitq data structures
     51  */
     52 
     53 /*
     54  * While a request is going to userspace, park the caller within the
     55  * kernel.  This is the kernel counterpart of "struct puffs_req".
     56  */
     57 struct puffs_park {
     58 	struct puffs_req	*park_preq;	/* req followed by buf	*/
     59 	uint64_t		park_id;	/* duplicate of preq_id */
     60 
     61 	size_t			park_copylen;	/* userspace copylength	*/
     62 	size_t			park_maxlen;	/* max size in comeback */
     63 
     64 	parkdone_fn		park_done;
     65 	void			*park_donearg;
     66 
     67 	int			park_flags;
     68 	int			park_refcount;
     69 
     70 	kcondvar_t		park_cv;
     71 	kmutex_t		park_mtx;
     72 
     73 	TAILQ_ENTRY(puffs_park) park_entries;
     74 };
     75 #define PARKFLAG_WAITERGONE	0x01
     76 #define PARKFLAG_DONE		0x02
     77 #define PARKFLAG_ONQUEUE1	0x04
     78 #define PARKFLAG_ONQUEUE2	0x08
     79 #define PARKFLAG_CALL		0x10
     80 #define PARKFLAG_WANTREPLY	0x20
     81 
     82 static struct pool_cache parkpc;
     83 static struct pool parkpool;
     84 
     85 static int
     86 makepark(void *arg, void *obj, int flags)
     87 {
     88 	struct puffs_park *park = obj;
     89 
     90 	mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
     91 	cv_init(&park->park_cv, "puffsrpl");
     92 
     93 	return 0;
     94 }
     95 
     96 static void
     97 nukepark(void *arg, void *obj)
     98 {
     99 	struct puffs_park *park = obj;
    100 
    101 	cv_destroy(&park->park_cv);
    102 	mutex_destroy(&park->park_mtx);
    103 }
    104 
    105 void
    106 puffs_msgif_init()
    107 {
    108 
    109 	pool_init(&parkpool, sizeof(struct puffs_park), 0, 0, 0,
    110 	    "puffprkl", &pool_allocator_nointr, IPL_NONE);
    111 	pool_cache_init(&parkpc, &parkpool, makepark, nukepark, NULL);
    112 }
    113 
    114 void
    115 puffs_msgif_destroy()
    116 {
    117 
    118 	pool_cache_destroy(&parkpc);
    119 	pool_destroy(&parkpool);
    120 }
    121 
    122 void *
    123 puffs_park_alloc(int waitok)
    124 {
    125 	struct puffs_park *park;
    126 
    127 	park = pool_cache_get(&parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
    128 	if (park) {
    129 		park->park_refcount = 1;
    130 		mutex_enter(&park->park_mtx);
    131 	}
    132 
    133 	return park;
    134 }
    135 
    136 static void
    137 puffs_park_reference(struct puffs_park *park)
    138 {
    139 
    140 	mutex_enter(&park->park_mtx);
    141 	park->park_refcount++;
    142 }
    143 
    144 void
    145 puffs_park_release(void *arg, int fullnuke)
    146 {
    147 	struct puffs_park *park = arg;
    148 
    149 	KASSERT(mutex_owned(&park->park_mtx));
    150 	--park->park_refcount;
    151 
    152 	mutex_exit(&park->park_mtx);
    153 	if (park->park_refcount == 0 || fullnuke)
    154 		pool_cache_put(&parkpc, park);
    155 }
    156 
    157 #ifdef PUFFSDEBUG
    158 static void
    159 parkdump(struct puffs_park *park)
    160 {
    161 
    162 	DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
    163 	    "\tcopy %zu, max %zu - done: %p/%p\n"
    164 	    "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
    165 	    park, park->park_preq, park->park_id,
    166 	    park->park_copylen, park->park_maxlen,
    167 	    park->park_done, park->park_donearg,
    168 	    park->park_flags, park->park_refcount,
    169 	    &park->park_cv, &park->park_mtx));
    170 }
    171 
    172 static void
    173 parkqdump(struct puffs_wq *q, int dumpall)
    174 {
    175 	struct puffs_park *park;
    176 	int total = 0;
    177 
    178 	TAILQ_FOREACH(park, q, park_entries) {
    179 		if (dumpall)
    180 			parkdump(park);
    181 		total++;
    182 	}
    183 	DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
    184 
    185 }
    186 #endif /* PUFFSDEBUG */
    187 
    188 /*
    189  * Converts a non-FAF op to a FAF.  This simply involves making copies
    190  * of the park and request structures and tagging the request as a FAF.
    191  * It is safe to block here, since the original op is not a FAF.
    192  */
    193 static void
    194 puffs_reqtofaf(struct puffs_park *park)
    195 {
    196 	struct puffs_req *newpreq;
    197 
    198 	KASSERT((park->park_preq->preq_opclass & PUFFSOPFLAG_FAF) == 0);
    199 
    200 	MALLOC(newpreq, struct puffs_req *, park->park_copylen,
    201 	    M_PUFFS, M_ZERO | M_WAITOK);
    202 
    203 	memcpy(newpreq, park->park_preq, park->park_copylen);
    204 
    205 	park->park_preq = newpreq;
    206 	park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
    207 	park->park_flags &= ~PARKFLAG_WANTREPLY;
    208 }
    209 
    210 
    211 /*
    212  * kernel-user-kernel waitqueues
    213  */
    214 
    215 static int touser(struct puffs_mount *, struct puffs_park *, uint64_t);
    216 
    217 uint64_t
    218 puffs_getreqid(struct puffs_mount *pmp)
    219 {
    220 	uint64_t rv;
    221 
    222 	mutex_enter(&pmp->pmp_lock);
    223 	rv = pmp->pmp_nextreq++;
    224 	mutex_exit(&pmp->pmp_lock);
    225 
    226 	return rv;
    227 }
    228 
    229 /* vfs request */
    230 int
    231 puffs_vfstouser(struct puffs_mount *pmp, int optype, void *kbuf, size_t buflen)
    232 {
    233 	struct puffs_park *park;
    234 
    235 	park = puffs_park_alloc(1);
    236 	park->park_preq = kbuf;
    237 
    238 	park->park_preq->preq_opclass = PUFFSOP_VFS;
    239 	park->park_preq->preq_optype = optype;
    240 
    241 	park->park_maxlen = park->park_copylen = buflen;
    242 	park->park_flags = 0;
    243 
    244 	return touser(pmp, park, puffs_getreqid(pmp));
    245 }
    246 
    247 void
    248 puffs_suspendtouser(struct puffs_mount *pmp, int status)
    249 {
    250 	struct puffs_vfsreq_suspend *pvfsr_susp;
    251 	struct puffs_park *park;
    252 
    253 	pvfsr_susp = malloc(sizeof(struct puffs_vfsreq_suspend),
    254 	    M_PUFFS, M_WAITOK | M_ZERO);
    255 	park = puffs_park_alloc(1);
    256 
    257 	pvfsr_susp->pvfsr_status = status;
    258 	park->park_preq = (struct puffs_req *)pvfsr_susp;
    259 
    260 	park->park_preq->preq_opclass = PUFFSOP_VFS | PUFFSOPFLAG_FAF;
    261 	park->park_preq->preq_optype = PUFFS_VFS_SUSPEND;
    262 
    263 	park->park_maxlen = park->park_copylen
    264 	    = sizeof(struct puffs_vfsreq_suspend);
    265 	park->park_flags = 0;
    266 
    267 	(void)touser(pmp, park, 0);
    268 }
    269 
    270 /*
    271  * vnode level request
    272  */
    273 int
    274 puffs_vntouser(struct puffs_mount *pmp, int optype,
    275 	void *kbuf, size_t buflen, size_t maxdelta,
    276 	struct vnode *vp_opc, struct vnode *vp_aux)
    277 {
    278 	struct puffs_park *park;
    279 	struct puffs_req *preq;
    280 	void *cookie = VPTOPNC(vp_opc);
    281 	struct puffs_node *pnode;
    282 	int rv;
    283 
    284 	park = puffs_park_alloc(1);
    285 	park->park_preq = kbuf;
    286 
    287 	park->park_preq->preq_opclass = PUFFSOP_VN;
    288 	park->park_preq->preq_optype = optype;
    289 	park->park_preq->preq_cookie = cookie;
    290 
    291 	park->park_copylen = buflen;
    292 	park->park_maxlen = buflen + maxdelta;
    293 	park->park_flags = 0;
    294 
    295 	rv = touser(pmp, park, puffs_getreqid(pmp));
    296 
    297 	/*
    298 	 * Check if the user server requests that inactive be called
    299 	 * when the time is right.
    300 	 */
    301 	preq = park->park_preq;
    302 	if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N1) {
    303 		pnode = vp_opc->v_data;
    304 		pnode->pn_stat |= PNODE_DOINACT;
    305 	}
    306 	if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N2) {
    307 		/* if no vp_aux, just ignore */
    308 		if (vp_aux) {
    309 			pnode = vp_aux->v_data;
    310 			pnode->pn_stat |= PNODE_DOINACT;
    311 		}
    312 	}
    313 	if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1) {
    314 		pnode = vp_opc->v_data;
    315 		pnode->pn_stat |= PNODE_NOREFS;
    316 	}
    317 	if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2) {
    318 		/* if no vp_aux, just ignore */
    319 		if (vp_aux) {
    320 			pnode = vp_aux->v_data;
    321 			pnode->pn_stat |= PNODE_NOREFS;
    322 		}
    323 	}
    324 
    325 	return rv;
    326 }
    327 
    328 /*
    329  * vnode level request, caller-controller req id
    330  */
    331 int
    332 puffs_vntouser_req(struct puffs_mount *pmp, int optype,
    333 	void *kbuf, size_t buflen, size_t maxdelta,
    334 	uint64_t reqid, struct vnode *vp_opc, struct vnode *vp_aux)
    335 {
    336 	struct puffs_park *park;
    337 	void *cookie = VPTOPNC(vp_opc);
    338 
    339 	park = puffs_park_alloc(1);
    340 	park->park_preq = kbuf;
    341 
    342 	park->park_preq->preq_opclass = PUFFSOP_VN;
    343 	park->park_preq->preq_optype = optype;
    344 	park->park_preq->preq_cookie = cookie;
    345 
    346 	park->park_copylen = buflen;
    347 	park->park_maxlen = buflen + maxdelta;
    348 	park->park_flags = 0;
    349 
    350 	return touser(pmp, park, reqid);
    351 }
    352 
    353 void
    354 puffs_vntouser_call(struct puffs_mount *pmp, int optype,
    355 	void *kbuf, size_t buflen, size_t maxdelta,
    356 	parkdone_fn donefn, void *donearg,
    357 	struct vnode *vp_opc, struct vnode *vp_aux)
    358 {
    359 	struct puffs_park *park;
    360 	void *cookie = VPTOPNC(vp_opc);
    361 
    362 	park = puffs_park_alloc(1);
    363 	park->park_preq = kbuf;
    364 
    365 	park->park_preq->preq_opclass = PUFFSOP_VN;
    366 	park->park_preq->preq_optype = optype;
    367 	park->park_preq->preq_cookie = cookie;
    368 
    369 	park->park_copylen = buflen;
    370 	park->park_maxlen = buflen + maxdelta;
    371 	park->park_done = donefn;
    372 	park->park_donearg = donearg;
    373 	park->park_flags = PARKFLAG_CALL;
    374 
    375 	(void) touser(pmp, park, puffs_getreqid(pmp));
    376 }
    377 
    378 /*
    379  * Notice: kbuf will be free'd later.  I must be allocated from the
    380  * kernel heap and it's ownership is shifted to this function from
    381  * now on, i.e. the caller is not allowed to use it anymore!
    382  */
    383 void
    384 puffs_vntouser_faf(struct puffs_mount *pmp, int optype,
    385 	void *kbuf, size_t buflen, struct vnode *vp_opc)
    386 {
    387 	struct puffs_park *park;
    388 	void *cookie = VPTOPNC(vp_opc);
    389 
    390 	/* XXX: is it allowable to sleep here? */
    391 	park = puffs_park_alloc(0);
    392 	if (park == NULL)
    393 		return; /* 2bad */
    394 
    395 	park->park_preq = kbuf;
    396 
    397 	park->park_preq->preq_opclass = PUFFSOP_VN | PUFFSOPFLAG_FAF;
    398 	park->park_preq->preq_optype = optype;
    399 	park->park_preq->preq_cookie = cookie;
    400 
    401 	park->park_maxlen = park->park_copylen = buflen;
    402 	park->park_flags = 0;
    403 
    404 	(void)touser(pmp, park, 0);
    405 }
    406 
    407 void
    408 puffs_cacheop(struct puffs_mount *pmp, struct puffs_park *park,
    409 	struct puffs_cacheinfo *pcinfo, size_t pcilen, void *cookie)
    410 {
    411 
    412 	park->park_preq = (struct puffs_req *)pcinfo;
    413 	park->park_preq->preq_opclass = PUFFSOP_CACHE | PUFFSOPFLAG_FAF;
    414 	park->park_preq->preq_optype = PCACHE_TYPE_WRITE; /* XXX */
    415 	park->park_preq->preq_cookie = cookie;
    416 
    417 	park->park_maxlen = park->park_copylen = pcilen;
    418 	park->park_flags = 0;
    419 
    420 	(void)touser(pmp, park, 0);
    421 }
    422 
    423 /*
    424  * Wait for the userspace ping-pong game in calling process context.
    425  *
    426  * This unlocks vnodes if they are supplied.  vp1 is the vnode
    427  * before in the locking order, i.e. the one which must be locked
    428  * before accessing vp2.  This is done here so that operations are
    429  * already ordered in the queue when vnodes are unlocked (I'm not
    430  * sure if that's really necessary, but it can't hurt).  Okok, maybe
    431  * there's a slight ugly-factor also, but let's not worry about that.
    432  */
    433 static int
    434 touser(struct puffs_mount *pmp, struct puffs_park *park, uint64_t reqid)
    435 {
    436 	struct lwp *l = curlwp;
    437 	struct mount *mp;
    438 	struct puffs_req *preq;
    439 	int rv = 0;
    440 
    441 	mp = PMPTOMP(pmp);
    442 	preq = park->park_preq;
    443 	preq->preq_id = park->park_id = reqid;
    444 	preq->preq_buflen = ALIGN(park->park_maxlen);
    445 
    446 	if (PUFFSOP_WANTREPLY(preq->preq_opclass))
    447 		park->park_flags |= PARKFLAG_WANTREPLY;
    448 
    449 	/*
    450 	 * To support PCATCH, yet another movie: check if there are signals
    451 	 * pending and we are issueing a non-FAF.  If so, return an error
    452 	 * directly UNLESS we are issueing INACTIVE.  In that case, convert
    453 	 * it to a FAF, fire off to the file server and return an error.
    454 	 * Yes, this is bordering disgusting.  Barfbags are on me.
    455 	 */
    456 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    457 	   && (park->park_flags & PARKFLAG_CALL) == 0
    458 	   && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    459 		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
    460 		    && preq->preq_optype == PUFFS_VN_INACTIVE) {
    461 			puffs_reqtofaf(park);
    462 			DPRINTF(("puffs touser: converted to FAF %p\n", park));
    463 			rv = EINTR;
    464 		} else {
    465 			puffs_park_release(park, 0);
    466 			return EINTR;
    467 		}
    468 	}
    469 
    470 	/*
    471 	 * test for suspension lock.
    472 	 *
    473 	 * Note that we *DO NOT* keep the lock, since that might block
    474 	 * lock acquiring PLUS it would give userlandia control over
    475 	 * the lock.  The operation queue enforces a strict ordering:
    476 	 * when the fs server gets in the op stream, it knows things
    477 	 * are in order.  The kernel locks can't guarantee that for
    478 	 * userspace, in any case.
    479 	 *
    480 	 * BUT: this presents a problem for ops which have a consistency
    481 	 * clause based on more than one operation.  Unfortunately such
    482 	 * operations (read, write) do not reliably work yet.
    483 	 *
    484 	 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
    485 	 *
    486 	 * XXX: and there is one more problem.  We sometimes need to
    487 	 * take a lazy lock in case the fs is suspending and we are
    488 	 * executing as the fs server context.  This might happen
    489 	 * e.g. in the case that the user server triggers a reclaim
    490 	 * in the kernel while the fs is suspending.  It's not a very
    491 	 * likely event, but it needs to be fixed some day.
    492 	 */
    493 
    494 	/*
    495 	 * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
    496 	 * the mutex here, since getpages() might be called locked.
    497 	 */
    498 	fstrans_start(mp, FSTRANS_NORMAL);
    499 	mutex_enter(&pmp->pmp_lock);
    500 	fstrans_done(mp);
    501 
    502 	if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    503 		mutex_exit(&pmp->pmp_lock);
    504 		puffs_park_release(park, 0);
    505 		return ENXIO;
    506 	}
    507 
    508 #ifdef PUFFSDEBUG
    509 	parkqdump(&pmp->pmp_req_touser, puffsdebug > 1);
    510 	parkqdump(&pmp->pmp_req_replywait, puffsdebug > 1);
    511 #endif
    512 
    513 	TAILQ_INSERT_TAIL(&pmp->pmp_req_touser, park, park_entries);
    514 	park->park_flags |= PARKFLAG_ONQUEUE1;
    515 	puffs_mp_reference(pmp);
    516 	pmp->pmp_req_touser_count++;
    517 	mutex_exit(&pmp->pmp_lock);
    518 
    519 	DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
    520 	    "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
    521 	    preq->preq_opclass, preq->preq_optype, park->park_flags));
    522 
    523 	cv_broadcast(&pmp->pmp_req_waiter_cv);
    524 	selnotify(pmp->pmp_sel, 0);
    525 
    526 	if ((park->park_flags & PARKFLAG_WANTREPLY)
    527 	    && (park->park_flags & PARKFLAG_CALL) == 0) {
    528 		int error;
    529 
    530 		error = cv_wait_sig(&park->park_cv, &park->park_mtx);
    531 		if (error) {
    532 			park->park_flags |= PARKFLAG_WAITERGONE;
    533 			if (park->park_flags & PARKFLAG_DONE) {
    534 				rv = preq->preq_rv;
    535 				puffs_park_release(park, 0);
    536 			} else {
    537 				/*
    538 				 * ok, we marked it as going away, but
    539 				 * still need to do queue ops.  take locks
    540 				 * in correct order.
    541 				 *
    542 				 * We don't want to release our reference
    543 				 * if it's on replywait queue to avoid error
    544 				 * to file server.  putop() code will DTRT.
    545 				 */
    546 				KASSERT(park->park_flags &
    547 				    (PARKFLAG_ONQUEUE1 | PARKFLAG_ONQUEUE2));
    548 				mutex_exit(&park->park_mtx);
    549 
    550 				mutex_enter(&pmp->pmp_lock);
    551 				mutex_enter(&park->park_mtx);
    552 				if (park->park_flags & PARKFLAG_ONQUEUE1) {
    553 					TAILQ_REMOVE(&pmp->pmp_req_touser,
    554 					    park, park_entries);
    555 					pmp->pmp_req_touser_count--;
    556 					park->park_flags &= ~PARKFLAG_ONQUEUE1;
    557 				}
    558 				if ((park->park_flags & PARKFLAG_ONQUEUE2) == 0)
    559 					puffs_park_release(park, 0);
    560 				else
    561 					mutex_exit(&park->park_mtx);
    562 				mutex_exit(&pmp->pmp_lock);
    563 
    564 				rv = error;
    565 			}
    566 		} else {
    567 			rv = preq->preq_rv;
    568 			puffs_park_release(park, 0);
    569 		}
    570 
    571 		/*
    572 		 * retake the lock and release.  This makes sure (haha,
    573 		 * I'm humorous) that we don't process the same vnode in
    574 		 * multiple threads due to the locks hacks we have in
    575 		 * puffs_lock().  In reality this is well protected by
    576 		 * the biglock, but once that's gone, well, hopefully
    577 		 * this will be fixed for real.  (and when you read this
    578 		 * comment in 2017 and subsequently barf, my condolences ;).
    579 		 */
    580 		if (rv == 0 && !fstrans_is_owner(mp)) {
    581 			fstrans_start(mp, FSTRANS_NORMAL);
    582 			fstrans_done(mp);
    583 		}
    584 	} else {
    585 		mutex_exit(&park->park_mtx);
    586 	}
    587 
    588 	mutex_enter(&pmp->pmp_lock);
    589 	puffs_mp_release(pmp);
    590 	mutex_exit(&pmp->pmp_lock);
    591 
    592 	return rv;
    593 }
    594 
    595 
    596 /*
    597  * getop: scan through queued requests until:
    598  *  1) max number of requests satisfied
    599  *     OR
    600  *  2) buffer runs out of space
    601  *     OR
    602  *  3) nonblocking is set AND there are no operations available
    603  *     OR
    604  *  4) at least one operation was transferred AND there are no more waiting
    605  */
    606 int
    607 puffs_getop(struct puffs_mount *pmp, struct puffs_reqh_get *phg, int nonblock)
    608 {
    609 	struct puffs_park *park;
    610 	struct puffs_req *preq;
    611 	uint8_t *bufpos;
    612 	int error, donesome;
    613 
    614 	donesome = error = 0;
    615 	bufpos = phg->phg_buf;
    616 
    617 	mutex_enter(&pmp->pmp_lock);
    618 	while (phg->phg_nops == 0 || donesome != phg->phg_nops) {
    619  again:
    620 		if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    621 			/* if we got some, they don't really matter anymore */
    622 			error = ENXIO;
    623 			goto out;
    624 		}
    625 		if (TAILQ_EMPTY(&pmp->pmp_req_touser)) {
    626 			if (donesome)
    627 				goto out;
    628 
    629 			if (nonblock) {
    630 				error = EWOULDBLOCK;
    631 				goto out;
    632 			}
    633 
    634 			error = cv_wait_sig(&pmp->pmp_req_waiter_cv,
    635 			    &pmp->pmp_lock);
    636 			if (error)
    637 				goto out;
    638 			else
    639 				goto again;
    640 		}
    641 
    642 		park = TAILQ_FIRST(&pmp->pmp_req_touser);
    643 		puffs_park_reference(park);
    644 
    645 		/* If it's a goner, don't process any furher */
    646 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    647 			puffs_park_release(park, 0);
    648 			continue;
    649 		}
    650 
    651 		preq = park->park_preq;
    652 		if (phg->phg_buflen < preq->preq_buflen) {
    653 			if (!donesome)
    654 				error = E2BIG;
    655 			puffs_park_release(park, 0);
    656 			goto out;
    657 		}
    658 
    659 		TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
    660 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    661 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    662 		pmp->pmp_req_touser_count--;
    663 		KASSERT(pmp->pmp_req_touser_count >= 0);
    664 		mutex_exit(&pmp->pmp_lock);
    665 
    666 		DPRINTF(("puffsgetop: get op %" PRIu64 " (%d.), from %p "
    667 		    "len %zu (buflen %zu), target %p\n", preq->preq_id,
    668 		    donesome, preq, park->park_copylen, preq->preq_buflen,
    669 		    bufpos));
    670 
    671 		if ((error = copyout(preq, bufpos, park->park_copylen)) != 0) {
    672 			DPRINTF(("puffs_getop: copyout failed: %d\n", error));
    673 			/*
    674 			 * ok, user server is probably trying to cheat.
    675 			 * stuff op back & return error to user.  We need
    676 			 * to take locks in the correct order.
    677 			 */
    678 			mutex_exit(&park->park_mtx);
    679 
    680 			/*
    681 			 * XXX: ONQUEUE1 | ONQUEUE2 invariant doesn't
    682 			 * hold here
    683 			 */
    684 
    685 			mutex_enter(&pmp->pmp_lock);
    686 			mutex_enter(&park->park_mtx);
    687 			if ((park->park_flags & PARKFLAG_WAITERGONE) == 0) {
    688 				 TAILQ_INSERT_HEAD(&pmp->pmp_req_touser, park,
    689 				     park_entries);
    690 				 park->park_flags |= PARKFLAG_ONQUEUE1;
    691 				 pmp->pmp_req_touser_count++;
    692 			}
    693 
    694 			if (donesome)
    695 				error = 0;
    696 			puffs_park_release(park, 0);
    697 			goto out;
    698 		}
    699 		bufpos += preq->preq_buflen;
    700 		phg->phg_buflen -= preq->preq_buflen;
    701 		donesome++;
    702 
    703 		/* XXXfixme: taking this lock in the wrong order */
    704 		mutex_enter(&pmp->pmp_lock);
    705 
    706 		if (park->park_flags & PARKFLAG_WANTREPLY) {
    707 			TAILQ_INSERT_TAIL(&pmp->pmp_req_replywait, park,
    708 			    park_entries);
    709 			park->park_flags |= PARKFLAG_ONQUEUE2;
    710 			puffs_park_release(park, 0);
    711 		} else {
    712 			free(preq, M_PUFFS);
    713 			puffs_park_release(park, 1);
    714 		}
    715 	}
    716 
    717  out:
    718 	phg->phg_more = pmp->pmp_req_touser_count;
    719 	mutex_exit(&pmp->pmp_lock);
    720 
    721 	phg->phg_nops = donesome;
    722 
    723 	return error;
    724 }
    725 
    726 int
    727 puffs_putop(struct puffs_mount *pmp, struct puffs_reqh_put *php)
    728 {
    729 	struct puffs_park *park;
    730 	struct puffs_req tmpreq;
    731 	struct puffs_req *nextpreq;
    732 	void *userbuf;
    733 	uint64_t id;
    734 	size_t reqlen;
    735 	int donesome, error, wgone, release;
    736 
    737 	donesome = error = wgone = 0;
    738 
    739 	id = php->php_id;
    740 	userbuf = php->php_buf;
    741 	reqlen = php->php_buflen;
    742 
    743 	mutex_enter(&pmp->pmp_lock);
    744 	while (donesome != php->php_nops) {
    745 		release = 0;
    746 #ifdef PUFFSDEBUG
    747 		DPRINTF(("puffsputop: searching for %" PRIu64 ", ubuf: %p, "
    748 		    "len %zu\n", id, userbuf, reqlen));
    749 #endif
    750 		TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
    751 			if (park->park_id == id)
    752 				break;
    753 		}
    754 
    755 		if (park == NULL) {
    756 			DPRINTF(("puffsputop: no request: %" PRIu64 "\n", id));
    757 			error = EINVAL;
    758 			break;
    759 		}
    760 
    761 		puffs_park_reference(park);
    762 		if (reqlen == 0 || reqlen > park->park_maxlen) {
    763 			DPRINTF(("puffsputop: invalid buffer length: "
    764 			    "%zu\n", reqlen));
    765 			error = E2BIG;
    766 			puffs_park_release(park, 0);
    767 			break;
    768 		}
    769 		wgone = park->park_flags & PARKFLAG_WAITERGONE;
    770 
    771 		/* check if it's still on the queue after acquiring lock */
    772 		if (park->park_flags & PARKFLAG_ONQUEUE2) {
    773 			TAILQ_REMOVE(&pmp->pmp_req_replywait, park,
    774 			    park_entries);
    775 			park->park_flags &= ~PARKFLAG_ONQUEUE2;
    776 		}
    777 
    778 		mutex_exit(&pmp->pmp_lock);
    779 
    780 		/*
    781 		 * If the caller has gone south, go to next, collect
    782 		 * $200 and free the structure there instead of wakeup.
    783 		 * We also need to copyin the header info.  Flag structure
    784 		 * release to mode total and utter destruction.
    785 		 */
    786 		if (wgone) {
    787 			DPRINTF(("puffs_putop: bad service - waiter gone for "
    788 			    "park %p\n", park));
    789 			error = copyin(userbuf, &tmpreq,
    790 			    sizeof(struct puffs_req));
    791 			release = 1;
    792 			if (error)
    793 				goto loopout;
    794 			nextpreq = &tmpreq;
    795 			goto next;
    796 		}
    797 
    798 		DPRINTF(("puffsputpop: copyin from %p to %p, len %zu\n",
    799 		    userbuf, park->park_preq, reqlen));
    800 		error = copyin(userbuf, park->park_preq, reqlen);
    801 		if (error)
    802 			goto loopout;
    803 		nextpreq = park->park_preq;
    804 
    805  next:
    806 		/* all's well, prepare for next op */
    807 		id = nextpreq->preq_id;
    808 		reqlen = nextpreq->preq_buflen;
    809 		userbuf = nextpreq->preq_nextbuf;
    810 		donesome++;
    811 
    812  loopout:
    813 		if (error && !wgone)
    814 			park->park_preq->preq_rv = error;
    815 
    816 		if (park->park_flags & PARKFLAG_CALL) {
    817 			park->park_done(park->park_preq, park->park_donearg);
    818 			release = 1;
    819 		}
    820 
    821 		if (!wgone) {
    822 			DPRINTF(("puffs_putop: flagging done for "
    823 			    "park %p\n", park));
    824 
    825 			cv_signal(&park->park_cv);
    826 		}
    827 		park->park_flags |= PARKFLAG_DONE;
    828 		puffs_park_release(park, release);
    829 
    830 		mutex_enter(&pmp->pmp_lock);
    831 		if (error)
    832 			break;
    833 		wgone = 0;
    834 	}
    835 
    836 	mutex_exit(&pmp->pmp_lock);
    837 	php->php_nops -= donesome;
    838 
    839 	return error;
    840 }
    841 
    842 /*
    843  * We're dead, kaput, RIP, slightly more than merely pining for the
    844  * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
    845  * our maker, ceased to be, etcetc.  YASD.  It's a dead FS!
    846  *
    847  * Caller must hold puffs mutex.
    848  */
    849 void
    850 puffs_userdead(struct puffs_mount *pmp)
    851 {
    852 	struct puffs_park *park, *park_next;
    853 
    854 	/*
    855 	 * Mark filesystem status as dying so that operations don't
    856 	 * attempt to march to userspace any longer.
    857 	 */
    858 	pmp->pmp_status = PUFFSTAT_DYING;
    859 
    860 	/* signal waiters on REQUEST TO file server queue */
    861 	for (park = TAILQ_FIRST(&pmp->pmp_req_touser); park; park = park_next) {
    862 		uint8_t opclass;
    863 
    864 		puffs_park_reference(park);
    865 		park_next = TAILQ_NEXT(park, park_entries);
    866 
    867 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
    868 		TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
    869 		park->park_flags &= ~PARKFLAG_ONQUEUE1;
    870 		pmp->pmp_req_touser_count--;
    871 
    872 		/*
    873 		 * If the waiter is gone, we may *NOT* access preq anymore.
    874 		 */
    875 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    876 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    877 			KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    878 			puffs_park_release(park, 0);
    879 		} else {
    880 			opclass = park->park_preq->preq_opclass;
    881 			park->park_preq->preq_rv = ENXIO;
    882 
    883 			if (park->park_flags & PARKFLAG_CALL) {
    884 				park->park_done(park->park_preq,
    885 				    park->park_donearg);
    886 				puffs_park_release(park, 1);
    887 			} else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
    888 				free(park->park_preq, M_PUFFS);
    889 				puffs_park_release(park, 1);
    890 			} else {
    891 				park->park_preq->preq_rv = ENXIO;
    892 				cv_signal(&park->park_cv);
    893 				puffs_park_release(park, 0);
    894 			}
    895 		}
    896 	}
    897 
    898 	/* signal waiters on RESPONSE FROM file server queue */
    899 	for (park=TAILQ_FIRST(&pmp->pmp_req_replywait); park; park=park_next) {
    900 		puffs_park_reference(park);
    901 		park_next = TAILQ_NEXT(park, park_entries);
    902 
    903 		KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
    904 		KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
    905 
    906 		TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
    907 		park->park_flags &= ~PARKFLAG_ONQUEUE2;
    908 
    909 		/*
    910 		 * If the waiter is gone, we may *NOT* access preq anymore.
    911 		 */
    912 		if (park->park_flags & PARKFLAG_WAITERGONE) {
    913 			KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
    914 			puffs_park_release(park, 0);
    915 		} else {
    916 			park->park_preq->preq_rv = ENXIO;
    917 			if (park->park_flags & PARKFLAG_CALL) {
    918 				park->park_done(park->park_preq,
    919 				    park->park_donearg);
    920 				puffs_park_release(park, 1);
    921 			} else {
    922 				cv_signal(&park->park_cv);
    923 				puffs_park_release(park, 0);
    924 			}
    925 		}
    926 	}
    927 }
    928 
    929 /* this is probably going to die away at some point? */
    930 /*
    931  * XXX: currently bitrotted
    932  */
    933 #if 0
    934 static int
    935 puffssizeop(struct puffs_mount *pmp, struct puffs_sizeop *psop_user)
    936 {
    937 	struct puffs_sizepark *pspark;
    938 	void *kernbuf;
    939 	size_t copylen;
    940 	int error;
    941 
    942 	/* locate correct op */
    943 	mutex_enter(&pmp->pmp_lock);
    944 	TAILQ_FOREACH(pspark, &pmp->pmp_req_sizepark, pkso_entries) {
    945 		if (pspark->pkso_reqid == psop_user->pso_reqid) {
    946 			TAILQ_REMOVE(&pmp->pmp_req_sizepark, pspark,
    947 			    pkso_entries);
    948 			break;
    949 		}
    950 	}
    951 	mutex_exit(&pmp->pmp_lock);
    952 
    953 	if (pspark == NULL)
    954 		return EINVAL;
    955 
    956 	error = 0;
    957 	copylen = MIN(pspark->pkso_bufsize, psop_user->pso_bufsize);
    958 
    959 	/*
    960 	 * XXX: uvm stuff to avoid bouncy-bouncy copying?
    961 	 */
    962 	if (PUFFS_SIZEOP_UIO(pspark->pkso_reqtype)) {
    963 		kernbuf = malloc(copylen, M_PUFFS, M_WAITOK | M_ZERO);
    964 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_IN) {
    965 			error = copyin(psop_user->pso_userbuf,
    966 			    kernbuf, copylen);
    967 			if (error) {
    968 				printf("psop ERROR1 %d\n", error);
    969 				goto escape;
    970 			}
    971 		}
    972 		error = uiomove(kernbuf, copylen, pspark->pkso_uio);
    973 		if (error) {
    974 			printf("uiomove from kernel %p, len %d failed: %d\n",
    975 			    kernbuf, (int)copylen, error);
    976 			goto escape;
    977 		}
    978 
    979 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_OUT) {
    980 			error = copyout(kernbuf,
    981 			    psop_user->pso_userbuf, copylen);
    982 			if (error) {
    983 				printf("psop ERROR2 %d\n", error);
    984 				goto escape;
    985 			}
    986 		}
    987  escape:
    988 		free(kernbuf, M_PUFFS);
    989 	} else if (PUFFS_SIZEOP_BUF(pspark->pkso_reqtype)) {
    990 		copylen = MAX(pspark->pkso_bufsize, psop_user->pso_bufsize);
    991 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_BUF_IN) {
    992 			error = copyin(psop_user->pso_userbuf,
    993 			pspark->pkso_copybuf, copylen);
    994 		} else {
    995 			error = copyout(pspark->pkso_copybuf,
    996 			    psop_user->pso_userbuf, copylen);
    997 		}
    998 	}
    999 #ifdef DIAGNOSTIC
   1000 	else
   1001 		panic("puffssizeop: invalid reqtype %d\n",
   1002 		    pspark->pkso_reqtype);
   1003 #endif /* DIAGNOSTIC */
   1004 
   1005 	return error;
   1006 }
   1007 #endif
   1008