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