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