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