Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.66
      1 /*	$NetBSD: puffs_vfsops.c,v 1.66 2007/11/10 21:45:08 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.66 2007/11/10 21:45:08 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/puttervar.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 MALLOC_JUSTDEFINE(M_PUFFS, "puffs", "Pass-to-Userspace Framework File System");
     58 
     59 #ifndef PUFFS_PNODEBUCKETS
     60 #define PUFFS_PNODEBUCKETS 256
     61 #endif
     62 #ifndef PUFFS_MAXPNODEBUCKETS
     63 #define PUFFS_MAXPNODEBUCKETS 8192
     64 #endif
     65 int puffs_pnodebuckets_default = PUFFS_PNODEBUCKETS;
     66 int puffs_maxpnodebuckets = PUFFS_MAXPNODEBUCKETS;
     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 	    struct lwp *l)
     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 = l->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 	MALLOC(args, struct puffs_kargs *, sizeof(struct puffs_kargs),
    108 	    M_PUFFS, M_WAITOK);
    109 
    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, l);
    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 	MALLOC(pmp, struct puffs_mount *, sizeof(struct puffs_mount),
    217 	    M_PUFFS, M_WAITOK | M_ZERO);
    218 
    219 	mp->mnt_fs_bshift = DEV_BSHIFT;
    220 	mp->mnt_dev_bshift = DEV_BSHIFT;
    221 	mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
    222 	mp->mnt_data = pmp;
    223 	mp->mnt_iflag |= IMNT_HAS_TRANS;
    224 
    225 	pmp->pmp_status = PUFFSTAT_MOUNTING;
    226 	pmp->pmp_mp = mp;
    227 	pmp->pmp_msg_maxsize = args->pa_maxmsglen;
    228 	pmp->pmp_args = *args;
    229 
    230 	pmp->pmp_npnodehash = args->pa_nhashbuckets;
    231 	pmp->pmp_pnodehash = malloc
    232 	    (sizeof(struct puffs_pnode_hashlist *) * pmp->pmp_npnodehash,
    233 	    M_PUFFS, M_WAITOK);
    234 	for (i = 0; i < pmp->pmp_npnodehash; i++)
    235 		LIST_INIT(&pmp->pmp_pnodehash[i]);
    236 	LIST_INIT(&pmp->pmp_newcookie);
    237 
    238 	/*
    239 	 * Inform the fileops processing code that we have a mountpoint.
    240 	 * If it doesn't know about anyone with our pid/fd having the
    241 	 * device open, punt
    242 	 */
    243 	if ((pmp->pmp_pi
    244 	    = putter_attach(mntpid, args->pa_fd, pmp, &puffs_putter)) == NULL) {
    245 		error = ENOENT;
    246 		goto out;
    247 	}
    248 
    249 	/* XXX: check parameters */
    250 	pmp->pmp_root_cookie = args->pa_root_cookie;
    251 	pmp->pmp_root_vtype = args->pa_root_vtype;
    252 	pmp->pmp_root_vsize = args->pa_root_vsize;
    253 	pmp->pmp_root_rdev = args->pa_root_rdev;
    254 
    255 	mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
    256 	cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
    257 	cv_init(&pmp->pmp_refcount_cv, "puffsref");
    258 	cv_init(&pmp->pmp_unmounting_cv, "puffsum");
    259 	TAILQ_INIT(&pmp->pmp_msg_touser);
    260 	TAILQ_INIT(&pmp->pmp_msg_replywait);
    261 
    262 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    263 	    mp, MPTOPUFFSMP(mp)));
    264 
    265 	vfs_getnewfsid(mp);
    266 
    267  out:
    268 	if (error && pmp && pmp->pmp_pnodehash)
    269 		free(pmp->pmp_pnodehash, M_PUFFS);
    270 	if (error && pmp)
    271 		FREE(pmp, M_PUFFS);
    272 	FREE(args, M_PUFFS);
    273 	return error;
    274 }
    275 
    276 int
    277 puffs_start(struct mount *mp, int flags, struct lwp *l)
    278 {
    279 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    280 
    281 	KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
    282 	pmp->pmp_status = PUFFSTAT_RUNNING;
    283 
    284 	return 0;
    285 }
    286 
    287 int
    288 puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    289 {
    290 	PUFFS_MSG_VARS(vfs, unmount);
    291 	struct puffs_mount *pmp;
    292 	int error, force;
    293 
    294 	error = 0;
    295 	force = mntflags & MNT_FORCE;
    296 	pmp = MPTOPUFFSMP(mp);
    297 
    298 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    299 	    "status 0x%x\n", pmp->pmp_status));
    300 
    301 	/*
    302 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    303 	 * root vnode does not get flushed until unmount.  The
    304 	 * userspace root node cookie is stored in the mount
    305 	 * structure, so we can always re-instantiate a root vnode,
    306 	 * should userspace unmount decide it doesn't want to
    307 	 * cooperate.
    308 	 */
    309 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    310 	if (error)
    311 		goto out;
    312 
    313 	/*
    314 	 * If we are not DYING, we should ask userspace's opinion
    315 	 * about the situation
    316 	 */
    317 	mutex_enter(&pmp->pmp_lock);
    318 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    319 		pmp->pmp_unmounting = 1;
    320 		mutex_exit(&pmp->pmp_lock);
    321 
    322 		PUFFS_MSG_ALLOC(vfs, unmount);
    323 		unmount_msg->pvfsr_flags = mntflags;
    324 		puffs_cidcvt(&unmount_msg->pvfsr_cid, l);
    325 
    326 		error = puffs_msg_vfs(pmp, park_unmount, PUFFS_VFS_UNMOUNT);
    327 		PUFFS_MSG_RELEASE(unmount);
    328 
    329 		error = checkerr(pmp, error, __func__);
    330 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    331 
    332 		mutex_enter(&pmp->pmp_lock);
    333 		pmp->pmp_unmounting = 0;
    334 		cv_broadcast(&pmp->pmp_unmounting_cv);
    335 	}
    336 
    337 	/*
    338 	 * if userspace cooperated or we really need to die,
    339 	 * screw what userland thinks and just die.
    340 	 */
    341 	if (error == 0 || force) {
    342 		/* tell waiters & other resources to go unwait themselves */
    343 		puffs_userdead(pmp);
    344 		putter_detach(pmp->pmp_pi);
    345 
    346 		/*
    347 		 * Wait until there are no more users for the mount resource.
    348 		 * Notice that this is hooked against transport_close
    349 		 * and return from touser.  In an ideal world, it would
    350 		 * be hooked against final return from all operations.
    351 		 * But currently it works well enough, since nobody
    352 		 * does weird blocking voodoo after return from touser().
    353 		 */
    354 		while (pmp->pmp_refcount != 0)
    355 			cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
    356 		mutex_exit(&pmp->pmp_lock);
    357 
    358 		/* free resources now that we hopefully have no waiters left */
    359 		cv_destroy(&pmp->pmp_unmounting_cv);
    360 		cv_destroy(&pmp->pmp_refcount_cv);
    361 		cv_destroy(&pmp->pmp_msg_waiter_cv);
    362 		mutex_destroy(&pmp->pmp_lock);
    363 
    364 		free(pmp->pmp_pnodehash, M_PUFFS);
    365 		FREE(pmp, M_PUFFS);
    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_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_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    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_cidcvt(&statvfs_msg->pvfsr_cid, l);
    410 
    411 	error = puffs_msg_vfs(pmp, park_statvfs, PUFFS_VFS_STATVFS);
    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,
    435 	int waitfor, int suspending, struct lwp *l)
    436 {
    437 	struct puffs_node *pn;
    438 	struct vnode *vp, *nvp;
    439 	int error, rv;
    440 
    441 	KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
    442 	KASSERT((suspending == 0)
    443 	    || (fstrans_is_owner(mp)
    444 	      && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
    445 
    446 	error = 0;
    447 
    448 	/*
    449 	 * Sync all cached data from regular vnodes (which are not
    450 	 * currently locked, see below).  After this we call VFS_SYNC
    451 	 * for the fs server, which should handle data and metadata for
    452 	 * all the nodes it knows to exist.
    453 	 */
    454 	simple_lock(&mntvnode_slock);
    455  loop:
    456 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
    457 		/* check if we're on the right list */
    458 		if (vp->v_mount != mp)
    459 			goto loop;
    460 
    461 		simple_lock(&vp->v_interlock);
    462 		pn = VPTOPP(vp);
    463 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
    464 
    465 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    466 			simple_unlock(&vp->v_interlock);
    467 			continue;
    468 		}
    469 
    470 		simple_unlock(&mntvnode_slock);
    471 
    472 		/*
    473 		 * Here we try to get a reference to the vnode and to
    474 		 * lock it.  This is mostly cargo-culted, but I will
    475 		 * offer an explanation to why I believe this might
    476 		 * actually do the right thing.
    477 		 *
    478 		 * If the vnode is a goner, we quite obviously don't need
    479 		 * to sync it.
    480 		 *
    481 		 * If the vnode was busy, we don't need to sync it because
    482 		 * this is never called with MNT_WAIT except from
    483 		 * dounmount(), when we are wait-flushing all the dirty
    484 		 * vnodes through other routes in any case.  So there,
    485 		 * sync() doesn't actually sync.  Happy now?
    486 		 *
    487 		 * NOTE: if we're suspending, vget() does NOT lock.
    488 		 * See puffs_lock() for details.
    489 		 */
    490 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    491 		if (rv) {
    492 			simple_lock(&mntvnode_slock);
    493 			if (rv == ENOENT)
    494 				goto loop;
    495 			continue;
    496 		}
    497 
    498 		/*
    499 		 * Thread information to puffs_strategy() through the
    500 		 * pnode flags: we want to issue the putpages operations
    501 		 * as FAF if we're suspending, since it's very probable
    502 		 * that our execution context is that of the userspace
    503 		 * daemon.  We can do this because:
    504 		 *   + we send the "going to suspend" prior to this part
    505 		 *   + if any of the writes fails in userspace, it's the
    506 		 *     file system server's problem to decide if this was a
    507 		 *     failed snapshot when it gets the "snapshot complete"
    508 		 *     notification.
    509 		 *   + if any of the writes fail in the kernel already, we
    510 		 *     immediately fail *and* notify the user server of
    511 		 *     failure.
    512 		 *
    513 		 * We also do FAFs if we're called from the syncer.  This
    514 		 * is just general optimization for trickle sync: no need
    515 		 * to really guarantee that the stuff ended on backing
    516 		 * storage.
    517 		 * TODO: Maybe also hint the user server of this twist?
    518 		 */
    519 		if (suspending || waitfor == MNT_LAZY) {
    520 			simple_lock(&vp->v_interlock);
    521 			pn->pn_stat |= PNODE_SUSPEND;
    522 			simple_unlock(&vp->v_interlock);
    523 		}
    524 		rv = VOP_FSYNC(vp, cred, waitfor, 0, 0, l);
    525 		if (suspending || waitfor == MNT_LAZY) {
    526 			simple_lock(&vp->v_interlock);
    527 			pn->pn_stat &= ~PNODE_SUSPEND;
    528 			simple_unlock(&vp->v_interlock);
    529 		}
    530 		if (rv)
    531 			error = rv;
    532 		vput(vp);
    533 		simple_lock(&mntvnode_slock);
    534 	}
    535 	simple_unlock(&mntvnode_slock);
    536 
    537 	return error;
    538 }
    539 
    540 int
    541 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
    542 	struct lwp *l)
    543 {
    544 	PUFFS_MSG_VARS(vfs, sync);
    545 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    546 	int error, rv;
    547 
    548 	error = pageflush(mp, cred, waitfor, 0, l);
    549 
    550 	/* sync fs */
    551 	PUFFS_MSG_ALLOC(vfs, sync);
    552 	sync_msg->pvfsr_waitfor = waitfor;
    553 	puffs_credcvt(&sync_msg->pvfsr_cred, cred);
    554 	puffs_cidcvt(&sync_msg->pvfsr_cid, l);
    555 
    556 	rv = puffs_msg_vfs(pmp, park_sync, PUFFS_VFS_SYNC);
    557 	rv = checkerr(pmp, rv, __func__);
    558 	if (rv)
    559 		error = rv;
    560 
    561 	PUFFS_MSG_RELEASE(sync);
    562 	return error;
    563 }
    564 
    565 int
    566 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    567 {
    568 	PUFFS_MSG_VARS(vfs, fhtonode);
    569 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    570 	struct vnode *vp;
    571 	void *fhdata;
    572 	size_t argsize, fhlen;
    573 	int error;
    574 
    575 	if (pmp->pmp_args.pa_fhsize == 0)
    576 		return EOPNOTSUPP;
    577 
    578 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    579 		fhlen = fhp->fid_len;
    580 		fhdata = fhp;
    581 	} else {
    582 		fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
    583 		fhdata = fhp->fid_data;
    584 
    585 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    586 			if (pmp->pmp_args.pa_fhsize < fhlen)
    587 				return EINVAL;
    588 		} else {
    589 			if (pmp->pmp_args.pa_fhsize != fhlen)
    590 				return EINVAL;
    591 		}
    592 	}
    593 
    594 	argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
    595 	puffs_msgmem_alloc(argsize, &park_fhtonode, (void **)&fhtonode_msg, 1);
    596 	fhtonode_msg->pvfsr_dsize = fhlen;
    597 	memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
    598 
    599 	error = puffs_msg_vfs(pmp, park_fhtonode, PUFFS_VFS_FHTOVP);
    600 	error = checkerr(pmp, error, __func__);
    601 	if (error)
    602 		goto out;
    603 
    604 	error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp);
    605 	DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
    606 	    fhtonode_msg->pvfsr_fhcookie, vp));
    607 	if (error == PUFFS_NOSUCHCOOKIE) {
    608 		error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
    609 		    fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
    610 		    fhtonode_msg->pvfsr_rdev, &vp);
    611 		if (error)
    612 			goto out;
    613 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    614 	} else if (error) {
    615 		goto out;
    616 	}
    617 
    618 	*vpp = vp;
    619  out:
    620 	puffs_msgmem_release(park_fhtonode);
    621 	return error;
    622 }
    623 
    624 int
    625 puffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    626 {
    627 	PUFFS_MSG_VARS(vfs, nodetofh);
    628 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    629 	size_t argsize, fhlen;
    630 	int error;
    631 
    632 	if (pmp->pmp_args.pa_fhsize == 0)
    633 		return EOPNOTSUPP;
    634 
    635 	/* if file handles are static len, we can test len immediately */
    636 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    637 	    && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
    638 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    639 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    640 		return E2BIG;
    641 	}
    642 
    643 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    644 		fhlen = *fh_size;
    645 	else
    646 		fhlen = PUFFS_FROMFHSIZE(*fh_size);
    647 
    648 	argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
    649 	puffs_msgmem_alloc(argsize, &park_nodetofh, (void **)&nodetofh_msg, 1);
    650 	nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
    651 	nodetofh_msg->pvfsr_dsize = fhlen;
    652 
    653 	error = puffs_msg_vfs(pmp, park_nodetofh, PUFFS_VFS_VPTOFH);
    654 	error = checkerr(pmp, error, __func__);
    655 
    656 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    657 		fhlen = nodetofh_msg->pvfsr_dsize;
    658 	else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
    659 		fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
    660 	else
    661 		fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    662 
    663 	if (error) {
    664 		if (error == E2BIG)
    665 			*fh_size = fhlen;
    666 		goto out;
    667 	}
    668 
    669 	if (fhlen > FHANDLE_SIZE_MAX) {
    670 		puffs_msg_errnotify(pmp, PUFFS_ERR_VPTOFH, E2BIG,
    671 		    "file handle too big", VPTOPNC(vp));
    672 		error = EPROTO;
    673 		goto out;
    674 	}
    675 
    676 	if (*fh_size < fhlen) {
    677 		*fh_size = fhlen;
    678 		error = E2BIG;
    679 		goto out;
    680 	}
    681 	*fh_size = fhlen;
    682 
    683 	if (fhp) {
    684 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    685 			memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
    686 		} else {
    687 			fhp->fid_len = *fh_size;
    688 			memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
    689 			    nodetofh_msg->pvfsr_dsize);
    690 		}
    691 	}
    692 
    693  out:
    694 	puffs_msgmem_release(park_nodetofh);
    695 	return error;
    696 }
    697 
    698 void
    699 puffs_init()
    700 {
    701 
    702 	/* some checks depend on this */
    703 	KASSERT(VNOVAL == VSIZENOTSET);
    704 
    705 	malloc_type_attach(M_PUFFS);
    706 
    707 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    708 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    709 	puffs_msgif_init();
    710 }
    711 
    712 void
    713 puffs_done()
    714 {
    715 
    716 	puffs_msgif_destroy();
    717 	pool_destroy(&puffs_pnpool);
    718 
    719 	malloc_type_detach(M_PUFFS);
    720 }
    721 
    722 int
    723 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    724 {
    725 
    726 	return EOPNOTSUPP;
    727 }
    728 
    729 int
    730 puffs_suspendctl(struct mount *mp, int cmd)
    731 {
    732 	PUFFS_MSG_VARS(vfs, suspend);
    733 	struct puffs_mount *pmp;
    734 	int error;
    735 
    736 	pmp = MPTOPUFFSMP(mp);
    737 	switch (cmd) {
    738 	case SUSPEND_SUSPEND:
    739 		DPRINTF(("puffs_suspendctl: suspending\n"));
    740 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
    741 			break;
    742 		PUFFS_MSG_ALLOC(vfs, suspend);
    743 		puffs_msg_setfaf(park_suspend);
    744 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_START;
    745 		puffs_msg_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    746 		PUFFS_MSG_RELEASE(suspend);
    747 
    748 		error = pageflush(mp, FSCRED, 0, 1, curlwp);
    749 		if (error == 0)
    750 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
    751 
    752 		if (error != 0) {
    753 			PUFFS_MSG_ALLOC(vfs, suspend);
    754 			puffs_msg_setfaf(park_suspend);
    755 			suspend_msg->pvfsr_status = PUFFS_SUSPEND_ERROR;
    756 			puffs_msg_vfs(pmp, park_suspend, PUFFS_VFS_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_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    766 		PUFFS_MSG_RELEASE(suspend);
    767 
    768 		break;
    769 
    770 	case SUSPEND_RESUME:
    771 		DPRINTF(("puffs_suspendctl: resume\n"));
    772 		error = 0;
    773 		(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    774 		PUFFS_MSG_ALLOC(vfs, suspend);
    775 		puffs_msg_setfaf(park_suspend);
    776 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_RESUME;
    777 		puffs_msg_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    778 		PUFFS_MSG_RELEASE(suspend);
    779 		break;
    780 
    781 	default:
    782 		error = EINVAL;
    783 		break;
    784 	}
    785 
    786 	DPRINTF(("puffs_suspendctl: return %d\n", error));
    787 	return error;
    788 }
    789 
    790 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    791 	&puffs_vnodeop_opv_desc,
    792 	&puffs_specop_opv_desc,
    793 	&puffs_fifoop_opv_desc,
    794 	&puffs_msgop_opv_desc,
    795 	NULL,
    796 };
    797 
    798 struct vfsops puffs_vfsops = {
    799 	MOUNT_PUFFS,
    800 	sizeof (struct puffs_kargs),
    801 	puffs_mount,		/* mount	*/
    802 	puffs_start,		/* start	*/
    803 	puffs_unmount,		/* unmount	*/
    804 	puffs_root,		/* root		*/
    805 	(void *)eopnotsupp,	/* quotactl	*/
    806 	puffs_statvfs,		/* statvfs	*/
    807 	puffs_sync,		/* sync		*/
    808 	(void *)eopnotsupp,	/* vget		*/
    809 	puffs_fhtovp,		/* fhtovp	*/
    810 	puffs_vptofh,		/* vptofh	*/
    811 	puffs_init,		/* init		*/
    812 	NULL,			/* reinit	*/
    813 	puffs_done,		/* done		*/
    814 	NULL,			/* mountroot	*/
    815 	puffs_snapshot,		/* snapshot	*/
    816 	vfs_stdextattrctl,	/* extattrctl	*/
    817 	puffs_suspendctl,	/* suspendctl	*/
    818 	puffs_vnodeopv_descs,	/* vnodeops	*/
    819 	0,			/* refcount	*/
    820 	{ NULL, NULL }
    821 };
    822 VFS_ATTACH(puffs_vfsops);
    823