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