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