geom_subr.c revision 112518
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 112518 2003-03-23 23:01:40Z 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, 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	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
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(NULL, NULL, NULL, 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, NULL, NULL, 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(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
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	/*
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
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
476
477int
478g_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
497int
498g_std_access(struct g_provider *pp __unused,
499	int dr __unused, int dw __unused, int de __unused)
500{
501
502        return (0);
503}
504
505void
506g_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
522void
523g_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
552void
553g_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
574static struct g_class *
575g_class_by_name(const char *name)
576{
577	struct g_class *mp;
578
579	g_trace(G_T_TOPOLOGY, "g_class_by_name(%s)", name);
580	g_topology_assert();
581	LIST_FOREACH(mp, &g_classes, class)
582		if (!strcmp(mp->name, name))
583			return (mp);
584	return (NULL);
585}
586
587struct g_geom *
588g_insert_geom(const char *class, struct g_consumer *cp)
589{
590	struct g_class *mp;
591	struct g_geom *gp;
592	struct g_provider *pp, *pp2;
593	struct g_consumer *cp2;
594	int error;
595
596	g_trace(G_T_TOPOLOGY, "g_insert_geomf(%s, %p)", class, cp);
597	g_topology_assert();
598	KASSERT(cp->provider != NULL, ("g_insert_geomf but not attached"));
599	/* XXX: check for events ?? */
600	mp = g_class_by_name(class);
601	if (mp == NULL)
602		return (NULL);
603	if (mp->config == NULL)
604		return (NULL);
605	pp = cp->provider;
606	gp = mp->taste(mp, pp, G_TF_TRANSPARENT);
607	if (gp == NULL)
608		return (NULL);
609	pp2 = LIST_FIRST(&gp->provider);
610	cp2 = LIST_FIRST(&gp->consumer);
611	cp2->acr += pp->acr;
612	cp2->acw += pp->acw;
613	cp2->ace += pp->ace;
614	pp2->acr += pp->acr;
615	pp2->acw += pp->acw;
616	pp2->ace += pp->ace;
617	LIST_REMOVE(cp, consumers);
618	LIST_INSERT_HEAD(&pp2->consumers, cp, consumers);
619	cp->provider = pp2;
620	error = redo_rank(gp);
621	KASSERT(error == 0, ("redo_rank failed in g_insert_geom"));
622	return (gp);
623}
624
625int
626g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
627{
628	int error, i;
629
630	i = len;
631	error = g_io_getattr(attr, cp, &i, var);
632	if (error)
633		return (error);
634	if (i != len)
635		return (EINVAL);
636	return (0);
637}
638
639/*
640 * Check if the given pointer is a live object
641 */
642
643void
644g_sanity(void *ptr)
645{
646	struct g_class *mp;
647	struct g_geom *gp;
648	struct g_consumer *cp;
649	struct g_provider *pp;
650
651	if (!(g_debugflags & 0x8))
652		return;
653	LIST_FOREACH(mp, &g_classes, class) {
654		KASSERT(mp != ptr, ("Ptr is live class"));
655		KASSERT(mp->protect == 0x20016600,
656		    ("corrupt class %p %x", mp, mp->protect));
657		LIST_FOREACH(gp, &mp->geom, geom) {
658			KASSERT(gp != ptr, ("Ptr is live geom"));
659			KASSERT(gp->protect == 0x20016601,
660			    ("corrupt geom, %p %x", gp, gp->protect));
661			KASSERT(gp->name != ptr, ("Ptr is live geom's name"));
662			LIST_FOREACH(cp, &gp->consumer, consumer) {
663				KASSERT(cp != ptr, ("Ptr is live consumer"));
664				KASSERT(cp->protect == 0x20016602,
665				    ("corrupt consumer %p %x",
666				    cp, cp->protect));
667			}
668			LIST_FOREACH(pp, &gp->provider, provider) {
669				KASSERT(pp != ptr, ("Ptr is live provider"));
670				KASSERT(pp->protect == 0x20016603,
671				    ("corrupt provider %p %x",
672				    pp, pp->protect));
673			}
674		}
675	}
676}
677
678#ifdef _KERNEL
679struct g_class *
680g_idclass(struct geomidorname *p)
681{
682	struct g_class *mp;
683	char *n;
684
685	if (p->len == 0) {
686		LIST_FOREACH(mp, &g_classes, class)
687			if ((uintptr_t)mp == p->u.id)
688				return (mp);
689		return (NULL);
690	}
691	n = g_malloc(p->len + 1, M_WAITOK);
692	if (copyin(p->u.name, n, p->len) == 0) {
693		n[p->len] = '\0';
694		LIST_FOREACH(mp, &g_classes, class)
695			if (!bcmp(n, mp->name, p->len + 1)) {
696				g_free(n);
697				return (mp);
698			}
699	}
700	g_free(n);
701	return (NULL);
702}
703
704struct g_geom *
705g_idgeom(struct geomidorname *p)
706{
707	struct g_class *mp;
708	struct g_geom *gp;
709	char *n;
710
711	if (p->len == 0) {
712		LIST_FOREACH(mp, &g_classes, class)
713			LIST_FOREACH(gp, &mp->geom, geom)
714				if ((uintptr_t)gp == p->u.id)
715					return (gp);
716		return (NULL);
717	}
718	n = g_malloc(p->len + 1, M_WAITOK);
719	if (copyin(p->u.name, n, p->len) == 0) {
720		n[p->len] = '\0';
721		LIST_FOREACH(mp, &g_classes, class)
722			LIST_FOREACH(gp, &mp->geom, geom)
723				if (!bcmp(n, gp->name, p->len + 1)) {
724					g_free(n);
725					return (gp);
726				}
727	}
728	g_free(n);
729	return (NULL);
730}
731
732struct g_provider *
733g_idprovider(struct geomidorname *p)
734{
735	struct g_class *mp;
736	struct g_geom *gp;
737	struct g_provider *pp;
738	char *n;
739
740	if (p->len == 0) {
741		LIST_FOREACH(mp, &g_classes, class)
742			LIST_FOREACH(gp, &mp->geom, geom)
743				LIST_FOREACH(pp, &gp->provider, provider)
744					if ((uintptr_t)pp == p->u.id)
745						return (pp);
746		return (NULL);
747	}
748	n = g_malloc(p->len + 1, M_WAITOK);
749	if (copyin(p->u.name, n, p->len) == 0) {
750		n[p->len] = '\0';
751		LIST_FOREACH(mp, &g_classes, class)
752			LIST_FOREACH(gp, &mp->geom, geom)
753				LIST_FOREACH(pp, &gp->provider, provider)
754					if (!bcmp(n, pp->name, p->len + 1)) {
755						g_free(n);
756						return (pp);
757					}
758	}
759	g_free(n);
760	return (NULL);
761}
762#endif /* _KERNEL */
763