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