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