Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.73
      1 /*	$NetBSD: puffs_vfsops.c,v 1.73 2007/12/30 23:04:12 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.73 2007/12/30 23:04:12 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, *nvp;
    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 	/*
    442 	 * Sync all cached data from regular vnodes (which are not
    443 	 * currently locked, see below).  After this we call VFS_SYNC
    444 	 * for the fs server, which should handle data and metadata for
    445 	 * all the nodes it knows to exist.
    446 	 */
    447 	simple_lock(&mntvnode_slock);
    448  loop:
    449 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
    450 		/* check if we're on the right list */
    451 		if (vp->v_mount != mp)
    452 			goto loop;
    453 
    454 		simple_lock(&vp->v_interlock);
    455 		pn = VPTOPP(vp);
    456 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
    457 
    458 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    459 			simple_unlock(&vp->v_interlock);
    460 			continue;
    461 		}
    462 
    463 		simple_unlock(&mntvnode_slock);
    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 		 * NOTE: if we're suspending, vget() does NOT lock.
    481 		 * See puffs_lock() for details.
    482 		 */
    483 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    484 		if (rv) {
    485 			simple_lock(&mntvnode_slock);
    486 			if (rv == ENOENT)
    487 				goto loop;
    488 			continue;
    489 		}
    490 
    491 		/*
    492 		 * Thread information to puffs_strategy() through the
    493 		 * pnode flags: we want to issue the putpages operations
    494 		 * as FAF if we're suspending, since it's very probable
    495 		 * that our execution context is that of the userspace
    496 		 * daemon.  We can do this because:
    497 		 *   + we send the "going to suspend" prior to this part
    498 		 *   + if any of the writes fails in userspace, it's the
    499 		 *     file system server's problem to decide if this was a
    500 		 *     failed snapshot when it gets the "snapshot complete"
    501 		 *     notification.
    502 		 *   + if any of the writes fail in the kernel already, we
    503 		 *     immediately fail *and* notify the user server of
    504 		 *     failure.
    505 		 *
    506 		 * We also do FAFs if we're called from the syncer.  This
    507 		 * is just general optimization for trickle sync: no need
    508 		 * to really guarantee that the stuff ended on backing
    509 		 * storage.
    510 		 * TODO: Maybe also hint the user server of this twist?
    511 		 */
    512 		if (suspending || waitfor == MNT_LAZY) {
    513 			simple_lock(&vp->v_interlock);
    514 			pn->pn_stat |= PNODE_SUSPEND;
    515 			simple_unlock(&vp->v_interlock);
    516 		}
    517 		rv = VOP_FSYNC(vp, cred, waitfor, 0, 0);
    518 		if (suspending || waitfor == MNT_LAZY) {
    519 			simple_lock(&vp->v_interlock);
    520 			pn->pn_stat &= ~PNODE_SUSPEND;
    521 			simple_unlock(&vp->v_interlock);
    522 		}
    523 		if (rv)
    524 			error = rv;
    525 		vput(vp);
    526 		simple_lock(&mntvnode_slock);
    527 	}
    528 	simple_unlock(&mntvnode_slock);
    529 
    530 	return error;
    531 }
    532 
    533 int
    534 puffs_vfsop_sync(struct mount *mp, int waitfor, struct kauth_cred *cred)
    535 {
    536 	PUFFS_MSG_VARS(vfs, sync);
    537 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    538 	int error, rv;
    539 
    540 	error = pageflush(mp, cred, waitfor, 0);
    541 
    542 	/* sync fs */
    543 	PUFFS_MSG_ALLOC(vfs, sync);
    544 	sync_msg->pvfsr_waitfor = waitfor;
    545 	puffs_credcvt(&sync_msg->pvfsr_cred, cred);
    546 	puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL);
    547 
    548 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv);
    549 	rv = checkerr(pmp, rv, __func__);
    550 	if (rv)
    551 		error = rv;
    552 
    553 	PUFFS_MSG_RELEASE(sync);
    554 	return error;
    555 }
    556 
    557 int
    558 puffs_vfsop_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    559 {
    560 	PUFFS_MSG_VARS(vfs, fhtonode);
    561 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    562 	struct vnode *vp;
    563 	void *fhdata;
    564 	size_t argsize, fhlen;
    565 	int error;
    566 
    567 	if (pmp->pmp_args.pa_fhsize == 0)
    568 		return EOPNOTSUPP;
    569 
    570 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    571 		fhlen = fhp->fid_len;
    572 		fhdata = fhp;
    573 	} else {
    574 		fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
    575 		fhdata = fhp->fid_data;
    576 
    577 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    578 			if (pmp->pmp_args.pa_fhsize < fhlen)
    579 				return EINVAL;
    580 		} else {
    581 			if (pmp->pmp_args.pa_fhsize != fhlen)
    582 				return EINVAL;
    583 		}
    584 	}
    585 
    586 	argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
    587 	puffs_msgmem_alloc(argsize, &park_fhtonode, (void **)&fhtonode_msg, 1);
    588 	fhtonode_msg->pvfsr_dsize = fhlen;
    589 	memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
    590 	puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL);
    591 
    592 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error);
    593 	error = checkerr(pmp, error, __func__);
    594 	if (error)
    595 		goto out;
    596 
    597 	error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp);
    598 	DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
    599 	    fhtonode_msg->pvfsr_fhcookie, vp));
    600 	if (error == PUFFS_NOSUCHCOOKIE) {
    601 		error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
    602 		    fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
    603 		    fhtonode_msg->pvfsr_rdev, &vp);
    604 		if (error)
    605 			goto out;
    606 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    607 	} else if (error) {
    608 		goto out;
    609 	}
    610 
    611 	*vpp = vp;
    612  out:
    613 	puffs_msgmem_release(park_fhtonode);
    614 	return error;
    615 }
    616 
    617 int
    618 puffs_vfsop_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    619 {
    620 	PUFFS_MSG_VARS(vfs, nodetofh);
    621 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    622 	size_t argsize, fhlen;
    623 	int error;
    624 
    625 	if (pmp->pmp_args.pa_fhsize == 0)
    626 		return EOPNOTSUPP;
    627 
    628 	/* if file handles are static len, we can test len immediately */
    629 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    630 	    && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
    631 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    632 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    633 		return E2BIG;
    634 	}
    635 
    636 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    637 		fhlen = *fh_size;
    638 	else
    639 		fhlen = PUFFS_FROMFHSIZE(*fh_size);
    640 
    641 	argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
    642 	puffs_msgmem_alloc(argsize, &park_nodetofh, (void **)&nodetofh_msg, 1);
    643 	nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
    644 	nodetofh_msg->pvfsr_dsize = fhlen;
    645 	puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL);
    646 
    647 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error);
    648 	error = checkerr(pmp, error, __func__);
    649 
    650 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    651 		fhlen = nodetofh_msg->pvfsr_dsize;
    652 	else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
    653 		fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
    654 	else
    655 		fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    656 
    657 	if (error) {
    658 		if (error == E2BIG)
    659 			*fh_size = fhlen;
    660 		goto out;
    661 	}
    662 
    663 	if (fhlen > FHANDLE_SIZE_MAX) {
    664 		puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG,
    665 		    "file handle too big", VPTOPNC(vp));
    666 		error = EPROTO;
    667 		goto out;
    668 	}
    669 
    670 	if (*fh_size < fhlen) {
    671 		*fh_size = fhlen;
    672 		error = E2BIG;
    673 		goto out;
    674 	}
    675 	*fh_size = fhlen;
    676 
    677 	if (fhp) {
    678 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    679 			memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
    680 		} else {
    681 			fhp->fid_len = *fh_size;
    682 			memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
    683 			    nodetofh_msg->pvfsr_dsize);
    684 		}
    685 	}
    686 
    687  out:
    688 	puffs_msgmem_release(park_nodetofh);
    689 	return error;
    690 }
    691 
    692 void
    693 puffs_vfsop_init()
    694 {
    695 
    696 	/* some checks depend on this */
    697 	KASSERT(VNOVAL == VSIZENOTSET);
    698 
    699 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    700 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    701 	puffs_msgif_init();
    702 }
    703 
    704 void
    705 puffs_vfsop_done()
    706 {
    707 
    708 	puffs_msgif_destroy();
    709 	pool_destroy(&puffs_pnpool);
    710 }
    711 
    712 int
    713 puffs_vfsop_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    714 {
    715 
    716 	return EOPNOTSUPP;
    717 }
    718 
    719 int
    720 puffs_vfsop_suspendctl(struct mount *mp, int cmd)
    721 {
    722 	PUFFS_MSG_VARS(vfs, suspend);
    723 	struct puffs_mount *pmp;
    724 	int error;
    725 
    726 	pmp = MPTOPUFFSMP(mp);
    727 	switch (cmd) {
    728 	case SUSPEND_SUSPEND:
    729 		DPRINTF(("puffs_suspendctl: suspending\n"));
    730 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
    731 			break;
    732 		PUFFS_MSG_ALLOC(vfs, suspend);
    733 		puffs_msg_setfaf(park_suspend);
    734 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_START;
    735 		puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    736 		    PUFFS_VFS_SUSPEND, NULL);
    737 
    738 		puffs_msg_enqueue(pmp, park_suspend);
    739 		PUFFS_MSG_RELEASE(suspend);
    740 
    741 		error = pageflush(mp, FSCRED, 0, 1);
    742 		if (error == 0)
    743 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
    744 
    745 		if (error != 0) {
    746 			PUFFS_MSG_ALLOC(vfs, suspend);
    747 			puffs_msg_setfaf(park_suspend);
    748 			suspend_msg->pvfsr_status = PUFFS_SUSPEND_ERROR;
    749 			puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    750 			    PUFFS_VFS_SUSPEND, NULL);
    751 
    752 			puffs_msg_enqueue(pmp, park_suspend);
    753 			PUFFS_MSG_RELEASE(suspend);
    754 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    755 			break;
    756 		}
    757 
    758 		PUFFS_MSG_ALLOC(vfs, suspend);
    759 		puffs_msg_setfaf(park_suspend);
    760 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_SUSPENDED;
    761 		puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    762 		    PUFFS_VFS_SUSPEND, NULL);
    763 
    764 		puffs_msg_enqueue(pmp, park_suspend);
    765 		PUFFS_MSG_RELEASE(suspend);
    766 
    767 		break;
    768 
    769 	case SUSPEND_RESUME:
    770 		DPRINTF(("puffs_suspendctl: resume\n"));
    771 		error = 0;
    772 		(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    773 		PUFFS_MSG_ALLOC(vfs, suspend);
    774 		puffs_msg_setfaf(park_suspend);
    775 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_RESUME;
    776 		puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    777 		    PUFFS_VFS_SUSPEND, NULL);
    778 
    779 		puffs_msg_enqueue(pmp, park_suspend);
    780 		PUFFS_MSG_RELEASE(suspend);
    781 		break;
    782 
    783 	default:
    784 		error = EINVAL;
    785 		break;
    786 	}
    787 
    788 	DPRINTF(("puffs_suspendctl: return %d\n", error));
    789 	return error;
    790 }
    791 
    792 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    793 	&puffs_vnodeop_opv_desc,
    794 	&puffs_specop_opv_desc,
    795 	&puffs_fifoop_opv_desc,
    796 	&puffs_msgop_opv_desc,
    797 	NULL,
    798 };
    799 
    800 struct vfsops puffs_vfsops = {
    801 	MOUNT_PUFFS,
    802 	sizeof (struct puffs_kargs),
    803 	puffs_vfsop_mount,		/* mount	*/
    804 	puffs_vfsop_start,		/* start	*/
    805 	puffs_vfsop_unmount,		/* unmount	*/
    806 	puffs_vfsop_root,		/* root		*/
    807 	(void *)eopnotsupp,		/* quotactl	*/
    808 	puffs_vfsop_statvfs,		/* statvfs	*/
    809 	puffs_vfsop_sync,		/* sync		*/
    810 	(void *)eopnotsupp,		/* vget		*/
    811 	puffs_vfsop_fhtovp,		/* fhtovp	*/
    812 	puffs_vfsop_vptofh,		/* vptofh	*/
    813 	puffs_vfsop_init,		/* init		*/
    814 	NULL,				/* reinit	*/
    815 	puffs_vfsop_done,		/* done		*/
    816 	NULL,				/* mountroot	*/
    817 	puffs_vfsop_snapshot,		/* snapshot	*/
    818 	vfs_stdextattrctl,		/* extattrctl	*/
    819 	puffs_vfsop_suspendctl,		/* suspendctl	*/
    820 	puffs_vnodeopv_descs,		/* vnodeops	*/
    821 	0,				/* refcount	*/
    822 	{ NULL, NULL }
    823 };
    824 VFS_ATTACH(puffs_vfsops);
    825