2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
7 * Copyright (c) 2013-2022 Alexander Motin <mav@FreeBSD.org>
9 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
10 * and NAI Labs, the Security Research Division of Network Associates, Inc.
11 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
12 * DARPA CHATS research program.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. The names of the authors may not be used to endorse or promote
23 * products derived from this software without specific prior written
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <machine/stdarg.h>
45 #include <geom/geom.h>
46 #include <geom/geom_int.h>
47 #include <geom/geom_disk.h>
50 g_confdot_consumer(struct sbuf *sb, struct g_consumer *cp)
53 sbuf_printf(sb, "z%p [label=\"r%dw%de%d\"];\n",
54 cp, cp->acr, cp->acw, cp->ace);
56 sbuf_printf(sb, "z%p -> z%p;\n", cp, cp->provider);
60 g_confdot_provider(struct sbuf *sb, struct g_provider *pp)
63 sbuf_printf(sb, "z%p [shape=hexagon,label=\"%s\\nr%dw%de%d\\nerr#%d\\n"
64 "sector=%u\\nstripe=%ju\"];\n", pp, pp->name, pp->acr, pp->acw,
65 pp->ace, pp->error, pp->sectorsize, (uintmax_t)pp->stripesize);
69 g_confdot_geom(struct sbuf *sb, struct g_geom *gp)
71 struct g_consumer *cp;
72 struct g_provider *pp;
74 sbuf_printf(sb, "z%p [shape=box,label=\"%s\\n%s\\nr#%d\"];\n",
75 gp, gp->class->name, gp->name, gp->rank);
76 LIST_FOREACH(cp, &gp->consumer, consumer) {
77 g_confdot_consumer(sb, cp);
78 sbuf_printf(sb, "z%p -> z%p;\n", gp, cp);
81 LIST_FOREACH(pp, &gp->provider, provider) {
82 g_confdot_provider(sb, pp);
83 sbuf_printf(sb, "z%p -> z%p;\n", pp, gp);
88 g_confdot_class(struct sbuf *sb, struct g_class *mp)
92 LIST_FOREACH(gp, &mp->geom, geom)
93 g_confdot_geom(sb, gp);
97 g_confdot(void *p, int flag )
102 KASSERT(flag != EV_CANCEL, ("g_confdot was cancelled"));
105 sbuf_cat(sb, "digraph geom {\n");
106 LIST_FOREACH(mp, &g_classes, class)
107 g_confdot_class(sb, mp);
113 g_conftxt_geom(struct sbuf *sb, struct g_geom *gp, int level)
115 struct g_provider *pp;
116 struct g_consumer *cp;
118 if (gp->flags & G_GEOM_WITHER)
120 LIST_FOREACH(pp, &gp->provider, provider) {
121 sbuf_printf(sb, "%d %s %s %ju %u", level, gp->class->name,
122 pp->name, (uintmax_t)pp->mediasize, pp->sectorsize);
123 if (gp->dumpconf != NULL)
124 gp->dumpconf(sb, NULL, gp, NULL, pp);
126 LIST_FOREACH(cp, &pp->consumers, consumers)
127 g_conftxt_geom(sb, cp->geom, level + 1);
132 g_conftxt_class(struct sbuf *sb, struct g_class *mp)
136 LIST_FOREACH(gp, &mp->geom, geom)
137 g_conftxt_geom(sb, gp, 0);
141 g_conftxt(void *p, int flag)
146 KASSERT(flag != EV_CANCEL, ("g_conftxt was cancelled"));
149 LIST_FOREACH(mp, &g_classes, class) {
150 if (!strcmp(mp->name, G_DISK_CLASS_NAME) || !strcmp(mp->name, "MD"))
151 g_conftxt_class(sb, mp);
157 g_conf_cat_escaped(struct sbuf *sb, const char *buf)
161 for (c = buf; *c != '\0'; c++) {
162 if (*c == '&' || *c == '<' || *c == '>' ||
163 *c == '\'' || *c == '"' || *c > 0x7e)
164 sbuf_printf(sb, "&#x%X;", *c);
165 else if (*c == '\t' || *c == '\n' || *c == '\r' || *c > 0x1f)
173 g_conf_printf_escaped(struct sbuf *sb, const char *fmt, ...)
180 sbuf_vprintf(s, fmt, ap);
184 g_conf_cat_escaped(sb, sbuf_data(s));
189 g_conf_consumer(struct sbuf *sb, struct g_consumer *cp)
192 sbuf_printf(sb, "\t<consumer id=\"%p\">\n", cp);
193 sbuf_printf(sb, "\t <geom ref=\"%p\"/>\n", cp->geom);
194 if (cp->provider != NULL)
195 sbuf_printf(sb, "\t <provider ref=\"%p\"/>\n", cp->provider);
196 sbuf_printf(sb, "\t <mode>r%dw%de%d</mode>\n",
197 cp->acr, cp->acw, cp->ace);
198 if (cp->geom->flags & G_GEOM_WITHER)
200 else if (cp->geom->dumpconf != NULL) {
201 sbuf_cat(sb, "\t <config>\n");
202 cp->geom->dumpconf(sb, "\t ", cp->geom, cp, NULL);
203 sbuf_cat(sb, "\t </config>\n");
205 sbuf_cat(sb, "\t</consumer>\n");
209 g_conf_provider(struct sbuf *sb, struct g_provider *pp)
211 struct g_geom_alias *gap;
213 sbuf_printf(sb, "\t<provider id=\"%p\">\n", pp);
214 sbuf_printf(sb, "\t <geom ref=\"%p\"/>\n", pp->geom);
215 sbuf_printf(sb, "\t <mode>r%dw%de%d</mode>\n",
216 pp->acr, pp->acw, pp->ace);
217 sbuf_cat(sb, "\t <name>");
218 g_conf_cat_escaped(sb, pp->name);
219 sbuf_cat(sb, "</name>\n");
220 LIST_FOREACH(gap, &pp->aliases, ga_next) {
221 sbuf_cat(sb, "\t <alias>");
222 g_conf_cat_escaped(sb, gap->ga_alias);
223 sbuf_cat(sb, "</alias>\n");
225 sbuf_printf(sb, "\t <mediasize>%jd</mediasize>\n",
226 (intmax_t)pp->mediasize);
227 sbuf_printf(sb, "\t <sectorsize>%u</sectorsize>\n", pp->sectorsize);
228 sbuf_printf(sb, "\t <stripesize>%ju</stripesize>\n", (uintmax_t)pp->stripesize);
229 sbuf_printf(sb, "\t <stripeoffset>%ju</stripeoffset>\n", (uintmax_t)pp->stripeoffset);
230 if (pp->flags & G_PF_WITHER)
231 sbuf_cat(sb, "\t <wither/>\n");
232 else if (pp->geom->flags & G_GEOM_WITHER)
234 else if (pp->geom->dumpconf != NULL) {
235 sbuf_cat(sb, "\t <config>\n");
236 pp->geom->dumpconf(sb, "\t ", pp->geom, NULL, pp);
237 sbuf_cat(sb, "\t </config>\n");
239 sbuf_cat(sb, "\t</provider>\n");
243 g_conf_geom(struct sbuf *sb, struct g_geom *gp)
245 struct g_consumer *cp;
246 struct g_provider *pp;
248 sbuf_printf(sb, " <geom id=\"%p\">\n", gp);
249 sbuf_printf(sb, " <class ref=\"%p\"/>\n", gp->class);
250 sbuf_cat(sb, " <name>");
251 g_conf_cat_escaped(sb, gp->name);
252 sbuf_cat(sb, "</name>\n");
253 sbuf_printf(sb, " <rank>%d</rank>\n", gp->rank);
254 if (gp->flags & G_GEOM_WITHER)
255 sbuf_cat(sb, " <wither/>\n");
256 else if (gp->dumpconf != NULL) {
257 sbuf_cat(sb, " <config>\n");
258 gp->dumpconf(sb, "\t", gp, NULL, NULL);
259 sbuf_cat(sb, " </config>\n");
261 LIST_FOREACH(cp, &gp->consumer, consumer)
262 g_conf_consumer(sb, cp);
263 LIST_FOREACH(pp, &gp->provider, provider)
264 g_conf_provider(sb, pp);
265 sbuf_cat(sb, " </geom>\n");
269 g_conf_matchgp(struct g_geom *gp, struct g_geom **gps)
274 for (; *gps != NULL; gps++) {
282 g_conf_class(struct sbuf *sb, struct g_class *mp, struct g_geom **gps)
286 sbuf_printf(sb, " <class id=\"%p\">\n", mp);
287 sbuf_cat(sb, " <name>");
288 g_conf_cat_escaped(sb, mp->name);
289 sbuf_cat(sb, "</name>\n");
290 LIST_FOREACH(gp, &mp->geom, geom) {
291 if (!g_conf_matchgp(gp, gps))
297 sbuf_cat(sb, " </class>\n");
301 g_conf_specific(struct sbuf *sb, struct g_geom **gps)
306 sbuf_cat(sb, "<mesh>\n");
307 LIST_FOREACH(mp2, &g_classes, class) {
308 g_conf_class(sb, mp2, gps);
312 sbuf_cat(sb, "</mesh>\n");
317 g_confxml(void *p, int flag)
320 KASSERT(flag != EV_CANCEL, ("g_confxml was cancelled"));
322 g_conf_specific(p, NULL);
326 (g_trace)(int level, const char *fmt, ...)
330 if (!(g_debugflags & level))