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