Home | History | Annotate | Line # | Download | only in raidctl
raidctl.c revision 1.3
      1  1.1   oster /*-
      2  1.1   oster  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      3  1.1   oster  * All rights reserved.
      4  1.1   oster  *
      5  1.1   oster  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1   oster  * by Greg Oster
      7  1.1   oster  *
      8  1.1   oster  * Redistribution and use in source and binary forms, with or without
      9  1.1   oster  * modification, are permitted provided that the following conditions
     10  1.1   oster  * are met:
     11  1.1   oster  * 1. Redistributions of source code must retain the above copyright
     12  1.1   oster  *    notice, this list of conditions and the following disclaimer.
     13  1.1   oster  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1   oster  *    notice, this list of conditions and the following disclaimer in the
     15  1.1   oster  *    documentation and/or other materials provided with the distribution.
     16  1.1   oster  * 3. All advertising materials mentioning features or use of this software
     17  1.1   oster  *    must display the following acknowledgement:
     18  1.1   oster  *        This product includes software developed by the NetBSD
     19  1.1   oster  *        Foundation, Inc. and its contributors.
     20  1.1   oster  * 4. Neither the name of The NetBSD Foundation nor the names of its
     21  1.1   oster  *    contributors may be used to endorse or promote products derived
     22  1.1   oster  *    from this software without specific prior written permission.
     23  1.1   oster  *
     24  1.1   oster  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.1   oster  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.1   oster  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.1   oster  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.1   oster  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.1   oster  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.1   oster  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.1   oster  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.1   oster  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.1   oster  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.1   oster  * POSSIBILITY OF SUCH DAMAGE.
     35  1.1   oster  */
     36  1.1   oster 
     37  1.1   oster /*
     38  1.1   oster 
     39  1.1   oster    This program is a re-write of the original rf_ctrl program
     40  1.1   oster    distributed by CMU with RAIDframe 1.1.
     41  1.1   oster 
     42  1.1   oster    This program is the user-land interface to the RAIDframe kernel
     43  1.1   oster    driver in NetBSD.
     44  1.1   oster 
     45  1.1   oster  */
     46  1.1   oster 
     47  1.1   oster #include <sys/param.h>
     48  1.1   oster #include <sys/ioctl.h>
     49  1.1   oster #include <sys/stat.h>
     50  1.1   oster #include <util.h>
     51  1.1   oster #include <stdio.h>
     52  1.1   oster #include <fcntl.h>
     53  1.1   oster #include <ctype.h>
     54  1.1   oster #include <err.h>
     55  1.2  mjacob #include <errno.h>
     56  1.1   oster #include <sys/types.h>
     57  1.1   oster #include <string.h>
     58  1.1   oster #include <sys/disklabel.h>
     59  1.1   oster #include <machine/disklabel.h>
     60  1.3   oster #include <unistd.h>
     61  1.3   oster 
     62  1.1   oster #include "rf_raidframe.h"
     63  1.1   oster 
     64  1.1   oster extern  char *__progname;
     65  1.1   oster 
     66  1.1   oster int     main __P((int, char *[]));
     67  1.1   oster static  void do_ioctl __P((int, unsigned long, void *, char *));
     68  1.1   oster static  void rf_configure __P((int, char*));
     69  1.1   oster static  char *device_status __P((RF_DiskStatus_t));
     70  1.1   oster static  void rf_get_device_status __P((int));
     71  1.1   oster static  void rf_fail_disk __P((int, char *, int));
     72  1.1   oster static  void usage __P((void));
     73  1.1   oster 
     74  1.1   oster int
     75  1.1   oster main(argc,argv)
     76  1.1   oster 	int argc;
     77  1.1   oster 	char *argv[];
     78  1.1   oster {
     79  1.1   oster 	extern char *optarg;
     80  1.1   oster 	extern int optind;
     81  1.1   oster 	int ch;
     82  1.1   oster 	int num_options;
     83  1.1   oster 	unsigned long action;
     84  1.1   oster 	char config_filename[PATH_MAX];
     85  1.1   oster 	char dev_name[PATH_MAX];
     86  1.1   oster 	char name[PATH_MAX];
     87  1.1   oster 	char component_to_fail[PATH_MAX];
     88  1.1   oster 	int do_recon;
     89  1.1   oster 	int raidID;
     90  1.1   oster 	int rawpart;
     91  1.1   oster 	int recon_percent_done;
     92  1.1   oster 	struct stat st;
     93  1.1   oster 	int fd;
     94  1.1   oster 
     95  1.1   oster 	num_options = 0;
     96  1.1   oster 	action = 0;
     97  1.1   oster 	do_recon = 0;
     98  1.1   oster 
     99  1.1   oster 	while ((ch = getopt(argc, argv, "c:Cf:F:rRsu")) != -1)
    100  1.1   oster 		switch(ch) {
    101  1.1   oster 		case 'c':
    102  1.1   oster 			strncpy(config_filename,optarg,PATH_MAX);
    103  1.1   oster 			action = RAIDFRAME_CONFIGURE;
    104  1.1   oster 			num_options++;
    105  1.1   oster 			break;
    106  1.1   oster 		case 'C':
    107  1.1   oster 			action = RAIDFRAME_COPYBACK;
    108  1.1   oster 			num_options++;
    109  1.1   oster 			break;
    110  1.1   oster 		case 'f':
    111  1.1   oster 			action = RAIDFRAME_FAIL_DISK;
    112  1.1   oster 			do_recon = 0;
    113  1.1   oster 			strncpy(component_to_fail, optarg, PATH_MAX);
    114  1.1   oster 			num_options++;
    115  1.1   oster 			break;
    116  1.1   oster 		case 'F':
    117  1.1   oster 			action = RAIDFRAME_FAIL_DISK;
    118  1.1   oster 			do_recon = 1;
    119  1.1   oster 			strncpy(component_to_fail, optarg, PATH_MAX);
    120  1.1   oster 			num_options++;
    121  1.1   oster 			break;
    122  1.1   oster 		case 'r':
    123  1.1   oster 			action = RAIDFRAME_REWRITEPARITY;
    124  1.1   oster 			num_options++;
    125  1.1   oster 			break;
    126  1.1   oster 		case 'R':
    127  1.1   oster 			action = RAIDFRAME_CHECKRECON;
    128  1.1   oster 			num_options++;
    129  1.1   oster 			break;
    130  1.1   oster 		case 's':
    131  1.1   oster 			action = RAIDFRAME_GET_INFO;
    132  1.1   oster 			num_options++;
    133  1.1   oster 			break;
    134  1.1   oster 		case 'u':
    135  1.1   oster 			action = RAIDFRAME_SHUTDOWN;
    136  1.1   oster 			num_options++;
    137  1.1   oster 			break;
    138  1.1   oster 		default:
    139  1.1   oster 			usage();
    140  1.1   oster 		}
    141  1.1   oster 	argc -= optind;
    142  1.1   oster 	argv += optind;
    143  1.1   oster 
    144  1.1   oster 	if ((num_options > 1) || (argc == NULL))
    145  1.1   oster 		usage();
    146  1.1   oster 
    147  1.1   oster 	strncpy(name,argv[0],PATH_MAX);
    148  1.1   oster 
    149  1.1   oster 	if ((name[0] == '/') || (name[0] == '.')) {
    150  1.1   oster 		/* they've (apparently) given a full path... */
    151  1.1   oster 		strncpy(dev_name, name, PATH_MAX);
    152  1.1   oster 	} else {
    153  1.1   oster 		if (isdigit(name[strlen(name)-1])) {
    154  1.1   oster 			rawpart = getrawpartition();
    155  1.1   oster 			snprintf(dev_name,PATH_MAX,"/dev/%s%c",name,
    156  1.1   oster 				 'a'+rawpart);
    157  1.1   oster 		} else {
    158  1.1   oster 			snprintf(dev_name,PATH_MAX,"/dev/%s",name);
    159  1.1   oster 		}
    160  1.1   oster 	}
    161  1.1   oster 
    162  1.1   oster 	if (stat(dev_name, &st) != 0) {
    163  1.1   oster 		fprintf(stderr,"%s: stat failure on: %s\n",
    164  1.1   oster 			__progname,dev_name);
    165  1.1   oster 		return (errno);
    166  1.1   oster 	}
    167  1.1   oster 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) {
    168  1.1   oster 		fprintf(stderr,"%s: invalid device: %s\n",
    169  1.1   oster 			__progname,dev_name);
    170  1.1   oster 		return (EINVAL);
    171  1.1   oster 	}
    172  1.1   oster 
    173  1.1   oster 	raidID = RF_DEV2RAIDID(st.st_rdev);
    174  1.1   oster 
    175  1.1   oster 	if ((fd = open( dev_name, O_RDWR, 0640)) < 0) {
    176  1.1   oster 		fprintf(stderr, "%s: unable to open device file: %s\n",
    177  1.1   oster 			__progname, dev_name);
    178  1.1   oster 		exit(1);
    179  1.1   oster 	}
    180  1.1   oster 
    181  1.1   oster 
    182  1.1   oster 	switch(action) {
    183  1.1   oster 	case RAIDFRAME_CONFIGURE:
    184  1.1   oster 		rf_configure(fd, config_filename);
    185  1.1   oster 		break;
    186  1.1   oster 	case RAIDFRAME_COPYBACK:
    187  1.1   oster 		printf("Copyback.\n");
    188  1.1   oster 		do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
    189  1.1   oster 		break;
    190  1.1   oster 	case RAIDFRAME_FAIL_DISK:
    191  1.1   oster 		rf_fail_disk(fd,component_to_fail,do_recon);
    192  1.1   oster 		break;
    193  1.1   oster 	case RAIDFRAME_REWRITEPARITY:
    194  1.1   oster 		printf("Initiating re-write of parity\n");
    195  1.1   oster 		do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    196  1.1   oster 			 "RAIDFRAME_REWRITEPARITY");
    197  1.1   oster 		break;
    198  1.1   oster 	case RAIDFRAME_CHECKRECON:
    199  1.1   oster 		do_ioctl(fd, RAIDFRAME_CHECKRECON, &recon_percent_done,
    200  1.1   oster 			 "RAIDFRAME_CHECKRECON");
    201  1.1   oster 		printf("Reconstruction is %d%% complete.\n",
    202  1.1   oster 		       recon_percent_done);
    203  1.1   oster 		break;
    204  1.1   oster 	case RAIDFRAME_GET_INFO:
    205  1.1   oster 		rf_get_device_status(fd);
    206  1.1   oster 		break;
    207  1.1   oster 	case RAIDFRAME_SHUTDOWN:
    208  1.1   oster 		do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
    209  1.1   oster 		break;
    210  1.1   oster 	default:
    211  1.1   oster 		break;
    212  1.1   oster 	}
    213  1.1   oster 
    214  1.1   oster 	close(fd);
    215  1.1   oster 	exit(0);
    216  1.1   oster }
    217  1.1   oster 
    218  1.1   oster static void
    219  1.1   oster do_ioctl(fd, command, arg, ioctl_name)
    220  1.1   oster 	int fd;
    221  1.1   oster 	unsigned long command;
    222  1.1   oster 	void *arg;
    223  1.1   oster 	char *ioctl_name;
    224  1.1   oster {
    225  1.1   oster 	if (ioctl(fd, command, arg) < 0) {
    226  1.1   oster 		warn("ioctl (%s) failed", ioctl_name);
    227  1.1   oster 		exit(1);
    228  1.1   oster 	}
    229  1.1   oster }
    230  1.1   oster 
    231  1.1   oster 
    232  1.1   oster static void
    233  1.1   oster rf_configure(fd,config_file)
    234  1.1   oster 	int fd;
    235  1.1   oster 	char *config_file;
    236  1.1   oster {
    237  1.1   oster 	void *generic;
    238  1.1   oster 	RF_Config_t cfg;
    239  1.1   oster 
    240  1.1   oster 	if (rf_MakeConfig( config_file, &cfg ) < 0) {
    241  1.1   oster 		fprintf(stderr,"%s: unable to create RAIDframe %s\n",
    242  1.1   oster 			__progname, "configuration structure\n");
    243  1.1   oster 		exit(1);
    244  1.1   oster 	}
    245  1.1   oster 
    246  1.1   oster 	/*
    247  1.1   oster 
    248  1.1   oster 	   Note the extra level of redirection needed here, since
    249  1.1   oster 	   what we really want to pass in is a pointer to the pointer to
    250  1.1   oster 	   the configuration structure.
    251  1.1   oster 
    252  1.1   oster 	 */
    253  1.1   oster 
    254  1.1   oster 	generic = (void *) &cfg;
    255  1.1   oster 	do_ioctl(fd,RAIDFRAME_CONFIGURE,&generic,"RAIDFRAME_CONFIGURE");
    256  1.1   oster #if 0
    257  1.1   oster 	if (ioctl(fd, RAIDFRAME_CONFIGURE, &generic) < 0) {
    258  1.1   oster 		warn("ioctl (RAIDFRAME_CONFIGURE): failed\n");
    259  1.1   oster 		exit(1);
    260  1.1   oster 	}
    261  1.1   oster #endif
    262  1.1   oster }
    263  1.1   oster 
    264  1.1   oster static char *
    265  1.1   oster device_status(status)
    266  1.1   oster 	RF_DiskStatus_t status;
    267  1.1   oster {
    268  1.1   oster 	static char status_string[256];
    269  1.1   oster 
    270  1.1   oster 	switch (status) {
    271  1.1   oster 	case rf_ds_optimal:
    272  1.1   oster 		strcpy(status_string,"optimal");
    273  1.1   oster 		break;
    274  1.1   oster 	case rf_ds_failed:
    275  1.1   oster 		strcpy(status_string,"failed");
    276  1.1   oster 		break;
    277  1.1   oster 	case rf_ds_reconstructing:
    278  1.1   oster 		strcpy(status_string,"reconstructing");
    279  1.1   oster 		break;
    280  1.1   oster 	case rf_ds_dist_spared:
    281  1.1   oster 		strcpy(status_string,"dist_spared");
    282  1.1   oster 		break;
    283  1.1   oster 	case rf_ds_spared:
    284  1.1   oster 		strcpy(status_string,"spared");
    285  1.1   oster 		break;
    286  1.1   oster 	case rf_ds_spare:
    287  1.1   oster 		strcpy(status_string,"spare");
    288  1.1   oster 		break;
    289  1.1   oster 	case rf_ds_used_spare:
    290  1.1   oster 		strcpy(status_string,"used_spare");
    291  1.1   oster 		break;
    292  1.1   oster 	default:
    293  1.1   oster 		strcpy(status_string,"UNKNOWN");
    294  1.1   oster 		break;
    295  1.1   oster 	}
    296  1.1   oster 	return(status_string);
    297  1.1   oster }
    298  1.1   oster 
    299  1.1   oster static void
    300  1.1   oster rf_get_device_status(fd)
    301  1.1   oster 	int fd;
    302  1.1   oster {
    303  1.1   oster 	RF_DeviceConfig_t device_config;
    304  1.1   oster 	void *cfg_ptr;
    305  1.1   oster 	int i;
    306  1.1   oster 
    307  1.1   oster 	cfg_ptr = &device_config;
    308  1.1   oster 
    309  1.1   oster 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
    310  1.1   oster 
    311  1.1   oster 	printf("Components:\n");
    312  1.1   oster 	for(i=0; i < device_config.ndevs; i++) {
    313  1.1   oster 		printf("%20s: %s\n", device_config.devs[i].devname,
    314  1.1   oster 		       device_status(device_config.devs[i].status));
    315  1.1   oster 	}
    316  1.1   oster 	if (device_config.nspares > 0) {
    317  1.1   oster 		printf("Spares:\n");
    318  1.1   oster 		for(i=0; i < device_config.nspares; i++) {
    319  1.1   oster 			printf("%20s [%d][%d]: %s\n",
    320  1.1   oster 			       device_config.spares[i].devname,
    321  1.1   oster 			       device_config.spares[i].spareRow,
    322  1.1   oster  			       device_config.spares[i].spareCol,
    323  1.1   oster 			       device_status(device_config.spares[i].status));
    324  1.1   oster 		}
    325  1.1   oster 	} else {
    326  1.1   oster 		printf("No spares.\n");
    327  1.1   oster 	}
    328  1.1   oster 
    329  1.1   oster }
    330  1.1   oster 
    331  1.1   oster static void
    332  1.1   oster rf_fail_disk(fd, component_to_fail, do_recon)
    333  1.1   oster 	int fd;
    334  1.1   oster 	char *component_to_fail;
    335  1.1   oster 	int do_recon;
    336  1.1   oster {
    337  1.1   oster 	struct rf_recon_req recon_request;
    338  1.1   oster 	RF_DeviceConfig_t device_config;
    339  1.1   oster 	void *cfg_ptr;
    340  1.1   oster 	int i;
    341  1.1   oster 	int found;
    342  1.1   oster 	int component_num;
    343  1.1   oster 
    344  1.1   oster 	component_num = -1;
    345  1.1   oster 
    346  1.1   oster 	/* Assuming a full path spec... */
    347  1.1   oster 	cfg_ptr = &device_config;
    348  1.1   oster 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    349  1.1   oster 		 "RAIDFRAME_GET_INFO");
    350  1.1   oster 	found = 0;
    351  1.1   oster 	for(i=0; i < device_config.ndevs; i++) {
    352  1.1   oster 		if (strncmp(component_to_fail,
    353  1.1   oster 			    device_config.devs[i].devname,
    354  1.1   oster 			    PATH_MAX)==0) {
    355  1.1   oster 			found = 1;
    356  1.1   oster 			component_num = i;
    357  1.1   oster 		}
    358  1.1   oster 	}
    359  1.1   oster 	if (!found) {
    360  1.1   oster 		fprintf(stderr,"%s: %s is not a component %s",
    361  1.1   oster 			__progname, component_to_fail,
    362  1.1   oster 			"of this device\n");
    363  1.1   oster 		exit(1);
    364  1.1   oster 	}
    365  1.1   oster 
    366  1.1   oster 	recon_request.row = component_num / device_config.cols;
    367  1.1   oster 	recon_request.col = component_num % device_config.cols;
    368  1.1   oster 	if (do_recon) {
    369  1.1   oster 		recon_request.flags = RF_FDFLAGS_RECON;
    370  1.1   oster 	} else {
    371  1.1   oster 		recon_request.flags = RF_FDFLAGS_NONE;
    372  1.1   oster 	}
    373  1.1   oster 	do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
    374  1.1   oster 		 "RAIDFRAME_FAIL_DISK");
    375  1.1   oster 
    376  1.1   oster }
    377  1.1   oster 
    378  1.1   oster static void
    379  1.1   oster usage()
    380  1.1   oster {
    381  1.1   oster 	fprintf(stderr, "usage: %s -c config_file dev\n", __progname);
    382  1.1   oster 	fprintf(stderr, "       %s -C dev\n", __progname);
    383  1.1   oster 	fprintf(stderr, "       %s -f component dev\n", __progname);
    384  1.1   oster 	fprintf(stderr, "       %s -F component dev\n", __progname);
    385  1.1   oster 	fprintf(stderr, "       %s -r dev\n", __progname);
    386  1.1   oster 	fprintf(stderr, "       %s -R dev\n", __progname);
    387  1.1   oster 	fprintf(stderr, "       %s -s dev\n", __progname);
    388  1.1   oster 	fprintf(stderr, "       %s -u dev\n", __progname);
    389  1.1   oster 	exit(1);
    390  1.1   oster 	/* NOTREACHED */
    391  1.1   oster }
    392