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