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