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