1 /*-
2 * Copyright (c) 1997, 1998, 1999
3 * Nan Yang Computer Services Limited. All rights reserved.
4 *
5 * Written by Greg Lehey
6 *
7 * This software is distributed under the so-called ``Berkeley
8 * License'':
9 *
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Nan Yang Computer
21 * Services Limited.
22 * 4. Neither the name of the Company nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * This software is provided ``as is'', and any express or implied
27 * warranties, including, but not limited to, the implied warranties of
28 * merchantability and fitness for a particular purpose are disclaimed.
29 * In no event shall the company or contributors be liable for any
30 * direct, indirect, incidental, special, exemplary, or consequential
31 * damages (including, but not limited to, procurement of substitute
32 * goods or services; loss of use, data, or profits; or business
33 * interruption) however caused and on any theory of liability, whether
34 * in contract, strict liability, or tort (including negligence or
35 * otherwise) arising in any way out of the use of this software, even if
36 * advised of the possibility of such damage.
37 *
38 * $Id: vinumutil.c,v 1.17 2003/04/28 02:54:43 grog Exp $
39 */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 /* This file contains utility routines used both in kernel and user context */
45
46 #include <dev/vinum/vinumhdr.h>
47 #include <dev/vinum/statetexts.h>
48 #ifndef _KERNEL
49 #include <stdio.h>
50 #include <string.h>
51 extern jmp_buf command_fail; /* return on a failed command */
52 #endif
53
54 static char numeric_state[32]; /* temporary buffer for ASCII conversions */
55 #define STATECOUNT(x) (sizeof (x##statetext) / sizeof (char *))
56 /* Return drive state as a string */
57 char *
58 drive_state(enum drivestate state)
59 {
60 if (((unsigned) state) >= STATECOUNT(drive)) {
61 sprintf(numeric_state, "Invalid state %d", (int) state);
62 return numeric_state;
63 } else
64 return drivestatetext[state];
65 }
66
67 /* Return volume state as a string */
68 char *
69 volume_state(enum volumestate state)
70 {
71 if (((unsigned) state) >= STATECOUNT(vol)) {
72 sprintf(numeric_state, "Invalid state %d", (int) state);
73 return numeric_state;
74 } else
75 return volstatetext[state];
76 }
77
78 /* Return plex state as a string */
79 char *
80 plex_state(enum plexstate state)
81 {
82 if (((unsigned) state) >= STATECOUNT(plex)) {
83 sprintf(numeric_state, "Invalid state %d", (int) state);
84 return numeric_state;
85 } else
86 return plexstatetext[state];
87 }
88
89 /* Return plex organization as a string */
90 char *
91 plex_org(enum plexorg org)
92 {
93 switch (org) {
94 case plex_disorg: /* disorganized */
95 return "disorg";
96 break;
97
98 case plex_concat: /* concatenated plex */
99 return "concat";
100 break;
101
102 case plex_striped: /* striped plex */
103 return "striped";
104 break;
105
106 case plex_raid4: /* RAID-4 plex */
107 return "raid4";
108
109 case plex_raid5: /* RAID-5 plex */
110 return "raid5";
111 break;
112
113 default:
114 sprintf(numeric_state, "Invalid org %d", (int) org);
115 return numeric_state;
116 }
117 }
118
119 /* Return sd state as a string */
120 char *
121 sd_state(enum sdstate state)
122 {
123 if (((unsigned) state) >= STATECOUNT(sd)) {
124 sprintf(numeric_state, "Invalid state %d", (int) state);
125 return numeric_state;
126 } else
127 return sdstatetext[state];
128 }
129
130 /* Now convert in the other direction */
131 /*
132 * These are currently used only internally,
133 * so we don't do too much error checking
134 */
135 enum drivestate
136 DriveState(char *text)
137 {
138 int i;
139 for (i = 0; i < STATECOUNT(drive); i++)
140 if (strcmp(text, drivestatetext[i]) == 0) /* found it */
141 return (enum drivestate) i;
142 return -1;
143 }
144
145 enum sdstate
146 SdState(char *text)
147 {
148 int i;
149 for (i = 0; i < STATECOUNT(sd); i++)
150 if (strcmp(text, sdstatetext[i]) == 0) /* found it */
151 return (enum sdstate) i;
152 return -1;
153 }
154
155 enum plexstate
156 PlexState(char *text)
157 {
158 int i;
159 for (i = 0; i < STATECOUNT(plex); i++)
160 if (strcmp(text, plexstatetext[i]) == 0) /* found it */
161 return (enum plexstate) i;
162 return -1;
163 }
164
165 enum volumestate
166 VolState(char *text)
167 {
168 int i;
169 for (i = 0; i < STATECOUNT(vol); i++)
170 if (strcmp(text, volstatetext[i]) == 0) /* found it */
171 return (enum volumestate) i;
172 return -1;
173 }
174
175 /*
176 * Take a number with an optional scale factor and convert
177 * it to a number of bytes.
178 *
179 * The scale factors are:
180 *
181 * s sectors (of 512 bytes)
182 * b blocks (of 512 bytes). This unit is deprecated,
183 * because it's confusing, but maintained to avoid
184 * confusing Veritas users.
185 * k kilobytes (1024 bytes)
186 * m megabytes (of 1024 * 1024 bytes)
187 * g gigabytes (of 1024 * 1024 * 1024 bytes)
188 */
189 u_int64_t
190 sizespec(char *spec)
191 {
192 u_int64_t size;
193 char *s;
194 int sign = 1; /* -1 if negative */
195
196 size = 0;
197 if (spec != NULL) { /* we have a parameter */
198 s = spec;
199 if (*s == '-') { /* negative, */
200 sign = -1;
201 s++; /* skip */
202 }
203 if ((*s >= '') && (*s <= '9')) { /* it's numeric */
204 while ((*s >= '') && (*s <= '9')) /* it's numeric */
205 size = size * 10 + *s++ - ''; /* convert it */
206 switch (*s) {
207 case '\0':
208 return size * sign;
209
210 case 'B':
211 case 'b':
212 case 'S':
213 case 's':
214 return size * sign * 512;
215
216 case 'K':
217 case 'k':
218 return size * sign * 1024;
219
220 case 'M':
221 case 'm':
222 return size * sign * 1024 * 1024;
223
224 case 'G':
225 case 'g':
226 return size * sign * 1024 * 1024 * 1024;
227 }
228 }
229 #ifdef _KERNEL
230 throw_rude_remark(EINVAL, "Invalid length specification: %s", spec);
231 #else
232 fprintf(stderr, "Invalid length specification: %s", spec);
233 longjmp(command_fail, 1);
234 #endif
235 }
236 #ifdef _KERNEL
237 throw_rude_remark(EINVAL, "Missing length specification");
238 #else
239 fprintf(stderr, "Missing length specification");
240 longjmp(command_fail, 1);
241 #endif
242 /* NOTREACHED */
243 return -1;
244 }
245
246 #ifdef _KERNEL
247 #define FOOTYPE struct cdev *
248 #else
249 #define FOOTYPE dev_t
250 #endif
251 /*
252 * Extract the volume number from a device number. Check that it's
253 * the correct type, and that it isn't one of the superdevs.
254 */
255 int
256 Volno(FOOTYPE dev)
257 {
258 int volno = minor(dev);
259
260 if (OBJTYPE(dev) != VINUM_VOLUME_TYPE)
261 return -1;
262 else
263 volno = ((volno & 0x3fff0000) >> 8) | (volno & 0xff);
264 if ((volno == VINUM_SUPERDEV_VOL)
265 || (volno == VINUM_DAEMON_VOL))
266 return -1;
267 else
268 return volno;
269 }
270
271 /*
272 * Extract a plex number from a device number.
273 * Don't check the major number, but check the
274 * type. Return -1 for invalid types.
275 */
276 int
277 Plexno(FOOTYPE dev)
278 {
279 int plexno = minor(dev);
280
281 if (OBJTYPE(dev) != VINUM_PLEX_TYPE)
282 return -1;
283 else
284 return ((plexno & 0x3fff0000) >> 8) | (plexno & 0xff);
285 }
286
287 /*
288 * Extract a subdisk number from a device number.
289 * Don't check the major number, but check the
290 * type. Return -1 for invalid types.
291 */
292 int
293 Sdno(FOOTYPE dev)
294 {
295 int sdno = minor(dev);
296
297 /*
298 * Care: VINUM_SD_TYPE is 2 or 3, which is why we use < instead of
299 * !=. It's not clear that this makes any sense abstracting it to
300 * this level.
301 */
302 if (OBJTYPE(dev) < VINUM_SD_TYPE)
303 return -1;
304 else
305 /*
306 * Note that the number we return includes the low-order bit of the
307 * type field. This gives us twice as many potential subdisks as
308 * plexes or volumes.
309 */
310 return ((sdno & 0x7fff0000) >> 8) | (sdno & 0xff);
311 }
Cache object: bd1f965fd6ecaa31d6555e29961f8b3d
|