Add a PAM config for kde.
[dragonfly.git] / sbin / vinum / vinumutil.c
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. 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.
21  *
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.
33  *
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 $
37  */
38
39 /* This file contains utility routines used both in kernel and user context */
40
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 */
46
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)
52 {
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];
58 }
59
60 /* Return volume state as a string */
61 char *
62 volume_state(enum volumestate state)
63 {
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];
69 }
70
71 /* Return plex state as a string */
72 char *
73 plex_state(enum plexstate state)
74 {
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];
80 }
81
82 /* Return plex organization as a string */
83 char *
84 plex_org(enum plexorg org)
85 {
86     switch (org) {
87     case plex_disorg:                                       /* disorganized */
88         return "disorg";
89         break;
90
91     case plex_concat:                                       /* concatenated plex */
92         return "concat";
93         break;
94
95     case plex_striped:                                      /* striped plex */
96         return "striped";
97         break;
98
99     case plex_raid4:                                        /* RAID-4 plex */
100         return "raid4";
101
102     case plex_raid5:                                        /* RAID-5 plex */
103         return "raid5";
104         break;
105
106     default:
107         sprintf(numeric_state, "Invalid org %d", (int) org);
108         return numeric_state;
109     }
110 }
111
112 /* Return sd state as a string */
113 char *
114 sd_state(enum sdstate state)
115 {
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];
121 }
122
123 /* Now convert in the other direction */
124 /*
125  * These are currently used only internally,
126  * so we don't do too much error checking
127  */
128 enum drivestate
129 DriveState(char *text)
130 {
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;
136 }
137
138 enum sdstate
139 SdState(char *text)
140 {
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;
146 }
147
148 enum plexstate
149 PlexState(char *text)
150 {
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;
156 }
157
158 enum volumestate
159 VolState(char *text)
160 {
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;
166 }
167
168 /*
169  * Take a number with an optional scale factor and convert
170  * it to a number of bytes.
171  *
172  * The scale factors are:
173  *
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)
181  */
182 u_int64_t
183 sizespec(char *spec)
184 {
185     u_int64_t size;
186     char *s;
187     int sign = 1;                                           /* -1 if negative */
188
189     size = 0;
190     if (spec != NULL) {                                     /* we have a parameter */
191         s = spec;
192         if (*s == '-') {                                    /* negative, */
193             sign = -1;
194             s++;                                            /* skip */
195         }
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;
202
203             case 'B':
204             case 'b':
205             case 'S':
206             case 's':
207                 return size * sign * 512;
208
209             case 'K':
210             case 'k':
211                 return size * sign * 1024;
212
213             case 'M':
214             case 'm':
215                 return size * sign * 1024 * 1024;
216
217             case 'G':
218             case 'g':
219                 return size * sign * 1024 * 1024 * 1024;
220             }
221         }
222         fprintf(stderr, "Invalid length specification: %s", spec);
223         longjmp(command_fail, -1);
224     }
225     fprintf(stderr, "Missing length specification");
226     longjmp(command_fail, -1);
227 }
228
229 /*
230  * Extract the volume number from a device number.
231  * Perform no checking.
232  */
233 int
234 Volno(dev_t dev)
235 {
236     return (minor(dev) & MASK(VINUM_VOL_WIDTH)) >> VINUM_VOL_SHIFT;
237 }
238
239 /*
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.
243  */
244 int
245 Plexno(dev_t dev)
246 {
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;
253
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))];
257
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 */
263     }
264     return 0;                                               /* compiler paranoia */
265 }
266
267 /*
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.
271  */
272 int
273 Sdno(dev_t dev)
274 {
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;
282
283     case VINUM_SD_TYPE:
284         return PLEX[Plexno(dev)].sdnos[(minor(dev) >> VINUM_SD_SHIFT) & (MASK(VINUM_SD_WIDTH))];
285
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 */
290     }
291     return -1;                                              /* compiler paranoia */
292 }