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