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