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