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