xref: /freebsd/sys/geom/geom_dump.c (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9  * DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
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 
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 	    pp, pp->name, pp->acr, pp->acw, pp->ace, pp->error);
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_printf(sb, "digraph geom {\n");
106 	LIST_FOREACH(mp, &g_classes, class)
107 		g_confdot_class(sb, mp);
108 	sbuf_printf(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_printf(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 
157 void
158 g_conf_printf_escaped(struct sbuf *sb, const char *fmt, ...)
159 {
160 	struct sbuf *s;
161 	const u_char *c;
162 	va_list ap;
163 
164 	s = sbuf_new_auto();
165 	va_start(ap, fmt);
166 	sbuf_vprintf(s, fmt, ap);
167 	va_end(ap);
168 	sbuf_finish(s);
169 
170 	for (c = sbuf_data(s); *c != '\0'; c++) {
171 		if (*c == '&' || *c == '<' || *c == '>' ||
172 		    *c == '\'' || *c == '"' || *c > 0x7e)
173 			sbuf_printf(sb, "&#x%X;", *c);
174 		else if (*c == '\t' || *c == '\n' || *c == '\r' || *c > 0x1f)
175 			sbuf_putc(sb, *c);
176 		else
177 			sbuf_putc(sb, '?');
178 	}
179 	sbuf_delete(s);
180 }
181 
182 static void
183 g_conf_consumer(struct sbuf *sb, struct g_consumer *cp)
184 {
185 
186 	sbuf_printf(sb, "\t<consumer id=\"%p\">\n", cp);
187 	sbuf_printf(sb, "\t  <geom ref=\"%p\"/>\n", cp->geom);
188 	if (cp->provider != NULL)
189 		sbuf_printf(sb, "\t  <provider ref=\"%p\"/>\n", cp->provider);
190 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
191 	    cp->acr, cp->acw, cp->ace);
192 	if (cp->geom->flags & G_GEOM_WITHER)
193 		;
194 	else if (cp->geom->dumpconf != NULL) {
195 		sbuf_printf(sb, "\t  <config>\n");
196 		cp->geom->dumpconf(sb, "\t    ", cp->geom, cp, NULL);
197 		sbuf_printf(sb, "\t  </config>\n");
198 	}
199 	sbuf_printf(sb, "\t</consumer>\n");
200 }
201 
202 static void
203 g_conf_provider(struct sbuf *sb, struct g_provider *pp)
204 {
205 
206 	sbuf_printf(sb, "\t<provider id=\"%p\">\n", pp);
207 	sbuf_printf(sb, "\t  <geom ref=\"%p\"/>\n", pp->geom);
208 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
209 	    pp->acr, pp->acw, pp->ace);
210 	sbuf_printf(sb, "\t  <name>");
211 	g_conf_printf_escaped(sb, "%s", pp->name);
212 	sbuf_printf(sb, "</name>\n");
213 	sbuf_printf(sb, "\t  <mediasize>%jd</mediasize>\n",
214 	    (intmax_t)pp->mediasize);
215 	sbuf_printf(sb, "\t  <sectorsize>%u</sectorsize>\n", pp->sectorsize);
216 	sbuf_printf(sb, "\t  <stripesize>%u</stripesize>\n", pp->stripesize);
217 	sbuf_printf(sb, "\t  <stripeoffset>%u</stripeoffset>\n", pp->stripeoffset);
218 	if (pp->geom->flags & G_GEOM_WITHER)
219 		;
220 	else if (pp->geom->dumpconf != NULL) {
221 		sbuf_printf(sb, "\t  <config>\n");
222 		pp->geom->dumpconf(sb, "\t    ", pp->geom, NULL, pp);
223 		sbuf_printf(sb, "\t  </config>\n");
224 	}
225 	sbuf_printf(sb, "\t</provider>\n");
226 }
227 
228 
229 static void
230 g_conf_geom(struct sbuf *sb, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
231 {
232 	struct g_consumer *cp2;
233 	struct g_provider *pp2;
234 
235 	sbuf_printf(sb, "    <geom id=\"%p\">\n", gp);
236 	sbuf_printf(sb, "      <class ref=\"%p\"/>\n", gp->class);
237 	sbuf_printf(sb, "      <name>");
238 	g_conf_printf_escaped(sb, "%s", gp->name);
239 	sbuf_printf(sb, "</name>\n");
240 	sbuf_printf(sb, "      <rank>%d</rank>\n", gp->rank);
241 	if (gp->flags & G_GEOM_WITHER)
242 		sbuf_printf(sb, "      <wither/>\n");
243 	else if (gp->dumpconf != NULL) {
244 		sbuf_printf(sb, "      <config>\n");
245 		gp->dumpconf(sb, "\t", gp, NULL, NULL);
246 		sbuf_printf(sb, "      </config>\n");
247 	}
248 	LIST_FOREACH(cp2, &gp->consumer, consumer) {
249 		if (cp != NULL && cp != cp2)
250 			continue;
251 		g_conf_consumer(sb, cp2);
252 	}
253 
254 	LIST_FOREACH(pp2, &gp->provider, provider) {
255 		if (pp != NULL && pp != pp2)
256 			continue;
257 		g_conf_provider(sb, pp2);
258 	}
259 	sbuf_printf(sb, "    </geom>\n");
260 }
261 
262 static void
263 g_conf_class(struct sbuf *sb, struct g_class *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
264 {
265 	struct g_geom *gp2;
266 
267 	sbuf_printf(sb, "  <class id=\"%p\">\n", mp);
268 	sbuf_printf(sb, "    <name>");
269 	g_conf_printf_escaped(sb, "%s", mp->name);
270 	sbuf_printf(sb, "</name>\n");
271 	LIST_FOREACH(gp2, &mp->geom, geom) {
272 		if (gp != NULL && gp != gp2)
273 			continue;
274 		g_conf_geom(sb, gp2, pp, cp);
275 	}
276 	sbuf_printf(sb, "  </class>\n");
277 }
278 
279 void
280 g_conf_specific(struct sbuf *sb, struct g_class *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp)
281 {
282 	struct g_class *mp2;
283 
284 	g_topology_assert();
285 	sbuf_printf(sb, "<mesh>\n");
286 	LIST_FOREACH(mp2, &g_classes, class) {
287 		if (mp != NULL && mp != mp2)
288 			continue;
289 		g_conf_class(sb, mp2, gp, pp, cp);
290 	}
291 	sbuf_printf(sb, "</mesh>\n");
292 	sbuf_finish(sb);
293 }
294 
295 void
296 g_confxml(void *p, int flag)
297 {
298 
299 	KASSERT(flag != EV_CANCEL, ("g_confxml was cancelled"));
300 	g_topology_assert();
301 	g_conf_specific(p, NULL, NULL, NULL, NULL);
302 }
303 
304 void
305 g_trace(int level, const char *fmt, ...)
306 {
307 	va_list ap;
308 
309 	if (!(g_debugflags & level))
310 		return;
311 	va_start(ap, fmt);
312 	vprintf(fmt, ap);
313 	va_end(ap);
314 	printf("\n");
315 }
316