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