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