ccdconfig.c revision 116126
157429Smarkm/* 257429Smarkm * Copyright (c) 2003 Poul-Henning Kamp 357429Smarkm * Copyright (c) 1995 Jason R. Thorpe. 457429Smarkm * All rights reserved. 557429Smarkm * 660573Skris * Redistribution and use in source and binary forms, with or without 765668Skris * modification, are permitted provided that the following conditions 865668Skris * are met: 965668Skris * 1. Redistributions of source code must retain the above copyright 1065668Skris * notice, this list of conditions and the following disclaimer. 1165668Skris * 2. Redistributions in binary form must reproduce the above copyright 1257429Smarkm * notice, this list of conditions and the following disclaimer in the 1357429Smarkm * documentation and/or other materials provided with the distribution. 1457429Smarkm * 3. All advertising materials mentioning features or use of this software 15126274Sdes * must display the following acknowledgement: 1657429Smarkm * This product includes software developed for the NetBSD Project 1757429Smarkm * by Jason R. Thorpe. 1857429Smarkm * 4. The name of the author may not be used to endorse or promote products 1976259Sgreen * derived from this software without specific prior written permission. 2057429Smarkm * 2157429Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2257429Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2360573Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2457429Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2557429Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26120489Sjoe * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27120489Sjoe * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28120489Sjoe * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29120489Sjoe * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30120489Sjoe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3157429Smarkm * SUCH DAMAGE. 3257429Smarkm */ 3357429Smarkm 3457429Smarkm#include <sys/cdefs.h> 3557429Smarkm__FBSDID("$FreeBSD: head/sbin/ccdconfig/ccdconfig.c 116126 2003-06-09 21:50:41Z phk $"); 3657429Smarkm 3760573Skris#include <sys/param.h> 3857429Smarkm#include <sys/linker.h> 3957429Smarkm#include <sys/module.h> 40120489Sjoe#include <ctype.h> 41120489Sjoe#include <err.h> 42124208Sdes#include <errno.h> 43120489Sjoe#include <limits.h> 44120489Sjoe#include <paths.h> 4557429Smarkm#include <stdio.h> 4657429Smarkm#include <stdlib.h> 4757429Smarkm#include <string.h> 4857429Smarkm#include <unistd.h> 4957429Smarkm#include <libgeom.h> 5057429Smarkm 5157429Smarkm#define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */ 5260573Skris#define CCDF_MIRROR 0x04 /* use mirroring */ 5357429Smarkm 5457429Smarkm#include "pathnames.h" 5557429Smarkm 5657429Smarkmstatic int lineno = 0; 5757429Smarkmstatic int verbose = 0; 5857429Smarkmstatic const char *ccdconf = _PATH_CCDCONF; 5957429Smarkm 6057429Smarkmstruct flagval { 6160573Skris const char *fv_flag; 6292555Sdes int fv_val; 6357429Smarkm} flagvaltab[] = { 6492555Sdes { "CCDF_UNIFORM", CCDF_UNIFORM }, 6592555Sdes { "uniform", CCDF_UNIFORM }, 6692555Sdes { "CCDF_MIRROR", CCDF_MIRROR }, 6757429Smarkm { "mirror", CCDF_MIRROR }, 6857429Smarkm { "none", 0 }, 6957429Smarkm { NULL, 0 }, 7057429Smarkm}; 7157429Smarkm 7257429Smarkm#define CCD_CONFIG 0 /* configure a device */ 7357429Smarkm#define CCD_CONFIGALL 1 /* configure all devices */ 7457429Smarkm#define CCD_UNCONFIG 2 /* unconfigure a device */ 7592555Sdes#define CCD_UNCONFIGALL 3 /* unconfigure all devices */ 7692555Sdes#define CCD_DUMP 4 /* dump a ccd's configuration */ 7757429Smarkm 78120113Snectarstatic int do_single(int, char **, int); 7992555Sdesstatic int do_all(int); 8092555Sdesstatic int dump_ccd(int, char **); 8199060Sdesstatic int flags_to_val(char *); 8299060Sdesstatic int resolve_ccdname(char *); 8399060Sdesstatic void usage(void); 8457429Smarkm 8557429Smarkmint 8657429Smarkmmain(int argc, char *argv[]) 8757429Smarkm{ 8857429Smarkm int ch, options = 0, action = CCD_CONFIG; 8957429Smarkm 9057429Smarkm while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) { 9157429Smarkm switch (ch) { 9292555Sdes case 'c': 9357429Smarkm action = CCD_CONFIG; 9492555Sdes ++options; 9557429Smarkm break; 9657429Smarkm 9757429Smarkm case 'C': 9857429Smarkm action = CCD_CONFIGALL; 9957429Smarkm ++options; 10057429Smarkm break; 10157429Smarkm 10257429Smarkm case 'f': 10357429Smarkm ccdconf = optarg; 10457429Smarkm break; 10557429Smarkm 10657429Smarkm case 'g': 10757429Smarkm action = CCD_DUMP; 108126274Sdes break; 109120113Snectar 110120113Snectar case 'u': 11199060Sdes action = CCD_UNCONFIG; 112120113Snectar ++options; 113120113Snectar break; 114120113Snectar 11557429Smarkm case 'U': 11692555Sdes action = CCD_UNCONFIGALL; 11757429Smarkm ++options; 11857429Smarkm break; 11957429Smarkm 12057429Smarkm case 'v': 12176259Sgreen verbose = 1; 12257429Smarkm break; 12357429Smarkm 12457429Smarkm default: 12557429Smarkm usage(); 12657429Smarkm } 12757429Smarkm } 12857429Smarkm argc -= optind; 12960573Skris argv += optind; 13092555Sdes 13157429Smarkm if (options > 1) 13257429Smarkm usage(); 13376259Sgreen 13476259Sgreen if (modfind("g_ccd") < 0) { 13557429Smarkm /* Not present in kernel, try loading it */ 13657429Smarkm if (kldload("geom_ccd") < 0 || modfind("g_ccd") < 0) 13757429Smarkm warn("geom_ccd module not available!"); 13857429Smarkm } 13957429Smarkm 14057429Smarkm switch (action) { 14160573Skris case CCD_CONFIG: 14276259Sgreen case CCD_UNCONFIG: 14357429Smarkm exit(do_single(argc, argv, action)); 14457429Smarkm /* NOTREACHED */ 14560573Skris 14657429Smarkm case CCD_CONFIGALL: 14757429Smarkm case CCD_UNCONFIGALL: 14857429Smarkm exit(do_all(action)); 14957429Smarkm /* NOTREACHED */ 15057429Smarkm 15160573Skris case CCD_DUMP: 15276259Sgreen exit(dump_ccd(argc, argv)); 15357429Smarkm /* NOTREACHED */ 15457429Smarkm } 15560573Skris /* NOTREACHED */ 15657429Smarkm return (0); 15757429Smarkm} 15857429Smarkm 15957429Smarkmstatic int 16057429Smarkmdo_single(int argc, char **argv, int action) 16192555Sdes{ 16257429Smarkm char *cp, *cp2; 16357429Smarkm int ccd, noflags = 0, i, ileave, flags = 0; 16457429Smarkm struct gctl_req *grq; 16557429Smarkm char const *errstr; 16657429Smarkm char buf1[BUFSIZ]; 16757429Smarkm int ex; 16857429Smarkm 16960573Skris /* 17057429Smarkm * If unconfiguring, all arguments are treated as ccds. 17157429Smarkm */ 172126274Sdes if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) { 17392555Sdes ex = 0; 17457429Smarkm for (i = 0; argc != 0; ) { 17576259Sgreen cp = *argv++; --argc; 17676259Sgreen if ((ccd = resolve_ccdname(cp)) < 0) { 17776259Sgreen warnx("invalid ccd name: %s", cp); 17876259Sgreen i = 1; 17976259Sgreen continue; 18076259Sgreen } 18176259Sgreen grq = gctl_get_handle(); 18276259Sgreen gctl_ro_param(grq, "verb", -1, "destroy geom"); 18357429Smarkm gctl_ro_param(grq, "class", -1, "CCD"); 184 sprintf(buf1, "ccd%d", ccd); 185 gctl_ro_param(grq, "geom", -1, buf1); 186 errstr = gctl_issue(grq); 187 if (errstr == NULL) { 188 if (verbose) 189 printf("%s unconfigured\n", cp); 190 gctl_free(grq); 191 continue; 192 } 193 warnx( 194 "%s\nor possibly kernel and ccdconfig out of sync", 195 errstr); 196 ex = 1; 197 } 198 return (ex); 199 } 200 201 /* Make sure there are enough arguments. */ 202 if (argc < 4) { 203 if (argc == 3) { 204 /* Assume that no flags are specified. */ 205 noflags = 1; 206 } else { 207 if (action == CCD_CONFIGALL) { 208 warnx("%s: bad line: %d", ccdconf, lineno); 209 return (1); 210 } else 211 usage(); 212 } 213 } 214 215 /* First argument is the ccd to configure. */ 216 cp = *argv++; --argc; 217 if ((ccd = resolve_ccdname(cp)) < 0) { 218 warnx("invalid ccd name: %s", cp); 219 return (1); 220 } 221 222 /* Next argument is the interleave factor. */ 223 cp = *argv++; --argc; 224 errno = 0; /* to check for ERANGE */ 225 ileave = (int)strtol(cp, &cp2, 10); 226 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) { 227 warnx("invalid interleave factor: %s", cp); 228 return (1); 229 } 230 231 if (noflags == 0) { 232 /* Next argument is the ccd configuration flags. */ 233 cp = *argv++; --argc; 234 if ((flags = flags_to_val(cp)) < 0) { 235 warnx("invalid flags argument: %s", cp); 236 return (1); 237 } 238 } 239 grq = gctl_get_handle(); 240 gctl_ro_param(grq, "verb", -1, "create geom"); 241 gctl_ro_param(grq, "class", -1, "CCD"); 242 gctl_ro_param(grq, "unit", sizeof(ccd), &ccd); 243 gctl_ro_param(grq, "ileave", sizeof(ileave), &ileave); 244 if (flags & CCDF_UNIFORM) 245 gctl_ro_param(grq, "uniform", -1, ""); 246 if (flags & CCDF_MIRROR) 247 gctl_ro_param(grq, "mirror", -1, ""); 248 gctl_ro_param(grq, "nprovider", sizeof(argc), &argc); 249 for (i = 0; i < argc; i++) { 250 sprintf(buf1, "provider%d", i); 251 cp = argv[i]; 252 if (!strncmp(cp, _PATH_DEV, strlen(_PATH_DEV))) 253 cp += strlen(_PATH_DEV); 254 gctl_ro_param(grq, buf1, -1, cp); 255 } 256 gctl_rw_param(grq, "output", sizeof(buf1), buf1); 257 errstr = gctl_issue(grq); 258 if (errstr == NULL) { 259 if (verbose) { 260 printf("%s", buf1); 261 } 262 gctl_free(grq); 263 return (0); 264 } 265 warnx( 266 "%s\nor possibly kernel and ccdconfig out of sync", 267 errstr); 268 return (1); 269} 270 271static int 272do_all(int action) 273{ 274 FILE *f; 275 char line[_POSIX2_LINE_MAX]; 276 char *cp, **argv; 277 int argc, rval; 278 gid_t egid; 279 280 rval = 0; 281 egid = getegid(); 282 setegid(getgid()); 283 if ((f = fopen(ccdconf, "r")) == NULL) { 284 setegid(egid); 285 warn("fopen: %s", ccdconf); 286 return (1); 287 } 288 setegid(egid); 289 290 while (fgets(line, sizeof(line), f) != NULL) { 291 argc = 0; 292 argv = NULL; 293 ++lineno; 294 if ((cp = strrchr(line, '\n')) != NULL) 295 *cp = '\0'; 296 297 /* Break up the line and pass it's contents to do_single(). */ 298 if (line[0] == '\0') 299 goto end_of_line; 300 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) { 301 if (*cp == '#') 302 break; 303 if ((argv = realloc(argv, 304 sizeof(char *) * ++argc)) == NULL) { 305 warnx("no memory to configure ccds"); 306 return (1); 307 } 308 argv[argc - 1] = cp; 309 /* 310 * If our action is to unconfigure all, then pass 311 * just the first token to do_single() and ignore 312 * the rest. Since this will be encountered on 313 * our first pass through the line, the Right 314 * Thing will happen. 315 */ 316 if (action == CCD_UNCONFIGALL) { 317 if (do_single(argc, argv, action)) 318 rval = 1; 319 goto end_of_line; 320 } 321 } 322 if (argc != 0) 323 if (do_single(argc, argv, action)) 324 rval = 1; 325 326 end_of_line: 327 if (argv != NULL) 328 free(argv); 329 } 330 331 (void)fclose(f); 332 return (rval); 333} 334 335static int 336resolve_ccdname(char *name) 337{ 338 339 if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV))) 340 name += strlen(_PATH_DEV); 341 if (strncmp(name, "ccd", 3)) 342 return -1; 343 name += 3; 344 if (!isdigit(*name)) 345 return -1; 346 return (strtoul(name, NULL, 10)); 347} 348 349static int 350dumpout(int unit) 351{ 352 static int v; 353 struct gctl_req *grq; 354 int ncp; 355 char *cp; 356 char const *errstr; 357 358 grq = gctl_get_handle(); 359 ncp = 65536; 360 cp = malloc(ncp); 361 gctl_ro_param(grq, "verb", -1, "list"); 362 gctl_ro_param(grq, "class", -1, "CCD"); 363 gctl_ro_param(grq, "unit", sizeof(unit), &unit); 364 gctl_rw_param(grq, "output", ncp, cp); 365 errstr = gctl_issue(grq); 366 if (errstr != NULL) 367 errx(1, "%s\nor possibly kernel and ccdconfig out of sync", 368 errstr); 369 if (strlen(cp) == 0) 370 errx(1, "ccd%d not configured", unit); 371 if (verbose && !v) { 372 printf("# ccd\t\tileave\tflags\tcomponent devices\n"); 373 v = 1; 374 } 375 printf("%s", cp); 376 free(cp); 377 return (0); 378} 379 380static int 381dump_ccd(int argc, char **argv) 382{ 383 int i, err; 384 385 if (argc == 0) { 386 err = dumpout(-1); 387 } else { 388 err = 0; 389 for (i = 0; err == 0 && i < argc; i++) 390 err = dumpout(resolve_ccdname(argv[i])); 391 } 392 return (err); 393} 394 395static int 396flags_to_val(char *flags) 397{ 398 char *cp, *tok; 399 int i, tmp, val; 400 size_t flagslen; 401 402 errno = 0; /* to check for ERANGE */ 403 val = (int)strtol(flags, &cp, 0); 404 if ((errno != ERANGE) && (*cp == '\0')) { 405 if (val & ~(CCDF_UNIFORM|CCDF_MIRROR)) 406 return (-1); 407 return (val); 408 } 409 410 flagslen = strlen(flags); 411 /* Check for values represented by strings. */ 412 if ((cp = strdup(flags)) == NULL) 413 err(1, "no memory to parse flags"); 414 tmp = 0; 415 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) { 416 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i) 417 if (strcmp(tok, flagvaltab[i].fv_flag) == 0) 418 break; 419 if (flagvaltab[i].fv_flag == NULL) { 420 free(cp); 421 return (-1); 422 } 423 tmp |= flagvaltab[i].fv_val; 424 } 425 426 /* If we get here, the string was ok. */ 427 free(cp); 428 return (tmp); 429} 430 431static void 432usage(void) 433{ 434 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n", 435 "usage: ccdconfig [-cv] ccd ileave [flags] dev [...]", 436 " ccdconfig -C [-v] [-f config_file]", 437 " ccdconfig -u [-v] ccd [...]", 438 " ccdconfig -U [-v] [-f config_file]", 439 " ccdconfig -g [ccd [...]]"); 440 exit(1); 441} 442