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