geom_event.c revision 122880
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 36/* 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 ? 39 */ 40 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: head/sys/geom/geom_event.c 122880 2003-11-18 07:54:12Z phk $"); 43 44#include <sys/param.h> 45#include <sys/malloc.h> 46#include <sys/systm.h> 47#include <sys/kernel.h> 48#include <sys/lock.h> 49#include <sys/mutex.h> 50#include <machine/stdarg.h> 51#include <sys/errno.h> 52#include <sys/time.h> 53#include <geom/geom.h> 54#include <geom/geom_int.h> 55 56TAILQ_HEAD(event_tailq_head, g_event); 57 58static struct event_tailq_head g_events = TAILQ_HEAD_INITIALIZER(g_events); 59static u_int g_pending_events; 60static TAILQ_HEAD(,g_provider) g_doorstep = TAILQ_HEAD_INITIALIZER(g_doorstep); 61static struct mtx g_eventlock; 62static struct sx g_eventstall; 63 64#define G_N_EVENTREFS 20 65 66struct g_event { 67 TAILQ_ENTRY(g_event) events; 68 g_event_t *func; 69 void *arg; 70 int flag; 71 void *ref[G_N_EVENTREFS]; 72}; 73 74#define EV_DONE 0x80000 75#define EV_WAKEUP 0x40000 76#define EV_CANCELED 0x20000 77 78void 79g_waitidle(void) 80{ 81 82 while (g_pending_events) 83 tsleep(&g_pending_events, PPAUSE, "g_waitidle", hz/5); 84} 85 86void 87g_stall_events(void) 88{ 89 90 sx_xlock(&g_eventstall); 91} 92 93void 94g_release_events(void) 95{ 96 97 sx_xunlock(&g_eventstall); 98} 99 100void 101g_orphan_provider(struct g_provider *pp, int error) 102{ 103 104 g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)", 105 pp, pp->name, error); 106 KASSERT(error != 0, 107 ("g_orphan_provider(%p(%s), 0) error must be non-zero\n", 108 pp, pp->name)); 109 pp->error = error; 110 mtx_lock(&g_eventlock); 111 TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan); 112 mtx_unlock(&g_eventlock); 113 wakeup(&g_wait_event); 114} 115 116/* 117 * This function is called once on each provider which the event handler 118 * finds on its g_doorstep. 119 */ 120 121static void 122g_orphan_register(struct g_provider *pp) 123{ 124 struct g_consumer *cp, *cp2; 125 126 g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name); 127 g_topology_assert(); 128 129 /* 130 * Tell all consumers the bad news. 131 * Don't be surprised if they self-destruct. 132 */ 133 cp = LIST_FIRST(&pp->consumers); 134 while (cp != NULL) { 135 cp2 = LIST_NEXT(cp, consumers); 136 KASSERT(cp->geom->orphan != NULL, 137 ("geom %s has no orphan, class %s", 138 cp->geom->name, cp->geom->class->name)); 139 cp->geom->orphan(cp); 140 cp = cp2; 141 } 142 if (LIST_EMPTY(&pp->consumers) && (pp->flags & G_PF_WITHER)) 143 g_destroy_provider(pp); 144#ifdef notyet 145 cp = LIST_FIRST(&pp->consumers); 146 if (cp != NULL) 147 return; 148 if (pp->geom->flags & G_GEOM_WITHER) 149 g_destroy_provider(pp); 150#endif 151} 152 153static int 154one_event(void) 155{ 156 struct g_event *ep; 157 struct g_provider *pp; 158 159 sx_xlock(&g_eventstall); 160 g_topology_lock(); 161 for (;;) { 162 mtx_lock(&g_eventlock); 163 pp = TAILQ_FIRST(&g_doorstep); 164 if (pp != NULL) 165 TAILQ_REMOVE(&g_doorstep, pp, orphan); 166 mtx_unlock(&g_eventlock); 167 if (pp == NULL) 168 break; 169 g_orphan_register(pp); 170 } 171 mtx_lock(&g_eventlock); 172 ep = TAILQ_FIRST(&g_events); 173 if (ep == NULL) { 174 mtx_unlock(&g_eventlock); 175 g_topology_unlock(); 176 sx_xunlock(&g_eventstall); 177 return (0); 178 } 179 TAILQ_REMOVE(&g_events, ep, events); 180 mtx_unlock(&g_eventlock); 181 g_topology_assert(); 182 ep->func(ep->arg, 0); 183 g_topology_assert(); 184 if (ep->flag & EV_WAKEUP) { 185 ep->flag |= EV_DONE; 186 wakeup(ep); 187 } else { 188 g_free(ep); 189 } 190 g_pending_events--; 191 if (g_pending_events == 0) 192 wakeup(&g_pending_events); 193 g_topology_unlock(); 194 sx_xunlock(&g_eventstall); 195 return (1); 196} 197 198void 199g_run_events() 200{ 201 202 while (one_event()) 203 ; 204} 205 206void 207g_cancel_event(void *ref) 208{ 209 struct g_event *ep, *epn; 210 struct g_provider *pp; 211 u_int n; 212 213 mtx_lock(&g_eventlock); 214 TAILQ_FOREACH(pp, &g_doorstep, orphan) { 215 if (pp != ref) 216 continue; 217 TAILQ_REMOVE(&g_doorstep, pp, orphan); 218 break; 219 } 220 for (ep = TAILQ_FIRST(&g_events); ep != NULL; ep = epn) { 221 epn = TAILQ_NEXT(ep, events); 222 for (n = 0; n < G_N_EVENTREFS; n++) { 223 if (ep->ref[n] == NULL) 224 break; 225 if (ep->ref[n] == ref) { 226 TAILQ_REMOVE(&g_events, ep, events); 227 ep->func(ep->arg, EV_CANCEL); 228 if (ep->flag & EV_WAKEUP) { 229 ep->flag |= EV_DONE; 230 ep->flag |= EV_CANCELED; 231 wakeup(ep); 232 } else { 233 g_free(ep); 234 } 235 break; 236 } 237 } 238 } 239 mtx_unlock(&g_eventlock); 240} 241 242static int 243g_post_event_x(g_event_t *func, void *arg, int flag, struct g_event **epp, va_list ap) 244{ 245 struct g_event *ep; 246 void *p; 247 u_int n; 248 249 g_trace(G_T_TOPOLOGY, "g_post_event_x(%p, %p, %d)", func, arg, flag); 250 ep = g_malloc(sizeof *ep, flag | M_ZERO); 251 if (ep == NULL) 252 return (ENOMEM); 253 ep->flag = flag; 254 for (n = 0; n < G_N_EVENTREFS; n++) { 255 p = va_arg(ap, void *); 256 if (p == NULL) 257 break; 258 g_trace(G_T_TOPOLOGY, " ref %p", p); 259 ep->ref[n] = p; 260 } 261 KASSERT(p == NULL, ("Too many references to event")); 262 ep->func = func; 263 ep->arg = arg; 264 mtx_lock(&g_eventlock); 265 g_pending_events++; 266 TAILQ_INSERT_TAIL(&g_events, ep, events); 267 mtx_unlock(&g_eventlock); 268 wakeup(&g_wait_event); 269 if (epp != NULL) 270 *epp = ep; 271 return (0); 272} 273 274int 275g_post_event(g_event_t *func, void *arg, int flag, ...) 276{ 277 va_list ap; 278 int i; 279 280 KASSERT(flag == M_WAITOK || flag == M_NOWAIT, 281 ("Wrong flag to g_post_event")); 282 va_start(ap, flag); 283 i = g_post_event_x(func, arg, flag, NULL, ap); 284 va_end(ap); 285 return (i); 286} 287 288 289/* 290 * XXX: It might actually be useful to call this function with topology held. 291 * XXX: This would ensure that the event gets created before anything else 292 * XXX: changes. At present all users have a handle on things in some other 293 * XXX: way, so this remains an XXX for now. 294 */ 295 296int 297g_waitfor_event(g_event_t *func, void *arg, int flag, ...) 298{ 299 va_list ap; 300 struct g_event *ep; 301 int error; 302 303 /* g_topology_assert_not(); */ 304 KASSERT(flag == M_WAITOK || flag == M_NOWAIT, 305 ("Wrong flag to g_post_event")); 306 va_start(ap, flag); 307 error = g_post_event_x(func, arg, flag | EV_WAKEUP, &ep, ap); 308 va_end(ap); 309 if (error) 310 return (error); 311 do 312 tsleep(ep, PRIBIO, "g_waitfor_event", hz); 313 while (!(ep->flag & EV_DONE)); 314 if (ep->flag & EV_CANCELED) 315 error = EAGAIN; 316 g_free(ep); 317 return (error); 318} 319 320void 321g_event_init() 322{ 323 324 mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF); 325 sx_init(&g_eventstall, "GEOM event stalling"); 326} 327