Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.53.2.7
      1 /*	$NetBSD: puffs_vfsops.c,v 1.53.2.7 2007/11/27 19:37:49 joerg 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.53.2.7 2007/11/27 19:37:49 joerg 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 
    319 		PUFFS_MSG_ENQUEUEWAIT(pmp, park_unmount, error);
    320 		PUFFS_MSG_RELEASE(unmount);
    321 
    322 		error = checkerr(pmp, error, __func__);
    323 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    324 
    325 		mutex_enter(&pmp->pmp_lock);
    326 		pmp->pmp_unmounting = 0;
    327 		cv_broadcast(&pmp->pmp_unmounting_cv);
    328 	}
    329 
    330 	/*
    331 	 * if userspace cooperated or we really need to die,
    332 	 * screw what userland thinks and just die.
    333 	 */
    334 	if (error == 0 || force) {
    335 		/* tell waiters & other resources to go unwait themselves */
    336 		puffs_userdead(pmp);
    337 		putter_detach(pmp->pmp_pi);
    338 
    339 		/*
    340 		 * Wait until there are no more users for the mount resource.
    341 		 * Notice that this is hooked against transport_close
    342 		 * and return from touser.  In an ideal world, it would
    343 		 * be hooked against final return from all operations.
    344 		 * But currently it works well enough, since nobody
    345 		 * does weird blocking voodoo after return from touser().
    346 		 */
    347 		while (pmp->pmp_refcount != 0)
    348 			cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
    349 		mutex_exit(&pmp->pmp_lock);
    350 
    351 		/* free resources now that we hopefully have no waiters left */
    352 		cv_destroy(&pmp->pmp_unmounting_cv);
    353 		cv_destroy(&pmp->pmp_refcount_cv);
    354 		cv_destroy(&pmp->pmp_msg_waiter_cv);
    355 		mutex_destroy(&pmp->pmp_lock);
    356 
    357 		kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash));
    358 		kmem_free(pmp, sizeof(struct puffs_mount));
    359 		error = 0;
    360 	} else {
    361 		mutex_exit(&pmp->pmp_lock);
    362 	}
    363 
    364  out:
    365 	DPRINTF(("puffs_unmount: return %d\n", error));
    366 	return error;
    367 }
    368 
    369 /*
    370  * This doesn't need to travel to userspace
    371  */
    372 int
    373 puffs_root(struct mount *mp, struct vnode **vpp)
    374 {
    375 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    376 	int rv;
    377 
    378 	rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, 1, 1, vpp);
    379 	KASSERT(rv != PUFFS_NOSUCHCOOKIE);
    380 	return rv;
    381 }
    382 
    383 int
    384 puffs_statvfs(struct mount *mp, struct statvfs *sbp)
    385 {
    386 	PUFFS_MSG_VARS(vfs, statvfs);
    387 	struct puffs_mount *pmp;
    388 	int error = 0;
    389 
    390 	pmp = MPTOPUFFSMP(mp);
    391 
    392 	/*
    393 	 * If we are mounting, it means that the userspace counterpart
    394 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    395 	 * requesting statvfs from userspace would mean a deadlock.
    396 	 * Compensate.
    397 	 */
    398 	if (pmp->pmp_status == PUFFSTAT_MOUNTING)
    399 		return EINPROGRESS;
    400 
    401 	PUFFS_MSG_ALLOC(vfs, statvfs);
    402 	puffs_msg_setinfo(park_statvfs, PUFFSOP_VFS, PUFFS_VFS_STATVFS, NULL);
    403 
    404 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_statvfs, error);
    405 	error = checkerr(pmp, error, __func__);
    406 	statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
    407 
    408 	/*
    409 	 * Try to produce a sensible result even in the event
    410 	 * of userspace error.
    411 	 *
    412 	 * XXX: cache the copy in non-error case
    413 	 */
    414 	if (!error) {
    415 		copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
    416 		(void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
    417 		    sizeof(struct statvfs));
    418 	} else {
    419 		copy_statvfs_info(sbp, mp);
    420 	}
    421 
    422 	PUFFS_MSG_RELEASE(statvfs);
    423 	return error;
    424 }
    425 
    426 static int
    427 pageflush(struct mount *mp, kauth_cred_t cred, int waitfor, int suspending)
    428 {
    429 	struct puffs_node *pn;
    430 	struct vnode *vp, *nvp;
    431 	int error, rv;
    432 
    433 	KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
    434 	KASSERT((suspending == 0)
    435 	    || (fstrans_is_owner(mp)
    436 	      && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
    437 
    438 	error = 0;
    439 
    440 	/*
    441 	 * Sync all cached data from regular vnodes (which are not
    442 	 * currently locked, see below).  After this we call VFS_SYNC
    443 	 * for the fs server, which should handle data and metadata for
    444 	 * all the nodes it knows to exist.
    445 	 */
    446 	simple_lock(&mntvnode_slock);
    447  loop:
    448 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
    449 		/* check if we're on the right list */
    450 		if (vp->v_mount != mp)
    451 			goto loop;
    452 
    453 		simple_lock(&vp->v_interlock);
    454 		pn = VPTOPP(vp);
    455 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
    456 
    457 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    458 			simple_unlock(&vp->v_interlock);
    459 			continue;
    460 		}
    461 
    462 		simple_unlock(&mntvnode_slock);
    463 
    464 		/*
    465 		 * Here we try to get a reference to the vnode and to
    466 		 * lock it.  This is mostly cargo-culted, but I will
    467 		 * offer an explanation to why I believe this might
    468 		 * actually do the right thing.
    469 		 *
    470 		 * If the vnode is a goner, we quite obviously don't need
    471 		 * to sync it.
    472 		 *
    473 		 * If the vnode was busy, we don't need to sync it because
    474 		 * this is never called with MNT_WAIT except from
    475 		 * dounmount(), when we are wait-flushing all the dirty
    476 		 * vnodes through other routes in any case.  So there,
    477 		 * sync() doesn't actually sync.  Happy now?
    478 		 *
    479 		 * NOTE: if we're suspending, vget() does NOT lock.
    480 		 * See puffs_lock() for details.
    481 		 */
    482 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    483 		if (rv) {
    484 			simple_lock(&mntvnode_slock);
    485 			if (rv == ENOENT)
    486 				goto loop;
    487 			continue;
    488 		}
    489 
    490 		/*
    491 		 * Thread information to puffs_strategy() through the
    492 		 * pnode flags: we want to issue the putpages operations
    493 		 * as FAF if we're suspending, since it's very probable
    494 		 * that our execution context is that of the userspace
    495 		 * daemon.  We can do this because:
    496 		 *   + we send the "going to suspend" prior to this part
    497 		 *   + if any of the writes fails in userspace, it's the
    498 		 *     file system server's problem to decide if this was a
    499 		 *     failed snapshot when it gets the "snapshot complete"
    500 		 *     notification.
    501 		 *   + if any of the writes fail in the kernel already, we
    502 		 *     immediately fail *and* notify the user server of
    503 		 *     failure.
    504 		 *
    505 		 * We also do FAFs if we're called from the syncer.  This
    506 		 * is just general optimization for trickle sync: no need
    507 		 * to really guarantee that the stuff ended on backing
    508 		 * storage.
    509 		 * TODO: Maybe also hint the user server of this twist?
    510 		 */
    511 		if (suspending || waitfor == MNT_LAZY) {
    512 			simple_lock(&vp->v_interlock);
    513 			pn->pn_stat |= PNODE_SUSPEND;
    514 			simple_unlock(&vp->v_interlock);
    515 		}
    516 		rv = VOP_FSYNC(vp, cred, waitfor, 0, 0);
    517 		if (suspending || waitfor == MNT_LAZY) {
    518 			simple_lock(&vp->v_interlock);
    519 			pn->pn_stat &= ~PNODE_SUSPEND;
    520 			simple_unlock(&vp->v_interlock);
    521 		}
    522 		if (rv)
    523 			error = rv;
    524 		vput(vp);
    525 		simple_lock(&mntvnode_slock);
    526 	}
    527 	simple_unlock(&mntvnode_slock);
    528 
    529 	return error;
    530 }
    531 
    532 int
    533 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred)
    534 {
    535 	PUFFS_MSG_VARS(vfs, sync);
    536 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    537 	int error, rv;
    538 
    539 	error = pageflush(mp, cred, waitfor, 0);
    540 
    541 	/* sync fs */
    542 	PUFFS_MSG_ALLOC(vfs, sync);
    543 	sync_msg->pvfsr_waitfor = waitfor;
    544 	puffs_credcvt(&sync_msg->pvfsr_cred, cred);
    545 	puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL);
    546 
    547 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv);
    548 	rv = checkerr(pmp, rv, __func__);
    549 	if (rv)
    550 		error = rv;
    551 
    552 	PUFFS_MSG_RELEASE(sync);
    553 	return error;
    554 }
    555 
    556 int
    557 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    558 {
    559 	PUFFS_MSG_VARS(vfs, fhtonode);
    560 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    561 	struct vnode *vp;
    562 	void *fhdata;
    563 	size_t argsize, fhlen;
    564 	int error;
    565 
    566 	if (pmp->pmp_args.pa_fhsize == 0)
    567 		return EOPNOTSUPP;
    568 
    569 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    570 		fhlen = fhp->fid_len;
    571 		fhdata = fhp;
    572 	} else {
    573 		fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
    574 		fhdata = fhp->fid_data;
    575 
    576 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    577 			if (pmp->pmp_args.pa_fhsize < fhlen)
    578 				return EINVAL;
    579 		} else {
    580 			if (pmp->pmp_args.pa_fhsize != fhlen)
    581 				return EINVAL;
    582 		}
    583 	}
    584 
    585 	argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
    586 	puffs_msgmem_alloc(argsize, &park_fhtonode, (void **)&fhtonode_msg, 1);
    587 	fhtonode_msg->pvfsr_dsize = fhlen;
    588 	memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
    589 	puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL);
    590 
    591 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error);
    592 	error = checkerr(pmp, error, __func__);
    593 	if (error)
    594 		goto out;
    595 
    596 	error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp);
    597 	DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
    598 	    fhtonode_msg->pvfsr_fhcookie, vp));
    599 	if (error == PUFFS_NOSUCHCOOKIE) {
    600 		error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
    601 		    fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
    602 		    fhtonode_msg->pvfsr_rdev, &vp);
    603 		if (error)
    604 			goto out;
    605 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    606 	} else if (error) {
    607 		goto out;
    608 	}
    609 
    610 	*vpp = vp;
    611  out:
    612 	puffs_msgmem_release(park_fhtonode);
    613 	return error;
    614 }
    615 
    616 int
    617 puffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    618 {
    619 	PUFFS_MSG_VARS(vfs, nodetofh);
    620 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    621 	size_t argsize, fhlen;
    622 	int error;
    623 
    624 	if (pmp->pmp_args.pa_fhsize == 0)
    625 		return EOPNOTSUPP;
    626 
    627 	/* if file handles are static len, we can test len immediately */
    628 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    629 	    && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
    630 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    631 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    632 		return E2BIG;
    633 	}
    634 
    635 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    636 		fhlen = *fh_size;
    637 	else
    638 		fhlen = PUFFS_FROMFHSIZE(*fh_size);
    639 
    640 	argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
    641 	puffs_msgmem_alloc(argsize, &park_nodetofh, (void **)&nodetofh_msg, 1);
    642 	nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
    643 	nodetofh_msg->pvfsr_dsize = fhlen;
    644 	puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL);
    645 
    646 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error);
    647 	error = checkerr(pmp, error, __func__);
    648 
    649 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    650 		fhlen = nodetofh_msg->pvfsr_dsize;
    651 	else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
    652 		fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
    653 	else
    654 		fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    655 
    656 	if (error) {
    657 		if (error == E2BIG)
    658 			*fh_size = fhlen;
    659 		goto out;
    660 	}
    661 
    662 	if (fhlen > FHANDLE_SIZE_MAX) {
    663 		puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG,
    664 		    "file handle too big", VPTOPNC(vp));
    665 		error = EPROTO;
    666 		goto out;
    667 	}
    668 
    669 	if (*fh_size < fhlen) {
    670 		*fh_size = fhlen;
    671 		error = E2BIG;
    672 		goto out;
    673 	}
    674 	*fh_size = fhlen;
    675 
    676 	if (fhp) {
    677 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    678 			memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
    679 		} else {
    680 			fhp->fid_len = *fh_size;
    681 			memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
    682 			    nodetofh_msg->pvfsr_dsize);
    683 		}
    684 	}
    685 
    686  out:
    687 	puffs_msgmem_release(park_nodetofh);
    688 	return error;
    689 }
    690 
    691 void
    692 puffs_init()
    693 {
    694 
    695 	/* some checks depend on this */
    696 	KASSERT(VNOVAL == VSIZENOTSET);
    697 
    698 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    699 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    700 	puffs_msgif_init();
    701 }
    702 
    703 void
    704 puffs_done()
    705 {
    706 
    707 	puffs_msgif_destroy();
    708 	pool_destroy(&puffs_pnpool);
    709 }
    710 
    711 int
    712 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    713 {
    714 
    715 	return EOPNOTSUPP;
    716 }
    717 
    718 int
    719 puffs_suspendctl(struct mount *mp, int cmd)
    720 {
    721 	PUFFS_MSG_VARS(vfs, suspend);
    722 	struct puffs_mount *pmp;
    723 	int error;
    724 
    725 	pmp = MPTOPUFFSMP(mp);
    726 	switch (cmd) {
    727 	case SUSPEND_SUSPEND:
    728 		DPRINTF(("puffs_suspendctl: suspending\n"));
    729 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
    730 			break;
    731 		PUFFS_MSG_ALLOC(vfs, suspend);
    732 		puffs_msg_setfaf(park_suspend);
    733 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_START;
    734 		puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    735 		    PUFFS_VFS_SUSPEND, NULL);
    736 
    737 		puffs_msg_enqueue(pmp, park_suspend);
    738 		PUFFS_MSG_RELEASE(suspend);
    739 
    740 		error = pageflush(mp, FSCRED, 0, 1);
    741 		if (error == 0)
    742 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
    743 
    744 		if (error != 0) {
    745 			PUFFS_MSG_ALLOC(vfs, suspend);
    746 			puffs_msg_setfaf(park_suspend);
    747 			suspend_msg->pvfsr_status = PUFFS_SUSPEND_ERROR;
    748 			puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    749 			    PUFFS_VFS_SUSPEND, NULL);
    750 
    751 			puffs_msg_enqueue(pmp, park_suspend);
    752 			PUFFS_MSG_RELEASE(suspend);
    753 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    754 			break;
    755 		}
    756 
    757 		PUFFS_MSG_ALLOC(vfs, suspend);
    758 		puffs_msg_setfaf(park_suspend);
    759 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_SUSPENDED;
    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 
    766 		break;
    767 
    768 	case SUSPEND_RESUME:
    769 		DPRINTF(("puffs_suspendctl: resume\n"));
    770 		error = 0;
    771 		(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    772 		PUFFS_MSG_ALLOC(vfs, suspend);
    773 		puffs_msg_setfaf(park_suspend);
    774 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_RESUME;
    775 		puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    776 		    PUFFS_VFS_SUSPEND, NULL);
    777 
    778 		puffs_msg_enqueue(pmp, park_suspend);
    779 		PUFFS_MSG_RELEASE(suspend);
    780 		break;
    781 
    782 	default:
    783 		error = EINVAL;
    784 		break;
    785 	}
    786 
    787 	DPRINTF(("puffs_suspendctl: return %d\n", error));
    788 	return error;
    789 }
    790 
    791 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    792 	&puffs_vnodeop_opv_desc,
    793 	&puffs_specop_opv_desc,
    794 	&puffs_fifoop_opv_desc,
    795 	&puffs_msgop_opv_desc,
    796 	NULL,
    797 };
    798 
    799 struct vfsops puffs_vfsops = {
    800 	MOUNT_PUFFS,
    801 	sizeof (struct puffs_kargs),
    802 	puffs_mount,		/* mount	*/
    803 	puffs_start,		/* start	*/
    804 	puffs_unmount,		/* unmount	*/
    805 	puffs_root,		/* root		*/
    806 	(void *)eopnotsupp,	/* quotactl	*/
    807 	puffs_statvfs,		/* statvfs	*/
    808 	puffs_sync,		/* sync		*/
    809 	(void *)eopnotsupp,	/* vget		*/
    810 	puffs_fhtovp,		/* fhtovp	*/
    811 	puffs_vptofh,		/* vptofh	*/
    812 	puffs_init,		/* init		*/
    813 	NULL,			/* reinit	*/
    814 	puffs_done,		/* done		*/
    815 	NULL,			/* mountroot	*/
    816 	puffs_snapshot,		/* snapshot	*/
    817 	vfs_stdextattrctl,	/* extattrctl	*/
    818 	puffs_suspendctl,	/* suspendctl	*/
    819 	puffs_vnodeopv_descs,	/* vnodeops	*/
    820 	0,			/* refcount	*/
    821 	{ NULL, NULL }
    822 };
    823 VFS_ATTACH(puffs_vfsops);
    824