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