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