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