puffs_msgif.c revision 1.80.2.1 1 /* $NetBSD: puffs_msgif.c,v 1.80.2.1 2010/08/17 06:47:19 uebayasi Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006, 2007 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: puffs_msgif.c,v 1.80.2.1 2010/08/17 06:47:19 uebayasi Exp $");
34
35 #include <sys/param.h>
36 #include <sys/atomic.h>
37 #include <sys/kmem.h>
38 #include <sys/kthread.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
43 #include <sys/proc.h>
44 #include <sys/vnode.h>
45 #include <sys/atomic.h>
46
47 #include <dev/putter/putter_sys.h>
48
49 #include <fs/puffs/puffs_msgif.h>
50 #include <fs/puffs/puffs_sys.h>
51
52 #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
53
54 /*
55 * waitq data structures
56 */
57
58 /*
59 * While a request is going to userspace, park the caller within the
60 * kernel. This is the kernel counterpart of "struct puffs_req".
61 */
62 struct puffs_msgpark {
63 struct puffs_req *park_preq; /* req followed by buf */
64
65 size_t park_copylen; /* userspace copylength */
66 size_t park_maxlen; /* max size in comeback */
67
68 struct puffs_req *park_creq; /* non-compat preq */
69 size_t park_creqlen; /* non-compat preq len */
70
71 parkdone_fn park_done; /* "biodone" a'la puffs */
72 void *park_donearg;
73
74 int park_flags;
75 int park_refcount;
76
77 kcondvar_t park_cv;
78 kmutex_t park_mtx;
79
80 TAILQ_ENTRY(puffs_msgpark) park_entries;
81 };
82 #define PARKFLAG_WAITERGONE 0x01
83 #define PARKFLAG_DONE 0x02
84 #define PARKFLAG_ONQUEUE1 0x04
85 #define PARKFLAG_ONQUEUE2 0x08
86 #define PARKFLAG_CALL 0x10
87 #define PARKFLAG_WANTREPLY 0x20
88 #define PARKFLAG_HASERROR 0x40
89
90 static pool_cache_t parkpc;
91 #ifdef PUFFSDEBUG
92 static int totalpark;
93 #endif
94
95 static int
96 makepark(void *arg, void *obj, int flags)
97 {
98 struct puffs_msgpark *park = obj;
99
100 mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
101 cv_init(&park->park_cv, "puffsrpl");
102
103 return 0;
104 }
105
106 static void
107 nukepark(void *arg, void *obj)
108 {
109 struct puffs_msgpark *park = obj;
110
111 cv_destroy(&park->park_cv);
112 mutex_destroy(&park->park_mtx);
113 }
114
115 void
116 puffs_msgif_init(void)
117 {
118
119 parkpc = pool_cache_init(sizeof(struct puffs_msgpark), 0, 0, 0,
120 "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
121 }
122
123 void
124 puffs_msgif_destroy(void)
125 {
126
127 pool_cache_destroy(parkpc);
128 }
129
130 static struct puffs_msgpark *
131 puffs_msgpark_alloc(int waitok)
132 {
133 struct puffs_msgpark *park;
134
135 park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
136 if (park == NULL)
137 return park;
138
139 park->park_refcount = 1;
140 park->park_preq = park->park_creq = NULL;
141 park->park_flags = PARKFLAG_WANTREPLY;
142
143 #ifdef PUFFSDEBUG
144 totalpark++;
145 #endif
146
147 return park;
148 }
149
150 static void
151 puffs_msgpark_reference(struct puffs_msgpark *park)
152 {
153
154 KASSERT(mutex_owned(&park->park_mtx));
155 park->park_refcount++;
156 }
157
158 /*
159 * Release reference to park structure.
160 */
161 static void
162 puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
163 {
164 struct puffs_req *preq = park->park_preq;
165 struct puffs_req *creq = park->park_creq;
166 int refcnt;
167
168 KASSERT(mutex_owned(&park->park_mtx));
169 refcnt = park->park_refcount -= howmany;
170 mutex_exit(&park->park_mtx);
171
172 KASSERT(refcnt >= 0);
173
174 if (refcnt == 0) {
175 if (preq)
176 kmem_free(preq, park->park_maxlen);
177 #if 1
178 if (creq)
179 kmem_free(creq, park->park_creqlen);
180 #endif
181 pool_cache_put(parkpc, park);
182
183 #ifdef PUFFSDEBUG
184 totalpark--;
185 #endif
186 }
187 }
188 #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
189
190 #ifdef PUFFSDEBUG
191 static void
192 parkdump(struct puffs_msgpark *park)
193 {
194
195 DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
196 "\tcopy %zu, max %zu - done: %p/%p\n"
197 "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
198 park, park->park_preq, park->park_preq->preq_id,
199 park->park_copylen, park->park_maxlen,
200 park->park_done, park->park_donearg,
201 park->park_flags, park->park_refcount,
202 &park->park_cv, &park->park_mtx));
203 }
204
205 static void
206 parkqdump(struct puffs_wq *q, int dumpall)
207 {
208 struct puffs_msgpark *park;
209 int total = 0;
210
211 TAILQ_FOREACH(park, q, park_entries) {
212 if (dumpall)
213 parkdump(park);
214 total++;
215 }
216 DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
217
218 }
219 #endif /* PUFFSDEBUG */
220
221 /*
222 * A word about locking in the park structures: the lock protects the
223 * fields of the *park* structure (not preq) and acts as an interlock
224 * in cv operations. The lock is always internal to this module and
225 * callers do not need to worry about it.
226 */
227
228 int
229 puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
230 int cansleep)
231 {
232 struct puffs_msgpark *park;
233 void *m;
234
235 m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
236 if (m == NULL) {
237 KASSERT(cansleep == 0);
238 return ENOMEM;
239 }
240
241 park = puffs_msgpark_alloc(cansleep);
242 if (park == NULL) {
243 KASSERT(cansleep == 0);
244 kmem_free(m, len);
245 return ENOMEM;
246 }
247
248 park->park_preq = m;
249 park->park_maxlen = park->park_copylen = len;
250
251 *ppark = park;
252 *mem = m;
253
254 return 0;
255 }
256
257 void
258 puffs_msgmem_release(struct puffs_msgpark *park)
259 {
260
261 if (park == NULL)
262 return;
263
264 mutex_enter(&park->park_mtx);
265 puffs_msgpark_release(park);
266 }
267
268 void
269 puffs_msg_setfaf(struct puffs_msgpark *park)
270 {
271
272 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
273 park->park_flags &= ~PARKFLAG_WANTREPLY;
274 }
275
276 void
277 puffs_msg_setdelta(struct puffs_msgpark *park, size_t delta)
278 {
279
280 KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
281 park->park_copylen = park->park_maxlen - delta;
282 }
283
284 void
285 puffs_msg_setinfo(struct puffs_msgpark *park, int class, int type,
286 puffs_cookie_t ck)
287 {
288
289 park->park_preq->preq_opclass = PUFFSOP_OPCLASS(class);
290 park->park_preq->preq_optype = type;
291 park->park_preq->preq_cookie = ck;
292 }
293
294 void
295 puffs_msg_setcall(struct puffs_msgpark *park, parkdone_fn donefn, void *donearg)
296 {
297
298 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
299 park->park_done = donefn;
300 park->park_donearg = donearg;
301 park->park_flags |= PARKFLAG_CALL;
302 }
303
304 /*
305 * kernel-user-kernel waitqueues
306 */
307
308 static uint64_t
309 puffs_getmsgid(struct puffs_mount *pmp)
310 {
311 uint64_t rv;
312
313 mutex_enter(&pmp->pmp_lock);
314 rv = pmp->pmp_nextmsgid++;
315 mutex_exit(&pmp->pmp_lock);
316
317 return rv;
318 }
319
320 /*
321 * A word about reference counting of parks. A reference must be taken
322 * when accessing a park and additionally when it is on a queue. So
323 * when taking it off a queue and releasing the access reference, the
324 * reference count is generally decremented by 2.
325 */
326
327 void
328 puffs_msg_enqueue(struct puffs_mount *pmp, struct puffs_msgpark *park)
329 {
330 struct lwp *l = curlwp;
331 struct mount *mp;
332 struct puffs_req *preq, *creq;
333 ssize_t delta;
334
335 mp = PMPTOMP(pmp);
336 preq = park->park_preq;
337
338 #if 1
339 /* check if we do compat adjustments */
340 if (pmp->pmp_docompat && puffs_compat_outgoing(preq, &creq, &delta)) {
341 park->park_creq = park->park_preq;
342 park->park_creqlen = park->park_maxlen;
343
344 park->park_maxlen += delta;
345 park->park_copylen += delta;
346 park->park_preq = preq = creq;
347 }
348 #endif
349
350 preq->preq_buflen = park->park_maxlen;
351 KASSERT(preq->preq_id == 0
352 || (preq->preq_opclass & PUFFSOPFLAG_ISRESPONSE));
353
354 if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
355 preq->preq_opclass |= PUFFSOPFLAG_FAF;
356 else
357 preq->preq_id = puffs_getmsgid(pmp);
358
359 /* fill in caller information */
360 preq->preq_pid = l->l_proc->p_pid;
361 preq->preq_lid = l->l_lid;
362
363 /*
364 * To support cv_sig, yet another movie: check if there are signals
365 * pending and we are issueing a non-FAF. If so, return an error
366 * directly UNLESS we are issueing INACTIVE/RECLAIM. In that case,
367 * convert it to a FAF, fire off to the file server and return
368 * an error. Yes, this is bordering disgusting. Barfbags are on me.
369 */
370 if (__predict_false((park->park_flags & PARKFLAG_WANTREPLY)
371 && (park->park_flags & PARKFLAG_CALL) == 0
372 && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))) {
373 park->park_flags |= PARKFLAG_HASERROR;
374 preq->preq_rv = EINTR;
375 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
376 && (preq->preq_optype == PUFFS_VN_INACTIVE
377 || preq->preq_optype == PUFFS_VN_RECLAIM)) {
378 park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
379 park->park_flags &= ~PARKFLAG_WANTREPLY;
380 DPRINTF(("puffs_msg_enqueue: converted to FAF %p\n",
381 park));
382 } else {
383 return;
384 }
385 }
386
387 mutex_enter(&pmp->pmp_lock);
388 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
389 mutex_exit(&pmp->pmp_lock);
390 park->park_flags |= PARKFLAG_HASERROR;
391 preq->preq_rv = ENXIO;
392 return;
393 }
394
395 #ifdef PUFFSDEBUG
396 parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
397 parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
398 #endif
399
400 /*
401 * Note: we don't need to lock park since we have the only
402 * reference to it at this point.
403 */
404 TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
405 park->park_flags |= PARKFLAG_ONQUEUE1;
406 pmp->pmp_msg_touser_count++;
407 park->park_refcount++;
408 mutex_exit(&pmp->pmp_lock);
409
410 cv_broadcast(&pmp->pmp_msg_waiter_cv);
411 putter_notify(pmp->pmp_pi);
412
413 DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
414 "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
415 preq->preq_opclass, preq->preq_optype, park->park_flags));
416 }
417
418 int
419 puffs_msg_wait(struct puffs_mount *pmp, struct puffs_msgpark *park)
420 {
421 struct puffs_req *preq = park->park_preq; /* XXX: hmmm */
422 int error = 0;
423 int rv;
424
425 mutex_enter(&pmp->pmp_lock);
426 puffs_mp_reference(pmp);
427 mutex_exit(&pmp->pmp_lock);
428
429 mutex_enter(&park->park_mtx);
430 if ((park->park_flags & PARKFLAG_WANTREPLY) == 0
431 || (park->park_flags & PARKFLAG_CALL)) {
432 mutex_exit(&park->park_mtx);
433 rv = 0;
434 goto skipwait;
435 }
436
437 /* did the response beat us to the wait? */
438 if (__predict_false((park->park_flags & PARKFLAG_DONE)
439 || (park->park_flags & PARKFLAG_HASERROR))) {
440 rv = park->park_preq->preq_rv;
441 mutex_exit(&park->park_mtx);
442 goto skipwait;
443 }
444
445 error = cv_wait_sig(&park->park_cv, &park->park_mtx);
446 DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
447 park, error));
448 if (error) {
449 park->park_flags |= PARKFLAG_WAITERGONE;
450 if (park->park_flags & PARKFLAG_DONE) {
451 rv = preq->preq_rv;
452 mutex_exit(&park->park_mtx);
453 } else {
454 /*
455 * ok, we marked it as going away, but
456 * still need to do queue ops. take locks
457 * in correct order.
458 *
459 * We don't want to release our reference
460 * if it's on replywait queue to avoid error
461 * to file server. putop() code will DTRT.
462 */
463 mutex_exit(&park->park_mtx);
464 mutex_enter(&pmp->pmp_lock);
465 mutex_enter(&park->park_mtx);
466
467 /*
468 * Still on queue1? We can safely remove it
469 * without any consequences since the file
470 * server hasn't seen it. "else" we need to
471 * wait for the response and just ignore it
472 * to avoid signalling an incorrect error to
473 * the file server.
474 */
475 if (park->park_flags & PARKFLAG_ONQUEUE1) {
476 TAILQ_REMOVE(&pmp->pmp_msg_touser,
477 park, park_entries);
478 puffs_msgpark_release(park);
479 pmp->pmp_msg_touser_count--;
480 park->park_flags &= ~PARKFLAG_ONQUEUE1;
481 } else {
482 mutex_exit(&park->park_mtx);
483 }
484 mutex_exit(&pmp->pmp_lock);
485
486 rv = EINTR;
487 }
488 } else {
489 rv = preq->preq_rv;
490 mutex_exit(&park->park_mtx);
491 }
492
493 skipwait:
494 mutex_enter(&pmp->pmp_lock);
495 puffs_mp_release(pmp);
496 mutex_exit(&pmp->pmp_lock);
497
498 return rv;
499 }
500
501 /*
502 * XXX: this suuuucks. Hopefully I'll get rid of this lossage once
503 * the whole setback-nonsense gets fixed.
504 */
505 int
506 puffs_msg_wait2(struct puffs_mount *pmp, struct puffs_msgpark *park,
507 struct puffs_node *pn1, struct puffs_node *pn2)
508 {
509 struct puffs_req *preq;
510 int rv;
511
512 rv = puffs_msg_wait(pmp, park);
513
514 preq = park->park_preq;
515 if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N1)
516 pn1->pn_stat |= PNODE_DOINACT;
517 if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N2)
518 pn2->pn_stat |= PNODE_DOINACT;
519
520 if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1)
521 pn1->pn_stat |= PNODE_NOREFS;
522 if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2)
523 pn2->pn_stat |= PNODE_NOREFS;
524
525 return rv;
526
527 }
528
529 /*
530 * XXX: lazy bum. please, for the love of foie gras, fix me.
531 * This should *NOT* depend on setfaf. Also "memcpy" could
532 * be done more nicely.
533 */
534 void
535 puffs_msg_sendresp(struct puffs_mount *pmp, struct puffs_req *origpreq, int rv)
536 {
537 struct puffs_msgpark *park;
538 struct puffs_req *preq;
539
540 puffs_msgmem_alloc(sizeof(struct puffs_req), &park, (void *)&preq, 1);
541 puffs_msg_setfaf(park); /* XXXXXX: avoids reqid override */
542
543 memcpy(preq, origpreq, sizeof(struct puffs_req));
544 preq->preq_rv = rv;
545 preq->preq_opclass |= PUFFSOPFLAG_ISRESPONSE;
546
547 puffs_msg_enqueue(pmp, park);
548 puffs_msgmem_release(park);
549 }
550
551 /*
552 * Get next request in the outgoing queue. "maxsize" controls the
553 * size the caller can accommodate and "nonblock" signals if this
554 * should block while waiting for input. Handles all locking internally.
555 */
556 int
557 puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
558 uint8_t **data, size_t *dlen, void **parkptr)
559 {
560 struct puffs_mount *pmp = this;
561 struct puffs_msgpark *park;
562 struct puffs_req *preq;
563 int error;
564
565 error = 0;
566 mutex_enter(&pmp->pmp_lock);
567 puffs_mp_reference(pmp);
568 for (;;) {
569 /* RIP? */
570 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
571 error = ENXIO;
572 break;
573 }
574
575 /* need platinum yendorian express card? */
576 if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
577 DPRINTF(("puffs_getout: no outgoing op, "));
578 if (nonblock) {
579 DPRINTF(("returning EWOULDBLOCK\n"));
580 error = EWOULDBLOCK;
581 break;
582 }
583 DPRINTF(("waiting ...\n"));
584
585 error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
586 &pmp->pmp_lock);
587 if (error)
588 break;
589 else
590 continue;
591 }
592
593 park = TAILQ_FIRST(&pmp->pmp_msg_touser);
594 if (park == NULL)
595 continue;
596
597 mutex_enter(&park->park_mtx);
598 puffs_msgpark_reference(park);
599
600 DPRINTF(("puffs_getout: found park at %p, ", park));
601
602 /* If it's a goner, don't process any furher */
603 if (park->park_flags & PARKFLAG_WAITERGONE) {
604 DPRINTF(("waitergone!\n"));
605 puffs_msgpark_release(park);
606 continue;
607 }
608 preq = park->park_preq;
609
610 #if 0
611 /* check size */
612 /*
613 * XXX: this check is not valid for now, we don't know
614 * the size of the caller's input buffer. i.e. this
615 * will most likely go away
616 */
617 if (maxsize < preq->preq_frhdr.pfr_len) {
618 DPRINTF(("buffer too small\n"));
619 puffs_msgpark_release(park);
620 error = E2BIG;
621 break;
622 }
623 #endif
624
625 DPRINTF(("returning\n"));
626
627 /*
628 * Ok, we found what we came for. Release it from the
629 * outgoing queue but do not unlock. We will unlock
630 * only after we "releaseout" it to avoid complications:
631 * otherwise it is (theoretically) possible for userland
632 * to race us into "put" before we have a change to put
633 * this baby on the receiving queue.
634 */
635 TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
636 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
637 park->park_flags &= ~PARKFLAG_ONQUEUE1;
638 mutex_exit(&park->park_mtx);
639
640 pmp->pmp_msg_touser_count--;
641 KASSERT(pmp->pmp_msg_touser_count >= 0);
642
643 break;
644 }
645 puffs_mp_release(pmp);
646 mutex_exit(&pmp->pmp_lock);
647
648 if (error == 0) {
649 *data = (uint8_t *)preq;
650 preq->preq_pth.pth_framelen = park->park_copylen;
651 *dlen = preq->preq_pth.pth_framelen;
652 *parkptr = park;
653 }
654
655 return error;
656 }
657
658 /*
659 * Release outgoing structure. Now, depending on the success of the
660 * outgoing send, it is either going onto the result waiting queue
661 * or the death chamber.
662 */
663 void
664 puffs_msgif_releaseout(void *this, void *parkptr, int status)
665 {
666 struct puffs_mount *pmp = this;
667 struct puffs_msgpark *park = parkptr;
668
669 DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
670 park, status));
671 mutex_enter(&pmp->pmp_lock);
672 mutex_enter(&park->park_mtx);
673 if (park->park_flags & PARKFLAG_WANTREPLY) {
674 if (status == 0) {
675 DPRINTF(("enqueue replywait\n"));
676 TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
677 park_entries);
678 park->park_flags |= PARKFLAG_ONQUEUE2;
679 } else {
680 DPRINTF(("error path!\n"));
681 park->park_preq->preq_rv = status;
682 park->park_flags |= PARKFLAG_DONE;
683 cv_signal(&park->park_cv);
684 }
685 puffs_msgpark_release(park);
686 } else {
687 DPRINTF(("release\n"));
688 puffs_msgpark_release1(park, 2);
689 }
690 mutex_exit(&pmp->pmp_lock);
691 }
692
693 size_t
694 puffs_msgif_waitcount(void *this)
695 {
696 struct puffs_mount *pmp = this;
697 size_t rv;
698
699 mutex_enter(&pmp->pmp_lock);
700 rv = pmp->pmp_msg_touser_count;
701 mutex_exit(&pmp->pmp_lock);
702
703 return rv;
704 }
705
706 /*
707 * XXX: locking with this one?
708 */
709 static void
710 puffsop_msg(void *this, struct puffs_req *preq)
711 {
712 struct puffs_mount *pmp = this;
713 struct putter_hdr *pth = &preq->preq_pth;
714 struct puffs_msgpark *park;
715 int wgone;
716
717 mutex_enter(&pmp->pmp_lock);
718
719 /* Locate waiter */
720 TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
721 if (park->park_preq->preq_id == preq->preq_id)
722 break;
723 }
724 if (park == NULL) {
725 DPRINTF(("puffsop_msg: no request: %" PRIu64 "\n",
726 preq->preq_id));
727 mutex_exit(&pmp->pmp_lock);
728 return; /* XXX send error */
729 }
730
731 mutex_enter(&park->park_mtx);
732 puffs_msgpark_reference(park);
733 if (pth->pth_framelen > park->park_maxlen) {
734 DPRINTF(("puffsop_msg: invalid buffer length: "
735 "%" PRIu64 " (req %" PRIu64 ", \n", pth->pth_framelen,
736 preq->preq_id));
737 park->park_preq->preq_rv = EPROTO;
738 cv_signal(&park->park_cv);
739 puffs_msgpark_release1(park, 2);
740 mutex_exit(&pmp->pmp_lock);
741 return; /* XXX: error */
742 }
743 wgone = park->park_flags & PARKFLAG_WAITERGONE;
744
745 KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
746 TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
747 park->park_flags &= ~PARKFLAG_ONQUEUE2;
748 mutex_exit(&pmp->pmp_lock);
749
750 if (wgone) {
751 DPRINTF(("puffsop_msg: bad service - waiter gone for "
752 "park %p\n", park));
753 } else {
754 #if 1
755 if (park->park_creq) {
756 struct puffs_req *creq;
757 size_t csize;
758
759 KASSERT(pmp->pmp_docompat);
760 puffs_compat_incoming(preq, park->park_creq);
761 creq = park->park_creq;
762 csize = park->park_creqlen;
763 park->park_creq = park->park_preq;
764 park->park_creqlen = park->park_maxlen;
765
766 park->park_preq = creq;
767 park->park_maxlen = csize;
768
769 memcpy(park->park_creq, preq, pth->pth_framelen);
770 } else {
771 #endif
772 memcpy(park->park_preq, preq, pth->pth_framelen);
773 }
774
775 if (park->park_flags & PARKFLAG_CALL) {
776 DPRINTF(("puffsop_msg: call for %p, arg %p\n",
777 park->park_preq, park->park_donearg));
778 park->park_done(pmp, preq, park->park_donearg);
779 }
780 }
781
782 if (!wgone) {
783 DPRINTF(("puffs_putop: flagging done for "
784 "park %p\n", park));
785 cv_signal(&park->park_cv);
786 }
787
788 park->park_flags |= PARKFLAG_DONE;
789 puffs_msgpark_release1(park, 2);
790 }
791
792 static void
793 puffsop_flush(struct puffs_mount *pmp, struct puffs_flush *pf)
794 {
795 struct vnode *vp;
796 voff_t offlo, offhi;
797 int rv, flags = 0;
798
799 KASSERT(pf->pf_req.preq_pth.pth_framelen == sizeof(struct puffs_flush));
800
801 /* XXX: slurry */
802 if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
803 cache_purgevfs(PMPTOMP(pmp));
804 rv = 0;
805 goto out;
806 }
807
808 /*
809 * Get vnode, don't lock it. Namecache is protected by its own lock
810 * and we have a reference to protect against premature harvesting.
811 *
812 * The node we want here might be locked and the op is in
813 * userspace waiting for us to complete ==> deadlock. Another
814 * reason we need to eventually bump locking to userspace, as we
815 * will need to lock the node if we wish to do flushes.
816 */
817 rv = puffs_cookie2vnode(pmp, pf->pf_cookie, 0, 0, &vp);
818 if (rv) {
819 if (rv == PUFFS_NOSUCHCOOKIE)
820 rv = ENOENT;
821 goto out;
822 }
823
824 switch (pf->pf_op) {
825 #if 0
826 /* not quite ready, yet */
827 case PUFFS_INVAL_NAMECACHE_NODE:
828 struct componentname *pf_cn;
829 char *name;
830 /* get comfortab^Wcomponentname */
831 pf_cn = kmem_alloc(componentname);
832 memset(pf_cn, 0, sizeof(struct componentname));
833 break;
834
835 #endif
836 case PUFFS_INVAL_NAMECACHE_DIR:
837 if (vp->v_type != VDIR) {
838 rv = EINVAL;
839 break;
840 }
841 cache_purge1(vp, NULL, PURGE_CHILDREN);
842 break;
843
844 case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
845 flags = PGO_FREE;
846 /*FALLTHROUGH*/
847 case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
848 if (flags == 0)
849 flags = PGO_CLEANIT;
850
851 if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
852 rv = EINVAL;
853 break;
854 }
855
856 offlo = trunc_page(pf->pf_start);
857 offhi = round_page(pf->pf_end);
858 if (offhi != 0 && offlo >= offhi) {
859 rv = EINVAL;
860 break;
861 }
862
863 mutex_enter(&vp->v_uobj.vmobjlock);
864 rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
865 break;
866
867 default:
868 rv = EINVAL;
869 }
870
871 vrele(vp);
872
873 out:
874 puffs_msg_sendresp(pmp, &pf->pf_req, rv);
875 }
876
877 int
878 puffs_msgif_dispatch(void *this, struct putter_hdr *pth)
879 {
880 struct puffs_mount *pmp = this;
881 struct puffs_req *preq = (struct puffs_req *)pth;
882 struct puffs_sopreq *psopr;
883
884 if (pth->pth_framelen < sizeof(struct puffs_req)) {
885 puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
886 return 0;
887 }
888
889 switch (PUFFSOP_OPCLASS(preq->preq_opclass)) {
890 case PUFFSOP_VN:
891 case PUFFSOP_VFS:
892 DPRINTF(("dispatch: vn/vfs message 0x%x\n", preq->preq_optype));
893 puffsop_msg(pmp, preq);
894 break;
895
896 case PUFFSOP_FLUSH: /* process in sop thread */
897 {
898 struct puffs_flush *pf;
899
900 DPRINTF(("dispatch: flush 0x%x\n", preq->preq_optype));
901
902 if (preq->preq_pth.pth_framelen != sizeof(struct puffs_flush)) {
903 puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
904 break;
905 }
906 pf = (struct puffs_flush *)preq;
907
908 psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
909 memcpy(&psopr->psopr_pf, pf, sizeof(*pf));
910 psopr->psopr_sopreq = PUFFS_SOPREQ_FLUSH;
911
912 mutex_enter(&pmp->pmp_sopmtx);
913 if (pmp->pmp_sopthrcount == 0) {
914 mutex_exit(&pmp->pmp_sopmtx);
915 kmem_free(psopr, sizeof(*psopr));
916 puffs_msg_sendresp(pmp, preq, ENXIO);
917 } else {
918 TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs,
919 psopr, psopr_entries);
920 cv_signal(&pmp->pmp_sopcv);
921 mutex_exit(&pmp->pmp_sopmtx);
922 }
923 break;
924 }
925
926 case PUFFSOP_UNMOUNT: /* process in sop thread */
927 {
928
929 DPRINTF(("dispatch: unmount 0x%x\n", preq->preq_optype));
930
931 psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
932 psopr->psopr_preq = *preq;
933 psopr->psopr_sopreq = PUFFS_SOPREQ_UNMOUNT;
934
935 mutex_enter(&pmp->pmp_sopmtx);
936 if (pmp->pmp_sopthrcount == 0) {
937 mutex_exit(&pmp->pmp_sopmtx);
938 kmem_free(psopr, sizeof(*psopr));
939 puffs_msg_sendresp(pmp, preq, ENXIO);
940 } else {
941 TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs,
942 psopr, psopr_entries);
943 cv_signal(&pmp->pmp_sopcv);
944 mutex_exit(&pmp->pmp_sopmtx);
945 }
946 break;
947 }
948
949 default:
950 DPRINTF(("dispatch: invalid class 0x%x\n", preq->preq_opclass));
951 puffs_msg_sendresp(pmp, preq, EOPNOTSUPP);
952 break;
953 }
954
955 return 0;
956 }
957
958 /*
959 * Work loop for thread processing all ops from server which
960 * cannot safely be handled in caller context. This includes
961 * everything which might need a lock currently "held" by the file
962 * server, i.e. a long-term kernel lock which will be released only
963 * once the file server acknowledges a request
964 */
965 void
966 puffs_sop_thread(void *arg)
967 {
968 struct puffs_mount *pmp = arg;
969 struct mount *mp = PMPTOMP(pmp);
970 struct puffs_sopreq *psopr;
971 bool keeprunning;
972 bool unmountme = false;
973
974 mutex_enter(&pmp->pmp_sopmtx);
975 for (keeprunning = true; keeprunning; ) {
976 while ((psopr = TAILQ_FIRST(&pmp->pmp_sopreqs)) == NULL)
977 cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
978 TAILQ_REMOVE(&pmp->pmp_sopreqs, psopr, psopr_entries);
979 mutex_exit(&pmp->pmp_sopmtx);
980
981 switch (psopr->psopr_sopreq) {
982 case PUFFS_SOPREQSYS_EXIT:
983 keeprunning = false;
984 break;
985 case PUFFS_SOPREQ_FLUSH:
986 puffsop_flush(pmp, &psopr->psopr_pf);
987 break;
988 case PUFFS_SOPREQ_UNMOUNT:
989 puffs_msg_sendresp(pmp, &psopr->psopr_preq, 0);
990
991 unmountme = true;
992 keeprunning = false;
993
994 /*
995 * We know the mountpoint is still alive because
996 * the thread that is us (poetic?) is still alive.
997 */
998 atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
999 break;
1000 }
1001
1002 kmem_free(psopr, sizeof(*psopr));
1003 mutex_enter(&pmp->pmp_sopmtx);
1004 }
1005
1006 /*
1007 * Purge remaining ops.
1008 */
1009 while ((psopr = TAILQ_FIRST(&pmp->pmp_sopreqs)) != NULL) {
1010 TAILQ_REMOVE(&pmp->pmp_sopreqs, psopr, psopr_entries);
1011 mutex_exit(&pmp->pmp_sopmtx);
1012 puffs_msg_sendresp(pmp, &psopr->psopr_preq, ENXIO);
1013 kmem_free(psopr, sizeof(*psopr));
1014 mutex_enter(&pmp->pmp_sopmtx);
1015 }
1016
1017 pmp->pmp_sopthrcount--;
1018 cv_broadcast(&pmp->pmp_sopcv);
1019 mutex_exit(&pmp->pmp_sopmtx); /* not allowed to access fs after this */
1020
1021 /*
1022 * If unmount was requested, we can now safely do it here, since
1023 * our context is dead from the point-of-view of puffs_unmount()
1024 * and we are just another thread. dounmount() makes internally
1025 * sure that VFS_UNMOUNT() isn't called reentrantly and that it
1026 * is eventually completed.
1027 */
1028 if (unmountme) {
1029 (void)dounmount(mp, MNT_FORCE, curlwp);
1030 vfs_destroy(mp);
1031 }
1032
1033 kthread_exit(0);
1034 }
1035
1036 int
1037 puffs_msgif_close(void *this)
1038 {
1039 struct puffs_mount *pmp = this;
1040 struct mount *mp = PMPTOMP(pmp);
1041
1042 mutex_enter(&pmp->pmp_lock);
1043 puffs_mp_reference(pmp);
1044
1045 /*
1046 * Free the waiting callers before proceeding any further.
1047 * The syncer might be jogging around in this file system
1048 * currently. If we allow it to go to the userspace of no
1049 * return while trying to get the syncer lock, well ...
1050 */
1051 puffs_userdead(pmp);
1052
1053 /*
1054 * Make sure someone from puffs_unmount() isn't currently in
1055 * userspace. If we don't take this precautionary step,
1056 * they might notice that the mountpoint has disappeared
1057 * from under them once they return. Especially note that we
1058 * cannot simply test for an unmounter before calling
1059 * dounmount(), since it might be possible that that particular
1060 * invocation of unmount was called without MNT_FORCE. Here we
1061 * *must* make sure unmount succeeds. Also, restart is necessary
1062 * since pmp isn't locked. We might end up with PUTTER_DEAD after
1063 * restart and exit from there.
1064 */
1065 if (pmp->pmp_unmounting) {
1066 cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
1067 puffs_mp_release(pmp);
1068 mutex_exit(&pmp->pmp_lock);
1069 DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
1070 "restart\n", pmp));
1071 return ERESTART;
1072 }
1073
1074 /* Won't access pmp from here anymore */
1075 atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
1076 puffs_mp_release(pmp);
1077 mutex_exit(&pmp->pmp_lock);
1078
1079 /* Detach from VFS. */
1080 (void)dounmount(mp, MNT_FORCE, curlwp);
1081 vfs_destroy(mp);
1082
1083 return 0;
1084 }
1085
1086 /*
1087 * We're dead, kaput, RIP, slightly more than merely pining for the
1088 * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
1089 * our maker, ceased to be, etcetc. YASD. It's a dead FS!
1090 *
1091 * Caller must hold puffs mutex.
1092 */
1093 void
1094 puffs_userdead(struct puffs_mount *pmp)
1095 {
1096 struct puffs_msgpark *park, *park_next;
1097
1098 /*
1099 * Mark filesystem status as dying so that operations don't
1100 * attempt to march to userspace any longer.
1101 */
1102 pmp->pmp_status = PUFFSTAT_DYING;
1103
1104 /* signal waiters on REQUEST TO file server queue */
1105 for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
1106 uint8_t opclass;
1107
1108 mutex_enter(&park->park_mtx);
1109 puffs_msgpark_reference(park);
1110 park_next = TAILQ_NEXT(park, park_entries);
1111
1112 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
1113 TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
1114 park->park_flags &= ~PARKFLAG_ONQUEUE1;
1115 pmp->pmp_msg_touser_count--;
1116
1117 /*
1118 * Even though waiters on QUEUE1 are removed in touser()
1119 * in case of WAITERGONE, it is still possible for us to
1120 * get raced here due to having to retake locks in said
1121 * touser(). In the race case simply "ignore" the item
1122 * on the queue and move on to the next one.
1123 */
1124 if (park->park_flags & PARKFLAG_WAITERGONE) {
1125 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1126 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1127 puffs_msgpark_release(park);
1128
1129 } else {
1130 opclass = park->park_preq->preq_opclass;
1131 park->park_preq->preq_rv = ENXIO;
1132
1133 if (park->park_flags & PARKFLAG_CALL) {
1134 park->park_done(pmp, park->park_preq,
1135 park->park_donearg);
1136 puffs_msgpark_release1(park, 2);
1137 } else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
1138 puffs_msgpark_release1(park, 2);
1139 } else {
1140 park->park_preq->preq_rv = ENXIO;
1141 cv_signal(&park->park_cv);
1142 puffs_msgpark_release(park);
1143 }
1144 }
1145 }
1146
1147 /* signal waiters on RESPONSE FROM file server queue */
1148 for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
1149 mutex_enter(&park->park_mtx);
1150 puffs_msgpark_reference(park);
1151 park_next = TAILQ_NEXT(park, park_entries);
1152
1153 KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
1154 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1155
1156 TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
1157 park->park_flags &= ~PARKFLAG_ONQUEUE2;
1158
1159 if (park->park_flags & PARKFLAG_WAITERGONE) {
1160 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1161 puffs_msgpark_release(park);
1162 } else {
1163 park->park_preq->preq_rv = ENXIO;
1164 if (park->park_flags & PARKFLAG_CALL) {
1165 park->park_done(pmp, park->park_preq,
1166 park->park_donearg);
1167 puffs_msgpark_release1(park, 2);
1168 } else {
1169 cv_signal(&park->park_cv);
1170 puffs_msgpark_release(park);
1171 }
1172 }
1173 }
1174
1175 cv_broadcast(&pmp->pmp_msg_waiter_cv);
1176 }
1177