xref: /freebsd/sys/geom/geom_dump.c (revision 1603881667360c015f6685131f2f25474fa67a72)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/sbuf.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <machine/stdarg.h>
46 
47 #include <geom/geom.h>
48 #include <geom/geom_int.h>
49 #include <geom/geom_disk.h>
50 
51 static void
52 g_confdot_consumer(struct sbuf *sb, struct g_consumer *cp)
53 {
54 
55 	sbuf_printf(sb, "z%p [label=\"r%dw%de%d\"];\n",
56 	    cp, cp->acr, cp->acw, cp->ace);
57 	if (cp->provider)
58 		sbuf_printf(sb, "z%p -> z%p;\n", cp, cp->provider);
59 }
60 
61 static void
62 g_confdot_provider(struct sbuf *sb, struct g_provider *pp)
63 {
64 
65 	sbuf_printf(sb, "z%p [shape=hexagon,label=\"%s\\nr%dw%de%d\\nerr#%d\\n"
66 	    "sector=%u\\nstripe=%ju\"];\n", pp, pp->name, pp->acr, pp->acw,
67 	    pp->ace, pp->error, pp->sectorsize, (uintmax_t)pp->stripesize);
68 }
69 
70 static void
71 g_confdot_geom(struct sbuf *sb, struct g_geom *gp)
72 {
73 	struct g_consumer *cp;
74 	struct g_provider *pp;
75 
76 	sbuf_printf(sb, "z%p [shape=box,label=\"%s\\n%s\\nr#%d\"];\n",
77 	    gp, gp->class->name, gp->name, gp->rank);
78 	LIST_FOREACH(cp, &gp->consumer, consumer) {
79 		g_confdot_consumer(sb, cp);
80 		sbuf_printf(sb, "z%p -> z%p;\n", gp, cp);
81 	}
82 
83 	LIST_FOREACH(pp, &gp->provider, provider) {
84 		g_confdot_provider(sb, pp);
85 		sbuf_printf(sb, "z%p -> z%p;\n", pp, gp);
86 	}
87 }
88 
89 static void
90 g_confdot_class(struct sbuf *sb, struct g_class *mp)
91 {
92 	struct g_geom *gp;
93 
94 	LIST_FOREACH(gp, &mp->geom, geom)
95 		g_confdot_geom(sb, gp);
96 }
97 
98 void
99 g_confdot(void *p, int flag )
100 {
101 	struct g_class *mp;
102 	struct sbuf *sb;
103 
104 	KASSERT(flag != EV_CANCEL, ("g_confdot was cancelled"));
105 	sb = p;
106 	g_topology_assert();
107 	sbuf_cat(sb, "digraph geom {\n");
108 	LIST_FOREACH(mp, &g_classes, class)
109 		g_confdot_class(sb, mp);
110 	sbuf_cat(sb, "}\n");
111 	sbuf_finish(sb);
112 }
113 
114 static void
115 g_conftxt_geom(struct sbuf *sb, struct g_geom *gp, int level)
116 {
117 	struct g_provider *pp;
118 	struct g_consumer *cp;
119 
120 	if (gp->flags & G_GEOM_WITHER)
121 		return;
122 	LIST_FOREACH(pp, &gp->provider, provider) {
123 		sbuf_printf(sb, "%d %s %s %ju %u", level, gp->class->name,
124 		    pp->name, (uintmax_t)pp->mediasize, pp->sectorsize);
125 		if (gp->dumpconf != NULL)
126 			gp->dumpconf(sb, NULL, gp, NULL, pp);
127 		sbuf_cat(sb, "\n");
128 		LIST_FOREACH(cp, &pp->consumers, consumers)
129 			g_conftxt_geom(sb, cp->geom, level + 1);
130 	}
131 }
132 
133 static void
134 g_conftxt_class(struct sbuf *sb, struct g_class *mp)
135 {
136 	struct g_geom *gp;
137 
138 	LIST_FOREACH(gp, &mp->geom, geom)
139 		g_conftxt_geom(sb, gp, 0);
140 }
141 
142 void
143 g_conftxt(void *p, int flag)
144 {
145 	struct g_class *mp;
146 	struct sbuf *sb;
147 
148 	KASSERT(flag != EV_CANCEL, ("g_conftxt was cancelled"));
149 	sb = p;
150 	g_topology_assert();
151 	LIST_FOREACH(mp, &g_classes, class) {
152 		if (!strcmp(mp->name, G_DISK_CLASS_NAME) || !strcmp(mp->name, "MD"))
153 			g_conftxt_class(sb, mp);
154 	}
155 	sbuf_finish(sb);
156 }
157 
158 void
159 g_conf_cat_escaped(struct sbuf *sb, const char *buf)
160 {
161 	const u_char *c;
162 
163 	for (c = buf; *c != '\0'; c++) {
164 		if (*c == '&' || *c == '<' || *c == '>' ||
165 		    *c == '\'' || *c == '"' || *c > 0x7e)
166 			sbuf_printf(sb, "&#x%X;", *c);
167 		else if (*c == '\t' || *c == '\n' || *c == '\r' || *c > 0x1f)
168 			sbuf_putc(sb, *c);
169 		else
170 			sbuf_putc(sb, '?');
171 	}
172 }
173 
174 void
175 g_conf_printf_escaped(struct sbuf *sb, const char *fmt, ...)
176 {
177 	struct sbuf *s;
178 	va_list ap;
179 
180 	s = sbuf_new_auto();
181 	va_start(ap, fmt);
182 	sbuf_vprintf(s, fmt, ap);
183 	va_end(ap);
184 	sbuf_finish(s);
185 
186 	g_conf_cat_escaped(sb, sbuf_data(s));
187 	sbuf_delete(s);
188 }
189 
190 static void
191 g_conf_consumer(struct sbuf *sb, struct g_consumer *cp)
192 {
193 
194 	sbuf_printf(sb, "\t<consumer id=\"%p\">\n", cp);
195 	sbuf_printf(sb, "\t  <geom ref=\"%p\"/>\n", cp->geom);
196 	if (cp->provider != NULL)
197 		sbuf_printf(sb, "\t  <provider ref=\"%p\"/>\n", cp->provider);
198 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
199 	    cp->acr, cp->acw, cp->ace);
200 	if (cp->geom->flags & G_GEOM_WITHER)
201 		;
202 	else if (cp->geom->dumpconf != NULL) {
203 		sbuf_cat(sb, "\t  <config>\n");
204 		cp->geom->dumpconf(sb, "\t    ", cp->geom, cp, NULL);
205 		sbuf_cat(sb, "\t  </config>\n");
206 	}
207 	sbuf_cat(sb, "\t</consumer>\n");
208 }
209 
210 static void
211 g_conf_provider(struct sbuf *sb, struct g_provider *pp)
212 {
213 	struct g_geom_alias *gap;
214 
215 	sbuf_printf(sb, "\t<provider id=\"%p\">\n", pp);
216 	sbuf_printf(sb, "\t  <geom ref=\"%p\"/>\n", pp->geom);
217 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
218 	    pp->acr, pp->acw, pp->ace);
219 	sbuf_cat(sb, "\t  <name>");
220 	g_conf_cat_escaped(sb, pp->name);
221 	sbuf_cat(sb, "</name>\n");
222 	LIST_FOREACH(gap, &pp->aliases, ga_next) {
223 		sbuf_cat(sb, "\t  <alias>");
224 		g_conf_cat_escaped(sb, gap->ga_alias);
225 		sbuf_cat(sb, "</alias>\n");
226 	}
227 	sbuf_printf(sb, "\t  <mediasize>%jd</mediasize>\n",
228 	    (intmax_t)pp->mediasize);
229 	sbuf_printf(sb, "\t  <sectorsize>%u</sectorsize>\n", pp->sectorsize);
230 	sbuf_printf(sb, "\t  <stripesize>%ju</stripesize>\n", (uintmax_t)pp->stripesize);
231 	sbuf_printf(sb, "\t  <stripeoffset>%ju</stripeoffset>\n", (uintmax_t)pp->stripeoffset);
232 	if (pp->flags & G_PF_WITHER)
233 		sbuf_cat(sb, "\t  <wither/>\n");
234 	else if (pp->geom->flags & G_GEOM_WITHER)
235 		;
236 	else if (pp->geom->dumpconf != NULL) {
237 		sbuf_cat(sb, "\t  <config>\n");
238 		pp->geom->dumpconf(sb, "\t    ", pp->geom, NULL, pp);
239 		sbuf_cat(sb, "\t  </config>\n");
240 	}
241 	sbuf_cat(sb, "\t</provider>\n");
242 }
243 
244 static void
245 g_conf_geom(struct sbuf *sb, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
246 {
247 	struct g_consumer *cp2;
248 	struct g_provider *pp2;
249 
250 	sbuf_printf(sb, "    <geom id=\"%p\">\n", gp);
251 	sbuf_printf(sb, "      <class ref=\"%p\"/>\n", gp->class);
252 	sbuf_cat(sb, "      <name>");
253 	g_conf_cat_escaped(sb, gp->name);
254 	sbuf_cat(sb, "</name>\n");
255 	sbuf_printf(sb, "      <rank>%d</rank>\n", gp->rank);
256 	if (gp->flags & G_GEOM_WITHER)
257 		sbuf_cat(sb, "      <wither/>\n");
258 	else if (gp->dumpconf != NULL) {
259 		sbuf_cat(sb, "      <config>\n");
260 		gp->dumpconf(sb, "\t", gp, NULL, NULL);
261 		sbuf_cat(sb, "      </config>\n");
262 	}
263 	LIST_FOREACH(cp2, &gp->consumer, consumer) {
264 		if (cp != NULL && cp != cp2)
265 			continue;
266 		g_conf_consumer(sb, cp2);
267 	}
268 
269 	LIST_FOREACH(pp2, &gp->provider, provider) {
270 		if (pp != NULL && pp != pp2)
271 			continue;
272 		g_conf_provider(sb, pp2);
273 	}
274 	sbuf_cat(sb, "    </geom>\n");
275 }
276 
277 static void
278 g_conf_class(struct sbuf *sb, struct g_class *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
279 {
280 	struct g_geom *gp2;
281 
282 	sbuf_printf(sb, "  <class id=\"%p\">\n", mp);
283 	sbuf_cat(sb, "    <name>");
284 	g_conf_cat_escaped(sb, mp->name);
285 	sbuf_cat(sb, "</name>\n");
286 	LIST_FOREACH(gp2, &mp->geom, geom) {
287 		if (gp != NULL && gp != gp2)
288 			continue;
289 		g_conf_geom(sb, gp2, pp, cp);
290 	}
291 	sbuf_cat(sb, "  </class>\n");
292 }
293 
294 void
295 g_conf_specific(struct sbuf *sb, struct g_class *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
296 {
297 	struct g_class *mp2;
298 
299 	g_topology_assert();
300 	sbuf_cat(sb, "<mesh>\n");
301 	LIST_FOREACH(mp2, &g_classes, class) {
302 		if (mp != NULL && mp != mp2)
303 			continue;
304 		g_conf_class(sb, mp2, gp, pp, cp);
305 	}
306 	sbuf_cat(sb, "</mesh>\n");
307 	sbuf_finish(sb);
308 }
309 
310 void
311 g_confxml(void *p, int flag)
312 {
313 
314 	KASSERT(flag != EV_CANCEL, ("g_confxml was cancelled"));
315 	g_topology_assert();
316 	g_conf_specific(p, NULL, NULL, NULL, NULL);
317 }
318 
319 void
320 (g_trace)(int level, const char *fmt, ...)
321 {
322 	va_list ap;
323 
324 	if (!(g_debugflags & level))
325 		return;
326 	va_start(ap, fmt);
327 	vprintf(fmt, ap);
328 	va_end(ap);
329 	printf("\n");
330 }
331