1 /* $NetBSD: raidctl.c,v 1.65 2016/01/06 22:57:44 wiz 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.65 2016/01/06 22:57:44 wiz 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 void do_ioctl(int, u_long, void *, const char *); 68 static void rf_configure(int, char*, int); 69 static const char *device_status(RF_DiskStatus_t); 70 static void rf_get_device_status(int); 71 static void rf_output_configuration(int, const char *); 72 static void get_component_number(int, char *, int *, int *); 73 static void rf_fail_disk(int, char *, int); 74 __dead static void usage(void); 75 static void get_component_label(int, char *); 76 static void set_component_label(int, char *); 77 static void init_component_labels(int, int); 78 static void set_autoconfig(int, int, char *); 79 static void add_hot_spare(int, char *); 80 static void remove_hot_spare(int, char *); 81 static void rebuild_in_place(int, char *); 82 static void check_status(int,int); 83 static void check_parity(int,int, char *); 84 static void do_meter(int, u_long); 85 static void get_bar(char *, double, int); 86 static void get_time_string(char *, int); 87 static void rf_output_pmstat(int, int); 88 static void rf_pm_configure(int, int, char *, int[]); 89 static unsigned int xstrtouint(const char *); 90 91 int verbose; 92 93 static const char *rootpart[] = { "No", "Force", "Soft", "*invalid*" }; 94 95 int 96 main(int argc,char *argv[]) 97 { 98 int ch, i; 99 int num_options; 100 unsigned long action; 101 char config_filename[PATH_MAX]; 102 char dev_name[PATH_MAX]; 103 char name[PATH_MAX]; 104 char component[PATH_MAX]; 105 char autoconf[10]; 106 char *parityconf = NULL; 107 int parityparams[3]; 108 int do_output; 109 int do_recon; 110 int do_rewrite; 111 int raidID; 112 int serial_number; 113 struct stat st; 114 int fd; 115 int force; 116 int openmode; 117 int last_unit; 118 119 num_options = 0; 120 action = 0; 121 do_output = 0; 122 do_recon = 0; 123 do_rewrite = 0; 124 serial_number = 0; 125 force = 0; 126 last_unit = 0; 127 openmode = O_RDWR; /* default to read/write */ 128 129 while ((ch = getopt(argc, argv, "a:A:Bc:C:f:F:g:GiI:l:mM:r:R:sSpPuU:v")) 130 != -1) 131 switch(ch) { 132 case 'a': 133 action = RAIDFRAME_ADD_HOT_SPARE; 134 strlcpy(component, optarg, sizeof(component)); 135 num_options++; 136 break; 137 case 'A': 138 action = RAIDFRAME_SET_AUTOCONFIG; 139 strlcpy(autoconf, optarg, sizeof(autoconf)); 140 num_options++; 141 break; 142 case 'B': 143 action = RAIDFRAME_COPYBACK; 144 num_options++; 145 break; 146 case 'c': 147 action = RAIDFRAME_CONFIGURE; 148 strlcpy(config_filename, optarg, 149 sizeof(config_filename)); 150 force = 0; 151 num_options++; 152 break; 153 case 'C': 154 strlcpy(config_filename, optarg, 155 sizeof(config_filename)); 156 action = RAIDFRAME_CONFIGURE; 157 force = 1; 158 num_options++; 159 break; 160 case 'f': 161 action = RAIDFRAME_FAIL_DISK; 162 strlcpy(component, optarg, sizeof(component)); 163 do_recon = 0; 164 num_options++; 165 break; 166 case 'F': 167 action = RAIDFRAME_FAIL_DISK; 168 strlcpy(component, optarg, sizeof(component)); 169 do_recon = 1; 170 num_options++; 171 break; 172 case 'g': 173 action = RAIDFRAME_GET_COMPONENT_LABEL; 174 strlcpy(component, optarg, sizeof(component)); 175 openmode = O_RDONLY; 176 num_options++; 177 break; 178 case 'G': 179 action = RAIDFRAME_GET_INFO; 180 openmode = O_RDONLY; 181 do_output = 1; 182 num_options++; 183 break; 184 case 'i': 185 action = RAIDFRAME_REWRITEPARITY; 186 num_options++; 187 break; 188 case 'I': 189 action = RAIDFRAME_INIT_LABELS; 190 serial_number = xstrtouint(optarg); 191 num_options++; 192 break; 193 case 'm': 194 action = RAIDFRAME_PARITYMAP_STATUS; 195 openmode = O_RDONLY; 196 num_options++; 197 break; 198 case 'M': 199 action = RAIDFRAME_PARITYMAP_SET_DISABLE; 200 parityconf = strdup(optarg); 201 num_options++; 202 /* XXXjld: should rf_pm_configure do the strtol()s? */ 203 i = 0; 204 while (i < 3 && optind < argc && 205 isdigit((int)argv[optind][0])) 206 parityparams[i++] = xstrtouint(argv[optind++]); 207 while (i < 3) 208 parityparams[i++] = 0; 209 break; 210 case 'l': 211 action = RAIDFRAME_SET_COMPONENT_LABEL; 212 strlcpy(component, optarg, sizeof(component)); 213 num_options++; 214 break; 215 case 'r': 216 action = RAIDFRAME_REMOVE_HOT_SPARE; 217 strlcpy(component, optarg, sizeof(component)); 218 num_options++; 219 break; 220 case 'R': 221 strlcpy(component, optarg, sizeof(component)); 222 action = RAIDFRAME_REBUILD_IN_PLACE; 223 num_options++; 224 break; 225 case 's': 226 action = RAIDFRAME_GET_INFO; 227 openmode = O_RDONLY; 228 num_options++; 229 break; 230 case 'S': 231 action = RAIDFRAME_CHECK_RECON_STATUS_EXT; 232 openmode = O_RDONLY; 233 num_options++; 234 break; 235 case 'p': 236 action = RAIDFRAME_CHECK_PARITY; 237 openmode = O_RDONLY; 238 num_options++; 239 break; 240 case 'P': 241 action = RAIDFRAME_CHECK_PARITY; 242 do_rewrite = 1; 243 num_options++; 244 break; 245 case 'u': 246 action = RAIDFRAME_SHUTDOWN; 247 num_options++; 248 break; 249 case 'U': 250 action = RAIDFRAME_SET_LAST_UNIT; 251 num_options++; 252 last_unit = atoi(optarg); 253 if (last_unit < 0) 254 errx(1, "Bad last unit %s", optarg); 255 break; 256 case 'v': 257 verbose = 1; 258 /* Don't bump num_options, as '-v' is not 259 an option like the others */ 260 /* num_options++; */ 261 break; 262 default: 263 usage(); 264 } 265 argc -= optind; 266 argv += optind; 267 268 if ((num_options > 1) || (argc == 0)) 269 usage(); 270 271 if (prog_init && prog_init() == -1) 272 err(1, "init failed"); 273 274 strlcpy(name, argv[0], sizeof(name)); 275 fd = opendisk1(name, openmode, dev_name, sizeof(dev_name), 0, 276 prog_open); 277 if (fd == -1) 278 err(1, "Unable to open device file: %s", name); 279 if (prog_fstat(fd, &st) == -1) 280 err(1, "stat failure on: %s", dev_name); 281 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) 282 err(1, "invalid device: %s", dev_name); 283 284 raidID = DISKUNIT(st.st_rdev); 285 286 switch(action) { 287 case RAIDFRAME_ADD_HOT_SPARE: 288 add_hot_spare(fd, component); 289 break; 290 case RAIDFRAME_REMOVE_HOT_SPARE: 291 remove_hot_spare(fd, component); 292 break; 293 case RAIDFRAME_CONFIGURE: 294 rf_configure(fd, config_filename, force); 295 break; 296 case RAIDFRAME_SET_AUTOCONFIG: 297 set_autoconfig(fd, raidID, autoconf); 298 break; 299 case RAIDFRAME_COPYBACK: 300 printf("Copyback.\n"); 301 do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK"); 302 if (verbose) { 303 sleep(3); /* XXX give the copyback a chance to start */ 304 printf("Copyback status:\n"); 305 do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS_EXT); 306 } 307 break; 308 case RAIDFRAME_FAIL_DISK: 309 rf_fail_disk(fd, component, do_recon); 310 break; 311 case RAIDFRAME_SET_COMPONENT_LABEL: 312 set_component_label(fd, component); 313 break; 314 case RAIDFRAME_GET_COMPONENT_LABEL: 315 get_component_label(fd, component); 316 break; 317 case RAIDFRAME_INIT_LABELS: 318 init_component_labels(fd, serial_number); 319 break; 320 case RAIDFRAME_REWRITEPARITY: 321 printf("Initiating re-write of parity\n"); 322 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL, 323 "RAIDFRAME_REWRITEPARITY"); 324 if (verbose) { 325 sleep(3); /* XXX give it time to get started */ 326 printf("Parity Re-write status:\n"); 327 do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT); 328 } 329 break; 330 case RAIDFRAME_CHECK_RECON_STATUS_EXT: 331 check_status(fd,1); 332 break; 333 case RAIDFRAME_GET_INFO: 334 if (do_output) 335 rf_output_configuration(fd, dev_name); 336 else 337 rf_get_device_status(fd); 338 break; 339 case RAIDFRAME_PARITYMAP_STATUS: 340 rf_output_pmstat(fd, raidID); 341 break; 342 case RAIDFRAME_PARITYMAP_SET_DISABLE: 343 rf_pm_configure(fd, raidID, parityconf, parityparams); 344 break; 345 case RAIDFRAME_REBUILD_IN_PLACE: 346 rebuild_in_place(fd, component); 347 break; 348 case RAIDFRAME_CHECK_PARITY: 349 check_parity(fd, do_rewrite, dev_name); 350 break; 351 case RAIDFRAME_SHUTDOWN: 352 do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN"); 353 break; 354 case RAIDFRAME_SET_LAST_UNIT: 355 do_ioctl(fd, RAIDFRAME_SET_LAST_UNIT, &last_unit, 356 "RAIDFRAME_SET_LAST_UNIT"); 357 break; 358 default: 359 break; 360 } 361 362 prog_close(fd); 363 exit(0); 364 } 365 366 void 367 do_ioctl(int fd, unsigned long command, void *arg, const char *ioctl_name) 368 { 369 if (prog_ioctl(fd, command, arg) == -1) 370 err(1, "ioctl (%s) failed", ioctl_name); 371 } 372 373 374 static void 375 rf_configure(int fd, char *config_file, int force) 376 { 377 void *generic; 378 RF_Config_t cfg; 379 380 if (rf_MakeConfig( config_file, &cfg ) != 0) 381 err(1, "Unable to create RAIDframe configuration structure"); 382 383 cfg.force = force; 384 385 /* 386 * Note the extra level of redirection needed here, since 387 * what we really want to pass in is a pointer to the pointer to 388 * the configuration structure. 389 */ 390 391 generic = &cfg; 392 do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE"); 393 } 394 395 static const char * 396 device_status(RF_DiskStatus_t status) 397 { 398 399 switch (status) { 400 case rf_ds_optimal: 401 return ("optimal"); 402 break; 403 case rf_ds_failed: 404 return ("failed"); 405 break; 406 case rf_ds_reconstructing: 407 return ("reconstructing"); 408 break; 409 case rf_ds_dist_spared: 410 return ("dist_spared"); 411 break; 412 case rf_ds_spared: 413 return ("spared"); 414 break; 415 case rf_ds_spare: 416 return ("spare"); 417 break; 418 case rf_ds_used_spare: 419 return ("used_spare"); 420 break; 421 default: 422 return ("UNKNOWN"); 423 } 424 /* NOTREACHED */ 425 } 426 427 static void 428 rf_get_device_status(int fd) 429 { 430 RF_DeviceConfig_t device_config; 431 void *cfg_ptr; 432 int is_clean; 433 int i; 434 435 cfg_ptr = &device_config; 436 437 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO"); 438 439 printf("Components:\n"); 440 for(i=0; i < device_config.ndevs; i++) { 441 printf("%20s: %s\n", device_config.devs[i].devname, 442 device_status(device_config.devs[i].status)); 443 } 444 if (device_config.nspares > 0) { 445 printf("Spares:\n"); 446 for(i=0; i < device_config.nspares; i++) { 447 printf("%20s: %s\n", 448 device_config.spares[i].devname, 449 device_status(device_config.spares[i].status)); 450 } 451 } else { 452 printf("No spares.\n"); 453 } 454 for(i=0; i < device_config.ndevs; i++) { 455 if (device_config.devs[i].status == rf_ds_optimal) { 456 get_component_label(fd, device_config.devs[i].devname); 457 } else { 458 printf("%s status is: %s. Skipping label.\n", 459 device_config.devs[i].devname, 460 device_status(device_config.devs[i].status)); 461 } 462 } 463 464 if (device_config.nspares > 0) { 465 for(i=0; i < device_config.nspares; i++) { 466 if ((device_config.spares[i].status == 467 rf_ds_optimal) || 468 (device_config.spares[i].status == 469 rf_ds_used_spare)) { 470 get_component_label(fd, 471 device_config.spares[i].devname); 472 } else { 473 printf("%s status is: %s. Skipping label.\n", 474 device_config.spares[i].devname, 475 device_status(device_config.spares[i].status)); 476 } 477 } 478 } 479 480 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean, 481 "RAIDFRAME_CHECK_PARITY"); 482 if (is_clean) { 483 printf("Parity status: clean\n"); 484 } else { 485 printf("Parity status: DIRTY\n"); 486 } 487 check_status(fd,0); 488 } 489 490 static void 491 rf_output_pmstat(int fd, int raidID) 492 { 493 char srs[7]; 494 unsigned int i, j; 495 int dis, dr; 496 struct rf_pmstat st; 497 498 if (prog_ioctl(fd, RAIDFRAME_PARITYMAP_STATUS, &st) == -1) { 499 if (errno == EINVAL) { 500 printf("raid%d: has no parity; parity map disabled\n", 501 raidID); 502 return; 503 } 504 err(1, "ioctl (%s) failed", "RAIDFRAME_PARITYMAP_STATUS"); 505 } 506 507 if (st.enabled) { 508 if (0 > humanize_number(srs, 7, st.region_size * DEV_BSIZE, 509 "B", HN_AUTOSCALE, HN_NOSPACE)) 510 strlcpy(srs, "???", 7); 511 512 printf("raid%d: parity map enabled with %u regions of %s\n", 513 raidID, st.params.regions, srs); 514 printf("raid%d: regions marked clean after %d intervals of" 515 " %d.%03ds\n", raidID, st.params.cooldown, 516 st.params.tickms / 1000, st.params.tickms % 1000); 517 printf("raid%d: write/sync/clean counters " 518 "%"PRIu64"/%"PRIu64"/%"PRIu64"\n", raidID, 519 st.ctrs.nwrite, st.ctrs.ncachesync, st.ctrs.nclearing); 520 521 dr = 0; 522 for (i = 0; i < st.params.regions; i++) 523 if (isset(st.dirty, i)) 524 dr++; 525 printf("raid%d: %d dirty region%s\n", raidID, dr, 526 dr == 1 ? "" : "s"); 527 528 if (verbose > 0) { 529 for (i = 0; i < RF_PARITYMAP_NBYTE; i += 32) { 530 printf(" "); 531 for (j = i; j < RF_PARITYMAP_NBYTE 532 && j < i + 32; j++) 533 printf("%x%x", st.dirty[j] & 15, 534 (st.dirty[j] >> 4) & 15); 535 printf("\n"); 536 } 537 } 538 } else { 539 printf("raid%d: parity map disabled\n", raidID); 540 } 541 542 do_ioctl(fd, RAIDFRAME_PARITYMAP_GET_DISABLE, &dis, 543 "RAIDFRAME_PARITYMAP_GET_DISABLE"); 544 printf("raid%d: parity map will %s %sabled on next configure\n", 545 raidID, dis == st.enabled ? "be" : "remain", dis ? "dis" : "en"); 546 } 547 548 static void 549 rf_pm_configure(int fd, int raidID, char *parityconf, int parityparams[]) 550 { 551 int dis; 552 struct rf_pmparams params; 553 554 if (strcasecmp(parityconf, "yes") == 0) 555 dis = 0; 556 else if (strcasecmp(parityconf, "no") == 0) 557 dis = 1; 558 else if (strcasecmp(parityconf, "set") == 0) { 559 params.cooldown = parityparams[0]; 560 params.tickms = parityparams[1]; 561 params.regions = parityparams[2]; 562 563 do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_PARAMS, ¶ms, 564 "RAIDFRAME_PARITYMAP_SET_PARAMS"); 565 566 if (params.cooldown != 0 || params.tickms != 0) { 567 printf("raid%d: parity cleaned after", raidID); 568 if (params.cooldown != 0) 569 printf(" %d", params.cooldown); 570 printf(" intervals"); 571 if (params.tickms != 0) { 572 printf(" of %d.%03ds", params.tickms / 1000, 573 params.tickms % 1000); 574 } 575 printf("\n"); 576 } 577 if (params.regions != 0) 578 printf("raid%d: will use %d regions on next" 579 " configuration\n", raidID, params.regions); 580 581 return; 582 /* XXX the control flow here could be prettier. */ 583 } else 584 err(1, "`%s' is not a valid parity map command", parityconf); 585 586 do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_DISABLE, &dis, 587 "RAIDFRAME_PARITYMAP_SET_DISABLE"); 588 printf("raid%d: parity map will be %sabled on next configure\n", 589 raidID, dis ? "dis" : "en"); 590 } 591 592 /* convert "component0" into "absent" */ 593 static const char *rf_output_devname(const char *name) 594 { 595 596 if (strncmp(name, "component", 9) == 0) 597 return "absent"; 598 return name; 599 } 600 601 static void 602 rf_output_configuration(int fd, const char *name) 603 { 604 RF_DeviceConfig_t device_config; 605 void *cfg_ptr; 606 int i; 607 RF_ComponentLabel_t component_label; 608 void *label_ptr; 609 int component_num; 610 int num_cols; 611 612 cfg_ptr = &device_config; 613 614 printf("# raidctl config file for %s\n", name); 615 printf("\n"); 616 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO"); 617 618 printf("START array\n"); 619 printf("# numRow numCol numSpare\n"); 620 printf("%d %d %d\n", device_config.rows, device_config.cols, 621 device_config.nspares); 622 printf("\n"); 623 624 printf("START disks\n"); 625 for(i=0; i < device_config.ndevs; i++) 626 printf("%s\n", 627 rf_output_devname(device_config.devs[i].devname)); 628 printf("\n"); 629 630 if (device_config.nspares > 0) { 631 printf("START spare\n"); 632 for(i=0; i < device_config.nspares; i++) 633 printf("%s\n", device_config.spares[i].devname); 634 printf("\n"); 635 } 636 637 for(i=0; i < device_config.ndevs; i++) { 638 if (device_config.devs[i].status == rf_ds_optimal) 639 break; 640 } 641 if (i == device_config.ndevs) { 642 printf("# WARNING: no optimal components; using %s\n", 643 device_config.devs[0].devname); 644 i = 0; 645 } 646 get_component_number(fd, device_config.devs[i].devname, 647 &component_num, &num_cols); 648 memset(&component_label, 0, sizeof(RF_ComponentLabel_t)); 649 component_label.row = component_num / num_cols; 650 component_label.column = component_num % num_cols; 651 label_ptr = &component_label; 652 do_ioctl(fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr, 653 "RAIDFRAME_GET_COMPONENT_LABEL"); 654 655 printf("START layout\n"); 656 printf( 657 "# sectPerSU SUsPerParityUnit SUsPerReconUnit RAID_level_%c\n", 658 (char) component_label.parityConfig); 659 printf("%d %d %d %c\n", 660 component_label.sectPerSU, component_label.SUsPerPU, 661 component_label.SUsPerRU, (char) component_label.parityConfig); 662 printf("\n"); 663 664 printf("START queue\n"); 665 printf("fifo %d\n", device_config.maxqdepth); 666 } 667 668 static void 669 get_component_number(int fd, char *component_name, int *component_number, 670 int *num_columns) 671 { 672 RF_DeviceConfig_t device_config; 673 void *cfg_ptr; 674 int i; 675 int found; 676 677 *component_number = -1; 678 679 /* Assuming a full path spec... */ 680 cfg_ptr = &device_config; 681 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, 682 "RAIDFRAME_GET_INFO"); 683 684 *num_columns = device_config.cols; 685 686 found = 0; 687 for(i=0; i < device_config.ndevs; i++) { 688 if (strncmp(component_name, device_config.devs[i].devname, 689 PATH_MAX)==0) { 690 found = 1; 691 *component_number = i; 692 } 693 } 694 if (!found) { /* maybe it's a spare? */ 695 for(i=0; i < device_config.nspares; i++) { 696 if (strncmp(component_name, 697 device_config.spares[i].devname, 698 PATH_MAX)==0) { 699 found = 1; 700 *component_number = i + device_config.ndevs; 701 /* the way spares are done should 702 really change... */ 703 *num_columns = device_config.cols + 704 device_config.nspares; 705 } 706 } 707 } 708 709 if (!found) 710 err(1,"%s is not a component of this device", component_name); 711 } 712 713 static void 714 rf_fail_disk(int fd, char *component_to_fail, int do_recon) 715 { 716 struct rf_recon_req recon_request; 717 int component_num; 718 int num_cols; 719 720 get_component_number(fd, component_to_fail, &component_num, &num_cols); 721 722 recon_request.row = component_num / num_cols; 723 recon_request.col = component_num % num_cols; 724 if (do_recon) { 725 recon_request.flags = RF_FDFLAGS_RECON; 726 } else { 727 recon_request.flags = RF_FDFLAGS_NONE; 728 } 729 do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request, 730 "RAIDFRAME_FAIL_DISK"); 731 if (do_recon && verbose) { 732 printf("Reconstruction status:\n"); 733 sleep(3); /* XXX give reconstruction a chance to start */ 734 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT); 735 } 736 } 737 738 static void 739 get_component_label(int fd, char *component) 740 { 741 RF_ComponentLabel_t component_label; 742 void *label_ptr; 743 int component_num; 744 int num_cols; 745 746 get_component_number(fd, component, &component_num, &num_cols); 747 748 memset( &component_label, 0, sizeof(RF_ComponentLabel_t)); 749 component_label.row = component_num / num_cols; 750 component_label.column = component_num % num_cols; 751 752 label_ptr = &component_label; 753 do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr, 754 "RAIDFRAME_GET_COMPONENT_LABEL"); 755 756 printf("Component label for %s:\n",component); 757 758 printf(" Row: %d, Column: %d, Num Rows: %d, Num Columns: %d\n", 759 component_label.row, component_label.column, 760 component_label.num_rows, component_label.num_columns); 761 printf(" Version: %d, Serial Number: %u, Mod Counter: %d\n", 762 component_label.version, component_label.serial_number, 763 component_label.mod_counter); 764 printf(" Clean: %s, Status: %d\n", 765 component_label.clean ? "Yes" : "No", 766 component_label.status ); 767 printf(" sectPerSU: %d, SUsPerPU: %d, SUsPerRU: %d\n", 768 component_label.sectPerSU, component_label.SUsPerPU, 769 component_label.SUsPerRU); 770 printf(" Queue size: %d, blocksize: %d, numBlocks: %"PRIu64"\n", 771 component_label.maxOutstanding, component_label.blockSize, 772 rf_component_label_numblocks(&component_label)); 773 printf(" RAID Level: %c\n", (char) component_label.parityConfig); 774 printf(" Autoconfig: %s\n", 775 component_label.autoconfigure ? "Yes" : "No" ); 776 printf(" Root partition: %s\n", 777 rootpart[component_label.root_partition & 3]); 778 printf(" Last configured as: raid%d\n", component_label.last_unit ); 779 } 780 781 static void 782 set_component_label(int fd, char *component) 783 { 784 RF_ComponentLabel_t component_label; 785 int component_num; 786 int num_cols; 787 788 get_component_number(fd, component, &component_num, &num_cols); 789 790 /* XXX This is currently here for testing, and future expandability */ 791 792 component_label.version = 1; 793 component_label.serial_number = 123456; 794 component_label.mod_counter = 0; 795 component_label.row = component_num / num_cols; 796 component_label.column = component_num % num_cols; 797 component_label.num_rows = 0; 798 component_label.num_columns = 5; 799 component_label.clean = 0; 800 component_label.status = 1; 801 802 do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label, 803 "RAIDFRAME_SET_COMPONENT_LABEL"); 804 } 805 806 807 static void 808 init_component_labels(int fd, int serial_number) 809 { 810 RF_ComponentLabel_t component_label; 811 812 component_label.version = 0; 813 component_label.serial_number = serial_number; 814 component_label.mod_counter = 0; 815 component_label.row = 0; 816 component_label.column = 0; 817 component_label.num_rows = 0; 818 component_label.num_columns = 0; 819 component_label.clean = 0; 820 component_label.status = 0; 821 822 do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label, 823 "RAIDFRAME_SET_COMPONENT_LABEL"); 824 } 825 826 static void 827 set_autoconfig(int fd, int raidID, char *autoconf) 828 { 829 int auto_config; 830 int root_config; 831 832 auto_config = 0; 833 root_config = 0; 834 835 if (strncasecmp(autoconf, "root", 4) == 0 || 836 strncasecmp(autoconf, "hard", 4) == 0 || 837 strncasecmp(autoconf, "force", 5) == 0) { 838 root_config = 1; 839 } else if (strncasecmp(autoconf, "soft", 4) == 0) { 840 root_config = 2; 841 } 842 843 if ((strncasecmp(autoconf,"yes", 3) == 0) || 844 root_config > 0) { 845 auto_config = 1; 846 } 847 848 do_ioctl(fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config, 849 "RAIDFRAME_SET_AUTOCONFIG"); 850 851 do_ioctl(fd, RAIDFRAME_SET_ROOT, &root_config, 852 "RAIDFRAME_SET_ROOT"); 853 854 printf("raid%d: Autoconfigure: %s\n", raidID, 855 auto_config ? "Yes" : "No"); 856 857 if (auto_config == 1) { 858 printf("raid%d: Root: %s\n", raidID, rootpart[root_config]); 859 } 860 } 861 862 static void 863 add_hot_spare(int fd, char *component) 864 { 865 RF_SingleComponent_t hot_spare; 866 867 hot_spare.row = 0; 868 hot_spare.column = 0; 869 strncpy(hot_spare.component_name, component, 870 sizeof(hot_spare.component_name)); 871 872 do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare, 873 "RAIDFRAME_ADD_HOT_SPARE"); 874 } 875 876 static void 877 remove_hot_spare(int fd, char *component) 878 { 879 RF_SingleComponent_t hot_spare; 880 int component_num; 881 int num_cols; 882 883 get_component_number(fd, component, &component_num, &num_cols); 884 885 hot_spare.row = component_num / num_cols; 886 hot_spare.column = component_num % num_cols; 887 888 strncpy(hot_spare.component_name, component, 889 sizeof(hot_spare.component_name)); 890 891 do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare, 892 "RAIDFRAME_REMOVE_HOT_SPARE"); 893 } 894 895 static void 896 rebuild_in_place(int fd, char *component) 897 { 898 RF_SingleComponent_t comp; 899 int component_num; 900 int num_cols; 901 902 get_component_number(fd, component, &component_num, &num_cols); 903 904 comp.row = 0; 905 comp.column = component_num; 906 strncpy(comp.component_name, component, sizeof(comp.component_name)); 907 908 do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp, 909 "RAIDFRAME_REBUILD_IN_PLACE"); 910 911 if (verbose) { 912 printf("Reconstruction status:\n"); 913 sleep(3); /* XXX give reconstruction a chance to start */ 914 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT); 915 } 916 917 } 918 919 static void 920 check_parity(int fd, int do_rewrite, char *dev_name) 921 { 922 int is_clean; 923 int percent_done; 924 925 is_clean = 0; 926 percent_done = 0; 927 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean, 928 "RAIDFRAME_CHECK_PARITY"); 929 if (is_clean) { 930 printf("%s: Parity status: clean\n",dev_name); 931 } else { 932 printf("%s: Parity status: DIRTY\n",dev_name); 933 if (do_rewrite) { 934 printf("%s: Initiating re-write of parity\n", 935 dev_name); 936 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL, 937 "RAIDFRAME_REWRITEPARITY"); 938 sleep(3); /* XXX give it time to 939 get started. */ 940 if (verbose) { 941 printf("Parity Re-write status:\n"); 942 do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT); 943 } else { 944 do_ioctl(fd, 945 RAIDFRAME_CHECK_PARITYREWRITE_STATUS, 946 &percent_done, 947 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS" 948 ); 949 while( percent_done < 100 ) { 950 sleep(3); /* wait a bit... */ 951 do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS, 952 &percent_done, "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"); 953 } 954 955 } 956 printf("%s: Parity Re-write complete\n", 957 dev_name); 958 } else { 959 /* parity is wrong, and is not being fixed. 960 Exit w/ an error. */ 961 exit(1); 962 } 963 } 964 } 965 966 967 static void 968 check_status(int fd, int meter) 969 { 970 int recon_percent_done = 0; 971 int parity_percent_done = 0; 972 int copyback_percent_done = 0; 973 974 do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done, 975 "RAIDFRAME_CHECK_RECON_STATUS"); 976 printf("Reconstruction is %d%% complete.\n", recon_percent_done); 977 do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS, 978 &parity_percent_done, 979 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"); 980 printf("Parity Re-write is %d%% complete.\n", parity_percent_done); 981 do_ioctl(fd, RAIDFRAME_CHECK_COPYBACK_STATUS, ©back_percent_done, 982 "RAIDFRAME_CHECK_COPYBACK_STATUS"); 983 printf("Copyback is %d%% complete.\n", copyback_percent_done); 984 985 if (meter) { 986 /* These 3 should be mutually exclusive at this point */ 987 if (recon_percent_done < 100) { 988 printf("Reconstruction status:\n"); 989 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT); 990 } else if (parity_percent_done < 100) { 991 printf("Parity Re-write status:\n"); 992 do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT); 993 } else if (copyback_percent_done < 100) { 994 printf("Copyback status:\n"); 995 do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS_EXT); 996 } 997 } 998 } 999 1000 const char *tbits = "|/-\\"; 1001 1002 static void 1003 do_meter(int fd, u_long option) 1004 { 1005 int percent_done; 1006 RF_uint64 start_value; 1007 RF_ProgressInfo_t progressInfo; 1008 void *pInfoPtr; 1009 struct timeval start_time; 1010 struct timeval current_time; 1011 double elapsed; 1012 int elapsed_sec; 1013 int elapsed_usec; 1014 int simple_eta,last_eta; 1015 double rate; 1016 RF_uint64 amount; 1017 int tbit_value; 1018 char bar_buffer[1024]; 1019 char eta_buffer[1024]; 1020 1021 if (gettimeofday(&start_time,NULL) == -1) 1022 err(1, "gettimeofday failed!?!?"); 1023 memset(&progressInfo, 0, sizeof(RF_ProgressInfo_t)); 1024 pInfoPtr=&progressInfo; 1025 1026 percent_done = 0; 1027 do_ioctl(fd, option, &pInfoPtr, ""); 1028 start_value = progressInfo.completed; 1029 current_time = start_time; 1030 simple_eta = 0; 1031 last_eta = 0; 1032 1033 tbit_value = 0; 1034 while(progressInfo.completed < progressInfo.total) { 1035 1036 percent_done = (progressInfo.completed * 100) / 1037 progressInfo.total; 1038 1039 get_bar(bar_buffer, percent_done, 40); 1040 1041 elapsed_sec = current_time.tv_sec - start_time.tv_sec; 1042 elapsed_usec = current_time.tv_usec - start_time.tv_usec; 1043 if (elapsed_usec < 0) { 1044 elapsed_usec-=1000000; 1045 elapsed_sec++; 1046 } 1047 1048 elapsed = (double) elapsed_sec + 1049 (double) elapsed_usec / 1000000.0; 1050 1051 amount = progressInfo.completed - start_value; 1052 1053 if (amount <= 0) { /* we don't do negatives (yet?) */ 1054 amount = 0; 1055 } 1056 1057 if (elapsed == 0) 1058 rate = 0.0; 1059 else 1060 rate = amount / elapsed; 1061 1062 if (rate > 0.0) { 1063 simple_eta = (int) (((double)progressInfo.total - 1064 (double) progressInfo.completed) 1065 / rate); 1066 } else { 1067 simple_eta = -1; 1068 } 1069 1070 if (simple_eta <=0) { 1071 simple_eta = last_eta; 1072 } else { 1073 last_eta = simple_eta; 1074 } 1075 1076 get_time_string(eta_buffer, simple_eta); 1077 1078 fprintf(stdout,"\r%3d%% |%s| ETA: %s %c", 1079 percent_done,bar_buffer,eta_buffer,tbits[tbit_value]); 1080 fflush(stdout); 1081 1082 if (++tbit_value>3) 1083 tbit_value = 0; 1084 1085 sleep(2); 1086 1087 if (gettimeofday(¤t_time,NULL) == -1) 1088 err(1, "gettimeofday failed!?!?"); 1089 1090 do_ioctl( fd, option, &pInfoPtr, ""); 1091 1092 1093 } 1094 printf("\n"); 1095 } 1096 /* 40 '*''s per line, then 40 ' ''s line. */ 1097 /* If you've got a screen wider than 160 characters, "tough" */ 1098 1099 #define STAR_MIDPOINT 4*40 1100 const char stars[] = "****************************************" 1101 "****************************************" 1102 "****************************************" 1103 "****************************************" 1104 " " 1105 " " 1106 " " 1107 " " 1108 " "; 1109 1110 static void 1111 get_bar(char *string, double percent, int max_strlen) 1112 { 1113 int offset; 1114 1115 if (max_strlen > STAR_MIDPOINT) { 1116 max_strlen = STAR_MIDPOINT; 1117 } 1118 offset = STAR_MIDPOINT - 1119 (int)((percent * max_strlen)/ 100); 1120 if (offset < 0) 1121 offset = 0; 1122 snprintf(string,max_strlen,"%s",stars+offset); 1123 } 1124 1125 static void 1126 get_time_string(char *string, int simple_time) 1127 { 1128 int minutes, seconds, hours; 1129 char hours_buffer[5]; 1130 char minutes_buffer[5]; 1131 char seconds_buffer[5]; 1132 1133 if (simple_time >= 0) { 1134 1135 minutes = (int) simple_time / 60; 1136 seconds = ((int)simple_time - 60*minutes); 1137 hours = minutes / 60; 1138 minutes = minutes - 60*hours; 1139 1140 if (hours > 0) { 1141 snprintf(hours_buffer,5,"%02d:",hours); 1142 } else { 1143 snprintf(hours_buffer,5," "); 1144 } 1145 1146 snprintf(minutes_buffer,5,"%02d:",minutes); 1147 snprintf(seconds_buffer,5,"%02d",seconds); 1148 snprintf(string,1024,"%s%s%s", 1149 hours_buffer, minutes_buffer, seconds_buffer); 1150 } else { 1151 snprintf(string,1024," --:--"); 1152 } 1153 1154 } 1155 1156 static void 1157 usage(void) 1158 { 1159 const char *progname = getprogname(); 1160 1161 fprintf(stderr, "usage: %s [-v] -A [yes | no | softroot | hardroot] dev\n", progname); 1162 fprintf(stderr, " %s [-v] -a component dev\n", progname); 1163 fprintf(stderr, " %s [-v] -B dev\n", progname); 1164 fprintf(stderr, " %s [-v] -C config_file dev\n", progname); 1165 fprintf(stderr, " %s [-v] -c config_file dev\n", progname); 1166 fprintf(stderr, " %s [-v] -F component dev\n", progname); 1167 fprintf(stderr, " %s [-v] -f component dev\n", progname); 1168 fprintf(stderr, " %s [-v] -G dev\n", progname); 1169 fprintf(stderr, " %s [-v] -g component dev\n", progname); 1170 fprintf(stderr, " %s [-v] -I serial_number dev\n", progname); 1171 fprintf(stderr, " %s [-v] -i dev\n", progname); 1172 fprintf(stderr, " %s [-v] -M [yes | no | set params] dev\n", 1173 progname); 1174 fprintf(stderr, " %s [-v] -m dev\n", progname); 1175 fprintf(stderr, " %s [-v] -P dev\n", progname); 1176 fprintf(stderr, " %s [-v] -p dev\n", progname); 1177 fprintf(stderr, " %s [-v] -R component dev\n", progname); 1178 fprintf(stderr, " %s [-v] -r component dev\n", progname); 1179 fprintf(stderr, " %s [-v] -S dev\n", progname); 1180 fprintf(stderr, " %s [-v] -s dev\n", progname); 1181 fprintf(stderr, " %s [-v] -U unit dev\n", progname); 1182 fprintf(stderr, " %s [-v] -u dev\n", progname); 1183 exit(1); 1184 /* NOTREACHED */ 1185 } 1186 1187 static unsigned int 1188 xstrtouint(const char *str) 1189 { 1190 int e; 1191 unsigned int num = (unsigned int)strtou(str, NULL, 10, 0, INT_MAX, &e); 1192 if (e) 1193 errc(EXIT_FAILURE, e, "Bad number `%s'", str); 1194 return num; 1195 } 1196