xref: /freebsd/sys/geom/geom_dump.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
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_printf(sb, "digraph geom {\n");
109 	LIST_FOREACH(mp, &g_classes, class)
110 		g_confdot_class(sb, mp);
111 	sbuf_printf(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_printf(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 
160 void
161 g_conf_printf_escaped(struct sbuf *sb, const char *fmt, ...)
162 {
163 	struct sbuf *s;
164 	const u_char *c;
165 	va_list ap;
166 
167 	s = sbuf_new_auto();
168 	va_start(ap, fmt);
169 	sbuf_vprintf(s, fmt, ap);
170 	va_end(ap);
171 	sbuf_finish(s);
172 
173 	for (c = sbuf_data(s); *c != '\0'; c++) {
174 		if (*c == '&' || *c == '<' || *c == '>' ||
175 		    *c == '\'' || *c == '"' || *c > 0x7e)
176 			sbuf_printf(sb, "&#x%X;", *c);
177 		else if (*c == '\t' || *c == '\n' || *c == '\r' || *c > 0x1f)
178 			sbuf_putc(sb, *c);
179 		else
180 			sbuf_putc(sb, '?');
181 	}
182 	sbuf_delete(s);
183 }
184 
185 static void
186 g_conf_consumer(struct sbuf *sb, struct g_consumer *cp)
187 {
188 
189 	sbuf_printf(sb, "\t<consumer id=\"%p\">\n", cp);
190 	sbuf_printf(sb, "\t  <geom ref=\"%p\"/>\n", cp->geom);
191 	if (cp->provider != NULL)
192 		sbuf_printf(sb, "\t  <provider ref=\"%p\"/>\n", cp->provider);
193 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
194 	    cp->acr, cp->acw, cp->ace);
195 	if (cp->geom->flags & G_GEOM_WITHER)
196 		;
197 	else if (cp->geom->dumpconf != NULL) {
198 		sbuf_printf(sb, "\t  <config>\n");
199 		cp->geom->dumpconf(sb, "\t    ", cp->geom, cp, NULL);
200 		sbuf_printf(sb, "\t  </config>\n");
201 	}
202 	sbuf_printf(sb, "\t</consumer>\n");
203 }
204 
205 static void
206 g_conf_provider(struct sbuf *sb, struct g_provider *pp)
207 {
208 
209 	sbuf_printf(sb, "\t<provider id=\"%p\">\n", pp);
210 	sbuf_printf(sb, "\t  <geom ref=\"%p\"/>\n", pp->geom);
211 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
212 	    pp->acr, pp->acw, pp->ace);
213 	sbuf_printf(sb, "\t  <name>");
214 	g_conf_printf_escaped(sb, "%s", pp->name);
215 	sbuf_printf(sb, "</name>\n");
216 	sbuf_printf(sb, "\t  <mediasize>%jd</mediasize>\n",
217 	    (intmax_t)pp->mediasize);
218 	sbuf_printf(sb, "\t  <sectorsize>%u</sectorsize>\n", pp->sectorsize);
219 	sbuf_printf(sb, "\t  <stripesize>%ju</stripesize>\n", (uintmax_t)pp->stripesize);
220 	sbuf_printf(sb, "\t  <stripeoffset>%ju</stripeoffset>\n", (uintmax_t)pp->stripeoffset);
221 	if (pp->flags & G_PF_WITHER)
222 		sbuf_printf(sb, "\t  <wither/>\n");
223 	else if (pp->geom->flags & G_GEOM_WITHER)
224 		;
225 	else if (pp->geom->dumpconf != NULL) {
226 		sbuf_printf(sb, "\t  <config>\n");
227 		pp->geom->dumpconf(sb, "\t    ", pp->geom, NULL, pp);
228 		sbuf_printf(sb, "\t  </config>\n");
229 	}
230 	sbuf_printf(sb, "\t</provider>\n");
231 }
232 
233 
234 static void
235 g_conf_geom(struct sbuf *sb, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
236 {
237 	struct g_consumer *cp2;
238 	struct g_provider *pp2;
239 	struct g_geom_alias *gap;
240 
241 	sbuf_printf(sb, "    <geom id=\"%p\">\n", gp);
242 	sbuf_printf(sb, "      <class ref=\"%p\"/>\n", gp->class);
243 	sbuf_printf(sb, "      <name>");
244 	g_conf_printf_escaped(sb, "%s", gp->name);
245 	sbuf_printf(sb, "</name>\n");
246 	sbuf_printf(sb, "      <rank>%d</rank>\n", gp->rank);
247 	if (gp->flags & G_GEOM_WITHER)
248 		sbuf_printf(sb, "      <wither/>\n");
249 	else if (gp->dumpconf != NULL) {
250 		sbuf_printf(sb, "      <config>\n");
251 		gp->dumpconf(sb, "\t", gp, NULL, NULL);
252 		sbuf_printf(sb, "      </config>\n");
253 	}
254 	LIST_FOREACH(cp2, &gp->consumer, consumer) {
255 		if (cp != NULL && cp != cp2)
256 			continue;
257 		g_conf_consumer(sb, cp2);
258 	}
259 
260 	LIST_FOREACH(pp2, &gp->provider, provider) {
261 		if (pp != NULL && pp != pp2)
262 			continue;
263 		g_conf_provider(sb, pp2);
264 	}
265 	LIST_FOREACH(gap, &gp->aliases, ga_next) {
266 		sbuf_printf(sb, "      <alias>\n");
267 		g_conf_printf_escaped(sb, "%s", gap->ga_alias);
268 		sbuf_printf(sb, "      </alias>\n");
269 	}
270 	sbuf_printf(sb, "    </geom>\n");
271 }
272 
273 static void
274 g_conf_class(struct sbuf *sb, struct g_class *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
275 {
276 	struct g_geom *gp2;
277 
278 	sbuf_printf(sb, "  <class id=\"%p\">\n", mp);
279 	sbuf_printf(sb, "    <name>");
280 	g_conf_printf_escaped(sb, "%s", mp->name);
281 	sbuf_printf(sb, "</name>\n");
282 	LIST_FOREACH(gp2, &mp->geom, geom) {
283 		if (gp != NULL && gp != gp2)
284 			continue;
285 		g_conf_geom(sb, gp2, pp, cp);
286 	}
287 	sbuf_printf(sb, "  </class>\n");
288 }
289 
290 void
291 g_conf_specific(struct sbuf *sb, struct g_class *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
292 {
293 	struct g_class *mp2;
294 
295 	g_topology_assert();
296 	sbuf_printf(sb, "<mesh>\n");
297 	LIST_FOREACH(mp2, &g_classes, class) {
298 		if (mp != NULL && mp != mp2)
299 			continue;
300 		g_conf_class(sb, mp2, gp, pp, cp);
301 	}
302 	sbuf_printf(sb, "</mesh>\n");
303 	sbuf_finish(sb);
304 }
305 
306 void
307 g_confxml(void *p, int flag)
308 {
309 
310 	KASSERT(flag != EV_CANCEL, ("g_confxml was cancelled"));
311 	g_topology_assert();
312 	g_conf_specific(p, NULL, NULL, NULL, NULL);
313 }
314 
315 void
316 g_trace(int level, const char *fmt, ...)
317 {
318 	va_list ap;
319 
320 	if (!(g_debugflags & level))
321 		return;
322 	va_start(ap, fmt);
323 	vprintf(fmt, ap);
324 	va_end(ap);
325 	printf("\n");
326 }
327