Home | History | Annotate | Line # | Download | only in raidctl
raidctl.c revision 1.5
      1 /*      $NetBSD: raidctl.c,v 1.5 1999/02/24 00:03:12 oster Exp $   */
      2 /*-
      3  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Greg Oster
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39 
     40    This program is a re-write of the original rf_ctrl program
     41    distributed by CMU with RAIDframe 1.1.
     42 
     43    This program is the user-land interface to the RAIDframe kernel
     44    driver in NetBSD.
     45 
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/stat.h>
     51 #include <util.h>
     52 #include <stdio.h>
     53 #include <fcntl.h>
     54 #include <ctype.h>
     55 #include <err.h>
     56 #include <errno.h>
     57 #include <sys/types.h>
     58 #include <string.h>
     59 #include <sys/disklabel.h>
     60 #include <machine/disklabel.h>
     61 #include <stdlib.h>
     62 #include <unistd.h>
     63 
     64 #include "rf_raidframe.h"
     65 
     66 extern  char *__progname;
     67 
     68 int     main __P((int, char *[]));
     69 static  void do_ioctl __P((int, unsigned long, void *, char *));
     70 static  void rf_configure __P((int, char*, int));
     71 static  char *device_status __P((RF_DiskStatus_t));
     72 static  void rf_get_device_status __P((int));
     73 static  void rf_fail_disk __P((int, char *, int));
     74 static  void usage __P((void));
     75 static  void get_component_label __P((int, char *));
     76 static  void set_component_label __P((int, char *));
     77 static  void init_component_labels __P((int, int));
     78 static  void add_hot_spare __P((int, char *));
     79 
     80 int
     81 main(argc,argv)
     82 	int argc;
     83 	char *argv[];
     84 {
     85 	extern char *optarg;
     86 	extern int optind;
     87 	int ch;
     88 	int num_options;
     89 	unsigned long action;
     90 	char config_filename[PATH_MAX];
     91 	char dev_name[PATH_MAX];
     92 	char name[PATH_MAX];
     93 	char component[PATH_MAX];
     94 	int do_recon;
     95 	int raidID;
     96 	int rawpart;
     97 	int recon_percent_done;
     98 	int serial_number;
     99 	struct stat st;
    100 	int fd;
    101 	int force;
    102 
    103 	num_options = 0;
    104 	action = 0;
    105 	do_recon = 0;
    106 	force = 0;
    107 
    108 #ifdef NOT_YET_BOYS_AND_GIRLS
    109 	while ((ch = getopt(argc, argv, "a:c:Cf:F:g:I:l:rRsuX:")) != -1)
    110 #endif
    111 	while ((ch = getopt(argc, argv, "c:Cf:F:rRsu")) != -1)
    112 		switch(ch) {
    113 		case 'a':
    114 			action = RAIDFRAME_ADD_HOT_SPARE;
    115 			strncpy(component, optarg, PATH_MAX);
    116 			num_options++;
    117 			break;
    118 		case 'c':
    119 			strncpy(config_filename,optarg,PATH_MAX);
    120 			action = RAIDFRAME_CONFIGURE;
    121 			num_options++;
    122 			break;
    123 		case 'C':
    124 			action = RAIDFRAME_COPYBACK;
    125 			num_options++;
    126 			break;
    127 		case 'f':
    128 			action = RAIDFRAME_FAIL_DISK;
    129 			do_recon = 0;
    130 			strncpy(component, optarg, PATH_MAX);
    131 			num_options++;
    132 			break;
    133 		case 'F':
    134 			action = RAIDFRAME_FAIL_DISK;
    135 			do_recon = 1;
    136 			strncpy(component, optarg, PATH_MAX);
    137 			num_options++;
    138 			break;
    139 		case 'g':
    140 			action = RAIDFRAME_GET_COMPONENT_LABEL;
    141 			strncpy(component, optarg, PATH_MAX);
    142 			num_options++;
    143 			break;
    144 		case 'I':
    145 			action = RAIDFRAME_INIT_LABELS;
    146 			serial_number = atoi(optarg);
    147 			num_options++;
    148 			break;
    149 		case 'l':
    150 			action = RAIDFRAME_SET_COMPONENT_LABEL;
    151 			strncpy(component, optarg, PATH_MAX);
    152 			num_options++;
    153 			break;
    154 		case 'r':
    155 			action = RAIDFRAME_REWRITEPARITY;
    156 			num_options++;
    157 			break;
    158 		case 'R':
    159 			action = RAIDFRAME_CHECKRECON;
    160 			num_options++;
    161 			break;
    162 		case 's':
    163 			action = RAIDFRAME_GET_INFO;
    164 			num_options++;
    165 			break;
    166 		case 'u':
    167 			action = RAIDFRAME_SHUTDOWN;
    168 			num_options++;
    169 			break;
    170 		case 'X':
    171 			strncpy(config_filename,optarg,PATH_MAX);
    172 			action = RAIDFRAME_CONFIGURE;
    173 			force = 1;
    174 			num_options++;
    175 			break;
    176 		default:
    177 			usage();
    178 		}
    179 	argc -= optind;
    180 	argv += optind;
    181 
    182 	if ((num_options > 1) || (argc == NULL))
    183 		usage();
    184 
    185 	strncpy(name,argv[0],PATH_MAX);
    186 
    187 	if ((name[0] == '/') || (name[0] == '.')) {
    188 		/* they've (apparently) given a full path... */
    189 		strncpy(dev_name, name, PATH_MAX);
    190 	} else {
    191 		if (isdigit(name[strlen(name)-1])) {
    192 			rawpart = getrawpartition();
    193 			snprintf(dev_name,PATH_MAX,"/dev/%s%c",name,
    194 				 'a'+rawpart);
    195 		} else {
    196 			snprintf(dev_name,PATH_MAX,"/dev/%s",name);
    197 		}
    198 	}
    199 
    200 	if (stat(dev_name, &st) != 0) {
    201 		fprintf(stderr,"%s: stat failure on: %s\n",
    202 			__progname,dev_name);
    203 		return (errno);
    204 	}
    205 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) {
    206 		fprintf(stderr,"%s: invalid device: %s\n",
    207 			__progname,dev_name);
    208 		return (EINVAL);
    209 	}
    210 
    211 	raidID = RF_DEV2RAIDID(st.st_rdev);
    212 
    213 	if ((fd = open( dev_name, O_RDWR, 0640)) < 0) {
    214 		fprintf(stderr, "%s: unable to open device file: %s\n",
    215 			__progname, dev_name);
    216 		exit(1);
    217 	}
    218 
    219 
    220 	switch(action) {
    221 	case RAIDFRAME_ADD_HOT_SPARE:
    222 		add_hot_spare(fd,component);
    223 		break;
    224 	case RAIDFRAME_CONFIGURE:
    225 		rf_configure(fd, config_filename,force);
    226 		break;
    227 	case RAIDFRAME_COPYBACK:
    228 		printf("Copyback.\n");
    229 		do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
    230 		break;
    231 	case RAIDFRAME_FAIL_DISK:
    232 		rf_fail_disk(fd,component,do_recon);
    233 		break;
    234 	case RAIDFRAME_SET_COMPONENT_LABEL:
    235 		set_component_label(fd,component);
    236 		break;
    237 	case RAIDFRAME_GET_COMPONENT_LABEL:
    238 		get_component_label(fd,component);
    239 		break;
    240 	case RAIDFRAME_INIT_LABELS:
    241 		init_component_labels(fd,serial_number);
    242 		break;
    243 	case RAIDFRAME_REWRITEPARITY:
    244 		printf("Initiating re-write of parity\n");
    245 		do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
    246 			 "RAIDFRAME_REWRITEPARITY");
    247 		break;
    248 	case RAIDFRAME_CHECKRECON:
    249 		do_ioctl(fd, RAIDFRAME_CHECKRECON, &recon_percent_done,
    250 			 "RAIDFRAME_CHECKRECON");
    251 		printf("Reconstruction is %d%% complete.\n",
    252 		       recon_percent_done);
    253 		break;
    254 	case RAIDFRAME_GET_INFO:
    255 		rf_get_device_status(fd);
    256 		break;
    257 	case RAIDFRAME_SHUTDOWN:
    258 		do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
    259 		break;
    260 	default:
    261 		break;
    262 	}
    263 
    264 	close(fd);
    265 	exit(0);
    266 }
    267 
    268 static void
    269 do_ioctl(fd, command, arg, ioctl_name)
    270 	int fd;
    271 	unsigned long command;
    272 	void *arg;
    273 	char *ioctl_name;
    274 {
    275 	if (ioctl(fd, command, arg) < 0) {
    276 		warn("ioctl (%s) failed", ioctl_name);
    277 		exit(1);
    278 	}
    279 }
    280 
    281 
    282 static void
    283 rf_configure(fd,config_file,force)
    284 	int fd;
    285 	char *config_file;
    286 	int force;
    287 {
    288 	void *generic;
    289 	RF_Config_t cfg;
    290 
    291 	if (rf_MakeConfig( config_file, &cfg ) < 0) {
    292 		fprintf(stderr,"%s: unable to create RAIDframe %s\n",
    293 			__progname, "configuration structure\n");
    294 		exit(1);
    295 	}
    296 
    297 	/*
    298 
    299 	   Note the extra level of redirection needed here, since
    300 	   what we really want to pass in is a pointer to the pointer to
    301 	   the configuration structure.
    302 
    303 	 */
    304 
    305 	generic = (void *) &cfg;
    306 	do_ioctl(fd,RAIDFRAME_CONFIGURE,&generic,"RAIDFRAME_CONFIGURE");
    307 
    308 }
    309 
    310 static char *
    311 device_status(status)
    312 	RF_DiskStatus_t status;
    313 {
    314 	static char status_string[256];
    315 
    316 	switch (status) {
    317 	case rf_ds_optimal:
    318 		strcpy(status_string,"optimal");
    319 		break;
    320 	case rf_ds_failed:
    321 		strcpy(status_string,"failed");
    322 		break;
    323 	case rf_ds_reconstructing:
    324 		strcpy(status_string,"reconstructing");
    325 		break;
    326 	case rf_ds_dist_spared:
    327 		strcpy(status_string,"dist_spared");
    328 		break;
    329 	case rf_ds_spared:
    330 		strcpy(status_string,"spared");
    331 		break;
    332 	case rf_ds_spare:
    333 		strcpy(status_string,"spare");
    334 		break;
    335 	case rf_ds_used_spare:
    336 		strcpy(status_string,"used_spare");
    337 		break;
    338 	default:
    339 		strcpy(status_string,"UNKNOWN");
    340 		break;
    341 	}
    342 	return(status_string);
    343 }
    344 
    345 static void
    346 rf_get_device_status(fd)
    347 	int fd;
    348 {
    349 	RF_DeviceConfig_t device_config;
    350 	void *cfg_ptr;
    351 	int i;
    352 
    353 	cfg_ptr = &device_config;
    354 
    355 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
    356 
    357 	printf("Components:\n");
    358 	for(i=0; i < device_config.ndevs; i++) {
    359 		printf("%20s: %s\n", device_config.devs[i].devname,
    360 		       device_status(device_config.devs[i].status));
    361 	}
    362 	if (device_config.nspares > 0) {
    363 		printf("Spares:\n");
    364 		for(i=0; i < device_config.nspares; i++) {
    365 			printf("%20s: %s\n",
    366 			       device_config.spares[i].devname,
    367 			       device_status(device_config.spares[i].status));
    368 		}
    369 	} else {
    370 		printf("No spares.\n");
    371 	}
    372 
    373 }
    374 
    375 static void
    376 rf_fail_disk(fd, component_to_fail, do_recon)
    377 	int fd;
    378 	char *component_to_fail;
    379 	int do_recon;
    380 {
    381 	struct rf_recon_req recon_request;
    382 	RF_DeviceConfig_t device_config;
    383 	void *cfg_ptr;
    384 	int i;
    385 	int found;
    386 	int component_num;
    387 
    388 	component_num = -1;
    389 
    390 	/* Assuming a full path spec... */
    391 	cfg_ptr = &device_config;
    392 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    393 		 "RAIDFRAME_GET_INFO");
    394 	found = 0;
    395 	for(i=0; i < device_config.ndevs; i++) {
    396 		if (strncmp(component_to_fail,
    397 			    device_config.devs[i].devname,
    398 			    PATH_MAX)==0) {
    399 			found = 1;
    400 			component_num = i;
    401 		}
    402 	}
    403 	if (!found) {
    404 		fprintf(stderr,"%s: %s is not a component %s",
    405 			__progname, component_to_fail,
    406 			"of this device\n");
    407 		exit(1);
    408 	}
    409 
    410 	recon_request.row = component_num / device_config.cols;
    411 	recon_request.col = component_num % device_config.cols;
    412 	if (do_recon) {
    413 		recon_request.flags = RF_FDFLAGS_RECON;
    414 	} else {
    415 		recon_request.flags = RF_FDFLAGS_NONE;
    416 	}
    417 	do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
    418 		 "RAIDFRAME_FAIL_DISK");
    419 
    420 }
    421 
    422 static void
    423 get_component_label(fd, component)
    424 	int fd;
    425 	char *component;
    426 {
    427 	RF_ComponentLabel_t component_label;
    428 	RF_DeviceConfig_t device_config;
    429 	void *cfg_ptr;
    430 	void *label_ptr;
    431 	int i;
    432 	int found;
    433 	int component_num;
    434 
    435 	component_num = -1;
    436 
    437 	/* Assuming a full path spec... */
    438 	cfg_ptr = &device_config;
    439 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    440 		 "RAIDFRAME_GET_INFO");
    441 	found = 0;
    442 	for(i=0; i < device_config.ndevs; i++) {
    443 		if (strncmp(component, device_config.devs[i].devname,
    444 			    PATH_MAX)==0) {
    445 			found = 1;
    446 			component_num = i;
    447 		}
    448 	}
    449 	if (!found) {
    450 		fprintf(stderr,"%s: %s is not a component %s",
    451 			__progname, component, "of this device\n");
    452 		exit(1);
    453 	}
    454 
    455 	memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
    456 	component_label.row = component_num / device_config.cols;
    457 	component_label.column = component_num % device_config.cols;
    458 
    459 	label_ptr = &component_label;
    460 	do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
    461 		  "RAIDFRAME_GET_COMPONENT_LABEL");
    462 
    463 	printf("Component label for %s:\n",component);
    464 	printf("Version: %d\n",component_label.version);
    465 	printf("Serial Number: %d\n",component_label.serial_number);
    466 	printf("Mod counter: %d\n",component_label.mod_counter);
    467 	printf("Row: %d\n", component_label.row);
    468 	printf("Column: %d\n", component_label.column);
    469 	printf("Num Rows: %d\n", component_label.num_rows);
    470 	printf("Num Columns: %d\n", component_label.num_columns);
    471 	printf("Clean: %d\n", component_label.clean);
    472 	printf("Status: %s\n", device_status(component_label.status));
    473 
    474 }
    475 
    476 static void
    477 set_component_label(fd, component)
    478 	int fd;
    479 	char *component;
    480 {
    481 	RF_ComponentLabel_t component_label;
    482 	RF_DeviceConfig_t device_config;
    483 	void *cfg_ptr;
    484 	int i;
    485 	int found;
    486 	int component_num;
    487 
    488 	component_num = -1;
    489 
    490 	/* Assuming a full path spec... */
    491 	cfg_ptr = &device_config;
    492 	do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
    493 		 "RAIDFRAME_GET_INFO");
    494 	found = 0;
    495 	for(i=0; i < device_config.ndevs; i++) {
    496 		if (strncmp(component, device_config.devs[i].devname,
    497 			    PATH_MAX)==0) {
    498 			found = 1;
    499 			component_num = i;
    500 		}
    501 	}
    502 	if (!found) {
    503 		fprintf(stderr,"%s: %s is not a component %s",
    504 			__progname, component, "of this device\n");
    505 		exit(1);
    506 	}
    507 
    508 	component_label.version = 1;
    509 	component_label.serial_number = 123456;
    510 	component_label.mod_counter = 0;
    511 	component_label.row = component_num / device_config.cols;
    512 	component_label.column = component_num % device_config.cols;
    513 	component_label.num_rows = 0;
    514 	component_label.num_columns = 5;
    515 	component_label.clean = 0;
    516 	component_label.status = 1;
    517 
    518 	do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
    519 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    520 
    521 }
    522 
    523 
    524 static void
    525 init_component_labels(fd, serial_number)
    526 	int fd;
    527 	int serial_number;
    528 {
    529 
    530 	RF_ComponentLabel_t component_label;
    531 
    532 	component_label.version = 0;
    533 	component_label.serial_number = serial_number;
    534 	component_label.mod_counter = 0;
    535 	component_label.row = 0;
    536 	component_label.column = 0;
    537 	component_label.num_rows = 0;
    538 	component_label.num_columns = 0;
    539 	component_label.clean = 0;
    540 	component_label.status = 0;
    541 
    542 	do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
    543 		  "RAIDFRAME_SET_COMPONENT_LABEL");
    544 
    545 
    546 }
    547 
    548 static void
    549 add_hot_spare(fd, component)
    550 	int fd;
    551 	char *component;
    552 {
    553 	RF_HotSpare_t hot_spare;
    554 
    555 	hot_spare.spare_number = 0;
    556 	strncpy(hot_spare.spare_name, component, sizeof(hot_spare.spare_name));
    557 
    558 	do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
    559 		  "RAIDFRAME_ADD_HOT_SPARE");
    560 
    561 }
    562 
    563 
    564 static void
    565 usage()
    566 {
    567 	fprintf(stderr, "usage: %s -c config_file dev\n", __progname);
    568 	fprintf(stderr, "       %s -C dev\n", __progname);
    569 	fprintf(stderr, "       %s -f component dev\n", __progname);
    570 	fprintf(stderr, "       %s -F component dev\n", __progname);
    571 #ifdef NOT_YET_BOYS_AND_GIRLS
    572 	fprintf(stderr, "       %s -I serial_number dev\n", __progname);
    573 #endif
    574 	fprintf(stderr, "       %s -r dev\n", __progname);
    575 	fprintf(stderr, "       %s -R dev\n", __progname);
    576 	fprintf(stderr, "       %s -s dev\n", __progname);
    577 #ifdef NOT_YET_BOYS_AND_GIRLS
    578 	fprintf(stderr, "       %s -u dev\n", __progname);
    579 #endif
    580 	exit(1);
    581 	/* NOTREACHED */
    582 }
    583