puffs.c revision 1.32 1 /* $NetBSD: puffs.c,v 1.32 2007/02/18 17:38:10 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.32 2007/02/18 17:38:10 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 if (pu->pu_flags & PUFFS_FLAG_OPDUMP)
359 puffsdump_req(preq);
360
361 pcc = puffs_cc_create(pu);
362
363 /* XXX: temporary kludging */
364 pcc->pcc_preq = malloc(preq->preq_buflen);
365 if (pcc->pcc_preq == NULL)
366 return -1;
367 (void) memcpy(pcc->pcc_preq, preq, preq->preq_buflen);
368
369 rv = puffs_docc(pcc, ppr);
370
371 if ((pcc->pcc_flags & PCC_DONE) == 0)
372 return 0;
373
374 return rv;
375 }
376
377 int
378 puffs_docc(struct puffs_cc *pcc, struct puffs_putreq *ppr)
379 {
380 struct puffs_usermount *pu = pcc->pcc_pu;
381 int rv;
382
383 assert((pcc->pcc_flags & PCC_DONE) == 0);
384
385 puffs_cc_continue(pcc);
386 rv = pcc->pcc_rv;
387
388 if ((pcc->pcc_flags & PCC_DONE) == 0)
389 rv = PUFFCALL_AGAIN;
390
391 /* check if we need to store this reply */
392 switch (rv) {
393 case PUFFCALL_ANSWER:
394 if (pu->pu_flags & PUFFS_FLAG_OPDUMP)
395 puffsdump_rv(pcc->pcc_preq);
396
397 puffs_req_putcc(ppr, pcc);
398 break;
399 case PUFFCALL_IGNORE:
400 puffs_cc_destroy(pcc);
401 break;
402 case PUFFCALL_AGAIN:
403 break;
404 default:
405 assert(/*CONSTCOND*/0);
406 }
407
408 return 0;
409 }
410
411 /* library private, but linked from callcontext.c */
412
413 void
414 puffs_calldispatcher(struct puffs_cc *pcc)
415 {
416 struct puffs_usermount *pu = pcc->pcc_pu;
417 struct puffs_ops *pops = &pu->pu_ops;
418 struct puffs_req *preq = pcc->pcc_preq;
419 void *auxbuf = preq; /* help with typecasting */
420 int error, rv, buildpath;
421
422 assert(pcc->pcc_flags & (PCC_ONCE | PCC_REALCC));
423
424 if (PUFFSOP_WANTREPLY(preq->preq_opclass))
425 rv = PUFFCALL_ANSWER;
426 else
427 rv = PUFFCALL_IGNORE;
428
429 buildpath = pu->pu_flags & PUFFS_FLAG_BUILDPATH;
430
431 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS) {
432 switch (preq->preq_optype) {
433 case PUFFS_VFS_UNMOUNT:
434 {
435 struct puffs_vfsreq_unmount *auxt = auxbuf;
436
437 pu->pu_state = PUFFS_STATE_UNMOUNTING;
438 error = pops->puffs_fs_unmount(pcc,
439 auxt->pvfsr_flags, auxt->pvfsr_pid);
440 if (!error)
441 pu->pu_state = PUFFS_STATE_UNMOUNTED;
442 else
443 pu->pu_state = PUFFS_STATE_RUNNING;
444 break;
445 }
446 case PUFFS_VFS_STATVFS:
447 {
448 struct puffs_vfsreq_statvfs *auxt = auxbuf;
449
450 error = pops->puffs_fs_statvfs(pcc,
451 &auxt->pvfsr_sb, auxt->pvfsr_pid);
452 break;
453 }
454 case PUFFS_VFS_SYNC:
455 {
456 struct puffs_vfsreq_sync *auxt = auxbuf;
457
458 error = pops->puffs_fs_sync(pcc,
459 auxt->pvfsr_waitfor, &auxt->pvfsr_cred,
460 auxt->pvfsr_pid);
461 break;
462 }
463 case PUFFS_VFS_SUSPEND:
464 {
465 struct puffs_vfsreq_suspend *auxt = auxbuf;
466
467 error = 0;
468 if (pops->puffs_fs_suspend == NULL)
469 break;
470
471 pops->puffs_fs_suspend(pcc, auxt->pvfsr_status);
472 break;
473 }
474
475 default:
476 /*
477 * I guess the kernel sees this one coming
478 */
479 error = EINVAL;
480 break;
481 }
482
483 /* XXX: audit return values */
484 /* XXX: sync with kernel */
485 } else if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN) {
486 switch (preq->preq_optype) {
487 case PUFFS_VN_LOOKUP:
488 {
489 struct puffs_vnreq_lookup *auxt = auxbuf;
490 struct puffs_cn pcn;
491
492 pcn.pcn_pkcnp = &auxt->pvnr_cn;
493 if (buildpath) {
494 error = puffs_path_pcnbuild(pu, &pcn,
495 preq->preq_cookie);
496 if (error)
497 break;
498 }
499
500 /* lookup *must* be present */
501 error = pops->puffs_node_lookup(pcc, preq->preq_cookie,
502 &auxt->pvnr_newnode, &auxt->pvnr_vtype,
503 &auxt->pvnr_size, &auxt->pvnr_rdev, &pcn);
504
505 if (buildpath) {
506 if (error) {
507 pu->pu_pathfree(pu, &pcn.pcn_po_full);
508 } else {
509 struct puffs_node *pn;
510
511 /*
512 * did we get a new node or a
513 * recycled node?
514 */
515 pn = PU_CMAP(pu, auxt->pvnr_newnode);
516 if (pn->pn_po.po_path == NULL)
517 pn->pn_po = pcn.pcn_po_full;
518 else
519 pu->pu_pathfree(pu,
520 &pcn.pcn_po_full);
521 }
522 }
523
524 break;
525 }
526
527 case PUFFS_VN_CREATE:
528 {
529 struct puffs_vnreq_create *auxt = auxbuf;
530 struct puffs_cn pcn;
531 if (pops->puffs_node_create == NULL) {
532 error = 0;
533 break;
534 }
535
536 pcn.pcn_pkcnp = &auxt->pvnr_cn;
537 if (buildpath) {
538 error = puffs_path_pcnbuild(pu, &pcn,
539 preq->preq_cookie);
540 if (error)
541 break;
542 }
543
544 error = pops->puffs_node_create(pcc,
545 preq->preq_cookie, &auxt->pvnr_newnode,
546 &pcn, &auxt->pvnr_va);
547
548 if (buildpath) {
549 if (error) {
550 pu->pu_pathfree(pu, &pcn.pcn_po_full);
551 } else {
552 struct puffs_node *pn;
553
554 pn = PU_CMAP(pu, auxt->pvnr_newnode);
555 pn->pn_po = pcn.pcn_po_full;
556 }
557 }
558
559 break;
560 }
561
562 case PUFFS_VN_MKNOD:
563 {
564 struct puffs_vnreq_mknod *auxt = auxbuf;
565 struct puffs_cn pcn;
566 if (pops->puffs_node_mknod == NULL) {
567 error = 0;
568 break;
569 }
570
571 pcn.pcn_pkcnp = &auxt->pvnr_cn;
572 if (buildpath) {
573 error = puffs_path_pcnbuild(pu, &pcn,
574 preq->preq_cookie);
575 if (error)
576 break;
577 }
578
579 error = pops->puffs_node_mknod(pcc,
580 preq->preq_cookie, &auxt->pvnr_newnode,
581 &pcn, &auxt->pvnr_va);
582
583 if (buildpath) {
584 if (error) {
585 pu->pu_pathfree(pu, &pcn.pcn_po_full);
586 } else {
587 struct puffs_node *pn;
588
589 pn = PU_CMAP(pu, auxt->pvnr_newnode);
590 pn->pn_po = pcn.pcn_po_full;
591 }
592 }
593
594 break;
595 }
596
597 case PUFFS_VN_OPEN:
598 {
599 struct puffs_vnreq_open *auxt = auxbuf;
600 if (pops->puffs_node_open == NULL) {
601 error = 0;
602 break;
603 }
604
605 error = pops->puffs_node_open(pcc,
606 preq->preq_cookie, auxt->pvnr_mode,
607 &auxt->pvnr_cred, auxt->pvnr_pid);
608 break;
609 }
610
611 case PUFFS_VN_CLOSE:
612 {
613 struct puffs_vnreq_close *auxt = auxbuf;
614 if (pops->puffs_node_close == NULL) {
615 error = 0;
616 break;
617 }
618
619 error = pops->puffs_node_close(pcc,
620 preq->preq_cookie, auxt->pvnr_fflag,
621 &auxt->pvnr_cred, auxt->pvnr_pid);
622 break;
623 }
624
625 case PUFFS_VN_ACCESS:
626 {
627 struct puffs_vnreq_access *auxt = auxbuf;
628 if (pops->puffs_node_access == NULL) {
629 error = 0;
630 break;
631 }
632
633 error = pops->puffs_node_access(pcc,
634 preq->preq_cookie, auxt->pvnr_mode,
635 &auxt->pvnr_cred, auxt->pvnr_pid);
636 break;
637 }
638
639 case PUFFS_VN_GETATTR:
640 {
641 struct puffs_vnreq_getattr *auxt = auxbuf;
642 if (pops->puffs_node_getattr == NULL) {
643 error = EOPNOTSUPP;
644 break;
645 }
646
647 error = pops->puffs_node_getattr(pcc,
648 preq->preq_cookie, &auxt->pvnr_va,
649 &auxt->pvnr_cred, auxt->pvnr_pid);
650 break;
651 }
652
653 case PUFFS_VN_SETATTR:
654 {
655 struct puffs_vnreq_setattr *auxt = auxbuf;
656 if (pops->puffs_node_setattr == NULL) {
657 error = EOPNOTSUPP;
658 break;
659 }
660
661 error = pops->puffs_node_setattr(pcc,
662 preq->preq_cookie, &auxt->pvnr_va,
663 &auxt->pvnr_cred, auxt->pvnr_pid);
664 break;
665 }
666
667 case PUFFS_VN_MMAP:
668 {
669 struct puffs_vnreq_mmap *auxt = auxbuf;
670 if (pops->puffs_node_mmap == NULL) {
671 error = 0;
672 break;
673 }
674
675 error = pops->puffs_node_mmap(pcc,
676 preq->preq_cookie, auxt->pvnr_fflags,
677 &auxt->pvnr_cred, auxt->pvnr_pid);
678 break;
679 }
680
681 case PUFFS_VN_FSYNC:
682 {
683 struct puffs_vnreq_fsync *auxt = auxbuf;
684 if (pops->puffs_node_fsync == NULL) {
685 error = 0;
686 break;
687 }
688
689 error = pops->puffs_node_fsync(pcc,
690 preq->preq_cookie, &auxt->pvnr_cred,
691 auxt->pvnr_flags, auxt->pvnr_offlo,
692 auxt->pvnr_offhi, auxt->pvnr_pid);
693 break;
694 }
695
696 case PUFFS_VN_SEEK:
697 {
698 struct puffs_vnreq_seek *auxt = auxbuf;
699 if (pops->puffs_node_seek == NULL) {
700 error = 0;
701 break;
702 }
703
704 error = pops->puffs_node_seek(pcc,
705 preq->preq_cookie, auxt->pvnr_oldoff,
706 auxt->pvnr_newoff, &auxt->pvnr_cred);
707 break;
708 }
709
710 case PUFFS_VN_REMOVE:
711 {
712 struct puffs_vnreq_remove *auxt = auxbuf;
713 struct puffs_cn pcn;
714 if (pops->puffs_node_remove == NULL) {
715 error = 0;
716 break;
717 }
718
719 pcn.pcn_pkcnp = &auxt->pvnr_cn;
720
721 error = pops->puffs_node_remove(pcc,
722 preq->preq_cookie, auxt->pvnr_cookie_targ, &pcn);
723 break;
724 }
725
726 case PUFFS_VN_LINK:
727 {
728 struct puffs_vnreq_link *auxt = auxbuf;
729 struct puffs_cn pcn;
730 if (pops->puffs_node_link == NULL) {
731 error = 0;
732 break;
733 }
734
735 pcn.pcn_pkcnp = &auxt->pvnr_cn;
736 if (buildpath) {
737 error = puffs_path_pcnbuild(pu, &pcn,
738 preq->preq_cookie);
739 if (error)
740 break;
741 }
742
743 error = pops->puffs_node_link(pcc,
744 preq->preq_cookie, auxt->pvnr_cookie_targ, &pcn);
745 if (buildpath)
746 pu->pu_pathfree(pu, &pcn.pcn_po_full);
747
748 break;
749 }
750
751 case PUFFS_VN_RENAME:
752 {
753 struct puffs_vnreq_rename *auxt = auxbuf;
754 struct puffs_cn pcn_src, pcn_targ;
755 struct puffs_node *pn_src;
756
757 if (pops->puffs_node_rename == NULL) {
758 error = 0;
759 break;
760 }
761
762 pcn_src.pcn_pkcnp = &auxt->pvnr_cn_src;
763 pcn_targ.pcn_pkcnp = &auxt->pvnr_cn_targ;
764 if (buildpath) {
765 pn_src = auxt->pvnr_cookie_src;
766 pcn_src.pcn_po_full = pn_src->pn_po;
767
768 error = puffs_path_pcnbuild(pu, &pcn_targ,
769 auxt->pvnr_cookie_targdir);
770 if (error)
771 break;
772 }
773
774 error = pops->puffs_node_rename(pcc,
775 preq->preq_cookie, auxt->pvnr_cookie_src,
776 &pcn_src, auxt->pvnr_cookie_targdir,
777 auxt->pvnr_cookie_targ, &pcn_targ);
778
779 if (buildpath) {
780 if (error) {
781 pu->pu_pathfree(pu,
782 &pcn_targ.pcn_po_full);
783 } else {
784 struct puffs_pathinfo pi;
785 struct puffs_pathobj po_old;
786
787 /* handle this node */
788 po_old = pn_src->pn_po;
789 pn_src->pn_po = pcn_targ.pcn_po_full;
790
791 if (pn_src->pn_va.va_type != VDIR) {
792 pu->pu_pathfree(pu, &po_old);
793 break;
794 }
795
796 /* handle all child nodes for DIRs */
797 pi.pi_old = &pcn_src.pcn_po_full;
798 pi.pi_new = &pcn_targ.pcn_po_full;
799
800 if (puffs_pn_nodewalk(pu,
801 puffs_path_prefixadj, &pi) != NULL)
802 error = ENOMEM;
803 pu->pu_pathfree(pu, &po_old);
804 }
805 }
806 break;
807 }
808
809 case PUFFS_VN_MKDIR:
810 {
811 struct puffs_vnreq_mkdir *auxt = auxbuf;
812 struct puffs_cn pcn;
813 if (pops->puffs_node_mkdir == NULL) {
814 error = 0;
815 break;
816 }
817
818 pcn.pcn_pkcnp = &auxt->pvnr_cn;
819 if (buildpath) {
820 error = puffs_path_pcnbuild(pu, &pcn,
821 preq->preq_cookie);
822 if (error)
823 break;
824 }
825
826 error = pops->puffs_node_mkdir(pcc,
827 preq->preq_cookie, &auxt->pvnr_newnode,
828 &pcn, &auxt->pvnr_va);
829
830 if (buildpath) {
831 if (error) {
832 pu->pu_pathfree(pu, &pcn.pcn_po_full);
833 } else {
834 struct puffs_node *pn;
835
836 pn = PU_CMAP(pu, auxt->pvnr_newnode);
837 pn->pn_po = pcn.pcn_po_full;
838 }
839 }
840
841 break;
842 }
843
844 case PUFFS_VN_RMDIR:
845 {
846 struct puffs_vnreq_rmdir *auxt = auxbuf;
847 struct puffs_cn pcn;
848 if (pops->puffs_node_rmdir == NULL) {
849 error = 0;
850 break;
851 }
852
853 pcn.pcn_pkcnp = &auxt->pvnr_cn;
854
855 error = pops->puffs_node_rmdir(pcc,
856 preq->preq_cookie, auxt->pvnr_cookie_targ, &pcn);
857 break;
858 }
859
860 case PUFFS_VN_SYMLINK:
861 {
862 struct puffs_vnreq_symlink *auxt = auxbuf;
863 struct puffs_cn pcn;
864 if (pops->puffs_node_symlink == NULL) {
865 error = 0;
866 break;
867 }
868
869 pcn.pcn_pkcnp = &auxt->pvnr_cn;
870 if (buildpath) {
871 error = puffs_path_pcnbuild(pu, &pcn,
872 preq->preq_cookie);
873 if (error)
874 break;
875 }
876
877 error = pops->puffs_node_symlink(pcc,
878 preq->preq_cookie, &auxt->pvnr_newnode,
879 &pcn, &auxt->pvnr_va, auxt->pvnr_link);
880
881 if (buildpath) {
882 if (error) {
883 pu->pu_pathfree(pu, &pcn.pcn_po_full);
884 } else {
885 struct puffs_node *pn;
886
887 pn = PU_CMAP(pu, auxt->pvnr_newnode);
888 pn->pn_po = pcn.pcn_po_full;
889 }
890 }
891
892 break;
893 }
894
895 case PUFFS_VN_READDIR:
896 {
897 struct puffs_vnreq_readdir *auxt = auxbuf;
898 size_t res;
899
900 if (pops->puffs_node_readdir == NULL) {
901 error = 0;
902 break;
903 }
904
905 res = auxt->pvnr_resid;
906 error = pops->puffs_node_readdir(pcc,
907 preq->preq_cookie, auxt->pvnr_dent,
908 &auxt->pvnr_cred, &auxt->pvnr_offset,
909 &auxt->pvnr_resid);
910
911 /* need to move a bit more */
912 preq->preq_buflen = sizeof(struct puffs_vnreq_readdir)
913 + (res - auxt->pvnr_resid);
914 break;
915 }
916
917 case PUFFS_VN_READLINK:
918 {
919 struct puffs_vnreq_readlink *auxt = auxbuf;
920 if (pops->puffs_node_readlink == NULL) {
921 error = EOPNOTSUPP;
922 break;
923 }
924
925 error = pops->puffs_node_readlink(pcc,
926 preq->preq_cookie, &auxt->pvnr_cred,
927 auxt->pvnr_link, &auxt->pvnr_linklen);
928 break;
929 }
930
931 case PUFFS_VN_RECLAIM:
932 {
933 struct puffs_vnreq_reclaim *auxt = auxbuf;
934 if (pops->puffs_node_reclaim == NULL) {
935 error = 0;
936 break;
937 }
938
939 error = pops->puffs_node_reclaim(pcc,
940 preq->preq_cookie, auxt->pvnr_pid);
941 break;
942 }
943
944 case PUFFS_VN_INACTIVE:
945 {
946 struct puffs_vnreq_inactive *auxt = auxbuf;
947 if (pops->puffs_node_inactive == NULL) {
948 error = EOPNOTSUPP;
949 break;
950 }
951
952 error = pops->puffs_node_inactive(pcc,
953 preq->preq_cookie, auxt->pvnr_pid,
954 &auxt->pvnr_backendrefs);
955 break;
956 }
957
958 case PUFFS_VN_PATHCONF:
959 {
960 struct puffs_vnreq_pathconf *auxt = auxbuf;
961 if (pops->puffs_node_pathconf == NULL) {
962 error = 0;
963 break;
964 }
965
966 error = pops->puffs_node_pathconf(pcc,
967 preq->preq_cookie, auxt->pvnr_name,
968 &auxt->pvnr_retval);
969 break;
970 }
971
972 case PUFFS_VN_ADVLOCK:
973 {
974 struct puffs_vnreq_advlock *auxt = auxbuf;
975 if (pops->puffs_node_advlock == NULL) {
976 error = 0;
977 break;
978 }
979
980 error = pops->puffs_node_advlock(pcc,
981 preq->preq_cookie, auxt->pvnr_id, auxt->pvnr_op,
982 &auxt->pvnr_fl, auxt->pvnr_flags);
983 break;
984 }
985
986 case PUFFS_VN_PRINT:
987 {
988 if (pops->puffs_node_print == NULL) {
989 error = 0;
990 break;
991 }
992
993 error = pops->puffs_node_print(pcc,
994 preq->preq_cookie);
995 break;
996 }
997
998 case PUFFS_VN_READ:
999 {
1000 struct puffs_vnreq_read *auxt = auxbuf;
1001 size_t res;
1002
1003 if (pops->puffs_node_read == NULL) {
1004 error = EIO;
1005 break;
1006 }
1007
1008 res = auxt->pvnr_resid;
1009 error = pops->puffs_node_read(pcc,
1010 preq->preq_cookie, auxt->pvnr_data,
1011 auxt->pvnr_offset, &auxt->pvnr_resid,
1012 &auxt->pvnr_cred, auxt->pvnr_ioflag);
1013
1014 /* need to move a bit more */
1015 preq->preq_buflen = sizeof(struct puffs_vnreq_read)
1016 + (res - auxt->pvnr_resid);
1017 break;
1018 }
1019
1020 case PUFFS_VN_WRITE:
1021 {
1022 struct puffs_vnreq_write *auxt = auxbuf;
1023
1024 if (pops->puffs_node_write == NULL) {
1025 error = EIO;
1026 break;
1027 }
1028
1029 error = pops->puffs_node_write(pcc,
1030 preq->preq_cookie, auxt->pvnr_data,
1031 auxt->pvnr_offset, &auxt->pvnr_resid,
1032 &auxt->pvnr_cred, auxt->pvnr_ioflag);
1033
1034 /* don't need to move data back to the kernel */
1035 preq->preq_buflen = sizeof(struct puffs_vnreq_write);
1036 break;
1037 }
1038
1039 /* holy bitrot, ryydman! */
1040 #if 0
1041 case PUFFS_VN_IOCTL:
1042 error = pops->puffs_node_ioctl1(pcc, preq->preq_cookie,
1043 (struct puffs_vnreq_ioctl *)auxbuf, &pop);
1044 if (error != 0)
1045 break;
1046 pop.pso_reqid = preq->preq_id;
1047
1048 /* let the kernel do it's intermediate duty */
1049 error = ioctl(pu->pu_fd, PUFFSSIZEOP, &pop);
1050 /*
1051 * XXX: I don't actually know what the correct
1052 * thing to do in case of an error is, so I'll
1053 * just ignore it for the time being.
1054 */
1055 error = pops->puffs_node_ioctl2(pcc, preq->preq_cookie,
1056 (struct puffs_vnreq_ioctl *)auxbuf, &pop);
1057 break;
1058
1059 case PUFFS_VN_FCNTL:
1060 error = pops->puffs_node_fcntl1(pcc, preq->preq_cookie,
1061 (struct puffs_vnreq_fcntl *)auxbuf, &pop);
1062 if (error != 0)
1063 break;
1064 pop.pso_reqid = preq->preq_id;
1065
1066 /* let the kernel do it's intermediate duty */
1067 error = ioctl(pu->pu_fd, PUFFSSIZEOP, &pop);
1068 /*
1069 * XXX: I don't actually know what the correct
1070 * thing to do in case of an error is, so I'll
1071 * just ignore it for the time being.
1072 */
1073 error = pops->puffs_node_fcntl2(pcc, preq->preq_cookie,
1074 (struct puffs_vnreq_fcntl *)auxbuf, &pop);
1075 break;
1076 #endif
1077
1078 default:
1079 printf("inval op %d\n", preq->preq_optype);
1080 error = EINVAL;
1081 break;
1082 }
1083 } else {
1084 /*
1085 * this one also
1086 */
1087 error = EINVAL;
1088 }
1089
1090 preq->preq_rv = error;
1091
1092 pcc->pcc_rv = rv;
1093 pcc->pcc_flags |= PCC_DONE;
1094 }
1095
1096
1097 #if 0
1098 case PUFFS_VN_POLL:
1099 {
1100 struct puffs_vnreq_poll *auxt = auxbuf;
1101 if (pops->puffs_node_poll == NULL) {
1102 error = 0;
1103 break;
1104 }
1105
1106 error = pops->puffs_node_poll(pcc,
1107 preq->preq_cookie, preq-);
1108 break;
1109 }
1110
1111 case PUFFS_VN_KQFILTER:
1112 {
1113 struct puffs_vnreq_kqfilter *auxt = auxbuf;
1114 if (pops->puffs_node_kqfilter == NULL) {
1115 error = 0;
1116 break;
1117 }
1118
1119 error = pops->puffs_node_kqfilter(pcc,
1120 preq->preq_cookie, );
1121 break;
1122 }
1123
1124 case PUFFS_VN_CLOSEEXTATTR:
1125 {
1126 struct puffs_vnreq_closeextattr *auxt = auxbuf;
1127 if (pops->puffs_closeextattr == NULL) {
1128 error = 0;
1129 break;
1130 }
1131
1132 error = pops->puffs_closeextattr(pcc,
1133 preq->preq_cookie, );
1134 break;
1135 }
1136
1137 case PUFFS_VN_GETEXTATTR:
1138 {
1139 struct puffs_vnreq_getextattr *auxt = auxbuf;
1140 if (pops->puffs_getextattr == NULL) {
1141 error = 0;
1142 break;
1143 }
1144
1145 error = pops->puffs_getextattr(pcc,
1146 preq->preq_cookie, );
1147 break;
1148 }
1149
1150 case PUFFS_VN_LISTEXTATTR:
1151 {
1152 struct puffs_vnreq_listextattr *auxt = auxbuf;
1153 if (pops->puffs_listextattr == NULL) {
1154 error = 0;
1155 break;
1156 }
1157
1158 error = pops->puffs_listextattr(pcc,
1159 preq->preq_cookie, );
1160 break;
1161 }
1162
1163 case PUFFS_VN_OPENEXTATTR:
1164 {
1165 struct puffs_vnreq_openextattr *auxt = auxbuf;
1166 if (pops->puffs_openextattr == NULL) {
1167 error = 0;
1168 break;
1169 }
1170
1171 error = pops->puffs_openextattr(pcc,
1172 preq->preq_cookie, );
1173 break;
1174 }
1175
1176 case PUFFS_VN_DELETEEXTATTR:
1177 {
1178 struct puffs_vnreq_deleteextattr *auxt = auxbuf;
1179 if (pops->puffs_deleteextattr == NULL) {
1180 error = 0;
1181 break;
1182 }
1183
1184 error = pops->puffs_deleteextattr(pcc,
1185 preq->preq_cookie, );
1186 break;
1187 }
1188
1189 case PUFFS_VN_SETEXTATTR:
1190 {
1191 struct puffs_vnreq_setextattr *auxt = auxbuf;
1192 if (pops->puffs_setextattr == NULL) {
1193 error = 0;
1194 break;
1195 }
1196
1197 error = pops->puffs_setextattr(pcc,
1198 preq->preq_cookie, );
1199 break;
1200 }
1201
1202 #endif
1203