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