Home | History | Annotate | Line # | Download | only in raidctl
raidctl.c revision 1.15
      1 /*      $NetBSD: raidctl.c,v 1.15 2000/03/23 14:50:36 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,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,1);
    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 	for(i=0; i < device_config.ndevs; i++) {
    438 		if (device_config.devs[i].status == rf_ds_optimal) {
    439 			get_component_label(fd, device_config.devs[i].devname);
    440 		} else {
    441 			printf("%s status is: %s.  Skipping label.\n",
    442 			       device_config.devs[i].devname,
    443 			       device_status(device_config.devs[i].status));
    444 		}
    445 	}
    446 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    447 		 "RAIDFRAME_CHECK_PARITY");
    448 	if (is_clean) {
    449 		printf("Parity status: clean\n");
    450 	} else {
    451 		printf("Parity status: DIRTY\n");
    452 	}
    453 	check_status(fd,0);
    454 }
    455 
    456 static void
    457 get_component_number(fd, component_name, component_number, num_columns)
    458 	int fd;
    459 	char *component_name;
    460 	int *component_number;
    461 	int *num_columns;
    462 {
    463 	RF_DeviceConfig_t device_config;
    464 	void *cfg_ptr;
    465 	int i;
    466 	int found;
    467 
    468 	*component_number = -1;
    469 
    470 	/* Assuming a full path spec... */
    471 	cfg_ptr = &device_config;
    472 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    473 		 "RAIDFRAME_GET_INFO");
    474 
    475 	*num_columns = device_config.cols;
    476 
    477 	found = 0;
    478 	for(i=0; i < device_config.ndevs; i++) {
    479 		if (strncmp(component_name, device_config.devs[i].devname,
    480 			    PATH_MAX)==0) {
    481 			found = 1;
    482 			*component_number = i;
    483 		}
    484 	}
    485 	if (!found) {
    486 		fprintf(stderr,"%s: %s is not a component %s", __progname,
    487 			component_name, "of this device\n");
    488 		exit(1);
    489 	}
    490 }
    491 
    492 static void
    493 rf_fail_disk(fd, component_to_fail, do_recon)
    494 	int fd;
    495 	char *component_to_fail;
    496 	int do_recon;
    497 {
    498 	struct rf_recon_req recon_request;
    499 	int component_num;
    500 	int num_cols;
    501 
    502 	get_component_number(fd, component_to_fail, &component_num, &num_cols);
    503 
    504 	recon_request.row = component_num / num_cols;
    505 	recon_request.col = component_num % num_cols;
    506 	if (do_recon) {
    507 		recon_request.flags = RF_FDFLAGS_RECON;
    508 	} else {
    509 		recon_request.flags = RF_FDFLAGS_NONE;
    510 	}
    511 	do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
    512 		 "RAIDFRAME_FAIL_DISK");
    513 	if (do_recon && verbose) {
    514 		printf("Reconstruction status:\n");
    515 		sleep(3); /* XXX give reconstruction a chance to start */
    516 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
    517 	}
    518 }
    519 
    520 static void
    521 get_component_label(fd, component)
    522 	int fd;
    523 	char *component;
    524 {
    525 	RF_ComponentLabel_t component_label;
    526 	void *label_ptr;
    527 	int component_num;
    528 	int num_cols;
    529 
    530 	get_component_number(fd, component, &component_num, &num_cols);
    531 
    532 	memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
    533 	component_label.row = component_num / num_cols;
    534 	component_label.column = component_num % num_cols;
    535 
    536 	label_ptr = &component_label;
    537 	do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
    538 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    539 
    540 	printf("Component label for %s:\n",component);
    541 
    542 	printf("   Row: %d Column: %d Num Rows: %d Num Columns: %d\n",
    543 	       component_label.row, component_label.column,
    544 	       component_label.num_rows, component_label.num_columns);
    545 	printf("   Version: %d Serial Number: %d Mod Counter: %d\n",
    546 	       component_label.version, component_label.serial_number,
    547 	       component_label.mod_counter);
    548 	printf("   Clean: %s Status: %d\n",
    549 	       component_label.clean ? "Yes" : "No",
    550 	       component_label.status );
    551 	printf("   sectPerSU: %d SUsPerPU: %d SUsPerRU: %d\n",
    552 	       component_label.sectPerSU, component_label.SUsPerPU,
    553 	       component_label.SUsPerRU);
    554 	printf("   RAID Level: %c  blocksize: %d numBlocks: %d\n",
    555 	       (char) component_label.parityConfig,
    556 	       component_label.blockSize, component_label.numBlocks);
    557 	printf("   Autoconfig: %s\n",
    558 	       component_label.autoconfigure ? "Yes" : "No" );
    559 	printf("   Root partition: %s\n",
    560 	       component_label.root_partition ? "Yes" : "No" );
    561 	printf("   Last configured as: raid%d\n", component_label.last_unit );
    562 }
    563 
    564 static void
    565 set_component_label(fd, component)
    566 	int fd;
    567 	char *component;
    568 {
    569 	RF_ComponentLabel_t component_label;
    570 	int component_num;
    571 	int num_cols;
    572 
    573 	get_component_number(fd, component, &component_num, &num_cols);
    574 
    575 	/* XXX This is currently here for testing, and future expandability */
    576 
    577 	component_label.version = 1;
    578 	component_label.serial_number = 123456;
    579 	component_label.mod_counter = 0;
    580 	component_label.row = component_num / num_cols;
    581 	component_label.column = component_num % num_cols;
    582 	component_label.num_rows = 0;
    583 	component_label.num_columns = 5;
    584 	component_label.clean = 0;
    585 	component_label.status = 1;
    586 
    587 	do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
    588 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    589 }
    590 
    591 
    592 static void
    593 init_component_labels(fd, serial_number)
    594 	int fd;
    595 	int serial_number;
    596 {
    597 	RF_ComponentLabel_t component_label;
    598 
    599 	component_label.version = 0;
    600 	component_label.serial_number = serial_number;
    601 	component_label.mod_counter = 0;
    602 	component_label.row = 0;
    603 	component_label.column = 0;
    604 	component_label.num_rows = 0;
    605 	component_label.num_columns = 0;
    606 	component_label.clean = 0;
    607 	component_label.status = 0;
    608 
    609 	do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
    610 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    611 }
    612 
    613 static void
    614 set_autoconfig(fd, raidID, autoconf)
    615 	int fd;
    616 	int raidID;
    617 	char *autoconf;
    618 {
    619 	int auto_config;
    620 	int root_config;
    621 
    622 	auto_config = 0;
    623 	root_config = 0;
    624 
    625 	if (strncasecmp(autoconf,"root", 4) == 0) {
    626 		root_config = 1;
    627 	}
    628 
    629 	if ((strncasecmp(autoconf,"yes", 3) == 0) ||
    630 	    root_config == 1) {
    631 		auto_config = 1;
    632 	}
    633 
    634 	do_ioctl(fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config,
    635 		 "RAIDFRAME_SET_AUTOCONFIG");
    636 
    637 	do_ioctl(fd, RAIDFRAME_SET_ROOT, &root_config,
    638 		 "RAIDFRAME_SET_ROOT");
    639 
    640 	printf("raid%d: Autoconfigure: %s\n", raidID,
    641 	       auto_config ? "Yes" : "No");
    642 
    643 	if (root_config == 1) {
    644 		printf("raid%d: Root: %s\n", raidID,
    645 		       auto_config ? "Yes" : "No");
    646 	}
    647 }
    648 
    649 static void
    650 add_hot_spare(fd, component)
    651 	int fd;
    652 	char *component;
    653 {
    654 	RF_SingleComponent_t hot_spare;
    655 
    656 	hot_spare.row = 0;
    657 	hot_spare.column = 0;
    658 	strncpy(hot_spare.component_name, component,
    659 		sizeof(hot_spare.component_name));
    660 
    661 	do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
    662 		  "RAIDFRAME_ADD_HOT_SPARE");
    663 }
    664 
    665 static void
    666 remove_hot_spare(fd, component)
    667 	int fd;
    668 	char *component;
    669 {
    670 	RF_SingleComponent_t hot_spare;
    671 	int component_num;
    672 	int num_cols;
    673 
    674 	get_component_number(fd, component, &component_num, &num_cols);
    675 
    676 	hot_spare.row = component_num / num_cols;
    677 	hot_spare.column = component_num % num_cols;
    678 
    679 	strncpy(hot_spare.component_name, component,
    680 		sizeof(hot_spare.component_name));
    681 
    682 	do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
    683 		  "RAIDFRAME_REMOVE_HOT_SPARE");
    684 }
    685 
    686 static void
    687 rebuild_in_place( fd, component )
    688 	int fd;
    689 	char *component;
    690 {
    691 	RF_SingleComponent_t comp;
    692 	int component_num;
    693 	int num_cols;
    694 
    695 	get_component_number(fd, component, &component_num, &num_cols);
    696 
    697 	comp.row = 0;
    698 	comp.column = component_num;
    699 	strncpy(comp.component_name, component, sizeof(comp.component_name));
    700 
    701 	do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
    702 		  "RAIDFRAME_REBUILD_IN_PLACE");
    703 
    704 	if (verbose) {
    705 		printf("Reconstruction status:\n");
    706 		sleep(3); /* XXX give reconstruction a chance to start */
    707 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
    708 	}
    709 
    710 }
    711 
    712 static void
    713 check_parity( fd, do_rewrite, dev_name )
    714 	int fd;
    715 	int do_rewrite;
    716 	char *dev_name;
    717 {
    718 	int is_clean;
    719 	int percent_done;
    720 
    721 	is_clean = 0;
    722 	percent_done = 0;
    723 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    724 		 "RAIDFRAME_CHECK_PARITY");
    725 	if (is_clean) {
    726 		printf("%s: Parity status: clean\n",dev_name);
    727 	} else {
    728 		printf("%s: Parity status: DIRTY\n",dev_name);
    729 		if (do_rewrite) {
    730 			printf("%s: Initiating re-write of parity\n",
    731 			       dev_name);
    732 			do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    733 				 "RAIDFRAME_REWRITEPARITY");
    734 			sleep(3); /* XXX give it time to
    735 				     get started. */
    736 			if (verbose) {
    737 				printf("Parity Re-write status:\n");
    738 				do_meter(fd,
    739 					 RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
    740 			} else {
    741 				do_ioctl(fd,
    742 					 RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    743 					 &percent_done,
    744 					 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"
    745 					 );
    746 				while( percent_done < 100 ) {
    747 					do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    748 						 &percent_done, "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    749 				}
    750 
    751 			}
    752 			       printf("%s: Parity Re-write complete\n",
    753 				      dev_name);
    754 		} else {
    755 			/* parity is wrong, and is not being fixed.
    756 			   Exit w/ an error. */
    757 			exit(1);
    758 		}
    759 	}
    760 }
    761 
    762 
    763 static void
    764 check_status( fd, meter )
    765 	int fd;
    766 	int meter;
    767 {
    768 	int recon_percent_done = 0;
    769 	int parity_percent_done = 0;
    770 	int copyback_percent_done = 0;
    771 
    772 	do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done,
    773 		 "RAIDFRAME_CHECK_RECON_STATUS");
    774 	printf("Reconstruction is %d%% complete.\n", recon_percent_done);
    775 	do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    776 		 &parity_percent_done,
    777 		 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    778 	printf("Parity Re-write is %d%% complete.\n", parity_percent_done);
    779 	do_ioctl(fd, RAIDFRAME_CHECK_COPYBACK_STATUS, &copyback_percent_done,
    780 		 "RAIDFRAME_CHECK_COPYBACK_STATUS");
    781 	printf("Copyback is %d%% complete.\n", copyback_percent_done);
    782 
    783 	if (meter) {
    784 		/* These 3 should be mutually exclusive at this point */
    785 		if (recon_percent_done < 100) {
    786 			printf("Reconstruction status:\n");
    787 			do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
    788 		} else if (parity_percent_done < 100) {
    789 			printf("Parity Re-write status:\n");
    790 			do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
    791 		} else if (copyback_percent_done < 100) {
    792 			printf("Copyback status:\n");
    793 			do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS);
    794 		}
    795 	}
    796 }
    797 
    798 char *tbits = "|/-\\";
    799 
    800 static void
    801 do_meter( fd, option )
    802 	int fd;
    803 	int option;
    804 {
    805 	int percent_done;
    806 	int last_percent;
    807 	int start_percent;
    808 	struct timeval start_time;
    809 	struct timeval last_time;
    810 	struct timeval current_time;
    811 	double elapsed;
    812 	int elapsed_sec;
    813 	int elapsed_usec;
    814 	int simple_eta,last_eta;
    815 	double rate;
    816 	int amount;
    817 	int tbit_value;
    818 	int wait_for_more_data;
    819 	char buffer[1024];
    820 	char bar_buffer[1024];
    821 	char eta_buffer[1024];
    822 
    823 	if (gettimeofday(&start_time,NULL)) {
    824 		fprintf(stderr,"%s: gettimeofday failed!?!?\n",__progname);
    825 		exit(errno);
    826 	}
    827 	percent_done = 0;
    828 	do_ioctl( fd, option, &percent_done, "");
    829 	last_percent = percent_done;
    830 	start_percent = percent_done;
    831 	last_time = start_time;
    832 	current_time = start_time;
    833 
    834 	wait_for_more_data = 0;
    835 	tbit_value = 0;
    836 
    837 	while(percent_done < 100) {
    838 
    839 		get_bar(bar_buffer, percent_done, 40);
    840 
    841 		elapsed_sec = current_time.tv_sec - last_time.tv_sec;
    842 
    843 		elapsed_usec = current_time.tv_usec - last_time.tv_usec;
    844 
    845 		if (elapsed_usec < 0) {
    846 			elapsed_usec-=1000000;
    847 			elapsed_sec++;
    848 		}
    849 
    850 		elapsed = (double) elapsed_sec +
    851 			(double) elapsed_usec / 1000000.0;
    852 		if (elapsed <= 0.0) {
    853 			elapsed = 0.0001; /* XXX */
    854 		}
    855 
    856 		amount = percent_done - last_percent;
    857 		if (amount <= 0) { /* we don't do negatives (yet?) */
    858 			amount = 0;
    859 			wait_for_more_data = 1;
    860 		} else {
    861 			wait_for_more_data = 0;
    862 		}
    863 		rate = amount / elapsed;
    864 
    865 
    866 		if (rate > 0.0) {
    867 			simple_eta = (int) ((100.0 - (double) last_percent ) / rate);
    868 		} else {
    869 			simple_eta = -1;
    870 		}
    871 		if (simple_eta <=0) {
    872 			simple_eta = last_eta;
    873 		} else {
    874 			last_eta = simple_eta;
    875 		}
    876 
    877 		get_time_string(eta_buffer, simple_eta);
    878 
    879 		snprintf(buffer,1024,"\r%3d%% |%s| ETA: %s %c",
    880 			 percent_done,bar_buffer,eta_buffer,tbits[tbit_value]);
    881 
    882 		write(fileno(stdout),buffer,strlen(buffer));
    883 		fflush(stdout);
    884 
    885 		/* resolution wasn't high enough... wait until we get another
    886 		   timestamp and perhaps more "work" done. */
    887 
    888 		if (!wait_for_more_data) {
    889 			last_time = current_time;
    890 			last_percent = percent_done;
    891 		}
    892 
    893 		if (++tbit_value>3)
    894 			tbit_value = 0;
    895 
    896 		sleep(2);
    897 
    898 		if (gettimeofday(&current_time,NULL)) {
    899 			fprintf(stderr,"%s: gettimeofday failed!?!?\n",
    900 				__progname);
    901 			exit(errno);
    902 		}
    903 
    904 		do_ioctl( fd, option, &percent_done, "");
    905 
    906 
    907 	}
    908 	printf("\n");
    909 }
    910 /* 40 '*''s per line, then 40 ' ''s line. */
    911 /* If you've got a screen wider than 160 characters, "tough" */
    912 
    913 #define STAR_MIDPOINT 4*40
    914 const char stars[] = "****************************************"
    915                      "****************************************"
    916                      "****************************************"
    917                      "****************************************"
    918                      "                                        "
    919                      "                                        "
    920                      "                                        "
    921                      "                                        "
    922                      "                                        ";
    923 
    924 static void
    925 get_bar(string,percent,max_strlen)
    926 	char *string;
    927 	double percent;
    928 	int max_strlen;
    929 {
    930 	int offset;
    931 
    932 	if (max_strlen > STAR_MIDPOINT) {
    933 		max_strlen = STAR_MIDPOINT;
    934 	}
    935 	offset = STAR_MIDPOINT -
    936 		(int)((percent * max_strlen)/ 100);
    937 	if (offset < 0)
    938 		offset = 0;
    939 	snprintf(string,max_strlen,"%s",&stars[offset]);
    940 }
    941 
    942 static void
    943 get_time_string(string,simple_time)
    944 	char *string;
    945 	int simple_time;
    946 {
    947 	int minutes, seconds, hours;
    948 	char hours_buffer[5];
    949 	char minutes_buffer[5];
    950 	char seconds_buffer[5];
    951 
    952 	if (simple_time >= 0) {
    953 
    954 		minutes = (int) simple_time / 60;
    955 		seconds = ((int)simple_time - 60*minutes);
    956 		hours = minutes / 60;
    957 		minutes = minutes - 60*hours;
    958 
    959 		if (hours > 0) {
    960 			snprintf(hours_buffer,5,"%02d:",hours);
    961 		} else {
    962 			snprintf(hours_buffer,5,"   ");
    963 		}
    964 
    965 		snprintf(minutes_buffer,5,"%02d:",minutes);
    966 		snprintf(seconds_buffer,5,"%02d",seconds);
    967 		snprintf(string,1024,"%s%s%s",
    968 			 hours_buffer, minutes_buffer, seconds_buffer);
    969 	} else {
    970 		snprintf(string,1024,"   --:--");
    971 	}
    972 
    973 }
    974 
    975 static void
    976 usage()
    977 {
    978 	fprintf(stderr, "usage: %s [-v] -a component dev\n", __progname);
    979 	fprintf(stderr, "       %s [-v] -A yes | no | root dev\n", __progname);
    980 	fprintf(stderr, "       %s [-v] -B dev\n", __progname);
    981 	fprintf(stderr, "       %s [-v] -c config_file dev\n", __progname);
    982 	fprintf(stderr, "       %s [-v] -C config_file dev\n", __progname);
    983 	fprintf(stderr, "       %s [-v] -f component dev\n", __progname);
    984 	fprintf(stderr, "       %s [-v] -F component dev\n", __progname);
    985 	fprintf(stderr, "       %s [-v] -g component dev\n", __progname);
    986 	fprintf(stderr, "       %s [-v] -i dev\n", __progname);
    987 	fprintf(stderr, "       %s [-v] -I serial_number dev\n", __progname);
    988 	fprintf(stderr, "       %s [-v] -r component dev\n", __progname);
    989 	fprintf(stderr, "       %s [-v] -R component dev\n", __progname);
    990 	fprintf(stderr, "       %s [-v] -s dev\n", __progname);
    991 	fprintf(stderr, "       %s [-v] -S dev\n", __progname);
    992 	fprintf(stderr, "       %s [-v] -u dev\n", __progname);
    993 #if 0
    994 	fprintf(stderr, "usage: %s %s\n", __progname,
    995 		"-a | -f | -F | -g | -r | -R component dev");
    996 	fprintf(stderr, "       %s -B | -i | -s | -S -u dev\n", __progname);
    997 	fprintf(stderr, "       %s -c | -C config_file dev\n", __progname);
    998 	fprintf(stderr, "       %s -I serial_number dev\n", __progname);
    999 #endif
   1000 	exit(1);
   1001 	/* NOTREACHED */
   1002 }
   1003