geom_subr.c revision 125342
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 125342 2004-02-02 19:49:41Z 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/*
563 * g_access_abs()
564 *
565 * Access-check with absolute new values:  Just fall through
566 * and use the relative version.
567 */
568int
569g_access_abs(struct g_consumer *cp, int acr, int acw, int ace)
570{
571
572	g_topology_assert();
573	return(g_access_rel(cp,
574		acr - cp->acr,
575		acw - cp->acw,
576		ace - cp->ace));
577}
578
579/*
580 * g_access_rel()
581 *
582 * Access-check with delta values.  The question asked is "can provider
583 * "cp" change the access counters by the relative amounts dc[rwe] ?"
584 */
585
586int
587g_access_rel(struct g_consumer *cp, int dcr, int dcw, int dce)
588{
589	struct g_provider *pp;
590	int pr,pw,pe;
591	int error;
592
593	pp = cp->provider;
594
595	g_trace(G_T_ACCESS, "g_access_rel(%p(%s), %d, %d, %d)",
596	    cp, pp->name, dcr, dcw, dce);
597
598	g_topology_assert();
599	KASSERT(cp->provider != NULL, ("access but not attached"));
600	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
601	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
602	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
603	KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
604	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
605
606	/*
607	 * If our class cares about being spoiled, and we have been, we
608	 * are probably just ahead of the event telling us that.  Fail
609	 * now rather than having to unravel this later.
610	 */
611	if (cp->geom->spoiled != NULL && cp->spoiled) {
612		KASSERT(dcr <= 0, ("spoiled but dcr = %d", dcr));
613		KASSERT(dcw <= 0, ("spoiled but dcw = %d", dcw));
614		KASSERT(dce <= 0, ("spoiled but dce = %d", dce));
615	}
616
617	/*
618	 * Figure out what counts the provider would have had, if this
619	 * consumer had (r0w0e0) at this time.
620	 */
621	pr = pp->acr - cp->acr;
622	pw = pp->acw - cp->acw;
623	pe = pp->ace - cp->ace;
624
625	g_trace(G_T_ACCESS,
626    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
627	    dcr, dcw, dce,
628	    cp->acr, cp->acw, cp->ace,
629	    pp->acr, pp->acw, pp->ace,
630	    pp, pp->name);
631
632	/* If foot-shooting is enabled, any open on rank#1 is OK */
633	if ((g_debugflags & 16) && pp->geom->rank == 1)
634		;
635	/* If we try exclusive but already write: fail */
636	else if (dce > 0 && pw > 0)
637		return (EPERM);
638	/* If we try write but already exclusive: fail */
639	else if (dcw > 0 && pe > 0)
640		return (EPERM);
641	/* If we try to open more but provider is error'ed: fail */
642	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
643		return (pp->error);
644
645	/* Ok then... */
646
647	error = pp->geom->access(pp, dcr, dcw, dce);
648	if (!error) {
649		/*
650		 * If we open first write, spoil any partner consumers.
651		 * If we close last write, trigger re-taste.
652		 */
653		if (pp->acw == 0 && dcw != 0)
654			g_spoil(pp, cp);
655		else if (pp->acw != 0 && pp->acw == -dcw &&
656		    !(pp->geom->flags & G_GEOM_WITHER))
657			g_post_event(g_new_provider_event, pp, M_WAITOK,
658			    pp, NULL);
659
660		pp->acr += dcr;
661		pp->acw += dcw;
662		pp->ace += dce;
663		cp->acr += dcr;
664		cp->acw += dcw;
665		cp->ace += dce;
666	}
667	return (error);
668}
669
670int
671g_handleattr_int(struct bio *bp, const char *attribute, int val)
672{
673
674	return (g_handleattr(bp, attribute, &val, sizeof val));
675}
676
677int
678g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
679{
680
681	return (g_handleattr(bp, attribute, &val, sizeof val));
682}
683
684int
685g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
686{
687	int error;
688
689	if (strcmp(bp->bio_attribute, attribute))
690		return (0);
691	if (bp->bio_length != len) {
692		printf("bio_length %jd len %d -> EFAULT\n",
693		    (intmax_t)bp->bio_length, len);
694		error = EFAULT;
695	} else {
696		error = 0;
697		bcopy(val, bp->bio_data, len);
698		bp->bio_completed = len;
699	}
700	g_io_deliver(bp, error);
701	return (1);
702}
703
704int
705g_std_access(struct g_provider *pp __unused,
706	int dr __unused, int dw __unused, int de __unused)
707{
708
709        return (0);
710}
711
712void
713g_std_done(struct bio *bp)
714{
715	struct bio *bp2;
716
717	bp2 = bp->bio_parent;
718	if (bp2->bio_error == 0)
719		bp2->bio_error = bp->bio_error;
720	bp2->bio_completed += bp->bio_completed;
721	g_destroy_bio(bp);
722	bp2->bio_inbed++;
723	if (bp2->bio_children == bp2->bio_inbed)
724		g_io_deliver(bp2, bp2->bio_error);
725}
726
727/* XXX: maybe this is only g_slice_spoiled */
728
729void
730g_std_spoiled(struct g_consumer *cp)
731{
732	struct g_geom *gp;
733	struct g_provider *pp;
734
735	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
736	g_topology_assert();
737	g_detach(cp);
738	gp = cp->geom;
739	LIST_FOREACH(pp, &gp->provider, provider)
740		g_orphan_provider(pp, ENXIO);
741	g_destroy_consumer(cp);
742	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
743		g_destroy_geom(gp);
744	else
745		gp->flags |= G_GEOM_WITHER;
746}
747
748/*
749 * Spoiling happens when a provider is opened for writing, but consumers
750 * which are configured by in-band data are attached (slicers for instance).
751 * Since the write might potentially change the in-band data, such consumers
752 * need to re-evaluate their existence after the writing session closes.
753 * We do this by (offering to) tear them down when the open for write happens
754 * in return for a re-taste when it closes again.
755 * Together with the fact that such consumers grab an 'e' bit whenever they
756 * are open, regardless of mode, this ends up DTRT.
757 */
758
759static void
760g_spoil_event(void *arg, int flag)
761{
762	struct g_provider *pp;
763	struct g_consumer *cp, *cp2;
764
765	g_topology_assert();
766	if (flag == EV_CANCEL)
767		return;
768	pp = arg;
769	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
770		cp2 = LIST_NEXT(cp, consumers);
771		if (!cp->spoiled)
772			continue;
773		cp->spoiled = 0;
774		if (cp->geom->spoiled == NULL)
775			continue;
776		cp->geom->spoiled(cp);
777		g_topology_assert();
778	}
779}
780
781void
782g_spoil(struct g_provider *pp, struct g_consumer *cp)
783{
784	struct g_consumer *cp2;
785
786	g_topology_assert();
787
788	LIST_FOREACH(cp2, &pp->consumers, consumers) {
789		if (cp2 == cp)
790			continue;
791/*
792		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
793		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
794*/
795		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
796		cp2->spoiled++;
797	}
798	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
799}
800
801int
802g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
803{
804	int error, i;
805
806	i = len;
807	error = g_io_getattr(attr, cp, &i, var);
808	if (error)
809		return (error);
810	if (i != len)
811		return (EINVAL);
812	return (0);
813}
814
815/*
816 * XXX: Bandaid for 5.2.
817 * XXX: DO NOT EVEN THINK ABOUT CALLING THIS FUNCTION!
818 */
819static int
820g_valid_obj(void const *ptr)
821{
822	struct g_class *mp;
823	struct g_geom *gp;
824	struct g_consumer *cp;
825	struct g_provider *pp;
826
827	g_topology_assert();
828	LIST_FOREACH(mp, &g_classes, class) {
829		if (ptr == mp)
830			return (1);
831		LIST_FOREACH(gp, &mp->geom, geom) {
832			if (ptr == gp)
833				return (1);
834			LIST_FOREACH(cp, &gp->consumer, consumer)
835				if (ptr == cp)
836					return (1);
837			LIST_FOREACH(pp, &gp->provider, provider)
838				if (ptr == pp)
839					return (1);
840		}
841	}
842	return(0);
843}
844
845/*
846 * Check if the given pointer is a live object
847 */
848
849void
850g_sanity(void const *ptr)
851{
852	struct g_class *mp;
853	struct g_geom *gp;
854	struct g_consumer *cp;
855	struct g_provider *pp;
856
857	if (!(g_debugflags & 0x8))
858		return;
859	LIST_FOREACH(mp, &g_classes, class) {
860		KASSERT(mp != ptr, ("Ptr is live class"));
861		LIST_FOREACH(gp, &mp->geom, geom) {
862			KASSERT(gp != ptr, ("Ptr is live geom"));
863			KASSERT(gp->name != ptr, ("Ptr is live geom's name"));
864			LIST_FOREACH(cp, &gp->consumer, consumer) {
865				KASSERT(cp != ptr, ("Ptr is live consumer"));
866			}
867			LIST_FOREACH(pp, &gp->provider, provider) {
868				KASSERT(pp != ptr, ("Ptr is live provider"));
869			}
870		}
871	}
872}
873
874