geom_subr.c revision 238565
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 238565 2012-07-18 07:26:20Z 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 (g_shutdown)
619		return;
620
621	hh = arg;
622	pp = hh->pp;
623	size = hh->size;
624	g_free(hh);
625
626	G_VALID_PROVIDER(pp);
627	g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
628
629	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
630		gp = cp->geom;
631		if (gp->resize == NULL && size < pp->mediasize)
632			cp->geom->orphan(cp);
633	}
634
635	pp->mediasize = size;
636
637	LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
638		gp = cp->geom;
639		if (gp->resize != NULL)
640			gp->resize(cp);
641	}
642
643	/*
644	 * After resizing, the previously invalid GEOM class metadata
645	 * might become valid.  This means we should retaste.
646	 */
647	LIST_FOREACH(mp, &g_classes, class) {
648		if (mp->taste == NULL)
649			continue;
650		LIST_FOREACH(cp, &pp->consumers, consumers)
651			if (cp->geom->class == mp)
652				break;
653		if (cp != NULL)
654			continue;
655		mp->taste(mp, pp, 0);
656		g_topology_assert();
657	}
658}
659
660void
661g_resize_provider(struct g_provider *pp, off_t size)
662{
663	struct g_hh00 *hh;
664
665	G_VALID_PROVIDER(pp);
666
667	if (size == pp->mediasize)
668		return;
669
670	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
671	hh->pp = pp;
672	hh->size = size;
673	g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
674}
675
676struct g_provider *
677g_provider_by_name(char const *arg)
678{
679	struct g_class *cp;
680	struct g_geom *gp;
681	struct g_provider *pp;
682
683	LIST_FOREACH(cp, &g_classes, class) {
684		LIST_FOREACH(gp, &cp->geom, geom) {
685			LIST_FOREACH(pp, &gp->provider, provider) {
686				if (!strcmp(arg, pp->name))
687					return (pp);
688			}
689		}
690	}
691	return (NULL);
692}
693
694void
695g_destroy_provider(struct g_provider *pp)
696{
697	struct g_geom *gp;
698
699	g_topology_assert();
700	G_VALID_PROVIDER(pp);
701	KASSERT(LIST_EMPTY(&pp->consumers),
702	    ("g_destroy_provider but attached"));
703	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
704	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
705	KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
706	g_cancel_event(pp);
707	LIST_REMOVE(pp, provider);
708	gp = pp->geom;
709	devstat_remove_entry(pp->stat);
710	/*
711	 * If a callback was provided, send notification that the provider
712	 * is now gone.
713	 */
714	if (gp->providergone != NULL)
715		gp->providergone(pp);
716
717	g_free(pp);
718	if ((gp->flags & G_GEOM_WITHER))
719		g_do_wither();
720}
721
722/*
723 * We keep the "geoms" list sorted by topological order (== increasing
724 * numerical rank) at all times.
725 * When an attach is done, the attaching geoms rank is invalidated
726 * and it is moved to the tail of the list.
727 * All geoms later in the sequence has their ranks reevaluated in
728 * sequence.  If we cannot assign rank to a geom because it's
729 * prerequisites do not have rank, we move that element to the tail
730 * of the sequence with invalid rank as well.
731 * At some point we encounter our original geom and if we stil fail
732 * to assign it a rank, there must be a loop and we fail back to
733 * g_attach() which detach again and calls redo_rank again
734 * to fix up the damage.
735 * It would be much simpler code wise to do it recursively, but we
736 * can't risk that on the kernel stack.
737 */
738
739static int
740redo_rank(struct g_geom *gp)
741{
742	struct g_consumer *cp;
743	struct g_geom *gp1, *gp2;
744	int n, m;
745
746	g_topology_assert();
747	G_VALID_GEOM(gp);
748
749	/* Invalidate this geoms rank and move it to the tail */
750	gp1 = TAILQ_NEXT(gp, geoms);
751	if (gp1 != NULL) {
752		gp->rank = 0;
753		TAILQ_REMOVE(&geoms, gp, geoms);
754		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
755	} else {
756		gp1 = gp;
757	}
758
759	/* re-rank the rest of the sequence */
760	for (; gp1 != NULL; gp1 = gp2) {
761		gp1->rank = 0;
762		m = 1;
763		LIST_FOREACH(cp, &gp1->consumer, consumer) {
764			if (cp->provider == NULL)
765				continue;
766			n = cp->provider->geom->rank;
767			if (n == 0) {
768				m = 0;
769				break;
770			} else if (n >= m)
771				m = n + 1;
772		}
773		gp1->rank = m;
774		gp2 = TAILQ_NEXT(gp1, geoms);
775
776		/* got a rank, moving on */
777		if (m != 0)
778			continue;
779
780		/* no rank to original geom means loop */
781		if (gp == gp1)
782			return (ELOOP);
783
784		/* no rank, put it at the end move on */
785		TAILQ_REMOVE(&geoms, gp1, geoms);
786		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
787	}
788	return (0);
789}
790
791int
792g_attach(struct g_consumer *cp, struct g_provider *pp)
793{
794	int error;
795
796	g_topology_assert();
797	G_VALID_CONSUMER(cp);
798	G_VALID_PROVIDER(pp);
799	g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
800	KASSERT(cp->provider == NULL, ("attach but attached"));
801	cp->provider = pp;
802	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
803	error = redo_rank(cp->geom);
804	if (error) {
805		LIST_REMOVE(cp, consumers);
806		cp->provider = NULL;
807		redo_rank(cp->geom);
808	}
809	return (error);
810}
811
812void
813g_detach(struct g_consumer *cp)
814{
815	struct g_provider *pp;
816
817	g_topology_assert();
818	G_VALID_CONSUMER(cp);
819	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
820	KASSERT(cp->provider != NULL, ("detach but not attached"));
821	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
822	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
823	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
824	KASSERT(cp->nstart == cp->nend,
825	    ("detach with active requests"));
826	pp = cp->provider;
827	LIST_REMOVE(cp, consumers);
828	cp->provider = NULL;
829	if (pp->geom->flags & G_GEOM_WITHER)
830		g_do_wither();
831	else if (pp->flags & G_PF_WITHER)
832		g_do_wither();
833	redo_rank(cp->geom);
834}
835
836/*
837 * g_access()
838 *
839 * Access-check with delta values.  The question asked is "can provider
840 * "cp" change the access counters by the relative amounts dc[rwe] ?"
841 */
842
843int
844g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
845{
846	struct g_provider *pp;
847	int pr,pw,pe;
848	int error;
849
850	g_topology_assert();
851	G_VALID_CONSUMER(cp);
852	pp = cp->provider;
853	KASSERT(pp != NULL, ("access but not attached"));
854	G_VALID_PROVIDER(pp);
855
856	g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
857	    cp, pp->name, dcr, dcw, dce);
858
859	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
860	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
861	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
862	KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
863	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
864
865	/*
866	 * If our class cares about being spoiled, and we have been, we
867	 * are probably just ahead of the event telling us that.  Fail
868	 * now rather than having to unravel this later.
869	 */
870	if (cp->geom->spoiled != NULL && cp->spoiled &&
871	    (dcr > 0 || dcw > 0 || dce > 0))
872		return (ENXIO);
873
874	/*
875	 * Figure out what counts the provider would have had, if this
876	 * consumer had (r0w0e0) at this time.
877	 */
878	pr = pp->acr - cp->acr;
879	pw = pp->acw - cp->acw;
880	pe = pp->ace - cp->ace;
881
882	g_trace(G_T_ACCESS,
883    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
884	    dcr, dcw, dce,
885	    cp->acr, cp->acw, cp->ace,
886	    pp->acr, pp->acw, pp->ace,
887	    pp, pp->name);
888
889	/* If foot-shooting is enabled, any open on rank#1 is OK */
890	if ((g_debugflags & 16) && pp->geom->rank == 1)
891		;
892	/* If we try exclusive but already write: fail */
893	else if (dce > 0 && pw > 0)
894		return (EPERM);
895	/* If we try write but already exclusive: fail */
896	else if (dcw > 0 && pe > 0)
897		return (EPERM);
898	/* If we try to open more but provider is error'ed: fail */
899	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
900		return (pp->error);
901
902	/* Ok then... */
903
904	error = pp->geom->access(pp, dcr, dcw, dce);
905	KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
906	    ("Geom provider %s::%s failed closing ->access()",
907	    pp->geom->class->name, pp->name));
908	if (!error) {
909		/*
910		 * If we open first write, spoil any partner consumers.
911		 * If we close last write and provider is not errored,
912		 * trigger re-taste.
913		 */
914		if (pp->acw == 0 && dcw != 0)
915			g_spoil(pp, cp);
916		else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
917		    !(pp->geom->flags & G_GEOM_WITHER))
918			g_post_event(g_new_provider_event, pp, M_WAITOK,
919			    pp, NULL);
920
921		pp->acr += dcr;
922		pp->acw += dcw;
923		pp->ace += dce;
924		cp->acr += dcr;
925		cp->acw += dcw;
926		cp->ace += dce;
927		if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
928			KASSERT(pp->sectorsize > 0,
929			    ("Provider %s lacks sectorsize", pp->name));
930	}
931	return (error);
932}
933
934int
935g_handleattr_int(struct bio *bp, const char *attribute, int val)
936{
937
938	return (g_handleattr(bp, attribute, &val, sizeof val));
939}
940
941int
942g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
943{
944
945	return (g_handleattr(bp, attribute, &val, sizeof val));
946}
947
948int
949g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
950{
951
952	return (g_handleattr(bp, attribute, str, 0));
953}
954
955int
956g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
957{
958	int error = 0;
959
960	if (strcmp(bp->bio_attribute, attribute))
961		return (0);
962	if (len == 0) {
963		bzero(bp->bio_data, bp->bio_length);
964		if (strlcpy(bp->bio_data, val, bp->bio_length) >=
965		    bp->bio_length) {
966			printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
967			    __func__, bp->bio_to->name,
968			    (intmax_t)bp->bio_length, strlen(val));
969			error = EFAULT;
970		}
971	} else if (bp->bio_length == len) {
972		bcopy(val, bp->bio_data, len);
973	} else {
974		printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
975		    bp->bio_to->name, (intmax_t)bp->bio_length, len);
976		error = EFAULT;
977	}
978	if (error == 0)
979		bp->bio_completed = bp->bio_length;
980	g_io_deliver(bp, error);
981	return (1);
982}
983
984int
985g_std_access(struct g_provider *pp,
986	int dr __unused, int dw __unused, int de __unused)
987{
988
989	g_topology_assert();
990	G_VALID_PROVIDER(pp);
991        return (0);
992}
993
994void
995g_std_done(struct bio *bp)
996{
997	struct bio *bp2;
998
999	bp2 = bp->bio_parent;
1000	if (bp2->bio_error == 0)
1001		bp2->bio_error = bp->bio_error;
1002	bp2->bio_completed += bp->bio_completed;
1003	g_destroy_bio(bp);
1004	bp2->bio_inbed++;
1005	if (bp2->bio_children == bp2->bio_inbed)
1006		g_io_deliver(bp2, bp2->bio_error);
1007}
1008
1009/* XXX: maybe this is only g_slice_spoiled */
1010
1011void
1012g_std_spoiled(struct g_consumer *cp)
1013{
1014	struct g_geom *gp;
1015	struct g_provider *pp;
1016
1017	g_topology_assert();
1018	G_VALID_CONSUMER(cp);
1019	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
1020	g_detach(cp);
1021	gp = cp->geom;
1022	LIST_FOREACH(pp, &gp->provider, provider)
1023		g_orphan_provider(pp, ENXIO);
1024	g_destroy_consumer(cp);
1025	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
1026		g_destroy_geom(gp);
1027	else
1028		gp->flags |= G_GEOM_WITHER;
1029}
1030
1031/*
1032 * Spoiling happens when a provider is opened for writing, but consumers
1033 * which are configured by in-band data are attached (slicers for instance).
1034 * Since the write might potentially change the in-band data, such consumers
1035 * need to re-evaluate their existence after the writing session closes.
1036 * We do this by (offering to) tear them down when the open for write happens
1037 * in return for a re-taste when it closes again.
1038 * Together with the fact that such consumers grab an 'e' bit whenever they
1039 * are open, regardless of mode, this ends up DTRT.
1040 */
1041
1042static void
1043g_spoil_event(void *arg, int flag)
1044{
1045	struct g_provider *pp;
1046	struct g_consumer *cp, *cp2;
1047
1048	g_topology_assert();
1049	if (flag == EV_CANCEL)
1050		return;
1051	pp = arg;
1052	G_VALID_PROVIDER(pp);
1053	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
1054		cp2 = LIST_NEXT(cp, consumers);
1055		if (!cp->spoiled)
1056			continue;
1057		cp->spoiled = 0;
1058		if (cp->geom->spoiled == NULL)
1059			continue;
1060		cp->geom->spoiled(cp);
1061		g_topology_assert();
1062	}
1063}
1064
1065void
1066g_spoil(struct g_provider *pp, struct g_consumer *cp)
1067{
1068	struct g_consumer *cp2;
1069
1070	g_topology_assert();
1071	G_VALID_PROVIDER(pp);
1072	G_VALID_CONSUMER(cp);
1073
1074	LIST_FOREACH(cp2, &pp->consumers, consumers) {
1075		if (cp2 == cp)
1076			continue;
1077/*
1078		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1079		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1080*/
1081		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1082		cp2->spoiled++;
1083	}
1084	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1085}
1086
1087int
1088g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1089{
1090	int error, i;
1091
1092	i = len;
1093	error = g_io_getattr(attr, cp, &i, var);
1094	if (error)
1095		return (error);
1096	if (i != len)
1097		return (EINVAL);
1098	return (0);
1099}
1100
1101static int
1102g_get_device_prefix_len(const char *name)
1103{
1104	int len;
1105
1106	if (strncmp(name, "ada", 3) == 0)
1107		len = 3;
1108	else if (strncmp(name, "ad", 2) == 0)
1109		len = 2;
1110	else
1111		return (0);
1112	if (name[len] < '0' || name[len] > '9')
1113		return (0);
1114	do {
1115		len++;
1116	} while (name[len] >= '0' && name[len] <= '9');
1117	return (len);
1118}
1119
1120int
1121g_compare_names(const char *namea, const char *nameb)
1122{
1123	int deva, devb;
1124
1125	if (strcmp(namea, nameb) == 0)
1126		return (1);
1127	deva = g_get_device_prefix_len(namea);
1128	if (deva == 0)
1129		return (0);
1130	devb = g_get_device_prefix_len(nameb);
1131	if (devb == 0)
1132		return (0);
1133	if (strcmp(namea + deva, nameb + devb) == 0)
1134		return (1);
1135	return (0);
1136}
1137
1138#if defined(DIAGNOSTIC) || defined(DDB)
1139/*
1140 * This function walks the mesh and returns a non-zero integer if it
1141 * finds the argument pointer is an object. The return value indicates
1142 * which type of object it is believed to be. If topology is not locked,
1143 * this function is potentially dangerous, but we don't assert that the
1144 * topology lock is held when called from debugger.
1145 */
1146int
1147g_valid_obj(void const *ptr)
1148{
1149	struct g_class *mp;
1150	struct g_geom *gp;
1151	struct g_consumer *cp;
1152	struct g_provider *pp;
1153
1154#ifdef KDB
1155	if (kdb_active == 0)
1156#endif
1157		g_topology_assert();
1158
1159	LIST_FOREACH(mp, &g_classes, class) {
1160		if (ptr == mp)
1161			return (1);
1162		LIST_FOREACH(gp, &mp->geom, geom) {
1163			if (ptr == gp)
1164				return (2);
1165			LIST_FOREACH(cp, &gp->consumer, consumer)
1166				if (ptr == cp)
1167					return (3);
1168			LIST_FOREACH(pp, &gp->provider, provider)
1169				if (ptr == pp)
1170					return (4);
1171		}
1172	}
1173	return(0);
1174}
1175#endif
1176
1177#ifdef DDB
1178
1179#define	gprintf(...)	do {						\
1180	db_printf("%*s", indent, "");					\
1181	db_printf(__VA_ARGS__);						\
1182} while (0)
1183#define	gprintln(...)	do {						\
1184	gprintf(__VA_ARGS__);						\
1185	db_printf("\n");						\
1186} while (0)
1187
1188#define	ADDFLAG(obj, flag, sflag)	do {				\
1189	if ((obj)->flags & (flag)) {					\
1190		if (comma)						\
1191			strlcat(str, ",", size);			\
1192		strlcat(str, (sflag), size);				\
1193		comma = 1;						\
1194	}								\
1195} while (0)
1196
1197static char *
1198provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1199{
1200	int comma = 0;
1201
1202	bzero(str, size);
1203	if (pp->flags == 0) {
1204		strlcpy(str, "NONE", size);
1205		return (str);
1206	}
1207	ADDFLAG(pp, G_PF_CANDELETE, "G_PF_CANDELETE");
1208	ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1209	ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1210	return (str);
1211}
1212
1213static char *
1214geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1215{
1216	int comma = 0;
1217
1218	bzero(str, size);
1219	if (gp->flags == 0) {
1220		strlcpy(str, "NONE", size);
1221		return (str);
1222	}
1223	ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1224	return (str);
1225}
1226static void
1227db_show_geom_consumer(int indent, struct g_consumer *cp)
1228{
1229
1230	if (indent == 0) {
1231		gprintln("consumer: %p", cp);
1232		gprintln("  class:    %s (%p)", cp->geom->class->name,
1233		    cp->geom->class);
1234		gprintln("  geom:     %s (%p)", cp->geom->name, cp->geom);
1235		if (cp->provider == NULL)
1236			gprintln("  provider: none");
1237		else {
1238			gprintln("  provider: %s (%p)", cp->provider->name,
1239			    cp->provider);
1240		}
1241		gprintln("  access:   r%dw%de%d", cp->acr, cp->acw, cp->ace);
1242		gprintln("  spoiled:  %d", cp->spoiled);
1243		gprintln("  nstart:   %u", cp->nstart);
1244		gprintln("  nend:     %u", cp->nend);
1245	} else {
1246		gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1247		    cp->provider != NULL ? cp->provider->name : "none",
1248		    cp->acr, cp->acw, cp->ace);
1249		if (cp->spoiled)
1250			db_printf(", spoiled=%d", cp->spoiled);
1251		db_printf("\n");
1252	}
1253}
1254
1255static void
1256db_show_geom_provider(int indent, struct g_provider *pp)
1257{
1258	struct g_consumer *cp;
1259	char flags[64];
1260
1261	if (indent == 0) {
1262		gprintln("provider: %s (%p)", pp->name, pp);
1263		gprintln("  class:        %s (%p)", pp->geom->class->name,
1264		    pp->geom->class);
1265		gprintln("  geom:         %s (%p)", pp->geom->name, pp->geom);
1266		gprintln("  mediasize:    %jd", (intmax_t)pp->mediasize);
1267		gprintln("  sectorsize:   %u", pp->sectorsize);
1268		gprintln("  stripesize:   %u", pp->stripesize);
1269		gprintln("  stripeoffset: %u", pp->stripeoffset);
1270		gprintln("  access:       r%dw%de%d", pp->acr, pp->acw,
1271		    pp->ace);
1272		gprintln("  flags:        %s (0x%04x)",
1273		    provider_flags_to_string(pp, flags, sizeof(flags)),
1274		    pp->flags);
1275		gprintln("  error:        %d", pp->error);
1276		gprintln("  nstart:       %u", pp->nstart);
1277		gprintln("  nend:         %u", pp->nend);
1278		if (LIST_EMPTY(&pp->consumers))
1279			gprintln("  consumers:    none");
1280	} else {
1281		gprintf("provider: %s (%p), access=r%dw%de%d",
1282		    pp->name, pp, pp->acr, pp->acw, pp->ace);
1283		if (pp->flags != 0) {
1284			db_printf(", flags=%s (0x%04x)",
1285			    provider_flags_to_string(pp, flags, sizeof(flags)),
1286			    pp->flags);
1287		}
1288		db_printf("\n");
1289	}
1290	if (!LIST_EMPTY(&pp->consumers)) {
1291		LIST_FOREACH(cp, &pp->consumers, consumers) {
1292			db_show_geom_consumer(indent + 2, cp);
1293			if (db_pager_quit)
1294				break;
1295		}
1296	}
1297}
1298
1299static void
1300db_show_geom_geom(int indent, struct g_geom *gp)
1301{
1302	struct g_provider *pp;
1303	struct g_consumer *cp;
1304	char flags[64];
1305
1306	if (indent == 0) {
1307		gprintln("geom: %s (%p)", gp->name, gp);
1308		gprintln("  class:     %s (%p)", gp->class->name, gp->class);
1309		gprintln("  flags:     %s (0x%04x)",
1310		    geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1311		gprintln("  rank:      %d", gp->rank);
1312		if (LIST_EMPTY(&gp->provider))
1313			gprintln("  providers: none");
1314		if (LIST_EMPTY(&gp->consumer))
1315			gprintln("  consumers: none");
1316	} else {
1317		gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1318		if (gp->flags != 0) {
1319			db_printf(", flags=%s (0x%04x)",
1320			    geom_flags_to_string(gp, flags, sizeof(flags)),
1321			    gp->flags);
1322		}
1323		db_printf("\n");
1324	}
1325	if (!LIST_EMPTY(&gp->provider)) {
1326		LIST_FOREACH(pp, &gp->provider, provider) {
1327			db_show_geom_provider(indent + 2, pp);
1328			if (db_pager_quit)
1329				break;
1330		}
1331	}
1332	if (!LIST_EMPTY(&gp->consumer)) {
1333		LIST_FOREACH(cp, &gp->consumer, consumer) {
1334			db_show_geom_consumer(indent + 2, cp);
1335			if (db_pager_quit)
1336				break;
1337		}
1338	}
1339}
1340
1341static void
1342db_show_geom_class(struct g_class *mp)
1343{
1344	struct g_geom *gp;
1345
1346	db_printf("class: %s (%p)\n", mp->name, mp);
1347	LIST_FOREACH(gp, &mp->geom, geom) {
1348		db_show_geom_geom(2, gp);
1349		if (db_pager_quit)
1350			break;
1351	}
1352}
1353
1354/*
1355 * Print the GEOM topology or the given object.
1356 */
1357DB_SHOW_COMMAND(geom, db_show_geom)
1358{
1359	struct g_class *mp;
1360
1361	if (!have_addr) {
1362		/* No address given, print the entire topology. */
1363		LIST_FOREACH(mp, &g_classes, class) {
1364			db_show_geom_class(mp);
1365			db_printf("\n");
1366			if (db_pager_quit)
1367				break;
1368		}
1369	} else {
1370		switch (g_valid_obj((void *)addr)) {
1371		case 1:
1372			db_show_geom_class((struct g_class *)addr);
1373			break;
1374		case 2:
1375			db_show_geom_geom(0, (struct g_geom *)addr);
1376			break;
1377		case 3:
1378			db_show_geom_consumer(0, (struct g_consumer *)addr);
1379			break;
1380		case 4:
1381			db_show_geom_provider(0, (struct g_provider *)addr);
1382			break;
1383		default:
1384			db_printf("Not a GEOM object.\n");
1385			break;
1386		}
1387	}
1388}
1389
1390static void
1391db_print_bio_cmd(struct bio *bp)
1392{
1393	db_printf("  cmd: ");
1394	switch (bp->bio_cmd) {
1395	case BIO_READ: db_printf("BIO_READ"); break;
1396	case BIO_WRITE: db_printf("BIO_WRITE"); break;
1397	case BIO_DELETE: db_printf("BIO_DELETE"); break;
1398	case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1399	case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1400	case BIO_CMD0: db_printf("BIO_CMD0"); break;
1401	case BIO_CMD1: db_printf("BIO_CMD1"); break;
1402	case BIO_CMD2: db_printf("BIO_CMD2"); break;
1403	default: db_printf("UNKNOWN"); break;
1404	}
1405	db_printf("\n");
1406}
1407
1408static void
1409db_print_bio_flags(struct bio *bp)
1410{
1411	int comma;
1412
1413	comma = 0;
1414	db_printf("  flags: ");
1415	if (bp->bio_flags & BIO_ERROR) {
1416		db_printf("BIO_ERROR");
1417		comma = 1;
1418	}
1419	if (bp->bio_flags & BIO_DONE) {
1420		db_printf("%sBIO_DONE", (comma ? ", " : ""));
1421		comma = 1;
1422	}
1423	if (bp->bio_flags & BIO_ONQUEUE)
1424		db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1425	db_printf("\n");
1426}
1427
1428/*
1429 * Print useful information in a BIO
1430 */
1431DB_SHOW_COMMAND(bio, db_show_bio)
1432{
1433	struct bio *bp;
1434
1435	if (have_addr) {
1436		bp = (struct bio *)addr;
1437		db_printf("BIO %p\n", bp);
1438		db_print_bio_cmd(bp);
1439		db_print_bio_flags(bp);
1440		db_printf("  cflags: 0x%hhx\n", bp->bio_cflags);
1441		db_printf("  pflags: 0x%hhx\n", bp->bio_pflags);
1442		db_printf("  offset: %jd\n", (intmax_t)bp->bio_offset);
1443		db_printf("  length: %jd\n", (intmax_t)bp->bio_length);
1444		db_printf("  bcount: %ld\n", bp->bio_bcount);
1445		db_printf("  resid: %ld\n", bp->bio_resid);
1446		db_printf("  completed: %jd\n", (intmax_t)bp->bio_completed);
1447		db_printf("  children: %u\n", bp->bio_children);
1448		db_printf("  inbed: %u\n", bp->bio_inbed);
1449		db_printf("  error: %d\n", bp->bio_error);
1450		db_printf("  parent: %p\n", bp->bio_parent);
1451		db_printf("  driver1: %p\n", bp->bio_driver1);
1452		db_printf("  driver2: %p\n", bp->bio_driver2);
1453		db_printf("  caller1: %p\n", bp->bio_caller1);
1454		db_printf("  caller2: %p\n", bp->bio_caller2);
1455		db_printf("  bio_from: %p\n", bp->bio_from);
1456		db_printf("  bio_to: %p\n", bp->bio_to);
1457	}
1458}
1459
1460#undef	gprintf
1461#undef	gprintln
1462#undef	ADDFLAG
1463
1464#endif	/* DDB */
1465