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