2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2004, 2007 Lukas Ertl
5 * Copyright (c) 1997, 1998, 1999
6 * Nan Yang Computer Services Limited. All rights reserved.
8 * Parts written by Greg Lehey
10 * This software is distributed under the so-called ``Berkeley
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Nan Yang Computer
25 * 4. Neither the name of the Company nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * This software is provided ``as is'', and any express or implied
30 * warranties, including, but not limited to, the implied warranties of
31 * merchantability and fitness for a particular purpose are disclaimed.
32 * In no event shall the company or contributors be liable for any
33 * direct, indirect, incidental, special, exemplary, or consequential
34 * damages (including, but not limited to, procurement of substitute
35 * goods or services; loss of use, data, or profits; or business
36 * interruption) however caused and on any theory of liability, whether
37 * in contract, strict liability, or tort (including negligence or
38 * otherwise) arising in any way out of the use of this software, even if
39 * advised of the possibility of such damage.
43 /* This file is shared between kernel and userland. */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
50 #include <sys/malloc.h>
51 #include <sys/systm.h>
53 #include <geom/geom.h>
54 #define iswhite(c) (((c) == ' ') || ((c) == '\t'))
61 #define iswhite isspace
65 #include <sys/mutex.h>
66 #include <sys/queue.h>
68 #include <geom/vinum/geom_vinum_var.h>
69 #include <geom/vinum/geom_vinum_share.h>
72 * Take a blank separated list of tokens and turn it into a list of
73 * individual nul-delimited strings. Build a list of pointers at
74 * token, which must have enough space for the tokens. Return the
75 * number of tokens, or -1 on error (typically a missing string
79 gv_tokenize(char *cptr, char *token[], int maxtoken)
81 int tokennr; /* Index of this token. */
82 char delim; /* Delimiter for searching for the partner. */
84 for (tokennr = 0; tokennr < maxtoken;) {
86 /* Skip leading white space. */
87 while (iswhite(*cptr))
91 if ((*cptr == '\0') || (*cptr == '\n') || (*cptr == '#'))
95 token[tokennr] = cptr; /* Point to it. */
96 tokennr++; /* One more. */
98 /* Run off the end? */
99 if (tokennr == maxtoken)
103 if ((delim == '\'') || (delim == '"')) {
107 /* Found the partner. */
108 if ((*cptr == delim) && (cptr[-1] != '\\')) {
111 /* Space after closing quote needed. */
119 } else if ((*cptr == '\0') || (*cptr == '\n'))
125 while ((*cptr != '\0') &&
130 /* Not end-of-line; delimit and move to the next. */
136 /* Can't get here. */
142 * Take a number with an optional scale factor and convert it to a number of
145 * The scale factors are:
147 * s sectors (of 512 bytes)
148 * b blocks (of 512 bytes). This unit is deprecated, because it's
149 * confusing, but maintained to avoid confusing Veritas users.
150 * k kilobytes (1024 bytes)
151 * m megabytes (of 1024 * 1024 bytes)
152 * g gigabytes (of 1024 * 1024 * 1024 bytes)
154 * XXX: need a way to signal error
157 gv_sizespec(char *spec)
165 if (spec != NULL) { /* we have a parameter */
167 if (*s == '-') { /* negative, */
173 if ((*s >= '0') && (*s <= '9')) {
176 while ((*s >= '0') && (*s <= '9'))
178 size = size * 10 + *s++ - '0';
188 return size * sign * 512;
192 return size * sign * 1024;
196 return size * sign * 1024 * 1024;
200 return size * sign * 1024 * 1024 * 1024;
209 gv_drivestate(int state)
222 gv_drivestatei(char *buf)
224 if (!strcmp(buf, "up"))
225 return (GV_DRIVE_UP);
227 return (GV_DRIVE_DOWN);
230 /* Translate from a string to a subdisk state. */
232 gv_sdstatei(char *buf)
234 if (!strcmp(buf, "up"))
236 else if (!strcmp(buf, "reviving"))
237 return (GV_SD_REVIVING);
238 else if (!strcmp(buf, "initializing"))
239 return (GV_SD_INITIALIZING);
240 else if (!strcmp(buf, "stale"))
241 return (GV_SD_STALE);
246 /* Translate from a subdisk state to a string. */
248 gv_sdstate(int state)
251 case GV_SD_INITIALIZING:
252 return "initializing";
266 /* Translate from a string to a plex state. */
268 gv_plexstatei(char *buf)
270 if (!strcmp(buf, "up"))
272 else if (!strcmp(buf, "initializing"))
273 return (GV_PLEX_INITIALIZING);
274 else if (!strcmp(buf, "degraded"))
275 return (GV_PLEX_DEGRADED);
276 else if (!strcmp(buf, "growable"))
277 return (GV_PLEX_GROWABLE);
279 return (GV_PLEX_DOWN);
282 /* Translate from a plex state to a string. */
284 gv_plexstate(int state)
289 case GV_PLEX_INITIALIZING:
290 return "initializing";
291 case GV_PLEX_DEGRADED:
293 case GV_PLEX_GROWABLE:
302 /* Translate from a string to a plex organization. */
304 gv_plexorgi(char *buf)
306 if (!strcmp(buf, "concat"))
307 return (GV_PLEX_CONCAT);
308 else if (!strcmp(buf, "striped"))
309 return (GV_PLEX_STRIPED);
310 else if (!strcmp(buf, "raid5"))
311 return (GV_PLEX_RAID5);
313 return (GV_PLEX_DISORG);
317 gv_volstatei(char *buf)
319 if (!strcmp(buf, "up"))
322 return (GV_VOL_DOWN);
326 gv_volstate(int state)
338 /* Translate from a plex organization to a string. */
347 case GV_PLEX_STRIPED:
357 gv_plexorg_short(int org)
364 case GV_PLEX_STRIPED:
379 s = g_malloc(sizeof(struct gv_sd), M_NOWAIT);
381 s = malloc(sizeof(struct gv_sd));
385 bzero(s, sizeof(struct gv_sd));
388 s->drive_offset = -1;
398 d = g_malloc(sizeof(struct gv_drive), M_NOWAIT);
400 d = malloc(sizeof(struct gv_drive));
404 bzero(d, sizeof(struct gv_drive));
409 gv_alloc_volume(void)
414 v = g_malloc(sizeof(struct gv_volume), M_NOWAIT);
416 v = malloc(sizeof(struct gv_volume));
420 bzero(v, sizeof(struct gv_volume));
430 p = g_malloc(sizeof(struct gv_plex), M_NOWAIT);
432 p = malloc(sizeof(struct gv_plex));
436 bzero(p, sizeof(struct gv_plex));
440 /* Get a new drive object. */
442 gv_new_drive(int max, char *token[])
448 if (token[1] == NULL || *token[1] == '\0')
450 d = gv_alloc_drive();
454 for (j = 1; j < max; j++) {
455 if (!strcmp(token[j], "state")) {
461 d->state = gv_drivestatei(token[j]);
462 } else if (!strcmp(token[j], "device")) {
470 if (strncmp(ptr, _PATH_DEV, 5) == 0)
472 strlcpy(d->device, ptr, sizeof(d->device));
474 /* We assume this is the drive name. */
475 strlcpy(d->name, token[j], sizeof(d->name));
479 if (strlen(d->name) == 0 || strlen(d->device) == 0)
490 /* Get a new volume object. */
492 gv_new_volume(int max, char *token[])
497 if (token[1] == NULL || *token[1] == '\0')
500 v = gv_alloc_volume();
505 for (j = 1; j < max; j++) {
506 if (!strcmp(token[j], "state")) {
512 v->state = gv_volstatei(token[j]);
514 /* We assume this is the volume name. */
515 strlcpy(v->name, token[j], sizeof(v->name));
519 if (strlen(v->name) == 0)
530 /* Get a new plex object. */
532 gv_new_plex(int max, char *token[])
537 if (token[1] == NULL || *token[1] == '\0')
545 for (j = 1; j < max; j++) {
546 if (!strcmp(token[j], "name")) {
552 strlcpy(p->name, token[j], sizeof(p->name));
553 } else if (!strcmp(token[j], "org")) {
559 p->org = gv_plexorgi(token[j]);
560 if ((p->org == GV_PLEX_RAID5) ||
561 (p->org == GV_PLEX_STRIPED)) {
567 p->stripesize = gv_sizespec(token[j]);
568 if (p->stripesize == 0) {
573 } else if (!strcmp(token[j], "state")) {
579 p->state = gv_plexstatei(token[j]);
580 } else if (!strcmp(token[j], "vol") ||
581 !strcmp(token[j], "volume")) {
587 strlcpy(p->volume, token[j], sizeof(p->volume));
604 /* Get a new subdisk object. */
606 gv_new_sd(int max, char *token[])
611 if (token[1] == NULL || *token[1] == '\0')
619 for (j = 1; j < max; j++) {
620 if (!strcmp(token[j], "name")) {
626 strlcpy(s->name, token[j], sizeof(s->name));
627 } else if (!strcmp(token[j], "drive")) {
633 strlcpy(s->drive, token[j], sizeof(s->drive));
634 } else if (!strcmp(token[j], "plex")) {
640 strlcpy(s->plex, token[j], sizeof(s->plex));
641 } else if (!strcmp(token[j], "state")) {
647 s->state = gv_sdstatei(token[j]);
648 } else if (!strcmp(token[j], "len") ||
649 !strcmp(token[j], "length")) {
655 s->size = gv_sizespec(token[j]);
658 } else if (!strcmp(token[j], "driveoffset")) {
664 s->drive_offset = gv_sizespec(token[j]);
665 if (s->drive_offset != 0 &&
666 s->drive_offset < GV_DATA_START) {
670 } else if (!strcmp(token[j], "plexoffset")) {
676 s->plex_offset = gv_sizespec(token[j]);
677 if (s->plex_offset < 0) {
687 if (strlen(s->drive) == 0)
699 * Take a size in bytes and return a pointer to a string which represents the
700 * size best. If lj is != 0, return left justified, otherwise in a fixed 10
701 * character field suitable for columnar printing.
703 * Note this uses a static string: it's only intended to be used immediately
707 gv_roughlength(off_t bytes, int lj)
709 static char desc[16];
712 if (bytes > (off_t)MEGABYTE * 10000)
713 snprintf(desc, sizeof(desc), lj ? "%jd GB" : "%10jd GB",
717 else if (bytes > KILOBYTE * 10000)
718 snprintf(desc, sizeof(desc), lj ? "%jd MB" : "%10jd MB",
722 else if (bytes > 10000)
723 snprintf(desc, sizeof(desc), lj ? "%jd kB" : "%10jd kB",
728 snprintf(desc, sizeof(desc), lj ? "%jd B" : "%10jd B", bytes);