geom_subr.c revision 239987
1169695Skan/*-
2169695Skan * Copyright (c) 2002 Poul-Henning Kamp
3169695Skan * Copyright (c) 2002 Networks Associates Technology, Inc.
4169695Skan * All rights reserved.
5169695Skan *
6169695Skan * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7169695Skan * and NAI Labs, the Security Research Division of Network Associates, Inc.
8169695Skan * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9169695Skan * DARPA CHATS research program.
10169695Skan *
11169695Skan * Redistribution and use in source and binary forms, with or without
12169695Skan * modification, are permitted provided that the following conditions
13169695Skan * are met:
14169695Skan * 1. Redistributions of source code must retain the above copyright
15169695Skan *    notice, this list of conditions and the following disclaimer.
16169695Skan * 2. Redistributions in binary form must reproduce the above copyright
17169695Skan *    notice, this list of conditions and the following disclaimer in the
18169695Skan *    documentation and/or other materials provided with the distribution.
19169695Skan * 3. The names of the authors may not be used to endorse or promote
20169695Skan *    products derived from this software without specific prior written
21169695Skan *    permission.
22169695Skan *
23169695Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24169695Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25169695Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26169695Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27169695Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28169695Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29169695Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30169695Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31169695Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32169695Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33169695Skan * SUCH DAMAGE.
34169695Skan */
35169695Skan
36169695Skan#include <sys/cdefs.h>
37169695Skan__FBSDID("$FreeBSD: head/sys/geom/geom_subr.c 239987 2012-09-01 10:52:19Z pjd $");
38169695Skan
39169695Skan#include "opt_ddb.h"
40169695Skan
41169695Skan#include <sys/param.h>
42169695Skan#include <sys/systm.h>
43169695Skan#include <sys/devicestat.h>
44169695Skan#include <sys/kernel.h>
45169695Skan#include <sys/malloc.h>
46169695Skan#include <sys/bio.h>
47169695Skan#include <sys/sysctl.h>
48169695Skan#include <sys/proc.h>
49169695Skan#include <sys/kthread.h>
50169695Skan#include <sys/lock.h>
51169695Skan#include <sys/mutex.h>
52169695Skan#include <sys/errno.h>
53169695Skan#include <sys/sbuf.h>
54169695Skan#include <geom/geom.h>
55169695Skan#include <geom/geom_int.h>
56169695Skan#include <machine/stdarg.h>
57169695Skan
58169695Skan#ifdef DDB
59169695Skan#include <ddb/ddb.h>
60169695Skan#endif
61169695Skan
62169695Skan#ifdef KDB
63169695Skan#include <sys/kdb.h>
64169695Skan#endif
65169695Skan
66169695Skanstruct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
67169695Skanstatic struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
68169695Skanchar *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
69169695Skan
70169695Skanstruct g_hh00 {
71169695Skan	struct g_class		*mp;
72169695Skan	struct g_provider	*pp;
73169695Skan	off_t			size;
74169695Skan	int			error;
75169695Skan	int			post;
76169695Skan};
77169695Skan
78169695Skan/*
79169695Skan * This event offers a new class a chance to taste all preexisting providers.
80169695Skan */
81169695Skanstatic void
82169695Skang_load_class(void *arg, int flag)
83169695Skan{
84169695Skan	struct g_hh00 *hh;
85169695Skan	struct g_class *mp2, *mp;
86169695Skan	struct g_geom *gp;
87169695Skan	struct g_provider *pp;
88169695Skan
89169695Skan	g_topology_assert();
90169695Skan	if (flag == EV_CANCEL)	/* XXX: can't happen ? */
91169695Skan		return;
92169695Skan	if (g_shutdown)
93169695Skan		return;
94169695Skan
95169695Skan	hh = arg;
96169695Skan	mp = hh->mp;
97169695Skan	hh->error = 0;
98169695Skan	if (hh->post) {
99169695Skan		g_free(hh);
100169695Skan		hh = NULL;
101169695Skan	}
102169695Skan	g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
103169695Skan	KASSERT(mp->name != NULL && *mp->name != '\0',
104169695Skan	    ("GEOM class has no name"));
105169695Skan	LIST_FOREACH(mp2, &g_classes, class) {
106169695Skan		if (mp2 == mp) {
107169695Skan			printf("The GEOM class %s is already loaded.\n",
108169695Skan			    mp2->name);
109169695Skan			if (hh != NULL)
110169695Skan				hh->error = EEXIST;
111169695Skan			return;
112169695Skan		} else if (strcmp(mp2->name, mp->name) == 0) {
113169695Skan			printf("A GEOM class %s is already loaded.\n",
114169695Skan			    mp2->name);
115169695Skan			if (hh != NULL)
116169695Skan				hh->error = EEXIST;
117169695Skan			return;
118169695Skan		}
119169695Skan	}
120169695Skan
121169695Skan	LIST_INIT(&mp->geom);
122169695Skan	LIST_INSERT_HEAD(&g_classes, mp, class);
123169695Skan	if (mp->init != NULL)
124169695Skan		mp->init(mp);
125169695Skan	if (mp->taste == NULL)
126169695Skan		return;
127169695Skan	LIST_FOREACH(mp2, &g_classes, class) {
128169695Skan		if (mp == mp2)
129169695Skan			continue;
130169695Skan		LIST_FOREACH(gp, &mp2->geom, geom) {
131169695Skan			LIST_FOREACH(pp, &gp->provider, provider) {
132169695Skan				mp->taste(mp, pp, 0);
133169695Skan				g_topology_assert();
134169695Skan			}
135169695Skan		}
136169695Skan	}
137169695Skan}
138169695Skan
139169695Skanstatic int
140169695Skang_unload_class(struct g_class *mp)
141169695Skan{
142169695Skan	struct g_geom *gp;
143169695Skan	struct g_provider *pp;
144169695Skan	struct g_consumer *cp;
145169695Skan	int error;
146169695Skan
147169695Skan	g_topology_lock();
148169695Skan	g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
149169695Skanretry:
150169695Skan	G_VALID_CLASS(mp);
151169695Skan	LIST_FOREACH(gp, &mp->geom, geom) {
152169695Skan		/* We refuse to unload if anything is open */
153169695Skan		LIST_FOREACH(pp, &gp->provider, provider)
154169695Skan			if (pp->acr || pp->acw || pp->ace) {
155169695Skan				g_topology_unlock();
156169695Skan				return (EBUSY);
157169695Skan			}
158169695Skan		LIST_FOREACH(cp, &gp->consumer, consumer)
159169695Skan			if (cp->acr || cp->acw || cp->ace) {
160169695Skan				g_topology_unlock();
161169695Skan				return (EBUSY);
162169695Skan			}
163169695Skan		/* If the geom is withering, wait for it to finish. */
164169695Skan		if (gp->flags & G_GEOM_WITHER) {
165169695Skan			g_topology_sleep(mp, 1);
166169695Skan			goto retry;
167169695Skan		}
168169695Skan	}
169169695Skan
170169695Skan	/*
171169695Skan	 * We allow unloading if we have no geoms, or a class
172169695Skan	 * method we can use to get rid of them.
173169695Skan	 */
174169695Skan	if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
175169695Skan		g_topology_unlock();
176169695Skan		return (EOPNOTSUPP);
177169695Skan	}
178169695Skan
179169695Skan	/* Bar new entries */
180169695Skan	mp->taste = NULL;
181169695Skan	mp->config = NULL;
182169695Skan
183169695Skan	LIST_FOREACH(gp, &mp->geom, geom) {
184169695Skan		error = mp->destroy_geom(NULL, mp, gp);
185169695Skan		if (error != 0) {
186169695Skan			g_topology_unlock();
187169695Skan			return (error);
188169695Skan		}
189169695Skan	}
190169695Skan	/* Wait for withering to finish. */
191169695Skan	for (;;) {
192169695Skan		gp = LIST_FIRST(&mp->geom);
193169695Skan		if (gp == NULL)
194169695Skan			break;
195169695Skan		KASSERT(gp->flags & G_GEOM_WITHER,
196169695Skan		   ("Non-withering geom in class %s", mp->name));
197169695Skan		g_topology_sleep(mp, 1);
198169695Skan	}
199169695Skan	G_VALID_CLASS(mp);
200169695Skan	if (mp->fini != NULL)
201169695Skan		mp->fini(mp);
202169695Skan	LIST_REMOVE(mp, class);
203169695Skan	g_topology_unlock();
204169695Skan
205169695Skan	return (0);
206169695Skan}
207169695Skan
208169695Skanint
209169695Skang_modevent(module_t mod, int type, void *data)
210169695Skan{
211169695Skan	struct g_hh00 *hh;
212169695Skan	int error;
213169695Skan	static int g_ignition;
214169695Skan	struct g_class *mp;
215169695Skan
216169695Skan	mp = data;
217169695Skan	if (mp->version != G_VERSION) {
218169695Skan		printf("GEOM class %s has Wrong version %x\n",
219169695Skan		    mp->name, mp->version);
220169695Skan		return (EINVAL);
221169695Skan	}
222169695Skan	if (!g_ignition) {
223169695Skan		g_ignition++;
224169695Skan		g_init();
225169695Skan	}
226169695Skan	error = EOPNOTSUPP;
227169695Skan	switch (type) {
228169695Skan	case MOD_LOAD:
229169695Skan		g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", mp->name);
230169695Skan		hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
231169695Skan		hh->mp = mp;
232169695Skan		/*
233169695Skan		 * Once the system is not cold, MOD_LOAD calls will be
234169695Skan		 * from the userland and the g_event thread will be able
235169695Skan		 * to acknowledge their completion.
236169695Skan		 */
237169695Skan		if (cold) {
238169695Skan			hh->post = 1;
239169695Skan			error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
240169695Skan		} else {
241169695Skan			error = g_waitfor_event(g_load_class, hh, M_WAITOK,
242169695Skan			    NULL);
243169695Skan			if (error == 0)
244169695Skan				error = hh->error;
245169695Skan			g_free(hh);
246169695Skan		}
247169695Skan		break;
248169695Skan	case MOD_UNLOAD:
249169695Skan		g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", mp->name);
250169695Skan		DROP_GIANT();
251169695Skan		error = g_unload_class(mp);
252169695Skan		PICKUP_GIANT();
253169695Skan		if (error == 0) {
254169695Skan			KASSERT(LIST_EMPTY(&mp->geom),
255169695Skan			    ("Unloaded class (%s) still has geom", mp->name));
256169695Skan		}
257169695Skan		break;
258169695Skan	}
259169695Skan	return (error);
260169695Skan}
261169695Skan
262169695Skanstatic void
263169695Skang_retaste_event(void *arg, int flag)
264169695Skan{
265169695Skan	struct g_class *mp, *mp2;
266169695Skan	struct g_geom *gp;
267169695Skan	struct g_hh00 *hh;
268169695Skan	struct g_provider *pp;
269169695Skan	struct g_consumer *cp;
270169695Skan
271169695Skan	g_topology_assert();
272169695Skan	if (flag == EV_CANCEL)  /* XXX: can't happen ? */
273169695Skan		return;
274169695Skan	if (g_shutdown)
275169695Skan		return;
276169695Skan
277169695Skan	hh = arg;
278169695Skan	mp = hh->mp;
279169695Skan	hh->error = 0;
280169695Skan	if (hh->post) {
281169695Skan		g_free(hh);
282169695Skan		hh = NULL;
283169695Skan	}
284169695Skan	g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name);
285169695Skan
286169695Skan	LIST_FOREACH(mp2, &g_classes, class) {
287169695Skan		LIST_FOREACH(gp, &mp2->geom, geom) {
288169695Skan			LIST_FOREACH(pp, &gp->provider, provider) {
289169695Skan				if (pp->acr || pp->acw || pp->ace)
290169695Skan					continue;
291169695Skan				LIST_FOREACH(cp, &pp->consumers, consumers) {
292169695Skan					if (cp->geom->class == mp &&
293169695Skan					    (cp->flags & G_CF_ORPHAN) == 0)
294169695Skan						break;
295169695Skan				}
296169695Skan				if (cp != NULL) {
297169695Skan					cp->flags |= G_CF_ORPHAN;
298169695Skan					g_wither_geom(cp->geom, ENXIO);
299169695Skan				}
300169695Skan				mp->taste(mp, pp, 0);
301169695Skan				g_topology_assert();
302169695Skan			}
303169695Skan		}
304169695Skan	}
305169695Skan}
306169695Skan
307169695Skanint
308169695Skang_retaste(struct g_class *mp)
309169695Skan{
310169695Skan	struct g_hh00 *hh;
311169695Skan	int error;
312169695Skan
313169695Skan	if (mp->taste == NULL)
314169695Skan		return (EINVAL);
315169695Skan
316169695Skan	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
317169695Skan	hh->mp = mp;
318169695Skan
319169695Skan	if (cold) {
320169695Skan		hh->post = 1;
321169695Skan		error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL);
322169695Skan	} else {
323169695Skan		error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL);
324169695Skan		if (error == 0)
325169695Skan			error = hh->error;
326169695Skan		g_free(hh);
327169695Skan	}
328169695Skan
329169695Skan	return (error);
330169695Skan}
331169695Skan
332169695Skanstruct g_geom *
333169695Skang_new_geomf(struct g_class *mp, const char *fmt, ...)
334169695Skan{
335169695Skan	struct g_geom *gp;
336169695Skan	va_list ap;
337169695Skan	struct sbuf *sb;
338169695Skan
339169695Skan	g_topology_assert();
340169695Skan	G_VALID_CLASS(mp);
341169695Skan	sb = sbuf_new_auto();
342169695Skan	va_start(ap, fmt);
343169695Skan	sbuf_vprintf(sb, fmt, ap);
344169695Skan	va_end(ap);
345169695Skan	sbuf_finish(sb);
346169695Skan	gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
347169695Skan	gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
348169695Skan	gp->class = mp;
349169695Skan	gp->rank = 1;
350169695Skan	LIST_INIT(&gp->consumer);
351169695Skan	LIST_INIT(&gp->provider);
352169695Skan	LIST_INSERT_HEAD(&mp->geom, gp, geom);
353169695Skan	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
354169695Skan	strcpy(gp->name, sbuf_data(sb));
355169695Skan	sbuf_delete(sb);
356169695Skan	/* Fill in defaults from class */
357169695Skan	gp->start = mp->start;
358169695Skan	gp->spoiled = mp->spoiled;
359169695Skan	gp->attrchanged = mp->attrchanged;
360169695Skan	gp->providergone = mp->providergone;
361	gp->dumpconf = mp->dumpconf;
362	gp->access = mp->access;
363	gp->orphan = mp->orphan;
364	gp->ioctl = mp->ioctl;
365	gp->resize = mp->resize;
366	return (gp);
367}
368
369void
370g_destroy_geom(struct g_geom *gp)
371{
372
373	g_topology_assert();
374	G_VALID_GEOM(gp);
375	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
376	KASSERT(LIST_EMPTY(&gp->consumer),
377	    ("g_destroy_geom(%s) with consumer(s) [%p]",
378	    gp->name, LIST_FIRST(&gp->consumer)));
379	KASSERT(LIST_EMPTY(&gp->provider),
380	    ("g_destroy_geom(%s) with provider(s) [%p]",
381	    gp->name, LIST_FIRST(&gp->provider)));
382	g_cancel_event(gp);
383	LIST_REMOVE(gp, geom);
384	TAILQ_REMOVE(&geoms, gp, geoms);
385	g_free(gp->name);
386	g_free(gp);
387}
388
389/*
390 * This function is called (repeatedly) until the geom has withered away.
391 */
392void
393g_wither_geom(struct g_geom *gp, int error)
394{
395	struct g_provider *pp;
396
397	g_topology_assert();
398	G_VALID_GEOM(gp);
399	g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
400	if (!(gp->flags & G_GEOM_WITHER)) {
401		gp->flags |= G_GEOM_WITHER;
402		LIST_FOREACH(pp, &gp->provider, provider)
403			if (!(pp->flags & G_PF_ORPHAN))
404				g_orphan_provider(pp, error);
405	}
406	g_do_wither();
407}
408
409/*
410 * Convenience function to destroy a particular provider.
411 */
412void
413g_wither_provider(struct g_provider *pp, int error)
414{
415
416	pp->flags |= G_PF_WITHER;
417	if (!(pp->flags & G_PF_ORPHAN))
418		g_orphan_provider(pp, error);
419}
420
421/*
422 * This function is called (repeatedly) until the has withered away.
423 */
424void
425g_wither_geom_close(struct g_geom *gp, int error)
426{
427	struct g_consumer *cp;
428
429	g_topology_assert();
430	G_VALID_GEOM(gp);
431	g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
432	LIST_FOREACH(cp, &gp->consumer, consumer)
433		if (cp->acr || cp->acw || cp->ace)
434			g_access(cp, -cp->acr, -cp->acw, -cp->ace);
435	g_wither_geom(gp, error);
436}
437
438/*
439 * This function is called (repeatedly) until we cant wash away more
440 * withered bits at present.  Return value contains two bits.  Bit 0
441 * set means "withering stuff we can't wash now", bit 1 means "call
442 * me again, there may be stuff I didn't get the first time around.
443 */
444int
445g_wither_washer()
446{
447	struct g_class *mp;
448	struct g_geom *gp, *gp2;
449	struct g_provider *pp, *pp2;
450	struct g_consumer *cp, *cp2;
451	int result;
452
453	result = 0;
454	g_topology_assert();
455	LIST_FOREACH(mp, &g_classes, class) {
456		LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
457			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
458				if (!(pp->flags & G_PF_WITHER))
459					continue;
460				if (LIST_EMPTY(&pp->consumers))
461					g_destroy_provider(pp);
462				else
463					result |= 1;
464			}
465			if (!(gp->flags & G_GEOM_WITHER))
466				continue;
467			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
468				if (LIST_EMPTY(&pp->consumers))
469					g_destroy_provider(pp);
470				else
471					result |= 1;
472			}
473			LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
474				if (cp->acr || cp->acw || cp->ace) {
475					result |= 1;
476					continue;
477				}
478				if (cp->provider != NULL)
479					g_detach(cp);
480				g_destroy_consumer(cp);
481				result |= 2;
482			}
483			if (LIST_EMPTY(&gp->provider) &&
484			    LIST_EMPTY(&gp->consumer))
485				g_destroy_geom(gp);
486			else
487				result |= 1;
488		}
489	}
490	return (result);
491}
492
493struct g_consumer *
494g_new_consumer(struct g_geom *gp)
495{
496	struct g_consumer *cp;
497
498	g_topology_assert();
499	G_VALID_GEOM(gp);
500	KASSERT(!(gp->flags & G_GEOM_WITHER),
501	    ("g_new_consumer on WITHERing geom(%s) (class %s)",
502	    gp->name, gp->class->name));
503	KASSERT(gp->orphan != NULL,
504	    ("g_new_consumer on geom(%s) (class %s) without orphan",
505	    gp->name, gp->class->name));
506
507	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
508	cp->geom = gp;
509	cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
510	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
511	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
512	return(cp);
513}
514
515void
516g_destroy_consumer(struct g_consumer *cp)
517{
518	struct g_geom *gp;
519
520	g_topology_assert();
521	G_VALID_CONSUMER(cp);
522	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
523	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
524	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
525	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
526	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
527	g_cancel_event(cp);
528	gp = cp->geom;
529	LIST_REMOVE(cp, consumer);
530	devstat_remove_entry(cp->stat);
531	g_free(cp);
532	if (gp->flags & G_GEOM_WITHER)
533		g_do_wither();
534}
535
536static void
537g_new_provider_event(void *arg, int flag)
538{
539	struct g_class *mp;
540	struct g_provider *pp;
541	struct g_consumer *cp, *next_cp;
542
543	g_topology_assert();
544	if (flag == EV_CANCEL)
545		return;
546	if (g_shutdown)
547		return;
548	pp = arg;
549	G_VALID_PROVIDER(pp);
550	KASSERT(!(pp->flags & G_PF_WITHER),
551	    ("g_new_provider_event but withered"));
552	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
553		if ((cp->flags & G_CF_ORPHAN) == 0 &&
554		    cp->geom->attrchanged != NULL)
555			cp->geom->attrchanged(cp, "GEOM::media");
556	}
557	LIST_FOREACH(mp, &g_classes, class) {
558		if (mp->taste == NULL)
559			continue;
560		LIST_FOREACH(cp, &pp->consumers, consumers)
561			if (cp->geom->class == mp &&
562			    (cp->flags & G_CF_ORPHAN) == 0)
563				break;
564		if (cp != NULL)
565			continue;
566		mp->taste(mp, pp, 0);
567		g_topology_assert();
568	}
569}
570
571
572struct g_provider *
573g_new_providerf(struct g_geom *gp, const char *fmt, ...)
574{
575	struct g_provider *pp;
576	struct sbuf *sb;
577	va_list ap;
578
579	g_topology_assert();
580	G_VALID_GEOM(gp);
581	KASSERT(gp->access != NULL,
582	    ("new provider on geom(%s) without ->access (class %s)",
583	    gp->name, gp->class->name));
584	KASSERT(gp->start != NULL,
585	    ("new provider on geom(%s) without ->start (class %s)",
586	    gp->name, gp->class->name));
587	KASSERT(!(gp->flags & G_GEOM_WITHER),
588	    ("new provider on WITHERing geom(%s) (class %s)",
589	    gp->name, gp->class->name));
590	sb = sbuf_new_auto();
591	va_start(ap, fmt);
592	sbuf_vprintf(sb, fmt, ap);
593	va_end(ap);
594	sbuf_finish(sb);
595	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
596	pp->name = (char *)(pp + 1);
597	strcpy(pp->name, sbuf_data(sb));
598	sbuf_delete(sb);
599	LIST_INIT(&pp->consumers);
600	pp->error = ENXIO;
601	pp->geom = gp;
602	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
603	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
604	LIST_INSERT_HEAD(&gp->provider, pp, provider);
605	g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
606	return (pp);
607}
608
609void
610g_error_provider(struct g_provider *pp, int error)
611{
612
613	/* G_VALID_PROVIDER(pp);  We may not have g_topology */
614	pp->error = error;
615}
616
617static void
618g_resize_provider_event(void *arg, int flag)
619{
620	struct g_hh00 *hh;
621	struct g_class *mp;
622	struct g_geom *gp;
623	struct g_provider *pp;
624	struct g_consumer *cp, *cp2;
625	off_t size;
626
627	g_topology_assert();
628	if (g_shutdown)
629		return;
630
631	hh = arg;
632	pp = hh->pp;
633	size = hh->size;
634	g_free(hh);
635
636	G_VALID_PROVIDER(pp);
637	g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
638
639	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
640		gp = cp->geom;
641		if (gp->resize == NULL && size < pp->mediasize) {
642			cp->flags |= G_CF_ORPHAN;
643			cp->geom->orphan(cp);
644		}
645	}
646
647	pp->mediasize = size;
648
649	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
650		gp = cp->geom;
651		if (gp->resize != NULL)
652			gp->resize(cp);
653	}
654
655	/*
656	 * After resizing, the previously invalid GEOM class metadata
657	 * might become valid.  This means we should retaste.
658	 */
659	LIST_FOREACH(mp, &g_classes, class) {
660		if (mp->taste == NULL)
661			continue;
662		LIST_FOREACH(cp, &pp->consumers, consumers)
663			if (cp->geom->class == mp &&
664			    (cp->flags & G_CF_ORPHAN) == 0)
665				break;
666		if (cp != NULL)
667			continue;
668		mp->taste(mp, pp, 0);
669		g_topology_assert();
670	}
671}
672
673void
674g_resize_provider(struct g_provider *pp, off_t size)
675{
676	struct g_hh00 *hh;
677
678	G_VALID_PROVIDER(pp);
679
680	if (size == pp->mediasize)
681		return;
682
683	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
684	hh->pp = pp;
685	hh->size = size;
686	g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
687}
688
689#ifndef	_PATH_DEV
690#define	_PATH_DEV	"/dev/"
691#endif
692
693struct g_provider *
694g_provider_by_name(char const *arg)
695{
696	struct g_class *cp;
697	struct g_geom *gp;
698	struct g_provider *pp;
699
700	if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
701		arg += sizeof(_PATH_DEV) - 1;
702
703	LIST_FOREACH(cp, &g_classes, class) {
704		LIST_FOREACH(gp, &cp->geom, geom) {
705			LIST_FOREACH(pp, &gp->provider, provider) {
706				if (!strcmp(arg, pp->name))
707					return (pp);
708			}
709		}
710	}
711
712	return (NULL);
713}
714
715void
716g_destroy_provider(struct g_provider *pp)
717{
718	struct g_geom *gp;
719
720	g_topology_assert();
721	G_VALID_PROVIDER(pp);
722	KASSERT(LIST_EMPTY(&pp->consumers),
723	    ("g_destroy_provider but attached"));
724	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
725	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
726	KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
727	g_cancel_event(pp);
728	LIST_REMOVE(pp, provider);
729	gp = pp->geom;
730	devstat_remove_entry(pp->stat);
731	/*
732	 * If a callback was provided, send notification that the provider
733	 * is now gone.
734	 */
735	if (gp->providergone != NULL)
736		gp->providergone(pp);
737
738	g_free(pp);
739	if ((gp->flags & G_GEOM_WITHER))
740		g_do_wither();
741}
742
743/*
744 * We keep the "geoms" list sorted by topological order (== increasing
745 * numerical rank) at all times.
746 * When an attach is done, the attaching geoms rank is invalidated
747 * and it is moved to the tail of the list.
748 * All geoms later in the sequence has their ranks reevaluated in
749 * sequence.  If we cannot assign rank to a geom because it's
750 * prerequisites do not have rank, we move that element to the tail
751 * of the sequence with invalid rank as well.
752 * At some point we encounter our original geom and if we stil fail
753 * to assign it a rank, there must be a loop and we fail back to
754 * g_attach() which detach again and calls redo_rank again
755 * to fix up the damage.
756 * It would be much simpler code wise to do it recursively, but we
757 * can't risk that on the kernel stack.
758 */
759
760static int
761redo_rank(struct g_geom *gp)
762{
763	struct g_consumer *cp;
764	struct g_geom *gp1, *gp2;
765	int n, m;
766
767	g_topology_assert();
768	G_VALID_GEOM(gp);
769
770	/* Invalidate this geoms rank and move it to the tail */
771	gp1 = TAILQ_NEXT(gp, geoms);
772	if (gp1 != NULL) {
773		gp->rank = 0;
774		TAILQ_REMOVE(&geoms, gp, geoms);
775		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
776	} else {
777		gp1 = gp;
778	}
779
780	/* re-rank the rest of the sequence */
781	for (; gp1 != NULL; gp1 = gp2) {
782		gp1->rank = 0;
783		m = 1;
784		LIST_FOREACH(cp, &gp1->consumer, consumer) {
785			if (cp->provider == NULL)
786				continue;
787			n = cp->provider->geom->rank;
788			if (n == 0) {
789				m = 0;
790				break;
791			} else if (n >= m)
792				m = n + 1;
793		}
794		gp1->rank = m;
795		gp2 = TAILQ_NEXT(gp1, geoms);
796
797		/* got a rank, moving on */
798		if (m != 0)
799			continue;
800
801		/* no rank to original geom means loop */
802		if (gp == gp1)
803			return (ELOOP);
804
805		/* no rank, put it at the end move on */
806		TAILQ_REMOVE(&geoms, gp1, geoms);
807		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
808	}
809	return (0);
810}
811
812int
813g_attach(struct g_consumer *cp, struct g_provider *pp)
814{
815	int error;
816
817	g_topology_assert();
818	G_VALID_CONSUMER(cp);
819	G_VALID_PROVIDER(pp);
820	g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
821	KASSERT(cp->provider == NULL, ("attach but attached"));
822	cp->provider = pp;
823	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
824	error = redo_rank(cp->geom);
825	if (error) {
826		LIST_REMOVE(cp, consumers);
827		cp->provider = NULL;
828		redo_rank(cp->geom);
829	}
830	return (error);
831}
832
833void
834g_detach(struct g_consumer *cp)
835{
836	struct g_provider *pp;
837
838	g_topology_assert();
839	G_VALID_CONSUMER(cp);
840	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
841	KASSERT(cp->provider != NULL, ("detach but not attached"));
842	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
843	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
844	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
845	KASSERT(cp->nstart == cp->nend,
846	    ("detach with active requests"));
847	pp = cp->provider;
848	LIST_REMOVE(cp, consumers);
849	cp->provider = NULL;
850	if (pp->geom->flags & G_GEOM_WITHER)
851		g_do_wither();
852	else if (pp->flags & G_PF_WITHER)
853		g_do_wither();
854	redo_rank(cp->geom);
855}
856
857/*
858 * g_access()
859 *
860 * Access-check with delta values.  The question asked is "can provider
861 * "cp" change the access counters by the relative amounts dc[rwe] ?"
862 */
863
864int
865g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
866{
867	struct g_provider *pp;
868	int pr,pw,pe;
869	int error;
870
871	g_topology_assert();
872	G_VALID_CONSUMER(cp);
873	pp = cp->provider;
874	KASSERT(pp != NULL, ("access but not attached"));
875	G_VALID_PROVIDER(pp);
876
877	g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
878	    cp, pp->name, dcr, dcw, dce);
879
880	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
881	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
882	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
883	KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
884	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
885
886	/*
887	 * If our class cares about being spoiled, and we have been, we
888	 * are probably just ahead of the event telling us that.  Fail
889	 * now rather than having to unravel this later.
890	 */
891	if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
892	    (dcr > 0 || dcw > 0 || dce > 0))
893		return (ENXIO);
894
895	/*
896	 * Figure out what counts the provider would have had, if this
897	 * consumer had (r0w0e0) at this time.
898	 */
899	pr = pp->acr - cp->acr;
900	pw = pp->acw - cp->acw;
901	pe = pp->ace - cp->ace;
902
903	g_trace(G_T_ACCESS,
904    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
905	    dcr, dcw, dce,
906	    cp->acr, cp->acw, cp->ace,
907	    pp->acr, pp->acw, pp->ace,
908	    pp, pp->name);
909
910	/* If foot-shooting is enabled, any open on rank#1 is OK */
911	if ((g_debugflags & 16) && pp->geom->rank == 1)
912		;
913	/* If we try exclusive but already write: fail */
914	else if (dce > 0 && pw > 0)
915		return (EPERM);
916	/* If we try write but already exclusive: fail */
917	else if (dcw > 0 && pe > 0)
918		return (EPERM);
919	/* If we try to open more but provider is error'ed: fail */
920	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
921		return (pp->error);
922
923	/* Ok then... */
924
925	error = pp->geom->access(pp, dcr, dcw, dce);
926	KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
927	    ("Geom provider %s::%s failed closing ->access()",
928	    pp->geom->class->name, pp->name));
929	if (!error) {
930		/*
931		 * If we open first write, spoil any partner consumers.
932		 * If we close last write and provider is not errored,
933		 * trigger re-taste.
934		 */
935		if (pp->acw == 0 && dcw != 0)
936			g_spoil(pp, cp);
937		else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
938		    !(pp->geom->flags & G_GEOM_WITHER))
939			g_post_event(g_new_provider_event, pp, M_WAITOK,
940			    pp, NULL);
941
942		pp->acr += dcr;
943		pp->acw += dcw;
944		pp->ace += dce;
945		cp->acr += dcr;
946		cp->acw += dcw;
947		cp->ace += dce;
948		if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
949			KASSERT(pp->sectorsize > 0,
950			    ("Provider %s lacks sectorsize", pp->name));
951	}
952	return (error);
953}
954
955int
956g_handleattr_int(struct bio *bp, const char *attribute, int val)
957{
958
959	return (g_handleattr(bp, attribute, &val, sizeof val));
960}
961
962int
963g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
964{
965
966	return (g_handleattr(bp, attribute, &val, sizeof val));
967}
968
969int
970g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
971{
972
973	return (g_handleattr(bp, attribute, str, 0));
974}
975
976int
977g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
978{
979	int error = 0;
980
981	if (strcmp(bp->bio_attribute, attribute))
982		return (0);
983	if (len == 0) {
984		bzero(bp->bio_data, bp->bio_length);
985		if (strlcpy(bp->bio_data, val, bp->bio_length) >=
986		    bp->bio_length) {
987			printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
988			    __func__, bp->bio_to->name,
989			    (intmax_t)bp->bio_length, strlen(val));
990			error = EFAULT;
991		}
992	} else if (bp->bio_length == len) {
993		bcopy(val, bp->bio_data, len);
994	} else {
995		printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
996		    bp->bio_to->name, (intmax_t)bp->bio_length, len);
997		error = EFAULT;
998	}
999	if (error == 0)
1000		bp->bio_completed = bp->bio_length;
1001	g_io_deliver(bp, error);
1002	return (1);
1003}
1004
1005int
1006g_std_access(struct g_provider *pp,
1007	int dr __unused, int dw __unused, int de __unused)
1008{
1009
1010	g_topology_assert();
1011	G_VALID_PROVIDER(pp);
1012        return (0);
1013}
1014
1015void
1016g_std_done(struct bio *bp)
1017{
1018	struct bio *bp2;
1019
1020	bp2 = bp->bio_parent;
1021	if (bp2->bio_error == 0)
1022		bp2->bio_error = bp->bio_error;
1023	bp2->bio_completed += bp->bio_completed;
1024	g_destroy_bio(bp);
1025	bp2->bio_inbed++;
1026	if (bp2->bio_children == bp2->bio_inbed)
1027		g_io_deliver(bp2, bp2->bio_error);
1028}
1029
1030/* XXX: maybe this is only g_slice_spoiled */
1031
1032void
1033g_std_spoiled(struct g_consumer *cp)
1034{
1035	struct g_geom *gp;
1036	struct g_provider *pp;
1037
1038	g_topology_assert();
1039	G_VALID_CONSUMER(cp);
1040	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
1041	cp->flags |= G_CF_ORPHAN;
1042	g_detach(cp);
1043	gp = cp->geom;
1044	LIST_FOREACH(pp, &gp->provider, provider)
1045		g_orphan_provider(pp, ENXIO);
1046	g_destroy_consumer(cp);
1047	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
1048		g_destroy_geom(gp);
1049	else
1050		gp->flags |= G_GEOM_WITHER;
1051}
1052
1053/*
1054 * Spoiling happens when a provider is opened for writing, but consumers
1055 * which are configured by in-band data are attached (slicers for instance).
1056 * Since the write might potentially change the in-band data, such consumers
1057 * need to re-evaluate their existence after the writing session closes.
1058 * We do this by (offering to) tear them down when the open for write happens
1059 * in return for a re-taste when it closes again.
1060 * Together with the fact that such consumers grab an 'e' bit whenever they
1061 * are open, regardless of mode, this ends up DTRT.
1062 */
1063
1064static void
1065g_spoil_event(void *arg, int flag)
1066{
1067	struct g_provider *pp;
1068	struct g_consumer *cp, *cp2;
1069
1070	g_topology_assert();
1071	if (flag == EV_CANCEL)
1072		return;
1073	pp = arg;
1074	G_VALID_PROVIDER(pp);
1075	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
1076		cp2 = LIST_NEXT(cp, consumers);
1077		if ((cp->flags & G_CF_SPOILED) == 0)
1078			continue;
1079		cp->flags &= ~G_CF_SPOILED;
1080		if (cp->geom->spoiled == NULL)
1081			continue;
1082		cp->geom->spoiled(cp);
1083		g_topology_assert();
1084	}
1085}
1086
1087void
1088g_spoil(struct g_provider *pp, struct g_consumer *cp)
1089{
1090	struct g_consumer *cp2;
1091
1092	g_topology_assert();
1093	G_VALID_PROVIDER(pp);
1094	G_VALID_CONSUMER(cp);
1095
1096	LIST_FOREACH(cp2, &pp->consumers, consumers) {
1097		if (cp2 == cp)
1098			continue;
1099/*
1100		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1101		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1102*/
1103		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1104		cp2->flags |= G_CF_SPOILED;
1105	}
1106	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1107}
1108
1109static void
1110g_media_changed_event(void *arg, int flag)
1111{
1112	struct g_provider *pp;
1113	int retaste;
1114
1115	g_topology_assert();
1116	if (flag == EV_CANCEL)
1117		return;
1118	pp = arg;
1119	G_VALID_PROVIDER(pp);
1120
1121	/*
1122	 * If provider was not open for writing, queue retaste after spoiling.
1123	 * If it was, retaste will happen automatically on close.
1124	 */
1125	retaste = (pp->acw == 0 && pp->error == 0 &&
1126	    !(pp->geom->flags & G_GEOM_WITHER));
1127	g_spoil_event(arg, flag);
1128	if (retaste)
1129		g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
1130}
1131
1132int
1133g_media_changed(struct g_provider *pp, int flag)
1134{
1135	struct g_consumer *cp;
1136
1137	LIST_FOREACH(cp, &pp->consumers, consumers)
1138		cp->flags |= G_CF_SPOILED;
1139	return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
1140}
1141
1142int
1143g_media_gone(struct g_provider *pp, int flag)
1144{
1145	struct g_consumer *cp;
1146
1147	LIST_FOREACH(cp, &pp->consumers, consumers)
1148		cp->flags |= G_CF_SPOILED;
1149	return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
1150}
1151
1152int
1153g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1154{
1155	int error, i;
1156
1157	i = len;
1158	error = g_io_getattr(attr, cp, &i, var);
1159	if (error)
1160		return (error);
1161	if (i != len)
1162		return (EINVAL);
1163	return (0);
1164}
1165
1166static int
1167g_get_device_prefix_len(const char *name)
1168{
1169	int len;
1170
1171	if (strncmp(name, "ada", 3) == 0)
1172		len = 3;
1173	else if (strncmp(name, "ad", 2) == 0)
1174		len = 2;
1175	else
1176		return (0);
1177	if (name[len] < '0' || name[len] > '9')
1178		return (0);
1179	do {
1180		len++;
1181	} while (name[len] >= '0' && name[len] <= '9');
1182	return (len);
1183}
1184
1185int
1186g_compare_names(const char *namea, const char *nameb)
1187{
1188	int deva, devb;
1189
1190	if (strcmp(namea, nameb) == 0)
1191		return (1);
1192	deva = g_get_device_prefix_len(namea);
1193	if (deva == 0)
1194		return (0);
1195	devb = g_get_device_prefix_len(nameb);
1196	if (devb == 0)
1197		return (0);
1198	if (strcmp(namea + deva, nameb + devb) == 0)
1199		return (1);
1200	return (0);
1201}
1202
1203#if defined(DIAGNOSTIC) || defined(DDB)
1204/*
1205 * This function walks the mesh and returns a non-zero integer if it
1206 * finds the argument pointer is an object. The return value indicates
1207 * which type of object it is believed to be. If topology is not locked,
1208 * this function is potentially dangerous, but we don't assert that the
1209 * topology lock is held when called from debugger.
1210 */
1211int
1212g_valid_obj(void const *ptr)
1213{
1214	struct g_class *mp;
1215	struct g_geom *gp;
1216	struct g_consumer *cp;
1217	struct g_provider *pp;
1218
1219#ifdef KDB
1220	if (kdb_active == 0)
1221#endif
1222		g_topology_assert();
1223
1224	LIST_FOREACH(mp, &g_classes, class) {
1225		if (ptr == mp)
1226			return (1);
1227		LIST_FOREACH(gp, &mp->geom, geom) {
1228			if (ptr == gp)
1229				return (2);
1230			LIST_FOREACH(cp, &gp->consumer, consumer)
1231				if (ptr == cp)
1232					return (3);
1233			LIST_FOREACH(pp, &gp->provider, provider)
1234				if (ptr == pp)
1235					return (4);
1236		}
1237	}
1238	return(0);
1239}
1240#endif
1241
1242#ifdef DDB
1243
1244#define	gprintf(...)	do {						\
1245	db_printf("%*s", indent, "");					\
1246	db_printf(__VA_ARGS__);						\
1247} while (0)
1248#define	gprintln(...)	do {						\
1249	gprintf(__VA_ARGS__);						\
1250	db_printf("\n");						\
1251} while (0)
1252
1253#define	ADDFLAG(obj, flag, sflag)	do {				\
1254	if ((obj)->flags & (flag)) {					\
1255		if (comma)						\
1256			strlcat(str, ",", size);			\
1257		strlcat(str, (sflag), size);				\
1258		comma = 1;						\
1259	}								\
1260} while (0)
1261
1262static char *
1263provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1264{
1265	int comma = 0;
1266
1267	bzero(str, size);
1268	if (pp->flags == 0) {
1269		strlcpy(str, "NONE", size);
1270		return (str);
1271	}
1272	ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1273	ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1274	return (str);
1275}
1276
1277static char *
1278geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1279{
1280	int comma = 0;
1281
1282	bzero(str, size);
1283	if (gp->flags == 0) {
1284		strlcpy(str, "NONE", size);
1285		return (str);
1286	}
1287	ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1288	return (str);
1289}
1290static void
1291db_show_geom_consumer(int indent, struct g_consumer *cp)
1292{
1293
1294	if (indent == 0) {
1295		gprintln("consumer: %p", cp);
1296		gprintln("  class:    %s (%p)", cp->geom->class->name,
1297		    cp->geom->class);
1298		gprintln("  geom:     %s (%p)", cp->geom->name, cp->geom);
1299		if (cp->provider == NULL)
1300			gprintln("  provider: none");
1301		else {
1302			gprintln("  provider: %s (%p)", cp->provider->name,
1303			    cp->provider);
1304		}
1305		gprintln("  access:   r%dw%de%d", cp->acr, cp->acw, cp->ace);
1306		gprintln("  flags:    0x%04x", cp->flags);
1307		gprintln("  nstart:   %u", cp->nstart);
1308		gprintln("  nend:     %u", cp->nend);
1309	} else {
1310		gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1311		    cp->provider != NULL ? cp->provider->name : "none",
1312		    cp->acr, cp->acw, cp->ace);
1313		if (cp->flags)
1314			db_printf(", flags=0x%04x", cp->flags);
1315		db_printf("\n");
1316	}
1317}
1318
1319static void
1320db_show_geom_provider(int indent, struct g_provider *pp)
1321{
1322	struct g_consumer *cp;
1323	char flags[64];
1324
1325	if (indent == 0) {
1326		gprintln("provider: %s (%p)", pp->name, pp);
1327		gprintln("  class:        %s (%p)", pp->geom->class->name,
1328		    pp->geom->class);
1329		gprintln("  geom:         %s (%p)", pp->geom->name, pp->geom);
1330		gprintln("  mediasize:    %jd", (intmax_t)pp->mediasize);
1331		gprintln("  sectorsize:   %u", pp->sectorsize);
1332		gprintln("  stripesize:   %u", pp->stripesize);
1333		gprintln("  stripeoffset: %u", pp->stripeoffset);
1334		gprintln("  access:       r%dw%de%d", pp->acr, pp->acw,
1335		    pp->ace);
1336		gprintln("  flags:        %s (0x%04x)",
1337		    provider_flags_to_string(pp, flags, sizeof(flags)),
1338		    pp->flags);
1339		gprintln("  error:        %d", pp->error);
1340		gprintln("  nstart:       %u", pp->nstart);
1341		gprintln("  nend:         %u", pp->nend);
1342		if (LIST_EMPTY(&pp->consumers))
1343			gprintln("  consumers:    none");
1344	} else {
1345		gprintf("provider: %s (%p), access=r%dw%de%d",
1346		    pp->name, pp, pp->acr, pp->acw, pp->ace);
1347		if (pp->flags != 0) {
1348			db_printf(", flags=%s (0x%04x)",
1349			    provider_flags_to_string(pp, flags, sizeof(flags)),
1350			    pp->flags);
1351		}
1352		db_printf("\n");
1353	}
1354	if (!LIST_EMPTY(&pp->consumers)) {
1355		LIST_FOREACH(cp, &pp->consumers, consumers) {
1356			db_show_geom_consumer(indent + 2, cp);
1357			if (db_pager_quit)
1358				break;
1359		}
1360	}
1361}
1362
1363static void
1364db_show_geom_geom(int indent, struct g_geom *gp)
1365{
1366	struct g_provider *pp;
1367	struct g_consumer *cp;
1368	char flags[64];
1369
1370	if (indent == 0) {
1371		gprintln("geom: %s (%p)", gp->name, gp);
1372		gprintln("  class:     %s (%p)", gp->class->name, gp->class);
1373		gprintln("  flags:     %s (0x%04x)",
1374		    geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1375		gprintln("  rank:      %d", gp->rank);
1376		if (LIST_EMPTY(&gp->provider))
1377			gprintln("  providers: none");
1378		if (LIST_EMPTY(&gp->consumer))
1379			gprintln("  consumers: none");
1380	} else {
1381		gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1382		if (gp->flags != 0) {
1383			db_printf(", flags=%s (0x%04x)",
1384			    geom_flags_to_string(gp, flags, sizeof(flags)),
1385			    gp->flags);
1386		}
1387		db_printf("\n");
1388	}
1389	if (!LIST_EMPTY(&gp->provider)) {
1390		LIST_FOREACH(pp, &gp->provider, provider) {
1391			db_show_geom_provider(indent + 2, pp);
1392			if (db_pager_quit)
1393				break;
1394		}
1395	}
1396	if (!LIST_EMPTY(&gp->consumer)) {
1397		LIST_FOREACH(cp, &gp->consumer, consumer) {
1398			db_show_geom_consumer(indent + 2, cp);
1399			if (db_pager_quit)
1400				break;
1401		}
1402	}
1403}
1404
1405static void
1406db_show_geom_class(struct g_class *mp)
1407{
1408	struct g_geom *gp;
1409
1410	db_printf("class: %s (%p)\n", mp->name, mp);
1411	LIST_FOREACH(gp, &mp->geom, geom) {
1412		db_show_geom_geom(2, gp);
1413		if (db_pager_quit)
1414			break;
1415	}
1416}
1417
1418/*
1419 * Print the GEOM topology or the given object.
1420 */
1421DB_SHOW_COMMAND(geom, db_show_geom)
1422{
1423	struct g_class *mp;
1424
1425	if (!have_addr) {
1426		/* No address given, print the entire topology. */
1427		LIST_FOREACH(mp, &g_classes, class) {
1428			db_show_geom_class(mp);
1429			db_printf("\n");
1430			if (db_pager_quit)
1431				break;
1432		}
1433	} else {
1434		switch (g_valid_obj((void *)addr)) {
1435		case 1:
1436			db_show_geom_class((struct g_class *)addr);
1437			break;
1438		case 2:
1439			db_show_geom_geom(0, (struct g_geom *)addr);
1440			break;
1441		case 3:
1442			db_show_geom_consumer(0, (struct g_consumer *)addr);
1443			break;
1444		case 4:
1445			db_show_geom_provider(0, (struct g_provider *)addr);
1446			break;
1447		default:
1448			db_printf("Not a GEOM object.\n");
1449			break;
1450		}
1451	}
1452}
1453
1454static void
1455db_print_bio_cmd(struct bio *bp)
1456{
1457	db_printf("  cmd: ");
1458	switch (bp->bio_cmd) {
1459	case BIO_READ: db_printf("BIO_READ"); break;
1460	case BIO_WRITE: db_printf("BIO_WRITE"); break;
1461	case BIO_DELETE: db_printf("BIO_DELETE"); break;
1462	case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1463	case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1464	case BIO_CMD0: db_printf("BIO_CMD0"); break;
1465	case BIO_CMD1: db_printf("BIO_CMD1"); break;
1466	case BIO_CMD2: db_printf("BIO_CMD2"); break;
1467	default: db_printf("UNKNOWN"); break;
1468	}
1469	db_printf("\n");
1470}
1471
1472static void
1473db_print_bio_flags(struct bio *bp)
1474{
1475	int comma;
1476
1477	comma = 0;
1478	db_printf("  flags: ");
1479	if (bp->bio_flags & BIO_ERROR) {
1480		db_printf("BIO_ERROR");
1481		comma = 1;
1482	}
1483	if (bp->bio_flags & BIO_DONE) {
1484		db_printf("%sBIO_DONE", (comma ? ", " : ""));
1485		comma = 1;
1486	}
1487	if (bp->bio_flags & BIO_ONQUEUE)
1488		db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1489	db_printf("\n");
1490}
1491
1492/*
1493 * Print useful information in a BIO
1494 */
1495DB_SHOW_COMMAND(bio, db_show_bio)
1496{
1497	struct bio *bp;
1498
1499	if (have_addr) {
1500		bp = (struct bio *)addr;
1501		db_printf("BIO %p\n", bp);
1502		db_print_bio_cmd(bp);
1503		db_print_bio_flags(bp);
1504		db_printf("  cflags: 0x%hhx\n", bp->bio_cflags);
1505		db_printf("  pflags: 0x%hhx\n", bp->bio_pflags);
1506		db_printf("  offset: %jd\n", (intmax_t)bp->bio_offset);
1507		db_printf("  length: %jd\n", (intmax_t)bp->bio_length);
1508		db_printf("  bcount: %ld\n", bp->bio_bcount);
1509		db_printf("  resid: %ld\n", bp->bio_resid);
1510		db_printf("  completed: %jd\n", (intmax_t)bp->bio_completed);
1511		db_printf("  children: %u\n", bp->bio_children);
1512		db_printf("  inbed: %u\n", bp->bio_inbed);
1513		db_printf("  error: %d\n", bp->bio_error);
1514		db_printf("  parent: %p\n", bp->bio_parent);
1515		db_printf("  driver1: %p\n", bp->bio_driver1);
1516		db_printf("  driver2: %p\n", bp->bio_driver2);
1517		db_printf("  caller1: %p\n", bp->bio_caller1);
1518		db_printf("  caller2: %p\n", bp->bio_caller2);
1519		db_printf("  bio_from: %p\n", bp->bio_from);
1520		db_printf("  bio_to: %p\n", bp->bio_to);
1521	}
1522}
1523
1524#undef	gprintf
1525#undef	gprintln
1526#undef	ADDFLAG
1527
1528#endif	/* DDB */
1529