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