etc/protocols - sync with NetBSD-8
[minix.git] / sys / fs / puffs / puffs_vfsops.c
blobf30f6f4e30c141cef0f99ad06d22cfcd19485886
1 /* $NetBSD: puffs_vfsops.c,v 1.117 2015/02/16 10:49:39 martin Exp $ */
3 /*
4 * Copyright (c) 2005, 2006 Antti Kantee. All Rights Reserved.
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.
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.117 2015/02/16 10:49:39 martin Exp $");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/mount.h>
38 #include <sys/extattr.h>
39 #include <sys/queue.h>
40 #include <sys/vnode.h>
41 #include <sys/dirent.h>
42 #include <sys/kauth.h>
43 #include <sys/proc.h>
44 #include <sys/module.h>
45 #include <sys/kthread.h>
47 #include <uvm/uvm.h>
49 #include <dev/putter/putter_sys.h>
51 #include <miscfs/genfs/genfs.h>
53 #include <fs/puffs/puffs_msgif.h>
54 #include <fs/puffs/puffs_sys.h>
56 #include <lib/libkern/libkern.h>
58 #include <nfs/nfsproto.h> /* for fh sizes */
60 MODULE(MODULE_CLASS_VFS, puffs, "putter");
62 VFS_PROTOS(puffs_vfsop);
64 static struct putter_ops puffs_putter = {
65 .pop_getout = puffs_msgif_getout,
66 .pop_releaseout = puffs_msgif_releaseout,
67 .pop_waitcount = puffs_msgif_waitcount,
68 .pop_dispatch = puffs_msgif_dispatch,
69 .pop_close = puffs_msgif_close,
72 static const struct genfs_ops puffs_genfsops = {
73 .gop_size = puffs_gop_size,
74 .gop_write = genfs_gop_write,
75 .gop_markupdate = puffs_gop_markupdate,
76 #if 0
77 .gop_alloc, should ask userspace
78 #endif
82 * Try to ensure data structures used by the puffs protocol
83 * do not unexpectedly change.
85 #if defined(__i386__) && defined(__ELF__)
86 CTASSERT(sizeof(struct puffs_kargs) == 3928);
87 CTASSERT(sizeof(struct vattr) == 136);
88 CTASSERT(sizeof(struct puffs_req) == 44);
89 #endif
91 int
92 puffs_vfsop_mount(struct mount *mp, const char *path, void *data,
93 size_t *data_len)
95 struct puffs_mount *pmp = NULL;
96 struct puffs_kargs *args;
97 char fstype[_VFS_NAMELEN];
98 char *p;
99 int error = 0, i;
100 pid_t mntpid = curlwp->l_proc->p_pid;
102 if (data == NULL)
103 return EINVAL;
104 if (*data_len < sizeof *args)
105 return EINVAL;
107 if (mp->mnt_flag & MNT_GETARGS) {
108 pmp = MPTOPUFFSMP(mp);
109 *(struct puffs_kargs *)data = pmp->pmp_args;
110 *data_len = sizeof *args;
111 return 0;
114 /* update is not supported currently */
115 if (mp->mnt_flag & MNT_UPDATE)
116 return EOPNOTSUPP;
118 args = (struct puffs_kargs *)data;
120 if (args->pa_vers != PUFFSVERSION) {
121 printf("puffs_mount: development version mismatch: "
122 "kernel %d, lib %d\n", PUFFSVERSION, args->pa_vers);
123 error = EINVAL;
124 goto out;
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;
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;
138 for (i = 0; i < __arraycount(args->pa_spare); i++) {
139 if (args->pa_spare[i] != 0) {
140 printf("puffs_mount: pa_spare[%d] = 0x%x\n",
141 i, args->pa_spare[i]);
142 error = EINVAL;
143 goto out;
147 /* use dummy value for passthrough */
148 if (args->pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
149 args->pa_fhsize = sizeof(struct fid);
151 /* sanitize file handle length */
152 if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) {
153 printf("puffs_mount: handle size %zu too large\n",
154 args->pa_fhsize);
155 error = EINVAL;
156 goto out;
158 /* sanity check file handle max sizes */
159 if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) {
160 size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize);
162 if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) {
163 if (NFSX_FHTOOBIG_P(kfhsize, 0)) {
164 printf("puffs_mount: fhsize larger than "
165 "NFSv2 max %d\n",
166 PUFFS_FROMFHSIZE(NFSX_V2FH));
167 error = EINVAL;
168 goto out;
172 if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) {
173 if (NFSX_FHTOOBIG_P(kfhsize, 1)) {
174 printf("puffs_mount: fhsize larger than "
175 "NFSv3 max %d\n",
176 PUFFS_FROMFHSIZE(NFSX_V3FHMAX));
177 error = EINVAL;
178 goto out;
183 /* don't allow non-printing characters (like my sweet umlauts.. snif) */
184 args->pa_typename[sizeof(args->pa_typename)-1] = '\0';
185 for (p = args->pa_typename; *p; p++)
186 if (*p < ' ' || *p > '~')
187 *p = '.';
189 args->pa_mntfromname[sizeof(args->pa_mntfromname)-1] = '\0';
190 for (p = args->pa_mntfromname; *p; p++)
191 if (*p < ' ' || *p > '~')
192 *p = '.';
194 /* build real name */
195 (void)strlcpy(fstype, PUFFS_TYPEPREFIX, sizeof(fstype));
196 (void)strlcat(fstype, args->pa_typename, sizeof(fstype));
198 /* inform user server if it got the max request size it wanted */
199 if (args->pa_maxmsglen == 0 || args->pa_maxmsglen > PUFFS_MSG_MAXSIZE)
200 args->pa_maxmsglen = PUFFS_MSG_MAXSIZE;
201 else if (args->pa_maxmsglen < 2*PUFFS_MSGSTRUCT_MAX)
202 args->pa_maxmsglen = 2*PUFFS_MSGSTRUCT_MAX;
204 (void)strlcpy(args->pa_typename, fstype, sizeof(args->pa_typename));
206 error = set_statvfs_info(path, UIO_USERSPACE, args->pa_mntfromname,
207 UIO_SYSSPACE, fstype, mp, curlwp);
208 if (error)
209 goto out;
210 mp->mnt_stat.f_iosize = DEV_BSIZE;
211 mp->mnt_stat.f_namemax = args->pa_svfsb.f_namemax;
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.
218 copy_statvfs_info(&args->pa_svfsb, mp);
219 (void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat));
221 KASSERT(curlwp != uvm.pagedaemon_lwp);
222 pmp = kmem_zalloc(sizeof(struct puffs_mount), KM_SLEEP);
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;
229 #if 0
231 * XXX: puffs code is MPSAFE. However, VFS really isn't.
232 * Currently, there is nothing which protects an inode from
233 * reclaim while there are threads inside the file system.
234 * This means that in the event of a server crash, an MPSAFE
235 * mount is likely to end up accessing invalid memory. For the
236 * non-mpsafe case, the kernel lock, general structure of
237 * puffs and pmp_refcount protect the threads during escape.
239 * Fixing this will require:
240 * a) fixing vfs
241 * OR
242 * b) adding a small sleep to puffs_msgif_close() between
243 * userdead() and dounmount().
244 * (well, this isn't really a fix, but would solve
245 * 99.999% of the race conditions).
247 * Also, in the event of "b", unmount -f should be used,
248 * like with any other file system, sparingly and only when
249 * it is "known" to be safe.
251 mp->mnt_iflags |= IMNT_MPSAFE;
252 #endif
254 pmp->pmp_status = PUFFSTAT_MOUNTING;
255 pmp->pmp_mp = mp;
256 pmp->pmp_msg_maxsize = args->pa_maxmsglen;
257 pmp->pmp_args = *args;
260 * Inform the fileops processing code that we have a mountpoint.
261 * If it doesn't know about anyone with our pid/fd having the
262 * device open, punt
264 if ((pmp->pmp_pi
265 = putter_attach(mntpid, args->pa_fd, pmp, &puffs_putter)) == NULL) {
266 error = ENOENT;
267 goto out;
270 /* XXX: check parameters */
271 pmp->pmp_root_cookie = args->pa_root_cookie;
272 pmp->pmp_root_vtype = args->pa_root_vtype;
273 pmp->pmp_root_vsize = args->pa_root_vsize;
274 pmp->pmp_root_rdev = args->pa_root_rdev;
275 pmp->pmp_docompat = args->pa_time32;
277 mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
278 mutex_init(&pmp->pmp_sopmtx, MUTEX_DEFAULT, IPL_NONE);
279 cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
280 cv_init(&pmp->pmp_refcount_cv, "puffsref");
281 cv_init(&pmp->pmp_unmounting_cv, "puffsum");
282 cv_init(&pmp->pmp_sopcv, "puffsop");
283 TAILQ_INIT(&pmp->pmp_msg_touser);
284 TAILQ_INIT(&pmp->pmp_msg_replywait);
285 TAILQ_INIT(&pmp->pmp_sopfastreqs);
286 TAILQ_INIT(&pmp->pmp_sopnodereqs);
288 if ((error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
289 puffs_sop_thread, pmp, NULL, "puffsop")) != 0)
290 goto out;
291 pmp->pmp_sopthrcount = 1;
293 DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
294 mp, MPTOPUFFSMP(mp)));
296 vfs_getnewfsid(mp);
298 out:
299 if (error && pmp && pmp->pmp_pi)
300 putter_detach(pmp->pmp_pi);
301 if (error && pmp)
302 kmem_free(pmp, sizeof(struct puffs_mount));
303 return error;
307 puffs_vfsop_start(struct mount *mp, int flags)
309 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
311 KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
312 pmp->pmp_status = PUFFSTAT_RUNNING;
314 return 0;
318 puffs_vfsop_unmount(struct mount *mp, int mntflags)
320 PUFFS_MSG_VARS(vfs, unmount);
321 struct puffs_mount *pmp;
322 int error, force;
324 error = 0;
325 force = mntflags & MNT_FORCE;
326 pmp = MPTOPUFFSMP(mp);
328 DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
329 "status 0x%x\n", pmp->pmp_status));
332 * flush all the vnodes. VOP_RECLAIM() takes care that the
333 * root vnode does not get flushed until unmount. The
334 * userspace root node cookie is stored in the mount
335 * structure, so we can always re-instantiate a root vnode,
336 * should userspace unmount decide it doesn't want to
337 * cooperate.
339 error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
340 if (error)
341 goto out;
344 * If we are not DYING, we should ask userspace's opinion
345 * about the situation
347 mutex_enter(&pmp->pmp_lock);
348 if (pmp->pmp_status != PUFFSTAT_DYING) {
349 pmp->pmp_unmounting = 1;
350 mutex_exit(&pmp->pmp_lock);
352 PUFFS_MSG_ALLOC(vfs, unmount);
353 puffs_msg_setinfo(park_unmount,
354 PUFFSOP_VFS, PUFFS_VFS_UNMOUNT, NULL);
355 unmount_msg->pvfsr_flags = mntflags;
357 PUFFS_MSG_ENQUEUEWAIT(pmp, park_unmount, error);
358 PUFFS_MSG_RELEASE(unmount);
360 error = checkerr(pmp, error, __func__);
361 DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
363 mutex_enter(&pmp->pmp_lock);
364 pmp->pmp_unmounting = 0;
365 cv_broadcast(&pmp->pmp_unmounting_cv);
369 * if userspace cooperated or we really need to die,
370 * screw what userland thinks and just die.
372 if (error == 0 || force) {
373 struct puffs_sopreq *psopr;
375 /* tell waiters & other resources to go unwait themselves */
376 puffs_userdead(pmp);
377 putter_detach(pmp->pmp_pi);
380 * Wait until there are no more users for the mount resource.
381 * Notice that this is hooked against transport_close
382 * and return from touser. In an ideal world, it would
383 * be hooked against final return from all operations.
384 * But currently it works well enough, since nobody
385 * does weird blocking voodoo after return from touser().
387 while (pmp->pmp_refcount != 0)
388 cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
389 mutex_exit(&pmp->pmp_lock);
392 * Release kernel thread now that there is nothing
393 * it would be wanting to lock.
395 KASSERT(curlwp != uvm.pagedaemon_lwp);
396 psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
397 psopr->psopr_sopreq = PUFFS_SOPREQSYS_EXIT;
398 mutex_enter(&pmp->pmp_sopmtx);
399 if (pmp->pmp_sopthrcount == 0) {
400 mutex_exit(&pmp->pmp_sopmtx);
401 kmem_free(psopr, sizeof(*psopr));
402 mutex_enter(&pmp->pmp_sopmtx);
403 KASSERT(pmp->pmp_sopthrcount == 0);
404 } else {
405 TAILQ_INSERT_TAIL(&pmp->pmp_sopfastreqs,
406 psopr, psopr_entries);
407 cv_signal(&pmp->pmp_sopcv);
409 while (pmp->pmp_sopthrcount > 0)
410 cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
411 mutex_exit(&pmp->pmp_sopmtx);
413 /* free resources now that we hopefully have no waiters left */
414 cv_destroy(&pmp->pmp_unmounting_cv);
415 cv_destroy(&pmp->pmp_refcount_cv);
416 cv_destroy(&pmp->pmp_msg_waiter_cv);
417 cv_destroy(&pmp->pmp_sopcv);
418 mutex_destroy(&pmp->pmp_lock);
419 mutex_destroy(&pmp->pmp_sopmtx);
421 kmem_free(pmp, sizeof(struct puffs_mount));
422 error = 0;
423 } else {
424 mutex_exit(&pmp->pmp_lock);
427 out:
428 DPRINTF(("puffs_unmount: return %d\n", error));
429 return error;
433 * This doesn't need to travel to userspace
436 puffs_vfsop_root(struct mount *mp, struct vnode **vpp)
438 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
439 int rv;
441 rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, vpp);
442 KASSERT(rv != PUFFS_NOSUCHCOOKIE);
443 if (rv != 0)
444 return rv;
445 rv = vn_lock(*vpp, LK_EXCLUSIVE);
446 if (rv != 0) {
447 vrele(*vpp);
448 *vpp = NULL;
449 return rv;
451 return 0;
455 puffs_vfsop_statvfs(struct mount *mp, struct statvfs *sbp)
457 PUFFS_MSG_VARS(vfs, statvfs);
458 struct puffs_mount *pmp;
459 int error = 0;
461 pmp = MPTOPUFFSMP(mp);
464 * If we are mounting, it means that the userspace counterpart
465 * is calling mount(2), but mount(2) also calls statvfs. So
466 * requesting statvfs from userspace would mean a deadlock.
467 * Compensate.
469 if (__predict_false(pmp->pmp_status == PUFFSTAT_MOUNTING))
470 return EINPROGRESS;
472 PUFFS_MSG_ALLOC(vfs, statvfs);
473 puffs_msg_setinfo(park_statvfs, PUFFSOP_VFS, PUFFS_VFS_STATVFS, NULL);
475 PUFFS_MSG_ENQUEUEWAIT(pmp, park_statvfs, error);
476 error = checkerr(pmp, error, __func__);
477 statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
480 * Try to produce a sensible result even in the event
481 * of userspace error.
483 * XXX: cache the copy in non-error case
485 if (!error) {
486 copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
487 (void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
488 sizeof(struct statvfs));
489 } else {
490 copy_statvfs_info(sbp, mp);
493 PUFFS_MSG_RELEASE(statvfs);
494 return error;
497 static bool
498 pageflush_selector(void *cl, struct vnode *vp)
500 return vp->v_type == VREG &&
501 !(LIST_EMPTY(&vp->v_dirtyblkhd) && UVM_OBJ_IS_CLEAN(&vp->v_uobj));
504 static int
505 pageflush(struct mount *mp, kauth_cred_t cred, int waitfor)
507 struct puffs_node *pn;
508 struct vnode *vp;
509 struct vnode_iterator *marker;
510 int error, rv, fsyncwait;
512 error = 0;
513 fsyncwait = (waitfor == MNT_WAIT) ? FSYNC_WAIT : 0;
516 * Sync all cached data from regular vnodes (which are not
517 * currently locked, see below). After this we call VFS_SYNC
518 * for the fs server, which should handle data and metadata for
519 * all the nodes it knows to exist.
521 vfs_vnode_iterator_init(mp, &marker);
522 while ((vp = vfs_vnode_iterator_next(marker, pageflush_selector,
523 NULL)))
526 * Here we try to get a reference to the vnode and to
527 * lock it. This is mostly cargo-culted, but I will
528 * offer an explanation to why I believe this might
529 * actually do the right thing.
531 * If the vnode is a goner, we quite obviously don't need
532 * to sync it.
534 * If the vnode was busy, we don't need to sync it because
535 * this is never called with MNT_WAIT except from
536 * dounmount(), when we are wait-flushing all the dirty
537 * vnodes through other routes in any case. So there,
538 * sync() doesn't actually sync. Happy now?
540 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
541 if (error) {
542 vrele(vp);
543 continue;
545 pn = VPTOPP(vp);
546 /* hmm.. is the FAF thing entirely sensible? */
547 if (waitfor == MNT_LAZY) {
548 mutex_enter(vp->v_interlock);
549 pn->pn_stat |= PNODE_FAF;
550 mutex_exit(vp->v_interlock);
552 rv = VOP_FSYNC(vp, cred, fsyncwait, 0, 0);
553 if (waitfor == MNT_LAZY) {
554 mutex_enter(vp->v_interlock);
555 pn->pn_stat &= ~PNODE_FAF;
556 mutex_exit(vp->v_interlock);
558 if (rv)
559 error = rv;
560 vput(vp);
562 vfs_vnode_iterator_destroy(marker);
564 return error;
568 puffs_vfsop_sync(struct mount *mp, int waitfor, struct kauth_cred *cred)
570 PUFFS_MSG_VARS(vfs, sync);
571 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
572 int error, rv;
574 error = pageflush(mp, cred, waitfor);
576 /* sync fs */
577 PUFFS_MSG_ALLOC(vfs, sync);
578 sync_msg->pvfsr_waitfor = waitfor;
579 puffs_credcvt(&sync_msg->pvfsr_cred, cred);
580 puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL);
582 PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv);
583 rv = checkerr(pmp, rv, __func__);
584 if (rv)
585 error = rv;
587 PUFFS_MSG_RELEASE(sync);
588 return error;
592 puffs_vfsop_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
594 PUFFS_MSG_VARS(vfs, fhtonode);
595 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
596 struct vnode *vp;
597 void *fhdata;
598 size_t argsize, fhlen;
599 int error;
601 if (pmp->pmp_args.pa_fhsize == 0)
602 return EOPNOTSUPP;
604 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
605 fhlen = fhp->fid_len;
606 fhdata = fhp;
607 } else {
608 fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
609 fhdata = fhp->fid_data;
611 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
612 if (pmp->pmp_args.pa_fhsize < fhlen)
613 return EINVAL;
614 } else {
615 if (pmp->pmp_args.pa_fhsize != fhlen)
616 return EINVAL;
620 argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
621 puffs_msgmem_alloc(argsize, &park_fhtonode, (void *)&fhtonode_msg, 1);
622 fhtonode_msg->pvfsr_dsize = fhlen;
623 memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
624 puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL);
626 PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error);
627 error = checkerr(pmp, error, __func__);
628 if (error)
629 goto out;
631 error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
632 fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
633 fhtonode_msg->pvfsr_rdev, &vp);
634 if (error)
635 goto out;
636 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
638 *vpp = vp;
639 out:
640 puffs_msgmem_release(park_fhtonode);
641 return error;
645 puffs_vfsop_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
647 PUFFS_MSG_VARS(vfs, nodetofh);
648 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
649 size_t argsize, fhlen;
650 int error;
652 if (pmp->pmp_args.pa_fhsize == 0)
653 return EOPNOTSUPP;
655 /* if file handles are static len, we can test len immediately */
656 if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
657 && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
658 && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
659 *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
660 return E2BIG;
663 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
664 fhlen = *fh_size;
665 else
666 fhlen = PUFFS_FROMFHSIZE(*fh_size);
668 argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
669 puffs_msgmem_alloc(argsize, &park_nodetofh, (void *)&nodetofh_msg, 1);
670 nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
671 nodetofh_msg->pvfsr_dsize = fhlen;
672 puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL);
674 PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error);
675 error = checkerr(pmp, error, __func__);
677 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
678 fhlen = nodetofh_msg->pvfsr_dsize;
679 else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
680 fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
681 else
682 fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
684 if (error) {
685 if (error == E2BIG)
686 *fh_size = fhlen;
687 goto out;
690 if (fhlen > FHANDLE_SIZE_MAX) {
691 puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG,
692 "file handle too big", VPTOPNC(vp));
693 error = EPROTO;
694 goto out;
697 if (*fh_size < fhlen) {
698 *fh_size = fhlen;
699 error = E2BIG;
700 goto out;
702 *fh_size = fhlen;
704 if (fhp) {
705 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
706 memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
707 } else {
708 fhp->fid_len = *fh_size;
709 memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
710 nodetofh_msg->pvfsr_dsize);
714 out:
715 puffs_msgmem_release(park_nodetofh);
716 return error;
720 puffs_vfsop_loadvnode(struct mount *mp, struct vnode *vp,
721 const void *key, size_t key_len, const void **new_key)
723 struct puffs_mount *pmp;
724 struct puffs_node *pnode;
726 KASSERT(key_len == sizeof(puffs_cookie_t));
728 pmp = MPTOPUFFSMP(mp);
730 /* Allocate and initialize the pnode. */
731 pnode = pool_get(&puffs_pnpool, PR_WAITOK);
732 memset(pnode, 0, sizeof(struct puffs_node));
734 pnode->pn_vp = vp;
735 memcpy(&pnode->pn_cookie, key, key_len);
736 pnode->pn_refcount = 1;
737 mutex_init(&pnode->pn_mtx, MUTEX_DEFAULT, IPL_NONE);
738 mutex_init(&pnode->pn_sizemtx, MUTEX_DEFAULT, IPL_NONE);
739 selinit(&pnode->pn_sel);
740 vp->v_tag = VT_PUFFS;
741 vp->v_type = VNON;
742 vp->v_op = puffs_vnodeop_p;
743 if (pnode->pn_cookie == pmp->pmp_root_cookie)
744 vp->v_vflag |= VV_ROOT;
745 vp->v_data = pnode;
747 genfs_node_init(vp, &puffs_genfsops);
748 uvm_vnp_setsize(vp, 0);
750 *new_key = &pnode->pn_cookie;
751 return 0;
754 void
755 puffs_vfsop_init(void)
758 /* some checks depend on this */
759 KASSERT(VNOVAL == VSIZENOTSET);
761 pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
762 "puffpnpl", &pool_allocator_nointr, IPL_NONE);
763 pool_init(&puffs_vapool, sizeof(struct vattr), 0, 0, 0,
764 "puffvapl", &pool_allocator_nointr, IPL_NONE);
765 puffs_msgif_init();
768 void
769 puffs_vfsop_done(void)
772 puffs_msgif_destroy();
773 pool_destroy(&puffs_pnpool);
774 pool_destroy(&puffs_vapool);
778 puffs_vfsop_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
781 return EOPNOTSUPP;
785 puffs_vfsop_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
786 int attrnamespace, const char *attrname)
788 PUFFS_MSG_VARS(vfs, extattrctl);
789 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
790 struct puffs_node *pnp;
791 puffs_cookie_t pnc;
792 int error, flags;
794 if (vp) {
795 /* doesn't make sense for puffs servers */
796 if (vp->v_mount != mp)
797 return EXDEV;
798 pnp = vp->v_data;
799 pnc = pnp->pn_cookie;
800 flags = PUFFS_EXTATTRCTL_HASNODE;
801 } else {
802 pnp = pnc = NULL;
803 flags = 0;
806 PUFFS_MSG_ALLOC(vfs, extattrctl);
807 extattrctl_msg->pvfsr_cmd = cmd;
808 extattrctl_msg->pvfsr_attrnamespace = attrnamespace;
809 extattrctl_msg->pvfsr_flags = flags;
810 if (attrname) {
811 strlcpy(extattrctl_msg->pvfsr_attrname, attrname,
812 sizeof(extattrctl_msg->pvfsr_attrname));
813 extattrctl_msg->pvfsr_flags |= PUFFS_EXTATTRCTL_HASATTRNAME;
815 puffs_msg_setinfo(park_extattrctl,
816 PUFFSOP_VFS, PUFFS_VFS_EXTATTRCTL, pnc);
818 puffs_msg_enqueue(pmp, park_extattrctl);
819 if (vp) {
820 mutex_enter(&pnp->pn_mtx);
821 puffs_referencenode(pnp);
822 mutex_exit(&pnp->pn_mtx);
823 VOP_UNLOCK(vp);
825 error = puffs_msg_wait2(pmp, park_extattrctl, pnp, NULL);
826 PUFFS_MSG_RELEASE(extattrctl);
827 if (vp) {
828 puffs_releasenode(pnp);
831 return checkerr(pmp, error, __func__);
834 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
835 &puffs_vnodeop_opv_desc,
836 &puffs_specop_opv_desc,
837 &puffs_fifoop_opv_desc,
838 &puffs_msgop_opv_desc,
839 NULL,
842 struct vfsops puffs_vfsops = {
843 .vfs_name = MOUNT_PUFFS,
844 .vfs_min_mount_data = sizeof (struct puffs_kargs),
845 .vfs_mount = puffs_vfsop_mount,
846 .vfs_start = puffs_vfsop_start,
847 .vfs_unmount = puffs_vfsop_unmount,
848 .vfs_root = puffs_vfsop_root,
849 .vfs_quotactl = (void *)eopnotsupp,
850 .vfs_statvfs = puffs_vfsop_statvfs,
851 .vfs_sync = puffs_vfsop_sync,
852 .vfs_vget = (void *)eopnotsupp,
853 .vfs_loadvnode = puffs_vfsop_loadvnode,
854 .vfs_fhtovp = puffs_vfsop_fhtovp,
855 .vfs_vptofh = puffs_vfsop_vptofh,
856 .vfs_init = puffs_vfsop_init,
857 .vfs_done = puffs_vfsop_done,
858 .vfs_snapshot = puffs_vfsop_snapshot,
859 .vfs_extattrctl = puffs_vfsop_extattrctl,
860 .vfs_suspendctl = (void *)eopnotsupp,
861 .vfs_renamelock_enter = genfs_renamelock_enter,
862 .vfs_renamelock_exit = genfs_renamelock_exit,
863 .vfs_fsync = (void *)eopnotsupp,
864 .vfs_opv_descs = puffs_vnodeopv_descs
867 static int
868 puffs_modcmd(modcmd_t cmd, void *arg)
871 switch (cmd) {
872 case MODULE_CMD_INIT:
873 return vfs_attach(&puffs_vfsops);
874 case MODULE_CMD_FINI:
875 return vfs_detach(&puffs_vfsops);
876 default:
877 return ENOTTY;