xref: /freebsd/sys/geom/geom_dump.c (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
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 
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 
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 	sbuf_printf(sb, "\t  <mediasize>%jd</mediasize>\n",
223 	    (intmax_t)pp->mediasize);
224 	sbuf_printf(sb, "\t  <sectorsize>%u</sectorsize>\n", pp->sectorsize);
225 	sbuf_printf(sb, "\t  <stripesize>%ju</stripesize>\n", (uintmax_t)pp->stripesize);
226 	sbuf_printf(sb, "\t  <stripeoffset>%ju</stripeoffset>\n", (uintmax_t)pp->stripeoffset);
227 	if (pp->flags & G_PF_WITHER)
228 		sbuf_cat(sb, "\t  <wither/>\n");
229 	else if (pp->geom->flags & G_GEOM_WITHER)
230 		;
231 	else if (pp->geom->dumpconf != NULL) {
232 		sbuf_cat(sb, "\t  <config>\n");
233 		pp->geom->dumpconf(sb, "\t    ", pp->geom, NULL, pp);
234 		sbuf_cat(sb, "\t  </config>\n");
235 	}
236 	sbuf_cat(sb, "\t</provider>\n");
237 }
238 
239 
240 static void
241 g_conf_geom(struct sbuf *sb, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
242 {
243 	struct g_consumer *cp2;
244 	struct g_provider *pp2;
245 	struct g_geom_alias *gap;
246 
247 	sbuf_printf(sb, "    <geom id=\"%p\">\n", gp);
248 	sbuf_printf(sb, "      <class ref=\"%p\"/>\n", gp->class);
249 	sbuf_cat(sb, "      <name>");
250 	g_conf_cat_escaped(sb, gp->name);
251 	sbuf_cat(sb, "</name>\n");
252 	sbuf_printf(sb, "      <rank>%d</rank>\n", gp->rank);
253 	if (gp->flags & G_GEOM_WITHER)
254 		sbuf_cat(sb, "      <wither/>\n");
255 	else if (gp->dumpconf != NULL) {
256 		sbuf_cat(sb, "      <config>\n");
257 		gp->dumpconf(sb, "\t", gp, NULL, NULL);
258 		sbuf_cat(sb, "      </config>\n");
259 	}
260 	LIST_FOREACH(cp2, &gp->consumer, consumer) {
261 		if (cp != NULL && cp != cp2)
262 			continue;
263 		g_conf_consumer(sb, cp2);
264 	}
265 
266 	LIST_FOREACH(pp2, &gp->provider, provider) {
267 		if (pp != NULL && pp != pp2)
268 			continue;
269 		g_conf_provider(sb, pp2);
270 	}
271 	LIST_FOREACH(gap, &gp->aliases, ga_next) {
272 		sbuf_cat(sb, "      <alias>\n");
273 		g_conf_cat_escaped(sb, gap->ga_alias);
274 		sbuf_cat(sb, "      </alias>\n");
275 	}
276 	sbuf_cat(sb, "    </geom>\n");
277 }
278 
279 static void
280 g_conf_class(struct sbuf *sb, struct g_class *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
281 {
282 	struct g_geom *gp2;
283 
284 	sbuf_printf(sb, "  <class id=\"%p\">\n", mp);
285 	sbuf_cat(sb, "    <name>");
286 	g_conf_cat_escaped(sb, mp->name);
287 	sbuf_cat(sb, "</name>\n");
288 	LIST_FOREACH(gp2, &mp->geom, geom) {
289 		if (gp != NULL && gp != gp2)
290 			continue;
291 		g_conf_geom(sb, gp2, pp, cp);
292 	}
293 	sbuf_cat(sb, "  </class>\n");
294 }
295 
296 void
297 g_conf_specific(struct sbuf *sb, struct g_class *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
298 {
299 	struct g_class *mp2;
300 
301 	g_topology_assert();
302 	sbuf_cat(sb, "<mesh>\n");
303 	LIST_FOREACH(mp2, &g_classes, class) {
304 		if (mp != NULL && mp != mp2)
305 			continue;
306 		g_conf_class(sb, mp2, gp, pp, cp);
307 	}
308 	sbuf_cat(sb, "</mesh>\n");
309 	sbuf_finish(sb);
310 }
311 
312 void
313 g_confxml(void *p, int flag)
314 {
315 
316 	KASSERT(flag != EV_CANCEL, ("g_confxml was cancelled"));
317 	g_topology_assert();
318 	g_conf_specific(p, NULL, NULL, NULL, NULL);
319 }
320 
321 void
322 g_trace(int level, const char *fmt, ...)
323 {
324 	va_list ap;
325 
326 	if (!(g_debugflags & level))
327 		return;
328 	va_start(ap, fmt);
329 	vprintf(fmt, ap);
330 	va_end(ap);
331 	printf("\n");
332 }
333