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