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