dispatcher.c revision 1.18 1 /* $NetBSD: dispatcher.c,v 1.18 2007/10/28 18:40:30 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Ulla Tuominen Foundation and the Finnish Cultural Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #if !defined(lint)
33 __RCSID("$NetBSD: dispatcher.c,v 1.18 2007/10/28 18:40:30 pooka Exp $");
34 #endif /* !lint */
35
36 #include <sys/types.h>
37 #include <sys/poll.h>
38
39 #include <assert.h>
40 #include <errno.h>
41 #ifdef PUFFS_WITH_THREADS
42 #include <pthread.h>
43 #endif
44 #include <puffs.h>
45 #include <puffsdump.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49
50 #include "puffs_priv.h"
51
52 static void processresult(struct puffs_cc *, struct puffs_putreq *, int);
53
54 /*
55 * Set the following to 1 to not handle each request on a separate
56 * stack. This is highly volatile kludge, therefore no external
57 * interface.
58 */
59 int puffs_fakecc;
60
61 /*
62 * Set the following to 1 to handle each request in a separate pthread.
63 * This is not exported as it should not be used yet unless having a
64 * very good knowledge of what you're signing up for (libpuffs is not
65 * threadsafe).
66 */
67 int puffs_usethreads;
68
69 #ifdef PUFFS_WITH_THREADS
70 struct puffs_workerargs {
71 struct puffs_cc *pwa_pcc;
72 struct puffs_putreq *pwa_ppr;
73 };
74
75 static void *
76 threadlauncher(void *v)
77 {
78 struct puffs_workerargs *ap = v;
79 struct puffs_cc *pcc = ap->pwa_pcc;
80 struct puffs_putreq *ppr = ap->pwa_ppr;
81
82 free(ap);
83 puffs_docc(pcc, ppr);
84 return NULL;
85 }
86 #endif
87
88 static int
89 dopreq2(struct puffs_usermount *pu, struct puffs_req *preq,
90 struct puffs_putreq *ppr)
91 {
92 struct puffs_cc *pcc;
93 int type;
94
95 if (puffs_fakecc) {
96 type = PCC_FAKECC;
97 } else if (puffs_usethreads) {
98 type = PCC_THREADED;
99 #ifndef PUFFS_WITH_THREADS
100 type = PCC_FAKECC;
101 #endif
102 } else {
103 type = PCC_REALCC;
104 }
105
106 if (puffs_cc_create(pu, preq, type, &pcc) == -1)
107 return errno;
108
109 puffs_cc_setcaller(pcc, preq->preq_pid, preq->preq_lid);
110
111 #ifdef PUFFS_WITH_THREADS
112 if (puffs_usethreads) {
113 struct puffs_workerargs *ap;
114 pthread_attr_t pattr;
115 pthread_t ptid;
116 int rv;
117
118 ap = malloc(sizeof(struct puffs_workerargs));
119 pcc = malloc(sizeof(struct puffs_cc));
120 pcc_init_unreal(pcc, PCC_THREADED);
121 pcc->pcc_pu = pu;
122 pcc->pcc_preq = preq;
123
124 pthread_attr_init(&pattr);
125 pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
126
127 ap->pwa_pcc = pcc;
128 ap->pwa_ppr = ppr;
129
130 rv = pthread_create(&ptid, &pattr, threadlauncher, ap);
131
132 return rv;
133 }
134 #endif
135 puffs_docc(pcc, ppr);
136 return 0;
137 }
138
139 /* user-visible point to handle a request from */
140 int
141 puffs_dopreq(struct puffs_usermount *pu, struct puffs_req *preq,
142 struct puffs_putreq *ppr)
143 {
144 struct puffs_executor *pex;
145
146 /*
147 * XXX: the structure is currently a mess. anyway, trap
148 * the cacheops here already, since they don't need a cc.
149 * I really should get around to revamping the operation
150 * dispatching code one of these days.
151 *
152 * Do the same for error notifications.
153 */
154 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_CACHE) {
155 struct puffs_cacheinfo *pci = (void *)preq;
156
157 if (pu->pu_ops.puffs_cache_write == NULL)
158 return 0;
159
160 pu->pu_ops.puffs_cache_write(pu, preq->preq_cookie,
161 pci->pcache_nruns, pci->pcache_runs);
162 return 0;
163 } else if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_ERROR) {
164 struct puffs_error *perr = (void *)preq;
165
166 pu->pu_errnotify(pu, preq->preq_optype,
167 perr->perr_error, perr->perr_str, preq->preq_cookie);
168 return 0;
169 }
170
171 if (pu->pu_flags & PUFFS_FLAG_OPDUMP)
172 puffsdump_req(preq);
173
174 /*
175 * Check if we are already processing an operation from the same
176 * caller. If so, queue this operation until the previous one
177 * finishes. This prevents us from executing certain operations
178 * out-of-order (e.g. fsync and reclaim).
179 *
180 * Each preq will only remove its own pex from the tailq.
181 * See puffs_docc() for the details on other-end removal
182 * and dispatching.
183 */
184 pex = malloc(sizeof(struct puffs_executor));
185 pex->pex_preq = preq;
186 /* mutex_enter */
187 TAILQ_INSERT_TAIL(&pu->pu_exq, pex, pex_entries);
188 TAILQ_FOREACH(pex, &pu->pu_exq, pex_entries) {
189 if (pex->pex_preq->preq_pid == preq->preq_pid
190 && pex->pex_preq->preq_lid == preq->preq_lid) {
191 if (pex->pex_preq != preq) {
192 /* mutex_exit */
193 return 0;
194 }
195 }
196 }
197
198 return dopreq2(pu, preq, ppr);
199 }
200
201 enum {PUFFCALL_ANSWER, PUFFCALL_IGNORE, PUFFCALL_AGAIN};
202
203 /* user-visible continuation point */
204 void
205 puffs_docc(struct puffs_cc *pcc, struct puffs_putreq *ppr)
206 {
207 struct puffs_usermount *pu = pcc->pcc_pu;
208 struct puffs_req *preq;
209 struct puffs_cc *pcc_iter;
210 struct puffs_executor *pex;
211 int found;
212
213 assert((pcc->pcc_flags & PCC_DONE) == 0);
214 pcc->pcc_ppr = ppr;
215
216 if (pcc->pcc_flags & PCC_REALCC)
217 puffs_cc_continue(pcc);
218 else
219 puffs_calldispatcher(pcc);
220
221 /* check if we need to schedule FAFs which were stalled */
222 found = 0;
223 preq = pcc->pcc_preq;
224 /* mutex_enter */
225 TAILQ_FOREACH(pex, &pu->pu_exq, pex_entries) {
226 if (pex->pex_preq->preq_pid == preq->preq_pid
227 && pex->pex_preq->preq_lid == preq->preq_lid) {
228 if (found == 0) {
229 /* this is us */
230 assert(pex->pex_preq == preq);
231 TAILQ_REMOVE(&pu->pu_exq, pex, pex_entries);
232 free(pex);
233 found = 1;
234 } else {
235 /* down at the mardi gras */
236 dopreq2(pu, pex->pex_preq, ppr);
237 break;
238 }
239 }
240 }
241 /* mutex_exit */
242
243 /* can't do this above due to PCC_BORROWED */
244 while ((pcc_iter = LIST_FIRST(&pu->pu_ccnukelst)) != NULL) {
245 LIST_REMOVE(pcc_iter, nlst_entries);
246 puffs_cc_destroy(pcc_iter);
247 }
248 }
249
250 /* library private, but linked from callcontext.c */
251
252 void
253 puffs_calldispatcher(struct puffs_cc *pcc)
254 {
255 struct puffs_usermount *pu = pcc->pcc_pu;
256 struct puffs_ops *pops = &pu->pu_ops;
257 struct puffs_req *preq = pcc->pcc_preq;
258 void *auxbuf = preq; /* help with typecasting */
259 void *opcookie = preq->preq_cookie;
260 int error, rv, buildpath;
261
262 assert(pcc->pcc_flags & (PCC_FAKECC | PCC_REALCC | PCC_THREADED));
263
264 if (PUFFSOP_WANTREPLY(preq->preq_opclass))
265 rv = PUFFCALL_ANSWER;
266 else
267 rv = PUFFCALL_IGNORE;
268
269 buildpath = pu->pu_flags & PUFFS_FLAG_BUILDPATH;
270 preq->preq_setbacks = 0;
271
272 if (pu->pu_oppre)
273 pu->pu_oppre(pcc);
274
275 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VFS) {
276 switch (preq->preq_optype) {
277 case PUFFS_VFS_UNMOUNT:
278 {
279 struct puffs_vfsmsg_unmount *auxt = auxbuf;
280 PUFFS_MAKECID(pcid, &auxt->pvfsr_cid);
281
282 PU_SETSTATE(pu, PUFFS_STATE_UNMOUNTING);
283 error = pops->puffs_fs_unmount(pcc,
284 auxt->pvfsr_flags, pcid);
285 if (!error)
286 PU_SETSTATE(pu, PUFFS_STATE_UNMOUNTED);
287 else
288 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
289 break;
290 }
291
292 case PUFFS_VFS_STATVFS:
293 {
294 struct puffs_vfsmsg_statvfs *auxt = auxbuf;
295 PUFFS_MAKECID(pcid, &auxt->pvfsr_cid);
296
297 error = pops->puffs_fs_statvfs(pcc,
298 &auxt->pvfsr_sb, pcid);
299 break;
300 }
301
302 case PUFFS_VFS_SYNC:
303 {
304 struct puffs_vfsmsg_sync *auxt = auxbuf;
305 PUFFS_MAKECRED(pcr, &auxt->pvfsr_cred);
306 PUFFS_MAKECID(pcid, &auxt->pvfsr_cid);
307
308 error = pops->puffs_fs_sync(pcc,
309 auxt->pvfsr_waitfor, pcr, pcid);
310 break;
311 }
312
313 case PUFFS_VFS_FHTOVP:
314 {
315 struct puffs_vfsmsg_fhtonode *auxt = auxbuf;
316 struct puffs_newinfo pni;
317
318 pni.pni_cookie = &auxt->pvfsr_fhcookie;
319 pni.pni_vtype = &auxt->pvfsr_vtype;
320 pni.pni_size = &auxt->pvfsr_size;
321 pni.pni_rdev = &auxt->pvfsr_rdev;
322
323 error = pops->puffs_fs_fhtonode(pcc, auxt->pvfsr_data,
324 auxt->pvfsr_dsize, &pni);
325
326 break;
327 }
328
329 case PUFFS_VFS_VPTOFH:
330 {
331 struct puffs_vfsmsg_nodetofh *auxt = auxbuf;
332
333 error = pops->puffs_fs_nodetofh(pcc,
334 auxt->pvfsr_fhcookie, auxt->pvfsr_data,
335 &auxt->pvfsr_dsize);
336
337 break;
338 }
339
340 case PUFFS_VFS_SUSPEND:
341 {
342 struct puffs_vfsmsg_suspend *auxt = auxbuf;
343
344 error = 0;
345 if (pops->puffs_fs_suspend == NULL)
346 break;
347
348 pops->puffs_fs_suspend(pcc, auxt->pvfsr_status);
349 break;
350 }
351
352 default:
353 /*
354 * I guess the kernel sees this one coming
355 */
356 error = EINVAL;
357 break;
358 }
359
360 /* XXX: audit return values */
361 /* XXX: sync with kernel */
362 } else if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN) {
363 switch (preq->preq_optype) {
364 case PUFFS_VN_LOOKUP:
365 {
366 struct puffs_vnmsg_lookup *auxt = auxbuf;
367 struct puffs_newinfo pni;
368 struct puffs_cn pcn;
369
370 pcn.pcn_pkcnp = &auxt->pvnr_cn;
371 PUFFS_KCREDTOCRED(pcn.pcn_cred, &auxt->pvnr_cn_cred);
372 PUFFS_KCIDTOCID(pcn.pcn_cid, &auxt->pvnr_cn_cid);
373 pni.pni_cookie = &auxt->pvnr_newnode;
374 pni.pni_vtype = &auxt->pvnr_vtype;
375 pni.pni_size = &auxt->pvnr_size;
376 pni.pni_rdev = &auxt->pvnr_rdev;
377
378 if (buildpath) {
379 error = puffs_path_pcnbuild(pu, &pcn, opcookie);
380 if (error)
381 break;
382 }
383
384 /* lookup *must* be present */
385 error = pops->puffs_node_lookup(pcc, opcookie,
386 &pni, &pcn);
387
388 if (buildpath) {
389 if (error) {
390 pu->pu_pathfree(pu, &pcn.pcn_po_full);
391 } else {
392 struct puffs_node *pn;
393
394 /*
395 * did we get a new node or a
396 * recycled node?
397 */
398 pn = PU_CMAP(pu, auxt->pvnr_newnode);
399 if (pn->pn_po.po_path == NULL)
400 pn->pn_po = pcn.pcn_po_full;
401 else
402 pu->pu_pathfree(pu,
403 &pcn.pcn_po_full);
404 }
405 }
406
407 break;
408 }
409
410 case PUFFS_VN_CREATE:
411 {
412 struct puffs_vnmsg_create *auxt = auxbuf;
413 struct puffs_newinfo pni;
414 struct puffs_cn pcn;
415
416 if (pops->puffs_node_create == NULL) {
417 error = 0;
418 break;
419 }
420
421 pcn.pcn_pkcnp = &auxt->pvnr_cn;
422 PUFFS_KCREDTOCRED(pcn.pcn_cred, &auxt->pvnr_cn_cred);
423 PUFFS_KCIDTOCID(pcn.pcn_cid, &auxt->pvnr_cn_cid);
424
425 memset(&pni, 0, sizeof(pni));
426 pni.pni_cookie = &auxt->pvnr_newnode;
427
428 if (buildpath) {
429 error = puffs_path_pcnbuild(pu, &pcn, opcookie);
430 if (error)
431 break;
432 }
433
434 error = pops->puffs_node_create(pcc,
435 opcookie, &pni, &pcn, &auxt->pvnr_va);
436
437 if (buildpath) {
438 if (error) {
439 pu->pu_pathfree(pu, &pcn.pcn_po_full);
440 } else {
441 struct puffs_node *pn;
442
443 pn = PU_CMAP(pu, auxt->pvnr_newnode);
444 pn->pn_po = pcn.pcn_po_full;
445 }
446 }
447
448 break;
449 }
450
451 case PUFFS_VN_MKNOD:
452 {
453 struct puffs_vnmsg_mknod *auxt = auxbuf;
454 struct puffs_newinfo pni;
455 struct puffs_cn pcn;
456
457 if (pops->puffs_node_mknod == NULL) {
458 error = 0;
459 break;
460 }
461
462 pcn.pcn_pkcnp = &auxt->pvnr_cn;
463 PUFFS_KCREDTOCRED(pcn.pcn_cred, &auxt->pvnr_cn_cred);
464 PUFFS_KCIDTOCID(pcn.pcn_cid, &auxt->pvnr_cn_cid);
465
466 memset(&pni, 0, sizeof(pni));
467 pni.pni_cookie = &auxt->pvnr_newnode;
468
469 if (buildpath) {
470 error = puffs_path_pcnbuild(pu, &pcn, opcookie);
471 if (error)
472 break;
473 }
474
475 error = pops->puffs_node_mknod(pcc,
476 opcookie, &pni, &pcn, &auxt->pvnr_va);
477
478 if (buildpath) {
479 if (error) {
480 pu->pu_pathfree(pu, &pcn.pcn_po_full);
481 } else {
482 struct puffs_node *pn;
483
484 pn = PU_CMAP(pu, auxt->pvnr_newnode);
485 pn->pn_po = pcn.pcn_po_full;
486 }
487 }
488
489 break;
490 }
491
492 case PUFFS_VN_OPEN:
493 {
494 struct puffs_vnmsg_open *auxt = auxbuf;
495 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
496 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
497
498 if (pops->puffs_node_open == NULL) {
499 error = 0;
500 break;
501 }
502
503 error = pops->puffs_node_open(pcc,
504 opcookie, auxt->pvnr_mode, pcr, pcid);
505 break;
506 }
507
508 case PUFFS_VN_CLOSE:
509 {
510 struct puffs_vnmsg_close *auxt = auxbuf;
511 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
512 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
513
514
515 if (pops->puffs_node_close == NULL) {
516 error = 0;
517 break;
518 }
519
520 error = pops->puffs_node_close(pcc,
521 opcookie, auxt->pvnr_fflag, pcr, pcid);
522 break;
523 }
524
525 case PUFFS_VN_ACCESS:
526 {
527 struct puffs_vnmsg_access *auxt = auxbuf;
528 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
529 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
530
531
532 if (pops->puffs_node_access == NULL) {
533 error = 0;
534 break;
535 }
536
537 error = pops->puffs_node_access(pcc,
538 opcookie, auxt->pvnr_mode, pcr, pcid);
539 break;
540 }
541
542 case PUFFS_VN_GETATTR:
543 {
544 struct puffs_vnmsg_getattr *auxt = auxbuf;
545 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
546 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
547
548
549 if (pops->puffs_node_getattr == NULL) {
550 error = EOPNOTSUPP;
551 break;
552 }
553
554 error = pops->puffs_node_getattr(pcc,
555 opcookie, &auxt->pvnr_va, pcr, pcid);
556 break;
557 }
558
559 case PUFFS_VN_SETATTR:
560 {
561 struct puffs_vnmsg_setattr *auxt = auxbuf;
562 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
563 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
564
565
566 if (pops->puffs_node_setattr == NULL) {
567 error = EOPNOTSUPP;
568 break;
569 }
570
571 error = pops->puffs_node_setattr(pcc,
572 opcookie, &auxt->pvnr_va, pcr, pcid);
573 break;
574 }
575
576 case PUFFS_VN_MMAP:
577 {
578 struct puffs_vnmsg_mmap *auxt = auxbuf;
579 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
580 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
581
582
583 if (pops->puffs_node_mmap == NULL) {
584 error = 0;
585 break;
586 }
587
588 error = pops->puffs_node_mmap(pcc,
589 opcookie, auxt->pvnr_prot, pcr, pcid);
590 break;
591 }
592
593 case PUFFS_VN_FSYNC:
594 {
595 struct puffs_vnmsg_fsync *auxt = auxbuf;
596 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
597 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
598
599
600 if (pops->puffs_node_fsync == NULL) {
601 error = 0;
602 break;
603 }
604
605 error = pops->puffs_node_fsync(pcc, opcookie, pcr,
606 auxt->pvnr_flags, auxt->pvnr_offlo,
607 auxt->pvnr_offhi, pcid);
608 break;
609 }
610
611 case PUFFS_VN_SEEK:
612 {
613 struct puffs_vnmsg_seek *auxt = auxbuf;
614 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
615
616 if (pops->puffs_node_seek == NULL) {
617 error = 0;
618 break;
619 }
620
621 error = pops->puffs_node_seek(pcc,
622 opcookie, auxt->pvnr_oldoff,
623 auxt->pvnr_newoff, pcr);
624 break;
625 }
626
627 case PUFFS_VN_REMOVE:
628 {
629 struct puffs_vnmsg_remove *auxt = auxbuf;
630 struct puffs_cn pcn;
631 if (pops->puffs_node_remove == NULL) {
632 error = 0;
633 break;
634 }
635
636 pcn.pcn_pkcnp = &auxt->pvnr_cn;
637 PUFFS_KCREDTOCRED(pcn.pcn_cred, &auxt->pvnr_cn_cred);
638 PUFFS_KCIDTOCID(pcn.pcn_cid, &auxt->pvnr_cn_cid);
639
640 error = pops->puffs_node_remove(pcc,
641 opcookie, auxt->pvnr_cookie_targ, &pcn);
642 break;
643 }
644
645 case PUFFS_VN_LINK:
646 {
647 struct puffs_vnmsg_link *auxt = auxbuf;
648 struct puffs_cn pcn;
649 if (pops->puffs_node_link == NULL) {
650 error = 0;
651 break;
652 }
653
654 pcn.pcn_pkcnp = &auxt->pvnr_cn;
655 PUFFS_KCREDTOCRED(pcn.pcn_cred, &auxt->pvnr_cn_cred);
656 PUFFS_KCIDTOCID(pcn.pcn_cid, &auxt->pvnr_cn_cid);
657
658 if (buildpath) {
659 error = puffs_path_pcnbuild(pu, &pcn, opcookie);
660 if (error)
661 break;
662 }
663
664 error = pops->puffs_node_link(pcc,
665 opcookie, auxt->pvnr_cookie_targ, &pcn);
666 if (buildpath)
667 pu->pu_pathfree(pu, &pcn.pcn_po_full);
668
669 break;
670 }
671
672 case PUFFS_VN_RENAME:
673 {
674 struct puffs_vnmsg_rename *auxt = auxbuf;
675 struct puffs_cn pcn_src, pcn_targ;
676 struct puffs_node *pn_src;
677
678 if (pops->puffs_node_rename == NULL) {
679 error = 0;
680 break;
681 }
682
683 pcn_src.pcn_pkcnp = &auxt->pvnr_cn_src;
684 PUFFS_KCREDTOCRED(pcn_src.pcn_cred,
685 &auxt->pvnr_cn_src_cred);
686 PUFFS_KCIDTOCID(pcn_src.pcn_cid,
687 &auxt->pvnr_cn_src_cid);
688
689 pcn_targ.pcn_pkcnp = &auxt->pvnr_cn_targ;
690 PUFFS_KCREDTOCRED(pcn_targ.pcn_cred,
691 &auxt->pvnr_cn_targ_cred);
692 PUFFS_KCIDTOCID(pcn_targ.pcn_cid,
693 &auxt->pvnr_cn_targ_cid);
694
695 if (buildpath) {
696 pn_src = auxt->pvnr_cookie_src;
697 pcn_src.pcn_po_full = pn_src->pn_po;
698
699 error = puffs_path_pcnbuild(pu, &pcn_targ,
700 auxt->pvnr_cookie_targdir);
701 if (error)
702 break;
703 }
704
705 error = pops->puffs_node_rename(pcc,
706 opcookie, auxt->pvnr_cookie_src,
707 &pcn_src, auxt->pvnr_cookie_targdir,
708 auxt->pvnr_cookie_targ, &pcn_targ);
709
710 if (buildpath) {
711 if (error) {
712 pu->pu_pathfree(pu,
713 &pcn_targ.pcn_po_full);
714 } else {
715 struct puffs_pathinfo pi;
716 struct puffs_pathobj po_old;
717
718 /* handle this node */
719 po_old = pn_src->pn_po;
720 pn_src->pn_po = pcn_targ.pcn_po_full;
721
722 if (pn_src->pn_va.va_type != VDIR) {
723 pu->pu_pathfree(pu, &po_old);
724 break;
725 }
726
727 /* handle all child nodes for DIRs */
728 pi.pi_old = &pcn_src.pcn_po_full;
729 pi.pi_new = &pcn_targ.pcn_po_full;
730
731 if (puffs_pn_nodewalk(pu,
732 puffs_path_prefixadj, &pi) != NULL)
733 error = ENOMEM;
734 pu->pu_pathfree(pu, &po_old);
735 }
736 }
737 break;
738 }
739
740 case PUFFS_VN_MKDIR:
741 {
742 struct puffs_vnmsg_mkdir *auxt = auxbuf;
743 struct puffs_newinfo pni;
744 struct puffs_cn pcn;
745
746 if (pops->puffs_node_mkdir == NULL) {
747 error = 0;
748 break;
749 }
750
751 pcn.pcn_pkcnp = &auxt->pvnr_cn;
752 PUFFS_KCREDTOCRED(pcn.pcn_cred, &auxt->pvnr_cn_cred);
753 PUFFS_KCIDTOCID(pcn.pcn_cid, &auxt->pvnr_cn_cid);
754
755 memset(&pni, 0, sizeof(pni));
756 pni.pni_cookie = &auxt->pvnr_newnode;
757
758 if (buildpath) {
759 error = puffs_path_pcnbuild(pu, &pcn, opcookie);
760 if (error)
761 break;
762 }
763
764 error = pops->puffs_node_mkdir(pcc,
765 opcookie, &pni, &pcn, &auxt->pvnr_va);
766
767 if (buildpath) {
768 if (error) {
769 pu->pu_pathfree(pu, &pcn.pcn_po_full);
770 } else {
771 struct puffs_node *pn;
772
773 pn = PU_CMAP(pu, auxt->pvnr_newnode);
774 pn->pn_po = pcn.pcn_po_full;
775 }
776 }
777
778 break;
779 }
780
781 case PUFFS_VN_RMDIR:
782 {
783 struct puffs_vnmsg_rmdir *auxt = auxbuf;
784 struct puffs_cn pcn;
785 if (pops->puffs_node_rmdir == NULL) {
786 error = 0;
787 break;
788 }
789
790 pcn.pcn_pkcnp = &auxt->pvnr_cn;
791 PUFFS_KCREDTOCRED(pcn.pcn_cred, &auxt->pvnr_cn_cred);
792 PUFFS_KCIDTOCID(pcn.pcn_cid, &auxt->pvnr_cn_cid);
793
794 error = pops->puffs_node_rmdir(pcc,
795 opcookie, auxt->pvnr_cookie_targ, &pcn);
796 break;
797 }
798
799 case PUFFS_VN_SYMLINK:
800 {
801 struct puffs_vnmsg_symlink *auxt = auxbuf;
802 struct puffs_newinfo pni;
803 struct puffs_cn pcn;
804
805 if (pops->puffs_node_symlink == NULL) {
806 error = 0;
807 break;
808 }
809
810 pcn.pcn_pkcnp = &auxt->pvnr_cn;
811 PUFFS_KCREDTOCRED(pcn.pcn_cred, &auxt->pvnr_cn_cred);
812 PUFFS_KCIDTOCID(pcn.pcn_cid, &auxt->pvnr_cn_cid);
813
814 memset(&pni, 0, sizeof(pni));
815 pni.pni_cookie = &auxt->pvnr_newnode;
816
817 if (buildpath) {
818 error = puffs_path_pcnbuild(pu, &pcn, opcookie);
819 if (error)
820 break;
821 }
822
823 error = pops->puffs_node_symlink(pcc,
824 opcookie, &pni, &pcn,
825 &auxt->pvnr_va, auxt->pvnr_link);
826
827 if (buildpath) {
828 if (error) {
829 pu->pu_pathfree(pu, &pcn.pcn_po_full);
830 } else {
831 struct puffs_node *pn;
832
833 pn = PU_CMAP(pu, auxt->pvnr_newnode);
834 pn->pn_po = pcn.pcn_po_full;
835 }
836 }
837
838 break;
839 }
840
841 case PUFFS_VN_READDIR:
842 {
843 struct puffs_vnmsg_readdir *auxt = auxbuf;
844 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
845 struct dirent *dent;
846 off_t *cookies;
847 size_t res, origcookies;
848
849 if (pops->puffs_node_readdir == NULL) {
850 error = 0;
851 break;
852 }
853
854 if (auxt->pvnr_ncookies) {
855 /* LINTED: pvnr_data is __aligned() */
856 cookies = (off_t *)auxt->pvnr_data;
857 origcookies = auxt->pvnr_ncookies;
858 } else {
859 cookies = NULL;
860 origcookies = 0;
861 }
862 /* LINTED: dentoff is aligned in the kernel */
863 dent = (struct dirent *)
864 (auxt->pvnr_data + auxt->pvnr_dentoff);
865
866 res = auxt->pvnr_resid;
867 error = pops->puffs_node_readdir(pcc,
868 opcookie, dent, &auxt->pvnr_offset,
869 &auxt->pvnr_resid, pcr, &auxt->pvnr_eofflag,
870 cookies, &auxt->pvnr_ncookies);
871
872 /* much easier to track non-working NFS */
873 assert(auxt->pvnr_ncookies <= origcookies);
874
875 /* need to move a bit more */
876 preq->preq_buflen = sizeof(struct puffs_vnmsg_readdir)
877 + auxt->pvnr_dentoff + (res - auxt->pvnr_resid);
878 break;
879 }
880
881 case PUFFS_VN_READLINK:
882 {
883 struct puffs_vnmsg_readlink *auxt = auxbuf;
884 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
885
886 if (pops->puffs_node_readlink == NULL) {
887 error = EOPNOTSUPP;
888 break;
889 }
890
891 /*LINTED*/
892 error = pops->puffs_node_readlink(pcc, opcookie, pcr,
893 auxt->pvnr_link, &auxt->pvnr_linklen);
894 break;
895 }
896
897 case PUFFS_VN_RECLAIM:
898 {
899 struct puffs_vnmsg_reclaim *auxt = auxbuf;
900 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
901
902 if (pops->puffs_node_reclaim == NULL) {
903 error = 0;
904 break;
905 }
906
907 error = pops->puffs_node_reclaim(pcc, opcookie, pcid);
908 break;
909 }
910
911 case PUFFS_VN_INACTIVE:
912 {
913 struct puffs_vnmsg_inactive *auxt = auxbuf;
914 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
915
916 if (pops->puffs_node_inactive == NULL) {
917 error = EOPNOTSUPP;
918 break;
919 }
920
921 error = pops->puffs_node_inactive(pcc, opcookie, pcid);
922 break;
923 }
924
925 case PUFFS_VN_PATHCONF:
926 {
927 struct puffs_vnmsg_pathconf *auxt = auxbuf;
928 if (pops->puffs_node_pathconf == NULL) {
929 error = 0;
930 break;
931 }
932
933 error = pops->puffs_node_pathconf(pcc,
934 opcookie, auxt->pvnr_name,
935 &auxt->pvnr_retval);
936 break;
937 }
938
939 case PUFFS_VN_ADVLOCK:
940 {
941 struct puffs_vnmsg_advlock *auxt = auxbuf;
942 if (pops->puffs_node_advlock == NULL) {
943 error = 0;
944 break;
945 }
946
947 error = pops->puffs_node_advlock(pcc,
948 opcookie, auxt->pvnr_id, auxt->pvnr_op,
949 &auxt->pvnr_fl, auxt->pvnr_flags);
950 break;
951 }
952
953 case PUFFS_VN_PRINT:
954 {
955 if (pops->puffs_node_print == NULL) {
956 error = 0;
957 break;
958 }
959
960 error = pops->puffs_node_print(pcc,
961 opcookie);
962 break;
963 }
964
965 case PUFFS_VN_READ:
966 {
967 struct puffs_vnmsg_read *auxt = auxbuf;
968 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
969 size_t res;
970
971 if (pops->puffs_node_read == NULL) {
972 error = EIO;
973 break;
974 }
975
976 res = auxt->pvnr_resid;
977 error = pops->puffs_node_read(pcc,
978 opcookie, auxt->pvnr_data,
979 auxt->pvnr_offset, &auxt->pvnr_resid,
980 pcr, auxt->pvnr_ioflag);
981
982 /* need to move a bit more */
983 preq->preq_buflen = sizeof(struct puffs_vnmsg_read)
984 + (res - auxt->pvnr_resid);
985 break;
986 }
987
988 case PUFFS_VN_WRITE:
989 {
990 struct puffs_vnmsg_write *auxt = auxbuf;
991 PUFFS_MAKECRED(pcr, &auxt->pvnr_cred);
992
993 if (pops->puffs_node_write == NULL) {
994 error = EIO;
995 break;
996 }
997
998 error = pops->puffs_node_write(pcc,
999 opcookie, auxt->pvnr_data,
1000 auxt->pvnr_offset, &auxt->pvnr_resid,
1001 pcr, auxt->pvnr_ioflag);
1002
1003 /* don't need to move data back to the kernel */
1004 preq->preq_buflen = sizeof(struct puffs_vnmsg_write);
1005 break;
1006 }
1007
1008 case PUFFS_VN_POLL:
1009 {
1010 struct puffs_vnmsg_poll *auxt = auxbuf;
1011 PUFFS_MAKECID(pcid, &auxt->pvnr_cid);
1012
1013 if (pops->puffs_node_poll == NULL) {
1014 error = 0;
1015
1016 /* emulate genfs_poll() */
1017 auxt->pvnr_events &= (POLLIN | POLLOUT
1018 | POLLRDNORM | POLLWRNORM);
1019
1020 break;
1021 }
1022
1023 error = pops->puffs_node_poll(pcc,
1024 opcookie, &auxt->pvnr_events, pcid);
1025 break;
1026 }
1027
1028 default:
1029 printf("inval op %d\n", preq->preq_optype);
1030 error = EINVAL;
1031 break;
1032 }
1033 } else {
1034 /*
1035 * this one also
1036 */
1037 error = EINVAL;
1038 }
1039
1040 preq->preq_rv = error;
1041 pcc->pcc_flags |= PCC_DONE;
1042
1043 if (pu->pu_oppost)
1044 pu->pu_oppost(pcc);
1045
1046 /*
1047 * Note, we are calling this from here so that we can run it
1048 * off of the continuation stack. Otherwise puffs_goto() would
1049 * not work.
1050 */
1051 processresult(pcc, pcc->pcc_ppr, rv);
1052 }
1053
1054 static void
1055 processresult(struct puffs_cc *pcc, struct puffs_putreq *ppr, int how)
1056 {
1057 struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
1058 struct puffs_req *preq = pcc->pcc_preq;
1059 int pccflags = pcc->pcc_flags;
1060
1061 /* check if we need to store this reply */
1062 switch (how) {
1063 case PUFFCALL_ANSWER:
1064 if (pu->pu_flags & PUFFS_FLAG_OPDUMP)
1065 puffsdump_rv(preq);
1066
1067 puffs_req_putcc(ppr, pcc);
1068 break;
1069 case PUFFCALL_IGNORE:
1070 LIST_INSERT_HEAD(&pu->pu_ccnukelst, pcc, nlst_entries);
1071 break;
1072 case PUFFCALL_AGAIN:
1073 if ((pcc->pcc_flags & PCC_REALCC) == 0)
1074 assert(pcc->pcc_flags & PCC_DONE);
1075 break;
1076 default:
1077 assert(/*CONSTCOND*/0);
1078 }
1079
1080 /* who needs information when you're living on borrowed time? */
1081 if (pccflags & PCC_BORROWED) {
1082 assert((pccflags & PCC_THREADED) == 0);
1083 puffs_cc_yield(pcc); /* back to borrow source */
1084 }
1085 }
1086