/*
* Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
*
* Development of this software was supported by the
* Google Summer of Code program and the Ulla Tuominen Foundation.
* The Google SoC project was mentored by Bill Studenmund.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* While a request is going to userspace, park the caller within the
* kernel. This is the kernel counterpart of "struct puffs_req".
*/
struct puffs_msgpark {
struct puffs_req *park_preq; /* req followed by buf */
size_t park_copylen; /* userspace copylength */
size_t park_maxlen; /* max size in comeback */
static void
parkqdump(struct puffs_wq *q, int dumpall)
{
struct puffs_msgpark *park;
int total = 0;
TAILQ_FOREACH(park, q, park_entries) {
if (dumpall)
parkdump(park);
total++;
}
DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
}
#endif /* PUFFSDEBUG */
/*
* A word about locking in the park structures: the lock protects the
* fields of the *park* structure (not preq) and acts as an interlock
* in cv operations. The lock is always internal to this module and
* callers do not need to worry about it.
*/
int
puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
int cansleep)
{
struct puffs_msgpark *park;
void *m;
/*
* A word about reference counting of parks. A reference must be taken
* when accessing a park and additionally when it is on a queue. So
* when taking it off a queue and releasing the access reference, the
* reference count is generally decremented by 2.
*/
/*
* Some clients reuse a park, so reset some flags. We might
* want to provide a caller-side interface for this and add
* a few more invariant checks here, but this will do for now.
*/
park->park_flags &= ~(PARKFLAG_DONE | PARKFLAG_HASERROR);
KASSERT((park->park_flags & PARKFLAG_WAITERGONE) == 0);
preq = park->park_preq;
#if 1
/* check if we do compat adjustments */
if (pmp->pmp_docompat) {
MODULE_HOOK_CALL(puffs_out_50_hook, (preq, &creq, &delta),
enosys(), ret);
if (ret == 0) {
park->park_creq = park->park_preq;
park->park_creqlen = park->park_maxlen;
/* fill in caller information */
preq->preq_pid = l->l_proc->p_pid;
preq->preq_lid = l->l_lid;
/*
* To support cv_sig, yet another movie: check if there are signals
* pending and we are issuing a non-FAF. If so, return an error
* directly UNLESS we are issuing INACTIVE/RECLAIM. In that case,
* convert it to a FAF, fire off to the file server and return
* an error. Yes, this is bordering disgusting. Barfbags are on me.
*/
if (__predict_false((park->park_flags & PARKFLAG_WANTREPLY)
&& (park->park_flags & PARKFLAG_CALL) == 0
&& (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))) {
sigset_t ss;
/*
* see the comment about signals in puffs_msg_wait.
*/
sigpending1(l, &ss);
if (sigismember(&ss, SIGINT) ||
sigismember(&ss, SIGTERM) ||
sigismember(&ss, SIGKILL) ||
sigismember(&ss, SIGHUP) ||
sigismember(&ss, SIGQUIT)) {
park->park_flags |= PARKFLAG_HASERROR;
preq->preq_rv = EINTR;
if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
&& (preq->preq_optype == PUFFS_VN_INACTIVE
|| preq->preq_optype == PUFFS_VN_RECLAIM)) {
park->park_preq->preq_opclass |=
PUFFSOPFLAG_FAF;
park->park_flags &= ~PARKFLAG_WANTREPLY;
DPRINTF(("puffs_msg_enqueue: "
"converted to FAF %p\n", park));
} else {
return;
}
}
}
/*
* Note: we don't need to lock park since we have the only
* reference to it at this point.
*/
TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
park->park_flags |= PARKFLAG_ONQUEUE1;
pmp->pmp_msg_touser_count++;
park->park_refcount++;
/*
* block unimportant signals.
*
* The set of "important" signals here was chosen to be same as
* nfs interruptible mount.
*/
sigfillset(&ss);
sigdelset(&ss, SIGINT);
sigdelset(&ss, SIGTERM);
sigdelset(&ss, SIGKILL);
sigdelset(&ss, SIGHUP);
sigdelset(&ss, SIGQUIT);
mutex_enter(p->p_lock);
sigprocmask1(l, SIG_BLOCK, &ss, &oss);
mutex_exit(p->p_lock);
mutex_enter(&park->park_mtx);
/* did the response beat us to the wait? */
if (__predict_false((park->park_flags & PARKFLAG_DONE)
|| (park->park_flags & PARKFLAG_HASERROR))) {
rv = park->park_preq->preq_rv;
mutex_exit(&park->park_mtx);
goto skipwait;
}
error = cv_wait_sig(&park->park_cv, &park->park_mtx);
DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
park, error));
if (error) {
park->park_flags |= PARKFLAG_WAITERGONE;
if (park->park_flags & PARKFLAG_DONE) {
rv = preq->preq_rv;
mutex_exit(&park->park_mtx);
} else {
/*
* ok, we marked it as going away, but
* still need to do queue ops. take locks
* in correct order.
*
* We don't want to release our reference
* if it's on replywait queue to avoid error
* to file server. putop() code will DTRT.
*/
mutex_exit(&park->park_mtx);
mutex_enter(&pmp->pmp_lock);
mutex_enter(&park->park_mtx);
/*
* Still on queue1? We can safely remove it
* without any consequences since the file
* server hasn't seen it. "else" we need to
* wait for the response and just ignore it
* to avoid signalling an incorrect error to
* the file server.
*/
if (park->park_flags & PARKFLAG_ONQUEUE1) {
TAILQ_REMOVE(&pmp->pmp_msg_touser,
park, park_entries);
puffs_msgpark_release(park);
pmp->pmp_msg_touser_count--;
park->park_flags &= ~PARKFLAG_ONQUEUE1;
} else {
mutex_exit(&park->park_mtx);
}
mutex_exit(&pmp->pmp_lock);
/*
* XXX: this suuuucks. Hopefully I'll get rid of this lossage once
* the whole setback-nonsense gets fixed.
*/
int
puffs_msg_wait2(struct puffs_mount *pmp, struct puffs_msgpark *park,
struct puffs_node *pn1, struct puffs_node *pn2)
{
struct puffs_req *preq;
int rv;
if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1)
pn1->pn_stat |= PNODE_NOREFS;
if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2)
pn2->pn_stat |= PNODE_NOREFS;
return rv;
}
/*
* XXX: lazy bum. please, for the love of foie gras, fix me.
* This should *NOT* depend on setfaf. Also "memcpy" could
* be done more nicely.
*/
void
puffs_msg_sendresp(struct puffs_mount *pmp, struct puffs_req *origpreq, int rv)
{
struct puffs_msgpark *park;
struct puffs_req *preq;
/*
* Get next request in the outgoing queue. "maxsize" controls the
* size the caller can accommodate and "nonblock" signals if this
* should block while waiting for input. Handles all locking internally.
*/
int
puffs_msgif_getout(void *ctx, size_t maxsize, int nonblock,
uint8_t **data, size_t *dlen, void **parkptr)
{
struct puffs_mount *pmp = ctx;
struct puffs_msgpark *park = NULL;
struct puffs_req *preq = NULL;
int error;
DPRINTF(("puffs_getout: found park at %p, ", park));
/* If it's a goner, don't process any further */
if (park->park_flags & PARKFLAG_WAITERGONE) {
DPRINTF(("waitergone!\n"));
puffs_msgpark_release(park);
continue;
}
preq = park->park_preq;
#if 0
/* check size */
/*
* XXX: this check is not valid for now, we don't know
* the size of the caller's input buffer. i.e. this
* will most likely go away
*/
if (maxsize < preq->preq_frhdr.pfr_len) {
DPRINTF(("buffer too small\n"));
puffs_msgpark_release(park);
error = E2BIG;
break;
}
#endif
DPRINTF(("returning\n"));
/*
* Ok, we found what we came for. Release it from the
* outgoing queue but do not unlock. We will unlock
* only after we "releaseout" it to avoid complications:
* otherwise it is (theoretically) possible for userland
* to race us into "put" before we have a change to put
* this baby on the receiving queue.
*/
TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
park->park_flags &= ~PARKFLAG_ONQUEUE1;
mutex_exit(&park->park_mtx);
/*
* Release outgoing structure. Now, depending on the success of the
* outgoing send, it is either going onto the result waiting queue
* or the death chamber.
*/
void
puffs_msgif_releaseout(void *ctx, void *parkptr, int status)
{
struct puffs_mount *pmp = ctx;
struct puffs_msgpark *park = parkptr;
/*
* Node expiry. We come here after an inactive on an unexpired node.
* The expiry has been queued and is done in sop thread.
*/
static void
puffsop_expire(struct puffs_mount *pmp, puffs_cookie_t cookie)
{
struct vnode *vp;
KASSERT(PUFFS_USE_FS_TTL(pmp));
/*
* If it still exists and has no reference,
* vrele should cause it to be reclaimed.
* Otherwise, we have nothing to do.
*/
if (puffs_cookie2vnode(pmp, cookie, &vp) == 0) {
VPTOPP(vp)->pn_stat &= ~PNODE_SOPEXP;
vrele(vp);
}
/*
* Get vnode, don't lock it. Namecache is protected by its own lock
* and we have a reference to protect against premature harvesting.
*
* The node we want here might be locked and the op is in
* userspace waiting for us to complete ==> deadlock. Another
* reason we need to eventually bump locking to userspace, as we
* will need to lock the node if we wish to do flushes.
*/
rv = puffs_cookie2vnode(pmp, pf->pf_cookie, &vp);
if (rv) {
if (rv == PUFFS_NOSUCHCOOKIE)
rv = ENOENT;
goto out;
}
switch (pf->pf_op) {
#if 0
/* not quite ready, yet */
case PUFFS_INVAL_NAMECACHE_NODE:
struct componentname *pf_cn;
char *name;
/* get comfortab^Wcomponentname */
pf_cn = kmem_alloc(componentname);
memset(pf_cn, 0, sizeof(struct componentname));
break;
#endif
case PUFFS_INVAL_NAMECACHE_DIR:
if (vp->v_type != VDIR) {
rv = EINVAL;
break;
}
cache_purge1(vp, NULL, 0, PURGE_CHILDREN);
break;
case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
flags = PGO_FREE;
/*FALLTHROUGH*/
case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
if (flags == 0)
flags = PGO_CLEANIT;
/*
* Work loop for thread processing all ops from server which
* cannot safely be handled in caller context. This includes
* everything which might need a lock currently "held" by the file
* server, i.e. a long-term kernel lock which will be released only
* once the file server acknowledges a request
*/
#define TIMED_OUT(expire) \
((int)((unsigned int)getticks() - (unsigned int)expire) > 0)
void
puffs_sop_thread(void *arg)
{
struct puffs_mount *pmp = arg;
struct mount *mp = PMPTOMP(pmp);
struct puffs_sopreq *psopr;
bool keeprunning;
bool unmountme = false;
int timeo;
mutex_enter(&pmp->pmp_sopmtx);
for (keeprunning = true; keeprunning; ) {
/*
* We have a fast queue for flush and umount, and a node
* queue for delayes node reclaims. Requests on node queue * are not honoured before clock reaches psopr_at. This
* code assumes that requests are ordered by psopr_at.
*/
do {
psopr = TAILQ_FIRST(&pmp->pmp_sopfastreqs);
if (psopr != NULL) {
TAILQ_REMOVE(&pmp->pmp_sopfastreqs,
psopr, psopr_entries);
break;
}
pmp->pmp_sopthrcount--;
cv_broadcast(&pmp->pmp_sopcv);
mutex_exit(&pmp->pmp_sopmtx); /* not allowed to access fs after this */
/*
* If unmount was requested, we can now safely do it here, since
* our context is dead from the point-of-view of puffs_unmount()
* and we are just another thread. dounmount() makes internally
* sure that VFS_UNMOUNT() isn't called reentrantly and that it
* is eventually completed.
*/
if (unmountme) {
(void)dounmount(mp, MNT_FORCE, curlwp);
vfs_rele(mp);
}
kthread_exit(0);
}
int
puffs_msgif_close(void *ctx)
{
struct puffs_mount *pmp = ctx;
struct mount *mp = PMPTOMP(pmp);
/*
* Free the waiting callers before proceeding any further.
* The syncer might be jogging around in this file system
* currently. If we allow it to go to the userspace of no
* return while trying to get the syncer lock, well ...
*/
puffs_userdead(pmp);
/*
* Make sure someone from puffs_unmount() isn't currently in
* userspace. If we don't take this precautionary step,
* they might notice that the mountpoint has disappeared
* from under them once they return. Especially note that we
* cannot simply test for an unmounter before calling
* dounmount(), since it might be possible that that particular
* invocation of unmount was called without MNT_FORCE. Here we
* *must* make sure unmount succeeds. Also, restart is necessary
* since pmp isn't locked. We might end up with PUTTER_DEAD after
* restart and exit from there.
*/
if (pmp->pmp_unmounting) {
cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
puffs_mp_release(pmp);
mutex_exit(&pmp->pmp_lock);
DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
"restart\n", pmp));
return ERESTART;
}
/* Won't access pmp from here anymore */
vfs_ref(mp);
puffs_mp_release(pmp);
mutex_exit(&pmp->pmp_lock);
/* Detach from VFS. */
(void)dounmount(mp, MNT_FORCE, curlwp);
vfs_rele(mp);
return 0;
}
/*
* We're dead, kaput, RIP, slightly more than merely pining for the
* fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
* our maker, ceased to be, etcetc. YASD. It's a dead FS!
*
* Caller must hold puffs mutex.
*/
void
puffs_userdead(struct puffs_mount *pmp)
{
struct puffs_msgpark *park, *park_next;
/*
* Mark filesystem status as dying so that operations don't
* attempt to march to userspace any longer.
*/
pmp->pmp_status = PUFFSTAT_DYING;
/* signal waiters on REQUEST TO file server queue */
for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
park->park_flags &= ~PARKFLAG_ONQUEUE1;
pmp->pmp_msg_touser_count--;
/*
* Even though waiters on QUEUE1 are removed in touser()
* in case of WAITERGONE, it is still possible for us to
* get raced here due to having to retake locks in said
* touser(). In the race case simply "ignore" the item
* on the queue and move on to the next one.
*/
if (park->park_flags & PARKFLAG_WAITERGONE) {
KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
puffs_msgpark_release(park);
/* signal waiters on RESPONSE FROM file server queue */
for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
mutex_enter(&park->park_mtx);
puffs_msgpark_reference(park);
park_next = TAILQ_NEXT(park, park_entries);