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