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