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