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_subr.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 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD: releng/5.4/sys/geom/geom_subr.c 145335 2005-04-20 19:11:07Z cvs2svn $");
   38 
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/devicestat.h>
   42 #include <sys/kernel.h>
   43 #include <sys/malloc.h>
   44 #include <sys/bio.h>
   45 #include <sys/sysctl.h>
   46 #include <sys/proc.h>
   47 #include <sys/kthread.h>
   48 #include <sys/lock.h>
   49 #include <sys/mutex.h>
   50 #include <sys/errno.h>
   51 #include <sys/sbuf.h>
   52 #include <geom/geom.h>
   53 #include <geom/geom_int.h>
   54 #include <machine/stdarg.h>
   55 
   56 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
   57 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
   58 char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
   59 
   60 struct g_hh00 {
   61         struct g_class  *mp;
   62         int             error;
   63 };
   64 
   65 /*
   66  * This event offers a new class a chance to taste all preexisting providers.
   67  */
   68 static void
   69 g_load_class(void *arg, int flag)
   70 {
   71         struct g_hh00 *hh;
   72         struct g_class *mp2, *mp;
   73         struct g_geom *gp;
   74         struct g_provider *pp;
   75 
   76         g_topology_assert();
   77         if (flag == EV_CANCEL)  /* XXX: can't happen ? */
   78                 return;
   79         if (g_shutdown)
   80                 return;
   81 
   82         hh = arg;
   83         mp = hh->mp;
   84         g_free(hh);
   85         g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
   86         KASSERT(mp->name != NULL && *mp->name != '\0',
   87             ("GEOM class has no name"));
   88         LIST_FOREACH(mp2, &g_classes, class) {
   89                 KASSERT(mp2 != mp,
   90                     ("The GEOM class %s already loaded", mp2->name));
   91                 KASSERT(strcmp(mp2->name, mp->name) != 0,
   92                     ("A GEOM class named %s is already loaded", mp2->name));
   93         }
   94 
   95         LIST_INIT(&mp->geom);
   96         LIST_INSERT_HEAD(&g_classes, mp, class);
   97         if (mp->init != NULL)
   98                 mp->init(mp);
   99         if (mp->taste == NULL)
  100                 return;
  101         LIST_FOREACH(mp2, &g_classes, class) {
  102                 if (mp == mp2)
  103                         continue;
  104                 LIST_FOREACH(gp, &mp2->geom, geom) {
  105                         LIST_FOREACH(pp, &gp->provider, provider) {
  106                                 mp->taste(mp, pp, 0);
  107                                 g_topology_assert();
  108                         }
  109                 }
  110         }
  111 }
  112 
  113 static void
  114 g_unload_class(void *arg, int flag)
  115 {
  116         struct g_hh00 *hh;
  117         struct g_class *mp;
  118         struct g_geom *gp;
  119         struct g_provider *pp;
  120         struct g_consumer *cp;
  121         int error;
  122 
  123         g_topology_assert();
  124         hh = arg;
  125         mp = hh->mp;
  126         G_VALID_CLASS(mp);
  127         g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
  128 
  129         /*
  130          * We allow unloading if we have no geoms, or a class
  131          * method we can use to get rid of them.
  132          */
  133         if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
  134                 hh->error = EOPNOTSUPP;
  135                 return;
  136         }
  137 
  138         /* We refuse to unload if anything is open */
  139         LIST_FOREACH(gp, &mp->geom, geom) {
  140                 LIST_FOREACH(pp, &gp->provider, provider)
  141                         if (pp->acr || pp->acw || pp->ace) {
  142                                 hh->error = EBUSY;
  143                                 return;
  144                         }
  145                 LIST_FOREACH(cp, &gp->consumer, consumer)
  146                         if (cp->acr || cp->acw || cp->ace) {
  147                                 hh->error = EBUSY;
  148                                 return;
  149                         }
  150         }
  151 
  152         /* Bar new entries */
  153         mp->taste = NULL;
  154         mp->config = NULL;
  155 
  156         error = 0;
  157         for (;;) {
  158                 gp = LIST_FIRST(&mp->geom);
  159                 if (gp == NULL)
  160                         break;
  161                 error = mp->destroy_geom(NULL, mp, gp);
  162                 if (error != 0)
  163                         break;
  164         }
  165         if (error == 0) {
  166                 if (mp->fini != NULL)
  167                         mp->fini(mp);
  168                 LIST_REMOVE(mp, class);
  169         }
  170         hh->error = error;
  171         return;
  172 }
  173 
  174 int
  175 g_modevent(module_t mod, int type, void *data)
  176 {
  177         struct g_hh00 *hh;
  178         int error;
  179         static int g_ignition;
  180         struct g_class *mp;
  181 
  182         mp = data;
  183         if (mp->version != G_VERSION) {
  184                 printf("GEOM class %s has Wrong version %x\n",
  185                     mp->name, mp->version);
  186                 return (EINVAL);
  187         }
  188         if (!g_ignition) {
  189                 g_ignition++;
  190                 g_init();
  191         }
  192         hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
  193         hh->mp = data;
  194         error = EOPNOTSUPP;
  195         switch (type) {
  196         case MOD_LOAD:
  197                 g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name);
  198                 g_post_event(g_load_class, hh, M_WAITOK, NULL);
  199                 error = 0;
  200                 break;
  201         case MOD_UNLOAD:
  202                 g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name);
  203                 error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL);
  204                 if (error == 0)
  205                         error = hh->error;
  206                 if (error == 0) {
  207                         KASSERT(LIST_EMPTY(&hh->mp->geom),
  208                             ("Unloaded class (%s) still has geom", hh->mp->name));
  209                 }
  210                 g_free(hh);
  211                 break;
  212         default:
  213                 g_free(hh);
  214                 break;
  215         }
  216         return (error);
  217 }
  218 
  219 struct g_geom *
  220 g_new_geomf(struct g_class *mp, const char *fmt, ...)
  221 {
  222         struct g_geom *gp;
  223         va_list ap;
  224         struct sbuf *sb;
  225 
  226         g_topology_assert();
  227         G_VALID_CLASS(mp);
  228         sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
  229         va_start(ap, fmt);
  230         sbuf_vprintf(sb, fmt, ap);
  231         va_end(ap);
  232         sbuf_finish(sb);
  233         gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
  234         gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
  235         gp->class = mp;
  236         gp->rank = 1;
  237         LIST_INIT(&gp->consumer);
  238         LIST_INIT(&gp->provider);
  239         LIST_INSERT_HEAD(&mp->geom, gp, geom);
  240         TAILQ_INSERT_HEAD(&geoms, gp, geoms);
  241         strcpy(gp->name, sbuf_data(sb));
  242         sbuf_delete(sb);
  243         /* Fill in defaults from class */
  244         gp->start = mp->start;
  245         gp->spoiled = mp->spoiled;
  246         gp->dumpconf = mp->dumpconf;
  247         gp->access = mp->access;
  248         gp->orphan = mp->orphan;
  249         gp->ioctl = mp->ioctl;
  250         return (gp);
  251 }
  252 
  253 void
  254 g_destroy_geom(struct g_geom *gp)
  255 {
  256 
  257         g_topology_assert();
  258         G_VALID_GEOM(gp);
  259         g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
  260         KASSERT(LIST_EMPTY(&gp->consumer),
  261             ("g_destroy_geom(%s) with consumer(s) [%p]",
  262             gp->name, LIST_FIRST(&gp->consumer)));
  263         KASSERT(LIST_EMPTY(&gp->provider),
  264             ("g_destroy_geom(%s) with provider(s) [%p]",
  265             gp->name, LIST_FIRST(&gp->provider)));
  266         g_cancel_event(gp);
  267         LIST_REMOVE(gp, geom);
  268         TAILQ_REMOVE(&geoms, gp, geoms);
  269         g_free(gp->name);
  270         g_free(gp);
  271 }
  272 
  273 /*
  274  * This function is called (repeatedly) until the has withered away.
  275  */
  276 void
  277 g_wither_geom(struct g_geom *gp, int error)
  278 {
  279         struct g_provider *pp;
  280 
  281         g_topology_assert();
  282         G_VALID_GEOM(gp);
  283         g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
  284         if (!(gp->flags & G_GEOM_WITHER)) {
  285                 gp->flags |= G_GEOM_WITHER;
  286                 LIST_FOREACH(pp, &gp->provider, provider)
  287                         if (!(pp->flags & G_PF_ORPHAN))
  288                                 g_orphan_provider(pp, error);
  289         }
  290         g_do_wither();
  291 }
  292 
  293 /*
  294  * This function is called (repeatedly) until we cant wash away more
  295  * withered bits at present.  Return value contains two bits.  Bit 0
  296  * set means "withering stuff we can't wash now", bit 1 means "call
  297  * me again, there may be stuff I didn't get the first time around.
  298  */
  299 int
  300 g_wither_washer()
  301 {
  302         struct g_class *mp;
  303         struct g_geom *gp, *gp2;
  304         struct g_provider *pp, *pp2;
  305         struct g_consumer *cp, *cp2;
  306         int result;
  307 
  308         result = 0;
  309         g_topology_assert();
  310         LIST_FOREACH(mp, &g_classes, class) {
  311                 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
  312                         LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
  313                                 if (!(pp->flags & G_PF_WITHER))
  314                                         continue;
  315                                 if (LIST_EMPTY(&pp->consumers))
  316                                         g_destroy_provider(pp);
  317                                 else
  318                                         result |= 1;
  319                         }
  320                         if (!(gp->flags & G_GEOM_WITHER))
  321                                 continue;
  322                         LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
  323                                 if (LIST_EMPTY(&pp->consumers))
  324                                         g_destroy_provider(pp);
  325                                 else
  326                                         result |= 1;
  327                         }
  328                         LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
  329                                 if (cp->acr || cp->acw || cp->ace) {
  330                                         result |= 1;
  331                                         continue;
  332                                 }
  333                                 if (cp->provider != NULL)
  334                                         g_detach(cp);
  335                                 g_destroy_consumer(cp);
  336                                 result |= 2;
  337                         }
  338                         if (LIST_EMPTY(&gp->provider) &&
  339                             LIST_EMPTY(&gp->consumer))
  340                                 g_destroy_geom(gp);
  341                         else
  342                                 result |= 1;
  343                 }
  344         }
  345         return (result);
  346 }
  347 
  348 struct g_consumer *
  349 g_new_consumer(struct g_geom *gp)
  350 {
  351         struct g_consumer *cp;
  352 
  353         g_topology_assert();
  354         G_VALID_GEOM(gp);
  355         KASSERT(!(gp->flags & G_GEOM_WITHER),
  356             ("g_new_consumer on WITHERing geom(%s) (class %s)",
  357             gp->name, gp->class->name));
  358         KASSERT(gp->orphan != NULL,
  359             ("g_new_consumer on geom(%s) (class %s) without orphan",
  360             gp->name, gp->class->name));
  361 
  362         cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
  363         cp->geom = gp;
  364         cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
  365             DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
  366         LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
  367         return(cp);
  368 }
  369 
  370 void
  371 g_destroy_consumer(struct g_consumer *cp)
  372 {
  373         struct g_geom *gp;
  374 
  375         g_topology_assert();
  376         G_VALID_CONSUMER(cp);
  377         g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
  378         KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
  379         KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
  380         KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
  381         KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
  382         g_cancel_event(cp);
  383         gp = cp->geom;
  384         LIST_REMOVE(cp, consumer);
  385         devstat_remove_entry(cp->stat);
  386         g_free(cp);
  387         if (gp->flags & G_GEOM_WITHER)
  388                 g_do_wither();
  389 }
  390 
  391 static void
  392 g_new_provider_event(void *arg, int flag)
  393 {
  394         struct g_class *mp;
  395         struct g_provider *pp;
  396         struct g_consumer *cp;
  397         int i;
  398 
  399         g_topology_assert();
  400         if (flag == EV_CANCEL)
  401                 return;
  402         if (g_shutdown)
  403                 return;
  404         pp = arg;
  405         G_VALID_PROVIDER(pp);
  406         LIST_FOREACH(mp, &g_classes, class) {
  407                 if (mp->taste == NULL)
  408                         continue;
  409                 i = 1;
  410                 LIST_FOREACH(cp, &pp->consumers, consumers)
  411                         if (cp->geom->class == mp)
  412                                 i = 0;
  413                 if (!i)
  414                         continue;
  415                 mp->taste(mp, pp, 0);
  416                 g_topology_assert();
  417         }
  418 }
  419 
  420 
  421 struct g_provider *
  422 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
  423 {
  424         struct g_provider *pp;
  425         struct sbuf *sb;
  426         va_list ap;
  427 
  428         g_topology_assert();
  429         G_VALID_GEOM(gp);
  430         KASSERT(gp->access != NULL,
  431             ("new provider on geom(%s) without ->access (class %s)",
  432             gp->name, gp->class->name));
  433         KASSERT(gp->start != NULL,
  434             ("new provider on geom(%s) without ->start (class %s)",
  435             gp->name, gp->class->name));
  436         KASSERT(!(gp->flags & G_GEOM_WITHER),
  437             ("new provider on WITHERing geom(%s) (class %s)",
  438             gp->name, gp->class->name));
  439         sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
  440         va_start(ap, fmt);
  441         sbuf_vprintf(sb, fmt, ap);
  442         va_end(ap);
  443         sbuf_finish(sb);
  444         pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
  445         pp->name = (char *)(pp + 1);
  446         strcpy(pp->name, sbuf_data(sb));
  447         sbuf_delete(sb);
  448         LIST_INIT(&pp->consumers);
  449         pp->error = ENXIO;
  450         pp->geom = gp;
  451         pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
  452             DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
  453         LIST_INSERT_HEAD(&gp->provider, pp, provider);
  454         g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
  455         return (pp);
  456 }
  457 
  458 void
  459 g_error_provider(struct g_provider *pp, int error)
  460 {
  461 
  462         /* G_VALID_PROVIDER(pp);  We may not have g_topology */
  463         pp->error = error;
  464 }
  465 
  466 struct g_provider *
  467 g_provider_by_name(char const *arg)
  468 {
  469         struct g_class *cp;
  470         struct g_geom *gp;
  471         struct g_provider *pp;
  472 
  473         LIST_FOREACH(cp, &g_classes, class) {
  474                 LIST_FOREACH(gp, &cp->geom, geom) {
  475                         LIST_FOREACH(pp, &gp->provider, provider) {
  476                                 if (!strcmp(arg, pp->name))
  477                                         return (pp);
  478                         }
  479                 }
  480         }
  481         return (NULL);
  482 }
  483 
  484 void
  485 g_destroy_provider(struct g_provider *pp)
  486 {
  487         struct g_geom *gp;
  488 
  489         g_topology_assert();
  490         G_VALID_PROVIDER(pp);
  491         KASSERT(LIST_EMPTY(&pp->consumers),
  492             ("g_destroy_provider but attached"));
  493         KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
  494         KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
  495         KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
  496         g_cancel_event(pp);
  497         LIST_REMOVE(pp, provider);
  498         gp = pp->geom;
  499         devstat_remove_entry(pp->stat);
  500         g_free(pp);
  501         if ((gp->flags & G_GEOM_WITHER))
  502                 g_do_wither();
  503 }
  504 
  505 /*
  506  * We keep the "geoms" list sorted by topological order (== increasing
  507  * numerical rank) at all times.
  508  * When an attach is done, the attaching geoms rank is invalidated
  509  * and it is moved to the tail of the list.
  510  * All geoms later in the sequence has their ranks reevaluated in
  511  * sequence.  If we cannot assign rank to a geom because it's
  512  * prerequisites do not have rank, we move that element to the tail
  513  * of the sequence with invalid rank as well.
  514  * At some point we encounter our original geom and if we stil fail
  515  * to assign it a rank, there must be a loop and we fail back to
  516  * g_attach() which detach again and calls redo_rank again
  517  * to fix up the damage.
  518  * It would be much simpler code wise to do it recursively, but we
  519  * can't risk that on the kernel stack.
  520  */
  521 
  522 static int
  523 redo_rank(struct g_geom *gp)
  524 {
  525         struct g_consumer *cp;
  526         struct g_geom *gp1, *gp2;
  527         int n, m;
  528 
  529         g_topology_assert();
  530         G_VALID_GEOM(gp);
  531 
  532         /* Invalidate this geoms rank and move it to the tail */
  533         gp1 = TAILQ_NEXT(gp, geoms);
  534         if (gp1 != NULL) {
  535                 gp->rank = 0;
  536                 TAILQ_REMOVE(&geoms, gp, geoms);
  537                 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
  538         } else {
  539                 gp1 = gp;
  540         }
  541 
  542         /* re-rank the rest of the sequence */
  543         for (; gp1 != NULL; gp1 = gp2) {
  544                 gp1->rank = 0;
  545                 m = 1;
  546                 LIST_FOREACH(cp, &gp1->consumer, consumer) {
  547                         if (cp->provider == NULL)
  548                                 continue;
  549                         n = cp->provider->geom->rank;
  550                         if (n == 0) {
  551                                 m = 0;
  552                                 break;
  553                         } else if (n >= m)
  554                                 m = n + 1;
  555                 }
  556                 gp1->rank = m;
  557                 gp2 = TAILQ_NEXT(gp1, geoms);
  558 
  559                 /* got a rank, moving on */
  560                 if (m != 0)
  561                         continue;
  562 
  563                 /* no rank to original geom means loop */
  564                 if (gp == gp1) 
  565                         return (ELOOP);
  566 
  567                 /* no rank, put it at the end move on */
  568                 TAILQ_REMOVE(&geoms, gp1, geoms);
  569                 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
  570         }
  571         return (0);
  572 }
  573 
  574 int
  575 g_attach(struct g_consumer *cp, struct g_provider *pp)
  576 {
  577         int error;
  578 
  579         g_topology_assert();
  580         G_VALID_CONSUMER(cp);
  581         G_VALID_PROVIDER(pp);
  582         KASSERT(cp->provider == NULL, ("attach but attached"));
  583         cp->provider = pp;
  584         LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
  585         error = redo_rank(cp->geom);
  586         if (error) {
  587                 LIST_REMOVE(cp, consumers);
  588                 cp->provider = NULL;
  589                 redo_rank(cp->geom);
  590         }
  591         return (error);
  592 }
  593 
  594 void
  595 g_detach(struct g_consumer *cp)
  596 {
  597         struct g_provider *pp;
  598 
  599         g_topology_assert();
  600         G_VALID_CONSUMER(cp);
  601         g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
  602         KASSERT(cp->provider != NULL, ("detach but not attached"));
  603         KASSERT(cp->acr == 0, ("detach but nonzero acr"));
  604         KASSERT(cp->acw == 0, ("detach but nonzero acw"));
  605         KASSERT(cp->ace == 0, ("detach but nonzero ace"));
  606         KASSERT(cp->nstart == cp->nend,
  607             ("detach with active requests"));
  608         pp = cp->provider;
  609         LIST_REMOVE(cp, consumers);
  610         cp->provider = NULL;
  611         if (pp->geom->flags & G_GEOM_WITHER)
  612                 g_do_wither();
  613         else if (pp->flags & G_PF_WITHER)
  614                 g_do_wither();
  615         redo_rank(cp->geom);
  616 }
  617 
  618 /*
  619  * g_access()
  620  *
  621  * Access-check with delta values.  The question asked is "can provider
  622  * "cp" change the access counters by the relative amounts dc[rwe] ?"
  623  */
  624 
  625 int
  626 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
  627 {
  628         struct g_provider *pp;
  629         int pr,pw,pe;
  630         int error;
  631 
  632         g_topology_assert();
  633         G_VALID_CONSUMER(cp);
  634         pp = cp->provider;
  635         KASSERT(pp != NULL, ("access but not attached"));
  636         G_VALID_PROVIDER(pp);
  637 
  638         g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
  639             cp, pp->name, dcr, dcw, dce);
  640 
  641         KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
  642         KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
  643         KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
  644         KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
  645         KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
  646 
  647         /*
  648          * If our class cares about being spoiled, and we have been, we
  649          * are probably just ahead of the event telling us that.  Fail
  650          * now rather than having to unravel this later.
  651          */
  652         if (cp->geom->spoiled != NULL && cp->spoiled &&
  653             (dcr > 0 || dcw > 0 || dce > 0))
  654                 return (ENXIO);
  655 
  656         /*
  657          * Figure out what counts the provider would have had, if this
  658          * consumer had (r0w0e0) at this time.
  659          */
  660         pr = pp->acr - cp->acr;
  661         pw = pp->acw - cp->acw;
  662         pe = pp->ace - cp->ace;
  663 
  664         g_trace(G_T_ACCESS,
  665     "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
  666             dcr, dcw, dce,
  667             cp->acr, cp->acw, cp->ace,
  668             pp->acr, pp->acw, pp->ace,
  669             pp, pp->name);
  670 
  671         /* If foot-shooting is enabled, any open on rank#1 is OK */
  672         if ((g_debugflags & 16) && pp->geom->rank == 1)
  673                 ;
  674         /* If we try exclusive but already write: fail */
  675         else if (dce > 0 && pw > 0)
  676                 return (EPERM);
  677         /* If we try write but already exclusive: fail */
  678         else if (dcw > 0 && pe > 0)
  679                 return (EPERM);
  680         /* If we try to open more but provider is error'ed: fail */
  681         else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
  682                 return (pp->error);
  683 
  684         /* Ok then... */
  685 
  686         error = pp->geom->access(pp, dcr, dcw, dce);
  687         KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
  688             ("Geom provider %s::%s failed closing ->access()",
  689             pp->geom->class->name, pp->name));
  690         if (!error) {
  691                 /*
  692                  * If we open first write, spoil any partner consumers.
  693                  * If we close last write, trigger re-taste.
  694                  */
  695                 if (pp->acw == 0 && dcw != 0)
  696                         g_spoil(pp, cp);
  697                 else if (pp->acw != 0 && pp->acw == -dcw && 
  698                     !(pp->geom->flags & G_GEOM_WITHER))
  699                         g_post_event(g_new_provider_event, pp, M_WAITOK, 
  700                             pp, NULL);
  701 
  702                 pp->acr += dcr;
  703                 pp->acw += dcw;
  704                 pp->ace += dce;
  705                 cp->acr += dcr;
  706                 cp->acw += dcw;
  707                 cp->ace += dce;
  708                 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
  709                         KASSERT(pp->sectorsize > 0,
  710                             ("Provider %s lacks sectorsize", pp->name));
  711         }
  712         return (error);
  713 }
  714 
  715 int
  716 g_handleattr_int(struct bio *bp, const char *attribute, int val)
  717 {
  718 
  719         return (g_handleattr(bp, attribute, &val, sizeof val));
  720 }
  721 
  722 int
  723 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
  724 {
  725 
  726         return (g_handleattr(bp, attribute, &val, sizeof val));
  727 }
  728 
  729 int
  730 g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
  731 {
  732         int error;
  733 
  734         if (strcmp(bp->bio_attribute, attribute))
  735                 return (0);
  736         if (bp->bio_length != len) {
  737                 printf("bio_length %jd len %d -> EFAULT\n",
  738                     (intmax_t)bp->bio_length, len);
  739                 error = EFAULT;
  740         } else {
  741                 error = 0;
  742                 bcopy(val, bp->bio_data, len);
  743                 bp->bio_completed = len;
  744         }
  745         g_io_deliver(bp, error);
  746         return (1);
  747 }
  748 
  749 int
  750 g_std_access(struct g_provider *pp,
  751         int dr __unused, int dw __unused, int de __unused)
  752 {
  753 
  754         g_topology_assert();
  755         G_VALID_PROVIDER(pp);
  756         return (0);
  757 }
  758 
  759 void
  760 g_std_done(struct bio *bp)
  761 {
  762         struct bio *bp2;
  763 
  764         bp2 = bp->bio_parent;
  765         if (bp2->bio_error == 0)
  766                 bp2->bio_error = bp->bio_error;
  767         bp2->bio_completed += bp->bio_completed;
  768         g_destroy_bio(bp);
  769         bp2->bio_inbed++;
  770         if (bp2->bio_children == bp2->bio_inbed)
  771                 g_io_deliver(bp2, bp2->bio_error);
  772 }
  773 
  774 /* XXX: maybe this is only g_slice_spoiled */
  775 
  776 void
  777 g_std_spoiled(struct g_consumer *cp)
  778 {
  779         struct g_geom *gp;
  780         struct g_provider *pp;
  781 
  782         g_topology_assert();
  783         G_VALID_CONSUMER(cp);
  784         g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
  785         g_detach(cp);
  786         gp = cp->geom;
  787         LIST_FOREACH(pp, &gp->provider, provider)
  788                 g_orphan_provider(pp, ENXIO);
  789         g_destroy_consumer(cp);
  790         if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
  791                 g_destroy_geom(gp);
  792         else
  793                 gp->flags |= G_GEOM_WITHER;
  794 }
  795 
  796 /*
  797  * Spoiling happens when a provider is opened for writing, but consumers
  798  * which are configured by in-band data are attached (slicers for instance).
  799  * Since the write might potentially change the in-band data, such consumers
  800  * need to re-evaluate their existence after the writing session closes.
  801  * We do this by (offering to) tear them down when the open for write happens
  802  * in return for a re-taste when it closes again.
  803  * Together with the fact that such consumers grab an 'e' bit whenever they
  804  * are open, regardless of mode, this ends up DTRT.
  805  */
  806 
  807 static void
  808 g_spoil_event(void *arg, int flag)
  809 {
  810         struct g_provider *pp;
  811         struct g_consumer *cp, *cp2;
  812 
  813         g_topology_assert();
  814         if (flag == EV_CANCEL)
  815                 return;
  816         pp = arg;
  817         G_VALID_PROVIDER(pp);
  818         for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
  819                 cp2 = LIST_NEXT(cp, consumers);
  820                 if (!cp->spoiled)
  821                         continue;
  822                 cp->spoiled = 0;
  823                 if (cp->geom->spoiled == NULL)
  824                         continue;
  825                 cp->geom->spoiled(cp);
  826                 g_topology_assert();
  827         }
  828 }
  829 
  830 void
  831 g_spoil(struct g_provider *pp, struct g_consumer *cp)
  832 {
  833         struct g_consumer *cp2;
  834 
  835         g_topology_assert();
  836         G_VALID_PROVIDER(pp);
  837         G_VALID_CONSUMER(cp);
  838 
  839         LIST_FOREACH(cp2, &pp->consumers, consumers) {
  840                 if (cp2 == cp)
  841                         continue;
  842 /*
  843                 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
  844                 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
  845 */
  846                 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
  847                 cp2->spoiled++;
  848         }
  849         g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
  850 }
  851 
  852 int
  853 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
  854 {
  855         int error, i;
  856 
  857         i = len;
  858         error = g_io_getattr(attr, cp, &i, var);
  859         if (error)
  860                 return (error);
  861         if (i != len)
  862                 return (EINVAL);
  863         return (0);
  864 }
  865 
  866 #ifdef DIAGNOSTIC
  867 /*
  868  * This function walks (topologically unsafely) the mesh and return a
  869  * non-zero integer if it finds the argument pointer is an object.
  870  * The return value indicates which type of object it is belived to be.
  871  * If topology is not locked, this function is potentially dangerous,
  872  * but since it is for debugging purposes and can be useful for instance
  873  * from DDB, we do not assert topology lock is held.
  874  */
  875 int
  876 g_valid_obj(void const *ptr)
  877 {
  878         struct g_class *mp;
  879         struct g_geom *gp;
  880         struct g_consumer *cp;
  881         struct g_provider *pp;
  882 
  883         LIST_FOREACH(mp, &g_classes, class) {
  884                 if (ptr == mp)
  885                         return (1);
  886                 LIST_FOREACH(gp, &mp->geom, geom) {
  887                         if (ptr == gp)
  888                                 return (2);
  889                         LIST_FOREACH(cp, &gp->consumer, consumer)
  890                                 if (ptr == cp)
  891                                         return (3);
  892                         LIST_FOREACH(pp, &gp->provider, provider)
  893                                 if (ptr == pp)
  894                                         return (4);
  895                 }
  896         }
  897         return(0);
  898 }
  899 #endif

Cache object: 31ac53b17efe479d9a9a33737d6a7da6


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