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