puffs_vfsops.c revision 1.87 1 /* $NetBSD: puffs_vfsops.c,v 1.87 2010/02/17 14:32:08 pooka 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.87 2010/02/17 14:32:08 pooka 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/proc.h>
44 #include <sys/module.h>
45 #include <sys/kthread.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 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
225 #if 0
226 /*
227 * XXX: puffs code is MPSAFE. However, VFS really isn't.
228 * Currently, there is nothing which protects an inode from
229 * reclaim while there are threads inside the file system.
230 * This means that in the event of a server crash, an MPSAFE
231 * mount is likely to end up accessing invalid memory. For the
232 * non-mpsafe case, the kernel lock, general structure of
233 * puffs and pmp_refcount protect the threads during escape.
234 *
235 * Fixing this will require:
236 * a) fixing vfs
237 * OR
238 * b) adding a small sleep to puffs_msgif_close() between
239 * userdead() and dounmount().
240 * (well, this isn't really a fix, but would solve
241 * 99.999% of the race conditions).
242 *
243 * Also, in the event of "b", unmount -f should be used,
244 * like with any other file system, sparingly and only when
245 * it is "known" to be safe.
246 */
247 mp->mnt_iflags |= IMNT_MPSAFE;
248 #endif
249
250 pmp->pmp_status = PUFFSTAT_MOUNTING;
251 pmp->pmp_mp = mp;
252 pmp->pmp_msg_maxsize = args->pa_maxmsglen;
253 pmp->pmp_args = *args;
254
255 pmp->pmp_npnodehash = args->pa_nhashbuckets;
256 pmp->pmp_pnodehash = kmem_alloc(BUCKETALLOC(pmp->pmp_npnodehash),
257 KM_SLEEP);
258 for (i = 0; i < pmp->pmp_npnodehash; i++)
259 LIST_INIT(&pmp->pmp_pnodehash[i]);
260 LIST_INIT(&pmp->pmp_newcookie);
261
262 /*
263 * Inform the fileops processing code that we have a mountpoint.
264 * If it doesn't know about anyone with our pid/fd having the
265 * device open, punt
266 */
267 if ((pmp->pmp_pi
268 = putter_attach(mntpid, args->pa_fd, pmp, &puffs_putter)) == NULL) {
269 error = ENOENT;
270 goto out;
271 }
272
273 /* XXX: check parameters */
274 pmp->pmp_root_cookie = args->pa_root_cookie;
275 pmp->pmp_root_vtype = args->pa_root_vtype;
276 pmp->pmp_root_vsize = args->pa_root_vsize;
277 pmp->pmp_root_rdev = args->pa_root_rdev;
278
279 mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
280 mutex_init(&pmp->pmp_sopmtx, MUTEX_DEFAULT, IPL_NONE);
281 cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
282 cv_init(&pmp->pmp_refcount_cv, "puffsref");
283 cv_init(&pmp->pmp_unmounting_cv, "puffsum");
284 cv_init(&pmp->pmp_sopcv, "puffsop");
285 TAILQ_INIT(&pmp->pmp_msg_touser);
286 TAILQ_INIT(&pmp->pmp_msg_replywait);
287 TAILQ_INIT(&pmp->pmp_sopreqs);
288
289 if ((error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
290 puffs_sop_thread, pmp, NULL, "puffsop")) != 0)
291 goto out;
292 pmp->pmp_sopthrcount = 1;
293
294 DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
295 mp, MPTOPUFFSMP(mp)));
296
297 vfs_getnewfsid(mp);
298
299 out:
300 if (error && pmp && pmp->pmp_pi)
301 putter_detach(pmp->pmp_pi);
302 if (error && pmp && pmp->pmp_pnodehash)
303 kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash));
304 if (error && pmp)
305 kmem_free(pmp, sizeof(struct puffs_mount));
306 return error;
307 }
308
309 int
310 puffs_vfsop_start(struct mount *mp, int flags)
311 {
312 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
313
314 KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
315 pmp->pmp_status = PUFFSTAT_RUNNING;
316
317 return 0;
318 }
319
320 int
321 puffs_vfsop_unmount(struct mount *mp, int mntflags)
322 {
323 PUFFS_MSG_VARS(vfs, unmount);
324 struct puffs_mount *pmp;
325 int error, force;
326
327 error = 0;
328 force = mntflags & MNT_FORCE;
329 pmp = MPTOPUFFSMP(mp);
330
331 DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
332 "status 0x%x\n", pmp->pmp_status));
333
334 /*
335 * flush all the vnodes. VOP_RECLAIM() takes care that the
336 * root vnode does not get flushed until unmount. The
337 * userspace root node cookie is stored in the mount
338 * structure, so we can always re-instantiate a root vnode,
339 * should userspace unmount decide it doesn't want to
340 * cooperate.
341 */
342 error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
343 if (error)
344 goto out;
345
346 /*
347 * If we are not DYING, we should ask userspace's opinion
348 * about the situation
349 */
350 mutex_enter(&pmp->pmp_lock);
351 if (pmp->pmp_status != PUFFSTAT_DYING) {
352 pmp->pmp_unmounting = 1;
353 mutex_exit(&pmp->pmp_lock);
354
355 PUFFS_MSG_ALLOC(vfs, unmount);
356 puffs_msg_setinfo(park_unmount,
357 PUFFSOP_VFS, PUFFS_VFS_UNMOUNT, NULL);
358 unmount_msg->pvfsr_flags = mntflags;
359
360 PUFFS_MSG_ENQUEUEWAIT(pmp, park_unmount, error);
361 PUFFS_MSG_RELEASE(unmount);
362
363 error = checkerr(pmp, error, __func__);
364 DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
365
366 mutex_enter(&pmp->pmp_lock);
367 pmp->pmp_unmounting = 0;
368 cv_broadcast(&pmp->pmp_unmounting_cv);
369 }
370
371 /*
372 * if userspace cooperated or we really need to die,
373 * screw what userland thinks and just die.
374 */
375 if (error == 0 || force) {
376 struct puffs_sopreq *psopr;
377
378 /* tell waiters & other resources to go unwait themselves */
379 puffs_userdead(pmp);
380 putter_detach(pmp->pmp_pi);
381
382 /*
383 * Wait until there are no more users for the mount resource.
384 * Notice that this is hooked against transport_close
385 * and return from touser. In an ideal world, it would
386 * be hooked against final return from all operations.
387 * But currently it works well enough, since nobody
388 * does weird blocking voodoo after return from touser().
389 */
390 while (pmp->pmp_refcount != 0)
391 cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
392 mutex_exit(&pmp->pmp_lock);
393
394 /*
395 * Release kernel thread now that there is nothing
396 * it would be wanting to lock.
397 */
398 psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
399 psopr->psopr_sopreq = PUFFS_SOPREQSYS_EXIT;
400 mutex_enter(&pmp->pmp_sopmtx);
401 if (pmp->pmp_sopthrcount == 0) {
402 mutex_exit(&pmp->pmp_sopmtx);
403 kmem_free(psopr, sizeof(*psopr));
404 mutex_enter(&pmp->pmp_sopmtx);
405 KASSERT(pmp->pmp_sopthrcount == 0);
406 } else {
407 TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs,
408 psopr, psopr_entries);
409 cv_signal(&pmp->pmp_sopcv);
410 }
411 while (pmp->pmp_sopthrcount > 0)
412 cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
413 mutex_exit(&pmp->pmp_sopmtx);
414
415 /* free resources now that we hopefully have no waiters left */
416 cv_destroy(&pmp->pmp_unmounting_cv);
417 cv_destroy(&pmp->pmp_refcount_cv);
418 cv_destroy(&pmp->pmp_msg_waiter_cv);
419 cv_destroy(&pmp->pmp_sopcv);
420 mutex_destroy(&pmp->pmp_lock);
421 mutex_destroy(&pmp->pmp_sopmtx);
422
423 kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash));
424 kmem_free(pmp, sizeof(struct puffs_mount));
425 error = 0;
426 } else {
427 mutex_exit(&pmp->pmp_lock);
428 }
429
430 out:
431 DPRINTF(("puffs_unmount: return %d\n", error));
432 return error;
433 }
434
435 /*
436 * This doesn't need to travel to userspace
437 */
438 int
439 puffs_vfsop_root(struct mount *mp, struct vnode **vpp)
440 {
441 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
442 int rv;
443
444 rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, 1, 1, vpp);
445 KASSERT(rv != PUFFS_NOSUCHCOOKIE);
446 return rv;
447 }
448
449 int
450 puffs_vfsop_statvfs(struct mount *mp, struct statvfs *sbp)
451 {
452 PUFFS_MSG_VARS(vfs, statvfs);
453 struct puffs_mount *pmp;
454 int error = 0;
455
456 pmp = MPTOPUFFSMP(mp);
457
458 /*
459 * If we are mounting, it means that the userspace counterpart
460 * is calling mount(2), but mount(2) also calls statvfs. So
461 * requesting statvfs from userspace would mean a deadlock.
462 * Compensate.
463 */
464 if (__predict_false(pmp->pmp_status == PUFFSTAT_MOUNTING))
465 return EINPROGRESS;
466
467 PUFFS_MSG_ALLOC(vfs, statvfs);
468 puffs_msg_setinfo(park_statvfs, PUFFSOP_VFS, PUFFS_VFS_STATVFS, NULL);
469
470 PUFFS_MSG_ENQUEUEWAIT(pmp, park_statvfs, error);
471 error = checkerr(pmp, error, __func__);
472 statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
473
474 /*
475 * Try to produce a sensible result even in the event
476 * of userspace error.
477 *
478 * XXX: cache the copy in non-error case
479 */
480 if (!error) {
481 copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
482 (void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
483 sizeof(struct statvfs));
484 } else {
485 copy_statvfs_info(sbp, mp);
486 }
487
488 PUFFS_MSG_RELEASE(statvfs);
489 return error;
490 }
491
492 static int
493 pageflush(struct mount *mp, kauth_cred_t cred, int waitfor)
494 {
495 struct puffs_node *pn;
496 struct vnode *vp, *mvp;
497 int error, rv;
498
499 error = 0;
500
501 /* Allocate a marker vnode. */
502 if ((mvp = vnalloc(mp)) == NULL)
503 return ENOMEM;
504
505 /*
506 * Sync all cached data from regular vnodes (which are not
507 * currently locked, see below). After this we call VFS_SYNC
508 * for the fs server, which should handle data and metadata for
509 * all the nodes it knows to exist.
510 */
511 mutex_enter(&mntvnode_lock);
512 loop:
513 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
514 vmark(mvp, vp);
515 if (vp->v_mount != mp || vismarker(vp))
516 continue;
517
518 mutex_enter(&vp->v_interlock);
519 pn = VPTOPP(vp);
520 if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
521 mutex_exit(&vp->v_interlock);
522 continue;
523 }
524
525 mutex_exit(&mntvnode_lock);
526
527 /*
528 * Here we try to get a reference to the vnode and to
529 * lock it. This is mostly cargo-culted, but I will
530 * offer an explanation to why I believe this might
531 * actually do the right thing.
532 *
533 * If the vnode is a goner, we quite obviously don't need
534 * to sync it.
535 *
536 * If the vnode was busy, we don't need to sync it because
537 * this is never called with MNT_WAIT except from
538 * dounmount(), when we are wait-flushing all the dirty
539 * vnodes through other routes in any case. So there,
540 * sync() doesn't actually sync. Happy now?
541 */
542 rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
543 if (rv) {
544 mutex_enter(&mntvnode_lock);
545 if (rv == ENOENT) {
546 (void)vunmark(mvp);
547 goto loop;
548 }
549 continue;
550 }
551
552 /* hmm.. is the FAF thing entirely sensible? */
553 if (waitfor == MNT_LAZY) {
554 mutex_enter(&vp->v_interlock);
555 pn->pn_stat |= PNODE_FAF;
556 mutex_exit(&vp->v_interlock);
557 }
558 rv = VOP_FSYNC(vp, cred, waitfor, 0, 0);
559 if (waitfor == MNT_LAZY) {
560 mutex_enter(&vp->v_interlock);
561 pn->pn_stat &= ~PNODE_FAF;
562 mutex_exit(&vp->v_interlock);
563 }
564 if (rv)
565 error = rv;
566 vput(vp);
567 mutex_enter(&mntvnode_lock);
568 }
569 mutex_exit(&mntvnode_lock);
570 vnfree(mvp);
571
572 return error;
573 }
574
575 int
576 puffs_vfsop_sync(struct mount *mp, int waitfor, struct kauth_cred *cred)
577 {
578 PUFFS_MSG_VARS(vfs, sync);
579 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
580 int error, rv;
581
582 error = pageflush(mp, cred, waitfor);
583
584 /* sync fs */
585 PUFFS_MSG_ALLOC(vfs, sync);
586 sync_msg->pvfsr_waitfor = waitfor;
587 puffs_credcvt(&sync_msg->pvfsr_cred, cred);
588 puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL);
589
590 PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv);
591 rv = checkerr(pmp, rv, __func__);
592 if (rv)
593 error = rv;
594
595 PUFFS_MSG_RELEASE(sync);
596 return error;
597 }
598
599 int
600 puffs_vfsop_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
601 {
602 PUFFS_MSG_VARS(vfs, fhtonode);
603 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
604 struct vnode *vp;
605 void *fhdata;
606 size_t argsize, fhlen;
607 int error;
608
609 if (pmp->pmp_args.pa_fhsize == 0)
610 return EOPNOTSUPP;
611
612 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
613 fhlen = fhp->fid_len;
614 fhdata = fhp;
615 } else {
616 fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
617 fhdata = fhp->fid_data;
618
619 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
620 if (pmp->pmp_args.pa_fhsize < fhlen)
621 return EINVAL;
622 } else {
623 if (pmp->pmp_args.pa_fhsize != fhlen)
624 return EINVAL;
625 }
626 }
627
628 argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
629 puffs_msgmem_alloc(argsize, &park_fhtonode, (void *)&fhtonode_msg, 1);
630 fhtonode_msg->pvfsr_dsize = fhlen;
631 memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
632 puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL);
633
634 PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error);
635 error = checkerr(pmp, error, __func__);
636 if (error)
637 goto out;
638
639 error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp);
640 DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
641 fhtonode_msg->pvfsr_fhcookie, vp));
642 if (error == PUFFS_NOSUCHCOOKIE) {
643 error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
644 fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
645 fhtonode_msg->pvfsr_rdev, &vp);
646 if (error)
647 goto out;
648 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
649 } else if (error) {
650 goto out;
651 }
652
653 *vpp = vp;
654 out:
655 puffs_msgmem_release(park_fhtonode);
656 return error;
657 }
658
659 int
660 puffs_vfsop_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
661 {
662 PUFFS_MSG_VARS(vfs, nodetofh);
663 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
664 size_t argsize, fhlen;
665 int error;
666
667 if (pmp->pmp_args.pa_fhsize == 0)
668 return EOPNOTSUPP;
669
670 /* if file handles are static len, we can test len immediately */
671 if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
672 && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
673 && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
674 *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
675 return E2BIG;
676 }
677
678 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
679 fhlen = *fh_size;
680 else
681 fhlen = PUFFS_FROMFHSIZE(*fh_size);
682
683 argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
684 puffs_msgmem_alloc(argsize, &park_nodetofh, (void *)&nodetofh_msg, 1);
685 nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
686 nodetofh_msg->pvfsr_dsize = fhlen;
687 puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL);
688
689 PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error);
690 error = checkerr(pmp, error, __func__);
691
692 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
693 fhlen = nodetofh_msg->pvfsr_dsize;
694 else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
695 fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
696 else
697 fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
698
699 if (error) {
700 if (error == E2BIG)
701 *fh_size = fhlen;
702 goto out;
703 }
704
705 if (fhlen > FHANDLE_SIZE_MAX) {
706 puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG,
707 "file handle too big", VPTOPNC(vp));
708 error = EPROTO;
709 goto out;
710 }
711
712 if (*fh_size < fhlen) {
713 *fh_size = fhlen;
714 error = E2BIG;
715 goto out;
716 }
717 *fh_size = fhlen;
718
719 if (fhp) {
720 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
721 memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
722 } else {
723 fhp->fid_len = *fh_size;
724 memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
725 nodetofh_msg->pvfsr_dsize);
726 }
727 }
728
729 out:
730 puffs_msgmem_release(park_nodetofh);
731 return error;
732 }
733
734 void
735 puffs_vfsop_init(void)
736 {
737
738 /* some checks depend on this */
739 KASSERT(VNOVAL == VSIZENOTSET);
740
741 pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
742 "puffpnpl", &pool_allocator_nointr, IPL_NONE);
743 puffs_msgif_init();
744 }
745
746 void
747 puffs_vfsop_done(void)
748 {
749
750 puffs_msgif_destroy();
751 pool_destroy(&puffs_pnpool);
752 }
753
754 int
755 puffs_vfsop_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
756 {
757
758 return EOPNOTSUPP;
759 }
760
761 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
762 &puffs_vnodeop_opv_desc,
763 &puffs_specop_opv_desc,
764 &puffs_fifoop_opv_desc,
765 &puffs_msgop_opv_desc,
766 NULL,
767 };
768
769 struct vfsops puffs_vfsops = {
770 MOUNT_PUFFS,
771 sizeof (struct puffs_kargs),
772 puffs_vfsop_mount, /* mount */
773 puffs_vfsop_start, /* start */
774 puffs_vfsop_unmount, /* unmount */
775 puffs_vfsop_root, /* root */
776 (void *)eopnotsupp, /* quotactl */
777 puffs_vfsop_statvfs, /* statvfs */
778 puffs_vfsop_sync, /* sync */
779 (void *)eopnotsupp, /* vget */
780 puffs_vfsop_fhtovp, /* fhtovp */
781 puffs_vfsop_vptofh, /* vptofh */
782 puffs_vfsop_init, /* init */
783 NULL, /* reinit */
784 puffs_vfsop_done, /* done */
785 NULL, /* mountroot */
786 puffs_vfsop_snapshot, /* snapshot */
787 vfs_stdextattrctl, /* extattrctl */
788 (void *)eopnotsupp, /* suspendctl */
789 genfs_renamelock_enter,
790 genfs_renamelock_exit,
791 (void *)eopnotsupp,
792 puffs_vnodeopv_descs, /* vnodeops */
793 0, /* refcount */
794 { NULL, NULL }
795 };
796
797 static int
798 puffs_modcmd(modcmd_t cmd, void *arg)
799 {
800
801 switch (cmd) {
802 case MODULE_CMD_INIT:
803 return vfs_attach(&puffs_vfsops);
804 case MODULE_CMD_FINI:
805 return vfs_detach(&puffs_vfsops);
806 default:
807 return ENOTTY;
808 }
809 }
810