geom_raid3.c revision 149302
1/*- 2 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sbin/geom/class/raid3/geom_raid3.c 149302 2005-08-19 22:13:09Z pjd $"); 29 30#include <sys/param.h> 31#include <errno.h> 32#include <paths.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <stdint.h> 36#include <string.h> 37#include <strings.h> 38#include <assert.h> 39#include <libgeom.h> 40#include <geom/raid3/g_raid3.h> 41#include <core/geom.h> 42#include <misc/subr.h> 43 44 45uint32_t lib_version = G_LIB_VERSION; 46uint32_t version = G_RAID3_VERSION; 47 48static void raid3_main(struct gctl_req *req, unsigned f); 49static void raid3_clear(struct gctl_req *req); 50static void raid3_dump(struct gctl_req *req); 51static void raid3_label(struct gctl_req *req); 52 53struct g_command class_commands[] = { 54 { "clear", G_FLAG_VERBOSE, raid3_main, G_NULL_OPTS, 55 "[-v] prov ..." 56 }, 57 { "configure", G_FLAG_VERBOSE, NULL, 58 { 59 { 'a', "autosync", NULL, G_TYPE_NONE }, 60 { 'd', "dynamic", NULL, G_TYPE_NONE }, 61 { 'h', "hardcode", NULL, G_TYPE_NONE }, 62 { 'n', "noautosync", NULL, G_TYPE_NONE }, 63 { 'r', "round_robin", NULL, G_TYPE_NONE }, 64 { 'R', "noround_robin", NULL, G_TYPE_NONE }, 65 { 'w', "verify", NULL, G_TYPE_NONE }, 66 { 'W', "noverify", NULL, G_TYPE_NONE }, 67 G_OPT_SENTINEL 68 }, 69 "[-adhnrRvwW] name" 70 }, 71 { "dump", 0, raid3_main, G_NULL_OPTS, 72 "prov ..." 73 }, 74 { "insert", G_FLAG_VERBOSE, NULL, 75 { 76 { 'h', "hardcode", NULL, G_TYPE_NONE }, 77 { 'n', "number", NULL, G_TYPE_NUMBER }, 78 G_OPT_SENTINEL 79 }, 80 "[-hv] <-n number> name prov" 81 }, 82 { "label", G_FLAG_VERBOSE, raid3_main, 83 { 84 { 'h', "hardcode", NULL, G_TYPE_NONE }, 85 { 'n', "noautosync", NULL, G_TYPE_NONE }, 86 { 'r', "round_robin", NULL, G_TYPE_NONE }, 87 { 'w', "verify", NULL, G_TYPE_NONE }, 88 G_OPT_SENTINEL 89 }, 90 "[-hnrvw] name prov prov prov ..." 91 }, 92 { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, 93 "[-v] name prov" 94 }, 95 { "remove", G_FLAG_VERBOSE, NULL, 96 { 97 { 'n', "number", NULL, G_TYPE_NUMBER }, 98 G_OPT_SENTINEL 99 }, 100 "[-v] <-n number> name" 101 }, 102 { "stop", G_FLAG_VERBOSE, NULL, 103 { 104 { 'f', "force", NULL, G_TYPE_NONE }, 105 G_OPT_SENTINEL 106 }, 107 "[-fv] name ..." 108 }, 109 G_CMD_SENTINEL 110}; 111 112static int verbose = 0; 113 114static void 115raid3_main(struct gctl_req *req, unsigned flags) 116{ 117 const char *name; 118 119 if ((flags & G_FLAG_VERBOSE) != 0) 120 verbose = 1; 121 122 name = gctl_get_asciiparam(req, "verb"); 123 if (name == NULL) { 124 gctl_error(req, "No '%s' argument.", "verb"); 125 return; 126 } 127 if (strcmp(name, "label") == 0) 128 raid3_label(req); 129 else if (strcmp(name, "clear") == 0) 130 raid3_clear(req); 131 else if (strcmp(name, "dump") == 0) 132 raid3_dump(req); 133 else 134 gctl_error(req, "Unknown command: %s.", name); 135} 136 137static void 138raid3_label(struct gctl_req *req) 139{ 140 struct g_raid3_metadata md; 141 u_char sector[512]; 142 const char *str; 143 char param[16]; 144 int *hardcode, *nargs, *noautosync, *round_robin, *verify; 145 int error, i; 146 unsigned sectorsize, ssize; 147 off_t mediasize, msize; 148 149 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 150 if (nargs == NULL) { 151 gctl_error(req, "No '%s' argument.", "nargs"); 152 return; 153 } 154 if (*nargs < 4) { 155 gctl_error(req, "Too few arguments."); 156 return; 157 } 158 if (bitcount32(*nargs - 2) != 1) { 159 gctl_error(req, "Invalid number of components."); 160 return; 161 } 162 163 strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic)); 164 md.md_version = G_RAID3_VERSION; 165 str = gctl_get_asciiparam(req, "arg0"); 166 if (str == NULL) { 167 gctl_error(req, "No 'arg%u' argument.", 0); 168 return; 169 } 170 strlcpy(md.md_name, str, sizeof(md.md_name)); 171 md.md_id = arc4random(); 172 md.md_all = *nargs - 1; 173 md.md_mflags = 0; 174 md.md_dflags = 0; 175 md.md_genid = 0; 176 md.md_syncid = 1; 177 md.md_sync_offset = 0; 178 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync)); 179 if (noautosync == NULL) { 180 gctl_error(req, "No '%s' argument.", "noautosync"); 181 return; 182 } 183 if (*noautosync) 184 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC; 185 round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin)); 186 if (round_robin == NULL) { 187 gctl_error(req, "No '%s' argument.", "round_robin"); 188 return; 189 } 190 if (*round_robin) 191 md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN; 192 verify = gctl_get_paraml(req, "verify", sizeof(*verify)); 193 if (verify == NULL) { 194 gctl_error(req, "No '%s' argument.", "verify"); 195 return; 196 } 197 if (*verify) 198 md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY; 199 if (*round_robin && *verify) { 200 gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w'); 201 return; 202 } 203 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 204 if (hardcode == NULL) { 205 gctl_error(req, "No '%s' argument.", "hardcode"); 206 return; 207 } 208 209 /* 210 * Calculate sectorsize by finding least common multiple from 211 * sectorsizes of every disk and find the smallest mediasize. 212 */ 213 mediasize = 0; 214 sectorsize = 0; 215 for (i = 1; i < *nargs; i++) { 216 snprintf(param, sizeof(param), "arg%u", i); 217 str = gctl_get_asciiparam(req, param); 218 219 msize = g_get_mediasize(str); 220 ssize = g_get_sectorsize(str); 221 if (msize == 0 || ssize == 0) { 222 gctl_error(req, "Can't get informations about %s: %s.", 223 str, strerror(errno)); 224 return; 225 } 226 msize -= ssize; 227 if (mediasize == 0 || (mediasize > 0 && msize < mediasize)) 228 mediasize = msize; 229 if (sectorsize == 0) 230 sectorsize = ssize; 231 else 232 sectorsize = g_lcm(sectorsize, ssize); 233 } 234 md.md_mediasize = mediasize * (*nargs - 2); 235 md.md_sectorsize = sectorsize * (*nargs - 2); 236 237 /* 238 * Clear last sector first, to spoil all components if device exists. 239 */ 240 for (i = 1; i < *nargs; i++) { 241 snprintf(param, sizeof(param), "arg%u", i); 242 str = gctl_get_asciiparam(req, param); 243 244 error = g_metadata_clear(str, NULL); 245 if (error != 0) { 246 gctl_error(req, "Can't store metadata on %s: %s.", str, 247 strerror(error)); 248 return; 249 } 250 } 251 252 /* 253 * Ok, store metadata (use disk number as priority). 254 */ 255 for (i = 1; i < *nargs; i++) { 256 snprintf(param, sizeof(param), "arg%u", i); 257 str = gctl_get_asciiparam(req, param); 258 259 msize = g_get_mediasize(str); 260 ssize = g_get_sectorsize(str); 261 if (mediasize < msize - ssize) { 262 fprintf(stderr, 263 "warning: %s: only %jd bytes from %jd bytes used.\n", 264 str, (intmax_t)mediasize, (intmax_t)(msize - ssize)); 265 } 266 267 md.md_no = i - 1; 268 md.md_provsize = msize; 269 if (!*hardcode) 270 bzero(md.md_provider, sizeof(md.md_provider)); 271 else { 272 if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0) 273 str += strlen(_PATH_DEV); 274 strlcpy(md.md_provider, str, sizeof(md.md_provider)); 275 } 276 if (*verify && md.md_no == md.md_all - 1) { 277 /* 278 * In "verify" mode, force synchronization of parity 279 * component on start. 280 */ 281 md.md_syncid = 0; 282 } 283 raid3_metadata_encode(&md, sector); 284 error = g_metadata_store(str, sector, sizeof(sector)); 285 if (error != 0) { 286 fprintf(stderr, "Can't store metadata on %s: %s.\n", 287 str, strerror(error)); 288 gctl_error(req, "Not fully done."); 289 continue; 290 } 291 if (verbose) 292 printf("Metadata value stored on %s.\n", str); 293 } 294} 295 296static void 297raid3_clear(struct gctl_req *req) 298{ 299 const char *name; 300 char param[16]; 301 int *nargs, error, i; 302 303 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 304 if (nargs == NULL) { 305 gctl_error(req, "No '%s' argument.", "nargs"); 306 return; 307 } 308 if (*nargs < 1) { 309 gctl_error(req, "Too few arguments."); 310 return; 311 } 312 313 for (i = 0; i < *nargs; i++) { 314 snprintf(param, sizeof(param), "arg%u", i); 315 name = gctl_get_asciiparam(req, param); 316 317 error = g_metadata_clear(name, G_RAID3_MAGIC); 318 if (error != 0) { 319 fprintf(stderr, "Can't clear metadata on %s: %s.\n", 320 name, strerror(error)); 321 gctl_error(req, "Not fully done."); 322 continue; 323 } 324 if (verbose) 325 printf("Metadata cleared on %s.\n", name); 326 } 327} 328 329static void 330raid3_dump(struct gctl_req *req) 331{ 332 struct g_raid3_metadata md, tmpmd; 333 const char *name; 334 char param[16]; 335 int *nargs, error, i; 336 337 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 338 if (nargs == NULL) { 339 gctl_error(req, "No '%s' argument.", "nargs"); 340 return; 341 } 342 if (*nargs < 1) { 343 gctl_error(req, "Too few arguments."); 344 return; 345 } 346 347 for (i = 0; i < *nargs; i++) { 348 snprintf(param, sizeof(param), "arg%u", i); 349 name = gctl_get_asciiparam(req, param); 350 351 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd), 352 G_RAID3_MAGIC); 353 if (error != 0) { 354 fprintf(stderr, "Can't read metadata from %s: %s.\n", 355 name, strerror(error)); 356 gctl_error(req, "Not fully done."); 357 continue; 358 } 359 if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) { 360 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n", 361 name); 362 gctl_error(req, "Not fully done."); 363 continue; 364 } 365 printf("Metadata on %s:\n", name); 366 raid3_metadata_dump(&md); 367 printf("\n"); 368 } 369} 370