2 * Copyright (c) 2004, 2007 Lukas Ertl
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/libkern.h>
33 #include <sys/malloc.h>
36 #include <geom/geom.h>
37 #include <geom/vinum/geom_vinum_var.h>
38 #include <geom/vinum/geom_vinum.h>
39 #include <geom/vinum/geom_vinum_share.h>
41 void gv_lvi(struct gv_volume *, struct sbuf *, int);
42 void gv_lpi(struct gv_plex *, struct sbuf *, int);
43 void gv_lsi(struct gv_sd *, struct sbuf *, int);
44 void gv_ldi(struct gv_drive *, struct sbuf *, int);
47 gv_list(struct g_geom *gp, struct gctl_req *req)
55 int *argc, i, *flags, type;
56 char *arg, buf[20], *cmd;
58 argc = gctl_get_paraml(req, "argc", sizeof(*argc));
61 gctl_error(req, "no arguments given");
65 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
67 gctl_error(req, "no flags given");
73 sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
75 /* Figure out which command was given. */
76 cmd = gctl_get_param(req, "cmd", NULL);
78 gctl_error(req, "no command given");
82 /* List specific objects or everything. */
83 if (!strcmp(cmd, "list") || !strcmp(cmd, "l")) {
85 for (i = 0; i < *argc; i++) {
86 snprintf(buf, sizeof(buf), "argv%d", i);
87 arg = gctl_get_param(req, buf, NULL);
90 type = gv_object_type(sc, arg);
93 v = gv_find_vol(sc, arg);
94 gv_lvi(v, sb, *flags);
97 p = gv_find_plex(sc, arg);
98 gv_lpi(p, sb, *flags);
101 s = gv_find_sd(sc, arg);
102 gv_lsi(s, sb, *flags);
105 d = gv_find_drive(sc, arg);
106 gv_ldi(d, sb, *flags);
109 gctl_error(req, "unknown object '%s'",
116 sbuf_printf(sb, "\n");
118 sbuf_printf(sb, "\n");
120 sbuf_printf(sb, "\n");
125 } else if (!strcmp(cmd, "ld")) {
127 for (i = 0; i < *argc; i++) {
128 snprintf(buf, sizeof(buf), "argv%d", i);
129 arg = gctl_get_param(req, buf, NULL);
132 type = gv_object_type(sc, arg);
133 if (type != GV_TYPE_DRIVE) {
134 gctl_error(req, "'%s' is not a drive",
138 d = gv_find_drive(sc, arg);
139 gv_ldi(d, sb, *flags);
146 } else if (!strcmp(cmd, "lv")) {
148 for (i = 0; i < *argc; i++) {
149 snprintf(buf, sizeof(buf), "argv%d", i);
150 arg = gctl_get_param(req, buf, NULL);
153 type = gv_object_type(sc, arg);
154 if (type != GV_TYPE_VOL) {
155 gctl_error(req, "'%s' is not a volume",
159 v = gv_find_vol(sc, arg);
160 gv_lvi(v, sb, *flags);
167 } else if (!strcmp(cmd, "lp")) {
169 for (i = 0; i < *argc; i++) {
170 snprintf(buf, sizeof(buf), "argv%d", i);
171 arg = gctl_get_param(req, buf, NULL);
174 type = gv_object_type(sc, arg);
175 if (type != GV_TYPE_PLEX) {
176 gctl_error(req, "'%s' is not a plex",
180 p = gv_find_plex(sc, arg);
181 gv_lpi(p, sb, *flags);
188 } else if (!strcmp(cmd, "ls")) {
190 for (i = 0; i < *argc; i++) {
191 snprintf(buf, sizeof(buf), "argv%d", i);
192 arg = gctl_get_param(req, buf, NULL);
195 type = gv_object_type(sc, arg);
196 if (type != GV_TYPE_SD) {
197 gctl_error(req, "'%s' is not a subdisk",
201 s = gv_find_sd(sc, arg);
202 gv_lsi(s, sb, *flags);
209 gctl_error(req, "unknown command '%s'", cmd);
212 gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1);
216 /* List one or more volumes. */
218 gv_lv(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb)
227 LIST_FOREACH(v, &sc->volumes, volume)
230 sbuf_printf(sb, "%d volume%s:\n", i, i == 1 ? "" : "s");
233 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
234 LIST_FOREACH(v, &sc->volumes, volume)
235 gv_lvi(v, sb, *flags);
239 /* List a single volume. */
241 gv_lvi(struct gv_volume *v, struct sbuf *sb, int flags)
246 if (flags & GV_FLAG_V) {
247 sbuf_printf(sb, "Volume %s:\tSize: %jd bytes (%jd MB)\n",
248 v->name, (intmax_t)v->size, (intmax_t)v->size / MEGABYTE);
249 sbuf_printf(sb, "\t\tState: %s\n", gv_volstate(v->state));
251 sbuf_printf(sb, "V %-21s State: %s\tPlexes: %7d\tSize: %s\n",
252 v->name, gv_volstate(v->state), v->plexcount,
253 gv_roughlength(v->size, 0));
256 if (flags & GV_FLAG_VV) {
258 LIST_FOREACH(p, &v->plexes, in_volume) {
259 sbuf_printf(sb, "\t\tPlex %2d:\t%s\t(%s), %s\n", i,
260 p->name, gv_plexstate(p->state),
261 gv_roughlength(p->size, 0));
266 if (flags & GV_FLAG_R) {
267 LIST_FOREACH(p, &v->plexes, in_volume)
268 gv_lpi(p, sb, flags);
272 /* List one or more plexes. */
274 gv_lp(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb)
283 LIST_FOREACH(p, &sc->plexes, plex)
286 sbuf_printf(sb, "%d plex%s:\n", i, i == 1 ? "" : "es");
289 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
290 LIST_FOREACH(p, &sc->plexes, plex)
291 gv_lpi(p, sb, *flags);
295 /* List a single plex. */
297 gv_lpi(struct gv_plex *p, struct sbuf *sb, int flags)
302 if (flags & GV_FLAG_V) {
303 sbuf_printf(sb, "Plex %s:\tSize:\t%9jd bytes (%jd MB)\n",
304 p->name, (intmax_t)p->size, (intmax_t)p->size / MEGABYTE);
305 sbuf_printf(sb, "\t\tSubdisks: %8d\n", p->sdcount);
306 sbuf_printf(sb, "\t\tState: %s\n", gv_plexstate(p->state));
307 if ((p->flags & GV_PLEX_SYNCING) ||
308 (p->flags & GV_PLEX_GROWING) ||
309 (p->flags & GV_PLEX_REBUILDING)) {
310 sbuf_printf(sb, "\t\tSynced: ");
311 sbuf_printf(sb, "%16jd bytes (%d%%)\n",
313 (p->size > 0) ? (int)((p->synced * 100) / p->size) :
316 sbuf_printf(sb, "\t\tOrganization: %s", gv_plexorg(p->org));
317 if (gv_is_striped(p)) {
318 sbuf_printf(sb, "\tStripe size: %s\n",
319 gv_roughlength(p->stripesize, 1));
321 sbuf_printf(sb, "\t\tFlags: %d\n", p->flags);
322 if (p->vol_sc != NULL) {
323 sbuf_printf(sb, "\t\tPart of volume %s\n", p->volume);
326 sbuf_printf(sb, "P %-18s %2s State: ", p->name,
327 gv_plexorg_short(p->org));
328 if ((p->flags & GV_PLEX_SYNCING) ||
329 (p->flags & GV_PLEX_GROWING) ||
330 (p->flags & GV_PLEX_REBUILDING)) {
331 sbuf_printf(sb, "S %d%%\t", (int)((p->synced * 100) /
334 sbuf_printf(sb, "%s\t", gv_plexstate(p->state));
336 sbuf_printf(sb, "Subdisks: %5d\tSize: %s\n", p->sdcount,
337 gv_roughlength(p->size, 0));
340 if (flags & GV_FLAG_VV) {
342 LIST_FOREACH(s, &p->subdisks, in_plex) {
343 sbuf_printf(sb, "\t\tSubdisk %d:\t%s\n", i, s->name);
344 sbuf_printf(sb, "\t\t state: %s\tsize %11jd "
345 "(%jd MB)\n", gv_sdstate(s->state),
346 (intmax_t)s->size, (intmax_t)s->size / MEGABYTE);
347 if (p->org == GV_PLEX_CONCAT) {
348 sbuf_printf(sb, "\t\t\toffset %9jd (0x%jx)\n",
349 (intmax_t)s->plex_offset,
350 (intmax_t)s->plex_offset);
356 if (flags & GV_FLAG_R) {
357 LIST_FOREACH(s, &p->subdisks, in_plex)
358 gv_lsi(s, sb, flags);
362 /* List one or more subdisks. */
364 gv_ls(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb)
373 LIST_FOREACH(s, &sc->subdisks, sd)
376 sbuf_printf(sb, "%d subdisk%s:\n", i, i == 1 ? "" : "s");
379 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
380 LIST_FOREACH(s, &sc->subdisks, sd)
381 gv_lsi(s, sb, *flags);
385 /* List a single subdisk. */
387 gv_lsi(struct gv_sd *s, struct sbuf *sb, int flags)
389 if (flags & GV_FLAG_V) {
390 sbuf_printf(sb, "Subdisk %s:\n", s->name);
391 sbuf_printf(sb, "\t\tSize: %16jd bytes (%jd MB)\n",
392 (intmax_t)s->size, (intmax_t)s->size / MEGABYTE);
393 sbuf_printf(sb, "\t\tState: %s\n", gv_sdstate(s->state));
395 if (s->state == GV_SD_INITIALIZING ||
396 s->state == GV_SD_REVIVING) {
397 if (s->state == GV_SD_INITIALIZING)
398 sbuf_printf(sb, "\t\tInitialized: ");
400 sbuf_printf(sb, "\t\tRevived: ");
402 sbuf_printf(sb, "%16jd bytes (%d%%)\n",
403 (intmax_t)s->initialized,
404 (int)((s->initialized * 100) / s->size));
407 if (s->plex_sc != NULL) {
408 sbuf_printf(sb, "\t\tPlex %s at offset %jd (%s)\n",
409 s->plex, (intmax_t)s->plex_offset,
410 gv_roughlength(s->plex_offset, 1));
413 sbuf_printf(sb, "\t\tDrive %s (%s) at offset %jd (%s)\n",
415 s->drive_sc == NULL ? "*missing*" : s->drive_sc->name,
416 (intmax_t)s->drive_offset,
417 gv_roughlength(s->drive_offset, 1));
418 sbuf_printf(sb, "\t\tFlags: %d\n", s->flags);
420 sbuf_printf(sb, "S %-21s State: ", s->name);
421 if (s->state == GV_SD_INITIALIZING ||
422 s->state == GV_SD_REVIVING) {
423 if (s->state == GV_SD_INITIALIZING)
424 sbuf_printf(sb, "I ");
426 sbuf_printf(sb, "R ");
427 sbuf_printf(sb, "%d%%\t",
428 (int)((s->initialized * 100) / s->size));
430 sbuf_printf(sb, "%s\t", gv_sdstate(s->state));
432 sbuf_printf(sb, "D: %-12s Size: %s\n", s->drive,
433 gv_roughlength(s->size, 0));
437 /* List one or more drives. */
439 gv_ld(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb)
448 LIST_FOREACH(d, &sc->drives, drive)
451 sbuf_printf(sb, "%d drive%s:\n", i, i == 1 ? "" : "s");
454 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
455 LIST_FOREACH(d, &sc->drives, drive)
456 gv_ldi(d, sb, *flags);
460 /* List a single drive. */
462 gv_ldi(struct gv_drive *d, struct sbuf *sb, int flags)
464 struct gv_freelist *fl;
467 /* Verbose listing. */
468 if (flags & GV_FLAG_V) {
469 sbuf_printf(sb, "Drive %s:\tDevice %s\n", d->name, d->device);
470 sbuf_printf(sb, "\t\tSize: %16jd bytes (%jd MB)\n",
471 (intmax_t)d->size, (intmax_t)d->size / MEGABYTE);
472 sbuf_printf(sb, "\t\tUsed: %16jd bytes (%jd MB)\n",
473 (intmax_t)d->size - d->avail,
474 (intmax_t)(d->size - d->avail) / MEGABYTE);
475 sbuf_printf(sb, "\t\tAvailable: %11jd bytes (%jd MB)\n",
476 (intmax_t)d->avail, (intmax_t)d->avail / MEGABYTE);
477 sbuf_printf(sb, "\t\tState: %s\n", gv_drivestate(d->state));
478 sbuf_printf(sb, "\t\tFlags: %d\n", d->flags);
480 /* Be very verbose. */
481 if (flags & GV_FLAG_VV) {
482 sbuf_printf(sb, "\t\tFree list contains %d entries:\n",
483 d->freelist_entries);
484 sbuf_printf(sb, "\t\t Offset\t Size\n");
485 LIST_FOREACH(fl, &d->freelist, freelist)
486 sbuf_printf(sb, "\t\t%9jd\t%9jd\n",
487 (intmax_t)fl->offset, (intmax_t)fl->size);
490 sbuf_printf(sb, "D %-21s State: %s\t/dev/%s\tA: %jd/%jd MB "
491 "(%d%%)\n", d->name, gv_drivestate(d->state), d->device,
492 (intmax_t)d->avail / MEGABYTE, (intmax_t)d->size / MEGABYTE,
493 d->size > 0 ? (int)((d->avail * 100) / d->size) : 0);
496 /* Recursive listing. */
497 if (flags & GV_FLAG_R) {
498 LIST_FOREACH(s, &d->subdisks, from_drive)
499 gv_lsi(s, sb, flags);