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