geom_subr.c revision 95310
155682Smarkm/*-
2233294Sstas * Copyright (c) 2002 Poul-Henning Kamp
3233294Sstas * Copyright (c) 2002 Networks Associates Technology, Inc.
4233294Sstas * All rights reserved.
555682Smarkm *
6233294Sstas * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7233294Sstas * and NAI Labs, the Security Research Division of Network Associates, Inc.
8233294Sstas * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
955682Smarkm * DARPA CHATS research program.
10233294Sstas *
11233294Sstas * Redistribution and use in source and binary forms, with or without
1255682Smarkm * modification, are permitted provided that the following conditions
13233294Sstas * are met:
14233294Sstas * 1. Redistributions of source code must retain the above copyright
15233294Sstas *    notice, this list of conditions and the following disclaimer.
1655682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
17233294Sstas *    notice, this list of conditions and the following disclaimer in the
18233294Sstas *    documentation and/or other materials provided with the distribution.
19233294Sstas * 3. The names of the authors may not be used to endorse or promote
2055682Smarkm *    products derived from this software without specific prior written
21233294Sstas *    permission.
22233294Sstas *
23233294Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3255682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3355682Smarkm * SUCH DAMAGE.
3455682Smarkm *
3555682Smarkm * $FreeBSD: head/sys/geom/geom_subr.c 95310 2002-04-23 11:48:45Z phk $
36233294Sstas */
3755682Smarkm
3855682Smarkm
39178825Sdfr#include <sys/param.h>
4055682Smarkm#ifndef _KERNEL
4178527Sassar#include <stdio.h>
4255682Smarkm#include <unistd.h>
4378527Sassar#include <stdlib.h>
4455682Smarkm#include <signal.h>
4555682Smarkm#include <string.h>
46178825Sdfr#include <err.h>
47178825Sdfr#else
48178825Sdfr#include <sys/systm.h>
4955682Smarkm#include <sys/kernel.h>
50178825Sdfr#include <sys/malloc.h>
5178527Sassar#include <sys/bio.h>
5255682Smarkm#include <sys/sysctl.h>
5355682Smarkm#include <sys/proc.h>
54178825Sdfr#include <sys/kthread.h>
55178825Sdfr#include <sys/lock.h>
5655682Smarkm#include <sys/mutex.h>
5755682Smarkm#endif
58178825Sdfr#include <sys/errno.h>
5955682Smarkm#include <sys/sbuf.h>
6055682Smarkm#include <geom/geom.h>
61178825Sdfr#include <geom/geom_int.h>
6255682Smarkm#include <machine/stdarg.h>
6355682Smarkm
6478527Sassarstruct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
6555682Smarkmstatic struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
6655682Smarkmstatic int g_nproviders;
6755682Smarkmchar *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
68178825Sdfr
69233294Sstasstatic int g_ignition;
7055682Smarkm
7155682Smarkmvoid
72178825Sdfrg_add_class(struct g_class *mp)
73178825Sdfr{
7455682Smarkm
7578527Sassar	if (!g_ignition) {
76178825Sdfr		g_ignition++;
77178825Sdfr		g_init();
78178825Sdfr	}
79178825Sdfr	mp->protect = 0x020016600;
8078527Sassar	g_topology_lock();
8155682Smarkm	g_trace(G_T_TOPOLOGY, "g_add_class(%s)", mp->name);
8255682Smarkm	LIST_INIT(&mp->geom);
83178825Sdfr	LIST_INSERT_HEAD(&g_classes, mp, class);
8455682Smarkm	if (g_nproviders > 0)
8578527Sassar		g_post_event(EV_NEW_CLASS, mp, NULL, NULL, NULL);
8655682Smarkm	g_topology_unlock();
8755682Smarkm}
88178825Sdfr
8955682Smarkmstruct g_geom *
9055682Smarkmg_new_geomf(struct g_class *mp, char *fmt, ...)
91178825Sdfr{
9255682Smarkm	struct g_geom *gp;
9355682Smarkm	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
117void
118g_destroy_geom(struct g_geom *gp)
119{
120
121	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
122	g_topology_assert();
123	KASSERT(gp->event == NULL, ("g_destroy_geom() with event"));
124	KASSERT(LIST_EMPTY(&gp->consumer),
125	    ("g_destroy_geom(%s) with consumer(s) [%p]",
126	    gp->name, LIST_FIRST(&gp->consumer)));
127	KASSERT(LIST_EMPTY(&gp->provider),
128	    ("g_destroy_geom(%s) with provider(s) [%p]",
129	    gp->name, LIST_FIRST(&gp->consumer)));
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	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
150	return(cp);
151}
152
153void
154g_destroy_consumer(struct g_consumer *cp)
155{
156
157	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
158	g_topology_assert();
159	KASSERT(cp->event == NULL, ("g_destroy_consumer() with event"));
160	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
161	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
162	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
163	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
164	LIST_REMOVE(cp, consumer);
165	g_free(cp);
166}
167
168struct g_provider *
169g_new_providerf(struct g_geom *gp, char *fmt, ...)
170{
171	struct g_provider *pp;
172	struct sbuf *sb;
173	va_list ap;
174
175	g_topology_assert();
176	va_start(ap, fmt);
177	mtx_lock(&Giant);
178	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
179	sbuf_vprintf(sb, fmt, ap);
180	sbuf_finish(sb);
181	mtx_unlock(&Giant);
182	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
183	pp->protect = 0x020016603;
184	pp->name = (char *)(pp + 1);
185	strcpy(pp->name, sbuf_data(sb));
186	sbuf_delete(sb);
187	LIST_INIT(&pp->consumers);
188	pp->error = ENXIO;
189	pp->geom = gp;
190	LIST_INSERT_HEAD(&gp->provider, pp, provider);
191	g_nproviders++;
192	g_post_event(EV_NEW_PROVIDER, NULL, NULL, pp, NULL);
193	return (pp);
194}
195
196void
197g_error_provider(struct g_provider *pp, int error)
198{
199
200	pp->error = error;
201}
202
203
204void
205g_destroy_provider(struct g_provider *pp)
206{
207	struct g_geom *gp;
208	struct g_consumer *cp;
209
210	g_topology_assert();
211	KASSERT(pp->event == NULL, ("g_destroy_provider() with event"));
212	KASSERT(LIST_EMPTY(&pp->consumers),
213	    ("g_destroy_provider but attached"));
214	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
215	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
216	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
217	g_nproviders--;
218	LIST_REMOVE(pp, provider);
219	gp = pp->geom;
220	g_free(pp);
221	if (!(gp->flags & G_GEOM_WITHER))
222		return;
223	if (!LIST_EMPTY(&gp->provider))
224		return;
225	for (;;) {
226		cp = LIST_FIRST(&gp->consumer);
227		if (cp == NULL)
228			break;
229		g_dettach(cp);
230		g_destroy_consumer(cp);
231	}
232	g_destroy_geom(gp);
233}
234
235/*
236 * We keep the "geoms" list sorted by topological order (== increasing
237 * numerical rank) at all times.
238 * When an attach is done, the attaching geoms rank is invalidated
239 * and it is moved to the tail of the list.
240 * All geoms later in the sequence has their ranks reevaluated in
241 * sequence.  If we cannot assign rank to a geom because it's
242 * prerequisites do not have rank, we move that element to the tail
243 * of the sequence with invalid rank as well.
244 * At some point we encounter our original geom and if we stil fail
245 * to assign it a rank, there must be a loop and we fail back to
246 * g_attach() which dettach again and calls redo_rank again
247 * to fix up the damage.
248 * It would be much simpler code wise to do it recursively, but we
249 * can't risk that on the kernel stack.
250 */
251
252static int
253redo_rank(struct g_geom *gp)
254{
255	struct g_consumer *cp;
256	struct g_geom *gp1, *gp2;
257	int n, m;
258
259	g_topology_assert();
260
261	/* Invalidate this geoms rank and move it to the tail */
262	gp1 = TAILQ_NEXT(gp, geoms);
263	if (gp1 != NULL) {
264		gp->rank = 0;
265		TAILQ_REMOVE(&geoms, gp, geoms);
266		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
267	} else {
268		gp1 = gp;
269	}
270
271	/* re-rank the rest of the sequence */
272	for (; gp1 != NULL; gp1 = gp2) {
273		gp1->rank = 0;
274		m = 1;
275		LIST_FOREACH(cp, &gp1->consumer, consumer) {
276			if (cp->provider == NULL)
277				continue;
278			n = cp->provider->geom->rank;
279			if (n == 0) {
280				m = 0;
281				break;
282			} else if (n >= m)
283				m = n + 1;
284		}
285		gp1->rank = m;
286		gp2 = TAILQ_NEXT(gp1, geoms);
287
288		/* got a rank, moving on */
289		if (m != 0)
290			continue;
291
292		/* no rank to original geom means loop */
293		if (gp == gp1) {
294			return (ELOOP);
295
296		/* no rank, put it at the end move on */
297		TAILQ_REMOVE(&geoms, gp1, geoms);
298		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
299		}
300	}
301	return (0);
302}
303
304int
305g_attach(struct g_consumer *cp, struct g_provider *pp)
306{
307	int error;
308
309	g_topology_assert();
310	KASSERT(cp->provider == NULL, ("attach but attached"));
311	cp->provider = pp;
312	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
313	error = redo_rank(cp->geom);
314	if (error) {
315		LIST_REMOVE(cp, consumers);
316		cp->provider = NULL;
317		redo_rank(cp->geom);
318	}
319	return (error);
320}
321
322void
323g_dettach(struct g_consumer *cp)
324{
325	struct g_provider *pp;
326
327	g_trace(G_T_TOPOLOGY, "g_dettach(%p)", cp);
328	KASSERT(cp != (void*)0xd0d0d0d0, ("ARGH!"));
329	g_topology_assert();
330	KASSERT(cp->provider != NULL, ("dettach but not attached"));
331	KASSERT(cp->acr == 0, ("dettach but nonzero acr"));
332	KASSERT(cp->acw == 0, ("dettach but nonzero acw"));
333	KASSERT(cp->ace == 0, ("dettach but nonzero ace"));
334	KASSERT(cp->biocount == 0, ("dettach but nonzero biocount"));
335	pp = cp->provider;
336	LIST_REMOVE(cp, consumers);
337	cp->provider = NULL;
338	if (LIST_EMPTY(&pp->consumers)) {
339		if (pp->geom->flags & G_GEOM_WITHER)
340			g_destroy_provider(pp);
341	}
342	redo_rank(cp->geom);
343}
344
345
346/*
347 * g_access_abs()
348 *
349 * Access-check with absolute new values:  Just fall through
350 * and use the relative version.
351 */
352int
353g_access_abs(struct g_consumer *cp, int acr, int acw, int ace)
354{
355
356	g_topology_assert();
357	return(g_access_rel(cp,
358		acr - cp->acr,
359		acw - cp->acw,
360		ace - cp->ace));
361}
362
363/*
364 * g_access_rel()
365 *
366 * Access-check with delta values.  The question asked is "can provider
367 * "cp" change the access counters by the relative amounts dc[rwe] ?"
368 */
369
370int
371g_access_rel(struct g_consumer *cp, int dcr, int dcw, int dce)
372{
373	struct g_provider *pp;
374	int pr,pw,pe;
375	int error;
376
377	pp = cp->provider;
378
379	g_trace(G_T_ACCESS, "g_access_rel(%p(%s), %d, %d, %d)",
380	    cp, pp->name, dcr, dcw, dce);
381
382	g_topology_assert();
383	KASSERT(cp->provider != NULL, ("access but not attached"));
384	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
385	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
386	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
387	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
388
389	/*
390	 * If our class cares about being spoiled, and we have been, we
391	 * are probably just ahead of the event telling us that.  Fail
392	 * now rather than having to unravel this later.
393	 */
394	if (cp->geom->spoiled != NULL && cp->spoiled) {
395		KASSERT(dcr >= 0, ("spoiled but dcr = %d", dcr));
396		KASSERT(dcw >= 0, ("spoiled but dce = %d", dcw));
397		KASSERT(dce >= 0, ("spoiled but dcw = %d", dce));
398		KASSERT(cp->acr == 0, ("spoiled but cp->acr = %d", cp->acr));
399		KASSERT(cp->acw == 0, ("spoiled but cp->acw = %d", cp->acw));
400		KASSERT(cp->ace == 0, ("spoiled but cp->ace = %d", cp->ace));
401		return(ENXIO);
402	}
403
404	/*
405	 * Figure out what counts the provider would have had, if this
406	 * consumer had (r0w0e0) at this time.
407	 */
408	pr = pp->acr - cp->acr;
409	pw = pp->acw - cp->acw;
410	pe = pp->ace - cp->ace;
411
412	g_trace(G_T_ACCESS,
413    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
414	    dcr, dcw, dce,
415	    cp->acr, cp->acw, cp->ace,
416	    pp->acr, pp->acw, pp->ace,
417	    pp, pp->name);
418
419	/* If we try exclusive but already write: fail */
420	if (dce > 0 && pw > 0)
421		return (EPERM);
422	/* If we try write but already exclusive: fail */
423	if (dcw > 0 && pe > 0)
424		return (EPERM);
425	/* If we try to open more but provider is error'ed: fail */
426	if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
427		return (pp->error);
428
429	/* Ok then... */
430
431	/*
432	 * If we open first write, spoil any partner consumers.
433	 * If we close last write, trigger re-taste.
434	 */
435	if (pp->acw == 0 && dcw != 0)
436		g_spoil(pp, cp);
437	else if (pp->acw != 0 && pp->acw == -dcw && !(pp->geom->flags & G_GEOM_WITHER))
438		g_post_event(EV_NEW_PROVIDER, NULL, NULL, pp, NULL);
439
440	error = pp->geom->access(pp, dcr, dcw, dce);
441	if (!error) {
442		pp->acr += dcr;
443		pp->acw += dcw;
444		pp->ace += dce;
445		cp->acr += dcr;
446		cp->acw += dcw;
447		cp->ace += dce;
448	}
449	return (error);
450}
451
452int
453g_haveattr_int(struct bio *bp, char *attribute, int val)
454{
455
456	return (g_haveattr(bp, attribute, &val, sizeof val));
457}
458
459int
460g_haveattr_off_t(struct bio *bp, char *attribute, off_t val)
461{
462
463	return (g_haveattr(bp, attribute, &val, sizeof val));
464}
465
466
467int
468g_haveattr(struct bio *bp, char *attribute, void *val, int len)
469{
470	int error;
471
472	if (strcmp(bp->bio_attribute, attribute))
473		return (0);
474	if (bp->bio_length != len) {
475		printf("bio_length %lld len %d -> EFAULT\n",
476		    (long long)bp->bio_length, len);
477		error = EFAULT;
478	} else {
479		error = 0;
480		bcopy(val, bp->bio_data, len);
481		bp->bio_completed = len;
482	}
483	bp->bio_error = error;
484	g_io_deliver(bp);
485	return (1);
486}
487
488int
489g_std_access(struct g_provider *pp __unused,
490	int dr __unused, int dw __unused, int de __unused)
491{
492
493        return (0);
494}
495
496void
497g_std_done(struct bio *bp)
498{
499	struct bio *bp2;
500
501	bp2 = bp->bio_linkage;
502	bp2->bio_error = bp->bio_error;
503	bp2->bio_completed = bp->bio_completed;
504	g_destroy_bio(bp);
505	g_io_deliver(bp2);
506}
507
508/* XXX: maybe this is only g_slice_spoiled */
509
510void
511g_std_spoiled(struct g_consumer *cp)
512{
513	struct g_geom *gp;
514	struct g_provider *pp;
515
516	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
517	g_topology_assert();
518	g_dettach(cp);
519	gp = cp->geom;
520	LIST_FOREACH(pp, &gp->provider, provider)
521		g_orphan_provider(pp, ENXIO);
522	g_destroy_consumer(cp);
523	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
524		g_destroy_geom(gp);
525	else
526		gp->flags |= G_GEOM_WITHER;
527}
528
529/*
530 * Spoiling happens when a provider is opened for writing, but consumers
531 * which are configured by in-band data are attached (slicers for instance).
532 * Since the write might potentially change the in-band data, such consumers
533 * need to re-evaluate their existence after the writing session closes.
534 * We do this by (offering to) tear them down when the open for write happens
535 * in return for a re-taste when it closes again.
536 * Together with the fact that such consumers grab an 'e' bit whenever they
537 * are open, regardless of mode, this ends up DTRT.
538 */
539
540void
541g_spoil(struct g_provider *pp, struct g_consumer *cp)
542{
543	struct g_consumer *cp2;
544
545	g_topology_assert();
546
547	LIST_FOREACH(cp2, &pp->consumers, consumers) {
548		if (cp2 == cp)
549			continue;
550/*
551		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
552		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
553*/
554		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
555		cp2->spoiled++;
556	}
557	g_post_event(EV_SPOILED, NULL, NULL, pp, cp);
558}
559
560static struct g_class *
561g_class_by_name(char *name)
562{
563	struct g_class *mp;
564
565	g_trace(G_T_TOPOLOGY, "g_class_by_name(%s)", name);
566	g_topology_assert();
567	LIST_FOREACH(mp, &g_classes, class)
568		if (!strcmp(mp->name, name))
569			return (mp);
570	return (NULL);
571}
572
573struct g_geom *
574g_create_geomf(char *class, struct g_provider *pp, char *fmt, ...)
575{
576	va_list ap;
577	struct sbuf *sb;
578	char *s;
579	struct g_class *mp;
580	struct g_geom *gp;
581
582	g_trace(G_T_TOPOLOGY, "g_create_geom(%s, %p(%s))", class,
583		pp, pp == NULL ? "" : pp->name);
584	g_topology_assert();
585	gp = NULL;
586	mp = g_class_by_name(class);
587	if (mp == NULL)
588		return (NULL);
589	if (fmt != NULL) {
590		va_start(ap, fmt);
591		mtx_lock(&Giant);
592		sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
593		sbuf_vprintf(sb, fmt, ap);
594		sbuf_finish(sb);
595		mtx_unlock(&Giant);
596		s = sbuf_data(sb);
597	} else {
598		s = NULL;
599	}
600	if (pp != NULL)
601		gp = mp->taste(mp, pp, G_TF_INSIST);
602	if (gp == NULL && mp->create_geom == NULL)
603		return (NULL);
604	if (gp == NULL)
605		gp = mp->create_geom(mp, pp, s);
606	/* XXX: delete sbuf  */
607	return (gp);
608}
609
610struct g_geom *
611g_insert_geom(char *class, struct g_consumer *cp)
612{
613	struct g_class *mp;
614	struct g_geom *gp;
615	struct g_provider *pp, *pp2;
616	struct g_consumer *cp2;
617	int error;
618
619	g_trace(G_T_TOPOLOGY, "g_insert_geomf(%s, %p)", class, cp);
620	g_topology_assert();
621	KASSERT(cp->provider != NULL, ("g_insert_geomf but not attached"));
622	/* XXX: check for events ?? */
623	mp = g_class_by_name(class);
624	if (mp == NULL)
625		return (NULL);
626	if (mp->create_geom == NULL)
627		return (NULL);
628	pp = cp->provider;
629	gp = mp->taste(mp, pp, G_TF_TRANSPARENT);
630	if (gp == NULL)
631		return (NULL);
632	pp2 = LIST_FIRST(&gp->provider);
633	cp2 = LIST_FIRST(&gp->consumer);
634	cp2->acr += pp->acr;
635	cp2->acw += pp->acw;
636	cp2->ace += pp->ace;
637	pp2->acr += pp->acr;
638	pp2->acw += pp->acw;
639	pp2->ace += pp->ace;
640	LIST_REMOVE(cp, consumers);
641	LIST_INSERT_HEAD(&pp2->consumers, cp, consumers);
642	cp->provider = pp2;
643	error = redo_rank(gp);
644	KASSERT(error == 0, ("redo_rank failed in g_insert_geom"));
645	return (gp);
646}
647
648int
649g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
650{
651	int error, i;
652
653	i = len;
654	error = g_io_getattr(attr, cp, &i, var);
655	if (error)
656		return (error);
657	if (i != len)
658		return (EINVAL);
659	return (0);
660}
661
662/*
663 * Check if the given pointer is a live object
664 */
665
666void
667g_sanity(void *ptr)
668{
669	struct g_class *mp;
670	struct g_geom *gp;
671	struct g_consumer *cp;
672	struct g_provider *pp;
673
674	LIST_FOREACH(mp, &g_classes, class) {
675		KASSERT(mp != ptr, ("Ptr is live class"));
676		KASSERT(mp->protect == 0x20016600,
677		    ("corrupt class %p %x", mp, mp->protect));
678		LIST_FOREACH(gp, &mp->geom, geom) {
679			KASSERT(gp != ptr, ("Ptr is live geom"));
680			KASSERT(gp->protect == 0x20016601,
681			    ("corrupt geom, %p %x", gp, gp->protect));
682			KASSERT(gp->name != ptr, ("Ptr is live geom's name"));
683			LIST_FOREACH(cp, &gp->consumer, consumer) {
684				KASSERT(cp != ptr, ("Ptr is live consumer"));
685				KASSERT(cp->protect == 0x20016602,
686				    ("corrupt consumer %p %x",
687				    cp, cp->protect));
688			}
689			LIST_FOREACH(pp, &gp->provider, provider) {
690				KASSERT(pp != ptr, ("Ptr is live provider"));
691				KASSERT(pp->protect == 0x20016603,
692				    ("corrupt provider %p %x",
693				    pp, pp->protect));
694			}
695		}
696	}
697}
698
699
700