raidctl.c revision 1.79 1 /* $NetBSD: raidctl.c,v 1.79 2023/09/17 20:07:39 oster Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Greg Oster
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * This program is a re-write of the original rf_ctrl program
34 * distributed by CMU with RAIDframe 1.1.
35 *
36 * This program is the user-land interface to the RAIDframe kernel
37 * driver in NetBSD.
38 */
39 #include <sys/cdefs.h>
40
41 #ifndef lint
42 __RCSID("$NetBSD: raidctl.c,v 1.79 2023/09/17 20:07:39 oster Exp $");
43 #endif
44
45
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/stat.h>
49 #include <sys/disklabel.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <inttypes.h>
59 #include <unistd.h>
60 #include <util.h>
61
62 #include <dev/raidframe/raidframevar.h>
63 #include <dev/raidframe/raidframeio.h>
64 #include "rf_configure.h"
65 #include "prog_ops.h"
66
67 #ifndef RAIDFRAME_REMOVE_COMPONENT
68 #define RAIDFRAME_REMOVE_COMPONENT RAIDFRAME_REMOVE_HOT_SPARE
69 #endif
70
71 #define CONFIGURE_TEST 1 /* must be different from any raidframe ioctl */
72
73 void do_ioctl(int, u_long, void *, const char *);
74 static void rf_configure(int, char*, int);
75 static const char *device_status(RF_DiskStatus_t);
76 static void rf_get_device_status(int);
77 static void rf_output_configuration(int, const char *);
78 static void get_component_number(int, char *, int *, int *);
79 static void rf_fail_disk(int, char *, int);
80 __dead static void usage(void);
81 static void get_component_label(int, char *);
82 static void set_component_label(int, char *);
83 static void init_component_labels(int, int);
84 static void set_autoconfig(int, int, char *);
85 static void add_hot_spare(int, char *);
86 static void remove_component(int, char *);
87 static void rebuild_in_place(int, char *);
88 static void check_status(int,int);
89 static void check_parity(int,int, char *);
90 static void do_meter(int, u_long);
91 static void get_bar(char *, double, int);
92 static void get_time_string(char *, size_t, int);
93 static void rf_output_pmstat(int, int);
94 static void rf_pm_configure(int, int, char *, int[]);
95 static unsigned int xstrtouint(const char *);
96
97 int verbose;
98
99 static const char *rootpart[] = { "No", "Force", "Soft", "*invalid*" };
100
101 static void
102 get_comp(char *buf, char *arg, size_t bufsz)
103 {
104 if (getfsspecname(buf, bufsz, arg) == NULL)
105 errx(1,"%s",buf);
106 }
107
108 int
109 main(int argc,char *argv[])
110 {
111 int ch, i;
112 int num_options;
113 unsigned long action;
114 char config_filename[PATH_MAX];
115 char dev_name[PATH_MAX];
116 char name[PATH_MAX];
117 char component[PATH_MAX];
118 char autoconf[10];
119 char *parityconf = NULL;
120 int parityparams[3];
121 int do_output;
122 int do_recon;
123 int do_rewrite;
124 int raidID;
125 int serial_number;
126 struct stat st;
127 int fd;
128 int force;
129 int openmode;
130 int last_unit;
131
132 num_options = 0;
133 action = 0;
134 do_output = 0;
135 do_recon = 0;
136 do_rewrite = 0;
137 serial_number = 0;
138 force = 0;
139 last_unit = 0;
140 openmode = O_RDWR; /* default to read/write */
141
142 while ((ch = getopt(argc, argv,
143 "a:A:Bc:C:f:F:g:GiI:l:LmM:r:R:sSpPt:uU:v")) != -1)
144 switch (ch) {
145 case 'a':
146 action = RAIDFRAME_ADD_HOT_SPARE;
147 get_comp(component, optarg, sizeof(component));
148 num_options++;
149 break;
150 case 'A':
151 action = RAIDFRAME_SET_AUTOCONFIG;
152 strlcpy(autoconf, optarg, sizeof(autoconf));
153 num_options++;
154 break;
155 case 'B':
156 action = RAIDFRAME_COPYBACK;
157 num_options++;
158 break;
159 case 'c':
160 action = RAIDFRAME_CONFIGURE;
161 strlcpy(config_filename, optarg,
162 sizeof(config_filename));
163 force = 0;
164 num_options++;
165 break;
166 case 'C':
167 strlcpy(config_filename, optarg,
168 sizeof(config_filename));
169 action = RAIDFRAME_CONFIGURE;
170 force = 1;
171 num_options++;
172 break;
173 case 'f':
174 action = RAIDFRAME_FAIL_DISK;
175 get_comp(component, optarg, sizeof(component));
176 do_recon = 0;
177 num_options++;
178 break;
179 case 'F':
180 action = RAIDFRAME_FAIL_DISK;
181 get_comp(component, optarg, sizeof(component));
182 do_recon = 1;
183 num_options++;
184 break;
185 case 'g':
186 action = RAIDFRAME_GET_COMPONENT_LABEL;
187 get_comp(component, optarg, sizeof(component));
188 openmode = O_RDONLY;
189 num_options++;
190 break;
191 case 'G':
192 action = RAIDFRAME_GET_INFO;
193 openmode = O_RDONLY;
194 do_output = 1;
195 num_options++;
196 break;
197 case 'i':
198 action = RAIDFRAME_REWRITEPARITY;
199 num_options++;
200 break;
201 case 'I':
202 action = RAIDFRAME_INIT_LABELS;
203 serial_number = xstrtouint(optarg);
204 num_options++;
205 break;
206 case 'l':
207 action = RAIDFRAME_SET_COMPONENT_LABEL;
208 get_comp(component, optarg, sizeof(component));
209 num_options++;
210 break;
211 case 'L':
212 action = RAIDFRAME_RESCAN;
213 num_options++;
214 break;
215 case 'm':
216 action = RAIDFRAME_PARITYMAP_STATUS;
217 openmode = O_RDONLY;
218 num_options++;
219 break;
220 case 'M':
221 action = RAIDFRAME_PARITYMAP_SET_DISABLE;
222 parityconf = strdup(optarg);
223 num_options++;
224 /* XXXjld: should rf_pm_configure do the strtol()s? */
225 i = 0;
226 while (i < 3 && optind < argc &&
227 isdigit((int)argv[optind][0]))
228 parityparams[i++] = xstrtouint(argv[optind++]);
229 while (i < 3)
230 parityparams[i++] = 0;
231 break;
232 case 'p':
233 action = RAIDFRAME_CHECK_PARITY;
234 openmode = O_RDONLY;
235 num_options++;
236 break;
237 case 'P':
238 action = RAIDFRAME_CHECK_PARITY;
239 do_rewrite = 1;
240 num_options++;
241 break;
242 case 'r':
243 action = RAIDFRAME_REMOVE_COMPONENT;
244 get_comp(component, optarg, sizeof(component));
245 num_options++;
246 break;
247 case 'R':
248 get_comp(component, optarg, sizeof(component));
249 action = RAIDFRAME_REBUILD_IN_PLACE;
250 num_options++;
251 break;
252 case 's':
253 action = RAIDFRAME_GET_INFO;
254 openmode = O_RDONLY;
255 num_options++;
256 break;
257 case 'S':
258 action = RAIDFRAME_CHECK_RECON_STATUS_EXT;
259 openmode = O_RDONLY;
260 num_options++;
261 break;
262 case 't':
263 action = CONFIGURE_TEST;
264 strlcpy(config_filename, optarg,
265 sizeof(config_filename));
266 num_options++;
267 break;
268 case 'u':
269 action = RAIDFRAME_SHUTDOWN;
270 num_options++;
271 break;
272 case 'U':
273 action = RAIDFRAME_SET_LAST_UNIT;
274 num_options++;
275 last_unit = atoi(optarg);
276 if (last_unit < 0)
277 errx(1, "Bad last unit %s", optarg);
278 break;
279 case 'v':
280 verbose = 1;
281 /* Don't bump num_options, as '-v' is not
282 an option like the others */
283 /* num_options++; */
284 break;
285 default:
286 usage();
287 }
288 argc -= optind;
289 argv += optind;
290
291 if (num_options > 1)
292 usage();
293
294 if (action == CONFIGURE_TEST) {
295 RF_Config_t cfg;
296
297 if (argc != 0)
298 usage();
299 if (rf_MakeConfig(config_filename, &cfg) != 0)
300 exit(1);
301 exit(0);;
302 }
303
304 if (argc != 1)
305 usage();
306
307 if (prog_init && prog_init() == -1)
308 err(1, "init failed");
309
310 strlcpy(name, argv[0], sizeof(name));
311 fd = opendisk1(name, openmode, dev_name, sizeof(dev_name), 0,
312 prog_open);
313 if (fd == -1)
314 err(1, "Unable to open device file: %s", name);
315 if (prog_fstat(fd, &st) == -1)
316 err(1, "stat failure on: %s", dev_name);
317 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
318 err(1, "invalid device: %s", dev_name);
319
320 raidID = DISKUNIT(st.st_rdev);
321
322 switch (action) {
323 case RAIDFRAME_ADD_HOT_SPARE:
324 add_hot_spare(fd, component);
325 break;
326 case RAIDFRAME_REMOVE_COMPONENT:
327 remove_component(fd, component);
328 break;
329 case RAIDFRAME_CONFIGURE:
330 rf_configure(fd, config_filename, force);
331 break;
332 case RAIDFRAME_SET_AUTOCONFIG:
333 set_autoconfig(fd, raidID, autoconf);
334 break;
335 case RAIDFRAME_COPYBACK:
336 printf("Copyback.\n");
337 do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
338 if (verbose) {
339 sleep(3); /* XXX give the copyback a chance to start */
340 printf("Copyback status:\n");
341 do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS_EXT);
342 }
343 break;
344 case RAIDFRAME_FAIL_DISK:
345 rf_fail_disk(fd, component, do_recon);
346 break;
347 case RAIDFRAME_SET_COMPONENT_LABEL:
348 set_component_label(fd, component);
349 break;
350 case RAIDFRAME_GET_COMPONENT_LABEL:
351 get_component_label(fd, component);
352 break;
353 case RAIDFRAME_INIT_LABELS:
354 init_component_labels(fd, serial_number);
355 break;
356 case RAIDFRAME_REWRITEPARITY:
357 printf("Initiating re-write of parity\n");
358 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
359 "RAIDFRAME_REWRITEPARITY");
360 if (verbose) {
361 sleep(3); /* XXX give it time to get started */
362 printf("Parity Re-write status:\n");
363 do_meter(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
364 }
365 break;
366 case RAIDFRAME_CHECK_RECON_STATUS_EXT:
367 check_status(fd,1);
368 break;
369 case RAIDFRAME_GET_INFO:
370 if (do_output)
371 rf_output_configuration(fd, dev_name);
372 else
373 rf_get_device_status(fd);
374 break;
375 case RAIDFRAME_PARITYMAP_STATUS:
376 rf_output_pmstat(fd, raidID);
377 break;
378 case RAIDFRAME_PARITYMAP_SET_DISABLE:
379 rf_pm_configure(fd, raidID, parityconf, parityparams);
380 break;
381 case RAIDFRAME_REBUILD_IN_PLACE:
382 rebuild_in_place(fd, component);
383 break;
384 case RAIDFRAME_CHECK_PARITY:
385 check_parity(fd, do_rewrite, dev_name);
386 break;
387 case RAIDFRAME_SHUTDOWN:
388 do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
389 break;
390 case RAIDFRAME_SET_LAST_UNIT:
391 do_ioctl(fd, RAIDFRAME_SET_LAST_UNIT, &last_unit,
392 "RAIDFRAME_SET_LAST_UNIT");
393 break;
394 case RAIDFRAME_RESCAN:
395 do_ioctl(fd, RAIDFRAME_RESCAN, NULL, "RAIDFRAME_RESCAN");
396 break;
397 default:
398 break;
399 }
400
401 prog_close(fd);
402 exit(0);
403 }
404
405 void
406 do_ioctl(int fd, unsigned long command, void *arg, const char *ioctl_name)
407 {
408 if (prog_ioctl(fd, command, arg) == -1)
409 err(1, "ioctl (%s) failed", ioctl_name);
410 }
411
412
413 static void
414 rf_configure(int fd, char *config_file, int force)
415 {
416 void *generic;
417 RF_Config_t cfg;
418
419 if (rf_MakeConfig( config_file, &cfg ) != 0)
420 err(1, "Unable to create RAIDframe configuration structure");
421
422 cfg.force = force;
423
424 /*
425 * Note the extra level of redirection needed here, since
426 * what we really want to pass in is a pointer to the pointer to
427 * the configuration structure.
428 */
429
430 generic = &cfg;
431 do_ioctl(fd, RAIDFRAME_CONFIGURE, &generic, "RAIDFRAME_CONFIGURE");
432 }
433
434 static const char *
435 device_status(RF_DiskStatus_t status)
436 {
437
438 switch (status) {
439 case rf_ds_optimal:
440 return ("optimal");
441 break;
442 case rf_ds_failed:
443 return ("failed");
444 break;
445 case rf_ds_reconstructing:
446 return ("reconstructing");
447 break;
448 case rf_ds_dist_spared:
449 return ("dist_spared");
450 break;
451 case rf_ds_spared:
452 return ("spared");
453 break;
454 case rf_ds_spare:
455 return ("spare");
456 break;
457 case rf_ds_used_spare:
458 return ("used_spare");
459 break;
460 default:
461 return ("UNKNOWN");
462 }
463 /* NOTREACHED */
464 }
465
466 static void
467 rf_get_device_status(int fd)
468 {
469 RF_DeviceConfig_t device_config;
470 void *cfg_ptr;
471 int is_clean;
472 int i, nspares;
473
474 cfg_ptr = &device_config;
475
476 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
477
478 printf("Components:\n");
479 for(i=0; i < device_config.ndevs; i++) {
480 printf("%20s: %s\n", device_config.devs[i].devname,
481 device_status(device_config.devs[i].status));
482 }
483
484 nspares = MIN(device_config.nspares,
485 __arraycount(device_config.spares));
486
487 if (nspares > 0) {
488 printf("Spares:\n");
489 for(i=0; i < nspares; i++) {
490 printf("%20s: %s\n",
491 device_config.spares[i].devname,
492 device_status(device_config.spares[i].status));
493 }
494 } else {
495 printf("No spares.\n");
496 }
497 for(i=0; i < device_config.ndevs; i++) {
498 if (device_config.devs[i].status == rf_ds_optimal) {
499 get_component_label(fd, device_config.devs[i].devname);
500 } else {
501 printf("%s status is: %s. Skipping label.\n",
502 device_config.devs[i].devname,
503 device_status(device_config.devs[i].status));
504 }
505 }
506
507 if (nspares > 0) {
508 for(i=0; i < nspares; i++) {
509 if ((device_config.spares[i].status ==
510 rf_ds_optimal) ||
511 (device_config.spares[i].status ==
512 rf_ds_used_spare)) {
513 get_component_label(fd,
514 device_config.spares[i].devname);
515 } else {
516 printf("%s status is: %s. Skipping label.\n",
517 device_config.spares[i].devname,
518 device_status(
519 device_config.spares[i].status));
520 }
521 }
522 }
523
524 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
525 "RAIDFRAME_CHECK_PARITY");
526 if (is_clean) {
527 printf("Parity status: clean\n");
528 } else {
529 printf("Parity status: DIRTY\n");
530 }
531 check_status(fd,0);
532 }
533
534 static void
535 rf_output_pmstat(int fd, int raidID)
536 {
537 char srs[7];
538 unsigned int i, j;
539 int dis, dr;
540 struct rf_pmstat st;
541
542 if (prog_ioctl(fd, RAIDFRAME_PARITYMAP_STATUS, &st) == -1) {
543 if (errno == EINVAL) {
544 printf("raid%d: has no parity; parity map disabled\n",
545 raidID);
546 return;
547 }
548 err(1, "ioctl (%s) failed", "RAIDFRAME_PARITYMAP_STATUS");
549 }
550
551 if (st.enabled) {
552 if (0 > humanize_number(srs, 7, st.region_size * DEV_BSIZE,
553 "B", HN_AUTOSCALE, HN_NOSPACE))
554 strlcpy(srs, "???", 7);
555
556 printf("raid%d: parity map enabled with %u regions of %s\n",
557 raidID, st.params.regions, srs);
558 printf("raid%d: regions marked clean after %d intervals of"
559 " %d.%03ds\n", raidID, st.params.cooldown,
560 st.params.tickms / 1000, st.params.tickms % 1000);
561 printf("raid%d: write/sync/clean counters "
562 "%"PRIu64"/%"PRIu64"/%"PRIu64"\n", raidID,
563 st.ctrs.nwrite, st.ctrs.ncachesync, st.ctrs.nclearing);
564
565 dr = 0;
566 for (i = 0; i < st.params.regions; i++)
567 if (isset(st.dirty, i))
568 dr++;
569 printf("raid%d: %d dirty region%s\n", raidID, dr,
570 dr == 1 ? "" : "s");
571
572 if (verbose > 0) {
573 for (i = 0; i < RF_PARITYMAP_NBYTE; i += 32) {
574 printf(" ");
575 for (j = i; j < RF_PARITYMAP_NBYTE
576 && j < i + 32; j++)
577 printf("%x%x", st.dirty[j] & 15,
578 (st.dirty[j] >> 4) & 15);
579 printf("\n");
580 }
581 }
582 } else {
583 printf("raid%d: parity map disabled\n", raidID);
584 }
585
586 do_ioctl(fd, RAIDFRAME_PARITYMAP_GET_DISABLE, &dis,
587 "RAIDFRAME_PARITYMAP_GET_DISABLE");
588 printf("raid%d: parity map will %s %sabled on next configure\n",
589 raidID, dis == st.enabled ? "be" : "remain", dis ? "dis" : "en");
590 }
591
592 static void
593 rf_pm_configure(int fd, int raidID, char *parityconf, int parityparams[])
594 {
595 int dis;
596 struct rf_pmparams params;
597
598 if (strcasecmp(parityconf, "yes") == 0)
599 dis = 0;
600 else if (strcasecmp(parityconf, "no") == 0)
601 dis = 1;
602 else if (strcasecmp(parityconf, "set") == 0) {
603 params.cooldown = parityparams[0];
604 params.tickms = parityparams[1];
605 params.regions = parityparams[2];
606
607 do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_PARAMS, ¶ms,
608 "RAIDFRAME_PARITYMAP_SET_PARAMS");
609
610 if (params.cooldown != 0 || params.tickms != 0) {
611 printf("raid%d: parity cleaned after", raidID);
612 if (params.cooldown != 0)
613 printf(" %d", params.cooldown);
614 printf(" intervals");
615 if (params.tickms != 0) {
616 printf(" of %d.%03ds", params.tickms / 1000,
617 params.tickms % 1000);
618 }
619 printf("\n");
620 }
621 if (params.regions != 0)
622 printf("raid%d: will use %d regions on next"
623 " configuration\n", raidID, params.regions);
624
625 return;
626 /* XXX the control flow here could be prettier. */
627 } else
628 err(1, "`%s' is not a valid parity map command", parityconf);
629
630 do_ioctl(fd, RAIDFRAME_PARITYMAP_SET_DISABLE, &dis,
631 "RAIDFRAME_PARITYMAP_SET_DISABLE");
632 printf("raid%d: parity map will be %sabled on next configure\n",
633 raidID, dis ? "dis" : "en");
634 }
635
636 /* convert "component0" into "absent" */
637 static const char *rf_output_devname(const char *name)
638 {
639
640 if (strncmp(name, "component", 9) == 0)
641 return "absent";
642 return name;
643 }
644
645 static void
646 rf_output_configuration(int fd, const char *name)
647 {
648 RF_DeviceConfig_t device_config;
649 void *cfg_ptr;
650 int i, nspares;
651 RF_ComponentLabel_t component_label;
652 void *label_ptr;
653 int component_num;
654 int num_cols;
655
656 cfg_ptr = &device_config;
657
658 printf("# raidctl config file for %s\n", name);
659 printf("\n");
660 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
661
662 nspares = MIN(device_config.nspares,
663 __arraycount(device_config.spares));
664
665 printf("START array\n");
666 printf("# numCol numSpare\n");
667 printf("%d %d\n", device_config.cols, device_config.nspares);
668 printf("\n");
669
670 printf("START disks\n");
671 for(i=0; i < device_config.ndevs; i++)
672 printf("%s\n",
673 rf_output_devname(device_config.devs[i].devname));
674 printf("\n");
675
676 if (nspares > 0) {
677 printf("START spare\n");
678 for(i=0; i < nspares; i++)
679 printf("%s\n", device_config.spares[i].devname);
680 printf("\n");
681 }
682
683 for(i=0; i < device_config.ndevs; i++) {
684 if (device_config.devs[i].status == rf_ds_optimal)
685 break;
686 }
687 if (i == device_config.ndevs) {
688 printf("# WARNING: no optimal components; using %s\n",
689 device_config.devs[0].devname);
690 i = 0;
691 }
692 get_component_number(fd, device_config.devs[i].devname,
693 &component_num, &num_cols);
694 memset(&component_label, 0, sizeof(RF_ComponentLabel_t));
695 component_label.row = component_num / num_cols;
696 component_label.column = component_num % num_cols;
697 label_ptr = &component_label;
698 do_ioctl(fd, RAIDFRAME_GET_COMPONENT_LABEL, label_ptr,
699 "RAIDFRAME_GET_COMPONENT_LABEL");
700
701 printf("START layout\n");
702 printf(
703 "# sectPerSU SUsPerParityUnit SUsPerReconUnit RAID_level_%c\n",
704 (char) component_label.parityConfig);
705 printf("%d %d %d %c\n",
706 component_label.sectPerSU, component_label.SUsPerPU,
707 component_label.SUsPerRU, (char) component_label.parityConfig);
708 printf("\n");
709
710 printf("START queue\n");
711 printf("fifo %d\n", device_config.maxqdepth);
712 }
713
714 static void
715 get_component_number(int fd, char *component_name, int *component_number,
716 int *num_columns)
717 {
718 RF_DeviceConfig_t device_config;
719 void *cfg_ptr;
720 int i, nspares;
721 int found;
722
723 *component_number = -1;
724
725 /* Assuming a full path spec... */
726 cfg_ptr = &device_config;
727 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
728 "RAIDFRAME_GET_INFO");
729
730 *num_columns = device_config.cols;
731
732 nspares = MIN(device_config.nspares,
733 __arraycount(device_config.spares));
734
735 found = 0;
736 for(i=0; i < device_config.ndevs; i++) {
737 if (strncmp(component_name, device_config.devs[i].devname,
738 PATH_MAX)==0) {
739 found = 1;
740 *component_number = i;
741 }
742 }
743 if (!found) { /* maybe it's a spare? */
744 for(i=0; i < nspares; i++) {
745 if (strncmp(component_name,
746 device_config.spares[i].devname,
747 PATH_MAX)==0) {
748 found = 1;
749 *component_number = i + device_config.ndevs;
750 /* the way spares are done should
751 really change... */
752 *num_columns = device_config.cols +
753 device_config.nspares;
754 }
755 }
756 }
757
758 if (!found)
759 err(1,"%s is not a component of this device", component_name);
760 }
761
762 static void
763 rf_fail_disk(int fd, char *component_to_fail, int do_recon)
764 {
765 struct rf_recon_req recon_request;
766 int component_num;
767 int num_cols;
768
769 get_component_number(fd, component_to_fail, &component_num, &num_cols);
770
771 recon_request.col = component_num % num_cols;
772 if (do_recon) {
773 recon_request.flags = RF_FDFLAGS_RECON;
774 } else {
775 recon_request.flags = RF_FDFLAGS_NONE;
776 }
777 do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
778 "RAIDFRAME_FAIL_DISK");
779 if (do_recon && verbose) {
780 printf("Reconstruction status:\n");
781 sleep(3); /* XXX give reconstruction a chance to start */
782 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
783 }
784 }
785
786 static void
787 get_component_label(int fd, char *component)
788 {
789 RF_ComponentLabel_t component_label;
790 void *label_ptr;
791 int component_num;
792 int num_cols;
793
794 get_component_number(fd, component, &component_num, &num_cols);
795
796 memset( &component_label, 0, sizeof(RF_ComponentLabel_t));
797 component_label.row = component_num / num_cols;
798 component_label.column = component_num % num_cols;
799
800 label_ptr = &component_label;
801 do_ioctl( fd, RAIDFRAME_GET_COMPONENT_LABEL, label_ptr,
802 "RAIDFRAME_GET_COMPONENT_LABEL");
803
804 printf("Component label for %s:\n",component);
805
806 printf(" Row: %d, Column: %d, Num Rows: %d, Num Columns: %d\n",
807 component_label.row, component_label.column,
808 component_label.num_rows, component_label.num_columns);
809 printf(" Version: %d, Serial Number: %u, Mod Counter: %d\n",
810 component_label.version, component_label.serial_number,
811 component_label.mod_counter);
812 printf(" Clean: %s, Status: %d\n",
813 component_label.clean ? "Yes" : "No",
814 component_label.status );
815 printf(" sectPerSU: %d, SUsPerPU: %d, SUsPerRU: %d\n",
816 component_label.sectPerSU, component_label.SUsPerPU,
817 component_label.SUsPerRU);
818 printf(" Queue size: %d, blocksize: %d, numBlocks: %"PRIu64"\n",
819 component_label.maxOutstanding, component_label.blockSize,
820 rf_component_label_numblocks(&component_label));
821 printf(" RAID Level: %c\n", (char) component_label.parityConfig);
822 printf(" Autoconfig: %s\n",
823 component_label.autoconfigure ? "Yes" : "No" );
824 printf(" Root partition: %s\n",
825 rootpart[component_label.root_partition & 3]);
826 printf(" Last configured as: raid%d\n", component_label.last_unit );
827 }
828
829 static void
830 set_component_label(int fd, char *component)
831 {
832 RF_ComponentLabel_t component_label;
833 int component_num;
834 int num_cols;
835
836 get_component_number(fd, component, &component_num, &num_cols);
837
838 /* XXX This is currently here for testing, and future expandability */
839
840 component_label.version = 1;
841 component_label.serial_number = 123456;
842 component_label.mod_counter = 0;
843 component_label.row = component_num / num_cols;
844 component_label.column = component_num % num_cols;
845 component_label.num_rows = 0;
846 component_label.num_columns = 5;
847 component_label.clean = 0;
848 component_label.status = 1;
849
850 do_ioctl( fd, RAIDFRAME_SET_COMPONENT_LABEL, &component_label,
851 "RAIDFRAME_SET_COMPONENT_LABEL");
852 }
853
854
855 static void
856 init_component_labels(int fd, int serial_number)
857 {
858 RF_ComponentLabel_t component_label;
859
860 component_label.version = 0;
861 component_label.serial_number = serial_number;
862 component_label.mod_counter = 0;
863 component_label.row = 0;
864 component_label.column = 0;
865 component_label.num_rows = 0;
866 component_label.num_columns = 0;
867 component_label.clean = 0;
868 component_label.status = 0;
869
870 do_ioctl( fd, RAIDFRAME_INIT_LABELS, &component_label,
871 "RAIDFRAME_INIT_LABELS");
872 }
873
874 static void
875 set_autoconfig(int fd, int raidID, char *autoconf)
876 {
877 int auto_config;
878 int root_config;
879
880 auto_config = 0;
881 root_config = 0;
882
883 if (strncasecmp(autoconf, "root", 4) == 0 ||
884 strncasecmp(autoconf, "hard", 4) == 0 ||
885 strncasecmp(autoconf, "force", 5) == 0) {
886 root_config = 1;
887 } else if (strncasecmp(autoconf, "soft", 4) == 0) {
888 root_config = 2;
889 }
890
891 if ((strncasecmp(autoconf,"yes", 3) == 0) ||
892 root_config > 0) {
893 auto_config = 1;
894 }
895
896 do_ioctl(fd, RAIDFRAME_SET_AUTOCONFIG, &auto_config,
897 "RAIDFRAME_SET_AUTOCONFIG");
898
899 do_ioctl(fd, RAIDFRAME_SET_ROOT, &root_config,
900 "RAIDFRAME_SET_ROOT");
901
902 printf("raid%d: Autoconfigure: %s\n", raidID,
903 auto_config ? "Yes" : "No");
904
905 if (auto_config == 1) {
906 printf("raid%d: Root: %s\n", raidID, rootpart[root_config]);
907 }
908 }
909
910 static void
911 add_hot_spare(int fd, char *component)
912 {
913 RF_SingleComponent_t hot_spare;
914
915 hot_spare.row = 0;
916 hot_spare.column = 0;
917 strncpy(hot_spare.component_name, component,
918 sizeof(hot_spare.component_name));
919
920 do_ioctl( fd, RAIDFRAME_ADD_HOT_SPARE, &hot_spare,
921 "RAIDFRAME_ADD_HOT_SPARE");
922 }
923
924 static void
925 remove_component(int fd, char *component)
926 {
927 RF_SingleComponent_t comp;
928 int component_num;
929 int num_cols;
930
931 get_component_number(fd, component, &component_num, &num_cols);
932
933 comp.row = component_num / num_cols;
934 comp.column = component_num % num_cols;
935
936 strncpy(comp.component_name, component,
937 sizeof(comp.component_name));
938
939 do_ioctl( fd, RAIDFRAME_REMOVE_COMPONENT, &comp,
940 "RAIDFRAME_REMOVE_COMPONENT");
941 }
942
943 static void
944 rebuild_in_place(int fd, char *component)
945 {
946 RF_SingleComponent_t comp;
947 int component_num;
948 int num_cols;
949
950 get_component_number(fd, component, &component_num, &num_cols);
951
952 comp.row = 0;
953 comp.column = component_num;
954 strncpy(comp.component_name, component, sizeof(comp.component_name));
955
956 do_ioctl( fd, RAIDFRAME_REBUILD_IN_PLACE, &comp,
957 "RAIDFRAME_REBUILD_IN_PLACE");
958
959 if (verbose) {
960 printf("Reconstruction status:\n");
961 sleep(3); /* XXX give reconstruction a chance to start */
962 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
963 }
964
965 }
966
967 static void
968 check_parity(int fd, int do_rewrite, char *dev_name)
969 {
970 int is_clean;
971 int percent_done;
972
973 is_clean = 0;
974 percent_done = 0;
975 do_ioctl(fd, RAIDFRAME_CHECK_PARITY, &is_clean,
976 "RAIDFRAME_CHECK_PARITY");
977 if (is_clean) {
978 printf("%s: Parity status: clean\n",dev_name);
979 } else {
980 printf("%s: Parity status: DIRTY\n",dev_name);
981 if (do_rewrite) {
982 printf("%s: Initiating re-write of parity\n",
983 dev_name);
984 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
985 "RAIDFRAME_REWRITEPARITY");
986 sleep(3); /* XXX give it time to
987 get started. */
988 if (verbose) {
989 printf("Parity Re-write status:\n");
990 do_meter(fd,
991 RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
992 } else {
993 do_ioctl(fd,
994 RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
995 &percent_done,
996 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS"
997 );
998 while( percent_done < 100 ) {
999 sleep(3); /* wait a bit... */
1000 do_ioctl(fd,
1001 RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
1002 &percent_done,
1003 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
1004 }
1005
1006 }
1007 printf("%s: Parity Re-write complete\n", dev_name);
1008 } else {
1009 /* parity is wrong, and is not being fixed.
1010 Exit w/ an error. */
1011 exit(1);
1012 }
1013 }
1014 }
1015
1016
1017 static void
1018 check_status(int fd, int meter)
1019 {
1020 int recon_percent_done = 0;
1021 int parity_percent_done = 0;
1022 int copyback_percent_done = 0;
1023
1024 do_ioctl(fd, RAIDFRAME_CHECK_RECON_STATUS, &recon_percent_done,
1025 "RAIDFRAME_CHECK_RECON_STATUS");
1026 printf("Reconstruction is %d%% complete.\n", recon_percent_done);
1027 do_ioctl(fd, RAIDFRAME_CHECK_PARITYREWRITE_STATUS,
1028 &parity_percent_done,
1029 "RAIDFRAME_CHECK_PARITYREWRITE_STATUS");
1030 printf("Parity Re-write is %d%% complete.\n", parity_percent_done);
1031 do_ioctl(fd, RAIDFRAME_CHECK_COPYBACK_STATUS, ©back_percent_done,
1032 "RAIDFRAME_CHECK_COPYBACK_STATUS");
1033 printf("Copyback is %d%% complete.\n", copyback_percent_done);
1034
1035 if (meter) {
1036 /* These 3 should be mutually exclusive at this point */
1037 if (recon_percent_done < 100) {
1038 printf("Reconstruction status:\n");
1039 do_meter(fd,RAIDFRAME_CHECK_RECON_STATUS_EXT);
1040 } else if (parity_percent_done < 100) {
1041 printf("Parity Re-write status:\n");
1042 do_meter(fd,RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT);
1043 } else if (copyback_percent_done < 100) {
1044 printf("Copyback status:\n");
1045 do_meter(fd,RAIDFRAME_CHECK_COPYBACK_STATUS_EXT);
1046 }
1047 }
1048 }
1049
1050 const char *tbits = "|/-\\";
1051
1052 static void
1053 do_meter(int fd, u_long option)
1054 {
1055 int percent_done;
1056 RF_uint64 start_value;
1057 RF_ProgressInfo_t progressInfo;
1058 void *pInfoPtr;
1059 struct timeval start_time;
1060 struct timeval current_time;
1061 double elapsed;
1062 int elapsed_sec;
1063 int elapsed_usec;
1064 int simple_eta,last_eta;
1065 double rate;
1066 RF_uint64 amount;
1067 int tbit_value;
1068 char bar_buffer[1024];
1069 char eta_buffer[1024];
1070
1071 if (gettimeofday(&start_time,NULL) == -1)
1072 err(1, "gettimeofday failed!?!?");
1073 memset(&progressInfo, 0, sizeof(RF_ProgressInfo_t));
1074 pInfoPtr=&progressInfo;
1075
1076 percent_done = 0;
1077 do_ioctl(fd, option, pInfoPtr, "");
1078 start_value = progressInfo.completed;
1079 current_time = start_time;
1080 simple_eta = 0;
1081 last_eta = 0;
1082
1083 tbit_value = 0;
1084 while(progressInfo.completed < progressInfo.total) {
1085
1086 percent_done = (progressInfo.completed * 100) /
1087 progressInfo.total;
1088
1089 get_bar(bar_buffer, percent_done, 40);
1090
1091 elapsed_sec = current_time.tv_sec - start_time.tv_sec;
1092 elapsed_usec = current_time.tv_usec - start_time.tv_usec;
1093 if (elapsed_usec < 0) {
1094 elapsed_usec-=1000000;
1095 elapsed_sec++;
1096 }
1097
1098 elapsed = (double) elapsed_sec +
1099 (double) elapsed_usec / 1000000.0;
1100
1101 amount = progressInfo.completed - start_value;
1102
1103 if (amount <= 0) { /* we don't do negatives (yet?) */
1104 amount = 0;
1105 }
1106
1107 if (elapsed == 0)
1108 rate = 0.0;
1109 else
1110 rate = amount / elapsed;
1111
1112 if (rate > 0.0) {
1113 simple_eta = (int) (((double)progressInfo.total -
1114 (double) progressInfo.completed)
1115 / rate);
1116 } else {
1117 simple_eta = -1;
1118 }
1119
1120 if (simple_eta <=0) {
1121 simple_eta = last_eta;
1122 } else {
1123 last_eta = simple_eta;
1124 }
1125
1126 get_time_string(eta_buffer, sizeof eta_buffer, simple_eta);
1127
1128 fprintf(stdout,"\r%3d%% |%s| ETA: %s %c",
1129 percent_done,bar_buffer,eta_buffer,tbits[tbit_value]);
1130 fflush(stdout);
1131
1132 if (++tbit_value>3)
1133 tbit_value = 0;
1134
1135 sleep(2);
1136
1137 if (gettimeofday(¤t_time,NULL) == -1)
1138 err(1, "gettimeofday failed!?!?");
1139
1140 do_ioctl( fd, option, pInfoPtr, "");
1141
1142
1143 }
1144 printf("\n");
1145 }
1146 /* 40 '*''s per line, then 40 ' ''s line. */
1147 /* If you've got a screen wider than 160 characters, "tough" */
1148
1149 #define STAR_MIDPOINT 4*40
1150 const char stars[] = "****************************************"
1151 "****************************************"
1152 "****************************************"
1153 "****************************************"
1154 " "
1155 " "
1156 " "
1157 " "
1158 " ";
1159
1160 static void
1161 get_bar(char *string, double percent, int max_strlen)
1162 {
1163 int offset;
1164
1165 if (max_strlen > STAR_MIDPOINT) {
1166 max_strlen = STAR_MIDPOINT;
1167 }
1168 offset = STAR_MIDPOINT -
1169 (int)((percent * max_strlen)/ 100);
1170 if (offset < 0)
1171 offset = 0;
1172 snprintf(string,max_strlen,"%s",stars+offset);
1173 }
1174
1175 static void
1176 get_time_string(char *string, size_t len, int simple_time)
1177 {
1178 int minutes, seconds, hours;
1179 char hours_buffer[8];
1180 char minutes_buffer[5];
1181 char seconds_buffer[5];
1182
1183 if (simple_time >= 0) {
1184
1185 minutes = simple_time / 60;
1186 seconds = simple_time - 60*minutes;
1187 hours = minutes / 60;
1188 minutes = minutes - 60*hours;
1189 #if defined(__GNUC__)
1190 /*
1191 * snprintf() truncation checker fails to detect that seconds
1192 * and minutes will be 0-59 range.
1193 */
1194 if (minutes < 0 || minutes > 60)
1195 minutes = 60;
1196 if (seconds < 0 || seconds > 60)
1197 seconds = 60;
1198 #endif
1199
1200 if (hours > 0) {
1201 snprintf(hours_buffer,sizeof hours_buffer,
1202 "%02d:",hours);
1203 } else {
1204 snprintf(hours_buffer,sizeof hours_buffer," ");
1205 }
1206
1207 snprintf(minutes_buffer,sizeof minutes_buffer,"%02d:",minutes);
1208 snprintf(seconds_buffer,sizeof seconds_buffer,"%02d",seconds);
1209 snprintf(string,len,"%s%s%s",
1210 hours_buffer, minutes_buffer, seconds_buffer);
1211 } else {
1212 snprintf(string,len," --:--");
1213 }
1214
1215 }
1216
1217 static void
1218 usage(void)
1219 {
1220 const char *progname = getprogname();
1221
1222 fprintf(stderr,
1223 "usage: %s [-v] -A [yes | no | softroot | hardroot] dev\n",
1224 progname);
1225 fprintf(stderr, " %s [-v] -a component dev\n", progname);
1226 fprintf(stderr, " %s [-v] -B dev\n", progname);
1227 fprintf(stderr, " %s [-v] -C config_file dev\n", progname);
1228 fprintf(stderr, " %s [-v] -c config_file dev\n", progname);
1229 fprintf(stderr, " %s [-v] -F component dev\n", progname);
1230 fprintf(stderr, " %s [-v] -f component dev\n", progname);
1231 fprintf(stderr, " %s [-v] -G dev\n", progname);
1232 fprintf(stderr, " %s [-v] -g component dev\n", progname);
1233 fprintf(stderr, " %s [-v] -I serial_number dev\n", progname);
1234 fprintf(stderr, " %s [-v] -i dev\n", progname);
1235 fprintf(stderr, " %s [-v] -M [yes | no | set params] dev\n",
1236 progname);
1237 fprintf(stderr, " %s [-v] -m dev\n", progname);
1238 fprintf(stderr, " %s [-v] -P dev\n", progname);
1239 fprintf(stderr, " %s [-v] -p dev\n", progname);
1240 fprintf(stderr, " %s [-v] -R component dev\n", progname);
1241 fprintf(stderr, " %s [-v] -r component dev\n", progname);
1242 fprintf(stderr, " %s [-v] -S dev\n", progname);
1243 fprintf(stderr, " %s [-v] -s dev\n", progname);
1244 fprintf(stderr, " %s [-v] -t config_file\n", progname);
1245 fprintf(stderr, " %s [-v] -U unit dev\n", progname);
1246 fprintf(stderr, " %s [-v] -u dev\n", progname);
1247 exit(1);
1248 /* NOTREACHED */
1249 }
1250
1251 static unsigned int
1252 xstrtouint(const char *str)
1253 {
1254 int e;
1255 unsigned int num = (unsigned int)strtou(str, NULL, 10, 0, INT_MAX, &e);
1256 if (e)
1257 errc(EXIT_FAILURE, e, "Bad number `%s'", str);
1258 return num;
1259 }
1260