geom_subr.c revision 97887
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 97887 2002-06-05 20:30:36Z 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/kernel.h>
50#include <sys/malloc.h>
51#include <sys/bio.h>
52#include <sys/sysctl.h>
53#include <sys/proc.h>
54#include <sys/kthread.h>
55#include <sys/lock.h>
56#include <sys/mutex.h>
57#endif
58#include <sys/errno.h>
59#include <sys/sbuf.h>
60#include <geom/geom.h>
61#include <geom/geom_int.h>
62#include <machine/stdarg.h>
63
64struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
65static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
66static int g_nproviders;
67char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
68
69static int g_ignition;
70
71void
72g_add_class(struct g_class *mp)
73{
74
75	if (!g_ignition) {
76		g_ignition++;
77		g_init();
78	}
79	mp->protect = 0x020016600;
80	g_topology_lock();
81	g_trace(G_T_TOPOLOGY, "g_add_class(%s)", mp->name);
82	LIST_INIT(&mp->geom);
83	LIST_INSERT_HEAD(&g_classes, mp, class);
84	if (g_nproviders > 0)
85		g_post_event(EV_NEW_CLASS, mp, NULL, NULL, NULL);
86	g_topology_unlock();
87}
88
89struct g_geom *
90g_new_geomf(struct g_class *mp, char *fmt, ...)
91{
92	struct g_geom *gp;
93	va_list ap;
94	struct sbuf *sb;
95
96	g_topology_assert();
97	va_start(ap, fmt);
98	mtx_lock(&Giant);
99	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
100	sbuf_vprintf(sb, fmt, ap);
101	sbuf_finish(sb);
102	mtx_unlock(&Giant);
103	gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
104	gp->protect = 0x020016601;
105	gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
106	gp->class = mp;
107	gp->rank = 1;
108	LIST_INIT(&gp->consumer);
109	LIST_INIT(&gp->provider);
110	LIST_INSERT_HEAD(&mp->geom, gp, geom);
111	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
112	strcpy(gp->name, sbuf_data(sb));
113	sbuf_delete(sb);
114	return (gp);
115}
116
117/*
118 * Add a magic space to a geom.  There is no locking here because nobody
119 * should be modifying these except the geom itself during configuration,
120 * so it cannot go away while we fiddling it.
121 * For now, no provision exists for removing magic spaces or for changing
122 * them on the fly.
123 */
124
125int
126g_add_magicspace(struct g_geom *gp, const char *name, off_t start, u_int len, u_int flags)
127{
128	int i;
129	struct g_magicspaces *msps;
130	struct g_magicspace *msp, *msp2;
131
132	if (gp->magicspaces == NULL) {
133		msps = g_malloc(sizeof *gp->magicspaces, M_WAITOK | M_ZERO);
134		msps->geom_id = (uintptr_t)gp;
135		strncpy(msps->class, gp->class->name, sizeof msps->class);
136		gp->magicspaces = msps;
137	}
138	msps = gp->magicspaces;
139	if (msps->nmagic >= msps->nspace) {
140		i = msps->nspace + 1;
141		msp = g_malloc(sizeof(*msp) * i, M_WAITOK | M_ZERO);
142		bcopy(msps->magicspace, msp, sizeof(*msp) * msps->nmagic);
143		msp2 = msps->magicspace;
144		msps->magicspace = msp;
145		if (msp2 != NULL)
146			g_free(msp2);
147		msps->nspace = i;
148	}
149	msp = &msps->magicspace[msps->nmagic++];
150	strncpy(msp->name, name, sizeof msp->name);
151	msp->offset = start;
152	msp->len = len;
153	msp->flags = flags;
154	return (0);
155}
156
157void
158g_destroy_geom(struct g_geom *gp)
159{
160
161	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
162	g_topology_assert();
163	KASSERT(gp->event == NULL, ("g_destroy_geom() with event"));
164	KASSERT(LIST_EMPTY(&gp->consumer),
165	    ("g_destroy_geom(%s) with consumer(s) [%p]",
166	    gp->name, LIST_FIRST(&gp->consumer)));
167	KASSERT(LIST_EMPTY(&gp->provider),
168	    ("g_destroy_geom(%s) with provider(s) [%p]",
169	    gp->name, LIST_FIRST(&gp->consumer)));
170	LIST_REMOVE(gp, geom);
171	TAILQ_REMOVE(&geoms, gp, geoms);
172	if (gp->magicspaces) {
173		g_free(gp->magicspaces->magicspace);
174		g_free(gp->magicspaces);
175	}
176	g_free(gp->name);
177	g_free(gp);
178}
179
180struct g_consumer *
181g_new_consumer(struct g_geom *gp)
182{
183	struct g_consumer *cp;
184
185	g_topology_assert();
186	KASSERT(gp->orphan != NULL,
187	    ("g_new_consumer on geom(%s) (class %s) without orphan",
188	    gp->name, gp->class->name));
189
190	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
191	cp->protect = 0x020016602;
192	cp->geom = gp;
193	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
194	return(cp);
195}
196
197void
198g_destroy_consumer(struct g_consumer *cp)
199{
200
201	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
202	g_topology_assert();
203	KASSERT(cp->event == NULL, ("g_destroy_consumer() with event"));
204	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
205	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
206	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
207	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
208	LIST_REMOVE(cp, consumer);
209	g_free(cp);
210}
211
212struct g_provider *
213g_new_providerf(struct g_geom *gp, char *fmt, ...)
214{
215	struct g_provider *pp;
216	struct sbuf *sb;
217	va_list ap;
218
219	g_topology_assert();
220	va_start(ap, fmt);
221	mtx_lock(&Giant);
222	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
223	sbuf_vprintf(sb, fmt, ap);
224	sbuf_finish(sb);
225	mtx_unlock(&Giant);
226	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
227	pp->protect = 0x020016603;
228	pp->name = (char *)(pp + 1);
229	strcpy(pp->name, sbuf_data(sb));
230	sbuf_delete(sb);
231	LIST_INIT(&pp->consumers);
232	pp->error = ENXIO;
233	pp->geom = gp;
234	LIST_INSERT_HEAD(&gp->provider, pp, provider);
235	g_nproviders++;
236	g_post_event(EV_NEW_PROVIDER, NULL, NULL, pp, NULL);
237	return (pp);
238}
239
240void
241g_error_provider(struct g_provider *pp, int error)
242{
243
244	pp->error = error;
245}
246
247
248void
249g_destroy_provider(struct g_provider *pp)
250{
251	struct g_geom *gp;
252	struct g_consumer *cp;
253
254	g_topology_assert();
255	KASSERT(pp->event == NULL, ("g_destroy_provider() with event"));
256	KASSERT(LIST_EMPTY(&pp->consumers),
257	    ("g_destroy_provider but attached"));
258	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
259	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
260	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
261	g_nproviders--;
262	LIST_REMOVE(pp, provider);
263	gp = pp->geom;
264	g_free(pp);
265	if (!(gp->flags & G_GEOM_WITHER))
266		return;
267	if (!LIST_EMPTY(&gp->provider))
268		return;
269	for (;;) {
270		cp = LIST_FIRST(&gp->consumer);
271		if (cp == NULL)
272			break;
273		g_dettach(cp);
274		g_destroy_consumer(cp);
275	}
276	g_destroy_geom(gp);
277}
278
279/*
280 * We keep the "geoms" list sorted by topological order (== increasing
281 * numerical rank) at all times.
282 * When an attach is done, the attaching geoms rank is invalidated
283 * and it is moved to the tail of the list.
284 * All geoms later in the sequence has their ranks reevaluated in
285 * sequence.  If we cannot assign rank to a geom because it's
286 * prerequisites do not have rank, we move that element to the tail
287 * of the sequence with invalid rank as well.
288 * At some point we encounter our original geom and if we stil fail
289 * to assign it a rank, there must be a loop and we fail back to
290 * g_attach() which dettach again and calls redo_rank again
291 * to fix up the damage.
292 * It would be much simpler code wise to do it recursively, but we
293 * can't risk that on the kernel stack.
294 */
295
296static int
297redo_rank(struct g_geom *gp)
298{
299	struct g_consumer *cp;
300	struct g_geom *gp1, *gp2;
301	int n, m;
302
303	g_topology_assert();
304
305	/* Invalidate this geoms rank and move it to the tail */
306	gp1 = TAILQ_NEXT(gp, geoms);
307	if (gp1 != NULL) {
308		gp->rank = 0;
309		TAILQ_REMOVE(&geoms, gp, geoms);
310		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
311	} else {
312		gp1 = gp;
313	}
314
315	/* re-rank the rest of the sequence */
316	for (; gp1 != NULL; gp1 = gp2) {
317		gp1->rank = 0;
318		m = 1;
319		LIST_FOREACH(cp, &gp1->consumer, consumer) {
320			if (cp->provider == NULL)
321				continue;
322			n = cp->provider->geom->rank;
323			if (n == 0) {
324				m = 0;
325				break;
326			} else if (n >= m)
327				m = n + 1;
328		}
329		gp1->rank = m;
330		gp2 = TAILQ_NEXT(gp1, geoms);
331
332		/* got a rank, moving on */
333		if (m != 0)
334			continue;
335
336		/* no rank to original geom means loop */
337		if (gp == gp1)
338			return (ELOOP);
339
340		/* no rank, put it at the end move on */
341		TAILQ_REMOVE(&geoms, gp1, geoms);
342		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
343	}
344	return (0);
345}
346
347int
348g_attach(struct g_consumer *cp, struct g_provider *pp)
349{
350	int error;
351
352	g_topology_assert();
353	KASSERT(cp->provider == NULL, ("attach but attached"));
354	cp->provider = pp;
355	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
356	error = redo_rank(cp->geom);
357	if (error) {
358		LIST_REMOVE(cp, consumers);
359		cp->provider = NULL;
360		redo_rank(cp->geom);
361	}
362	return (error);
363}
364
365void
366g_dettach(struct g_consumer *cp)
367{
368	struct g_provider *pp;
369
370	g_trace(G_T_TOPOLOGY, "g_dettach(%p)", cp);
371	KASSERT(cp != (void*)0xd0d0d0d0, ("ARGH!"));
372	g_topology_assert();
373	KASSERT(cp->provider != NULL, ("dettach but not attached"));
374	KASSERT(cp->acr == 0, ("dettach but nonzero acr"));
375	KASSERT(cp->acw == 0, ("dettach but nonzero acw"));
376	KASSERT(cp->ace == 0, ("dettach but nonzero ace"));
377	KASSERT(cp->biocount == 0, ("dettach but nonzero biocount"));
378	pp = cp->provider;
379	LIST_REMOVE(cp, consumers);
380	cp->provider = NULL;
381	if (LIST_EMPTY(&pp->consumers)) {
382		if (pp->geom->flags & G_GEOM_WITHER)
383			g_destroy_provider(pp);
384	}
385	redo_rank(cp->geom);
386}
387
388
389/*
390 * g_access_abs()
391 *
392 * Access-check with absolute new values:  Just fall through
393 * and use the relative version.
394 */
395int
396g_access_abs(struct g_consumer *cp, int acr, int acw, int ace)
397{
398
399	g_topology_assert();
400	return(g_access_rel(cp,
401		acr - cp->acr,
402		acw - cp->acw,
403		ace - cp->ace));
404}
405
406/*
407 * g_access_rel()
408 *
409 * Access-check with delta values.  The question asked is "can provider
410 * "cp" change the access counters by the relative amounts dc[rwe] ?"
411 */
412
413int
414g_access_rel(struct g_consumer *cp, int dcr, int dcw, int dce)
415{
416	struct g_provider *pp;
417	int pr,pw,pe;
418	int error;
419
420	pp = cp->provider;
421
422	g_trace(G_T_ACCESS, "g_access_rel(%p(%s), %d, %d, %d)",
423	    cp, pp->name, dcr, dcw, dce);
424
425	g_topology_assert();
426	KASSERT(cp->provider != NULL, ("access but not attached"));
427	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
428	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
429	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
430	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
431
432	/*
433	 * If our class cares about being spoiled, and we have been, we
434	 * are probably just ahead of the event telling us that.  Fail
435	 * now rather than having to unravel this later.
436	 */
437	if (cp->geom->spoiled != NULL && cp->spoiled) {
438		KASSERT(dcr >= 0, ("spoiled but dcr = %d", dcr));
439		KASSERT(dcw >= 0, ("spoiled but dce = %d", dcw));
440		KASSERT(dce >= 0, ("spoiled but dcw = %d", dce));
441		KASSERT(cp->acr == 0, ("spoiled but cp->acr = %d", cp->acr));
442		KASSERT(cp->acw == 0, ("spoiled but cp->acw = %d", cp->acw));
443		KASSERT(cp->ace == 0, ("spoiled but cp->ace = %d", cp->ace));
444		return(ENXIO);
445	}
446
447	/*
448	 * Figure out what counts the provider would have had, if this
449	 * consumer had (r0w0e0) at this time.
450	 */
451	pr = pp->acr - cp->acr;
452	pw = pp->acw - cp->acw;
453	pe = pp->ace - cp->ace;
454
455	g_trace(G_T_ACCESS,
456    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
457	    dcr, dcw, dce,
458	    cp->acr, cp->acw, cp->ace,
459	    pp->acr, pp->acw, pp->ace,
460	    pp, pp->name);
461
462	/* If we try exclusive but already write: fail */
463	if (dce > 0 && pw > 0)
464		return (EPERM);
465	/* If we try write but already exclusive: fail */
466	if (dcw > 0 && pe > 0)
467		return (EPERM);
468	/* If we try to open more but provider is error'ed: fail */
469	if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
470		return (pp->error);
471
472	/* Ok then... */
473
474	/*
475	 * If we open first write, spoil any partner consumers.
476	 * If we close last write, trigger re-taste.
477	 */
478	if (pp->acw == 0 && dcw != 0)
479		g_spoil(pp, cp);
480	else if (pp->acw != 0 && pp->acw == -dcw && !(pp->geom->flags & G_GEOM_WITHER))
481		g_post_event(EV_NEW_PROVIDER, NULL, NULL, pp, NULL);
482
483	error = pp->geom->access(pp, dcr, dcw, dce);
484	if (!error) {
485		pp->acr += dcr;
486		pp->acw += dcw;
487		pp->ace += dce;
488		cp->acr += dcr;
489		cp->acw += dcw;
490		cp->ace += dce;
491	}
492	return (error);
493}
494
495int
496g_haveattr_int(struct bio *bp, char *attribute, int val)
497{
498
499	return (g_haveattr(bp, attribute, &val, sizeof val));
500}
501
502int
503g_haveattr_off_t(struct bio *bp, char *attribute, off_t val)
504{
505
506	return (g_haveattr(bp, attribute, &val, sizeof val));
507}
508
509
510int
511g_haveattr(struct bio *bp, char *attribute, void *val, int len)
512{
513	int error;
514
515	if (strcmp(bp->bio_attribute, attribute))
516		return (0);
517	if (bp->bio_length != len) {
518		printf("bio_length %lld len %d -> EFAULT\n",
519		    (long long)bp->bio_length, len);
520		error = EFAULT;
521	} else {
522		error = 0;
523		bcopy(val, bp->bio_data, len);
524		bp->bio_completed = len;
525	}
526	bp->bio_error = error;
527	g_io_deliver(bp);
528	return (1);
529}
530
531int
532g_std_access(struct g_provider *pp __unused,
533	int dr __unused, int dw __unused, int de __unused)
534{
535
536        return (0);
537}
538
539void
540g_std_done(struct bio *bp)
541{
542	struct bio *bp2;
543
544	bp2 = bp->bio_linkage;
545	bp2->bio_error = bp->bio_error;
546	bp2->bio_completed = bp->bio_completed;
547	g_destroy_bio(bp);
548	g_io_deliver(bp2);
549}
550
551/* XXX: maybe this is only g_slice_spoiled */
552
553void
554g_std_spoiled(struct g_consumer *cp)
555{
556	struct g_geom *gp;
557	struct g_provider *pp;
558
559	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
560	g_topology_assert();
561	g_dettach(cp);
562	gp = cp->geom;
563	LIST_FOREACH(pp, &gp->provider, provider)
564		g_orphan_provider(pp, ENXIO);
565	g_destroy_consumer(cp);
566	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
567		g_destroy_geom(gp);
568	else
569		gp->flags |= G_GEOM_WITHER;
570}
571
572/*
573 * Spoiling happens when a provider is opened for writing, but consumers
574 * which are configured by in-band data are attached (slicers for instance).
575 * Since the write might potentially change the in-band data, such consumers
576 * need to re-evaluate their existence after the writing session closes.
577 * We do this by (offering to) tear them down when the open for write happens
578 * in return for a re-taste when it closes again.
579 * Together with the fact that such consumers grab an 'e' bit whenever they
580 * are open, regardless of mode, this ends up DTRT.
581 */
582
583void
584g_spoil(struct g_provider *pp, struct g_consumer *cp)
585{
586	struct g_consumer *cp2;
587
588	g_topology_assert();
589
590	LIST_FOREACH(cp2, &pp->consumers, consumers) {
591		if (cp2 == cp)
592			continue;
593/*
594		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
595		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
596*/
597		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
598		cp2->spoiled++;
599	}
600	g_post_event(EV_SPOILED, NULL, NULL, pp, cp);
601}
602
603static struct g_class *
604g_class_by_name(char *name)
605{
606	struct g_class *mp;
607
608	g_trace(G_T_TOPOLOGY, "g_class_by_name(%s)", name);
609	g_topology_assert();
610	LIST_FOREACH(mp, &g_classes, class)
611		if (!strcmp(mp->name, name))
612			return (mp);
613	return (NULL);
614}
615
616struct g_geom *
617g_create_geomf(char *class, struct g_provider *pp, char *fmt, ...)
618{
619	va_list ap;
620	struct sbuf *sb;
621	char *s;
622	struct g_class *mp;
623	struct g_geom *gp;
624
625	g_trace(G_T_TOPOLOGY, "g_create_geom(%s, %p(%s))", class,
626		pp, pp == NULL ? "" : pp->name);
627	g_topology_assert();
628	gp = NULL;
629	mp = g_class_by_name(class);
630	if (mp == NULL)
631		return (NULL);
632	if (fmt != NULL) {
633		va_start(ap, fmt);
634		mtx_lock(&Giant);
635		sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
636		sbuf_vprintf(sb, fmt, ap);
637		sbuf_finish(sb);
638		mtx_unlock(&Giant);
639		s = sbuf_data(sb);
640	} else {
641		s = NULL;
642	}
643	if (pp != NULL)
644		gp = mp->taste(mp, pp, G_TF_INSIST);
645	if (gp == NULL && mp->create_geom == NULL)
646		return (NULL);
647	if (gp == NULL)
648		gp = mp->create_geom(mp, pp, s);
649	/* XXX: delete sbuf  */
650	return (gp);
651}
652
653struct g_geom *
654g_insert_geom(char *class, struct g_consumer *cp)
655{
656	struct g_class *mp;
657	struct g_geom *gp;
658	struct g_provider *pp, *pp2;
659	struct g_consumer *cp2;
660	int error;
661
662	g_trace(G_T_TOPOLOGY, "g_insert_geomf(%s, %p)", class, cp);
663	g_topology_assert();
664	KASSERT(cp->provider != NULL, ("g_insert_geomf but not attached"));
665	/* XXX: check for events ?? */
666	mp = g_class_by_name(class);
667	if (mp == NULL)
668		return (NULL);
669	if (mp->create_geom == NULL)
670		return (NULL);
671	pp = cp->provider;
672	gp = mp->taste(mp, pp, G_TF_TRANSPARENT);
673	if (gp == NULL)
674		return (NULL);
675	pp2 = LIST_FIRST(&gp->provider);
676	cp2 = LIST_FIRST(&gp->consumer);
677	cp2->acr += pp->acr;
678	cp2->acw += pp->acw;
679	cp2->ace += pp->ace;
680	pp2->acr += pp->acr;
681	pp2->acw += pp->acw;
682	pp2->ace += pp->ace;
683	LIST_REMOVE(cp, consumers);
684	LIST_INSERT_HEAD(&pp2->consumers, cp, consumers);
685	cp->provider = pp2;
686	error = redo_rank(gp);
687	KASSERT(error == 0, ("redo_rank failed in g_insert_geom"));
688	return (gp);
689}
690
691int
692g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
693{
694	int error, i;
695
696	i = len;
697	error = g_io_getattr(attr, cp, &i, var);
698	if (error)
699		return (error);
700	if (i != len)
701		return (EINVAL);
702	return (0);
703}
704
705/*
706 * Check if the given pointer is a live object
707 */
708
709void
710g_sanity(void *ptr)
711{
712	struct g_class *mp;
713	struct g_geom *gp;
714	struct g_consumer *cp;
715	struct g_provider *pp;
716
717	LIST_FOREACH(mp, &g_classes, class) {
718		KASSERT(mp != ptr, ("Ptr is live class"));
719		KASSERT(mp->protect == 0x20016600,
720		    ("corrupt class %p %x", mp, mp->protect));
721		LIST_FOREACH(gp, &mp->geom, geom) {
722			KASSERT(gp != ptr, ("Ptr is live geom"));
723			KASSERT(gp->protect == 0x20016601,
724			    ("corrupt geom, %p %x", gp, gp->protect));
725			KASSERT(gp->name != ptr, ("Ptr is live geom's name"));
726			LIST_FOREACH(cp, &gp->consumer, consumer) {
727				KASSERT(cp != ptr, ("Ptr is live consumer"));
728				KASSERT(cp->protect == 0x20016602,
729				    ("corrupt consumer %p %x",
730				    cp, cp->protect));
731			}
732			LIST_FOREACH(pp, &gp->provider, provider) {
733				KASSERT(pp != ptr, ("Ptr is live provider"));
734				KASSERT(pp->protect == 0x20016603,
735				    ("corrupt provider %p %x",
736				    pp, pp->protect));
737			}
738		}
739	}
740}
741
742
743