geom_subr.c revision 108295
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 108295 2002-12-26 20:46:30Z phk $
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
65struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
66static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
67static int g_nproviders;
68char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
69
70static int g_ignition;
71
72void
73g_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
90struct g_geom *
91g_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
116void
117g_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	LIST_REMOVE(gp, geom);
130	TAILQ_REMOVE(&geoms, gp, geoms);
131	g_free(gp->name);
132	g_free(gp);
133}
134
135struct g_consumer *
136g_new_consumer(struct g_geom *gp)
137{
138	struct g_consumer *cp;
139
140	g_topology_assert();
141	KASSERT(gp->orphan != NULL,
142	    ("g_new_consumer on geom(%s) (class %s) without orphan",
143	    gp->name, gp->class->name));
144
145	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
146	cp->protect = 0x020016602;
147	cp->geom = gp;
148	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
149	return(cp);
150}
151
152void
153g_destroy_consumer(struct g_consumer *cp)
154{
155
156	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
157	g_topology_assert();
158	KASSERT(cp->event == NULL, ("g_destroy_consumer() with event"));
159	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
160	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
161	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
162	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
163	LIST_REMOVE(cp, consumer);
164	g_free(cp);
165}
166
167struct g_provider *
168g_new_providerf(struct g_geom *gp, const char *fmt, ...)
169{
170	struct g_provider *pp;
171	struct sbuf *sb;
172	va_list ap;
173
174	g_topology_assert();
175	va_start(ap, fmt);
176	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
177	sbuf_vprintf(sb, fmt, ap);
178	sbuf_finish(sb);
179	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
180	pp->protect = 0x020016603;
181	pp->name = (char *)(pp + 1);
182	strcpy(pp->name, sbuf_data(sb));
183	sbuf_delete(sb);
184	LIST_INIT(&pp->consumers);
185	pp->error = ENXIO;
186	pp->geom = gp;
187	LIST_INSERT_HEAD(&gp->provider, pp, provider);
188	g_nproviders++;
189	g_post_event(EV_NEW_PROVIDER, NULL, NULL, pp, NULL);
190	return (pp);
191}
192
193void
194g_error_provider(struct g_provider *pp, int error)
195{
196
197	pp->error = error;
198}
199
200
201void
202g_destroy_provider(struct g_provider *pp)
203{
204	struct g_geom *gp;
205	struct g_consumer *cp;
206
207	g_topology_assert();
208	KASSERT(pp->event == NULL, ("g_destroy_provider() with event"));
209	KASSERT(LIST_EMPTY(&pp->consumers),
210	    ("g_destroy_provider but attached"));
211	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
212	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
213	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
214	g_nproviders--;
215	LIST_REMOVE(pp, provider);
216	gp = pp->geom;
217	g_free(pp);
218	if (!(gp->flags & G_GEOM_WITHER))
219		return;
220	if (!LIST_EMPTY(&gp->provider))
221		return;
222	for (;;) {
223		cp = LIST_FIRST(&gp->consumer);
224		if (cp == NULL)
225			break;
226		g_detach(cp);
227		g_destroy_consumer(cp);
228	}
229	g_destroy_geom(gp);
230}
231
232/*
233 * We keep the "geoms" list sorted by topological order (== increasing
234 * numerical rank) at all times.
235 * When an attach is done, the attaching geoms rank is invalidated
236 * and it is moved to the tail of the list.
237 * All geoms later in the sequence has their ranks reevaluated in
238 * sequence.  If we cannot assign rank to a geom because it's
239 * prerequisites do not have rank, we move that element to the tail
240 * of the sequence with invalid rank as well.
241 * At some point we encounter our original geom and if we stil fail
242 * to assign it a rank, there must be a loop and we fail back to
243 * g_attach() which detach again and calls redo_rank again
244 * to fix up the damage.
245 * It would be much simpler code wise to do it recursively, but we
246 * can't risk that on the kernel stack.
247 */
248
249static int
250redo_rank(struct g_geom *gp)
251{
252	struct g_consumer *cp;
253	struct g_geom *gp1, *gp2;
254	int n, m;
255
256	g_topology_assert();
257
258	/* Invalidate this geoms rank and move it to the tail */
259	gp1 = TAILQ_NEXT(gp, geoms);
260	if (gp1 != NULL) {
261		gp->rank = 0;
262		TAILQ_REMOVE(&geoms, gp, geoms);
263		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
264	} else {
265		gp1 = gp;
266	}
267
268	/* re-rank the rest of the sequence */
269	for (; gp1 != NULL; gp1 = gp2) {
270		gp1->rank = 0;
271		m = 1;
272		LIST_FOREACH(cp, &gp1->consumer, consumer) {
273			if (cp->provider == NULL)
274				continue;
275			n = cp->provider->geom->rank;
276			if (n == 0) {
277				m = 0;
278				break;
279			} else if (n >= m)
280				m = n + 1;
281		}
282		gp1->rank = m;
283		gp2 = TAILQ_NEXT(gp1, geoms);
284
285		/* got a rank, moving on */
286		if (m != 0)
287			continue;
288
289		/* no rank to original geom means loop */
290		if (gp == gp1)
291			return (ELOOP);
292
293		/* no rank, put it at the end move on */
294		TAILQ_REMOVE(&geoms, gp1, geoms);
295		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
296	}
297	return (0);
298}
299
300int
301g_attach(struct g_consumer *cp, struct g_provider *pp)
302{
303	int error;
304
305	g_topology_assert();
306	KASSERT(cp->provider == NULL, ("attach but attached"));
307	cp->provider = pp;
308	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
309	error = redo_rank(cp->geom);
310	if (error) {
311		LIST_REMOVE(cp, consumers);
312		cp->provider = NULL;
313		redo_rank(cp->geom);
314	}
315	return (error);
316}
317
318void
319g_detach(struct g_consumer *cp)
320{
321	struct g_provider *pp;
322
323	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
324	KASSERT(cp != (void*)0xd0d0d0d0, ("ARGH!"));
325	g_topology_assert();
326	KASSERT(cp->provider != NULL, ("detach but not attached"));
327	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
328	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
329	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
330	KASSERT(cp->biocount == 0, ("detach but nonzero biocount"));
331	pp = cp->provider;
332	LIST_REMOVE(cp, consumers);
333	cp->provider = NULL;
334	if (LIST_EMPTY(&pp->consumers)) {
335		if (pp->geom->flags & G_GEOM_WITHER)
336			g_destroy_provider(pp);
337	}
338	redo_rank(cp->geom);
339}
340
341
342/*
343 * g_access_abs()
344 *
345 * Access-check with absolute new values:  Just fall through
346 * and use the relative version.
347 */
348int
349g_access_abs(struct g_consumer *cp, int acr, int acw, int ace)
350{
351
352	g_topology_assert();
353	return(g_access_rel(cp,
354		acr - cp->acr,
355		acw - cp->acw,
356		ace - cp->ace));
357}
358
359/*
360 * g_access_rel()
361 *
362 * Access-check with delta values.  The question asked is "can provider
363 * "cp" change the access counters by the relative amounts dc[rwe] ?"
364 */
365
366int
367g_access_rel(struct g_consumer *cp, int dcr, int dcw, int dce)
368{
369	struct g_provider *pp;
370	int pr,pw,pe;
371	int error;
372
373	pp = cp->provider;
374
375	g_trace(G_T_ACCESS, "g_access_rel(%p(%s), %d, %d, %d)",
376	    cp, pp->name, dcr, dcw, dce);
377
378	g_topology_assert();
379	KASSERT(cp->provider != NULL, ("access but not attached"));
380	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
381	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
382	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
383	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
384
385	/*
386	 * If our class cares about being spoiled, and we have been, we
387	 * are probably just ahead of the event telling us that.  Fail
388	 * now rather than having to unravel this later.
389	 */
390	if (cp->geom->spoiled != NULL && cp->spoiled) {
391		KASSERT(dcr >= 0, ("spoiled but dcr = %d", dcr));
392		KASSERT(dcw >= 0, ("spoiled but dce = %d", dcw));
393		KASSERT(dce >= 0, ("spoiled but dcw = %d", dce));
394		KASSERT(cp->acr == 0, ("spoiled but cp->acr = %d", cp->acr));
395		KASSERT(cp->acw == 0, ("spoiled but cp->acw = %d", cp->acw));
396		KASSERT(cp->ace == 0, ("spoiled but cp->ace = %d", cp->ace));
397		return(ENXIO);
398	}
399
400	/*
401	 * Figure out what counts the provider would have had, if this
402	 * consumer had (r0w0e0) at this time.
403	 */
404	pr = pp->acr - cp->acr;
405	pw = pp->acw - cp->acw;
406	pe = pp->ace - cp->ace;
407
408	g_trace(G_T_ACCESS,
409    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
410	    dcr, dcw, dce,
411	    cp->acr, cp->acw, cp->ace,
412	    pp->acr, pp->acw, pp->ace,
413	    pp, pp->name);
414
415	/* If we try exclusive but already write: fail */
416	if (dce > 0 && pw > 0)
417		return (EPERM);
418	/* If we try write but already exclusive: fail */
419	if (dcw > 0 && pe > 0)
420		return (EPERM);
421	/* If we try to open more but provider is error'ed: fail */
422	if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
423		return (pp->error);
424
425	/* Ok then... */
426
427	/*
428	 * If we open first write, spoil any partner consumers.
429	 * If we close last write, trigger re-taste.
430	 */
431	if (pp->acw == 0 && dcw != 0)
432		g_spoil(pp, cp);
433	else if (pp->acw != 0 && pp->acw == -dcw &&
434	    !(pp->geom->flags & G_GEOM_WITHER))
435		g_post_event(EV_NEW_PROVIDER, NULL, NULL, pp, NULL);
436
437	error = pp->geom->access(pp, dcr, dcw, dce);
438	if (!error) {
439		pp->acr += dcr;
440		pp->acw += dcw;
441		pp->ace += dce;
442		cp->acr += dcr;
443		cp->acw += dcw;
444		cp->ace += dce;
445	}
446	return (error);
447}
448
449int
450g_handleattr_int(struct bio *bp, const char *attribute, int val)
451{
452
453	return (g_handleattr(bp, attribute, &val, sizeof val));
454}
455
456int
457g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
458{
459
460	return (g_handleattr(bp, attribute, &val, sizeof val));
461}
462
463
464int
465g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
466{
467	int error;
468
469	if (strcmp(bp->bio_attribute, attribute))
470		return (0);
471	if (bp->bio_length != len) {
472		printf("bio_length %jd len %d -> EFAULT\n",
473		    (intmax_t)bp->bio_length, len);
474		error = EFAULT;
475	} else {
476		error = 0;
477		bcopy(val, bp->bio_data, len);
478		bp->bio_completed = len;
479	}
480	g_io_deliver(bp, error);
481	return (1);
482}
483
484int
485g_std_access(struct g_provider *pp __unused,
486	int dr __unused, int dw __unused, int de __unused)
487{
488
489        return (0);
490}
491
492void
493g_std_done(struct bio *bp)
494{
495	struct bio *bp2;
496
497	bp2 = bp->bio_linkage;
498	if (bp2->bio_error == 0)
499		bp2->bio_error = bp->bio_error;
500	bp2->bio_completed += bp->bio_completed;
501	g_destroy_bio(bp);
502	bp2->bio_children--;	/* XXX: atomic ? */
503	if (bp2->bio_children == 0)
504		g_io_deliver(bp2, bp2->bio_error);
505}
506
507/* XXX: maybe this is only g_slice_spoiled */
508
509void
510g_std_spoiled(struct g_consumer *cp)
511{
512	struct g_geom *gp;
513	struct g_provider *pp;
514
515	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
516	g_topology_assert();
517	g_detach(cp);
518	gp = cp->geom;
519	LIST_FOREACH(pp, &gp->provider, provider)
520		g_orphan_provider(pp, ENXIO);
521	g_destroy_consumer(cp);
522	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
523		g_destroy_geom(gp);
524	else
525		gp->flags |= G_GEOM_WITHER;
526}
527
528/*
529 * Spoiling happens when a provider is opened for writing, but consumers
530 * which are configured by in-band data are attached (slicers for instance).
531 * Since the write might potentially change the in-band data, such consumers
532 * need to re-evaluate their existence after the writing session closes.
533 * We do this by (offering to) tear them down when the open for write happens
534 * in return for a re-taste when it closes again.
535 * Together with the fact that such consumers grab an 'e' bit whenever they
536 * are open, regardless of mode, this ends up DTRT.
537 */
538
539void
540g_spoil(struct g_provider *pp, struct g_consumer *cp)
541{
542	struct g_consumer *cp2;
543
544	g_topology_assert();
545
546	if (!strcmp(pp->name, "geom.ctl"))
547		return;
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(const 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(const 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->config == 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
665#ifdef _KERNEL
666struct g_class *
667g_idclass(struct geomidorname *p)
668{
669	struct g_class *mp;
670	char *n;
671
672	if (p->len == 0) {
673		LIST_FOREACH(mp, &g_classes, class)
674			if ((uintptr_t)mp == p->u.id)
675				return (mp);
676		return (NULL);
677	}
678	n = g_malloc(p->len + 1, M_WAITOK);
679	if (copyin(p->u.name, n, p->len) == 0) {
680		n[p->len] = '\0';
681		LIST_FOREACH(mp, &g_classes, class)
682			if (!bcmp(n, mp->name, p->len + 1)) {
683				g_free(n);
684				return (mp);
685			}
686	}
687	g_free(n);
688	return (NULL);
689}
690
691struct g_geom *
692g_idgeom(struct geomidorname *p)
693{
694	struct g_class *mp;
695	struct g_geom *gp;
696	char *n;
697
698	if (p->len == 0) {
699		LIST_FOREACH(mp, &g_classes, class)
700			LIST_FOREACH(gp, &mp->geom, geom)
701				if ((uintptr_t)gp == p->u.id)
702					return (gp);
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			LIST_FOREACH(gp, &mp->geom, geom)
710				if (!bcmp(n, gp->name, p->len + 1)) {
711					g_free(n);
712					return (gp);
713				}
714	}
715	g_free(n);
716	return (NULL);
717}
718
719struct g_provider *
720g_idprovider(struct geomidorname *p)
721{
722	struct g_class *mp;
723	struct g_geom *gp;
724	struct g_provider *pp;
725	char *n;
726
727	if (p->len == 0) {
728		LIST_FOREACH(mp, &g_classes, class)
729			LIST_FOREACH(gp, &mp->geom, geom)
730				LIST_FOREACH(pp, &gp->provider, provider)
731					if ((uintptr_t)pp == p->u.id)
732						return (pp);
733		return (NULL);
734	}
735	n = g_malloc(p->len + 1, M_WAITOK);
736	if (copyin(p->u.name, n, p->len) == 0) {
737		n[p->len] = '\0';
738		LIST_FOREACH(mp, &g_classes, class)
739			LIST_FOREACH(gp, &mp->geom, geom)
740				LIST_FOREACH(pp, &gp->provider, provider)
741					if (!bcmp(n, pp->name, p->len + 1)) {
742						g_free(n);
743						return (pp);
744					}
745	}
746	g_free(n);
747	return (NULL);
748}
749#endif /* _KERNEL */
750