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

Cache object: e658a7b1ca00436146d5b635b248d389


[ 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.