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