Home | History | Annotate | Line # | Download | only in raidctl
raidctl.c revision 1.69
      1 /*      $NetBSD: raidctl.c,v 1.69 2019/02/06 22:38:10 oster Exp $   */
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Greg Oster
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     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.69 2019/02/06 22:38:10 oster 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 *, size_t, 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 /* convert "component0" into "absent" */
    593 static const char *rf_output_devname(const char *name)
    594 {
    595 
    596 	if (strncmp(name, "component", 9) == 0)
    597 		return "absent";
    598 	return name;
    599 }
    600 
    601 static void
    602 rf_output_configuration(int fd, const char *name)
    603 {
    604 	RF_DeviceConfig_t device_config;
    605 	void *cfg_ptr;
    606 	int i;
    607 	RF_ComponentLabel_t component_label;
    608 	void *label_ptr;
    609 	int component_num;
    610 	int num_cols;
    611 
    612 	cfg_ptr = &device_config;
    613 
    614 	printf("# raidctl config file for %s\n", name);
    615 	printf("\n");
    616 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
    617 
    618 	/*
    619 	 * After NetBSD 9, convert this to not output the numRow's value,
    620 	 * which is no longer required or ever used.
    621 	 */
    622 	printf("START array\n");
    623 	printf("# numRow numCol numSpare\n");
    624 	printf("%d %d %d\n", 1, device_config.cols,
    625 	    device_config.nspares);
    626 	printf("\n");
    627 
    628 	printf("START disks\n");
    629 	for(i=0; i < device_config.ndevs; i++)
    630 		printf("%s\n",
    631 		    rf_output_devname(device_config.devs[i].devname));
    632 	printf("\n");
    633 
    634 	if (device_config.nspares > 0) {
    635 		printf("START spare\n");
    636 		for(i=0; i < device_config.nspares; i++)
    637 			printf("%s\n", device_config.spares[i].devname);
    638 		printf("\n");
    639 	}
    640 
    641 	for(i=0; i < device_config.ndevs; i++) {
    642 		if (device_config.devs[i].status == rf_ds_optimal)
    643 			break;
    644 	}
    645 	if (i == device_config.ndevs) {
    646 		printf("# WARNING: no optimal components; using %s\n",
    647 		    device_config.devs[0].devname);
    648 		i = 0;
    649 	}
    650 	get_component_number(fd, device_config.devs[i].devname,
    651 	    &component_num, &num_cols);
    652 	memset(&component_label, 0, sizeof(RF_ComponentLabel_t));
    653 	component_label.row = component_num / num_cols;
    654 	component_label.column = component_num % num_cols;
    655 	label_ptr = &component_label;
    656 	do_ioctl(fd, RAIDFRAME_GET_COMPONENT_LABEL, label_ptr,
    657 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    658 
    659 	printf("START layout\n");
    660 	printf(
    661 	    "# sectPerSU SUsPerParityUnit SUsPerReconUnit RAID_level_%c\n",
    662 	    (char) component_label.parityConfig);
    663 	printf("%d %d %d %c\n",
    664 	    component_label.sectPerSU, component_label.SUsPerPU,
    665 	    component_label.SUsPerRU, (char) component_label.parityConfig);
    666 	printf("\n");
    667 
    668 	printf("START queue\n");
    669 	printf("fifo %d\n", device_config.maxqdepth);
    670 }
    671 
    672 static void
    673 get_component_number(int fd, char *component_name, int *component_number,
    674 		     int *num_columns)
    675 {
    676 	RF_DeviceConfig_t device_config;
    677 	void *cfg_ptr;
    678 	int i;
    679 	int found;
    680 
    681 	*component_number = -1;
    682 
    683 	/* Assuming a full path spec... */
    684 	cfg_ptr = &device_config;
    685 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    686 		 "RAIDFRAME_GET_INFO");
    687 
    688 	*num_columns = device_config.cols;
    689 
    690 	found = 0;
    691 	for(i=0; i < device_config.ndevs; i++) {
    692 		if (strncmp(component_name, device_config.devs[i].devname,
    693 			    PATH_MAX)==0) {
    694 			found = 1;
    695 			*component_number = i;
    696 		}
    697 	}
    698 	if (!found) { /* maybe it's a spare? */
    699 		for(i=0; i < device_config.nspares; i++) {
    700 			if (strncmp(component_name,
    701 				    device_config.spares[i].devname,
    702 				    PATH_MAX)==0) {
    703 				found = 1;
    704 				*component_number = i + device_config.ndevs;
    705 				/* the way spares are done should
    706 				   really change... */
    707 				*num_columns = device_config.cols +
    708 					device_config.nspares;
    709 			}
    710 		}
    711 	}
    712 
    713 	if (!found)
    714 		err(1,"%s is not a component of this device", component_name);
    715 }
    716 
    717 static void
    718 rf_fail_disk(int fd, char *component_to_fail, int do_recon)
    719 {
    720 	struct rf_recon_req recon_request;
    721 	int component_num;
    722 	int num_cols;
    723 
    724 	get_component_number(fd, component_to_fail, &component_num, &num_cols);
    725 
    726 	recon_request.col = component_num % num_cols;
    727 	if (do_recon) {
    728 		recon_request.flags = RF_FDFLAGS_RECON;
    729 	} else {
    730 		recon_request.flags = RF_FDFLAGS_NONE;
    731 	}
    732 	do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
    733 		 "RAIDFRAME_FAIL_DISK");
    734 	if (do_recon && verbose) {
    735 		printf("Reconstruction status:\n");
    736 		sleep(3); /* XXX give reconstruction a chance to start */
    737 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
    738 	}
    739 }
    740 
    741 static void
    742 get_component_label(int fd, char *component)
    743 {
    744 	RF_ComponentLabel_t component_label;
    745 	void *label_ptr;
    746 	int component_num;
    747 	int num_cols;
    748 
    749 	get_component_number(fd, component, &component_num, &num_cols);
    750 
    751 	memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
    752 	component_label.row = component_num / num_cols;
    753 	component_label.column = component_num % num_cols;
    754 
    755 	label_ptr = &component_label;
    756 	do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, label_ptr,
    757 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    758 
    759 	printf("Component label for %s:\n",component);
    760 
    761 	printf("   Row: %d, Column: %d, Num Rows: %d, Num Columns: %d\n",
    762 	       component_label.row, component_label.column,
    763 	       component_label.num_rows, component_label.num_columns);
    764 	printf("   Version: %d, Serial Number: %u, Mod Counter: %d\n",
    765 	       component_label.version, component_label.serial_number,
    766 	       component_label.mod_counter);
    767 	printf("   Clean: %s, Status: %d\n",
    768 	       component_label.clean ? "Yes" : "No",
    769 	       component_label.status );
    770 	printf("   sectPerSU: %d, SUsPerPU: %d, SUsPerRU: %d\n",
    771 	       component_label.sectPerSU, component_label.SUsPerPU,
    772 	       component_label.SUsPerRU);
    773 	printf("   Queue size: %d, blocksize: %d, numBlocks: %"PRIu64"\n",
    774 	       component_label.maxOutstanding, component_label.blockSize,
    775 	       rf_component_label_numblocks(&component_label));
    776 	printf("   RAID Level: %c\n", (char) component_label.parityConfig);
    777 	printf("   Autoconfig: %s\n",
    778 	       component_label.autoconfigure ? "Yes" : "No" );
    779 	printf("   Root partition: %s\n",
    780 	       rootpart[component_label.root_partition & 3]);
    781 	printf("   Last configured as: raid%d\n", component_label.last_unit );
    782 }
    783 
    784 static void
    785 set_component_label(int fd, char *component)
    786 {
    787 	RF_ComponentLabel_t component_label;
    788 	int component_num;
    789 	int num_cols;
    790 
    791 	get_component_number(fd, component, &component_num, &num_cols);
    792 
    793 	/* XXX This is currently here for testing, and future expandability */
    794 
    795 	component_label.version = 1;
    796 	component_label.serial_number = 123456;
    797 	component_label.mod_counter = 0;
    798 	component_label.row = component_num / num_cols;
    799 	component_label.column = component_num % num_cols;
    800 	component_label.num_rows = 0;
    801 	component_label.num_columns = 5;
    802 	component_label.clean = 0;
    803 	component_label.status = 1;
    804 
    805 	do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
    806 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    807 }
    808 
    809 
    810 static void
    811 init_component_labels(int fd, int serial_number)
    812 {
    813 	RF_ComponentLabel_t component_label;
    814 
    815 	component_label.version = 0;
    816 	component_label.serial_number = serial_number;
    817 	component_label.mod_counter = 0;
    818 	component_label.row = 0;
    819 	component_label.column = 0;
    820 	component_label.num_rows = 0;
    821 	component_label.num_columns = 0;
    822 	component_label.clean = 0;
    823 	component_label.status = 0;
    824 
    825 	do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
    826 		  "RAIDFRAME_INIT_LABELS");
    827 }
    828 
    829 static void
    830 set_autoconfig(int fd, int raidID, char *autoconf)
    831 {
    832 	int auto_config;
    833 	int root_config;
    834 
    835 	auto_config = 0;
    836 	root_config = 0;
    837 
    838 	if (strncasecmp(autoconf, "root", 4) == 0 ||
    839 	    strncasecmp(autoconf, "hard", 4) == 0 ||
    840 	    strncasecmp(autoconf, "force", 5) == 0) {
    841 		root_config = 1;
    842 	} else if (strncasecmp(autoconf, "soft", 4) == 0) {
    843 		root_config = 2;
    844 	}
    845 
    846 	if ((strncasecmp(autoconf,"yes", 3) == 0) ||
    847 	    root_config > 0) {
    848 		auto_config = 1;
    849 	}
    850 
    851 	do_ioctl(fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config,
    852 		 "RAIDFRAME_SET_AUTOCONFIG");
    853 
    854 	do_ioctl(fd, RAIDFRAME_SET_ROOT, &root_config,
    855 		 "RAIDFRAME_SET_ROOT");
    856 
    857 	printf("raid%d: Autoconfigure: %s\n", raidID,
    858 	       auto_config ? "Yes" : "No");
    859 
    860 	if (auto_config == 1) {
    861 		printf("raid%d: Root: %s\n", raidID, rootpart[root_config]);
    862 	}
    863 }
    864 
    865 static void
    866 add_hot_spare(int fd, char *component)
    867 {
    868 	RF_SingleComponent_t hot_spare;
    869 
    870 	hot_spare.row = 0;
    871 	hot_spare.column = 0;
    872 	strncpy(hot_spare.component_name, component,
    873 		sizeof(hot_spare.component_name));
    874 
    875 	do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
    876 		  "RAIDFRAME_ADD_HOT_SPARE");
    877 }
    878 
    879 static void
    880 remove_hot_spare(int fd, char *component)
    881 {
    882 	RF_SingleComponent_t hot_spare;
    883 	int component_num;
    884 	int num_cols;
    885 
    886 	get_component_number(fd, component, &component_num, &num_cols);
    887 
    888 	hot_spare.row = component_num / num_cols;
    889 	hot_spare.column = component_num % num_cols;
    890 
    891 	strncpy(hot_spare.component_name, component,
    892 		sizeof(hot_spare.component_name));
    893 
    894 	do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
    895 		  "RAIDFRAME_REMOVE_HOT_SPARE");
    896 }
    897 
    898 static void
    899 rebuild_in_place(int fd, char *component)
    900 {
    901 	RF_SingleComponent_t comp;
    902 	int component_num;
    903 	int num_cols;
    904 
    905 	get_component_number(fd, component, &component_num, &num_cols);
    906 
    907 	comp.row = 0;
    908 	comp.column = component_num;
    909 	strncpy(comp.component_name, component, sizeof(comp.component_name));
    910 
    911 	do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
    912 		  "RAIDFRAME_REBUILD_IN_PLACE");
    913 
    914 	if (verbose) {
    915 		printf("Reconstruction status:\n");
    916 		sleep(3); /* XXX give reconstruction a chance to start */
    917 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
    918 	}
    919 
    920 }
    921 
    922 static void
    923 check_parity(int fd, int do_rewrite, char *dev_name)
    924 {
    925 	int is_clean;
    926 	int percent_done;
    927 
    928 	is_clean = 0;
    929 	percent_done = 0;
    930 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    931 		 "RAIDFRAME_CHECK_PARITY");
    932 	if (is_clean) {
    933 		printf("%s: Parity status: clean\n",dev_name);
    934 	} else {
    935 		printf("%s: Parity status: DIRTY\n",dev_name);
    936 		if (do_rewrite) {
    937 			printf("%s: Initiating re-write of parity\n",
    938 			       dev_name);
    939 			do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    940 				 "RAIDFRAME_REWRITEPARITY");
    941 			sleep(3); /* XXX give it time to
    942 				     get started. */
    943 			if (verbose) {
    944 				printf("Parity Re-write status:\n");
    945 				do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
    946 			} else {
    947 				do_ioctl(fd,
    948 					 RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    949 					 &percent_done,
    950 					 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"
    951 					 );
    952 				while( percent_done < 100 ) {
    953 					sleep(3); /* wait a bit... */
    954 					do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    955 						 &percent_done, "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    956 				}
    957 
    958 			}
    959 			       printf("%s: Parity Re-write complete\n",
    960 				      dev_name);
    961 		} else {
    962 			/* parity is wrong, and is not being fixed.
    963 			   Exit w/ an error. */
    964 			exit(1);
    965 		}
    966 	}
    967 }
    968 
    969 
    970 static void
    971 check_status(int fd, int meter)
    972 {
    973 	int recon_percent_done = 0;
    974 	int parity_percent_done = 0;
    975 	int copyback_percent_done = 0;
    976 
    977 	do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done,
    978 		 "RAIDFRAME_CHECK_RECON_STATUS");
    979 	printf("Reconstruction is %d%% complete.\n", recon_percent_done);
    980 	do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    981 		 &parity_percent_done,
    982 		 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    983 	printf("Parity Re-write is %d%% complete.\n", parity_percent_done);
    984 	do_ioctl(fd, RAIDFRAME_CHECK_COPYBACK_STATUS, &copyback_percent_done,
    985 		 "RAIDFRAME_CHECK_COPYBACK_STATUS");
    986 	printf("Copyback is %d%% complete.\n", copyback_percent_done);
    987 
    988 	if (meter) {
    989 		/* These 3 should be mutually exclusive at this point */
    990 		if (recon_percent_done < 100) {
    991 			printf("Reconstruction status:\n");
    992 			do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
    993 		} else if (parity_percent_done < 100) {
    994 			printf("Parity Re-write status:\n");
    995 			do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
    996 		} else if (copyback_percent_done < 100) {
    997 			printf("Copyback status:\n");
    998 			do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS_EXT);
    999 		}
   1000 	}
   1001 }
   1002 
   1003 const char *tbits = "|/-\\";
   1004 
   1005 static void
   1006 do_meter(int fd, u_long option)
   1007 {
   1008 	int percent_done;
   1009 	RF_uint64 start_value;
   1010 	RF_ProgressInfo_t progressInfo;
   1011 	void *pInfoPtr;
   1012 	struct timeval start_time;
   1013 	struct timeval current_time;
   1014 	double elapsed;
   1015 	int elapsed_sec;
   1016 	int elapsed_usec;
   1017 	int simple_eta,last_eta;
   1018 	double rate;
   1019 	RF_uint64 amount;
   1020 	int tbit_value;
   1021 	char bar_buffer[1024];
   1022 	char eta_buffer[1024];
   1023 
   1024 	if (gettimeofday(&start_time,NULL) == -1)
   1025 		err(1, "gettimeofday failed!?!?");
   1026 	memset(&progressInfo, 0, sizeof(RF_ProgressInfo_t));
   1027 	pInfoPtr=&progressInfo;
   1028 
   1029 	percent_done = 0;
   1030 	do_ioctl(fd, option, pInfoPtr, "");
   1031 	start_value = progressInfo.completed;
   1032 	current_time = start_time;
   1033 	simple_eta = 0;
   1034 	last_eta = 0;
   1035 
   1036 	tbit_value = 0;
   1037 	while(progressInfo.completed < progressInfo.total) {
   1038 
   1039 		percent_done = (progressInfo.completed * 100) /
   1040 			progressInfo.total;
   1041 
   1042 		get_bar(bar_buffer, percent_done, 40);
   1043 
   1044 		elapsed_sec = current_time.tv_sec - start_time.tv_sec;
   1045 		elapsed_usec = current_time.tv_usec - start_time.tv_usec;
   1046 		if (elapsed_usec < 0) {
   1047 			elapsed_usec-=1000000;
   1048 			elapsed_sec++;
   1049 		}
   1050 
   1051 		elapsed = (double) elapsed_sec +
   1052 			(double) elapsed_usec / 1000000.0;
   1053 
   1054 		amount = progressInfo.completed - start_value;
   1055 
   1056 		if (amount <= 0) { /* we don't do negatives (yet?) */
   1057 			amount = 0;
   1058 		}
   1059 
   1060 		if (elapsed == 0)
   1061 			rate = 0.0;
   1062 		else
   1063 			rate = amount / elapsed;
   1064 
   1065 		if (rate > 0.0) {
   1066 			simple_eta = (int) (((double)progressInfo.total -
   1067 					     (double) progressInfo.completed)
   1068 					    / rate);
   1069 		} else {
   1070 			simple_eta = -1;
   1071 		}
   1072 
   1073 		if (simple_eta <=0) {
   1074 			simple_eta = last_eta;
   1075 		} else {
   1076 			last_eta = simple_eta;
   1077 		}
   1078 
   1079 		get_time_string(eta_buffer, sizeof eta_buffer, simple_eta);
   1080 
   1081 		fprintf(stdout,"\r%3d%% |%s| ETA: %s %c",
   1082 			percent_done,bar_buffer,eta_buffer,tbits[tbit_value]);
   1083 		fflush(stdout);
   1084 
   1085 		if (++tbit_value>3)
   1086 			tbit_value = 0;
   1087 
   1088 		sleep(2);
   1089 
   1090 		if (gettimeofday(&current_time,NULL) == -1)
   1091 			err(1, "gettimeofday failed!?!?");
   1092 
   1093 		do_ioctl( fd, option, pInfoPtr, "");
   1094 
   1095 
   1096 	}
   1097 	printf("\n");
   1098 }
   1099 /* 40 '*''s per line, then 40 ' ''s line. */
   1100 /* If you've got a screen wider than 160 characters, "tough" */
   1101 
   1102 #define STAR_MIDPOINT 4*40
   1103 const char stars[] = "****************************************"
   1104                      "****************************************"
   1105                      "****************************************"
   1106                      "****************************************"
   1107                      "                                        "
   1108                      "                                        "
   1109                      "                                        "
   1110                      "                                        "
   1111                      "                                        ";
   1112 
   1113 static void
   1114 get_bar(char *string, double percent, int max_strlen)
   1115 {
   1116 	int offset;
   1117 
   1118 	if (max_strlen > STAR_MIDPOINT) {
   1119 		max_strlen = STAR_MIDPOINT;
   1120 	}
   1121 	offset = STAR_MIDPOINT -
   1122 		(int)((percent * max_strlen)/ 100);
   1123 	if (offset < 0)
   1124 		offset = 0;
   1125 	snprintf(string,max_strlen,"%s",stars+offset);
   1126 }
   1127 
   1128 static void
   1129 get_time_string(char *string, size_t len, int simple_time)
   1130 {
   1131 	int minutes, seconds, hours;
   1132 	char hours_buffer[8];
   1133 	char minutes_buffer[5];
   1134 	char seconds_buffer[5];
   1135 
   1136 	if (simple_time >= 0) {
   1137 
   1138 		minutes = simple_time / 60;
   1139 		seconds = simple_time - 60*minutes;
   1140 		hours = minutes / 60;
   1141 		minutes = minutes - 60*hours;
   1142 #if defined(__GNUC__)
   1143 		/*
   1144 		 * snprintf() truncation checker fails to detect that seconds
   1145 		 * and minutes will be 0-59 range.
   1146 		 */
   1147 		if (minutes < 0 || minutes > 60)
   1148 			minutes = 60;
   1149 		if (seconds < 0 || seconds > 60)
   1150 			seconds = 60;
   1151 #endif
   1152 
   1153 		if (hours > 0) {
   1154 			snprintf(hours_buffer,sizeof hours_buffer,"%02d:",hours);
   1155 		} else {
   1156 			snprintf(hours_buffer,sizeof hours_buffer,"   ");
   1157 		}
   1158 
   1159 		snprintf(minutes_buffer,sizeof minutes_buffer,"%02d:",minutes);
   1160 		snprintf(seconds_buffer,sizeof seconds_buffer,"%02d",seconds);
   1161 		snprintf(string,len,"%s%s%s",
   1162 			 hours_buffer, minutes_buffer, seconds_buffer);
   1163 	} else {
   1164 		snprintf(string,len,"   --:--");
   1165 	}
   1166 
   1167 }
   1168 
   1169 static void
   1170 usage(void)
   1171 {
   1172 	const char *progname = getprogname();
   1173 
   1174 	fprintf(stderr, "usage: %s [-v] -A [yes | no | softroot | hardroot] dev\n", progname);
   1175 	fprintf(stderr, "       %s [-v] -a component dev\n", progname);
   1176 	fprintf(stderr, "       %s [-v] -B dev\n", progname);
   1177 	fprintf(stderr, "       %s [-v] -C config_file dev\n", progname);
   1178 	fprintf(stderr, "       %s [-v] -c config_file dev\n", progname);
   1179 	fprintf(stderr, "       %s [-v] -F component dev\n", progname);
   1180 	fprintf(stderr, "       %s [-v] -f component dev\n", progname);
   1181 	fprintf(stderr, "       %s [-v] -G dev\n", progname);
   1182 	fprintf(stderr, "       %s [-v] -g component dev\n", progname);
   1183 	fprintf(stderr, "       %s [-v] -I serial_number dev\n", progname);
   1184 	fprintf(stderr, "       %s [-v] -i dev\n", progname);
   1185 	fprintf(stderr, "       %s [-v] -M [yes | no | set params] dev\n",
   1186 	    progname);
   1187 	fprintf(stderr, "       %s [-v] -m dev\n", progname);
   1188 	fprintf(stderr, "       %s [-v] -P dev\n", progname);
   1189 	fprintf(stderr, "       %s [-v] -p dev\n", progname);
   1190 	fprintf(stderr, "       %s [-v] -R component dev\n", progname);
   1191 	fprintf(stderr, "       %s [-v] -r component dev\n", progname);
   1192 	fprintf(stderr, "       %s [-v] -S dev\n", progname);
   1193 	fprintf(stderr, "       %s [-v] -s dev\n", progname);
   1194 	fprintf(stderr, "       %s [-v] -U unit dev\n", progname);
   1195 	fprintf(stderr, "       %s [-v] -u dev\n", progname);
   1196 	exit(1);
   1197 	/* NOTREACHED */
   1198 }
   1199 
   1200 static unsigned int
   1201 xstrtouint(const char *str)
   1202 {
   1203 	int e;
   1204 	unsigned int num = (unsigned int)strtou(str, NULL, 10, 0, INT_MAX, &e);
   1205 	if (e)
   1206 		errc(EXIT_FAILURE, e, "Bad number `%s'", str);
   1207 	return num;
   1208 }
   1209