2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * 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/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/malloc.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
51 #include <sys/mutex.h>
53 #include <sys/errno.h>
55 #include <geom/geom.h>
56 #include <geom/geom_int.h>
58 #include <machine/stdarg.h>
60 TAILQ_HEAD(event_tailq_head, g_event);
62 static struct event_tailq_head g_events = TAILQ_HEAD_INITIALIZER(g_events);
63 static u_int g_pending_events;
64 static TAILQ_HEAD(,g_provider) g_doorstep = TAILQ_HEAD_INITIALIZER(g_doorstep);
65 static struct mtx g_eventlock;
66 static int g_wither_work;
68 #define G_N_EVENTREFS 20
71 TAILQ_ENTRY(g_event) events;
75 void *ref[G_N_EVENTREFS];
78 #define EV_DONE 0x80000
79 #define EV_WAKEUP 0x40000
80 #define EV_CANCELED 0x20000
81 #define EV_INPROGRESS 0x10000
87 g_topology_assert_not();
89 mtx_lock(&g_eventlock);
90 TSWAIT("GEOM events");
91 while (!TAILQ_EMPTY(&g_events))
92 msleep(&g_pending_events, &g_eventlock, PPAUSE,
94 TSUNWAIT("GEOM events");
95 mtx_unlock(&g_eventlock);
96 curthread->td_pflags &= ~TDP_GEOM;
105 mtx_lock(&g_eventlock);
106 while (!TAILQ_EMPTY(&g_events)) {
108 msleep(&g_pending_events, &g_eventlock, PPAUSE,
109 "g_waitidlel", hz/5);
112 mtx_unlock(&g_eventlock);
116 struct g_attrchanged_args {
117 struct g_provider *pp;
122 g_attr_changed_event(void *arg, int flag)
124 struct g_attrchanged_args *args;
125 struct g_provider *pp;
126 struct g_consumer *cp;
127 struct g_consumer *next_cp;
133 if (flag != EV_CANCEL && g_shutdown == 0) {
136 * Tell all consumers of the change.
138 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
139 if (cp->geom->attrchanged != NULL)
140 cp->geom->attrchanged(cp, args->attr);
147 g_attr_changed(struct g_provider *pp, const char *attr, int flag)
149 struct g_attrchanged_args *args;
152 args = g_malloc(sizeof *args, flag);
157 error = g_post_event(g_attr_changed_event, args, flag, pp, NULL);
164 g_orphan_provider(struct g_provider *pp, int error)
167 /* G_VALID_PROVIDER(pp) We likely lack topology lock */
168 g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)",
169 pp, pp->name, error);
171 ("g_orphan_provider(%p(%s), 0) error must be non-zero\n",
175 mtx_lock(&g_eventlock);
176 KASSERT(!(pp->flags & G_PF_ORPHAN),
177 ("g_orphan_provider(%p(%s)), already an orphan", pp, pp->name));
178 pp->flags |= G_PF_ORPHAN;
179 TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan);
180 mtx_unlock(&g_eventlock);
181 wakeup(&g_wait_event);
185 * This function is called once on each provider which the event handler
186 * finds on its g_doorstep.
190 g_orphan_register(struct g_provider *pp)
192 struct g_consumer *cp, *cp2;
196 G_VALID_PROVIDER(pp);
197 g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name);
201 wf = pp->flags & G_PF_WITHER;
202 pp->flags &= ~G_PF_WITHER;
205 * Tell all consumers the bad news.
206 * Don't be surprised if they self-destruct.
208 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
209 KASSERT(cp->geom->orphan != NULL,
210 ("geom %s has no orphan, class %s",
211 cp->geom->name, cp->geom->class->name));
213 * XXX: g_dev_orphan method does deferred destroying
214 * and it is possible, that other event could already
215 * call the orphan method. Check consumer's flags to
216 * do not schedule it twice.
218 if (cp->flags & G_CF_ORPHAN)
220 cp->flags |= G_CF_ORPHAN;
221 cp->geom->orphan(cp);
223 if (LIST_EMPTY(&pp->consumers) && wf)
224 g_destroy_provider(pp);
228 cp = LIST_FIRST(&pp->consumers);
231 if (pp->geom->flags & G_GEOM_WITHER)
232 g_destroy_provider(pp);
240 struct g_provider *pp;
243 mtx_lock(&g_eventlock);
244 TAILQ_FOREACH(pp, &g_doorstep, orphan) {
245 if (pp->nstart == pp->nend)
249 G_VALID_PROVIDER(pp);
250 TAILQ_REMOVE(&g_doorstep, pp, orphan);
251 mtx_unlock(&g_eventlock);
252 g_orphan_register(pp);
256 ep = TAILQ_FIRST(&g_events);
258 wakeup(&g_pending_events);
261 if (ep->flag & EV_INPROGRESS) {
262 mtx_unlock(&g_eventlock);
265 ep->flag |= EV_INPROGRESS;
266 mtx_unlock(&g_eventlock);
268 ep->func(ep->arg, 0);
270 mtx_lock(&g_eventlock);
271 TSRELEASE("GEOM events");
272 TAILQ_REMOVE(&g_events, ep, events);
273 ep->flag &= ~EV_INPROGRESS;
274 if (ep->flag & EV_WAKEUP) {
276 mtx_unlock(&g_eventlock);
279 mtx_unlock(&g_eventlock);
293 mtx_assert(&g_eventlock, MA_OWNED);
296 mtx_unlock(&g_eventlock);
301 msleep(&g_wait_event, &g_eventlock, PRIBIO | PDROP,
302 "-", TAILQ_EMPTY(&g_doorstep) ? 0 : hz / 10);
309 g_cancel_event(void *ref)
311 struct g_event *ep, *epn;
312 struct g_provider *pp;
315 mtx_lock(&g_eventlock);
316 TAILQ_FOREACH(pp, &g_doorstep, orphan) {
319 TAILQ_REMOVE(&g_doorstep, pp, orphan);
322 TAILQ_FOREACH_SAFE(ep, &g_events, events, epn) {
323 if (ep->flag & EV_INPROGRESS)
325 for (n = 0; n < G_N_EVENTREFS; n++) {
326 if (ep->ref[n] == NULL)
328 if (ep->ref[n] != ref)
330 TSRELEASE("GEOM events");
331 TAILQ_REMOVE(&g_events, ep, events);
332 ep->func(ep->arg, EV_CANCEL);
333 mtx_assert(&g_eventlock, MA_OWNED);
334 if (ep->flag & EV_WAKEUP) {
335 ep->flag |= (EV_DONE|EV_CANCELED);
343 if (TAILQ_EMPTY(&g_events))
344 wakeup(&g_pending_events);
345 mtx_unlock(&g_eventlock);
349 g_post_event_x(g_event_t *func, void *arg, int flag, int wuflag, struct g_event **epp, va_list ap)
355 g_trace(G_T_TOPOLOGY, "g_post_event_x(%p, %p, %d, %d)",
356 func, arg, flag, wuflag);
357 KASSERT(wuflag == 0 || wuflag == EV_WAKEUP,
358 ("Wrong wuflag in g_post_event_x(0x%x)", wuflag));
359 ep = g_malloc(sizeof *ep, flag | M_ZERO);
363 for (n = 0; n < G_N_EVENTREFS; n++) {
364 p = va_arg(ap, void *);
367 g_trace(G_T_TOPOLOGY, " ref %p", p);
370 KASSERT(p == NULL, ("Too many references to event"));
373 mtx_lock(&g_eventlock);
374 TSHOLD("GEOM events");
375 TAILQ_INSERT_TAIL(&g_events, ep, events);
376 mtx_unlock(&g_eventlock);
377 wakeup(&g_wait_event);
380 curthread->td_pflags |= TDP_GEOM;
385 g_post_event(g_event_t *func, void *arg, int flag, ...)
390 KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
391 ("Wrong flag to g_post_event"));
393 i = g_post_event_x(func, arg, flag, 0, NULL, ap);
402 mtx_lock(&g_eventlock);
404 mtx_unlock(&g_eventlock);
405 wakeup(&g_wait_event);
409 * XXX: It might actually be useful to call this function with topology held.
410 * XXX: This would ensure that the event gets created before anything else
411 * XXX: changes. At present all users have a handle on things in some other
412 * XXX: way, so this remains an XXX for now.
416 g_waitfor_event(g_event_t *func, void *arg, int flag, ...)
422 g_topology_assert_not();
423 KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
424 ("Wrong flag to g_post_event"));
426 error = g_post_event_x(func, arg, flag, EV_WAKEUP, &ep, ap);
431 mtx_lock(&g_eventlock);
432 while (!(ep->flag & EV_DONE))
433 msleep(ep, &g_eventlock, PRIBIO, "g_waitfor_event", hz);
434 if (ep->flag & EV_CANCELED)
436 mtx_unlock(&g_eventlock);
446 mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF);