Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.84
      1 /*	$NetBSD: puffs_vfsops.c,v 1.84 2009/12/07 20:57:55 pooka Exp $	*/
      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>
     33 __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.84 2009/12/07 20:57:55 pooka Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/mount.h>
     37 #include <sys/malloc.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>
     43 #include <sys/proc.h>
     44 #include <sys/module.h>
     45 #include <sys/kthread.h>
     46 
     47 #include <dev/putter/putter_sys.h>
     48 
     49 #include <miscfs/genfs/genfs.h>
     50 
     51 #include <fs/puffs/puffs_msgif.h>
     52 #include <fs/puffs/puffs_sys.h>
     53 
     54 #include <lib/libkern/libkern.h>
     55 
     56 #include <nfs/nfsproto.h> /* for fh sizes */
     57 
     58 MODULE(MODULE_CLASS_VFS, puffs, "putter");
     59 
     60 VFS_PROTOS(puffs_vfsop);
     61 
     62 #ifndef PUFFS_PNODEBUCKETS
     63 #define PUFFS_PNODEBUCKETS 256
     64 #endif
     65 #ifndef PUFFS_MAXPNODEBUCKETS
     66 #define PUFFS_MAXPNODEBUCKETS 8192
     67 #endif
     68 int puffs_pnodebuckets_default = PUFFS_PNODEBUCKETS;
     69 int puffs_maxpnodebuckets = PUFFS_MAXPNODEBUCKETS;
     70 
     71 #define BUCKETALLOC(a) (sizeof(struct puffs_pnode_hashlist *) * (a))
     72 
     73 static struct putter_ops puffs_putter = {
     74 	.pop_getout	= puffs_msgif_getout,
     75 	.pop_releaseout	= puffs_msgif_releaseout,
     76 	.pop_waitcount	= puffs_msgif_waitcount,
     77 	.pop_dispatch	= puffs_msgif_dispatch,
     78 	.pop_close	= puffs_msgif_close,
     79 };
     80 
     81 int
     82 puffs_vfsop_mount(struct mount *mp, const char *path, void *data,
     83 	size_t *data_len)
     84 {
     85 	struct puffs_mount *pmp = NULL;
     86 	struct puffs_kargs *args;
     87 	char fstype[_VFS_NAMELEN];
     88 	char *p;
     89 	int error = 0, i;
     90 	pid_t mntpid = curlwp->l_proc->p_pid;
     91 
     92 	if (*data_len < sizeof *args)
     93 		return EINVAL;
     94 
     95 	if (mp->mnt_flag & MNT_GETARGS) {
     96 		pmp = MPTOPUFFSMP(mp);
     97 		*(struct puffs_kargs *)data = pmp->pmp_args;
     98 		*data_len = sizeof *args;
     99 		return 0;
    100 	}
    101 
    102 	/* update is not supported currently */
    103 	if (mp->mnt_flag & MNT_UPDATE)
    104 		return EOPNOTSUPP;
    105 
    106 	/*
    107 	 * We need the file system name
    108 	 */
    109 	if (!data)
    110 		return EINVAL;
    111 
    112 	args = (struct puffs_kargs *)data;
    113 
    114 	/* devel phase */
    115 	if (args->pa_vers != (PUFFSVERSION | PUFFSDEVELVERS)) {
    116 		printf("puffs_mount: development version mismatch: "
    117 		    "kernel %d, lib %d\n",
    118 		    PUFFSVERSION, args->pa_vers & ~PUFFSDEVELVERS);
    119 		error = EINVAL;
    120 		goto out;
    121 	}
    122 
    123 	if ((args->pa_flags & ~PUFFS_KFLAG_MASK) != 0) {
    124 		printf("puffs_mount: invalid KFLAGs 0x%x\n", args->pa_flags);
    125 		error = EINVAL;
    126 		goto out;
    127 	}
    128 	if ((args->pa_fhflags & ~PUFFS_FHFLAG_MASK) != 0) {
    129 		printf("puffs_mount: invalid FHFLAGs 0x%x\n", args->pa_fhflags);
    130 		error = EINVAL;
    131 		goto out;
    132 	}
    133 
    134 	/* use dummy value for passthrough */
    135 	if (args->pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    136 		args->pa_fhsize = sizeof(struct fid);
    137 
    138 	/* sanitize file handle length */
    139 	if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) {
    140 		printf("puffs_mount: handle size %zu too large\n",
    141 		    args->pa_fhsize);
    142 		error = EINVAL;
    143 		goto out;
    144 	}
    145 	/* sanity check file handle max sizes */
    146 	if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) {
    147 		size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize);
    148 
    149 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) {
    150 			if (NFSX_FHTOOBIG_P(kfhsize, 0)) {
    151 				printf("puffs_mount: fhsize larger than "
    152 				    "NFSv2 max %d\n",
    153 				    PUFFS_FROMFHSIZE(NFSX_V2FH));
    154 				error = EINVAL;
    155 				goto out;
    156 			}
    157 		}
    158 
    159 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) {
    160 			if (NFSX_FHTOOBIG_P(kfhsize, 1)) {
    161 				printf("puffs_mount: fhsize larger than "
    162 				    "NFSv3 max %d\n",
    163 				    PUFFS_FROMFHSIZE(NFSX_V3FHMAX));
    164 				error = EINVAL;
    165 				goto out;
    166 			}
    167 		}
    168 	}
    169 
    170 	/* don't allow non-printing characters (like my sweet umlauts.. snif) */
    171 	args->pa_typename[sizeof(args->pa_typename)-1] = '\0';
    172 	for (p = args->pa_typename; *p; p++)
    173 		if (*p < ' ' || *p > '~')
    174 			*p = '.';
    175 
    176 	args->pa_mntfromname[sizeof(args->pa_mntfromname)-1] = '\0';
    177 	for (p = args->pa_mntfromname; *p; p++)
    178 		if (*p < ' ' || *p > '~')
    179 			*p = '.';
    180 
    181 	/* build real name */
    182 	(void)strlcpy(fstype, PUFFS_TYPEPREFIX, sizeof(fstype));
    183 	(void)strlcat(fstype, args->pa_typename, sizeof(fstype));
    184 
    185 	/* inform user server if it got the max request size it wanted */
    186 	if (args->pa_maxmsglen == 0 || args->pa_maxmsglen > PUFFS_MSG_MAXSIZE)
    187 		args->pa_maxmsglen = PUFFS_MSG_MAXSIZE;
    188 	else if (args->pa_maxmsglen < 2*PUFFS_MSGSTRUCT_MAX)
    189 		args->pa_maxmsglen = 2*PUFFS_MSGSTRUCT_MAX;
    190 
    191 	(void)strlcpy(args->pa_typename, fstype, sizeof(args->pa_typename));
    192 
    193 	if (args->pa_nhashbuckets == 0)
    194 		args->pa_nhashbuckets = puffs_pnodebuckets_default;
    195 	if (args->pa_nhashbuckets < 1)
    196 		args->pa_nhashbuckets = 1;
    197 	if (args->pa_nhashbuckets > PUFFS_MAXPNODEBUCKETS) {
    198 		args->pa_nhashbuckets = puffs_maxpnodebuckets;
    199 		printf("puffs_mount: using %d hash buckets. "
    200 		    "adjust puffs_maxpnodebuckets for more\n",
    201 		    puffs_maxpnodebuckets);
    202 	}
    203 
    204 	error = set_statvfs_info(path, UIO_USERSPACE, args->pa_mntfromname,
    205 	    UIO_SYSSPACE, fstype, mp, curlwp);
    206 	if (error)
    207 		goto out;
    208 	mp->mnt_stat.f_iosize = DEV_BSIZE;
    209 
    210 	/*
    211 	 * We can't handle the VFS_STATVFS() mount_domount() does
    212 	 * after VFS_MOUNT() because we'd deadlock, so handle it
    213 	 * here already.
    214 	 */
    215 	copy_statvfs_info(&args->pa_svfsb, mp);
    216 	(void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat));
    217 
    218 	pmp = kmem_zalloc(sizeof(struct puffs_mount), KM_SLEEP);
    219 
    220 	mp->mnt_fs_bshift = DEV_BSHIFT;
    221 	mp->mnt_dev_bshift = DEV_BSHIFT;
    222 	mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
    223 	mp->mnt_data = pmp;
    224 
    225 	pmp->pmp_status = PUFFSTAT_MOUNTING;
    226 	pmp->pmp_mp = mp;
    227 	pmp->pmp_msg_maxsize = args->pa_maxmsglen;
    228 	pmp->pmp_args = *args;
    229 
    230 	pmp->pmp_npnodehash = args->pa_nhashbuckets;
    231 	pmp->pmp_pnodehash = kmem_alloc(BUCKETALLOC(pmp->pmp_npnodehash), KM_SLEEP);
    232 	for (i = 0; i < pmp->pmp_npnodehash; i++)
    233 		LIST_INIT(&pmp->pmp_pnodehash[i]);
    234 	LIST_INIT(&pmp->pmp_newcookie);
    235 
    236 	/*
    237 	 * Inform the fileops processing code that we have a mountpoint.
    238 	 * If it doesn't know about anyone with our pid/fd having the
    239 	 * device open, punt
    240 	 */
    241 	if ((pmp->pmp_pi
    242 	    = putter_attach(mntpid, args->pa_fd, pmp, &puffs_putter)) == NULL) {
    243 		error = ENOENT;
    244 		goto out;
    245 	}
    246 
    247 	/* XXX: check parameters */
    248 	pmp->pmp_root_cookie = args->pa_root_cookie;
    249 	pmp->pmp_root_vtype = args->pa_root_vtype;
    250 	pmp->pmp_root_vsize = args->pa_root_vsize;
    251 	pmp->pmp_root_rdev = args->pa_root_rdev;
    252 
    253 	mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
    254 	mutex_init(&pmp->pmp_sopmtx, MUTEX_DEFAULT, IPL_NONE);
    255 	cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
    256 	cv_init(&pmp->pmp_refcount_cv, "puffsref");
    257 	cv_init(&pmp->pmp_unmounting_cv, "puffsum");
    258 	cv_init(&pmp->pmp_sopcv, "puffsop");
    259 	TAILQ_INIT(&pmp->pmp_msg_touser);
    260 	TAILQ_INIT(&pmp->pmp_msg_replywait);
    261 	TAILQ_INIT(&pmp->pmp_sopreqs);
    262 
    263 	if ((error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    264 	    puffs_sop_thread, pmp, NULL, "puffsop")) != 0)
    265 		goto out;
    266 	pmp->pmp_sopthrcount = 1;
    267 
    268 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    269 	    mp, MPTOPUFFSMP(mp)));
    270 
    271 	vfs_getnewfsid(mp);
    272 
    273  out:
    274 	if (error && pmp && pmp->pmp_pi)
    275 		putter_detach(pmp->pmp_pi);
    276 	if (error && pmp && pmp->pmp_pnodehash)
    277 		kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash));
    278 	if (error && pmp)
    279 		kmem_free(pmp, sizeof(struct puffs_mount));
    280 	return error;
    281 }
    282 
    283 int
    284 puffs_vfsop_start(struct mount *mp, int flags)
    285 {
    286 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    287 
    288 	KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
    289 	pmp->pmp_status = PUFFSTAT_RUNNING;
    290 
    291 	return 0;
    292 }
    293 
    294 int
    295 puffs_vfsop_unmount(struct mount *mp, int mntflags)
    296 {
    297 	PUFFS_MSG_VARS(vfs, unmount);
    298 	struct puffs_mount *pmp;
    299 	int error, force;
    300 
    301 	error = 0;
    302 	force = mntflags & MNT_FORCE;
    303 	pmp = MPTOPUFFSMP(mp);
    304 
    305 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    306 	    "status 0x%x\n", pmp->pmp_status));
    307 
    308 	/*
    309 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    310 	 * root vnode does not get flushed until unmount.  The
    311 	 * userspace root node cookie is stored in the mount
    312 	 * structure, so we can always re-instantiate a root vnode,
    313 	 * should userspace unmount decide it doesn't want to
    314 	 * cooperate.
    315 	 */
    316 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    317 	if (error)
    318 		goto out;
    319 
    320 	/*
    321 	 * If we are not DYING, we should ask userspace's opinion
    322 	 * about the situation
    323 	 */
    324 	mutex_enter(&pmp->pmp_lock);
    325 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    326 		pmp->pmp_unmounting = 1;
    327 		mutex_exit(&pmp->pmp_lock);
    328 
    329 		PUFFS_MSG_ALLOC(vfs, unmount);
    330 		puffs_msg_setinfo(park_unmount,
    331 		    PUFFSOP_VFS, PUFFS_VFS_UNMOUNT, NULL);
    332 		unmount_msg->pvfsr_flags = mntflags;
    333 
    334 		PUFFS_MSG_ENQUEUEWAIT(pmp, park_unmount, error);
    335 		PUFFS_MSG_RELEASE(unmount);
    336 
    337 		error = checkerr(pmp, error, __func__);
    338 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    339 
    340 		mutex_enter(&pmp->pmp_lock);
    341 		pmp->pmp_unmounting = 0;
    342 		cv_broadcast(&pmp->pmp_unmounting_cv);
    343 	}
    344 
    345 	/*
    346 	 * if userspace cooperated or we really need to die,
    347 	 * screw what userland thinks and just die.
    348 	 */
    349 	if (error == 0 || force) {
    350 		struct puffs_sopreq *psopr;
    351 
    352 		/* tell waiters & other resources to go unwait themselves */
    353 		puffs_userdead(pmp);
    354 		putter_detach(pmp->pmp_pi);
    355 
    356 		/*
    357 		 * Wait until there are no more users for the mount resource.
    358 		 * Notice that this is hooked against transport_close
    359 		 * and return from touser.  In an ideal world, it would
    360 		 * be hooked against final return from all operations.
    361 		 * But currently it works well enough, since nobody
    362 		 * does weird blocking voodoo after return from touser().
    363 		 */
    364 		while (pmp->pmp_refcount != 0)
    365 			cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
    366 		mutex_exit(&pmp->pmp_lock);
    367 
    368 		/*
    369 		 * Release kernel thread now that there is nothing
    370 		 * it would be wanting to lock.
    371 		 */
    372 		psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
    373 		psopr->psopr_sopreq = PUFFS_SOPREQ_EXIT;
    374 		mutex_enter(&pmp->pmp_sopmtx);
    375 		TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs, psopr, psopr_entries);
    376 		cv_signal(&pmp->pmp_sopcv);
    377 		while (pmp->pmp_sopthrcount > 0)
    378 			cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
    379 		mutex_exit(&pmp->pmp_sopmtx);
    380 
    381 		/* free resources now that we hopefully have no waiters left */
    382 		cv_destroy(&pmp->pmp_unmounting_cv);
    383 		cv_destroy(&pmp->pmp_refcount_cv);
    384 		cv_destroy(&pmp->pmp_msg_waiter_cv);
    385 		cv_destroy(&pmp->pmp_sopcv);
    386 		mutex_destroy(&pmp->pmp_lock);
    387 		mutex_destroy(&pmp->pmp_sopmtx);
    388 
    389 		kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash));
    390 		kmem_free(pmp, sizeof(struct puffs_mount));
    391 		error = 0;
    392 	} else {
    393 		mutex_exit(&pmp->pmp_lock);
    394 	}
    395 
    396  out:
    397 	DPRINTF(("puffs_unmount: return %d\n", error));
    398 	return error;
    399 }
    400 
    401 /*
    402  * This doesn't need to travel to userspace
    403  */
    404 int
    405 puffs_vfsop_root(struct mount *mp, struct vnode **vpp)
    406 {
    407 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    408 	int rv;
    409 
    410 	rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, 1, 1, vpp);
    411 	KASSERT(rv != PUFFS_NOSUCHCOOKIE);
    412 	return rv;
    413 }
    414 
    415 int
    416 puffs_vfsop_statvfs(struct mount *mp, struct statvfs *sbp)
    417 {
    418 	PUFFS_MSG_VARS(vfs, statvfs);
    419 	struct puffs_mount *pmp;
    420 	int error = 0;
    421 
    422 	pmp = MPTOPUFFSMP(mp);
    423 
    424 	/*
    425 	 * If we are mounting, it means that the userspace counterpart
    426 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    427 	 * requesting statvfs from userspace would mean a deadlock.
    428 	 * Compensate.
    429 	 */
    430 	if (pmp->pmp_status == PUFFSTAT_MOUNTING)
    431 		return EINPROGRESS;
    432 
    433 	PUFFS_MSG_ALLOC(vfs, statvfs);
    434 	puffs_msg_setinfo(park_statvfs, PUFFSOP_VFS, PUFFS_VFS_STATVFS, NULL);
    435 
    436 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_statvfs, error);
    437 	error = checkerr(pmp, error, __func__);
    438 	statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
    439 
    440 	/*
    441 	 * Try to produce a sensible result even in the event
    442 	 * of userspace error.
    443 	 *
    444 	 * XXX: cache the copy in non-error case
    445 	 */
    446 	if (!error) {
    447 		copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
    448 		(void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
    449 		    sizeof(struct statvfs));
    450 	} else {
    451 		copy_statvfs_info(sbp, mp);
    452 	}
    453 
    454 	PUFFS_MSG_RELEASE(statvfs);
    455 	return error;
    456 }
    457 
    458 static int
    459 pageflush(struct mount *mp, kauth_cred_t cred, int waitfor)
    460 {
    461 	struct puffs_node *pn;
    462 	struct vnode *vp, *mvp;
    463 	int error, rv;
    464 
    465 	error = 0;
    466 
    467 	/* Allocate a marker vnode. */
    468 	if ((mvp = vnalloc(mp)) == NULL)
    469 		return ENOMEM;
    470 
    471 	/*
    472 	 * Sync all cached data from regular vnodes (which are not
    473 	 * currently locked, see below).  After this we call VFS_SYNC
    474 	 * for the fs server, which should handle data and metadata for
    475 	 * all the nodes it knows to exist.
    476 	 */
    477 	mutex_enter(&mntvnode_lock);
    478  loop:
    479 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    480 		vmark(mvp, vp);
    481 		if (vp->v_mount != mp || vismarker(vp))
    482 			continue;
    483 
    484 		mutex_enter(&vp->v_interlock);
    485 		pn = VPTOPP(vp);
    486 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    487 			mutex_exit(&vp->v_interlock);
    488 			continue;
    489 		}
    490 
    491 		mutex_exit(&mntvnode_lock);
    492 
    493 		/*
    494 		 * Here we try to get a reference to the vnode and to
    495 		 * lock it.  This is mostly cargo-culted, but I will
    496 		 * offer an explanation to why I believe this might
    497 		 * actually do the right thing.
    498 		 *
    499 		 * If the vnode is a goner, we quite obviously don't need
    500 		 * to sync it.
    501 		 *
    502 		 * If the vnode was busy, we don't need to sync it because
    503 		 * this is never called with MNT_WAIT except from
    504 		 * dounmount(), when we are wait-flushing all the dirty
    505 		 * vnodes through other routes in any case.  So there,
    506 		 * sync() doesn't actually sync.  Happy now?
    507 		 */
    508 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    509 		if (rv) {
    510 			mutex_enter(&mntvnode_lock);
    511 			if (rv == ENOENT) {
    512 				(void)vunmark(mvp);
    513 				goto loop;
    514 			}
    515 			continue;
    516 		}
    517 
    518 		/* hmm.. is the FAF thing entirely sensible? */
    519 		if (waitfor == MNT_LAZY) {
    520 			mutex_enter(&vp->v_interlock);
    521 			pn->pn_stat |= PNODE_FAF;
    522 			mutex_exit(&vp->v_interlock);
    523 		}
    524 		rv = VOP_FSYNC(vp, cred, waitfor, 0, 0);
    525 		if (waitfor == MNT_LAZY) {
    526 			mutex_enter(&vp->v_interlock);
    527 			pn->pn_stat &= ~PNODE_FAF;
    528 			mutex_exit(&vp->v_interlock);
    529 		}
    530 		if (rv)
    531 			error = rv;
    532 		vput(vp);
    533 		mutex_enter(&mntvnode_lock);
    534 	}
    535 	mutex_exit(&mntvnode_lock);
    536 	vnfree(mvp);
    537 
    538 	return error;
    539 }
    540 
    541 int
    542 puffs_vfsop_sync(struct mount *mp, int waitfor, struct kauth_cred *cred)
    543 {
    544 	PUFFS_MSG_VARS(vfs, sync);
    545 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    546 	int error, rv;
    547 
    548 	error = pageflush(mp, cred, waitfor);
    549 
    550 	/* sync fs */
    551 	PUFFS_MSG_ALLOC(vfs, sync);
    552 	sync_msg->pvfsr_waitfor = waitfor;
    553 	puffs_credcvt(&sync_msg->pvfsr_cred, cred);
    554 	puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL);
    555 
    556 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv);
    557 	rv = checkerr(pmp, rv, __func__);
    558 	if (rv)
    559 		error = rv;
    560 
    561 	PUFFS_MSG_RELEASE(sync);
    562 	return error;
    563 }
    564 
    565 int
    566 puffs_vfsop_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    567 {
    568 	PUFFS_MSG_VARS(vfs, fhtonode);
    569 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    570 	struct vnode *vp;
    571 	void *fhdata;
    572 	size_t argsize, fhlen;
    573 	int error;
    574 
    575 	if (pmp->pmp_args.pa_fhsize == 0)
    576 		return EOPNOTSUPP;
    577 
    578 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    579 		fhlen = fhp->fid_len;
    580 		fhdata = fhp;
    581 	} else {
    582 		fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
    583 		fhdata = fhp->fid_data;
    584 
    585 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    586 			if (pmp->pmp_args.pa_fhsize < fhlen)
    587 				return EINVAL;
    588 		} else {
    589 			if (pmp->pmp_args.pa_fhsize != fhlen)
    590 				return EINVAL;
    591 		}
    592 	}
    593 
    594 	argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
    595 	puffs_msgmem_alloc(argsize, &park_fhtonode, (void *)&fhtonode_msg, 1);
    596 	fhtonode_msg->pvfsr_dsize = fhlen;
    597 	memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
    598 	puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL);
    599 
    600 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error);
    601 	error = checkerr(pmp, error, __func__);
    602 	if (error)
    603 		goto out;
    604 
    605 	error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp);
    606 	DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
    607 	    fhtonode_msg->pvfsr_fhcookie, vp));
    608 	if (error == PUFFS_NOSUCHCOOKIE) {
    609 		error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
    610 		    fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
    611 		    fhtonode_msg->pvfsr_rdev, &vp);
    612 		if (error)
    613 			goto out;
    614 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    615 	} else if (error) {
    616 		goto out;
    617 	}
    618 
    619 	*vpp = vp;
    620  out:
    621 	puffs_msgmem_release(park_fhtonode);
    622 	return error;
    623 }
    624 
    625 int
    626 puffs_vfsop_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    627 {
    628 	PUFFS_MSG_VARS(vfs, nodetofh);
    629 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    630 	size_t argsize, fhlen;
    631 	int error;
    632 
    633 	if (pmp->pmp_args.pa_fhsize == 0)
    634 		return EOPNOTSUPP;
    635 
    636 	/* if file handles are static len, we can test len immediately */
    637 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    638 	    && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
    639 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    640 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    641 		return E2BIG;
    642 	}
    643 
    644 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    645 		fhlen = *fh_size;
    646 	else
    647 		fhlen = PUFFS_FROMFHSIZE(*fh_size);
    648 
    649 	argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
    650 	puffs_msgmem_alloc(argsize, &park_nodetofh, (void *)&nodetofh_msg, 1);
    651 	nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
    652 	nodetofh_msg->pvfsr_dsize = fhlen;
    653 	puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL);
    654 
    655 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error);
    656 	error = checkerr(pmp, error, __func__);
    657 
    658 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    659 		fhlen = nodetofh_msg->pvfsr_dsize;
    660 	else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
    661 		fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
    662 	else
    663 		fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    664 
    665 	if (error) {
    666 		if (error == E2BIG)
    667 			*fh_size = fhlen;
    668 		goto out;
    669 	}
    670 
    671 	if (fhlen > FHANDLE_SIZE_MAX) {
    672 		puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG,
    673 		    "file handle too big", VPTOPNC(vp));
    674 		error = EPROTO;
    675 		goto out;
    676 	}
    677 
    678 	if (*fh_size < fhlen) {
    679 		*fh_size = fhlen;
    680 		error = E2BIG;
    681 		goto out;
    682 	}
    683 	*fh_size = fhlen;
    684 
    685 	if (fhp) {
    686 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    687 			memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
    688 		} else {
    689 			fhp->fid_len = *fh_size;
    690 			memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
    691 			    nodetofh_msg->pvfsr_dsize);
    692 		}
    693 	}
    694 
    695  out:
    696 	puffs_msgmem_release(park_nodetofh);
    697 	return error;
    698 }
    699 
    700 void
    701 puffs_vfsop_init(void)
    702 {
    703 
    704 	/* some checks depend on this */
    705 	KASSERT(VNOVAL == VSIZENOTSET);
    706 
    707 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    708 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    709 	puffs_msgif_init();
    710 }
    711 
    712 void
    713 puffs_vfsop_done(void)
    714 {
    715 
    716 	puffs_msgif_destroy();
    717 	pool_destroy(&puffs_pnpool);
    718 }
    719 
    720 int
    721 puffs_vfsop_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    722 {
    723 
    724 	return EOPNOTSUPP;
    725 }
    726 
    727 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    728 	&puffs_vnodeop_opv_desc,
    729 	&puffs_specop_opv_desc,
    730 	&puffs_fifoop_opv_desc,
    731 	&puffs_msgop_opv_desc,
    732 	NULL,
    733 };
    734 
    735 struct vfsops puffs_vfsops = {
    736 	MOUNT_PUFFS,
    737 	sizeof (struct puffs_kargs),
    738 	puffs_vfsop_mount,		/* mount	*/
    739 	puffs_vfsop_start,		/* start	*/
    740 	puffs_vfsop_unmount,		/* unmount	*/
    741 	puffs_vfsop_root,		/* root		*/
    742 	(void *)eopnotsupp,		/* quotactl	*/
    743 	puffs_vfsop_statvfs,		/* statvfs	*/
    744 	puffs_vfsop_sync,		/* sync		*/
    745 	(void *)eopnotsupp,		/* vget		*/
    746 	puffs_vfsop_fhtovp,		/* fhtovp	*/
    747 	puffs_vfsop_vptofh,		/* vptofh	*/
    748 	puffs_vfsop_init,		/* init		*/
    749 	NULL,				/* reinit	*/
    750 	puffs_vfsop_done,		/* done		*/
    751 	NULL,				/* mountroot	*/
    752 	puffs_vfsop_snapshot,		/* snapshot	*/
    753 	vfs_stdextattrctl,		/* extattrctl	*/
    754 	(void *)eopnotsupp,		/* suspendctl	*/
    755 	genfs_renamelock_enter,
    756 	genfs_renamelock_exit,
    757 	(void *)eopnotsupp,
    758 	puffs_vnodeopv_descs,		/* vnodeops	*/
    759 	0,				/* refcount	*/
    760 	{ NULL, NULL }
    761 };
    762 
    763 static int
    764 puffs_modcmd(modcmd_t cmd, void *arg)
    765 {
    766 
    767 	switch (cmd) {
    768 	case MODULE_CMD_INIT:
    769 		return vfs_attach(&puffs_vfsops);
    770 	case MODULE_CMD_FINI:
    771 		return vfs_detach(&puffs_vfsops);
    772 	default:
    773 		return ENOTTY;
    774 	}
    775 }
    776