geom_raid3.c revision 134168
1/*- 2 * Copyright (c) 2004 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 134168 2004-08-22 16:21:12Z 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 { "configure", G_FLAG_VERBOSE, NULL, 56 { 57 { 'a', "autosync", NULL, G_TYPE_NONE }, 58 { 'd', "dynamic", NULL, G_TYPE_NONE }, 59 { 'h', "hardcode", NULL, G_TYPE_NONE }, 60 { 'n', "noautosync", NULL, G_TYPE_NONE }, 61 { 'r', "round_robin", NULL, G_TYPE_NONE }, 62 { 'R', "noround_robin", NULL, G_TYPE_NONE }, 63 { 'w', "verify", NULL, G_TYPE_NONE }, 64 { 'W', "noverify", NULL, G_TYPE_NONE }, 65 G_OPT_SENTINEL 66 } 67 }, 68 { "dump", 0, raid3_main, G_NULL_OPTS }, 69 { "insert", G_FLAG_VERBOSE, NULL, 70 { 71 { 'h', "hardcode", NULL, G_TYPE_NONE }, 72 { 'n', "number", NULL, G_TYPE_NUMBER }, 73 G_OPT_SENTINEL 74 } 75 }, 76 { "label", G_FLAG_VERBOSE, raid3_main, 77 { 78 { 'h', "hardcode", NULL, G_TYPE_NONE }, 79 { 'n', "noautosync", NULL, G_TYPE_NONE }, 80 { 'r', "round_robin", NULL, G_TYPE_NONE }, 81 { 'w', "verify", NULL, G_TYPE_NONE }, 82 G_OPT_SENTINEL 83 } 84 }, 85 { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS }, 86 { "remove", G_FLAG_VERBOSE, NULL, 87 { 88 { 'n', "number", NULL, G_TYPE_NUMBER }, 89 G_OPT_SENTINEL 90 } 91 }, 92 { "stop", G_FLAG_VERBOSE, NULL, 93 { 94 { 'f', "force", NULL, G_TYPE_NONE }, 95 G_OPT_SENTINEL 96 } 97 }, 98 G_CMD_SENTINEL 99}; 100 101static int verbose = 0; 102 103void usage(const char *); 104void 105usage(const char *comm) 106{ 107 fprintf(stderr, 108 "usage: %s label [-hnrvw] name prov prov prov [prov [...]]\n" 109 " %s clear [-v] prov [prov [...]]\n" 110 " %s dump prov [prov [...]]\n" 111 " %s configure [-adhnrRvwW] name\n" 112 " %s rebuild [-v] name prov\n" 113 " %s insert [-hv] <-n number> name prov\n" 114 " %s remove [-v] <-n number> name\n" 115 " %s stop [-fv] name [...]\n", 116 comm, comm, comm, comm, comm, comm, comm, comm); 117 exit(EXIT_FAILURE); 118} 119 120static void 121raid3_main(struct gctl_req *req, unsigned flags) 122{ 123 const char *name; 124 125 if ((flags & G_FLAG_VERBOSE) != 0) 126 verbose = 1; 127 128 name = gctl_get_asciiparam(req, "verb"); 129 if (name == NULL) { 130 gctl_error(req, "No '%s' argument.", "verb"); 131 return; 132 } 133 if (strcmp(name, "label") == 0) 134 raid3_label(req); 135 else if (strcmp(name, "clear") == 0) 136 raid3_clear(req); 137 else if (strcmp(name, "dump") == 0) 138 raid3_dump(req); 139 else 140 gctl_error(req, "Unknown command: %s.", name); 141} 142 143static void 144raid3_label(struct gctl_req *req) 145{ 146 struct g_raid3_metadata md; 147 u_char sector[512]; 148 const char *str; 149 char param[16]; 150 int *hardcode, *nargs, *noautosync, *round_robin, *verify; 151 int error, i; 152 unsigned sectorsize; 153 off_t mediasize; 154 155 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 156 if (nargs == NULL) { 157 gctl_error(req, "No '%s' argument.", "nargs"); 158 return; 159 } 160 if (*nargs < 4) { 161 gctl_error(req, "Too few arguments."); 162 return; 163 } 164#ifndef BITCOUNT 165#define BITCOUNT(x) (((BX_(x) + (BX_(x) >> 4)) & 0x0F0F0F0F) % 255) 166#define BX_(x) ((x) - (((x) >> 1) & 0x77777777) - \ 167 (((x) >> 2) & 0x33333333) - (((x) >> 3) & 0x11111111)) 168#endif 169 if (BITCOUNT(*nargs - 2) != 1) { 170 gctl_error(req, "Invalid number of components."); 171 return; 172 } 173 174 strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic)); 175 md.md_version = G_RAID3_VERSION; 176 str = gctl_get_asciiparam(req, "arg0"); 177 if (str == NULL) { 178 gctl_error(req, "No 'arg%u' argument.", 0); 179 return; 180 } 181 strlcpy(md.md_name, str, sizeof(md.md_name)); 182 md.md_all = *nargs - 1; 183 md.md_mflags = 0; 184 md.md_dflags = 0; 185 md.md_syncid = 1; 186 md.md_sync_offset = 0; 187 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync)); 188 if (noautosync == NULL) { 189 gctl_error(req, "No '%s' argument.", "noautosync"); 190 return; 191 } 192 if (*noautosync) 193 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC; 194 round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin)); 195 if (round_robin == NULL) { 196 gctl_error(req, "No '%s' argument.", "round_robin"); 197 return; 198 } 199 if (*round_robin) 200 md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN; 201 verify = gctl_get_paraml(req, "verify", sizeof(*verify)); 202 if (verify == NULL) { 203 gctl_error(req, "No '%s' argument.", "verify"); 204 return; 205 } 206 if (*verify) 207 md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY; 208 if (*round_robin && *verify) { 209 gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w'); 210 return; 211 } 212 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 213 if (hardcode == NULL) { 214 gctl_error(req, "No '%s' argument.", "hardcode"); 215 return; 216 } 217 218 /* 219 * Calculate sectorsize by finding least common multiple from 220 * sectorsizes of every disk and find the smallest mediasize. 221 */ 222 mediasize = 0; 223 sectorsize = 0; 224 for (i = 1; i < *nargs; i++) { 225 unsigned ssize; 226 off_t msize; 227 228 snprintf(param, sizeof(param), "arg%u", i); 229 str = gctl_get_asciiparam(req, param); 230 231 msize = g_get_mediasize(str); 232 ssize = g_get_sectorsize(str); 233 if (msize == 0 || ssize == 0) { 234 gctl_error(req, "Can't get informations about %s: %s.", 235 str, strerror(errno)); 236 return; 237 } 238 msize -= ssize; 239 if (mediasize == 0 || (mediasize > 0 && msize < mediasize)) 240 mediasize = msize; 241 if (sectorsize == 0) 242 sectorsize = ssize; 243 else 244 sectorsize = g_lcm(sectorsize, ssize); 245 } 246 md.md_mediasize = mediasize * (*nargs - 2); 247 md.md_sectorsize = sectorsize * (*nargs - 2); 248 249 /* 250 * Clear last sector first, to spoil all components if device exists. 251 */ 252 for (i = 1; i < *nargs; i++) { 253 snprintf(param, sizeof(param), "arg%u", i); 254 str = gctl_get_asciiparam(req, param); 255 256 error = g_metadata_clear(str, NULL); 257 if (error != 0) { 258 gctl_error(req, "Can't store metadata on %s: %s.", str, 259 strerror(error)); 260 return; 261 } 262 } 263 264 /* 265 * Ok, store metadata (use disk number as priority). 266 */ 267 for (i = 1; i < *nargs; i++) { 268 snprintf(param, sizeof(param), "arg%u", i); 269 str = gctl_get_asciiparam(req, param); 270 271 md.md_no = i - 1; 272 if (!*hardcode) 273 bzero(md.md_provider, sizeof(md.md_provider)); 274 else { 275 if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0) 276 str += strlen(_PATH_DEV); 277 strlcpy(md.md_provider, str, sizeof(md.md_provider)); 278 } 279 raid3_metadata_encode(&md, sector); 280 error = g_metadata_store(str, sector, sizeof(sector)); 281 if (error != 0) { 282 fprintf(stderr, "Can't store metadata on %s: %s.\n", 283 str, strerror(error)); 284 gctl_error(req, "Not fully done."); 285 continue; 286 } 287 if (verbose) 288 printf("Metadata value stored on %s.\n", str); 289 } 290} 291 292static void 293raid3_clear(struct gctl_req *req) 294{ 295 const char *name; 296 char param[16]; 297 int *nargs, error, i; 298 299 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 300 if (nargs == NULL) { 301 gctl_error(req, "No '%s' argument.", "nargs"); 302 return; 303 } 304 if (*nargs < 1) { 305 gctl_error(req, "Too few arguments."); 306 return; 307 } 308 309 for (i = 0; i < *nargs; i++) { 310 snprintf(param, sizeof(param), "arg%u", i); 311 name = gctl_get_asciiparam(req, param); 312 313 error = g_metadata_clear(name, G_RAID3_MAGIC); 314 if (error != 0) { 315 fprintf(stderr, "Can't clear metadata on %s: %s.\n", 316 name, strerror(error)); 317 gctl_error(req, "Not fully done."); 318 continue; 319 } 320 if (verbose) 321 printf("Metadata cleared on %s.\n", name); 322 } 323} 324 325static void 326raid3_dump(struct gctl_req *req) 327{ 328 struct g_raid3_metadata md, tmpmd; 329 const char *name; 330 char param[16]; 331 int *nargs, error, i; 332 333 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 334 if (nargs == NULL) { 335 gctl_error(req, "No '%s' argument.", "nargs"); 336 return; 337 } 338 if (*nargs < 1) { 339 gctl_error(req, "Too few arguments."); 340 return; 341 } 342 343 for (i = 0; i < *nargs; i++) { 344 snprintf(param, sizeof(param), "arg%u", i); 345 name = gctl_get_asciiparam(req, param); 346 347 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd), 348 G_RAID3_MAGIC); 349 if (error != 0) { 350 fprintf(stderr, "Can't read metadata from %s: %s.\n", 351 name, strerror(error)); 352 gctl_error(req, "Not fully done."); 353 continue; 354 } 355 if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) { 356 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n", 357 name); 358 gctl_error(req, "Not fully done."); 359 continue; 360 } 361 printf("Metadata on %s:\n", name); 362 raid3_metadata_dump(&md); 363 printf("\n"); 364 } 365} 366