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