xref: /freebsd/sys/geom/geom_dump.c (revision 61e21613)
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/param.h>
40 #include <sys/sbuf.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <machine/stdarg.h>
44 
45 #include <geom/geom.h>
46 #include <geom/geom_int.h>
47 #include <geom/geom_disk.h>
48 
49 static void
50 g_confdot_consumer(struct sbuf *sb, struct g_consumer *cp)
51 {
52 
53 	sbuf_printf(sb, "z%p [label=\"r%dw%de%d\"];\n",
54 	    cp, cp->acr, cp->acw, cp->ace);
55 	if (cp->provider)
56 		sbuf_printf(sb, "z%p -> z%p;\n", cp, cp->provider);
57 }
58 
59 static void
60 g_confdot_provider(struct sbuf *sb, struct g_provider *pp)
61 {
62 
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);
66 }
67 
68 static void
69 g_confdot_geom(struct sbuf *sb, struct g_geom *gp)
70 {
71 	struct g_consumer *cp;
72 	struct g_provider *pp;
73 
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);
79 	}
80 
81 	LIST_FOREACH(pp, &gp->provider, provider) {
82 		g_confdot_provider(sb, pp);
83 		sbuf_printf(sb, "z%p -> z%p;\n", pp, gp);
84 	}
85 }
86 
87 static void
88 g_confdot_class(struct sbuf *sb, struct g_class *mp)
89 {
90 	struct g_geom *gp;
91 
92 	LIST_FOREACH(gp, &mp->geom, geom)
93 		g_confdot_geom(sb, gp);
94 }
95 
96 void
97 g_confdot(void *p, int flag )
98 {
99 	struct g_class *mp;
100 	struct sbuf *sb;
101 
102 	KASSERT(flag != EV_CANCEL, ("g_confdot was cancelled"));
103 	sb = p;
104 	g_topology_assert();
105 	sbuf_cat(sb, "digraph geom {\n");
106 	LIST_FOREACH(mp, &g_classes, class)
107 		g_confdot_class(sb, mp);
108 	sbuf_cat(sb, "}\n");
109 	sbuf_finish(sb);
110 }
111 
112 static void
113 g_conftxt_geom(struct sbuf *sb, struct g_geom *gp, int level)
114 {
115 	struct g_provider *pp;
116 	struct g_consumer *cp;
117 
118 	if (gp->flags & G_GEOM_WITHER)
119 		return;
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);
125 		sbuf_cat(sb, "\n");
126 		LIST_FOREACH(cp, &pp->consumers, consumers)
127 			g_conftxt_geom(sb, cp->geom, level + 1);
128 	}
129 }
130 
131 static void
132 g_conftxt_class(struct sbuf *sb, struct g_class *mp)
133 {
134 	struct g_geom *gp;
135 
136 	LIST_FOREACH(gp, &mp->geom, geom)
137 		g_conftxt_geom(sb, gp, 0);
138 }
139 
140 void
141 g_conftxt(void *p, int flag)
142 {
143 	struct g_class *mp;
144 	struct sbuf *sb;
145 
146 	KASSERT(flag != EV_CANCEL, ("g_conftxt was cancelled"));
147 	sb = p;
148 	g_topology_assert();
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);
152 	}
153 	sbuf_finish(sb);
154 }
155 
156 void
157 g_conf_cat_escaped(struct sbuf *sb, const char *buf)
158 {
159 	const u_char *c;
160 
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)
166 			sbuf_putc(sb, *c);
167 		else
168 			sbuf_putc(sb, '?');
169 	}
170 }
171 
172 void
173 g_conf_printf_escaped(struct sbuf *sb, const char *fmt, ...)
174 {
175 	struct sbuf *s;
176 	va_list ap;
177 
178 	s = sbuf_new_auto();
179 	va_start(ap, fmt);
180 	sbuf_vprintf(s, fmt, ap);
181 	va_end(ap);
182 	sbuf_finish(s);
183 
184 	g_conf_cat_escaped(sb, sbuf_data(s));
185 	sbuf_delete(s);
186 }
187 
188 static void
189 g_conf_consumer(struct sbuf *sb, struct g_consumer *cp)
190 {
191 
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)
199 		;
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");
204 	}
205 	sbuf_cat(sb, "\t</consumer>\n");
206 }
207 
208 static void
209 g_conf_provider(struct sbuf *sb, struct g_provider *pp)
210 {
211 	struct g_geom_alias *gap;
212 
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");
224 	}
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)
233 		;
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");
238 	}
239 	sbuf_cat(sb, "\t</provider>\n");
240 }
241 
242 static void
243 g_conf_geom(struct sbuf *sb, struct g_geom *gp)
244 {
245 	struct g_consumer *cp;
246 	struct g_provider *pp;
247 
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");
260 	}
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");
266 }
267 
268 static bool
269 g_conf_matchgp(struct g_geom *gp, struct g_geom **gps)
270 {
271 
272 	if (gps == NULL)
273 		return (true);
274 	for (; *gps != NULL; gps++) {
275 		if (*gps == gp)
276 			return (true);
277 	}
278 	return (false);
279 }
280 
281 static void
282 g_conf_class(struct sbuf *sb, struct g_class *mp, struct g_geom **gps)
283 {
284 	struct g_geom *gp;
285 
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))
292 			continue;
293 		g_conf_geom(sb, gp);
294 		if (sbuf_error(sb))
295 			break;
296 	}
297 	sbuf_cat(sb, "  </class>\n");
298 }
299 
300 void
301 g_conf_specific(struct sbuf *sb, struct g_geom **gps)
302 {
303 	struct g_class *mp2;
304 
305 	g_topology_assert();
306 	sbuf_cat(sb, "<mesh>\n");
307 	LIST_FOREACH(mp2, &g_classes, class) {
308 		g_conf_class(sb, mp2, gps);
309 		if (sbuf_error(sb))
310 			break;
311 	}
312 	sbuf_cat(sb, "</mesh>\n");
313 	sbuf_finish(sb);
314 }
315 
316 void
317 g_confxml(void *p, int flag)
318 {
319 
320 	KASSERT(flag != EV_CANCEL, ("g_confxml was cancelled"));
321 	g_topology_assert();
322 	g_conf_specific(p, NULL);
323 }
324 
325 void
326 (g_trace)(int level, const char *fmt, ...)
327 {
328 	va_list ap;
329 
330 	if (!(g_debugflags & level))
331 		return;
332 	va_start(ap, fmt);
333 	vprintf(fmt, ap);
334 	va_end(ap);
335 	printf("\n");
336 }
337