Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.114
      1 /*	$NetBSD: puffs_vfsops.c,v 1.114 2014/08/28 08:29:50 hannken 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.114 2014/08/28 08:29:50 hannken Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/kernel.h>
     37 #include <sys/mount.h>
     38 #include <sys/malloc.h>
     39 #include <sys/extattr.h>
     40 #include <sys/queue.h>
     41 #include <sys/vnode.h>
     42 #include <sys/dirent.h>
     43 #include <sys/kauth.h>
     44 #include <sys/proc.h>
     45 #include <sys/module.h>
     46 #include <sys/kthread.h>
     47 
     48 #include <uvm/uvm.h>
     49 
     50 #include <dev/putter/putter_sys.h>
     51 
     52 #include <miscfs/genfs/genfs.h>
     53 
     54 #include <fs/puffs/puffs_msgif.h>
     55 #include <fs/puffs/puffs_sys.h>
     56 
     57 #include <lib/libkern/libkern.h>
     58 
     59 #include <nfs/nfsproto.h> /* for fh sizes */
     60 
     61 MODULE(MODULE_CLASS_VFS, puffs, "putter");
     62 
     63 VFS_PROTOS(puffs_vfsop);
     64 
     65 static struct putter_ops puffs_putter = {
     66 	.pop_getout	= puffs_msgif_getout,
     67 	.pop_releaseout	= puffs_msgif_releaseout,
     68 	.pop_waitcount	= puffs_msgif_waitcount,
     69 	.pop_dispatch	= puffs_msgif_dispatch,
     70 	.pop_close	= puffs_msgif_close,
     71 };
     72 
     73 static const struct genfs_ops puffs_genfsops = {
     74         .gop_size = puffs_gop_size,
     75 	.gop_write = genfs_gop_write,
     76 	.gop_markupdate = puffs_gop_markupdate,
     77 #if 0
     78 	.gop_alloc, should ask userspace
     79 #endif
     80 };
     81 
     82 /*
     83  * Try to ensure data structures used by the puffs protocol
     84  * do not unexpectedly change.
     85  */
     86 #if defined(__i386__) && defined(__ELF__)
     87 CTASSERT(sizeof(struct puffs_kargs) == 3928);
     88 CTASSERT(sizeof(struct vattr) == 136);
     89 CTASSERT(sizeof(struct puffs_req) == 44);
     90 #endif
     91 
     92 int
     93 puffs_vfsop_mount(struct mount *mp, const char *path, void *data,
     94 	size_t *data_len)
     95 {
     96 	struct puffs_mount *pmp = NULL;
     97 	struct puffs_kargs *args;
     98 	char fstype[_VFS_NAMELEN];
     99 	char *p;
    100 	int error = 0, i;
    101 	pid_t mntpid = curlwp->l_proc->p_pid;
    102 
    103 	if (data == NULL)
    104 		return EINVAL;
    105 	if (*data_len < sizeof *args)
    106 		return EINVAL;
    107 
    108 	if (mp->mnt_flag & MNT_GETARGS) {
    109 		pmp = MPTOPUFFSMP(mp);
    110 		*(struct puffs_kargs *)data = pmp->pmp_args;
    111 		*data_len = sizeof *args;
    112 		return 0;
    113 	}
    114 
    115 	/* update is not supported currently */
    116 	if (mp->mnt_flag & MNT_UPDATE)
    117 		return EOPNOTSUPP;
    118 
    119 	args = (struct puffs_kargs *)data;
    120 
    121 	if (args->pa_vers != PUFFSVERSION) {
    122 		printf("puffs_mount: development version mismatch: "
    123 		    "kernel %d, lib %d\n", PUFFSVERSION, args->pa_vers);
    124 		error = EINVAL;
    125 		goto out;
    126 	}
    127 
    128 	if ((args->pa_flags & ~PUFFS_KFLAG_MASK) != 0) {
    129 		printf("puffs_mount: invalid KFLAGs 0x%x\n", args->pa_flags);
    130 		error = EINVAL;
    131 		goto out;
    132 	}
    133 	if ((args->pa_fhflags & ~PUFFS_FHFLAG_MASK) != 0) {
    134 		printf("puffs_mount: invalid FHFLAGs 0x%x\n", args->pa_fhflags);
    135 		error = EINVAL;
    136 		goto out;
    137 	}
    138 
    139 	for (i = 0; i < __arraycount(args->pa_spare); i++) {
    140 		if (args->pa_spare[i] != 0) {
    141 			printf("puffs_mount: pa_spare[%d] = 0x%x\n",
    142 			    i, args->pa_spare[i]);
    143 			error = EINVAL;
    144 			goto out;
    145 		}
    146 	}
    147 
    148 	/* use dummy value for passthrough */
    149 	if (args->pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    150 		args->pa_fhsize = sizeof(struct fid);
    151 
    152 	/* sanitize file handle length */
    153 	if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) {
    154 		printf("puffs_mount: handle size %zu too large\n",
    155 		    args->pa_fhsize);
    156 		error = EINVAL;
    157 		goto out;
    158 	}
    159 	/* sanity check file handle max sizes */
    160 	if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) {
    161 		size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize);
    162 
    163 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) {
    164 			if (NFSX_FHTOOBIG_P(kfhsize, 0)) {
    165 				printf("puffs_mount: fhsize larger than "
    166 				    "NFSv2 max %d\n",
    167 				    PUFFS_FROMFHSIZE(NFSX_V2FH));
    168 				error = EINVAL;
    169 				goto out;
    170 			}
    171 		}
    172 
    173 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) {
    174 			if (NFSX_FHTOOBIG_P(kfhsize, 1)) {
    175 				printf("puffs_mount: fhsize larger than "
    176 				    "NFSv3 max %d\n",
    177 				    PUFFS_FROMFHSIZE(NFSX_V3FHMAX));
    178 				error = EINVAL;
    179 				goto out;
    180 			}
    181 		}
    182 	}
    183 
    184 	/* don't allow non-printing characters (like my sweet umlauts.. snif) */
    185 	args->pa_typename[sizeof(args->pa_typename)-1] = '\0';
    186 	for (p = args->pa_typename; *p; p++)
    187 		if (*p < ' ' || *p > '~')
    188 			*p = '.';
    189 
    190 	args->pa_mntfromname[sizeof(args->pa_mntfromname)-1] = '\0';
    191 	for (p = args->pa_mntfromname; *p; p++)
    192 		if (*p < ' ' || *p > '~')
    193 			*p = '.';
    194 
    195 	/* build real name */
    196 	(void)strlcpy(fstype, PUFFS_TYPEPREFIX, sizeof(fstype));
    197 	(void)strlcat(fstype, args->pa_typename, sizeof(fstype));
    198 
    199 	/* inform user server if it got the max request size it wanted */
    200 	if (args->pa_maxmsglen == 0 || args->pa_maxmsglen > PUFFS_MSG_MAXSIZE)
    201 		args->pa_maxmsglen = PUFFS_MSG_MAXSIZE;
    202 	else if (args->pa_maxmsglen < 2*PUFFS_MSGSTRUCT_MAX)
    203 		args->pa_maxmsglen = 2*PUFFS_MSGSTRUCT_MAX;
    204 
    205 	(void)strlcpy(args->pa_typename, fstype, sizeof(args->pa_typename));
    206 
    207 	error = set_statvfs_info(path, UIO_USERSPACE, args->pa_mntfromname,
    208 	    UIO_SYSSPACE, fstype, mp, curlwp);
    209 	if (error)
    210 		goto out;
    211 	mp->mnt_stat.f_iosize = DEV_BSIZE;
    212 	mp->mnt_stat.f_namemax = args->pa_svfsb.f_namemax;
    213 
    214 	/*
    215 	 * We can't handle the VFS_STATVFS() mount_domount() does
    216 	 * after VFS_MOUNT() because we'd deadlock, so handle it
    217 	 * here already.
    218 	 */
    219 	copy_statvfs_info(&args->pa_svfsb, mp);
    220 	(void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat));
    221 
    222 	KASSERT(curlwp != uvm.pagedaemon_lwp);
    223 	pmp = kmem_zalloc(sizeof(struct puffs_mount), KM_SLEEP);
    224 
    225 	mp->mnt_fs_bshift = DEV_BSHIFT;
    226 	mp->mnt_dev_bshift = DEV_BSHIFT;
    227 	mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
    228 	mp->mnt_data = pmp;
    229 
    230 #if 0
    231 	/*
    232 	 * XXX: puffs code is MPSAFE.  However, VFS really isn't.
    233 	 * Currently, there is nothing which protects an inode from
    234 	 * reclaim while there are threads inside the file system.
    235 	 * This means that in the event of a server crash, an MPSAFE
    236 	 * mount is likely to end up accessing invalid memory.  For the
    237 	 * non-mpsafe case, the kernel lock, general structure of
    238 	 * puffs and pmp_refcount protect the threads during escape.
    239 	 *
    240 	 * Fixing this will require:
    241 	 *  a) fixing vfs
    242 	 * OR
    243 	 *  b) adding a small sleep to puffs_msgif_close() between
    244 	 *     userdead() and dounmount().
    245 	 *     (well, this isn't really a fix, but would solve
    246 	 *     99.999% of the race conditions).
    247 	 *
    248 	 * Also, in the event of "b", unmount -f should be used,
    249 	 * like with any other file system, sparingly and only when
    250 	 * it is "known" to be safe.
    251 	 */
    252 	mp->mnt_iflags |= IMNT_MPSAFE;
    253 #endif
    254 
    255 	pmp->pmp_status = PUFFSTAT_MOUNTING;
    256 	pmp->pmp_mp = mp;
    257 	pmp->pmp_msg_maxsize = args->pa_maxmsglen;
    258 	pmp->pmp_args = *args;
    259 
    260 	/*
    261 	 * Inform the fileops processing code that we have a mountpoint.
    262 	 * If it doesn't know about anyone with our pid/fd having the
    263 	 * device open, punt
    264 	 */
    265 	if ((pmp->pmp_pi
    266 	    = putter_attach(mntpid, args->pa_fd, pmp, &puffs_putter)) == NULL) {
    267 		error = ENOENT;
    268 		goto out;
    269 	}
    270 
    271 	/* XXX: check parameters */
    272 	pmp->pmp_root_cookie = args->pa_root_cookie;
    273 	pmp->pmp_root_vtype = args->pa_root_vtype;
    274 	pmp->pmp_root_vsize = args->pa_root_vsize;
    275 	pmp->pmp_root_rdev = args->pa_root_rdev;
    276 	pmp->pmp_docompat = args->pa_time32;
    277 
    278 	mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
    279 	mutex_init(&pmp->pmp_sopmtx, MUTEX_DEFAULT, IPL_NONE);
    280 	cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
    281 	cv_init(&pmp->pmp_refcount_cv, "puffsref");
    282 	cv_init(&pmp->pmp_unmounting_cv, "puffsum");
    283 	cv_init(&pmp->pmp_sopcv, "puffsop");
    284 	TAILQ_INIT(&pmp->pmp_msg_touser);
    285 	TAILQ_INIT(&pmp->pmp_msg_replywait);
    286 	TAILQ_INIT(&pmp->pmp_sopfastreqs);
    287 	TAILQ_INIT(&pmp->pmp_sopnodereqs);
    288 
    289 	if ((error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    290 	    puffs_sop_thread, pmp, NULL, "puffsop")) != 0)
    291 		goto out;
    292 	pmp->pmp_sopthrcount = 1;
    293 
    294 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    295 	    mp, MPTOPUFFSMP(mp)));
    296 
    297 	vfs_getnewfsid(mp);
    298 
    299  out:
    300 	if (error && pmp && pmp->pmp_pi)
    301 		putter_detach(pmp->pmp_pi);
    302 	if (error && pmp)
    303 		kmem_free(pmp, sizeof(struct puffs_mount));
    304 	return error;
    305 }
    306 
    307 int
    308 puffs_vfsop_start(struct mount *mp, int flags)
    309 {
    310 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    311 
    312 	KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
    313 	pmp->pmp_status = PUFFSTAT_RUNNING;
    314 
    315 	return 0;
    316 }
    317 
    318 int
    319 puffs_vfsop_unmount(struct mount *mp, int mntflags)
    320 {
    321 	PUFFS_MSG_VARS(vfs, unmount);
    322 	struct puffs_mount *pmp;
    323 	int error, force;
    324 
    325 	error = 0;
    326 	force = mntflags & MNT_FORCE;
    327 	pmp = MPTOPUFFSMP(mp);
    328 
    329 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    330 	    "status 0x%x\n", pmp->pmp_status));
    331 
    332 	/*
    333 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    334 	 * root vnode does not get flushed until unmount.  The
    335 	 * userspace root node cookie is stored in the mount
    336 	 * structure, so we can always re-instantiate a root vnode,
    337 	 * should userspace unmount decide it doesn't want to
    338 	 * cooperate.
    339 	 */
    340 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    341 	if (error)
    342 		goto out;
    343 
    344 	/*
    345 	 * If we are not DYING, we should ask userspace's opinion
    346 	 * about the situation
    347 	 */
    348 	mutex_enter(&pmp->pmp_lock);
    349 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    350 		pmp->pmp_unmounting = 1;
    351 		mutex_exit(&pmp->pmp_lock);
    352 
    353 		PUFFS_MSG_ALLOC(vfs, unmount);
    354 		puffs_msg_setinfo(park_unmount,
    355 		    PUFFSOP_VFS, PUFFS_VFS_UNMOUNT, NULL);
    356 		unmount_msg->pvfsr_flags = mntflags;
    357 
    358 		PUFFS_MSG_ENQUEUEWAIT(pmp, park_unmount, error);
    359 		PUFFS_MSG_RELEASE(unmount);
    360 
    361 		error = checkerr(pmp, error, __func__);
    362 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    363 
    364 		mutex_enter(&pmp->pmp_lock);
    365 		pmp->pmp_unmounting = 0;
    366 		cv_broadcast(&pmp->pmp_unmounting_cv);
    367 	}
    368 
    369 	/*
    370 	 * if userspace cooperated or we really need to die,
    371 	 * screw what userland thinks and just die.
    372 	 */
    373 	if (error == 0 || force) {
    374 		struct puffs_sopreq *psopr;
    375 
    376 		/* tell waiters & other resources to go unwait themselves */
    377 		puffs_userdead(pmp);
    378 		putter_detach(pmp->pmp_pi);
    379 
    380 		/*
    381 		 * Wait until there are no more users for the mount resource.
    382 		 * Notice that this is hooked against transport_close
    383 		 * and return from touser.  In an ideal world, it would
    384 		 * be hooked against final return from all operations.
    385 		 * But currently it works well enough, since nobody
    386 		 * does weird blocking voodoo after return from touser().
    387 		 */
    388 		while (pmp->pmp_refcount != 0)
    389 			cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
    390 		mutex_exit(&pmp->pmp_lock);
    391 
    392 		/*
    393 		 * Release kernel thread now that there is nothing
    394 		 * it would be wanting to lock.
    395 		 */
    396 		KASSERT(curlwp != uvm.pagedaemon_lwp);
    397 		psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
    398 		psopr->psopr_sopreq = PUFFS_SOPREQSYS_EXIT;
    399 		mutex_enter(&pmp->pmp_sopmtx);
    400 		if (pmp->pmp_sopthrcount == 0) {
    401 			mutex_exit(&pmp->pmp_sopmtx);
    402 			kmem_free(psopr, sizeof(*psopr));
    403 			mutex_enter(&pmp->pmp_sopmtx);
    404 			KASSERT(pmp->pmp_sopthrcount == 0);
    405 		} else {
    406 			TAILQ_INSERT_TAIL(&pmp->pmp_sopfastreqs,
    407 			    psopr, psopr_entries);
    408 			cv_signal(&pmp->pmp_sopcv);
    409 		}
    410 		while (pmp->pmp_sopthrcount > 0)
    411 			cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
    412 		mutex_exit(&pmp->pmp_sopmtx);
    413 
    414 		/* free resources now that we hopefully have no waiters left */
    415 		cv_destroy(&pmp->pmp_unmounting_cv);
    416 		cv_destroy(&pmp->pmp_refcount_cv);
    417 		cv_destroy(&pmp->pmp_msg_waiter_cv);
    418 		cv_destroy(&pmp->pmp_sopcv);
    419 		mutex_destroy(&pmp->pmp_lock);
    420 		mutex_destroy(&pmp->pmp_sopmtx);
    421 
    422 		kmem_free(pmp, sizeof(struct puffs_mount));
    423 		error = 0;
    424 	} else {
    425 		mutex_exit(&pmp->pmp_lock);
    426 	}
    427 
    428  out:
    429 	DPRINTF(("puffs_unmount: return %d\n", error));
    430 	return error;
    431 }
    432 
    433 /*
    434  * This doesn't need to travel to userspace
    435  */
    436 int
    437 puffs_vfsop_root(struct mount *mp, struct vnode **vpp)
    438 {
    439 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    440 	int rv;
    441 
    442 	rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, vpp);
    443 	KASSERT(rv != PUFFS_NOSUCHCOOKIE);
    444 	if (rv != 0)
    445 		return rv;
    446 	rv = vn_lock(*vpp, LK_EXCLUSIVE);
    447 	if (rv != 0) {
    448 		vrele(*vpp);
    449 		*vpp = NULL;
    450 		return rv;
    451 	}
    452 	return 0;
    453 }
    454 
    455 int
    456 puffs_vfsop_statvfs(struct mount *mp, struct statvfs *sbp)
    457 {
    458 	PUFFS_MSG_VARS(vfs, statvfs);
    459 	struct puffs_mount *pmp;
    460 	int error = 0;
    461 
    462 	pmp = MPTOPUFFSMP(mp);
    463 
    464 	/*
    465 	 * If we are mounting, it means that the userspace counterpart
    466 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    467 	 * requesting statvfs from userspace would mean a deadlock.
    468 	 * Compensate.
    469 	 */
    470 	if (__predict_false(pmp->pmp_status == PUFFSTAT_MOUNTING))
    471 		return EINPROGRESS;
    472 
    473 	PUFFS_MSG_ALLOC(vfs, statvfs);
    474 	puffs_msg_setinfo(park_statvfs, PUFFSOP_VFS, PUFFS_VFS_STATVFS, NULL);
    475 
    476 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_statvfs, error);
    477 	error = checkerr(pmp, error, __func__);
    478 	statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
    479 
    480 	/*
    481 	 * Try to produce a sensible result even in the event
    482 	 * of userspace error.
    483 	 *
    484 	 * XXX: cache the copy in non-error case
    485 	 */
    486 	if (!error) {
    487 		copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
    488 		(void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
    489 		    sizeof(struct statvfs));
    490 	} else {
    491 		copy_statvfs_info(sbp, mp);
    492 	}
    493 
    494 	PUFFS_MSG_RELEASE(statvfs);
    495 	return error;
    496 }
    497 
    498 static bool
    499 pageflush_selector(void *cl, struct vnode *vp)
    500 {
    501 	return vp->v_type == VREG &&
    502 	    !(LIST_EMPTY(&vp->v_dirtyblkhd) && UVM_OBJ_IS_CLEAN(&vp->v_uobj));
    503 }
    504 
    505 static int
    506 pageflush(struct mount *mp, kauth_cred_t cred, int waitfor)
    507 {
    508 	struct puffs_node *pn;
    509 	struct vnode *vp;
    510 	struct vnode_iterator *marker;
    511 	int error, rv, fsyncwait;
    512 
    513 	error = 0;
    514 	fsyncwait = (waitfor == MNT_WAIT) ? FSYNC_WAIT : 0;
    515 
    516 	/*
    517 	 * Sync all cached data from regular vnodes (which are not
    518 	 * currently locked, see below).  After this we call VFS_SYNC
    519 	 * for the fs server, which should handle data and metadata for
    520 	 * all the nodes it knows to exist.
    521 	 */
    522 	vfs_vnode_iterator_init(mp, &marker);
    523 	while ((vp = vfs_vnode_iterator_next(marker, pageflush_selector,
    524 	    NULL)))
    525 	{
    526 		/*
    527 		 * Here we try to get a reference to the vnode and to
    528 		 * lock it.  This is mostly cargo-culted, but I will
    529 		 * offer an explanation to why I believe this might
    530 		 * actually do the right thing.
    531 		 *
    532 		 * If the vnode is a goner, we quite obviously don't need
    533 		 * to sync it.
    534 		 *
    535 		 * If the vnode was busy, we don't need to sync it because
    536 		 * this is never called with MNT_WAIT except from
    537 		 * dounmount(), when we are wait-flushing all the dirty
    538 		 * vnodes through other routes in any case.  So there,
    539 		 * sync() doesn't actually sync.  Happy now?
    540 		 */
    541 		error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
    542 		if (error) {
    543 			vrele(vp);
    544 			continue;
    545 		}
    546 		pn = VPTOPP(vp);
    547 		/* hmm.. is the FAF thing entirely sensible? */
    548 		if (waitfor == MNT_LAZY) {
    549 			mutex_enter(vp->v_interlock);
    550 			pn->pn_stat |= PNODE_FAF;
    551 			mutex_exit(vp->v_interlock);
    552 		}
    553 		rv = VOP_FSYNC(vp, cred, fsyncwait, 0, 0);
    554 		if (waitfor == MNT_LAZY) {
    555 			mutex_enter(vp->v_interlock);
    556 			pn->pn_stat &= ~PNODE_FAF;
    557 			mutex_exit(vp->v_interlock);
    558 		}
    559 		if (rv)
    560 			error = rv;
    561 		vput(vp);
    562 	}
    563 	vfs_vnode_iterator_destroy(marker);
    564 
    565 	return error;
    566 }
    567 
    568 int
    569 puffs_vfsop_sync(struct mount *mp, int waitfor, struct kauth_cred *cred)
    570 {
    571 	PUFFS_MSG_VARS(vfs, sync);
    572 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    573 	int error, rv;
    574 
    575 	error = pageflush(mp, cred, waitfor);
    576 
    577 	/* sync fs */
    578 	PUFFS_MSG_ALLOC(vfs, sync);
    579 	sync_msg->pvfsr_waitfor = waitfor;
    580 	puffs_credcvt(&sync_msg->pvfsr_cred, cred);
    581 	puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL);
    582 
    583 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv);
    584 	rv = checkerr(pmp, rv, __func__);
    585 	if (rv)
    586 		error = rv;
    587 
    588 	PUFFS_MSG_RELEASE(sync);
    589 	return error;
    590 }
    591 
    592 int
    593 puffs_vfsop_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    594 {
    595 	PUFFS_MSG_VARS(vfs, fhtonode);
    596 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    597 	struct vnode *vp;
    598 	void *fhdata;
    599 	size_t argsize, fhlen;
    600 	int error;
    601 
    602 	if (pmp->pmp_args.pa_fhsize == 0)
    603 		return EOPNOTSUPP;
    604 
    605 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    606 		fhlen = fhp->fid_len;
    607 		fhdata = fhp;
    608 	} else {
    609 		fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
    610 		fhdata = fhp->fid_data;
    611 
    612 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    613 			if (pmp->pmp_args.pa_fhsize < fhlen)
    614 				return EINVAL;
    615 		} else {
    616 			if (pmp->pmp_args.pa_fhsize != fhlen)
    617 				return EINVAL;
    618 		}
    619 	}
    620 
    621 	argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
    622 	puffs_msgmem_alloc(argsize, &park_fhtonode, (void *)&fhtonode_msg, 1);
    623 	fhtonode_msg->pvfsr_dsize = fhlen;
    624 	memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
    625 	puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL);
    626 
    627 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error);
    628 	error = checkerr(pmp, error, __func__);
    629 	if (error)
    630 		goto out;
    631 
    632 	error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
    633 	    fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
    634 	    fhtonode_msg->pvfsr_rdev, &vp);
    635 	if (error)
    636 		goto out;
    637 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    638 
    639 	*vpp = vp;
    640  out:
    641 	puffs_msgmem_release(park_fhtonode);
    642 	return error;
    643 }
    644 
    645 int
    646 puffs_vfsop_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    647 {
    648 	PUFFS_MSG_VARS(vfs, nodetofh);
    649 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    650 	size_t argsize, fhlen;
    651 	int error;
    652 
    653 	if (pmp->pmp_args.pa_fhsize == 0)
    654 		return EOPNOTSUPP;
    655 
    656 	/* if file handles are static len, we can test len immediately */
    657 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    658 	    && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
    659 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    660 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    661 		return E2BIG;
    662 	}
    663 
    664 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    665 		fhlen = *fh_size;
    666 	else
    667 		fhlen = PUFFS_FROMFHSIZE(*fh_size);
    668 
    669 	argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
    670 	puffs_msgmem_alloc(argsize, &park_nodetofh, (void *)&nodetofh_msg, 1);
    671 	nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
    672 	nodetofh_msg->pvfsr_dsize = fhlen;
    673 	puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL);
    674 
    675 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error);
    676 	error = checkerr(pmp, error, __func__);
    677 
    678 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    679 		fhlen = nodetofh_msg->pvfsr_dsize;
    680 	else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
    681 		fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
    682 	else
    683 		fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    684 
    685 	if (error) {
    686 		if (error == E2BIG)
    687 			*fh_size = fhlen;
    688 		goto out;
    689 	}
    690 
    691 	if (fhlen > FHANDLE_SIZE_MAX) {
    692 		puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG,
    693 		    "file handle too big", VPTOPNC(vp));
    694 		error = EPROTO;
    695 		goto out;
    696 	}
    697 
    698 	if (*fh_size < fhlen) {
    699 		*fh_size = fhlen;
    700 		error = E2BIG;
    701 		goto out;
    702 	}
    703 	*fh_size = fhlen;
    704 
    705 	if (fhp) {
    706 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    707 			memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
    708 		} else {
    709 			fhp->fid_len = *fh_size;
    710 			memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
    711 			    nodetofh_msg->pvfsr_dsize);
    712 		}
    713 	}
    714 
    715  out:
    716 	puffs_msgmem_release(park_nodetofh);
    717 	return error;
    718 }
    719 
    720 int
    721 puffs_vfsop_loadvnode(struct mount *mp, struct vnode *vp,
    722     const void *key, size_t key_len, const void **new_key)
    723 {
    724 	struct puffs_mount *pmp;
    725 	struct puffs_node *pnode;
    726 
    727 	KASSERT(key_len == sizeof(puffs_cookie_t));
    728 
    729 	pmp = MPTOPUFFSMP(mp);
    730 
    731 	/* Allocate and initialize the pnode. */
    732 	pnode = pool_get(&puffs_pnpool, PR_WAITOK);
    733 	memset(pnode, 0, sizeof(struct puffs_node));
    734 
    735 	pnode->pn_vp = vp;
    736 	memcpy(&pnode->pn_cookie, key, key_len);
    737 	pnode->pn_refcount = 1;
    738 	mutex_init(&pnode->pn_mtx, MUTEX_DEFAULT, IPL_NONE);
    739 	mutex_init(&pnode->pn_sizemtx, MUTEX_DEFAULT, IPL_NONE);
    740 	selinit(&pnode->pn_sel);
    741 	vp->v_tag = VT_PUFFS;
    742 	vp->v_type = VNON;
    743 	vp->v_op = puffs_vnodeop_p;
    744 	if (pnode->pn_cookie == pmp->pmp_root_cookie)
    745 		vp->v_vflag |= VV_ROOT;
    746 	vp->v_data = pnode;
    747 
    748 	genfs_node_init(vp, &puffs_genfsops);
    749 	uvm_vnp_setsize(vp, 0);
    750 
    751 	*new_key = &pnode->pn_cookie;
    752 	return 0;
    753 }
    754 
    755 void
    756 puffs_vfsop_init(void)
    757 {
    758 
    759 	/* some checks depend on this */
    760 	KASSERT(VNOVAL == VSIZENOTSET);
    761 
    762 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    763 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    764 	pool_init(&puffs_vapool, sizeof(struct vattr), 0, 0, 0,
    765 	    "puffvapl", &pool_allocator_nointr, IPL_NONE);
    766 	puffs_msgif_init();
    767 }
    768 
    769 void
    770 puffs_vfsop_done(void)
    771 {
    772 
    773 	puffs_msgif_destroy();
    774 	pool_destroy(&puffs_pnpool);
    775 	pool_destroy(&puffs_vapool);
    776 }
    777 
    778 int
    779 puffs_vfsop_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    780 {
    781 
    782 	return EOPNOTSUPP;
    783 }
    784 
    785 int
    786 puffs_vfsop_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
    787 	int attrnamespace, const char *attrname)
    788 {
    789 	PUFFS_MSG_VARS(vfs, extattrctl);
    790 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    791 	struct puffs_node *pnp;
    792 	puffs_cookie_t pnc;
    793 	int error, flags;
    794 
    795 	if (vp) {
    796 		/* doesn't make sense for puffs servers */
    797 		if (vp->v_mount != mp)
    798 			return EXDEV;
    799 		pnp = vp->v_data;
    800 		pnc = pnp->pn_cookie;
    801 		flags = PUFFS_EXTATTRCTL_HASNODE;
    802 	} else {
    803 		pnp = pnc = NULL;
    804 		flags = 0;
    805 	}
    806 
    807 	PUFFS_MSG_ALLOC(vfs, extattrctl);
    808 	extattrctl_msg->pvfsr_cmd = cmd;
    809 	extattrctl_msg->pvfsr_attrnamespace = attrnamespace;
    810 	extattrctl_msg->pvfsr_flags = flags;
    811 	if (attrname) {
    812 		strlcpy(extattrctl_msg->pvfsr_attrname, attrname,
    813 		    sizeof(extattrctl_msg->pvfsr_attrname));
    814 		extattrctl_msg->pvfsr_flags |= PUFFS_EXTATTRCTL_HASATTRNAME;
    815 	}
    816 	puffs_msg_setinfo(park_extattrctl,
    817 	    PUFFSOP_VFS, PUFFS_VFS_EXTATTRCTL, pnc);
    818 
    819 	puffs_msg_enqueue(pmp, park_extattrctl);
    820 	if (vp) {
    821 		mutex_enter(&pnp->pn_mtx);
    822 		puffs_referencenode(pnp);
    823 		mutex_exit(&pnp->pn_mtx);
    824 		VOP_UNLOCK(vp);
    825 	}
    826 	error = puffs_msg_wait2(pmp, park_extattrctl, pnp, NULL);
    827 	PUFFS_MSG_RELEASE(extattrctl);
    828 	if (vp) {
    829 		puffs_releasenode(pnp);
    830 	}
    831 
    832 	return checkerr(pmp, error, __func__);
    833 }
    834 
    835 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    836 	&puffs_vnodeop_opv_desc,
    837 	&puffs_specop_opv_desc,
    838 	&puffs_fifoop_opv_desc,
    839 	&puffs_msgop_opv_desc,
    840 	NULL,
    841 };
    842 
    843 struct vfsops puffs_vfsops = {
    844 	.vfs_name = MOUNT_PUFFS,
    845 	.vfs_min_mount_data = sizeof (struct puffs_kargs),
    846 	.vfs_mount = puffs_vfsop_mount,
    847 	.vfs_start = puffs_vfsop_start,
    848 	.vfs_unmount = puffs_vfsop_unmount,
    849 	.vfs_root = puffs_vfsop_root,
    850 	.vfs_quotactl = (void *)eopnotsupp,
    851 	.vfs_statvfs = puffs_vfsop_statvfs,
    852 	.vfs_sync = puffs_vfsop_sync,
    853 	.vfs_vget = (void *)eopnotsupp,
    854 	.vfs_loadvnode = puffs_vfsop_loadvnode,
    855 	.vfs_fhtovp = puffs_vfsop_fhtovp,
    856 	.vfs_vptofh = puffs_vfsop_vptofh,
    857 	.vfs_init = puffs_vfsop_init,
    858 	.vfs_done = puffs_vfsop_done,
    859 	.vfs_snapshot = puffs_vfsop_snapshot,
    860 	.vfs_extattrctl = puffs_vfsop_extattrctl,
    861 	.vfs_suspendctl = (void *)eopnotsupp,
    862 	.vfs_renamelock_enter = genfs_renamelock_enter,
    863 	.vfs_renamelock_exit = genfs_renamelock_exit,
    864 	.vfs_fsync = (void *)eopnotsupp,
    865 	.vfs_opv_descs = puffs_vnodeopv_descs
    866 };
    867 
    868 static int
    869 puffs_modcmd(modcmd_t cmd, void *arg)
    870 {
    871 
    872 	switch (cmd) {
    873 	case MODULE_CMD_INIT:
    874 		return vfs_attach(&puffs_vfsops);
    875 	case MODULE_CMD_FINI:
    876 		return vfs_detach(&puffs_vfsops);
    877 	default:
    878 		return ENOTTY;
    879 	}
    880 }
    881