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