raidctl.c revision 1.17 1 /* $NetBSD: raidctl.c,v 1.17 2000/05/23 00:33:13 thorpej 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 void do_ioctl __P((int, unsigned long, void *, const 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 set_autoconfig __P((int, int, char *));
80 static void add_hot_spare __P((int, char *));
81 static void remove_hot_spare __P((int, char *));
82 static void rebuild_in_place __P((int, char *));
83 static void check_status __P((int,int));
84 static void check_parity __P((int,int,char *));
85 static void do_meter __P((int, u_long));
86 static void get_bar __P((char *, double, int));
87 static void get_time_string __P((char *, int));
88
89 int verbose = 0;
90
91 int
92 main(argc,argv)
93 int argc;
94 char *argv[];
95 {
96 int ch;
97 int num_options;
98 unsigned long action;
99 char config_filename[PATH_MAX];
100 char dev_name[PATH_MAX];
101 char name[PATH_MAX];
102 char component[PATH_MAX];
103 char autoconf[10];
104 int do_recon;
105 int do_rewrite;
106 int is_clean;
107 int raidID;
108 int rawpart;
109 int serial_number;
110 struct stat st;
111 int fd;
112 int force;
113
114 num_options = 0;
115 action = 0;
116 do_recon = 0;
117 do_rewrite = 0;
118 is_clean = 0;
119 force = 0;
120
121 while ((ch = getopt(argc, argv, "a:A:Bc:C:f:F:g:iI:l:r:R:sSpPuv"))
122 != -1)
123 switch(ch) {
124 case 'a':
125 action = RAIDFRAME_ADD_HOT_SPARE;
126 strncpy(component, optarg, PATH_MAX);
127 num_options++;
128 break;
129 case 'A':
130 action = RAIDFRAME_SET_AUTOCONFIG;
131 strncpy(autoconf, optarg, 10);
132 num_options++;
133 break;
134 case 'B':
135 action = RAIDFRAME_COPYBACK;
136 num_options++;
137 break;
138 case 'c':
139 action = RAIDFRAME_CONFIGURE;
140 strncpy(config_filename,optarg,PATH_MAX);
141 force = 0;
142 num_options++;
143 break;
144 case 'C':
145 strncpy(config_filename,optarg,PATH_MAX);
146 action = RAIDFRAME_CONFIGURE;
147 force = 1;
148 num_options++;
149 break;
150 case 'f':
151 action = RAIDFRAME_FAIL_DISK;
152 strncpy(component, optarg, PATH_MAX);
153 do_recon = 0;
154 num_options++;
155 break;
156 case 'F':
157 action = RAIDFRAME_FAIL_DISK;
158 strncpy(component, optarg, PATH_MAX);
159 do_recon = 1;
160 num_options++;
161 break;
162 case 'g':
163 action = RAIDFRAME_GET_COMPONENT_LABEL;
164 strncpy(component, optarg, PATH_MAX);
165 num_options++;
166 break;
167 case 'i':
168 action = RAIDFRAME_REWRITEPARITY;
169 num_options++;
170 break;
171 case 'I':
172 action = RAIDFRAME_INIT_LABELS;
173 serial_number = atoi(optarg);
174 num_options++;
175 break;
176 case 'l':
177 action = RAIDFRAME_SET_COMPONENT_LABEL;
178 strncpy(component, optarg, PATH_MAX);
179 num_options++;
180 break;
181 case 'r':
182 action = RAIDFRAME_REMOVE_HOT_SPARE;
183 strncpy(component, optarg, PATH_MAX);
184 num_options++;
185 break;
186 case 'R':
187 strncpy(component,optarg,PATH_MAX);
188 action = RAIDFRAME_REBUILD_IN_PLACE;
189 num_options++;
190 break;
191 case 's':
192 action = RAIDFRAME_GET_INFO;
193 num_options++;
194 break;
195 case 'S':
196 action = RAIDFRAME_CHECK_RECON_STATUS;
197 num_options++;
198 break;
199 case 'p':
200 action = RAIDFRAME_CHECK_PARITY;
201 num_options++;
202 break;
203 case 'P':
204 action = RAIDFRAME_CHECK_PARITY;
205 do_rewrite = 1;
206 num_options++;
207 break;
208 case 'u':
209 action = RAIDFRAME_SHUTDOWN;
210 num_options++;
211 break;
212 case 'v':
213 verbose = 1;
214 /* Don't bump num_options, as '-v' is not
215 an option like the others */
216 /* num_options++; */
217 break;
218 default:
219 usage();
220 }
221 argc -= optind;
222 argv += optind;
223
224 if ((num_options > 1) || (argc == NULL))
225 usage();
226
227 strncpy(name,argv[0],PATH_MAX);
228
229 if ((name[0] == '/') || (name[0] == '.')) {
230 /* they've (apparently) given a full path... */
231 strncpy(dev_name, name, PATH_MAX);
232 } else {
233 if (isdigit(name[strlen(name)-1])) {
234 rawpart = getrawpartition();
235 snprintf(dev_name,PATH_MAX,"/dev/%s%c",name,
236 'a'+rawpart);
237 } else {
238 snprintf(dev_name,PATH_MAX,"/dev/%s",name);
239 }
240 }
241
242 if (stat(dev_name, &st) != 0) {
243 fprintf(stderr,"%s: stat failure on: %s\n",
244 __progname,dev_name);
245 return (errno);
246 }
247 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) {
248 fprintf(stderr,"%s: invalid device: %s\n",
249 __progname,dev_name);
250 return (EINVAL);
251 }
252
253 raidID = RF_DEV2RAIDID(st.st_rdev);
254
255 if ((fd = open( dev_name, O_RDWR, 0640)) < 0) {
256 fprintf(stderr, "%s: unable to open device file: %s\n",
257 __progname, dev_name);
258 exit(1);
259 }
260
261
262 switch(action) {
263 case RAIDFRAME_ADD_HOT_SPARE:
264 add_hot_spare(fd, component);
265 break;
266 case RAIDFRAME_REMOVE_HOT_SPARE:
267 remove_hot_spare(fd, component);
268 break;
269 case RAIDFRAME_CONFIGURE:
270 rf_configure(fd, config_filename, force);
271 break;
272 case RAIDFRAME_SET_AUTOCONFIG:
273 set_autoconfig(fd, raidID, autoconf);
274 break;
275 case RAIDFRAME_COPYBACK:
276 printf("Copyback.\n");
277 do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
278 if (verbose) {
279 sleep(3); /* XXX give the copyback a chance to start */
280 printf("Copyback status:\n");
281 do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS);
282 }
283 break;
284 case RAIDFRAME_FAIL_DISK:
285 rf_fail_disk(fd, component, do_recon);
286 break;
287 case RAIDFRAME_SET_COMPONENT_LABEL:
288 set_component_label(fd, component);
289 break;
290 case RAIDFRAME_GET_COMPONENT_LABEL:
291 get_component_label(fd, component);
292 break;
293 case RAIDFRAME_INIT_LABELS:
294 init_component_labels(fd, serial_number);
295 break;
296 case RAIDFRAME_REWRITEPARITY:
297 printf("Initiating re-write of parity\n");
298 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
299 "RAIDFRAME_REWRITEPARITY");
300 if (verbose) {
301 sleep(3); /* XXX give it time to get started */
302 printf("Parity Re-write status:\n");
303 do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
304 }
305 break;
306 case RAIDFRAME_CHECK_RECON_STATUS:
307 check_status(fd,1);
308 break;
309 case RAIDFRAME_GET_INFO:
310 rf_get_device_status(fd);
311 break;
312 case RAIDFRAME_REBUILD_IN_PLACE:
313 rebuild_in_place(fd, component);
314 break;
315 case RAIDFRAME_CHECK_PARITY:
316 check_parity(fd, do_rewrite, dev_name);
317 break;
318 case RAIDFRAME_SHUTDOWN:
319 do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
320 break;
321 default:
322 break;
323 }
324
325 close(fd);
326 exit(0);
327 }
328
329 void
330 do_ioctl(fd, command, arg, ioctl_name)
331 int fd;
332 unsigned long command;
333 void *arg;
334 const char *ioctl_name;
335 {
336 if (ioctl(fd, command, arg) < 0) {
337 warn("ioctl (%s) failed", ioctl_name);
338 exit(1);
339 }
340 }
341
342
343 static void
344 rf_configure(fd,config_file,force)
345 int fd;
346 char *config_file;
347 int force;
348 {
349 void *generic;
350 RF_Config_t cfg;
351
352 if (rf_MakeConfig( config_file, &cfg ) != 0) {
353 fprintf(stderr,"%s: unable to create RAIDframe %s\n",
354 __progname, "configuration structure\n");
355 exit(1);
356 }
357
358 cfg.force = force;
359
360 /*
361
362 Note the extra level of redirection needed here, since
363 what we really want to pass in is a pointer to the pointer to
364 the configuration structure.
365
366 */
367
368 generic = (void *) &cfg;
369 do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE");
370 }
371
372 static char *
373 device_status(status)
374 RF_DiskStatus_t status;
375 {
376 static char status_string[256];
377
378 switch (status) {
379 case rf_ds_optimal:
380 strcpy(status_string,"optimal");
381 break;
382 case rf_ds_failed:
383 strcpy(status_string,"failed");
384 break;
385 case rf_ds_reconstructing:
386 strcpy(status_string,"reconstructing");
387 break;
388 case rf_ds_dist_spared:
389 strcpy(status_string,"dist_spared");
390 break;
391 case rf_ds_spared:
392 strcpy(status_string,"spared");
393 break;
394 case rf_ds_spare:
395 strcpy(status_string,"spare");
396 break;
397 case rf_ds_used_spare:
398 strcpy(status_string,"used_spare");
399 break;
400 default:
401 strcpy(status_string,"UNKNOWN");
402 break;
403 }
404 return(status_string);
405 }
406
407 static void
408 rf_get_device_status(fd)
409 int fd;
410 {
411 RF_DeviceConfig_t device_config;
412 void *cfg_ptr;
413 int is_clean;
414 int i;
415
416 cfg_ptr = &device_config;
417
418 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
419
420 printf("Components:\n");
421 for(i=0; i < device_config.ndevs; i++) {
422 printf("%20s: %s\n", device_config.devs[i].devname,
423 device_status(device_config.devs[i].status));
424 }
425 if (device_config.nspares > 0) {
426 printf("Spares:\n");
427 for(i=0; i < device_config.nspares; i++) {
428 printf("%20s: %s\n",
429 device_config.spares[i].devname,
430 device_status(device_config.spares[i].status));
431 }
432 } else {
433 printf("No spares.\n");
434 }
435 for(i=0; i < device_config.ndevs; i++) {
436 if (device_config.devs[i].status == rf_ds_optimal) {
437 get_component_label(fd, device_config.devs[i].devname);
438 } else {
439 printf("%s status is: %s. Skipping label.\n",
440 device_config.devs[i].devname,
441 device_status(device_config.devs[i].status));
442 }
443 }
444 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
445 "RAIDFRAME_CHECK_PARITY");
446 if (is_clean) {
447 printf("Parity status: clean\n");
448 } else {
449 printf("Parity status: DIRTY\n");
450 }
451 check_status(fd,0);
452 }
453
454 static void
455 get_component_number(fd, component_name, component_number, num_columns)
456 int fd;
457 char *component_name;
458 int *component_number;
459 int *num_columns;
460 {
461 RF_DeviceConfig_t device_config;
462 void *cfg_ptr;
463 int i;
464 int found;
465
466 *component_number = -1;
467
468 /* Assuming a full path spec... */
469 cfg_ptr = &device_config;
470 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
471 "RAIDFRAME_GET_INFO");
472
473 *num_columns = device_config.cols;
474
475 found = 0;
476 for(i=0; i < device_config.ndevs; i++) {
477 if (strncmp(component_name, device_config.devs[i].devname,
478 PATH_MAX)==0) {
479 found = 1;
480 *component_number = i;
481 }
482 }
483 if (!found) {
484 fprintf(stderr,"%s: %s is not a component %s", __progname,
485 component_name, "of this device\n");
486 exit(1);
487 }
488 }
489
490 static void
491 rf_fail_disk(fd, component_to_fail, do_recon)
492 int fd;
493 char *component_to_fail;
494 int do_recon;
495 {
496 struct rf_recon_req recon_request;
497 int component_num;
498 int num_cols;
499
500 get_component_number(fd, component_to_fail, &component_num, &num_cols);
501
502 recon_request.row = component_num / num_cols;
503 recon_request.col = component_num % num_cols;
504 if (do_recon) {
505 recon_request.flags = RF_FDFLAGS_RECON;
506 } else {
507 recon_request.flags = RF_FDFLAGS_NONE;
508 }
509 do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
510 "RAIDFRAME_FAIL_DISK");
511 if (do_recon && verbose) {
512 printf("Reconstruction status:\n");
513 sleep(3); /* XXX give reconstruction a chance to start */
514 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
515 }
516 }
517
518 static void
519 get_component_label(fd, component)
520 int fd;
521 char *component;
522 {
523 RF_ComponentLabel_t component_label;
524 void *label_ptr;
525 int component_num;
526 int num_cols;
527
528 get_component_number(fd, component, &component_num, &num_cols);
529
530 memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
531 component_label.row = component_num / num_cols;
532 component_label.column = component_num % num_cols;
533
534 label_ptr = &component_label;
535 do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
536 "RAIDFRAME_GET_COMPONENT_LABEL");
537
538 printf("Component label for %s:\n",component);
539
540 printf(" Row: %d Column: %d Num Rows: %d Num Columns: %d\n",
541 component_label.row, component_label.column,
542 component_label.num_rows, component_label.num_columns);
543 printf(" Version: %d Serial Number: %d Mod Counter: %d\n",
544 component_label.version, component_label.serial_number,
545 component_label.mod_counter);
546 printf(" Clean: %s Status: %d\n",
547 component_label.clean ? "Yes" : "No",
548 component_label.status );
549 printf(" sectPerSU: %d SUsPerPU: %d SUsPerRU: %d\n",
550 component_label.sectPerSU, component_label.SUsPerPU,
551 component_label.SUsPerRU);
552 printf(" RAID Level: %c blocksize: %d numBlocks: %d\n",
553 (char) component_label.parityConfig,
554 component_label.blockSize, component_label.numBlocks);
555 printf(" Autoconfig: %s\n",
556 component_label.autoconfigure ? "Yes" : "No" );
557 printf(" Root partition: %s\n",
558 component_label.root_partition ? "Yes" : "No" );
559 printf(" Last configured as: raid%d\n", component_label.last_unit );
560 }
561
562 static void
563 set_component_label(fd, component)
564 int fd;
565 char *component;
566 {
567 RF_ComponentLabel_t component_label;
568 int component_num;
569 int num_cols;
570
571 get_component_number(fd, component, &component_num, &num_cols);
572
573 /* XXX This is currently here for testing, and future expandability */
574
575 component_label.version = 1;
576 component_label.serial_number = 123456;
577 component_label.mod_counter = 0;
578 component_label.row = component_num / num_cols;
579 component_label.column = component_num % num_cols;
580 component_label.num_rows = 0;
581 component_label.num_columns = 5;
582 component_label.clean = 0;
583 component_label.status = 1;
584
585 do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
586 "RAIDFRAME_SET_COMPONENT_LABEL");
587 }
588
589
590 static void
591 init_component_labels(fd, serial_number)
592 int fd;
593 int serial_number;
594 {
595 RF_ComponentLabel_t component_label;
596
597 component_label.version = 0;
598 component_label.serial_number = serial_number;
599 component_label.mod_counter = 0;
600 component_label.row = 0;
601 component_label.column = 0;
602 component_label.num_rows = 0;
603 component_label.num_columns = 0;
604 component_label.clean = 0;
605 component_label.status = 0;
606
607 do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
608 "RAIDFRAME_SET_COMPONENT_LABEL");
609 }
610
611 static void
612 set_autoconfig(fd, raidID, autoconf)
613 int fd;
614 int raidID;
615 char *autoconf;
616 {
617 int auto_config;
618 int root_config;
619
620 auto_config = 0;
621 root_config = 0;
622
623 if (strncasecmp(autoconf,"root", 4) == 0) {
624 root_config = 1;
625 }
626
627 if ((strncasecmp(autoconf,"yes", 3) == 0) ||
628 root_config == 1) {
629 auto_config = 1;
630 }
631
632 do_ioctl(fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config,
633 "RAIDFRAME_SET_AUTOCONFIG");
634
635 do_ioctl(fd, RAIDFRAME_SET_ROOT, &root_config,
636 "RAIDFRAME_SET_ROOT");
637
638 printf("raid%d: Autoconfigure: %s\n", raidID,
639 auto_config ? "Yes" : "No");
640
641 if (root_config == 1) {
642 printf("raid%d: Root: %s\n", raidID,
643 auto_config ? "Yes" : "No");
644 }
645 }
646
647 static void
648 add_hot_spare(fd, component)
649 int fd;
650 char *component;
651 {
652 RF_SingleComponent_t hot_spare;
653
654 hot_spare.row = 0;
655 hot_spare.column = 0;
656 strncpy(hot_spare.component_name, component,
657 sizeof(hot_spare.component_name));
658
659 do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
660 "RAIDFRAME_ADD_HOT_SPARE");
661 }
662
663 static void
664 remove_hot_spare(fd, component)
665 int fd;
666 char *component;
667 {
668 RF_SingleComponent_t hot_spare;
669 int component_num;
670 int num_cols;
671
672 get_component_number(fd, component, &component_num, &num_cols);
673
674 hot_spare.row = component_num / num_cols;
675 hot_spare.column = component_num % num_cols;
676
677 strncpy(hot_spare.component_name, component,
678 sizeof(hot_spare.component_name));
679
680 do_ioctl( fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
681 "RAIDFRAME_REMOVE_HOT_SPARE");
682 }
683
684 static void
685 rebuild_in_place( fd, component )
686 int fd;
687 char *component;
688 {
689 RF_SingleComponent_t comp;
690 int component_num;
691 int num_cols;
692
693 get_component_number(fd, component, &component_num, &num_cols);
694
695 comp.row = 0;
696 comp.column = component_num;
697 strncpy(comp.component_name, component, sizeof(comp.component_name));
698
699 do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
700 "RAIDFRAME_REBUILD_IN_PLACE");
701
702 if (verbose) {
703 printf("Reconstruction status:\n");
704 sleep(3); /* XXX give reconstruction a chance to start */
705 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
706 }
707
708 }
709
710 static void
711 check_parity( fd, do_rewrite, dev_name )
712 int fd;
713 int do_rewrite;
714 char *dev_name;
715 {
716 int is_clean;
717 int percent_done;
718
719 is_clean = 0;
720 percent_done = 0;
721 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
722 "RAIDFRAME_CHECK_PARITY");
723 if (is_clean) {
724 printf("%s: Parity status: clean\n",dev_name);
725 } else {
726 printf("%s: Parity status: DIRTY\n",dev_name);
727 if (do_rewrite) {
728 printf("%s: Initiating re-write of parity\n",
729 dev_name);
730 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
731 "RAIDFRAME_REWRITEPARITY");
732 sleep(3); /* XXX give it time to
733 get started. */
734 if (verbose) {
735 printf("Parity Re-write status:\n");
736 do_meter(fd,
737 RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
738 } else {
739 do_ioctl(fd,
740 RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
741 &percent_done,
742 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"
743 );
744 while( percent_done < 100 ) {
745 do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
746 &percent_done, "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
747 }
748
749 }
750 printf("%s: Parity Re-write complete\n",
751 dev_name);
752 } else {
753 /* parity is wrong, and is not being fixed.
754 Exit w/ an error. */
755 exit(1);
756 }
757 }
758 }
759
760
761 static void
762 check_status( fd, meter )
763 int fd;
764 int meter;
765 {
766 int recon_percent_done = 0;
767 int parity_percent_done = 0;
768 int copyback_percent_done = 0;
769
770 do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done,
771 "RAIDFRAME_CHECK_RECON_STATUS");
772 printf("Reconstruction is %d%% complete.\n", recon_percent_done);
773 do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
774 &parity_percent_done,
775 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
776 printf("Parity Re-write is %d%% complete.\n", parity_percent_done);
777 do_ioctl(fd, RAIDFRAME_CHECK_COPYBACK_STATUS, ©back_percent_done,
778 "RAIDFRAME_CHECK_COPYBACK_STATUS");
779 printf("Copyback is %d%% complete.\n", copyback_percent_done);
780
781 if (meter) {
782 /* These 3 should be mutually exclusive at this point */
783 if (recon_percent_done < 100) {
784 printf("Reconstruction status:\n");
785 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS);
786 } else if (parity_percent_done < 100) {
787 printf("Parity Re-write status:\n");
788 do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS);
789 } else if (copyback_percent_done < 100) {
790 printf("Copyback status:\n");
791 do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS);
792 }
793 }
794 }
795
796 char *tbits = "|/-\\";
797
798 static void
799 do_meter(fd, option)
800 int fd;
801 u_long option;
802 {
803 int percent_done;
804 int last_percent;
805 int start_percent;
806 struct timeval start_time;
807 struct timeval last_time;
808 struct timeval current_time;
809 double elapsed;
810 int elapsed_sec;
811 int elapsed_usec;
812 int simple_eta,last_eta;
813 double rate;
814 int amount;
815 int tbit_value;
816 int wait_for_more_data;
817 char buffer[1024];
818 char bar_buffer[1024];
819 char eta_buffer[1024];
820
821 if (gettimeofday(&start_time,NULL)) {
822 fprintf(stderr,"%s: gettimeofday failed!?!?\n",__progname);
823 exit(errno);
824 }
825 percent_done = 0;
826 do_ioctl(fd, option, &percent_done, "");
827 last_percent = percent_done;
828 start_percent = percent_done;
829 last_time = start_time;
830 current_time = start_time;
831
832 wait_for_more_data = 0;
833 tbit_value = 0;
834
835 while(percent_done < 100) {
836
837 get_bar(bar_buffer, percent_done, 40);
838
839 elapsed_sec = current_time.tv_sec - last_time.tv_sec;
840
841 elapsed_usec = current_time.tv_usec - last_time.tv_usec;
842
843 if (elapsed_usec < 0) {
844 elapsed_usec-=1000000;
845 elapsed_sec++;
846 }
847
848 elapsed = (double) elapsed_sec +
849 (double) elapsed_usec / 1000000.0;
850 if (elapsed <= 0.0) {
851 elapsed = 0.0001; /* XXX */
852 }
853
854 amount = percent_done - last_percent;
855 if (amount <= 0) { /* we don't do negatives (yet?) */
856 amount = 0;
857 wait_for_more_data = 1;
858 } else {
859 wait_for_more_data = 0;
860 }
861 rate = amount / elapsed;
862
863
864 if (rate > 0.0) {
865 simple_eta = (int) ((100.0 - (double) last_percent ) / rate);
866 } else {
867 simple_eta = -1;
868 }
869 if (simple_eta <=0) {
870 simple_eta = last_eta;
871 } else {
872 last_eta = simple_eta;
873 }
874
875 get_time_string(eta_buffer, simple_eta);
876
877 snprintf(buffer,1024,"\r%3d%% |%s| ETA: %s %c",
878 percent_done,bar_buffer,eta_buffer,tbits[tbit_value]);
879
880 write(fileno(stdout),buffer,strlen(buffer));
881 fflush(stdout);
882
883 /* resolution wasn't high enough... wait until we get another
884 timestamp and perhaps more "work" done. */
885
886 if (!wait_for_more_data) {
887 last_time = current_time;
888 last_percent = percent_done;
889 }
890
891 if (++tbit_value>3)
892 tbit_value = 0;
893
894 sleep(2);
895
896 if (gettimeofday(¤t_time,NULL)) {
897 fprintf(stderr,"%s: gettimeofday failed!?!?\n",
898 __progname);
899 exit(errno);
900 }
901
902 do_ioctl( fd, option, &percent_done, "");
903
904
905 }
906 printf("\n");
907 }
908 /* 40 '*''s per line, then 40 ' ''s line. */
909 /* If you've got a screen wider than 160 characters, "tough" */
910
911 #define STAR_MIDPOINT 4*40
912 const char stars[] = "****************************************"
913 "****************************************"
914 "****************************************"
915 "****************************************"
916 " "
917 " "
918 " "
919 " "
920 " ";
921
922 static void
923 get_bar(string,percent,max_strlen)
924 char *string;
925 double percent;
926 int max_strlen;
927 {
928 int offset;
929
930 if (max_strlen > STAR_MIDPOINT) {
931 max_strlen = STAR_MIDPOINT;
932 }
933 offset = STAR_MIDPOINT -
934 (int)((percent * max_strlen)/ 100);
935 if (offset < 0)
936 offset = 0;
937 snprintf(string,max_strlen,"%s",&stars[offset]);
938 }
939
940 static void
941 get_time_string(string,simple_time)
942 char *string;
943 int simple_time;
944 {
945 int minutes, seconds, hours;
946 char hours_buffer[5];
947 char minutes_buffer[5];
948 char seconds_buffer[5];
949
950 if (simple_time >= 0) {
951
952 minutes = (int) simple_time / 60;
953 seconds = ((int)simple_time - 60*minutes);
954 hours = minutes / 60;
955 minutes = minutes - 60*hours;
956
957 if (hours > 0) {
958 snprintf(hours_buffer,5,"%02d:",hours);
959 } else {
960 snprintf(hours_buffer,5," ");
961 }
962
963 snprintf(minutes_buffer,5,"%02d:",minutes);
964 snprintf(seconds_buffer,5,"%02d",seconds);
965 snprintf(string,1024,"%s%s%s",
966 hours_buffer, minutes_buffer, seconds_buffer);
967 } else {
968 snprintf(string,1024," --:--");
969 }
970
971 }
972
973 static void
974 usage()
975 {
976 fprintf(stderr, "usage: %s [-v] -a component dev\n", __progname);
977 fprintf(stderr, " %s [-v] -A yes | no | root dev\n", __progname);
978 fprintf(stderr, " %s [-v] -B dev\n", __progname);
979 fprintf(stderr, " %s [-v] -c config_file dev\n", __progname);
980 fprintf(stderr, " %s [-v] -C config_file dev\n", __progname);
981 fprintf(stderr, " %s [-v] -f component dev\n", __progname);
982 fprintf(stderr, " %s [-v] -F component dev\n", __progname);
983 fprintf(stderr, " %s [-v] -g component dev\n", __progname);
984 fprintf(stderr, " %s [-v] -i dev\n", __progname);
985 fprintf(stderr, " %s [-v] -I serial_number dev\n", __progname);
986 fprintf(stderr, " %s [-v] -r component dev\n", __progname);
987 fprintf(stderr, " %s [-v] -R component dev\n", __progname);
988 fprintf(stderr, " %s [-v] -s dev\n", __progname);
989 fprintf(stderr, " %s [-v] -S dev\n", __progname);
990 fprintf(stderr, " %s [-v] -u dev\n", __progname);
991 #if 0
992 fprintf(stderr, "usage: %s %s\n", __progname,
993 "-a | -f | -F | -g | -r | -R component dev");
994 fprintf(stderr, " %s -B | -i | -s | -S -u dev\n", __progname);
995 fprintf(stderr, " %s -c | -C config_file dev\n", __progname);
996 fprintf(stderr, " %s -I serial_number dev\n", __progname);
997 #endif
998 exit(1);
999 /* NOTREACHED */
1000 }
1001