puffs_msgif.c revision 1.53 1 /* $NetBSD: puffs_msgif.c,v 1.53 2007/11/10 21:45:06 pooka 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.53 2007/11/10 21:45:06 pooka Exp $");
34
35 #include <sys/param.h>
36 #include <sys/fstrans.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
46 #include <dev/puttervar.h>
47
48 #include <fs/puffs/puffs_msgif.h>
49 #include <fs/puffs/puffs_sys.h>
50
51 #include <miscfs/syncfs/syncfs.h> /* XXX: for syncer_mutex reference */
52
53 /*
54 * waitq data structures
55 */
56
57 /*
58 * While a request is going to userspace, park the caller within the
59 * kernel. This is the kernel counterpart of "struct puffs_req".
60 */
61 struct puffs_msgpark {
62 struct puffs_req *park_preq; /* req followed by buf */
63
64 size_t park_copylen; /* userspace copylength */
65 size_t park_maxlen; /* max size in comeback */
66
67 parkdone_fn park_done; /* "biodone" a'la puffs */
68 void *park_donearg;
69
70 int park_flags;
71 int park_refcount;
72
73 kcondvar_t park_cv;
74 kmutex_t park_mtx;
75
76 TAILQ_ENTRY(puffs_msgpark) park_entries;
77 };
78 #define PARKFLAG_WAITERGONE 0x01
79 #define PARKFLAG_DONE 0x02
80 #define PARKFLAG_ONQUEUE1 0x04
81 #define PARKFLAG_ONQUEUE2 0x08
82 #define PARKFLAG_CALL 0x10
83 #define PARKFLAG_WANTREPLY 0x20
84
85 static pool_cache_t parkpc;
86
87 static int
88 makepark(void *arg, void *obj, int flags)
89 {
90 struct puffs_msgpark *park = obj;
91
92 mutex_init(&park->park_mtx, MUTEX_DEFAULT, IPL_NONE);
93 cv_init(&park->park_cv, "puffsrpl");
94
95 return 0;
96 }
97
98 static void
99 nukepark(void *arg, void *obj)
100 {
101 struct puffs_msgpark *park = obj;
102
103 cv_destroy(&park->park_cv);
104 mutex_destroy(&park->park_mtx);
105 }
106
107 void
108 puffs_msgif_init()
109 {
110
111 parkpc = pool_cache_init(sizeof(struct puffs_msgpark), 0, 0, 0,
112 "puffprkl", NULL, IPL_NONE, makepark, nukepark, NULL);
113 }
114
115 void
116 puffs_msgif_destroy()
117 {
118
119 pool_cache_destroy(parkpc);
120 }
121
122 static int alloced;
123
124 static struct puffs_msgpark *
125 puffs_msgpark_alloc(int waitok)
126 {
127 struct puffs_msgpark *park;
128
129 park = pool_cache_get(parkpc, waitok ? PR_WAITOK : PR_NOWAIT);
130 if (park == NULL)
131 return park;
132
133 park->park_refcount = 1;
134 park->park_preq = NULL;
135 park->park_flags = PARKFLAG_WANTREPLY;
136
137 return park;
138 }
139
140 static void
141 puffs_msgpark_reference(struct puffs_msgpark *park)
142 {
143
144 KASSERT(mutex_owned(&park->park_mtx));
145 park->park_refcount++;
146 }
147
148 /*
149 * Release reference to park structure.
150 */
151 static void
152 puffs_msgpark_release1(struct puffs_msgpark *park, int howmany)
153 {
154 struct puffs_req *preq = park->park_preq;
155 int refcnt;
156
157 KASSERT(mutex_owned(&park->park_mtx));
158 refcnt = park->park_refcount -= howmany;
159 mutex_exit(&park->park_mtx);
160
161 KASSERT(refcnt >= 0);
162
163 if (refcnt == 0) {
164 alloced--;
165 if (preq)
166 kmem_free(preq, park->park_maxlen);
167 pool_cache_put(parkpc, park);
168 }
169 }
170 #define puffs_msgpark_release(a) puffs_msgpark_release1(a, 1)
171
172 #ifdef PUFFSDEBUG
173 static void
174 parkdump(struct puffs_msgpark *park)
175 {
176
177 DPRINTF(("park %p, preq %p, id %" PRIu64 "\n"
178 "\tcopy %zu, max %zu - done: %p/%p\n"
179 "\tflags 0x%08x, refcount %d, cv/mtx: %p/%p\n",
180 park, park->park_preq, park->park_preq->preq_id,
181 park->park_copylen, park->park_maxlen,
182 park->park_done, park->park_donearg,
183 park->park_flags, park->park_refcount,
184 &park->park_cv, &park->park_mtx));
185 }
186
187 static void
188 parkqdump(struct puffs_wq *q, int dumpall)
189 {
190 struct puffs_msgpark *park;
191 int total = 0;
192
193 TAILQ_FOREACH(park, q, park_entries) {
194 if (dumpall)
195 parkdump(park);
196 total++;
197 }
198 DPRINTF(("puffs waitqueue at %p dumped, %d total\n", q, total));
199
200 }
201 #endif /* PUFFSDEBUG */
202
203 /*
204 * A word about locking in the park structures: the lock protects the
205 * fields of the *park* structure (not preq) and acts as an interlock
206 * in cv operations. The lock is always internal to this module and
207 * callers do not need to worry about it.
208 */
209
210 int
211 puffs_msgmem_alloc(size_t len, struct puffs_msgpark **ppark, void **mem,
212 int cansleep)
213 {
214 struct puffs_msgpark *park;
215 void *m;
216
217 m = kmem_zalloc(len, cansleep ? KM_SLEEP : KM_NOSLEEP);
218 if (m == NULL) {
219 KASSERT(cansleep == 0);
220 return ENOMEM;
221 }
222
223 park = puffs_msgpark_alloc(cansleep);
224 if (park == NULL) {
225 KASSERT(cansleep == 0);
226 kmem_free(m, len);
227 return ENOMEM;
228 }
229
230 park->park_preq = m;
231 park->park_maxlen = len;
232
233 *ppark = park;
234 *mem = m;
235
236 return 0;
237 }
238
239 void
240 puffs_msgmem_release(struct puffs_msgpark *park)
241 {
242
243 if (park == NULL)
244 return;
245
246 mutex_enter(&park->park_mtx);
247 puffs_msgpark_release(park);
248 }
249
250 void
251 puffs_msg_setfaf(struct puffs_msgpark *park)
252 {
253
254 park->park_flags &= ~PARKFLAG_WANTREPLY;
255 }
256
257 /*
258 * kernel-user-kernel waitqueues
259 */
260
261 static int touser(struct puffs_mount *, struct puffs_msgpark *);
262
263 static uint64_t
264 puffs_getmsgid(struct puffs_mount *pmp)
265 {
266 uint64_t rv;
267
268 mutex_enter(&pmp->pmp_lock);
269 rv = pmp->pmp_nextmsgid++;
270 mutex_exit(&pmp->pmp_lock);
271
272 return rv;
273 }
274
275 /* vfs request */
276 int
277 puffs_msg_vfs(struct puffs_mount *pmp, struct puffs_msgpark *park, int optype)
278 {
279
280 park->park_preq->preq_opclass = PUFFSOP_VFS;
281 park->park_preq->preq_optype = optype;
282
283 park->park_copylen = park->park_maxlen;
284
285 return touser(pmp, park);
286 }
287
288 /*
289 * vnode level request
290 */
291 int
292 puffs_msg_vn(struct puffs_mount *pmp, struct puffs_msgpark *park,
293 int optype, size_t delta, struct vnode *vp_opc, struct vnode *vp_aux)
294 {
295 struct puffs_req *preq;
296 void *cookie = VPTOPNC(vp_opc);
297 struct puffs_node *pnode;
298 int rv;
299
300 park->park_preq->preq_opclass = PUFFSOP_VN;
301 park->park_preq->preq_optype = optype;
302 park->park_preq->preq_cookie = cookie;
303
304 KASSERT(delta < park->park_maxlen); /* "<=" wouldn't make sense */
305 park->park_copylen = park->park_maxlen - delta;
306
307 rv = touser(pmp, park);
308
309 /*
310 * Check if the user server requests that inactive be called
311 * when the time is right.
312 */
313 preq = park->park_preq;
314 if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N1) {
315 pnode = vp_opc->v_data;
316 pnode->pn_stat |= PNODE_DOINACT;
317 }
318 if (preq->preq_setbacks & PUFFS_SETBACK_INACT_N2) {
319 /* if no vp_aux, just ignore */
320 if (vp_aux) {
321 pnode = vp_aux->v_data;
322 pnode->pn_stat |= PNODE_DOINACT;
323 }
324 }
325 if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N1) {
326 pnode = vp_opc->v_data;
327 pnode->pn_stat |= PNODE_NOREFS;
328 }
329 if (preq->preq_setbacks & PUFFS_SETBACK_NOREF_N2) {
330 /* if no vp_aux, just ignore */
331 if (vp_aux) {
332 pnode = vp_aux->v_data;
333 pnode->pn_stat |= PNODE_NOREFS;
334 }
335 }
336
337 return rv;
338 }
339
340 void
341 puffs_msg_vncall(struct puffs_mount *pmp, struct puffs_msgpark *park,
342 int optype, size_t delta, parkdone_fn donefn, void *donearg,
343 struct vnode *vp_opc)
344 {
345 void *cookie = VPTOPNC(vp_opc);
346
347 park->park_preq->preq_opclass = PUFFSOP_VN;
348 park->park_preq->preq_optype = optype;
349 park->park_preq->preq_cookie = cookie;
350
351 KASSERT(delta < park->park_maxlen);
352 park->park_copylen = park->park_maxlen - delta;
353 park->park_done = donefn;
354 park->park_donearg = donearg;
355 park->park_flags |= PARKFLAG_CALL;
356
357 (void) touser(pmp, park);
358 }
359
360 int
361 puffs_msg_raw(struct puffs_mount *pmp, struct puffs_msgpark *park)
362 {
363
364 park->park_copylen = park->park_maxlen;
365
366 return touser(pmp, park);
367 }
368
369 void
370 puffs_msg_errnotify(struct puffs_mount *pmp, uint8_t type, int error,
371 const char *str, void *cookie)
372 {
373 struct puffs_msgpark *park;
374 struct puffs_error *perr;
375
376 puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void **)&perr,1);
377
378 perr->perr_error = error;
379 strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
380
381 park->park_preq->preq_opclass |= PUFFSOP_ERROR | PUFFSOPFLAG_FAF;
382 park->park_preq->preq_optype = type;
383 park->park_preq->preq_cookie = cookie;
384
385 park->park_copylen = park->park_maxlen;
386
387 (void)touser(pmp, park);
388 }
389
390 /*
391 * Wait for the userspace ping-pong game in calling process context,
392 * unless a FAF / async call, in which case just enqueues the request
393 * and return immediately.
394 */
395 static int
396 touser(struct puffs_mount *pmp, struct puffs_msgpark *park)
397 {
398 struct lwp *l = curlwp;
399 struct mount *mp;
400 struct puffs_req *preq;
401 int rv = 0;
402
403 mp = PMPTOMP(pmp);
404 preq = park->park_preq;
405 preq->preq_buflen = park->park_maxlen;
406 KASSERT(preq->preq_id == 0);
407
408 if ((park->park_flags & PARKFLAG_WANTREPLY) == 0)
409 preq->preq_opclass |= PUFFSOPFLAG_FAF;
410 else
411 preq->preq_id = puffs_getmsgid(pmp);
412
413 /* fill in caller information */
414 preq->preq_pid = l->l_proc->p_pid;
415 preq->preq_lid = l->l_lid;
416
417 /*
418 * To support cv_sig, yet another movie: check if there are signals
419 * pending and we are issueing a non-FAF. If so, return an error
420 * directly UNLESS we are issueing INACTIVE. In that case, convert
421 * it to a FAF, fire off to the file server and return an error.
422 * Yes, this is bordering disgusting. Barfbags are on me.
423 */
424 if ((park->park_flags & PARKFLAG_WANTREPLY)
425 && (park->park_flags & PARKFLAG_CALL) == 0
426 && (l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
427 if (PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN
428 && preq->preq_optype == PUFFS_VN_INACTIVE) {
429 park->park_preq->preq_opclass |= PUFFSOPFLAG_FAF;
430 park->park_flags &= ~PARKFLAG_WANTREPLY;
431 DPRINTF(("puffs touser: converted to FAF %p\n", park));
432 rv = EINTR;
433 } else {
434 return EINTR;
435 }
436 }
437
438 /*
439 * test for suspension lock.
440 *
441 * Note that we *DO NOT* keep the lock, since that might block
442 * lock acquiring PLUS it would give userlandia control over
443 * the lock. The operation queue enforces a strict ordering:
444 * when the fs server gets in the op stream, it knows things
445 * are in order. The kernel locks can't guarantee that for
446 * userspace, in any case.
447 *
448 * BUT: this presents a problem for ops which have a consistency
449 * clause based on more than one operation. Unfortunately such
450 * operations (read, write) do not reliably work yet.
451 *
452 * Ya, Ya, it's wrong wong wrong, me be fixink this someday.
453 *
454 * XXX: and there is one more problem. We sometimes need to
455 * take a lazy lock in case the fs is suspending and we are
456 * executing as the fs server context. This might happen
457 * e.g. in the case that the user server triggers a reclaim
458 * in the kernel while the fs is suspending. It's not a very
459 * likely event, but it needs to be fixed some day.
460 */
461
462 /*
463 * MOREXXX: once PUFFS_WCACHEINFO is enabled, we can't take
464 * the mutex here, since getpages() might be called locked.
465 */
466 fstrans_start(mp, FSTRANS_NORMAL);
467 mutex_enter(&pmp->pmp_lock);
468 fstrans_done(mp);
469
470 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
471 mutex_exit(&pmp->pmp_lock);
472 return ENXIO;
473 }
474
475 #ifdef PUFFSDEBUG
476 parkqdump(&pmp->pmp_msg_touser, puffsdebug > 1);
477 parkqdump(&pmp->pmp_msg_replywait, puffsdebug > 1);
478 #endif
479
480 mutex_enter(&park->park_mtx);
481 TAILQ_INSERT_TAIL(&pmp->pmp_msg_touser, park, park_entries);
482 park->park_flags |= PARKFLAG_ONQUEUE1;
483 puffs_mp_reference(pmp);
484 pmp->pmp_msg_touser_count++;
485 mutex_exit(&pmp->pmp_lock);
486
487 DPRINTF(("touser: req %" PRIu64 ", preq: %p, park: %p, "
488 "c/t: 0x%x/0x%x, f: 0x%x\n", preq->preq_id, preq, park,
489 preq->preq_opclass, preq->preq_optype, park->park_flags));
490
491 cv_broadcast(&pmp->pmp_msg_waiter_cv);
492 putter_notify(pmp->pmp_pi);
493
494 if ((park->park_flags & PARKFLAG_WANTREPLY)
495 && (park->park_flags & PARKFLAG_CALL) == 0) {
496 int error;
497
498 error = cv_wait_sig(&park->park_cv, &park->park_mtx);
499 DPRINTF(("puffs_touser: waiter for %p woke up with %d\n",
500 park, error));
501 if (error) {
502 park->park_flags |= PARKFLAG_WAITERGONE;
503 if (park->park_flags & PARKFLAG_DONE) {
504 rv = preq->preq_rv;
505 } else {
506 /*
507 * ok, we marked it as going away, but
508 * still need to do queue ops. take locks
509 * in correct order.
510 *
511 * We don't want to release our reference
512 * if it's on replywait queue to avoid error
513 * to file server. putop() code will DTRT.
514 */
515 mutex_exit(&park->park_mtx);
516 mutex_enter(&pmp->pmp_lock);
517 mutex_enter(&park->park_mtx);
518
519 /* remove from queue1 */
520 if (park->park_flags & PARKFLAG_ONQUEUE1) {
521 TAILQ_REMOVE(&pmp->pmp_msg_touser,
522 park, park_entries);
523 pmp->pmp_msg_touser_count--;
524 park->park_flags &= ~PARKFLAG_ONQUEUE1;
525 }
526
527 /*
528 * If it's waiting for a response already,
529 * boost reference count. Park will get
530 * nuked once the response arrives from
531 * the file server.
532 */
533 if (park->park_flags & PARKFLAG_ONQUEUE2)
534 puffs_msgpark_reference(park);
535
536 mutex_exit(&pmp->pmp_lock);
537
538 rv = error;
539 }
540 } else {
541 rv = preq->preq_rv;
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 } else {
559 /*
560 * Take extra reference for FAF, i.e. don't free us
561 * immediately upon return to the caller, but rather
562 * only when the message has been transported.
563 */
564 puffs_msgpark_reference(park);
565 }
566
567 mutex_exit(&park->park_mtx);
568
569 mutex_enter(&pmp->pmp_lock);
570 puffs_mp_release(pmp);
571 mutex_exit(&pmp->pmp_lock);
572
573 return rv;
574 }
575
576 /*
577 * Get next request in the outgoing queue. "maxsize" controls the
578 * size the caller can accommodate and "nonblock" signals if this
579 * should block while waiting for input. Handles all locking internally.
580 */
581 int
582 puffs_msgif_getout(void *this, size_t maxsize, int nonblock,
583 uint8_t **data, size_t *dlen, void **parkptr)
584 {
585 struct puffs_mount *pmp = this;
586 struct puffs_msgpark *park;
587 struct puffs_req *preq;
588 int error;
589
590 error = 0;
591 mutex_enter(&pmp->pmp_lock);
592 puffs_mp_reference(pmp);
593 for (;;) {
594 /* RIP? */
595 if (pmp->pmp_status != PUFFSTAT_RUNNING) {
596 error = ENXIO;
597 break;
598 }
599
600 /* need platinum yendorian express card? */
601 if (TAILQ_EMPTY(&pmp->pmp_msg_touser)) {
602 DPRINTF(("puffs_getout: no outgoing op, "));
603 if (nonblock) {
604 DPRINTF(("returning EWOULDBLOCK\n"));
605 error = EWOULDBLOCK;
606 break;
607 }
608 DPRINTF(("waiting ...\n"));
609
610 error = cv_wait_sig(&pmp->pmp_msg_waiter_cv,
611 &pmp->pmp_lock);
612 if (error)
613 break;
614 else
615 continue;
616 }
617
618 park = TAILQ_FIRST(&pmp->pmp_msg_touser);
619 if (park == NULL)
620 continue;
621
622 mutex_enter(&park->park_mtx);
623 puffs_msgpark_reference(park);
624
625 DPRINTF(("puffs_getout: found park at %p, ", park));
626
627 /* If it's a goner, don't process any furher */
628 if (park->park_flags & PARKFLAG_WAITERGONE) {
629 DPRINTF(("waitergone!\n"));
630 puffs_msgpark_release(park);
631 continue;
632 }
633
634 /* check size */
635 preq = park->park_preq;
636 if (maxsize < preq->preq_frhdr.pfr_len) {
637 DPRINTF(("buffer too small\n"));
638 puffs_msgpark_release(park);
639 error = E2BIG;
640 break;
641 }
642
643 DPRINTF(("returning\n"));
644
645 /*
646 * Ok, we found what we came for. Release it from the
647 * outgoing queue but do not unlock. We will unlock
648 * only after we "releaseout" it to avoid complications:
649 * otherwise it is (theoretically) possible for userland
650 * to race us into "put" before we have a change to put
651 * this baby on the receiving queue.
652 */
653 TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
654 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
655 park->park_flags &= ~PARKFLAG_ONQUEUE1;
656 mutex_exit(&park->park_mtx);
657
658 pmp->pmp_msg_touser_count--;
659 KASSERT(pmp->pmp_msg_touser_count >= 0);
660
661 break;
662 }
663 puffs_mp_release(pmp);
664 mutex_exit(&pmp->pmp_lock);
665
666 if (error == 0) {
667 *data = (uint8_t *)preq;
668 preq->preq_frhdr.pfr_len = park->park_copylen;
669 preq->preq_frhdr.pfr_alloclen = park->park_maxlen;
670 preq->preq_frhdr.pfr_type = preq->preq_opclass; /* yay! */
671 *dlen = preq->preq_frhdr.pfr_len;
672 *parkptr = park;
673 }
674
675 return error;
676 }
677
678 /*
679 * Release outgoing structure. Now, depending on the success of the
680 * outgoing send, it is either going onto the result waiting queue
681 * or the death chamber.
682 */
683 void
684 puffs_msgif_releaseout(void *this, void *parkptr, int status)
685 {
686 struct puffs_mount *pmp = this;
687 struct puffs_msgpark *park = parkptr;
688
689 DPRINTF(("puffs_releaseout: returning park %p, errno %d: " ,
690 park, status));
691 mutex_enter(&pmp->pmp_lock);
692 mutex_enter(&park->park_mtx);
693 if (park->park_flags & PARKFLAG_WANTREPLY) {
694 if (status == 0) {
695 DPRINTF(("enqueue replywait\n"));
696 TAILQ_INSERT_TAIL(&pmp->pmp_msg_replywait, park,
697 park_entries);
698 park->park_flags |= PARKFLAG_ONQUEUE2;
699 } else {
700 DPRINTF(("error path!\n"));
701 park->park_preq->preq_rv = status;
702 park->park_flags |= PARKFLAG_DONE;
703 cv_signal(&park->park_cv);
704 }
705 puffs_msgpark_release(park);
706 } else {
707 DPRINTF(("release\n"));
708 puffs_msgpark_release1(park, 2);
709 }
710 mutex_exit(&pmp->pmp_lock);
711 }
712
713 size_t
714 puffs_msgif_waitcount(void *this)
715 {
716 struct puffs_mount *pmp = this;
717 size_t rv;
718
719 mutex_enter(&pmp->pmp_lock);
720 rv = pmp->pmp_msg_touser_count;
721 mutex_exit(&pmp->pmp_lock);
722
723 return rv;
724 }
725
726 /*
727 * XXX: locking with this one?
728 */
729 static void
730 puffs_msgif_incoming(void *this, void *buf)
731 {
732 struct puffs_mount *pmp = this;
733 struct puffs_req *preq = buf;
734 struct puffs_frame *pfr = &preq->preq_frhdr;
735 struct puffs_msgpark *park;
736 int release, wgone;
737
738 /* XXX */
739 if (PUFFSOP_OPCLASS(preq->preq_opclass) != PUFFSOP_VN
740 && PUFFSOP_OPCLASS(preq->preq_opclass) != PUFFSOP_VFS)
741 return;
742
743 mutex_enter(&pmp->pmp_lock);
744
745 /* Locate waiter */
746 TAILQ_FOREACH(park, &pmp->pmp_msg_replywait, park_entries) {
747 if (park->park_preq->preq_id == preq->preq_id)
748 break;
749 }
750 if (park == NULL) {
751 DPRINTF(("puffs_msgif_income: no request: %" PRIu64 "\n",
752 preq->preq_id));
753 mutex_exit(&pmp->pmp_lock);
754 return; /* XXX send error */
755 }
756
757 mutex_enter(&park->park_mtx);
758 puffs_msgpark_reference(park);
759 if (pfr->pfr_len > park->park_maxlen) {
760 DPRINTF(("puffs_msgif_income: invalid buffer length: "
761 "%zu (req %" PRIu64 ", \n", pfr->pfr_len, preq->preq_id));
762 park->park_preq->preq_rv = EPROTO;
763 cv_signal(&park->park_cv);
764 puffs_msgpark_release(park);
765 mutex_exit(&pmp->pmp_lock);
766 return; /* XXX: error */
767 }
768 wgone = park->park_flags & PARKFLAG_WAITERGONE;
769
770 KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
771 TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
772 park->park_flags &= ~PARKFLAG_ONQUEUE2;
773 mutex_exit(&pmp->pmp_lock);
774
775 if (wgone) {
776 DPRINTF(("puffs_putop: bad service - waiter gone for "
777 "park %p\n", park));
778 release = 2;
779 } else {
780 if (park->park_flags & PARKFLAG_CALL) {
781 DPRINTF(("puffs_msgif_income: call for %p, arg %p\n",
782 park->park_preq, park->park_donearg));
783 park->park_done(pmp, buf, park->park_donearg);
784 release = 2;
785 } else {
786 /* XXX: yes, I know */
787 memcpy(park->park_preq, buf, pfr->pfr_len);
788 release = 1;
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, release);
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 int
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 /* XXX: slurry */
867 if (pf->pf_op == PUFFS_INVAL_NAMECACHE_ALL) {
868 cache_purgevfs(PMPTOMP(pmp));
869 return 0;
870 }
871
872 /*
873 * Get vnode, don't lock it. Namecache is protected by its own lock
874 * and we have a reference to protect against premature harvesting.
875 *
876 * The node we want here might be locked and the op is in
877 * userspace waiting for us to complete ==> deadlock. Another
878 * reason we need to eventually bump locking to userspace, as we
879 * will need to lock the node if we wish to do flushes.
880 */
881 rv = puffs_cookie2vnode(pmp, pf->pf_cookie, 0, 0, &vp);
882 if (rv) {
883 if (rv == PUFFS_NOSUCHCOOKIE)
884 return ENOENT;
885 return rv;
886 }
887
888 switch (pf->pf_op) {
889 #if 0
890 /* not quite ready, yet */
891 case PUFFS_INVAL_NAMECACHE_NODE:
892 struct componentname *pf_cn;
893 char *name;
894 /* get comfortab^Wcomponentname */
895 MALLOC(pf_cn, struct componentname *,
896 sizeof(struct componentname), M_PUFFS, M_WAITOK | M_ZERO);
897 memset(pf_cn, 0, sizeof(struct componentname));
898 break;
899
900 #endif
901 case PUFFS_INVAL_NAMECACHE_DIR:
902 if (vp->v_type != VDIR) {
903 rv = EINVAL;
904 break;
905 }
906 cache_purge1(vp, NULL, PURGE_CHILDREN);
907 break;
908
909 case PUFFS_INVAL_PAGECACHE_NODE_RANGE:
910 flags = PGO_FREE;
911 /*FALLTHROUGH*/
912 case PUFFS_FLUSH_PAGECACHE_NODE_RANGE:
913 if (flags == 0)
914 flags = PGO_CLEANIT;
915
916 if (pf->pf_end > vp->v_size || vp->v_type != VREG) {
917 rv = EINVAL;
918 break;
919 }
920
921 offlo = trunc_page(pf->pf_start);
922 offhi = round_page(pf->pf_end);
923 if (offhi != 0 && offlo >= offhi) {
924 rv = EINVAL;
925 break;
926 }
927
928 simple_lock(&vp->v_uobj.vmobjlock);
929 rv = VOP_PUTPAGES(vp, offlo, offhi, flags);
930 break;
931
932 default:
933 rv = EINVAL;
934 }
935
936 vrele(vp);
937
938 return rv;
939 }
940
941 int
942 puffs_msgif_dispatch(void *this, uint8_t *buf)
943 {
944 struct puffs_mount *pmp = this;
945 struct puffs_frame *pfr = (struct puffs_frame *)buf;
946
947 switch (PUFFSOP_OPCLASS(pfr->pfr_type)) {
948 case PUFFSOP_VN:
949 case PUFFSOP_VFS:
950 puffs_msgif_incoming(pmp, buf);
951 break;
952 case PUFFSOP_FLUSH:
953 puffsop_flush(pmp, (void *)buf);
954 break;
955 case PUFFSOP_SUSPEND:
956 puffsop_suspend(pmp);
957 break;
958 default:
959 /* XXX: send error */
960 break;
961 }
962
963 return 0;
964 }
965
966 int
967 puffs_msgif_close(void *this)
968 {
969 struct puffs_mount *pmp = this;
970 struct mount *mp = PMPTOMP(pmp);
971 int gone, rv;
972
973 mutex_enter(&pmp->pmp_lock);
974 puffs_mp_reference(pmp);
975
976 /*
977 * Free the waiting callers before proceeding any further.
978 * The syncer might be jogging around in this file system
979 * currently. If we allow it to go to the userspace of no
980 * return while trying to get the syncer lock, well ...
981 * synclk: I feel happy, I feel fine.
982 * lockmgr: You're not fooling anyone, you know.
983 */
984 puffs_userdead(pmp);
985
986 /*
987 * Make sure someone from puffs_unmount() isn't currently in
988 * userspace. If we don't take this precautionary step,
989 * they might notice that the mountpoint has disappeared
990 * from under them once they return. Especially note that we
991 * cannot simply test for an unmounter before calling
992 * dounmount(), since it might be possible that that particular
993 * invocation of unmount was called without MNT_FORCE. Here we
994 * *must* make sure unmount succeeds. Also, restart is necessary
995 * since pmp isn't locked. We might end up with PUTTER_DEAD after
996 * restart and exit from there.
997 */
998 if (pmp->pmp_unmounting) {
999 cv_wait(&pmp->pmp_unmounting_cv, &pmp->pmp_lock);
1000 puffs_mp_release(pmp);
1001 mutex_exit(&pmp->pmp_lock);
1002 DPRINTF(("puffs_fop_close: unmount was in progress for pmp %p, "
1003 "restart\n", pmp));
1004 return ERESTART;
1005 }
1006
1007 /* Won't access pmp from here anymore */
1008 puffs_mp_release(pmp);
1009 mutex_exit(&pmp->pmp_lock);
1010
1011 /*
1012 * Detach from VFS. First do necessary XXX-dance (from
1013 * sys_unmount() & other callers of dounmount()
1014 *
1015 * XXX Freeze syncer. Must do this before locking the
1016 * mount point. See dounmount() for details.
1017 *
1018 * XXX2: take a reference to the mountpoint before starting to
1019 * wait for syncer_mutex. Otherwise the mointpoint can be
1020 * wiped out while we wait.
1021 */
1022 simple_lock(&mp->mnt_slock);
1023 mp->mnt_wcnt++;
1024 simple_unlock(&mp->mnt_slock);
1025
1026 mutex_enter(&syncer_mutex);
1027
1028 simple_lock(&mp->mnt_slock);
1029 mp->mnt_wcnt--;
1030 if (mp->mnt_wcnt == 0)
1031 wakeup(&mp->mnt_wcnt);
1032 gone = mp->mnt_iflag & IMNT_GONE;
1033 simple_unlock(&mp->mnt_slock);
1034 if (gone) {
1035 mutex_exit(&syncer_mutex);
1036 return 0;
1037 }
1038
1039 /*
1040 * microscopic race condition here (although not with the current
1041 * kernel), but can't really fix it without starting a crusade
1042 * against vfs_busy(), so let it be, let it be, let it be
1043 */
1044
1045 /*
1046 * The only way vfs_busy() will fail for us is if the filesystem
1047 * is already a goner.
1048 * XXX: skating on the thin ice of modern calling conventions ...
1049 */
1050 if (vfs_busy(mp, 0, 0)) {
1051 mutex_exit(&syncer_mutex);
1052 return 0;
1053 }
1054
1055 /*
1056 * Once we have the mount point, unmount() can't interfere..
1057 * or at least in theory it shouldn't. dounmount() reentracy
1058 * might require some visiting at some point.
1059 */
1060 rv = dounmount(mp, MNT_FORCE, curlwp);
1061 KASSERT(rv == 0);
1062
1063 return 0;
1064 }
1065
1066 /*
1067 * We're dead, kaput, RIP, slightly more than merely pining for the
1068 * fjords, belly-up, fallen, lifeless, finished, expired, gone to meet
1069 * our maker, ceased to be, etcetc. YASD. It's a dead FS!
1070 *
1071 * Caller must hold puffs mutex.
1072 */
1073 void
1074 puffs_userdead(struct puffs_mount *pmp)
1075 {
1076 struct puffs_msgpark *park, *park_next;
1077
1078 /*
1079 * Mark filesystem status as dying so that operations don't
1080 * attempt to march to userspace any longer.
1081 */
1082 pmp->pmp_status = PUFFSTAT_DYING;
1083
1084 /* signal waiters on REQUEST TO file server queue */
1085 for (park = TAILQ_FIRST(&pmp->pmp_msg_touser); park; park = park_next) {
1086 uint8_t opclass;
1087
1088 mutex_enter(&park->park_mtx);
1089 puffs_msgpark_reference(park);
1090 park_next = TAILQ_NEXT(park, park_entries);
1091
1092 KASSERT(park->park_flags & PARKFLAG_ONQUEUE1);
1093 TAILQ_REMOVE(&pmp->pmp_msg_touser, park, park_entries);
1094 park->park_flags &= ~PARKFLAG_ONQUEUE1;
1095 pmp->pmp_msg_touser_count--;
1096
1097 /*
1098 * Even though waiters on QUEUE1 are removed in touser()
1099 * in case of WAITERGONE, it is still possible for us to
1100 * get raced here due to having to retake locks in said
1101 * touser(). In the race case simply "ignore" the item
1102 * on the queue and move on to the next one.
1103 */
1104 if (park->park_flags & PARKFLAG_WAITERGONE) {
1105 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1106 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1107 puffs_msgpark_release(park);
1108
1109 } else {
1110 opclass = park->park_preq->preq_opclass;
1111 park->park_preq->preq_rv = ENXIO;
1112
1113 if (park->park_flags & PARKFLAG_CALL) {
1114 park->park_done(pmp, park->park_preq,
1115 park->park_donearg);
1116 puffs_msgpark_release1(park, 2);
1117 } else if ((park->park_flags & PARKFLAG_WANTREPLY)==0) {
1118 puffs_msgpark_release1(park, 2);
1119 } else {
1120 park->park_preq->preq_rv = ENXIO;
1121 cv_signal(&park->park_cv);
1122 puffs_msgpark_release(park);
1123 }
1124 }
1125 }
1126
1127 /* signal waiters on RESPONSE FROM file server queue */
1128 for (park=TAILQ_FIRST(&pmp->pmp_msg_replywait); park; park=park_next) {
1129 mutex_enter(&park->park_mtx);
1130 puffs_msgpark_reference(park);
1131 park_next = TAILQ_NEXT(park, park_entries);
1132
1133 KASSERT(park->park_flags & PARKFLAG_ONQUEUE2);
1134 KASSERT(park->park_flags & PARKFLAG_WANTREPLY);
1135
1136 TAILQ_REMOVE(&pmp->pmp_msg_replywait, park, park_entries);
1137 park->park_flags &= ~PARKFLAG_ONQUEUE2;
1138
1139 if (park->park_flags & PARKFLAG_WAITERGONE) {
1140 KASSERT((park->park_flags & PARKFLAG_CALL) == 0);
1141 puffs_msgpark_release(park);
1142 } else {
1143 park->park_preq->preq_rv = ENXIO;
1144 if (park->park_flags & PARKFLAG_CALL) {
1145 park->park_done(pmp, park->park_preq,
1146 park->park_donearg);
1147 puffs_msgpark_release1(park, 2);
1148 } else {
1149 cv_signal(&park->park_cv);
1150 puffs_msgpark_release(park);
1151 }
1152 }
1153 }
1154
1155 cv_broadcast(&pmp->pmp_msg_waiter_cv);
1156 }
1157