]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_subr.c
Fix a lock leak in g_mirror_destroy().
[FreeBSD/FreeBSD.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         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  */
81 static void
82 g_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
139 static int
140 g_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);
149 retry:
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
208 int
209 g_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                 error = g_unload_class(mp);
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 *mp, *mp2;
264         struct g_geom *gp;
265         struct g_hh00 *hh;
266         struct g_provider *pp;
267         struct g_consumer *cp;
268
269         g_topology_assert();
270         if (flag == EV_CANCEL)  /* XXX: can't happen ? */
271                 return;
272         if (g_shutdown || g_notaste)
273                 return;
274
275         hh = arg;
276         mp = hh->mp;
277         hh->error = 0;
278         if (hh->post) {
279                 g_free(hh);
280                 hh = NULL;
281         }
282         g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name);
283
284         LIST_FOREACH(mp2, &g_classes, class) {
285                 LIST_FOREACH(gp, &mp2->geom, geom) {
286                         LIST_FOREACH(pp, &gp->provider, provider) {
287                                 if (pp->acr || pp->acw || pp->ace)
288                                         continue;
289                                 LIST_FOREACH(cp, &pp->consumers, consumers) {
290                                         if (cp->geom->class == mp &&
291                                             (cp->flags & G_CF_ORPHAN) == 0)
292                                                 break;
293                                 }
294                                 if (cp != NULL) {
295                                         cp->flags |= G_CF_ORPHAN;
296                                         g_wither_geom(cp->geom, ENXIO);
297                                 }
298                                 mp->taste(mp, pp, 0);
299                                 g_topology_assert();
300                         }
301                 }
302         }
303 }
304
305 int
306 g_retaste(struct g_class *mp)
307 {
308         struct g_hh00 *hh;
309         int error;
310
311         if (mp->taste == NULL)
312                 return (EINVAL);
313
314         hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
315         hh->mp = mp;
316
317         if (cold) {
318                 hh->post = 1;
319                 error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL);
320         } else {
321                 error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL);
322                 if (error == 0)
323                         error = hh->error;
324                 g_free(hh);
325         }
326
327         return (error);
328 }
329
330 struct g_geom *
331 g_new_geomf(struct g_class *mp, const char *fmt, ...)
332 {
333         struct g_geom *gp;
334         va_list ap;
335         struct sbuf *sb;
336
337         g_topology_assert();
338         G_VALID_CLASS(mp);
339         sb = sbuf_new_auto();
340         va_start(ap, fmt);
341         sbuf_vprintf(sb, fmt, ap);
342         va_end(ap);
343         sbuf_finish(sb);
344         gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
345         gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
346         gp->class = mp;
347         gp->rank = 1;
348         LIST_INIT(&gp->consumer);
349         LIST_INIT(&gp->provider);
350         LIST_INIT(&gp->aliases);
351         LIST_INSERT_HEAD(&mp->geom, gp, geom);
352         TAILQ_INSERT_HEAD(&geoms, gp, geoms);
353         strcpy(gp->name, sbuf_data(sb));
354         sbuf_delete(sb);
355         /* Fill in defaults from class */
356         gp->start = mp->start;
357         gp->spoiled = mp->spoiled;
358         gp->attrchanged = mp->attrchanged;
359         gp->providergone = mp->providergone;
360         gp->dumpconf = mp->dumpconf;
361         gp->access = mp->access;
362         gp->orphan = mp->orphan;
363         gp->ioctl = mp->ioctl;
364         gp->resize = mp->resize;
365         return (gp);
366 }
367
368 void
369 g_destroy_geom(struct g_geom *gp)
370 {
371         struct g_geom_alias *gap, *gaptmp;
372
373         g_topology_assert();
374         G_VALID_GEOM(gp);
375         g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
376         KASSERT(LIST_EMPTY(&gp->consumer),
377             ("g_destroy_geom(%s) with consumer(s) [%p]",
378             gp->name, LIST_FIRST(&gp->consumer)));
379         KASSERT(LIST_EMPTY(&gp->provider),
380             ("g_destroy_geom(%s) with provider(s) [%p]",
381             gp->name, LIST_FIRST(&gp->provider)));
382         g_cancel_event(gp);
383         LIST_REMOVE(gp, geom);
384         TAILQ_REMOVE(&geoms, gp, geoms);
385         LIST_FOREACH_SAFE(gap, &gp->aliases, ga_next, gaptmp)
386                 g_free(gap);
387         g_free(gp->name);
388         g_free(gp);
389 }
390
391 /*
392  * This function is called (repeatedly) until the geom has withered away.
393  */
394 void
395 g_wither_geom(struct g_geom *gp, int error)
396 {
397         struct g_provider *pp;
398
399         g_topology_assert();
400         G_VALID_GEOM(gp);
401         g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
402         if (!(gp->flags & G_GEOM_WITHER)) {
403                 gp->flags |= G_GEOM_WITHER;
404                 LIST_FOREACH(pp, &gp->provider, provider)
405                         if (!(pp->flags & G_PF_ORPHAN))
406                                 g_orphan_provider(pp, error);
407         }
408         g_do_wither();
409 }
410
411 /*
412  * Convenience function to destroy a particular provider.
413  */
414 void
415 g_wither_provider(struct g_provider *pp, int error)
416 {
417
418         pp->flags |= G_PF_WITHER;
419         if (!(pp->flags & G_PF_ORPHAN))
420                 g_orphan_provider(pp, error);
421 }
422
423 /*
424  * This function is called (repeatedly) until the has withered away.
425  */
426 void
427 g_wither_geom_close(struct g_geom *gp, int error)
428 {
429         struct g_consumer *cp;
430
431         g_topology_assert();
432         G_VALID_GEOM(gp);
433         g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
434         LIST_FOREACH(cp, &gp->consumer, consumer)
435                 if (cp->acr || cp->acw || cp->ace)
436                         g_access(cp, -cp->acr, -cp->acw, -cp->ace);
437         g_wither_geom(gp, error);
438 }
439
440 /*
441  * This function is called (repeatedly) until we cant wash away more
442  * withered bits at present.
443  */
444 void
445 g_wither_washer()
446 {
447         struct g_class *mp;
448         struct g_geom *gp, *gp2;
449         struct g_provider *pp, *pp2;
450         struct g_consumer *cp, *cp2;
451
452         g_topology_assert();
453         LIST_FOREACH(mp, &g_classes, class) {
454                 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
455                         LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
456                                 if (!(pp->flags & G_PF_WITHER))
457                                         continue;
458                                 if (LIST_EMPTY(&pp->consumers))
459                                         g_destroy_provider(pp);
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                         }
467                         LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
468                                 if (cp->acr || cp->acw || cp->ace)
469                                         continue;
470                                 if (cp->provider != NULL)
471                                         g_detach(cp);
472                                 g_destroy_consumer(cp);
473                         }
474                         if (LIST_EMPTY(&gp->provider) &&
475                             LIST_EMPTY(&gp->consumer))
476                                 g_destroy_geom(gp);
477                 }
478         }
479 }
480
481 struct g_consumer *
482 g_new_consumer(struct g_geom *gp)
483 {
484         struct g_consumer *cp;
485
486         g_topology_assert();
487         G_VALID_GEOM(gp);
488         KASSERT(!(gp->flags & G_GEOM_WITHER),
489             ("g_new_consumer on WITHERing geom(%s) (class %s)",
490             gp->name, gp->class->name));
491         KASSERT(gp->orphan != NULL,
492             ("g_new_consumer on geom(%s) (class %s) without orphan",
493             gp->name, gp->class->name));
494
495         cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
496         cp->geom = gp;
497         cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
498             DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
499         LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
500         return(cp);
501 }
502
503 void
504 g_destroy_consumer(struct g_consumer *cp)
505 {
506         struct g_geom *gp;
507
508         g_topology_assert();
509         G_VALID_CONSUMER(cp);
510         g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
511         KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
512         KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
513         KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
514         KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
515         g_cancel_event(cp);
516         gp = cp->geom;
517         LIST_REMOVE(cp, consumer);
518         devstat_remove_entry(cp->stat);
519         g_free(cp);
520         if (gp->flags & G_GEOM_WITHER)
521                 g_do_wither();
522 }
523
524 static void
525 g_new_provider_event(void *arg, int flag)
526 {
527         struct g_class *mp;
528         struct g_provider *pp;
529         struct g_consumer *cp, *next_cp;
530
531         g_topology_assert();
532         if (flag == EV_CANCEL)
533                 return;
534         if (g_shutdown)
535                 return;
536         pp = arg;
537         G_VALID_PROVIDER(pp);
538         KASSERT(!(pp->flags & G_PF_WITHER),
539             ("g_new_provider_event but withered"));
540         LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
541                 if ((cp->flags & G_CF_ORPHAN) == 0 &&
542                     cp->geom->attrchanged != NULL)
543                         cp->geom->attrchanged(cp, "GEOM::media");
544         }
545         if (g_notaste)
546                 return;
547         LIST_FOREACH(mp, &g_classes, class) {
548                 if (mp->taste == NULL)
549                         continue;
550                 LIST_FOREACH(cp, &pp->consumers, consumers)
551                         if (cp->geom->class == mp &&
552                             (cp->flags & G_CF_ORPHAN) == 0)
553                                 break;
554                 if (cp != NULL)
555                         continue;
556                 mp->taste(mp, pp, 0);
557                 g_topology_assert();
558         }
559 }
560
561
562 struct g_provider *
563 g_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
599 void
600 g_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
607 static void
608 g_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         KASSERT(!(pp->flags & G_PF_WITHER),
628             ("g_resize_provider_event but withered"));
629         g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
630
631         LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
632                 gp = cp->geom;
633                 if (gp->resize == NULL && size < pp->mediasize) {
634                         /*
635                          * XXX: g_dev_orphan method does deferred destroying
636                          * and it is possible, that other event could already
637                          * call the orphan method. Check consumer's flags to
638                          * do not schedule it twice.
639                          */
640                         if (cp->flags & G_CF_ORPHAN)
641                                 continue;
642                         cp->flags |= G_CF_ORPHAN;
643                         cp->geom->orphan(cp);
644                 }
645         }
646
647         pp->mediasize = size;
648         
649         LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
650                 gp = cp->geom;
651                 if ((gp->flags & G_GEOM_WITHER) == 0 && gp->resize != NULL)
652                         gp->resize(cp);
653         }
654
655         /*
656          * After resizing, the previously invalid GEOM class metadata
657          * might become valid.  This means we should retaste.
658          */
659         LIST_FOREACH(mp, &g_classes, class) {
660                 if (mp->taste == NULL)
661                         continue;
662                 LIST_FOREACH(cp, &pp->consumers, consumers)
663                         if (cp->geom->class == mp &&
664                             (cp->flags & G_CF_ORPHAN) == 0)
665                                 break;
666                 if (cp != NULL)
667                         continue;
668                 mp->taste(mp, pp, 0);
669                 g_topology_assert();
670         }
671 }
672
673 void
674 g_resize_provider(struct g_provider *pp, off_t size)
675 {
676         struct g_hh00 *hh;
677
678         G_VALID_PROVIDER(pp);
679         if (pp->flags & G_PF_WITHER)
680                 return;
681
682         if (size == pp->mediasize)
683                 return;
684
685         hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
686         hh->pp = pp;
687         hh->size = size;
688         g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
689 }
690
691 #ifndef _PATH_DEV
692 #define _PATH_DEV       "/dev/"
693 #endif
694
695 struct g_provider *
696 g_provider_by_name(char const *arg)
697 {
698         struct g_class *cp;
699         struct g_geom *gp;
700         struct g_provider *pp, *wpp;
701
702         if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
703                 arg += sizeof(_PATH_DEV) - 1;
704
705         wpp = NULL;
706         LIST_FOREACH(cp, &g_classes, class) {
707                 LIST_FOREACH(gp, &cp->geom, geom) {
708                         LIST_FOREACH(pp, &gp->provider, provider) {
709                                 if (strcmp(arg, pp->name) != 0)
710                                         continue;
711                                 if ((gp->flags & G_GEOM_WITHER) == 0 &&
712                                     (pp->flags & G_PF_WITHER) == 0)
713                                         return (pp);
714                                 else
715                                         wpp = pp;
716                         }
717                 }
718         }
719
720         return (wpp);
721 }
722
723 void
724 g_destroy_provider(struct g_provider *pp)
725 {
726         struct g_geom *gp;
727
728         g_topology_assert();
729         G_VALID_PROVIDER(pp);
730         KASSERT(LIST_EMPTY(&pp->consumers),
731             ("g_destroy_provider but attached"));
732         KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
733         KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
734         KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
735         g_cancel_event(pp);
736         LIST_REMOVE(pp, provider);
737         gp = pp->geom;
738         devstat_remove_entry(pp->stat);
739         /*
740          * If a callback was provided, send notification that the provider
741          * is now gone.
742          */
743         if (gp->providergone != NULL)
744                 gp->providergone(pp);
745
746         g_free(pp);
747         if ((gp->flags & G_GEOM_WITHER))
748                 g_do_wither();
749 }
750
751 /*
752  * We keep the "geoms" list sorted by topological order (== increasing
753  * numerical rank) at all times.
754  * When an attach is done, the attaching geoms rank is invalidated
755  * and it is moved to the tail of the list.
756  * All geoms later in the sequence has their ranks reevaluated in
757  * sequence.  If we cannot assign rank to a geom because it's
758  * prerequisites do not have rank, we move that element to the tail
759  * of the sequence with invalid rank as well.
760  * At some point we encounter our original geom and if we stil fail
761  * to assign it a rank, there must be a loop and we fail back to
762  * g_attach() which detach again and calls redo_rank again
763  * to fix up the damage.
764  * It would be much simpler code wise to do it recursively, but we
765  * can't risk that on the kernel stack.
766  */
767
768 static int
769 redo_rank(struct g_geom *gp)
770 {
771         struct g_consumer *cp;
772         struct g_geom *gp1, *gp2;
773         int n, m;
774
775         g_topology_assert();
776         G_VALID_GEOM(gp);
777
778         /* Invalidate this geoms rank and move it to the tail */
779         gp1 = TAILQ_NEXT(gp, geoms);
780         if (gp1 != NULL) {
781                 gp->rank = 0;
782                 TAILQ_REMOVE(&geoms, gp, geoms);
783                 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
784         } else {
785                 gp1 = gp;
786         }
787
788         /* re-rank the rest of the sequence */
789         for (; gp1 != NULL; gp1 = gp2) {
790                 gp1->rank = 0;
791                 m = 1;
792                 LIST_FOREACH(cp, &gp1->consumer, consumer) {
793                         if (cp->provider == NULL)
794                                 continue;
795                         n = cp->provider->geom->rank;
796                         if (n == 0) {
797                                 m = 0;
798                                 break;
799                         } else if (n >= m)
800                                 m = n + 1;
801                 }
802                 gp1->rank = m;
803                 gp2 = TAILQ_NEXT(gp1, geoms);
804
805                 /* got a rank, moving on */
806                 if (m != 0)
807                         continue;
808
809                 /* no rank to original geom means loop */
810                 if (gp == gp1) 
811                         return (ELOOP);
812
813                 /* no rank, put it at the end move on */
814                 TAILQ_REMOVE(&geoms, gp1, geoms);
815                 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
816         }
817         return (0);
818 }
819
820 int
821 g_attach(struct g_consumer *cp, struct g_provider *pp)
822 {
823         int error;
824
825         g_topology_assert();
826         G_VALID_CONSUMER(cp);
827         G_VALID_PROVIDER(pp);
828         g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
829         KASSERT(cp->provider == NULL, ("attach but attached"));
830         cp->provider = pp;
831         cp->flags &= ~G_CF_ORPHAN;
832         LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
833         error = redo_rank(cp->geom);
834         if (error) {
835                 LIST_REMOVE(cp, consumers);
836                 cp->provider = NULL;
837                 redo_rank(cp->geom);
838         }
839         return (error);
840 }
841
842 void
843 g_detach(struct g_consumer *cp)
844 {
845         struct g_provider *pp;
846
847         g_topology_assert();
848         G_VALID_CONSUMER(cp);
849         g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
850         KASSERT(cp->provider != NULL, ("detach but not attached"));
851         KASSERT(cp->acr == 0, ("detach but nonzero acr"));
852         KASSERT(cp->acw == 0, ("detach but nonzero acw"));
853         KASSERT(cp->ace == 0, ("detach but nonzero ace"));
854         KASSERT(cp->nstart == cp->nend,
855             ("detach with active requests"));
856         pp = cp->provider;
857         LIST_REMOVE(cp, consumers);
858         cp->provider = NULL;
859         if ((cp->geom->flags & G_GEOM_WITHER) ||
860             (pp->geom->flags & G_GEOM_WITHER) ||
861             (pp->flags & G_PF_WITHER))
862                 g_do_wither();
863         redo_rank(cp->geom);
864 }
865
866 /*
867  * g_access()
868  *
869  * Access-check with delta values.  The question asked is "can provider
870  * "cp" change the access counters by the relative amounts dc[rwe] ?"
871  */
872
873 int
874 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
875 {
876         struct g_provider *pp;
877         int pr,pw,pe;
878         int error;
879
880         g_topology_assert();
881         G_VALID_CONSUMER(cp);
882         pp = cp->provider;
883         KASSERT(pp != NULL, ("access but not attached"));
884         G_VALID_PROVIDER(pp);
885
886         g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
887             cp, pp->name, dcr, dcw, dce);
888
889         KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
890         KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
891         KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
892         KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
893         KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
894
895         /*
896          * If our class cares about being spoiled, and we have been, we
897          * are probably just ahead of the event telling us that.  Fail
898          * now rather than having to unravel this later.
899          */
900         if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
901             (dcr > 0 || dcw > 0 || dce > 0))
902                 return (ENXIO);
903
904         /*
905          * Figure out what counts the provider would have had, if this
906          * consumer had (r0w0e0) at this time.
907          */
908         pr = pp->acr - cp->acr;
909         pw = pp->acw - cp->acw;
910         pe = pp->ace - cp->ace;
911
912         g_trace(G_T_ACCESS,
913     "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
914             dcr, dcw, dce,
915             cp->acr, cp->acw, cp->ace,
916             pp->acr, pp->acw, pp->ace,
917             pp, pp->name);
918
919         /* If foot-shooting is enabled, any open on rank#1 is OK */
920         if ((g_debugflags & 16) && pp->geom->rank == 1)
921                 ;
922         /* If we try exclusive but already write: fail */
923         else if (dce > 0 && pw > 0)
924                 return (EPERM);
925         /* If we try write but already exclusive: fail */
926         else if (dcw > 0 && pe > 0)
927                 return (EPERM);
928         /* If we try to open more but provider is error'ed: fail */
929         else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0) {
930                 printf("%s(%d): provider %s has error %d set\n",
931                     __func__, __LINE__, pp->name, pp->error);
932                 return (pp->error);
933         }
934
935         /* Ok then... */
936
937         error = pp->geom->access(pp, dcr, dcw, dce);
938         KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
939             ("Geom provider %s::%s dcr=%d dcw=%d dce=%d error=%d failed "
940             "closing ->access()", pp->geom->class->name, pp->name, dcr, dcw,
941             dce, error));
942         if (!error) {
943                 /*
944                  * If we open first write, spoil any partner consumers.
945                  * If we close last write and provider is not errored,
946                  * trigger re-taste.
947                  */
948                 if (pp->acw == 0 && dcw != 0)
949                         g_spoil(pp, cp);
950                 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
951                     !(pp->geom->flags & G_GEOM_WITHER))
952                         g_post_event(g_new_provider_event, pp, M_WAITOK, 
953                             pp, NULL);
954
955                 pp->acr += dcr;
956                 pp->acw += dcw;
957                 pp->ace += dce;
958                 cp->acr += dcr;
959                 cp->acw += dcw;
960                 cp->ace += dce;
961                 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
962                         KASSERT(pp->sectorsize > 0,
963                             ("Provider %s lacks sectorsize", pp->name));
964                 if ((cp->geom->flags & G_GEOM_WITHER) &&
965                     cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
966                         g_do_wither();
967         }
968         return (error);
969 }
970
971 int
972 g_handleattr_int(struct bio *bp, const char *attribute, int val)
973 {
974
975         return (g_handleattr(bp, attribute, &val, sizeof val));
976 }
977
978 int
979 g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val)
980 {
981
982         return (g_handleattr(bp, attribute, &val, sizeof val));
983 }
984
985 int
986 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
987 {
988
989         return (g_handleattr(bp, attribute, &val, sizeof val));
990 }
991
992 int
993 g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
994 {
995
996         return (g_handleattr(bp, attribute, str, 0));
997 }
998
999 int
1000 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
1001 {
1002         int error = 0;
1003
1004         if (strcmp(bp->bio_attribute, attribute))
1005                 return (0);
1006         if (len == 0) {
1007                 bzero(bp->bio_data, bp->bio_length);
1008                 if (strlcpy(bp->bio_data, val, bp->bio_length) >=
1009                     bp->bio_length) {
1010                         printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
1011                             __func__, bp->bio_to->name,
1012                             (intmax_t)bp->bio_length, strlen(val));
1013                         error = EFAULT;
1014                 }
1015         } else if (bp->bio_length == len) {
1016                 bcopy(val, bp->bio_data, len);
1017         } else {
1018                 printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
1019                     bp->bio_to->name, (intmax_t)bp->bio_length, len);
1020                 error = EFAULT;
1021         }
1022         if (error == 0)
1023                 bp->bio_completed = bp->bio_length;
1024         g_io_deliver(bp, error);
1025         return (1);
1026 }
1027
1028 int
1029 g_std_access(struct g_provider *pp,
1030         int dr __unused, int dw __unused, int de __unused)
1031 {
1032
1033         g_topology_assert();
1034         G_VALID_PROVIDER(pp);
1035         return (0);
1036 }
1037
1038 void
1039 g_std_done(struct bio *bp)
1040 {
1041         struct bio *bp2;
1042
1043         bp2 = bp->bio_parent;
1044         if (bp2->bio_error == 0)
1045                 bp2->bio_error = bp->bio_error;
1046         bp2->bio_completed += bp->bio_completed;
1047         g_destroy_bio(bp);
1048         bp2->bio_inbed++;
1049         if (bp2->bio_children == bp2->bio_inbed)
1050                 g_io_deliver(bp2, bp2->bio_error);
1051 }
1052
1053 /* XXX: maybe this is only g_slice_spoiled */
1054
1055 void
1056 g_std_spoiled(struct g_consumer *cp)
1057 {
1058         struct g_geom *gp;
1059         struct g_provider *pp;
1060
1061         g_topology_assert();
1062         G_VALID_CONSUMER(cp);
1063         g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
1064         cp->flags |= G_CF_ORPHAN;
1065         g_detach(cp);
1066         gp = cp->geom;
1067         LIST_FOREACH(pp, &gp->provider, provider)
1068                 g_orphan_provider(pp, ENXIO);
1069         g_destroy_consumer(cp);
1070         if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
1071                 g_destroy_geom(gp);
1072         else
1073                 gp->flags |= G_GEOM_WITHER;
1074 }
1075
1076 /*
1077  * Spoiling happens when a provider is opened for writing, but consumers
1078  * which are configured by in-band data are attached (slicers for instance).
1079  * Since the write might potentially change the in-band data, such consumers
1080  * need to re-evaluate their existence after the writing session closes.
1081  * We do this by (offering to) tear them down when the open for write happens
1082  * in return for a re-taste when it closes again.
1083  * Together with the fact that such consumers grab an 'e' bit whenever they
1084  * are open, regardless of mode, this ends up DTRT.
1085  */
1086
1087 static void
1088 g_spoil_event(void *arg, int flag)
1089 {
1090         struct g_provider *pp;
1091         struct g_consumer *cp, *cp2;
1092
1093         g_topology_assert();
1094         if (flag == EV_CANCEL)
1095                 return;
1096         pp = arg;
1097         G_VALID_PROVIDER(pp);
1098         g_trace(G_T_TOPOLOGY, "%s %p(%s:%s:%s)", __func__, pp,
1099             pp->geom->class->name, pp->geom->name, pp->name);
1100         for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
1101                 cp2 = LIST_NEXT(cp, consumers);
1102                 if ((cp->flags & G_CF_SPOILED) == 0)
1103                         continue;
1104                 cp->flags &= ~G_CF_SPOILED;
1105                 if (cp->geom->spoiled == NULL)
1106                         continue;
1107                 cp->geom->spoiled(cp);
1108                 g_topology_assert();
1109         }
1110 }
1111
1112 void
1113 g_spoil(struct g_provider *pp, struct g_consumer *cp)
1114 {
1115         struct g_consumer *cp2;
1116
1117         g_topology_assert();
1118         G_VALID_PROVIDER(pp);
1119         G_VALID_CONSUMER(cp);
1120
1121         LIST_FOREACH(cp2, &pp->consumers, consumers) {
1122                 if (cp2 == cp)
1123                         continue;
1124 /*
1125                 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1126                 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1127 */
1128                 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1129                 cp2->flags |= G_CF_SPOILED;
1130         }
1131         g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1132 }
1133
1134 static void
1135 g_media_changed_event(void *arg, int flag)
1136 {
1137         struct g_provider *pp;
1138         int retaste;
1139
1140         g_topology_assert();
1141         if (flag == EV_CANCEL)
1142                 return;
1143         pp = arg;
1144         G_VALID_PROVIDER(pp);
1145
1146         /*
1147          * If provider was not open for writing, queue retaste after spoiling.
1148          * If it was, retaste will happen automatically on close.
1149          */
1150         retaste = (pp->acw == 0 && pp->error == 0 &&
1151             !(pp->geom->flags & G_GEOM_WITHER));
1152         g_spoil_event(arg, flag);
1153         if (retaste)
1154                 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
1155 }
1156
1157 int
1158 g_media_changed(struct g_provider *pp, int flag)
1159 {
1160         struct g_consumer *cp;
1161
1162         LIST_FOREACH(cp, &pp->consumers, consumers)
1163                 cp->flags |= G_CF_SPOILED;
1164         return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
1165 }
1166
1167 int
1168 g_media_gone(struct g_provider *pp, int flag)
1169 {
1170         struct g_consumer *cp;
1171
1172         LIST_FOREACH(cp, &pp->consumers, consumers)
1173                 cp->flags |= G_CF_SPOILED;
1174         return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
1175 }
1176
1177 int
1178 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1179 {
1180         int error, i;
1181
1182         i = len;
1183         error = g_io_getattr(attr, cp, &i, var);
1184         if (error)
1185                 return (error);
1186         if (i != len)
1187                 return (EINVAL);
1188         return (0);
1189 }
1190
1191 static int
1192 g_get_device_prefix_len(const char *name)
1193 {
1194         int len;
1195
1196         if (strncmp(name, "ada", 3) == 0)
1197                 len = 3;
1198         else if (strncmp(name, "ad", 2) == 0)
1199                 len = 2;
1200         else
1201                 return (0);
1202         if (name[len] < '0' || name[len] > '9')
1203                 return (0);
1204         do {
1205                 len++;
1206         } while (name[len] >= '0' && name[len] <= '9');
1207         return (len);
1208 }
1209
1210 int
1211 g_compare_names(const char *namea, const char *nameb)
1212 {
1213         int deva, devb;
1214
1215         if (strcmp(namea, nameb) == 0)
1216                 return (1);
1217         deva = g_get_device_prefix_len(namea);
1218         if (deva == 0)
1219                 return (0);
1220         devb = g_get_device_prefix_len(nameb);
1221         if (devb == 0)
1222                 return (0);
1223         if (strcmp(namea + deva, nameb + devb) == 0)
1224                 return (1);
1225         return (0);
1226 }
1227
1228 void
1229 g_geom_add_alias(struct g_geom *gp, const char *alias)
1230 {
1231         struct g_geom_alias *gap;
1232
1233         gap = (struct g_geom_alias *)g_malloc(
1234                 sizeof(struct g_geom_alias) + strlen(alias) + 1, M_WAITOK);
1235         strcpy((char *)(gap + 1), alias);
1236         gap->ga_alias = (const char *)(gap + 1);
1237         LIST_INSERT_HEAD(&gp->aliases, gap, ga_next);
1238 }
1239
1240 #if defined(DIAGNOSTIC) || defined(DDB)
1241 /*
1242  * This function walks the mesh and returns a non-zero integer if it
1243  * finds the argument pointer is an object. The return value indicates
1244  * which type of object it is believed to be. If topology is not locked,
1245  * this function is potentially dangerous, but we don't assert that the
1246  * topology lock is held when called from debugger.
1247  */
1248 int
1249 g_valid_obj(void const *ptr)
1250 {
1251         struct g_class *mp;
1252         struct g_geom *gp;
1253         struct g_consumer *cp;
1254         struct g_provider *pp;
1255
1256 #ifdef KDB
1257         if (kdb_active == 0)
1258 #endif
1259                 g_topology_assert();
1260
1261         LIST_FOREACH(mp, &g_classes, class) {
1262                 if (ptr == mp)
1263                         return (1);
1264                 LIST_FOREACH(gp, &mp->geom, geom) {
1265                         if (ptr == gp)
1266                                 return (2);
1267                         LIST_FOREACH(cp, &gp->consumer, consumer)
1268                                 if (ptr == cp)
1269                                         return (3);
1270                         LIST_FOREACH(pp, &gp->provider, provider)
1271                                 if (ptr == pp)
1272                                         return (4);
1273                 }
1274         }
1275         return(0);
1276 }
1277 #endif
1278
1279 #ifdef DDB
1280
1281 #define gprintf(...)    do {                                            \
1282         db_printf("%*s", indent, "");                                   \
1283         db_printf(__VA_ARGS__);                                         \
1284 } while (0)
1285 #define gprintln(...)   do {                                            \
1286         gprintf(__VA_ARGS__);                                           \
1287         db_printf("\n");                                                \
1288 } while (0)
1289
1290 #define ADDFLAG(obj, flag, sflag)       do {                            \
1291         if ((obj)->flags & (flag)) {                                    \
1292                 if (comma)                                              \
1293                         strlcat(str, ",", size);                        \
1294                 strlcat(str, (sflag), size);                            \
1295                 comma = 1;                                              \
1296         }                                                               \
1297 } while (0)
1298
1299 static char *
1300 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1301 {
1302         int comma = 0;
1303
1304         bzero(str, size);
1305         if (pp->flags == 0) {
1306                 strlcpy(str, "NONE", size);
1307                 return (str);
1308         }
1309         ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1310         ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1311         return (str);
1312 }
1313
1314 static char *
1315 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1316 {
1317         int comma = 0;
1318
1319         bzero(str, size);
1320         if (gp->flags == 0) {
1321                 strlcpy(str, "NONE", size);
1322                 return (str);
1323         }
1324         ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1325         return (str);
1326 }
1327 static void
1328 db_show_geom_consumer(int indent, struct g_consumer *cp)
1329 {
1330
1331         if (indent == 0) {
1332                 gprintln("consumer: %p", cp);
1333                 gprintln("  class:    %s (%p)", cp->geom->class->name,
1334                     cp->geom->class);
1335                 gprintln("  geom:     %s (%p)", cp->geom->name, cp->geom);
1336                 if (cp->provider == NULL)
1337                         gprintln("  provider: none");
1338                 else {
1339                         gprintln("  provider: %s (%p)", cp->provider->name,
1340                             cp->provider);
1341                 }
1342                 gprintln("  access:   r%dw%de%d", cp->acr, cp->acw, cp->ace);
1343                 gprintln("  flags:    0x%04x", cp->flags);
1344                 gprintln("  nstart:   %u", cp->nstart);
1345                 gprintln("  nend:     %u", cp->nend);
1346         } else {
1347                 gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1348                     cp->provider != NULL ? cp->provider->name : "none",
1349                     cp->acr, cp->acw, cp->ace);
1350                 if (cp->flags)
1351                         db_printf(", flags=0x%04x", cp->flags);
1352                 db_printf("\n");
1353         }
1354 }
1355
1356 static void
1357 db_show_geom_provider(int indent, struct g_provider *pp)
1358 {
1359         struct g_consumer *cp;
1360         char flags[64];
1361
1362         if (indent == 0) {
1363                 gprintln("provider: %s (%p)", pp->name, pp);
1364                 gprintln("  class:        %s (%p)", pp->geom->class->name,
1365                     pp->geom->class);
1366                 gprintln("  geom:         %s (%p)", pp->geom->name, pp->geom);
1367                 gprintln("  mediasize:    %jd", (intmax_t)pp->mediasize);
1368                 gprintln("  sectorsize:   %u", pp->sectorsize);
1369                 gprintln("  stripesize:   %u", pp->stripesize);
1370                 gprintln("  stripeoffset: %u", pp->stripeoffset);
1371                 gprintln("  access:       r%dw%de%d", pp->acr, pp->acw,
1372                     pp->ace);
1373                 gprintln("  flags:        %s (0x%04x)",
1374                     provider_flags_to_string(pp, flags, sizeof(flags)),
1375                     pp->flags);
1376                 gprintln("  error:        %d", pp->error);
1377                 gprintln("  nstart:       %u", pp->nstart);
1378                 gprintln("  nend:         %u", pp->nend);
1379                 if (LIST_EMPTY(&pp->consumers))
1380                         gprintln("  consumers:    none");
1381         } else {
1382                 gprintf("provider: %s (%p), access=r%dw%de%d",
1383                     pp->name, pp, pp->acr, pp->acw, pp->ace);
1384                 if (pp->flags != 0) {
1385                         db_printf(", flags=%s (0x%04x)",
1386                             provider_flags_to_string(pp, flags, sizeof(flags)),
1387                             pp->flags);
1388                 }
1389                 db_printf("\n");
1390         }
1391         if (!LIST_EMPTY(&pp->consumers)) {
1392                 LIST_FOREACH(cp, &pp->consumers, consumers) {
1393                         db_show_geom_consumer(indent + 2, cp);
1394                         if (db_pager_quit)
1395                                 break;
1396                 }
1397         }
1398 }
1399
1400 static void
1401 db_show_geom_geom(int indent, struct g_geom *gp)
1402 {
1403         struct g_provider *pp;
1404         struct g_consumer *cp;
1405         char flags[64];
1406
1407         if (indent == 0) {
1408                 gprintln("geom: %s (%p)", gp->name, gp);
1409                 gprintln("  class:     %s (%p)", gp->class->name, gp->class);
1410                 gprintln("  flags:     %s (0x%04x)",
1411                     geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1412                 gprintln("  rank:      %d", gp->rank);
1413                 if (LIST_EMPTY(&gp->provider))
1414                         gprintln("  providers: none");
1415                 if (LIST_EMPTY(&gp->consumer))
1416                         gprintln("  consumers: none");
1417         } else {
1418                 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1419                 if (gp->flags != 0) {
1420                         db_printf(", flags=%s (0x%04x)",
1421                             geom_flags_to_string(gp, flags, sizeof(flags)),
1422                             gp->flags);
1423                 }
1424                 db_printf("\n");
1425         }
1426         if (!LIST_EMPTY(&gp->provider)) {
1427                 LIST_FOREACH(pp, &gp->provider, provider) {
1428                         db_show_geom_provider(indent + 2, pp);
1429                         if (db_pager_quit)
1430                                 break;
1431                 }
1432         }
1433         if (!LIST_EMPTY(&gp->consumer)) {
1434                 LIST_FOREACH(cp, &gp->consumer, consumer) {
1435                         db_show_geom_consumer(indent + 2, cp);
1436                         if (db_pager_quit)
1437                                 break;
1438                 }
1439         }
1440 }
1441
1442 static void
1443 db_show_geom_class(struct g_class *mp)
1444 {
1445         struct g_geom *gp;
1446
1447         db_printf("class: %s (%p)\n", mp->name, mp);
1448         LIST_FOREACH(gp, &mp->geom, geom) {
1449                 db_show_geom_geom(2, gp);
1450                 if (db_pager_quit)
1451                         break;
1452         }
1453 }
1454
1455 /*
1456  * Print the GEOM topology or the given object.
1457  */
1458 DB_SHOW_COMMAND(geom, db_show_geom)
1459 {
1460         struct g_class *mp;
1461
1462         if (!have_addr) {
1463                 /* No address given, print the entire topology. */
1464                 LIST_FOREACH(mp, &g_classes, class) {
1465                         db_show_geom_class(mp);
1466                         db_printf("\n");
1467                         if (db_pager_quit)
1468                                 break;
1469                 }
1470         } else {
1471                 switch (g_valid_obj((void *)addr)) {
1472                 case 1:
1473                         db_show_geom_class((struct g_class *)addr);
1474                         break;
1475                 case 2:
1476                         db_show_geom_geom(0, (struct g_geom *)addr);
1477                         break;
1478                 case 3:
1479                         db_show_geom_consumer(0, (struct g_consumer *)addr);
1480                         break;
1481                 case 4:
1482                         db_show_geom_provider(0, (struct g_provider *)addr);
1483                         break;
1484                 default:
1485                         db_printf("Not a GEOM object.\n");
1486                         break;
1487                 }
1488         }
1489 }
1490
1491 static void
1492 db_print_bio_cmd(struct bio *bp)
1493 {
1494         db_printf("  cmd: ");
1495         switch (bp->bio_cmd) {
1496         case BIO_READ: db_printf("BIO_READ"); break;
1497         case BIO_WRITE: db_printf("BIO_WRITE"); break;
1498         case BIO_DELETE: db_printf("BIO_DELETE"); break;
1499         case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1500         case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1501         case BIO_CMD0: db_printf("BIO_CMD0"); break;
1502         case BIO_CMD1: db_printf("BIO_CMD1"); break;
1503         case BIO_CMD2: db_printf("BIO_CMD2"); break;
1504         case BIO_ZONE: db_printf("BIO_ZONE"); break;
1505         default: db_printf("UNKNOWN"); break;
1506         }
1507         db_printf("\n");
1508 }
1509
1510 static void
1511 db_print_bio_flags(struct bio *bp)
1512 {
1513         int comma;
1514
1515         comma = 0;
1516         db_printf("  flags: ");
1517         if (bp->bio_flags & BIO_ERROR) {
1518                 db_printf("BIO_ERROR");
1519                 comma = 1;
1520         }
1521         if (bp->bio_flags & BIO_DONE) {
1522                 db_printf("%sBIO_DONE", (comma ? ", " : ""));
1523                 comma = 1;
1524         }
1525         if (bp->bio_flags & BIO_ONQUEUE)
1526                 db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1527         db_printf("\n");
1528 }
1529
1530 /*
1531  * Print useful information in a BIO
1532  */
1533 DB_SHOW_COMMAND(bio, db_show_bio)
1534 {
1535         struct bio *bp;
1536
1537         if (have_addr) {
1538                 bp = (struct bio *)addr;
1539                 db_printf("BIO %p\n", bp);
1540                 db_print_bio_cmd(bp);
1541                 db_print_bio_flags(bp);
1542                 db_printf("  cflags: 0x%hx\n", bp->bio_cflags);
1543                 db_printf("  pflags: 0x%hx\n", bp->bio_pflags);
1544                 db_printf("  offset: %jd\n", (intmax_t)bp->bio_offset);
1545                 db_printf("  length: %jd\n", (intmax_t)bp->bio_length);
1546                 db_printf("  bcount: %ld\n", bp->bio_bcount);
1547                 db_printf("  resid: %ld\n", bp->bio_resid);
1548                 db_printf("  completed: %jd\n", (intmax_t)bp->bio_completed);
1549                 db_printf("  children: %u\n", bp->bio_children);
1550                 db_printf("  inbed: %u\n", bp->bio_inbed);
1551                 db_printf("  error: %d\n", bp->bio_error);
1552                 db_printf("  parent: %p\n", bp->bio_parent);
1553                 db_printf("  driver1: %p\n", bp->bio_driver1);
1554                 db_printf("  driver2: %p\n", bp->bio_driver2);
1555                 db_printf("  caller1: %p\n", bp->bio_caller1);
1556                 db_printf("  caller2: %p\n", bp->bio_caller2);
1557                 db_printf("  bio_from: %p\n", bp->bio_from);
1558                 db_printf("  bio_to: %p\n", bp->bio_to);
1559
1560 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1561                 db_printf("  bio_track_bp: %p\n", bp->bio_track_bp);
1562 #endif
1563         }
1564 }
1565
1566 #undef  gprintf
1567 #undef  gprintln
1568 #undef  ADDFLAG
1569
1570 #endif  /* DDB */