puffs_msgif.c revision 1.11 1 /* $NetBSD: puffs_msgif.c,v 1.11 2006/12/10 22:37:04 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.11 2006/12/10 22:37:04 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 error = ltsleep(&pmp->pmp_req_touser, PUSER | PCATCH,
291 "puffs2", 0, &pmp->pmp_lock);
292 if (error)
293 goto out;
294 else
295 goto again;
296 }
297
298 park = TAILQ_FIRST(&pmp->pmp_req_touser);
299 preq = park->park_preq;
300
301 if (phg->phg_buflen < preq->preq_buflen) {
302 if (!donesome)
303 error = E2BIG;
304 goto out;
305 }
306 TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
307
308 simple_unlock(&pmp->pmp_lock);
309 DPRINTF(("puffsgetop: get op %" PRIu64 " (%d.), from %p "
310 "len %zu (buflen %zu), target %p\n", preq->preq_id,
311 donesome, preq, park->park_copylen, preq->preq_buflen,
312 bufpos));
313
314 if ((error = copyout(preq, bufpos, park->park_copylen)) != 0) {
315 DPRINTF((" FAILED %d\n", error));
316 /*
317 * ok, user server is probably trying to cheat.
318 * stuff op back & return error to user
319 */
320 simple_lock(&pmp->pmp_lock);
321 TAILQ_INSERT_HEAD(&pmp->pmp_req_touser, park,
322 park_entries);
323
324 if (donesome)
325 error = 0;
326 goto out;
327 }
328 bufpos += preq->preq_buflen;
329 phg->phg_buflen -= preq->preq_buflen;
330 donesome++;
331
332 simple_lock(&pmp->pmp_lock);
333 pmp->pmp_req_touser_waiters--;
334
335 if (PUFFSOP_WANTREPLY(preq->preq_opclass)) {
336 TAILQ_INSERT_TAIL(&pmp->pmp_req_replywait, park,
337 park_entries);
338 } else {
339 simple_unlock(&pmp->pmp_lock);
340 free(preq, M_PUFFS);
341 free(park, M_PUFFS);
342 simple_lock(&pmp->pmp_lock);
343 }
344 }
345
346 out:
347 phg->phg_more = pmp->pmp_req_touser_waiters;
348 simple_unlock(&pmp->pmp_lock);
349
350 phg->phg_nops = donesome;
351
352 return error;
353 }
354
355 int
356 puffs_putop(struct puffs_mount *pmp, struct puffs_reqh_put *php)
357 {
358 struct puffs_park *park;
359 void *userbuf;
360 uint64_t id;
361 size_t reqlen;
362 int error;
363 int donesome;
364
365 donesome = error = 0;
366
367 id = php->php_id;
368 userbuf = php->php_buf;
369 reqlen = php->php_buflen;
370
371 simple_lock(&pmp->pmp_lock);
372 while (donesome != php->php_nops) {
373 #ifdef DEBUG
374 simple_unlock(&pmp->pmp_lock);
375 DPRINTF(("puffsputop: searching for %" PRIu64 ", ubuf: %p, "
376 "len %zu\n", id, userbuf, reqlen));
377 simple_lock(&pmp->pmp_lock);
378 #endif
379 TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
380 if (park->park_preq->preq_id == id)
381 break;
382 }
383
384 if (park == NULL) {
385 error = EINVAL;
386 break;
387 }
388 TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
389 simple_unlock(&pmp->pmp_lock);
390
391 if (park->park_maxlen != park->park_copylen) {
392 /* sanitycheck size of incoming transmission. */
393 if (reqlen > pmp->pmp_req_maxsize) {
394 DPRINTF(("puffsputop: outrageous user buf "
395 "size: %zu\n", reqlen));
396 error = EINVAL;
397 goto loopout;
398 }
399
400 if (reqlen > park->park_copylen) {
401 if (reqlen > park->park_maxlen) {
402 DPRINTF(("puffsputop: adj copysize "
403 "> max size, %zu vs %zu\n",
404 reqlen, park->park_maxlen));
405 error = EINVAL;
406 goto loopout;
407 }
408 free(park->park_preq, M_PUFFS);
409 park->park_preq = malloc(reqlen,
410 M_PUFFS, M_WAITOK);
411
412 park->park_copylen = reqlen;
413 DPRINTF(("puffsputop: adjbuf, new addr %p, "
414 "len %zu\n", park->park_preq, reqlen));
415 }
416 } else {
417 if (reqlen == 0 || reqlen > park->park_copylen) {
418 reqlen = park->park_copylen;
419 DPRINTF(("puffsputop: kernel bufsize override: "
420 "%zu\n", reqlen));
421 }
422 }
423
424 DPRINTF(("puffsputpop: copyin from %p to %p, len %zu\n",
425 userbuf, park->park_preq, reqlen));
426 error = copyin(userbuf, park->park_preq, reqlen);
427 if (error)
428 goto loopout;
429
430 /* all's well, prepare for next op */
431 id = park->park_preq->preq_id;
432 reqlen = park->park_preq->preq_buflen;
433 userbuf = park->park_preq->preq_nextbuf;
434 donesome++;
435
436 loopout:
437 if (error)
438 park->park_preq->preq_rv = error;
439 wakeup(park);
440
441 simple_lock(&pmp->pmp_lock);
442 if (error)
443 break;
444 }
445
446 simple_unlock(&pmp->pmp_lock);
447 php->php_nops -= donesome;
448
449 return error;
450 }
451
452 /* this is probably going to die away at some point? */
453 /*
454 * XXX: currently bitrotted
455 */
456 #if 0
457 static int
458 puffssizeop(struct puffs_mount *pmp, struct puffs_sizeop *psop_user)
459 {
460 struct puffs_sizepark *pspark;
461 void *kernbuf;
462 size_t copylen;
463 int error;
464
465 /* locate correct op */
466 simple_lock(&pmp->pmp_lock);
467 TAILQ_FOREACH(pspark, &pmp->pmp_req_sizepark, pkso_entries) {
468 if (pspark->pkso_reqid == psop_user->pso_reqid) {
469 TAILQ_REMOVE(&pmp->pmp_req_sizepark, pspark,
470 pkso_entries);
471 break;
472 }
473 }
474 simple_unlock(&pmp->pmp_lock);
475
476 if (pspark == NULL)
477 return EINVAL;
478
479 error = 0;
480 copylen = MIN(pspark->pkso_bufsize, psop_user->pso_bufsize);
481
482 /*
483 * XXX: uvm stuff to avoid bouncy-bouncy copying?
484 */
485 if (PUFFS_SIZEOP_UIO(pspark->pkso_reqtype)) {
486 kernbuf = malloc(copylen, M_PUFFS, M_WAITOK | M_ZERO);
487 if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_IN) {
488 error = copyin(psop_user->pso_userbuf,
489 kernbuf, copylen);
490 if (error) {
491 printf("psop ERROR1 %d\n", error);
492 goto escape;
493 }
494 }
495 error = uiomove(kernbuf, copylen, pspark->pkso_uio);
496 if (error) {
497 printf("uiomove from kernel %p, len %d failed: %d\n",
498 kernbuf, (int)copylen, error);
499 goto escape;
500 }
501
502 if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_OUT) {
503 error = copyout(kernbuf,
504 psop_user->pso_userbuf, copylen);
505 if (error) {
506 printf("psop ERROR2 %d\n", error);
507 goto escape;
508 }
509 }
510 escape:
511 free(kernbuf, M_PUFFS);
512 } else if (PUFFS_SIZEOP_BUF(pspark->pkso_reqtype)) {
513 copylen = MAX(pspark->pkso_bufsize, psop_user->pso_bufsize);
514 if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_BUF_IN) {
515 error = copyin(psop_user->pso_userbuf,
516 pspark->pkso_copybuf, copylen);
517 } else {
518 error = copyout(pspark->pkso_copybuf,
519 psop_user->pso_userbuf, copylen);
520 }
521 }
522 #ifdef DIAGNOSTIC
523 else
524 panic("puffssizeop: invalid reqtype %d\n",
525 pspark->pkso_reqtype);
526 #endif /* DIAGNOSTIC */
527
528 return error;
529 }
530 #endif
531