puffs_msgif.c revision 1.14 1 /* $NetBSD: puffs_msgif.c,v 1.14 2007/01/15 23:29:08 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.14 2007/01/15 23:29:08 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 uint64_t 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 simple_unlock(&pmp->pmp_lock);
201 return ENXIO;
202 }
203
204 preq = ppark->park_preq;
205 preq->preq_id = reqid;
206 preq->preq_buflen = ALIGN(ppark->park_maxlen);
207
208 TAILQ_INSERT_TAIL(&pmp->pmp_req_touser, ppark, park_entries);
209 pmp->pmp_req_touser_waiters++;
210
211 /*
212 * Don't do unlock-relock dance yet. There are a couple of
213 * unsolved issues with it. If we don't unlock, we can have
214 * processes wanting vn_lock in case userspace hangs. But
215 * that can be "solved" by killing the userspace process. It
216 * would of course be nicer to have antilocking in the userspace
217 * interface protocol itself.. your patience will be rewarded.
218 */
219 #if 0
220 /* unlock */
221 if (vp2)
222 VOP_UNLOCK(vp2, 0);
223 if (vp1)
224 VOP_UNLOCK(vp1, 0);
225 #endif
226
227 /*
228 * XXX: does releasing the lock here cause trouble? Can't hold
229 * it, because otherwise the below would cause locking against
230 * oneself-problems in the kqueue stuff. yes, it is a
231 * theoretical race, so it must be solved
232 */
233 simple_unlock(&pmp->pmp_lock);
234
235 wakeup(&pmp->pmp_req_touser);
236 selnotify(pmp->pmp_sel, 0);
237
238 if (PUFFSOP_WANTREPLY(ppark->park_preq->preq_opclass))
239 ltsleep(ppark, PUSER, "puffs1", 0, NULL);
240
241 #if 0
242 /* relock */
243 if (vp1)
244 KASSERT(vn_lock(vp1, LK_EXCLUSIVE | LK_RETRY) == 0);
245 if (vp2)
246 KASSERT(vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY) == 0);
247 #endif
248
249 return ppark->park_preq->preq_rv;
250 }
251
252
253 /*
254 * getop: scan through queued requests until:
255 * 1) max number of requests satisfied
256 * OR
257 * 2) buffer runs out of space
258 * OR
259 * 3) nonblocking is set AND there are no operations available
260 * OR
261 * 4) at least one operation was transferred AND there are no more waiting
262 */
263 int
264 puffs_getop(struct puffs_mount *pmp, struct puffs_reqh_get *phg, int nonblock)
265 {
266 struct puffs_park *park;
267 struct puffs_req *preq;
268 uint8_t *bufpos;
269 int error, donesome;
270
271 donesome = error = 0;
272 bufpos = phg->phg_buf;
273
274 simple_lock(&pmp->pmp_lock);
275 while (phg->phg_nops == 0 || donesome != phg->phg_nops) {
276 again:
277 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
278 /* if we got some, they don't really matter anymore */
279 error = ENXIO;
280 goto out;
281 }
282 if (TAILQ_EMPTY(&pmp->pmp_req_touser)) {
283 if (donesome)
284 goto out;
285
286 if (nonblock) {
287 error = EWOULDBLOCK;
288 goto out;
289 }
290
291 error = ltsleep(&pmp->pmp_req_touser, PUSER | PCATCH,
292 "puffs2", 0, &pmp->pmp_lock);
293 if (error)
294 goto out;
295 else
296 goto again;
297 }
298
299 park = TAILQ_FIRST(&pmp->pmp_req_touser);
300 preq = park->park_preq;
301
302 if (phg->phg_buflen < preq->preq_buflen) {
303 if (!donesome)
304 error = E2BIG;
305 goto out;
306 }
307 TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
308
309 simple_unlock(&pmp->pmp_lock);
310 DPRINTF(("puffsgetop: get op %" PRIu64 " (%d.), from %p "
311 "len %zu (buflen %zu), target %p\n", preq->preq_id,
312 donesome, preq, park->park_copylen, preq->preq_buflen,
313 bufpos));
314
315 if ((error = copyout(preq, bufpos, park->park_copylen)) != 0) {
316 DPRINTF((" FAILED %d\n", error));
317 /*
318 * ok, user server is probably trying to cheat.
319 * stuff op back & return error to user
320 */
321 simple_lock(&pmp->pmp_lock);
322 TAILQ_INSERT_HEAD(&pmp->pmp_req_touser, park,
323 park_entries);
324
325 if (donesome)
326 error = 0;
327 goto out;
328 }
329 bufpos += preq->preq_buflen;
330 phg->phg_buflen -= preq->preq_buflen;
331 donesome++;
332
333 simple_lock(&pmp->pmp_lock);
334 pmp->pmp_req_touser_waiters--;
335
336 if (PUFFSOP_WANTREPLY(preq->preq_opclass)) {
337 TAILQ_INSERT_TAIL(&pmp->pmp_req_replywait, park,
338 park_entries);
339 } else {
340 simple_unlock(&pmp->pmp_lock);
341 free(preq, M_PUFFS);
342 free(park, M_PUFFS);
343 simple_lock(&pmp->pmp_lock);
344 }
345 }
346
347 out:
348 phg->phg_more = pmp->pmp_req_touser_waiters;
349 simple_unlock(&pmp->pmp_lock);
350
351 phg->phg_nops = donesome;
352
353 return error;
354 }
355
356 int
357 puffs_putop(struct puffs_mount *pmp, struct puffs_reqh_put *php)
358 {
359 struct puffs_park *park;
360 void *userbuf;
361 uint64_t id;
362 size_t reqlen;
363 int error;
364 int donesome;
365
366 donesome = error = 0;
367
368 id = php->php_id;
369 userbuf = php->php_buf;
370 reqlen = php->php_buflen;
371
372 simple_lock(&pmp->pmp_lock);
373 while (donesome != php->php_nops) {
374 #ifdef DEBUG
375 simple_unlock(&pmp->pmp_lock);
376 DPRINTF(("puffsputop: searching for %" PRIu64 ", ubuf: %p, "
377 "len %zu\n", id, userbuf, reqlen));
378 simple_lock(&pmp->pmp_lock);
379 #endif
380 TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
381 if (park->park_preq->preq_id == id)
382 break;
383 }
384
385 if (park == NULL) {
386 error = EINVAL;
387 break;
388 }
389 TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
390 simple_unlock(&pmp->pmp_lock);
391
392 if (park->park_maxlen != park->park_copylen) {
393 /* sanitycheck size of incoming transmission. */
394 if (reqlen > pmp->pmp_req_maxsize) {
395 DPRINTF(("puffsputop: outrageous user buf "
396 "size: %zu\n", reqlen));
397 error = EINVAL;
398 goto loopout;
399 }
400
401 if (reqlen > park->park_copylen) {
402 if (reqlen > park->park_maxlen) {
403 DPRINTF(("puffsputop: adj copysize "
404 "> max size, %zu vs %zu\n",
405 reqlen, park->park_maxlen));
406 error = EINVAL;
407 goto loopout;
408 }
409 free(park->park_preq, M_PUFFS);
410 park->park_preq = malloc(reqlen,
411 M_PUFFS, M_WAITOK);
412
413 park->park_copylen = reqlen;
414 DPRINTF(("puffsputop: adjbuf, new addr %p, "
415 "len %zu\n", park->park_preq, reqlen));
416 }
417 } else {
418 if (reqlen == 0 || reqlen > park->park_copylen) {
419 reqlen = park->park_copylen;
420 DPRINTF(("puffsputop: kernel bufsize override: "
421 "%zu\n", reqlen));
422 }
423 }
424
425 DPRINTF(("puffsputpop: copyin from %p to %p, len %zu\n",
426 userbuf, park->park_preq, reqlen));
427 error = copyin(userbuf, park->park_preq, reqlen);
428 if (error)
429 goto loopout;
430
431 /* all's well, prepare for next op */
432 id = park->park_preq->preq_id;
433 reqlen = park->park_preq->preq_buflen;
434 userbuf = park->park_preq->preq_nextbuf;
435 donesome++;
436
437 loopout:
438 if (error)
439 park->park_preq->preq_rv = error;
440 wakeup(park);
441
442 simple_lock(&pmp->pmp_lock);
443 if (error)
444 break;
445 }
446
447 simple_unlock(&pmp->pmp_lock);
448 php->php_nops -= donesome;
449
450 return error;
451 }
452
453 /* this is probably going to die away at some point? */
454 /*
455 * XXX: currently bitrotted
456 */
457 #if 0
458 static int
459 puffssizeop(struct puffs_mount *pmp, struct puffs_sizeop *psop_user)
460 {
461 struct puffs_sizepark *pspark;
462 void *kernbuf;
463 size_t copylen;
464 int error;
465
466 /* locate correct op */
467 simple_lock(&pmp->pmp_lock);
468 TAILQ_FOREACH(pspark, &pmp->pmp_req_sizepark, pkso_entries) {
469 if (pspark->pkso_reqid == psop_user->pso_reqid) {
470 TAILQ_REMOVE(&pmp->pmp_req_sizepark, pspark,
471 pkso_entries);
472 break;
473 }
474 }
475 simple_unlock(&pmp->pmp_lock);
476
477 if (pspark == NULL)
478 return EINVAL;
479
480 error = 0;
481 copylen = MIN(pspark->pkso_bufsize, psop_user->pso_bufsize);
482
483 /*
484 * XXX: uvm stuff to avoid bouncy-bouncy copying?
485 */
486 if (PUFFS_SIZEOP_UIO(pspark->pkso_reqtype)) {
487 kernbuf = malloc(copylen, M_PUFFS, M_WAITOK | M_ZERO);
488 if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_IN) {
489 error = copyin(psop_user->pso_userbuf,
490 kernbuf, copylen);
491 if (error) {
492 printf("psop ERROR1 %d\n", error);
493 goto escape;
494 }
495 }
496 error = uiomove(kernbuf, copylen, pspark->pkso_uio);
497 if (error) {
498 printf("uiomove from kernel %p, len %d failed: %d\n",
499 kernbuf, (int)copylen, error);
500 goto escape;
501 }
502
503 if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_OUT) {
504 error = copyout(kernbuf,
505 psop_user->pso_userbuf, copylen);
506 if (error) {
507 printf("psop ERROR2 %d\n", error);
508 goto escape;
509 }
510 }
511 escape:
512 free(kernbuf, M_PUFFS);
513 } else if (PUFFS_SIZEOP_BUF(pspark->pkso_reqtype)) {
514 copylen = MAX(pspark->pkso_bufsize, psop_user->pso_bufsize);
515 if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_BUF_IN) {
516 error = copyin(psop_user->pso_userbuf,
517 pspark->pkso_copybuf, copylen);
518 } else {
519 error = copyout(pspark->pkso_copybuf,
520 psop_user->pso_userbuf, copylen);
521 }
522 }
523 #ifdef DIAGNOSTIC
524 else
525 panic("puffssizeop: invalid reqtype %d\n",
526 pspark->pkso_reqtype);
527 #endif /* DIAGNOSTIC */
528
529 return error;
530 }
531 #endif
532