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