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