summaryrefslogtreecommitdiffstats
path: root/sys/geom/vinum/geom_vinum_list.c
blob: cb4ccfa5a45f34f95648dce2fc78808a15c8d515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*-
 *  Copyright (c) 2004 Lukas Ertl
 *  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/libkern.h>
#include <sys/malloc.h>

#include <geom/geom.h>
#include <geom/vinum/geom_vinum_var.h>
#include <geom/vinum/geom_vinum.h>
#include <geom/vinum/geom_vinum_share.h>

void	gv_lvi(struct gv_volume *, struct sbuf *, int);
void	gv_lpi(struct gv_plex *, struct sbuf *, int);
void	gv_lsi(struct gv_sd *, struct sbuf *, int);
void	gv_ldi(struct gv_drive *, struct sbuf *, int);

void
gv_list(struct g_geom *gp, struct gctl_req *req)
{
	struct gv_softc *sc;
	struct gv_drive *d;
	struct gv_plex *p;
	struct gv_sd *s;
	struct gv_volume *v;
	struct sbuf *sb;
	int *argc, i, *flags, type;
	char *arg, buf[20], *cmd;

	argc = gctl_get_paraml(req, "argc", sizeof(*argc));

	if (argc == NULL) {
		gctl_error(req, "no arguments given");
		return;
	}

	flags = gctl_get_paraml(req, "flags", sizeof(*flags));

	sc = gp->softc;

	sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);

	/* Figure out which command was given. */
	cmd = gctl_get_param(req, "cmd", NULL);

	/* List specific objects or everything. */
	if (!strcmp(cmd, "list") || !strcmp(cmd, "l")) {
		if (*argc) {
			for (i = 0; i < *argc; i++) {
				snprintf(buf, sizeof(buf), "argv%d", i);
				arg = gctl_get_param(req, buf, NULL);
				if (arg == NULL)
					continue;
				type = gv_object_type(sc, arg);
				switch (type) {
				case GV_TYPE_VOL:
					v = gv_find_vol(sc, arg);
					gv_lvi(v, sb, *flags);
					break;
				case GV_TYPE_PLEX:
					p = gv_find_plex(sc, arg);
					gv_lpi(p, sb, *flags);
					break;
				case GV_TYPE_SD:
					s = gv_find_sd(sc, arg);
					gv_lsi(s, sb, *flags);
					break;
				case GV_TYPE_DRIVE:
					d = gv_find_drive(sc, arg);
					gv_ldi(d, sb, *flags);
					break;
				default:
					gctl_error(req, "unknown object '%s'",
					    arg);
					break;
				}
			}
		} else {
			gv_ld(gp, req, sb);
			sbuf_printf(sb, "\n");
			gv_lv(gp, req, sb);
			sbuf_printf(sb, "\n");
			gv_lp(gp, req, sb);
			sbuf_printf(sb, "\n");
			gv_ls(gp, req, sb);
		}

	/* List drives. */
	} else if (!strcmp(cmd, "ld")) {
		if (*argc) {
			for (i = 0; i < *argc; i++) {
				snprintf(buf, sizeof(buf), "argv%d", i);
				arg = gctl_get_param(req, buf, NULL);
				if (arg == NULL)
					continue;
				type = gv_object_type(sc, arg);
				if (type != GV_TYPE_DRIVE) {
					gctl_error(req, "'%s' is not a drive",
					    arg);
					continue;
				} else {
					d = gv_find_drive(sc, arg);
					gv_ldi(d, sb, *flags);
				}
			}
		} else
			gv_ld(gp, req, sb);

	/* List volumes. */
	} else if (!strcmp(cmd, "lv")) {
		if (*argc) {
			for (i = 0; i < *argc; i++) {
				snprintf(buf, sizeof(buf), "argv%d", i);
				arg = gctl_get_param(req, buf, NULL);
				if (arg == NULL)
					continue;
				type = gv_object_type(sc, arg);
				if (type != GV_TYPE_VOL) {
					gctl_error(req, "'%s' is not a volume",
					    arg);
					continue;
				} else {
					v = gv_find_vol(sc, arg);
					gv_lvi(v, sb, *flags);
				}
			}
		} else
			gv_lv(gp, req, sb);

	/* List plexes. */
	} else if (!strcmp(cmd, "lp")) {
		if (*argc) {
			for (i = 0; i < *argc; i++) {
				snprintf(buf, sizeof(buf), "argv%d", i);
				arg = gctl_get_param(req, buf, NULL);
				if (arg == NULL)
					continue;
				type = gv_object_type(sc, arg);
				if (type != GV_TYPE_PLEX) {
					gctl_error(req, "'%s' is not a plex",
					    arg);
					continue;
				} else {
					p = gv_find_plex(sc, arg);
					gv_lpi(p, sb, *flags);
				}
			}
		} else
			gv_lp(gp, req, sb);

	/* List subdisks. */
	} else if (!strcmp(cmd, "ls")) {
		if (*argc) {
			for (i = 0; i < *argc; i++) {
				snprintf(buf, sizeof(buf), "argv%d", i);
				arg = gctl_get_param(req, buf, NULL);
				if (arg == NULL)
					continue;
				type = gv_object_type(sc, arg);
				if (type != GV_TYPE_SD) {
					gctl_error(req, "'%s' is not a subdisk",
					    arg);
					continue;
				} else {
					s = gv_find_sd(sc, arg);
					gv_lsi(s, sb, *flags);
				}
			}
		} else
			gv_ls(gp, req, sb);

	} else
		gctl_error(req, "unknown command '%s'", cmd);

	sbuf_finish(sb);
	gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1);
	sbuf_delete(sb);
}

/* List one or more volumes. */
void
gv_lv(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb)
{
	struct gv_softc *sc;
	struct gv_volume *v;
	int i, *flags;

	sc = gp->softc;
	i = 0;

	LIST_FOREACH(v, &sc->volumes, volume)
		i++;
	
	sbuf_printf(sb, "%d volume%s:\n", i, i == 1 ? "" : "s");

	if (i) {
		flags = gctl_get_paraml(req, "flags", sizeof(*flags));
		LIST_FOREACH(v, &sc->volumes, volume)
			gv_lvi(v, sb, *flags);
	}
}

/* List a single volume. */
void
gv_lvi(struct gv_volume *v, struct sbuf *sb, int flags)
{
	struct gv_plex *p;
	int i;

	if (flags & GV_FLAG_V) {
		sbuf_printf(sb, "Volume %s:\tSize: %jd bytes (%jd MB)\n",
		    v->name, (intmax_t)v->size, (intmax_t)v->size / MEGABYTE);
		sbuf_printf(sb, "\t\tState: %s\n", gv_volstate(v->state));
	} else {
		sbuf_printf(sb, "V %-21s State: %s\tPlexes: %7d\tSize: %s\n",
		    v->name, gv_volstate(v->state), v->plexcount,
		    gv_roughlength(v->size, 0));
	}

	if (flags & GV_FLAG_VV) {
		i = 0;
		LIST_FOREACH(p, &v->plexes, in_volume) {
			sbuf_printf(sb, "\t\tPlex %2d:\t%s\t(%s), %s\n", i,
			    p->name, gv_plexstate(p->state),
			    gv_roughlength(p->size, 0));
			i++;
		}
	}

	if (flags & GV_FLAG_R) {
		LIST_FOREACH(p, &v->plexes, in_volume)
			gv_lpi(p, sb, flags);
	}
}

/* List one or more plexes. */
void
gv_lp(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb)
{
	struct gv_softc *sc;
	struct gv_plex *p;
	int i, *flags;

	sc = gp->softc;
	i = 0;

	LIST_FOREACH(p, &sc->plexes, plex)
		i++;

	sbuf_printf(sb, "%d plex%s:\n", i, i == 1 ? "" : "es");

	if (i) {
		flags = gctl_get_paraml(req, "flags", sizeof(*flags));
		LIST_FOREACH(p, &sc->plexes, plex)
			gv_lpi(p, sb, *flags);
	}
}

/* List a single plex. */
void
gv_lpi(struct gv_plex *p, struct sbuf *sb, int flags)
{
	struct gv_sd *s;
	int i;

	if (flags & GV_FLAG_V) {
		sbuf_printf(sb, "Plex %s:\tSize:\t%9jd bytes (%jd MB)\n",
		    p->name, (intmax_t)p->size, (intmax_t)p->size / MEGABYTE);
		sbuf_printf(sb, "\t\tSubdisks: %8d\n", p->sdcount);
		sbuf_printf(sb, "\t\tState: %s\n\t\tOrganization: %s",
		    gv_plexstate(p->state), gv_plexorg(p->org));
		if (gv_is_striped(p)) {
			sbuf_printf(sb, "\tStripe size: %s\n",
			    gv_roughlength(p->stripesize, 1));
		}
		if (p->vol_sc != NULL) {
			sbuf_printf(sb, "\t\tPart of volume %s\n", p->volume);
		}
	} else {
		sbuf_printf(sb, "P %-18s %2s State: %s\tSubdisks: %5d"
		    "\tSize: %s\n", p->name, gv_plexorg_short(p->org),
		    gv_plexstate(p->state), p->sdcount,
		    gv_roughlength(p->size, 0));
	}

	if (flags & GV_FLAG_VV) {
		i = 0;
		LIST_FOREACH(s, &p->subdisks, in_plex) {
			sbuf_printf(sb, "\t\tSubdisk %d:\t%s\n", i, s->name);
			sbuf_printf(sb, "\t\t  state: %s\tsize %11jd "
			    "(%jd MB)\n", gv_sdstate(s->state),
			    (intmax_t)s->size, (intmax_t)s->size / MEGABYTE);
			if (p->org == GV_PLEX_CONCAT) {
				sbuf_printf(sb, "\t\t\toffset %9jd (0x%jx)\n",
				    (intmax_t)s->plex_offset,
				    (intmax_t)s->plex_offset);
			}
			i++;
		}
	}

	if (flags & GV_FLAG_R) {
		LIST_FOREACH(s, &p->subdisks, in_plex)
			gv_lsi(s, sb, flags);
	}
}

/* List one or more subdisks. */
void
gv_ls(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb)
{
	struct gv_softc *sc;
	struct gv_sd *s;
	int i, *flags;

	sc = gp->softc;
	i = 0;

	LIST_FOREACH(s, &sc->subdisks, sd)
		i++;
	
	sbuf_printf(sb, "%d subdisk%s:\n", i, i == 1 ? "" : "s");

	if (i) {
		flags = gctl_get_paraml(req, "flags", sizeof(*flags));
		LIST_FOREACH(s, &sc->subdisks, sd)
			gv_lsi(s, sb, *flags);
	}
}

/* List a single subdisk. */
void
gv_lsi(struct gv_sd *s, struct sbuf *sb, int flags)
{
	if (flags & GV_FLAG_V) {
		sbuf_printf(sb, "Subdisk %s:\n", s->name);
		sbuf_printf(sb, "\t\tSize: %16jd bytes (%jd MB)\n",
		    (intmax_t)s->size, (intmax_t)s->size / MEGABYTE);
		sbuf_printf(sb, "\t\tState: %s\n", gv_sdstate(s->state));

		if (s->state == GV_SD_INITIALIZING ||
		    s->state == GV_SD_REVIVING) {
			if (s->state == GV_SD_INITIALIZING)
				sbuf_printf(sb, "\t\tInitialized: ");
			else
				sbuf_printf(sb, "\t\tRevived: ");
				
			sbuf_printf(sb, "%16jd bytes (%d%%)\n",
			    (intmax_t)s->initialized,
			    (int)((s->initialized * 100) / s->size));
		}

		if (s->plex_sc != NULL) {
			sbuf_printf(sb, "\t\tPlex %s at offset %jd (%s)\n",
			    s->plex, (intmax_t)s->plex_offset,
			    gv_roughlength(s->plex_offset, 1));
		}

		sbuf_printf(sb, "\t\tDrive %s (%s) at offset %jd (%s)\n",
		    s->drive,
		    s->drive_sc == NULL ? "*missing*" : s->drive_sc->name,
		    (intmax_t)s->drive_offset,
		    gv_roughlength(s->drive_offset, 1));
	} else {
		sbuf_printf(sb, "S %-21s State: ", s->name);
		if (s->state == GV_SD_INITIALIZING ||
		    s->state == GV_SD_REVIVING) {
			if (s->state == GV_SD_INITIALIZING)
				sbuf_printf(sb, "I ");
			else
				sbuf_printf(sb, "R ");
			sbuf_printf(sb, "%d%%\t",
			    (int)((s->initialized * 100) / s->size));
		} else {
			sbuf_printf(sb, "%s\t", gv_sdstate(s->state));
		}
		sbuf_printf(sb, "D: %-12s Size: %s\n", s->drive,
		    gv_roughlength(s->size, 0));
	}
}

/* List one or more drives. */
void
gv_ld(struct g_geom *gp, struct gctl_req *req, struct sbuf *sb)
{
	struct gv_softc *sc;
	struct gv_drive *d;
	int i, *flags;

	sc = gp->softc;
	i = 0;

	LIST_FOREACH(d, &sc->drives, drive)
		i++;
	
	sbuf_printf(sb, "%d drive%s:\n", i, i == 1 ? "" : "s");

	if (i) {
		flags = gctl_get_paraml(req, "flags", sizeof(*flags));
		LIST_FOREACH(d, &sc->drives, drive)
			gv_ldi(d, sb, *flags);
	}
}

/* List a single drive. */
void
gv_ldi(struct gv_drive *d, struct sbuf *sb, int flags)
{
	struct gv_freelist *fl;
	struct gv_sd *s;

	/* Verbose listing. */
	if (flags & GV_FLAG_V) {
		sbuf_printf(sb, "Drive %s:\tDevice %s\n", d->name, d->device);
		sbuf_printf(sb, "\t\tSize: %16jd bytes (%jd MB)\n",
		    (intmax_t)d->size, (intmax_t)d->size / MEGABYTE);
		sbuf_printf(sb, "\t\tUsed: %16jd bytes (%jd MB)\n",
		    (intmax_t)d->size - d->avail,
		    (intmax_t)(d->size - d->avail) / MEGABYTE);
		sbuf_printf(sb, "\t\tAvailable: %11jd bytes (%jd MB)\n",
		    (intmax_t)d->avail, (intmax_t)d->avail / MEGABYTE);
		sbuf_printf(sb, "\t\tState: %s\n", gv_drivestate(d->state));

		/* Be very verbose. */
		if (flags & GV_FLAG_VV) {
			sbuf_printf(sb, "\t\tFree list contains %d entries:\n",
			    d->freelist_entries);
			sbuf_printf(sb, "\t\t   Offset\t     Size\n");
			LIST_FOREACH(fl, &d->freelist, freelist)
				sbuf_printf(sb, "\t\t%9jd\t%9jd\n",
				    (intmax_t)fl->offset, (intmax_t)fl->size);
		}
	} else {
		sbuf_printf(sb, "D %-21s State: %s\t/dev/%s\tA: %jd/%jd MB "
		    "(%d%%)\n", d->name, gv_drivestate(d->state), d->device,
		    (intmax_t)d->avail / MEGABYTE, (intmax_t)d->size / MEGABYTE,
		    (int)((d->avail * 100) / d->size));
	}

	/* Recursive listing. */
	if (flags & GV_FLAG_R) {
		LIST_FOREACH(s, &d->subdisks, from_drive)
			gv_lsi(s, sb, flags);
	}
}
OpenPOWER on IntegriCloud