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