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