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,
99 CTLADM_CMD_READCAPACITY,
100 CTLADM_CMD_MODESENSE,
102 CTLADM_CMD_DUMPSTRUCTS,
105 CTLADM_CMD_SYNC_CACHE,
110 CTLADM_CMD_HARDSTART,
115 CTLADM_CMD_ERR_INJECT,
119 CTLADM_CMD_INQ_VPD_DEVID,
124 CTLADM_CMD_ISTERMINATE
125 } ctladm_cmdfunction;
128 CTLADM_ARG_NONE = 0x0000000,
129 CTLADM_ARG_AUTOSENSE = 0x0000001,
130 CTLADM_ARG_DEVICE = 0x0000002,
131 CTLADM_ARG_ARRAYSIZE = 0x0000004,
132 CTLADM_ARG_BACKEND = 0x0000008,
133 CTLADM_ARG_CDBSIZE = 0x0000010,
134 CTLADM_ARG_DATALEN = 0x0000020,
135 CTLADM_ARG_FILENAME = 0x0000040,
136 CTLADM_ARG_LBA = 0x0000080,
137 CTLADM_ARG_PC = 0x0000100,
138 CTLADM_ARG_PAGE_CODE = 0x0000200,
139 CTLADM_ARG_PAGE_LIST = 0x0000400,
140 CTLADM_ARG_SUBPAGE = 0x0000800,
141 CTLADM_ARG_PAGELIST = 0x0001000,
142 CTLADM_ARG_DBD = 0x0002000,
143 CTLADM_ARG_TARG_LUN = 0x0004000,
144 CTLADM_ARG_BLOCKSIZE = 0x0008000,
145 CTLADM_ARG_IMMED = 0x0010000,
146 CTLADM_ARG_RELADR = 0x0020000,
147 CTLADM_ARG_RETRIES = 0x0040000,
148 CTLADM_ARG_ONOFFLINE = 0x0080000,
149 CTLADM_ARG_ONESHOT = 0x0100000,
150 CTLADM_ARG_TIMEOUT = 0x0200000,
151 CTLADM_ARG_INITIATOR = 0x0400000,
152 CTLADM_ARG_NOCOPY = 0x0800000,
153 CTLADM_ARG_NEED_TL = 0x1000000
159 ctladm_cmdargs argnum;
169 static const char rw_opts[] = "Nb:c:d:f:l:";
170 static const char startstop_opts[] = "io";
172 static struct ctladm_opts option_table[] = {
173 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
174 {"bbrread", CTLADM_CMD_BBRREAD, CTLADM_ARG_NEED_TL, "d:l:"},
175 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
176 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
177 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
178 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
179 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
180 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
181 {"getsync", CTLADM_CMD_GETSYNC, CTLADM_ARG_NEED_TL, NULL},
182 {"hardstart", CTLADM_CMD_HARDSTART, CTLADM_ARG_NONE, NULL},
183 {"hardstop", CTLADM_CMD_HARDSTOP, CTLADM_ARG_NONE, NULL},
184 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
185 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
186 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
187 {"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"},
188 {"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"},
189 {"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"},
190 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
191 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
192 {"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:s:"},
193 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
194 {"portlist", CTLADM_CMD_PORTLIST, CTLADM_ARG_NONE, "f:vx"},
195 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
196 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
197 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
198 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
199 {"realsync", CTLADM_CMD_REALSYNC, CTLADM_ARG_NONE, NULL},
200 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
201 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
202 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
203 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
204 {"setsync", CTLADM_CMD_SETSYNC, CTLADM_ARG_NEED_TL, "i:"},
205 {"shutdown", CTLADM_CMD_SHUTDOWN, CTLADM_ARG_NONE, NULL},
206 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
207 {"startup", CTLADM_CMD_STARTUP, CTLADM_ARG_NONE, NULL},
208 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
209 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
210 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
211 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
212 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
213 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
218 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
219 ctladm_cmdargs *argnum, const char **subopt);
220 static int cctl_parse_tl(char *str, int *target, int *lun);
221 static int cctl_dump_ooa(int fd, int argc, char **argv);
222 static int cctl_port_dump(int fd, int quiet, int xml, int32_t fe_num,
223 ctl_port_type port_type);
224 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
225 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
226 static int cctl_delay(int fd, int target, int lun, int argc, char **argv,
228 static int cctl_lunlist(int fd);
229 static void cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len);
230 static void cctl_cfi_bbr_statusstr(cfi_bbrread_status, char *str, int str_len);
231 static int cctl_hardstopstart(int fd, ctladm_cmdfunction command);
232 static int cctl_bbrread(int fd, int target, int lun, int iid, int argc,
233 char **argv, char *combinedopt);
234 static int cctl_startup_shutdown(int fd, int target, int lun, int iid,
235 ctladm_cmdfunction command);
236 static int cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
237 int argc, char **argv, char *combinedopt);
238 static int cctl_start_stop(int fd, int target, int lun, int iid, int retries,
239 int start, int argc, char **argv, char *combinedopt);
240 static int cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
241 int argc, char **argv, char *combinedopt);
242 static int cctl_read_capacity(int fd, int target, int lun, int iid,
243 int retries, int argc, char **argv,
245 static int cctl_read_write(int fd, int target, int lun, int iid, int retries,
246 int argc, char **argv, char *combinedopt,
247 ctladm_cmdfunction command);
248 static int cctl_get_luns(int fd, int target, int lun, int iid, int retries,
249 struct scsi_report_luns_data **lun_data,
251 static int cctl_report_luns(int fd, int target, int lun, int iid, int retries);
252 static int cctl_tur(int fd, int target, int lun, int iid, int retries);
253 static int cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
254 char *path_str, int path_len,
255 struct scsi_inquiry_data *inq_data);
256 static int cctl_inquiry(int fd, int target, int lun, int iid, int retries);
257 static int cctl_req_sense(int fd, int target, int lun, int iid, int retries);
258 static int cctl_persistent_reserve_in(int fd, int target, int lun,
259 int initiator, int argc, char **argv,
260 char *combinedopt, int retry_count);
261 static int cctl_persistent_reserve_out(int fd, int target, int lun,
262 int initiator, int argc, char **argv,
263 char *combinedopt, int retry_count);
264 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
265 static int cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator);
266 static int cctl_report_target_port_group(int fd, int target, int lun,
268 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
271 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
272 ctladm_cmdargs *argnum, const char **subopt)
274 struct ctladm_opts *opts;
277 for (opts = table; (opts != NULL) && (opts->optname != NULL);
279 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
280 *cmdnum = opts->cmdnum;
281 *argnum = opts->argnum;
282 *subopt = opts->subopt;
284 if (strcmp(opts->optname, arg) == 0)
285 return (CC_OR_FOUND);
287 if (++num_matches > 1)
288 return(CC_OR_AMBIGUOUS);
295 return(CC_OR_NOT_FOUND);
300 cctl_parse_tl(char *str, int *target, int *lun)
307 while (isspace(*str) && (*str != '\0'))
310 tmpstr = (char *)strtok(str, ":");
311 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
312 *target = strtol(tmpstr, NULL, 0);
313 tmpstr = (char *)strtok(NULL, ":");
314 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
315 *lun = strtol(tmpstr, NULL, 0);
325 cctl_dump_ooa(int fd, int argc, char **argv)
328 long double cmd_latency;
329 int num_entries, len;
330 int target = -1, lun = -1;
337 && (isdigit(argv[2][0]))) {
338 retval = cctl_parse_tl(argv[2], &target, &lun);
340 warnx("invalid target:lun argument %s", argv[2]);
344 len = num_entries * sizeof(struct ctl_ooa_entry);
346 bzero(&ooa, sizeof(ooa));
348 ooa.entries = malloc(len);
350 if (ooa.entries == NULL) {
351 warn("%s: error mallocing %d bytes", __func__, len);
358 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
361 ooa.alloc_num = num_entries;
362 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
363 warn("%s: CTL_GET_OOA ioctl failed", __func__);
368 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
369 num_entries = num_entries * 2;
375 if (ooa.status != CTL_OOA_OK) {
376 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
382 fprintf(stdout, "Dumping OOA queues\n");
383 for (i = 0; i < ooa.fill_num; i++) {
384 struct ctl_ooa_entry *entry;
385 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
386 struct bintime delta_bt;
389 entry = &ooa.entries[i];
391 delta_bt = ooa.cur_bt;
392 bintime_sub(&delta_bt, &entry->start_bt);
393 bintime2timespec(&delta_bt, &ts);
394 cmd_latency = ts.tv_sec * 1000;
396 cmd_latency += ts.tv_nsec / 1000000;
398 fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
400 (intmax_t)entry->lun_num, entry->tag_num,
401 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
403 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
404 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
406 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
408 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
409 scsi_op_desc(entry->cdb[0], NULL),
410 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
413 fprintf(stdout, "OOA queues dump done\n");
415 if (ioctl(fd, CTL_DUMP_OOA) == -1) {
416 warn("%s: CTL_DUMP_OOA ioctl failed", __func__);
428 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
430 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
438 cctl_port_dump(int fd, int quiet, int xml, int32_t targ_port,
439 ctl_port_type port_type)
441 struct ctl_port_list port_list;
442 struct ctl_port_entry *entries;
443 struct sbuf *sb = NULL;
452 entries = malloc(sizeof(*entries) * num_entries);
453 bzero(&port_list, sizeof(port_list));
454 port_list.entries = entries;
455 port_list.alloc_num = num_entries;
456 port_list.alloc_len = num_entries * sizeof(*entries);
457 if (ioctl(fd, CTL_GET_PORT_LIST, &port_list) != 0) {
458 warn("%s: CTL_GET_PORT_LIST ioctl failed", __func__);
461 if (port_list.status == CTL_PORT_LIST_NEED_MORE_SPACE) {
462 printf("%s: allocated %d, need %d, retrying\n", __func__,
463 num_entries, port_list.fill_num + port_list.dropped_num);
465 num_entries = port_list.fill_num + port_list.dropped_num;
471 printf("Port Online Type Name pp vp %-18s %-18s\n",
475 sb = sbuf_new_auto();
476 sbuf_printf(sb, "<ctlfelist>\n");
478 for (i = 0; i < port_list.fill_num; i++) {
479 struct ctl_port_entry *entry;
484 switch (entry->port_type) {
494 case CTL_PORT_INTERNAL:
512 * If the user specified a frontend number or a particular
513 * frontend type, only print out that particular frontend
516 if ((targ_port != -1)
517 && (targ_port != entry->targ_port))
519 else if ((port_type != CTL_PORT_NONE)
520 && ((port_type & entry->port_type) == 0))
526 printf("Num: %ju Type: %s (%#x) Name: %s Physical Port: %d "
527 "Virtual Port: %d\n", (uintmax_t)entry->fe_num, type,
528 entry->port_type, entry->fe_name, entry->physical_port,
529 entry->virtual_port);
530 printf("WWNN %#jx WWPN %#jx Online: %s\n",
531 (uintmax_t)entry->wwnn, (uintmax_t)entry->wwpn,
532 (entry->online) ? "YES" : "NO" );
535 printf("%-4d %-6s %-8s %-12s %-2d %-2d %#-18jx "
537 entry->targ_port, (entry->online) ? "YES" : "NO",
538 type, entry->port_name, entry->physical_port,
539 entry->virtual_port, (uintmax_t)entry->wwnn,
540 (uintmax_t)entry->wwpn);
542 sbuf_printf(sb, "<targ_port id=\"%d\">\n",
544 sbuf_printf(sb, "<online>%s</online>\n",
545 (entry->online) ? "YES" : "NO");
546 sbuf_printf(sb, "<port_type>%s</port_type>\n", type);
547 sbuf_printf(sb, "<port_name>%s</port_name>\n",
549 sbuf_printf(sb, "<physical_port>%d</physical_port>\n",
550 entry->physical_port);
551 sbuf_printf(sb, "<virtual_port>%d</virtual_port>\n",
552 entry->virtual_port);
553 sbuf_printf(sb, "<wwnn>%#jx</wwnn>\n",
554 (uintmax_t)entry->wwnn);
555 sbuf_printf(sb, "<wwpn>%#jx</wwpn>\n",
556 (uintmax_t)entry->wwpn);
557 sbuf_printf(sb, "</targ_port>\n");
562 sbuf_printf(sb, "</ctlfelist>\n");
563 if (sbuf_finish(sb) != 0)
564 err(1, "%s: sbuf_finish", __func__);
565 printf("%s", sbuf_data(sb));
570 * Give some indication that we didn't find the frontend or
571 * frontend type requested by the user. We could print something
572 * out, but it would probably be better to hide that behind a
576 && ((targ_port != -1)
577 || (port_type != CTL_PORT_NONE)))
591 static struct ctladm_opts cctl_fe_table[] = {
592 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
593 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
594 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
595 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
596 {"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
597 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
602 cctl_port(int fd, int argc, char **argv, char *combinedopt)
605 int32_t targ_port = -1;
607 int wwnn_set = 0, wwpn_set = 0;
608 uint64_t wwnn = 0, wwpn = 0;
609 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
610 struct ctl_port_entry entry;
611 ctl_port_type port_type = CTL_PORT_NONE;
612 int quiet = 0, xml = 0;
614 while ((c = getopt(argc, argv, combinedopt)) != -1) {
617 if (port_mode != CCTL_PORT_MODE_NONE)
620 port_mode = CCTL_PORT_MODE_LIST;
623 if (port_mode != CCTL_PORT_MODE_NONE)
626 if (strcasecmp(optarg, "on") == 0)
627 port_mode = CCTL_PORT_MODE_ON;
628 else if (strcasecmp(optarg, "off") == 0)
629 port_mode = CCTL_PORT_MODE_OFF;
631 warnx("Invalid -o argument %s, \"on\" or "
632 "\"off\" are the only valid args",
639 targ_port = strtol(optarg, NULL, 0);
645 ctladm_optret optret;
646 ctladm_cmdargs argnum;
648 ctl_port_type tmp_port_type;
650 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
652 if (optret == CC_OR_AMBIGUOUS) {
653 warnx("%s: ambiguous frontend type %s",
657 } else if (optret == CC_OR_NOT_FOUND) {
658 warnx("%s: invalid frontend type %s",
664 port_type |= tmp_port_type;
668 if ((port_mode != CCTL_PORT_MODE_NONE)
669 && (port_mode != CCTL_PORT_MODE_SET))
672 port_mode = CCTL_PORT_MODE_SET;
674 wwnn = strtoull(optarg, NULL, 0);
678 if ((port_mode != CCTL_PORT_MODE_NONE)
679 && (port_mode != CCTL_PORT_MODE_SET))
682 port_mode = CCTL_PORT_MODE_SET;
684 wwpn = strtoull(optarg, NULL, 0);
694 * The user can specify either one or more frontend types (-t), or
695 * a specific frontend, but not both.
697 * If the user didn't specify a frontend type or number, set it to
698 * all. This is primarily needed for the enable/disable ioctls.
699 * This will be a no-op for the listing code. For the set ioctl,
700 * we'll throw an error, since that only works on one port at a time.
702 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
703 warnx("%s: can only specify one of -t or -n", __func__);
706 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
707 port_type = CTL_PORT_ALL;
709 bzero(&entry, sizeof(entry));
712 * These are needed for all but list/dump mode.
714 entry.port_type = port_type;
715 entry.targ_port = targ_port;
718 case CCTL_PORT_MODE_LIST:
719 cctl_port_dump(fd, quiet, xml, targ_port, port_type);
721 case CCTL_PORT_MODE_SET:
722 if (targ_port == -1) {
723 warnx("%s: -w and -W require -n", __func__);
729 entry.flags |= CTL_PORT_WWNN_VALID;
733 entry.flags |= CTL_PORT_WWPN_VALID;
737 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
738 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
743 case CCTL_PORT_MODE_ON:
744 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
745 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
749 fprintf(stdout, "Front End Ports enabled\n");
751 case CCTL_PORT_MODE_OFF:
752 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
753 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
757 fprintf(stdout, "Front End Ports disabled\n");
760 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
771 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
776 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
779 if (ioctl(fd, CTL_IO, io) == -1) {
780 warn("%s: error sending CTL_IO ioctl", func);
783 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
790 cctl_delay(int fd, int target, int lun, int argc, char **argv,
793 struct ctl_io_delay_info delay_info;
794 char *delayloc = NULL;
795 char *delaytype = NULL;
802 memset(&delay_info, 0, sizeof(delay_info));
804 while ((c = getopt(argc, argv, combinedopt)) != -1) {
807 delaytype = strdup(optarg);
810 delayloc = strdup(optarg);
813 delaytime = strtoul(optarg, NULL, 0);
818 if (delaytime == -1) {
819 warnx("%s: you must specify the delaytime with -t", __func__);
824 if (strcasecmp(delayloc, "datamove") == 0)
825 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
826 else if (strcasecmp(delayloc, "done") == 0)
827 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
829 warnx("%s: invalid delay location %s", __func__, delayloc);
834 if ((delaytype == NULL)
835 || (strcmp(delaytype, "oneshot") == 0))
836 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
837 else if (strcmp(delaytype, "cont") == 0)
838 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
840 warnx("%s: invalid delay type %s", __func__, delaytype);
845 delay_info.target_id = target;
846 delay_info.lun_id = lun;
847 delay_info.delay_secs = delaytime;
849 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
850 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
854 switch (delay_info.status) {
855 case CTL_DELAY_STATUS_NONE:
856 warnx("%s: no delay status??", __func__);
859 case CTL_DELAY_STATUS_OK:
861 case CTL_DELAY_STATUS_INVALID_LUN:
862 warnx("%s: invalid lun %d", __func__, lun);
865 case CTL_DELAY_STATUS_INVALID_TYPE:
866 warnx("%s: invalid delay type %d", __func__,
867 delay_info.delay_type);
870 case CTL_DELAY_STATUS_INVALID_LOC:
871 warnx("%s: delay location %s not implemented?", __func__,
875 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
876 warnx("%s: delay not implemented in the kernel", __func__);
877 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
881 warnx("%s: unknown delay return status %d", __func__,
888 /* delayloc should never be NULL, but just in case...*/
889 if (delayloc != NULL)
896 cctl_realsync(int fd, int argc, char **argv)
905 warnx("%s %s takes exactly one argument", argv[0], argv[1]);
912 if (strncasecmp(syncarg, "query", min(strlen(syncarg),
913 strlen("query"))) == 0) {
914 if (ioctl(fd, CTL_REALSYNC_GET, &syncstate) == -1) {
915 warn("%s: CTL_REALSYNC_GET ioctl failed", __func__);
919 fprintf(stdout, "SYNCHRONIZE CACHE support is: ");
922 fprintf(stdout, "OFF\n");
925 fprintf(stdout, "ON\n");
928 fprintf(stdout, "unknown (%d)\n", syncstate);
932 } else if (strcasecmp(syncarg, "on") == 0) {
934 } else if (strcasecmp(syncarg, "off") == 0) {
937 warnx("%s: invalid realsync argument %s", __func__, syncarg);
942 if (ioctl(fd, CTL_REALSYNC_SET, &syncstate) == -1) {
943 warn("%s: CTL_REALSYNC_SET ioctl failed", __func__);
952 cctl_getsetsync(int fd, int target, int lun, ctladm_cmdfunction command,
953 int argc, char **argv, char *combinedopt)
955 struct ctl_sync_info sync_info;
957 int sync_interval = -1;
963 memset(&sync_info, 0, sizeof(sync_info));
964 sync_info.target_id = target;
965 sync_info.lun_id = lun;
967 while ((c = getopt(argc, argv, combinedopt)) != -1) {
970 sync_interval = strtoul(optarg, NULL, 0);
977 if (command == CTLADM_CMD_SETSYNC) {
978 if (sync_interval == -1) {
979 warnx("%s: you must specify the sync interval with -i",
984 sync_info.sync_interval = sync_interval;
985 ioctl_cmd = CTL_SETSYNC;
987 ioctl_cmd = CTL_GETSYNC;
990 if (ioctl(fd, ioctl_cmd, &sync_info) == -1) {
991 warn("%s: CTL_%sSYNC ioctl failed", __func__,
992 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET");
997 switch (sync_info.status) {
999 if (command == CTLADM_CMD_GETSYNC) {
1000 fprintf(stdout, "%d:%d: sync interval: %d\n",
1001 target, lun, sync_info.sync_interval);
1004 case CTL_GS_SYNC_NO_LUN:
1005 warnx("%s: unknown target:LUN %d:%d", __func__, target, lun);
1008 case CTL_GS_SYNC_NONE:
1010 warnx("%s: unknown CTL_%sSYNC status %d", __func__,
1011 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET",
1020 static struct ctladm_opts cctl_err_types[] = {
1021 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
1022 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
1023 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
1024 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
1029 static struct ctladm_opts cctl_err_patterns[] = {
1030 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
1031 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
1032 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1033 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1034 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
1035 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
1036 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
1038 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
1044 cctl_error_inject(int fd, uint32_t target, uint32_t lun, int argc, char **argv,
1048 struct ctl_error_desc err_desc;
1051 uint64_t delete_id = 0;
1052 int delete_id_set = 0;
1058 bzero(&err_desc, sizeof(err_desc));
1059 err_desc.target_id = target;
1060 err_desc.lun_id = lun;
1062 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1068 delete_id = strtoull(optarg, NULL, 0);
1073 ctladm_optret optret;
1074 ctladm_cmdargs argnum;
1078 ctl_lun_error err_type;
1080 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
1081 warnx("%s: can't specify multiple -i "
1082 "arguments", __func__);
1086 optret = getoption(cctl_err_types, optarg,
1087 &err_type, &argnum, &subopt);
1088 err_desc.lun_error = err_type;
1090 ctl_lun_error_pattern pattern;
1092 optret = getoption(cctl_err_patterns, optarg,
1093 &pattern, &argnum, &subopt);
1094 err_desc.error_pattern |= pattern;
1097 if (optret == CC_OR_AMBIGUOUS) {
1098 warnx("%s: ambiguous argument %s", __func__,
1102 } else if (optret == CC_OR_NOT_FOUND) {
1103 warnx("%s: argument %s not found", __func__,
1111 char *tmpstr, *tmpstr2;
1113 tmpstr = strdup(optarg);
1114 if (tmpstr == NULL) {
1115 warn("%s: error duplicating string %s",
1121 tmpstr2 = strsep(&tmpstr, ",");
1122 if (tmpstr2 == NULL) {
1123 warnx("%s: invalid -r argument %s", __func__,
1129 lba = strtoull(tmpstr2, NULL, 0);
1130 tmpstr2 = strsep(&tmpstr, ",");
1131 if (tmpstr2 == NULL) {
1132 warnx("%s: no len argument for -r lba,len, got"
1133 " %s", __func__, optarg);
1138 len = strtoul(tmpstr2, NULL, 0);
1143 struct get_hook hook;
1146 sense_len = strtol(optarg, NULL, 0);
1147 if (sense_len <= 0) {
1148 warnx("invalid number of sense bytes %d",
1154 sense_len = MIN(sense_len, SSD_FULL_SIZE);
1156 hook.argc = argc - optind;
1157 hook.argv = argv + optind;
1160 sensestr = cget(&hook, NULL);
1161 if ((sensestr != NULL)
1162 && (sensestr[0] == '-')) {
1166 (uint8_t *)&err_desc.custom_sense,
1167 sense_len, sensestr, iget, &hook);
1177 if (delete_id_set != 0) {
1178 err_desc.serial = delete_id;
1179 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
1180 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
1187 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
1188 warnx("%s: error injection command (-i) needed",
1192 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
1193 && (sense_len == 0)) {
1194 warnx("%s: custom error requires -s", __func__);
1199 if (continuous != 0)
1200 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
1203 * If fd_sense is set, we need to read the sense data the user
1204 * wants returned from stdin.
1206 if (fd_sense == 1) {
1208 int amt_to_read = sense_len;
1209 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
1211 for (amt_read = 0; amt_to_read > 0;
1212 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1213 if (amt_read == -1) {
1214 warn("error reading sense data from stdin");
1218 amt_to_read -= amt_read;
1219 buf_ptr += amt_read;
1223 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1224 warnx("%s: command pattern (-p) needed", __func__);
1230 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1232 * We could check here to see whether it's a read/write
1233 * command, but that will be pointless once we allow
1234 * custom patterns. At that point, the user could specify
1235 * a READ(6) CDB type, and we wouldn't have an easy way here
1236 * to verify whether range checking is possible there. The
1237 * user will just figure it out when his error never gets
1241 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1242 warnx("%s: need read and/or write pattern if range "
1243 "is specified", __func__);
1248 err_desc.lba_range.lba = lba;
1249 err_desc.lba_range.len = len;
1252 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1253 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1256 printf("Error injection succeeded, serial number is %ju\n",
1257 (uintmax_t)err_desc.serial);
1265 cctl_lunlist(int fd)
1267 struct scsi_report_luns_data *lun_data;
1268 struct scsi_inquiry_data *inq_data;
1282 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1283 * if we ever acquire the ability to have multiple targets.
1285 if ((retval = cctl_get_luns(fd, target, /*lun*/ 0, initid,
1286 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1289 inq_data = malloc(sizeof(*inq_data));
1290 if (inq_data == NULL) {
1291 warn("%s: couldn't allocate memory for inquiry data\n",
1296 for (i = 0; i < num_luns; i++) {
1300 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1301 case RPL_LUNDATA_ATYP_PERIPH:
1302 lun_val = lun_data->luns[i].lundata[1];
1304 case RPL_LUNDATA_ATYP_FLAT:
1305 lun_val = (lun_data->luns[i].lundata[0] &
1306 RPL_LUNDATA_FLAT_LUN_MASK) |
1307 (lun_data->luns[i].lundata[1] <<
1308 RPL_LUNDATA_FLAT_LUN_BITS);
1310 case RPL_LUNDATA_ATYP_LUN:
1311 case RPL_LUNDATA_ATYP_EXTLUN:
1313 fprintf(stdout, "Unsupported LUN format %d\n",
1314 lun_data->luns[i].lundata[0] &
1315 RPL_LUNDATA_ATYP_MASK);
1322 if ((retval = cctl_get_inquiry(fd, target, lun_val, initid,
1323 /*retries*/ 2, scsi_path,
1328 printf("%s", scsi_path);
1329 scsi_print_inquiry(inq_data);
1333 if (lun_data != NULL)
1336 if (inq_data != NULL)
1343 cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len)
1346 case CFI_MT_PORT_OFFLINE:
1347 snprintf(str, str_len, "Port Offline");
1350 snprintf(str, str_len, "Error");
1352 case CFI_MT_SUCCESS:
1353 snprintf(str, str_len, "Success");
1356 snprintf(str, str_len, "None??");
1359 snprintf(str, str_len, "Unknown status: %d", status);
1365 cctl_cfi_bbr_statusstr(cfi_bbrread_status status, char *str, int str_len)
1368 case CFI_BBR_SUCCESS:
1369 snprintf(str, str_len, "Success");
1371 case CFI_BBR_LUN_UNCONFIG:
1372 snprintf(str, str_len, "LUN not configured");
1374 case CFI_BBR_NO_LUN:
1375 snprintf(str, str_len, "LUN does not exist");
1377 case CFI_BBR_NO_MEM:
1378 snprintf(str, str_len, "Memory allocation error");
1380 case CFI_BBR_BAD_LEN:
1381 snprintf(str, str_len, "Length is not a multiple of blocksize");
1383 case CFI_BBR_RESERV_CONFLICT:
1384 snprintf(str, str_len, "Reservation conflict");
1386 case CFI_BBR_LUN_STOPPED:
1387 snprintf(str, str_len, "LUN is powered off");
1389 case CFI_BBR_LUN_OFFLINE_CTL:
1390 snprintf(str, str_len, "LUN is offline");
1392 case CFI_BBR_LUN_OFFLINE_RC:
1393 snprintf(str, str_len, "RAIDCore array is offline (double "
1396 case CFI_BBR_SCSI_ERROR:
1397 snprintf(str, str_len, "SCSI Error");
1400 snprintf(str, str_len, "Error");
1403 snprintf(str, str_len, "Unknown status: %d", status);
1409 cctl_hardstopstart(int fd, ctladm_cmdfunction command)
1411 struct ctl_hard_startstop_info hs_info;
1412 char error_str[256];
1418 if (command == CTLADM_CMD_HARDSTART)
1423 if (ioctl(fd, (do_start == 1) ? CTL_HARD_START : CTL_HARD_STOP,
1425 warn("%s: CTL_HARD_%s ioctl failed", __func__,
1426 (do_start == 1) ? "START" : "STOP");
1431 fprintf(stdout, "Hard %s Status: ", (command == CTLADM_CMD_HARDSTOP) ?
1433 cctl_cfi_mt_statusstr(hs_info.status, error_str, sizeof(error_str));
1434 fprintf(stdout, "%s\n", error_str);
1435 fprintf(stdout, "Total LUNs: %d\n", hs_info.total_luns);
1436 fprintf(stdout, "LUNs complete: %d\n", hs_info.luns_complete);
1437 fprintf(stdout, "LUNs failed: %d\n", hs_info.luns_failed);
1444 cctl_bbrread(int fd, int target __unused, int lun, int iid __unused,
1445 int argc, char **argv, char *combinedopt)
1447 struct ctl_bbrread_info bbr_info;
1448 char error_str[256];
1457 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1460 datalen = strtoul(optarg, NULL, 0);
1463 lba = strtoull(optarg, NULL, 0);
1472 warnx("%s: you must specify an LBA with -l", __func__);
1477 if (datalen == -1) {
1478 warnx("%s: you must specify a length with -d", __func__);
1483 bbr_info.lun_num = lun;
1486 * XXX KDM get the blocksize first??
1488 if ((datalen % 512) != 0) {
1489 warnx("%s: data length %d is not a multiple of 512 bytes",
1494 bbr_info.len = datalen;
1496 if (ioctl(fd, CTL_BBRREAD, &bbr_info) == -1) {
1497 warn("%s: CTL_BBRREAD ioctl failed", __func__);
1501 cctl_cfi_mt_statusstr(bbr_info.status, error_str, sizeof(error_str));
1502 fprintf(stdout, "BBR Read Overall Status: %s\n", error_str);
1503 cctl_cfi_bbr_statusstr(bbr_info.bbr_status, error_str,
1505 fprintf(stdout, "BBR Read Status: %s\n", error_str);
1507 * XXX KDM should we bother printing out SCSI status if we get
1508 * CFI_BBR_SCSI_ERROR back?
1510 * Return non-zero if this fails?
1517 cctl_startup_shutdown(int fd, int target, int lun, int iid,
1518 ctladm_cmdfunction command)
1522 struct scsi_report_luns_data *lun_data;
1523 struct scsi_inquiry_data *inq_data;
1533 * - step through each lun, do an inquiry
1534 * - check OOA queue on direct access luns
1535 * - send stop with offline bit to each direct access device with a
1537 * - if we get a reservation conflict, reset the LUN to clear it
1538 * and reissue the stop with the offline bit set
1543 io = ctl_scsi_alloc_io(id);
1545 warnx("%s: can't allocate memory", __func__);
1549 if ((retval = cctl_get_luns(fd, target, lun, iid, /*retries*/ 2,
1550 &lun_data, &num_luns)) != 0)
1553 inq_data = malloc(sizeof(*inq_data));
1554 if (inq_data == NULL) {
1555 warn("%s: couldn't allocate memory for inquiry data\n",
1560 for (i = 0; i < num_luns; i++) {
1565 * XXX KDM figure out a way to share this code with
1568 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1569 case RPL_LUNDATA_ATYP_PERIPH:
1570 lun_val = lun_data->luns[i].lundata[1];
1572 case RPL_LUNDATA_ATYP_FLAT:
1573 lun_val = (lun_data->luns[i].lundata[0] &
1574 RPL_LUNDATA_FLAT_LUN_MASK) |
1575 (lun_data->luns[i].lundata[1] <<
1576 RPL_LUNDATA_FLAT_LUN_BITS);
1578 case RPL_LUNDATA_ATYP_LUN:
1579 case RPL_LUNDATA_ATYP_EXTLUN:
1581 fprintf(stdout, "Unsupported LUN format %d\n",
1582 lun_data->luns[i].lundata[0] &
1583 RPL_LUNDATA_ATYP_MASK);
1590 if ((retval = cctl_get_inquiry(fd, target, lun_val, iid,
1591 /*retries*/ 2, scsi_path,
1596 printf("%s", scsi_path);
1597 scsi_print_inquiry(inq_data);
1599 * We only want to shutdown direct access devices.
1601 if (SID_TYPE(inq_data) != T_DIRECT) {
1602 printf("%s LUN is not direct access, skipped\n",
1607 if (command == CTLADM_CMD_SHUTDOWN) {
1608 struct ctl_ooa_info ooa_info;
1610 ooa_info.target_id = target;
1611 ooa_info.lun_id = lun_val;
1613 if (ioctl(fd, CTL_CHECK_OOA, &ooa_info) == -1) {
1614 printf("%s CTL_CHECK_OOA ioctl failed\n",
1619 if (ooa_info.status != CTL_OOA_SUCCESS) {
1620 printf("%s CTL_CHECK_OOA returned status %d\n",
1621 scsi_path, ooa_info.status);
1624 if (ooa_info.num_entries != 0) {
1625 printf("%s %d entr%s in the OOA queue, "
1626 "skipping shutdown\n", scsi_path,
1627 ooa_info.num_entries,
1628 (ooa_info.num_entries > 1)?"ies" : "y" );
1633 ctl_scsi_start_stop(/*io*/ io,
1634 /*start*/(command == CTLADM_CMD_STARTUP) ?
1638 /*power_conditions*/ SSS_PC_START_VALID,
1641 (command == CTLADM_CMD_STARTUP) ?
1642 CTL_TAG_SIMPLE :CTL_TAG_ORDERED,
1645 io->io_hdr.nexus.targ_target.id = target;
1646 io->io_hdr.nexus.targ_lun = lun_val;
1647 io->io_hdr.nexus.initid = id;
1649 if (cctl_do_io(fd, /*retries*/ 3, io, __func__) != 0) {
1654 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1655 ctl_io_error_print(io, inq_data, stderr);
1657 printf("%s LUN is now %s\n", scsi_path,
1658 (command == CTLADM_CMD_STARTUP) ? "online" :
1663 if (lun_data != NULL)
1666 if (inq_data != NULL)
1670 ctl_scsi_free_io(io);
1676 cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
1677 int argc, char **argv, char *combinedopt)
1683 uint64_t our_lba = 0;
1684 uint32_t our_block_count = 0;
1685 int reladr = 0, immed = 0;
1691 io = ctl_scsi_alloc_io(id);
1693 warnx("%s: can't allocate memory", __func__);
1697 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1700 our_block_count = strtoul(optarg, NULL, 0);
1703 cdb_size = strtol(optarg, NULL, 0);
1709 our_lba = strtoull(optarg, NULL, 0);
1719 if (cdb_size != -1) {
1725 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1726 "and 16", __func__, cdb_size);
1729 break; /* NOTREACHED */
1734 ctl_scsi_sync_cache(/*io*/ io,
1737 /*minimum_cdb_size*/ cdb_size,
1738 /*starting_lba*/ our_lba,
1739 /*block_count*/ our_block_count,
1740 /*tag_type*/ CTL_TAG_SIMPLE,
1743 io->io_hdr.nexus.targ_target.id = target;
1744 io->io_hdr.nexus.targ_lun = lun;
1745 io->io_hdr.nexus.initid = id;
1747 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1752 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1753 fprintf(stdout, "Cache synchronized successfully\n");
1755 ctl_io_error_print(io, NULL, stderr);
1757 ctl_scsi_free_io(io);
1763 cctl_start_stop(int fd, int target, int lun, int iid, int retries, int start,
1764 int argc, char **argv, char *combinedopt)
1769 int immed = 0, onoffline = 0;
1775 io = ctl_scsi_alloc_io(id);
1777 warnx("%s: can't allocate memory", __func__);
1781 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1794 * Use an ordered tag for the stop command, to guarantee that any
1795 * pending I/O will finish before the stop command executes. This
1796 * would normally be the case anyway, since CTL will basically
1797 * treat the start/stop command as an ordered command with respect
1798 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1800 ctl_scsi_start_stop(/*io*/ io,
1803 /*immediate*/ immed,
1804 /*power_conditions*/ SSS_PC_START_VALID,
1805 /*onoffline*/ onoffline,
1806 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1810 io->io_hdr.nexus.targ_target.id = target;
1811 io->io_hdr.nexus.targ_lun = lun;
1812 io->io_hdr.nexus.initid = id;
1814 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1819 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1820 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1821 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1822 (start) ? "started" : "stopped");
1824 ctl_io_error_print(io, NULL, stderr);
1827 ctl_scsi_free_io(io);
1833 cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
1834 int argc, char **argv, char *combinedopt)
1840 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1850 io = ctl_scsi_alloc_io(id);
1852 warn("%s: can't allocate memory", __func__);
1856 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1859 pc = strtoul(optarg, NULL, 0);
1862 subpage = strtoul(optarg, NULL, 0);
1871 page_code = strtoul(optarg, NULL, 0);
1874 cdbsize = strtol(optarg, NULL, 0);
1881 if (((list == 0) && (page_code == -1))
1882 || ((list != 0) && (page_code != -1))) {
1883 warnx("%s: you must specify either a page code (-m) or -l",
1889 if ((page_code != -1)
1890 && ((page_code > SMS_ALL_PAGES_PAGE)
1891 || (page_code < 0))) {
1892 warnx("%s: page code %d is out of range", __func__,
1899 page_code = SMS_ALL_PAGES_PAGE;
1901 warnx("%s: arg -P makes no sense with -l",
1906 if (subpage != -1) {
1907 warnx("%s: arg -S makes no sense with -l", __func__);
1914 pc = SMS_PAGE_CTRL_CURRENT;
1918 warnx("%s: page control value %d is out of range: 0-3",
1928 || (subpage < 0))) {
1929 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1940 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1941 "and 10", __func__, cdbsize);
1957 dataptr = (uint8_t *)malloc(datalen);
1958 if (dataptr == NULL) {
1959 warn("%s: can't allocate %d bytes", __func__, datalen);
1964 memset(dataptr, 0, datalen);
1966 ctl_scsi_mode_sense(io,
1967 /*data_ptr*/ dataptr,
1968 /*data_len*/ datalen,
1971 /*page_code*/ page_code,
1973 /*subpage*/ subpage,
1974 /*minimum_cdb_size*/ cdbsize,
1975 /*tag_type*/ CTL_TAG_SIMPLE,
1978 io->io_hdr.nexus.targ_target.id = target;
1979 io->io_hdr.nexus.targ_lun = lun;
1980 io->io_hdr.nexus.initid = id;
1982 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1987 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1988 int pages_len, used_len;
1989 uint32_t returned_len;
1992 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1993 struct scsi_mode_hdr_6 *hdr6;
1996 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1998 returned_len = hdr6->datalen + 1;
1999 bdlen = hdr6->block_descr_len;
2001 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
2003 struct scsi_mode_hdr_10 *hdr10;
2006 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
2008 returned_len = scsi_2btoul(hdr10->datalen) + 2;
2009 bdlen = scsi_2btoul(hdr10->block_descr_len);
2011 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
2013 /* just in case they can give us more than we allocated for */
2014 returned_len = min(returned_len, datalen);
2015 pages_len = returned_len - (ndataptr - dataptr);
2017 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
2018 returned_len, pages_len);
2021 fprintf(stdout, "Supported mode pages:\n");
2022 for (used_len = 0; used_len < pages_len;) {
2023 struct scsi_mode_page_header *header;
2025 header = (struct scsi_mode_page_header *)
2026 &ndataptr[used_len];
2027 fprintf(stdout, "%d\n", header->page_code);
2028 used_len += header->page_length + 2;
2031 for (used_len = 0; used_len < pages_len; used_len++) {
2032 fprintf(stdout, "0x%x ", ndataptr[used_len]);
2033 if (((used_len+1) % 16) == 0)
2034 fprintf(stdout, "\n");
2036 fprintf(stdout, "\n");
2039 ctl_io_error_print(io, NULL, stderr);
2042 ctl_scsi_free_io(io);
2044 if (dataptr != NULL)
2051 cctl_read_capacity(int fd, int target, int lun, int iid, int retries,
2052 int argc, char **argv, char *combinedopt)
2056 struct scsi_read_capacity_data *data;
2057 struct scsi_read_capacity_data_long *longdata;
2058 int cdbsize = -1, retval;
2067 io = ctl_scsi_alloc_io(id);
2069 warn("%s: can't allocate memory\n", __func__);
2073 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2076 cdbsize = strtol(optarg, NULL, 0);
2082 if (cdbsize != -1) {
2088 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
2089 "and 16", __func__, cdbsize);
2092 break; /* NOTREACHED */
2097 dataptr = (uint8_t *)malloc(sizeof(*longdata));
2098 if (dataptr == NULL) {
2099 warn("%s: can't allocate %zd bytes\n", __func__,
2104 memset(dataptr, 0, sizeof(*longdata));
2110 ctl_scsi_read_capacity(io,
2111 /*data_ptr*/ dataptr,
2112 /*data_len*/ sizeof(*longdata),
2116 /*tag_type*/ CTL_TAG_SIMPLE,
2120 ctl_scsi_read_capacity_16(io,
2121 /*data_ptr*/ dataptr,
2122 /*data_len*/ sizeof(*longdata),
2126 /*tag_type*/ CTL_TAG_SIMPLE,
2131 io->io_hdr.nexus.initid = id;
2132 io->io_hdr.nexus.targ_target.id = target;
2133 io->io_hdr.nexus.targ_lun = lun;
2135 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2140 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2144 if (cdbsize == 10) {
2146 data = (struct scsi_read_capacity_data *)dataptr;
2148 maxlba = scsi_4btoul(data->addr);
2149 blocksize = scsi_4btoul(data->length);
2151 if (maxlba == 0xffffffff) {
2156 longdata=(struct scsi_read_capacity_data_long *)dataptr;
2158 maxlba = scsi_8btou64(longdata->addr);
2159 blocksize = scsi_4btoul(longdata->length);
2162 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
2163 (uintmax_t)maxlba, blocksize);
2165 ctl_io_error_print(io, NULL, stderr);
2168 ctl_scsi_free_io(io);
2170 if (dataptr != NULL)
2177 cctl_read_write(int fd, int target, int lun, int iid, int retries,
2178 int argc, char **argv, char *combinedopt,
2179 ctladm_cmdfunction command)
2183 int file_fd, do_stdio;
2184 int cdbsize = -1, databytes;
2186 char *filename = NULL;
2187 int datalen = -1, blocksize = -1;
2199 io = ctl_scsi_alloc_io(id);
2201 warn("%s: can't allocate memory\n", __func__);
2205 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2208 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
2211 blocksize = strtoul(optarg, NULL, 0);
2214 cdbsize = strtoul(optarg, NULL, 0);
2217 datalen = strtoul(optarg, NULL, 0);
2220 filename = strdup(optarg);
2223 lba = strtoull(optarg, NULL, 0);
2230 if (filename == NULL) {
2231 warnx("%s: you must supply a filename using -f", __func__);
2236 if (datalen == -1) {
2237 warnx("%s: you must specify the data length with -d", __func__);
2243 warnx("%s: you must specify the LBA with -l", __func__);
2248 if (blocksize == -1) {
2249 warnx("%s: you must specify the blocksize with -b", __func__);
2254 if (cdbsize != -1) {
2262 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
2263 "10, 12 or 16", __func__, cdbsize);
2266 break; /* NOTREACHED */
2271 databytes = datalen * blocksize;
2272 dataptr = (uint8_t *)malloc(databytes);
2274 if (dataptr == NULL) {
2275 warn("%s: can't allocate %d bytes\n", __func__, databytes);
2279 if (strcmp(filename, "-") == 0) {
2280 if (command == CTLADM_CMD_READ)
2281 file_fd = STDOUT_FILENO;
2283 file_fd = STDIN_FILENO;
2286 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
2287 if (file_fd == -1) {
2288 warn("%s: can't open file %s", __func__, filename);
2294 memset(dataptr, 0, databytes);
2296 if (command == CTLADM_CMD_WRITE) {
2299 bytes_read = read(file_fd, dataptr, databytes);
2300 if (bytes_read == -1) {
2301 warn("%s: error reading file %s", __func__, filename);
2305 if (bytes_read != databytes) {
2306 warnx("%s: only read %d bytes from file %s",
2307 __func__, bytes_read, filename);
2312 ctl_scsi_read_write(io,
2313 /*data_ptr*/ dataptr,
2314 /*data_len*/ databytes,
2315 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
2317 /*minimum_cdb_size*/ cdbsize,
2319 /*num_blocks*/ datalen,
2320 /*tag_type*/ CTL_TAG_SIMPLE,
2323 io->io_hdr.nexus.targ_target.id = target;
2324 io->io_hdr.nexus.targ_lun = lun;
2325 io->io_hdr.nexus.initid = id;
2327 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2332 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2333 && (command == CTLADM_CMD_READ)) {
2336 bytes_written = write(file_fd, dataptr, databytes);
2337 if (bytes_written == -1) {
2338 warn("%s: can't write to %s", __func__, filename);
2341 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
2342 ctl_io_error_print(io, NULL, stderr);
2347 ctl_scsi_free_io(io);
2349 if (dataptr != NULL)
2360 cctl_get_luns(int fd, int target, int lun, int iid, int retries, struct
2361 scsi_report_luns_data **lun_data, uint32_t *num_luns)
2372 io = ctl_scsi_alloc_io(id);
2374 warnx("%s: can't allocate memory", __func__);
2379 * lun_data includes space for 1 lun, allocate space for 4 initially.
2380 * If that isn't enough, we'll allocate more.
2384 lun_datalen = sizeof(*lun_data) +
2385 (nluns * sizeof(struct scsi_report_luns_lundata));
2386 *lun_data = malloc(lun_datalen);
2388 if (*lun_data == NULL) {
2389 warnx("%s: can't allocate memory", __func__);
2390 ctl_scsi_free_io(io);
2394 ctl_scsi_report_luns(io,
2395 /*data_ptr*/ (uint8_t *)*lun_data,
2396 /*data_len*/ lun_datalen,
2397 /*select_report*/ RPL_REPORT_ALL,
2398 /*tag_type*/ CTL_TAG_SIMPLE,
2401 io->io_hdr.nexus.initid = id;
2402 io->io_hdr.nexus.targ_target.id = target;
2403 io->io_hdr.nexus.targ_lun = lun;
2405 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2410 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2411 uint32_t returned_len, returned_luns;
2413 returned_len = scsi_4btoul((*lun_data)->length);
2414 returned_luns = returned_len / 8;
2415 if (returned_luns > nluns) {
2416 nluns = returned_luns;
2420 /* These should be the same */
2421 *num_luns = MIN(returned_luns, nluns);
2423 ctl_io_error_print(io, NULL, stderr);
2427 ctl_scsi_free_io(io);
2433 cctl_report_luns(int fd, int target, int lun, int iid, int retries)
2435 struct scsi_report_luns_data *lun_data;
2436 uint32_t num_luns, i;
2441 if ((retval = cctl_get_luns(fd, target, lun, iid, retries, &lun_data,
2445 fprintf(stdout, "%u LUNs returned\n", num_luns);
2446 for (i = 0; i < num_luns; i++) {
2450 * XXX KDM figure out a way to share this code with
2453 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
2454 case RPL_LUNDATA_ATYP_PERIPH:
2455 lun_val = lun_data->luns[i].lundata[1];
2457 case RPL_LUNDATA_ATYP_FLAT:
2458 lun_val = (lun_data->luns[i].lundata[0] &
2459 RPL_LUNDATA_FLAT_LUN_MASK) |
2460 (lun_data->luns[i].lundata[1] <<
2461 RPL_LUNDATA_FLAT_LUN_BITS);
2463 case RPL_LUNDATA_ATYP_LUN:
2464 case RPL_LUNDATA_ATYP_EXTLUN:
2466 fprintf(stdout, "Unsupported LUN format %d\n",
2467 lun_data->luns[i].lundata[0] &
2468 RPL_LUNDATA_ATYP_MASK);
2475 fprintf(stdout, "%d\n", lun_val);
2479 if (lun_data != NULL)
2486 cctl_tur(int fd, int target, int lun, int iid, int retries)
2493 io = ctl_scsi_alloc_io(id);
2495 fprintf(stderr, "can't allocate memory\n");
2500 /* tag_type */ CTL_TAG_SIMPLE,
2503 io->io_hdr.nexus.targ_target.id = target;
2504 io->io_hdr.nexus.targ_lun = lun;
2505 io->io_hdr.nexus.initid = id;
2507 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2508 ctl_scsi_free_io(io);
2512 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2513 fprintf(stdout, "Unit is ready\n");
2515 ctl_io_error_print(io, NULL, stderr);
2521 cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
2522 char *path_str, int path_len,
2523 struct scsi_inquiry_data *inq_data)
2533 io = ctl_scsi_alloc_io(id);
2535 warnx("cctl_inquiry: can't allocate memory\n");
2539 ctl_scsi_inquiry(/*io*/ io,
2540 /*data_ptr*/ (uint8_t *)inq_data,
2541 /*data_len*/ sizeof(*inq_data),
2544 /*tag_type*/ CTL_TAG_SIMPLE,
2547 io->io_hdr.nexus.targ_target.id = target;
2548 io->io_hdr.nexus.targ_lun = lun;
2549 io->io_hdr.nexus.initid = id;
2551 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2556 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
2558 ctl_io_error_print(io, NULL, stderr);
2559 } else if (path_str != NULL)
2560 ctl_scsi_path_string(io, path_str, path_len);
2563 ctl_scsi_free_io(io);
2569 cctl_inquiry(int fd, int target, int lun, int iid, int retries)
2571 struct scsi_inquiry_data *inq_data;
2577 inq_data = malloc(sizeof(*inq_data));
2578 if (inq_data == NULL) {
2579 warnx("%s: can't allocate inquiry data", __func__);
2584 if ((retval = cctl_get_inquiry(fd, target, lun, iid, retries, scsi_path,
2585 sizeof(scsi_path), inq_data)) != 0)
2588 printf("%s", scsi_path);
2589 scsi_print_inquiry(inq_data);
2592 if (inq_data != NULL)
2599 cctl_req_sense(int fd, int target, int lun, int iid, int retries)
2602 struct scsi_sense_data *sense_data;
2610 io = ctl_scsi_alloc_io(id);
2612 warnx("cctl_req_sense: can't allocate memory\n");
2615 sense_data = malloc(sizeof(*sense_data));
2616 memset(sense_data, 0, sizeof(*sense_data));
2618 ctl_scsi_request_sense(/*io*/ io,
2619 /*data_ptr*/ (uint8_t *)sense_data,
2620 /*data_len*/ sizeof(*sense_data),
2622 /*tag_type*/ CTL_TAG_SIMPLE,
2625 io->io_hdr.nexus.targ_target.id = target;
2626 io->io_hdr.nexus.targ_lun = lun;
2627 io->io_hdr.nexus.initid = id;
2629 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2634 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2635 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2636 io->scsiio.sense_len = sizeof(*sense_data);
2637 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2639 ctl_io_error_print(io, NULL, stderr);
2643 ctl_scsi_free_io(io);
2650 cctl_report_target_port_group(int fd, int target, int lun, int initiator)
2662 io = ctl_scsi_alloc_io(id);
2664 warn("%s: can't allocate memory", __func__);
2669 dataptr = (uint8_t *)malloc(datalen);
2670 if (dataptr == NULL) {
2671 warn("%s: can't allocate %d bytes", __func__, datalen);
2676 memset(dataptr, 0, datalen);
2678 ctl_scsi_maintenance_in(/*io*/ io,
2679 /*data_ptr*/ dataptr,
2680 /*data_len*/ datalen,
2681 /*action*/ SA_RPRT_TRGT_GRP,
2682 /*tag_type*/ CTL_TAG_SIMPLE,
2685 io->io_hdr.nexus.targ_target.id = target;
2686 io->io_hdr.nexus.targ_lun = lun;
2687 io->io_hdr.nexus.initid = id;
2689 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2694 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2695 int returned_len, used_len;
2697 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2699 for (used_len = 0; used_len < returned_len; used_len++) {
2700 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2701 if (((used_len+1) % 8) == 0)
2702 fprintf(stdout, "\n");
2704 fprintf(stdout, "\n");
2706 ctl_io_error_print(io, NULL, stderr);
2709 ctl_scsi_free_io(io);
2711 if (dataptr != NULL)
2718 cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator)
2730 io = ctl_scsi_alloc_io(id);
2732 warn("%s: can't allocate memory", __func__);
2737 dataptr = (uint8_t *)malloc(datalen);
2738 if (dataptr == NULL) {
2739 warn("%s: can't allocate %d bytes", __func__, datalen);
2744 memset(dataptr, 0, datalen);
2746 ctl_scsi_inquiry(/*io*/ io,
2747 /*data_ptr*/ dataptr,
2748 /*data_len*/ datalen,
2750 /*page_code*/ SVPD_DEVICE_ID,
2751 /*tag_type*/ CTL_TAG_SIMPLE,
2754 io->io_hdr.nexus.targ_target.id = target;
2755 io->io_hdr.nexus.targ_lun = lun;
2756 io->io_hdr.nexus.initid = id;
2758 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2763 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2764 int returned_len, used_len;
2766 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2768 for (used_len = 0; used_len < returned_len; used_len++) {
2769 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2770 if (((used_len+1) % 8) == 0)
2771 fprintf(stdout, "\n");
2773 fprintf(stdout, "\n");
2775 ctl_io_error_print(io, NULL, stderr);
2778 ctl_scsi_free_io(io);
2780 if (dataptr != NULL)
2787 cctl_persistent_reserve_in(int fd, int target, int lun, int initiator,
2788 int argc, char **argv, char *combinedopt,
2803 io = ctl_scsi_alloc_io(id);
2805 warn("%s: can't allocate memory", __func__);
2809 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2812 action = strtol(optarg, NULL, 0);
2819 if (action < 0 || action > 2) {
2820 warn("action must be specified and in the range: 0-2");
2827 dataptr = (uint8_t *)malloc(datalen);
2828 if (dataptr == NULL) {
2829 warn("%s: can't allocate %d bytes", __func__, datalen);
2834 memset(dataptr, 0, datalen);
2836 ctl_scsi_persistent_res_in(io,
2837 /*data_ptr*/ dataptr,
2838 /*data_len*/ datalen,
2840 /*tag_type*/ CTL_TAG_SIMPLE,
2843 io->io_hdr.nexus.targ_target.id = target;
2844 io->io_hdr.nexus.targ_lun = lun;
2845 io->io_hdr.nexus.initid = id;
2847 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2852 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2853 int returned_len, used_len;
2859 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2860 returned_len = min(returned_len, 256);
2863 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2869 warnx("%s: invalid action %d", __func__, action);
2871 break; /* NOTREACHED */
2874 for (used_len = 0; used_len < returned_len; used_len++) {
2875 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2876 if (((used_len+1) % 8) == 0)
2877 fprintf(stdout, "\n");
2879 fprintf(stdout, "\n");
2881 ctl_io_error_print(io, NULL, stderr);
2884 ctl_scsi_free_io(io);
2886 if (dataptr != NULL)
2893 cctl_persistent_reserve_out(int fd, int target, int lun, int initiator,
2894 int argc, char **argv, char *combinedopt,
2900 uint64_t key = 0, sa_key = 0;
2901 int action = -1, restype = -1;
2910 io = ctl_scsi_alloc_io(id);
2912 warn("%s: can't allocate memory", __func__);
2916 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2919 action = strtol(optarg, NULL, 0);
2922 key = strtoull(optarg, NULL, 0);
2925 restype = strtol(optarg, NULL, 0);
2928 sa_key = strtoull(optarg, NULL, 0);
2934 if (action < 0 || action > 5) {
2935 warn("action must be specified and in the range: 0-5");
2940 if (restype < 0 || restype > 5) {
2941 if (action != 0 && action != 5 && action != 3) {
2942 warn("'restype' must specified and in the range: 0-5");
2949 dataptr = (uint8_t *)malloc(datalen);
2950 if (dataptr == NULL) {
2951 warn("%s: can't allocate %d bytes", __func__, datalen);
2956 memset(dataptr, 0, datalen);
2958 ctl_scsi_persistent_res_out(io,
2959 /*data_ptr*/ dataptr,
2960 /*data_len*/ datalen,
2965 /*tag_type*/ CTL_TAG_SIMPLE,
2968 io->io_hdr.nexus.targ_target.id = target;
2969 io->io_hdr.nexus.targ_lun = lun;
2970 io->io_hdr.nexus.initid = id;
2972 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2976 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2978 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2979 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2980 "successfully\n", scsi_path);
2982 ctl_io_error_print(io, NULL, stderr);
2985 ctl_scsi_free_io(io);
2987 if (dataptr != NULL)
2993 struct cctl_req_option {
2998 STAILQ_ENTRY(cctl_req_option) links;
3002 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
3004 struct ctl_lun_req req;
3005 int device_type = -1;
3006 uint64_t lun_size = 0;
3007 uint32_t blocksize = 0, req_lun_id = 0;
3008 char *serial_num = NULL;
3009 char *device_id = NULL;
3010 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
3011 char *backend_name = NULL;
3012 STAILQ_HEAD(, cctl_req_option) option_list;
3013 int num_options = 0;
3016 STAILQ_INIT(&option_list);
3018 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3021 backend_name = strdup(optarg);
3024 blocksize = strtoul(optarg, NULL, 0);
3028 device_id = strdup(optarg);
3031 req_lun_id = strtoul(optarg, NULL, 0);
3035 struct cctl_req_option *option;
3039 tmpstr = strdup(optarg);
3040 name = strsep(&tmpstr, "=");
3042 warnx("%s: option -o takes \"name=value\""
3043 "argument", __func__);
3047 value = strsep(&tmpstr, "=");
3048 if (value == NULL) {
3049 warnx("%s: option -o takes \"name=value\""
3050 "argument", __func__);
3054 option = malloc(sizeof(*option));
3055 if (option == NULL) {
3056 warn("%s: error allocating %zd bytes",
3057 __func__, sizeof(*option));
3061 option->name = strdup(name);
3062 option->namelen = strlen(name) + 1;
3063 option->value = strdup(value);
3064 option->vallen = strlen(value) + 1;
3067 STAILQ_INSERT_TAIL(&option_list, option, links);
3072 if (strcasecmp(optarg, "auto") != 0) {
3073 retval = expand_number(optarg, &lun_size);
3075 warn("%s: invalid -s argument",
3084 serial_num = strdup(optarg);
3087 device_type = strtoul(optarg, NULL, 0);
3094 if (backend_name == NULL) {
3095 warnx("%s: backend name (-b) must be specified", __func__);
3100 bzero(&req, sizeof(req));
3102 strlcpy(req.backend, backend_name, sizeof(req.backend));
3103 req.reqtype = CTL_LUNREQ_CREATE;
3105 if (blocksize_set != 0)
3106 req.reqdata.create.blocksize_bytes = blocksize;
3108 if (lun_size_set != 0)
3109 req.reqdata.create.lun_size_bytes = lun_size;
3111 if (lun_id_set != 0) {
3112 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
3113 req.reqdata.create.req_lun_id = req_lun_id;
3116 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
3118 if (device_type != -1)
3119 req.reqdata.create.device_type = device_type;
3121 req.reqdata.create.device_type = T_DIRECT;
3123 if (serial_num != NULL) {
3124 strlcpy(req.reqdata.create.serial_num, serial_num,
3125 sizeof(req.reqdata.create.serial_num));
3126 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
3129 if (device_id != NULL) {
3130 strlcpy(req.reqdata.create.device_id, device_id,
3131 sizeof(req.reqdata.create.device_id));
3132 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
3135 req.num_be_args = num_options;
3136 if (num_options > 0) {
3137 struct cctl_req_option *option, *next_option;
3140 req.be_args = malloc(num_options * sizeof(*req.be_args));
3141 if (req.be_args == NULL) {
3142 warn("%s: error allocating %zd bytes", __func__,
3143 num_options * sizeof(*req.be_args));
3148 for (i = 0, option = STAILQ_FIRST(&option_list);
3149 i < num_options; i++, option = next_option) {
3150 next_option = STAILQ_NEXT(option, links);
3152 req.be_args[i].namelen = option->namelen;
3153 req.be_args[i].name = strdup(option->name);
3154 req.be_args[i].vallen = option->vallen;
3155 req.be_args[i].value = strdup(option->value);
3157 * XXX KDM do we want a way to specify a writeable
3158 * flag of some sort? Do we want a way to specify
3161 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3163 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3166 free(option->value);
3171 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3172 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3177 if (req.status == CTL_LUN_ERROR) {
3178 warnx("%s: error returned from LUN creation request:\n%s",
3179 __func__, req.error_str);
3182 } else if (req.status != CTL_LUN_OK) {
3183 warnx("%s: unknown LUN creation request status %d",
3184 __func__, req.status);
3189 fprintf(stdout, "LUN created successfully\n");
3190 fprintf(stdout, "backend: %s\n", req.backend);
3191 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
3192 fprintf(stdout, "LUN size: %ju bytes\n",
3193 (uintmax_t)req.reqdata.create.lun_size_bytes);
3194 fprintf(stdout, "blocksize %u bytes\n",
3195 req.reqdata.create.blocksize_bytes);
3196 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
3197 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
3198 fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
3205 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
3207 struct ctl_lun_req req;
3208 uint32_t lun_id = 0;
3210 char *backend_name = NULL;
3211 STAILQ_HEAD(, cctl_req_option) option_list;
3212 int num_options = 0;
3215 STAILQ_INIT(&option_list);
3217 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3220 backend_name = strdup(optarg);
3223 lun_id = strtoul(optarg, NULL, 0);
3227 struct cctl_req_option *option;
3231 tmpstr = strdup(optarg);
3232 name = strsep(&tmpstr, "=");
3234 warnx("%s: option -o takes \"name=value\""
3235 "argument", __func__);
3239 value = strsep(&tmpstr, "=");
3240 if (value == NULL) {
3241 warnx("%s: option -o takes \"name=value\""
3242 "argument", __func__);
3246 option = malloc(sizeof(*option));
3247 if (option == NULL) {
3248 warn("%s: error allocating %zd bytes",
3249 __func__, sizeof(*option));
3253 option->name = strdup(name);
3254 option->namelen = strlen(name) + 1;
3255 option->value = strdup(value);
3256 option->vallen = strlen(value) + 1;
3259 STAILQ_INSERT_TAIL(&option_list, option, links);
3268 if (backend_name == NULL)
3269 errx(1, "%s: backend name (-b) must be specified", __func__);
3271 if (lun_id_set == 0)
3272 errx(1, "%s: LUN id (-l) must be specified", __func__);
3274 bzero(&req, sizeof(req));
3276 strlcpy(req.backend, backend_name, sizeof(req.backend));
3277 req.reqtype = CTL_LUNREQ_RM;
3279 req.reqdata.rm.lun_id = lun_id;
3281 req.num_be_args = num_options;
3282 if (num_options > 0) {
3283 struct cctl_req_option *option, *next_option;
3286 req.be_args = malloc(num_options * sizeof(*req.be_args));
3287 if (req.be_args == NULL) {
3288 warn("%s: error allocating %zd bytes", __func__,
3289 num_options * sizeof(*req.be_args));
3294 for (i = 0, option = STAILQ_FIRST(&option_list);
3295 i < num_options; i++, option = next_option) {
3296 next_option = STAILQ_NEXT(option, links);
3298 req.be_args[i].namelen = option->namelen;
3299 req.be_args[i].name = strdup(option->name);
3300 req.be_args[i].vallen = option->vallen;
3301 req.be_args[i].value = strdup(option->value);
3303 * XXX KDM do we want a way to specify a writeable
3304 * flag of some sort? Do we want a way to specify
3307 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3309 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3312 free(option->value);
3317 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3318 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3323 if (req.status == CTL_LUN_ERROR) {
3324 warnx("%s: error returned from LUN removal request:\n%s",
3325 __func__, req.error_str);
3328 } else if (req.status != CTL_LUN_OK) {
3329 warnx("%s: unknown LUN removal request status %d",
3330 __func__, req.status);
3335 printf("LUN %d deleted successfully\n", lun_id);
3342 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
3344 struct ctl_lun_req req;
3345 uint64_t lun_size = 0;
3346 uint32_t lun_id = 0;
3347 int lun_id_set = 0, lun_size_set = 0;
3348 char *backend_name = NULL;
3351 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3354 backend_name = strdup(optarg);
3357 lun_id = strtoul(optarg, NULL, 0);
3361 if (strcasecmp(optarg, "auto") != 0) {
3362 retval = expand_number(optarg, &lun_size);
3364 warn("%s: invalid -s argument",
3377 if (backend_name == NULL)
3378 errx(1, "%s: backend name (-b) must be specified", __func__);
3380 if (lun_id_set == 0)
3381 errx(1, "%s: LUN id (-l) must be specified", __func__);
3383 if (lun_size_set == 0)
3384 errx(1, "%s: size (-s) must be specified", __func__);
3386 bzero(&req, sizeof(req));
3388 strlcpy(req.backend, backend_name, sizeof(req.backend));
3389 req.reqtype = CTL_LUNREQ_MODIFY;
3391 req.reqdata.modify.lun_id = lun_id;
3392 req.reqdata.modify.lun_size_bytes = lun_size;
3394 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3395 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3400 if (req.status == CTL_LUN_ERROR) {
3401 warnx("%s: error returned from LUN modification request:\n%s",
3402 __func__, req.error_str);
3405 } else if (req.status != CTL_LUN_OK) {
3406 warnx("%s: unknown LUN modification request status %d",
3407 __func__, req.status);
3412 printf("LUN %d modified successfully\n", lun_id);
3418 struct cctl_islist_conn {
3421 char *initiator_addr;
3422 char *initiator_alias;
3425 char *header_digest;
3427 char *max_data_segment_length;;
3430 STAILQ_ENTRY(cctl_islist_conn) links;
3433 struct cctl_islist_data {
3435 STAILQ_HEAD(,cctl_islist_conn) conn_list;
3436 struct cctl_islist_conn *cur_conn;
3438 struct sbuf *cur_sb[32];
3442 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
3445 struct cctl_islist_data *islist;
3446 struct cctl_islist_conn *cur_conn;
3448 islist = (struct cctl_islist_data *)user_data;
3449 cur_conn = islist->cur_conn;
3451 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
3452 sizeof(islist->cur_sb[0])))
3453 errx(1, "%s: too many nesting levels, %zd max", __func__,
3454 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
3456 islist->cur_sb[islist->level] = sbuf_new_auto();
3457 if (islist->cur_sb[islist->level] == NULL)
3458 err(1, "%s: Unable to allocate sbuf", __func__);
3460 if (strcmp(name, "connection") == 0) {
3461 if (cur_conn != NULL)
3462 errx(1, "%s: improper connection element nesting",
3465 cur_conn = calloc(1, sizeof(*cur_conn));
3466 if (cur_conn == NULL)
3467 err(1, "%s: cannot allocate %zd bytes", __func__,
3470 islist->num_conns++;
3471 islist->cur_conn = cur_conn;
3473 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
3475 for (i = 0; attr[i] != NULL; i += 2) {
3476 if (strcmp(attr[i], "id") == 0) {
3477 cur_conn->connection_id =
3478 strtoull(attr[i+1], NULL, 0);
3481 "%s: invalid connection attribute %s = %s",
3482 __func__, attr[i], attr[i+1]);
3489 cctl_islist_end_element(void *user_data, const char *name)
3491 struct cctl_islist_data *islist;
3492 struct cctl_islist_conn *cur_conn;
3495 islist = (struct cctl_islist_data *)user_data;
3496 cur_conn = islist->cur_conn;
3498 if ((cur_conn == NULL)
3499 && (strcmp(name, "ctlislist") != 0))
3500 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
3502 if (islist->cur_sb[islist->level] == NULL)
3503 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3504 islist->level, name);
3506 sbuf_finish(islist->cur_sb[islist->level]);
3507 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
3509 err(1, "%s can't allocate %zd bytes for string", __func__,
3510 sbuf_len(islist->cur_sb[islist->level]));
3512 sbuf_delete(islist->cur_sb[islist->level]);
3513 islist->cur_sb[islist->level] = NULL;
3516 if (strcmp(name, "initiator") == 0) {
3517 cur_conn->initiator = str;
3519 } else if (strcmp(name, "initiator_addr") == 0) {
3520 cur_conn->initiator_addr = str;
3522 } else if (strcmp(name, "initiator_alias") == 0) {
3523 cur_conn->initiator_alias = str;
3525 } else if (strcmp(name, "target") == 0) {
3526 cur_conn->target = str;
3528 } else if (strcmp(name, "target_alias") == 0) {
3529 cur_conn->target_alias = str;
3531 } else if (strcmp(name, "header_digest") == 0) {
3532 cur_conn->header_digest = str;
3534 } else if (strcmp(name, "data_digest") == 0) {
3535 cur_conn->data_digest = str;
3537 } else if (strcmp(name, "max_data_segment_length") == 0) {
3538 cur_conn->max_data_segment_length = str;
3540 } else if (strcmp(name, "immediate_data") == 0) {
3541 cur_conn->immediate_data = atoi(str);
3542 } else if (strcmp(name, "iser") == 0) {
3543 cur_conn->iser = atoi(str);
3544 } else if (strcmp(name, "connection") == 0) {
3545 islist->cur_conn = NULL;
3546 } else if (strcmp(name, "ctlislist") == 0) {
3548 errx(1, "unknown element %s", name);
3554 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
3556 struct cctl_islist_data *islist;
3558 islist = (struct cctl_islist_data *)user_data;
3560 sbuf_bcat(islist->cur_sb[islist->level], str, len);
3564 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
3566 struct ctl_iscsi req;
3567 struct cctl_islist_data islist;
3568 struct cctl_islist_conn *conn;
3573 int c, retval, verbose = 0;
3578 bzero(&islist, sizeof(islist));
3579 STAILQ_INIT(&islist.conn_list);
3581 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3595 conn_str = malloc(conn_len);
3597 bzero(&req, sizeof(req));
3598 req.type = CTL_ISCSI_LIST;
3599 req.data.list.alloc_len = conn_len;
3600 req.data.list.conn_xml = conn_str;
3602 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3603 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3608 if (req.status == CTL_ISCSI_ERROR) {
3609 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
3610 __func__, req.error_str);
3611 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
3612 conn_len = conn_len << 1;
3616 if (dump_xml != 0) {
3617 printf("%s", conn_str);
3621 parser = XML_ParserCreate(NULL);
3622 if (parser == NULL) {
3623 warn("%s: Unable to create XML parser", __func__);
3628 XML_SetUserData(parser, &islist);
3629 XML_SetElementHandler(parser, cctl_islist_start_element,
3630 cctl_islist_end_element);
3631 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3633 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3634 XML_ParserFree(parser);
3641 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3642 printf("Session ID: %d\n", conn->connection_id);
3643 printf("Initiator name: %s\n", conn->initiator);
3644 printf("Initiator portal: %s\n", conn->initiator_addr);
3645 printf("Initiator alias: %s\n", conn->initiator_alias);
3646 printf("Target name: %s\n", conn->target);
3647 printf("Target alias: %s\n", conn->target_alias);
3648 printf("Header digest: %s\n", conn->header_digest);
3649 printf("Data digest: %s\n", conn->data_digest);
3650 printf("DataSegmentLen: %s\n", conn->max_data_segment_length);
3651 printf("ImmediateData: %s\n", conn->immediate_data ? "Yes" : "No");
3652 printf("iSER (RDMA): %s\n", conn->iser ? "Yes" : "No");
3656 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3658 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3659 printf("%4u %-16s %-36s %-36s\n",
3660 conn->connection_id, conn->initiator_addr, conn->initiator,
3671 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3673 struct ctl_iscsi req;
3675 int all = 0, connection_id = -1, nargs = 0;
3676 char *initiator_name = NULL, *initiator_addr = NULL;
3678 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3685 connection_id = strtoul(optarg, NULL, 0);
3689 initiator_name = strdup(optarg);
3690 if (initiator_name == NULL)
3691 err(1, "%s: strdup", __func__);
3695 initiator_addr = strdup(optarg);
3696 if (initiator_addr == NULL)
3697 err(1, "%s: strdup", __func__);
3706 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3709 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3712 bzero(&req, sizeof(req));
3713 req.type = CTL_ISCSI_LOGOUT;
3714 req.data.logout.connection_id = connection_id;
3715 if (initiator_addr != NULL)
3716 strlcpy(req.data.logout.initiator_addr,
3717 initiator_addr, sizeof(req.data.logout.initiator_addr));
3718 if (initiator_name != NULL)
3719 strlcpy(req.data.logout.initiator_name,
3720 initiator_name, sizeof(req.data.logout.initiator_name));
3722 req.data.logout.all = 1;
3724 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3725 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3730 if (req.status != CTL_ISCSI_OK) {
3731 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3732 __func__, req.error_str);
3737 printf("iSCSI logout requests submitted\n");
3744 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3746 struct ctl_iscsi req;
3748 int all = 0, connection_id = -1, nargs = 0;
3749 char *initiator_name = NULL, *initiator_addr = NULL;
3751 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3758 connection_id = strtoul(optarg, NULL, 0);
3762 initiator_name = strdup(optarg);
3763 if (initiator_name == NULL)
3764 err(1, "%s: strdup", __func__);
3768 initiator_addr = strdup(optarg);
3769 if (initiator_addr == NULL)
3770 err(1, "%s: strdup", __func__);
3779 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3782 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3785 bzero(&req, sizeof(req));
3786 req.type = CTL_ISCSI_TERMINATE;
3787 req.data.terminate.connection_id = connection_id;
3788 if (initiator_addr != NULL)
3789 strlcpy(req.data.terminate.initiator_addr,
3790 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3791 if (initiator_name != NULL)
3792 strlcpy(req.data.terminate.initiator_name,
3793 initiator_name, sizeof(req.data.terminate.initiator_name));
3795 req.data.terminate.all = 1;
3797 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3798 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3803 if (req.status != CTL_ISCSI_OK) {
3804 warnx("%s: error returned from CTL iSCSI connection "
3805 "termination request:\n%s", __func__, req.error_str);
3810 printf("iSCSI connections terminated\n");
3817 * Name/value pair used for per-LUN attributes.
3819 struct cctl_lun_nv {
3822 STAILQ_ENTRY(cctl_lun_nv) links;
3826 * Backend LUN information.
3831 uint64_t size_blocks;
3833 char *serial_number;
3835 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3836 STAILQ_ENTRY(cctl_lun) links;
3839 struct cctl_devlist_data {
3841 STAILQ_HEAD(,cctl_lun) lun_list;
3842 struct cctl_lun *cur_lun;
3844 struct sbuf *cur_sb[32];
3848 cctl_start_element(void *user_data, const char *name, const char **attr)
3851 struct cctl_devlist_data *devlist;
3852 struct cctl_lun *cur_lun;
3854 devlist = (struct cctl_devlist_data *)user_data;
3855 cur_lun = devlist->cur_lun;
3857 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3858 sizeof(devlist->cur_sb[0])))
3859 errx(1, "%s: too many nesting levels, %zd max", __func__,
3860 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3862 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3863 if (devlist->cur_sb[devlist->level] == NULL)
3864 err(1, "%s: Unable to allocate sbuf", __func__);
3866 if (strcmp(name, "lun") == 0) {
3867 if (cur_lun != NULL)
3868 errx(1, "%s: improper lun element nesting", __func__);
3870 cur_lun = calloc(1, sizeof(*cur_lun));
3871 if (cur_lun == NULL)
3872 err(1, "%s: cannot allocate %zd bytes", __func__,
3875 devlist->num_luns++;
3876 devlist->cur_lun = cur_lun;
3878 STAILQ_INIT(&cur_lun->attr_list);
3879 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3881 for (i = 0; attr[i] != NULL; i += 2) {
3882 if (strcmp(attr[i], "id") == 0) {
3883 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3885 errx(1, "%s: invalid LUN attribute %s = %s",
3886 __func__, attr[i], attr[i+1]);
3893 cctl_end_element(void *user_data, const char *name)
3895 struct cctl_devlist_data *devlist;
3896 struct cctl_lun *cur_lun;
3899 devlist = (struct cctl_devlist_data *)user_data;
3900 cur_lun = devlist->cur_lun;
3902 if ((cur_lun == NULL)
3903 && (strcmp(name, "ctllunlist") != 0))
3904 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3906 if (devlist->cur_sb[devlist->level] == NULL)
3907 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3908 devlist->level, name);
3910 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3911 err(1, "%s: sbuf_finish", __func__);
3912 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3914 err(1, "%s can't allocate %zd bytes for string", __func__,
3915 sbuf_len(devlist->cur_sb[devlist->level]));
3917 if (strlen(str) == 0) {
3922 sbuf_delete(devlist->cur_sb[devlist->level]);
3923 devlist->cur_sb[devlist->level] = NULL;
3926 if (strcmp(name, "backend_type") == 0) {
3927 cur_lun->backend_type = str;
3929 } else if (strcmp(name, "size") == 0) {
3930 cur_lun->size_blocks = strtoull(str, NULL, 0);
3931 } else if (strcmp(name, "blocksize") == 0) {
3932 cur_lun->blocksize = strtoul(str, NULL, 0);
3933 } else if (strcmp(name, "serial_number") == 0) {
3934 cur_lun->serial_number = str;
3936 } else if (strcmp(name, "device_id") == 0) {
3937 cur_lun->device_id = str;
3939 } else if (strcmp(name, "lun") == 0) {
3940 devlist->cur_lun = NULL;
3941 } else if (strcmp(name, "ctllunlist") == 0) {
3944 struct cctl_lun_nv *nv;
3946 nv = calloc(1, sizeof(*nv));
3948 err(1, "%s: can't allocate %zd bytes for nv pair",
3949 __func__, sizeof(*nv));
3951 nv->name = strdup(name);
3952 if (nv->name == NULL)
3953 err(1, "%s: can't allocated %zd bytes for string",
3954 __func__, strlen(name));
3958 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3965 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3967 struct cctl_devlist_data *devlist;
3969 devlist = (struct cctl_devlist_data *)user_data;
3971 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3975 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3977 struct ctl_lun_list list;
3978 struct cctl_devlist_data devlist;
3979 struct cctl_lun *lun;
3985 char *backend = NULL;
3991 bzero(&devlist, sizeof(devlist));
3992 STAILQ_INIT(&devlist.lun_list);
3994 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3997 backend = strdup(optarg);
4011 lun_str = malloc(lun_len);
4013 bzero(&list, sizeof(list));
4014 list.alloc_len = lun_len;
4015 list.status = CTL_LUN_LIST_NONE;
4016 list.lun_xml = lun_str;
4018 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
4019 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
4024 if (list.status == CTL_LUN_LIST_ERROR) {
4025 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
4026 __func__, list.error_str);
4027 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
4028 lun_len = lun_len << 1;
4032 if (dump_xml != 0) {
4033 printf("%s", lun_str);
4037 parser = XML_ParserCreate(NULL);
4038 if (parser == NULL) {
4039 warn("%s: Unable to create XML parser", __func__);
4044 XML_SetUserData(parser, &devlist);
4045 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
4046 XML_SetCharacterDataHandler(parser, cctl_char_handler);
4048 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
4049 XML_ParserFree(parser);
4055 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
4056 "Serial Number", "Device ID");
4057 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
4058 struct cctl_lun_nv *nv;
4060 if ((backend != NULL)
4061 && (strcmp(lun->backend_type, backend) != 0))
4064 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
4065 (uintmax_t)lun->lun_id,
4066 lun->backend_type, (uintmax_t)lun->size_blocks,
4067 lun->blocksize, lun->serial_number, lun->device_id);
4072 STAILQ_FOREACH(nv, &lun->attr_list, links) {
4073 printf(" %s=%s\n", nv->name, nv->value);
4088 char *frontend_type;
4092 STAILQ_HEAD(,cctl_lun_nv) attr_list;
4093 STAILQ_ENTRY(cctl_port) links;
4096 struct cctl_portlist_data {
4098 STAILQ_HEAD(,cctl_port) port_list;
4099 struct cctl_port *cur_port;
4101 struct sbuf *cur_sb[32];
4105 cctl_start_pelement(void *user_data, const char *name, const char **attr)
4108 struct cctl_portlist_data *portlist;
4109 struct cctl_port *cur_port;
4111 portlist = (struct cctl_portlist_data *)user_data;
4112 cur_port = portlist->cur_port;
4114 if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
4115 sizeof(portlist->cur_sb[0])))
4116 errx(1, "%s: too many nesting levels, %zd max", __func__,
4117 sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
4119 portlist->cur_sb[portlist->level] = sbuf_new_auto();
4120 if (portlist->cur_sb[portlist->level] == NULL)
4121 err(1, "%s: Unable to allocate sbuf", __func__);
4123 if (strcmp(name, "targ_port") == 0) {
4124 if (cur_port != NULL)
4125 errx(1, "%s: improper port element nesting", __func__);
4127 cur_port = calloc(1, sizeof(*cur_port));
4128 if (cur_port == NULL)
4129 err(1, "%s: cannot allocate %zd bytes", __func__,
4132 portlist->num_ports++;
4133 portlist->cur_port = cur_port;
4135 STAILQ_INIT(&cur_port->attr_list);
4136 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
4138 for (i = 0; attr[i] != NULL; i += 2) {
4139 if (strcmp(attr[i], "id") == 0) {
4140 cur_port->port_id = strtoull(attr[i+1], NULL, 0);
4142 errx(1, "%s: invalid LUN attribute %s = %s",
4143 __func__, attr[i], attr[i+1]);
4150 cctl_end_pelement(void *user_data, const char *name)
4152 struct cctl_portlist_data *portlist;
4153 struct cctl_port *cur_port;
4156 portlist = (struct cctl_portlist_data *)user_data;
4157 cur_port = portlist->cur_port;
4159 if ((cur_port == NULL)
4160 && (strcmp(name, "ctlportlist") != 0))
4161 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
4163 if (portlist->cur_sb[portlist->level] == NULL)
4164 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
4165 portlist->level, name);
4167 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
4168 err(1, "%s: sbuf_finish", __func__);
4169 str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
4171 err(1, "%s can't allocate %zd bytes for string", __func__,
4172 sbuf_len(portlist->cur_sb[portlist->level]));
4174 if (strlen(str) == 0) {
4179 sbuf_delete(portlist->cur_sb[portlist->level]);
4180 portlist->cur_sb[portlist->level] = NULL;
4183 if (strcmp(name, "frontend_type") == 0) {
4184 cur_port->frontend_type = str;
4186 } else if (strcmp(name, "port_name") == 0) {
4187 cur_port->name = str;
4189 } else if (strcmp(name, "online") == 0) {
4190 cur_port->online = str;
4192 } else if (strcmp(name, "physical_port") == 0) {
4193 cur_port->pp = strtoull(str, NULL, 0);
4194 } else if (strcmp(name, "virtual_port") == 0) {
4195 cur_port->vp = strtoull(str, NULL, 0);
4196 } else if (strcmp(name, "wwnn") == 0) {
4197 cur_port->wwnn = str;
4199 } else if (strcmp(name, "wwpn") == 0) {
4200 cur_port->wwpn = str;
4202 } else if (strcmp(name, "targ_port") == 0) {
4203 portlist->cur_port = NULL;
4204 } else if (strcmp(name, "ctlportlist") == 0) {
4207 struct cctl_lun_nv *nv;
4209 nv = calloc(1, sizeof(*nv));
4211 err(1, "%s: can't allocate %zd bytes for nv pair",
4212 __func__, sizeof(*nv));
4214 nv->name = strdup(name);
4215 if (nv->name == NULL)
4216 err(1, "%s: can't allocated %zd bytes for string",
4217 __func__, strlen(name));
4221 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
4228 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
4230 struct cctl_portlist_data *portlist;
4232 portlist = (struct cctl_portlist_data *)user_data;
4234 sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
4238 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
4240 struct ctl_lun_list list;
4241 struct cctl_portlist_data portlist;
4242 struct cctl_port *port;
4248 char *frontend = NULL;
4254 bzero(&portlist, sizeof(portlist));
4255 STAILQ_INIT(&portlist.port_list);
4257 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4260 frontend = strdup(optarg);
4274 port_str = malloc(port_len);
4276 bzero(&list, sizeof(list));
4277 list.alloc_len = port_len;
4278 list.status = CTL_LUN_LIST_NONE;
4279 list.lun_xml = port_str;
4281 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
4282 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
4287 if (list.status == CTL_LUN_LIST_ERROR) {
4288 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
4289 __func__, list.error_str);
4290 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
4291 port_len = port_len << 1;
4295 if (dump_xml != 0) {
4296 printf("%s", port_str);
4300 parser = XML_ParserCreate(NULL);
4301 if (parser == NULL) {
4302 warn("%s: Unable to create XML parser", __func__);
4307 XML_SetUserData(parser, &portlist);
4308 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
4309 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
4311 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
4312 XML_ParserFree(parser);
4318 printf("Port Online Frontend %-12s pp vp %-18s %-18s\n",
4319 "Name", "WWNN", "WWPN");
4320 STAILQ_FOREACH(port, &portlist.port_list, links) {
4321 struct cctl_lun_nv *nv;
4323 if ((frontend != NULL)
4324 && (strcmp(port->frontend_type, frontend) != 0))
4327 printf("%-4ju %-6s %-8s %-12s %-2d %-2d %-18s %-18s\n",
4328 (uintmax_t)port->port_id, port->online,
4329 port->frontend_type, port->name, port->pp, port->vp,
4330 port->wwnn, port->wwpn);
4335 STAILQ_FOREACH(nv, &port->attr_list, links) {
4336 printf(" %s=%s\n", nv->name, nv->value);
4348 fprintf(error ? stderr : stdout,
4350 "Primary commands:\n"
4351 " ctladm tur [dev_id][general options]\n"
4352 " ctladm inquiry [dev_id][general options]\n"
4353 " ctladm devid [dev_id][general options]\n"
4354 " ctladm reqsense [dev_id][general options]\n"
4355 " ctladm reportluns [dev_id][general options]\n"
4356 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
4357 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4358 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
4359 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4360 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
4361 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
4362 " [-d] [-S subpage] [-c cdbsize]\n"
4363 " ctladm prin [dev_id][general options] <-a action>\n"
4364 " ctladm prout [dev_id][general options] <-a action>\n"
4365 " <-r restype] [-k key] [-s sa_key]\n"
4366 " ctladm rtpg [dev_id][general options]\n"
4367 " ctladm start [dev_id][general options] [-i] [-o]\n"
4368 " ctladm stop [dev_id][general options] [-i] [-o]\n"
4369 " ctladm synccache [dev_id][general options] [-l lba]\n"
4370 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
4371 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
4372 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
4373 " [-S serial_num] [-t dev_type]\n"
4374 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
4375 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
4376 " ctladm devlist [-b backend] [-v] [-x]\n"
4377 " ctladm shutdown\n"
4379 " ctladm hardstop\n"
4380 " ctladm hardstart\n"
4382 " ctladm bbrread [dev_id] <-l lba> <-d datalen>\n"
4383 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
4385 " ctladm realsync <on|off|query>\n"
4386 " ctladm setsync [dev_id] <-i interval>\n"
4387 " ctladm getsync [dev_id]\n"
4388 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
4389 " [-s len fmt [args]] [-c] [-d delete_id]\n"
4390 " ctladm port <-l | -o <on|off> | [-w wwnn][-W wwpn]>\n"
4391 " [-p targ_port] [-t port_type] [-q] [-x]\n"
4392 " ctladm portlist [-f frontend] [-v] [-x]\n"
4393 " ctladm islist [-v | -x]\n"
4394 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
4395 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
4397 " ctladm dumpstructs\n"
4399 "General Options:\n"
4400 "-I intiator_id : defaults to 7, used to change the initiator id\n"
4401 "-C retries : specify the number of times to retry this command\n"
4402 "-D devicename : specify the device to operate on\n"
4403 " : (default is %s)\n"
4404 "read/write options:\n"
4405 "-l lba : logical block address\n"
4406 "-d len : read/write length, in blocks\n"
4407 "-f file|- : write/read data to/from file or stdout/stdin\n"
4408 "-b blocksize : block size, in bytes\n"
4409 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
4410 "-N : do not copy data to/from userland\n"
4411 "readcapacity options:\n"
4412 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4413 "modesense options:\n"
4414 "-m page : specify the mode page to view\n"
4415 "-l : request a list of supported pages\n"
4416 "-P pc : specify the page control value: 0-3 (current,\n"
4417 " changeable, default, saved, respectively)\n"
4418 "-d : disable block descriptors for mode sense\n"
4419 "-S subpage : specify a subpage\n"
4420 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
4421 "persistent reserve in options:\n"
4422 "-a action : specify the action value: 0-2 (read key, read\n"
4423 " reservation, read capabilities, respectively)\n"
4424 "persistent reserve out options:\n"
4425 "-a action : specify the action value: 0-5 (register, reserve,\n"
4426 " release, clear, preempt, register and ignore)\n"
4427 "-k key : key value\n"
4428 "-s sa_key : service action value\n"
4429 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
4430 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
4431 "start/stop options:\n"
4432 "-i : set the immediate bit (CTL does not support this)\n"
4433 "-o : set the on/offline bit\n"
4434 "synccache options:\n"
4435 "-l lba : set the starting LBA\n"
4436 "-b blockcount : set the length to sync in blocks\n"
4437 "-r : set the relative addressing bit\n"
4438 "-i : set the immediate bit\n"
4439 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4441 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4442 "-B blocksize : LUN blocksize in bytes (some backends)\n"
4443 "-d device_id : SCSI VPD page 0x83 ID\n"
4444 "-l lun_id : requested LUN number\n"
4445 "-o name=value : backend-specific options, multiple allowed\n"
4446 "-s size_bytes : LUN size in bytes (some backends)\n"
4447 "-S serial_num : SCSI VPD page 0x80 serial number\n"
4448 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
4450 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4451 "-l lun_id : LUN number to delete\n"
4452 "-o name=value : backend-specific options, multiple allowed\n"
4453 "devlist options:\n"
4454 "-b backend : list devices from specified backend only\n"
4455 "-v : be verbose, show backend attributes\n"
4456 "-x : dump raw XML\n"
4458 "-l datamove|done : delay command at datamove or done phase\n"
4459 "-T oneshot : delay one command, then resume normal completion\n"
4460 "-T cont : delay all commands\n"
4461 "-t secs : number of seconds to delay\n"
4463 "-i error_action : action to perform\n"
4464 "-p pattern : command pattern to look for\n"
4465 "-r lba,len : LBA range for pattern\n"
4466 "-s len fmt [args] : sense data for custom sense action\n"
4467 "-c : continuous operation\n"
4468 "-d delete_id : error id to delete\n"
4470 "-l : list frontend ports\n"
4471 "-o on|off : turn frontend ports on or off\n"
4472 "-w wwnn : set WWNN for one frontend\n"
4473 "-W wwpn : set WWPN for one frontend\n"
4474 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
4475 "-p targ_port : specify target port number\n"
4476 "-q : omit header in list output\n"
4477 "-x : output port list in XML format\n"
4478 "bbrread options:\n"
4479 "-l lba : starting LBA\n"
4480 "-d datalen : length, in bytes, to read\n",
4485 main(int argc, char **argv)
4488 ctladm_cmdfunction command;
4489 ctladm_cmdargs cmdargs;
4490 ctladm_optret optreturn;
4492 const char *mainopt = "C:D:I:";
4493 const char *subopt = NULL;
4494 char combinedopt[256];
4503 cmdargs = CTLADM_ARG_NONE;
4504 command = CTLADM_CMD_HELP;
4519 * Get the base option.
4521 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4523 if (optreturn == CC_OR_AMBIGUOUS) {
4524 warnx("ambiguous option %s", argv[1]);
4527 } else if (optreturn == CC_OR_NOT_FOUND) {
4528 warnx("option %s not found", argv[1]);
4533 if (cmdargs & CTLADM_ARG_NEED_TL) {
4535 || (!isdigit(argv[2][0]))) {
4536 warnx("option %s requires a target:lun argument",
4541 retval = cctl_parse_tl(argv[2], &target, &lun);
4543 errx(1, "invalid target:lun argument %s", argv[2]);
4545 cmdargs |= CTLADM_ARG_TARG_LUN;
4550 * Ahh, getopt(3) is a pain.
4552 * This is a gross hack. There really aren't many other good
4553 * options (excuse the pun) for parsing options in a situation like
4554 * this. getopt is kinda braindead, so you end up having to run
4555 * through the options twice, and give each invocation of getopt
4556 * the option string for the other invocation.
4558 * You would think that you could just have two groups of options.
4559 * The first group would get parsed by the first invocation of
4560 * getopt, and the second group would get parsed by the second
4561 * invocation of getopt. It doesn't quite work out that way. When
4562 * the first invocation of getopt finishes, it leaves optind pointing
4563 * to the argument _after_ the first argument in the second group.
4564 * So when the second invocation of getopt comes around, it doesn't
4565 * recognize the first argument it gets and then bails out.
4567 * A nice alternative would be to have a flag for getopt that says
4568 * "just keep parsing arguments even when you encounter an unknown
4569 * argument", but there isn't one. So there's no real clean way to
4570 * easily parse two sets of arguments without having one invocation
4571 * of getopt know about the other.
4573 * Without this hack, the first invocation of getopt would work as
4574 * long as the generic arguments are first, but the second invocation
4575 * (in the subfunction) would fail in one of two ways. In the case
4576 * where you don't set optreset, it would fail because optind may be
4577 * pointing to the argument after the one it should be pointing at.
4578 * In the case where you do set optreset, and reset optind, it would
4579 * fail because getopt would run into the first set of options, which
4580 * it doesn't understand.
4582 * All of this would "sort of" work if you could somehow figure out
4583 * whether optind had been incremented one option too far. The
4584 * mechanics of that, however, are more daunting than just giving
4585 * both invocations all of the expect options for either invocation.
4587 * Needless to say, I wouldn't mind if someone invented a better
4588 * (non-GPL!) command line parsing interface than getopt. I
4589 * wouldn't mind if someone added more knobs to getopt to make it
4590 * work better. Who knows, I may talk myself into doing it someday,
4591 * if the standards weenies let me. As it is, it just leads to
4592 * hackery like this and causes people to avoid it in some cases.
4594 * KDM, September 8th, 1998
4597 sprintf(combinedopt, "%s%s", mainopt, subopt);
4599 sprintf(combinedopt, "%s", mainopt);
4602 * Start getopt processing at argv[2/3], since we've already
4603 * accepted argv[1..2] as the command name, and as a possible
4609 * Now we run through the argument list looking for generic
4610 * options, and ignoring options that possibly belong to
4613 while ((c = getopt(argc, argv, combinedopt))!= -1){
4616 cmdargs |= CTLADM_ARG_RETRIES;
4617 retries = strtol(optarg, NULL, 0);
4620 device = strdup(optarg);
4621 cmdargs |= CTLADM_ARG_DEVICE;
4624 cmdargs |= CTLADM_ARG_INITIATOR;
4625 initid = strtol(optarg, NULL, 0);
4632 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4639 * Default to opening the CTL device for now.
4641 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4642 && (command != CTLADM_CMD_HELP)) {
4643 device = strdup(CTL_DEFAULT_DEV);
4644 cmdargs |= CTLADM_ARG_DEVICE;
4647 if ((cmdargs & CTLADM_ARG_DEVICE)
4648 && (command != CTLADM_CMD_HELP)) {
4649 fd = open(device, O_RDWR);
4650 if (fd == -1 && errno == ENOENT) {
4651 saved_errno = errno;
4652 retval = kldload("ctl");
4654 fd = open(device, O_RDWR);
4656 errno = saved_errno;
4659 fprintf(stderr, "%s: error opening %s: %s\n",
4660 argv[0], device, strerror(errno));
4664 } else if ((command != CTLADM_CMD_HELP)
4665 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4666 fprintf(stderr, "%s: you must specify a device with the "
4667 "--device argument for this command\n", argv[0]);
4668 command = CTLADM_CMD_HELP;
4673 case CTLADM_CMD_TUR:
4674 retval = cctl_tur(fd, target, lun, initid, retries);
4676 case CTLADM_CMD_INQUIRY:
4677 retval = cctl_inquiry(fd, target, lun, initid, retries);
4679 case CTLADM_CMD_REQ_SENSE:
4680 retval = cctl_req_sense(fd, target, lun, initid, retries);
4682 case CTLADM_CMD_REPORT_LUNS:
4683 retval = cctl_report_luns(fd, target, lun, initid, retries);
4685 case CTLADM_CMD_CREATE:
4686 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4689 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4691 case CTLADM_CMD_DEVLIST:
4692 retval = cctl_devlist(fd, argc, argv, combinedopt);
4694 case CTLADM_CMD_READ:
4695 case CTLADM_CMD_WRITE:
4696 retval = cctl_read_write(fd, target, lun, initid, retries,
4697 argc, argv, combinedopt, command);
4699 case CTLADM_CMD_PORT:
4700 retval = cctl_port(fd, argc, argv, combinedopt);
4702 case CTLADM_CMD_PORTLIST:
4703 retval = cctl_portlist(fd, argc, argv, combinedopt);
4705 case CTLADM_CMD_READCAPACITY:
4706 retval = cctl_read_capacity(fd, target, lun, initid, retries,
4707 argc, argv, combinedopt);
4709 case CTLADM_CMD_MODESENSE:
4710 retval = cctl_mode_sense(fd, target, lun, initid, retries,
4711 argc, argv, combinedopt);
4713 case CTLADM_CMD_START:
4714 case CTLADM_CMD_STOP:
4715 retval = cctl_start_stop(fd, target, lun, initid, retries,
4716 (command == CTLADM_CMD_START) ? 1 : 0,
4717 argc, argv, combinedopt);
4719 case CTLADM_CMD_SYNC_CACHE:
4720 retval = cctl_sync_cache(fd, target, lun, initid, retries,
4721 argc, argv, combinedopt);
4723 case CTLADM_CMD_SHUTDOWN:
4724 case CTLADM_CMD_STARTUP:
4725 retval = cctl_startup_shutdown(fd, target, lun, initid,
4728 case CTLADM_CMD_HARDSTOP:
4729 case CTLADM_CMD_HARDSTART:
4730 retval = cctl_hardstopstart(fd, command);
4732 case CTLADM_CMD_BBRREAD:
4733 retval = cctl_bbrread(fd, target, lun, initid, argc, argv,
4736 case CTLADM_CMD_LUNLIST:
4737 retval = cctl_lunlist(fd);
4739 case CTLADM_CMD_DELAY:
4740 retval = cctl_delay(fd, target, lun, argc, argv, combinedopt);
4742 case CTLADM_CMD_REALSYNC:
4743 retval = cctl_realsync(fd, argc, argv);
4745 case CTLADM_CMD_SETSYNC:
4746 case CTLADM_CMD_GETSYNC:
4747 retval = cctl_getsetsync(fd, target, lun, command,
4748 argc, argv, combinedopt);
4750 case CTLADM_CMD_ERR_INJECT:
4751 retval = cctl_error_inject(fd, target, lun, argc, argv,
4754 case CTLADM_CMD_DUMPOOA:
4755 retval = cctl_dump_ooa(fd, argc, argv);
4757 case CTLADM_CMD_DUMPSTRUCTS:
4758 retval = cctl_dump_structs(fd, cmdargs);
4760 case CTLADM_CMD_PRES_IN:
4761 retval = cctl_persistent_reserve_in(fd, target, lun, initid,
4762 argc, argv, combinedopt,
4765 case CTLADM_CMD_PRES_OUT:
4766 retval = cctl_persistent_reserve_out(fd, target, lun, initid,
4767 argc, argv, combinedopt,
4770 case CTLADM_CMD_INQ_VPD_DEVID:
4771 retval = cctl_inquiry_vpd_devid(fd, target, lun, initid);
4773 case CTLADM_CMD_RTPG:
4774 retval = cctl_report_target_port_group(fd, target, lun, initid);
4776 case CTLADM_CMD_MODIFY:
4777 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4779 case CTLADM_CMD_ISLIST:
4780 retval = cctl_islist(fd, argc, argv, combinedopt);
4782 case CTLADM_CMD_ISLOGOUT:
4783 retval = cctl_islogout(fd, argc, argv, combinedopt);
4785 case CTLADM_CMD_ISTERMINATE:
4786 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4788 case CTLADM_CMD_HELP: