summaryrefslogtreecommitdiffstats
path: root/sys/dev/vinum/vinumutil.c
blob: f63bbd7f308f1522b53bcdbb332db29284e81dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*-
 * Copyright (c) 1997, 1998, 1999
 *	Nan Yang Computer Services Limited.  All rights reserved.
 *
 *  Written by Greg Lehey
 *
 *  This software is distributed under the so-called ``Berkeley
 *  License'':
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Nan Yang Computer
 *      Services Limited.
 * 4. Neither the name of the Company nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * This software is provided ``as is'', and any express or implied
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose are disclaimed.
 * In no event shall the company or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even if
 * advised of the possibility of such damage.
 *
 * $Id: vinumutil.c,v 1.17 2003/04/28 02:54:43 grog Exp $
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/* This file contains utility routines used both in kernel and user context */

#include <dev/vinum/vinumhdr.h>
#include <dev/vinum/statetexts.h>
#ifndef _KERNEL
#include <stdio.h>
#include <string.h>
extern jmp_buf command_fail;				    /* return on a failed command */
#endif

static char numeric_state[32];				    /* temporary buffer for ASCII conversions */
#define STATECOUNT(x) (sizeof (x##statetext) / sizeof (char *))
/* Return drive state as a string */
char *
drive_state(enum drivestate state)
{
    if (((unsigned) state) >= STATECOUNT(drive)) {
	sprintf(numeric_state, "Invalid state %d", (int) state);
	return numeric_state;
    } else
	return drivestatetext[state];
}

/* Return volume state as a string */
char *
volume_state(enum volumestate state)
{
    if (((unsigned) state) >= STATECOUNT(vol)) {
	sprintf(numeric_state, "Invalid state %d", (int) state);
	return numeric_state;
    } else
	return volstatetext[state];
}

/* Return plex state as a string */
char *
plex_state(enum plexstate state)
{
    if (((unsigned) state) >= STATECOUNT(plex)) {
	sprintf(numeric_state, "Invalid state %d", (int) state);
	return numeric_state;
    } else
	return plexstatetext[state];
}

/* Return plex organization as a string */
char *
plex_org(enum plexorg org)
{
    switch (org) {
    case plex_disorg:					    /* disorganized */
	return "disorg";
	break;

    case plex_concat:					    /* concatenated plex */
	return "concat";
	break;

    case plex_striped:					    /* striped plex */
	return "striped";
	break;

    case plex_raid4:					    /* RAID-4 plex */
	return "raid4";

    case plex_raid5:					    /* RAID-5 plex */
	return "raid5";
	break;

    default:
	sprintf(numeric_state, "Invalid org %d", (int) org);
	return numeric_state;
    }
}

/* Return sd state as a string */
char *
sd_state(enum sdstate state)
{
    if (((unsigned) state) >= STATECOUNT(sd)) {
	sprintf(numeric_state, "Invalid state %d", (int) state);
	return numeric_state;
    } else
	return sdstatetext[state];
}

/* Now convert in the other direction */
/*
 * These are currently used only internally,
 * so we don't do too much error checking
 */
enum drivestate
DriveState(char *text)
{
    int i;
    for (i = 0; i < STATECOUNT(drive); i++)
	if (strcmp(text, drivestatetext[i]) == 0)	    /* found it */
	    return (enum drivestate) i;
    return -1;
}

enum sdstate
SdState(char *text)
{
    int i;
    for (i = 0; i < STATECOUNT(sd); i++)
	if (strcmp(text, sdstatetext[i]) == 0)		    /* found it */
	    return (enum sdstate) i;
    return -1;
}

enum plexstate
PlexState(char *text)
{
    int i;
    for (i = 0; i < STATECOUNT(plex); i++)
	if (strcmp(text, plexstatetext[i]) == 0)	    /* found it */
	    return (enum plexstate) i;
    return -1;
}

enum volumestate
VolState(char *text)
{
    int i;
    for (i = 0; i < STATECOUNT(vol); i++)
	if (strcmp(text, volstatetext[i]) == 0)		    /* found it */
	    return (enum volumestate) i;
    return -1;
}

/*
 * Take a number with an optional scale factor and convert
 * it to a number of bytes.
 *
 * The scale factors are:
 *
 * s    sectors (of 512 bytes)
 * b    blocks (of 512 bytes).  This unit is deprecated,
 *      because it's confusing, but maintained to avoid
 *      confusing Veritas users.
 * k    kilobytes (1024 bytes)
 * m    megabytes (of 1024 * 1024 bytes)
 * g    gigabytes (of 1024 * 1024 * 1024 bytes)
 */
u_int64_t
sizespec(char *spec)
{
    u_int64_t size;
    char *s;
    int sign = 1;					    /* -1 if negative */

    size = 0;
    if (spec != NULL) {					    /* we have a parameter */
	s = spec;
	if (*s == '-') {				    /* negative, */
	    sign = -1;
	    s++;					    /* skip */
	}
	if ((*s >= '0') && (*s <= '9')) {		    /* it's numeric */
	    while ((*s >= '0') && (*s <= '9'))		    /* it's numeric */
		size = size * 10 + *s++ - '0';		    /* convert it */
	    switch (*s) {
	    case '\0':
		return size * sign;

	    case 'B':
	    case 'b':
	    case 'S':
	    case 's':
		return size * sign * 512;

	    case 'K':
	    case 'k':
		return size * sign * 1024;

	    case 'M':
	    case 'm':
		return size * sign * 1024 * 1024;

	    case 'G':
	    case 'g':
		return size * sign * 1024 * 1024 * 1024;
	    }
	}
#ifdef _KERNEL
	throw_rude_remark(EINVAL, "Invalid length specification: %s", spec);
#else
	fprintf(stderr, "Invalid length specification: %s", spec);
	longjmp(command_fail, 1);
#endif
    }
#ifdef _KERNEL
    throw_rude_remark(EINVAL, "Missing length specification");
#else
    fprintf(stderr, "Missing length specification");
    longjmp(command_fail, 1);
#endif
    /* NOTREACHED */
    return -1;
}

#ifdef _KERNEL
#define FOOTYPE struct cdev *
#else
#define FOOTYPE dev_t
#endif
/*
 * Extract the volume number from a device number.  Check that it's
 * the correct type, and that it isn't one of the superdevs.
 */
int
Volno(FOOTYPE dev)
{
    int volno = minor(dev);

    if (OBJTYPE(dev) != VINUM_VOLUME_TYPE)
	return -1;
    else
	volno = ((volno & 0x3fff0000) >> 8) | (volno & 0xff);
    if ((volno == VINUM_SUPERDEV_VOL)
	|| (volno == VINUM_DAEMON_VOL))
	return -1;
    else
	return volno;
}

/*
 * Extract a plex number from a device number.
 * Don't check the major number, but check the
 * type.  Return -1 for invalid types.
 */
int
Plexno(FOOTYPE dev)
{
    int plexno = minor(dev);

    if (OBJTYPE(dev) != VINUM_PLEX_TYPE)
	return -1;
    else
	return ((plexno & 0x3fff0000) >> 8) | (plexno & 0xff);
}

/*
 * Extract a subdisk number from a device number.
 * Don't check the major number, but check the
 * type.  Return -1 for invalid types.
 */
int
Sdno(FOOTYPE dev)
{
    int sdno = minor(dev);

    /*
     * Care: VINUM_SD_TYPE is 2 or 3, which is why we use < instead of
     * !=.  It's not clear that this makes any sense abstracting it to
     * this level.
     */
    if (OBJTYPE(dev) < VINUM_SD_TYPE)
	return -1;
    else
/*
 * Note that the number we return includes the low-order bit of the
 * type field.  This gives us twice as many potential subdisks as
 * plexes or volumes.
 */
	return ((sdno & 0x7fff0000) >> 8) | (sdno & 0xff);
}
OpenPOWER on IntegriCloud