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