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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
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.
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/sbin/vinum/vinumutil.c,v 1.6 2005/02/03 22:05:27 joerg Exp $
43 /* This file contains utility routines used both in kernel and user context */
45 #include <dev/raid/vinum/vinumhdr.h>
46 #include <dev/raid/vinum/statetexts.h>
49 extern jmp_buf command_fail
; /* return on a failed command */
51 static char numeric_state
[32]; /* temporary buffer for ASCII conversions */
52 #define STATECOUNT(x) (sizeof (x##statetext) / sizeof (char *))
53 /* Return drive state as a string */
55 drive_state(enum drivestate state
)
57 if (((unsigned) state
) >= STATECOUNT(drive
)) {
58 sprintf(numeric_state
, "Invalid state %d", (int) state
);
61 return drivestatetext
[state
];
64 /* Return volume state as a string */
66 volume_state(enum volumestate state
)
68 if (((unsigned) state
) >= STATECOUNT(vol
)) {
69 sprintf(numeric_state
, "Invalid state %d", (int) state
);
72 return volstatetext
[state
];
75 /* Return plex state as a string */
77 plex_state(enum plexstate state
)
79 if (((unsigned) state
) >= STATECOUNT(plex
)) {
80 sprintf(numeric_state
, "Invalid state %d", (int) state
);
83 return plexstatetext
[state
];
86 /* Return plex organization as a string */
88 plex_org(enum plexorg org
)
91 case plex_disorg
: /* disorganized */
95 case plex_concat
: /* concatenated plex */
99 case plex_striped
: /* striped plex */
103 case plex_raid4
: /* RAID-4 plex */
106 case plex_raid5
: /* RAID-5 plex */
111 sprintf(numeric_state
, "Invalid org %d", (int) org
);
112 return numeric_state
;
116 /* Return sd state as a string */
118 sd_state(enum sdstate state
)
120 if (((unsigned) state
) >= STATECOUNT(sd
)) {
121 sprintf(numeric_state
, "Invalid state %d", (int) state
);
122 return numeric_state
;
124 return sdstatetext
[state
];
127 /* Now convert in the other direction */
129 * These are currently used only internally,
130 * so we don't do too much error checking
133 DriveState(char *text
)
136 for (i
= 0; i
< STATECOUNT(drive
); i
++)
137 if (strcmp(text
, drivestatetext
[i
]) == 0) /* found it */
138 return (enum drivestate
) i
;
146 for (i
= 0; i
< STATECOUNT(sd
); i
++)
147 if (strcmp(text
, sdstatetext
[i
]) == 0) /* found it */
148 return (enum sdstate
) i
;
153 PlexState(char *text
)
156 for (i
= 0; i
< STATECOUNT(plex
); i
++)
157 if (strcmp(text
, plexstatetext
[i
]) == 0) /* found it */
158 return (enum plexstate
) i
;
166 for (i
= 0; i
< STATECOUNT(vol
); i
++)
167 if (strcmp(text
, volstatetext
[i
]) == 0) /* found it */
168 return (enum volumestate
) i
;
173 * Take a number with an optional scale factor and convert
174 * it to a number of bytes.
176 * The scale factors are:
178 * s sectors (of 512 bytes)
179 * b blocks (of 512 bytes). This unit is deprecated,
180 * because it's confusing, but maintained to avoid
181 * confusing Veritas users.
182 * k kilobytes (1024 bytes)
183 * m megabytes (of 1024 * 1024 bytes)
184 * g gigabytes (of 1024 * 1024 * 1024 bytes)
191 int sign
= 1; /* -1 if negative */
194 if (spec
!= NULL
) { /* we have a parameter */
196 if (*s
== '-') { /* negative, */
200 if ((*s
>= '0') && (*s
<= '9')) { /* it's numeric */
201 while ((*s
>= '0') && (*s
<= '9')) /* it's numeric */
202 size
= size
* 10 + *s
++ - '0'; /* convert it */
211 return size
* sign
* 512;
215 return size
* sign
* 1024;
219 return size
* sign
* 1024 * 1024;
223 return size
* sign
* 1024 * 1024 * 1024;
226 fprintf(stderr
, "Invalid length specification: %s", spec
);
227 longjmp(command_fail
, -1);
229 fprintf(stderr
, "Missing length specification");
230 longjmp(command_fail
, -1);
234 * Extract the volume number from a device number.
235 * Perform no checking.
240 return (minor(dev
) & MASK(VINUM_VOL_WIDTH
)) >> VINUM_VOL_SHIFT
;
244 * Extract a plex number from a device number.
245 * Don't check the major number, but check the
246 * type. Return -1 for invalid types.
251 switch (DEVTYPE(dev
)) {
252 case VINUM_VOLUME_TYPE
:
253 case VINUM_DRIVE_TYPE
:
254 case VINUM_SUPERDEV_TYPE
: /* ordinary super device */
255 case VINUM_RAWSD_TYPE
:
258 case VINUM_PLEX_TYPE
:
260 return VOL
[Volno(dev
)].plex
[(minor(dev
) >> VINUM_PLEX_SHIFT
) & (MASK(VINUM_PLEX_WIDTH
))];
262 case VINUM_RAWPLEX_TYPE
:
263 return ((minor(dev
) & MASK(VINUM_VOL_WIDTH
)) >> VINUM_VOL_SHIFT
) /* low order 8 bits */
264 |((minor(dev
) >> VINUM_RAWPLEX_SHIFT
)
265 & (MASK(VINUM_RAWPLEX_WIDTH
)
266 << (VINUM_VOL_SHIFT
+ VINUM_VOL_WIDTH
))); /* upper 12 bits */
268 return 0; /* compiler paranoia */
272 * Extract a subdisk number from a device number.
273 * Don't check the major number, but check the
274 * type. Return -1 for invalid types.
279 switch (DEVTYPE(dev
)) {
280 case VINUM_VOLUME_TYPE
:
281 case VINUM_DRIVE_TYPE
:
282 case VINUM_SUPERDEV_TYPE
: /* ordinary super device */
283 case VINUM_PLEX_TYPE
:
284 case VINUM_RAWPLEX_TYPE
:
288 return PLEX
[Plexno(dev
)].sdnos
[(minor(dev
) >> VINUM_SD_SHIFT
) & (MASK(VINUM_SD_WIDTH
))];
290 case VINUM_RAWSD_TYPE
:
291 return ((minor(dev
) & MASK(VINUM_VOL_WIDTH
)) >> VINUM_VOL_SHIFT
) /* low order 8 bits */
292 |((minor(dev
) >> VINUM_RAWPLEX_SHIFT
) & (MASK(VINUM_RAWPLEX_WIDTH
)
293 << (VINUM_VOL_SHIFT
+ VINUM_VOL_WIDTH
))); /* upper 12 bits */
295 return -1; /* compiler paranoia */