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