2 * Copyright (c) 2004, 2007 Lukas Ertl
3 * Copyright (c) 1997, 1998, 1999
4 * Nan Yang Computer Services Limited. All rights reserved.
6 * Parts written by Greg Lehey
8 * This software is distributed under the so-called ``Berkeley
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Nan Yang Computer
23 * 4. Neither the name of the Company nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * This software is provided ``as is'', and any express or implied
28 * warranties, including, but not limited to, the implied warranties of
29 * merchantability and fitness for a particular purpose are disclaimed.
30 * In no event shall the company or contributors be liable for any
31 * direct, indirect, incidental, special, exemplary, or consequential
32 * damages (including, but not limited to, procurement of substitute
33 * goods or services; loss of use, data, or profits; or business
34 * interruption) however caused and on any theory of liability, whether
35 * in contract, strict liability, or tort (including negligence or
36 * otherwise) arising in any way out of the use of this software, even if
37 * advised of the possibility of such damage.
41 /* This file is shared between kernel and userland. */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
48 #include <sys/malloc.h>
49 #include <sys/systm.h>
51 #include <geom/geom.h>
52 #define iswhite(c) (((c) == ' ') || ((c) == '\t'))
58 #define iswhite isspace
62 #include <sys/mutex.h>
63 #include <sys/queue.h>
65 #include <geom/vinum/geom_vinum_var.h>
66 #include <geom/vinum/geom_vinum_share.h>
69 * Take a blank separated list of tokens and turn it into a list of
70 * individual nul-delimited strings. Build a list of pointers at
71 * token, which must have enough space for the tokens. Return the
72 * number of tokens, or -1 on error (typically a missing string
76 gv_tokenize(char *cptr, char *token[], int maxtoken)
78 int tokennr; /* Index of this token. */
79 char delim; /* Delimiter for searching for the partner. */
81 for (tokennr = 0; tokennr < maxtoken;) {
83 /* Skip leading white space. */
84 while (iswhite(*cptr))
88 if ((*cptr == '\0') || (*cptr == '\n') || (*cptr == '#'))
92 token[tokennr] = cptr; /* Point to it. */
93 tokennr++; /* One more. */
95 /* Run off the end? */
96 if (tokennr == maxtoken)
100 if ((delim == '\'') || (delim == '"')) {
104 /* Found the partner. */
105 if ((*cptr == delim) && (cptr[-1] != '\\')) {
108 /* Space after closing quote needed. */
116 } else if ((*cptr == '\0') || (*cptr == '\n'))
122 while ((*cptr != '\0') &&
127 /* Not end-of-line; delimit and move to the next. */
133 /* Can't get here. */
139 * Take a number with an optional scale factor and convert it to a number of
142 * The scale factors are:
144 * s sectors (of 512 bytes)
145 * b blocks (of 512 bytes). This unit is deprecated, because it's
146 * confusing, but maintained to avoid confusing Veritas users.
147 * k kilobytes (1024 bytes)
148 * m megabytes (of 1024 * 1024 bytes)
149 * g gigabytes (of 1024 * 1024 * 1024 bytes)
151 * XXX: need a way to signal error
154 gv_sizespec(char *spec)
162 if (spec != NULL) { /* we have a parameter */
164 if (*s == '-') { /* negative, */
170 if ((*s >= '0') && (*s <= '9')) {
173 while ((*s >= '0') && (*s <= '9'))
175 size = size * 10 + *s++ - '0';
185 return size * sign * 512;
189 return size * sign * 1024;
193 return size * sign * 1024 * 1024;
197 return size * sign * 1024 * 1024 * 1024;
206 gv_drivestate(int state)
219 gv_drivestatei(char *buf)
221 if (!strcmp(buf, "up"))
222 return (GV_DRIVE_UP);
224 return (GV_DRIVE_DOWN);
227 /* Translate from a string to a subdisk state. */
229 gv_sdstatei(char *buf)
231 if (!strcmp(buf, "up"))
233 else if (!strcmp(buf, "reviving"))
234 return (GV_SD_REVIVING);
235 else if (!strcmp(buf, "initializing"))
236 return (GV_SD_INITIALIZING);
237 else if (!strcmp(buf, "stale"))
238 return (GV_SD_STALE);
243 /* Translate from a subdisk state to a string. */
245 gv_sdstate(int state)
248 case GV_SD_INITIALIZING:
249 return "initializing";
263 /* Translate from a string to a plex state. */
265 gv_plexstatei(char *buf)
267 if (!strcmp(buf, "up"))
269 else if (!strcmp(buf, "initializing"))
270 return (GV_PLEX_INITIALIZING);
271 else if (!strcmp(buf, "degraded"))
272 return (GV_PLEX_DEGRADED);
273 else if (!strcmp(buf, "growable"))
274 return (GV_PLEX_GROWABLE);
276 return (GV_PLEX_DOWN);
279 /* Translate from a plex state to a string. */
281 gv_plexstate(int state)
286 case GV_PLEX_INITIALIZING:
287 return "initializing";
288 case GV_PLEX_DEGRADED:
290 case GV_PLEX_GROWABLE:
299 /* Translate from a string to a plex organization. */
301 gv_plexorgi(char *buf)
303 if (!strcmp(buf, "concat"))
304 return (GV_PLEX_CONCAT);
305 else if (!strcmp(buf, "striped"))
306 return (GV_PLEX_STRIPED);
307 else if (!strcmp(buf, "raid5"))
308 return (GV_PLEX_RAID5);
310 return (GV_PLEX_DISORG);
314 gv_volstatei(char *buf)
316 if (!strcmp(buf, "up"))
319 return (GV_VOL_DOWN);
323 gv_volstate(int state)
335 /* Translate from a plex organization to a string. */
344 case GV_PLEX_STRIPED:
354 gv_plexorg_short(int org)
361 case GV_PLEX_STRIPED:
376 s = g_malloc(sizeof(struct gv_sd), M_NOWAIT);
378 s = malloc(sizeof(struct gv_sd));
382 bzero(s, sizeof(struct gv_sd));
385 s->drive_offset = -1;
395 d = g_malloc(sizeof(struct gv_drive), M_NOWAIT);
397 d = malloc(sizeof(struct gv_drive));
401 bzero(d, sizeof(struct gv_drive));
406 gv_alloc_volume(void)
411 v = g_malloc(sizeof(struct gv_volume), M_NOWAIT);
413 v = malloc(sizeof(struct gv_volume));
417 bzero(v, sizeof(struct gv_volume));
427 p = g_malloc(sizeof(struct gv_plex), M_NOWAIT);
429 p = malloc(sizeof(struct gv_plex));
433 bzero(p, sizeof(struct gv_plex));
437 /* Get a new drive object. */
439 gv_new_drive(int max, char *token[])
445 if (token[1] == NULL || *token[1] == '\0')
447 d = gv_alloc_drive();
451 for (j = 1; j < max; j++) {
452 if (!strcmp(token[j], "state")) {
458 d->state = gv_drivestatei(token[j]);
459 } else if (!strcmp(token[j], "device")) {
467 if (strncmp(ptr, "/dev/", 5) == 0)
469 strlcpy(d->device, ptr, sizeof(d->device));
471 /* We assume this is the drive name. */
472 strlcpy(d->name, token[j], sizeof(d->name));
476 if (strlen(d->name) == 0 || strlen(d->device) == 0)
487 /* Get a new volume object. */
489 gv_new_volume(int max, char *token[])
494 if (token[1] == NULL || *token[1] == '\0')
497 v = gv_alloc_volume();
502 for (j = 1; j < max; j++) {
503 if (!strcmp(token[j], "state")) {
509 v->state = gv_volstatei(token[j]);
511 /* We assume this is the volume name. */
512 strlcpy(v->name, token[j], sizeof(v->name));
516 if (strlen(v->name) == 0)
527 /* Get a new plex object. */
529 gv_new_plex(int max, char *token[])
534 if (token[1] == NULL || *token[1] == '\0')
542 for (j = 1; j < max; j++) {
543 if (!strcmp(token[j], "name")) {
549 strlcpy(p->name, token[j], sizeof(p->name));
550 } else if (!strcmp(token[j], "org")) {
556 p->org = gv_plexorgi(token[j]);
557 if ((p->org == GV_PLEX_RAID5) ||
558 (p->org == GV_PLEX_STRIPED)) {
564 p->stripesize = gv_sizespec(token[j]);
565 if (p->stripesize == 0) {
570 } else if (!strcmp(token[j], "state")) {
576 p->state = gv_plexstatei(token[j]);
577 } else if (!strcmp(token[j], "vol") ||
578 !strcmp(token[j], "volume")) {
584 strlcpy(p->volume, token[j], sizeof(p->volume));
601 /* Get a new subdisk object. */
603 gv_new_sd(int max, char *token[])
608 if (token[1] == NULL || *token[1] == '\0')
616 for (j = 1; j < max; j++) {
617 if (!strcmp(token[j], "name")) {
623 strlcpy(s->name, token[j], sizeof(s->name));
624 } else if (!strcmp(token[j], "drive")) {
630 strlcpy(s->drive, token[j], sizeof(s->drive));
631 } else if (!strcmp(token[j], "plex")) {
637 strlcpy(s->plex, token[j], sizeof(s->plex));
638 } else if (!strcmp(token[j], "state")) {
644 s->state = gv_sdstatei(token[j]);
645 } else if (!strcmp(token[j], "len") ||
646 !strcmp(token[j], "length")) {
652 s->size = gv_sizespec(token[j]);
655 } else if (!strcmp(token[j], "driveoffset")) {
661 s->drive_offset = gv_sizespec(token[j]);
662 if (s->drive_offset != 0 &&
663 s->drive_offset < GV_DATA_START) {
667 } else if (!strcmp(token[j], "plexoffset")) {
673 s->plex_offset = gv_sizespec(token[j]);
674 if (s->plex_offset < 0) {
684 if (strlen(s->drive) == 0)
696 * Take a size in bytes and return a pointer to a string which represents the
697 * size best. If lj is != 0, return left justified, otherwise in a fixed 10
698 * character field suitable for columnar printing.
700 * Note this uses a static string: it's only intended to be used immediately
704 gv_roughlength(off_t bytes, int lj)
706 static char desc[16];
709 if (bytes > (off_t)MEGABYTE * 10000)
710 snprintf(desc, sizeof(desc), lj ? "%jd GB" : "%10jd GB",
714 else if (bytes > KILOBYTE * 10000)
715 snprintf(desc, sizeof(desc), lj ? "%jd MB" : "%10jd MB",
719 else if (bytes > 10000)
720 snprintf(desc, sizeof(desc), lj ? "%jd kB" : "%10jd kB",
725 snprintf(desc, sizeof(desc), lj ? "%jd B" : "%10jd B", bytes);