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