Home | History | Annotate | Line # | Download | only in raidctl
raidctl.c revision 1.13
      1 /*      $NetBSD: raidctl.c,v 1.13 2000/02/24 23:52:46 oster Exp $   */
      2 /*-
      3  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Greg Oster
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39 
     40    This program is a re-write of the original rf_ctrl program
     41    distributed by CMU with RAIDframe 1.1.
     42 
     43    This program is the user-land interface to the RAIDframe kernel
     44    driver in NetBSD.
     45 
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/stat.h>
     51 #include <util.h>
     52 #include <stdio.h>
     53 #include <fcntl.h>
     54 #include <ctype.h>
     55 #include <err.h>
     56 #include <errno.h>
     57 #include <sys/types.h>
     58 #include <string.h>
     59 #include <sys/disklabel.h>
     60 #include <machine/disklabel.h>
     61 #include <stdlib.h>
     62 #include <unistd.h>
     63 
     64 #include "rf_raidframe.h"
     65 
     66 extern  char *__progname;
     67 
     68 int     main __P((int, char *[]));
     69 void do_ioctl __P((int, unsigned long, void *, char *));
     70 static  void rf_configure __P((int, char*, int));
     71 static  char *device_status __P((RF_DiskStatus_t));
     72 static  void rf_get_device_status __P((int));
     73 static  void get_component_number __P((int, char *, int *, int *));
     74 static  void rf_fail_disk __P((int, char *, int));
     75 static  void usage __P((void));
     76 static  void get_component_label __P((int, char *));
     77 static  void set_component_label __P((int, char *));
     78 static  void init_component_labels __P((int, int));
     79 static  void set_autoconfig __P((int, int, char *));
     80 static  void add_hot_spare __P((int, char *));
     81 static  void remove_hot_spare __P((int, char *));
     82 static  void rebuild_in_place __P((int, char *));
     83 static  void check_status __P((int));
     84 static  void check_parity __P((int,int,char *));
     85 static  void do_meter __P((int, int));
     86 static  void get_bar __P((char *, double, int));
     87 static  void get_time_string __P((char *, int));
     88 
     89 int verbose = 0;
     90 
     91 int
     92 main(argc,argv)
     93 	int argc;
     94 	char *argv[];
     95 {
     96 	extern char *optarg;
     97 	extern int optind;
     98 	int ch;
     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 	int do_recon;
    107 	int do_rewrite;
    108 	int is_clean;
    109 	int raidID;
    110 	int rawpart;
    111 	int serial_number;
    112 	struct stat st;
    113 	int fd;
    114 	int force;
    115 
    116 	num_options = 0;
    117 	action = 0;
    118 	do_recon = 0;
    119 	do_rewrite = 0;
    120 	is_clean = 0;
    121 	force = 0;
    122 
    123 	while ((ch = getopt(argc, argv, "a:A:Bc:C:f:F:g:iI:l:r:R:sSpPuv"))
    124 	       != -1)
    125 		switch(ch) {
    126 		case 'a':
    127 			action = RAIDFRAME_ADD_HOT_SPARE;
    128 			strncpy(component, optarg, PATH_MAX);
    129 			num_options++;
    130 			break;
    131 		case 'A':
    132 			action = RAIDFRAME_SET_AUTOCONFIG;
    133 			strncpy(autoconf, optarg, 10);
    134 			num_options++;
    135 			break;
    136 		case 'B':
    137 			action = RAIDFRAME_COPYBACK;
    138 			num_options++;
    139 			break;
    140 		case 'c':
    141 			action = RAIDFRAME_CONFIGURE;
    142 			strncpy(config_filename,optarg,PATH_MAX);
    143 			force = 0;
    144 			num_options++;
    145 			break;
    146 		case 'C':
    147 			strncpy(config_filename,optarg,PATH_MAX);
    148 			action = RAIDFRAME_CONFIGURE;
    149 			force = 1;
    150 			num_options++;
    151 			break;
    152 		case 'f':
    153 			action = RAIDFRAME_FAIL_DISK;
    154 			strncpy(component, optarg, PATH_MAX);
    155 			do_recon = 0;
    156 			num_options++;
    157 			break;
    158 		case 'F':
    159 			action = RAIDFRAME_FAIL_DISK;
    160 			strncpy(component, optarg, PATH_MAX);
    161 			do_recon = 1;
    162 			num_options++;
    163 			break;
    164 		case 'g':
    165 			action = RAIDFRAME_GET_COMPONENT_LABEL;
    166 			strncpy(component, optarg, PATH_MAX);
    167 			num_options++;
    168 			break;
    169 		case 'i':
    170 			action = RAIDFRAME_REWRITEPARITY;
    171 			num_options++;
    172 			break;
    173 		case 'I':
    174 			action = RAIDFRAME_INIT_LABELS;
    175 			serial_number = atoi(optarg);
    176 			num_options++;
    177 			break;
    178 		case 'l':
    179 			action = RAIDFRAME_SET_COMPONENT_LABEL;
    180 			strncpy(component, optarg, PATH_MAX);
    181 			num_options++;
    182 			break;
    183 		case 'r':
    184 			action = RAIDFRAME_REMOVE_HOT_SPARE;
    185 			strncpy(component, optarg, PATH_MAX);
    186 			num_options++;
    187 			break;
    188 		case 'R':
    189 			strncpy(component,optarg,PATH_MAX);
    190 			action = RAIDFRAME_REBUILD_IN_PLACE;
    191 			num_options++;
    192 			break;
    193 		case 's':
    194 			action = RAIDFRAME_GET_INFO;
    195 			num_options++;
    196 			break;
    197 		case 'S':
    198 			action = RAIDFRAME_CHECK_RECON_STATUS;
    199 			num_options++;
    200 			break;
    201 		case 'p':
    202 			action = RAIDFRAME_CHECK_PARITY;
    203 			num_options++;
    204 			break;
    205 		case 'P':
    206 			action = RAIDFRAME_CHECK_PARITY;
    207 			do_rewrite = 1;
    208 			num_options++;
    209 			break;
    210 		case 'u':
    211 			action = RAIDFRAME_SHUTDOWN;
    212 			num_options++;
    213 			break;
    214 		case 'v':
    215 			verbose = 1;
    216 			/* Don't bump num_options, as '-v' is not
    217 			   an option like the others */
    218 			/* num_options++; */
    219 			break;
    220 		default:
    221 			usage();
    222 		}
    223 	argc -= optind;
    224 	argv += optind;
    225 
    226 	if ((num_options > 1) || (argc == NULL))
    227 		usage();
    228 
    229 	strncpy(name,argv[0],PATH_MAX);
    230 
    231 	if ((name[0] == '/') || (name[0] == '.')) {
    232 		/* they've (apparently) given a full path... */
    233 		strncpy(dev_name, name, PATH_MAX);
    234 	} else {
    235 		if (isdigit(name[strlen(name)-1])) {
    236 			rawpart = getrawpartition();
    237 			snprintf(dev_name,PATH_MAX,"/dev/%s%c",name,
    238 				 'a'+rawpart);
    239 		} else {
    240 			snprintf(dev_name,PATH_MAX,"/dev/%s",name);
    241 		}
    242 	}
    243 
    244 	if (stat(dev_name, &st) != 0) {
    245 		fprintf(stderr,"%s: stat failure on: %s\n",
    246 			__progname,dev_name);
    247 		return (errno);
    248 	}
    249 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) {
    250 		fprintf(stderr,"%s: invalid device: %s\n",
    251 			__progname,dev_name);
    252 		return (EINVAL);
    253 	}
    254 
    255 	raidID = RF_DEV2RAIDID(st.st_rdev);
    256 
    257 	if ((fd = open( dev_name, O_RDWR, 0640)) < 0) {
    258 		fprintf(stderr, "%s: unable to open device file: %s\n",
    259 			__progname, dev_name);
    260 		exit(1);
    261 	}
    262 
    263 
    264 	switch(action) {
    265 	case RAIDFRAME_ADD_HOT_SPARE:
    266 		add_hot_spare(fd, component);
    267 		break;
    268 	case RAIDFRAME_REMOVE_HOT_SPARE:
    269 		remove_hot_spare(fd, component);
    270 		break;
    271 	case RAIDFRAME_CONFIGURE:
    272 		rf_configure(fd, config_filename, force);
    273 		break;
    274 	case RAIDFRAME_SET_AUTOCONFIG:
    275 		set_autoconfig(fd, raidID, autoconf);
    276 		break;
    277 	case RAIDFRAME_COPYBACK:
    278 		printf("Copyback.\n");
    279 		do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
    280 		if (verbose) {
    281 			sleep(3); /* XXX give the copyback a chance to start */
    282 			printf("Copyback status:\n");
    283 			do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS);
    284 		}
    285 		break;
    286 	case RAIDFRAME_FAIL_DISK:
    287 		rf_fail_disk(fd, component, do_recon);
    288 		break;
    289 	case RAIDFRAME_SET_COMPONENT_LABEL:
    290 		set_component_label(fd, component);
    291 		break;
    292 	case RAIDFRAME_GET_COMPONENT_LABEL:
    293 		get_component_label(fd, component);
    294 		break;
    295 	case RAIDFRAME_INIT_LABELS:
    296 		init_component_labels(fd, serial_number);
    297 		break;
    298 	case RAIDFRAME_REWRITEPARITY:
    299 		printf("Initiating re-write of parity\n");
    300 		do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    301 			 "RAIDFRAME_REWRITEPARITY");
    302 		if (verbose) {
    303 			sleep(3); /* XXX give it time to get started */
    304 			printf("Parity Re-write status:\n");
    305 			do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
    306 		}
    307 		break;
    308 	case RAIDFRAME_CHECK_RECON_STATUS:
    309 		check_status(fd);
    310 		break;
    311 	case RAIDFRAME_GET_INFO:
    312 		rf_get_device_status(fd);
    313 		break;
    314 	case RAIDFRAME_REBUILD_IN_PLACE:
    315 		rebuild_in_place(fd, component);
    316 		break;
    317 	case RAIDFRAME_CHECK_PARITY:
    318 		check_parity(fd, do_rewrite, dev_name);
    319 		break;
    320 	case RAIDFRAME_SHUTDOWN:
    321 		do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
    322 		break;
    323 	default:
    324 		break;
    325 	}
    326 
    327 	close(fd);
    328 	exit(0);
    329 }
    330 
    331 void
    332 do_ioctl(fd, command, arg, ioctl_name)
    333 	int fd;
    334 	unsigned long command;
    335 	void *arg;
    336 	char *ioctl_name;
    337 {
    338 	if (ioctl(fd, command, arg) < 0) {
    339 		warn("ioctl (%s) failed", ioctl_name);
    340 		exit(1);
    341 	}
    342 }
    343 
    344 
    345 static void
    346 rf_configure(fd,config_file,force)
    347 	int fd;
    348 	char *config_file;
    349 	int force;
    350 {
    351 	void *generic;
    352 	RF_Config_t cfg;
    353 
    354 	if (rf_MakeConfig( config_file, &cfg ) != 0) {
    355 		fprintf(stderr,"%s: unable to create RAIDframe %s\n",
    356 			__progname, "configuration structure\n");
    357 		exit(1);
    358 	}
    359 
    360 	cfg.force = force;
    361 
    362 	/*
    363 
    364 	   Note the extra level of redirection needed here, since
    365 	   what we really want to pass in is a pointer to the pointer to
    366 	   the configuration structure.
    367 
    368 	 */
    369 
    370 	generic = (void *) &cfg;
    371 	do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE");
    372 }
    373 
    374 static char *
    375 device_status(status)
    376 	RF_DiskStatus_t status;
    377 {
    378 	static char status_string[256];
    379 
    380 	switch (status) {
    381 	case rf_ds_optimal:
    382 		strcpy(status_string,"optimal");
    383 		break;
    384 	case rf_ds_failed:
    385 		strcpy(status_string,"failed");
    386 		break;
    387 	case rf_ds_reconstructing:
    388 		strcpy(status_string,"reconstructing");
    389 		break;
    390 	case rf_ds_dist_spared:
    391 		strcpy(status_string,"dist_spared");
    392 		break;
    393 	case rf_ds_spared:
    394 		strcpy(status_string,"spared");
    395 		break;
    396 	case rf_ds_spare:
    397 		strcpy(status_string,"spare");
    398 		break;
    399 	case rf_ds_used_spare:
    400 		strcpy(status_string,"used_spare");
    401 		break;
    402 	default:
    403 		strcpy(status_string,"UNKNOWN");
    404 		break;
    405 	}
    406 	return(status_string);
    407 }
    408 
    409 static void
    410 rf_get_device_status(fd)
    411 	int fd;
    412 {
    413 	RF_DeviceConfig_t device_config;
    414 	void *cfg_ptr;
    415 	int is_clean;
    416 	int i;
    417 
    418 	cfg_ptr = &device_config;
    419 
    420 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
    421 
    422 	printf("Components:\n");
    423 	for(i=0; i < device_config.ndevs; i++) {
    424 		printf("%20s: %s\n", device_config.devs[i].devname,
    425 		       device_status(device_config.devs[i].status));
    426 	}
    427 	if (device_config.nspares > 0) {
    428 		printf("Spares:\n");
    429 		for(i=0; i < device_config.nspares; i++) {
    430 			printf("%20s: %s\n",
    431 			       device_config.spares[i].devname,
    432 			       device_status(device_config.spares[i].status));
    433 		}
    434 	} else {
    435 		printf("No spares.\n");
    436 	}
    437 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    438 		 "RAIDFRAME_CHECK_PARITY");
    439 	if (is_clean) {
    440 		printf("Parity status: clean\n");
    441 	} else {
    442 		printf("Parity status: DIRTY\n");
    443 	}
    444 	check_status(fd);
    445 }
    446 
    447 static void
    448 get_component_number(fd, component_name, component_number, num_columns)
    449 	int fd;
    450 	char *component_name;
    451 	int *component_number;
    452 	int *num_columns;
    453 {
    454 	RF_DeviceConfig_t device_config;
    455 	void *cfg_ptr;
    456 	int i;
    457 	int found;
    458 
    459 	*component_number = -1;
    460 
    461 	/* Assuming a full path spec... */
    462 	cfg_ptr = &device_config;
    463 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    464 		 "RAIDFRAME_GET_INFO");
    465 
    466 	*num_columns = device_config.cols;
    467 
    468 	found = 0;
    469 	for(i=0; i < device_config.ndevs; i++) {
    470 		if (strncmp(component_name, device_config.devs[i].devname,
    471 			    PATH_MAX)==0) {
    472 			found = 1;
    473 			*component_number = i;
    474 		}
    475 	}
    476 	if (!found) {
    477 		fprintf(stderr,"%s: %s is not a component %s", __progname,
    478 			component_name, "of this device\n");
    479 		exit(1);
    480 	}
    481 }
    482 
    483 static void
    484 rf_fail_disk(fd, component_to_fail, do_recon)
    485 	int fd;
    486 	char *component_to_fail;
    487 	int do_recon;
    488 {
    489 	struct rf_recon_req recon_request;
    490 	int component_num;
    491 	int num_cols;
    492 
    493 	get_component_number(fd, component_to_fail, &component_num, &num_cols);
    494 
    495 	recon_request.row = component_num / num_cols;
    496 	recon_request.col = component_num % num_cols;
    497 	if (do_recon) {
    498 		recon_request.flags = RF_FDFLAGS_RECON;
    499 	} else {
    500 		recon_request.flags = RF_FDFLAGS_NONE;
    501 	}
    502 	do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
    503 		 "RAIDFRAME_FAIL_DISK");
    504 	if (do_recon && verbose) {
    505 		printf("Reconstruction status:\n");
    506 		sleep(3); /* XXX give reconstruction a chance to start */
    507 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
    508 	}
    509 }
    510 
    511 static void
    512 get_component_label(fd, component)
    513 	int fd;
    514 	char *component;
    515 {
    516 	RF_ComponentLabel_t component_label;
    517 	void *label_ptr;
    518 	int component_num;
    519 	int num_cols;
    520 
    521 	get_component_number(fd, component, &component_num, &num_cols);
    522 
    523 	memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
    524 	component_label.row = component_num / num_cols;
    525 	component_label.column = component_num % num_cols;
    526 
    527 	label_ptr = &component_label;
    528 	do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
    529 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    530 
    531 	printf("Component label for %s:\n",component);
    532 
    533 	printf("   Row: %d Column: %d Num Rows: %d Num Columns: %d\n",
    534 	       component_label.row, component_label.column,
    535 	       component_label.num_rows, component_label.num_columns);
    536 	printf("   Version: %d Serial Number: %d Mod Counter: %d\n",
    537 	       component_label.version, component_label.serial_number,
    538 	       component_label.mod_counter);
    539 	printf("   Clean: %s Status: %d\n",
    540 	       component_label.clean ? "Yes" : "No",
    541 	       component_label.status );
    542 	printf("   sectPerSU: %d SUsPerPU: %d SUsPerRU: %d\n",
    543 	       component_label.sectPerSU, component_label.SUsPerPU,
    544 	       component_label.SUsPerRU);
    545 	printf("   RAID Level: %c  blocksize: %d numBlocks: %d\n",
    546 	       (char) component_label.parityConfig,
    547 	       component_label.blockSize, component_label.numBlocks);
    548 	printf("   Autoconfig: %s\n",
    549 	       component_label.autoconfigure ? "Yes" : "No" );
    550 	printf("   Last configured as: raid%d\n", component_label.last_unit );
    551 #if 0
    552 	printf("   Config order: %d\n", component_label.config_order);
    553 #endif
    554 #if 0
    555 	/* old version */
    556 	printf("Version: %d\n",component_label.version);
    557 	printf("Serial Number: %d\n",component_label.serial_number);
    558 	printf("Mod counter: %d\n",component_label.mod_counter);
    559 	printf("Row: %d\n", component_label.row);
    560 	printf("Column: %d\n", component_label.column);
    561 	printf("Num Rows: %d\n", component_label.num_rows);
    562 	printf("Num Columns: %d\n", component_label.num_columns);
    563 	printf("Clean: %d\n", component_label.clean);
    564 	printf("Status: %s\n", device_status(component_label.status));
    565 #endif
    566 }
    567 
    568 static void
    569 set_component_label(fd, component)
    570 	int fd;
    571 	char *component;
    572 {
    573 	RF_ComponentLabel_t component_label;
    574 	int component_num;
    575 	int num_cols;
    576 
    577 	get_component_number(fd, component, &component_num, &num_cols);
    578 
    579 	/* XXX This is currently here for testing, and future expandability */
    580 
    581 	component_label.version = 1;
    582 	component_label.serial_number = 123456;
    583 	component_label.mod_counter = 0;
    584 	component_label.row = component_num / num_cols;
    585 	component_label.column = component_num % num_cols;
    586 	component_label.num_rows = 0;
    587 	component_label.num_columns = 5;
    588 	component_label.clean = 0;
    589 	component_label.status = 1;
    590 
    591 	do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
    592 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    593 }
    594 
    595 
    596 static void
    597 init_component_labels(fd, serial_number)
    598 	int fd;
    599 	int serial_number;
    600 {
    601 	RF_ComponentLabel_t component_label;
    602 
    603 	component_label.version = 0;
    604 	component_label.serial_number = serial_number;
    605 	component_label.mod_counter = 0;
    606 	component_label.row = 0;
    607 	component_label.column = 0;
    608 	component_label.num_rows = 0;
    609 	component_label.num_columns = 0;
    610 	component_label.clean = 0;
    611 	component_label.status = 0;
    612 
    613 	do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
    614 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    615 }
    616 
    617 static void
    618 set_autoconfig(fd, raidID, autoconf)
    619 	int fd;
    620 	int raidID;
    621 	char *autoconf;
    622 {
    623 	int auto_config;
    624 	int root_config;
    625 
    626 	auto_config = 0;
    627 	root_config = 0;
    628 
    629 	if (strncasecmp(autoconf,"root", 4) == 0) {
    630 		root_config = 1;
    631 	}
    632 
    633 	if ((strncasecmp(autoconf,"yes", 3) == 0) ||
    634 	    root_config == 1) {
    635 		auto_config = 1;
    636 	}
    637 
    638 	do_ioctl(fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config,
    639 		 "RAIDFRAME_SET_AUTOCONFIG");
    640 
    641 	do_ioctl(fd, RAIDFRAME_SET_ROOT, &root_config,
    642 		 "RAIDFRAME_SET_ROOT");
    643 
    644 	printf("raid%d: Autoconfigure: %s\n", raidID,
    645 	       auto_config ? "Yes" : "No");
    646 
    647 	if (root_config == 1) {
    648 		printf("raid%d: Root: %s\n", raidID,
    649 		       auto_config ? "Yes" : "No");
    650 	}
    651 }
    652 
    653 static void
    654 add_hot_spare(fd, component)
    655 	int fd;
    656 	char *component;
    657 {
    658 	RF_SingleComponent_t hot_spare;
    659 
    660 	hot_spare.row = 0;
    661 	hot_spare.column = 0;
    662 	strncpy(hot_spare.component_name, component,
    663 		sizeof(hot_spare.component_name));
    664 
    665 	do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
    666 		  "RAIDFRAME_ADD_HOT_SPARE");
    667 }
    668 
    669 static void
    670 remove_hot_spare(fd, component)
    671 	int fd;
    672 	char *component;
    673 {
    674 	RF_SingleComponent_t hot_spare;
    675 	int component_num;
    676 	int num_cols;
    677 
    678 	get_component_number(fd, component, &component_num, &num_cols);
    679 
    680 	hot_spare.row = component_num / num_cols;
    681 	hot_spare.column = component_num % num_cols;
    682 
    683 	strncpy(hot_spare.component_name, component,
    684 		sizeof(hot_spare.component_name));
    685 
    686 	do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
    687 		  "RAIDFRAME_REMOVE_HOT_SPARE");
    688 }
    689 
    690 static void
    691 rebuild_in_place( fd, component )
    692 	int fd;
    693 	char *component;
    694 {
    695 	RF_SingleComponent_t comp;
    696 	int component_num;
    697 	int num_cols;
    698 
    699 	get_component_number(fd, component, &component_num, &num_cols);
    700 
    701 	comp.row = 0;
    702 	comp.column = component_num;
    703 	strncpy(comp.component_name, component, sizeof(comp.component_name));
    704 
    705 	do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
    706 		  "RAIDFRAME_REBUILD_IN_PLACE");
    707 
    708 	if (verbose) {
    709 		printf("Reconstruction status:\n");
    710 		sleep(3); /* XXX give reconstruction a chance to start */
    711 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
    712 	}
    713 
    714 }
    715 
    716 static void
    717 check_parity( fd, do_rewrite, dev_name )
    718 	int fd;
    719 	int do_rewrite;
    720 	char *dev_name;
    721 {
    722 	int is_clean;
    723 	int percent_done;
    724 
    725 	is_clean = 0;
    726 	percent_done = 0;
    727 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    728 		 "RAIDFRAME_CHECK_PARITY");
    729 	if (is_clean) {
    730 		printf("%s: Parity status: clean\n",dev_name);
    731 	} else {
    732 		printf("%s: Parity status: DIRTY\n",dev_name);
    733 		if (do_rewrite) {
    734 			printf("%s: Initiating re-write of parity\n",
    735 			       dev_name);
    736 			do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    737 				 "RAIDFRAME_REWRITEPARITY");
    738 			sleep(3); /* XXX give it time to
    739 				     get started. */
    740 			if (verbose) {
    741 				printf("Parity Re-write status:\n");
    742 				do_meter(fd,
    743 					 RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
    744 			} else {
    745 				do_ioctl(fd,
    746 					 RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    747 					 &percent_done,
    748 					 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"
    749 					 );
    750 				while( percent_done < 100 ) {
    751 					do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    752 						 &percent_done, "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    753 				}
    754 
    755 			}
    756 			       printf("%s: Parity Re-write complete\n",
    757 				      dev_name);
    758 		} else {
    759 			/* parity is wrong, and is not being fixed.
    760 			   Exit w/ an error. */
    761 			exit(1);
    762 		}
    763 	}
    764 }
    765 
    766 
    767 static void
    768 check_status( fd )
    769 	int fd;
    770 {
    771 	int recon_percent_done = 0;
    772 	int parity_percent_done = 0;
    773 	int copyback_percent_done = 0;
    774 
    775 	do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done,
    776 		 "RAIDFRAME_CHECK_RECON_STATUS");
    777 	printf("Reconstruction is %d%% complete.\n", recon_percent_done);
    778 	do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    779 		 &parity_percent_done,
    780 		 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    781 	printf("Parity Re-write is %d%% complete.\n", parity_percent_done);
    782 	do_ioctl(fd, RAIDFRAME_CHECK_COPYBACK_STATUS, &copyback_percent_done,
    783 		 "RAIDFRAME_CHECK_COPYBACK_STATUS");
    784 	printf("Copyback is %d%% complete.\n", copyback_percent_done);
    785 
    786 	/* These 3 should be mutually exclusive at this point */
    787 	if (recon_percent_done < 100) {
    788 		printf("Reconstruction status:\n");
    789 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
    790 	} else if (parity_percent_done < 100) {
    791 		printf("Parity Re-write status:\n");
    792 		do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
    793 	} else if (copyback_percent_done < 100) {
    794 		printf("Copyback status:\n");
    795 		do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS);
    796 	}
    797 }
    798 
    799 char *tbits = "|/-\\";
    800 
    801 static void
    802 do_meter( fd, option )
    803 	int fd;
    804 	int option;
    805 {
    806 	int percent_done;
    807 	int last_percent;
    808 	int start_percent;
    809 	struct timeval start_time;
    810 	struct timeval last_time;
    811 	struct timeval current_time;
    812 	double elapsed;
    813 	int elapsed_sec;
    814 	int elapsed_usec;
    815 	int simple_eta,last_eta;
    816 	double rate;
    817 	int amount;
    818 	int tbit_value;
    819 	int wait_for_more_data;
    820 	char buffer[1024];
    821 	char bar_buffer[1024];
    822 	char eta_buffer[1024];
    823 
    824 	if (gettimeofday(&start_time,NULL)) {
    825 		fprintf(stderr,"%s: gettimeofday failed!?!?\n",__progname);
    826 		exit(errno);
    827 	}
    828 	percent_done = 0;
    829 	do_ioctl( fd, option, &percent_done, "");
    830 	last_percent = percent_done;
    831 	start_percent = percent_done;
    832 	last_time = start_time;
    833 	current_time = start_time;
    834 
    835 	wait_for_more_data = 0;
    836 	tbit_value = 0;
    837 
    838 	while(percent_done < 100) {
    839 
    840 		get_bar(bar_buffer, percent_done, 40);
    841 
    842 		elapsed_sec = current_time.tv_sec - last_time.tv_sec;
    843 
    844 		elapsed_usec = current_time.tv_usec - last_time.tv_usec;
    845 
    846 		if (elapsed_usec < 0) {
    847 			elapsed_usec-=1000000;
    848 			elapsed_sec++;
    849 		}
    850 
    851 		elapsed = (double) elapsed_sec +
    852 			(double) elapsed_usec / 1000000.0;
    853 		if (elapsed <= 0.0) {
    854 			elapsed = 0.0001; /* XXX */
    855 		}
    856 
    857 		amount = percent_done - last_percent;
    858 		if (amount <= 0) { /* we don't do negatives (yet?) */
    859 			amount = 0;
    860 			wait_for_more_data = 1;
    861 		} else {
    862 			wait_for_more_data = 0;
    863 		}
    864 		rate = amount / elapsed;
    865 
    866 
    867 		if (rate > 0.0) {
    868 			simple_eta = (int) ((100.0 - (double) last_percent ) / rate);
    869 		} else {
    870 			simple_eta = -1;
    871 		}
    872 		if (simple_eta <=0) {
    873 			simple_eta = last_eta;
    874 		} else {
    875 			last_eta = simple_eta;
    876 		}
    877 
    878 		get_time_string(eta_buffer, simple_eta);
    879 
    880 		snprintf(buffer,1024,"\r%3d%% |%s| ETA: %s %c",
    881 			 percent_done,bar_buffer,eta_buffer,tbits[tbit_value]);
    882 
    883 		write(fileno(stdout),buffer,strlen(buffer));
    884 		fflush(stdout);
    885 
    886 		/* resolution wasn't high enough... wait until we get another
    887 		   timestamp and perhaps more "work" done. */
    888 
    889 		if (!wait_for_more_data) {
    890 			last_time = current_time;
    891 			last_percent = percent_done;
    892 		}
    893 
    894 		if (++tbit_value>3)
    895 			tbit_value = 0;
    896 
    897 		sleep(2);
    898 
    899 		if (gettimeofday(&current_time,NULL)) {
    900 			fprintf(stderr,"%s: gettimeofday failed!?!?\n",
    901 				__progname);
    902 			exit(errno);
    903 		}
    904 
    905 		do_ioctl( fd, option, &percent_done, "");
    906 
    907 
    908 	}
    909 	printf("\n");
    910 }
    911 /* 40 '*''s per line, then 40 ' ''s line. */
    912 /* If you've got a screen wider than 160 characters, "tough" */
    913 
    914 #define STAR_MIDPOINT 4*40
    915 const char stars[] = "****************************************"
    916                      "****************************************"
    917                      "****************************************"
    918                      "****************************************"
    919                      "                                        "
    920                      "                                        "
    921                      "                                        "
    922                      "                                        "
    923                      "                                        ";
    924 
    925 static void
    926 get_bar(string,percent,max_strlen)
    927 	char *string;
    928 	double percent;
    929 	int max_strlen;
    930 {
    931 	int offset;
    932 
    933 	if (max_strlen > STAR_MIDPOINT) {
    934 		max_strlen = STAR_MIDPOINT;
    935 	}
    936 	offset = STAR_MIDPOINT -
    937 		(int)((percent * max_strlen)/ 100);
    938 	if (offset < 0)
    939 		offset = 0;
    940 	snprintf(string,max_strlen,"%s",&stars[offset]);
    941 }
    942 
    943 static void
    944 get_time_string(string,simple_time)
    945 	char *string;
    946 	int simple_time;
    947 {
    948 	int minutes, seconds, hours;
    949 	char hours_buffer[5];
    950 	char minutes_buffer[5];
    951 	char seconds_buffer[5];
    952 
    953 	if (simple_time >= 0) {
    954 
    955 		minutes = (int) simple_time / 60;
    956 		seconds = ((int)simple_time - 60*minutes);
    957 		hours = minutes / 60;
    958 		minutes = minutes - 60*hours;
    959 
    960 		if (hours > 0) {
    961 			snprintf(hours_buffer,5,"%02d:",hours);
    962 		} else {
    963 			snprintf(hours_buffer,5,"   ");
    964 		}
    965 
    966 		snprintf(minutes_buffer,5,"%02d:",minutes);
    967 		snprintf(seconds_buffer,5,"%02d",seconds);
    968 		snprintf(string,1024,"%s%s%s",
    969 			 hours_buffer, minutes_buffer, seconds_buffer);
    970 	} else {
    971 		snprintf(string,1024,"   --:--");
    972 	}
    973 
    974 }
    975 
    976 static void
    977 usage()
    978 {
    979 	fprintf(stderr, "usage: %s [-v] -a component dev\n", __progname);
    980 	fprintf(stderr, "       %s [-v] -A yes | no | root dev\n", __progname);
    981 	fprintf(stderr, "       %s [-v] -B dev\n", __progname);
    982 	fprintf(stderr, "       %s [-v] -c config_file dev\n", __progname);
    983 	fprintf(stderr, "       %s [-v] -C config_file dev\n", __progname);
    984 	fprintf(stderr, "       %s [-v] -f component dev\n", __progname);
    985 	fprintf(stderr, "       %s [-v] -F component dev\n", __progname);
    986 	fprintf(stderr, "       %s [-v] -g component dev\n", __progname);
    987 	fprintf(stderr, "       %s [-v] -i dev\n", __progname);
    988 	fprintf(stderr, "       %s [-v] -I serial_number dev\n", __progname);
    989 	fprintf(stderr, "       %s [-v] -r component dev\n", __progname);
    990 	fprintf(stderr, "       %s [-v] -R component dev\n", __progname);
    991 	fprintf(stderr, "       %s [-v] -s dev\n", __progname);
    992 	fprintf(stderr, "       %s [-v] -S dev\n", __progname);
    993 	fprintf(stderr, "       %s [-v] -u dev\n", __progname);
    994 #if 0
    995 	fprintf(stderr, "usage: %s %s\n", __progname,
    996 		"-a | -f | -F | -g | -r | -R component dev");
    997 	fprintf(stderr, "       %s -B | -i | -s | -S -u dev\n", __progname);
    998 	fprintf(stderr, "       %s -c | -C config_file dev\n", __progname);
    999 	fprintf(stderr, "       %s -I serial_number dev\n", __progname);
   1000 #endif
   1001 	exit(1);
   1002 	/* NOTREACHED */
   1003 }
   1004