geom_subr.c revision 162326
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 162326 2006-09-15 16:36:45Z pjd $"); 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 62struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes); 63static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms); 64char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim; 65 66struct g_hh00 { 67 struct g_class *mp; 68 int error; 69 int post; 70}; 71 72/* 73 * This event offers a new class a chance to taste all preexisting providers. 74 */ 75static void 76g_load_class(void *arg, int flag) 77{ 78 struct g_hh00 *hh; 79 struct g_class *mp2, *mp; 80 struct g_geom *gp; 81 struct g_provider *pp; 82 83 g_topology_assert(); 84 if (flag == EV_CANCEL) /* XXX: can't happen ? */ 85 return; 86 if (g_shutdown) 87 return; 88 89 hh = arg; 90 mp = hh->mp; 91 hh->error = 0; 92 if (hh->post) { 93 g_free(hh); 94 hh = NULL; 95 } 96 g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name); 97 KASSERT(mp->name != NULL && *mp->name != '\0', 98 ("GEOM class has no name")); 99 LIST_FOREACH(mp2, &g_classes, class) { 100 if (mp2 == mp) { 101 printf("The GEOM class %s is already loaded.\n", 102 mp2->name); 103 if (hh != NULL) 104 hh->error = EEXIST; 105 return; 106 } else if (strcmp(mp2->name, mp->name) == 0) { 107 printf("A GEOM class %s is already loaded.\n", 108 mp2->name); 109 if (hh != NULL) 110 hh->error = EEXIST; 111 return; 112 } 113 } 114 115 LIST_INIT(&mp->geom); 116 LIST_INSERT_HEAD(&g_classes, mp, class); 117 if (mp->init != NULL) 118 mp->init(mp); 119 if (mp->taste == NULL) 120 return; 121 LIST_FOREACH(mp2, &g_classes, class) { 122 if (mp == mp2) 123 continue; 124 LIST_FOREACH(gp, &mp2->geom, geom) { 125 LIST_FOREACH(pp, &gp->provider, provider) { 126 mp->taste(mp, pp, 0); 127 g_topology_assert(); 128 } 129 } 130 } 131} 132 133static void 134g_unload_class(void *arg, int flag) 135{ 136 struct g_hh00 *hh; 137 struct g_class *mp; 138 struct g_geom *gp; 139 struct g_provider *pp; 140 struct g_consumer *cp; 141 int error; 142 143 g_topology_assert(); 144 hh = arg; 145 mp = hh->mp; 146 G_VALID_CLASS(mp); 147 g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name); 148 149 /* 150 * We allow unloading if we have no geoms, or a class 151 * method we can use to get rid of them. 152 */ 153 if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) { 154 hh->error = EOPNOTSUPP; 155 return; 156 } 157 158 /* We refuse to unload if anything is open */ 159 LIST_FOREACH(gp, &mp->geom, geom) { 160 LIST_FOREACH(pp, &gp->provider, provider) 161 if (pp->acr || pp->acw || pp->ace) { 162 hh->error = EBUSY; 163 return; 164 } 165 LIST_FOREACH(cp, &gp->consumer, consumer) 166 if (cp->acr || cp->acw || cp->ace) { 167 hh->error = EBUSY; 168 return; 169 } 170 } 171 172 /* Bar new entries */ 173 mp->taste = NULL; 174 mp->config = NULL; 175 176 error = 0; 177 for (;;) { 178 gp = LIST_FIRST(&mp->geom); 179 if (gp == NULL) 180 break; 181 error = mp->destroy_geom(NULL, mp, gp); 182 if (error != 0) 183 break; 184 } 185 if (error == 0) { 186 if (mp->fini != NULL) 187 mp->fini(mp); 188 LIST_REMOVE(mp, class); 189 } 190 hh->error = error; 191 return; 192} 193 194int 195g_modevent(module_t mod, int type, void *data) 196{ 197 struct g_hh00 *hh; 198 int error; 199 static int g_ignition; 200 struct g_class *mp; 201 202 mp = data; 203 if (mp->version != G_VERSION) { 204 printf("GEOM class %s has Wrong version %x\n", 205 mp->name, mp->version); 206 return (EINVAL); 207 } 208 if (!g_ignition) { 209 g_ignition++; 210 g_init(); 211 } 212 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO); 213 hh->mp = data; 214 error = EOPNOTSUPP; 215 switch (type) { 216 case MOD_LOAD: 217 g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name); 218 /* 219 * Once the system is not cold, MOD_LOAD calls will be 220 * from the userland and the g_event thread will be able 221 * to acknowledge their completion. 222 */ 223 if (cold) { 224 hh->post = 1; 225 error = g_post_event(g_load_class, hh, M_WAITOK, NULL); 226 } else { 227 error = g_waitfor_event(g_load_class, hh, M_WAITOK, 228 NULL); 229 if (error == 0) 230 error = hh->error; 231 g_free(hh); 232 } 233 break; 234 case MOD_UNLOAD: 235 g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name); 236 error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL); 237 if (error == 0) 238 error = hh->error; 239 if (error == 0) { 240 KASSERT(LIST_EMPTY(&hh->mp->geom), 241 ("Unloaded class (%s) still has geom", hh->mp->name)); 242 } 243 g_free(hh); 244 break; 245 default: 246 g_free(hh); 247 break; 248 } 249 return (error); 250} 251 252struct g_geom * 253g_new_geomf(struct g_class *mp, const char *fmt, ...) 254{ 255 struct g_geom *gp; 256 va_list ap; 257 struct sbuf *sb; 258 259 g_topology_assert(); 260 G_VALID_CLASS(mp); 261 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 262 va_start(ap, fmt); 263 sbuf_vprintf(sb, fmt, ap); 264 va_end(ap); 265 sbuf_finish(sb); 266 gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO); 267 gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO); 268 gp->class = mp; 269 gp->rank = 1; 270 LIST_INIT(&gp->consumer); 271 LIST_INIT(&gp->provider); 272 LIST_INSERT_HEAD(&mp->geom, gp, geom); 273 TAILQ_INSERT_HEAD(&geoms, gp, geoms); 274 strcpy(gp->name, sbuf_data(sb)); 275 sbuf_delete(sb); 276 /* Fill in defaults from class */ 277 gp->start = mp->start; 278 gp->spoiled = mp->spoiled; 279 gp->dumpconf = mp->dumpconf; 280 gp->access = mp->access; 281 gp->orphan = mp->orphan; 282 gp->ioctl = mp->ioctl; 283 return (gp); 284} 285 286void 287g_destroy_geom(struct g_geom *gp) 288{ 289 290 g_topology_assert(); 291 G_VALID_GEOM(gp); 292 g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name); 293 KASSERT(LIST_EMPTY(&gp->consumer), 294 ("g_destroy_geom(%s) with consumer(s) [%p]", 295 gp->name, LIST_FIRST(&gp->consumer))); 296 KASSERT(LIST_EMPTY(&gp->provider), 297 ("g_destroy_geom(%s) with provider(s) [%p]", 298 gp->name, LIST_FIRST(&gp->provider))); 299 g_cancel_event(gp); 300 LIST_REMOVE(gp, geom); 301 TAILQ_REMOVE(&geoms, gp, geoms); 302 g_free(gp->name); 303 g_free(gp); 304} 305 306/* 307 * This function is called (repeatedly) until the has withered away. 308 */ 309void 310g_wither_geom(struct g_geom *gp, int error) 311{ 312 struct g_provider *pp; 313 314 g_topology_assert(); 315 G_VALID_GEOM(gp); 316 g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name); 317 if (!(gp->flags & G_GEOM_WITHER)) { 318 gp->flags |= G_GEOM_WITHER; 319 LIST_FOREACH(pp, &gp->provider, provider) 320 if (!(pp->flags & G_PF_ORPHAN)) 321 g_orphan_provider(pp, error); 322 } 323 g_do_wither(); 324} 325 326/* 327 * Convenience function to destroy a particular provider. 328 */ 329void 330g_wither_provider(struct g_provider *pp, int error) 331{ 332 333 pp->flags |= G_PF_WITHER; 334 if (!(pp->flags & G_PF_ORPHAN)) 335 g_orphan_provider(pp, error); 336} 337 338/* 339 * This function is called (repeatedly) until the has withered away. 340 */ 341void 342g_wither_geom_close(struct g_geom *gp, int error) 343{ 344 struct g_consumer *cp; 345 346 g_topology_assert(); 347 G_VALID_GEOM(gp); 348 g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name); 349 LIST_FOREACH(cp, &gp->consumer, consumer) 350 if (cp->acr || cp->acw || cp->ace) 351 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 352 g_wither_geom(gp, error); 353} 354 355/* 356 * This function is called (repeatedly) until we cant wash away more 357 * withered bits at present. Return value contains two bits. Bit 0 358 * set means "withering stuff we can't wash now", bit 1 means "call 359 * me again, there may be stuff I didn't get the first time around. 360 */ 361int 362g_wither_washer() 363{ 364 struct g_class *mp; 365 struct g_geom *gp, *gp2; 366 struct g_provider *pp, *pp2; 367 struct g_consumer *cp, *cp2; 368 int result; 369 370 result = 0; 371 g_topology_assert(); 372 LIST_FOREACH(mp, &g_classes, class) { 373 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 374 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) { 375 if (!(pp->flags & G_PF_WITHER)) 376 continue; 377 if (LIST_EMPTY(&pp->consumers)) 378 g_destroy_provider(pp); 379 else 380 result |= 1; 381 } 382 if (!(gp->flags & G_GEOM_WITHER)) 383 continue; 384 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) { 385 if (LIST_EMPTY(&pp->consumers)) 386 g_destroy_provider(pp); 387 else 388 result |= 1; 389 } 390 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) { 391 if (cp->acr || cp->acw || cp->ace) { 392 result |= 1; 393 continue; 394 } 395 if (cp->provider != NULL) 396 g_detach(cp); 397 g_destroy_consumer(cp); 398 result |= 2; 399 } 400 if (LIST_EMPTY(&gp->provider) && 401 LIST_EMPTY(&gp->consumer)) 402 g_destroy_geom(gp); 403 else 404 result |= 1; 405 } 406 } 407 return (result); 408} 409 410struct g_consumer * 411g_new_consumer(struct g_geom *gp) 412{ 413 struct g_consumer *cp; 414 415 g_topology_assert(); 416 G_VALID_GEOM(gp); 417 KASSERT(!(gp->flags & G_GEOM_WITHER), 418 ("g_new_consumer on WITHERing geom(%s) (class %s)", 419 gp->name, gp->class->name)); 420 KASSERT(gp->orphan != NULL, 421 ("g_new_consumer on geom(%s) (class %s) without orphan", 422 gp->name, gp->class->name)); 423 424 cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO); 425 cp->geom = gp; 426 cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED, 427 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 428 LIST_INSERT_HEAD(&gp->consumer, cp, consumer); 429 return(cp); 430} 431 432void 433g_destroy_consumer(struct g_consumer *cp) 434{ 435 struct g_geom *gp; 436 437 g_topology_assert(); 438 G_VALID_CONSUMER(cp); 439 g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp); 440 KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached")); 441 KASSERT (cp->acr == 0, ("g_destroy_consumer with acr")); 442 KASSERT (cp->acw == 0, ("g_destroy_consumer with acw")); 443 KASSERT (cp->ace == 0, ("g_destroy_consumer with ace")); 444 g_cancel_event(cp); 445 gp = cp->geom; 446 LIST_REMOVE(cp, consumer); 447 devstat_remove_entry(cp->stat); 448 g_free(cp); 449 if (gp->flags & G_GEOM_WITHER) 450 g_do_wither(); 451} 452 453static void 454g_new_provider_event(void *arg, int flag) 455{ 456 struct g_class *mp; 457 struct g_provider *pp; 458 struct g_consumer *cp; 459 int i; 460 461 g_topology_assert(); 462 if (flag == EV_CANCEL) 463 return; 464 if (g_shutdown) 465 return; 466 pp = arg; 467 G_VALID_PROVIDER(pp); 468 LIST_FOREACH(mp, &g_classes, class) { 469 if (mp->taste == NULL) 470 continue; 471 i = 1; 472 LIST_FOREACH(cp, &pp->consumers, consumers) 473 if (cp->geom->class == mp) 474 i = 0; 475 if (!i) 476 continue; 477 mp->taste(mp, pp, 0); 478 g_topology_assert(); 479 } 480} 481 482 483struct g_provider * 484g_new_providerf(struct g_geom *gp, const char *fmt, ...) 485{ 486 struct g_provider *pp; 487 struct sbuf *sb; 488 va_list ap; 489 490 g_topology_assert(); 491 G_VALID_GEOM(gp); 492 KASSERT(gp->access != NULL, 493 ("new provider on geom(%s) without ->access (class %s)", 494 gp->name, gp->class->name)); 495 KASSERT(gp->start != NULL, 496 ("new provider on geom(%s) without ->start (class %s)", 497 gp->name, gp->class->name)); 498 KASSERT(!(gp->flags & G_GEOM_WITHER), 499 ("new provider on WITHERing geom(%s) (class %s)", 500 gp->name, gp->class->name)); 501 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 502 va_start(ap, fmt); 503 sbuf_vprintf(sb, fmt, ap); 504 va_end(ap); 505 sbuf_finish(sb); 506 pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO); 507 pp->name = (char *)(pp + 1); 508 strcpy(pp->name, sbuf_data(sb)); 509 sbuf_delete(sb); 510 LIST_INIT(&pp->consumers); 511 pp->error = ENXIO; 512 pp->geom = gp; 513 pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED, 514 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 515 LIST_INSERT_HEAD(&gp->provider, pp, provider); 516 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL); 517 return (pp); 518} 519 520void 521g_error_provider(struct g_provider *pp, int error) 522{ 523 524 /* G_VALID_PROVIDER(pp); We may not have g_topology */ 525 pp->error = error; 526} 527 528struct g_provider * 529g_provider_by_name(char const *arg) 530{ 531 struct g_class *cp; 532 struct g_geom *gp; 533 struct g_provider *pp; 534 535 LIST_FOREACH(cp, &g_classes, class) { 536 LIST_FOREACH(gp, &cp->geom, geom) { 537 LIST_FOREACH(pp, &gp->provider, provider) { 538 if (!strcmp(arg, pp->name)) 539 return (pp); 540 } 541 } 542 } 543 return (NULL); 544} 545 546void 547g_destroy_provider(struct g_provider *pp) 548{ 549 struct g_geom *gp; 550 551 g_topology_assert(); 552 G_VALID_PROVIDER(pp); 553 KASSERT(LIST_EMPTY(&pp->consumers), 554 ("g_destroy_provider but attached")); 555 KASSERT (pp->acr == 0, ("g_destroy_provider with acr")); 556 KASSERT (pp->acw == 0, ("g_destroy_provider with acw")); 557 KASSERT (pp->acw == 0, ("g_destroy_provider with ace")); 558 g_cancel_event(pp); 559 LIST_REMOVE(pp, provider); 560 gp = pp->geom; 561 devstat_remove_entry(pp->stat); 562 g_free(pp); 563 if ((gp->flags & G_GEOM_WITHER)) 564 g_do_wither(); 565} 566 567/* 568 * We keep the "geoms" list sorted by topological order (== increasing 569 * numerical rank) at all times. 570 * When an attach is done, the attaching geoms rank is invalidated 571 * and it is moved to the tail of the list. 572 * All geoms later in the sequence has their ranks reevaluated in 573 * sequence. If we cannot assign rank to a geom because it's 574 * prerequisites do not have rank, we move that element to the tail 575 * of the sequence with invalid rank as well. 576 * At some point we encounter our original geom and if we stil fail 577 * to assign it a rank, there must be a loop and we fail back to 578 * g_attach() which detach again and calls redo_rank again 579 * to fix up the damage. 580 * It would be much simpler code wise to do it recursively, but we 581 * can't risk that on the kernel stack. 582 */ 583 584static int 585redo_rank(struct g_geom *gp) 586{ 587 struct g_consumer *cp; 588 struct g_geom *gp1, *gp2; 589 int n, m; 590 591 g_topology_assert(); 592 G_VALID_GEOM(gp); 593 594 /* Invalidate this geoms rank and move it to the tail */ 595 gp1 = TAILQ_NEXT(gp, geoms); 596 if (gp1 != NULL) { 597 gp->rank = 0; 598 TAILQ_REMOVE(&geoms, gp, geoms); 599 TAILQ_INSERT_TAIL(&geoms, gp, geoms); 600 } else { 601 gp1 = gp; 602 } 603 604 /* re-rank the rest of the sequence */ 605 for (; gp1 != NULL; gp1 = gp2) { 606 gp1->rank = 0; 607 m = 1; 608 LIST_FOREACH(cp, &gp1->consumer, consumer) { 609 if (cp->provider == NULL) 610 continue; 611 n = cp->provider->geom->rank; 612 if (n == 0) { 613 m = 0; 614 break; 615 } else if (n >= m) 616 m = n + 1; 617 } 618 gp1->rank = m; 619 gp2 = TAILQ_NEXT(gp1, geoms); 620 621 /* got a rank, moving on */ 622 if (m != 0) 623 continue; 624 625 /* no rank to original geom means loop */ 626 if (gp == gp1) 627 return (ELOOP); 628 629 /* no rank, put it at the end move on */ 630 TAILQ_REMOVE(&geoms, gp1, geoms); 631 TAILQ_INSERT_TAIL(&geoms, gp1, geoms); 632 } 633 return (0); 634} 635 636int 637g_attach(struct g_consumer *cp, struct g_provider *pp) 638{ 639 int error; 640 641 g_topology_assert(); 642 G_VALID_CONSUMER(cp); 643 G_VALID_PROVIDER(pp); 644 KASSERT(cp->provider == NULL, ("attach but attached")); 645 cp->provider = pp; 646 LIST_INSERT_HEAD(&pp->consumers, cp, consumers); 647 error = redo_rank(cp->geom); 648 if (error) { 649 LIST_REMOVE(cp, consumers); 650 cp->provider = NULL; 651 redo_rank(cp->geom); 652 } 653 return (error); 654} 655 656void 657g_detach(struct g_consumer *cp) 658{ 659 struct g_provider *pp; 660 661 g_topology_assert(); 662 G_VALID_CONSUMER(cp); 663 g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp); 664 KASSERT(cp->provider != NULL, ("detach but not attached")); 665 KASSERT(cp->acr == 0, ("detach but nonzero acr")); 666 KASSERT(cp->acw == 0, ("detach but nonzero acw")); 667 KASSERT(cp->ace == 0, ("detach but nonzero ace")); 668 KASSERT(cp->nstart == cp->nend, 669 ("detach with active requests")); 670 pp = cp->provider; 671 LIST_REMOVE(cp, consumers); 672 cp->provider = NULL; 673 if (pp->geom->flags & G_GEOM_WITHER) 674 g_do_wither(); 675 else if (pp->flags & G_PF_WITHER) 676 g_do_wither(); 677 redo_rank(cp->geom); 678} 679 680/* 681 * g_access() 682 * 683 * Access-check with delta values. The question asked is "can provider 684 * "cp" change the access counters by the relative amounts dc[rwe] ?" 685 */ 686 687int 688g_access(struct g_consumer *cp, int dcr, int dcw, int dce) 689{ 690 struct g_provider *pp; 691 int pr,pw,pe; 692 int error; 693 694 g_topology_assert(); 695 G_VALID_CONSUMER(cp); 696 pp = cp->provider; 697 KASSERT(pp != NULL, ("access but not attached")); 698 G_VALID_PROVIDER(pp); 699 700 g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)", 701 cp, pp->name, dcr, dcw, dce); 702 703 KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr")); 704 KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw")); 705 KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace")); 706 KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request")); 707 KASSERT(pp->geom->access != NULL, ("NULL geom->access")); 708 709 /* 710 * If our class cares about being spoiled, and we have been, we 711 * are probably just ahead of the event telling us that. Fail 712 * now rather than having to unravel this later. 713 */ 714 if (cp->geom->spoiled != NULL && cp->spoiled && 715 (dcr > 0 || dcw > 0 || dce > 0)) 716 return (ENXIO); 717 718 /* 719 * Figure out what counts the provider would have had, if this 720 * consumer had (r0w0e0) at this time. 721 */ 722 pr = pp->acr - cp->acr; 723 pw = pp->acw - cp->acw; 724 pe = pp->ace - cp->ace; 725 726 g_trace(G_T_ACCESS, 727 "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)", 728 dcr, dcw, dce, 729 cp->acr, cp->acw, cp->ace, 730 pp->acr, pp->acw, pp->ace, 731 pp, pp->name); 732 733 /* If foot-shooting is enabled, any open on rank#1 is OK */ 734 if ((g_debugflags & 16) && pp->geom->rank == 1) 735 ; 736 /* If we try exclusive but already write: fail */ 737 else if (dce > 0 && pw > 0) 738 return (EPERM); 739 /* If we try write but already exclusive: fail */ 740 else if (dcw > 0 && pe > 0) 741 return (EPERM); 742 /* If we try to open more but provider is error'ed: fail */ 743 else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0) 744 return (pp->error); 745 746 /* Ok then... */ 747 748 error = pp->geom->access(pp, dcr, dcw, dce); 749 KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0, 750 ("Geom provider %s::%s failed closing ->access()", 751 pp->geom->class->name, pp->name)); 752 if (!error) { 753 /* 754 * If we open first write, spoil any partner consumers. 755 * If we close last write and provider is not errored, 756 * trigger re-taste. 757 */ 758 if (pp->acw == 0 && dcw != 0) 759 g_spoil(pp, cp); 760 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 && 761 !(pp->geom->flags & G_GEOM_WITHER)) 762 g_post_event(g_new_provider_event, pp, M_WAITOK, 763 pp, NULL); 764 765 pp->acr += dcr; 766 pp->acw += dcw; 767 pp->ace += dce; 768 cp->acr += dcr; 769 cp->acw += dcw; 770 cp->ace += dce; 771 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) 772 KASSERT(pp->sectorsize > 0, 773 ("Provider %s lacks sectorsize", pp->name)); 774 } 775 return (error); 776} 777 778int 779g_handleattr_int(struct bio *bp, const char *attribute, int val) 780{ 781 782 return (g_handleattr(bp, attribute, &val, sizeof val)); 783} 784 785int 786g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val) 787{ 788 789 return (g_handleattr(bp, attribute, &val, sizeof val)); 790} 791 792int 793g_handleattr(struct bio *bp, const char *attribute, void *val, int len) 794{ 795 int error; 796 797 if (strcmp(bp->bio_attribute, attribute)) 798 return (0); 799 if (bp->bio_length != len) { 800 printf("bio_length %jd len %d -> EFAULT\n", 801 (intmax_t)bp->bio_length, len); 802 error = EFAULT; 803 } else { 804 error = 0; 805 bcopy(val, bp->bio_data, len); 806 bp->bio_completed = len; 807 } 808 g_io_deliver(bp, error); 809 return (1); 810} 811 812int 813g_std_access(struct g_provider *pp, 814 int dr __unused, int dw __unused, int de __unused) 815{ 816 817 g_topology_assert(); 818 G_VALID_PROVIDER(pp); 819 return (0); 820} 821 822void 823g_std_done(struct bio *bp) 824{ 825 struct bio *bp2; 826 827 bp2 = bp->bio_parent; 828 if (bp2->bio_error == 0) 829 bp2->bio_error = bp->bio_error; 830 bp2->bio_completed += bp->bio_completed; 831 g_destroy_bio(bp); 832 bp2->bio_inbed++; 833 if (bp2->bio_children == bp2->bio_inbed) 834 g_io_deliver(bp2, bp2->bio_error); 835} 836 837/* XXX: maybe this is only g_slice_spoiled */ 838 839void 840g_std_spoiled(struct g_consumer *cp) 841{ 842 struct g_geom *gp; 843 struct g_provider *pp; 844 845 g_topology_assert(); 846 G_VALID_CONSUMER(cp); 847 g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp); 848 g_detach(cp); 849 gp = cp->geom; 850 LIST_FOREACH(pp, &gp->provider, provider) 851 g_orphan_provider(pp, ENXIO); 852 g_destroy_consumer(cp); 853 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer)) 854 g_destroy_geom(gp); 855 else 856 gp->flags |= G_GEOM_WITHER; 857} 858 859/* 860 * Spoiling happens when a provider is opened for writing, but consumers 861 * which are configured by in-band data are attached (slicers for instance). 862 * Since the write might potentially change the in-band data, such consumers 863 * need to re-evaluate their existence after the writing session closes. 864 * We do this by (offering to) tear them down when the open for write happens 865 * in return for a re-taste when it closes again. 866 * Together with the fact that such consumers grab an 'e' bit whenever they 867 * are open, regardless of mode, this ends up DTRT. 868 */ 869 870static void 871g_spoil_event(void *arg, int flag) 872{ 873 struct g_provider *pp; 874 struct g_consumer *cp, *cp2; 875 876 g_topology_assert(); 877 if (flag == EV_CANCEL) 878 return; 879 pp = arg; 880 G_VALID_PROVIDER(pp); 881 for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) { 882 cp2 = LIST_NEXT(cp, consumers); 883 if (!cp->spoiled) 884 continue; 885 cp->spoiled = 0; 886 if (cp->geom->spoiled == NULL) 887 continue; 888 cp->geom->spoiled(cp); 889 g_topology_assert(); 890 } 891} 892 893void 894g_spoil(struct g_provider *pp, struct g_consumer *cp) 895{ 896 struct g_consumer *cp2; 897 898 g_topology_assert(); 899 G_VALID_PROVIDER(pp); 900 G_VALID_CONSUMER(cp); 901 902 LIST_FOREACH(cp2, &pp->consumers, consumers) { 903 if (cp2 == cp) 904 continue; 905/* 906 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr)); 907 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw)); 908*/ 909 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace)); 910 cp2->spoiled++; 911 } 912 g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL); 913} 914 915int 916g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len) 917{ 918 int error, i; 919 920 i = len; 921 error = g_io_getattr(attr, cp, &i, var); 922 if (error) 923 return (error); 924 if (i != len) 925 return (EINVAL); 926 return (0); 927} 928 929#if defined(DIAGNOSTIC) || defined(DDB) 930/* 931 * This function walks (topologically unsafely) the mesh and return a 932 * non-zero integer if it finds the argument pointer is an object. 933 * The return value indicates which type of object it is belived to be. 934 * If topology is not locked, this function is potentially dangerous, 935 * but since it is for debugging purposes and can be useful for instance 936 * from DDB, we do not assert topology lock is held. 937 */ 938int 939g_valid_obj(void const *ptr) 940{ 941 struct g_class *mp; 942 struct g_geom *gp; 943 struct g_consumer *cp; 944 struct g_provider *pp; 945 946 LIST_FOREACH(mp, &g_classes, class) { 947 if (ptr == mp) 948 return (1); 949 LIST_FOREACH(gp, &mp->geom, geom) { 950 if (ptr == gp) 951 return (2); 952 LIST_FOREACH(cp, &gp->consumer, consumer) 953 if (ptr == cp) 954 return (3); 955 LIST_FOREACH(pp, &gp->provider, provider) 956 if (ptr == pp) 957 return (4); 958 } 959 } 960 return(0); 961} 962#endif 963 964#ifdef DDB 965 966#define gprintf(...) do { \ 967 printf("%*s", indent, ""); \ 968 printf(__VA_ARGS__); \ 969} while (0) 970#define gprintln(...) do { \ 971 gprintf(__VA_ARGS__); \ 972 printf("\n"); \ 973} while (0) 974 975#define ADDFLAG(obj, flag, sflag) do { \ 976 if ((obj)->flags & (flag)) { \ 977 if (comma) \ 978 strlcat(str, ",", size); \ 979 strlcat(str, (sflag), size); \ 980 comma = 1; \ 981 } \ 982} while (0) 983 984static char * 985provider_flags_to_string(struct g_provider *pp, char *str, size_t size) 986{ 987 int comma = 0; 988 989 bzero(str, size); 990 if (pp->flags == 0) { 991 strlcpy(str, "NONE", size); 992 return (str); 993 } 994 ADDFLAG(pp, G_PF_CANDELETE, "G_PF_CANDELETE"); 995 ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER"); 996 ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN"); 997 return (str); 998} 999 1000static char * 1001geom_flags_to_string(struct g_geom *gp, char *str, size_t size) 1002{ 1003 int comma = 0; 1004 1005 bzero(str, size); 1006 if (gp->flags == 0) { 1007 strlcpy(str, "NONE", size); 1008 return (str); 1009 } 1010 ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER"); 1011 return (str); 1012} 1013static void 1014db_show_geom_consumer(int indent, struct g_consumer *cp) 1015{ 1016 1017 if (indent == 0) { 1018 gprintln("consumer: %p", cp); 1019 gprintln(" class: %s (%p)", cp->geom->class->name, 1020 cp->geom->class); 1021 gprintln(" geom: %s (%p)", cp->geom->name, cp->geom); 1022 if (cp->provider == NULL) 1023 gprintln(" provider: none"); 1024 else { 1025 gprintln(" provider: %s (%p)", cp->provider->name, 1026 cp->provider); 1027 } 1028 gprintln(" access: r%dw%de%d", cp->acr, cp->acw, cp->ace); 1029 gprintln(" spoiled: %d", cp->spoiled); 1030 gprintln(" nstart: %u", cp->nstart); 1031 gprintln(" nend: %u", cp->nend); 1032 } else { 1033 gprintf("consumer: %p (%s), access=r%dw%de%d", cp, 1034 cp->provider != NULL ? cp->provider->name : "none", 1035 cp->acr, cp->acw, cp->ace); 1036 if (cp->spoiled) 1037 printf(", spoiled=%d", cp->spoiled); 1038 printf("\n"); 1039 } 1040} 1041 1042static void 1043db_show_geom_provider(int indent, struct g_provider *pp) 1044{ 1045 struct g_consumer *cp; 1046 char flags[64]; 1047 1048 if (indent == 0) { 1049 gprintln("provider: %s (%p)", pp->name, pp); 1050 gprintln(" class: %s (%p)", pp->geom->class->name, 1051 pp->geom->class); 1052 gprintln(" geom: %s (%p)", pp->geom->name, pp->geom); 1053 gprintln(" mediasize: %jd", (intmax_t)pp->mediasize); 1054 gprintln(" sectorsize: %u", pp->sectorsize); 1055 gprintln(" stripesize: %u", pp->stripesize); 1056 gprintln(" stripeoffset: %u", pp->stripeoffset); 1057 gprintln(" access: r%dw%de%d", pp->acr, pp->acw, 1058 pp->ace); 1059 gprintln(" flags: %s (0x%04x)", 1060 provider_flags_to_string(pp, flags, sizeof(flags)), 1061 pp->flags); 1062 gprintln(" error: %d", pp->error); 1063 gprintln(" nstart: %u", pp->nstart); 1064 gprintln(" nend: %u", pp->nend); 1065 if (LIST_EMPTY(&pp->consumers)) 1066 gprintln(" consumers: none"); 1067 } else { 1068 gprintf("provider: %s (%p), access=r%dw%de%d", 1069 pp->name, pp, pp->acr, pp->acw, pp->ace); 1070 if (pp->flags != 0) { 1071 printf(", flags=%s (0x%04x)", 1072 provider_flags_to_string(pp, flags, sizeof(flags)), 1073 pp->flags); 1074 } 1075 printf("\n"); 1076 } 1077 if (!LIST_EMPTY(&pp->consumers)) { 1078 LIST_FOREACH(cp, &pp->consumers, consumers) 1079 db_show_geom_consumer(indent + 2, cp); 1080 } 1081} 1082 1083static void 1084db_show_geom_geom(int indent, struct g_geom *gp) 1085{ 1086 struct g_provider *pp; 1087 struct g_consumer *cp; 1088 char flags[64]; 1089 1090 if (indent == 0) { 1091 gprintln("geom: %s (%p)", gp->name, gp); 1092 gprintln(" class: %s (%p)", gp->class->name, gp->class); 1093 gprintln(" flags: %s (0x%04x)", 1094 geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags); 1095 gprintln(" rank: %d", gp->rank); 1096 if (LIST_EMPTY(&gp->provider)) 1097 gprintln(" providers: none"); 1098 if (LIST_EMPTY(&gp->consumer)) 1099 gprintln(" consumers: none"); 1100 } else { 1101 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank); 1102 if (gp->flags != 0) { 1103 printf(", flags=%s (0x%04x)", 1104 geom_flags_to_string(gp, flags, sizeof(flags)), 1105 gp->flags); 1106 } 1107 printf("\n"); 1108 } 1109 if (!LIST_EMPTY(&gp->provider)) { 1110 LIST_FOREACH(pp, &gp->provider, provider) 1111 db_show_geom_provider(indent + 2, pp); 1112 } 1113 if (!LIST_EMPTY(&gp->consumer)) { 1114 LIST_FOREACH(cp, &gp->consumer, consumer) 1115 db_show_geom_consumer(indent + 2, cp); 1116 } 1117} 1118 1119static void 1120db_show_geom_class(struct g_class *mp) 1121{ 1122 struct g_geom *gp; 1123 1124 printf("class: %s (%p)\n", mp->name, mp); 1125 LIST_FOREACH(gp, &mp->geom, geom) 1126 db_show_geom_geom(2, gp); 1127} 1128 1129/* 1130 * Print the GEOM topology or the given object. 1131 */ 1132DB_SHOW_COMMAND(geom, db_show_geom) 1133{ 1134 struct g_class *mp; 1135 1136 if (!have_addr) { 1137 /* No address given, print the entire topology. */ 1138 LIST_FOREACH(mp, &g_classes, class) { 1139 db_show_geom_class(mp); 1140 printf("\n"); 1141 } 1142 } else { 1143 switch (g_valid_obj((void *)addr)) { 1144 case 1: 1145 db_show_geom_class((struct g_class *)addr); 1146 break; 1147 case 2: 1148 db_show_geom_geom(0, (struct g_geom *)addr); 1149 break; 1150 case 3: 1151 db_show_geom_consumer(0, (struct g_consumer *)addr); 1152 break; 1153 case 4: 1154 db_show_geom_provider(0, (struct g_provider *)addr); 1155 break; 1156 default: 1157 printf("Not a GEOM object.\n"); 1158 break; 1159 } 1160 } 1161} 1162 1163#undef gprintf 1164#undef gprintln 1165#undef ADDFLAG 1166 1167#endif /* DDB */ 1168