1 /* $NetBSD: ccdconfig.c,v 1.2.2.1 1995/11/11 02:43:35 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Jason R. Thorpe. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the NetBSD Project 18 * by Jason R. Thorpe. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sbin/ccdconfig/ccdconfig.c,v 1.16.2.2 2000/12/11 01:03:25 obrien Exp $ 35 * $DragonFly: src/sbin/ccdconfig/ccdconfig.c,v 1.7 2006/10/16 22:02:22 pavalos Exp $ 36 */ 37 38 #include <sys/param.h> 39 #include <sys/linker.h> 40 #include <sys/disklabel.h> 41 #include <sys/stat.h> 42 #include <sys/module.h> 43 #include <ctype.h> 44 #include <err.h> 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <kvm.h> 48 #include <limits.h> 49 #include <paths.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 55 #include <sys/devicestat.h> 56 #include <sys/ccdvar.h> 57 58 #include "pathnames.h" 59 60 static int lineno = 0; 61 static int verbose = 0; 62 static const char *ccdconf = _PATH_CCDCONF; 63 64 static char *core = NULL; 65 static char *kernel = NULL; 66 67 struct flagval { 68 const char *fv_flag; 69 int fv_val; 70 } flagvaltab[] = { 71 { "CCDF_SWAP", CCDF_SWAP }, 72 { "CCDF_UNIFORM", CCDF_UNIFORM }, 73 { "CCDF_MIRROR", CCDF_MIRROR }, 74 { "CCDF_PARITY", CCDF_PARITY }, 75 { NULL, 0 }, 76 }; 77 78 static struct nlist nl[] = { 79 { "_ccd_softc", 0, 0, 0, 0 }, 80 #define SYM_CCDSOFTC 0 81 { "_numccd", 0, 0, 0, 0 }, 82 #define SYM_NUMCCD 1 83 { NULL , 0, 0, 0, 0 }, 84 }; 85 86 #define CCD_CONFIG 0 /* configure a device */ 87 #define CCD_CONFIGALL 1 /* configure all devices */ 88 #define CCD_UNCONFIG 2 /* unconfigure a device */ 89 #define CCD_UNCONFIGALL 3 /* unconfigure all devices */ 90 #define CCD_DUMP 4 /* dump a ccd's configuration */ 91 92 static int checkdev(char *); 93 static int do_io(char *, u_long, struct ccd_ioctl *); 94 static int do_single(int, char **, int); 95 static int do_all(int); 96 static int dump_ccd(int, char **); 97 static int getmaxpartitions(void); 98 static int getrawpartition(void); 99 static int flags_to_val(char *); 100 static void print_ccd_info(struct ccd_softc *, kvm_t *); 101 static char *resolve_ccdname(char *); 102 static void usage(void); 103 104 int 105 main(int argc, char **argv) 106 { 107 int ch, options = 0, action = CCD_CONFIG; 108 109 while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) { 110 switch (ch) { 111 case 'c': 112 action = CCD_CONFIG; 113 ++options; 114 break; 115 116 case 'C': 117 action = CCD_CONFIGALL; 118 ++options; 119 break; 120 121 case 'f': 122 ccdconf = optarg; 123 break; 124 125 case 'g': 126 action = CCD_DUMP; 127 break; 128 129 case 'M': 130 core = optarg; 131 break; 132 133 case 'N': 134 kernel = optarg; 135 break; 136 137 case 'u': 138 action = CCD_UNCONFIG; 139 ++options; 140 break; 141 142 case 'U': 143 action = CCD_UNCONFIGALL; 144 ++options; 145 break; 146 147 case 'v': 148 verbose = 1; 149 break; 150 151 default: 152 usage(); 153 } 154 } 155 argc -= optind; 156 argv += optind; 157 158 if (options > 1) 159 usage(); 160 161 /* 162 * Discard setgid privileges if not the running kernel so that bad 163 * guys can't print interesting stuff from kernel memory. 164 */ 165 if (core != NULL || kernel != NULL || action != CCD_DUMP) { 166 setegid(getgid()); 167 setgid(getgid()); 168 } 169 170 if (modfind("ccd") < 0) { 171 /* Not present in kernel, try loading it */ 172 if (kldload("ccd") < 0 || modfind("ccd") < 0) 173 warn("ccd module not available!"); 174 } 175 176 switch (action) { 177 case CCD_CONFIG: 178 case CCD_UNCONFIG: 179 exit(do_single(argc, argv, action)); 180 /* NOTREACHED */ 181 182 case CCD_CONFIGALL: 183 case CCD_UNCONFIGALL: 184 exit(do_all(action)); 185 /* NOTREACHED */ 186 187 case CCD_DUMP: 188 exit(dump_ccd(argc, argv)); 189 /* NOTREACHED */ 190 } 191 /* NOTREACHED */ 192 return (0); 193 } 194 195 static int 196 do_single(int argc, char **argv, int action) 197 { 198 struct ccd_ioctl ccio; 199 char *ccd, *cp, *cp2, **disks; 200 int noflags = 0, ileave, flags = 0, j; 201 unsigned int i; 202 203 bzero(&ccio, sizeof(ccio)); 204 205 /* 206 * If unconfiguring, all arguments are treated as ccds. 207 */ 208 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) { 209 for (i = 0; argc != 0; ) { 210 cp = *argv++; --argc; 211 if ((ccd = resolve_ccdname(cp)) == NULL) { 212 warnx("invalid ccd name: %s", cp); 213 i = 1; 214 continue; 215 } 216 if (do_io(ccd, CCDIOCCLR, &ccio)) 217 i = 1; 218 else 219 if (verbose) 220 printf("%s unconfigured\n", cp); 221 } 222 return (i); 223 } 224 225 /* Make sure there are enough arguments. */ 226 if (argc < 4) { 227 if (argc == 3) { 228 /* Assume that no flags are specified. */ 229 noflags = 1; 230 } else { 231 if (action == CCD_CONFIGALL) { 232 warnx("%s: bad line: %d", ccdconf, lineno); 233 return (1); 234 } else 235 usage(); 236 } 237 } 238 239 /* First argument is the ccd to configure. */ 240 cp = *argv++; --argc; 241 if ((ccd = resolve_ccdname(cp)) == NULL) { 242 warnx("invalid ccd name: %s", cp); 243 return (1); 244 } 245 246 /* Next argument is the interleave factor. */ 247 cp = *argv++; --argc; 248 errno = 0; /* to check for ERANGE */ 249 ileave = (int)strtol(cp, &cp2, 10); 250 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) { 251 warnx("invalid interleave factor: %s", cp); 252 return (1); 253 } 254 255 if (noflags == 0) { 256 /* Next argument is the ccd configuration flags. */ 257 cp = *argv++; --argc; 258 if ((flags = flags_to_val(cp)) < 0) { 259 warnx("invalid flags argument: %s", cp); 260 return (1); 261 } 262 } 263 264 /* Next is the list of disks to make the ccd from. */ 265 disks = malloc(argc * sizeof(char *)); 266 if (disks == NULL) { 267 warnx("no memory to configure ccd"); 268 return (1); 269 } 270 for (i = 0; argc != 0; ) { 271 cp = *argv++; --argc; 272 if ((j = checkdev(cp)) == 0) 273 disks[i++] = cp; 274 else { 275 warnx("%s: %s", cp, strerror(j)); 276 return (1); 277 } 278 } 279 280 /* Fill in the ccio. */ 281 ccio.ccio_disks = disks; 282 ccio.ccio_ndisks = i; 283 ccio.ccio_ileave = ileave; 284 ccio.ccio_flags = flags; 285 286 if (do_io(ccd, CCDIOCSET, &ccio)) { 287 free(disks); 288 return (1); 289 } 290 291 if (verbose) { 292 printf("ccd%d: %d components ", ccio.ccio_unit, 293 ccio.ccio_ndisks); 294 for (i = 0; i < ccio.ccio_ndisks; ++i) { 295 if ((cp2 = strrchr(disks[i], '/')) != NULL) 296 ++cp2; 297 else 298 cp2 = disks[i]; 299 printf("%c%s%c", 300 i == 0 ? '(' : ' ', cp2, 301 i == ccio.ccio_ndisks - 1 ? ')' : ','); 302 } 303 printf(", %lu blocks ", (u_long)ccio.ccio_size); 304 if (ccio.ccio_ileave != 0) 305 printf("interleaved at %d blocks\n", ccio.ccio_ileave); 306 else 307 printf("concatenated\n"); 308 } 309 310 free(disks); 311 return (0); 312 } 313 314 static int 315 do_all(int action) 316 { 317 FILE *f; 318 char line[_POSIX2_LINE_MAX]; 319 char *cp, **argv; 320 int argc, rval; 321 gid_t egid; 322 323 rval = 0; 324 egid = getegid(); 325 setegid(getgid()); 326 if ((f = fopen(ccdconf, "r")) == NULL) { 327 setegid(egid); 328 warn("fopen: %s", ccdconf); 329 return (1); 330 } 331 setegid(egid); 332 333 while (fgets(line, sizeof(line), f) != NULL) { 334 argc = 0; 335 argv = NULL; 336 ++lineno; 337 if ((cp = strrchr(line, '\n')) != NULL) 338 *cp = '\0'; 339 340 /* Break up the line and pass it's contents to do_single(). */ 341 if (line[0] == '\0') 342 goto end_of_line; 343 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) { 344 if (*cp == '#') 345 break; 346 if ((argv = realloc(argv, 347 sizeof(char *) * ++argc)) == NULL) { 348 warnx("no memory to configure ccds"); 349 return (1); 350 } 351 argv[argc - 1] = cp; 352 /* 353 * If our action is to unconfigure all, then pass 354 * just the first token to do_single() and ignore 355 * the rest. Since this will be encountered on 356 * our first pass through the line, the Right 357 * Thing will happen. 358 */ 359 if (action == CCD_UNCONFIGALL) { 360 if (do_single(argc, argv, action)) 361 rval = 1; 362 goto end_of_line; 363 } 364 } 365 if (argc != 0) 366 if (do_single(argc, argv, action)) 367 rval = 1; 368 369 end_of_line: 370 if (argv != NULL) 371 free(argv); 372 } 373 374 fclose(f); 375 return (rval); 376 } 377 378 static int 379 checkdev(char *path) 380 { 381 struct stat st; 382 383 if (stat(path, &st) != 0) 384 return (errno); 385 386 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) 387 return (EINVAL); 388 389 return (0); 390 } 391 392 static int 393 pathtounit(char *path, int *unitp) 394 { 395 struct stat st; 396 int maxpartitions; 397 398 if (stat(path, &st) != 0) 399 return (errno); 400 401 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) 402 return (EINVAL); 403 404 if ((maxpartitions = getmaxpartitions()) < 0) 405 return (errno); 406 407 *unitp = minor(st.st_rdev) / maxpartitions; 408 409 return (0); 410 } 411 412 static char * 413 resolve_ccdname(char *name) 414 { 415 char c, *path; 416 size_t len; 417 int rawpart; 418 419 if (name[0] == '/' || name[0] == '.') { 420 /* Assume they gave the correct pathname. */ 421 return (strdup(name)); 422 } 423 424 len = strlen(name); 425 if (len > 0) 426 c = name[len - 1]; 427 else 428 c = '\0'; 429 430 if (isdigit(c)) { 431 if ((rawpart = getrawpartition()) < 0) 432 return (NULL); 433 asprintf(&path, "%s%s%c", _PATH_DEV, name, 'a' + rawpart); 434 } else { 435 asprintf(&path, "%s%s", _PATH_DEV, name); 436 } 437 438 return (path); 439 } 440 441 static int 442 do_io(char *path, u_long cmd, struct ccd_ioctl *cciop) 443 { 444 int fd; 445 const char *cp; 446 447 if ((fd = open(path, O_RDWR, 0640)) < 0) { 448 warn("open: %s", path); 449 return (1); 450 } 451 452 if (ioctl(fd, cmd, cciop) < 0) { 453 switch (cmd) { 454 case CCDIOCSET: 455 cp = "CCDIOCSET"; 456 break; 457 458 case CCDIOCCLR: 459 cp = "CCDIOCCLR"; 460 break; 461 462 default: 463 cp = "unknown"; 464 } 465 warn("ioctl (%s): %s", cp, path); 466 return (1); 467 } 468 469 return (0); 470 } 471 472 #define KVM_ABORT(kd, str) { \ 473 kvm_close((kd)); \ 474 warnx("%s", (str)); \ 475 warnx("%s", kvm_geterr((kd))); \ 476 return (1); \ 477 } 478 479 static int 480 dump_ccd(int argc, char **argv) 481 { 482 char errbuf[_POSIX2_LINE_MAX], *ccd, *cp; 483 struct ccd_softc *cs, *kcs; 484 ssize_t readsize; 485 int i, error, numccd, numconfiged = 0; 486 kvm_t *kd; 487 488 bzero(errbuf, sizeof(errbuf)); 489 490 if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY, 491 errbuf)) == NULL) { 492 warnx("can't open kvm: %s", errbuf); 493 return (1); 494 } 495 496 if (kvm_nlist(kd, nl)) 497 KVM_ABORT(kd, "ccd-related symbols not available"); 498 499 /* Check to see how many ccds are currently configured. */ 500 if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd, 501 sizeof(numccd)) != sizeof(numccd)) 502 KVM_ABORT(kd, "can't determine number of configured ccds"); 503 504 if (numccd == 0) { 505 printf("ccd driver in kernel, but is uninitialized\n"); 506 goto done; 507 } 508 509 /* Allocate space for the configuration data. */ 510 readsize = numccd * sizeof(struct ccd_softc); 511 if ((cs = malloc(readsize)) == NULL) { 512 warnx("no memory for configuration data"); 513 goto bad; 514 } 515 bzero(cs, readsize); 516 517 /* 518 * Read the ccd configuration data from the kernel and dump 519 * it to stdout. 520 */ 521 if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs, 522 sizeof(kcs)) != sizeof(kcs)) { 523 free(cs); 524 KVM_ABORT(kd, "can't find pointer to configuration data"); 525 } 526 if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) { 527 free(cs); 528 KVM_ABORT(kd, "can't read configuration data"); 529 } 530 531 if (argc == 0) { 532 for (i = 0; i < numccd; ++i) 533 if (cs[i].sc_flags & CCDF_INITED) { 534 ++numconfiged; 535 print_ccd_info(&cs[i], kd); 536 } 537 538 if (numconfiged == 0) 539 printf("no concatenated disks configured\n"); 540 } else { 541 while (argc) { 542 cp = *argv++; --argc; 543 if ((ccd = resolve_ccdname(cp)) == NULL) { 544 warnx("invalid ccd name: %s", cp); 545 continue; 546 } 547 if ((error = pathtounit(ccd, &i)) != 0) { 548 warnx("%s: %s", ccd, strerror(error)); 549 continue; 550 } 551 if (i >= numccd) { 552 warnx("ccd%d not configured", i); 553 continue; 554 } 555 if (cs[i].sc_flags & CCDF_INITED) 556 print_ccd_info(&cs[i], kd); 557 else 558 printf("ccd%d not configured\n", i); 559 } 560 } 561 562 free(cs); 563 564 done: 565 kvm_close(kd); 566 return (0); 567 568 bad: 569 kvm_close(kd); 570 return (1); 571 } 572 573 static void 574 print_ccd_info(struct ccd_softc *cs, kvm_t *kd) 575 { 576 static int header_printed = 0; 577 struct ccdcinfo *cip; 578 ssize_t readsize; 579 char path[MAXPATHLEN]; 580 unsigned int i; 581 582 if (header_printed == 0 && verbose) { 583 printf("# ccd\t\tileave\tflags\tcompnent devices\n"); 584 header_printed = 1; 585 } 586 587 readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo); 588 if ((cip = malloc(readsize)) == NULL) { 589 warn("ccd%d: can't allocate memory for component info", 590 cs->sc_unit); 591 return; 592 } 593 bzero(cip, readsize); 594 595 /* Dump out softc information. */ 596 printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave, 597 cs->sc_cflags & CCDF_USERMASK); 598 fflush(stdout); 599 600 /* Read in the component info. */ 601 if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip, 602 readsize) != readsize) { 603 printf("\n"); 604 warnx("can't read component info"); 605 warnx("%s", kvm_geterr(kd)); 606 goto done; 607 } 608 609 /* Read component pathname and display component info. */ 610 for (i = 0; i < cs->sc_nccdisks; ++i) { 611 if ((unsigned)kvm_read(kd, (u_long)cip[i].ci_path, (char *)path, 612 cip[i].ci_pathlen) != cip[i].ci_pathlen) { 613 printf("\n"); 614 warnx("can't read component pathname"); 615 warnx("%s", kvm_geterr(kd)); 616 goto done; 617 } 618 printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path); 619 fflush(stdout); 620 } 621 622 done: 623 free(cip); 624 } 625 626 static int 627 getmaxpartitions(void) 628 { 629 return (MAXPARTITIONS); 630 } 631 632 static int 633 getrawpartition(void) 634 { 635 return (RAW_PART); 636 } 637 638 static int 639 flags_to_val(char *flags) 640 { 641 char *cp, *tok; 642 int i, tmp, val = ~CCDF_USERMASK; 643 size_t flagslen; 644 645 /* 646 * The most common case is that of NIL flags, so check for 647 * those first. 648 */ 649 if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 || 650 strcmp("0", flags) == 0) 651 return (0); 652 653 flagslen = strlen(flags); 654 655 /* Check for values represented by strings. */ 656 if ((cp = strdup(flags)) == NULL) 657 err(1, "no memory to parse flags"); 658 tmp = 0; 659 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) { 660 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i) 661 if (strcmp(tok, flagvaltab[i].fv_flag) == 0) 662 break; 663 if (flagvaltab[i].fv_flag == NULL) { 664 free(cp); 665 goto bad_string; 666 } 667 tmp |= flagvaltab[i].fv_val; 668 } 669 670 /* If we get here, the string was ok. */ 671 free(cp); 672 val = tmp; 673 goto out; 674 675 bad_string: 676 677 /* Check for values represented in hex. */ 678 if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') { 679 errno = 0; /* to check for ERANGE */ 680 val = (int)strtol(&flags[2], &cp, 16); 681 if ((errno == ERANGE) || (*cp != '\0')) 682 return (-1); 683 goto out; 684 } 685 686 /* Check for values represented in decimal. */ 687 errno = 0; /* to check for ERANGE */ 688 val = (int)strtol(flags, &cp, 10); 689 if ((errno == ERANGE) || (*cp != '\0')) 690 return (-1); 691 692 out: 693 return (((val & ~CCDF_USERMASK) == 0) ? val : -1); 694 } 695 696 static void 697 usage(void) 698 { 699 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n", 700 "usage: ccdconfig [-cv] ccd ileave [flags] dev [...]", 701 " ccdconfig -C [-v] [-f config_file]", 702 " ccdconfig -u [-v] ccd [...]", 703 " ccdconfig -U [-v] [-f config_file]", 704 " ccdconfig -g [-M core] [-N system] [ccd [...]]"); 705 exit(1); 706 } 707 708 /* Local Variables: */ 709 /* c-argdecl-indent: 8 */ 710 /* c-indent-level: 8 */ 711 /* End: */ 712