1 /* $NetBSD: puffs_msgif.c,v 1.87 2011/07/03 08:57:43 mrg Exp $ */
4 * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/param.h>
33 #include <sys/kthread.h>
35 #include <sys/malloc.h>
36 #include <sys/objcache.h>
37 #include <sys/mount.h>
38 #include <sys/namei.h>
40 #include <sys/signal2.h>
41 #include <sys/vnode.h>
42 #include <machine/inttypes.h>
44 #include <dev/misc/putter/putter_sys.h>
45 #include <vfs/puffs/puffs_msgif.h>
46 #include <vfs/puffs/puffs_sys.h>
49 * waitq data structures
53 * While a request is going to userspace, park the caller within the
54 * kernel. This is the kernel counterpart of "struct puffs_req".
56 struct puffs_msgpark {
57 struct puffs_req *park_preq; /* req followed by buf */
59 size_t park_copylen; /* userspace copylength */
60 size_t park_maxlen; /* max size in comeback */
62 struct puffs_req *park_creq; /* non-compat preq */
63 size_t park_creqlen; /* non-compat preq len */
65 parkdone_fn park_done; /* "biodone" a'la puffs */
74 TAILQ_ENTRY(puffs_msgpark) park_entries;
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
84 static struct objcache *parkpc;
90 makepark(void *obj, void *privdata, int flags)
92 struct puffs_msgpark *park = obj;
94 lockinit(&park->park_mtx, "puffs park_mtx", 0, 0);
95 cv_init(&park->park_cv, "puffsrpl");
101 nukepark(void *obj, void *privdata)
103 struct puffs_msgpark *park = obj;
105 cv_destroy(&park->park_cv);
106 lockuninit(&park->park_mtx);
110 puffs_msgif_init(void)
113 parkpc = objcache_create_mbacked(M_PUFFS, sizeof(struct puffs_msgpark),
114 NULL, 0, makepark, nukepark, NULL);
118 puffs_msgif_destroy(void)
121 objcache_destroy(parkpc);
124 static struct puffs_msgpark *
125 puffs_msgpark_alloc(int waitok)
127 struct puffs_msgpark *park;
129 park = objcache_get(parkpc, waitok ? M_WAITOK : M_NOWAIT);
133 park->park_refcount = 1;
134 park->park_preq = park->park_creq = NULL;
135 park->park_flags = PARKFLAG_WANTREPLY;
145 puffs_msgpark_reference(struct puffs_msgpark *park)
148 KKASSERT(lockstatus(&park->park_mtx, curthread) == LK_EXCLUSIVE);
149 park->park_refcount++;
153 * Release reference to park structure.
156 puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
158 struct puffs_req *preq = park->park_preq;
159 struct puffs_req *creq = park->park_creq;
162 KKASSERT(lockstatus(&park->park_mtx, curthread) == LK_EXCLUSIVE);
163 refcnt = park->park_refcount -= howmany;
164 lockmgr(&park->park_mtx, LK_RELEASE);
166 KKASSERT(refcnt >= 0);
170 kfree(preq, M_PUFFS);
173 kfree(creq, M_PUFFS);
175 objcache_put(parkpc, park);
182 #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
186 parkdump(struct puffs_msgpark *park)
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));
200 parkqdump(struct puffs_wq *q, int dumpall)
202 struct puffs_msgpark *park;
205 TAILQ_FOREACH(park, q, park_entries) {
210 DPRINTF_VERBOSE(("puffs waitqueue at %p dumped, %d total\n", q, total));
213 #endif /* PUFFSDEBUG */
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.
223 puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
226 struct puffs_msgpark *park;
229 m = kmalloc(len, M_PUFFS, M_ZERO | (cansleep ? M_WAITOK : M_NOWAIT));
231 KKASSERT(cansleep == 0);
235 park = puffs_msgpark_alloc(cansleep);
237 KKASSERT(cansleep == 0);
243 park->park_maxlen = park->park_copylen = len;
252 puffs_msgmem_release(struct puffs_msgpark *park)
258 lockmgr(&park->park_mtx, LK_EXCLUSIVE);
259 puffs_msgpark_release(park);
263 puffs_msg_setfaf(struct puffs_msgpark *park)
266 KKASSERT((park->park_flags & PARKFLAG_CALL) == 0);
267 park->park_flags &= ~PARKFLAG_WANTREPLY;
271 puffs_msg_setdelta(struct puffs_msgpark *park, size_t delta)
274 KKASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
275 park->park_copylen = park->park_maxlen - delta;
279 puffs_msg_setinfo(struct puffs_msgpark *park, int class, int type,
283 park->park_preq->preq_opclass = PUFFSOP_OPCLASS(class);
284 park->park_preq->preq_optype = type;
285 park->park_preq->preq_cookie = ck;
289 puffs_msg_setcall(struct puffs_msgpark *park, parkdone_fn donefn, void *donearg)
292 KKASSERT(park->park_flags & PARKFLAG_WANTREPLY);
293 park->park_done = donefn;
294 park->park_donearg = donearg;
295 park->park_flags |= PARKFLAG_CALL;
299 * kernel-user-kernel waitqueues
303 puffs_getmsgid(struct puffs_mount *pmp)
307 lockmgr(&pmp->pmp_lock, LK_EXCLUSIVE);
308 rv = pmp->pmp_nextmsgid++;
309 lockmgr(&pmp->pmp_lock, LK_RELEASE);
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.
322 puffs_msg_enqueue(struct puffs_mount *pmp, struct puffs_msgpark *park)
324 struct thread *td = curthread;
326 struct puffs_req *preq;
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.
334 KKASSERT(pmp != NULL && park != NULL);
335 park->park_flags &= ~(PARKFLAG_DONE | PARKFLAG_HASERROR);
336 KKASSERT((park->park_flags & PARKFLAG_WAITERGONE) == 0);
339 preq = park->park_preq;
341 preq->preq_buflen = park->park_maxlen;
342 KKASSERT(preq->preq_id == 0
343 || (preq->preq_opclass & PUFFSOPFLAG_ISRESPONSE));
345 if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
346 preq->preq_opclass |= PUFFSOPFLAG_FAF;
348 preq->preq_id = puffs_getmsgid(pmp);
350 /* fill in caller information */
351 if (td->td_proc == NULL || td->td_lwp == NULL) {
352 DPRINTF_VERBOSE(("puffs_msg_enqueue: no process\n"));
357 preq->preq_pid = td->td_proc->p_pid;
358 preq->preq_lid = td->td_lwp->lwp_tid;
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.
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))) {
374 * see the comment about signals in puffs_msg_wait.
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 |=
388 park->park_flags &= ~PARKFLAG_WANTREPLY;
389 DPRINTF_VERBOSE(("puffs_msg_enqueue: "
390 "converted to FAF %p\n", park));
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;
407 parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
408 parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
412 * Note: we don't need to lock park since we have the only
413 * reference to it at this point.
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);
421 cv_broadcast(&pmp->pmp_msg_waiter_cv);
422 putter_notify(pmp->pmp_pi);
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));
430 puffs_msg_wait(struct puffs_mount *pmp, struct puffs_msgpark *park)
432 struct puffs_req *preq = park->park_preq; /* XXX: hmmm */
434 struct lwp *l = curthread->td_lwp;
435 struct proc *p = curthread->td_proc;
442 KKASSERT(pmp != NULL && park != NULL);
445 * block unimportant signals.
447 * The set of "important" signals here was chosen to be same as
448 * nfs interruptible mount.
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);
462 lockmgr(&pmp->pmp_lock, LK_EXCLUSIVE);
463 puffs_mp_reference(pmp);
464 lockmgr(&pmp->pmp_lock, LK_RELEASE);
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);
475 if ((park->park_flags & PARKFLAG_WANTREPLY) == 0
476 || (park->park_flags & PARKFLAG_CALL)) {
477 lockmgr(&park->park_mtx, LK_RELEASE);
482 error = cv_wait_sig(&park->park_cv, &park->park_mtx);
483 DPRINTF_VERBOSE(("puffs_touser: waiter for %p woke up with %d\n",
486 park->park_flags |= PARKFLAG_WAITERGONE;
487 if (park->park_flags & PARKFLAG_DONE) {
489 lockmgr(&park->park_mtx, LK_RELEASE);
492 * ok, we marked it as going away, but
493 * still need to do queue ops. take locks
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.
500 lockmgr(&park->park_mtx, LK_RELEASE);
501 lockmgr(&pmp->pmp_lock, LK_EXCLUSIVE);
502 lockmgr(&park->park_mtx, LK_EXCLUSIVE);
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
512 if (park->park_flags & PARKFLAG_ONQUEUE1) {
513 TAILQ_REMOVE(&pmp->pmp_msg_touser,
515 puffs_msgpark_release(park);
516 pmp->pmp_msg_touser_count--;
517 park->park_flags &= ~PARKFLAG_ONQUEUE1;
519 lockmgr(&park->park_mtx, LK_RELEASE);
521 lockmgr(&pmp->pmp_lock, LK_RELEASE);
527 lockmgr(&park->park_mtx, LK_RELEASE);
531 lockmgr(&pmp->pmp_lock, LK_EXCLUSIVE);
532 puffs_mp_release(pmp);
533 lockmgr(&pmp->pmp_lock, LK_RELEASE);
536 lockmgr(p->p_lock, LK_EXCLUSIVE);
537 sigprocmask1(l, SIG_SETMASK, &oss, NULL);
538 lockmgr(p->p_lock, LK_RELEASE);
545 * XXX: this suuuucks. Hopefully I'll get rid of this lossage once
546 * the whole setback-nonsense gets fixed.
549 puffs_msg_wait2(struct puffs_mount *pmp, struct puffs_msgpark *park,
550 struct puffs_node *pn1, struct puffs_node *pn2)
552 struct puffs_req *preq;
555 rv = puffs_msg_wait(pmp, park);
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;
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;
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.
578 puffs_msg_sendresp(struct puffs_mount *pmp, struct puffs_req *origpreq, int rv)
580 struct puffs_msgpark *park;
581 struct puffs_req *preq;
583 puffs_msgmem_alloc(sizeof(struct puffs_req), &park, (void *)&preq, 1);
584 puffs_msg_setfaf(park); /* XXXXXX: avoids reqid override */
586 memcpy(preq, origpreq, sizeof(struct puffs_req));
588 preq->preq_opclass |= PUFFSOPFLAG_ISRESPONSE;
590 puffs_msg_enqueue(pmp, park);
591 puffs_msgmem_release(park);
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.
600 puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
601 uint8_t **data, size_t *dlen, void **parkptr)
603 struct puffs_mount *pmp = this;
604 struct puffs_msgpark *park = NULL;
605 struct puffs_req *preq = NULL;
609 lockmgr(&pmp->pmp_lock, LK_EXCLUSIVE);
610 puffs_mp_reference(pmp);
613 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
618 /* need platinum yendorian express card? */
619 if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
620 DPRINTF_VERBOSE(("puffs_getout: no outgoing op, "));
622 DPRINTF_VERBOSE(("returning EWOULDBLOCK\n"));
626 DPRINTF_VERBOSE(("waiting ...\n"));
628 error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
636 park = TAILQ_FIRST(&pmp->pmp_msg_touser);
640 lockmgr(&park->park_mtx, LK_EXCLUSIVE);
641 puffs_msgpark_reference(park);
643 DPRINTF_VERBOSE(("puffs_getout: found park at %p, ", park));
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);
651 preq = park->park_preq;
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
660 if (maxsize < preq->preq_frhdr.pfr_len) {
661 DPRINTF(("buffer too small\n"));
662 puffs_msgpark_release(park);
668 DPRINTF_VERBOSE(("returning\n"));
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.
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);
683 pmp->pmp_msg_touser_count--;
684 KKASSERT(pmp->pmp_msg_touser_count >= 0);
688 puffs_mp_release(pmp);
689 lockmgr(&pmp->pmp_lock, LK_RELEASE);
692 *data = (uint8_t *)preq;
693 preq->preq_pth.pth_framelen = park->park_copylen;
694 *dlen = preq->preq_pth.pth_framelen;
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.
707 puffs_msgif_releaseout(void *this, void *parkptr, int status)
709 struct puffs_mount *pmp = this;
710 struct puffs_msgpark *park = parkptr;
712 DPRINTF_VERBOSE(("puffs_releaseout: returning park %p, errno %d: " ,
714 lockmgr(&pmp->pmp_lock, LK_EXCLUSIVE);
715 lockmgr(&park->park_mtx, LK_EXCLUSIVE);
716 if (park->park_flags & PARKFLAG_WANTREPLY) {
718 DPRINTF_VERBOSE(("enqueue replywait\n"));
719 TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
721 park->park_flags |= PARKFLAG_ONQUEUE2;
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);
728 puffs_msgpark_release(park);
730 DPRINTF_VERBOSE(("release\n"));
731 puffs_msgpark_release1(park, 2);
733 lockmgr(&pmp->pmp_lock, LK_RELEASE);
737 puffs_msgif_waitcount(void *this)
739 struct puffs_mount *pmp = this;
742 lockmgr(&pmp->pmp_lock, LK_EXCLUSIVE);
743 rv = pmp->pmp_msg_touser_count;
744 lockmgr(&pmp->pmp_lock, LK_RELEASE);
750 * XXX: locking with this one?
753 puffsop_msg(void *this, struct puffs_req *preq)
755 struct puffs_mount *pmp = this;
756 struct putter_hdr *pth = &preq->preq_pth;
757 struct puffs_msgpark *park;
760 lockmgr(&pmp->pmp_lock, LK_EXCLUSIVE);
763 TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
764 if (park->park_preq->preq_id == preq->preq_id)
768 DPRINTF_VERBOSE(("puffsop_msg: no request: %" PRIu64 "\n",
770 lockmgr(&pmp->pmp_lock, LK_RELEASE);
771 return; /* XXX send error */
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,
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 */
786 wgone = park->park_flags & PARKFLAG_WAITERGONE;
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);
794 DPRINTF_VERBOSE(("puffsop_msg: bad service - waiter gone for "
797 memcpy(park->park_preq, preq, pth->pth_framelen);
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);
807 DPRINTF_VERBOSE(("puffs_putop: flagging done for "
809 cv_signal(&park->park_cv);
812 park->park_flags |= PARKFLAG_DONE;
813 puffs_msgpark_release1(park, 2);
817 puffsop_flush(struct puffs_mount *pmp, struct puffs_flush *pf)
826 KKASSERT(pf->pf_req.preq_pth.pth_framelen == sizeof(struct puffs_flush));
829 if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
831 cache_purgevfs(PMPTOMP(pmp));
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.
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.
847 rv = puffs_cookie2vnode(pmp, pf->pf_cookie, 0, &vp);
849 if (rv == PUFFS_NOSUCHCOOKIE)
856 /* not quite ready, yet */
857 case PUFFS_INVAL_NAMECACHE_NODE:
858 struct componentname *pf_cn;
860 /* get comfortab^Wcomponentname */
861 pf_cn = kmem_alloc(componentname);
862 memset(pf_cn, 0, sizeof(struct componentname));
866 case PUFFS_INVAL_NAMECACHE_DIR:
867 if (vp->v_type != VDIR) {
872 /* deadlocks, needs its own kernel thread */
878 case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
881 case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
885 if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
890 offlo = trunc_page(pf->pf_start);
891 offhi = round_page(pf->pf_end);
892 if (offhi != 0 && offlo >= offhi) {
897 lockmgr(&vp->v_uobj.vmobjlock, LK_EXCLUSIVE);
898 rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
909 puffs_msg_sendresp(pmp, &pf->pf_req, rv);
913 puffs_msgif_dispatch(void *this, struct putter_hdr *pth)
915 struct puffs_mount *pmp = this;
916 struct puffs_req *preq = (struct puffs_req *)pth;
917 struct puffs_sopreq *psopr;
919 if (pth->pth_framelen < sizeof(struct puffs_req)) {
920 puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
924 switch (PUFFSOP_OPCLASS(preq->preq_opclass)) {
927 DPRINTF_VERBOSE(("dispatch: vn/vfs message 0x%x\n",
929 puffsop_msg(pmp, preq);
932 case PUFFSOP_FLUSH: /* process in sop thread */
934 struct puffs_flush *pf;
936 DPRINTF(("dispatch: flush 0x%x\n", preq->preq_optype));
938 if (preq->preq_pth.pth_framelen != sizeof(struct puffs_flush)) {
939 puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
942 pf = (struct puffs_flush *)preq;
944 psopr = kmalloc(sizeof(*psopr), M_PUFFS, M_WAITOK);
945 memcpy(&psopr->psopr_pf, pf, sizeof(*pf));
946 psopr->psopr_sopreq = PUFFS_SOPREQ_FLUSH;
948 lockmgr(&pmp->pmp_sopmtx, LK_EXCLUSIVE);
949 if (pmp->pmp_sopthrcount == 0) {
950 lockmgr(&pmp->pmp_sopmtx, LK_RELEASE);
951 kfree(psopr, M_PUFFS);
952 puffs_msg_sendresp(pmp, preq, ENXIO);
954 TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs,
955 psopr, psopr_entries);
956 cv_signal(&pmp->pmp_sopcv);
957 lockmgr(&pmp->pmp_sopmtx, LK_RELEASE);
962 case PUFFSOP_UNMOUNT: /* process in sop thread */
965 DPRINTF(("dispatch: unmount 0x%x\n", preq->preq_optype));
967 psopr = kmalloc(sizeof(*psopr), M_PUFFS, M_WAITOK);
968 psopr->psopr_preq = *preq;
969 psopr->psopr_sopreq = PUFFS_SOPREQ_UNMOUNT;
971 lockmgr(&pmp->pmp_sopmtx, LK_EXCLUSIVE);
972 if (pmp->pmp_sopthrcount == 0) {
973 lockmgr(&pmp->pmp_sopmtx, LK_RELEASE);
974 kfree(psopr, M_PUFFS);
975 puffs_msg_sendresp(pmp, preq, ENXIO);
977 TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs,
978 psopr, psopr_entries);
979 cv_signal(&pmp->pmp_sopcv);
980 lockmgr(&pmp->pmp_sopmtx, LK_RELEASE);
986 DPRINTF(("dispatch: invalid class 0x%x\n", preq->preq_opclass));
987 puffs_msg_sendresp(pmp, preq, EOPNOTSUPP);
995 * Work loop for thread processing all ops from server which
996 * cannot safely be handled in caller context. This includes
997 * everything which might need a lock currently "held" by the file
998 * server, i.e. a long-term kernel lock which will be released only
999 * once the file server acknowledges a request
1002 puffs_sop_thread(void *arg)
1004 struct puffs_mount *pmp = arg;
1005 struct mount *mp = PMPTOMP(pmp);
1006 struct puffs_sopreq *psopr;
1007 boolean_t keeprunning;
1008 boolean_t unmountme = FALSE;
1010 lockmgr(&pmp->pmp_sopmtx, LK_EXCLUSIVE);
1011 for (keeprunning = TRUE; keeprunning; ) {
1012 while ((psopr = TAILQ_FIRST(&pmp->pmp_sopreqs)) == NULL)
1013 cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
1014 TAILQ_REMOVE(&pmp->pmp_sopreqs, psopr, psopr_entries);
1015 lockmgr(&pmp->pmp_sopmtx, LK_RELEASE);
1017 switch (psopr->psopr_sopreq) {
1018 case PUFFS_SOPREQSYS_EXIT:
1019 keeprunning = FALSE;
1021 case PUFFS_SOPREQ_FLUSH:
1022 puffsop_flush(pmp, &psopr->psopr_pf);
1024 case PUFFS_SOPREQ_UNMOUNT:
1025 puffs_msg_sendresp(pmp, &psopr->psopr_preq, 0);
1028 keeprunning = FALSE;
1033 kfree(psopr, M_PUFFS);
1034 lockmgr(&pmp->pmp_sopmtx, LK_EXCLUSIVE);
1038 * Purge remaining ops.
1040 while ((psopr = TAILQ_FIRST(&pmp->pmp_sopreqs)) != NULL) {
1041 TAILQ_REMOVE(&pmp->pmp_sopreqs, psopr, psopr_entries);
1042 lockmgr(&pmp->pmp_sopmtx, LK_RELEASE);
1043 puffs_msg_sendresp(pmp, &psopr->psopr_preq, ENXIO);
1044 kfree(psopr, M_PUFFS);
1045 lockmgr(&pmp->pmp_sopmtx, LK_EXCLUSIVE);
1048 pmp->pmp_sopthrcount--;
1049 cv_broadcast(&pmp->pmp_sopcv);
1050 lockmgr(&pmp->pmp_sopmtx, LK_RELEASE); /* not allowed to access fs after this */
1053 * If unmount was requested, we can now safely do it here, since
1054 * our context is dead from the point-of-view of puffs_unmount()
1055 * and we are just another thread. dounmount() makes internally
1056 * sure that VFS_UNMOUNT() isn't called reentrantly and that it
1057 * is eventually completed.
1060 (void)dounmount(mp, MNT_FORCE);
1067 puffs_msgif_close(void *this)
1069 struct puffs_mount *pmp = this;
1070 struct mount *mp = PMPTOMP(pmp);
1072 lockmgr(&pmp->pmp_lock, LK_EXCLUSIVE);
1073 puffs_mp_reference(pmp);
1076 * Free the waiting callers before proceeding any further.
1077 * The syncer might be jogging around in this file system
1078 * currently. If we allow it to go to the userspace of no
1079 * return while trying to get the syncer lock, well ...
1081 puffs_userdead(pmp);
1084 * Make sure someone from puffs_unmount() isn't currently in
1085 * userspace. If we don't take this precautionary step,
1086 * they might notice that the mountpoint has disappeared
1087 * from under them once they return. Especially note that we
1088 * cannot simply test for an unmounter before calling
1089 * dounmount(), since it might be possible that that particular
1090 * invocation of unmount was called without MNT_FORCE. Here we
1091 * *must* make sure unmount succeeds. Also, restart is necessary
1092 * since pmp isn't locked. We might end up with PUTTER_DEAD after
1093 * restart and exit from there.
1095 if (pmp->pmp_unmounting) {
1096 cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
1097 puffs_mp_release(pmp);
1098 lockmgr(&pmp->pmp_lock, LK_RELEASE);
1099 DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
1104 /* Won't access pmp from here anymore */
1105 puffs_mp_release(pmp);
1106 lockmgr(&pmp->pmp_lock, LK_RELEASE);
1108 /* Detach from VFS. */
1109 (void)dounmount(mp, MNT_FORCE);
1115 * We're dead, kaput, RIP, slightly more than merely pining for the
1116 * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
1117 * our maker, ceased to be, etcetc. YASD. It's a dead FS!
1119 * Caller must hold puffs mutex.
1122 puffs_userdead(struct puffs_mount *pmp)
1124 struct puffs_msgpark *park, *park_next;
1127 * Mark filesystem status as dying so that operations don't
1128 * attempt to march to userspace any longer.
1130 pmp->pmp_status = PUFFSTAT_DYING;
1132 /* signal waiters on REQUEST TO file server queue */
1133 for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
1136 lockmgr(&park->park_mtx, LK_EXCLUSIVE);
1137 puffs_msgpark_reference(park);
1138 park_next = TAILQ_NEXT(park, park_entries);
1140 KKASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
1141 TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
1142 park->park_flags &= ~PARKFLAG_ONQUEUE1;
1143 pmp->pmp_msg_touser_count--;
1146 * Even though waiters on QUEUE1 are removed in touser()
1147 * in case of WAITERGONE, it is still possible for us to
1148 * get raced here due to having to retake locks in said
1149 * touser(). In the race case simply "ignore" the item
1150 * on the queue and move on to the next one.
1152 if (park->park_flags & PARKFLAG_WAITERGONE) {
1153 KKASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1154 KKASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1155 puffs_msgpark_release(park);
1158 opclass = park->park_preq->preq_opclass;
1159 park->park_preq->preq_rv = ENXIO;
1161 if (park->park_flags & PARKFLAG_CALL) {
1162 park->park_done(pmp, park->park_preq,
1163 park->park_donearg);
1164 puffs_msgpark_release1(park, 2);
1165 } else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
1166 puffs_msgpark_release1(park, 2);
1168 park->park_preq->preq_rv = ENXIO;
1169 cv_signal(&park->park_cv);
1170 puffs_msgpark_release(park);
1175 /* signal waiters on RESPONSE FROM file server queue */
1176 for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
1177 lockmgr(&park->park_mtx, LK_EXCLUSIVE);
1178 puffs_msgpark_reference(park);
1179 park_next = TAILQ_NEXT(park, park_entries);
1181 KKASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
1182 KKASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1184 TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
1185 park->park_flags &= ~PARKFLAG_ONQUEUE2;
1187 if (park->park_flags & PARKFLAG_WAITERGONE) {
1188 KKASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1189 puffs_msgpark_release(park);
1191 park->park_preq->preq_rv = ENXIO;
1192 if (park->park_flags & PARKFLAG_CALL) {
1193 park->park_done(pmp, park->park_preq,
1194 park->park_donearg);
1195 puffs_msgpark_release1(park, 2);
1197 cv_signal(&park->park_cv);
1198 puffs_msgpark_release(park);
1203 cv_broadcast(&pmp->pmp_msg_waiter_cv);