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;
99 struct g_attrchanged_args {
100 struct g_provider *pp;
105 g_attr_changed_event(void *arg, int flag)
107 struct g_attrchanged_args *args;
108 struct g_provider *pp;
109 struct g_consumer *cp;
110 struct g_consumer *next_cp;
116 if (flag != EV_CANCEL && g_shutdown == 0) {
118 * Tell all consumers of the change.
120 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
121 if (cp->geom->attrchanged != NULL)
122 cp->geom->attrchanged(cp, args->attr);
129 g_attr_changed(struct g_provider *pp, const char *attr, int flag)
131 struct g_attrchanged_args *args;
134 args = g_malloc(sizeof *args, flag);
139 error = g_post_event(g_attr_changed_event, args, flag, pp, NULL);
146 g_orphan_provider(struct g_provider *pp, int error)
149 /* G_VALID_PROVIDER(pp) We likely lack topology lock */
150 g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)",
151 pp, pp->name, error);
153 ("g_orphan_provider(%p(%s), 0) error must be non-zero\n",
157 mtx_lock(&g_eventlock);
158 KASSERT(!(pp->flags & G_PF_ORPHAN),
159 ("g_orphan_provider(%p(%s)), already an orphan", pp, pp->name));
160 pp->flags |= G_PF_ORPHAN;
161 TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan);
162 mtx_unlock(&g_eventlock);
163 wakeup(&g_wait_event);
167 * This function is called once on each provider which the event handler
168 * finds on its g_doorstep.
172 g_orphan_register(struct g_provider *pp)
174 struct g_consumer *cp, *cp2;
178 G_VALID_PROVIDER(pp);
179 g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name);
183 wf = pp->flags & G_PF_WITHER;
184 pp->flags &= ~G_PF_WITHER;
187 * Tell all consumers the bad news.
188 * Don't be surprised if they self-destruct.
190 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
191 KASSERT(cp->geom->orphan != NULL,
192 ("geom %s has no orphan, class %s",
193 cp->geom->name, cp->geom->class->name));
195 * XXX: g_dev_orphan method does deferred destroying
196 * and it is possible, that other event could already
197 * call the orphan method. Check consumer's flags to
198 * do not schedule it twice.
200 if (cp->flags & G_CF_ORPHAN)
202 cp->flags |= G_CF_ORPHAN;
203 cp->geom->orphan(cp);
205 if (LIST_EMPTY(&pp->consumers) && wf)
206 g_destroy_provider(pp);
210 cp = LIST_FIRST(&pp->consumers);
213 if (pp->geom->flags & G_GEOM_WITHER)
214 g_destroy_provider(pp);
222 struct g_provider *pp;
225 mtx_lock(&g_eventlock);
226 pp = TAILQ_FIRST(&g_doorstep);
228 G_VALID_PROVIDER(pp);
229 TAILQ_REMOVE(&g_doorstep, pp, orphan);
230 mtx_unlock(&g_eventlock);
231 g_orphan_register(pp);
235 ep = TAILQ_FIRST(&g_events);
237 wakeup(&g_pending_events);
240 ep->flag |= EV_INPROGRESS;
241 mtx_unlock(&g_eventlock);
243 ep->func(ep->arg, 0);
245 mtx_lock(&g_eventlock);
246 TSRELEASE("GEOM events");
247 TAILQ_REMOVE(&g_events, ep, events);
248 ep->flag &= ~EV_INPROGRESS;
249 if (ep->flag & EV_WAKEUP) {
252 mtx_unlock(&g_eventlock);
254 mtx_unlock(&g_eventlock);
268 mtx_assert(&g_eventlock, MA_OWNED);
271 mtx_unlock(&g_eventlock);
276 msleep(&g_wait_event, &g_eventlock, PRIBIO | PDROP,
284 g_cancel_event(void *ref)
286 struct g_event *ep, *epn;
287 struct g_provider *pp;
290 mtx_lock(&g_eventlock);
291 TAILQ_FOREACH(pp, &g_doorstep, orphan) {
294 TAILQ_REMOVE(&g_doorstep, pp, orphan);
297 TAILQ_FOREACH_SAFE(ep, &g_events, events, epn) {
298 if (ep->flag & EV_INPROGRESS)
300 for (n = 0; n < G_N_EVENTREFS; n++) {
301 if (ep->ref[n] == NULL)
303 if (ep->ref[n] != ref)
305 TSRELEASE("GEOM events");
306 TAILQ_REMOVE(&g_events, ep, events);
307 ep->func(ep->arg, EV_CANCEL);
308 mtx_assert(&g_eventlock, MA_OWNED);
309 if (ep->flag & EV_WAKEUP) {
310 ep->flag |= (EV_DONE|EV_CANCELED);
318 if (TAILQ_EMPTY(&g_events))
319 wakeup(&g_pending_events);
320 mtx_unlock(&g_eventlock);
324 g_alloc_event(int flag)
326 KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
327 ("Wrong flag to g_alloc_event"));
329 return (g_malloc(sizeof(struct g_event), flag | M_ZERO));
333 g_post_event_ep_va(g_event_t *func, void *arg, int wuflag,
334 struct g_event *ep, va_list ap)
340 for (n = 0; n < G_N_EVENTREFS; n++) {
341 p = va_arg(ap, void *);
344 g_trace(G_T_TOPOLOGY, " ref %p", p);
347 KASSERT(p == NULL, ("Too many references to event"));
350 mtx_lock(&g_eventlock);
351 TSHOLD("GEOM events");
352 TAILQ_INSERT_TAIL(&g_events, ep, events);
353 mtx_unlock(&g_eventlock);
354 wakeup(&g_wait_event);
355 curthread->td_pflags |= TDP_GEOM;
356 thread_lock(curthread);
357 curthread->td_flags |= TDF_ASTPENDING;
358 thread_unlock(curthread);
362 g_post_event_ep(g_event_t *func, void *arg, struct g_event *ep, ...)
367 g_post_event_ep_va(func, arg, 0, ep, ap);
373 g_post_event_x(g_event_t *func, void *arg, int flag, int wuflag, struct g_event **epp, va_list ap)
377 g_trace(G_T_TOPOLOGY, "g_post_event_x(%p, %p, %d, %d)",
378 func, arg, flag, wuflag);
379 KASSERT(wuflag == 0 || wuflag == EV_WAKEUP,
380 ("Wrong wuflag in g_post_event_x(0x%x)", wuflag));
381 ep = g_alloc_event(flag);
386 g_post_event_ep_va(func, arg, wuflag, ep, ap);
391 g_post_event(g_event_t *func, void *arg, int flag, ...)
396 KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
397 ("Wrong flag to g_post_event"));
399 i = g_post_event_x(func, arg, flag, 0, NULL, ap);
408 mtx_lock(&g_eventlock);
410 mtx_unlock(&g_eventlock);
411 wakeup(&g_wait_event);
415 * XXX: It might actually be useful to call this function with topology held.
416 * XXX: This would ensure that the event gets created before anything else
417 * XXX: changes. At present all users have a handle on things in some other
418 * XXX: way, so this remains an XXX for now.
422 g_waitfor_event(g_event_t *func, void *arg, int flag, ...)
428 g_topology_assert_not();
429 KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
430 ("Wrong flag to g_post_event"));
432 error = g_post_event_x(func, arg, flag, EV_WAKEUP, &ep, ap);
437 mtx_lock(&g_eventlock);
438 while (!(ep->flag & EV_DONE))
439 msleep(ep, &g_eventlock, PRIBIO, "g_waitfor_event", 0);
440 if (ep->flag & EV_CANCELED)
442 mtx_unlock(&g_eventlock);
452 mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF);