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