xref: /freebsd/sys/geom/geom_event.c (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
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 __FBSDID("$FreeBSD$");
45 
46 #include <sys/param.h>
47 #include <sys/malloc.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.h>
53 #include <sys/errno.h>
54 #include <sys/time.h>
55 #include <geom/geom.h>
56 #include <geom/geom_int.h>
57 
58 #include <machine/stdarg.h>
59 
60 TAILQ_HEAD(event_tailq_head, g_event);
61 
62 static struct event_tailq_head g_events = TAILQ_HEAD_INITIALIZER(g_events);
63 static u_int g_pending_events;
64 static TAILQ_HEAD(,g_provider) g_doorstep = TAILQ_HEAD_INITIALIZER(g_doorstep);
65 static struct mtx g_eventlock;
66 static int g_wither_work;
67 
68 #define G_N_EVENTREFS		20
69 
70 struct g_event {
71 	TAILQ_ENTRY(g_event)	events;
72 	g_event_t		*func;
73 	void			*arg;
74 	int			flag;
75 	void			*ref[G_N_EVENTREFS];
76 };
77 
78 #define EV_DONE		0x80000
79 #define EV_WAKEUP	0x40000
80 #define EV_CANCELED	0x20000
81 #define EV_INPROGRESS	0x10000
82 
83 void
84 g_waitidle(void)
85 {
86 
87 	g_topology_assert_not();
88 
89 	mtx_lock(&g_eventlock);
90 	TSWAIT("GEOM events");
91 	while (!TAILQ_EMPTY(&g_events))
92 		msleep(&g_pending_events, &g_eventlock, PPAUSE,
93 		    "g_waitidle", 0);
94 	TSUNWAIT("GEOM events");
95 	mtx_unlock(&g_eventlock);
96 	curthread->td_pflags &= ~TDP_GEOM;
97 }
98 
99 struct g_attrchanged_args {
100 	struct g_provider *pp;
101 	const char *attr;
102 };
103 
104 static void
105 g_attr_changed_event(void *arg, int flag)
106 {
107 	struct g_attrchanged_args *args;
108 	struct g_provider *pp;
109 	struct g_consumer *cp;
110 	struct g_consumer *next_cp;
111 
112 	args = arg;
113 	pp = args->pp;
114 
115 	g_topology_assert();
116 	if (flag != EV_CANCEL && g_shutdown == 0) {
117 		/*
118 		 * Tell all consumers of the change.
119 		 */
120 		LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
121 			if (cp->geom->attrchanged != NULL)
122 				cp->geom->attrchanged(cp, args->attr);
123 		}
124 	}
125 	g_free(args);
126 }
127 
128 int
129 g_attr_changed(struct g_provider *pp, const char *attr, int flag)
130 {
131 	struct g_attrchanged_args *args;
132 	int error;
133 
134 	args = g_malloc(sizeof *args, flag);
135 	if (args == NULL)
136 		return (ENOMEM);
137 	args->pp = pp;
138 	args->attr = attr;
139 	error = g_post_event(g_attr_changed_event, args, flag, pp, NULL);
140 	if (error != 0)
141 		g_free(args);
142 	return (error);
143 }
144 
145 void
146 g_orphan_provider(struct g_provider *pp, int error)
147 {
148 
149 	/* G_VALID_PROVIDER(pp)  We likely lack topology lock */
150 	g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)",
151 	    pp, pp->name, error);
152 	KASSERT(error != 0,
153 	    ("g_orphan_provider(%p(%s), 0) error must be non-zero\n",
154 	     pp, pp->name));
155 
156 	pp->error = error;
157 	mtx_lock(&g_eventlock);
158 	KASSERT(!(pp->flags & G_PF_ORPHAN),
159 	    ("g_orphan_provider(%p(%s)), already an orphan", pp, pp->name));
160 	pp->flags |= G_PF_ORPHAN;
161 	TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan);
162 	mtx_unlock(&g_eventlock);
163 	wakeup(&g_wait_event);
164 }
165 
166 /*
167  * This function is called once on each provider which the event handler
168  * finds on its g_doorstep.
169  */
170 
171 static void
172 g_orphan_register(struct g_provider *pp)
173 {
174 	struct g_consumer *cp, *cp2;
175 	int wf;
176 
177 	g_topology_assert();
178 	G_VALID_PROVIDER(pp);
179 	g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name);
180 
181 	g_cancel_event(pp);
182 
183 	wf = pp->flags & G_PF_WITHER;
184 	pp->flags &= ~G_PF_WITHER;
185 
186 	/*
187 	 * Tell all consumers the bad news.
188 	 * Don't be surprised if they self-destruct.
189 	 */
190 	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
191 		KASSERT(cp->geom->orphan != NULL,
192 		    ("geom %s has no orphan, class %s",
193 		    cp->geom->name, cp->geom->class->name));
194 		/*
195 		 * XXX: g_dev_orphan method does deferred destroying
196 		 * and it is possible, that other event could already
197 		 * call the orphan method. Check consumer's flags to
198 		 * do not schedule it twice.
199 		 */
200 		if (cp->flags & G_CF_ORPHAN)
201 			continue;
202 		cp->flags |= G_CF_ORPHAN;
203 		cp->geom->orphan(cp);
204 	}
205 	if (LIST_EMPTY(&pp->consumers) && wf)
206 		g_destroy_provider(pp);
207 	else
208 		pp->flags |= wf;
209 #ifdef notyet
210 	cp = LIST_FIRST(&pp->consumers);
211 	if (cp != NULL)
212 		return;
213 	if (pp->geom->flags & G_GEOM_WITHER)
214 		g_destroy_provider(pp);
215 #endif
216 }
217 
218 static int
219 one_event(void)
220 {
221 	struct g_event *ep;
222 	struct g_provider *pp;
223 
224 	g_topology_assert();
225 	mtx_lock(&g_eventlock);
226 	pp = TAILQ_FIRST(&g_doorstep);
227 	if (pp != NULL) {
228 		G_VALID_PROVIDER(pp);
229 		TAILQ_REMOVE(&g_doorstep, pp, orphan);
230 		mtx_unlock(&g_eventlock);
231 		g_orphan_register(pp);
232 		return (1);
233 	}
234 
235 	ep = TAILQ_FIRST(&g_events);
236 	if (ep == NULL) {
237 		wakeup(&g_pending_events);
238 		return (0);
239 	}
240 	ep->flag |= EV_INPROGRESS;
241 	mtx_unlock(&g_eventlock);
242 	g_topology_assert();
243 	ep->func(ep->arg, 0);
244 	g_topology_assert();
245 	mtx_lock(&g_eventlock);
246 	TSRELEASE("GEOM events");
247 	TAILQ_REMOVE(&g_events, ep, events);
248 	ep->flag &= ~EV_INPROGRESS;
249 	if (ep->flag & EV_WAKEUP) {
250 		ep->flag |= EV_DONE;
251 		wakeup(ep);
252 		mtx_unlock(&g_eventlock);
253 	} else {
254 		mtx_unlock(&g_eventlock);
255 		g_free(ep);
256 	}
257 	return (1);
258 }
259 
260 void
261 g_run_events()
262 {
263 
264 	for (;;) {
265 		g_topology_lock();
266 		while (one_event())
267 			;
268 		mtx_assert(&g_eventlock, MA_OWNED);
269 		if (g_wither_work) {
270 			g_wither_work = 0;
271 			mtx_unlock(&g_eventlock);
272 			g_wither_washer();
273 			g_topology_unlock();
274 		} else {
275 			g_topology_unlock();
276 			msleep(&g_wait_event, &g_eventlock, PRIBIO | PDROP,
277 			    "-", 0);
278 		}
279 	}
280 	/* NOTREACHED */
281 }
282 
283 void
284 g_cancel_event(void *ref)
285 {
286 	struct g_event *ep, *epn;
287 	struct g_provider *pp;
288 	u_int n;
289 
290 	mtx_lock(&g_eventlock);
291 	TAILQ_FOREACH(pp, &g_doorstep, orphan) {
292 		if (pp != ref)
293 			continue;
294 		TAILQ_REMOVE(&g_doorstep, pp, orphan);
295 		break;
296 	}
297 	TAILQ_FOREACH_SAFE(ep, &g_events, events, epn) {
298 		if (ep->flag & EV_INPROGRESS)
299 			continue;
300 		for (n = 0; n < G_N_EVENTREFS; n++) {
301 			if (ep->ref[n] == NULL)
302 				break;
303 			if (ep->ref[n] != ref)
304 				continue;
305 			TSRELEASE("GEOM events");
306 			TAILQ_REMOVE(&g_events, ep, events);
307 			ep->func(ep->arg, EV_CANCEL);
308 			mtx_assert(&g_eventlock, MA_OWNED);
309 			if (ep->flag & EV_WAKEUP) {
310 				ep->flag |= (EV_DONE|EV_CANCELED);
311 				wakeup(ep);
312 			} else {
313 				g_free(ep);
314 			}
315 			break;
316 		}
317 	}
318 	if (TAILQ_EMPTY(&g_events))
319 		wakeup(&g_pending_events);
320 	mtx_unlock(&g_eventlock);
321 }
322 
323 struct g_event *
324 g_alloc_event(int flag)
325 {
326 	KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
327 	    ("Wrong flag to g_alloc_event"));
328 
329 	return (g_malloc(sizeof(struct g_event), flag | M_ZERO));
330 }
331 
332 static void
333 g_post_event_ep_va(g_event_t *func, void *arg, int wuflag,
334     struct g_event *ep, va_list ap)
335 {
336 	void *p;
337 	u_int n;
338 
339 	ep->flag = wuflag;
340 	for (n = 0; n < G_N_EVENTREFS; n++) {
341 		p = va_arg(ap, void *);
342 		if (p == NULL)
343 			break;
344 		g_trace(G_T_TOPOLOGY, "  ref %p", p);
345 		ep->ref[n] = p;
346 	}
347 	KASSERT(p == NULL, ("Too many references to event"));
348 	ep->func = func;
349 	ep->arg = arg;
350 	mtx_lock(&g_eventlock);
351 	TSHOLD("GEOM events");
352 	TAILQ_INSERT_TAIL(&g_events, ep, events);
353 	mtx_unlock(&g_eventlock);
354 	wakeup(&g_wait_event);
355 	curthread->td_pflags |= TDP_GEOM;
356 	thread_lock(curthread);
357 	curthread->td_flags |= TDF_ASTPENDING;
358 	thread_unlock(curthread);
359 }
360 
361 void
362 g_post_event_ep(g_event_t *func, void *arg, struct g_event *ep, ...)
363 {
364 	va_list ap;
365 
366 	va_start(ap, ep);
367 	g_post_event_ep_va(func, arg, 0, ep, ap);
368 	va_end(ap);
369 }
370 
371 
372 static int
373 g_post_event_x(g_event_t *func, void *arg, int flag, int wuflag, struct g_event **epp, va_list ap)
374 {
375 	struct g_event *ep;
376 
377 	g_trace(G_T_TOPOLOGY, "g_post_event_x(%p, %p, %d, %d)",
378 	    func, arg, flag, wuflag);
379 	KASSERT(wuflag == 0 || wuflag == EV_WAKEUP,
380 	    ("Wrong wuflag in g_post_event_x(0x%x)", wuflag));
381 	ep = g_alloc_event(flag);
382 	if (ep == NULL)
383 		return (ENOMEM);
384 	if (epp != NULL)
385 		*epp = ep;
386 	g_post_event_ep_va(func, arg, wuflag, ep, ap);
387 	return (0);
388 }
389 
390 int
391 g_post_event(g_event_t *func, void *arg, int flag, ...)
392 {
393 	va_list ap;
394 	int i;
395 
396 	KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
397 	    ("Wrong flag to g_post_event"));
398 	va_start(ap, flag);
399 	i = g_post_event_x(func, arg, flag, 0, NULL, ap);
400 	va_end(ap);
401 	return (i);
402 }
403 
404 void
405 g_do_wither()
406 {
407 
408 	mtx_lock(&g_eventlock);
409 	g_wither_work = 1;
410 	mtx_unlock(&g_eventlock);
411 	wakeup(&g_wait_event);
412 }
413 
414 /*
415  * XXX: It might actually be useful to call this function with topology held.
416  * XXX: This would ensure that the event gets created before anything else
417  * XXX: changes.  At present all users have a handle on things in some other
418  * XXX: way, so this remains an XXX for now.
419  */
420 
421 int
422 g_waitfor_event(g_event_t *func, void *arg, int flag, ...)
423 {
424 	va_list ap;
425 	struct g_event *ep;
426 	int error;
427 
428 	g_topology_assert_not();
429 	KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
430 	    ("Wrong flag to g_post_event"));
431 	va_start(ap, flag);
432 	error = g_post_event_x(func, arg, flag, EV_WAKEUP, &ep, ap);
433 	va_end(ap);
434 	if (error)
435 		return (error);
436 
437 	mtx_lock(&g_eventlock);
438 	while (!(ep->flag & EV_DONE))
439 		msleep(ep, &g_eventlock, PRIBIO, "g_waitfor_event", 0);
440 	if (ep->flag & EV_CANCELED)
441 		error = EAGAIN;
442 	mtx_unlock(&g_eventlock);
443 
444 	g_free(ep);
445 	return (error);
446 }
447 
448 void
449 g_event_init()
450 {
451 
452 	mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF);
453 }
454