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