geom_raid3.c revision 147947
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 147947 2005-07-13 07:46:55Z 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)); 176147947Spjd md.md_id = arc4random(); 177133808Spjd md.md_all = *nargs - 1; 178133808Spjd md.md_mflags = 0; 179133808Spjd md.md_dflags = 0; 180139295Spjd md.md_genid = 0; 181133808Spjd md.md_syncid = 1; 182133808Spjd md.md_sync_offset = 0; 183133808Spjd noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync)); 184133808Spjd if (noautosync == NULL) { 185133808Spjd gctl_error(req, "No '%s' argument.", "noautosync"); 186133808Spjd return; 187133808Spjd } 188133808Spjd if (*noautosync) 189133808Spjd md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC; 190134124Spjd round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin)); 191134124Spjd if (round_robin == NULL) { 192134124Spjd gctl_error(req, "No '%s' argument.", "round_robin"); 193134124Spjd return; 194134124Spjd } 195134124Spjd if (*round_robin) 196134124Spjd md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN; 197134168Spjd verify = gctl_get_paraml(req, "verify", sizeof(*verify)); 198134168Spjd if (verify == NULL) { 199134168Spjd gctl_error(req, "No '%s' argument.", "verify"); 200134168Spjd return; 201134168Spjd } 202134168Spjd if (*verify) 203134168Spjd md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY; 204134168Spjd if (*round_robin && *verify) { 205134168Spjd gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w'); 206134168Spjd return; 207134168Spjd } 208133808Spjd hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 209133808Spjd if (hardcode == NULL) { 210133808Spjd gctl_error(req, "No '%s' argument.", "hardcode"); 211133808Spjd return; 212133808Spjd } 213133808Spjd 214133808Spjd /* 215133808Spjd * Calculate sectorsize by finding least common multiple from 216133808Spjd * sectorsizes of every disk and find the smallest mediasize. 217133808Spjd */ 218133808Spjd mediasize = 0; 219133808Spjd sectorsize = 0; 220133808Spjd for (i = 1; i < *nargs; i++) { 221133808Spjd snprintf(param, sizeof(param), "arg%u", i); 222133808Spjd str = gctl_get_asciiparam(req, param); 223133808Spjd 224133808Spjd msize = g_get_mediasize(str); 225133808Spjd ssize = g_get_sectorsize(str); 226133808Spjd if (msize == 0 || ssize == 0) { 227133808Spjd gctl_error(req, "Can't get informations about %s: %s.", 228133808Spjd str, strerror(errno)); 229133808Spjd return; 230133808Spjd } 231133808Spjd msize -= ssize; 232133808Spjd if (mediasize == 0 || (mediasize > 0 && msize < mediasize)) 233133808Spjd mediasize = msize; 234133808Spjd if (sectorsize == 0) 235133808Spjd sectorsize = ssize; 236133808Spjd else 237133808Spjd sectorsize = g_lcm(sectorsize, ssize); 238133808Spjd } 239133808Spjd md.md_mediasize = mediasize * (*nargs - 2); 240133808Spjd md.md_sectorsize = sectorsize * (*nargs - 2); 241133808Spjd 242133808Spjd /* 243133808Spjd * Clear last sector first, to spoil all components if device exists. 244133808Spjd */ 245133808Spjd for (i = 1; i < *nargs; i++) { 246133808Spjd snprintf(param, sizeof(param), "arg%u", i); 247133808Spjd str = gctl_get_asciiparam(req, param); 248133808Spjd 249133808Spjd error = g_metadata_clear(str, NULL); 250133808Spjd if (error != 0) { 251133808Spjd gctl_error(req, "Can't store metadata on %s: %s.", str, 252133808Spjd strerror(error)); 253133808Spjd return; 254133808Spjd } 255133808Spjd } 256133808Spjd 257133808Spjd /* 258133808Spjd * Ok, store metadata (use disk number as priority). 259133808Spjd */ 260133808Spjd for (i = 1; i < *nargs; i++) { 261133808Spjd snprintf(param, sizeof(param), "arg%u", i); 262133808Spjd str = gctl_get_asciiparam(req, param); 263133808Spjd 264142727Spjd msize = g_get_mediasize(str); 265142727Spjd ssize = g_get_sectorsize(str); 266142727Spjd if (mediasize < msize - ssize) { 267134420Spjd fprintf(stderr, 268134420Spjd "warning: %s: only %jd bytes from %jd bytes used.\n", 269142727Spjd str, (intmax_t)mediasize, (intmax_t)(msize - ssize)); 270134420Spjd } 271134420Spjd 272133808Spjd md.md_no = i - 1; 273142727Spjd md.md_provsize = msize; 274133808Spjd if (!*hardcode) 275133808Spjd bzero(md.md_provider, sizeof(md.md_provider)); 276133808Spjd else { 277133808Spjd if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0) 278133808Spjd str += strlen(_PATH_DEV); 279133808Spjd strlcpy(md.md_provider, str, sizeof(md.md_provider)); 280133808Spjd } 281134539Spjd if (*verify && md.md_no == md.md_all - 1) { 282134539Spjd /* 283134539Spjd * In "verify" mode, force synchronization of parity 284134539Spjd * component on start. 285134539Spjd */ 286134539Spjd md.md_syncid = 0; 287134539Spjd } 288133808Spjd raid3_metadata_encode(&md, sector); 289133808Spjd error = g_metadata_store(str, sector, sizeof(sector)); 290133808Spjd if (error != 0) { 291133808Spjd fprintf(stderr, "Can't store metadata on %s: %s.\n", 292133808Spjd str, strerror(error)); 293133808Spjd gctl_error(req, "Not fully done."); 294133808Spjd continue; 295133808Spjd } 296133808Spjd if (verbose) 297133808Spjd printf("Metadata value stored on %s.\n", str); 298133808Spjd } 299133808Spjd} 300133808Spjd 301133808Spjdstatic void 302133808Spjdraid3_clear(struct gctl_req *req) 303133808Spjd{ 304133808Spjd const char *name; 305133808Spjd char param[16]; 306133808Spjd int *nargs, error, i; 307133808Spjd 308133808Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 309133808Spjd if (nargs == NULL) { 310133808Spjd gctl_error(req, "No '%s' argument.", "nargs"); 311133808Spjd return; 312133808Spjd } 313133808Spjd if (*nargs < 1) { 314133808Spjd gctl_error(req, "Too few arguments."); 315133808Spjd return; 316133808Spjd } 317133808Spjd 318133808Spjd for (i = 0; i < *nargs; i++) { 319133808Spjd snprintf(param, sizeof(param), "arg%u", i); 320133808Spjd name = gctl_get_asciiparam(req, param); 321133808Spjd 322133808Spjd error = g_metadata_clear(name, G_RAID3_MAGIC); 323133808Spjd if (error != 0) { 324133808Spjd fprintf(stderr, "Can't clear metadata on %s: %s.\n", 325133808Spjd name, strerror(error)); 326133808Spjd gctl_error(req, "Not fully done."); 327133808Spjd continue; 328133808Spjd } 329133808Spjd if (verbose) 330133808Spjd printf("Metadata cleared on %s.\n", name); 331133808Spjd } 332133808Spjd} 333133808Spjd 334133808Spjdstatic void 335133808Spjdraid3_dump(struct gctl_req *req) 336133808Spjd{ 337133808Spjd struct g_raid3_metadata md, tmpmd; 338133808Spjd const char *name; 339133808Spjd char param[16]; 340133808Spjd int *nargs, error, i; 341133808Spjd 342133808Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 343133808Spjd if (nargs == NULL) { 344133808Spjd gctl_error(req, "No '%s' argument.", "nargs"); 345133808Spjd return; 346133808Spjd } 347133808Spjd if (*nargs < 1) { 348133808Spjd gctl_error(req, "Too few arguments."); 349133808Spjd return; 350133808Spjd } 351133808Spjd 352133808Spjd for (i = 0; i < *nargs; i++) { 353133808Spjd snprintf(param, sizeof(param), "arg%u", i); 354133808Spjd name = gctl_get_asciiparam(req, param); 355133808Spjd 356133808Spjd error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd), 357133808Spjd G_RAID3_MAGIC); 358133808Spjd if (error != 0) { 359133808Spjd fprintf(stderr, "Can't read metadata from %s: %s.\n", 360133808Spjd name, strerror(error)); 361133808Spjd gctl_error(req, "Not fully done."); 362133808Spjd continue; 363133808Spjd } 364133808Spjd if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) { 365133808Spjd fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n", 366133808Spjd name); 367133808Spjd gctl_error(req, "Not fully done."); 368133808Spjd continue; 369133808Spjd } 370133808Spjd printf("Metadata on %s:\n", name); 371133808Spjd raid3_metadata_dump(&md); 372133808Spjd printf("\n"); 373133808Spjd } 374133808Spjd} 375