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:
509 * If the user specified a frontend number or a particular
510 * frontend type, only print out that particular frontend
513 if ((targ_port != -1)
514 && (targ_port != entry->targ_port))
516 else if ((port_type != CTL_PORT_NONE)
517 && ((port_type & entry->port_type) == 0))
523 printf("Num: %ju Type: %s (%#x) Name: %s Physical Port: %d "
524 "Virtual Port: %d\n", (uintmax_t)entry->fe_num, type,
525 entry->port_type, entry->fe_name, entry->physical_port,
526 entry->virtual_port);
527 printf("WWNN %#jx WWPN %#jx Online: %s\n",
528 (uintmax_t)entry->wwnn, (uintmax_t)entry->wwpn,
529 (entry->online) ? "YES" : "NO" );
532 printf("%-4d %-6s %-8s %-12s %-2d %-2d %#-18jx "
534 entry->targ_port, (entry->online) ? "YES" : "NO",
535 type, entry->port_name, entry->physical_port,
536 entry->virtual_port, (uintmax_t)entry->wwnn,
537 (uintmax_t)entry->wwpn);
539 sbuf_printf(sb, "<targ_port id=\"%d\">\n",
541 sbuf_printf(sb, "<online>%s</online>\n",
542 (entry->online) ? "YES" : "NO");
543 sbuf_printf(sb, "<port_type>%s</port_type>\n", type);
544 sbuf_printf(sb, "<port_name>%s</port_name>\n",
546 sbuf_printf(sb, "<physical_port>%d</physical_port>\n",
547 entry->physical_port);
548 sbuf_printf(sb, "<virtual_port>%d</virtual_port>\n",
549 entry->virtual_port);
550 sbuf_printf(sb, "<wwnn>%#jx</wwnn>\n",
551 (uintmax_t)entry->wwnn);
552 sbuf_printf(sb, "<wwpn>%#jx</wwpn>\n",
553 (uintmax_t)entry->wwpn);
554 sbuf_printf(sb, "</targ_port>\n");
559 sbuf_printf(sb, "</ctlfelist>\n");
560 if (sbuf_finish(sb) != 0)
561 err(1, "%s: sbuf_finish", __func__);
562 printf("%s", sbuf_data(sb));
567 * Give some indication that we didn't find the frontend or
568 * frontend type requested by the user. We could print something
569 * out, but it would probably be better to hide that behind a
573 && ((targ_port != -1)
574 || (port_type != CTL_PORT_NONE)))
588 static struct ctladm_opts cctl_fe_table[] = {
589 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
590 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
591 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
592 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
593 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
598 cctl_port(int fd, int argc, char **argv, char *combinedopt)
601 int32_t targ_port = -1;
603 int wwnn_set = 0, wwpn_set = 0;
604 uint64_t wwnn = 0, wwpn = 0;
605 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
606 struct ctl_port_entry entry;
607 ctl_port_type port_type = CTL_PORT_NONE;
608 int quiet = 0, xml = 0;
610 while ((c = getopt(argc, argv, combinedopt)) != -1) {
613 if (port_mode != CCTL_PORT_MODE_NONE)
616 port_mode = CCTL_PORT_MODE_LIST;
619 if (port_mode != CCTL_PORT_MODE_NONE)
622 if (strcasecmp(optarg, "on") == 0)
623 port_mode = CCTL_PORT_MODE_ON;
624 else if (strcasecmp(optarg, "off") == 0)
625 port_mode = CCTL_PORT_MODE_OFF;
627 warnx("Invalid -o argument %s, \"on\" or "
628 "\"off\" are the only valid args",
635 targ_port = strtol(optarg, NULL, 0);
641 ctladm_optret optret;
642 ctladm_cmdargs argnum;
644 ctl_port_type tmp_port_type;
646 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
648 if (optret == CC_OR_AMBIGUOUS) {
649 warnx("%s: ambiguous frontend type %s",
653 } else if (optret == CC_OR_NOT_FOUND) {
654 warnx("%s: invalid frontend type %s",
660 port_type |= tmp_port_type;
664 if ((port_mode != CCTL_PORT_MODE_NONE)
665 && (port_mode != CCTL_PORT_MODE_SET))
668 port_mode = CCTL_PORT_MODE_SET;
670 wwnn = strtoull(optarg, NULL, 0);
674 if ((port_mode != CCTL_PORT_MODE_NONE)
675 && (port_mode != CCTL_PORT_MODE_SET))
678 port_mode = CCTL_PORT_MODE_SET;
680 wwpn = strtoull(optarg, NULL, 0);
690 * The user can specify either one or more frontend types (-t), or
691 * a specific frontend, but not both.
693 * If the user didn't specify a frontend type or number, set it to
694 * all. This is primarily needed for the enable/disable ioctls.
695 * This will be a no-op for the listing code. For the set ioctl,
696 * we'll throw an error, since that only works on one port at a time.
698 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
699 warnx("%s: can only specify one of -t or -n", __func__);
702 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
703 port_type = CTL_PORT_ALL;
705 bzero(&entry, sizeof(entry));
708 * These are needed for all but list/dump mode.
710 entry.port_type = port_type;
711 entry.targ_port = targ_port;
714 case CCTL_PORT_MODE_LIST:
715 cctl_port_dump(fd, quiet, xml, targ_port, port_type);
717 case CCTL_PORT_MODE_SET:
718 if (targ_port == -1) {
719 warnx("%s: -w and -W require -n", __func__);
725 entry.flags |= CTL_PORT_WWNN_VALID;
729 entry.flags |= CTL_PORT_WWPN_VALID;
733 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
734 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
739 case CCTL_PORT_MODE_ON:
740 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
741 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
745 fprintf(stdout, "Front End Ports enabled\n");
747 case CCTL_PORT_MODE_OFF:
748 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
749 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
753 fprintf(stdout, "Front End Ports disabled\n");
756 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
767 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
772 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
775 if (ioctl(fd, CTL_IO, io) == -1) {
776 warn("%s: error sending CTL_IO ioctl", func);
779 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
786 cctl_delay(int fd, int target, int lun, int argc, char **argv,
789 struct ctl_io_delay_info delay_info;
790 char *delayloc = NULL;
791 char *delaytype = NULL;
798 memset(&delay_info, 0, sizeof(delay_info));
800 while ((c = getopt(argc, argv, combinedopt)) != -1) {
803 delaytype = strdup(optarg);
806 delayloc = strdup(optarg);
809 delaytime = strtoul(optarg, NULL, 0);
814 if (delaytime == -1) {
815 warnx("%s: you must specify the delaytime with -t", __func__);
820 if (strcasecmp(delayloc, "datamove") == 0)
821 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
822 else if (strcasecmp(delayloc, "done") == 0)
823 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
825 warnx("%s: invalid delay location %s", __func__, delayloc);
830 if ((delaytype == NULL)
831 || (strcmp(delaytype, "oneshot") == 0))
832 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
833 else if (strcmp(delaytype, "cont") == 0)
834 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
836 warnx("%s: invalid delay type %s", __func__, delaytype);
841 delay_info.target_id = target;
842 delay_info.lun_id = lun;
843 delay_info.delay_secs = delaytime;
845 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
846 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
850 switch (delay_info.status) {
851 case CTL_DELAY_STATUS_NONE:
852 warnx("%s: no delay status??", __func__);
855 case CTL_DELAY_STATUS_OK:
857 case CTL_DELAY_STATUS_INVALID_LUN:
858 warnx("%s: invalid lun %d", __func__, lun);
861 case CTL_DELAY_STATUS_INVALID_TYPE:
862 warnx("%s: invalid delay type %d", __func__,
863 delay_info.delay_type);
866 case CTL_DELAY_STATUS_INVALID_LOC:
867 warnx("%s: delay location %s not implemented?", __func__,
871 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
872 warnx("%s: delay not implemented in the kernel", __func__);
873 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
877 warnx("%s: unknown delay return status %d", __func__,
884 /* delayloc should never be NULL, but just in case...*/
885 if (delayloc != NULL)
892 cctl_realsync(int fd, int argc, char **argv)
901 warnx("%s %s takes exactly one argument", argv[0], argv[1]);
908 if (strncasecmp(syncarg, "query", min(strlen(syncarg),
909 strlen("query"))) == 0) {
910 if (ioctl(fd, CTL_REALSYNC_GET, &syncstate) == -1) {
911 warn("%s: CTL_REALSYNC_GET ioctl failed", __func__);
915 fprintf(stdout, "SYNCHRONIZE CACHE support is: ");
918 fprintf(stdout, "OFF\n");
921 fprintf(stdout, "ON\n");
924 fprintf(stdout, "unknown (%d)\n", syncstate);
928 } else if (strcasecmp(syncarg, "on") == 0) {
930 } else if (strcasecmp(syncarg, "off") == 0) {
933 warnx("%s: invalid realsync argument %s", __func__, syncarg);
938 if (ioctl(fd, CTL_REALSYNC_SET, &syncstate) == -1) {
939 warn("%s: CTL_REALSYNC_SET ioctl failed", __func__);
948 cctl_getsetsync(int fd, int target, int lun, ctladm_cmdfunction command,
949 int argc, char **argv, char *combinedopt)
951 struct ctl_sync_info sync_info;
953 int sync_interval = -1;
959 memset(&sync_info, 0, sizeof(sync_info));
960 sync_info.target_id = target;
961 sync_info.lun_id = lun;
963 while ((c = getopt(argc, argv, combinedopt)) != -1) {
966 sync_interval = strtoul(optarg, NULL, 0);
973 if (command == CTLADM_CMD_SETSYNC) {
974 if (sync_interval == -1) {
975 warnx("%s: you must specify the sync interval with -i",
980 sync_info.sync_interval = sync_interval;
981 ioctl_cmd = CTL_SETSYNC;
983 ioctl_cmd = CTL_GETSYNC;
986 if (ioctl(fd, ioctl_cmd, &sync_info) == -1) {
987 warn("%s: CTL_%sSYNC ioctl failed", __func__,
988 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET");
993 switch (sync_info.status) {
995 if (command == CTLADM_CMD_GETSYNC) {
996 fprintf(stdout, "%d:%d: sync interval: %d\n",
997 target, lun, sync_info.sync_interval);
1000 case CTL_GS_SYNC_NO_LUN:
1001 warnx("%s: unknown target:LUN %d:%d", __func__, target, lun);
1004 case CTL_GS_SYNC_NONE:
1006 warnx("%s: unknown CTL_%sSYNC status %d", __func__,
1007 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET",
1016 static struct ctladm_opts cctl_err_types[] = {
1017 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
1018 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
1019 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
1020 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
1025 static struct ctladm_opts cctl_err_patterns[] = {
1026 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
1027 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
1028 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1029 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1030 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
1031 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
1032 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
1034 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
1040 cctl_error_inject(int fd, uint32_t target, uint32_t lun, int argc, char **argv,
1044 struct ctl_error_desc err_desc;
1047 uint64_t delete_id = 0;
1048 int delete_id_set = 0;
1054 bzero(&err_desc, sizeof(err_desc));
1055 err_desc.target_id = target;
1056 err_desc.lun_id = lun;
1058 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1064 delete_id = strtoull(optarg, NULL, 0);
1069 ctladm_optret optret;
1070 ctladm_cmdargs argnum;
1074 ctl_lun_error err_type;
1076 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
1077 warnx("%s: can't specify multiple -i "
1078 "arguments", __func__);
1082 optret = getoption(cctl_err_types, optarg,
1083 &err_type, &argnum, &subopt);
1084 err_desc.lun_error = err_type;
1086 ctl_lun_error_pattern pattern;
1088 optret = getoption(cctl_err_patterns, optarg,
1089 &pattern, &argnum, &subopt);
1090 err_desc.error_pattern |= pattern;
1093 if (optret == CC_OR_AMBIGUOUS) {
1094 warnx("%s: ambiguous argument %s", __func__,
1098 } else if (optret == CC_OR_NOT_FOUND) {
1099 warnx("%s: argument %s not found", __func__,
1107 char *tmpstr, *tmpstr2;
1109 tmpstr = strdup(optarg);
1110 if (tmpstr == NULL) {
1111 warn("%s: error duplicating string %s",
1117 tmpstr2 = strsep(&tmpstr, ",");
1118 if (tmpstr2 == NULL) {
1119 warnx("%s: invalid -r argument %s", __func__,
1125 lba = strtoull(tmpstr2, NULL, 0);
1126 tmpstr2 = strsep(&tmpstr, ",");
1127 if (tmpstr2 == NULL) {
1128 warnx("%s: no len argument for -r lba,len, got"
1129 " %s", __func__, optarg);
1134 len = strtoul(tmpstr2, NULL, 0);
1139 struct get_hook hook;
1142 sense_len = strtol(optarg, NULL, 0);
1143 if (sense_len <= 0) {
1144 warnx("invalid number of sense bytes %d",
1150 sense_len = MIN(sense_len, SSD_FULL_SIZE);
1152 hook.argc = argc - optind;
1153 hook.argv = argv + optind;
1156 sensestr = cget(&hook, NULL);
1157 if ((sensestr != NULL)
1158 && (sensestr[0] == '-')) {
1162 (uint8_t *)&err_desc.custom_sense,
1163 sense_len, sensestr, iget, &hook);
1173 if (delete_id_set != 0) {
1174 err_desc.serial = delete_id;
1175 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
1176 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
1183 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
1184 warnx("%s: error injection command (-i) needed",
1188 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
1189 && (sense_len == 0)) {
1190 warnx("%s: custom error requires -s", __func__);
1195 if (continuous != 0)
1196 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
1199 * If fd_sense is set, we need to read the sense data the user
1200 * wants returned from stdin.
1202 if (fd_sense == 1) {
1204 int amt_to_read = sense_len;
1205 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
1207 for (amt_read = 0; amt_to_read > 0;
1208 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1209 if (amt_read == -1) {
1210 warn("error reading sense data from stdin");
1214 amt_to_read -= amt_read;
1215 buf_ptr += amt_read;
1219 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1220 warnx("%s: command pattern (-p) needed", __func__);
1226 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1228 * We could check here to see whether it's a read/write
1229 * command, but that will be pointless once we allow
1230 * custom patterns. At that point, the user could specify
1231 * a READ(6) CDB type, and we wouldn't have an easy way here
1232 * to verify whether range checking is possible there. The
1233 * user will just figure it out when his error never gets
1237 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1238 warnx("%s: need read and/or write pattern if range "
1239 "is specified", __func__);
1244 err_desc.lba_range.lba = lba;
1245 err_desc.lba_range.len = len;
1248 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1249 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1252 printf("Error injection succeeded, serial number is %ju\n",
1253 (uintmax_t)err_desc.serial);
1261 cctl_lunlist(int fd)
1263 struct scsi_report_luns_data *lun_data;
1264 struct scsi_inquiry_data *inq_data;
1278 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1279 * if we ever acquire the ability to have multiple targets.
1281 if ((retval = cctl_get_luns(fd, target, /*lun*/ 0, initid,
1282 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1285 inq_data = malloc(sizeof(*inq_data));
1286 if (inq_data == NULL) {
1287 warn("%s: couldn't allocate memory for inquiry data\n",
1292 for (i = 0; i < num_luns; i++) {
1296 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1297 case RPL_LUNDATA_ATYP_PERIPH:
1298 lun_val = lun_data->luns[i].lundata[1];
1300 case RPL_LUNDATA_ATYP_FLAT:
1301 lun_val = (lun_data->luns[i].lundata[0] &
1302 RPL_LUNDATA_FLAT_LUN_MASK) |
1303 (lun_data->luns[i].lundata[1] <<
1304 RPL_LUNDATA_FLAT_LUN_BITS);
1306 case RPL_LUNDATA_ATYP_LUN:
1307 case RPL_LUNDATA_ATYP_EXTLUN:
1309 fprintf(stdout, "Unsupported LUN format %d\n",
1310 lun_data->luns[i].lundata[0] &
1311 RPL_LUNDATA_ATYP_MASK);
1318 if ((retval = cctl_get_inquiry(fd, target, lun_val, initid,
1319 /*retries*/ 2, scsi_path,
1324 printf("%s", scsi_path);
1325 scsi_print_inquiry(inq_data);
1329 if (lun_data != NULL)
1332 if (inq_data != NULL)
1339 cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len)
1342 case CFI_MT_PORT_OFFLINE:
1343 snprintf(str, str_len, "Port Offline");
1346 snprintf(str, str_len, "Error");
1348 case CFI_MT_SUCCESS:
1349 snprintf(str, str_len, "Success");
1352 snprintf(str, str_len, "None??");
1355 snprintf(str, str_len, "Unknown status: %d", status);
1361 cctl_cfi_bbr_statusstr(cfi_bbrread_status status, char *str, int str_len)
1364 case CFI_BBR_SUCCESS:
1365 snprintf(str, str_len, "Success");
1367 case CFI_BBR_LUN_UNCONFIG:
1368 snprintf(str, str_len, "LUN not configured");
1370 case CFI_BBR_NO_LUN:
1371 snprintf(str, str_len, "LUN does not exist");
1373 case CFI_BBR_NO_MEM:
1374 snprintf(str, str_len, "Memory allocation error");
1376 case CFI_BBR_BAD_LEN:
1377 snprintf(str, str_len, "Length is not a multiple of blocksize");
1379 case CFI_BBR_RESERV_CONFLICT:
1380 snprintf(str, str_len, "Reservation conflict");
1382 case CFI_BBR_LUN_STOPPED:
1383 snprintf(str, str_len, "LUN is powered off");
1385 case CFI_BBR_LUN_OFFLINE_CTL:
1386 snprintf(str, str_len, "LUN is offline");
1388 case CFI_BBR_LUN_OFFLINE_RC:
1389 snprintf(str, str_len, "RAIDCore array is offline (double "
1392 case CFI_BBR_SCSI_ERROR:
1393 snprintf(str, str_len, "SCSI Error");
1396 snprintf(str, str_len, "Error");
1399 snprintf(str, str_len, "Unknown status: %d", status);
1405 cctl_hardstopstart(int fd, ctladm_cmdfunction command)
1407 struct ctl_hard_startstop_info hs_info;
1408 char error_str[256];
1414 if (command == CTLADM_CMD_HARDSTART)
1419 if (ioctl(fd, (do_start == 1) ? CTL_HARD_START : CTL_HARD_STOP,
1421 warn("%s: CTL_HARD_%s ioctl failed", __func__,
1422 (do_start == 1) ? "START" : "STOP");
1427 fprintf(stdout, "Hard %s Status: ", (command == CTLADM_CMD_HARDSTOP) ?
1429 cctl_cfi_mt_statusstr(hs_info.status, error_str, sizeof(error_str));
1430 fprintf(stdout, "%s\n", error_str);
1431 fprintf(stdout, "Total LUNs: %d\n", hs_info.total_luns);
1432 fprintf(stdout, "LUNs complete: %d\n", hs_info.luns_complete);
1433 fprintf(stdout, "LUNs failed: %d\n", hs_info.luns_failed);
1440 cctl_bbrread(int fd, int target __unused, int lun, int iid __unused,
1441 int argc, char **argv, char *combinedopt)
1443 struct ctl_bbrread_info bbr_info;
1444 char error_str[256];
1453 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1456 datalen = strtoul(optarg, NULL, 0);
1459 lba = strtoull(optarg, NULL, 0);
1468 warnx("%s: you must specify an LBA with -l", __func__);
1473 if (datalen == -1) {
1474 warnx("%s: you must specify a length with -d", __func__);
1479 bbr_info.lun_num = lun;
1482 * XXX KDM get the blocksize first??
1484 if ((datalen % 512) != 0) {
1485 warnx("%s: data length %d is not a multiple of 512 bytes",
1490 bbr_info.len = datalen;
1492 if (ioctl(fd, CTL_BBRREAD, &bbr_info) == -1) {
1493 warn("%s: CTL_BBRREAD ioctl failed", __func__);
1497 cctl_cfi_mt_statusstr(bbr_info.status, error_str, sizeof(error_str));
1498 fprintf(stdout, "BBR Read Overall Status: %s\n", error_str);
1499 cctl_cfi_bbr_statusstr(bbr_info.bbr_status, error_str,
1501 fprintf(stdout, "BBR Read Status: %s\n", error_str);
1503 * XXX KDM should we bother printing out SCSI status if we get
1504 * CFI_BBR_SCSI_ERROR back?
1506 * Return non-zero if this fails?
1513 cctl_startup_shutdown(int fd, int target, int lun, int iid,
1514 ctladm_cmdfunction command)
1518 struct scsi_report_luns_data *lun_data;
1519 struct scsi_inquiry_data *inq_data;
1529 * - step through each lun, do an inquiry
1530 * - check OOA queue on direct access luns
1531 * - send stop with offline bit to each direct access device with a
1533 * - if we get a reservation conflict, reset the LUN to clear it
1534 * and reissue the stop with the offline bit set
1539 io = ctl_scsi_alloc_io(id);
1541 warnx("%s: can't allocate memory", __func__);
1545 if ((retval = cctl_get_luns(fd, target, lun, iid, /*retries*/ 2,
1546 &lun_data, &num_luns)) != 0)
1549 inq_data = malloc(sizeof(*inq_data));
1550 if (inq_data == NULL) {
1551 warn("%s: couldn't allocate memory for inquiry data\n",
1556 for (i = 0; i < num_luns; i++) {
1561 * XXX KDM figure out a way to share this code with
1564 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1565 case RPL_LUNDATA_ATYP_PERIPH:
1566 lun_val = lun_data->luns[i].lundata[1];
1568 case RPL_LUNDATA_ATYP_FLAT:
1569 lun_val = (lun_data->luns[i].lundata[0] &
1570 RPL_LUNDATA_FLAT_LUN_MASK) |
1571 (lun_data->luns[i].lundata[1] <<
1572 RPL_LUNDATA_FLAT_LUN_BITS);
1574 case RPL_LUNDATA_ATYP_LUN:
1575 case RPL_LUNDATA_ATYP_EXTLUN:
1577 fprintf(stdout, "Unsupported LUN format %d\n",
1578 lun_data->luns[i].lundata[0] &
1579 RPL_LUNDATA_ATYP_MASK);
1586 if ((retval = cctl_get_inquiry(fd, target, lun_val, iid,
1587 /*retries*/ 2, scsi_path,
1592 printf("%s", scsi_path);
1593 scsi_print_inquiry(inq_data);
1595 * We only want to shutdown direct access devices.
1597 if (SID_TYPE(inq_data) != T_DIRECT) {
1598 printf("%s LUN is not direct access, skipped\n",
1603 if (command == CTLADM_CMD_SHUTDOWN) {
1604 struct ctl_ooa_info ooa_info;
1606 ooa_info.target_id = target;
1607 ooa_info.lun_id = lun_val;
1609 if (ioctl(fd, CTL_CHECK_OOA, &ooa_info) == -1) {
1610 printf("%s CTL_CHECK_OOA ioctl failed\n",
1615 if (ooa_info.status != CTL_OOA_SUCCESS) {
1616 printf("%s CTL_CHECK_OOA returned status %d\n",
1617 scsi_path, ooa_info.status);
1620 if (ooa_info.num_entries != 0) {
1621 printf("%s %d entr%s in the OOA queue, "
1622 "skipping shutdown\n", scsi_path,
1623 ooa_info.num_entries,
1624 (ooa_info.num_entries > 1)?"ies" : "y" );
1629 ctl_scsi_start_stop(/*io*/ io,
1630 /*start*/(command == CTLADM_CMD_STARTUP) ?
1634 /*power_conditions*/ SSS_PC_START_VALID,
1637 (command == CTLADM_CMD_STARTUP) ?
1638 CTL_TAG_SIMPLE :CTL_TAG_ORDERED,
1641 io->io_hdr.nexus.targ_target.id = target;
1642 io->io_hdr.nexus.targ_lun = lun_val;
1643 io->io_hdr.nexus.initid = id;
1645 if (cctl_do_io(fd, /*retries*/ 3, io, __func__) != 0) {
1650 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1651 ctl_io_error_print(io, inq_data, stderr);
1653 printf("%s LUN is now %s\n", scsi_path,
1654 (command == CTLADM_CMD_STARTUP) ? "online" :
1659 if (lun_data != NULL)
1662 if (inq_data != NULL)
1666 ctl_scsi_free_io(io);
1672 cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
1673 int argc, char **argv, char *combinedopt)
1679 uint64_t our_lba = 0;
1680 uint32_t our_block_count = 0;
1681 int reladr = 0, immed = 0;
1687 io = ctl_scsi_alloc_io(id);
1689 warnx("%s: can't allocate memory", __func__);
1693 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1696 our_block_count = strtoul(optarg, NULL, 0);
1699 cdb_size = strtol(optarg, NULL, 0);
1705 our_lba = strtoull(optarg, NULL, 0);
1715 if (cdb_size != -1) {
1721 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1722 "and 16", __func__, cdb_size);
1725 break; /* NOTREACHED */
1730 ctl_scsi_sync_cache(/*io*/ io,
1733 /*minimum_cdb_size*/ cdb_size,
1734 /*starting_lba*/ our_lba,
1735 /*block_count*/ our_block_count,
1736 /*tag_type*/ CTL_TAG_SIMPLE,
1739 io->io_hdr.nexus.targ_target.id = target;
1740 io->io_hdr.nexus.targ_lun = lun;
1741 io->io_hdr.nexus.initid = id;
1743 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1748 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1749 fprintf(stdout, "Cache synchronized successfully\n");
1751 ctl_io_error_print(io, NULL, stderr);
1753 ctl_scsi_free_io(io);
1759 cctl_start_stop(int fd, int target, int lun, int iid, int retries, int start,
1760 int argc, char **argv, char *combinedopt)
1765 int immed = 0, onoffline = 0;
1771 io = ctl_scsi_alloc_io(id);
1773 warnx("%s: can't allocate memory", __func__);
1777 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1790 * Use an ordered tag for the stop command, to guarantee that any
1791 * pending I/O will finish before the stop command executes. This
1792 * would normally be the case anyway, since CTL will basically
1793 * treat the start/stop command as an ordered command with respect
1794 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1796 ctl_scsi_start_stop(/*io*/ io,
1799 /*immediate*/ immed,
1800 /*power_conditions*/ SSS_PC_START_VALID,
1801 /*onoffline*/ onoffline,
1802 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1806 io->io_hdr.nexus.targ_target.id = target;
1807 io->io_hdr.nexus.targ_lun = lun;
1808 io->io_hdr.nexus.initid = id;
1810 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1815 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1816 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1817 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1818 (start) ? "started" : "stopped");
1820 ctl_io_error_print(io, NULL, stderr);
1823 ctl_scsi_free_io(io);
1829 cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
1830 int argc, char **argv, char *combinedopt)
1836 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1846 io = ctl_scsi_alloc_io(id);
1848 warn("%s: can't allocate memory", __func__);
1852 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1855 pc = strtoul(optarg, NULL, 0);
1858 subpage = strtoul(optarg, NULL, 0);
1867 page_code = strtoul(optarg, NULL, 0);
1870 cdbsize = strtol(optarg, NULL, 0);
1877 if (((list == 0) && (page_code == -1))
1878 || ((list != 0) && (page_code != -1))) {
1879 warnx("%s: you must specify either a page code (-m) or -l",
1885 if ((page_code != -1)
1886 && ((page_code > SMS_ALL_PAGES_PAGE)
1887 || (page_code < 0))) {
1888 warnx("%s: page code %d is out of range", __func__,
1895 page_code = SMS_ALL_PAGES_PAGE;
1897 warnx("%s: arg -P makes no sense with -l",
1902 if (subpage != -1) {
1903 warnx("%s: arg -S makes no sense with -l", __func__);
1910 pc = SMS_PAGE_CTRL_CURRENT;
1914 warnx("%s: page control value %d is out of range: 0-3",
1924 || (subpage < 0))) {
1925 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1936 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1937 "and 10", __func__, cdbsize);
1953 dataptr = (uint8_t *)malloc(datalen);
1954 if (dataptr == NULL) {
1955 warn("%s: can't allocate %d bytes", __func__, datalen);
1960 memset(dataptr, 0, datalen);
1962 ctl_scsi_mode_sense(io,
1963 /*data_ptr*/ dataptr,
1964 /*data_len*/ datalen,
1967 /*page_code*/ page_code,
1969 /*subpage*/ subpage,
1970 /*minimum_cdb_size*/ cdbsize,
1971 /*tag_type*/ CTL_TAG_SIMPLE,
1974 io->io_hdr.nexus.targ_target.id = target;
1975 io->io_hdr.nexus.targ_lun = lun;
1976 io->io_hdr.nexus.initid = id;
1978 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1983 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1984 int pages_len, used_len;
1985 uint32_t returned_len;
1988 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1989 struct scsi_mode_hdr_6 *hdr6;
1992 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1994 returned_len = hdr6->datalen + 1;
1995 bdlen = hdr6->block_descr_len;
1997 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1999 struct scsi_mode_hdr_10 *hdr10;
2002 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
2004 returned_len = scsi_2btoul(hdr10->datalen) + 2;
2005 bdlen = scsi_2btoul(hdr10->block_descr_len);
2007 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
2009 /* just in case they can give us more than we allocated for */
2010 returned_len = min(returned_len, datalen);
2011 pages_len = returned_len - (ndataptr - dataptr);
2013 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
2014 returned_len, pages_len);
2017 fprintf(stdout, "Supported mode pages:\n");
2018 for (used_len = 0; used_len < pages_len;) {
2019 struct scsi_mode_page_header *header;
2021 header = (struct scsi_mode_page_header *)
2022 &ndataptr[used_len];
2023 fprintf(stdout, "%d\n", header->page_code);
2024 used_len += header->page_length + 2;
2027 for (used_len = 0; used_len < pages_len; used_len++) {
2028 fprintf(stdout, "0x%x ", ndataptr[used_len]);
2029 if (((used_len+1) % 16) == 0)
2030 fprintf(stdout, "\n");
2032 fprintf(stdout, "\n");
2035 ctl_io_error_print(io, NULL, stderr);
2038 ctl_scsi_free_io(io);
2040 if (dataptr != NULL)
2047 cctl_read_capacity(int fd, int target, int lun, int iid, int retries,
2048 int argc, char **argv, char *combinedopt)
2052 struct scsi_read_capacity_data *data;
2053 struct scsi_read_capacity_data_long *longdata;
2054 int cdbsize = -1, retval;
2063 io = ctl_scsi_alloc_io(id);
2065 warn("%s: can't allocate memory\n", __func__);
2069 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2072 cdbsize = strtol(optarg, NULL, 0);
2078 if (cdbsize != -1) {
2084 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
2085 "and 16", __func__, cdbsize);
2088 break; /* NOTREACHED */
2093 dataptr = (uint8_t *)malloc(sizeof(*longdata));
2094 if (dataptr == NULL) {
2095 warn("%s: can't allocate %zd bytes\n", __func__,
2100 memset(dataptr, 0, sizeof(*longdata));
2106 ctl_scsi_read_capacity(io,
2107 /*data_ptr*/ dataptr,
2108 /*data_len*/ sizeof(*longdata),
2112 /*tag_type*/ CTL_TAG_SIMPLE,
2116 ctl_scsi_read_capacity_16(io,
2117 /*data_ptr*/ dataptr,
2118 /*data_len*/ sizeof(*longdata),
2122 /*tag_type*/ CTL_TAG_SIMPLE,
2127 io->io_hdr.nexus.initid = id;
2128 io->io_hdr.nexus.targ_target.id = target;
2129 io->io_hdr.nexus.targ_lun = lun;
2131 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2136 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2140 if (cdbsize == 10) {
2142 data = (struct scsi_read_capacity_data *)dataptr;
2144 maxlba = scsi_4btoul(data->addr);
2145 blocksize = scsi_4btoul(data->length);
2147 if (maxlba == 0xffffffff) {
2152 longdata=(struct scsi_read_capacity_data_long *)dataptr;
2154 maxlba = scsi_8btou64(longdata->addr);
2155 blocksize = scsi_4btoul(longdata->length);
2158 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
2159 (uintmax_t)maxlba, blocksize);
2161 ctl_io_error_print(io, NULL, stderr);
2164 ctl_scsi_free_io(io);
2166 if (dataptr != NULL)
2173 cctl_read_write(int fd, int target, int lun, int iid, int retries,
2174 int argc, char **argv, char *combinedopt,
2175 ctladm_cmdfunction command)
2179 int file_fd, do_stdio;
2180 int cdbsize = -1, databytes;
2182 char *filename = NULL;
2183 int datalen = -1, blocksize = -1;
2195 io = ctl_scsi_alloc_io(id);
2197 warn("%s: can't allocate memory\n", __func__);
2201 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2204 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
2207 blocksize = strtoul(optarg, NULL, 0);
2210 cdbsize = strtoul(optarg, NULL, 0);
2213 datalen = strtoul(optarg, NULL, 0);
2216 filename = strdup(optarg);
2219 lba = strtoull(optarg, NULL, 0);
2226 if (filename == NULL) {
2227 warnx("%s: you must supply a filename using -f", __func__);
2232 if (datalen == -1) {
2233 warnx("%s: you must specify the data length with -d", __func__);
2239 warnx("%s: you must specify the LBA with -l", __func__);
2244 if (blocksize == -1) {
2245 warnx("%s: you must specify the blocksize with -b", __func__);
2250 if (cdbsize != -1) {
2258 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
2259 "10, 12 or 16", __func__, cdbsize);
2262 break; /* NOTREACHED */
2267 databytes = datalen * blocksize;
2268 dataptr = (uint8_t *)malloc(databytes);
2270 if (dataptr == NULL) {
2271 warn("%s: can't allocate %d bytes\n", __func__, databytes);
2275 if (strcmp(filename, "-") == 0) {
2276 if (command == CTLADM_CMD_READ)
2277 file_fd = STDOUT_FILENO;
2279 file_fd = STDIN_FILENO;
2282 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
2283 if (file_fd == -1) {
2284 warn("%s: can't open file %s", __func__, filename);
2290 memset(dataptr, 0, databytes);
2292 if (command == CTLADM_CMD_WRITE) {
2295 bytes_read = read(file_fd, dataptr, databytes);
2296 if (bytes_read == -1) {
2297 warn("%s: error reading file %s", __func__, filename);
2301 if (bytes_read != databytes) {
2302 warnx("%s: only read %d bytes from file %s",
2303 __func__, bytes_read, filename);
2308 ctl_scsi_read_write(io,
2309 /*data_ptr*/ dataptr,
2310 /*data_len*/ databytes,
2311 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
2313 /*minimum_cdb_size*/ cdbsize,
2315 /*num_blocks*/ datalen,
2316 /*tag_type*/ CTL_TAG_SIMPLE,
2319 io->io_hdr.nexus.targ_target.id = target;
2320 io->io_hdr.nexus.targ_lun = lun;
2321 io->io_hdr.nexus.initid = id;
2323 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2328 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2329 && (command == CTLADM_CMD_READ)) {
2332 bytes_written = write(file_fd, dataptr, databytes);
2333 if (bytes_written == -1) {
2334 warn("%s: can't write to %s", __func__, filename);
2337 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
2338 ctl_io_error_print(io, NULL, stderr);
2343 ctl_scsi_free_io(io);
2345 if (dataptr != NULL)
2356 cctl_get_luns(int fd, int target, int lun, int iid, int retries, struct
2357 scsi_report_luns_data **lun_data, uint32_t *num_luns)
2368 io = ctl_scsi_alloc_io(id);
2370 warnx("%s: can't allocate memory", __func__);
2375 * lun_data includes space for 1 lun, allocate space for 4 initially.
2376 * If that isn't enough, we'll allocate more.
2380 lun_datalen = sizeof(*lun_data) +
2381 (nluns * sizeof(struct scsi_report_luns_lundata));
2382 *lun_data = malloc(lun_datalen);
2384 if (*lun_data == NULL) {
2385 warnx("%s: can't allocate memory", __func__);
2386 ctl_scsi_free_io(io);
2390 ctl_scsi_report_luns(io,
2391 /*data_ptr*/ (uint8_t *)*lun_data,
2392 /*data_len*/ lun_datalen,
2393 /*select_report*/ RPL_REPORT_ALL,
2394 /*tag_type*/ CTL_TAG_SIMPLE,
2397 io->io_hdr.nexus.initid = id;
2398 io->io_hdr.nexus.targ_target.id = target;
2399 io->io_hdr.nexus.targ_lun = lun;
2401 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2406 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2407 uint32_t returned_len, returned_luns;
2409 returned_len = scsi_4btoul((*lun_data)->length);
2410 returned_luns = returned_len / 8;
2411 if (returned_luns > nluns) {
2412 nluns = returned_luns;
2416 /* These should be the same */
2417 *num_luns = MIN(returned_luns, nluns);
2419 ctl_io_error_print(io, NULL, stderr);
2423 ctl_scsi_free_io(io);
2429 cctl_report_luns(int fd, int target, int lun, int iid, int retries)
2431 struct scsi_report_luns_data *lun_data;
2432 uint32_t num_luns, i;
2437 if ((retval = cctl_get_luns(fd, target, lun, iid, retries, &lun_data,
2441 fprintf(stdout, "%u LUNs returned\n", num_luns);
2442 for (i = 0; i < num_luns; i++) {
2446 * XXX KDM figure out a way to share this code with
2449 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
2450 case RPL_LUNDATA_ATYP_PERIPH:
2451 lun_val = lun_data->luns[i].lundata[1];
2453 case RPL_LUNDATA_ATYP_FLAT:
2454 lun_val = (lun_data->luns[i].lundata[0] &
2455 RPL_LUNDATA_FLAT_LUN_MASK) |
2456 (lun_data->luns[i].lundata[1] <<
2457 RPL_LUNDATA_FLAT_LUN_BITS);
2459 case RPL_LUNDATA_ATYP_LUN:
2460 case RPL_LUNDATA_ATYP_EXTLUN:
2462 fprintf(stdout, "Unsupported LUN format %d\n",
2463 lun_data->luns[i].lundata[0] &
2464 RPL_LUNDATA_ATYP_MASK);
2471 fprintf(stdout, "%d\n", lun_val);
2475 if (lun_data != NULL)
2482 cctl_tur(int fd, int target, int lun, int iid, int retries)
2489 io = ctl_scsi_alloc_io(id);
2491 fprintf(stderr, "can't allocate memory\n");
2496 /* tag_type */ CTL_TAG_SIMPLE,
2499 io->io_hdr.nexus.targ_target.id = target;
2500 io->io_hdr.nexus.targ_lun = lun;
2501 io->io_hdr.nexus.initid = id;
2503 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2504 ctl_scsi_free_io(io);
2508 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2509 fprintf(stdout, "Unit is ready\n");
2511 ctl_io_error_print(io, NULL, stderr);
2517 cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
2518 char *path_str, int path_len,
2519 struct scsi_inquiry_data *inq_data)
2529 io = ctl_scsi_alloc_io(id);
2531 warnx("cctl_inquiry: can't allocate memory\n");
2535 ctl_scsi_inquiry(/*io*/ io,
2536 /*data_ptr*/ (uint8_t *)inq_data,
2537 /*data_len*/ sizeof(*inq_data),
2540 /*tag_type*/ CTL_TAG_SIMPLE,
2543 io->io_hdr.nexus.targ_target.id = target;
2544 io->io_hdr.nexus.targ_lun = lun;
2545 io->io_hdr.nexus.initid = id;
2547 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2552 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
2554 ctl_io_error_print(io, NULL, stderr);
2555 } else if (path_str != NULL)
2556 ctl_scsi_path_string(io, path_str, path_len);
2559 ctl_scsi_free_io(io);
2565 cctl_inquiry(int fd, int target, int lun, int iid, int retries)
2567 struct scsi_inquiry_data *inq_data;
2573 inq_data = malloc(sizeof(*inq_data));
2574 if (inq_data == NULL) {
2575 warnx("%s: can't allocate inquiry data", __func__);
2580 if ((retval = cctl_get_inquiry(fd, target, lun, iid, retries, scsi_path,
2581 sizeof(scsi_path), inq_data)) != 0)
2584 printf("%s", scsi_path);
2585 scsi_print_inquiry(inq_data);
2588 if (inq_data != NULL)
2595 cctl_req_sense(int fd, int target, int lun, int iid, int retries)
2598 struct scsi_sense_data *sense_data;
2606 io = ctl_scsi_alloc_io(id);
2608 warnx("cctl_req_sense: can't allocate memory\n");
2611 sense_data = malloc(sizeof(*sense_data));
2612 memset(sense_data, 0, sizeof(*sense_data));
2614 ctl_scsi_request_sense(/*io*/ io,
2615 /*data_ptr*/ (uint8_t *)sense_data,
2616 /*data_len*/ sizeof(*sense_data),
2618 /*tag_type*/ CTL_TAG_SIMPLE,
2621 io->io_hdr.nexus.targ_target.id = target;
2622 io->io_hdr.nexus.targ_lun = lun;
2623 io->io_hdr.nexus.initid = id;
2625 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2630 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2631 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2632 io->scsiio.sense_len = sizeof(*sense_data);
2633 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2635 ctl_io_error_print(io, NULL, stderr);
2639 ctl_scsi_free_io(io);
2646 cctl_report_target_port_group(int fd, int target, int lun, int initiator)
2658 io = ctl_scsi_alloc_io(id);
2660 warn("%s: can't allocate memory", __func__);
2665 dataptr = (uint8_t *)malloc(datalen);
2666 if (dataptr == NULL) {
2667 warn("%s: can't allocate %d bytes", __func__, datalen);
2672 memset(dataptr, 0, datalen);
2674 ctl_scsi_maintenance_in(/*io*/ io,
2675 /*data_ptr*/ dataptr,
2676 /*data_len*/ datalen,
2677 /*action*/ SA_RPRT_TRGT_GRP,
2678 /*tag_type*/ CTL_TAG_SIMPLE,
2681 io->io_hdr.nexus.targ_target.id = target;
2682 io->io_hdr.nexus.targ_lun = lun;
2683 io->io_hdr.nexus.initid = id;
2685 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2690 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2691 int returned_len, used_len;
2693 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2695 for (used_len = 0; used_len < returned_len; used_len++) {
2696 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2697 if (((used_len+1) % 8) == 0)
2698 fprintf(stdout, "\n");
2700 fprintf(stdout, "\n");
2702 ctl_io_error_print(io, NULL, stderr);
2705 ctl_scsi_free_io(io);
2707 if (dataptr != NULL)
2714 cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator)
2726 io = ctl_scsi_alloc_io(id);
2728 warn("%s: can't allocate memory", __func__);
2733 dataptr = (uint8_t *)malloc(datalen);
2734 if (dataptr == NULL) {
2735 warn("%s: can't allocate %d bytes", __func__, datalen);
2740 memset(dataptr, 0, datalen);
2742 ctl_scsi_inquiry(/*io*/ io,
2743 /*data_ptr*/ dataptr,
2744 /*data_len*/ datalen,
2746 /*page_code*/ SVPD_DEVICE_ID,
2747 /*tag_type*/ CTL_TAG_SIMPLE,
2750 io->io_hdr.nexus.targ_target.id = target;
2751 io->io_hdr.nexus.targ_lun = lun;
2752 io->io_hdr.nexus.initid = id;
2754 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2759 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2760 int returned_len, used_len;
2762 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2764 for (used_len = 0; used_len < returned_len; used_len++) {
2765 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2766 if (((used_len+1) % 8) == 0)
2767 fprintf(stdout, "\n");
2769 fprintf(stdout, "\n");
2771 ctl_io_error_print(io, NULL, stderr);
2774 ctl_scsi_free_io(io);
2776 if (dataptr != NULL)
2783 cctl_persistent_reserve_in(int fd, int target, int lun, int initiator,
2784 int argc, char **argv, char *combinedopt,
2799 io = ctl_scsi_alloc_io(id);
2801 warn("%s: can't allocate memory", __func__);
2805 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2808 action = strtol(optarg, NULL, 0);
2815 if (action < 0 || action > 2) {
2816 warn("action must be specified and in the range: 0-2");
2823 dataptr = (uint8_t *)malloc(datalen);
2824 if (dataptr == NULL) {
2825 warn("%s: can't allocate %d bytes", __func__, datalen);
2830 memset(dataptr, 0, datalen);
2832 ctl_scsi_persistent_res_in(io,
2833 /*data_ptr*/ dataptr,
2834 /*data_len*/ datalen,
2836 /*tag_type*/ CTL_TAG_SIMPLE,
2839 io->io_hdr.nexus.targ_target.id = target;
2840 io->io_hdr.nexus.targ_lun = lun;
2841 io->io_hdr.nexus.initid = id;
2843 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2848 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2849 int returned_len, used_len;
2855 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2856 returned_len = min(returned_len, 256);
2859 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2865 warnx("%s: invalid action %d", __func__, action);
2867 break; /* NOTREACHED */
2870 for (used_len = 0; used_len < returned_len; used_len++) {
2871 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2872 if (((used_len+1) % 8) == 0)
2873 fprintf(stdout, "\n");
2875 fprintf(stdout, "\n");
2877 ctl_io_error_print(io, NULL, stderr);
2880 ctl_scsi_free_io(io);
2882 if (dataptr != NULL)
2889 cctl_persistent_reserve_out(int fd, int target, int lun, int initiator,
2890 int argc, char **argv, char *combinedopt,
2896 uint64_t key = 0, sa_key = 0;
2897 int action = -1, restype = -1;
2906 io = ctl_scsi_alloc_io(id);
2908 warn("%s: can't allocate memory", __func__);
2912 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2915 action = strtol(optarg, NULL, 0);
2918 key = strtoull(optarg, NULL, 0);
2921 restype = strtol(optarg, NULL, 0);
2924 sa_key = strtoull(optarg, NULL, 0);
2930 if (action < 0 || action > 5) {
2931 warn("action must be specified and in the range: 0-5");
2936 if (restype < 0 || restype > 5) {
2937 if (action != 0 && action != 5 && action != 3) {
2938 warn("'restype' must specified and in the range: 0-5");
2945 dataptr = (uint8_t *)malloc(datalen);
2946 if (dataptr == NULL) {
2947 warn("%s: can't allocate %d bytes", __func__, datalen);
2952 memset(dataptr, 0, datalen);
2954 ctl_scsi_persistent_res_out(io,
2955 /*data_ptr*/ dataptr,
2956 /*data_len*/ datalen,
2961 /*tag_type*/ CTL_TAG_SIMPLE,
2964 io->io_hdr.nexus.targ_target.id = target;
2965 io->io_hdr.nexus.targ_lun = lun;
2966 io->io_hdr.nexus.initid = id;
2968 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2972 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2974 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2975 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2976 "successfully\n", scsi_path);
2978 ctl_io_error_print(io, NULL, stderr);
2981 ctl_scsi_free_io(io);
2983 if (dataptr != NULL)
2989 struct cctl_req_option {
2994 STAILQ_ENTRY(cctl_req_option) links;
2998 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
3000 struct ctl_lun_req req;
3001 int device_type = -1;
3002 uint64_t lun_size = 0;
3003 uint32_t blocksize = 0, req_lun_id = 0;
3004 char *serial_num = NULL;
3005 char *device_id = NULL;
3006 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
3007 char *backend_name = NULL;
3008 STAILQ_HEAD(, cctl_req_option) option_list;
3009 int num_options = 0;
3012 STAILQ_INIT(&option_list);
3014 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3017 backend_name = strdup(optarg);
3020 blocksize = strtoul(optarg, NULL, 0);
3024 device_id = strdup(optarg);
3027 req_lun_id = strtoul(optarg, NULL, 0);
3031 struct cctl_req_option *option;
3035 tmpstr = strdup(optarg);
3036 name = strsep(&tmpstr, "=");
3038 warnx("%s: option -o takes \"name=value\""
3039 "argument", __func__);
3043 value = strsep(&tmpstr, "=");
3044 if (value == NULL) {
3045 warnx("%s: option -o takes \"name=value\""
3046 "argument", __func__);
3050 option = malloc(sizeof(*option));
3051 if (option == NULL) {
3052 warn("%s: error allocating %zd bytes",
3053 __func__, sizeof(*option));
3057 option->name = strdup(name);
3058 option->namelen = strlen(name) + 1;
3059 option->value = strdup(value);
3060 option->vallen = strlen(value) + 1;
3063 STAILQ_INSERT_TAIL(&option_list, option, links);
3068 if (strcasecmp(optarg, "auto") != 0) {
3069 retval = expand_number(optarg, &lun_size);
3071 warn("%s: invalid -s argument",
3080 serial_num = strdup(optarg);
3083 device_type = strtoul(optarg, NULL, 0);
3090 if (backend_name == NULL) {
3091 warnx("%s: backend name (-b) must be specified", __func__);
3096 bzero(&req, sizeof(req));
3098 strlcpy(req.backend, backend_name, sizeof(req.backend));
3099 req.reqtype = CTL_LUNREQ_CREATE;
3101 if (blocksize_set != 0)
3102 req.reqdata.create.blocksize_bytes = blocksize;
3104 if (lun_size_set != 0)
3105 req.reqdata.create.lun_size_bytes = lun_size;
3107 if (lun_id_set != 0) {
3108 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
3109 req.reqdata.create.req_lun_id = req_lun_id;
3112 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
3114 if (device_type != -1)
3115 req.reqdata.create.device_type = device_type;
3117 req.reqdata.create.device_type = T_DIRECT;
3119 if (serial_num != NULL) {
3120 strlcpy(req.reqdata.create.serial_num, serial_num,
3121 sizeof(req.reqdata.create.serial_num));
3122 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
3125 if (device_id != NULL) {
3126 strlcpy(req.reqdata.create.device_id, device_id,
3127 sizeof(req.reqdata.create.device_id));
3128 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
3131 req.num_be_args = num_options;
3132 if (num_options > 0) {
3133 struct cctl_req_option *option, *next_option;
3136 req.be_args = malloc(num_options * sizeof(*req.be_args));
3137 if (req.be_args == NULL) {
3138 warn("%s: error allocating %zd bytes", __func__,
3139 num_options * sizeof(*req.be_args));
3144 for (i = 0, option = STAILQ_FIRST(&option_list);
3145 i < num_options; i++, option = next_option) {
3146 next_option = STAILQ_NEXT(option, links);
3148 req.be_args[i].namelen = option->namelen;
3149 req.be_args[i].name = strdup(option->name);
3150 req.be_args[i].vallen = option->vallen;
3151 req.be_args[i].value = strdup(option->value);
3153 * XXX KDM do we want a way to specify a writeable
3154 * flag of some sort? Do we want a way to specify
3157 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3159 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3162 free(option->value);
3167 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3168 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3173 if (req.status == CTL_LUN_ERROR) {
3174 warnx("%s: error returned from LUN creation request:\n%s",
3175 __func__, req.error_str);
3178 } else if (req.status != CTL_LUN_OK) {
3179 warnx("%s: unknown LUN creation request status %d",
3180 __func__, req.status);
3185 fprintf(stdout, "LUN created successfully\n");
3186 fprintf(stdout, "backend: %s\n", req.backend);
3187 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
3188 fprintf(stdout, "LUN size: %ju bytes\n",
3189 (uintmax_t)req.reqdata.create.lun_size_bytes);
3190 fprintf(stdout, "blocksize %u bytes\n",
3191 req.reqdata.create.blocksize_bytes);
3192 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
3193 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
3194 fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
3201 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
3203 struct ctl_lun_req req;
3204 uint32_t lun_id = 0;
3206 char *backend_name = NULL;
3207 STAILQ_HEAD(, cctl_req_option) option_list;
3208 int num_options = 0;
3211 STAILQ_INIT(&option_list);
3213 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3216 backend_name = strdup(optarg);
3219 lun_id = strtoul(optarg, NULL, 0);
3223 struct cctl_req_option *option;
3227 tmpstr = strdup(optarg);
3228 name = strsep(&tmpstr, "=");
3230 warnx("%s: option -o takes \"name=value\""
3231 "argument", __func__);
3235 value = strsep(&tmpstr, "=");
3236 if (value == NULL) {
3237 warnx("%s: option -o takes \"name=value\""
3238 "argument", __func__);
3242 option = malloc(sizeof(*option));
3243 if (option == NULL) {
3244 warn("%s: error allocating %zd bytes",
3245 __func__, sizeof(*option));
3249 option->name = strdup(name);
3250 option->namelen = strlen(name) + 1;
3251 option->value = strdup(value);
3252 option->vallen = strlen(value) + 1;
3255 STAILQ_INSERT_TAIL(&option_list, option, links);
3264 if (backend_name == NULL)
3265 errx(1, "%s: backend name (-b) must be specified", __func__);
3267 if (lun_id_set == 0)
3268 errx(1, "%s: LUN id (-l) must be specified", __func__);
3270 bzero(&req, sizeof(req));
3272 strlcpy(req.backend, backend_name, sizeof(req.backend));
3273 req.reqtype = CTL_LUNREQ_RM;
3275 req.reqdata.rm.lun_id = lun_id;
3277 req.num_be_args = num_options;
3278 if (num_options > 0) {
3279 struct cctl_req_option *option, *next_option;
3282 req.be_args = malloc(num_options * sizeof(*req.be_args));
3283 if (req.be_args == NULL) {
3284 warn("%s: error allocating %zd bytes", __func__,
3285 num_options * sizeof(*req.be_args));
3290 for (i = 0, option = STAILQ_FIRST(&option_list);
3291 i < num_options; i++, option = next_option) {
3292 next_option = STAILQ_NEXT(option, links);
3294 req.be_args[i].namelen = option->namelen;
3295 req.be_args[i].name = strdup(option->name);
3296 req.be_args[i].vallen = option->vallen;
3297 req.be_args[i].value = strdup(option->value);
3299 * XXX KDM do we want a way to specify a writeable
3300 * flag of some sort? Do we want a way to specify
3303 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3305 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3308 free(option->value);
3313 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3314 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3319 if (req.status == CTL_LUN_ERROR) {
3320 warnx("%s: error returned from LUN removal request:\n%s",
3321 __func__, req.error_str);
3324 } else if (req.status != CTL_LUN_OK) {
3325 warnx("%s: unknown LUN removal request status %d",
3326 __func__, req.status);
3331 printf("LUN %d deleted successfully\n", lun_id);
3338 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
3340 struct ctl_lun_req req;
3341 uint64_t lun_size = 0;
3342 uint32_t lun_id = 0;
3343 int lun_id_set = 0, lun_size_set = 0;
3344 char *backend_name = NULL;
3347 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3350 backend_name = strdup(optarg);
3353 lun_id = strtoul(optarg, NULL, 0);
3357 if (strcasecmp(optarg, "auto") != 0) {
3358 retval = expand_number(optarg, &lun_size);
3360 warn("%s: invalid -s argument",
3373 if (backend_name == NULL)
3374 errx(1, "%s: backend name (-b) must be specified", __func__);
3376 if (lun_id_set == 0)
3377 errx(1, "%s: LUN id (-l) must be specified", __func__);
3379 if (lun_size_set == 0)
3380 errx(1, "%s: size (-s) must be specified", __func__);
3382 bzero(&req, sizeof(req));
3384 strlcpy(req.backend, backend_name, sizeof(req.backend));
3385 req.reqtype = CTL_LUNREQ_MODIFY;
3387 req.reqdata.modify.lun_id = lun_id;
3388 req.reqdata.modify.lun_size_bytes = lun_size;
3390 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3391 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3396 if (req.status == CTL_LUN_ERROR) {
3397 warnx("%s: error returned from LUN modification request:\n%s",
3398 __func__, req.error_str);
3401 } else if (req.status != CTL_LUN_OK) {
3402 warnx("%s: unknown LUN modification request status %d",
3403 __func__, req.status);
3408 printf("LUN %d modified successfully\n", lun_id);
3414 struct cctl_islist_conn {
3417 char *initiator_addr;
3418 char *initiator_alias;
3421 char *header_digest;
3423 char *max_data_segment_length;;
3426 STAILQ_ENTRY(cctl_islist_conn) links;
3429 struct cctl_islist_data {
3431 STAILQ_HEAD(,cctl_islist_conn) conn_list;
3432 struct cctl_islist_conn *cur_conn;
3434 struct sbuf *cur_sb[32];
3438 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
3441 struct cctl_islist_data *islist;
3442 struct cctl_islist_conn *cur_conn;
3444 islist = (struct cctl_islist_data *)user_data;
3445 cur_conn = islist->cur_conn;
3447 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
3448 sizeof(islist->cur_sb[0])))
3449 errx(1, "%s: too many nesting levels, %zd max", __func__,
3450 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
3452 islist->cur_sb[islist->level] = sbuf_new_auto();
3453 if (islist->cur_sb[islist->level] == NULL)
3454 err(1, "%s: Unable to allocate sbuf", __func__);
3456 if (strcmp(name, "connection") == 0) {
3457 if (cur_conn != NULL)
3458 errx(1, "%s: improper connection element nesting",
3461 cur_conn = calloc(1, sizeof(*cur_conn));
3462 if (cur_conn == NULL)
3463 err(1, "%s: cannot allocate %zd bytes", __func__,
3466 islist->num_conns++;
3467 islist->cur_conn = cur_conn;
3469 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
3471 for (i = 0; attr[i] != NULL; i += 2) {
3472 if (strcmp(attr[i], "id") == 0) {
3473 cur_conn->connection_id =
3474 strtoull(attr[i+1], NULL, 0);
3477 "%s: invalid connection attribute %s = %s",
3478 __func__, attr[i], attr[i+1]);
3485 cctl_islist_end_element(void *user_data, const char *name)
3487 struct cctl_islist_data *islist;
3488 struct cctl_islist_conn *cur_conn;
3491 islist = (struct cctl_islist_data *)user_data;
3492 cur_conn = islist->cur_conn;
3494 if ((cur_conn == NULL)
3495 && (strcmp(name, "ctlislist") != 0))
3496 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
3498 if (islist->cur_sb[islist->level] == NULL)
3499 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3500 islist->level, name);
3502 sbuf_finish(islist->cur_sb[islist->level]);
3503 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
3505 err(1, "%s can't allocate %zd bytes for string", __func__,
3506 sbuf_len(islist->cur_sb[islist->level]));
3508 sbuf_delete(islist->cur_sb[islist->level]);
3509 islist->cur_sb[islist->level] = NULL;
3512 if (strcmp(name, "initiator") == 0) {
3513 cur_conn->initiator = str;
3515 } else if (strcmp(name, "initiator_addr") == 0) {
3516 cur_conn->initiator_addr = str;
3518 } else if (strcmp(name, "initiator_alias") == 0) {
3519 cur_conn->initiator_alias = str;
3521 } else if (strcmp(name, "target") == 0) {
3522 cur_conn->target = str;
3524 } else if (strcmp(name, "target_alias") == 0) {
3525 cur_conn->target_alias = str;
3527 } else if (strcmp(name, "header_digest") == 0) {
3528 cur_conn->header_digest = str;
3530 } else if (strcmp(name, "data_digest") == 0) {
3531 cur_conn->data_digest = str;
3533 } else if (strcmp(name, "max_data_segment_length") == 0) {
3534 cur_conn->max_data_segment_length = str;
3536 } else if (strcmp(name, "immediate_data") == 0) {
3537 cur_conn->immediate_data = atoi(str);
3538 } else if (strcmp(name, "iser") == 0) {
3539 cur_conn->iser = atoi(str);
3540 } else if (strcmp(name, "connection") == 0) {
3541 islist->cur_conn = NULL;
3542 } else if (strcmp(name, "ctlislist") == 0) {
3544 errx(1, "unknown element %s", name);
3550 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
3552 struct cctl_islist_data *islist;
3554 islist = (struct cctl_islist_data *)user_data;
3556 sbuf_bcat(islist->cur_sb[islist->level], str, len);
3560 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
3562 struct ctl_iscsi req;
3563 struct cctl_islist_data islist;
3564 struct cctl_islist_conn *conn;
3569 int c, retval, verbose = 0;
3574 bzero(&islist, sizeof(islist));
3575 STAILQ_INIT(&islist.conn_list);
3577 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3591 conn_str = malloc(conn_len);
3593 bzero(&req, sizeof(req));
3594 req.type = CTL_ISCSI_LIST;
3595 req.data.list.alloc_len = conn_len;
3596 req.data.list.conn_xml = conn_str;
3598 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3599 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3604 if (req.status == CTL_ISCSI_ERROR) {
3605 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
3606 __func__, req.error_str);
3607 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
3608 conn_len = conn_len << 1;
3612 if (dump_xml != 0) {
3613 printf("%s", conn_str);
3617 parser = XML_ParserCreate(NULL);
3618 if (parser == NULL) {
3619 warn("%s: Unable to create XML parser", __func__);
3624 XML_SetUserData(parser, &islist);
3625 XML_SetElementHandler(parser, cctl_islist_start_element,
3626 cctl_islist_end_element);
3627 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3629 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3630 XML_ParserFree(parser);
3637 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3638 printf("Session ID: %d\n", conn->connection_id);
3639 printf("Initiator name: %s\n", conn->initiator);
3640 printf("Initiator portal: %s\n", conn->initiator_addr);
3641 printf("Initiator alias: %s\n", conn->initiator_alias);
3642 printf("Target name: %s\n", conn->target);
3643 printf("Target alias: %s\n", conn->target_alias);
3644 printf("Header digest: %s\n", conn->header_digest);
3645 printf("Data digest: %s\n", conn->data_digest);
3646 printf("DataSegmentLen: %s\n", conn->max_data_segment_length);
3647 printf("ImmediateData: %s\n", conn->immediate_data ? "Yes" : "No");
3648 printf("iSER (RDMA): %s\n", conn->iser ? "Yes" : "No");
3652 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3654 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3655 printf("%4u %-16s %-36s %-36s\n",
3656 conn->connection_id, conn->initiator_addr, conn->initiator,
3667 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3669 struct ctl_iscsi req;
3671 int all = 0, connection_id = -1, nargs = 0;
3672 char *initiator_name = NULL, *initiator_addr = NULL;
3674 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3681 connection_id = strtoul(optarg, NULL, 0);
3685 initiator_name = strdup(optarg);
3686 if (initiator_name == NULL)
3687 err(1, "%s: strdup", __func__);
3691 initiator_addr = strdup(optarg);
3692 if (initiator_addr == NULL)
3693 err(1, "%s: strdup", __func__);
3702 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3705 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3708 bzero(&req, sizeof(req));
3709 req.type = CTL_ISCSI_LOGOUT;
3710 req.data.logout.connection_id = connection_id;
3711 if (initiator_addr != NULL)
3712 strlcpy(req.data.logout.initiator_addr,
3713 initiator_addr, sizeof(req.data.logout.initiator_addr));
3714 if (initiator_name != NULL)
3715 strlcpy(req.data.logout.initiator_name,
3716 initiator_name, sizeof(req.data.logout.initiator_name));
3718 req.data.logout.all = 1;
3720 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3721 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3726 if (req.status != CTL_ISCSI_OK) {
3727 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3728 __func__, req.error_str);
3733 printf("iSCSI logout requests submitted\n");
3740 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3742 struct ctl_iscsi req;
3744 int all = 0, connection_id = -1, nargs = 0;
3745 char *initiator_name = NULL, *initiator_addr = NULL;
3747 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3754 connection_id = strtoul(optarg, NULL, 0);
3758 initiator_name = strdup(optarg);
3759 if (initiator_name == NULL)
3760 err(1, "%s: strdup", __func__);
3764 initiator_addr = strdup(optarg);
3765 if (initiator_addr == NULL)
3766 err(1, "%s: strdup", __func__);
3775 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3778 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3781 bzero(&req, sizeof(req));
3782 req.type = CTL_ISCSI_TERMINATE;
3783 req.data.terminate.connection_id = connection_id;
3784 if (initiator_addr != NULL)
3785 strlcpy(req.data.terminate.initiator_addr,
3786 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3787 if (initiator_name != NULL)
3788 strlcpy(req.data.terminate.initiator_name,
3789 initiator_name, sizeof(req.data.terminate.initiator_name));
3791 req.data.terminate.all = 1;
3793 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3794 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3799 if (req.status != CTL_ISCSI_OK) {
3800 warnx("%s: error returned from CTL iSCSI connection "
3801 "termination request:\n%s", __func__, req.error_str);
3806 printf("iSCSI connections terminated\n");
3813 * Name/value pair used for per-LUN attributes.
3815 struct cctl_lun_nv {
3818 STAILQ_ENTRY(cctl_lun_nv) links;
3822 * Backend LUN information.
3827 uint64_t size_blocks;
3829 char *serial_number;
3831 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3832 STAILQ_ENTRY(cctl_lun) links;
3835 struct cctl_devlist_data {
3837 STAILQ_HEAD(,cctl_lun) lun_list;
3838 struct cctl_lun *cur_lun;
3840 struct sbuf *cur_sb[32];
3844 cctl_start_element(void *user_data, const char *name, const char **attr)
3847 struct cctl_devlist_data *devlist;
3848 struct cctl_lun *cur_lun;
3850 devlist = (struct cctl_devlist_data *)user_data;
3851 cur_lun = devlist->cur_lun;
3853 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3854 sizeof(devlist->cur_sb[0])))
3855 errx(1, "%s: too many nesting levels, %zd max", __func__,
3856 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3858 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3859 if (devlist->cur_sb[devlist->level] == NULL)
3860 err(1, "%s: Unable to allocate sbuf", __func__);
3862 if (strcmp(name, "lun") == 0) {
3863 if (cur_lun != NULL)
3864 errx(1, "%s: improper lun element nesting", __func__);
3866 cur_lun = calloc(1, sizeof(*cur_lun));
3867 if (cur_lun == NULL)
3868 err(1, "%s: cannot allocate %zd bytes", __func__,
3871 devlist->num_luns++;
3872 devlist->cur_lun = cur_lun;
3874 STAILQ_INIT(&cur_lun->attr_list);
3875 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3877 for (i = 0; attr[i] != NULL; i += 2) {
3878 if (strcmp(attr[i], "id") == 0) {
3879 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3881 errx(1, "%s: invalid LUN attribute %s = %s",
3882 __func__, attr[i], attr[i+1]);
3889 cctl_end_element(void *user_data, const char *name)
3891 struct cctl_devlist_data *devlist;
3892 struct cctl_lun *cur_lun;
3895 devlist = (struct cctl_devlist_data *)user_data;
3896 cur_lun = devlist->cur_lun;
3898 if ((cur_lun == NULL)
3899 && (strcmp(name, "ctllunlist") != 0))
3900 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3902 if (devlist->cur_sb[devlist->level] == NULL)
3903 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3904 devlist->level, name);
3906 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3907 err(1, "%s: sbuf_finish", __func__);
3908 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3910 err(1, "%s can't allocate %zd bytes for string", __func__,
3911 sbuf_len(devlist->cur_sb[devlist->level]));
3913 if (strlen(str) == 0) {
3918 sbuf_delete(devlist->cur_sb[devlist->level]);
3919 devlist->cur_sb[devlist->level] = NULL;
3922 if (strcmp(name, "backend_type") == 0) {
3923 cur_lun->backend_type = str;
3925 } else if (strcmp(name, "size") == 0) {
3926 cur_lun->size_blocks = strtoull(str, NULL, 0);
3927 } else if (strcmp(name, "blocksize") == 0) {
3928 cur_lun->blocksize = strtoul(str, NULL, 0);
3929 } else if (strcmp(name, "serial_number") == 0) {
3930 cur_lun->serial_number = str;
3932 } else if (strcmp(name, "device_id") == 0) {
3933 cur_lun->device_id = str;
3935 } else if (strcmp(name, "lun") == 0) {
3936 devlist->cur_lun = NULL;
3937 } else if (strcmp(name, "ctllunlist") == 0) {
3940 struct cctl_lun_nv *nv;
3942 nv = calloc(1, sizeof(*nv));
3944 err(1, "%s: can't allocate %zd bytes for nv pair",
3945 __func__, sizeof(*nv));
3947 nv->name = strdup(name);
3948 if (nv->name == NULL)
3949 err(1, "%s: can't allocated %zd bytes for string",
3950 __func__, strlen(name));
3954 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3961 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3963 struct cctl_devlist_data *devlist;
3965 devlist = (struct cctl_devlist_data *)user_data;
3967 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3971 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3973 struct ctl_lun_list list;
3974 struct cctl_devlist_data devlist;
3975 struct cctl_lun *lun;
3981 char *backend = NULL;
3987 bzero(&devlist, sizeof(devlist));
3988 STAILQ_INIT(&devlist.lun_list);
3990 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3993 backend = strdup(optarg);
4007 lun_str = malloc(lun_len);
4009 bzero(&list, sizeof(list));
4010 list.alloc_len = lun_len;
4011 list.status = CTL_LUN_LIST_NONE;
4012 list.lun_xml = lun_str;
4014 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
4015 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
4020 if (list.status == CTL_LUN_LIST_ERROR) {
4021 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
4022 __func__, list.error_str);
4023 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
4024 lun_len = lun_len << 1;
4028 if (dump_xml != 0) {
4029 printf("%s", lun_str);
4033 parser = XML_ParserCreate(NULL);
4034 if (parser == NULL) {
4035 warn("%s: Unable to create XML parser", __func__);
4040 XML_SetUserData(parser, &devlist);
4041 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
4042 XML_SetCharacterDataHandler(parser, cctl_char_handler);
4044 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
4045 XML_ParserFree(parser);
4051 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
4052 "Serial Number", "Device ID");
4053 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
4054 struct cctl_lun_nv *nv;
4056 if ((backend != NULL)
4057 && (strcmp(lun->backend_type, backend) != 0))
4060 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
4061 (uintmax_t)lun->lun_id,
4062 lun->backend_type, (uintmax_t)lun->size_blocks,
4063 lun->blocksize, lun->serial_number, lun->device_id);
4068 STAILQ_FOREACH(nv, &lun->attr_list, links) {
4069 printf(" %s=%s\n", nv->name, nv->value);
4084 char *frontend_type;
4088 STAILQ_HEAD(,cctl_lun_nv) attr_list;
4089 STAILQ_ENTRY(cctl_port) links;
4092 struct cctl_portlist_data {
4094 STAILQ_HEAD(,cctl_port) port_list;
4095 struct cctl_port *cur_port;
4097 struct sbuf *cur_sb[32];
4101 cctl_start_pelement(void *user_data, const char *name, const char **attr)
4104 struct cctl_portlist_data *portlist;
4105 struct cctl_port *cur_port;
4107 portlist = (struct cctl_portlist_data *)user_data;
4108 cur_port = portlist->cur_port;
4110 if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
4111 sizeof(portlist->cur_sb[0])))
4112 errx(1, "%s: too many nesting levels, %zd max", __func__,
4113 sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
4115 portlist->cur_sb[portlist->level] = sbuf_new_auto();
4116 if (portlist->cur_sb[portlist->level] == NULL)
4117 err(1, "%s: Unable to allocate sbuf", __func__);
4119 if (strcmp(name, "targ_port") == 0) {
4120 if (cur_port != NULL)
4121 errx(1, "%s: improper port element nesting", __func__);
4123 cur_port = calloc(1, sizeof(*cur_port));
4124 if (cur_port == NULL)
4125 err(1, "%s: cannot allocate %zd bytes", __func__,
4128 portlist->num_ports++;
4129 portlist->cur_port = cur_port;
4131 STAILQ_INIT(&cur_port->attr_list);
4132 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
4134 for (i = 0; attr[i] != NULL; i += 2) {
4135 if (strcmp(attr[i], "id") == 0) {
4136 cur_port->port_id = strtoull(attr[i+1], NULL, 0);
4138 errx(1, "%s: invalid LUN attribute %s = %s",
4139 __func__, attr[i], attr[i+1]);
4146 cctl_end_pelement(void *user_data, const char *name)
4148 struct cctl_portlist_data *portlist;
4149 struct cctl_port *cur_port;
4152 portlist = (struct cctl_portlist_data *)user_data;
4153 cur_port = portlist->cur_port;
4155 if ((cur_port == NULL)
4156 && (strcmp(name, "ctlportlist") != 0))
4157 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
4159 if (portlist->cur_sb[portlist->level] == NULL)
4160 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
4161 portlist->level, name);
4163 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
4164 err(1, "%s: sbuf_finish", __func__);
4165 str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
4167 err(1, "%s can't allocate %zd bytes for string", __func__,
4168 sbuf_len(portlist->cur_sb[portlist->level]));
4170 if (strlen(str) == 0) {
4175 sbuf_delete(portlist->cur_sb[portlist->level]);
4176 portlist->cur_sb[portlist->level] = NULL;
4179 if (strcmp(name, "frontend_type") == 0) {
4180 cur_port->frontend_type = str;
4182 } else if (strcmp(name, "port_name") == 0) {
4183 cur_port->name = str;
4185 } else if (strcmp(name, "online") == 0) {
4186 cur_port->online = str;
4188 } else if (strcmp(name, "physical_port") == 0) {
4189 cur_port->pp = strtoull(str, NULL, 0);
4190 } else if (strcmp(name, "virtual_port") == 0) {
4191 cur_port->pp = strtoull(str, NULL, 0);
4192 } else if (strcmp(name, "wwnn") == 0) {
4193 cur_port->wwnn = str;
4195 } else if (strcmp(name, "wwpn") == 0) {
4196 cur_port->wwpn = str;
4198 } else if (strcmp(name, "targ_port") == 0) {
4199 portlist->cur_port = NULL;
4200 } else if (strcmp(name, "ctlportlist") == 0) {
4203 struct cctl_lun_nv *nv;
4205 nv = calloc(1, sizeof(*nv));
4207 err(1, "%s: can't allocate %zd bytes for nv pair",
4208 __func__, sizeof(*nv));
4210 nv->name = strdup(name);
4211 if (nv->name == NULL)
4212 err(1, "%s: can't allocated %zd bytes for string",
4213 __func__, strlen(name));
4217 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
4224 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
4226 struct cctl_portlist_data *portlist;
4228 portlist = (struct cctl_portlist_data *)user_data;
4230 sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
4234 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
4236 struct ctl_lun_list list;
4237 struct cctl_portlist_data portlist;
4238 struct cctl_port *port;
4244 char *frontend = NULL;
4250 bzero(&portlist, sizeof(portlist));
4251 STAILQ_INIT(&portlist.port_list);
4253 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4256 frontend = strdup(optarg);
4270 port_str = malloc(port_len);
4272 bzero(&list, sizeof(list));
4273 list.alloc_len = port_len;
4274 list.status = CTL_LUN_LIST_NONE;
4275 list.lun_xml = port_str;
4277 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
4278 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
4283 if (list.status == CTL_LUN_LIST_ERROR) {
4284 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
4285 __func__, list.error_str);
4286 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
4287 port_len = port_len << 1;
4291 if (dump_xml != 0) {
4292 printf("%s", port_str);
4296 parser = XML_ParserCreate(NULL);
4297 if (parser == NULL) {
4298 warn("%s: Unable to create XML parser", __func__);
4303 XML_SetUserData(parser, &portlist);
4304 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
4305 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
4307 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
4308 XML_ParserFree(parser);
4314 printf("Port Online Frontend %-12s pp vp %-18s %-18s\n",
4315 "Name", "WWNN", "WWPN");
4316 STAILQ_FOREACH(port, &portlist.port_list, links) {
4317 struct cctl_lun_nv *nv;
4319 if ((frontend != NULL)
4320 && (strcmp(port->frontend_type, frontend) != 0))
4323 printf("%-4ju %-6s %-8s %-12s %-2d %-2d %-18s %-18s\n",
4324 (uintmax_t)port->port_id, port->online,
4325 port->frontend_type, port->name, port->pp, port->vp,
4326 port->wwnn, port->wwpn);
4331 STAILQ_FOREACH(nv, &port->attr_list, links) {
4332 printf(" %s=%s\n", nv->name, nv->value);
4344 fprintf(error ? stderr : stdout,
4346 "Primary commands:\n"
4347 " ctladm tur [dev_id][general options]\n"
4348 " ctladm inquiry [dev_id][general options]\n"
4349 " ctladm devid [dev_id][general options]\n"
4350 " ctladm reqsense [dev_id][general options]\n"
4351 " ctladm reportluns [dev_id][general options]\n"
4352 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
4353 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4354 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
4355 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4356 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
4357 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
4358 " [-d] [-S subpage] [-c cdbsize]\n"
4359 " ctladm prin [dev_id][general options] <-a action>\n"
4360 " ctladm prout [dev_id][general options] <-a action>\n"
4361 " <-r restype] [-k key] [-s sa_key]\n"
4362 " ctladm rtpg [dev_id][general options]\n"
4363 " ctladm start [dev_id][general options] [-i] [-o]\n"
4364 " ctladm stop [dev_id][general options] [-i] [-o]\n"
4365 " ctladm synccache [dev_id][general options] [-l lba]\n"
4366 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
4367 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
4368 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
4369 " [-S serial_num] [-t dev_type]\n"
4370 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
4371 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
4372 " ctladm devlist [-b backend] [-v] [-x]\n"
4373 " ctladm shutdown\n"
4375 " ctladm hardstop\n"
4376 " ctladm hardstart\n"
4378 " ctladm bbrread [dev_id] <-l lba> <-d datalen>\n"
4379 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
4381 " ctladm realsync <on|off|query>\n"
4382 " ctladm setsync [dev_id] <-i interval>\n"
4383 " ctladm getsync [dev_id]\n"
4384 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
4385 " [-s len fmt [args]] [-c] [-d delete_id]\n"
4386 " ctladm port <-l | -o <on|off> | [-w wwnn][-W wwpn]>\n"
4387 " [-p targ_port] [-t port_type] [-q] [-x]\n"
4388 " ctladm portlist [-f frontend] [-v] [-x]\n"
4389 " ctladm islist [-v | -x]\n"
4390 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
4391 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
4393 " ctladm dumpstructs\n"
4395 "General Options:\n"
4396 "-I intiator_id : defaults to 7, used to change the initiator id\n"
4397 "-C retries : specify the number of times to retry this command\n"
4398 "-D devicename : specify the device to operate on\n"
4399 " : (default is %s)\n"
4400 "read/write options:\n"
4401 "-l lba : logical block address\n"
4402 "-d len : read/write length, in blocks\n"
4403 "-f file|- : write/read data to/from file or stdout/stdin\n"
4404 "-b blocksize : block size, in bytes\n"
4405 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
4406 "-N : do not copy data to/from userland\n"
4407 "readcapacity options:\n"
4408 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4409 "modesense options:\n"
4410 "-m page : specify the mode page to view\n"
4411 "-l : request a list of supported pages\n"
4412 "-P pc : specify the page control value: 0-3 (current,\n"
4413 " changeable, default, saved, respectively)\n"
4414 "-d : disable block descriptors for mode sense\n"
4415 "-S subpage : specify a subpage\n"
4416 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
4417 "persistent reserve in options:\n"
4418 "-a action : specify the action value: 0-2 (read key, read\n"
4419 " reservation, read capabilities, respectively)\n"
4420 "persistent reserve out options:\n"
4421 "-a action : specify the action value: 0-5 (register, reserve,\n"
4422 " release, clear, preempt, register and ignore)\n"
4423 "-k key : key value\n"
4424 "-s sa_key : service action value\n"
4425 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
4426 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
4427 "start/stop options:\n"
4428 "-i : set the immediate bit (CTL does not support this)\n"
4429 "-o : set the on/offline bit\n"
4430 "synccache options:\n"
4431 "-l lba : set the starting LBA\n"
4432 "-b blockcount : set the length to sync in blocks\n"
4433 "-r : set the relative addressing bit\n"
4434 "-i : set the immediate bit\n"
4435 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4437 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4438 "-B blocksize : LUN blocksize in bytes (some backends)\n"
4439 "-d device_id : SCSI VPD page 0x83 ID\n"
4440 "-l lun_id : requested LUN number\n"
4441 "-o name=value : backend-specific options, multiple allowed\n"
4442 "-s size_bytes : LUN size in bytes (some backends)\n"
4443 "-S serial_num : SCSI VPD page 0x80 serial number\n"
4444 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
4446 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4447 "-l lun_id : LUN number to delete\n"
4448 "-o name=value : backend-specific options, multiple allowed\n"
4449 "devlist options:\n"
4450 "-b backend : list devices from specified backend only\n"
4451 "-v : be verbose, show backend attributes\n"
4452 "-x : dump raw XML\n"
4454 "-l datamove|done : delay command at datamove or done phase\n"
4455 "-T oneshot : delay one command, then resume normal completion\n"
4456 "-T cont : delay all commands\n"
4457 "-t secs : number of seconds to delay\n"
4459 "-i error_action : action to perform\n"
4460 "-p pattern : command pattern to look for\n"
4461 "-r lba,len : LBA range for pattern\n"
4462 "-s len fmt [args] : sense data for custom sense action\n"
4463 "-c : continuous operation\n"
4464 "-d delete_id : error id to delete\n"
4466 "-l : list frontend ports\n"
4467 "-o on|off : turn frontend ports on or off\n"
4468 "-w wwnn : set WWNN for one frontend\n"
4469 "-W wwpn : set WWPN for one frontend\n"
4470 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
4471 "-p targ_port : specify target port number\n"
4472 "-q : omit header in list output\n"
4473 "-x : output port list in XML format\n"
4474 "bbrread options:\n"
4475 "-l lba : starting LBA\n"
4476 "-d datalen : length, in bytes, to read\n",
4481 main(int argc, char **argv)
4484 ctladm_cmdfunction command;
4485 ctladm_cmdargs cmdargs;
4486 ctladm_optret optreturn;
4488 const char *mainopt = "C:D:I:";
4489 const char *subopt = NULL;
4490 char combinedopt[256];
4499 cmdargs = CTLADM_ARG_NONE;
4500 command = CTLADM_CMD_HELP;
4515 * Get the base option.
4517 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4519 if (optreturn == CC_OR_AMBIGUOUS) {
4520 warnx("ambiguous option %s", argv[1]);
4523 } else if (optreturn == CC_OR_NOT_FOUND) {
4524 warnx("option %s not found", argv[1]);
4529 if (cmdargs & CTLADM_ARG_NEED_TL) {
4531 || (!isdigit(argv[2][0]))) {
4532 warnx("option %s requires a target:lun argument",
4537 retval = cctl_parse_tl(argv[2], &target, &lun);
4539 errx(1, "invalid target:lun argument %s", argv[2]);
4541 cmdargs |= CTLADM_ARG_TARG_LUN;
4546 * Ahh, getopt(3) is a pain.
4548 * This is a gross hack. There really aren't many other good
4549 * options (excuse the pun) for parsing options in a situation like
4550 * this. getopt is kinda braindead, so you end up having to run
4551 * through the options twice, and give each invocation of getopt
4552 * the option string for the other invocation.
4554 * You would think that you could just have two groups of options.
4555 * The first group would get parsed by the first invocation of
4556 * getopt, and the second group would get parsed by the second
4557 * invocation of getopt. It doesn't quite work out that way. When
4558 * the first invocation of getopt finishes, it leaves optind pointing
4559 * to the argument _after_ the first argument in the second group.
4560 * So when the second invocation of getopt comes around, it doesn't
4561 * recognize the first argument it gets and then bails out.
4563 * A nice alternative would be to have a flag for getopt that says
4564 * "just keep parsing arguments even when you encounter an unknown
4565 * argument", but there isn't one. So there's no real clean way to
4566 * easily parse two sets of arguments without having one invocation
4567 * of getopt know about the other.
4569 * Without this hack, the first invocation of getopt would work as
4570 * long as the generic arguments are first, but the second invocation
4571 * (in the subfunction) would fail in one of two ways. In the case
4572 * where you don't set optreset, it would fail because optind may be
4573 * pointing to the argument after the one it should be pointing at.
4574 * In the case where you do set optreset, and reset optind, it would
4575 * fail because getopt would run into the first set of options, which
4576 * it doesn't understand.
4578 * All of this would "sort of" work if you could somehow figure out
4579 * whether optind had been incremented one option too far. The
4580 * mechanics of that, however, are more daunting than just giving
4581 * both invocations all of the expect options for either invocation.
4583 * Needless to say, I wouldn't mind if someone invented a better
4584 * (non-GPL!) command line parsing interface than getopt. I
4585 * wouldn't mind if someone added more knobs to getopt to make it
4586 * work better. Who knows, I may talk myself into doing it someday,
4587 * if the standards weenies let me. As it is, it just leads to
4588 * hackery like this and causes people to avoid it in some cases.
4590 * KDM, September 8th, 1998
4593 sprintf(combinedopt, "%s%s", mainopt, subopt);
4595 sprintf(combinedopt, "%s", mainopt);
4598 * Start getopt processing at argv[2/3], since we've already
4599 * accepted argv[1..2] as the command name, and as a possible
4605 * Now we run through the argument list looking for generic
4606 * options, and ignoring options that possibly belong to
4609 while ((c = getopt(argc, argv, combinedopt))!= -1){
4612 cmdargs |= CTLADM_ARG_RETRIES;
4613 retries = strtol(optarg, NULL, 0);
4616 device = strdup(optarg);
4617 cmdargs |= CTLADM_ARG_DEVICE;
4620 cmdargs |= CTLADM_ARG_INITIATOR;
4621 initid = strtol(optarg, NULL, 0);
4628 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4635 * Default to opening the CTL device for now.
4637 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4638 && (command != CTLADM_CMD_HELP)) {
4639 device = strdup(CTL_DEFAULT_DEV);
4640 cmdargs |= CTLADM_ARG_DEVICE;
4643 if ((cmdargs & CTLADM_ARG_DEVICE)
4644 && (command != CTLADM_CMD_HELP)) {
4645 fd = open(device, O_RDWR);
4646 if (fd == -1 && errno == ENOENT) {
4647 saved_errno = errno;
4648 retval = kldload("ctl");
4650 fd = open(device, O_RDWR);
4652 errno = saved_errno;
4655 fprintf(stderr, "%s: error opening %s: %s\n",
4656 argv[0], device, strerror(errno));
4660 } else if ((command != CTLADM_CMD_HELP)
4661 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4662 fprintf(stderr, "%s: you must specify a device with the "
4663 "--device argument for this command\n", argv[0]);
4664 command = CTLADM_CMD_HELP;
4669 case CTLADM_CMD_TUR:
4670 retval = cctl_tur(fd, target, lun, initid, retries);
4672 case CTLADM_CMD_INQUIRY:
4673 retval = cctl_inquiry(fd, target, lun, initid, retries);
4675 case CTLADM_CMD_REQ_SENSE:
4676 retval = cctl_req_sense(fd, target, lun, initid, retries);
4678 case CTLADM_CMD_REPORT_LUNS:
4679 retval = cctl_report_luns(fd, target, lun, initid, retries);
4681 case CTLADM_CMD_CREATE:
4682 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4685 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4687 case CTLADM_CMD_DEVLIST:
4688 retval = cctl_devlist(fd, argc, argv, combinedopt);
4690 case CTLADM_CMD_READ:
4691 case CTLADM_CMD_WRITE:
4692 retval = cctl_read_write(fd, target, lun, initid, retries,
4693 argc, argv, combinedopt, command);
4695 case CTLADM_CMD_PORT:
4696 retval = cctl_port(fd, argc, argv, combinedopt);
4698 case CTLADM_CMD_PORTLIST:
4699 retval = cctl_portlist(fd, argc, argv, combinedopt);
4701 case CTLADM_CMD_READCAPACITY:
4702 retval = cctl_read_capacity(fd, target, lun, initid, retries,
4703 argc, argv, combinedopt);
4705 case CTLADM_CMD_MODESENSE:
4706 retval = cctl_mode_sense(fd, target, lun, initid, retries,
4707 argc, argv, combinedopt);
4709 case CTLADM_CMD_START:
4710 case CTLADM_CMD_STOP:
4711 retval = cctl_start_stop(fd, target, lun, initid, retries,
4712 (command == CTLADM_CMD_START) ? 1 : 0,
4713 argc, argv, combinedopt);
4715 case CTLADM_CMD_SYNC_CACHE:
4716 retval = cctl_sync_cache(fd, target, lun, initid, retries,
4717 argc, argv, combinedopt);
4719 case CTLADM_CMD_SHUTDOWN:
4720 case CTLADM_CMD_STARTUP:
4721 retval = cctl_startup_shutdown(fd, target, lun, initid,
4724 case CTLADM_CMD_HARDSTOP:
4725 case CTLADM_CMD_HARDSTART:
4726 retval = cctl_hardstopstart(fd, command);
4728 case CTLADM_CMD_BBRREAD:
4729 retval = cctl_bbrread(fd, target, lun, initid, argc, argv,
4732 case CTLADM_CMD_LUNLIST:
4733 retval = cctl_lunlist(fd);
4735 case CTLADM_CMD_DELAY:
4736 retval = cctl_delay(fd, target, lun, argc, argv, combinedopt);
4738 case CTLADM_CMD_REALSYNC:
4739 retval = cctl_realsync(fd, argc, argv);
4741 case CTLADM_CMD_SETSYNC:
4742 case CTLADM_CMD_GETSYNC:
4743 retval = cctl_getsetsync(fd, target, lun, command,
4744 argc, argv, combinedopt);
4746 case CTLADM_CMD_ERR_INJECT:
4747 retval = cctl_error_inject(fd, target, lun, argc, argv,
4750 case CTLADM_CMD_DUMPOOA:
4751 retval = cctl_dump_ooa(fd, argc, argv);
4753 case CTLADM_CMD_DUMPSTRUCTS:
4754 retval = cctl_dump_structs(fd, cmdargs);
4756 case CTLADM_CMD_PRES_IN:
4757 retval = cctl_persistent_reserve_in(fd, target, lun, initid,
4758 argc, argv, combinedopt,
4761 case CTLADM_CMD_PRES_OUT:
4762 retval = cctl_persistent_reserve_out(fd, target, lun, initid,
4763 argc, argv, combinedopt,
4766 case CTLADM_CMD_INQ_VPD_DEVID:
4767 retval = cctl_inquiry_vpd_devid(fd, target, lun, initid);
4769 case CTLADM_CMD_RTPG:
4770 retval = cctl_report_target_port_group(fd, target, lun, initid);
4772 case CTLADM_CMD_MODIFY:
4773 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4775 case CTLADM_CMD_ISLIST:
4776 retval = cctl_islist(fd, argc, argv, combinedopt);
4778 case CTLADM_CMD_ISLOGOUT:
4779 retval = cctl_islogout(fd, argc, argv, combinedopt);
4781 case CTLADM_CMD_ISTERMINATE:
4782 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4784 case CTLADM_CMD_HELP: