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