Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.93
      1 /*	$NetBSD: puffs_vfsops.c,v 1.93 2010/07/06 13:47:47 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.93 2010/07/06 13:47:47 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 	pmp->pmp_docompat = args->pa_time32;
    296 
    297 	mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
    298 	mutex_init(&pmp->pmp_sopmtx, MUTEX_DEFAULT, IPL_NONE);
    299 	cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
    300 	cv_init(&pmp->pmp_refcount_cv, "puffsref");
    301 	cv_init(&pmp->pmp_unmounting_cv, "puffsum");
    302 	cv_init(&pmp->pmp_sopcv, "puffsop");
    303 	TAILQ_INIT(&pmp->pmp_msg_touser);
    304 	TAILQ_INIT(&pmp->pmp_msg_replywait);
    305 	TAILQ_INIT(&pmp->pmp_sopreqs);
    306 
    307 	if ((error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    308 	    puffs_sop_thread, pmp, NULL, "puffsop")) != 0)
    309 		goto out;
    310 	pmp->pmp_sopthrcount = 1;
    311 
    312 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    313 	    mp, MPTOPUFFSMP(mp)));
    314 
    315 	vfs_getnewfsid(mp);
    316 
    317  out:
    318 	if (error && pmp && pmp->pmp_pi)
    319 		putter_detach(pmp->pmp_pi);
    320 	if (error && pmp && pmp->pmp_pnodehash)
    321 		kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash));
    322 	if (error && pmp)
    323 		kmem_free(pmp, sizeof(struct puffs_mount));
    324 	return error;
    325 }
    326 
    327 int
    328 puffs_vfsop_start(struct mount *mp, int flags)
    329 {
    330 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    331 
    332 	KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
    333 	pmp->pmp_status = PUFFSTAT_RUNNING;
    334 
    335 	return 0;
    336 }
    337 
    338 int
    339 puffs_vfsop_unmount(struct mount *mp, int mntflags)
    340 {
    341 	PUFFS_MSG_VARS(vfs, unmount);
    342 	struct puffs_mount *pmp;
    343 	int error, force;
    344 
    345 	error = 0;
    346 	force = mntflags & MNT_FORCE;
    347 	pmp = MPTOPUFFSMP(mp);
    348 
    349 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    350 	    "status 0x%x\n", pmp->pmp_status));
    351 
    352 	/*
    353 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    354 	 * root vnode does not get flushed until unmount.  The
    355 	 * userspace root node cookie is stored in the mount
    356 	 * structure, so we can always re-instantiate a root vnode,
    357 	 * should userspace unmount decide it doesn't want to
    358 	 * cooperate.
    359 	 */
    360 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    361 	if (error)
    362 		goto out;
    363 
    364 	/*
    365 	 * If we are not DYING, we should ask userspace's opinion
    366 	 * about the situation
    367 	 */
    368 	mutex_enter(&pmp->pmp_lock);
    369 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    370 		pmp->pmp_unmounting = 1;
    371 		mutex_exit(&pmp->pmp_lock);
    372 
    373 		PUFFS_MSG_ALLOC(vfs, unmount);
    374 		puffs_msg_setinfo(park_unmount,
    375 		    PUFFSOP_VFS, PUFFS_VFS_UNMOUNT, NULL);
    376 		unmount_msg->pvfsr_flags = mntflags;
    377 
    378 		PUFFS_MSG_ENQUEUEWAIT(pmp, park_unmount, error);
    379 		PUFFS_MSG_RELEASE(unmount);
    380 
    381 		error = checkerr(pmp, error, __func__);
    382 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    383 
    384 		mutex_enter(&pmp->pmp_lock);
    385 		pmp->pmp_unmounting = 0;
    386 		cv_broadcast(&pmp->pmp_unmounting_cv);
    387 	}
    388 
    389 	/*
    390 	 * if userspace cooperated or we really need to die,
    391 	 * screw what userland thinks and just die.
    392 	 */
    393 	if (error == 0 || force) {
    394 		struct puffs_sopreq *psopr;
    395 
    396 		/* tell waiters & other resources to go unwait themselves */
    397 		puffs_userdead(pmp);
    398 		putter_detach(pmp->pmp_pi);
    399 
    400 		/*
    401 		 * Wait until there are no more users for the mount resource.
    402 		 * Notice that this is hooked against transport_close
    403 		 * and return from touser.  In an ideal world, it would
    404 		 * be hooked against final return from all operations.
    405 		 * But currently it works well enough, since nobody
    406 		 * does weird blocking voodoo after return from touser().
    407 		 */
    408 		while (pmp->pmp_refcount != 0)
    409 			cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
    410 		mutex_exit(&pmp->pmp_lock);
    411 
    412 		/*
    413 		 * Release kernel thread now that there is nothing
    414 		 * it would be wanting to lock.
    415 		 */
    416 		psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
    417 		psopr->psopr_sopreq = PUFFS_SOPREQSYS_EXIT;
    418 		mutex_enter(&pmp->pmp_sopmtx);
    419 		if (pmp->pmp_sopthrcount == 0) {
    420 			mutex_exit(&pmp->pmp_sopmtx);
    421 			kmem_free(psopr, sizeof(*psopr));
    422 			mutex_enter(&pmp->pmp_sopmtx);
    423 			KASSERT(pmp->pmp_sopthrcount == 0);
    424 		} else {
    425 			TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs,
    426 			    psopr, psopr_entries);
    427 			cv_signal(&pmp->pmp_sopcv);
    428 		}
    429 		while (pmp->pmp_sopthrcount > 0)
    430 			cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
    431 		mutex_exit(&pmp->pmp_sopmtx);
    432 
    433 		/* free resources now that we hopefully have no waiters left */
    434 		cv_destroy(&pmp->pmp_unmounting_cv);
    435 		cv_destroy(&pmp->pmp_refcount_cv);
    436 		cv_destroy(&pmp->pmp_msg_waiter_cv);
    437 		cv_destroy(&pmp->pmp_sopcv);
    438 		mutex_destroy(&pmp->pmp_lock);
    439 		mutex_destroy(&pmp->pmp_sopmtx);
    440 
    441 		kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash));
    442 		kmem_free(pmp, sizeof(struct puffs_mount));
    443 		error = 0;
    444 	} else {
    445 		mutex_exit(&pmp->pmp_lock);
    446 	}
    447 
    448  out:
    449 	DPRINTF(("puffs_unmount: return %d\n", error));
    450 	return error;
    451 }
    452 
    453 /*
    454  * This doesn't need to travel to userspace
    455  */
    456 int
    457 puffs_vfsop_root(struct mount *mp, struct vnode **vpp)
    458 {
    459 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    460 	int rv;
    461 
    462 	rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, 1, 1, vpp);
    463 	KASSERT(rv != PUFFS_NOSUCHCOOKIE);
    464 	return rv;
    465 }
    466 
    467 int
    468 puffs_vfsop_statvfs(struct mount *mp, struct statvfs *sbp)
    469 {
    470 	PUFFS_MSG_VARS(vfs, statvfs);
    471 	struct puffs_mount *pmp;
    472 	int error = 0;
    473 
    474 	pmp = MPTOPUFFSMP(mp);
    475 
    476 	/*
    477 	 * If we are mounting, it means that the userspace counterpart
    478 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    479 	 * requesting statvfs from userspace would mean a deadlock.
    480 	 * Compensate.
    481 	 */
    482 	if (__predict_false(pmp->pmp_status == PUFFSTAT_MOUNTING))
    483 		return EINPROGRESS;
    484 
    485 	PUFFS_MSG_ALLOC(vfs, statvfs);
    486 	puffs_msg_setinfo(park_statvfs, PUFFSOP_VFS, PUFFS_VFS_STATVFS, NULL);
    487 
    488 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_statvfs, error);
    489 	error = checkerr(pmp, error, __func__);
    490 	statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
    491 
    492 	/*
    493 	 * Try to produce a sensible result even in the event
    494 	 * of userspace error.
    495 	 *
    496 	 * XXX: cache the copy in non-error case
    497 	 */
    498 	if (!error) {
    499 		copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
    500 		(void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
    501 		    sizeof(struct statvfs));
    502 	} else {
    503 		copy_statvfs_info(sbp, mp);
    504 	}
    505 
    506 	PUFFS_MSG_RELEASE(statvfs);
    507 	return error;
    508 }
    509 
    510 static int
    511 pageflush(struct mount *mp, kauth_cred_t cred, int waitfor)
    512 {
    513 	struct puffs_node *pn;
    514 	struct vnode *vp, *mvp;
    515 	int error, rv;
    516 
    517 	error = 0;
    518 
    519 	/* Allocate a marker vnode. */
    520 	if ((mvp = vnalloc(mp)) == NULL)
    521 		return ENOMEM;
    522 
    523 	/*
    524 	 * Sync all cached data from regular vnodes (which are not
    525 	 * currently locked, see below).  After this we call VFS_SYNC
    526 	 * for the fs server, which should handle data and metadata for
    527 	 * all the nodes it knows to exist.
    528 	 */
    529 	mutex_enter(&mntvnode_lock);
    530  loop:
    531 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    532 		vmark(mvp, vp);
    533 		if (vp->v_mount != mp || vismarker(vp))
    534 			continue;
    535 
    536 		mutex_enter(&vp->v_interlock);
    537 		pn = VPTOPP(vp);
    538 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    539 			mutex_exit(&vp->v_interlock);
    540 			continue;
    541 		}
    542 
    543 		mutex_exit(&mntvnode_lock);
    544 
    545 		/*
    546 		 * Here we try to get a reference to the vnode and to
    547 		 * lock it.  This is mostly cargo-culted, but I will
    548 		 * offer an explanation to why I believe this might
    549 		 * actually do the right thing.
    550 		 *
    551 		 * If the vnode is a goner, we quite obviously don't need
    552 		 * to sync it.
    553 		 *
    554 		 * If the vnode was busy, we don't need to sync it because
    555 		 * this is never called with MNT_WAIT except from
    556 		 * dounmount(), when we are wait-flushing all the dirty
    557 		 * vnodes through other routes in any case.  So there,
    558 		 * sync() doesn't actually sync.  Happy now?
    559 		 */
    560 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    561 		if (rv) {
    562 			mutex_enter(&mntvnode_lock);
    563 			if (rv == ENOENT) {
    564 				(void)vunmark(mvp);
    565 				goto loop;
    566 			}
    567 			continue;
    568 		}
    569 
    570 		/* hmm.. is the FAF thing entirely sensible? */
    571 		if (waitfor == MNT_LAZY) {
    572 			mutex_enter(&vp->v_interlock);
    573 			pn->pn_stat |= PNODE_FAF;
    574 			mutex_exit(&vp->v_interlock);
    575 		}
    576 		rv = VOP_FSYNC(vp, cred, waitfor, 0, 0);
    577 		if (waitfor == MNT_LAZY) {
    578 			mutex_enter(&vp->v_interlock);
    579 			pn->pn_stat &= ~PNODE_FAF;
    580 			mutex_exit(&vp->v_interlock);
    581 		}
    582 		if (rv)
    583 			error = rv;
    584 		vput(vp);
    585 		mutex_enter(&mntvnode_lock);
    586 	}
    587 	mutex_exit(&mntvnode_lock);
    588 	vnfree(mvp);
    589 
    590 	return error;
    591 }
    592 
    593 int
    594 puffs_vfsop_sync(struct mount *mp, int waitfor, struct kauth_cred *cred)
    595 {
    596 	PUFFS_MSG_VARS(vfs, sync);
    597 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    598 	int error, rv;
    599 
    600 	error = pageflush(mp, cred, waitfor);
    601 
    602 	/* sync fs */
    603 	PUFFS_MSG_ALLOC(vfs, sync);
    604 	sync_msg->pvfsr_waitfor = waitfor;
    605 	puffs_credcvt(&sync_msg->pvfsr_cred, cred);
    606 	puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL);
    607 
    608 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv);
    609 	rv = checkerr(pmp, rv, __func__);
    610 	if (rv)
    611 		error = rv;
    612 
    613 	PUFFS_MSG_RELEASE(sync);
    614 	return error;
    615 }
    616 
    617 int
    618 puffs_vfsop_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    619 {
    620 	PUFFS_MSG_VARS(vfs, fhtonode);
    621 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    622 	struct vnode *vp;
    623 	void *fhdata;
    624 	size_t argsize, fhlen;
    625 	int error;
    626 
    627 	if (pmp->pmp_args.pa_fhsize == 0)
    628 		return EOPNOTSUPP;
    629 
    630 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    631 		fhlen = fhp->fid_len;
    632 		fhdata = fhp;
    633 	} else {
    634 		fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
    635 		fhdata = fhp->fid_data;
    636 
    637 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    638 			if (pmp->pmp_args.pa_fhsize < fhlen)
    639 				return EINVAL;
    640 		} else {
    641 			if (pmp->pmp_args.pa_fhsize != fhlen)
    642 				return EINVAL;
    643 		}
    644 	}
    645 
    646 	argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
    647 	puffs_msgmem_alloc(argsize, &park_fhtonode, (void *)&fhtonode_msg, 1);
    648 	fhtonode_msg->pvfsr_dsize = fhlen;
    649 	memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
    650 	puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL);
    651 
    652 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error);
    653 	error = checkerr(pmp, error, __func__);
    654 	if (error)
    655 		goto out;
    656 
    657 	error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp);
    658 	DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
    659 	    fhtonode_msg->pvfsr_fhcookie, vp));
    660 	if (error == PUFFS_NOSUCHCOOKIE) {
    661 		error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
    662 		    fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
    663 		    fhtonode_msg->pvfsr_rdev, &vp);
    664 		if (error)
    665 			goto out;
    666 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    667 	} else if (error) {
    668 		goto out;
    669 	}
    670 
    671 	*vpp = vp;
    672  out:
    673 	puffs_msgmem_release(park_fhtonode);
    674 	return error;
    675 }
    676 
    677 int
    678 puffs_vfsop_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    679 {
    680 	PUFFS_MSG_VARS(vfs, nodetofh);
    681 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    682 	size_t argsize, fhlen;
    683 	int error;
    684 
    685 	if (pmp->pmp_args.pa_fhsize == 0)
    686 		return EOPNOTSUPP;
    687 
    688 	/* if file handles are static len, we can test len immediately */
    689 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    690 	    && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
    691 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    692 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    693 		return E2BIG;
    694 	}
    695 
    696 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    697 		fhlen = *fh_size;
    698 	else
    699 		fhlen = PUFFS_FROMFHSIZE(*fh_size);
    700 
    701 	argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
    702 	puffs_msgmem_alloc(argsize, &park_nodetofh, (void *)&nodetofh_msg, 1);
    703 	nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
    704 	nodetofh_msg->pvfsr_dsize = fhlen;
    705 	puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL);
    706 
    707 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error);
    708 	error = checkerr(pmp, error, __func__);
    709 
    710 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    711 		fhlen = nodetofh_msg->pvfsr_dsize;
    712 	else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
    713 		fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
    714 	else
    715 		fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    716 
    717 	if (error) {
    718 		if (error == E2BIG)
    719 			*fh_size = fhlen;
    720 		goto out;
    721 	}
    722 
    723 	if (fhlen > FHANDLE_SIZE_MAX) {
    724 		puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG,
    725 		    "file handle too big", VPTOPNC(vp));
    726 		error = EPROTO;
    727 		goto out;
    728 	}
    729 
    730 	if (*fh_size < fhlen) {
    731 		*fh_size = fhlen;
    732 		error = E2BIG;
    733 		goto out;
    734 	}
    735 	*fh_size = fhlen;
    736 
    737 	if (fhp) {
    738 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    739 			memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
    740 		} else {
    741 			fhp->fid_len = *fh_size;
    742 			memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
    743 			    nodetofh_msg->pvfsr_dsize);
    744 		}
    745 	}
    746 
    747  out:
    748 	puffs_msgmem_release(park_nodetofh);
    749 	return error;
    750 }
    751 
    752 void
    753 puffs_vfsop_init(void)
    754 {
    755 
    756 	/* some checks depend on this */
    757 	KASSERT(VNOVAL == VSIZENOTSET);
    758 
    759 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    760 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    761 	puffs_msgif_init();
    762 }
    763 
    764 void
    765 puffs_vfsop_done(void)
    766 {
    767 
    768 	puffs_msgif_destroy();
    769 	pool_destroy(&puffs_pnpool);
    770 }
    771 
    772 int
    773 puffs_vfsop_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    774 {
    775 
    776 	return EOPNOTSUPP;
    777 }
    778 
    779 int
    780 puffs_vfsop_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
    781 	int attrnamespace, const char *attrname)
    782 {
    783 	PUFFS_MSG_VARS(vfs, extattrctl);
    784 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    785 	struct puffs_node *pnp;
    786 	puffs_cookie_t pnc;
    787 	int error, flags;
    788 
    789 	if (vp) {
    790 		/* doesn't make sense for puffs servers */
    791 		if (vp->v_mount != mp)
    792 			return EXDEV;
    793 		pnp = vp->v_data;
    794 		pnc = pnp->pn_cookie;
    795 		flags = PUFFS_EXTATTRCTL_HASNODE;
    796 	} else {
    797 		pnp = pnc = NULL;
    798 		flags = 0;
    799 	}
    800 
    801 	PUFFS_MSG_ALLOC(vfs, extattrctl);
    802 	extattrctl_msg->pvfsr_cmd = cmd;
    803 	extattrctl_msg->pvfsr_attrnamespace = attrnamespace;
    804 	extattrctl_msg->pvfsr_flags = flags;
    805 	if (attrname) {
    806 		strlcpy(extattrctl_msg->pvfsr_attrname, attrname,
    807 		    sizeof(extattrctl_msg->pvfsr_attrname));
    808 		extattrctl_msg->pvfsr_flags |= PUFFS_EXTATTRCTL_HASATTRNAME;
    809 	}
    810 	puffs_msg_setinfo(park_extattrctl,
    811 	    PUFFSOP_VFS, PUFFS_VFS_EXTATTRCTL, pnc);
    812 
    813 	puffs_msg_enqueue(pmp, park_extattrctl);
    814 	if (vp) {
    815 		mutex_enter(&pnp->pn_mtx);
    816 		puffs_referencenode(pnp);
    817 		mutex_exit(&pnp->pn_mtx);
    818 		VOP_UNLOCK(vp);
    819 	}
    820 	error = puffs_msg_wait2(pmp, park_extattrctl, pnp, NULL);
    821 	PUFFS_MSG_RELEASE(extattrctl);
    822 	if (vp) {
    823 		puffs_releasenode(pnp);
    824 	}
    825 
    826 	return checkerr(pmp, error, __func__);
    827 }
    828 
    829 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    830 	&puffs_vnodeop_opv_desc,
    831 	&puffs_specop_opv_desc,
    832 	&puffs_fifoop_opv_desc,
    833 	&puffs_msgop_opv_desc,
    834 	NULL,
    835 };
    836 
    837 struct vfsops puffs_vfsops = {
    838 	MOUNT_PUFFS,
    839 	sizeof (struct puffs_kargs),
    840 	puffs_vfsop_mount,		/* mount	*/
    841 	puffs_vfsop_start,		/* start	*/
    842 	puffs_vfsop_unmount,		/* unmount	*/
    843 	puffs_vfsop_root,		/* root		*/
    844 	(void *)eopnotsupp,		/* quotactl	*/
    845 	puffs_vfsop_statvfs,		/* statvfs	*/
    846 	puffs_vfsop_sync,		/* sync		*/
    847 	(void *)eopnotsupp,		/* vget		*/
    848 	puffs_vfsop_fhtovp,		/* fhtovp	*/
    849 	puffs_vfsop_vptofh,		/* vptofh	*/
    850 	puffs_vfsop_init,		/* init		*/
    851 	NULL,				/* reinit	*/
    852 	puffs_vfsop_done,		/* done		*/
    853 	NULL,				/* mountroot	*/
    854 	puffs_vfsop_snapshot,		/* snapshot	*/
    855 	puffs_vfsop_extattrctl,		/* extattrctl	*/
    856 	(void *)eopnotsupp,		/* suspendctl	*/
    857 	genfs_renamelock_enter,
    858 	genfs_renamelock_exit,
    859 	(void *)eopnotsupp,
    860 	puffs_vnodeopv_descs,		/* vnodeops	*/
    861 	0,				/* refcount	*/
    862 	{ NULL, NULL }
    863 };
    864 
    865 static int
    866 puffs_modcmd(modcmd_t cmd, void *arg)
    867 {
    868 
    869 	switch (cmd) {
    870 	case MODULE_CMD_INIT:
    871 		return vfs_attach(&puffs_vfsops);
    872 	case MODULE_CMD_FINI:
    873 		return vfs_detach(&puffs_vfsops);
    874 	default:
    875 		return ENOTTY;
    876 	}
    877 }
    878