raidctl.c revision 1.8 1 /* $NetBSD: raidctl.c,v 1.8 1999/08/15 03:15:00 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 get_component_number __P((int, char *, int *, int *));
74 static void rf_fail_disk __P((int, char *, int));
75 static void usage __P((void));
76 static void get_component_label __P((int, char *));
77 static void set_component_label __P((int, char *));
78 static void init_component_labels __P((int, int));
79 static void add_hot_spare __P((int, char *));
80 static void remove_hot_spare __P((int, char *));
81 static void rebuild_in_place __P((int, char *));
82
83 int
84 main(argc,argv)
85 int argc;
86 char *argv[];
87 {
88 extern char *optarg;
89 extern int optind;
90 int ch;
91 int num_options;
92 unsigned long action;
93 char config_filename[PATH_MAX];
94 char dev_name[PATH_MAX];
95 char name[PATH_MAX];
96 char component[PATH_MAX];
97 int do_recon;
98 int do_rewrite;
99 int is_clean;
100 int raidID;
101 int rawpart;
102 int recon_percent_done;
103 int serial_number;
104 struct stat st;
105 int fd;
106 int force;
107
108 num_options = 0;
109 action = 0;
110 do_recon = 0;
111 do_rewrite = 0;
112 is_clean = 0;
113 force = 0;
114
115 while ((ch = getopt(argc, argv, "a:Bc:C:f:F:g:iI:l:r:R:sSpPu")) != -1)
116 switch(ch) {
117 case 'a':
118 action = RAIDFRAME_ADD_HOT_SPARE;
119 strncpy(component, optarg, PATH_MAX);
120 num_options++;
121 break;
122 case 'B':
123 action = RAIDFRAME_COPYBACK;
124 num_options++;
125 break;
126 case 'c':
127 action = RAIDFRAME_CONFIGURE;
128 strncpy(config_filename,optarg,PATH_MAX);
129 force = 0;
130 num_options++;
131 break;
132 case 'C':
133 strncpy(config_filename,optarg,PATH_MAX);
134 action = RAIDFRAME_CONFIGURE;
135 force = 1;
136 num_options++;
137 break;
138 case 'f':
139 action = RAIDFRAME_FAIL_DISK;
140 strncpy(component, optarg, PATH_MAX);
141 do_recon = 0;
142 num_options++;
143 break;
144 case 'F':
145 action = RAIDFRAME_FAIL_DISK;
146 strncpy(component, optarg, PATH_MAX);
147 do_recon = 1;
148 num_options++;
149 break;
150 case 'g':
151 action = RAIDFRAME_GET_COMPONENT_LABEL;
152 strncpy(component, optarg, PATH_MAX);
153 num_options++;
154 break;
155 case 'i':
156 action = RAIDFRAME_REWRITEPARITY;
157 num_options++;
158 break;
159 case 'I':
160 action = RAIDFRAME_INIT_LABELS;
161 serial_number = atoi(optarg);
162 num_options++;
163 break;
164 case 'l':
165 action = RAIDFRAME_SET_COMPONENT_LABEL;
166 strncpy(component, optarg, PATH_MAX);
167 num_options++;
168 break;
169 case 'r':
170 action = RAIDFRAME_REMOVE_HOT_SPARE;
171 strncpy(component, optarg, PATH_MAX);
172 num_options++;
173 break;
174 case 'R':
175 strncpy(component,optarg,PATH_MAX);
176 action = RAIDFRAME_REBUILD_IN_PLACE;
177 num_options++;
178 break;
179 case 's':
180 action = RAIDFRAME_GET_INFO;
181 num_options++;
182 break;
183 case 'S':
184 action = RAIDFRAME_CHECKRECON;
185 num_options++;
186 break;
187 case 'p':
188 action = RAIDFRAME_CHECK_PARITY;
189 num_options++;
190 break;
191 case 'P':
192 action = RAIDFRAME_CHECK_PARITY;
193 do_rewrite = 1;
194 num_options++;
195 break;
196 case 'u':
197 action = RAIDFRAME_SHUTDOWN;
198 num_options++;
199 break;
200 default:
201 usage();
202 }
203 argc -= optind;
204 argv += optind;
205
206 if ((num_options > 1) || (argc == NULL))
207 usage();
208
209 strncpy(name,argv[0],PATH_MAX);
210
211 if ((name[0] == '/') || (name[0] == '.')) {
212 /* they've (apparently) given a full path... */
213 strncpy(dev_name, name, PATH_MAX);
214 } else {
215 if (isdigit(name[strlen(name)-1])) {
216 rawpart = getrawpartition();
217 snprintf(dev_name,PATH_MAX,"/dev/%s%c",name,
218 'a'+rawpart);
219 } else {
220 snprintf(dev_name,PATH_MAX,"/dev/%s",name);
221 }
222 }
223
224 if (stat(dev_name, &st) != 0) {
225 fprintf(stderr,"%s: stat failure on: %s\n",
226 __progname,dev_name);
227 return (errno);
228 }
229 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) {
230 fprintf(stderr,"%s: invalid device: %s\n",
231 __progname,dev_name);
232 return (EINVAL);
233 }
234
235 raidID = RF_DEV2RAIDID(st.st_rdev);
236
237 if ((fd = open( dev_name, O_RDWR, 0640)) < 0) {
238 fprintf(stderr, "%s: unable to open device file: %s\n",
239 __progname, dev_name);
240 exit(1);
241 }
242
243
244 switch(action) {
245 case RAIDFRAME_ADD_HOT_SPARE:
246 add_hot_spare(fd,component);
247 break;
248 case RAIDFRAME_REMOVE_HOT_SPARE:
249 remove_hot_spare(fd,component);
250 break;
251 case RAIDFRAME_CONFIGURE:
252 rf_configure(fd, config_filename,force);
253 break;
254 case RAIDFRAME_COPYBACK:
255 printf("Copyback.\n");
256 do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
257 break;
258 case RAIDFRAME_FAIL_DISK:
259 rf_fail_disk(fd,component,do_recon);
260 break;
261 case RAIDFRAME_SET_COMPONENT_LABEL:
262 set_component_label(fd,component);
263 break;
264 case RAIDFRAME_GET_COMPONENT_LABEL:
265 get_component_label(fd,component);
266 break;
267 case RAIDFRAME_INIT_LABELS:
268 init_component_labels(fd,serial_number);
269 break;
270 case RAIDFRAME_REWRITEPARITY:
271 printf("Initiating re-write of parity\n");
272 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
273 "RAIDFRAME_REWRITEPARITY");
274 break;
275 case RAIDFRAME_CHECKRECON:
276 do_ioctl(fd, RAIDFRAME_CHECKRECON, &recon_percent_done,
277 "RAIDFRAME_CHECKRECON");
278 printf("Reconstruction is %d%% complete.\n",
279 recon_percent_done);
280 break;
281 case RAIDFRAME_GET_INFO:
282 rf_get_device_status(fd);
283 break;
284 case RAIDFRAME_REBUILD_IN_PLACE:
285 rebuild_in_place(fd,component);
286 break;
287 case RAIDFRAME_CHECK_PARITY:
288 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
289 "RAIDFRAME_CHECK_PARITY");
290 if (is_clean) {
291 printf("%s: Parity status: clean\n",dev_name);
292 } else {
293 printf("%s: Parity status: DIRTY\n",dev_name);
294 if (do_rewrite) {
295 printf("%s: Initiating re-write of parity\n",
296 dev_name);
297 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
298 "RAIDFRAME_REWRITEPARITY");
299 } else {
300 /* parity is wrong, and is not being fixed.
301 Exit w/ an error. */
302 exit(1);
303 }
304 }
305 break;
306 case RAIDFRAME_SHUTDOWN:
307 do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
308 break;
309 default:
310 break;
311 }
312
313 close(fd);
314 exit(0);
315 }
316
317 static void
318 do_ioctl(fd, command, arg, ioctl_name)
319 int fd;
320 unsigned long command;
321 void *arg;
322 char *ioctl_name;
323 {
324 if (ioctl(fd, command, arg) < 0) {
325 warn("ioctl (%s) failed", ioctl_name);
326 exit(1);
327 }
328 }
329
330
331 static void
332 rf_configure(fd,config_file,force)
333 int fd;
334 char *config_file;
335 int force;
336 {
337 void *generic;
338 RF_Config_t cfg;
339
340 if (rf_MakeConfig( config_file, &cfg ) < 0) {
341 fprintf(stderr,"%s: unable to create RAIDframe %s\n",
342 __progname, "configuration structure\n");
343 exit(1);
344 }
345
346 cfg.force = force;
347
348 /*
349
350 Note the extra level of redirection needed here, since
351 what we really want to pass in is a pointer to the pointer to
352 the configuration structure.
353
354 */
355
356 generic = (void *) &cfg;
357 do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE");
358 }
359
360 static char *
361 device_status(status)
362 RF_DiskStatus_t status;
363 {
364 static char status_string[256];
365
366 switch (status) {
367 case rf_ds_optimal:
368 strcpy(status_string,"optimal");
369 break;
370 case rf_ds_failed:
371 strcpy(status_string,"failed");
372 break;
373 case rf_ds_reconstructing:
374 strcpy(status_string,"reconstructing");
375 break;
376 case rf_ds_dist_spared:
377 strcpy(status_string,"dist_spared");
378 break;
379 case rf_ds_spared:
380 strcpy(status_string,"spared");
381 break;
382 case rf_ds_spare:
383 strcpy(status_string,"spare");
384 break;
385 case rf_ds_used_spare:
386 strcpy(status_string,"used_spare");
387 break;
388 default:
389 strcpy(status_string,"UNKNOWN");
390 break;
391 }
392 return(status_string);
393 }
394
395 static void
396 rf_get_device_status(fd)
397 int fd;
398 {
399 RF_DeviceConfig_t device_config;
400 void *cfg_ptr;
401 int is_clean;
402 int i;
403
404 cfg_ptr = &device_config;
405
406 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
407
408 printf("Components:\n");
409 for(i=0; i < device_config.ndevs; i++) {
410 printf("%20s: %s\n", device_config.devs[i].devname,
411 device_status(device_config.devs[i].status));
412 }
413 if (device_config.nspares > 0) {
414 printf("Spares:\n");
415 for(i=0; i < device_config.nspares; i++) {
416 printf("%20s: %s\n",
417 device_config.spares[i].devname,
418 device_status(device_config.spares[i].status));
419 }
420 } else {
421 printf("No spares.\n");
422 }
423 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
424 "RAIDFRAME_CHECK_PARITY");
425 if (is_clean) {
426 printf("Parity status: clean\n");
427 } else {
428 printf("Parity status: DIRTY\n");
429 }
430 }
431
432 static void
433 get_component_number(fd, component_name, component_number, num_columns)
434 int fd;
435 char *component_name;
436 int *component_number;
437 int *num_columns;
438 {
439 RF_DeviceConfig_t device_config;
440 void *cfg_ptr;
441 int i;
442 int found;
443
444 *component_number = -1;
445
446 /* Assuming a full path spec... */
447 cfg_ptr = &device_config;
448 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
449 "RAIDFRAME_GET_INFO");
450
451 *num_columns = device_config.cols;
452
453 found = 0;
454 for(i=0; i < device_config.ndevs; i++) {
455 if (strncmp(component_name, device_config.devs[i].devname,
456 PATH_MAX)==0) {
457 found = 1;
458 *component_number = i;
459 }
460 }
461 if (!found) {
462 fprintf(stderr,"%s: %s is not a component %s", __progname,
463 component_name, "of this device\n");
464 exit(1);
465 }
466 }
467
468 static void
469 rf_fail_disk(fd, component_to_fail, do_recon)
470 int fd;
471 char *component_to_fail;
472 int do_recon;
473 {
474 struct rf_recon_req recon_request;
475 int component_num;
476 int num_cols;
477
478 get_component_number(fd, component_to_fail, &component_num, &num_cols);
479
480 recon_request.row = component_num / num_cols;
481 recon_request.col = component_num % num_cols;
482 if (do_recon) {
483 recon_request.flags = RF_FDFLAGS_RECON;
484 } else {
485 recon_request.flags = RF_FDFLAGS_NONE;
486 }
487 do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
488 "RAIDFRAME_FAIL_DISK");
489 }
490
491 static void
492 get_component_label(fd, component)
493 int fd;
494 char *component;
495 {
496 RF_ComponentLabel_t component_label;
497 void *label_ptr;
498 int component_num;
499 int num_cols;
500
501 get_component_number(fd, component, &component_num, &num_cols);
502
503 memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
504 component_label.row = component_num / num_cols;
505 component_label.column = component_num % num_cols;
506
507 label_ptr = &component_label;
508 do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
509 "RAIDFRAME_GET_COMPONENT_LABEL");
510
511 printf("Component label for %s:\n",component);
512 printf("Version: %d\n",component_label.version);
513 printf("Serial Number: %d\n",component_label.serial_number);
514 printf("Mod counter: %d\n",component_label.mod_counter);
515 printf("Row: %d\n", component_label.row);
516 printf("Column: %d\n", component_label.column);
517 printf("Num Rows: %d\n", component_label.num_rows);
518 printf("Num Columns: %d\n", component_label.num_columns);
519 printf("Clean: %d\n", component_label.clean);
520 printf("Status: %s\n", device_status(component_label.status));
521 }
522
523 static void
524 set_component_label(fd, component)
525 int fd;
526 char *component;
527 {
528 RF_ComponentLabel_t component_label;
529 int component_num;
530 int num_cols;
531
532 get_component_number(fd, component, &component_num, &num_cols);
533
534 /* XXX This is currently here for testing, and future expandability */
535
536 component_label.version = 1;
537 component_label.serial_number = 123456;
538 component_label.mod_counter = 0;
539 component_label.row = component_num / num_cols;
540 component_label.column = component_num % num_cols;
541 component_label.num_rows = 0;
542 component_label.num_columns = 5;
543 component_label.clean = 0;
544 component_label.status = 1;
545
546 do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
547 "RAIDFRAME_SET_COMPONENT_LABEL");
548 }
549
550
551 static void
552 init_component_labels(fd, serial_number)
553 int fd;
554 int serial_number;
555 {
556 RF_ComponentLabel_t component_label;
557
558 component_label.version = 0;
559 component_label.serial_number = serial_number;
560 component_label.mod_counter = 0;
561 component_label.row = 0;
562 component_label.column = 0;
563 component_label.num_rows = 0;
564 component_label.num_columns = 0;
565 component_label.clean = 0;
566 component_label.status = 0;
567
568 do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
569 "RAIDFRAME_SET_COMPONENT_LABEL");
570 }
571
572 static void
573 add_hot_spare(fd, component)
574 int fd;
575 char *component;
576 {
577 RF_SingleComponent_t hot_spare;
578
579 hot_spare.row = 0;
580 hot_spare.column = 0;
581 strncpy(hot_spare.component_name, component,
582 sizeof(hot_spare.component_name));
583
584 do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
585 "RAIDFRAME_ADD_HOT_SPARE");
586 }
587
588 static void
589 remove_hot_spare(fd, component)
590 int fd;
591 char *component;
592 {
593 RF_SingleComponent_t hot_spare;
594 int component_num;
595 int num_cols;
596
597 get_component_number(fd, component, &component_num, &num_cols);
598
599 hot_spare.row = component_num / num_cols;
600 hot_spare.column = component_num % num_cols;
601
602 strncpy(hot_spare.component_name, component,
603 sizeof(hot_spare.component_name));
604
605 do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
606 "RAIDFRAME_REMOVE_HOT_SPARE");
607 }
608
609 static void
610 rebuild_in_place( fd, component )
611 int fd;
612 char *component;
613 {
614 RF_SingleComponent_t comp;
615 int component_num;
616 int num_cols;
617
618 get_component_number(fd, component, &component_num, &num_cols);
619
620 comp.row = 0;
621 comp.column = component_num;
622 strncpy(comp.component_name, component, sizeof(comp.component_name));
623
624 do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
625 "RAIDFRAME_REBUILD_IN_PLACE");
626 }
627
628 static void
629 usage()
630 {
631 fprintf(stderr, "usage: %s -a component dev\n", __progname);
632 fprintf(stderr, " %s -B dev\n", __progname);
633 fprintf(stderr, " %s -c config_file dev\n", __progname);
634 fprintf(stderr, " %s -C config_file dev\n", __progname);
635 fprintf(stderr, " %s -f component dev\n", __progname);
636 fprintf(stderr, " %s -F component dev\n", __progname);
637 fprintf(stderr, " %s -g component dev\n", __progname);
638 fprintf(stderr, " %s -i dev\n", __progname);
639 fprintf(stderr, " %s -I serial_number dev\n", __progname);
640 fprintf(stderr, " %s -r component dev\n", __progname);
641 fprintf(stderr, " %s -R component dev\n", __progname);
642 fprintf(stderr, " %s -s dev\n", __progname);
643 fprintf(stderr, " %s -S dev\n", __progname);
644 fprintf(stderr, " %s -u dev\n", __progname);
645 #if 0
646 fprintf(stderr, "usage: %s %s\n", __progname,
647 "-a | -f | -F | -g | -r | -R component dev");
648 fprintf(stderr, " %s -B | -i | -s | -S -u dev\n", __progname);
649 fprintf(stderr, " %s -c | -C config_file dev\n", __progname);
650 fprintf(stderr, " %s -I serial_number dev\n", __progname);
651 #endif
652 exit(1);
653 /* NOTREACHED */
654 }
655