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