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