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