puffs_vfsops.c revision 1.28.6.12       1 /*	$NetBSD: puffs_vfsops.c,v 1.28.6.12 2007/10/28 22:01:25 ad 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.28.6.12 2007/10/28 22:01:25 ad 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: "
    104 		    "kernel %d, lib %d\n",
    105 		    PUFFSVERSION, args->pa_vers & ~PUFFSDEVELVERS);
    106 		error = EINVAL;
    107 		goto out;
    108 	}
    109 
    110 	if ((args->pa_flags & ~PUFFS_KFLAG_MASK) != 0) {
    111 		printf("puffs_mount: invalid KFLAGs 0x%x\n", args->pa_flags);
    112 		error = EINVAL;
    113 		goto out;
    114 	}
    115 	if ((args->pa_fhflags & ~PUFFS_FHFLAG_MASK) != 0) {
    116 		printf("puffs_mount: invalid FHFLAGs 0x%x\n", args->pa_fhflags);
    117 		error = EINVAL;
    118 		goto out;
    119 	}
    120 
    121 	/* use dummy value for passthrough */
    122 	if (args->pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    123 		args->pa_fhsize = sizeof(struct fid);
    124 
    125 	/* sanitize file handle length */
    126 	if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) {
    127 		printf("puffs_mount: handle size %zu too large\n",
    128 		    args->pa_fhsize);
    129 		error = EINVAL;
    130 		goto out;
    131 	}
    132 	/* sanity check file handle max sizes */
    133 	if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) {
    134 		size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize);
    135 
    136 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) {
    137 			if (NFSX_FHTOOBIG_P(kfhsize, 0)) {
    138 				printf("puffs_mount: fhsize larger than "
    139 				    "NFSv2 max %d\n",
    140 				    PUFFS_FROMFHSIZE(NFSX_V2FH));
    141 				error = EINVAL;
    142 				goto out;
    143 			}
    144 		}
    145 
    146 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) {
    147 			if (NFSX_FHTOOBIG_P(kfhsize, 1)) {
    148 				printf("puffs_mount: fhsize larger than "
    149 				    "NFSv3 max %d\n",
    150 				    PUFFS_FROMFHSIZE(NFSX_V3FHMAX));
    151 				error = EINVAL;
    152 				goto out;
    153 			}
    154 		}
    155 	}
    156 
    157 	/* don't allow non-printing characters (like my sweet umlauts.. snif) */
    158 	args->pa_typename[sizeof(args->pa_typename)-1] = '\0';
    159 	for (p = args->pa_typename; *p; p++)
    160 		if (*p < ' ' || *p > '~')
    161 			*p = '.';
    162 
    163 	args->pa_mntfromname[sizeof(args->pa_mntfromname)-1] = '\0';
    164 	for (p = args->pa_mntfromname; *p; p++)
    165 		if (*p < ' ' || *p > '~')
    166 			*p = '.';
    167 
    168 	/* build real name */
    169 	(void)strlcpy(fstype, PUFFS_TYPEPREFIX, sizeof(fstype));
    170 	(void)strlcat(fstype, args->pa_typename, sizeof(fstype));
    171 
    172 	/* inform user server if it got the max request size it wanted */
    173 	if (args->pa_maxmsglen == 0 || args->pa_maxmsglen > PUFFS_MSG_MAXSIZE)
    174 		args->pa_maxmsglen = PUFFS_MSG_MAXSIZE;
    175 	else if (args->pa_maxmsglen < 2*PUFFS_MSGSTRUCT_MAX)
    176 		args->pa_maxmsglen = 2*PUFFS_MSGSTRUCT_MAX;
    177 
    178 	(void)strlcpy(args->pa_typename, fstype, sizeof(args->pa_typename));
    179 
    180 	if (args->pa_nhashbuckets == 0)
    181 		args->pa_nhashbuckets = puffs_pnodebuckets_default;
    182 	if (args->pa_nhashbuckets < 1)
    183 		args->pa_nhashbuckets = 1;
    184 	if (args->pa_nhashbuckets > PUFFS_MAXPNODEBUCKETS) {
    185 		args->pa_nhashbuckets = puffs_maxpnodebuckets;
    186 		printf("puffs_mount: using %d hash buckets. "
    187 		    "adjust puffs_maxpnodebuckets for more\n",
    188 		    puffs_maxpnodebuckets);
    189 	}
    190 
    191 	error = set_statvfs_info(path, UIO_USERSPACE, args->pa_mntfromname,
    192 	    UIO_SYSSPACE, fstype, mp, l);
    193 	if (error)
    194 		goto out;
    195 	mp->mnt_stat.f_iosize = DEV_BSIZE;
    196 
    197 	/*
    198 	 * We can't handle the VFS_STATVFS() mount_domount() does
    199 	 * after VFS_MOUNT() because we'd deadlock, so handle it
    200 	 * here already.
    201 	 */
    202 	copy_statvfs_info(&args->pa_svfsb, mp);
    203 	(void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat));
    204 
    205 	MALLOC(pmp, struct puffs_mount *, sizeof(struct puffs_mount),
    206 	    M_PUFFS, M_WAITOK | M_ZERO);
    207 
    208 	mp->mnt_fs_bshift = DEV_BSHIFT;
    209 	mp->mnt_dev_bshift = DEV_BSHIFT;
    210 	mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
    211 	mp->mnt_data = pmp;
    212 	mp->mnt_iflag |= IMNT_HAS_TRANS;
    213 
    214 	pmp->pmp_status = PUFFSTAT_MOUNTING;
    215 	pmp->pmp_mp = mp;
    216 	pmp->pmp_msg_maxsize = args->pa_maxmsglen;
    217 	pmp->pmp_args = *args;
    218 
    219 	pmp->pmp_npnodehash = args->pa_nhashbuckets;
    220 	pmp->pmp_pnodehash = malloc
    221 	    (sizeof(struct puffs_pnode_hashlist *) * pmp->pmp_npnodehash,
    222 	    M_PUFFS, M_WAITOK);
    223 	for (i = 0; i < pmp->pmp_npnodehash; i++)
    224 		LIST_INIT(&pmp->pmp_pnodehash[i]);
    225 	LIST_INIT(&pmp->pmp_newcookie);
    226 
    227 	/*
    228 	 * Inform the fileops processing code that we have a mountpoint.
    229 	 * If it doesn't know about anyone with our pid/fd having the
    230 	 * device open, punt
    231 	 */
    232 	if (puffs_setpmp(l->l_proc->p_pid, args->pa_fd, pmp)) {
    233 		error = ENOENT;
    234 		goto out;
    235 	}
    236 
    237 	/* XXX: check parameters */
    238 	pmp->pmp_root_cookie = args->pa_root_cookie;
    239 	pmp->pmp_root_vtype = args->pa_root_vtype;
    240 	pmp->pmp_root_vsize = args->pa_root_vsize;
    241 	pmp->pmp_root_rdev = args->pa_root_rdev;
    242 
    243 	mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
    244 	cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
    245 	cv_init(&pmp->pmp_refcount_cv, "puffsref");
    246 	cv_init(&pmp->pmp_unmounting_cv, "puffsum");
    247 	TAILQ_INIT(&pmp->pmp_msg_touser);
    248 	TAILQ_INIT(&pmp->pmp_msg_replywait);
    249 
    250 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    251 	    mp, MPTOPUFFSMP(mp)));
    252 
    253 	vfs_getnewfsid(mp);
    254 
    255  out:
    256 	if (error && pmp && pmp->pmp_pnodehash)
    257 		free(pmp->pmp_pnodehash, M_PUFFS);
    258 	if (error && pmp)
    259 		FREE(pmp, M_PUFFS);
    260 	FREE(args, M_PUFFS);
    261 	return error;
    262 }
    263 
    264 int
    265 puffs_start(struct mount *mp, int flags, struct lwp *l)
    266 {
    267 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    268 
    269 	KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
    270 	pmp->pmp_status = PUFFSTAT_RUNNING;
    271 
    272 	return 0;
    273 }
    274 
    275 int
    276 puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    277 {
    278 	PUFFS_MSG_VARS(vfs, unmount);
    279 	struct puffs_mount *pmp;
    280 	int error, force;
    281 
    282 	error = 0;
    283 	force = mntflags & MNT_FORCE;
    284 	pmp = MPTOPUFFSMP(mp);
    285 
    286 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    287 	    "status 0x%x\n", pmp->pmp_status));
    288 
    289 	/*
    290 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    291 	 * root vnode does not get flushed until unmount.  The
    292 	 * userspace root node cookie is stored in the mount
    293 	 * structure, so we can always re-instantiate a root vnode,
    294 	 * should userspace unmount decide it doesn't want to
    295 	 * cooperate.
    296 	 */
    297 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    298 	if (error)
    299 		goto out;
    300 
    301 	/*
    302 	 * If we are not DYING, we should ask userspace's opinion
    303 	 * about the situation
    304 	 */
    305 	mutex_enter(&pmp->pmp_lock);
    306 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    307 		pmp->pmp_unmounting = 1;
    308 		mutex_exit(&pmp->pmp_lock);
    309 
    310 		PUFFS_MSG_ALLOC(vfs, unmount);
    311 		unmount_msg->pvfsr_flags = mntflags;
    312 		puffs_cidcvt(&unmount_msg->pvfsr_cid, l);
    313 
    314 		error = puffs_msg_vfs(pmp, park_unmount, PUFFS_VFS_UNMOUNT);
    315 		PUFFS_MSG_RELEASE(unmount);
    316 
    317 		error = checkerr(pmp, error, __func__);
    318 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    319 
    320 		mutex_enter(&pmp->pmp_lock);
    321 		pmp->pmp_unmounting = 0;
    322 		cv_broadcast(&pmp->pmp_unmounting_cv);
    323 	}
    324 
    325 	/*
    326 	 * if userspace cooperated or we really need to die,
    327 	 * screw what userland thinks and just die.
    328 	 */
    329 	if (error == 0 || force) {
    330 		/* tell waiters & other resources to go unwait themselves */
    331 		puffs_userdead(pmp);
    332 		puffs_nukebypmp(pmp);
    333 
    334 		/*
    335 		 * Wait until there are no more users for the mount resource.
    336 		 * Notice that this is hooked against transport_close
    337 		 * and return from touser.  In an ideal world, it would
    338 		 * be hooked against final return from all operations.
    339 		 * But currently it works well enough, since nobody
    340 		 * does weird blocking voodoo after return from touser().
    341 		 */
    342 		while (pmp->pmp_refcount != 0)
    343 			cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
    344 		mutex_exit(&pmp->pmp_lock);
    345 
    346 		/* free resources now that we hopefully have no waiters left */
    347 		cv_destroy(&pmp->pmp_unmounting_cv);
    348 		cv_destroy(&pmp->pmp_refcount_cv);
    349 		cv_destroy(&pmp->pmp_msg_waiter_cv);
    350 		mutex_destroy(&pmp->pmp_lock);
    351 
    352 		free(pmp->pmp_pnodehash, M_PUFFS);
    353 		FREE(pmp, M_PUFFS);
    354 		error = 0;
    355 	} else {
    356 		mutex_exit(&pmp->pmp_lock);
    357 	}
    358 
    359  out:
    360 	DPRINTF(("puffs_unmount: return %d\n", error));
    361 	return error;
    362 }
    363 
    364 /*
    365  * This doesn't need to travel to userspace
    366  */
    367 int
    368 puffs_root(struct mount *mp, struct vnode **vpp)
    369 {
    370 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    371 	int rv;
    372 
    373 	rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, 1, 1, vpp);
    374 	KASSERT(rv != PUFFS_NOSUCHCOOKIE);
    375 	return rv;
    376 }
    377 
    378 int
    379 puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    380 {
    381 	PUFFS_MSG_VARS(vfs, statvfs);
    382 	struct puffs_mount *pmp;
    383 	int error = 0;
    384 
    385 	pmp = MPTOPUFFSMP(mp);
    386 
    387 	/*
    388 	 * If we are mounting, it means that the userspace counterpart
    389 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    390 	 * requesting statvfs from userspace would mean a deadlock.
    391 	 * Compensate.
    392 	 */
    393 	if (pmp->pmp_status == PUFFSTAT_MOUNTING)
    394 		return EINPROGRESS;
    395 
    396 	PUFFS_MSG_ALLOC(vfs, statvfs);
    397 	puffs_cidcvt(&statvfs_msg->pvfsr_cid, l);
    398 
    399 	error = puffs_msg_vfs(pmp, park_statvfs, PUFFS_VFS_STATVFS);
    400 	error = checkerr(pmp, error, __func__);
    401 	statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
    402 
    403 	/*
    404 	 * Try to produce a sensible result even in the event
    405 	 * of userspace error.
    406 	 *
    407 	 * XXX: cache the copy in non-error case
    408 	 */
    409 	if (!error) {
    410 		copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
    411 		(void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
    412 		    sizeof(struct statvfs));
    413 	} else {
    414 		copy_statvfs_info(sbp, mp);
    415 	}
    416 
    417 	PUFFS_MSG_RELEASE(statvfs);
    418 	return error;
    419 }
    420 
    421 static int
    422 pageflush(struct mount *mp, kauth_cred_t cred,
    423 	int waitfor, int suspending, struct lwp *l)
    424 {
    425 	struct puffs_node *pn;
    426 	struct vnode *vp, *mvp;
    427 	int error, rv;
    428 
    429 	KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
    430 	KASSERT((suspending == 0)
    431 	    || (fstrans_is_owner(mp)
    432 	      && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
    433 
    434 	error = 0;
    435 
    436 	/* Allocate a marker vnode. */
    437 	if ((mvp = valloc(mp)) == NULL)
    438 		return ENOMEM;
    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 	mutex_enter(&mntvnode_lock);
    447  loop:
    448 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    449 		vmark(mvp, vp);
    450 		if (vp->v_mount != mp || vismarker(vp))
    451 			continue;
    452 
    453 		mutex_enter(&vp->v_interlock);
    454 		pn = VPTOPP(vp);
    455 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    456 			mutex_exit(&vp->v_interlock);
    457 			continue;
    458 		}
    459 
    460 		mutex_exit(&mntvnode_lock);
    461 
    462 		/*
    463 		 * Here we try to get a reference to the vnode and to
    464 		 * lock it.  This is mostly cargo-culted, but I will
    465 		 * offer an explanation to why I believe this might
    466 		 * actually do the right thing.
    467 		 *
    468 		 * If the vnode is a goner, we quite obviously don't need
    469 		 * to sync it.
    470 		 *
    471 		 * If the vnode was busy, we don't need to sync it because
    472 		 * this is never called with MNT_WAIT except from
    473 		 * dounmount(), when we are wait-flushing all the dirty
    474 		 * vnodes through other routes in any case.  So there,
    475 		 * sync() doesn't actually sync.  Happy now?
    476 		 *
    477 		 * NOTE: if we're suspending, vget() does NOT lock.
    478 		 * See puffs_lock() for details.
    479 		 */
    480 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    481 		if (rv) {
    482 			mutex_enter(&mntvnode_lock);
    483 			if (rv == ENOENT) {
    484 				(void)vunmark(mvp);
    485 				goto loop;
    486 			}
    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 			mutex_enter(&vp->v_interlock);
    513 			pn->pn_stat |= PNODE_SUSPEND;
    514 			mutex_exit(&vp->v_interlock);
    515 		}
    516 		rv = VOP_FSYNC(vp, cred, waitfor, 0, 0, l);
    517 		if (suspending || waitfor == MNT_LAZY) {
    518 			mutex_enter(&vp->v_interlock);
    519 			pn->pn_stat &= ~PNODE_SUSPEND;
    520 			mutex_exit(&vp->v_interlock);
    521 		}
    522 		if (rv)
    523 			error = rv;
    524 		vput(vp);
    525 		mutex_enter(&mntvnode_lock);
    526 	}
    527 	mutex_exit(&mntvnode_lock);
    528 	vfree(mvp);
    529 
    530 	return error;
    531 }
    532 
    533 int
    534 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
    535 	struct lwp *l)
    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, l);
    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, l);
    548 
    549 	rv = puffs_msg_vfs(pmp, park_sync, PUFFS_VFS_SYNC);
    550 	rv = checkerr(pmp, rv, __func__);
    551 	if (rv)
    552 		error = rv;
    553 
    554 	PUFFS_MSG_RELEASE(sync);
    555 	return error;
    556 }
    557 
    558 int
    559 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    560 {
    561 	PUFFS_MSG_VARS(vfs, fhtonode);
    562 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    563 	struct vnode *vp;
    564 	void *fhdata;
    565 	size_t argsize, fhlen;
    566 	int error;
    567 
    568 	if (pmp->pmp_args.pa_fhsize == 0)
    569 		return EOPNOTSUPP;
    570 
    571 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    572 		fhlen = fhp->fid_len;
    573 		fhdata = fhp;
    574 	} else {
    575 		fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
    576 		fhdata = fhp->fid_data;
    577 
    578 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    579 			if (pmp->pmp_args.pa_fhsize < fhlen)
    580 				return EINVAL;
    581 		} else {
    582 			if (pmp->pmp_args.pa_fhsize != fhlen)
    583 				return EINVAL;
    584 		}
    585 	}
    586 
    587 	argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
    588 	puffs_msgmem_alloc(argsize, &park_fhtonode, (void **)&fhtonode_msg, 1);
    589 	fhtonode_msg->pvfsr_dsize = fhlen;
    590 	memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
    591 
    592 	error = puffs_msg_vfs(pmp, park_fhtonode, PUFFS_VFS_FHTOVP);
    593 	error = checkerr(pmp, error, __func__);
    594 	if (error)
    595 		goto out;
    596 
    597 	error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp);
    598 	DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
    599 	    fhtonode_msg->pvfsr_fhcookie, vp));
    600 	if (error == PUFFS_NOSUCHCOOKIE) {
    601 		error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
    602 		    fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
    603 		    fhtonode_msg->pvfsr_rdev, &vp);
    604 		if (error)
    605 			goto out;
    606 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    607 	} else if (error) {
    608 		goto out;
    609 	}
    610 
    611 	*vpp = vp;
    612  out:
    613 	puffs_msgmem_release(park_fhtonode);
    614 	return error;
    615 }
    616 
    617 int
    618 puffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    619 {
    620 	PUFFS_MSG_VARS(vfs, nodetofh);
    621 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    622 	size_t argsize, fhlen;
    623 	int error;
    624 
    625 	if (pmp->pmp_args.pa_fhsize == 0)
    626 		return EOPNOTSUPP;
    627 
    628 	/* if file handles are static len, we can test len immediately */
    629 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    630 	    && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
    631 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    632 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    633 		return E2BIG;
    634 	}
    635 
    636 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    637 		fhlen = *fh_size;
    638 	else
    639 		fhlen = PUFFS_FROMFHSIZE(*fh_size);
    640 
    641 	argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
    642 	puffs_msgmem_alloc(argsize, &park_nodetofh, (void **)&nodetofh_msg, 1);
    643 	nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
    644 	nodetofh_msg->pvfsr_dsize = fhlen;
    645 
    646 	error = puffs_msg_vfs(pmp, park_nodetofh, PUFFS_VFS_VPTOFH);
    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_msg_errnotify(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 	malloc_type_attach(M_PUFFS);
    699 
    700 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    701 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    702 	puffs_transport_init();
    703 	puffs_msgif_init();
    704 }
    705 
    706 void
    707 puffs_done()
    708 {
    709 
    710 	puffs_msgif_destroy();
    711 	puffs_transport_destroy();
    712 	pool_destroy(&puffs_pnpool);
    713 
    714 	malloc_type_detach(M_PUFFS);
    715 }
    716 
    717 int
    718 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    719 {
    720 
    721 	return EOPNOTSUPP;
    722 }
    723 
    724 int
    725 puffs_suspendctl(struct mount *mp, int cmd)
    726 {
    727 	PUFFS_MSG_VARS(vfs, suspend);
    728 	struct puffs_mount *pmp;
    729 	int error;
    730 
    731 	pmp = MPTOPUFFSMP(mp);
    732 	switch (cmd) {
    733 	case SUSPEND_SUSPEND:
    734 		DPRINTF(("puffs_suspendctl: suspending\n"));
    735 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
    736 			break;
    737 		PUFFS_MSG_ALLOC(vfs, suspend);
    738 		puffs_msg_setfaf(park_suspend);
    739 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_START;
    740 		puffs_msg_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    741 		PUFFS_MSG_RELEASE(suspend);
    742 
    743 		error = pageflush(mp, FSCRED, 0, 1, curlwp);
    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_vfs(pmp, park_suspend, PUFFS_VFS_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_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    761 		PUFFS_MSG_RELEASE(suspend);
    762 
    763 		break;
    764 
    765 	case SUSPEND_RESUME:
    766 		DPRINTF(("puffs_suspendctl: resume\n"));
    767 		error = 0;
    768 		(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    769 		PUFFS_MSG_ALLOC(vfs, suspend);
    770 		puffs_msg_setfaf(park_suspend);
    771 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_RESUME;
    772 		puffs_msg_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    773 		PUFFS_MSG_RELEASE(suspend);
    774 		break;
    775 
    776 	default:
    777 		error = EINVAL;
    778 		break;
    779 	}
    780 
    781 	DPRINTF(("puffs_suspendctl: return %d\n", error));
    782 	return error;
    783 }
    784 
    785 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    786 	&puffs_vnodeop_opv_desc,
    787 	&puffs_specop_opv_desc,
    788 	&puffs_fifoop_opv_desc,
    789 	&puffs_msgop_opv_desc,
    790 	NULL,
    791 };
    792 
    793 struct vfsops puffs_vfsops = {
    794 	MOUNT_PUFFS,
    795 	sizeof (struct puffs_kargs),
    796 	puffs_mount,		/* mount	*/
    797 	puffs_start,		/* start	*/
    798 	puffs_unmount,		/* unmount	*/
    799 	puffs_root,		/* root		*/
    800 	(void *)eopnotsupp,	/* quotactl	*/
    801 	puffs_statvfs,		/* statvfs	*/
    802 	puffs_sync,		/* sync		*/
    803 	(void *)eopnotsupp,	/* vget		*/
    804 	puffs_fhtovp,		/* fhtovp	*/
    805 	puffs_vptofh,		/* vptofh	*/
    806 	puffs_init,		/* init		*/
    807 	NULL,			/* reinit	*/
    808 	puffs_done,		/* done		*/
    809 	NULL,			/* mountroot	*/
    810 	puffs_snapshot,		/* snapshot	*/
    811 	vfs_stdextattrctl,	/* extattrctl	*/
    812 	puffs_suspendctl,	/* suspendctl	*/
    813 	puffs_vnodeopv_descs,	/* vnodeops	*/
    814 	0,			/* refcount	*/
    815 	{ NULL, NULL }
    816 };
    817 VFS_ATTACH(puffs_vfsops);
    818