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