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