puffs.c revision 1.33 1 /* $NetBSD: puffs.c,v 1.33 2007/03/20 10:22:23 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 * 3. The name of the company nor the name of the author may be used to
19 * endorse or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #if !defined(lint)
37 __RCSID("$NetBSD: puffs.c,v 1.33 2007/03/20 10:22:23 pooka Exp $");
38 #endif /* !lint */
39
40 #include <sys/param.h>
41 #include <sys/mount.h>
42
43 #include <assert.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <mntopts.h>
48 #include <puffs.h>
49 #include <puffsdump.h>
50 #include <stdarg.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <syslog.h>
55 #include <unistd.h>
56
57 #include "puffs_priv.h"
58
59 /* Most file systems want this for opts, so just give it to them */
60 const struct mntopt puffsmopts[] = {
61 MOPT_STDOPTS,
62 PUFFSMOPT_STD,
63 MOPT_NULL,
64 };
65
66 #define FILLOP(lower, upper) \
67 do { \
68 if (pops->puffs_node_##lower) \
69 opmask[PUFFS_VN_##upper] = 1; \
70 } while (/*CONSTCOND*/0)
71 static void
72 fillvnopmask(struct puffs_ops *pops, uint8_t *opmask)
73 {
74
75 memset(opmask, 0, PUFFS_VN_MAX);
76
77 FILLOP(create, CREATE);
78 FILLOP(mknod, MKNOD);
79 FILLOP(open, OPEN);
80 FILLOP(close, CLOSE);
81 FILLOP(access, ACCESS);
82 FILLOP(getattr, GETATTR);
83 FILLOP(setattr, SETATTR);
84 FILLOP(poll, POLL); /* XXX: not ready in kernel */
85 FILLOP(mmap, MMAP);
86 FILLOP(fsync, FSYNC);
87 FILLOP(seek, SEEK);
88 FILLOP(remove, REMOVE);
89 FILLOP(link, LINK);
90 FILLOP(rename, RENAME);
91 FILLOP(mkdir, MKDIR);
92 FILLOP(rmdir, RMDIR);
93 FILLOP(symlink, SYMLINK);
94 FILLOP(readdir, READDIR);
95 FILLOP(readlink, READLINK);
96 FILLOP(reclaim, RECLAIM);
97 FILLOP(inactive, INACTIVE);
98 FILLOP(print, PRINT);
99 FILLOP(read, READ);
100 FILLOP(write, WRITE);
101
102 /* XXX: not implemented in the kernel */
103 FILLOP(getextattr, GETEXTATTR);
104 FILLOP(setextattr, SETEXTATTR);
105 FILLOP(listextattr, LISTEXTATTR);
106 }
107 #undef FILLOP
108
109 int
110 puffs_getselectable(struct puffs_usermount *pu)
111 {
112
113 return pu->pu_fd;
114 }
115
116 int
117 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
118 {
119 int x;
120
121 x = mode;
122 return ioctl(pu->pu_fd, FIONBIO, &x);
123 }
124
125 int
126 puffs_getstate(struct puffs_usermount *pu)
127 {
128
129 return pu->pu_state;
130 }
131
132 void
133 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
134 {
135
136 pu->pu_cc_stacksize = ss;
137 }
138
139 struct puffs_pathobj *
140 puffs_getrootpathobj(struct puffs_usermount *pu)
141 {
142 struct puffs_node *pnr;
143
144 pnr = pu->pu_pn_root;
145 if (pnr == NULL) {
146 errno = ENOENT;
147 return NULL;
148 }
149
150 return &pnr->pn_po;
151 }
152
153
154 void
155 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
156 {
157
158 pu->pu_pathbuild = fn;
159 }
160
161 void
162 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
163 {
164
165 pu->pu_pathtransform = fn;
166 }
167
168 void
169 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
170 {
171
172 pu->pu_pathcmp = fn;
173 }
174
175 void
176 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
177 {
178
179 pu->pu_pathfree = fn;
180 }
181
182 void
183 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
184 {
185
186 pu->pu_namemod = fn;
187 }
188
189 enum {PUFFCALL_ANSWER, PUFFCALL_IGNORE, PUFFCALL_AGAIN};
190
191 struct puffs_usermount *
192 _puffs_mount(int develv, struct puffs_ops *pops, const char *dir, int mntflags,
193 const char *puffsname, void *priv, uint32_t pflags, size_t maxreqlen)
194 {
195 struct puffs_args pargs;
196 struct puffs_usermount *pu;
197 int fd = 0;
198
199 if (develv != PUFFS_DEVEL_LIBVERSION) {
200 warnx("puffs_mount: mounting with lib version %d, need %d",
201 develv, PUFFS_DEVEL_LIBVERSION);
202 errno = EINVAL;
203 return NULL;
204 }
205
206 fd = open("/dev/puffs", O_RDONLY);
207 if (fd == -1)
208 return NULL;
209 if (fd <= 2)
210 warnx("puffs_mount: device fd %d (<= 2), sure this is "
211 "what you want?", fd);
212
213 pargs.pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
214 pargs.pa_flags = PUFFS_FLAG_KERN(pflags);
215 pargs.pa_fd = fd;
216 pargs.pa_maxreqlen = maxreqlen;
217 fillvnopmask(pops, pargs.pa_vnopmask);
218 (void)strlcpy(pargs.pa_name, puffsname, sizeof(pargs.pa_name));
219
220 pu = malloc(sizeof(struct puffs_usermount));
221 if (!pu)
222 return NULL;
223
224 pu->pu_flags = pflags;
225 pu->pu_ops = *pops;
226 free(pops); /* XXX */
227 pu->pu_fd = fd;
228 pu->pu_privdata = priv;
229 pu->pu_cc_stacksize = PUFFS_CC_STACKSIZE_DEFAULT;
230 LIST_INIT(&pu->pu_pnodelst);
231
232 /* defaults for some user-settable translation functions */
233 pu->pu_cmap = NULL; /* identity translation */
234
235 pu->pu_pathbuild = puffs_stdpath_buildpath;
236 pu->pu_pathfree = puffs_stdpath_freepath;
237 pu->pu_pathcmp = puffs_stdpath_cmppath;
238 pu->pu_pathtransform = NULL;
239 pu->pu_namemod = NULL;
240
241 pu->pu_state = PUFFS_STATE_MOUNTING;
242
243 #if 1
244 /* XXXkludgehere */
245 /* kauth doesn't provide this service any longer */
246 if (geteuid() != 0)
247 mntflags |= MNT_NOSUID | MNT_NODEV;
248 #endif
249
250 if (mount(MOUNT_PUFFS, dir, mntflags, &pargs) == -1)
251 goto failfree;
252 pu->pu_maxreqlen = pargs.pa_maxreqlen;
253
254 return pu;
255
256 failfree:
257 /* can't unmount() from here for obvious reasons */
258 if (fd)
259 close(fd);
260 free(pu);
261 return NULL;
262 }
263
264 int
265 puffs_start(struct puffs_usermount *pu, void *rootcookie, struct statvfs *sbp)
266 {
267 struct puffs_startreq sreq;
268
269 memset(&sreq, 0, sizeof(struct puffs_startreq));
270 sreq.psr_cookie = rootcookie;
271 sreq.psr_sb = *sbp;
272
273 /* tell kernel we're flying */
274 if (ioctl(pu->pu_fd, PUFFSSTARTOP, &sreq) == -1)
275 return -1;
276
277 pu->pu_state = PUFFS_STATE_RUNNING;
278
279 return 0;
280 }
281
282 /*
283 * XXX: there's currently no clean way to request unmount from
284 * within the user server, so be very brutal about it.
285 */
286 /*ARGSUSED*/
287 int
288 puffs_exit(struct puffs_usermount *pu, int force)
289 {
290 struct puffs_node *pn, *pn_next;
291
292 force = 1; /* currently */
293
294 if (pu->pu_fd)
295 close(pu->pu_fd);
296
297 pn = LIST_FIRST(&pu->pu_pnodelst);
298 while (pn) {
299 pn_next = LIST_NEXT(pn, pn_entries);
300 puffs_pn_put(pn);
301 pn = pn_next;
302 }
303 free(pu);
304
305 return 0; /* always succesful for now, WILL CHANGE */
306 }
307
308 int
309 puffs_mainloop(struct puffs_usermount *pu, int flags)
310 {
311 struct puffs_getreq *pgr;
312 struct puffs_putreq *ppr;
313 int rv;
314
315 rv = -1;
316 pgr = puffs_req_makeget(pu, pu->pu_maxreqlen, 0);
317 if (pgr == NULL)
318 return -1;
319
320 ppr = puffs_req_makeput(pu);
321 if (ppr == NULL) {
322 puffs_req_destroyget(pgr);
323 return -1;
324 }
325
326 if ((flags & PUFFSLOOP_NODAEMON) == 0)
327 if (daemon(1, 0) == -1)
328 goto out;
329
330 /* XXX: should be a bit more robust with errors here */
331 while (puffs_getstate(pu) == PUFFS_STATE_RUNNING
332 || puffs_getstate(pu) == PUFFS_STATE_UNMOUNTING) {
333 puffs_req_resetput(ppr);
334
335 if (puffs_req_handle(pu, pgr, ppr, 0) == -1) {
336 rv = -1;
337 break;
338 }
339 if (puffs_req_putput(ppr) == -1) {
340 rv = -1;
341 break;
342 }
343 }
344
345 out:
346 puffs_req_destroyput(ppr);
347 puffs_req_destroyget(pgr);
348 return rv;
349 }
350
351 int
352 puffs_dopreq(struct puffs_usermount *pu, struct puffs_req *preq,
353 struct puffs_putreq *ppr)
354 {
355 struct puffs_cc *pcc;
356 int rv;
357
358 /*
359 * XXX: the structure is currently a mess. anyway, trap
360 * the cacheops here already, since they don't need a cc.
361 * I really should get around to revamping the operation
362 * dispatching code one of these days.
363 */
364 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_CACHE) {
365 struct puffs_cacheinfo *pci = (void *)preq;
366
367 if (pu->pu_ops.puffs_cache_write == NULL)
368 return 0;
369
370 pu->pu_ops.puffs_cache_write(pu, preq->preq_cookie,
371 pci->pcache_nruns, pci->pcache_runs);
372 }
373
374 if (pu->pu_flags & PUFFS_FLAG_OPDUMP)
375 puffsdump_req(preq);
376
377 pcc = puffs_cc_create(pu);
378
379 /* XXX: temporary kludging */
380 pcc->pcc_preq = malloc(preq->preq_buflen);
381 if (pcc->pcc_preq == NULL)
382 return -1;
383 (void) memcpy(pcc->pcc_preq, preq, preq->preq_buflen);
384
385 rv = puffs_docc(pcc, ppr);
386
387 if ((pcc->pcc_flags & PCC_DONE) == 0)
388 return 0;
389
390 return rv;
391 }
392
393 int
394 puffs_docc(struct puffs_cc *pcc, struct puffs_putreq *ppr)
395 {
396 struct puffs_usermount *pu = pcc->pcc_pu;
397 int rv;
398
399 assert((pcc->pcc_flags & PCC_DONE) == 0);
400
401 puffs_cc_continue(pcc);
402 rv = pcc->pcc_rv;
403
404 if ((pcc->pcc_flags & PCC_DONE) == 0)
405 rv = PUFFCALL_AGAIN;
406
407 /* check if we need to store this reply */
408 switch (rv) {
409 case PUFFCALL_ANSWER:
410 if (pu->pu_flags & PUFFS_FLAG_OPDUMP)
411 puffsdump_rv(pcc->pcc_preq);
412
413 puffs_req_putcc(ppr, pcc);
414 break;
415 case PUFFCALL_IGNORE:
416 puffs_cc_destroy(pcc);
417 break;
418 case PUFFCALL_AGAIN:
419 break;
420 default:
421 assert(/*CONSTCOND*/0);
422 }
423
424 return 0;
425 }
426
427 /* library private, but linked from callcontext.c */
428
429 void
430 puffs_calldispatcher(struct puffs_cc *pcc)
431 {
432 struct puffs_usermount *pu = pcc->pcc_pu;
433 struct puffs_ops *pops = &pu->pu_ops;
434 struct puffs_req *preq = pcc->pcc_preq;
435 void *auxbuf = preq; /* help with typecasting */
436 int error, rv, buildpath;
437
438 assert(pcc->pcc_flags & (PCC_ONCE | PCC_REALCC));
439
440 if (PUFFSOP_WANTREPLY(preq->preq_opclass))
441 rv = PUFFCALL_ANSWER;
442 else
443 rv = PUFFCALL_IGNORE;
444
445 buildpath = pu->pu_flags & PUFFS_FLAG_BUILDPATH;
446
447 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS) {
448 switch (preq->preq_optype) {
449 case PUFFS_VFS_UNMOUNT:
450 {
451 struct puffs_vfsreq_unmount *auxt = auxbuf;
452
453 pu->pu_state = PUFFS_STATE_UNMOUNTING;
454 error = pops->puffs_fs_unmount(pcc,
455 auxt->pvfsr_flags, auxt->pvfsr_pid);
456 if (!error)
457 pu->pu_state = PUFFS_STATE_UNMOUNTED;
458 else
459 pu->pu_state = PUFFS_STATE_RUNNING;
460 break;
461 }
462 case PUFFS_VFS_STATVFS:
463 {
464 struct puffs_vfsreq_statvfs *auxt = auxbuf;
465
466 error = pops->puffs_fs_statvfs(pcc,
467 &auxt->pvfsr_sb, auxt->pvfsr_pid);
468 break;
469 }
470 case PUFFS_VFS_SYNC:
471 {
472 struct puffs_vfsreq_sync *auxt = auxbuf;
473
474 error = pops->puffs_fs_sync(pcc,
475 auxt->pvfsr_waitfor, &auxt->pvfsr_cred,
476 auxt->pvfsr_pid);
477 break;
478 }
479 case PUFFS_VFS_SUSPEND:
480 {
481 struct puffs_vfsreq_suspend *auxt = auxbuf;
482
483 error = 0;
484 if (pops->puffs_fs_suspend == NULL)
485 break;
486
487 pops->puffs_fs_suspend(pcc, auxt->pvfsr_status);
488 break;
489 }
490
491 default:
492 /*
493 * I guess the kernel sees this one coming
494 */
495 error = EINVAL;
496 break;
497 }
498
499 /* XXX: audit return values */
500 /* XXX: sync with kernel */
501 } else if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN) {
502 switch (preq->preq_optype) {
503 case PUFFS_VN_LOOKUP:
504 {
505 struct puffs_vnreq_lookup *auxt = auxbuf;
506 struct puffs_cn pcn;
507
508 pcn.pcn_pkcnp = &auxt->pvnr_cn;
509 if (buildpath) {
510 error = puffs_path_pcnbuild(pu, &pcn,
511 preq->preq_cookie);
512 if (error)
513 break;
514 }
515
516 /* lookup *must* be present */
517 error = pops->puffs_node_lookup(pcc, preq->preq_cookie,
518 &auxt->pvnr_newnode, &auxt->pvnr_vtype,
519 &auxt->pvnr_size, &auxt->pvnr_rdev, &pcn);
520
521 if (buildpath) {
522 if (error) {
523 pu->pu_pathfree(pu, &pcn.pcn_po_full);
524 } else {
525 struct puffs_node *pn;
526
527 /*
528 * did we get a new node or a
529 * recycled node?
530 */
531 pn = PU_CMAP(pu, auxt->pvnr_newnode);
532 if (pn->pn_po.po_path == NULL)
533 pn->pn_po = pcn.pcn_po_full;
534 else
535 pu->pu_pathfree(pu,
536 &pcn.pcn_po_full);
537 }
538 }
539
540 break;
541 }
542
543 case PUFFS_VN_CREATE:
544 {
545 struct puffs_vnreq_create *auxt = auxbuf;
546 struct puffs_cn pcn;
547 if (pops->puffs_node_create == NULL) {
548 error = 0;
549 break;
550 }
551
552 pcn.pcn_pkcnp = &auxt->pvnr_cn;
553 if (buildpath) {
554 error = puffs_path_pcnbuild(pu, &pcn,
555 preq->preq_cookie);
556 if (error)
557 break;
558 }
559
560 error = pops->puffs_node_create(pcc,
561 preq->preq_cookie, &auxt->pvnr_newnode,
562 &pcn, &auxt->pvnr_va);
563
564 if (buildpath) {
565 if (error) {
566 pu->pu_pathfree(pu, &pcn.pcn_po_full);
567 } else {
568 struct puffs_node *pn;
569
570 pn = PU_CMAP(pu, auxt->pvnr_newnode);
571 pn->pn_po = pcn.pcn_po_full;
572 }
573 }
574
575 break;
576 }
577
578 case PUFFS_VN_MKNOD:
579 {
580 struct puffs_vnreq_mknod *auxt = auxbuf;
581 struct puffs_cn pcn;
582 if (pops->puffs_node_mknod == NULL) {
583 error = 0;
584 break;
585 }
586
587 pcn.pcn_pkcnp = &auxt->pvnr_cn;
588 if (buildpath) {
589 error = puffs_path_pcnbuild(pu, &pcn,
590 preq->preq_cookie);
591 if (error)
592 break;
593 }
594
595 error = pops->puffs_node_mknod(pcc,
596 preq->preq_cookie, &auxt->pvnr_newnode,
597 &pcn, &auxt->pvnr_va);
598
599 if (buildpath) {
600 if (error) {
601 pu->pu_pathfree(pu, &pcn.pcn_po_full);
602 } else {
603 struct puffs_node *pn;
604
605 pn = PU_CMAP(pu, auxt->pvnr_newnode);
606 pn->pn_po = pcn.pcn_po_full;
607 }
608 }
609
610 break;
611 }
612
613 case PUFFS_VN_OPEN:
614 {
615 struct puffs_vnreq_open *auxt = auxbuf;
616 if (pops->puffs_node_open == NULL) {
617 error = 0;
618 break;
619 }
620
621 error = pops->puffs_node_open(pcc,
622 preq->preq_cookie, auxt->pvnr_mode,
623 &auxt->pvnr_cred, auxt->pvnr_pid);
624 break;
625 }
626
627 case PUFFS_VN_CLOSE:
628 {
629 struct puffs_vnreq_close *auxt = auxbuf;
630 if (pops->puffs_node_close == NULL) {
631 error = 0;
632 break;
633 }
634
635 error = pops->puffs_node_close(pcc,
636 preq->preq_cookie, auxt->pvnr_fflag,
637 &auxt->pvnr_cred, auxt->pvnr_pid);
638 break;
639 }
640
641 case PUFFS_VN_ACCESS:
642 {
643 struct puffs_vnreq_access *auxt = auxbuf;
644 if (pops->puffs_node_access == NULL) {
645 error = 0;
646 break;
647 }
648
649 error = pops->puffs_node_access(pcc,
650 preq->preq_cookie, auxt->pvnr_mode,
651 &auxt->pvnr_cred, auxt->pvnr_pid);
652 break;
653 }
654
655 case PUFFS_VN_GETATTR:
656 {
657 struct puffs_vnreq_getattr *auxt = auxbuf;
658 if (pops->puffs_node_getattr == NULL) {
659 error = EOPNOTSUPP;
660 break;
661 }
662
663 error = pops->puffs_node_getattr(pcc,
664 preq->preq_cookie, &auxt->pvnr_va,
665 &auxt->pvnr_cred, auxt->pvnr_pid);
666 break;
667 }
668
669 case PUFFS_VN_SETATTR:
670 {
671 struct puffs_vnreq_setattr *auxt = auxbuf;
672 if (pops->puffs_node_setattr == NULL) {
673 error = EOPNOTSUPP;
674 break;
675 }
676
677 error = pops->puffs_node_setattr(pcc,
678 preq->preq_cookie, &auxt->pvnr_va,
679 &auxt->pvnr_cred, auxt->pvnr_pid);
680 break;
681 }
682
683 case PUFFS_VN_MMAP:
684 {
685 struct puffs_vnreq_mmap *auxt = auxbuf;
686 if (pops->puffs_node_mmap == NULL) {
687 error = 0;
688 break;
689 }
690
691 error = pops->puffs_node_mmap(pcc,
692 preq->preq_cookie, auxt->pvnr_fflags,
693 &auxt->pvnr_cred, auxt->pvnr_pid);
694 break;
695 }
696
697 case PUFFS_VN_FSYNC:
698 {
699 struct puffs_vnreq_fsync *auxt = auxbuf;
700 if (pops->puffs_node_fsync == NULL) {
701 error = 0;
702 break;
703 }
704
705 error = pops->puffs_node_fsync(pcc,
706 preq->preq_cookie, &auxt->pvnr_cred,
707 auxt->pvnr_flags, auxt->pvnr_offlo,
708 auxt->pvnr_offhi, auxt->pvnr_pid);
709 break;
710 }
711
712 case PUFFS_VN_SEEK:
713 {
714 struct puffs_vnreq_seek *auxt = auxbuf;
715 if (pops->puffs_node_seek == NULL) {
716 error = 0;
717 break;
718 }
719
720 error = pops->puffs_node_seek(pcc,
721 preq->preq_cookie, auxt->pvnr_oldoff,
722 auxt->pvnr_newoff, &auxt->pvnr_cred);
723 break;
724 }
725
726 case PUFFS_VN_REMOVE:
727 {
728 struct puffs_vnreq_remove *auxt = auxbuf;
729 struct puffs_cn pcn;
730 if (pops->puffs_node_remove == NULL) {
731 error = 0;
732 break;
733 }
734
735 pcn.pcn_pkcnp = &auxt->pvnr_cn;
736
737 error = pops->puffs_node_remove(pcc,
738 preq->preq_cookie, auxt->pvnr_cookie_targ, &pcn);
739 break;
740 }
741
742 case PUFFS_VN_LINK:
743 {
744 struct puffs_vnreq_link *auxt = auxbuf;
745 struct puffs_cn pcn;
746 if (pops->puffs_node_link == NULL) {
747 error = 0;
748 break;
749 }
750
751 pcn.pcn_pkcnp = &auxt->pvnr_cn;
752 if (buildpath) {
753 error = puffs_path_pcnbuild(pu, &pcn,
754 preq->preq_cookie);
755 if (error)
756 break;
757 }
758
759 error = pops->puffs_node_link(pcc,
760 preq->preq_cookie, auxt->pvnr_cookie_targ, &pcn);
761 if (buildpath)
762 pu->pu_pathfree(pu, &pcn.pcn_po_full);
763
764 break;
765 }
766
767 case PUFFS_VN_RENAME:
768 {
769 struct puffs_vnreq_rename *auxt = auxbuf;
770 struct puffs_cn pcn_src, pcn_targ;
771 struct puffs_node *pn_src;
772
773 if (pops->puffs_node_rename == NULL) {
774 error = 0;
775 break;
776 }
777
778 pcn_src.pcn_pkcnp = &auxt->pvnr_cn_src;
779 pcn_targ.pcn_pkcnp = &auxt->pvnr_cn_targ;
780 if (buildpath) {
781 pn_src = auxt->pvnr_cookie_src;
782 pcn_src.pcn_po_full = pn_src->pn_po;
783
784 error = puffs_path_pcnbuild(pu, &pcn_targ,
785 auxt->pvnr_cookie_targdir);
786 if (error)
787 break;
788 }
789
790 error = pops->puffs_node_rename(pcc,
791 preq->preq_cookie, auxt->pvnr_cookie_src,
792 &pcn_src, auxt->pvnr_cookie_targdir,
793 auxt->pvnr_cookie_targ, &pcn_targ);
794
795 if (buildpath) {
796 if (error) {
797 pu->pu_pathfree(pu,
798 &pcn_targ.pcn_po_full);
799 } else {
800 struct puffs_pathinfo pi;
801 struct puffs_pathobj po_old;
802
803 /* handle this node */
804 po_old = pn_src->pn_po;
805 pn_src->pn_po = pcn_targ.pcn_po_full;
806
807 if (pn_src->pn_va.va_type != VDIR) {
808 pu->pu_pathfree(pu, &po_old);
809 break;
810 }
811
812 /* handle all child nodes for DIRs */
813 pi.pi_old = &pcn_src.pcn_po_full;
814 pi.pi_new = &pcn_targ.pcn_po_full;
815
816 if (puffs_pn_nodewalk(pu,
817 puffs_path_prefixadj, &pi) != NULL)
818 error = ENOMEM;
819 pu->pu_pathfree(pu, &po_old);
820 }
821 }
822 break;
823 }
824
825 case PUFFS_VN_MKDIR:
826 {
827 struct puffs_vnreq_mkdir *auxt = auxbuf;
828 struct puffs_cn pcn;
829 if (pops->puffs_node_mkdir == NULL) {
830 error = 0;
831 break;
832 }
833
834 pcn.pcn_pkcnp = &auxt->pvnr_cn;
835 if (buildpath) {
836 error = puffs_path_pcnbuild(pu, &pcn,
837 preq->preq_cookie);
838 if (error)
839 break;
840 }
841
842 error = pops->puffs_node_mkdir(pcc,
843 preq->preq_cookie, &auxt->pvnr_newnode,
844 &pcn, &auxt->pvnr_va);
845
846 if (buildpath) {
847 if (error) {
848 pu->pu_pathfree(pu, &pcn.pcn_po_full);
849 } else {
850 struct puffs_node *pn;
851
852 pn = PU_CMAP(pu, auxt->pvnr_newnode);
853 pn->pn_po = pcn.pcn_po_full;
854 }
855 }
856
857 break;
858 }
859
860 case PUFFS_VN_RMDIR:
861 {
862 struct puffs_vnreq_rmdir *auxt = auxbuf;
863 struct puffs_cn pcn;
864 if (pops->puffs_node_rmdir == NULL) {
865 error = 0;
866 break;
867 }
868
869 pcn.pcn_pkcnp = &auxt->pvnr_cn;
870
871 error = pops->puffs_node_rmdir(pcc,
872 preq->preq_cookie, auxt->pvnr_cookie_targ, &pcn);
873 break;
874 }
875
876 case PUFFS_VN_SYMLINK:
877 {
878 struct puffs_vnreq_symlink *auxt = auxbuf;
879 struct puffs_cn pcn;
880 if (pops->puffs_node_symlink == NULL) {
881 error = 0;
882 break;
883 }
884
885 pcn.pcn_pkcnp = &auxt->pvnr_cn;
886 if (buildpath) {
887 error = puffs_path_pcnbuild(pu, &pcn,
888 preq->preq_cookie);
889 if (error)
890 break;
891 }
892
893 error = pops->puffs_node_symlink(pcc,
894 preq->preq_cookie, &auxt->pvnr_newnode,
895 &pcn, &auxt->pvnr_va, auxt->pvnr_link);
896
897 if (buildpath) {
898 if (error) {
899 pu->pu_pathfree(pu, &pcn.pcn_po_full);
900 } else {
901 struct puffs_node *pn;
902
903 pn = PU_CMAP(pu, auxt->pvnr_newnode);
904 pn->pn_po = pcn.pcn_po_full;
905 }
906 }
907
908 break;
909 }
910
911 case PUFFS_VN_READDIR:
912 {
913 struct puffs_vnreq_readdir *auxt = auxbuf;
914 size_t res;
915
916 if (pops->puffs_node_readdir == NULL) {
917 error = 0;
918 break;
919 }
920
921 res = auxt->pvnr_resid;
922 error = pops->puffs_node_readdir(pcc,
923 preq->preq_cookie, auxt->pvnr_dent,
924 &auxt->pvnr_cred, &auxt->pvnr_offset,
925 &auxt->pvnr_resid);
926
927 /* need to move a bit more */
928 preq->preq_buflen = sizeof(struct puffs_vnreq_readdir)
929 + (res - auxt->pvnr_resid);
930 break;
931 }
932
933 case PUFFS_VN_READLINK:
934 {
935 struct puffs_vnreq_readlink *auxt = auxbuf;
936 if (pops->puffs_node_readlink == NULL) {
937 error = EOPNOTSUPP;
938 break;
939 }
940
941 error = pops->puffs_node_readlink(pcc,
942 preq->preq_cookie, &auxt->pvnr_cred,
943 auxt->pvnr_link, &auxt->pvnr_linklen);
944 break;
945 }
946
947 case PUFFS_VN_RECLAIM:
948 {
949 struct puffs_vnreq_reclaim *auxt = auxbuf;
950 if (pops->puffs_node_reclaim == NULL) {
951 error = 0;
952 break;
953 }
954
955 error = pops->puffs_node_reclaim(pcc,
956 preq->preq_cookie, auxt->pvnr_pid);
957 break;
958 }
959
960 case PUFFS_VN_INACTIVE:
961 {
962 struct puffs_vnreq_inactive *auxt = auxbuf;
963 if (pops->puffs_node_inactive == NULL) {
964 error = EOPNOTSUPP;
965 break;
966 }
967
968 error = pops->puffs_node_inactive(pcc,
969 preq->preq_cookie, auxt->pvnr_pid,
970 &auxt->pvnr_backendrefs);
971 break;
972 }
973
974 case PUFFS_VN_PATHCONF:
975 {
976 struct puffs_vnreq_pathconf *auxt = auxbuf;
977 if (pops->puffs_node_pathconf == NULL) {
978 error = 0;
979 break;
980 }
981
982 error = pops->puffs_node_pathconf(pcc,
983 preq->preq_cookie, auxt->pvnr_name,
984 &auxt->pvnr_retval);
985 break;
986 }
987
988 case PUFFS_VN_ADVLOCK:
989 {
990 struct puffs_vnreq_advlock *auxt = auxbuf;
991 if (pops->puffs_node_advlock == NULL) {
992 error = 0;
993 break;
994 }
995
996 error = pops->puffs_node_advlock(pcc,
997 preq->preq_cookie, auxt->pvnr_id, auxt->pvnr_op,
998 &auxt->pvnr_fl, auxt->pvnr_flags);
999 break;
1000 }
1001
1002 case PUFFS_VN_PRINT:
1003 {
1004 if (pops->puffs_node_print == NULL) {
1005 error = 0;
1006 break;
1007 }
1008
1009 error = pops->puffs_node_print(pcc,
1010 preq->preq_cookie);
1011 break;
1012 }
1013
1014 case PUFFS_VN_READ:
1015 {
1016 struct puffs_vnreq_read *auxt = auxbuf;
1017 size_t res;
1018
1019 if (pops->puffs_node_read == NULL) {
1020 error = EIO;
1021 break;
1022 }
1023
1024 res = auxt->pvnr_resid;
1025 error = pops->puffs_node_read(pcc,
1026 preq->preq_cookie, auxt->pvnr_data,
1027 auxt->pvnr_offset, &auxt->pvnr_resid,
1028 &auxt->pvnr_cred, auxt->pvnr_ioflag);
1029
1030 /* need to move a bit more */
1031 preq->preq_buflen = sizeof(struct puffs_vnreq_read)
1032 + (res - auxt->pvnr_resid);
1033 break;
1034 }
1035
1036 case PUFFS_VN_WRITE:
1037 {
1038 struct puffs_vnreq_write *auxt = auxbuf;
1039
1040 if (pops->puffs_node_write == NULL) {
1041 error = EIO;
1042 break;
1043 }
1044
1045 error = pops->puffs_node_write(pcc,
1046 preq->preq_cookie, auxt->pvnr_data,
1047 auxt->pvnr_offset, &auxt->pvnr_resid,
1048 &auxt->pvnr_cred, auxt->pvnr_ioflag);
1049
1050 /* don't need to move data back to the kernel */
1051 preq->preq_buflen = sizeof(struct puffs_vnreq_write);
1052 break;
1053 }
1054
1055 /* holy bitrot, ryydman! */
1056 #if 0
1057 case PUFFS_VN_IOCTL:
1058 error = pops->puffs_node_ioctl1(pcc, preq->preq_cookie,
1059 (struct puffs_vnreq_ioctl *)auxbuf, &pop);
1060 if (error != 0)
1061 break;
1062 pop.pso_reqid = preq->preq_id;
1063
1064 /* let the kernel do it's intermediate duty */
1065 error = ioctl(pu->pu_fd, PUFFSSIZEOP, &pop);
1066 /*
1067 * XXX: I don't actually know what the correct
1068 * thing to do in case of an error is, so I'll
1069 * just ignore it for the time being.
1070 */
1071 error = pops->puffs_node_ioctl2(pcc, preq->preq_cookie,
1072 (struct puffs_vnreq_ioctl *)auxbuf, &pop);
1073 break;
1074
1075 case PUFFS_VN_FCNTL:
1076 error = pops->puffs_node_fcntl1(pcc, preq->preq_cookie,
1077 (struct puffs_vnreq_fcntl *)auxbuf, &pop);
1078 if (error != 0)
1079 break;
1080 pop.pso_reqid = preq->preq_id;
1081
1082 /* let the kernel do it's intermediate duty */
1083 error = ioctl(pu->pu_fd, PUFFSSIZEOP, &pop);
1084 /*
1085 * XXX: I don't actually know what the correct
1086 * thing to do in case of an error is, so I'll
1087 * just ignore it for the time being.
1088 */
1089 error = pops->puffs_node_fcntl2(pcc, preq->preq_cookie,
1090 (struct puffs_vnreq_fcntl *)auxbuf, &pop);
1091 break;
1092 #endif
1093
1094 default:
1095 printf("inval op %d\n", preq->preq_optype);
1096 error = EINVAL;
1097 break;
1098 }
1099 } else {
1100 /*
1101 * this one also
1102 */
1103 error = EINVAL;
1104 }
1105
1106 preq->preq_rv = error;
1107
1108 pcc->pcc_rv = rv;
1109 pcc->pcc_flags |= PCC_DONE;
1110 }
1111
1112
1113 #if 0
1114 case PUFFS_VN_POLL:
1115 {
1116 struct puffs_vnreq_poll *auxt = auxbuf;
1117 if (pops->puffs_node_poll == NULL) {
1118 error = 0;
1119 break;
1120 }
1121
1122 error = pops->puffs_node_poll(pcc,
1123 preq->preq_cookie, preq-);
1124 break;
1125 }
1126
1127 case PUFFS_VN_KQFILTER:
1128 {
1129 struct puffs_vnreq_kqfilter *auxt = auxbuf;
1130 if (pops->puffs_node_kqfilter == NULL) {
1131 error = 0;
1132 break;
1133 }
1134
1135 error = pops->puffs_node_kqfilter(pcc,
1136 preq->preq_cookie, );
1137 break;
1138 }
1139
1140 case PUFFS_VN_CLOSEEXTATTR:
1141 {
1142 struct puffs_vnreq_closeextattr *auxt = auxbuf;
1143 if (pops->puffs_closeextattr == NULL) {
1144 error = 0;
1145 break;
1146 }
1147
1148 error = pops->puffs_closeextattr(pcc,
1149 preq->preq_cookie, );
1150 break;
1151 }
1152
1153 case PUFFS_VN_GETEXTATTR:
1154 {
1155 struct puffs_vnreq_getextattr *auxt = auxbuf;
1156 if (pops->puffs_getextattr == NULL) {
1157 error = 0;
1158 break;
1159 }
1160
1161 error = pops->puffs_getextattr(pcc,
1162 preq->preq_cookie, );
1163 break;
1164 }
1165
1166 case PUFFS_VN_LISTEXTATTR:
1167 {
1168 struct puffs_vnreq_listextattr *auxt = auxbuf;
1169 if (pops->puffs_listextattr == NULL) {
1170 error = 0;
1171 break;
1172 }
1173
1174 error = pops->puffs_listextattr(pcc,
1175 preq->preq_cookie, );
1176 break;
1177 }
1178
1179 case PUFFS_VN_OPENEXTATTR:
1180 {
1181 struct puffs_vnreq_openextattr *auxt = auxbuf;
1182 if (pops->puffs_openextattr == NULL) {
1183 error = 0;
1184 break;
1185 }
1186
1187 error = pops->puffs_openextattr(pcc,
1188 preq->preq_cookie, );
1189 break;
1190 }
1191
1192 case PUFFS_VN_DELETEEXTATTR:
1193 {
1194 struct puffs_vnreq_deleteextattr *auxt = auxbuf;
1195 if (pops->puffs_deleteextattr == NULL) {
1196 error = 0;
1197 break;
1198 }
1199
1200 error = pops->puffs_deleteextattr(pcc,
1201 preq->preq_cookie, );
1202 break;
1203 }
1204
1205 case PUFFS_VN_SETEXTATTR:
1206 {
1207 struct puffs_vnreq_setextattr *auxt = auxbuf;
1208 if (pops->puffs_setextattr == NULL) {
1209 error = 0;
1210 break;
1211 }
1212
1213 error = pops->puffs_setextattr(pcc,
1214 preq->preq_cookie, );
1215 break;
1216 }
1217
1218 #endif
1219