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