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