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