2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3 * Copyright (c) 1997-2007 Kenneth D. Merry
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
35 * $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.c#4 $
38 * CAM Target Layer exercise program.
40 * Author: Ken Merry <ken@FreeBSD.org>
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/ioctl.h>
47 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/linker.h>
51 #include <sys/queue.h>
52 #include <sys/callout.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/ctl/ctl.h>
68 #include <cam/ctl/ctl_io.h>
69 #include <cam/ctl/ctl_frontend_internal.h>
70 #include <cam/ctl/ctl_backend.h>
71 #include <cam/ctl/ctl_ioctl.h>
72 #include <cam/ctl/ctl_backend_block.h>
73 #include <cam/ctl/ctl_util.h>
74 #include <cam/ctl/ctl_scsi_all.h>
82 #define min(x,y) (x < y) ? x : y
89 CTLADM_CMD_REPORT_LUNS,
98 CTLADM_CMD_READCAPACITY,
101 CTLADM_CMD_DUMPSTRUCTS,
104 CTLADM_CMD_SYNC_CACHE,
109 CTLADM_CMD_HARDSTART,
114 CTLADM_CMD_ERR_INJECT,
118 CTLADM_CMD_INQ_VPD_DEVID,
121 } ctladm_cmdfunction;
124 CTLADM_ARG_NONE = 0x0000000,
125 CTLADM_ARG_AUTOSENSE = 0x0000001,
126 CTLADM_ARG_DEVICE = 0x0000002,
127 CTLADM_ARG_ARRAYSIZE = 0x0000004,
128 CTLADM_ARG_BACKEND = 0x0000008,
129 CTLADM_ARG_CDBSIZE = 0x0000010,
130 CTLADM_ARG_DATALEN = 0x0000020,
131 CTLADM_ARG_FILENAME = 0x0000040,
132 CTLADM_ARG_LBA = 0x0000080,
133 CTLADM_ARG_PC = 0x0000100,
134 CTLADM_ARG_PAGE_CODE = 0x0000200,
135 CTLADM_ARG_PAGE_LIST = 0x0000400,
136 CTLADM_ARG_SUBPAGE = 0x0000800,
137 CTLADM_ARG_PAGELIST = 0x0001000,
138 CTLADM_ARG_DBD = 0x0002000,
139 CTLADM_ARG_TARG_LUN = 0x0004000,
140 CTLADM_ARG_BLOCKSIZE = 0x0008000,
141 CTLADM_ARG_IMMED = 0x0010000,
142 CTLADM_ARG_RELADR = 0x0020000,
143 CTLADM_ARG_RETRIES = 0x0040000,
144 CTLADM_ARG_ONOFFLINE = 0x0080000,
145 CTLADM_ARG_ONESHOT = 0x0100000,
146 CTLADM_ARG_TIMEOUT = 0x0200000,
147 CTLADM_ARG_INITIATOR = 0x0400000,
148 CTLADM_ARG_NOCOPY = 0x0800000,
149 CTLADM_ARG_NEED_TL = 0x1000000
155 ctladm_cmdargs argnum;
165 static const char rw_opts[] = "Nb:c:d:f:l:";
166 static const char startstop_opts[] = "io";
168 struct ctladm_opts option_table[] = {
169 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
170 {"bbrread", CTLADM_CMD_BBRREAD, CTLADM_ARG_NEED_TL, "d:l:"},
171 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
172 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
173 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
174 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
175 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
176 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
177 {"getsync", CTLADM_CMD_GETSYNC, CTLADM_ARG_NEED_TL, NULL},
178 {"hardstart", CTLADM_CMD_HARDSTART, CTLADM_ARG_NONE, NULL},
179 {"hardstop", CTLADM_CMD_HARDSTOP, CTLADM_ARG_NONE, NULL},
180 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
181 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
182 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
183 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
184 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
185 {"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:s:"},
186 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
187 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
188 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
189 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
190 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
191 {"realsync", CTLADM_CMD_REALSYNC, CTLADM_ARG_NONE, NULL},
192 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
193 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
194 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
195 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
196 {"setsync", CTLADM_CMD_SETSYNC, CTLADM_ARG_NEED_TL, "i:"},
197 {"shutdown", CTLADM_CMD_SHUTDOWN, CTLADM_ARG_NONE, NULL},
198 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
199 {"startup", CTLADM_CMD_STARTUP, CTLADM_ARG_NONE, NULL},
200 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
201 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
202 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
203 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
204 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
205 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
210 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
211 ctladm_cmdargs *argnum, const char **subopt);
212 static int cctl_parse_tl(char *str, int *target, int *lun);
213 static int cctl_dump_ooa(int fd, int argc, char **argv);
214 static int cctl_port_dump(int fd, int quiet, int xml, int32_t fe_num,
215 ctl_port_type port_type);
216 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
217 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
218 static int cctl_delay(int fd, int target, int lun, int argc, char **argv,
220 static int cctl_lunlist(int fd);
221 static void cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len);
222 static void cctl_cfi_bbr_statusstr(cfi_bbrread_status, char *str, int str_len);
223 static int cctl_hardstopstart(int fd, ctladm_cmdfunction command);
224 static int cctl_bbrread(int fd, int target, int lun, int iid, int argc,
225 char **argv, char *combinedopt);
226 static int cctl_startup_shutdown(int fd, int target, int lun, int iid,
227 ctladm_cmdfunction command);
228 static int cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
229 int argc, char **argv, char *combinedopt);
230 static int cctl_start_stop(int fd, int target, int lun, int iid, int retries,
231 int start, int argc, char **argv, char *combinedopt);
232 static int cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
233 int argc, char **argv, char *combinedopt);
234 static int cctl_read_capacity(int fd, int target, int lun, int iid,
235 int retries, int argc, char **argv,
237 static int cctl_read_write(int fd, int target, int lun, int iid, int retries,
238 int argc, char **argv, char *combinedopt,
239 ctladm_cmdfunction command);
240 static int cctl_get_luns(int fd, int target, int lun, int iid, int retries,
241 struct scsi_report_luns_data **lun_data,
243 static int cctl_report_luns(int fd, int target, int lun, int iid, int retries);
244 static int cctl_tur(int fd, int target, int lun, int iid, int retries);
245 static int cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
246 char *path_str, int path_len,
247 struct scsi_inquiry_data *inq_data);
248 static int cctl_inquiry(int fd, int target, int lun, int iid, int retries);
249 static int cctl_req_sense(int fd, int target, int lun, int iid, int retries);
250 static int cctl_persistent_reserve_in(int fd, int target, int lun,
251 int initiator, int argc, char **argv,
252 char *combinedopt, int retry_count);
253 static int cctl_persistent_reserve_out(int fd, int target, int lun,
254 int initiator, int argc, char **argv,
255 char *combinedopt, int retry_count);
256 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
257 static int cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator);
258 static int cctl_report_target_port_group(int fd, int target, int lun,
260 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
263 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
264 ctladm_cmdargs *argnum, const char **subopt)
266 struct ctladm_opts *opts;
269 for (opts = table; (opts != NULL) && (opts->optname != NULL);
271 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
272 *cmdnum = opts->cmdnum;
273 *argnum = opts->argnum;
274 *subopt = opts->subopt;
276 if (strcmp(opts->optname, arg) == 0)
277 return (CC_OR_FOUND);
279 if (++num_matches > 1)
280 return(CC_OR_AMBIGUOUS);
287 return(CC_OR_NOT_FOUND);
292 cctl_parse_tl(char *str, int *target, int *lun)
299 while (isspace(*str) && (*str != '\0'))
302 tmpstr = (char *)strtok(str, ":");
303 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
304 *target = strtol(tmpstr, NULL, 0);
305 tmpstr = (char *)strtok(NULL, ":");
306 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
307 *lun = strtol(tmpstr, NULL, 0);
317 cctl_dump_ooa(int fd, int argc, char **argv)
320 long double cmd_latency;
321 int num_entries, len;
322 int target = -1, lun = -1;
329 && (isdigit(argv[2][0]))) {
330 retval = cctl_parse_tl(argv[2], &target, &lun);
332 warnx("invalid target:lun argument %s", argv[2]);
336 len = num_entries * sizeof(struct ctl_ooa_entry);
338 bzero(&ooa, sizeof(ooa));
340 ooa.entries = malloc(len);
342 if (ooa.entries == NULL) {
343 warn("%s: error mallocing %d bytes", __func__, len);
350 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
353 ooa.alloc_num = num_entries;
354 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
355 warn("%s: CTL_GET_OOA ioctl failed", __func__);
360 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
361 num_entries = num_entries * 2;
367 if (ooa.status != CTL_OOA_OK) {
368 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
374 fprintf(stdout, "Dumping OOA queues\n");
375 for (i = 0; i < ooa.fill_num; i++) {
376 struct ctl_ooa_entry *entry;
377 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
378 struct bintime delta_bt;
381 entry = &ooa.entries[i];
383 delta_bt = ooa.cur_bt;
384 bintime_sub(&delta_bt, &entry->start_bt);
385 bintime2timespec(&delta_bt, &ts);
386 cmd_latency = ts.tv_sec * 1000;
388 cmd_latency += ts.tv_nsec / 1000000;
390 fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
392 (intmax_t)entry->lun_num, entry->tag_num,
393 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
395 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
396 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
398 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
400 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
401 scsi_op_desc(entry->cdb[0], NULL),
402 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
405 fprintf(stdout, "OOA queues dump done\n");
407 if (ioctl(fd, CTL_DUMP_OOA) == -1) {
408 warn("%s: CTL_DUMP_OOA ioctl failed", __func__);
420 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
422 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
430 cctl_port_dump(int fd, int quiet, int xml, int32_t targ_port,
431 ctl_port_type port_type)
433 struct ctl_port_list port_list;
434 struct ctl_port_entry *entries;
435 struct sbuf *sb = NULL;
444 entries = malloc(sizeof(*entries) * num_entries);
445 bzero(&port_list, sizeof(port_list));
446 port_list.entries = entries;
447 port_list.alloc_num = num_entries;
448 port_list.alloc_len = num_entries * sizeof(*entries);
449 if (ioctl(fd, CTL_GET_PORT_LIST, &port_list) != 0) {
450 warn("%s: CTL_GET_PORT_LIST ioctl failed", __func__);
453 if (port_list.status == CTL_PORT_LIST_NEED_MORE_SPACE) {
454 printf("%s: allocated %d, need %d, retrying\n", __func__,
455 num_entries, port_list.fill_num + port_list.dropped_num);
457 num_entries = port_list.fill_num + port_list.dropped_num;
463 printf("Port Online Type Name pp vp %-18s %-18s\n",
467 sb = sbuf_new_auto();
468 sbuf_printf(sb, "<ctlfelist>\n");
470 for (i = 0; i < port_list.fill_num; i++) {
471 struct ctl_port_entry *entry;
476 switch (entry->port_type) {
486 case CTL_PORT_INTERNAL:
498 * If the user specified a frontend number or a particular
499 * frontend type, only print out that particular frontend
502 if ((targ_port != -1)
503 && (targ_port != entry->targ_port))
505 else if ((port_type != CTL_PORT_NONE)
506 && ((port_type & entry->port_type) == 0))
512 printf("Num: %ju Type: %s (%#x) Name: %s Physical Port: %d "
513 "Virtual Port: %d\n", (uintmax_t)entry->fe_num, type,
514 entry->port_type, entry->fe_name, entry->physical_port,
515 entry->virtual_port);
516 printf("WWNN %#jx WWPN %#jx Online: %s\n",
517 (uintmax_t)entry->wwnn, (uintmax_t)entry->wwpn,
518 (entry->online) ? "YES" : "NO" );
521 printf("%-4d %-6s %-8s %-12s %-2d %-2d %#-18jx "
523 entry->targ_port, (entry->online) ? "YES" : "NO",
524 type, entry->port_name, entry->physical_port,
525 entry->virtual_port, (uintmax_t)entry->wwnn,
526 (uintmax_t)entry->wwpn);
528 sbuf_printf(sb, "<targ_port id=\"%d\">\n",
530 sbuf_printf(sb, "<online>%s</online>\n",
531 (entry->online) ? "YES" : "NO");
532 sbuf_printf(sb, "<port_type>%s</port_type>\n", type);
533 sbuf_printf(sb, "<port_name>%s</port_name>\n",
535 sbuf_printf(sb, "<physical_port>%d</physical_port>\n",
536 entry->physical_port);
537 sbuf_printf(sb, "<virtual_port>%d</virtual_port>\n",
538 entry->virtual_port);
539 sbuf_printf(sb, "<wwnn>%#jx</wwnn>\n",
540 (uintmax_t)entry->wwnn);
541 sbuf_printf(sb, "<wwpn>%#jx</wwpn>\n",
542 (uintmax_t)entry->wwpn);
543 sbuf_printf(sb, "</targ_port>\n");
548 sbuf_printf(sb, "</ctlfelist>\n");
549 if (sbuf_finish(sb) != 0)
550 err(1, "%s: sbuf_finish", __func__);
551 printf("%s", sbuf_data(sb));
556 * Give some indication that we didn't find the frontend or
557 * frontend type requested by the user. We could print something
558 * out, but it would probably be better to hide that behind a
562 && ((targ_port != -1)
563 || (port_type != CTL_PORT_NONE)))
577 struct ctladm_opts cctl_fe_table[] = {
578 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
579 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
580 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
581 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
586 cctl_port(int fd, int argc, char **argv, char *combinedopt)
589 int32_t targ_port = -1;
591 int wwnn_set = 0, wwpn_set = 0;
592 uint64_t wwnn = 0, wwpn = 0;
593 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
594 struct ctl_port_entry entry;
595 ctl_port_type port_type = CTL_PORT_NONE;
596 int quiet = 0, xml = 0;
598 while ((c = getopt(argc, argv, combinedopt)) != -1) {
601 if (port_mode != CCTL_PORT_MODE_NONE)
604 port_mode = CCTL_PORT_MODE_LIST;
607 if (port_mode != CCTL_PORT_MODE_NONE)
610 if (strcasecmp(optarg, "on") == 0)
611 port_mode = CCTL_PORT_MODE_ON;
612 else if (strcasecmp(optarg, "off") == 0)
613 port_mode = CCTL_PORT_MODE_OFF;
615 warnx("Invalid -o argument %s, \"on\" or "
616 "\"off\" are the only valid args",
623 targ_port = strtol(optarg, NULL, 0);
629 ctladm_optret optret;
630 ctladm_cmdargs argnum;
632 ctl_port_type tmp_port_type;
634 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
636 if (optret == CC_OR_AMBIGUOUS) {
637 warnx("%s: ambiguous frontend type %s",
641 } else if (optret == CC_OR_NOT_FOUND) {
642 warnx("%s: invalid frontend type %s",
648 port_type |= tmp_port_type;
652 if ((port_mode != CCTL_PORT_MODE_NONE)
653 && (port_mode != CCTL_PORT_MODE_SET))
656 port_mode = CCTL_PORT_MODE_SET;
658 wwnn = strtoull(optarg, NULL, 0);
662 if ((port_mode != CCTL_PORT_MODE_NONE)
663 && (port_mode != CCTL_PORT_MODE_SET))
666 port_mode = CCTL_PORT_MODE_SET;
668 wwpn = strtoull(optarg, NULL, 0);
678 * The user can specify either one or more frontend types (-t), or
679 * a specific frontend, but not both.
681 * If the user didn't specify a frontend type or number, set it to
682 * all. This is primarily needed for the enable/disable ioctls.
683 * This will be a no-op for the listing code. For the set ioctl,
684 * we'll throw an error, since that only works on one port at a time.
686 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
687 warnx("%s: can only specify one of -t or -n", __func__);
690 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
691 port_type = CTL_PORT_ALL;
693 bzero(&entry, sizeof(&entry));
696 * These are needed for all but list/dump mode.
698 entry.port_type = port_type;
699 entry.targ_port = targ_port;
702 case CCTL_PORT_MODE_LIST:
703 cctl_port_dump(fd, quiet, xml, targ_port, port_type);
705 case CCTL_PORT_MODE_SET:
706 if (targ_port == -1) {
707 warnx("%s: -w and -W require -n", __func__);
713 entry.flags |= CTL_PORT_WWNN_VALID;
717 entry.flags |= CTL_PORT_WWPN_VALID;
721 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
722 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
727 case CCTL_PORT_MODE_ON:
728 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
729 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
733 fprintf(stdout, "Front End Ports enabled\n");
735 case CCTL_PORT_MODE_OFF:
736 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
737 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
741 fprintf(stdout, "Front End Ports disabled\n");
744 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
755 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
760 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
763 if (ioctl(fd, CTL_IO, io) == -1) {
764 warn("%s: error sending CTL_IO ioctl", func);
767 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
774 cctl_delay(int fd, int target, int lun, int argc, char **argv,
777 struct ctl_io_delay_info delay_info;
778 char *delayloc = NULL;
779 char *delaytype = NULL;
786 memset(&delay_info, 0, sizeof(delay_info));
788 while ((c = getopt(argc, argv, combinedopt)) != -1) {
791 delaytype = strdup(optarg);
794 delayloc = strdup(optarg);
797 delaytime = strtoul(optarg, NULL, 0);
802 if (delaytime == -1) {
803 warnx("%s: you must specify the delaytime with -t", __func__);
808 if (strcasecmp(delayloc, "datamove") == 0)
809 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
810 else if (strcasecmp(delayloc, "done") == 0)
811 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
813 warnx("%s: invalid delay location %s", __func__, delayloc);
818 if ((delaytype == NULL)
819 || (strcmp(delaytype, "oneshot") == 0))
820 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
821 else if (strcmp(delaytype, "cont") == 0)
822 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
824 warnx("%s: invalid delay type %s", __func__, delaytype);
829 delay_info.target_id = target;
830 delay_info.lun_id = lun;
831 delay_info.delay_secs = delaytime;
833 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
834 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
838 switch (delay_info.status) {
839 case CTL_DELAY_STATUS_NONE:
840 warnx("%s: no delay status??", __func__);
843 case CTL_DELAY_STATUS_OK:
845 case CTL_DELAY_STATUS_INVALID_LUN:
846 warnx("%s: invalid lun %d", __func__, lun);
849 case CTL_DELAY_STATUS_INVALID_TYPE:
850 warnx("%s: invalid delay type %d", __func__,
851 delay_info.delay_type);
854 case CTL_DELAY_STATUS_INVALID_LOC:
855 warnx("%s: delay location %s not implemented?", __func__,
859 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
860 warnx("%s: delay not implemented in the kernel", __func__);
861 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
865 warnx("%s: unknown delay return status %d", __func__,
872 /* delayloc should never be NULL, but just in case...*/
873 if (delayloc != NULL)
880 cctl_realsync(int fd, int argc, char **argv)
889 warnx("%s %s takes exactly one argument", argv[0], argv[1]);
896 if (strncasecmp(syncarg, "query", min(strlen(syncarg),
897 strlen("query"))) == 0) {
898 if (ioctl(fd, CTL_REALSYNC_GET, &syncstate) == -1) {
899 warn("%s: CTL_REALSYNC_GET ioctl failed", __func__);
903 fprintf(stdout, "SYNCHRONIZE CACHE support is: ");
906 fprintf(stdout, "OFF\n");
909 fprintf(stdout, "ON\n");
912 fprintf(stdout, "unknown (%d)\n", syncstate);
916 } else if (strcasecmp(syncarg, "on") == 0) {
918 } else if (strcasecmp(syncarg, "off") == 0) {
921 warnx("%s: invalid realsync argument %s", __func__, syncarg);
926 if (ioctl(fd, CTL_REALSYNC_SET, &syncstate) == -1) {
927 warn("%s: CTL_REALSYNC_SET ioctl failed", __func__);
936 cctl_getsetsync(int fd, int target, int lun, ctladm_cmdfunction command,
937 int argc, char **argv, char *combinedopt)
939 struct ctl_sync_info sync_info;
941 int sync_interval = -1;
947 memset(&sync_info, 0, sizeof(sync_info));
948 sync_info.target_id = target;
949 sync_info.lun_id = lun;
951 while ((c = getopt(argc, argv, combinedopt)) != -1) {
954 sync_interval = strtoul(optarg, NULL, 0);
961 if (command == CTLADM_CMD_SETSYNC) {
962 if (sync_interval == -1) {
963 warnx("%s: you must specify the sync interval with -i",
968 sync_info.sync_interval = sync_interval;
969 ioctl_cmd = CTL_SETSYNC;
971 ioctl_cmd = CTL_GETSYNC;
974 if (ioctl(fd, ioctl_cmd, &sync_info) == -1) {
975 warn("%s: CTL_%sSYNC ioctl failed", __func__,
976 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET");
981 switch (sync_info.status) {
983 if (command == CTLADM_CMD_GETSYNC) {
984 fprintf(stdout, "%d:%d: sync interval: %d\n",
985 target, lun, sync_info.sync_interval);
988 case CTL_GS_SYNC_NO_LUN:
989 warnx("%s: unknown target:LUN %d:%d", __func__, target, lun);
992 case CTL_GS_SYNC_NONE:
994 warnx("%s: unknown CTL_%sSYNC status %d", __func__,
995 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET",
1004 struct ctladm_opts cctl_err_types[] = {
1005 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
1006 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
1007 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
1008 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
1013 struct ctladm_opts cctl_err_patterns[] = {
1014 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
1015 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
1016 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1017 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1018 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
1019 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
1020 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
1022 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
1028 cctl_error_inject(int fd, uint32_t target, uint32_t lun, int argc, char **argv,
1032 struct ctl_error_desc err_desc;
1035 uint64_t delete_id = 0;
1036 int delete_id_set = 0;
1042 bzero(&err_desc, sizeof(err_desc));
1043 err_desc.target_id = target;
1044 err_desc.lun_id = lun;
1046 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1052 delete_id = strtoull(optarg, NULL, 0);
1057 ctladm_optret optret;
1058 ctladm_cmdargs argnum;
1062 ctl_lun_error err_type;
1064 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
1065 warnx("%s: can't specify multiple -i "
1066 "arguments", __func__);
1070 optret = getoption(cctl_err_types, optarg,
1071 &err_type, &argnum, &subopt);
1072 err_desc.lun_error = err_type;
1074 ctl_lun_error_pattern pattern;
1076 optret = getoption(cctl_err_patterns, optarg,
1077 &pattern, &argnum, &subopt);
1078 err_desc.error_pattern |= pattern;
1081 if (optret == CC_OR_AMBIGUOUS) {
1082 warnx("%s: ambiguous argument %s", __func__,
1086 } else if (optret == CC_OR_NOT_FOUND) {
1087 warnx("%s: argument %s not found", __func__,
1095 char *tmpstr, *tmpstr2;
1097 tmpstr = strdup(optarg);
1098 if (tmpstr == NULL) {
1099 warn("%s: error duplicating string %s",
1105 tmpstr2 = strsep(&tmpstr, ",");
1106 if (tmpstr2 == NULL) {
1107 warnx("%s: invalid -r argument %s", __func__,
1113 lba = strtoull(tmpstr2, NULL, 0);
1114 tmpstr2 = strsep(&tmpstr, ",");
1115 if (tmpstr2 == NULL) {
1116 warnx("%s: no len argument for -r lba,len, got"
1117 " %s", __func__, optarg);
1122 len = strtoul(tmpstr2, NULL, 0);
1127 struct get_hook hook;
1130 sense_len = strtol(optarg, NULL, 0);
1131 if (sense_len <= 0) {
1132 warnx("invalid number of sense bytes %d",
1138 sense_len = MIN(sense_len, SSD_FULL_SIZE);
1140 hook.argc = argc - optind;
1141 hook.argv = argv + optind;
1144 sensestr = cget(&hook, NULL);
1145 if ((sensestr != NULL)
1146 && (sensestr[0] == '-')) {
1150 (uint8_t *)&err_desc.custom_sense,
1151 sense_len, sensestr, iget, &hook);
1161 if (delete_id_set != 0) {
1162 err_desc.serial = delete_id;
1163 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
1164 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
1171 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
1172 warnx("%s: error injection command (-i) needed",
1176 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
1177 && (sense_len == 0)) {
1178 warnx("%s: custom error requires -s", __func__);
1183 if (continuous != 0)
1184 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
1187 * If fd_sense is set, we need to read the sense data the user
1188 * wants returned from stdin.
1190 if (fd_sense == 1) {
1192 int amt_to_read = sense_len;
1193 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
1195 for (amt_read = 0; amt_to_read > 0;
1196 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1197 if (amt_read == -1) {
1198 warn("error reading sense data from stdin");
1202 amt_to_read -= amt_read;
1203 buf_ptr += amt_read;
1207 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1208 warnx("%s: command pattern (-p) needed", __func__);
1214 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1216 * We could check here to see whether it's a read/write
1217 * command, but that will be pointless once we allow
1218 * custom patterns. At that point, the user could specify
1219 * a READ(6) CDB type, and we wouldn't have an easy way here
1220 * to verify whether range checking is possible there. The
1221 * user will just figure it out when his error never gets
1225 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1226 warnx("%s: need read and/or write pattern if range "
1227 "is specified", __func__);
1232 err_desc.lba_range.lba = lba;
1233 err_desc.lba_range.len = len;
1236 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1237 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1240 printf("Error injection succeeded, serial number is %ju\n",
1241 (uintmax_t)err_desc.serial);
1249 cctl_lunlist(int fd)
1251 struct scsi_report_luns_data *lun_data;
1252 struct scsi_inquiry_data *inq_data;
1266 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1267 * if we ever acquire the ability to have multiple targets.
1269 if ((retval = cctl_get_luns(fd, target, /*lun*/ 0, initid,
1270 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1273 inq_data = malloc(sizeof(*inq_data));
1274 if (inq_data == NULL) {
1275 warn("%s: couldn't allocate memory for inquiry data\n",
1280 for (i = 0; i < num_luns; i++) {
1284 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1285 case RPL_LUNDATA_ATYP_PERIPH:
1286 lun_val = lun_data->luns[i].lundata[1];
1288 case RPL_LUNDATA_ATYP_FLAT:
1289 lun_val = (lun_data->luns[i].lundata[0] &
1290 RPL_LUNDATA_FLAT_LUN_MASK) |
1291 (lun_data->luns[i].lundata[1] <<
1292 RPL_LUNDATA_FLAT_LUN_BITS);
1294 case RPL_LUNDATA_ATYP_LUN:
1295 case RPL_LUNDATA_ATYP_EXTLUN:
1297 fprintf(stdout, "Unsupported LUN format %d\n",
1298 lun_data->luns[i].lundata[0] &
1299 RPL_LUNDATA_ATYP_MASK);
1306 if ((retval = cctl_get_inquiry(fd, target, lun_val, initid,
1307 /*retries*/ 2, scsi_path,
1312 printf("%s", scsi_path);
1313 scsi_print_inquiry(inq_data);
1317 if (lun_data != NULL)
1320 if (inq_data != NULL)
1327 cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len)
1330 case CFI_MT_PORT_OFFLINE:
1331 snprintf(str, str_len, "Port Offline");
1334 snprintf(str, str_len, "Error");
1336 case CFI_MT_SUCCESS:
1337 snprintf(str, str_len, "Success");
1340 snprintf(str, str_len, "None??");
1343 snprintf(str, str_len, "Unknown status: %d", status);
1349 cctl_cfi_bbr_statusstr(cfi_bbrread_status status, char *str, int str_len)
1352 case CFI_BBR_SUCCESS:
1353 snprintf(str, str_len, "Success");
1355 case CFI_BBR_LUN_UNCONFIG:
1356 snprintf(str, str_len, "LUN not configured");
1358 case CFI_BBR_NO_LUN:
1359 snprintf(str, str_len, "LUN does not exist");
1361 case CFI_BBR_NO_MEM:
1362 snprintf(str, str_len, "Memory allocation error");
1364 case CFI_BBR_BAD_LEN:
1365 snprintf(str, str_len, "Length is not a multiple of blocksize");
1367 case CFI_BBR_RESERV_CONFLICT:
1368 snprintf(str, str_len, "Reservation conflict");
1370 case CFI_BBR_LUN_STOPPED:
1371 snprintf(str, str_len, "LUN is powered off");
1373 case CFI_BBR_LUN_OFFLINE_CTL:
1374 snprintf(str, str_len, "LUN is offline");
1376 case CFI_BBR_LUN_OFFLINE_RC:
1377 snprintf(str, str_len, "RAIDCore array is offline (double "
1380 case CFI_BBR_SCSI_ERROR:
1381 snprintf(str, str_len, "SCSI Error");
1384 snprintf(str, str_len, "Error");
1387 snprintf(str, str_len, "Unknown status: %d", status);
1393 cctl_hardstopstart(int fd, ctladm_cmdfunction command)
1395 struct ctl_hard_startstop_info hs_info;
1396 char error_str[256];
1402 if (command == CTLADM_CMD_HARDSTART)
1407 if (ioctl(fd, (do_start == 1) ? CTL_HARD_START : CTL_HARD_STOP,
1409 warn("%s: CTL_HARD_%s ioctl failed", __func__,
1410 (do_start == 1) ? "START" : "STOP");
1415 fprintf(stdout, "Hard %s Status: ", (command == CTLADM_CMD_HARDSTOP) ?
1417 cctl_cfi_mt_statusstr(hs_info.status, error_str, sizeof(error_str));
1418 fprintf(stdout, "%s\n", error_str);
1419 fprintf(stdout, "Total LUNs: %d\n", hs_info.total_luns);
1420 fprintf(stdout, "LUNs complete: %d\n", hs_info.luns_complete);
1421 fprintf(stdout, "LUNs failed: %d\n", hs_info.luns_failed);
1428 cctl_bbrread(int fd, int target __unused, int lun, int iid __unused,
1429 int argc, char **argv, char *combinedopt)
1431 struct ctl_bbrread_info bbr_info;
1432 char error_str[256];
1441 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1444 datalen = strtoul(optarg, NULL, 0);
1447 lba = strtoull(optarg, NULL, 0);
1456 warnx("%s: you must specify an LBA with -l", __func__);
1461 if (datalen == -1) {
1462 warnx("%s: you must specify a length with -d", __func__);
1467 bbr_info.lun_num = lun;
1470 * XXX KDM get the blocksize first??
1472 if ((datalen % 512) != 0) {
1473 warnx("%s: data length %d is not a multiple of 512 bytes",
1478 bbr_info.len = datalen;
1480 if (ioctl(fd, CTL_BBRREAD, &bbr_info) == -1) {
1481 warn("%s: CTL_BBRREAD ioctl failed", __func__);
1485 cctl_cfi_mt_statusstr(bbr_info.status, error_str, sizeof(error_str));
1486 fprintf(stdout, "BBR Read Overall Status: %s\n", error_str);
1487 cctl_cfi_bbr_statusstr(bbr_info.bbr_status, error_str,
1489 fprintf(stdout, "BBR Read Status: %s\n", error_str);
1491 * XXX KDM should we bother printing out SCSI status if we get
1492 * CFI_BBR_SCSI_ERROR back?
1494 * Return non-zero if this fails?
1501 cctl_startup_shutdown(int fd, int target, int lun, int iid,
1502 ctladm_cmdfunction command)
1506 struct scsi_report_luns_data *lun_data;
1507 struct scsi_inquiry_data *inq_data;
1517 * - step through each lun, do an inquiry
1518 * - check OOA queue on direct access luns
1519 * - send stop with offline bit to each direct access device with a
1521 * - if we get a reservation conflict, reset the LUN to clear it
1522 * and reissue the stop with the offline bit set
1527 io = ctl_scsi_alloc_io(id);
1529 warnx("%s: can't allocate memory", __func__);
1533 if ((retval = cctl_get_luns(fd, target, lun, iid, /*retries*/ 2,
1534 &lun_data, &num_luns)) != 0)
1537 inq_data = malloc(sizeof(*inq_data));
1538 if (inq_data == NULL) {
1539 warn("%s: couldn't allocate memory for inquiry data\n",
1544 for (i = 0; i < num_luns; i++) {
1549 * XXX KDM figure out a way to share this code with
1552 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1553 case RPL_LUNDATA_ATYP_PERIPH:
1554 lun_val = lun_data->luns[i].lundata[1];
1556 case RPL_LUNDATA_ATYP_FLAT:
1557 lun_val = (lun_data->luns[i].lundata[0] &
1558 RPL_LUNDATA_FLAT_LUN_MASK) |
1559 (lun_data->luns[i].lundata[1] <<
1560 RPL_LUNDATA_FLAT_LUN_BITS);
1562 case RPL_LUNDATA_ATYP_LUN:
1563 case RPL_LUNDATA_ATYP_EXTLUN:
1565 fprintf(stdout, "Unsupported LUN format %d\n",
1566 lun_data->luns[i].lundata[0] &
1567 RPL_LUNDATA_ATYP_MASK);
1574 if ((retval = cctl_get_inquiry(fd, target, lun_val, iid,
1575 /*retries*/ 2, scsi_path,
1580 printf("%s", scsi_path);
1581 scsi_print_inquiry(inq_data);
1583 * We only want to shutdown direct access devices.
1585 if (SID_TYPE(inq_data) != T_DIRECT) {
1586 printf("%s LUN is not direct access, skipped\n",
1591 if (command == CTLADM_CMD_SHUTDOWN) {
1592 struct ctl_ooa_info ooa_info;
1594 ooa_info.target_id = target;
1595 ooa_info.lun_id = lun_val;
1597 if (ioctl(fd, CTL_CHECK_OOA, &ooa_info) == -1) {
1598 printf("%s CTL_CHECK_OOA ioctl failed\n",
1603 if (ooa_info.status != CTL_OOA_SUCCESS) {
1604 printf("%s CTL_CHECK_OOA returned status %d\n",
1605 scsi_path, ooa_info.status);
1608 if (ooa_info.num_entries != 0) {
1609 printf("%s %d entr%s in the OOA queue, "
1610 "skipping shutdown\n", scsi_path,
1611 ooa_info.num_entries,
1612 (ooa_info.num_entries > 1)?"ies" : "y" );
1617 ctl_scsi_start_stop(/*io*/ io,
1618 /*start*/(command == CTLADM_CMD_STARTUP) ?
1622 /*power_conditions*/ SSS_PC_START_VALID,
1625 (command == CTLADM_CMD_STARTUP) ?
1626 CTL_TAG_SIMPLE :CTL_TAG_ORDERED,
1629 io->io_hdr.nexus.targ_target.id = target;
1630 io->io_hdr.nexus.targ_lun = lun_val;
1631 io->io_hdr.nexus.initid = id;
1633 if (cctl_do_io(fd, /*retries*/ 3, io, __func__) != 0) {
1638 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1639 ctl_io_error_print(io, inq_data, stderr);
1641 printf("%s LUN is now %s\n", scsi_path,
1642 (command == CTLADM_CMD_STARTUP) ? "online" :
1647 if (lun_data != NULL)
1650 if (inq_data != NULL)
1654 ctl_scsi_free_io(io);
1660 cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
1661 int argc, char **argv, char *combinedopt)
1667 uint64_t our_lba = 0;
1668 uint32_t our_block_count = 0;
1669 int reladr = 0, immed = 0;
1675 io = ctl_scsi_alloc_io(id);
1677 warnx("%s: can't allocate memory", __func__);
1681 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1684 our_block_count = strtoul(optarg, NULL, 0);
1687 cdb_size = strtol(optarg, NULL, 0);
1693 our_lba = strtoull(optarg, NULL, 0);
1703 if (cdb_size != -1) {
1709 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1710 "and 16", __func__, cdb_size);
1713 break; /* NOTREACHED */
1718 ctl_scsi_sync_cache(/*io*/ io,
1721 /*minimum_cdb_size*/ cdb_size,
1722 /*starting_lba*/ our_lba,
1723 /*block_count*/ our_block_count,
1724 /*tag_type*/ CTL_TAG_SIMPLE,
1727 io->io_hdr.nexus.targ_target.id = target;
1728 io->io_hdr.nexus.targ_lun = lun;
1729 io->io_hdr.nexus.initid = id;
1731 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1736 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1737 fprintf(stdout, "Cache synchronized successfully\n");
1739 ctl_io_error_print(io, NULL, stderr);
1741 ctl_scsi_free_io(io);
1747 cctl_start_stop(int fd, int target, int lun, int iid, int retries, int start,
1748 int argc, char **argv, char *combinedopt)
1753 int immed = 0, onoffline = 0;
1759 io = ctl_scsi_alloc_io(id);
1761 warnx("%s: can't allocate memory", __func__);
1765 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1778 * Use an ordered tag for the stop command, to guarantee that any
1779 * pending I/O will finish before the stop command executes. This
1780 * would normally be the case anyway, since CTL will basically
1781 * treat the start/stop command as an ordered command with respect
1782 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1784 ctl_scsi_start_stop(/*io*/ io,
1787 /*immediate*/ immed,
1788 /*power_conditions*/ SSS_PC_START_VALID,
1789 /*onoffline*/ onoffline,
1790 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1794 io->io_hdr.nexus.targ_target.id = target;
1795 io->io_hdr.nexus.targ_lun = lun;
1796 io->io_hdr.nexus.initid = id;
1798 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1803 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1804 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1805 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1806 (start) ? "started" : "stopped");
1808 ctl_io_error_print(io, NULL, stderr);
1811 ctl_scsi_free_io(io);
1817 cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
1818 int argc, char **argv, char *combinedopt)
1824 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1834 io = ctl_scsi_alloc_io(id);
1836 warn("%s: can't allocate memory", __func__);
1840 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1843 pc = strtoul(optarg, NULL, 0);
1846 subpage = strtoul(optarg, NULL, 0);
1855 page_code = strtoul(optarg, NULL, 0);
1858 cdbsize = strtol(optarg, NULL, 0);
1865 if (((list == 0) && (page_code == -1))
1866 || ((list != 0) && (page_code != -1))) {
1867 warnx("%s: you must specify either a page code (-m) or -l",
1873 if ((page_code != -1)
1874 && ((page_code > SMS_ALL_PAGES_PAGE)
1875 || (page_code < 0))) {
1876 warnx("%s: page code %d is out of range", __func__,
1883 page_code = SMS_ALL_PAGES_PAGE;
1885 warnx("%s: arg -P makes no sense with -l",
1890 if (subpage != -1) {
1891 warnx("%s: arg -S makes no sense with -l", __func__);
1898 pc = SMS_PAGE_CTRL_CURRENT;
1902 warnx("%s: page control value %d is out of range: 0-3",
1912 || (subpage < 0))) {
1913 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1924 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1925 "and 10", __func__, cdbsize);
1941 dataptr = (uint8_t *)malloc(datalen);
1942 if (dataptr == NULL) {
1943 warn("%s: can't allocate %d bytes", __func__, datalen);
1948 memset(dataptr, 0, datalen);
1950 ctl_scsi_mode_sense(io,
1951 /*data_ptr*/ dataptr,
1952 /*data_len*/ datalen,
1955 /*page_code*/ page_code,
1957 /*subpage*/ subpage,
1958 /*minimum_cdb_size*/ cdbsize,
1959 /*tag_type*/ CTL_TAG_SIMPLE,
1962 io->io_hdr.nexus.targ_target.id = target;
1963 io->io_hdr.nexus.targ_lun = lun;
1964 io->io_hdr.nexus.initid = id;
1966 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1971 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1972 int pages_len, used_len;
1973 uint32_t returned_len;
1976 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1977 struct scsi_mode_hdr_6 *hdr6;
1980 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1982 returned_len = hdr6->datalen + 1;
1983 bdlen = hdr6->block_descr_len;
1985 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1987 struct scsi_mode_hdr_10 *hdr10;
1990 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
1992 returned_len = scsi_2btoul(hdr10->datalen) + 2;
1993 bdlen = scsi_2btoul(hdr10->block_descr_len);
1995 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
1997 /* just in case they can give us more than we allocated for */
1998 returned_len = min(returned_len, datalen);
1999 pages_len = returned_len - (ndataptr - dataptr);
2001 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
2002 returned_len, pages_len);
2005 fprintf(stdout, "Supported mode pages:\n");
2006 for (used_len = 0; used_len < pages_len;) {
2007 struct scsi_mode_page_header *header;
2009 header = (struct scsi_mode_page_header *)
2010 &ndataptr[used_len];
2011 fprintf(stdout, "%d\n", header->page_code);
2012 used_len += header->page_length + 2;
2015 for (used_len = 0; used_len < pages_len; used_len++) {
2016 fprintf(stdout, "0x%x ", ndataptr[used_len]);
2017 if (((used_len+1) % 16) == 0)
2018 fprintf(stdout, "\n");
2020 fprintf(stdout, "\n");
2023 ctl_io_error_print(io, NULL, stderr);
2026 ctl_scsi_free_io(io);
2028 if (dataptr != NULL)
2035 cctl_read_capacity(int fd, int target, int lun, int iid, int retries,
2036 int argc, char **argv, char *combinedopt)
2040 struct scsi_read_capacity_data *data;
2041 struct scsi_read_capacity_data_long *longdata;
2042 int cdbsize = -1, retval;
2051 io = ctl_scsi_alloc_io(id);
2053 warn("%s: can't allocate memory\n", __func__);
2057 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2060 cdbsize = strtol(optarg, NULL, 0);
2066 if (cdbsize != -1) {
2072 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
2073 "and 16", __func__, cdbsize);
2076 break; /* NOTREACHED */
2081 dataptr = (uint8_t *)malloc(sizeof(*longdata));
2082 if (dataptr == NULL) {
2083 warn("%s: can't allocate %zd bytes\n", __func__,
2088 memset(dataptr, 0, sizeof(*longdata));
2094 ctl_scsi_read_capacity(io,
2095 /*data_ptr*/ dataptr,
2096 /*data_len*/ sizeof(*longdata),
2100 /*tag_type*/ CTL_TAG_SIMPLE,
2104 ctl_scsi_read_capacity_16(io,
2105 /*data_ptr*/ dataptr,
2106 /*data_len*/ sizeof(*longdata),
2110 /*tag_type*/ CTL_TAG_SIMPLE,
2115 io->io_hdr.nexus.initid = id;
2116 io->io_hdr.nexus.targ_target.id = target;
2117 io->io_hdr.nexus.targ_lun = lun;
2119 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2124 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2128 if (cdbsize == 10) {
2130 data = (struct scsi_read_capacity_data *)dataptr;
2132 maxlba = scsi_4btoul(data->addr);
2133 blocksize = scsi_4btoul(data->length);
2135 if (maxlba == 0xffffffff) {
2140 longdata=(struct scsi_read_capacity_data_long *)dataptr;
2142 maxlba = scsi_8btou64(longdata->addr);
2143 blocksize = scsi_4btoul(longdata->length);
2146 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
2147 (uintmax_t)maxlba, blocksize);
2149 ctl_io_error_print(io, NULL, stderr);
2152 ctl_scsi_free_io(io);
2154 if (dataptr != NULL)
2161 cctl_read_write(int fd, int target, int lun, int iid, int retries,
2162 int argc, char **argv, char *combinedopt,
2163 ctladm_cmdfunction command)
2167 int file_fd, do_stdio;
2168 int cdbsize = -1, databytes;
2170 char *filename = NULL;
2171 int datalen = -1, blocksize = -1;
2183 io = ctl_scsi_alloc_io(id);
2185 warn("%s: can't allocate memory\n", __func__);
2189 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2192 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
2195 blocksize = strtoul(optarg, NULL, 0);
2198 cdbsize = strtoul(optarg, NULL, 0);
2201 datalen = strtoul(optarg, NULL, 0);
2204 filename = strdup(optarg);
2207 lba = strtoull(optarg, NULL, 0);
2214 if (filename == NULL) {
2215 warnx("%s: you must supply a filename using -f", __func__);
2220 if (datalen == -1) {
2221 warnx("%s: you must specify the data length with -d", __func__);
2227 warnx("%s: you must specify the LBA with -l", __func__);
2232 if (blocksize == -1) {
2233 warnx("%s: you must specify the blocksize with -b", __func__);
2238 if (cdbsize != -1) {
2246 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
2247 "10, 12 or 16", __func__, cdbsize);
2250 break; /* NOTREACHED */
2255 databytes = datalen * blocksize;
2256 dataptr = (uint8_t *)malloc(databytes);
2258 if (dataptr == NULL) {
2259 warn("%s: can't allocate %d bytes\n", __func__, databytes);
2263 if (strcmp(filename, "-") == 0) {
2264 if (command == CTLADM_CMD_READ)
2265 file_fd = STDOUT_FILENO;
2267 file_fd = STDIN_FILENO;
2270 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
2271 if (file_fd == -1) {
2272 warn("%s: can't open file %s", __func__, filename);
2278 memset(dataptr, 0, databytes);
2280 if (command == CTLADM_CMD_WRITE) {
2283 bytes_read = read(file_fd, dataptr, databytes);
2284 if (bytes_read == -1) {
2285 warn("%s: error reading file %s", __func__, filename);
2289 if (bytes_read != databytes) {
2290 warnx("%s: only read %d bytes from file %s",
2291 __func__, bytes_read, filename);
2296 ctl_scsi_read_write(io,
2297 /*data_ptr*/ dataptr,
2298 /*data_len*/ databytes,
2299 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
2301 /*minimum_cdb_size*/ cdbsize,
2303 /*num_blocks*/ datalen,
2304 /*tag_type*/ CTL_TAG_SIMPLE,
2307 io->io_hdr.nexus.targ_target.id = target;
2308 io->io_hdr.nexus.targ_lun = lun;
2309 io->io_hdr.nexus.initid = id;
2311 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2316 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2317 && (command == CTLADM_CMD_READ)) {
2320 bytes_written = write(file_fd, dataptr, databytes);
2321 if (bytes_written == -1) {
2322 warn("%s: can't write to %s", __func__, filename);
2325 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
2326 ctl_io_error_print(io, NULL, stderr);
2331 ctl_scsi_free_io(io);
2333 if (dataptr != NULL)
2344 cctl_get_luns(int fd, int target, int lun, int iid, int retries, struct
2345 scsi_report_luns_data **lun_data, uint32_t *num_luns)
2356 io = ctl_scsi_alloc_io(id);
2358 warnx("%s: can't allocate memory", __func__);
2363 * lun_data includes space for 1 lun, allocate space for 4 initially.
2364 * If that isn't enough, we'll allocate more.
2368 lun_datalen = sizeof(*lun_data) +
2369 (nluns * sizeof(struct scsi_report_luns_lundata));
2370 *lun_data = malloc(lun_datalen);
2372 if (*lun_data == NULL) {
2373 warnx("%s: can't allocate memory", __func__);
2374 ctl_scsi_free_io(io);
2378 ctl_scsi_report_luns(io,
2379 /*data_ptr*/ (uint8_t *)*lun_data,
2380 /*data_len*/ lun_datalen,
2381 /*select_report*/ RPL_REPORT_ALL,
2382 /*tag_type*/ CTL_TAG_SIMPLE,
2385 io->io_hdr.nexus.initid = id;
2386 io->io_hdr.nexus.targ_target.id = target;
2387 io->io_hdr.nexus.targ_lun = lun;
2389 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2394 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2395 uint32_t returned_len, returned_luns;
2397 returned_len = scsi_4btoul((*lun_data)->length);
2398 returned_luns = returned_len / 8;
2399 if (returned_luns > nluns) {
2400 nluns = returned_luns;
2404 /* These should be the same */
2405 *num_luns = MIN(returned_luns, nluns);
2407 ctl_io_error_print(io, NULL, stderr);
2411 ctl_scsi_free_io(io);
2417 cctl_report_luns(int fd, int target, int lun, int iid, int retries)
2419 struct scsi_report_luns_data *lun_data;
2420 uint32_t num_luns, i;
2425 if ((retval = cctl_get_luns(fd, target, lun, iid, retries, &lun_data,
2429 fprintf(stdout, "%u LUNs returned\n", num_luns);
2430 for (i = 0; i < num_luns; i++) {
2434 * XXX KDM figure out a way to share this code with
2437 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
2438 case RPL_LUNDATA_ATYP_PERIPH:
2439 lun_val = lun_data->luns[i].lundata[1];
2441 case RPL_LUNDATA_ATYP_FLAT:
2442 lun_val = (lun_data->luns[i].lundata[0] &
2443 RPL_LUNDATA_FLAT_LUN_MASK) |
2444 (lun_data->luns[i].lundata[1] <<
2445 RPL_LUNDATA_FLAT_LUN_BITS);
2447 case RPL_LUNDATA_ATYP_LUN:
2448 case RPL_LUNDATA_ATYP_EXTLUN:
2450 fprintf(stdout, "Unsupported LUN format %d\n",
2451 lun_data->luns[i].lundata[0] &
2452 RPL_LUNDATA_ATYP_MASK);
2459 fprintf(stdout, "%d\n", lun_val);
2463 if (lun_data != NULL)
2470 cctl_tur(int fd, int target, int lun, int iid, int retries)
2477 io = ctl_scsi_alloc_io(id);
2479 fprintf(stderr, "can't allocate memory\n");
2484 /* tag_type */ CTL_TAG_SIMPLE,
2487 io->io_hdr.nexus.targ_target.id = target;
2488 io->io_hdr.nexus.targ_lun = lun;
2489 io->io_hdr.nexus.initid = id;
2491 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2492 ctl_scsi_free_io(io);
2496 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2497 fprintf(stdout, "Unit is ready\n");
2499 ctl_io_error_print(io, NULL, stderr);
2505 cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
2506 char *path_str, int path_len,
2507 struct scsi_inquiry_data *inq_data)
2517 io = ctl_scsi_alloc_io(id);
2519 warnx("cctl_inquiry: can't allocate memory\n");
2523 ctl_scsi_inquiry(/*io*/ io,
2524 /*data_ptr*/ (uint8_t *)inq_data,
2525 /*data_len*/ sizeof(*inq_data),
2528 /*tag_type*/ CTL_TAG_SIMPLE,
2531 io->io_hdr.nexus.targ_target.id = target;
2532 io->io_hdr.nexus.targ_lun = lun;
2533 io->io_hdr.nexus.initid = id;
2535 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2540 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
2542 ctl_io_error_print(io, NULL, stderr);
2543 } else if (path_str != NULL)
2544 ctl_scsi_path_string(io, path_str, path_len);
2547 ctl_scsi_free_io(io);
2553 cctl_inquiry(int fd, int target, int lun, int iid, int retries)
2555 struct scsi_inquiry_data *inq_data;
2561 inq_data = malloc(sizeof(*inq_data));
2562 if (inq_data == NULL) {
2563 warnx("%s: can't allocate inquiry data", __func__);
2568 if ((retval = cctl_get_inquiry(fd, target, lun, iid, retries, scsi_path,
2569 sizeof(scsi_path), inq_data)) != 0)
2572 printf("%s", scsi_path);
2573 scsi_print_inquiry(inq_data);
2576 if (inq_data != NULL)
2583 cctl_req_sense(int fd, int target, int lun, int iid, int retries)
2586 struct scsi_sense_data *sense_data;
2594 io = ctl_scsi_alloc_io(id);
2596 warnx("cctl_req_sense: can't allocate memory\n");
2599 sense_data = malloc(sizeof(*sense_data));
2600 memset(sense_data, 0, sizeof(*sense_data));
2602 ctl_scsi_request_sense(/*io*/ io,
2603 /*data_ptr*/ (uint8_t *)sense_data,
2604 /*data_len*/ sizeof(*sense_data),
2606 /*tag_type*/ CTL_TAG_SIMPLE,
2609 io->io_hdr.nexus.targ_target.id = target;
2610 io->io_hdr.nexus.targ_lun = lun;
2611 io->io_hdr.nexus.initid = id;
2613 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2618 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2619 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2620 io->scsiio.sense_len = sizeof(*sense_data);
2621 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2623 ctl_io_error_print(io, NULL, stderr);
2627 ctl_scsi_free_io(io);
2634 cctl_report_target_port_group(int fd, int target, int lun, int initiator)
2646 io = ctl_scsi_alloc_io(id);
2648 warn("%s: can't allocate memory", __func__);
2653 dataptr = (uint8_t *)malloc(datalen);
2654 if (dataptr == NULL) {
2655 warn("%s: can't allocate %d bytes", __func__, datalen);
2660 memset(dataptr, 0, datalen);
2662 ctl_scsi_maintenance_in(/*io*/ io,
2663 /*data_ptr*/ dataptr,
2664 /*data_len*/ datalen,
2665 /*action*/ SA_RPRT_TRGT_GRP,
2666 /*tag_type*/ CTL_TAG_SIMPLE,
2669 io->io_hdr.nexus.targ_target.id = target;
2670 io->io_hdr.nexus.targ_lun = lun;
2671 io->io_hdr.nexus.initid = id;
2673 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2678 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2679 int returned_len, used_len;
2681 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2683 for (used_len = 0; used_len < returned_len; used_len++) {
2684 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2685 if (((used_len+1) % 8) == 0)
2686 fprintf(stdout, "\n");
2688 fprintf(stdout, "\n");
2690 ctl_io_error_print(io, NULL, stderr);
2693 ctl_scsi_free_io(io);
2695 if (dataptr != NULL)
2702 cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator)
2714 io = ctl_scsi_alloc_io(id);
2716 warn("%s: can't allocate memory", __func__);
2721 dataptr = (uint8_t *)malloc(datalen);
2722 if (dataptr == NULL) {
2723 warn("%s: can't allocate %d bytes", __func__, datalen);
2728 memset(dataptr, 0, datalen);
2730 ctl_scsi_inquiry(/*io*/ io,
2731 /*data_ptr*/ dataptr,
2732 /*data_len*/ datalen,
2734 /*page_code*/ SVPD_DEVICE_ID,
2735 /*tag_type*/ CTL_TAG_SIMPLE,
2738 io->io_hdr.nexus.targ_target.id = target;
2739 io->io_hdr.nexus.targ_lun = lun;
2740 io->io_hdr.nexus.initid = id;
2742 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2747 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2748 int returned_len, used_len;
2750 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2752 for (used_len = 0; used_len < returned_len; used_len++) {
2753 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2754 if (((used_len+1) % 8) == 0)
2755 fprintf(stdout, "\n");
2757 fprintf(stdout, "\n");
2759 ctl_io_error_print(io, NULL, stderr);
2762 ctl_scsi_free_io(io);
2764 if (dataptr != NULL)
2771 cctl_persistent_reserve_in(int fd, int target, int lun, int initiator,
2772 int argc, char **argv, char *combinedopt,
2787 io = ctl_scsi_alloc_io(id);
2789 warn("%s: can't allocate memory", __func__);
2793 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2796 action = strtol(optarg, NULL, 0);
2803 if (action < 0 || action > 2) {
2804 warn("action must be specified and in the range: 0-2");
2811 dataptr = (uint8_t *)malloc(datalen);
2812 if (dataptr == NULL) {
2813 warn("%s: can't allocate %d bytes", __func__, datalen);
2818 memset(dataptr, 0, datalen);
2820 ctl_scsi_persistent_res_in(io,
2821 /*data_ptr*/ dataptr,
2822 /*data_len*/ datalen,
2824 /*tag_type*/ CTL_TAG_SIMPLE,
2827 io->io_hdr.nexus.targ_target.id = target;
2828 io->io_hdr.nexus.targ_lun = lun;
2829 io->io_hdr.nexus.initid = id;
2831 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2836 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2837 int returned_len, used_len;
2843 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2844 returned_len = min(returned_len, 256);
2847 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2853 warnx("%s: invalid action %d", __func__, action);
2855 break; /* NOTREACHED */
2858 for (used_len = 0; used_len < returned_len; used_len++) {
2859 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2860 if (((used_len+1) % 8) == 0)
2861 fprintf(stdout, "\n");
2863 fprintf(stdout, "\n");
2865 ctl_io_error_print(io, NULL, stderr);
2868 ctl_scsi_free_io(io);
2870 if (dataptr != NULL)
2877 cctl_persistent_reserve_out(int fd, int target, int lun, int initiator,
2878 int argc, char **argv, char *combinedopt,
2884 uint64_t key = 0, sa_key = 0;
2885 int action = -1, restype = -1;
2894 io = ctl_scsi_alloc_io(id);
2896 warn("%s: can't allocate memory", __func__);
2900 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2903 action = strtol(optarg, NULL, 0);
2906 key = strtoull(optarg, NULL, 0);
2909 restype = strtol(optarg, NULL, 0);
2912 sa_key = strtoull(optarg, NULL, 0);
2918 if (action < 0 || action > 5) {
2919 warn("action must be specified and in the range: 0-5");
2924 if (restype < 0 || restype > 5) {
2925 if (action != 0 && action != 5 && action != 3) {
2926 warn("'restype' must specified and in the range: 0-5");
2933 dataptr = (uint8_t *)malloc(datalen);
2934 if (dataptr == NULL) {
2935 warn("%s: can't allocate %d bytes", __func__, datalen);
2940 memset(dataptr, 0, datalen);
2942 ctl_scsi_persistent_res_out(io,
2943 /*data_ptr*/ dataptr,
2944 /*data_len*/ datalen,
2949 /*tag_type*/ CTL_TAG_SIMPLE,
2952 io->io_hdr.nexus.targ_target.id = target;
2953 io->io_hdr.nexus.targ_lun = lun;
2954 io->io_hdr.nexus.initid = id;
2956 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2960 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2962 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2963 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2964 "successfully\n", scsi_path);
2966 ctl_io_error_print(io, NULL, stderr);
2969 ctl_scsi_free_io(io);
2971 if (dataptr != NULL)
2977 struct cctl_req_option {
2982 STAILQ_ENTRY(cctl_req_option) links;
2986 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2988 struct ctl_lun_req req;
2989 int device_type = -1;
2990 uint64_t lun_size = 0;
2991 uint32_t blocksize = 0, req_lun_id = 0;
2992 char *serial_num = NULL;
2993 char *device_id = NULL;
2994 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
2995 char *backend_name = NULL;
2996 STAILQ_HEAD(, cctl_req_option) option_list;
2997 int num_options = 0;
3000 STAILQ_INIT(&option_list);
3002 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3005 backend_name = strdup(optarg);
3008 blocksize = strtoul(optarg, NULL, 0);
3012 device_id = strdup(optarg);
3015 req_lun_id = strtoul(optarg, NULL, 0);
3019 struct cctl_req_option *option;
3023 tmpstr = strdup(optarg);
3024 name = strsep(&tmpstr, "=");
3026 warnx("%s: option -o takes \"name=value\""
3027 "argument", __func__);
3031 value = strsep(&tmpstr, "=");
3032 if (value == NULL) {
3033 warnx("%s: option -o takes \"name=value\""
3034 "argument", __func__);
3038 option = malloc(sizeof(*option));
3039 if (option == NULL) {
3040 warn("%s: error allocating %zd bytes",
3041 __func__, sizeof(*option));
3045 option->name = strdup(name);
3046 option->namelen = strlen(name) + 1;
3047 option->value = strdup(value);
3048 option->vallen = strlen(value) + 1;
3051 STAILQ_INSERT_TAIL(&option_list, option, links);
3056 if (strcasecmp(optarg, "auto") != 0) {
3057 retval = expand_number(optarg, &lun_size);
3059 warn("%s: invalid -s argument",
3068 serial_num = strdup(optarg);
3071 device_type = strtoul(optarg, NULL, 0);
3078 if (backend_name == NULL) {
3079 warnx("%s: backend name (-b) must be specified", __func__);
3084 bzero(&req, sizeof(req));
3086 strlcpy(req.backend, backend_name, sizeof(req.backend));
3087 req.reqtype = CTL_LUNREQ_CREATE;
3089 if (blocksize_set != 0)
3090 req.reqdata.create.blocksize_bytes = blocksize;
3092 if (lun_size_set != 0)
3093 req.reqdata.create.lun_size_bytes = lun_size;
3095 if (lun_id_set != 0) {
3096 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
3097 req.reqdata.create.req_lun_id = req_lun_id;
3100 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
3102 if (device_type != -1)
3103 req.reqdata.create.device_type = device_type;
3105 req.reqdata.create.device_type = T_DIRECT;
3107 if (serial_num != NULL) {
3108 strlcpy(req.reqdata.create.serial_num, serial_num,
3109 sizeof(req.reqdata.create.serial_num));
3110 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
3113 if (device_id != NULL) {
3114 strlcpy(req.reqdata.create.device_id, device_id,
3115 sizeof(req.reqdata.create.device_id));
3116 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
3119 req.num_be_args = num_options;
3120 if (num_options > 0) {
3121 struct cctl_req_option *option, *next_option;
3124 req.be_args = malloc(num_options * sizeof(*req.be_args));
3125 if (req.be_args == NULL) {
3126 warn("%s: error allocating %zd bytes", __func__,
3127 num_options * sizeof(*req.be_args));
3132 for (i = 0, option = STAILQ_FIRST(&option_list);
3133 i < num_options; i++, option = next_option) {
3134 next_option = STAILQ_NEXT(option, links);
3136 req.be_args[i].namelen = option->namelen;
3137 req.be_args[i].name = strdup(option->name);
3138 req.be_args[i].vallen = option->vallen;
3139 req.be_args[i].value = strdup(option->value);
3141 * XXX KDM do we want a way to specify a writeable
3142 * flag of some sort? Do we want a way to specify
3145 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3147 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3150 free(option->value);
3155 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3156 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3161 if (req.status == CTL_LUN_ERROR) {
3162 warnx("%s: error returned from LUN creation request:\n%s",
3163 __func__, req.error_str);
3166 } else if (req.status != CTL_LUN_OK) {
3167 warnx("%s: unknown LUN creation request status %d",
3168 __func__, req.status);
3173 fprintf(stdout, "LUN created successfully\n");
3174 fprintf(stdout, "backend: %s\n", req.backend);
3175 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
3176 fprintf(stdout, "LUN size: %ju bytes\n",
3177 (uintmax_t)req.reqdata.create.lun_size_bytes);
3178 fprintf(stdout, "blocksize %u bytes\n",
3179 req.reqdata.create.blocksize_bytes);
3180 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
3181 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
3182 fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
3189 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
3191 struct ctl_lun_req req;
3192 uint32_t lun_id = 0;
3194 char *backend_name = NULL;
3195 STAILQ_HEAD(, cctl_req_option) option_list;
3196 int num_options = 0;
3199 STAILQ_INIT(&option_list);
3201 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3204 backend_name = strdup(optarg);
3207 lun_id = strtoul(optarg, NULL, 0);
3211 struct cctl_req_option *option;
3215 tmpstr = strdup(optarg);
3216 name = strsep(&tmpstr, "=");
3218 warnx("%s: option -o takes \"name=value\""
3219 "argument", __func__);
3223 value = strsep(&tmpstr, "=");
3224 if (value == NULL) {
3225 warnx("%s: option -o takes \"name=value\""
3226 "argument", __func__);
3230 option = malloc(sizeof(*option));
3231 if (option == NULL) {
3232 warn("%s: error allocating %zd bytes",
3233 __func__, sizeof(*option));
3237 option->name = strdup(name);
3238 option->namelen = strlen(name) + 1;
3239 option->value = strdup(value);
3240 option->vallen = strlen(value) + 1;
3243 STAILQ_INSERT_TAIL(&option_list, option, links);
3252 if (backend_name == NULL)
3253 errx(1, "%s: backend name (-b) must be specified", __func__);
3255 if (lun_id_set == 0)
3256 errx(1, "%s: LUN id (-l) must be specified", __func__);
3258 bzero(&req, sizeof(req));
3260 strlcpy(req.backend, backend_name, sizeof(req.backend));
3261 req.reqtype = CTL_LUNREQ_RM;
3263 req.reqdata.rm.lun_id = lun_id;
3265 req.num_be_args = num_options;
3266 if (num_options > 0) {
3267 struct cctl_req_option *option, *next_option;
3270 req.be_args = malloc(num_options * sizeof(*req.be_args));
3271 if (req.be_args == NULL) {
3272 warn("%s: error allocating %zd bytes", __func__,
3273 num_options * sizeof(*req.be_args));
3278 for (i = 0, option = STAILQ_FIRST(&option_list);
3279 i < num_options; i++, option = next_option) {
3280 next_option = STAILQ_NEXT(option, links);
3282 req.be_args[i].namelen = option->namelen;
3283 req.be_args[i].name = strdup(option->name);
3284 req.be_args[i].vallen = option->vallen;
3285 req.be_args[i].value = strdup(option->value);
3287 * XXX KDM do we want a way to specify a writeable
3288 * flag of some sort? Do we want a way to specify
3291 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3293 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3296 free(option->value);
3301 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3302 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3307 if (req.status == CTL_LUN_ERROR) {
3308 warnx("%s: error returned from LUN removal request:\n%s",
3309 __func__, req.error_str);
3312 } else if (req.status != CTL_LUN_OK) {
3313 warnx("%s: unknown LUN removal request status %d",
3314 __func__, req.status);
3319 printf("LUN %d deleted successfully\n", lun_id);
3326 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
3328 struct ctl_lun_req req;
3329 uint64_t lun_size = 0;
3330 uint32_t lun_id = 0;
3331 int lun_id_set = 0, lun_size_set = 0;
3332 char *backend_name = NULL;
3335 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3338 backend_name = strdup(optarg);
3341 lun_id = strtoul(optarg, NULL, 0);
3345 if (strcasecmp(optarg, "auto") != 0) {
3346 retval = expand_number(optarg, &lun_size);
3348 warn("%s: invalid -s argument",
3361 if (backend_name == NULL)
3362 errx(1, "%s: backend name (-b) must be specified", __func__);
3364 if (lun_id_set == 0)
3365 errx(1, "%s: LUN id (-l) must be specified", __func__);
3367 if (lun_size_set == 0)
3368 errx(1, "%s: size (-s) must be specified", __func__);
3370 bzero(&req, sizeof(req));
3372 strlcpy(req.backend, backend_name, sizeof(req.backend));
3373 req.reqtype = CTL_LUNREQ_MODIFY;
3375 req.reqdata.modify.lun_id = lun_id;
3376 req.reqdata.modify.lun_size_bytes = lun_size;
3378 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3379 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3384 if (req.status == CTL_LUN_ERROR) {
3385 warnx("%s: error returned from LUN modification request:\n%s",
3386 __func__, req.error_str);
3389 } else if (req.status != CTL_LUN_OK) {
3390 warnx("%s: unknown LUN modification request status %d",
3391 __func__, req.status);
3396 printf("LUN %d modified successfully\n", lun_id);
3404 * Name/value pair used for per-LUN attributes.
3406 struct cctl_lun_nv {
3409 STAILQ_ENTRY(cctl_lun_nv) links;
3413 * Backend LUN information.
3418 uint64_t size_blocks;
3420 char *serial_number;
3422 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3423 STAILQ_ENTRY(cctl_lun) links;
3426 struct cctl_devlist_data {
3428 STAILQ_HEAD(,cctl_lun) lun_list;
3429 struct cctl_lun *cur_lun;
3431 struct sbuf *cur_sb[32];
3435 cctl_start_element(void *user_data, const char *name, const char **attr)
3438 struct cctl_devlist_data *devlist;
3439 struct cctl_lun *cur_lun;
3441 devlist = (struct cctl_devlist_data *)user_data;
3442 cur_lun = devlist->cur_lun;
3444 if ((u_int)devlist->level > (sizeof(devlist->cur_sb) /
3445 sizeof(devlist->cur_sb[0])))
3446 errx(1, "%s: too many nesting levels, %zd max", __func__,
3447 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3449 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3450 if (devlist->cur_sb[devlist->level] == NULL)
3451 err(1, "%s: Unable to allocate sbuf", __func__);
3453 if (strcmp(name, "lun") == 0) {
3454 if (cur_lun != NULL)
3455 errx(1, "%s: improper lun element nesting", __func__);
3457 cur_lun = calloc(1, sizeof(*cur_lun));
3458 if (cur_lun == NULL)
3459 err(1, "%s: cannot allocate %zd bytes", __func__,
3462 devlist->num_luns++;
3463 devlist->cur_lun = cur_lun;
3465 STAILQ_INIT(&cur_lun->attr_list);
3466 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3468 for (i = 0; attr[i] != NULL; i += 2) {
3469 if (strcmp(attr[i], "id") == 0) {
3470 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3472 errx(1, "%s: invalid LUN attribute %s = %s",
3473 __func__, attr[i], attr[i+1]);
3480 cctl_end_element(void *user_data, const char *name)
3482 struct cctl_devlist_data *devlist;
3483 struct cctl_lun *cur_lun;
3486 devlist = (struct cctl_devlist_data *)user_data;
3487 cur_lun = devlist->cur_lun;
3489 if ((cur_lun == NULL)
3490 && (strcmp(name, "ctllunlist") != 0))
3491 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3493 if (devlist->cur_sb[devlist->level] == NULL)
3494 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3495 devlist->level, name);
3497 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3498 err(1, "%s: sbuf_finish", __func__);
3499 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3501 err(1, "%s can't allocate %zd bytes for string", __func__,
3502 sbuf_len(devlist->cur_sb[devlist->level]));
3504 if (strlen(str) == 0) {
3509 sbuf_delete(devlist->cur_sb[devlist->level]);
3510 devlist->cur_sb[devlist->level] = NULL;
3513 if (strcmp(name, "backend_type") == 0) {
3514 cur_lun->backend_type = str;
3516 } else if (strcmp(name, "size") == 0) {
3517 cur_lun->size_blocks = strtoull(str, NULL, 0);
3518 } else if (strcmp(name, "blocksize") == 0) {
3519 cur_lun->blocksize = strtoul(str, NULL, 0);
3520 } else if (strcmp(name, "serial_number") == 0) {
3521 cur_lun->serial_number = str;
3523 } else if (strcmp(name, "device_id") == 0) {
3524 cur_lun->device_id = str;
3526 } else if (strcmp(name, "lun") == 0) {
3527 devlist->cur_lun = NULL;
3528 } else if (strcmp(name, "ctllunlist") == 0) {
3531 struct cctl_lun_nv *nv;
3533 nv = calloc(1, sizeof(*nv));
3535 err(1, "%s: can't allocate %zd bytes for nv pair",
3536 __func__, sizeof(*nv));
3538 nv->name = strdup(name);
3539 if (nv->name == NULL)
3540 err(1, "%s: can't allocated %zd bytes for string",
3541 __func__, strlen(name));
3545 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3552 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3554 struct cctl_devlist_data *devlist;
3556 devlist = (struct cctl_devlist_data *)user_data;
3558 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3562 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3564 struct ctl_lun_list list;
3565 struct cctl_devlist_data devlist;
3566 struct cctl_lun *lun;
3572 char *backend = NULL;
3578 bzero(&devlist, sizeof(devlist));
3579 STAILQ_INIT(&devlist.lun_list);
3581 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3584 backend = strdup(optarg);
3598 lun_str = malloc(lun_len);
3600 bzero(&list, sizeof(list));
3601 list.alloc_len = lun_len;
3602 list.status = CTL_LUN_LIST_NONE;
3603 list.lun_xml = lun_str;
3605 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
3606 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
3611 if (list.status == CTL_LUN_LIST_ERROR) {
3612 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
3613 __func__, list.error_str);
3614 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3615 lun_len = lun_len << 1;
3619 if (dump_xml != 0) {
3620 printf("%s", lun_str);
3624 parser = XML_ParserCreate(NULL);
3625 if (parser == NULL) {
3626 warn("%s: Unable to create XML parser", __func__);
3631 XML_SetUserData(parser, &devlist);
3632 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
3633 XML_SetCharacterDataHandler(parser, cctl_char_handler);
3635 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
3636 XML_ParserFree(parser);
3642 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
3643 "Serial Number", "Device ID");
3644 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
3645 struct cctl_lun_nv *nv;
3647 if ((backend != NULL)
3648 && (strcmp(lun->backend_type, backend) != 0))
3651 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
3652 (uintmax_t)lun->lun_id,
3653 lun->backend_type, (uintmax_t)lun->size_blocks,
3654 lun->blocksize, lun->serial_number, lun->device_id);
3659 STAILQ_FOREACH(nv, &lun->attr_list, links) {
3660 printf(" %s=%s\n", nv->name, nv->value);
3672 fprintf(error ? stderr : stdout,
3674 "Primary commands:\n"
3675 " ctladm tur [dev_id][general options]\n"
3676 " ctladm inquiry [dev_id][general options]\n"
3677 " ctladm devid [dev_id][general options]\n"
3678 " ctladm reqsense [dev_id][general options]\n"
3679 " ctladm reportluns [dev_id][general options]\n"
3680 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
3681 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3682 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
3683 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3684 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
3685 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
3686 " [-d] [-S subpage] [-c cdbsize]\n"
3687 " ctladm prin [dev_id][general options] <-a action>\n"
3688 " ctladm prout [dev_id][general options] <-a action>\n"
3689 " <-r restype] [-k key] [-s sa_key]\n"
3690 " ctladm rtpg [dev_id][general options]\n"
3691 " ctladm start [dev_id][general options] [-i] [-o]\n"
3692 " ctladm stop [dev_id][general options] [-i] [-o]\n"
3693 " ctladm synccache [dev_id][general options] [-l lba]\n"
3694 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
3695 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
3696 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
3697 " [-S serial_num] [-t dev_type]\n"
3698 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
3699 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
3700 " ctladm devlist [-b][-v][-x]\n"
3701 " ctladm shutdown\n"
3703 " ctladm hardstop\n"
3704 " ctladm hardstart\n"
3706 " ctladm bbrread [dev_id] <-l lba> <-d datalen>\n"
3707 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
3709 " ctladm realsync <on|off|query>\n"
3710 " ctladm setsync [dev_id] <-i interval>\n"
3711 " ctladm getsync [dev_id]\n"
3712 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
3713 " [-s len fmt [args]] [-c] [-d delete_id]\n"
3714 " ctladm port <-l | -o <on|off> | [-w wwnn][-W wwpn]>\n"
3715 " [-p targ_port] [-t port_type] [-q] [-x]\n"
3717 " ctladm dumpstructs\n"
3719 "General Options:\n"
3720 "-I intiator_id : defaults to 7, used to change the initiator id\n"
3721 "-C retries : specify the number of times to retry this command\n"
3722 "-D devicename : specify the device to operate on\n"
3723 " : (default is %s)\n"
3724 "read/write options:\n"
3725 "-l lba : logical block address\n"
3726 "-d len : read/write length, in blocks\n"
3727 "-f file|- : write/read data to/from file or stdout/stdin\n"
3728 "-b blocksize : block size, in bytes\n"
3729 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
3730 "-N : do not copy data to/from userland\n"
3731 "readcapacity options:\n"
3732 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3733 "modesense options:\n"
3734 "-m page : specify the mode page to view\n"
3735 "-l : request a list of supported pages\n"
3736 "-P pc : specify the page control value: 0-3 (current,\n"
3737 " changeable, default, saved, respectively)\n"
3738 "-d : disable block descriptors for mode sense\n"
3739 "-S subpage : specify a subpage\n"
3740 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
3741 "persistent reserve in options:\n"
3742 "-a action : specify the action value: 0-2 (read key, read\n"
3743 " reservation, read capabilities, respectively)\n"
3744 "persistent reserve out options:\n"
3745 "-a action : specify the action value: 0-5 (register, reserve,\n"
3746 " release, clear, preempt, register and ignore)\n"
3747 "-k key : key value\n"
3748 "-s sa_key : service action value\n"
3749 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
3750 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
3751 "start/stop options:\n"
3752 "-i : set the immediate bit (CTL does not support this)\n"
3753 "-o : set the on/offline bit\n"
3754 "synccache options:\n"
3755 "-l lba : set the starting LBA\n"
3756 "-b blockcount : set the length to sync in blocks\n"
3757 "-r : set the relative addressing bit\n"
3758 "-i : set the immediate bit\n"
3759 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3761 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3762 "-B blocksize : LUN blocksize in bytes (some backends)\n"
3763 "-d device_id : SCSI VPD page 0x83 ID\n"
3764 "-l lun_id : requested LUN number\n"
3765 "-o name=value : backend-specific options, multiple allowed\n"
3766 "-s size_bytes : LUN size in bytes (some backends)\n"
3767 "-S serial_num : SCSI VPD page 0x80 serial number\n"
3768 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
3770 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3771 "-l lun_id : LUN number to delete\n"
3772 "-o name=value : backend-specific options, multiple allowed\n"
3773 "devlist options:\n"
3774 "-b backend : list devices from specified backend only\n"
3775 "-v : be verbose, show backend attributes\n"
3776 "-x : dump raw XML\n"
3778 "-l datamove|done : delay command at datamove or done phase\n"
3779 "-T oneshot : delay one command, then resume normal completion\n"
3780 "-T cont : delay all commands\n"
3781 "-t secs : number of seconds to delay\n"
3783 "-i error_action : action to perform\n"
3784 "-p pattern : command pattern to look for\n"
3785 "-r lba,len : LBA range for pattern\n"
3786 "-s len fmt [args] : sense data for custom sense action\n"
3787 "-c : continuous operation\n"
3788 "-d delete_id : error id to delete\n"
3790 "-l : list frontend ports\n"
3791 "-o on|off : turn frontend ports on or off\n"
3792 "-w wwnn : set WWNN for one frontend\n"
3793 "-W wwpn : set WWPN for one frontend\n"
3794 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
3795 "-p targ_port : specify target port number\n"
3796 "-q : omit header in list output\n"
3797 "-x : output port list in XML format\n"
3798 "bbrread options:\n"
3799 "-l lba : starting LBA\n"
3800 "-d datalen : length, in bytes, to read\n",
3805 main(int argc, char **argv)
3808 ctladm_cmdfunction command;
3809 ctladm_cmdargs cmdargs;
3810 ctladm_optret optreturn;
3812 const char *mainopt = "C:D:I:";
3813 const char *subopt = NULL;
3814 char combinedopt[256];
3823 cmdargs = CTLADM_ARG_NONE;
3824 command = CTLADM_CMD_HELP;
3839 * Get the base option.
3841 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
3843 if (optreturn == CC_OR_AMBIGUOUS) {
3844 warnx("ambiguous option %s", argv[1]);
3847 } else if (optreturn == CC_OR_NOT_FOUND) {
3848 warnx("option %s not found", argv[1]);
3853 if (cmdargs & CTLADM_ARG_NEED_TL) {
3855 || (!isdigit(argv[2][0]))) {
3856 warnx("option %s requires a target:lun argument",
3861 retval = cctl_parse_tl(argv[2], &target, &lun);
3863 errx(1, "invalid target:lun argument %s", argv[2]);
3865 cmdargs |= CTLADM_ARG_TARG_LUN;
3870 * Ahh, getopt(3) is a pain.
3872 * This is a gross hack. There really aren't many other good
3873 * options (excuse the pun) for parsing options in a situation like
3874 * this. getopt is kinda braindead, so you end up having to run
3875 * through the options twice, and give each invocation of getopt
3876 * the option string for the other invocation.
3878 * You would think that you could just have two groups of options.
3879 * The first group would get parsed by the first invocation of
3880 * getopt, and the second group would get parsed by the second
3881 * invocation of getopt. It doesn't quite work out that way. When
3882 * the first invocation of getopt finishes, it leaves optind pointing
3883 * to the argument _after_ the first argument in the second group.
3884 * So when the second invocation of getopt comes around, it doesn't
3885 * recognize the first argument it gets and then bails out.
3887 * A nice alternative would be to have a flag for getopt that says
3888 * "just keep parsing arguments even when you encounter an unknown
3889 * argument", but there isn't one. So there's no real clean way to
3890 * easily parse two sets of arguments without having one invocation
3891 * of getopt know about the other.
3893 * Without this hack, the first invocation of getopt would work as
3894 * long as the generic arguments are first, but the second invocation
3895 * (in the subfunction) would fail in one of two ways. In the case
3896 * where you don't set optreset, it would fail because optind may be
3897 * pointing to the argument after the one it should be pointing at.
3898 * In the case where you do set optreset, and reset optind, it would
3899 * fail because getopt would run into the first set of options, which
3900 * it doesn't understand.
3902 * All of this would "sort of" work if you could somehow figure out
3903 * whether optind had been incremented one option too far. The
3904 * mechanics of that, however, are more daunting than just giving
3905 * both invocations all of the expect options for either invocation.
3907 * Needless to say, I wouldn't mind if someone invented a better
3908 * (non-GPL!) command line parsing interface than getopt. I
3909 * wouldn't mind if someone added more knobs to getopt to make it
3910 * work better. Who knows, I may talk myself into doing it someday,
3911 * if the standards weenies let me. As it is, it just leads to
3912 * hackery like this and causes people to avoid it in some cases.
3914 * KDM, September 8th, 1998
3917 sprintf(combinedopt, "%s%s", mainopt, subopt);
3919 sprintf(combinedopt, "%s", mainopt);
3922 * Start getopt processing at argv[2/3], since we've already
3923 * accepted argv[1..2] as the command name, and as a possible
3929 * Now we run through the argument list looking for generic
3930 * options, and ignoring options that possibly belong to
3933 while ((c = getopt(argc, argv, combinedopt))!= -1){
3936 cmdargs |= CTLADM_ARG_RETRIES;
3937 retries = strtol(optarg, NULL, 0);
3940 device = strdup(optarg);
3941 cmdargs |= CTLADM_ARG_DEVICE;
3944 cmdargs |= CTLADM_ARG_INITIATOR;
3945 initid = strtol(optarg, NULL, 0);
3952 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
3959 * Default to opening the CTL device for now.
3961 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
3962 && (command != CTLADM_CMD_HELP)) {
3963 device = strdup(CTL_DEFAULT_DEV);
3964 cmdargs |= CTLADM_ARG_DEVICE;
3967 if ((cmdargs & CTLADM_ARG_DEVICE)
3968 && (command != CTLADM_CMD_HELP)) {
3969 fd = open(device, O_RDWR);
3970 if (fd == -1 && errno == ENOENT) {
3971 saved_errno = errno;
3972 retval = kldload("ctl");
3974 fd = open(device, O_RDWR);
3976 errno = saved_errno;
3979 fprintf(stderr, "%s: error opening %s: %s\n",
3980 argv[0], device, strerror(errno));
3984 } else if ((command != CTLADM_CMD_HELP)
3985 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
3986 fprintf(stderr, "%s: you must specify a device with the "
3987 "--device argument for this command\n", argv[0]);
3988 command = CTLADM_CMD_HELP;
3993 case CTLADM_CMD_TUR:
3994 retval = cctl_tur(fd, target, lun, initid, retries);
3996 case CTLADM_CMD_INQUIRY:
3997 retval = cctl_inquiry(fd, target, lun, initid, retries);
3999 case CTLADM_CMD_REQ_SENSE:
4000 retval = cctl_req_sense(fd, target, lun, initid, retries);
4002 case CTLADM_CMD_REPORT_LUNS:
4003 retval = cctl_report_luns(fd, target, lun, initid, retries);
4005 case CTLADM_CMD_CREATE:
4006 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4009 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4011 case CTLADM_CMD_DEVLIST:
4012 retval = cctl_devlist(fd, argc, argv, combinedopt);
4014 case CTLADM_CMD_READ:
4015 case CTLADM_CMD_WRITE:
4016 retval = cctl_read_write(fd, target, lun, initid, retries,
4017 argc, argv, combinedopt, command);
4019 case CTLADM_CMD_PORT:
4020 retval = cctl_port(fd, argc, argv, combinedopt);
4022 case CTLADM_CMD_READCAPACITY:
4023 retval = cctl_read_capacity(fd, target, lun, initid, retries,
4024 argc, argv, combinedopt);
4026 case CTLADM_CMD_MODESENSE:
4027 retval = cctl_mode_sense(fd, target, lun, initid, retries,
4028 argc, argv, combinedopt);
4030 case CTLADM_CMD_START:
4031 case CTLADM_CMD_STOP:
4032 retval = cctl_start_stop(fd, target, lun, initid, retries,
4033 (command == CTLADM_CMD_START) ? 1 : 0,
4034 argc, argv, combinedopt);
4036 case CTLADM_CMD_SYNC_CACHE:
4037 retval = cctl_sync_cache(fd, target, lun, initid, retries,
4038 argc, argv, combinedopt);
4040 case CTLADM_CMD_SHUTDOWN:
4041 case CTLADM_CMD_STARTUP:
4042 retval = cctl_startup_shutdown(fd, target, lun, initid,
4045 case CTLADM_CMD_HARDSTOP:
4046 case CTLADM_CMD_HARDSTART:
4047 retval = cctl_hardstopstart(fd, command);
4049 case CTLADM_CMD_BBRREAD:
4050 retval = cctl_bbrread(fd, target, lun, initid, argc, argv,
4053 case CTLADM_CMD_LUNLIST:
4054 retval = cctl_lunlist(fd);
4056 case CTLADM_CMD_DELAY:
4057 retval = cctl_delay(fd, target, lun, argc, argv, combinedopt);
4059 case CTLADM_CMD_REALSYNC:
4060 retval = cctl_realsync(fd, argc, argv);
4062 case CTLADM_CMD_SETSYNC:
4063 case CTLADM_CMD_GETSYNC:
4064 retval = cctl_getsetsync(fd, target, lun, command,
4065 argc, argv, combinedopt);
4067 case CTLADM_CMD_ERR_INJECT:
4068 retval = cctl_error_inject(fd, target, lun, argc, argv,
4071 case CTLADM_CMD_DUMPOOA:
4072 retval = cctl_dump_ooa(fd, argc, argv);
4074 case CTLADM_CMD_DUMPSTRUCTS:
4075 retval = cctl_dump_structs(fd, cmdargs);
4077 case CTLADM_CMD_PRES_IN:
4078 retval = cctl_persistent_reserve_in(fd, target, lun, initid,
4079 argc, argv, combinedopt,
4082 case CTLADM_CMD_PRES_OUT:
4083 retval = cctl_persistent_reserve_out(fd, target, lun, initid,
4084 argc, argv, combinedopt,
4087 case CTLADM_CMD_INQ_VPD_DEVID:
4088 retval = cctl_inquiry_vpd_devid(fd, target, lun, initid);
4090 case CTLADM_CMD_RTPG:
4091 retval = cctl_report_target_port_group(fd, target, lun, initid);
4093 case CTLADM_CMD_MODIFY:
4094 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4096 case CTLADM_CMD_HELP: