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