2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3 * Copyright (c) 1997-2007 Kenneth D. Merry
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * substantially similar to the "NO WARRANTY" disclaimer below
14 * ("Disclaimer") and any redistribution must be conditioned upon
15 * including a substantially similar Disclaimer requirement for further
16 * binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGES.
31 * $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.c#4 $
34 * CAM Target Layer exercise program.
36 * Author: Ken Merry <ken@FreeBSD.org>
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/ioctl.h>
43 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/callout.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 #include <cam/ctl/ctl.h>
63 #include <cam/ctl/ctl_io.h>
64 #include <cam/ctl/ctl_frontend_internal.h>
65 #include <cam/ctl/ctl_backend.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_backend_block.h>
68 #include <cam/ctl/ctl_util.h>
69 #include <cam/ctl/ctl_scsi_all.h>
76 #define min(x,y) (x < y) ? x : y
83 CTLADM_CMD_REPORT_LUNS,
92 CTLADM_CMD_READCAPACITY,
95 CTLADM_CMD_DUMPSTRUCTS,
98 CTLADM_CMD_SYNC_CACHE,
103 CTLADM_CMD_HARDSTART,
108 CTLADM_CMD_ERR_INJECT,
112 CTLADM_CMD_INQ_VPD_DEVID,
114 } ctladm_cmdfunction;
117 CTLADM_ARG_NONE = 0x0000000,
118 CTLADM_ARG_AUTOSENSE = 0x0000001,
119 CTLADM_ARG_DEVICE = 0x0000002,
120 CTLADM_ARG_ARRAYSIZE = 0x0000004,
121 CTLADM_ARG_BACKEND = 0x0000008,
122 CTLADM_ARG_CDBSIZE = 0x0000010,
123 CTLADM_ARG_DATALEN = 0x0000020,
124 CTLADM_ARG_FILENAME = 0x0000040,
125 CTLADM_ARG_LBA = 0x0000080,
126 CTLADM_ARG_PC = 0x0000100,
127 CTLADM_ARG_PAGE_CODE = 0x0000200,
128 CTLADM_ARG_PAGE_LIST = 0x0000400,
129 CTLADM_ARG_SUBPAGE = 0x0000800,
130 CTLADM_ARG_PAGELIST = 0x0001000,
131 CTLADM_ARG_DBD = 0x0002000,
132 CTLADM_ARG_TARG_LUN = 0x0004000,
133 CTLADM_ARG_BLOCKSIZE = 0x0008000,
134 CTLADM_ARG_IMMED = 0x0010000,
135 CTLADM_ARG_RELADR = 0x0020000,
136 CTLADM_ARG_RETRIES = 0x0040000,
137 CTLADM_ARG_ONOFFLINE = 0x0080000,
138 CTLADM_ARG_ONESHOT = 0x0100000,
139 CTLADM_ARG_TIMEOUT = 0x0200000,
140 CTLADM_ARG_INITIATOR = 0x0400000,
141 CTLADM_ARG_NOCOPY = 0x0800000,
142 CTLADM_ARG_NEED_TL = 0x1000000
148 ctladm_cmdargs argnum;
158 static const char rw_opts[] = "Nb:c:d:f:l:";
159 static const char startstop_opts[] = "io";
161 struct ctladm_opts option_table[] = {
162 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
163 {"bbrread", CTLADM_CMD_BBRREAD, CTLADM_ARG_NEED_TL, "d:l:"},
164 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
165 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
166 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
167 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
168 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
169 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
170 {"getsync", CTLADM_CMD_GETSYNC, CTLADM_ARG_NEED_TL, NULL},
171 {"hardstart", CTLADM_CMD_HARDSTART, CTLADM_ARG_NONE, NULL},
172 {"hardstop", CTLADM_CMD_HARDSTOP, CTLADM_ARG_NONE, NULL},
173 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
174 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
175 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
176 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
177 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
178 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
179 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
180 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
181 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
182 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
183 {"realsync", CTLADM_CMD_REALSYNC, CTLADM_ARG_NONE, NULL},
184 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
185 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
186 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
187 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
188 {"setsync", CTLADM_CMD_SETSYNC, CTLADM_ARG_NEED_TL, "i:"},
189 {"shutdown", CTLADM_CMD_SHUTDOWN, CTLADM_ARG_NONE, NULL},
190 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
191 {"startup", CTLADM_CMD_STARTUP, CTLADM_ARG_NONE, NULL},
192 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
193 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
194 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
195 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
196 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
197 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
202 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
203 ctladm_cmdargs *argnum, const char **subopt);
204 static int cctl_parse_tl(char *str, int *target, int *lun);
205 static int cctl_dump_ooa(int fd, int argc, char **argv);
206 static int cctl_port_dump(int fd, int quiet, int xml, int32_t fe_num,
207 ctl_port_type port_type);
208 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
209 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
210 static int cctl_delay(int fd, int target, int lun, int argc, char **argv,
212 static int cctl_lunlist(int fd);
213 static void cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len);
214 static void cctl_cfi_bbr_statusstr(cfi_bbrread_status, char *str, int str_len);
215 static int cctl_hardstopstart(int fd, ctladm_cmdfunction command);
216 static int cctl_bbrread(int fd, int target, int lun, int iid, int argc,
217 char **argv, char *combinedopt);
218 static int cctl_startup_shutdown(int fd, int target, int lun, int iid,
219 ctladm_cmdfunction command);
220 static int cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
221 int argc, char **argv, char *combinedopt);
222 static int cctl_start_stop(int fd, int target, int lun, int iid, int retries,
223 int start, int argc, char **argv, char *combinedopt);
224 static int cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
225 int argc, char **argv, char *combinedopt);
226 static int cctl_read_capacity(int fd, int target, int lun, int iid,
227 int retries, int argc, char **argv,
229 static int cctl_read_write(int fd, int target, int lun, int iid, int retries,
230 int argc, char **argv, char *combinedopt,
231 ctladm_cmdfunction command);
232 static int cctl_get_luns(int fd, int target, int lun, int iid, int retries,
233 struct scsi_report_luns_data **lun_data,
235 static int cctl_report_luns(int fd, int target, int lun, int iid, int retries);
236 static int cctl_tur(int fd, int target, int lun, int iid, int retries);
237 static int cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
238 char *path_str, int path_len,
239 struct scsi_inquiry_data *inq_data);
240 static int cctl_inquiry(int fd, int target, int lun, int iid, int retries);
241 static int cctl_req_sense(int fd, int target, int lun, int iid, int retries);
242 static int cctl_persistent_reserve_in(int fd, int target, int lun,
243 int initiator, int argc, char **argv,
244 char *combinedopt, int retry_count);
245 static int cctl_persistent_reserve_out(int fd, int target, int lun,
246 int initiator, int argc, char **argv,
247 char *combinedopt, int retry_count);
248 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
249 static int cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator);
250 static int cctl_report_target_port_group(int fd, int target, int lun,
254 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
255 ctladm_cmdargs *argnum, const char **subopt)
257 struct ctladm_opts *opts;
260 for (opts = table; (opts != NULL) && (opts->optname != NULL);
262 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
263 *cmdnum = opts->cmdnum;
264 *argnum = opts->argnum;
265 *subopt = opts->subopt;
267 if (strcmp(opts->optname, arg) == 0)
268 return (CC_OR_FOUND);
270 if (++num_matches > 1)
271 return(CC_OR_AMBIGUOUS);
278 return(CC_OR_NOT_FOUND);
283 cctl_parse_tl(char *str, int *target, int *lun)
290 while (isspace(*str) && (*str != '\0'))
293 tmpstr = (char *)strtok(str, ":");
294 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
295 *target = strtol(tmpstr, NULL, 0);
296 tmpstr = (char *)strtok(NULL, ":");
297 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
298 *lun = strtol(tmpstr, NULL, 0);
308 cctl_dump_ooa(int fd, int argc, char **argv)
311 long double cmd_latency;
312 int num_entries, len;
313 int target = -1, lun = -1;
320 && (isdigit(argv[2][0]))) {
321 retval = cctl_parse_tl(argv[2], &target, &lun);
323 warnx("invalid target:lun argument %s", argv[2]);
327 len = num_entries * sizeof(struct ctl_ooa_entry);
329 bzero(&ooa, sizeof(ooa));
331 ooa.entries = malloc(len);
333 if (ooa.entries == NULL) {
334 warn("%s: error mallocing %d bytes", __func__, len);
341 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
344 ooa.alloc_num = num_entries;
345 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
346 warn("%s: CTL_GET_OOA ioctl failed", __func__);
351 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
352 num_entries = num_entries * 2;
358 if (ooa.status != CTL_OOA_OK) {
359 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
365 fprintf(stdout, "Dumping OOA queues\n");
366 for (i = 0; i < ooa.fill_num; i++) {
367 struct ctl_ooa_entry *entry;
368 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
369 struct bintime delta_bt;
372 entry = &ooa.entries[i];
374 delta_bt = ooa.cur_bt;
375 bintime_sub(&delta_bt, &entry->start_bt);
376 bintime2timespec(&delta_bt, &ts);
377 cmd_latency = ts.tv_sec * 1000;
379 cmd_latency += ts.tv_nsec / 1000000;
381 fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
383 (intmax_t)entry->lun_num, entry->tag_num,
384 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
386 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
387 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
389 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
391 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
392 scsi_op_desc(entry->cdb[0], NULL),
393 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
396 fprintf(stdout, "OOA queues dump done\n");
398 if (ioctl(fd, CTL_DUMP_OOA) == -1) {
399 warn("%s: CTL_DUMP_OOA ioctl failed", __func__);
411 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
413 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
421 cctl_port_dump(int fd, int quiet, int xml, int32_t targ_port,
422 ctl_port_type port_type)
424 struct ctl_port_list port_list;
425 struct ctl_port_entry *entries;
426 struct sbuf *sb = NULL;
435 entries = malloc(sizeof(*entries) * num_entries);
436 bzero(&port_list, sizeof(port_list));
437 port_list.entries = entries;
438 port_list.alloc_num = num_entries;
439 port_list.alloc_len = num_entries * sizeof(*entries);
440 if (ioctl(fd, CTL_GET_PORT_LIST, &port_list) != 0) {
441 warn("%s: CTL_GET_PORT_LIST ioctl failed", __func__);
444 if (port_list.status == CTL_PORT_LIST_NEED_MORE_SPACE) {
445 printf("%s: allocated %d, need %d, retrying\n", __func__,
446 num_entries, port_list.fill_num + port_list.dropped_num);
448 num_entries = port_list.fill_num + port_list.dropped_num;
454 printf("Port Online Type Name pp vp %-18s %-18s\n",
458 sb = sbuf_new_auto();
459 sbuf_printf(sb, "<ctlfelist>\n");
461 for (i = 0; i < port_list.fill_num; i++) {
462 struct ctl_port_entry *entry;
467 switch (entry->port_type) {
477 case CTL_PORT_INTERNAL:
489 * If the user specified a frontend number or a particular
490 * frontend type, only print out that particular frontend
493 if ((targ_port != -1)
494 && (targ_port != entry->targ_port))
496 else if ((port_type != CTL_PORT_NONE)
497 && ((port_type & entry->port_type) == 0))
503 printf("Num: %ju Type: %s (%#x) Name: %s Physical Port: %d "
504 "Virtual Port: %d\n", (uintmax_t)entry->fe_num, type,
505 entry->port_type, entry->fe_name, entry->physical_port,
506 entry->virtual_port);
507 printf("WWNN %#jx WWPN %#jx Online: %s\n",
508 (uintmax_t)entry->wwnn, (uintmax_t)entry->wwpn,
509 (entry->online) ? "YES" : "NO" );
512 printf("%-4d %-6s %-8s %-12s %-2d %-2d %#-18jx "
514 entry->targ_port, (entry->online) ? "YES" : "NO",
515 type, entry->port_name, entry->physical_port,
516 entry->virtual_port, (uintmax_t)entry->wwnn,
517 (uintmax_t)entry->wwpn);
519 sbuf_printf(sb, "<targ_port id=\"%d\">\n",
521 sbuf_printf(sb, "<online>%s</online>\n",
522 (entry->online) ? "YES" : "NO");
523 sbuf_printf(sb, "<port_type>%s</port_type>\n", type);
524 sbuf_printf(sb, "<port_name>%s</port_name>\n",
526 sbuf_printf(sb, "<physical_port>%d</physical_port>\n",
527 entry->physical_port);
528 sbuf_printf(sb, "<virtual_port>%d</virtual_port>\n",
529 entry->virtual_port);
530 sbuf_printf(sb, "<wwnn>%#jx</wwnn>\n",
531 (uintmax_t)entry->wwnn);
532 sbuf_printf(sb, "<wwpn>%#jx</wwpn>\n",
533 (uintmax_t)entry->wwpn);
534 sbuf_printf(sb, "</targ_port>\n");
539 sbuf_printf(sb, "</ctlfelist>\n");
541 printf("%s", sbuf_data(sb));
546 * Give some indication that we didn't find the frontend or
547 * frontend type requested by the user. We could print something
548 * out, but it would probably be better to hide that behind a
552 && ((targ_port != -1)
553 || (port_type != CTL_PORT_NONE)))
567 struct ctladm_opts cctl_fe_table[] = {
568 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
569 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
570 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
571 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
576 cctl_port(int fd, int argc, char **argv, char *combinedopt)
579 int32_t targ_port = -1;
581 int wwnn_set = 0, wwpn_set = 0;
582 uint64_t wwnn = 0, wwpn = 0;
583 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
584 struct ctl_port_entry entry;
585 ctl_port_type port_type = CTL_PORT_NONE;
586 int quiet = 0, xml = 0;
588 while ((c = getopt(argc, argv, combinedopt)) != -1) {
591 if (port_mode != CCTL_PORT_MODE_NONE)
594 port_mode = CCTL_PORT_MODE_LIST;
597 if (port_mode != CCTL_PORT_MODE_NONE)
600 if (strcasecmp(optarg, "on") == 0)
601 port_mode = CCTL_PORT_MODE_ON;
602 else if (strcasecmp(optarg, "off") == 0)
603 port_mode = CCTL_PORT_MODE_OFF;
605 warnx("Invalid -o argument %s, \"on\" or "
606 "\"off\" are the only valid args",
613 targ_port = strtol(optarg, NULL, 0);
619 ctladm_optret optret;
620 ctladm_cmdargs argnum;
622 ctl_port_type tmp_port_type;
624 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
626 if (optret == CC_OR_AMBIGUOUS) {
627 warnx("%s: ambiguous frontend type %s",
631 } else if (optret == CC_OR_NOT_FOUND) {
632 warnx("%s: invalid frontend type %s",
638 port_type |= tmp_port_type;
642 if ((port_mode != CCTL_PORT_MODE_NONE)
643 && (port_mode != CCTL_PORT_MODE_SET))
646 port_mode = CCTL_PORT_MODE_SET;
648 wwnn = strtoull(optarg, NULL, 0);
652 if ((port_mode != CCTL_PORT_MODE_NONE)
653 && (port_mode != CCTL_PORT_MODE_SET))
656 port_mode = CCTL_PORT_MODE_SET;
658 wwpn = strtoull(optarg, NULL, 0);
668 * The user can specify either one or more frontend types (-t), or
669 * a specific frontend, but not both.
671 * If the user didn't specify a frontend type or number, set it to
672 * all. This is primarily needed for the enable/disable ioctls.
673 * This will be a no-op for the listing code. For the set ioctl,
674 * we'll throw an error, since that only works on one port at a time.
676 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
677 warnx("%s: can only specify one of -t or -n", __func__);
680 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
681 port_type = CTL_PORT_ALL;
683 bzero(&entry, sizeof(&entry));
686 * These are needed for all but list/dump mode.
688 entry.port_type = port_type;
689 entry.targ_port = targ_port;
692 case CCTL_PORT_MODE_LIST:
693 cctl_port_dump(fd, quiet, xml, targ_port, port_type);
695 case CCTL_PORT_MODE_SET:
696 if (targ_port == -1) {
697 warnx("%s: -w and -W require -n", __func__);
703 entry.flags |= CTL_PORT_WWNN_VALID;
707 entry.flags |= CTL_PORT_WWPN_VALID;
711 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
712 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
717 case CCTL_PORT_MODE_ON:
718 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
719 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
723 fprintf(stdout, "Front End Ports enabled\n");
725 case CCTL_PORT_MODE_OFF:
726 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
727 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
731 fprintf(stdout, "Front End Ports disabled\n");
734 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
745 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
750 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
753 if (ioctl(fd, CTL_IO, io) == -1) {
754 warn("%s: error sending CTL_IO ioctl", func);
757 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
764 cctl_delay(int fd, int target, int lun, int argc, char **argv,
767 struct ctl_io_delay_info delay_info;
768 char *delayloc = NULL;
769 char *delaytype = NULL;
776 memset(&delay_info, 0, sizeof(delay_info));
778 while ((c = getopt(argc, argv, combinedopt)) != -1) {
781 delaytype = strdup(optarg);
784 delayloc = strdup(optarg);
787 delaytime = strtoul(optarg, NULL, 0);
792 if (delaytime == -1) {
793 warnx("%s: you must specify the delaytime with -t", __func__);
798 if (strcasecmp(delayloc, "datamove") == 0)
799 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
800 else if (strcasecmp(delayloc, "done") == 0)
801 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
803 warnx("%s: invalid delay location %s", __func__, delayloc);
808 if ((delaytype == NULL)
809 || (strcmp(delaytype, "oneshot") == 0))
810 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
811 else if (strcmp(delaytype, "cont") == 0)
812 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
814 warnx("%s: invalid delay type %s", __func__, delaytype);
819 delay_info.target_id = target;
820 delay_info.lun_id = lun;
821 delay_info.delay_secs = delaytime;
823 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
824 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
828 switch (delay_info.status) {
829 case CTL_DELAY_STATUS_NONE:
830 warnx("%s: no delay status??", __func__);
833 case CTL_DELAY_STATUS_OK:
835 case CTL_DELAY_STATUS_INVALID_LUN:
836 warnx("%s: invalid lun %d", __func__, lun);
839 case CTL_DELAY_STATUS_INVALID_TYPE:
840 warnx("%s: invalid delay type %d", __func__,
841 delay_info.delay_type);
844 case CTL_DELAY_STATUS_INVALID_LOC:
845 warnx("%s: delay location %s not implemented?", __func__,
849 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
850 warnx("%s: delay not implemented in the kernel", __func__);
851 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
855 warnx("%s: unknown delay return status %d", __func__,
862 /* delayloc should never be NULL, but just in case...*/
863 if (delayloc != NULL)
870 cctl_realsync(int fd, int argc, char **argv)
879 warnx("%s %s takes exactly one argument", argv[0], argv[1]);
886 if (strncasecmp(syncarg, "query", min(strlen(syncarg),
887 strlen("query"))) == 0) {
888 if (ioctl(fd, CTL_REALSYNC_GET, &syncstate) == -1) {
889 warn("%s: CTL_REALSYNC_GET ioctl failed", __func__);
893 fprintf(stdout, "SYNCHRONIZE CACHE support is: ");
896 fprintf(stdout, "OFF\n");
899 fprintf(stdout, "ON\n");
902 fprintf(stdout, "unknown (%d)\n", syncstate);
906 } else if (strcasecmp(syncarg, "on") == 0) {
908 } else if (strcasecmp(syncarg, "off") == 0) {
911 warnx("%s: invalid realsync argument %s", __func__, syncarg);
916 if (ioctl(fd, CTL_REALSYNC_SET, &syncstate) == -1) {
917 warn("%s: CTL_REALSYNC_SET ioctl failed", __func__);
926 cctl_getsetsync(int fd, int target, int lun, ctladm_cmdfunction command,
927 int argc, char **argv, char *combinedopt)
929 struct ctl_sync_info sync_info;
931 int sync_interval = -1;
937 memset(&sync_info, 0, sizeof(sync_info));
938 sync_info.target_id = target;
939 sync_info.lun_id = lun;
941 while ((c = getopt(argc, argv, combinedopt)) != -1) {
944 sync_interval = strtoul(optarg, NULL, 0);
951 if (command == CTLADM_CMD_SETSYNC) {
952 if (sync_interval == -1) {
953 warnx("%s: you must specify the sync interval with -i",
958 sync_info.sync_interval = sync_interval;
959 ioctl_cmd = CTL_SETSYNC;
961 ioctl_cmd = CTL_GETSYNC;
964 if (ioctl(fd, ioctl_cmd, &sync_info) == -1) {
965 warn("%s: CTL_%sSYNC ioctl failed", __func__,
966 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET");
971 switch (sync_info.status) {
973 if (command == CTLADM_CMD_GETSYNC) {
974 fprintf(stdout, "%d:%d: sync interval: %d\n",
975 target, lun, sync_info.sync_interval);
978 case CTL_GS_SYNC_NO_LUN:
979 warnx("%s: unknown target:LUN %d:%d", __func__, target, lun);
982 case CTL_GS_SYNC_NONE:
984 warnx("%s: unknown CTL_%sSYNC status %d", __func__,
985 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET",
994 struct ctladm_opts cctl_err_types[] = {
995 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
996 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
997 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
998 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
1003 struct ctladm_opts cctl_err_patterns[] = {
1004 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
1005 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
1006 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1007 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1008 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
1009 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
1010 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
1012 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
1018 cctl_error_inject(int fd, uint32_t target, uint32_t lun, int argc, char **argv,
1022 struct ctl_error_desc err_desc;
1025 uint64_t delete_id = 0;
1026 int delete_id_set = 0;
1032 bzero(&err_desc, sizeof(err_desc));
1033 err_desc.target_id = target;
1034 err_desc.lun_id = lun;
1036 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1042 delete_id = strtoull(optarg, NULL, 0);
1047 ctladm_optret optret;
1048 ctladm_cmdargs argnum;
1052 ctl_lun_error err_type;
1054 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
1055 warnx("%s: can't specify multiple -i "
1056 "arguments", __func__);
1060 optret = getoption(cctl_err_types, optarg,
1061 &err_type, &argnum, &subopt);
1062 err_desc.lun_error = err_type;
1064 ctl_lun_error_pattern pattern;
1066 optret = getoption(cctl_err_patterns, optarg,
1067 &pattern, &argnum, &subopt);
1068 err_desc.error_pattern |= pattern;
1071 if (optret == CC_OR_AMBIGUOUS) {
1072 warnx("%s: ambiguous argument %s", __func__,
1076 } else if (optret == CC_OR_NOT_FOUND) {
1077 warnx("%s: argument %s not found", __func__,
1085 char *tmpstr, *tmpstr2;
1087 tmpstr = strdup(optarg);
1088 if (tmpstr == NULL) {
1089 warn("%s: error duplicating string %s",
1095 tmpstr2 = strsep(&tmpstr, ",");
1096 if (tmpstr2 == NULL) {
1097 warnx("%s: invalid -r argument %s", __func__,
1103 lba = strtoull(tmpstr2, NULL, 0);
1104 tmpstr2 = strsep(&tmpstr, ",");
1105 if (tmpstr2 == NULL) {
1106 warnx("%s: no len argument for -r lba,len, got"
1107 " %s", __func__, optarg);
1112 len = strtoul(tmpstr2, NULL, 0);
1117 struct get_hook hook;
1120 sense_len = strtol(optarg, NULL, 0);
1121 if (sense_len <= 0) {
1122 warnx("invalid number of sense bytes %d",
1128 sense_len = MIN(sense_len, SSD_FULL_SIZE);
1130 hook.argc = argc - optind;
1131 hook.argv = argv + optind;
1134 sensestr = cget(&hook, NULL);
1135 if ((sensestr != NULL)
1136 && (sensestr[0] == '-')) {
1140 (uint8_t *)&err_desc.custom_sense,
1141 sense_len, sensestr, iget, &hook);
1151 if (delete_id_set != 0) {
1152 err_desc.serial = delete_id;
1153 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
1154 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
1161 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
1162 warnx("%s: error injection command (-i) needed",
1166 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
1167 && (sense_len == 0)) {
1168 warnx("%s: custom error requires -s", __func__);
1173 if (continuous != 0)
1174 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
1177 * If fd_sense is set, we need to read the sense data the user
1178 * wants returned from stdin.
1180 if (fd_sense == 1) {
1182 int amt_to_read = sense_len;
1183 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
1185 for (amt_read = 0; amt_to_read > 0;
1186 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1187 if (amt_read == -1) {
1188 warn("error reading sense data from stdin");
1192 amt_to_read -= amt_read;
1193 buf_ptr += amt_read;
1197 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1198 warnx("%s: command pattern (-p) needed", __func__);
1204 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1206 * We could check here to see whether it's a read/write
1207 * command, but that will be pointless once we allow
1208 * custom patterns. At that point, the user could specify
1209 * a READ(6) CDB type, and we wouldn't have an easy way here
1210 * to verify whether range checking is possible there. The
1211 * user will just figure it out when his error never gets
1215 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1216 warnx("%s: need read and/or write pattern if range "
1217 "is specified", __func__);
1222 err_desc.lba_range.lba = lba;
1223 err_desc.lba_range.len = len;
1226 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1227 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1230 printf("Error injection succeeded, serial number is %ju\n",
1231 (uintmax_t)err_desc.serial);
1239 cctl_lunlist(int fd)
1241 struct scsi_report_luns_data *lun_data;
1242 struct scsi_inquiry_data *inq_data;
1256 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1257 * if we ever acquire the ability to have multiple targets.
1259 if ((retval = cctl_get_luns(fd, target, /*lun*/ 0, initid,
1260 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1263 inq_data = malloc(sizeof(*inq_data));
1264 if (inq_data == NULL) {
1265 warn("%s: couldn't allocate memory for inquiry data\n",
1270 for (i = 0; i < num_luns; i++) {
1274 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1275 case RPL_LUNDATA_ATYP_PERIPH:
1276 lun_val = lun_data->luns[i].lundata[1];
1278 case RPL_LUNDATA_ATYP_FLAT:
1279 lun_val = (lun_data->luns[i].lundata[0] &
1280 RPL_LUNDATA_FLAT_LUN_MASK) |
1281 (lun_data->luns[i].lundata[1] <<
1282 RPL_LUNDATA_FLAT_LUN_BITS);
1284 case RPL_LUNDATA_ATYP_LUN:
1285 case RPL_LUNDATA_ATYP_EXTLUN:
1287 fprintf(stdout, "Unsupported LUN format %d\n",
1288 lun_data->luns[i].lundata[0] &
1289 RPL_LUNDATA_ATYP_MASK);
1296 if ((retval = cctl_get_inquiry(fd, target, lun_val, initid,
1297 /*retries*/ 2, scsi_path,
1302 printf("%s", scsi_path);
1303 scsi_print_inquiry(inq_data);
1307 if (lun_data != NULL)
1310 if (inq_data != NULL)
1317 cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len)
1320 case CFI_MT_PORT_OFFLINE:
1321 snprintf(str, str_len, "Port Offline");
1324 snprintf(str, str_len, "Error");
1326 case CFI_MT_SUCCESS:
1327 snprintf(str, str_len, "Success");
1330 snprintf(str, str_len, "None??");
1333 snprintf(str, str_len, "Unknown status: %d", status);
1339 cctl_cfi_bbr_statusstr(cfi_bbrread_status status, char *str, int str_len)
1342 case CFI_BBR_SUCCESS:
1343 snprintf(str, str_len, "Success");
1345 case CFI_BBR_LUN_UNCONFIG:
1346 snprintf(str, str_len, "LUN not configured");
1348 case CFI_BBR_NO_LUN:
1349 snprintf(str, str_len, "LUN does not exist");
1351 case CFI_BBR_NO_MEM:
1352 snprintf(str, str_len, "Memory allocation error");
1354 case CFI_BBR_BAD_LEN:
1355 snprintf(str, str_len, "Length is not a multiple of blocksize");
1357 case CFI_BBR_RESERV_CONFLICT:
1358 snprintf(str, str_len, "Reservation conflict");
1360 case CFI_BBR_LUN_STOPPED:
1361 snprintf(str, str_len, "LUN is powered off");
1363 case CFI_BBR_LUN_OFFLINE_CTL:
1364 snprintf(str, str_len, "LUN is offline");
1366 case CFI_BBR_LUN_OFFLINE_RC:
1367 snprintf(str, str_len, "RAIDCore array is offline (double "
1370 case CFI_BBR_SCSI_ERROR:
1371 snprintf(str, str_len, "SCSI Error");
1374 snprintf(str, str_len, "Error");
1377 snprintf(str, str_len, "Unknown status: %d", status);
1383 cctl_hardstopstart(int fd, ctladm_cmdfunction command)
1385 struct ctl_hard_startstop_info hs_info;
1386 char error_str[256];
1392 if (command == CTLADM_CMD_HARDSTART)
1397 if (ioctl(fd, (do_start == 1) ? CTL_HARD_START : CTL_HARD_STOP,
1399 warn("%s: CTL_HARD_%s ioctl failed", __func__,
1400 (do_start == 1) ? "START" : "STOP");
1405 fprintf(stdout, "Hard %s Status: ", (command == CTLADM_CMD_HARDSTOP) ?
1407 cctl_cfi_mt_statusstr(hs_info.status, error_str, sizeof(error_str));
1408 fprintf(stdout, "%s\n", error_str);
1409 fprintf(stdout, "Total LUNs: %d\n", hs_info.total_luns);
1410 fprintf(stdout, "LUNs complete: %d\n", hs_info.luns_complete);
1411 fprintf(stdout, "LUNs failed: %d\n", hs_info.luns_failed);
1418 cctl_bbrread(int fd, int target __unused, int lun, int iid __unused,
1419 int argc, char **argv, char *combinedopt)
1421 struct ctl_bbrread_info bbr_info;
1422 char error_str[256];
1431 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1434 datalen = strtoul(optarg, NULL, 0);
1437 lba = strtoull(optarg, NULL, 0);
1446 warnx("%s: you must specify an LBA with -l", __func__);
1451 if (datalen == -1) {
1452 warnx("%s: you must specify a length with -d", __func__);
1457 bbr_info.lun_num = lun;
1460 * XXX KDM get the blocksize first??
1462 if ((datalen % 512) != 0) {
1463 warnx("%s: data length %d is not a multiple of 512 bytes",
1468 bbr_info.len = datalen;
1470 if (ioctl(fd, CTL_BBRREAD, &bbr_info) == -1) {
1471 warn("%s: CTL_BBRREAD ioctl failed", __func__);
1475 cctl_cfi_mt_statusstr(bbr_info.status, error_str, sizeof(error_str));
1476 fprintf(stdout, "BBR Read Overall Status: %s\n", error_str);
1477 cctl_cfi_bbr_statusstr(bbr_info.bbr_status, error_str,
1479 fprintf(stdout, "BBR Read Status: %s\n", error_str);
1481 * XXX KDM should we bother printing out SCSI status if we get
1482 * CFI_BBR_SCSI_ERROR back?
1484 * Return non-zero if this fails?
1491 cctl_startup_shutdown(int fd, int target, int lun, int iid,
1492 ctladm_cmdfunction command)
1496 struct scsi_report_luns_data *lun_data;
1497 struct scsi_inquiry_data *inq_data;
1507 * - step through each lun, do an inquiry
1508 * - check OOA queue on direct access luns
1509 * - send stop with offline bit to each direct access device with a
1511 * - if we get a reservation conflict, reset the LUN to clear it
1512 * and reissue the stop with the offline bit set
1517 io = ctl_scsi_alloc_io(id);
1519 warnx("%s: can't allocate memory", __func__);
1523 if ((retval = cctl_get_luns(fd, target, lun, iid, /*retries*/ 2,
1524 &lun_data, &num_luns)) != 0)
1527 inq_data = malloc(sizeof(*inq_data));
1528 if (inq_data == NULL) {
1529 warn("%s: couldn't allocate memory for inquiry data\n",
1534 for (i = 0; i < num_luns; i++) {
1539 * XXX KDM figure out a way to share this code with
1542 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1543 case RPL_LUNDATA_ATYP_PERIPH:
1544 lun_val = lun_data->luns[i].lundata[1];
1546 case RPL_LUNDATA_ATYP_FLAT:
1547 lun_val = (lun_data->luns[i].lundata[0] &
1548 RPL_LUNDATA_FLAT_LUN_MASK) |
1549 (lun_data->luns[i].lundata[1] <<
1550 RPL_LUNDATA_FLAT_LUN_BITS);
1552 case RPL_LUNDATA_ATYP_LUN:
1553 case RPL_LUNDATA_ATYP_EXTLUN:
1555 fprintf(stdout, "Unsupported LUN format %d\n",
1556 lun_data->luns[i].lundata[0] &
1557 RPL_LUNDATA_ATYP_MASK);
1564 if ((retval = cctl_get_inquiry(fd, target, lun_val, iid,
1565 /*retries*/ 2, scsi_path,
1570 printf("%s", scsi_path);
1571 scsi_print_inquiry(inq_data);
1573 * We only want to shutdown direct access devices.
1575 if (SID_TYPE(inq_data) != T_DIRECT) {
1576 printf("%s LUN is not direct access, skipped\n",
1581 if (command == CTLADM_CMD_SHUTDOWN) {
1582 struct ctl_ooa_info ooa_info;
1584 ooa_info.target_id = target;
1585 ooa_info.lun_id = lun_val;
1587 if (ioctl(fd, CTL_CHECK_OOA, &ooa_info) == -1) {
1588 printf("%s CTL_CHECK_OOA ioctl failed\n",
1593 if (ooa_info.status != CTL_OOA_SUCCESS) {
1594 printf("%s CTL_CHECK_OOA returned status %d\n",
1595 scsi_path, ooa_info.status);
1598 if (ooa_info.num_entries != 0) {
1599 printf("%s %d entr%s in the OOA queue, "
1600 "skipping shutdown\n", scsi_path,
1601 ooa_info.num_entries,
1602 (ooa_info.num_entries > 1)?"ies" : "y" );
1607 ctl_scsi_start_stop(/*io*/ io,
1608 /*start*/(command == CTLADM_CMD_STARTUP) ?
1612 /*power_conditions*/ SSS_PC_START_VALID,
1615 (command == CTLADM_CMD_STARTUP) ?
1616 CTL_TAG_SIMPLE :CTL_TAG_ORDERED,
1619 io->io_hdr.nexus.targ_target.id = target;
1620 io->io_hdr.nexus.targ_lun = lun_val;
1621 io->io_hdr.nexus.initid = id;
1623 if (cctl_do_io(fd, /*retries*/ 3, io, __func__) != 0) {
1628 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1629 ctl_io_error_print(io, inq_data, stderr);
1631 printf("%s LUN is now %s\n", scsi_path,
1632 (command == CTLADM_CMD_STARTUP) ? "online" :
1637 if (lun_data != NULL)
1640 if (inq_data != NULL)
1644 ctl_scsi_free_io(io);
1650 cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
1651 int argc, char **argv, char *combinedopt)
1657 uint64_t our_lba = 0;
1658 uint32_t our_block_count = 0;
1659 int reladr = 0, immed = 0;
1665 io = ctl_scsi_alloc_io(id);
1667 warnx("%s: can't allocate memory", __func__);
1671 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1674 our_block_count = strtoul(optarg, NULL, 0);
1677 cdb_size = strtol(optarg, NULL, 0);
1683 our_lba = strtoull(optarg, NULL, 0);
1693 if (cdb_size != -1) {
1699 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1700 "and 16", __func__, cdb_size);
1703 break; /* NOTREACHED */
1708 ctl_scsi_sync_cache(/*io*/ io,
1711 /*minimum_cdb_size*/ cdb_size,
1712 /*starting_lba*/ our_lba,
1713 /*block_count*/ our_block_count,
1714 /*tag_type*/ CTL_TAG_SIMPLE,
1717 io->io_hdr.nexus.targ_target.id = target;
1718 io->io_hdr.nexus.targ_lun = lun;
1719 io->io_hdr.nexus.initid = id;
1721 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1726 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1727 fprintf(stdout, "Cache synchronized successfully\n");
1729 ctl_io_error_print(io, NULL, stderr);
1731 ctl_scsi_free_io(io);
1737 cctl_start_stop(int fd, int target, int lun, int iid, int retries, int start,
1738 int argc, char **argv, char *combinedopt)
1743 int immed = 0, onoffline = 0;
1749 io = ctl_scsi_alloc_io(id);
1751 warnx("%s: can't allocate memory", __func__);
1755 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1768 * Use an ordered tag for the stop command, to guarantee that any
1769 * pending I/O will finish before the stop command executes. This
1770 * would normally be the case anyway, since CTL will basically
1771 * treat the start/stop command as an ordered command with respect
1772 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1774 ctl_scsi_start_stop(/*io*/ io,
1777 /*immediate*/ immed,
1778 /*power_conditions*/ SSS_PC_START_VALID,
1779 /*onoffline*/ onoffline,
1780 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1784 io->io_hdr.nexus.targ_target.id = target;
1785 io->io_hdr.nexus.targ_lun = lun;
1786 io->io_hdr.nexus.initid = id;
1788 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1793 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1794 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1795 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1796 (start) ? "started" : "stopped");
1798 ctl_io_error_print(io, NULL, stderr);
1801 ctl_scsi_free_io(io);
1807 cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
1808 int argc, char **argv, char *combinedopt)
1814 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1824 io = ctl_scsi_alloc_io(id);
1826 warn("%s: can't allocate memory", __func__);
1830 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1833 pc = strtoul(optarg, NULL, 0);
1836 subpage = strtoul(optarg, NULL, 0);
1845 page_code = strtoul(optarg, NULL, 0);
1848 cdbsize = strtol(optarg, NULL, 0);
1855 if (((list == 0) && (page_code == -1))
1856 || ((list != 0) && (page_code != -1))) {
1857 warnx("%s: you must specify either a page code (-m) or -l",
1863 if ((page_code != -1)
1864 && ((page_code > SMS_ALL_PAGES_PAGE)
1865 || (page_code < 0))) {
1866 warnx("%s: page code %d is out of range", __func__,
1873 page_code = SMS_ALL_PAGES_PAGE;
1875 warnx("%s: arg -P makes no sense with -l",
1880 if (subpage != -1) {
1881 warnx("%s: arg -S makes no sense with -l", __func__);
1888 pc = SMS_PAGE_CTRL_CURRENT;
1892 warnx("%s: page control value %d is out of range: 0-3",
1902 || (subpage < 0))) {
1903 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1914 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1915 "and 10", __func__, cdbsize);
1931 dataptr = (uint8_t *)malloc(datalen);
1932 if (dataptr == NULL) {
1933 warn("%s: can't allocate %d bytes", __func__, datalen);
1938 memset(dataptr, 0, datalen);
1940 ctl_scsi_mode_sense(io,
1941 /*data_ptr*/ dataptr,
1942 /*data_len*/ datalen,
1945 /*page_code*/ page_code,
1947 /*subpage*/ subpage,
1948 /*minimum_cdb_size*/ cdbsize,
1949 /*tag_type*/ CTL_TAG_SIMPLE,
1952 io->io_hdr.nexus.targ_target.id = target;
1953 io->io_hdr.nexus.targ_lun = lun;
1954 io->io_hdr.nexus.initid = id;
1956 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1961 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1962 int pages_len, used_len;
1963 uint32_t returned_len;
1966 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1967 struct scsi_mode_hdr_6 *hdr6;
1970 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1972 returned_len = hdr6->datalen + 1;
1973 bdlen = hdr6->block_descr_len;
1975 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1977 struct scsi_mode_hdr_10 *hdr10;
1980 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
1982 returned_len = scsi_2btoul(hdr10->datalen) + 2;
1983 bdlen = scsi_2btoul(hdr10->block_descr_len);
1985 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
1987 /* just in case they can give us more than we allocated for */
1988 returned_len = min(returned_len, datalen);
1989 pages_len = returned_len - (ndataptr - dataptr);
1991 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
1992 returned_len, pages_len);
1995 fprintf(stdout, "Supported mode pages:\n");
1996 for (used_len = 0; used_len < pages_len;) {
1997 struct scsi_mode_page_header *header;
1999 header = (struct scsi_mode_page_header *)
2000 &ndataptr[used_len];
2001 fprintf(stdout, "%d\n", header->page_code);
2002 used_len += header->page_length + 2;
2005 for (used_len = 0; used_len < pages_len; used_len++) {
2006 fprintf(stdout, "0x%x ", ndataptr[used_len]);
2007 if (((used_len+1) % 16) == 0)
2008 fprintf(stdout, "\n");
2010 fprintf(stdout, "\n");
2013 ctl_io_error_print(io, NULL, stderr);
2016 ctl_scsi_free_io(io);
2018 if (dataptr != NULL)
2025 cctl_read_capacity(int fd, int target, int lun, int iid, int retries,
2026 int argc, char **argv, char *combinedopt)
2030 struct scsi_read_capacity_data *data;
2031 struct scsi_read_capacity_data_long *longdata;
2032 int cdbsize = -1, retval;
2041 io = ctl_scsi_alloc_io(id);
2043 warn("%s: can't allocate memory\n", __func__);
2047 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2050 cdbsize = strtol(optarg, NULL, 0);
2056 if (cdbsize != -1) {
2062 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
2063 "and 16", __func__, cdbsize);
2066 break; /* NOTREACHED */
2071 dataptr = (uint8_t *)malloc(sizeof(*longdata));
2072 if (dataptr == NULL) {
2073 warn("%s: can't allocate %zd bytes\n", __func__,
2078 memset(dataptr, 0, sizeof(*longdata));
2084 ctl_scsi_read_capacity(io,
2085 /*data_ptr*/ dataptr,
2086 /*data_len*/ sizeof(*longdata),
2090 /*tag_type*/ CTL_TAG_SIMPLE,
2094 ctl_scsi_read_capacity_16(io,
2095 /*data_ptr*/ dataptr,
2096 /*data_len*/ sizeof(*longdata),
2100 /*tag_type*/ CTL_TAG_SIMPLE,
2105 io->io_hdr.nexus.initid = id;
2106 io->io_hdr.nexus.targ_target.id = target;
2107 io->io_hdr.nexus.targ_lun = lun;
2109 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2114 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2118 if (cdbsize == 10) {
2120 data = (struct scsi_read_capacity_data *)dataptr;
2122 maxlba = scsi_4btoul(data->addr);
2123 blocksize = scsi_4btoul(data->length);
2125 if (maxlba == 0xffffffff) {
2130 longdata=(struct scsi_read_capacity_data_long *)dataptr;
2132 maxlba = scsi_8btou64(longdata->addr);
2133 blocksize = scsi_4btoul(longdata->length);
2136 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
2137 (uintmax_t)maxlba, blocksize);
2139 ctl_io_error_print(io, NULL, stderr);
2142 ctl_scsi_free_io(io);
2144 if (dataptr != NULL)
2151 cctl_read_write(int fd, int target, int lun, int iid, int retries,
2152 int argc, char **argv, char *combinedopt,
2153 ctladm_cmdfunction command)
2157 int file_fd, do_stdio;
2158 int cdbsize = -1, databytes;
2160 char *filename = NULL;
2161 int datalen = -1, blocksize = -1;
2173 io = ctl_scsi_alloc_io(id);
2175 warn("%s: can't allocate memory\n", __func__);
2179 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2182 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
2185 blocksize = strtoul(optarg, NULL, 0);
2188 cdbsize = strtoul(optarg, NULL, 0);
2191 datalen = strtoul(optarg, NULL, 0);
2194 filename = strdup(optarg);
2197 lba = strtoull(optarg, NULL, 0);
2204 if (filename == NULL) {
2205 warnx("%s: you must supply a filename using -f", __func__);
2210 if (datalen == -1) {
2211 warnx("%s: you must specify the data length with -d", __func__);
2217 warnx("%s: you must specify the LBA with -l", __func__);
2222 if (blocksize == -1) {
2223 warnx("%s: you must specify the blocksize with -b", __func__);
2228 if (cdbsize != -1) {
2236 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
2237 "10, 12 or 16", __func__, cdbsize);
2240 break; /* NOTREACHED */
2245 databytes = datalen * blocksize;
2246 dataptr = (uint8_t *)malloc(databytes);
2248 if (dataptr == NULL) {
2249 warn("%s: can't allocate %d bytes\n", __func__, databytes);
2253 if (strcmp(filename, "-") == 0) {
2254 if (command == CTLADM_CMD_READ)
2255 file_fd = STDOUT_FILENO;
2257 file_fd = STDIN_FILENO;
2260 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
2261 if (file_fd == -1) {
2262 warn("%s: can't open file %s", __func__, filename);
2268 memset(dataptr, 0, databytes);
2270 if (command == CTLADM_CMD_WRITE) {
2273 bytes_read = read(file_fd, dataptr, databytes);
2274 if (bytes_read == -1) {
2275 warn("%s: error reading file %s", __func__, filename);
2279 if (bytes_read != databytes) {
2280 warnx("%s: only read %d bytes from file %s",
2281 __func__, bytes_read, filename);
2286 ctl_scsi_read_write(io,
2287 /*data_ptr*/ dataptr,
2288 /*data_len*/ databytes,
2289 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
2291 /*minimum_cdb_size*/ cdbsize,
2293 /*num_blocks*/ datalen,
2294 /*tag_type*/ CTL_TAG_SIMPLE,
2297 io->io_hdr.nexus.targ_target.id = target;
2298 io->io_hdr.nexus.targ_lun = lun;
2299 io->io_hdr.nexus.initid = id;
2301 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2306 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2307 && (command == CTLADM_CMD_READ)) {
2310 bytes_written = write(file_fd, dataptr, databytes);
2311 if (bytes_written == -1) {
2312 warn("%s: can't write to %s", __func__, filename);
2315 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
2316 ctl_io_error_print(io, NULL, stderr);
2321 ctl_scsi_free_io(io);
2323 if (dataptr != NULL)
2334 cctl_get_luns(int fd, int target, int lun, int iid, int retries, struct
2335 scsi_report_luns_data **lun_data, uint32_t *num_luns)
2346 io = ctl_scsi_alloc_io(id);
2348 warnx("%s: can't allocate memory", __func__);
2353 * lun_data includes space for 1 lun, allocate space for 4 initially.
2354 * If that isn't enough, we'll allocate more.
2358 lun_datalen = sizeof(*lun_data) +
2359 (nluns * sizeof(struct scsi_report_luns_lundata));
2360 *lun_data = malloc(lun_datalen);
2362 if (*lun_data == NULL) {
2363 warnx("%s: can't allocate memory", __func__);
2364 ctl_scsi_free_io(io);
2368 ctl_scsi_report_luns(io,
2369 /*data_ptr*/ (uint8_t *)*lun_data,
2370 /*data_len*/ lun_datalen,
2371 /*select_report*/ RPL_REPORT_ALL,
2372 /*tag_type*/ CTL_TAG_SIMPLE,
2375 io->io_hdr.nexus.initid = id;
2376 io->io_hdr.nexus.targ_target.id = target;
2377 io->io_hdr.nexus.targ_lun = lun;
2379 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2384 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2385 uint32_t returned_len, returned_luns;
2387 returned_len = scsi_4btoul((*lun_data)->length);
2388 returned_luns = returned_len / 8;
2389 if (returned_luns > nluns) {
2390 nluns = returned_luns;
2394 /* These should be the same */
2395 *num_luns = MIN(returned_luns, nluns);
2397 ctl_io_error_print(io, NULL, stderr);
2401 ctl_scsi_free_io(io);
2407 cctl_report_luns(int fd, int target, int lun, int iid, int retries)
2409 struct scsi_report_luns_data *lun_data;
2410 uint32_t num_luns, i;
2415 if ((retval = cctl_get_luns(fd, target, lun, iid, retries, &lun_data,
2419 fprintf(stdout, "%u LUNs returned\n", num_luns);
2420 for (i = 0; i < num_luns; i++) {
2424 * XXX KDM figure out a way to share this code with
2427 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
2428 case RPL_LUNDATA_ATYP_PERIPH:
2429 lun_val = lun_data->luns[i].lundata[1];
2431 case RPL_LUNDATA_ATYP_FLAT:
2432 lun_val = (lun_data->luns[i].lundata[0] &
2433 RPL_LUNDATA_FLAT_LUN_MASK) |
2434 (lun_data->luns[i].lundata[1] <<
2435 RPL_LUNDATA_FLAT_LUN_BITS);
2437 case RPL_LUNDATA_ATYP_LUN:
2438 case RPL_LUNDATA_ATYP_EXTLUN:
2440 fprintf(stdout, "Unsupported LUN format %d\n",
2441 lun_data->luns[i].lundata[0] &
2442 RPL_LUNDATA_ATYP_MASK);
2449 fprintf(stdout, "%d\n", lun_val);
2453 if (lun_data != NULL)
2460 cctl_tur(int fd, int target, int lun, int iid, int retries)
2467 io = ctl_scsi_alloc_io(id);
2469 fprintf(stderr, "can't allocate memory\n");
2474 /* tag_type */ CTL_TAG_SIMPLE,
2477 io->io_hdr.nexus.targ_target.id = target;
2478 io->io_hdr.nexus.targ_lun = lun;
2479 io->io_hdr.nexus.initid = id;
2481 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2482 ctl_scsi_free_io(io);
2486 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2487 fprintf(stdout, "Unit is ready\n");
2489 ctl_io_error_print(io, NULL, stderr);
2495 cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
2496 char *path_str, int path_len,
2497 struct scsi_inquiry_data *inq_data)
2507 io = ctl_scsi_alloc_io(id);
2509 warnx("cctl_inquiry: can't allocate memory\n");
2513 ctl_scsi_inquiry(/*io*/ io,
2514 /*data_ptr*/ (uint8_t *)inq_data,
2515 /*data_len*/ sizeof(*inq_data),
2518 /*tag_type*/ CTL_TAG_SIMPLE,
2521 io->io_hdr.nexus.targ_target.id = target;
2522 io->io_hdr.nexus.targ_lun = lun;
2523 io->io_hdr.nexus.initid = id;
2525 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2530 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
2532 ctl_io_error_print(io, NULL, stderr);
2533 } else if (path_str != NULL)
2534 ctl_scsi_path_string(io, path_str, path_len);
2537 ctl_scsi_free_io(io);
2543 cctl_inquiry(int fd, int target, int lun, int iid, int retries)
2545 struct scsi_inquiry_data *inq_data;
2551 inq_data = malloc(sizeof(*inq_data));
2552 if (inq_data == NULL) {
2553 warnx("%s: can't allocate inquiry data", __func__);
2558 if ((retval = cctl_get_inquiry(fd, target, lun, iid, retries, scsi_path,
2559 sizeof(scsi_path), inq_data)) != 0)
2562 printf("%s", scsi_path);
2563 scsi_print_inquiry(inq_data);
2566 if (inq_data != NULL)
2573 cctl_req_sense(int fd, int target, int lun, int iid, int retries)
2576 struct scsi_sense_data *sense_data;
2584 io = ctl_scsi_alloc_io(id);
2586 warnx("cctl_req_sense: can't allocate memory\n");
2589 sense_data = malloc(sizeof(*sense_data));
2590 memset(sense_data, 0, sizeof(*sense_data));
2592 ctl_scsi_request_sense(/*io*/ io,
2593 /*data_ptr*/ (uint8_t *)sense_data,
2594 /*data_len*/ sizeof(*sense_data),
2596 /*tag_type*/ CTL_TAG_SIMPLE,
2599 io->io_hdr.nexus.targ_target.id = target;
2600 io->io_hdr.nexus.targ_lun = lun;
2601 io->io_hdr.nexus.initid = id;
2603 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2608 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2609 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2610 io->scsiio.sense_len = sizeof(*sense_data);
2611 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2613 ctl_io_error_print(io, NULL, stderr);
2617 ctl_scsi_free_io(io);
2624 cctl_report_target_port_group(int fd, int target, int lun, int initiator)
2636 io = ctl_scsi_alloc_io(id);
2638 warn("%s: can't allocate memory", __func__);
2643 dataptr = (uint8_t *)malloc(datalen);
2644 if (dataptr == NULL) {
2645 warn("%s: can't allocate %d bytes", __func__, datalen);
2650 memset(dataptr, 0, datalen);
2652 ctl_scsi_maintenance_in(/*io*/ io,
2653 /*data_ptr*/ dataptr,
2654 /*data_len*/ datalen,
2655 /*action*/ SA_RPRT_TRGT_GRP,
2656 /*tag_type*/ CTL_TAG_SIMPLE,
2659 io->io_hdr.nexus.targ_target.id = target;
2660 io->io_hdr.nexus.targ_lun = lun;
2661 io->io_hdr.nexus.initid = id;
2663 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2668 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2669 int returned_len, used_len;
2671 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2673 for (used_len = 0; used_len < returned_len; used_len++) {
2674 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2675 if (((used_len+1) % 8) == 0)
2676 fprintf(stdout, "\n");
2678 fprintf(stdout, "\n");
2680 ctl_io_error_print(io, NULL, stderr);
2683 ctl_scsi_free_io(io);
2685 if (dataptr != NULL)
2692 cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator)
2704 io = ctl_scsi_alloc_io(id);
2706 warn("%s: can't allocate memory", __func__);
2711 dataptr = (uint8_t *)malloc(datalen);
2712 if (dataptr == NULL) {
2713 warn("%s: can't allocate %d bytes", __func__, datalen);
2718 memset(dataptr, 0, datalen);
2720 ctl_scsi_inquiry(/*io*/ io,
2721 /*data_ptr*/ dataptr,
2722 /*data_len*/ datalen,
2724 /*page_code*/ SVPD_DEVICE_ID,
2725 /*tag_type*/ CTL_TAG_SIMPLE,
2728 io->io_hdr.nexus.targ_target.id = target;
2729 io->io_hdr.nexus.targ_lun = lun;
2730 io->io_hdr.nexus.initid = id;
2732 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2737 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2738 int returned_len, used_len;
2740 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2742 for (used_len = 0; used_len < returned_len; used_len++) {
2743 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2744 if (((used_len+1) % 8) == 0)
2745 fprintf(stdout, "\n");
2747 fprintf(stdout, "\n");
2749 ctl_io_error_print(io, NULL, stderr);
2752 ctl_scsi_free_io(io);
2754 if (dataptr != NULL)
2761 cctl_persistent_reserve_in(int fd, int target, int lun, int initiator,
2762 int argc, char **argv, char *combinedopt,
2777 io = ctl_scsi_alloc_io(id);
2779 warn("%s: can't allocate memory", __func__);
2783 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2786 action = strtol(optarg, NULL, 0);
2793 if (action < 0 || action > 2) {
2794 warn("action must be specified and in the range: 0-2");
2801 dataptr = (uint8_t *)malloc(datalen);
2802 if (dataptr == NULL) {
2803 warn("%s: can't allocate %d bytes", __func__, datalen);
2808 memset(dataptr, 0, datalen);
2810 ctl_scsi_persistent_res_in(io,
2811 /*data_ptr*/ dataptr,
2812 /*data_len*/ datalen,
2814 /*tag_type*/ CTL_TAG_SIMPLE,
2817 io->io_hdr.nexus.targ_target.id = target;
2818 io->io_hdr.nexus.targ_lun = lun;
2819 io->io_hdr.nexus.initid = id;
2821 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2826 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2827 int returned_len, used_len;
2833 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2834 returned_len = min(returned_len, 256);
2837 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2843 warnx("%s: invalid action %d", __func__, action);
2845 break; /* NOTREACHED */
2848 for (used_len = 0; used_len < returned_len; used_len++) {
2849 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2850 if (((used_len+1) % 8) == 0)
2851 fprintf(stdout, "\n");
2853 fprintf(stdout, "\n");
2855 ctl_io_error_print(io, NULL, stderr);
2858 ctl_scsi_free_io(io);
2860 if (dataptr != NULL)
2867 cctl_persistent_reserve_out(int fd, int target, int lun, int initiator,
2868 int argc, char **argv, char *combinedopt,
2874 uint64_t key = 0, sa_key = 0;
2875 int action = -1, restype = -1;
2884 io = ctl_scsi_alloc_io(id);
2886 warn("%s: can't allocate memory", __func__);
2890 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2893 action = strtol(optarg, NULL, 0);
2896 key = strtoull(optarg, NULL, 0);
2899 restype = strtol(optarg, NULL, 0);
2902 sa_key = strtoull(optarg, NULL, 0);
2908 if (action < 0 || action > 5) {
2909 warn("action must be specified and in the range: 0-5");
2914 if (restype < 0 || restype > 5) {
2915 if (action != 0 && action != 5 && action != 3) {
2916 warn("'restype' must specified and in the range: 0-5");
2923 dataptr = (uint8_t *)malloc(datalen);
2924 if (dataptr == NULL) {
2925 warn("%s: can't allocate %d bytes", __func__, datalen);
2930 memset(dataptr, 0, datalen);
2932 ctl_scsi_persistent_res_out(io,
2933 /*data_ptr*/ dataptr,
2934 /*data_len*/ datalen,
2939 /*tag_type*/ CTL_TAG_SIMPLE,
2942 io->io_hdr.nexus.targ_target.id = target;
2943 io->io_hdr.nexus.targ_lun = lun;
2944 io->io_hdr.nexus.initid = id;
2946 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2950 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2952 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2953 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2954 "successfully\n", scsi_path);
2956 ctl_io_error_print(io, NULL, stderr);
2959 ctl_scsi_free_io(io);
2961 if (dataptr != NULL)
2967 struct cctl_req_option {
2972 STAILQ_ENTRY(cctl_req_option) links;
2976 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2978 struct ctl_lun_req req;
2979 int device_type = -1;
2980 uint64_t lun_size = 0;
2981 uint32_t blocksize = 0, req_lun_id = 0;
2982 char *serial_num = NULL;
2983 char *device_id = NULL;
2984 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
2985 char *backend_name = NULL;
2986 STAILQ_HEAD(, cctl_req_option) option_list;
2987 int num_options = 0;
2990 STAILQ_INIT(&option_list);
2992 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2995 backend_name = strdup(optarg);
2998 blocksize = strtoul(optarg, NULL, 0);
3002 device_id = strdup(optarg);
3005 req_lun_id = strtoul(optarg, NULL, 0);
3009 struct cctl_req_option *option;
3013 tmpstr = strdup(optarg);
3014 name = strsep(&tmpstr, "=");
3016 warnx("%s: option -o takes \"name=value\""
3017 "argument", __func__);
3021 value = strsep(&tmpstr, "=");
3022 if (value == NULL) {
3023 warnx("%s: option -o takes \"name=value\""
3024 "argument", __func__);
3028 option = malloc(sizeof(*option));
3029 if (option == NULL) {
3030 warn("%s: error allocating %zd bytes",
3031 __func__, sizeof(*option));
3035 option->name = strdup(name);
3036 option->namelen = strlen(name) + 1;
3037 option->value = strdup(value);
3038 option->vallen = strlen(value) + 1;
3041 STAILQ_INSERT_TAIL(&option_list, option, links);
3046 lun_size = strtoull(optarg, NULL, 0);
3050 serial_num = strdup(optarg);
3053 device_type = strtoul(optarg, NULL, 0);
3060 if (backend_name == NULL) {
3061 warnx("%s: backend name (-b) must be specified", __func__);
3066 bzero(&req, sizeof(req));
3068 strlcpy(req.backend, backend_name, sizeof(req.backend));
3069 req.reqtype = CTL_LUNREQ_CREATE;
3071 if (blocksize_set != 0)
3072 req.reqdata.create.blocksize_bytes = blocksize;
3074 if (lun_size_set != 0)
3075 req.reqdata.create.lun_size_bytes = lun_size;
3077 if (lun_id_set != 0) {
3078 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
3079 req.reqdata.create.req_lun_id = req_lun_id;
3082 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
3084 if (device_type != -1)
3085 req.reqdata.create.device_type = device_type;
3087 req.reqdata.create.device_type = T_DIRECT;
3089 if (serial_num != NULL) {
3090 strlcpy(req.reqdata.create.serial_num, serial_num,
3091 sizeof(req.reqdata.create.serial_num));
3092 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
3095 if (device_id != NULL) {
3096 strlcpy(req.reqdata.create.device_id, device_id,
3097 sizeof(req.reqdata.create.device_id));
3098 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
3101 req.num_be_args = num_options;
3102 if (num_options > 0) {
3103 struct cctl_req_option *option, *next_option;
3106 req.be_args = malloc(num_options * sizeof(*req.be_args));
3107 if (req.be_args == NULL) {
3108 warn("%s: error allocating %zd bytes", __func__,
3109 num_options * sizeof(*req.be_args));
3114 for (i = 0, option = STAILQ_FIRST(&option_list);
3115 i < num_options; i++, option = next_option) {
3116 next_option = STAILQ_NEXT(option, links);
3118 req.be_args[i].namelen = option->namelen;
3119 req.be_args[i].name = strdup(option->name);
3120 req.be_args[i].vallen = option->vallen;
3121 req.be_args[i].value = strdup(option->value);
3123 * XXX KDM do we want a way to specify a writeable
3124 * flag of some sort? Do we want a way to specify
3127 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3129 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3132 free(option->value);
3137 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3138 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3143 if (req.status == CTL_LUN_ERROR) {
3144 warnx("%s: error returned from LUN creation request:\n%s",
3145 __func__, req.error_str);
3148 } else if (req.status != CTL_LUN_OK) {
3149 warnx("%s: unknown LUN creation request status %d",
3150 __func__, req.status);
3155 fprintf(stdout, "LUN created successfully\n");
3156 fprintf(stdout, "backend: %s\n", req.backend);
3157 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
3158 fprintf(stdout, "LUN size: %ju bytes\n",
3159 (uintmax_t)req.reqdata.create.lun_size_bytes);
3160 fprintf(stdout, "blocksize %u bytes\n",
3161 req.reqdata.create.blocksize_bytes);
3162 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
3163 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
3164 fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
3171 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
3173 struct ctl_lun_req req;
3174 uint32_t lun_id = 0;
3176 char *backend_name = NULL;
3177 STAILQ_HEAD(, cctl_req_option) option_list;
3178 int num_options = 0;
3181 STAILQ_INIT(&option_list);
3183 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3186 backend_name = strdup(optarg);
3189 lun_id = strtoul(optarg, NULL, 0);
3193 struct cctl_req_option *option;
3197 tmpstr = strdup(optarg);
3198 name = strsep(&tmpstr, "=");
3200 warnx("%s: option -o takes \"name=value\""
3201 "argument", __func__);
3205 value = strsep(&tmpstr, "=");
3206 if (value == NULL) {
3207 warnx("%s: option -o takes \"name=value\""
3208 "argument", __func__);
3212 option = malloc(sizeof(*option));
3213 if (option == NULL) {
3214 warn("%s: error allocating %zd bytes",
3215 __func__, sizeof(*option));
3219 option->name = strdup(name);
3220 option->namelen = strlen(name) + 1;
3221 option->value = strdup(value);
3222 option->vallen = strlen(value) + 1;
3225 STAILQ_INSERT_TAIL(&option_list, option, links);
3234 if (backend_name == NULL)
3235 errx(1, "%s: backend name (-b) must be specified", __func__);
3237 if (lun_id_set == 0)
3238 errx(1, "%s: LUN id (-l) must be specified", __func__);
3240 bzero(&req, sizeof(req));
3242 strlcpy(req.backend, backend_name, sizeof(req.backend));
3243 req.reqtype = CTL_LUNREQ_RM;
3245 req.reqdata.rm.lun_id = lun_id;
3247 req.num_be_args = num_options;
3248 if (num_options > 0) {
3249 struct cctl_req_option *option, *next_option;
3252 req.be_args = malloc(num_options * sizeof(*req.be_args));
3253 if (req.be_args == NULL) {
3254 warn("%s: error allocating %zd bytes", __func__,
3255 num_options * sizeof(*req.be_args));
3260 for (i = 0, option = STAILQ_FIRST(&option_list);
3261 i < num_options; i++, option = next_option) {
3262 next_option = STAILQ_NEXT(option, links);
3264 req.be_args[i].namelen = option->namelen;
3265 req.be_args[i].name = strdup(option->name);
3266 req.be_args[i].vallen = option->vallen;
3267 req.be_args[i].value = strdup(option->value);
3269 * XXX KDM do we want a way to specify a writeable
3270 * flag of some sort? Do we want a way to specify
3273 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3275 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3278 free(option->value);
3283 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3284 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3289 if (req.status == CTL_LUN_ERROR) {
3290 warnx("%s: error returned from LUN creation request:\n%s",
3291 __func__, req.error_str);
3294 } else if (req.status != CTL_LUN_OK) {
3295 warnx("%s: unknown LUN creation request status %d",
3296 __func__, req.status);
3301 printf("LUN %d deleted successfully\n", lun_id);
3308 * Name/value pair used for per-LUN attributes.
3310 struct cctl_lun_nv {
3313 STAILQ_ENTRY(cctl_lun_nv) links;
3317 * Backend LUN information.
3322 uint64_t size_blocks;
3324 char *serial_number;
3326 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3327 STAILQ_ENTRY(cctl_lun) links;
3330 struct cctl_devlist_data {
3332 STAILQ_HEAD(,cctl_lun) lun_list;
3333 struct cctl_lun *cur_lun;
3335 struct sbuf *cur_sb[32];
3339 cctl_start_element(void *user_data, const char *name, const char **attr)
3342 struct cctl_devlist_data *devlist;
3343 struct cctl_lun *cur_lun;
3345 devlist = (struct cctl_devlist_data *)user_data;
3346 cur_lun = devlist->cur_lun;
3348 if ((u_int)devlist->level > (sizeof(devlist->cur_sb) /
3349 sizeof(devlist->cur_sb[0])))
3350 errx(1, "%s: too many nesting levels, %zd max", __func__,
3351 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3353 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3354 if (devlist->cur_sb[devlist->level] == NULL)
3355 err(1, "%s: Unable to allocate sbuf", __func__);
3357 if (strcmp(name, "lun") == 0) {
3358 if (cur_lun != NULL)
3359 errx(1, "%s: improper lun element nesting", __func__);
3361 cur_lun = calloc(1, sizeof(*cur_lun));
3362 if (cur_lun == NULL)
3363 err(1, "%s: cannot allocate %zd bytes", __func__,
3366 devlist->num_luns++;
3367 devlist->cur_lun = cur_lun;
3369 STAILQ_INIT(&cur_lun->attr_list);
3370 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3372 for (i = 0; attr[i] != NULL; i += 2) {
3373 if (strcmp(attr[i], "id") == 0) {
3374 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3376 errx(1, "%s: invalid LUN attribute %s = %s",
3377 __func__, attr[i], attr[i+1]);
3384 cctl_end_element(void *user_data, const char *name)
3386 struct cctl_devlist_data *devlist;
3387 struct cctl_lun *cur_lun;
3390 devlist = (struct cctl_devlist_data *)user_data;
3391 cur_lun = devlist->cur_lun;
3393 if ((cur_lun == NULL)
3394 && (strcmp(name, "ctllunlist") != 0))
3395 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3397 if (devlist->cur_sb[devlist->level] == NULL)
3398 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3399 devlist->level, name);
3401 sbuf_finish(devlist->cur_sb[devlist->level]);
3402 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3404 err(1, "%s can't allocate %zd bytes for string", __func__,
3405 sbuf_len(devlist->cur_sb[devlist->level]));
3407 if (strlen(str) == 0) {
3412 sbuf_delete(devlist->cur_sb[devlist->level]);
3413 devlist->cur_sb[devlist->level] = NULL;
3416 if (strcmp(name, "backend_type") == 0) {
3417 cur_lun->backend_type = str;
3419 } else if (strcmp(name, "size") == 0) {
3420 cur_lun->size_blocks = strtoull(str, NULL, 0);
3421 } else if (strcmp(name, "blocksize") == 0) {
3422 cur_lun->blocksize = strtoul(str, NULL, 0);
3423 } else if (strcmp(name, "serial_number") == 0) {
3424 cur_lun->serial_number = str;
3426 } else if (strcmp(name, "device_id") == 0) {
3427 cur_lun->device_id = str;
3429 } else if (strcmp(name, "lun") == 0) {
3430 devlist->cur_lun = NULL;
3431 } else if (strcmp(name, "ctllunlist") == 0) {
3434 struct cctl_lun_nv *nv;
3436 nv = calloc(1, sizeof(*nv));
3438 err(1, "%s: can't allocate %zd bytes for nv pair",
3439 __func__, sizeof(*nv));
3441 nv->name = strdup(name);
3442 if (nv->name == NULL)
3443 err(1, "%s: can't allocated %zd bytes for string",
3444 __func__, strlen(name));
3448 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3455 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3457 struct cctl_devlist_data *devlist;
3459 devlist = (struct cctl_devlist_data *)user_data;
3461 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3465 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3467 struct ctl_lun_list list;
3468 struct cctl_devlist_data devlist;
3469 struct cctl_lun *lun;
3475 char *backend = NULL;
3481 bzero(&devlist, sizeof(devlist));
3482 STAILQ_INIT(&devlist.lun_list);
3484 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3487 backend = strdup(optarg);
3501 lun_str = malloc(lun_len);
3503 bzero(&list, sizeof(list));
3504 list.alloc_len = lun_len;
3505 list.status = CTL_LUN_LIST_NONE;
3506 list.lun_xml = lun_str;
3508 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
3509 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
3514 if (list.status == CTL_LUN_LIST_ERROR) {
3515 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
3516 __func__, list.error_str);
3517 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3518 lun_len = lun_len << 1;
3522 if (dump_xml != 0) {
3523 printf("%s", lun_str);
3527 parser = XML_ParserCreate(NULL);
3528 if (parser == NULL) {
3529 warn("%s: Unable to create XML parser", __func__);
3534 XML_SetUserData(parser, &devlist);
3535 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
3536 XML_SetCharacterDataHandler(parser, cctl_char_handler);
3538 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
3539 XML_ParserFree(parser);
3545 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
3546 "Serial Number", "Device ID");
3547 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
3548 struct cctl_lun_nv *nv;
3550 if ((backend != NULL)
3551 && (strcmp(lun->backend_type, backend) != 0))
3554 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
3555 (uintmax_t)lun->lun_id,
3556 lun->backend_type, (uintmax_t)lun->size_blocks,
3557 lun->blocksize, lun->serial_number, lun->device_id);
3562 STAILQ_FOREACH(nv, &lun->attr_list, links) {
3563 printf(" %s=%s\n", nv->name, nv->value);
3575 fprintf(error ? stderr : stdout,
3577 "Primary commands:\n"
3578 " ctladm tur [dev_id][general options]\n"
3579 " ctladm inquiry [dev_id][general options]\n"
3580 " ctladm devid [dev_id][general options]\n"
3581 " ctladm reqsense [dev_id][general options]\n"
3582 " ctladm reportluns [dev_id][general options]\n"
3583 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
3584 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3585 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
3586 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3587 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
3588 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
3589 " [-d] [-S subpage] [-c cdbsize]\n"
3590 " ctladm prin [dev_id][general options] <-a action>\n"
3591 " ctladm prout [dev_id][general options] <-a action>\n"
3592 " <-r restype] [-k key] [-s sa_key]\n"
3593 " ctladm rtpg [dev_id][general options]\n"
3594 " ctladm start [dev_id][general options] [-i] [-o]\n"
3595 " ctladm stop [dev_id][general options] [-i] [-o]\n"
3596 " ctladm synccache [dev_id][general options] [-l lba]\n"
3597 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
3598 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
3599 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
3600 " [-S serial_num] [-t dev_type]\n"
3601 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
3602 " ctladm devlist [-b][-v][-x]\n"
3603 " ctladm shutdown\n"
3605 " ctladm hardstop\n"
3606 " ctladm hardstart\n"
3608 " ctladm bbrread [dev_id] <-l lba> <-d datalen>\n"
3609 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
3611 " ctladm realsync <on|off|query>\n"
3612 " ctladm setsync [dev_id] <-i interval>\n"
3613 " ctladm getsync [dev_id]\n"
3614 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
3615 " [-s len fmt [args]] [-c] [-d delete_id]\n"
3616 " ctladm port <-l | -o <on|off> | [-w wwnn][-W wwpn]>\n"
3617 " [-p targ_port] [-t port_type] [-q] [-x]\n"
3619 " ctladm dumpstructs\n"
3621 "General Options:\n"
3622 "-I intiator_id : defaults to 7, used to change the initiator id\n"
3623 "-C retries : specify the number of times to retry this command\n"
3624 "-D devicename : specify the device to operate on\n"
3625 " : (default is %s)\n"
3626 "read/write options:\n"
3627 "-l lba : logical block address\n"
3628 "-d len : read/write length, in blocks\n"
3629 "-f file|- : write/read data to/from file or stdout/stdin\n"
3630 "-b blocksize : block size, in bytes\n"
3631 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
3632 "-N : do not copy data to/from userland\n"
3633 "readcapacity options:\n"
3634 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3635 "modesense options:\n"
3636 "-m page : specify the mode page to view\n"
3637 "-l : request a list of supported pages\n"
3638 "-P pc : specify the page control value: 0-3 (current,\n"
3639 " changeable, default, saved, respectively)\n"
3640 "-d : disable block descriptors for mode sense\n"
3641 "-S subpage : specify a subpage\n"
3642 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
3643 "persistent reserve in options:\n"
3644 "-a action : specify the action value: 0-2 (read key, read\n"
3645 " reservation, read capabilities, respectively)\n"
3646 "persistent reserve out options:\n"
3647 "-a action : specify the action value: 0-5 (register, reserve,\n"
3648 " release, clear, preempt, register and ignore)\n"
3649 "-k key : key value\n"
3650 "-s sa_key : service action value\n"
3651 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
3652 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
3653 "start/stop options:\n"
3654 "-i : set the immediate bit (CTL does not support this)\n"
3655 "-o : set the on/offline bit\n"
3656 "synccache options:\n"
3657 "-l lba : set the starting LBA\n"
3658 "-b blockcount : set the length to sync in blocks\n"
3659 "-r : set the relative addressing bit\n"
3660 "-i : set the immediate bit\n"
3661 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3663 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3664 "-B blocksize : LUN blocksize in bytes (some backends)\n"
3665 "-d device_id : SCSI VPD page 0x83 ID\n"
3666 "-l lun_id : requested LUN number\n"
3667 "-o name=value : backend-specific options, multiple allowed\n"
3668 "-s size_bytes : LUN size in bytes (some backends)\n"
3669 "-S serial_num : SCSI VPD page 0x80 serial number\n"
3670 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
3672 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3673 "-l lun_id : LUN number to delete\n"
3674 "-o name=value : backend-specific options, multiple allowed\n"
3675 "devlist options:\n"
3676 "-b backend : list devices from specified backend only\n"
3677 "-v : be verbose, show backend attributes\n"
3678 "-x : dump raw XML\n"
3680 "-l datamove|done : delay command at datamove or done phase\n"
3681 "-T oneshot : delay one command, then resume normal completion\n"
3682 "-T cont : delay all commands\n"
3683 "-t secs : number of seconds to delay\n"
3685 "-i error_action : action to perform\n"
3686 "-p pattern : command pattern to look for\n"
3687 "-r lba,len : LBA range for pattern\n"
3688 "-s len fmt [args] : sense data for custom sense action\n"
3689 "-c : continuous operation\n"
3690 "-d delete_id : error id to delete\n"
3692 "-l : list frontend ports\n"
3693 "-o on|off : turn frontend ports on or off\n"
3694 "-w wwnn : set WWNN for one frontend\n"
3695 "-W wwpn : set WWPN for one frontend\n"
3696 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
3697 "-p targ_port : specify target port number\n"
3698 "-q : omit header in list output\n"
3699 "-x : output port list in XML format\n"
3700 "bbrread options:\n"
3701 "-l lba : starting LBA\n"
3702 "-d datalen : length, in bytes, to read\n",
3707 main(int argc, char **argv)
3710 ctladm_cmdfunction command;
3711 ctladm_cmdargs cmdargs;
3712 ctladm_optret optreturn;
3714 const char *mainopt = "C:D:I:";
3715 const char *subopt = NULL;
3716 char combinedopt[256];
3724 cmdargs = CTLADM_ARG_NONE;
3725 command = CTLADM_CMD_HELP;
3740 * Get the base option.
3742 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
3744 if (optreturn == CC_OR_AMBIGUOUS) {
3745 warnx("ambiguous option %s", argv[1]);
3748 } else if (optreturn == CC_OR_NOT_FOUND) {
3749 warnx("option %s not found", argv[1]);
3754 if (cmdargs & CTLADM_ARG_NEED_TL) {
3756 || (!isdigit(argv[2][0]))) {
3757 warnx("option %s requires a target:lun argument",
3762 retval = cctl_parse_tl(argv[2], &target, &lun);
3764 errx(1, "invalid target:lun argument %s", argv[2]);
3766 cmdargs |= CTLADM_ARG_TARG_LUN;
3771 * Ahh, getopt(3) is a pain.
3773 * This is a gross hack. There really aren't many other good
3774 * options (excuse the pun) for parsing options in a situation like
3775 * this. getopt is kinda braindead, so you end up having to run
3776 * through the options twice, and give each invocation of getopt
3777 * the option string for the other invocation.
3779 * You would think that you could just have two groups of options.
3780 * The first group would get parsed by the first invocation of
3781 * getopt, and the second group would get parsed by the second
3782 * invocation of getopt. It doesn't quite work out that way. When
3783 * the first invocation of getopt finishes, it leaves optind pointing
3784 * to the argument _after_ the first argument in the second group.
3785 * So when the second invocation of getopt comes around, it doesn't
3786 * recognize the first argument it gets and then bails out.
3788 * A nice alternative would be to have a flag for getopt that says
3789 * "just keep parsing arguments even when you encounter an unknown
3790 * argument", but there isn't one. So there's no real clean way to
3791 * easily parse two sets of arguments without having one invocation
3792 * of getopt know about the other.
3794 * Without this hack, the first invocation of getopt would work as
3795 * long as the generic arguments are first, but the second invocation
3796 * (in the subfunction) would fail in one of two ways. In the case
3797 * where you don't set optreset, it would fail because optind may be
3798 * pointing to the argument after the one it should be pointing at.
3799 * In the case where you do set optreset, and reset optind, it would
3800 * fail because getopt would run into the first set of options, which
3801 * it doesn't understand.
3803 * All of this would "sort of" work if you could somehow figure out
3804 * whether optind had been incremented one option too far. The
3805 * mechanics of that, however, are more daunting than just giving
3806 * both invocations all of the expect options for either invocation.
3808 * Needless to say, I wouldn't mind if someone invented a better
3809 * (non-GPL!) command line parsing interface than getopt. I
3810 * wouldn't mind if someone added more knobs to getopt to make it
3811 * work better. Who knows, I may talk myself into doing it someday,
3812 * if the standards weenies let me. As it is, it just leads to
3813 * hackery like this and causes people to avoid it in some cases.
3815 * KDM, September 8th, 1998
3818 sprintf(combinedopt, "%s%s", mainopt, subopt);
3820 sprintf(combinedopt, "%s", mainopt);
3823 * Start getopt processing at argv[2/3], since we've already
3824 * accepted argv[1..2] as the command name, and as a possible
3830 * Now we run through the argument list looking for generic
3831 * options, and ignoring options that possibly belong to
3834 while ((c = getopt(argc, argv, combinedopt))!= -1){
3837 cmdargs |= CTLADM_ARG_RETRIES;
3838 retries = strtol(optarg, NULL, 0);
3841 device = strdup(optarg);
3842 cmdargs |= CTLADM_ARG_DEVICE;
3845 cmdargs |= CTLADM_ARG_INITIATOR;
3846 initid = strtol(optarg, NULL, 0);
3853 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
3860 * Default to opening the CTL device for now.
3862 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
3863 && (command != CTLADM_CMD_HELP)) {
3864 device = strdup(CTL_DEFAULT_DEV);
3865 cmdargs |= CTLADM_ARG_DEVICE;
3868 if ((cmdargs & CTLADM_ARG_DEVICE)
3869 && (command != CTLADM_CMD_HELP)) {
3870 fd = open(device, O_RDWR);
3872 fprintf(stderr, "%s: error opening %s: %s\n",
3873 argv[0], device, strerror(errno));
3877 } else if ((command != CTLADM_CMD_HELP)
3878 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
3879 fprintf(stderr, "%s: you must specify a device with the "
3880 "--device argument for this command\n", argv[0]);
3881 command = CTLADM_CMD_HELP;
3886 case CTLADM_CMD_TUR:
3887 retval = cctl_tur(fd, target, lun, initid, retries);
3889 case CTLADM_CMD_INQUIRY:
3890 retval = cctl_inquiry(fd, target, lun, initid, retries);
3892 case CTLADM_CMD_REQ_SENSE:
3893 retval = cctl_req_sense(fd, target, lun, initid, retries);
3895 case CTLADM_CMD_REPORT_LUNS:
3896 retval = cctl_report_luns(fd, target, lun, initid, retries);
3898 case CTLADM_CMD_CREATE:
3899 retval = cctl_create_lun(fd, argc, argv, combinedopt);
3902 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
3904 case CTLADM_CMD_DEVLIST:
3905 retval = cctl_devlist(fd, argc, argv, combinedopt);
3907 case CTLADM_CMD_READ:
3908 case CTLADM_CMD_WRITE:
3909 retval = cctl_read_write(fd, target, lun, initid, retries,
3910 argc, argv, combinedopt, command);
3912 case CTLADM_CMD_PORT:
3913 retval = cctl_port(fd, argc, argv, combinedopt);
3915 case CTLADM_CMD_READCAPACITY:
3916 retval = cctl_read_capacity(fd, target, lun, initid, retries,
3917 argc, argv, combinedopt);
3919 case CTLADM_CMD_MODESENSE:
3920 retval = cctl_mode_sense(fd, target, lun, initid, retries,
3921 argc, argv, combinedopt);
3923 case CTLADM_CMD_START:
3924 case CTLADM_CMD_STOP:
3925 retval = cctl_start_stop(fd, target, lun, initid, retries,
3926 (command == CTLADM_CMD_START) ? 1 : 0,
3927 argc, argv, combinedopt);
3929 case CTLADM_CMD_SYNC_CACHE:
3930 retval = cctl_sync_cache(fd, target, lun, initid, retries,
3931 argc, argv, combinedopt);
3933 case CTLADM_CMD_SHUTDOWN:
3934 case CTLADM_CMD_STARTUP:
3935 retval = cctl_startup_shutdown(fd, target, lun, initid,
3938 case CTLADM_CMD_HARDSTOP:
3939 case CTLADM_CMD_HARDSTART:
3940 retval = cctl_hardstopstart(fd, command);
3942 case CTLADM_CMD_BBRREAD:
3943 retval = cctl_bbrread(fd, target, lun, initid, argc, argv,
3946 case CTLADM_CMD_LUNLIST:
3947 retval = cctl_lunlist(fd);
3949 case CTLADM_CMD_DELAY:
3950 retval = cctl_delay(fd, target, lun, argc, argv, combinedopt);
3952 case CTLADM_CMD_REALSYNC:
3953 retval = cctl_realsync(fd, argc, argv);
3955 case CTLADM_CMD_SETSYNC:
3956 case CTLADM_CMD_GETSYNC:
3957 retval = cctl_getsetsync(fd, target, lun, command,
3958 argc, argv, combinedopt);
3960 case CTLADM_CMD_ERR_INJECT:
3961 retval = cctl_error_inject(fd, target, lun, argc, argv,
3964 case CTLADM_CMD_DUMPOOA:
3965 retval = cctl_dump_ooa(fd, argc, argv);
3967 case CTLADM_CMD_DUMPSTRUCTS:
3968 retval = cctl_dump_structs(fd, cmdargs);
3970 case CTLADM_CMD_PRES_IN:
3971 retval = cctl_persistent_reserve_in(fd, target, lun, initid,
3972 argc, argv, combinedopt,
3975 case CTLADM_CMD_PRES_OUT:
3976 retval = cctl_persistent_reserve_out(fd, target, lun, initid,
3977 argc, argv, combinedopt,
3980 case CTLADM_CMD_INQ_VPD_DEVID:
3981 retval = cctl_inquiry_vpd_devid(fd, target, lun, initid);
3983 case CTLADM_CMD_RTPG:
3984 retval = cctl_report_target_port_group(fd, target, lun, initid);
3986 case CTLADM_CMD_HELP: