The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_event.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 39bfc073f375ae1b6f8f49f64462a4c5


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.