2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * XXX: How do we in general know that objects referenced in events
40 * have not been destroyed before we get around to handle the event ?
43 #include <sys/param.h>
52 #include <sys/malloc.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/mutex.h>
58 #include <sys/errno.h>
60 #include <geom/geom.h>
62 static struct event_tailq_head g_events = TAILQ_HEAD_INITIALIZER(g_events);
63 static u_int g_pending_events, g_silence_events;
64 static void g_do_event(struct g_event *ep, struct thread *tp);
65 static TAILQ_HEAD(,g_provider) g_doorstep = TAILQ_HEAD_INITIALIZER(g_doorstep);
66 static struct mtx g_doorlock;
81 wakeup(&g_silence_events);
82 while (g_pending_events)
83 tsleep(&g_pending_events, PPAUSE, "g_rattle", hz/5);
88 g_orphan_provider(struct g_provider *pp, int error)
91 g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)",
94 ("g_orphan_provider(%p(%s), 0) error must be non-zero\n",
97 mtx_lock(&g_doorlock);
98 TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan);
99 mtx_unlock(&g_doorlock);
101 wakeup(&g_wait_event);
106 * This function is called once on each provider which the event handler
107 * finds on its g_doorstep.
111 g_orphan_register(struct g_provider *pp, struct thread *tp)
113 struct g_consumer *cp, *cp2;
115 g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name);
119 * Tell all consumers the bad news.
120 * Don't get surprised if they self-destruct.
122 cp = LIST_FIRST(&pp->consumers);
124 cp2 = LIST_NEXT(cp, consumers);
125 KASSERT(cp->geom->method->orphan != NULL,
126 ("method %s has no orphan, geom %s",
127 cp->geom->method->name, cp->geom->name));
128 cp->geom->method->orphan(cp, tp);
134 g_destroy_event(struct g_event *ep)
141 g_do_event(struct g_event *ep, struct thread *tp)
143 struct g_method *mp, *mp2;
145 struct g_consumer *cp, *cp2;
146 struct g_provider *pp;
149 g_trace(G_T_TOPOLOGY, "g_do_event(%p) %d m:%p g:%p p:%p c:%p - ",
150 ep, ep->event, ep->method, ep->geom, ep->provider, ep->consumer);
155 if (mp2->taste == NULL)
157 LIST_FOREACH(mp, &g_methods, method) {
160 LIST_FOREACH(gp, &mp->geom, geom) {
161 LIST_FOREACH(pp, &gp->provider, provider) {
162 mp2->taste(ep->method, pp, tp, 0);
168 case EV_NEW_PROVIDER:
169 g_trace(G_T_TOPOLOGY, "EV_NEW_PROVIDER(%s)",
171 LIST_FOREACH(mp, &g_methods, method) {
172 if (mp->taste == NULL)
175 LIST_FOREACH(cp, &ep->provider->consumers, consumers)
176 if(cp->geom->method == mp)
179 mp->taste(mp, ep->provider, tp, 0);
185 g_trace(G_T_TOPOLOGY, "EV_SPOILED(%p(%s),%p)",
186 ep->provider, ep->provider->name, ep->consumer);
187 cp = LIST_FIRST(&ep->provider->consumers);
189 cp2 = LIST_NEXT(cp, consumers);
191 g_trace(G_T_TOPOLOGY, "spoiling %p (%s) (%p)",
192 cp, cp->geom->name, cp->geom->spoiled);
193 if (cp->geom->spoiled != NULL)
194 cp->geom->spoiled(cp);
202 KASSERT(1 == 0, ("Unknown event %d", ep->event));
207 one_event(struct thread *tp)
210 struct g_provider *pp;
214 mtx_lock(&g_doorlock);
215 pp = TAILQ_FIRST(&g_doorstep);
217 TAILQ_REMOVE(&g_doorstep, pp, orphan);
218 mtx_unlock(&g_doorlock);
221 g_orphan_register(pp, tp);
223 ep = TAILQ_FIRST(&g_events);
228 TAILQ_REMOVE(&g_events, ep, events);
229 if (ep->method != NULL)
230 ep->method->event = NULL;
231 if (ep->geom != NULL)
232 ep->geom->event = NULL;
233 if (ep->provider != NULL)
234 ep->provider->event = NULL;
235 if (ep->consumer != NULL)
236 ep->consumer->event = NULL;
239 if (g_pending_events == 0) {
241 wakeup(&g_pending_events);
250 g_run_events(struct thread *tp)
253 while (one_event(tp))
258 g_post_event(enum g_events ev, struct g_method *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
262 g_trace(G_T_TOPOLOGY, "g_post_event(%d, %p, %p, %p, %p)",
265 ep = g_malloc(sizeof *ep, M_WAITOK | M_ZERO);
269 KASSERT(mp->event == NULL, ("Double event on method"));
274 KASSERT(gp->event == NULL, ("Double event on geom"));
279 KASSERT(pp->event == NULL, ("Double event on provider"));
284 KASSERT(cp->event == NULL, ("Double event on consumer"));
288 TAILQ_INSERT_TAIL(&g_events, ep, events);
290 wakeup(&g_wait_event);
298 mtx_init(&g_doorlock, "GEOM orphanage", MTX_DEF);