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