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