Home | History | Annotate | Line # | Download | only in raidctl
raidctl.c revision 1.11
      1 /*      $NetBSD: raidctl.c,v 1.11 2000/01/09 03:06:35 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 add_hot_spare __P((int, char *));
     80 static  void remove_hot_spare __P((int, char *));
     81 static  void rebuild_in_place __P((int, char *));
     82 static  void check_status __P((int));
     83 static  void check_parity __P((int,int,char *));
     84 static  void do_meter __P((int, int));
     85 static  void get_bar __P((char *, double, int));
     86 static  void get_time_string __P((char *, int));
     87 
     88 int verbose = 0;
     89 
     90 int
     91 main(argc,argv)
     92 	int argc;
     93 	char *argv[];
     94 {
     95 	extern char *optarg;
     96 	extern int optind;
     97 	int ch;
     98 	int num_options;
     99 	unsigned long action;
    100 	char config_filename[PATH_MAX];
    101 	char dev_name[PATH_MAX];
    102 	char name[PATH_MAX];
    103 	char component[PATH_MAX];
    104 	int do_recon;
    105 	int do_rewrite;
    106 	int is_clean;
    107 	int raidID;
    108 	int rawpart;
    109 	int serial_number;
    110 	struct stat st;
    111 	int fd;
    112 	int force;
    113 
    114 	num_options = 0;
    115 	action = 0;
    116 	do_recon = 0;
    117 	do_rewrite = 0;
    118 	is_clean = 0;
    119 	force = 0;
    120 
    121 	while ((ch = getopt(argc, argv, "a:Bc:C:f:F:g:iI:l:r:R:sSpPuv")) != -1)
    122 		switch(ch) {
    123 		case 'a':
    124 			action = RAIDFRAME_ADD_HOT_SPARE;
    125 			strncpy(component, optarg, PATH_MAX);
    126 			num_options++;
    127 			break;
    128 		case 'B':
    129 			action = RAIDFRAME_COPYBACK;
    130 			num_options++;
    131 			break;
    132 		case 'c':
    133 			action = RAIDFRAME_CONFIGURE;
    134 			strncpy(config_filename,optarg,PATH_MAX);
    135 			force = 0;
    136 			num_options++;
    137 			break;
    138 		case 'C':
    139 			strncpy(config_filename,optarg,PATH_MAX);
    140 			action = RAIDFRAME_CONFIGURE;
    141 			force = 1;
    142 			num_options++;
    143 			break;
    144 		case 'f':
    145 			action = RAIDFRAME_FAIL_DISK;
    146 			strncpy(component, optarg, PATH_MAX);
    147 			do_recon = 0;
    148 			num_options++;
    149 			break;
    150 		case 'F':
    151 			action = RAIDFRAME_FAIL_DISK;
    152 			strncpy(component, optarg, PATH_MAX);
    153 			do_recon = 1;
    154 			num_options++;
    155 			break;
    156 		case 'g':
    157 			action = RAIDFRAME_GET_COMPONENT_LABEL;
    158 			strncpy(component, optarg, PATH_MAX);
    159 			num_options++;
    160 			break;
    161 		case 'i':
    162 			action = RAIDFRAME_REWRITEPARITY;
    163 			num_options++;
    164 			break;
    165 		case 'I':
    166 			action = RAIDFRAME_INIT_LABELS;
    167 			serial_number = atoi(optarg);
    168 			num_options++;
    169 			break;
    170 		case 'l':
    171 			action = RAIDFRAME_SET_COMPONENT_LABEL;
    172 			strncpy(component, optarg, PATH_MAX);
    173 			num_options++;
    174 			break;
    175 		case 'r':
    176 			action = RAIDFRAME_REMOVE_HOT_SPARE;
    177 			strncpy(component, optarg, PATH_MAX);
    178 			num_options++;
    179 			break;
    180 		case 'R':
    181 			strncpy(component,optarg,PATH_MAX);
    182 			action = RAIDFRAME_REBUILD_IN_PLACE;
    183 			num_options++;
    184 			break;
    185 		case 's':
    186 			action = RAIDFRAME_GET_INFO;
    187 			num_options++;
    188 			break;
    189 		case 'S':
    190 			action = RAIDFRAME_CHECK_RECON_STATUS;
    191 			num_options++;
    192 			break;
    193 		case 'p':
    194 			action = RAIDFRAME_CHECK_PARITY;
    195 			num_options++;
    196 			break;
    197 		case 'P':
    198 			action = RAIDFRAME_CHECK_PARITY;
    199 			do_rewrite = 1;
    200 			num_options++;
    201 			break;
    202 		case 'u':
    203 			action = RAIDFRAME_SHUTDOWN;
    204 			num_options++;
    205 			break;
    206 		case 'v':
    207 			verbose = 1;
    208 			/* Don't bump num_options, as '-v' is not
    209 			   an option like the others */
    210 			/* num_options++; */
    211 			break;
    212 		default:
    213 			usage();
    214 		}
    215 	argc -= optind;
    216 	argv += optind;
    217 
    218 	if ((num_options > 1) || (argc == NULL))
    219 		usage();
    220 
    221 	strncpy(name,argv[0],PATH_MAX);
    222 
    223 	if ((name[0] == '/') || (name[0] == '.')) {
    224 		/* they've (apparently) given a full path... */
    225 		strncpy(dev_name, name, PATH_MAX);
    226 	} else {
    227 		if (isdigit(name[strlen(name)-1])) {
    228 			rawpart = getrawpartition();
    229 			snprintf(dev_name,PATH_MAX,"/dev/%s%c",name,
    230 				 'a'+rawpart);
    231 		} else {
    232 			snprintf(dev_name,PATH_MAX,"/dev/%s",name);
    233 		}
    234 	}
    235 
    236 	if (stat(dev_name, &st) != 0) {
    237 		fprintf(stderr,"%s: stat failure on: %s\n",
    238 			__progname,dev_name);
    239 		return (errno);
    240 	}
    241 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) {
    242 		fprintf(stderr,"%s: invalid device: %s\n",
    243 			__progname,dev_name);
    244 		return (EINVAL);
    245 	}
    246 
    247 	raidID = RF_DEV2RAIDID(st.st_rdev);
    248 
    249 	if ((fd = open( dev_name, O_RDWR, 0640)) < 0) {
    250 		fprintf(stderr, "%s: unable to open device file: %s\n",
    251 			__progname, dev_name);
    252 		exit(1);
    253 	}
    254 
    255 
    256 	switch(action) {
    257 	case RAIDFRAME_ADD_HOT_SPARE:
    258 		add_hot_spare(fd,component);
    259 		break;
    260 	case RAIDFRAME_REMOVE_HOT_SPARE:
    261 		remove_hot_spare(fd,component);
    262 		break;
    263 	case RAIDFRAME_CONFIGURE:
    264 		rf_configure(fd, config_filename,force);
    265 		break;
    266 	case RAIDFRAME_COPYBACK:
    267 		printf("Copyback.\n");
    268 		do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
    269 		if (verbose) {
    270 			sleep(3); /* XXX give the copyback a chance to start */
    271 			printf("Copyback status:\n");
    272 			do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS);
    273 		}
    274 		break;
    275 	case RAIDFRAME_FAIL_DISK:
    276 		rf_fail_disk(fd,component,do_recon);
    277 		break;
    278 	case RAIDFRAME_SET_COMPONENT_LABEL:
    279 		set_component_label(fd,component);
    280 		break;
    281 	case RAIDFRAME_GET_COMPONENT_LABEL:
    282 		get_component_label(fd,component);
    283 		break;
    284 	case RAIDFRAME_INIT_LABELS:
    285 		init_component_labels(fd,serial_number);
    286 		break;
    287 	case RAIDFRAME_REWRITEPARITY:
    288 		printf("Initiating re-write of parity\n");
    289 		do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    290 			 "RAIDFRAME_REWRITEPARITY");
    291 		if (verbose) {
    292 			sleep(3); /* XXX give it time to get started */
    293 			printf("Parity Re-write status:\n");
    294 			do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
    295 		}
    296 		break;
    297 	case RAIDFRAME_CHECK_RECON_STATUS:
    298 		check_status(fd);
    299 		break;
    300 	case RAIDFRAME_GET_INFO:
    301 		rf_get_device_status(fd);
    302 		break;
    303 	case RAIDFRAME_REBUILD_IN_PLACE:
    304 		rebuild_in_place(fd,component);
    305 		break;
    306 	case RAIDFRAME_CHECK_PARITY:
    307 		check_parity(fd,do_rewrite,dev_name);
    308 		break;
    309 	case RAIDFRAME_SHUTDOWN:
    310 		do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
    311 		break;
    312 	default:
    313 		break;
    314 	}
    315 
    316 	close(fd);
    317 	exit(0);
    318 }
    319 
    320 void
    321 do_ioctl(fd, command, arg, ioctl_name)
    322 	int fd;
    323 	unsigned long command;
    324 	void *arg;
    325 	char *ioctl_name;
    326 {
    327 	if (ioctl(fd, command, arg) < 0) {
    328 		warn("ioctl (%s) failed", ioctl_name);
    329 		exit(1);
    330 	}
    331 }
    332 
    333 
    334 static void
    335 rf_configure(fd,config_file,force)
    336 	int fd;
    337 	char *config_file;
    338 	int force;
    339 {
    340 	void *generic;
    341 	RF_Config_t cfg;
    342 
    343 	if (rf_MakeConfig( config_file, &cfg ) != 0) {
    344 		fprintf(stderr,"%s: unable to create RAIDframe %s\n",
    345 			__progname, "configuration structure\n");
    346 		exit(1);
    347 	}
    348 
    349 	cfg.force = force;
    350 
    351 	/*
    352 
    353 	   Note the extra level of redirection needed here, since
    354 	   what we really want to pass in is a pointer to the pointer to
    355 	   the configuration structure.
    356 
    357 	 */
    358 
    359 	generic = (void *) &cfg;
    360 	do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE");
    361 }
    362 
    363 static char *
    364 device_status(status)
    365 	RF_DiskStatus_t status;
    366 {
    367 	static char status_string[256];
    368 
    369 	switch (status) {
    370 	case rf_ds_optimal:
    371 		strcpy(status_string,"optimal");
    372 		break;
    373 	case rf_ds_failed:
    374 		strcpy(status_string,"failed");
    375 		break;
    376 	case rf_ds_reconstructing:
    377 		strcpy(status_string,"reconstructing");
    378 		break;
    379 	case rf_ds_dist_spared:
    380 		strcpy(status_string,"dist_spared");
    381 		break;
    382 	case rf_ds_spared:
    383 		strcpy(status_string,"spared");
    384 		break;
    385 	case rf_ds_spare:
    386 		strcpy(status_string,"spare");
    387 		break;
    388 	case rf_ds_used_spare:
    389 		strcpy(status_string,"used_spare");
    390 		break;
    391 	default:
    392 		strcpy(status_string,"UNKNOWN");
    393 		break;
    394 	}
    395 	return(status_string);
    396 }
    397 
    398 static void
    399 rf_get_device_status(fd)
    400 	int fd;
    401 {
    402 	RF_DeviceConfig_t device_config;
    403 	void *cfg_ptr;
    404 	int is_clean;
    405 	int i;
    406 
    407 	cfg_ptr = &device_config;
    408 
    409 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
    410 
    411 	printf("Components:\n");
    412 	for(i=0; i < device_config.ndevs; i++) {
    413 		printf("%20s: %s\n", device_config.devs[i].devname,
    414 		       device_status(device_config.devs[i].status));
    415 	}
    416 	if (device_config.nspares > 0) {
    417 		printf("Spares:\n");
    418 		for(i=0; i < device_config.nspares; i++) {
    419 			printf("%20s: %s\n",
    420 			       device_config.spares[i].devname,
    421 			       device_status(device_config.spares[i].status));
    422 		}
    423 	} else {
    424 		printf("No spares.\n");
    425 	}
    426 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    427 		 "RAIDFRAME_CHECK_PARITY");
    428 	if (is_clean) {
    429 		printf("Parity status: clean\n");
    430 	} else {
    431 		printf("Parity status: DIRTY\n");
    432 	}
    433 	check_status(fd);
    434 }
    435 
    436 static void
    437 get_component_number(fd, component_name, component_number, num_columns)
    438 	int fd;
    439 	char *component_name;
    440 	int *component_number;
    441 	int *num_columns;
    442 {
    443 	RF_DeviceConfig_t device_config;
    444 	void *cfg_ptr;
    445 	int i;
    446 	int found;
    447 
    448 	*component_number = -1;
    449 
    450 	/* Assuming a full path spec... */
    451 	cfg_ptr = &device_config;
    452 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    453 		 "RAIDFRAME_GET_INFO");
    454 
    455 	*num_columns = device_config.cols;
    456 
    457 	found = 0;
    458 	for(i=0; i < device_config.ndevs; i++) {
    459 		if (strncmp(component_name, device_config.devs[i].devname,
    460 			    PATH_MAX)==0) {
    461 			found = 1;
    462 			*component_number = i;
    463 		}
    464 	}
    465 	if (!found) {
    466 		fprintf(stderr,"%s: %s is not a component %s", __progname,
    467 			component_name, "of this device\n");
    468 		exit(1);
    469 	}
    470 }
    471 
    472 static void
    473 rf_fail_disk(fd, component_to_fail, do_recon)
    474 	int fd;
    475 	char *component_to_fail;
    476 	int do_recon;
    477 {
    478 	struct rf_recon_req recon_request;
    479 	int component_num;
    480 	int num_cols;
    481 
    482 	get_component_number(fd, component_to_fail, &component_num, &num_cols);
    483 
    484 	recon_request.row = component_num / num_cols;
    485 	recon_request.col = component_num % num_cols;
    486 	if (do_recon) {
    487 		recon_request.flags = RF_FDFLAGS_RECON;
    488 	} else {
    489 		recon_request.flags = RF_FDFLAGS_NONE;
    490 	}
    491 	do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
    492 		 "RAIDFRAME_FAIL_DISK");
    493 	if (do_recon && verbose) {
    494 		printf("Reconstruction status:\n");
    495 		sleep(3); /* XXX give reconstruction a chance to start */
    496 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
    497 	}
    498 }
    499 
    500 static void
    501 get_component_label(fd, component)
    502 	int fd;
    503 	char *component;
    504 {
    505 	RF_ComponentLabel_t component_label;
    506 	void *label_ptr;
    507 	int component_num;
    508 	int num_cols;
    509 
    510 	get_component_number(fd, component, &component_num, &num_cols);
    511 
    512 	memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
    513 	component_label.row = component_num / num_cols;
    514 	component_label.column = component_num % num_cols;
    515 
    516 	label_ptr = &component_label;
    517 	do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
    518 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    519 
    520 	printf("Component label for %s:\n",component);
    521 	printf("Version: %d\n",component_label.version);
    522 	printf("Serial Number: %d\n",component_label.serial_number);
    523 	printf("Mod counter: %d\n",component_label.mod_counter);
    524 	printf("Row: %d\n", component_label.row);
    525 	printf("Column: %d\n", component_label.column);
    526 	printf("Num Rows: %d\n", component_label.num_rows);
    527 	printf("Num Columns: %d\n", component_label.num_columns);
    528 	printf("Clean: %d\n", component_label.clean);
    529 	printf("Status: %s\n", device_status(component_label.status));
    530 }
    531 
    532 static void
    533 set_component_label(fd, component)
    534 	int fd;
    535 	char *component;
    536 {
    537 	RF_ComponentLabel_t component_label;
    538 	int component_num;
    539 	int num_cols;
    540 
    541 	get_component_number(fd, component, &component_num, &num_cols);
    542 
    543 	/* XXX This is currently here for testing, and future expandability */
    544 
    545 	component_label.version = 1;
    546 	component_label.serial_number = 123456;
    547 	component_label.mod_counter = 0;
    548 	component_label.row = component_num / num_cols;
    549 	component_label.column = component_num % num_cols;
    550 	component_label.num_rows = 0;
    551 	component_label.num_columns = 5;
    552 	component_label.clean = 0;
    553 	component_label.status = 1;
    554 
    555 	do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
    556 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    557 }
    558 
    559 
    560 static void
    561 init_component_labels(fd, serial_number)
    562 	int fd;
    563 	int serial_number;
    564 {
    565 	RF_ComponentLabel_t component_label;
    566 
    567 	component_label.version = 0;
    568 	component_label.serial_number = serial_number;
    569 	component_label.mod_counter = 0;
    570 	component_label.row = 0;
    571 	component_label.column = 0;
    572 	component_label.num_rows = 0;
    573 	component_label.num_columns = 0;
    574 	component_label.clean = 0;
    575 	component_label.status = 0;
    576 
    577 	do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
    578 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    579 }
    580 
    581 static void
    582 add_hot_spare(fd, component)
    583 	int fd;
    584 	char *component;
    585 {
    586 	RF_SingleComponent_t hot_spare;
    587 
    588 	hot_spare.row = 0;
    589 	hot_spare.column = 0;
    590 	strncpy(hot_spare.component_name, component,
    591 		sizeof(hot_spare.component_name));
    592 
    593 	do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
    594 		  "RAIDFRAME_ADD_HOT_SPARE");
    595 }
    596 
    597 static void
    598 remove_hot_spare(fd, component)
    599 	int fd;
    600 	char *component;
    601 {
    602 	RF_SingleComponent_t hot_spare;
    603 	int component_num;
    604 	int num_cols;
    605 
    606 	get_component_number(fd, component, &component_num, &num_cols);
    607 
    608 	hot_spare.row = component_num / num_cols;
    609 	hot_spare.column = component_num % num_cols;
    610 
    611 	strncpy(hot_spare.component_name, component,
    612 		sizeof(hot_spare.component_name));
    613 
    614 	do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
    615 		  "RAIDFRAME_REMOVE_HOT_SPARE");
    616 }
    617 
    618 static void
    619 rebuild_in_place( fd, component )
    620 	int fd;
    621 	char *component;
    622 {
    623 	RF_SingleComponent_t comp;
    624 	int component_num;
    625 	int num_cols;
    626 
    627 	get_component_number(fd, component, &component_num, &num_cols);
    628 
    629 	comp.row = 0;
    630 	comp.column = component_num;
    631 	strncpy(comp.component_name, component, sizeof(comp.component_name));
    632 
    633 	do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
    634 		  "RAIDFRAME_REBUILD_IN_PLACE");
    635 
    636 	if (verbose) {
    637 		printf("Reconstruction status:\n");
    638 		sleep(3); /* XXX give reconstruction a chance to start */
    639 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
    640 	}
    641 
    642 }
    643 
    644 static void
    645 check_parity( fd, do_rewrite, dev_name )
    646 	int fd;
    647 	int do_rewrite;
    648 	char *dev_name;
    649 {
    650 	int is_clean;
    651 	int percent_done;
    652 
    653 	is_clean = 0;
    654 	percent_done = 0;
    655 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    656 		 "RAIDFRAME_CHECK_PARITY");
    657 	if (is_clean) {
    658 		printf("%s: Parity status: clean\n",dev_name);
    659 	} else {
    660 		printf("%s: Parity status: DIRTY\n",dev_name);
    661 		if (do_rewrite) {
    662 			printf("%s: Initiating re-write of parity\n",
    663 			       dev_name);
    664 			do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    665 				 "RAIDFRAME_REWRITEPARITY");
    666 			sleep(3); /* XXX give it time to
    667 				     get started. */
    668 			if (verbose) {
    669 				printf("Parity Re-write status:\n");
    670 				do_meter(fd,
    671 					 RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
    672 			} else {
    673 				do_ioctl(fd,
    674 					 RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    675 					 &percent_done,
    676 					 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"
    677 					 );
    678 				while( percent_done < 100 ) {
    679 					do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    680 						 &percent_done, "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    681 				}
    682 
    683 			}
    684 			       printf("%s: Parity Re-write complete\n",
    685 				      dev_name);
    686 		} else {
    687 			/* parity is wrong, and is not being fixed.
    688 			   Exit w/ an error. */
    689 			exit(1);
    690 		}
    691 	}
    692 }
    693 
    694 
    695 static void
    696 check_status( fd )
    697 	int fd;
    698 {
    699 	int recon_percent_done = 0;
    700 	int parity_percent_done = 0;
    701 	int copyback_percent_done = 0;
    702 
    703 	do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done,
    704 		 "RAIDFRAME_CHECK_RECON_STATUS");
    705 	printf("Reconstruction is %d%% complete.\n", recon_percent_done);
    706 	do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    707 		 &parity_percent_done,
    708 		 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    709 	printf("Parity Re-write is %d%% complete.\n", parity_percent_done);
    710 	do_ioctl(fd, RAIDFRAME_CHECK_COPYBACK_STATUS, &copyback_percent_done,
    711 		 "RAIDFRAME_CHECK_COPYBACK_STATUS");
    712 	printf("Copyback is %d%% complete.\n", copyback_percent_done);
    713 
    714 	/* These 3 should be mutually exclusive at this point */
    715 	if (recon_percent_done < 100) {
    716 		printf("Reconstruction status:\n");
    717 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
    718 	} else if (parity_percent_done < 100) {
    719 		printf("Parity Re-write status:\n");
    720 		do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
    721 	} else if (copyback_percent_done < 100) {
    722 		printf("Copyback status:\n");
    723 		do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS);
    724 	}
    725 }
    726 
    727 char *tbits = "|/-\\";
    728 
    729 static void
    730 do_meter( fd, option )
    731 	int fd;
    732 	int option;
    733 {
    734 	int percent_done;
    735 	int last_percent;
    736 	int start_percent;
    737 	struct timeval start_time;
    738 	struct timeval last_time;
    739 	struct timeval current_time;
    740 	double elapsed;
    741 	int elapsed_sec;
    742 	int elapsed_usec;
    743 	int simple_eta,last_eta;
    744 	double rate;
    745 	int amount;
    746 	int tbit_value;
    747 	int wait_for_more_data;
    748 	char buffer[1024];
    749 	char bar_buffer[1024];
    750 	char eta_buffer[1024];
    751 
    752 	if (gettimeofday(&start_time,NULL)) {
    753 		fprintf(stderr,"%s: gettimeofday failed!?!?\n",__progname);
    754 		exit(errno);
    755 	}
    756 	percent_done = 0;
    757 	do_ioctl( fd, option, &percent_done, "");
    758 	last_percent = percent_done;
    759 	start_percent = percent_done;
    760 	last_time = start_time;
    761 	current_time = start_time;
    762 
    763 	wait_for_more_data = 0;
    764 	tbit_value = 0;
    765 
    766 	while(percent_done < 100) {
    767 
    768 		get_bar(bar_buffer, percent_done, 40);
    769 
    770 		elapsed_sec = current_time.tv_sec - last_time.tv_sec;
    771 
    772 		elapsed_usec = current_time.tv_usec - last_time.tv_usec;
    773 
    774 		if (elapsed_usec < 0) {
    775 			elapsed_usec-=1000000;
    776 			elapsed_sec++;
    777 		}
    778 
    779 		elapsed = (double) elapsed_sec +
    780 			(double) elapsed_usec / 1000000.0;
    781 		if (elapsed <= 0.0) {
    782 			elapsed = 0.0001; /* XXX */
    783 		}
    784 
    785 		amount = percent_done - last_percent;
    786 		if (amount <= 0) { /* we don't do negatives (yet?) */
    787 			amount = 0;
    788 			wait_for_more_data = 1;
    789 		} else {
    790 			wait_for_more_data = 0;
    791 		}
    792 		rate = amount / elapsed;
    793 
    794 
    795 		if (rate > 0.0) {
    796 			simple_eta = (int) ((100.0 - (double) last_percent ) / rate);
    797 		} else {
    798 			simple_eta = -1;
    799 		}
    800 		if (simple_eta <=0) {
    801 			simple_eta = last_eta;
    802 		} else {
    803 			last_eta = simple_eta;
    804 		}
    805 
    806 		get_time_string(eta_buffer, simple_eta);
    807 
    808 		snprintf(buffer,1024,"\r%3d%% |%s| ETA: %s %c",
    809 			 percent_done,bar_buffer,eta_buffer,tbits[tbit_value]);
    810 
    811 		write(fileno(stdout),buffer,strlen(buffer));
    812 		fflush(stdout);
    813 
    814 		/* resolution wasn't high enough... wait until we get another
    815 		   timestamp and perhaps more "work" done. */
    816 
    817 		if (!wait_for_more_data) {
    818 			last_time = current_time;
    819 			last_percent = percent_done;
    820 		}
    821 
    822 		if (++tbit_value>3)
    823 			tbit_value = 0;
    824 
    825 		sleep(2);
    826 
    827 		if (gettimeofday(&current_time,NULL)) {
    828 			fprintf(stderr,"%s: gettimeofday failed!?!?\n",
    829 				__progname);
    830 			exit(errno);
    831 		}
    832 
    833 		do_ioctl( fd, option, &percent_done, "");
    834 
    835 
    836 	}
    837 	printf("\n");
    838 }
    839 /* 40 '*''s per line, then 40 ' ''s line. */
    840 /* If you've got a screen wider than 160 characters, "tough" */
    841 
    842 #define STAR_MIDPOINT 4*40
    843 const char stars[] = "****************************************"
    844                      "****************************************"
    845                      "****************************************"
    846                      "****************************************"
    847                      "                                        "
    848                      "                                        "
    849                      "                                        "
    850                      "                                        "
    851                      "                                        ";
    852 
    853 static void
    854 get_bar(string,percent,max_strlen)
    855 	char *string;
    856 	double percent;
    857 	int max_strlen;
    858 {
    859 	int offset;
    860 
    861 	if (max_strlen > STAR_MIDPOINT) {
    862 		max_strlen = STAR_MIDPOINT;
    863 	}
    864 	offset = STAR_MIDPOINT -
    865 		(int)((percent * max_strlen)/ 100);
    866 	if (offset < 0)
    867 		offset = 0;
    868 	snprintf(string,max_strlen,"%s",&stars[offset]);
    869 }
    870 
    871 static void
    872 get_time_string(string,simple_time)
    873 	char *string;
    874 	int simple_time;
    875 {
    876 	int minutes, seconds, hours;
    877 	char hours_buffer[5];
    878 	char minutes_buffer[5];
    879 	char seconds_buffer[5];
    880 
    881 	if (simple_time >= 0) {
    882 
    883 		minutes = (int) simple_time / 60;
    884 		seconds = ((int)simple_time - 60*minutes);
    885 		hours = minutes / 60;
    886 		minutes = minutes - 60*hours;
    887 
    888 		if (hours > 0) {
    889 			snprintf(hours_buffer,5,"%02d:",hours);
    890 		} else {
    891 			snprintf(hours_buffer,5,"   ");
    892 		}
    893 
    894 		snprintf(minutes_buffer,5,"%02d:",minutes);
    895 		snprintf(seconds_buffer,5,"%02d",seconds);
    896 		snprintf(string,1024,"%s%s%s",
    897 			 hours_buffer, minutes_buffer, seconds_buffer);
    898 	} else {
    899 		snprintf(string,1024,"   --:--");
    900 	}
    901 
    902 }
    903 
    904 static void
    905 usage()
    906 {
    907 	fprintf(stderr, "usage: %s [-v] -a component dev\n", __progname);
    908 	fprintf(stderr, "       %s [-v] -B dev\n", __progname);
    909 	fprintf(stderr, "       %s [-v] -c config_file dev\n", __progname);
    910 	fprintf(stderr, "       %s [-v] -C config_file dev\n", __progname);
    911 	fprintf(stderr, "       %s [-v] -f component dev\n", __progname);
    912 	fprintf(stderr, "       %s [-v] -F component dev\n", __progname);
    913 	fprintf(stderr, "       %s [-v] -g component dev\n", __progname);
    914 	fprintf(stderr, "       %s [-v] -i dev\n", __progname);
    915 	fprintf(stderr, "       %s [-v] -I serial_number dev\n", __progname);
    916 	fprintf(stderr, "       %s [-v] -r component dev\n", __progname);
    917 	fprintf(stderr, "       %s [-v] -R component dev\n", __progname);
    918 	fprintf(stderr, "       %s [-v] -s dev\n", __progname);
    919 	fprintf(stderr, "       %s [-v] -S dev\n", __progname);
    920 	fprintf(stderr, "       %s [-v] -u dev\n", __progname);
    921 #if 0
    922 	fprintf(stderr, "usage: %s %s\n", __progname,
    923 		"-a | -f | -F | -g | -r | -R component dev");
    924 	fprintf(stderr, "       %s -B | -i | -s | -S -u dev\n", __progname);
    925 	fprintf(stderr, "       %s -c | -C config_file dev\n", __progname);
    926 	fprintf(stderr, "       %s -I serial_number dev\n", __progname);
    927 #endif
    928 	exit(1);
    929 	/* NOTREACHED */
    930 }
    931