Home | History | Annotate | Line # | Download | only in puffs
puffs_msgif.c revision 1.19.2.1
      1  1.19.2.1       ad /*	$NetBSD: puffs_msgif.c,v 1.19.2.1 2007/04/05 21:57:48 ad Exp $	*/
      2       1.1    pooka 
      3       1.1    pooka /*
      4      1.16    pooka  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
      5       1.1    pooka  *
      6       1.1    pooka  * Development of this software was supported by the
      7       1.1    pooka  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8       1.1    pooka  * The Google SoC project was mentored by Bill Studenmund.
      9       1.1    pooka  *
     10       1.1    pooka  * Redistribution and use in source and binary forms, with or without
     11       1.1    pooka  * modification, are permitted provided that the following conditions
     12       1.1    pooka  * are met:
     13       1.1    pooka  * 1. Redistributions of source code must retain the above copyright
     14       1.1    pooka  *    notice, this list of conditions and the following disclaimer.
     15       1.1    pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1    pooka  *    notice, this list of conditions and the following disclaimer in the
     17       1.1    pooka  *    documentation and/or other materials provided with the distribution.
     18       1.1    pooka  * 3. The name of the company nor the name of the author may be used to
     19       1.1    pooka  *    endorse or promote products derived from this software without specific
     20       1.1    pooka  *    prior written permission.
     21       1.1    pooka  *
     22       1.1    pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23       1.1    pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24       1.1    pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25       1.1    pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     26       1.1    pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1    pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28       1.1    pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1    pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1    pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1    pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1    pooka  * SUCH DAMAGE.
     33       1.1    pooka  */
     34       1.1    pooka 
     35       1.1    pooka #include <sys/cdefs.h>
     36  1.19.2.1       ad __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.19.2.1 2007/04/05 21:57:48 ad Exp $");
     37       1.1    pooka 
     38       1.1    pooka #include <sys/param.h>
     39      1.16    pooka #include <sys/fstrans.h>
     40       1.1    pooka #include <sys/malloc.h>
     41       1.1    pooka #include <sys/mount.h>
     42       1.1    pooka #include <sys/vnode.h>
     43       1.1    pooka #include <sys/lock.h>
     44  1.19.2.1       ad #include <sys/proc.h>
     45       1.1    pooka 
     46       1.1    pooka #include <fs/puffs/puffs_msgif.h>
     47       1.1    pooka #include <fs/puffs/puffs_sys.h>
     48       1.1    pooka 
     49       1.1    pooka 
     50       1.1    pooka /*
     51       1.1    pooka  * kernel-user-kernel waitqueues
     52       1.1    pooka  */
     53       1.1    pooka 
     54       1.4    pooka static int touser(struct puffs_mount *, struct puffs_park *, uint64_t,
     55       1.1    pooka 		  struct vnode *, struct vnode *);
     56       1.1    pooka 
     57       1.4    pooka uint64_t
     58       1.1    pooka puffs_getreqid(struct puffs_mount *pmp)
     59       1.1    pooka {
     60      1.14    pooka 	uint64_t rv;
     61       1.1    pooka 
     62       1.1    pooka 	simple_lock(&pmp->pmp_lock);
     63       1.1    pooka 	rv = pmp->pmp_nextreq++;
     64       1.1    pooka 	simple_unlock(&pmp->pmp_lock);
     65       1.1    pooka 
     66       1.1    pooka 	return rv;
     67       1.1    pooka }
     68       1.1    pooka 
     69       1.1    pooka /* vfs request */
     70       1.1    pooka int
     71       1.1    pooka puffs_vfstouser(struct puffs_mount *pmp, int optype, void *kbuf, size_t buflen)
     72       1.1    pooka {
     73       1.1    pooka 	struct puffs_park park;
     74       1.1    pooka 
     75       1.9    pooka 	park.park_preq = kbuf;
     76       1.1    pooka 
     77       1.9    pooka 	park.park_preq->preq_opclass = PUFFSOP_VFS;
     78       1.9    pooka 	park.park_preq->preq_optype = optype;
     79       1.1    pooka 
     80       1.9    pooka 	park.park_maxlen = park.park_copylen = buflen;
     81      1.19    pooka 	park.park_flags = 0;
     82       1.1    pooka 
     83       1.1    pooka 	return touser(pmp, &park, puffs_getreqid(pmp), NULL, NULL);
     84       1.1    pooka }
     85       1.1    pooka 
     86      1.16    pooka void
     87      1.16    pooka puffs_suspendtouser(struct puffs_mount *pmp, int status)
     88      1.16    pooka {
     89      1.16    pooka 	struct puffs_vfsreq_suspend *pvfsr_susp;
     90      1.16    pooka 	struct puffs_park *ppark;
     91      1.16    pooka 
     92      1.16    pooka 	pvfsr_susp = malloc(sizeof(struct puffs_vfsreq_suspend),
     93      1.16    pooka 	    M_PUFFS, M_WAITOK | M_ZERO);
     94      1.16    pooka 	ppark = malloc(sizeof(struct puffs_park), M_PUFFS, M_WAITOK | M_ZERO);
     95      1.16    pooka 
     96      1.16    pooka 	pvfsr_susp->pvfsr_status = status;
     97      1.16    pooka 	ppark->park_preq = (struct puffs_req *)pvfsr_susp;
     98      1.16    pooka 
     99      1.16    pooka 	ppark->park_preq->preq_opclass = PUFFSOP_VFS | PUFFSOPFLAG_FAF;
    100      1.16    pooka 	ppark->park_preq->preq_optype = PUFFS_VFS_SUSPEND;
    101      1.16    pooka 
    102      1.16    pooka 	ppark->park_maxlen = ppark->park_copylen
    103      1.16    pooka 	    = sizeof(struct puffs_vfsreq_suspend);
    104      1.19    pooka 	ppark->park_flags = 0;
    105      1.16    pooka 
    106      1.16    pooka 	(void)touser(pmp, ppark, 0, NULL, NULL);
    107      1.16    pooka }
    108      1.16    pooka 
    109       1.1    pooka /*
    110       1.1    pooka  * vnode level request
    111       1.1    pooka  */
    112       1.1    pooka int
    113       1.1    pooka puffs_vntouser(struct puffs_mount *pmp, int optype,
    114       1.1    pooka 	void *kbuf, size_t buflen, void *cookie,
    115       1.1    pooka 	struct vnode *vp1, struct vnode *vp2)
    116       1.1    pooka {
    117       1.1    pooka 	struct puffs_park park;
    118       1.1    pooka 
    119       1.9    pooka 	park.park_preq = kbuf;
    120       1.1    pooka 
    121       1.9    pooka 	park.park_preq->preq_opclass = PUFFSOP_VN;
    122       1.9    pooka 	park.park_preq->preq_optype = optype;
    123       1.9    pooka 	park.park_preq->preq_cookie = cookie;
    124       1.9    pooka 
    125       1.9    pooka 	park.park_maxlen = park.park_copylen = buflen;
    126      1.19    pooka 	park.park_flags = 0;
    127       1.1    pooka 
    128       1.1    pooka 	return touser(pmp, &park, puffs_getreqid(pmp), vp1, vp2);
    129       1.1    pooka }
    130       1.1    pooka 
    131       1.1    pooka /*
    132       1.1    pooka  * vnode level request, caller-controller req id
    133       1.1    pooka  */
    134       1.1    pooka int
    135       1.1    pooka puffs_vntouser_req(struct puffs_mount *pmp, int optype,
    136       1.4    pooka 	void *kbuf, size_t buflen, void *cookie, uint64_t reqid,
    137       1.1    pooka 	struct vnode *vp1, struct vnode *vp2)
    138       1.1    pooka {
    139       1.1    pooka 	struct puffs_park park;
    140       1.1    pooka 
    141       1.9    pooka 	park.park_preq = kbuf;
    142       1.9    pooka 
    143       1.9    pooka 	park.park_preq->preq_opclass = PUFFSOP_VN;
    144       1.9    pooka 	park.park_preq->preq_optype = optype;
    145       1.9    pooka 	park.park_preq->preq_cookie = cookie;
    146       1.1    pooka 
    147       1.9    pooka 	park.park_maxlen = park.park_copylen = buflen;
    148      1.19    pooka 	park.park_flags = 0;
    149       1.1    pooka 
    150       1.1    pooka 	return touser(pmp, &park, reqid, vp1, vp2);
    151       1.1    pooka }
    152       1.1    pooka 
    153       1.1    pooka /*
    154       1.9    pooka  * vnode level request, copy routines can adjust "kernbuf".
    155       1.9    pooka  * We overload park_copylen != park_maxlen to signal that the park
    156       1.9    pooka  * in question is of adjusting type.
    157       1.1    pooka  */
    158       1.1    pooka int
    159       1.1    pooka puffs_vntouser_adjbuf(struct puffs_mount *pmp, int optype,
    160       1.9    pooka 	void **kbuf, size_t *buflen, size_t maxdelta,
    161       1.9    pooka 	void *cookie, struct vnode *vp1, struct vnode *vp2)
    162       1.1    pooka {
    163       1.1    pooka 	struct puffs_park park;
    164       1.1    pooka 	int error;
    165       1.1    pooka 
    166       1.9    pooka 	park.park_preq = *kbuf;
    167       1.1    pooka 
    168       1.9    pooka 	park.park_preq->preq_opclass = PUFFSOP_VN;
    169       1.9    pooka 	park.park_preq->preq_optype = optype;
    170       1.9    pooka 	park.park_preq->preq_cookie = cookie;
    171       1.9    pooka 
    172       1.9    pooka 	park.park_copylen = *buflen;
    173       1.9    pooka 	park.park_maxlen = maxdelta + *buflen;
    174      1.19    pooka 	park.park_flags = PUFFS_PARKFLAG_ADJUSTABLE;
    175       1.1    pooka 
    176       1.1    pooka 	error = touser(pmp, &park, puffs_getreqid(pmp), vp1, vp2);
    177       1.9    pooka 
    178       1.9    pooka 	*kbuf = park.park_preq;
    179       1.9    pooka 	*buflen = park.park_copylen;
    180       1.1    pooka 
    181       1.1    pooka 	return error;
    182       1.1    pooka }
    183       1.1    pooka 
    184       1.1    pooka /*
    185       1.4    pooka  * Notice: kbuf will be free'd later.  I must be allocated from the
    186       1.4    pooka  * kernel heap and it's ownership is shifted to this function from
    187       1.4    pooka  * now on, i.e. the caller is not allowed to use it anymore!
    188       1.4    pooka  */
    189       1.4    pooka void
    190       1.4    pooka puffs_vntouser_faf(struct puffs_mount *pmp, int optype,
    191       1.4    pooka 	void *kbuf, size_t buflen, void *cookie)
    192       1.4    pooka {
    193       1.4    pooka 	struct puffs_park *ppark;
    194       1.4    pooka 
    195       1.4    pooka 	/* XXX: is it allowable to sleep here? */
    196       1.4    pooka 	ppark = malloc(sizeof(struct puffs_park), M_PUFFS, M_NOWAIT | M_ZERO);
    197       1.4    pooka 	if (ppark == NULL)
    198       1.4    pooka 		return; /* 2bad */
    199       1.4    pooka 
    200       1.9    pooka 	ppark->park_preq = kbuf;
    201       1.9    pooka 
    202       1.9    pooka 	ppark->park_preq->preq_opclass = PUFFSOP_VN | PUFFSOPFLAG_FAF;
    203       1.9    pooka 	ppark->park_preq->preq_optype = optype;
    204       1.9    pooka 	ppark->park_preq->preq_cookie = cookie;
    205       1.9    pooka 
    206       1.9    pooka 	ppark->park_maxlen = ppark->park_copylen = buflen;
    207      1.19    pooka 	ppark->park_flags = 0;
    208       1.4    pooka 
    209       1.4    pooka 	(void)touser(pmp, ppark, 0, NULL, NULL);
    210       1.4    pooka }
    211       1.4    pooka 
    212       1.4    pooka /*
    213       1.1    pooka  * Wait for the userspace ping-pong game in calling process context.
    214       1.1    pooka  *
    215       1.1    pooka  * This unlocks vnodes if they are supplied.  vp1 is the vnode
    216       1.1    pooka  * before in the locking order, i.e. the one which must be locked
    217       1.1    pooka  * before accessing vp2.  This is done here so that operations are
    218       1.1    pooka  * already ordered in the queue when vnodes are unlocked (I'm not
    219       1.1    pooka  * sure if that's really necessary, but it can't hurt).  Okok, maybe
    220       1.1    pooka  * there's a slight ugly-factor also, but let's not worry about that.
    221       1.1    pooka  */
    222       1.1    pooka static int
    223       1.4    pooka touser(struct puffs_mount *pmp, struct puffs_park *ppark, uint64_t reqid,
    224       1.1    pooka 	struct vnode *vp1, struct vnode *vp2)
    225       1.1    pooka {
    226      1.19    pooka 	struct lwp *l = curlwp;
    227      1.16    pooka 	struct mount *mp;
    228       1.9    pooka 	struct puffs_req *preq;
    229      1.19    pooka 	int rv = 0;
    230       1.1    pooka 
    231      1.16    pooka 	mp = PMPTOMP(pmp);
    232      1.19    pooka 	preq = ppark->park_preq;
    233      1.19    pooka 	preq->preq_id = ppark->park_id = reqid;
    234      1.19    pooka 	preq->preq_buflen = ALIGN(ppark->park_maxlen);
    235      1.19    pooka 
    236      1.19    pooka 	/*
    237      1.19    pooka 	 * To support PCATCH, yet another movie: check if there are signals
    238      1.19    pooka 	 * pending and we are issueing a non-FAF.  If so, return an error
    239      1.19    pooka 	 * directly UNLESS we are issueing INACTIVE.  In that case, convert
    240      1.19    pooka 	 * it to a FAF, fire off to the file server and return an error.
    241      1.19    pooka 	 * Yes, this is bordering disgusting.  Barfbags are on me.
    242      1.19    pooka 	 */
    243      1.19    pooka 	if (PUFFSOP_WANTREPLY(ppark->park_preq->preq_opclass)
    244      1.19    pooka 	   && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    245      1.19    pooka 		if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
    246      1.19    pooka 		    && preq->preq_optype == PUFFS_VN_INACTIVE) {
    247      1.19    pooka 			struct puffs_park *newpark;
    248      1.19    pooka 
    249      1.19    pooka 			newpark = puffs_reqtofaf(ppark);
    250      1.19    pooka 			DPRINTF(("puffs touser: converted to FAF, old %p, "
    251      1.19    pooka 			    "new %p\n", ppark, newpark));
    252      1.19    pooka 			ppark = newpark;
    253      1.19    pooka 			rv = EINTR;
    254      1.19    pooka 		} else {
    255      1.19    pooka 			return EINTR;
    256      1.19    pooka 		}
    257      1.19    pooka 	}
    258      1.16    pooka 
    259      1.16    pooka 	/*
    260      1.16    pooka 	 * test for suspension lock.
    261      1.16    pooka 	 *
    262      1.16    pooka 	 * Note that we *DO NOT* keep the lock, since that might block
    263      1.16    pooka 	 * lock acquiring PLUS it would give userlandia control over
    264      1.16    pooka 	 * the lock.  The operation queue enforces a strict ordering:
    265      1.16    pooka 	 * when the fs server gets in the op stream, it knows things
    266      1.16    pooka 	 * are in order.  The kernel locks can't guarantee that for
    267      1.16    pooka 	 * userspace, in any case.
    268      1.16    pooka 	 *
    269      1.16    pooka 	 * BUT: this presents a problem for ops which have a consistency
    270      1.16    pooka 	 * clause based on more than one operation.  Unfortunately such
    271      1.16    pooka 	 * operations (read, write) do not reliably work yet.
    272      1.16    pooka 	 *
    273      1.16    pooka 	 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
    274      1.18    pooka 	 *
    275      1.18    pooka 	 * XXX: and there is one more problem.  We sometimes need to
    276      1.18    pooka 	 * take a lazy lock in case the fs is suspending and we are
    277      1.18    pooka 	 * executing as the fs server context.  This might happen
    278      1.18    pooka 	 * e.g. in the case that the user server triggers a reclaim
    279      1.18    pooka 	 * in the kernel while the fs is suspending.  It's not a very
    280      1.18    pooka 	 * likely event, but it needs to be fixed some day.
    281      1.16    pooka 	 */
    282      1.18    pooka 	fstrans_start(mp, FSTRANS_NORMAL);
    283       1.1    pooka 	simple_lock(&pmp->pmp_lock);
    284      1.16    pooka 	fstrans_done(mp);
    285      1.16    pooka 
    286      1.13    pooka 	if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    287       1.1    pooka 		simple_unlock(&pmp->pmp_lock);
    288       1.1    pooka 		return ENXIO;
    289       1.1    pooka 	}
    290       1.1    pooka 
    291       1.4    pooka 	TAILQ_INSERT_TAIL(&pmp->pmp_req_touser, ppark, park_entries);
    292       1.1    pooka 	pmp->pmp_req_touser_waiters++;
    293       1.1    pooka 
    294       1.1    pooka 	/*
    295       1.1    pooka 	 * Don't do unlock-relock dance yet.  There are a couple of
    296       1.1    pooka 	 * unsolved issues with it.  If we don't unlock, we can have
    297       1.1    pooka 	 * processes wanting vn_lock in case userspace hangs.  But
    298       1.1    pooka 	 * that can be "solved" by killing the userspace process.  It
    299       1.1    pooka 	 * would of course be nicer to have antilocking in the userspace
    300       1.1    pooka 	 * interface protocol itself.. your patience will be rewarded.
    301       1.1    pooka 	 */
    302       1.1    pooka #if 0
    303       1.1    pooka 	/* unlock */
    304       1.1    pooka 	if (vp2)
    305       1.1    pooka 		VOP_UNLOCK(vp2, 0);
    306       1.1    pooka 	if (vp1)
    307       1.1    pooka 		VOP_UNLOCK(vp1, 0);
    308       1.1    pooka #endif
    309       1.1    pooka 
    310       1.1    pooka 	/*
    311       1.1    pooka 	 * XXX: does releasing the lock here cause trouble?  Can't hold
    312       1.1    pooka 	 * it, because otherwise the below would cause locking against
    313       1.4    pooka 	 * oneself-problems in the kqueue stuff.  yes, it is a
    314       1.4    pooka 	 * theoretical race, so it must be solved
    315       1.1    pooka 	 */
    316       1.1    pooka 	simple_unlock(&pmp->pmp_lock);
    317       1.1    pooka 
    318      1.15    pooka 	DPRINTF(("touser: enqueueing req %" PRIu64 ", preq: %p, park: %p, "
    319      1.15    pooka 	    "c/t: 0x%x/0x%x\n", preq->preq_id, preq, ppark, preq->preq_opclass,
    320      1.15    pooka 	    preq->preq_optype));
    321      1.15    pooka 
    322       1.1    pooka 	wakeup(&pmp->pmp_req_touser);
    323       1.1    pooka 	selnotify(pmp->pmp_sel, 0);
    324       1.1    pooka 
    325      1.16    pooka 	if (PUFFSOP_WANTREPLY(ppark->park_preq->preq_opclass)) {
    326      1.19    pooka 		struct puffs_park *valetpark = NULL;
    327      1.19    pooka 		int error;
    328      1.19    pooka 
    329      1.19    pooka 		error = ltsleep(ppark, PUSER | PCATCH, "puffs1", 0, NULL);
    330      1.19    pooka 		rv = ppark->park_preq->preq_rv;
    331      1.19    pooka 
    332      1.19    pooka 		/*
    333      1.19    pooka 		 * Ok, so it gets a bit tricky around here once again.
    334      1.19    pooka 		 * We want to give interruptibility to the sleep to work
    335      1.19    pooka 		 * around all kinds of locking-against-oneself problems
    336      1.19    pooka 		 * and the file system recursing into itself and so forth.
    337      1.19    pooka 		 * So if we break out of the ltsleep() for anything except
    338      1.19    pooka 		 * natural causes, we need to caution ourselves.
    339      1.19    pooka 		 *
    340      1.19    pooka 		 * The stages at which we can break out are:
    341      1.19    pooka 		 *  1) operation waiting to be fetched by file server
    342      1.19    pooka 		 *  2) operation being copied to userspace, not on either queue
    343      1.19    pooka 		 *  3) file server operating on .. err .. operation
    344      1.19    pooka 		 *  4) putop: locate the correct park structure from the queue
    345      1.19    pooka 		 *  5) putop: copy response from userspace
    346      1.19    pooka 		 *  6) putop: wakeup waiter
    347      1.19    pooka 		 *
    348      1.19    pooka 		 * If we are still at stage 1, no problem, just remove
    349      1.19    pooka 		 * ourselves from the queue to userspace.  If we are at
    350      1.19    pooka 		 * the stage before 4 has completed, replace the park structure
    351      1.19    pooka 		 * with a park structure indicating that the caller is
    352      1.19    pooka 		 * no more and no proper reply is required.  If the server
    353      1.19    pooka 		 * is already copying data from userspace to the kernel,
    354      1.19    pooka 		 * wait for it to finish and return the real return value to
    355      1.19    pooka 		 * the caller.
    356      1.19    pooka 		 */
    357      1.19    pooka  checkagain:
    358      1.19    pooka 		if (valetpark) {
    359      1.19    pooka 			FREE(valetpark, M_PUFFS);
    360      1.19    pooka 			valetpark = NULL;
    361      1.19    pooka 		}
    362      1.19    pooka 
    363      1.19    pooka 		if (error) {
    364      1.19    pooka 			DPRINTF(("puffs touser: got %d from ltsleep, "
    365      1.19    pooka 			    "(unlocked) flags 0x%x (park %p)\n",
    366      1.19    pooka 			    error, ppark->park_flags, ppark));
    367      1.19    pooka 			rv = error;
    368      1.19    pooka 
    369      1.19    pooka 			MALLOC(valetpark, struct puffs_park *,
    370      1.19    pooka 			    sizeof(struct puffs_park), M_PUFFS,
    371      1.19    pooka 			    M_ZERO | M_WAITOK);
    372      1.19    pooka 
    373      1.19    pooka 			simple_lock(&pmp->pmp_lock);
    374      1.19    pooka 
    375      1.19    pooka 			/*
    376      1.19    pooka 			 * The order here for the clauses, per description
    377      1.19    pooka 			 * in comment above, is:
    378      1.19    pooka 			 *   1, after 6, after 4, 2-3.
    379      1.19    pooka 			 */
    380      1.19    pooka 			if ((ppark->park_flags&PUFFS_PARKFLAG_PROCESSING)==0) {
    381      1.19    pooka 				TAILQ_REMOVE(&pmp->pmp_req_touser, ppark,
    382      1.19    pooka 				    park_entries);
    383      1.19    pooka 				simple_unlock(&pmp->pmp_lock);
    384      1.19    pooka 				FREE(valetpark, M_PUFFS);
    385      1.19    pooka 				DPRINTF(("puffs touser: park %p removed "
    386      1.19    pooka 				    "from queue one\n", ppark));
    387      1.19    pooka 			} else if
    388      1.19    pooka 			   (ppark->park_flags & PUFFS_PARKFLAG_RECVREPLY) {
    389      1.19    pooka 				if (ppark->park_flags & PUFFS_PARKFLAG_DONE) {
    390      1.19    pooka 					rv = ppark->park_preq->preq_rv;
    391      1.19    pooka 					simple_unlock(&pmp->pmp_lock);
    392      1.19    pooka 					FREE(valetpark, M_PUFFS);
    393      1.19    pooka 				} else {
    394      1.19    pooka 					error = ltsleep(ppark,
    395      1.19    pooka 					    PUSER | PCATCH | PNORELOCK,
    396      1.19    pooka 					    "puffsre1", 0, &pmp->pmp_lock);
    397      1.19    pooka 					goto checkagain;
    398      1.19    pooka 				}
    399      1.19    pooka 			} else {
    400      1.19    pooka 				valetpark->park_flags
    401      1.19    pooka 				    = PUFFS_PARKFLAG_WAITERGONE;
    402      1.19    pooka 				ppark->park_flags |= PUFFS_PARKFLAG_WAITERGONE;
    403      1.19    pooka 				valetpark->park_id = ppark->park_id;
    404      1.19    pooka 
    405      1.19    pooka 				if (ppark->park_flags & PUFFS_PARKFLAG_RQUEUE) {
    406      1.19    pooka 					TAILQ_INSERT_BEFORE(ppark, valetpark,
    407      1.19    pooka 					    park_entries);
    408      1.19    pooka 					TAILQ_REMOVE(&pmp->pmp_req_replywait,
    409      1.19    pooka 					    ppark, park_entries);
    410      1.19    pooka 				} else {
    411      1.19    pooka 					TAILQ_INSERT_TAIL(
    412      1.19    pooka 					    &pmp->pmp_req_replywait,
    413      1.19    pooka 					    valetpark, park_entries);
    414      1.19    pooka 				}
    415       1.1    pooka 
    416      1.19    pooka 				simple_unlock(&pmp->pmp_lock);
    417      1.19    pooka 				DPRINTF(("puffs touser: replaced park %p "
    418      1.19    pooka 				    "with valet park %p\n", ppark, valetpark));
    419      1.19    pooka 			}
    420      1.19    pooka 		}
    421      1.19    pooka 
    422      1.16    pooka 		/*
    423      1.16    pooka 		 * retake the lock and release.  This makes sure (haha,
    424      1.16    pooka 		 * I'm humorous) that we don't process the same vnode in
    425      1.16    pooka 		 * multiple threads due to the locks hacks we have in
    426      1.16    pooka 		 * puffs_lock().  In reality this is well protected by
    427      1.16    pooka 		 * the biglock, but once that's gone, well, hopefully
    428      1.16    pooka 		 * this will be fixed for real.  (and when you read this
    429      1.16    pooka 		 * comment in 2017 and subsequently barf, my condolences ;).
    430      1.16    pooka 		 */
    431      1.19    pooka 		if (rv == 0 && !fstrans_is_owner(mp)) {
    432      1.17  hannken 			fstrans_start(mp, FSTRANS_NORMAL);
    433      1.16    pooka 			fstrans_done(mp);
    434      1.16    pooka 		}
    435      1.16    pooka 	}
    436      1.16    pooka 
    437       1.1    pooka #if 0
    438       1.1    pooka 	/* relock */
    439       1.1    pooka 	if (vp1)
    440       1.1    pooka 		KASSERT(vn_lock(vp1, LK_EXCLUSIVE | LK_RETRY) == 0);
    441       1.1    pooka 	if (vp2)
    442       1.1    pooka 		KASSERT(vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY) == 0);
    443       1.1    pooka #endif
    444       1.1    pooka 
    445      1.16    pooka 	simple_lock(&pmp->pmp_lock);
    446      1.16    pooka 	if (--pmp->pmp_req_touser_waiters == 0)
    447      1.16    pooka 		wakeup(&pmp->pmp_req_touser_waiters);
    448      1.16    pooka 	simple_unlock(&pmp->pmp_lock);
    449      1.16    pooka 
    450      1.19    pooka 	return rv;
    451       1.1    pooka }
    452       1.1    pooka 
    453       1.1    pooka 
    454       1.9    pooka /*
    455       1.9    pooka  * getop: scan through queued requests until:
    456       1.9    pooka  *  1) max number of requests satisfied
    457       1.9    pooka  *     OR
    458       1.9    pooka  *  2) buffer runs out of space
    459       1.9    pooka  *     OR
    460       1.9    pooka  *  3) nonblocking is set AND there are no operations available
    461       1.9    pooka  *     OR
    462       1.9    pooka  *  4) at least one operation was transferred AND there are no more waiting
    463       1.9    pooka  */
    464      1.10    pooka int
    465      1.10    pooka puffs_getop(struct puffs_mount *pmp, struct puffs_reqh_get *phg, int nonblock)
    466       1.1    pooka {
    467       1.1    pooka 	struct puffs_park *park;
    468       1.9    pooka 	struct puffs_req *preq;
    469       1.9    pooka 	uint8_t *bufpos;
    470       1.9    pooka 	int error, donesome;
    471       1.9    pooka 
    472       1.9    pooka 	donesome = error = 0;
    473       1.9    pooka 	bufpos = phg->phg_buf;
    474       1.1    pooka 
    475       1.1    pooka 	simple_lock(&pmp->pmp_lock);
    476       1.9    pooka 	while (phg->phg_nops == 0 || donesome != phg->phg_nops) {
    477       1.1    pooka  again:
    478       1.9    pooka 		if (pmp->pmp_status != PUFFSTAT_RUNNING) {
    479       1.9    pooka 			/* if we got some, they don't really matter anymore */
    480       1.9    pooka 			error = ENXIO;
    481       1.9    pooka 			goto out;
    482       1.9    pooka 		}
    483       1.9    pooka 		if (TAILQ_EMPTY(&pmp->pmp_req_touser)) {
    484      1.12    pooka 			if (donesome)
    485      1.12    pooka 				goto out;
    486      1.12    pooka 
    487      1.12    pooka 			if (nonblock) {
    488      1.12    pooka 				error = EWOULDBLOCK;
    489       1.9    pooka 				goto out;
    490       1.9    pooka 			}
    491       1.9    pooka 
    492      1.11    pooka 			error = ltsleep(&pmp->pmp_req_touser, PUSER | PCATCH,
    493      1.11    pooka 			    "puffs2", 0, &pmp->pmp_lock);
    494      1.11    pooka 			if (error)
    495      1.11    pooka 				goto out;
    496      1.11    pooka 			else
    497      1.11    pooka 				goto again;
    498       1.9    pooka 		}
    499       1.9    pooka 
    500       1.9    pooka 		park = TAILQ_FIRST(&pmp->pmp_req_touser);
    501       1.9    pooka 		preq = park->park_preq;
    502       1.9    pooka 
    503       1.9    pooka 		if (phg->phg_buflen < preq->preq_buflen) {
    504       1.9    pooka 			if (!donesome)
    505       1.9    pooka 				error = E2BIG;
    506       1.9    pooka 			goto out;
    507       1.9    pooka 		}
    508       1.9    pooka 		TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
    509      1.19    pooka 		park->park_flags |= PUFFS_PARKFLAG_PROCESSING;
    510      1.19    pooka 		simple_unlock(&pmp->pmp_lock);
    511       1.9    pooka 
    512       1.9    pooka 		DPRINTF(("puffsgetop: get op %" PRIu64 " (%d.), from %p "
    513       1.9    pooka 		    "len %zu (buflen %zu), target %p\n", preq->preq_id,
    514       1.9    pooka 		    donesome, preq, park->park_copylen, preq->preq_buflen,
    515       1.9    pooka 		    bufpos));
    516       1.9    pooka 
    517       1.9    pooka 		if ((error = copyout(preq, bufpos, park->park_copylen)) != 0) {
    518       1.9    pooka 			DPRINTF(("    FAILED %d\n", error));
    519       1.9    pooka 			/*
    520       1.9    pooka 			 * ok, user server is probably trying to cheat.
    521       1.9    pooka 			 * stuff op back & return error to user
    522       1.9    pooka 			 */
    523       1.9    pooka 			 simple_lock(&pmp->pmp_lock);
    524       1.9    pooka 			 TAILQ_INSERT_HEAD(&pmp->pmp_req_touser, park,
    525       1.9    pooka 			     park_entries);
    526       1.9    pooka 
    527       1.9    pooka 			 if (donesome)
    528       1.9    pooka 				error = 0;
    529       1.9    pooka 			 goto out;
    530       1.9    pooka 		}
    531       1.9    pooka 		bufpos += preq->preq_buflen;
    532       1.9    pooka 		phg->phg_buflen -= preq->preq_buflen;
    533       1.9    pooka 		donesome++;
    534       1.9    pooka 
    535       1.9    pooka 		simple_lock(&pmp->pmp_lock);
    536       1.9    pooka 		if (PUFFSOP_WANTREPLY(preq->preq_opclass)) {
    537      1.19    pooka 			if ((park->park_flags & PUFFS_PARKFLAG_WAITERGONE)==0) {
    538      1.19    pooka 				TAILQ_INSERT_TAIL(&pmp->pmp_req_replywait, park,
    539      1.19    pooka 				    park_entries);
    540      1.19    pooka 				park->park_flags |= PUFFS_PARKFLAG_RQUEUE;
    541      1.19    pooka 			}
    542       1.9    pooka 		} else {
    543       1.1    pooka 			simple_unlock(&pmp->pmp_lock);
    544       1.9    pooka 			free(preq, M_PUFFS);
    545       1.9    pooka 			free(park, M_PUFFS);
    546       1.9    pooka 			simple_lock(&pmp->pmp_lock);
    547       1.1    pooka 		}
    548       1.1    pooka 	}
    549       1.1    pooka 
    550       1.9    pooka  out:
    551       1.9    pooka 	phg->phg_more = pmp->pmp_req_touser_waiters;
    552       1.1    pooka 	simple_unlock(&pmp->pmp_lock);
    553       1.1    pooka 
    554       1.9    pooka 	phg->phg_nops = donesome;
    555       1.4    pooka 
    556       1.9    pooka 	return error;
    557       1.1    pooka }
    558       1.1    pooka 
    559      1.10    pooka int
    560      1.10    pooka puffs_putop(struct puffs_mount *pmp, struct puffs_reqh_put *php)
    561       1.1    pooka {
    562       1.1    pooka 	struct puffs_park *park;
    563      1.19    pooka 	struct puffs_req tmpreq;
    564      1.19    pooka 	struct puffs_req *nextpreq;
    565       1.9    pooka 	void *userbuf;
    566       1.9    pooka 	uint64_t id;
    567       1.9    pooka 	size_t reqlen;
    568      1.19    pooka 	int donesome, error, wgone;
    569       1.9    pooka 
    570      1.19    pooka 	donesome = error = wgone = 0;
    571       1.9    pooka 
    572       1.9    pooka 	id = php->php_id;
    573       1.9    pooka 	userbuf = php->php_buf;
    574       1.9    pooka 	reqlen = php->php_buflen;
    575       1.1    pooka 
    576       1.1    pooka 	simple_lock(&pmp->pmp_lock);
    577       1.9    pooka 	while (donesome != php->php_nops) {
    578      1.19    pooka #ifdef PUFFSDEBUG
    579       1.9    pooka 		simple_unlock(&pmp->pmp_lock);
    580       1.9    pooka 		DPRINTF(("puffsputop: searching for %" PRIu64 ", ubuf: %p, "
    581       1.9    pooka 		    "len %zu\n", id, userbuf, reqlen));
    582       1.9    pooka 		simple_lock(&pmp->pmp_lock);
    583       1.9    pooka #endif
    584       1.9    pooka 		TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
    585      1.19    pooka 			if (park->park_id == id)
    586       1.9    pooka 				break;
    587       1.9    pooka 		}
    588       1.9    pooka 
    589       1.9    pooka 		if (park == NULL) {
    590       1.9    pooka 			error = EINVAL;
    591       1.1    pooka 			break;
    592       1.1    pooka 		}
    593       1.9    pooka 		TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
    594      1.19    pooka 		park->park_flags |= PUFFS_PARKFLAG_RECVREPLY;
    595       1.9    pooka 		simple_unlock(&pmp->pmp_lock);
    596       1.9    pooka 
    597      1.19    pooka 		/*
    598      1.19    pooka 		 * If the caller has gone south, go to next, collect
    599      1.19    pooka 		 * $200 and free the structure there instead of wakeup.
    600      1.19    pooka 		 * We also need to copyin the
    601      1.19    pooka 		 */
    602      1.19    pooka 		if (park->park_flags & PUFFS_PARKFLAG_WAITERGONE) {
    603      1.19    pooka 			DPRINTF(("puffs_putop: bad service - waiter gone for "
    604      1.19    pooka 			    "park %p\n", park));
    605      1.19    pooka 			wgone = 1;
    606      1.19    pooka 			error = copyin(userbuf, &tmpreq,
    607      1.19    pooka 			    sizeof(struct puffs_req));
    608      1.19    pooka 			if (error)
    609      1.19    pooka 				goto loopout;
    610      1.19    pooka 			nextpreq = &tmpreq;
    611      1.19    pooka 			goto next;
    612      1.19    pooka 		}
    613      1.19    pooka 
    614      1.19    pooka 		if (park->park_flags & PUFFS_PARKFLAG_ADJUSTABLE) {
    615       1.9    pooka 			/* sanitycheck size of incoming transmission. */
    616       1.9    pooka 			if (reqlen > pmp->pmp_req_maxsize) {
    617       1.9    pooka 				DPRINTF(("puffsputop: outrageous user buf "
    618       1.9    pooka 				    "size: %zu\n", reqlen));
    619       1.9    pooka 				error = EINVAL;
    620       1.9    pooka 				goto loopout;
    621       1.9    pooka 			}
    622       1.1    pooka 
    623       1.9    pooka 			if (reqlen > park->park_copylen) {
    624       1.9    pooka 				if (reqlen > park->park_maxlen) {
    625       1.9    pooka 					DPRINTF(("puffsputop: adj copysize "
    626       1.9    pooka 					    "> max size, %zu vs %zu\n",
    627       1.9    pooka 					    reqlen, park->park_maxlen));
    628       1.9    pooka 					error = EINVAL;
    629       1.9    pooka 					goto loopout;
    630       1.9    pooka 				}
    631       1.9    pooka 				free(park->park_preq, M_PUFFS);
    632       1.9    pooka 				park->park_preq = malloc(reqlen,
    633       1.9    pooka 				    M_PUFFS, M_WAITOK);
    634       1.9    pooka 
    635       1.9    pooka 				park->park_copylen = reqlen;
    636       1.9    pooka 				DPRINTF(("puffsputop: adjbuf, new addr %p, "
    637       1.9    pooka 				    "len %zu\n", park->park_preq, reqlen));
    638       1.9    pooka 			}
    639       1.9    pooka 		} else {
    640       1.9    pooka 			if (reqlen == 0 || reqlen > park->park_copylen) {
    641       1.9    pooka 				reqlen = park->park_copylen;
    642       1.9    pooka 				DPRINTF(("puffsputop: kernel bufsize override: "
    643       1.9    pooka 				    "%zu\n", reqlen));
    644       1.9    pooka 			}
    645       1.9    pooka 		}
    646       1.1    pooka 
    647       1.9    pooka 		DPRINTF(("puffsputpop: copyin from %p to %p, len %zu\n",
    648       1.9    pooka 		    userbuf, park->park_preq, reqlen));
    649       1.9    pooka 		error = copyin(userbuf, park->park_preq, reqlen);
    650       1.9    pooka 		if (error)
    651       1.9    pooka 			goto loopout;
    652      1.19    pooka 		nextpreq = park->park_preq;
    653       1.9    pooka 
    654      1.19    pooka  next:
    655       1.9    pooka 		/* all's well, prepare for next op */
    656      1.19    pooka 		id = nextpreq->preq_id;
    657      1.19    pooka 		reqlen = nextpreq->preq_buflen;
    658      1.19    pooka 		userbuf = nextpreq->preq_nextbuf;
    659       1.9    pooka 		donesome++;
    660       1.9    pooka 
    661       1.9    pooka  loopout:
    662      1.19    pooka 		if (error && park->park_preq)
    663       1.9    pooka 			park->park_preq->preq_rv = error;
    664       1.1    pooka 
    665      1.19    pooka 		if (wgone) {
    666      1.19    pooka 			FREE(park, M_PUFFS);
    667      1.19    pooka 			simple_lock(&pmp->pmp_lock);
    668      1.19    pooka 		} else {
    669      1.19    pooka 			DPRINTF(("puffs_putop: flagging done for park %p\n",
    670      1.19    pooka 			    park));
    671      1.19    pooka 			simple_lock(&pmp->pmp_lock);
    672      1.19    pooka 			park->park_flags |= PUFFS_PARKFLAG_DONE;
    673      1.19    pooka 			wakeup(park);
    674      1.19    pooka 		}
    675      1.19    pooka 
    676       1.9    pooka 		if (error)
    677       1.9    pooka 			break;
    678      1.19    pooka 		wgone = 0;
    679       1.1    pooka 	}
    680       1.1    pooka 
    681       1.9    pooka 	simple_unlock(&pmp->pmp_lock);
    682       1.9    pooka 	php->php_nops -= donesome;
    683       1.1    pooka 
    684       1.1    pooka 	return error;
    685       1.1    pooka }
    686       1.1    pooka 
    687       1.1    pooka /* this is probably going to die away at some point? */
    688       1.9    pooka /*
    689       1.9    pooka  * XXX: currently bitrotted
    690       1.9    pooka  */
    691       1.9    pooka #if 0
    692       1.1    pooka static int
    693       1.1    pooka puffssizeop(struct puffs_mount *pmp, struct puffs_sizeop *psop_user)
    694       1.1    pooka {
    695       1.1    pooka 	struct puffs_sizepark *pspark;
    696       1.1    pooka 	void *kernbuf;
    697       1.1    pooka 	size_t copylen;
    698       1.1    pooka 	int error;
    699       1.1    pooka 
    700       1.1    pooka 	/* locate correct op */
    701       1.1    pooka 	simple_lock(&pmp->pmp_lock);
    702       1.1    pooka 	TAILQ_FOREACH(pspark, &pmp->pmp_req_sizepark, pkso_entries) {
    703       1.1    pooka 		if (pspark->pkso_reqid == psop_user->pso_reqid) {
    704       1.1    pooka 			TAILQ_REMOVE(&pmp->pmp_req_sizepark, pspark,
    705       1.1    pooka 			    pkso_entries);
    706       1.1    pooka 			break;
    707       1.1    pooka 		}
    708       1.1    pooka 	}
    709       1.1    pooka 	simple_unlock(&pmp->pmp_lock);
    710       1.1    pooka 
    711       1.1    pooka 	if (pspark == NULL)
    712       1.1    pooka 		return EINVAL;
    713       1.1    pooka 
    714       1.1    pooka 	error = 0;
    715       1.1    pooka 	copylen = MIN(pspark->pkso_bufsize, psop_user->pso_bufsize);
    716       1.1    pooka 
    717       1.1    pooka 	/*
    718       1.1    pooka 	 * XXX: uvm stuff to avoid bouncy-bouncy copying?
    719       1.1    pooka 	 */
    720       1.1    pooka 	if (PUFFS_SIZEOP_UIO(pspark->pkso_reqtype)) {
    721       1.1    pooka 		kernbuf = malloc(copylen, M_PUFFS, M_WAITOK | M_ZERO);
    722       1.1    pooka 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_IN) {
    723       1.1    pooka 			error = copyin(psop_user->pso_userbuf,
    724       1.1    pooka 			    kernbuf, copylen);
    725       1.1    pooka 			if (error) {
    726       1.1    pooka 				printf("psop ERROR1 %d\n", error);
    727       1.1    pooka 				goto escape;
    728       1.1    pooka 			}
    729       1.1    pooka 		}
    730       1.1    pooka 		error = uiomove(kernbuf, copylen, pspark->pkso_uio);
    731       1.1    pooka 		if (error) {
    732       1.1    pooka 			printf("uiomove from kernel %p, len %d failed: %d\n",
    733       1.1    pooka 			    kernbuf, (int)copylen, error);
    734       1.1    pooka 			goto escape;
    735       1.1    pooka 		}
    736       1.1    pooka 
    737       1.1    pooka 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_OUT) {
    738       1.1    pooka 			error = copyout(kernbuf,
    739       1.1    pooka 			    psop_user->pso_userbuf, copylen);
    740       1.1    pooka 			if (error) {
    741       1.1    pooka 				printf("psop ERROR2 %d\n", error);
    742       1.1    pooka 				goto escape;
    743       1.1    pooka 			}
    744       1.1    pooka 		}
    745       1.1    pooka  escape:
    746       1.1    pooka 		free(kernbuf, M_PUFFS);
    747       1.1    pooka 	} else if (PUFFS_SIZEOP_BUF(pspark->pkso_reqtype)) {
    748       1.1    pooka 		copylen = MAX(pspark->pkso_bufsize, psop_user->pso_bufsize);
    749       1.1    pooka 		if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_BUF_IN) {
    750       1.1    pooka 			error = copyin(psop_user->pso_userbuf,
    751       1.1    pooka 			pspark->pkso_copybuf, copylen);
    752       1.1    pooka 		} else {
    753       1.1    pooka 			error = copyout(pspark->pkso_copybuf,
    754       1.1    pooka 			    psop_user->pso_userbuf, copylen);
    755       1.1    pooka 		}
    756       1.1    pooka 	}
    757       1.1    pooka #ifdef DIAGNOSTIC
    758       1.1    pooka 	else
    759       1.1    pooka 		panic("puffssizeop: invalid reqtype %d\n",
    760       1.1    pooka 		    pspark->pkso_reqtype);
    761       1.1    pooka #endif /* DIAGNOSTIC */
    762       1.1    pooka 
    763       1.1    pooka 	return error;
    764       1.1    pooka }
    765       1.9    pooka #endif
    766