ccdconfig.c (108592) | ccdconfig.c (109417) |
---|---|
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 --- 20 unchanged lines hidden (view full) --- 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 36static const char rcsid[] = | 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 --- 20 unchanged lines hidden (view full) --- 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 36static const char rcsid[] = |
37 "$FreeBSD: head/sbin/ccdconfig/ccdconfig.c 108592 2003-01-03 08:56:54Z phk $"; | 37 "$FreeBSD: head/sbin/ccdconfig/ccdconfig.c 109417 2003-01-17 13:23:41Z phk $"; |
38#endif /* not lint */ 39 40#include <sys/param.h> 41#include <sys/linker.h> 42#include <sys/disklabel.h> 43#include <sys/stat.h> 44#include <sys/module.h> 45#include <ctype.h> --- 9 unchanged lines hidden (view full) --- 55 56#include <sys/devicestat.h> 57#include <sys/ccdvar.h> 58 59#include "pathnames.h" 60 61static int lineno = 0; 62static int verbose = 0; | 38#endif /* not lint */ 39 40#include <sys/param.h> 41#include <sys/linker.h> 42#include <sys/disklabel.h> 43#include <sys/stat.h> 44#include <sys/module.h> 45#include <ctype.h> --- 9 unchanged lines hidden (view full) --- 55 56#include <sys/devicestat.h> 57#include <sys/ccdvar.h> 58 59#include "pathnames.h" 60 61static int lineno = 0; 62static int verbose = 0; |
63static char *ccdconf = _PATH_CCDCONF; | 63static const char *ccdconf = _PATH_CCDCONF; |
64 65struct flagval { | 64 65struct flagval { |
66 char *fv_flag; | 66 const char *fv_flag; |
67 int fv_val; 68} flagvaltab[] = { 69 { "CCDF_UNIFORM", CCDF_UNIFORM }, 70 { "CCDF_MIRROR", CCDF_MIRROR }, 71 { NULL, 0 }, 72}; 73 74#define CCD_CONFIG 0 /* configure a device */ 75#define CCD_CONFIGALL 1 /* configure all devices */ 76#define CCD_UNCONFIG 2 /* unconfigure a device */ 77#define CCD_UNCONFIGALL 3 /* unconfigure all devices */ 78#define CCD_DUMP 4 /* dump a ccd's configuration */ 79 80static int checkdev(char *); | 67 int fv_val; 68} flagvaltab[] = { 69 { "CCDF_UNIFORM", CCDF_UNIFORM }, 70 { "CCDF_MIRROR", CCDF_MIRROR }, 71 { NULL, 0 }, 72}; 73 74#define CCD_CONFIG 0 /* configure a device */ 75#define CCD_CONFIGALL 1 /* configure all devices */ 76#define CCD_UNCONFIG 2 /* unconfigure a device */ 77#define CCD_UNCONFIGALL 3 /* unconfigure all devices */ 78#define CCD_DUMP 4 /* dump a ccd's configuration */ 79 80static int checkdev(char *); |
81static int do_io(char *, u_long, struct ccd_ioctl *); | 81static int do_io(int, u_long, struct ccd_ioctl *); |
82static int do_single(int, char **, int); 83static int do_all(int); 84static int dump_ccd(int, char **); | 82static int do_single(int, char **, int); 83static int do_all(int); 84static int dump_ccd(int, char **); |
85static int getmaxpartitions(void); 86static int getrawpartition(void); | |
87static int flags_to_val(char *); 88static void print_ccd_info(struct ccd_s *); | 85static int flags_to_val(char *); 86static void print_ccd_info(struct ccd_s *); |
89static char *resolve_ccdname(char *); | 87static int resolve_ccdname(char *); |
90static void usage(void); 91 92int 93main(int argc, char *argv[]) 94{ 95 int ch, options = 0, action = CCD_CONFIG; 96 97 while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) { --- 64 unchanged lines hidden (view full) --- 162 /* NOTREACHED */ 163 return (0); 164} 165 166static int 167do_single(int argc, char **argv, int action) 168{ 169 struct ccd_ioctl ccio; | 88static void usage(void); 89 90int 91main(int argc, char *argv[]) 92{ 93 int ch, options = 0, action = CCD_CONFIG; 94 95 while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) { --- 64 unchanged lines hidden (view full) --- 160 /* NOTREACHED */ 161 return (0); 162} 163 164static int 165do_single(int argc, char **argv, int action) 166{ 167 struct ccd_ioctl ccio; |
170 char *ccd, *cp, *cp2, **disks; 171 int noflags = 0, i, ileave, flags = 0, j; | 168 char *cp, *cp2, **disks; 169 int ccd, noflags = 0, i, ileave, flags = 0, j; 170 u_int u; |
172 173 bzero(&ccio, sizeof(ccio)); 174 175 /* 176 * If unconfiguring, all arguments are treated as ccds. 177 */ 178 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) { 179 for (i = 0; argc != 0; ) { --- 76 unchanged lines hidden (view full) --- 256 if (do_io(ccd, CCDIOCSET, &ccio)) { 257 free(disks); 258 return (1); 259 } 260 261 if (verbose) { 262 printf("ccd%d: %d components ", ccio.ccio_unit, 263 ccio.ccio_ndisks); | 171 172 bzero(&ccio, sizeof(ccio)); 173 174 /* 175 * If unconfiguring, all arguments are treated as ccds. 176 */ 177 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) { 178 for (i = 0; argc != 0; ) { --- 76 unchanged lines hidden (view full) --- 255 if (do_io(ccd, CCDIOCSET, &ccio)) { 256 free(disks); 257 return (1); 258 } 259 260 if (verbose) { 261 printf("ccd%d: %d components ", ccio.ccio_unit, 262 ccio.ccio_ndisks); |
264 for (i = 0; i < ccio.ccio_ndisks; ++i) { 265 if ((cp2 = strrchr(disks[i], '/')) != NULL) | 263 for (u = 0; u < ccio.ccio_ndisks; ++u) { 264 if ((cp2 = strrchr(disks[u], '/')) != NULL) |
266 ++cp2; 267 else | 265 ++cp2; 266 else |
268 cp2 = disks[i]; | 267 cp2 = disks[u]; |
269 printf("%c%s%c", | 268 printf("%c%s%c", |
270 i == 0 ? '(' : ' ', cp2, 271 i == ccio.ccio_ndisks - 1 ? ')' : ','); | 269 u == 0 ? '(' : ' ', cp2, 270 u == ccio.ccio_ndisks - 1 ? ')' : ','); |
272 } 273 printf(", %lu blocks ", (u_long)ccio.ccio_size); 274 if (ccio.ccio_ileave != 0) 275 printf("interleaved at %d blocks\n", ccio.ccio_ileave); 276 else 277 printf("concatenated\n"); 278 } 279 --- 75 unchanged lines hidden (view full) --- 355 356 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) 357 return (EINVAL); 358 359 return (0); 360} 361 362static int | 271 } 272 printf(", %lu blocks ", (u_long)ccio.ccio_size); 273 if (ccio.ccio_ileave != 0) 274 printf("interleaved at %d blocks\n", ccio.ccio_ileave); 275 else 276 printf("concatenated\n"); 277 } 278 --- 75 unchanged lines hidden (view full) --- 354 355 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) 356 return (EINVAL); 357 358 return (0); 359} 360 361static int |
363pathtounit(char *path, int *unitp) 364{ 365 struct stat st; 366 int maxpartitions; 367 368 if (stat(path, &st) != 0) 369 return (errno); 370 371 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) 372 return (EINVAL); 373 374 if ((maxpartitions = getmaxpartitions()) < 0) 375 return (errno); 376 377 *unitp = minor(st.st_rdev) / maxpartitions; 378 379 return (0); 380} 381 382static char * | |
383resolve_ccdname(char *name) 384{ | 362resolve_ccdname(char *name) 363{ |
385 char c, *path; 386 size_t len, newlen; 387 int rawpart; | |
388 | 364 |
389 if (name[0] == '/' || name[0] == '.') { 390 /* Assume they gave the correct pathname. */ 391 return (strdup(name)); 392 } 393 394 len = strlen(name); 395 c = name[len - 1]; 396 397 newlen = len + 8; 398 if ((path = malloc(newlen)) == NULL) 399 return (NULL); 400 bzero(path, newlen); 401 402 if (isdigit(c)) { 403 if ((rawpart = getrawpartition()) < 0) { 404 free(path); 405 return (NULL); 406 } 407 (void)sprintf(path, "%s%s%c", _PATH_DEV, name, 'a' + rawpart); 408 } else 409 (void)sprintf(path, "%s%s", _PATH_DEV, name); 410 411 return (path); | 365 if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV))) 366 name += strlen(_PATH_DEV); 367 if (strncmp(name, "ccd", 3)) 368 return -1; 369 name += 3; 370 if (!isdigit(*name)) 371 return -1; 372 return (strtoul(name, NULL, 10)); |
412} 413 414static int | 373} 374 375static int |
415do_io(char *path, u_long cmd, struct ccd_ioctl *cciop) | 376do_io(int unit, u_long cmd, struct ccd_ioctl *cciop) |
416{ 417 int fd; 418 char *cp; | 377{ 378 int fd; 379 char *cp; |
380 char *path; |
|
419 | 381 |
382 asprintf(&path, "%sccd%dc", _PATH_DEV, unit); 383 |
|
420 if ((fd = open(path, O_RDWR, 0640)) < 0) { 421 warn("open: %s", path); 422 return (1); 423 } 424 425 if (ioctl(fd, cmd, cciop) < 0) { 426 switch (cmd) { 427 case CCDIOCSET: --- 20 unchanged lines hidden (view full) --- 448 } 449 450 return (0); 451} 452 453static int 454dump_ccd(int argc, char **argv) 455{ | 384 if ((fd = open(path, O_RDWR, 0640)) < 0) { 385 warn("open: %s", path); 386 return (1); 387 } 388 389 if (ioctl(fd, cmd, cciop) < 0) { 390 switch (cmd) { 391 case CCDIOCSET: --- 20 unchanged lines hidden (view full) --- 412 } 413 414 return (0); 415} 416 417static int 418dump_ccd(int argc, char **argv) 419{ |
456 char *ccd, *cp; | 420 char *cp; |
457 int i, error, numccd, numconfiged = 0; 458 struct ccdconf conf; | 421 int i, error, numccd, numconfiged = 0; 422 struct ccdconf conf; |
423 int ccd; |
|
459 460 /* 461 * Read the ccd configuration data from the kernel and dump 462 * it to stdout. 463 */ | 424 425 /* 426 * Read the ccd configuration data from the kernel and dump 427 * it to stdout. 428 */ |
464 if ((ccd = resolve_ccdname("ccd0")) == NULL) { /* XXX */ | 429 if ((ccd = resolve_ccdname("ccd0")) < 0) { /* XXX */ |
465 warnx("invalid ccd name: %s", cp); 466 return (1); 467 } 468 conf.size = 0; 469 if (do_io(ccd, CCDCONFINFO, (struct ccd_ioctl *) &conf)) 470 return (1); 471 if (conf.size == 0) { 472 printf("no concatenated disks configured\n"); --- 11 unchanged lines hidden (view full) --- 484 numconfiged = conf.size / sizeof(struct ccd_s); 485 486 if (argc == 0) { 487 for (i = 0; i < numconfiged; i++) 488 print_ccd_info(&(conf.buffer[i])); 489 } else { 490 while (argc) { 491 cp = *argv++; --argc; | 430 warnx("invalid ccd name: %s", cp); 431 return (1); 432 } 433 conf.size = 0; 434 if (do_io(ccd, CCDCONFINFO, (struct ccd_ioctl *) &conf)) 435 return (1); 436 if (conf.size == 0) { 437 printf("no concatenated disks configured\n"); --- 11 unchanged lines hidden (view full) --- 449 numconfiged = conf.size / sizeof(struct ccd_s); 450 451 if (argc == 0) { 452 for (i = 0; i < numconfiged; i++) 453 print_ccd_info(&(conf.buffer[i])); 454 } else { 455 while (argc) { 456 cp = *argv++; --argc; |
492 if ((ccd = resolve_ccdname(cp)) == NULL) { | 457 if ((ccd = resolve_ccdname(cp)) < 0) { |
493 warnx("invalid ccd name: %s", cp); 494 continue; 495 } | 458 warnx("invalid ccd name: %s", cp); 459 continue; 460 } |
496 if ((error = pathtounit(ccd, &numccd)) != 0) { 497 warnx("%s: %s", ccd, strerror(error)); 498 continue; 499 } | |
500 error = 1; 501 for (i = 0; i < numconfiged; i++) { | 461 error = 1; 462 for (i = 0; i < numconfiged; i++) { |
502 if (conf.buffer[i].sc_unit == numccd) { | 463 if (conf.buffer[i].sc_unit == ccd) { |
503 print_ccd_info(&(conf.buffer[i])); 504 error = 0; 505 break; 506 } 507 } 508 if (error) { 509 warnx("ccd%d not configured", numccd); 510 continue; 511 } 512 } 513 } 514 515 return (0); 516} 517 518static void 519print_ccd_info(struct ccd_s *cs) 520{ | 464 print_ccd_info(&(conf.buffer[i])); 465 error = 0; 466 break; 467 } 468 } 469 if (error) { 470 warnx("ccd%d not configured", numccd); 471 continue; 472 } 473 } 474 } 475 476 return (0); 477} 478 479static void 480print_ccd_info(struct ccd_s *cs) 481{ |
521 char *cp, *ccd; | 482 char *cp; |
522 static int header_printed = 0; 523 struct ccdcpps cpps; | 483 static int header_printed = 0; 484 struct ccdcpps cpps; |
485 int ccd; |
|
524 525 /* Print out header if necessary*/ 526 if (header_printed == 0 && verbose) { 527 printf("# ccd\t\tileave\tflags\tcompnent devices\n"); 528 header_printed = 1; 529 } 530 531 /* Dump out softc information. */ --- 6 unchanged lines hidden (view full) --- 538 cs->device_stats.unit_number); 539 if (cp == NULL) { 540 printf("\n"); 541 warn("ccd%d: can't allocate memory", 542 cs->sc_unit); 543 return; 544 } 545 | 486 487 /* Print out header if necessary*/ 488 if (header_printed == 0 && verbose) { 489 printf("# ccd\t\tileave\tflags\tcompnent devices\n"); 490 header_printed = 1; 491 } 492 493 /* Dump out softc information. */ --- 6 unchanged lines hidden (view full) --- 500 cs->device_stats.unit_number); 501 if (cp == NULL) { 502 printf("\n"); 503 warn("ccd%d: can't allocate memory", 504 cs->sc_unit); 505 return; 506 } 507 |
546 if ((ccd = resolve_ccdname(cp)) == NULL) { | 508 if ((ccd = resolve_ccdname(cp)) < 0) { |
547 printf("\n"); 548 warnx("can't read component info: invalid ccd name: %s", cp); 549 return; 550 } 551 cpps.size = 0; 552 if (do_io(ccd, CCDCPPINFO, (struct ccd_ioctl *) &cpps)) { 553 printf("\n"); 554 warnx("can't read component info"); --- 17 unchanged lines hidden (view full) --- 572 printf((cp + strlen(cp) + 1) < (cpps.buffer + cpps.size) ? 573 "%s " : "%s\n", cp); 574 fflush(stdout); 575 } 576 return; 577} 578 579static int | 509 printf("\n"); 510 warnx("can't read component info: invalid ccd name: %s", cp); 511 return; 512 } 513 cpps.size = 0; 514 if (do_io(ccd, CCDCPPINFO, (struct ccd_ioctl *) &cpps)) { 515 printf("\n"); 516 warnx("can't read component info"); --- 17 unchanged lines hidden (view full) --- 534 printf((cp + strlen(cp) + 1) < (cpps.buffer + cpps.size) ? 535 "%s " : "%s\n", cp); 536 fflush(stdout); 537 } 538 return; 539} 540 541static int |
580getmaxpartitions(void) 581{ 582 return (MAXPARTITIONS); 583} 584 585static int 586getrawpartition(void) 587{ 588 return (RAW_PART); 589} 590 591static int | |
592flags_to_val(char *flags) 593{ 594 char *cp, *tok; 595 int i, tmp, val = ~CCDF_USERMASK; 596 size_t flagslen; 597 598 /* 599 * The most common case is that of NIL flags, so check for --- 65 unchanged lines hidden --- | 542flags_to_val(char *flags) 543{ 544 char *cp, *tok; 545 int i, tmp, val = ~CCDF_USERMASK; 546 size_t flagslen; 547 548 /* 549 * The most common case is that of NIL flags, so check for --- 65 unchanged lines hidden --- |