1 /*- 2 * Copyright (c) 2004 Lukas Ertl 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/libkern.h> 33 #include <sys/malloc.h> 34 35 #include <geom/geom.h> 36 #include <geom/vinum/geom_vinum_var.h> 37 #include <geom/vinum/geom_vinum.h> 38 #include <geom/vinum/geom_vinum_share.h> 39 40 void gv_lvi(struct gv_volume *, struct sbuf *, int); 41 void gv_lpi(struct gv_plex *, struct sbuf *, int); 42 void gv_lsi(struct gv_sd *, struct sbuf *, int); 43 void gv_ldi(struct gv_drive *, struct sbuf *, int); 44 45 void 46 gv_list(struct g_geom *gp, struct gctl_req *req) 47 { 48 struct gv_softc *sc; 49 struct gv_drive *d; 50 struct gv_plex *p; 51 struct gv_sd *s; 52 struct gv_volume *v; 53 struct sbuf *sb; 54 int *argc, i, *flags, type; 55 char *arg, buf[20], *cmd; 56 57 argc = gctl_get_paraml(req, "argc", sizeof(*argc)); 58 59 if (argc == NULL) { 60 gctl_error(req, "no arguments given"); 61 return; 62 } 63 64 flags = gctl_get_paraml(req, "flags", sizeof(*flags)); 65 if (flags == NULL) { 66 gctl_error(req, "no flags given"); 67 return; 68 } 69 70 sc = gp->softc; 71 72 sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN); 73 74 /* Figure out which command was given. */ 75 cmd = gctl_get_param(req, "cmd", NULL); 76 if (cmd == NULL) { 77 gctl_error(req, "no command given"); 78 return; 79 } 80 81 /* List specific objects or everything. */ 82 if (!strcmp(cmd, "list") || !strcmp(cmd, "l")) { 83 if (*argc) { 84 for (i = 0; i < *argc; i++) { 85 snprintf(buf, sizeof(buf), "argv%d", i); 86 arg = gctl_get_param(req, buf, NULL); 87 if (arg == NULL) 88 continue; 89 type = gv_object_type(sc, arg); 90 switch (type) { 91 case GV_TYPE_VOL: 92 v = gv_find_vol(sc, arg); 93 gv_lvi(v, sb, *flags); 94 break; 95 case GV_TYPE_PLEX: 96 p = gv_find_plex(sc, arg); 97 gv_lpi(p, sb, *flags); 98 break; 99 case GV_TYPE_SD: 100 s = gv_find_sd(sc, arg); 101 gv_lsi(s, sb, *flags); 102 break; 103 case GV_TYPE_DRIVE: 104 d = gv_find_drive(sc, arg); 105 gv_ldi(d, sb, *flags); 106 break; 107 default: 108 gctl_error(req, "unknown object '%s'", 109 arg); 110 break; 111 } 112 } 113 } else { 114 gv_ld(gp, req, sb); 115 sbuf_printf(sb, "\n"); 116 gv_lv(gp, req, sb); 117 sbuf_printf(sb, "\n"); 118 gv_lp(gp, req, sb); 119 sbuf_printf(sb, "\n"); 120 gv_ls(gp, req, sb); 121 } 122 123 /* List drives. */ 124 } else if (!strcmp(cmd, "ld")) { 125 if (*argc) { 126 for (i = 0; i < *argc; i++) { 127 snprintf(buf, sizeof(buf), "argv%d", i); 128 arg = gctl_get_param(req, buf, NULL); 129 if (arg == NULL) 130 continue; 131 type = gv_object_type(sc, arg); 132 if (type != GV_TYPE_DRIVE) { 133 gctl_error(req, "'%s' is not a drive", 134 arg); 135 continue; 136 } else { 137 d = gv_find_drive(sc, arg); 138 gv_ldi(d, sb, *flags); 139 } 140 } 141 } else 142 gv_ld(gp, req, sb); 143 144 /* List volumes. */ 145 } else if (!strcmp(cmd, "lv")) { 146 if (*argc) { 147 for (i = 0; i < *argc; i++) { 148 snprintf(buf, sizeof(buf), "argv%d", i); 149 arg = gctl_get_param(req, buf, NULL); 150 if (arg == NULL) 151 continue; 152 type = gv_object_type(sc, arg); 153 if (type != GV_TYPE_VOL) { 154 gctl_error(req, "'%s' is not a volume", 155 arg); 156 continue; 157 } else { 158 v = gv_find_vol(sc, arg); 159 gv_lvi(v, sb, *flags); 160 } 161 } 162 } else 163 gv_lv(gp, req, sb); 164 165 /* List plexes. */ 166 } else if (!strcmp(cmd, "lp")) { 167 if (*argc) { 168 for (i = 0; i < *argc; i++) { 169 snprintf(buf, sizeof(buf), "argv%d", i); 170 arg = gctl_get_param(req, buf, NULL); 171 if (arg == NULL) 172 continue; 173 type = gv_object_type(sc, arg); 174 if (type != GV_TYPE_PLEX) { 175 gctl_error(req, "'%s' is not a plex", 176 arg); 177 continue; 178 } else { 179 p = gv_find_plex(sc, arg); 180 gv_lpi(p, sb, *flags); 181 } 182 } 183 } else 184 gv_lp(gp, req, sb); 185 186 /* List subdisks. */ 187 } else if (!strcmp(cmd, "ls")) { 188 if (*argc) { 189 for (i = 0; i < *argc; i++) { 190 snprintf(buf, sizeof(buf), "argv%d", i); 191 arg = gctl_get_param(req, buf, NULL); 192 if (arg == NULL) 193 continue; 194 type = gv_object_type(sc, arg); 195 if (type != GV_TYPE_SD) { 196 gctl_error(req, "'%s' is not a subdisk", 197 arg); 198 continue; 199 } else { 200 s = gv_find_sd(sc, arg); 201 gv_lsi(s, sb, *flags); 202 } 203 } 204 } else 205 gv_ls(gp, req, sb); 206 207 } else 208 gctl_error(req, "unknown command '%s'", cmd); 209 210 sbuf_finish(sb); 211 gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1); 212 sbuf_delete(sb); 213 } 214 215 /* List one or more volumes. */ 216 void 217 gv_lv(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb) 218 { 219 struct gv_softc *sc; 220 struct gv_volume *v; 221 int i, *flags; 222 223 sc = gp->softc; 224 i = 0; 225 226 LIST_FOREACH(v, &sc->volumes, volume) 227 i++; 228 229 sbuf_printf(sb, "%d volume%s:\n", i, i == 1 ? "" : "s"); 230 231 if (i) { 232 flags = gctl_get_paraml(req, "flags", sizeof(*flags)); 233 LIST_FOREACH(v, &sc->volumes, volume) 234 gv_lvi(v, sb, *flags); 235 } 236 } 237 238 /* List a single volume. */ 239 void 240 gv_lvi(struct gv_volume *v, struct sbuf *sb, int flags) 241 { 242 struct gv_plex *p; 243 int i; 244 245 if (flags & GV_FLAG_V) { 246 sbuf_printf(sb, "Volume %s:\tSize: %jd bytes (%jd MB)\n", 247 v->name, (intmax_t)v->size, (intmax_t)v->size / MEGABYTE); 248 sbuf_printf(sb, "\t\tState: %s\n", gv_volstate(v->state)); 249 } else { 250 sbuf_printf(sb, "V %-21s State: %s\tPlexes: %7d\tSize: %s\n", 251 v->name, gv_volstate(v->state), v->plexcount, 252 gv_roughlength(v->size, 0)); 253 } 254 255 if (flags & GV_FLAG_VV) { 256 i = 0; 257 LIST_FOREACH(p, &v->plexes, in_volume) { 258 sbuf_printf(sb, "\t\tPlex %2d:\t%s\t(%s), %s\n", i, 259 p->name, gv_plexstate(p->state), 260 gv_roughlength(p->size, 0)); 261 i++; 262 } 263 } 264 265 if (flags & GV_FLAG_R) { 266 LIST_FOREACH(p, &v->plexes, in_volume) 267 gv_lpi(p, sb, flags); 268 } 269 } 270 271 /* List one or more plexes. */ 272 void 273 gv_lp(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb) 274 { 275 struct gv_softc *sc; 276 struct gv_plex *p; 277 int i, *flags; 278 279 sc = gp->softc; 280 i = 0; 281 282 LIST_FOREACH(p, &sc->plexes, plex) 283 i++; 284 285 sbuf_printf(sb, "%d plex%s:\n", i, i == 1 ? "" : "es"); 286 287 if (i) { 288 flags = gctl_get_paraml(req, "flags", sizeof(*flags)); 289 LIST_FOREACH(p, &sc->plexes, plex) 290 gv_lpi(p, sb, *flags); 291 } 292 } 293 294 /* List a single plex. */ 295 void 296 gv_lpi(struct gv_plex *p, struct sbuf *sb, int flags) 297 { 298 struct gv_sd *s; 299 int i; 300 301 if (flags & GV_FLAG_V) { 302 sbuf_printf(sb, "Plex %s:\tSize:\t%9jd bytes (%jd MB)\n", 303 p->name, (intmax_t)p->size, (intmax_t)p->size / MEGABYTE); 304 sbuf_printf(sb, "\t\tSubdisks: %8d\n", p->sdcount); 305 sbuf_printf(sb, "\t\tState: %s\n\t\tOrganization: %s", 306 gv_plexstate(p->state), gv_plexorg(p->org)); 307 if (gv_is_striped(p)) { 308 sbuf_printf(sb, "\tStripe size: %s\n", 309 gv_roughlength(p->stripesize, 1)); 310 } 311 if (p->vol_sc != NULL) { 312 sbuf_printf(sb, "\t\tPart of volume %s\n", p->volume); 313 } 314 } else { 315 sbuf_printf(sb, "P %-18s %2s State: %s\tSubdisks: %5d" 316 "\tSize: %s\n", p->name, gv_plexorg_short(p->org), 317 gv_plexstate(p->state), p->sdcount, 318 gv_roughlength(p->size, 0)); 319 } 320 321 if (flags & GV_FLAG_VV) { 322 i = 0; 323 LIST_FOREACH(s, &p->subdisks, in_plex) { 324 sbuf_printf(sb, "\t\tSubdisk %d:\t%s\n", i, s->name); 325 sbuf_printf(sb, "\t\t state: %s\tsize %11jd " 326 "(%jd MB)\n", gv_sdstate(s->state), 327 (intmax_t)s->size, (intmax_t)s->size / MEGABYTE); 328 if (p->org == GV_PLEX_CONCAT) { 329 sbuf_printf(sb, "\t\t\toffset %9jd (0x%jx)\n", 330 (intmax_t)s->plex_offset, 331 (intmax_t)s->plex_offset); 332 } 333 i++; 334 } 335 } 336 337 if (flags & GV_FLAG_R) { 338 LIST_FOREACH(s, &p->subdisks, in_plex) 339 gv_lsi(s, sb, flags); 340 } 341 } 342 343 /* List one or more subdisks. */ 344 void 345 gv_ls(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb) 346 { 347 struct gv_softc *sc; 348 struct gv_sd *s; 349 int i, *flags; 350 351 sc = gp->softc; 352 i = 0; 353 354 LIST_FOREACH(s, &sc->subdisks, sd) 355 i++; 356 357 sbuf_printf(sb, "%d subdisk%s:\n", i, i == 1 ? "" : "s"); 358 359 if (i) { 360 flags = gctl_get_paraml(req, "flags", sizeof(*flags)); 361 LIST_FOREACH(s, &sc->subdisks, sd) 362 gv_lsi(s, sb, *flags); 363 } 364 } 365 366 /* List a single subdisk. */ 367 void 368 gv_lsi(struct gv_sd *s, struct sbuf *sb, int flags) 369 { 370 if (flags & GV_FLAG_V) { 371 sbuf_printf(sb, "Subdisk %s:\n", s->name); 372 sbuf_printf(sb, "\t\tSize: %16jd bytes (%jd MB)\n", 373 (intmax_t)s->size, (intmax_t)s->size / MEGABYTE); 374 sbuf_printf(sb, "\t\tState: %s\n", gv_sdstate(s->state)); 375 376 if (s->state == GV_SD_INITIALIZING || 377 s->state == GV_SD_REVIVING) { 378 if (s->state == GV_SD_INITIALIZING) 379 sbuf_printf(sb, "\t\tInitialized: "); 380 else 381 sbuf_printf(sb, "\t\tRevived: "); 382 383 sbuf_printf(sb, "%16jd bytes (%d%%)\n", 384 (intmax_t)s->initialized, 385 (int)((s->initialized * 100) / s->size)); 386 } 387 388 if (s->plex_sc != NULL) { 389 sbuf_printf(sb, "\t\tPlex %s at offset %jd (%s)\n", 390 s->plex, (intmax_t)s->plex_offset, 391 gv_roughlength(s->plex_offset, 1)); 392 } 393 394 sbuf_printf(sb, "\t\tDrive %s (%s) at offset %jd (%s)\n", 395 s->drive, 396 s->drive_sc == NULL ? "*missing*" : s->drive_sc->name, 397 (intmax_t)s->drive_offset, 398 gv_roughlength(s->drive_offset, 1)); 399 } else { 400 sbuf_printf(sb, "S %-21s State: ", s->name); 401 if (s->state == GV_SD_INITIALIZING || 402 s->state == GV_SD_REVIVING) { 403 if (s->state == GV_SD_INITIALIZING) 404 sbuf_printf(sb, "I "); 405 else 406 sbuf_printf(sb, "R "); 407 sbuf_printf(sb, "%d%%\t", 408 (int)((s->initialized * 100) / s->size)); 409 } else { 410 sbuf_printf(sb, "%s\t", gv_sdstate(s->state)); 411 } 412 sbuf_printf(sb, "D: %-12s Size: %s\n", s->drive, 413 gv_roughlength(s->size, 0)); 414 } 415 } 416 417 /* List one or more drives. */ 418 void 419 gv_ld(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb) 420 { 421 struct gv_softc *sc; 422 struct gv_drive *d; 423 int i, *flags; 424 425 sc = gp->softc; 426 i = 0; 427 428 LIST_FOREACH(d, &sc->drives, drive) 429 i++; 430 431 sbuf_printf(sb, "%d drive%s:\n", i, i == 1 ? "" : "s"); 432 433 if (i) { 434 flags = gctl_get_paraml(req, "flags", sizeof(*flags)); 435 LIST_FOREACH(d, &sc->drives, drive) 436 gv_ldi(d, sb, *flags); 437 } 438 } 439 440 /* List a single drive. */ 441 void 442 gv_ldi(struct gv_drive *d, struct sbuf *sb, int flags) 443 { 444 struct gv_freelist *fl; 445 struct gv_sd *s; 446 447 /* Verbose listing. */ 448 if (flags & GV_FLAG_V) { 449 sbuf_printf(sb, "Drive %s:\tDevice %s\n", d->name, d->device); 450 sbuf_printf(sb, "\t\tSize: %16jd bytes (%jd MB)\n", 451 (intmax_t)d->size, (intmax_t)d->size / MEGABYTE); 452 sbuf_printf(sb, "\t\tUsed: %16jd bytes (%jd MB)\n", 453 (intmax_t)d->size - d->avail, 454 (intmax_t)(d->size - d->avail) / MEGABYTE); 455 sbuf_printf(sb, "\t\tAvailable: %11jd bytes (%jd MB)\n", 456 (intmax_t)d->avail, (intmax_t)d->avail / MEGABYTE); 457 sbuf_printf(sb, "\t\tState: %s\n", gv_drivestate(d->state)); 458 459 /* Be very verbose. */ 460 if (flags & GV_FLAG_VV) { 461 sbuf_printf(sb, "\t\tFree list contains %d entries:\n", 462 d->freelist_entries); 463 sbuf_printf(sb, "\t\t Offset\t Size\n"); 464 LIST_FOREACH(fl, &d->freelist, freelist) 465 sbuf_printf(sb, "\t\t%9jd\t%9jd\n", 466 (intmax_t)fl->offset, (intmax_t)fl->size); 467 } 468 } else { 469 sbuf_printf(sb, "D %-21s State: %s\t/dev/%s\tA: %jd/%jd MB " 470 "(%d%%)\n", d->name, gv_drivestate(d->state), d->device, 471 (intmax_t)d->avail / MEGABYTE, (intmax_t)d->size / MEGABYTE, 472 (int)((d->avail * 100) / d->size)); 473 } 474 475 /* Recursive listing. */ 476 if (flags & GV_FLAG_R) { 477 LIST_FOREACH(s, &d->subdisks, from_drive) 478 gv_lsi(s, sb, flags); 479 } 480 } 481