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