1 /* $NetBSD: raidctl.c,v 1.91 2026/10/28 21:33:13 tls Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Greg Oster 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This program is a re-write of the original rf_ctrl program 34 * distributed by CMU with RAIDframe 1.1. 35 * 36 * This program is the user-land interface to the RAIDframe kernel 37 * driver in NetBSD. 38 */ 39 #include <sys/cdefs.h> 40 41 #ifndef lint 42 __RCSID("$NetBSD: raidctl.c,v 1.91 2026/10/28 21:33:13 tls Exp $"); 43 #endif 44 45 46 #include <sys/param.h> 47 #include <sys/ioctl.h> 48 #include <sys/stat.h> 49 #include <sys/disklabel.h> 50 51 #include <ctype.h> 52 #include <err.h> 53 #include <errno.h> 54 #include <fcntl.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <inttypes.h> 59 #include <unistd.h> 60 #include <util.h> 61 62 #include <dev/raidframe/raidframevar.h> 63 #include <dev/raidframe/raidframeio.h> 64 #include "rf_configure.h" 65 #include "prog_ops.h" 66 67 #ifndef RAIDFRAME_REMOVE_COMPONENT 68 #define RAIDFRAME_REMOVE_COMPONENT RAIDFRAME_REMOVE_HOT_SPARE 69 #endif 70 71 #define CONFIGURE_TEST 1 /* must be different from any raidframe ioctl */ 72 73 void do_ioctl(int, u_long, void *, const char *); 74 static void rf_configure(int, char*, int); 75 static const char *device_status(RF_DiskStatus_t); 76 static void rf_get_device_status(int); 77 static void rf_output_configuration(int, const char *); 78 static void get_component_number(int, char *, int *, int *); 79 static void rf_fail_disk(int, char *, int); 80 __dead static void usage(void); 81 static void get_component_label(int, char *); 82 static void set_component_label(int, char *); 83 static void init_component_labels(int, int); 84 static void set_autoconfig(int, int, char *); 85 static void add_hot_spare(int, char *); 86 static void remove_component(int, char *); 87 static void rebuild_in_place(int, char *); 88 static void check_status(int,int); 89 static void check_parity(int,int, char *); 90 static void do_meter(int, u_long); 91 static void get_bar(char *, double, int); 92 static void get_time_string(char *, size_t, int); 93 static void rf_output_pmstat(int, int); 94 static void rf_pm_configure(int, int, char *, int[]); 95 static void rf_simple_create(int, int, char *[]); 96 static unsigned int xstrtouint(const char *); 97 98 int verbose; 99 100 static const char *rootpart[] = { "No", "Force", "Soft", "*invalid*" }; 101 102 static void 103 get_comp(char *buf, char *arg, size_t bufsz) 104 { 105 if (getfsspecname(buf, bufsz, arg) == NULL) 106 errx(1,"%s",buf); 107 } 108 109 int 110 main(int argc,char *argv[]) 111 { 112 int ch, i; 113 int num_options; 114 unsigned long action; 115 char config_filename[PATH_MAX]; 116 char dev_name[PATH_MAX]; 117 char name[PATH_MAX]; 118 char component[PATH_MAX]; 119 char autoconf[10]; 120 char *parityconf = NULL; 121 int parityparams[3]; 122 int do_output; 123 int do_recon; 124 int do_rewrite; 125 int raidID; 126 int serial_number; 127 struct stat st; 128 int fd; 129 int force; 130 int openmode; 131 int last_unit; 132 struct timeval tv; 133 134 num_options = 0; 135 action = 0; 136 do_output = 0; 137 do_recon = 0; 138 do_rewrite = 0; 139 serial_number = 0; 140 force = 0; 141 last_unit = 0; 142 openmode = O_RDWR; /* default to read/write */ 143 144 if (argc > 5) { 145 /* we have at least 5 args, so it might be a simplified config */ 146 147 strlcpy(name, argv[1], sizeof(name)); 148 fd = opendisk(name, openmode, dev_name, sizeof(dev_name), 0); 149 if (fd != -1) { 150 /* we were able to open the device... */ 151 if (fstat(fd, &st) == -1) 152 err(1, "stat failure on: %s", dev_name); 153 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) 154 err(1, "invalid device: %s", dev_name); 155 156 raidID = DISKUNIT(st.st_rdev); 157 if (strncmp(argv[2],"create",6)==0) { 158 rf_simple_create(fd,argc-3,&argv[3]); 159 160 /* set serial number, set autoconfig, init parity */ 161 162 if (gettimeofday(&tv,NULL) == -1) { 163 serial_number = 12345777; 164 } else { 165 serial_number = tv.tv_sec; 166 } 167 init_component_labels(fd, serial_number); 168 strlcpy(autoconf, "yes", sizeof(autoconf)); 169 set_autoconfig(fd, raidID, autoconf); 170 171 } else 172 usage(); 173 174 close(fd); 175 exit(0); 176 } 177 178 /* otherwise we go back to regular parsing */ 179 } 180 181 while ((ch = getopt(argc, argv, 182 "a:A:c:C:f:F:g:GiI:l:LmM:r:R:sSpPt:uU:v")) != -1) 183 switch (ch) { 184 case 'a': 185 action = RAIDFRAME_ADD_HOT_SPARE; 186 get_comp(component, optarg, sizeof(component)); 187 num_options++; 188 break; 189 case 'A': 190 action = RAIDFRAME_SET_AUTOCONFIG; 191 strlcpy(autoconf, optarg, sizeof(autoconf)); 192 num_options++; 193 break; 194 case 'c': 195 action = RAIDFRAME_CONFIGURE; 196 strlcpy(config_filename, optarg, 197 sizeof(config_filename)); 198 force = 0; 199 num_options++; 200 break; 201 case 'C': 202 strlcpy(config_filename, optarg, 203 sizeof(config_filename)); 204 action = RAIDFRAME_CONFIGURE; 205 force = 1; 206 num_options++; 207 break; 208 case 'f': 209 action = RAIDFRAME_FAIL_DISK; 210 get_comp(component, optarg, sizeof(component)); 211 do_recon = 0; 212 num_options++; 213 break; 214 case 'F': 215 action = RAIDFRAME_FAIL_DISK; 216 get_comp(component, optarg, sizeof(component)); 217 do_recon = 1; 218 num_options++; 219 break; 220 case 'g': 221 action = RAIDFRAME_GET_COMPONENT_LABEL; 222 get_comp(component, optarg, sizeof(component)); 223 openmode = O_RDONLY; 224 num_options++; 225 break; 226 case 'G': 227 action = RAIDFRAME_GET_INFO; 228 openmode = O_RDONLY; 229 do_output = 1; 230 num_options++; 231 break; 232 case 'i': 233 action = RAIDFRAME_REWRITEPARITY; 234 num_options++; 235 break; 236 case 'I': 237 action = RAIDFRAME_INIT_LABELS; 238 serial_number = xstrtouint(optarg); 239 num_options++; 240 break; 241 case 'l': 242 action = RAIDFRAME_SET_COMPONENT_LABEL; 243 get_comp(component, optarg, sizeof(component)); 244 num_options++; 245 break; 246 case 'L': 247 action = RAIDFRAME_RESCAN; 248 num_options++; 249 break; 250 case 'm': 251 action = RAIDFRAME_PARITYMAP_STATUS; 252 openmode = O_RDONLY; 253 num_options++; 254 break; 255 case 'M': 256 action = RAIDFRAME_PARITYMAP_SET_DISABLE; 257 parityconf = strdup(optarg); 258 num_options++; 259 /* XXXjld: should rf_pm_configure do the strtol()s? */ 260 i = 0; 261 while (i < 3 && optind < argc && 262 isdigit((unsigned char)argv[optind][0])) 263 parityparams[i++] = xstrtouint(argv[optind++]); 264 while (i < 3) 265 parityparams[i++] = 0; 266 break; 267 case 'p': 268 action = RAIDFRAME_CHECK_PARITY; 269 openmode = O_RDONLY; 270 num_options++; 271 break; 272 case 'P': 273 action = RAIDFRAME_CHECK_PARITY; 274 do_rewrite = 1; 275 num_options++; 276 break; 277 case 'r': 278 action = RAIDFRAME_REMOVE_COMPONENT; 279 get_comp(component, optarg, sizeof(component)); 280 num_options++; 281 break; 282 case 'R': 283 get_comp(component, optarg, sizeof(component)); 284 action = RAIDFRAME_REBUILD_IN_PLACE; 285 num_options++; 286 break; 287 case 's': 288 action = RAIDFRAME_GET_INFO; 289 openmode = O_RDONLY; 290 num_options++; 291 break; 292 case 'S': 293 action = RAIDFRAME_CHECK_RECON_STATUS_EXT; 294 openmode = O_RDONLY; 295 num_options++; 296 break; 297 case 't': 298 action = CONFIGURE_TEST; 299 strlcpy(config_filename, optarg, 300 sizeof(config_filename)); 301 num_options++; 302 break; 303 case 'u': 304 action = RAIDFRAME_SHUTDOWN; 305 num_options++; 306 break; 307 case 'U': 308 action = RAIDFRAME_SET_LAST_UNIT; 309 num_options++; 310 last_unit = atoi(optarg); 311 if (last_unit < 0) 312 errx(1, "Bad last unit %s", optarg); 313 break; 314 case 'v': 315 verbose = 1; 316 /* Don't bump num_options, as '-v' is not 317 an option like the others */ 318 /* num_options++; */ 319 break; 320 default: 321 usage(); 322 } 323 argc -= optind; 324 argv += optind; 325 326 if (num_options > 1) 327 usage(); 328 329 if (action == CONFIGURE_TEST) { 330 RF_Config_t cfg; 331 332 if (argc != 0) 333 usage(); 334 if (rf_MakeConfig(config_filename, &cfg) != 0) 335 exit(1); 336 exit(0);; 337 } 338 339 if (argc != 1) 340 usage(); 341 342 if (prog_init && prog_init() == -1) 343 err(1, "init failed"); 344 345 strlcpy(name, argv[0], sizeof(name)); 346 fd = opendisk1(name, openmode, dev_name, sizeof(dev_name), 0, 347 prog_open); 348 if (fd == -1) 349 err(1, "Unable to open device file: %s", name); 350 if (prog_fstat(fd, &st) == -1) 351 err(1, "stat failure on: %s", dev_name); 352 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) 353 err(1, "invalid device: %s", dev_name); 354 355 raidID = DISKUNIT(st.st_rdev); 356 357 switch (action) { 358 case RAIDFRAME_ADD_HOT_SPARE: 359 add_hot_spare(fd, component); 360 break; 361 case RAIDFRAME_REMOVE_COMPONENT: 362 remove_component(fd, component); 363 break; 364 case RAIDFRAME_CONFIGURE: 365 rf_configure(fd, config_filename, force); 366 break; 367 case RAIDFRAME_SET_AUTOCONFIG: 368 set_autoconfig(fd, raidID, autoconf); 369 break; 370 case RAIDFRAME_FAIL_DISK: 371 rf_fail_disk(fd, component, do_recon); 372 break; 373 case RAIDFRAME_SET_COMPONENT_LABEL: 374 set_component_label(fd, component); 375 break; 376 case RAIDFRAME_GET_COMPONENT_LABEL: 377 get_component_label(fd, component); 378 break; 379 case RAIDFRAME_INIT_LABELS: 380 init_component_labels(fd, serial_number); 381 break; 382 case RAIDFRAME_REWRITEPARITY: 383 printf("Initiating re-write of parity\n"); 384 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL, 385 "RAIDFRAME_REWRITEPARITY"); 386 if (verbose) { 387 sleep(3); /* XXX give it time to get started */ 388 printf("Parity Re-write status:\n"); 389 do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT); 390 } 391 break; 392 case RAIDFRAME_CHECK_RECON_STATUS_EXT: 393 check_status(fd,1); 394 break; 395 case RAIDFRAME_GET_INFO: 396 if (do_output) 397 rf_output_configuration(fd, dev_name); 398 else 399 rf_get_device_status(fd); 400 break; 401 case RAIDFRAME_PARITYMAP_STATUS: 402 rf_output_pmstat(fd, raidID); 403 break; 404 case RAIDFRAME_PARITYMAP_SET_DISABLE: 405 rf_pm_configure(fd, raidID, parityconf, parityparams); 406 break; 407 case RAIDFRAME_REBUILD_IN_PLACE: 408 rebuild_in_place(fd, component); 409 break; 410 case RAIDFRAME_CHECK_PARITY: 411 check_parity(fd, do_rewrite, dev_name); 412 break; 413 case RAIDFRAME_SHUTDOWN: 414 do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN"); 415 break; 416 case RAIDFRAME_SET_LAST_UNIT: 417 do_ioctl(fd, RAIDFRAME_SET_LAST_UNIT, &last_unit, 418 "RAIDFRAME_SET_LAST_UNIT"); 419 break; 420 case RAIDFRAME_RESCAN: 421 do_ioctl(fd, RAIDFRAME_RESCAN, NULL, "RAIDFRAME_RESCAN"); 422 break; 423 default: 424 break; 425 } 426 427 prog_close(fd); 428 exit(0); 429 } 430 431 void 432 do_ioctl(int fd, unsigned long command, void *arg, const char *ioctl_name) 433 { 434 if (prog_ioctl(fd, command, arg) == -1) 435 err(1, "ioctl (%s) failed", ioctl_name); 436 } 437 438 439 static void 440 rf_configure(int fd, char *config_file, int force) 441 { 442 void *generic; 443 RF_Config_t cfg; 444 445 if (rf_MakeConfig( config_file, &cfg ) != 0) 446 err(1, "Unable to create RAIDframe configuration structure"); 447 448 cfg.force = force; 449 450 /* 451 * Note the extra level of redirection needed here, since 452 * what we really want to pass in is a pointer to the pointer to 453 * the configuration structure. 454 */ 455 456 generic = &cfg; 457 do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE"); 458 } 459 460 static const char * 461 device_status(RF_DiskStatus_t status) 462 { 463 464 switch (status) { 465 case rf_ds_optimal: 466 return ("optimal"); 467 case rf_ds_failed: 468 return ("failed"); 469 case rf_ds_reconstructing: 470 return ("reconstructing"); 471 case rf_ds_dist_spared: 472 return ("dist_spared"); 473 case rf_ds_spared: 474 return ("spared"); 475 case rf_ds_spare: 476 return ("spare"); 477 case rf_ds_used_spare: 478 return ("used_spare"); 479 case rf_ds_rebuilding_spare: 480 return ("rebuilding_spare"); 481 default: 482 return ("UNKNOWN"); 483 } 484 /* NOTREACHED */ 485 } 486 487 static void 488 rf_get_device_status(int fd) 489 { 490 RF_DeviceConfig_t device_config; 491 void *cfg_ptr; 492 int is_clean; 493 int i, nspares; 494 495 cfg_ptr = &device_config; 496 497 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO"); 498 499 printf("Components:\n"); 500 for(i=0; i < device_config.ndevs; i++) { 501 printf("%20s: %s\n", device_config.devs[i].devname, 502 device_status(device_config.devs[i].status)); 503 } 504 505 nspares = MIN(device_config.nspares, 506 __arraycount(device_config.spares)); 507 508 if (nspares > 0) { 509 printf("Spares:\n"); 510 for(i=0; i < nspares; i++) { 511 printf("%20s: %s\n", 512 device_config.spares[i].devname, 513 device_status(device_config.spares[i].status)); 514 } 515 } else { 516 printf("No spares.\n"); 517 } 518 for(i=0; i < device_config.ndevs; i++) { 519 if (device_config.devs[i].status == rf_ds_optimal) { 520 get_component_label(fd, device_config.devs[i].devname); 521 } else { 522 printf("%s status is: %s. Skipping label.\n", 523 device_config.devs[i].devname, 524 device_status(device_config.devs[i].status)); 525 } 526 } 527 528 if (nspares > 0) { 529 for(i=0; i < nspares; i++) { 530 if ((device_config.spares[i].status == 531 rf_ds_optimal) || 532 (device_config.spares[i].status == 533 rf_ds_used_spare)) { 534 get_component_label(fd, 535 device_config.spares[i].devname); 536 } else { 537 printf("%s status is: %s. Skipping label.\n", 538 device_config.spares[i].devname, 539 device_status( 540 device_config.spares[i].status)); 541 } 542 } 543 } 544 545 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean, 546 "RAIDFRAME_CHECK_PARITY"); 547 if (is_clean) { 548 printf("Parity status: clean\n"); 549 } else { 550 printf("Parity status: DIRTY\n"); 551 } 552 check_status(fd,0); 553 } 554 555 static void 556 rf_output_pmstat(int fd, int raidID) 557 { 558 char srs[7]; 559 unsigned int i, j; 560 int dis, dr; 561 struct rf_pmstat st; 562 563 if (prog_ioctl(fd, RAIDFRAME_PARITYMAP_STATUS, &st) == -1) { 564 if (errno == EINVAL) { 565 printf("raid%d: has no parity; parity map disabled\n", 566 raidID); 567 return; 568 } 569 err(1, "ioctl (%s) failed", "RAIDFRAME_PARITYMAP_STATUS"); 570 } 571 572 if (st.enabled) { 573 if (0 > humanize_number(srs, 7, st.region_size * DEV_BSIZE, 574 "B", HN_AUTOSCALE, HN_NOSPACE)) 575 strlcpy(srs, "???", 7); 576 577 printf("raid%d: parity map enabled with %u regions of %s\n", 578 raidID, st.params.regions, srs); 579 printf("raid%d: regions marked clean after %d intervals of" 580 " %d.%03ds\n", raidID, st.params.cooldown, 581 st.params.tickms / 1000, st.params.tickms % 1000); 582 printf("raid%d: write/sync/clean counters " 583 "%"PRIu64"/%"PRIu64"/%"PRIu64"\n", raidID, 584 st.ctrs.nwrite, st.ctrs.ncachesync, st.ctrs.nclearing); 585 586 dr = 0; 587 for (i = 0; i < st.params.regions; i++) 588 if (isset(st.dirty, i)) 589 dr++; 590 printf("raid%d: %d dirty region%s\n", raidID, dr, 591 dr == 1 ? "" : "s"); 592 593 if (verbose > 0) { 594 for (i = 0; i < RF_PARITYMAP_NBYTE; i += 32) { 595 printf(" "); 596 for (j = i; j < RF_PARITYMAP_NBYTE 597 && j < i + 32; j++) 598 printf("%x%x", st.dirty[j] & 15, 599 (st.dirty[j] >> 4) & 15); 600 printf("\n"); 601 } 602 } 603 } else { 604 printf("raid%d: parity map disabled\n", raidID); 605 } 606 607 do_ioctl(fd, RAIDFRAME_PARITYMAP_GET_DISABLE, &dis, 608 "RAIDFRAME_PARITYMAP_GET_DISABLE"); 609 printf("raid%d: parity map will %s %sabled on next configure\n", 610 raidID, dis == st.enabled ? "be" : "remain", dis ? "dis" : "en"); 611 } 612 613 static void 614 rf_pm_configure(int fd, int raidID, char *parityconf, int parityparams[]) 615 { 616 int dis; 617 struct rf_pmparams params; 618 619 if (strcasecmp(parityconf, "yes") == 0) 620 dis = 0; 621 else if (strcasecmp(parityconf, "no") == 0) 622 dis = 1; 623 else if (strcasecmp(parityconf, "set") == 0) { 624 params.cooldown = parityparams[0]; 625 params.tickms = parityparams[1]; 626 params.regions = parityparams[2]; 627 628 do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_PARAMS, ¶ms, 629 "RAIDFRAME_PARITYMAP_SET_PARAMS"); 630 631 if (params.cooldown != 0 || params.tickms != 0) { 632 printf("raid%d: parity cleaned after", raidID); 633 if (params.cooldown != 0) 634 printf(" %d", params.cooldown); 635 printf(" intervals"); 636 if (params.tickms != 0) { 637 printf(" of %d.%03ds", params.tickms / 1000, 638 params.tickms % 1000); 639 } 640 printf("\n"); 641 } 642 if (params.regions != 0) 643 printf("raid%d: will use %d regions on next" 644 " configuration\n", raidID, params.regions); 645 646 return; 647 /* XXX the control flow here could be prettier. */ 648 } else 649 err(1, "`%s' is not a valid parity map command", parityconf); 650 651 do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_DISABLE, &dis, 652 "RAIDFRAME_PARITYMAP_SET_DISABLE"); 653 printf("raid%d: parity map will be %sabled on next configure\n", 654 raidID, dis ? "dis" : "en"); 655 } 656 657 /* convert "component0" into "absent" */ 658 static const char *rf_output_devname(const char *name) 659 { 660 661 if (strncmp(name, "component", 9) == 0) 662 return "absent"; 663 return name; 664 } 665 666 static void 667 rf_output_configuration(int fd, const char *name) 668 { 669 RF_DeviceConfig_t device_config; 670 void *cfg_ptr; 671 int i, nspares; 672 RF_ComponentLabel_t component_label; 673 void *label_ptr; 674 int component_num; 675 int num_cols; 676 677 cfg_ptr = &device_config; 678 679 printf("# raidctl config file for %s\n", name); 680 printf("\n"); 681 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO"); 682 683 nspares = MIN(device_config.nspares, 684 __arraycount(device_config.spares)); 685 686 int active_spares = 0; 687 for (i = 0; i < nspares; i++) { 688 if (device_config.spares[i].status == rf_ds_spare) 689 active_spares++; 690 } 691 692 printf("START array\n"); 693 printf("# numCol numSpare\n"); 694 printf("%d %d\n", device_config.cols, active_spares); 695 printf("\n"); 696 697 printf("START disks\n"); 698 for(i=0; i < device_config.ndevs; i++) 699 printf("%s\n", 700 rf_output_devname(device_config.devs[i].devname)); 701 printf("\n"); 702 703 if (active_spares > 0) { 704 printf("START spare\n"); 705 for(i=0; i < nspares; i++) { 706 if (device_config.spares[i].status == rf_ds_spare) 707 printf("%s\n", 708 device_config.spares[i].devname); 709 } 710 printf("\n"); 711 } 712 713 for(i=0; i < device_config.ndevs; i++) { 714 if (device_config.devs[i].status == rf_ds_optimal) 715 break; 716 } 717 if (i == device_config.ndevs) { 718 printf("# WARNING: no optimal components; using %s\n", 719 device_config.devs[0].devname); 720 i = 0; 721 } 722 get_component_number(fd, device_config.devs[i].devname, 723 &component_num, &num_cols); 724 memset(&component_label, 0, sizeof(RF_ComponentLabel_t)); 725 component_label.row = component_num / num_cols; 726 component_label.column = component_num % num_cols; 727 label_ptr = &component_label; 728 do_ioctl(fd, RAIDFRAME_GET_COMPONENT_LABEL, label_ptr, 729 "RAIDFRAME_GET_COMPONENT_LABEL"); 730 731 printf("START layout\n"); 732 printf( 733 "# sectPerSU SUsPerParityUnit SUsPerReconUnit RAID_level_%c\n", 734 (char) component_label.parityConfig); 735 printf("%d %d %d %c\n", 736 component_label.sectPerSU, component_label.SUsPerPU, 737 component_label.SUsPerRU, (char) component_label.parityConfig); 738 printf("\n"); 739 740 printf("START queue\n"); 741 printf("fifo %d\n", device_config.maxqdepth); 742 } 743 744 static void 745 get_component_number(int fd, char *component_name, int *component_number, 746 int *num_columns) 747 { 748 RF_DeviceConfig_t device_config; 749 void *cfg_ptr; 750 int i, nspares; 751 int found; 752 753 *component_number = -1; 754 755 /* Assuming a full path spec... */ 756 cfg_ptr = &device_config; 757 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, 758 "RAIDFRAME_GET_INFO"); 759 760 *num_columns = device_config.cols; 761 762 nspares = MIN(device_config.nspares, 763 __arraycount(device_config.spares)); 764 765 found = 0; 766 for(i=0; i < device_config.ndevs; i++) { 767 if (strncmp(component_name, device_config.devs[i].devname, 768 PATH_MAX)==0) { 769 found = 1; 770 *component_number = i; 771 } 772 } 773 if (!found) { /* maybe it's a spare? */ 774 for(i=0; i < nspares; i++) { 775 if (strncmp(component_name, 776 device_config.spares[i].devname, 777 PATH_MAX)==0) { 778 found = 1; 779 *component_number = i + device_config.ndevs; 780 /* the way spares are done should 781 really change... */ 782 *num_columns = device_config.cols + 783 device_config.nspares; 784 } 785 } 786 } 787 788 if (!found) 789 err(1,"%s is not a component of this device", component_name); 790 } 791 792 static void 793 rf_fail_disk(int fd, char *component_to_fail, int do_recon) 794 { 795 struct rf_recon_req recon_request; 796 int component_num; 797 int num_cols; 798 799 get_component_number(fd, component_to_fail, &component_num, &num_cols); 800 801 recon_request.col = component_num % num_cols; 802 if (do_recon) { 803 recon_request.flags = RF_FDFLAGS_RECON; 804 } else { 805 recon_request.flags = RF_FDFLAGS_NONE; 806 } 807 do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request, 808 "RAIDFRAME_FAIL_DISK"); 809 if (do_recon && verbose) { 810 printf("Reconstruction status:\n"); 811 sleep(3); /* XXX give reconstruction a chance to start */ 812 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT); 813 } 814 } 815 816 static void 817 get_component_label(int fd, char *component) 818 { 819 RF_ComponentLabel_t component_label; 820 void *label_ptr; 821 int component_num; 822 int num_cols; 823 824 get_component_number(fd, component, &component_num, &num_cols); 825 826 memset( &component_label, 0, sizeof(RF_ComponentLabel_t)); 827 component_label.row = component_num / num_cols; 828 component_label.column = component_num % num_cols; 829 830 label_ptr = &component_label; 831 do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, label_ptr, 832 "RAIDFRAME_GET_COMPONENT_LABEL"); 833 834 printf("Component label for %s:\n",component); 835 836 printf(" Row: %d, Column: %d, Num Rows: %d, Num Columns: %d\n", 837 component_label.row, component_label.column, 838 component_label.num_rows, component_label.num_columns); 839 printf(" Version: %d, Serial Number: %u, Mod Counter: %d\n", 840 component_label.version, component_label.serial_number, 841 component_label.mod_counter); 842 printf(" Clean: %s, Status: %d\n", 843 component_label.clean ? "Yes" : "No", 844 component_label.status ); 845 printf(" sectPerSU: %d, SUsPerPU: %d, SUsPerRU: %d\n", 846 component_label.sectPerSU, component_label.SUsPerPU, 847 component_label.SUsPerRU); 848 printf(" Queue size: %d, blocksize: %d, numBlocks: %"PRIu64"\n", 849 component_label.maxOutstanding, component_label.blockSize, 850 rf_component_label_numblocks(&component_label)); 851 printf(" RAID Level: %c\n", (char) component_label.parityConfig); 852 printf(" Autoconfig: %s\n", 853 component_label.autoconfigure ? "Yes" : "No" ); 854 printf(" Root partition: %s\n", 855 rootpart[component_label.root_partition & 3]); 856 printf(" Last configured as: raid%d\n", component_label.last_unit ); 857 } 858 859 static void 860 set_component_label(int fd, char *component) 861 { 862 RF_ComponentLabel_t component_label; 863 int component_num; 864 int num_cols; 865 866 get_component_number(fd, component, &component_num, &num_cols); 867 868 /* XXX This is currently here for testing, and future expandability */ 869 870 component_label.version = 1; 871 component_label.serial_number = 123456; 872 component_label.mod_counter = 0; 873 component_label.row = component_num / num_cols; 874 component_label.column = component_num % num_cols; 875 component_label.num_rows = 0; 876 component_label.num_columns = 5; 877 component_label.clean = 0; 878 component_label.status = 1; 879 880 do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label, 881 "RAIDFRAME_SET_COMPONENT_LABEL"); 882 } 883 884 885 static void 886 init_component_labels(int fd, int serial_number) 887 { 888 RF_ComponentLabel_t component_label; 889 890 component_label.version = 0; 891 component_label.serial_number = serial_number; 892 component_label.mod_counter = 0; 893 component_label.row = 0; 894 component_label.column = 0; 895 component_label.num_rows = 0; 896 component_label.num_columns = 0; 897 component_label.clean = 0; 898 component_label.status = 0; 899 900 do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label, 901 "RAIDFRAME_INIT_LABELS"); 902 } 903 904 static void 905 set_autoconfig(int fd, int raidID, char *autoconf) 906 { 907 int auto_config; 908 int root_config; 909 910 auto_config = 0; 911 root_config = 0; 912 913 if (strncasecmp(autoconf, "root", 4) == 0 || 914 strncasecmp(autoconf, "hard", 4) == 0 || 915 strncasecmp(autoconf, "force", 5) == 0) { 916 root_config = 1; 917 } else if (strncasecmp(autoconf, "soft", 4) == 0) { 918 root_config = 2; 919 } 920 921 if ((strncasecmp(autoconf,"yes", 3) == 0) || 922 root_config > 0) { 923 auto_config = 1; 924 } 925 926 do_ioctl(fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config, 927 "RAIDFRAME_SET_AUTOCONFIG"); 928 929 do_ioctl(fd, RAIDFRAME_SET_ROOT, &root_config, 930 "RAIDFRAME_SET_ROOT"); 931 932 if (verbose) { 933 printf("raid%d: Autoconfigure: %s\n", raidID, 934 auto_config ? "Yes" : "No"); 935 if (auto_config == 1) { 936 printf("raid%d: Root: %s\n", raidID, rootpart[root_config]); 937 } 938 } 939 } 940 941 static void 942 add_hot_spare(int fd, char *component) 943 { 944 RF_SingleComponent_t hot_spare; 945 946 hot_spare.row = 0; 947 hot_spare.column = 0; 948 strncpy(hot_spare.component_name, component, 949 sizeof(hot_spare.component_name)); 950 951 do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare, 952 "RAIDFRAME_ADD_HOT_SPARE"); 953 } 954 955 static void 956 remove_component(int fd, char *component) 957 { 958 RF_SingleComponent_t comp; 959 int component_num; 960 int num_cols; 961 962 get_component_number(fd, component, &component_num, &num_cols); 963 964 comp.row = component_num / num_cols; 965 comp.column = component_num % num_cols; 966 967 strncpy(comp.component_name, component, 968 sizeof(comp.component_name)); 969 970 do_ioctl( fd, RAIDFRAME_REMOVE_COMPONENT, &comp, 971 "RAIDFRAME_REMOVE_COMPONENT"); 972 } 973 974 static void 975 rebuild_in_place(int fd, char *component) 976 { 977 RF_SingleComponent_t comp; 978 int component_num; 979 int num_cols; 980 981 get_component_number(fd, component, &component_num, &num_cols); 982 983 comp.row = 0; 984 comp.column = component_num; 985 strncpy(comp.component_name, component, sizeof(comp.component_name)); 986 987 do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp, 988 "RAIDFRAME_REBUILD_IN_PLACE"); 989 990 if (verbose) { 991 printf("Reconstruction status:\n"); 992 sleep(3); /* XXX give reconstruction a chance to start */ 993 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT); 994 } 995 996 } 997 998 static void 999 check_parity(int fd, int do_rewrite, char *dev_name) 1000 { 1001 int is_clean; 1002 int percent_done; 1003 1004 is_clean = 0; 1005 percent_done = 0; 1006 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean, 1007 "RAIDFRAME_CHECK_PARITY"); 1008 if (is_clean) { 1009 printf("%s: Parity status: clean\n",dev_name); 1010 } else { 1011 printf("%s: Parity status: DIRTY\n",dev_name); 1012 if (do_rewrite) { 1013 printf("%s: Initiating re-write of parity\n", 1014 dev_name); 1015 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL, 1016 "RAIDFRAME_REWRITEPARITY"); 1017 sleep(3); /* XXX give it time to 1018 get started. */ 1019 if (verbose) { 1020 printf("Parity Re-write status:\n"); 1021 do_meter(fd, 1022 RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT); 1023 } else { 1024 do_ioctl(fd, 1025 RAIDFRAME_CHECK_PARITYREWRITE_STATUS, 1026 &percent_done, 1027 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS" 1028 ); 1029 while( percent_done < 100 ) { 1030 sleep(3); /* wait a bit... */ 1031 do_ioctl(fd, 1032 RAIDFRAME_CHECK_PARITYREWRITE_STATUS, 1033 &percent_done, 1034 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"); 1035 } 1036 1037 } 1038 printf("%s: Parity Re-write complete\n", dev_name); 1039 } else { 1040 /* parity is wrong, and is not being fixed. 1041 Exit w/ an error. */ 1042 exit(1); 1043 } 1044 } 1045 } 1046 1047 1048 static void 1049 check_status(int fd, int meter) 1050 { 1051 int recon_percent_done = 0; 1052 int parity_percent_done = 0; 1053 1054 do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done, 1055 "RAIDFRAME_CHECK_RECON_STATUS"); 1056 printf("Reconstruction is %d%% complete.\n", recon_percent_done); 1057 do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS, 1058 &parity_percent_done, 1059 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"); 1060 printf("Parity Re-write is %d%% complete.\n", parity_percent_done); 1061 1062 if (meter) { 1063 /* These 3 should be mutually exclusive at this point */ 1064 if (recon_percent_done < 100) { 1065 printf("Reconstruction status:\n"); 1066 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT); 1067 } else if (parity_percent_done < 100) { 1068 printf("Parity Re-write status:\n"); 1069 do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT); 1070 } 1071 } 1072 } 1073 1074 const char *tbits = "|/-\\"; 1075 1076 static void 1077 do_meter(int fd, u_long option) 1078 { 1079 int percent_done; 1080 RF_uint64 start_value; 1081 RF_ProgressInfo_t progressInfo; 1082 void *pInfoPtr; 1083 struct timeval start_time; 1084 struct timeval current_time; 1085 double elapsed; 1086 int elapsed_sec; 1087 int elapsed_usec; 1088 int simple_eta,last_eta; 1089 double rate; 1090 RF_uint64 amount; 1091 int tbit_value; 1092 char bar_buffer[1024]; 1093 char eta_buffer[1024]; 1094 1095 if (gettimeofday(&start_time,NULL) == -1) 1096 err(1, "gettimeofday failed!?!?"); 1097 memset(&progressInfo, 0, sizeof(RF_ProgressInfo_t)); 1098 pInfoPtr=&progressInfo; 1099 1100 percent_done = 0; 1101 do_ioctl(fd, option, pInfoPtr, ""); 1102 start_value = progressInfo.completed; 1103 current_time = start_time; 1104 simple_eta = 0; 1105 last_eta = 0; 1106 1107 tbit_value = 0; 1108 while(progressInfo.completed < progressInfo.total) { 1109 1110 percent_done = (progressInfo.completed * 100) / 1111 progressInfo.total; 1112 1113 get_bar(bar_buffer, percent_done, 40); 1114 1115 elapsed_sec = current_time.tv_sec - start_time.tv_sec; 1116 elapsed_usec = current_time.tv_usec - start_time.tv_usec; 1117 if (elapsed_usec < 0) { 1118 elapsed_usec-=1000000; 1119 elapsed_sec++; 1120 } 1121 1122 elapsed = (double) elapsed_sec + 1123 (double) elapsed_usec / 1000000.0; 1124 1125 amount = progressInfo.completed - start_value; 1126 1127 if (amount <= 0) { /* we don't do negatives (yet?) */ 1128 amount = 0; 1129 } 1130 1131 if (elapsed == 0) 1132 rate = 0.0; 1133 else 1134 rate = amount / elapsed; 1135 1136 if (rate > 0.0) { 1137 simple_eta = (int) (((double)progressInfo.total - 1138 (double) progressInfo.completed) 1139 / rate); 1140 } else { 1141 simple_eta = -1; 1142 } 1143 1144 if (simple_eta <=0) { 1145 simple_eta = last_eta; 1146 } else { 1147 last_eta = simple_eta; 1148 } 1149 1150 get_time_string(eta_buffer, sizeof eta_buffer, simple_eta); 1151 1152 fprintf(stdout,"\r%3d%% |%s| ETA: %s %c", 1153 percent_done,bar_buffer,eta_buffer,tbits[tbit_value]); 1154 fflush(stdout); 1155 1156 if (++tbit_value>3) 1157 tbit_value = 0; 1158 1159 sleep(2); 1160 1161 if (gettimeofday(¤t_time,NULL) == -1) 1162 err(1, "gettimeofday failed!?!?"); 1163 1164 do_ioctl( fd, option, pInfoPtr, ""); 1165 1166 1167 } 1168 printf("\n"); 1169 } 1170 /* 40 '*''s per line, then 40 ' ''s line. */ 1171 /* If you've got a screen wider than 160 characters, "tough" */ 1172 1173 #define STAR_MIDPOINT 4*40 1174 const char stars[] = "****************************************" 1175 "****************************************" 1176 "****************************************" 1177 "****************************************" 1178 " " 1179 " " 1180 " " 1181 " " 1182 " "; 1183 1184 static void 1185 get_bar(char *string, double percent, int max_strlen) 1186 { 1187 int offset; 1188 1189 if (max_strlen > STAR_MIDPOINT) { 1190 max_strlen = STAR_MIDPOINT; 1191 } 1192 offset = STAR_MIDPOINT - 1193 (int)((percent * max_strlen)/ 100); 1194 if (offset < 0) 1195 offset = 0; 1196 snprintf(string,max_strlen,"%s",stars+offset); 1197 } 1198 1199 static void 1200 get_time_string(char *string, size_t len, int simple_time) 1201 { 1202 int minutes, seconds, hours; 1203 char hours_buffer[8]; 1204 char minutes_buffer[5]; 1205 char seconds_buffer[5]; 1206 1207 if (simple_time >= 0) { 1208 1209 minutes = simple_time / 60; 1210 seconds = simple_time - 60*minutes; 1211 hours = minutes / 60; 1212 minutes = minutes - 60*hours; 1213 #if defined(__GNUC__) 1214 /* 1215 * snprintf() truncation checker fails to detect that seconds 1216 * and minutes will be 0-59 range. 1217 */ 1218 if (minutes < 0 || minutes > 60) 1219 minutes = 60; 1220 if (seconds < 0 || seconds > 60) 1221 seconds = 60; 1222 #endif 1223 1224 if (hours > 0) { 1225 snprintf(hours_buffer,sizeof hours_buffer, 1226 "%02d:",hours); 1227 } else { 1228 snprintf(hours_buffer,sizeof hours_buffer," "); 1229 } 1230 1231 snprintf(minutes_buffer,sizeof minutes_buffer,"%02d:",minutes); 1232 snprintf(seconds_buffer,sizeof seconds_buffer,"%02d",seconds); 1233 snprintf(string,len,"%s%s%s", 1234 hours_buffer, minutes_buffer, seconds_buffer); 1235 } else { 1236 snprintf(string,len," --:--"); 1237 } 1238 1239 } 1240 1241 static void 1242 component_err(int level) 1243 { 1244 const char *limit; 1245 switch (level) { 1246 case 0: 1247 limit = "more than one"; 1248 break; 1249 case 1: 1250 limit = "exactly two"; 1251 break; 1252 case 5: 1253 limit = "more than two"; 1254 break; 1255 default: 1256 return; /* no use */ 1257 } 1258 1259 fprintf(stderr, "For RAID level %d the number of columns must be %s.\n", level, limit); 1260 usage(); 1261 } 1262 1263 /* Simplified RAID creation with a single command line... */ 1264 static void 1265 rf_simple_create(int fd, int argc, char *argv[]) 1266 { 1267 int i; 1268 int level; 1269 int num_components; 1270 char *components[RF_MAXCOL]; 1271 void *generic; 1272 RF_Config_t cfg; 1273 1274 /* 1275 * Note the extra level of redirection needed here, since 1276 * what we really want to pass in is a pointer to the pointer to 1277 * the configuration structure. 1278 */ 1279 1280 1281 if (strcmp(argv[0],"mirror")==0) { 1282 level = 1; 1283 } else 1284 level = xstrtouint(argv[0]); 1285 1286 if (level != 0 && level != 1 && level !=5) 1287 usage(); 1288 1289 /* remaining args must be components */ 1290 num_components = 0; 1291 for (i=1 ; i<argc ; i++) { 1292 components[i-1] = argv[i]; 1293 num_components++; 1294 } 1295 1296 /* Level 0 must have at least two components. 1297 Level 1 must have exactly two components. 1298 Level 5 must have at least three components. */ 1299 if ((level == 0 && num_components < 2) || 1300 (level == 1 && num_components != 2) || 1301 (level == 5 && num_components < 3)) 1302 component_err(level); 1303 1304 /* build a config... */ 1305 1306 memset(&cfg, 0, sizeof(cfg)); 1307 1308 cfg.numCol = num_components; 1309 cfg.numSpare = 0; 1310 1311 for (i=0 ; i<num_components; i++) { 1312 strlcpy(cfg.devnames[0][i], components[i], 1313 sizeof(cfg.devnames[0][i])); 1314 } 1315 1316 /* pick some reasonable values for sectPerSU, etc. */ 1317 if (level == 0) { 1318 if (num_components == 2) { 1319 /* 64 blocks (32K) per component - 64K data per stripe */ 1320 cfg.sectPerSU = 64; 1321 } else if (num_components == 3 || num_components == 4) { 1322 /* 32 blocks (16K) per component - 64K data per strip for 1323 the 4-component case. */ 1324 cfg.sectPerSU = 32; 1325 } else { 1326 /* 16 blocks (8K) per component */ 1327 cfg.sectPerSU = 16; 1328 } 1329 } else if (level == 1) { 1330 /* 128 blocks (64K per component) - 64K per stripe */ 1331 cfg.sectPerSU = 128; 1332 } else if (level == 5) { 1333 if (num_components == 3) { 1334 /* 64 blocks (32K) per disk - 64K data per stripe */ 1335 cfg.sectPerSU = 64; 1336 } else if (num_components >= 4 && num_components < 9) { 1337 /* 4 components makes 3 data components. No power of 2 is 1338 evenly divisible by 3 so performance will be lousy 1339 regardless of what number we choose here. 5 components is 1340 what we are really hoping for here, as 5 components with 4 1341 data components on RAID 5 means 32 blocks (16K) per data 1342 component, or 64K per stripe */ 1343 cfg.sectPerSU = 32; 1344 } else { 1345 /* 9 components here is optimal for 16 blocks (8K) per data 1346 component */ 1347 cfg.sectPerSU = 16; 1348 } 1349 } else 1350 usage(); 1351 1352 cfg.SUsPerPU = 1; 1353 cfg.SUsPerRU = 1; 1354 cfg.parityConfig = '0' + level; 1355 strlcpy(cfg.diskQueueType, "fifo", sizeof(cfg.diskQueueType)); 1356 cfg.maxOutstandingDiskReqs = RF_DEFAULT_QUEUE_DEPTH; 1357 cfg.force = 1; 1358 1359 /* configure... */ 1360 1361 generic = &cfg; 1362 do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE"); 1363 1364 if (level == 1 || level == 5) 1365 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL, 1366 "RAIDFRAME_REWRITEPARITY"); 1367 } 1368 1369 1370 static void 1371 usage(void) 1372 { 1373 const char *progname = getprogname(); 1374 1375 fprintf(stderr, 1376 "usage: %s dev create [0 | 1 | mirror | 5] component component ...\n", 1377 progname); 1378 fprintf(stderr, " %s [-v] -A [yes | no | softroot | hardroot] dev\n", 1379 progname); 1380 fprintf(stderr, " %s [-v] -a component dev\n", progname); 1381 fprintf(stderr, " %s [-v] -B dev\n", progname); 1382 fprintf(stderr, " %s [-v] -C config_file dev\n", progname); 1383 fprintf(stderr, " %s [-v] -c config_file dev\n", progname); 1384 fprintf(stderr, " %s [-v] -F component dev\n", progname); 1385 fprintf(stderr, " %s [-v] -f component dev\n", progname); 1386 fprintf(stderr, " %s [-v] -G dev\n", progname); 1387 fprintf(stderr, " %s [-v] -g component dev\n", progname); 1388 fprintf(stderr, " %s [-v] -I serial_number dev\n", progname); 1389 fprintf(stderr, " %s [-v] -i dev\n", progname); 1390 fprintf(stderr, " %s [-v] -M [yes | no | set params] dev\n", 1391 progname); 1392 fprintf(stderr, " %s [-v] -m dev\n", progname); 1393 fprintf(stderr, " %s [-v] -P dev\n", progname); 1394 fprintf(stderr, " %s [-v] -p dev\n", progname); 1395 fprintf(stderr, " %s [-v] -R component dev\n", progname); 1396 fprintf(stderr, " %s [-v] -r component dev\n", progname); 1397 fprintf(stderr, " %s [-v] -S dev\n", progname); 1398 fprintf(stderr, " %s [-v] -s dev\n", progname); 1399 fprintf(stderr, " %s [-v] -t config_file\n", progname); 1400 fprintf(stderr, " %s [-v] -U unit dev\n", progname); 1401 fprintf(stderr, " %s [-v] -u dev\n", progname); 1402 exit(1); 1403 /* NOTREACHED */ 1404 } 1405 1406 static unsigned int 1407 xstrtouint(const char *str) 1408 { 1409 int e; 1410 unsigned int num = (unsigned int)strtou(str, NULL, 10, 0, INT_MAX, &e); 1411 if (e) 1412 errc(EXIT_FAILURE, e, "Bad number `%s'", str); 1413 return num; 1414 } 1415