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: src/sys/geom/geom_event.c,v 1.50.2.1 2005/02/28 19:32:24 phk Exp $");
   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 <machine/stdarg.h>
   52 #include <sys/errno.h>
   53 #include <sys/time.h>
   54 #include <geom/geom.h>
   55 #include <geom/geom_int.h>
   56 
   57 TAILQ_HEAD(event_tailq_head, g_event);
   58 
   59 static struct event_tailq_head g_events = TAILQ_HEAD_INITIALIZER(g_events);
   60 static u_int g_pending_events;
   61 static TAILQ_HEAD(,g_provider) g_doorstep = TAILQ_HEAD_INITIALIZER(g_doorstep);
   62 static struct mtx g_eventlock;
   63 static int g_wither_work;
   64 
   65 #define G_N_EVENTREFS           20
   66 
   67 struct g_event {
   68         TAILQ_ENTRY(g_event)    events;
   69         g_event_t               *func;
   70         void                    *arg;
   71         int                     flag;
   72         void                    *ref[G_N_EVENTREFS];
   73 };
   74 
   75 #define EV_DONE         0x80000
   76 #define EV_WAKEUP       0x40000
   77 #define EV_CANCELED     0x20000
   78 
   79 void
   80 g_waitidle(void)
   81 {
   82 
   83         g_topology_assert_not();
   84         mtx_assert(&Giant, MA_NOTOWNED);
   85 
   86         while (g_pending_events)
   87                 tsleep(&g_pending_events, PPAUSE, "g_waitidle", hz/5);
   88         curthread->td_pflags &= ~TDP_GEOM;
   89 }
   90 
   91 void
   92 g_orphan_provider(struct g_provider *pp, int error)
   93 {
   94 
   95         /* G_VALID_PROVIDER(pp)  We likely lack topology lock */
   96         g_trace(G_T_TOPOLOGY, "g_orphan_provider(%p(%s), %d)",
   97             pp, pp->name, error);
   98         KASSERT(error != 0,
   99             ("g_orphan_provider(%p(%s), 0) error must be non-zero\n",
  100              pp, pp->name));
  101         
  102         pp->error = error;
  103         mtx_lock(&g_eventlock);
  104         KASSERT(!(pp->flags & G_PF_ORPHAN),
  105             ("g_orphan_provider(%p(%s)), already an orphan", pp, pp->name));
  106         pp->flags |= G_PF_ORPHAN;
  107         TAILQ_INSERT_TAIL(&g_doorstep, pp, orphan);
  108         mtx_unlock(&g_eventlock);
  109         wakeup(&g_wait_event);
  110 }
  111 
  112 /*
  113  * This function is called once on each provider which the event handler
  114  * finds on its g_doorstep.
  115  */
  116 
  117 static void
  118 g_orphan_register(struct g_provider *pp)
  119 {
  120         struct g_consumer *cp, *cp2;
  121         int wf;
  122 
  123         g_topology_assert();
  124         G_VALID_PROVIDER(pp);
  125         g_trace(G_T_TOPOLOGY, "g_orphan_register(%s)", pp->name);
  126 
  127         wf = pp->flags & G_PF_WITHER;
  128         pp->flags &= ~G_PF_WITHER;
  129 
  130         /*
  131          * Tell all consumers the bad news.
  132          * Don't be surprised if they self-destruct.
  133          */
  134         cp = LIST_FIRST(&pp->consumers);
  135         while (cp != NULL) {
  136                 cp2 = LIST_NEXT(cp, consumers);
  137                 KASSERT(cp->geom->orphan != NULL,
  138                     ("geom %s has no orphan, class %s",
  139                     cp->geom->name, cp->geom->class->name));
  140                 cp->geom->orphan(cp);
  141                 cp = cp2;
  142         }
  143         if (LIST_EMPTY(&pp->consumers) && wf)
  144                 g_destroy_provider(pp);
  145         else
  146                 pp->flags |= wf;
  147 #ifdef notyet
  148         cp = LIST_FIRST(&pp->consumers);
  149         if (cp != NULL)
  150                 return;
  151         if (pp->geom->flags & G_GEOM_WITHER)
  152                 g_destroy_provider(pp);
  153 #endif
  154 }
  155 
  156 static int
  157 one_event(void)
  158 {
  159         struct g_event *ep;
  160         struct g_provider *pp;
  161 
  162         g_topology_lock();
  163         for (;;) {
  164                 mtx_lock(&g_eventlock);
  165                 pp = TAILQ_FIRST(&g_doorstep);
  166                 if (pp != NULL) {
  167                         G_VALID_PROVIDER(pp);
  168                         TAILQ_REMOVE(&g_doorstep, pp, orphan);
  169                 }
  170                 mtx_unlock(&g_eventlock);
  171                 if (pp == NULL)
  172                         break;
  173                 g_orphan_register(pp);
  174         }
  175         mtx_lock(&g_eventlock);
  176         ep = TAILQ_FIRST(&g_events);
  177         if (ep == NULL) {
  178                 mtx_unlock(&g_eventlock);
  179                 g_topology_unlock();
  180                 return (0);
  181         }
  182         TAILQ_REMOVE(&g_events, ep, events);
  183         mtx_unlock(&g_eventlock);
  184         g_topology_assert();
  185         ep->func(ep->arg, 0);
  186         g_topology_assert();
  187         if (ep->flag & EV_WAKEUP) {
  188                 ep->flag |= EV_DONE;
  189                 wakeup(ep);
  190         } else {
  191                 g_free(ep);
  192         }
  193         g_pending_events--;
  194         if (g_pending_events == 0)
  195                 wakeup(&g_pending_events);
  196         g_topology_unlock();
  197         return (1);
  198 }
  199 
  200 void
  201 g_run_events()
  202 {
  203         int i;
  204 
  205         while (one_event())
  206                 ;
  207         g_topology_lock();
  208         i = g_wither_work;
  209         while (i) {
  210                 i = g_wither_washer();
  211                 g_wither_work = i & 1;
  212                 i &= 2;
  213         }
  214         g_topology_unlock();
  215 }
  216 
  217 void
  218 g_cancel_event(void *ref)
  219 {
  220         struct g_event *ep, *epn;
  221         struct g_provider *pp;
  222         u_int n;
  223 
  224         mtx_lock(&g_eventlock);
  225         TAILQ_FOREACH(pp, &g_doorstep, orphan) {
  226                 if (pp != ref)
  227                         continue;
  228                 TAILQ_REMOVE(&g_doorstep, pp, orphan);
  229                 break;
  230         }
  231         for (ep = TAILQ_FIRST(&g_events); ep != NULL; ep = epn) {
  232                 epn = TAILQ_NEXT(ep, events);
  233                 for (n = 0; n < G_N_EVENTREFS; n++) {
  234                         if (ep->ref[n] == NULL)
  235                                 break;
  236                         if (ep->ref[n] == ref) {
  237                                 TAILQ_REMOVE(&g_events, ep, events);
  238                                 ep->func(ep->arg, EV_CANCEL);
  239                                 if (ep->flag & EV_WAKEUP) {
  240                                         ep->flag |= EV_DONE;
  241                                         ep->flag |= EV_CANCELED;
  242                                         wakeup(ep);
  243                                 } else {
  244                                         g_free(ep);
  245                                 }
  246                                 if (--g_pending_events == 0)
  247                                         wakeup(&g_pending_events);
  248                                 break;
  249                         }
  250                 }
  251         }
  252         mtx_unlock(&g_eventlock);
  253 }
  254 
  255 static int
  256 g_post_event_x(g_event_t *func, void *arg, int flag, int wuflag, struct g_event **epp, va_list ap)
  257 {
  258         struct g_event *ep;
  259         void *p;
  260         u_int n;
  261 
  262         g_trace(G_T_TOPOLOGY, "g_post_event_x(%p, %p, %d, %d)",
  263             func, arg, flag, wakeup);
  264         KASSERT(wuflag == 0 || wuflag == EV_WAKEUP,
  265             ("Wrong wuflag in g_post_event_x(0x%x)", wuflag));
  266         ep = g_malloc(sizeof *ep, flag | M_ZERO);
  267         if (ep == NULL)
  268                 return (ENOMEM);
  269         ep->flag = wuflag;
  270         for (n = 0; n < G_N_EVENTREFS; n++) {
  271                 p = va_arg(ap, void *);
  272                 if (p == NULL)
  273                         break;
  274                 g_trace(G_T_TOPOLOGY, "  ref %p", p);
  275                 ep->ref[n] = p;
  276         }
  277         KASSERT(p == NULL, ("Too many references to event"));
  278         ep->func = func;
  279         ep->arg = arg;
  280         mtx_lock(&g_eventlock);
  281         g_pending_events++;
  282         TAILQ_INSERT_TAIL(&g_events, ep, events);
  283         mtx_unlock(&g_eventlock);
  284         wakeup(&g_wait_event);
  285         if (epp != NULL)
  286                 *epp = ep;
  287         curthread->td_pflags |= TDP_GEOM;
  288         return (0);
  289 }
  290 
  291 int
  292 g_post_event(g_event_t *func, void *arg, int flag, ...)
  293 {
  294         va_list ap;
  295         int i;
  296 
  297         KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
  298             ("Wrong flag to g_post_event"));
  299         va_start(ap, flag);
  300         i = g_post_event_x(func, arg, flag, 0, NULL, ap);
  301         va_end(ap);
  302         return (i);
  303 }
  304 
  305 void
  306 g_do_wither() {
  307 
  308         g_wither_work = 1;
  309         wakeup(&g_wait_event);
  310 }
  311 
  312 /*
  313  * XXX: It might actually be useful to call this function with topology held.
  314  * XXX: This would ensure that the event gets created before anything else
  315  * XXX: changes.  At present all users have a handle on things in some other
  316  * XXX: way, so this remains an XXX for now.
  317  */
  318 
  319 int
  320 g_waitfor_event(g_event_t *func, void *arg, int flag, ...)
  321 {
  322         va_list ap;
  323         struct g_event *ep;
  324         int error;
  325 
  326         g_topology_assert_not();
  327         KASSERT(flag == M_WAITOK || flag == M_NOWAIT,
  328             ("Wrong flag to g_post_event"));
  329         va_start(ap, flag);
  330         error = g_post_event_x(func, arg, flag, EV_WAKEUP, &ep, ap);
  331         va_end(ap);
  332         if (error)
  333                 return (error);
  334         do 
  335                 tsleep(ep, PRIBIO, "g_waitfor_event", hz);
  336         while (!(ep->flag & EV_DONE));
  337         if (ep->flag & EV_CANCELED)
  338                 error = EAGAIN;
  339         g_free(ep);
  340         return (error);
  341 }
  342 
  343 void
  344 g_event_init()
  345 {
  346 
  347         mtx_init(&g_eventlock, "GEOM orphanage", NULL, MTX_DEF);
  348 }

Cache object: b2767cc1d50214c5976be1309a259d4b


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