raidctl.c revision 1.4 1 /* $NetBSD: raidctl.c,v 1.4 1999/02/04 14:50:31 oster Exp $ */
2 /*-
3 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Greg Oster
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39
40 This program is a re-write of the original rf_ctrl program
41 distributed by CMU with RAIDframe 1.1.
42
43 This program is the user-land interface to the RAIDframe kernel
44 driver in NetBSD.
45
46 */
47
48 #include <sys/param.h>
49 #include <sys/ioctl.h>
50 #include <sys/stat.h>
51 #include <util.h>
52 #include <stdio.h>
53 #include <fcntl.h>
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <sys/types.h>
58 #include <string.h>
59 #include <sys/disklabel.h>
60 #include <machine/disklabel.h>
61 #include <unistd.h>
62
63 #include "rf_raidframe.h"
64
65 extern char *__progname;
66
67 int main __P((int, char *[]));
68 static void do_ioctl __P((int, unsigned long, void *, char *));
69 static void rf_configure __P((int, char*));
70 static char *device_status __P((RF_DiskStatus_t));
71 static void rf_get_device_status __P((int));
72 static void rf_fail_disk __P((int, char *, int));
73 static void usage __P((void));
74
75 int
76 main(argc,argv)
77 int argc;
78 char *argv[];
79 {
80 extern char *optarg;
81 extern int optind;
82 int ch;
83 int num_options;
84 unsigned long action;
85 char config_filename[PATH_MAX];
86 char dev_name[PATH_MAX];
87 char name[PATH_MAX];
88 char component_to_fail[PATH_MAX];
89 int do_recon;
90 int raidID;
91 int rawpart;
92 int recon_percent_done;
93 struct stat st;
94 int fd;
95
96 num_options = 0;
97 action = 0;
98 do_recon = 0;
99
100 while ((ch = getopt(argc, argv, "c:Cf:F:rRsu")) != -1)
101 switch(ch) {
102 case 'c':
103 strncpy(config_filename,optarg,PATH_MAX);
104 action = RAIDFRAME_CONFIGURE;
105 num_options++;
106 break;
107 case 'C':
108 action = RAIDFRAME_COPYBACK;
109 num_options++;
110 break;
111 case 'f':
112 action = RAIDFRAME_FAIL_DISK;
113 do_recon = 0;
114 strncpy(component_to_fail, optarg, PATH_MAX);
115 num_options++;
116 break;
117 case 'F':
118 action = RAIDFRAME_FAIL_DISK;
119 do_recon = 1;
120 strncpy(component_to_fail, optarg, PATH_MAX);
121 num_options++;
122 break;
123 case 'r':
124 action = RAIDFRAME_REWRITEPARITY;
125 num_options++;
126 break;
127 case 'R':
128 action = RAIDFRAME_CHECKRECON;
129 num_options++;
130 break;
131 case 's':
132 action = RAIDFRAME_GET_INFO;
133 num_options++;
134 break;
135 case 'u':
136 action = RAIDFRAME_SHUTDOWN;
137 num_options++;
138 break;
139 default:
140 usage();
141 }
142 argc -= optind;
143 argv += optind;
144
145 if ((num_options > 1) || (argc == NULL))
146 usage();
147
148 strncpy(name,argv[0],PATH_MAX);
149
150 if ((name[0] == '/') || (name[0] == '.')) {
151 /* they've (apparently) given a full path... */
152 strncpy(dev_name, name, PATH_MAX);
153 } else {
154 if (isdigit(name[strlen(name)-1])) {
155 rawpart = getrawpartition();
156 snprintf(dev_name,PATH_MAX,"/dev/%s%c",name,
157 'a'+rawpart);
158 } else {
159 snprintf(dev_name,PATH_MAX,"/dev/%s",name);
160 }
161 }
162
163 if (stat(dev_name, &st) != 0) {
164 fprintf(stderr,"%s: stat failure on: %s\n",
165 __progname,dev_name);
166 return (errno);
167 }
168 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) {
169 fprintf(stderr,"%s: invalid device: %s\n",
170 __progname,dev_name);
171 return (EINVAL);
172 }
173
174 raidID = RF_DEV2RAIDID(st.st_rdev);
175
176 if ((fd = open( dev_name, O_RDWR, 0640)) < 0) {
177 fprintf(stderr, "%s: unable to open device file: %s\n",
178 __progname, dev_name);
179 exit(1);
180 }
181
182
183 switch(action) {
184 case RAIDFRAME_CONFIGURE:
185 rf_configure(fd, config_filename);
186 break;
187 case RAIDFRAME_COPYBACK:
188 printf("Copyback.\n");
189 do_ioctl(fd, RAIDFRAME_COPYBACK, NULL, "RAIDFRAME_COPYBACK");
190 break;
191 case RAIDFRAME_FAIL_DISK:
192 rf_fail_disk(fd,component_to_fail,do_recon);
193 break;
194 case RAIDFRAME_REWRITEPARITY:
195 printf("Initiating re-write of parity\n");
196 do_ioctl(fd, RAIDFRAME_REWRITEPARITY, NULL,
197 "RAIDFRAME_REWRITEPARITY");
198 break;
199 case RAIDFRAME_CHECKRECON:
200 do_ioctl(fd, RAIDFRAME_CHECKRECON, &recon_percent_done,
201 "RAIDFRAME_CHECKRECON");
202 printf("Reconstruction is %d%% complete.\n",
203 recon_percent_done);
204 break;
205 case RAIDFRAME_GET_INFO:
206 rf_get_device_status(fd);
207 break;
208 case RAIDFRAME_SHUTDOWN:
209 do_ioctl(fd, RAIDFRAME_SHUTDOWN, NULL, "RAIDFRAME_SHUTDOWN");
210 break;
211 default:
212 break;
213 }
214
215 close(fd);
216 exit(0);
217 }
218
219 static void
220 do_ioctl(fd, command, arg, ioctl_name)
221 int fd;
222 unsigned long command;
223 void *arg;
224 char *ioctl_name;
225 {
226 if (ioctl(fd, command, arg) < 0) {
227 warn("ioctl (%s) failed", ioctl_name);
228 exit(1);
229 }
230 }
231
232
233 static void
234 rf_configure(fd,config_file)
235 int fd;
236 char *config_file;
237 {
238 void *generic;
239 RF_Config_t cfg;
240
241 if (rf_MakeConfig( config_file, &cfg ) < 0) {
242 fprintf(stderr,"%s: unable to create RAIDframe %s\n",
243 __progname, "configuration structure\n");
244 exit(1);
245 }
246
247 /*
248
249 Note the extra level of redirection needed here, since
250 what we really want to pass in is a pointer to the pointer to
251 the configuration structure.
252
253 */
254
255 generic = (void *) &cfg;
256 do_ioctl(fd,RAIDFRAME_CONFIGURE,&generic,"RAIDFRAME_CONFIGURE");
257 #if 0
258 if (ioctl(fd, RAIDFRAME_CONFIGURE, &generic) < 0) {
259 warn("ioctl (RAIDFRAME_CONFIGURE): failed\n");
260 exit(1);
261 }
262 #endif
263 }
264
265 static char *
266 device_status(status)
267 RF_DiskStatus_t status;
268 {
269 static char status_string[256];
270
271 switch (status) {
272 case rf_ds_optimal:
273 strcpy(status_string,"optimal");
274 break;
275 case rf_ds_failed:
276 strcpy(status_string,"failed");
277 break;
278 case rf_ds_reconstructing:
279 strcpy(status_string,"reconstructing");
280 break;
281 case rf_ds_dist_spared:
282 strcpy(status_string,"dist_spared");
283 break;
284 case rf_ds_spared:
285 strcpy(status_string,"spared");
286 break;
287 case rf_ds_spare:
288 strcpy(status_string,"spare");
289 break;
290 case rf_ds_used_spare:
291 strcpy(status_string,"used_spare");
292 break;
293 default:
294 strcpy(status_string,"UNKNOWN");
295 break;
296 }
297 return(status_string);
298 }
299
300 static void
301 rf_get_device_status(fd)
302 int fd;
303 {
304 RF_DeviceConfig_t device_config;
305 void *cfg_ptr;
306 int i;
307
308 cfg_ptr = &device_config;
309
310 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr, "RAIDFRAME_GET_INFO");
311
312 printf("Components:\n");
313 for(i=0; i < device_config.ndevs; i++) {
314 printf("%20s: %s\n", device_config.devs[i].devname,
315 device_status(device_config.devs[i].status));
316 }
317 if (device_config.nspares > 0) {
318 printf("Spares:\n");
319 for(i=0; i < device_config.nspares; i++) {
320 printf("%20s [%d][%d]: %s\n",
321 device_config.spares[i].devname,
322 device_config.spares[i].spareRow,
323 device_config.spares[i].spareCol,
324 device_status(device_config.spares[i].status));
325 }
326 } else {
327 printf("No spares.\n");
328 }
329
330 }
331
332 static void
333 rf_fail_disk(fd, component_to_fail, do_recon)
334 int fd;
335 char *component_to_fail;
336 int do_recon;
337 {
338 struct rf_recon_req recon_request;
339 RF_DeviceConfig_t device_config;
340 void *cfg_ptr;
341 int i;
342 int found;
343 int component_num;
344
345 component_num = -1;
346
347 /* Assuming a full path spec... */
348 cfg_ptr = &device_config;
349 do_ioctl(fd, RAIDFRAME_GET_INFO, &cfg_ptr,
350 "RAIDFRAME_GET_INFO");
351 found = 0;
352 for(i=0; i < device_config.ndevs; i++) {
353 if (strncmp(component_to_fail,
354 device_config.devs[i].devname,
355 PATH_MAX)==0) {
356 found = 1;
357 component_num = i;
358 }
359 }
360 if (!found) {
361 fprintf(stderr,"%s: %s is not a component %s",
362 __progname, component_to_fail,
363 "of this device\n");
364 exit(1);
365 }
366
367 recon_request.row = component_num / device_config.cols;
368 recon_request.col = component_num % device_config.cols;
369 if (do_recon) {
370 recon_request.flags = RF_FDFLAGS_RECON;
371 } else {
372 recon_request.flags = RF_FDFLAGS_NONE;
373 }
374 do_ioctl(fd, RAIDFRAME_FAIL_DISK, &recon_request,
375 "RAIDFRAME_FAIL_DISK");
376
377 }
378
379 static void
380 usage()
381 {
382 fprintf(stderr, "usage: %s -c config_file dev\n", __progname);
383 fprintf(stderr, " %s -C dev\n", __progname);
384 fprintf(stderr, " %s -f component dev\n", __progname);
385 fprintf(stderr, " %s -F component dev\n", __progname);
386 fprintf(stderr, " %s -r dev\n", __progname);
387 fprintf(stderr, " %s -R dev\n", __progname);
388 fprintf(stderr, " %s -s dev\n", __progname);
389 fprintf(stderr, " %s -u dev\n", __progname);
390 exit(1);
391 /* NOTREACHED */
392 }
393