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