puffs_vfsops.c revision 1.47 1 /* $NetBSD: puffs_vfsops.c,v 1.47 2007/07/09 21:10:49 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.47 2007/07/09 21:10:49 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 puffs_cidcvt(&unmount_arg.pvfsr_cid, 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 = MPTOPUFFSMP(mp);
344
345 return puffs_pnode2vnode(pmp, pmp->pmp_root_cookie, 1, vpp);
346 }
347
348 int
349 puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
350 {
351 struct puffs_vfsreq_statvfs *statvfs_arg; /* too big for stack */
352 struct puffs_mount *pmp;
353 int error = 0;
354
355 pmp = MPTOPUFFSMP(mp);
356
357 /*
358 * If we are mounting, it means that the userspace counterpart
359 * is calling mount(2), but mount(2) also calls statvfs. So
360 * requesting statvfs from userspace would mean a deadlock.
361 * Compensate.
362 */
363 if (pmp->pmp_status == PUFFSTAT_MOUNTING)
364 return EINPROGRESS;
365
366 /* too big for stack */
367 MALLOC(statvfs_arg, struct puffs_vfsreq_statvfs *,
368 sizeof(struct puffs_vfsreq_statvfs), M_PUFFS, M_WAITOK | M_ZERO);
369 puffs_cidcvt(&statvfs_arg->pvfsr_cid, l);
370
371 error = puffs_vfstouser(pmp, PUFFS_VFS_STATVFS,
372 statvfs_arg, sizeof(*statvfs_arg));
373 statvfs_arg->pvfsr_sb.f_iosize = DEV_BSIZE;
374
375 /*
376 * Try to produce a sensible result even in the event
377 * of userspace error.
378 *
379 * XXX: cache the copy in non-error case
380 */
381 if (!error) {
382 copy_statvfs_info(&statvfs_arg->pvfsr_sb, mp);
383 (void)memcpy(sbp, &statvfs_arg->pvfsr_sb,
384 sizeof(struct statvfs));
385 } else {
386 copy_statvfs_info(sbp, mp);
387 }
388
389 FREE(statvfs_arg, M_PUFFS);
390 return error;
391 }
392
393 static int
394 pageflush(struct mount *mp, kauth_cred_t cred,
395 int waitfor, int suspending, struct lwp *l)
396 {
397 struct puffs_node *pn;
398 struct vnode *vp, *nvp;
399 int error, rv;
400
401 KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
402 KASSERT((suspending == 0)
403 || (fstrans_is_owner(mp)
404 && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
405
406 error = 0;
407
408 /*
409 * Sync all cached data from regular vnodes (which are not
410 * currently locked, see below). After this we call VFS_SYNC
411 * for the fs server, which should handle data and metadata for
412 * all the nodes it knows to exist.
413 */
414 simple_lock(&mntvnode_slock);
415 loop:
416 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
417 /* check if we're on the right list */
418 if (vp->v_mount != mp)
419 goto loop;
420
421 simple_lock(&vp->v_interlock);
422 pn = VPTOPP(vp);
423 nvp = TAILQ_NEXT(vp, v_mntvnodes);
424
425 if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
426 simple_unlock(&vp->v_interlock);
427 continue;
428 }
429
430 simple_unlock(&mntvnode_slock);
431
432 /*
433 * Here we try to get a reference to the vnode and to
434 * lock it. This is mostly cargo-culted, but I will
435 * offer an explanation to why I believe this might
436 * actually do the right thing.
437 *
438 * If the vnode is a goner, we quite obviously don't need
439 * to sync it.
440 *
441 * If the vnode was busy, we don't need to sync it because
442 * this is never called with MNT_WAIT except from
443 * dounmount(), when we are wait-flushing all the dirty
444 * vnodes through other routes in any case. So there,
445 * sync() doesn't actually sync. Happy now?
446 *
447 * NOTE: if we're suspending, vget() does NOT lock.
448 * See puffs_lock() for details.
449 */
450 rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
451 if (rv) {
452 simple_lock(&mntvnode_slock);
453 if (rv == ENOENT)
454 goto loop;
455 continue;
456 }
457
458 /*
459 * Thread information to puffs_strategy() through the
460 * pnode flags: we want to issue the putpages operations
461 * as FAF if we're suspending, since it's very probable
462 * that our execution context is that of the userspace
463 * daemon. We can do this because:
464 * + we send the "going to suspend" prior to this part
465 * + if any of the writes fails in userspace, it's the
466 * file system server's problem to decide if this was a
467 * failed snapshot when it gets the "snapshot complete"
468 * notification.
469 * + if any of the writes fail in the kernel already, we
470 * immediately fail *and* notify the user server of
471 * failure.
472 *
473 * We also do FAFs if we're called from the syncer. This
474 * is just general optimization for trickle sync: no need
475 * to really guarantee that the stuff ended on backing
476 * storage.
477 * TODO: Maybe also hint the user server of this twist?
478 */
479 if (suspending || waitfor == MNT_LAZY) {
480 simple_lock(&vp->v_interlock);
481 pn->pn_stat |= PNODE_SUSPEND;
482 simple_unlock(&vp->v_interlock);
483 }
484 rv = VOP_FSYNC(vp, cred, waitfor, 0, 0, l);
485 if (suspending || waitfor == MNT_LAZY) {
486 simple_lock(&vp->v_interlock);
487 pn->pn_stat &= ~PNODE_SUSPEND;
488 simple_unlock(&vp->v_interlock);
489 }
490 if (rv)
491 error = rv;
492 vput(vp);
493 simple_lock(&mntvnode_slock);
494 }
495 simple_unlock(&mntvnode_slock);
496
497 return error;
498 }
499
500 int
501 puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
502 struct lwp *l)
503 {
504 int error, rv;
505
506 PUFFS_VFSREQ(sync);
507
508 error = pageflush(mp, cred, waitfor, 0, l);
509
510 /* sync fs */
511 sync_arg.pvfsr_waitfor = waitfor;
512 puffs_credcvt(&sync_arg.pvfsr_cred, cred);
513 puffs_cidcvt(&sync_arg.pvfsr_cid, l);
514
515 rv = puffs_vfstouser(MPTOPUFFSMP(mp), PUFFS_VFS_SYNC,
516 &sync_arg, sizeof(sync_arg));
517 if (rv)
518 error = rv;
519
520 return error;
521 }
522
523 int
524 puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
525 {
526 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
527 struct puffs_vfsreq_fhtonode *fhtonode_argp;
528 struct vnode *vp;
529 size_t argsize;
530 int error;
531
532 if (pmp->pmp_args.pa_fhsize == 0)
533 return EOPNOTSUPP;
534
535 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
536 if (pmp->pmp_args.pa_fhsize < PUFFS_FROMFHSIZE(fhp->fid_len))
537 return EINVAL;
538 } else {
539 if (pmp->pmp_args.pa_fhsize != PUFFS_FROMFHSIZE(fhp->fid_len))
540 return EINVAL;
541 }
542
543 argsize = sizeof(struct puffs_vfsreq_fhtonode)
544 + PUFFS_FROMFHSIZE(fhp->fid_len);
545 fhtonode_argp = malloc(argsize, M_PUFFS, M_ZERO | M_WAITOK);
546 fhtonode_argp->pvfsr_dsize = PUFFS_FROMFHSIZE(fhp->fid_len);
547 memcpy(fhtonode_argp->pvfsr_data, fhp->fid_data,
548 PUFFS_FROMFHSIZE(fhp->fid_len));
549
550 error = puffs_vfstouser(pmp, PUFFS_VFS_FHTOVP, fhtonode_argp, argsize);
551 if (error)
552 goto out;
553
554 error = puffs_pnode2vnode(pmp, fhtonode_argp->pvfsr_fhcookie, 1, &vp);
555 DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
556 fhtonode_argp->pvfsr_fhcookie, vp));
557 if (error) {
558 error = puffs_getvnode(mp, fhtonode_argp->pvfsr_fhcookie,
559 fhtonode_argp->pvfsr_vtype, fhtonode_argp->pvfsr_size,
560 fhtonode_argp->pvfsr_rdev, &vp);
561 if (error)
562 goto out;
563 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
564 }
565
566 *vpp = vp;
567 out:
568 free(fhtonode_argp, M_PUFFS);
569 return error;
570 }
571
572 int
573 puffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
574 {
575 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
576 struct puffs_vfsreq_nodetofh *nodetofh_argp;
577 size_t argsize;
578 int error;
579
580 if (pmp->pmp_args.pa_fhsize == 0)
581 return EOPNOTSUPP;
582
583 /* if file handles are static length, we can return immediately */
584 if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
585 && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
586 *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
587 return E2BIG;
588 }
589
590 argsize = sizeof(struct puffs_vfsreq_nodetofh)
591 + PUFFS_FROMFHSIZE(*fh_size);
592 nodetofh_argp = malloc(argsize, M_PUFFS, M_ZERO | M_WAITOK);
593 nodetofh_argp->pvfsr_fhcookie = VPTOPNC(vp);
594 nodetofh_argp->pvfsr_dsize = PUFFS_FROMFHSIZE(*fh_size);
595
596 error = puffs_vfstouser(pmp, PUFFS_VFS_VPTOFH, nodetofh_argp, argsize);
597 if (error) {
598 if (error == E2BIG)
599 *fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
600 goto out;
601 }
602
603 if (PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize) > FHANDLE_SIZE_MAX) {
604 /* XXX: wrong direction */
605 error = EINVAL;
606 goto out;
607 }
608
609 if (*fh_size < PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize)) {
610 *fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
611 error = E2BIG;
612 goto out;
613 }
614 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
615 *fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
616 } else {
617 *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
618 }
619
620 if (fhp) {
621 fhp->fid_len = *fh_size;
622 memcpy(fhp->fid_data,
623 nodetofh_argp->pvfsr_data, nodetofh_argp->pvfsr_dsize);
624 }
625
626 out:
627 free(nodetofh_argp, M_PUFFS);
628 return error;
629 }
630
631 void
632 puffs_init()
633 {
634
635 malloc_type_attach(M_PUFFS);
636
637 pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
638 "puffpnpl", &pool_allocator_nointr, IPL_NONE);
639 puffs_transport_init();
640 puffs_msgif_init();
641 }
642
643 void
644 puffs_done()
645 {
646
647 puffs_msgif_destroy();
648 puffs_transport_destroy();
649 pool_destroy(&puffs_pnpool);
650
651 malloc_type_detach(M_PUFFS);
652 }
653
654 int
655 puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
656 {
657
658 return EOPNOTSUPP;
659 }
660
661 int
662 puffs_suspendctl(struct mount *mp, int cmd)
663 {
664 struct puffs_mount *pmp;
665 int error;
666
667 pmp = MPTOPUFFSMP(mp);
668 switch (cmd) {
669 case SUSPEND_SUSPEND:
670 DPRINTF(("puffs_suspendctl: suspending\n"));
671 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
672 break;
673 puffs_suspendtouser(pmp, PUFFS_SUSPEND_START);
674
675 error = pageflush(mp, FSCRED, 0, 1, curlwp);
676 if (error == 0)
677 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
678
679 if (error != 0) {
680 puffs_suspendtouser(pmp, PUFFS_SUSPEND_ERROR);
681 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
682 break;
683 }
684
685 puffs_suspendtouser(pmp, PUFFS_SUSPEND_SUSPENDED);
686
687 break;
688
689 case SUSPEND_RESUME:
690 DPRINTF(("puffs_suspendctl: resume\n"));
691 error = 0;
692 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
693 puffs_suspendtouser(pmp, PUFFS_SUSPEND_RESUME);
694 break;
695
696 default:
697 error = EINVAL;
698 break;
699 }
700
701 DPRINTF(("puffs_suspendctl: return %d\n", error));
702 return error;
703 }
704
705 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
706 &puffs_vnodeop_opv_desc,
707 &puffs_specop_opv_desc,
708 &puffs_fifoop_opv_desc,
709 &puffs_msgop_opv_desc,
710 NULL,
711 };
712
713 struct vfsops puffs_vfsops = {
714 MOUNT_PUFFS,
715 puffs_mount, /* mount */
716 puffs_start, /* start */
717 puffs_unmount, /* unmount */
718 puffs_root, /* root */
719 (void *)eopnotsupp, /* quotactl */
720 puffs_statvfs, /* statvfs */
721 puffs_sync, /* sync */
722 (void *)eopnotsupp, /* vget */
723 puffs_fhtovp, /* fhtovp */
724 puffs_vptofh, /* vptofh */
725 puffs_init, /* init */
726 NULL, /* reinit */
727 puffs_done, /* done */
728 NULL, /* mountroot */
729 puffs_snapshot, /* snapshot */
730 vfs_stdextattrctl, /* extattrctl */
731 puffs_suspendctl, /* suspendctl */
732 puffs_vnodeopv_descs, /* vnodeops */
733 0, /* refcount */
734 { NULL, NULL }
735 };
736 VFS_ATTACH(puffs_vfsops);
737