geom_dump.c revision 106301
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: head/sys/geom/geom_dump.c 106301 2002-11-01 15:57:55Z phk $
36 */
37
38
39#include <sys/param.h>
40#include <sys/stdint.h>
41#include <sys/sbuf.h>
42#ifndef _KERNEL
43#include <stdio.h>
44#include <string.h>
45#include <unistd.h>
46#include <stdlib.h>
47#include <err.h>
48#else
49#include <sys/systm.h>
50#include <sys/malloc.h>
51#endif
52#include <machine/stdarg.h>
53
54#include <geom/geom.h>
55#include <geom/geom_int.h>
56
57
58static void
59g_confdot_consumer(struct sbuf *sb, struct g_consumer *cp)
60{
61
62	sbuf_printf(sb, "z%p [label=\"r%dw%de%d\\nbio #%d\"];\n",
63	    cp, cp->acr, cp->acw, cp->ace, cp->biocount);
64	if (cp->provider)
65		sbuf_printf(sb, "z%p -> z%p;\n", cp, cp->provider);
66}
67
68static void
69g_confdot_provider(struct sbuf *sb, struct g_provider *pp)
70{
71
72	sbuf_printf(sb, "z%p [shape=hexagon,label=\"%s\\nr%dw%de%d\\nerr#%d\"];\n",
73	    pp, pp->name, pp->acr, pp->acw, pp->ace, pp->error);
74}
75
76static void
77g_confdot_geom(struct sbuf *sb, struct g_geom *gp)
78{
79	struct g_consumer *cp;
80	struct g_provider *pp;
81
82	sbuf_printf(sb, "z%p [shape=box,label=\"%s\\n%s\\nr#%d\"];\n",
83	    gp, gp->class->name, gp->name, gp->rank);
84	LIST_FOREACH(cp, &gp->consumer, consumer) {
85		g_confdot_consumer(sb, cp);
86		sbuf_printf(sb, "z%p -> z%p;\n", gp, cp);
87	}
88
89	LIST_FOREACH(pp, &gp->provider, provider) {
90		g_confdot_provider(sb, pp);
91		sbuf_printf(sb, "z%p -> z%p;\n", pp, gp);
92	}
93}
94
95static void
96g_confdot_class(struct sbuf *sb, struct g_class *mp)
97{
98	struct g_geom *gp;
99
100	LIST_FOREACH(gp, &mp->geom, geom)
101		g_confdot_geom(sb, gp);
102}
103
104void
105g_confdot(void *p)
106{
107	struct g_class *mp;
108	struct sbuf *sb;
109
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
120static void
121g_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		gp->dumpconf(sb, NULL, gp, NULL, pp);
130		sbuf_printf(sb, "\n");
131		LIST_FOREACH(cp, &pp->consumers, consumers)
132			g_conftxt_geom(sb, cp->geom, level + 1);
133	}
134}
135
136static void
137g_conftxt_class(struct sbuf *sb, struct g_class *mp)
138{
139	struct g_geom *gp;
140
141	LIST_FOREACH(gp, &mp->geom, geom)
142		g_conftxt_geom(sb, gp, 0);
143}
144
145void
146g_conftxt(void *p)
147{
148	struct g_class *mp;
149	struct sbuf *sb;
150
151	sb = p;
152	g_topology_assert();
153	LIST_FOREACH(mp, &g_classes, class)
154		if (!strcmp(mp->name, "DISK"))
155			break;
156	if (mp != NULL)
157		g_conftxt_class(sb, mp);
158	else
159		printf("no DISK\n");
160	sbuf_finish(sb);
161	wakeup(p);
162}
163
164
165static void
166g_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) {
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
183static void
184g_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) {
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
204static void
205g_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) {
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
233static void
234g_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
248void
249g_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
267void
268g_confxml(void *p)
269{
270
271	g_topology_assert();
272	g_conf_specific(p, NULL, NULL, NULL, NULL);
273	wakeup(p);
274}
275
276void
277g_trace(int level, char *fmt, ...)
278{
279	va_list ap;
280
281	g_sanity(NULL);
282	if (!(g_debugflags & level))
283		return;
284	va_start(ap, fmt);
285	vprintf(fmt, ap);
286	printf("\n");
287}
288
289void
290g_hexdump(void *ptr, int length)
291{
292	int i, j, k;
293	unsigned char *cp;
294
295	cp = ptr;
296	for (i = 0; i < length; i+= 16) {
297		printf("%04x  ", i);
298		for (j = 0; j < 16; j++) {
299			k = i + j;
300			if (k < length)
301				printf(" %02x", cp[k]);
302			else
303				printf("   ");
304		}
305		printf("  |");
306		for (j = 0; j < 16; j++) {
307			k = i + j;
308			if (k >= length)
309				printf(" ");
310			else if (cp[k] >= ' ' && cp[k] <= '~')
311				printf("%c", cp[k]);
312			else
313				printf(".");
314		}
315		printf("|\n");
316	}
317}
318
319