g_virstor.c revision 227309
1172302Spjd/*- 2172302Spjd * Copyright (c) 2006-2007 Ivan Voras <ivoras@freebsd.org> 3172302Spjd * All rights reserved. 4172302Spjd * 5172302Spjd * Redistribution and use in source and binary forms, with or without 6172302Spjd * modification, are permitted provided that the following conditions 7172302Spjd * are met: 8172302Spjd * 1. Redistributions of source code must retain the above copyright 9172302Spjd * notice, this list of conditions and the following disclaimer. 10172302Spjd * 2. Redistributions in binary form must reproduce the above copyright 11172302Spjd * notice, this list of conditions and the following disclaimer in the 12172302Spjd * documentation and/or other materials provided with the distribution. 13172302Spjd * 14172302Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15172302Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16172302Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17172302Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18172302Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19172302Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20172302Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21172302Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22172302Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23172302Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24172302Spjd * SUCH DAMAGE. 25172302Spjd */ 26172302Spjd 27172302Spjd/* Implementation notes: 28172302Spjd * - "Components" are wrappers around providers that make up the 29172302Spjd * virtual storage (i.e. a virstor has "physical" components) 30172302Spjd */ 31172302Spjd 32172302Spjd#include <sys/cdefs.h> 33172302Spjd__FBSDID("$FreeBSD: head/sys/geom/virstor/g_virstor.c 227309 2011-11-07 15:43:11Z ed $"); 34172302Spjd 35172302Spjd#include <sys/param.h> 36172302Spjd#include <sys/systm.h> 37172302Spjd#include <sys/kernel.h> 38172302Spjd#include <sys/module.h> 39172302Spjd#include <sys/lock.h> 40172302Spjd#include <sys/mutex.h> 41172302Spjd#include <sys/sx.h> 42172302Spjd#include <sys/bio.h> 43223921Sae#include <sys/sbuf.h> 44172302Spjd#include <sys/sysctl.h> 45172302Spjd#include <sys/malloc.h> 46172302Spjd#include <sys/time.h> 47172302Spjd#include <sys/proc.h> 48172302Spjd#include <sys/kthread.h> 49172302Spjd#include <sys/mutex.h> 50172302Spjd#include <vm/uma.h> 51172302Spjd#include <geom/geom.h> 52172302Spjd 53172302Spjd#include <geom/virstor/g_virstor.h> 54172302Spjd#include <geom/virstor/g_virstor_md.h> 55172302Spjd 56219029SnetchildFEATURE(g_virstor, "GEOM virtual storage support"); 57219029Snetchild 58172302Spjd/* Declare malloc(9) label */ 59172302Spjdstatic MALLOC_DEFINE(M_GVIRSTOR, "gvirstor", "GEOM_VIRSTOR Data"); 60172302Spjd 61172302Spjd/* GEOM class methods */ 62172302Spjdstatic g_init_t g_virstor_init; 63172302Spjdstatic g_fini_t g_virstor_fini; 64172302Spjdstatic g_taste_t g_virstor_taste; 65172302Spjdstatic g_ctl_req_t g_virstor_config; 66172302Spjdstatic g_ctl_destroy_geom_t g_virstor_destroy_geom; 67172302Spjd 68172302Spjd/* Declare & initialize class structure ("geom class") */ 69172302Spjdstruct g_class g_virstor_class = { 70172302Spjd .name = G_VIRSTOR_CLASS_NAME, 71172302Spjd .version = G_VERSION, 72172302Spjd .init = g_virstor_init, 73172302Spjd .fini = g_virstor_fini, 74172302Spjd .taste = g_virstor_taste, 75172302Spjd .ctlreq = g_virstor_config, 76172302Spjd .destroy_geom = g_virstor_destroy_geom 77172302Spjd /* The .dumpconf and the rest are only usable for a geom instance, so 78172302Spjd * they will be set when such instance is created. */ 79172302Spjd}; 80172302Spjd 81172302Spjd/* Declare sysctl's and loader tunables */ 82172302SpjdSYSCTL_DECL(_kern_geom); 83227309Sedstatic SYSCTL_NODE(_kern_geom, OID_AUTO, virstor, CTLFLAG_RW, 0, 84227309Sed "GEOM_GVIRSTOR information"); 85172302Spjd 86172302Spjdstatic u_int g_virstor_debug = 2; /* XXX: lower to 2 when released to public */ 87172302SpjdTUNABLE_INT("kern.geom.virstor.debug", &g_virstor_debug); 88172302SpjdSYSCTL_UINT(_kern_geom_virstor, OID_AUTO, debug, CTLFLAG_RW, &g_virstor_debug, 89172302Spjd 0, "Debug level (2=production, 5=normal, 15=excessive)"); 90172302Spjd 91172302Spjdstatic u_int g_virstor_chunk_watermark = 100; 92172302SpjdTUNABLE_INT("kern.geom.virstor.chunk_watermark", &g_virstor_chunk_watermark); 93172302SpjdSYSCTL_UINT(_kern_geom_virstor, OID_AUTO, chunk_watermark, CTLFLAG_RW, 94172302Spjd &g_virstor_chunk_watermark, 0, 95172302Spjd "Minimum number of free chunks before issuing administrative warning"); 96172302Spjd 97172302Spjdstatic u_int g_virstor_component_watermark = 1; 98172302SpjdTUNABLE_INT("kern.geom.virstor.component_watermark", 99172302Spjd &g_virstor_component_watermark); 100172302SpjdSYSCTL_UINT(_kern_geom_virstor, OID_AUTO, component_watermark, CTLFLAG_RW, 101172302Spjd &g_virstor_component_watermark, 0, 102172302Spjd "Minimum number of free components before issuing administrative warning"); 103172302Spjd 104172302Spjdstatic int read_metadata(struct g_consumer *, struct g_virstor_metadata *); 105202987Sivorasstatic void write_metadata(struct g_consumer *, struct g_virstor_metadata *); 106172302Spjdstatic int clear_metadata(struct g_virstor_component *); 107172302Spjdstatic int add_provider_to_geom(struct g_virstor_softc *, struct g_provider *, 108172302Spjd struct g_virstor_metadata *); 109172302Spjdstatic struct g_geom *create_virstor_geom(struct g_class *, 110172302Spjd struct g_virstor_metadata *); 111172302Spjdstatic void virstor_check_and_run(struct g_virstor_softc *); 112172302Spjdstatic u_int virstor_valid_components(struct g_virstor_softc *); 113172302Spjdstatic int virstor_geom_destroy(struct g_virstor_softc *, boolean_t, 114172302Spjd boolean_t); 115172302Spjdstatic void remove_component(struct g_virstor_softc *, 116172302Spjd struct g_virstor_component *, boolean_t); 117172302Spjdstatic void bioq_dismantle(struct bio_queue_head *); 118172302Spjdstatic int allocate_chunk(struct g_virstor_softc *, 119172302Spjd struct g_virstor_component **, u_int *, u_int *); 120172302Spjdstatic void delay_destroy_consumer(void *, int); 121172302Spjdstatic void dump_component(struct g_virstor_component *comp); 122172302Spjd#if 0 123172302Spjdstatic void dump_me(struct virstor_map_entry *me, unsigned int nr); 124172302Spjd#endif 125172302Spjd 126172302Spjdstatic void virstor_ctl_stop(struct gctl_req *, struct g_class *); 127172302Spjdstatic void virstor_ctl_add(struct gctl_req *, struct g_class *); 128172302Spjdstatic void virstor_ctl_remove(struct gctl_req *, struct g_class *); 129172302Spjdstatic struct g_virstor_softc * virstor_find_geom(const struct g_class *, 130172302Spjd const char *); 131172302Spjdstatic void update_metadata(struct g_virstor_softc *); 132172302Spjdstatic void fill_metadata(struct g_virstor_softc *, struct g_virstor_metadata *, 133172302Spjd u_int, u_int); 134172302Spjd 135172302Spjdstatic void g_virstor_orphan(struct g_consumer *); 136172302Spjdstatic int g_virstor_access(struct g_provider *, int, int, int); 137172302Spjdstatic void g_virstor_start(struct bio *); 138172302Spjdstatic void g_virstor_dumpconf(struct sbuf *, const char *, struct g_geom *, 139172302Spjd struct g_consumer *, struct g_provider *); 140172302Spjdstatic void g_virstor_done(struct bio *); 141172302Spjd 142172302Spjdstatic void invalid_call(void); 143172302Spjd/* 144172302Spjd * Initialise GEOM class (per-class callback) 145172302Spjd */ 146172302Spjdstatic void 147172302Spjdg_virstor_init(struct g_class *mp __unused) 148172302Spjd{ 149172302Spjd 150172302Spjd /* Catch map struct size mismatch at compile time; Map entries must 151172302Spjd * fit into MAXPHYS exactly, with no wasted space. */ 152172302Spjd CTASSERT(VIRSTOR_MAP_BLOCK_ENTRIES*VIRSTOR_MAP_ENTRY_SIZE == MAXPHYS); 153172302Spjd 154172302Spjd /* Init UMA zones, TAILQ's, other global vars */ 155172302Spjd} 156172302Spjd 157172302Spjd/* 158172302Spjd * Finalise GEOM class (per-class callback) 159172302Spjd */ 160172302Spjdstatic void 161172302Spjdg_virstor_fini(struct g_class *mp __unused) 162172302Spjd{ 163172302Spjd 164172302Spjd /* Deinit UMA zones & global vars */ 165172302Spjd} 166172302Spjd 167172302Spjd/* 168172302Spjd * Config (per-class callback) 169172302Spjd */ 170172302Spjdstatic void 171172302Spjdg_virstor_config(struct gctl_req *req, struct g_class *cp, char const *verb) 172172302Spjd{ 173172302Spjd uint32_t *version; 174172302Spjd 175172302Spjd g_topology_assert(); 176172302Spjd 177172302Spjd version = gctl_get_paraml(req, "version", sizeof(*version)); 178172302Spjd if (version == NULL) { 179172302Spjd gctl_error(req, "Failed to get 'version' argument"); 180172302Spjd return; 181172302Spjd } 182172302Spjd if (*version != G_VIRSTOR_VERSION) { 183172302Spjd gctl_error(req, "Userland and kernel versions out of sync"); 184172302Spjd return; 185172302Spjd } 186172302Spjd 187172302Spjd g_topology_unlock(); 188172302Spjd if (strcmp(verb, "add") == 0) 189172302Spjd virstor_ctl_add(req, cp); 190172302Spjd else if (strcmp(verb, "stop") == 0 || strcmp(verb, "destroy") == 0) 191172302Spjd virstor_ctl_stop(req, cp); 192172302Spjd else if (strcmp(verb, "remove") == 0) 193172302Spjd virstor_ctl_remove(req, cp); 194172302Spjd else 195172302Spjd gctl_error(req, "unknown verb: '%s'", verb); 196172302Spjd g_topology_lock(); 197172302Spjd} 198172302Spjd 199172302Spjd/* 200172302Spjd * "stop" verb from userland 201172302Spjd */ 202172302Spjdstatic void 203172302Spjdvirstor_ctl_stop(struct gctl_req *req, struct g_class *cp) 204172302Spjd{ 205172302Spjd int *force, *nargs; 206172302Spjd int i; 207172302Spjd 208172302Spjd nargs = gctl_get_paraml(req, "nargs", sizeof *nargs); 209172302Spjd if (nargs == NULL) { 210172302Spjd gctl_error(req, "Error fetching argument '%s'", "nargs"); 211172302Spjd return; 212172302Spjd } 213172302Spjd if (*nargs < 1) { 214172302Spjd gctl_error(req, "Invalid number of arguments"); 215172302Spjd return; 216172302Spjd } 217172302Spjd force = gctl_get_paraml(req, "force", sizeof *force); 218172302Spjd if (force == NULL) { 219172302Spjd gctl_error(req, "Error fetching argument '%s'", "force"); 220172302Spjd return; 221172302Spjd } 222172302Spjd 223172302Spjd g_topology_lock(); 224172302Spjd for (i = 0; i < *nargs; i++) { 225172302Spjd char param[8]; 226172302Spjd const char *name; 227172302Spjd struct g_virstor_softc *sc; 228172302Spjd int error; 229172302Spjd 230172302Spjd sprintf(param, "arg%d", i); 231172302Spjd name = gctl_get_asciiparam(req, param); 232180120Sdelphij if (name == NULL) { 233180120Sdelphij gctl_error(req, "No 'arg%d' argument", i); 234180120Sdelphij g_topology_unlock(); 235180120Sdelphij return; 236180120Sdelphij } 237172302Spjd sc = virstor_find_geom(cp, name); 238172302Spjd LOG_MSG(LVL_INFO, "Stopping %s by the userland command", 239172302Spjd sc->geom->name); 240172302Spjd update_metadata(sc); 241172302Spjd if ((error = virstor_geom_destroy(sc, TRUE, TRUE)) != 0) { 242172302Spjd LOG_MSG(LVL_ERROR, "Cannot destroy %s: %d", 243172302Spjd sc->geom->name, error); 244172302Spjd } 245172302Spjd } 246172302Spjd g_topology_unlock(); 247172302Spjd} 248172302Spjd 249172302Spjd/* 250172302Spjd * "add" verb from userland - add new component(s) to the structure. 251172302Spjd * This will be done all at once in here, without going through the 252172302Spjd * .taste function for new components. 253172302Spjd */ 254172302Spjdstatic void 255172302Spjdvirstor_ctl_add(struct gctl_req *req, struct g_class *cp) 256172302Spjd{ 257172302Spjd /* Note: while this is going on, I/O is being done on 258172302Spjd * the g_up and g_down threads. The idea is to make changes 259172302Spjd * to softc members in a way that can atomically activate 260172302Spjd * them all at once. */ 261172302Spjd struct g_virstor_softc *sc; 262172302Spjd int *hardcode, *nargs; 263172302Spjd const char *geom_name; /* geom to add a component to */ 264172302Spjd struct g_consumer *fcp; 265172302Spjd struct g_virstor_bio_q *bq; 266172302Spjd u_int added; 267172302Spjd int error; 268172302Spjd int i; 269172302Spjd 270172302Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 271172302Spjd if (nargs == NULL) { 272172302Spjd gctl_error(req, "Error fetching argument '%s'", "nargs"); 273172302Spjd return; 274172302Spjd } 275172302Spjd if (*nargs < 2) { 276172302Spjd gctl_error(req, "Invalid number of arguments"); 277172302Spjd return; 278172302Spjd } 279172302Spjd hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 280172302Spjd if (hardcode == NULL) { 281172302Spjd gctl_error(req, "Error fetching argument '%s'", "hardcode"); 282172302Spjd return; 283172302Spjd } 284172302Spjd 285172302Spjd /* Find "our" geom */ 286172302Spjd geom_name = gctl_get_asciiparam(req, "arg0"); 287172302Spjd if (geom_name == NULL) { 288172302Spjd gctl_error(req, "Error fetching argument '%s'", "geom_name (arg0)"); 289172302Spjd return; 290172302Spjd } 291172302Spjd sc = virstor_find_geom(cp, geom_name); 292172302Spjd if (sc == NULL) { 293172302Spjd gctl_error(req, "Don't know anything about '%s'", geom_name); 294172302Spjd return; 295172302Spjd } 296172302Spjd 297172302Spjd if (virstor_valid_components(sc) != sc->n_components) { 298172302Spjd LOG_MSG(LVL_ERROR, "Cannot add components to incomplete " 299172302Spjd "virstor %s", sc->geom->name); 300172302Spjd gctl_error(req, "Virstor %s is incomplete", sc->geom->name); 301172302Spjd return; 302172302Spjd } 303172302Spjd 304172302Spjd fcp = sc->components[0].gcons; 305172302Spjd added = 0; 306172302Spjd g_topology_lock(); 307172302Spjd for (i = 1; i < *nargs; i++) { 308172302Spjd struct g_virstor_metadata md; 309172302Spjd char aname[8]; 310172302Spjd const char *prov_name; 311172302Spjd struct g_provider *pp; 312172302Spjd struct g_consumer *cp; 313172302Spjd u_int nc; 314172302Spjd u_int j; 315172302Spjd 316172302Spjd snprintf(aname, sizeof aname, "arg%d", i); 317172302Spjd prov_name = gctl_get_asciiparam(req, aname); 318203408Sdelphij if (prov_name == NULL) { 319203408Sdelphij gctl_error(req, "Error fetching argument '%s'", aname); 320203408Sdelphij g_topology_unlock(); 321203408Sdelphij return; 322203408Sdelphij } 323213662Sae if (strncmp(prov_name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 324213662Sae prov_name += sizeof(_PATH_DEV) - 1; 325172302Spjd 326172302Spjd pp = g_provider_by_name(prov_name); 327172302Spjd if (pp == NULL) { 328172302Spjd /* This is the most common error so be verbose about it */ 329172302Spjd if (added != 0) { 330172302Spjd gctl_error(req, "Invalid provider: '%s' (added" 331172302Spjd " %u components)", prov_name, added); 332172302Spjd update_metadata(sc); 333172302Spjd } else { 334172302Spjd gctl_error(req, "Invalid provider: '%s'", 335172302Spjd prov_name); 336172302Spjd } 337172302Spjd g_topology_unlock(); 338172302Spjd return; 339172302Spjd } 340172302Spjd cp = g_new_consumer(sc->geom); 341172302Spjd if (cp == NULL) { 342172302Spjd gctl_error(req, "Cannot create consumer"); 343172302Spjd g_topology_unlock(); 344172302Spjd return; 345172302Spjd } 346172302Spjd error = g_attach(cp, pp); 347172302Spjd if (error != 0) { 348172302Spjd gctl_error(req, "Cannot attach a consumer to %s", 349172302Spjd pp->name); 350172302Spjd g_destroy_consumer(cp); 351172302Spjd g_topology_unlock(); 352172302Spjd return; 353172302Spjd } 354172302Spjd if (fcp->acr != 0 || fcp->acw != 0 || fcp->ace != 0) { 355172302Spjd error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 356172302Spjd if (error != 0) { 357172302Spjd gctl_error(req, "Access request failed for %s", 358172302Spjd pp->name); 359172302Spjd g_destroy_consumer(cp); 360172302Spjd g_topology_unlock(); 361172302Spjd return; 362172302Spjd } 363172302Spjd } 364172302Spjd if (fcp->provider->sectorsize != pp->sectorsize) { 365172302Spjd gctl_error(req, "Sector size doesn't fit for %s", 366172302Spjd pp->name); 367172302Spjd g_destroy_consumer(cp); 368172302Spjd g_topology_unlock(); 369172302Spjd return; 370172302Spjd } 371172302Spjd for (j = 0; j < sc->n_components; j++) { 372172302Spjd if (strcmp(sc->components[j].gcons->provider->name, 373172302Spjd pp->name) == 0) { 374172302Spjd gctl_error(req, "Component %s already in %s", 375172302Spjd pp->name, sc->geom->name); 376172302Spjd g_destroy_consumer(cp); 377172302Spjd g_topology_unlock(); 378172302Spjd return; 379172302Spjd } 380172302Spjd } 381172302Spjd sc->components = realloc(sc->components, 382172302Spjd sizeof(*sc->components) * (sc->n_components + 1), 383172302Spjd M_GVIRSTOR, M_WAITOK); 384172302Spjd 385172302Spjd nc = sc->n_components; 386172302Spjd sc->components[nc].gcons = cp; 387172302Spjd sc->components[nc].sc = sc; 388172302Spjd sc->components[nc].index = nc; 389172302Spjd sc->components[nc].chunk_count = cp->provider->mediasize / 390172302Spjd sc->chunk_size; 391172302Spjd sc->components[nc].chunk_next = 0; 392172302Spjd sc->components[nc].chunk_reserved = 0; 393172302Spjd 394172302Spjd if (sc->components[nc].chunk_count < 4) { 395172302Spjd gctl_error(req, "Provider too small: %s", 396172302Spjd cp->provider->name); 397172302Spjd g_destroy_consumer(cp); 398172302Spjd g_topology_unlock(); 399172302Spjd return; 400172302Spjd } 401172302Spjd fill_metadata(sc, &md, nc, *hardcode); 402172302Spjd write_metadata(cp, &md); 403172302Spjd /* The new component becomes visible when n_components is 404172302Spjd * incremented */ 405172302Spjd sc->n_components++; 406172302Spjd added++; 407172302Spjd 408172302Spjd } 409172302Spjd /* This call to update_metadata() is critical. In case there's a 410172302Spjd * power failure in the middle of it and some components are updated 411172302Spjd * while others are not, there will be trouble on next .taste() iff 412172302Spjd * a non-updated component is detected first */ 413172302Spjd update_metadata(sc); 414172302Spjd g_topology_unlock(); 415172302Spjd LOG_MSG(LVL_INFO, "Added %d component(s) to %s", added, 416172302Spjd sc->geom->name); 417172302Spjd /* Fire off BIOs previously queued because there wasn't any 418172302Spjd * physical space left. If the BIOs still can't be satisfied 419172302Spjd * they will again be added to the end of the queue (during 420172302Spjd * which the mutex will be recursed) */ 421172302Spjd bq = malloc(sizeof(*bq), M_GVIRSTOR, M_WAITOK); 422172302Spjd bq->bio = NULL; 423172302Spjd mtx_lock(&sc->delayed_bio_q_mtx); 424172302Spjd /* First, insert a sentinel to the queue end, so we don't 425172302Spjd * end up in an infinite loop if there's still no free 426172302Spjd * space available. */ 427172302Spjd STAILQ_INSERT_TAIL(&sc->delayed_bio_q, bq, linkage); 428172302Spjd while (!STAILQ_EMPTY(&sc->delayed_bio_q)) { 429172302Spjd bq = STAILQ_FIRST(&sc->delayed_bio_q); 430172302Spjd if (bq->bio != NULL) { 431172302Spjd g_virstor_start(bq->bio); 432172302Spjd STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage); 433172302Spjd free(bq, M_GVIRSTOR); 434172302Spjd } else { 435172302Spjd STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage); 436172302Spjd free(bq, M_GVIRSTOR); 437172302Spjd break; 438172302Spjd } 439172302Spjd } 440172302Spjd mtx_unlock(&sc->delayed_bio_q_mtx); 441172302Spjd 442172302Spjd} 443172302Spjd 444172302Spjd/* 445172302Spjd * Find a geom handled by the class 446172302Spjd */ 447172302Spjdstatic struct g_virstor_softc * 448172302Spjdvirstor_find_geom(const struct g_class *cp, const char *name) 449172302Spjd{ 450172302Spjd struct g_geom *gp; 451172302Spjd 452172302Spjd LIST_FOREACH(gp, &cp->geom, geom) { 453172302Spjd if (strcmp(name, gp->name) == 0) 454172302Spjd return (gp->softc); 455172302Spjd } 456172302Spjd return (NULL); 457172302Spjd} 458172302Spjd 459172302Spjd/* 460172302Spjd * Update metadata on all components to reflect the current state 461172302Spjd * of these fields: 462172302Spjd * - chunk_next 463172302Spjd * - flags 464172302Spjd * - md_count 465172302Spjd * Expects things to be set up so write_metadata() can work, i.e. 466172302Spjd * the topology lock must be held. 467172302Spjd */ 468172302Spjdstatic void 469172302Spjdupdate_metadata(struct g_virstor_softc *sc) 470172302Spjd{ 471172302Spjd struct g_virstor_metadata md; 472172302Spjd int n; 473172302Spjd 474172302Spjd if (virstor_valid_components(sc) != sc->n_components) 475172302Spjd return; /* Incomplete device */ 476172302Spjd LOG_MSG(LVL_DEBUG, "Updating metadata on components for %s", 477172302Spjd sc->geom->name); 478172302Spjd /* Update metadata on components */ 479172302Spjd g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, 480172302Spjd sc->geom->class->name, sc->geom->name); 481172302Spjd g_topology_assert(); 482172302Spjd for (n = 0; n < sc->n_components; n++) { 483172302Spjd read_metadata(sc->components[n].gcons, &md); 484172302Spjd md.chunk_next = sc->components[n].chunk_next; 485172302Spjd md.flags = sc->components[n].flags; 486172302Spjd md.md_count = sc->n_components; 487172302Spjd write_metadata(sc->components[n].gcons, &md); 488172302Spjd } 489172302Spjd} 490172302Spjd 491172302Spjd/* 492172302Spjd * Fills metadata (struct md) from information stored in softc and the nc'th 493172302Spjd * component of virstor 494172302Spjd */ 495172302Spjdstatic void 496172302Spjdfill_metadata(struct g_virstor_softc *sc, struct g_virstor_metadata *md, 497172302Spjd u_int nc, u_int hardcode) 498172302Spjd{ 499172302Spjd struct g_virstor_component *c; 500172302Spjd 501172302Spjd bzero(md, sizeof *md); 502172302Spjd c = &sc->components[nc]; 503172302Spjd 504172302Spjd strncpy(md->md_magic, G_VIRSTOR_MAGIC, sizeof md->md_magic); 505172302Spjd md->md_version = G_VIRSTOR_VERSION; 506172302Spjd strncpy(md->md_name, sc->geom->name, sizeof md->md_name); 507172302Spjd md->md_id = sc->id; 508172302Spjd md->md_virsize = sc->virsize; 509172302Spjd md->md_chunk_size = sc->chunk_size; 510172302Spjd md->md_count = sc->n_components; 511172302Spjd 512172302Spjd if (hardcode) { 513172302Spjd strncpy(md->provider, c->gcons->provider->name, 514172302Spjd sizeof md->provider); 515172302Spjd } 516172302Spjd md->no = nc; 517172302Spjd md->provsize = c->gcons->provider->mediasize; 518172302Spjd md->chunk_count = c->chunk_count; 519172302Spjd md->chunk_next = c->chunk_next; 520172302Spjd md->chunk_reserved = c->chunk_reserved; 521172302Spjd md->flags = c->flags; 522172302Spjd} 523172302Spjd 524172302Spjd/* 525172302Spjd * Remove a component from virstor device. 526172302Spjd * Can only be done if the component is unallocated. 527172302Spjd */ 528172302Spjdstatic void 529172302Spjdvirstor_ctl_remove(struct gctl_req *req, struct g_class *cp) 530172302Spjd{ 531172302Spjd /* As this is executed in parallel to I/O, operations on virstor 532172302Spjd * structures must be as atomic as possible. */ 533172302Spjd struct g_virstor_softc *sc; 534172302Spjd int *nargs; 535172302Spjd const char *geom_name; 536172302Spjd u_int removed; 537172302Spjd int i; 538172302Spjd 539172302Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 540172302Spjd if (nargs == NULL) { 541172302Spjd gctl_error(req, "Error fetching argument '%s'", "nargs"); 542172302Spjd return; 543172302Spjd } 544172302Spjd if (*nargs < 2) { 545172302Spjd gctl_error(req, "Invalid number of arguments"); 546172302Spjd return; 547172302Spjd } 548172302Spjd /* Find "our" geom */ 549172302Spjd geom_name = gctl_get_asciiparam(req, "arg0"); 550172302Spjd if (geom_name == NULL) { 551172302Spjd gctl_error(req, "Error fetching argument '%s'", 552172302Spjd "geom_name (arg0)"); 553172302Spjd return; 554172302Spjd } 555172302Spjd sc = virstor_find_geom(cp, geom_name); 556172302Spjd if (sc == NULL) { 557172302Spjd gctl_error(req, "Don't know anything about '%s'", geom_name); 558172302Spjd return; 559172302Spjd } 560172302Spjd 561172302Spjd if (virstor_valid_components(sc) != sc->n_components) { 562172302Spjd LOG_MSG(LVL_ERROR, "Cannot remove components from incomplete " 563172302Spjd "virstor %s", sc->geom->name); 564172302Spjd gctl_error(req, "Virstor %s is incomplete", sc->geom->name); 565172302Spjd return; 566172302Spjd } 567172302Spjd 568172302Spjd removed = 0; 569172302Spjd for (i = 1; i < *nargs; i++) { 570172302Spjd char param[8]; 571172302Spjd const char *prov_name; 572172302Spjd int j, found; 573172302Spjd struct g_virstor_component *newcomp, *compbak; 574172302Spjd 575172302Spjd sprintf(param, "arg%d", i); 576172302Spjd prov_name = gctl_get_asciiparam(req, param); 577203408Sdelphij if (prov_name == NULL) { 578203408Sdelphij gctl_error(req, "Error fetching argument '%s'", param); 579203408Sdelphij return; 580203408Sdelphij } 581213662Sae if (strncmp(prov_name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 582213662Sae prov_name += sizeof(_PATH_DEV) - 1; 583172302Spjd 584172302Spjd found = -1; 585172302Spjd for (j = 0; j < sc->n_components; j++) { 586172302Spjd if (strcmp(sc->components[j].gcons->provider->name, 587172302Spjd prov_name) == 0) { 588172302Spjd found = j; 589172302Spjd break; 590172302Spjd } 591172302Spjd } 592172302Spjd if (found == -1) { 593172302Spjd LOG_MSG(LVL_ERROR, "No %s component in %s", 594172302Spjd prov_name, sc->geom->name); 595172302Spjd continue; 596172302Spjd } 597172302Spjd 598172302Spjd compbak = sc->components; 599172302Spjd newcomp = malloc(sc->n_components * sizeof(*sc->components), 600172302Spjd M_GVIRSTOR, M_WAITOK | M_ZERO); 601172302Spjd bcopy(sc->components, newcomp, found * sizeof(*sc->components)); 602172302Spjd bcopy(&sc->components[found + 1], newcomp + found, 603172302Spjd found * sizeof(*sc->components)); 604172302Spjd if ((sc->components[j].flags & VIRSTOR_PROVIDER_ALLOCATED) != 0) { 605172302Spjd LOG_MSG(LVL_ERROR, "Allocated provider %s cannot be " 606172302Spjd "removed from %s", 607172302Spjd prov_name, sc->geom->name); 608172302Spjd free(newcomp, M_GVIRSTOR); 609172302Spjd /* We'll consider this non-fatal error */ 610172302Spjd continue; 611172302Spjd } 612172302Spjd /* Renumerate unallocated components */ 613172302Spjd for (j = 0; j < sc->n_components-1; j++) { 614172302Spjd if ((sc->components[j].flags & 615172302Spjd VIRSTOR_PROVIDER_ALLOCATED) == 0) { 616172302Spjd sc->components[j].index = j; 617172302Spjd } 618172302Spjd } 619172302Spjd /* This is the critical section. If a component allocation 620172302Spjd * event happens while both variables are not yet set, 621172302Spjd * there will be trouble. Something will panic on encountering 622172302Spjd * NULL sc->components[x].gcomp member. 623172302Spjd * Luckily, component allocation happens very rarely and 624172302Spjd * removing components is an abnormal action in any case. */ 625172302Spjd sc->components = newcomp; 626172302Spjd sc->n_components--; 627172302Spjd /* End critical section */ 628172302Spjd 629172302Spjd g_topology_lock(); 630172302Spjd if (clear_metadata(&compbak[found]) != 0) { 631172302Spjd LOG_MSG(LVL_WARNING, "Trouble ahead: cannot clear " 632172302Spjd "metadata on %s", prov_name); 633172302Spjd } 634172302Spjd g_detach(compbak[found].gcons); 635172302Spjd g_destroy_consumer(compbak[found].gcons); 636172302Spjd g_topology_unlock(); 637172302Spjd 638172302Spjd free(compbak, M_GVIRSTOR); 639172302Spjd 640172302Spjd removed++; 641172302Spjd } 642172302Spjd 643172302Spjd /* This call to update_metadata() is critical. In case there's a 644172302Spjd * power failure in the middle of it and some components are updated 645172302Spjd * while others are not, there will be trouble on next .taste() iff 646172302Spjd * a non-updated component is detected first */ 647172302Spjd g_topology_lock(); 648172302Spjd update_metadata(sc); 649172302Spjd g_topology_unlock(); 650172302Spjd LOG_MSG(LVL_INFO, "Removed %d component(s) from %s", removed, 651172302Spjd sc->geom->name); 652172302Spjd} 653172302Spjd 654172302Spjd/* 655172302Spjd * Clear metadata sector on component 656172302Spjd */ 657172302Spjdstatic int 658172302Spjdclear_metadata(struct g_virstor_component *comp) 659172302Spjd{ 660172302Spjd char *buf; 661172302Spjd int error; 662172302Spjd 663172302Spjd LOG_MSG(LVL_INFO, "Clearing metadata on %s", 664172302Spjd comp->gcons->provider->name); 665172302Spjd g_topology_assert(); 666172302Spjd error = g_access(comp->gcons, 0, 1, 0); 667172302Spjd if (error != 0) 668172302Spjd return (error); 669172302Spjd buf = malloc(comp->gcons->provider->sectorsize, M_GVIRSTOR, 670172302Spjd M_WAITOK | M_ZERO); 671172302Spjd error = g_write_data(comp->gcons, 672172302Spjd comp->gcons->provider->mediasize - 673172302Spjd comp->gcons->provider->sectorsize, 674172302Spjd buf, 675172302Spjd comp->gcons->provider->sectorsize); 676172302Spjd free(buf, M_GVIRSTOR); 677172302Spjd g_access(comp->gcons, 0, -1, 0); 678172302Spjd return (error); 679172302Spjd} 680172302Spjd 681172302Spjd/* 682172302Spjd * Destroy geom forcibly. 683172302Spjd */ 684172302Spjdstatic int 685172302Spjdg_virstor_destroy_geom(struct gctl_req *req __unused, struct g_class *mp, 686172302Spjd struct g_geom *gp) 687172302Spjd{ 688172302Spjd struct g_virstor_softc *sc; 689172302Spjd int exitval; 690172302Spjd 691172302Spjd sc = gp->softc; 692172302Spjd KASSERT(sc != NULL, ("%s: NULL sc", __func__)); 693172302Spjd 694172302Spjd exitval = 0; 695172302Spjd LOG_MSG(LVL_DEBUG, "%s called for %s, sc=%p", __func__, gp->name, 696172302Spjd gp->softc); 697172302Spjd 698172302Spjd if (sc != NULL) { 699172302Spjd#ifdef INVARIANTS 700172302Spjd char *buf; 701172302Spjd int error; 702172302Spjd off_t off; 703172302Spjd int isclean, count; 704172302Spjd int n; 705172302Spjd 706172302Spjd LOG_MSG(LVL_INFO, "INVARIANTS detected"); 707172302Spjd LOG_MSG(LVL_INFO, "Verifying allocation " 708172302Spjd "table for %s", sc->geom->name); 709172302Spjd count = 0; 710172302Spjd for (n = 0; n < sc->chunk_count; n++) { 711172302Spjd if (sc->map[n].flags || VIRSTOR_MAP_ALLOCATED != 0) 712172302Spjd count++; 713172302Spjd } 714172302Spjd LOG_MSG(LVL_INFO, "Device %s has %d allocated chunks", 715172302Spjd sc->geom->name, count); 716172302Spjd n = off = count = 0; 717172302Spjd isclean = 1; 718172302Spjd if (virstor_valid_components(sc) != sc->n_components) { 719172302Spjd /* This is a incomplete virstor device (not all 720172302Spjd * components have been found) */ 721172302Spjd LOG_MSG(LVL_ERROR, "Device %s is incomplete", 722172302Spjd sc->geom->name); 723172302Spjd goto bailout; 724172302Spjd } 725172302Spjd error = g_access(sc->components[0].gcons, 1, 0, 0); 726172302Spjd KASSERT(error == 0, ("%s: g_access failed (%d)", __func__, 727172302Spjd error)); 728172302Spjd /* Compare the whole on-disk allocation table with what's 729172302Spjd * currently in memory */ 730172302Spjd while (n < sc->chunk_count) { 731172302Spjd buf = g_read_data(sc->components[0].gcons, off, 732172302Spjd sc->sectorsize, &error); 733172302Spjd KASSERT(buf != NULL, ("g_read_data returned NULL (%d) " 734172302Spjd "for read at %jd", error, off)); 735172302Spjd if (bcmp(buf, &sc->map[n], sc->sectorsize) != 0) { 736172302Spjd LOG_MSG(LVL_ERROR, "ERROR in allocation table, " 737172302Spjd "entry %d, offset %jd", n, off); 738172302Spjd isclean = 0; 739172302Spjd count++; 740172302Spjd } 741172302Spjd n += sc->me_per_sector; 742172302Spjd off += sc->sectorsize; 743172302Spjd g_free(buf); 744172302Spjd } 745172302Spjd error = g_access(sc->components[0].gcons, -1, 0, 0); 746172302Spjd KASSERT(error == 0, ("%s: g_access failed (%d) on exit", 747172302Spjd __func__, error)); 748172302Spjd if (isclean != 1) { 749172302Spjd LOG_MSG(LVL_ERROR, "ALLOCATION TABLE CORRUPTED FOR %s " 750172304Spjd "(%d sectors don't match, max %zu allocations)", 751172302Spjd sc->geom->name, count, 752172302Spjd count * sc->me_per_sector); 753172302Spjd } else { 754172302Spjd LOG_MSG(LVL_INFO, "Allocation table ok for %s", 755172302Spjd sc->geom->name); 756172302Spjd } 757172302Spjdbailout: 758172302Spjd#endif 759172302Spjd update_metadata(sc); 760172302Spjd virstor_geom_destroy(sc, FALSE, FALSE); 761172302Spjd exitval = EAGAIN; 762172302Spjd } else 763172302Spjd exitval = 0; 764172302Spjd return (exitval); 765172302Spjd} 766172302Spjd 767172302Spjd/* 768172302Spjd * Taste event (per-class callback) 769172302Spjd * Examines a provider and creates geom instances if needed 770172302Spjd */ 771172302Spjdstatic struct g_geom * 772172302Spjdg_virstor_taste(struct g_class *mp, struct g_provider *pp, int flags) 773172302Spjd{ 774172302Spjd struct g_virstor_metadata md; 775172302Spjd struct g_geom *gp; 776172302Spjd struct g_consumer *cp; 777172302Spjd struct g_virstor_softc *sc; 778172302Spjd int error; 779172302Spjd 780172302Spjd g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 781172302Spjd g_topology_assert(); 782172302Spjd LOG_MSG(LVL_DEBUG, "Tasting %s", pp->name); 783172302Spjd 784172302Spjd /* We need a dummy geom to attach a consumer to the given provider */ 785172302Spjd gp = g_new_geomf(mp, "virstor:taste.helper"); 786172302Spjd gp->start = (void *)invalid_call; /* XXX: hacked up so the */ 787172302Spjd gp->access = (void *)invalid_call; /* compiler doesn't complain. */ 788172302Spjd gp->orphan = (void *)invalid_call; /* I really want these to fail. */ 789172302Spjd 790172302Spjd cp = g_new_consumer(gp); 791172302Spjd g_attach(cp, pp); 792172302Spjd error = read_metadata(cp, &md); 793172302Spjd g_detach(cp); 794172302Spjd g_destroy_consumer(cp); 795172302Spjd g_destroy_geom(gp); 796172302Spjd 797172302Spjd if (error != 0) 798172302Spjd return (NULL); 799172302Spjd 800172302Spjd if (strcmp(md.md_magic, G_VIRSTOR_MAGIC) != 0) 801172302Spjd return (NULL); 802172302Spjd if (md.md_version != G_VIRSTOR_VERSION) { 803172302Spjd LOG_MSG(LVL_ERROR, "Kernel module version invalid " 804172302Spjd "to handle %s (%s) : %d should be %d", 805172302Spjd md.md_name, pp->name, md.md_version, G_VIRSTOR_VERSION); 806172302Spjd return (NULL); 807172302Spjd } 808172302Spjd if (md.provsize != pp->mediasize) 809172302Spjd return (NULL); 810172302Spjd 811172302Spjd /* If the provider name is hardcoded, use the offered provider only 812172302Spjd * if it's been offered with its proper name (the one used in 813172302Spjd * the label command). */ 814221101Smav if (md.provider[0] != '\0' && 815221101Smav !g_compare_names(md.provider, pp->name)) 816221101Smav return (NULL); 817172302Spjd 818172302Spjd /* Iterate all geoms this class already knows about to see if a new 819172302Spjd * geom instance of this class needs to be created (in case the provider 820172302Spjd * is first from a (possibly) multi-consumer geom) or it just needs 821172302Spjd * to be added to an existing instance. */ 822172302Spjd sc = NULL; 823172302Spjd gp = NULL; 824172302Spjd LIST_FOREACH(gp, &mp->geom, geom) { 825172302Spjd sc = gp->softc; 826172302Spjd if (sc == NULL) 827172302Spjd continue; 828172302Spjd if (strcmp(md.md_name, sc->geom->name) != 0) 829172302Spjd continue; 830172302Spjd if (md.md_id != sc->id) 831172302Spjd continue; 832172302Spjd break; 833172302Spjd } 834172302Spjd if (gp != NULL) { /* We found an existing geom instance; add to it */ 835172302Spjd LOG_MSG(LVL_INFO, "Adding %s to %s", pp->name, md.md_name); 836172302Spjd error = add_provider_to_geom(sc, pp, &md); 837172302Spjd if (error != 0) { 838172302Spjd LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)", 839172302Spjd pp->name, md.md_name, error); 840172302Spjd return (NULL); 841172302Spjd } 842172302Spjd } else { /* New geom instance needs to be created */ 843172302Spjd gp = create_virstor_geom(mp, &md); 844172302Spjd if (gp == NULL) { 845172302Spjd LOG_MSG(LVL_ERROR, "Error creating new instance of " 846172302Spjd "class %s: %s", mp->name, md.md_name); 847172302Spjd LOG_MSG(LVL_DEBUG, "Error creating %s at %s", 848172302Spjd md.md_name, pp->name); 849172302Spjd return (NULL); 850172302Spjd } 851172302Spjd sc = gp->softc; 852172302Spjd LOG_MSG(LVL_INFO, "Adding %s to %s (first found)", pp->name, 853172302Spjd md.md_name); 854172302Spjd error = add_provider_to_geom(sc, pp, &md); 855172302Spjd if (error != 0) { 856172302Spjd LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)", 857172302Spjd pp->name, md.md_name, error); 858172302Spjd virstor_geom_destroy(sc, TRUE, FALSE); 859172302Spjd return (NULL); 860172302Spjd } 861172302Spjd } 862172302Spjd 863172302Spjd return (gp); 864172302Spjd} 865172302Spjd 866172302Spjd/* 867172302Spjd * Destroyes consumer passed to it in arguments. Used as a callback 868172302Spjd * on g_event queue. 869172302Spjd */ 870172302Spjdstatic void 871172302Spjddelay_destroy_consumer(void *arg, int flags __unused) 872172302Spjd{ 873172302Spjd struct g_consumer *c = arg; 874172302Spjd KASSERT(c != NULL, ("%s: invalid consumer", __func__)); 875172302Spjd LOG_MSG(LVL_DEBUG, "Consumer %s destroyed with delay", 876172302Spjd c->provider->name); 877172302Spjd g_detach(c); 878172302Spjd g_destroy_consumer(c); 879172302Spjd} 880172302Spjd 881172302Spjd/* 882172302Spjd * Remove a component (consumer) from geom instance; If it's the first 883172302Spjd * component being removed, orphan the provider to announce geom's being 884172302Spjd * dismantled 885172302Spjd */ 886172302Spjdstatic void 887172302Spjdremove_component(struct g_virstor_softc *sc, struct g_virstor_component *comp, 888172302Spjd boolean_t delay) 889172302Spjd{ 890172302Spjd struct g_consumer *c; 891172302Spjd 892172302Spjd KASSERT(comp->gcons != NULL, ("Component with no consumer in %s", 893172302Spjd sc->geom->name)); 894172302Spjd c = comp->gcons; 895172302Spjd 896172302Spjd comp->gcons = NULL; 897172302Spjd KASSERT(c->provider != NULL, ("%s: no provider", __func__)); 898172302Spjd LOG_MSG(LVL_DEBUG, "Component %s removed from %s", c->provider->name, 899172302Spjd sc->geom->name); 900172302Spjd if (sc->provider != NULL) { 901172302Spjd /* Whither, GEOM? */ 902172302Spjd sc->provider->flags |= G_PF_WITHER; 903172302Spjd g_orphan_provider(sc->provider, ENXIO); 904172302Spjd sc->provider = NULL; 905172302Spjd LOG_MSG(LVL_INFO, "Removing provider %s", sc->geom->name); 906172302Spjd } 907172302Spjd 908172302Spjd if (c->acr > 0 || c->acw > 0 || c->ace > 0) 909172302Spjd g_access(c, -c->acr, -c->acw, -c->ace); 910172302Spjd if (delay) { 911172302Spjd /* Destroy consumer after it's tasted */ 912172302Spjd g_post_event(delay_destroy_consumer, c, M_WAITOK, NULL); 913172302Spjd } else { 914172302Spjd g_detach(c); 915172302Spjd g_destroy_consumer(c); 916172302Spjd } 917172302Spjd} 918172302Spjd 919172302Spjd/* 920172302Spjd * Destroy geom - called internally 921172302Spjd * See g_virstor_destroy_geom for the other one 922172302Spjd */ 923172302Spjdstatic int 924172302Spjdvirstor_geom_destroy(struct g_virstor_softc *sc, boolean_t force, 925172302Spjd boolean_t delay) 926172302Spjd{ 927172302Spjd struct g_provider *pp; 928172302Spjd struct g_geom *gp; 929172302Spjd int n; 930172302Spjd 931172302Spjd g_topology_assert(); 932172302Spjd 933172302Spjd if (sc == NULL) 934172302Spjd return (ENXIO); 935172302Spjd 936172302Spjd pp = sc->provider; 937172302Spjd if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 938172302Spjd LOG_MSG(force ? LVL_WARNING : LVL_ERROR, 939172302Spjd "Device %s is still open.", pp->name); 940172302Spjd if (!force) 941172302Spjd return (EBUSY); 942172302Spjd } 943172302Spjd 944172302Spjd for (n = 0; n < sc->n_components; n++) { 945172302Spjd if (sc->components[n].gcons != NULL) 946172302Spjd remove_component(sc, &sc->components[n], delay); 947172302Spjd } 948172302Spjd 949172302Spjd gp = sc->geom; 950172302Spjd gp->softc = NULL; 951172302Spjd 952172302Spjd KASSERT(sc->provider == NULL, ("Provider still exists for %s", 953172302Spjd gp->name)); 954172302Spjd 955172302Spjd /* XXX: This might or might not work, since we're called with 956172302Spjd * the topology lock held. Also, it might panic the kernel if 957172302Spjd * the error'd BIO is in softupdates code. */ 958172302Spjd mtx_lock(&sc->delayed_bio_q_mtx); 959172302Spjd while (!STAILQ_EMPTY(&sc->delayed_bio_q)) { 960172302Spjd struct g_virstor_bio_q *bq; 961172302Spjd bq = STAILQ_FIRST(&sc->delayed_bio_q); 962172302Spjd bq->bio->bio_error = ENOSPC; 963172302Spjd g_io_deliver(bq->bio, EIO); 964172302Spjd STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage); 965172302Spjd free(bq, M_GVIRSTOR); 966172302Spjd } 967172302Spjd mtx_unlock(&sc->delayed_bio_q_mtx); 968172302Spjd mtx_destroy(&sc->delayed_bio_q_mtx); 969172302Spjd 970172302Spjd free(sc->map, M_GVIRSTOR); 971172302Spjd free(sc->components, M_GVIRSTOR); 972172302Spjd bzero(sc, sizeof *sc); 973172302Spjd free(sc, M_GVIRSTOR); 974172302Spjd 975172302Spjd pp = LIST_FIRST(&gp->provider); /* We only offer one provider */ 976172302Spjd if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)) 977172302Spjd LOG_MSG(LVL_DEBUG, "Device %s destroyed", gp->name); 978172302Spjd 979172302Spjd g_wither_geom(gp, ENXIO); 980172302Spjd 981172302Spjd return (0); 982172302Spjd} 983172302Spjd 984172302Spjd/* 985172302Spjd * Utility function: read metadata & decode. Wants topology lock to be 986172302Spjd * held. 987172302Spjd */ 988172302Spjdstatic int 989172302Spjdread_metadata(struct g_consumer *cp, struct g_virstor_metadata *md) 990172302Spjd{ 991172302Spjd struct g_provider *pp; 992172302Spjd char *buf; 993172302Spjd int error; 994172302Spjd 995172302Spjd g_topology_assert(); 996172302Spjd error = g_access(cp, 1, 0, 0); 997172302Spjd if (error != 0) 998172302Spjd return (error); 999172302Spjd pp = cp->provider; 1000172302Spjd g_topology_unlock(); 1001172302Spjd buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 1002172302Spjd &error); 1003172302Spjd g_topology_lock(); 1004172302Spjd g_access(cp, -1, 0, 0); 1005172302Spjd if (buf == NULL) 1006172302Spjd return (error); 1007172302Spjd 1008172302Spjd virstor_metadata_decode(buf, md); 1009172302Spjd g_free(buf); 1010172302Spjd 1011172302Spjd return (0); 1012172302Spjd} 1013172302Spjd 1014172302Spjd/** 1015172302Spjd * Utility function: encode & write metadata. Assumes topology lock is 1016172302Spjd * held. 1017202987Sivoras * 1018202987Sivoras * There is no useful way of recovering from errors in this function, 1019202987Sivoras * not involving panicking the kernel. If the metadata cannot be written 1020202987Sivoras * the most we can do is notify the operator and hope he spots it and 1021202987Sivoras * replaces the broken drive. 1022172302Spjd */ 1023202987Sivorasstatic void 1024172302Spjdwrite_metadata(struct g_consumer *cp, struct g_virstor_metadata *md) 1025172302Spjd{ 1026172302Spjd struct g_provider *pp; 1027172302Spjd char *buf; 1028172302Spjd int error; 1029172302Spjd 1030172302Spjd KASSERT(cp != NULL && md != NULL && cp->provider != NULL, 1031172302Spjd ("Something's fishy in %s", __func__)); 1032172302Spjd LOG_MSG(LVL_DEBUG, "Writing metadata on %s", cp->provider->name); 1033172302Spjd g_topology_assert(); 1034172302Spjd error = g_access(cp, 0, 1, 0); 1035202987Sivoras if (error != 0) { 1036202987Sivoras LOG_MSG(LVL_ERROR, "g_access(0,1,0) failed for %s: %d", 1037202987Sivoras cp->provider->name, error); 1038202987Sivoras return; 1039202987Sivoras } 1040172302Spjd pp = cp->provider; 1041172302Spjd 1042172302Spjd buf = malloc(pp->sectorsize, M_GVIRSTOR, M_WAITOK); 1043172302Spjd virstor_metadata_encode(md, buf); 1044172302Spjd g_topology_unlock(); 1045172302Spjd error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf, 1046172302Spjd pp->sectorsize); 1047172302Spjd g_topology_lock(); 1048172302Spjd g_access(cp, 0, -1, 0); 1049202987Sivoras free(buf, M_GVIRSTOR); 1050172302Spjd 1051202987Sivoras if (error != 0) 1052202987Sivoras LOG_MSG(LVL_ERROR, "Error %d writing metadata to %s", 1053202987Sivoras error, cp->provider->name); 1054172302Spjd} 1055172302Spjd 1056172302Spjd/* 1057172302Spjd * Creates a new instance of this GEOM class, initialise softc 1058172302Spjd */ 1059172302Spjdstatic struct g_geom * 1060172302Spjdcreate_virstor_geom(struct g_class *mp, struct g_virstor_metadata *md) 1061172302Spjd{ 1062172302Spjd struct g_geom *gp; 1063172302Spjd struct g_virstor_softc *sc; 1064172302Spjd 1065172302Spjd LOG_MSG(LVL_DEBUG, "Creating geom instance for %s (id=%u)", 1066172302Spjd md->md_name, md->md_id); 1067172302Spjd 1068172302Spjd if (md->md_count < 1 || md->md_chunk_size < 1 || 1069172302Spjd md->md_virsize < md->md_chunk_size) { 1070172302Spjd /* This is bogus configuration, and probably means data is 1071172302Spjd * somehow corrupted. Panic, maybe? */ 1072172302Spjd LOG_MSG(LVL_ERROR, "Nonsensical metadata information for %s", 1073172302Spjd md->md_name); 1074172302Spjd return (NULL); 1075172302Spjd } 1076172302Spjd 1077172302Spjd /* Check if it's already created */ 1078172302Spjd LIST_FOREACH(gp, &mp->geom, geom) { 1079172302Spjd sc = gp->softc; 1080172302Spjd if (sc != NULL && strcmp(sc->geom->name, md->md_name) == 0) { 1081172302Spjd LOG_MSG(LVL_WARNING, "Geom %s already exists", 1082172302Spjd md->md_name); 1083172302Spjd if (sc->id != md->md_id) { 1084172302Spjd LOG_MSG(LVL_ERROR, 1085172302Spjd "Some stale or invalid components " 1086172302Spjd "exist for virstor device named %s. " 1087172302Spjd "You will need to <CLEAR> all stale " 1088172302Spjd "components and maybe reconfigure " 1089172302Spjd "the virstor device. Tune " 1090172302Spjd "kern.geom.virstor.debug sysctl up " 1091172302Spjd "for more information.", 1092172302Spjd sc->geom->name); 1093172302Spjd } 1094172302Spjd return (NULL); 1095172302Spjd } 1096172302Spjd } 1097172302Spjd gp = g_new_geomf(mp, "%s", md->md_name); 1098172302Spjd gp->softc = NULL; /* to circumevent races that test softc */ 1099172302Spjd 1100172302Spjd gp->start = g_virstor_start; 1101172302Spjd gp->spoiled = g_virstor_orphan; 1102172302Spjd gp->orphan = g_virstor_orphan; 1103172302Spjd gp->access = g_virstor_access; 1104172302Spjd gp->dumpconf = g_virstor_dumpconf; 1105172302Spjd 1106172302Spjd sc = malloc(sizeof(*sc), M_GVIRSTOR, M_WAITOK | M_ZERO); 1107172302Spjd sc->id = md->md_id; 1108172302Spjd sc->n_components = md->md_count; 1109172302Spjd sc->components = malloc(sizeof(struct g_virstor_component) * md->md_count, 1110172302Spjd M_GVIRSTOR, M_WAITOK | M_ZERO); 1111172302Spjd sc->chunk_size = md->md_chunk_size; 1112172302Spjd sc->virsize = md->md_virsize; 1113172302Spjd STAILQ_INIT(&sc->delayed_bio_q); 1114172302Spjd mtx_init(&sc->delayed_bio_q_mtx, "gvirstor_delayed_bio_q_mtx", 1115172302Spjd "gvirstor", MTX_DEF | MTX_RECURSE); 1116172302Spjd 1117172302Spjd sc->geom = gp; 1118172302Spjd sc->provider = NULL; /* virstor_check_and_run will create it */ 1119172302Spjd gp->softc = sc; 1120172302Spjd 1121172302Spjd LOG_MSG(LVL_ANNOUNCE, "Device %s created", sc->geom->name); 1122172302Spjd 1123172302Spjd return (gp); 1124172302Spjd} 1125172302Spjd 1126172302Spjd/* 1127172302Spjd * Add provider to a GEOM class instance 1128172302Spjd */ 1129172302Spjdstatic int 1130172302Spjdadd_provider_to_geom(struct g_virstor_softc *sc, struct g_provider *pp, 1131172302Spjd struct g_virstor_metadata *md) 1132172302Spjd{ 1133172302Spjd struct g_virstor_component *component; 1134172302Spjd struct g_consumer *cp, *fcp; 1135172302Spjd struct g_geom *gp; 1136172302Spjd int error; 1137172302Spjd 1138172302Spjd if (md->no >= sc->n_components) 1139172302Spjd return (EINVAL); 1140172302Spjd 1141172302Spjd /* "Current" compontent */ 1142172302Spjd component = &(sc->components[md->no]); 1143172302Spjd if (component->gcons != NULL) 1144172302Spjd return (EEXIST); 1145172302Spjd 1146172302Spjd gp = sc->geom; 1147172302Spjd fcp = LIST_FIRST(&gp->consumer); 1148172302Spjd 1149172302Spjd cp = g_new_consumer(gp); 1150172302Spjd error = g_attach(cp, pp); 1151172302Spjd 1152172302Spjd if (error != 0) { 1153172302Spjd g_destroy_consumer(cp); 1154172302Spjd return (error); 1155172302Spjd } 1156172302Spjd 1157172302Spjd if (fcp != NULL) { 1158172302Spjd if (fcp->provider->sectorsize != pp->sectorsize) { 1159172302Spjd /* TODO: this can be made to work */ 1160172302Spjd LOG_MSG(LVL_ERROR, "Provider %s of %s has invalid " 1161172302Spjd "sector size (%d)", pp->name, sc->geom->name, 1162172302Spjd pp->sectorsize); 1163172302Spjd return (EINVAL); 1164172302Spjd } 1165172302Spjd if (fcp->acr > 0 || fcp->acw || fcp->ace > 0) { 1166172302Spjd /* Replicate access permissions from first "live" consumer 1167172302Spjd * to the new one */ 1168172302Spjd error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 1169172302Spjd if (error != 0) { 1170172302Spjd g_detach(cp); 1171172302Spjd g_destroy_consumer(cp); 1172172302Spjd return (error); 1173172302Spjd } 1174172302Spjd } 1175172302Spjd } 1176172302Spjd 1177172302Spjd /* Bring up a new component */ 1178172302Spjd cp->private = component; 1179172302Spjd component->gcons = cp; 1180172302Spjd component->sc = sc; 1181172302Spjd component->index = md->no; 1182172302Spjd component->chunk_count = md->chunk_count; 1183172302Spjd component->chunk_next = md->chunk_next; 1184172302Spjd component->chunk_reserved = md->chunk_reserved; 1185172302Spjd component->flags = md->flags; 1186172302Spjd 1187172302Spjd LOG_MSG(LVL_DEBUG, "%s attached to %s", pp->name, sc->geom->name); 1188172302Spjd 1189172302Spjd virstor_check_and_run(sc); 1190172302Spjd return (0); 1191172302Spjd} 1192172302Spjd 1193172302Spjd/* 1194172302Spjd * Check if everything's ready to create the geom provider & device entry, 1195172302Spjd * create and start provider. 1196172302Spjd * Called ultimately by .taste, from g_event thread 1197172302Spjd */ 1198172302Spjdstatic void 1199172302Spjdvirstor_check_and_run(struct g_virstor_softc *sc) 1200172302Spjd{ 1201172302Spjd off_t off; 1202172302Spjd size_t n, count; 1203172302Spjd int index; 1204172302Spjd int error; 1205172302Spjd 1206172302Spjd if (virstor_valid_components(sc) != sc->n_components) 1207172302Spjd return; 1208172302Spjd 1209172302Spjd if (virstor_valid_components(sc) == 0) { 1210172302Spjd /* This is actually a candidate for panic() */ 1211172302Spjd LOG_MSG(LVL_ERROR, "No valid components for %s?", 1212172302Spjd sc->provider->name); 1213172302Spjd return; 1214172302Spjd } 1215172302Spjd 1216172302Spjd sc->sectorsize = sc->components[0].gcons->provider->sectorsize; 1217172302Spjd 1218172302Spjd /* Initialise allocation map from the first consumer */ 1219172302Spjd sc->chunk_count = sc->virsize / sc->chunk_size; 1220172302Spjd if (sc->chunk_count * (off_t)sc->chunk_size != sc->virsize) { 1221172302Spjd LOG_MSG(LVL_WARNING, "Device %s truncated to %ju bytes", 1222172302Spjd sc->provider->name, 1223172302Spjd sc->chunk_count * (off_t)sc->chunk_size); 1224172302Spjd } 1225172302Spjd sc->map_size = sc->chunk_count * sizeof *(sc->map); 1226172302Spjd /* The following allocation is in order of 4MB - 8MB */ 1227172302Spjd sc->map = malloc(sc->map_size, M_GVIRSTOR, M_WAITOK); 1228172302Spjd KASSERT(sc->map != NULL, ("%s: Memory allocation error (%zu bytes) for %s", 1229172302Spjd __func__, sc->map_size, sc->provider->name)); 1230172302Spjd sc->map_sectors = sc->map_size / sc->sectorsize; 1231172302Spjd 1232172302Spjd count = 0; 1233172302Spjd for (n = 0; n < sc->n_components; n++) 1234172302Spjd count += sc->components[n].chunk_count; 1235172302Spjd LOG_MSG(LVL_INFO, "Device %s has %zu physical chunks and %zu virtual " 1236172302Spjd "(%zu KB chunks)", 1237172302Spjd sc->geom->name, count, sc->chunk_count, sc->chunk_size / 1024); 1238172302Spjd 1239172302Spjd error = g_access(sc->components[0].gcons, 1, 0, 0); 1240172302Spjd if (error != 0) { 1241172302Spjd LOG_MSG(LVL_ERROR, "Cannot acquire read access for %s to " 1242172302Spjd "read allocation map for %s", 1243172302Spjd sc->components[0].gcons->provider->name, 1244172302Spjd sc->geom->name); 1245172302Spjd return; 1246172302Spjd } 1247172302Spjd /* Read in the allocation map */ 1248172302Spjd LOG_MSG(LVL_DEBUG, "Reading map for %s from %s", sc->geom->name, 1249172302Spjd sc->components[0].gcons->provider->name); 1250172302Spjd off = count = n = 0; 1251172302Spjd while (count < sc->map_size) { 1252172302Spjd struct g_virstor_map_entry *mapbuf; 1253172302Spjd size_t bs; 1254172302Spjd 1255172302Spjd bs = MIN(MAXPHYS, sc->map_size - count); 1256172302Spjd if (bs % sc->sectorsize != 0) { 1257172302Spjd /* Check for alignment errors */ 1258172302Spjd bs = (bs / sc->sectorsize) * sc->sectorsize; 1259172302Spjd if (bs == 0) 1260172302Spjd break; 1261172302Spjd LOG_MSG(LVL_ERROR, "Trouble: map is not sector-aligned " 1262172302Spjd "for %s on %s", sc->geom->name, 1263172302Spjd sc->components[0].gcons->provider->name); 1264172302Spjd } 1265172302Spjd mapbuf = g_read_data(sc->components[0].gcons, off, bs, &error); 1266172302Spjd if (mapbuf == NULL) { 1267172302Spjd free(sc->map, M_GVIRSTOR); 1268172302Spjd LOG_MSG(LVL_ERROR, "Error reading allocation map " 1269172302Spjd "for %s from %s (offset %ju) (error %d)", 1270172302Spjd sc->geom->name, 1271172302Spjd sc->components[0].gcons->provider->name, 1272172302Spjd off, error); 1273172302Spjd return; 1274172302Spjd } 1275172302Spjd 1276172302Spjd bcopy(mapbuf, &sc->map[n], bs); 1277172302Spjd off += bs; 1278172302Spjd count += bs; 1279172302Spjd n += bs / sizeof *(sc->map); 1280172302Spjd g_free(mapbuf); 1281172302Spjd } 1282172302Spjd g_access(sc->components[0].gcons, -1, 0, 0); 1283172302Spjd LOG_MSG(LVL_DEBUG, "Read map for %s", sc->geom->name); 1284172302Spjd 1285172302Spjd /* find first component with allocatable chunks */ 1286172302Spjd index = -1; 1287172302Spjd for (n = 0; n < sc->n_components; n++) { 1288172302Spjd if (sc->components[n].chunk_next < 1289172302Spjd sc->components[n].chunk_count) { 1290172302Spjd index = n; 1291172302Spjd break; 1292172302Spjd } 1293172302Spjd } 1294172302Spjd if (index == -1) 1295172302Spjd /* not found? set it to the last component and handle it 1296172302Spjd * later */ 1297172302Spjd index = sc->n_components - 1; 1298172302Spjd 1299172302Spjd if (index >= sc->n_components - g_virstor_component_watermark - 1) { 1300172302Spjd LOG_MSG(LVL_WARNING, "Device %s running out of components " 1301172302Spjd "(%d/%u: %s)", sc->geom->name, 1302172302Spjd index+1, 1303172302Spjd sc->n_components, 1304172302Spjd sc->components[index].gcons->provider->name); 1305172302Spjd } 1306172302Spjd sc->curr_component = index; 1307172302Spjd 1308172302Spjd if (sc->components[index].chunk_next >= 1309172302Spjd sc->components[index].chunk_count - g_virstor_chunk_watermark) { 1310172302Spjd LOG_MSG(LVL_WARNING, 1311172302Spjd "Component %s of %s is running out of free space " 1312172302Spjd "(%u chunks left)", 1313172302Spjd sc->components[index].gcons->provider->name, 1314172302Spjd sc->geom->name, sc->components[index].chunk_count - 1315172302Spjd sc->components[index].chunk_next); 1316172302Spjd } 1317172302Spjd 1318172302Spjd sc->me_per_sector = sc->sectorsize / sizeof *(sc->map); 1319172302Spjd if (sc->sectorsize % sizeof *(sc->map) != 0) { 1320172302Spjd LOG_MSG(LVL_ERROR, 1321172302Spjd "%s: Map entries don't fit exactly in a sector (%s)", 1322172302Spjd __func__, sc->geom->name); 1323172302Spjd return; 1324172302Spjd } 1325172302Spjd 1326172302Spjd /* Recalculate allocated chunks in components & at the same time 1327172302Spjd * verify map data is sane. We could trust metadata on this, but 1328172302Spjd * we want to make sure. */ 1329172302Spjd for (n = 0; n < sc->n_components; n++) 1330172302Spjd sc->components[n].chunk_next = sc->components[n].chunk_reserved; 1331172302Spjd 1332172302Spjd for (n = 0; n < sc->chunk_count; n++) { 1333172302Spjd if (sc->map[n].provider_no >= sc->n_components || 1334172302Spjd sc->map[n].provider_chunk >= 1335172302Spjd sc->components[sc->map[n].provider_no].chunk_count) { 1336172302Spjd LOG_MSG(LVL_ERROR, "%s: Invalid entry %u in map for %s", 1337172302Spjd __func__, (u_int)n, sc->geom->name); 1338172302Spjd LOG_MSG(LVL_ERROR, "%s: provider_no: %u, n_components: %u" 1339172302Spjd " provider_chunk: %u, chunk_count: %u", __func__, 1340172302Spjd sc->map[n].provider_no, sc->n_components, 1341172302Spjd sc->map[n].provider_chunk, 1342172302Spjd sc->components[sc->map[n].provider_no].chunk_count); 1343172302Spjd return; 1344172302Spjd } 1345172302Spjd if (sc->map[n].flags & VIRSTOR_MAP_ALLOCATED) 1346172302Spjd sc->components[sc->map[n].provider_no].chunk_next++; 1347172302Spjd } 1348172302Spjd 1349172302Spjd sc->provider = g_new_providerf(sc->geom, "virstor/%s", 1350172302Spjd sc->geom->name); 1351172302Spjd 1352172302Spjd sc->provider->sectorsize = sc->sectorsize; 1353172302Spjd sc->provider->mediasize = sc->virsize; 1354172302Spjd g_error_provider(sc->provider, 0); 1355172302Spjd 1356172302Spjd LOG_MSG(LVL_INFO, "%s activated", sc->provider->name); 1357172302Spjd LOG_MSG(LVL_DEBUG, "%s starting with current component %u, starting " 1358172302Spjd "chunk %u", sc->provider->name, sc->curr_component, 1359172302Spjd sc->components[sc->curr_component].chunk_next); 1360172302Spjd} 1361172302Spjd 1362172302Spjd/* 1363172302Spjd * Returns count of active providers in this geom instance 1364172302Spjd */ 1365172302Spjdstatic u_int 1366172302Spjdvirstor_valid_components(struct g_virstor_softc *sc) 1367172302Spjd{ 1368172302Spjd unsigned int nc, i; 1369172302Spjd 1370172302Spjd nc = 0; 1371172302Spjd KASSERT(sc != NULL, ("%s: softc is NULL", __func__)); 1372172302Spjd KASSERT(sc->components != NULL, ("%s: sc->components is NULL", __func__)); 1373172302Spjd for (i = 0; i < sc->n_components; i++) 1374172302Spjd if (sc->components[i].gcons != NULL) 1375172302Spjd nc++; 1376172302Spjd return (nc); 1377172302Spjd} 1378172302Spjd 1379172302Spjd/* 1380172302Spjd * Called when the consumer gets orphaned (?) 1381172302Spjd */ 1382172302Spjdstatic void 1383172302Spjdg_virstor_orphan(struct g_consumer *cp) 1384172302Spjd{ 1385172302Spjd struct g_virstor_softc *sc; 1386172302Spjd struct g_virstor_component *comp; 1387172302Spjd struct g_geom *gp; 1388172302Spjd 1389172302Spjd g_topology_assert(); 1390172302Spjd gp = cp->geom; 1391172302Spjd sc = gp->softc; 1392172302Spjd if (sc == NULL) 1393172302Spjd return; 1394172302Spjd 1395172302Spjd comp = cp->private; 1396172302Spjd KASSERT(comp != NULL, ("%s: No component in private part of consumer", 1397172302Spjd __func__)); 1398172302Spjd remove_component(sc, comp, FALSE); 1399172302Spjd if (virstor_valid_components(sc) == 0) 1400172302Spjd virstor_geom_destroy(sc, TRUE, FALSE); 1401172302Spjd} 1402172302Spjd 1403172302Spjd/* 1404172302Spjd * Called to notify geom when it's been opened, and for what intent 1405172302Spjd */ 1406172302Spjdstatic int 1407172302Spjdg_virstor_access(struct g_provider *pp, int dr, int dw, int de) 1408172302Spjd{ 1409172302Spjd struct g_consumer *c; 1410172302Spjd struct g_virstor_softc *sc; 1411172302Spjd struct g_geom *gp; 1412172302Spjd int error; 1413172302Spjd 1414172302Spjd KASSERT(pp != NULL, ("%s: NULL provider", __func__)); 1415172302Spjd gp = pp->geom; 1416172302Spjd KASSERT(gp != NULL, ("%s: NULL geom", __func__)); 1417172302Spjd sc = gp->softc; 1418172302Spjd 1419172302Spjd if (sc == NULL) { 1420172302Spjd /* It seems that .access can be called with negative dr,dw,dx 1421172302Spjd * in this case but I want to check for myself */ 1422172302Spjd LOG_MSG(LVL_WARNING, "access(%d, %d, %d) for %s", 1423172302Spjd dr, dw, de, pp->name); 1424172302Spjd /* This should only happen when geom is withered so 1425172302Spjd * allow only negative requests */ 1426172302Spjd KASSERT(dr <= 0 && dw <= 0 && de <= 0, 1427172302Spjd ("%s: Positive access for %s", __func__, pp->name)); 1428172302Spjd if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0) 1429172302Spjd LOG_MSG(LVL_DEBUG, "Device %s definitely destroyed", 1430172302Spjd pp->name); 1431172302Spjd return (0); 1432172302Spjd } 1433172302Spjd 1434172302Spjd /* Grab an exclusive bit to propagate on our consumers on first open */ 1435172302Spjd if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) 1436172302Spjd de++; 1437172302Spjd /* ... drop it on close */ 1438172302Spjd if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0) { 1439172302Spjd de--; 1440172302Spjd update_metadata(sc); /* Writes statistical information */ 1441172302Spjd } 1442172302Spjd 1443172302Spjd error = ENXIO; 1444172302Spjd LIST_FOREACH(c, &gp->consumer, consumer) { 1445172302Spjd KASSERT(c != NULL, ("%s: consumer is NULL", __func__)); 1446172302Spjd error = g_access(c, dr, dw, de); 1447172302Spjd if (error != 0) { 1448172302Spjd struct g_consumer *c2; 1449172302Spjd 1450172302Spjd /* Backout earlier changes */ 1451172302Spjd LIST_FOREACH(c2, &gp->consumer, consumer) { 1452172302Spjd if (c2 == c) /* all eariler components fixed */ 1453172302Spjd return (error); 1454172302Spjd g_access(c2, -dr, -dw, -de); 1455172302Spjd } 1456172302Spjd } 1457172302Spjd } 1458172302Spjd 1459172302Spjd return (error); 1460172302Spjd} 1461172302Spjd 1462172302Spjd/* 1463172302Spjd * Generate XML dump of current state 1464172302Spjd */ 1465172302Spjdstatic void 1466172302Spjdg_virstor_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1467172302Spjd struct g_consumer *cp, struct g_provider *pp) 1468172302Spjd{ 1469172302Spjd struct g_virstor_softc *sc; 1470172302Spjd 1471172302Spjd g_topology_assert(); 1472172302Spjd sc = gp->softc; 1473172302Spjd 1474172302Spjd if (sc == NULL || pp != NULL) 1475172302Spjd return; 1476172302Spjd 1477172302Spjd if (cp != NULL) { 1478172302Spjd /* For each component */ 1479172302Spjd struct g_virstor_component *comp; 1480172302Spjd 1481172302Spjd comp = cp->private; 1482172302Spjd if (comp == NULL) 1483172302Spjd return; 1484172302Spjd sbuf_printf(sb, "%s<ComponentIndex>%u</ComponentIndex>\n", 1485172302Spjd indent, comp->index); 1486172302Spjd sbuf_printf(sb, "%s<ChunkCount>%u</ChunkCount>\n", 1487172302Spjd indent, comp->chunk_count); 1488172302Spjd sbuf_printf(sb, "%s<ChunksUsed>%u</ChunksUsed>\n", 1489172302Spjd indent, comp->chunk_next); 1490172302Spjd sbuf_printf(sb, "%s<ChunksReserved>%u</ChunksReserved>\n", 1491172302Spjd indent, comp->chunk_reserved); 1492172302Spjd sbuf_printf(sb, "%s<StorageFree>%u%%</StorageFree>\n", 1493172302Spjd indent, 1494172302Spjd comp->chunk_next > 0 ? 100 - 1495172302Spjd ((comp->chunk_next + comp->chunk_reserved) * 100) / 1496172302Spjd comp->chunk_count : 100); 1497172302Spjd } else { 1498172302Spjd /* For the whole thing */ 1499172302Spjd u_int count, used, i; 1500172302Spjd off_t size; 1501172302Spjd 1502172302Spjd count = used = size = 0; 1503172302Spjd for (i = 0; i < sc->n_components; i++) { 1504172302Spjd if (sc->components[i].gcons != NULL) { 1505172302Spjd count += sc->components[i].chunk_count; 1506172302Spjd used += sc->components[i].chunk_next + 1507172302Spjd sc->components[i].chunk_reserved; 1508172302Spjd size += sc->components[i].gcons-> 1509172302Spjd provider->mediasize; 1510172302Spjd } 1511172302Spjd } 1512172302Spjd 1513172302Spjd sbuf_printf(sb, "%s<Status>" 1514172302Spjd "Components=%u, Online=%u</Status>\n", indent, 1515172302Spjd sc->n_components, virstor_valid_components(sc)); 1516172302Spjd sbuf_printf(sb, "%s<State>%u%% physical free</State>\n", 1517172302Spjd indent, 100-(used * 100) / count); 1518172302Spjd sbuf_printf(sb, "%s<ChunkSize>%zu</ChunkSize>\n", indent, 1519172302Spjd sc->chunk_size); 1520172302Spjd sbuf_printf(sb, "%s<PhysicalFree>%u%%</PhysicalFree>\n", 1521172302Spjd indent, used > 0 ? 100 - (used * 100) / count : 100); 1522172302Spjd sbuf_printf(sb, "%s<ChunkPhysicalCount>%u</ChunkPhysicalCount>\n", 1523172302Spjd indent, count); 1524172302Spjd sbuf_printf(sb, "%s<ChunkVirtualCount>%zu</ChunkVirtualCount>\n", 1525172302Spjd indent, sc->chunk_count); 1526172302Spjd sbuf_printf(sb, "%s<PhysicalBacking>%zu%%</PhysicalBacking>\n", 1527172302Spjd indent, 1528172302Spjd (count * 100) / sc->chunk_count); 1529172302Spjd sbuf_printf(sb, "%s<PhysicalBackingSize>%jd</PhysicalBackingSize>\n", 1530172302Spjd indent, size); 1531172302Spjd sbuf_printf(sb, "%s<VirtualSize>%jd</VirtualSize>\n", indent, 1532172302Spjd sc->virsize); 1533172302Spjd } 1534172302Spjd} 1535172302Spjd 1536172302Spjd/* 1537172302Spjd * GEOM .done handler 1538172302Spjd * Can't use standard handler because one requested IO may 1539172302Spjd * fork into additional data IOs 1540172302Spjd */ 1541172302Spjdstatic void 1542172302Spjdg_virstor_done(struct bio *b) 1543172302Spjd{ 1544172302Spjd struct g_virstor_softc *sc; 1545172302Spjd struct bio *parent_b; 1546172302Spjd 1547172302Spjd parent_b = b->bio_parent; 1548172302Spjd sc = parent_b->bio_to->geom->softc; 1549172302Spjd 1550172302Spjd if (b->bio_error != 0) { 1551172302Spjd LOG_MSG(LVL_ERROR, "Error %d for offset=%ju, length=%ju, %s", 1552172302Spjd b->bio_error, b->bio_offset, b->bio_length, 1553172302Spjd b->bio_to->name); 1554172302Spjd if (parent_b->bio_error == 0) 1555172302Spjd parent_b->bio_error = b->bio_error; 1556172302Spjd } 1557172302Spjd 1558172302Spjd parent_b->bio_inbed++; 1559172302Spjd parent_b->bio_completed += b->bio_completed; 1560172302Spjd 1561172302Spjd if (parent_b->bio_children == parent_b->bio_inbed) { 1562172302Spjd parent_b->bio_completed = parent_b->bio_length; 1563172302Spjd g_io_deliver(parent_b, parent_b->bio_error); 1564172302Spjd } 1565172302Spjd g_destroy_bio(b); 1566172302Spjd} 1567172302Spjd 1568172302Spjd/* 1569172302Spjd * I/O starts here 1570172302Spjd * Called in g_down thread 1571172302Spjd */ 1572172302Spjdstatic void 1573172302Spjdg_virstor_start(struct bio *b) 1574172302Spjd{ 1575172302Spjd struct g_virstor_softc *sc; 1576172302Spjd struct g_virstor_component *comp; 1577172302Spjd struct bio *cb; 1578172302Spjd struct g_provider *pp; 1579172302Spjd char *addr; 1580172302Spjd off_t offset, length; 1581172302Spjd struct bio_queue_head bq; 1582172302Spjd size_t chunk_size; /* cached for convenience */ 1583172302Spjd u_int count; 1584172302Spjd 1585172302Spjd pp = b->bio_to; 1586172302Spjd sc = pp->geom->softc; 1587172302Spjd KASSERT(sc != NULL, ("%s: no softc (error=%d, device=%s)", __func__, 1588172302Spjd b->bio_to->error, b->bio_to->name)); 1589172302Spjd 1590172302Spjd LOG_REQ(LVL_MOREDEBUG, b, "%s", __func__); 1591172302Spjd 1592172302Spjd switch (b->bio_cmd) { 1593172302Spjd case BIO_READ: 1594172302Spjd case BIO_WRITE: 1595172302Spjd case BIO_DELETE: 1596172302Spjd break; 1597172302Spjd default: 1598172302Spjd g_io_deliver(b, EOPNOTSUPP); 1599172302Spjd return; 1600172302Spjd } 1601172302Spjd 1602172302Spjd LOG_MSG(LVL_DEBUG2, "BIO arrived, size=%ju", b->bio_length); 1603172302Spjd bioq_init(&bq); 1604172302Spjd 1605172302Spjd chunk_size = sc->chunk_size; 1606172302Spjd addr = b->bio_data; 1607172302Spjd offset = b->bio_offset; /* virtual offset and length */ 1608172302Spjd length = b->bio_length; 1609172302Spjd 1610172302Spjd while (length > 0) { 1611172302Spjd size_t chunk_index, in_chunk_offset, in_chunk_length; 1612172302Spjd struct virstor_map_entry *me; 1613172302Spjd 1614172302Spjd chunk_index = offset / chunk_size; /* round downwards */ 1615172302Spjd in_chunk_offset = offset % chunk_size; 1616172302Spjd in_chunk_length = min(length, chunk_size - in_chunk_offset); 1617172302Spjd LOG_MSG(LVL_DEBUG, "Mapped %s(%ju, %ju) to (%zu,%zu,%zu)", 1618172302Spjd b->bio_cmd == BIO_READ ? "R" : "W", 1619172302Spjd offset, length, 1620172302Spjd chunk_index, in_chunk_offset, in_chunk_length); 1621172302Spjd me = &sc->map[chunk_index]; 1622172302Spjd 1623172302Spjd if (b->bio_cmd == BIO_READ || b->bio_cmd == BIO_DELETE) { 1624172302Spjd if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) { 1625172302Spjd /* Reads from unallocated chunks return zeroed 1626172302Spjd * buffers */ 1627172302Spjd if (b->bio_cmd == BIO_READ) 1628172302Spjd bzero(addr, in_chunk_length); 1629172302Spjd } else { 1630172302Spjd comp = &sc->components[me->provider_no]; 1631172302Spjd 1632172302Spjd cb = g_clone_bio(b); 1633172302Spjd if (cb == NULL) { 1634172302Spjd bioq_dismantle(&bq); 1635172302Spjd if (b->bio_error == 0) 1636172302Spjd b->bio_error = ENOMEM; 1637172302Spjd g_io_deliver(b, b->bio_error); 1638172302Spjd return; 1639172302Spjd } 1640172302Spjd cb->bio_to = comp->gcons->provider; 1641172302Spjd cb->bio_done = g_virstor_done; 1642172302Spjd cb->bio_offset = 1643172302Spjd (off_t)me->provider_chunk * (off_t)chunk_size 1644172302Spjd + in_chunk_offset; 1645172302Spjd cb->bio_length = in_chunk_length; 1646172302Spjd cb->bio_data = addr; 1647172302Spjd cb->bio_caller1 = comp; 1648172302Spjd bioq_disksort(&bq, cb); 1649172302Spjd } 1650172302Spjd } else { /* handle BIO_WRITE */ 1651172302Spjd KASSERT(b->bio_cmd == BIO_WRITE, 1652172302Spjd ("%s: Unknown command %d", __func__, 1653172302Spjd b->bio_cmd)); 1654172302Spjd 1655172302Spjd if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) { 1656172302Spjd /* We have a virtual chunk, represented by 1657172302Spjd * the "me" entry, but it's not yet allocated 1658172302Spjd * (tied to) a physical chunk. So do it now. */ 1659172302Spjd struct virstor_map_entry *data_me; 1660172302Spjd u_int phys_chunk, comp_no; 1661172302Spjd off_t s_offset; 1662172302Spjd int error; 1663172302Spjd 1664172302Spjd error = allocate_chunk(sc, &comp, &comp_no, 1665172302Spjd &phys_chunk); 1666172302Spjd if (error != 0) { 1667172302Spjd /* We cannot allocate a physical chunk 1668172302Spjd * to satisfy this request, so we'll 1669172302Spjd * delay it to when we can... 1670172302Spjd * XXX: this will prevent the fs from 1671172302Spjd * being umounted! */ 1672172302Spjd struct g_virstor_bio_q *biq; 1673172302Spjd biq = malloc(sizeof *biq, M_GVIRSTOR, 1674172302Spjd M_NOWAIT); 1675172302Spjd if (biq == NULL) { 1676172302Spjd bioq_dismantle(&bq); 1677172302Spjd if (b->bio_error == 0) 1678172302Spjd b->bio_error = ENOMEM; 1679172302Spjd g_io_deliver(b, b->bio_error); 1680172302Spjd return; 1681172302Spjd } 1682172302Spjd biq->bio = b; 1683172302Spjd mtx_lock(&sc->delayed_bio_q_mtx); 1684172302Spjd STAILQ_INSERT_TAIL(&sc->delayed_bio_q, 1685172302Spjd biq, linkage); 1686172302Spjd mtx_unlock(&sc->delayed_bio_q_mtx); 1687172302Spjd LOG_MSG(LVL_WARNING, "Delaying BIO " 1688172302Spjd "(size=%ju) until free physical " 1689172302Spjd "space can be found on %s", 1690172302Spjd b->bio_length, 1691172302Spjd sc->provider->name); 1692172302Spjd return; 1693172302Spjd } 1694172302Spjd LOG_MSG(LVL_DEBUG, "Allocated chunk %u on %s " 1695172302Spjd "for %s", 1696172302Spjd phys_chunk, 1697172302Spjd comp->gcons->provider->name, 1698172302Spjd sc->provider->name); 1699172302Spjd 1700172302Spjd me->provider_no = comp_no; 1701172302Spjd me->provider_chunk = phys_chunk; 1702172302Spjd me->flags |= VIRSTOR_MAP_ALLOCATED; 1703172302Spjd 1704172302Spjd cb = g_clone_bio(b); 1705172302Spjd if (cb == NULL) { 1706172302Spjd me->flags &= ~VIRSTOR_MAP_ALLOCATED; 1707172302Spjd me->provider_no = 0; 1708172302Spjd me->provider_chunk = 0; 1709172302Spjd bioq_dismantle(&bq); 1710172302Spjd if (b->bio_error == 0) 1711172302Spjd b->bio_error = ENOMEM; 1712172302Spjd g_io_deliver(b, b->bio_error); 1713172302Spjd return; 1714172302Spjd } 1715172302Spjd 1716172302Spjd /* The allocation table is stored continuously 1717172302Spjd * at the start of the drive. We need to 1718172302Spjd * calculate the offset of the sector that holds 1719172302Spjd * this map entry both on the drive and in the 1720172302Spjd * map array. 1721172302Spjd * sc_offset will end up pointing to the drive 1722172302Spjd * sector. */ 1723172302Spjd s_offset = chunk_index * sizeof *me; 1724172302Spjd s_offset = (s_offset / sc->sectorsize) * 1725172302Spjd sc->sectorsize; 1726172302Spjd 1727172302Spjd /* data_me points to map entry sector 1728172302Spjd * in memory (analoguos to offset) */ 1729172302Spjd data_me = &sc->map[(chunk_index / 1730172302Spjd sc->me_per_sector) * sc->me_per_sector]; 1731172302Spjd 1732172302Spjd /* Commit sector with map entry to storage */ 1733172302Spjd cb->bio_to = sc->components[0].gcons->provider; 1734172302Spjd cb->bio_done = g_virstor_done; 1735172302Spjd cb->bio_offset = s_offset; 1736172302Spjd cb->bio_data = (char *)data_me; 1737172302Spjd cb->bio_length = sc->sectorsize; 1738172302Spjd cb->bio_caller1 = &sc->components[0]; 1739172302Spjd bioq_disksort(&bq, cb); 1740172302Spjd } 1741172302Spjd 1742172302Spjd comp = &sc->components[me->provider_no]; 1743172302Spjd cb = g_clone_bio(b); 1744172302Spjd if (cb == NULL) { 1745172302Spjd bioq_dismantle(&bq); 1746172302Spjd if (b->bio_error == 0) 1747172302Spjd b->bio_error = ENOMEM; 1748172302Spjd g_io_deliver(b, b->bio_error); 1749172302Spjd return; 1750172302Spjd } 1751172302Spjd /* Finally, handle the data */ 1752172302Spjd cb->bio_to = comp->gcons->provider; 1753172302Spjd cb->bio_done = g_virstor_done; 1754172302Spjd cb->bio_offset = (off_t)me->provider_chunk*(off_t)chunk_size + 1755172302Spjd in_chunk_offset; 1756172302Spjd cb->bio_length = in_chunk_length; 1757172302Spjd cb->bio_data = addr; 1758172302Spjd cb->bio_caller1 = comp; 1759172302Spjd bioq_disksort(&bq, cb); 1760172302Spjd } 1761172302Spjd addr += in_chunk_length; 1762172302Spjd length -= in_chunk_length; 1763172302Spjd offset += in_chunk_length; 1764172302Spjd } 1765172302Spjd 1766172302Spjd /* Fire off bio's here */ 1767172302Spjd count = 0; 1768172302Spjd for (cb = bioq_first(&bq); cb != NULL; cb = bioq_first(&bq)) { 1769172302Spjd bioq_remove(&bq, cb); 1770172302Spjd LOG_REQ(LVL_MOREDEBUG, cb, "Firing request"); 1771172302Spjd comp = cb->bio_caller1; 1772172302Spjd cb->bio_caller1 = NULL; 1773172302Spjd LOG_MSG(LVL_DEBUG, " firing bio, offset=%ju, length=%ju", 1774172302Spjd cb->bio_offset, cb->bio_length); 1775172302Spjd g_io_request(cb, comp->gcons); 1776172302Spjd count++; 1777172302Spjd } 1778172302Spjd if (count == 0) { /* We handled everything locally */ 1779172302Spjd b->bio_completed = b->bio_length; 1780172302Spjd g_io_deliver(b, 0); 1781172302Spjd } 1782172302Spjd 1783172302Spjd} 1784172302Spjd 1785172302Spjd/* 1786172302Spjd * Allocate a chunk from a physical provider. Returns physical component, 1787172302Spjd * chunk index relative to the component and the component's index. 1788172302Spjd */ 1789172302Spjdstatic int 1790172302Spjdallocate_chunk(struct g_virstor_softc *sc, struct g_virstor_component **comp, 1791172302Spjd u_int *comp_no_p, u_int *chunk) 1792172302Spjd{ 1793172302Spjd u_int comp_no; 1794172302Spjd 1795172302Spjd KASSERT(sc->curr_component < sc->n_components, 1796172302Spjd ("%s: Invalid curr_component: %u", __func__, sc->curr_component)); 1797172302Spjd 1798172302Spjd comp_no = sc->curr_component; 1799172302Spjd *comp = &sc->components[comp_no]; 1800172302Spjd dump_component(*comp); 1801172302Spjd if ((*comp)->chunk_next >= (*comp)->chunk_count) { 1802172302Spjd /* This component is full. Allocate next component */ 1803172302Spjd if (comp_no >= sc->n_components-1) { 1804172302Spjd LOG_MSG(LVL_ERROR, "All physical space allocated for %s", 1805172302Spjd sc->geom->name); 1806172302Spjd return (-1); 1807172302Spjd } 1808172302Spjd (*comp)->flags &= ~VIRSTOR_PROVIDER_CURRENT; 1809172302Spjd sc->curr_component = ++comp_no; 1810172302Spjd 1811172302Spjd *comp = &sc->components[comp_no]; 1812172302Spjd if (comp_no >= sc->n_components - g_virstor_component_watermark-1) 1813172302Spjd LOG_MSG(LVL_WARNING, "Device %s running out of components " 1814172302Spjd "(switching to %u/%u: %s)", sc->geom->name, 1815172302Spjd comp_no+1, sc->n_components, 1816172302Spjd (*comp)->gcons->provider->name); 1817172302Spjd /* Take care not to overwrite reserved chunks */ 1818172302Spjd if ( (*comp)->chunk_reserved > 0 && 1819172302Spjd (*comp)->chunk_next < (*comp)->chunk_reserved) 1820172302Spjd (*comp)->chunk_next = (*comp)->chunk_reserved; 1821172302Spjd 1822172302Spjd (*comp)->flags |= 1823172302Spjd VIRSTOR_PROVIDER_ALLOCATED | VIRSTOR_PROVIDER_CURRENT; 1824172302Spjd dump_component(*comp); 1825172302Spjd *comp_no_p = comp_no; 1826172302Spjd *chunk = (*comp)->chunk_next++; 1827172302Spjd } else { 1828172302Spjd *comp_no_p = comp_no; 1829172302Spjd *chunk = (*comp)->chunk_next++; 1830172302Spjd } 1831172302Spjd return (0); 1832172302Spjd} 1833172302Spjd 1834172302Spjd/* Dump a component */ 1835172302Spjdstatic void 1836172302Spjddump_component(struct g_virstor_component *comp) 1837172302Spjd{ 1838172302Spjd 1839172302Spjd if (g_virstor_debug < LVL_DEBUG2) 1840172302Spjd return; 1841172302Spjd printf("Component %d: %s\n", comp->index, comp->gcons->provider->name); 1842172302Spjd printf(" chunk_count: %u\n", comp->chunk_count); 1843172302Spjd printf(" chunk_next: %u\n", comp->chunk_next); 1844172302Spjd printf(" flags: %u\n", comp->flags); 1845172302Spjd} 1846172302Spjd 1847172302Spjd#if 0 1848172302Spjd/* Dump a map entry */ 1849172302Spjdstatic void 1850172302Spjddump_me(struct virstor_map_entry *me, unsigned int nr) 1851172302Spjd{ 1852172302Spjd if (g_virstor_debug < LVL_DEBUG) 1853172302Spjd return; 1854172302Spjd printf("VIRT. CHUNK #%d: ", nr); 1855172302Spjd if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) 1856172302Spjd printf("(unallocated)\n"); 1857172302Spjd else 1858172302Spjd printf("allocated at provider %u, provider_chunk %u\n", 1859172302Spjd me->provider_no, me->provider_chunk); 1860172302Spjd} 1861172302Spjd#endif 1862172302Spjd 1863172302Spjd/* 1864172302Spjd * Dismantle bio_queue and destroy its components 1865172302Spjd */ 1866172302Spjdstatic void 1867172302Spjdbioq_dismantle(struct bio_queue_head *bq) 1868172302Spjd{ 1869172302Spjd struct bio *b; 1870172302Spjd 1871172302Spjd for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) { 1872172302Spjd bioq_remove(bq, b); 1873172302Spjd g_destroy_bio(b); 1874172302Spjd } 1875172302Spjd} 1876172302Spjd 1877172302Spjd/* 1878172302Spjd * The function that shouldn't be called. 1879172302Spjd * When this is called, the stack is already garbled because of 1880172302Spjd * argument mismatch. There's nothing to do now but panic, which is 1881172302Spjd * accidentally the whole purpose of this function. 1882172302Spjd * Motivation: to guard from accidentally calling geom methods when 1883172302Spjd * they shouldn't be called. (see g_..._taste) 1884172302Spjd */ 1885172302Spjdstatic void 1886172302Spjdinvalid_call(void) 1887172302Spjd{ 1888172302Spjd panic("invalid_call() has just been called. Something's fishy here."); 1889172302Spjd} 1890172302Spjd 1891172302SpjdDECLARE_GEOM_CLASS(g_virstor_class, g_virstor); /* Let there be light */ 1892