1132904Spjd/*- 2196879Spjd * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3132904Spjd * All rights reserved. 4132904Spjd * 5132904Spjd * Redistribution and use in source and binary forms, with or without 6132904Spjd * modification, are permitted provided that the following conditions 7132904Spjd * are met: 8132904Spjd * 1. Redistributions of source code must retain the above copyright 9132904Spjd * notice, this list of conditions and the following disclaimer. 10132904Spjd * 2. Redistributions in binary form must reproduce the above copyright 11132904Spjd * notice, this list of conditions and the following disclaimer in the 12132904Spjd * documentation and/or other materials provided with the distribution. 13155175Spjd * 14132904Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15132904Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16132904Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17132904Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18132904Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19132904Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20132904Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21132904Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22132904Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23132904Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24132904Spjd * SUCH DAMAGE. 25132904Spjd */ 26132904Spjd 27132904Spjd#include <sys/cdefs.h> 28132904Spjd__FBSDID("$FreeBSD$"); 29132904Spjd 30132904Spjd#include <sys/param.h> 31132904Spjd#include <errno.h> 32133447Spjd#include <paths.h> 33132904Spjd#include <stdio.h> 34132904Spjd#include <stdlib.h> 35132904Spjd#include <stdint.h> 36132904Spjd#include <string.h> 37132904Spjd#include <strings.h> 38132904Spjd#include <assert.h> 39132904Spjd#include <libgeom.h> 40132904Spjd#include <geom/mirror/g_mirror.h> 41132904Spjd#include <core/geom.h> 42132904Spjd#include <misc/subr.h> 43132904Spjd 44132904Spjduint32_t lib_version = G_LIB_VERSION; 45132904Spjduint32_t version = G_MIRROR_VERSION; 46132904Spjd 47212554Spjd#define GMIRROR_BALANCE "load" 48212554Spjd#define GMIRROR_SLICE "4096" 49212554Spjd#define GMIRROR_PRIORITY "0" 50212547Spjd 51133447Spjdstatic void mirror_main(struct gctl_req *req, unsigned flags); 52132904Spjdstatic void mirror_activate(struct gctl_req *req); 53132904Spjdstatic void mirror_clear(struct gctl_req *req); 54132904Spjdstatic void mirror_dump(struct gctl_req *req); 55132904Spjdstatic void mirror_label(struct gctl_req *req); 56132904Spjd 57132904Spjdstruct g_command class_commands[] = { 58212554Spjd { "activate", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS, 59143586Spjd "[-v] name prov ..." 60143586Spjd }, 61212554Spjd { "clear", G_FLAG_VERBOSE, mirror_main, G_NULL_OPTS, 62143586Spjd "[-v] prov ..." 63143586Spjd }, 64132904Spjd { "configure", G_FLAG_VERBOSE, NULL, 65132904Spjd { 66162868Spjd { 'a', "autosync", NULL, G_TYPE_BOOL }, 67212547Spjd { 'b', "balance", "", G_TYPE_STRING }, 68162868Spjd { 'd', "dynamic", NULL, G_TYPE_BOOL }, 69163888Spjd { 'f', "failsync", NULL, G_TYPE_BOOL }, 70163888Spjd { 'F', "nofailsync", NULL, G_TYPE_BOOL }, 71162868Spjd { 'h', "hardcode", NULL, G_TYPE_BOOL }, 72162868Spjd { 'n', "noautosync", NULL, G_TYPE_BOOL }, 73212554Spjd { 'p', "priority", "-1", G_TYPE_NUMBER }, 74212554Spjd { 's', "slice", "-1", G_TYPE_NUMBER }, 75132904Spjd G_OPT_SENTINEL 76143586Spjd }, 77212554Spjd "[-adfFhnv] [-b balance] [-s slice] name\n" 78212554Spjd "[-v] -p priority name prov" 79132904Spjd }, 80212554Spjd { "deactivate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, 81143586Spjd "[-v] name prov ..." 82143586Spjd }, 83260507Sae { "destroy", G_FLAG_VERBOSE, NULL, 84260507Sae { 85260507Sae { 'f', "force", NULL, G_TYPE_BOOL }, 86260507Sae G_OPT_SENTINEL 87260507Sae }, 88260507Sae "[-fv] name ..." 89260507Sae }, 90212554Spjd { "dump", 0, mirror_main, G_NULL_OPTS, 91143586Spjd "prov ..." 92143586Spjd }, 93212554Spjd { "forget", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, 94143586Spjd "name ..." 95143586Spjd }, 96132904Spjd { "label", G_FLAG_VERBOSE, mirror_main, 97132904Spjd { 98212547Spjd { 'b', "balance", GMIRROR_BALANCE, G_TYPE_STRING }, 99163888Spjd { 'F', "nofailsync", NULL, G_TYPE_BOOL }, 100162868Spjd { 'h', "hardcode", NULL, G_TYPE_BOOL }, 101162868Spjd { 'n', "noautosync", NULL, G_TYPE_BOOL }, 102212554Spjd { 's', "slice", GMIRROR_SLICE, G_TYPE_NUMBER }, 103132904Spjd G_OPT_SENTINEL 104143586Spjd }, 105212554Spjd "[-Fhnv] [-b balance] [-s slice] name prov ..." 106132904Spjd }, 107132904Spjd { "insert", G_FLAG_VERBOSE, NULL, 108132904Spjd { 109162868Spjd { 'h', "hardcode", NULL, G_TYPE_BOOL }, 110162868Spjd { 'i', "inactive", NULL, G_TYPE_BOOL }, 111212554Spjd { 'p', "priority", GMIRROR_PRIORITY, G_TYPE_NUMBER }, 112132904Spjd G_OPT_SENTINEL 113143586Spjd }, 114212554Spjd "[-hiv] [-p priority] name prov ..." 115132904Spjd }, 116212554Spjd { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, 117143586Spjd "[-v] name prov ..." 118143586Spjd }, 119212554Spjd { "remove", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, 120143586Spjd "[-v] name prov ..." 121143586Spjd }, 122132904Spjd { "stop", G_FLAG_VERBOSE, NULL, 123132904Spjd { 124162868Spjd { 'f', "force", NULL, G_TYPE_BOOL }, 125132904Spjd G_OPT_SENTINEL 126143586Spjd }, 127212554Spjd "[-fv] name ..." 128132904Spjd }, 129132904Spjd G_CMD_SENTINEL 130132904Spjd}; 131132904Spjd 132132904Spjdstatic int verbose = 0; 133132904Spjd 134132904Spjdstatic void 135133447Spjdmirror_main(struct gctl_req *req, unsigned flags) 136132904Spjd{ 137132904Spjd const char *name; 138132904Spjd 139133447Spjd if ((flags & G_FLAG_VERBOSE) != 0) 140132904Spjd verbose = 1; 141132904Spjd 142153190Spjd name = gctl_get_ascii(req, "verb"); 143132904Spjd if (name == NULL) { 144132904Spjd gctl_error(req, "No '%s' argument.", "verb"); 145132904Spjd return; 146132904Spjd } 147132904Spjd if (strcmp(name, "label") == 0) 148132904Spjd mirror_label(req); 149132904Spjd else if (strcmp(name, "clear") == 0) 150132904Spjd mirror_clear(req); 151132904Spjd else if (strcmp(name, "dump") == 0) 152132904Spjd mirror_dump(req); 153132904Spjd else if (strcmp(name, "activate") == 0) 154132904Spjd mirror_activate(req); 155132904Spjd else 156132904Spjd gctl_error(req, "Unknown command: %s.", name); 157132904Spjd} 158132904Spjd 159132904Spjdstatic void 160132904Spjdmirror_label(struct gctl_req *req) 161132904Spjd{ 162132904Spjd struct g_mirror_metadata md; 163132904Spjd u_char sector[512]; 164132904Spjd const char *str; 165132904Spjd unsigned sectorsize; 166132904Spjd off_t mediasize; 167153190Spjd intmax_t val; 168163888Spjd int error, i, nargs, bal, hardcode; 169132904Spjd 170153190Spjd nargs = gctl_get_int(req, "nargs"); 171153190Spjd if (nargs < 2) { 172132904Spjd gctl_error(req, "Too few arguments."); 173132904Spjd return; 174132904Spjd } 175132904Spjd 176132904Spjd strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic)); 177132904Spjd md.md_version = G_MIRROR_VERSION; 178153190Spjd str = gctl_get_ascii(req, "arg0"); 179132904Spjd strlcpy(md.md_name, str, sizeof(md.md_name)); 180132904Spjd md.md_mid = arc4random(); 181153190Spjd md.md_all = nargs - 1; 182132904Spjd md.md_mflags = 0; 183132904Spjd md.md_dflags = 0; 184139213Spjd md.md_genid = 0; 185132904Spjd md.md_syncid = 1; 186132904Spjd md.md_sync_offset = 0; 187153190Spjd val = gctl_get_intmax(req, "slice"); 188153190Spjd md.md_slice = val; 189153190Spjd str = gctl_get_ascii(req, "balance"); 190132904Spjd bal = balance_id(str); 191132904Spjd if (bal == -1) { 192151651Sceri gctl_error(req, "Invalid balance algorithm."); 193132904Spjd return; 194132904Spjd } 195132904Spjd md.md_balance = bal; 196163888Spjd if (gctl_get_int(req, "noautosync")) 197132904Spjd md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC; 198163888Spjd if (gctl_get_int(req, "nofailsync")) 199163888Spjd md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC; 200153190Spjd hardcode = gctl_get_int(req, "hardcode"); 201132904Spjd 202132904Spjd /* 203132904Spjd * Calculate sectorsize by finding least common multiple from 204132904Spjd * sectorsizes of every disk and find the smallest mediasize. 205132904Spjd */ 206132904Spjd mediasize = 0; 207132904Spjd sectorsize = 0; 208153190Spjd for (i = 1; i < nargs; i++) { 209132904Spjd unsigned ssize; 210132904Spjd off_t msize; 211132904Spjd 212153190Spjd str = gctl_get_ascii(req, "arg%d", i); 213132904Spjd msize = g_get_mediasize(str); 214132904Spjd ssize = g_get_sectorsize(str); 215132904Spjd if (msize == 0 || ssize == 0) { 216132904Spjd gctl_error(req, "Can't get informations about %s: %s.", 217132904Spjd str, strerror(errno)); 218132904Spjd return; 219132904Spjd } 220132904Spjd msize -= ssize; 221132904Spjd if (mediasize == 0 || (mediasize > 0 && msize < mediasize)) 222132904Spjd mediasize = msize; 223132904Spjd if (sectorsize == 0) 224132904Spjd sectorsize = ssize; 225132904Spjd else 226132904Spjd sectorsize = g_lcm(sectorsize, ssize); 227132904Spjd } 228132904Spjd md.md_mediasize = mediasize; 229132904Spjd md.md_sectorsize = sectorsize; 230163205Spjd md.md_mediasize -= (md.md_mediasize % md.md_sectorsize); 231132904Spjd 232132904Spjd /* 233132904Spjd * Clear last sector first, to spoil all components if device exists. 234132904Spjd */ 235153190Spjd for (i = 1; i < nargs; i++) { 236153190Spjd str = gctl_get_ascii(req, "arg%d", i); 237132904Spjd error = g_metadata_clear(str, NULL); 238132904Spjd if (error != 0) { 239132904Spjd gctl_error(req, "Can't store metadata on %s: %s.", str, 240132904Spjd strerror(error)); 241132904Spjd return; 242132904Spjd } 243132904Spjd } 244132904Spjd 245132904Spjd /* 246132904Spjd * Ok, store metadata (use disk number as priority). 247132904Spjd */ 248153190Spjd for (i = 1; i < nargs; i++) { 249153190Spjd str = gctl_get_ascii(req, "arg%d", i); 250132904Spjd md.md_did = arc4random(); 251132904Spjd md.md_priority = i - 1; 252142727Spjd md.md_provsize = g_get_mediasize(str); 253142727Spjd assert(md.md_provsize != 0); 254153190Spjd if (!hardcode) 255133447Spjd bzero(md.md_provider, sizeof(md.md_provider)); 256133447Spjd else { 257213662Sae if (strncmp(str, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 258213662Sae str += sizeof(_PATH_DEV) - 1; 259133447Spjd strlcpy(md.md_provider, str, sizeof(md.md_provider)); 260133447Spjd } 261132904Spjd mirror_metadata_encode(&md, sector); 262132904Spjd error = g_metadata_store(str, sector, sizeof(sector)); 263132904Spjd if (error != 0) { 264132904Spjd fprintf(stderr, "Can't store metadata on %s: %s.\n", 265132904Spjd str, strerror(error)); 266132904Spjd gctl_error(req, "Not fully done."); 267132904Spjd continue; 268132904Spjd } 269132904Spjd if (verbose) 270132904Spjd printf("Metadata value stored on %s.\n", str); 271132904Spjd } 272132904Spjd} 273132904Spjd 274132904Spjdstatic void 275132904Spjdmirror_clear(struct gctl_req *req) 276132904Spjd{ 277132904Spjd const char *name; 278153190Spjd int error, i, nargs; 279132904Spjd 280153190Spjd nargs = gctl_get_int(req, "nargs"); 281153190Spjd if (nargs < 1) { 282132904Spjd gctl_error(req, "Too few arguments."); 283132904Spjd return; 284132904Spjd } 285132904Spjd 286153190Spjd for (i = 0; i < nargs; i++) { 287153190Spjd name = gctl_get_ascii(req, "arg%d", i); 288132904Spjd error = g_metadata_clear(name, G_MIRROR_MAGIC); 289132904Spjd if (error != 0) { 290132904Spjd fprintf(stderr, "Can't clear metadata on %s: %s.\n", 291132904Spjd name, strerror(error)); 292132904Spjd gctl_error(req, "Not fully done."); 293132904Spjd continue; 294132904Spjd } 295132904Spjd if (verbose) 296155175Spjd printf("Metadata cleared on %s.\n", name); 297132904Spjd } 298132904Spjd} 299132904Spjd 300132904Spjdstatic void 301132904Spjdmirror_dump(struct gctl_req *req) 302132904Spjd{ 303132904Spjd struct g_mirror_metadata md, tmpmd; 304132904Spjd const char *name; 305153190Spjd int error, i, nargs; 306132904Spjd 307153190Spjd nargs = gctl_get_int(req, "nargs"); 308153190Spjd if (nargs < 1) { 309132904Spjd gctl_error(req, "Too few arguments."); 310132904Spjd return; 311132904Spjd } 312132904Spjd 313153190Spjd for (i = 0; i < nargs; i++) { 314153190Spjd name = gctl_get_ascii(req, "arg%d", i); 315132904Spjd error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd), 316132904Spjd G_MIRROR_MAGIC); 317132904Spjd if (error != 0) { 318132904Spjd fprintf(stderr, "Can't read metadata from %s: %s.\n", 319132904Spjd name, strerror(error)); 320132904Spjd gctl_error(req, "Not fully done."); 321132904Spjd continue; 322132904Spjd } 323132904Spjd if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) { 324132904Spjd fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n", 325132904Spjd name); 326132904Spjd gctl_error(req, "Not fully done."); 327132904Spjd continue; 328132904Spjd } 329132904Spjd printf("Metadata on %s:\n", name); 330132904Spjd mirror_metadata_dump(&md); 331132904Spjd printf("\n"); 332132904Spjd } 333132904Spjd} 334132904Spjd 335132904Spjdstatic void 336132904Spjdmirror_activate(struct gctl_req *req) 337132904Spjd{ 338132904Spjd struct g_mirror_metadata md, tmpmd; 339132904Spjd const char *name, *path; 340153190Spjd int error, i, nargs; 341132904Spjd 342153190Spjd nargs = gctl_get_int(req, "nargs"); 343153190Spjd if (nargs < 2) { 344132904Spjd gctl_error(req, "Too few arguments."); 345132904Spjd return; 346132904Spjd } 347153190Spjd name = gctl_get_ascii(req, "arg0"); 348132904Spjd 349153190Spjd for (i = 1; i < nargs; i++) { 350153190Spjd path = gctl_get_ascii(req, "arg%d", i); 351132904Spjd error = g_metadata_read(path, (u_char *)&tmpmd, sizeof(tmpmd), 352132904Spjd G_MIRROR_MAGIC); 353132904Spjd if (error != 0) { 354132904Spjd fprintf(stderr, "Cannot read metadata from %s: %s.\n", 355132904Spjd path, strerror(error)); 356132904Spjd gctl_error(req, "Not fully done."); 357132904Spjd continue; 358132904Spjd } 359132904Spjd if (mirror_metadata_decode((u_char *)&tmpmd, &md) != 0) { 360132904Spjd fprintf(stderr, 361132904Spjd "MD5 hash mismatch for provider %s, skipping.\n", 362132904Spjd path); 363132904Spjd gctl_error(req, "Not fully done."); 364132904Spjd continue; 365132904Spjd } 366132904Spjd if (strcmp(md.md_name, name) != 0) { 367132904Spjd fprintf(stderr, 368132904Spjd "Provider %s is not the mirror %s component.\n", 369132904Spjd path, name); 370132904Spjd gctl_error(req, "Not fully done."); 371132904Spjd continue; 372132904Spjd } 373132904Spjd md.md_dflags &= ~G_MIRROR_DISK_FLAG_INACTIVE; 374132904Spjd mirror_metadata_encode(&md, (u_char *)&tmpmd); 375132904Spjd error = g_metadata_store(path, (u_char *)&tmpmd, sizeof(tmpmd)); 376132904Spjd if (error != 0) { 377132904Spjd fprintf(stderr, "Cannot write metadata from %s: %s.\n", 378132904Spjd path, strerror(error)); 379132904Spjd gctl_error(req, "Not fully done."); 380132904Spjd continue; 381132904Spjd } 382132904Spjd if (verbose) 383155175Spjd printf("Provider %s activated.\n", path); 384132904Spjd } 385132904Spjd} 386