]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/geom/geom_subr.c
MFC r289577:
[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.
438  */
439 void
440 g_wither_washer()
441 {
442         struct g_class *mp;
443         struct g_geom *gp, *gp2;
444         struct g_provider *pp, *pp2;
445         struct g_consumer *cp, *cp2;
446
447         g_topology_assert();
448         LIST_FOREACH(mp, &g_classes, class) {
449                 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
450                         LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
451                                 if (!(pp->flags & G_PF_WITHER))
452                                         continue;
453                                 if (LIST_EMPTY(&pp->consumers))
454                                         g_destroy_provider(pp);
455                         }
456                         if (!(gp->flags & G_GEOM_WITHER))
457                                 continue;
458                         LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
459                                 if (LIST_EMPTY(&pp->consumers))
460                                         g_destroy_provider(pp);
461                         }
462                         LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
463                                 if (cp->acr || cp->acw || cp->ace)
464                                         continue;
465                                 if (cp->provider != NULL)
466                                         g_detach(cp);
467                                 g_destroy_consumer(cp);
468                         }
469                         if (LIST_EMPTY(&gp->provider) &&
470                             LIST_EMPTY(&gp->consumer))
471                                 g_destroy_geom(gp);
472                 }
473         }
474 }
475
476 struct g_consumer *
477 g_new_consumer(struct g_geom *gp)
478 {
479         struct g_consumer *cp;
480
481         g_topology_assert();
482         G_VALID_GEOM(gp);
483         KASSERT(!(gp->flags & G_GEOM_WITHER),
484             ("g_new_consumer on WITHERing geom(%s) (class %s)",
485             gp->name, gp->class->name));
486         KASSERT(gp->orphan != NULL,
487             ("g_new_consumer on geom(%s) (class %s) without orphan",
488             gp->name, gp->class->name));
489
490         cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
491         cp->geom = gp;
492         cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
493             DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
494         LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
495         return(cp);
496 }
497
498 void
499 g_destroy_consumer(struct g_consumer *cp)
500 {
501         struct g_geom *gp;
502
503         g_topology_assert();
504         G_VALID_CONSUMER(cp);
505         g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
506         KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
507         KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
508         KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
509         KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
510         g_cancel_event(cp);
511         gp = cp->geom;
512         LIST_REMOVE(cp, consumer);
513         devstat_remove_entry(cp->stat);
514         g_free(cp);
515         if (gp->flags & G_GEOM_WITHER)
516                 g_do_wither();
517 }
518
519 static void
520 g_new_provider_event(void *arg, int flag)
521 {
522         struct g_class *mp;
523         struct g_provider *pp;
524         struct g_consumer *cp, *next_cp;
525
526         g_topology_assert();
527         if (flag == EV_CANCEL)
528                 return;
529         if (g_shutdown)
530                 return;
531         pp = arg;
532         G_VALID_PROVIDER(pp);
533         KASSERT(!(pp->flags & G_PF_WITHER),
534             ("g_new_provider_event but withered"));
535         LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
536                 if ((cp->flags & G_CF_ORPHAN) == 0 &&
537                     cp->geom->attrchanged != NULL)
538                         cp->geom->attrchanged(cp, "GEOM::media");
539         }
540         LIST_FOREACH(mp, &g_classes, class) {
541                 if (mp->taste == NULL)
542                         continue;
543                 LIST_FOREACH(cp, &pp->consumers, consumers)
544                         if (cp->geom->class == mp &&
545                             (cp->flags & G_CF_ORPHAN) == 0)
546                                 break;
547                 if (cp != NULL)
548                         continue;
549                 mp->taste(mp, pp, 0);
550                 g_topology_assert();
551         }
552 }
553
554
555 struct g_provider *
556 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
557 {
558         struct g_provider *pp;
559         struct sbuf *sb;
560         va_list ap;
561
562         g_topology_assert();
563         G_VALID_GEOM(gp);
564         KASSERT(gp->access != NULL,
565             ("new provider on geom(%s) without ->access (class %s)",
566             gp->name, gp->class->name));
567         KASSERT(gp->start != NULL,
568             ("new provider on geom(%s) without ->start (class %s)",
569             gp->name, gp->class->name));
570         KASSERT(!(gp->flags & G_GEOM_WITHER),
571             ("new provider on WITHERing geom(%s) (class %s)",
572             gp->name, gp->class->name));
573         sb = sbuf_new_auto();
574         va_start(ap, fmt);
575         sbuf_vprintf(sb, fmt, ap);
576         va_end(ap);
577         sbuf_finish(sb);
578         pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
579         pp->name = (char *)(pp + 1);
580         strcpy(pp->name, sbuf_data(sb));
581         sbuf_delete(sb);
582         LIST_INIT(&pp->consumers);
583         pp->error = ENXIO;
584         pp->geom = gp;
585         pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
586             DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
587         LIST_INSERT_HEAD(&gp->provider, pp, provider);
588         g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
589         return (pp);
590 }
591
592 void
593 g_error_provider(struct g_provider *pp, int error)
594 {
595
596         /* G_VALID_PROVIDER(pp);  We may not have g_topology */
597         pp->error = error;
598 }
599
600 #ifndef _PATH_DEV
601 #define _PATH_DEV       "/dev/"
602 #endif
603
604 struct g_provider *
605 g_provider_by_name(char const *arg)
606 {
607         struct g_class *cp;
608         struct g_geom *gp;
609         struct g_provider *pp, *wpp;
610
611         if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
612                 arg += sizeof(_PATH_DEV) - 1;
613
614         wpp = NULL;
615         LIST_FOREACH(cp, &g_classes, class) {
616                 LIST_FOREACH(gp, &cp->geom, geom) {
617                         LIST_FOREACH(pp, &gp->provider, provider) {
618                                 if (strcmp(arg, pp->name) != 0)
619                                         continue;
620                                 if ((gp->flags & G_GEOM_WITHER) == 0 &&
621                                     (pp->flags & G_PF_WITHER) == 0)
622                                         return (pp);
623                                 else
624                                         wpp = pp;
625                         }
626                 }
627         }
628
629         return (wpp);
630 }
631
632 void
633 g_destroy_provider(struct g_provider *pp)
634 {
635         struct g_geom *gp;
636
637         g_topology_assert();
638         G_VALID_PROVIDER(pp);
639         KASSERT(LIST_EMPTY(&pp->consumers),
640             ("g_destroy_provider but attached"));
641         KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
642         KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
643         KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
644         g_cancel_event(pp);
645         LIST_REMOVE(pp, provider);
646         gp = pp->geom;
647         devstat_remove_entry(pp->stat);
648         /*
649          * If a callback was provided, send notification that the provider
650          * is now gone.
651          */
652         if (gp->providergone != NULL)
653                 gp->providergone(pp);
654
655         g_free(pp);
656         if ((gp->flags & G_GEOM_WITHER))
657                 g_do_wither();
658 }
659
660 /*
661  * We keep the "geoms" list sorted by topological order (== increasing
662  * numerical rank) at all times.
663  * When an attach is done, the attaching geoms rank is invalidated
664  * and it is moved to the tail of the list.
665  * All geoms later in the sequence has their ranks reevaluated in
666  * sequence.  If we cannot assign rank to a geom because it's
667  * prerequisites do not have rank, we move that element to the tail
668  * of the sequence with invalid rank as well.
669  * At some point we encounter our original geom and if we stil fail
670  * to assign it a rank, there must be a loop and we fail back to
671  * g_attach() which detach again and calls redo_rank again
672  * to fix up the damage.
673  * It would be much simpler code wise to do it recursively, but we
674  * can't risk that on the kernel stack.
675  */
676
677 static int
678 redo_rank(struct g_geom *gp)
679 {
680         struct g_consumer *cp;
681         struct g_geom *gp1, *gp2;
682         int n, m;
683
684         g_topology_assert();
685         G_VALID_GEOM(gp);
686
687         /* Invalidate this geoms rank and move it to the tail */
688         gp1 = TAILQ_NEXT(gp, geoms);
689         if (gp1 != NULL) {
690                 gp->rank = 0;
691                 TAILQ_REMOVE(&geoms, gp, geoms);
692                 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
693         } else {
694                 gp1 = gp;
695         }
696
697         /* re-rank the rest of the sequence */
698         for (; gp1 != NULL; gp1 = gp2) {
699                 gp1->rank = 0;
700                 m = 1;
701                 LIST_FOREACH(cp, &gp1->consumer, consumer) {
702                         if (cp->provider == NULL)
703                                 continue;
704                         n = cp->provider->geom->rank;
705                         if (n == 0) {
706                                 m = 0;
707                                 break;
708                         } else if (n >= m)
709                                 m = n + 1;
710                 }
711                 gp1->rank = m;
712                 gp2 = TAILQ_NEXT(gp1, geoms);
713
714                 /* got a rank, moving on */
715                 if (m != 0)
716                         continue;
717
718                 /* no rank to original geom means loop */
719                 if (gp == gp1) 
720                         return (ELOOP);
721
722                 /* no rank, put it at the end move on */
723                 TAILQ_REMOVE(&geoms, gp1, geoms);
724                 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
725         }
726         return (0);
727 }
728
729 int
730 g_attach(struct g_consumer *cp, struct g_provider *pp)
731 {
732         int error;
733
734         g_topology_assert();
735         G_VALID_CONSUMER(cp);
736         G_VALID_PROVIDER(pp);
737         g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
738         KASSERT(cp->provider == NULL, ("attach but attached"));
739         cp->provider = pp;
740         LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
741         error = redo_rank(cp->geom);
742         if (error) {
743                 LIST_REMOVE(cp, consumers);
744                 cp->provider = NULL;
745                 redo_rank(cp->geom);
746         }
747         return (error);
748 }
749
750 void
751 g_detach(struct g_consumer *cp)
752 {
753         struct g_provider *pp;
754
755         g_topology_assert();
756         G_VALID_CONSUMER(cp);
757         g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
758         KASSERT(cp->provider != NULL, ("detach but not attached"));
759         KASSERT(cp->acr == 0, ("detach but nonzero acr"));
760         KASSERT(cp->acw == 0, ("detach but nonzero acw"));
761         KASSERT(cp->ace == 0, ("detach but nonzero ace"));
762         KASSERT(cp->nstart == cp->nend,
763             ("detach with active requests"));
764         pp = cp->provider;
765         LIST_REMOVE(cp, consumers);
766         cp->provider = NULL;
767         if ((cp->geom->flags & G_GEOM_WITHER) ||
768             (pp->geom->flags & G_GEOM_WITHER) ||
769             (pp->flags & G_PF_WITHER))
770                 g_do_wither();
771         redo_rank(cp->geom);
772 }
773
774 /*
775  * g_access()
776  *
777  * Access-check with delta values.  The question asked is "can provider
778  * "cp" change the access counters by the relative amounts dc[rwe] ?"
779  */
780
781 int
782 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
783 {
784         struct g_provider *pp;
785         int pr,pw,pe;
786         int error;
787
788         g_topology_assert();
789         G_VALID_CONSUMER(cp);
790         pp = cp->provider;
791         KASSERT(pp != NULL, ("access but not attached"));
792         G_VALID_PROVIDER(pp);
793
794         g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
795             cp, pp->name, dcr, dcw, dce);
796
797         KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
798         KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
799         KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
800         KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
801         KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
802
803         /*
804          * If our class cares about being spoiled, and we have been, we
805          * are probably just ahead of the event telling us that.  Fail
806          * now rather than having to unravel this later.
807          */
808         if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
809             (dcr > 0 || dcw > 0 || dce > 0))
810                 return (ENXIO);
811
812         /*
813          * Figure out what counts the provider would have had, if this
814          * consumer had (r0w0e0) at this time.
815          */
816         pr = pp->acr - cp->acr;
817         pw = pp->acw - cp->acw;
818         pe = pp->ace - cp->ace;
819
820         g_trace(G_T_ACCESS,
821     "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
822             dcr, dcw, dce,
823             cp->acr, cp->acw, cp->ace,
824             pp->acr, pp->acw, pp->ace,
825             pp, pp->name);
826
827         /* If foot-shooting is enabled, any open on rank#1 is OK */
828         if ((g_debugflags & 16) && pp->geom->rank == 1)
829                 ;
830         /* If we try exclusive but already write: fail */
831         else if (dce > 0 && pw > 0)
832                 return (EPERM);
833         /* If we try write but already exclusive: fail */
834         else if (dcw > 0 && pe > 0)
835                 return (EPERM);
836         /* If we try to open more but provider is error'ed: fail */
837         else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
838                 return (pp->error);
839
840         /* Ok then... */
841
842         error = pp->geom->access(pp, dcr, dcw, dce);
843         KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
844             ("Geom provider %s::%s dcr=%d dcw=%d dce=%d error=%d failed "
845             "closing ->access()", pp->geom->class->name, pp->name, dcr, dcw,
846             dce, error));
847         if (!error) {
848                 /*
849                  * If we open first write, spoil any partner consumers.
850                  * If we close last write and provider is not errored,
851                  * trigger re-taste.
852                  */
853                 if (pp->acw == 0 && dcw != 0)
854                         g_spoil(pp, cp);
855                 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
856                     !(pp->geom->flags & G_GEOM_WITHER))
857                         g_post_event(g_new_provider_event, pp, M_WAITOK, 
858                             pp, NULL);
859
860                 pp->acr += dcr;
861                 pp->acw += dcw;
862                 pp->ace += dce;
863                 cp->acr += dcr;
864                 cp->acw += dcw;
865                 cp->ace += dce;
866                 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
867                         KASSERT(pp->sectorsize > 0,
868                             ("Provider %s lacks sectorsize", pp->name));
869                 if ((cp->geom->flags & G_GEOM_WITHER) &&
870                     cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
871                         g_do_wither();
872         }
873         return (error);
874 }
875
876 int
877 g_handleattr_int(struct bio *bp, const char *attribute, int val)
878 {
879
880         return (g_handleattr(bp, attribute, &val, sizeof val));
881 }
882
883 int
884 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
885 {
886
887         return (g_handleattr(bp, attribute, &val, sizeof val));
888 }
889
890 int
891 g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
892 {
893
894         return (g_handleattr(bp, attribute, str, 0));
895 }
896
897 int
898 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
899 {
900         int error = 0;
901
902         if (strcmp(bp->bio_attribute, attribute))
903                 return (0);
904         if (len == 0) {
905                 bzero(bp->bio_data, bp->bio_length);
906                 if (strlcpy(bp->bio_data, val, bp->bio_length) >=
907                     bp->bio_length) {
908                         printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
909                             __func__, bp->bio_to->name,
910                             (intmax_t)bp->bio_length, strlen(val));
911                         error = EFAULT;
912                 }
913         } else if (bp->bio_length == len) {
914                 bcopy(val, bp->bio_data, len);
915         } else {
916                 printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
917                     bp->bio_to->name, (intmax_t)bp->bio_length, len);
918                 error = EFAULT;
919         }
920         if (error == 0)
921                 bp->bio_completed = bp->bio_length;
922         g_io_deliver(bp, error);
923         return (1);
924 }
925
926 int
927 g_std_access(struct g_provider *pp,
928         int dr __unused, int dw __unused, int de __unused)
929 {
930
931         g_topology_assert();
932         G_VALID_PROVIDER(pp);
933         return (0);
934 }
935
936 void
937 g_std_done(struct bio *bp)
938 {
939         struct bio *bp2;
940
941         bp2 = bp->bio_parent;
942         if (bp2->bio_error == 0)
943                 bp2->bio_error = bp->bio_error;
944         bp2->bio_completed += bp->bio_completed;
945         g_destroy_bio(bp);
946         bp2->bio_inbed++;
947         if (bp2->bio_children == bp2->bio_inbed)
948                 g_io_deliver(bp2, bp2->bio_error);
949 }
950
951 /* XXX: maybe this is only g_slice_spoiled */
952
953 void
954 g_std_spoiled(struct g_consumer *cp)
955 {
956         struct g_geom *gp;
957         struct g_provider *pp;
958
959         g_topology_assert();
960         G_VALID_CONSUMER(cp);
961         g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
962         cp->flags |= G_CF_ORPHAN;
963         g_detach(cp);
964         gp = cp->geom;
965         LIST_FOREACH(pp, &gp->provider, provider)
966                 g_orphan_provider(pp, ENXIO);
967         g_destroy_consumer(cp);
968         if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
969                 g_destroy_geom(gp);
970         else
971                 gp->flags |= G_GEOM_WITHER;
972 }
973
974 /*
975  * Spoiling happens when a provider is opened for writing, but consumers
976  * which are configured by in-band data are attached (slicers for instance).
977  * Since the write might potentially change the in-band data, such consumers
978  * need to re-evaluate their existence after the writing session closes.
979  * We do this by (offering to) tear them down when the open for write happens
980  * in return for a re-taste when it closes again.
981  * Together with the fact that such consumers grab an 'e' bit whenever they
982  * are open, regardless of mode, this ends up DTRT.
983  */
984
985 static void
986 g_spoil_event(void *arg, int flag)
987 {
988         struct g_provider *pp;
989         struct g_consumer *cp, *cp2;
990
991         g_topology_assert();
992         if (flag == EV_CANCEL)
993                 return;
994         pp = arg;
995         G_VALID_PROVIDER(pp);
996         for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
997                 cp2 = LIST_NEXT(cp, consumers);
998                 if ((cp->flags & G_CF_SPOILED) == 0)
999                         continue;
1000                 cp->flags &= ~G_CF_SPOILED;
1001                 if (cp->geom->spoiled == NULL)
1002                         continue;
1003                 cp->geom->spoiled(cp);
1004                 g_topology_assert();
1005         }
1006 }
1007
1008 void
1009 g_spoil(struct g_provider *pp, struct g_consumer *cp)
1010 {
1011         struct g_consumer *cp2;
1012
1013         g_topology_assert();
1014         G_VALID_PROVIDER(pp);
1015         G_VALID_CONSUMER(cp);
1016
1017         LIST_FOREACH(cp2, &pp->consumers, consumers) {
1018                 if (cp2 == cp)
1019                         continue;
1020 /*
1021                 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1022                 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1023 */
1024                 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1025                 cp2->flags |= G_CF_SPOILED;
1026         }
1027         g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1028 }
1029
1030 static void
1031 g_media_changed_event(void *arg, int flag)
1032 {
1033         struct g_provider *pp;
1034         int retaste;
1035
1036         g_topology_assert();
1037         if (flag == EV_CANCEL)
1038                 return;
1039         pp = arg;
1040         G_VALID_PROVIDER(pp);
1041
1042         /*
1043          * If provider was not open for writing, queue retaste after spoiling.
1044          * If it was, retaste will happen automatically on close.
1045          */
1046         retaste = (pp->acw == 0 && pp->error == 0 &&
1047             !(pp->geom->flags & G_GEOM_WITHER));
1048         g_spoil_event(arg, flag);
1049         if (retaste)
1050                 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
1051 }
1052
1053 int
1054 g_media_changed(struct g_provider *pp, int flag)
1055 {
1056         struct g_consumer *cp;
1057
1058         LIST_FOREACH(cp, &pp->consumers, consumers)
1059                 cp->flags |= G_CF_SPOILED;
1060         return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
1061 }
1062
1063 int
1064 g_media_gone(struct g_provider *pp, int flag)
1065 {
1066         struct g_consumer *cp;
1067
1068         LIST_FOREACH(cp, &pp->consumers, consumers)
1069                 cp->flags |= G_CF_SPOILED;
1070         return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
1071 }
1072
1073 int
1074 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1075 {
1076         int error, i;
1077
1078         i = len;
1079         error = g_io_getattr(attr, cp, &i, var);
1080         if (error)
1081                 return (error);
1082         if (i != len)
1083                 return (EINVAL);
1084         return (0);
1085 }
1086
1087 static int
1088 g_get_device_prefix_len(const char *name)
1089 {
1090         int len;
1091
1092         if (strncmp(name, "ada", 3) == 0)
1093                 len = 3;
1094         else if (strncmp(name, "ad", 2) == 0)
1095                 len = 2;
1096         else
1097                 return (0);
1098         if (name[len] < '0' || name[len] > '9')
1099                 return (0);
1100         do {
1101                 len++;
1102         } while (name[len] >= '0' && name[len] <= '9');
1103         return (len);
1104 }
1105
1106 int
1107 g_compare_names(const char *namea, const char *nameb)
1108 {
1109         int deva, devb;
1110
1111         if (strcmp(namea, nameb) == 0)
1112                 return (1);
1113         deva = g_get_device_prefix_len(namea);
1114         if (deva == 0)
1115                 return (0);
1116         devb = g_get_device_prefix_len(nameb);
1117         if (devb == 0)
1118                 return (0);
1119         if (strcmp(namea + deva, nameb + devb) == 0)
1120                 return (1);
1121         return (0);
1122 }
1123
1124 #if defined(DIAGNOSTIC) || defined(DDB)
1125 /*
1126  * This function walks the mesh and returns a non-zero integer if it
1127  * finds the argument pointer is an object. The return value indicates
1128  * which type of object it is believed to be. If topology is not locked,
1129  * this function is potentially dangerous, but we don't assert that the
1130  * topology lock is held when called from debugger.
1131  */
1132 int
1133 g_valid_obj(void const *ptr)
1134 {
1135         struct g_class *mp;
1136         struct g_geom *gp;
1137         struct g_consumer *cp;
1138         struct g_provider *pp;
1139
1140 #ifdef KDB
1141         if (kdb_active == 0)
1142 #endif
1143                 g_topology_assert();
1144
1145         LIST_FOREACH(mp, &g_classes, class) {
1146                 if (ptr == mp)
1147                         return (1);
1148                 LIST_FOREACH(gp, &mp->geom, geom) {
1149                         if (ptr == gp)
1150                                 return (2);
1151                         LIST_FOREACH(cp, &gp->consumer, consumer)
1152                                 if (ptr == cp)
1153                                         return (3);
1154                         LIST_FOREACH(pp, &gp->provider, provider)
1155                                 if (ptr == pp)
1156                                         return (4);
1157                 }
1158         }
1159         return(0);
1160 }
1161 #endif
1162
1163 #ifdef DDB
1164
1165 #define gprintf(...)    do {                                            \
1166         db_printf("%*s", indent, "");                                   \
1167         db_printf(__VA_ARGS__);                                         \
1168 } while (0)
1169 #define gprintln(...)   do {                                            \
1170         gprintf(__VA_ARGS__);                                           \
1171         db_printf("\n");                                                \
1172 } while (0)
1173
1174 #define ADDFLAG(obj, flag, sflag)       do {                            \
1175         if ((obj)->flags & (flag)) {                                    \
1176                 if (comma)                                              \
1177                         strlcat(str, ",", size);                        \
1178                 strlcat(str, (sflag), size);                            \
1179                 comma = 1;                                              \
1180         }                                                               \
1181 } while (0)
1182
1183 static char *
1184 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1185 {
1186         int comma = 0;
1187
1188         bzero(str, size);
1189         if (pp->flags == 0) {
1190                 strlcpy(str, "NONE", size);
1191                 return (str);
1192         }
1193         ADDFLAG(pp, G_PF_CANDELETE, "G_PF_CANDELETE");
1194         ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1195         ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1196         return (str);
1197 }
1198
1199 static char *
1200 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1201 {
1202         int comma = 0;
1203
1204         bzero(str, size);
1205         if (gp->flags == 0) {
1206                 strlcpy(str, "NONE", size);
1207                 return (str);
1208         }
1209         ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1210         return (str);
1211 }
1212 static void
1213 db_show_geom_consumer(int indent, struct g_consumer *cp)
1214 {
1215
1216         if (indent == 0) {
1217                 gprintln("consumer: %p", cp);
1218                 gprintln("  class:    %s (%p)", cp->geom->class->name,
1219                     cp->geom->class);
1220                 gprintln("  geom:     %s (%p)", cp->geom->name, cp->geom);
1221                 if (cp->provider == NULL)
1222                         gprintln("  provider: none");
1223                 else {
1224                         gprintln("  provider: %s (%p)", cp->provider->name,
1225                             cp->provider);
1226                 }
1227                 gprintln("  access:   r%dw%de%d", cp->acr, cp->acw, cp->ace);
1228                 gprintln("  flags:    0x%04x", cp->flags);
1229                 gprintln("  nstart:   %u", cp->nstart);
1230                 gprintln("  nend:     %u", cp->nend);
1231         } else {
1232                 gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1233                     cp->provider != NULL ? cp->provider->name : "none",
1234                     cp->acr, cp->acw, cp->ace);
1235                 if (cp->flags)
1236                         db_printf(", flags=0x%04x", cp->flags);
1237                 db_printf("\n");
1238         }
1239 }
1240
1241 static void
1242 db_show_geom_provider(int indent, struct g_provider *pp)
1243 {
1244         struct g_consumer *cp;
1245         char flags[64];
1246
1247         if (indent == 0) {
1248                 gprintln("provider: %s (%p)", pp->name, pp);
1249                 gprintln("  class:        %s (%p)", pp->geom->class->name,
1250                     pp->geom->class);
1251                 gprintln("  geom:         %s (%p)", pp->geom->name, pp->geom);
1252                 gprintln("  mediasize:    %jd", (intmax_t)pp->mediasize);
1253                 gprintln("  sectorsize:   %u", pp->sectorsize);
1254                 gprintln("  stripesize:   %u", pp->stripesize);
1255                 gprintln("  stripeoffset: %u", pp->stripeoffset);
1256                 gprintln("  access:       r%dw%de%d", pp->acr, pp->acw,
1257                     pp->ace);
1258                 gprintln("  flags:        %s (0x%04x)",
1259                     provider_flags_to_string(pp, flags, sizeof(flags)),
1260                     pp->flags);
1261                 gprintln("  error:        %d", pp->error);
1262                 gprintln("  nstart:       %u", pp->nstart);
1263                 gprintln("  nend:         %u", pp->nend);
1264                 if (LIST_EMPTY(&pp->consumers))
1265                         gprintln("  consumers:    none");
1266         } else {
1267                 gprintf("provider: %s (%p), access=r%dw%de%d",
1268                     pp->name, pp, pp->acr, pp->acw, pp->ace);
1269                 if (pp->flags != 0) {
1270                         db_printf(", flags=%s (0x%04x)",
1271                             provider_flags_to_string(pp, flags, sizeof(flags)),
1272                             pp->flags);
1273                 }
1274                 db_printf("\n");
1275         }
1276         if (!LIST_EMPTY(&pp->consumers)) {
1277                 LIST_FOREACH(cp, &pp->consumers, consumers) {
1278                         db_show_geom_consumer(indent + 2, cp);
1279                         if (db_pager_quit)
1280                                 break;
1281                 }
1282         }
1283 }
1284
1285 static void
1286 db_show_geom_geom(int indent, struct g_geom *gp)
1287 {
1288         struct g_provider *pp;
1289         struct g_consumer *cp;
1290         char flags[64];
1291
1292         if (indent == 0) {
1293                 gprintln("geom: %s (%p)", gp->name, gp);
1294                 gprintln("  class:     %s (%p)", gp->class->name, gp->class);
1295                 gprintln("  flags:     %s (0x%04x)",
1296                     geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1297                 gprintln("  rank:      %d", gp->rank);
1298                 if (LIST_EMPTY(&gp->provider))
1299                         gprintln("  providers: none");
1300                 if (LIST_EMPTY(&gp->consumer))
1301                         gprintln("  consumers: none");
1302         } else {
1303                 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1304                 if (gp->flags != 0) {
1305                         db_printf(", flags=%s (0x%04x)",
1306                             geom_flags_to_string(gp, flags, sizeof(flags)),
1307                             gp->flags);
1308                 }
1309                 db_printf("\n");
1310         }
1311         if (!LIST_EMPTY(&gp->provider)) {
1312                 LIST_FOREACH(pp, &gp->provider, provider) {
1313                         db_show_geom_provider(indent + 2, pp);
1314                         if (db_pager_quit)
1315                                 break;
1316                 }
1317         }
1318         if (!LIST_EMPTY(&gp->consumer)) {
1319                 LIST_FOREACH(cp, &gp->consumer, consumer) {
1320                         db_show_geom_consumer(indent + 2, cp);
1321                         if (db_pager_quit)
1322                                 break;
1323                 }
1324         }
1325 }
1326
1327 static void
1328 db_show_geom_class(struct g_class *mp)
1329 {
1330         struct g_geom *gp;
1331
1332         db_printf("class: %s (%p)\n", mp->name, mp);
1333         LIST_FOREACH(gp, &mp->geom, geom) {
1334                 db_show_geom_geom(2, gp);
1335                 if (db_pager_quit)
1336                         break;
1337         }
1338 }
1339
1340 /*
1341  * Print the GEOM topology or the given object.
1342  */
1343 DB_SHOW_COMMAND(geom, db_show_geom)
1344 {
1345         struct g_class *mp;
1346
1347         if (!have_addr) {
1348                 /* No address given, print the entire topology. */
1349                 LIST_FOREACH(mp, &g_classes, class) {
1350                         db_show_geom_class(mp);
1351                         db_printf("\n");
1352                         if (db_pager_quit)
1353                                 break;
1354                 }
1355         } else {
1356                 switch (g_valid_obj((void *)addr)) {
1357                 case 1:
1358                         db_show_geom_class((struct g_class *)addr);
1359                         break;
1360                 case 2:
1361                         db_show_geom_geom(0, (struct g_geom *)addr);
1362                         break;
1363                 case 3:
1364                         db_show_geom_consumer(0, (struct g_consumer *)addr);
1365                         break;
1366                 case 4:
1367                         db_show_geom_provider(0, (struct g_provider *)addr);
1368                         break;
1369                 default:
1370                         db_printf("Not a GEOM object.\n");
1371                         break;
1372                 }
1373         }
1374 }
1375
1376 static void
1377 db_print_bio_cmd(struct bio *bp)
1378 {
1379         db_printf("  cmd: ");
1380         switch (bp->bio_cmd) {
1381         case BIO_READ: db_printf("BIO_READ"); break;
1382         case BIO_WRITE: db_printf("BIO_WRITE"); break;
1383         case BIO_DELETE: db_printf("BIO_DELETE"); break;
1384         case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1385         case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1386         case BIO_CMD0: db_printf("BIO_CMD0"); break;
1387         case BIO_CMD1: db_printf("BIO_CMD1"); break;
1388         case BIO_CMD2: db_printf("BIO_CMD2"); break;
1389         default: db_printf("UNKNOWN"); break;
1390         }
1391         db_printf("\n");
1392 }
1393
1394 static void
1395 db_print_bio_flags(struct bio *bp)
1396 {
1397         int comma;
1398
1399         comma = 0;
1400         db_printf("  flags: ");
1401         if (bp->bio_flags & BIO_ERROR) {
1402                 db_printf("BIO_ERROR");
1403                 comma = 1;
1404         }
1405         if (bp->bio_flags & BIO_DONE) {
1406                 db_printf("%sBIO_DONE", (comma ? ", " : ""));
1407                 comma = 1;
1408         }
1409         if (bp->bio_flags & BIO_ONQUEUE)
1410                 db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1411         db_printf("\n");
1412 }
1413
1414 /*
1415  * Print useful information in a BIO
1416  */
1417 DB_SHOW_COMMAND(bio, db_show_bio)
1418 {
1419         struct bio *bp;
1420
1421         if (have_addr) {
1422                 bp = (struct bio *)addr;
1423                 db_printf("BIO %p\n", bp);
1424                 db_print_bio_cmd(bp);
1425                 db_print_bio_flags(bp);
1426                 db_printf("  cflags: 0x%hhx\n", bp->bio_cflags);
1427                 db_printf("  pflags: 0x%hhx\n", bp->bio_pflags);
1428                 db_printf("  offset: %jd\n", (intmax_t)bp->bio_offset);
1429                 db_printf("  length: %jd\n", (intmax_t)bp->bio_length);
1430                 db_printf("  bcount: %ld\n", bp->bio_bcount);
1431                 db_printf("  resid: %ld\n", bp->bio_resid);
1432                 db_printf("  completed: %jd\n", (intmax_t)bp->bio_completed);
1433                 db_printf("  children: %u\n", bp->bio_children);
1434                 db_printf("  inbed: %u\n", bp->bio_inbed);
1435                 db_printf("  error: %d\n", bp->bio_error);
1436                 db_printf("  parent: %p\n", bp->bio_parent);
1437                 db_printf("  driver1: %p\n", bp->bio_driver1);
1438                 db_printf("  driver2: %p\n", bp->bio_driver2);
1439                 db_printf("  caller1: %p\n", bp->bio_caller1);
1440                 db_printf("  caller2: %p\n", bp->bio_caller2);
1441                 db_printf("  bio_from: %p\n", bp->bio_from);
1442                 db_printf("  bio_to: %p\n", bp->bio_to);
1443         }
1444 }
1445
1446 #undef  gprintf
1447 #undef  gprintln
1448 #undef  ADDFLAG
1449
1450 #endif  /* DDB */