puffs_vfsops.c revision 1.28.6.7 1 /* $NetBSD: puffs_vfsops.c,v 1.28.6.7 2007/06/17 21:31:10 ad 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.28.6.7 2007/06/17 21:31:10 ad 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/fstrans.h>
44 #include <sys/proc.h>
45
46 #include <lib/libkern/libkern.h>
47
48 #include <fs/puffs/puffs_msgif.h>
49 #include <fs/puffs/puffs_sys.h>
50
51 #include <nfs/nfsproto.h> /* for fh sizes */
52
53 VFS_PROTOS(puffs);
54
55 MALLOC_JUSTDEFINE(M_PUFFS, "puffs", "Pass-to-Userspace Framework File System");
56
57 #ifndef PUFFS_PNODEBUCKETS
58 #define PUFFS_PNODEBUCKETS 256
59 #endif
60 #ifndef PUFFS_MAXPNODEBUCKETS
61 #define PUFFS_MAXPNODEBUCKETS 8192
62 #endif
63 int puffs_pnodebuckets_default = PUFFS_PNODEBUCKETS;
64 int puffs_maxpnodebuckets = PUFFS_MAXPNODEBUCKETS;
65
66 int
67 puffs_mount(struct mount *mp, const char *path, void *data,
68 struct nameidata *ndp, struct lwp *l)
69 {
70 struct puffs_mount *pmp = NULL;
71 struct puffs_kargs *args;
72 char namebuf[PUFFSNAMESIZE+sizeof(PUFFS_NAMEPREFIX)+1]; /* spooky */
73 int error = 0, i;
74
75 if (mp->mnt_flag & MNT_GETARGS) {
76 pmp = MPTOPUFFSMP(mp);
77 return copyout(&pmp->pmp_args,data,sizeof(struct puffs_kargs));
78 }
79
80 /* update is not supported currently */
81 if (mp->mnt_flag & MNT_UPDATE)
82 return EOPNOTSUPP;
83
84 /*
85 * We need the file system name
86 */
87 if (!data)
88 return EINVAL;
89
90 MALLOC(args, struct puffs_kargs *, sizeof(struct puffs_kargs),
91 M_PUFFS, M_WAITOK);
92
93 error = copyin(data, args, sizeof(struct puffs_kargs));
94 if (error)
95 goto out;
96
97 /* devel phase */
98 if (args->pa_vers != (PUFFSVERSION | PUFFSDEVELVERS)) {
99 printf("puffs_mount: development version mismatch\n");
100 error = EINVAL;
101 goto out;
102 }
103
104 /* nuke spy bits */
105 args->pa_flags &= PUFFS_KFLAG_MASK;
106
107 /* sanitize file handle length */
108 if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) {
109 printf("puffs_mount: handle size %zu too large\n",
110 args->pa_fhsize);
111 error = EINVAL;
112 goto out;
113 }
114 /* sanity check file handle max sizes */
115 if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) {
116 size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize);
117
118 if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) {
119 if (NFSX_FHTOOBIG_P(kfhsize, 0)) {
120 printf("puffs_mount: fhsize larger than "
121 "NFSv2 max %d\n",
122 PUFFS_FROMFHSIZE(NFSX_V2FH));
123 error = EINVAL;
124 goto out;
125 }
126 }
127
128 if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) {
129 if (NFSX_FHTOOBIG_P(kfhsize, 1)) {
130 printf("puffs_mount: fhsize larger than "
131 "NFSv3 max %d\n",
132 PUFFS_FROMFHSIZE(NFSX_V3FHMAX));
133 error = EINVAL;
134 goto out;
135 }
136 }
137 }
138
139 /* build real name */
140 (void)strlcpy(namebuf, PUFFS_NAMEPREFIX, sizeof(namebuf));
141 (void)strlcat(namebuf, args->pa_name, sizeof(namebuf));
142
143 /* inform user server if it got the max request size it wanted */
144 if (args->pa_maxreqlen == 0 || args->pa_maxreqlen > PUFFS_REQ_MAXSIZE)
145 args->pa_maxreqlen = PUFFS_REQ_MAXSIZE;
146 else if (args->pa_maxreqlen < PUFFS_REQSTRUCT_MAX)
147 args->pa_maxreqlen = PUFFS_REQSTRUCT_MAX;
148 (void)strlcpy(args->pa_name, namebuf, sizeof(args->pa_name));
149
150 if (args->pa_nhashbuckets == 0)
151 args->pa_nhashbuckets = puffs_pnodebuckets_default;
152 if (args->pa_nhashbuckets < 1)
153 args->pa_nhashbuckets = 1;
154 if (args->pa_nhashbuckets > PUFFS_MAXPNODEBUCKETS) {
155 args->pa_nhashbuckets = puffs_maxpnodebuckets;
156 printf("puffs_mount: using %d hash buckets. "
157 "adjust puffs_maxpnodebuckets for more\n",
158 puffs_maxpnodebuckets);
159 }
160
161 error = copyout(args, data, sizeof(struct puffs_kargs));
162 if (error)
163 goto out;
164
165 error = set_statvfs_info(path, UIO_USERSPACE, namebuf,
166 UIO_SYSSPACE, mp, l);
167 if (error)
168 goto out;
169 mp->mnt_stat.f_iosize = DEV_BSIZE;
170
171 /*
172 * We can't handle the VFS_STATVFS() mount_domount() does
173 * after VFS_MOUNT() because we'd deadlock, so handle it
174 * here already.
175 */
176 copy_statvfs_info(&args->pa_svfsb, mp);
177 (void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat));
178
179 MALLOC(pmp, struct puffs_mount *, sizeof(struct puffs_mount),
180 M_PUFFS, M_WAITOK | M_ZERO);
181
182 mp->mnt_fs_bshift = DEV_BSHIFT;
183 mp->mnt_dev_bshift = DEV_BSHIFT;
184 mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
185 mp->mnt_data = pmp;
186 mp->mnt_iflag |= IMNT_HAS_TRANS;
187
188 pmp->pmp_status = PUFFSTAT_MOUNTING;
189 pmp->pmp_nextreq = 0;
190 pmp->pmp_mp = mp;
191 pmp->pmp_req_maxsize = args->pa_maxreqlen;
192 pmp->pmp_args = *args;
193
194 pmp->pmp_npnodehash = args->pa_nhashbuckets;
195 pmp->pmp_pnodehash = malloc
196 (sizeof(struct puffs_pnode_hashlist *) * pmp->pmp_npnodehash,
197 M_PUFFS, M_WAITOK);
198 for (i = 0; i < pmp->pmp_npnodehash; i++)
199 LIST_INIT(&pmp->pmp_pnodehash[i]);
200
201 /*
202 * Inform the fileops processing code that we have a mountpoint.
203 * If it doesn't know about anyone with our pid/fd having the
204 * device open, punt
205 */
206 if (puffs_setpmp(l->l_proc->p_pid, args->pa_fd, pmp)) {
207 error = ENOENT;
208 goto out;
209 }
210
211 /* XXX: check parameters */
212 pmp->pmp_root_cookie = args->pa_root_cookie;
213 pmp->pmp_root_vtype = args->pa_root_vtype;
214 pmp->pmp_root_vsize = args->pa_root_vsize;
215 pmp->pmp_root_rdev = args->pa_root_rdev;
216
217 mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
218 cv_init(&pmp->pmp_req_waiter_cv, "puffsget");
219 cv_init(&pmp->pmp_refcount_cv, "puffsref");
220 cv_init(&pmp->pmp_unmounting_cv, "puffsum");
221 TAILQ_INIT(&pmp->pmp_req_touser);
222 TAILQ_INIT(&pmp->pmp_req_replywait);
223 TAILQ_INIT(&pmp->pmp_req_sizepark);
224
225 DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
226 mp, MPTOPUFFSMP(mp)));
227
228 vfs_getnewfsid(mp);
229
230 out:
231 if (error && pmp && pmp->pmp_pnodehash)
232 free(pmp->pmp_pnodehash, M_PUFFS);
233 if (error && pmp)
234 FREE(pmp, M_PUFFS);
235 FREE(args, M_PUFFS);
236 return error;
237 }
238
239 int
240 puffs_start(struct mount *mp, int flags, struct lwp *l)
241 {
242 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
243
244 KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
245 pmp->pmp_status = PUFFSTAT_RUNNING;
246
247 return 0;
248 }
249
250 int
251 puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
252 {
253 struct puffs_mount *pmp;
254 int error, force;
255
256 PUFFS_VFSREQ(unmount);
257
258 error = 0;
259 force = mntflags & MNT_FORCE;
260 pmp = MPTOPUFFSMP(mp);
261
262 DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
263 "status 0x%x\n", pmp->pmp_status));
264
265 /*
266 * flush all the vnodes. VOP_RECLAIM() takes care that the
267 * root vnode does not get flushed until unmount. The
268 * userspace root node cookie is stored in the mount
269 * structure, so we can always re-instantiate a root vnode,
270 * should userspace unmount decide it doesn't want to
271 * cooperate.
272 */
273 error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
274 if (error)
275 goto out;
276
277 /*
278 * If we are not DYING, we should ask userspace's opinion
279 * about the situation
280 */
281 mutex_enter(&pmp->pmp_lock);
282 if (pmp->pmp_status != PUFFSTAT_DYING) {
283 pmp->pmp_unmounting = 1;
284 mutex_exit(&pmp->pmp_lock);
285
286 unmount_arg.pvfsr_flags = mntflags;
287 unmount_arg.pvfsr_pid = puffs_lwp2pid(l);
288
289 error = puffs_vfstouser(pmp, PUFFS_VFS_UNMOUNT,
290 &unmount_arg, sizeof(unmount_arg));
291 DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
292
293 mutex_enter(&pmp->pmp_lock);
294 pmp->pmp_unmounting = 0;
295 cv_broadcast(&pmp->pmp_unmounting_cv);
296 }
297
298 /*
299 * if userspace cooperated or we really need to die,
300 * screw what userland thinks and just die.
301 */
302 if (error == 0 || force) {
303 /* tell waiters & other resources to go unwait themselves */
304 puffs_userdead(pmp);
305 puffs_nukebypmp(pmp);
306
307 /*
308 * Wait until there are no more users for the mount resource.
309 * Notice that this is hooked against transport_close
310 * and return from touser. In an ideal world, it would
311 * be hooked against final return from all operations.
312 * But currently it works well enough, since nobody
313 * does weird blocking voodoo after return from touser().
314 */
315 while (pmp->pmp_refcount != 0)
316 cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
317 mutex_exit(&pmp->pmp_lock);
318
319 /* free resources now that we hopefully have no waiters left */
320 cv_destroy(&pmp->pmp_unmounting_cv);
321 cv_destroy(&pmp->pmp_refcount_cv);
322 cv_destroy(&pmp->pmp_req_waiter_cv);
323 mutex_destroy(&pmp->pmp_lock);
324
325 free(pmp->pmp_pnodehash, M_PUFFS);
326 FREE(pmp, M_PUFFS);
327 error = 0;
328 } else {
329 mutex_exit(&pmp->pmp_lock);
330 }
331
332 out:
333 DPRINTF(("puffs_unmount: return %d\n", error));
334 return error;
335 }
336
337 /*
338 * This doesn't need to travel to userspace
339 */
340 int
341 puffs_root(struct mount *mp, struct vnode **vpp)
342 {
343 struct puffs_mount *pmp;
344 struct puffs_node *pn;
345 struct vnode *vp;
346
347 pmp = MPTOPUFFSMP(mp);
348
349 /*
350 * pmp_lock must be held if vref()'ing or vrele()'ing the
351 * root vnode. the latter is controlled by puffs_inactive().
352 */
353 mutex_enter(&pmp->pmp_lock);
354 vp = pmp->pmp_root;
355 if (vp) {
356 mutex_enter(&vp->v_interlock);
357 mutex_exit(&pmp->pmp_lock);
358 pn = VPTOPP(vp);
359 if (vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK))
360 goto grabnew;
361 *vpp = vp;
362 return 0;
363 } else
364 mutex_exit(&pmp->pmp_lock);
365
366 /* XXX: this is wrong, so FIXME */
367 grabnew:
368
369 /*
370 * So, didn't have the magic root vnode available.
371 * No matter, grab another an stuff it with the cookie.
372 */
373 if (puffs_getvnode(mp, pmp->pmp_root_cookie, pmp->pmp_root_vtype,
374 pmp->pmp_root_vsize, pmp->pmp_root_rdev, &vp))
375 panic("sloppy programming");
376
377 mutex_enter(&pmp->pmp_lock);
378 /*
379 * check if by mysterious force someone else created a root
380 * vnode while we were executing.
381 */
382 if (pmp->pmp_root) {
383 vref(pmp->pmp_root);
384 mutex_exit(&pmp->pmp_lock);
385 puffs_putvnode(vp);
386 vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
387 *vpp = pmp->pmp_root;
388 return 0;
389 }
390
391 /* store cache */
392 vp->v_vflag = VV_ROOT;
393 vp->v_iflag = 0;
394 pmp->pmp_root = vp;
395 mutex_exit(&pmp->pmp_lock);
396
397 vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
398
399 *vpp = vp;
400 return 0;
401 }
402
403 int
404 puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
405 {
406 struct puffs_vfsreq_statvfs *statvfs_arg; /* too big for stack */
407 struct puffs_mount *pmp;
408 int error = 0;
409
410 pmp = MPTOPUFFSMP(mp);
411
412 /*
413 * If we are mounting, it means that the userspace counterpart
414 * is calling mount(2), but mount(2) also calls statvfs. So
415 * requesting statvfs from userspace would mean a deadlock.
416 * Compensate.
417 */
418 if (pmp->pmp_status == PUFFSTAT_MOUNTING)
419 return EINPROGRESS;
420
421 /* too big for stack */
422 MALLOC(statvfs_arg, struct puffs_vfsreq_statvfs *,
423 sizeof(struct puffs_vfsreq_statvfs), M_PUFFS, M_WAITOK | M_ZERO);
424 statvfs_arg->pvfsr_pid = puffs_lwp2pid(l);
425
426 error = puffs_vfstouser(pmp, PUFFS_VFS_STATVFS,
427 statvfs_arg, sizeof(*statvfs_arg));
428 statvfs_arg->pvfsr_sb.f_iosize = DEV_BSIZE;
429
430 /*
431 * Try to produce a sensible result even in the event
432 * of userspace error.
433 *
434 * XXX: cache the copy in non-error case
435 */
436 if (!error) {
437 copy_statvfs_info(&statvfs_arg->pvfsr_sb, mp);
438 (void)memcpy(sbp, &statvfs_arg->pvfsr_sb,
439 sizeof(struct statvfs));
440 } else {
441 copy_statvfs_info(sbp, mp);
442 }
443
444 FREE(statvfs_arg, M_PUFFS);
445 return error;
446 }
447
448 static int
449 pageflush(struct mount *mp, kauth_cred_t cred,
450 int waitfor, int suspending, struct lwp *l)
451 {
452 struct puffs_node *pn;
453 struct vnode *vp, *nvp;
454 int error, rv;
455
456 KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
457 KASSERT((suspending == 0)
458 || (fstrans_is_owner(mp)
459 && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
460
461 error = 0;
462
463 /*
464 * Sync all cached data from regular vnodes (which are not
465 * currently locked, see below). After this we call VFS_SYNC
466 * for the fs server, which should handle data and metadata for
467 * all the nodes it knows to exist.
468 */
469 mutex_enter(&mntvnode_lock);
470 loop:
471 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
472 /* check if we're on the right list */
473 if (vp->v_mount != mp)
474 goto loop;
475
476 mutex_enter(&vp->v_interlock);
477 pn = VPTOPP(vp);
478 nvp = TAILQ_NEXT(vp, v_mntvnodes);
479
480 if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
481 mutex_exit(&vp->v_interlock);
482 continue;
483 }
484
485 mutex_exit(&mntvnode_lock);
486
487 /*
488 * Here we try to get a reference to the vnode and to
489 * lock it. This is mostly cargo-culted, but I will
490 * offer an explanation to why I believe this might
491 * actually do the right thing.
492 *
493 * If the vnode is a goner, we quite obviously don't need
494 * to sync it.
495 *
496 * If the vnode was busy, we don't need to sync it because
497 * this is never called with MNT_WAIT except from
498 * dounmount(), when we are wait-flushing all the dirty
499 * vnodes through other routes in any case. So there,
500 * sync() doesn't actually sync. Happy now?
501 *
502 * NOTE: if we're suspending, vget() does NOT lock.
503 * See puffs_lock() for details.
504 */
505 rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
506 if (rv) {
507 mutex_enter(&mntvnode_lock);
508 if (rv == ENOENT)
509 goto loop;
510 continue;
511 }
512
513 /*
514 * Thread information to puffs_strategy() through the
515 * pnode flags: we want to issue the putpages operations
516 * as FAF if we're suspending, since it's very probable
517 * that our execution context is that of the userspace
518 * daemon. We can do this because:
519 * + we send the "going to suspend" prior to this part
520 * + if any of the writes fails in userspace, it's the
521 * file system server's problem to decide if this was a
522 * failed snapshot when it gets the "snapshot complete"
523 * notification.
524 * + if any of the writes fail in the kernel already, we
525 * immediately fail *and* notify the user server of
526 * failure.
527 *
528 * We also do FAFs if we're called from the syncer. This
529 * is just general optimization for trickle sync: no need
530 * to really guarantee that the stuff ended on backing
531 * storage.
532 * TODO: Maybe also hint the user server of this twist?
533 */
534 if (suspending || waitfor == MNT_LAZY) {
535 mutex_enter(&vp->v_interlock);
536 pn->pn_stat |= PNODE_SUSPEND;
537 mutex_exit(&vp->v_interlock);
538 }
539 rv = VOP_FSYNC(vp, cred, waitfor, 0, 0, l);
540 if (suspending || waitfor == MNT_LAZY) {
541 mutex_enter(&vp->v_interlock);
542 pn->pn_stat &= ~PNODE_SUSPEND;
543 mutex_exit(&vp->v_interlock);
544 }
545 if (rv)
546 error = rv;
547 vput(vp);
548 mutex_enter(&mntvnode_lock);
549 }
550 mutex_exit(&mntvnode_lock);
551
552 return error;
553 }
554
555 int
556 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
557 struct lwp *l)
558 {
559 int error, rv;
560
561 PUFFS_VFSREQ(sync);
562
563 error = pageflush(mp, cred, waitfor, 0, l);
564
565 /* sync fs */
566 sync_arg.pvfsr_waitfor = waitfor;
567 puffs_credcvt(&sync_arg.pvfsr_cred, cred);
568 sync_arg.pvfsr_pid = puffs_lwp2pid(l);
569
570 rv = puffs_vfstouser(MPTOPUFFSMP(mp), PUFFS_VFS_SYNC,
571 &sync_arg, sizeof(sync_arg));
572 if (rv)
573 error = rv;
574
575 return error;
576 }
577
578 int
579 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
580 {
581 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
582 struct puffs_vfsreq_fhtonode *fhtonode_argp;
583 struct vnode *vp;
584 size_t argsize;
585 int error;
586
587 if (pmp->pmp_args.pa_fhsize == 0)
588 return EOPNOTSUPP;
589
590 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
591 if (pmp->pmp_args.pa_fhsize < PUFFS_FROMFHSIZE(fhp->fid_len))
592 return EINVAL;
593 } else {
594 if (pmp->pmp_args.pa_fhsize != PUFFS_FROMFHSIZE(fhp->fid_len))
595 return EINVAL;
596 }
597
598 argsize = sizeof(struct puffs_vfsreq_fhtonode)
599 + PUFFS_FROMFHSIZE(fhp->fid_len);
600 fhtonode_argp = malloc(argsize, M_PUFFS, M_ZERO | M_WAITOK);
601 fhtonode_argp->pvfsr_dsize = PUFFS_FROMFHSIZE(fhp->fid_len);
602 memcpy(fhtonode_argp->pvfsr_data, fhp->fid_data,
603 PUFFS_FROMFHSIZE(fhp->fid_len));
604
605 error = puffs_vfstouser(pmp, PUFFS_VFS_FHTOVP, fhtonode_argp, argsize);
606 if (error)
607 goto out;
608
609 vp = puffs_pnode2vnode(pmp, fhtonode_argp->pvfsr_fhcookie, 1);
610 DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
611 fhtonode_argp->pvfsr_fhcookie, vp));
612 if (!vp) {
613 error = puffs_getvnode(mp, fhtonode_argp->pvfsr_fhcookie,
614 fhtonode_argp->pvfsr_vtype, fhtonode_argp->pvfsr_size,
615 fhtonode_argp->pvfsr_rdev, &vp);
616 if (error)
617 goto out;
618 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
619 }
620
621 *vpp = vp;
622 out:
623 free(fhtonode_argp, M_PUFFS);
624 return error;
625 }
626
627 int
628 puffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
629 {
630 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
631 struct puffs_vfsreq_nodetofh *nodetofh_argp;
632 size_t argsize;
633 int error;
634
635 if (pmp->pmp_args.pa_fhsize == 0)
636 return EOPNOTSUPP;
637
638 /* if file handles are static length, we can return immediately */
639 if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
640 && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
641 *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
642 return E2BIG;
643 }
644
645 argsize = sizeof(struct puffs_vfsreq_nodetofh)
646 + PUFFS_FROMFHSIZE(*fh_size);
647 nodetofh_argp = malloc(argsize, M_PUFFS, M_ZERO | M_WAITOK);
648 nodetofh_argp->pvfsr_fhcookie = VPTOPNC(vp);
649 nodetofh_argp->pvfsr_dsize = PUFFS_FROMFHSIZE(*fh_size);
650
651 error = puffs_vfstouser(pmp, PUFFS_VFS_VPTOFH, nodetofh_argp, argsize);
652 if (error) {
653 if (error == E2BIG)
654 *fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
655 goto out;
656 }
657
658 if (PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize) > FHANDLE_SIZE_MAX) {
659 /* XXX: wrong direction */
660 error = EINVAL;
661 goto out;
662 }
663
664 if (*fh_size < PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize)) {
665 *fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
666 error = E2BIG;
667 goto out;
668 }
669 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
670 *fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
671 } else {
672 *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
673 }
674
675 if (fhp) {
676 fhp->fid_len = *fh_size;
677 memcpy(fhp->fid_data,
678 nodetofh_argp->pvfsr_data, nodetofh_argp->pvfsr_dsize);
679 }
680
681 out:
682 free(nodetofh_argp, M_PUFFS);
683 return error;
684 }
685
686 void
687 puffs_init()
688 {
689
690 malloc_type_attach(M_PUFFS);
691
692 pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
693 "puffpnpl", &pool_allocator_nointr, IPL_NONE);
694 puffs_transport_init();
695 puffs_msgif_init();
696 }
697
698 void
699 puffs_done()
700 {
701
702 puffs_msgif_destroy();
703 puffs_transport_destroy();
704 pool_destroy(&puffs_pnpool);
705
706 malloc_type_detach(M_PUFFS);
707 }
708
709 int
710 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
711 {
712
713 return EOPNOTSUPP;
714 }
715
716 int
717 puffs_suspendctl(struct mount *mp, int cmd)
718 {
719 struct puffs_mount *pmp;
720 int error;
721
722 pmp = MPTOPUFFSMP(mp);
723 switch (cmd) {
724 case SUSPEND_SUSPEND:
725 DPRINTF(("puffs_suspendctl: suspending\n"));
726 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
727 break;
728 puffs_suspendtouser(pmp, PUFFS_SUSPEND_START);
729
730 error = pageflush(mp, FSCRED, 0, 1, curlwp);
731 if (error == 0)
732 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
733
734 if (error != 0) {
735 puffs_suspendtouser(pmp, PUFFS_SUSPEND_ERROR);
736 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
737 break;
738 }
739
740 puffs_suspendtouser(pmp, PUFFS_SUSPEND_SUSPENDED);
741
742 break;
743
744 case SUSPEND_RESUME:
745 DPRINTF(("puffs_suspendctl: resume\n"));
746 error = 0;
747 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
748 puffs_suspendtouser(pmp, PUFFS_SUSPEND_RESUME);
749 break;
750
751 default:
752 error = EINVAL;
753 break;
754 }
755
756 DPRINTF(("puffs_suspendctl: return %d\n", error));
757 return error;
758 }
759
760 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
761 &puffs_vnodeop_opv_desc,
762 &puffs_specop_opv_desc,
763 &puffs_fifoop_opv_desc,
764 &puffs_msgop_opv_desc,
765 NULL,
766 };
767
768 struct vfsops puffs_vfsops = {
769 MOUNT_PUFFS,
770 puffs_mount, /* mount */
771 puffs_start, /* start */
772 puffs_unmount, /* unmount */
773 puffs_root, /* root */
774 (void *)eopnotsupp, /* quotactl */
775 puffs_statvfs, /* statvfs */
776 puffs_sync, /* sync */
777 (void *)eopnotsupp, /* vget */
778 puffs_fhtovp, /* fhtovp */
779 puffs_vptofh, /* vptofh */
780 puffs_init, /* init */
781 NULL, /* reinit */
782 puffs_done, /* done */
783 NULL, /* mountroot */
784 puffs_snapshot, /* snapshot */
785 vfs_stdextattrctl, /* extattrctl */
786 puffs_suspendctl, /* suspendctl */
787 puffs_vnodeopv_descs, /* vnodeops */
788 0, /* refcount */
789 { NULL, NULL }
790 };
791 VFS_ATTACH(puffs_vfsops);
792