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