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