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