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