[BACK]Return to puffs_msgif.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / fs / puffs

Annotation of src/sys/fs/puffs/puffs_msgif.c, Revision 1.91

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

CVSweb <webmaster@jp.NetBSD.org>