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