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