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