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