geom_raid3.c revision 143586
1133808Spjd/*- 2142727Spjd * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3133808Spjd * All rights reserved. 4133808Spjd * 5133808Spjd * Redistribution and use in source and binary forms, with or without 6133808Spjd * modification, are permitted provided that the following conditions 7133808Spjd * are met: 8133808Spjd * 1. Redistributions of source code must retain the above copyright 9133808Spjd * notice, this list of conditions and the following disclaimer. 10133808Spjd * 2. Redistributions in binary form must reproduce the above copyright 11133808Spjd * notice, this list of conditions and the following disclaimer in the 12133808Spjd * documentation and/or other materials provided with the distribution. 13133808Spjd * 14133808Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15133808Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16133808Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17133808Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18133808Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19133808Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20133808Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21133808Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22133808Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23133808Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24133808Spjd * SUCH DAMAGE. 25133808Spjd */ 26133808Spjd 27133808Spjd#include <sys/cdefs.h> 28133808Spjd__FBSDID("$FreeBSD: head/sbin/geom/class/raid3/geom_raid3.c 143586 2005-03-14 14:25:47Z pjd $"); 29133808Spjd 30133808Spjd#include <sys/param.h> 31133808Spjd#include <errno.h> 32133808Spjd#include <paths.h> 33133808Spjd#include <stdio.h> 34133808Spjd#include <stdlib.h> 35133808Spjd#include <stdint.h> 36133808Spjd#include <string.h> 37133808Spjd#include <strings.h> 38133808Spjd#include <assert.h> 39133808Spjd#include <libgeom.h> 40133808Spjd#include <geom/raid3/g_raid3.h> 41133808Spjd#include <core/geom.h> 42133808Spjd#include <misc/subr.h> 43133808Spjd 44133808Spjd 45133808Spjduint32_t lib_version = G_LIB_VERSION; 46133808Spjduint32_t version = G_RAID3_VERSION; 47133808Spjd 48133808Spjdstatic void raid3_main(struct gctl_req *req, unsigned f); 49133808Spjdstatic void raid3_clear(struct gctl_req *req); 50133808Spjdstatic void raid3_dump(struct gctl_req *req); 51133808Spjdstatic void raid3_label(struct gctl_req *req); 52133808Spjd 53133808Spjdstruct g_command class_commands[] = { 54143586Spjd { "clear", G_FLAG_VERBOSE, raid3_main, G_NULL_OPTS, 55143586Spjd "[-v] prov ..." 56143586Spjd }, 57133808Spjd { "configure", G_FLAG_VERBOSE, NULL, 58133808Spjd { 59133808Spjd { 'a', "autosync", NULL, G_TYPE_NONE }, 60133808Spjd { 'd', "dynamic", NULL, G_TYPE_NONE }, 61133808Spjd { 'h', "hardcode", NULL, G_TYPE_NONE }, 62133808Spjd { 'n', "noautosync", NULL, G_TYPE_NONE }, 63134124Spjd { 'r', "round_robin", NULL, G_TYPE_NONE }, 64134124Spjd { 'R', "noround_robin", NULL, G_TYPE_NONE }, 65134168Spjd { 'w', "verify", NULL, G_TYPE_NONE }, 66134168Spjd { 'W', "noverify", NULL, G_TYPE_NONE }, 67133808Spjd G_OPT_SENTINEL 68143586Spjd }, 69143586Spjd "[-adhnrRvwW] name" 70133808Spjd }, 71143586Spjd { "dump", 0, raid3_main, G_NULL_OPTS, 72143586Spjd "prov ..." 73143586Spjd }, 74133808Spjd { "insert", G_FLAG_VERBOSE, NULL, 75133808Spjd { 76133808Spjd { 'h', "hardcode", NULL, G_TYPE_NONE }, 77133808Spjd { 'n', "number", NULL, G_TYPE_NUMBER }, 78133808Spjd G_OPT_SENTINEL 79143586Spjd }, 80143586Spjd "[-hv] <-n number> name prov" 81133808Spjd }, 82133808Spjd { "label", G_FLAG_VERBOSE, raid3_main, 83133808Spjd { 84133808Spjd { 'h', "hardcode", NULL, G_TYPE_NONE }, 85133808Spjd { 'n', "noautosync", NULL, G_TYPE_NONE }, 86134124Spjd { 'r', "round_robin", NULL, G_TYPE_NONE }, 87134168Spjd { 'w', "verify", NULL, G_TYPE_NONE }, 88133808Spjd G_OPT_SENTINEL 89143586Spjd }, 90143586Spjd "[-hnrvw] name prov prov prov ..." 91133808Spjd }, 92143586Spjd { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, 93143586Spjd "[-v] name prov" 94143586Spjd }, 95133808Spjd { "remove", G_FLAG_VERBOSE, NULL, 96133808Spjd { 97133808Spjd { 'n', "number", NULL, G_TYPE_NUMBER }, 98133808Spjd G_OPT_SENTINEL 99143586Spjd }, 100143586Spjd "[-v] <-n number> name" 101133808Spjd }, 102133808Spjd { "stop", G_FLAG_VERBOSE, NULL, 103133808Spjd { 104133808Spjd { 'f', "force", NULL, G_TYPE_NONE }, 105133808Spjd G_OPT_SENTINEL 106143586Spjd }, 107143586Spjd "[-fv] name ..." 108133808Spjd }, 109133808Spjd G_CMD_SENTINEL 110133808Spjd}; 111133808Spjd 112133808Spjdstatic int verbose = 0; 113133808Spjd 114133808Spjdstatic void 115133808Spjdraid3_main(struct gctl_req *req, unsigned flags) 116133808Spjd{ 117133808Spjd const char *name; 118133808Spjd 119133808Spjd if ((flags & G_FLAG_VERBOSE) != 0) 120133808Spjd verbose = 1; 121133808Spjd 122133808Spjd name = gctl_get_asciiparam(req, "verb"); 123133808Spjd if (name == NULL) { 124133808Spjd gctl_error(req, "No '%s' argument.", "verb"); 125133808Spjd return; 126133808Spjd } 127133808Spjd if (strcmp(name, "label") == 0) 128133808Spjd raid3_label(req); 129133808Spjd else if (strcmp(name, "clear") == 0) 130133808Spjd raid3_clear(req); 131133808Spjd else if (strcmp(name, "dump") == 0) 132133808Spjd raid3_dump(req); 133133808Spjd else 134133808Spjd gctl_error(req, "Unknown command: %s.", name); 135133808Spjd} 136133808Spjd 137133808Spjdstatic void 138133808Spjdraid3_label(struct gctl_req *req) 139133808Spjd{ 140133808Spjd struct g_raid3_metadata md; 141133808Spjd u_char sector[512]; 142133808Spjd const char *str; 143133808Spjd char param[16]; 144134168Spjd int *hardcode, *nargs, *noautosync, *round_robin, *verify; 145134124Spjd int error, i; 146134420Spjd unsigned sectorsize, ssize; 147134420Spjd off_t mediasize, msize; 148133808Spjd 149133808Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 150133808Spjd if (nargs == NULL) { 151133808Spjd gctl_error(req, "No '%s' argument.", "nargs"); 152133808Spjd return; 153133808Spjd } 154133808Spjd if (*nargs < 4) { 155133808Spjd gctl_error(req, "Too few arguments."); 156133808Spjd return; 157133808Spjd } 158133808Spjd#ifndef BITCOUNT 159133808Spjd#define BITCOUNT(x) (((BX_(x) + (BX_(x) >> 4)) & 0x0F0F0F0F) % 255) 160133808Spjd#define BX_(x) ((x) - (((x) >> 1) & 0x77777777) - \ 161133808Spjd (((x) >> 2) & 0x33333333) - (((x) >> 3) & 0x11111111)) 162133808Spjd#endif 163133808Spjd if (BITCOUNT(*nargs - 2) != 1) { 164133808Spjd gctl_error(req, "Invalid number of components."); 165133808Spjd return; 166133808Spjd } 167133808Spjd 168133808Spjd strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic)); 169133808Spjd md.md_version = G_RAID3_VERSION; 170133808Spjd str = gctl_get_asciiparam(req, "arg0"); 171133808Spjd if (str == NULL) { 172133808Spjd gctl_error(req, "No 'arg%u' argument.", 0); 173133808Spjd return; 174133808Spjd } 175133808Spjd strlcpy(md.md_name, str, sizeof(md.md_name)); 176133808Spjd md.md_all = *nargs - 1; 177133808Spjd md.md_mflags = 0; 178133808Spjd md.md_dflags = 0; 179139295Spjd md.md_genid = 0; 180133808Spjd md.md_syncid = 1; 181133808Spjd md.md_sync_offset = 0; 182133808Spjd noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync)); 183133808Spjd if (noautosync == NULL) { 184133808Spjd gctl_error(req, "No '%s' argument.", "noautosync"); 185133808Spjd return; 186133808Spjd } 187133808Spjd if (*noautosync) 188133808Spjd md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC; 189134124Spjd round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin)); 190134124Spjd if (round_robin == NULL) { 191134124Spjd gctl_error(req, "No '%s' argument.", "round_robin"); 192134124Spjd return; 193134124Spjd } 194134124Spjd if (*round_robin) 195134124Spjd md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN; 196134168Spjd verify = gctl_get_paraml(req, "verify", sizeof(*verify)); 197134168Spjd if (verify == NULL) { 198134168Spjd gctl_error(req, "No '%s' argument.", "verify"); 199134168Spjd return; 200134168Spjd } 201134168Spjd if (*verify) 202134168Spjd md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY; 203134168Spjd if (*round_robin && *verify) { 204134168Spjd gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w'); 205134168Spjd return; 206134168Spjd } 207133808Spjd hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 208133808Spjd if (hardcode == NULL) { 209133808Spjd gctl_error(req, "No '%s' argument.", "hardcode"); 210133808Spjd return; 211133808Spjd } 212133808Spjd 213133808Spjd /* 214133808Spjd * Calculate sectorsize by finding least common multiple from 215133808Spjd * sectorsizes of every disk and find the smallest mediasize. 216133808Spjd */ 217133808Spjd mediasize = 0; 218133808Spjd sectorsize = 0; 219133808Spjd for (i = 1; i < *nargs; i++) { 220133808Spjd snprintf(param, sizeof(param), "arg%u", i); 221133808Spjd str = gctl_get_asciiparam(req, param); 222133808Spjd 223133808Spjd msize = g_get_mediasize(str); 224133808Spjd ssize = g_get_sectorsize(str); 225133808Spjd if (msize == 0 || ssize == 0) { 226133808Spjd gctl_error(req, "Can't get informations about %s: %s.", 227133808Spjd str, strerror(errno)); 228133808Spjd return; 229133808Spjd } 230133808Spjd msize -= ssize; 231133808Spjd if (mediasize == 0 || (mediasize > 0 && msize < mediasize)) 232133808Spjd mediasize = msize; 233133808Spjd if (sectorsize == 0) 234133808Spjd sectorsize = ssize; 235133808Spjd else 236133808Spjd sectorsize = g_lcm(sectorsize, ssize); 237133808Spjd } 238133808Spjd md.md_mediasize = mediasize * (*nargs - 2); 239133808Spjd md.md_sectorsize = sectorsize * (*nargs - 2); 240133808Spjd 241133808Spjd /* 242133808Spjd * Clear last sector first, to spoil all components if device exists. 243133808Spjd */ 244133808Spjd for (i = 1; i < *nargs; i++) { 245133808Spjd snprintf(param, sizeof(param), "arg%u", i); 246133808Spjd str = gctl_get_asciiparam(req, param); 247133808Spjd 248133808Spjd error = g_metadata_clear(str, NULL); 249133808Spjd if (error != 0) { 250133808Spjd gctl_error(req, "Can't store metadata on %s: %s.", str, 251133808Spjd strerror(error)); 252133808Spjd return; 253133808Spjd } 254133808Spjd } 255133808Spjd 256133808Spjd /* 257133808Spjd * Ok, store metadata (use disk number as priority). 258133808Spjd */ 259133808Spjd for (i = 1; i < *nargs; i++) { 260133808Spjd snprintf(param, sizeof(param), "arg%u", i); 261133808Spjd str = gctl_get_asciiparam(req, param); 262133808Spjd 263142727Spjd msize = g_get_mediasize(str); 264142727Spjd ssize = g_get_sectorsize(str); 265142727Spjd if (mediasize < msize - ssize) { 266134420Spjd fprintf(stderr, 267134420Spjd "warning: %s: only %jd bytes from %jd bytes used.\n", 268142727Spjd str, (intmax_t)mediasize, (intmax_t)(msize - ssize)); 269134420Spjd } 270134420Spjd 271133808Spjd md.md_no = i - 1; 272142727Spjd md.md_provsize = msize; 273133808Spjd if (!*hardcode) 274133808Spjd bzero(md.md_provider, sizeof(md.md_provider)); 275133808Spjd else { 276133808Spjd if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0) 277133808Spjd str += strlen(_PATH_DEV); 278133808Spjd strlcpy(md.md_provider, str, sizeof(md.md_provider)); 279133808Spjd } 280134539Spjd if (*verify && md.md_no == md.md_all - 1) { 281134539Spjd /* 282134539Spjd * In "verify" mode, force synchronization of parity 283134539Spjd * component on start. 284134539Spjd */ 285134539Spjd md.md_syncid = 0; 286134539Spjd } 287133808Spjd raid3_metadata_encode(&md, sector); 288133808Spjd error = g_metadata_store(str, sector, sizeof(sector)); 289133808Spjd if (error != 0) { 290133808Spjd fprintf(stderr, "Can't store metadata on %s: %s.\n", 291133808Spjd str, strerror(error)); 292133808Spjd gctl_error(req, "Not fully done."); 293133808Spjd continue; 294133808Spjd } 295133808Spjd if (verbose) 296133808Spjd printf("Metadata value stored on %s.\n", str); 297133808Spjd } 298133808Spjd} 299133808Spjd 300133808Spjdstatic void 301133808Spjdraid3_clear(struct gctl_req *req) 302133808Spjd{ 303133808Spjd const char *name; 304133808Spjd char param[16]; 305133808Spjd int *nargs, error, i; 306133808Spjd 307133808Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 308133808Spjd if (nargs == NULL) { 309133808Spjd gctl_error(req, "No '%s' argument.", "nargs"); 310133808Spjd return; 311133808Spjd } 312133808Spjd if (*nargs < 1) { 313133808Spjd gctl_error(req, "Too few arguments."); 314133808Spjd return; 315133808Spjd } 316133808Spjd 317133808Spjd for (i = 0; i < *nargs; i++) { 318133808Spjd snprintf(param, sizeof(param), "arg%u", i); 319133808Spjd name = gctl_get_asciiparam(req, param); 320133808Spjd 321133808Spjd error = g_metadata_clear(name, G_RAID3_MAGIC); 322133808Spjd if (error != 0) { 323133808Spjd fprintf(stderr, "Can't clear metadata on %s: %s.\n", 324133808Spjd name, strerror(error)); 325133808Spjd gctl_error(req, "Not fully done."); 326133808Spjd continue; 327133808Spjd } 328133808Spjd if (verbose) 329133808Spjd printf("Metadata cleared on %s.\n", name); 330133808Spjd } 331133808Spjd} 332133808Spjd 333133808Spjdstatic void 334133808Spjdraid3_dump(struct gctl_req *req) 335133808Spjd{ 336133808Spjd struct g_raid3_metadata md, tmpmd; 337133808Spjd const char *name; 338133808Spjd char param[16]; 339133808Spjd int *nargs, error, i; 340133808Spjd 341133808Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 342133808Spjd if (nargs == NULL) { 343133808Spjd gctl_error(req, "No '%s' argument.", "nargs"); 344133808Spjd return; 345133808Spjd } 346133808Spjd if (*nargs < 1) { 347133808Spjd gctl_error(req, "Too few arguments."); 348133808Spjd return; 349133808Spjd } 350133808Spjd 351133808Spjd for (i = 0; i < *nargs; i++) { 352133808Spjd snprintf(param, sizeof(param), "arg%u", i); 353133808Spjd name = gctl_get_asciiparam(req, param); 354133808Spjd 355133808Spjd error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd), 356133808Spjd G_RAID3_MAGIC); 357133808Spjd if (error != 0) { 358133808Spjd fprintf(stderr, "Can't read metadata from %s: %s.\n", 359133808Spjd name, strerror(error)); 360133808Spjd gctl_error(req, "Not fully done."); 361133808Spjd continue; 362133808Spjd } 363133808Spjd if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) { 364133808Spjd fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n", 365133808Spjd name); 366133808Spjd gctl_error(req, "Not fully done."); 367133808Spjd continue; 368133808Spjd } 369133808Spjd printf("Metadata on %s:\n", name); 370133808Spjd raid3_metadata_dump(&md); 371133808Spjd printf("\n"); 372133808Spjd } 373133808Spjd} 374