geom_subr.c revision 152565
1204591Sluigi/*-
2204591Sluigi * Copyright (c) 2002 Poul-Henning Kamp
3204591Sluigi * Copyright (c) 2002 Networks Associates Technology, Inc.
4204591Sluigi * All rights reserved.
5204591Sluigi *
6204591Sluigi * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7204591Sluigi * and NAI Labs, the Security Research Division of Network Associates, Inc.
8204591Sluigi * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9204591Sluigi * DARPA CHATS research program.
10204591Sluigi *
11204591Sluigi * Redistribution and use in source and binary forms, with or without
12204591Sluigi * modification, are permitted provided that the following conditions
13204591Sluigi * are met:
14204591Sluigi * 1. Redistributions of source code must retain the above copyright
15204591Sluigi *    notice, this list of conditions and the following disclaimer.
16204591Sluigi * 2. Redistributions in binary form must reproduce the above copyright
17204591Sluigi *    notice, this list of conditions and the following disclaimer in the
18204591Sluigi *    documentation and/or other materials provided with the distribution.
19204591Sluigi * 3. The names of the authors may not be used to endorse or promote
20204591Sluigi *    products derived from this software without specific prior written
21204591Sluigi *    permission.
22204591Sluigi *
23204591Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24204591Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25204591Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26204591Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27204591Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28204591Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29204591Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30204591Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31204591Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32204591Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33204591Sluigi * SUCH DAMAGE.
34204591Sluigi */
35204591Sluigi
36204591Sluigi#include <sys/cdefs.h>
37204591Sluigi__FBSDID("$FreeBSD: head/sys/geom/geom_subr.c 152565 2005-11-18 02:43:49Z jdp $");
38204591Sluigi
39204591Sluigi#include <sys/param.h>
40240494Sglebius#include <sys/systm.h>
41204591Sluigi#include <sys/devicestat.h>
42204591Sluigi#include <sys/kernel.h>
43204591Sluigi#include <sys/malloc.h>
44204591Sluigi#include <sys/bio.h>
45204591Sluigi#include <sys/sysctl.h>
46204591Sluigi#include <sys/proc.h>
47204591Sluigi#include <sys/kthread.h>
48204591Sluigi#include <sys/lock.h>
49204591Sluigi#include <sys/mutex.h>
50204591Sluigi#include <sys/errno.h>
51204591Sluigi#include <sys/sbuf.h>
52204591Sluigi#include <geom/geom.h>
53204591Sluigi#include <geom/geom_int.h>
54204591Sluigi#include <machine/stdarg.h>
55204591Sluigi
56204591Sluigistruct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
57204591Sluigistatic struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
58204591Sluigichar *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
59204591Sluigi
60204591Sluigistruct g_hh00 {
61204591Sluigi	struct g_class	*mp;
62227293Sed	int		error;
63204591Sluigi	int		post;
64204591Sluigi};
65204591Sluigi
66204591Sluigi/*
67204591Sluigi * This event offers a new class a chance to taste all preexisting providers.
68204591Sluigi */
69204591Sluigistatic void
70204591Sluigig_load_class(void *arg, int flag)
71204591Sluigi{
72204591Sluigi	struct g_hh00 *hh;
73204591Sluigi	struct g_class *mp2, *mp;
74204591Sluigi	struct g_geom *gp;
75204591Sluigi	struct g_provider *pp;
76204591Sluigi
77204591Sluigi	g_topology_assert();
78204591Sluigi	if (flag == EV_CANCEL)	/* XXX: can't happen ? */
79204591Sluigi		return;
80204591Sluigi	if (g_shutdown)
81204591Sluigi		return;
82204591Sluigi
83204591Sluigi	hh = arg;
84204591Sluigi	mp = hh->mp;
85204591Sluigi	hh->error = 0;
86204591Sluigi	if (hh->post) {
87204591Sluigi		g_free(hh);
88204591Sluigi		hh = NULL;
89204591Sluigi	}
90204591Sluigi	g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
91204591Sluigi	KASSERT(mp->name != NULL && *mp->name != '\0',
92204591Sluigi	    ("GEOM class has no name"));
93204591Sluigi	LIST_FOREACH(mp2, &g_classes, class) {
94204591Sluigi		if (mp2 == mp) {
95204591Sluigi			printf("The GEOM class %s is already loaded.\n",
96204591Sluigi			    mp2->name);
97204591Sluigi			if (hh != NULL)
98204591Sluigi				hh->error = EEXIST;
99204591Sluigi			return;
100204591Sluigi		} else if (strcmp(mp2->name, mp->name) == 0) {
101204591Sluigi			printf("A GEOM class %s is already loaded.\n",
102204591Sluigi			    mp2->name);
103204591Sluigi			if (hh != NULL)
104204591Sluigi				hh->error = EEXIST;
105204591Sluigi			return;
106204591Sluigi		}
107204591Sluigi	}
108204591Sluigi
109204591Sluigi	LIST_INIT(&mp->geom);
110204591Sluigi	LIST_INSERT_HEAD(&g_classes, mp, class);
111204591Sluigi	if (mp->init != NULL)
112204591Sluigi		mp->init(mp);
113204591Sluigi	if (mp->taste == NULL)
114204591Sluigi		return;
115204591Sluigi	LIST_FOREACH(mp2, &g_classes, class) {
116204591Sluigi		if (mp == mp2)
117204591Sluigi			continue;
118204591Sluigi		LIST_FOREACH(gp, &mp2->geom, geom) {
119204591Sluigi			LIST_FOREACH(pp, &gp->provider, provider) {
120204591Sluigi				mp->taste(mp, pp, 0);
121204591Sluigi				g_topology_assert();
122204591Sluigi			}
123204591Sluigi		}
124204591Sluigi	}
125204591Sluigi}
126204591Sluigi
127204591Sluigistatic void
128204591Sluigig_unload_class(void *arg, int flag)
129204591Sluigi{
130204591Sluigi	struct g_hh00 *hh;
131204591Sluigi	struct g_class *mp;
132204591Sluigi	struct g_geom *gp;
133204591Sluigi	struct g_provider *pp;
134204591Sluigi	struct g_consumer *cp;
135204591Sluigi	int error;
136204591Sluigi
137204591Sluigi	g_topology_assert();
138204591Sluigi	hh = arg;
139204591Sluigi	mp = hh->mp;
140204591Sluigi	G_VALID_CLASS(mp);
141204591Sluigi	g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
142204591Sluigi
143204591Sluigi	/*
144204591Sluigi	 * We allow unloading if we have no geoms, or a class
145204591Sluigi	 * method we can use to get rid of them.
146204591Sluigi	 */
147204591Sluigi	if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
148204591Sluigi		hh->error = EOPNOTSUPP;
149204591Sluigi		return;
150204591Sluigi	}
151204591Sluigi
152204591Sluigi	/* We refuse to unload if anything is open */
153204591Sluigi	LIST_FOREACH(gp, &mp->geom, geom) {
154204591Sluigi		LIST_FOREACH(pp, &gp->provider, provider)
155204591Sluigi			if (pp->acr || pp->acw || pp->ace) {
156204591Sluigi				hh->error = EBUSY;
157204591Sluigi				return;
158204591Sluigi			}
159204591Sluigi		LIST_FOREACH(cp, &gp->consumer, consumer)
160204591Sluigi			if (cp->acr || cp->acw || cp->ace) {
161204591Sluigi				hh->error = EBUSY;
162204591Sluigi				return;
163204591Sluigi			}
164204591Sluigi	}
165204591Sluigi
166204591Sluigi	/* Bar new entries */
167204591Sluigi	mp->taste = NULL;
168204591Sluigi	mp->config = NULL;
169204591Sluigi
170204591Sluigi	error = 0;
171204591Sluigi	for (;;) {
172204591Sluigi		gp = LIST_FIRST(&mp->geom);
173204591Sluigi		if (gp == NULL)
174204591Sluigi			break;
175204591Sluigi		error = mp->destroy_geom(NULL, mp, gp);
176204591Sluigi		if (error != 0)
177204591Sluigi			break;
178204591Sluigi	}
179204591Sluigi	if (error == 0) {
180204591Sluigi		if (mp->fini != NULL)
181204591Sluigi			mp->fini(mp);
182204591Sluigi		LIST_REMOVE(mp, class);
183204591Sluigi	}
184204591Sluigi	hh->error = error;
185204591Sluigi	return;
186204591Sluigi}
187204591Sluigi
188204591Sluigiint
189204591Sluigig_modevent(module_t mod, int type, void *data)
190204591Sluigi{
191204591Sluigi	struct g_hh00 *hh;
192204591Sluigi	int error;
193204591Sluigi	static int g_ignition;
194204591Sluigi	struct g_class *mp;
195204591Sluigi
196204591Sluigi	mp = data;
197204591Sluigi	if (mp->version != G_VERSION) {
198204591Sluigi		printf("GEOM class %s has Wrong version %x\n",
199204591Sluigi		    mp->name, mp->version);
200204591Sluigi		return (EINVAL);
201204591Sluigi	}
202204591Sluigi	if (!g_ignition) {
203204591Sluigi		g_ignition++;
204204591Sluigi		g_init();
205204591Sluigi	}
206204591Sluigi	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
207204591Sluigi	hh->mp = data;
208204591Sluigi	error = EOPNOTSUPP;
209204591Sluigi	switch (type) {
210204591Sluigi	case MOD_LOAD:
211204591Sluigi		g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name);
212204591Sluigi		/*
213204591Sluigi		 * Once the system is not cold, MOD_LOAD calls will be
214204591Sluigi		 * from the userland and the g_event thread will be able
215204591Sluigi		 * to acknowledge their completion.
216204591Sluigi		 */
217204591Sluigi		if (cold) {
218204591Sluigi			hh->post = 1;
219204591Sluigi			error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
220204591Sluigi		} else {
221204591Sluigi			error = g_waitfor_event(g_load_class, hh, M_WAITOK,
222204591Sluigi			    NULL);
223204591Sluigi			if (error == 0)
224204591Sluigi				error = hh->error;
225204591Sluigi			g_free(hh);
226204591Sluigi		}
227204591Sluigi		break;
228204591Sluigi	case MOD_UNLOAD:
229204591Sluigi		g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name);
230204591Sluigi		error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL);
231204591Sluigi		if (error == 0)
232204591Sluigi			error = hh->error;
233204591Sluigi		if (error == 0) {
234204591Sluigi			KASSERT(LIST_EMPTY(&hh->mp->geom),
235204591Sluigi			    ("Unloaded class (%s) still has geom", hh->mp->name));
236204591Sluigi		}
237204591Sluigi		g_free(hh);
238204591Sluigi		break;
239204591Sluigi	default:
240204591Sluigi		g_free(hh);
241204591Sluigi		break;
242204591Sluigi	}
243204591Sluigi	return (error);
244204591Sluigi}
245204591Sluigi
246204591Sluigistruct g_geom *
247204591Sluigig_new_geomf(struct g_class *mp, const char *fmt, ...)
248204591Sluigi{
249204591Sluigi	struct g_geom *gp;
250204591Sluigi	va_list ap;
251204591Sluigi	struct sbuf *sb;
252204591Sluigi
253204591Sluigi	g_topology_assert();
254204591Sluigi	G_VALID_CLASS(mp);
255204591Sluigi	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
256204591Sluigi	va_start(ap, fmt);
257204591Sluigi	sbuf_vprintf(sb, fmt, ap);
258204591Sluigi	va_end(ap);
259204591Sluigi	sbuf_finish(sb);
260204591Sluigi	gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
261204591Sluigi	gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
262204591Sluigi	gp->class = mp;
263204591Sluigi	gp->rank = 1;
264204591Sluigi	LIST_INIT(&gp->consumer);
265204591Sluigi	LIST_INIT(&gp->provider);
266204591Sluigi	LIST_INSERT_HEAD(&mp->geom, gp, geom);
267204591Sluigi	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
268204591Sluigi	strcpy(gp->name, sbuf_data(sb));
269204591Sluigi	sbuf_delete(sb);
270204591Sluigi	/* Fill in defaults from class */
271204591Sluigi	gp->start = mp->start;
272204591Sluigi	gp->spoiled = mp->spoiled;
273204591Sluigi	gp->dumpconf = mp->dumpconf;
274204591Sluigi	gp->access = mp->access;
275204591Sluigi	gp->orphan = mp->orphan;
276204591Sluigi	gp->ioctl = mp->ioctl;
277204591Sluigi	return (gp);
278204591Sluigi}
279204591Sluigi
280204591Sluigivoid
281204591Sluigig_destroy_geom(struct g_geom *gp)
282204591Sluigi{
283204591Sluigi
284204591Sluigi	g_topology_assert();
285204591Sluigi	G_VALID_GEOM(gp);
286204591Sluigi	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
287204591Sluigi	KASSERT(LIST_EMPTY(&gp->consumer),
288204591Sluigi	    ("g_destroy_geom(%s) with consumer(s) [%p]",
289204591Sluigi	    gp->name, LIST_FIRST(&gp->consumer)));
290204591Sluigi	KASSERT(LIST_EMPTY(&gp->provider),
291204591Sluigi	    ("g_destroy_geom(%s) with provider(s) [%p]",
292204591Sluigi	    gp->name, LIST_FIRST(&gp->provider)));
293204591Sluigi	g_cancel_event(gp);
294204591Sluigi	LIST_REMOVE(gp, geom);
295204591Sluigi	TAILQ_REMOVE(&geoms, gp, geoms);
296204591Sluigi	g_free(gp->name);
297204591Sluigi	g_free(gp);
298204591Sluigi}
299204591Sluigi
300204591Sluigi/*
301204591Sluigi * This function is called (repeatedly) until the has withered away.
302204591Sluigi */
303204591Sluigivoid
304204591Sluigig_wither_geom(struct g_geom *gp, int error)
305204591Sluigi{
306204591Sluigi	struct g_provider *pp;
307204591Sluigi
308204591Sluigi	g_topology_assert();
309204591Sluigi	G_VALID_GEOM(gp);
310204591Sluigi	g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
311204591Sluigi	if (!(gp->flags & G_GEOM_WITHER)) {
312204591Sluigi		gp->flags |= G_GEOM_WITHER;
313204591Sluigi		LIST_FOREACH(pp, &gp->provider, provider)
314204591Sluigi			if (!(pp->flags & G_PF_ORPHAN))
315204591Sluigi				g_orphan_provider(pp, error);
316204591Sluigi	}
317204591Sluigi	g_do_wither();
318204591Sluigi}
319204591Sluigi
320204591Sluigi/*
321204591Sluigi * This function is called (repeatedly) until the has withered away.
322204591Sluigi */
323204591Sluigivoid
324204591Sluigig_wither_geom_close(struct g_geom *gp, int error)
325204591Sluigi{
326204865Sluigi	struct g_consumer *cp;
327204591Sluigi
328204591Sluigi	g_topology_assert();
329204591Sluigi	G_VALID_GEOM(gp);
330204591Sluigi	g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
331204591Sluigi	LIST_FOREACH(cp, &gp->consumer, consumer)
332204865Sluigi		if (cp->acr || cp->acw || cp->ace)
333204591Sluigi			g_access(cp, -cp->acr, -cp->acw, -cp->ace);
334204591Sluigi	g_wither_geom(gp, error);
335204591Sluigi}
336204591Sluigi
337204591Sluigi/*
338204591Sluigi * This function is called (repeatedly) until we cant wash away more
339204865Sluigi * withered bits at present.  Return value contains two bits.  Bit 0
340204591Sluigi * set means "withering stuff we can't wash now", bit 1 means "call
341204591Sluigi * me again, there may be stuff I didn't get the first time around.
342204591Sluigi */
343204591Sluigiint
344204591Sluigig_wither_washer()
345204591Sluigi{
346204591Sluigi	struct g_class *mp;
347204591Sluigi	struct g_geom *gp, *gp2;
348204591Sluigi	struct g_provider *pp, *pp2;
349204591Sluigi	struct g_consumer *cp, *cp2;
350204591Sluigi	int result;
351204591Sluigi
352204591Sluigi	result = 0;
353204591Sluigi	g_topology_assert();
354204591Sluigi	LIST_FOREACH(mp, &g_classes, class) {
355204591Sluigi		LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
356204591Sluigi			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
357204591Sluigi				if (!(pp->flags & G_PF_WITHER))
358204591Sluigi					continue;
359204591Sluigi				if (LIST_EMPTY(&pp->consumers))
360204591Sluigi					g_destroy_provider(pp);
361204591Sluigi				else
362204591Sluigi					result |= 1;
363204591Sluigi			}
364204591Sluigi			if (!(gp->flags & G_GEOM_WITHER))
365204591Sluigi				continue;
366204591Sluigi			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
367204591Sluigi				if (LIST_EMPTY(&pp->consumers))
368204591Sluigi					g_destroy_provider(pp);
369204591Sluigi				else
370204591Sluigi					result |= 1;
371204591Sluigi			}
372204591Sluigi			LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
373204591Sluigi				if (cp->acr || cp->acw || cp->ace) {
374204591Sluigi					result |= 1;
375204591Sluigi					continue;
376204591Sluigi				}
377204591Sluigi				if (cp->provider != NULL)
378204591Sluigi					g_detach(cp);
379204591Sluigi				g_destroy_consumer(cp);
380204591Sluigi				result |= 2;
381204591Sluigi			}
382204591Sluigi			if (LIST_EMPTY(&gp->provider) &&
383204591Sluigi			    LIST_EMPTY(&gp->consumer))
384204591Sluigi				g_destroy_geom(gp);
385204591Sluigi			else
386204591Sluigi				result |= 1;
387204591Sluigi		}
388204591Sluigi	}
389204591Sluigi	return (result);
390204591Sluigi}
391204591Sluigi
392204591Sluigistruct g_consumer *
393204591Sluigig_new_consumer(struct g_geom *gp)
394204591Sluigi{
395204591Sluigi	struct g_consumer *cp;
396204591Sluigi
397204591Sluigi	g_topology_assert();
398204591Sluigi	G_VALID_GEOM(gp);
399204591Sluigi	KASSERT(!(gp->flags & G_GEOM_WITHER),
400204591Sluigi	    ("g_new_consumer on WITHERing geom(%s) (class %s)",
401204591Sluigi	    gp->name, gp->class->name));
402204591Sluigi	KASSERT(gp->orphan != NULL,
403204591Sluigi	    ("g_new_consumer on geom(%s) (class %s) without orphan",
404204591Sluigi	    gp->name, gp->class->name));
405204591Sluigi
406204591Sluigi	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
407204591Sluigi	cp->geom = gp;
408204591Sluigi	cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
409204591Sluigi	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
410204591Sluigi	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
411204591Sluigi	return(cp);
412204591Sluigi}
413204865Sluigi
414204591Sluigivoid
415204591Sluigig_destroy_consumer(struct g_consumer *cp)
416204591Sluigi{
417204591Sluigi	struct g_geom *gp;
418204591Sluigi
419204591Sluigi	g_topology_assert();
420204591Sluigi	G_VALID_CONSUMER(cp);
421204591Sluigi	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
422204591Sluigi	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
423204591Sluigi	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
424204591Sluigi	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
425204591Sluigi	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
426204591Sluigi	g_cancel_event(cp);
427204591Sluigi	gp = cp->geom;
428204591Sluigi	LIST_REMOVE(cp, consumer);
429204591Sluigi	devstat_remove_entry(cp->stat);
430204591Sluigi	g_free(cp);
431204591Sluigi	if (gp->flags & G_GEOM_WITHER)
432204591Sluigi		g_do_wither();
433204591Sluigi}
434204591Sluigi
435204591Sluigistatic void
436204591Sluigig_new_provider_event(void *arg, int flag)
437204591Sluigi{
438204591Sluigi	struct g_class *mp;
439204591Sluigi	struct g_provider *pp;
440204591Sluigi	struct g_consumer *cp;
441204591Sluigi	int i;
442204591Sluigi
443204591Sluigi	g_topology_assert();
444204591Sluigi	if (flag == EV_CANCEL)
445204591Sluigi		return;
446204591Sluigi	if (g_shutdown)
447204591Sluigi		return;
448204591Sluigi	pp = arg;
449204591Sluigi	G_VALID_PROVIDER(pp);
450204591Sluigi	LIST_FOREACH(mp, &g_classes, class) {
451204591Sluigi		if (mp->taste == NULL)
452204591Sluigi			continue;
453204591Sluigi		i = 1;
454204591Sluigi		LIST_FOREACH(cp, &pp->consumers, consumers)
455204591Sluigi			if (cp->geom->class == mp)
456204591Sluigi				i = 0;
457204591Sluigi		if (!i)
458204591Sluigi			continue;
459204591Sluigi		mp->taste(mp, pp, 0);
460204591Sluigi		g_topology_assert();
461204591Sluigi	}
462204591Sluigi}
463204591Sluigi
464204591Sluigi
465204591Sluigistruct g_provider *
466204591Sluigig_new_providerf(struct g_geom *gp, const char *fmt, ...)
467204591Sluigi{
468204591Sluigi	struct g_provider *pp;
469204591Sluigi	struct sbuf *sb;
470204591Sluigi	va_list ap;
471204591Sluigi
472204591Sluigi	g_topology_assert();
473204591Sluigi	G_VALID_GEOM(gp);
474204865Sluigi	KASSERT(gp->access != NULL,
475204865Sluigi	    ("new provider on geom(%s) without ->access (class %s)",
476204591Sluigi	    gp->name, gp->class->name));
477204591Sluigi	KASSERT(gp->start != NULL,
478204591Sluigi	    ("new provider on geom(%s) without ->start (class %s)",
479204591Sluigi	    gp->name, gp->class->name));
480204591Sluigi	KASSERT(!(gp->flags & G_GEOM_WITHER),
481204591Sluigi	    ("new provider on WITHERing geom(%s) (class %s)",
482204591Sluigi	    gp->name, gp->class->name));
483204591Sluigi	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
484204591Sluigi	va_start(ap, fmt);
485204591Sluigi	sbuf_vprintf(sb, fmt, ap);
486204591Sluigi	va_end(ap);
487204591Sluigi	sbuf_finish(sb);
488204591Sluigi	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
489204591Sluigi	pp->name = (char *)(pp + 1);
490204591Sluigi	strcpy(pp->name, sbuf_data(sb));
491204591Sluigi	sbuf_delete(sb);
492204591Sluigi	LIST_INIT(&pp->consumers);
493204591Sluigi	pp->error = ENXIO;
494204591Sluigi	pp->geom = gp;
495204591Sluigi	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
496204591Sluigi	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
497204591Sluigi	LIST_INSERT_HEAD(&gp->provider, pp, provider);
498204591Sluigi	g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
499204591Sluigi	return (pp);
500204591Sluigi}
501204591Sluigi
502204591Sluigivoid
503204591Sluigig_error_provider(struct g_provider *pp, int error)
504204591Sluigi{
505204591Sluigi
506204591Sluigi	/* G_VALID_PROVIDER(pp);  We may not have g_topology */
507204591Sluigi	pp->error = error;
508204591Sluigi}
509204591Sluigi
510204591Sluigistruct g_provider *
511204591Sluigig_provider_by_name(char const *arg)
512204591Sluigi{
513204591Sluigi	struct g_class *cp;
514204591Sluigi	struct g_geom *gp;
515204591Sluigi	struct g_provider *pp;
516204591Sluigi
517210119Sluigi	LIST_FOREACH(cp, &g_classes, class) {
518204591Sluigi		LIST_FOREACH(gp, &cp->geom, geom) {
519210119Sluigi			LIST_FOREACH(pp, &gp->provider, provider) {
520210119Sluigi				if (!strcmp(arg, pp->name))
521210119Sluigi					return (pp);
522210119Sluigi			}
523204591Sluigi		}
524204591Sluigi	}
525204591Sluigi	return (NULL);
526204591Sluigi}
527204591Sluigi
528204591Sluigivoid
529204591Sluigig_destroy_provider(struct g_provider *pp)
530204591Sluigi{
531204591Sluigi	struct g_geom *gp;
532204591Sluigi
533204591Sluigi	g_topology_assert();
534204591Sluigi	G_VALID_PROVIDER(pp);
535204591Sluigi	KASSERT(LIST_EMPTY(&pp->consumers),
536204591Sluigi	    ("g_destroy_provider but attached"));
537204591Sluigi	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
538204591Sluigi	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
539204591Sluigi	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
540204591Sluigi	g_cancel_event(pp);
541204591Sluigi	LIST_REMOVE(pp, provider);
542204591Sluigi	gp = pp->geom;
543204591Sluigi	devstat_remove_entry(pp->stat);
544204591Sluigi	g_free(pp);
545204591Sluigi	if ((gp->flags & G_GEOM_WITHER))
546204591Sluigi		g_do_wither();
547204591Sluigi}
548204591Sluigi
549204591Sluigi/*
550204591Sluigi * We keep the "geoms" list sorted by topological order (== increasing
551204591Sluigi * numerical rank) at all times.
552204591Sluigi * When an attach is done, the attaching geoms rank is invalidated
553 * and it is moved to the tail of the list.
554 * All geoms later in the sequence has their ranks reevaluated in
555 * sequence.  If we cannot assign rank to a geom because it's
556 * prerequisites do not have rank, we move that element to the tail
557 * of the sequence with invalid rank as well.
558 * At some point we encounter our original geom and if we stil fail
559 * to assign it a rank, there must be a loop and we fail back to
560 * g_attach() which detach again and calls redo_rank again
561 * to fix up the damage.
562 * It would be much simpler code wise to do it recursively, but we
563 * can't risk that on the kernel stack.
564 */
565
566static int
567redo_rank(struct g_geom *gp)
568{
569	struct g_consumer *cp;
570	struct g_geom *gp1, *gp2;
571	int n, m;
572
573	g_topology_assert();
574	G_VALID_GEOM(gp);
575
576	/* Invalidate this geoms rank and move it to the tail */
577	gp1 = TAILQ_NEXT(gp, geoms);
578	if (gp1 != NULL) {
579		gp->rank = 0;
580		TAILQ_REMOVE(&geoms, gp, geoms);
581		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
582	} else {
583		gp1 = gp;
584	}
585
586	/* re-rank the rest of the sequence */
587	for (; gp1 != NULL; gp1 = gp2) {
588		gp1->rank = 0;
589		m = 1;
590		LIST_FOREACH(cp, &gp1->consumer, consumer) {
591			if (cp->provider == NULL)
592				continue;
593			n = cp->provider->geom->rank;
594			if (n == 0) {
595				m = 0;
596				break;
597			} else if (n >= m)
598				m = n + 1;
599		}
600		gp1->rank = m;
601		gp2 = TAILQ_NEXT(gp1, geoms);
602
603		/* got a rank, moving on */
604		if (m != 0)
605			continue;
606
607		/* no rank to original geom means loop */
608		if (gp == gp1)
609			return (ELOOP);
610
611		/* no rank, put it at the end move on */
612		TAILQ_REMOVE(&geoms, gp1, geoms);
613		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
614	}
615	return (0);
616}
617
618int
619g_attach(struct g_consumer *cp, struct g_provider *pp)
620{
621	int error;
622
623	g_topology_assert();
624	G_VALID_CONSUMER(cp);
625	G_VALID_PROVIDER(pp);
626	KASSERT(cp->provider == NULL, ("attach but attached"));
627	cp->provider = pp;
628	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
629	error = redo_rank(cp->geom);
630	if (error) {
631		LIST_REMOVE(cp, consumers);
632		cp->provider = NULL;
633		redo_rank(cp->geom);
634	}
635	return (error);
636}
637
638void
639g_detach(struct g_consumer *cp)
640{
641	struct g_provider *pp;
642
643	g_topology_assert();
644	G_VALID_CONSUMER(cp);
645	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
646	KASSERT(cp->provider != NULL, ("detach but not attached"));
647	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
648	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
649	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
650	KASSERT(cp->nstart == cp->nend,
651	    ("detach with active requests"));
652	pp = cp->provider;
653	LIST_REMOVE(cp, consumers);
654	cp->provider = NULL;
655	if (pp->geom->flags & G_GEOM_WITHER)
656		g_do_wither();
657	else if (pp->flags & G_PF_WITHER)
658		g_do_wither();
659	redo_rank(cp->geom);
660}
661
662/*
663 * g_access()
664 *
665 * Access-check with delta values.  The question asked is "can provider
666 * "cp" change the access counters by the relative amounts dc[rwe] ?"
667 */
668
669int
670g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
671{
672	struct g_provider *pp;
673	int pr,pw,pe;
674	int error;
675
676	g_topology_assert();
677	G_VALID_CONSUMER(cp);
678	pp = cp->provider;
679	KASSERT(pp != NULL, ("access but not attached"));
680	G_VALID_PROVIDER(pp);
681
682	g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
683	    cp, pp->name, dcr, dcw, dce);
684
685	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
686	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
687	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
688	KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
689	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
690
691	/*
692	 * If our class cares about being spoiled, and we have been, we
693	 * are probably just ahead of the event telling us that.  Fail
694	 * now rather than having to unravel this later.
695	 */
696	if (cp->geom->spoiled != NULL && cp->spoiled &&
697	    (dcr > 0 || dcw > 0 || dce > 0))
698		return (ENXIO);
699
700	/*
701	 * Figure out what counts the provider would have had, if this
702	 * consumer had (r0w0e0) at this time.
703	 */
704	pr = pp->acr - cp->acr;
705	pw = pp->acw - cp->acw;
706	pe = pp->ace - cp->ace;
707
708	g_trace(G_T_ACCESS,
709    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
710	    dcr, dcw, dce,
711	    cp->acr, cp->acw, cp->ace,
712	    pp->acr, pp->acw, pp->ace,
713	    pp, pp->name);
714
715	/* If foot-shooting is enabled, any open on rank#1 is OK */
716	if ((g_debugflags & 16) && pp->geom->rank == 1)
717		;
718	/* If we try exclusive but already write: fail */
719	else if (dce > 0 && pw > 0)
720		return (EPERM);
721	/* If we try write but already exclusive: fail */
722	else if (dcw > 0 && pe > 0)
723		return (EPERM);
724	/* If we try to open more but provider is error'ed: fail */
725	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
726		return (pp->error);
727
728	/* Ok then... */
729
730	error = pp->geom->access(pp, dcr, dcw, dce);
731	KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
732	    ("Geom provider %s::%s failed closing ->access()",
733	    pp->geom->class->name, pp->name));
734	if (!error) {
735		/*
736		 * If we open first write, spoil any partner consumers.
737		 * If we close last write and provider is not errored,
738		 * trigger re-taste.
739		 */
740		if (pp->acw == 0 && dcw != 0)
741			g_spoil(pp, cp);
742		else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
743		    !(pp->geom->flags & G_GEOM_WITHER))
744			g_post_event(g_new_provider_event, pp, M_WAITOK,
745			    pp, NULL);
746
747		pp->acr += dcr;
748		pp->acw += dcw;
749		pp->ace += dce;
750		cp->acr += dcr;
751		cp->acw += dcw;
752		cp->ace += dce;
753		if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
754			KASSERT(pp->sectorsize > 0,
755			    ("Provider %s lacks sectorsize", pp->name));
756	}
757	return (error);
758}
759
760int
761g_handleattr_int(struct bio *bp, const char *attribute, int val)
762{
763
764	return (g_handleattr(bp, attribute, &val, sizeof val));
765}
766
767int
768g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
769{
770
771	return (g_handleattr(bp, attribute, &val, sizeof val));
772}
773
774int
775g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
776{
777	int error;
778
779	if (strcmp(bp->bio_attribute, attribute))
780		return (0);
781	if (bp->bio_length != len) {
782		printf("bio_length %jd len %d -> EFAULT\n",
783		    (intmax_t)bp->bio_length, len);
784		error = EFAULT;
785	} else {
786		error = 0;
787		bcopy(val, bp->bio_data, len);
788		bp->bio_completed = len;
789	}
790	g_io_deliver(bp, error);
791	return (1);
792}
793
794int
795g_std_access(struct g_provider *pp,
796	int dr __unused, int dw __unused, int de __unused)
797{
798
799	g_topology_assert();
800	G_VALID_PROVIDER(pp);
801        return (0);
802}
803
804void
805g_std_done(struct bio *bp)
806{
807	struct bio *bp2;
808
809	bp2 = bp->bio_parent;
810	if (bp2->bio_error == 0)
811		bp2->bio_error = bp->bio_error;
812	bp2->bio_completed += bp->bio_completed;
813	g_destroy_bio(bp);
814	bp2->bio_inbed++;
815	if (bp2->bio_children == bp2->bio_inbed)
816		g_io_deliver(bp2, bp2->bio_error);
817}
818
819/* XXX: maybe this is only g_slice_spoiled */
820
821void
822g_std_spoiled(struct g_consumer *cp)
823{
824	struct g_geom *gp;
825	struct g_provider *pp;
826
827	g_topology_assert();
828	G_VALID_CONSUMER(cp);
829	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
830	g_detach(cp);
831	gp = cp->geom;
832	LIST_FOREACH(pp, &gp->provider, provider)
833		g_orphan_provider(pp, ENXIO);
834	g_destroy_consumer(cp);
835	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
836		g_destroy_geom(gp);
837	else
838		gp->flags |= G_GEOM_WITHER;
839}
840
841/*
842 * Spoiling happens when a provider is opened for writing, but consumers
843 * which are configured by in-band data are attached (slicers for instance).
844 * Since the write might potentially change the in-band data, such consumers
845 * need to re-evaluate their existence after the writing session closes.
846 * We do this by (offering to) tear them down when the open for write happens
847 * in return for a re-taste when it closes again.
848 * Together with the fact that such consumers grab an 'e' bit whenever they
849 * are open, regardless of mode, this ends up DTRT.
850 */
851
852static void
853g_spoil_event(void *arg, int flag)
854{
855	struct g_provider *pp;
856	struct g_consumer *cp, *cp2;
857
858	g_topology_assert();
859	if (flag == EV_CANCEL)
860		return;
861	pp = arg;
862	G_VALID_PROVIDER(pp);
863	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
864		cp2 = LIST_NEXT(cp, consumers);
865		if (!cp->spoiled)
866			continue;
867		cp->spoiled = 0;
868		if (cp->geom->spoiled == NULL)
869			continue;
870		cp->geom->spoiled(cp);
871		g_topology_assert();
872	}
873}
874
875void
876g_spoil(struct g_provider *pp, struct g_consumer *cp)
877{
878	struct g_consumer *cp2;
879
880	g_topology_assert();
881	G_VALID_PROVIDER(pp);
882	G_VALID_CONSUMER(cp);
883
884	LIST_FOREACH(cp2, &pp->consumers, consumers) {
885		if (cp2 == cp)
886			continue;
887/*
888		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
889		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
890*/
891		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
892		cp2->spoiled++;
893	}
894	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
895}
896
897int
898g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
899{
900	int error, i;
901
902	i = len;
903	error = g_io_getattr(attr, cp, &i, var);
904	if (error)
905		return (error);
906	if (i != len)
907		return (EINVAL);
908	return (0);
909}
910
911#ifdef DIAGNOSTIC
912/*
913 * This function walks (topologically unsafely) the mesh and return a
914 * non-zero integer if it finds the argument pointer is an object.
915 * The return value indicates which type of object it is belived to be.
916 * If topology is not locked, this function is potentially dangerous,
917 * but since it is for debugging purposes and can be useful for instance
918 * from DDB, we do not assert topology lock is held.
919 */
920int
921g_valid_obj(void const *ptr)
922{
923	struct g_class *mp;
924	struct g_geom *gp;
925	struct g_consumer *cp;
926	struct g_provider *pp;
927
928	LIST_FOREACH(mp, &g_classes, class) {
929		if (ptr == mp)
930			return (1);
931		LIST_FOREACH(gp, &mp->geom, geom) {
932			if (ptr == gp)
933				return (2);
934			LIST_FOREACH(cp, &gp->consumer, consumer)
935				if (ptr == cp)
936					return (3);
937			LIST_FOREACH(pp, &gp->provider, provider)
938				if (ptr == pp)
939					return (4);
940		}
941	}
942	return(0);
943}
944#endif
945