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