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