puffs_msgif.c revision 1.88 1 /* $NetBSD: puffs_msgif.c,v 1.88 2011/10/18 15:39:09 manu 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.88 2011/10/18 15:39:09 manu Exp $");
34
35 #include <sys/param.h>
36 #include <sys/atomic.h>
37 #include <sys/kmem.h>
38 #include <sys/kthread.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
43 #include <sys/proc.h>
44 #include <sys/vnode.h>
45 #include <sys/atomic.h>
46
47 #include <uvm/uvm.h>
48
49 #include <dev/putter/putter_sys.h>
50
51 #include <fs/puffs/puffs_msgif.h>
52 #include <fs/puffs/puffs_sys.h>
53
54 #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
55
56 /*
57 * waitq data structures
58 */
59
60 /*
61 * While a request is going to userspace, park the caller within the
62 * kernel. This is the kernel counterpart of "struct puffs_req".
63 */
64 struct puffs_msgpark {
65 struct puffs_req *park_preq; /* req followed by buf */
66
67 size_t park_copylen; /* userspace copylength */
68 size_t park_maxlen; /* max size in comeback */
69
70 struct puffs_req *park_creq; /* non-compat preq */
71 size_t park_creqlen; /* non-compat preq len */
72
73 parkdone_fn park_done; /* "biodone" a'la puffs */
74 void *park_donearg;
75
76 int park_flags;
77 int park_refcount;
78
79 kcondvar_t park_cv;
80 kmutex_t park_mtx;
81
82 TAILQ_ENTRY(puffs_msgpark) park_entries;
83 };
84 #define PARKFLAG_WAITERGONE 0x01
85 #define PARKFLAG_DONE 0x02
86 #define PARKFLAG_ONQUEUE1 0x04
87 #define PARKFLAG_ONQUEUE2 0x08
88 #define PARKFLAG_CALL 0x10
89 #define PARKFLAG_WANTREPLY 0x20
90 #define PARKFLAG_HASERROR 0x40
91
92 static pool_cache_t parkpc;
93 #ifdef PUFFSDEBUG
94 static int totalpark;
95 #endif
96
97 static int
98 makepark(void *arg, void *obj, int flags)
99 {
100 struct puffs_msgpark *park = obj;
101
102 mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
103 cv_init(&park->park_cv, "puffsrpl");
104
105 return 0;
106 }
107
108 static void
109 nukepark(void *arg, void *obj)
110 {
111 struct puffs_msgpark *park = obj;
112
113 cv_destroy(&park->park_cv);
114 mutex_destroy(&park->park_mtx);
115 }
116
117 void
118 puffs_msgif_init(void)
119 {
120
121 parkpc = pool_cache_init(sizeof(struct puffs_msgpark), 0, 0, 0,
122 "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
123 }
124
125 void
126 puffs_msgif_destroy(void)
127 {
128
129 pool_cache_destroy(parkpc);
130 }
131
132 static struct puffs_msgpark *
133 puffs_msgpark_alloc(int waitok)
134 {
135 struct puffs_msgpark *park;
136
137 #ifdef DIAGNOSTIC
138 if (curlwp == uvm.pagedaemon_lwp)
139 KASSERT(!waitok);
140 #endif
141 park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
142 if (park == NULL)
143 return park;
144
145 park->park_refcount = 1;
146 park->park_preq = park->park_creq = NULL;
147 park->park_flags = PARKFLAG_WANTREPLY;
148
149 #ifdef PUFFSDEBUG
150 totalpark++;
151 #endif
152
153 return park;
154 }
155
156 static void
157 puffs_msgpark_reference(struct puffs_msgpark *park)
158 {
159
160 KASSERT(mutex_owned(&park->park_mtx));
161 park->park_refcount++;
162 }
163
164 /*
165 * Release reference to park structure.
166 */
167 static void
168 puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
169 {
170 struct puffs_req *preq = park->park_preq;
171 struct puffs_req *creq = park->park_creq;
172 int refcnt;
173
174 KASSERT(mutex_owned(&park->park_mtx));
175 refcnt = park->park_refcount -= howmany;
176 mutex_exit(&park->park_mtx);
177
178 KASSERT(refcnt >= 0);
179
180 if (refcnt == 0) {
181 if (preq)
182 kmem_free(preq, park->park_maxlen);
183 #if 1
184 if (creq)
185 kmem_free(creq, park->park_creqlen);
186 #endif
187 pool_cache_put(parkpc, park);
188
189 #ifdef PUFFSDEBUG
190 totalpark--;
191 #endif
192 }
193 }
194 #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
195
196 #ifdef PUFFSDEBUG
197 static void
198 parkdump(struct puffs_msgpark *park)
199 {
200
201 DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
202 "\tcopy %zu, max %zu - done: %p/%p\n"
203 "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
204 park, park->park_preq, park->park_preq->preq_id,
205 park->park_copylen, park->park_maxlen,
206 park->park_done, park->park_donearg,
207 park->park_flags, park->park_refcount,
208 &park->park_cv, &park->park_mtx));
209 }
210
211 static void
212 parkqdump(struct puffs_wq *q, int dumpall)
213 {
214 struct puffs_msgpark *park;
215 int total = 0;
216
217 TAILQ_FOREACH(park, q, park_entries) {
218 if (dumpall)
219 parkdump(park);
220 total++;
221 }
222 DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
223
224 }
225 #endif /* PUFFSDEBUG */
226
227 /*
228 * A word about locking in the park structures: the lock protects the
229 * fields of the *park* structure (not preq) and acts as an interlock
230 * in cv operations. The lock is always internal to this module and
231 * callers do not need to worry about it.
232 */
233
234 int
235 puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
236 int cansleep)
237 {
238 struct puffs_msgpark *park;
239 void *m;
240
241 #ifdef DIAGNOSTIC
242 if (curlwp == uvm.pagedaemon_lwp)
243 KASSERT(!cansleep);
244 #endif
245 m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
246 if (m == NULL) {
247 KASSERT(cansleep == 0);
248 return ENOMEM;
249 }
250
251 park = puffs_msgpark_alloc(cansleep);
252 if (park == NULL) {
253 KASSERT(cansleep == 0);
254 kmem_free(m, len);
255 return ENOMEM;
256 }
257
258 park->park_preq = m;
259 park->park_maxlen = park->park_copylen = len;
260
261 *ppark = park;
262 *mem = m;
263
264 return 0;
265 }
266
267 void
268 puffs_msgmem_release(struct puffs_msgpark *park)
269 {
270
271 if (park == NULL)
272 return;
273
274 mutex_enter(&park->park_mtx);
275 puffs_msgpark_release(park);
276 }
277
278 void
279 puffs_msg_setfaf(struct puffs_msgpark *park)
280 {
281
282 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
283 park->park_flags &= ~PARKFLAG_WANTREPLY;
284 }
285
286 void
287 puffs_msg_setdelta(struct puffs_msgpark *park, size_t delta)
288 {
289
290 KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
291 park->park_copylen = park->park_maxlen - delta;
292 }
293
294 void
295 puffs_msg_setinfo(struct puffs_msgpark *park, int class, int type,
296 puffs_cookie_t ck)
297 {
298
299 park->park_preq->preq_opclass = PUFFSOP_OPCLASS(class);
300 park->park_preq->preq_optype = type;
301 park->park_preq->preq_cookie = ck;
302 }
303
304 void
305 puffs_msg_setcall(struct puffs_msgpark *park, parkdone_fn donefn, void *donearg)
306 {
307
308 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
309 park->park_done = donefn;
310 park->park_donearg = donearg;
311 park->park_flags |= PARKFLAG_CALL;
312 }
313
314 /*
315 * kernel-user-kernel waitqueues
316 */
317
318 static uint64_t
319 puffs_getmsgid(struct puffs_mount *pmp)
320 {
321 uint64_t rv;
322
323 mutex_enter(&pmp->pmp_lock);
324 rv = pmp->pmp_nextmsgid++;
325 mutex_exit(&pmp->pmp_lock);
326
327 return rv;
328 }
329
330 /*
331 * A word about reference counting of parks. A reference must be taken
332 * when accessing a park and additionally when it is on a queue. So
333 * when taking it off a queue and releasing the access reference, the
334 * reference count is generally decremented by 2.
335 */
336
337 void
338 puffs_msg_enqueue(struct puffs_mount *pmp, struct puffs_msgpark *park)
339 {
340 struct lwp *l = curlwp;
341 struct mount *mp;
342 struct puffs_req *preq, *creq;
343 ssize_t delta;
344
345 /*
346 * Some clients reuse a park, so reset some flags. We might
347 * want to provide a caller-side interface for this and add
348 * a few more invariant checks here, but this will do for now.
349 */
350 park->park_flags &= ~(PARKFLAG_DONE | PARKFLAG_HASERROR);
351 KASSERT((park->park_flags & PARKFLAG_WAITERGONE) == 0);
352
353 mp = PMPTOMP(pmp);
354 preq = park->park_preq;
355
356 #if 1
357 /* check if we do compat adjustments */
358 if (pmp->pmp_docompat && puffs_compat_outgoing(preq, &creq, &delta)) {
359 park->park_creq = park->park_preq;
360 park->park_creqlen = park->park_maxlen;
361
362 park->park_maxlen += delta;
363 park->park_copylen += delta;
364 park->park_preq = preq = creq;
365 }
366 #endif
367
368 preq->preq_buflen = park->park_maxlen;
369 KASSERT(preq->preq_id == 0
370 || (preq->preq_opclass & PUFFSOPFLAG_ISRESPONSE));
371
372 if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
373 preq->preq_opclass |= PUFFSOPFLAG_FAF;
374 else
375 preq->preq_id = puffs_getmsgid(pmp);
376
377 /* fill in caller information */
378 preq->preq_pid = l->l_proc->p_pid;
379 preq->preq_lid = l->l_lid;
380
381 /*
382 * To support cv_sig, yet another movie: check if there are signals
383 * pending and we are issueing a non-FAF. If so, return an error
384 * directly UNLESS we are issueing INACTIVE/RECLAIM. In that case,
385 * convert it to a FAF, fire off to the file server and return
386 * an error. Yes, this is bordering disgusting. Barfbags are on me.
387 */
388 if (__predict_false((park->park_flags & PARKFLAG_WANTREPLY)
389 && (park->park_flags & PARKFLAG_CALL) == 0
390 && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))) {
391 sigset_t ss;
392
393 /*
394 * see the comment about signals in puffs_msg_wait.
395 */
396 sigpending1(l, &ss);
397 if (sigismember(&ss, SIGINT) ||
398 sigismember(&ss, SIGTERM) ||
399 sigismember(&ss, SIGKILL) ||
400 sigismember(&ss, SIGHUP) ||
401 sigismember(&ss, SIGQUIT)) {
402 park->park_flags |= PARKFLAG_HASERROR;
403 preq->preq_rv = EINTR;
404 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
405 && (preq->preq_optype == PUFFS_VN_INACTIVE
406 || preq->preq_optype == PUFFS_VN_RECLAIM)) {
407 park->park_preq->preq_opclass |=
408 PUFFSOPFLAG_FAF;
409 park->park_flags &= ~PARKFLAG_WANTREPLY;
410 DPRINTF(("puffs_msg_enqueue: "
411 "converted to FAF %p\n", park));
412 } else {
413 return;
414 }
415 }
416 }
417
418 mutex_enter(&pmp->pmp_lock);
419 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
420 mutex_exit(&pmp->pmp_lock);
421 park->park_flags |= PARKFLAG_HASERROR;
422 preq->preq_rv = ENXIO;
423 return;
424 }
425
426 #ifdef PUFFSDEBUG
427 parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
428 parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
429 #endif
430
431 /*
432 * Note: we don't need to lock park since we have the only
433 * reference to it at this point.
434 */
435 TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
436 park->park_flags |= PARKFLAG_ONQUEUE1;
437 pmp->pmp_msg_touser_count++;
438 park->park_refcount++;
439 mutex_exit(&pmp->pmp_lock);
440
441 cv_broadcast(&pmp->pmp_msg_waiter_cv);
442 putter_notify(pmp->pmp_pi);
443
444 DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
445 "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
446 preq->preq_opclass, preq->preq_optype, park->park_flags));
447 }
448
449 int
450 puffs_msg_wait(struct puffs_mount *pmp, struct puffs_msgpark *park)
451 {
452 lwp_t *l = curlwp;
453 proc_t *p = l->l_proc;
454 struct puffs_req *preq = park->park_preq; /* XXX: hmmm */
455 sigset_t ss;
456 sigset_t oss;
457 int error = 0;
458 int rv;
459
460 /*
461 * block unimportant signals.
462 *
463 * The set of "important" signals here was chosen to be same as
464 * nfs interruptible mount.
465 */
466 sigfillset(&ss);
467 sigdelset(&ss, SIGINT);
468 sigdelset(&ss, SIGTERM);
469 sigdelset(&ss, SIGKILL);
470 sigdelset(&ss, SIGHUP);
471 sigdelset(&ss, SIGQUIT);
472 mutex_enter(p->p_lock);
473 sigprocmask1(l, SIG_BLOCK, &ss, &oss);
474 mutex_exit(p->p_lock);
475
476 mutex_enter(&pmp->pmp_lock);
477 puffs_mp_reference(pmp);
478 mutex_exit(&pmp->pmp_lock);
479
480 mutex_enter(&park->park_mtx);
481 /* did the response beat us to the wait? */
482 if (__predict_false((park->park_flags & PARKFLAG_DONE)
483 || (park->park_flags & PARKFLAG_HASERROR))) {
484 rv = park->park_preq->preq_rv;
485 mutex_exit(&park->park_mtx);
486 goto skipwait;
487 }
488
489 if ((park->park_flags & PARKFLAG_WANTREPLY) == 0
490 || (park->park_flags & PARKFLAG_CALL)) {
491 mutex_exit(&park->park_mtx);
492 rv = 0;
493 goto skipwait;
494 }
495
496 error = cv_wait_sig(&park->park_cv, &park->park_mtx);
497 DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
498 park, error));
499 if (error) {
500 park->park_flags |= PARKFLAG_WAITERGONE;
501 if (park->park_flags & PARKFLAG_DONE) {
502 rv = preq->preq_rv;
503 mutex_exit(&park->park_mtx);
504 } else {
505 /*
506 * ok, we marked it as going away, but
507 * still need to do queue ops. take locks
508 * in correct order.
509 *
510 * We don't want to release our reference
511 * if it's on replywait queue to avoid error
512 * to file server. putop() code will DTRT.
513 */
514 mutex_exit(&park->park_mtx);
515 mutex_enter(&pmp->pmp_lock);
516 mutex_enter(&park->park_mtx);
517
518 /*
519 * Still on queue1? We can safely remove it
520 * without any consequences since the file
521 * server hasn't seen it. "else" we need to
522 * wait for the response and just ignore it
523 * to avoid signalling an incorrect error to
524 * the file server.
525 */
526 if (park->park_flags & PARKFLAG_ONQUEUE1) {
527 TAILQ_REMOVE(&pmp->pmp_msg_touser,
528 park, park_entries);
529 puffs_msgpark_release(park);
530 pmp->pmp_msg_touser_count--;
531 park->park_flags &= ~PARKFLAG_ONQUEUE1;
532 } else {
533 mutex_exit(&park->park_mtx);
534 }
535 mutex_exit(&pmp->pmp_lock);
536
537 rv = EINTR;
538 }
539 } else {
540 rv = preq->preq_rv;
541 mutex_exit(&park->park_mtx);
542 }
543
544 skipwait:
545 mutex_enter(&pmp->pmp_lock);
546 puffs_mp_release(pmp);
547 mutex_exit(&pmp->pmp_lock);
548
549 mutex_enter(p->p_lock);
550 sigprocmask1(l, SIG_SETMASK, &oss, NULL);
551 mutex_exit(p->p_lock);
552
553 return rv;
554 }
555
556 /*
557 * XXX: this suuuucks. Hopefully I'll get rid of this lossage once
558 * the whole setback-nonsense gets fixed.
559 */
560 int
561 puffs_msg_wait2(struct puffs_mount *pmp, struct puffs_msgpark *park,
562 struct puffs_node *pn1, struct puffs_node *pn2)
563 {
564 struct puffs_req *preq;
565 int rv;
566
567 rv = puffs_msg_wait(pmp, park);
568
569 preq = park->park_preq;
570 if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N1)
571 pn1->pn_stat |= PNODE_DOINACT;
572 if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N2)
573 pn2->pn_stat |= PNODE_DOINACT;
574
575 if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1)
576 pn1->pn_stat |= PNODE_NOREFS;
577 if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2)
578 pn2->pn_stat |= PNODE_NOREFS;
579
580 return rv;
581
582 }
583
584 /*
585 * XXX: lazy bum. please, for the love of foie gras, fix me.
586 * This should *NOT* depend on setfaf. Also "memcpy" could
587 * be done more nicely.
588 */
589 void
590 puffs_msg_sendresp(struct puffs_mount *pmp, struct puffs_req *origpreq, int rv)
591 {
592 struct puffs_msgpark *park;
593 struct puffs_req *preq;
594
595 puffs_msgmem_alloc(sizeof(struct puffs_req), &park, (void *)&preq, 1);
596 puffs_msg_setfaf(park); /* XXXXXX: avoids reqid override */
597
598 memcpy(preq, origpreq, sizeof(struct puffs_req));
599 preq->preq_rv = rv;
600 preq->preq_opclass |= PUFFSOPFLAG_ISRESPONSE;
601
602 puffs_msg_enqueue(pmp, park);
603 puffs_msgmem_release(park);
604 }
605
606 /*
607 * Get next request in the outgoing queue. "maxsize" controls the
608 * size the caller can accommodate and "nonblock" signals if this
609 * should block while waiting for input. Handles all locking internally.
610 */
611 int
612 puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
613 uint8_t **data, size_t *dlen, void **parkptr)
614 {
615 struct puffs_mount *pmp = this;
616 struct puffs_msgpark *park = NULL;
617 struct puffs_req *preq = NULL;
618 int error;
619
620 error = 0;
621 mutex_enter(&pmp->pmp_lock);
622 puffs_mp_reference(pmp);
623 for (;;) {
624 /* RIP? */
625 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
626 error = ENXIO;
627 break;
628 }
629
630 /* need platinum yendorian express card? */
631 if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
632 DPRINTF(("puffs_getout: no outgoing op, "));
633 if (nonblock) {
634 DPRINTF(("returning EWOULDBLOCK\n"));
635 error = EWOULDBLOCK;
636 break;
637 }
638 DPRINTF(("waiting ...\n"));
639
640 error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
641 &pmp->pmp_lock);
642 if (error)
643 break;
644 else
645 continue;
646 }
647
648 park = TAILQ_FIRST(&pmp->pmp_msg_touser);
649 if (park == NULL)
650 continue;
651
652 mutex_enter(&park->park_mtx);
653 puffs_msgpark_reference(park);
654
655 DPRINTF(("puffs_getout: found park at %p, ", park));
656
657 /* If it's a goner, don't process any furher */
658 if (park->park_flags & PARKFLAG_WAITERGONE) {
659 DPRINTF(("waitergone!\n"));
660 puffs_msgpark_release(park);
661 continue;
662 }
663 preq = park->park_preq;
664
665 #if 0
666 /* check size */
667 /*
668 * XXX: this check is not valid for now, we don't know
669 * the size of the caller's input buffer. i.e. this
670 * will most likely go away
671 */
672 if (maxsize < preq->preq_frhdr.pfr_len) {
673 DPRINTF(("buffer too small\n"));
674 puffs_msgpark_release(park);
675 error = E2BIG;
676 break;
677 }
678 #endif
679
680 DPRINTF(("returning\n"));
681
682 /*
683 * Ok, we found what we came for. Release it from the
684 * outgoing queue but do not unlock. We will unlock
685 * only after we "releaseout" it to avoid complications:
686 * otherwise it is (theoretically) possible for userland
687 * to race us into "put" before we have a change to put
688 * this baby on the receiving queue.
689 */
690 TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
691 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
692 park->park_flags &= ~PARKFLAG_ONQUEUE1;
693 mutex_exit(&park->park_mtx);
694
695 pmp->pmp_msg_touser_count--;
696 KASSERT(pmp->pmp_msg_touser_count >= 0);
697
698 break;
699 }
700 puffs_mp_release(pmp);
701 mutex_exit(&pmp->pmp_lock);
702
703 if (error == 0) {
704 *data = (uint8_t *)preq;
705 preq->preq_pth.pth_framelen = park->park_copylen;
706 *dlen = preq->preq_pth.pth_framelen;
707 *parkptr = park;
708 }
709
710 return error;
711 }
712
713 /*
714 * Release outgoing structure. Now, depending on the success of the
715 * outgoing send, it is either going onto the result waiting queue
716 * or the death chamber.
717 */
718 void
719 puffs_msgif_releaseout(void *this, void *parkptr, int status)
720 {
721 struct puffs_mount *pmp = this;
722 struct puffs_msgpark *park = parkptr;
723
724 DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
725 park, status));
726 mutex_enter(&pmp->pmp_lock);
727 mutex_enter(&park->park_mtx);
728 if (park->park_flags & PARKFLAG_WANTREPLY) {
729 if (status == 0) {
730 DPRINTF(("enqueue replywait\n"));
731 TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
732 park_entries);
733 park->park_flags |= PARKFLAG_ONQUEUE2;
734 } else {
735 DPRINTF(("error path!\n"));
736 park->park_preq->preq_rv = status;
737 park->park_flags |= PARKFLAG_DONE;
738 cv_signal(&park->park_cv);
739 }
740 puffs_msgpark_release(park);
741 } else {
742 DPRINTF(("release\n"));
743 puffs_msgpark_release1(park, 2);
744 }
745 mutex_exit(&pmp->pmp_lock);
746 }
747
748 size_t
749 puffs_msgif_waitcount(void *this)
750 {
751 struct puffs_mount *pmp = this;
752 size_t rv;
753
754 mutex_enter(&pmp->pmp_lock);
755 rv = pmp->pmp_msg_touser_count;
756 mutex_exit(&pmp->pmp_lock);
757
758 return rv;
759 }
760
761 /*
762 * XXX: locking with this one?
763 */
764 static void
765 puffsop_msg(void *this, struct puffs_req *preq)
766 {
767 struct puffs_mount *pmp = this;
768 struct putter_hdr *pth = &preq->preq_pth;
769 struct puffs_msgpark *park;
770 int wgone;
771
772 mutex_enter(&pmp->pmp_lock);
773
774 /* Locate waiter */
775 TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
776 if (park->park_preq->preq_id == preq->preq_id)
777 break;
778 }
779 if (park == NULL) {
780 DPRINTF(("puffsop_msg: no request: %" PRIu64 "\n",
781 preq->preq_id));
782 mutex_exit(&pmp->pmp_lock);
783 return; /* XXX send error */
784 }
785
786 mutex_enter(&park->park_mtx);
787 puffs_msgpark_reference(park);
788 if (pth->pth_framelen > park->park_maxlen) {
789 DPRINTF(("puffsop_msg: invalid buffer length: "
790 "%" PRIu64 " (req %" PRIu64 ", \n", pth->pth_framelen,
791 preq->preq_id));
792 park->park_preq->preq_rv = EPROTO;
793 cv_signal(&park->park_cv);
794 puffs_msgpark_release1(park, 2);
795 mutex_exit(&pmp->pmp_lock);
796 return; /* XXX: error */
797 }
798 wgone = park->park_flags & PARKFLAG_WAITERGONE;
799
800 KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
801 TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
802 park->park_flags &= ~PARKFLAG_ONQUEUE2;
803 mutex_exit(&pmp->pmp_lock);
804
805 if (wgone) {
806 DPRINTF(("puffsop_msg: bad service - waiter gone for "
807 "park %p\n", park));
808 } else {
809 #if 1
810 if (park->park_creq) {
811 struct puffs_req *creq;
812 size_t csize;
813
814 KASSERT(pmp->pmp_docompat);
815 puffs_compat_incoming(preq, park->park_creq);
816 creq = park->park_creq;
817 csize = park->park_creqlen;
818 park->park_creq = park->park_preq;
819 park->park_creqlen = park->park_maxlen;
820
821 park->park_preq = creq;
822 park->park_maxlen = csize;
823
824 memcpy(park->park_creq, preq, pth->pth_framelen);
825 } else {
826 #endif
827 memcpy(park->park_preq, preq, pth->pth_framelen);
828 }
829
830 if (park->park_flags & PARKFLAG_CALL) {
831 DPRINTF(("puffsop_msg: call for %p, arg %p\n",
832 park->park_preq, park->park_donearg));
833 park->park_done(pmp, preq, park->park_donearg);
834 }
835 }
836
837 if (!wgone) {
838 DPRINTF(("puffs_putop: flagging done for "
839 "park %p\n", park));
840 cv_signal(&park->park_cv);
841 }
842
843 park->park_flags |= PARKFLAG_DONE;
844 puffs_msgpark_release1(park, 2);
845 }
846
847 static void
848 puffsop_flush(struct puffs_mount *pmp, struct puffs_flush *pf)
849 {
850 struct vnode *vp;
851 voff_t offlo, offhi;
852 int rv, flags = 0;
853
854 KASSERT(pf->pf_req.preq_pth.pth_framelen == sizeof(struct puffs_flush));
855
856 /* XXX: slurry */
857 if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
858 cache_purgevfs(PMPTOMP(pmp));
859 rv = 0;
860 goto out;
861 }
862
863 /*
864 * Get vnode, don't lock it. Namecache is protected by its own lock
865 * and we have a reference to protect against premature harvesting.
866 *
867 * The node we want here might be locked and the op is in
868 * userspace waiting for us to complete ==> deadlock. Another
869 * reason we need to eventually bump locking to userspace, as we
870 * will need to lock the node if we wish to do flushes.
871 */
872 rv = puffs_cookie2vnode(pmp, pf->pf_cookie, 0, 0, &vp);
873 if (rv) {
874 if (rv == PUFFS_NOSUCHCOOKIE)
875 rv = ENOENT;
876 goto out;
877 }
878
879 switch (pf->pf_op) {
880 #if 0
881 /* not quite ready, yet */
882 case PUFFS_INVAL_NAMECACHE_NODE:
883 struct componentname *pf_cn;
884 char *name;
885 /* get comfortab^Wcomponentname */
886 pf_cn = kmem_alloc(componentname);
887 memset(pf_cn, 0, sizeof(struct componentname));
888 break;
889
890 #endif
891 case PUFFS_INVAL_NAMECACHE_DIR:
892 if (vp->v_type != VDIR) {
893 rv = EINVAL;
894 break;
895 }
896 cache_purge1(vp, NULL, PURGE_CHILDREN);
897 break;
898
899 case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
900 flags = PGO_FREE;
901 /*FALLTHROUGH*/
902 case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
903 if (flags == 0)
904 flags = PGO_CLEANIT;
905
906 if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
907 rv = EINVAL;
908 break;
909 }
910
911 offlo = trunc_page(pf->pf_start);
912 offhi = round_page(pf->pf_end);
913 if (offhi != 0 && offlo >= offhi) {
914 rv = EINVAL;
915 break;
916 }
917
918 mutex_enter(vp->v_uobj.vmobjlock);
919 rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
920 break;
921
922 default:
923 rv = EINVAL;
924 }
925
926 vrele(vp);
927
928 out:
929 puffs_msg_sendresp(pmp, &pf->pf_req, rv);
930 }
931
932 int
933 puffs_msgif_dispatch(void *this, struct putter_hdr *pth)
934 {
935 struct puffs_mount *pmp = this;
936 struct puffs_req *preq = (struct puffs_req *)pth;
937 struct puffs_sopreq *psopr;
938
939 if (pth->pth_framelen < sizeof(struct puffs_req)) {
940 puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
941 return 0;
942 }
943
944 switch (PUFFSOP_OPCLASS(preq->preq_opclass)) {
945 case PUFFSOP_VN:
946 case PUFFSOP_VFS:
947 DPRINTF(("dispatch: vn/vfs message 0x%x\n", preq->preq_optype));
948 puffsop_msg(pmp, preq);
949 break;
950
951 case PUFFSOP_FLUSH: /* process in sop thread */
952 {
953 struct puffs_flush *pf;
954
955 DPRINTF(("dispatch: flush 0x%x\n", preq->preq_optype));
956
957 if (preq->preq_pth.pth_framelen != sizeof(struct puffs_flush)) {
958 puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
959 break;
960 }
961 pf = (struct puffs_flush *)preq;
962
963 #ifdef DIAGNOSTIC
964 KASSERT(curlwp != uvm.pagedaemon_lwp);
965 #endif
966 psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
967 memcpy(&psopr->psopr_pf, pf, sizeof(*pf));
968 psopr->psopr_sopreq = PUFFS_SOPREQ_FLUSH;
969
970 mutex_enter(&pmp->pmp_sopmtx);
971 if (pmp->pmp_sopthrcount == 0) {
972 mutex_exit(&pmp->pmp_sopmtx);
973 kmem_free(psopr, sizeof(*psopr));
974 puffs_msg_sendresp(pmp, preq, ENXIO);
975 } else {
976 TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs,
977 psopr, psopr_entries);
978 cv_signal(&pmp->pmp_sopcv);
979 mutex_exit(&pmp->pmp_sopmtx);
980 }
981 break;
982 }
983
984 case PUFFSOP_UNMOUNT: /* process in sop thread */
985 {
986
987 DPRINTF(("dispatch: unmount 0x%x\n", preq->preq_optype));
988
989 #ifdef DIAGNOSTIC
990 KASSERT(curlwp != uvm.pagedaemon_lwp);
991 #endif
992 psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
993 psopr->psopr_preq = *preq;
994 psopr->psopr_sopreq = PUFFS_SOPREQ_UNMOUNT;
995
996 mutex_enter(&pmp->pmp_sopmtx);
997 if (pmp->pmp_sopthrcount == 0) {
998 mutex_exit(&pmp->pmp_sopmtx);
999 kmem_free(psopr, sizeof(*psopr));
1000 puffs_msg_sendresp(pmp, preq, ENXIO);
1001 } else {
1002 TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs,
1003 psopr, psopr_entries);
1004 cv_signal(&pmp->pmp_sopcv);
1005 mutex_exit(&pmp->pmp_sopmtx);
1006 }
1007 break;
1008 }
1009
1010 default:
1011 DPRINTF(("dispatch: invalid class 0x%x\n", preq->preq_opclass));
1012 puffs_msg_sendresp(pmp, preq, EOPNOTSUPP);
1013 break;
1014 }
1015
1016 return 0;
1017 }
1018
1019 /*
1020 * Work loop for thread processing all ops from server which
1021 * cannot safely be handled in caller context. This includes
1022 * everything which might need a lock currently "held" by the file
1023 * server, i.e. a long-term kernel lock which will be released only
1024 * once the file server acknowledges a request
1025 */
1026 void
1027 puffs_sop_thread(void *arg)
1028 {
1029 struct puffs_mount *pmp = arg;
1030 struct mount *mp = PMPTOMP(pmp);
1031 struct puffs_sopreq *psopr;
1032 bool keeprunning;
1033 bool unmountme = false;
1034
1035 mutex_enter(&pmp->pmp_sopmtx);
1036 for (keeprunning = true; keeprunning; ) {
1037 while ((psopr = TAILQ_FIRST(&pmp->pmp_sopreqs)) == NULL)
1038 cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
1039 TAILQ_REMOVE(&pmp->pmp_sopreqs, psopr, psopr_entries);
1040 mutex_exit(&pmp->pmp_sopmtx);
1041
1042 switch (psopr->psopr_sopreq) {
1043 case PUFFS_SOPREQSYS_EXIT:
1044 keeprunning = false;
1045 break;
1046 case PUFFS_SOPREQ_FLUSH:
1047 puffsop_flush(pmp, &psopr->psopr_pf);
1048 break;
1049 case PUFFS_SOPREQ_UNMOUNT:
1050 puffs_msg_sendresp(pmp, &psopr->psopr_preq, 0);
1051
1052 unmountme = true;
1053 keeprunning = false;
1054
1055 /*
1056 * We know the mountpoint is still alive because
1057 * the thread that is us (poetic?) is still alive.
1058 */
1059 atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
1060 break;
1061 }
1062
1063 kmem_free(psopr, sizeof(*psopr));
1064 mutex_enter(&pmp->pmp_sopmtx);
1065 }
1066
1067 /*
1068 * Purge remaining ops.
1069 */
1070 while ((psopr = TAILQ_FIRST(&pmp->pmp_sopreqs)) != NULL) {
1071 TAILQ_REMOVE(&pmp->pmp_sopreqs, psopr, psopr_entries);
1072 mutex_exit(&pmp->pmp_sopmtx);
1073 puffs_msg_sendresp(pmp, &psopr->psopr_preq, ENXIO);
1074 kmem_free(psopr, sizeof(*psopr));
1075 mutex_enter(&pmp->pmp_sopmtx);
1076 }
1077
1078 pmp->pmp_sopthrcount--;
1079 cv_broadcast(&pmp->pmp_sopcv);
1080 mutex_exit(&pmp->pmp_sopmtx); /* not allowed to access fs after this */
1081
1082 /*
1083 * If unmount was requested, we can now safely do it here, since
1084 * our context is dead from the point-of-view of puffs_unmount()
1085 * and we are just another thread. dounmount() makes internally
1086 * sure that VFS_UNMOUNT() isn't called reentrantly and that it
1087 * is eventually completed.
1088 */
1089 if (unmountme) {
1090 (void)dounmount(mp, MNT_FORCE, curlwp);
1091 vfs_destroy(mp);
1092 }
1093
1094 kthread_exit(0);
1095 }
1096
1097 int
1098 puffs_msgif_close(void *this)
1099 {
1100 struct puffs_mount *pmp = this;
1101 struct mount *mp = PMPTOMP(pmp);
1102
1103 mutex_enter(&pmp->pmp_lock);
1104 puffs_mp_reference(pmp);
1105
1106 /*
1107 * Free the waiting callers before proceeding any further.
1108 * The syncer might be jogging around in this file system
1109 * currently. If we allow it to go to the userspace of no
1110 * return while trying to get the syncer lock, well ...
1111 */
1112 puffs_userdead(pmp);
1113
1114 /*
1115 * Make sure someone from puffs_unmount() isn't currently in
1116 * userspace. If we don't take this precautionary step,
1117 * they might notice that the mountpoint has disappeared
1118 * from under them once they return. Especially note that we
1119 * cannot simply test for an unmounter before calling
1120 * dounmount(), since it might be possible that that particular
1121 * invocation of unmount was called without MNT_FORCE. Here we
1122 * *must* make sure unmount succeeds. Also, restart is necessary
1123 * since pmp isn't locked. We might end up with PUTTER_DEAD after
1124 * restart and exit from there.
1125 */
1126 if (pmp->pmp_unmounting) {
1127 cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
1128 puffs_mp_release(pmp);
1129 mutex_exit(&pmp->pmp_lock);
1130 DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
1131 "restart\n", pmp));
1132 return ERESTART;
1133 }
1134
1135 /* Won't access pmp from here anymore */
1136 atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
1137 puffs_mp_release(pmp);
1138 mutex_exit(&pmp->pmp_lock);
1139
1140 /* Detach from VFS. */
1141 (void)dounmount(mp, MNT_FORCE, curlwp);
1142 vfs_destroy(mp);
1143
1144 return 0;
1145 }
1146
1147 /*
1148 * We're dead, kaput, RIP, slightly more than merely pining for the
1149 * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
1150 * our maker, ceased to be, etcetc. YASD. It's a dead FS!
1151 *
1152 * Caller must hold puffs mutex.
1153 */
1154 void
1155 puffs_userdead(struct puffs_mount *pmp)
1156 {
1157 struct puffs_msgpark *park, *park_next;
1158
1159 /*
1160 * Mark filesystem status as dying so that operations don't
1161 * attempt to march to userspace any longer.
1162 */
1163 pmp->pmp_status = PUFFSTAT_DYING;
1164
1165 /* signal waiters on REQUEST TO file server queue */
1166 for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
1167 uint8_t opclass;
1168
1169 mutex_enter(&park->park_mtx);
1170 puffs_msgpark_reference(park);
1171 park_next = TAILQ_NEXT(park, park_entries);
1172
1173 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
1174 TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
1175 park->park_flags &= ~PARKFLAG_ONQUEUE1;
1176 pmp->pmp_msg_touser_count--;
1177
1178 /*
1179 * Even though waiters on QUEUE1 are removed in touser()
1180 * in case of WAITERGONE, it is still possible for us to
1181 * get raced here due to having to retake locks in said
1182 * touser(). In the race case simply "ignore" the item
1183 * on the queue and move on to the next one.
1184 */
1185 if (park->park_flags & PARKFLAG_WAITERGONE) {
1186 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1187 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1188 puffs_msgpark_release(park);
1189
1190 } else {
1191 opclass = park->park_preq->preq_opclass;
1192 park->park_preq->preq_rv = ENXIO;
1193
1194 if (park->park_flags & PARKFLAG_CALL) {
1195 park->park_done(pmp, park->park_preq,
1196 park->park_donearg);
1197 puffs_msgpark_release1(park, 2);
1198 } else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
1199 puffs_msgpark_release1(park, 2);
1200 } else {
1201 park->park_preq->preq_rv = ENXIO;
1202 cv_signal(&park->park_cv);
1203 puffs_msgpark_release(park);
1204 }
1205 }
1206 }
1207
1208 /* signal waiters on RESPONSE FROM file server queue */
1209 for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
1210 mutex_enter(&park->park_mtx);
1211 puffs_msgpark_reference(park);
1212 park_next = TAILQ_NEXT(park, park_entries);
1213
1214 KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
1215 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1216
1217 TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
1218 park->park_flags &= ~PARKFLAG_ONQUEUE2;
1219
1220 if (park->park_flags & PARKFLAG_WAITERGONE) {
1221 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1222 puffs_msgpark_release(park);
1223 } else {
1224 park->park_preq->preq_rv = ENXIO;
1225 if (park->park_flags & PARKFLAG_CALL) {
1226 park->park_done(pmp, park->park_preq,
1227 park->park_donearg);
1228 puffs_msgpark_release1(park, 2);
1229 } else {
1230 cv_signal(&park->park_cv);
1231 puffs_msgpark_release(park);
1232 }
1233 }
1234 }
1235
1236 cv_broadcast(&pmp->pmp_msg_waiter_cv);
1237 }
1238