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