summaryrefslogtreecommitdiff
path: root/sbin/raidctl/raidctl.c
blob: 01f3174859629677325c405f08f674859ceb584b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
/*	$OpenBSD: raidctl.c,v 1.23 2004/07/17 02:14:33 deraadt Exp $	*/
/*      $NetBSD: raidctl.c,v 1.27 2001/07/10 01:30:52 lukem Exp $   */

/*-
 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Greg Oster
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This program is a re-write of the original rf_ctrl program
 * distributed by CMU with RAIDframe 1.1.
 *
 * This program is the userland interface to the RAIDframe kernel
 * driver in Net/OpenBSD.
 */

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#ifdef NETBSD
#include <sys/disklabel.h>
#endif

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>

#include "rf_raidframe.h"

extern  char *__progname;

typedef struct {
	int	fd;
	int	id;
} fdidpair;

int     main(int, char *[]);
void	do_ioctl(int, u_long, void *, const char *);
static  void rf_configure(fdidpair *, char*, int);
static  const char *device_status(RF_DiskStatus_t);
static  void rf_get_device_status(fdidpair *, int);
static	void rf_output_configuration(fdidpair *, int);
static  void get_component_number(fdidpair *, char *, int *, int *);
static  void rf_fail_disk(fdidpair *, char *, int);
static  void usage(void);
static  void get_component_label(fdidpair *, char *);
static  void set_component_label(fdidpair *, char *);
static  void init_component_labels(fdidpair *, int);
static  void set_autoconfig(fdidpair *, char *);
static  void add_hot_spare(fdidpair *, char *);
static  void remove_hot_spare(fdidpair *, char *);
static  void rebuild_in_place(fdidpair *, char *);
static  void check_status(fdidpair *,int,int);
static  void check_parity(fdidpair *,int,int);
static  void do_meter(fdidpair *, int, u_long);
static  void get_bar(char *, double, int);
static  void get_time_string(char *, int);
static  int open_device(fdidpair **, char *);
static  int get_all_devices(char ***, const char *);

int verbose;
int do_all;

int
main(int argc, char *argv[])
{
	int ch;
	int num_options;
	unsigned int action;
	char config_filename[PATH_MAX];
	char name[PATH_MAX];
	char component[PATH_MAX];
	char autoconf[10];
	int do_output;
	int do_recon;
	int do_rewrite;
	int serial_number;
	int i, nfd;
	fdidpair *fds;
	int force;
	u_long meter;
	const char *actionstr;

	num_options = 0;
	action = 0;
	meter = 0;
	do_output = 0;
	do_recon = 0;
	do_rewrite = 0;
	do_all = 0;
	serial_number = 0;
	force = 0;
	actionstr = NULL;

	while ((ch = getopt(argc, argv, "a:A:Bc:C:f:F:g:GiI:l:r:R:sSpPuv"))
	       != -1)
		switch(ch) {
		case 'a':
			action = RAIDFRAME_ADD_HOT_SPARE;
			if (strlcpy(component, optarg, sizeof component) >= sizeof(component))
				errx(1, "-a arg too long");	
			num_options++;
			break;
		case 'A':
			action = RAIDFRAME_SET_AUTOCONFIG;
			if (strlcpy(autoconf, optarg, sizeof(autoconf)) >= sizeof(autoconf))
				errx(1, "-A arg too long");
			num_options++;
			break;
		case 'B':
			action = RAIDFRAME_COPYBACK;
			num_options++;
			break;
		case 'c':
			action = RAIDFRAME_CONFIGURE;
			if (strlcpy(config_filename, optarg, sizeof config_filename) >=
			      sizeof(config_filename))
				errx(1, "-c arg too long");
			force = 0;
			num_options++;
			break;
		case 'C':
			if (strlcpy(config_filename, optarg, sizeof config_filename) >=
			      sizeof(config_filename))
				errx(1, "-C arg too long");
			action = RAIDFRAME_CONFIGURE;
			force = 1;
			num_options++;
			break;
		case 'f':
			action = RAIDFRAME_FAIL_DISK;
			if (strlcpy(component, optarg, sizeof component) >= sizeof(component))
				errx(1, "-f arg too long");
			do_recon = 0;
			num_options++;
			break;
		case 'F':
			action = RAIDFRAME_FAIL_DISK;
			if (strlcpy(component, optarg, sizeof component) >= sizeof(component))
				errx(1, "-F arg too long");
			do_recon = 1;
			num_options++;
			break;
		case 'g':
			action = RAIDFRAME_GET_COMPONENT_LABEL;
			if (strlcpy(component, optarg, sizeof component) >= sizeof(component))
				errx(1, "-g arg too long");
			num_options++;
			break;
		case 'G':
			action = RAIDFRAME_GET_INFO;
			do_output = 1;
			num_options++;
			break;
		case 'i':
			action = RAIDFRAME_REWRITEPARITY;
			num_options++;
			break;
		case 'I':
			action = RAIDFRAME_INIT_LABELS;
			serial_number = atoi(optarg);
			num_options++;
			break;
		case 'l':
			action = RAIDFRAME_SET_COMPONENT_LABEL;
			if (strlcpy(component, optarg, sizeof component) >= sizeof(component))
				errx(1, "-l arg too long");
			num_options++;
			break;
		case 'r':
			action = RAIDFRAME_REMOVE_HOT_SPARE;
			if (strlcpy(component, optarg, sizeof component) >= sizeof(component))
				errx(1, "-r arg too long");
			num_options++;
			break;
		case 'R':
			if (strlcpy(component, optarg, sizeof component) >= sizeof(component))
				errx(1, "-R arg too long");
			action = RAIDFRAME_REBUILD_IN_PLACE;
			num_options++;
			break;
		case 's':
			action = RAIDFRAME_GET_INFO;
			num_options++;
			break;
		case 'S':
			action = RAIDFRAME_CHECK_RECON_STATUS_EXT;
			num_options++;
			break;
		case 'p':
			action = RAIDFRAME_CHECK_PARITY;
			num_options++;
			break;
		case 'P':
			action = RAIDFRAME_CHECK_PARITY;
			do_rewrite = 1;
			num_options++;
			break;
		case 'u':
			action = RAIDFRAME_SHUTDOWN;
			num_options++;
			break;
		case 'v':
			verbose = 1;
			break;
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if ((num_options > 1) || (argc == 0))
		usage();

	if (strlcpy(name, argv[0], sizeof name) >= sizeof(name))
		errx(1, "device name too long");

	if ((nfd = open_device(&fds, name)) < 1) {
		/* No configured raid device */
		free(fds);
		return (0);
	}

	if (do_all) {
		switch(action) {
		case RAIDFRAME_ADD_HOT_SPARE:
		case RAIDFRAME_REMOVE_HOT_SPARE:
		case RAIDFRAME_CONFIGURE:
		case RAIDFRAME_SET_AUTOCONFIG:
		case RAIDFRAME_FAIL_DISK:
		case RAIDFRAME_SET_COMPONENT_LABEL:
		case RAIDFRAME_GET_COMPONENT_LABEL:
		case RAIDFRAME_INIT_LABELS:
		case RAIDFRAME_REBUILD_IN_PLACE:
			errx(1,
			    "This action doesn't work with the 'all' device");
			break;
		default:
			break;
		}
	}

	switch(action) {
	case RAIDFRAME_ADD_HOT_SPARE:
		add_hot_spare(fds, component);
		break;
	case RAIDFRAME_REMOVE_HOT_SPARE:
		remove_hot_spare(fds, component);
		break;
	case RAIDFRAME_CONFIGURE:
		rf_configure(fds, config_filename, force);
		break;
	case RAIDFRAME_SET_AUTOCONFIG:
		set_autoconfig(fds, autoconf);
		break;
	case RAIDFRAME_COPYBACK:
		i = nfd;
		while (i--) {
			do_ioctl(fds[i].fd, RAIDFRAME_COPYBACK, NULL,
				 "RAIDFRAME_COPYBACK");
		}
		actionstr = "Copyback";
		meter = RAIDFRAME_CHECK_COPYBACK_STATUS_EXT;
		break;
	case RAIDFRAME_FAIL_DISK:
		rf_fail_disk(fds, component, do_recon);
		break;
	case RAIDFRAME_SET_COMPONENT_LABEL:
		set_component_label(fds, component);
		break;
	case RAIDFRAME_GET_COMPONENT_LABEL:
		get_component_label(fds, component);
		break;
	case RAIDFRAME_INIT_LABELS:
		init_component_labels(fds, serial_number);
		break;
	case RAIDFRAME_REWRITEPARITY:
		i = nfd;
		while (i--) {
			do_ioctl(fds[i].fd, RAIDFRAME_REWRITEPARITY, NULL,
				 "RAIDFRAME_REWRITEPARITY");
		}
		actionstr = "Parity Re-Write";
		meter = RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT;
		break;
	case RAIDFRAME_CHECK_RECON_STATUS_EXT:
		check_status(fds, nfd, 1);
		break;
	case RAIDFRAME_GET_INFO:
		if (do_output)
			rf_output_configuration(fds, nfd);
		else
			rf_get_device_status(fds, nfd);
		break;
	case RAIDFRAME_REBUILD_IN_PLACE:
		rebuild_in_place(fds, component);
		break;
	case RAIDFRAME_CHECK_PARITY:
		check_parity(fds, nfd, do_rewrite);
		break;
	case RAIDFRAME_SHUTDOWN:
		i = nfd;
		while (i--) {
			do_ioctl(fds[i].fd, RAIDFRAME_SHUTDOWN, NULL,
				 "RAIDFRAME_SHUTDOWN");
		}
		break;
	default:
		break;
	}

	if (verbose && meter) {
		sleep(3);	/* XXX give the action a chance to start */
		printf("%s status:\n", actionstr);
		do_meter(fds, nfd, meter);
	}

	i = nfd;
	while (i--)
		close(fds[i].fd);

	free(fds);
	return (0);
}

void
do_ioctl(int fd, unsigned long command, void *arg, const char *ioctl_name)
{
	if (ioctl(fd, command, arg) < 0)
		errx(1, "ioctl (%s) failed", ioctl_name);
}


static void
rf_configure(fdidpair *fds, char *config_file, int force)
{
	void *generic;
	RF_Config_t cfg;

	if (rf_MakeConfig(config_file, &cfg) != 0)
		errx(1, "unable to create RAIDframe configuration structure");
	
	cfg.force = force;

	/*
	 * Note the extra level of redirection needed here, since
	 * what we really want to pass in is a pointer to the pointer to
	 * the configuration structure.
	 */

	generic = (void *) &cfg;
	do_ioctl(fds->fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE");
}

static const char *
device_status(RF_DiskStatus_t status)
{

	switch (status) {
	case rf_ds_optimal:
		return ("optimal");
	case rf_ds_failed:
		return ("failed");
	case rf_ds_reconstructing:
		return ("reconstructing");
	case rf_ds_dist_spared:
		return ("dist_spared");
	case rf_ds_spared:
		return ("spared");
	case rf_ds_spare:
		return ("spare");
	case rf_ds_used_spare:
		return ("used_spare");
	default:
		return ("UNKNOWN");
	}
	/* NOTREACHED */
}

static void
rf_get_device_status(fdidpair *fds, int nfd)
{
	RF_DeviceConfig_t device_config;
	void *cfg_ptr;
	int is_clean;
	int i,j;

	cfg_ptr = &device_config;

	i = nfd;
	while (i--) {
		do_ioctl(fds[i].fd, RAIDFRAME_GET_INFO, &cfg_ptr,
			 "RAIDFRAME_GET_INFO");

		printf("raid%d Components:\n", fds[i].id);
		for (j = 0; j < device_config.ndevs; j++) {
			printf("%20s: %s\n", device_config.devs[j].devname,
			       device_status(device_config.devs[j].status));
		}
		if (device_config.nspares > 0) {
			printf("Spares:\n");
			for (j = 0; j < device_config.nspares; j++) {
				printf("%20s: %s\n",
				       device_config.spares[j].devname,
				       device_status(device_config.spares[j].status));
			}
		} else {
			printf("No spares.\n");
		}
		if (verbose) {
			for(j=0; j < device_config.ndevs; j++) {
				if (device_config.devs[j].status ==
				    rf_ds_optimal) {
					get_component_label(&fds[i],
					   device_config.devs[j].devname);
				} else {
					printf("%s status is: %s.  "
					   "Skipping label.\n",
					   device_config.devs[j].devname,
					   device_status(device_config.devs[j].status));
				}
			}

			if (device_config.nspares > 0) {
				for(j=0; j < device_config.nspares; j++) {
					if ((device_config.spares[j].status ==
					     rf_ds_optimal) ||
					    (device_config.spares[j].status ==
					     rf_ds_used_spare)) {
						get_component_label(&fds[i],
						   device_config.spares[j].devname);
					} else {
						printf("%s status is: %s.  "
						   "Skipping label.\n",
						   device_config.spares[j].devname,
						   device_status(device_config.spares[j].status));
					}		
				}
			}
		}

		do_ioctl(fds[i].fd, RAIDFRAME_CHECK_PARITY, &is_clean,
			 "RAIDFRAME_CHECK_PARITY");
		if (is_clean) {
			printf("Parity status: clean\n");
		} else {
			printf("Parity status: DIRTY\n");
		}
		check_status(&fds[i], 1, 0);
	}
}

static void
rf_output_configuration(fdidpair *fds, int nfd)
{
	RF_DeviceConfig_t device_config;
	void *cfg_ptr;
	int i,j;
	RF_ComponentLabel_t component_label;
	void *label_ptr;
	int component_num;
	int num_cols;
	char name[PATH_MAX];

	cfg_ptr = &device_config;

	i = nfd;
	while (i--) {
		snprintf(name, PATH_MAX, "/dev/raid%dc", fds[i].id);

		printf("# raidctl config file for %s\n", name);
		printf("\n");
		do_ioctl(fds[i].fd, RAIDFRAME_GET_INFO, &cfg_ptr,
			 "RAIDFRAME_GET_INFO");

		printf("START array\n");
		printf("# numRow numCol numSpare\n");
		printf("%d %d %d\n", device_config.rows, device_config.cols,
		    device_config.nspares);
		printf("\n");

		printf("START disks\n");
		for(j = 0; j < device_config.ndevs; j++)
			printf("%s\n", device_config.devs[j].devname);
		printf("\n");

		if (device_config.nspares > 0) {
			printf("START spare\n");
			for(j = 0; j < device_config.nspares; j++)
				printf("%s\n", device_config.spares[j].devname);
			printf("\n");
		}

		for(j = 0; j < device_config.ndevs; j++) {
			if (device_config.devs[j].status == rf_ds_optimal)
				break;
		}
		if (j == device_config.ndevs) {
			printf("# WARNING: no optimal components; using %s\n",
			    device_config.devs[0].devname);
			j = 0;
		}
		get_component_number(&fds[i], device_config.devs[j].devname,
		    &component_num, &num_cols);
		memset(&component_label, 0, sizeof(RF_ComponentLabel_t));
		component_label.row = component_num / num_cols;
		component_label.column = component_num % num_cols;
		label_ptr = &component_label;
		do_ioctl(fds[i].fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
			 "RAIDFRAME_GET_COMPONENT_LABEL");

		printf("START layout\n");
		printf(
		    "# sectPerSU SUsPerParityUnit SUsPerReconUnit "
		    "RAID_level_%c\n",
		    (char) component_label.parityConfig);
		printf("%d %d %d %c\n",
		    component_label.sectPerSU, component_label.SUsPerPU,
		    component_label.SUsPerRU,
		    (char) component_label.parityConfig);
		printf("\n");

		printf("START queue\n");
		printf("fifo %d\n", device_config.maxqdepth);
	}
}

static void
get_component_number(fdidpair *fds, char *component_name, int *component_number,
		     int *num_columns)
{
	RF_DeviceConfig_t device_config;
	void *cfg_ptr;
	int i;
	int found;

	*component_number = -1;
		
	/* Assuming a full path spec... */
	cfg_ptr = &device_config;
	do_ioctl(fds->fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");

	*num_columns = device_config.cols;

	found = 0;
	for (i = 0; i < device_config.ndevs; i++) {
		if (strncmp(component_name, device_config.devs[i].devname,
			    PATH_MAX) == 0) {
			found = 1;
			*component_number = i;
		}
	}
	if (!found) { /* maybe it's a spare? */
		for (i = 0; i < device_config.nspares; i++) {
			if (strncmp(component_name,
				    device_config.spares[i].devname,
				    PATH_MAX) == 0) {
				found = 1;
				*component_number = i + device_config.ndevs;
				/* the way spares are done should
				   really change... */
				*num_columns = device_config.cols +
					device_config.nspares;
			}
		}
	}

	if (!found)
		errx(1, "%s is not a component of this device", component_name);
}

static void
rf_fail_disk(fdidpair *fds, char *component_to_fail, int do_recon)
{
	struct rf_recon_req recon_request;
	int component_num;
	int num_cols;

	get_component_number(fds, component_to_fail, &component_num, &num_cols);

	recon_request.row = component_num / num_cols;
	recon_request.col = component_num % num_cols;
	if (do_recon) {
		recon_request.flags = RF_FDFLAGS_RECON;
	} else {
		recon_request.flags = RF_FDFLAGS_NONE;
	}
	do_ioctl(fds->fd, RAIDFRAME_FAIL_DISK, &recon_request,
		 "RAIDFRAME_FAIL_DISK");
	if (do_recon && verbose) {
		printf("Reconstruction status:\n");
		sleep(3); /* XXX give reconstruction a chance to start */
		do_meter(fds, 1, RAIDFRAME_CHECK_RECON_STATUS_EXT);
	}
}

static void
get_component_label(fdidpair *fds, char *component)
{
	RF_ComponentLabel_t component_label;
	void *label_ptr;
	int component_num;
	int num_cols;

	get_component_number(fds, component, &component_num, &num_cols);

	memset(&component_label, 0, sizeof(RF_ComponentLabel_t));
	component_label.row = component_num / num_cols;
	component_label.column = component_num % num_cols;

	label_ptr = &component_label;
	do_ioctl(fds->fd, RAIDFRAME_GET_COMPONENT_LABEL, &label_ptr,
		 "RAIDFRAME_GET_COMPONENT_LABEL");

	printf("Component label for %s:\n", component);

	printf("   Row: %d, Column: %d, Num Rows: %d, Num Columns: %d\n",
	       component_label.row, component_label.column,
	       component_label.num_rows, component_label.num_columns);
	printf("   Version: %d, Serial Number: %d, Mod Counter: %d\n",
	       component_label.version, component_label.serial_number,
	       component_label.mod_counter);
	printf("   Clean: %s, Status: %d\n",
	       component_label.clean ? "Yes" : "No",
	       component_label.status);
	printf("   sectPerSU: %d, SUsPerPU: %d, SUsPerRU: %d\n",
	       component_label.sectPerSU, component_label.SUsPerPU,
	       component_label.SUsPerRU);
	printf("   Queue size: %d, blocksize: %d, numBlocks: %d\n",
	       component_label.maxOutstanding, component_label.blockSize,
	       component_label.numBlocks);
	printf("   RAID Level: %c\n", (char) component_label.parityConfig);
	printf("   Autoconfig: %s\n",
	       component_label.autoconfigure ? "Yes" : "No");
	printf("   Root partition: %s\n",
	       component_label.root_partition ? "Yes" : "No");
	printf("   Last configured as: raid%d\n", component_label.last_unit);
}

static void
set_component_label(fdidpair *fds, char *component)
{
	RF_ComponentLabel_t component_label;
	int component_num;
	int num_cols;

	get_component_number(fds, component, &component_num, &num_cols);

	/* XXX This is currently here for testing, and future expandability */

	component_label.version = 1;
	component_label.serial_number = 123456;
	component_label.mod_counter = 0;
	component_label.row = component_num / num_cols;
	component_label.column = component_num % num_cols;
	component_label.num_rows = 0;
	component_label.num_columns = 5;
	component_label.clean = 0;
	component_label.status = 1;
	
	do_ioctl(fds->fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
		 "RAIDFRAME_SET_COMPONENT_LABEL");
}


static void
init_component_labels(fdidpair *fds, int serial_number)
{
	RF_ComponentLabel_t component_label;

	component_label.version = 0;
	component_label.serial_number = serial_number;
	component_label.mod_counter = 0;
	component_label.row = 0;
	component_label.column = 0;
	component_label.num_rows = 0;
	component_label.num_columns = 0;
	component_label.clean = 0;
	component_label.status = 0;
	
	do_ioctl(fds->fd, RAIDFRAME_INIT_LABELS, &component_label,
		 "RAIDFRAME_SET_COMPONENT_LABEL");
}

static void
set_autoconfig(fdidpair *fds, char *autoconf)
{
	int auto_config;
	int root_config;

	auto_config = 0;
	root_config = 0;

	if (strncasecmp(autoconf, "root", 4) == 0) {
		root_config = 1;
	}

	if ((strncasecmp(autoconf, "yes", 3) == 0) ||
	    root_config == 1) {
		auto_config = 1;
	}

	do_ioctl(fds->fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config,
		 "RAIDFRAME_SET_AUTOCONFIG");

	do_ioctl(fds->fd, RAIDFRAME_SET_ROOT, &root_config,
		 "RAIDFRAME_SET_ROOT");

	printf("raid%d: Autoconfigure: %s\n", fds->id,
	       auto_config ? "Yes" : "No");

	if (root_config == 1) {
		printf("raid%d: Root: %s\n", fds->id,
		       auto_config ? "Yes" : "No");
	}
}

static void
add_hot_spare(fdidpair *fds, char *component)
{
	RF_SingleComponent_t hot_spare;

	hot_spare.row = 0;
	hot_spare.column = 0;
	strlcpy(hot_spare.component_name, component,
		sizeof(hot_spare.component_name));
	
	do_ioctl(fds->fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
		 "RAIDFRAME_ADD_HOT_SPARE");
}

static void
remove_hot_spare(fdidpair *fds, char *component)
{
	RF_SingleComponent_t hot_spare;
	int component_num;
	int num_cols;

	get_component_number(fds, component, &component_num, &num_cols);

	hot_spare.row = component_num / num_cols;
	hot_spare.column = component_num % num_cols;

	strlcpy(hot_spare.component_name, component,
		sizeof(hot_spare.component_name));
	
	do_ioctl(fds->fd, RAIDFRAME_REMOVE_HOT_SPARE, &hot_spare,
		 "RAIDFRAME_REMOVE_HOT_SPARE");
}

static void
rebuild_in_place(fdidpair *fds, char *component)
{
	RF_SingleComponent_t comp;
	int component_num;
	int num_cols;

	get_component_number(fds, component, &component_num, &num_cols);

	comp.row = 0;
	comp.column = component_num;
	strlcpy(comp.component_name, component, sizeof(comp.component_name));
	
	do_ioctl(fds->fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
		 "RAIDFRAME_REBUILD_IN_PLACE");

	if (verbose) {
		printf("Reconstruction status:\n");
		sleep(3); /* XXX give reconstruction a chance to start */
		do_meter(fds, 1, RAIDFRAME_CHECK_RECON_STATUS_EXT);
	}

}

static void
check_parity(fdidpair *fds, int nfd, int do_rewrite)
{
	int i, is_clean, all_dirty, was_dirty;
	int percent_done;
	char dev_name[PATH_MAX];

	all_dirty = 0;
	i = nfd;
	while (i--) {
		is_clean = 0;
		percent_done = 0;
		snprintf(dev_name, PATH_MAX, "raid%d", fds[i].id);

		do_ioctl(fds[i].fd, RAIDFRAME_CHECK_PARITY, &is_clean,
		 	"RAIDFRAME_CHECK_PARITY");
		if (is_clean) {
			printf("%s: Parity status: clean\n", dev_name);
		} else {
			all_dirty |= 1 << fds[i].id;
			printf("%s: Parity status: DIRTY\n", dev_name);
			if (do_rewrite) {
				printf("%s: Initiating re-write of parity\n",
				    dev_name);
				do_ioctl(fds[i].fd, RAIDFRAME_REWRITEPARITY,
				    NULL, "RAIDFRAME_REWRITEPARITY");
			} else {
				/* parity is wrong, and is not being fixed. */
				exit(1);
			}
		}
	}

	if (do_all)
		strncpy(dev_name, "all raid", PATH_MAX);

	was_dirty = all_dirty;
	while (all_dirty) {
		sleep(3); /* wait a bit... */
		if (verbose) {
			printf("Parity Re-write status:\n");
			do_meter(fds, nfd,
			    RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
			all_dirty = 0;
		} else {
			i = nfd;
			while (i--) {
				do_ioctl(fds[i].fd,
				 	RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
				 	&percent_done,
				 	"RAIDFRAME_CHECK_PARITYREWRITE_STATUS"
				 	);
				if (percent_done == 100) {
					all_dirty &= ~(1 << fds[i].id);
				}
			}
		}
	}
	if (verbose && was_dirty)
		printf("%s: Parity Re-write complete\n", dev_name);
}


static void
check_status(fdidpair *fds, int nfd, int meter)
{
	int i;
	int recon_percent_done = 0;
	int parity_percent_done = 0;
	int copyback_percent_done = 0;
	int do_recon = 0;
	int do_parity = 0;
	int do_copyback = 0;
	u_long check = 0;

	i = nfd;
	while (i--) {
		if (meter) {
			printf("raid%d Status:\n", fds[i].id);
		}
		do_ioctl(fds[i].fd, RAIDFRAME_CHECK_RECON_STATUS,
			 &recon_percent_done,
			 "RAIDFRAME_CHECK_RECON_STATUS");
		printf("Reconstruction is %d%% complete.\n",
			recon_percent_done);
		if (recon_percent_done < 100) {
			do_recon |= 1 << fds[i].id;
		}
		do_ioctl(fds[i].fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
			 &parity_percent_done,
			 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
		printf("Parity Re-write is %d%% complete.\n",
			parity_percent_done);
		if (parity_percent_done < 100) {
			do_parity |= 1 << fds[i].id;
		}
		do_ioctl(fds[i].fd, RAIDFRAME_CHECK_COPYBACK_STATUS,
			 &copyback_percent_done,
			 "RAIDFRAME_CHECK_COPYBACK_STATUS");
		printf("Copyback is %d%% complete.\n",
			copyback_percent_done);
		if (copyback_percent_done < 100) {
			do_copyback |= 1 << fds[i].id;
		}
	}

	if (meter && verbose) {
		/* These 3 should be mutually exclusive at this point */
		if (do_recon) {
			printf("Reconstruction status:\n");
			check = RAIDFRAME_CHECK_RECON_STATUS_EXT;
		} else if (do_parity) {
			printf("Parity Re-write status:\n");
			check = RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT;
		} else if (do_copyback) {
			printf("Copyback status:\n");
			check = RAIDFRAME_CHECK_COPYBACK_STATUS_EXT;
		}
		do_meter(fds, nfd, check);
	}
}

const char *tbits = "|/-\\";

static void
do_meter(fdidpair *fds, int nfd, u_long option)
{
	int percent_done;
	int start_value;
	RF_ProgressInfo_t *progressInfo;
	void *pInfoPtr;
	struct timeval start_time;
	struct timeval current_time;
	double elapsed;
	int elapsed_sec;
	int elapsed_usec;
	int progress_total, progress_completed;
	int simple_eta, last_eta;
	double rate;
	int amount;
	int tbit_value;
	char buffer[1024];
	char bar_buffer[1024];
	char eta_buffer[1024];
	int not_done;
	int i;

	not_done = 0;
	percent_done = 0;
	tbit_value = 0;
	start_value = 0;
	last_eta = 0;
	progress_total = progress_completed = 0;
	progressInfo = malloc(nfd * sizeof(RF_ProgressInfo_t));
	memset(&progressInfo[0], 0, nfd * sizeof(RF_ProgressInfo_t));

	if (gettimeofday(&start_time, NULL))
		err(1, "gettimeofday");

	current_time = start_time;

	i = nfd;
	while (i--) {
		pInfoPtr = &progressInfo[i];
		do_ioctl(fds[i].fd, option, &pInfoPtr, "");
		start_value += progressInfo[i].completed;
		progress_total += progressInfo[i].total;

		if (progressInfo[i].completed < progressInfo[i].total) {
			not_done |= 1 << i;
		}
	}

	while (not_done) {
		progress_completed = 0;
		percent_done = 0;
		amount = 0;

		i = nfd;
		while (i--) {
			pInfoPtr = &progressInfo[i];
			do_ioctl(fds[i].fd, option, &pInfoPtr, "");
			progress_completed += progressInfo[i].completed;

			if (progressInfo[i].completed >=
			    progressInfo[i].total) {
				not_done &= ~(1 << i);
			}
		}
		percent_done = (progress_completed * 100) / progress_total;
		amount = progress_completed - start_value;

		get_bar(bar_buffer, percent_done, 40);

		elapsed_sec =   current_time.tv_sec -
				start_time.tv_sec;
		elapsed_usec =  current_time.tv_usec -
				start_time.tv_usec;
		if (elapsed_usec < 0) {
			elapsed_usec += 1000000;
			elapsed_sec--;
		}

		elapsed = (double) elapsed_sec +
			  (double) elapsed_usec / 1000000.0;

		if (amount <= 0) { /* we don't do negatives (yet?) */
			amount = 0;
		}

		if (elapsed == 0)
			rate = 0.0;
		else
			rate = amount / elapsed;

		if (rate > 0.0) {
			simple_eta = (int)
				(((double)progress_total -
				  (double) progress_completed)
				 / rate);
		} else {
			simple_eta = -1;
		}

		if (simple_eta <= 0) {
			simple_eta = last_eta;
		} else {
			last_eta = simple_eta;
		}

		get_time_string(eta_buffer, simple_eta);

		snprintf(buffer, 1024,
			 "\r\t%3d%% |%s| ETA: %s %c",
			 percent_done, bar_buffer,
			 eta_buffer, tbits[tbit_value]);

		write(fileno(stdout), buffer, strlen(buffer));
		fflush(stdout);

		if (++tbit_value > 3)
			tbit_value = 0;

		if (not_done)
			sleep(2);

		if (gettimeofday(&current_time, NULL))
			err(1, "gettimeofday");
	}
	printf("\n");
}

/* 40 '*''s per line, then 40 ' ''s line. */
/* If you've got a screen wider than 160 characters, "tough" */

#define STAR_MIDPOINT 4*40
const char stars[] = "****************************************"
                     "****************************************"
                     "****************************************"
                     "****************************************"
                     "                                        "
                     "                                        "
                     "                                        "
                     "                                        ";

static void
get_bar(char *string, double percent, int max_strlen)
{
	int offset;

	if (max_strlen > STAR_MIDPOINT) {
		max_strlen = STAR_MIDPOINT;
	}
	offset = STAR_MIDPOINT -
		(int)((percent * max_strlen) / 100);
	if (offset < 0)
		offset = 0;
	snprintf(string, max_strlen, "%s", &stars[offset]);
}

static void
get_time_string(char *string, int simple_time)
{
	int minutes, seconds, hours;
	char hours_buffer[5];
	char minutes_buffer[5];
	char seconds_buffer[5];

	if (simple_time >= 0) {

		minutes = (int) simple_time / 60;
		seconds = ((int)simple_time - 60 * minutes);
		hours = minutes / 60;
		minutes = minutes - 60 * hours;
		
		if (hours > 0) {
			snprintf(hours_buffer, sizeof(hours_buffer),
				 "%02d:", hours);
		} else {
			snprintf(hours_buffer, sizeof(hours_buffer), "   ");
		}
		
		snprintf(minutes_buffer, sizeof(minutes_buffer),
			 "%02d:", minutes);
		snprintf(seconds_buffer, sizeof(seconds_buffer),
			 "%02d", seconds);
		snprintf(string, 1024, "%s%s%s",
			 hours_buffer, minutes_buffer, seconds_buffer);
	} else {
		snprintf(string, 1024, "   --:--");
	}
	
}

static int
open_device(fdidpair **devfd, char *name)
{
	int nfd, i;
 	struct stat st;
	char **devname;

	if (strcmp(name, "all") == 0) {
		do_all = 1;
		nfd = get_all_devices(&devname, "raid");
	} else {
		nfd = 1;
		devname = malloc(sizeof(void*));
		devname[0] = malloc(PATH_MAX);

		if ((name[0] == '/') || (name[0] == '.')) {
			/* they've (apparently) given a full path... */
			strlcpy(devname[0], name, PATH_MAX);
		} else {
			if (isdigit(name[strlen(name) - 1])) {
				snprintf(devname[0], PATH_MAX, "%s%s%c",
				    _PATH_DEV, name, 'a' + getrawpartition());		
			} else {
				snprintf(devname[0], PATH_MAX, "%s%s",
				    _PATH_DEV, name);
			}
		}
	}

	if ((*devfd = malloc(nfd * sizeof(fdidpair))) == NULL)
		errx(1, "malloc() error");

	i = nfd;
	while (i--) {
		if (stat(devname[i], &st) != 0)
			errx(errno, "stat failure on: %s", devname[i]);
		if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
			errx(EINVAL, "invalid device: %s", devname[i]);

		if (((*devfd)[i].fd = open(devname[i], O_RDWR, 0640)) < 0)
			errx(1, "unable to open device file: %s", devname[i]);
		(*devfd)[i].id = RF_DEV2RAIDID(st.st_rdev);

		free(devname[i]);
	}

	if (devname != NULL)
		free(devname);

	return (nfd);
}

static int
get_all_devices(char ***diskarray, const char *genericname)
{
	int	i, numdevs, mib[2];
	size_t	len;
	char	*disks, *fp, *p;

	numdevs = 0;

	mib[0] = CTL_HW;
	mib[1] = HW_DISKNAMES;
	sysctl(mib, 2, NULL, &len, NULL, 0);
	if ((disks = malloc(len + 1)) == NULL)
		errx(1, "malloc() error");
	sysctl(mib, 2, disks, &len, NULL, 0);
	disks[len] = '\0';

	fp = disks;
	while ((fp = strstr((const char*)fp, genericname)) != NULL) {
		numdevs++;
		fp++;
	}

	*diskarray = (char**) malloc(numdevs * sizeof(void*));
	i = 0;
	fp = disks;
	while ((p = strsep(&fp, ",")) != NULL) {
		if (strstr((const char*)p, genericname) != NULL) {
			int len = strlen(p) + 7;

			(*diskarray)[i] = (char*) malloc(len);
			snprintf((*diskarray)[i++], len, "/dev/%s%c", p,
				'a' + getrawpartition());
		}
	}

	free(disks);

	return (numdevs);
}

static void
usage(void)
{
	fprintf(stderr,
	    "usage: raidctl [-v] [-afFgrR component] [-BGipPsSu] [-cC config_file]\n");
	fprintf(stderr,
	    "               [-A [yes | no | root]] [-I serial_number] dev\n");
	exit(1);
	/* NOTREACHED */
}