geom_subr.c revision 112988
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 112988 2003-04-02 20:41:18Z phk $
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
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);
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	g_cancel_event(gp);
130	LIST_REMOVE(gp, geom);
131	TAILQ_REMOVE(&geoms, gp, geoms);
132	g_free(gp->name);
133	g_free(gp);
134}
135
136struct g_consumer *
137g_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
155void
156g_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(cp);
167	LIST_REMOVE(cp, consumer);
168	devstat_remove_entry(cp->stat);
169	g_free(cp);
170}
171
172struct g_provider *
173g_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, pp, NULL);
197	return (pp);
198}
199
200void
201g_error_provider(struct g_provider *pp, int error)
202{
203
204	pp->error = error;
205}
206
207
208void
209g_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(pp);
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
258static int
259redo_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
309int
310g_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
327void
328g_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 */
358int
359g_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
376int
377g_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	error = pp->geom->access(pp, dcr, dcw, dce);
441	if (!error) {
442		/*
443		 * If we open first write, spoil any partner consumers.
444		 * If we close last write, trigger re-taste.
445		 */
446		if (pp->acw == 0 && dcw != 0)
447			g_spoil(pp, cp);
448		else if (pp->acw != 0 && pp->acw == -dcw &&
449		    !(pp->geom->flags & G_GEOM_WITHER))
450			g_post_event(EV_NEW_PROVIDER, pp, NULL);
451
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
462int
463g_handleattr_int(struct bio *bp, const char *attribute, int val)
464{
465
466	return (g_handleattr(bp, attribute, &val, sizeof val));
467}
468
469int
470g_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
476int
477g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
478{
479	int error;
480
481	if (strcmp(bp->bio_attribute, attribute))
482		return (0);
483	if (bp->bio_length != len) {
484		printf("bio_length %jd len %d -> EFAULT\n",
485		    (intmax_t)bp->bio_length, len);
486		error = EFAULT;
487	} else {
488		error = 0;
489		bcopy(val, bp->bio_data, len);
490		bp->bio_completed = len;
491	}
492	g_io_deliver(bp, error);
493	return (1);
494}
495
496int
497g_std_access(struct g_provider *pp __unused,
498	int dr __unused, int dw __unused, int de __unused)
499{
500
501        return (0);
502}
503
504void
505g_std_done(struct bio *bp)
506{
507	struct bio *bp2;
508
509	bp2 = bp->bio_parent;
510	if (bp2->bio_error == 0)
511		bp2->bio_error = bp->bio_error;
512	bp2->bio_completed += bp->bio_completed;
513	g_destroy_bio(bp);
514	bp2->bio_inbed++;
515	if (bp2->bio_children == bp2->bio_inbed)
516		g_io_deliver(bp2, bp2->bio_error);
517}
518
519/* XXX: maybe this is only g_slice_spoiled */
520
521void
522g_std_spoiled(struct g_consumer *cp)
523{
524	struct g_geom *gp;
525	struct g_provider *pp;
526
527	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
528	g_topology_assert();
529	g_detach(cp);
530	gp = cp->geom;
531	LIST_FOREACH(pp, &gp->provider, provider)
532		g_orphan_provider(pp, ENXIO);
533	g_destroy_consumer(cp);
534	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
535		g_destroy_geom(gp);
536	else
537		gp->flags |= G_GEOM_WITHER;
538}
539
540/*
541 * Spoiling happens when a provider is opened for writing, but consumers
542 * which are configured by in-band data are attached (slicers for instance).
543 * Since the write might potentially change the in-band data, such consumers
544 * need to re-evaluate their existence after the writing session closes.
545 * We do this by (offering to) tear them down when the open for write happens
546 * in return for a re-taste when it closes again.
547 * Together with the fact that such consumers grab an 'e' bit whenever they
548 * are open, regardless of mode, this ends up DTRT.
549 */
550
551void
552g_spoil(struct g_provider *pp, struct g_consumer *cp)
553{
554	struct g_consumer *cp2;
555
556	g_topology_assert();
557
558	if (!strcmp(pp->name, "geom.ctl"))
559		return;
560	LIST_FOREACH(cp2, &pp->consumers, consumers) {
561		if (cp2 == cp)
562			continue;
563/*
564		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
565		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
566*/
567		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
568		cp2->spoiled++;
569	}
570	g_post_event(EV_SPOILED, pp, cp, NULL);
571}
572
573int
574g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
575{
576	int error, i;
577
578	i = len;
579	error = g_io_getattr(attr, cp, &i, var);
580	if (error)
581		return (error);
582	if (i != len)
583		return (EINVAL);
584	return (0);
585}
586
587/*
588 * Check if the given pointer is a live object
589 */
590
591void
592g_sanity(void *ptr)
593{
594	struct g_class *mp;
595	struct g_geom *gp;
596	struct g_consumer *cp;
597	struct g_provider *pp;
598
599	if (!(g_debugflags & 0x8))
600		return;
601	LIST_FOREACH(mp, &g_classes, class) {
602		KASSERT(mp != ptr, ("Ptr is live class"));
603		KASSERT(mp->protect == 0x20016600,
604		    ("corrupt class %p %x", mp, mp->protect));
605		LIST_FOREACH(gp, &mp->geom, geom) {
606			KASSERT(gp != ptr, ("Ptr is live geom"));
607			KASSERT(gp->protect == 0x20016601,
608			    ("corrupt geom, %p %x", gp, gp->protect));
609			KASSERT(gp->name != ptr, ("Ptr is live geom's name"));
610			LIST_FOREACH(cp, &gp->consumer, consumer) {
611				KASSERT(cp != ptr, ("Ptr is live consumer"));
612				KASSERT(cp->protect == 0x20016602,
613				    ("corrupt consumer %p %x",
614				    cp, cp->protect));
615			}
616			LIST_FOREACH(pp, &gp->provider, provider) {
617				KASSERT(pp != ptr, ("Ptr is live provider"));
618				KASSERT(pp->protect == 0x20016603,
619				    ("corrupt provider %p %x",
620				    pp, pp->protect));
621			}
622		}
623	}
624}
625
626#ifdef _KERNEL
627struct g_class *
628g_idclass(struct geomidorname *p)
629{
630	struct g_class *mp;
631	char *n;
632
633	if (p->len == 0) {
634		LIST_FOREACH(mp, &g_classes, class)
635			if ((uintptr_t)mp == p->u.id)
636				return (mp);
637		return (NULL);
638	}
639	n = g_malloc(p->len + 1, M_WAITOK);
640	if (copyin(p->u.name, n, p->len) == 0) {
641		n[p->len] = '\0';
642		LIST_FOREACH(mp, &g_classes, class)
643			if (!bcmp(n, mp->name, p->len + 1)) {
644				g_free(n);
645				return (mp);
646			}
647	}
648	g_free(n);
649	return (NULL);
650}
651
652struct g_geom *
653g_idgeom(struct geomidorname *p)
654{
655	struct g_class *mp;
656	struct g_geom *gp;
657	char *n;
658
659	if (p->len == 0) {
660		LIST_FOREACH(mp, &g_classes, class)
661			LIST_FOREACH(gp, &mp->geom, geom)
662				if ((uintptr_t)gp == p->u.id)
663					return (gp);
664		return (NULL);
665	}
666	n = g_malloc(p->len + 1, M_WAITOK);
667	if (copyin(p->u.name, n, p->len) == 0) {
668		n[p->len] = '\0';
669		LIST_FOREACH(mp, &g_classes, class)
670			LIST_FOREACH(gp, &mp->geom, geom)
671				if (!bcmp(n, gp->name, p->len + 1)) {
672					g_free(n);
673					return (gp);
674				}
675	}
676	g_free(n);
677	return (NULL);
678}
679
680struct g_provider *
681g_idprovider(struct geomidorname *p)
682{
683	struct g_class *mp;
684	struct g_geom *gp;
685	struct g_provider *pp;
686	char *n;
687
688	if (p->len == 0) {
689		LIST_FOREACH(mp, &g_classes, class)
690			LIST_FOREACH(gp, &mp->geom, geom)
691				LIST_FOREACH(pp, &gp->provider, provider)
692					if ((uintptr_t)pp == p->u.id)
693						return (pp);
694		return (NULL);
695	}
696	n = g_malloc(p->len + 1, M_WAITOK);
697	if (copyin(p->u.name, n, p->len) == 0) {
698		n[p->len] = '\0';
699		LIST_FOREACH(mp, &g_classes, class)
700			LIST_FOREACH(gp, &mp->geom, geom)
701				LIST_FOREACH(pp, &gp->provider, provider)
702					if (!bcmp(n, pp->name, p->len + 1)) {
703						g_free(n);
704						return (pp);
705					}
706	}
707	g_free(n);
708	return (NULL);
709}
710#endif /* _KERNEL */
711