bioctl.c revision 1.9 1 1.9 xtraeme /* $NetBSD: bioctl.c,v 1.9 2008/03/01 16:08:41 xtraeme Exp $ */
2 1.7 xtraeme /* $OpenBSD: bioctl.c,v 1.52 2007/03/20 15:26:06 jmc Exp $ */
3 1.1 bouyer
4 1.1 bouyer /*
5 1.7 xtraeme * Copyright (c) 2007, 2008 Juan Romero Pardines
6 1.1 bouyer * Copyright (c) 2004, 2005 Marco Peereboom
7 1.1 bouyer * All rights reserved.
8 1.1 bouyer *
9 1.1 bouyer * Redistribution and use in source and binary forms, with or without
10 1.1 bouyer * modification, are permitted provided that the following conditions
11 1.1 bouyer * are met:
12 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer.
14 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
16 1.1 bouyer * documentation and/or other materials provided with the distribution.
17 1.1 bouyer *
18 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 1.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
22 1.1 bouyer * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 bouyer * SUCH DAMAGE.
29 1.1 bouyer *
30 1.1 bouyer */
31 1.1 bouyer #include <sys/cdefs.h>
32 1.1 bouyer
33 1.1 bouyer #ifndef lint
34 1.9 xtraeme __RCSID("$NetBSD: bioctl.c,v 1.9 2008/03/01 16:08:41 xtraeme Exp $");
35 1.1 bouyer #endif
36 1.1 bouyer
37 1.7 xtraeme #include <sys/types.h>
38 1.1 bouyer #include <sys/ioctl.h>
39 1.1 bouyer #include <sys/param.h>
40 1.1 bouyer #include <sys/queue.h>
41 1.1 bouyer #include <dev/biovar.h>
42 1.1 bouyer
43 1.1 bouyer #include <errno.h>
44 1.1 bouyer #include <err.h>
45 1.1 bouyer #include <fcntl.h>
46 1.1 bouyer #include <util.h>
47 1.7 xtraeme #include <stdbool.h>
48 1.1 bouyer #include <stdio.h>
49 1.1 bouyer #include <stdlib.h>
50 1.1 bouyer #include <string.h>
51 1.1 bouyer #include <unistd.h>
52 1.1 bouyer #include <ctype.h>
53 1.1 bouyer #include <util.h>
54 1.1 bouyer #include "strtonum.h"
55 1.1 bouyer
56 1.7 xtraeme struct command {
57 1.7 xtraeme const char *cmd_name;
58 1.7 xtraeme const char *arg_names;
59 1.7 xtraeme void (*cmd_func)(int, int, char **);
60 1.7 xtraeme };
61 1.7 xtraeme
62 1.7 xtraeme struct biotmp {
63 1.7 xtraeme struct bioc_inq *bi;
64 1.7 xtraeme struct bioc_vol *bv;
65 1.7 xtraeme char volname[64];
66 1.7 xtraeme int fd;
67 1.7 xtraeme int volid;
68 1.7 xtraeme int diskid;
69 1.7 xtraeme bool format;
70 1.7 xtraeme bool show_disknovol;
71 1.7 xtraeme };
72 1.7 xtraeme
73 1.1 bouyer struct locator {
74 1.1 bouyer int channel;
75 1.1 bouyer int target;
76 1.1 bouyer int lun;
77 1.1 bouyer };
78 1.1 bouyer
79 1.7 xtraeme static void usage(void);
80 1.7 xtraeme static void bio_alarm(int, int, char **);
81 1.7 xtraeme static void bio_show_common(int, int, char **);
82 1.7 xtraeme static int bio_show_volumes(struct biotmp *);
83 1.7 xtraeme static void bio_show_disks(struct biotmp *);
84 1.7 xtraeme static void bio_setblink(int, int, char **);
85 1.7 xtraeme static void bio_blink(int, char *, int, int);
86 1.7 xtraeme static void bio_setstate_hotspare(int, int, char **);
87 1.7 xtraeme static void bio_setstate_passthru(int, int, char **);
88 1.7 xtraeme static void bio_setstate_common(int, char *, struct bioc_setstate *,
89 1.7 xtraeme struct locator *);
90 1.7 xtraeme static void bio_setstate_consistency(int, int, char **);
91 1.7 xtraeme static void bio_volops_create(int, int, char **);
92 1.7 xtraeme #ifdef notyet
93 1.7 xtraeme static void bio_volops_modify(int, int, char **);
94 1.7 xtraeme #endif
95 1.7 xtraeme static void bio_volops_remove(int, int, char **);
96 1.7 xtraeme
97 1.2 xtraeme static const char *str2locator(const char *, struct locator *);
98 1.1 bouyer
99 1.7 xtraeme static struct bio_locate bl;
100 1.7 xtraeme static struct command commands[] = {
101 1.7 xtraeme {
102 1.7 xtraeme "show",
103 1.7 xtraeme "[disks] | [volumes]",
104 1.7 xtraeme bio_show_common },
105 1.7 xtraeme {
106 1.7 xtraeme "alarm",
107 1.7 xtraeme "[enable] | [disable] | [silence] | [test]",
108 1.7 xtraeme bio_alarm },
109 1.7 xtraeme {
110 1.7 xtraeme "blink",
111 1.7 xtraeme "start | stop [channel:target[.lun]]",
112 1.7 xtraeme bio_setblink },
113 1.7 xtraeme {
114 1.7 xtraeme "hotspare",
115 1.7 xtraeme "add | remove channel:target.lun",
116 1.7 xtraeme bio_setstate_hotspare },
117 1.7 xtraeme {
118 1.7 xtraeme "passthru",
119 1.7 xtraeme "add DISKID | remove channel:target.lun",
120 1.7 xtraeme bio_setstate_passthru },
121 1.7 xtraeme {
122 1.7 xtraeme "check",
123 1.7 xtraeme "start | stop VOLID",
124 1.7 xtraeme bio_setstate_consistency },
125 1.7 xtraeme {
126 1.7 xtraeme "create",
127 1.7 xtraeme "volume VOLID DISKIDs [SIZE] STRIPE RAID_LEVEL channel:target.lun",
128 1.7 xtraeme bio_volops_create },
129 1.7 xtraeme #ifdef notyet
130 1.7 xtraeme {
131 1.7 xtraeme "modify",
132 1.7 xtraeme "volume VOLID STRIPE RAID_LEVEL channel:target.lun",
133 1.7 xtraeme bio_volops_modify },
134 1.7 xtraeme #endif
135 1.7 xtraeme {
136 1.7 xtraeme "remove",
137 1.7 xtraeme "volume VOLID channel:target.lun",
138 1.7 xtraeme bio_volops_remove },
139 1.2 xtraeme
140 1.7 xtraeme { NULL, NULL, NULL }
141 1.7 xtraeme };
142 1.1 bouyer
143 1.1 bouyer int
144 1.7 xtraeme main(int argc, char **argv)
145 1.1 bouyer {
146 1.7 xtraeme char *dvname;
147 1.7 xtraeme const char *cmdname;
148 1.7 xtraeme int fd = 0, i;
149 1.1 bouyer
150 1.7 xtraeme /* Must have at least: device command */
151 1.7 xtraeme if (argc < 3)
152 1.1 bouyer usage();
153 1.1 bouyer
154 1.7 xtraeme /* Skip program name, get and skip device name and command */
155 1.2 xtraeme setprogname(*argv);
156 1.7 xtraeme dvname = argv[1];
157 1.7 xtraeme cmdname = argv[2];
158 1.7 xtraeme argv += 3;
159 1.7 xtraeme argc -= 3;
160 1.7 xtraeme
161 1.7 xtraeme /* Look up and call the command */
162 1.7 xtraeme for (i = 0; commands[i].cmd_name != NULL; i++)
163 1.7 xtraeme if (strcmp(cmdname, commands[i].cmd_name) == 0)
164 1.7 xtraeme break;
165 1.7 xtraeme if (commands[i].cmd_name == NULL)
166 1.7 xtraeme errx(EXIT_FAILURE, "unknown command: %s", cmdname);
167 1.7 xtraeme
168 1.7 xtraeme /* Locate the device by issuing the BIOCLOCATE ioctl */
169 1.7 xtraeme fd = open("/dev/bio", O_RDWR);
170 1.7 xtraeme if (fd == -1)
171 1.7 xtraeme err(EXIT_FAILURE, "Can't open /dev/bio");
172 1.7 xtraeme
173 1.7 xtraeme bl.bl_name = dvname;
174 1.7 xtraeme if (ioctl(fd, BIOCLOCATE, &bl) == -1)
175 1.7 xtraeme errx(EXIT_FAILURE, "Can't locate %s device via /dev/bio",
176 1.7 xtraeme bl.bl_name);
177 1.2 xtraeme
178 1.7 xtraeme /* and execute the command */
179 1.7 xtraeme (*commands[i].cmd_func)(fd, argc, argv);
180 1.1 bouyer
181 1.7 xtraeme (void)close(fd);
182 1.2 xtraeme exit(EXIT_SUCCESS);
183 1.1 bouyer }
184 1.1 bouyer
185 1.2 xtraeme static void
186 1.1 bouyer usage(void)
187 1.1 bouyer {
188 1.7 xtraeme int i;
189 1.7 xtraeme
190 1.7 xtraeme (void)fprintf(stderr, "usage: %s device command [arg [...]]\n",
191 1.7 xtraeme getprogname());
192 1.7 xtraeme
193 1.7 xtraeme (void)fprintf(stderr, "Available commands:\n");
194 1.7 xtraeme for (i = 0; commands[i].cmd_name != NULL; i++)
195 1.7 xtraeme (void)fprintf(stderr, " %s %s\n", commands[i].cmd_name,
196 1.7 xtraeme commands[i].arg_names);
197 1.7 xtraeme
198 1.2 xtraeme exit(EXIT_FAILURE);
199 1.2 xtraeme /* NOTREACHED */
200 1.1 bouyer }
201 1.1 bouyer
202 1.2 xtraeme static const char *
203 1.1 bouyer str2locator(const char *string, struct locator *location)
204 1.1 bouyer {
205 1.7 xtraeme const char *errstr;
206 1.7 xtraeme char parse[80], *targ, *lun;
207 1.1 bouyer
208 1.1 bouyer strlcpy(parse, string, sizeof parse);
209 1.1 bouyer targ = strchr(parse, ':');
210 1.1 bouyer if (targ == NULL)
211 1.7 xtraeme return "target not specified";
212 1.7 xtraeme
213 1.1 bouyer *targ++ = '\0';
214 1.1 bouyer lun = strchr(targ, '.');
215 1.1 bouyer if (lun != NULL) {
216 1.1 bouyer *lun++ = '\0';
217 1.1 bouyer location->lun = strtonum(lun, 0, 256, &errstr);
218 1.1 bouyer if (errstr)
219 1.2 xtraeme return errstr;
220 1.1 bouyer } else
221 1.1 bouyer location->lun = 0;
222 1.1 bouyer
223 1.1 bouyer location->target = strtonum(targ, 0, 256, &errstr);
224 1.1 bouyer if (errstr)
225 1.2 xtraeme return errstr;
226 1.1 bouyer location->channel = strtonum(parse, 0, 256, &errstr);
227 1.1 bouyer if (errstr)
228 1.2 xtraeme return errstr;
229 1.2 xtraeme return NULL;
230 1.1 bouyer }
231 1.1 bouyer
232 1.7 xtraeme /*
233 1.7 xtraeme * Shows info about available RAID volumes.
234 1.7 xtraeme */
235 1.7 xtraeme static int
236 1.7 xtraeme bio_show_volumes(struct biotmp *bt)
237 1.7 xtraeme {
238 1.7 xtraeme struct bioc_vol bv;
239 1.7 xtraeme const char *status;
240 1.7 xtraeme char size[64], percent[16], seconds[20];
241 1.7 xtraeme char rtype[16], stripe[16], tmp[32];
242 1.7 xtraeme
243 1.7 xtraeme memset(&bv, 0, sizeof(bv));
244 1.7 xtraeme bv.bv_cookie = bl.bl_cookie;
245 1.7 xtraeme bv.bv_volid = bt->volid;
246 1.7 xtraeme bv.bv_percent = -1;
247 1.7 xtraeme bv.bv_seconds = -1;
248 1.7 xtraeme
249 1.7 xtraeme if (ioctl(bt->fd, BIOCVOL, &bv) == -1)
250 1.7 xtraeme err(EXIT_FAILURE, "BIOCVOL");
251 1.7 xtraeme
252 1.7 xtraeme percent[0] = '\0';
253 1.7 xtraeme seconds[0] = '\0';
254 1.7 xtraeme if (bv.bv_percent != -1)
255 1.7 xtraeme snprintf(percent, sizeof(percent),
256 1.7 xtraeme " %3.2f%% done", bv.bv_percent / 10.0);
257 1.7 xtraeme if (bv.bv_seconds)
258 1.7 xtraeme snprintf(seconds, sizeof(seconds),
259 1.7 xtraeme " %u seconds", bv.bv_seconds);
260 1.7 xtraeme
261 1.7 xtraeme switch (bv.bv_status) {
262 1.7 xtraeme case BIOC_SVONLINE:
263 1.7 xtraeme status = BIOC_SVONLINE_S;
264 1.7 xtraeme break;
265 1.7 xtraeme case BIOC_SVOFFLINE:
266 1.7 xtraeme status = BIOC_SVOFFLINE_S;
267 1.7 xtraeme break;
268 1.7 xtraeme case BIOC_SVDEGRADED:
269 1.7 xtraeme status = BIOC_SVDEGRADED_S;
270 1.7 xtraeme break;
271 1.7 xtraeme case BIOC_SVBUILDING:
272 1.7 xtraeme status = BIOC_SVBUILDING_S;
273 1.7 xtraeme break;
274 1.7 xtraeme case BIOC_SVREBUILD:
275 1.7 xtraeme status = BIOC_SVREBUILD_S;
276 1.7 xtraeme break;
277 1.7 xtraeme case BIOC_SVMIGRATING:
278 1.7 xtraeme status = BIOC_SVMIGRATING_S;
279 1.7 xtraeme break;
280 1.7 xtraeme case BIOC_SVSCRUB:
281 1.7 xtraeme status = BIOC_SVSCRUB_S;
282 1.7 xtraeme break;
283 1.7 xtraeme case BIOC_SVCHECKING:
284 1.7 xtraeme status = BIOC_SVCHECKING_S;
285 1.7 xtraeme break;
286 1.7 xtraeme case BIOC_SVINVALID:
287 1.7 xtraeme default:
288 1.7 xtraeme status = BIOC_SVINVALID_S;
289 1.7 xtraeme break;
290 1.7 xtraeme }
291 1.7 xtraeme
292 1.7 xtraeme snprintf(bt->volname, sizeof(bt->volname), "%u", bv.bv_volid);
293 1.7 xtraeme if (bv.bv_vendor)
294 1.7 xtraeme snprintf(tmp, sizeof(tmp), "%s %s", bv.bv_dev, bv.bv_vendor);
295 1.7 xtraeme else
296 1.7 xtraeme snprintf(tmp, sizeof(tmp), "%s", bv.bv_dev);
297 1.7 xtraeme
298 1.7 xtraeme switch (bv.bv_level) {
299 1.7 xtraeme case BIOC_SVOL_HOTSPARE:
300 1.7 xtraeme snprintf(rtype, sizeof(rtype), "Hot spare");
301 1.7 xtraeme snprintf(stripe, sizeof(stripe), "N/A");
302 1.7 xtraeme break;
303 1.7 xtraeme case BIOC_SVOL_PASSTHRU:
304 1.7 xtraeme snprintf(rtype, sizeof(rtype), "Pass through");
305 1.7 xtraeme snprintf(stripe, sizeof(stripe), "N/A");
306 1.7 xtraeme break;
307 1.7 xtraeme default:
308 1.7 xtraeme snprintf(rtype, sizeof(rtype), "RAID %u", bv.bv_level);
309 1.9 xtraeme if (bv.bv_level == 1 || bv.bv_stripe_size == 0)
310 1.9 xtraeme snprintf(stripe, sizeof(stripe), "N/A");
311 1.9 xtraeme else
312 1.9 xtraeme snprintf(stripe, sizeof(stripe), "%uK",
313 1.9 xtraeme bv.bv_stripe_size);
314 1.7 xtraeme break;
315 1.7 xtraeme }
316 1.7 xtraeme
317 1.7 xtraeme humanize_number(size, 5, (int64_t)bv.bv_size, "", HN_AUTOSCALE,
318 1.7 xtraeme HN_B | HN_NOSPACE | HN_DECIMAL);
319 1.7 xtraeme
320 1.9 xtraeme printf("%6s %-12s %4s %20s %8s %6s %s%s\n",
321 1.7 xtraeme bt->volname, status, size, tmp,
322 1.7 xtraeme rtype, stripe, percent, seconds);
323 1.7 xtraeme
324 1.7 xtraeme bt->bv = &bv;
325 1.7 xtraeme
326 1.7 xtraeme return bv.bv_nodisk;
327 1.7 xtraeme }
328 1.7 xtraeme
329 1.7 xtraeme /*
330 1.7 xtraeme * Shows info about physical disks.
331 1.7 xtraeme */
332 1.2 xtraeme static void
333 1.7 xtraeme bio_show_disks(struct biotmp *bt)
334 1.1 bouyer {
335 1.7 xtraeme struct bioc_disk bd;
336 1.7 xtraeme const char *status;
337 1.7 xtraeme char size[64], serial[32], scsiname[16];
338 1.7 xtraeme
339 1.7 xtraeme memset(&bd, 0, sizeof(bd));
340 1.7 xtraeme bd.bd_cookie = bl.bl_cookie;
341 1.7 xtraeme bd.bd_diskid = bt->diskid;
342 1.7 xtraeme bd.bd_volid = bt->volid;
343 1.7 xtraeme
344 1.7 xtraeme if (bt->show_disknovol) {
345 1.7 xtraeme if (ioctl(bt->fd, BIOCDISK_NOVOL, &bd) == -1)
346 1.7 xtraeme err(EXIT_FAILURE, "BIOCDISK_NOVOL");
347 1.7 xtraeme if (!bd.bd_disknovol)
348 1.7 xtraeme return;
349 1.7 xtraeme } else {
350 1.7 xtraeme if (ioctl(bt->fd, BIOCDISK, &bd) == -1)
351 1.7 xtraeme err(EXIT_FAILURE, "BIOCDISK");
352 1.7 xtraeme }
353 1.7 xtraeme
354 1.7 xtraeme switch (bd.bd_status) {
355 1.7 xtraeme case BIOC_SDONLINE:
356 1.7 xtraeme status = BIOC_SDONLINE_S;
357 1.7 xtraeme break;
358 1.7 xtraeme case BIOC_SDOFFLINE:
359 1.7 xtraeme status = BIOC_SDOFFLINE_S;
360 1.7 xtraeme break;
361 1.7 xtraeme case BIOC_SDFAILED:
362 1.7 xtraeme status = BIOC_SDFAILED_S;
363 1.7 xtraeme break;
364 1.7 xtraeme case BIOC_SDREBUILD:
365 1.7 xtraeme status = BIOC_SDREBUILD_S;
366 1.7 xtraeme break;
367 1.7 xtraeme case BIOC_SDHOTSPARE:
368 1.7 xtraeme status = BIOC_SDHOTSPARE_S;
369 1.7 xtraeme break;
370 1.7 xtraeme case BIOC_SDUNUSED:
371 1.7 xtraeme status = BIOC_SDUNUSED_S;
372 1.7 xtraeme break;
373 1.7 xtraeme case BIOC_SDSCRUB:
374 1.7 xtraeme status = BIOC_SDSCRUB_S;
375 1.7 xtraeme break;
376 1.7 xtraeme case BIOC_SDPASSTHRU:
377 1.7 xtraeme status = BIOC_SDPASSTHRU_S;
378 1.7 xtraeme break;
379 1.7 xtraeme case BIOC_SDINVALID:
380 1.7 xtraeme default:
381 1.7 xtraeme status = BIOC_SDINVALID_S;
382 1.7 xtraeme break;
383 1.7 xtraeme }
384 1.7 xtraeme
385 1.7 xtraeme if (bt->format)
386 1.7 xtraeme snprintf(bt->volname, sizeof(bt->volname),
387 1.7 xtraeme "%u:%u", bt->bv->bv_volid, bd.bd_diskid);
388 1.7 xtraeme
389 1.7 xtraeme humanize_number(size, 5, bd.bd_size, "", HN_AUTOSCALE,
390 1.7 xtraeme HN_B | HN_NOSPACE | HN_DECIMAL);
391 1.7 xtraeme
392 1.7 xtraeme if (bd.bd_procdev[0])
393 1.7 xtraeme snprintf(scsiname, sizeof(scsiname), "%u:%u.%u %s",
394 1.7 xtraeme bd.bd_channel, bd.bd_target, bd.bd_lun,
395 1.7 xtraeme bd.bd_procdev);
396 1.7 xtraeme else
397 1.7 xtraeme snprintf(scsiname, sizeof(scsiname), "%u:%u.%u noencl",
398 1.7 xtraeme bd.bd_channel, bd.bd_target, bd.bd_lun);
399 1.7 xtraeme
400 1.7 xtraeme if (bd.bd_serial[0])
401 1.7 xtraeme strlcpy(serial, bd.bd_serial, sizeof(serial));
402 1.7 xtraeme else
403 1.7 xtraeme strlcpy(serial, "unknown serial", sizeof(serial));
404 1.7 xtraeme
405 1.7 xtraeme if (bt->format)
406 1.7 xtraeme printf("%6s %-12s %4s %20s <%s>\n",
407 1.7 xtraeme bt->volname, status, size, scsiname,
408 1.7 xtraeme bd.bd_vendor);
409 1.7 xtraeme else
410 1.7 xtraeme printf("%5d [%-28s] %-12s %-6s %12s\n",
411 1.7 xtraeme bt->diskid, bd.bd_vendor, status, size, scsiname);
412 1.1 bouyer
413 1.7 xtraeme }
414 1.7 xtraeme
415 1.7 xtraeme /*
416 1.7 xtraeme * Shows info about volumes/disks.
417 1.7 xtraeme */
418 1.7 xtraeme static void
419 1.7 xtraeme bio_show_common(int fd, int argc, char **argv)
420 1.7 xtraeme {
421 1.7 xtraeme struct biotmp *biot;
422 1.7 xtraeme struct bioc_inq bi;
423 1.7 xtraeme int i, d, ndisks;
424 1.7 xtraeme bool show_all, show_disks;
425 1.7 xtraeme bool show_vols, show_caps;
426 1.1 bouyer
427 1.7 xtraeme show_all = show_disks = show_vols = show_caps = false;
428 1.1 bouyer
429 1.7 xtraeme if (argc > 1)
430 1.7 xtraeme usage();
431 1.1 bouyer
432 1.7 xtraeme if (argv[0]) {
433 1.7 xtraeme if (strcmp(argv[0], "disks") == 0)
434 1.7 xtraeme show_disks = true;
435 1.7 xtraeme else if (strcmp(argv[0], "volumes") == 0)
436 1.7 xtraeme show_vols = true;
437 1.7 xtraeme else
438 1.7 xtraeme usage();
439 1.7 xtraeme } else
440 1.7 xtraeme show_all = true;
441 1.1 bouyer
442 1.7 xtraeme memset(&bi, 0, sizeof(bi));
443 1.7 xtraeme bi.bi_cookie = bl.bl_cookie;
444 1.1 bouyer
445 1.7 xtraeme if (ioctl(fd, BIOCINQ, &bi) == -1)
446 1.7 xtraeme err(EXIT_FAILURE, "BIOCINQ");
447 1.1 bouyer
448 1.7 xtraeme /*
449 1.7 xtraeme * If there are volumes there's no point to continue.
450 1.7 xtraeme */
451 1.7 xtraeme if (show_all || show_vols) {
452 1.7 xtraeme if (!bi.bi_novol) {
453 1.7 xtraeme warnx("no volumes available");
454 1.7 xtraeme return;
455 1.1 bouyer }
456 1.7 xtraeme }
457 1.1 bouyer
458 1.7 xtraeme biot = calloc(1, sizeof(*biot));
459 1.7 xtraeme if (!biot)
460 1.7 xtraeme err(EXIT_FAILURE, "biotemp calloc");
461 1.7 xtraeme
462 1.7 xtraeme biot->fd = fd;
463 1.7 xtraeme biot->bi = &bi;
464 1.7 xtraeme /*
465 1.7 xtraeme * Go to the disks section if that was specified.
466 1.7 xtraeme */
467 1.7 xtraeme if (show_disks)
468 1.7 xtraeme goto disks;
469 1.7 xtraeme
470 1.7 xtraeme /*
471 1.7 xtraeme * Common code to show only info about volumes and disks
472 1.7 xtraeme * associated to them.
473 1.7 xtraeme */
474 1.9 xtraeme printf("%6s %-12s %4s %20s %8s %6s\n",
475 1.7 xtraeme "Volume", "Status", "Size", "Device/Label",
476 1.9 xtraeme "Level", "Stripe");
477 1.7 xtraeme printf("=============================================="
478 1.9 xtraeme "===============\n");
479 1.1 bouyer
480 1.7 xtraeme for (i = 0; i < bi.bi_novol; i++) {
481 1.7 xtraeme biot->format = true;
482 1.7 xtraeme biot->volid = i;
483 1.7 xtraeme ndisks = bio_show_volumes(biot);
484 1.7 xtraeme if (show_vols)
485 1.7 xtraeme continue;
486 1.7 xtraeme
487 1.7 xtraeme for (d = 0; d < ndisks; d++) {
488 1.7 xtraeme biot->diskid = d;
489 1.7 xtraeme bio_show_disks(biot);
490 1.1 bouyer }
491 1.1 bouyer
492 1.7 xtraeme }
493 1.7 xtraeme goto out;
494 1.1 bouyer
495 1.7 xtraeme disks:
496 1.7 xtraeme /*
497 1.7 xtraeme * show info about all disks connected to the raid controller,
498 1.7 xtraeme * even if they aren't associated with a volume or raid set.
499 1.7 xtraeme */
500 1.7 xtraeme if (show_disks) {
501 1.7 xtraeme printf("%5s %-30s %-12s %-6s %12s\n",
502 1.7 xtraeme "Disk", "Model/Serial", "Status", "Size", "Location");
503 1.7 xtraeme printf("==============================================="
504 1.7 xtraeme "======================\n");
505 1.7 xtraeme for (d = 0; d < bi.bi_nodisk; d++) {
506 1.7 xtraeme biot->show_disknovol = true;
507 1.7 xtraeme biot->diskid = d;
508 1.7 xtraeme bio_show_disks(biot);
509 1.1 bouyer }
510 1.1 bouyer }
511 1.7 xtraeme out:
512 1.7 xtraeme free(biot);
513 1.1 bouyer }
514 1.1 bouyer
515 1.7 xtraeme /*
516 1.7 xtraeme * To handle the alarm feature.
517 1.7 xtraeme */
518 1.2 xtraeme static void
519 1.7 xtraeme bio_alarm(int fd, int argc, char **argv)
520 1.1 bouyer {
521 1.7 xtraeme struct bioc_alarm ba;
522 1.7 xtraeme bool show = false;
523 1.1 bouyer
524 1.7 xtraeme memset(&ba, 0, sizeof(ba));
525 1.1 bouyer ba.ba_cookie = bl.bl_cookie;
526 1.1 bouyer
527 1.7 xtraeme if (argc > 1)
528 1.7 xtraeme usage();
529 1.7 xtraeme
530 1.7 xtraeme if (argc == 0) {
531 1.7 xtraeme /* show alarm status */
532 1.7 xtraeme ba.ba_opcode = BIOC_GASTATUS;
533 1.7 xtraeme show = true;
534 1.7 xtraeme } else if (strcmp(argv[0], "silence") == 0) {
535 1.7 xtraeme /* silence alarm */
536 1.1 bouyer ba.ba_opcode = BIOC_SASILENCE;
537 1.7 xtraeme } else if (strcmp(argv[0], "enable") == 0) {
538 1.7 xtraeme /* enable alarm */
539 1.7 xtraeme ba.ba_opcode = BIOC_SAENABLE;
540 1.7 xtraeme } else if (strcmp(argv[0], "disable") == 0) {
541 1.7 xtraeme /* disable alarm */
542 1.7 xtraeme ba.ba_opcode = BIOC_SADISABLE;
543 1.7 xtraeme } else if (strcmp(argv[0], "test") == 0) {
544 1.7 xtraeme /* test alarm */
545 1.7 xtraeme ba.ba_opcode = BIOC_SATEST;
546 1.7 xtraeme } else
547 1.7 xtraeme usage();
548 1.7 xtraeme
549 1.7 xtraeme if (ioctl(fd, BIOCALARM, &ba) == -1)
550 1.7 xtraeme err(EXIT_FAILURE, "BIOCALARM");
551 1.7 xtraeme
552 1.7 xtraeme if (show)
553 1.7 xtraeme printf("alarm is currently %s\n",
554 1.7 xtraeme ba.ba_status ? "enabled" : "disabled");
555 1.7 xtraeme }
556 1.7 xtraeme
557 1.7 xtraeme /*
558 1.7 xtraeme * To add/remove a hotspare disk.
559 1.7 xtraeme */
560 1.7 xtraeme static void
561 1.7 xtraeme bio_setstate_hotspare(int fd, int argc, char **argv)
562 1.7 xtraeme {
563 1.7 xtraeme struct bioc_setstate bs;
564 1.7 xtraeme struct locator location;
565 1.7 xtraeme
566 1.7 xtraeme memset(&bs, 0, sizeof(bs));
567 1.7 xtraeme
568 1.7 xtraeme if (argc != 2)
569 1.7 xtraeme usage();
570 1.7 xtraeme
571 1.7 xtraeme if (strcmp(argv[0], "add") == 0)
572 1.7 xtraeme bs.bs_status = BIOC_SSHOTSPARE;
573 1.7 xtraeme else if (strcmp(argv[0], "remove") == 0)
574 1.7 xtraeme bs.bs_status = BIOC_SSDELHOTSPARE;
575 1.7 xtraeme else
576 1.7 xtraeme usage();
577 1.7 xtraeme
578 1.7 xtraeme bio_setstate_common(fd, argv[1], &bs, &location);
579 1.7 xtraeme }
580 1.7 xtraeme
581 1.7 xtraeme /*
582 1.7 xtraeme * To add/remove a pass through disk.
583 1.7 xtraeme */
584 1.7 xtraeme static void
585 1.7 xtraeme bio_setstate_passthru(int fd, int argc, char **argv)
586 1.7 xtraeme {
587 1.7 xtraeme struct bioc_setstate bs;
588 1.7 xtraeme struct locator location;
589 1.7 xtraeme char *endptr;
590 1.7 xtraeme bool rem = false;
591 1.7 xtraeme
592 1.7 xtraeme if (argc > 3)
593 1.7 xtraeme usage();
594 1.7 xtraeme
595 1.7 xtraeme memset(&bs, 0, sizeof(bs));
596 1.7 xtraeme
597 1.7 xtraeme if (strcmp(argv[0], "add") == 0) {
598 1.7 xtraeme if (argv[1] == NULL || argv[2] == NULL)
599 1.7 xtraeme usage();
600 1.7 xtraeme
601 1.7 xtraeme bs.bs_status = BIOC_SSPASSTHRU;
602 1.7 xtraeme } else if (strcmp(argv[0], "remove") == 0) {
603 1.7 xtraeme if (argv[1] == NULL)
604 1.7 xtraeme usage();
605 1.7 xtraeme
606 1.7 xtraeme bs.bs_status = BIOC_SSDELPASSTHRU;
607 1.7 xtraeme rem = true;
608 1.7 xtraeme } else
609 1.7 xtraeme usage();
610 1.7 xtraeme
611 1.7 xtraeme if (rem)
612 1.7 xtraeme bio_setstate_common(fd, argv[1], &bs, &location);
613 1.7 xtraeme else {
614 1.7 xtraeme bs.bs_other_id = (unsigned int)strtoul(argv[1], &endptr, 10);
615 1.7 xtraeme if (*endptr != '\0')
616 1.7 xtraeme errx(EXIT_FAILURE, "Invalid Volume ID value");
617 1.7 xtraeme
618 1.7 xtraeme bio_setstate_common(fd, argv[2], &bs, &location);
619 1.7 xtraeme }
620 1.7 xtraeme }
621 1.7 xtraeme
622 1.7 xtraeme /*
623 1.7 xtraeme * To start/stop a consistency check in a RAID volume.
624 1.7 xtraeme */
625 1.7 xtraeme static void
626 1.7 xtraeme bio_setstate_consistency(int fd, int argc, char **argv)
627 1.7 xtraeme {
628 1.7 xtraeme struct bioc_setstate bs;
629 1.7 xtraeme char *endptr;
630 1.7 xtraeme
631 1.7 xtraeme if (argc > 2)
632 1.7 xtraeme usage();
633 1.7 xtraeme
634 1.8 xtraeme memset(&bs, 0, sizeof(bs));
635 1.8 xtraeme
636 1.7 xtraeme if (strcmp(argv[0], "start") == 0)
637 1.7 xtraeme bs.bs_status = BIOC_SSCHECKSTART_VOL;
638 1.7 xtraeme else if (strcmp(argv[0], "stop") == 0)
639 1.7 xtraeme bs.bs_status = BIOC_SSCHECKSTOP_VOL;
640 1.7 xtraeme else
641 1.7 xtraeme usage();
642 1.7 xtraeme
643 1.8 xtraeme bs.bs_volid = (unsigned int)strtoul(argv[1], &endptr, 10);
644 1.7 xtraeme if (*endptr != '\0')
645 1.7 xtraeme errx(EXIT_FAILURE, "Invalid Volume ID value");
646 1.7 xtraeme
647 1.7 xtraeme bio_setstate_common(fd, NULL, &bs, NULL);
648 1.7 xtraeme }
649 1.7 xtraeme
650 1.7 xtraeme static void
651 1.7 xtraeme bio_setstate_common(int fd, char *arg, struct bioc_setstate *bs,
652 1.7 xtraeme struct locator *location)
653 1.7 xtraeme {
654 1.7 xtraeme const char *errstr;
655 1.7 xtraeme
656 1.7 xtraeme if (!arg || !location)
657 1.7 xtraeme goto send;
658 1.7 xtraeme
659 1.7 xtraeme errstr = str2locator(arg, location);
660 1.7 xtraeme if (errstr)
661 1.7 xtraeme errx(EXIT_FAILURE, "Target %s: %s", arg, errstr);
662 1.7 xtraeme
663 1.7 xtraeme bs->bs_channel = location->channel;
664 1.7 xtraeme bs->bs_target = location->target;
665 1.7 xtraeme bs->bs_lun = location->lun;
666 1.7 xtraeme
667 1.7 xtraeme send:
668 1.7 xtraeme bs->bs_cookie = bl.bl_cookie;
669 1.7 xtraeme
670 1.7 xtraeme if (ioctl(fd, BIOCSETSTATE, bs) == -1)
671 1.7 xtraeme err(EXIT_FAILURE, "BIOCSETSTATE");
672 1.7 xtraeme }
673 1.7 xtraeme
674 1.7 xtraeme /*
675 1.7 xtraeme * To create a RAID volume.
676 1.7 xtraeme */
677 1.7 xtraeme static void
678 1.7 xtraeme bio_volops_create(int fd, int argc, char **argv)
679 1.7 xtraeme {
680 1.7 xtraeme struct bioc_volops bc;
681 1.7 xtraeme struct bioc_inq bi;
682 1.7 xtraeme struct bioc_disk bd;
683 1.7 xtraeme struct locator location;
684 1.7 xtraeme uint64_t total_disksize = 0, first_disksize = 0;
685 1.7 xtraeme int64_t volsize = 0;
686 1.7 xtraeme const char *errstr;
687 1.7 xtraeme char *endptr, *stripe;
688 1.7 xtraeme char *scsiname, *raid_level, size[64];
689 1.7 xtraeme int disk_first = 0, disk_end = 0;
690 1.7 xtraeme int i, nfreedisks = 0;
691 1.7 xtraeme int user_disks = 0;
692 1.7 xtraeme
693 1.7 xtraeme if (argc < 6 || argc > 7)
694 1.7 xtraeme usage();
695 1.7 xtraeme
696 1.7 xtraeme if (strcmp(argv[0], "volume") != 0)
697 1.7 xtraeme usage();
698 1.7 xtraeme
699 1.7 xtraeme /*
700 1.7 xtraeme * No size requested, use max size depending on RAID level.
701 1.7 xtraeme */
702 1.7 xtraeme if (argc == 6) {
703 1.7 xtraeme stripe = argv[3];
704 1.7 xtraeme raid_level = argv[4];
705 1.7 xtraeme scsiname = argv[5];
706 1.7 xtraeme } else {
707 1.7 xtraeme stripe = argv[4];
708 1.7 xtraeme raid_level = argv[5];
709 1.7 xtraeme scsiname = argv[6];
710 1.7 xtraeme }
711 1.7 xtraeme
712 1.7 xtraeme memset(&bd, 0, sizeof(bd));
713 1.7 xtraeme memset(&bc, 0, sizeof(bc));
714 1.7 xtraeme memset(&bi, 0, sizeof(bi));
715 1.7 xtraeme
716 1.7 xtraeme bc.bc_cookie = bd.bd_cookie = bi.bi_cookie = bl.bl_cookie;
717 1.7 xtraeme bc.bc_opcode = BIOC_VCREATE_VOLUME;
718 1.7 xtraeme
719 1.7 xtraeme bc.bc_volid = (unsigned int)strtoul(argv[1], &endptr, 10);
720 1.7 xtraeme if (*endptr != '\0')
721 1.7 xtraeme errx(EXIT_FAILURE, "Invalid Volume ID value");
722 1.7 xtraeme
723 1.7 xtraeme if (argc == 7)
724 1.7 xtraeme if (dehumanize_number(argv[3], &volsize) == -1)
725 1.7 xtraeme errx(EXIT_FAILURE, "Invalid SIZE value");
726 1.7 xtraeme
727 1.7 xtraeme bc.bc_stripe = (unsigned int)strtoul(stripe, &endptr, 10);
728 1.7 xtraeme if (*endptr != '\0')
729 1.7 xtraeme errx(EXIT_FAILURE, "Invalid STRIPE size value");
730 1.7 xtraeme
731 1.7 xtraeme bc.bc_level = (unsigned int)strtoul(raid_level, &endptr, 10);
732 1.7 xtraeme if (*endptr != '\0')
733 1.7 xtraeme errx(EXIT_FAILURE, "Invalid RAID_LEVEL value");
734 1.7 xtraeme
735 1.7 xtraeme errstr = str2locator(scsiname, &location);
736 1.7 xtraeme if (errstr)
737 1.7 xtraeme errx(EXIT_FAILURE, "Target %s: %s", scsiname, errstr);
738 1.7 xtraeme
739 1.7 xtraeme /*
740 1.7 xtraeme * Parse the device list that will be used for the volume,
741 1.7 xtraeme * by using a bit field for the disks.
742 1.7 xtraeme */
743 1.7 xtraeme if ((isdigit((unsigned char)argv[2][0]) == 0) || argv[2][1] != '-' ||
744 1.7 xtraeme (isdigit((unsigned char)argv[2][2]) == 0))
745 1.7 xtraeme errx(EXIT_FAILURE, "Invalid DISKIDs value");
746 1.7 xtraeme
747 1.7 xtraeme disk_first = atoi(&argv[2][0]);
748 1.7 xtraeme disk_end = atoi(&argv[2][2]);
749 1.7 xtraeme
750 1.7 xtraeme for (i = disk_first; i < disk_end + 1; i++) {
751 1.7 xtraeme bc.bc_devmask |= (1 << i);
752 1.7 xtraeme user_disks++;
753 1.7 xtraeme }
754 1.7 xtraeme
755 1.7 xtraeme /*
756 1.7 xtraeme * Find out how many disks are free and how much size we
757 1.7 xtraeme * have available for the new volume.
758 1.7 xtraeme */
759 1.7 xtraeme if (ioctl(fd, BIOCINQ, &bi) == -1)
760 1.7 xtraeme err(EXIT_FAILURE, "BIOCINQ");
761 1.7 xtraeme
762 1.7 xtraeme for (i = 0; i < bi.bi_nodisk; i++) {
763 1.7 xtraeme bd.bd_diskid = i;
764 1.7 xtraeme if (ioctl(fd, BIOCDISK_NOVOL, &bd) == -1)
765 1.7 xtraeme err(EXIT_FAILURE, "BIOCDISK_NOVOL");
766 1.7 xtraeme
767 1.7 xtraeme if (bd.bd_status == BIOC_SDUNUSED) {
768 1.7 xtraeme if (i == 0)
769 1.7 xtraeme first_disksize = bd.bd_size;
770 1.7 xtraeme
771 1.7 xtraeme total_disksize += bd.bd_size;
772 1.7 xtraeme nfreedisks++;
773 1.7 xtraeme }
774 1.7 xtraeme }
775 1.7 xtraeme
776 1.7 xtraeme /*
777 1.7 xtraeme * Basic checks to be sure we don't do something stupid.
778 1.7 xtraeme */
779 1.7 xtraeme if (nfreedisks == 0)
780 1.7 xtraeme errx(EXIT_FAILURE, "No free disks available");
781 1.7 xtraeme
782 1.7 xtraeme switch (bc.bc_level) {
783 1.7 xtraeme case 0: /* RAID 0 requires at least one disk */
784 1.7 xtraeme if (argc == 7) {
785 1.7 xtraeme if (volsize > total_disksize)
786 1.7 xtraeme errx(EXIT_FAILURE, "volume size specified "
787 1.7 xtraeme "is larger than available on free disks");
788 1.7 xtraeme bc.bc_size = (uint64_t)volsize;
789 1.7 xtraeme } else
790 1.7 xtraeme bc.bc_size = total_disksize;
791 1.1 bouyer
792 1.1 bouyer break;
793 1.7 xtraeme case 1: /* RAID 1 requires two disks and size is total - 1 disk */
794 1.7 xtraeme if (nfreedisks < 2 || user_disks < 2)
795 1.7 xtraeme errx(EXIT_FAILURE, "2 disks are required at least for "
796 1.7 xtraeme "this RAID level");
797 1.7 xtraeme
798 1.7 xtraeme if (argc == 7) {
799 1.7 xtraeme if (volsize > (total_disksize - first_disksize))
800 1.7 xtraeme errx(EXIT_FAILURE, "volume size specified "
801 1.7 xtraeme "is larger than available on free disks");
802 1.7 xtraeme bc.bc_size = (uint64_t)volsize;
803 1.7 xtraeme } else
804 1.7 xtraeme bc.bc_size = (total_disksize - first_disksize);
805 1.1 bouyer
806 1.1 bouyer break;
807 1.7 xtraeme case 3: /* RAID 0+1/3/5 requires three disks and size is total - 1 disk */
808 1.7 xtraeme case 5:
809 1.7 xtraeme if (nfreedisks < 3 || user_disks < 3)
810 1.7 xtraeme errx(EXIT_FAILURE, "3 disks are required at least for "
811 1.7 xtraeme "this RAID level");
812 1.7 xtraeme
813 1.7 xtraeme if (argc == 7) {
814 1.7 xtraeme if (volsize > (total_disksize - first_disksize))
815 1.7 xtraeme errx(EXIT_FAILURE, "volume size specified "
816 1.7 xtraeme "is larger than available on free disks");
817 1.7 xtraeme bc.bc_size = (uint64_t)volsize;
818 1.7 xtraeme } else
819 1.7 xtraeme bc.bc_size = (total_disksize - first_disksize);
820 1.1 bouyer
821 1.1 bouyer break;
822 1.7 xtraeme case 6: /* RAID 6 requires four disks and size is total - 2 disks */
823 1.7 xtraeme if (nfreedisks < 4 || user_disks < 4)
824 1.7 xtraeme errx(EXIT_FAILURE, "4 disks are required at least for "
825 1.7 xtraeme "this RAID level");
826 1.7 xtraeme
827 1.7 xtraeme if (argc == 7) {
828 1.7 xtraeme if (volsize > (total_disksize - (first_disksize * 2)))
829 1.7 xtraeme err(EXIT_FAILURE, "volume size specified "
830 1.7 xtraeme "is larger than available on free disks");
831 1.7 xtraeme bc.bc_size = (uint64_t)volsize;
832 1.7 xtraeme } else
833 1.7 xtraeme bc.bc_size = (total_disksize - (first_disksize * 2));
834 1.1 bouyer
835 1.1 bouyer break;
836 1.1 bouyer default:
837 1.7 xtraeme errx(EXIT_FAILURE, "Unsupported RAID level");
838 1.1 bouyer }
839 1.1 bouyer
840 1.7 xtraeme bc.bc_channel = location.channel;
841 1.7 xtraeme bc.bc_target = location.target;
842 1.7 xtraeme bc.bc_lun = location.lun;
843 1.7 xtraeme
844 1.7 xtraeme if (ioctl(fd, BIOCVOLOPS, &bc) == -1)
845 1.7 xtraeme err(EXIT_FAILURE, "BIOCVOLOPS");
846 1.7 xtraeme
847 1.7 xtraeme humanize_number(size, 5, bc.bc_size, "", HN_AUTOSCALE,
848 1.7 xtraeme HN_B | HN_NOSPACE | HN_DECIMAL);
849 1.7 xtraeme
850 1.7 xtraeme printf("Created volume %u size: %s stripe: %uK level: %u "
851 1.7 xtraeme "SCSI location: %u:%u.%u\n", bc.bc_volid, size, bc.bc_stripe,
852 1.7 xtraeme bc.bc_level, bc.bc_channel, bc.bc_target, bc.bc_lun);
853 1.7 xtraeme }
854 1.1 bouyer
855 1.7 xtraeme #ifdef notyet
856 1.7 xtraeme /*
857 1.7 xtraeme * To modify a RAID volume.
858 1.7 xtraeme */
859 1.7 xtraeme static void
860 1.7 xtraeme bio_volops_modify(int fd, int argc, char **argv)
861 1.7 xtraeme {
862 1.7 xtraeme /* XTRAEME: TODO */
863 1.1 bouyer }
864 1.7 xtraeme #endif
865 1.1 bouyer
866 1.7 xtraeme /*
867 1.7 xtraeme * To remove a RAID volume.
868 1.7 xtraeme */
869 1.2 xtraeme static void
870 1.7 xtraeme bio_volops_remove(int fd, int argc, char **argv)
871 1.1 bouyer {
872 1.7 xtraeme struct bioc_volops bc;
873 1.1 bouyer struct locator location;
874 1.1 bouyer const char *errstr;
875 1.7 xtraeme char *endptr;
876 1.1 bouyer
877 1.7 xtraeme if (argc != 3 || strcmp(argv[0], "volume") != 0)
878 1.7 xtraeme usage();
879 1.7 xtraeme
880 1.7 xtraeme memset(&bc, 0, sizeof(bc));
881 1.7 xtraeme bc.bc_cookie = bl.bl_cookie;
882 1.7 xtraeme bc.bc_opcode = BIOC_VREMOVE_VOLUME;
883 1.7 xtraeme
884 1.7 xtraeme bc.bc_volid = (unsigned int)strtoul(argv[1], &endptr, 10);
885 1.7 xtraeme if (*endptr != '\0')
886 1.7 xtraeme errx(EXIT_FAILURE, "Invalid Volume ID value");
887 1.7 xtraeme
888 1.7 xtraeme errstr = str2locator(argv[2], &location);
889 1.1 bouyer if (errstr)
890 1.7 xtraeme errx(EXIT_FAILURE, "Target %s: %s", argv[2], errstr);
891 1.7 xtraeme
892 1.7 xtraeme bc.bc_channel = location.channel;
893 1.7 xtraeme bc.bc_target = location.target;
894 1.7 xtraeme bc.bc_lun = location.lun;
895 1.1 bouyer
896 1.7 xtraeme if (ioctl(fd, BIOCVOLOPS, &bc) == -1)
897 1.7 xtraeme err(EXIT_FAILURE, "BIOCVOLOPS");
898 1.1 bouyer
899 1.7 xtraeme printf("Removed volume %u at SCSI location %u:%u.%u\n",
900 1.7 xtraeme bc.bc_volid, bc.bc_channel, bc.bc_target, bc.bc_lun);
901 1.1 bouyer }
902 1.1 bouyer
903 1.7 xtraeme /*
904 1.7 xtraeme * To blink/unblink a disk in enclosures.
905 1.7 xtraeme */
906 1.2 xtraeme static void
907 1.7 xtraeme bio_setblink(int fd, int argc, char **argv)
908 1.1 bouyer {
909 1.1 bouyer struct locator location;
910 1.1 bouyer struct bioc_inq bi;
911 1.1 bouyer struct bioc_vol bv;
912 1.1 bouyer struct bioc_disk bd;
913 1.1 bouyer struct bioc_blink bb;
914 1.1 bouyer const char *errstr;
915 1.7 xtraeme int v, d, rv, blink = 0;
916 1.7 xtraeme
917 1.7 xtraeme if (argc != 2)
918 1.7 xtraeme usage();
919 1.1 bouyer
920 1.7 xtraeme if (strcmp(argv[0], "start") == 0)
921 1.7 xtraeme blink = BIOC_SBBLINK;
922 1.7 xtraeme else if (strcmp(argv[0], "stop") == 0)
923 1.7 xtraeme blink = BIOC_SBUNBLINK;
924 1.7 xtraeme else
925 1.7 xtraeme usage();
926 1.7 xtraeme
927 1.7 xtraeme errstr = str2locator(argv[1], &location);
928 1.1 bouyer if (errstr)
929 1.7 xtraeme errx(EXIT_FAILURE, "Target %s: %s", argv[1], errstr);
930 1.1 bouyer
931 1.1 bouyer /* try setting blink on the device directly */
932 1.1 bouyer memset(&bb, 0, sizeof(bb));
933 1.1 bouyer bb.bb_cookie = bl.bl_cookie;
934 1.1 bouyer bb.bb_status = blink;
935 1.1 bouyer bb.bb_target = location.target;
936 1.1 bouyer bb.bb_channel = location.channel;
937 1.2 xtraeme rv = ioctl(fd, BIOCBLINK, &bb);
938 1.1 bouyer if (rv == 0)
939 1.1 bouyer return;
940 1.1 bouyer
941 1.1 bouyer /* if the blink didnt work, try to find something that will */
942 1.1 bouyer memset(&bi, 0, sizeof(bi));
943 1.1 bouyer bi.bi_cookie = bl.bl_cookie;
944 1.2 xtraeme rv = ioctl(fd, BIOCINQ, &bi);
945 1.7 xtraeme if (rv == -1)
946 1.7 xtraeme err(EXIT_FAILURE, "BIOCINQ");
947 1.1 bouyer
948 1.1 bouyer for (v = 0; v < bi.bi_novol; v++) {
949 1.1 bouyer memset(&bv, 0, sizeof(bv));
950 1.1 bouyer bv.bv_cookie = bl.bl_cookie;
951 1.1 bouyer bv.bv_volid = v;
952 1.2 xtraeme rv = ioctl(fd, BIOCVOL, &bv);
953 1.4 xtraeme if (rv == -1)
954 1.7 xtraeme err(EXIT_FAILURE, "BIOCVOL");
955 1.1 bouyer
956 1.1 bouyer for (d = 0; d < bv.bv_nodisk; d++) {
957 1.1 bouyer memset(&bd, 0, sizeof(bd));
958 1.1 bouyer bd.bd_cookie = bl.bl_cookie;
959 1.1 bouyer bd.bd_volid = v;
960 1.1 bouyer bd.bd_diskid = d;
961 1.1 bouyer
962 1.2 xtraeme rv = ioctl(fd, BIOCDISK, &bd);
963 1.4 xtraeme if (rv == -1)
964 1.7 xtraeme err(EXIT_FAILURE, "BIOCDISK");
965 1.1 bouyer
966 1.1 bouyer if (bd.bd_channel == location.channel &&
967 1.1 bouyer bd.bd_target == location.target &&
968 1.1 bouyer bd.bd_lun == location.lun) {
969 1.1 bouyer if (bd.bd_procdev[0] != '\0') {
970 1.2 xtraeme bio_blink(fd, bd.bd_procdev,
971 1.1 bouyer location.target, blink);
972 1.1 bouyer } else
973 1.7 xtraeme warnx("Disk %s is not in an enclosure",
974 1.7 xtraeme argv[1]);
975 1.1 bouyer return;
976 1.1 bouyer }
977 1.1 bouyer }
978 1.1 bouyer }
979 1.1 bouyer
980 1.7 xtraeme warnx("Disk %s does not exist", argv[1]);
981 1.1 bouyer }
982 1.1 bouyer
983 1.2 xtraeme static void
984 1.2 xtraeme bio_blink(int fd, char *enclosure, int target, int blinktype)
985 1.1 bouyer {
986 1.1 bouyer struct bio_locate bio;
987 1.1 bouyer struct bioc_blink blink;
988 1.1 bouyer
989 1.1 bouyer bio.bl_name = enclosure;
990 1.7 xtraeme if (ioctl(fd, BIOCLOCATE, &bio) == -1)
991 1.4 xtraeme errx(EXIT_FAILURE,
992 1.7 xtraeme "Can't locate %s device via /dev/bio", enclosure);
993 1.1 bouyer
994 1.1 bouyer memset(&blink, 0, sizeof(blink));
995 1.1 bouyer blink.bb_cookie = bio.bl_cookie;
996 1.1 bouyer blink.bb_status = blinktype;
997 1.1 bouyer blink.bb_target = target;
998 1.1 bouyer
999 1.7 xtraeme if (ioctl(fd, BIOCBLINK, &blink) == -1)
1000 1.7 xtraeme err(EXIT_FAILURE, "BIOCBLINK");
1001 1.1 bouyer }
1002