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