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