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