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