geom_subr.c revision 238213
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/geom/geom_subr.c 238213 2012-07-07 20:13:40Z trasz $");
38
39#include "opt_ddb.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/devicestat.h>
44#include <sys/kernel.h>
45#include <sys/malloc.h>
46#include <sys/bio.h>
47#include <sys/sysctl.h>
48#include <sys/proc.h>
49#include <sys/kthread.h>
50#include <sys/lock.h>
51#include <sys/mutex.h>
52#include <sys/errno.h>
53#include <sys/sbuf.h>
54#include <geom/geom.h>
55#include <geom/geom_int.h>
56#include <machine/stdarg.h>
57
58#ifdef DDB
59#include <ddb/ddb.h>
60#endif
61
62#ifdef KDB
63#include <sys/kdb.h>
64#endif
65
66struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
67static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
68char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
69
70struct g_hh00 {
71	struct g_class		*mp;
72	struct g_provider	*pp;
73	off_t			size;
74	int			error;
75	int			post;
76};
77
78/*
79 * This event offers a new class a chance to taste all preexisting providers.
80 */
81static void
82g_load_class(void *arg, int flag)
83{
84	struct g_hh00 *hh;
85	struct g_class *mp2, *mp;
86	struct g_geom *gp;
87	struct g_provider *pp;
88
89	g_topology_assert();
90	if (flag == EV_CANCEL)	/* XXX: can't happen ? */
91		return;
92	if (g_shutdown)
93		return;
94
95	hh = arg;
96	mp = hh->mp;
97	hh->error = 0;
98	if (hh->post) {
99		g_free(hh);
100		hh = NULL;
101	}
102	g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
103	KASSERT(mp->name != NULL && *mp->name != '\0',
104	    ("GEOM class has no name"));
105	LIST_FOREACH(mp2, &g_classes, class) {
106		if (mp2 == mp) {
107			printf("The GEOM class %s is already loaded.\n",
108			    mp2->name);
109			if (hh != NULL)
110				hh->error = EEXIST;
111			return;
112		} else if (strcmp(mp2->name, mp->name) == 0) {
113			printf("A GEOM class %s is already loaded.\n",
114			    mp2->name);
115			if (hh != NULL)
116				hh->error = EEXIST;
117			return;
118		}
119	}
120
121	LIST_INIT(&mp->geom);
122	LIST_INSERT_HEAD(&g_classes, mp, class);
123	if (mp->init != NULL)
124		mp->init(mp);
125	if (mp->taste == NULL)
126		return;
127	LIST_FOREACH(mp2, &g_classes, class) {
128		if (mp == mp2)
129			continue;
130		LIST_FOREACH(gp, &mp2->geom, geom) {
131			LIST_FOREACH(pp, &gp->provider, provider) {
132				mp->taste(mp, pp, 0);
133				g_topology_assert();
134			}
135		}
136	}
137}
138
139static int
140g_unload_class(struct g_class *mp)
141{
142	struct g_geom *gp;
143	struct g_provider *pp;
144	struct g_consumer *cp;
145	int error;
146
147	g_topology_lock();
148	g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
149retry:
150	G_VALID_CLASS(mp);
151	LIST_FOREACH(gp, &mp->geom, geom) {
152		/* We refuse to unload if anything is open */
153		LIST_FOREACH(pp, &gp->provider, provider)
154			if (pp->acr || pp->acw || pp->ace) {
155				g_topology_unlock();
156				return (EBUSY);
157			}
158		LIST_FOREACH(cp, &gp->consumer, consumer)
159			if (cp->acr || cp->acw || cp->ace) {
160				g_topology_unlock();
161				return (EBUSY);
162			}
163		/* If the geom is withering, wait for it to finish. */
164		if (gp->flags & G_GEOM_WITHER) {
165			g_topology_sleep(mp, 1);
166			goto retry;
167		}
168	}
169
170	/*
171	 * We allow unloading if we have no geoms, or a class
172	 * method we can use to get rid of them.
173	 */
174	if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
175		g_topology_unlock();
176		return (EOPNOTSUPP);
177	}
178
179	/* Bar new entries */
180	mp->taste = NULL;
181	mp->config = NULL;
182
183	LIST_FOREACH(gp, &mp->geom, geom) {
184		error = mp->destroy_geom(NULL, mp, gp);
185		if (error != 0) {
186			g_topology_unlock();
187			return (error);
188		}
189	}
190	/* Wait for withering to finish. */
191	for (;;) {
192		gp = LIST_FIRST(&mp->geom);
193		if (gp == NULL)
194			break;
195		KASSERT(gp->flags & G_GEOM_WITHER,
196		   ("Non-withering geom in class %s", mp->name));
197		g_topology_sleep(mp, 1);
198	}
199	G_VALID_CLASS(mp);
200	if (mp->fini != NULL)
201		mp->fini(mp);
202	LIST_REMOVE(mp, class);
203	g_topology_unlock();
204
205	return (0);
206}
207
208int
209g_modevent(module_t mod, int type, void *data)
210{
211	struct g_hh00 *hh;
212	int error;
213	static int g_ignition;
214	struct g_class *mp;
215
216	mp = data;
217	if (mp->version != G_VERSION) {
218		printf("GEOM class %s has Wrong version %x\n",
219		    mp->name, mp->version);
220		return (EINVAL);
221	}
222	if (!g_ignition) {
223		g_ignition++;
224		g_init();
225	}
226	error = EOPNOTSUPP;
227	switch (type) {
228	case MOD_LOAD:
229		g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", mp->name);
230		hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
231		hh->mp = mp;
232		/*
233		 * Once the system is not cold, MOD_LOAD calls will be
234		 * from the userland and the g_event thread will be able
235		 * to acknowledge their completion.
236		 */
237		if (cold) {
238			hh->post = 1;
239			error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
240		} else {
241			error = g_waitfor_event(g_load_class, hh, M_WAITOK,
242			    NULL);
243			if (error == 0)
244				error = hh->error;
245			g_free(hh);
246		}
247		break;
248	case MOD_UNLOAD:
249		g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", mp->name);
250		DROP_GIANT();
251		error = g_unload_class(mp);
252		PICKUP_GIANT();
253		if (error == 0) {
254			KASSERT(LIST_EMPTY(&mp->geom),
255			    ("Unloaded class (%s) still has geom", mp->name));
256		}
257		break;
258	}
259	return (error);
260}
261
262static void
263g_retaste_event(void *arg, int flag)
264{
265	struct g_class *cp, *mp;
266	struct g_geom *gp, *gp2;
267	struct g_hh00 *hh;
268	struct g_provider *pp;
269
270	g_topology_assert();
271	if (flag == EV_CANCEL)  /* XXX: can't happen ? */
272		return;
273	if (g_shutdown)
274		return;
275
276	hh = arg;
277	mp = hh->mp;
278	hh->error = 0;
279	if (hh->post) {
280		g_free(hh);
281		hh = NULL;
282	}
283	g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name);
284
285	LIST_FOREACH(cp, &g_classes, class) {
286		LIST_FOREACH(gp, &cp->geom, geom) {
287			LIST_FOREACH(pp, &gp->provider, provider) {
288				if (pp->acr || pp->acw || pp->ace)
289					continue;
290				LIST_FOREACH(gp2, &mp->geom, geom) {
291					if (!strcmp(pp->name, gp2->name))
292						break;
293				}
294				if (gp2 != NULL)
295					g_wither_geom(gp2, ENXIO);
296				mp->taste(mp, pp, 0);
297				g_topology_assert();
298			}
299		}
300	}
301}
302
303int
304g_retaste(struct g_class *mp)
305{
306	struct g_hh00 *hh;
307	int error;
308
309	if (mp->taste == NULL)
310		return (EINVAL);
311
312	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
313	hh->mp = mp;
314
315	if (cold) {
316		hh->post = 1;
317		error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL);
318	} else {
319		error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL);
320		if (error == 0)
321			error = hh->error;
322		g_free(hh);
323	}
324
325	return (error);
326}
327
328struct g_geom *
329g_new_geomf(struct g_class *mp, const char *fmt, ...)
330{
331	struct g_geom *gp;
332	va_list ap;
333	struct sbuf *sb;
334
335	g_topology_assert();
336	G_VALID_CLASS(mp);
337	sb = sbuf_new_auto();
338	va_start(ap, fmt);
339	sbuf_vprintf(sb, fmt, ap);
340	va_end(ap);
341	sbuf_finish(sb);
342	gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
343	gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
344	gp->class = mp;
345	gp->rank = 1;
346	LIST_INIT(&gp->consumer);
347	LIST_INIT(&gp->provider);
348	LIST_INSERT_HEAD(&mp->geom, gp, geom);
349	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
350	strcpy(gp->name, sbuf_data(sb));
351	sbuf_delete(sb);
352	/* Fill in defaults from class */
353	gp->start = mp->start;
354	gp->spoiled = mp->spoiled;
355	gp->attrchanged = mp->attrchanged;
356	gp->providergone = mp->providergone;
357	gp->dumpconf = mp->dumpconf;
358	gp->access = mp->access;
359	gp->orphan = mp->orphan;
360	gp->ioctl = mp->ioctl;
361	gp->resize = mp->resize;
362	return (gp);
363}
364
365void
366g_destroy_geom(struct g_geom *gp)
367{
368
369	g_topology_assert();
370	G_VALID_GEOM(gp);
371	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
372	KASSERT(LIST_EMPTY(&gp->consumer),
373	    ("g_destroy_geom(%s) with consumer(s) [%p]",
374	    gp->name, LIST_FIRST(&gp->consumer)));
375	KASSERT(LIST_EMPTY(&gp->provider),
376	    ("g_destroy_geom(%s) with provider(s) [%p]",
377	    gp->name, LIST_FIRST(&gp->provider)));
378	g_cancel_event(gp);
379	LIST_REMOVE(gp, geom);
380	TAILQ_REMOVE(&geoms, gp, geoms);
381	g_free(gp->name);
382	g_free(gp);
383}
384
385/*
386 * This function is called (repeatedly) until the geom has withered away.
387 */
388void
389g_wither_geom(struct g_geom *gp, int error)
390{
391	struct g_provider *pp;
392
393	g_topology_assert();
394	G_VALID_GEOM(gp);
395	g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
396	if (!(gp->flags & G_GEOM_WITHER)) {
397		gp->flags |= G_GEOM_WITHER;
398		LIST_FOREACH(pp, &gp->provider, provider)
399			if (!(pp->flags & G_PF_ORPHAN))
400				g_orphan_provider(pp, error);
401	}
402	g_do_wither();
403}
404
405/*
406 * Convenience function to destroy a particular provider.
407 */
408void
409g_wither_provider(struct g_provider *pp, int error)
410{
411
412	pp->flags |= G_PF_WITHER;
413	if (!(pp->flags & G_PF_ORPHAN))
414		g_orphan_provider(pp, error);
415}
416
417/*
418 * This function is called (repeatedly) until the has withered away.
419 */
420void
421g_wither_geom_close(struct g_geom *gp, int error)
422{
423	struct g_consumer *cp;
424
425	g_topology_assert();
426	G_VALID_GEOM(gp);
427	g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
428	LIST_FOREACH(cp, &gp->consumer, consumer)
429		if (cp->acr || cp->acw || cp->ace)
430			g_access(cp, -cp->acr, -cp->acw, -cp->ace);
431	g_wither_geom(gp, error);
432}
433
434/*
435 * This function is called (repeatedly) until we cant wash away more
436 * withered bits at present.  Return value contains two bits.  Bit 0
437 * set means "withering stuff we can't wash now", bit 1 means "call
438 * me again, there may be stuff I didn't get the first time around.
439 */
440int
441g_wither_washer()
442{
443	struct g_class *mp;
444	struct g_geom *gp, *gp2;
445	struct g_provider *pp, *pp2;
446	struct g_consumer *cp, *cp2;
447	int result;
448
449	result = 0;
450	g_topology_assert();
451	LIST_FOREACH(mp, &g_classes, class) {
452		LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
453			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
454				if (!(pp->flags & G_PF_WITHER))
455					continue;
456				if (LIST_EMPTY(&pp->consumers))
457					g_destroy_provider(pp);
458				else
459					result |= 1;
460			}
461			if (!(gp->flags & G_GEOM_WITHER))
462				continue;
463			LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
464				if (LIST_EMPTY(&pp->consumers))
465					g_destroy_provider(pp);
466				else
467					result |= 1;
468			}
469			LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
470				if (cp->acr || cp->acw || cp->ace) {
471					result |= 1;
472					continue;
473				}
474				if (cp->provider != NULL)
475					g_detach(cp);
476				g_destroy_consumer(cp);
477				result |= 2;
478			}
479			if (LIST_EMPTY(&gp->provider) &&
480			    LIST_EMPTY(&gp->consumer))
481				g_destroy_geom(gp);
482			else
483				result |= 1;
484		}
485	}
486	return (result);
487}
488
489struct g_consumer *
490g_new_consumer(struct g_geom *gp)
491{
492	struct g_consumer *cp;
493
494	g_topology_assert();
495	G_VALID_GEOM(gp);
496	KASSERT(!(gp->flags & G_GEOM_WITHER),
497	    ("g_new_consumer on WITHERing geom(%s) (class %s)",
498	    gp->name, gp->class->name));
499	KASSERT(gp->orphan != NULL,
500	    ("g_new_consumer on geom(%s) (class %s) without orphan",
501	    gp->name, gp->class->name));
502
503	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
504	cp->geom = gp;
505	cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
506	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
507	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
508	return(cp);
509}
510
511void
512g_destroy_consumer(struct g_consumer *cp)
513{
514	struct g_geom *gp;
515
516	g_topology_assert();
517	G_VALID_CONSUMER(cp);
518	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
519	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
520	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
521	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
522	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
523	g_cancel_event(cp);
524	gp = cp->geom;
525	LIST_REMOVE(cp, consumer);
526	devstat_remove_entry(cp->stat);
527	g_free(cp);
528	if (gp->flags & G_GEOM_WITHER)
529		g_do_wither();
530}
531
532static void
533g_new_provider_event(void *arg, int flag)
534{
535	struct g_class *mp;
536	struct g_provider *pp;
537	struct g_consumer *cp;
538
539	g_topology_assert();
540	if (flag == EV_CANCEL)
541		return;
542	if (g_shutdown)
543		return;
544	pp = arg;
545	G_VALID_PROVIDER(pp);
546	KASSERT(!(pp->flags & G_PF_WITHER),
547	    ("g_new_provider_event but withered"));
548	LIST_FOREACH(mp, &g_classes, class) {
549		if (mp->taste == NULL)
550			continue;
551		LIST_FOREACH(cp, &pp->consumers, consumers)
552			if (cp->geom->class == mp)
553				break;
554		if (cp != NULL)
555			continue;
556		mp->taste(mp, pp, 0);
557		g_topology_assert();
558	}
559}
560
561
562struct g_provider *
563g_new_providerf(struct g_geom *gp, const char *fmt, ...)
564{
565	struct g_provider *pp;
566	struct sbuf *sb;
567	va_list ap;
568
569	g_topology_assert();
570	G_VALID_GEOM(gp);
571	KASSERT(gp->access != NULL,
572	    ("new provider on geom(%s) without ->access (class %s)",
573	    gp->name, gp->class->name));
574	KASSERT(gp->start != NULL,
575	    ("new provider on geom(%s) without ->start (class %s)",
576	    gp->name, gp->class->name));
577	KASSERT(!(gp->flags & G_GEOM_WITHER),
578	    ("new provider on WITHERing geom(%s) (class %s)",
579	    gp->name, gp->class->name));
580	sb = sbuf_new_auto();
581	va_start(ap, fmt);
582	sbuf_vprintf(sb, fmt, ap);
583	va_end(ap);
584	sbuf_finish(sb);
585	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
586	pp->name = (char *)(pp + 1);
587	strcpy(pp->name, sbuf_data(sb));
588	sbuf_delete(sb);
589	LIST_INIT(&pp->consumers);
590	pp->error = ENXIO;
591	pp->geom = gp;
592	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
593	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
594	LIST_INSERT_HEAD(&gp->provider, pp, provider);
595	g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
596	return (pp);
597}
598
599void
600g_error_provider(struct g_provider *pp, int error)
601{
602
603	/* G_VALID_PROVIDER(pp);  We may not have g_topology */
604	pp->error = error;
605}
606
607static void
608g_resize_provider_event(void *arg, int flag)
609{
610	struct g_hh00 *hh;
611	struct g_class *mp;
612	struct g_geom *gp;
613	struct g_provider *pp;
614	struct g_consumer *cp, *cp2;
615	off_t size;
616
617	g_topology_assert();
618	if (flag == EV_CANCEL)
619		return;
620	if (g_shutdown)
621		return;
622
623	hh = arg;
624	pp = hh->pp;
625	size = hh->size;
626
627	G_VALID_PROVIDER(pp);
628	g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
629
630	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
631		gp = cp->geom;
632		if (gp->resize == NULL && size < pp->mediasize)
633			cp->geom->orphan(cp);
634	}
635
636	pp->mediasize = size;
637
638	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
639		gp = cp->geom;
640		if (gp->resize != NULL)
641			gp->resize(cp);
642	}
643
644	/*
645	 * After resizing, the previously invalid GEOM class metadata
646	 * might become valid.  This means we should retaste.
647	 */
648	LIST_FOREACH(mp, &g_classes, class) {
649		if (mp->taste == NULL)
650			continue;
651		LIST_FOREACH(cp, &pp->consumers, consumers)
652			if (cp->geom->class == mp)
653				break;
654		if (cp != NULL)
655			continue;
656		mp->taste(mp, pp, 0);
657		g_topology_assert();
658	}
659}
660
661void
662g_resize_provider(struct g_provider *pp, off_t size)
663{
664	struct g_hh00 *hh;
665
666	G_VALID_PROVIDER(pp);
667
668	if (size == pp->mediasize)
669		return;
670
671	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
672	hh->pp = pp;
673	hh->size = size;
674	g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
675}
676
677struct g_provider *
678g_provider_by_name(char const *arg)
679{
680	struct g_class *cp;
681	struct g_geom *gp;
682	struct g_provider *pp;
683
684	LIST_FOREACH(cp, &g_classes, class) {
685		LIST_FOREACH(gp, &cp->geom, geom) {
686			LIST_FOREACH(pp, &gp->provider, provider) {
687				if (!strcmp(arg, pp->name))
688					return (pp);
689			}
690		}
691	}
692	return (NULL);
693}
694
695void
696g_destroy_provider(struct g_provider *pp)
697{
698	struct g_geom *gp;
699
700	g_topology_assert();
701	G_VALID_PROVIDER(pp);
702	KASSERT(LIST_EMPTY(&pp->consumers),
703	    ("g_destroy_provider but attached"));
704	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
705	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
706	KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
707	g_cancel_event(pp);
708	LIST_REMOVE(pp, provider);
709	gp = pp->geom;
710	devstat_remove_entry(pp->stat);
711	/*
712	 * If a callback was provided, send notification that the provider
713	 * is now gone.
714	 */
715	if (gp->providergone != NULL)
716		gp->providergone(pp);
717
718	g_free(pp);
719	if ((gp->flags & G_GEOM_WITHER))
720		g_do_wither();
721}
722
723/*
724 * We keep the "geoms" list sorted by topological order (== increasing
725 * numerical rank) at all times.
726 * When an attach is done, the attaching geoms rank is invalidated
727 * and it is moved to the tail of the list.
728 * All geoms later in the sequence has their ranks reevaluated in
729 * sequence.  If we cannot assign rank to a geom because it's
730 * prerequisites do not have rank, we move that element to the tail
731 * of the sequence with invalid rank as well.
732 * At some point we encounter our original geom and if we stil fail
733 * to assign it a rank, there must be a loop and we fail back to
734 * g_attach() which detach again and calls redo_rank again
735 * to fix up the damage.
736 * It would be much simpler code wise to do it recursively, but we
737 * can't risk that on the kernel stack.
738 */
739
740static int
741redo_rank(struct g_geom *gp)
742{
743	struct g_consumer *cp;
744	struct g_geom *gp1, *gp2;
745	int n, m;
746
747	g_topology_assert();
748	G_VALID_GEOM(gp);
749
750	/* Invalidate this geoms rank and move it to the tail */
751	gp1 = TAILQ_NEXT(gp, geoms);
752	if (gp1 != NULL) {
753		gp->rank = 0;
754		TAILQ_REMOVE(&geoms, gp, geoms);
755		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
756	} else {
757		gp1 = gp;
758	}
759
760	/* re-rank the rest of the sequence */
761	for (; gp1 != NULL; gp1 = gp2) {
762		gp1->rank = 0;
763		m = 1;
764		LIST_FOREACH(cp, &gp1->consumer, consumer) {
765			if (cp->provider == NULL)
766				continue;
767			n = cp->provider->geom->rank;
768			if (n == 0) {
769				m = 0;
770				break;
771			} else if (n >= m)
772				m = n + 1;
773		}
774		gp1->rank = m;
775		gp2 = TAILQ_NEXT(gp1, geoms);
776
777		/* got a rank, moving on */
778		if (m != 0)
779			continue;
780
781		/* no rank to original geom means loop */
782		if (gp == gp1)
783			return (ELOOP);
784
785		/* no rank, put it at the end move on */
786		TAILQ_REMOVE(&geoms, gp1, geoms);
787		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
788	}
789	return (0);
790}
791
792int
793g_attach(struct g_consumer *cp, struct g_provider *pp)
794{
795	int error;
796
797	g_topology_assert();
798	G_VALID_CONSUMER(cp);
799	G_VALID_PROVIDER(pp);
800	g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
801	KASSERT(cp->provider == NULL, ("attach but attached"));
802	cp->provider = pp;
803	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
804	error = redo_rank(cp->geom);
805	if (error) {
806		LIST_REMOVE(cp, consumers);
807		cp->provider = NULL;
808		redo_rank(cp->geom);
809	}
810	return (error);
811}
812
813void
814g_detach(struct g_consumer *cp)
815{
816	struct g_provider *pp;
817
818	g_topology_assert();
819	G_VALID_CONSUMER(cp);
820	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
821	KASSERT(cp->provider != NULL, ("detach but not attached"));
822	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
823	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
824	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
825	KASSERT(cp->nstart == cp->nend,
826	    ("detach with active requests"));
827	pp = cp->provider;
828	LIST_REMOVE(cp, consumers);
829	cp->provider = NULL;
830	if (pp->geom->flags & G_GEOM_WITHER)
831		g_do_wither();
832	else if (pp->flags & G_PF_WITHER)
833		g_do_wither();
834	redo_rank(cp->geom);
835}
836
837/*
838 * g_access()
839 *
840 * Access-check with delta values.  The question asked is "can provider
841 * "cp" change the access counters by the relative amounts dc[rwe] ?"
842 */
843
844int
845g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
846{
847	struct g_provider *pp;
848	int pr,pw,pe;
849	int error;
850
851	g_topology_assert();
852	G_VALID_CONSUMER(cp);
853	pp = cp->provider;
854	KASSERT(pp != NULL, ("access but not attached"));
855	G_VALID_PROVIDER(pp);
856
857	g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
858	    cp, pp->name, dcr, dcw, dce);
859
860	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
861	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
862	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
863	KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
864	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
865
866	/*
867	 * If our class cares about being spoiled, and we have been, we
868	 * are probably just ahead of the event telling us that.  Fail
869	 * now rather than having to unravel this later.
870	 */
871	if (cp->geom->spoiled != NULL && cp->spoiled &&
872	    (dcr > 0 || dcw > 0 || dce > 0))
873		return (ENXIO);
874
875	/*
876	 * Figure out what counts the provider would have had, if this
877	 * consumer had (r0w0e0) at this time.
878	 */
879	pr = pp->acr - cp->acr;
880	pw = pp->acw - cp->acw;
881	pe = pp->ace - cp->ace;
882
883	g_trace(G_T_ACCESS,
884    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
885	    dcr, dcw, dce,
886	    cp->acr, cp->acw, cp->ace,
887	    pp->acr, pp->acw, pp->ace,
888	    pp, pp->name);
889
890	/* If foot-shooting is enabled, any open on rank#1 is OK */
891	if ((g_debugflags & 16) && pp->geom->rank == 1)
892		;
893	/* If we try exclusive but already write: fail */
894	else if (dce > 0 && pw > 0)
895		return (EPERM);
896	/* If we try write but already exclusive: fail */
897	else if (dcw > 0 && pe > 0)
898		return (EPERM);
899	/* If we try to open more but provider is error'ed: fail */
900	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
901		return (pp->error);
902
903	/* Ok then... */
904
905	error = pp->geom->access(pp, dcr, dcw, dce);
906	KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
907	    ("Geom provider %s::%s failed closing ->access()",
908	    pp->geom->class->name, pp->name));
909	if (!error) {
910		/*
911		 * If we open first write, spoil any partner consumers.
912		 * If we close last write and provider is not errored,
913		 * trigger re-taste.
914		 */
915		if (pp->acw == 0 && dcw != 0)
916			g_spoil(pp, cp);
917		else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
918		    !(pp->geom->flags & G_GEOM_WITHER))
919			g_post_event(g_new_provider_event, pp, M_WAITOK,
920			    pp, NULL);
921
922		pp->acr += dcr;
923		pp->acw += dcw;
924		pp->ace += dce;
925		cp->acr += dcr;
926		cp->acw += dcw;
927		cp->ace += dce;
928		if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
929			KASSERT(pp->sectorsize > 0,
930			    ("Provider %s lacks sectorsize", pp->name));
931	}
932	return (error);
933}
934
935int
936g_handleattr_int(struct bio *bp, const char *attribute, int val)
937{
938
939	return (g_handleattr(bp, attribute, &val, sizeof val));
940}
941
942int
943g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
944{
945
946	return (g_handleattr(bp, attribute, &val, sizeof val));
947}
948
949int
950g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
951{
952
953	return (g_handleattr(bp, attribute, str, 0));
954}
955
956int
957g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
958{
959	int error = 0;
960
961	if (strcmp(bp->bio_attribute, attribute))
962		return (0);
963	if (len == 0) {
964		bzero(bp->bio_data, bp->bio_length);
965		if (strlcpy(bp->bio_data, val, bp->bio_length) >=
966		    bp->bio_length) {
967			printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
968			    __func__, bp->bio_to->name,
969			    (intmax_t)bp->bio_length, strlen(val));
970			error = EFAULT;
971		}
972	} else if (bp->bio_length == len) {
973		bcopy(val, bp->bio_data, len);
974	} else {
975		printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
976		    bp->bio_to->name, (intmax_t)bp->bio_length, len);
977		error = EFAULT;
978	}
979	if (error == 0)
980		bp->bio_completed = bp->bio_length;
981	g_io_deliver(bp, error);
982	return (1);
983}
984
985int
986g_std_access(struct g_provider *pp,
987	int dr __unused, int dw __unused, int de __unused)
988{
989
990	g_topology_assert();
991	G_VALID_PROVIDER(pp);
992        return (0);
993}
994
995void
996g_std_done(struct bio *bp)
997{
998	struct bio *bp2;
999
1000	bp2 = bp->bio_parent;
1001	if (bp2->bio_error == 0)
1002		bp2->bio_error = bp->bio_error;
1003	bp2->bio_completed += bp->bio_completed;
1004	g_destroy_bio(bp);
1005	bp2->bio_inbed++;
1006	if (bp2->bio_children == bp2->bio_inbed)
1007		g_io_deliver(bp2, bp2->bio_error);
1008}
1009
1010/* XXX: maybe this is only g_slice_spoiled */
1011
1012void
1013g_std_spoiled(struct g_consumer *cp)
1014{
1015	struct g_geom *gp;
1016	struct g_provider *pp;
1017
1018	g_topology_assert();
1019	G_VALID_CONSUMER(cp);
1020	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
1021	g_detach(cp);
1022	gp = cp->geom;
1023	LIST_FOREACH(pp, &gp->provider, provider)
1024		g_orphan_provider(pp, ENXIO);
1025	g_destroy_consumer(cp);
1026	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
1027		g_destroy_geom(gp);
1028	else
1029		gp->flags |= G_GEOM_WITHER;
1030}
1031
1032/*
1033 * Spoiling happens when a provider is opened for writing, but consumers
1034 * which are configured by in-band data are attached (slicers for instance).
1035 * Since the write might potentially change the in-band data, such consumers
1036 * need to re-evaluate their existence after the writing session closes.
1037 * We do this by (offering to) tear them down when the open for write happens
1038 * in return for a re-taste when it closes again.
1039 * Together with the fact that such consumers grab an 'e' bit whenever they
1040 * are open, regardless of mode, this ends up DTRT.
1041 */
1042
1043static void
1044g_spoil_event(void *arg, int flag)
1045{
1046	struct g_provider *pp;
1047	struct g_consumer *cp, *cp2;
1048
1049	g_topology_assert();
1050	if (flag == EV_CANCEL)
1051		return;
1052	pp = arg;
1053	G_VALID_PROVIDER(pp);
1054	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
1055		cp2 = LIST_NEXT(cp, consumers);
1056		if (!cp->spoiled)
1057			continue;
1058		cp->spoiled = 0;
1059		if (cp->geom->spoiled == NULL)
1060			continue;
1061		cp->geom->spoiled(cp);
1062		g_topology_assert();
1063	}
1064}
1065
1066void
1067g_spoil(struct g_provider *pp, struct g_consumer *cp)
1068{
1069	struct g_consumer *cp2;
1070
1071	g_topology_assert();
1072	G_VALID_PROVIDER(pp);
1073	G_VALID_CONSUMER(cp);
1074
1075	LIST_FOREACH(cp2, &pp->consumers, consumers) {
1076		if (cp2 == cp)
1077			continue;
1078/*
1079		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1080		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1081*/
1082		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1083		cp2->spoiled++;
1084	}
1085	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1086}
1087
1088int
1089g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1090{
1091	int error, i;
1092
1093	i = len;
1094	error = g_io_getattr(attr, cp, &i, var);
1095	if (error)
1096		return (error);
1097	if (i != len)
1098		return (EINVAL);
1099	return (0);
1100}
1101
1102static int
1103g_get_device_prefix_len(const char *name)
1104{
1105	int len;
1106
1107	if (strncmp(name, "ada", 3) == 0)
1108		len = 3;
1109	else if (strncmp(name, "ad", 2) == 0)
1110		len = 2;
1111	else
1112		return (0);
1113	if (name[len] < '0' || name[len] > '9')
1114		return (0);
1115	do {
1116		len++;
1117	} while (name[len] >= '0' && name[len] <= '9');
1118	return (len);
1119}
1120
1121int
1122g_compare_names(const char *namea, const char *nameb)
1123{
1124	int deva, devb;
1125
1126	if (strcmp(namea, nameb) == 0)
1127		return (1);
1128	deva = g_get_device_prefix_len(namea);
1129	if (deva == 0)
1130		return (0);
1131	devb = g_get_device_prefix_len(nameb);
1132	if (devb == 0)
1133		return (0);
1134	if (strcmp(namea + deva, nameb + devb) == 0)
1135		return (1);
1136	return (0);
1137}
1138
1139#if defined(DIAGNOSTIC) || defined(DDB)
1140/*
1141 * This function walks the mesh and returns a non-zero integer if it
1142 * finds the argument pointer is an object. The return value indicates
1143 * which type of object it is believed to be. If topology is not locked,
1144 * this function is potentially dangerous, but we don't assert that the
1145 * topology lock is held when called from debugger.
1146 */
1147int
1148g_valid_obj(void const *ptr)
1149{
1150	struct g_class *mp;
1151	struct g_geom *gp;
1152	struct g_consumer *cp;
1153	struct g_provider *pp;
1154
1155#ifdef KDB
1156	if (kdb_active == 0)
1157#endif
1158		g_topology_assert();
1159
1160	LIST_FOREACH(mp, &g_classes, class) {
1161		if (ptr == mp)
1162			return (1);
1163		LIST_FOREACH(gp, &mp->geom, geom) {
1164			if (ptr == gp)
1165				return (2);
1166			LIST_FOREACH(cp, &gp->consumer, consumer)
1167				if (ptr == cp)
1168					return (3);
1169			LIST_FOREACH(pp, &gp->provider, provider)
1170				if (ptr == pp)
1171					return (4);
1172		}
1173	}
1174	return(0);
1175}
1176#endif
1177
1178#ifdef DDB
1179
1180#define	gprintf(...)	do {						\
1181	db_printf("%*s", indent, "");					\
1182	db_printf(__VA_ARGS__);						\
1183} while (0)
1184#define	gprintln(...)	do {						\
1185	gprintf(__VA_ARGS__);						\
1186	db_printf("\n");						\
1187} while (0)
1188
1189#define	ADDFLAG(obj, flag, sflag)	do {				\
1190	if ((obj)->flags & (flag)) {					\
1191		if (comma)						\
1192			strlcat(str, ",", size);			\
1193		strlcat(str, (sflag), size);				\
1194		comma = 1;						\
1195	}								\
1196} while (0)
1197
1198static char *
1199provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1200{
1201	int comma = 0;
1202
1203	bzero(str, size);
1204	if (pp->flags == 0) {
1205		strlcpy(str, "NONE", size);
1206		return (str);
1207	}
1208	ADDFLAG(pp, G_PF_CANDELETE, "G_PF_CANDELETE");
1209	ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1210	ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1211	return (str);
1212}
1213
1214static char *
1215geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1216{
1217	int comma = 0;
1218
1219	bzero(str, size);
1220	if (gp->flags == 0) {
1221		strlcpy(str, "NONE", size);
1222		return (str);
1223	}
1224	ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1225	return (str);
1226}
1227static void
1228db_show_geom_consumer(int indent, struct g_consumer *cp)
1229{
1230
1231	if (indent == 0) {
1232		gprintln("consumer: %p", cp);
1233		gprintln("  class:    %s (%p)", cp->geom->class->name,
1234		    cp->geom->class);
1235		gprintln("  geom:     %s (%p)", cp->geom->name, cp->geom);
1236		if (cp->provider == NULL)
1237			gprintln("  provider: none");
1238		else {
1239			gprintln("  provider: %s (%p)", cp->provider->name,
1240			    cp->provider);
1241		}
1242		gprintln("  access:   r%dw%de%d", cp->acr, cp->acw, cp->ace);
1243		gprintln("  spoiled:  %d", cp->spoiled);
1244		gprintln("  nstart:   %u", cp->nstart);
1245		gprintln("  nend:     %u", cp->nend);
1246	} else {
1247		gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1248		    cp->provider != NULL ? cp->provider->name : "none",
1249		    cp->acr, cp->acw, cp->ace);
1250		if (cp->spoiled)
1251			db_printf(", spoiled=%d", cp->spoiled);
1252		db_printf("\n");
1253	}
1254}
1255
1256static void
1257db_show_geom_provider(int indent, struct g_provider *pp)
1258{
1259	struct g_consumer *cp;
1260	char flags[64];
1261
1262	if (indent == 0) {
1263		gprintln("provider: %s (%p)", pp->name, pp);
1264		gprintln("  class:        %s (%p)", pp->geom->class->name,
1265		    pp->geom->class);
1266		gprintln("  geom:         %s (%p)", pp->geom->name, pp->geom);
1267		gprintln("  mediasize:    %jd", (intmax_t)pp->mediasize);
1268		gprintln("  sectorsize:   %u", pp->sectorsize);
1269		gprintln("  stripesize:   %u", pp->stripesize);
1270		gprintln("  stripeoffset: %u", pp->stripeoffset);
1271		gprintln("  access:       r%dw%de%d", pp->acr, pp->acw,
1272		    pp->ace);
1273		gprintln("  flags:        %s (0x%04x)",
1274		    provider_flags_to_string(pp, flags, sizeof(flags)),
1275		    pp->flags);
1276		gprintln("  error:        %d", pp->error);
1277		gprintln("  nstart:       %u", pp->nstart);
1278		gprintln("  nend:         %u", pp->nend);
1279		if (LIST_EMPTY(&pp->consumers))
1280			gprintln("  consumers:    none");
1281	} else {
1282		gprintf("provider: %s (%p), access=r%dw%de%d",
1283		    pp->name, pp, pp->acr, pp->acw, pp->ace);
1284		if (pp->flags != 0) {
1285			db_printf(", flags=%s (0x%04x)",
1286			    provider_flags_to_string(pp, flags, sizeof(flags)),
1287			    pp->flags);
1288		}
1289		db_printf("\n");
1290	}
1291	if (!LIST_EMPTY(&pp->consumers)) {
1292		LIST_FOREACH(cp, &pp->consumers, consumers) {
1293			db_show_geom_consumer(indent + 2, cp);
1294			if (db_pager_quit)
1295				break;
1296		}
1297	}
1298}
1299
1300static void
1301db_show_geom_geom(int indent, struct g_geom *gp)
1302{
1303	struct g_provider *pp;
1304	struct g_consumer *cp;
1305	char flags[64];
1306
1307	if (indent == 0) {
1308		gprintln("geom: %s (%p)", gp->name, gp);
1309		gprintln("  class:     %s (%p)", gp->class->name, gp->class);
1310		gprintln("  flags:     %s (0x%04x)",
1311		    geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1312		gprintln("  rank:      %d", gp->rank);
1313		if (LIST_EMPTY(&gp->provider))
1314			gprintln("  providers: none");
1315		if (LIST_EMPTY(&gp->consumer))
1316			gprintln("  consumers: none");
1317	} else {
1318		gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1319		if (gp->flags != 0) {
1320			db_printf(", flags=%s (0x%04x)",
1321			    geom_flags_to_string(gp, flags, sizeof(flags)),
1322			    gp->flags);
1323		}
1324		db_printf("\n");
1325	}
1326	if (!LIST_EMPTY(&gp->provider)) {
1327		LIST_FOREACH(pp, &gp->provider, provider) {
1328			db_show_geom_provider(indent + 2, pp);
1329			if (db_pager_quit)
1330				break;
1331		}
1332	}
1333	if (!LIST_EMPTY(&gp->consumer)) {
1334		LIST_FOREACH(cp, &gp->consumer, consumer) {
1335			db_show_geom_consumer(indent + 2, cp);
1336			if (db_pager_quit)
1337				break;
1338		}
1339	}
1340}
1341
1342static void
1343db_show_geom_class(struct g_class *mp)
1344{
1345	struct g_geom *gp;
1346
1347	db_printf("class: %s (%p)\n", mp->name, mp);
1348	LIST_FOREACH(gp, &mp->geom, geom) {
1349		db_show_geom_geom(2, gp);
1350		if (db_pager_quit)
1351			break;
1352	}
1353}
1354
1355/*
1356 * Print the GEOM topology or the given object.
1357 */
1358DB_SHOW_COMMAND(geom, db_show_geom)
1359{
1360	struct g_class *mp;
1361
1362	if (!have_addr) {
1363		/* No address given, print the entire topology. */
1364		LIST_FOREACH(mp, &g_classes, class) {
1365			db_show_geom_class(mp);
1366			db_printf("\n");
1367			if (db_pager_quit)
1368				break;
1369		}
1370	} else {
1371		switch (g_valid_obj((void *)addr)) {
1372		case 1:
1373			db_show_geom_class((struct g_class *)addr);
1374			break;
1375		case 2:
1376			db_show_geom_geom(0, (struct g_geom *)addr);
1377			break;
1378		case 3:
1379			db_show_geom_consumer(0, (struct g_consumer *)addr);
1380			break;
1381		case 4:
1382			db_show_geom_provider(0, (struct g_provider *)addr);
1383			break;
1384		default:
1385			db_printf("Not a GEOM object.\n");
1386			break;
1387		}
1388	}
1389}
1390
1391static void
1392db_print_bio_cmd(struct bio *bp)
1393{
1394	db_printf("  cmd: ");
1395	switch (bp->bio_cmd) {
1396	case BIO_READ: db_printf("BIO_READ"); break;
1397	case BIO_WRITE: db_printf("BIO_WRITE"); break;
1398	case BIO_DELETE: db_printf("BIO_DELETE"); break;
1399	case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1400	case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1401	case BIO_CMD0: db_printf("BIO_CMD0"); break;
1402	case BIO_CMD1: db_printf("BIO_CMD1"); break;
1403	case BIO_CMD2: db_printf("BIO_CMD2"); break;
1404	default: db_printf("UNKNOWN"); break;
1405	}
1406	db_printf("\n");
1407}
1408
1409static void
1410db_print_bio_flags(struct bio *bp)
1411{
1412	int comma;
1413
1414	comma = 0;
1415	db_printf("  flags: ");
1416	if (bp->bio_flags & BIO_ERROR) {
1417		db_printf("BIO_ERROR");
1418		comma = 1;
1419	}
1420	if (bp->bio_flags & BIO_DONE) {
1421		db_printf("%sBIO_DONE", (comma ? ", " : ""));
1422		comma = 1;
1423	}
1424	if (bp->bio_flags & BIO_ONQUEUE)
1425		db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1426	db_printf("\n");
1427}
1428
1429/*
1430 * Print useful information in a BIO
1431 */
1432DB_SHOW_COMMAND(bio, db_show_bio)
1433{
1434	struct bio *bp;
1435
1436	if (have_addr) {
1437		bp = (struct bio *)addr;
1438		db_printf("BIO %p\n", bp);
1439		db_print_bio_cmd(bp);
1440		db_print_bio_flags(bp);
1441		db_printf("  cflags: 0x%hhx\n", bp->bio_cflags);
1442		db_printf("  pflags: 0x%hhx\n", bp->bio_pflags);
1443		db_printf("  offset: %jd\n", (intmax_t)bp->bio_offset);
1444		db_printf("  length: %jd\n", (intmax_t)bp->bio_length);
1445		db_printf("  bcount: %ld\n", bp->bio_bcount);
1446		db_printf("  resid: %ld\n", bp->bio_resid);
1447		db_printf("  completed: %jd\n", (intmax_t)bp->bio_completed);
1448		db_printf("  children: %u\n", bp->bio_children);
1449		db_printf("  inbed: %u\n", bp->bio_inbed);
1450		db_printf("  error: %d\n", bp->bio_error);
1451		db_printf("  parent: %p\n", bp->bio_parent);
1452		db_printf("  driver1: %p\n", bp->bio_driver1);
1453		db_printf("  driver2: %p\n", bp->bio_driver2);
1454		db_printf("  caller1: %p\n", bp->bio_caller1);
1455		db_printf("  caller2: %p\n", bp->bio_caller2);
1456		db_printf("  bio_from: %p\n", bp->bio_from);
1457		db_printf("  bio_to: %p\n", bp->bio_to);
1458	}
1459}
1460
1461#undef	gprintf
1462#undef	gprintln
1463#undef	ADDFLAG
1464
1465#endif	/* DDB */
1466