geom_subr.c revision 126798
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
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/geom/geom_subr.c 126798 2004-03-10 08:49:08Z phk $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/devicestat.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/bio.h>
45#include <sys/sysctl.h>
46#include <sys/proc.h>
47#include <sys/kthread.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#include <sys/errno.h>
51#include <sys/sbuf.h>
52#include <geom/geom.h>
53#include <geom/geom_int.h>
54#include <machine/stdarg.h>
55
56struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
57static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
58char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
59
60struct g_hh00 {
61	struct g_class	*mp;
62	int		error;
63};
64
65/*
66 * This event offers a new class a chance to taste all preexisting providers.
67 */
68static void
69g_load_class(void *arg, int flag)
70{
71	struct g_hh00 *hh;
72	struct g_class *mp2, *mp;
73	struct g_geom *gp;
74	struct g_provider *pp;
75
76	g_topology_assert();
77	if (flag == EV_CANCEL)	/* XXX: can't happen ? */
78		return;
79	if (g_shutdown)
80		return;
81
82	hh = arg;
83	mp = hh->mp;
84	g_free(hh);
85	g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
86	KASSERT(mp->name != NULL && *mp->name != '\0',
87	    ("GEOM class has no name"));
88	LIST_FOREACH(mp2, &g_classes, class) {
89		KASSERT(mp2 != mp,
90		    ("The GEOM class %s already loaded", mp2->name));
91		KASSERT(strcmp(mp2->name, mp->name) != 0,
92		    ("A GEOM class named %s is already loaded", mp2->name));
93	}
94
95	LIST_INIT(&mp->geom);
96	LIST_INSERT_HEAD(&g_classes, mp, class);
97	if (mp->init != NULL)
98		mp->init(mp);
99	if (mp->taste == NULL)
100		return;
101	LIST_FOREACH(mp2, &g_classes, class) {
102		if (mp == mp2)
103			continue;
104		LIST_FOREACH(gp, &mp2->geom, geom) {
105			LIST_FOREACH(pp, &gp->provider, provider) {
106				mp->taste(mp, pp, 0);
107				g_topology_assert();
108			}
109		}
110	}
111}
112
113static void
114g_unload_class(void *arg, int flag)
115{
116	struct g_hh00 *hh;
117	struct g_class *mp;
118	struct g_geom *gp;
119	struct g_provider *pp;
120	struct g_consumer *cp;
121	int error;
122
123	g_topology_assert();
124	hh = arg;
125	mp = hh->mp;
126	G_VALID_CLASS(mp);
127	g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
128
129	/*
130	 * We allow unloading if we have no geoms, or a class
131	 * method we can use to get rid of them.
132	 */
133	if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
134		hh->error = EOPNOTSUPP;
135		return;
136	}
137
138	/* We refuse to unload if anything is open */
139	LIST_FOREACH(gp, &mp->geom, geom) {
140		LIST_FOREACH(pp, &gp->provider, provider)
141			if (pp->acr || pp->acw || pp->ace) {
142				hh->error = EBUSY;
143				return;
144			}
145		LIST_FOREACH(cp, &gp->consumer, consumer)
146			if (cp->acr || cp->acw || cp->ace) {
147				hh->error = EBUSY;
148				return;
149			}
150	}
151
152	/* Bar new entries */
153	mp->taste = NULL;
154	mp->config = NULL;
155
156	error = 0;
157	LIST_FOREACH(gp, &mp->geom, geom) {
158		error = mp->destroy_geom(NULL, mp, gp);
159		if (error != 0)
160			break;
161	}
162	if (error == 0) {
163		if (mp->fini != NULL)
164			mp->fini(mp);
165		LIST_REMOVE(mp, class);
166	}
167	hh->error = error;
168	return;
169}
170
171int
172g_modevent(module_t mod, int type, void *data)
173{
174	struct g_hh00 *hh;
175	int error;
176	static int g_ignition;
177
178	if (!g_ignition) {
179		g_ignition++;
180		g_init();
181	}
182	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
183	hh->mp = data;
184	error = EOPNOTSUPP;
185	switch (type) {
186	case MOD_LOAD:
187		g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name);
188		g_post_event(g_load_class, hh, M_WAITOK, NULL);
189		error = 0;
190		break;
191	case MOD_UNLOAD:
192		g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name);
193		error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL);
194		if (error == 0)
195			error = hh->error;
196		if (error == 0) {
197			g_waitidle();
198			KASSERT(LIST_EMPTY(&hh->mp->geom),
199			    ("Unloaded class (%s) still has geom", hh->mp->name));
200		}
201		g_free(hh);
202		break;
203	default:
204		g_free(hh);
205		break;
206	}
207	return (error);
208}
209
210struct g_geom *
211g_new_geomf(struct g_class *mp, const char *fmt, ...)
212{
213	struct g_geom *gp;
214	va_list ap;
215	struct sbuf *sb;
216
217	g_topology_assert();
218	G_VALID_CLASS(mp);
219	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
220	va_start(ap, fmt);
221	sbuf_vprintf(sb, fmt, ap);
222	va_end(ap);
223	sbuf_finish(sb);
224	gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
225	gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
226	gp->class = mp;
227	gp->rank = 1;
228	LIST_INIT(&gp->consumer);
229	LIST_INIT(&gp->provider);
230	LIST_INSERT_HEAD(&mp->geom, gp, geom);
231	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
232	strcpy(gp->name, sbuf_data(sb));
233	sbuf_delete(sb);
234	return (gp);
235}
236
237void
238g_destroy_geom(struct g_geom *gp)
239{
240
241	g_topology_assert();
242	G_VALID_GEOM(gp);
243	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
244	KASSERT(LIST_EMPTY(&gp->consumer),
245	    ("g_destroy_geom(%s) with consumer(s) [%p]",
246	    gp->name, LIST_FIRST(&gp->consumer)));
247	KASSERT(LIST_EMPTY(&gp->provider),
248	    ("g_destroy_geom(%s) with provider(s) [%p]",
249	    gp->name, LIST_FIRST(&gp->provider)));
250	g_cancel_event(gp);
251	LIST_REMOVE(gp, geom);
252	TAILQ_REMOVE(&geoms, gp, geoms);
253	g_free(gp->name);
254	g_free(gp);
255}
256
257/*
258 * This function is called (repeatedly) until the has withered away.
259 */
260void
261g_wither_geom(struct g_geom *gp, int error)
262{
263	struct g_provider *pp, *pp2;
264	struct g_consumer *cp, *cp2;
265	static int once_is_enough;
266
267	g_topology_assert();
268	G_VALID_GEOM(gp);
269	if (once_is_enough)
270		return;
271	once_is_enough = 1;
272	g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
273	if (!(gp->flags & G_GEOM_WITHER)) {
274		gp->flags |= G_GEOM_WITHER;
275		LIST_FOREACH(pp, &gp->provider, provider)
276			if (!(pp->flags & G_PF_ORPHAN))
277				g_orphan_provider(pp, error);
278	}
279	for (pp = LIST_FIRST(&gp->provider); pp != NULL; pp = pp2) {
280		pp2 = LIST_NEXT(pp, provider);
281		if (!LIST_EMPTY(&pp->consumers))
282			continue;
283		g_destroy_provider(pp);
284	}
285	for (cp = LIST_FIRST(&gp->consumer); cp != NULL; cp = cp2) {
286		cp2 = LIST_NEXT(cp, consumer);
287		if (cp->acr || cp->acw || cp->ace)
288			continue;
289		g_detach(cp);
290		g_destroy_consumer(cp);
291	}
292	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
293		g_destroy_geom(gp);
294	once_is_enough = 0;
295}
296
297struct g_consumer *
298g_new_consumer(struct g_geom *gp)
299{
300	struct g_consumer *cp;
301
302	g_topology_assert();
303	G_VALID_GEOM(gp);
304	KASSERT(!(gp->flags & G_GEOM_WITHER),
305	    ("g_new_consumer on WITHERing geom(%s) (class %s)",
306	    gp->name, gp->class->name));
307	KASSERT(gp->orphan != NULL,
308	    ("g_new_consumer on geom(%s) (class %s) without orphan",
309	    gp->name, gp->class->name));
310
311	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
312	cp->geom = gp;
313	cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
314	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
315	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
316	return(cp);
317}
318
319void
320g_destroy_consumer(struct g_consumer *cp)
321{
322	struct g_geom *gp;
323
324	g_topology_assert();
325	G_VALID_CONSUMER(cp);
326	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
327	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
328	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
329	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
330	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
331	g_cancel_event(cp);
332	gp = cp->geom;
333	LIST_REMOVE(cp, consumer);
334	devstat_remove_entry(cp->stat);
335	g_free(cp);
336	if (gp->flags & G_GEOM_WITHER)
337		g_wither_geom(gp, 0);
338}
339
340static void
341g_new_provider_event(void *arg, int flag)
342{
343	struct g_class *mp;
344	struct g_provider *pp;
345	struct g_consumer *cp;
346	int i;
347
348	g_topology_assert();
349	if (flag == EV_CANCEL)
350		return;
351	if (g_shutdown)
352		return;
353	pp = arg;
354	G_VALID_PROVIDER(pp);
355	LIST_FOREACH(mp, &g_classes, class) {
356		if (mp->taste == NULL)
357			continue;
358		i = 1;
359		LIST_FOREACH(cp, &pp->consumers, consumers)
360			if (cp->geom->class == mp)
361				i = 0;
362		if (!i)
363			continue;
364		mp->taste(mp, pp, 0);
365		g_topology_assert();
366	}
367}
368
369
370struct g_provider *
371g_new_providerf(struct g_geom *gp, const char *fmt, ...)
372{
373	struct g_provider *pp;
374	struct sbuf *sb;
375	va_list ap;
376
377	g_topology_assert();
378	G_VALID_GEOM(gp);
379	KASSERT(gp->start != NULL,
380	    ("new provider on geom(%s) without ->start (class %s)",
381	    gp->name, gp->class->name));
382	KASSERT(!(gp->flags & G_GEOM_WITHER),
383	    ("new provider on WITHERing geom(%s) (class %s)",
384	    gp->name, gp->class->name));
385	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
386	va_start(ap, fmt);
387	sbuf_vprintf(sb, fmt, ap);
388	va_end(ap);
389	sbuf_finish(sb);
390	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
391	pp->name = (char *)(pp + 1);
392	strcpy(pp->name, sbuf_data(sb));
393	sbuf_delete(sb);
394	LIST_INIT(&pp->consumers);
395	pp->error = ENXIO;
396	pp->geom = gp;
397	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
398	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
399	LIST_INSERT_HEAD(&gp->provider, pp, provider);
400	g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
401	return (pp);
402}
403
404void
405g_error_provider(struct g_provider *pp, int error)
406{
407
408	/* G_VALID_PROVIDER(pp);  We may not have g_topology */
409	pp->error = error;
410}
411
412struct g_provider *
413g_provider_by_name(char const *arg)
414{
415	struct g_class *cp;
416	struct g_geom *gp;
417	struct g_provider *pp;
418
419	LIST_FOREACH(cp, &g_classes, class) {
420		LIST_FOREACH(gp, &cp->geom, geom) {
421			LIST_FOREACH(pp, &gp->provider, provider) {
422				if (!strcmp(arg, pp->name))
423					return (pp);
424			}
425		}
426	}
427	return (NULL);
428}
429
430void
431g_destroy_provider(struct g_provider *pp)
432{
433	struct g_geom *gp;
434
435	g_topology_assert();
436	G_VALID_PROVIDER(pp);
437	KASSERT(LIST_EMPTY(&pp->consumers),
438	    ("g_destroy_provider but attached"));
439	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
440	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
441	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
442	g_cancel_event(pp);
443	LIST_REMOVE(pp, provider);
444	gp = pp->geom;
445	devstat_remove_entry(pp->stat);
446	g_free(pp);
447	if ((gp->flags & G_GEOM_WITHER))
448		g_wither_geom(gp, 0);
449}
450
451/*
452 * We keep the "geoms" list sorted by topological order (== increasing
453 * numerical rank) at all times.
454 * When an attach is done, the attaching geoms rank is invalidated
455 * and it is moved to the tail of the list.
456 * All geoms later in the sequence has their ranks reevaluated in
457 * sequence.  If we cannot assign rank to a geom because it's
458 * prerequisites do not have rank, we move that element to the tail
459 * of the sequence with invalid rank as well.
460 * At some point we encounter our original geom and if we stil fail
461 * to assign it a rank, there must be a loop and we fail back to
462 * g_attach() which detach again and calls redo_rank again
463 * to fix up the damage.
464 * It would be much simpler code wise to do it recursively, but we
465 * can't risk that on the kernel stack.
466 */
467
468static int
469redo_rank(struct g_geom *gp)
470{
471	struct g_consumer *cp;
472	struct g_geom *gp1, *gp2;
473	int n, m;
474
475	g_topology_assert();
476	G_VALID_GEOM(gp);
477
478	/* Invalidate this geoms rank and move it to the tail */
479	gp1 = TAILQ_NEXT(gp, geoms);
480	if (gp1 != NULL) {
481		gp->rank = 0;
482		TAILQ_REMOVE(&geoms, gp, geoms);
483		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
484	} else {
485		gp1 = gp;
486	}
487
488	/* re-rank the rest of the sequence */
489	for (; gp1 != NULL; gp1 = gp2) {
490		gp1->rank = 0;
491		m = 1;
492		LIST_FOREACH(cp, &gp1->consumer, consumer) {
493			if (cp->provider == NULL)
494				continue;
495			n = cp->provider->geom->rank;
496			if (n == 0) {
497				m = 0;
498				break;
499			} else if (n >= m)
500				m = n + 1;
501		}
502		gp1->rank = m;
503		gp2 = TAILQ_NEXT(gp1, geoms);
504
505		/* got a rank, moving on */
506		if (m != 0)
507			continue;
508
509		/* no rank to original geom means loop */
510		if (gp == gp1)
511			return (ELOOP);
512
513		/* no rank, put it at the end move on */
514		TAILQ_REMOVE(&geoms, gp1, geoms);
515		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
516	}
517	return (0);
518}
519
520int
521g_attach(struct g_consumer *cp, struct g_provider *pp)
522{
523	int error;
524
525	g_topology_assert();
526	G_VALID_CONSUMER(cp);
527	G_VALID_PROVIDER(pp);
528	KASSERT(cp->provider == NULL, ("attach but attached"));
529	cp->provider = pp;
530	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
531	error = redo_rank(cp->geom);
532	if (error) {
533		LIST_REMOVE(cp, consumers);
534		cp->provider = NULL;
535		redo_rank(cp->geom);
536	}
537	return (error);
538}
539
540void
541g_detach(struct g_consumer *cp)
542{
543	struct g_provider *pp;
544
545	g_topology_assert();
546	G_VALID_CONSUMER(cp);
547	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
548	KASSERT(cp->provider != NULL, ("detach but not attached"));
549	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
550	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
551	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
552	KASSERT(cp->nstart == cp->nend,
553	    ("detach with active requests"));
554	pp = cp->provider;
555	LIST_REMOVE(cp, consumers);
556	cp->provider = NULL;
557	if (pp->geom->flags & G_GEOM_WITHER)
558		g_wither_geom(pp->geom, 0);
559	else if (pp->flags & G_PF_WITHER)
560		g_destroy_provider(pp);
561	redo_rank(cp->geom);
562}
563
564/*
565 * g_access()
566 *
567 * Access-check with delta values.  The question asked is "can provider
568 * "cp" change the access counters by the relative amounts dc[rwe] ?"
569 */
570
571int
572g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
573{
574	struct g_provider *pp;
575	int pr,pw,pe;
576	int error;
577
578	g_topology_assert();
579	G_VALID_CONSUMER(cp);
580	pp = cp->provider;
581	G_VALID_PROVIDER(pp);
582
583	g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
584	    cp, pp->name, dcr, dcw, dce);
585
586	KASSERT(cp->provider != NULL, ("access but not attached"));
587	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
588	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
589	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
590	KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
591	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
592
593	/*
594	 * If our class cares about being spoiled, and we have been, we
595	 * are probably just ahead of the event telling us that.  Fail
596	 * now rather than having to unravel this later.
597	 */
598	if (cp->geom->spoiled != NULL && cp->spoiled) {
599		KASSERT(dcr <= 0, ("spoiled but dcr = %d", dcr));
600		KASSERT(dcw <= 0, ("spoiled but dcw = %d", dcw));
601		KASSERT(dce <= 0, ("spoiled but dce = %d", dce));
602	}
603
604	/*
605	 * Figure out what counts the provider would have had, if this
606	 * consumer had (r0w0e0) at this time.
607	 */
608	pr = pp->acr - cp->acr;
609	pw = pp->acw - cp->acw;
610	pe = pp->ace - cp->ace;
611
612	g_trace(G_T_ACCESS,
613    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
614	    dcr, dcw, dce,
615	    cp->acr, cp->acw, cp->ace,
616	    pp->acr, pp->acw, pp->ace,
617	    pp, pp->name);
618
619	/* If foot-shooting is enabled, any open on rank#1 is OK */
620	if ((g_debugflags & 16) && pp->geom->rank == 1)
621		;
622	/* If we try exclusive but already write: fail */
623	else if (dce > 0 && pw > 0)
624		return (EPERM);
625	/* If we try write but already exclusive: fail */
626	else if (dcw > 0 && pe > 0)
627		return (EPERM);
628	/* If we try to open more but provider is error'ed: fail */
629	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
630		return (pp->error);
631
632	/* Ok then... */
633
634	error = pp->geom->access(pp, dcr, dcw, dce);
635	KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
636	    ("Geom provider %s::%s failed closing ->access()",
637	    pp->geom->class->name, pp->name));
638	if (!error) {
639		/*
640		 * If we open first write, spoil any partner consumers.
641		 * If we close last write, trigger re-taste.
642		 */
643		if (pp->acw == 0 && dcw != 0)
644			g_spoil(pp, cp);
645		else if (pp->acw != 0 && pp->acw == -dcw &&
646		    !(pp->geom->flags & G_GEOM_WITHER))
647			g_post_event(g_new_provider_event, pp, M_WAITOK,
648			    pp, NULL);
649
650		pp->acr += dcr;
651		pp->acw += dcw;
652		pp->ace += dce;
653		cp->acr += dcr;
654		cp->acw += dcw;
655		cp->ace += dce;
656	}
657	return (error);
658}
659
660int
661g_handleattr_int(struct bio *bp, const char *attribute, int val)
662{
663
664	return (g_handleattr(bp, attribute, &val, sizeof val));
665}
666
667int
668g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
669{
670
671	return (g_handleattr(bp, attribute, &val, sizeof val));
672}
673
674int
675g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
676{
677	int error;
678
679	if (strcmp(bp->bio_attribute, attribute))
680		return (0);
681	if (bp->bio_length != len) {
682		printf("bio_length %jd len %d -> EFAULT\n",
683		    (intmax_t)bp->bio_length, len);
684		error = EFAULT;
685	} else {
686		error = 0;
687		bcopy(val, bp->bio_data, len);
688		bp->bio_completed = len;
689	}
690	g_io_deliver(bp, error);
691	return (1);
692}
693
694int
695g_std_access(struct g_provider *pp,
696	int dr __unused, int dw __unused, int de __unused)
697{
698
699	g_topology_assert();
700	G_VALID_PROVIDER(pp);
701        return (0);
702}
703
704void
705g_std_done(struct bio *bp)
706{
707	struct bio *bp2;
708
709	bp2 = bp->bio_parent;
710	if (bp2->bio_error == 0)
711		bp2->bio_error = bp->bio_error;
712	bp2->bio_completed += bp->bio_completed;
713	g_destroy_bio(bp);
714	bp2->bio_inbed++;
715	if (bp2->bio_children == bp2->bio_inbed)
716		g_io_deliver(bp2, bp2->bio_error);
717}
718
719/* XXX: maybe this is only g_slice_spoiled */
720
721void
722g_std_spoiled(struct g_consumer *cp)
723{
724	struct g_geom *gp;
725	struct g_provider *pp;
726
727	g_topology_assert();
728	G_VALID_CONSUMER(cp);
729	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
730	g_detach(cp);
731	gp = cp->geom;
732	LIST_FOREACH(pp, &gp->provider, provider)
733		g_orphan_provider(pp, ENXIO);
734	g_destroy_consumer(cp);
735	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
736		g_destroy_geom(gp);
737	else
738		gp->flags |= G_GEOM_WITHER;
739}
740
741/*
742 * Spoiling happens when a provider is opened for writing, but consumers
743 * which are configured by in-band data are attached (slicers for instance).
744 * Since the write might potentially change the in-band data, such consumers
745 * need to re-evaluate their existence after the writing session closes.
746 * We do this by (offering to) tear them down when the open for write happens
747 * in return for a re-taste when it closes again.
748 * Together with the fact that such consumers grab an 'e' bit whenever they
749 * are open, regardless of mode, this ends up DTRT.
750 */
751
752static void
753g_spoil_event(void *arg, int flag)
754{
755	struct g_provider *pp;
756	struct g_consumer *cp, *cp2;
757
758	g_topology_assert();
759	if (flag == EV_CANCEL)
760		return;
761	pp = arg;
762	G_VALID_PROVIDER(pp);
763	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
764		cp2 = LIST_NEXT(cp, consumers);
765		if (!cp->spoiled)
766			continue;
767		cp->spoiled = 0;
768		if (cp->geom->spoiled == NULL)
769			continue;
770		cp->geom->spoiled(cp);
771		g_topology_assert();
772	}
773}
774
775void
776g_spoil(struct g_provider *pp, struct g_consumer *cp)
777{
778	struct g_consumer *cp2;
779
780	g_topology_assert();
781	G_VALID_PROVIDER(pp);
782	G_VALID_CONSUMER(cp);
783
784	LIST_FOREACH(cp2, &pp->consumers, consumers) {
785		if (cp2 == cp)
786			continue;
787/*
788		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
789		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
790*/
791		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
792		cp2->spoiled++;
793	}
794	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
795}
796
797int
798g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
799{
800	int error, i;
801
802	i = len;
803	error = g_io_getattr(attr, cp, &i, var);
804	if (error)
805		return (error);
806	if (i != len)
807		return (EINVAL);
808	return (0);
809}
810
811#ifdef DIAGNOSTIC
812/*
813 * This function walks (topologically unsafely) the mesh and return a
814 * non-zero integer if it finds the argument pointer is an object.
815 * The return value indicates which type of object it is belived to be.
816 * If topology is not locked, this function is potentially dangerous,
817 * but since it is for debugging purposes and can be useful for instance
818 * from DDB, we do not assert topology lock is held.
819 */
820int
821g_valid_obj(void const *ptr)
822{
823	struct g_class *mp;
824	struct g_geom *gp;
825	struct g_consumer *cp;
826	struct g_provider *pp;
827
828	LIST_FOREACH(mp, &g_classes, class) {
829		if (ptr == mp)
830			return (1);
831		LIST_FOREACH(gp, &mp->geom, geom) {
832			if (ptr == gp)
833				return (2);
834			LIST_FOREACH(cp, &gp->consumer, consumer)
835				if (ptr == cp)
836					return (3);
837			LIST_FOREACH(pp, &gp->provider, provider)
838				if (ptr == pp)
839					return (4);
840		}
841	}
842	return(0);
843}
844#endif
845