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