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