summaryrefslogtreecommitdiff
path: root/sbin/bioctl/bioctl.c
blob: b846f941a37719d6aecc08f93cf20efe853052d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
/* $NetBSD: bioctl.c,v 1.19 2022/05/10 14:16:25 msaitoh Exp $ */
/* $OpenBSD: bioctl.c,v 1.52 2007/03/20 15:26:06 jmc Exp $ */

/*
 * Copyright (c) 2007, 2008 Juan Romero Pardines
 * Copyright (c) 2004, 2005 Marco Peereboom
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
#include <sys/cdefs.h>

#ifndef lint
__RCSID("$NetBSD: bioctl.c,v 1.19 2022/05/10 14:16:25 msaitoh Exp $");
#endif

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <dev/biovar.h>

#include <errno.h>
#include <err.h>
#include <fcntl.h>
#include <util.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <util.h>

struct command {
	const char *cmd_name;
	const char *arg_names;
	void (*cmd_func)(int, int, char **);
};

struct biotmp {
	struct bioc_inq *bi;
	struct bioc_vol *bv;
	char volname[64];
	int fd;
	int volid;
	int diskid;
	bool format;
	bool show_disknovol;
};

struct locator {
	int channel;
	int target;
	int lun;
};

__dead static void 	usage(void);
static void	bio_alarm(int, int, char **);
static void	bio_show_common(int, int, char **);
static int	bio_show_volumes(struct biotmp *);
static void	bio_show_disks(struct biotmp *);
static void	bio_setblink(int, int, char **);
static void 	bio_blink(int, char *, int, int);
static void	bio_setstate_hotspare(int, int, char **);
static void	bio_setstate_passthru(int, int, char **);
static void	bio_setstate_common(int, char *, struct bioc_setstate *,
				    struct locator *);
static void	bio_setstate_consistency(int, int, char **);
static void	bio_volops_create(int, int, char **);
#ifdef notyet
static void	bio_volops_modify(int, int, char **);
#endif
static void	bio_volops_remove(int, int, char **);

static const char *str2locator(const char *, struct locator *);

static struct bio_locate bl;
static struct command commands[] = {
	{ 
	  "show",
	  "[disks] | [volumes]",
	  bio_show_common },
	{ 
	  "alarm",
	  "[enable] | [disable] | [silence] | [test]",
	  bio_alarm },
	{ 
	  "blink",
	  "start [channel:target[.lun]] | stop [channel:target[.lun]]",
	  bio_setblink },
	{
	  "hotspare",
	  "add channel:target.lun | remove channel:target.lun",
	  bio_setstate_hotspare },
	{
	  "passthru",
	  "add DISKID channel:target.lun | remove channel:target.lun",
	  bio_setstate_passthru },
	{
	  "check",
	  "start VOLID | stop VOLID",
	  bio_setstate_consistency },
	{
	  "create",
	  "volume VOLID DISKIDs [SIZE] STRIPE RAID_LEVEL channel:target.lun",
	  bio_volops_create },
#ifdef notyet
	{
	  "modify",
	  "volume VOLID STRIPE RAID_LEVEL channel:target.lun",
	  bio_volops_modify },
#endif
	{
	  "remove",
	  "volume VOLID channel:target.lun",
	  bio_volops_remove },

	{ NULL, NULL, NULL }
};

int
main(int argc, char **argv)
{
	char 		*dvname;
	const char 	*cmdname;
	int 		fd = 0, i;

	/* Must have at least: device command */
	if (argc < 3)
		usage();

	/* Skip program name, get and skip device name and command */
	setprogname(*argv);
	dvname = argv[1];
	cmdname = argv[2];
	argv += 3;
	argc -= 3;

	/* Look up and call the command */
	for (i = 0; commands[i].cmd_name != NULL; i++)
		if (strcmp(cmdname, commands[i].cmd_name) == 0)
			break;
	if (commands[i].cmd_name == NULL)
		errx(EXIT_FAILURE, "unknown command: %s", cmdname);

	/* Locate the device by issuing the BIOCLOCATE ioctl */
	fd = open("/dev/bio", O_RDWR);
	if (fd == -1)
		err(EXIT_FAILURE, "Can't open /dev/bio");

	bl.bl_name = dvname;
	if (ioctl(fd, BIOCLOCATE, &bl) == -1)
		errx(EXIT_FAILURE, "Can't locate %s device via /dev/bio",
		    bl.bl_name);

	/* and execute the command */
	(*commands[i].cmd_func)(fd, argc, argv);

	(void)close(fd);
	exit(EXIT_SUCCESS);
}

static void
usage(void)
{
	int i;

	(void)fprintf(stderr, "usage: %s device command [arg [...]]\n",
	    getprogname());

	(void)fprintf(stderr, "Available commands:\n");
	for (i = 0; commands[i].cmd_name != NULL; i++)
		(void)fprintf(stderr, "  %s %s\n", commands[i].cmd_name,
		    commands[i].arg_names);

	exit(EXIT_FAILURE);
	/* NOTREACHED */
}

static const char *
str2locator(const char *string, struct locator *location)
{
	const char 	*errstr;
	char 		parse[80], *targ, *lun;

	strlcpy(parse, string, sizeof parse);
	targ = strchr(parse, ':');
	if (targ == NULL)
		return "target not specified";

	*targ++ = '\0';
	lun = strchr(targ, '.');
	if (lun != NULL) {
		*lun++ = '\0';
		location->lun = strtonum(lun, 0, 256, &errstr);
		if (errstr)
			return errstr;
	} else
		location->lun = 0;

	location->target = strtonum(targ, 0, 256, &errstr);
	if (errstr)
		return errstr;
	location->channel = strtonum(parse, 0, 256, &errstr);
	if (errstr)
		return errstr;
	return NULL;
}

/*
 * Shows info about available RAID volumes.
 */
static int
bio_show_volumes(struct biotmp *bt)
{
	struct bioc_vol 	bv;
	const char 		*status, *rtypestr, *stripestr;
	char 			size[64], percent[16], seconds[20];
	char 			rtype[16], stripe[16], tmp[48];

	rtypestr = stripestr = NULL;

	memset(&bv, 0, sizeof(bv));
	bv.bv_cookie = bl.bl_cookie;
	bv.bv_volid = bt->volid;
	bv.bv_percent = -1;
	bv.bv_seconds = 0;

	if (ioctl(bt->fd, BIOCVOL, &bv) == -1)
		err(EXIT_FAILURE, "BIOCVOL");

	percent[0] = '\0';
	seconds[0] = '\0';
	if (bv.bv_percent != -1)
		snprintf(percent, sizeof(percent),
		    " %3.2f%% done", bv.bv_percent / 10.0);
	if (bv.bv_seconds)
		snprintf(seconds, sizeof(seconds),
		    " %u seconds", bv.bv_seconds);

	switch (bv.bv_status) {
	case BIOC_SVONLINE:
		status = BIOC_SVONLINE_S;
		break;
	case BIOC_SVOFFLINE:
		status = BIOC_SVOFFLINE_S;
		break;
	case BIOC_SVDEGRADED:
		status = BIOC_SVDEGRADED_S;
		break;
	case BIOC_SVBUILDING:
		status = BIOC_SVBUILDING_S;
		break;
	case BIOC_SVREBUILD:
		status = BIOC_SVREBUILD_S;
		break;
	case BIOC_SVMIGRATING:
		status = BIOC_SVMIGRATING_S;
		break;
	case BIOC_SVSCRUB:
		status = BIOC_SVSCRUB_S;
		break;
	case BIOC_SVCHECKING:
		status = BIOC_SVCHECKING_S;
		break;
	case BIOC_SVINVALID:
	default:
		status = BIOC_SVINVALID_S;
		break;
	}

	snprintf(bt->volname, sizeof(bt->volname), "%u", bv.bv_volid);
	if (bv.bv_vendor[0])
		snprintf(tmp, sizeof(tmp), "%s %s", bv.bv_dev, bv.bv_vendor);
	else
		snprintf(tmp, sizeof(tmp), "%s", bv.bv_dev);

	switch (bv.bv_level) {
	case BIOC_SVOL_HOTSPARE:
		rtypestr = "Hot spare";
		stripestr = "N/A";
		break;
	case BIOC_SVOL_PASSTHRU:
		rtypestr = "Pass through";
		stripestr = "N/A";
		break;
	case BIOC_SVOL_RAID01:
		rtypestr = "RAID 0+1";
		break;
	case BIOC_SVOL_RAID10:
		rtypestr = "RAID 1+0";
		break;
	default:
		snprintf(rtype, sizeof(rtype), "RAID %u", bv.bv_level);
		if (bv.bv_level == 1 || bv.bv_stripe_size == 0)
			stripestr = "N/A";
		break;
	}

	if (rtypestr)
		strlcpy(rtype, rtypestr, sizeof(rtype));
	if (stripestr)
		strlcpy(stripe, stripestr, sizeof(stripe));
	else
		snprintf(stripe, sizeof(stripe), "%uK", bv.bv_stripe_size);

	humanize_number(size, 5, (int64_t)bv.bv_size, "", HN_AUTOSCALE,
	    HN_B | HN_NOSPACE | HN_DECIMAL);

	printf("%6s %-12s %4s %20s %8s %6s %s%s\n",
	    bt->volname, status, size, tmp,
	    rtype, stripe, percent, seconds);

	bt->bv = &bv;

	return bv.bv_nodisk;
}

/*
 * Shows info about physical disks.
 */
static void
bio_show_disks(struct biotmp *bt)
{
	struct bioc_disk 	bd;
	const char 		*status;
	char 			size[64], serial[32], scsiname[34];

	memset(&bd, 0, sizeof(bd));
	bd.bd_cookie = bl.bl_cookie;
	bd.bd_diskid = bt->diskid;
	bd.bd_volid = bt->volid;

	if (bt->show_disknovol) {
		if (ioctl(bt->fd, BIOCDISK_NOVOL, &bd) == -1)
			err(EXIT_FAILURE, "BIOCDISK_NOVOL");
		if (!bd.bd_disknovol)
			return;
	} else {
		if (ioctl(bt->fd, BIOCDISK, &bd) == -1)
			err(EXIT_FAILURE, "BIOCDISK");
	}

	switch (bd.bd_status) {
	case BIOC_SDONLINE:
		status = BIOC_SDONLINE_S;
		break;
	case BIOC_SDOFFLINE:
		status = BIOC_SDOFFLINE_S;
		break;
	case BIOC_SDFAILED:
		status = BIOC_SDFAILED_S;
		break;
	case BIOC_SDREBUILD:
		status = BIOC_SDREBUILD_S;
		break;
	case BIOC_SDHOTSPARE:
		status = BIOC_SDHOTSPARE_S;
		break;
	case BIOC_SDUNUSED:
		status = BIOC_SDUNUSED_S;
		break;
	case BIOC_SDSCRUB:
		status = BIOC_SDSCRUB_S;
		break;
	case BIOC_SDPASSTHRU:
		status = BIOC_SDPASSTHRU_S;
		break;
	case BIOC_SDINVALID:
	default:
		status = BIOC_SDINVALID_S;
		break;
	}

	if (bt->format)
		snprintf(bt->volname, sizeof(bt->volname),
		    "%u:%u", bt->bv->bv_volid, bd.bd_diskid);

	humanize_number(size, 5, bd.bd_size, "", HN_AUTOSCALE,
	    HN_B | HN_NOSPACE | HN_DECIMAL);

	if (bd.bd_procdev[0])
		snprintf(scsiname, sizeof(scsiname), "%u:%u.%u %s",
	    	    bd.bd_channel, bd.bd_target, bd.bd_lun,
		    bd.bd_procdev);
	else
		snprintf(scsiname, sizeof(scsiname), "%u:%u.%u noencl",
		    bd.bd_channel, bd.bd_target, bd.bd_lun);

	if (bd.bd_serial[0])
		strlcpy(serial, bd.bd_serial, sizeof(serial));
	else
		strlcpy(serial, "unknown serial", sizeof(serial));

	if (bt->format)
		printf("%6s %-12s %4s %20s <%s>\n",
		    bt->volname, status, size, scsiname,
				    bd.bd_vendor);
	else
		printf("%5d [%-28s] %-12s %-6s %12s\n",
		    bt->diskid, bd.bd_vendor, status, size, scsiname);

}

/*
 * Shows info about volumes/disks.
 */
static void
bio_show_common(int fd, int argc, char **argv)
{
	struct biotmp 		*biot;
	struct bioc_inq 	bi;
	int 			i, d, ndisks;
	bool 			show_all, show_disks;
	bool 			show_vols, show_caps;

	show_all = show_disks = show_vols = show_caps = false;

	if (argc > 1)
		usage();

	if (argv[0]) {
		if (strcmp(argv[0], "disks") == 0)
			show_disks = true;
		else if (strcmp(argv[0], "volumes") == 0)
			show_vols = true;
		else
			usage();
	} else
		show_all = true;

	memset(&bi, 0, sizeof(bi));
	bi.bi_cookie = bl.bl_cookie;

	if (ioctl(fd, BIOCINQ, &bi) == -1)
		err(EXIT_FAILURE, "BIOCINQ");

	/*
	 * If there are volumes there's no point to continue.
	 */
	if (show_all || show_vols) {
		if (!bi.bi_novol) {
			warnx("no volumes available");
			return;
		}
	}

	biot = calloc(1, sizeof(*biot));
	if (!biot)
		err(EXIT_FAILURE, "biotemp calloc");

	biot->fd = fd;
	biot->bi = &bi;
	/*
	 * Go to the disks section if that was specified.
	 */
	if (show_disks)
		goto disks;

	/* 
	 * Common code to show only info about volumes and disks
	 * associated to them.
	 */
	printf("%6s %-12s %4s %20s %8s %6s\n",
	    "Volume", "Status", "Size", "Device/Label",
	    "Level", "Stripe");
	printf("=============================================="
	       "===============\n");

	for (i = 0; i < bi.bi_novol; i++) {
		biot->format = true;
		biot->volid = i;
		ndisks = bio_show_volumes(biot);
		if (show_vols)
			continue;

		for (d = 0; d < ndisks; d++) {
			biot->diskid = d;
			bio_show_disks(biot);
		}

	}
	goto out;

disks:
	/* 
	 * show info about all disks connected to the raid controller,
	 * even if they aren't associated with a volume or raid set.
	 */
	if (show_disks) {
		printf("%5s %-30s %-12s %-6s %12s\n",
		    "Disk", "Model/Serial", "Status", "Size", "Location");
		printf("==============================================="
		       "======================\n");
		for (d = 0; d < bi.bi_nodisk; d++) {
			biot->show_disknovol = true;
			biot->diskid = d;
			bio_show_disks(biot);
		}
	}
out:
	free(biot);
}

/*
 * To handle the alarm feature.
 */
static void
bio_alarm(int fd, int argc, char **argv)
{
	struct bioc_alarm 	ba;
	bool 			show = false;

	memset(&ba, 0, sizeof(ba));
	ba.ba_cookie = bl.bl_cookie;

	if (argc > 1)
		usage();

	if (argc == 0) {
		/* show alarm status */
		ba.ba_opcode = BIOC_GASTATUS;
		show = true;
	} else if (strcmp(argv[0], "silence") == 0) {
		/* silence alarm */
		ba.ba_opcode = BIOC_SASILENCE;
	} else if (strcmp(argv[0], "enable") == 0) {
		/* enable alarm */
		ba.ba_opcode = BIOC_SAENABLE;
	} else if (strcmp(argv[0], "disable") == 0) {
		/* disable alarm */
		ba.ba_opcode = BIOC_SADISABLE;
	} else if (strcmp(argv[0], "test") == 0) {
		/* test alarm */
		ba.ba_opcode = BIOC_SATEST;
	} else
		usage();

	if (ioctl(fd, BIOCALARM, &ba) == -1)
		err(EXIT_FAILURE, "BIOCALARM");

	if (show)
		printf("alarm is currently %s\n",
		    ba.ba_status ? "enabled" : "disabled");
}

/*
 * To add/remove a hotspare disk.
 */
static void
bio_setstate_hotspare(int fd, int argc, char **argv)
{
	struct bioc_setstate	bs;
	struct locator		location;

	memset(&bs, 0, sizeof(bs));

	if (argc != 2)
		usage();

	if (strcmp(argv[0], "add") == 0)
		bs.bs_status = BIOC_SSHOTSPARE;
	else if (strcmp(argv[0], "remove") == 0)
		bs.bs_status = BIOC_SSDELHOTSPARE;
	else
		usage();

	bio_setstate_common(fd, argv[1], &bs, &location);
}

/*
 * To add/remove a pass through disk.
 */
static void
bio_setstate_passthru(int fd, int argc, char **argv)
{
	struct bioc_setstate	bs;
	struct locator		location;
	char			*endptr;
	bool			rem = false;

	if (argc < 2 || argc > 3)
		usage();

	memset(&bs, 0, sizeof(bs));

	if (strcmp(argv[0], "add") == 0) {
		if (argv[1] == NULL || argv[2] == NULL)
			usage();

		bs.bs_status = BIOC_SSPASSTHRU;
	} else if (strcmp(argv[0], "remove") == 0) {
		if (argv[1] == NULL)
			usage();

		bs.bs_status = BIOC_SSDELPASSTHRU;
		rem = true;
	} else
		usage();

	if (rem)
		bio_setstate_common(fd, argv[1], &bs, &location);
	else {
		bs.bs_other_id = (unsigned int)strtoul(argv[1], &endptr, 10);
		if (*endptr != '\0')
			errx(EXIT_FAILURE, "Invalid Volume ID value");

		bio_setstate_common(fd, argv[2], &bs, &location);
	}
}

/*
 * To start/stop a consistency check in a RAID volume.
 */
static void
bio_setstate_consistency(int fd, int argc, char **argv)
{
	struct bioc_setstate	bs;
	char			*endptr;

	if (argc != 2)
		usage();

	memset(&bs, 0, sizeof(bs));

	if (strcmp(argv[0], "start") == 0)
		bs.bs_status = BIOC_SSCHECKSTART_VOL;
	else if (strcmp(argv[0], "stop") == 0)
		bs.bs_status = BIOC_SSCHECKSTOP_VOL;
	else
		usage();

	bs.bs_volid = (unsigned int)strtoul(argv[1], &endptr, 10);
	if (*endptr != '\0')
		errx(EXIT_FAILURE, "Invalid Volume ID value");

	bio_setstate_common(fd, NULL, &bs, NULL);
}

static void
bio_setstate_common(int fd, char *arg, struct bioc_setstate *bs,
		    struct locator *location)
{
	const char		*errstr;

	if (!arg || !location)
		goto send;

	errstr = str2locator(arg, location);
	if (errstr)
		errx(EXIT_FAILURE, "Target %s: %s", arg, errstr);

	bs->bs_channel = location->channel;
	bs->bs_target = location->target;
	bs->bs_lun = location->lun;

send:
	bs->bs_cookie = bl.bl_cookie;

	if (ioctl(fd, BIOCSETSTATE, bs) == -1)
		err(EXIT_FAILURE, "BIOCSETSTATE");
}

/*
 * To create a RAID volume.
 */
static void
bio_volops_create(int fd, int argc, char **argv)
{
	struct bioc_volops	bc;
	struct bioc_inq 	bi;
	struct bioc_disk	bd;
	struct locator		location;
	uint64_t 		total_size = 0, disksize = 0;
	int64_t 		volsize = 0;
	const char 		*errstr;
	char 			*endptr, *stripe, levelstr[32];
	char			*scsiname, *raid_level, size[64];
	int			disk_first = 0, disk_end = 0;
	int			i, nfreedisks = 0;
	int			user_disks = 0;

	if (argc < 6 || argc > 7)
		usage();

	if (strcmp(argv[0], "volume") != 0)
		usage();

	/* 
	 * No size requested, use max size depending on RAID level.
	 */
	if (argc == 6) {
		stripe = argv[3];
		raid_level = argv[4];
		scsiname = argv[5];
	} else {
		stripe = argv[4];
		raid_level = argv[5];
		scsiname = argv[6];
	}

	memset(&bd, 0, sizeof(bd));
	memset(&bc, 0, sizeof(bc));
	memset(&bi, 0, sizeof(bi));

	bc.bc_cookie = bd.bd_cookie = bi.bi_cookie = bl.bl_cookie;
	bc.bc_opcode = BIOC_VCREATE_VOLUME;

	bc.bc_volid = (unsigned int)strtoul(argv[1], &endptr, 10);
	if (*endptr != '\0')
		errx(EXIT_FAILURE, "Invalid Volume ID value");

	if (argc == 7)
		if (dehumanize_number(argv[3], &volsize) == -1
		    || volsize < 0)
			errx(EXIT_FAILURE, "Invalid SIZE value");

	bc.bc_stripe = (unsigned int)strtoul(stripe, &endptr, 10);
	if (*endptr != '\0')
		errx(EXIT_FAILURE, "Invalid STRIPE size value");

	bc.bc_level = (unsigned int)strtoul(raid_level, &endptr, 10);
	if (*endptr != '\0')
		errx(EXIT_FAILURE, "Invalid RAID_LEVEL value");

	errstr = str2locator(scsiname, &location);
	if (errstr)
		errx(EXIT_FAILURE, "Target %s: %s", scsiname, errstr);

	/*
	 * Parse the device list that will be used for the volume,
	 * by using a bit field for the disks.
	 */
	if ((isdigit((unsigned char)argv[2][0]) == 0) || argv[2][1] != '-' ||
	    (isdigit((unsigned char)argv[2][2]) == 0))
		errx(EXIT_FAILURE, "Invalid DISKIDs value");

	disk_first = atoi(&argv[2][0]);
	disk_end = atoi(&argv[2][2]);

	for (i = disk_first; i < disk_end + 1; i++) {
		bc.bc_devmask |= (1 << i);
		user_disks++;
	}

	/*
	 * Find out how many disks are free and how much size we
	 * have available for the new volume.
	 */
	if (ioctl(fd, BIOCINQ, &bi) == -1)
		err(EXIT_FAILURE, "BIOCINQ");

	for (i = 0; i < bi.bi_nodisk; i++) {
		bd.bd_diskid = i;
		if (ioctl(fd, BIOCDISK_NOVOL, &bd) == -1)
			err(EXIT_FAILURE, "BIOCDISK_NOVOL");

		if (bd.bd_status == BIOC_SDUNUSED) {
			if (i == 0)
				disksize = bd.bd_size;

			total_size += bd.bd_size;
			nfreedisks++;
		}
	}

	if (user_disks > nfreedisks)
		errx(EXIT_FAILURE, "specified disks number is higher than "
		    "available free disks");

	/*
	 * Basic checks to be sure we don't do something stupid.
	 */
	if (nfreedisks == 0)
		errx(EXIT_FAILURE, "No free disks available");

	switch (bc.bc_level) {
	case 0:	/* RAID 0 requires at least one disk */
		if (argc == 7) {
			if ((uint64_t)volsize > (disksize * user_disks))
				errx(EXIT_FAILURE, "volume size specified "
				   "is larger than available on free disks");
			bc.bc_size = (uint64_t)volsize;
		} else
			bc.bc_size = disksize * user_disks;

		break;
	case 1:	/* RAID 1 requires two disks and size is total / 2 */
		if (nfreedisks < 2 || user_disks < 2)
			errx(EXIT_FAILURE, "2 disks are required at least for "
			    "this RAID level");

		/* RAID 1+0 requires three disks at least */
		if (nfreedisks > 2 && user_disks > 2)
			bc.bc_level = BIOC_SVOL_RAID10;

		if (argc == 7) {
			if ((uint64_t)volsize > ((disksize * user_disks) / 2))
				errx(EXIT_FAILURE, "volume size specified "
				   "is larger than available on free disks");
			bc.bc_size = (uint64_t)volsize;
		} else
			bc.bc_size = ((disksize * user_disks) / 2);

		break;
	case 3:	/* RAID 3/5 requires three disks and size is total - 1 disk */
	case 5:
		if (nfreedisks < 3 || user_disks < 3)
			errx(EXIT_FAILURE, "3 disks are required at least for "
			    "this RAID level");

		if (argc == 7) {
			if ((uint64_t)volsize > (disksize * (user_disks - 1)))
				errx(EXIT_FAILURE, "volume size specified "
				    "is larger than available on free disks");
			bc.bc_size = (uint64_t)volsize;
		} else
			bc.bc_size = (disksize * (user_disks - 1));

		break;
	case 6:	/* RAID 6 requires four disks and size is total - 2 disks */
		if (nfreedisks < 4 || user_disks < 4)
			errx(EXIT_FAILURE, "4 disks are required at least for "
			    "this RAID level");

		if (argc == 7) {
			if ((uint64_t)volsize >
			    ((disksize * user_disks) - (disksize * 2)))
				err(EXIT_FAILURE, "volume size specified "
				    "is larger than available on free disks");
			bc.bc_size = (uint64_t)volsize;
		} else
			bc.bc_size =
			    (((disksize * user_disks) - (disksize * 2)));

		break;
	default:
		errx(EXIT_FAILURE, "Unsupported RAID level");
	}

	bc.bc_channel = location.channel;
	bc.bc_target = location.target;
	bc.bc_lun = location.lun;

	if (ioctl(fd, BIOCVOLOPS, &bc) == -1)
		err(EXIT_FAILURE, "BIOCVOLOPS");

        humanize_number(size, 5, bc.bc_size, "", HN_AUTOSCALE,
	    HN_B | HN_NOSPACE | HN_DECIMAL);

	if (bc.bc_level == BIOC_SVOL_RAID10)
		snprintf(levelstr, sizeof(levelstr), "1+0");
	else
		snprintf(levelstr, sizeof(levelstr), "%u", bc.bc_level);

	printf("Created volume %u size: %s stripe: %uK level: %s "
	    "SCSI location: %u:%u.%u\n", bc.bc_volid, size, bc.bc_stripe,
	    levelstr, bc.bc_channel, bc.bc_target, bc.bc_lun);
}

#ifdef notyet
/*
 * To modify a RAID volume.
 */
static void
bio_volops_modify(int fd, int argc, char **argv)
{
	/* XTRAEME: TODO */
}
#endif

/*
 * To remove a RAID volume.
 */
static void
bio_volops_remove(int fd, int argc, char **argv)
{
	struct bioc_volops	bc;
	struct locator		location;
	const char		*errstr;
	char			*endptr;

	if (argc != 3 || strcmp(argv[0], "volume") != 0)
		usage();

	memset(&bc, 0, sizeof(bc));
	bc.bc_cookie = bl.bl_cookie;
	bc.bc_opcode = BIOC_VREMOVE_VOLUME;

	bc.bc_volid = (unsigned int)strtoul(argv[1], &endptr, 10);
	if (*endptr != '\0')
		errx(EXIT_FAILURE, "Invalid Volume ID value");

	errstr = str2locator(argv[2], &location);
	if (errstr)
		errx(EXIT_FAILURE, "Target %s: %s", argv[2], errstr);

	bc.bc_channel = location.channel;
	bc.bc_target = location.target;
	bc.bc_lun = location.lun;

	if (ioctl(fd, BIOCVOLOPS, &bc) == -1)
		err(EXIT_FAILURE, "BIOCVOLOPS");

	printf("Removed volume %u at SCSI location %u:%u.%u\n",
	    bc.bc_volid, bc.bc_channel, bc.bc_target, bc.bc_lun);
}

/*
 * To blink/unblink a disk in enclosures.
 */
static void
bio_setblink(int fd, int argc, char **argv)
{
	struct locator		location;
	struct bioc_inq		bi;
	struct bioc_vol		bv;
	struct bioc_disk	bd;
	struct bioc_blink	bb;
	const char		*errstr;
	int			v, d, rv, blink = 0;

	if (argc != 2)
		usage();

	if (strcmp(argv[0], "start") == 0)
		blink = BIOC_SBBLINK;
	else if (strcmp(argv[0], "stop") == 0)
		blink = BIOC_SBUNBLINK;
	else
		usage();

	errstr = str2locator(argv[1], &location);
	if (errstr)
		errx(EXIT_FAILURE, "Target %s: %s", argv[1], errstr);

	/* try setting blink on the device directly */
	memset(&bb, 0, sizeof(bb));
	bb.bb_cookie = bl.bl_cookie;
	bb.bb_status = blink;
	bb.bb_target = location.target;
	bb.bb_channel = location.channel;
	rv = ioctl(fd, BIOCBLINK, &bb);
	if (rv == 0)
		return;

	/* if the blink didnt work, try to find something that will */
	memset(&bi, 0, sizeof(bi));
	bi.bi_cookie = bl.bl_cookie;
	rv = ioctl(fd, BIOCINQ, &bi);
	if (rv == -1)
		err(EXIT_FAILURE, "BIOCINQ");

	for (v = 0; v < bi.bi_novol; v++) {
		memset(&bv, 0, sizeof(bv));
		bv.bv_cookie = bl.bl_cookie;
		bv.bv_volid = v;
		rv = ioctl(fd, BIOCVOL, &bv);
		if (rv == -1)
			err(EXIT_FAILURE, "BIOCVOL");

		for (d = 0; d < bv.bv_nodisk; d++) {
			memset(&bd, 0, sizeof(bd));
			bd.bd_cookie = bl.bl_cookie;
			bd.bd_volid = v;
			bd.bd_diskid = d;

			rv = ioctl(fd, BIOCDISK, &bd);
			if (rv == -1)
				err(EXIT_FAILURE, "BIOCDISK");

			if (bd.bd_channel == location.channel &&
			    bd.bd_target == location.target &&
			    bd.bd_lun == location.lun) {
				if (bd.bd_procdev[0] != '\0') {
					bio_blink(fd, bd.bd_procdev,
					    location.target, blink);
				} else
					warnx("Disk %s is not in an enclosure",
					   argv[1]);
				return;
			}
		}
	}

	warnx("Disk %s does not exist", argv[1]);
}

static void
bio_blink(int fd, char *enclosure, int target, int blinktype)
{
	struct bio_locate	bio;
	struct bioc_blink	blink;

	bio.bl_name = enclosure;
	if (ioctl(fd, BIOCLOCATE, &bio) == -1)
		errx(EXIT_FAILURE,
		    "Can't locate %s device via /dev/bio", enclosure);

	memset(&blink, 0, sizeof(blink));
	blink.bb_cookie = bio.bl_cookie;
	blink.bb_status = blinktype;
	blink.bb_target = target;

	if (ioctl(fd, BIOCBLINK, &blink) == -1)
		err(EXIT_FAILURE, "BIOCBLINK");
}