geom_stripe.c revision 135825
1/*- 2 * Copyright (c) 2004 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/stripe/geom_stripe.c 135825 2004-09-26 17:15:42Z 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/stripe/g_stripe.h> 41 42#include "core/geom.h" 43#include "misc/subr.h" 44 45 46uint32_t lib_version = G_LIB_VERSION; 47uint32_t version = G_STRIPE_VERSION; 48 49static intmax_t stripesize = 4096; 50 51static void stripe_main(struct gctl_req *req, unsigned flags); 52static void stripe_clear(struct gctl_req *req); 53static void stripe_dump(struct gctl_req *req); 54static void stripe_label(struct gctl_req *req); 55 56struct g_command class_commands[] = { 57 { "clear", G_FLAG_VERBOSE, stripe_main, G_NULL_OPTS }, 58 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, 59 { 60 { 's', "stripesize", &stripesize, G_TYPE_NUMBER }, 61 G_OPT_SENTINEL 62 } 63 }, 64 { "destroy", G_FLAG_VERBOSE, NULL, 65 { 66 { 'f', "force", NULL, G_TYPE_NONE }, 67 G_OPT_SENTINEL 68 } 69 }, 70 { "dump", 0, stripe_main, G_NULL_OPTS }, 71 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, stripe_main, 72 { 73 { 'h', "hardcode", NULL, G_TYPE_NONE }, 74 { 's', "stripesize", &stripesize, G_TYPE_NUMBER }, 75 G_OPT_SENTINEL 76 } 77 }, 78 { "stop", G_FLAG_VERBOSE, NULL, 79 { 80 { 'f', "force", NULL, G_TYPE_NONE }, 81 G_OPT_SENTINEL 82 } 83 }, 84 G_CMD_SENTINEL 85}; 86 87static int verbose = 0; 88 89void usage(const char *name); 90void 91usage(const char *name) 92{ 93 94 fprintf(stderr, "usage: %s create [-hv] [-s stripesize] <name> <prov> <prov> [prov [...]]\n", name); 95 fprintf(stderr, " %s destroy [-fv] <name> [name [...]]\n", name); 96 fprintf(stderr, " %s label [-hv] [-s stripesize] <name> <prov> <prov> [prov [...]]\n", name); 97 fprintf(stderr, " %s stop [-fv] <name> [name [...]]\n", name); 98 fprintf(stderr, " %s clear [-v] <prov> [prov [...]]\n", name); 99 fprintf(stderr, " %s dump <prov> [prov [...]]\n", name); 100} 101 102static void 103stripe_main(struct gctl_req *req, unsigned flags) 104{ 105 const char *name; 106 107 if ((flags & G_FLAG_VERBOSE) != 0) 108 verbose = 1; 109 110 name = gctl_get_asciiparam(req, "verb"); 111 if (name == NULL) { 112 gctl_error(req, "No '%s' argument.", "verb"); 113 return; 114 } 115 if (strcmp(name, "label") == 0) 116 stripe_label(req); 117 else if (strcmp(name, "clear") == 0) 118 stripe_clear(req); 119 else if (strcmp(name, "dump") == 0) 120 stripe_dump(req); 121 else 122 gctl_error(req, "Unknown command: %s.", name); 123} 124 125static void 126stripe_label(struct gctl_req *req) 127{ 128 struct g_stripe_metadata md; 129 intmax_t *stripesizep; 130 off_t compsize, msize; 131 u_char sector[512]; 132 unsigned i, ssize, secsize; 133 const char *name; 134 char param[16]; 135 int *hardcode, *nargs, error; 136 137 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 138 if (nargs == NULL) { 139 gctl_error(req, "No '%s' argument.", "nargs"); 140 return; 141 } 142 if (*nargs <= 2) { 143 gctl_error(req, "Too few arguments."); 144 return; 145 } 146 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 147 if (hardcode == NULL) { 148 gctl_error(req, "No '%s' argument.", "hardcode"); 149 return; 150 } 151 152 /* 153 * Clear last sector first to spoil all components if device exists. 154 */ 155 compsize = 0; 156 secsize = 0; 157 for (i = 1; i < (unsigned)*nargs; i++) { 158 snprintf(param, sizeof(param), "arg%u", i); 159 name = gctl_get_asciiparam(req, param); 160 161 msize = g_get_mediasize(name); 162 ssize = g_get_sectorsize(name); 163 if (msize == 0 || ssize == 0) { 164 gctl_error(req, "Can't get informations about %s: %s.", 165 name, strerror(errno)); 166 return; 167 } 168 msize -= ssize; 169 if (compsize == 0 || (compsize > 0 && msize < compsize)) 170 compsize = msize; 171 if (secsize == 0) 172 secsize = ssize; 173 else 174 secsize = g_lcm(secsize, ssize); 175 176 error = g_metadata_clear(name, NULL); 177 if (error != 0) { 178 gctl_error(req, "Can't store metadata on %s: %s.", name, 179 strerror(error)); 180 return; 181 } 182 } 183 184 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic)); 185 md.md_version = G_STRIPE_VERSION; 186 name = gctl_get_asciiparam(req, "arg0"); 187 if (name == NULL) { 188 gctl_error(req, "No 'arg%u' argument.", 0); 189 return; 190 } 191 strlcpy(md.md_name, name, sizeof(md.md_name)); 192 md.md_id = arc4random(); 193 md.md_all = *nargs - 1; 194 stripesizep = gctl_get_paraml(req, "stripesize", sizeof(*stripesizep)); 195 if (stripesizep == NULL) { 196 gctl_error(req, "No '%s' argument.", "stripesize"); 197 return; 198 } 199 if ((*stripesizep % secsize) != 0) { 200 gctl_error(req, "Stripesize should be multiple of %u.", 201 secsize); 202 return; 203 } 204 md.md_stripesize = *stripesizep; 205 206 /* 207 * Ok, store metadata. 208 */ 209 for (i = 1; i < (unsigned)*nargs; i++) { 210 snprintf(param, sizeof(param), "arg%u", i); 211 name = gctl_get_asciiparam(req, param); 212 213 msize = g_get_mediasize(name) - g_get_sectorsize(name); 214 if (compsize < msize) { 215 fprintf(stderr, 216 "warning: %s: only %jd bytes from %jd bytes used.\n", 217 name, (intmax_t)compsize, (intmax_t)msize); 218 } 219 220 md.md_no = i - 1; 221 if (!*hardcode) 222 bzero(md.md_provider, sizeof(md.md_provider)); 223 else { 224 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0) 225 name += strlen(_PATH_DEV); 226 strlcpy(md.md_provider, name, sizeof(md.md_provider)); 227 } 228 stripe_metadata_encode(&md, sector); 229 error = g_metadata_store(name, sector, sizeof(sector)); 230 if (error != 0) { 231 fprintf(stderr, "Can't store metadata on %s: %s.\n", 232 name, strerror(error)); 233 gctl_error(req, "Not fully done."); 234 continue; 235 } 236 if (verbose) 237 printf("Metadata value stored on %s.\n", name); 238 } 239} 240 241static void 242stripe_clear(struct gctl_req *req) 243{ 244 const char *name; 245 char param[16]; 246 unsigned i; 247 int *nargs, error; 248 249 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 250 if (nargs == NULL) { 251 gctl_error(req, "No '%s' argument.", "nargs"); 252 return; 253 } 254 if (*nargs < 1) { 255 gctl_error(req, "Too few arguments."); 256 return; 257 } 258 259 for (i = 0; i < (unsigned)*nargs; i++) { 260 snprintf(param, sizeof(param), "arg%u", i); 261 name = gctl_get_asciiparam(req, param); 262 263 error = g_metadata_clear(name, G_STRIPE_MAGIC); 264 if (error != 0) { 265 fprintf(stderr, "Can't clear metadata on %s: %s.\n", 266 name, strerror(error)); 267 gctl_error(req, "Not fully done."); 268 continue; 269 } 270 if (verbose) 271 printf("Metadata cleared on %s.\n", name); 272 } 273} 274 275static void 276stripe_metadata_dump(const struct g_stripe_metadata *md) 277{ 278 279 printf(" Magic string: %s\n", md->md_magic); 280 printf(" Metadata version: %u\n", (u_int)md->md_version); 281 printf(" Device name: %s\n", md->md_name); 282 printf(" Device ID: %u\n", (u_int)md->md_id); 283 printf(" Disk number: %u\n", (u_int)md->md_no); 284 printf("Total number of disks: %u\n", (u_int)md->md_all); 285 printf(" Stripe size: %u\n", (u_int)md->md_stripesize); 286 printf(" Hardcoded provider: %s\n", md->md_provider); 287} 288 289static void 290stripe_dump(struct gctl_req *req) 291{ 292 struct g_stripe_metadata md, tmpmd; 293 const char *name; 294 char param[16]; 295 int *nargs, error, i; 296 297 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 298 if (nargs == NULL) { 299 gctl_error(req, "No '%s' argument.", "nargs"); 300 return; 301 } 302 if (*nargs < 1) { 303 gctl_error(req, "Too few arguments."); 304 return; 305 } 306 307 for (i = 0; i < *nargs; i++) { 308 snprintf(param, sizeof(param), "arg%u", i); 309 name = gctl_get_asciiparam(req, param); 310 311 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd), 312 G_STRIPE_MAGIC); 313 if (error != 0) { 314 fprintf(stderr, "Can't read metadata from %s: %s.\n", 315 name, strerror(error)); 316 gctl_error(req, "Not fully done."); 317 continue; 318 } 319 stripe_metadata_decode((u_char *)&tmpmd, &md); 320 printf("Metadata on %s:\n", name); 321 stripe_metadata_dump(&md); 322 printf("\n"); 323 } 324} 325