geom_subr.c revision 113929
1130803Smarcel/*-
2130803Smarcel * Copyright (c) 2002 Poul-Henning Kamp
3130803Smarcel * Copyright (c) 2002 Networks Associates Technology, Inc.
4130803Smarcel * All rights reserved.
5130803Smarcel *
6130803Smarcel * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7130803Smarcel * and NAI Labs, the Security Research Division of Network Associates, Inc.
8130803Smarcel * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9130803Smarcel * DARPA CHATS research program.
10130803Smarcel *
11130803Smarcel * Redistribution and use in source and binary forms, with or without
12130803Smarcel * modification, are permitted provided that the following conditions
13130803Smarcel * are met:
14130803Smarcel * 1. Redistributions of source code must retain the above copyright
15130803Smarcel *    notice, this list of conditions and the following disclaimer.
16130803Smarcel * 2. Redistributions in binary form must reproduce the above copyright
17130803Smarcel *    notice, this list of conditions and the following disclaimer in the
18130803Smarcel *    documentation and/or other materials provided with the distribution.
19130803Smarcel * 3. The names of the authors may not be used to endorse or promote
20130803Smarcel *    products derived from this software without specific prior written
21130803Smarcel *    permission.
22130803Smarcel *
23130803Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24130803Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25130803Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26130803Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27130803Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28130803Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29130803Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30130803Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31130803Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32130803Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33130803Smarcel * SUCH DAMAGE.
34130803Smarcel *
35130803Smarcel * $FreeBSD: head/sys/geom/geom_subr.c 113929 2003-04-23 20:06:38Z phk $
36130803Smarcel */
37130803Smarcel
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
192struct g_provider *
193g_new_providerf(struct g_geom *gp, const char *fmt, ...)
194{
195	struct g_provider *pp;
196	struct sbuf *sb;
197	va_list ap;
198
199	g_topology_assert();
200	va_start(ap, fmt);
201	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
202	sbuf_vprintf(sb, fmt, ap);
203	sbuf_finish(sb);
204	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
205	pp->protect = 0x020016603;
206	pp->name = (char *)(pp + 1);
207	strcpy(pp->name, sbuf_data(sb));
208	sbuf_delete(sb);
209	LIST_INIT(&pp->consumers);
210	pp->error = ENXIO;
211	pp->geom = gp;
212	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
213	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
214	LIST_INSERT_HEAD(&gp->provider, pp, provider);
215	g_nproviders++;
216	g_post_event(EV_NEW_PROVIDER, pp, NULL);
217	return (pp);
218}
219
220void
221g_error_provider(struct g_provider *pp, int error)
222{
223
224	pp->error = error;
225}
226
227
228void
229g_destroy_provider(struct g_provider *pp)
230{
231	struct g_geom *gp;
232	struct g_consumer *cp;
233
234	g_topology_assert();
235	KASSERT(LIST_EMPTY(&pp->consumers),
236	    ("g_destroy_provider but attached"));
237	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
238	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
239	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
240	g_cancel_event(pp);
241	g_nproviders--;
242	LIST_REMOVE(pp, provider);
243	gp = pp->geom;
244	devstat_remove_entry(pp->stat);
245	g_free(pp);
246	if (!(gp->flags & G_GEOM_WITHER))
247		return;
248	if (!LIST_EMPTY(&gp->provider))
249		return;
250	for (;;) {
251		cp = LIST_FIRST(&gp->consumer);
252		if (cp == NULL)
253			break;
254		g_detach(cp);
255		g_destroy_consumer(cp);
256	}
257	g_destroy_geom(gp);
258}
259
260/*
261 * We keep the "geoms" list sorted by topological order (== increasing
262 * numerical rank) at all times.
263 * When an attach is done, the attaching geoms rank is invalidated
264 * and it is moved to the tail of the list.
265 * All geoms later in the sequence has their ranks reevaluated in
266 * sequence.  If we cannot assign rank to a geom because it's
267 * prerequisites do not have rank, we move that element to the tail
268 * of the sequence with invalid rank as well.
269 * At some point we encounter our original geom and if we stil fail
270 * to assign it a rank, there must be a loop and we fail back to
271 * g_attach() which detach again and calls redo_rank again
272 * to fix up the damage.
273 * It would be much simpler code wise to do it recursively, but we
274 * can't risk that on the kernel stack.
275 */
276
277static int
278redo_rank(struct g_geom *gp)
279{
280	struct g_consumer *cp;
281	struct g_geom *gp1, *gp2;
282	int n, m;
283
284	g_topology_assert();
285
286	/* Invalidate this geoms rank and move it to the tail */
287	gp1 = TAILQ_NEXT(gp, geoms);
288	if (gp1 != NULL) {
289		gp->rank = 0;
290		TAILQ_REMOVE(&geoms, gp, geoms);
291		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
292	} else {
293		gp1 = gp;
294	}
295
296	/* re-rank the rest of the sequence */
297	for (; gp1 != NULL; gp1 = gp2) {
298		gp1->rank = 0;
299		m = 1;
300		LIST_FOREACH(cp, &gp1->consumer, consumer) {
301			if (cp->provider == NULL)
302				continue;
303			n = cp->provider->geom->rank;
304			if (n == 0) {
305				m = 0;
306				break;
307			} else if (n >= m)
308				m = n + 1;
309		}
310		gp1->rank = m;
311		gp2 = TAILQ_NEXT(gp1, geoms);
312
313		/* got a rank, moving on */
314		if (m != 0)
315			continue;
316
317		/* no rank to original geom means loop */
318		if (gp == gp1)
319			return (ELOOP);
320
321		/* no rank, put it at the end move on */
322		TAILQ_REMOVE(&geoms, gp1, geoms);
323		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
324	}
325	return (0);
326}
327
328int
329g_attach(struct g_consumer *cp, struct g_provider *pp)
330{
331	int error;
332
333	g_topology_assert();
334	KASSERT(cp->provider == NULL, ("attach but attached"));
335	cp->provider = pp;
336	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
337	error = redo_rank(cp->geom);
338	if (error) {
339		LIST_REMOVE(cp, consumers);
340		cp->provider = NULL;
341		redo_rank(cp->geom);
342	}
343	return (error);
344}
345
346void
347g_detach(struct g_consumer *cp)
348{
349	struct g_provider *pp;
350
351	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
352	KASSERT(cp != (void*)0xd0d0d0d0, ("ARGH!"));
353	g_topology_assert();
354	KASSERT(cp->provider != NULL, ("detach but not attached"));
355	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
356	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
357	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
358	KASSERT(cp->nstart == cp->nend,
359	    ("detach with active requests"));
360	pp = cp->provider;
361	LIST_REMOVE(cp, consumers);
362	cp->provider = NULL;
363	if (LIST_EMPTY(&pp->consumers)) {
364		if (pp->geom->flags & G_GEOM_WITHER)
365			g_destroy_provider(pp);
366	}
367	redo_rank(cp->geom);
368}
369
370
371/*
372 * g_access_abs()
373 *
374 * Access-check with absolute new values:  Just fall through
375 * and use the relative version.
376 */
377int
378g_access_abs(struct g_consumer *cp, int acr, int acw, int ace)
379{
380
381	g_topology_assert();
382	return(g_access_rel(cp,
383		acr - cp->acr,
384		acw - cp->acw,
385		ace - cp->ace));
386}
387
388/*
389 * g_access_rel()
390 *
391 * Access-check with delta values.  The question asked is "can provider
392 * "cp" change the access counters by the relative amounts dc[rwe] ?"
393 */
394
395int
396g_access_rel(struct g_consumer *cp, int dcr, int dcw, int dce)
397{
398	struct g_provider *pp;
399	int pr,pw,pe;
400	int error;
401
402	pp = cp->provider;
403
404	g_trace(G_T_ACCESS, "g_access_rel(%p(%s), %d, %d, %d)",
405	    cp, pp->name, dcr, dcw, dce);
406
407	g_topology_assert();
408	KASSERT(cp->provider != NULL, ("access but not attached"));
409	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
410	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
411	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
412	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
413
414	/*
415	 * If our class cares about being spoiled, and we have been, we
416	 * are probably just ahead of the event telling us that.  Fail
417	 * now rather than having to unravel this later.
418	 */
419	if (cp->geom->spoiled != NULL && cp->spoiled) {
420		KASSERT(dcr <= 0, ("spoiled but dcr = %d", dcr));
421		KASSERT(dcw <= 0, ("spoiled but dce = %d", dcw));
422		KASSERT(dce <= 0, ("spoiled but dcw = %d", dce));
423	}
424
425	/*
426	 * Figure out what counts the provider would have had, if this
427	 * consumer had (r0w0e0) at this time.
428	 */
429	pr = pp->acr - cp->acr;
430	pw = pp->acw - cp->acw;
431	pe = pp->ace - cp->ace;
432
433	g_trace(G_T_ACCESS,
434    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
435	    dcr, dcw, dce,
436	    cp->acr, cp->acw, cp->ace,
437	    pp->acr, pp->acw, pp->ace,
438	    pp, pp->name);
439
440	/* If foot-shooting is enabled, any open on rank#1 is OK */
441	if ((g_debugflags & 16) && pp->geom->rank == 1)
442		;
443	/* If we try exclusive but already write: fail */
444	else if (dce > 0 && pw > 0)
445		return (EPERM);
446	/* If we try write but already exclusive: fail */
447	else if (dcw > 0 && pe > 0)
448		return (EPERM);
449	/* If we try to open more but provider is error'ed: fail */
450	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
451		return (pp->error);
452
453	/* Ok then... */
454
455	error = pp->geom->access(pp, dcr, dcw, dce);
456	if (!error) {
457		/*
458		 * If we open first write, spoil any partner consumers.
459		 * If we close last write, trigger re-taste.
460		 */
461		if (pp->acw == 0 && dcw != 0)
462			g_spoil(pp, cp);
463		else if (pp->acw != 0 && pp->acw == -dcw &&
464		    !(pp->geom->flags & G_GEOM_WITHER))
465			g_post_event(EV_NEW_PROVIDER, pp, NULL);
466
467		pp->acr += dcr;
468		pp->acw += dcw;
469		pp->ace += dce;
470		cp->acr += dcr;
471		cp->acw += dcw;
472		cp->ace += dce;
473	}
474	return (error);
475}
476
477int
478g_handleattr_int(struct bio *bp, const char *attribute, int val)
479{
480
481	return (g_handleattr(bp, attribute, &val, sizeof val));
482}
483
484int
485g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
486{
487
488	return (g_handleattr(bp, attribute, &val, sizeof val));
489}
490
491int
492g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
493{
494	int error;
495
496	if (strcmp(bp->bio_attribute, attribute))
497		return (0);
498	if (bp->bio_length != len) {
499		printf("bio_length %jd len %d -> EFAULT\n",
500		    (intmax_t)bp->bio_length, len);
501		error = EFAULT;
502	} else {
503		error = 0;
504		bcopy(val, bp->bio_data, len);
505		bp->bio_completed = len;
506	}
507	g_io_deliver(bp, error);
508	return (1);
509}
510
511int
512g_std_access(struct g_provider *pp __unused,
513	int dr __unused, int dw __unused, int de __unused)
514{
515
516        return (0);
517}
518
519void
520g_std_done(struct bio *bp)
521{
522	struct bio *bp2;
523
524	bp2 = bp->bio_parent;
525	if (bp2->bio_error == 0)
526		bp2->bio_error = bp->bio_error;
527	bp2->bio_completed += bp->bio_completed;
528	g_destroy_bio(bp);
529	bp2->bio_inbed++;
530	if (bp2->bio_children == bp2->bio_inbed)
531		g_io_deliver(bp2, bp2->bio_error);
532}
533
534/* XXX: maybe this is only g_slice_spoiled */
535
536void
537g_std_spoiled(struct g_consumer *cp)
538{
539	struct g_geom *gp;
540	struct g_provider *pp;
541
542	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
543	g_topology_assert();
544	g_detach(cp);
545	gp = cp->geom;
546	LIST_FOREACH(pp, &gp->provider, provider)
547		g_orphan_provider(pp, ENXIO);
548	g_destroy_consumer(cp);
549	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
550		g_destroy_geom(gp);
551	else
552		gp->flags |= G_GEOM_WITHER;
553}
554
555/*
556 * Spoiling happens when a provider is opened for writing, but consumers
557 * which are configured by in-band data are attached (slicers for instance).
558 * Since the write might potentially change the in-band data, such consumers
559 * need to re-evaluate their existence after the writing session closes.
560 * We do this by (offering to) tear them down when the open for write happens
561 * in return for a re-taste when it closes again.
562 * Together with the fact that such consumers grab an 'e' bit whenever they
563 * are open, regardless of mode, this ends up DTRT.
564 */
565
566static void
567g_spoil_event(void *arg, int flag)
568{
569	struct g_provider *pp;
570	struct g_consumer *cp, *cp2;
571
572	g_topology_assert();
573	if (flag == EV_CANCEL)
574		return;
575	pp = arg;
576	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
577		cp2 = LIST_NEXT(cp, consumers);
578		if (!cp->spoiled)
579			continue;
580		cp->spoiled = 0;
581		if (cp->geom->spoiled == NULL)
582			continue;
583		cp->geom->spoiled(cp);
584		g_topology_assert();
585	}
586}
587
588void
589g_spoil(struct g_provider *pp, struct g_consumer *cp)
590{
591	struct g_consumer *cp2;
592
593	g_topology_assert();
594
595	if (!strcmp(pp->name, "geom.ctl"))
596		return;
597	LIST_FOREACH(cp2, &pp->consumers, consumers) {
598		if (cp2 == cp)
599			continue;
600/*
601		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
602		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
603*/
604		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
605		cp2->spoiled++;
606	}
607	g_call_me(g_spoil_event, pp, pp, NULL);
608}
609
610int
611g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
612{
613	int error, i;
614
615	i = len;
616	error = g_io_getattr(attr, cp, &i, var);
617	if (error)
618		return (error);
619	if (i != len)
620		return (EINVAL);
621	return (0);
622}
623
624/*
625 * Check if the given pointer is a live object
626 */
627
628void
629g_sanity(void *ptr)
630{
631	struct g_class *mp;
632	struct g_geom *gp;
633	struct g_consumer *cp;
634	struct g_provider *pp;
635
636	if (!(g_debugflags & 0x8))
637		return;
638	LIST_FOREACH(mp, &g_classes, class) {
639		KASSERT(mp != ptr, ("Ptr is live class"));
640		KASSERT(mp->protect == 0x20016600,
641		    ("corrupt class %p %x", mp, mp->protect));
642		LIST_FOREACH(gp, &mp->geom, geom) {
643			KASSERT(gp != ptr, ("Ptr is live geom"));
644			KASSERT(gp->protect == 0x20016601,
645			    ("corrupt geom, %p %x", gp, gp->protect));
646			KASSERT(gp->name != ptr, ("Ptr is live geom's name"));
647			LIST_FOREACH(cp, &gp->consumer, consumer) {
648				KASSERT(cp != ptr, ("Ptr is live consumer"));
649				KASSERT(cp->protect == 0x20016602,
650				    ("corrupt consumer %p %x",
651				    cp, cp->protect));
652			}
653			LIST_FOREACH(pp, &gp->provider, provider) {
654				KASSERT(pp != ptr, ("Ptr is live provider"));
655				KASSERT(pp->protect == 0x20016603,
656				    ("corrupt provider %p %x",
657				    pp, pp->protect));
658			}
659		}
660	}
661}
662
663struct g_class *
664g_idclass(struct geomidorname *p)
665{
666	struct g_class *mp;
667	char *n;
668
669	if (p->len == 0) {
670		LIST_FOREACH(mp, &g_classes, class)
671			if ((uintptr_t)mp == p->u.id)
672				return (mp);
673		return (NULL);
674	}
675	n = g_malloc(p->len + 1, M_WAITOK);
676	if (copyin(p->u.name, n, p->len) == 0) {
677		n[p->len] = '\0';
678		LIST_FOREACH(mp, &g_classes, class)
679			if (!bcmp(n, mp->name, p->len + 1)) {
680				g_free(n);
681				return (mp);
682			}
683	}
684	g_free(n);
685	return (NULL);
686}
687
688struct g_geom *
689g_idgeom(struct geomidorname *p)
690{
691	struct g_class *mp;
692	struct g_geom *gp;
693	char *n;
694
695	if (p->len == 0) {
696		LIST_FOREACH(mp, &g_classes, class)
697			LIST_FOREACH(gp, &mp->geom, geom)
698				if ((uintptr_t)gp == p->u.id)
699					return (gp);
700		return (NULL);
701	}
702	n = g_malloc(p->len + 1, M_WAITOK);
703	if (copyin(p->u.name, n, p->len) == 0) {
704		n[p->len] = '\0';
705		LIST_FOREACH(mp, &g_classes, class)
706			LIST_FOREACH(gp, &mp->geom, geom)
707				if (!bcmp(n, gp->name, p->len + 1)) {
708					g_free(n);
709					return (gp);
710				}
711	}
712	g_free(n);
713	return (NULL);
714}
715
716struct g_provider *
717g_idprovider(struct geomidorname *p)
718{
719	struct g_class *mp;
720	struct g_geom *gp;
721	struct g_provider *pp;
722	char *n;
723
724	if (p->len == 0) {
725		LIST_FOREACH(mp, &g_classes, class)
726			LIST_FOREACH(gp, &mp->geom, geom)
727				LIST_FOREACH(pp, &gp->provider, provider)
728					if ((uintptr_t)pp == p->u.id)
729						return (pp);
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				LIST_FOREACH(pp, &gp->provider, provider)
738					if (!bcmp(n, pp->name, p->len + 1)) {
739						g_free(n);
740						return (pp);
741					}
742	}
743	g_free(n);
744	return (NULL);
745}
746