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