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