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