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