xref: /freebsd/sys/geom/geom_event.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
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/cdefs.h>
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(struct thread *td)
83 {
84 
85 	g_topology_assert_not();
86 
87 	mtx_lock(&g_eventlock);
88 	TSWAIT("GEOM events");
89 	while (!TAILQ_EMPTY(&g_events))
90 		msleep(&g_pending_events, &g_eventlock, PPAUSE,
91 		    "g_waitidle", 0);
92 	TSUNWAIT("GEOM events");
93 	mtx_unlock(&g_eventlock);
94 	td->td_pflags &= ~TDP_GEOM;
95 }
96 
97 static void
98 ast_geom(struct thread *td, int tda __unused)
99 {
100 	/*
101 	 * If this thread tickled GEOM, we need to wait for the giggling to
102 	 * stop before we return to userland.
103 	 */
104 	g_waitidle(td);
105 }
106 
107 static void
108 geom_event_init(void *arg __unused)
109 {
110 	ast_register(TDA_GEOM, ASTR_ASTF_REQUIRED | ASTR_TDP | ASTR_KCLEAR,
111 	    TDP_GEOM, ast_geom);
112 }
113 SYSINIT(geom_event, SI_SUB_INTRINSIC, SI_ORDER_ANY, geom_event_init, NULL);
114 
115 struct g_attrchanged_args {
116 	struct g_provider *pp;
117 	const char *attr;
118 };
119 
120 static void
121 g_attr_changed_event(void *arg, int flag)
122 {
123 	struct g_attrchanged_args *args;
124 	struct g_provider *pp;
125 	struct g_consumer *cp;
126 	struct g_consumer *next_cp;
127 
128 	args = arg;
129 	pp = args->pp;
130 
131 	g_topology_assert();
132 	if (flag != EV_CANCEL && g_shutdown == 0) {
133 		/*
134 		 * Tell all consumers of the change.
135 		 */
136 		LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
137 			if (cp->geom->attrchanged != NULL)
138 				cp->geom->attrchanged(cp, args->attr);
139 		}
140 	}
141 	g_free(args);
142 }
143 
144 int
145 g_attr_changed(struct g_provider *pp, const char *attr, int flag)
146 {
147 	struct g_attrchanged_args *args;
148 	int error;
149 
150 	args = g_malloc(sizeof *args, flag);
151 	if (args == NULL)
152 		return (ENOMEM);
153 	args->pp = pp;
154 	args->attr = attr;
155 	error = g_post_event(g_attr_changed_event, args, flag, pp, NULL);
156 	if (error != 0)
157 		g_free(args);
158 	return (error);
159 }
160 
161 void
162 g_orphan_provider(struct g_provider *pp, int error)
163 {
164 
165 	/* G_VALID_PROVIDER(pp)  We likely lack topology lock */
166 	g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)",
167 	    pp, pp->name, error);
168 	KASSERT(error != 0,
169 	    ("g_orphan_provider(%p(%s), 0) error must be non-zero\n",
170 	     pp, pp->name));
171 
172 	pp->error = error;
173 	mtx_lock(&g_eventlock);
174 	KASSERT(!(pp->flags & G_PF_ORPHAN),
175 	    ("g_orphan_provider(%p(%s)), already an orphan", pp, pp->name));
176 	pp->flags |= G_PF_ORPHAN;
177 	TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan);
178 	mtx_unlock(&g_eventlock);
179 	wakeup(&g_wait_event);
180 }
181 
182 /*
183  * This function is called once on each provider which the event handler
184  * finds on its g_doorstep.
185  */
186 
187 static void
188 g_orphan_register(struct g_provider *pp)
189 {
190 	struct g_consumer *cp, *cp2;
191 	int wf;
192 
193 	g_topology_assert();
194 	G_VALID_PROVIDER(pp);
195 	g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name);
196 
197 	g_cancel_event(pp);
198 
199 	wf = pp->flags & G_PF_WITHER;
200 	pp->flags &= ~G_PF_WITHER;
201 
202 	/*
203 	 * Tell all consumers the bad news.
204 	 * Don't be surprised if they self-destruct.
205 	 */
206 	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
207 		KASSERT(cp->geom->orphan != NULL,
208 		    ("geom %s has no orphan, class %s",
209 		    cp->geom->name, cp->geom->class->name));
210 		/*
211 		 * XXX: g_dev_orphan method does deferred destroying
212 		 * and it is possible, that other event could already
213 		 * call the orphan method. Check consumer's flags to
214 		 * do not schedule it twice.
215 		 */
216 		if (cp->flags & G_CF_ORPHAN)
217 			continue;
218 		cp->flags |= G_CF_ORPHAN;
219 		cp->geom->orphan(cp);
220 	}
221 	if (LIST_EMPTY(&pp->consumers) && wf)
222 		g_destroy_provider(pp);
223 	else
224 		pp->flags |= wf;
225 #ifdef notyet
226 	cp = LIST_FIRST(&pp->consumers);
227 	if (cp != NULL)
228 		return;
229 	if (pp->geom->flags & G_GEOM_WITHER)
230 		g_destroy_provider(pp);
231 #endif
232 }
233 
234 static int
235 one_event(void)
236 {
237 	struct g_event *ep;
238 	struct g_provider *pp;
239 
240 	g_topology_assert();
241 	mtx_lock(&g_eventlock);
242 	pp = TAILQ_FIRST(&g_doorstep);
243 	if (pp != NULL) {
244 		G_VALID_PROVIDER(pp);
245 		TAILQ_REMOVE(&g_doorstep, pp, orphan);
246 		mtx_unlock(&g_eventlock);
247 		g_orphan_register(pp);
248 		return (1);
249 	}
250 
251 	ep = TAILQ_FIRST(&g_events);
252 	if (ep == NULL) {
253 		wakeup(&g_pending_events);
254 		return (0);
255 	}
256 	ep->flag |= EV_INPROGRESS;
257 	mtx_unlock(&g_eventlock);
258 	g_topology_assert();
259 	ep->func(ep->arg, 0);
260 	g_topology_assert();
261 	mtx_lock(&g_eventlock);
262 	TSRELEASE("GEOM events");
263 	TAILQ_REMOVE(&g_events, ep, events);
264 	ep->flag &= ~EV_INPROGRESS;
265 	if (ep->flag & EV_WAKEUP) {
266 		ep->flag |= EV_DONE;
267 		wakeup(ep);
268 		mtx_unlock(&g_eventlock);
269 	} else {
270 		mtx_unlock(&g_eventlock);
271 		g_free(ep);
272 	}
273 	return (1);
274 }
275 
276 void
277 g_run_events(void)
278 {
279 
280 	for (;;) {
281 		g_topology_lock();
282 		while (one_event())
283 			;
284 		mtx_assert(&g_eventlock, MA_OWNED);
285 		if (g_wither_work) {
286 			g_wither_work = 0;
287 			mtx_unlock(&g_eventlock);
288 			g_wither_washer();
289 			g_topology_unlock();
290 		} else {
291 			g_topology_unlock();
292 			msleep(&g_wait_event, &g_eventlock, PRIBIO | PDROP,
293 			    "-", 0);
294 		}
295 	}
296 	/* NOTREACHED */
297 }
298 
299 void
300 g_cancel_event(void *ref)
301 {
302 	struct g_event *ep, *epn;
303 	struct g_provider *pp;
304 	u_int n;
305 
306 	mtx_lock(&g_eventlock);
307 	TAILQ_FOREACH(pp, &g_doorstep, orphan) {
308 		if (pp != ref)
309 			continue;
310 		TAILQ_REMOVE(&g_doorstep, pp, orphan);
311 		break;
312 	}
313 	TAILQ_FOREACH_SAFE(ep, &g_events, events, epn) {
314 		if (ep->flag & EV_INPROGRESS)
315 			continue;
316 		for (n = 0; n < G_N_EVENTREFS; n++) {
317 			if (ep->ref[n] == NULL)
318 				break;
319 			if (ep->ref[n] != ref)
320 				continue;
321 			TSRELEASE("GEOM events");
322 			TAILQ_REMOVE(&g_events, ep, events);
323 			ep->func(ep->arg, EV_CANCEL);
324 			mtx_assert(&g_eventlock, MA_OWNED);
325 			if (ep->flag & EV_WAKEUP) {
326 				ep->flag |= (EV_DONE|EV_CANCELED);
327 				wakeup(ep);
328 			} else {
329 				g_free(ep);
330 			}
331 			break;
332 		}
333 	}
334 	if (TAILQ_EMPTY(&g_events))
335 		wakeup(&g_pending_events);
336 	mtx_unlock(&g_eventlock);
337 }
338 
339 struct g_event *
340 g_alloc_event(int flag)
341 {
342 	KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
343 	    ("Wrong flag to g_alloc_event"));
344 
345 	return (g_malloc(sizeof(struct g_event), flag | M_ZERO));
346 }
347 
348 static void
349 g_post_event_ep_va(g_event_t *func, void *arg, int wuflag,
350     struct g_event *ep, va_list ap)
351 {
352 	void *p;
353 	u_int n;
354 
355 	ep->flag = wuflag;
356 	for (n = 0; n < G_N_EVENTREFS; n++) {
357 		p = va_arg(ap, void *);
358 		if (p == NULL)
359 			break;
360 		g_trace(G_T_TOPOLOGY, "  ref %p", p);
361 		ep->ref[n] = p;
362 	}
363 	KASSERT(p == NULL, ("Too many references to event"));
364 	ep->func = func;
365 	ep->arg = arg;
366 	mtx_lock(&g_eventlock);
367 	TSHOLD("GEOM events");
368 	TAILQ_INSERT_TAIL(&g_events, ep, events);
369 	mtx_unlock(&g_eventlock);
370 	wakeup(&g_wait_event);
371 	curthread->td_pflags |= TDP_GEOM;
372 	ast_sched(curthread, TDA_GEOM);
373 }
374 
375 void
376 g_post_event_ep(g_event_t *func, void *arg, struct g_event *ep, ...)
377 {
378 	va_list ap;
379 
380 	va_start(ap, ep);
381 	g_post_event_ep_va(func, arg, 0, ep, ap);
382 	va_end(ap);
383 }
384 
385 
386 static int
387 g_post_event_x(g_event_t *func, void *arg, int flag, int wuflag, struct g_event **epp, va_list ap)
388 {
389 	struct g_event *ep;
390 
391 	g_trace(G_T_TOPOLOGY, "g_post_event_x(%p, %p, %d, %d)",
392 	    func, arg, flag, wuflag);
393 	KASSERT(wuflag == 0 || wuflag == EV_WAKEUP,
394 	    ("Wrong wuflag in g_post_event_x(0x%x)", wuflag));
395 	ep = g_alloc_event(flag);
396 	if (ep == NULL)
397 		return (ENOMEM);
398 	if (epp != NULL)
399 		*epp = ep;
400 	g_post_event_ep_va(func, arg, wuflag, ep, ap);
401 	return (0);
402 }
403 
404 int
405 g_post_event(g_event_t *func, void *arg, int flag, ...)
406 {
407 	va_list ap;
408 	int i;
409 
410 	KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
411 	    ("Wrong flag to g_post_event"));
412 	va_start(ap, flag);
413 	i = g_post_event_x(func, arg, flag, 0, NULL, ap);
414 	va_end(ap);
415 	return (i);
416 }
417 
418 void
419 g_do_wither(void)
420 {
421 
422 	mtx_lock(&g_eventlock);
423 	g_wither_work = 1;
424 	mtx_unlock(&g_eventlock);
425 	wakeup(&g_wait_event);
426 }
427 
428 /*
429  * XXX: It might actually be useful to call this function with topology held.
430  * XXX: This would ensure that the event gets created before anything else
431  * XXX: changes.  At present all users have a handle on things in some other
432  * XXX: way, so this remains an XXX for now.
433  */
434 
435 int
436 g_waitfor_event(g_event_t *func, void *arg, int flag, ...)
437 {
438 	va_list ap;
439 	struct g_event *ep;
440 	int error;
441 
442 	g_topology_assert_not();
443 	KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
444 	    ("Wrong flag to g_post_event"));
445 	va_start(ap, flag);
446 	error = g_post_event_x(func, arg, flag, EV_WAKEUP, &ep, ap);
447 	va_end(ap);
448 	if (error)
449 		return (error);
450 
451 	mtx_lock(&g_eventlock);
452 	while (!(ep->flag & EV_DONE))
453 		msleep(ep, &g_eventlock, PRIBIO, "g_waitfor_event", 0);
454 	if (ep->flag & EV_CANCELED)
455 		error = EAGAIN;
456 	mtx_unlock(&g_eventlock);
457 
458 	g_free(ep);
459 	return (error);
460 }
461 
462 void
463 g_event_init(void)
464 {
465 
466 	mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF);
467 }
468