Home | History | Annotate | Line # | Download | only in raidctl
raidctl.c revision 1.66
      1  1.66       mrg /*      $NetBSD: raidctl.c,v 1.66 2018/01/18 00:32:49 mrg Exp $   */
      2  1.18   thorpej 
      3   1.1     oster /*-
      4   1.1     oster  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5   1.1     oster  * All rights reserved.
      6   1.1     oster  *
      7   1.1     oster  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     oster  * by Greg Oster
      9   1.1     oster  *
     10   1.1     oster  * Redistribution and use in source and binary forms, with or without
     11   1.1     oster  * modification, are permitted provided that the following conditions
     12   1.1     oster  * are met:
     13   1.1     oster  * 1. Redistributions of source code must retain the above copyright
     14   1.1     oster  *    notice, this list of conditions and the following disclaimer.
     15   1.1     oster  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     oster  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     oster  *    documentation and/or other materials provided with the distribution.
     18   1.1     oster  *
     19   1.1     oster  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     oster  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     oster  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     oster  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     oster  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     oster  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     oster  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     oster  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     oster  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     oster  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     oster  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     oster  */
     31   1.1     oster 
     32   1.1     oster /*
     33  1.18   thorpej  * This program is a re-write of the original rf_ctrl program
     34  1.18   thorpej  * distributed by CMU with RAIDframe 1.1.
     35  1.18   thorpej  *
     36  1.18   thorpej  * This program is the user-land interface to the RAIDframe kernel
     37  1.18   thorpej  * driver in NetBSD.
     38   1.1     oster  */
     39  1.33       agc #include <sys/cdefs.h>
     40  1.33       agc 
     41  1.33       agc #ifndef lint
     42  1.66       mrg __RCSID("$NetBSD: raidctl.c,v 1.66 2018/01/18 00:32:49 mrg Exp $");
     43  1.33       agc #endif
     44  1.33       agc 
     45   1.1     oster 
     46   1.1     oster #include <sys/param.h>
     47   1.1     oster #include <sys/ioctl.h>
     48   1.1     oster #include <sys/stat.h>
     49  1.18   thorpej #include <sys/disklabel.h>
     50  1.18   thorpej 
     51   1.1     oster #include <ctype.h>
     52   1.1     oster #include <err.h>
     53   1.2    mjacob #include <errno.h>
     54  1.25     lukem #include <fcntl.h>
     55  1.25     lukem #include <stdio.h>
     56  1.25     lukem #include <stdlib.h>
     57   1.1     oster #include <string.h>
     58  1.59  christos #include <inttypes.h>
     59   1.3     oster #include <unistd.h>
     60  1.25     lukem #include <util.h>
     61   1.3     oster 
     62  1.29     oster #include <dev/raidframe/raidframevar.h>
     63  1.29     oster #include <dev/raidframe/raidframeio.h>
     64  1.29     oster #include "rf_configure.h"
     65  1.50     pooka #include "prog_ops.h"
     66   1.1     oster 
     67  1.36   xtraeme void	do_ioctl(int, u_long, void *, const char *);
     68  1.36   xtraeme static  void rf_configure(int, char*, int);
     69  1.36   xtraeme static  const char *device_status(RF_DiskStatus_t);
     70  1.36   xtraeme static  void rf_get_device_status(int);
     71  1.36   xtraeme static	void rf_output_configuration(int, const char *);
     72  1.36   xtraeme static  void get_component_number(int, char *, int *, int *);
     73  1.36   xtraeme static  void rf_fail_disk(int, char *, int);
     74  1.53     joerg __dead static  void usage(void);
     75  1.36   xtraeme static  void get_component_label(int, char *);
     76  1.36   xtraeme static  void set_component_label(int, char *);
     77  1.36   xtraeme static  void init_component_labels(int, int);
     78  1.36   xtraeme static  void set_autoconfig(int, int, char *);
     79  1.36   xtraeme static  void add_hot_spare(int, char *);
     80  1.36   xtraeme static  void remove_hot_spare(int, char *);
     81  1.36   xtraeme static  void rebuild_in_place(int, char *);
     82  1.36   xtraeme static  void check_status(int,int);
     83  1.36   xtraeme static  void check_parity(int,int, char *);
     84  1.36   xtraeme static  void do_meter(int, u_long);
     85  1.36   xtraeme static  void get_bar(char *, double, int);
     86  1.36   xtraeme static  void get_time_string(char *, int);
     87  1.42       jld static  void rf_output_pmstat(int, int);
     88  1.42       jld static  void rf_pm_configure(int, int, char *, int[]);
     89  1.59  christos static  unsigned int xstrtouint(const char *);
     90  1.10     oster 
     91  1.18   thorpej int verbose;
     92   1.1     oster 
     93  1.57  christos static const char *rootpart[] = { "No", "Force", "Soft", "*invalid*" };
     94  1.57  christos 
     95   1.1     oster int
     96  1.36   xtraeme main(int argc,char *argv[])
     97   1.1     oster {
     98  1.42       jld 	int ch, i;
     99   1.1     oster 	int num_options;
    100   1.1     oster 	unsigned long action;
    101   1.1     oster 	char config_filename[PATH_MAX];
    102   1.1     oster 	char dev_name[PATH_MAX];
    103   1.1     oster 	char name[PATH_MAX];
    104   1.5     oster 	char component[PATH_MAX];
    105  1.12     oster 	char autoconf[10];
    106  1.42       jld 	char *parityconf = NULL;
    107  1.42       jld 	int parityparams[3];
    108  1.27     lukem 	int do_output;
    109   1.1     oster 	int do_recon;
    110   1.7     oster 	int do_rewrite;
    111   1.1     oster 	int raidID;
    112   1.5     oster 	int serial_number;
    113   1.1     oster 	struct stat st;
    114   1.1     oster 	int fd;
    115   1.5     oster 	int force;
    116  1.30    simonb 	int openmode;
    117  1.64  christos 	int last_unit;
    118   1.1     oster 
    119   1.1     oster 	num_options = 0;
    120   1.1     oster 	action = 0;
    121  1.27     lukem 	do_output = 0;
    122   1.1     oster 	do_recon = 0;
    123   1.7     oster 	do_rewrite = 0;
    124  1.38     lukem 	serial_number = 0;
    125   1.5     oster 	force = 0;
    126  1.64  christos 	last_unit = 0;
    127  1.30    simonb 	openmode = O_RDWR;	/* default to read/write */
    128   1.1     oster 
    129  1.64  christos 	while ((ch = getopt(argc, argv, "a:A:Bc:C:f:F:g:GiI:l:mM:r:R:sSpPuU:v"))
    130  1.12     oster 	       != -1)
    131   1.1     oster 		switch(ch) {
    132   1.5     oster 		case 'a':
    133   1.5     oster 			action = RAIDFRAME_ADD_HOT_SPARE;
    134  1.32    itojun 			strlcpy(component, optarg, sizeof(component));
    135   1.5     oster 			num_options++;
    136   1.5     oster 			break;
    137  1.12     oster 		case 'A':
    138  1.12     oster 			action = RAIDFRAME_SET_AUTOCONFIG;
    139  1.32    itojun 			strlcpy(autoconf, optarg, sizeof(autoconf));
    140  1.12     oster 			num_options++;
    141  1.12     oster 			break;
    142   1.6     oster 		case 'B':
    143   1.6     oster 			action = RAIDFRAME_COPYBACK;
    144   1.6     oster 			num_options++;
    145   1.6     oster 			break;
    146   1.1     oster 		case 'c':
    147   1.6     oster 			action = RAIDFRAME_CONFIGURE;
    148  1.32    itojun 			strlcpy(config_filename, optarg,
    149  1.32    itojun 			    sizeof(config_filename));
    150   1.6     oster 			force = 0;
    151   1.1     oster 			num_options++;
    152   1.1     oster 			break;
    153   1.1     oster 		case 'C':
    154  1.32    itojun 			strlcpy(config_filename, optarg,
    155  1.32    itojun 			    sizeof(config_filename));
    156   1.6     oster 			action = RAIDFRAME_CONFIGURE;
    157   1.6     oster 			force = 1;
    158   1.1     oster 			num_options++;
    159   1.1     oster 			break;
    160   1.1     oster 		case 'f':
    161   1.1     oster 			action = RAIDFRAME_FAIL_DISK;
    162  1.32    itojun 			strlcpy(component, optarg, sizeof(component));
    163   1.1     oster 			do_recon = 0;
    164   1.1     oster 			num_options++;
    165   1.1     oster 			break;
    166   1.1     oster 		case 'F':
    167   1.1     oster 			action = RAIDFRAME_FAIL_DISK;
    168  1.32    itojun 			strlcpy(component, optarg, sizeof(component));
    169   1.1     oster 			do_recon = 1;
    170   1.5     oster 			num_options++;
    171   1.5     oster 			break;
    172   1.5     oster 		case 'g':
    173   1.5     oster 			action = RAIDFRAME_GET_COMPONENT_LABEL;
    174  1.32    itojun 			strlcpy(component, optarg, sizeof(component));
    175  1.30    simonb 			openmode = O_RDONLY;
    176   1.5     oster 			num_options++;
    177   1.5     oster 			break;
    178  1.27     lukem 		case 'G':
    179  1.27     lukem 			action = RAIDFRAME_GET_INFO;
    180  1.30    simonb 			openmode = O_RDONLY;
    181  1.27     lukem 			do_output = 1;
    182  1.27     lukem 			num_options++;
    183  1.27     lukem 			break;
    184   1.6     oster 		case 'i':
    185   1.6     oster 			action = RAIDFRAME_REWRITEPARITY;
    186   1.6     oster 			num_options++;
    187   1.6     oster 			break;
    188   1.5     oster 		case 'I':
    189   1.5     oster 			action = RAIDFRAME_INIT_LABELS;
    190  1.59  christos 			serial_number = xstrtouint(optarg);
    191   1.5     oster 			num_options++;
    192   1.5     oster 			break;
    193  1.42       jld 		case 'm':
    194  1.42       jld 			action = RAIDFRAME_PARITYMAP_STATUS;
    195  1.42       jld 			openmode = O_RDONLY;
    196  1.42       jld 			num_options++;
    197  1.42       jld 			break;
    198  1.42       jld 		case 'M':
    199  1.42       jld 			action = RAIDFRAME_PARITYMAP_SET_DISABLE;
    200  1.42       jld 			parityconf = strdup(optarg);
    201  1.42       jld 			num_options++;
    202  1.58      manu 			/* XXXjld: should rf_pm_configure do the strtol()s? */
    203  1.42       jld 			i = 0;
    204  1.42       jld 			while (i < 3 && optind < argc &&
    205  1.42       jld 			    isdigit((int)argv[optind][0]))
    206  1.59  christos 				parityparams[i++] = xstrtouint(argv[optind++]);
    207  1.42       jld 			while (i < 3)
    208  1.42       jld 				parityparams[i++] = 0;
    209  1.42       jld 			break;
    210   1.6     oster 		case 'l':
    211   1.5     oster 			action = RAIDFRAME_SET_COMPONENT_LABEL;
    212  1.32    itojun 			strlcpy(component, optarg, sizeof(component));
    213   1.1     oster 			num_options++;
    214   1.1     oster 			break;
    215   1.1     oster 		case 'r':
    216   1.6     oster 			action = RAIDFRAME_REMOVE_HOT_SPARE;
    217  1.32    itojun 			strlcpy(component, optarg, sizeof(component));
    218   1.1     oster 			num_options++;
    219   1.1     oster 			break;
    220   1.1     oster 		case 'R':
    221  1.32    itojun 			strlcpy(component, optarg, sizeof(component));
    222   1.6     oster 			action = RAIDFRAME_REBUILD_IN_PLACE;
    223   1.1     oster 			num_options++;
    224   1.1     oster 			break;
    225   1.1     oster 		case 's':
    226   1.1     oster 			action = RAIDFRAME_GET_INFO;
    227  1.30    simonb 			openmode = O_RDONLY;
    228   1.1     oster 			num_options++;
    229   1.1     oster 			break;
    230   1.6     oster 		case 'S':
    231  1.19     oster 			action = RAIDFRAME_CHECK_RECON_STATUS_EXT;
    232  1.30    simonb 			openmode = O_RDONLY;
    233   1.6     oster 			num_options++;
    234   1.6     oster 			break;
    235   1.7     oster 		case 'p':
    236   1.7     oster 			action = RAIDFRAME_CHECK_PARITY;
    237  1.30    simonb 			openmode = O_RDONLY;
    238   1.7     oster 			num_options++;
    239   1.7     oster 			break;
    240   1.7     oster 		case 'P':
    241   1.7     oster 			action = RAIDFRAME_CHECK_PARITY;
    242   1.7     oster 			do_rewrite = 1;
    243   1.7     oster 			num_options++;
    244   1.7     oster 			break;
    245   1.1     oster 		case 'u':
    246   1.1     oster 			action = RAIDFRAME_SHUTDOWN;
    247   1.1     oster 			num_options++;
    248   1.1     oster 			break;
    249  1.64  christos 		case 'U':
    250  1.64  christos 			action = RAIDFRAME_SET_LAST_UNIT;
    251  1.64  christos 			num_options++;
    252  1.64  christos 			last_unit = atoi(optarg);
    253  1.64  christos 			if (last_unit < 0)
    254  1.64  christos 				errx(1, "Bad last unit %s", optarg);
    255  1.64  christos 			break;
    256  1.10     oster 		case 'v':
    257  1.10     oster 			verbose = 1;
    258  1.10     oster 			/* Don't bump num_options, as '-v' is not
    259  1.10     oster 			   an option like the others */
    260  1.10     oster 			/* num_options++; */
    261  1.10     oster 			break;
    262   1.1     oster 		default:
    263   1.1     oster 			usage();
    264   1.1     oster 		}
    265   1.1     oster 	argc -= optind;
    266   1.1     oster 	argv += optind;
    267   1.1     oster 
    268  1.34      fvdl 	if ((num_options > 1) || (argc == 0))
    269   1.1     oster 		usage();
    270   1.1     oster 
    271  1.50     pooka 	if (prog_init && prog_init() == -1)
    272  1.50     pooka 		err(1, "init failed");
    273  1.50     pooka 
    274  1.32    itojun 	strlcpy(name, argv[0], sizeof(name));
    275  1.41     pooka 	fd = opendisk1(name, openmode, dev_name, sizeof(dev_name), 0,
    276  1.50     pooka 	    prog_open);
    277  1.45  christos 	if (fd == -1)
    278  1.45  christos 		err(1, "Unable to open device file: %s", name);
    279  1.51     pooka 	if (prog_fstat(fd, &st) == -1)
    280  1.45  christos 		err(1, "stat failure on: %s", dev_name);
    281  1.45  christos 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    282  1.45  christos 		err(1, "invalid device: %s", dev_name);
    283   1.1     oster 
    284  1.28     oster 	raidID = DISKUNIT(st.st_rdev);
    285   1.1     oster 
    286   1.1     oster 	switch(action) {
    287   1.5     oster 	case RAIDFRAME_ADD_HOT_SPARE:
    288  1.12     oster 		add_hot_spare(fd, component);
    289   1.5     oster 		break;
    290   1.6     oster 	case RAIDFRAME_REMOVE_HOT_SPARE:
    291  1.12     oster 		remove_hot_spare(fd, component);
    292   1.6     oster 		break;
    293   1.1     oster 	case RAIDFRAME_CONFIGURE:
    294  1.12     oster 		rf_configure(fd, config_filename, force);
    295  1.12     oster 		break;
    296  1.12     oster 	case RAIDFRAME_SET_AUTOCONFIG:
    297  1.12     oster 		set_autoconfig(fd, raidID, autoconf);
    298   1.1     oster 		break;
    299   1.1     oster 	case RAIDFRAME_COPYBACK:
    300   1.1     oster 		printf("Copyback.\n");
    301   1.1     oster 		do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
    302  1.10     oster 		if (verbose) {
    303  1.10     oster 			sleep(3); /* XXX give the copyback a chance to start */
    304  1.10     oster 			printf("Copyback status:\n");
    305  1.19     oster 			do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS_EXT);
    306  1.10     oster 		}
    307   1.1     oster 		break;
    308   1.1     oster 	case RAIDFRAME_FAIL_DISK:
    309  1.12     oster 		rf_fail_disk(fd, component, do_recon);
    310   1.5     oster 		break;
    311   1.5     oster 	case RAIDFRAME_SET_COMPONENT_LABEL:
    312  1.12     oster 		set_component_label(fd, component);
    313   1.5     oster 		break;
    314   1.5     oster 	case RAIDFRAME_GET_COMPONENT_LABEL:
    315  1.12     oster 		get_component_label(fd, component);
    316   1.5     oster 		break;
    317   1.5     oster 	case RAIDFRAME_INIT_LABELS:
    318  1.12     oster 		init_component_labels(fd, serial_number);
    319   1.1     oster 		break;
    320   1.1     oster 	case RAIDFRAME_REWRITEPARITY:
    321   1.1     oster 		printf("Initiating re-write of parity\n");
    322   1.1     oster 		do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    323   1.1     oster 			 "RAIDFRAME_REWRITEPARITY");
    324  1.10     oster 		if (verbose) {
    325  1.10     oster 			sleep(3); /* XXX give it time to get started */
    326  1.10     oster 			printf("Parity Re-write status:\n");
    327  1.19     oster 			do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
    328  1.10     oster 		}
    329   1.1     oster 		break;
    330  1.19     oster 	case RAIDFRAME_CHECK_RECON_STATUS_EXT:
    331  1.14     oster 		check_status(fd,1);
    332   1.1     oster 		break;
    333   1.1     oster 	case RAIDFRAME_GET_INFO:
    334  1.27     lukem 		if (do_output)
    335  1.27     lukem 			rf_output_configuration(fd, dev_name);
    336  1.27     lukem 		else
    337  1.27     lukem 			rf_get_device_status(fd);
    338   1.1     oster 		break;
    339  1.42       jld 	case RAIDFRAME_PARITYMAP_STATUS:
    340  1.42       jld 		rf_output_pmstat(fd, raidID);
    341  1.42       jld 		break;
    342  1.42       jld 	case RAIDFRAME_PARITYMAP_SET_DISABLE:
    343  1.42       jld 		rf_pm_configure(fd, raidID, parityconf, parityparams);
    344  1.42       jld 		break;
    345   1.6     oster 	case RAIDFRAME_REBUILD_IN_PLACE:
    346  1.12     oster 		rebuild_in_place(fd, component);
    347   1.6     oster 		break;
    348   1.7     oster 	case RAIDFRAME_CHECK_PARITY:
    349  1.12     oster 		check_parity(fd, do_rewrite, dev_name);
    350   1.7     oster 		break;
    351   1.1     oster 	case RAIDFRAME_SHUTDOWN:
    352   1.1     oster 		do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
    353   1.1     oster 		break;
    354  1.64  christos 	case RAIDFRAME_SET_LAST_UNIT:
    355  1.64  christos 		do_ioctl(fd, RAIDFRAME_SET_LAST_UNIT, &last_unit,
    356  1.64  christos 		    "RAIDFRAME_SET_LAST_UNIT");
    357  1.64  christos 		break;
    358   1.1     oster 	default:
    359   1.1     oster 		break;
    360   1.1     oster 	}
    361   1.1     oster 
    362  1.50     pooka 	prog_close(fd);
    363   1.1     oster 	exit(0);
    364   1.1     oster }
    365   1.1     oster 
    366  1.10     oster void
    367  1.36   xtraeme do_ioctl(int fd, unsigned long command, void *arg, const char *ioctl_name)
    368   1.1     oster {
    369  1.50     pooka 	if (prog_ioctl(fd, command, arg) == -1)
    370  1.45  christos 		err(1, "ioctl (%s) failed", ioctl_name);
    371   1.1     oster }
    372   1.1     oster 
    373   1.1     oster 
    374   1.1     oster static void
    375  1.36   xtraeme rf_configure(int fd, char *config_file, int force)
    376   1.1     oster {
    377   1.1     oster 	void *generic;
    378   1.1     oster 	RF_Config_t cfg;
    379   1.1     oster 
    380  1.45  christos 	if (rf_MakeConfig( config_file, &cfg ) != 0)
    381  1.45  christos 		err(1, "Unable to create RAIDframe configuration structure");
    382   1.1     oster 
    383   1.6     oster 	cfg.force = force;
    384   1.6     oster 
    385   1.1     oster 	/*
    386  1.18   thorpej 	 * Note the extra level of redirection needed here, since
    387  1.18   thorpej 	 * what we really want to pass in is a pointer to the pointer to
    388  1.18   thorpej 	 * the configuration structure.
    389   1.1     oster 	 */
    390   1.1     oster 
    391  1.45  christos 	generic = &cfg;
    392   1.6     oster 	do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE");
    393   1.1     oster }
    394   1.1     oster 
    395  1.18   thorpej static const char *
    396  1.36   xtraeme device_status(RF_DiskStatus_t status)
    397   1.1     oster {
    398   1.1     oster 
    399   1.1     oster 	switch (status) {
    400   1.1     oster 	case rf_ds_optimal:
    401  1.18   thorpej 		return ("optimal");
    402   1.1     oster 		break;
    403   1.1     oster 	case rf_ds_failed:
    404  1.18   thorpej 		return ("failed");
    405   1.1     oster 		break;
    406   1.1     oster 	case rf_ds_reconstructing:
    407  1.18   thorpej 		return ("reconstructing");
    408   1.1     oster 		break;
    409   1.1     oster 	case rf_ds_dist_spared:
    410  1.18   thorpej 		return ("dist_spared");
    411   1.1     oster 		break;
    412   1.1     oster 	case rf_ds_spared:
    413  1.18   thorpej 		return ("spared");
    414   1.1     oster 		break;
    415   1.1     oster 	case rf_ds_spare:
    416  1.18   thorpej 		return ("spare");
    417   1.1     oster 		break;
    418   1.1     oster 	case rf_ds_used_spare:
    419  1.18   thorpej 		return ("used_spare");
    420   1.1     oster 		break;
    421   1.1     oster 	default:
    422  1.18   thorpej 		return ("UNKNOWN");
    423   1.1     oster 	}
    424  1.18   thorpej 	/* NOTREACHED */
    425   1.1     oster }
    426   1.1     oster 
    427   1.1     oster static void
    428  1.36   xtraeme rf_get_device_status(int fd)
    429   1.1     oster {
    430   1.1     oster 	RF_DeviceConfig_t device_config;
    431   1.1     oster 	void *cfg_ptr;
    432   1.8     oster 	int is_clean;
    433   1.1     oster 	int i;
    434   1.1     oster 
    435   1.1     oster 	cfg_ptr = &device_config;
    436   1.1     oster 
    437   1.1     oster 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
    438   1.1     oster 
    439   1.1     oster 	printf("Components:\n");
    440   1.1     oster 	for(i=0; i < device_config.ndevs; i++) {
    441   1.1     oster 		printf("%20s: %s\n", device_config.devs[i].devname,
    442   1.1     oster 		       device_status(device_config.devs[i].status));
    443   1.1     oster 	}
    444   1.1     oster 	if (device_config.nspares > 0) {
    445   1.1     oster 		printf("Spares:\n");
    446   1.1     oster 		for(i=0; i < device_config.nspares; i++) {
    447   1.5     oster 			printf("%20s: %s\n",
    448   1.1     oster 			       device_config.spares[i].devname,
    449   1.1     oster 			       device_status(device_config.spares[i].status));
    450   1.1     oster 		}
    451   1.1     oster 	} else {
    452   1.1     oster 		printf("No spares.\n");
    453   1.8     oster 	}
    454  1.14     oster 	for(i=0; i < device_config.ndevs; i++) {
    455  1.14     oster 		if (device_config.devs[i].status == rf_ds_optimal) {
    456  1.14     oster 			get_component_label(fd, device_config.devs[i].devname);
    457  1.14     oster 		} else {
    458  1.14     oster 			printf("%s status is: %s.  Skipping label.\n",
    459  1.14     oster 			       device_config.devs[i].devname,
    460  1.14     oster 			       device_status(device_config.devs[i].status));
    461  1.14     oster 		}
    462  1.14     oster 	}
    463  1.23     oster 
    464  1.21     oster 	if (device_config.nspares > 0) {
    465  1.21     oster 		for(i=0; i < device_config.nspares; i++) {
    466  1.21     oster 			if ((device_config.spares[i].status ==
    467  1.21     oster 			     rf_ds_optimal) ||
    468  1.21     oster 			    (device_config.spares[i].status ==
    469  1.21     oster 			     rf_ds_used_spare)) {
    470  1.21     oster 				get_component_label(fd,
    471  1.21     oster 					    device_config.spares[i].devname);
    472  1.21     oster 			} else {
    473  1.21     oster 				printf("%s status is: %s.  Skipping label.\n",
    474  1.21     oster 				       device_config.spares[i].devname,
    475  1.21     oster 				       device_status(device_config.spares[i].status));
    476  1.21     oster 			}
    477  1.21     oster 		}
    478  1.21     oster 	}
    479  1.23     oster 
    480   1.8     oster 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    481   1.8     oster 		 "RAIDFRAME_CHECK_PARITY");
    482   1.8     oster 	if (is_clean) {
    483   1.8     oster 		printf("Parity status: clean\n");
    484   1.8     oster 	} else {
    485   1.8     oster 		printf("Parity status: DIRTY\n");
    486   1.1     oster 	}
    487  1.14     oster 	check_status(fd,0);
    488   1.1     oster }
    489   1.1     oster 
    490   1.1     oster static void
    491  1.42       jld rf_output_pmstat(int fd, int raidID)
    492  1.42       jld {
    493  1.42       jld 	char srs[7];
    494  1.47    plunky 	unsigned int i, j;
    495  1.47    plunky 	int dis, dr;
    496  1.42       jld 	struct rf_pmstat st;
    497  1.42       jld 
    498  1.50     pooka 	if (prog_ioctl(fd, RAIDFRAME_PARITYMAP_STATUS, &st) == -1) {
    499  1.48       jld 		if (errno == EINVAL) {
    500  1.48       jld 			printf("raid%d: has no parity; parity map disabled\n",
    501  1.48       jld 				raidID);
    502  1.48       jld 			return;
    503  1.48       jld 		}
    504  1.48       jld 		err(1, "ioctl (%s) failed", "RAIDFRAME_PARITYMAP_STATUS");
    505  1.48       jld 	}
    506  1.48       jld 
    507  1.42       jld 	if (st.enabled) {
    508  1.42       jld 		if (0 > humanize_number(srs, 7, st.region_size * DEV_BSIZE,
    509  1.42       jld 			"B", HN_AUTOSCALE, HN_NOSPACE))
    510  1.42       jld 			strlcpy(srs, "???", 7);
    511  1.42       jld 
    512  1.42       jld 		printf("raid%d: parity map enabled with %u regions of %s\n",
    513  1.42       jld 		    raidID, st.params.regions, srs);
    514  1.43       jld 		printf("raid%d: regions marked clean after %d intervals of"
    515  1.42       jld 		    " %d.%03ds\n", raidID, st.params.cooldown,
    516  1.42       jld 		    st.params.tickms / 1000, st.params.tickms % 1000);
    517  1.42       jld 		printf("raid%d: write/sync/clean counters "
    518  1.42       jld 		    "%"PRIu64"/%"PRIu64"/%"PRIu64"\n", raidID,
    519  1.42       jld 		    st.ctrs.nwrite, st.ctrs.ncachesync, st.ctrs.nclearing);
    520  1.42       jld 
    521  1.42       jld 		dr = 0;
    522  1.46       jld 		for (i = 0; i < st.params.regions; i++)
    523  1.42       jld 			if (isset(st.dirty, i))
    524  1.42       jld 				dr++;
    525  1.42       jld 		printf("raid%d: %d dirty region%s\n", raidID, dr,
    526  1.42       jld 		    dr == 1 ? "" : "s");
    527  1.42       jld 
    528  1.42       jld 		if (verbose > 0) {
    529  1.42       jld 			for (i = 0; i < RF_PARITYMAP_NBYTE; i += 32) {
    530  1.42       jld 				printf("    ");
    531  1.42       jld 				for (j = i; j < RF_PARITYMAP_NBYTE
    532  1.42       jld 					 && j < i + 32; j++)
    533  1.42       jld 					printf("%x%x", st.dirty[j] & 15,
    534  1.42       jld 					    (st.dirty[j] >> 4) & 15);
    535  1.42       jld 				printf("\n");
    536  1.42       jld 			}
    537  1.42       jld 		}
    538  1.42       jld 	} else {
    539  1.42       jld 		printf("raid%d: parity map disabled\n", raidID);
    540  1.42       jld 	}
    541  1.42       jld 
    542  1.42       jld 	do_ioctl(fd, RAIDFRAME_PARITYMAP_GET_DISABLE, &dis,
    543  1.42       jld 	    "RAIDFRAME_PARITYMAP_GET_DISABLE");
    544  1.42       jld 	printf("raid%d: parity map will %s %sabled on next configure\n",
    545  1.42       jld 	    raidID, dis == st.enabled ? "be" : "remain", dis ? "dis" : "en");
    546  1.42       jld }
    547  1.42       jld 
    548  1.42       jld static void
    549  1.42       jld rf_pm_configure(int fd, int raidID, char *parityconf, int parityparams[])
    550  1.42       jld {
    551  1.42       jld 	int dis;
    552  1.42       jld 	struct rf_pmparams params;
    553  1.42       jld 
    554  1.42       jld 	if (strcasecmp(parityconf, "yes") == 0)
    555  1.42       jld 		dis = 0;
    556  1.42       jld 	else if (strcasecmp(parityconf, "no") == 0)
    557  1.42       jld 		dis = 1;
    558  1.42       jld 	else if (strcasecmp(parityconf, "set") == 0) {
    559  1.42       jld 		params.cooldown = parityparams[0];
    560  1.42       jld 		params.tickms = parityparams[1];
    561  1.42       jld 		params.regions = parityparams[2];
    562  1.42       jld 
    563  1.42       jld 		do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_PARAMS, &params,
    564  1.42       jld 		    "RAIDFRAME_PARITYMAP_SET_PARAMS");
    565  1.42       jld 
    566  1.42       jld 		if (params.cooldown != 0 || params.tickms != 0) {
    567  1.42       jld 			printf("raid%d: parity cleaned after", raidID);
    568  1.42       jld 			if (params.cooldown != 0)
    569  1.42       jld 				printf(" %d", params.cooldown);
    570  1.42       jld 			printf(" intervals");
    571  1.42       jld 			if (params.tickms != 0) {
    572  1.42       jld 				printf(" of %d.%03ds", params.tickms / 1000,
    573  1.42       jld 				    params.tickms % 1000);
    574  1.42       jld 			}
    575  1.42       jld 			printf("\n");
    576  1.42       jld 		}
    577  1.42       jld 		if (params.regions != 0)
    578  1.42       jld 			printf("raid%d: will use %d regions on next"
    579  1.42       jld 			    " configuration\n", raidID, params.regions);
    580  1.42       jld 
    581  1.42       jld 		return;
    582  1.42       jld 		/* XXX the control flow here could be prettier. */
    583  1.45  christos 	} else
    584  1.45  christos 		err(1, "`%s' is not a valid parity map command", parityconf);
    585  1.42       jld 
    586  1.42       jld 	do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_DISABLE, &dis,
    587  1.42       jld 	    "RAIDFRAME_PARITYMAP_SET_DISABLE");
    588  1.42       jld 	printf("raid%d: parity map will be %sabled on next configure\n",
    589  1.42       jld 	    raidID, dis ? "dis" : "en");
    590  1.42       jld }
    591  1.42       jld 
    592  1.62       mrg /* convert "component0" into "absent" */
    593  1.63       bad static const char *rf_output_devname(const char *name)
    594  1.62       mrg {
    595  1.62       mrg 
    596  1.63       bad 	if (strncmp(name, "component", 9) == 0)
    597  1.62       mrg 		return "absent";
    598  1.63       bad 	return name;
    599  1.62       mrg }
    600  1.42       jld 
    601  1.42       jld static void
    602  1.36   xtraeme rf_output_configuration(int fd, const char *name)
    603  1.27     lukem {
    604  1.27     lukem 	RF_DeviceConfig_t device_config;
    605  1.27     lukem 	void *cfg_ptr;
    606  1.27     lukem 	int i;
    607  1.27     lukem 	RF_ComponentLabel_t component_label;
    608  1.27     lukem 	void *label_ptr;
    609  1.27     lukem 	int component_num;
    610  1.27     lukem 	int num_cols;
    611  1.27     lukem 
    612  1.27     lukem 	cfg_ptr = &device_config;
    613  1.27     lukem 
    614  1.27     lukem 	printf("# raidctl config file for %s\n", name);
    615  1.27     lukem 	printf("\n");
    616  1.27     lukem 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
    617  1.27     lukem 
    618  1.66       mrg 	/*
    619  1.66       mrg 	 * After NetBSD 9, convert this to not output the numRow's value,
    620  1.66       mrg 	 * which is no longer required or ever used.
    621  1.66       mrg 	 */
    622  1.27     lukem 	printf("START array\n");
    623  1.27     lukem 	printf("# numRow numCol numSpare\n");
    624  1.66       mrg 	printf("%d %d %d\n", 1, device_config.cols,
    625  1.27     lukem 	    device_config.nspares);
    626  1.27     lukem 	printf("\n");
    627  1.27     lukem 
    628  1.27     lukem 	printf("START disks\n");
    629  1.27     lukem 	for(i=0; i < device_config.ndevs; i++)
    630  1.62       mrg 		printf("%s\n",
    631  1.62       mrg 		    rf_output_devname(device_config.devs[i].devname));
    632  1.27     lukem 	printf("\n");
    633  1.27     lukem 
    634  1.27     lukem 	if (device_config.nspares > 0) {
    635  1.27     lukem 		printf("START spare\n");
    636  1.27     lukem 		for(i=0; i < device_config.nspares; i++)
    637  1.27     lukem 			printf("%s\n", device_config.spares[i].devname);
    638  1.27     lukem 		printf("\n");
    639  1.27     lukem 	}
    640  1.27     lukem 
    641  1.27     lukem 	for(i=0; i < device_config.ndevs; i++) {
    642  1.27     lukem 		if (device_config.devs[i].status == rf_ds_optimal)
    643  1.27     lukem 			break;
    644  1.27     lukem 	}
    645  1.27     lukem 	if (i == device_config.ndevs) {
    646  1.27     lukem 		printf("# WARNING: no optimal components; using %s\n",
    647  1.27     lukem 		    device_config.devs[0].devname);
    648  1.27     lukem 		i = 0;
    649  1.27     lukem 	}
    650  1.27     lukem 	get_component_number(fd, device_config.devs[i].devname,
    651  1.27     lukem 	    &component_num, &num_cols);
    652  1.27     lukem 	memset(&component_label, 0, sizeof(RF_ComponentLabel_t));
    653  1.27     lukem 	component_label.row = component_num / num_cols;
    654  1.27     lukem 	component_label.column = component_num % num_cols;
    655  1.27     lukem 	label_ptr = &component_label;
    656  1.27     lukem 	do_ioctl(fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
    657  1.27     lukem 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    658  1.27     lukem 
    659  1.27     lukem 	printf("START layout\n");
    660  1.27     lukem 	printf(
    661  1.27     lukem 	    "# sectPerSU SUsPerParityUnit SUsPerReconUnit RAID_level_%c\n",
    662  1.27     lukem 	    (char) component_label.parityConfig);
    663  1.27     lukem 	printf("%d %d %d %c\n",
    664  1.27     lukem 	    component_label.sectPerSU, component_label.SUsPerPU,
    665  1.27     lukem 	    component_label.SUsPerRU, (char) component_label.parityConfig);
    666  1.27     lukem 	printf("\n");
    667  1.27     lukem 
    668  1.27     lukem 	printf("START queue\n");
    669  1.27     lukem 	printf("fifo %d\n", device_config.maxqdepth);
    670  1.27     lukem }
    671  1.27     lukem 
    672  1.27     lukem static void
    673  1.36   xtraeme get_component_number(int fd, char *component_name, int *component_number,
    674  1.36   xtraeme 		     int *num_columns)
    675   1.1     oster {
    676   1.1     oster 	RF_DeviceConfig_t device_config;
    677   1.1     oster 	void *cfg_ptr;
    678   1.1     oster 	int i;
    679   1.1     oster 	int found;
    680   1.1     oster 
    681   1.6     oster 	*component_number = -1;
    682   1.1     oster 
    683   1.1     oster 	/* Assuming a full path spec... */
    684   1.1     oster 	cfg_ptr = &device_config;
    685   1.1     oster 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    686   1.1     oster 		 "RAIDFRAME_GET_INFO");
    687   1.6     oster 
    688   1.6     oster 	*num_columns = device_config.cols;
    689   1.6     oster 
    690   1.1     oster 	found = 0;
    691   1.1     oster 	for(i=0; i < device_config.ndevs; i++) {
    692   1.6     oster 		if (strncmp(component_name, device_config.devs[i].devname,
    693   1.1     oster 			    PATH_MAX)==0) {
    694   1.1     oster 			found = 1;
    695   1.6     oster 			*component_number = i;
    696   1.1     oster 		}
    697   1.1     oster 	}
    698  1.21     oster 	if (!found) { /* maybe it's a spare? */
    699  1.21     oster 		for(i=0; i < device_config.nspares; i++) {
    700  1.21     oster 			if (strncmp(component_name,
    701  1.21     oster 				    device_config.spares[i].devname,
    702  1.21     oster 				    PATH_MAX)==0) {
    703  1.21     oster 				found = 1;
    704  1.21     oster 				*component_number = i + device_config.ndevs;
    705  1.23     oster 				/* the way spares are done should
    706  1.23     oster 				   really change... */
    707  1.23     oster 				*num_columns = device_config.cols +
    708  1.23     oster 					device_config.nspares;
    709  1.21     oster 			}
    710  1.21     oster 		}
    711  1.21     oster 	}
    712  1.21     oster 
    713  1.45  christos 	if (!found)
    714  1.45  christos 		err(1,"%s is not a component of this device", component_name);
    715   1.6     oster }
    716   1.6     oster 
    717   1.6     oster static void
    718  1.36   xtraeme rf_fail_disk(int fd, char *component_to_fail, int do_recon)
    719   1.6     oster {
    720   1.6     oster 	struct rf_recon_req recon_request;
    721   1.6     oster 	int component_num;
    722   1.6     oster 	int num_cols;
    723   1.6     oster 
    724   1.6     oster 	get_component_number(fd, component_to_fail, &component_num, &num_cols);
    725   1.1     oster 
    726   1.6     oster 	recon_request.col = component_num % num_cols;
    727   1.1     oster 	if (do_recon) {
    728   1.1     oster 		recon_request.flags = RF_FDFLAGS_RECON;
    729   1.1     oster 	} else {
    730   1.1     oster 		recon_request.flags = RF_FDFLAGS_NONE;
    731   1.1     oster 	}
    732   1.1     oster 	do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
    733   1.1     oster 		 "RAIDFRAME_FAIL_DISK");
    734  1.10     oster 	if (do_recon && verbose) {
    735  1.10     oster 		printf("Reconstruction status:\n");
    736  1.10     oster 		sleep(3); /* XXX give reconstruction a chance to start */
    737  1.19     oster 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
    738  1.10     oster 	}
    739   1.1     oster }
    740   1.1     oster 
    741   1.1     oster static void
    742  1.36   xtraeme get_component_label(int fd, char *component)
    743   1.5     oster {
    744   1.5     oster 	RF_ComponentLabel_t component_label;
    745   1.5     oster 	void *label_ptr;
    746   1.5     oster 	int component_num;
    747   1.6     oster 	int num_cols;
    748   1.5     oster 
    749   1.6     oster 	get_component_number(fd, component, &component_num, &num_cols);
    750   1.5     oster 
    751   1.5     oster 	memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
    752   1.6     oster 	component_label.row = component_num / num_cols;
    753   1.6     oster 	component_label.column = component_num % num_cols;
    754   1.5     oster 
    755   1.5     oster 	label_ptr = &component_label;
    756   1.5     oster 	do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
    757   1.5     oster 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    758   1.5     oster 
    759   1.5     oster 	printf("Component label for %s:\n",component);
    760  1.12     oster 
    761  1.25     lukem 	printf("   Row: %d, Column: %d, Num Rows: %d, Num Columns: %d\n",
    762  1.12     oster 	       component_label.row, component_label.column,
    763  1.12     oster 	       component_label.num_rows, component_label.num_columns);
    764  1.54       mrg 	printf("   Version: %d, Serial Number: %u, Mod Counter: %d\n",
    765  1.12     oster 	       component_label.version, component_label.serial_number,
    766  1.12     oster 	       component_label.mod_counter);
    767  1.25     lukem 	printf("   Clean: %s, Status: %d\n",
    768  1.12     oster 	       component_label.clean ? "Yes" : "No",
    769  1.12     oster 	       component_label.status );
    770  1.25     lukem 	printf("   sectPerSU: %d, SUsPerPU: %d, SUsPerRU: %d\n",
    771  1.12     oster 	       component_label.sectPerSU, component_label.SUsPerPU,
    772  1.12     oster 	       component_label.SUsPerRU);
    773  1.52     enami 	printf("   Queue size: %d, blocksize: %d, numBlocks: %"PRIu64"\n",
    774  1.25     lukem 	       component_label.maxOutstanding, component_label.blockSize,
    775  1.52     enami 	       rf_component_label_numblocks(&component_label));
    776  1.25     lukem 	printf("   RAID Level: %c\n", (char) component_label.parityConfig);
    777  1.12     oster 	printf("   Autoconfig: %s\n",
    778  1.12     oster 	       component_label.autoconfigure ? "Yes" : "No" );
    779  1.15     oster 	printf("   Root partition: %s\n",
    780  1.57  christos 	       rootpart[component_label.root_partition & 3]);
    781  1.12     oster 	printf("   Last configured as: raid%d\n", component_label.last_unit );
    782   1.5     oster }
    783   1.5     oster 
    784   1.5     oster static void
    785  1.36   xtraeme set_component_label(int fd, char *component)
    786   1.5     oster {
    787   1.5     oster 	RF_ComponentLabel_t component_label;
    788   1.5     oster 	int component_num;
    789   1.6     oster 	int num_cols;
    790   1.5     oster 
    791   1.6     oster 	get_component_number(fd, component, &component_num, &num_cols);
    792   1.6     oster 
    793   1.6     oster 	/* XXX This is currently here for testing, and future expandability */
    794   1.5     oster 
    795   1.5     oster 	component_label.version = 1;
    796   1.5     oster 	component_label.serial_number = 123456;
    797   1.5     oster 	component_label.mod_counter = 0;
    798   1.6     oster 	component_label.row = component_num / num_cols;
    799   1.6     oster 	component_label.column = component_num % num_cols;
    800   1.5     oster 	component_label.num_rows = 0;
    801   1.5     oster 	component_label.num_columns = 5;
    802   1.5     oster 	component_label.clean = 0;
    803   1.5     oster 	component_label.status = 1;
    804   1.5     oster 
    805   1.5     oster 	do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
    806   1.5     oster 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    807   1.5     oster }
    808   1.5     oster 
    809   1.5     oster 
    810   1.5     oster static void
    811  1.36   xtraeme init_component_labels(int fd, int serial_number)
    812   1.5     oster {
    813   1.5     oster 	RF_ComponentLabel_t component_label;
    814   1.5     oster 
    815   1.5     oster 	component_label.version = 0;
    816   1.5     oster 	component_label.serial_number = serial_number;
    817   1.5     oster 	component_label.mod_counter = 0;
    818   1.5     oster 	component_label.row = 0;
    819   1.5     oster 	component_label.column = 0;
    820   1.5     oster 	component_label.num_rows = 0;
    821   1.5     oster 	component_label.num_columns = 0;
    822   1.5     oster 	component_label.clean = 0;
    823   1.5     oster 	component_label.status = 0;
    824   1.5     oster 
    825   1.5     oster 	do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
    826   1.5     oster 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    827  1.12     oster }
    828  1.12     oster 
    829  1.12     oster static void
    830  1.36   xtraeme set_autoconfig(int fd, int raidID, char *autoconf)
    831  1.12     oster {
    832  1.12     oster 	int auto_config;
    833  1.12     oster 	int root_config;
    834  1.12     oster 
    835  1.12     oster 	auto_config = 0;
    836  1.12     oster 	root_config = 0;
    837  1.12     oster 
    838  1.57  christos 	if (strncasecmp(autoconf, "root", 4) == 0 ||
    839  1.57  christos 	    strncasecmp(autoconf, "hard", 4) == 0 ||
    840  1.61  sborrill 	    strncasecmp(autoconf, "force", 5) == 0) {
    841  1.12     oster 		root_config = 1;
    842  1.57  christos 	} else if (strncasecmp(autoconf, "soft", 4) == 0) {
    843  1.57  christos 		root_config = 2;
    844  1.12     oster 	}
    845  1.12     oster 
    846  1.12     oster 	if ((strncasecmp(autoconf,"yes", 3) == 0) ||
    847  1.57  christos 	    root_config > 0) {
    848  1.12     oster 		auto_config = 1;
    849  1.12     oster 	}
    850  1.12     oster 
    851  1.12     oster 	do_ioctl(fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config,
    852  1.12     oster 		 "RAIDFRAME_SET_AUTOCONFIG");
    853  1.12     oster 
    854  1.12     oster 	do_ioctl(fd, RAIDFRAME_SET_ROOT, &root_config,
    855  1.12     oster 		 "RAIDFRAME_SET_ROOT");
    856  1.12     oster 
    857  1.12     oster 	printf("raid%d: Autoconfigure: %s\n", raidID,
    858  1.12     oster 	       auto_config ? "Yes" : "No");
    859  1.12     oster 
    860  1.57  christos 	if (auto_config == 1) {
    861  1.57  christos 		printf("raid%d: Root: %s\n", raidID, rootpart[root_config]);
    862  1.12     oster 	}
    863   1.5     oster }
    864   1.5     oster 
    865   1.5     oster static void
    866  1.36   xtraeme add_hot_spare(int fd, char *component)
    867   1.5     oster {
    868   1.6     oster 	RF_SingleComponent_t hot_spare;
    869   1.5     oster 
    870   1.6     oster 	hot_spare.row = 0;
    871   1.6     oster 	hot_spare.column = 0;
    872   1.6     oster 	strncpy(hot_spare.component_name, component,
    873   1.6     oster 		sizeof(hot_spare.component_name));
    874   1.5     oster 
    875   1.5     oster 	do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
    876   1.5     oster 		  "RAIDFRAME_ADD_HOT_SPARE");
    877   1.6     oster }
    878   1.6     oster 
    879   1.6     oster static void
    880  1.36   xtraeme remove_hot_spare(int fd, char *component)
    881   1.6     oster {
    882   1.6     oster 	RF_SingleComponent_t hot_spare;
    883   1.6     oster 	int component_num;
    884   1.6     oster 	int num_cols;
    885   1.5     oster 
    886   1.6     oster 	get_component_number(fd, component, &component_num, &num_cols);
    887   1.6     oster 
    888   1.6     oster 	hot_spare.row = component_num / num_cols;
    889   1.6     oster 	hot_spare.column = component_num % num_cols;
    890   1.6     oster 
    891   1.6     oster 	strncpy(hot_spare.component_name, component,
    892   1.6     oster 		sizeof(hot_spare.component_name));
    893   1.6     oster 
    894   1.6     oster 	do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
    895   1.6     oster 		  "RAIDFRAME_REMOVE_HOT_SPARE");
    896   1.6     oster }
    897   1.6     oster 
    898   1.6     oster static void
    899  1.36   xtraeme rebuild_in_place(int fd, char *component)
    900   1.6     oster {
    901   1.6     oster 	RF_SingleComponent_t comp;
    902   1.6     oster 	int component_num;
    903   1.6     oster 	int num_cols;
    904   1.6     oster 
    905   1.6     oster 	get_component_number(fd, component, &component_num, &num_cols);
    906   1.6     oster 
    907   1.6     oster 	comp.row = 0;
    908   1.6     oster 	comp.column = component_num;
    909   1.6     oster 	strncpy(comp.component_name, component, sizeof(comp.component_name));
    910   1.6     oster 
    911   1.6     oster 	do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
    912   1.6     oster 		  "RAIDFRAME_REBUILD_IN_PLACE");
    913  1.10     oster 
    914  1.10     oster 	if (verbose) {
    915  1.10     oster 		printf("Reconstruction status:\n");
    916  1.10     oster 		sleep(3); /* XXX give reconstruction a chance to start */
    917  1.19     oster 		do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
    918  1.10     oster 	}
    919  1.10     oster 
    920  1.10     oster }
    921  1.10     oster 
    922  1.10     oster static void
    923  1.36   xtraeme check_parity(int fd, int do_rewrite, char *dev_name)
    924  1.10     oster {
    925  1.10     oster 	int is_clean;
    926  1.10     oster 	int percent_done;
    927  1.10     oster 
    928  1.10     oster 	is_clean = 0;
    929  1.10     oster 	percent_done = 0;
    930  1.10     oster 	do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
    931  1.10     oster 		 "RAIDFRAME_CHECK_PARITY");
    932  1.10     oster 	if (is_clean) {
    933  1.10     oster 		printf("%s: Parity status: clean\n",dev_name);
    934  1.10     oster 	} else {
    935  1.10     oster 		printf("%s: Parity status: DIRTY\n",dev_name);
    936  1.10     oster 		if (do_rewrite) {
    937  1.10     oster 			printf("%s: Initiating re-write of parity\n",
    938  1.10     oster 			       dev_name);
    939  1.10     oster 			do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    940  1.10     oster 				 "RAIDFRAME_REWRITEPARITY");
    941  1.10     oster 			sleep(3); /* XXX give it time to
    942  1.10     oster 				     get started. */
    943  1.10     oster 			if (verbose) {
    944  1.10     oster 				printf("Parity Re-write status:\n");
    945  1.19     oster 				do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
    946  1.10     oster 			} else {
    947  1.10     oster 				do_ioctl(fd,
    948  1.10     oster 					 RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    949  1.10     oster 					 &percent_done,
    950  1.10     oster 					 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"
    951  1.10     oster 					 );
    952  1.10     oster 				while( percent_done < 100 ) {
    953  1.24     oster 					sleep(3); /* wait a bit... */
    954  1.10     oster 					do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    955  1.10     oster 						 &percent_done, "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    956  1.10     oster 				}
    957  1.10     oster 
    958  1.10     oster 			}
    959  1.10     oster 			       printf("%s: Parity Re-write complete\n",
    960  1.10     oster 				      dev_name);
    961  1.10     oster 		} else {
    962  1.10     oster 			/* parity is wrong, and is not being fixed.
    963  1.10     oster 			   Exit w/ an error. */
    964  1.10     oster 			exit(1);
    965  1.10     oster 		}
    966  1.10     oster 	}
    967  1.10     oster }
    968  1.10     oster 
    969  1.10     oster 
    970  1.10     oster static void
    971  1.36   xtraeme check_status(int fd, int meter)
    972  1.10     oster {
    973  1.10     oster 	int recon_percent_done = 0;
    974  1.10     oster 	int parity_percent_done = 0;
    975  1.10     oster 	int copyback_percent_done = 0;
    976  1.10     oster 
    977  1.10     oster 	do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done,
    978  1.10     oster 		 "RAIDFRAME_CHECK_RECON_STATUS");
    979  1.10     oster 	printf("Reconstruction is %d%% complete.\n", recon_percent_done);
    980  1.10     oster 	do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
    981  1.10     oster 		 &parity_percent_done,
    982  1.10     oster 		 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
    983  1.10     oster 	printf("Parity Re-write is %d%% complete.\n", parity_percent_done);
    984  1.10     oster 	do_ioctl(fd, RAIDFRAME_CHECK_COPYBACK_STATUS, &copyback_percent_done,
    985  1.10     oster 		 "RAIDFRAME_CHECK_COPYBACK_STATUS");
    986  1.10     oster 	printf("Copyback is %d%% complete.\n", copyback_percent_done);
    987  1.10     oster 
    988  1.14     oster 	if (meter) {
    989  1.14     oster 		/* These 3 should be mutually exclusive at this point */
    990  1.14     oster 		if (recon_percent_done < 100) {
    991  1.14     oster 			printf("Reconstruction status:\n");
    992  1.19     oster 			do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
    993  1.14     oster 		} else if (parity_percent_done < 100) {
    994  1.14     oster 			printf("Parity Re-write status:\n");
    995  1.19     oster 			do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
    996  1.14     oster 		} else if (copyback_percent_done < 100) {
    997  1.14     oster 			printf("Copyback status:\n");
    998  1.19     oster 			do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS_EXT);
    999  1.14     oster 		}
   1000  1.10     oster 	}
   1001  1.10     oster }
   1002  1.10     oster 
   1003  1.18   thorpej const char *tbits = "|/-\\";
   1004  1.10     oster 
   1005  1.10     oster static void
   1006  1.36   xtraeme do_meter(int fd, u_long option)
   1007  1.10     oster {
   1008  1.10     oster 	int percent_done;
   1009  1.37     oster 	RF_uint64 start_value;
   1010  1.19     oster 	RF_ProgressInfo_t progressInfo;
   1011  1.19     oster 	void *pInfoPtr;
   1012  1.10     oster 	struct timeval start_time;
   1013  1.10     oster 	struct timeval current_time;
   1014  1.10     oster 	double elapsed;
   1015  1.10     oster 	int elapsed_sec;
   1016  1.10     oster 	int elapsed_usec;
   1017  1.20     oster 	int simple_eta,last_eta;
   1018  1.10     oster 	double rate;
   1019  1.37     oster 	RF_uint64 amount;
   1020  1.10     oster 	int tbit_value;
   1021  1.10     oster 	char bar_buffer[1024];
   1022  1.10     oster 	char eta_buffer[1024];
   1023  1.10     oster 
   1024  1.45  christos 	if (gettimeofday(&start_time,NULL) == -1)
   1025  1.45  christos 		err(1, "gettimeofday failed!?!?");
   1026  1.19     oster 	memset(&progressInfo, 0, sizeof(RF_ProgressInfo_t));
   1027  1.19     oster 	pInfoPtr=&progressInfo;
   1028  1.19     oster 
   1029  1.10     oster 	percent_done = 0;
   1030  1.19     oster 	do_ioctl(fd, option, &pInfoPtr, "");
   1031  1.37     oster 	start_value = progressInfo.completed;
   1032  1.10     oster 	current_time = start_time;
   1033  1.38     lukem 	simple_eta = 0;
   1034  1.38     lukem 	last_eta = 0;
   1035  1.10     oster 
   1036  1.10     oster 	tbit_value = 0;
   1037  1.19     oster 	while(progressInfo.completed < progressInfo.total) {
   1038  1.10     oster 
   1039  1.19     oster 		percent_done = (progressInfo.completed * 100) /
   1040  1.19     oster 			progressInfo.total;
   1041  1.10     oster 
   1042  1.10     oster 		get_bar(bar_buffer, percent_done, 40);
   1043  1.10     oster 
   1044  1.20     oster 		elapsed_sec = current_time.tv_sec - start_time.tv_sec;
   1045  1.20     oster 		elapsed_usec = current_time.tv_usec - start_time.tv_usec;
   1046  1.10     oster 		if (elapsed_usec < 0) {
   1047  1.10     oster 			elapsed_usec-=1000000;
   1048  1.10     oster 			elapsed_sec++;
   1049  1.10     oster 		}
   1050  1.20     oster 
   1051  1.10     oster 		elapsed = (double) elapsed_sec +
   1052  1.10     oster 			(double) elapsed_usec / 1000000.0;
   1053  1.19     oster 
   1054  1.20     oster 		amount = progressInfo.completed - start_value;
   1055  1.19     oster 
   1056  1.10     oster 		if (amount <= 0) { /* we don't do negatives (yet?) */
   1057  1.10     oster 			amount = 0;
   1058  1.10     oster 		}
   1059  1.20     oster 
   1060  1.22     oster 		if (elapsed == 0)
   1061  1.22     oster 			rate = 0.0;
   1062  1.22     oster 		else
   1063  1.22     oster 			rate = amount / elapsed;
   1064  1.10     oster 
   1065  1.10     oster 		if (rate > 0.0) {
   1066  1.19     oster 			simple_eta = (int) (((double)progressInfo.total -
   1067  1.20     oster 					     (double) progressInfo.completed)
   1068  1.19     oster 					    / rate);
   1069  1.10     oster 		} else {
   1070  1.10     oster 			simple_eta = -1;
   1071  1.10     oster 		}
   1072  1.19     oster 
   1073  1.10     oster 		if (simple_eta <=0) {
   1074  1.10     oster 			simple_eta = last_eta;
   1075  1.10     oster 		} else {
   1076  1.10     oster 			last_eta = simple_eta;
   1077  1.10     oster 		}
   1078  1.10     oster 
   1079  1.20     oster 		get_time_string(eta_buffer, simple_eta);
   1080  1.10     oster 
   1081  1.55  christos 		fprintf(stdout,"\r%3d%% |%s| ETA: %s %c",
   1082  1.55  christos 			percent_done,bar_buffer,eta_buffer,tbits[tbit_value]);
   1083  1.10     oster 		fflush(stdout);
   1084  1.10     oster 
   1085  1.10     oster 		if (++tbit_value>3)
   1086  1.10     oster 			tbit_value = 0;
   1087  1.10     oster 
   1088  1.10     oster 		sleep(2);
   1089  1.10     oster 
   1090  1.45  christos 		if (gettimeofday(&current_time,NULL) == -1)
   1091  1.45  christos 			err(1, "gettimeofday failed!?!?");
   1092  1.10     oster 
   1093  1.19     oster 		do_ioctl( fd, option, &pInfoPtr, "");
   1094  1.10     oster 
   1095  1.10     oster 
   1096  1.10     oster 	}
   1097  1.10     oster 	printf("\n");
   1098  1.10     oster }
   1099  1.10     oster /* 40 '*''s per line, then 40 ' ''s line. */
   1100  1.10     oster /* If you've got a screen wider than 160 characters, "tough" */
   1101  1.10     oster 
   1102  1.10     oster #define STAR_MIDPOINT 4*40
   1103  1.10     oster const char stars[] = "****************************************"
   1104  1.10     oster                      "****************************************"
   1105  1.10     oster                      "****************************************"
   1106  1.10     oster                      "****************************************"
   1107  1.10     oster                      "                                        "
   1108  1.10     oster                      "                                        "
   1109  1.10     oster                      "                                        "
   1110  1.10     oster                      "                                        "
   1111  1.10     oster                      "                                        ";
   1112  1.10     oster 
   1113  1.10     oster static void
   1114  1.36   xtraeme get_bar(char *string, double percent, int max_strlen)
   1115  1.10     oster {
   1116  1.10     oster 	int offset;
   1117  1.10     oster 
   1118  1.10     oster 	if (max_strlen > STAR_MIDPOINT) {
   1119  1.10     oster 		max_strlen = STAR_MIDPOINT;
   1120  1.10     oster 	}
   1121  1.10     oster 	offset = STAR_MIDPOINT -
   1122  1.10     oster 		(int)((percent * max_strlen)/ 100);
   1123  1.10     oster 	if (offset < 0)
   1124  1.10     oster 		offset = 0;
   1125  1.60     pooka 	snprintf(string,max_strlen,"%s",stars+offset);
   1126  1.10     oster }
   1127  1.10     oster 
   1128  1.10     oster static void
   1129  1.36   xtraeme get_time_string(char *string, int simple_time)
   1130  1.10     oster {
   1131  1.10     oster 	int minutes, seconds, hours;
   1132  1.10     oster 	char hours_buffer[5];
   1133  1.10     oster 	char minutes_buffer[5];
   1134  1.10     oster 	char seconds_buffer[5];
   1135  1.10     oster 
   1136  1.10     oster 	if (simple_time >= 0) {
   1137  1.10     oster 
   1138  1.10     oster 		minutes = (int) simple_time / 60;
   1139  1.10     oster 		seconds = ((int)simple_time - 60*minutes);
   1140  1.10     oster 		hours = minutes / 60;
   1141  1.10     oster 		minutes = minutes - 60*hours;
   1142  1.10     oster 
   1143  1.10     oster 		if (hours > 0) {
   1144  1.10     oster 			snprintf(hours_buffer,5,"%02d:",hours);
   1145  1.10     oster 		} else {
   1146  1.10     oster 			snprintf(hours_buffer,5,"   ");
   1147  1.10     oster 		}
   1148  1.10     oster 
   1149  1.10     oster 		snprintf(minutes_buffer,5,"%02d:",minutes);
   1150  1.10     oster 		snprintf(seconds_buffer,5,"%02d",seconds);
   1151  1.10     oster 		snprintf(string,1024,"%s%s%s",
   1152  1.10     oster 			 hours_buffer, minutes_buffer, seconds_buffer);
   1153  1.10     oster 	} else {
   1154  1.10     oster 		snprintf(string,1024,"   --:--");
   1155  1.10     oster 	}
   1156  1.10     oster 
   1157   1.5     oster }
   1158   1.5     oster 
   1159   1.5     oster static void
   1160  1.36   xtraeme usage(void)
   1161   1.1     oster {
   1162  1.26       cgd 	const char *progname = getprogname();
   1163  1.26       cgd 
   1164  1.65       wiz 	fprintf(stderr, "usage: %s [-v] -A [yes | no | softroot | hardroot] dev\n", progname);
   1165  1.65       wiz 	fprintf(stderr, "       %s [-v] -a component dev\n", progname);
   1166  1.26       cgd 	fprintf(stderr, "       %s [-v] -B dev\n", progname);
   1167  1.65       wiz 	fprintf(stderr, "       %s [-v] -C config_file dev\n", progname);
   1168  1.26       cgd 	fprintf(stderr, "       %s [-v] -c config_file dev\n", progname);
   1169  1.65       wiz 	fprintf(stderr, "       %s [-v] -F component dev\n", progname);
   1170  1.26       cgd 	fprintf(stderr, "       %s [-v] -f component dev\n", progname);
   1171  1.65       wiz 	fprintf(stderr, "       %s [-v] -G dev\n", progname);
   1172  1.26       cgd 	fprintf(stderr, "       %s [-v] -g component dev\n", progname);
   1173  1.65       wiz 	fprintf(stderr, "       %s [-v] -I serial_number dev\n", progname);
   1174  1.26       cgd 	fprintf(stderr, "       %s [-v] -i dev\n", progname);
   1175  1.42       jld 	fprintf(stderr, "       %s [-v] -M [yes | no | set params] dev\n",
   1176  1.42       jld 	    progname);
   1177  1.65       wiz 	fprintf(stderr, "       %s [-v] -m dev\n", progname);
   1178  1.65       wiz 	fprintf(stderr, "       %s [-v] -P dev\n", progname);
   1179  1.31     oster 	fprintf(stderr, "       %s [-v] -p dev\n", progname);
   1180  1.65       wiz 	fprintf(stderr, "       %s [-v] -R component dev\n", progname);
   1181  1.26       cgd 	fprintf(stderr, "       %s [-v] -r component dev\n", progname);
   1182  1.65       wiz 	fprintf(stderr, "       %s [-v] -S dev\n", progname);
   1183  1.26       cgd 	fprintf(stderr, "       %s [-v] -s dev\n", progname);
   1184  1.65       wiz 	fprintf(stderr, "       %s [-v] -U unit dev\n", progname);
   1185  1.26       cgd 	fprintf(stderr, "       %s [-v] -u dev\n", progname);
   1186   1.1     oster 	exit(1);
   1187   1.1     oster 	/* NOTREACHED */
   1188   1.1     oster }
   1189  1.58      manu 
   1190  1.58      manu static unsigned int
   1191  1.59  christos xstrtouint(const char *str)
   1192  1.58      manu {
   1193  1.59  christos 	int e;
   1194  1.59  christos 	unsigned int num = (unsigned int)strtou(str, NULL, 10, 0, INT_MAX, &e);
   1195  1.59  christos 	if (e)
   1196  1.59  christos 		errc(EXIT_FAILURE, e, "Bad number `%s'", str);
   1197  1.59  christos 	return num;
   1198  1.58      manu }
   1199