1 /* $NetBSD: puffs_msgif.c,v 1.78 2010/01/07 22:59:27 pooka 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/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.78 2010/01/07 22:59:27 pooka Exp $");
35 #include <sys/param.h>
36 #include <sys/atomic.h>
38 #include <sys/kthread.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/atomic.h>
47 #include <dev/putter/putter_sys.h>
49 #include <fs/puffs/puffs_msgif.h>
50 #include <fs/puffs/puffs_sys.h>
52 #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
55 * waitq data structures
59 * While a request is going to userspace, park the caller within the
60 * kernel. This is the kernel counterpart of "struct puffs_req".
62 struct puffs_msgpark
{
63 struct puffs_req
*park_preq
; /* req followed by buf */
65 size_t park_copylen
; /* userspace copylength */
66 size_t park_maxlen
; /* max size in comeback */
68 parkdone_fn park_done
; /* "biodone" a'la puffs */
77 TAILQ_ENTRY(puffs_msgpark
) park_entries
;
79 #define PARKFLAG_WAITERGONE 0x01
80 #define PARKFLAG_DONE 0x02
81 #define PARKFLAG_ONQUEUE1 0x04
82 #define PARKFLAG_ONQUEUE2 0x08
83 #define PARKFLAG_CALL 0x10
84 #define PARKFLAG_WANTREPLY 0x20
85 #define PARKFLAG_HASERROR 0x40
87 static pool_cache_t parkpc
;
93 makepark(void *arg
, void *obj
, int flags
)
95 struct puffs_msgpark
*park
= obj
;
97 mutex_init(&park
->park_mtx
, MUTEX_DEFAULT
, IPL_NONE
);
98 cv_init(&park
->park_cv
, "puffsrpl");
104 nukepark(void *arg
, void *obj
)
106 struct puffs_msgpark
*park
= obj
;
108 cv_destroy(&park
->park_cv
);
109 mutex_destroy(&park
->park_mtx
);
113 puffs_msgif_init(void)
116 parkpc
= pool_cache_init(sizeof(struct puffs_msgpark
), 0, 0, 0,
117 "puffprkl", NULL
, IPL_NONE
, makepark
, nukepark
, NULL
);
121 puffs_msgif_destroy(void)
124 pool_cache_destroy(parkpc
);
129 static struct puffs_msgpark
*
130 puffs_msgpark_alloc(int waitok
)
132 struct puffs_msgpark
*park
;
134 park
= pool_cache_get(parkpc
, waitok
? PR_WAITOK
: PR_NOWAIT
);
138 park
->park_refcount
= 1;
139 park
->park_preq
= NULL
;
140 park
->park_flags
= PARKFLAG_WANTREPLY
;
150 puffs_msgpark_reference(struct puffs_msgpark
*park
)
153 KASSERT(mutex_owned(&park
->park_mtx
));
154 park
->park_refcount
++;
158 * Release reference to park structure.
161 puffs_msgpark_release1(struct puffs_msgpark
*park
, int howmany
)
163 struct puffs_req
*preq
= park
->park_preq
;
166 KASSERT(mutex_owned(&park
->park_mtx
));
167 refcnt
= park
->park_refcount
-= howmany
;
168 mutex_exit(&park
->park_mtx
);
170 KASSERT(refcnt
>= 0);
175 kmem_free(preq
, park
->park_maxlen
);
176 pool_cache_put(parkpc
, park
);
183 #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
187 parkdump(struct puffs_msgpark
*park
)
190 DPRINTF(("park %p, preq %p, id %" PRIu64
"\n"
191 "\tcopy %zu, max %zu - done: %p/%p\n"
192 "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
193 park
, park
->park_preq
, park
->park_preq
->preq_id
,
194 park
->park_copylen
, park
->park_maxlen
,
195 park
->park_done
, park
->park_donearg
,
196 park
->park_flags
, park
->park_refcount
,
197 &park
->park_cv
, &park
->park_mtx
));
201 parkqdump(struct puffs_wq
*q
, int dumpall
)
203 struct puffs_msgpark
*park
;
206 TAILQ_FOREACH(park
, q
, park_entries
) {
211 DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q
, total
));
214 #endif /* PUFFSDEBUG */
217 * A word about locking in the park structures: the lock protects the
218 * fields of the *park* structure (not preq) and acts as an interlock
219 * in cv operations. The lock is always internal to this module and
220 * callers do not need to worry about it.
224 puffs_msgmem_alloc(size_t len
, struct puffs_msgpark
**ppark
, void **mem
,
227 struct puffs_msgpark
*park
;
230 m
= kmem_zalloc(len
, cansleep
? KM_SLEEP
: KM_NOSLEEP
);
232 KASSERT(cansleep
== 0);
236 park
= puffs_msgpark_alloc(cansleep
);
238 KASSERT(cansleep
== 0);
244 park
->park_maxlen
= park
->park_copylen
= len
;
253 puffs_msgmem_release(struct puffs_msgpark
*park
)
259 mutex_enter(&park
->park_mtx
);
260 puffs_msgpark_release(park
);
264 puffs_msg_setfaf(struct puffs_msgpark
*park
)
267 KASSERT((park
->park_flags
& PARKFLAG_CALL
) == 0);
268 park
->park_flags
&= ~PARKFLAG_WANTREPLY
;
272 puffs_msg_setdelta(struct puffs_msgpark
*park
, size_t delta
)
275 KASSERT(delta
< park
->park_maxlen
); /* "<=" wouldn't make sense */
276 park
->park_copylen
= park
->park_maxlen
- delta
;
280 puffs_msg_setinfo(struct puffs_msgpark
*park
, int class, int type
,
284 park
->park_preq
->preq_opclass
= PUFFSOP_OPCLASS(class);
285 park
->park_preq
->preq_optype
= type
;
286 park
->park_preq
->preq_cookie
= ck
;
290 puffs_msg_setcall(struct puffs_msgpark
*park
, parkdone_fn donefn
, void *donearg
)
293 KASSERT(park
->park_flags
& PARKFLAG_WANTREPLY
);
294 park
->park_done
= donefn
;
295 park
->park_donearg
= donearg
;
296 park
->park_flags
|= PARKFLAG_CALL
;
300 * kernel-user-kernel waitqueues
304 puffs_getmsgid(struct puffs_mount
*pmp
)
308 mutex_enter(&pmp
->pmp_lock
);
309 rv
= pmp
->pmp_nextmsgid
++;
310 mutex_exit(&pmp
->pmp_lock
);
316 * A word about reference counting of parks. A reference must be taken
317 * when accessing a park and additionally when it is on a queue. So
318 * when taking it off a queue and releasing the access reference, the
319 * reference count is generally decremented by 2.
323 puffs_msg_enqueue(struct puffs_mount
*pmp
, struct puffs_msgpark
*park
)
325 struct lwp
*l
= curlwp
;
327 struct puffs_req
*preq
;
330 preq
= park
->park_preq
;
331 preq
->preq_buflen
= park
->park_maxlen
;
332 KASSERT(preq
->preq_id
== 0
333 || (preq
->preq_opclass
& PUFFSOPFLAG_ISRESPONSE
));
335 if ((park
->park_flags
& PARKFLAG_WANTREPLY
) == 0)
336 preq
->preq_opclass
|= PUFFSOPFLAG_FAF
;
338 preq
->preq_id
= puffs_getmsgid(pmp
);
340 /* fill in caller information */
341 preq
->preq_pid
= l
->l_proc
->p_pid
;
342 preq
->preq_lid
= l
->l_lid
;
345 * To support cv_sig, yet another movie: check if there are signals
346 * pending and we are issueing a non-FAF. If so, return an error
347 * directly UNLESS we are issueing INACTIVE/RECLAIM. In that case,
348 * convert it to a FAF, fire off to the file server and return
349 * an error. Yes, this is bordering disgusting. Barfbags are on me.
351 if (__predict_false((park
->park_flags
& PARKFLAG_WANTREPLY
)
352 && (park
->park_flags
& PARKFLAG_CALL
) == 0
353 && (l
->l_flag
& LW_PENDSIG
) != 0 && sigispending(l
, 0))) {
354 park
->park_flags
|= PARKFLAG_HASERROR
;
355 preq
->preq_rv
= EINTR
;
356 if (PUFFSOP_OPCLASS(preq
->preq_opclass
) == PUFFSOP_VN
357 && (preq
->preq_optype
== PUFFS_VN_INACTIVE
358 || preq
->preq_optype
== PUFFS_VN_RECLAIM
)) {
359 park
->park_preq
->preq_opclass
|= PUFFSOPFLAG_FAF
;
360 park
->park_flags
&= ~PARKFLAG_WANTREPLY
;
361 DPRINTF(("puffs_msg_enqueue: converted to FAF %p\n",
368 mutex_enter(&pmp
->pmp_lock
);
369 if (pmp
->pmp_status
!= PUFFSTAT_RUNNING
) {
370 mutex_exit(&pmp
->pmp_lock
);
371 park
->park_flags
|= PARKFLAG_HASERROR
;
372 preq
->preq_rv
= ENXIO
;
377 parkqdump(&pmp
->pmp_msg_touser
, puffsdebug
> 1);
378 parkqdump(&pmp
->pmp_msg_replywait
, puffsdebug
> 1);
382 * Note: we don't need to lock park since we have the only
383 * reference to it at this point.
385 TAILQ_INSERT_TAIL(&pmp
->pmp_msg_touser
, park
, park_entries
);
386 park
->park_flags
|= PARKFLAG_ONQUEUE1
;
387 pmp
->pmp_msg_touser_count
++;
388 park
->park_refcount
++;
389 mutex_exit(&pmp
->pmp_lock
);
391 cv_broadcast(&pmp
->pmp_msg_waiter_cv
);
392 putter_notify(pmp
->pmp_pi
);
394 DPRINTF(("touser: req %" PRIu64
", preq: %p, park: %p, "
395 "c/t: 0x%x/0x%x, f: 0x%x\n", preq
->preq_id
, preq
, park
,
396 preq
->preq_opclass
, preq
->preq_optype
, park
->park_flags
));
400 puffs_msg_wait(struct puffs_mount
*pmp
, struct puffs_msgpark
*park
)
402 struct puffs_req
*preq
= park
->park_preq
; /* XXX: hmmm */
406 mutex_enter(&pmp
->pmp_lock
);
407 puffs_mp_reference(pmp
);
408 mutex_exit(&pmp
->pmp_lock
);
410 mutex_enter(&park
->park_mtx
);
411 if ((park
->park_flags
& PARKFLAG_WANTREPLY
) == 0
412 || (park
->park_flags
& PARKFLAG_CALL
)) {
413 mutex_exit(&park
->park_mtx
);
418 /* did the response beat us to the wait? */
419 if (__predict_false((park
->park_flags
& PARKFLAG_DONE
)
420 || (park
->park_flags
& PARKFLAG_HASERROR
))) {
421 rv
= park
->park_preq
->preq_rv
;
422 mutex_exit(&park
->park_mtx
);
426 error
= cv_wait_sig(&park
->park_cv
, &park
->park_mtx
);
427 DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
430 park
->park_flags
|= PARKFLAG_WAITERGONE
;
431 if (park
->park_flags
& PARKFLAG_DONE
) {
433 mutex_exit(&park
->park_mtx
);
436 * ok, we marked it as going away, but
437 * still need to do queue ops. take locks
440 * We don't want to release our reference
441 * if it's on replywait queue to avoid error
442 * to file server. putop() code will DTRT.
444 mutex_exit(&park
->park_mtx
);
445 mutex_enter(&pmp
->pmp_lock
);
446 mutex_enter(&park
->park_mtx
);
449 * Still on queue1? We can safely remove it
450 * without any consequences since the file
451 * server hasn't seen it. "else" we need to
452 * wait for the response and just ignore it
453 * to avoid signalling an incorrect error to
456 if (park
->park_flags
& PARKFLAG_ONQUEUE1
) {
457 TAILQ_REMOVE(&pmp
->pmp_msg_touser
,
459 puffs_msgpark_release(park
);
460 pmp
->pmp_msg_touser_count
--;
461 park
->park_flags
&= ~PARKFLAG_ONQUEUE1
;
463 mutex_exit(&park
->park_mtx
);
465 mutex_exit(&pmp
->pmp_lock
);
471 mutex_exit(&park
->park_mtx
);
475 mutex_enter(&pmp
->pmp_lock
);
476 puffs_mp_release(pmp
);
477 mutex_exit(&pmp
->pmp_lock
);
483 * XXX: this suuuucks. Hopefully I'll get rid of this lossage once
484 * the whole setback-nonsense gets fixed.
487 puffs_msg_wait2(struct puffs_mount
*pmp
, struct puffs_msgpark
*park
,
488 struct puffs_node
*pn1
, struct puffs_node
*pn2
)
490 struct puffs_req
*preq
;
493 rv
= puffs_msg_wait(pmp
, park
);
495 preq
= park
->park_preq
;
496 if (pn1
&& preq
->preq_setbacks
& PUFFS_SETBACK_INACT_N1
)
497 pn1
->pn_stat
|= PNODE_DOINACT
;
498 if (pn2
&& preq
->preq_setbacks
& PUFFS_SETBACK_INACT_N2
)
499 pn2
->pn_stat
|= PNODE_DOINACT
;
501 if (pn1
&& preq
->preq_setbacks
& PUFFS_SETBACK_NOREF_N1
)
502 pn1
->pn_stat
|= PNODE_NOREFS
;
503 if (pn2
&& preq
->preq_setbacks
& PUFFS_SETBACK_NOREF_N2
)
504 pn2
->pn_stat
|= PNODE_NOREFS
;
511 * XXX: lazy bum. please, for the love of foie gras, fix me.
512 * This should *NOT* depend on setfaf. Also "memcpy" could
513 * be done more nicely.
516 puffs_msg_sendresp(struct puffs_mount
*pmp
, struct puffs_req
*origpreq
, int rv
)
518 struct puffs_msgpark
*park
;
519 struct puffs_req
*preq
;
521 puffs_msgmem_alloc(sizeof(struct puffs_req
), &park
, (void *)&preq
, 1);
522 puffs_msg_setfaf(park
); /* XXXXXX: avoids reqid override */
524 memcpy(preq
, origpreq
, sizeof(struct puffs_req
));
526 preq
->preq_opclass
|= PUFFSOPFLAG_ISRESPONSE
;
528 puffs_msg_enqueue(pmp
, park
);
529 puffs_msgmem_release(park
);
533 * Get next request in the outgoing queue. "maxsize" controls the
534 * size the caller can accommodate and "nonblock" signals if this
535 * should block while waiting for input. Handles all locking internally.
538 puffs_msgif_getout(void *this, size_t maxsize
, int nonblock
,
539 uint8_t **data
, size_t *dlen
, void **parkptr
)
541 struct puffs_mount
*pmp
= this;
542 struct puffs_msgpark
*park
;
543 struct puffs_req
*preq
;
547 mutex_enter(&pmp
->pmp_lock
);
548 puffs_mp_reference(pmp
);
551 if (pmp
->pmp_status
!= PUFFSTAT_RUNNING
) {
556 /* need platinum yendorian express card? */
557 if (TAILQ_EMPTY(&pmp
->pmp_msg_touser
)) {
558 DPRINTF(("puffs_getout: no outgoing op, "));
560 DPRINTF(("returning EWOULDBLOCK\n"));
564 DPRINTF(("waiting ...\n"));
566 error
= cv_wait_sig(&pmp
->pmp_msg_waiter_cv
,
574 park
= TAILQ_FIRST(&pmp
->pmp_msg_touser
);
578 mutex_enter(&park
->park_mtx
);
579 puffs_msgpark_reference(park
);
581 DPRINTF(("puffs_getout: found park at %p, ", park
));
583 /* If it's a goner, don't process any furher */
584 if (park
->park_flags
& PARKFLAG_WAITERGONE
) {
585 DPRINTF(("waitergone!\n"));
586 puffs_msgpark_release(park
);
589 preq
= park
->park_preq
;
594 * XXX: this check is not valid for now, we don't know
595 * the size of the caller's input buffer. i.e. this
596 * will most likely go away
598 if (maxsize
< preq
->preq_frhdr
.pfr_len
) {
599 DPRINTF(("buffer too small\n"));
600 puffs_msgpark_release(park
);
606 DPRINTF(("returning\n"));
609 * Ok, we found what we came for. Release it from the
610 * outgoing queue but do not unlock. We will unlock
611 * only after we "releaseout" it to avoid complications:
612 * otherwise it is (theoretically) possible for userland
613 * to race us into "put" before we have a change to put
614 * this baby on the receiving queue.
616 TAILQ_REMOVE(&pmp
->pmp_msg_touser
, park
, park_entries
);
617 KASSERT(park
->park_flags
& PARKFLAG_ONQUEUE1
);
618 park
->park_flags
&= ~PARKFLAG_ONQUEUE1
;
619 mutex_exit(&park
->park_mtx
);
621 pmp
->pmp_msg_touser_count
--;
622 KASSERT(pmp
->pmp_msg_touser_count
>= 0);
626 puffs_mp_release(pmp
);
627 mutex_exit(&pmp
->pmp_lock
);
630 *data
= (uint8_t *)preq
;
631 preq
->preq_pth
.pth_framelen
= park
->park_copylen
;
632 *dlen
= preq
->preq_pth
.pth_framelen
;
640 * Release outgoing structure. Now, depending on the success of the
641 * outgoing send, it is either going onto the result waiting queue
642 * or the death chamber.
645 puffs_msgif_releaseout(void *this, void *parkptr
, int status
)
647 struct puffs_mount
*pmp
= this;
648 struct puffs_msgpark
*park
= parkptr
;
650 DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
652 mutex_enter(&pmp
->pmp_lock
);
653 mutex_enter(&park
->park_mtx
);
654 if (park
->park_flags
& PARKFLAG_WANTREPLY
) {
656 DPRINTF(("enqueue replywait\n"));
657 TAILQ_INSERT_TAIL(&pmp
->pmp_msg_replywait
, park
,
659 park
->park_flags
|= PARKFLAG_ONQUEUE2
;
661 DPRINTF(("error path!\n"));
662 park
->park_preq
->preq_rv
= status
;
663 park
->park_flags
|= PARKFLAG_DONE
;
664 cv_signal(&park
->park_cv
);
666 puffs_msgpark_release(park
);
668 DPRINTF(("release\n"));
669 puffs_msgpark_release1(park
, 2);
671 mutex_exit(&pmp
->pmp_lock
);
675 puffs_msgif_waitcount(void *this)
677 struct puffs_mount
*pmp
= this;
680 mutex_enter(&pmp
->pmp_lock
);
681 rv
= pmp
->pmp_msg_touser_count
;
682 mutex_exit(&pmp
->pmp_lock
);
688 * XXX: locking with this one?
691 puffsop_msg(void *this, struct puffs_req
*preq
)
693 struct puffs_mount
*pmp
= this;
694 struct putter_hdr
*pth
= &preq
->preq_pth
;
695 struct puffs_msgpark
*park
;
698 mutex_enter(&pmp
->pmp_lock
);
701 TAILQ_FOREACH(park
, &pmp
->pmp_msg_replywait
, park_entries
) {
702 if (park
->park_preq
->preq_id
== preq
->preq_id
)
706 DPRINTF(("puffsop_msg: no request: %" PRIu64
"\n",
708 mutex_exit(&pmp
->pmp_lock
);
709 return; /* XXX send error */
712 mutex_enter(&park
->park_mtx
);
713 puffs_msgpark_reference(park
);
714 if (pth
->pth_framelen
> park
->park_maxlen
) {
715 DPRINTF(("puffsop_msg: invalid buffer length: "
716 "%" PRIu64
" (req %" PRIu64
", \n", pth
->pth_framelen
,
718 park
->park_preq
->preq_rv
= EPROTO
;
719 cv_signal(&park
->park_cv
);
720 puffs_msgpark_release1(park
, 2);
721 mutex_exit(&pmp
->pmp_lock
);
722 return; /* XXX: error */
724 wgone
= park
->park_flags
& PARKFLAG_WAITERGONE
;
726 KASSERT(park
->park_flags
& PARKFLAG_ONQUEUE2
);
727 TAILQ_REMOVE(&pmp
->pmp_msg_replywait
, park
, park_entries
);
728 park
->park_flags
&= ~PARKFLAG_ONQUEUE2
;
729 mutex_exit(&pmp
->pmp_lock
);
732 DPRINTF(("puffsop_msg: bad service - waiter gone for "
735 if (park
->park_flags
& PARKFLAG_CALL
) {
736 DPRINTF(("puffsop_msg: call for %p, arg %p\n",
737 park
->park_preq
, park
->park_donearg
));
738 park
->park_done(pmp
, preq
, park
->park_donearg
);
740 /* XXX: yes, I know */
741 memcpy(park
->park_preq
, preq
, pth
->pth_framelen
);
746 DPRINTF(("puffs_putop: flagging done for "
748 cv_signal(&park
->park_cv
);
751 park
->park_flags
|= PARKFLAG_DONE
;
752 puffs_msgpark_release1(park
, 2);
756 puffsop_flush(struct puffs_mount
*pmp
, struct puffs_flush
*pf
)
762 KASSERT(pf
->pf_req
.preq_pth
.pth_framelen
== sizeof(struct puffs_flush
));
765 if (pf
->pf_op
== PUFFS_INVAL_NAMECACHE_ALL
) {
766 cache_purgevfs(PMPTOMP(pmp
));
772 * Get vnode, don't lock it. Namecache is protected by its own lock
773 * and we have a reference to protect against premature harvesting.
775 * The node we want here might be locked and the op is in
776 * userspace waiting for us to complete ==> deadlock. Another
777 * reason we need to eventually bump locking to userspace, as we
778 * will need to lock the node if we wish to do flushes.
780 rv
= puffs_cookie2vnode(pmp
, pf
->pf_cookie
, 0, 0, &vp
);
782 if (rv
== PUFFS_NOSUCHCOOKIE
)
789 /* not quite ready, yet */
790 case PUFFS_INVAL_NAMECACHE_NODE
:
791 struct componentname
*pf_cn
;
793 /* get comfortab^Wcomponentname */
794 pf_cn
= kmem_alloc(componentname
);
795 memset(pf_cn
, 0, sizeof(struct componentname
));
799 case PUFFS_INVAL_NAMECACHE_DIR
:
800 if (vp
->v_type
!= VDIR
) {
804 cache_purge1(vp
, NULL
, PURGE_CHILDREN
);
807 case PUFFS_INVAL_PAGECACHE_NODE_RANGE
:
810 case PUFFS_FLUSH_PAGECACHE_NODE_RANGE
:
814 if (pf
->pf_end
> vp
->v_size
|| vp
->v_type
!= VREG
) {
819 offlo
= trunc_page(pf
->pf_start
);
820 offhi
= round_page(pf
->pf_end
);
821 if (offhi
!= 0 && offlo
>= offhi
) {
826 mutex_enter(&vp
->v_uobj
.vmobjlock
);
827 rv
= VOP_PUTPAGES(vp
, offlo
, offhi
, flags
);
837 puffs_msg_sendresp(pmp
, &pf
->pf_req
, rv
);
841 puffs_msgif_dispatch(void *this, struct putter_hdr
*pth
)
843 struct puffs_mount
*pmp
= this;
844 struct puffs_req
*preq
= (struct puffs_req
*)pth
;
845 struct puffs_sopreq
*psopr
;
847 if (pth
->pth_framelen
< sizeof(struct puffs_req
)) {
848 puffs_msg_sendresp(pmp
, preq
, EINVAL
); /* E2SMALL */
852 switch (PUFFSOP_OPCLASS(preq
->preq_opclass
)) {
855 DPRINTF(("dispatch: vn/vfs message 0x%x\n", preq
->preq_optype
));
856 puffsop_msg(pmp
, preq
);
859 case PUFFSOP_FLUSH
: /* process in sop thread */
861 struct puffs_flush
*pf
;
863 DPRINTF(("dispatch: flush 0x%x\n", preq
->preq_optype
));
865 if (preq
->preq_pth
.pth_framelen
!= sizeof(struct puffs_flush
)) {
866 puffs_msg_sendresp(pmp
, preq
, EINVAL
); /* E2SMALL */
869 pf
= (struct puffs_flush
*)preq
;
871 psopr
= kmem_alloc(sizeof(*psopr
), KM_SLEEP
);
872 memcpy(&psopr
->psopr_pf
, pf
, sizeof(*pf
));
873 psopr
->psopr_sopreq
= PUFFS_SOPREQ_FLUSH
;
875 mutex_enter(&pmp
->pmp_sopmtx
);
876 TAILQ_INSERT_TAIL(&pmp
->pmp_sopreqs
, psopr
, psopr_entries
);
877 cv_signal(&pmp
->pmp_sopcv
);
878 mutex_exit(&pmp
->pmp_sopmtx
);
882 case PUFFSOP_UNMOUNT
: /* process in sop thread */
885 DPRINTF(("dispatch: unmount 0x%x\n", preq
->preq_optype
));
887 psopr
= kmem_alloc(sizeof(*psopr
), KM_SLEEP
);
888 psopr
->psopr_preq
= *preq
;
889 psopr
->psopr_sopreq
= PUFFS_SOPREQ_UNMOUNT
;
891 mutex_enter(&pmp
->pmp_sopmtx
);
892 TAILQ_INSERT_TAIL(&pmp
->pmp_sopreqs
, psopr
, psopr_entries
);
893 cv_signal(&pmp
->pmp_sopcv
);
894 mutex_exit(&pmp
->pmp_sopmtx
);
899 DPRINTF(("dispatch: invalid class 0x%x\n", preq
->preq_opclass
));
900 puffs_msg_sendresp(pmp
, preq
, EOPNOTSUPP
);
908 * Work loop for thread processing all ops from server which
909 * cannot safely be handled in caller context. This includes
910 * everything which might need a lock currently "held" by the file
911 * server, i.e. a long-term kernel lock which will be released only
912 * once the file server acknowledges a request
915 puffs_sop_thread(void *arg
)
917 struct puffs_mount
*pmp
= arg
;
918 struct mount
*mp
= PMPTOMP(pmp
);
919 struct puffs_sopreq
*psopr
;
921 bool unmountme
= false;
923 mutex_enter(&pmp
->pmp_sopmtx
);
924 for (keeprunning
= true; keeprunning
; ) {
925 while ((psopr
= TAILQ_FIRST(&pmp
->pmp_sopreqs
)) == NULL
)
926 cv_wait(&pmp
->pmp_sopcv
, &pmp
->pmp_sopmtx
);
927 TAILQ_REMOVE(&pmp
->pmp_sopreqs
, psopr
, psopr_entries
);
928 mutex_exit(&pmp
->pmp_sopmtx
);
930 switch (psopr
->psopr_sopreq
) {
931 case PUFFS_SOPREQSYS_EXIT
:
934 case PUFFS_SOPREQ_FLUSH
:
935 puffsop_flush(pmp
, &psopr
->psopr_pf
);
937 case PUFFS_SOPREQ_UNMOUNT
:
938 puffs_msg_sendresp(pmp
, &psopr
->psopr_preq
, 0);
944 * We know the mountpoint is still alive because
945 * the thread that is us (poetic?) is still alive.
947 atomic_inc_uint((unsigned int*)&mp
->mnt_refcnt
);
951 kmem_free(psopr
, sizeof(*psopr
));
952 mutex_enter(&pmp
->pmp_sopmtx
);
956 * Purge remaining ops. could send error, but ...
958 while ((psopr
= TAILQ_FIRST(&pmp
->pmp_sopreqs
)) != NULL
) {
959 TAILQ_REMOVE(&pmp
->pmp_sopreqs
, psopr
, psopr_entries
);
960 mutex_exit(&pmp
->pmp_sopmtx
);
961 kmem_free(psopr
, sizeof(*psopr
));
962 mutex_enter(&pmp
->pmp_sopmtx
);
965 pmp
->pmp_sopthrcount
--;
966 cv_broadcast(&pmp
->pmp_sopcv
);
967 mutex_exit(&pmp
->pmp_sopmtx
); /* not allowed to access fs after this */
970 * If unmount was requested, we can now safely do it here, since
971 * our context is dead from the point-of-view of puffs_unmount()
972 * and we are just another thread. dounmount() makes internally
973 * sure that VFS_UNMOUNT() isn't called reentrantly and that it
974 * is eventually completed.
977 (void)dounmount(mp
, MNT_FORCE
, curlwp
);
985 puffs_msgif_close(void *this)
987 struct puffs_mount
*pmp
= this;
988 struct mount
*mp
= PMPTOMP(pmp
);
990 mutex_enter(&pmp
->pmp_lock
);
991 puffs_mp_reference(pmp
);
994 * Free the waiting callers before proceeding any further.
995 * The syncer might be jogging around in this file system
996 * currently. If we allow it to go to the userspace of no
997 * return while trying to get the syncer lock, well ...
1002 * Make sure someone from puffs_unmount() isn't currently in
1003 * userspace. If we don't take this precautionary step,
1004 * they might notice that the mountpoint has disappeared
1005 * from under them once they return. Especially note that we
1006 * cannot simply test for an unmounter before calling
1007 * dounmount(), since it might be possible that that particular
1008 * invocation of unmount was called without MNT_FORCE. Here we
1009 * *must* make sure unmount succeeds. Also, restart is necessary
1010 * since pmp isn't locked. We might end up with PUTTER_DEAD after
1011 * restart and exit from there.
1013 if (pmp
->pmp_unmounting
) {
1014 cv_wait(&pmp
->pmp_unmounting_cv
, &pmp
->pmp_lock
);
1015 puffs_mp_release(pmp
);
1016 mutex_exit(&pmp
->pmp_lock
);
1017 DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
1022 /* Won't access pmp from here anymore */
1023 atomic_inc_uint((unsigned int*)&mp
->mnt_refcnt
);
1024 puffs_mp_release(pmp
);
1025 mutex_exit(&pmp
->pmp_lock
);
1027 /* Detach from VFS. */
1028 (void)dounmount(mp
, MNT_FORCE
, curlwp
);
1035 * We're dead, kaput, RIP, slightly more than merely pining for the
1036 * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
1037 * our maker, ceased to be, etcetc. YASD. It's a dead FS!
1039 * Caller must hold puffs mutex.
1042 puffs_userdead(struct puffs_mount
*pmp
)
1044 struct puffs_msgpark
*park
, *park_next
;
1047 * Mark filesystem status as dying so that operations don't
1048 * attempt to march to userspace any longer.
1050 pmp
->pmp_status
= PUFFSTAT_DYING
;
1052 /* signal waiters on REQUEST TO file server queue */
1053 for (park
= TAILQ_FIRST(&pmp
->pmp_msg_touser
); park
; park
= park_next
) {
1056 mutex_enter(&park
->park_mtx
);
1057 puffs_msgpark_reference(park
);
1058 park_next
= TAILQ_NEXT(park
, park_entries
);
1060 KASSERT(park
->park_flags
& PARKFLAG_ONQUEUE1
);
1061 TAILQ_REMOVE(&pmp
->pmp_msg_touser
, park
, park_entries
);
1062 park
->park_flags
&= ~PARKFLAG_ONQUEUE1
;
1063 pmp
->pmp_msg_touser_count
--;
1066 * Even though waiters on QUEUE1 are removed in touser()
1067 * in case of WAITERGONE, it is still possible for us to
1068 * get raced here due to having to retake locks in said
1069 * touser(). In the race case simply "ignore" the item
1070 * on the queue and move on to the next one.
1072 if (park
->park_flags
& PARKFLAG_WAITERGONE
) {
1073 KASSERT((park
->park_flags
& PARKFLAG_CALL
) == 0);
1074 KASSERT(park
->park_flags
& PARKFLAG_WANTREPLY
);
1075 puffs_msgpark_release(park
);
1078 opclass
= park
->park_preq
->preq_opclass
;
1079 park
->park_preq
->preq_rv
= ENXIO
;
1081 if (park
->park_flags
& PARKFLAG_CALL
) {
1082 park
->park_done(pmp
, park
->park_preq
,
1083 park
->park_donearg
);
1084 puffs_msgpark_release1(park
, 2);
1085 } else if ((park
->park_flags
& PARKFLAG_WANTREPLY
)==0) {
1086 puffs_msgpark_release1(park
, 2);
1088 park
->park_preq
->preq_rv
= ENXIO
;
1089 cv_signal(&park
->park_cv
);
1090 puffs_msgpark_release(park
);
1095 /* signal waiters on RESPONSE FROM file server queue */
1096 for (park
=TAILQ_FIRST(&pmp
->pmp_msg_replywait
); park
; park
=park_next
) {
1097 mutex_enter(&park
->park_mtx
);
1098 puffs_msgpark_reference(park
);
1099 park_next
= TAILQ_NEXT(park
, park_entries
);
1101 KASSERT(park
->park_flags
& PARKFLAG_ONQUEUE2
);
1102 KASSERT(park
->park_flags
& PARKFLAG_WANTREPLY
);
1104 TAILQ_REMOVE(&pmp
->pmp_msg_replywait
, park
, park_entries
);
1105 park
->park_flags
&= ~PARKFLAG_ONQUEUE2
;
1107 if (park
->park_flags
& PARKFLAG_WAITERGONE
) {
1108 KASSERT((park
->park_flags
& PARKFLAG_CALL
) == 0);
1109 puffs_msgpark_release(park
);
1111 park
->park_preq
->preq_rv
= ENXIO
;
1112 if (park
->park_flags
& PARKFLAG_CALL
) {
1113 park
->park_done(pmp
, park
->park_preq
,
1114 park
->park_donearg
);
1115 puffs_msgpark_release1(park
, 2);
1117 cv_signal(&park
->park_cv
);
1118 puffs_msgpark_release(park
);
1123 cv_broadcast(&pmp
->pmp_msg_waiter_cv
);