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/10.4/sys/geom/geom_subr.c 281298 2015-04-09 10:05:58Z mav $");
   38 
   39 #include "opt_ddb.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/devicestat.h>
   44 #include <sys/kernel.h>
   45 #include <sys/malloc.h>
   46 #include <sys/bio.h>
   47 #include <sys/sysctl.h>
   48 #include <sys/proc.h>
   49 #include <sys/kthread.h>
   50 #include <sys/lock.h>
   51 #include <sys/mutex.h>
   52 #include <sys/errno.h>
   53 #include <sys/sbuf.h>
   54 #include <geom/geom.h>
   55 #include <geom/geom_int.h>
   56 #include <machine/stdarg.h>
   57 
   58 #ifdef DDB
   59 #include <ddb/ddb.h>
   60 #endif
   61 
   62 #ifdef KDB
   63 #include <sys/kdb.h>
   64 #endif
   65 
   66 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
   67 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
   68 char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
   69 
   70 struct g_hh00 {
   71         struct g_class          *mp;
   72         struct g_provider       *pp;
   73         off_t                   size;
   74         int                     error;
   75         int                     post;
   76 };
   77 
   78 /*
   79  * This event offers a new class a chance to taste all preexisting providers.
   80  */
   81 static void
   82 g_load_class(void *arg, int flag)
   83 {
   84         struct g_hh00 *hh;
   85         struct g_class *mp2, *mp;
   86         struct g_geom *gp;
   87         struct g_provider *pp;
   88 
   89         g_topology_assert();
   90         if (flag == EV_CANCEL)  /* XXX: can't happen ? */
   91                 return;
   92         if (g_shutdown)
   93                 return;
   94 
   95         hh = arg;
   96         mp = hh->mp;
   97         hh->error = 0;
   98         if (hh->post) {
   99                 g_free(hh);
  100                 hh = NULL;
  101         }
  102         g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
  103         KASSERT(mp->name != NULL && *mp->name != '\0',
  104             ("GEOM class has no name"));
  105         LIST_FOREACH(mp2, &g_classes, class) {
  106                 if (mp2 == mp) {
  107                         printf("The GEOM class %s is already loaded.\n",
  108                             mp2->name);
  109                         if (hh != NULL)
  110                                 hh->error = EEXIST;
  111                         return;
  112                 } else if (strcmp(mp2->name, mp->name) == 0) {
  113                         printf("A GEOM class %s is already loaded.\n",
  114                             mp2->name);
  115                         if (hh != NULL)
  116                                 hh->error = EEXIST;
  117                         return;
  118                 }
  119         }
  120 
  121         LIST_INIT(&mp->geom);
  122         LIST_INSERT_HEAD(&g_classes, mp, class);
  123         if (mp->init != NULL)
  124                 mp->init(mp);
  125         if (mp->taste == NULL)
  126                 return;
  127         LIST_FOREACH(mp2, &g_classes, class) {
  128                 if (mp == mp2)
  129                         continue;
  130                 LIST_FOREACH(gp, &mp2->geom, geom) {
  131                         LIST_FOREACH(pp, &gp->provider, provider) {
  132                                 mp->taste(mp, pp, 0);
  133                                 g_topology_assert();
  134                         }
  135                 }
  136         }
  137 }
  138 
  139 static int
  140 g_unload_class(struct g_class *mp)
  141 {
  142         struct g_geom *gp;
  143         struct g_provider *pp;
  144         struct g_consumer *cp;
  145         int error;
  146 
  147         g_topology_lock();
  148         g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
  149 retry:
  150         G_VALID_CLASS(mp);
  151         LIST_FOREACH(gp, &mp->geom, geom) {
  152                 /* We refuse to unload if anything is open */
  153                 LIST_FOREACH(pp, &gp->provider, provider)
  154                         if (pp->acr || pp->acw || pp->ace) {
  155                                 g_topology_unlock();
  156                                 return (EBUSY);
  157                         }
  158                 LIST_FOREACH(cp, &gp->consumer, consumer)
  159                         if (cp->acr || cp->acw || cp->ace) {
  160                                 g_topology_unlock();
  161                                 return (EBUSY);
  162                         }
  163                 /* If the geom is withering, wait for it to finish. */
  164                 if (gp->flags & G_GEOM_WITHER) {
  165                         g_topology_sleep(mp, 1);
  166                         goto retry;
  167                 }
  168         }
  169 
  170         /*
  171          * We allow unloading if we have no geoms, or a class
  172          * method we can use to get rid of them.
  173          */
  174         if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
  175                 g_topology_unlock();
  176                 return (EOPNOTSUPP);
  177         }
  178 
  179         /* Bar new entries */
  180         mp->taste = NULL;
  181         mp->config = NULL;
  182 
  183         LIST_FOREACH(gp, &mp->geom, geom) {
  184                 error = mp->destroy_geom(NULL, mp, gp);
  185                 if (error != 0) {
  186                         g_topology_unlock();
  187                         return (error);
  188                 }
  189         }
  190         /* Wait for withering to finish. */
  191         for (;;) {
  192                 gp = LIST_FIRST(&mp->geom);
  193                 if (gp == NULL)
  194                         break;
  195                 KASSERT(gp->flags & G_GEOM_WITHER,
  196                    ("Non-withering geom in class %s", mp->name));
  197                 g_topology_sleep(mp, 1);
  198         }
  199         G_VALID_CLASS(mp);
  200         if (mp->fini != NULL)
  201                 mp->fini(mp);
  202         LIST_REMOVE(mp, class);
  203         g_topology_unlock();
  204 
  205         return (0);
  206 }
  207 
  208 int
  209 g_modevent(module_t mod, int type, void *data)
  210 {
  211         struct g_hh00 *hh;
  212         int error;
  213         static int g_ignition;
  214         struct g_class *mp;
  215 
  216         mp = data;
  217         if (mp->version != G_VERSION) {
  218                 printf("GEOM class %s has Wrong version %x\n",
  219                     mp->name, mp->version);
  220                 return (EINVAL);
  221         }
  222         if (!g_ignition) {
  223                 g_ignition++;
  224                 g_init();
  225         }
  226         error = EOPNOTSUPP;
  227         switch (type) {
  228         case MOD_LOAD:
  229                 g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", mp->name);
  230                 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
  231                 hh->mp = mp;
  232                 /*
  233                  * Once the system is not cold, MOD_LOAD calls will be
  234                  * from the userland and the g_event thread will be able
  235                  * to acknowledge their completion.
  236                  */
  237                 if (cold) {
  238                         hh->post = 1;
  239                         error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
  240                 } else {
  241                         error = g_waitfor_event(g_load_class, hh, M_WAITOK,
  242                             NULL);
  243                         if (error == 0)
  244                                 error = hh->error;
  245                         g_free(hh);
  246                 }
  247                 break;
  248         case MOD_UNLOAD:
  249                 g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", mp->name);
  250                 DROP_GIANT();
  251                 error = g_unload_class(mp);
  252                 PICKUP_GIANT();
  253                 if (error == 0) {
  254                         KASSERT(LIST_EMPTY(&mp->geom),
  255                             ("Unloaded class (%s) still has geom", mp->name));
  256                 }
  257                 break;
  258         }
  259         return (error);
  260 }
  261 
  262 static void
  263 g_retaste_event(void *arg, int flag)
  264 {
  265         struct g_class *mp, *mp2;
  266         struct g_geom *gp;
  267         struct g_hh00 *hh;
  268         struct g_provider *pp;
  269         struct g_consumer *cp;
  270 
  271         g_topology_assert();
  272         if (flag == EV_CANCEL)  /* XXX: can't happen ? */
  273                 return;
  274         if (g_shutdown || g_notaste)
  275                 return;
  276 
  277         hh = arg;
  278         mp = hh->mp;
  279         hh->error = 0;
  280         if (hh->post) {
  281                 g_free(hh);
  282                 hh = NULL;
  283         }
  284         g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name);
  285 
  286         LIST_FOREACH(mp2, &g_classes, class) {
  287                 LIST_FOREACH(gp, &mp2->geom, geom) {
  288                         LIST_FOREACH(pp, &gp->provider, provider) {
  289                                 if (pp->acr || pp->acw || pp->ace)
  290                                         continue;
  291                                 LIST_FOREACH(cp, &pp->consumers, consumers) {
  292                                         if (cp->geom->class == mp &&
  293                                             (cp->flags & G_CF_ORPHAN) == 0)
  294                                                 break;
  295                                 }
  296                                 if (cp != NULL) {
  297                                         cp->flags |= G_CF_ORPHAN;
  298                                         g_wither_geom(cp->geom, ENXIO);
  299                                 }
  300                                 mp->taste(mp, pp, 0);
  301                                 g_topology_assert();
  302                         }
  303                 }
  304         }
  305 }
  306 
  307 int
  308 g_retaste(struct g_class *mp)
  309 {
  310         struct g_hh00 *hh;
  311         int error;
  312 
  313         if (mp->taste == NULL)
  314                 return (EINVAL);
  315 
  316         hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
  317         hh->mp = mp;
  318 
  319         if (cold) {
  320                 hh->post = 1;
  321                 error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL);
  322         } else {
  323                 error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL);
  324                 if (error == 0)
  325                         error = hh->error;
  326                 g_free(hh);
  327         }
  328 
  329         return (error);
  330 }
  331 
  332 struct g_geom *
  333 g_new_geomf(struct g_class *mp, const char *fmt, ...)
  334 {
  335         struct g_geom *gp;
  336         va_list ap;
  337         struct sbuf *sb;
  338 
  339         g_topology_assert();
  340         G_VALID_CLASS(mp);
  341         sb = sbuf_new_auto();
  342         va_start(ap, fmt);
  343         sbuf_vprintf(sb, fmt, ap);
  344         va_end(ap);
  345         sbuf_finish(sb);
  346         gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
  347         gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
  348         gp->class = mp;
  349         gp->rank = 1;
  350         LIST_INIT(&gp->consumer);
  351         LIST_INIT(&gp->provider);
  352         LIST_INSERT_HEAD(&mp->geom, gp, geom);
  353         TAILQ_INSERT_HEAD(&geoms, gp, geoms);
  354         strcpy(gp->name, sbuf_data(sb));
  355         sbuf_delete(sb);
  356         /* Fill in defaults from class */
  357         gp->start = mp->start;
  358         gp->spoiled = mp->spoiled;
  359         gp->attrchanged = mp->attrchanged;
  360         gp->providergone = mp->providergone;
  361         gp->dumpconf = mp->dumpconf;
  362         gp->access = mp->access;
  363         gp->orphan = mp->orphan;
  364         gp->ioctl = mp->ioctl;
  365         gp->resize = mp->resize;
  366         return (gp);
  367 }
  368 
  369 void
  370 g_destroy_geom(struct g_geom *gp)
  371 {
  372 
  373         g_topology_assert();
  374         G_VALID_GEOM(gp);
  375         g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
  376         KASSERT(LIST_EMPTY(&gp->consumer),
  377             ("g_destroy_geom(%s) with consumer(s) [%p]",
  378             gp->name, LIST_FIRST(&gp->consumer)));
  379         KASSERT(LIST_EMPTY(&gp->provider),
  380             ("g_destroy_geom(%s) with provider(s) [%p]",
  381             gp->name, LIST_FIRST(&gp->provider)));
  382         g_cancel_event(gp);
  383         LIST_REMOVE(gp, geom);
  384         TAILQ_REMOVE(&geoms, gp, geoms);
  385         g_free(gp->name);
  386         g_free(gp);
  387 }
  388 
  389 /*
  390  * This function is called (repeatedly) until the geom has withered away.
  391  */
  392 void
  393 g_wither_geom(struct g_geom *gp, int error)
  394 {
  395         struct g_provider *pp;
  396 
  397         g_topology_assert();
  398         G_VALID_GEOM(gp);
  399         g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
  400         if (!(gp->flags & G_GEOM_WITHER)) {
  401                 gp->flags |= G_GEOM_WITHER;
  402                 LIST_FOREACH(pp, &gp->provider, provider)
  403                         if (!(pp->flags & G_PF_ORPHAN))
  404                                 g_orphan_provider(pp, error);
  405         }
  406         g_do_wither();
  407 }
  408 
  409 /*
  410  * Convenience function to destroy a particular provider.
  411  */
  412 void
  413 g_wither_provider(struct g_provider *pp, int error)
  414 {
  415 
  416         pp->flags |= G_PF_WITHER;
  417         if (!(pp->flags & G_PF_ORPHAN))
  418                 g_orphan_provider(pp, error);
  419 }
  420 
  421 /*
  422  * This function is called (repeatedly) until the has withered away.
  423  */
  424 void
  425 g_wither_geom_close(struct g_geom *gp, int error)
  426 {
  427         struct g_consumer *cp;
  428 
  429         g_topology_assert();
  430         G_VALID_GEOM(gp);
  431         g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
  432         LIST_FOREACH(cp, &gp->consumer, consumer)
  433                 if (cp->acr || cp->acw || cp->ace)
  434                         g_access(cp, -cp->acr, -cp->acw, -cp->ace);
  435         g_wither_geom(gp, error);
  436 }
  437 
  438 /*
  439  * This function is called (repeatedly) until we cant wash away more
  440  * withered bits at present.
  441  */
  442 void
  443 g_wither_washer()
  444 {
  445         struct g_class *mp;
  446         struct g_geom *gp, *gp2;
  447         struct g_provider *pp, *pp2;
  448         struct g_consumer *cp, *cp2;
  449 
  450         g_topology_assert();
  451         LIST_FOREACH(mp, &g_classes, class) {
  452                 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
  453                         LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
  454                                 if (!(pp->flags & G_PF_WITHER))
  455                                         continue;
  456                                 if (LIST_EMPTY(&pp->consumers))
  457                                         g_destroy_provider(pp);
  458                         }
  459                         if (!(gp->flags & G_GEOM_WITHER))
  460                                 continue;
  461                         LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
  462                                 if (LIST_EMPTY(&pp->consumers))
  463                                         g_destroy_provider(pp);
  464                         }
  465                         LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
  466                                 if (cp->acr || cp->acw || cp->ace)
  467                                         continue;
  468                                 if (cp->provider != NULL)
  469                                         g_detach(cp);
  470                                 g_destroy_consumer(cp);
  471                         }
  472                         if (LIST_EMPTY(&gp->provider) &&
  473                             LIST_EMPTY(&gp->consumer))
  474                                 g_destroy_geom(gp);
  475                 }
  476         }
  477 }
  478 
  479 struct g_consumer *
  480 g_new_consumer(struct g_geom *gp)
  481 {
  482         struct g_consumer *cp;
  483 
  484         g_topology_assert();
  485         G_VALID_GEOM(gp);
  486         KASSERT(!(gp->flags & G_GEOM_WITHER),
  487             ("g_new_consumer on WITHERing geom(%s) (class %s)",
  488             gp->name, gp->class->name));
  489         KASSERT(gp->orphan != NULL,
  490             ("g_new_consumer on geom(%s) (class %s) without orphan",
  491             gp->name, gp->class->name));
  492 
  493         cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
  494         cp->geom = gp;
  495         cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
  496             DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
  497         LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
  498         return(cp);
  499 }
  500 
  501 void
  502 g_destroy_consumer(struct g_consumer *cp)
  503 {
  504         struct g_geom *gp;
  505 
  506         g_topology_assert();
  507         G_VALID_CONSUMER(cp);
  508         g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
  509         KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
  510         KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
  511         KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
  512         KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
  513         g_cancel_event(cp);
  514         gp = cp->geom;
  515         LIST_REMOVE(cp, consumer);
  516         devstat_remove_entry(cp->stat);
  517         g_free(cp);
  518         if (gp->flags & G_GEOM_WITHER)
  519                 g_do_wither();
  520 }
  521 
  522 static void
  523 g_new_provider_event(void *arg, int flag)
  524 {
  525         struct g_class *mp;
  526         struct g_provider *pp;
  527         struct g_consumer *cp, *next_cp;
  528 
  529         g_topology_assert();
  530         if (flag == EV_CANCEL)
  531                 return;
  532         if (g_shutdown)
  533                 return;
  534         pp = arg;
  535         G_VALID_PROVIDER(pp);
  536         KASSERT(!(pp->flags & G_PF_WITHER),
  537             ("g_new_provider_event but withered"));
  538         LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
  539                 if ((cp->flags & G_CF_ORPHAN) == 0 &&
  540                     cp->geom->attrchanged != NULL)
  541                         cp->geom->attrchanged(cp, "GEOM::media");
  542         }
  543         if (g_notaste)
  544                 return;
  545         LIST_FOREACH(mp, &g_classes, class) {
  546                 if (mp->taste == NULL)
  547                         continue;
  548                 LIST_FOREACH(cp, &pp->consumers, consumers)
  549                         if (cp->geom->class == mp &&
  550                             (cp->flags & G_CF_ORPHAN) == 0)
  551                                 break;
  552                 if (cp != NULL)
  553                         continue;
  554                 mp->taste(mp, pp, 0);
  555                 g_topology_assert();
  556         }
  557 }
  558 
  559 
  560 struct g_provider *
  561 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
  562 {
  563         struct g_provider *pp;
  564         struct sbuf *sb;
  565         va_list ap;
  566 
  567         g_topology_assert();
  568         G_VALID_GEOM(gp);
  569         KASSERT(gp->access != NULL,
  570             ("new provider on geom(%s) without ->access (class %s)",
  571             gp->name, gp->class->name));
  572         KASSERT(gp->start != NULL,
  573             ("new provider on geom(%s) without ->start (class %s)",
  574             gp->name, gp->class->name));
  575         KASSERT(!(gp->flags & G_GEOM_WITHER),
  576             ("new provider on WITHERing geom(%s) (class %s)",
  577             gp->name, gp->class->name));
  578         sb = sbuf_new_auto();
  579         va_start(ap, fmt);
  580         sbuf_vprintf(sb, fmt, ap);
  581         va_end(ap);
  582         sbuf_finish(sb);
  583         pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
  584         pp->name = (char *)(pp + 1);
  585         strcpy(pp->name, sbuf_data(sb));
  586         sbuf_delete(sb);
  587         LIST_INIT(&pp->consumers);
  588         pp->error = ENXIO;
  589         pp->geom = gp;
  590         pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
  591             DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
  592         LIST_INSERT_HEAD(&gp->provider, pp, provider);
  593         g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
  594         return (pp);
  595 }
  596 
  597 void
  598 g_error_provider(struct g_provider *pp, int error)
  599 {
  600 
  601         /* G_VALID_PROVIDER(pp);  We may not have g_topology */
  602         pp->error = error;
  603 }
  604 
  605 static void
  606 g_resize_provider_event(void *arg, int flag)
  607 {
  608         struct g_hh00 *hh;
  609         struct g_class *mp;
  610         struct g_geom *gp;
  611         struct g_provider *pp;
  612         struct g_consumer *cp, *cp2;
  613         off_t size;
  614 
  615         g_topology_assert();
  616         if (g_shutdown)
  617                 return;
  618 
  619         hh = arg;
  620         pp = hh->pp;
  621         size = hh->size;
  622         g_free(hh);
  623 
  624         G_VALID_PROVIDER(pp);
  625         g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
  626 
  627         LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
  628                 gp = cp->geom;
  629                 if (gp->resize == NULL && size < pp->mediasize) {
  630                         cp->flags |= G_CF_ORPHAN;
  631                         cp->geom->orphan(cp);
  632                 }
  633         }
  634 
  635         pp->mediasize = size;
  636         
  637         LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
  638                 gp = cp->geom;
  639                 if (gp->resize != NULL)
  640                         gp->resize(cp);
  641         }
  642 
  643         /*
  644          * After resizing, the previously invalid GEOM class metadata
  645          * might become valid.  This means we should retaste.
  646          */
  647         LIST_FOREACH(mp, &g_classes, class) {
  648                 if (mp->taste == NULL)
  649                         continue;
  650                 LIST_FOREACH(cp, &pp->consumers, consumers)
  651                         if (cp->geom->class == mp &&
  652                             (cp->flags & G_CF_ORPHAN) == 0)
  653                                 break;
  654                 if (cp != NULL)
  655                         continue;
  656                 mp->taste(mp, pp, 0);
  657                 g_topology_assert();
  658         }
  659 }
  660 
  661 void
  662 g_resize_provider(struct g_provider *pp, off_t size)
  663 {
  664         struct g_hh00 *hh;
  665 
  666         G_VALID_PROVIDER(pp);
  667 
  668         if (size == pp->mediasize)
  669                 return;
  670 
  671         hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
  672         hh->pp = pp;
  673         hh->size = size;
  674         g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
  675 }
  676 
  677 #ifndef _PATH_DEV
  678 #define _PATH_DEV       "/dev/"
  679 #endif
  680 
  681 struct g_provider *
  682 g_provider_by_name(char const *arg)
  683 {
  684         struct g_class *cp;
  685         struct g_geom *gp;
  686         struct g_provider *pp, *wpp;
  687 
  688         if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
  689                 arg += sizeof(_PATH_DEV) - 1;
  690 
  691         wpp = NULL;
  692         LIST_FOREACH(cp, &g_classes, class) {
  693                 LIST_FOREACH(gp, &cp->geom, geom) {
  694                         LIST_FOREACH(pp, &gp->provider, provider) {
  695                                 if (strcmp(arg, pp->name) != 0)
  696                                         continue;
  697                                 if ((gp->flags & G_GEOM_WITHER) == 0 &&
  698                                     (pp->flags & G_PF_WITHER) == 0)
  699                                         return (pp);
  700                                 else
  701                                         wpp = pp;
  702                         }
  703                 }
  704         }
  705 
  706         return (wpp);
  707 }
  708 
  709 void
  710 g_destroy_provider(struct g_provider *pp)
  711 {
  712         struct g_geom *gp;
  713 
  714         g_topology_assert();
  715         G_VALID_PROVIDER(pp);
  716         KASSERT(LIST_EMPTY(&pp->consumers),
  717             ("g_destroy_provider but attached"));
  718         KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
  719         KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
  720         KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
  721         g_cancel_event(pp);
  722         LIST_REMOVE(pp, provider);
  723         gp = pp->geom;
  724         devstat_remove_entry(pp->stat);
  725         /*
  726          * If a callback was provided, send notification that the provider
  727          * is now gone.
  728          */
  729         if (gp->providergone != NULL)
  730                 gp->providergone(pp);
  731 
  732         g_free(pp);
  733         if ((gp->flags & G_GEOM_WITHER))
  734                 g_do_wither();
  735 }
  736 
  737 /*
  738  * We keep the "geoms" list sorted by topological order (== increasing
  739  * numerical rank) at all times.
  740  * When an attach is done, the attaching geoms rank is invalidated
  741  * and it is moved to the tail of the list.
  742  * All geoms later in the sequence has their ranks reevaluated in
  743  * sequence.  If we cannot assign rank to a geom because it's
  744  * prerequisites do not have rank, we move that element to the tail
  745  * of the sequence with invalid rank as well.
  746  * At some point we encounter our original geom and if we stil fail
  747  * to assign it a rank, there must be a loop and we fail back to
  748  * g_attach() which detach again and calls redo_rank again
  749  * to fix up the damage.
  750  * It would be much simpler code wise to do it recursively, but we
  751  * can't risk that on the kernel stack.
  752  */
  753 
  754 static int
  755 redo_rank(struct g_geom *gp)
  756 {
  757         struct g_consumer *cp;
  758         struct g_geom *gp1, *gp2;
  759         int n, m;
  760 
  761         g_topology_assert();
  762         G_VALID_GEOM(gp);
  763 
  764         /* Invalidate this geoms rank and move it to the tail */
  765         gp1 = TAILQ_NEXT(gp, geoms);
  766         if (gp1 != NULL) {
  767                 gp->rank = 0;
  768                 TAILQ_REMOVE(&geoms, gp, geoms);
  769                 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
  770         } else {
  771                 gp1 = gp;
  772         }
  773 
  774         /* re-rank the rest of the sequence */
  775         for (; gp1 != NULL; gp1 = gp2) {
  776                 gp1->rank = 0;
  777                 m = 1;
  778                 LIST_FOREACH(cp, &gp1->consumer, consumer) {
  779                         if (cp->provider == NULL)
  780                                 continue;
  781                         n = cp->provider->geom->rank;
  782                         if (n == 0) {
  783                                 m = 0;
  784                                 break;
  785                         } else if (n >= m)
  786                                 m = n + 1;
  787                 }
  788                 gp1->rank = m;
  789                 gp2 = TAILQ_NEXT(gp1, geoms);
  790 
  791                 /* got a rank, moving on */
  792                 if (m != 0)
  793                         continue;
  794 
  795                 /* no rank to original geom means loop */
  796                 if (gp == gp1) 
  797                         return (ELOOP);
  798 
  799                 /* no rank, put it at the end move on */
  800                 TAILQ_REMOVE(&geoms, gp1, geoms);
  801                 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
  802         }
  803         return (0);
  804 }
  805 
  806 int
  807 g_attach(struct g_consumer *cp, struct g_provider *pp)
  808 {
  809         int error;
  810 
  811         g_topology_assert();
  812         G_VALID_CONSUMER(cp);
  813         G_VALID_PROVIDER(pp);
  814         g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
  815         KASSERT(cp->provider == NULL, ("attach but attached"));
  816         cp->provider = pp;
  817         LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
  818         error = redo_rank(cp->geom);
  819         if (error) {
  820                 LIST_REMOVE(cp, consumers);
  821                 cp->provider = NULL;
  822                 redo_rank(cp->geom);
  823         }
  824         return (error);
  825 }
  826 
  827 void
  828 g_detach(struct g_consumer *cp)
  829 {
  830         struct g_provider *pp;
  831 
  832         g_topology_assert();
  833         G_VALID_CONSUMER(cp);
  834         g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
  835         KASSERT(cp->provider != NULL, ("detach but not attached"));
  836         KASSERT(cp->acr == 0, ("detach but nonzero acr"));
  837         KASSERT(cp->acw == 0, ("detach but nonzero acw"));
  838         KASSERT(cp->ace == 0, ("detach but nonzero ace"));
  839         KASSERT(cp->nstart == cp->nend,
  840             ("detach with active requests"));
  841         pp = cp->provider;
  842         LIST_REMOVE(cp, consumers);
  843         cp->provider = NULL;
  844         if ((cp->geom->flags & G_GEOM_WITHER) ||
  845             (pp->geom->flags & G_GEOM_WITHER) ||
  846             (pp->flags & G_PF_WITHER))
  847                 g_do_wither();
  848         redo_rank(cp->geom);
  849 }
  850 
  851 /*
  852  * g_access()
  853  *
  854  * Access-check with delta values.  The question asked is "can provider
  855  * "cp" change the access counters by the relative amounts dc[rwe] ?"
  856  */
  857 
  858 int
  859 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
  860 {
  861         struct g_provider *pp;
  862         int pr,pw,pe;
  863         int error;
  864 
  865         g_topology_assert();
  866         G_VALID_CONSUMER(cp);
  867         pp = cp->provider;
  868         KASSERT(pp != NULL, ("access but not attached"));
  869         G_VALID_PROVIDER(pp);
  870 
  871         g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
  872             cp, pp->name, dcr, dcw, dce);
  873 
  874         KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
  875         KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
  876         KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
  877         KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
  878         KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
  879 
  880         /*
  881          * If our class cares about being spoiled, and we have been, we
  882          * are probably just ahead of the event telling us that.  Fail
  883          * now rather than having to unravel this later.
  884          */
  885         if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
  886             (dcr > 0 || dcw > 0 || dce > 0))
  887                 return (ENXIO);
  888 
  889         /*
  890          * Figure out what counts the provider would have had, if this
  891          * consumer had (r0w0e0) at this time.
  892          */
  893         pr = pp->acr - cp->acr;
  894         pw = pp->acw - cp->acw;
  895         pe = pp->ace - cp->ace;
  896 
  897         g_trace(G_T_ACCESS,
  898     "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
  899             dcr, dcw, dce,
  900             cp->acr, cp->acw, cp->ace,
  901             pp->acr, pp->acw, pp->ace,
  902             pp, pp->name);
  903 
  904         /* If foot-shooting is enabled, any open on rank#1 is OK */
  905         if ((g_debugflags & 16) && pp->geom->rank == 1)
  906                 ;
  907         /* If we try exclusive but already write: fail */
  908         else if (dce > 0 && pw > 0)
  909                 return (EPERM);
  910         /* If we try write but already exclusive: fail */
  911         else if (dcw > 0 && pe > 0)
  912                 return (EPERM);
  913         /* If we try to open more but provider is error'ed: fail */
  914         else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
  915                 return (pp->error);
  916 
  917         /* Ok then... */
  918 
  919         error = pp->geom->access(pp, dcr, dcw, dce);
  920         KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
  921             ("Geom provider %s::%s dcr=%d dcw=%d dce=%d error=%d failed "
  922             "closing ->access()", pp->geom->class->name, pp->name, dcr, dcw,
  923             dce, error));
  924         if (!error) {
  925                 /*
  926                  * If we open first write, spoil any partner consumers.
  927                  * If we close last write and provider is not errored,
  928                  * trigger re-taste.
  929                  */
  930                 if (pp->acw == 0 && dcw != 0)
  931                         g_spoil(pp, cp);
  932                 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
  933                     !(pp->geom->flags & G_GEOM_WITHER))
  934                         g_post_event(g_new_provider_event, pp, M_WAITOK, 
  935                             pp, NULL);
  936 
  937                 pp->acr += dcr;
  938                 pp->acw += dcw;
  939                 pp->ace += dce;
  940                 cp->acr += dcr;
  941                 cp->acw += dcw;
  942                 cp->ace += dce;
  943                 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
  944                         KASSERT(pp->sectorsize > 0,
  945                             ("Provider %s lacks sectorsize", pp->name));
  946                 if ((cp->geom->flags & G_GEOM_WITHER) &&
  947                     cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
  948                         g_do_wither();
  949         }
  950         return (error);
  951 }
  952 
  953 int
  954 g_handleattr_int(struct bio *bp, const char *attribute, int val)
  955 {
  956 
  957         return (g_handleattr(bp, attribute, &val, sizeof val));
  958 }
  959 
  960 int
  961 g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val)
  962 {
  963 
  964         return (g_handleattr(bp, attribute, &val, sizeof val));
  965 }
  966 
  967 int
  968 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
  969 {
  970 
  971         return (g_handleattr(bp, attribute, &val, sizeof val));
  972 }
  973 
  974 int
  975 g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
  976 {
  977 
  978         return (g_handleattr(bp, attribute, str, 0));
  979 }
  980 
  981 int
  982 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
  983 {
  984         int error = 0;
  985 
  986         if (strcmp(bp->bio_attribute, attribute))
  987                 return (0);
  988         if (len == 0) {
  989                 bzero(bp->bio_data, bp->bio_length);
  990                 if (strlcpy(bp->bio_data, val, bp->bio_length) >=
  991                     bp->bio_length) {
  992                         printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
  993                             __func__, bp->bio_to->name,
  994                             (intmax_t)bp->bio_length, strlen(val));
  995                         error = EFAULT;
  996                 }
  997         } else if (bp->bio_length == len) {
  998                 bcopy(val, bp->bio_data, len);
  999         } else {
 1000                 printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
 1001                     bp->bio_to->name, (intmax_t)bp->bio_length, len);
 1002                 error = EFAULT;
 1003         }
 1004         if (error == 0)
 1005                 bp->bio_completed = bp->bio_length;
 1006         g_io_deliver(bp, error);
 1007         return (1);
 1008 }
 1009 
 1010 int
 1011 g_std_access(struct g_provider *pp,
 1012         int dr __unused, int dw __unused, int de __unused)
 1013 {
 1014 
 1015         g_topology_assert();
 1016         G_VALID_PROVIDER(pp);
 1017         return (0);
 1018 }
 1019 
 1020 void
 1021 g_std_done(struct bio *bp)
 1022 {
 1023         struct bio *bp2;
 1024 
 1025         bp2 = bp->bio_parent;
 1026         if (bp2->bio_error == 0)
 1027                 bp2->bio_error = bp->bio_error;
 1028         bp2->bio_completed += bp->bio_completed;
 1029         g_destroy_bio(bp);
 1030         bp2->bio_inbed++;
 1031         if (bp2->bio_children == bp2->bio_inbed)
 1032                 g_io_deliver(bp2, bp2->bio_error);
 1033 }
 1034 
 1035 /* XXX: maybe this is only g_slice_spoiled */
 1036 
 1037 void
 1038 g_std_spoiled(struct g_consumer *cp)
 1039 {
 1040         struct g_geom *gp;
 1041         struct g_provider *pp;
 1042 
 1043         g_topology_assert();
 1044         G_VALID_CONSUMER(cp);
 1045         g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
 1046         cp->flags |= G_CF_ORPHAN;
 1047         g_detach(cp);
 1048         gp = cp->geom;
 1049         LIST_FOREACH(pp, &gp->provider, provider)
 1050                 g_orphan_provider(pp, ENXIO);
 1051         g_destroy_consumer(cp);
 1052         if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
 1053                 g_destroy_geom(gp);
 1054         else
 1055                 gp->flags |= G_GEOM_WITHER;
 1056 }
 1057 
 1058 /*
 1059  * Spoiling happens when a provider is opened for writing, but consumers
 1060  * which are configured by in-band data are attached (slicers for instance).
 1061  * Since the write might potentially change the in-band data, such consumers
 1062  * need to re-evaluate their existence after the writing session closes.
 1063  * We do this by (offering to) tear them down when the open for write happens
 1064  * in return for a re-taste when it closes again.
 1065  * Together with the fact that such consumers grab an 'e' bit whenever they
 1066  * are open, regardless of mode, this ends up DTRT.
 1067  */
 1068 
 1069 static void
 1070 g_spoil_event(void *arg, int flag)
 1071 {
 1072         struct g_provider *pp;
 1073         struct g_consumer *cp, *cp2;
 1074 
 1075         g_topology_assert();
 1076         if (flag == EV_CANCEL)
 1077                 return;
 1078         pp = arg;
 1079         G_VALID_PROVIDER(pp);
 1080         g_trace(G_T_TOPOLOGY, "%s %p(%s:%s:%s)", __func__, pp,
 1081             pp->geom->class->name, pp->geom->name, pp->name);
 1082         for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
 1083                 cp2 = LIST_NEXT(cp, consumers);
 1084                 if ((cp->flags & G_CF_SPOILED) == 0)
 1085                         continue;
 1086                 cp->flags &= ~G_CF_SPOILED;
 1087                 if (cp->geom->spoiled == NULL)
 1088                         continue;
 1089                 cp->geom->spoiled(cp);
 1090                 g_topology_assert();
 1091         }
 1092 }
 1093 
 1094 void
 1095 g_spoil(struct g_provider *pp, struct g_consumer *cp)
 1096 {
 1097         struct g_consumer *cp2;
 1098 
 1099         g_topology_assert();
 1100         G_VALID_PROVIDER(pp);
 1101         G_VALID_CONSUMER(cp);
 1102 
 1103         LIST_FOREACH(cp2, &pp->consumers, consumers) {
 1104                 if (cp2 == cp)
 1105                         continue;
 1106 /*
 1107                 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
 1108                 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
 1109 */
 1110                 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
 1111                 cp2->flags |= G_CF_SPOILED;
 1112         }
 1113         g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
 1114 }
 1115 
 1116 static void
 1117 g_media_changed_event(void *arg, int flag)
 1118 {
 1119         struct g_provider *pp;
 1120         int retaste;
 1121 
 1122         g_topology_assert();
 1123         if (flag == EV_CANCEL)
 1124                 return;
 1125         pp = arg;
 1126         G_VALID_PROVIDER(pp);
 1127 
 1128         /*
 1129          * If provider was not open for writing, queue retaste after spoiling.
 1130          * If it was, retaste will happen automatically on close.
 1131          */
 1132         retaste = (pp->acw == 0 && pp->error == 0 &&
 1133             !(pp->geom->flags & G_GEOM_WITHER));
 1134         g_spoil_event(arg, flag);
 1135         if (retaste)
 1136                 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
 1137 }
 1138 
 1139 int
 1140 g_media_changed(struct g_provider *pp, int flag)
 1141 {
 1142         struct g_consumer *cp;
 1143 
 1144         LIST_FOREACH(cp, &pp->consumers, consumers)
 1145                 cp->flags |= G_CF_SPOILED;
 1146         return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
 1147 }
 1148 
 1149 int
 1150 g_media_gone(struct g_provider *pp, int flag)
 1151 {
 1152         struct g_consumer *cp;
 1153 
 1154         LIST_FOREACH(cp, &pp->consumers, consumers)
 1155                 cp->flags |= G_CF_SPOILED;
 1156         return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
 1157 }
 1158 
 1159 int
 1160 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
 1161 {
 1162         int error, i;
 1163 
 1164         i = len;
 1165         error = g_io_getattr(attr, cp, &i, var);
 1166         if (error)
 1167                 return (error);
 1168         if (i != len)
 1169                 return (EINVAL);
 1170         return (0);
 1171 }
 1172 
 1173 static int
 1174 g_get_device_prefix_len(const char *name)
 1175 {
 1176         int len;
 1177 
 1178         if (strncmp(name, "ada", 3) == 0)
 1179                 len = 3;
 1180         else if (strncmp(name, "ad", 2) == 0)
 1181                 len = 2;
 1182         else
 1183                 return (0);
 1184         if (name[len] < '' || name[len] > '9')
 1185                 return (0);
 1186         do {
 1187                 len++;
 1188         } while (name[len] >= '' && name[len] <= '9');
 1189         return (len);
 1190 }
 1191 
 1192 int
 1193 g_compare_names(const char *namea, const char *nameb)
 1194 {
 1195         int deva, devb;
 1196 
 1197         if (strcmp(namea, nameb) == 0)
 1198                 return (1);
 1199         deva = g_get_device_prefix_len(namea);
 1200         if (deva == 0)
 1201                 return (0);
 1202         devb = g_get_device_prefix_len(nameb);
 1203         if (devb == 0)
 1204                 return (0);
 1205         if (strcmp(namea + deva, nameb + devb) == 0)
 1206                 return (1);
 1207         return (0);
 1208 }
 1209 
 1210 #if defined(DIAGNOSTIC) || defined(DDB)
 1211 /*
 1212  * This function walks the mesh and returns a non-zero integer if it
 1213  * finds the argument pointer is an object. The return value indicates
 1214  * which type of object it is believed to be. If topology is not locked,
 1215  * this function is potentially dangerous, but we don't assert that the
 1216  * topology lock is held when called from debugger.
 1217  */
 1218 int
 1219 g_valid_obj(void const *ptr)
 1220 {
 1221         struct g_class *mp;
 1222         struct g_geom *gp;
 1223         struct g_consumer *cp;
 1224         struct g_provider *pp;
 1225 
 1226 #ifdef KDB
 1227         if (kdb_active == 0)
 1228 #endif
 1229                 g_topology_assert();
 1230 
 1231         LIST_FOREACH(mp, &g_classes, class) {
 1232                 if (ptr == mp)
 1233                         return (1);
 1234                 LIST_FOREACH(gp, &mp->geom, geom) {
 1235                         if (ptr == gp)
 1236                                 return (2);
 1237                         LIST_FOREACH(cp, &gp->consumer, consumer)
 1238                                 if (ptr == cp)
 1239                                         return (3);
 1240                         LIST_FOREACH(pp, &gp->provider, provider)
 1241                                 if (ptr == pp)
 1242                                         return (4);
 1243                 }
 1244         }
 1245         return(0);
 1246 }
 1247 #endif
 1248 
 1249 #ifdef DDB
 1250 
 1251 #define gprintf(...)    do {                                            \
 1252         db_printf("%*s", indent, "");                                   \
 1253         db_printf(__VA_ARGS__);                                         \
 1254 } while (0)
 1255 #define gprintln(...)   do {                                            \
 1256         gprintf(__VA_ARGS__);                                           \
 1257         db_printf("\n");                                                \
 1258 } while (0)
 1259 
 1260 #define ADDFLAG(obj, flag, sflag)       do {                            \
 1261         if ((obj)->flags & (flag)) {                                    \
 1262                 if (comma)                                              \
 1263                         strlcat(str, ",", size);                        \
 1264                 strlcat(str, (sflag), size);                            \
 1265                 comma = 1;                                              \
 1266         }                                                               \
 1267 } while (0)
 1268 
 1269 static char *
 1270 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
 1271 {
 1272         int comma = 0;
 1273 
 1274         bzero(str, size);
 1275         if (pp->flags == 0) {
 1276                 strlcpy(str, "NONE", size);
 1277                 return (str);
 1278         }
 1279         ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
 1280         ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
 1281         return (str);
 1282 }
 1283 
 1284 static char *
 1285 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
 1286 {
 1287         int comma = 0;
 1288 
 1289         bzero(str, size);
 1290         if (gp->flags == 0) {
 1291                 strlcpy(str, "NONE", size);
 1292                 return (str);
 1293         }
 1294         ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
 1295         return (str);
 1296 }
 1297 static void
 1298 db_show_geom_consumer(int indent, struct g_consumer *cp)
 1299 {
 1300 
 1301         if (indent == 0) {
 1302                 gprintln("consumer: %p", cp);
 1303                 gprintln("  class:    %s (%p)", cp->geom->class->name,
 1304                     cp->geom->class);
 1305                 gprintln("  geom:     %s (%p)", cp->geom->name, cp->geom);
 1306                 if (cp->provider == NULL)
 1307                         gprintln("  provider: none");
 1308                 else {
 1309                         gprintln("  provider: %s (%p)", cp->provider->name,
 1310                             cp->provider);
 1311                 }
 1312                 gprintln("  access:   r%dw%de%d", cp->acr, cp->acw, cp->ace);
 1313                 gprintln("  flags:    0x%04x", cp->flags);
 1314                 gprintln("  nstart:   %u", cp->nstart);
 1315                 gprintln("  nend:     %u", cp->nend);
 1316         } else {
 1317                 gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
 1318                     cp->provider != NULL ? cp->provider->name : "none",
 1319                     cp->acr, cp->acw, cp->ace);
 1320                 if (cp->flags)
 1321                         db_printf(", flags=0x%04x", cp->flags);
 1322                 db_printf("\n");
 1323         }
 1324 }
 1325 
 1326 static void
 1327 db_show_geom_provider(int indent, struct g_provider *pp)
 1328 {
 1329         struct g_consumer *cp;
 1330         char flags[64];
 1331 
 1332         if (indent == 0) {
 1333                 gprintln("provider: %s (%p)", pp->name, pp);
 1334                 gprintln("  class:        %s (%p)", pp->geom->class->name,
 1335                     pp->geom->class);
 1336                 gprintln("  geom:         %s (%p)", pp->geom->name, pp->geom);
 1337                 gprintln("  mediasize:    %jd", (intmax_t)pp->mediasize);
 1338                 gprintln("  sectorsize:   %u", pp->sectorsize);
 1339                 gprintln("  stripesize:   %u", pp->stripesize);
 1340                 gprintln("  stripeoffset: %u", pp->stripeoffset);
 1341                 gprintln("  access:       r%dw%de%d", pp->acr, pp->acw,
 1342                     pp->ace);
 1343                 gprintln("  flags:        %s (0x%04x)",
 1344                     provider_flags_to_string(pp, flags, sizeof(flags)),
 1345                     pp->flags);
 1346                 gprintln("  error:        %d", pp->error);
 1347                 gprintln("  nstart:       %u", pp->nstart);
 1348                 gprintln("  nend:         %u", pp->nend);
 1349                 if (LIST_EMPTY(&pp->consumers))
 1350                         gprintln("  consumers:    none");
 1351         } else {
 1352                 gprintf("provider: %s (%p), access=r%dw%de%d",
 1353                     pp->name, pp, pp->acr, pp->acw, pp->ace);
 1354                 if (pp->flags != 0) {
 1355                         db_printf(", flags=%s (0x%04x)",
 1356                             provider_flags_to_string(pp, flags, sizeof(flags)),
 1357                             pp->flags);
 1358                 }
 1359                 db_printf("\n");
 1360         }
 1361         if (!LIST_EMPTY(&pp->consumers)) {
 1362                 LIST_FOREACH(cp, &pp->consumers, consumers) {
 1363                         db_show_geom_consumer(indent + 2, cp);
 1364                         if (db_pager_quit)
 1365                                 break;
 1366                 }
 1367         }
 1368 }
 1369 
 1370 static void
 1371 db_show_geom_geom(int indent, struct g_geom *gp)
 1372 {
 1373         struct g_provider *pp;
 1374         struct g_consumer *cp;
 1375         char flags[64];
 1376 
 1377         if (indent == 0) {
 1378                 gprintln("geom: %s (%p)", gp->name, gp);
 1379                 gprintln("  class:     %s (%p)", gp->class->name, gp->class);
 1380                 gprintln("  flags:     %s (0x%04x)",
 1381                     geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
 1382                 gprintln("  rank:      %d", gp->rank);
 1383                 if (LIST_EMPTY(&gp->provider))
 1384                         gprintln("  providers: none");
 1385                 if (LIST_EMPTY(&gp->consumer))
 1386                         gprintln("  consumers: none");
 1387         } else {
 1388                 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
 1389                 if (gp->flags != 0) {
 1390                         db_printf(", flags=%s (0x%04x)",
 1391                             geom_flags_to_string(gp, flags, sizeof(flags)),
 1392                             gp->flags);
 1393                 }
 1394                 db_printf("\n");
 1395         }
 1396         if (!LIST_EMPTY(&gp->provider)) {
 1397                 LIST_FOREACH(pp, &gp->provider, provider) {
 1398                         db_show_geom_provider(indent + 2, pp);
 1399                         if (db_pager_quit)
 1400                                 break;
 1401                 }
 1402         }
 1403         if (!LIST_EMPTY(&gp->consumer)) {
 1404                 LIST_FOREACH(cp, &gp->consumer, consumer) {
 1405                         db_show_geom_consumer(indent + 2, cp);
 1406                         if (db_pager_quit)
 1407                                 break;
 1408                 }
 1409         }
 1410 }
 1411 
 1412 static void
 1413 db_show_geom_class(struct g_class *mp)
 1414 {
 1415         struct g_geom *gp;
 1416 
 1417         db_printf("class: %s (%p)\n", mp->name, mp);
 1418         LIST_FOREACH(gp, &mp->geom, geom) {
 1419                 db_show_geom_geom(2, gp);
 1420                 if (db_pager_quit)
 1421                         break;
 1422         }
 1423 }
 1424 
 1425 /*
 1426  * Print the GEOM topology or the given object.
 1427  */
 1428 DB_SHOW_COMMAND(geom, db_show_geom)
 1429 {
 1430         struct g_class *mp;
 1431 
 1432         if (!have_addr) {
 1433                 /* No address given, print the entire topology. */
 1434                 LIST_FOREACH(mp, &g_classes, class) {
 1435                         db_show_geom_class(mp);
 1436                         db_printf("\n");
 1437                         if (db_pager_quit)
 1438                                 break;
 1439                 }
 1440         } else {
 1441                 switch (g_valid_obj((void *)addr)) {
 1442                 case 1:
 1443                         db_show_geom_class((struct g_class *)addr);
 1444                         break;
 1445                 case 2:
 1446                         db_show_geom_geom(0, (struct g_geom *)addr);
 1447                         break;
 1448                 case 3:
 1449                         db_show_geom_consumer(0, (struct g_consumer *)addr);
 1450                         break;
 1451                 case 4:
 1452                         db_show_geom_provider(0, (struct g_provider *)addr);
 1453                         break;
 1454                 default:
 1455                         db_printf("Not a GEOM object.\n");
 1456                         break;
 1457                 }
 1458         }
 1459 }
 1460 
 1461 static void
 1462 db_print_bio_cmd(struct bio *bp)
 1463 {
 1464         db_printf("  cmd: ");
 1465         switch (bp->bio_cmd) {
 1466         case BIO_READ: db_printf("BIO_READ"); break;
 1467         case BIO_WRITE: db_printf("BIO_WRITE"); break;
 1468         case BIO_DELETE: db_printf("BIO_DELETE"); break;
 1469         case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
 1470         case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
 1471         case BIO_CMD0: db_printf("BIO_CMD0"); break;
 1472         case BIO_CMD1: db_printf("BIO_CMD1"); break;
 1473         case BIO_CMD2: db_printf("BIO_CMD2"); break;
 1474         default: db_printf("UNKNOWN"); break;
 1475         }
 1476         db_printf("\n");
 1477 }
 1478 
 1479 static void
 1480 db_print_bio_flags(struct bio *bp)
 1481 {
 1482         int comma;
 1483 
 1484         comma = 0;
 1485         db_printf("  flags: ");
 1486         if (bp->bio_flags & BIO_ERROR) {
 1487                 db_printf("BIO_ERROR");
 1488                 comma = 1;
 1489         }
 1490         if (bp->bio_flags & BIO_DONE) {
 1491                 db_printf("%sBIO_DONE", (comma ? ", " : ""));
 1492                 comma = 1;
 1493         }
 1494         if (bp->bio_flags & BIO_ONQUEUE)
 1495                 db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
 1496         db_printf("\n");
 1497 }
 1498 
 1499 /*
 1500  * Print useful information in a BIO
 1501  */
 1502 DB_SHOW_COMMAND(bio, db_show_bio)
 1503 {
 1504         struct bio *bp;
 1505 
 1506         if (have_addr) {
 1507                 bp = (struct bio *)addr;
 1508                 db_printf("BIO %p\n", bp);
 1509                 db_print_bio_cmd(bp);
 1510                 db_print_bio_flags(bp);
 1511                 db_printf("  cflags: 0x%hhx\n", bp->bio_cflags);
 1512                 db_printf("  pflags: 0x%hhx\n", bp->bio_pflags);
 1513                 db_printf("  offset: %jd\n", (intmax_t)bp->bio_offset);
 1514                 db_printf("  length: %jd\n", (intmax_t)bp->bio_length);
 1515                 db_printf("  bcount: %ld\n", bp->bio_bcount);
 1516                 db_printf("  resid: %ld\n", bp->bio_resid);
 1517                 db_printf("  completed: %jd\n", (intmax_t)bp->bio_completed);
 1518                 db_printf("  children: %u\n", bp->bio_children);
 1519                 db_printf("  inbed: %u\n", bp->bio_inbed);
 1520                 db_printf("  error: %d\n", bp->bio_error);
 1521                 db_printf("  parent: %p\n", bp->bio_parent);
 1522                 db_printf("  driver1: %p\n", bp->bio_driver1);
 1523                 db_printf("  driver2: %p\n", bp->bio_driver2);
 1524                 db_printf("  caller1: %p\n", bp->bio_caller1);
 1525                 db_printf("  caller2: %p\n", bp->bio_caller2);
 1526                 db_printf("  bio_from: %p\n", bp->bio_from);
 1527                 db_printf("  bio_to: %p\n", bp->bio_to);
 1528         }
 1529 }
 1530 
 1531 #undef  gprintf
 1532 #undef  gprintln
 1533 #undef  ADDFLAG
 1534 
 1535 #endif  /* DDB */

Cache object: 6077fc30fd2eaf7e3438b800a20b7761


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