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