geom_subr.c revision 113937
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 113937 2003-04-23 20:46:12Z 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_post_event(g_new_class_event, mp, M_WAITOK, 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_post_event(g_new_provider_event, pp, M_WAITOK, 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_post_event(g_new_provider_event, pp, M_WAITOK,
495			    pp, NULL);
496
497		pp->acr += dcr;
498		pp->acw += dcw;
499		pp->ace += dce;
500		cp->acr += dcr;
501		cp->acw += dcw;
502		cp->ace += dce;
503	}
504	return (error);
505}
506
507int
508g_handleattr_int(struct bio *bp, const char *attribute, int val)
509{
510
511	return (g_handleattr(bp, attribute, &val, sizeof val));
512}
513
514int
515g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
516{
517
518	return (g_handleattr(bp, attribute, &val, sizeof val));
519}
520
521int
522g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
523{
524	int error;
525
526	if (strcmp(bp->bio_attribute, attribute))
527		return (0);
528	if (bp->bio_length != len) {
529		printf("bio_length %jd len %d -> EFAULT\n",
530		    (intmax_t)bp->bio_length, len);
531		error = EFAULT;
532	} else {
533		error = 0;
534		bcopy(val, bp->bio_data, len);
535		bp->bio_completed = len;
536	}
537	g_io_deliver(bp, error);
538	return (1);
539}
540
541int
542g_std_access(struct g_provider *pp __unused,
543	int dr __unused, int dw __unused, int de __unused)
544{
545
546        return (0);
547}
548
549void
550g_std_done(struct bio *bp)
551{
552	struct bio *bp2;
553
554	bp2 = bp->bio_parent;
555	if (bp2->bio_error == 0)
556		bp2->bio_error = bp->bio_error;
557	bp2->bio_completed += bp->bio_completed;
558	g_destroy_bio(bp);
559	bp2->bio_inbed++;
560	if (bp2->bio_children == bp2->bio_inbed)
561		g_io_deliver(bp2, bp2->bio_error);
562}
563
564/* XXX: maybe this is only g_slice_spoiled */
565
566void
567g_std_spoiled(struct g_consumer *cp)
568{
569	struct g_geom *gp;
570	struct g_provider *pp;
571
572	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
573	g_topology_assert();
574	g_detach(cp);
575	gp = cp->geom;
576	LIST_FOREACH(pp, &gp->provider, provider)
577		g_orphan_provider(pp, ENXIO);
578	g_destroy_consumer(cp);
579	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
580		g_destroy_geom(gp);
581	else
582		gp->flags |= G_GEOM_WITHER;
583}
584
585/*
586 * Spoiling happens when a provider is opened for writing, but consumers
587 * which are configured by in-band data are attached (slicers for instance).
588 * Since the write might potentially change the in-band data, such consumers
589 * need to re-evaluate their existence after the writing session closes.
590 * We do this by (offering to) tear them down when the open for write happens
591 * in return for a re-taste when it closes again.
592 * Together with the fact that such consumers grab an 'e' bit whenever they
593 * are open, regardless of mode, this ends up DTRT.
594 */
595
596static void
597g_spoil_event(void *arg, int flag)
598{
599	struct g_provider *pp;
600	struct g_consumer *cp, *cp2;
601
602	g_topology_assert();
603	if (flag == EV_CANCEL)
604		return;
605	pp = arg;
606	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
607		cp2 = LIST_NEXT(cp, consumers);
608		if (!cp->spoiled)
609			continue;
610		cp->spoiled = 0;
611		if (cp->geom->spoiled == NULL)
612			continue;
613		cp->geom->spoiled(cp);
614		g_topology_assert();
615	}
616}
617
618void
619g_spoil(struct g_provider *pp, struct g_consumer *cp)
620{
621	struct g_consumer *cp2;
622
623	g_topology_assert();
624
625	if (!strcmp(pp->name, "geom.ctl"))
626		return;
627	LIST_FOREACH(cp2, &pp->consumers, consumers) {
628		if (cp2 == cp)
629			continue;
630/*
631		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
632		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
633*/
634		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
635		cp2->spoiled++;
636	}
637	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
638}
639
640int
641g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
642{
643	int error, i;
644
645	i = len;
646	error = g_io_getattr(attr, cp, &i, var);
647	if (error)
648		return (error);
649	if (i != len)
650		return (EINVAL);
651	return (0);
652}
653
654/*
655 * Check if the given pointer is a live object
656 */
657
658void
659g_sanity(void *ptr)
660{
661	struct g_class *mp;
662	struct g_geom *gp;
663	struct g_consumer *cp;
664	struct g_provider *pp;
665
666	if (!(g_debugflags & 0x8))
667		return;
668	LIST_FOREACH(mp, &g_classes, class) {
669		KASSERT(mp != ptr, ("Ptr is live class"));
670		KASSERT(mp->protect == 0x20016600,
671		    ("corrupt class %p %x", mp, mp->protect));
672		LIST_FOREACH(gp, &mp->geom, geom) {
673			KASSERT(gp != ptr, ("Ptr is live geom"));
674			KASSERT(gp->protect == 0x20016601,
675			    ("corrupt geom, %p %x", gp, gp->protect));
676			KASSERT(gp->name != ptr, ("Ptr is live geom's name"));
677			LIST_FOREACH(cp, &gp->consumer, consumer) {
678				KASSERT(cp != ptr, ("Ptr is live consumer"));
679				KASSERT(cp->protect == 0x20016602,
680				    ("corrupt consumer %p %x",
681				    cp, cp->protect));
682			}
683			LIST_FOREACH(pp, &gp->provider, provider) {
684				KASSERT(pp != ptr, ("Ptr is live provider"));
685				KASSERT(pp->protect == 0x20016603,
686				    ("corrupt provider %p %x",
687				    pp, pp->protect));
688			}
689		}
690	}
691}
692
693struct g_class *
694g_idclass(struct geomidorname *p)
695{
696	struct g_class *mp;
697	char *n;
698
699	if (p->len == 0) {
700		LIST_FOREACH(mp, &g_classes, class)
701			if ((uintptr_t)mp == p->u.id)
702				return (mp);
703		return (NULL);
704	}
705	n = g_malloc(p->len + 1, M_WAITOK);
706	if (copyin(p->u.name, n, p->len) == 0) {
707		n[p->len] = '\0';
708		LIST_FOREACH(mp, &g_classes, class)
709			if (!bcmp(n, mp->name, p->len + 1)) {
710				g_free(n);
711				return (mp);
712			}
713	}
714	g_free(n);
715	return (NULL);
716}
717
718struct g_geom *
719g_idgeom(struct geomidorname *p)
720{
721	struct g_class *mp;
722	struct g_geom *gp;
723	char *n;
724
725	if (p->len == 0) {
726		LIST_FOREACH(mp, &g_classes, class)
727			LIST_FOREACH(gp, &mp->geom, geom)
728				if ((uintptr_t)gp == p->u.id)
729					return (gp);
730		return (NULL);
731	}
732	n = g_malloc(p->len + 1, M_WAITOK);
733	if (copyin(p->u.name, n, p->len) == 0) {
734		n[p->len] = '\0';
735		LIST_FOREACH(mp, &g_classes, class)
736			LIST_FOREACH(gp, &mp->geom, geom)
737				if (!bcmp(n, gp->name, p->len + 1)) {
738					g_free(n);
739					return (gp);
740				}
741	}
742	g_free(n);
743	return (NULL);
744}
745
746struct g_provider *
747g_idprovider(struct geomidorname *p)
748{
749	struct g_class *mp;
750	struct g_geom *gp;
751	struct g_provider *pp;
752	char *n;
753
754	if (p->len == 0) {
755		LIST_FOREACH(mp, &g_classes, class)
756			LIST_FOREACH(gp, &mp->geom, geom)
757				LIST_FOREACH(pp, &gp->provider, provider)
758					if ((uintptr_t)pp == p->u.id)
759						return (pp);
760		return (NULL);
761	}
762	n = g_malloc(p->len + 1, M_WAITOK);
763	if (copyin(p->u.name, n, p->len) == 0) {
764		n[p->len] = '\0';
765		LIST_FOREACH(mp, &g_classes, class)
766			LIST_FOREACH(gp, &mp->geom, geom)
767				LIST_FOREACH(pp, &gp->provider, provider)
768					if (!bcmp(n, pp->name, p->len + 1)) {
769						g_free(n);
770						return (pp);
771					}
772	}
773	g_free(n);
774	return (NULL);
775}
776