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