Sync zoneinfo database with tzdata2010e from elsie.
[dragonfly.git] / sbin / vinum / vinumutil.c
blob98925f9b594445300eb6cfd48f23f89fbbd2fbc8
1 /*-
2 * Copyright (c) 1997, 1998, 1999
3 * Nan Yang Computer Services Limited. All rights reserved.
5 * Written by Greg Lehey
7 * This software is distributed under the so-called ``Berkeley
8 * License'':
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the Company nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * This software is provided ``as is'', and any express or implied
23 * warranties, including, but not limited to, the implied warranties of
24 * merchantability and fitness for a particular purpose are disclaimed.
25 * In no event shall the company or contributors be liable for any
26 * direct, indirect, incidental, special, exemplary, or consequential
27 * damages (including, but not limited to, procurement of substitute
28 * goods or services; loss of use, data, or profits; or business
29 * interruption) however caused and on any theory of liability, whether
30 * in contract, strict liability, or tort (including negligence or
31 * otherwise) arising in any way out of the use of this software, even if
32 * advised of the possibility of such damage.
34 * $Id: vinumutil.c,v 1.14 1999/12/30 07:04:02 grog Exp grog $
35 * $FreeBSD: src/sys/dev/vinum/vinumutil.c,v 1.15 2000/02/29 06:16:44 grog Exp $
36 * $DragonFly: src/sbin/vinum/vinumutil.c,v 1.7 2007/01/20 19:20:39 dillon Exp $
39 /* This file contains utility routines used both in kernel and user context */
41 #include <dev/raid/vinum/vinumhdr.h>
42 #include <dev/raid/vinum/statetexts.h>
43 #include <stdio.h>
44 #include <string.h>
45 extern jmp_buf command_fail; /* return on a failed command */
47 static char numeric_state[32]; /* temporary buffer for ASCII conversions */
48 #define STATECOUNT(x) (sizeof (x##statetext) / sizeof (char *))
49 /* Return drive state as a string */
50 char *
51 drive_state(enum drivestate state)
53 if (((unsigned) state) >= STATECOUNT(drive)) {
54 sprintf(numeric_state, "Invalid state %d", (int) state);
55 return numeric_state;
56 } else
57 return drivestatetext[state];
60 /* Return volume state as a string */
61 char *
62 volume_state(enum volumestate state)
64 if (((unsigned) state) >= STATECOUNT(vol)) {
65 sprintf(numeric_state, "Invalid state %d", (int) state);
66 return numeric_state;
67 } else
68 return volstatetext[state];
71 /* Return plex state as a string */
72 char *
73 plex_state(enum plexstate state)
75 if (((unsigned) state) >= STATECOUNT(plex)) {
76 sprintf(numeric_state, "Invalid state %d", (int) state);
77 return numeric_state;
78 } else
79 return plexstatetext[state];
82 /* Return plex organization as a string */
83 char *
84 plex_org(enum plexorg org)
86 switch (org) {
87 case plex_disorg: /* disorganized */
88 return "disorg";
89 break;
91 case plex_concat: /* concatenated plex */
92 return "concat";
93 break;
95 case plex_striped: /* striped plex */
96 return "striped";
97 break;
99 case plex_raid4: /* RAID-4 plex */
100 return "raid4";
102 case plex_raid5: /* RAID-5 plex */
103 return "raid5";
104 break;
106 default:
107 sprintf(numeric_state, "Invalid org %d", (int) org);
108 return numeric_state;
112 /* Return sd state as a string */
113 char *
114 sd_state(enum sdstate state)
116 if (((unsigned) state) >= STATECOUNT(sd)) {
117 sprintf(numeric_state, "Invalid state %d", (int) state);
118 return numeric_state;
119 } else
120 return sdstatetext[state];
123 /* Now convert in the other direction */
125 * These are currently used only internally,
126 * so we don't do too much error checking
128 enum drivestate
129 DriveState(char *text)
131 int i;
132 for (i = 0; i < STATECOUNT(drive); i++)
133 if (strcmp(text, drivestatetext[i]) == 0) /* found it */
134 return (enum drivestate) i;
135 return -1;
138 enum sdstate
139 SdState(char *text)
141 int i;
142 for (i = 0; i < STATECOUNT(sd); i++)
143 if (strcmp(text, sdstatetext[i]) == 0) /* found it */
144 return (enum sdstate) i;
145 return -1;
148 enum plexstate
149 PlexState(char *text)
151 int i;
152 for (i = 0; i < STATECOUNT(plex); i++)
153 if (strcmp(text, plexstatetext[i]) == 0) /* found it */
154 return (enum plexstate) i;
155 return -1;
158 enum volumestate
159 VolState(char *text)
161 int i;
162 for (i = 0; i < STATECOUNT(vol); i++)
163 if (strcmp(text, volstatetext[i]) == 0) /* found it */
164 return (enum volumestate) i;
165 return -1;
169 * Take a number with an optional scale factor and convert
170 * it to a number of bytes.
172 * The scale factors are:
174 * s sectors (of 512 bytes)
175 * b blocks (of 512 bytes). This unit is deprecated,
176 * because it's confusing, but maintained to avoid
177 * confusing Veritas users.
178 * k kilobytes (1024 bytes)
179 * m megabytes (of 1024 * 1024 bytes)
180 * g gigabytes (of 1024 * 1024 * 1024 bytes)
182 u_int64_t
183 sizespec(char *spec)
185 u_int64_t size;
186 char *s;
187 int sign = 1; /* -1 if negative */
189 size = 0;
190 if (spec != NULL) { /* we have a parameter */
191 s = spec;
192 if (*s == '-') { /* negative, */
193 sign = -1;
194 s++; /* skip */
196 if ((*s >= '0') && (*s <= '9')) { /* it's numeric */
197 while ((*s >= '0') && (*s <= '9')) /* it's numeric */
198 size = size * 10 + *s++ - '0'; /* convert it */
199 switch (*s) {
200 case '\0':
201 return size * sign;
203 case 'B':
204 case 'b':
205 case 'S':
206 case 's':
207 return size * sign * 512;
209 case 'K':
210 case 'k':
211 return size * sign * 1024;
213 case 'M':
214 case 'm':
215 return size * sign * 1024 * 1024;
217 case 'G':
218 case 'g':
219 return size * sign * 1024 * 1024 * 1024;
222 fprintf(stderr, "Invalid length specification: %s", spec);
223 longjmp(command_fail, -1);
225 fprintf(stderr, "Missing length specification");
226 longjmp(command_fail, -1);
230 * Extract the volume number from a device number.
231 * Perform no checking.
234 Volno(dev_t dev)
236 return (minor(dev) & MASK(VINUM_VOL_WIDTH)) >> VINUM_VOL_SHIFT;
240 * Extract a plex number from a device number.
241 * Don't check the major number, but check the
242 * type. Return -1 for invalid types.
245 Plexno(dev_t dev)
247 switch (DEVTYPE(dev)) {
248 case VINUM_VOLUME_TYPE:
249 case VINUM_DRIVE_TYPE:
250 case VINUM_SUPERDEV_TYPE: /* ordinary super device */
251 case VINUM_RAWSD_TYPE:
252 return -1;
254 case VINUM_PLEX_TYPE:
255 case VINUM_SD_TYPE:
256 return VOL[Volno(dev)].plex[(minor(dev) >> VINUM_PLEX_SHIFT) & (MASK(VINUM_PLEX_WIDTH))];
258 case VINUM_RAWPLEX_TYPE:
259 return ((minor(dev) & MASK(VINUM_VOL_WIDTH)) >> VINUM_VOL_SHIFT) /* low order 8 bits */
260 |((minor(dev) >> VINUM_RAWPLEX_SHIFT)
261 & (MASK(VINUM_RAWPLEX_WIDTH)
262 << (VINUM_VOL_SHIFT + VINUM_VOL_WIDTH))); /* upper 12 bits */
264 return 0; /* compiler paranoia */
268 * Extract a subdisk number from a device number.
269 * Don't check the major number, but check the
270 * type. Return -1 for invalid types.
273 Sdno(dev_t dev)
275 switch (DEVTYPE(dev)) {
276 case VINUM_VOLUME_TYPE:
277 case VINUM_DRIVE_TYPE:
278 case VINUM_SUPERDEV_TYPE: /* ordinary super device */
279 case VINUM_PLEX_TYPE:
280 case VINUM_RAWPLEX_TYPE:
281 return -1;
283 case VINUM_SD_TYPE:
284 return PLEX[Plexno(dev)].sdnos[(minor(dev) >> VINUM_SD_SHIFT) & (MASK(VINUM_SD_WIDTH))];
286 case VINUM_RAWSD_TYPE:
287 return ((minor(dev) & MASK(VINUM_VOL_WIDTH)) >> VINUM_VOL_SHIFT) /* low order 8 bits */
288 |((minor(dev) >> VINUM_RAWPLEX_SHIFT) & (MASK(VINUM_RAWPLEX_WIDTH)
289 << (VINUM_VOL_SHIFT + VINUM_VOL_WIDTH))); /* upper 12 bits */
291 return -1; /* compiler paranoia */