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. 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 20: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>
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 */
51 drive_state(enum drivestate state
)
53 if (((unsigned) state
) >= STATECOUNT(drive
)) {
54 sprintf(numeric_state
, "Invalid state %d", (int) state
);
57 return drivestatetext
[state
];
60 /* Return volume state as a string */
62 volume_state(enum volumestate state
)
64 if (((unsigned) state
) >= STATECOUNT(vol
)) {
65 sprintf(numeric_state
, "Invalid state %d", (int) state
);
68 return volstatetext
[state
];
71 /* Return plex state as a string */
73 plex_state(enum plexstate state
)
75 if (((unsigned) state
) >= STATECOUNT(plex
)) {
76 sprintf(numeric_state
, "Invalid state %d", (int) state
);
79 return plexstatetext
[state
];
82 /* Return plex organization as a string */
84 plex_org(enum plexorg org
)
87 case plex_disorg
: /* disorganized */
91 case plex_concat
: /* concatenated plex */
95 case plex_striped
: /* striped plex */
99 case plex_raid4
: /* RAID-4 plex */
102 case plex_raid5
: /* RAID-5 plex */
107 sprintf(numeric_state
, "Invalid org %d", (int) org
);
108 return numeric_state
;
112 /* Return sd state as a string */
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
;
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
129 DriveState(char *text
)
132 for (i
= 0; i
< STATECOUNT(drive
); i
++)
133 if (strcmp(text
, drivestatetext
[i
]) == 0) /* found it */
134 return (enum drivestate
) i
;
142 for (i
= 0; i
< STATECOUNT(sd
); i
++)
143 if (strcmp(text
, sdstatetext
[i
]) == 0) /* found it */
144 return (enum sdstate
) i
;
149 PlexState(char *text
)
152 for (i
= 0; i
< STATECOUNT(plex
); i
++)
153 if (strcmp(text
, plexstatetext
[i
]) == 0) /* found it */
154 return (enum plexstate
) i
;
162 for (i
= 0; i
< STATECOUNT(vol
); i
++)
163 if (strcmp(text
, volstatetext
[i
]) == 0) /* found it */
164 return (enum volumestate
) i
;
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)
187 int sign
= 1; /* -1 if negative */
190 if (spec
!= NULL
) { /* we have a parameter */
192 if (*s
== '-') { /* negative, */
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 */
207 return size
* sign
* 512;
211 return size
* sign
* 1024;
215 return size
* sign
* 1024 * 1024;
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.
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.
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
:
254 case VINUM_PLEX_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.
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
:
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 */