xref: /dragonfly/sys/dev/raid/vinum/vinumutil.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
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.14 1999/12/30 07:04:02 grog Exp grog $
39  * $FreeBSD: src/sys/dev/vinum/vinumutil.c,v 1.15 2000/02/29 06:16:44 grog Exp $
40  * $DragonFly: src/sys/dev/raid/vinum/vinumutil.c,v 1.6 2006/12/20 18:14:40 dillon Exp $
41  */
42 
43 /* This file contains utility routines used both in kernel and user context */
44 
45 #include "vinumhdr.h"
46 #include "statetexts.h"
47 
48 #ifdef _KERNEL
49 #define sprintf     ksprintf
50 #endif
51 
52 static char numeric_state[32];                                            /* temporary buffer for ASCII conversions */
53 #define STATECOUNT(x) (sizeof (x##statetext) / sizeof (char *))
54 /* Return drive state as a string */
55 char *
drive_state(enum drivestate state)56 drive_state(enum drivestate state)
57 {
58     if (((unsigned) state) >= STATECOUNT(drive)) {
59           sprintf(numeric_state, "Invalid state %d", (int) state);
60           return numeric_state;
61     } else
62           return drivestatetext[state];
63 }
64 
65 /* Return volume state as a string */
66 char *
volume_state(enum volumestate state)67 volume_state(enum volumestate state)
68 {
69     if (((unsigned) state) >= STATECOUNT(vol)) {
70           sprintf(numeric_state, "Invalid state %d", (int) state);
71           return numeric_state;
72     } else
73           return volstatetext[state];
74 }
75 
76 /* Return plex state as a string */
77 char *
plex_state(enum plexstate state)78 plex_state(enum plexstate state)
79 {
80     if (((unsigned) state) >= STATECOUNT(plex)) {
81           sprintf(numeric_state, "Invalid state %d", (int) state);
82           return numeric_state;
83     } else
84           return plexstatetext[state];
85 }
86 
87 /* Return plex organization as a string */
88 char *
plex_org(enum plexorg org)89 plex_org(enum plexorg org)
90 {
91     switch (org) {
92     case plex_disorg:                                                     /* disorganized */
93           return "disorg";
94           break;
95 
96     case plex_concat:                                                     /* concatenated plex */
97           return "concat";
98           break;
99 
100     case plex_striped:                                                    /* striped plex */
101           return "striped";
102           break;
103 
104     case plex_raid4:                                                      /* RAID-4 plex */
105           return "raid4";
106 
107     case plex_raid5:                                                      /* RAID-5 plex */
108           return "raid5";
109           break;
110 
111     default:
112           sprintf(numeric_state, "Invalid org %d", (int) org);
113           return numeric_state;
114     }
115 }
116 
117 /* Return sd state as a string */
118 char *
sd_state(enum sdstate state)119 sd_state(enum sdstate state)
120 {
121     if (((unsigned) state) >= STATECOUNT(sd)) {
122           sprintf(numeric_state, "Invalid state %d", (int) state);
123           return numeric_state;
124     } else
125           return sdstatetext[state];
126 }
127 
128 /* Now convert in the other direction */
129 /*
130  * These are currently used only internally,
131  * so we don't do too much error checking
132  */
133 enum drivestate
DriveState(char * text)134 DriveState(char *text)
135 {
136     int i;
137     for (i = 0; i < STATECOUNT(drive); i++)
138           if (strcmp(text, drivestatetext[i]) == 0)             /* found it */
139               return (enum drivestate) i;
140     return -1;
141 }
142 
143 enum sdstate
SdState(char * text)144 SdState(char *text)
145 {
146     int i;
147     for (i = 0; i < STATECOUNT(sd); i++)
148           if (strcmp(text, sdstatetext[i]) == 0)                /* found it */
149               return (enum sdstate) i;
150     return -1;
151 }
152 
153 enum plexstate
PlexState(char * text)154 PlexState(char *text)
155 {
156     int i;
157     for (i = 0; i < STATECOUNT(plex); i++)
158           if (strcmp(text, plexstatetext[i]) == 0)              /* found it */
159               return (enum plexstate) i;
160     return -1;
161 }
162 
163 enum volumestate
VolState(char * text)164 VolState(char *text)
165 {
166     int i;
167     for (i = 0; i < STATECOUNT(vol); i++)
168           if (strcmp(text, volstatetext[i]) == 0)               /* found it */
169               return (enum volumestate) i;
170     return -1;
171 }
172 
173 /*
174  * Take a number with an optional scale factor and convert
175  * it to a number of bytes.
176  *
177  * The scale factors are:
178  *
179  * s    sectors (of 512 bytes)
180  * b    blocks (of 512 bytes).  This unit is deprecated,
181  *      because it's confusing, but maintained to avoid
182  *      confusing Veritas users.
183  * k    kilobytes (1024 bytes)
184  * m    megabytes (of 1024 * 1024 bytes)
185  * g    gigabytes (of 1024 * 1024 * 1024 bytes)
186  */
187 u_int64_t
sizespec(char * spec)188 sizespec(char *spec)
189 {
190     u_int64_t size;
191     char *s;
192     int sign = 1;                                               /* -1 if negative */
193 
194     size = 0;
195     if (spec != NULL) {                                                   /* we have a parameter */
196           s = spec;
197           if (*s == '-') {                                      /* negative, */
198               sign = -1;
199               s++;                                              /* skip */
200           }
201           if ((*s >= '0') && (*s <= '9')) {                     /* it's numeric */
202               while ((*s >= '0') && (*s <= '9'))                /* it's numeric */
203                     size = size * 10 + *s++ - '0';                        /* convert it */
204               switch (*s) {
205               case '\0':
206                     return size * sign;
207 
208               case 'B':
209               case 'b':
210               case 'S':
211               case 's':
212                     return size * sign * 512;
213 
214               case 'K':
215               case 'k':
216                     return size * sign * 1024;
217 
218               case 'M':
219               case 'm':
220                     return size * sign * 1024 * 1024;
221 
222               case 'G':
223               case 'g':
224                     return size * sign * 1024 * 1024 * 1024;
225               }
226           }
227           throw_rude_remark(EINVAL, "Invalid length specification: %s", spec);
228     }
229     throw_rude_remark(EINVAL, "Missing length specification");
230     /* NOTREACHED */
231     return -1;
232 }
233 
234 /*
235  * Extract the volume number from a device number.
236  * Perform no checking.
237  */
238 int
Volno(cdev_t dev)239 Volno(cdev_t dev)
240 {
241     return (minor(dev) & MASK(VINUM_VOL_WIDTH)) >> VINUM_VOL_SHIFT;
242 }
243 
244 /*
245  * Extract a plex number from a device number.
246  * Don't check the major number, but check the
247  * type.  Return -1 for invalid types.
248  */
249 int
Plexno(cdev_t dev)250 Plexno(cdev_t dev)
251 {
252     switch (DEVTYPE(dev)) {
253     case VINUM_VOLUME_TYPE:
254     case VINUM_DRIVE_TYPE:
255     case VINUM_SUPERDEV_TYPE:                                   /* ordinary super device */
256     case VINUM_RAWSD_TYPE:
257           return -1;
258 
259     case VINUM_PLEX_TYPE:
260     case VINUM_SD_TYPE:
261           return VOL[Volno(dev)].plex[(minor(dev) >> VINUM_PLEX_SHIFT) & (MASK(VINUM_PLEX_WIDTH))];
262 
263     case VINUM_RAWPLEX_TYPE:
264           return ((minor(dev) & MASK(VINUM_VOL_WIDTH)) >> VINUM_VOL_SHIFT) /* low order 8 bits */
265           |((minor(dev) >> VINUM_RAWPLEX_SHIFT)
266               & (MASK(VINUM_RAWPLEX_WIDTH)
267                     << (VINUM_VOL_SHIFT + VINUM_VOL_WIDTH)));   /* upper 12 bits */
268     }
269     return 0;                                                             /* compiler paranoia */
270 }
271 
272 /*
273  * Extract a subdisk number from a device number.
274  * Don't check the major number, but check the
275  * type.  Return -1 for invalid types.
276  */
277 int
Sdno(cdev_t dev)278 Sdno(cdev_t dev)
279 {
280     switch (DEVTYPE(dev)) {
281     case VINUM_VOLUME_TYPE:
282     case VINUM_DRIVE_TYPE:
283     case VINUM_SUPERDEV_TYPE:                                   /* ordinary super device */
284     case VINUM_PLEX_TYPE:
285     case VINUM_RAWPLEX_TYPE:
286           return -1;
287 
288     case VINUM_SD_TYPE:
289           return PLEX[Plexno(dev)].sdnos[(minor(dev) >> VINUM_SD_SHIFT) & (MASK(VINUM_SD_WIDTH))];
290 
291     case VINUM_RAWSD_TYPE:
292           return ((minor(dev) & MASK(VINUM_VOL_WIDTH)) >> VINUM_VOL_SHIFT) /* low order 8 bits */
293           |((minor(dev) >> VINUM_RAWPLEX_SHIFT) & (MASK(VINUM_RAWPLEX_WIDTH)
294                     << (VINUM_VOL_SHIFT + VINUM_VOL_WIDTH)));   /* upper 12 bits */
295     }
296     return -1;                                                            /* compiler paranoia */
297 }
298