2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
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>
47 #include <sys/sysctl.h>
49 #include <sys/kthread.h>
51 #include <sys/mutex.h>
52 #include <sys/errno.h>
54 #include <geom/geom.h>
55 #include <geom/geom_int.h>
56 #include <machine/stdarg.h>
62 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
63 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
64 char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
73 * This event offers a new class a chance to taste all preexisting providers.
76 g_load_class(void *arg, int flag)
79 struct g_class *mp2, *mp;
81 struct g_provider *pp;
84 if (flag == EV_CANCEL) /* XXX: can't happen ? */
96 g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
97 KASSERT(mp->name != NULL && *mp->name != '\0',
98 ("GEOM class has no name"));
99 LIST_FOREACH(mp2, &g_classes, class) {
101 printf("The GEOM class %s is already loaded.\n",
106 } else if (strcmp(mp2->name, mp->name) == 0) {
107 printf("A GEOM class %s is already loaded.\n",
115 LIST_INIT(&mp->geom);
116 LIST_INSERT_HEAD(&g_classes, mp, class);
117 if (mp->init != NULL)
119 if (mp->taste == NULL)
121 LIST_FOREACH(mp2, &g_classes, class) {
124 LIST_FOREACH(gp, &mp2->geom, geom) {
125 LIST_FOREACH(pp, &gp->provider, provider) {
126 mp->taste(mp, pp, 0);
134 g_unload_class(void *arg, int flag)
139 struct g_provider *pp;
140 struct g_consumer *cp;
147 g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
150 * We allow unloading if we have no geoms, or a class
151 * method we can use to get rid of them.
153 if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
154 hh->error = EOPNOTSUPP;
158 /* We refuse to unload if anything is open */
159 LIST_FOREACH(gp, &mp->geom, geom) {
160 LIST_FOREACH(pp, &gp->provider, provider)
161 if (pp->acr || pp->acw || pp->ace) {
165 LIST_FOREACH(cp, &gp->consumer, consumer)
166 if (cp->acr || cp->acw || cp->ace) {
172 /* Bar new entries */
178 gp = LIST_FIRST(&mp->geom);
181 error = mp->destroy_geom(NULL, mp, gp);
186 if (mp->fini != NULL)
188 LIST_REMOVE(mp, class);
195 g_modevent(module_t mod, int type, void *data)
199 static int g_ignition;
203 if (mp->version != G_VERSION) {
204 printf("GEOM class %s has Wrong version %x\n",
205 mp->name, mp->version);
212 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
217 g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name);
219 * Once the system is not cold, MOD_LOAD calls will be
220 * from the userland and the g_event thread will be able
221 * to acknowledge their completion.
225 error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
227 error = g_waitfor_event(g_load_class, hh, M_WAITOK,
235 g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name);
236 error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL);
240 KASSERT(LIST_EMPTY(&hh->mp->geom),
241 ("Unloaded class (%s) still has geom", hh->mp->name));
253 g_new_geomf(struct g_class *mp, const char *fmt, ...)
261 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
263 sbuf_vprintf(sb, fmt, ap);
266 gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
267 gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
270 LIST_INIT(&gp->consumer);
271 LIST_INIT(&gp->provider);
272 LIST_INSERT_HEAD(&mp->geom, gp, geom);
273 TAILQ_INSERT_HEAD(&geoms, gp, geoms);
274 strcpy(gp->name, sbuf_data(sb));
276 /* Fill in defaults from class */
277 gp->start = mp->start;
278 gp->spoiled = mp->spoiled;
279 gp->dumpconf = mp->dumpconf;
280 gp->access = mp->access;
281 gp->orphan = mp->orphan;
282 gp->ioctl = mp->ioctl;
287 g_destroy_geom(struct g_geom *gp)
292 g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
293 KASSERT(LIST_EMPTY(&gp->consumer),
294 ("g_destroy_geom(%s) with consumer(s) [%p]",
295 gp->name, LIST_FIRST(&gp->consumer)));
296 KASSERT(LIST_EMPTY(&gp->provider),
297 ("g_destroy_geom(%s) with provider(s) [%p]",
298 gp->name, LIST_FIRST(&gp->provider)));
300 LIST_REMOVE(gp, geom);
301 TAILQ_REMOVE(&geoms, gp, geoms);
307 * This function is called (repeatedly) until the has withered away.
310 g_wither_geom(struct g_geom *gp, int error)
312 struct g_provider *pp;
316 g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
317 if (!(gp->flags & G_GEOM_WITHER)) {
318 gp->flags |= G_GEOM_WITHER;
319 LIST_FOREACH(pp, &gp->provider, provider)
320 if (!(pp->flags & G_PF_ORPHAN))
321 g_orphan_provider(pp, error);
327 * Convenience function to destroy a particular provider.
330 g_wither_provider(struct g_provider *pp, int error)
333 pp->flags |= G_PF_WITHER;
334 if (!(pp->flags & G_PF_ORPHAN))
335 g_orphan_provider(pp, error);
339 * This function is called (repeatedly) until the has withered away.
342 g_wither_geom_close(struct g_geom *gp, int error)
344 struct g_consumer *cp;
348 g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
349 LIST_FOREACH(cp, &gp->consumer, consumer)
350 if (cp->acr || cp->acw || cp->ace)
351 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
352 g_wither_geom(gp, error);
356 * This function is called (repeatedly) until we cant wash away more
357 * withered bits at present. Return value contains two bits. Bit 0
358 * set means "withering stuff we can't wash now", bit 1 means "call
359 * me again, there may be stuff I didn't get the first time around.
365 struct g_geom *gp, *gp2;
366 struct g_provider *pp, *pp2;
367 struct g_consumer *cp, *cp2;
372 LIST_FOREACH(mp, &g_classes, class) {
373 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
374 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
375 if (!(pp->flags & G_PF_WITHER))
377 if (LIST_EMPTY(&pp->consumers))
378 g_destroy_provider(pp);
382 if (!(gp->flags & G_GEOM_WITHER))
384 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
385 if (LIST_EMPTY(&pp->consumers))
386 g_destroy_provider(pp);
390 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
391 if (cp->acr || cp->acw || cp->ace) {
395 if (cp->provider != NULL)
397 g_destroy_consumer(cp);
400 if (LIST_EMPTY(&gp->provider) &&
401 LIST_EMPTY(&gp->consumer))
411 g_new_consumer(struct g_geom *gp)
413 struct g_consumer *cp;
417 KASSERT(!(gp->flags & G_GEOM_WITHER),
418 ("g_new_consumer on WITHERing geom(%s) (class %s)",
419 gp->name, gp->class->name));
420 KASSERT(gp->orphan != NULL,
421 ("g_new_consumer on geom(%s) (class %s) without orphan",
422 gp->name, gp->class->name));
424 cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
426 cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
427 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
428 LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
433 g_destroy_consumer(struct g_consumer *cp)
438 G_VALID_CONSUMER(cp);
439 g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
440 KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
441 KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
442 KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
443 KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
446 LIST_REMOVE(cp, consumer);
447 devstat_remove_entry(cp->stat);
449 if (gp->flags & G_GEOM_WITHER)
454 g_new_provider_event(void *arg, int flag)
457 struct g_provider *pp;
458 struct g_consumer *cp;
462 if (flag == EV_CANCEL)
467 G_VALID_PROVIDER(pp);
468 LIST_FOREACH(mp, &g_classes, class) {
469 if (mp->taste == NULL)
472 LIST_FOREACH(cp, &pp->consumers, consumers)
473 if (cp->geom->class == mp)
477 mp->taste(mp, pp, 0);
484 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
486 struct g_provider *pp;
492 KASSERT(gp->access != NULL,
493 ("new provider on geom(%s) without ->access (class %s)",
494 gp->name, gp->class->name));
495 KASSERT(gp->start != NULL,
496 ("new provider on geom(%s) without ->start (class %s)",
497 gp->name, gp->class->name));
498 KASSERT(!(gp->flags & G_GEOM_WITHER),
499 ("new provider on WITHERing geom(%s) (class %s)",
500 gp->name, gp->class->name));
501 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
503 sbuf_vprintf(sb, fmt, ap);
506 pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
507 pp->name = (char *)(pp + 1);
508 strcpy(pp->name, sbuf_data(sb));
510 LIST_INIT(&pp->consumers);
513 pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
514 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
515 LIST_INSERT_HEAD(&gp->provider, pp, provider);
516 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
521 g_error_provider(struct g_provider *pp, int error)
524 /* G_VALID_PROVIDER(pp); We may not have g_topology */
529 g_provider_by_name(char const *arg)
533 struct g_provider *pp;
535 LIST_FOREACH(cp, &g_classes, class) {
536 LIST_FOREACH(gp, &cp->geom, geom) {
537 LIST_FOREACH(pp, &gp->provider, provider) {
538 if (!strcmp(arg, pp->name))
547 g_destroy_provider(struct g_provider *pp)
552 G_VALID_PROVIDER(pp);
553 KASSERT(LIST_EMPTY(&pp->consumers),
554 ("g_destroy_provider but attached"));
555 KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
556 KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
557 KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
559 LIST_REMOVE(pp, provider);
561 devstat_remove_entry(pp->stat);
563 if ((gp->flags & G_GEOM_WITHER))
568 * We keep the "geoms" list sorted by topological order (== increasing
569 * numerical rank) at all times.
570 * When an attach is done, the attaching geoms rank is invalidated
571 * and it is moved to the tail of the list.
572 * All geoms later in the sequence has their ranks reevaluated in
573 * sequence. If we cannot assign rank to a geom because it's
574 * prerequisites do not have rank, we move that element to the tail
575 * of the sequence with invalid rank as well.
576 * At some point we encounter our original geom and if we stil fail
577 * to assign it a rank, there must be a loop and we fail back to
578 * g_attach() which detach again and calls redo_rank again
579 * to fix up the damage.
580 * It would be much simpler code wise to do it recursively, but we
581 * can't risk that on the kernel stack.
585 redo_rank(struct g_geom *gp)
587 struct g_consumer *cp;
588 struct g_geom *gp1, *gp2;
594 /* Invalidate this geoms rank and move it to the tail */
595 gp1 = TAILQ_NEXT(gp, geoms);
598 TAILQ_REMOVE(&geoms, gp, geoms);
599 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
604 /* re-rank the rest of the sequence */
605 for (; gp1 != NULL; gp1 = gp2) {
608 LIST_FOREACH(cp, &gp1->consumer, consumer) {
609 if (cp->provider == NULL)
611 n = cp->provider->geom->rank;
619 gp2 = TAILQ_NEXT(gp1, geoms);
621 /* got a rank, moving on */
625 /* no rank to original geom means loop */
629 /* no rank, put it at the end move on */
630 TAILQ_REMOVE(&geoms, gp1, geoms);
631 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
637 g_attach(struct g_consumer *cp, struct g_provider *pp)
642 G_VALID_CONSUMER(cp);
643 G_VALID_PROVIDER(pp);
644 KASSERT(cp->provider == NULL, ("attach but attached"));
646 LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
647 error = redo_rank(cp->geom);
649 LIST_REMOVE(cp, consumers);
657 g_detach(struct g_consumer *cp)
659 struct g_provider *pp;
662 G_VALID_CONSUMER(cp);
663 g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
664 KASSERT(cp->provider != NULL, ("detach but not attached"));
665 KASSERT(cp->acr == 0, ("detach but nonzero acr"));
666 KASSERT(cp->acw == 0, ("detach but nonzero acw"));
667 KASSERT(cp->ace == 0, ("detach but nonzero ace"));
668 KASSERT(cp->nstart == cp->nend,
669 ("detach with active requests"));
671 LIST_REMOVE(cp, consumers);
673 if (pp->geom->flags & G_GEOM_WITHER)
675 else if (pp->flags & G_PF_WITHER)
683 * Access-check with delta values. The question asked is "can provider
684 * "cp" change the access counters by the relative amounts dc[rwe] ?"
688 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
690 struct g_provider *pp;
695 G_VALID_CONSUMER(cp);
697 KASSERT(pp != NULL, ("access but not attached"));
698 G_VALID_PROVIDER(pp);
700 g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
701 cp, pp->name, dcr, dcw, dce);
703 KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
704 KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
705 KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
706 KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
707 KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
710 * If our class cares about being spoiled, and we have been, we
711 * are probably just ahead of the event telling us that. Fail
712 * now rather than having to unravel this later.
714 if (cp->geom->spoiled != NULL && cp->spoiled &&
715 (dcr > 0 || dcw > 0 || dce > 0))
719 * Figure out what counts the provider would have had, if this
720 * consumer had (r0w0e0) at this time.
722 pr = pp->acr - cp->acr;
723 pw = pp->acw - cp->acw;
724 pe = pp->ace - cp->ace;
727 "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
729 cp->acr, cp->acw, cp->ace,
730 pp->acr, pp->acw, pp->ace,
733 /* If foot-shooting is enabled, any open on rank#1 is OK */
734 if ((g_debugflags & 16) && pp->geom->rank == 1)
736 /* If we try exclusive but already write: fail */
737 else if (dce > 0 && pw > 0)
739 /* If we try write but already exclusive: fail */
740 else if (dcw > 0 && pe > 0)
742 /* If we try to open more but provider is error'ed: fail */
743 else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
748 error = pp->geom->access(pp, dcr, dcw, dce);
749 KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
750 ("Geom provider %s::%s failed closing ->access()",
751 pp->geom->class->name, pp->name));
754 * If we open first write, spoil any partner consumers.
755 * If we close last write and provider is not errored,
758 if (pp->acw == 0 && dcw != 0)
760 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
761 !(pp->geom->flags & G_GEOM_WITHER))
762 g_post_event(g_new_provider_event, pp, M_WAITOK,
771 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
772 KASSERT(pp->sectorsize > 0,
773 ("Provider %s lacks sectorsize", pp->name));
779 g_handleattr_int(struct bio *bp, const char *attribute, int val)
782 return (g_handleattr(bp, attribute, &val, sizeof val));
786 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
789 return (g_handleattr(bp, attribute, &val, sizeof val));
793 g_handleattr_str(struct bio *bp, const char *attribute, char *str)
796 return (g_handleattr(bp, attribute, str, 0));
800 g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
804 if (strcmp(bp->bio_attribute, attribute))
807 bzero(bp->bio_data, bp->bio_length);
808 if (strlcpy(bp->bio_data, val, bp->bio_length) >=
810 printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
811 __func__, bp->bio_to->name,
812 (intmax_t)bp->bio_length, strlen(val));
815 } else if (bp->bio_length == len) {
816 bcopy(val, bp->bio_data, len);
817 bp->bio_completed = len;
819 printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
820 bp->bio_to->name, (intmax_t)bp->bio_length, len);
823 g_io_deliver(bp, error);
828 g_std_access(struct g_provider *pp,
829 int dr __unused, int dw __unused, int de __unused)
833 G_VALID_PROVIDER(pp);
838 g_std_done(struct bio *bp)
842 bp2 = bp->bio_parent;
843 if (bp2->bio_error == 0)
844 bp2->bio_error = bp->bio_error;
845 bp2->bio_completed += bp->bio_completed;
848 if (bp2->bio_children == bp2->bio_inbed)
849 g_io_deliver(bp2, bp2->bio_error);
852 /* XXX: maybe this is only g_slice_spoiled */
855 g_std_spoiled(struct g_consumer *cp)
858 struct g_provider *pp;
861 G_VALID_CONSUMER(cp);
862 g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
865 LIST_FOREACH(pp, &gp->provider, provider)
866 g_orphan_provider(pp, ENXIO);
867 g_destroy_consumer(cp);
868 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
871 gp->flags |= G_GEOM_WITHER;
875 * Spoiling happens when a provider is opened for writing, but consumers
876 * which are configured by in-band data are attached (slicers for instance).
877 * Since the write might potentially change the in-band data, such consumers
878 * need to re-evaluate their existence after the writing session closes.
879 * We do this by (offering to) tear them down when the open for write happens
880 * in return for a re-taste when it closes again.
881 * Together with the fact that such consumers grab an 'e' bit whenever they
882 * are open, regardless of mode, this ends up DTRT.
886 g_spoil_event(void *arg, int flag)
888 struct g_provider *pp;
889 struct g_consumer *cp, *cp2;
892 if (flag == EV_CANCEL)
895 G_VALID_PROVIDER(pp);
896 for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
897 cp2 = LIST_NEXT(cp, consumers);
901 if (cp->geom->spoiled == NULL)
903 cp->geom->spoiled(cp);
909 g_spoil(struct g_provider *pp, struct g_consumer *cp)
911 struct g_consumer *cp2;
914 G_VALID_PROVIDER(pp);
915 G_VALID_CONSUMER(cp);
917 LIST_FOREACH(cp2, &pp->consumers, consumers) {
921 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
922 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
924 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
927 g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
931 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
936 error = g_io_getattr(attr, cp, &i, var);
944 #if defined(DIAGNOSTIC) || defined(DDB)
946 * This function walks (topologically unsafely) the mesh and return a
947 * non-zero integer if it finds the argument pointer is an object.
948 * The return value indicates which type of object it is belived to be.
949 * If topology is not locked, this function is potentially dangerous,
950 * but since it is for debugging purposes and can be useful for instance
951 * from DDB, we do not assert topology lock is held.
954 g_valid_obj(void const *ptr)
958 struct g_consumer *cp;
959 struct g_provider *pp;
961 LIST_FOREACH(mp, &g_classes, class) {
964 LIST_FOREACH(gp, &mp->geom, geom) {
967 LIST_FOREACH(cp, &gp->consumer, consumer)
970 LIST_FOREACH(pp, &gp->provider, provider)
981 #define gprintf(...) do { \
982 printf("%*s", indent, ""); \
983 printf(__VA_ARGS__); \
985 #define gprintln(...) do { \
986 gprintf(__VA_ARGS__); \
990 #define ADDFLAG(obj, flag, sflag) do { \
991 if ((obj)->flags & (flag)) { \
993 strlcat(str, ",", size); \
994 strlcat(str, (sflag), size); \
1000 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1005 if (pp->flags == 0) {
1006 strlcpy(str, "NONE", size);
1009 ADDFLAG(pp, G_PF_CANDELETE, "G_PF_CANDELETE");
1010 ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1011 ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1016 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1021 if (gp->flags == 0) {
1022 strlcpy(str, "NONE", size);
1025 ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1029 db_show_geom_consumer(int indent, struct g_consumer *cp)
1033 gprintln("consumer: %p", cp);
1034 gprintln(" class: %s (%p)", cp->geom->class->name,
1036 gprintln(" geom: %s (%p)", cp->geom->name, cp->geom);
1037 if (cp->provider == NULL)
1038 gprintln(" provider: none");
1040 gprintln(" provider: %s (%p)", cp->provider->name,
1043 gprintln(" access: r%dw%de%d", cp->acr, cp->acw, cp->ace);
1044 gprintln(" spoiled: %d", cp->spoiled);
1045 gprintln(" nstart: %u", cp->nstart);
1046 gprintln(" nend: %u", cp->nend);
1048 gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1049 cp->provider != NULL ? cp->provider->name : "none",
1050 cp->acr, cp->acw, cp->ace);
1052 printf(", spoiled=%d", cp->spoiled);
1058 db_show_geom_provider(int indent, struct g_provider *pp)
1060 struct g_consumer *cp;
1064 gprintln("provider: %s (%p)", pp->name, pp);
1065 gprintln(" class: %s (%p)", pp->geom->class->name,
1067 gprintln(" geom: %s (%p)", pp->geom->name, pp->geom);
1068 gprintln(" mediasize: %jd", (intmax_t)pp->mediasize);
1069 gprintln(" sectorsize: %u", pp->sectorsize);
1070 gprintln(" stripesize: %u", pp->stripesize);
1071 gprintln(" stripeoffset: %u", pp->stripeoffset);
1072 gprintln(" access: r%dw%de%d", pp->acr, pp->acw,
1074 gprintln(" flags: %s (0x%04x)",
1075 provider_flags_to_string(pp, flags, sizeof(flags)),
1077 gprintln(" error: %d", pp->error);
1078 gprintln(" nstart: %u", pp->nstart);
1079 gprintln(" nend: %u", pp->nend);
1080 if (LIST_EMPTY(&pp->consumers))
1081 gprintln(" consumers: none");
1083 gprintf("provider: %s (%p), access=r%dw%de%d",
1084 pp->name, pp, pp->acr, pp->acw, pp->ace);
1085 if (pp->flags != 0) {
1086 printf(", flags=%s (0x%04x)",
1087 provider_flags_to_string(pp, flags, sizeof(flags)),
1092 if (!LIST_EMPTY(&pp->consumers)) {
1093 LIST_FOREACH(cp, &pp->consumers, consumers)
1094 db_show_geom_consumer(indent + 2, cp);
1099 db_show_geom_geom(int indent, struct g_geom *gp)
1101 struct g_provider *pp;
1102 struct g_consumer *cp;
1106 gprintln("geom: %s (%p)", gp->name, gp);
1107 gprintln(" class: %s (%p)", gp->class->name, gp->class);
1108 gprintln(" flags: %s (0x%04x)",
1109 geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1110 gprintln(" rank: %d", gp->rank);
1111 if (LIST_EMPTY(&gp->provider))
1112 gprintln(" providers: none");
1113 if (LIST_EMPTY(&gp->consumer))
1114 gprintln(" consumers: none");
1116 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1117 if (gp->flags != 0) {
1118 printf(", flags=%s (0x%04x)",
1119 geom_flags_to_string(gp, flags, sizeof(flags)),
1124 if (!LIST_EMPTY(&gp->provider)) {
1125 LIST_FOREACH(pp, &gp->provider, provider)
1126 db_show_geom_provider(indent + 2, pp);
1128 if (!LIST_EMPTY(&gp->consumer)) {
1129 LIST_FOREACH(cp, &gp->consumer, consumer)
1130 db_show_geom_consumer(indent + 2, cp);
1135 db_show_geom_class(struct g_class *mp)
1139 printf("class: %s (%p)\n", mp->name, mp);
1140 LIST_FOREACH(gp, &mp->geom, geom)
1141 db_show_geom_geom(2, gp);
1145 * Print the GEOM topology or the given object.
1147 DB_SHOW_COMMAND(geom, db_show_geom)
1152 /* No address given, print the entire topology. */
1153 LIST_FOREACH(mp, &g_classes, class) {
1154 db_show_geom_class(mp);
1158 switch (g_valid_obj((void *)addr)) {
1160 db_show_geom_class((struct g_class *)addr);
1163 db_show_geom_geom(0, (struct g_geom *)addr);
1166 db_show_geom_consumer(0, (struct g_consumer *)addr);
1169 db_show_geom_provider(0, (struct g_provider *)addr);
1172 printf("Not a GEOM object.\n");