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