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