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