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