geom_subr.c revision 115949
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 * $FreeBSD: head/sys/geom/geom_subr.c 115949 2003-06-07 10:16:53Z phk $ 36 */ 37 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 60 61struct g_hh00 { 62 struct g_class *mp; 63 int error; 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 g_free(hh); 86 g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name); 87 LIST_FOREACH(mp2, &g_classes, class) { 88 KASSERT(mp2 != mp, 89 ("The GEOM class %s already loaded", mp2->name)); 90 KASSERT(strcmp(mp2->name, mp->name) != 0, 91 ("A GEOM class named %s is already loaded", mp2->name)); 92 } 93 94 if (mp->init != NULL) 95 mp->init(mp); 96 LIST_INIT(&mp->geom); 97 LIST_INSERT_HEAD(&g_classes, mp, class); 98 if (mp->taste == NULL) 99 return; 100 LIST_FOREACH(mp2, &g_classes, class) { 101 if (mp == mp2) 102 continue; 103 LIST_FOREACH(gp, &mp2->geom, geom) { 104 LIST_FOREACH(pp, &gp->provider, provider) { 105 mp->taste(mp, pp, 0); 106 g_topology_assert(); 107 } 108 } 109 } 110} 111 112static void 113g_unload_class(void *arg, int flag) 114{ 115 struct g_hh00 *hh; 116 struct g_class *mp; 117 struct g_geom *gp; 118 struct g_provider *pp; 119 struct g_consumer *cp; 120 int error; 121 122 g_topology_assert(); 123 hh = arg; 124 mp = hh->mp; 125 g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name); 126 if (mp->destroy_geom == NULL) { 127 hh->error = EOPNOTSUPP; 128 return; 129 } 130 131 /* We refuse to unload if anything is open */ 132 LIST_FOREACH(gp, &mp->geom, geom) { 133 LIST_FOREACH(pp, &gp->provider, provider) 134 if (pp->acr || pp->acw || pp->ace) { 135 hh->error = EBUSY; 136 return; 137 } 138 LIST_FOREACH(cp, &gp->consumer, consumer) 139 if (cp->acr || cp->acw || cp->ace) { 140 hh->error = EBUSY; 141 return; 142 } 143 } 144 145 /* Bar new entries */ 146 mp->taste = NULL; 147 mp->config = NULL; 148 149 error = 0; 150 LIST_FOREACH(gp, &mp->geom, geom) { 151 error = mp->destroy_geom(NULL, mp, gp); 152 if (error != 0) 153 break; 154 } 155 if (error == 0) { 156 LIST_REMOVE(mp, class); 157 if (mp->fini != NULL) 158 mp->fini(mp); 159 } 160 hh->error = error; 161 return; 162} 163 164int 165g_modevent(module_t mod, int type, void *data) 166{ 167 struct g_hh00 *hh; 168 int error; 169 static int g_ignition; 170 171 if (!g_ignition) { 172 g_ignition++; 173 g_init(); 174 } 175 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO); 176 hh->mp = data; 177 error = EOPNOTSUPP; 178 switch (type) { 179 case MOD_LOAD: 180 g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name); 181 g_post_event(g_load_class, hh, M_WAITOK, NULL); 182 error = 0; 183 break; 184 case MOD_UNLOAD: 185 g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name); 186 error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL); 187 if (error == 0) 188 error = hh->error; 189 g_waitidle(); 190 KASSERT(LIST_EMPTY(&hh->mp->geom), 191 ("Unloaded class (%s) still has geom", hh->mp->name)); 192 g_free(hh); 193 break; 194 } 195 return (error); 196} 197 198struct g_geom * 199g_new_geomf(struct g_class *mp, const char *fmt, ...) 200{ 201 struct g_geom *gp; 202 va_list ap; 203 struct sbuf *sb; 204 205 g_topology_assert(); 206 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 207 va_start(ap, fmt); 208 sbuf_vprintf(sb, fmt, ap); 209 va_end(ap); 210 sbuf_finish(sb); 211 gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO); 212 gp->protect = 0x020016601; 213 gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO); 214 gp->class = mp; 215 gp->rank = 1; 216 LIST_INIT(&gp->consumer); 217 LIST_INIT(&gp->provider); 218 LIST_INSERT_HEAD(&mp->geom, gp, geom); 219 TAILQ_INSERT_HEAD(&geoms, gp, geoms); 220 strcpy(gp->name, sbuf_data(sb)); 221 sbuf_delete(sb); 222 return (gp); 223} 224 225void 226g_destroy_geom(struct g_geom *gp) 227{ 228 229 g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name); 230 g_topology_assert(); 231 KASSERT(LIST_EMPTY(&gp->consumer), 232 ("g_destroy_geom(%s) with consumer(s) [%p]", 233 gp->name, LIST_FIRST(&gp->consumer))); 234 KASSERT(LIST_EMPTY(&gp->provider), 235 ("g_destroy_geom(%s) with provider(s) [%p]", 236 gp->name, LIST_FIRST(&gp->consumer))); 237 g_cancel_event(gp); 238 LIST_REMOVE(gp, geom); 239 TAILQ_REMOVE(&geoms, gp, geoms); 240 g_free(gp->name); 241 g_free(gp); 242} 243 244/* 245 * This function is called (repeatedly) until has withered away. 246 */ 247void 248g_wither_geom(struct g_geom *gp, int error) 249{ 250 struct g_provider *pp, *pp2; 251 struct g_consumer *cp, *cp2; 252 static int once_is_enough; 253 254 if (once_is_enough) 255 return; 256 once_is_enough = 1; 257 g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name); 258 g_topology_assert(); 259 if (!(gp->flags & G_GEOM_WITHER)) { 260 gp->flags |= G_GEOM_WITHER; 261 LIST_FOREACH(pp, &gp->provider, provider) 262 g_orphan_provider(pp, error); 263 } 264 for (pp = LIST_FIRST(&gp->provider); pp != NULL; pp = pp2) { 265 pp2 = LIST_NEXT(pp, provider); 266 if (!LIST_EMPTY(&pp->consumers)) 267 continue; 268 g_destroy_provider(pp); 269 } 270 for (cp = LIST_FIRST(&gp->consumer); cp != NULL; cp = cp2) { 271 cp2 = LIST_NEXT(cp, consumer); 272 if (cp->acr || cp->acw || cp->ace) 273 continue; 274 g_detach(cp); 275 g_destroy_consumer(cp); 276 } 277 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer)) 278 g_destroy_geom(gp); 279 once_is_enough = 0; 280} 281 282struct g_consumer * 283g_new_consumer(struct g_geom *gp) 284{ 285 struct g_consumer *cp; 286 287 g_topology_assert(); 288 KASSERT(gp->orphan != NULL, 289 ("g_new_consumer on geom(%s) (class %s) without orphan", 290 gp->name, gp->class->name)); 291 292 cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO); 293 cp->protect = 0x020016602; 294 cp->geom = gp; 295 cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED, 296 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 297 LIST_INSERT_HEAD(&gp->consumer, cp, consumer); 298 return(cp); 299} 300 301void 302g_destroy_consumer(struct g_consumer *cp) 303{ 304 struct g_geom *gp; 305 306 g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp); 307 g_topology_assert(); 308 KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached")); 309 KASSERT (cp->acr == 0, ("g_destroy_consumer with acr")); 310 KASSERT (cp->acw == 0, ("g_destroy_consumer with acw")); 311 KASSERT (cp->ace == 0, ("g_destroy_consumer with ace")); 312 g_cancel_event(cp); 313 gp = cp->geom; 314 LIST_REMOVE(cp, consumer); 315 devstat_remove_entry(cp->stat); 316 g_free(cp); 317 if (gp->flags & G_GEOM_WITHER) 318 g_wither_geom(gp, 0); 319} 320 321static void 322g_new_provider_event(void *arg, int flag) 323{ 324 struct g_class *mp; 325 struct g_provider *pp; 326 struct g_consumer *cp; 327 int i; 328 329 g_topology_assert(); 330 if (flag == EV_CANCEL) 331 return; 332 if (g_shutdown) 333 return; 334 pp = arg; 335 LIST_FOREACH(mp, &g_classes, class) { 336 if (mp->taste == NULL) 337 continue; 338 i = 1; 339 LIST_FOREACH(cp, &pp->consumers, consumers) 340 if (cp->geom->class == mp) 341 i = 0; 342 if (!i) 343 continue; 344 mp->taste(mp, pp, 0); 345 g_topology_assert(); 346 } 347} 348 349 350struct g_provider * 351g_new_providerf(struct g_geom *gp, const char *fmt, ...) 352{ 353 struct g_provider *pp; 354 struct sbuf *sb; 355 va_list ap; 356 357 g_topology_assert(); 358 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 359 va_start(ap, fmt); 360 sbuf_vprintf(sb, fmt, ap); 361 va_end(ap); 362 sbuf_finish(sb); 363 pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO); 364 pp->protect = 0x020016603; 365 pp->name = (char *)(pp + 1); 366 strcpy(pp->name, sbuf_data(sb)); 367 sbuf_delete(sb); 368 LIST_INIT(&pp->consumers); 369 pp->error = ENXIO; 370 pp->geom = gp; 371 pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED, 372 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 373 LIST_INSERT_HEAD(&gp->provider, pp, provider); 374 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL); 375 return (pp); 376} 377 378void 379g_error_provider(struct g_provider *pp, int error) 380{ 381 382 pp->error = error; 383} 384 385struct g_provider * 386g_provider_by_name(char const *arg) 387{ 388 struct g_class *cp; 389 struct g_geom *gp; 390 struct g_provider *pp; 391 392 LIST_FOREACH(cp, &g_classes, class) { 393 LIST_FOREACH(gp, &cp->geom, geom) { 394 LIST_FOREACH(pp, &gp->provider, provider) { 395 if (!strcmp(arg, pp->name)) 396 return (pp); 397 } 398 } 399 } 400 return (NULL); 401} 402 403void 404g_destroy_provider(struct g_provider *pp) 405{ 406 struct g_geom *gp; 407 408 g_topology_assert(); 409 KASSERT(LIST_EMPTY(&pp->consumers), 410 ("g_destroy_provider but attached")); 411 KASSERT (pp->acr == 0, ("g_destroy_provider with acr")); 412 KASSERT (pp->acw == 0, ("g_destroy_provider with acw")); 413 KASSERT (pp->acw == 0, ("g_destroy_provider with ace")); 414 g_cancel_event(pp); 415 LIST_REMOVE(pp, provider); 416 gp = pp->geom; 417 devstat_remove_entry(pp->stat); 418 g_free(pp); 419 if ((gp->flags & G_GEOM_WITHER)) 420 g_wither_geom(gp, 0); 421} 422 423/* 424 * We keep the "geoms" list sorted by topological order (== increasing 425 * numerical rank) at all times. 426 * When an attach is done, the attaching geoms rank is invalidated 427 * and it is moved to the tail of the list. 428 * All geoms later in the sequence has their ranks reevaluated in 429 * sequence. If we cannot assign rank to a geom because it's 430 * prerequisites do not have rank, we move that element to the tail 431 * of the sequence with invalid rank as well. 432 * At some point we encounter our original geom and if we stil fail 433 * to assign it a rank, there must be a loop and we fail back to 434 * g_attach() which detach again and calls redo_rank again 435 * to fix up the damage. 436 * It would be much simpler code wise to do it recursively, but we 437 * can't risk that on the kernel stack. 438 */ 439 440static int 441redo_rank(struct g_geom *gp) 442{ 443 struct g_consumer *cp; 444 struct g_geom *gp1, *gp2; 445 int n, m; 446 447 g_topology_assert(); 448 449 /* Invalidate this geoms rank and move it to the tail */ 450 gp1 = TAILQ_NEXT(gp, geoms); 451 if (gp1 != NULL) { 452 gp->rank = 0; 453 TAILQ_REMOVE(&geoms, gp, geoms); 454 TAILQ_INSERT_TAIL(&geoms, gp, geoms); 455 } else { 456 gp1 = gp; 457 } 458 459 /* re-rank the rest of the sequence */ 460 for (; gp1 != NULL; gp1 = gp2) { 461 gp1->rank = 0; 462 m = 1; 463 LIST_FOREACH(cp, &gp1->consumer, consumer) { 464 if (cp->provider == NULL) 465 continue; 466 n = cp->provider->geom->rank; 467 if (n == 0) { 468 m = 0; 469 break; 470 } else if (n >= m) 471 m = n + 1; 472 } 473 gp1->rank = m; 474 gp2 = TAILQ_NEXT(gp1, geoms); 475 476 /* got a rank, moving on */ 477 if (m != 0) 478 continue; 479 480 /* no rank to original geom means loop */ 481 if (gp == gp1) 482 return (ELOOP); 483 484 /* no rank, put it at the end move on */ 485 TAILQ_REMOVE(&geoms, gp1, geoms); 486 TAILQ_INSERT_TAIL(&geoms, gp1, geoms); 487 } 488 return (0); 489} 490 491int 492g_attach(struct g_consumer *cp, struct g_provider *pp) 493{ 494 int error; 495 496 g_topology_assert(); 497 KASSERT(cp->provider == NULL, ("attach but attached")); 498 cp->provider = pp; 499 LIST_INSERT_HEAD(&pp->consumers, cp, consumers); 500 error = redo_rank(cp->geom); 501 if (error) { 502 LIST_REMOVE(cp, consumers); 503 cp->provider = NULL; 504 redo_rank(cp->geom); 505 } 506 return (error); 507} 508 509void 510g_detach(struct g_consumer *cp) 511{ 512 struct g_provider *pp; 513 514 g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp); 515 KASSERT(cp != (void*)0xd0d0d0d0, ("ARGH!")); 516 g_topology_assert(); 517 KASSERT(cp->provider != NULL, ("detach but not attached")); 518 KASSERT(cp->acr == 0, ("detach but nonzero acr")); 519 KASSERT(cp->acw == 0, ("detach but nonzero acw")); 520 KASSERT(cp->ace == 0, ("detach but nonzero ace")); 521 KASSERT(cp->nstart == cp->nend, 522 ("detach with active requests")); 523 pp = cp->provider; 524 LIST_REMOVE(cp, consumers); 525 cp->provider = NULL; 526 if (pp->geom->flags & G_GEOM_WITHER) 527 g_wither_geom(pp->geom, 0); 528 redo_rank(cp->geom); 529} 530 531 532/* 533 * g_access_abs() 534 * 535 * Access-check with absolute new values: Just fall through 536 * and use the relative version. 537 */ 538int 539g_access_abs(struct g_consumer *cp, int acr, int acw, int ace) 540{ 541 542 g_topology_assert(); 543 return(g_access_rel(cp, 544 acr - cp->acr, 545 acw - cp->acw, 546 ace - cp->ace)); 547} 548 549/* 550 * g_access_rel() 551 * 552 * Access-check with delta values. The question asked is "can provider 553 * "cp" change the access counters by the relative amounts dc[rwe] ?" 554 */ 555 556int 557g_access_rel(struct g_consumer *cp, int dcr, int dcw, int dce) 558{ 559 struct g_provider *pp; 560 int pr,pw,pe; 561 int error; 562 563 pp = cp->provider; 564 565 g_trace(G_T_ACCESS, "g_access_rel(%p(%s), %d, %d, %d)", 566 cp, pp->name, dcr, dcw, dce); 567 568 g_topology_assert(); 569 KASSERT(cp->provider != NULL, ("access but not attached")); 570 KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr")); 571 KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw")); 572 KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace")); 573 KASSERT(pp->geom->access != NULL, ("NULL geom->access")); 574 575 /* 576 * If our class cares about being spoiled, and we have been, we 577 * are probably just ahead of the event telling us that. Fail 578 * now rather than having to unravel this later. 579 */ 580 if (cp->geom->spoiled != NULL && cp->spoiled) { 581 KASSERT(dcr <= 0, ("spoiled but dcr = %d", dcr)); 582 KASSERT(dcw <= 0, ("spoiled but dce = %d", dcw)); 583 KASSERT(dce <= 0, ("spoiled but dcw = %d", dce)); 584 } 585 586 /* 587 * Figure out what counts the provider would have had, if this 588 * consumer had (r0w0e0) at this time. 589 */ 590 pr = pp->acr - cp->acr; 591 pw = pp->acw - cp->acw; 592 pe = pp->ace - cp->ace; 593 594 g_trace(G_T_ACCESS, 595 "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)", 596 dcr, dcw, dce, 597 cp->acr, cp->acw, cp->ace, 598 pp->acr, pp->acw, pp->ace, 599 pp, pp->name); 600 601 /* If foot-shooting is enabled, any open on rank#1 is OK */ 602 if ((g_debugflags & 16) && pp->geom->rank == 1) 603 ; 604 /* If we try exclusive but already write: fail */ 605 else if (dce > 0 && pw > 0) 606 return (EPERM); 607 /* If we try write but already exclusive: fail */ 608 else if (dcw > 0 && pe > 0) 609 return (EPERM); 610 /* If we try to open more but provider is error'ed: fail */ 611 else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0) 612 return (pp->error); 613 614 /* Ok then... */ 615 616 error = pp->geom->access(pp, dcr, dcw, dce); 617 if (!error) { 618 /* 619 * If we open first write, spoil any partner consumers. 620 * If we close last write, trigger re-taste. 621 */ 622 if (pp->acw == 0 && dcw != 0) 623 g_spoil(pp, cp); 624 else if (pp->acw != 0 && pp->acw == -dcw && 625 !(pp->geom->flags & G_GEOM_WITHER)) 626 g_post_event(g_new_provider_event, pp, M_WAITOK, 627 pp, NULL); 628 629 pp->acr += dcr; 630 pp->acw += dcw; 631 pp->ace += dce; 632 cp->acr += dcr; 633 cp->acw += dcw; 634 cp->ace += dce; 635 } 636 return (error); 637} 638 639int 640g_handleattr_int(struct bio *bp, const char *attribute, int val) 641{ 642 643 return (g_handleattr(bp, attribute, &val, sizeof val)); 644} 645 646int 647g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val) 648{ 649 650 return (g_handleattr(bp, attribute, &val, sizeof val)); 651} 652 653int 654g_handleattr(struct bio *bp, const char *attribute, void *val, int len) 655{ 656 int error; 657 658 if (strcmp(bp->bio_attribute, attribute)) 659 return (0); 660 if (bp->bio_length != len) { 661 printf("bio_length %jd len %d -> EFAULT\n", 662 (intmax_t)bp->bio_length, len); 663 error = EFAULT; 664 } else { 665 error = 0; 666 bcopy(val, bp->bio_data, len); 667 bp->bio_completed = len; 668 } 669 g_io_deliver(bp, error); 670 return (1); 671} 672 673int 674g_std_access(struct g_provider *pp __unused, 675 int dr __unused, int dw __unused, int de __unused) 676{ 677 678 return (0); 679} 680 681void 682g_std_done(struct bio *bp) 683{ 684 struct bio *bp2; 685 686 bp2 = bp->bio_parent; 687 if (bp2->bio_error == 0) 688 bp2->bio_error = bp->bio_error; 689 bp2->bio_completed += bp->bio_completed; 690 g_destroy_bio(bp); 691 bp2->bio_inbed++; 692 if (bp2->bio_children == bp2->bio_inbed) 693 g_io_deliver(bp2, bp2->bio_error); 694} 695 696/* XXX: maybe this is only g_slice_spoiled */ 697 698void 699g_std_spoiled(struct g_consumer *cp) 700{ 701 struct g_geom *gp; 702 struct g_provider *pp; 703 704 g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp); 705 g_topology_assert(); 706 g_detach(cp); 707 gp = cp->geom; 708 LIST_FOREACH(pp, &gp->provider, provider) 709 g_orphan_provider(pp, ENXIO); 710 g_destroy_consumer(cp); 711 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer)) 712 g_destroy_geom(gp); 713 else 714 gp->flags |= G_GEOM_WITHER; 715} 716 717/* 718 * Spoiling happens when a provider is opened for writing, but consumers 719 * which are configured by in-band data are attached (slicers for instance). 720 * Since the write might potentially change the in-band data, such consumers 721 * need to re-evaluate their existence after the writing session closes. 722 * We do this by (offering to) tear them down when the open for write happens 723 * in return for a re-taste when it closes again. 724 * Together with the fact that such consumers grab an 'e' bit whenever they 725 * are open, regardless of mode, this ends up DTRT. 726 */ 727 728static void 729g_spoil_event(void *arg, int flag) 730{ 731 struct g_provider *pp; 732 struct g_consumer *cp, *cp2; 733 734 g_topology_assert(); 735 if (flag == EV_CANCEL) 736 return; 737 pp = arg; 738 for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) { 739 cp2 = LIST_NEXT(cp, consumers); 740 if (!cp->spoiled) 741 continue; 742 cp->spoiled = 0; 743 if (cp->geom->spoiled == NULL) 744 continue; 745 cp->geom->spoiled(cp); 746 g_topology_assert(); 747 } 748} 749 750void 751g_spoil(struct g_provider *pp, struct g_consumer *cp) 752{ 753 struct g_consumer *cp2; 754 755 g_topology_assert(); 756 757 LIST_FOREACH(cp2, &pp->consumers, consumers) { 758 if (cp2 == cp) 759 continue; 760/* 761 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr)); 762 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw)); 763*/ 764 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace)); 765 cp2->spoiled++; 766 } 767 g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL); 768} 769 770int 771g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len) 772{ 773 int error, i; 774 775 i = len; 776 error = g_io_getattr(attr, cp, &i, var); 777 if (error) 778 return (error); 779 if (i != len) 780 return (EINVAL); 781 return (0); 782} 783 784/* 785 * Check if the given pointer is a live object 786 */ 787 788void 789g_sanity(void const *ptr) 790{ 791 struct g_class *mp; 792 struct g_geom *gp; 793 struct g_consumer *cp; 794 struct g_provider *pp; 795 796 if (!(g_debugflags & 0x8)) 797 return; 798 LIST_FOREACH(mp, &g_classes, class) { 799 KASSERT(mp != ptr, ("Ptr is live class")); 800 KASSERT(mp->protect == 0x20016600, 801 ("corrupt class %p %x", mp, mp->protect)); 802 LIST_FOREACH(gp, &mp->geom, geom) { 803 KASSERT(gp != ptr, ("Ptr is live geom")); 804 KASSERT(gp->protect == 0x20016601, 805 ("corrupt geom, %p %x", gp, gp->protect)); 806 KASSERT(gp->name != ptr, ("Ptr is live geom's name")); 807 LIST_FOREACH(cp, &gp->consumer, consumer) { 808 KASSERT(cp != ptr, ("Ptr is live consumer")); 809 KASSERT(cp->protect == 0x20016602, 810 ("corrupt consumer %p %x", 811 cp, cp->protect)); 812 } 813 LIST_FOREACH(pp, &gp->provider, provider) { 814 KASSERT(pp != ptr, ("Ptr is live provider")); 815 KASSERT(pp->protect == 0x20016603, 816 ("corrupt provider %p %x", 817 pp, pp->protect)); 818 } 819 } 820 } 821} 822 823