xref: /freebsd/sys/geom/geom_dump.c (revision 87384c51e047349fdbcf4f07fe52d900ce108040)
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  * Copyright (c) 2013-2022 Alexander Motin <mav@FreeBSD.org>
8  *
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.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
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
24  *    permission.
25  *
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
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/sbuf.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <machine/stdarg.h>
47 
48 #include <geom/geom.h>
49 #include <geom/geom_int.h>
50 #include <geom/geom_disk.h>
51 
52 static void
53 g_confdot_consumer(struct sbuf *sb, struct g_consumer *cp)
54 {
55 
56 	sbuf_printf(sb, "z%p [label=\"r%dw%de%d\"];\n",
57 	    cp, cp->acr, cp->acw, cp->ace);
58 	if (cp->provider)
59 		sbuf_printf(sb, "z%p -> z%p;\n", cp, cp->provider);
60 }
61 
62 static void
63 g_confdot_provider(struct sbuf *sb, struct g_provider *pp)
64 {
65 
66 	sbuf_printf(sb, "z%p [shape=hexagon,label=\"%s\\nr%dw%de%d\\nerr#%d\\n"
67 	    "sector=%u\\nstripe=%ju\"];\n", pp, pp->name, pp->acr, pp->acw,
68 	    pp->ace, pp->error, pp->sectorsize, (uintmax_t)pp->stripesize);
69 }
70 
71 static void
72 g_confdot_geom(struct sbuf *sb, struct g_geom *gp)
73 {
74 	struct g_consumer *cp;
75 	struct g_provider *pp;
76 
77 	sbuf_printf(sb, "z%p [shape=box,label=\"%s\\n%s\\nr#%d\"];\n",
78 	    gp, gp->class->name, gp->name, gp->rank);
79 	LIST_FOREACH(cp, &gp->consumer, consumer) {
80 		g_confdot_consumer(sb, cp);
81 		sbuf_printf(sb, "z%p -> z%p;\n", gp, cp);
82 	}
83 
84 	LIST_FOREACH(pp, &gp->provider, provider) {
85 		g_confdot_provider(sb, pp);
86 		sbuf_printf(sb, "z%p -> z%p;\n", pp, gp);
87 	}
88 }
89 
90 static void
91 g_confdot_class(struct sbuf *sb, struct g_class *mp)
92 {
93 	struct g_geom *gp;
94 
95 	LIST_FOREACH(gp, &mp->geom, geom)
96 		g_confdot_geom(sb, gp);
97 }
98 
99 void
100 g_confdot(void *p, int flag )
101 {
102 	struct g_class *mp;
103 	struct sbuf *sb;
104 
105 	KASSERT(flag != EV_CANCEL, ("g_confdot was cancelled"));
106 	sb = p;
107 	g_topology_assert();
108 	sbuf_cat(sb, "digraph geom {\n");
109 	LIST_FOREACH(mp, &g_classes, class)
110 		g_confdot_class(sb, mp);
111 	sbuf_cat(sb, "}\n");
112 	sbuf_finish(sb);
113 }
114 
115 static void
116 g_conftxt_geom(struct sbuf *sb, struct g_geom *gp, int level)
117 {
118 	struct g_provider *pp;
119 	struct g_consumer *cp;
120 
121 	if (gp->flags & G_GEOM_WITHER)
122 		return;
123 	LIST_FOREACH(pp, &gp->provider, provider) {
124 		sbuf_printf(sb, "%d %s %s %ju %u", level, gp->class->name,
125 		    pp->name, (uintmax_t)pp->mediasize, pp->sectorsize);
126 		if (gp->dumpconf != NULL)
127 			gp->dumpconf(sb, NULL, gp, NULL, pp);
128 		sbuf_cat(sb, "\n");
129 		LIST_FOREACH(cp, &pp->consumers, consumers)
130 			g_conftxt_geom(sb, cp->geom, level + 1);
131 	}
132 }
133 
134 static void
135 g_conftxt_class(struct sbuf *sb, struct g_class *mp)
136 {
137 	struct g_geom *gp;
138 
139 	LIST_FOREACH(gp, &mp->geom, geom)
140 		g_conftxt_geom(sb, gp, 0);
141 }
142 
143 void
144 g_conftxt(void *p, int flag)
145 {
146 	struct g_class *mp;
147 	struct sbuf *sb;
148 
149 	KASSERT(flag != EV_CANCEL, ("g_conftxt was cancelled"));
150 	sb = p;
151 	g_topology_assert();
152 	LIST_FOREACH(mp, &g_classes, class) {
153 		if (!strcmp(mp->name, G_DISK_CLASS_NAME) || !strcmp(mp->name, "MD"))
154 			g_conftxt_class(sb, mp);
155 	}
156 	sbuf_finish(sb);
157 }
158 
159 void
160 g_conf_cat_escaped(struct sbuf *sb, const char *buf)
161 {
162 	const u_char *c;
163 
164 	for (c = buf; *c != '\0'; c++) {
165 		if (*c == '&' || *c == '<' || *c == '>' ||
166 		    *c == '\'' || *c == '"' || *c > 0x7e)
167 			sbuf_printf(sb, "&#x%X;", *c);
168 		else if (*c == '\t' || *c == '\n' || *c == '\r' || *c > 0x1f)
169 			sbuf_putc(sb, *c);
170 		else
171 			sbuf_putc(sb, '?');
172 	}
173 }
174 
175 void
176 g_conf_printf_escaped(struct sbuf *sb, const char *fmt, ...)
177 {
178 	struct sbuf *s;
179 	va_list ap;
180 
181 	s = sbuf_new_auto();
182 	va_start(ap, fmt);
183 	sbuf_vprintf(s, fmt, ap);
184 	va_end(ap);
185 	sbuf_finish(s);
186 
187 	g_conf_cat_escaped(sb, sbuf_data(s));
188 	sbuf_delete(s);
189 }
190 
191 static void
192 g_conf_consumer(struct sbuf *sb, struct g_consumer *cp)
193 {
194 
195 	sbuf_printf(sb, "\t<consumer id=\"%p\">\n", cp);
196 	sbuf_printf(sb, "\t  <geom ref=\"%p\"/>\n", cp->geom);
197 	if (cp->provider != NULL)
198 		sbuf_printf(sb, "\t  <provider ref=\"%p\"/>\n", cp->provider);
199 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
200 	    cp->acr, cp->acw, cp->ace);
201 	if (cp->geom->flags & G_GEOM_WITHER)
202 		;
203 	else if (cp->geom->dumpconf != NULL) {
204 		sbuf_cat(sb, "\t  <config>\n");
205 		cp->geom->dumpconf(sb, "\t    ", cp->geom, cp, NULL);
206 		sbuf_cat(sb, "\t  </config>\n");
207 	}
208 	sbuf_cat(sb, "\t</consumer>\n");
209 }
210 
211 static void
212 g_conf_provider(struct sbuf *sb, struct g_provider *pp)
213 {
214 	struct g_geom_alias *gap;
215 
216 	sbuf_printf(sb, "\t<provider id=\"%p\">\n", pp);
217 	sbuf_printf(sb, "\t  <geom ref=\"%p\"/>\n", pp->geom);
218 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
219 	    pp->acr, pp->acw, pp->ace);
220 	sbuf_cat(sb, "\t  <name>");
221 	g_conf_cat_escaped(sb, pp->name);
222 	sbuf_cat(sb, "</name>\n");
223 	LIST_FOREACH(gap, &pp->aliases, ga_next) {
224 		sbuf_cat(sb, "\t  <alias>");
225 		g_conf_cat_escaped(sb, gap->ga_alias);
226 		sbuf_cat(sb, "</alias>\n");
227 	}
228 	sbuf_printf(sb, "\t  <mediasize>%jd</mediasize>\n",
229 	    (intmax_t)pp->mediasize);
230 	sbuf_printf(sb, "\t  <sectorsize>%u</sectorsize>\n", pp->sectorsize);
231 	sbuf_printf(sb, "\t  <stripesize>%ju</stripesize>\n", (uintmax_t)pp->stripesize);
232 	sbuf_printf(sb, "\t  <stripeoffset>%ju</stripeoffset>\n", (uintmax_t)pp->stripeoffset);
233 	if (pp->flags & G_PF_WITHER)
234 		sbuf_cat(sb, "\t  <wither/>\n");
235 	else if (pp->geom->flags & G_GEOM_WITHER)
236 		;
237 	else if (pp->geom->dumpconf != NULL) {
238 		sbuf_cat(sb, "\t  <config>\n");
239 		pp->geom->dumpconf(sb, "\t    ", pp->geom, NULL, pp);
240 		sbuf_cat(sb, "\t  </config>\n");
241 	}
242 	sbuf_cat(sb, "\t</provider>\n");
243 }
244 
245 static void
246 g_conf_geom(struct sbuf *sb, struct g_geom *gp)
247 {
248 	struct g_consumer *cp;
249 	struct g_provider *pp;
250 
251 	sbuf_printf(sb, "    <geom id=\"%p\">\n", gp);
252 	sbuf_printf(sb, "      <class ref=\"%p\"/>\n", gp->class);
253 	sbuf_cat(sb, "      <name>");
254 	g_conf_cat_escaped(sb, gp->name);
255 	sbuf_cat(sb, "</name>\n");
256 	sbuf_printf(sb, "      <rank>%d</rank>\n", gp->rank);
257 	if (gp->flags & G_GEOM_WITHER)
258 		sbuf_cat(sb, "      <wither/>\n");
259 	else if (gp->dumpconf != NULL) {
260 		sbuf_cat(sb, "      <config>\n");
261 		gp->dumpconf(sb, "\t", gp, NULL, NULL);
262 		sbuf_cat(sb, "      </config>\n");
263 	}
264 	LIST_FOREACH(cp, &gp->consumer, consumer)
265 		g_conf_consumer(sb, cp);
266 	LIST_FOREACH(pp, &gp->provider, provider)
267 		g_conf_provider(sb, pp);
268 	sbuf_cat(sb, "    </geom>\n");
269 }
270 
271 static bool
272 g_conf_matchgp(struct g_geom *gp, struct g_geom **gps)
273 {
274 
275 	if (gps == NULL)
276 		return (true);
277 	for (; *gps != NULL; gps++) {
278 		if (*gps == gp)
279 			return (true);
280 	}
281 	return (false);
282 }
283 
284 static void
285 g_conf_class(struct sbuf *sb, struct g_class *mp, struct g_geom **gps)
286 {
287 	struct g_geom *gp;
288 
289 	sbuf_printf(sb, "  <class id=\"%p\">\n", mp);
290 	sbuf_cat(sb, "    <name>");
291 	g_conf_cat_escaped(sb, mp->name);
292 	sbuf_cat(sb, "</name>\n");
293 	LIST_FOREACH(gp, &mp->geom, geom) {
294 		if (!g_conf_matchgp(gp, gps))
295 			continue;
296 		g_conf_geom(sb, gp);
297 		if (sbuf_error(sb))
298 			break;
299 	}
300 	sbuf_cat(sb, "  </class>\n");
301 }
302 
303 void
304 g_conf_specific(struct sbuf *sb, struct g_geom **gps)
305 {
306 	struct g_class *mp2;
307 
308 	g_topology_assert();
309 	sbuf_cat(sb, "<mesh>\n");
310 	LIST_FOREACH(mp2, &g_classes, class) {
311 		g_conf_class(sb, mp2, gps);
312 		if (sbuf_error(sb))
313 			break;
314 	}
315 	sbuf_cat(sb, "</mesh>\n");
316 	sbuf_finish(sb);
317 }
318 
319 void
320 g_confxml(void *p, int flag)
321 {
322 
323 	KASSERT(flag != EV_CANCEL, ("g_confxml was cancelled"));
324 	g_topology_assert();
325 	g_conf_specific(p, NULL);
326 }
327 
328 void
329 (g_trace)(int level, const char *fmt, ...)
330 {
331 	va_list ap;
332 
333 	if (!(g_debugflags & level))
334 		return;
335 	va_start(ap, fmt);
336 	vprintf(fmt, ap);
337 	va_end(ap);
338 	printf("\n");
339 }
340