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