puffs_msgif.c revision 1.72.4.5 1 /* $NetBSD: puffs_msgif.c,v 1.72.4.5 2011/11/02 20:11:12 riz 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.72.4.5 2011/11/02 20:11:12 riz 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 <uvm/uvm.h>
49
50 #include <dev/putter/putter_sys.h>
51
52 #include <fs/puffs/puffs_msgif.h>
53 #include <fs/puffs/puffs_sys.h>
54
55 #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
56
57 /*
58 * waitq data structures
59 */
60
61 /*
62 * While a request is going to userspace, park the caller within the
63 * kernel. This is the kernel counterpart of "struct puffs_req".
64 */
65 struct puffs_msgpark {
66 struct puffs_req *park_preq; /* req followed by buf */
67
68 size_t park_copylen; /* userspace copylength */
69 size_t park_maxlen; /* max size in comeback */
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()
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()
125 {
126
127 pool_cache_destroy(parkpc);
128 }
129
130 static int alloced;
131
132 static struct puffs_msgpark *
133 puffs_msgpark_alloc(int waitok)
134 {
135 struct puffs_msgpark *park;
136
137 KASSERT(curlwp != uvm.pagedaemon_lwp || !waitok);
138 park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
139 if (park == NULL)
140 return park;
141
142 park->park_refcount = 1;
143 park->park_preq = NULL;
144 park->park_flags = PARKFLAG_WANTREPLY;
145
146 #ifdef PUFFSDEBUG
147 totalpark++;
148 #endif
149
150 return park;
151 }
152
153 static void
154 puffs_msgpark_reference(struct puffs_msgpark *park)
155 {
156
157 KASSERT(mutex_owned(&park->park_mtx));
158 park->park_refcount++;
159 }
160
161 /*
162 * Release reference to park structure.
163 */
164 static void
165 puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
166 {
167 struct puffs_req *preq = park->park_preq;
168 int refcnt;
169
170 KASSERT(mutex_owned(&park->park_mtx));
171 refcnt = park->park_refcount -= howmany;
172 mutex_exit(&park->park_mtx);
173
174 KASSERT(refcnt >= 0);
175
176 if (refcnt == 0) {
177 alloced--;
178 if (preq)
179 kmem_free(preq, park->park_maxlen);
180 pool_cache_put(parkpc, park);
181
182 #ifdef PUFFSDEBUG
183 totalpark--;
184 #endif
185 }
186 }
187 #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
188
189 #ifdef PUFFSDEBUG
190 static void
191 parkdump(struct puffs_msgpark *park)
192 {
193
194 DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
195 "\tcopy %zu, max %zu - done: %p/%p\n"
196 "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
197 park, park->park_preq, park->park_preq->preq_id,
198 park->park_copylen, park->park_maxlen,
199 park->park_done, park->park_donearg,
200 park->park_flags, park->park_refcount,
201 &park->park_cv, &park->park_mtx));
202 }
203
204 static void
205 parkqdump(struct puffs_wq *q, int dumpall)
206 {
207 struct puffs_msgpark *park;
208 int total = 0;
209
210 TAILQ_FOREACH(park, q, park_entries) {
211 if (dumpall)
212 parkdump(park);
213 total++;
214 }
215 DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
216
217 }
218 #endif /* PUFFSDEBUG */
219
220 /*
221 * A word about locking in the park structures: the lock protects the
222 * fields of the *park* structure (not preq) and acts as an interlock
223 * in cv operations. The lock is always internal to this module and
224 * callers do not need to worry about it.
225 */
226
227 int
228 puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
229 int cansleep)
230 {
231 struct puffs_msgpark *park;
232 void *m;
233
234 KASSERT(curlwp != uvm.pagedaemon_lwp || !cansleep);
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;
333
334 mp = PMPTOMP(pmp);
335 preq = park->park_preq;
336 preq->preq_buflen = park->park_maxlen;
337 KASSERT(preq->preq_id == 0
338 || (preq->preq_opclass & PUFFSOPFLAG_ISRESPONSE));
339
340 if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
341 preq->preq_opclass |= PUFFSOPFLAG_FAF;
342 else
343 preq->preq_id = puffs_getmsgid(pmp);
344
345 /* fill in caller information */
346 preq->preq_pid = l->l_proc->p_pid;
347 preq->preq_lid = l->l_lid;
348
349 /*
350 * To support cv_sig, yet another movie: check if there are signals
351 * pending and we are issueing a non-FAF. If so, return an error
352 * directly UNLESS we are issueing INACTIVE/RECLAIM. In that case,
353 * convert it to a FAF, fire off to the file server and return
354 * an error. Yes, this is bordering disgusting. Barfbags are on me.
355 */
356 if (__predict_false((park->park_flags & PARKFLAG_WANTREPLY)
357 && (park->park_flags & PARKFLAG_CALL) == 0
358 && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0))) {
359 sigset_t ss;
360
361 /*
362 * see the comment about signals in puffs_msg_wait.
363 */
364 sigpending1(l, &ss);
365 if (sigismember(&ss, SIGINT) ||
366 sigismember(&ss, SIGTERM) ||
367 sigismember(&ss, SIGKILL) ||
368 sigismember(&ss, SIGHUP) ||
369 sigismember(&ss, SIGQUIT)) {
370 park->park_flags |= PARKFLAG_HASERROR;
371 preq->preq_rv = EINTR;
372 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
373 && (preq->preq_optype == PUFFS_VN_INACTIVE
374 || preq->preq_optype == PUFFS_VN_RECLAIM)) {
375 park->park_preq->preq_opclass |=
376 PUFFSOPFLAG_FAF;
377 park->park_flags &= ~PARKFLAG_WANTREPLY;
378 DPRINTF(("puffs_msg_enqueue: "
379 "converted to FAF %p\n", park));
380 } else {
381 return;
382 }
383 }
384 }
385
386 /*
387 * test for suspension lock.
388 *
389 * Note that we *DO NOT* keep the lock, since that might block
390 * lock acquiring PLUS it would give userlandia control over
391 * the lock. The operation queue enforces a strict ordering:
392 * when the fs server gets in the op stream, it knows things
393 * are in order. The kernel locks can't guarantee that for
394 * userspace, in any case.
395 *
396 * BUT: this presents a problem for ops which have a consistency
397 * clause based on more than one operation. Unfortunately such
398 * operations (read, write) do not reliably work yet.
399 *
400 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
401 *
402 * XXX: and there is one more problem. We sometimes need to
403 * take a lazy lock in case the fs is suspending and we are
404 * executing as the fs server context. This might happen
405 * e.g. in the case that the user server triggers a reclaim
406 * in the kernel while the fs is suspending. It's not a very
407 * likely event, but it needs to be fixed some day.
408 */
409
410 /*
411 * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
412 * the mutex here, since getpages() might be called locked.
413 */
414 fstrans_start(mp, FSTRANS_NORMAL);
415 mutex_enter(&pmp->pmp_lock);
416 fstrans_done(mp);
417
418 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
419 mutex_exit(&pmp->pmp_lock);
420 park->park_flags |= PARKFLAG_HASERROR;
421 preq->preq_rv = ENXIO;
422 return;
423 }
424
425 #ifdef PUFFSDEBUG
426 parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
427 parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
428 #endif
429
430 /*
431 * Note: we don't need to lock park since we have the only
432 * reference to it at this point.
433 */
434 TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
435 park->park_flags |= PARKFLAG_ONQUEUE1;
436 pmp->pmp_msg_touser_count++;
437 park->park_refcount++;
438 mutex_exit(&pmp->pmp_lock);
439
440 cv_broadcast(&pmp->pmp_msg_waiter_cv);
441 putter_notify(pmp->pmp_pi);
442
443 DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
444 "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
445 preq->preq_opclass, preq->preq_optype, park->park_flags));
446 }
447
448 int
449 puffs_msg_wait(struct puffs_mount *pmp, struct puffs_msgpark *park)
450 {
451 lwp_t *l = curlwp;
452 proc_t *p = l->l_proc;
453 struct puffs_req *preq = park->park_preq; /* XXX: hmmm */
454 sigset_t ss;
455 sigset_t oss;
456 struct mount *mp = PMPTOMP(pmp);
457 int error = 0;
458 int rv;
459
460 /*
461 * block unimportant signals.
462 *
463 * The set of "important" signals here was chosen to be same as
464 * nfs interruptible mount.
465 */
466 sigfillset(&ss);
467 sigdelset(&ss, SIGINT);
468 sigdelset(&ss, SIGTERM);
469 sigdelset(&ss, SIGKILL);
470 sigdelset(&ss, SIGHUP);
471 sigdelset(&ss, SIGQUIT);
472 mutex_enter(p->p_lock);
473 sigprocmask1(l, SIG_BLOCK, &ss, &oss);
474 mutex_exit(p->p_lock);
475
476 mutex_enter(&pmp->pmp_lock);
477 puffs_mp_reference(pmp);
478 mutex_exit(&pmp->pmp_lock);
479
480 mutex_enter(&park->park_mtx);
481 if ((park->park_flags & PARKFLAG_WANTREPLY) == 0
482 || (park->park_flags & PARKFLAG_CALL)) {
483 mutex_exit(&park->park_mtx);
484 rv = 0;
485 goto skipwait;
486 }
487
488 /* did the response beat us to the wait? */
489 if (__predict_false((park->park_flags & PARKFLAG_DONE)
490 || (park->park_flags & PARKFLAG_HASERROR))) {
491 rv = park->park_preq->preq_rv;
492 mutex_exit(&park->park_mtx);
493 goto skipwait;
494 }
495
496 error = cv_wait_sig(&park->park_cv, &park->park_mtx);
497 DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
498 park, error));
499 if (error) {
500 park->park_flags |= PARKFLAG_WAITERGONE;
501 if (park->park_flags & PARKFLAG_DONE) {
502 rv = preq->preq_rv;
503 mutex_exit(&park->park_mtx);
504 } else {
505 /*
506 * ok, we marked it as going away, but
507 * still need to do queue ops. take locks
508 * in correct order.
509 *
510 * We don't want to release our reference
511 * if it's on replywait queue to avoid error
512 * to file server. putop() code will DTRT.
513 */
514 mutex_exit(&park->park_mtx);
515 mutex_enter(&pmp->pmp_lock);
516 mutex_enter(&park->park_mtx);
517
518 /*
519 * Still on queue1? We can safely remove it
520 * without any consequences since the file
521 * server hasn't seen it. "else" we need to
522 * wait for the response and just ignore it
523 * to avoid signalling an incorrect error to
524 * the file server.
525 */
526 if (park->park_flags & PARKFLAG_ONQUEUE1) {
527 TAILQ_REMOVE(&pmp->pmp_msg_touser,
528 park, park_entries);
529 puffs_msgpark_release(park);
530 pmp->pmp_msg_touser_count--;
531 park->park_flags &= ~PARKFLAG_ONQUEUE1;
532 } else {
533 mutex_exit(&park->park_mtx);
534 }
535 mutex_exit(&pmp->pmp_lock);
536
537 rv = EINTR;
538 }
539 } else {
540 rv = preq->preq_rv;
541 mutex_exit(&park->park_mtx);
542 }
543
544 /*
545 * retake the lock and release. This makes sure (haha,
546 * I'm humorous) that we don't process the same vnode in
547 * multiple threads due to the locks hacks we have in
548 * puffs_lock(). In reality this is well protected by
549 * the biglock, but once that's gone, well, hopefully
550 * this will be fixed for real. (and when you read this
551 * comment in 2017 and subsequently barf, my condolences ;).
552 */
553 if (rv == 0 && !fstrans_is_owner(mp)) {
554 fstrans_start(mp, FSTRANS_NORMAL);
555 fstrans_done(mp);
556 }
557
558 skipwait:
559 mutex_enter(&pmp->pmp_lock);
560 puffs_mp_release(pmp);
561 mutex_exit(&pmp->pmp_lock);
562
563 mutex_enter(p->p_lock);
564 sigprocmask1(l, SIG_SETMASK, &oss, NULL);
565 mutex_exit(p->p_lock);
566
567 return rv;
568 }
569
570 /*
571 * XXX: this suuuucks. Hopefully I'll get rid of this lossage once
572 * the whole setback-nonsense gets fixed.
573 */
574 int
575 puffs_msg_wait2(struct puffs_mount *pmp, struct puffs_msgpark *park,
576 struct puffs_node *pn1, struct puffs_node *pn2)
577 {
578 struct puffs_req *preq;
579 int rv;
580
581 rv = puffs_msg_wait(pmp, park);
582
583 preq = park->park_preq;
584 if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N1)
585 pn1->pn_stat |= PNODE_DOINACT;
586 if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_INACT_N2)
587 pn2->pn_stat |= PNODE_DOINACT;
588
589 if (pn1 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1)
590 pn1->pn_stat |= PNODE_NOREFS;
591 if (pn2 && preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2)
592 pn2->pn_stat |= PNODE_NOREFS;
593
594 return rv;
595
596 }
597
598 /*
599 * XXX: lazy bum. please, for the love of foie gras, fix me.
600 * This should *NOT* depend on setfaf. Also "memcpy" could
601 * be done more nicely.
602 */
603 void
604 puffs_msg_sendresp(struct puffs_mount *pmp, struct puffs_req *origpreq, int rv)
605 {
606 struct puffs_msgpark *park;
607 struct puffs_req *preq;
608
609 puffs_msgmem_alloc(sizeof(struct puffs_req), &park, (void *)&preq, 1);
610 puffs_msg_setfaf(park); /* XXXXXX: avoids reqid override */
611
612 memcpy(preq, origpreq, sizeof(struct puffs_req));
613 preq->preq_rv = rv;
614 preq->preq_opclass |= PUFFSOPFLAG_ISRESPONSE;
615
616 puffs_msg_enqueue(pmp, park);
617 puffs_msgmem_release(park);
618 }
619
620 /*
621 * Get next request in the outgoing queue. "maxsize" controls the
622 * size the caller can accommodate and "nonblock" signals if this
623 * should block while waiting for input. Handles all locking internally.
624 */
625 int
626 puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
627 uint8_t **data, size_t *dlen, void **parkptr)
628 {
629 struct puffs_mount *pmp = this;
630 struct puffs_msgpark *park;
631 struct puffs_req *preq;
632 int error;
633
634 error = 0;
635 mutex_enter(&pmp->pmp_lock);
636 puffs_mp_reference(pmp);
637 for (;;) {
638 /* RIP? */
639 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
640 error = ENXIO;
641 break;
642 }
643
644 /* need platinum yendorian express card? */
645 if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
646 DPRINTF(("puffs_getout: no outgoing op, "));
647 if (nonblock) {
648 DPRINTF(("returning EWOULDBLOCK\n"));
649 error = EWOULDBLOCK;
650 break;
651 }
652 DPRINTF(("waiting ...\n"));
653
654 error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
655 &pmp->pmp_lock);
656 if (error)
657 break;
658 else
659 continue;
660 }
661
662 park = TAILQ_FIRST(&pmp->pmp_msg_touser);
663 if (park == NULL)
664 continue;
665
666 mutex_enter(&park->park_mtx);
667 puffs_msgpark_reference(park);
668
669 DPRINTF(("puffs_getout: found park at %p, ", park));
670
671 /* If it's a goner, don't process any furher */
672 if (park->park_flags & PARKFLAG_WAITERGONE) {
673 DPRINTF(("waitergone!\n"));
674 puffs_msgpark_release(park);
675 continue;
676 }
677 preq = park->park_preq;
678
679 #if 0
680 /* check size */
681 /*
682 * XXX: this check is not valid for now, we don't know
683 * the size of the caller's input buffer. i.e. this
684 * will most likely go away
685 */
686 if (maxsize < preq->preq_frhdr.pfr_len) {
687 DPRINTF(("buffer too small\n"));
688 puffs_msgpark_release(park);
689 error = E2BIG;
690 break;
691 }
692 #endif
693
694 DPRINTF(("returning\n"));
695
696 /*
697 * Ok, we found what we came for. Release it from the
698 * outgoing queue but do not unlock. We will unlock
699 * only after we "releaseout" it to avoid complications:
700 * otherwise it is (theoretically) possible for userland
701 * to race us into "put" before we have a change to put
702 * this baby on the receiving queue.
703 */
704 TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
705 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
706 park->park_flags &= ~PARKFLAG_ONQUEUE1;
707 mutex_exit(&park->park_mtx);
708
709 pmp->pmp_msg_touser_count--;
710 KASSERT(pmp->pmp_msg_touser_count >= 0);
711
712 break;
713 }
714 puffs_mp_release(pmp);
715 mutex_exit(&pmp->pmp_lock);
716
717 if (error == 0) {
718 *data = (uint8_t *)preq;
719 preq->preq_pth.pth_framelen = park->park_copylen;
720 *dlen = preq->preq_pth.pth_framelen;
721 *parkptr = park;
722 }
723
724 return error;
725 }
726
727 /*
728 * Release outgoing structure. Now, depending on the success of the
729 * outgoing send, it is either going onto the result waiting queue
730 * or the death chamber.
731 */
732 void
733 puffs_msgif_releaseout(void *this, void *parkptr, int status)
734 {
735 struct puffs_mount *pmp = this;
736 struct puffs_msgpark *park = parkptr;
737
738 DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
739 park, status));
740 mutex_enter(&pmp->pmp_lock);
741 mutex_enter(&park->park_mtx);
742 if (park->park_flags & PARKFLAG_WANTREPLY) {
743 if (status == 0) {
744 DPRINTF(("enqueue replywait\n"));
745 TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
746 park_entries);
747 park->park_flags |= PARKFLAG_ONQUEUE2;
748 } else {
749 DPRINTF(("error path!\n"));
750 park->park_preq->preq_rv = status;
751 park->park_flags |= PARKFLAG_DONE;
752 cv_signal(&park->park_cv);
753 }
754 puffs_msgpark_release(park);
755 } else {
756 DPRINTF(("release\n"));
757 puffs_msgpark_release1(park, 2);
758 }
759 mutex_exit(&pmp->pmp_lock);
760 }
761
762 size_t
763 puffs_msgif_waitcount(void *this)
764 {
765 struct puffs_mount *pmp = this;
766 size_t rv;
767
768 mutex_enter(&pmp->pmp_lock);
769 rv = pmp->pmp_msg_touser_count;
770 mutex_exit(&pmp->pmp_lock);
771
772 return rv;
773 }
774
775 /*
776 * XXX: locking with this one?
777 */
778 static void
779 puffsop_msg(void *this, struct puffs_req *preq)
780 {
781 struct puffs_mount *pmp = this;
782 struct putter_hdr *pth = &preq->preq_pth;
783 struct puffs_msgpark *park;
784 int wgone;
785
786 mutex_enter(&pmp->pmp_lock);
787
788 /* Locate waiter */
789 TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
790 if (park->park_preq->preq_id == preq->preq_id)
791 break;
792 }
793 if (park == NULL) {
794 DPRINTF(("puffsop_msg: no request: %" PRIu64 "\n",
795 preq->preq_id));
796 mutex_exit(&pmp->pmp_lock);
797 return; /* XXX send error */
798 }
799
800 mutex_enter(&park->park_mtx);
801 puffs_msgpark_reference(park);
802 if (pth->pth_framelen > park->park_maxlen) {
803 DPRINTF(("puffsop_msg: invalid buffer length: "
804 "%" PRIu64 " (req %" PRIu64 ", \n", pth->pth_framelen,
805 preq->preq_id));
806 park->park_preq->preq_rv = EPROTO;
807 cv_signal(&park->park_cv);
808 puffs_msgpark_release1(park, 2);
809 mutex_exit(&pmp->pmp_lock);
810 return; /* XXX: error */
811 }
812 wgone = park->park_flags & PARKFLAG_WAITERGONE;
813
814 KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
815 TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
816 park->park_flags &= ~PARKFLAG_ONQUEUE2;
817 mutex_exit(&pmp->pmp_lock);
818
819 if (wgone) {
820 DPRINTF(("puffsop_msg: bad service - waiter gone for "
821 "park %p\n", park));
822 } else {
823 if (park->park_flags & PARKFLAG_CALL) {
824 DPRINTF(("puffsop_msg: call for %p, arg %p\n",
825 park->park_preq, park->park_donearg));
826 park->park_done(pmp, preq, park->park_donearg);
827 } else {
828 /* XXX: yes, I know */
829 memcpy(park->park_preq, preq, pth->pth_framelen);
830 }
831 }
832
833 if (!wgone) {
834 DPRINTF(("puffs_putop: flagging done for "
835 "park %p\n", park));
836 cv_signal(&park->park_cv);
837 }
838
839 park->park_flags |= PARKFLAG_DONE;
840 puffs_msgpark_release1(park, 2);
841 }
842
843 /*
844 * helpers
845 */
846 static void
847 dosuspendresume(void *arg)
848 {
849 struct puffs_mount *pmp = arg;
850 struct mount *mp;
851 int rv;
852
853 mp = PMPTOMP(pmp);
854 /*
855 * XXX? does this really do any good or is it just
856 * paranoid stupidity? or stupid paranoia?
857 */
858 if (mp->mnt_iflag & IMNT_UNMOUNT) {
859 printf("puffs dosuspendresume(): detected suspend on "
860 "unmounting fs\n");
861 goto out;
862 }
863
864 /* Do the dance. Allow only one concurrent suspend */
865 rv = vfs_suspend(PMPTOMP(pmp), 1);
866 if (rv == 0)
867 vfs_resume(PMPTOMP(pmp));
868
869 out:
870 mutex_enter(&pmp->pmp_lock);
871 KASSERT(pmp->pmp_suspend == 1);
872 pmp->pmp_suspend = 0;
873 puffs_mp_release(pmp);
874 mutex_exit(&pmp->pmp_lock);
875
876 kthread_exit(0);
877 }
878
879 static void
880 puffsop_suspend(struct puffs_mount *pmp)
881 {
882 int rv = 0;
883
884 mutex_enter(&pmp->pmp_lock);
885 if (pmp->pmp_suspend || pmp->pmp_status != PUFFSTAT_RUNNING) {
886 rv = EBUSY;
887 } else {
888 puffs_mp_reference(pmp);
889 pmp->pmp_suspend = 1;
890 }
891 mutex_exit(&pmp->pmp_lock);
892 if (rv)
893 return;
894 rv = kthread_create(PRI_NONE, 0, NULL, dosuspendresume,
895 pmp, NULL, "puffsusp");
896
897 /* XXX: "return" rv */
898 }
899
900 static void
901 puffsop_flush(struct puffs_mount *pmp, struct puffs_flush *pf)
902 {
903 struct vnode *vp;
904 voff_t offlo, offhi;
905 int rv, flags = 0;
906
907 if (pf->pf_req.preq_pth.pth_framelen != sizeof(struct puffs_flush)) {
908 puffs_msg_sendresp(pmp, &pf->pf_req, EINVAL); /* E2SMALL */
909 return;
910 }
911
912 /* XXX: slurry */
913 if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
914 cache_purgevfs(PMPTOMP(pmp));
915 rv = 0;
916 goto out;
917 }
918
919 /*
920 * Get vnode, don't lock it. Namecache is protected by its own lock
921 * and we have a reference to protect against premature harvesting.
922 *
923 * The node we want here might be locked and the op is in
924 * userspace waiting for us to complete ==> deadlock. Another
925 * reason we need to eventually bump locking to userspace, as we
926 * will need to lock the node if we wish to do flushes.
927 */
928 rv = puffs_cookie2vnode(pmp, pf->pf_cookie, 0, 0, &vp);
929 if (rv) {
930 if (rv == PUFFS_NOSUCHCOOKIE)
931 rv = ENOENT;
932 goto out;
933 }
934
935 switch (pf->pf_op) {
936 #if 0
937 /* not quite ready, yet */
938 case PUFFS_INVAL_NAMECACHE_NODE:
939 struct componentname *pf_cn;
940 char *name;
941 /* get comfortab^Wcomponentname */
942 KASSERT(curlwp != uvm.pagedaemon_lwp);
943 pf_cn = kmem_alloc(componentname);
944 memset(pf_cn, 0, sizeof(struct componentname));
945 break;
946
947 #endif
948 case PUFFS_INVAL_NAMECACHE_DIR:
949 if (vp->v_type != VDIR) {
950 rv = EINVAL;
951 break;
952 }
953 cache_purge1(vp, NULL, PURGE_CHILDREN);
954 break;
955
956 case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
957 flags = PGO_FREE;
958 /*FALLTHROUGH*/
959 case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
960 if (flags == 0)
961 flags = PGO_CLEANIT;
962
963 if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
964 rv = EINVAL;
965 break;
966 }
967
968 offlo = trunc_page(pf->pf_start);
969 offhi = round_page(pf->pf_end);
970 if (offhi != 0 && offlo >= offhi) {
971 rv = EINVAL;
972 break;
973 }
974
975 mutex_enter(&vp->v_uobj.vmobjlock);
976 rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
977 break;
978
979 default:
980 rv = EINVAL;
981 }
982
983 vrele(vp);
984
985 out:
986 puffs_msg_sendresp(pmp, &pf->pf_req, rv);
987 }
988
989 int
990 puffs_msgif_dispatch(void *this, struct putter_hdr *pth)
991 {
992 struct puffs_mount *pmp = this;
993 struct puffs_req *preq = (struct puffs_req *)pth;
994 struct puffs_sopreq *psopr;
995
996 if (pth->pth_framelen < sizeof(struct puffs_req)) {
997 puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
998 return 0;
999 }
1000
1001 switch (PUFFSOP_OPCLASS(preq->preq_opclass)) {
1002 case PUFFSOP_VN:
1003 case PUFFSOP_VFS:
1004 DPRINTF(("dispatch: vn/vfs message 0x%x\n", preq->preq_optype));
1005 puffsop_msg(pmp, preq);
1006 break;
1007 case PUFFSOP_FLUSH: /* process in sop thread */
1008 {
1009 struct puffs_flush *pf;
1010
1011 DPRINTF(("dispatch: flush 0x%x\n", preq->preq_optype));
1012
1013 if (preq->preq_pth.pth_framelen != sizeof(struct puffs_flush)) {
1014 puffs_msg_sendresp(pmp, preq, EINVAL); /* E2SMALL */
1015 break;
1016 }
1017 pf = (struct puffs_flush *)preq;
1018
1019 KASSERT(curlwp != uvm.pagedaemon_lwp);
1020 psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
1021 memcpy(&psopr->psopr_pf, pf, sizeof(*pf));
1022 psopr->psopr_sopreq = PUFFS_SOPREQ_FLUSH;
1023
1024 mutex_enter(&pmp->pmp_sopmtx);
1025 TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs, psopr, psopr_entries);
1026 cv_signal(&pmp->pmp_sopcv);
1027 mutex_exit(&pmp->pmp_sopmtx);
1028 break;
1029 }
1030 case PUFFSOP_SUSPEND:
1031 DPRINTF(("dispatch: suspend\n"));
1032 puffsop_suspend(pmp);
1033 break;
1034 default:
1035 DPRINTF(("dispatch: invalid class 0x%x\n", preq->preq_opclass));
1036 puffs_msg_sendresp(pmp, preq, EINVAL);
1037 break;
1038 }
1039
1040 return 0;
1041 }
1042
1043 /*
1044 * Work loop for thread processing all ops from server which
1045 * cannot safely be handled in caller context. This includes
1046 * everything which might need a lock currently "held" by the file
1047 * server, i.e. a long-term kernel lock which will be released only
1048 * once the file server acknowledges a request
1049 */
1050 void
1051 puffs_sop_thread(void *arg)
1052 {
1053 struct puffs_mount *pmp = arg;
1054 struct puffs_sopreq *psopr;
1055 struct puffs_req *preq;
1056 bool keeprunning = true;
1057
1058 mutex_enter(&pmp->pmp_sopmtx);
1059 while (keeprunning) {
1060 while ((psopr = TAILQ_FIRST(&pmp->pmp_sopreqs)) == NULL)
1061 cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
1062 TAILQ_REMOVE(&pmp->pmp_sopreqs, psopr, psopr_entries);
1063 mutex_exit(&pmp->pmp_sopmtx);
1064
1065 preq = &psopr->psopr_preq;
1066 switch (psopr->psopr_sopreq) {
1067 case PUFFS_SOPREQ_EXIT:
1068 keeprunning = false;
1069 break;
1070 case PUFFS_SOPREQ_FLUSH:
1071 puffsop_flush(pmp, (struct puffs_flush *)preq);
1072 break;
1073 }
1074
1075 kmem_free(psopr, sizeof(*psopr));
1076 mutex_enter(&pmp->pmp_sopmtx);
1077 }
1078
1079 /*
1080 * Purge remaining ops. could send error, but that is highly
1081 * unlikely to reach the caller.
1082 */
1083 while ((psopr = TAILQ_FIRST(&pmp->pmp_sopreqs)) != NULL) {
1084 TAILQ_REMOVE(&pmp->pmp_sopreqs, psopr, psopr_entries);
1085 mutex_exit(&pmp->pmp_sopmtx);
1086 kmem_free(psopr, sizeof(*psopr));
1087 mutex_enter(&pmp->pmp_sopmtx);
1088 }
1089
1090 pmp->pmp_sopthrcount--;
1091 cv_signal(&pmp->pmp_sopcv);
1092 mutex_exit(&pmp->pmp_sopmtx); /* not allowed to access fs after this */
1093
1094 kthread_exit(0);
1095 }
1096
1097 int
1098 puffs_msgif_close(void *this)
1099 {
1100 struct puffs_mount *pmp = this;
1101 struct mount *mp = PMPTOMP(pmp);
1102
1103 mutex_enter(&pmp->pmp_lock);
1104 puffs_mp_reference(pmp);
1105
1106 /*
1107 * Free the waiting callers before proceeding any further.
1108 * The syncer might be jogging around in this file system
1109 * currently. If we allow it to go to the userspace of no
1110 * return while trying to get the syncer lock, well ...
1111 */
1112 puffs_userdead(pmp);
1113
1114 /*
1115 * Make sure someone from puffs_unmount() isn't currently in
1116 * userspace. If we don't take this precautionary step,
1117 * they might notice that the mountpoint has disappeared
1118 * from under them once they return. Especially note that we
1119 * cannot simply test for an unmounter before calling
1120 * dounmount(), since it might be possible that that particular
1121 * invocation of unmount was called without MNT_FORCE. Here we
1122 * *must* make sure unmount succeeds. Also, restart is necessary
1123 * since pmp isn't locked. We might end up with PUTTER_DEAD after
1124 * restart and exit from there.
1125 */
1126 if (pmp->pmp_unmounting) {
1127 cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
1128 puffs_mp_release(pmp);
1129 mutex_exit(&pmp->pmp_lock);
1130 DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
1131 "restart\n", pmp));
1132 return ERESTART;
1133 }
1134
1135 /* Won't access pmp from here anymore */
1136 atomic_inc_uint((unsigned int*)&mp->mnt_refcnt);
1137 puffs_mp_release(pmp);
1138 mutex_exit(&pmp->pmp_lock);
1139
1140 /* Detach from VFS. */
1141 (void)dounmount(mp, MNT_FORCE, curlwp);
1142 vfs_destroy(mp);
1143
1144 return 0;
1145 }
1146
1147 /*
1148 * We're dead, kaput, RIP, slightly more than merely pining for the
1149 * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
1150 * our maker, ceased to be, etcetc. YASD. It's a dead FS!
1151 *
1152 * Caller must hold puffs mutex.
1153 */
1154 void
1155 puffs_userdead(struct puffs_mount *pmp)
1156 {
1157 struct puffs_msgpark *park, *park_next;
1158
1159 /*
1160 * Mark filesystem status as dying so that operations don't
1161 * attempt to march to userspace any longer.
1162 */
1163 pmp->pmp_status = PUFFSTAT_DYING;
1164
1165 /* signal waiters on REQUEST TO file server queue */
1166 for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
1167 uint8_t opclass;
1168
1169 mutex_enter(&park->park_mtx);
1170 puffs_msgpark_reference(park);
1171 park_next = TAILQ_NEXT(park, park_entries);
1172
1173 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
1174 TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
1175 park->park_flags &= ~PARKFLAG_ONQUEUE1;
1176 pmp->pmp_msg_touser_count--;
1177
1178 /*
1179 * Even though waiters on QUEUE1 are removed in touser()
1180 * in case of WAITERGONE, it is still possible for us to
1181 * get raced here due to having to retake locks in said
1182 * touser(). In the race case simply "ignore" the item
1183 * on the queue and move on to the next one.
1184 */
1185 if (park->park_flags & PARKFLAG_WAITERGONE) {
1186 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1187 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1188 puffs_msgpark_release(park);
1189
1190 } else {
1191 opclass = park->park_preq->preq_opclass;
1192 park->park_preq->preq_rv = ENXIO;
1193
1194 if (park->park_flags & PARKFLAG_CALL) {
1195 park->park_done(pmp, park->park_preq,
1196 park->park_donearg);
1197 puffs_msgpark_release1(park, 2);
1198 } else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
1199 puffs_msgpark_release1(park, 2);
1200 } else {
1201 park->park_preq->preq_rv = ENXIO;
1202 cv_signal(&park->park_cv);
1203 puffs_msgpark_release(park);
1204 }
1205 }
1206 }
1207
1208 /* signal waiters on RESPONSE FROM file server queue */
1209 for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
1210 mutex_enter(&park->park_mtx);
1211 puffs_msgpark_reference(park);
1212 park_next = TAILQ_NEXT(park, park_entries);
1213
1214 KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
1215 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1216
1217 TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
1218 park->park_flags &= ~PARKFLAG_ONQUEUE2;
1219
1220 if (park->park_flags & PARKFLAG_WAITERGONE) {
1221 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1222 puffs_msgpark_release(park);
1223 } else {
1224 park->park_preq->preq_rv = ENXIO;
1225 if (park->park_flags & PARKFLAG_CALL) {
1226 park->park_done(pmp, park->park_preq,
1227 park->park_donearg);
1228 puffs_msgpark_release1(park, 2);
1229 } else {
1230 cv_signal(&park->park_cv);
1231 puffs_msgpark_release(park);
1232 }
1233 }
1234 }
1235
1236 cv_broadcast(&pmp->pmp_msg_waiter_cv);
1237 }
1238