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
37 * XXX: How do we in general know that objects referenced in events
38 * have not been destroyed before we get around to handle the event ?
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/malloc.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/mutex.h>
51 #include <sys/errno.h>
53 #include <geom/geom.h>
54 #include <geom/geom_int.h>
56 #include <machine/stdarg.h>
58 TAILQ_HEAD(event_tailq_head, g_event);
60 static struct event_tailq_head g_events = TAILQ_HEAD_INITIALIZER(g_events);
61 static u_int g_pending_events;
62 static TAILQ_HEAD(,g_provider) g_doorstep = TAILQ_HEAD_INITIALIZER(g_doorstep);
63 static struct mtx g_eventlock;
64 static int g_wither_work;
66 #define G_N_EVENTREFS 20
69 TAILQ_ENTRY(g_event) events;
73 void *ref[G_N_EVENTREFS];
76 #define EV_DONE 0x80000
77 #define EV_WAKEUP 0x40000
78 #define EV_CANCELED 0x20000
79 #define EV_INPROGRESS 0x10000
85 g_topology_assert_not();
86 mtx_assert(&Giant, MA_NOTOWNED);
88 mtx_lock(&g_eventlock);
89 while (!TAILQ_EMPTY(&g_events))
90 msleep(&g_pending_events, &g_eventlock, PPAUSE,
92 mtx_unlock(&g_eventlock);
93 curthread->td_pflags &= ~TDP_GEOM;
102 mtx_lock(&g_eventlock);
103 while (!TAILQ_EMPTY(&g_events)) {
105 msleep(&g_pending_events, &g_eventlock, PPAUSE,
106 "g_waitidlel", hz/5);
109 mtx_unlock(&g_eventlock);
113 struct g_attrchanged_args {
114 struct g_provider *pp;
119 g_attr_changed_event(void *arg, int flag)
121 struct g_attrchanged_args *args;
122 struct g_provider *pp;
123 struct g_consumer *cp;
124 struct g_consumer *next_cp;
130 if (flag != EV_CANCEL && g_shutdown == 0) {
133 * Tell all consumers of the change.
135 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
136 if (cp->geom->attrchanged != NULL)
137 cp->geom->attrchanged(cp, args->attr);
144 g_attr_changed(struct g_provider *pp, const char *attr, int flag)
146 struct g_attrchanged_args *args;
149 args = g_malloc(sizeof *args, flag);
154 error = g_post_event(g_attr_changed_event, args, flag, pp, NULL);
161 g_orphan_provider(struct g_provider *pp, int error)
164 /* G_VALID_PROVIDER(pp) We likely lack topology lock */
165 g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)",
166 pp, pp->name, error);
168 ("g_orphan_provider(%p(%s), 0) error must be non-zero\n",
172 mtx_lock(&g_eventlock);
173 KASSERT(!(pp->flags & G_PF_ORPHAN),
174 ("g_orphan_provider(%p(%s)), already an orphan", pp, pp->name));
175 pp->flags |= G_PF_ORPHAN;
176 TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan);
177 mtx_unlock(&g_eventlock);
178 wakeup(&g_wait_event);
182 * This function is called once on each provider which the event handler
183 * finds on its g_doorstep.
187 g_orphan_register(struct g_provider *pp)
189 struct g_consumer *cp, *cp2;
193 G_VALID_PROVIDER(pp);
194 g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name);
198 wf = pp->flags & G_PF_WITHER;
199 pp->flags &= ~G_PF_WITHER;
202 * Tell all consumers the bad news.
203 * Don't be surprised if they self-destruct.
205 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
206 KASSERT(cp->geom->orphan != NULL,
207 ("geom %s has no orphan, class %s",
208 cp->geom->name, cp->geom->class->name));
210 * XXX: g_dev_orphan method does deferred destroying
211 * and it is possible, that other event could already
212 * call the orphan method. Check consumer's flags to
213 * do not schedule it twice.
215 if (cp->flags & G_CF_ORPHAN)
217 cp->flags |= G_CF_ORPHAN;
218 cp->geom->orphan(cp);
220 if (LIST_EMPTY(&pp->consumers) && wf)
221 g_destroy_provider(pp);
225 cp = LIST_FIRST(&pp->consumers);
228 if (pp->geom->flags & G_GEOM_WITHER)
229 g_destroy_provider(pp);
237 struct g_provider *pp;
240 mtx_lock(&g_eventlock);
241 TAILQ_FOREACH(pp, &g_doorstep, orphan) {
242 if (pp->nstart == pp->nend)
246 G_VALID_PROVIDER(pp);
247 TAILQ_REMOVE(&g_doorstep, pp, orphan);
248 mtx_unlock(&g_eventlock);
249 g_orphan_register(pp);
253 ep = TAILQ_FIRST(&g_events);
255 wakeup(&g_pending_events);
258 if (ep->flag & EV_INPROGRESS) {
259 mtx_unlock(&g_eventlock);
262 ep->flag |= EV_INPROGRESS;
263 mtx_unlock(&g_eventlock);
265 ep->func(ep->arg, 0);
267 mtx_lock(&g_eventlock);
268 TAILQ_REMOVE(&g_events, ep, events);
269 ep->flag &= ~EV_INPROGRESS;
270 if (ep->flag & EV_WAKEUP) {
272 mtx_unlock(&g_eventlock);
275 mtx_unlock(&g_eventlock);
289 mtx_assert(&g_eventlock, MA_OWNED);
292 mtx_unlock(&g_eventlock);
297 msleep(&g_wait_event, &g_eventlock, PRIBIO | PDROP,
298 "-", TAILQ_EMPTY(&g_doorstep) ? 0 : hz / 10);
305 g_cancel_event(void *ref)
307 struct g_event *ep, *epn;
308 struct g_provider *pp;
311 mtx_lock(&g_eventlock);
312 TAILQ_FOREACH(pp, &g_doorstep, orphan) {
315 TAILQ_REMOVE(&g_doorstep, pp, orphan);
318 TAILQ_FOREACH_SAFE(ep, &g_events, events, epn) {
319 if (ep->flag & EV_INPROGRESS)
321 for (n = 0; n < G_N_EVENTREFS; n++) {
322 if (ep->ref[n] == NULL)
324 if (ep->ref[n] != ref)
326 TAILQ_REMOVE(&g_events, ep, events);
327 ep->func(ep->arg, EV_CANCEL);
328 mtx_assert(&g_eventlock, MA_OWNED);
329 if (ep->flag & EV_WAKEUP) {
330 ep->flag |= (EV_DONE|EV_CANCELED);
338 if (TAILQ_EMPTY(&g_events))
339 wakeup(&g_pending_events);
340 mtx_unlock(&g_eventlock);
344 g_post_event_x(g_event_t *func, void *arg, int flag, int wuflag, struct g_event **epp, va_list ap)
350 g_trace(G_T_TOPOLOGY, "g_post_event_x(%p, %p, %d, %d)",
351 func, arg, flag, wuflag);
352 KASSERT(wuflag == 0 || wuflag == EV_WAKEUP,
353 ("Wrong wuflag in g_post_event_x(0x%x)", wuflag));
354 ep = g_malloc(sizeof *ep, flag | M_ZERO);
358 for (n = 0; n < G_N_EVENTREFS; n++) {
359 p = va_arg(ap, void *);
362 g_trace(G_T_TOPOLOGY, " ref %p", p);
365 KASSERT(p == NULL, ("Too many references to event"));
368 mtx_lock(&g_eventlock);
369 TAILQ_INSERT_TAIL(&g_events, ep, events);
370 mtx_unlock(&g_eventlock);
371 wakeup(&g_wait_event);
374 curthread->td_pflags |= TDP_GEOM;
379 g_post_event(g_event_t *func, void *arg, int flag, ...)
384 KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
385 ("Wrong flag to g_post_event"));
387 i = g_post_event_x(func, arg, flag, 0, NULL, ap);
396 mtx_lock(&g_eventlock);
398 mtx_unlock(&g_eventlock);
399 wakeup(&g_wait_event);
403 * XXX: It might actually be useful to call this function with topology held.
404 * XXX: This would ensure that the event gets created before anything else
405 * XXX: changes. At present all users have a handle on things in some other
406 * XXX: way, so this remains an XXX for now.
410 g_waitfor_event(g_event_t *func, void *arg, int flag, ...)
416 g_topology_assert_not();
417 KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
418 ("Wrong flag to g_post_event"));
420 error = g_post_event_x(func, arg, flag, EV_WAKEUP, &ep, ap);
425 mtx_lock(&g_eventlock);
426 while (!(ep->flag & EV_DONE))
427 msleep(ep, &g_eventlock, PRIBIO, "g_waitfor_event", hz);
428 if (ep->flag & EV_CANCELED)
430 mtx_unlock(&g_eventlock);
440 mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF);