Home | History | Annotate | Line # | Download | only in raidctl
raidctl.c revision 1.49
      1 /*      $NetBSD: raidctl.c,v 1.49 2010/11/08 12:42:35 pooka 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * This program is a re-write of the original rf_ctrl program
     34  * distributed by CMU with RAIDframe 1.1.
     35  *
     36  * This program is the user-land interface to the RAIDframe kernel
     37  * driver in NetBSD.
     38  */
     39 #include <sys/cdefs.h>
     40 
     41 #ifndef lint
     42 __RCSID("$NetBSD: raidctl.c,v 1.49 2010/11/08 12:42:35 pooka Exp $");
     43 #endif
     44 
     45 
     46 #include <sys/param.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/stat.h>
     49 #include <sys/disklabel.h>
     50 
     51 #include <ctype.h>
     52 #include <err.h>
     53 #include <errno.h>
     54 #include <fcntl.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 #include <util.h>
     60 
     61 #ifdef RUMP_ACTION
     62 #include <rump/rump.h>
     63 #include <rump/rumpclient.h>
     64 #include <rump/rump_syscalls.h>
     65 #endif
     66 
     67 #include <dev/raidframe/raidframevar.h>
     68 #include <dev/raidframe/raidframeio.h>
     69 #include "rf_configure.h"
     70 
     71 void	do_ioctl(int, u_long, void *, const char *);
     72 static  void rf_configure(int, char*, int);
     73 static  const char *device_status(RF_DiskStatus_t);
     74 static  void rf_get_device_status(int);
     75 static	void rf_output_configuration(int, const char *);
     76 static  void get_component_number(int, char *, int *, int *);
     77 static  void rf_fail_disk(int, char *, int);
     78 static  void usage(void);
     79 static  void get_component_label(int, char *);
     80 static  void set_component_label(int, char *);
     81 static  void init_component_labels(int, int);
     82 static  void set_autoconfig(int, int, char *);
     83 static  void add_hot_spare(int, char *);
     84 static  void remove_hot_spare(int, char *);
     85 static  void rebuild_in_place(int, char *);
     86 static  void check_status(int,int);
     87 static  void check_parity(int,int, char *);
     88 static  void do_meter(int, u_long);
     89 static  void get_bar(char *, double, int);
     90 static  void get_time_string(char *, int);
     91 static  void rf_output_pmstat(int, int);
     92 static  void rf_pm_configure(int, int, char *, int[]);
     93 
     94 int verbose;
     95 
     96 int
     97 main(int argc,char *argv[])
     98 {
     99 	int ch, i;
    100 	int num_options;
    101 	unsigned long action;
    102 	char config_filename[PATH_MAX];
    103 	char dev_name[PATH_MAX];
    104 	char name[PATH_MAX];
    105 	char component[PATH_MAX];
    106 	char autoconf[10];
    107 	char *parityconf = NULL;
    108 	int parityparams[3];
    109 	int do_output;
    110 	int do_recon;
    111 	int do_rewrite;
    112 	int is_clean;
    113 	int raidID;
    114 	int serial_number;
    115 	struct stat st;
    116 	int fd;
    117 	int force;
    118 	int openmode;
    119 
    120 	num_options = 0;
    121 	action = 0;
    122 	do_output = 0;
    123 	do_recon = 0;
    124 	do_rewrite = 0;
    125 	is_clean = 0;
    126 	serial_number = 0;
    127 	force = 0;
    128 	openmode = O_RDWR;	/* default to read/write */
    129 
    130 #ifdef RUMP_ACTION
    131 	if (rumpclient_init() == -1)
    132 		err(1, "rump client init failed");
    133 #endif
    134 
    135 	while ((ch = getopt(argc, argv, "a:A:Bc:C:f:F:g:GiI:l:mM:r:R:sSpPuv"))
    136 	       != -1)
    137 		switch(ch) {
    138 		case 'a':
    139 			action = RAIDFRAME_ADD_HOT_SPARE;
    140 			strlcpy(component, optarg, sizeof(component));
    141 			num_options++;
    142 			break;
    143 		case 'A':
    144 			action = RAIDFRAME_SET_AUTOCONFIG;
    145 			strlcpy(autoconf, optarg, sizeof(autoconf));
    146 			num_options++;
    147 			break;
    148 		case 'B':
    149 			action = RAIDFRAME_COPYBACK;
    150 			num_options++;
    151 			break;
    152 		case 'c':
    153 			action = RAIDFRAME_CONFIGURE;
    154 			strlcpy(config_filename, optarg,
    155 			    sizeof(config_filename));
    156 			force = 0;
    157 			num_options++;
    158 			break;
    159 		case 'C':
    160 			strlcpy(config_filename, optarg,
    161 			    sizeof(config_filename));
    162 			action = RAIDFRAME_CONFIGURE;
    163 			force = 1;
    164 			num_options++;
    165 			break;
    166 		case 'f':
    167 			action = RAIDFRAME_FAIL_DISK;
    168 			strlcpy(component, optarg, sizeof(component));
    169 			do_recon = 0;
    170 			num_options++;
    171 			break;
    172 		case 'F':
    173 			action = RAIDFRAME_FAIL_DISK;
    174 			strlcpy(component, optarg, sizeof(component));
    175 			do_recon = 1;
    176 			num_options++;
    177 			break;
    178 		case 'g':
    179 			action = RAIDFRAME_GET_COMPONENT_LABEL;
    180 			strlcpy(component, optarg, sizeof(component));
    181 			openmode = O_RDONLY;
    182 			num_options++;
    183 			break;
    184 		case 'G':
    185 			action = RAIDFRAME_GET_INFO;
    186 			openmode = O_RDONLY;
    187 			do_output = 1;
    188 			num_options++;
    189 			break;
    190 		case 'i':
    191 			action = RAIDFRAME_REWRITEPARITY;
    192 			num_options++;
    193 			break;
    194 		case 'I':
    195 			action = RAIDFRAME_INIT_LABELS;
    196 			serial_number = atoi(optarg);
    197 			num_options++;
    198 			break;
    199 		case 'm':
    200 			action = RAIDFRAME_PARITYMAP_STATUS;
    201 			openmode = O_RDONLY;
    202 			num_options++;
    203 			break;
    204 		case 'M':
    205 			action = RAIDFRAME_PARITYMAP_SET_DISABLE;
    206 			parityconf = strdup(optarg);
    207 			num_options++;
    208 			/* XXXjld: should rf_pm_configure do the atoi()s? */
    209 			i = 0;
    210 			while (i < 3 && optind < argc &&
    211 			    isdigit((int)argv[optind][0]))
    212 				parityparams[i++] = atoi(argv[optind++]);
    213 			while (i < 3)
    214 				parityparams[i++] = 0;
    215 			break;
    216 		case 'l':
    217 			action = RAIDFRAME_SET_COMPONENT_LABEL;
    218 			strlcpy(component, optarg, sizeof(component));
    219 			num_options++;
    220 			break;
    221 		case 'r':
    222 			action = RAIDFRAME_REMOVE_HOT_SPARE;
    223 			strlcpy(component, optarg, sizeof(component));
    224 			num_options++;
    225 			break;
    226 		case 'R':
    227 			strlcpy(component, optarg, sizeof(component));
    228 			action = RAIDFRAME_REBUILD_IN_PLACE;
    229 			num_options++;
    230 			break;
    231 		case 's':
    232 			action = RAIDFRAME_GET_INFO;
    233 			openmode = O_RDONLY;
    234 			num_options++;
    235 			break;
    236 		case 'S':
    237 			action = RAIDFRAME_CHECK_RECON_STATUS_EXT;
    238 			openmode = O_RDONLY;
    239 			num_options++;
    240 			break;
    241 		case 'p':
    242 			action = RAIDFRAME_CHECK_PARITY;
    243 			openmode = O_RDONLY;
    244 			num_options++;
    245 			break;
    246 		case 'P':
    247 			action = RAIDFRAME_CHECK_PARITY;
    248 			do_rewrite = 1;
    249 			num_options++;
    250 			break;
    251 		case 'u':
    252 			action = RAIDFRAME_SHUTDOWN;
    253 			num_options++;
    254 			break;
    255 		case 'v':
    256 			verbose = 1;
    257 			/* Don't bump num_options, as '-v' is not
    258 			   an option like the others */
    259 			/* num_options++; */
    260 			break;
    261 		default:
    262 			usage();
    263 		}
    264 	argc -= optind;
    265 	argv += optind;
    266 
    267 	if ((num_options > 1) || (argc == 0))
    268 		usage();
    269 
    270 	strlcpy(name, argv[0], sizeof(name));
    271 #ifdef RUMP_ACTION
    272 	fd = opendisk1(name, openmode, dev_name, sizeof(dev_name), 0,
    273 	    rump_sys_open);
    274 #else
    275 	fd = opendisk(name, openmode, dev_name, sizeof(dev_name), 0);
    276 #endif
    277 	if (fd == -1)
    278 		err(1, "Unable to open device file: %s", name);
    279 	if (fstat(fd, &st) == -1)
    280 		err(1, "stat failure on: %s", dev_name);
    281 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    282 		err(1, "invalid device: %s", dev_name);
    283 
    284 	raidID = DISKUNIT(st.st_rdev);
    285 
    286 	switch(action) {
    287 	case RAIDFRAME_ADD_HOT_SPARE:
    288 		add_hot_spare(fd, component);
    289 		break;
    290 	case RAIDFRAME_REMOVE_HOT_SPARE:
    291 		remove_hot_spare(fd, component);
    292 		break;
    293 	case RAIDFRAME_CONFIGURE:
    294 		rf_configure(fd, config_filename, force);
    295 		break;
    296 	case RAIDFRAME_SET_AUTOCONFIG:
    297 		set_autoconfig(fd, raidID, autoconf);
    298 		break;
    299 	case RAIDFRAME_COPYBACK:
    300 		printf("Copyback.\n");
    301 		do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
    302 		if (verbose) {
    303 			sleep(3); /* XXX give the copyback a chance to start */
    304 			printf("Copyback status:\n");
    305 			do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS_EXT);
    306 		}
    307 		break;
    308 	case RAIDFRAME_FAIL_DISK:
    309 		rf_fail_disk(fd, component, do_recon);
    310 		break;
    311 	case RAIDFRAME_SET_COMPONENT_LABEL:
    312 		set_component_label(fd, component);
    313 		break;
    314 	case RAIDFRAME_GET_COMPONENT_LABEL:
    315 		get_component_label(fd, component);
    316 		break;
    317 	case RAIDFRAME_INIT_LABELS:
    318 		init_component_labels(fd, serial_number);
    319 		break;
    320 	case RAIDFRAME_REWRITEPARITY:
    321 		printf("Initiating re-write of parity\n");
    322 		do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    323 			 "RAIDFRAME_REWRITEPARITY");
    324 		if (verbose) {
    325 			sleep(3); /* XXX give it time to get started */
    326 			printf("Parity Re-write status:\n");
    327 			do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
    328 		}
    329 		break;
    330 	case RAIDFRAME_CHECK_RECON_STATUS_EXT:
    331 		check_status(fd,1);
    332 		break;
    333 	case RAIDFRAME_GET_INFO:
    334 		if (do_output)
    335 			rf_output_configuration(fd, dev_name);
    336 		else
    337 			rf_get_device_status(fd);
    338 		break;
    339 	case RAIDFRAME_PARITYMAP_STATUS:
    340 		rf_output_pmstat(fd, raidID);
    341 		break;
    342 	case RAIDFRAME_PARITYMAP_SET_DISABLE:
    343 		rf_pm_configure(fd, raidID, parityconf, parityparams);
    344 		break;
    345 	case RAIDFRAME_REBUILD_IN_PLACE:
    346 		rebuild_in_place(fd, component);
    347 		break;
    348 	case RAIDFRAME_CHECK_PARITY:
    349 		check_parity(fd, do_rewrite, dev_name);
    350 		break;
    351 	case RAIDFRAME_SHUTDOWN:
    352 		do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
    353 		break;
    354 	default:
    355 		break;
    356 	}
    357 
    358 	close(fd);
    359 	exit(0);
    360 }
    361 
    362 void
    363 do_ioctl(int fd, unsigned long command, void *arg, const char *ioctl_name)
    364 {
    365 	if (ioctl(fd, command, arg) == -1)
    366 		err(1, "ioctl (%s) failed", ioctl_name);
    367 }
    368 
    369 
    370 static void
    371 rf_configure(int fd, char *config_file, int force)
    372 {
    373 	void *generic;
    374 	RF_Config_t cfg;
    375 
    376 	if (rf_MakeConfig( config_file, &cfg ) != 0)
    377 		err(1, "Unable to create RAIDframe configuration structure");
    378 
    379 	cfg.force = force;
    380 
    381 	/*
    382 	 * Note the extra level of redirection needed here, since
    383 	 * what we really want to pass in is a pointer to the pointer to
    384 	 * the configuration structure.
    385 	 */
    386 
    387 	generic = &cfg;
    388 	do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE");
    389 }
    390 
    391 static const char *
    392 device_status(RF_DiskStatus_t status)
    393 {
    394 
    395 	switch (status) {
    396 	case rf_ds_optimal:
    397 		return ("optimal");
    398 		break;
    399 	case rf_ds_failed:
    400 		return ("failed");
    401 		break;
    402 	case rf_ds_reconstructing:
    403 		return ("reconstructing");
    404 		break;
    405 	case rf_ds_dist_spared:
    406 		return ("dist_spared");
    407 		break;
    408 	case rf_ds_spared:
    409 		return ("spared");
    410 		break;
    411 	case rf_ds_spare:
    412 		return ("spare");
    413 		break;
    414 	case rf_ds_used_spare:
    415 		return ("used_spare");
    416 		break;
    417 	default:
    418 		return ("UNKNOWN");
    419 	}
    420 	/* NOTREACHED */
    421 }
    422 
    423 static void
    424 rf_get_device_status(int fd)
    425 {
    426 	RF_DeviceConfig_t device_config;
    427 	void *cfg_ptr;
    428 	int is_clean;
    429 	int i;
    430 
    431 	cfg_ptr = &device_config;
    432 
    433 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
    434 
    435 	printf("Components:\n");
    436 	for(i=0; i < device_config.ndevs; i++) {
    437 		printf("%20s: %s\n", device_config.devs[i].devname,
    438 		       device_status(device_config.devs[i].status));
    439 	}
    440 	if (device_config.nspares > 0) {
    441 		printf("Spares:\n");
    442 		for(i=0; i < device_config.nspares; i++) {
    443 			printf("%20s: %s\n",
    444 			       device_config.spares[i].devname,
    445 			       device_status(device_config.spares[i].status));
    446 		}
    447 	} else {
    448 		printf("No spares.\n");
    449 	}
    450 	for(i=0; i < device_config.ndevs; i++) {
    451 		if (device_config.devs[i].status == rf_ds_optimal) {
    452 			get_component_label(fd, device_config.devs[i].devname);
    453 		} else {
    454 			printf("%s status is: %s.  Skipping label.\n",
    455 			       device_config.devs[i].devname,
    456 			       device_status(device_config.devs[i].status));
    457 		}
    458 	}
    459 
    460 	if (device_config.nspares > 0) {
    461 		for(i=0; i < device_config.nspares; i++) {
    462 			if ((device_config.spares[i].status ==
    463 			     rf_ds_optimal) ||
    464 			    (device_config.spares[i].status ==
    465 			     rf_ds_used_spare)) {
    466 				get_component_label(fd,
    467 					    device_config.spares[i].devname);
    468 			} else {
    469 				printf("%s status is: %s.  Skipping label.\n",
    470 				       device_config.spares[i].devname,
    471 				       device_status(device_config.spares[i].status));
    472 			}
    473 		}
    474 	}
    475 
    476 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    477 		 "RAIDFRAME_CHECK_PARITY");
    478 	if (is_clean) {
    479 		printf("Parity status: clean\n");
    480 	} else {
    481 		printf("Parity status: DIRTY\n");
    482 	}
    483 	check_status(fd,0);
    484 }
    485 
    486 static void
    487 rf_output_pmstat(int fd, int raidID)
    488 {
    489 	char srs[7];
    490 	unsigned int i, j;
    491 	int dis, dr;
    492 	struct rf_pmstat st;
    493 
    494 	if (ioctl(fd, RAIDFRAME_PARITYMAP_STATUS, &st) == -1) {
    495 		if (errno == EINVAL) {
    496 			printf("raid%d: has no parity; parity map disabled\n",
    497 				raidID);
    498 			return;
    499 		}
    500 		err(1, "ioctl (%s) failed", "RAIDFRAME_PARITYMAP_STATUS");
    501 	}
    502 
    503 	if (st.enabled) {
    504 		if (0 > humanize_number(srs, 7, st.region_size * DEV_BSIZE,
    505 			"B", HN_AUTOSCALE, HN_NOSPACE))
    506 			strlcpy(srs, "???", 7);
    507 
    508 		printf("raid%d: parity map enabled with %u regions of %s\n",
    509 		    raidID, st.params.regions, srs);
    510 		printf("raid%d: regions marked clean after %d intervals of"
    511 		    " %d.%03ds\n", raidID, st.params.cooldown,
    512 		    st.params.tickms / 1000, st.params.tickms % 1000);
    513 		printf("raid%d: write/sync/clean counters "
    514 		    "%"PRIu64"/%"PRIu64"/%"PRIu64"\n", raidID,
    515 		    st.ctrs.nwrite, st.ctrs.ncachesync, st.ctrs.nclearing);
    516 
    517 		dr = 0;
    518 		for (i = 0; i < st.params.regions; i++)
    519 			if (isset(st.dirty, i))
    520 				dr++;
    521 		printf("raid%d: %d dirty region%s\n", raidID, dr,
    522 		    dr == 1 ? "" : "s");
    523 
    524 		if (verbose > 0) {
    525 			for (i = 0; i < RF_PARITYMAP_NBYTE; i += 32) {
    526 				printf("    ");
    527 				for (j = i; j < RF_PARITYMAP_NBYTE
    528 					 && j < i + 32; j++)
    529 					printf("%x%x", st.dirty[j] & 15,
    530 					    (st.dirty[j] >> 4) & 15);
    531 				printf("\n");
    532 			}
    533 		}
    534 	} else {
    535 		printf("raid%d: parity map disabled\n", raidID);
    536 	}
    537 
    538 	do_ioctl(fd, RAIDFRAME_PARITYMAP_GET_DISABLE, &dis,
    539 	    "RAIDFRAME_PARITYMAP_GET_DISABLE");
    540 	printf("raid%d: parity map will %s %sabled on next configure\n",
    541 	    raidID, dis == st.enabled ? "be" : "remain", dis ? "dis" : "en");
    542 }
    543 
    544 static void
    545 rf_pm_configure(int fd, int raidID, char *parityconf, int parityparams[])
    546 {
    547 	int dis;
    548 	struct rf_pmparams params;
    549 
    550 	if (strcasecmp(parityconf, "yes") == 0)
    551 		dis = 0;
    552 	else if (strcasecmp(parityconf, "no") == 0)
    553 		dis = 1;
    554 	else if (strcasecmp(parityconf, "set") == 0) {
    555 		params.cooldown = parityparams[0];
    556 		params.tickms = parityparams[1];
    557 		params.regions = parityparams[2];
    558 
    559 		do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_PARAMS, &params,
    560 		    "RAIDFRAME_PARITYMAP_SET_PARAMS");
    561 
    562 		if (params.cooldown != 0 || params.tickms != 0) {
    563 			printf("raid%d: parity cleaned after", raidID);
    564 			if (params.cooldown != 0)
    565 				printf(" %d", params.cooldown);
    566 			printf(" intervals");
    567 			if (params.tickms != 0) {
    568 				printf(" of %d.%03ds", params.tickms / 1000,
    569 				    params.tickms % 1000);
    570 			}
    571 			printf("\n");
    572 		}
    573 		if (params.regions != 0)
    574 			printf("raid%d: will use %d regions on next"
    575 			    " configuration\n", raidID, params.regions);
    576 
    577 		return;
    578 		/* XXX the control flow here could be prettier. */
    579 	} else
    580 		err(1, "`%s' is not a valid parity map command", parityconf);
    581 
    582 	do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_DISABLE, &dis,
    583 	    "RAIDFRAME_PARITYMAP_SET_DISABLE");
    584 	printf("raid%d: parity map will be %sabled on next configure\n",
    585 	    raidID, dis ? "dis" : "en");
    586 }
    587 
    588 
    589 static void
    590 rf_output_configuration(int fd, const char *name)
    591 {
    592 	RF_DeviceConfig_t device_config;
    593 	void *cfg_ptr;
    594 	int i;
    595 	RF_ComponentLabel_t component_label;
    596 	void *label_ptr;
    597 	int component_num;
    598 	int num_cols;
    599 
    600 	cfg_ptr = &device_config;
    601 
    602 	printf("# raidctl config file for %s\n", name);
    603 	printf("\n");
    604 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
    605 
    606 	printf("START array\n");
    607 	printf("# numRow numCol numSpare\n");
    608 	printf("%d %d %d\n", device_config.rows, device_config.cols,
    609 	    device_config.nspares);
    610 	printf("\n");
    611 
    612 	printf("START disks\n");
    613 	for(i=0; i < device_config.ndevs; i++)
    614 		printf("%s\n", device_config.devs[i].devname);
    615 	printf("\n");
    616 
    617 	if (device_config.nspares > 0) {
    618 		printf("START spare\n");
    619 		for(i=0; i < device_config.nspares; i++)
    620 			printf("%s\n", device_config.spares[i].devname);
    621 		printf("\n");
    622 	}
    623 
    624 	for(i=0; i < device_config.ndevs; i++) {
    625 		if (device_config.devs[i].status == rf_ds_optimal)
    626 			break;
    627 	}
    628 	if (i == device_config.ndevs) {
    629 		printf("# WARNING: no optimal components; using %s\n",
    630 		    device_config.devs[0].devname);
    631 		i = 0;
    632 	}
    633 	get_component_number(fd, device_config.devs[i].devname,
    634 	    &component_num, &num_cols);
    635 	memset(&component_label, 0, sizeof(RF_ComponentLabel_t));
    636 	component_label.row = component_num / num_cols;
    637 	component_label.column = component_num % num_cols;
    638 	label_ptr = &component_label;
    639 	do_ioctl(fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
    640 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    641 
    642 	printf("START layout\n");
    643 	printf(
    644 	    "# sectPerSU SUsPerParityUnit SUsPerReconUnit RAID_level_%c\n",
    645 	    (char) component_label.parityConfig);
    646 	printf("%d %d %d %c\n",
    647 	    component_label.sectPerSU, component_label.SUsPerPU,
    648 	    component_label.SUsPerRU, (char) component_label.parityConfig);
    649 	printf("\n");
    650 
    651 	printf("START queue\n");
    652 	printf("fifo %d\n", device_config.maxqdepth);
    653 }
    654 
    655 static void
    656 get_component_number(int fd, char *component_name, int *component_number,
    657 		     int *num_columns)
    658 {
    659 	RF_DeviceConfig_t device_config;
    660 	void *cfg_ptr;
    661 	int i;
    662 	int found;
    663 
    664 	*component_number = -1;
    665 
    666 	/* Assuming a full path spec... */
    667 	cfg_ptr = &device_config;
    668 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    669 		 "RAIDFRAME_GET_INFO");
    670 
    671 	*num_columns = device_config.cols;
    672 
    673 	found = 0;
    674 	for(i=0; i < device_config.ndevs; i++) {
    675 		if (strncmp(component_name, device_config.devs[i].devname,
    676 			    PATH_MAX)==0) {
    677 			found = 1;
    678 			*component_number = i;
    679 		}
    680 	}
    681 	if (!found) { /* maybe it's a spare? */
    682 		for(i=0; i < device_config.nspares; i++) {
    683 			if (strncmp(component_name,
    684 				    device_config.spares[i].devname,
    685 				    PATH_MAX)==0) {
    686 				found = 1;
    687 				*component_number = i + device_config.ndevs;
    688 				/* the way spares are done should
    689 				   really change... */
    690 				*num_columns = device_config.cols +
    691 					device_config.nspares;
    692 			}
    693 		}
    694 	}
    695 
    696 	if (!found)
    697 		err(1,"%s is not a component of this device", component_name);
    698 }
    699 
    700 static void
    701 rf_fail_disk(int fd, char *component_to_fail, int do_recon)
    702 {
    703 	struct rf_recon_req recon_request;
    704 	int component_num;
    705 	int num_cols;
    706 
    707 	get_component_number(fd, component_to_fail, &component_num, &num_cols);
    708 
    709 	recon_request.row = component_num / num_cols;
    710 	recon_request.col = component_num % num_cols;
    711 	if (do_recon) {
    712 		recon_request.flags = RF_FDFLAGS_RECON;
    713 	} else {
    714 		recon_request.flags = RF_FDFLAGS_NONE;
    715 	}
    716 	do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
    717 		 "RAIDFRAME_FAIL_DISK");
    718 	if (do_recon && verbose) {
    719 		printf("Reconstruction status:\n");
    720 		sleep(3); /* XXX give reconstruction a chance to start */
    721 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
    722 	}
    723 }
    724 
    725 static void
    726 get_component_label(int fd, char *component)
    727 {
    728 	RF_ComponentLabel_t component_label;
    729 	void *label_ptr;
    730 	int component_num;
    731 	int num_cols;
    732 
    733 	get_component_number(fd, component, &component_num, &num_cols);
    734 
    735 	memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
    736 	component_label.row = component_num / num_cols;
    737 	component_label.column = component_num % num_cols;
    738 
    739 	label_ptr = &component_label;
    740 	do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
    741 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    742 
    743 	printf("Component label for %s:\n",component);
    744 
    745 	printf("   Row: %d, Column: %d, Num Rows: %d, Num Columns: %d\n",
    746 	       component_label.row, component_label.column,
    747 	       component_label.num_rows, component_label.num_columns);
    748 	printf("   Version: %d, Serial Number: %d, Mod Counter: %d\n",
    749 	       component_label.version, component_label.serial_number,
    750 	       component_label.mod_counter);
    751 	printf("   Clean: %s, Status: %d\n",
    752 	       component_label.clean ? "Yes" : "No",
    753 	       component_label.status );
    754 	printf("   sectPerSU: %d, SUsPerPU: %d, SUsPerRU: %d\n",
    755 	       component_label.sectPerSU, component_label.SUsPerPU,
    756 	       component_label.SUsPerRU);
    757 	printf("   Queue size: %d, blocksize: %d, numBlocks: %u\n",
    758 	       component_label.maxOutstanding, component_label.blockSize,
    759 	       component_label.numBlocks);
    760 	printf("   RAID Level: %c\n", (char) component_label.parityConfig);
    761 	printf("   Autoconfig: %s\n",
    762 	       component_label.autoconfigure ? "Yes" : "No" );
    763 	printf("   Root partition: %s\n",
    764 	       component_label.root_partition ? "Yes" : "No" );
    765 	printf("   Last configured as: raid%d\n", component_label.last_unit );
    766 }
    767 
    768 static void
    769 set_component_label(int fd, char *component)
    770 {
    771 	RF_ComponentLabel_t component_label;
    772 	int component_num;
    773 	int num_cols;
    774 
    775 	get_component_number(fd, component, &component_num, &num_cols);
    776 
    777 	/* XXX This is currently here for testing, and future expandability */
    778 
    779 	component_label.version = 1;
    780 	component_label.serial_number = 123456;
    781 	component_label.mod_counter = 0;
    782 	component_label.row = component_num / num_cols;
    783 	component_label.column = component_num % num_cols;
    784 	component_label.num_rows = 0;
    785 	component_label.num_columns = 5;
    786 	component_label.clean = 0;
    787 	component_label.status = 1;
    788 
    789 	do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
    790 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    791 }
    792 
    793 
    794 static void
    795 init_component_labels(int fd, int serial_number)
    796 {
    797 	RF_ComponentLabel_t component_label;
    798 
    799 	component_label.version = 0;
    800 	component_label.serial_number = serial_number;
    801 	component_label.mod_counter = 0;
    802 	component_label.row = 0;
    803 	component_label.column = 0;
    804 	component_label.num_rows = 0;
    805 	component_label.num_columns = 0;
    806 	component_label.clean = 0;
    807 	component_label.status = 0;
    808 
    809 	do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
    810 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    811 }
    812 
    813 static void
    814 set_autoconfig(int fd, int raidID, char *autoconf)
    815 {
    816 	int auto_config;
    817 	int root_config;
    818 
    819 	auto_config = 0;
    820 	root_config = 0;
    821 
    822 	if (strncasecmp(autoconf,"root", 4) == 0) {
    823 		root_config = 1;
    824 	}
    825 
    826 	if ((strncasecmp(autoconf,"yes", 3) == 0) ||
    827 	    root_config == 1) {
    828 		auto_config = 1;
    829 	}
    830 
    831 	do_ioctl(fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config,
    832 		 "RAIDFRAME_SET_AUTOCONFIG");
    833 
    834 	do_ioctl(fd, RAIDFRAME_SET_ROOT, &root_config,
    835 		 "RAIDFRAME_SET_ROOT");
    836 
    837 	printf("raid%d: Autoconfigure: %s\n", raidID,
    838 	       auto_config ? "Yes" : "No");
    839 
    840 	if (root_config == 1) {
    841 		printf("raid%d: Root: %s\n", raidID,
    842 		       auto_config ? "Yes" : "No");
    843 	}
    844 }
    845 
    846 static void
    847 add_hot_spare(int fd, char *component)
    848 {
    849 	RF_SingleComponent_t hot_spare;
    850 
    851 	hot_spare.row = 0;
    852 	hot_spare.column = 0;
    853 	strncpy(hot_spare.component_name, component,
    854 		sizeof(hot_spare.component_name));
    855 
    856 	do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
    857 		  "RAIDFRAME_ADD_HOT_SPARE");
    858 }
    859 
    860 static void
    861 remove_hot_spare(int fd, char *component)
    862 {
    863 	RF_SingleComponent_t hot_spare;
    864 	int component_num;
    865 	int num_cols;
    866 
    867 	get_component_number(fd, component, &component_num, &num_cols);
    868 
    869 	hot_spare.row = component_num / num_cols;
    870 	hot_spare.column = component_num % num_cols;
    871 
    872 	strncpy(hot_spare.component_name, component,
    873 		sizeof(hot_spare.component_name));
    874 
    875 	do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
    876 		  "RAIDFRAME_REMOVE_HOT_SPARE");
    877 }
    878 
    879 static void
    880 rebuild_in_place(int fd, char *component)
    881 {
    882 	RF_SingleComponent_t comp;
    883 	int component_num;
    884 	int num_cols;
    885 
    886 	get_component_number(fd, component, &component_num, &num_cols);
    887 
    888 	comp.row = 0;
    889 	comp.column = component_num;
    890 	strncpy(comp.component_name, component, sizeof(comp.component_name));
    891 
    892 	do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
    893 		  "RAIDFRAME_REBUILD_IN_PLACE");
    894 
    895 	if (verbose) {
    896 		printf("Reconstruction status:\n");
    897 		sleep(3); /* XXX give reconstruction a chance to start */
    898 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
    899 	}
    900 
    901 }
    902 
    903 static void
    904 check_parity(int fd, int do_rewrite, char *dev_name)
    905 {
    906 	int is_clean;
    907 	int percent_done;
    908 
    909 	is_clean = 0;
    910 	percent_done = 0;
    911 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    912 		 "RAIDFRAME_CHECK_PARITY");
    913 	if (is_clean) {
    914 		printf("%s: Parity status: clean\n",dev_name);
    915 	} else {
    916 		printf("%s: Parity status: DIRTY\n",dev_name);
    917 		if (do_rewrite) {
    918 			printf("%s: Initiating re-write of parity\n",
    919 			       dev_name);
    920 			do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    921 				 "RAIDFRAME_REWRITEPARITY");
    922 			sleep(3); /* XXX give it time to
    923 				     get started. */
    924 			if (verbose) {
    925 				printf("Parity Re-write status:\n");
    926 				do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
    927 			} else {
    928 				do_ioctl(fd,
    929 					 RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    930 					 &percent_done,
    931 					 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"
    932 					 );
    933 				while( percent_done < 100 ) {
    934 					sleep(3); /* wait a bit... */
    935 					do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    936 						 &percent_done, "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    937 				}
    938 
    939 			}
    940 			       printf("%s: Parity Re-write complete\n",
    941 				      dev_name);
    942 		} else {
    943 			/* parity is wrong, and is not being fixed.
    944 			   Exit w/ an error. */
    945 			exit(1);
    946 		}
    947 	}
    948 }
    949 
    950 
    951 static void
    952 check_status(int fd, int meter)
    953 {
    954 	int recon_percent_done = 0;
    955 	int parity_percent_done = 0;
    956 	int copyback_percent_done = 0;
    957 
    958 	do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done,
    959 		 "RAIDFRAME_CHECK_RECON_STATUS");
    960 	printf("Reconstruction is %d%% complete.\n", recon_percent_done);
    961 	do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    962 		 &parity_percent_done,
    963 		 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    964 	printf("Parity Re-write is %d%% complete.\n", parity_percent_done);
    965 	do_ioctl(fd, RAIDFRAME_CHECK_COPYBACK_STATUS, &copyback_percent_done,
    966 		 "RAIDFRAME_CHECK_COPYBACK_STATUS");
    967 	printf("Copyback is %d%% complete.\n", copyback_percent_done);
    968 
    969 	if (meter) {
    970 		/* These 3 should be mutually exclusive at this point */
    971 		if (recon_percent_done < 100) {
    972 			printf("Reconstruction status:\n");
    973 			do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
    974 		} else if (parity_percent_done < 100) {
    975 			printf("Parity Re-write status:\n");
    976 			do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
    977 		} else if (copyback_percent_done < 100) {
    978 			printf("Copyback status:\n");
    979 			do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS_EXT);
    980 		}
    981 	}
    982 }
    983 
    984 const char *tbits = "|/-\\";
    985 
    986 static void
    987 do_meter(int fd, u_long option)
    988 {
    989 	int percent_done;
    990 	RF_uint64 start_value;
    991 	RF_ProgressInfo_t progressInfo;
    992 	void *pInfoPtr;
    993 	struct timeval start_time;
    994 	struct timeval current_time;
    995 	double elapsed;
    996 	int elapsed_sec;
    997 	int elapsed_usec;
    998 	int simple_eta,last_eta;
    999 	double rate;
   1000 	RF_uint64 amount;
   1001 	int tbit_value;
   1002 	char buffer[1024];
   1003 	char bar_buffer[1024];
   1004 	char eta_buffer[1024];
   1005 
   1006 	if (gettimeofday(&start_time,NULL) == -1)
   1007 		err(1, "gettimeofday failed!?!?");
   1008 	memset(&progressInfo, 0, sizeof(RF_ProgressInfo_t));
   1009 	pInfoPtr=&progressInfo;
   1010 
   1011 	percent_done = 0;
   1012 	do_ioctl(fd, option, &pInfoPtr, "");
   1013 	start_value = progressInfo.completed;
   1014 	current_time = start_time;
   1015 	simple_eta = 0;
   1016 	last_eta = 0;
   1017 
   1018 	tbit_value = 0;
   1019 	while(progressInfo.completed < progressInfo.total) {
   1020 
   1021 		percent_done = (progressInfo.completed * 100) /
   1022 			progressInfo.total;
   1023 
   1024 		get_bar(bar_buffer, percent_done, 40);
   1025 
   1026 		elapsed_sec = current_time.tv_sec - start_time.tv_sec;
   1027 		elapsed_usec = current_time.tv_usec - start_time.tv_usec;
   1028 		if (elapsed_usec < 0) {
   1029 			elapsed_usec-=1000000;
   1030 			elapsed_sec++;
   1031 		}
   1032 
   1033 		elapsed = (double) elapsed_sec +
   1034 			(double) elapsed_usec / 1000000.0;
   1035 
   1036 		amount = progressInfo.completed - start_value;
   1037 
   1038 		if (amount <= 0) { /* we don't do negatives (yet?) */
   1039 			amount = 0;
   1040 		}
   1041 
   1042 		if (elapsed == 0)
   1043 			rate = 0.0;
   1044 		else
   1045 			rate = amount / elapsed;
   1046 
   1047 		if (rate > 0.0) {
   1048 			simple_eta = (int) (((double)progressInfo.total -
   1049 					     (double) progressInfo.completed)
   1050 					    / rate);
   1051 		} else {
   1052 			simple_eta = -1;
   1053 		}
   1054 
   1055 		if (simple_eta <=0) {
   1056 			simple_eta = last_eta;
   1057 		} else {
   1058 			last_eta = simple_eta;
   1059 		}
   1060 
   1061 		get_time_string(eta_buffer, simple_eta);
   1062 
   1063 		snprintf(buffer,1024,"\r%3d%% |%s| ETA: %s %c",
   1064 			 percent_done,bar_buffer,eta_buffer,tbits[tbit_value]);
   1065 
   1066 		write(fileno(stdout),buffer,strlen(buffer));
   1067 		fflush(stdout);
   1068 
   1069 		if (++tbit_value>3)
   1070 			tbit_value = 0;
   1071 
   1072 		sleep(2);
   1073 
   1074 		if (gettimeofday(&current_time,NULL) == -1)
   1075 			err(1, "gettimeofday failed!?!?");
   1076 
   1077 		do_ioctl( fd, option, &pInfoPtr, "");
   1078 
   1079 
   1080 	}
   1081 	printf("\n");
   1082 }
   1083 /* 40 '*''s per line, then 40 ' ''s line. */
   1084 /* If you've got a screen wider than 160 characters, "tough" */
   1085 
   1086 #define STAR_MIDPOINT 4*40
   1087 const char stars[] = "****************************************"
   1088                      "****************************************"
   1089                      "****************************************"
   1090                      "****************************************"
   1091                      "                                        "
   1092                      "                                        "
   1093                      "                                        "
   1094                      "                                        "
   1095                      "                                        ";
   1096 
   1097 static void
   1098 get_bar(char *string, double percent, int max_strlen)
   1099 {
   1100 	int offset;
   1101 
   1102 	if (max_strlen > STAR_MIDPOINT) {
   1103 		max_strlen = STAR_MIDPOINT;
   1104 	}
   1105 	offset = STAR_MIDPOINT -
   1106 		(int)((percent * max_strlen)/ 100);
   1107 	if (offset < 0)
   1108 		offset = 0;
   1109 	snprintf(string,max_strlen,"%s",&stars[offset]);
   1110 }
   1111 
   1112 static void
   1113 get_time_string(char *string, int simple_time)
   1114 {
   1115 	int minutes, seconds, hours;
   1116 	char hours_buffer[5];
   1117 	char minutes_buffer[5];
   1118 	char seconds_buffer[5];
   1119 
   1120 	if (simple_time >= 0) {
   1121 
   1122 		minutes = (int) simple_time / 60;
   1123 		seconds = ((int)simple_time - 60*minutes);
   1124 		hours = minutes / 60;
   1125 		minutes = minutes - 60*hours;
   1126 
   1127 		if (hours > 0) {
   1128 			snprintf(hours_buffer,5,"%02d:",hours);
   1129 		} else {
   1130 			snprintf(hours_buffer,5,"   ");
   1131 		}
   1132 
   1133 		snprintf(minutes_buffer,5,"%02d:",minutes);
   1134 		snprintf(seconds_buffer,5,"%02d",seconds);
   1135 		snprintf(string,1024,"%s%s%s",
   1136 			 hours_buffer, minutes_buffer, seconds_buffer);
   1137 	} else {
   1138 		snprintf(string,1024,"   --:--");
   1139 	}
   1140 
   1141 }
   1142 
   1143 static void
   1144 usage(void)
   1145 {
   1146 	const char *progname = getprogname();
   1147 
   1148 	fprintf(stderr, "usage: %s [-v] -a component dev\n", progname);
   1149 	fprintf(stderr, "       %s [-v] -A [yes | no | root] dev\n", progname);
   1150 	fprintf(stderr, "       %s [-v] -B dev\n", progname);
   1151 	fprintf(stderr, "       %s [-v] -c config_file dev\n", progname);
   1152 	fprintf(stderr, "       %s [-v] -C config_file dev\n", progname);
   1153 	fprintf(stderr, "       %s [-v] -f component dev\n", progname);
   1154 	fprintf(stderr, "       %s [-v] -F component dev\n", progname);
   1155 	fprintf(stderr, "       %s [-v] -g component dev\n", progname);
   1156 	fprintf(stderr, "       %s [-v] -G dev\n", progname);
   1157 	fprintf(stderr, "       %s [-v] -i dev\n", progname);
   1158 	fprintf(stderr, "       %s [-v] -I serial_number dev\n", progname);
   1159 	fprintf(stderr, "       %s [-v] -m dev\n", progname);
   1160 	fprintf(stderr, "       %s [-v] -M [yes | no | set params] dev\n",
   1161 	    progname);
   1162 	fprintf(stderr, "       %s [-v] -p dev\n", progname);
   1163 	fprintf(stderr, "       %s [-v] -P dev\n", progname);
   1164 	fprintf(stderr, "       %s [-v] -r component dev\n", progname);
   1165 	fprintf(stderr, "       %s [-v] -R component dev\n", progname);
   1166 	fprintf(stderr, "       %s [-v] -s dev\n", progname);
   1167 	fprintf(stderr, "       %s [-v] -S dev\n", progname);
   1168 	fprintf(stderr, "       %s [-v] -u dev\n", progname);
   1169 	exit(1);
   1170 	/* NOTREACHED */
   1171 }
   1172