Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.45
      1 /*	$NetBSD: puffs_vfsops.c,v 1.45 2007/06/21 14:54:49 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.45 2007/06/21 14:54:49 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 = MPTOPUFFSMP(mp);
    343 
    344 	return puffs_pnode2vnode(pmp, pmp->pmp_root_cookie, 1, vpp);
    345 }
    346 
    347 int
    348 puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    349 {
    350 	struct puffs_vfsreq_statvfs *statvfs_arg; /* too big for stack */
    351 	struct puffs_mount *pmp;
    352 	int error = 0;
    353 
    354 	pmp = MPTOPUFFSMP(mp);
    355 
    356 	/*
    357 	 * If we are mounting, it means that the userspace counterpart
    358 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    359 	 * requesting statvfs from userspace would mean a deadlock.
    360 	 * Compensate.
    361 	 */
    362 	if (pmp->pmp_status == PUFFSTAT_MOUNTING)
    363 		return EINPROGRESS;
    364 
    365 	/* too big for stack */
    366 	MALLOC(statvfs_arg, struct puffs_vfsreq_statvfs *,
    367 	    sizeof(struct puffs_vfsreq_statvfs), M_PUFFS, M_WAITOK | M_ZERO);
    368 	statvfs_arg->pvfsr_pid = puffs_lwp2pid(l);
    369 
    370 	error = puffs_vfstouser(pmp, PUFFS_VFS_STATVFS,
    371 	    statvfs_arg, sizeof(*statvfs_arg));
    372 	statvfs_arg->pvfsr_sb.f_iosize = DEV_BSIZE;
    373 
    374 	/*
    375 	 * Try to produce a sensible result even in the event
    376 	 * of userspace error.
    377 	 *
    378 	 * XXX: cache the copy in non-error case
    379 	 */
    380 	if (!error) {
    381 		copy_statvfs_info(&statvfs_arg->pvfsr_sb, mp);
    382 		(void)memcpy(sbp, &statvfs_arg->pvfsr_sb,
    383 		    sizeof(struct statvfs));
    384 	} else {
    385 		copy_statvfs_info(sbp, mp);
    386 	}
    387 
    388 	FREE(statvfs_arg, M_PUFFS);
    389 	return error;
    390 }
    391 
    392 static int
    393 pageflush(struct mount *mp, kauth_cred_t cred,
    394 	int waitfor, int suspending, struct lwp *l)
    395 {
    396 	struct puffs_node *pn;
    397 	struct vnode *vp, *nvp;
    398 	int error, rv;
    399 
    400 	KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
    401 	KASSERT((suspending == 0)
    402 	    || (fstrans_is_owner(mp)
    403 	      && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
    404 
    405 	error = 0;
    406 
    407 	/*
    408 	 * Sync all cached data from regular vnodes (which are not
    409 	 * currently locked, see below).  After this we call VFS_SYNC
    410 	 * for the fs server, which should handle data and metadata for
    411 	 * all the nodes it knows to exist.
    412 	 */
    413 	simple_lock(&mntvnode_slock);
    414  loop:
    415 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
    416 		/* check if we're on the right list */
    417 		if (vp->v_mount != mp)
    418 			goto loop;
    419 
    420 		simple_lock(&vp->v_interlock);
    421 		pn = VPTOPP(vp);
    422 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
    423 
    424 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    425 			simple_unlock(&vp->v_interlock);
    426 			continue;
    427 		}
    428 
    429 		simple_unlock(&mntvnode_slock);
    430 
    431 		/*
    432 		 * Here we try to get a reference to the vnode and to
    433 		 * lock it.  This is mostly cargo-culted, but I will
    434 		 * offer an explanation to why I believe this might
    435 		 * actually do the right thing.
    436 		 *
    437 		 * If the vnode is a goner, we quite obviously don't need
    438 		 * to sync it.
    439 		 *
    440 		 * If the vnode was busy, we don't need to sync it because
    441 		 * this is never called with MNT_WAIT except from
    442 		 * dounmount(), when we are wait-flushing all the dirty
    443 		 * vnodes through other routes in any case.  So there,
    444 		 * sync() doesn't actually sync.  Happy now?
    445 		 *
    446 		 * NOTE: if we're suspending, vget() does NOT lock.
    447 		 * See puffs_lock() for details.
    448 		 */
    449 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    450 		if (rv) {
    451 			simple_lock(&mntvnode_slock);
    452 			if (rv == ENOENT)
    453 				goto loop;
    454 			continue;
    455 		}
    456 
    457 		/*
    458 		 * Thread information to puffs_strategy() through the
    459 		 * pnode flags: we want to issue the putpages operations
    460 		 * as FAF if we're suspending, since it's very probable
    461 		 * that our execution context is that of the userspace
    462 		 * daemon.  We can do this because:
    463 		 *   + we send the "going to suspend" prior to this part
    464 		 *   + if any of the writes fails in userspace, it's the
    465 		 *     file system server's problem to decide if this was a
    466 		 *     failed snapshot when it gets the "snapshot complete"
    467 		 *     notification.
    468 		 *   + if any of the writes fail in the kernel already, we
    469 		 *     immediately fail *and* notify the user server of
    470 		 *     failure.
    471 		 *
    472 		 * We also do FAFs if we're called from the syncer.  This
    473 		 * is just general optimization for trickle sync: no need
    474 		 * to really guarantee that the stuff ended on backing
    475 		 * storage.
    476 		 * TODO: Maybe also hint the user server of this twist?
    477 		 */
    478 		if (suspending || waitfor == MNT_LAZY) {
    479 			simple_lock(&vp->v_interlock);
    480 			pn->pn_stat |= PNODE_SUSPEND;
    481 			simple_unlock(&vp->v_interlock);
    482 		}
    483 		rv = VOP_FSYNC(vp, cred, waitfor, 0, 0, l);
    484 		if (suspending || waitfor == MNT_LAZY) {
    485 			simple_lock(&vp->v_interlock);
    486 			pn->pn_stat &= ~PNODE_SUSPEND;
    487 			simple_unlock(&vp->v_interlock);
    488 		}
    489 		if (rv)
    490 			error = rv;
    491 		vput(vp);
    492 		simple_lock(&mntvnode_slock);
    493 	}
    494 	simple_unlock(&mntvnode_slock);
    495 
    496 	return error;
    497 }
    498 
    499 int
    500 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
    501 	struct lwp *l)
    502 {
    503 	int error, rv;
    504 
    505 	PUFFS_VFSREQ(sync);
    506 
    507 	error = pageflush(mp, cred, waitfor, 0, l);
    508 
    509 	/* sync fs */
    510 	sync_arg.pvfsr_waitfor = waitfor;
    511 	puffs_credcvt(&sync_arg.pvfsr_cred, cred);
    512 	sync_arg.pvfsr_pid = puffs_lwp2pid(l);
    513 
    514 	rv = puffs_vfstouser(MPTOPUFFSMP(mp), PUFFS_VFS_SYNC,
    515 	    &sync_arg, sizeof(sync_arg));
    516 	if (rv)
    517 		error = rv;
    518 
    519 	return error;
    520 }
    521 
    522 int
    523 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    524 {
    525 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    526 	struct puffs_vfsreq_fhtonode *fhtonode_argp;
    527 	struct vnode *vp;
    528 	size_t argsize;
    529 	int error;
    530 
    531 	if (pmp->pmp_args.pa_fhsize == 0)
    532 		return EOPNOTSUPP;
    533 
    534 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    535 		if (pmp->pmp_args.pa_fhsize < PUFFS_FROMFHSIZE(fhp->fid_len))
    536 			return EINVAL;
    537 	} else {
    538 		if (pmp->pmp_args.pa_fhsize != PUFFS_FROMFHSIZE(fhp->fid_len))
    539 			return EINVAL;
    540 	}
    541 
    542 	argsize = sizeof(struct puffs_vfsreq_fhtonode)
    543 	    + PUFFS_FROMFHSIZE(fhp->fid_len);
    544 	fhtonode_argp = malloc(argsize, M_PUFFS, M_ZERO | M_WAITOK);
    545 	fhtonode_argp->pvfsr_dsize = PUFFS_FROMFHSIZE(fhp->fid_len);
    546 	memcpy(fhtonode_argp->pvfsr_data, fhp->fid_data,
    547 	    PUFFS_FROMFHSIZE(fhp->fid_len));
    548 
    549 	error = puffs_vfstouser(pmp, PUFFS_VFS_FHTOVP, fhtonode_argp, argsize);
    550 	if (error)
    551 		goto out;
    552 
    553 	error = puffs_pnode2vnode(pmp, fhtonode_argp->pvfsr_fhcookie, 1, &vp);
    554 	DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
    555 	    fhtonode_argp->pvfsr_fhcookie, vp));
    556 	if (error) {
    557 		error = puffs_getvnode(mp, fhtonode_argp->pvfsr_fhcookie,
    558 		    fhtonode_argp->pvfsr_vtype, fhtonode_argp->pvfsr_size,
    559 		    fhtonode_argp->pvfsr_rdev, &vp);
    560 		if (error)
    561 			goto out;
    562 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    563 	}
    564 
    565 	*vpp = vp;
    566  out:
    567 	free(fhtonode_argp, M_PUFFS);
    568 	return error;
    569 }
    570 
    571 int
    572 puffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    573 {
    574 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    575 	struct puffs_vfsreq_nodetofh *nodetofh_argp;
    576 	size_t argsize;
    577 	int error;
    578 
    579 	if (pmp->pmp_args.pa_fhsize == 0)
    580 		return EOPNOTSUPP;
    581 
    582 	/* if file handles are static length, we can return immediately */
    583 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    584 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    585 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    586 		return E2BIG;
    587 	}
    588 
    589 	argsize = sizeof(struct puffs_vfsreq_nodetofh)
    590 	    + PUFFS_FROMFHSIZE(*fh_size);
    591 	nodetofh_argp = malloc(argsize, M_PUFFS, M_ZERO | M_WAITOK);
    592 	nodetofh_argp->pvfsr_fhcookie = VPTOPNC(vp);
    593 	nodetofh_argp->pvfsr_dsize = PUFFS_FROMFHSIZE(*fh_size);
    594 
    595 	error = puffs_vfstouser(pmp, PUFFS_VFS_VPTOFH, nodetofh_argp, argsize);
    596 	if (error) {
    597 		if (error == E2BIG)
    598 			*fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
    599 		goto out;
    600 	}
    601 
    602 	if (PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize) > FHANDLE_SIZE_MAX) {
    603 		/* XXX: wrong direction */
    604 		error = EINVAL;
    605 		goto out;
    606 	}
    607 
    608 	if (*fh_size < PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize)) {
    609 		*fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
    610 		error = E2BIG;
    611 		goto out;
    612 	}
    613 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    614 		*fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
    615 	} else {
    616 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    617 	}
    618 
    619 	if (fhp) {
    620 		fhp->fid_len = *fh_size;
    621 		memcpy(fhp->fid_data,
    622 		    nodetofh_argp->pvfsr_data, nodetofh_argp->pvfsr_dsize);
    623 	}
    624 
    625  out:
    626 	free(nodetofh_argp, M_PUFFS);
    627 	return error;
    628 }
    629 
    630 void
    631 puffs_init()
    632 {
    633 
    634 	malloc_type_attach(M_PUFFS);
    635 
    636 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    637 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    638 	puffs_transport_init();
    639 	puffs_msgif_init();
    640 }
    641 
    642 void
    643 puffs_done()
    644 {
    645 
    646 	puffs_msgif_destroy();
    647 	puffs_transport_destroy();
    648 	pool_destroy(&puffs_pnpool);
    649 
    650 	malloc_type_detach(M_PUFFS);
    651 }
    652 
    653 int
    654 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    655 {
    656 
    657 	return EOPNOTSUPP;
    658 }
    659 
    660 int
    661 puffs_suspendctl(struct mount *mp, int cmd)
    662 {
    663 	struct puffs_mount *pmp;
    664 	int error;
    665 
    666 	pmp = MPTOPUFFSMP(mp);
    667 	switch (cmd) {
    668 	case SUSPEND_SUSPEND:
    669 		DPRINTF(("puffs_suspendctl: suspending\n"));
    670 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
    671 			break;
    672 		puffs_suspendtouser(pmp, PUFFS_SUSPEND_START);
    673 
    674 		error = pageflush(mp, FSCRED, 0, 1, curlwp);
    675 		if (error == 0)
    676 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
    677 
    678 		if (error != 0) {
    679 			puffs_suspendtouser(pmp, PUFFS_SUSPEND_ERROR);
    680 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    681 			break;
    682 		}
    683 
    684 		puffs_suspendtouser(pmp, PUFFS_SUSPEND_SUSPENDED);
    685 
    686 		break;
    687 
    688 	case SUSPEND_RESUME:
    689 		DPRINTF(("puffs_suspendctl: resume\n"));
    690 		error = 0;
    691 		(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    692 		puffs_suspendtouser(pmp, PUFFS_SUSPEND_RESUME);
    693 		break;
    694 
    695 	default:
    696 		error = EINVAL;
    697 		break;
    698 	}
    699 
    700 	DPRINTF(("puffs_suspendctl: return %d\n", error));
    701 	return error;
    702 }
    703 
    704 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    705 	&puffs_vnodeop_opv_desc,
    706 	&puffs_specop_opv_desc,
    707 	&puffs_fifoop_opv_desc,
    708 	&puffs_msgop_opv_desc,
    709 	NULL,
    710 };
    711 
    712 struct vfsops puffs_vfsops = {
    713 	MOUNT_PUFFS,
    714 	puffs_mount,		/* mount	*/
    715 	puffs_start,		/* start	*/
    716 	puffs_unmount,		/* unmount	*/
    717 	puffs_root,		/* root		*/
    718 	(void *)eopnotsupp,	/* quotactl	*/
    719 	puffs_statvfs,		/* statvfs	*/
    720 	puffs_sync,		/* sync		*/
    721 	(void *)eopnotsupp,	/* vget		*/
    722 	puffs_fhtovp,		/* fhtovp	*/
    723 	puffs_vptofh,		/* vptofh	*/
    724 	puffs_init,		/* init		*/
    725 	NULL,			/* reinit	*/
    726 	puffs_done,		/* done		*/
    727 	NULL,			/* mountroot	*/
    728 	puffs_snapshot,		/* snapshot	*/
    729 	vfs_stdextattrctl,	/* extattrctl	*/
    730 	puffs_suspendctl,	/* suspendctl	*/
    731 	puffs_vnodeopv_descs,	/* vnodeops	*/
    732 	0,			/* refcount	*/
    733 	{ NULL, NULL }
    734 };
    735 VFS_ATTACH(puffs_vfsops);
    736