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/virstor/g_virstor.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) 2006-2007 Ivan Voras <ivoras@freebsd.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 /* Implementation notes:
   28  * - "Components" are wrappers around providers that make up the
   29  *   virtual storage (i.e. a virstor has "physical" components)
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include <sys/param.h>
   36 #include <sys/systm.h>
   37 #include <sys/kernel.h>
   38 #include <sys/module.h>
   39 #include <sys/lock.h>
   40 #include <sys/mutex.h>
   41 #include <sys/sx.h>
   42 #include <sys/bio.h>
   43 #include <sys/sysctl.h>
   44 #include <sys/malloc.h>
   45 #include <sys/time.h>
   46 #include <sys/proc.h>
   47 #include <sys/kthread.h>
   48 #include <sys/mutex.h>
   49 #include <vm/uma.h>
   50 #include <geom/geom.h>
   51 
   52 #include <geom/virstor/g_virstor.h>
   53 #include <geom/virstor/g_virstor_md.h>
   54 
   55 /* Declare malloc(9) label */
   56 static MALLOC_DEFINE(M_GVIRSTOR, "gvirstor", "GEOM_VIRSTOR Data");
   57 
   58 /* GEOM class methods */
   59 static g_init_t g_virstor_init;
   60 static g_fini_t g_virstor_fini;
   61 static g_taste_t g_virstor_taste;
   62 static g_ctl_req_t g_virstor_config;
   63 static g_ctl_destroy_geom_t g_virstor_destroy_geom;
   64 
   65 /* Declare & initialize class structure ("geom class") */
   66 struct g_class g_virstor_class = {
   67         .name =         G_VIRSTOR_CLASS_NAME,
   68         .version =      G_VERSION,
   69         .init =         g_virstor_init,
   70         .fini =         g_virstor_fini,
   71         .taste =        g_virstor_taste,
   72         .ctlreq =       g_virstor_config,
   73         .destroy_geom = g_virstor_destroy_geom
   74         /* The .dumpconf and the rest are only usable for a geom instance, so
   75          * they will be set when such instance is created. */
   76 };
   77 
   78 /* Declare sysctl's and loader tunables */
   79 SYSCTL_DECL(_kern_geom);
   80 SYSCTL_NODE(_kern_geom, OID_AUTO, virstor, CTLFLAG_RW, 0, "GEOM_GVIRSTOR information");
   81 
   82 static u_int g_virstor_debug = 2; /* XXX: lower to 2 when released to public */
   83 TUNABLE_INT("kern.geom.virstor.debug", &g_virstor_debug);
   84 SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, debug, CTLFLAG_RW, &g_virstor_debug,
   85     0, "Debug level (2=production, 5=normal, 15=excessive)");
   86 
   87 static u_int g_virstor_chunk_watermark = 100;
   88 TUNABLE_INT("kern.geom.virstor.chunk_watermark", &g_virstor_chunk_watermark);
   89 SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, chunk_watermark, CTLFLAG_RW,
   90     &g_virstor_chunk_watermark, 0,
   91     "Minimum number of free chunks before issuing administrative warning");
   92 
   93 static u_int g_virstor_component_watermark = 1;
   94 TUNABLE_INT("kern.geom.virstor.component_watermark",
   95     &g_virstor_component_watermark);
   96 SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, component_watermark, CTLFLAG_RW,
   97     &g_virstor_component_watermark, 0,
   98     "Minimum number of free components before issuing administrative warning");
   99 
  100 static int read_metadata(struct g_consumer *, struct g_virstor_metadata *);
  101 static int write_metadata(struct g_consumer *, struct g_virstor_metadata *);
  102 static int clear_metadata(struct g_virstor_component *);
  103 static int add_provider_to_geom(struct g_virstor_softc *, struct g_provider *,
  104     struct g_virstor_metadata *);
  105 static struct g_geom *create_virstor_geom(struct g_class *,
  106     struct g_virstor_metadata *);
  107 static void virstor_check_and_run(struct g_virstor_softc *);
  108 static u_int virstor_valid_components(struct g_virstor_softc *);
  109 static int virstor_geom_destroy(struct g_virstor_softc *, boolean_t,
  110     boolean_t);
  111 static void remove_component(struct g_virstor_softc *,
  112     struct g_virstor_component *, boolean_t);
  113 static void bioq_dismantle(struct bio_queue_head *);
  114 static int allocate_chunk(struct g_virstor_softc *,
  115     struct g_virstor_component **, u_int *, u_int *);
  116 static void delay_destroy_consumer(void *, int);
  117 static void dump_component(struct g_virstor_component *comp);
  118 #if 0
  119 static void dump_me(struct virstor_map_entry *me, unsigned int nr);
  120 #endif
  121 
  122 static void virstor_ctl_stop(struct gctl_req *, struct g_class *);
  123 static void virstor_ctl_add(struct gctl_req *, struct g_class *);
  124 static void virstor_ctl_remove(struct gctl_req *, struct g_class *);
  125 static struct g_virstor_softc * virstor_find_geom(const struct g_class *,
  126     const char *);
  127 static void update_metadata(struct g_virstor_softc *);
  128 static void fill_metadata(struct g_virstor_softc *, struct g_virstor_metadata *,
  129     u_int, u_int);
  130 
  131 static void g_virstor_orphan(struct g_consumer *);
  132 static int g_virstor_access(struct g_provider *, int, int, int);
  133 static void g_virstor_start(struct bio *);
  134 static void g_virstor_dumpconf(struct sbuf *, const char *, struct g_geom *,
  135     struct g_consumer *, struct g_provider *);
  136 static void g_virstor_done(struct bio *);
  137 
  138 static void invalid_call(void);
  139 /*
  140  * Initialise GEOM class (per-class callback)
  141  */
  142 static void
  143 g_virstor_init(struct g_class *mp __unused)
  144 {
  145 
  146         /* Catch map struct size mismatch at compile time; Map entries must
  147          * fit into MAXPHYS exactly, with no wasted space. */
  148         CTASSERT(VIRSTOR_MAP_BLOCK_ENTRIES*VIRSTOR_MAP_ENTRY_SIZE == MAXPHYS);
  149 
  150         /* Init UMA zones, TAILQ's, other global vars */
  151 }
  152 
  153 /*
  154  * Finalise GEOM class (per-class callback)
  155  */
  156 static void
  157 g_virstor_fini(struct g_class *mp __unused)
  158 {
  159 
  160         /* Deinit UMA zones & global vars */
  161 }
  162 
  163 /*
  164  * Config (per-class callback)
  165  */
  166 static void
  167 g_virstor_config(struct gctl_req *req, struct g_class *cp, char const *verb)
  168 {
  169         uint32_t *version;
  170 
  171         g_topology_assert();
  172 
  173         version = gctl_get_paraml(req, "version", sizeof(*version));
  174         if (version == NULL) {
  175                 gctl_error(req, "Failed to get 'version' argument");
  176                 return;
  177         }
  178         if (*version != G_VIRSTOR_VERSION) {
  179                 gctl_error(req, "Userland and kernel versions out of sync");
  180                 return;
  181         }
  182 
  183         g_topology_unlock();
  184         if (strcmp(verb, "add") == 0)
  185                 virstor_ctl_add(req, cp);
  186         else if (strcmp(verb, "stop") == 0 || strcmp(verb, "destroy") == 0)
  187                 virstor_ctl_stop(req, cp);
  188         else if (strcmp(verb, "remove") == 0)
  189                 virstor_ctl_remove(req, cp);
  190         else
  191                 gctl_error(req, "unknown verb: '%s'", verb);
  192         g_topology_lock();
  193 }
  194 
  195 /*
  196  * "stop" verb from userland
  197  */
  198 static void
  199 virstor_ctl_stop(struct gctl_req *req, struct g_class *cp)
  200 {
  201         int *force, *nargs;
  202         int i;
  203 
  204         nargs = gctl_get_paraml(req, "nargs", sizeof *nargs);
  205         if (nargs == NULL) {
  206                 gctl_error(req, "Error fetching argument '%s'", "nargs");
  207                 return;
  208         }
  209         if (*nargs < 1) {
  210                 gctl_error(req, "Invalid number of arguments");
  211                 return;
  212         }
  213         force = gctl_get_paraml(req, "force", sizeof *force);
  214         if (force == NULL) {
  215                 gctl_error(req, "Error fetching argument '%s'", "force");
  216                 return;
  217         }
  218 
  219         g_topology_lock();
  220         for (i = 0; i < *nargs; i++) {
  221                 char param[8];
  222                 const char *name;
  223                 struct g_virstor_softc *sc;
  224                 int error;
  225 
  226                 sprintf(param, "arg%d", i);
  227                 name = gctl_get_asciiparam(req, param);
  228                 if (name == NULL) {
  229                         gctl_error(req, "No 'arg%d' argument", i);
  230                         g_topology_unlock();
  231                         return;
  232                 }
  233                 sc = virstor_find_geom(cp, name);
  234                 if (sc == NULL) {
  235                         gctl_error(req, "Don't know anything about '%s'", name);
  236                         g_topology_unlock();
  237                         return;
  238                 }
  239 
  240                 LOG_MSG(LVL_INFO, "Stopping %s by the userland command",
  241                     sc->geom->name);
  242                 update_metadata(sc);
  243                 if ((error = virstor_geom_destroy(sc, TRUE, TRUE)) != 0) {
  244                         LOG_MSG(LVL_ERROR, "Cannot destroy %s: %d",
  245                             sc->geom->name, error);
  246                 }
  247         }
  248         g_topology_unlock();
  249 }
  250 
  251 /*
  252  * "add" verb from userland - add new component(s) to the structure.
  253  * This will be done all at once in here, without going through the
  254  * .taste function for new components.
  255  */
  256 static void
  257 virstor_ctl_add(struct gctl_req *req, struct g_class *cp)
  258 {
  259         /* Note: while this is going on, I/O is being done on
  260          * the g_up and g_down threads. The idea is to make changes
  261          * to softc members in a way that can atomically activate
  262          * them all at once. */
  263         struct g_virstor_softc *sc;
  264         int *hardcode, *nargs;
  265         const char *geom_name;  /* geom to add a component to */
  266         struct g_consumer *fcp;
  267         struct g_virstor_bio_q *bq;
  268         u_int added;
  269         int error;
  270         int i;
  271 
  272         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
  273         if (nargs == NULL) {
  274                 gctl_error(req, "Error fetching argument '%s'", "nargs");
  275                 return;
  276         }
  277         if (*nargs < 2) {
  278                 gctl_error(req, "Invalid number of arguments");
  279                 return;
  280         }
  281         hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
  282         if (hardcode == NULL) {
  283                 gctl_error(req, "Error fetching argument '%s'", "hardcode");
  284                 return;
  285         }
  286 
  287         /* Find "our" geom */
  288         geom_name = gctl_get_asciiparam(req, "arg0");
  289         if (geom_name == NULL) {
  290                 gctl_error(req, "Error fetching argument '%s'", "geom_name (arg0)");
  291                 return;
  292         }
  293         sc = virstor_find_geom(cp, geom_name);
  294         if (sc == NULL) {
  295                 gctl_error(req, "Don't know anything about '%s'", geom_name);
  296                 return;
  297         }
  298 
  299         if (virstor_valid_components(sc) != sc->n_components) {
  300                 LOG_MSG(LVL_ERROR, "Cannot add components to incomplete "
  301                     "virstor %s", sc->geom->name);
  302                 gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
  303                 return;
  304         }
  305 
  306         fcp = sc->components[0].gcons;
  307         added = 0;
  308         g_topology_lock();
  309         for (i = 1; i < *nargs; i++) {
  310                 struct g_virstor_metadata md;
  311                 char aname[8];
  312                 const char *prov_name;
  313                 struct g_provider *pp;
  314                 struct g_consumer *cp;
  315                 u_int nc;
  316                 u_int j;
  317 
  318                 snprintf(aname, sizeof aname, "arg%d", i);
  319                 prov_name = gctl_get_asciiparam(req, aname);
  320                 if (prov_name == NULL) {
  321                         gctl_error(req, "Error fetching argument '%s'", aname);
  322                         g_topology_unlock();
  323                         return;
  324                 }
  325                 if (strncmp(prov_name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
  326                         prov_name += strlen(_PATH_DEV);
  327 
  328                 pp = g_provider_by_name(prov_name);
  329                 if (pp == NULL) {
  330                         /* This is the most common error so be verbose about it */
  331                         if (added != 0) {
  332                                 gctl_error(req, "Invalid provider: '%s' (added"
  333                                     " %u components)", prov_name, added);
  334                                 update_metadata(sc);
  335                         } else {
  336                                 gctl_error(req, "Invalid provider: '%s'",
  337                                     prov_name);
  338                         }
  339                         g_topology_unlock();
  340                         return;
  341                 }
  342                 cp = g_new_consumer(sc->geom);
  343                 if (cp == NULL) {
  344                         gctl_error(req, "Cannot create consumer");
  345                         g_topology_unlock();
  346                         return;
  347                 }
  348                 error = g_attach(cp, pp);
  349                 if (error != 0) {
  350                         gctl_error(req, "Cannot attach a consumer to %s",
  351                             pp->name);
  352                         g_destroy_consumer(cp);
  353                         g_topology_unlock();
  354                         return;
  355                 }
  356                 if (fcp->acr != 0 || fcp->acw != 0 || fcp->ace != 0) {
  357                         error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
  358                         if (error != 0) {
  359                                 gctl_error(req, "Access request failed for %s",
  360                                     pp->name);
  361                                 g_destroy_consumer(cp);
  362                                 g_topology_unlock();
  363                                 return;
  364                         }
  365                 }
  366                 if (fcp->provider->sectorsize != pp->sectorsize) {
  367                         gctl_error(req, "Sector size doesn't fit for %s",
  368                             pp->name);
  369                         g_destroy_consumer(cp);
  370                         g_topology_unlock();
  371                         return;
  372                 }
  373                 for (j = 0; j < sc->n_components; j++) {
  374                         if (strcmp(sc->components[j].gcons->provider->name,
  375                             pp->name) == 0) {
  376                                 gctl_error(req, "Component %s already in %s",
  377                                     pp->name, sc->geom->name);
  378                                 g_destroy_consumer(cp);
  379                                 g_topology_unlock();
  380                                 return;
  381                         }
  382                 }
  383                 sc->components = realloc(sc->components,
  384                     sizeof(*sc->components) * (sc->n_components + 1),
  385                     M_GVIRSTOR, M_WAITOK);
  386 
  387                 nc = sc->n_components;
  388                 sc->components[nc].gcons = cp;
  389                 sc->components[nc].sc = sc;
  390                 sc->components[nc].index = nc;
  391                 sc->components[nc].chunk_count = cp->provider->mediasize /
  392                     sc->chunk_size;
  393                 sc->components[nc].chunk_next = 0;
  394                 sc->components[nc].chunk_reserved = 0;
  395 
  396                 if (sc->components[nc].chunk_count < 4) {
  397                         gctl_error(req, "Provider too small: %s",
  398                             cp->provider->name);
  399                         g_destroy_consumer(cp);
  400                         g_topology_unlock();
  401                         return;
  402                 }
  403                 fill_metadata(sc, &md, nc, *hardcode);
  404                 write_metadata(cp, &md);
  405                 /* The new component becomes visible when n_components is
  406                  * incremented */
  407                 sc->n_components++;
  408                 added++;
  409 
  410         }
  411         /* This call to update_metadata() is critical. In case there's a
  412          * power failure in the middle of it and some components are updated
  413          * while others are not, there will be trouble on next .taste() iff
  414          * a non-updated component is detected first */
  415         update_metadata(sc);
  416         g_topology_unlock();
  417         LOG_MSG(LVL_INFO, "Added %d component(s) to %s", added,
  418             sc->geom->name);
  419         /* Fire off BIOs previously queued because there wasn't any
  420          * physical space left. If the BIOs still can't be satisfied
  421          * they will again be added to the end of the queue (during
  422          * which the mutex will be recursed) */
  423         bq = malloc(sizeof(*bq), M_GVIRSTOR, M_WAITOK);
  424         bq->bio = NULL;
  425         mtx_lock(&sc->delayed_bio_q_mtx);
  426         /* First, insert a sentinel to the queue end, so we don't
  427          * end up in an infinite loop if there's still no free
  428          * space available. */
  429         STAILQ_INSERT_TAIL(&sc->delayed_bio_q, bq, linkage);
  430         while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
  431                 bq = STAILQ_FIRST(&sc->delayed_bio_q);
  432                 if (bq->bio != NULL) {
  433                         g_virstor_start(bq->bio);
  434                         STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
  435                         free(bq, M_GVIRSTOR);
  436                 } else {
  437                         STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
  438                         free(bq, M_GVIRSTOR);
  439                         break;
  440                 }
  441         }
  442         mtx_unlock(&sc->delayed_bio_q_mtx);
  443 
  444 }
  445 
  446 /*
  447  * Find a geom handled by the class
  448  */
  449 static struct g_virstor_softc *
  450 virstor_find_geom(const struct g_class *cp, const char *name)
  451 {
  452         struct g_geom *gp;
  453 
  454         LIST_FOREACH(gp, &cp->geom, geom) {
  455                 if (strcmp(name, gp->name) == 0)
  456                         return (gp->softc);
  457         }
  458         return (NULL);
  459 }
  460 
  461 /*
  462  * Update metadata on all components to reflect the current state
  463  * of these fields:
  464  *    - chunk_next
  465  *    - flags
  466  *    - md_count
  467  * Expects things to be set up so write_metadata() can work, i.e.
  468  * the topology lock must be held.
  469  */
  470 static void
  471 update_metadata(struct g_virstor_softc *sc)
  472 {
  473         struct g_virstor_metadata md;
  474         int n;
  475 
  476         if (virstor_valid_components(sc) != sc->n_components)
  477                 return; /* Incomplete device */
  478         LOG_MSG(LVL_DEBUG, "Updating metadata on components for %s",
  479             sc->geom->name);
  480         /* Update metadata on components */
  481         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__,
  482             sc->geom->class->name, sc->geom->name);
  483         g_topology_assert();
  484         for (n = 0; n < sc->n_components; n++) {
  485                 read_metadata(sc->components[n].gcons, &md);
  486                 md.chunk_next = sc->components[n].chunk_next;
  487                 md.flags = sc->components[n].flags;
  488                 md.md_count = sc->n_components;
  489                 write_metadata(sc->components[n].gcons, &md);
  490         }
  491 }
  492 
  493 /*
  494  * Fills metadata (struct md) from information stored in softc and the nc'th
  495  * component of virstor
  496  */
  497 static void
  498 fill_metadata(struct g_virstor_softc *sc, struct g_virstor_metadata *md,
  499     u_int nc, u_int hardcode)
  500 {
  501         struct g_virstor_component *c;
  502 
  503         bzero(md, sizeof *md);
  504         c = &sc->components[nc];
  505 
  506         strncpy(md->md_magic, G_VIRSTOR_MAGIC, sizeof md->md_magic);
  507         md->md_version = G_VIRSTOR_VERSION;
  508         strncpy(md->md_name, sc->geom->name, sizeof md->md_name);
  509         md->md_id = sc->id;
  510         md->md_virsize = sc->virsize;
  511         md->md_chunk_size = sc->chunk_size;
  512         md->md_count = sc->n_components;
  513 
  514         if (hardcode) {
  515                 strncpy(md->provider, c->gcons->provider->name,
  516                     sizeof md->provider);
  517         }
  518         md->no = nc;
  519         md->provsize = c->gcons->provider->mediasize;
  520         md->chunk_count = c->chunk_count;
  521         md->chunk_next = c->chunk_next;
  522         md->chunk_reserved = c->chunk_reserved;
  523         md->flags = c->flags;
  524 }
  525 
  526 /*
  527  * Remove a component from virstor device.
  528  * Can only be done if the component is unallocated.
  529  */
  530 static void
  531 virstor_ctl_remove(struct gctl_req *req, struct g_class *cp)
  532 {
  533         /* As this is executed in parallel to I/O, operations on virstor
  534          * structures must be as atomic as possible. */
  535         struct g_virstor_softc *sc;
  536         int *nargs;
  537         const char *geom_name;
  538         u_int removed;
  539         int i;
  540 
  541         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
  542         if (nargs == NULL) {
  543                 gctl_error(req, "Error fetching argument '%s'", "nargs");
  544                 return;
  545         }
  546         if (*nargs < 2) {
  547                 gctl_error(req, "Invalid number of arguments");
  548                 return;
  549         }
  550         /* Find "our" geom */
  551         geom_name = gctl_get_asciiparam(req, "arg0");
  552         if (geom_name == NULL) {
  553                 gctl_error(req, "Error fetching argument '%s'",
  554                     "geom_name (arg0)");
  555                 return;
  556         }
  557         sc = virstor_find_geom(cp, geom_name);
  558         if (sc == NULL) {
  559                 gctl_error(req, "Don't know anything about '%s'", geom_name);
  560                 return;
  561         }
  562 
  563         if (virstor_valid_components(sc) != sc->n_components) {
  564                 LOG_MSG(LVL_ERROR, "Cannot remove components from incomplete "
  565                     "virstor %s", sc->geom->name);
  566                 gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
  567                 return;
  568         }
  569 
  570         removed = 0;
  571         for (i = 1; i < *nargs; i++) {
  572                 char param[8];
  573                 const char *prov_name;
  574                 int j, found;
  575                 struct g_virstor_component *newcomp, *compbak;
  576 
  577                 sprintf(param, "arg%d", i);
  578                 prov_name = gctl_get_asciiparam(req, param);
  579                 if (prov_name == NULL) {
  580                         gctl_error(req, "Error fetching argument '%s'", param);
  581                         return;
  582                 }
  583                 if (strncmp(prov_name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
  584                         prov_name += strlen(_PATH_DEV);
  585 
  586                 found = -1;
  587                 for (j = 0; j < sc->n_components; j++) {
  588                         if (strcmp(sc->components[j].gcons->provider->name,
  589                             prov_name) == 0) {
  590                                 found = j;
  591                                 break;
  592                         }
  593                 }
  594                 if (found == -1) {
  595                         LOG_MSG(LVL_ERROR, "No %s component in %s",
  596                             prov_name, sc->geom->name);
  597                         continue;
  598                 }
  599 
  600                 compbak = sc->components;
  601                 newcomp = malloc(sc->n_components * sizeof(*sc->components),
  602                     M_GVIRSTOR, M_WAITOK | M_ZERO);
  603                 bcopy(sc->components, newcomp, found * sizeof(*sc->components));
  604                 bcopy(&sc->components[found + 1], newcomp + found,
  605                     found * sizeof(*sc->components));
  606                 if ((sc->components[j].flags & VIRSTOR_PROVIDER_ALLOCATED) != 0) {
  607                         LOG_MSG(LVL_ERROR, "Allocated provider %s cannot be "
  608                             "removed from %s",
  609                             prov_name, sc->geom->name);
  610                         free(newcomp, M_GVIRSTOR);
  611                         /* We'll consider this non-fatal error */
  612                         continue;
  613                 }
  614                 /* Renumerate unallocated components */
  615                 for (j = 0; j < sc->n_components-1; j++) {
  616                         if ((sc->components[j].flags &
  617                             VIRSTOR_PROVIDER_ALLOCATED) == 0) {
  618                                 sc->components[j].index = j;
  619                         }
  620                 }
  621                 /* This is the critical section. If a component allocation
  622                  * event happens while both variables are not yet set,
  623                  * there will be trouble. Something will panic on encountering
  624                  * NULL sc->components[x].gcomp member.
  625                  * Luckily, component allocation happens very rarely and
  626                  * removing components is an abnormal action in any case. */
  627                 sc->components = newcomp;
  628                 sc->n_components--;
  629                 /* End critical section */
  630 
  631                 g_topology_lock();
  632                 if (clear_metadata(&compbak[found]) != 0) {
  633                         LOG_MSG(LVL_WARNING, "Trouble ahead: cannot clear "
  634                             "metadata on %s", prov_name);
  635                 }
  636                 g_detach(compbak[found].gcons);
  637                 g_destroy_consumer(compbak[found].gcons);
  638                 g_topology_unlock();
  639 
  640                 free(compbak, M_GVIRSTOR);
  641 
  642                 removed++;
  643         }
  644 
  645         /* This call to update_metadata() is critical. In case there's a
  646          * power failure in the middle of it and some components are updated
  647          * while others are not, there will be trouble on next .taste() iff
  648          * a non-updated component is detected first */
  649         g_topology_lock();
  650         update_metadata(sc);
  651         g_topology_unlock();
  652         LOG_MSG(LVL_INFO, "Removed %d component(s) from %s", removed,
  653             sc->geom->name);
  654 }
  655 
  656 /*
  657  * Clear metadata sector on component
  658  */
  659 static int
  660 clear_metadata(struct g_virstor_component *comp)
  661 {
  662         char *buf;
  663         int error;
  664 
  665         LOG_MSG(LVL_INFO, "Clearing metadata on %s",
  666             comp->gcons->provider->name);
  667         g_topology_assert();
  668         error = g_access(comp->gcons, 0, 1, 0);
  669         if (error != 0)
  670                 return (error);
  671         buf = malloc(comp->gcons->provider->sectorsize, M_GVIRSTOR,
  672             M_WAITOK | M_ZERO);
  673         error = g_write_data(comp->gcons,
  674             comp->gcons->provider->mediasize -
  675             comp->gcons->provider->sectorsize,
  676             buf,
  677             comp->gcons->provider->sectorsize);
  678         free(buf, M_GVIRSTOR);
  679         g_access(comp->gcons, 0, -1, 0);
  680         return (error);
  681 }
  682 
  683 /*
  684  * Destroy geom forcibly.
  685  */
  686 static int
  687 g_virstor_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
  688     struct g_geom *gp)
  689 {
  690         struct g_virstor_softc *sc;
  691         int exitval;
  692 
  693         sc = gp->softc;
  694         KASSERT(sc != NULL, ("%s: NULL sc", __func__));
  695         
  696         exitval = 0;
  697         LOG_MSG(LVL_DEBUG, "%s called for %s, sc=%p", __func__, gp->name,
  698             gp->softc);
  699 
  700         if (sc != NULL) {
  701 #ifdef INVARIANTS
  702                 char *buf;
  703                 int error;
  704                 off_t off;
  705                 int isclean, count;
  706                 int n;
  707 
  708                 LOG_MSG(LVL_INFO, "INVARIANTS detected");
  709                 LOG_MSG(LVL_INFO, "Verifying allocation "
  710                     "table for %s", sc->geom->name);
  711                 count = 0;
  712                 for (n = 0; n < sc->chunk_count; n++) {
  713                         if (sc->map[n].flags || VIRSTOR_MAP_ALLOCATED != 0)
  714                                 count++;
  715                 }
  716                 LOG_MSG(LVL_INFO, "Device %s has %d allocated chunks",
  717                     sc->geom->name, count);
  718                 n = off = count = 0;
  719                 isclean = 1;
  720                 if (virstor_valid_components(sc) != sc->n_components) {
  721                         /* This is a incomplete virstor device (not all
  722                          * components have been found) */
  723                         LOG_MSG(LVL_ERROR, "Device %s is incomplete",
  724                             sc->geom->name);
  725                         goto bailout;
  726                 }
  727                 error = g_access(sc->components[0].gcons, 1, 0, 0);
  728                 KASSERT(error == 0, ("%s: g_access failed (%d)", __func__,
  729                     error));
  730                 /* Compare the whole on-disk allocation table with what's
  731                  * currently in memory */
  732                 while (n < sc->chunk_count) {
  733                         buf = g_read_data(sc->components[0].gcons, off,
  734                             sc->sectorsize, &error);
  735                         KASSERT(buf != NULL, ("g_read_data returned NULL (%d) "
  736                             "for read at %jd", error, off));
  737                         if (bcmp(buf, &sc->map[n], sc->sectorsize) != 0) {
  738                                 LOG_MSG(LVL_ERROR, "ERROR in allocation table, "
  739                                     "entry %d, offset %jd", n, off);
  740                                 isclean = 0;
  741                                 count++;
  742                         }
  743                         n += sc->me_per_sector;
  744                         off += sc->sectorsize;
  745                         g_free(buf);
  746                 }
  747                 error = g_access(sc->components[0].gcons, -1, 0, 0);
  748                 KASSERT(error == 0, ("%s: g_access failed (%d) on exit",
  749                     __func__, error));
  750                 if (isclean != 1) {
  751                         LOG_MSG(LVL_ERROR, "ALLOCATION TABLE CORRUPTED FOR %s "
  752                             "(%d sectors don't match, max %zu allocations)",
  753                             sc->geom->name, count,
  754                             count * sc->me_per_sector);
  755                 } else {
  756                         LOG_MSG(LVL_INFO, "Allocation table ok for %s",
  757                             sc->geom->name);
  758                 }
  759 bailout:
  760 #endif
  761                 update_metadata(sc);
  762                 virstor_geom_destroy(sc, FALSE, FALSE);
  763                 exitval = EAGAIN;
  764         } else
  765                 exitval = 0;
  766         return (exitval);
  767 }
  768 
  769 /*
  770  * Taste event (per-class callback)
  771  * Examines a provider and creates geom instances if needed
  772  */
  773 static struct g_geom *
  774 g_virstor_taste(struct g_class *mp, struct g_provider *pp, int flags)
  775 {
  776         struct g_virstor_metadata md;
  777         struct g_geom *gp;
  778         struct g_consumer *cp;
  779         struct g_virstor_softc *sc;
  780         int error;
  781 
  782         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
  783         g_topology_assert();
  784         LOG_MSG(LVL_DEBUG, "Tasting %s", pp->name);
  785 
  786         /* We need a dummy geom to attach a consumer to the given provider */
  787         gp = g_new_geomf(mp, "virstor:taste.helper");
  788         gp->start = (void *)invalid_call;       /* XXX: hacked up so the        */
  789         gp->access = (void *)invalid_call;      /* compiler doesn't complain.   */
  790         gp->orphan = (void *)invalid_call;      /* I really want these to fail. */
  791 
  792         cp = g_new_consumer(gp);
  793         g_attach(cp, pp);
  794         error = read_metadata(cp, &md);
  795         g_detach(cp);
  796         g_destroy_consumer(cp);
  797         g_destroy_geom(gp);
  798 
  799         if (error != 0)
  800                 return (NULL);
  801 
  802         if (strcmp(md.md_magic, G_VIRSTOR_MAGIC) != 0)
  803                 return (NULL);
  804         if (md.md_version != G_VIRSTOR_VERSION) {
  805                 LOG_MSG(LVL_ERROR, "Kernel module version invalid "
  806                     "to handle %s (%s) : %d should be %d",
  807                     md.md_name, pp->name, md.md_version, G_VIRSTOR_VERSION);
  808                 return (NULL);
  809         }
  810         if (md.provsize != pp->mediasize)
  811                 return (NULL);
  812 
  813         /* If the provider name is hardcoded, use the offered provider only
  814          * if it's been offered with its proper name (the one used in
  815          * the label command). */
  816         if (md.provider[0] != '\0' &&
  817             !g_compare_names(md.provider, pp->name))
  818                 return (NULL);
  819 
  820         /* Iterate all geoms this class already knows about to see if a new
  821          * geom instance of this class needs to be created (in case the provider
  822          * is first from a (possibly) multi-consumer geom) or it just needs
  823          * to be added to an existing instance. */
  824         sc = NULL;
  825         gp = NULL;
  826         LIST_FOREACH(gp, &mp->geom, geom) {
  827                 sc = gp->softc;
  828                 if (sc == NULL)
  829                         continue;
  830                 if (strcmp(md.md_name, sc->geom->name) != 0)
  831                         continue;
  832                 if (md.md_id != sc->id)
  833                         continue;
  834                 break;
  835         }
  836         if (gp != NULL) { /* We found an existing geom instance; add to it */
  837                 LOG_MSG(LVL_INFO, "Adding %s to %s", pp->name, md.md_name);
  838                 error = add_provider_to_geom(sc, pp, &md);
  839                 if (error != 0) {
  840                         LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
  841                             pp->name, md.md_name, error);
  842                         return (NULL);
  843                 }
  844         } else { /* New geom instance needs to be created */
  845                 gp = create_virstor_geom(mp, &md);
  846                 if (gp == NULL) {
  847                         LOG_MSG(LVL_ERROR, "Error creating new instance of "
  848                             "class %s: %s", mp->name, md.md_name);
  849                         LOG_MSG(LVL_DEBUG, "Error creating %s at %s",
  850                             md.md_name, pp->name);
  851                         return (NULL);
  852                 }
  853                 sc = gp->softc;
  854                 LOG_MSG(LVL_INFO, "Adding %s to %s (first found)", pp->name,
  855                     md.md_name);
  856                 error = add_provider_to_geom(sc, pp, &md);
  857                 if (error != 0) {
  858                         LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
  859                             pp->name, md.md_name, error);
  860                         virstor_geom_destroy(sc, TRUE, FALSE);
  861                         return (NULL);
  862                 }
  863         }
  864 
  865         return (gp);
  866 }
  867 
  868 /*
  869  * Destroyes consumer passed to it in arguments. Used as a callback
  870  * on g_event queue.
  871  */
  872 static void
  873 delay_destroy_consumer(void *arg, int flags __unused)
  874 {
  875         struct g_consumer *c = arg;
  876         KASSERT(c != NULL, ("%s: invalid consumer", __func__));
  877         LOG_MSG(LVL_DEBUG, "Consumer %s destroyed with delay",
  878             c->provider->name);
  879         g_detach(c);
  880         g_destroy_consumer(c);
  881 }
  882 
  883 /*
  884  * Remove a component (consumer) from geom instance; If it's the first
  885  * component being removed, orphan the provider to announce geom's being
  886  * dismantled
  887  */
  888 static void
  889 remove_component(struct g_virstor_softc *sc, struct g_virstor_component *comp,
  890     boolean_t delay)
  891 {
  892         struct g_consumer *c;
  893 
  894         KASSERT(comp->gcons != NULL, ("Component with no consumer in %s",
  895             sc->geom->name));
  896         c = comp->gcons;
  897 
  898         comp->gcons = NULL;
  899         KASSERT(c->provider != NULL, ("%s: no provider", __func__));
  900         LOG_MSG(LVL_DEBUG, "Component %s removed from %s", c->provider->name,
  901             sc->geom->name);
  902         if (sc->provider != NULL) {
  903                 /* Whither, GEOM? */
  904                 sc->provider->flags |= G_PF_WITHER;
  905                 g_orphan_provider(sc->provider, ENXIO);
  906                 sc->provider = NULL;
  907                 LOG_MSG(LVL_INFO, "Removing provider %s", sc->geom->name);
  908         }
  909 
  910         if (c->acr > 0 || c->acw > 0 || c->ace > 0)
  911                 g_access(c, -c->acr, -c->acw, -c->ace);
  912         if (delay) {
  913                 /* Destroy consumer after it's tasted */
  914                 g_post_event(delay_destroy_consumer, c, M_WAITOK, NULL);
  915         } else {
  916                 g_detach(c);
  917                 g_destroy_consumer(c);
  918         }
  919 }
  920 
  921 /*
  922  * Destroy geom - called internally
  923  * See g_virstor_destroy_geom for the other one
  924  */
  925 static int
  926 virstor_geom_destroy(struct g_virstor_softc *sc, boolean_t force,
  927     boolean_t delay)
  928 {
  929         struct g_provider *pp;
  930         struct g_geom *gp;
  931         int n;
  932 
  933         g_topology_assert();
  934 
  935         if (sc == NULL)
  936                 return (ENXIO);
  937 
  938         pp = sc->provider;
  939         if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
  940                 LOG_MSG(force ? LVL_WARNING : LVL_ERROR,
  941                     "Device %s is still open.", pp->name);
  942                 if (!force)
  943                         return (EBUSY);
  944         }
  945 
  946         for (n = 0; n < sc->n_components; n++) {
  947                 if (sc->components[n].gcons != NULL)
  948                         remove_component(sc, &sc->components[n], delay);
  949         }
  950 
  951         gp = sc->geom;
  952         gp->softc = NULL;
  953 
  954         KASSERT(sc->provider == NULL, ("Provider still exists for %s",
  955             gp->name));
  956 
  957         /* XXX: This might or might not work, since we're called with
  958          * the topology lock held. Also, it might panic the kernel if
  959          * the error'd BIO is in softupdates code. */
  960         mtx_lock(&sc->delayed_bio_q_mtx);
  961         while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
  962                 struct g_virstor_bio_q *bq;
  963                 bq = STAILQ_FIRST(&sc->delayed_bio_q);
  964                 bq->bio->bio_error = ENOSPC;
  965                 g_io_deliver(bq->bio, EIO);
  966                 STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
  967                 free(bq, M_GVIRSTOR);
  968         }
  969         mtx_unlock(&sc->delayed_bio_q_mtx);
  970         mtx_destroy(&sc->delayed_bio_q_mtx);
  971 
  972         free(sc->map, M_GVIRSTOR);
  973         free(sc->components, M_GVIRSTOR);
  974         bzero(sc, sizeof *sc);
  975         free(sc, M_GVIRSTOR);
  976 
  977         pp = LIST_FIRST(&gp->provider); /* We only offer one provider */
  978         if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
  979                 LOG_MSG(LVL_DEBUG, "Device %s destroyed", gp->name);
  980 
  981         g_wither_geom(gp, ENXIO);
  982 
  983         return (0);
  984 }
  985 
  986 /*
  987  * Utility function: read metadata & decode. Wants topology lock to be
  988  * held.
  989  */
  990 static int
  991 read_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
  992 {
  993         struct g_provider *pp;
  994         char *buf;
  995         int error;
  996 
  997         g_topology_assert();
  998         error = g_access(cp, 1, 0, 0);
  999         if (error != 0)
 1000                 return (error);
 1001         pp = cp->provider;
 1002         g_topology_unlock();
 1003         buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
 1004             &error);
 1005         g_topology_lock();
 1006         g_access(cp, -1, 0, 0);
 1007         if (buf == NULL)
 1008                 return (error);
 1009 
 1010         virstor_metadata_decode(buf, md);
 1011         g_free(buf);
 1012 
 1013         return (0);
 1014 }
 1015 
 1016 /**
 1017  * Utility function: encode & write metadata. Assumes topology lock is
 1018  * held.
 1019  */
 1020 static int
 1021 write_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
 1022 {
 1023         struct g_provider *pp;
 1024         char *buf;
 1025         int error;
 1026 
 1027         KASSERT(cp != NULL && md != NULL && cp->provider != NULL,
 1028             ("Something's fishy in %s", __func__));
 1029         LOG_MSG(LVL_DEBUG, "Writing metadata on %s", cp->provider->name);
 1030         g_topology_assert();
 1031         error = g_access(cp, 0, 1, 0);
 1032         if (error != 0)
 1033                 return (error);
 1034         pp = cp->provider;
 1035 
 1036         buf = malloc(pp->sectorsize, M_GVIRSTOR, M_WAITOK);
 1037         virstor_metadata_encode(md, buf);
 1038         g_topology_unlock();
 1039         error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf,
 1040             pp->sectorsize);
 1041         g_topology_lock();
 1042         g_access(cp, 0, -1, 0);
 1043 
 1044         free(buf, M_GVIRSTOR);
 1045         return (0);
 1046 }
 1047 
 1048 /*
 1049  * Creates a new instance of this GEOM class, initialise softc
 1050  */
 1051 static struct g_geom *
 1052 create_virstor_geom(struct g_class *mp, struct g_virstor_metadata *md)
 1053 {
 1054         struct g_geom *gp;
 1055         struct g_virstor_softc *sc;
 1056 
 1057         LOG_MSG(LVL_DEBUG, "Creating geom instance for %s (id=%u)",
 1058             md->md_name, md->md_id);
 1059 
 1060         if (md->md_count < 1 || md->md_chunk_size < 1 ||
 1061             md->md_virsize < md->md_chunk_size) {
 1062                 /* This is bogus configuration, and probably means data is
 1063                  * somehow corrupted. Panic, maybe? */
 1064                 LOG_MSG(LVL_ERROR, "Nonsensical metadata information for %s",
 1065                     md->md_name);
 1066                 return (NULL);
 1067         }
 1068 
 1069         /* Check if it's already created */
 1070         LIST_FOREACH(gp, &mp->geom, geom) {
 1071                 sc = gp->softc;
 1072                 if (sc != NULL && strcmp(sc->geom->name, md->md_name) == 0) {
 1073                         LOG_MSG(LVL_WARNING, "Geom %s already exists",
 1074                             md->md_name);
 1075                         if (sc->id != md->md_id) {
 1076                                 LOG_MSG(LVL_ERROR,
 1077                                     "Some stale or invalid components "
 1078                                     "exist for virstor device named %s. "
 1079                                     "You will need to <CLEAR> all stale "
 1080                                     "components and maybe reconfigure "
 1081                                     "the virstor device. Tune "
 1082                                     "kern.geom.virstor.debug sysctl up "
 1083                                     "for more information.",
 1084                                     sc->geom->name);
 1085                         }
 1086                         return (NULL);
 1087                 }
 1088         }
 1089         gp = g_new_geomf(mp, "%s", md->md_name);
 1090         gp->softc = NULL; /* to circumevent races that test softc */
 1091 
 1092         gp->start = g_virstor_start;
 1093         gp->spoiled = g_virstor_orphan;
 1094         gp->orphan = g_virstor_orphan;
 1095         gp->access = g_virstor_access;
 1096         gp->dumpconf = g_virstor_dumpconf;
 1097 
 1098         sc = malloc(sizeof(*sc), M_GVIRSTOR, M_WAITOK | M_ZERO);
 1099         sc->id = md->md_id;
 1100         sc->n_components = md->md_count;
 1101         sc->components = malloc(sizeof(struct g_virstor_component) * md->md_count,
 1102             M_GVIRSTOR, M_WAITOK | M_ZERO);
 1103         sc->chunk_size = md->md_chunk_size;
 1104         sc->virsize = md->md_virsize;
 1105         STAILQ_INIT(&sc->delayed_bio_q);
 1106         mtx_init(&sc->delayed_bio_q_mtx, "gvirstor_delayed_bio_q_mtx",
 1107             "gvirstor", MTX_DEF | MTX_RECURSE);
 1108 
 1109         sc->geom = gp;
 1110         sc->provider = NULL; /* virstor_check_and_run will create it */
 1111         gp->softc = sc;
 1112 
 1113         LOG_MSG(LVL_ANNOUNCE, "Device %s created", sc->geom->name);
 1114 
 1115         return (gp);
 1116 }
 1117 
 1118 /*
 1119  * Add provider to a GEOM class instance
 1120  */
 1121 static int
 1122 add_provider_to_geom(struct g_virstor_softc *sc, struct g_provider *pp,
 1123     struct g_virstor_metadata *md)
 1124 {
 1125         struct g_virstor_component *component;
 1126         struct g_consumer *cp, *fcp;
 1127         struct g_geom *gp;
 1128         int error;
 1129 
 1130         if (md->no >= sc->n_components)
 1131                 return (EINVAL);
 1132 
 1133         /* "Current" compontent */
 1134         component = &(sc->components[md->no]);
 1135         if (component->gcons != NULL)
 1136                 return (EEXIST);
 1137 
 1138         gp = sc->geom;
 1139         fcp = LIST_FIRST(&gp->consumer);
 1140 
 1141         cp = g_new_consumer(gp);
 1142         error = g_attach(cp, pp);
 1143 
 1144         if (error != 0) {
 1145                 g_destroy_consumer(cp);
 1146                 return (error);
 1147         }
 1148 
 1149         if (fcp != NULL) {
 1150                 if (fcp->provider->sectorsize != pp->sectorsize) {
 1151                         /* TODO: this can be made to work */
 1152                         LOG_MSG(LVL_ERROR, "Provider %s of %s has invalid "
 1153                             "sector size (%d)", pp->name, sc->geom->name,
 1154                             pp->sectorsize);
 1155                         return (EINVAL);
 1156                 }
 1157                 if (fcp->acr > 0 || fcp->acw || fcp->ace > 0) {
 1158                         /* Replicate access permissions from first "live" consumer
 1159                          * to the new one */
 1160                         error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
 1161                         if (error != 0) {
 1162                                 g_detach(cp);
 1163                                 g_destroy_consumer(cp);
 1164                                 return (error);
 1165                         }
 1166                 }
 1167         }
 1168 
 1169         /* Bring up a new component */
 1170         cp->private = component;
 1171         component->gcons = cp;
 1172         component->sc = sc;
 1173         component->index = md->no;
 1174         component->chunk_count = md->chunk_count;
 1175         component->chunk_next = md->chunk_next;
 1176         component->chunk_reserved = md->chunk_reserved;
 1177         component->flags = md->flags;
 1178 
 1179         LOG_MSG(LVL_DEBUG, "%s attached to %s", pp->name, sc->geom->name);
 1180 
 1181         virstor_check_and_run(sc);
 1182         return (0);
 1183 }
 1184 
 1185 /*
 1186  * Check if everything's ready to create the geom provider & device entry,
 1187  * create and start provider.
 1188  * Called ultimately by .taste, from g_event thread
 1189  */
 1190 static void
 1191 virstor_check_and_run(struct g_virstor_softc *sc)
 1192 {
 1193         off_t off;
 1194         size_t n, count;
 1195         int index;
 1196         int error;
 1197 
 1198         if (virstor_valid_components(sc) != sc->n_components)
 1199                 return;
 1200 
 1201         if (virstor_valid_components(sc) == 0) {
 1202                 /* This is actually a candidate for panic() */
 1203                 LOG_MSG(LVL_ERROR, "No valid components for %s?",
 1204                     sc->provider->name);
 1205                 return;
 1206         }
 1207 
 1208         sc->sectorsize = sc->components[0].gcons->provider->sectorsize;
 1209 
 1210         /* Initialise allocation map from the first consumer */
 1211         sc->chunk_count = sc->virsize / sc->chunk_size;
 1212         if (sc->chunk_count * (off_t)sc->chunk_size != sc->virsize) {
 1213                 LOG_MSG(LVL_WARNING, "Device %s truncated to %ju bytes",
 1214                     sc->provider->name,
 1215                     sc->chunk_count * (off_t)sc->chunk_size);
 1216         }
 1217         sc->map_size = sc->chunk_count * sizeof *(sc->map);
 1218         /* The following allocation is in order of 4MB - 8MB */
 1219         sc->map = malloc(sc->map_size, M_GVIRSTOR, M_WAITOK);
 1220         KASSERT(sc->map != NULL, ("%s: Memory allocation error (%zu bytes) for %s",
 1221             __func__, sc->map_size, sc->provider->name));
 1222         sc->map_sectors = sc->map_size / sc->sectorsize;
 1223 
 1224         count = 0;
 1225         for (n = 0; n < sc->n_components; n++)
 1226                 count += sc->components[n].chunk_count;
 1227         LOG_MSG(LVL_INFO, "Device %s has %zu physical chunks and %zu virtual "
 1228             "(%zu KB chunks)",
 1229             sc->geom->name, count, sc->chunk_count, sc->chunk_size / 1024);
 1230 
 1231         error = g_access(sc->components[0].gcons, 1, 0, 0);
 1232         if (error != 0) {
 1233                 LOG_MSG(LVL_ERROR, "Cannot acquire read access for %s to "
 1234                     "read allocation map for %s",
 1235                     sc->components[0].gcons->provider->name,
 1236                     sc->geom->name);
 1237                 return;
 1238         }
 1239         /* Read in the allocation map */
 1240         LOG_MSG(LVL_DEBUG, "Reading map for %s from %s", sc->geom->name,
 1241             sc->components[0].gcons->provider->name);
 1242         off = count = n = 0;
 1243         while (count < sc->map_size) {
 1244                 struct g_virstor_map_entry *mapbuf;
 1245                 size_t bs;
 1246 
 1247                 bs = MIN(MAXPHYS, sc->map_size - count);
 1248                 if (bs % sc->sectorsize != 0) {
 1249                         /* Check for alignment errors */
 1250                         bs = (bs / sc->sectorsize) * sc->sectorsize;
 1251                         if (bs == 0)
 1252                                 break;
 1253                         LOG_MSG(LVL_ERROR, "Trouble: map is not sector-aligned "
 1254                             "for %s on %s", sc->geom->name,
 1255                             sc->components[0].gcons->provider->name);
 1256                 }
 1257                 mapbuf = g_read_data(sc->components[0].gcons, off, bs, &error);
 1258                 if (mapbuf == NULL) {
 1259                         free(sc->map, M_GVIRSTOR);
 1260                         LOG_MSG(LVL_ERROR, "Error reading allocation map "
 1261                             "for %s from %s (offset %ju) (error %d)",
 1262                             sc->geom->name,
 1263                             sc->components[0].gcons->provider->name,
 1264                             off, error);
 1265                         return;
 1266                 }
 1267 
 1268                 bcopy(mapbuf, &sc->map[n], bs);
 1269                 off += bs;
 1270                 count += bs;
 1271                 n += bs / sizeof *(sc->map);
 1272                 g_free(mapbuf);
 1273         }
 1274         g_access(sc->components[0].gcons, -1, 0, 0);
 1275         LOG_MSG(LVL_DEBUG, "Read map for %s", sc->geom->name);
 1276 
 1277         /* find first component with allocatable chunks */
 1278         index = -1;
 1279         for (n = 0; n < sc->n_components; n++) {
 1280                 if (sc->components[n].chunk_next <
 1281                     sc->components[n].chunk_count) {
 1282                         index = n;
 1283                         break;
 1284                 }
 1285         }
 1286         if (index == -1)
 1287                 /* not found? set it to the last component and handle it
 1288                  * later */
 1289                 index = sc->n_components - 1;
 1290 
 1291         if (index >= sc->n_components - g_virstor_component_watermark - 1) {
 1292                 LOG_MSG(LVL_WARNING, "Device %s running out of components "
 1293                     "(%d/%u: %s)", sc->geom->name,
 1294                     index+1,
 1295                     sc->n_components,
 1296                     sc->components[index].gcons->provider->name);
 1297         }
 1298         sc->curr_component = index;
 1299 
 1300         if (sc->components[index].chunk_next >=
 1301             sc->components[index].chunk_count - g_virstor_chunk_watermark) {
 1302                 LOG_MSG(LVL_WARNING,
 1303                     "Component %s of %s is running out of free space "
 1304                     "(%u chunks left)",
 1305                     sc->components[index].gcons->provider->name,
 1306                     sc->geom->name, sc->components[index].chunk_count -
 1307                     sc->components[index].chunk_next);
 1308         }
 1309 
 1310         sc->me_per_sector = sc->sectorsize / sizeof *(sc->map);
 1311         if (sc->sectorsize % sizeof *(sc->map) != 0) {
 1312                 LOG_MSG(LVL_ERROR,
 1313                     "%s: Map entries don't fit exactly in a sector (%s)",
 1314                     __func__, sc->geom->name);
 1315                 return;
 1316         }
 1317 
 1318         /* Recalculate allocated chunks in components & at the same time
 1319          * verify map data is sane. We could trust metadata on this, but
 1320          * we want to make sure. */
 1321         for (n = 0; n < sc->n_components; n++)
 1322                 sc->components[n].chunk_next = sc->components[n].chunk_reserved;
 1323 
 1324         for (n = 0; n < sc->chunk_count; n++) {
 1325                 if (sc->map[n].provider_no >= sc->n_components ||
 1326                         sc->map[n].provider_chunk >=
 1327                         sc->components[sc->map[n].provider_no].chunk_count) {
 1328                         LOG_MSG(LVL_ERROR, "%s: Invalid entry %u in map for %s",
 1329                             __func__, (u_int)n, sc->geom->name);
 1330                         LOG_MSG(LVL_ERROR, "%s: provider_no: %u, n_components: %u"
 1331                             " provider_chunk: %u, chunk_count: %u", __func__,
 1332                             sc->map[n].provider_no, sc->n_components,
 1333                             sc->map[n].provider_chunk,
 1334                             sc->components[sc->map[n].provider_no].chunk_count);
 1335                         return;
 1336                 }
 1337                 if (sc->map[n].flags & VIRSTOR_MAP_ALLOCATED)
 1338                         sc->components[sc->map[n].provider_no].chunk_next++;
 1339         }
 1340 
 1341         sc->provider = g_new_providerf(sc->geom, "virstor/%s",
 1342             sc->geom->name);
 1343 
 1344         sc->provider->sectorsize = sc->sectorsize;
 1345         sc->provider->mediasize = sc->virsize;
 1346         g_error_provider(sc->provider, 0);
 1347 
 1348         LOG_MSG(LVL_INFO, "%s activated", sc->provider->name);
 1349         LOG_MSG(LVL_DEBUG, "%s starting with current component %u, starting "
 1350             "chunk %u", sc->provider->name, sc->curr_component,
 1351             sc->components[sc->curr_component].chunk_next);
 1352 }
 1353 
 1354 /*
 1355  * Returns count of active providers in this geom instance
 1356  */
 1357 static u_int
 1358 virstor_valid_components(struct g_virstor_softc *sc)
 1359 {
 1360         unsigned int nc, i;
 1361 
 1362         nc = 0;
 1363         KASSERT(sc != NULL, ("%s: softc is NULL", __func__));
 1364         KASSERT(sc->components != NULL, ("%s: sc->components is NULL", __func__));
 1365         for (i = 0; i < sc->n_components; i++)
 1366                 if (sc->components[i].gcons != NULL)
 1367                         nc++;
 1368         return (nc);
 1369 }
 1370 
 1371 /*
 1372  * Called when the consumer gets orphaned (?)
 1373  */
 1374 static void
 1375 g_virstor_orphan(struct g_consumer *cp)
 1376 {
 1377         struct g_virstor_softc *sc;
 1378         struct g_virstor_component *comp;
 1379         struct g_geom *gp;
 1380 
 1381         g_topology_assert();
 1382         gp = cp->geom;
 1383         sc = gp->softc;
 1384         if (sc == NULL)
 1385                 return;
 1386 
 1387         comp = cp->private;
 1388         KASSERT(comp != NULL, ("%s: No component in private part of consumer",
 1389             __func__));
 1390         remove_component(sc, comp, FALSE);
 1391         if (virstor_valid_components(sc) == 0)
 1392                 virstor_geom_destroy(sc, TRUE, FALSE);
 1393 }
 1394 
 1395 /*
 1396  * Called to notify geom when it's been opened, and for what intent
 1397  */
 1398 static int
 1399 g_virstor_access(struct g_provider *pp, int dr, int dw, int de)
 1400 {
 1401         struct g_consumer *c;
 1402         struct g_virstor_softc *sc;
 1403         struct g_geom *gp;
 1404         int error;
 1405 
 1406         KASSERT(pp != NULL, ("%s: NULL provider", __func__));
 1407         gp = pp->geom;
 1408         KASSERT(gp != NULL, ("%s: NULL geom", __func__));
 1409         sc = gp->softc;
 1410 
 1411         if (sc == NULL) {
 1412                 /* It seems that .access can be called with negative dr,dw,dx
 1413                  * in this case but I want to check for myself */
 1414                 LOG_MSG(LVL_WARNING, "access(%d, %d, %d) for %s",
 1415                     dr, dw, de, pp->name);
 1416                 /* This should only happen when geom is withered so
 1417                  * allow only negative requests */
 1418                 KASSERT(dr <= 0 && dw <= 0 && de <= 0,
 1419                     ("%s: Positive access for %s", __func__, pp->name));
 1420                 if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0)
 1421                         LOG_MSG(LVL_DEBUG, "Device %s definitely destroyed",
 1422                             pp->name);
 1423                 return (0);
 1424         }
 1425 
 1426         /* Grab an exclusive bit to propagate on our consumers on first open */
 1427         if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
 1428                 de++;
 1429         /* ... drop it on close */
 1430         if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0) {
 1431                 de--;
 1432                 update_metadata(sc);    /* Writes statistical information */
 1433         }
 1434 
 1435         error = ENXIO;
 1436         LIST_FOREACH(c, &gp->consumer, consumer) {
 1437                 KASSERT(c != NULL, ("%s: consumer is NULL", __func__));
 1438                 error = g_access(c, dr, dw, de);
 1439                 if (error != 0) {
 1440                         struct g_consumer *c2;
 1441 
 1442                         /* Backout earlier changes */
 1443                         LIST_FOREACH(c2, &gp->consumer, consumer) {
 1444                                 if (c2 == c) /* all eariler components fixed */
 1445                                         return (error);
 1446                                 g_access(c2, -dr, -dw, -de);
 1447                         }
 1448                 }
 1449         }
 1450 
 1451         return (error);
 1452 }
 1453 
 1454 /*
 1455  * Generate XML dump of current state
 1456  */
 1457 static void
 1458 g_virstor_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
 1459     struct g_consumer *cp, struct g_provider *pp)
 1460 {
 1461         struct g_virstor_softc *sc;
 1462 
 1463         g_topology_assert();
 1464         sc = gp->softc;
 1465 
 1466         if (sc == NULL || pp != NULL)
 1467                 return;
 1468 
 1469         if (cp != NULL) {
 1470                 /* For each component */
 1471                 struct g_virstor_component *comp;
 1472 
 1473                 comp = cp->private;
 1474                 if (comp == NULL)
 1475                         return;
 1476                 sbuf_printf(sb, "%s<ComponentIndex>%u</ComponentIndex>\n",
 1477                     indent, comp->index);
 1478                 sbuf_printf(sb, "%s<ChunkCount>%u</ChunkCount>\n",
 1479                     indent, comp->chunk_count);
 1480                 sbuf_printf(sb, "%s<ChunksUsed>%u</ChunksUsed>\n",
 1481                     indent, comp->chunk_next);
 1482                 sbuf_printf(sb, "%s<ChunksReserved>%u</ChunksReserved>\n",
 1483                     indent, comp->chunk_reserved);
 1484                 sbuf_printf(sb, "%s<StorageFree>%u%%</StorageFree>\n",
 1485                     indent,
 1486                     comp->chunk_next > 0 ? 100 -
 1487                     ((comp->chunk_next + comp->chunk_reserved) * 100) /
 1488                     comp->chunk_count : 100);
 1489         } else {
 1490                 /* For the whole thing */
 1491                 u_int count, used, i;
 1492                 off_t size;
 1493 
 1494                 count = used = size = 0;
 1495                 for (i = 0; i < sc->n_components; i++) {
 1496                         if (sc->components[i].gcons != NULL) {
 1497                                 count += sc->components[i].chunk_count;
 1498                                 used += sc->components[i].chunk_next +
 1499                                     sc->components[i].chunk_reserved;
 1500                                 size += sc->components[i].gcons->
 1501                                     provider->mediasize;
 1502                         }
 1503                 }
 1504 
 1505                 sbuf_printf(sb, "%s<Status>"
 1506                     "Components=%u, Online=%u</Status>\n", indent,
 1507                     sc->n_components, virstor_valid_components(sc));
 1508                 sbuf_printf(sb, "%s<State>%u%% physical free</State>\n",
 1509                     indent, 100-(used * 100) / count);
 1510                 sbuf_printf(sb, "%s<ChunkSize>%zu</ChunkSize>\n", indent,
 1511                     sc->chunk_size);
 1512                 sbuf_printf(sb, "%s<PhysicalFree>%u%%</PhysicalFree>\n",
 1513                     indent, used > 0 ? 100 - (used * 100) / count : 100);
 1514                 sbuf_printf(sb, "%s<ChunkPhysicalCount>%u</ChunkPhysicalCount>\n",
 1515                     indent, count);
 1516                 sbuf_printf(sb, "%s<ChunkVirtualCount>%zu</ChunkVirtualCount>\n",
 1517                     indent, sc->chunk_count);
 1518                 sbuf_printf(sb, "%s<PhysicalBacking>%zu%%</PhysicalBacking>\n",
 1519                     indent,
 1520                     (count * 100) / sc->chunk_count);
 1521                 sbuf_printf(sb, "%s<PhysicalBackingSize>%jd</PhysicalBackingSize>\n",
 1522                     indent, size);
 1523                 sbuf_printf(sb, "%s<VirtualSize>%jd</VirtualSize>\n", indent,
 1524                     sc->virsize);
 1525         }
 1526 }
 1527 
 1528 /*
 1529  * GEOM .done handler
 1530  * Can't use standard handler because one requested IO may
 1531  * fork into additional data IOs
 1532  */
 1533 static void
 1534 g_virstor_done(struct bio *b)
 1535 {
 1536         struct g_virstor_softc *sc;
 1537         struct bio *parent_b;
 1538 
 1539         parent_b = b->bio_parent;
 1540         sc = parent_b->bio_to->geom->softc;
 1541 
 1542         if (b->bio_error != 0) {
 1543                 LOG_MSG(LVL_ERROR, "Error %d for offset=%ju, length=%ju, %s",
 1544                     b->bio_error, b->bio_offset, b->bio_length,
 1545                     b->bio_to->name);
 1546                 if (parent_b->bio_error == 0)
 1547                         parent_b->bio_error = b->bio_error;
 1548         }
 1549 
 1550         parent_b->bio_inbed++;
 1551         parent_b->bio_completed += b->bio_completed;
 1552 
 1553         if (parent_b->bio_children == parent_b->bio_inbed) {
 1554                 parent_b->bio_completed = parent_b->bio_length;
 1555                 g_io_deliver(parent_b, parent_b->bio_error);
 1556         }
 1557         g_destroy_bio(b);
 1558 }
 1559 
 1560 /*
 1561  * I/O starts here
 1562  * Called in g_down thread
 1563  */
 1564 static void
 1565 g_virstor_start(struct bio *b)
 1566 {
 1567         struct g_virstor_softc *sc;
 1568         struct g_virstor_component *comp;
 1569         struct bio *cb;
 1570         struct g_provider *pp;
 1571         char *addr;
 1572         off_t offset, length;
 1573         struct bio_queue_head bq;
 1574         size_t chunk_size;      /* cached for convenience */
 1575         u_int count;
 1576 
 1577         pp = b->bio_to;
 1578         sc = pp->geom->softc;
 1579         KASSERT(sc != NULL, ("%s: no softc (error=%d, device=%s)", __func__,
 1580             b->bio_to->error, b->bio_to->name));
 1581 
 1582         LOG_REQ(LVL_MOREDEBUG, b, "%s", __func__);
 1583 
 1584         switch (b->bio_cmd) {
 1585         case BIO_READ:
 1586         case BIO_WRITE:
 1587         case BIO_DELETE:
 1588                 break;
 1589         default:
 1590                 g_io_deliver(b, EOPNOTSUPP);
 1591                 return;
 1592         }
 1593 
 1594         LOG_MSG(LVL_DEBUG2, "BIO arrived, size=%ju", b->bio_length);
 1595         bioq_init(&bq);
 1596 
 1597         chunk_size = sc->chunk_size;
 1598         addr = b->bio_data;
 1599         offset = b->bio_offset; /* virtual offset and length */
 1600         length = b->bio_length;
 1601 
 1602         while (length > 0) {
 1603                 size_t chunk_index, in_chunk_offset, in_chunk_length;
 1604                 struct virstor_map_entry *me;
 1605 
 1606                 chunk_index = offset / chunk_size; /* round downwards */
 1607                 in_chunk_offset = offset % chunk_size;
 1608                 in_chunk_length = min(length, chunk_size - in_chunk_offset);
 1609                 LOG_MSG(LVL_DEBUG, "Mapped %s(%ju, %ju) to (%zu,%zu,%zu)",
 1610                     b->bio_cmd == BIO_READ ? "R" : "W",
 1611                     offset, length,
 1612                     chunk_index, in_chunk_offset, in_chunk_length);
 1613                 me = &sc->map[chunk_index];
 1614 
 1615                 if (b->bio_cmd == BIO_READ || b->bio_cmd == BIO_DELETE) {
 1616                         if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
 1617                                 /* Reads from unallocated chunks return zeroed
 1618                                  * buffers */
 1619                                 if (b->bio_cmd == BIO_READ)
 1620                                         bzero(addr, in_chunk_length);
 1621                         } else {
 1622                                 comp = &sc->components[me->provider_no];
 1623 
 1624                                 cb = g_clone_bio(b);
 1625                                 if (cb == NULL) {
 1626                                         bioq_dismantle(&bq);
 1627                                         if (b->bio_error == 0)
 1628                                                 b->bio_error = ENOMEM;
 1629                                         g_io_deliver(b, b->bio_error);
 1630                                         return;
 1631                                 }
 1632                                 cb->bio_to = comp->gcons->provider;
 1633                                 cb->bio_done = g_virstor_done;
 1634                                 cb->bio_offset =
 1635                                     (off_t)me->provider_chunk * (off_t)chunk_size
 1636                                     + in_chunk_offset;
 1637                                 cb->bio_length = in_chunk_length;
 1638                                 cb->bio_data = addr;
 1639                                 cb->bio_caller1 = comp;
 1640                                 bioq_disksort(&bq, cb);
 1641                         }
 1642                 } else { /* handle BIO_WRITE */
 1643                         KASSERT(b->bio_cmd == BIO_WRITE,
 1644                             ("%s: Unknown command %d", __func__,
 1645                             b->bio_cmd));
 1646 
 1647                         if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
 1648                                 /* We have a virtual chunk, represented by
 1649                                  * the "me" entry, but it's not yet allocated
 1650                                  * (tied to) a physical chunk. So do it now. */
 1651                                 struct virstor_map_entry *data_me;
 1652                                 u_int phys_chunk, comp_no;
 1653                                 off_t s_offset;
 1654                                 int error;
 1655 
 1656                                 error = allocate_chunk(sc, &comp, &comp_no,
 1657                                     &phys_chunk);
 1658                                 if (error != 0) {
 1659                                         /* We cannot allocate a physical chunk
 1660                                          * to satisfy this request, so we'll
 1661                                          * delay it to when we can...
 1662                                          * XXX: this will prevent the fs from
 1663                                          * being umounted! */
 1664                                         struct g_virstor_bio_q *biq;
 1665                                         biq = malloc(sizeof *biq, M_GVIRSTOR,
 1666                                             M_NOWAIT);
 1667                                         if (biq == NULL) {
 1668                                                 bioq_dismantle(&bq);
 1669                                                 if (b->bio_error == 0)
 1670                                                         b->bio_error = ENOMEM;
 1671                                                 g_io_deliver(b, b->bio_error);
 1672                                                 return;
 1673                                         }
 1674                                         biq->bio = b;
 1675                                         mtx_lock(&sc->delayed_bio_q_mtx);
 1676                                         STAILQ_INSERT_TAIL(&sc->delayed_bio_q,
 1677                                             biq, linkage);
 1678                                         mtx_unlock(&sc->delayed_bio_q_mtx);
 1679                                         LOG_MSG(LVL_WARNING, "Delaying BIO "
 1680                                             "(size=%ju) until free physical "
 1681                                             "space can be found on %s",
 1682                                             b->bio_length,
 1683                                             sc->provider->name);
 1684                                         return;
 1685                                 }
 1686                                 LOG_MSG(LVL_DEBUG, "Allocated chunk %u on %s "
 1687                                     "for %s",
 1688                                     phys_chunk,
 1689                                     comp->gcons->provider->name,
 1690                                     sc->provider->name);
 1691 
 1692                                 me->provider_no = comp_no;
 1693                                 me->provider_chunk = phys_chunk;
 1694                                 me->flags |= VIRSTOR_MAP_ALLOCATED;
 1695 
 1696                                 cb = g_clone_bio(b);
 1697                                 if (cb == NULL) {
 1698                                         me->flags &= ~VIRSTOR_MAP_ALLOCATED;
 1699                                         me->provider_no = 0;
 1700                                         me->provider_chunk = 0;
 1701                                         bioq_dismantle(&bq);
 1702                                         if (b->bio_error == 0)
 1703                                                 b->bio_error = ENOMEM;
 1704                                         g_io_deliver(b, b->bio_error);
 1705                                         return;
 1706                                 }
 1707 
 1708                                 /* The allocation table is stored continuously
 1709                                  * at the start of the drive. We need to
 1710                                  * calculate the offset of the sector that holds
 1711                                  * this map entry both on the drive and in the
 1712                                  * map array.
 1713                                  * sc_offset will end up pointing to the drive
 1714                                  * sector. */
 1715                                 s_offset = chunk_index * sizeof *me;
 1716                                 s_offset = (s_offset / sc->sectorsize) *
 1717                                     sc->sectorsize;
 1718 
 1719                                 /* data_me points to map entry sector
 1720                                  * in memory (analoguos to offset) */
 1721                                 data_me = &sc->map[(chunk_index /
 1722                                     sc->me_per_sector) * sc->me_per_sector];
 1723 
 1724                                 /* Commit sector with map entry to storage */
 1725                                 cb->bio_to = sc->components[0].gcons->provider;
 1726                                 cb->bio_done = g_virstor_done;
 1727                                 cb->bio_offset = s_offset;
 1728                                 cb->bio_data = (char *)data_me;
 1729                                 cb->bio_length = sc->sectorsize;
 1730                                 cb->bio_caller1 = &sc->components[0];
 1731                                 bioq_disksort(&bq, cb);
 1732                         }
 1733 
 1734                         comp = &sc->components[me->provider_no];
 1735                         cb = g_clone_bio(b);
 1736                         if (cb == NULL) {
 1737                                 bioq_dismantle(&bq);
 1738                                 if (b->bio_error == 0)
 1739                                         b->bio_error = ENOMEM;
 1740                                 g_io_deliver(b, b->bio_error);
 1741                                 return;
 1742                         }
 1743                         /* Finally, handle the data */
 1744                         cb->bio_to = comp->gcons->provider;
 1745                         cb->bio_done = g_virstor_done;
 1746                         cb->bio_offset = (off_t)me->provider_chunk*(off_t)chunk_size +
 1747                             in_chunk_offset;
 1748                         cb->bio_length = in_chunk_length;
 1749                         cb->bio_data = addr;
 1750                         cb->bio_caller1 = comp;
 1751                         bioq_disksort(&bq, cb);
 1752                 }
 1753                 addr += in_chunk_length;
 1754                 length -= in_chunk_length;
 1755                 offset += in_chunk_length;
 1756         }
 1757 
 1758         /* Fire off bio's here */
 1759         count = 0;
 1760         for (cb = bioq_first(&bq); cb != NULL; cb = bioq_first(&bq)) {
 1761                 bioq_remove(&bq, cb);
 1762                 LOG_REQ(LVL_MOREDEBUG, cb, "Firing request");
 1763                 comp = cb->bio_caller1;
 1764                 cb->bio_caller1 = NULL;
 1765                 LOG_MSG(LVL_DEBUG, " firing bio, offset=%ju, length=%ju",
 1766                     cb->bio_offset, cb->bio_length);
 1767                 g_io_request(cb, comp->gcons);
 1768                 count++;
 1769         }
 1770         if (count == 0) { /* We handled everything locally */
 1771                 b->bio_completed = b->bio_length;
 1772                 g_io_deliver(b, 0);
 1773         }
 1774 
 1775 }
 1776 
 1777 /*
 1778  * Allocate a chunk from a physical provider. Returns physical component,
 1779  * chunk index relative to the component and the component's index.
 1780  */
 1781 static int
 1782 allocate_chunk(struct g_virstor_softc *sc, struct g_virstor_component **comp,
 1783     u_int *comp_no_p, u_int *chunk)
 1784 {
 1785         u_int comp_no;
 1786 
 1787         KASSERT(sc->curr_component < sc->n_components,
 1788             ("%s: Invalid curr_component: %u",  __func__, sc->curr_component));
 1789 
 1790         comp_no = sc->curr_component;
 1791         *comp = &sc->components[comp_no];
 1792         dump_component(*comp);
 1793         if ((*comp)->chunk_next >= (*comp)->chunk_count) {
 1794                 /* This component is full. Allocate next component */
 1795                 if (comp_no >= sc->n_components-1) {
 1796                         LOG_MSG(LVL_ERROR, "All physical space allocated for %s",
 1797                             sc->geom->name);
 1798                         return (-1);
 1799                 }
 1800                 (*comp)->flags &= ~VIRSTOR_PROVIDER_CURRENT;
 1801                 sc->curr_component = ++comp_no;
 1802 
 1803                 *comp = &sc->components[comp_no];
 1804                 if (comp_no >= sc->n_components - g_virstor_component_watermark-1)
 1805                         LOG_MSG(LVL_WARNING, "Device %s running out of components "
 1806                             "(switching to %u/%u: %s)", sc->geom->name,
 1807                             comp_no+1, sc->n_components,
 1808                             (*comp)->gcons->provider->name);
 1809                 /* Take care not to overwrite reserved chunks */
 1810                 if ( (*comp)->chunk_reserved > 0 &&
 1811                     (*comp)->chunk_next < (*comp)->chunk_reserved)
 1812                         (*comp)->chunk_next = (*comp)->chunk_reserved;
 1813 
 1814                 (*comp)->flags |=
 1815                     VIRSTOR_PROVIDER_ALLOCATED | VIRSTOR_PROVIDER_CURRENT;
 1816                 dump_component(*comp);
 1817                 *comp_no_p = comp_no;
 1818                 *chunk = (*comp)->chunk_next++;
 1819         } else {
 1820                 *comp_no_p = comp_no;
 1821                 *chunk = (*comp)->chunk_next++;
 1822         }
 1823         return (0);
 1824 }
 1825 
 1826 /* Dump a component */
 1827 static void
 1828 dump_component(struct g_virstor_component *comp)
 1829 {
 1830 
 1831         if (g_virstor_debug < LVL_DEBUG2)
 1832                 return;
 1833         printf("Component %d: %s\n", comp->index, comp->gcons->provider->name);
 1834         printf("  chunk_count: %u\n", comp->chunk_count);
 1835         printf("   chunk_next: %u\n", comp->chunk_next);
 1836         printf("        flags: %u\n", comp->flags);
 1837 }
 1838 
 1839 #if 0
 1840 /* Dump a map entry */
 1841 static void
 1842 dump_me(struct virstor_map_entry *me, unsigned int nr)
 1843 {
 1844         if (g_virstor_debug < LVL_DEBUG)
 1845                 return;
 1846         printf("VIRT. CHUNK #%d: ", nr);
 1847         if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0)
 1848                 printf("(unallocated)\n");
 1849         else
 1850                 printf("allocated at provider %u, provider_chunk %u\n",
 1851                     me->provider_no, me->provider_chunk);
 1852 }
 1853 #endif
 1854 
 1855 /*
 1856  * Dismantle bio_queue and destroy its components
 1857  */
 1858 static void
 1859 bioq_dismantle(struct bio_queue_head *bq)
 1860 {
 1861         struct bio *b;
 1862 
 1863         for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) {
 1864                 bioq_remove(bq, b);
 1865                 g_destroy_bio(b);
 1866         }
 1867 }
 1868 
 1869 /*
 1870  * The function that shouldn't be called.
 1871  * When this is called, the stack is already garbled because of
 1872  * argument mismatch. There's nothing to do now but panic, which is
 1873  * accidentally the whole purpose of this function.
 1874  * Motivation: to guard from accidentally calling geom methods when
 1875  * they shouldn't be called. (see g_..._taste)
 1876  */
 1877 static void
 1878 invalid_call(void)
 1879 {
 1880         panic("invalid_call() has just been called. Something's fishy here.");
 1881 }
 1882 
 1883 DECLARE_GEOM_CLASS(g_virstor_class, g_virstor); /* Let there be light */

Cache object: c775c24306dc8086368149c96cf0f043


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