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