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