geom_subr.c revision 133316
1232950Stheraven/*-
2232950Stheraven * Copyright (c) 2002 Poul-Henning Kamp
3232950Stheraven * Copyright (c) 2002 Networks Associates Technology, Inc.
4232950Stheraven * All rights reserved.
5232950Stheraven *
6232950Stheraven * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7232950Stheraven * and NAI Labs, the Security Research Division of Network Associates, Inc.
8232950Stheraven * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9232950Stheraven * DARPA CHATS research program.
10232950Stheraven *
11232950Stheraven * Redistribution and use in source and binary forms, with or without
12232950Stheraven * modification, are permitted provided that the following conditions
13232950Stheraven * are met:
14232950Stheraven * 1. Redistributions of source code must retain the above copyright
15232950Stheraven *    notice, this list of conditions and the following disclaimer.
16232950Stheraven * 2. Redistributions in binary form must reproduce the above copyright
17232950Stheraven *    notice, this list of conditions and the following disclaimer in the
18232950Stheraven *    documentation and/or other materials provided with the distribution.
19232950Stheraven * 3. The names of the authors may not be used to endorse or promote
20232950Stheraven *    products derived from this software without specific prior written
21232950Stheraven *    permission.
22232950Stheraven *
23232950Stheraven * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24232950Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25232950Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26232950Stheraven * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27227825Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28227825Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29227825Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30227825Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31227825Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32233235Stheraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33233235Stheraven * SUCH DAMAGE.
34233235Stheraven */
35233235Stheraven
36227825Stheraven#include <sys/cdefs.h>
37233235Stheraven__FBSDID("$FreeBSD: head/sys/geom/geom_subr.c 133316 2004-08-08 07:30:47Z phk $");
38233235Stheraven
39233235Stheraven#include <sys/param.h>
40233235Stheraven#include <sys/systm.h>
41233235Stheraven#include <sys/devicestat.h>
42233235Stheraven#include <sys/kernel.h>
43233235Stheraven#include <sys/malloc.h>
44233235Stheraven#include <sys/bio.h>
45233235Stheraven#include <sys/sysctl.h>
46233235Stheraven#include <sys/proc.h>
47233235Stheraven#include <sys/kthread.h>
48233235Stheraven#include <sys/lock.h>
49233235Stheraven#include <sys/mutex.h>
50233235Stheraven#include <sys/errno.h>
51233235Stheraven#include <sys/sbuf.h>
52233235Stheraven#include <geom/geom.h>
53233235Stheraven#include <geom/geom_int.h>
54233235Stheraven#include <machine/stdarg.h>
55233235Stheraven
56233235Stheravenstruct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
57233235Stheravenstatic struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
58233235Stheravenchar *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
59233235Stheraven
60233235Stheravenstruct g_hh00 {
61233235Stheraven	struct g_class	*mp;
62233235Stheraven	int		error;
63233235Stheraven};
64233235Stheraven
65233235Stheraven/*
66233235Stheraven * This event offers a new class a chance to taste all preexisting providers.
67233235Stheraven */
68233235Stheravenstatic void
69233235Stheraveng_load_class(void *arg, int flag)
70233235Stheraven{
71233235Stheraven	struct g_hh00 *hh;
72233235Stheraven	struct g_class *mp2, *mp;
73233235Stheraven	struct g_geom *gp;
74233235Stheraven	struct g_provider *pp;
75233235Stheraven
76233235Stheraven	g_topology_assert();
77233235Stheraven	if (flag == EV_CANCEL)	/* XXX: can't happen ? */
78233235Stheraven		return;
79233235Stheraven	if (g_shutdown)
80233235Stheraven		return;
81233235Stheraven
82233235Stheraven	hh = arg;
83233235Stheraven	mp = hh->mp;
84233235Stheraven	g_free(hh);
85233235Stheraven	g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
86233235Stheraven	KASSERT(mp->name != NULL && *mp->name != '\0',
87233235Stheraven	    ("GEOM class has no name"));
88233235Stheraven	LIST_FOREACH(mp2, &g_classes, class) {
89233235Stheraven		KASSERT(mp2 != mp,
90233235Stheraven		    ("The GEOM class %s already loaded", mp2->name));
91233235Stheraven		KASSERT(strcmp(mp2->name, mp->name) != 0,
92233235Stheraven		    ("A GEOM class named %s is already loaded", mp2->name));
93233235Stheraven	}
94233235Stheraven
95233235Stheraven	LIST_INIT(&mp->geom);
96233235Stheraven	LIST_INSERT_HEAD(&g_classes, mp, class);
97233235Stheraven	if (mp->init != NULL)
98233235Stheraven		mp->init(mp);
99233235Stheraven	if (mp->taste == NULL)
100233235Stheraven		return;
101233235Stheraven	LIST_FOREACH(mp2, &g_classes, class) {
102233235Stheraven		if (mp == mp2)
103233235Stheraven			continue;
104233235Stheraven		LIST_FOREACH(gp, &mp2->geom, geom) {
105233235Stheraven			LIST_FOREACH(pp, &gp->provider, provider) {
106233235Stheraven				mp->taste(mp, pp, 0);
107233235Stheraven				g_topology_assert();
108233235Stheraven			}
109233235Stheraven		}
110233235Stheraven	}
111233235Stheraven}
112233235Stheraven
113227825Stheravenstatic void
114227825Stheraveng_unload_class(void *arg, int flag)
115227825Stheraven{
116227825Stheraven	struct g_hh00 *hh;
117227825Stheraven	struct g_class *mp;
118227825Stheraven	struct g_geom *gp;
119227825Stheraven	struct g_provider *pp;
120227825Stheraven	struct g_consumer *cp;
121227825Stheraven	int error;
122227825Stheraven
123227825Stheraven	g_topology_assert();
124227825Stheraven	hh = arg;
125227825Stheraven	mp = hh->mp;
126227825Stheraven	G_VALID_CLASS(mp);
127227825Stheraven	g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
128227825Stheraven
129227825Stheraven	/*
130227825Stheraven	 * We allow unloading if we have no geoms, or a class
131227825Stheraven	 * method we can use to get rid of them.
132233235Stheraven	 */
133227825Stheraven	if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
134227825Stheraven		hh->error = EOPNOTSUPP;
135227825Stheraven		return;
136227825Stheraven	}
137227825Stheraven
138227825Stheraven	/* We refuse to unload if anything is open */
139227825Stheraven	LIST_FOREACH(gp, &mp->geom, geom) {
140227825Stheraven		LIST_FOREACH(pp, &gp->provider, provider)
141227825Stheraven			if (pp->acr || pp->acw || pp->ace) {
142227825Stheraven				hh->error = EBUSY;
143227825Stheraven				return;
144227825Stheraven			}
145227825Stheraven		LIST_FOREACH(cp, &gp->consumer, consumer)
146227825Stheraven			if (cp->acr || cp->acw || cp->ace) {
147227825Stheraven				hh->error = EBUSY;
148227825Stheraven				return;
149227825Stheraven			}
150227825Stheraven	}
151233235Stheraven
152233235Stheraven	/* Bar new entries */
153233235Stheraven	mp->taste = NULL;
154233235Stheraven	mp->config = NULL;
155233235Stheraven
156233235Stheraven	error = 0;
157227825Stheraven	for (;;) {
158227825Stheraven		gp = LIST_FIRST(&mp->geom);
159227825Stheraven		if (gp == NULL)
160227825Stheraven			break;
161227825Stheraven		error = mp->destroy_geom(NULL, mp, gp);
162227825Stheraven		if (error != 0)
163227825Stheraven			break;
164227825Stheraven	}
165227825Stheraven	if (error == 0) {
166227825Stheraven		if (mp->fini != NULL)
167233235Stheraven			mp->fini(mp);
168233235Stheraven		LIST_REMOVE(mp, class);
169233235Stheraven	}
170227825Stheraven	hh->error = error;
171227825Stheraven	return;
172227825Stheraven}
173227825Stheraven
174227825Stheravenint
175227825Stheraveng_modevent(module_t mod, int type, void *data)
176227825Stheraven{
177227825Stheraven	struct g_hh00 *hh;
178227825Stheraven	int error;
179227825Stheraven	static int g_ignition;
180227825Stheraven	struct g_class *mp;
181227825Stheraven
182227825Stheraven	mp = data;
183227825Stheraven	if (mp->version != 0 && mp->version != G_VERSION) {
184227825Stheraven		printf("GEOM class %s has Wrong version %x\n",
185227825Stheraven		    mp->name, mp->version);
186227825Stheraven		return (EINVAL);
187227825Stheraven	}
188227825Stheraven	if (!g_ignition) {
189227825Stheraven		g_ignition++;
190227825Stheraven		g_init();
191227825Stheraven	}
192227825Stheraven	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
193227825Stheraven	hh->mp = data;
194227825Stheraven	error = EOPNOTSUPP;
195227825Stheraven	switch (type) {
196227825Stheraven	case MOD_LOAD:
197227825Stheraven		g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name);
198227825Stheraven		g_post_event(g_load_class, hh, M_WAITOK, NULL);
199227825Stheraven		error = 0;
200227825Stheraven		break;
201227825Stheraven	case MOD_UNLOAD:
202227825Stheraven		g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name);
203227825Stheraven		error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL);
204227825Stheraven		if (error == 0)
205227825Stheraven			error = hh->error;
206227825Stheraven		if (error == 0) {
207227825Stheraven			g_waitidle();
208227825Stheraven			KASSERT(LIST_EMPTY(&hh->mp->geom),
209227825Stheraven			    ("Unloaded class (%s) still has geom", hh->mp->name));
210227825Stheraven		}
211227825Stheraven		g_free(hh);
212227825Stheraven		break;
213227825Stheraven	default:
214227825Stheraven		g_free(hh);
215227825Stheraven		break;
216227825Stheraven	}
217227825Stheraven	return (error);
218227825Stheraven}
219227825Stheraven
220227825Stheravenstruct g_geom *
221227825Stheraveng_new_geomf(struct g_class *mp, const char *fmt, ...)
222227825Stheraven{
223227825Stheraven	struct g_geom *gp;
224227825Stheraven	va_list ap;
225227825Stheraven	struct sbuf *sb;
226227825Stheraven
227227825Stheraven	g_topology_assert();
228227825Stheraven	G_VALID_CLASS(mp);
229227825Stheraven	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
230227825Stheraven	va_start(ap, fmt);
231227825Stheraven	sbuf_vprintf(sb, fmt, ap);
232227825Stheraven	va_end(ap);
233227825Stheraven	sbuf_finish(sb);
234227825Stheraven	gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
235227825Stheraven	gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
236227825Stheraven	gp->class = mp;
237227825Stheraven	gp->rank = 1;
238227825Stheraven	LIST_INIT(&gp->consumer);
239227825Stheraven	LIST_INIT(&gp->provider);
240227825Stheraven	LIST_INSERT_HEAD(&mp->geom, gp, geom);
241227825Stheraven	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
242227825Stheraven	strcpy(gp->name, sbuf_data(sb));
243227825Stheraven	sbuf_delete(sb);
244227825Stheraven	/* Fill in defaults from class */
245227825Stheraven	gp->start = mp->start;
246227825Stheraven	gp->spoiled = mp->spoiled;
247227825Stheraven	gp->dumpconf = mp->dumpconf;
248227825Stheraven	gp->access = mp->access;
249227825Stheraven	gp->orphan = mp->orphan;
250233235Stheraven	gp->ioctl = mp->ioctl;
251233235Stheraven	return (gp);
252233235Stheraven}
253227825Stheraven
254227825Stheravenvoid
255227825Stheraveng_destroy_geom(struct g_geom *gp)
256227825Stheraven{
257227825Stheraven
258227825Stheraven	g_topology_assert();
259227825Stheraven	G_VALID_GEOM(gp);
260227825Stheraven	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
261227825Stheraven	KASSERT(LIST_EMPTY(&gp->consumer),
262227825Stheraven	    ("g_destroy_geom(%s) with consumer(s) [%p]",
263227825Stheraven	    gp->name, LIST_FIRST(&gp->consumer)));
264227825Stheraven	KASSERT(LIST_EMPTY(&gp->provider),
265227825Stheraven	    ("g_destroy_geom(%s) with provider(s) [%p]",
266227825Stheraven	    gp->name, LIST_FIRST(&gp->provider)));
267227825Stheraven	g_cancel_event(gp);
268227825Stheraven	LIST_REMOVE(gp, geom);
269227825Stheraven	TAILQ_REMOVE(&geoms, gp, geoms);
270227825Stheraven	g_free(gp->name);
271227825Stheraven	g_free(gp);
272227825Stheraven}
273227825Stheraven
274227825Stheraven/*
275227825Stheraven * This function is called (repeatedly) until the has withered away.
276227825Stheraven */
277227825Stheravenvoid
278227825Stheraveng_wither_geom(struct g_geom *gp, int error)
279227825Stheraven{
280227825Stheraven	struct g_provider *pp;
281227825Stheraven
282227825Stheraven	g_topology_assert();
283227825Stheraven	G_VALID_GEOM(gp);
284227825Stheraven	g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
285227825Stheraven	if (!(gp->flags & G_GEOM_WITHER)) {
286227825Stheraven		gp->flags |= G_GEOM_WITHER;
287233235Stheraven		LIST_FOREACH(pp, &gp->provider, provider)
288233235Stheraven			if (!(pp->flags & G_PF_ORPHAN))
289233235Stheraven				g_orphan_provider(pp, error);
290233235Stheraven	}
291227825Stheraven	g_do_wither();
292227825Stheraven}
293227825Stheraven
294227825Stheraven/*
295227825Stheraven * This function is called (repeatedly) until we cant wash away more
296227825Stheraven * withered bits at present.  Return value contains two bits.  Bit 0
297227825Stheraven * set means "withering stuff we can't wash now", bit 1 means "call
298227825Stheraven * me again, there may be stuff I didn't get the first time around.
299227825Stheraven */
300227825Stheravenint
301227825Stheraveng_wither_washer()
302227825Stheraven{
303227825Stheraven	struct g_class *mp;
304227825Stheraven	struct g_geom *gp, *gp2;
305227825Stheraven	struct g_provider *pp, *pp2;
306227825Stheraven	struct g_consumer *cp, *cp2;
307227825Stheraven	int result;
308227825Stheraven
309227825Stheraven	result = 0;
310227825Stheraven	g_topology_assert();
311227825Stheraven	LIST_FOREACH(mp, &g_classes, class) {
312227825Stheraven		LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
313227825Stheraven			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
314				if (!(pp->flags & G_PF_WITHER))
315					continue;
316				if (LIST_EMPTY(&pp->consumers))
317					g_destroy_provider(pp);
318				else
319					result |= 1;
320			}
321			if (!(gp->flags & G_GEOM_WITHER))
322				continue;
323			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
324				if (LIST_EMPTY(&pp->consumers))
325					g_destroy_provider(pp);
326				else
327					result |= 1;
328			}
329			LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
330				if (cp->acr || cp->acw || cp->ace) {
331					result |= 1;
332					continue;
333				}
334				if (cp->provider != NULL)
335					g_detach(cp);
336				g_destroy_consumer(cp);
337				result |= 2;
338			}
339			if (LIST_EMPTY(&gp->provider) &&
340			    LIST_EMPTY(&gp->consumer))
341				g_destroy_geom(gp);
342			else
343				result |= 1;
344		}
345	}
346	return (result);
347}
348
349struct g_consumer *
350g_new_consumer(struct g_geom *gp)
351{
352	struct g_consumer *cp;
353
354	g_topology_assert();
355	G_VALID_GEOM(gp);
356	KASSERT(!(gp->flags & G_GEOM_WITHER),
357	    ("g_new_consumer on WITHERing geom(%s) (class %s)",
358	    gp->name, gp->class->name));
359	KASSERT(gp->orphan != NULL,
360	    ("g_new_consumer on geom(%s) (class %s) without orphan",
361	    gp->name, gp->class->name));
362
363	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
364	cp->geom = gp;
365	cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
366	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
367	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
368	return(cp);
369}
370
371void
372g_destroy_consumer(struct g_consumer *cp)
373{
374	struct g_geom *gp;
375
376	g_topology_assert();
377	G_VALID_CONSUMER(cp);
378	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
379	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
380	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
381	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
382	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
383	g_cancel_event(cp);
384	gp = cp->geom;
385	LIST_REMOVE(cp, consumer);
386	devstat_remove_entry(cp->stat);
387	g_free(cp);
388	if (gp->flags & G_GEOM_WITHER)
389		g_do_wither();
390}
391
392static void
393g_new_provider_event(void *arg, int flag)
394{
395	struct g_class *mp;
396	struct g_provider *pp;
397	struct g_consumer *cp;
398	int i;
399
400	g_topology_assert();
401	if (flag == EV_CANCEL)
402		return;
403	if (g_shutdown)
404		return;
405	pp = arg;
406	G_VALID_PROVIDER(pp);
407	LIST_FOREACH(mp, &g_classes, class) {
408		if (mp->taste == NULL)
409			continue;
410		i = 1;
411		LIST_FOREACH(cp, &pp->consumers, consumers)
412			if (cp->geom->class == mp)
413				i = 0;
414		if (!i)
415			continue;
416		mp->taste(mp, pp, 0);
417		g_topology_assert();
418	}
419}
420
421
422struct g_provider *
423g_new_providerf(struct g_geom *gp, const char *fmt, ...)
424{
425	struct g_provider *pp;
426	struct sbuf *sb;
427	va_list ap;
428
429	g_topology_assert();
430	G_VALID_GEOM(gp);
431	KASSERT(gp->access != NULL,
432	    ("new provider on geom(%s) without ->access (class %s)",
433	    gp->name, gp->class->name));
434	KASSERT(gp->start != NULL,
435	    ("new provider on geom(%s) without ->start (class %s)",
436	    gp->name, gp->class->name));
437	KASSERT(!(gp->flags & G_GEOM_WITHER),
438	    ("new provider on WITHERing geom(%s) (class %s)",
439	    gp->name, gp->class->name));
440	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
441	va_start(ap, fmt);
442	sbuf_vprintf(sb, fmt, ap);
443	va_end(ap);
444	sbuf_finish(sb);
445	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
446	pp->name = (char *)(pp + 1);
447	strcpy(pp->name, sbuf_data(sb));
448	sbuf_delete(sb);
449	LIST_INIT(&pp->consumers);
450	pp->error = ENXIO;
451	pp->geom = gp;
452	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
453	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
454	LIST_INSERT_HEAD(&gp->provider, pp, provider);
455	g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
456	return (pp);
457}
458
459void
460g_error_provider(struct g_provider *pp, int error)
461{
462
463	/* G_VALID_PROVIDER(pp);  We may not have g_topology */
464	pp->error = error;
465}
466
467struct g_provider *
468g_provider_by_name(char const *arg)
469{
470	struct g_class *cp;
471	struct g_geom *gp;
472	struct g_provider *pp;
473
474	LIST_FOREACH(cp, &g_classes, class) {
475		LIST_FOREACH(gp, &cp->geom, geom) {
476			LIST_FOREACH(pp, &gp->provider, provider) {
477				if (!strcmp(arg, pp->name))
478					return (pp);
479			}
480		}
481	}
482	return (NULL);
483}
484
485void
486g_destroy_provider(struct g_provider *pp)
487{
488	struct g_geom *gp;
489
490	g_topology_assert();
491	G_VALID_PROVIDER(pp);
492	KASSERT(LIST_EMPTY(&pp->consumers),
493	    ("g_destroy_provider but attached"));
494	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
495	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
496	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
497	g_cancel_event(pp);
498	LIST_REMOVE(pp, provider);
499	gp = pp->geom;
500	devstat_remove_entry(pp->stat);
501	g_free(pp);
502	if ((gp->flags & G_GEOM_WITHER))
503		g_do_wither();
504}
505
506/*
507 * We keep the "geoms" list sorted by topological order (== increasing
508 * numerical rank) at all times.
509 * When an attach is done, the attaching geoms rank is invalidated
510 * and it is moved to the tail of the list.
511 * All geoms later in the sequence has their ranks reevaluated in
512 * sequence.  If we cannot assign rank to a geom because it's
513 * prerequisites do not have rank, we move that element to the tail
514 * of the sequence with invalid rank as well.
515 * At some point we encounter our original geom and if we stil fail
516 * to assign it a rank, there must be a loop and we fail back to
517 * g_attach() which detach again and calls redo_rank again
518 * to fix up the damage.
519 * It would be much simpler code wise to do it recursively, but we
520 * can't risk that on the kernel stack.
521 */
522
523static int
524redo_rank(struct g_geom *gp)
525{
526	struct g_consumer *cp;
527	struct g_geom *gp1, *gp2;
528	int n, m;
529
530	g_topology_assert();
531	G_VALID_GEOM(gp);
532
533	/* Invalidate this geoms rank and move it to the tail */
534	gp1 = TAILQ_NEXT(gp, geoms);
535	if (gp1 != NULL) {
536		gp->rank = 0;
537		TAILQ_REMOVE(&geoms, gp, geoms);
538		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
539	} else {
540		gp1 = gp;
541	}
542
543	/* re-rank the rest of the sequence */
544	for (; gp1 != NULL; gp1 = gp2) {
545		gp1->rank = 0;
546		m = 1;
547		LIST_FOREACH(cp, &gp1->consumer, consumer) {
548			if (cp->provider == NULL)
549				continue;
550			n = cp->provider->geom->rank;
551			if (n == 0) {
552				m = 0;
553				break;
554			} else if (n >= m)
555				m = n + 1;
556		}
557		gp1->rank = m;
558		gp2 = TAILQ_NEXT(gp1, geoms);
559
560		/* got a rank, moving on */
561		if (m != 0)
562			continue;
563
564		/* no rank to original geom means loop */
565		if (gp == gp1)
566			return (ELOOP);
567
568		/* no rank, put it at the end move on */
569		TAILQ_REMOVE(&geoms, gp1, geoms);
570		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
571	}
572	return (0);
573}
574
575int
576g_attach(struct g_consumer *cp, struct g_provider *pp)
577{
578	int error;
579
580	g_topology_assert();
581	G_VALID_CONSUMER(cp);
582	G_VALID_PROVIDER(pp);
583	KASSERT(cp->provider == NULL, ("attach but attached"));
584	cp->provider = pp;
585	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
586	error = redo_rank(cp->geom);
587	if (error) {
588		LIST_REMOVE(cp, consumers);
589		cp->provider = NULL;
590		redo_rank(cp->geom);
591	}
592	return (error);
593}
594
595void
596g_detach(struct g_consumer *cp)
597{
598	struct g_provider *pp;
599
600	g_topology_assert();
601	G_VALID_CONSUMER(cp);
602	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
603	KASSERT(cp->provider != NULL, ("detach but not attached"));
604	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
605	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
606	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
607	KASSERT(cp->nstart == cp->nend,
608	    ("detach with active requests"));
609	pp = cp->provider;
610	LIST_REMOVE(cp, consumers);
611	cp->provider = NULL;
612	if (pp->geom->flags & G_GEOM_WITHER)
613		g_do_wither();
614	else if (pp->flags & G_PF_WITHER)
615		g_do_wither();
616	redo_rank(cp->geom);
617}
618
619/*
620 * g_access()
621 *
622 * Access-check with delta values.  The question asked is "can provider
623 * "cp" change the access counters by the relative amounts dc[rwe] ?"
624 */
625
626int
627g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
628{
629	struct g_provider *pp;
630	int pr,pw,pe;
631	int error;
632
633	g_topology_assert();
634	G_VALID_CONSUMER(cp);
635	pp = cp->provider;
636	KASSERT(pp != NULL, ("access but not attached"));
637	G_VALID_PROVIDER(pp);
638
639	g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
640	    cp, pp->name, dcr, dcw, dce);
641
642	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
643	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
644	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
645	KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
646	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
647
648	/*
649	 * If our class cares about being spoiled, and we have been, we
650	 * are probably just ahead of the event telling us that.  Fail
651	 * now rather than having to unravel this later.
652	 */
653	if (cp->geom->spoiled != NULL && cp->spoiled &&
654	    (dcr > 0 || dcw > 0 || dce > 0))
655		return (ENXIO);
656
657	/*
658	 * Figure out what counts the provider would have had, if this
659	 * consumer had (r0w0e0) at this time.
660	 */
661	pr = pp->acr - cp->acr;
662	pw = pp->acw - cp->acw;
663	pe = pp->ace - cp->ace;
664
665	g_trace(G_T_ACCESS,
666    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
667	    dcr, dcw, dce,
668	    cp->acr, cp->acw, cp->ace,
669	    pp->acr, pp->acw, pp->ace,
670	    pp, pp->name);
671
672	/* If foot-shooting is enabled, any open on rank#1 is OK */
673	if ((g_debugflags & 16) && pp->geom->rank == 1)
674		;
675	/* If we try exclusive but already write: fail */
676	else if (dce > 0 && pw > 0)
677		return (EPERM);
678	/* If we try write but already exclusive: fail */
679	else if (dcw > 0 && pe > 0)
680		return (EPERM);
681	/* If we try to open more but provider is error'ed: fail */
682	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
683		return (pp->error);
684
685	/* Ok then... */
686
687	error = pp->geom->access(pp, dcr, dcw, dce);
688	KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
689	    ("Geom provider %s::%s failed closing ->access()",
690	    pp->geom->class->name, pp->name));
691	if (!error) {
692		/*
693		 * If we open first write, spoil any partner consumers.
694		 * If we close last write, trigger re-taste.
695		 */
696		if (pp->acw == 0 && dcw != 0)
697			g_spoil(pp, cp);
698		else if (pp->acw != 0 && pp->acw == -dcw &&
699		    !(pp->geom->flags & G_GEOM_WITHER))
700			g_post_event(g_new_provider_event, pp, M_WAITOK,
701			    pp, NULL);
702
703		pp->acr += dcr;
704		pp->acw += dcw;
705		pp->ace += dce;
706		cp->acr += dcr;
707		cp->acw += dcw;
708		cp->ace += dce;
709	}
710	return (error);
711}
712
713int
714g_handleattr_int(struct bio *bp, const char *attribute, int val)
715{
716
717	return (g_handleattr(bp, attribute, &val, sizeof val));
718}
719
720int
721g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
722{
723
724	return (g_handleattr(bp, attribute, &val, sizeof val));
725}
726
727int
728g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
729{
730	int error;
731
732	if (strcmp(bp->bio_attribute, attribute))
733		return (0);
734	if (bp->bio_length != len) {
735		printf("bio_length %jd len %d -> EFAULT\n",
736		    (intmax_t)bp->bio_length, len);
737		error = EFAULT;
738	} else {
739		error = 0;
740		bcopy(val, bp->bio_data, len);
741		bp->bio_completed = len;
742	}
743	g_io_deliver(bp, error);
744	return (1);
745}
746
747int
748g_std_access(struct g_provider *pp,
749	int dr __unused, int dw __unused, int de __unused)
750{
751
752	g_topology_assert();
753	G_VALID_PROVIDER(pp);
754        return (0);
755}
756
757void
758g_std_done(struct bio *bp)
759{
760	struct bio *bp2;
761
762	bp2 = bp->bio_parent;
763	if (bp2->bio_error == 0)
764		bp2->bio_error = bp->bio_error;
765	bp2->bio_completed += bp->bio_completed;
766	g_destroy_bio(bp);
767	bp2->bio_inbed++;
768	if (bp2->bio_children == bp2->bio_inbed)
769		g_io_deliver(bp2, bp2->bio_error);
770}
771
772/* XXX: maybe this is only g_slice_spoiled */
773
774void
775g_std_spoiled(struct g_consumer *cp)
776{
777	struct g_geom *gp;
778	struct g_provider *pp;
779
780	g_topology_assert();
781	G_VALID_CONSUMER(cp);
782	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
783	g_detach(cp);
784	gp = cp->geom;
785	LIST_FOREACH(pp, &gp->provider, provider)
786		g_orphan_provider(pp, ENXIO);
787	g_destroy_consumer(cp);
788	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
789		g_destroy_geom(gp);
790	else
791		gp->flags |= G_GEOM_WITHER;
792}
793
794/*
795 * Spoiling happens when a provider is opened for writing, but consumers
796 * which are configured by in-band data are attached (slicers for instance).
797 * Since the write might potentially change the in-band data, such consumers
798 * need to re-evaluate their existence after the writing session closes.
799 * We do this by (offering to) tear them down when the open for write happens
800 * in return for a re-taste when it closes again.
801 * Together with the fact that such consumers grab an 'e' bit whenever they
802 * are open, regardless of mode, this ends up DTRT.
803 */
804
805static void
806g_spoil_event(void *arg, int flag)
807{
808	struct g_provider *pp;
809	struct g_consumer *cp, *cp2;
810
811	g_topology_assert();
812	if (flag == EV_CANCEL)
813		return;
814	pp = arg;
815	G_VALID_PROVIDER(pp);
816	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
817		cp2 = LIST_NEXT(cp, consumers);
818		if (!cp->spoiled)
819			continue;
820		cp->spoiled = 0;
821		if (cp->geom->spoiled == NULL)
822			continue;
823		cp->geom->spoiled(cp);
824		g_topology_assert();
825	}
826}
827
828void
829g_spoil(struct g_provider *pp, struct g_consumer *cp)
830{
831	struct g_consumer *cp2;
832
833	g_topology_assert();
834	G_VALID_PROVIDER(pp);
835	G_VALID_CONSUMER(cp);
836
837	LIST_FOREACH(cp2, &pp->consumers, consumers) {
838		if (cp2 == cp)
839			continue;
840/*
841		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
842		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
843*/
844		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
845		cp2->spoiled++;
846	}
847	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
848}
849
850int
851g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
852{
853	int error, i;
854
855	i = len;
856	error = g_io_getattr(attr, cp, &i, var);
857	if (error)
858		return (error);
859	if (i != len)
860		return (EINVAL);
861	return (0);
862}
863
864#ifdef DIAGNOSTIC
865/*
866 * This function walks (topologically unsafely) the mesh and return a
867 * non-zero integer if it finds the argument pointer is an object.
868 * The return value indicates which type of object it is belived to be.
869 * If topology is not locked, this function is potentially dangerous,
870 * but since it is for debugging purposes and can be useful for instance
871 * from DDB, we do not assert topology lock is held.
872 */
873int
874g_valid_obj(void const *ptr)
875{
876	struct g_class *mp;
877	struct g_geom *gp;
878	struct g_consumer *cp;
879	struct g_provider *pp;
880
881	LIST_FOREACH(mp, &g_classes, class) {
882		if (ptr == mp)
883			return (1);
884		LIST_FOREACH(gp, &mp->geom, geom) {
885			if (ptr == gp)
886				return (2);
887			LIST_FOREACH(cp, &gp->consumer, consumer)
888				if (ptr == cp)
889					return (3);
890			LIST_FOREACH(pp, &gp->provider, provider)
891				if (ptr == pp)
892					return (4);
893		}
894	}
895	return(0);
896}
897#endif
898