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