1 /*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 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. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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 21 * permission. 22 * 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 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD$ 36 */ 37 38 /* 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 ? 41 */ 42 43 #include <sys/param.h> 44 #ifndef _KERNEL 45 #include <stdio.h> 46 #include <unistd.h> 47 #include <string.h> 48 #include <stdlib.h> 49 #include <signal.h> 50 #include <err.h> 51 #else 52 #include <sys/malloc.h> 53 #include <sys/systm.h> 54 #include <sys/kernel.h> 55 #include <sys/lock.h> 56 #include <sys/mutex.h> 57 #include <sys/eventhandler.h> 58 #endif 59 #include <sys/errno.h> 60 #include <sys/time.h> 61 #include <geom/geom.h> 62 #include <geom/geom_int.h> 63 64 static struct event_tailq_head g_events = TAILQ_HEAD_INITIALIZER(g_events); 65 static u_int g_pending_events; 66 static void g_do_event(struct g_event *ep); 67 static TAILQ_HEAD(,g_provider) g_doorstep = TAILQ_HEAD_INITIALIZER(g_doorstep); 68 static struct mtx g_eventlock; 69 static int g_shutdown; 70 71 void 72 g_waitidle(void) 73 { 74 75 while (g_pending_events) 76 tsleep(&g_pending_events, PPAUSE, "g_waitidle", hz/5); 77 } 78 79 void 80 g_orphan_provider(struct g_provider *pp, int error) 81 { 82 83 g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)", 84 pp, pp->name, error); 85 KASSERT(error != 0, 86 ("g_orphan_provider(%p(%s), 0) error must be non-zero\n", 87 pp, pp->name)); 88 pp->error = error; 89 mtx_lock(&g_eventlock); 90 TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan); 91 mtx_unlock(&g_eventlock); 92 wakeup(&g_wait_event); 93 } 94 95 /* 96 * This function is called once on each provider which the event handler 97 * finds on its g_doorstep. 98 */ 99 100 static void 101 g_orphan_register(struct g_provider *pp) 102 { 103 struct g_consumer *cp, *cp2; 104 105 g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name); 106 g_topology_assert(); 107 108 /* 109 * Tell all consumers the bad news. 110 * Don't get surprised if they self-destruct. 111 */ 112 cp = LIST_FIRST(&pp->consumers); 113 while (cp != NULL) { 114 cp2 = LIST_NEXT(cp, consumers); 115 KASSERT(cp->geom->orphan != NULL, 116 ("geom %s has no orphan, class %s", 117 cp->geom->name, cp->geom->class->name)); 118 cp->geom->orphan(cp); 119 cp = cp2; 120 } 121 } 122 123 static void 124 g_destroy_event(struct g_event *ep) 125 { 126 127 g_free(ep); 128 } 129 130 static void 131 g_do_event(struct g_event *ep) 132 { 133 struct g_class *mp, *mp2; 134 struct g_geom *gp; 135 struct g_consumer *cp, *cp2; 136 struct g_provider *pp; 137 int i; 138 139 g_trace(G_T_TOPOLOGY, "g_do_event(%p) %d m:%p g:%p p:%p c:%p - ", 140 ep, ep->event, ep->class, ep->geom, ep->provider, ep->consumer); 141 g_topology_assert(); 142 switch (ep->event) { 143 case EV_CALL_ME: 144 ep->func(ep->arg); 145 g_topology_assert(); 146 break; 147 case EV_NEW_CLASS: 148 mp2 = ep->class; 149 if (g_shutdown) 150 break; 151 if (mp2->taste == NULL) 152 break; 153 if (g_shutdown) 154 break; 155 LIST_FOREACH(mp, &g_classes, class) { 156 if (mp2 == mp) 157 continue; 158 LIST_FOREACH(gp, &mp->geom, geom) { 159 LIST_FOREACH(pp, &gp->provider, provider) { 160 mp2->taste(ep->class, pp, 0); 161 g_topology_assert(); 162 } 163 } 164 } 165 break; 166 case EV_NEW_PROVIDER: 167 if (g_shutdown) 168 break; 169 g_trace(G_T_TOPOLOGY, "EV_NEW_PROVIDER(%s)", 170 ep->provider->name); 171 LIST_FOREACH(mp, &g_classes, class) { 172 if (mp->taste == NULL) 173 continue; 174 if (!strcmp(ep->provider->name, "geom.ctl") && 175 strcmp(mp->name, "DEV")) 176 continue; 177 i = 1; 178 LIST_FOREACH(cp, &ep->provider->consumers, consumers) 179 if(cp->geom->class == mp) 180 i = 0; 181 if (i) { 182 mp->taste(mp, ep->provider, 0); 183 g_topology_assert(); 184 } 185 } 186 break; 187 case EV_SPOILED: 188 g_trace(G_T_TOPOLOGY, "EV_SPOILED(%p(%s),%p)", 189 ep->provider, ep->provider->name, ep->consumer); 190 cp = LIST_FIRST(&ep->provider->consumers); 191 while (cp != NULL) { 192 cp2 = LIST_NEXT(cp, consumers); 193 if (cp->spoiled) { 194 g_trace(G_T_TOPOLOGY, "spoiling %p (%s) (%p)", 195 cp, cp->geom->name, cp->geom->spoiled); 196 if (cp->geom->spoiled != NULL) 197 cp->geom->spoiled(cp); 198 else 199 cp->spoiled = 0; 200 } 201 cp = cp2; 202 } 203 break; 204 case EV_LAST: 205 default: 206 KASSERT(1 == 0, ("Unknown event %d", ep->event)); 207 } 208 } 209 210 static int 211 one_event(void) 212 { 213 struct g_event *ep; 214 struct g_provider *pp; 215 216 g_topology_lock(); 217 for (;;) { 218 mtx_lock(&g_eventlock); 219 pp = TAILQ_FIRST(&g_doorstep); 220 if (pp != NULL) 221 TAILQ_REMOVE(&g_doorstep, pp, orphan); 222 mtx_unlock(&g_eventlock); 223 if (pp == NULL) 224 break; 225 g_orphan_register(pp); 226 } 227 mtx_lock(&g_eventlock); 228 ep = TAILQ_FIRST(&g_events); 229 if (ep == NULL) { 230 mtx_unlock(&g_eventlock); 231 g_topology_unlock(); 232 return (0); 233 } 234 TAILQ_REMOVE(&g_events, ep, events); 235 mtx_unlock(&g_eventlock); 236 if (ep->class != NULL) 237 ep->class->event = NULL; 238 if (ep->geom != NULL) 239 ep->geom->event = NULL; 240 if (ep->provider != NULL) 241 ep->provider->event = NULL; 242 if (ep->consumer != NULL) 243 ep->consumer->event = NULL; 244 g_do_event(ep); 245 g_destroy_event(ep); 246 g_pending_events--; 247 if (g_pending_events == 0) 248 wakeup(&g_pending_events); 249 g_topology_unlock(); 250 return (1); 251 } 252 253 void 254 g_run_events() 255 { 256 257 while (one_event()) 258 ; 259 } 260 261 void 262 g_post_event(enum g_events ev, struct g_class *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp) 263 { 264 struct g_event *ep; 265 266 g_trace(G_T_TOPOLOGY, "g_post_event(%d, %p, %p, %p, %p)", 267 ev, mp, gp, pp, cp); 268 g_topology_assert(); 269 ep = g_malloc(sizeof *ep, M_WAITOK | M_ZERO); 270 ep->event = ev; 271 if (mp != NULL) { 272 ep->class = mp; 273 KASSERT(mp->event == NULL, ("Double event on class %d %d", 274 ep->event, mp->event->event)); 275 mp->event = ep; 276 } 277 if (gp != NULL) { 278 ep->geom = gp; 279 KASSERT(gp->event == NULL, ("Double event on geom %d %d", 280 ep->event, gp->event->event)); 281 gp->event = ep; 282 } 283 if (pp != NULL) { 284 ep->provider = pp; 285 KASSERT(pp->event == NULL, ("Double event on provider %s %d %d", 286 pp->name, ep->event, pp->event->event)); 287 pp->event = ep; 288 } 289 if (cp != NULL) { 290 ep->consumer = cp; 291 KASSERT(cp->event == NULL, ("Double event on consumer %d %d", 292 ep->event, cp->event->event)); 293 cp->event = ep; 294 } 295 mtx_lock(&g_eventlock); 296 g_pending_events++; 297 TAILQ_INSERT_TAIL(&g_events, ep, events); 298 mtx_unlock(&g_eventlock); 299 wakeup(&g_wait_event); 300 } 301 302 int 303 g_call_me(g_call_me_t *func, void *arg) 304 { 305 struct g_event *ep; 306 307 g_trace(G_T_TOPOLOGY, "g_call_me(%p, %p", func, arg); 308 ep = g_malloc(sizeof *ep, M_NOWAIT | M_ZERO); 309 if (ep == NULL) 310 return (ENOMEM); 311 ep->event = EV_CALL_ME; 312 ep->func = func; 313 ep->arg = arg; 314 mtx_lock(&g_eventlock); 315 g_pending_events++; 316 TAILQ_INSERT_TAIL(&g_events, ep, events); 317 mtx_unlock(&g_eventlock); 318 wakeup(&g_wait_event); 319 return (0); 320 } 321 322 #ifdef _KERNEL 323 static void 324 geom_shutdown(void *foo __unused) 325 { 326 327 g_shutdown = 1; 328 } 329 #endif 330 331 void 332 g_event_init() 333 { 334 335 #ifdef _KERNEL 336 337 EVENTHANDLER_REGISTER(shutdown_pre_sync, geom_shutdown, NULL, 338 SHUTDOWN_PRI_FIRST); 339 #endif 340 mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF); 341 } 342