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