1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2002 Poul-Henning Kamp 5 * Copyright (c) 2002 Networks Associates Technology, Inc. 6 * All rights reserved. 7 * 8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 9 * and NAI Labs, the Security Research Division of Network Associates, Inc. 10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 11 * DARPA CHATS research program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. The names of the authors may not be used to endorse or promote 22 * products derived from this software without specific prior written 23 * permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 #include "opt_ddb.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/devicestat.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/bio.h> 47 #include <sys/proc.h> 48 #include <sys/kthread.h> 49 #include <sys/lock.h> 50 #include <sys/mutex.h> 51 #include <sys/errno.h> 52 #include <sys/sbuf.h> 53 #include <sys/sdt.h> 54 #include <geom/geom.h> 55 #include <geom/geom_dbg.h> 56 #include <geom/geom_int.h> 57 #include <machine/stdarg.h> 58 59 #ifdef DDB 60 #include <ddb/ddb.h> 61 #endif 62 63 #ifdef KDB 64 #include <sys/kdb.h> 65 #endif 66 67 SDT_PROVIDER_DEFINE(geom); 68 69 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes); 70 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms); 71 char *g_wait_event, *g_wait_up, *g_wait_down; 72 73 struct g_hh00 { 74 struct g_class *mp; 75 struct g_provider *pp; 76 off_t size; 77 int error; 78 int post; 79 }; 80 81 void 82 g_dbg_printf(const char *classname, int lvl, struct bio *bp, 83 const char *format, 84 ...) 85 { 86 #ifndef PRINTF_BUFR_SIZE 87 #define PRINTF_BUFR_SIZE 64 88 #endif 89 char bufr[PRINTF_BUFR_SIZE]; 90 struct sbuf sb, *sbp __unused; 91 va_list ap; 92 93 sbp = sbuf_new(&sb, bufr, sizeof(bufr), SBUF_FIXEDLEN); 94 KASSERT(sbp != NULL, ("sbuf_new misused?")); 95 96 sbuf_set_drain(&sb, sbuf_printf_drain, NULL); 97 98 sbuf_cat(&sb, classname); 99 if (lvl >= 0) 100 sbuf_printf(&sb, "[%d]", lvl); 101 102 va_start(ap, format); 103 sbuf_vprintf(&sb, format, ap); 104 va_end(ap); 105 106 if (bp != NULL) { 107 sbuf_putc(&sb, ' '); 108 g_format_bio(&sb, bp); 109 } 110 111 /* Terminate the debug line with a single '\n'. */ 112 sbuf_nl_terminate(&sb); 113 114 /* Flush line to printf. */ 115 sbuf_finish(&sb); 116 sbuf_delete(&sb); 117 } 118 119 /* 120 * This event offers a new class a chance to taste all preexisting providers. 121 */ 122 static void 123 g_load_class(void *arg, int flag) 124 { 125 struct g_hh00 *hh; 126 struct g_class *mp2, *mp; 127 struct g_geom *gp; 128 struct g_provider *pp; 129 130 g_topology_assert(); 131 if (flag == EV_CANCEL) /* XXX: can't happen ? */ 132 return; 133 if (g_shutdown) 134 return; 135 136 hh = arg; 137 mp = hh->mp; 138 hh->error = 0; 139 if (hh->post) { 140 g_free(hh); 141 hh = NULL; 142 } 143 g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name); 144 KASSERT(mp->name != NULL && *mp->name != '\0', 145 ("GEOM class has no name")); 146 LIST_FOREACH(mp2, &g_classes, class) { 147 if (mp2 == mp) { 148 printf("The GEOM class %s is already loaded.\n", 149 mp2->name); 150 if (hh != NULL) 151 hh->error = EEXIST; 152 return; 153 } else if (strcmp(mp2->name, mp->name) == 0) { 154 printf("A GEOM class %s is already loaded.\n", 155 mp2->name); 156 if (hh != NULL) 157 hh->error = EEXIST; 158 return; 159 } 160 } 161 162 LIST_INIT(&mp->geom); 163 LIST_INSERT_HEAD(&g_classes, mp, class); 164 if (mp->init != NULL) 165 mp->init(mp); 166 if (mp->taste == NULL) 167 return; 168 LIST_FOREACH(mp2, &g_classes, class) { 169 if (mp == mp2) 170 continue; 171 LIST_FOREACH(gp, &mp2->geom, geom) { 172 LIST_FOREACH(pp, &gp->provider, provider) { 173 mp->taste(mp, pp, 0); 174 g_topology_assert(); 175 } 176 } 177 } 178 } 179 180 static int 181 g_unload_class(struct g_class *mp) 182 { 183 struct g_geom *gp; 184 struct g_provider *pp; 185 struct g_consumer *cp; 186 int error; 187 188 g_topology_lock(); 189 g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name); 190 retry: 191 G_VALID_CLASS(mp); 192 LIST_FOREACH(gp, &mp->geom, geom) { 193 /* We refuse to unload if anything is open */ 194 LIST_FOREACH(pp, &gp->provider, provider) 195 if (pp->acr || pp->acw || pp->ace) { 196 g_topology_unlock(); 197 return (EBUSY); 198 } 199 LIST_FOREACH(cp, &gp->consumer, consumer) 200 if (cp->acr || cp->acw || cp->ace) { 201 g_topology_unlock(); 202 return (EBUSY); 203 } 204 /* If the geom is withering, wait for it to finish. */ 205 if (gp->flags & G_GEOM_WITHER) { 206 g_topology_sleep(mp, 1); 207 goto retry; 208 } 209 } 210 211 /* 212 * We allow unloading if we have no geoms, or a class 213 * method we can use to get rid of them. 214 */ 215 if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) { 216 g_topology_unlock(); 217 return (EOPNOTSUPP); 218 } 219 220 /* Bar new entries */ 221 mp->taste = NULL; 222 223 LIST_FOREACH(gp, &mp->geom, geom) { 224 error = mp->destroy_geom(NULL, mp, gp); 225 if (error != 0) { 226 g_topology_unlock(); 227 return (error); 228 } 229 } 230 /* Wait for withering to finish. */ 231 for (;;) { 232 gp = LIST_FIRST(&mp->geom); 233 if (gp == NULL) 234 break; 235 KASSERT(gp->flags & G_GEOM_WITHER, 236 ("Non-withering geom in class %s", mp->name)); 237 g_topology_sleep(mp, 1); 238 } 239 G_VALID_CLASS(mp); 240 if (mp->fini != NULL) 241 mp->fini(mp); 242 LIST_REMOVE(mp, class); 243 g_topology_unlock(); 244 245 return (0); 246 } 247 248 int 249 g_modevent(module_t mod, int type, void *data) 250 { 251 struct g_hh00 *hh; 252 int error; 253 static int g_ignition; 254 struct g_class *mp; 255 256 mp = data; 257 if (mp->version != G_VERSION) { 258 printf("GEOM class %s has Wrong version %x\n", 259 mp->name, mp->version); 260 return (EINVAL); 261 } 262 if (!g_ignition) { 263 g_ignition++; 264 g_init(); 265 } 266 error = EOPNOTSUPP; 267 switch (type) { 268 case MOD_LOAD: 269 g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", mp->name); 270 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO); 271 hh->mp = mp; 272 /* 273 * Once the system is not cold, MOD_LOAD calls will be 274 * from the userland and the g_event thread will be able 275 * to acknowledge their completion. 276 */ 277 if (cold) { 278 hh->post = 1; 279 error = g_post_event(g_load_class, hh, M_WAITOK, NULL); 280 } else { 281 error = g_waitfor_event(g_load_class, hh, M_WAITOK, 282 NULL); 283 if (error == 0) 284 error = hh->error; 285 g_free(hh); 286 } 287 break; 288 case MOD_UNLOAD: 289 g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", mp->name); 290 error = g_unload_class(mp); 291 if (error == 0) { 292 KASSERT(LIST_EMPTY(&mp->geom), 293 ("Unloaded class (%s) still has geom", mp->name)); 294 } 295 break; 296 } 297 return (error); 298 } 299 300 static void 301 g_retaste_event(void *arg, int flag) 302 { 303 struct g_class *mp, *mp2; 304 struct g_geom *gp; 305 struct g_hh00 *hh; 306 struct g_provider *pp; 307 struct g_consumer *cp; 308 309 g_topology_assert(); 310 if (flag == EV_CANCEL) /* XXX: can't happen ? */ 311 return; 312 if (g_shutdown || g_notaste) 313 return; 314 315 hh = arg; 316 mp = hh->mp; 317 hh->error = 0; 318 if (hh->post) { 319 g_free(hh); 320 hh = NULL; 321 } 322 g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name); 323 324 LIST_FOREACH(mp2, &g_classes, class) { 325 LIST_FOREACH(gp, &mp2->geom, geom) { 326 LIST_FOREACH(pp, &gp->provider, provider) { 327 if (pp->acr || pp->acw || pp->ace) 328 continue; 329 LIST_FOREACH(cp, &pp->consumers, consumers) { 330 if (cp->geom->class == mp && 331 (cp->flags & G_CF_ORPHAN) == 0) 332 break; 333 } 334 if (cp != NULL) { 335 cp->flags |= G_CF_ORPHAN; 336 g_wither_geom(cp->geom, ENXIO); 337 } 338 mp->taste(mp, pp, 0); 339 g_topology_assert(); 340 } 341 } 342 } 343 } 344 345 int 346 g_retaste(struct g_class *mp) 347 { 348 struct g_hh00 *hh; 349 int error; 350 351 if (mp->taste == NULL) 352 return (EINVAL); 353 354 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO); 355 hh->mp = mp; 356 357 if (cold) { 358 hh->post = 1; 359 error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL); 360 } else { 361 error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL); 362 if (error == 0) 363 error = hh->error; 364 g_free(hh); 365 } 366 367 return (error); 368 } 369 370 struct g_geom * 371 g_new_geomf(struct g_class *mp, const char *fmt, ...) 372 { 373 struct g_geom *gp; 374 va_list ap; 375 struct sbuf *sb; 376 377 g_topology_assert(); 378 G_VALID_CLASS(mp); 379 sb = sbuf_new_auto(); 380 va_start(ap, fmt); 381 sbuf_vprintf(sb, fmt, ap); 382 va_end(ap); 383 sbuf_finish(sb); 384 gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO); 385 gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO); 386 gp->class = mp; 387 gp->rank = 1; 388 LIST_INIT(&gp->consumer); 389 LIST_INIT(&gp->provider); 390 LIST_INSERT_HEAD(&mp->geom, gp, geom); 391 TAILQ_INSERT_HEAD(&geoms, gp, geoms); 392 strcpy(gp->name, sbuf_data(sb)); 393 sbuf_delete(sb); 394 /* Fill in defaults from class */ 395 gp->start = mp->start; 396 gp->spoiled = mp->spoiled; 397 gp->attrchanged = mp->attrchanged; 398 gp->providergone = mp->providergone; 399 gp->dumpconf = mp->dumpconf; 400 gp->access = mp->access; 401 gp->orphan = mp->orphan; 402 gp->ioctl = mp->ioctl; 403 gp->resize = mp->resize; 404 return (gp); 405 } 406 407 void 408 g_destroy_geom(struct g_geom *gp) 409 { 410 411 g_topology_assert(); 412 G_VALID_GEOM(gp); 413 g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name); 414 KASSERT(LIST_EMPTY(&gp->consumer), 415 ("g_destroy_geom(%s) with consumer(s) [%p]", 416 gp->name, LIST_FIRST(&gp->consumer))); 417 KASSERT(LIST_EMPTY(&gp->provider), 418 ("g_destroy_geom(%s) with provider(s) [%p]", 419 gp->name, LIST_FIRST(&gp->provider))); 420 g_cancel_event(gp); 421 LIST_REMOVE(gp, geom); 422 TAILQ_REMOVE(&geoms, gp, geoms); 423 g_free(gp->name); 424 g_free(gp); 425 } 426 427 /* 428 * This function is called (repeatedly) until the geom has withered away. 429 */ 430 void 431 g_wither_geom(struct g_geom *gp, int error) 432 { 433 struct g_provider *pp; 434 435 g_topology_assert(); 436 G_VALID_GEOM(gp); 437 g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name); 438 if (!(gp->flags & G_GEOM_WITHER)) { 439 gp->flags |= G_GEOM_WITHER; 440 LIST_FOREACH(pp, &gp->provider, provider) 441 if (!(pp->flags & G_PF_ORPHAN)) 442 g_orphan_provider(pp, error); 443 } 444 g_do_wither(); 445 } 446 447 /* 448 * Convenience function to destroy a particular provider. 449 */ 450 void 451 g_wither_provider(struct g_provider *pp, int error) 452 { 453 454 pp->flags |= G_PF_WITHER; 455 if (!(pp->flags & G_PF_ORPHAN)) 456 g_orphan_provider(pp, error); 457 } 458 459 /* 460 * This function is called (repeatedly) until the has withered away. 461 */ 462 void 463 g_wither_geom_close(struct g_geom *gp, int error) 464 { 465 struct g_consumer *cp; 466 467 g_topology_assert(); 468 G_VALID_GEOM(gp); 469 g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name); 470 LIST_FOREACH(cp, &gp->consumer, consumer) 471 if (cp->acr || cp->acw || cp->ace) 472 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 473 g_wither_geom(gp, error); 474 } 475 476 /* 477 * This function is called (repeatedly) until we can't wash away more 478 * withered bits at present. 479 */ 480 void 481 g_wither_washer(void) 482 { 483 struct g_class *mp; 484 struct g_geom *gp, *gp2; 485 struct g_provider *pp, *pp2; 486 struct g_consumer *cp, *cp2; 487 488 g_topology_assert(); 489 LIST_FOREACH(mp, &g_classes, class) { 490 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 491 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) { 492 if (!(pp->flags & G_PF_WITHER)) 493 continue; 494 if (LIST_EMPTY(&pp->consumers)) 495 g_destroy_provider(pp); 496 } 497 if (!(gp->flags & G_GEOM_WITHER)) 498 continue; 499 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) { 500 if (LIST_EMPTY(&pp->consumers)) 501 g_destroy_provider(pp); 502 } 503 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) { 504 if (cp->acr || cp->acw || cp->ace) 505 continue; 506 if (cp->provider != NULL) 507 g_detach(cp); 508 g_destroy_consumer(cp); 509 } 510 if (LIST_EMPTY(&gp->provider) && 511 LIST_EMPTY(&gp->consumer)) 512 g_destroy_geom(gp); 513 } 514 } 515 } 516 517 struct g_consumer * 518 g_new_consumer(struct g_geom *gp) 519 { 520 struct g_consumer *cp; 521 522 g_topology_assert(); 523 G_VALID_GEOM(gp); 524 KASSERT(!(gp->flags & G_GEOM_WITHER), 525 ("g_new_consumer on WITHERing geom(%s) (class %s)", 526 gp->name, gp->class->name)); 527 KASSERT(gp->orphan != NULL, 528 ("g_new_consumer on geom(%s) (class %s) without orphan", 529 gp->name, gp->class->name)); 530 531 cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO); 532 cp->geom = gp; 533 cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED, 534 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 535 LIST_INSERT_HEAD(&gp->consumer, cp, consumer); 536 return(cp); 537 } 538 539 void 540 g_destroy_consumer(struct g_consumer *cp) 541 { 542 struct g_geom *gp; 543 544 g_topology_assert(); 545 G_VALID_CONSUMER(cp); 546 g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp); 547 KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached")); 548 KASSERT (cp->acr == 0, ("g_destroy_consumer with acr")); 549 KASSERT (cp->acw == 0, ("g_destroy_consumer with acw")); 550 KASSERT (cp->ace == 0, ("g_destroy_consumer with ace")); 551 g_cancel_event(cp); 552 gp = cp->geom; 553 LIST_REMOVE(cp, consumer); 554 devstat_remove_entry(cp->stat); 555 g_free(cp); 556 if (gp->flags & G_GEOM_WITHER) 557 g_do_wither(); 558 } 559 560 static void 561 g_new_provider_event(void *arg, int flag) 562 { 563 struct g_class *mp; 564 struct g_provider *pp; 565 struct g_consumer *cp, *next_cp; 566 567 g_topology_assert(); 568 if (flag == EV_CANCEL) 569 return; 570 if (g_shutdown) 571 return; 572 pp = arg; 573 G_VALID_PROVIDER(pp); 574 if ((pp->flags & G_PF_WITHER) != 0) 575 return; 576 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) { 577 if ((cp->flags & G_CF_ORPHAN) == 0 && 578 cp->geom->attrchanged != NULL) 579 cp->geom->attrchanged(cp, "GEOM::media"); 580 } 581 if (g_notaste) 582 return; 583 LIST_FOREACH(mp, &g_classes, class) { 584 if (mp->taste == NULL) 585 continue; 586 LIST_FOREACH(cp, &pp->consumers, consumers) 587 if (cp->geom->class == mp && 588 (cp->flags & G_CF_ORPHAN) == 0) 589 break; 590 if (cp != NULL) 591 continue; 592 mp->taste(mp, pp, 0); 593 g_topology_assert(); 594 } 595 } 596 597 struct g_provider * 598 g_new_providerf(struct g_geom *gp, const char *fmt, ...) 599 { 600 struct g_provider *pp; 601 struct sbuf *sb; 602 va_list ap; 603 604 g_topology_assert(); 605 G_VALID_GEOM(gp); 606 KASSERT(gp->access != NULL, 607 ("new provider on geom(%s) without ->access (class %s)", 608 gp->name, gp->class->name)); 609 KASSERT(gp->start != NULL, 610 ("new provider on geom(%s) without ->start (class %s)", 611 gp->name, gp->class->name)); 612 KASSERT(!(gp->flags & G_GEOM_WITHER), 613 ("new provider on WITHERing geom(%s) (class %s)", 614 gp->name, gp->class->name)); 615 sb = sbuf_new_auto(); 616 va_start(ap, fmt); 617 sbuf_vprintf(sb, fmt, ap); 618 va_end(ap); 619 sbuf_finish(sb); 620 pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO); 621 pp->name = (char *)(pp + 1); 622 strcpy(pp->name, sbuf_data(sb)); 623 sbuf_delete(sb); 624 LIST_INIT(&pp->consumers); 625 LIST_INIT(&pp->aliases); 626 pp->error = ENXIO; 627 pp->geom = gp; 628 pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED, 629 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 630 LIST_INSERT_HEAD(&gp->provider, pp, provider); 631 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL); 632 return (pp); 633 } 634 635 void 636 g_provider_add_alias(struct g_provider *pp, const char *fmt, ...) 637 { 638 struct sbuf *sb; 639 struct g_geom_alias *gap; 640 va_list ap; 641 642 /* 643 * Generate the alias string and save it in the list. 644 */ 645 sb = sbuf_new_auto(); 646 va_start(ap, fmt); 647 sbuf_vprintf(sb, fmt, ap); 648 va_end(ap); 649 sbuf_finish(sb); 650 651 LIST_FOREACH(gap, &pp->aliases, ga_next) { 652 if (strcmp(gap->ga_alias, sbuf_data(sb)) != 0) 653 continue; 654 /* Don't re-add the same alias. */ 655 sbuf_delete(sb); 656 return; 657 } 658 659 gap = g_malloc(sizeof(*gap) + sbuf_len(sb) + 1, M_WAITOK | M_ZERO); 660 memcpy((char *)(gap + 1), sbuf_data(sb), sbuf_len(sb)); 661 sbuf_delete(sb); 662 gap->ga_alias = (const char *)(gap + 1); 663 LIST_INSERT_HEAD(&pp->aliases, gap, ga_next); 664 } 665 666 void 667 g_error_provider(struct g_provider *pp, int error) 668 { 669 670 /* G_VALID_PROVIDER(pp); We may not have g_topology */ 671 pp->error = error; 672 } 673 674 static void 675 g_resize_provider_event(void *arg, int flag) 676 { 677 struct g_hh00 *hh; 678 struct g_class *mp; 679 struct g_geom *gp; 680 struct g_provider *pp; 681 struct g_consumer *cp, *cp2; 682 off_t size; 683 684 g_topology_assert(); 685 if (g_shutdown) 686 return; 687 688 hh = arg; 689 pp = hh->pp; 690 size = hh->size; 691 g_free(hh); 692 693 G_VALID_PROVIDER(pp); 694 KASSERT(!(pp->flags & G_PF_WITHER), 695 ("g_resize_provider_event but withered")); 696 g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp); 697 698 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) { 699 gp = cp->geom; 700 if (gp->resize == NULL && size < pp->mediasize) { 701 /* 702 * XXX: g_dev_orphan method does deferred destroying 703 * and it is possible, that other event could already 704 * call the orphan method. Check consumer's flags to 705 * do not schedule it twice. 706 */ 707 if (cp->flags & G_CF_ORPHAN) 708 continue; 709 cp->flags |= G_CF_ORPHAN; 710 cp->geom->orphan(cp); 711 } 712 } 713 714 pp->mediasize = size; 715 716 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) { 717 gp = cp->geom; 718 if ((gp->flags & G_GEOM_WITHER) == 0 && gp->resize != NULL) 719 gp->resize(cp); 720 } 721 722 /* 723 * After resizing, the previously invalid GEOM class metadata 724 * might become valid. This means we should retaste. 725 */ 726 LIST_FOREACH(mp, &g_classes, class) { 727 if (mp->taste == NULL) 728 continue; 729 LIST_FOREACH(cp, &pp->consumers, consumers) 730 if (cp->geom->class == mp && 731 (cp->flags & G_CF_ORPHAN) == 0) 732 break; 733 if (cp != NULL) 734 continue; 735 mp->taste(mp, pp, 0); 736 g_topology_assert(); 737 } 738 } 739 740 void 741 g_resize_provider(struct g_provider *pp, off_t size) 742 { 743 struct g_hh00 *hh; 744 745 G_VALID_PROVIDER(pp); 746 if (pp->flags & G_PF_WITHER) 747 return; 748 749 if (size == pp->mediasize) 750 return; 751 752 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO); 753 hh->pp = pp; 754 hh->size = size; 755 g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL); 756 } 757 758 struct g_provider * 759 g_provider_by_name(char const *arg) 760 { 761 struct g_class *cp; 762 struct g_geom *gp; 763 struct g_provider *pp, *wpp; 764 765 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 766 arg += sizeof(_PATH_DEV) - 1; 767 768 wpp = NULL; 769 LIST_FOREACH(cp, &g_classes, class) { 770 LIST_FOREACH(gp, &cp->geom, geom) { 771 LIST_FOREACH(pp, &gp->provider, provider) { 772 if (strcmp(arg, pp->name) != 0) 773 continue; 774 if ((gp->flags & G_GEOM_WITHER) == 0 && 775 (pp->flags & G_PF_WITHER) == 0) 776 return (pp); 777 else 778 wpp = pp; 779 } 780 } 781 } 782 783 return (wpp); 784 } 785 786 void 787 g_destroy_provider(struct g_provider *pp) 788 { 789 struct g_geom *gp; 790 struct g_geom_alias *gap, *gaptmp; 791 792 g_topology_assert(); 793 G_VALID_PROVIDER(pp); 794 KASSERT(LIST_EMPTY(&pp->consumers), 795 ("g_destroy_provider but attached")); 796 KASSERT (pp->acr == 0, ("g_destroy_provider with acr")); 797 KASSERT (pp->acw == 0, ("g_destroy_provider with acw")); 798 KASSERT (pp->ace == 0, ("g_destroy_provider with ace")); 799 g_cancel_event(pp); 800 LIST_REMOVE(pp, provider); 801 gp = pp->geom; 802 devstat_remove_entry(pp->stat); 803 /* 804 * If a callback was provided, send notification that the provider 805 * is now gone. 806 */ 807 if (gp->providergone != NULL) 808 gp->providergone(pp); 809 LIST_FOREACH_SAFE(gap, &pp->aliases, ga_next, gaptmp) 810 g_free(gap); 811 g_free(pp); 812 if ((gp->flags & G_GEOM_WITHER)) 813 g_do_wither(); 814 } 815 816 /* 817 * We keep the "geoms" list sorted by topological order (== increasing 818 * numerical rank) at all times. 819 * When an attach is done, the attaching geoms rank is invalidated 820 * and it is moved to the tail of the list. 821 * All geoms later in the sequence has their ranks reevaluated in 822 * sequence. If we cannot assign rank to a geom because it's 823 * prerequisites do not have rank, we move that element to the tail 824 * of the sequence with invalid rank as well. 825 * At some point we encounter our original geom and if we stil fail 826 * to assign it a rank, there must be a loop and we fail back to 827 * g_attach() which detach again and calls redo_rank again 828 * to fix up the damage. 829 * It would be much simpler code wise to do it recursively, but we 830 * can't risk that on the kernel stack. 831 */ 832 833 static int 834 redo_rank(struct g_geom *gp) 835 { 836 struct g_consumer *cp; 837 struct g_geom *gp1, *gp2; 838 int n, m; 839 840 g_topology_assert(); 841 G_VALID_GEOM(gp); 842 843 /* Invalidate this geoms rank and move it to the tail */ 844 gp1 = TAILQ_NEXT(gp, geoms); 845 if (gp1 != NULL) { 846 gp->rank = 0; 847 TAILQ_REMOVE(&geoms, gp, geoms); 848 TAILQ_INSERT_TAIL(&geoms, gp, geoms); 849 } else { 850 gp1 = gp; 851 } 852 853 /* re-rank the rest of the sequence */ 854 for (; gp1 != NULL; gp1 = gp2) { 855 gp1->rank = 0; 856 m = 1; 857 LIST_FOREACH(cp, &gp1->consumer, consumer) { 858 if (cp->provider == NULL) 859 continue; 860 n = cp->provider->geom->rank; 861 if (n == 0) { 862 m = 0; 863 break; 864 } else if (n >= m) 865 m = n + 1; 866 } 867 gp1->rank = m; 868 gp2 = TAILQ_NEXT(gp1, geoms); 869 870 /* got a rank, moving on */ 871 if (m != 0) 872 continue; 873 874 /* no rank to original geom means loop */ 875 if (gp == gp1) 876 return (ELOOP); 877 878 /* no rank, put it at the end move on */ 879 TAILQ_REMOVE(&geoms, gp1, geoms); 880 TAILQ_INSERT_TAIL(&geoms, gp1, geoms); 881 } 882 return (0); 883 } 884 885 int 886 g_attach(struct g_consumer *cp, struct g_provider *pp) 887 { 888 int error; 889 890 g_topology_assert(); 891 G_VALID_CONSUMER(cp); 892 G_VALID_PROVIDER(pp); 893 g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp); 894 KASSERT(cp->provider == NULL, ("attach but attached")); 895 if ((pp->flags & (G_PF_ORPHAN | G_PF_WITHER)) != 0) 896 return (ENXIO); 897 cp->provider = pp; 898 cp->flags &= ~G_CF_ORPHAN; 899 LIST_INSERT_HEAD(&pp->consumers, cp, consumers); 900 error = redo_rank(cp->geom); 901 if (error) { 902 LIST_REMOVE(cp, consumers); 903 cp->provider = NULL; 904 redo_rank(cp->geom); 905 } 906 return (error); 907 } 908 909 void 910 g_detach(struct g_consumer *cp) 911 { 912 struct g_provider *pp; 913 914 g_topology_assert(); 915 G_VALID_CONSUMER(cp); 916 g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp); 917 KASSERT(cp->provider != NULL, ("detach but not attached")); 918 KASSERT(cp->acr == 0, ("detach but nonzero acr")); 919 KASSERT(cp->acw == 0, ("detach but nonzero acw")); 920 KASSERT(cp->ace == 0, ("detach but nonzero ace")); 921 KASSERT(cp->nstart == cp->nend, 922 ("detach with active requests")); 923 pp = cp->provider; 924 LIST_REMOVE(cp, consumers); 925 cp->provider = NULL; 926 if ((cp->geom->flags & G_GEOM_WITHER) || 927 (pp->geom->flags & G_GEOM_WITHER) || 928 (pp->flags & G_PF_WITHER)) 929 g_do_wither(); 930 redo_rank(cp->geom); 931 } 932 933 /* 934 * g_access() 935 * 936 * Access-check with delta values. The question asked is "can provider 937 * "cp" change the access counters by the relative amounts dc[rwe] ?" 938 */ 939 940 int 941 g_access(struct g_consumer *cp, int dcr, int dcw, int dce) 942 { 943 struct g_provider *pp; 944 struct g_geom *gp; 945 int pw, pe; 946 #ifdef INVARIANTS 947 int sr, sw, se; 948 #endif 949 int error; 950 951 g_topology_assert(); 952 G_VALID_CONSUMER(cp); 953 pp = cp->provider; 954 KASSERT(pp != NULL, ("access but not attached")); 955 G_VALID_PROVIDER(pp); 956 gp = pp->geom; 957 958 g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)", 959 cp, pp->name, dcr, dcw, dce); 960 961 KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr")); 962 KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw")); 963 KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace")); 964 KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request")); 965 KASSERT(cp->acr + dcr != 0 || cp->acw + dcw != 0 || 966 cp->ace + dce != 0 || cp->nstart == cp->nend, 967 ("Last close with active requests")); 968 KASSERT(gp->access != NULL, ("NULL geom->access")); 969 970 /* 971 * If our class cares about being spoiled, and we have been, we 972 * are probably just ahead of the event telling us that. Fail 973 * now rather than having to unravel this later. 974 */ 975 if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) && 976 (dcr > 0 || dcw > 0 || dce > 0)) 977 return (ENXIO); 978 979 /* 980 * A number of GEOM classes either need to perform an I/O on the first 981 * open or to acquire a different subsystem's lock. To do that they 982 * may have to drop the topology lock. 983 * Other GEOM classes perform special actions when opening a lower rank 984 * geom for the first time. As a result, more than one thread may 985 * end up performing the special actions. 986 * So, we prevent concurrent "first" opens by marking the consumer with 987 * special flag. 988 * 989 * Note that if the geom's access method never drops the topology lock, 990 * then we will never see G_GEOM_IN_ACCESS here. 991 */ 992 while ((gp->flags & G_GEOM_IN_ACCESS) != 0) { 993 g_trace(G_T_ACCESS, 994 "%s: race on geom %s via provider %s and consumer of %s", 995 __func__, gp->name, pp->name, cp->geom->name); 996 gp->flags |= G_GEOM_ACCESS_WAIT; 997 g_topology_sleep(gp, 0); 998 } 999 1000 /* 1001 * Figure out what counts the provider would have had, if this 1002 * consumer had (r0w0e0) at this time. 1003 */ 1004 pw = pp->acw - cp->acw; 1005 pe = pp->ace - cp->ace; 1006 1007 g_trace(G_T_ACCESS, 1008 "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)", 1009 dcr, dcw, dce, 1010 cp->acr, cp->acw, cp->ace, 1011 pp->acr, pp->acw, pp->ace, 1012 pp, pp->name); 1013 1014 /* If foot-shooting is enabled, any open on rank#1 is OK */ 1015 if ((g_debugflags & G_F_FOOTSHOOTING) && gp->rank == 1) 1016 ; 1017 /* If we try exclusive but already write: fail */ 1018 else if (dce > 0 && pw > 0) 1019 return (EPERM); 1020 /* If we try write but already exclusive: fail */ 1021 else if (dcw > 0 && pe > 0) 1022 return (EPERM); 1023 /* If we try to open more but provider is error'ed: fail */ 1024 else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0) { 1025 printf("%s(%d): provider %s has error %d set\n", 1026 __func__, __LINE__, pp->name, pp->error); 1027 return (pp->error); 1028 } 1029 1030 /* Ok then... */ 1031 1032 #ifdef INVARIANTS 1033 sr = cp->acr; 1034 sw = cp->acw; 1035 se = cp->ace; 1036 #endif 1037 gp->flags |= G_GEOM_IN_ACCESS; 1038 error = gp->access(pp, dcr, dcw, dce); 1039 KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0, 1040 ("Geom provider %s::%s dcr=%d dcw=%d dce=%d error=%d failed " 1041 "closing ->access()", gp->class->name, pp->name, dcr, dcw, 1042 dce, error)); 1043 1044 g_topology_assert(); 1045 gp->flags &= ~G_GEOM_IN_ACCESS; 1046 KASSERT(cp->acr == sr && cp->acw == sw && cp->ace == se, 1047 ("Access counts changed during geom->access")); 1048 if ((gp->flags & G_GEOM_ACCESS_WAIT) != 0) { 1049 gp->flags &= ~G_GEOM_ACCESS_WAIT; 1050 wakeup(gp); 1051 } 1052 1053 if (!error) { 1054 /* 1055 * If we open first write, spoil any partner consumers. 1056 * If we close last write and provider is not errored, 1057 * trigger re-taste. 1058 */ 1059 if (pp->acw == 0 && dcw != 0) 1060 g_spoil(pp, cp); 1061 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 && 1062 !(gp->flags & G_GEOM_WITHER)) 1063 g_post_event(g_new_provider_event, pp, M_WAITOK, 1064 pp, NULL); 1065 1066 pp->acr += dcr; 1067 pp->acw += dcw; 1068 pp->ace += dce; 1069 cp->acr += dcr; 1070 cp->acw += dcw; 1071 cp->ace += dce; 1072 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) 1073 KASSERT(pp->sectorsize > 0, 1074 ("Provider %s lacks sectorsize", pp->name)); 1075 if ((cp->geom->flags & G_GEOM_WITHER) && 1076 cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 1077 g_do_wither(); 1078 } 1079 return (error); 1080 } 1081 1082 int 1083 g_handleattr_int(struct bio *bp, const char *attribute, int val) 1084 { 1085 1086 return (g_handleattr(bp, attribute, &val, sizeof val)); 1087 } 1088 1089 int 1090 g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val) 1091 { 1092 1093 return (g_handleattr(bp, attribute, &val, sizeof val)); 1094 } 1095 1096 int 1097 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val) 1098 { 1099 1100 return (g_handleattr(bp, attribute, &val, sizeof val)); 1101 } 1102 1103 int 1104 g_handleattr_str(struct bio *bp, const char *attribute, const char *str) 1105 { 1106 1107 return (g_handleattr(bp, attribute, str, 0)); 1108 } 1109 1110 int 1111 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len) 1112 { 1113 int error = 0; 1114 1115 if (strcmp(bp->bio_attribute, attribute)) 1116 return (0); 1117 if (len == 0) { 1118 bzero(bp->bio_data, bp->bio_length); 1119 if (strlcpy(bp->bio_data, val, bp->bio_length) >= 1120 bp->bio_length) { 1121 printf("%s: %s %s bio_length %jd strlen %zu -> EFAULT\n", 1122 __func__, bp->bio_to->name, attribute, 1123 (intmax_t)bp->bio_length, strlen(val)); 1124 error = EFAULT; 1125 } 1126 } else if (bp->bio_length == len) { 1127 bcopy(val, bp->bio_data, len); 1128 } else { 1129 printf("%s: %s %s bio_length %jd len %d -> EFAULT\n", __func__, 1130 bp->bio_to->name, attribute, (intmax_t)bp->bio_length, len); 1131 error = EFAULT; 1132 } 1133 if (error == 0) 1134 bp->bio_completed = bp->bio_length; 1135 g_io_deliver(bp, error); 1136 return (1); 1137 } 1138 1139 int 1140 g_std_access(struct g_provider *pp, 1141 int dr __unused, int dw __unused, int de __unused) 1142 { 1143 1144 g_topology_assert(); 1145 G_VALID_PROVIDER(pp); 1146 return (0); 1147 } 1148 1149 void 1150 g_std_done(struct bio *bp) 1151 { 1152 struct bio *bp2; 1153 1154 bp2 = bp->bio_parent; 1155 if (bp2->bio_error == 0) 1156 bp2->bio_error = bp->bio_error; 1157 bp2->bio_completed += bp->bio_completed; 1158 g_destroy_bio(bp); 1159 bp2->bio_inbed++; 1160 if (bp2->bio_children == bp2->bio_inbed) { 1161 if (bp2->bio_cmd == BIO_SPEEDUP) 1162 bp2->bio_completed = bp2->bio_length; 1163 g_io_deliver(bp2, bp2->bio_error); 1164 } 1165 } 1166 1167 /* XXX: maybe this is only g_slice_spoiled */ 1168 1169 void 1170 g_std_spoiled(struct g_consumer *cp) 1171 { 1172 struct g_geom *gp; 1173 struct g_provider *pp; 1174 1175 g_topology_assert(); 1176 G_VALID_CONSUMER(cp); 1177 g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp); 1178 cp->flags |= G_CF_ORPHAN; 1179 g_detach(cp); 1180 gp = cp->geom; 1181 LIST_FOREACH(pp, &gp->provider, provider) 1182 g_orphan_provider(pp, ENXIO); 1183 g_destroy_consumer(cp); 1184 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer)) 1185 g_destroy_geom(gp); 1186 else 1187 gp->flags |= G_GEOM_WITHER; 1188 } 1189 1190 /* 1191 * Spoiling happens when a provider is opened for writing, but consumers 1192 * which are configured by in-band data are attached (slicers for instance). 1193 * Since the write might potentially change the in-band data, such consumers 1194 * need to re-evaluate their existence after the writing session closes. 1195 * We do this by (offering to) tear them down when the open for write happens 1196 * in return for a re-taste when it closes again. 1197 * Together with the fact that such consumers grab an 'e' bit whenever they 1198 * are open, regardless of mode, this ends up DTRT. 1199 */ 1200 1201 static void 1202 g_spoil_event(void *arg, int flag) 1203 { 1204 struct g_provider *pp; 1205 struct g_consumer *cp, *cp2; 1206 1207 g_topology_assert(); 1208 if (flag == EV_CANCEL) 1209 return; 1210 pp = arg; 1211 G_VALID_PROVIDER(pp); 1212 g_trace(G_T_TOPOLOGY, "%s %p(%s:%s:%s)", __func__, pp, 1213 pp->geom->class->name, pp->geom->name, pp->name); 1214 for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) { 1215 cp2 = LIST_NEXT(cp, consumers); 1216 if ((cp->flags & G_CF_SPOILED) == 0) 1217 continue; 1218 cp->flags &= ~G_CF_SPOILED; 1219 if (cp->geom->spoiled == NULL) 1220 continue; 1221 cp->geom->spoiled(cp); 1222 g_topology_assert(); 1223 } 1224 } 1225 1226 void 1227 g_spoil(struct g_provider *pp, struct g_consumer *cp) 1228 { 1229 struct g_consumer *cp2; 1230 1231 g_topology_assert(); 1232 G_VALID_PROVIDER(pp); 1233 G_VALID_CONSUMER(cp); 1234 1235 LIST_FOREACH(cp2, &pp->consumers, consumers) { 1236 if (cp2 == cp) 1237 continue; 1238 /* 1239 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr)); 1240 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw)); 1241 */ 1242 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace)); 1243 cp2->flags |= G_CF_SPOILED; 1244 } 1245 g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL); 1246 } 1247 1248 static void 1249 g_media_changed_event(void *arg, int flag) 1250 { 1251 struct g_provider *pp; 1252 int retaste; 1253 1254 g_topology_assert(); 1255 if (flag == EV_CANCEL) 1256 return; 1257 pp = arg; 1258 G_VALID_PROVIDER(pp); 1259 1260 /* 1261 * If provider was not open for writing, queue retaste after spoiling. 1262 * If it was, retaste will happen automatically on close. 1263 */ 1264 retaste = (pp->acw == 0 && pp->error == 0 && 1265 !(pp->geom->flags & G_GEOM_WITHER)); 1266 g_spoil_event(arg, flag); 1267 if (retaste) 1268 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL); 1269 } 1270 1271 int 1272 g_media_changed(struct g_provider *pp, int flag) 1273 { 1274 struct g_consumer *cp; 1275 1276 LIST_FOREACH(cp, &pp->consumers, consumers) 1277 cp->flags |= G_CF_SPOILED; 1278 return (g_post_event(g_media_changed_event, pp, flag, pp, NULL)); 1279 } 1280 1281 int 1282 g_media_gone(struct g_provider *pp, int flag) 1283 { 1284 struct g_consumer *cp; 1285 1286 LIST_FOREACH(cp, &pp->consumers, consumers) 1287 cp->flags |= G_CF_SPOILED; 1288 return (g_post_event(g_spoil_event, pp, flag, pp, NULL)); 1289 } 1290 1291 int 1292 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len) 1293 { 1294 int error, i; 1295 1296 i = len; 1297 error = g_io_getattr(attr, cp, &i, var); 1298 if (error) 1299 return (error); 1300 if (i != len) 1301 return (EINVAL); 1302 return (0); 1303 } 1304 1305 static int 1306 g_get_device_prefix_len(const char *name) 1307 { 1308 int len; 1309 1310 if (strncmp(name, "ada", 3) == 0) 1311 len = 3; 1312 else if (strncmp(name, "ad", 2) == 0) 1313 len = 2; 1314 else 1315 return (0); 1316 if (name[len] < '0' || name[len] > '9') 1317 return (0); 1318 do { 1319 len++; 1320 } while (name[len] >= '0' && name[len] <= '9'); 1321 return (len); 1322 } 1323 1324 int 1325 g_compare_names(const char *namea, const char *nameb) 1326 { 1327 int deva, devb; 1328 1329 if (strcmp(namea, nameb) == 0) 1330 return (1); 1331 deva = g_get_device_prefix_len(namea); 1332 if (deva == 0) 1333 return (0); 1334 devb = g_get_device_prefix_len(nameb); 1335 if (devb == 0) 1336 return (0); 1337 if (strcmp(namea + deva, nameb + devb) == 0) 1338 return (1); 1339 return (0); 1340 } 1341 1342 #if defined(DIAGNOSTIC) || defined(DDB) 1343 /* 1344 * This function walks the mesh and returns a non-zero integer if it 1345 * finds the argument pointer is an object. The return value indicates 1346 * which type of object it is believed to be. If topology is not locked, 1347 * this function is potentially dangerous, but we don't assert that the 1348 * topology lock is held when called from debugger. 1349 */ 1350 int 1351 g_valid_obj(void const *ptr) 1352 { 1353 struct g_class *mp; 1354 struct g_geom *gp; 1355 struct g_consumer *cp; 1356 struct g_provider *pp; 1357 1358 #ifdef KDB 1359 if (kdb_active == 0) 1360 #endif 1361 g_topology_assert(); 1362 1363 LIST_FOREACH(mp, &g_classes, class) { 1364 if (ptr == mp) 1365 return (1); 1366 LIST_FOREACH(gp, &mp->geom, geom) { 1367 if (ptr == gp) 1368 return (2); 1369 LIST_FOREACH(cp, &gp->consumer, consumer) 1370 if (ptr == cp) 1371 return (3); 1372 LIST_FOREACH(pp, &gp->provider, provider) 1373 if (ptr == pp) 1374 return (4); 1375 } 1376 } 1377 return(0); 1378 } 1379 #endif 1380 1381 #ifdef DDB 1382 1383 #define gprintf(...) do { \ 1384 db_printf("%*s", indent, ""); \ 1385 db_printf(__VA_ARGS__); \ 1386 } while (0) 1387 #define gprintln(...) do { \ 1388 gprintf(__VA_ARGS__); \ 1389 db_printf("\n"); \ 1390 } while (0) 1391 1392 #define ADDFLAG(obj, flag, sflag) do { \ 1393 if ((obj)->flags & (flag)) { \ 1394 if (comma) \ 1395 strlcat(str, ",", size); \ 1396 strlcat(str, (sflag), size); \ 1397 comma = 1; \ 1398 } \ 1399 } while (0) 1400 1401 static char * 1402 provider_flags_to_string(struct g_provider *pp, char *str, size_t size) 1403 { 1404 int comma = 0; 1405 1406 bzero(str, size); 1407 if (pp->flags == 0) { 1408 strlcpy(str, "NONE", size); 1409 return (str); 1410 } 1411 ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER"); 1412 ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN"); 1413 return (str); 1414 } 1415 1416 static char * 1417 geom_flags_to_string(struct g_geom *gp, char *str, size_t size) 1418 { 1419 int comma = 0; 1420 1421 bzero(str, size); 1422 if (gp->flags == 0) { 1423 strlcpy(str, "NONE", size); 1424 return (str); 1425 } 1426 ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER"); 1427 return (str); 1428 } 1429 static void 1430 db_show_geom_consumer(int indent, struct g_consumer *cp) 1431 { 1432 1433 if (indent == 0) { 1434 gprintln("consumer: %p", cp); 1435 gprintln(" class: %s (%p)", cp->geom->class->name, 1436 cp->geom->class); 1437 gprintln(" geom: %s (%p)", cp->geom->name, cp->geom); 1438 if (cp->provider == NULL) 1439 gprintln(" provider: none"); 1440 else { 1441 gprintln(" provider: %s (%p)", cp->provider->name, 1442 cp->provider); 1443 } 1444 gprintln(" access: r%dw%de%d", cp->acr, cp->acw, cp->ace); 1445 gprintln(" flags: 0x%04x", cp->flags); 1446 #ifdef INVARIANTS 1447 gprintln(" nstart: %u", cp->nstart); 1448 gprintln(" nend: %u", cp->nend); 1449 #endif 1450 } else { 1451 gprintf("consumer: %p (%s), access=r%dw%de%d", cp, 1452 cp->provider != NULL ? cp->provider->name : "none", 1453 cp->acr, cp->acw, cp->ace); 1454 if (cp->flags) 1455 db_printf(", flags=0x%04x", cp->flags); 1456 db_printf("\n"); 1457 } 1458 } 1459 1460 static void 1461 db_show_geom_provider(int indent, struct g_provider *pp) 1462 { 1463 struct g_consumer *cp; 1464 char flags[64]; 1465 1466 if (indent == 0) { 1467 gprintln("provider: %s (%p)", pp->name, pp); 1468 gprintln(" class: %s (%p)", pp->geom->class->name, 1469 pp->geom->class); 1470 gprintln(" geom: %s (%p)", pp->geom->name, pp->geom); 1471 gprintln(" mediasize: %jd", (intmax_t)pp->mediasize); 1472 gprintln(" sectorsize: %u", pp->sectorsize); 1473 gprintln(" stripesize: %ju", (uintmax_t)pp->stripesize); 1474 gprintln(" stripeoffset: %ju", (uintmax_t)pp->stripeoffset); 1475 gprintln(" access: r%dw%de%d", pp->acr, pp->acw, 1476 pp->ace); 1477 gprintln(" flags: %s (0x%04x)", 1478 provider_flags_to_string(pp, flags, sizeof(flags)), 1479 pp->flags); 1480 gprintln(" error: %d", pp->error); 1481 if (LIST_EMPTY(&pp->consumers)) 1482 gprintln(" consumers: none"); 1483 } else { 1484 gprintf("provider: %s (%p), access=r%dw%de%d", 1485 pp->name, pp, pp->acr, pp->acw, pp->ace); 1486 if (pp->flags != 0) { 1487 db_printf(", flags=%s (0x%04x)", 1488 provider_flags_to_string(pp, flags, sizeof(flags)), 1489 pp->flags); 1490 } 1491 db_printf("\n"); 1492 } 1493 if (!LIST_EMPTY(&pp->consumers)) { 1494 LIST_FOREACH(cp, &pp->consumers, consumers) { 1495 db_show_geom_consumer(indent + 2, cp); 1496 if (db_pager_quit) 1497 break; 1498 } 1499 } 1500 } 1501 1502 static void 1503 db_show_geom_geom(int indent, struct g_geom *gp) 1504 { 1505 struct g_provider *pp; 1506 struct g_consumer *cp; 1507 char flags[64]; 1508 1509 if (indent == 0) { 1510 gprintln("geom: %s (%p)", gp->name, gp); 1511 gprintln(" class: %s (%p)", gp->class->name, gp->class); 1512 gprintln(" flags: %s (0x%04x)", 1513 geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags); 1514 gprintln(" rank: %d", gp->rank); 1515 if (LIST_EMPTY(&gp->provider)) 1516 gprintln(" providers: none"); 1517 if (LIST_EMPTY(&gp->consumer)) 1518 gprintln(" consumers: none"); 1519 } else { 1520 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank); 1521 if (gp->flags != 0) { 1522 db_printf(", flags=%s (0x%04x)", 1523 geom_flags_to_string(gp, flags, sizeof(flags)), 1524 gp->flags); 1525 } 1526 db_printf("\n"); 1527 } 1528 if (!LIST_EMPTY(&gp->provider)) { 1529 LIST_FOREACH(pp, &gp->provider, provider) { 1530 db_show_geom_provider(indent + 2, pp); 1531 if (db_pager_quit) 1532 break; 1533 } 1534 } 1535 if (!LIST_EMPTY(&gp->consumer)) { 1536 LIST_FOREACH(cp, &gp->consumer, consumer) { 1537 db_show_geom_consumer(indent + 2, cp); 1538 if (db_pager_quit) 1539 break; 1540 } 1541 } 1542 } 1543 1544 static void 1545 db_show_geom_class(struct g_class *mp) 1546 { 1547 struct g_geom *gp; 1548 1549 db_printf("class: %s (%p)\n", mp->name, mp); 1550 LIST_FOREACH(gp, &mp->geom, geom) { 1551 db_show_geom_geom(2, gp); 1552 if (db_pager_quit) 1553 break; 1554 } 1555 } 1556 1557 /* 1558 * Print the GEOM topology or the given object. 1559 */ 1560 DB_SHOW_COMMAND(geom, db_show_geom) 1561 { 1562 struct g_class *mp; 1563 1564 if (!have_addr) { 1565 /* No address given, print the entire topology. */ 1566 LIST_FOREACH(mp, &g_classes, class) { 1567 db_show_geom_class(mp); 1568 db_printf("\n"); 1569 if (db_pager_quit) 1570 break; 1571 } 1572 } else { 1573 switch (g_valid_obj((void *)addr)) { 1574 case 1: 1575 db_show_geom_class((struct g_class *)addr); 1576 break; 1577 case 2: 1578 db_show_geom_geom(0, (struct g_geom *)addr); 1579 break; 1580 case 3: 1581 db_show_geom_consumer(0, (struct g_consumer *)addr); 1582 break; 1583 case 4: 1584 db_show_geom_provider(0, (struct g_provider *)addr); 1585 break; 1586 default: 1587 db_printf("Not a GEOM object.\n"); 1588 break; 1589 } 1590 } 1591 } 1592 1593 static void 1594 db_print_bio_cmd(struct bio *bp) 1595 { 1596 db_printf(" cmd: "); 1597 switch (bp->bio_cmd) { 1598 case BIO_READ: db_printf("BIO_READ"); break; 1599 case BIO_WRITE: db_printf("BIO_WRITE"); break; 1600 case BIO_DELETE: db_printf("BIO_DELETE"); break; 1601 case BIO_GETATTR: db_printf("BIO_GETATTR"); break; 1602 case BIO_FLUSH: db_printf("BIO_FLUSH"); break; 1603 case BIO_CMD0: db_printf("BIO_CMD0"); break; 1604 case BIO_CMD1: db_printf("BIO_CMD1"); break; 1605 case BIO_CMD2: db_printf("BIO_CMD2"); break; 1606 case BIO_ZONE: db_printf("BIO_ZONE"); break; 1607 default: db_printf("UNKNOWN"); break; 1608 } 1609 db_printf("\n"); 1610 } 1611 1612 static void 1613 db_print_bio_flags(struct bio *bp) 1614 { 1615 int comma; 1616 1617 comma = 0; 1618 db_printf(" flags: "); 1619 if (bp->bio_flags & BIO_ERROR) { 1620 db_printf("BIO_ERROR"); 1621 comma = 1; 1622 } 1623 if (bp->bio_flags & BIO_DONE) { 1624 db_printf("%sBIO_DONE", (comma ? ", " : "")); 1625 comma = 1; 1626 } 1627 if (bp->bio_flags & BIO_ONQUEUE) 1628 db_printf("%sBIO_ONQUEUE", (comma ? ", " : "")); 1629 db_printf("\n"); 1630 } 1631 1632 /* 1633 * Print useful information in a BIO 1634 */ 1635 DB_SHOW_COMMAND(bio, db_show_bio) 1636 { 1637 struct bio *bp; 1638 1639 if (have_addr) { 1640 bp = (struct bio *)addr; 1641 db_printf("BIO %p\n", bp); 1642 db_print_bio_cmd(bp); 1643 db_print_bio_flags(bp); 1644 db_printf(" cflags: 0x%hx\n", bp->bio_cflags); 1645 db_printf(" pflags: 0x%hx\n", bp->bio_pflags); 1646 db_printf(" offset: %jd\n", (intmax_t)bp->bio_offset); 1647 db_printf(" length: %jd\n", (intmax_t)bp->bio_length); 1648 db_printf(" bcount: %ld\n", bp->bio_bcount); 1649 db_printf(" resid: %ld\n", bp->bio_resid); 1650 db_printf(" completed: %jd\n", (intmax_t)bp->bio_completed); 1651 db_printf(" children: %u\n", bp->bio_children); 1652 db_printf(" inbed: %u\n", bp->bio_inbed); 1653 db_printf(" error: %d\n", bp->bio_error); 1654 db_printf(" parent: %p\n", bp->bio_parent); 1655 db_printf(" driver1: %p\n", bp->bio_driver1); 1656 db_printf(" driver2: %p\n", bp->bio_driver2); 1657 db_printf(" caller1: %p\n", bp->bio_caller1); 1658 db_printf(" caller2: %p\n", bp->bio_caller2); 1659 db_printf(" bio_from: %p\n", bp->bio_from); 1660 db_printf(" bio_to: %p\n", bp->bio_to); 1661 1662 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 1663 db_printf(" bio_track_bp: %p\n", bp->bio_track_bp); 1664 #endif 1665 } 1666 } 1667 1668 #undef gprintf 1669 #undef gprintln 1670 #undef ADDFLAG 1671 1672 #endif /* DDB */ 1673