raidctl.c revision 1.7 1 /* $NetBSD: raidctl.c,v 1.7 1999/08/10 18:21:39 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 i;
402
403 cfg_ptr = &device_config;
404
405 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
406
407 printf("Components:\n");
408 for(i=0; i < device_config.ndevs; i++) {
409 printf("%20s: %s\n", device_config.devs[i].devname,
410 device_status(device_config.devs[i].status));
411 }
412 if (device_config.nspares > 0) {
413 printf("Spares:\n");
414 for(i=0; i < device_config.nspares; i++) {
415 printf("%20s: %s\n",
416 device_config.spares[i].devname,
417 device_status(device_config.spares[i].status));
418 }
419 } else {
420 printf("No spares.\n");
421 }
422 }
423
424 static void
425 get_component_number(fd, component_name, component_number, num_columns)
426 int fd;
427 char *component_name;
428 int *component_number;
429 int *num_columns;
430 {
431 RF_DeviceConfig_t device_config;
432 void *cfg_ptr;
433 int i;
434 int found;
435
436 *component_number = -1;
437
438 /* Assuming a full path spec... */
439 cfg_ptr = &device_config;
440 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
441 "RAIDFRAME_GET_INFO");
442
443 *num_columns = device_config.cols;
444
445 found = 0;
446 for(i=0; i < device_config.ndevs; i++) {
447 if (strncmp(component_name, device_config.devs[i].devname,
448 PATH_MAX)==0) {
449 found = 1;
450 *component_number = i;
451 }
452 }
453 if (!found) {
454 fprintf(stderr,"%s: %s is not a component %s", __progname,
455 component_name, "of this device\n");
456 exit(1);
457 }
458 }
459
460 static void
461 rf_fail_disk(fd, component_to_fail, do_recon)
462 int fd;
463 char *component_to_fail;
464 int do_recon;
465 {
466 struct rf_recon_req recon_request;
467 int component_num;
468 int num_cols;
469
470 get_component_number(fd, component_to_fail, &component_num, &num_cols);
471
472 recon_request.row = component_num / num_cols;
473 recon_request.col = component_num % num_cols;
474 if (do_recon) {
475 recon_request.flags = RF_FDFLAGS_RECON;
476 } else {
477 recon_request.flags = RF_FDFLAGS_NONE;
478 }
479 do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
480 "RAIDFRAME_FAIL_DISK");
481 }
482
483 static void
484 get_component_label(fd, component)
485 int fd;
486 char *component;
487 {
488 RF_ComponentLabel_t component_label;
489 void *label_ptr;
490 int component_num;
491 int num_cols;
492
493 get_component_number(fd, component, &component_num, &num_cols);
494
495 memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
496 component_label.row = component_num / num_cols;
497 component_label.column = component_num % num_cols;
498
499 label_ptr = &component_label;
500 do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
501 "RAIDFRAME_GET_COMPONENT_LABEL");
502
503 printf("Component label for %s:\n",component);
504 printf("Version: %d\n",component_label.version);
505 printf("Serial Number: %d\n",component_label.serial_number);
506 printf("Mod counter: %d\n",component_label.mod_counter);
507 printf("Row: %d\n", component_label.row);
508 printf("Column: %d\n", component_label.column);
509 printf("Num Rows: %d\n", component_label.num_rows);
510 printf("Num Columns: %d\n", component_label.num_columns);
511 printf("Clean: %d\n", component_label.clean);
512 printf("Status: %s\n", device_status(component_label.status));
513 }
514
515 static void
516 set_component_label(fd, component)
517 int fd;
518 char *component;
519 {
520 RF_ComponentLabel_t component_label;
521 int component_num;
522 int num_cols;
523
524 get_component_number(fd, component, &component_num, &num_cols);
525
526 /* XXX This is currently here for testing, and future expandability */
527
528 component_label.version = 1;
529 component_label.serial_number = 123456;
530 component_label.mod_counter = 0;
531 component_label.row = component_num / num_cols;
532 component_label.column = component_num % num_cols;
533 component_label.num_rows = 0;
534 component_label.num_columns = 5;
535 component_label.clean = 0;
536 component_label.status = 1;
537
538 do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
539 "RAIDFRAME_SET_COMPONENT_LABEL");
540 }
541
542
543 static void
544 init_component_labels(fd, serial_number)
545 int fd;
546 int serial_number;
547 {
548 RF_ComponentLabel_t component_label;
549
550 component_label.version = 0;
551 component_label.serial_number = serial_number;
552 component_label.mod_counter = 0;
553 component_label.row = 0;
554 component_label.column = 0;
555 component_label.num_rows = 0;
556 component_label.num_columns = 0;
557 component_label.clean = 0;
558 component_label.status = 0;
559
560 do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
561 "RAIDFRAME_SET_COMPONENT_LABEL");
562 }
563
564 static void
565 add_hot_spare(fd, component)
566 int fd;
567 char *component;
568 {
569 RF_SingleComponent_t hot_spare;
570
571 hot_spare.row = 0;
572 hot_spare.column = 0;
573 strncpy(hot_spare.component_name, component,
574 sizeof(hot_spare.component_name));
575
576 do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
577 "RAIDFRAME_ADD_HOT_SPARE");
578 }
579
580 static void
581 remove_hot_spare(fd, component)
582 int fd;
583 char *component;
584 {
585 RF_SingleComponent_t hot_spare;
586 int component_num;
587 int num_cols;
588
589 get_component_number(fd, component, &component_num, &num_cols);
590
591 hot_spare.row = component_num / num_cols;
592 hot_spare.column = component_num % num_cols;
593
594 strncpy(hot_spare.component_name, component,
595 sizeof(hot_spare.component_name));
596
597 do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
598 "RAIDFRAME_REMOVE_HOT_SPARE");
599 }
600
601 static void
602 rebuild_in_place( fd, component )
603 int fd;
604 char *component;
605 {
606 RF_SingleComponent_t comp;
607 int component_num;
608 int num_cols;
609
610 get_component_number(fd, component, &component_num, &num_cols);
611
612 comp.row = 0;
613 comp.column = component_num;
614 strncpy(comp.component_name, component, sizeof(comp.component_name));
615
616 do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
617 "RAIDFRAME_REBUILD_IN_PLACE");
618 }
619
620 static void
621 usage()
622 {
623 fprintf(stderr, "usage: %s -a component dev\n", __progname);
624 fprintf(stderr, " %s -B dev\n", __progname);
625 fprintf(stderr, " %s -c config_file dev\n", __progname);
626 fprintf(stderr, " %s -C config_file dev\n", __progname);
627 fprintf(stderr, " %s -f component dev\n", __progname);
628 fprintf(stderr, " %s -F component dev\n", __progname);
629 fprintf(stderr, " %s -g component dev\n", __progname);
630 fprintf(stderr, " %s -i dev\n", __progname);
631 fprintf(stderr, " %s -I serial_number dev\n", __progname);
632 fprintf(stderr, " %s -r component dev\n", __progname);
633 fprintf(stderr, " %s -R component dev\n", __progname);
634 fprintf(stderr, " %s -s dev\n", __progname);
635 fprintf(stderr, " %s -S dev\n", __progname);
636 fprintf(stderr, " %s -u dev\n", __progname);
637 #if 0
638 fprintf(stderr, "usage: %s %s\n", __progname,
639 "-a | -f | -F | -g | -r | -R component dev");
640 fprintf(stderr, " %s -B | -i | -s | -S -u dev\n", __progname);
641 fprintf(stderr, " %s -c | -C config_file dev\n", __progname);
642 fprintf(stderr, " %s -I serial_number dev\n", __progname);
643 #endif
644 exit(1);
645 /* NOTREACHED */
646 }
647