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

Annotation of src/sys/fs/puffs/puffs_vfsops.c, Revision 1.117

1.117   ! martin      1: /*     $NetBSD: puffs_vfsops.c,v 1.116 2015/02/15 20:21:29 manu Exp $  */
1.1       pooka       2:
                      3: /*
                      4:  * Copyright (c) 2005, 2006  Antti Kantee.  All Rights Reserved.
                      5:  *
                      6:  * Development of this software was supported by the
                      7:  * Google Summer of Code program and the Ulla Tuominen Foundation.
                      8:  * The Google SoC project was mentored by Bill Studenmund.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
                     20:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     21:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
                     22:  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
                     25:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #include <sys/cdefs.h>
1.117   ! martin     33: __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.116 2015/02/15 20:21:29 manu Exp $");
1.1       pooka      34:
                     35: #include <sys/param.h>
1.102     manu       36: #include <sys/kernel.h>
1.1       pooka      37: #include <sys/mount.h>
                     38: #include <sys/extattr.h>
                     39: #include <sys/queue.h>
                     40: #include <sys/vnode.h>
                     41: #include <sys/dirent.h>
                     42: #include <sys/kauth.h>
1.47      ad         43: #include <sys/proc.h>
1.80      rumble     44: #include <sys/module.h>
1.84      pooka      45: #include <sys/kthread.h>
1.1       pooka      46:
1.99      manu       47: #include <uvm/uvm.h>
                     48:
1.68      pooka      49: #include <dev/putter/putter_sys.h>
1.1       pooka      50:
1.78      dholland   51: #include <miscfs/genfs/genfs.h>
                     52:
1.1       pooka      53: #include <fs/puffs/puffs_msgif.h>
                     54: #include <fs/puffs/puffs_sys.h>
                     55:
1.66      pooka      56: #include <lib/libkern/libkern.h>
                     57:
1.38      pooka      58: #include <nfs/nfsproto.h> /* for fh sizes */
                     59:
1.81      jmcneill   60: MODULE(MODULE_CLASS_VFS, puffs, "putter");
1.80      rumble     61:
1.73      pooka      62: VFS_PROTOS(puffs_vfsop);
1.1       pooka      63:
1.66      pooka      64: static struct putter_ops puffs_putter = {
                     65:        .pop_getout     = puffs_msgif_getout,
                     66:        .pop_releaseout = puffs_msgif_releaseout,
                     67:        .pop_waitcount  = puffs_msgif_waitcount,
                     68:        .pop_dispatch   = puffs_msgif_dispatch,
                     69:        .pop_close      = puffs_msgif_close,
                     70: };
                     71:
1.114     hannken    72: static const struct genfs_ops puffs_genfsops = {
                     73:         .gop_size = puffs_gop_size,
                     74:        .gop_write = genfs_gop_write,
                     75:        .gop_markupdate = puffs_gop_markupdate,
                     76: #if 0
                     77:        .gop_alloc, should ask userspace
                     78: #endif
                     79: };
                     80:
1.92      pooka      81: /*
                     82:  * Try to ensure data structures used by the puffs protocol
                     83:  * do not unexpectedly change.
                     84:  */
1.107     pooka      85: #if defined(__i386__) && defined(__ELF__)
1.92      pooka      86: CTASSERT(sizeof(struct puffs_kargs) == 3928);
                     87: CTASSERT(sizeof(struct vattr) == 136);
                     88: CTASSERT(sizeof(struct puffs_req) == 44);
                     89: #endif
                     90:
1.1       pooka      91: int
1.73      pooka      92: puffs_vfsop_mount(struct mount *mp, const char *path, void *data,
                     93:        size_t *data_len)
1.1       pooka      94: {
1.22      pooka      95:        struct puffs_mount *pmp = NULL;
1.34      pooka      96:        struct puffs_kargs *args;
1.51      pooka      97:        char fstype[_VFS_NAMELEN];
                     98:        char *p;
1.22      pooka      99:        int error = 0, i;
1.71      pooka     100:        pid_t mntpid = curlwp->l_proc->p_pid;
1.1       pooka     101:
1.110     maxv      102:        if (data == NULL)
                    103:                return EINVAL;
1.48      dsl       104:        if (*data_len < sizeof *args)
                    105:                return EINVAL;
                    106:
1.1       pooka     107:        if (mp->mnt_flag & MNT_GETARGS) {
                    108:                pmp = MPTOPUFFSMP(mp);
1.48      dsl       109:                *(struct puffs_kargs *)data = pmp->pmp_args;
                    110:                *data_len = sizeof *args;
                    111:                return 0;
1.1       pooka     112:        }
                    113:
                    114:        /* update is not supported currently */
                    115:        if (mp->mnt_flag & MNT_UPDATE)
                    116:                return EOPNOTSUPP;
                    117:
1.70      pooka     118:        args = (struct puffs_kargs *)data;
1.17      pooka     119:
1.89      pooka     120:        if (args->pa_vers != PUFFSVERSION) {
1.64      pooka     121:                printf("puffs_mount: development version mismatch: "
1.89      pooka     122:                    "kernel %d, lib %d\n", PUFFSVERSION, args->pa_vers);
1.17      pooka     123:                error = EINVAL;
                    124:                goto out;
                    125:        }
1.1       pooka     126:
1.54      pooka     127:        if ((args->pa_flags & ~PUFFS_KFLAG_MASK) != 0) {
                    128:                printf("puffs_mount: invalid KFLAGs 0x%x\n", args->pa_flags);
                    129:                error = EINVAL;
                    130:                goto out;
                    131:        }
                    132:        if ((args->pa_fhflags & ~PUFFS_FHFLAG_MASK) != 0) {
                    133:                printf("puffs_mount: invalid FHFLAGs 0x%x\n", args->pa_fhflags);
                    134:                error = EINVAL;
                    135:                goto out;
                    136:        }
                    137:
1.91      pooka     138:        for (i = 0; i < __arraycount(args->pa_spare); i++) {
                    139:                if (args->pa_spare[i] != 0) {
                    140:                        printf("puffs_mount: pa_spare[%d] = 0x%x\n",
                    141:                            i, args->pa_spare[i]);
                    142:                        error = EINVAL;
                    143:                        goto out;
                    144:                }
                    145:        }
                    146:
1.54      pooka     147:        /* use dummy value for passthrough */
                    148:        if (args->pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
                    149:                args->pa_fhsize = sizeof(struct fid);
1.8       pooka     150:
1.38      pooka     151:        /* sanitize file handle length */
                    152:        if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) {
                    153:                printf("puffs_mount: handle size %zu too large\n",
                    154:                    args->pa_fhsize);
                    155:                error = EINVAL;
                    156:                goto out;
                    157:        }
                    158:        /* sanity check file handle max sizes */
                    159:        if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) {
                    160:                size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize);
                    161:
                    162:                if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) {
                    163:                        if (NFSX_FHTOOBIG_P(kfhsize, 0)) {
                    164:                                printf("puffs_mount: fhsize larger than "
                    165:                                    "NFSv2 max %d\n",
                    166:                                    PUFFS_FROMFHSIZE(NFSX_V2FH));
                    167:                                error = EINVAL;
                    168:                                goto out;
                    169:                        }
                    170:                }
                    171:
                    172:                if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) {
                    173:                        if (NFSX_FHTOOBIG_P(kfhsize, 1)) {
                    174:                                printf("puffs_mount: fhsize larger than "
                    175:                                    "NFSv3 max %d\n",
                    176:                                    PUFFS_FROMFHSIZE(NFSX_V3FHMAX));
                    177:                                error = EINVAL;
                    178:                                goto out;
                    179:                        }
                    180:                }
                    181:        }
                    182:
1.51      pooka     183:        /* don't allow non-printing characters (like my sweet umlauts.. snif) */
                    184:        args->pa_typename[sizeof(args->pa_typename)-1] = '\0';
                    185:        for (p = args->pa_typename; *p; p++)
                    186:                if (*p < ' ' || *p > '~')
                    187:                        *p = '.';
                    188:
                    189:        args->pa_mntfromname[sizeof(args->pa_mntfromname)-1] = '\0';
                    190:        for (p = args->pa_mntfromname; *p; p++)
                    191:                if (*p < ' ' || *p > '~')
                    192:                        *p = '.';
                    193:
1.1       pooka     194:        /* build real name */
1.51      pooka     195:        (void)strlcpy(fstype, PUFFS_TYPEPREFIX, sizeof(fstype));
                    196:        (void)strlcat(fstype, args->pa_typename, sizeof(fstype));
1.1       pooka     197:
                    198:        /* inform user server if it got the max request size it wanted */
1.63      pooka     199:        if (args->pa_maxmsglen == 0 || args->pa_maxmsglen > PUFFS_MSG_MAXSIZE)
                    200:                args->pa_maxmsglen = PUFFS_MSG_MAXSIZE;
                    201:        else if (args->pa_maxmsglen < 2*PUFFS_MSGSTRUCT_MAX)
                    202:                args->pa_maxmsglen = 2*PUFFS_MSGSTRUCT_MAX;
                    203:
1.51      pooka     204:        (void)strlcpy(args->pa_typename, fstype, sizeof(args->pa_typename));
1.1       pooka     205:
1.51      pooka     206:        error = set_statvfs_info(path, UIO_USERSPACE, args->pa_mntfromname,
1.71      pooka     207:            UIO_SYSSPACE, fstype, mp, curlwp);
1.1       pooka     208:        if (error)
1.17      pooka     209:                goto out;
1.10      pooka     210:        mp->mnt_stat.f_iosize = DEV_BSIZE;
1.94      pooka     211:        mp->mnt_stat.f_namemax = args->pa_svfsb.f_namemax;
1.1       pooka     212:
1.42      pooka     213:        /*
                    214:         * We can't handle the VFS_STATVFS() mount_domount() does
                    215:         * after VFS_MOUNT() because we'd deadlock, so handle it
                    216:         * here already.
                    217:         */
                    218:        copy_statvfs_info(&args->pa_svfsb, mp);
                    219:        (void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat));
                    220:
1.99      manu      221:        KASSERT(curlwp != uvm.pagedaemon_lwp);
1.70      pooka     222:        pmp = kmem_zalloc(sizeof(struct puffs_mount), KM_SLEEP);
1.1       pooka     223:
1.6       pooka     224:        mp->mnt_fs_bshift = DEV_BSHIFT;
                    225:        mp->mnt_dev_bshift = DEV_BSHIFT;
                    226:        mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
1.1       pooka     227:        mp->mnt_data = pmp;
1.6       pooka     228:
1.87      pooka     229: #if 0
                    230:        /*
                    231:         * XXX: puffs code is MPSAFE.  However, VFS really isn't.
                    232:         * Currently, there is nothing which protects an inode from
                    233:         * reclaim while there are threads inside the file system.
                    234:         * This means that in the event of a server crash, an MPSAFE
                    235:         * mount is likely to end up accessing invalid memory.  For the
                    236:         * non-mpsafe case, the kernel lock, general structure of
                    237:         * puffs and pmp_refcount protect the threads during escape.
                    238:         *
                    239:         * Fixing this will require:
                    240:         *  a) fixing vfs
                    241:         * OR
                    242:         *  b) adding a small sleep to puffs_msgif_close() between
                    243:         *     userdead() and dounmount().
                    244:         *     (well, this isn't really a fix, but would solve
                    245:         *     99.999% of the race conditions).
                    246:         *
                    247:         * Also, in the event of "b", unmount -f should be used,
                    248:         * like with any other file system, sparingly and only when
                    249:         * it is "known" to be safe.
                    250:         */
                    251:        mp->mnt_iflags |= IMNT_MPSAFE;
                    252: #endif
                    253:
1.1       pooka     254:        pmp->pmp_status = PUFFSTAT_MOUNTING;
                    255:        pmp->pmp_mp = mp;
1.63      pooka     256:        pmp->pmp_msg_maxsize = args->pa_maxmsglen;
1.17      pooka     257:        pmp->pmp_args = *args;
1.1       pooka     258:
                    259:        /*
                    260:         * Inform the fileops processing code that we have a mountpoint.
                    261:         * If it doesn't know about anyone with our pid/fd having the
                    262:         * device open, punt
                    263:         */
1.66      pooka     264:        if ((pmp->pmp_pi
                    265:            = putter_attach(mntpid, args->pa_fd, pmp, &puffs_putter)) == NULL) {
1.17      pooka     266:                error = ENOENT;
                    267:                goto out;
1.1       pooka     268:        }
                    269:
1.42      pooka     270:        /* XXX: check parameters */
                    271:        pmp->pmp_root_cookie = args->pa_root_cookie;
                    272:        pmp->pmp_root_vtype = args->pa_root_vtype;
                    273:        pmp->pmp_root_vsize = args->pa_root_vsize;
                    274:        pmp->pmp_root_rdev = args->pa_root_rdev;
1.93      pooka     275:        pmp->pmp_docompat = args->pa_time32;
1.42      pooka     276:
1.31      pooka     277:        mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
1.84      pooka     278:        mutex_init(&pmp->pmp_sopmtx, MUTEX_DEFAULT, IPL_NONE);
1.63      pooka     279:        cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
1.41      pooka     280:        cv_init(&pmp->pmp_refcount_cv, "puffsref");
1.31      pooka     281:        cv_init(&pmp->pmp_unmounting_cv, "puffsum");
1.84      pooka     282:        cv_init(&pmp->pmp_sopcv, "puffsop");
1.63      pooka     283:        TAILQ_INIT(&pmp->pmp_msg_touser);
                    284:        TAILQ_INIT(&pmp->pmp_msg_replywait);
1.102     manu      285:        TAILQ_INIT(&pmp->pmp_sopfastreqs);
1.105     manu      286:        TAILQ_INIT(&pmp->pmp_sopnodereqs);
1.84      pooka     287:
                    288:        if ((error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
                    289:            puffs_sop_thread, pmp, NULL, "puffsop")) != 0)
                    290:                goto out;
                    291:        pmp->pmp_sopthrcount = 1;
1.1       pooka     292:
                    293:        DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
                    294:            mp, MPTOPUFFSMP(mp)));
                    295:
                    296:        vfs_getnewfsid(mp);
                    297:
1.17      pooka     298:  out:
1.84      pooka     299:        if (error && pmp && pmp->pmp_pi)
                    300:                putter_detach(pmp->pmp_pi);
1.22      pooka     301:        if (error && pmp)
1.70      pooka     302:                kmem_free(pmp, sizeof(struct puffs_mount));
1.17      pooka     303:        return error;
1.1       pooka     304: }
                    305:
                    306: int
1.73      pooka     307: puffs_vfsop_start(struct mount *mp, int flags)
1.1       pooka     308: {
1.42      pooka     309:        struct puffs_mount *pmp = MPTOPUFFSMP(mp);
1.1       pooka     310:
1.42      pooka     311:        KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
1.1       pooka     312:        pmp->pmp_status = PUFFSTAT_RUNNING;
                    313:
                    314:        return 0;
                    315: }
                    316:
                    317: int
1.73      pooka     318: puffs_vfsop_unmount(struct mount *mp, int mntflags)
1.1       pooka     319: {
1.63      pooka     320:        PUFFS_MSG_VARS(vfs, unmount);
1.1       pooka     321:        struct puffs_mount *pmp;
                    322:        int error, force;
                    323:
                    324:        error = 0;
                    325:        force = mntflags & MNT_FORCE;
                    326:        pmp = MPTOPUFFSMP(mp);
                    327:
                    328:        DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
                    329:            "status 0x%x\n", pmp->pmp_status));
                    330:
                    331:        /*
                    332:         * flush all the vnodes.  VOP_RECLAIM() takes care that the
                    333:         * root vnode does not get flushed until unmount.  The
                    334:         * userspace root node cookie is stored in the mount
                    335:         * structure, so we can always re-instantiate a root vnode,
                    336:         * should userspace unmount decide it doesn't want to
                    337:         * cooperate.
                    338:         */
                    339:        error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
                    340:        if (error)
                    341:                goto out;
                    342:
                    343:        /*
                    344:         * If we are not DYING, we should ask userspace's opinion
                    345:         * about the situation
                    346:         */
1.31      pooka     347:        mutex_enter(&pmp->pmp_lock);
1.1       pooka     348:        if (pmp->pmp_status != PUFFSTAT_DYING) {
1.16      pooka     349:                pmp->pmp_unmounting = 1;
1.31      pooka     350:                mutex_exit(&pmp->pmp_lock);
1.16      pooka     351:
1.63      pooka     352:                PUFFS_MSG_ALLOC(vfs, unmount);
1.69      pooka     353:                puffs_msg_setinfo(park_unmount,
                    354:                    PUFFSOP_VFS, PUFFS_VFS_UNMOUNT, NULL);
1.63      pooka     355:                unmount_msg->pvfsr_flags = mntflags;
                    356:
1.69      pooka     357:                PUFFS_MSG_ENQUEUEWAIT(pmp, park_unmount, error);
1.63      pooka     358:                PUFFS_MSG_RELEASE(unmount);
1.16      pooka     359:
1.60      pooka     360:                error = checkerr(pmp, error, __func__);
1.16      pooka     361:                DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
                    362:
1.31      pooka     363:                mutex_enter(&pmp->pmp_lock);
1.16      pooka     364:                pmp->pmp_unmounting = 0;
1.31      pooka     365:                cv_broadcast(&pmp->pmp_unmounting_cv);
1.1       pooka     366:        }
                    367:
                    368:        /*
                    369:         * if userspace cooperated or we really need to die,
                    370:         * screw what userland thinks and just die.
                    371:         */
                    372:        if (error == 0 || force) {
1.84      pooka     373:                struct puffs_sopreq *psopr;
                    374:
1.26      pooka     375:                /* tell waiters & other resources to go unwait themselves */
                    376:                puffs_userdead(pmp);
1.66      pooka     377:                putter_detach(pmp->pmp_pi);
1.26      pooka     378:
                    379:                /*
1.41      pooka     380:                 * Wait until there are no more users for the mount resource.
                    381:                 * Notice that this is hooked against transport_close
                    382:                 * and return from touser.  In an ideal world, it would
                    383:                 * be hooked against final return from all operations.
                    384:                 * But currently it works well enough, since nobody
                    385:                 * does weird blocking voodoo after return from touser().
1.26      pooka     386:                 */
1.41      pooka     387:                while (pmp->pmp_refcount != 0)
                    388:                        cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
1.31      pooka     389:                mutex_exit(&pmp->pmp_lock);
1.26      pooka     390:
1.84      pooka     391:                /*
                    392:                 * Release kernel thread now that there is nothing
                    393:                 * it would be wanting to lock.
                    394:                 */
1.99      manu      395:                KASSERT(curlwp != uvm.pagedaemon_lwp);
1.84      pooka     396:                psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
1.85      pooka     397:                psopr->psopr_sopreq = PUFFS_SOPREQSYS_EXIT;
1.84      pooka     398:                mutex_enter(&pmp->pmp_sopmtx);
1.86      pooka     399:                if (pmp->pmp_sopthrcount == 0) {
                    400:                        mutex_exit(&pmp->pmp_sopmtx);
                    401:                        kmem_free(psopr, sizeof(*psopr));
                    402:                        mutex_enter(&pmp->pmp_sopmtx);
                    403:                        KASSERT(pmp->pmp_sopthrcount == 0);
                    404:                } else {
1.102     manu      405:                        TAILQ_INSERT_TAIL(&pmp->pmp_sopfastreqs,
1.86      pooka     406:                            psopr, psopr_entries);
                    407:                        cv_signal(&pmp->pmp_sopcv);
                    408:                }
1.84      pooka     409:                while (pmp->pmp_sopthrcount > 0)
                    410:                        cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
                    411:                mutex_exit(&pmp->pmp_sopmtx);
                    412:
1.26      pooka     413:                /* free resources now that we hopefully have no waiters left */
1.41      pooka     414:                cv_destroy(&pmp->pmp_unmounting_cv);
                    415:                cv_destroy(&pmp->pmp_refcount_cv);
1.63      pooka     416:                cv_destroy(&pmp->pmp_msg_waiter_cv);
1.84      pooka     417:                cv_destroy(&pmp->pmp_sopcv);
1.31      pooka     418:                mutex_destroy(&pmp->pmp_lock);
1.84      pooka     419:                mutex_destroy(&pmp->pmp_sopmtx);
1.31      pooka     420:
1.70      pooka     421:                kmem_free(pmp, sizeof(struct puffs_mount));
1.1       pooka     422:                error = 0;
1.16      pooka     423:        } else {
1.31      pooka     424:                mutex_exit(&pmp->pmp_lock);
1.1       pooka     425:        }
                    426:
                    427:  out:
1.2       pooka     428:        DPRINTF(("puffs_unmount: return %d\n", error));
1.1       pooka     429:        return error;
                    430: }
                    431:
                    432: /*
                    433:  * This doesn't need to travel to userspace
                    434:  */
                    435: int
1.73      pooka     436: puffs_vfsop_root(struct mount *mp, struct vnode **vpp)
1.1       pooka     437: {
1.45      pooka     438:        struct puffs_mount *pmp = MPTOPUFFSMP(mp);
1.59      pooka     439:        int rv;
1.1       pooka     440:
1.114     hannken   441:        rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, vpp);
1.59      pooka     442:        KASSERT(rv != PUFFS_NOSUCHCOOKIE);
1.114     hannken   443:        if (rv != 0)
                    444:                return rv;
                    445:        rv = vn_lock(*vpp, LK_EXCLUSIVE);
                    446:        if (rv != 0) {
                    447:                vrele(*vpp);
                    448:                *vpp = NULL;
                    449:                return rv;
                    450:        }
                    451:        return 0;
1.1       pooka     452: }
                    453:
                    454: int
1.73      pooka     455: puffs_vfsop_statvfs(struct mount *mp, struct statvfs *sbp)
1.1       pooka     456: {
1.63      pooka     457:        PUFFS_MSG_VARS(vfs, statvfs);
1.1       pooka     458:        struct puffs_mount *pmp;
                    459:        int error = 0;
                    460:
                    461:        pmp = MPTOPUFFSMP(mp);
                    462:
                    463:        /*
                    464:         * If we are mounting, it means that the userspace counterpart
                    465:         * is calling mount(2), but mount(2) also calls statvfs.  So
                    466:         * requesting statvfs from userspace would mean a deadlock.
                    467:         * Compensate.
                    468:         */
1.87      pooka     469:        if (__predict_false(pmp->pmp_status == PUFFSTAT_MOUNTING))
1.1       pooka     470:                return EINPROGRESS;
                    471:
1.63      pooka     472:        PUFFS_MSG_ALLOC(vfs, statvfs);
1.69      pooka     473:        puffs_msg_setinfo(park_statvfs, PUFFSOP_VFS, PUFFS_VFS_STATVFS, NULL);
1.1       pooka     474:
1.69      pooka     475:        PUFFS_MSG_ENQUEUEWAIT(pmp, park_statvfs, error);
1.60      pooka     476:        error = checkerr(pmp, error, __func__);
1.63      pooka     477:        statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
1.10      pooka     478:
1.1       pooka     479:        /*
                    480:         * Try to produce a sensible result even in the event
                    481:         * of userspace error.
                    482:         *
                    483:         * XXX: cache the copy in non-error case
                    484:         */
                    485:        if (!error) {
1.63      pooka     486:                copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
                    487:                (void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
1.1       pooka     488:                    sizeof(struct statvfs));
                    489:        } else {
                    490:                copy_statvfs_info(sbp, mp);
                    491:        }
                    492:
1.63      pooka     493:        PUFFS_MSG_RELEASE(statvfs);
1.14      pooka     494:        return error;
1.1       pooka     495: }
                    496:
1.111     christos  497: static bool
                    498: pageflush_selector(void *cl, struct vnode *vp)
                    499: {
1.113     christos  500:        return vp->v_type == VREG &&
                    501:            !(LIST_EMPTY(&vp->v_dirtyblkhd) && UVM_OBJ_IS_CLEAN(&vp->v_uobj));
1.111     christos  502: }
                    503:
1.26      pooka     504: static int
1.83      pooka     505: pageflush(struct mount *mp, kauth_cred_t cred, int waitfor)
1.1       pooka     506: {
1.26      pooka     507:        struct puffs_node *pn;
1.108     hannken   508:        struct vnode *vp;
                    509:        struct vnode_iterator *marker;
1.97      manu      510:        int error, rv, fsyncwait;
1.1       pooka     511:
1.18      pooka     512:        error = 0;
1.97      manu      513:        fsyncwait = (waitfor == MNT_WAIT) ? FSYNC_WAIT : 0;
1.18      pooka     514:
                    515:        /*
1.24      pooka     516:         * Sync all cached data from regular vnodes (which are not
                    517:         * currently locked, see below).  After this we call VFS_SYNC
                    518:         * for the fs server, which should handle data and metadata for
                    519:         * all the nodes it knows to exist.
1.18      pooka     520:         */
1.108     hannken   521:        vfs_vnode_iterator_init(mp, &marker);
1.111     christos  522:        while ((vp = vfs_vnode_iterator_next(marker, pageflush_selector,
                    523:            NULL)))
                    524:        {
1.21      pooka     525:                /*
                    526:                 * Here we try to get a reference to the vnode and to
                    527:                 * lock it.  This is mostly cargo-culted, but I will
                    528:                 * offer an explanation to why I believe this might
                    529:                 * actually do the right thing.
                    530:                 *
                    531:                 * If the vnode is a goner, we quite obviously don't need
                    532:                 * to sync it.
                    533:                 *
                    534:                 * If the vnode was busy, we don't need to sync it because
                    535:                 * this is never called with MNT_WAIT except from
                    536:                 * dounmount(), when we are wait-flushing all the dirty
                    537:                 * vnodes through other routes in any case.  So there,
                    538:                 * sync() doesn't actually sync.  Happy now?
                    539:                 */
1.108     hannken   540:                error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
                    541:                if (error) {
                    542:                        vrele(vp);
                    543:                        continue;
                    544:                }
                    545:                pn = VPTOPP(vp);
1.83      pooka     546:                /* hmm.. is the FAF thing entirely sensible? */
                    547:                if (waitfor == MNT_LAZY) {
1.96      rmind     548:                        mutex_enter(vp->v_interlock);
1.83      pooka     549:                        pn->pn_stat |= PNODE_FAF;
1.96      rmind     550:                        mutex_exit(vp->v_interlock);
1.30      pooka     551:                }
1.97      manu      552:                rv = VOP_FSYNC(vp, cred, fsyncwait, 0, 0);
1.83      pooka     553:                if (waitfor == MNT_LAZY) {
1.96      rmind     554:                        mutex_enter(vp->v_interlock);
1.83      pooka     555:                        pn->pn_stat &= ~PNODE_FAF;
1.96      rmind     556:                        mutex_exit(vp->v_interlock);
1.26      pooka     557:                }
1.21      pooka     558:                if (rv)
1.18      pooka     559:                        error = rv;
                    560:                vput(vp);
                    561:        }
1.108     hannken   562:        vfs_vnode_iterator_destroy(marker);
1.18      pooka     563:
1.26      pooka     564:        return error;
                    565: }
                    566:
                    567: int
1.73      pooka     568: puffs_vfsop_sync(struct mount *mp, int waitfor, struct kauth_cred *cred)
1.26      pooka     569: {
1.63      pooka     570:        PUFFS_MSG_VARS(vfs, sync);
1.62      pooka     571:        struct puffs_mount *pmp = MPTOPUFFSMP(mp);
1.26      pooka     572:        int error, rv;
                    573:
1.83      pooka     574:        error = pageflush(mp, cred, waitfor);
1.26      pooka     575:
1.18      pooka     576:        /* sync fs */
1.63      pooka     577:        PUFFS_MSG_ALLOC(vfs, sync);
                    578:        sync_msg->pvfsr_waitfor = waitfor;
                    579:        puffs_credcvt(&sync_msg->pvfsr_cred, cred);
1.69      pooka     580:        puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL);
1.1       pooka     581:
1.69      pooka     582:        PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv);
1.62      pooka     583:        rv = checkerr(pmp, rv, __func__);
1.18      pooka     584:        if (rv)
                    585:                error = rv;
1.1       pooka     586:
1.63      pooka     587:        PUFFS_MSG_RELEASE(sync);
1.1       pooka     588:        return error;
                    589: }
                    590:
                    591: int
1.73      pooka     592: puffs_vfsop_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1.1       pooka     593: {
1.63      pooka     594:        PUFFS_MSG_VARS(vfs, fhtonode);
1.33      pooka     595:        struct puffs_mount *pmp = MPTOPUFFSMP(mp);
                    596:        struct vnode *vp;
1.54      pooka     597:        void *fhdata;
                    598:        size_t argsize, fhlen;
1.33      pooka     599:        int error;
                    600:
1.38      pooka     601:        if (pmp->pmp_args.pa_fhsize == 0)
1.33      pooka     602:                return EOPNOTSUPP;
                    603:
1.54      pooka     604:        if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
                    605:                fhlen = fhp->fid_len;
                    606:                fhdata = fhp;
1.39      pooka     607:        } else {
1.54      pooka     608:                fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
                    609:                fhdata = fhp->fid_data;
                    610:
                    611:                if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
                    612:                        if (pmp->pmp_args.pa_fhsize < fhlen)
                    613:                                return EINVAL;
                    614:                } else {
                    615:                        if (pmp->pmp_args.pa_fhsize != fhlen)
                    616:                                return EINVAL;
                    617:                }
1.39      pooka     618:        }
1.33      pooka     619:
1.63      pooka     620:        argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
1.75      pooka     621:        puffs_msgmem_alloc(argsize, &park_fhtonode, (void *)&fhtonode_msg, 1);
1.63      pooka     622:        fhtonode_msg->pvfsr_dsize = fhlen;
                    623:        memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
1.69      pooka     624:        puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL);
1.33      pooka     625:
1.69      pooka     626:        PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error);
1.60      pooka     627:        error = checkerr(pmp, error, __func__);
1.33      pooka     628:        if (error)
1.38      pooka     629:                goto out;
1.33      pooka     630:
1.114     hannken   631:        error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
                    632:            fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
                    633:            fhtonode_msg->pvfsr_rdev, &vp);
                    634:        if (error)
1.59      pooka     635:                goto out;
1.114     hannken   636:        vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1.1       pooka     637:
1.33      pooka     638:        *vpp = vp;
1.38      pooka     639:  out:
1.63      pooka     640:        puffs_msgmem_release(park_fhtonode);
1.38      pooka     641:        return error;
1.1       pooka     642: }
                    643:
                    644: int
1.73      pooka     645: puffs_vfsop_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1.1       pooka     646: {
1.63      pooka     647:        PUFFS_MSG_VARS(vfs, nodetofh);
1.33      pooka     648:        struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
1.54      pooka     649:        size_t argsize, fhlen;
1.33      pooka     650:        int error;
                    651:
1.38      pooka     652:        if (pmp->pmp_args.pa_fhsize == 0)
1.33      pooka     653:                return EOPNOTSUPP;
                    654:
1.54      pooka     655:        /* if file handles are static len, we can test len immediately */
1.38      pooka     656:        if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
1.54      pooka     657:            && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
1.38      pooka     658:            && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
                    659:                *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
1.33      pooka     660:                return E2BIG;
                    661:        }
1.1       pooka     662:
1.54      pooka     663:        if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
                    664:                fhlen = *fh_size;
                    665:        else
                    666:                fhlen = PUFFS_FROMFHSIZE(*fh_size);
                    667:
1.63      pooka     668:        argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
1.75      pooka     669:        puffs_msgmem_alloc(argsize, &park_nodetofh, (void *)&nodetofh_msg, 1);
1.63      pooka     670:        nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
                    671:        nodetofh_msg->pvfsr_dsize = fhlen;
1.69      pooka     672:        puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL);
1.38      pooka     673:
1.69      pooka     674:        PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error);
1.60      pooka     675:        error = checkerr(pmp, error, __func__);
1.54      pooka     676:
                    677:        if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
1.63      pooka     678:                fhlen = nodetofh_msg->pvfsr_dsize;
1.54      pooka     679:        else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
1.63      pooka     680:                fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
1.54      pooka     681:        else
                    682:                fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
                    683:
1.38      pooka     684:        if (error) {
                    685:                if (error == E2BIG)
1.54      pooka     686:                        *fh_size = fhlen;
1.38      pooka     687:                goto out;
                    688:        }
                    689:
1.54      pooka     690:        if (fhlen > FHANDLE_SIZE_MAX) {
1.69      pooka     691:                puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG,
1.60      pooka     692:                    "file handle too big", VPTOPNC(vp));
1.58      pooka     693:                error = EPROTO;
1.38      pooka     694:                goto out;
                    695:        }
1.33      pooka     696:
1.54      pooka     697:        if (*fh_size < fhlen) {
                    698:                *fh_size = fhlen;
1.38      pooka     699:                error = E2BIG;
                    700:                goto out;
                    701:        }
1.54      pooka     702:        *fh_size = fhlen;
1.1       pooka     703:
1.38      pooka     704:        if (fhp) {
1.54      pooka     705:                if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
1.63      pooka     706:                        memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
1.54      pooka     707:                } else {
                    708:                        fhp->fid_len = *fh_size;
1.63      pooka     709:                        memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
                    710:                            nodetofh_msg->pvfsr_dsize);
1.54      pooka     711:                }
1.38      pooka     712:        }
1.1       pooka     713:
1.38      pooka     714:  out:
1.63      pooka     715:        puffs_msgmem_release(park_nodetofh);
1.38      pooka     716:        return error;
1.1       pooka     717: }
                    718:
1.114     hannken   719: int
                    720: puffs_vfsop_loadvnode(struct mount *mp, struct vnode *vp,
                    721:     const void *key, size_t key_len, const void **new_key)
                    722: {
                    723:        struct puffs_mount *pmp;
                    724:        struct puffs_node *pnode;
                    725:
                    726:        KASSERT(key_len == sizeof(puffs_cookie_t));
                    727:
                    728:        pmp = MPTOPUFFSMP(mp);
                    729:
                    730:        /* Allocate and initialize the pnode. */
                    731:        pnode = pool_get(&puffs_pnpool, PR_WAITOK);
                    732:        memset(pnode, 0, sizeof(struct puffs_node));
                    733:
                    734:        pnode->pn_vp = vp;
                    735:        memcpy(&pnode->pn_cookie, key, key_len);
                    736:        pnode->pn_refcount = 1;
                    737:        mutex_init(&pnode->pn_mtx, MUTEX_DEFAULT, IPL_NONE);
                    738:        mutex_init(&pnode->pn_sizemtx, MUTEX_DEFAULT, IPL_NONE);
                    739:        selinit(&pnode->pn_sel);
                    740:        vp->v_tag = VT_PUFFS;
                    741:        vp->v_type = VNON;
                    742:        vp->v_op = puffs_vnodeop_p;
                    743:        if (pnode->pn_cookie == pmp->pmp_root_cookie)
                    744:                vp->v_vflag |= VV_ROOT;
                    745:        vp->v_data = pnode;
                    746:
                    747:        genfs_node_init(vp, &puffs_genfsops);
                    748:        uvm_vnp_setsize(vp, 0);
                    749:
                    750:        *new_key = &pnode->pn_cookie;
                    751:        return 0;
                    752: }
                    753:
1.1       pooka     754: void
1.82      cegger    755: puffs_vfsop_init(void)
1.1       pooka     756: {
                    757:
1.56      pooka     758:        /* some checks depend on this */
                    759:        KASSERT(VNOVAL == VSIZENOTSET);
                    760:
1.31      pooka     761:        pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
                    762:            "puffpnpl", &pool_allocator_nointr, IPL_NONE);
1.101     manu      763:        pool_init(&puffs_vapool, sizeof(struct vattr), 0, 0, 0,
                    764:            "puffvapl", &pool_allocator_nointr, IPL_NONE);
1.31      pooka     765:        puffs_msgif_init();
1.1       pooka     766: }
                    767:
                    768: void
1.82      cegger    769: puffs_vfsop_done(void)
1.1       pooka     770: {
                    771:
1.31      pooka     772:        puffs_msgif_destroy();
                    773:        pool_destroy(&puffs_pnpool);
1.101     manu      774:        pool_destroy(&puffs_vapool);
1.1       pooka     775: }
                    776:
                    777: int
1.73      pooka     778: puffs_vfsop_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
1.1       pooka     779: {
                    780:
                    781:        return EOPNOTSUPP;
                    782: }
                    783:
1.88      pooka     784: int
                    785: puffs_vfsop_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
                    786:        int attrnamespace, const char *attrname)
                    787: {
                    788:        PUFFS_MSG_VARS(vfs, extattrctl);
                    789:        struct puffs_mount *pmp = MPTOPUFFSMP(mp);
                    790:        struct puffs_node *pnp;
                    791:        puffs_cookie_t pnc;
                    792:        int error, flags;
                    793:
                    794:        if (vp) {
                    795:                /* doesn't make sense for puffs servers */
                    796:                if (vp->v_mount != mp)
                    797:                        return EXDEV;
                    798:                pnp = vp->v_data;
                    799:                pnc = pnp->pn_cookie;
                    800:                flags = PUFFS_EXTATTRCTL_HASNODE;
                    801:        } else {
                    802:                pnp = pnc = NULL;
                    803:                flags = 0;
                    804:        }
                    805:
                    806:        PUFFS_MSG_ALLOC(vfs, extattrctl);
                    807:        extattrctl_msg->pvfsr_cmd = cmd;
                    808:        extattrctl_msg->pvfsr_attrnamespace = attrnamespace;
                    809:        extattrctl_msg->pvfsr_flags = flags;
                    810:        if (attrname) {
                    811:                strlcpy(extattrctl_msg->pvfsr_attrname, attrname,
                    812:                    sizeof(extattrctl_msg->pvfsr_attrname));
                    813:                extattrctl_msg->pvfsr_flags |= PUFFS_EXTATTRCTL_HASATTRNAME;
                    814:        }
                    815:        puffs_msg_setinfo(park_extattrctl,
                    816:            PUFFSOP_VFS, PUFFS_VFS_EXTATTRCTL, pnc);
                    817:
                    818:        puffs_msg_enqueue(pmp, park_extattrctl);
                    819:        if (vp) {
                    820:                mutex_enter(&pnp->pn_mtx);
                    821:                puffs_referencenode(pnp);
                    822:                mutex_exit(&pnp->pn_mtx);
1.90      hannken   823:                VOP_UNLOCK(vp);
1.88      pooka     824:        }
                    825:        error = puffs_msg_wait2(pmp, park_extattrctl, pnp, NULL);
                    826:        PUFFS_MSG_RELEASE(extattrctl);
                    827:        if (vp) {
                    828:                puffs_releasenode(pnp);
                    829:        }
                    830:
                    831:        return checkerr(pmp, error, __func__);
                    832: }
                    833:
1.1       pooka     834: const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
                    835:        &puffs_vnodeop_opv_desc,
1.3       pooka     836:        &puffs_specop_opv_desc,
1.4       pooka     837:        &puffs_fifoop_opv_desc,
1.12      pooka     838:        &puffs_msgop_opv_desc,
1.1       pooka     839:        NULL,
                    840: };
                    841:
                    842: struct vfsops puffs_vfsops = {
1.109     hannken   843:        .vfs_name = MOUNT_PUFFS,
                    844:        .vfs_min_mount_data = sizeof (struct puffs_kargs),
                    845:        .vfs_mount = puffs_vfsop_mount,
                    846:        .vfs_start = puffs_vfsop_start,
                    847:        .vfs_unmount = puffs_vfsop_unmount,
                    848:        .vfs_root = puffs_vfsop_root,
                    849:        .vfs_quotactl = (void *)eopnotsupp,
                    850:        .vfs_statvfs = puffs_vfsop_statvfs,
                    851:        .vfs_sync = puffs_vfsop_sync,
                    852:        .vfs_vget = (void *)eopnotsupp,
1.114     hannken   853:        .vfs_loadvnode = puffs_vfsop_loadvnode,
1.109     hannken   854:        .vfs_fhtovp = puffs_vfsop_fhtovp,
                    855:        .vfs_vptofh = puffs_vfsop_vptofh,
                    856:        .vfs_init = puffs_vfsop_init,
                    857:        .vfs_done = puffs_vfsop_done,
                    858:        .vfs_snapshot = puffs_vfsop_snapshot,
                    859:        .vfs_extattrctl = puffs_vfsop_extattrctl,
                    860:        .vfs_suspendctl = (void *)eopnotsupp,
                    861:        .vfs_renamelock_enter = genfs_renamelock_enter,
                    862:        .vfs_renamelock_exit = genfs_renamelock_exit,
                    863:        .vfs_fsync = (void *)eopnotsupp,
                    864:        .vfs_opv_descs = puffs_vnodeopv_descs
1.1       pooka     865: };
1.80      rumble    866:
                    867: static int
                    868: puffs_modcmd(modcmd_t cmd, void *arg)
                    869: {
                    870:
                    871:        switch (cmd) {
                    872:        case MODULE_CMD_INIT:
                    873:                return vfs_attach(&puffs_vfsops);
                    874:        case MODULE_CMD_FINI:
                    875:                return vfs_detach(&puffs_vfsops);
                    876:        default:
                    877:                return ENOTTY;
                    878:        }
                    879: }

CVSweb <webmaster@jp.NetBSD.org>