geom_raid3.c revision 201578
1/*- 2 * Copyright (c) 2004-2005 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 201578 2010-01-05 13:25:12Z mav $"); 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 intmax_t default_blocksize = 0; 49 50static void raid3_main(struct gctl_req *req, unsigned f); 51static void raid3_clear(struct gctl_req *req); 52static void raid3_dump(struct gctl_req *req); 53static void raid3_label(struct gctl_req *req); 54 55struct g_command class_commands[] = { 56 { "clear", G_FLAG_VERBOSE, raid3_main, G_NULL_OPTS, NULL, 57 "[-v] prov ..." 58 }, 59 { "configure", G_FLAG_VERBOSE, NULL, 60 { 61 { 'a', "autosync", NULL, G_TYPE_BOOL }, 62 { 'd', "dynamic", NULL, G_TYPE_BOOL }, 63 { 'f', "failsync", NULL, G_TYPE_BOOL }, 64 { 'F', "nofailsync", NULL, G_TYPE_BOOL }, 65 { 'h', "hardcode", NULL, G_TYPE_BOOL }, 66 { 'n', "noautosync", NULL, G_TYPE_BOOL }, 67 { 'r', "round_robin", NULL, G_TYPE_BOOL }, 68 { 'R', "noround_robin", NULL, G_TYPE_BOOL }, 69 { 'w', "verify", NULL, G_TYPE_BOOL }, 70 { 'W', "noverify", NULL, G_TYPE_BOOL }, 71 G_OPT_SENTINEL 72 }, 73 NULL, "[-adfFhnrRvwW] name" 74 }, 75 { "dump", 0, raid3_main, G_NULL_OPTS, NULL, 76 "prov ..." 77 }, 78 { "insert", G_FLAG_VERBOSE, NULL, 79 { 80 { 'h', "hardcode", NULL, G_TYPE_BOOL }, 81 { 'n', "number", NULL, G_TYPE_NUMBER }, 82 G_OPT_SENTINEL 83 }, 84 NULL, "[-hv] <-n number> name prov" 85 }, 86 { "label", G_FLAG_VERBOSE, raid3_main, 87 { 88 { 'h', "hardcode", NULL, G_TYPE_BOOL }, 89 { 'F', "nofailsync", NULL, G_TYPE_BOOL }, 90 { 'n', "noautosync", NULL, G_TYPE_BOOL }, 91 { 'r', "round_robin", NULL, G_TYPE_BOOL }, 92 { 's', "blocksize", &default_blocksize, G_TYPE_NUMBER }, 93 { 'w', "verify", NULL, G_TYPE_BOOL }, 94 G_OPT_SENTINEL 95 }, 96 NULL, "[-hFnrvw] [-s blocksize] name prov prov prov ..." 97 }, 98 { "rebuild", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, NULL, 99 "[-v] name prov" 100 }, 101 { "remove", G_FLAG_VERBOSE, NULL, 102 { 103 { 'n', "number", NULL, G_TYPE_NUMBER }, 104 G_OPT_SENTINEL 105 }, 106 NULL, "[-v] <-n number> name" 107 }, 108 { "stop", G_FLAG_VERBOSE, NULL, 109 { 110 { 'f', "force", NULL, G_TYPE_BOOL }, 111 G_OPT_SENTINEL 112 }, 113 NULL, "[-fv] name ..." 114 }, 115 G_CMD_SENTINEL 116}; 117 118static int verbose = 0; 119 120static void 121raid3_main(struct gctl_req *req, unsigned flags) 122{ 123 const char *name; 124 125 if ((flags & G_FLAG_VERBOSE) != 0) 126 verbose = 1; 127 128 name = gctl_get_ascii(req, "verb"); 129 if (name == NULL) { 130 gctl_error(req, "No '%s' argument.", "verb"); 131 return; 132 } 133 if (strcmp(name, "label") == 0) 134 raid3_label(req); 135 else if (strcmp(name, "clear") == 0) 136 raid3_clear(req); 137 else if (strcmp(name, "dump") == 0) 138 raid3_dump(req); 139 else 140 gctl_error(req, "Unknown command: %s.", name); 141} 142 143static void 144raid3_label(struct gctl_req *req) 145{ 146 struct g_raid3_metadata md; 147 u_char sector[512]; 148 const char *str; 149 unsigned sectorsize, ssize; 150 off_t mediasize, msize; 151 int hardcode, round_robin, verify; 152 int error, i, nargs; 153 154 nargs = gctl_get_int(req, "nargs"); 155 if (nargs < 4) { 156 gctl_error(req, "Too few arguments."); 157 return; 158 } 159 if (bitcount32(nargs - 2) != 1) { 160 gctl_error(req, "Invalid number of components."); 161 return; 162 } 163 164 strlcpy(md.md_magic, G_RAID3_MAGIC, sizeof(md.md_magic)); 165 md.md_version = G_RAID3_VERSION; 166 str = gctl_get_ascii(req, "arg0"); 167 strlcpy(md.md_name, str, sizeof(md.md_name)); 168 md.md_id = arc4random(); 169 md.md_all = nargs - 1; 170 md.md_mflags = 0; 171 md.md_dflags = 0; 172 md.md_genid = 0; 173 md.md_syncid = 1; 174 md.md_sync_offset = 0; 175 if (gctl_get_int(req, "noautosync")) 176 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC; 177 if (gctl_get_int(req, "nofailsync")) 178 md.md_mflags |= G_RAID3_DEVICE_FLAG_NOFAILSYNC; 179 round_robin = gctl_get_int(req, "round_robin"); 180 if (round_robin) 181 md.md_mflags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN; 182 verify = gctl_get_int(req, "verify"); 183 if (verify) 184 md.md_mflags |= G_RAID3_DEVICE_FLAG_VERIFY; 185 if (round_robin && verify) { 186 gctl_error(req, "Both '%c' and '%c' options given.", 'r', 'w'); 187 return; 188 } 189 hardcode = gctl_get_int(req, "hardcode"); 190 191 /* 192 * Calculate sectorsize by finding least common multiple from 193 * sectorsizes of every disk and find the smallest mediasize. 194 */ 195 mediasize = 0; 196 sectorsize = gctl_get_intmax(req, "blocksize"); 197 for (i = 1; i < nargs; i++) { 198 str = gctl_get_ascii(req, "arg%d", i); 199 msize = g_get_mediasize(str); 200 ssize = g_get_sectorsize(str); 201 if (msize == 0 || ssize == 0) { 202 gctl_error(req, "Can't get informations about %s: %s.", 203 str, strerror(errno)); 204 return; 205 } 206 msize -= ssize; 207 if (mediasize == 0 || (mediasize > 0 && msize < mediasize)) 208 mediasize = msize; 209 if (sectorsize == 0) 210 sectorsize = ssize; 211 else 212 sectorsize = g_lcm(sectorsize, ssize); 213 } 214 md.md_mediasize = mediasize * (nargs - 2); 215 md.md_sectorsize = sectorsize * (nargs - 2); 216 md.md_mediasize -= (md.md_mediasize % md.md_sectorsize); 217 218 /* 219 * Clear last sector first, to spoil all components if device exists. 220 */ 221 for (i = 1; i < nargs; i++) { 222 str = gctl_get_ascii(req, "arg%d", i); 223 error = g_metadata_clear(str, NULL); 224 if (error != 0) { 225 gctl_error(req, "Can't store metadata on %s: %s.", str, 226 strerror(error)); 227 return; 228 } 229 } 230 231 /* 232 * Ok, store metadata (use disk number as priority). 233 */ 234 for (i = 1; i < nargs; i++) { 235 str = gctl_get_ascii(req, "arg%d", i); 236 msize = g_get_mediasize(str); 237 ssize = g_get_sectorsize(str); 238 if (mediasize < msize - ssize) { 239 fprintf(stderr, 240 "warning: %s: only %jd bytes from %jd bytes used.\n", 241 str, (intmax_t)mediasize, (intmax_t)(msize - ssize)); 242 } 243 244 md.md_no = i - 1; 245 md.md_provsize = msize; 246 if (!hardcode) 247 bzero(md.md_provider, sizeof(md.md_provider)); 248 else { 249 if (strncmp(str, _PATH_DEV, strlen(_PATH_DEV)) == 0) 250 str += strlen(_PATH_DEV); 251 strlcpy(md.md_provider, str, sizeof(md.md_provider)); 252 } 253 if (verify && md.md_no == md.md_all - 1) { 254 /* 255 * In "verify" mode, force synchronization of parity 256 * component on start. 257 */ 258 md.md_syncid = 0; 259 } 260 raid3_metadata_encode(&md, sector); 261 error = g_metadata_store(str, sector, sizeof(sector)); 262 if (error != 0) { 263 fprintf(stderr, "Can't store metadata on %s: %s.\n", 264 str, strerror(error)); 265 gctl_error(req, "Not fully done."); 266 continue; 267 } 268 if (verbose) 269 printf("Metadata value stored on %s.\n", str); 270 } 271} 272 273static void 274raid3_clear(struct gctl_req *req) 275{ 276 const char *name; 277 int error, i, nargs; 278 279 nargs = gctl_get_int(req, "nargs"); 280 if (nargs < 1) { 281 gctl_error(req, "Too few arguments."); 282 return; 283 } 284 285 for (i = 0; i < nargs; i++) { 286 name = gctl_get_ascii(req, "arg%d", i); 287 error = g_metadata_clear(name, G_RAID3_MAGIC); 288 if (error != 0) { 289 fprintf(stderr, "Can't clear metadata on %s: %s.\n", 290 name, strerror(error)); 291 gctl_error(req, "Not fully done."); 292 continue; 293 } 294 if (verbose) 295 printf("Metadata cleared on %s.\n", name); 296 } 297} 298 299static void 300raid3_dump(struct gctl_req *req) 301{ 302 struct g_raid3_metadata md, tmpmd; 303 const char *name; 304 int error, i, nargs; 305 306 nargs = gctl_get_int(req, "nargs"); 307 if (nargs < 1) { 308 gctl_error(req, "Too few arguments."); 309 return; 310 } 311 312 for (i = 0; i < nargs; i++) { 313 name = gctl_get_ascii(req, "arg%d", i); 314 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd), 315 G_RAID3_MAGIC); 316 if (error != 0) { 317 fprintf(stderr, "Can't read metadata from %s: %s.\n", 318 name, strerror(error)); 319 gctl_error(req, "Not fully done."); 320 continue; 321 } 322 if (raid3_metadata_decode((u_char *)&tmpmd, &md) != 0) { 323 fprintf(stderr, "MD5 hash mismatch for %s, skipping.\n", 324 name); 325 gctl_error(req, "Not fully done."); 326 continue; 327 } 328 printf("Metadata on %s:\n", name); 329 raid3_metadata_dump(&md); 330 printf("\n"); 331 } 332} 333