puffs_msgif.c revision 1.33 1 /* $NetBSD: puffs_msgif.c,v 1.33 2007/04/24 09:44:57 pooka 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.33 2007/04/24 09:44:57 pooka 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 * waitq data structures
50 */
51
52 /*
53 * While a request is going to userspace, park the caller within the
54 * kernel. This is the kernel counterpart of "struct puffs_req".
55 */
56 struct puffs_park {
57 struct puffs_req *park_preq; /* req followed by buf */
58 uint64_t park_id; /* duplicate of preq_id */
59
60 size_t park_copylen; /* userspace copylength */
61 size_t park_maxlen; /* max size in comeback */
62
63 parkdone_fn park_done;
64 void *park_donearg;
65
66 int park_flags;
67 int park_refcount;
68
69 kcondvar_t park_cv;
70 kmutex_t park_mtx;
71
72 TAILQ_ENTRY(puffs_park) park_entries;
73 };
74 #define PARKFLAG_WAITERGONE 0x01
75 #define PARKFLAG_DONE 0x02
76 #define PARKFLAG_ONQUEUE1 0x04
77 #define PARKFLAG_ONQUEUE2 0x08
78 #define PARKFLAG_CALL 0x10
79 #define PARKFLAG_WANTREPLY 0x20
80
81 static struct pool_cache parkpc;
82 static struct pool parkpool;
83
84 static int
85 makepark(void *arg, void *obj, int flags)
86 {
87 struct puffs_park *park = obj;
88
89 mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
90 cv_init(&park->park_cv, "puffsrpl");
91
92 return 0;
93 }
94
95 static void
96 nukepark(void *arg, void *obj)
97 {
98 struct puffs_park *park = obj;
99
100 cv_destroy(&park->park_cv);
101 mutex_destroy(&park->park_mtx);
102 }
103
104 void
105 puffs_msgif_init()
106 {
107
108 pool_init(&parkpool, sizeof(struct puffs_park), 0, 0, 0,
109 "puffprkl", &pool_allocator_nointr, IPL_NONE);
110 pool_cache_init(&parkpc, &parkpool, makepark, nukepark, NULL);
111 }
112
113 void
114 puffs_msgif_destroy()
115 {
116
117 pool_cache_destroy(&parkpc);
118 pool_destroy(&parkpool);
119 }
120
121 void *
122 puffs_park_alloc(int waitok)
123 {
124 struct puffs_park *park;
125
126 park = pool_cache_get(&parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
127 if (park) {
128 park->park_refcount = 1;
129 mutex_enter(&park->park_mtx);
130 }
131
132 return park;
133 }
134
135 static void
136 puffs_park_reference(struct puffs_park *park)
137 {
138
139 mutex_enter(&park->park_mtx);
140 park->park_refcount++;
141 }
142
143 void
144 puffs_park_release(void *arg, int fullnuke)
145 {
146 struct puffs_park *park = arg;
147
148 KASSERT(mutex_owned(&park->park_mtx));
149 --park->park_refcount;
150
151 mutex_exit(&park->park_mtx);
152 if (park->park_refcount == 0 || fullnuke)
153 pool_cache_put(&parkpc, park);
154 }
155
156 #ifdef PUFFSDEBUG
157 static void
158 parkdump(struct puffs_park *park)
159 {
160
161 DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
162 "\tcopy %zu, max %zu - done: %p/%p\n"
163 "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
164 park, park->park_preq, park->park_id,
165 park->park_copylen, park->park_maxlen,
166 park->park_done, park->park_donearg,
167 park->park_flags, park->park_refcount,
168 &park->park_cv, &park->park_mtx));
169 }
170
171 static void
172 parkqdump(struct puffs_wq *q, int dumpall)
173 {
174 struct puffs_park *park;
175 int total = 0;
176
177 TAILQ_FOREACH(park, q, park_entries) {
178 if (dumpall)
179 parkdump(park);
180 total++;
181 }
182 DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
183
184 }
185 #endif /* PUFFSDEBUG */
186
187 /*
188 * Converts a non-FAF op to a FAF. This simply involves making copies
189 * of the park and request structures and tagging the request as a FAF.
190 * It is safe to block here, since the original op is not a FAF.
191 */
192 static void
193 puffs_reqtofaf(struct puffs_park *park)
194 {
195 struct puffs_req *newpreq;
196
197 KASSERT((park->park_preq->preq_opclass & PUFFSOPFLAG_FAF) == 0);
198
199 MALLOC(newpreq, struct puffs_req *, park->park_copylen,
200 M_PUFFS, M_ZERO | M_WAITOK);
201
202 memcpy(newpreq, park->park_preq, park->park_copylen);
203
204 park->park_preq = newpreq;
205 park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
206 }
207
208
209 /*
210 * kernel-user-kernel waitqueues
211 */
212
213 static int touser(struct puffs_mount *, struct puffs_park *, uint64_t,
214 struct vnode *, struct vnode *);
215
216 uint64_t
217 puffs_getreqid(struct puffs_mount *pmp)
218 {
219 uint64_t rv;
220
221 mutex_enter(&pmp->pmp_lock);
222 rv = pmp->pmp_nextreq++;
223 mutex_exit(&pmp->pmp_lock);
224
225 return rv;
226 }
227
228 /* vfs request */
229 int
230 puffs_vfstouser(struct puffs_mount *pmp, int optype, void *kbuf, size_t buflen)
231 {
232 struct puffs_park *park;
233
234 park = puffs_park_alloc(1);
235 park->park_preq = kbuf;
236
237 park->park_preq->preq_opclass = PUFFSOP_VFS;
238 park->park_preq->preq_optype = optype;
239
240 park->park_maxlen = park->park_copylen = buflen;
241 park->park_flags = 0;
242
243 return touser(pmp, park, puffs_getreqid(pmp), NULL, NULL);
244 }
245
246 void
247 puffs_suspendtouser(struct puffs_mount *pmp, int status)
248 {
249 struct puffs_vfsreq_suspend *pvfsr_susp;
250 struct puffs_park *park;
251
252 pvfsr_susp = malloc(sizeof(struct puffs_vfsreq_suspend),
253 M_PUFFS, M_WAITOK | M_ZERO);
254 park = puffs_park_alloc(1);
255
256 pvfsr_susp->pvfsr_status = status;
257 park->park_preq = (struct puffs_req *)pvfsr_susp;
258
259 park->park_preq->preq_opclass = PUFFSOP_VFS | PUFFSOPFLAG_FAF;
260 park->park_preq->preq_optype = PUFFS_VFS_SUSPEND;
261
262 park->park_maxlen = park->park_copylen
263 = sizeof(struct puffs_vfsreq_suspend);
264 park->park_flags = 0;
265
266 (void)touser(pmp, park, 0, NULL, NULL);
267 }
268
269 /*
270 * vnode level request
271 */
272 int
273 puffs_vntouser(struct puffs_mount *pmp, int optype,
274 void *kbuf, size_t buflen, size_t maxdelta, void *cookie,
275 struct vnode *vp1, struct vnode *vp2)
276 {
277 struct puffs_park *park;
278
279 park = puffs_park_alloc(1);
280 park->park_preq = kbuf;
281
282 park->park_preq->preq_opclass = PUFFSOP_VN;
283 park->park_preq->preq_optype = optype;
284 park->park_preq->preq_cookie = cookie;
285
286 park->park_copylen = buflen;
287 park->park_maxlen = buflen + maxdelta;
288 park->park_flags = 0;
289
290 return touser(pmp, park, puffs_getreqid(pmp), vp1, vp2);
291 }
292
293 /*
294 * vnode level request, caller-controller req id
295 */
296 int
297 puffs_vntouser_req(struct puffs_mount *pmp, int optype,
298 void *kbuf, size_t buflen, size_t maxdelta, void *cookie,
299 uint64_t reqid, struct vnode *vp1, struct vnode *vp2)
300 {
301 struct puffs_park *park;
302
303 park = puffs_park_alloc(1);
304 park->park_preq = kbuf;
305
306 park->park_preq->preq_opclass = PUFFSOP_VN;
307 park->park_preq->preq_optype = optype;
308 park->park_preq->preq_cookie = cookie;
309
310 park->park_copylen = buflen;
311 park->park_maxlen = buflen + maxdelta;
312 park->park_flags = 0;
313
314 return touser(pmp, park, reqid, vp1, vp2);
315 }
316
317 void
318 puffs_vntouser_call(struct puffs_mount *pmp, int optype,
319 void *kbuf, size_t buflen, size_t maxdelta, void *cookie,
320 parkdone_fn donefn, void *donearg,
321 struct vnode *vp1, struct vnode *vp2)
322 {
323 struct puffs_park *park;
324
325 park = puffs_park_alloc(1);
326 park->park_preq = kbuf;
327
328 park->park_preq->preq_opclass = PUFFSOP_VN;
329 park->park_preq->preq_optype = optype;
330 park->park_preq->preq_cookie = cookie;
331
332 park->park_copylen = buflen;
333 park->park_maxlen = buflen + maxdelta;
334 park->park_done = donefn;
335 park->park_donearg = donearg;
336 park->park_flags = PARKFLAG_CALL;
337
338 (void) touser(pmp, park, puffs_getreqid(pmp), vp1, vp2);
339 }
340
341 /*
342 * Notice: kbuf will be free'd later. I must be allocated from the
343 * kernel heap and it's ownership is shifted to this function from
344 * now on, i.e. the caller is not allowed to use it anymore!
345 */
346 void
347 puffs_vntouser_faf(struct puffs_mount *pmp, int optype,
348 void *kbuf, size_t buflen, void *cookie)
349 {
350 struct puffs_park *park;
351
352 /* XXX: is it allowable to sleep here? */
353 park = puffs_park_alloc(0);
354 if (park == NULL)
355 return; /* 2bad */
356
357 park->park_preq = kbuf;
358
359 park->park_preq->preq_opclass = PUFFSOP_VN | PUFFSOPFLAG_FAF;
360 park->park_preq->preq_optype = optype;
361 park->park_preq->preq_cookie = cookie;
362
363 park->park_maxlen = park->park_copylen = buflen;
364 park->park_flags = 0;
365
366 (void)touser(pmp, park, 0, NULL, NULL);
367 }
368
369 void
370 puffs_cacheop(struct puffs_mount *pmp, struct puffs_park *park,
371 struct puffs_cacheinfo *pcinfo, size_t pcilen, void *cookie)
372 {
373
374 park->park_preq = (struct puffs_req *)pcinfo;
375 park->park_preq->preq_opclass = PUFFSOP_CACHE | PUFFSOPFLAG_FAF;
376 park->park_preq->preq_optype = PCACHE_TYPE_WRITE; /* XXX */
377 park->park_preq->preq_cookie = cookie;
378
379 park->park_maxlen = park->park_copylen = pcilen;
380 park->park_flags = 0;
381
382 (void)touser(pmp, park, 0, NULL, NULL);
383 }
384
385 /*
386 * Wait for the userspace ping-pong game in calling process context.
387 *
388 * This unlocks vnodes if they are supplied. vp1 is the vnode
389 * before in the locking order, i.e. the one which must be locked
390 * before accessing vp2. This is done here so that operations are
391 * already ordered in the queue when vnodes are unlocked (I'm not
392 * sure if that's really necessary, but it can't hurt). Okok, maybe
393 * there's a slight ugly-factor also, but let's not worry about that.
394 */
395 static int
396 touser(struct puffs_mount *pmp, struct puffs_park *park, uint64_t reqid,
397 struct vnode *vp1, struct vnode *vp2)
398 {
399 struct lwp *l = curlwp;
400 struct mount *mp;
401 struct puffs_req *preq;
402 int rv = 0;
403
404 mp = PMPTOMP(pmp);
405 preq = park->park_preq;
406 preq->preq_id = park->park_id = reqid;
407 preq->preq_buflen = ALIGN(park->park_maxlen);
408
409 if (PUFFSOP_WANTREPLY(preq->preq_opclass))
410 park->park_flags |= PARKFLAG_WANTREPLY;
411
412 /*
413 * To support PCATCH, yet another movie: check if there are signals
414 * pending and we are issueing a non-FAF. If so, return an error
415 * directly UNLESS we are issueing INACTIVE. In that case, convert
416 * it to a FAF, fire off to the file server and return an error.
417 * Yes, this is bordering disgusting. Barfbags are on me.
418 */
419 if ((park->park_flags & PARKFLAG_WANTREPLY)
420 && (park->park_flags & PARKFLAG_CALL) == 0
421 && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
422 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
423 && preq->preq_optype == PUFFS_VN_INACTIVE) {
424 puffs_reqtofaf(park);
425 DPRINTF(("puffs touser: converted to FAF %p\n", park));
426 rv = EINTR;
427 } else {
428 puffs_park_release(park, 0);
429 return EINTR;
430 }
431 }
432
433 /*
434 * test for suspension lock.
435 *
436 * Note that we *DO NOT* keep the lock, since that might block
437 * lock acquiring PLUS it would give userlandia control over
438 * the lock. The operation queue enforces a strict ordering:
439 * when the fs server gets in the op stream, it knows things
440 * are in order. The kernel locks can't guarantee that for
441 * userspace, in any case.
442 *
443 * BUT: this presents a problem for ops which have a consistency
444 * clause based on more than one operation. Unfortunately such
445 * operations (read, write) do not reliably work yet.
446 *
447 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
448 *
449 * XXX: and there is one more problem. We sometimes need to
450 * take a lazy lock in case the fs is suspending and we are
451 * executing as the fs server context. This might happen
452 * e.g. in the case that the user server triggers a reclaim
453 * in the kernel while the fs is suspending. It's not a very
454 * likely event, but it needs to be fixed some day.
455 */
456
457 /*
458 * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
459 * the mutex here, since getpages() might be called locked.
460 */
461 fstrans_start(mp, FSTRANS_NORMAL);
462 mutex_enter(&pmp->pmp_lock);
463 fstrans_done(mp);
464
465 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
466 mutex_exit(&pmp->pmp_lock);
467 puffs_park_release(park, 0);
468 return ENXIO;
469 }
470
471 #ifdef PUFFSDEBUG
472 parkqdump(&pmp->pmp_req_touser, puffsdebug > 1);
473 parkqdump(&pmp->pmp_req_replywait, puffsdebug > 1);
474 #endif
475
476 TAILQ_INSERT_TAIL(&pmp->pmp_req_touser, park, park_entries);
477 park->park_flags |= PARKFLAG_ONQUEUE1;
478 pmp->pmp_req_waiters++;
479 mutex_exit(&pmp->pmp_lock);
480
481 #if 0
482 /*
483 * Don't do unlock-relock dance yet. There are a couple of
484 * unsolved issues with it. If we don't unlock, we can have
485 * processes wanting vn_lock in case userspace hangs. But
486 * that can be "solved" by killing the userspace process. It
487 * would of course be nicer to have antilocking in the userspace
488 * interface protocol itself.. your patience will be rewarded.
489 */
490 /* unlock */
491 if (vp2)
492 VOP_UNLOCK(vp2, 0);
493 if (vp1)
494 VOP_UNLOCK(vp1, 0);
495 #endif
496
497 DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
498 "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
499 preq->preq_opclass, preq->preq_optype, park->park_flags));
500
501 cv_broadcast(&pmp->pmp_req_waiter_cv);
502 selnotify(pmp->pmp_sel, 0);
503
504 if ((park->park_flags & PARKFLAG_WANTREPLY)
505 && (park->park_flags & PARKFLAG_CALL) == 0) {
506 int error;
507
508 error = cv_wait_sig(&park->park_cv, &park->park_mtx);
509 if (error) {
510 park->park_flags |= PARKFLAG_WAITERGONE;
511 if (park->park_flags & PARKFLAG_DONE) {
512 rv = preq->preq_rv;
513 puffs_park_release(park, 0);
514 } else {
515 /*
516 * ok, we marked it as going away, but
517 * still need to do queue ops. take locks
518 * in correct order.
519 *
520 * We don't want to release our reference
521 * if it's on replywait queue to avoid error
522 * to file server. putop() code will DTRT.
523 */
524 KASSERT(park->park_flags &
525 (PARKFLAG_ONQUEUE1 | PARKFLAG_ONQUEUE2));
526 mutex_exit(&park->park_mtx);
527
528 mutex_enter(&pmp->pmp_lock);
529 mutex_enter(&park->park_mtx);
530 if (park->park_flags & PARKFLAG_ONQUEUE1)
531 TAILQ_REMOVE(&pmp->pmp_req_touser,
532 park, park_entries);
533 park->park_flags &= ~PARKFLAG_ONQUEUE1;
534 if ((park->park_flags & PARKFLAG_ONQUEUE2) == 0)
535 puffs_park_release(park, 0);
536 else
537 mutex_exit(&park->park_mtx);
538 mutex_exit(&pmp->pmp_lock);
539
540 rv = error;
541 }
542 } else {
543 rv = preq->preq_rv;
544 puffs_park_release(park, 0);
545 }
546
547 /*
548 * retake the lock and release. This makes sure (haha,
549 * I'm humorous) that we don't process the same vnode in
550 * multiple threads due to the locks hacks we have in
551 * puffs_lock(). In reality this is well protected by
552 * the biglock, but once that's gone, well, hopefully
553 * this will be fixed for real. (and when you read this
554 * comment in 2017 and subsequently barf, my condolences ;).
555 */
556 if (rv == 0 && !fstrans_is_owner(mp)) {
557 fstrans_start(mp, FSTRANS_NORMAL);
558 fstrans_done(mp);
559 }
560 } else {
561 mutex_exit(&park->park_mtx);
562 }
563
564 #if 0
565 /* relock */
566 if (vp1)
567 KASSERT(vn_lock(vp1, LK_EXCLUSIVE | LK_RETRY) == 0);
568 if (vp2)
569 KASSERT(vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY) == 0);
570 #endif
571
572 mutex_enter(&pmp->pmp_lock);
573 if (--pmp->pmp_req_waiters == 0) {
574 KASSERT(cv_has_waiters(&pmp->pmp_req_waitersink_cv) <= 1);
575 cv_signal(&pmp->pmp_req_waitersink_cv);
576 }
577 mutex_exit(&pmp->pmp_lock);
578
579 return rv;
580 }
581
582
583 /*
584 * getop: scan through queued requests until:
585 * 1) max number of requests satisfied
586 * OR
587 * 2) buffer runs out of space
588 * OR
589 * 3) nonblocking is set AND there are no operations available
590 * OR
591 * 4) at least one operation was transferred AND there are no more waiting
592 */
593 int
594 puffs_getop(struct puffs_mount *pmp, struct puffs_reqh_get *phg, int nonblock)
595 {
596 struct puffs_park *park;
597 struct puffs_req *preq;
598 uint8_t *bufpos;
599 int error, donesome;
600
601 donesome = error = 0;
602 bufpos = phg->phg_buf;
603
604 mutex_enter(&pmp->pmp_lock);
605 while (phg->phg_nops == 0 || donesome != phg->phg_nops) {
606 again:
607 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
608 /* if we got some, they don't really matter anymore */
609 error = ENXIO;
610 goto out;
611 }
612 if (TAILQ_EMPTY(&pmp->pmp_req_touser)) {
613 if (donesome)
614 goto out;
615
616 if (nonblock) {
617 error = EWOULDBLOCK;
618 goto out;
619 }
620
621 error = cv_wait_sig(&pmp->pmp_req_waiter_cv,
622 &pmp->pmp_lock);
623 if (error)
624 goto out;
625 else
626 goto again;
627 }
628
629 park = TAILQ_FIRST(&pmp->pmp_req_touser);
630 puffs_park_reference(park);
631
632 /* If it's a goner, don't process any furher */
633 if (park->park_flags & PARKFLAG_WAITERGONE) {
634 puffs_park_release(park, 0);
635 continue;
636 }
637
638 preq = park->park_preq;
639 if (phg->phg_buflen < preq->preq_buflen) {
640 if (!donesome)
641 error = E2BIG;
642 puffs_park_release(park, 0);
643 goto out;
644 }
645
646 TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
647 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
648 park->park_flags &= ~PARKFLAG_ONQUEUE1;
649 mutex_exit(&pmp->pmp_lock);
650
651 DPRINTF(("puffsgetop: get op %" PRIu64 " (%d.), from %p "
652 "len %zu (buflen %zu), target %p\n", preq->preq_id,
653 donesome, preq, park->park_copylen, preq->preq_buflen,
654 bufpos));
655
656 if ((error = copyout(preq, bufpos, park->park_copylen)) != 0) {
657 DPRINTF(("puffs_getop: copyout failed: %d\n", error));
658 /*
659 * ok, user server is probably trying to cheat.
660 * stuff op back & return error to user. We need
661 * to take locks in the correct order.
662 */
663 mutex_exit(&park->park_mtx);
664
665 /*
666 * XXX: ONQUEUE1 | ONQUEUE2 invariant doesn't
667 * hold here
668 */
669
670 mutex_enter(&pmp->pmp_lock);
671 mutex_enter(&park->park_mtx);
672 if ((park->park_flags & PARKFLAG_WAITERGONE) == 0) {
673 TAILQ_INSERT_HEAD(&pmp->pmp_req_touser, park,
674 park_entries);
675 park->park_flags |= PARKFLAG_ONQUEUE1;
676 }
677
678 if (donesome)
679 error = 0;
680 puffs_park_release(park, 0);
681 goto out;
682 }
683 bufpos += preq->preq_buflen;
684 phg->phg_buflen -= preq->preq_buflen;
685 donesome++;
686
687 /* XXXfixme: taking this lock in the wrong order */
688 mutex_enter(&pmp->pmp_lock);
689
690 if (park->park_flags & PARKFLAG_WANTREPLY) {
691 TAILQ_INSERT_TAIL(&pmp->pmp_req_replywait, park,
692 park_entries);
693 park->park_flags |= PARKFLAG_ONQUEUE2;
694 puffs_park_release(park, 0);
695 } else {
696 free(preq, M_PUFFS);
697 puffs_park_release(park, 1);
698 }
699 }
700
701 out:
702 phg->phg_more = pmp->pmp_req_waiters;
703 mutex_exit(&pmp->pmp_lock);
704
705 phg->phg_nops = donesome;
706
707 return error;
708 }
709
710 int
711 puffs_putop(struct puffs_mount *pmp, struct puffs_reqh_put *php)
712 {
713 struct puffs_park *park;
714 struct puffs_req tmpreq;
715 struct puffs_req *nextpreq;
716 void *userbuf;
717 uint64_t id;
718 size_t reqlen;
719 int donesome, error, wgone, release;
720
721 donesome = error = wgone = 0;
722
723 id = php->php_id;
724 userbuf = php->php_buf;
725 reqlen = php->php_buflen;
726
727 mutex_enter(&pmp->pmp_lock);
728 while (donesome != php->php_nops) {
729 release = 0;
730 #ifdef PUFFSDEBUG
731 DPRINTF(("puffsputop: searching for %" PRIu64 ", ubuf: %p, "
732 "len %zu\n", id, userbuf, reqlen));
733 #endif
734 TAILQ_FOREACH(park, &pmp->pmp_req_replywait, park_entries) {
735 if (park->park_id == id)
736 break;
737 }
738
739 if (park == NULL) {
740 DPRINTF(("puffsputop: no request: %" PRIu64 "\n", id));
741 error = EINVAL;
742 break;
743 }
744
745 puffs_park_reference(park);
746 if (reqlen == 0 || reqlen > park->park_maxlen) {
747 DPRINTF(("puffsputop: invalid buffer length: "
748 "%zu\n", reqlen));
749 error = E2BIG;
750 puffs_park_release(park, 0);
751 break;
752 }
753 wgone = park->park_flags & PARKFLAG_WAITERGONE;
754
755 /* check if it's still on the queue after acquiring lock */
756 if (park->park_flags & PARKFLAG_ONQUEUE2) {
757 TAILQ_REMOVE(&pmp->pmp_req_replywait, park,
758 park_entries);
759 park->park_flags &= ~PARKFLAG_ONQUEUE2;
760 }
761
762 mutex_exit(&pmp->pmp_lock);
763
764 /*
765 * If the caller has gone south, go to next, collect
766 * $200 and free the structure there instead of wakeup.
767 * We also need to copyin the header info. Flag structure
768 * release to mode total and utter destruction.
769 */
770 if (wgone) {
771 DPRINTF(("puffs_putop: bad service - waiter gone for "
772 "park %p\n", park));
773 error = copyin(userbuf, &tmpreq,
774 sizeof(struct puffs_req));
775 release = 1;
776 if (error)
777 goto loopout;
778 nextpreq = &tmpreq;
779 goto next;
780 }
781
782 DPRINTF(("puffsputpop: copyin from %p to %p, len %zu\n",
783 userbuf, park->park_preq, reqlen));
784 error = copyin(userbuf, park->park_preq, reqlen);
785 if (error)
786 goto loopout;
787 nextpreq = park->park_preq;
788
789 next:
790 /* all's well, prepare for next op */
791 id = nextpreq->preq_id;
792 reqlen = nextpreq->preq_buflen;
793 userbuf = nextpreq->preq_nextbuf;
794 donesome++;
795
796 loopout:
797 if (error && !wgone)
798 park->park_preq->preq_rv = error;
799
800 if (park->park_flags & PARKFLAG_CALL) {
801 park->park_done(park->park_preq, park->park_donearg);
802 release = 1;
803 }
804
805 if (!wgone) {
806 DPRINTF(("puffs_putop: flagging done for "
807 "park %p\n", park));
808
809 cv_signal(&park->park_cv);
810 }
811 park->park_flags |= PARKFLAG_DONE;
812 puffs_park_release(park, release);
813
814 mutex_enter(&pmp->pmp_lock);
815 if (error)
816 break;
817 wgone = 0;
818 }
819
820 mutex_exit(&pmp->pmp_lock);
821 php->php_nops -= donesome;
822
823 return error;
824 }
825
826 /*
827 * We're dead, kaput, RIP, slightly more than merely pining for the
828 * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
829 * our maker, ceased to be, etcetc. YASD. It's a dead FS!
830 *
831 * Caller must hold puffs mutex.
832 */
833 void
834 puffs_userdead(struct puffs_mount *pmp)
835 {
836 struct puffs_park *park, *park_next;
837
838 /*
839 * Mark filesystem status as dying so that operations don't
840 * attempt to march to userspace any longer.
841 */
842 pmp->pmp_status = PUFFSTAT_DYING;
843
844 /* signal waiters on REQUEST TO file server queue */
845 for (park = TAILQ_FIRST(&pmp->pmp_req_touser); park; park = park_next) {
846 uint8_t opclass;
847
848 puffs_park_reference(park);
849 park_next = TAILQ_NEXT(park, park_entries);
850
851 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
852 TAILQ_REMOVE(&pmp->pmp_req_touser, park, park_entries);
853 park->park_flags &= ~PARKFLAG_ONQUEUE1;
854
855 /*
856 * If the waiter is gone, we may *NOT* access preq anymore.
857 */
858 if (park->park_flags & PARKFLAG_WAITERGONE) {
859 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
860 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
861 puffs_park_release(park, 0);
862 } else {
863 opclass = park->park_preq->preq_opclass;
864 park->park_preq->preq_rv = ENXIO;
865
866 if (park->park_flags & PARKFLAG_CALL) {
867 park->park_done(park->park_preq,
868 park->park_donearg);
869 puffs_park_release(park, 1);
870 } else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
871 free(park->park_preq, M_PUFFS);
872 puffs_park_release(park, 1);
873 } else {
874 park->park_preq->preq_rv = ENXIO;
875 cv_signal(&park->park_cv);
876 puffs_park_release(park, 0);
877 }
878 }
879 }
880
881 /* signal waiters on RESPONSE FROM file server queue */
882 for (park=TAILQ_FIRST(&pmp->pmp_req_replywait); park; park=park_next) {
883 puffs_park_reference(park);
884 park_next = TAILQ_NEXT(park, park_entries);
885
886 KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
887 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
888
889 TAILQ_REMOVE(&pmp->pmp_req_replywait, park, park_entries);
890 park->park_flags &= ~PARKFLAG_ONQUEUE2;
891
892 /*
893 * If the waiter is gone, we may *NOT* access preq anymore.
894 */
895 if (park->park_flags & PARKFLAG_WAITERGONE) {
896 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
897 puffs_park_release(park, 0);
898 } else {
899 park->park_preq->preq_rv = ENXIO;
900 if (park->park_flags & PARKFLAG_CALL) {
901 park->park_done(park->park_preq,
902 park->park_donearg);
903 puffs_park_release(park, 1);
904 } else {
905 cv_signal(&park->park_cv);
906 puffs_park_release(park, 0);
907 }
908 }
909 }
910 }
911
912 /* this is probably going to die away at some point? */
913 /*
914 * XXX: currently bitrotted
915 */
916 #if 0
917 static int
918 puffssizeop(struct puffs_mount *pmp, struct puffs_sizeop *psop_user)
919 {
920 struct puffs_sizepark *pspark;
921 void *kernbuf;
922 size_t copylen;
923 int error;
924
925 /* locate correct op */
926 mutex_enter(&pmp->pmp_lock);
927 TAILQ_FOREACH(pspark, &pmp->pmp_req_sizepark, pkso_entries) {
928 if (pspark->pkso_reqid == psop_user->pso_reqid) {
929 TAILQ_REMOVE(&pmp->pmp_req_sizepark, pspark,
930 pkso_entries);
931 break;
932 }
933 }
934 mutex_exit(&pmp->pmp_lock);
935
936 if (pspark == NULL)
937 return EINVAL;
938
939 error = 0;
940 copylen = MIN(pspark->pkso_bufsize, psop_user->pso_bufsize);
941
942 /*
943 * XXX: uvm stuff to avoid bouncy-bouncy copying?
944 */
945 if (PUFFS_SIZEOP_UIO(pspark->pkso_reqtype)) {
946 kernbuf = malloc(copylen, M_PUFFS, M_WAITOK | M_ZERO);
947 if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_IN) {
948 error = copyin(psop_user->pso_userbuf,
949 kernbuf, copylen);
950 if (error) {
951 printf("psop ERROR1 %d\n", error);
952 goto escape;
953 }
954 }
955 error = uiomove(kernbuf, copylen, pspark->pkso_uio);
956 if (error) {
957 printf("uiomove from kernel %p, len %d failed: %d\n",
958 kernbuf, (int)copylen, error);
959 goto escape;
960 }
961
962 if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_UIO_OUT) {
963 error = copyout(kernbuf,
964 psop_user->pso_userbuf, copylen);
965 if (error) {
966 printf("psop ERROR2 %d\n", error);
967 goto escape;
968 }
969 }
970 escape:
971 free(kernbuf, M_PUFFS);
972 } else if (PUFFS_SIZEOP_BUF(pspark->pkso_reqtype)) {
973 copylen = MAX(pspark->pkso_bufsize, psop_user->pso_bufsize);
974 if (pspark->pkso_reqtype == PUFFS_SIZEOPREQ_BUF_IN) {
975 error = copyin(psop_user->pso_userbuf,
976 pspark->pkso_copybuf, copylen);
977 } else {
978 error = copyout(pspark->pkso_copybuf,
979 psop_user->pso_userbuf, copylen);
980 }
981 }
982 #ifdef DIAGNOSTIC
983 else
984 panic("puffssizeop: invalid reqtype %d\n",
985 pspark->pkso_reqtype);
986 #endif /* DIAGNOSTIC */
987
988 return error;
989 }
990 #endif
991