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