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:ip:qvx"},
195 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
196 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
197 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
198 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
199 {"realsync", CTLADM_CMD_REALSYNC, CTLADM_ARG_NONE, NULL},
200 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
201 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
202 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
203 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
204 {"setsync", CTLADM_CMD_SETSYNC, CTLADM_ARG_NEED_TL, "i:"},
205 {"shutdown", CTLADM_CMD_SHUTDOWN, CTLADM_ARG_NONE, NULL},
206 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
207 {"startup", CTLADM_CMD_STARTUP, CTLADM_ARG_NONE, NULL},
208 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
209 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
210 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
211 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
212 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
213 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
218 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
219 ctladm_cmdargs *argnum, const char **subopt);
220 static int cctl_parse_tl(char *str, int *target, int *lun);
221 static int cctl_dump_ooa(int fd, int argc, char **argv);
222 static int cctl_port_dump(int fd, int quiet, int xml, int32_t fe_num,
223 ctl_port_type port_type);
224 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
225 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
226 static int cctl_delay(int fd, int target, int lun, int argc, char **argv,
228 static int cctl_lunlist(int fd);
229 static void cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len);
230 static void cctl_cfi_bbr_statusstr(cfi_bbrread_status, char *str, int str_len);
231 static int cctl_hardstopstart(int fd, ctladm_cmdfunction command);
232 static int cctl_bbrread(int fd, int target, int lun, int iid, int argc,
233 char **argv, char *combinedopt);
234 static int cctl_startup_shutdown(int fd, int target, int lun, int iid,
235 ctladm_cmdfunction command);
236 static int cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
237 int argc, char **argv, char *combinedopt);
238 static int cctl_start_stop(int fd, int target, int lun, int iid, int retries,
239 int start, int argc, char **argv, char *combinedopt);
240 static int cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
241 int argc, char **argv, char *combinedopt);
242 static int cctl_read_capacity(int fd, int target, int lun, int iid,
243 int retries, int argc, char **argv,
245 static int cctl_read_write(int fd, int target, int lun, int iid, int retries,
246 int argc, char **argv, char *combinedopt,
247 ctladm_cmdfunction command);
248 static int cctl_get_luns(int fd, int target, int lun, int iid, int retries,
249 struct scsi_report_luns_data **lun_data,
251 static int cctl_report_luns(int fd, int target, int lun, int iid, int retries);
252 static int cctl_tur(int fd, int target, int lun, int iid, int retries);
253 static int cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
254 char *path_str, int path_len,
255 struct scsi_inquiry_data *inq_data);
256 static int cctl_inquiry(int fd, int target, int lun, int iid, int retries);
257 static int cctl_req_sense(int fd, int target, int lun, int iid, int retries);
258 static int cctl_persistent_reserve_in(int fd, int target, int lun,
259 int initiator, int argc, char **argv,
260 char *combinedopt, int retry_count);
261 static int cctl_persistent_reserve_out(int fd, int target, int lun,
262 int initiator, int argc, char **argv,
263 char *combinedopt, int retry_count);
264 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
265 static int cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator);
266 static int cctl_report_target_port_group(int fd, int target, int lun,
268 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
271 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
272 ctladm_cmdargs *argnum, const char **subopt)
274 struct ctladm_opts *opts;
277 for (opts = table; (opts != NULL) && (opts->optname != NULL);
279 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
280 *cmdnum = opts->cmdnum;
281 *argnum = opts->argnum;
282 *subopt = opts->subopt;
284 if (strcmp(opts->optname, arg) == 0)
285 return (CC_OR_FOUND);
287 if (++num_matches > 1)
288 return(CC_OR_AMBIGUOUS);
295 return(CC_OR_NOT_FOUND);
300 cctl_parse_tl(char *str, int *target, int *lun)
307 while (isspace(*str) && (*str != '\0'))
310 tmpstr = (char *)strtok(str, ":");
311 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
312 *target = strtol(tmpstr, NULL, 0);
313 tmpstr = (char *)strtok(NULL, ":");
314 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
315 *lun = strtol(tmpstr, NULL, 0);
325 cctl_dump_ooa(int fd, int argc, char **argv)
328 long double cmd_latency;
329 int num_entries, len;
330 int target = -1, lun = -1;
337 && (isdigit(argv[2][0]))) {
338 retval = cctl_parse_tl(argv[2], &target, &lun);
340 warnx("invalid target:lun argument %s", argv[2]);
344 len = num_entries * sizeof(struct ctl_ooa_entry);
346 bzero(&ooa, sizeof(ooa));
348 ooa.entries = malloc(len);
350 if (ooa.entries == NULL) {
351 warn("%s: error mallocing %d bytes", __func__, len);
358 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
361 ooa.alloc_num = num_entries;
362 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
363 warn("%s: CTL_GET_OOA ioctl failed", __func__);
368 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
369 num_entries = num_entries * 2;
375 if (ooa.status != CTL_OOA_OK) {
376 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
382 fprintf(stdout, "Dumping OOA queues\n");
383 for (i = 0; i < ooa.fill_num; i++) {
384 struct ctl_ooa_entry *entry;
385 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
386 struct bintime delta_bt;
389 entry = &ooa.entries[i];
391 delta_bt = ooa.cur_bt;
392 bintime_sub(&delta_bt, &entry->start_bt);
393 bintime2timespec(&delta_bt, &ts);
394 cmd_latency = ts.tv_sec * 1000;
396 cmd_latency += ts.tv_nsec / 1000000;
398 fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
400 (intmax_t)entry->lun_num, entry->tag_num,
401 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
403 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
404 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
406 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
408 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
409 scsi_op_desc(entry->cdb[0], NULL),
410 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
413 fprintf(stdout, "OOA queues dump done\n");
415 if (ioctl(fd, CTL_DUMP_OOA) == -1) {
416 warn("%s: CTL_DUMP_OOA ioctl failed", __func__);
428 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
430 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
438 cctl_port_dump(int fd, int quiet, int xml, int32_t targ_port,
439 ctl_port_type port_type)
441 struct ctl_port_list port_list;
442 struct ctl_port_entry *entries;
443 struct sbuf *sb = NULL;
452 entries = malloc(sizeof(*entries) * num_entries);
453 bzero(&port_list, sizeof(port_list));
454 port_list.entries = entries;
455 port_list.alloc_num = num_entries;
456 port_list.alloc_len = num_entries * sizeof(*entries);
457 if (ioctl(fd, CTL_GET_PORT_LIST, &port_list) != 0) {
458 warn("%s: CTL_GET_PORT_LIST ioctl failed", __func__);
461 if (port_list.status == CTL_PORT_LIST_NEED_MORE_SPACE) {
462 printf("%s: allocated %d, need %d, retrying\n", __func__,
463 num_entries, port_list.fill_num + port_list.dropped_num);
465 num_entries = port_list.fill_num + port_list.dropped_num;
471 printf("Port Online Type Name pp vp %-18s %-18s\n",
475 sb = sbuf_new_auto();
476 sbuf_printf(sb, "<ctlfelist>\n");
478 for (i = 0; i < port_list.fill_num; i++) {
479 struct ctl_port_entry *entry;
484 switch (entry->port_type) {
494 case CTL_PORT_INTERNAL:
512 * If the user specified a frontend number or a particular
513 * frontend type, only print out that particular frontend
516 if ((targ_port != -1)
517 && (targ_port != entry->targ_port))
519 else if ((port_type != CTL_PORT_NONE)
520 && ((port_type & entry->port_type) == 0))
526 printf("Num: %ju Type: %s (%#x) Name: %s Physical Port: %d "
527 "Virtual Port: %d\n", (uintmax_t)entry->fe_num, type,
528 entry->port_type, entry->fe_name, entry->physical_port,
529 entry->virtual_port);
530 printf("WWNN %#jx WWPN %#jx Online: %s\n",
531 (uintmax_t)entry->wwnn, (uintmax_t)entry->wwpn,
532 (entry->online) ? "YES" : "NO" );
535 printf("%-4d %-6s %-8s %-12s %-2d %-2d %#-18jx "
537 entry->targ_port, (entry->online) ? "YES" : "NO",
538 type, entry->port_name, entry->physical_port,
539 entry->virtual_port, (uintmax_t)entry->wwnn,
540 (uintmax_t)entry->wwpn);
542 sbuf_printf(sb, "<targ_port id=\"%d\">\n",
544 sbuf_printf(sb, "<online>%s</online>\n",
545 (entry->online) ? "YES" : "NO");
546 sbuf_printf(sb, "<port_type>%s</port_type>\n", type);
547 sbuf_printf(sb, "<port_name>%s</port_name>\n",
549 sbuf_printf(sb, "<physical_port>%d</physical_port>\n",
550 entry->physical_port);
551 sbuf_printf(sb, "<virtual_port>%d</virtual_port>\n",
552 entry->virtual_port);
553 sbuf_printf(sb, "<wwnn>%#jx</wwnn>\n",
554 (uintmax_t)entry->wwnn);
555 sbuf_printf(sb, "<wwpn>%#jx</wwpn>\n",
556 (uintmax_t)entry->wwpn);
557 sbuf_printf(sb, "</targ_port>\n");
562 sbuf_printf(sb, "</ctlfelist>\n");
563 if (sbuf_finish(sb) != 0)
564 err(1, "%s: sbuf_finish", __func__);
565 printf("%s", sbuf_data(sb));
570 * Give some indication that we didn't find the frontend or
571 * frontend type requested by the user. We could print something
572 * out, but it would probably be better to hide that behind a
576 && ((targ_port != -1)
577 || (port_type != CTL_PORT_NONE)))
591 static struct ctladm_opts cctl_fe_table[] = {
592 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
593 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
594 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
595 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
596 {"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
597 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
602 cctl_port(int fd, int argc, char **argv, char *combinedopt)
605 int32_t targ_port = -1;
607 int wwnn_set = 0, wwpn_set = 0;
608 uint64_t wwnn = 0, wwpn = 0;
609 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
610 struct ctl_port_entry entry;
611 ctl_port_type port_type = CTL_PORT_NONE;
612 int quiet = 0, xml = 0;
614 while ((c = getopt(argc, argv, combinedopt)) != -1) {
617 if (port_mode != CCTL_PORT_MODE_NONE)
620 port_mode = CCTL_PORT_MODE_LIST;
623 if (port_mode != CCTL_PORT_MODE_NONE)
626 if (strcasecmp(optarg, "on") == 0)
627 port_mode = CCTL_PORT_MODE_ON;
628 else if (strcasecmp(optarg, "off") == 0)
629 port_mode = CCTL_PORT_MODE_OFF;
631 warnx("Invalid -o argument %s, \"on\" or "
632 "\"off\" are the only valid args",
639 targ_port = strtol(optarg, NULL, 0);
645 ctladm_optret optret;
646 ctladm_cmdargs argnum;
648 ctl_port_type tmp_port_type;
650 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
652 if (optret == CC_OR_AMBIGUOUS) {
653 warnx("%s: ambiguous frontend type %s",
657 } else if (optret == CC_OR_NOT_FOUND) {
658 warnx("%s: invalid frontend type %s",
664 port_type |= tmp_port_type;
668 if ((port_mode != CCTL_PORT_MODE_NONE)
669 && (port_mode != CCTL_PORT_MODE_SET))
672 port_mode = CCTL_PORT_MODE_SET;
674 wwnn = strtoull(optarg, NULL, 0);
678 if ((port_mode != CCTL_PORT_MODE_NONE)
679 && (port_mode != CCTL_PORT_MODE_SET))
682 port_mode = CCTL_PORT_MODE_SET;
684 wwpn = strtoull(optarg, NULL, 0);
694 * The user can specify either one or more frontend types (-t), or
695 * a specific frontend, but not both.
697 * If the user didn't specify a frontend type or number, set it to
698 * all. This is primarily needed for the enable/disable ioctls.
699 * This will be a no-op for the listing code. For the set ioctl,
700 * we'll throw an error, since that only works on one port at a time.
702 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
703 warnx("%s: can only specify one of -t or -n", __func__);
706 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
707 port_type = CTL_PORT_ALL;
709 bzero(&entry, sizeof(entry));
712 * These are needed for all but list/dump mode.
714 entry.port_type = port_type;
715 entry.targ_port = targ_port;
718 case CCTL_PORT_MODE_LIST:
719 cctl_port_dump(fd, quiet, xml, targ_port, port_type);
721 case CCTL_PORT_MODE_SET:
722 if (targ_port == -1) {
723 warnx("%s: -w and -W require -n", __func__);
729 entry.flags |= CTL_PORT_WWNN_VALID;
733 entry.flags |= CTL_PORT_WWPN_VALID;
737 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
738 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
743 case CCTL_PORT_MODE_ON:
744 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
745 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
749 fprintf(stdout, "Front End Ports enabled\n");
751 case CCTL_PORT_MODE_OFF:
752 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
753 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
757 fprintf(stdout, "Front End Ports disabled\n");
760 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
771 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
776 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
779 if (ioctl(fd, CTL_IO, io) == -1) {
780 warn("%s: error sending CTL_IO ioctl", func);
783 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
790 cctl_delay(int fd, int target, int lun, int argc, char **argv,
793 struct ctl_io_delay_info delay_info;
794 char *delayloc = NULL;
795 char *delaytype = NULL;
802 memset(&delay_info, 0, sizeof(delay_info));
804 while ((c = getopt(argc, argv, combinedopt)) != -1) {
807 delaytype = strdup(optarg);
810 delayloc = strdup(optarg);
813 delaytime = strtoul(optarg, NULL, 0);
818 if (delaytime == -1) {
819 warnx("%s: you must specify the delaytime with -t", __func__);
824 if (strcasecmp(delayloc, "datamove") == 0)
825 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
826 else if (strcasecmp(delayloc, "done") == 0)
827 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
829 warnx("%s: invalid delay location %s", __func__, delayloc);
834 if ((delaytype == NULL)
835 || (strcmp(delaytype, "oneshot") == 0))
836 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
837 else if (strcmp(delaytype, "cont") == 0)
838 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
840 warnx("%s: invalid delay type %s", __func__, delaytype);
845 delay_info.target_id = target;
846 delay_info.lun_id = lun;
847 delay_info.delay_secs = delaytime;
849 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
850 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
854 switch (delay_info.status) {
855 case CTL_DELAY_STATUS_NONE:
856 warnx("%s: no delay status??", __func__);
859 case CTL_DELAY_STATUS_OK:
861 case CTL_DELAY_STATUS_INVALID_LUN:
862 warnx("%s: invalid lun %d", __func__, lun);
865 case CTL_DELAY_STATUS_INVALID_TYPE:
866 warnx("%s: invalid delay type %d", __func__,
867 delay_info.delay_type);
870 case CTL_DELAY_STATUS_INVALID_LOC:
871 warnx("%s: delay location %s not implemented?", __func__,
875 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
876 warnx("%s: delay not implemented in the kernel", __func__);
877 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
881 warnx("%s: unknown delay return status %d", __func__,
888 /* delayloc should never be NULL, but just in case...*/
889 if (delayloc != NULL)
896 cctl_realsync(int fd, int argc, char **argv)
905 warnx("%s %s takes exactly one argument", argv[0], argv[1]);
912 if (strncasecmp(syncarg, "query", min(strlen(syncarg),
913 strlen("query"))) == 0) {
914 if (ioctl(fd, CTL_REALSYNC_GET, &syncstate) == -1) {
915 warn("%s: CTL_REALSYNC_GET ioctl failed", __func__);
919 fprintf(stdout, "SYNCHRONIZE CACHE support is: ");
922 fprintf(stdout, "OFF\n");
925 fprintf(stdout, "ON\n");
928 fprintf(stdout, "unknown (%d)\n", syncstate);
932 } else if (strcasecmp(syncarg, "on") == 0) {
934 } else if (strcasecmp(syncarg, "off") == 0) {
937 warnx("%s: invalid realsync argument %s", __func__, syncarg);
942 if (ioctl(fd, CTL_REALSYNC_SET, &syncstate) == -1) {
943 warn("%s: CTL_REALSYNC_SET ioctl failed", __func__);
952 cctl_getsetsync(int fd, int target, int lun, ctladm_cmdfunction command,
953 int argc, char **argv, char *combinedopt)
955 struct ctl_sync_info sync_info;
957 int sync_interval = -1;
963 memset(&sync_info, 0, sizeof(sync_info));
964 sync_info.target_id = target;
965 sync_info.lun_id = lun;
967 while ((c = getopt(argc, argv, combinedopt)) != -1) {
970 sync_interval = strtoul(optarg, NULL, 0);
977 if (command == CTLADM_CMD_SETSYNC) {
978 if (sync_interval == -1) {
979 warnx("%s: you must specify the sync interval with -i",
984 sync_info.sync_interval = sync_interval;
985 ioctl_cmd = CTL_SETSYNC;
987 ioctl_cmd = CTL_GETSYNC;
990 if (ioctl(fd, ioctl_cmd, &sync_info) == -1) {
991 warn("%s: CTL_%sSYNC ioctl failed", __func__,
992 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET");
997 switch (sync_info.status) {
999 if (command == CTLADM_CMD_GETSYNC) {
1000 fprintf(stdout, "%d:%d: sync interval: %d\n",
1001 target, lun, sync_info.sync_interval);
1004 case CTL_GS_SYNC_NO_LUN:
1005 warnx("%s: unknown target:LUN %d:%d", __func__, target, lun);
1008 case CTL_GS_SYNC_NONE:
1010 warnx("%s: unknown CTL_%sSYNC status %d", __func__,
1011 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET",
1020 static struct ctladm_opts cctl_err_types[] = {
1021 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
1022 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
1023 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
1024 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
1029 static struct ctladm_opts cctl_err_patterns[] = {
1030 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
1031 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
1032 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1033 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1034 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
1035 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
1036 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
1038 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
1044 cctl_error_inject(int fd, uint32_t target, uint32_t lun, int argc, char **argv,
1048 struct ctl_error_desc err_desc;
1051 uint64_t delete_id = 0;
1052 int delete_id_set = 0;
1058 bzero(&err_desc, sizeof(err_desc));
1059 err_desc.target_id = target;
1060 err_desc.lun_id = lun;
1062 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1068 delete_id = strtoull(optarg, NULL, 0);
1073 ctladm_optret optret;
1074 ctladm_cmdargs argnum;
1078 ctl_lun_error err_type;
1080 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
1081 warnx("%s: can't specify multiple -i "
1082 "arguments", __func__);
1086 optret = getoption(cctl_err_types, optarg,
1087 &err_type, &argnum, &subopt);
1088 err_desc.lun_error = err_type;
1090 ctl_lun_error_pattern pattern;
1092 optret = getoption(cctl_err_patterns, optarg,
1093 &pattern, &argnum, &subopt);
1094 err_desc.error_pattern |= pattern;
1097 if (optret == CC_OR_AMBIGUOUS) {
1098 warnx("%s: ambiguous argument %s", __func__,
1102 } else if (optret == CC_OR_NOT_FOUND) {
1103 warnx("%s: argument %s not found", __func__,
1111 char *tmpstr, *tmpstr2;
1113 tmpstr = strdup(optarg);
1114 if (tmpstr == NULL) {
1115 warn("%s: error duplicating string %s",
1121 tmpstr2 = strsep(&tmpstr, ",");
1122 if (tmpstr2 == NULL) {
1123 warnx("%s: invalid -r argument %s", __func__,
1129 lba = strtoull(tmpstr2, NULL, 0);
1130 tmpstr2 = strsep(&tmpstr, ",");
1131 if (tmpstr2 == NULL) {
1132 warnx("%s: no len argument for -r lba,len, got"
1133 " %s", __func__, optarg);
1138 len = strtoul(tmpstr2, NULL, 0);
1143 struct get_hook hook;
1146 sense_len = strtol(optarg, NULL, 0);
1147 if (sense_len <= 0) {
1148 warnx("invalid number of sense bytes %d",
1154 sense_len = MIN(sense_len, SSD_FULL_SIZE);
1156 hook.argc = argc - optind;
1157 hook.argv = argv + optind;
1160 sensestr = cget(&hook, NULL);
1161 if ((sensestr != NULL)
1162 && (sensestr[0] == '-')) {
1166 (uint8_t *)&err_desc.custom_sense,
1167 sense_len, sensestr, iget, &hook);
1177 if (delete_id_set != 0) {
1178 err_desc.serial = delete_id;
1179 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
1180 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
1187 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
1188 warnx("%s: error injection command (-i) needed",
1192 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
1193 && (sense_len == 0)) {
1194 warnx("%s: custom error requires -s", __func__);
1199 if (continuous != 0)
1200 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
1203 * If fd_sense is set, we need to read the sense data the user
1204 * wants returned from stdin.
1206 if (fd_sense == 1) {
1208 int amt_to_read = sense_len;
1209 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
1211 for (amt_read = 0; amt_to_read > 0;
1212 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1213 if (amt_read == -1) {
1214 warn("error reading sense data from stdin");
1218 amt_to_read -= amt_read;
1219 buf_ptr += amt_read;
1223 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1224 warnx("%s: command pattern (-p) needed", __func__);
1230 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1232 * We could check here to see whether it's a read/write
1233 * command, but that will be pointless once we allow
1234 * custom patterns. At that point, the user could specify
1235 * a READ(6) CDB type, and we wouldn't have an easy way here
1236 * to verify whether range checking is possible there. The
1237 * user will just figure it out when his error never gets
1241 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1242 warnx("%s: need read and/or write pattern if range "
1243 "is specified", __func__);
1248 err_desc.lba_range.lba = lba;
1249 err_desc.lba_range.len = len;
1252 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1253 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1256 printf("Error injection succeeded, serial number is %ju\n",
1257 (uintmax_t)err_desc.serial);
1265 cctl_lunlist(int fd)
1267 struct scsi_report_luns_data *lun_data;
1268 struct scsi_inquiry_data *inq_data;
1282 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1283 * if we ever acquire the ability to have multiple targets.
1285 if ((retval = cctl_get_luns(fd, target, /*lun*/ 0, initid,
1286 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1289 inq_data = malloc(sizeof(*inq_data));
1290 if (inq_data == NULL) {
1291 warn("%s: couldn't allocate memory for inquiry data\n",
1296 for (i = 0; i < num_luns; i++) {
1300 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1301 case RPL_LUNDATA_ATYP_PERIPH:
1302 lun_val = lun_data->luns[i].lundata[1];
1304 case RPL_LUNDATA_ATYP_FLAT:
1305 lun_val = (lun_data->luns[i].lundata[0] &
1306 RPL_LUNDATA_FLAT_LUN_MASK) |
1307 (lun_data->luns[i].lundata[1] <<
1308 RPL_LUNDATA_FLAT_LUN_BITS);
1310 case RPL_LUNDATA_ATYP_LUN:
1311 case RPL_LUNDATA_ATYP_EXTLUN:
1313 fprintf(stdout, "Unsupported LUN format %d\n",
1314 lun_data->luns[i].lundata[0] &
1315 RPL_LUNDATA_ATYP_MASK);
1322 if ((retval = cctl_get_inquiry(fd, target, lun_val, initid,
1323 /*retries*/ 2, scsi_path,
1328 printf("%s", scsi_path);
1329 scsi_print_inquiry(inq_data);
1333 if (lun_data != NULL)
1336 if (inq_data != NULL)
1343 cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len)
1346 case CFI_MT_PORT_OFFLINE:
1347 snprintf(str, str_len, "Port Offline");
1350 snprintf(str, str_len, "Error");
1352 case CFI_MT_SUCCESS:
1353 snprintf(str, str_len, "Success");
1356 snprintf(str, str_len, "None??");
1359 snprintf(str, str_len, "Unknown status: %d", status);
1365 cctl_cfi_bbr_statusstr(cfi_bbrread_status status, char *str, int str_len)
1368 case CFI_BBR_SUCCESS:
1369 snprintf(str, str_len, "Success");
1371 case CFI_BBR_LUN_UNCONFIG:
1372 snprintf(str, str_len, "LUN not configured");
1374 case CFI_BBR_NO_LUN:
1375 snprintf(str, str_len, "LUN does not exist");
1377 case CFI_BBR_NO_MEM:
1378 snprintf(str, str_len, "Memory allocation error");
1380 case CFI_BBR_BAD_LEN:
1381 snprintf(str, str_len, "Length is not a multiple of blocksize");
1383 case CFI_BBR_RESERV_CONFLICT:
1384 snprintf(str, str_len, "Reservation conflict");
1386 case CFI_BBR_LUN_STOPPED:
1387 snprintf(str, str_len, "LUN is powered off");
1389 case CFI_BBR_LUN_OFFLINE_CTL:
1390 snprintf(str, str_len, "LUN is offline");
1392 case CFI_BBR_LUN_OFFLINE_RC:
1393 snprintf(str, str_len, "RAIDCore array is offline (double "
1396 case CFI_BBR_SCSI_ERROR:
1397 snprintf(str, str_len, "SCSI Error");
1400 snprintf(str, str_len, "Error");
1403 snprintf(str, str_len, "Unknown status: %d", status);
1409 cctl_hardstopstart(int fd, ctladm_cmdfunction command)
1411 struct ctl_hard_startstop_info hs_info;
1412 char error_str[256];
1418 if (command == CTLADM_CMD_HARDSTART)
1423 if (ioctl(fd, (do_start == 1) ? CTL_HARD_START : CTL_HARD_STOP,
1425 warn("%s: CTL_HARD_%s ioctl failed", __func__,
1426 (do_start == 1) ? "START" : "STOP");
1431 fprintf(stdout, "Hard %s Status: ", (command == CTLADM_CMD_HARDSTOP) ?
1433 cctl_cfi_mt_statusstr(hs_info.status, error_str, sizeof(error_str));
1434 fprintf(stdout, "%s\n", error_str);
1435 fprintf(stdout, "Total LUNs: %d\n", hs_info.total_luns);
1436 fprintf(stdout, "LUNs complete: %d\n", hs_info.luns_complete);
1437 fprintf(stdout, "LUNs failed: %d\n", hs_info.luns_failed);
1444 cctl_bbrread(int fd, int target __unused, int lun, int iid __unused,
1445 int argc, char **argv, char *combinedopt)
1447 struct ctl_bbrread_info bbr_info;
1448 char error_str[256];
1457 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1460 datalen = strtoul(optarg, NULL, 0);
1463 lba = strtoull(optarg, NULL, 0);
1472 warnx("%s: you must specify an LBA with -l", __func__);
1477 if (datalen == -1) {
1478 warnx("%s: you must specify a length with -d", __func__);
1483 bbr_info.lun_num = lun;
1486 * XXX KDM get the blocksize first??
1488 if ((datalen % 512) != 0) {
1489 warnx("%s: data length %d is not a multiple of 512 bytes",
1494 bbr_info.len = datalen;
1496 if (ioctl(fd, CTL_BBRREAD, &bbr_info) == -1) {
1497 warn("%s: CTL_BBRREAD ioctl failed", __func__);
1501 cctl_cfi_mt_statusstr(bbr_info.status, error_str, sizeof(error_str));
1502 fprintf(stdout, "BBR Read Overall Status: %s\n", error_str);
1503 cctl_cfi_bbr_statusstr(bbr_info.bbr_status, error_str,
1505 fprintf(stdout, "BBR Read Status: %s\n", error_str);
1507 * XXX KDM should we bother printing out SCSI status if we get
1508 * CFI_BBR_SCSI_ERROR back?
1510 * Return non-zero if this fails?
1517 cctl_startup_shutdown(int fd, int target, int lun, int iid,
1518 ctladm_cmdfunction command)
1522 struct scsi_report_luns_data *lun_data;
1523 struct scsi_inquiry_data *inq_data;
1533 * - step through each lun, do an inquiry
1534 * - check OOA queue on direct access luns
1535 * - send stop with offline bit to each direct access device with a
1537 * - if we get a reservation conflict, reset the LUN to clear it
1538 * and reissue the stop with the offline bit set
1543 io = ctl_scsi_alloc_io(id);
1545 warnx("%s: can't allocate memory", __func__);
1549 if ((retval = cctl_get_luns(fd, target, lun, iid, /*retries*/ 2,
1550 &lun_data, &num_luns)) != 0)
1553 inq_data = malloc(sizeof(*inq_data));
1554 if (inq_data == NULL) {
1555 warn("%s: couldn't allocate memory for inquiry data\n",
1560 for (i = 0; i < num_luns; i++) {
1565 * XXX KDM figure out a way to share this code with
1568 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1569 case RPL_LUNDATA_ATYP_PERIPH:
1570 lun_val = lun_data->luns[i].lundata[1];
1572 case RPL_LUNDATA_ATYP_FLAT:
1573 lun_val = (lun_data->luns[i].lundata[0] &
1574 RPL_LUNDATA_FLAT_LUN_MASK) |
1575 (lun_data->luns[i].lundata[1] <<
1576 RPL_LUNDATA_FLAT_LUN_BITS);
1578 case RPL_LUNDATA_ATYP_LUN:
1579 case RPL_LUNDATA_ATYP_EXTLUN:
1581 fprintf(stdout, "Unsupported LUN format %d\n",
1582 lun_data->luns[i].lundata[0] &
1583 RPL_LUNDATA_ATYP_MASK);
1590 if ((retval = cctl_get_inquiry(fd, target, lun_val, iid,
1591 /*retries*/ 2, scsi_path,
1596 printf("%s", scsi_path);
1597 scsi_print_inquiry(inq_data);
1599 * We only want to shutdown direct access devices.
1601 if (SID_TYPE(inq_data) != T_DIRECT) {
1602 printf("%s LUN is not direct access, skipped\n",
1607 if (command == CTLADM_CMD_SHUTDOWN) {
1608 struct ctl_ooa_info ooa_info;
1610 ooa_info.target_id = target;
1611 ooa_info.lun_id = lun_val;
1613 if (ioctl(fd, CTL_CHECK_OOA, &ooa_info) == -1) {
1614 printf("%s CTL_CHECK_OOA ioctl failed\n",
1619 if (ooa_info.status != CTL_OOA_SUCCESS) {
1620 printf("%s CTL_CHECK_OOA returned status %d\n",
1621 scsi_path, ooa_info.status);
1624 if (ooa_info.num_entries != 0) {
1625 printf("%s %d entr%s in the OOA queue, "
1626 "skipping shutdown\n", scsi_path,
1627 ooa_info.num_entries,
1628 (ooa_info.num_entries > 1)?"ies" : "y" );
1633 ctl_scsi_start_stop(/*io*/ io,
1634 /*start*/(command == CTLADM_CMD_STARTUP) ?
1638 /*power_conditions*/ SSS_PC_START_VALID,
1641 (command == CTLADM_CMD_STARTUP) ?
1642 CTL_TAG_SIMPLE :CTL_TAG_ORDERED,
1645 io->io_hdr.nexus.targ_target.id = target;
1646 io->io_hdr.nexus.targ_lun = lun_val;
1647 io->io_hdr.nexus.initid = id;
1649 if (cctl_do_io(fd, /*retries*/ 3, io, __func__) != 0) {
1654 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1655 ctl_io_error_print(io, inq_data, stderr);
1657 printf("%s LUN is now %s\n", scsi_path,
1658 (command == CTLADM_CMD_STARTUP) ? "online" :
1663 if (lun_data != NULL)
1666 if (inq_data != NULL)
1670 ctl_scsi_free_io(io);
1676 cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
1677 int argc, char **argv, char *combinedopt)
1683 uint64_t our_lba = 0;
1684 uint32_t our_block_count = 0;
1685 int reladr = 0, immed = 0;
1691 io = ctl_scsi_alloc_io(id);
1693 warnx("%s: can't allocate memory", __func__);
1697 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1700 our_block_count = strtoul(optarg, NULL, 0);
1703 cdb_size = strtol(optarg, NULL, 0);
1709 our_lba = strtoull(optarg, NULL, 0);
1719 if (cdb_size != -1) {
1725 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1726 "and 16", __func__, cdb_size);
1729 break; /* NOTREACHED */
1734 ctl_scsi_sync_cache(/*io*/ io,
1737 /*minimum_cdb_size*/ cdb_size,
1738 /*starting_lba*/ our_lba,
1739 /*block_count*/ our_block_count,
1740 /*tag_type*/ CTL_TAG_SIMPLE,
1743 io->io_hdr.nexus.targ_target.id = target;
1744 io->io_hdr.nexus.targ_lun = lun;
1745 io->io_hdr.nexus.initid = id;
1747 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1752 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1753 fprintf(stdout, "Cache synchronized successfully\n");
1755 ctl_io_error_print(io, NULL, stderr);
1757 ctl_scsi_free_io(io);
1763 cctl_start_stop(int fd, int target, int lun, int iid, int retries, int start,
1764 int argc, char **argv, char *combinedopt)
1769 int immed = 0, onoffline = 0;
1775 io = ctl_scsi_alloc_io(id);
1777 warnx("%s: can't allocate memory", __func__);
1781 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1794 * Use an ordered tag for the stop command, to guarantee that any
1795 * pending I/O will finish before the stop command executes. This
1796 * would normally be the case anyway, since CTL will basically
1797 * treat the start/stop command as an ordered command with respect
1798 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1800 ctl_scsi_start_stop(/*io*/ io,
1803 /*immediate*/ immed,
1804 /*power_conditions*/ SSS_PC_START_VALID,
1805 /*onoffline*/ onoffline,
1806 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1810 io->io_hdr.nexus.targ_target.id = target;
1811 io->io_hdr.nexus.targ_lun = lun;
1812 io->io_hdr.nexus.initid = id;
1814 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1819 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1820 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1821 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1822 (start) ? "started" : "stopped");
1824 ctl_io_error_print(io, NULL, stderr);
1827 ctl_scsi_free_io(io);
1833 cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
1834 int argc, char **argv, char *combinedopt)
1840 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1850 io = ctl_scsi_alloc_io(id);
1852 warn("%s: can't allocate memory", __func__);
1856 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1859 pc = strtoul(optarg, NULL, 0);
1862 subpage = strtoul(optarg, NULL, 0);
1871 page_code = strtoul(optarg, NULL, 0);
1874 cdbsize = strtol(optarg, NULL, 0);
1881 if (((list == 0) && (page_code == -1))
1882 || ((list != 0) && (page_code != -1))) {
1883 warnx("%s: you must specify either a page code (-m) or -l",
1889 if ((page_code != -1)
1890 && ((page_code > SMS_ALL_PAGES_PAGE)
1891 || (page_code < 0))) {
1892 warnx("%s: page code %d is out of range", __func__,
1899 page_code = SMS_ALL_PAGES_PAGE;
1901 warnx("%s: arg -P makes no sense with -l",
1906 if (subpage != -1) {
1907 warnx("%s: arg -S makes no sense with -l", __func__);
1914 pc = SMS_PAGE_CTRL_CURRENT;
1918 warnx("%s: page control value %d is out of range: 0-3",
1928 || (subpage < 0))) {
1929 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1940 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1941 "and 10", __func__, cdbsize);
1957 dataptr = (uint8_t *)malloc(datalen);
1958 if (dataptr == NULL) {
1959 warn("%s: can't allocate %d bytes", __func__, datalen);
1964 memset(dataptr, 0, datalen);
1966 ctl_scsi_mode_sense(io,
1967 /*data_ptr*/ dataptr,
1968 /*data_len*/ datalen,
1971 /*page_code*/ page_code,
1973 /*subpage*/ subpage,
1974 /*minimum_cdb_size*/ cdbsize,
1975 /*tag_type*/ CTL_TAG_SIMPLE,
1978 io->io_hdr.nexus.targ_target.id = target;
1979 io->io_hdr.nexus.targ_lun = lun;
1980 io->io_hdr.nexus.initid = id;
1982 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1987 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1988 int pages_len, used_len;
1989 uint32_t returned_len;
1992 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1993 struct scsi_mode_hdr_6 *hdr6;
1996 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1998 returned_len = hdr6->datalen + 1;
1999 bdlen = hdr6->block_descr_len;
2001 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
2003 struct scsi_mode_hdr_10 *hdr10;
2006 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
2008 returned_len = scsi_2btoul(hdr10->datalen) + 2;
2009 bdlen = scsi_2btoul(hdr10->block_descr_len);
2011 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
2013 /* just in case they can give us more than we allocated for */
2014 returned_len = min(returned_len, datalen);
2015 pages_len = returned_len - (ndataptr - dataptr);
2017 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
2018 returned_len, pages_len);
2021 fprintf(stdout, "Supported mode pages:\n");
2022 for (used_len = 0; used_len < pages_len;) {
2023 struct scsi_mode_page_header *header;
2025 header = (struct scsi_mode_page_header *)
2026 &ndataptr[used_len];
2027 fprintf(stdout, "%d\n", header->page_code);
2028 used_len += header->page_length + 2;
2031 for (used_len = 0; used_len < pages_len; used_len++) {
2032 fprintf(stdout, "0x%x ", ndataptr[used_len]);
2033 if (((used_len+1) % 16) == 0)
2034 fprintf(stdout, "\n");
2036 fprintf(stdout, "\n");
2039 ctl_io_error_print(io, NULL, stderr);
2042 ctl_scsi_free_io(io);
2044 if (dataptr != NULL)
2051 cctl_read_capacity(int fd, int target, int lun, int iid, int retries,
2052 int argc, char **argv, char *combinedopt)
2056 struct scsi_read_capacity_data *data;
2057 struct scsi_read_capacity_data_long *longdata;
2058 int cdbsize = -1, retval;
2067 io = ctl_scsi_alloc_io(id);
2069 warn("%s: can't allocate memory\n", __func__);
2073 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2076 cdbsize = strtol(optarg, NULL, 0);
2082 if (cdbsize != -1) {
2088 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
2089 "and 16", __func__, cdbsize);
2092 break; /* NOTREACHED */
2097 dataptr = (uint8_t *)malloc(sizeof(*longdata));
2098 if (dataptr == NULL) {
2099 warn("%s: can't allocate %zd bytes\n", __func__,
2104 memset(dataptr, 0, sizeof(*longdata));
2110 ctl_scsi_read_capacity(io,
2111 /*data_ptr*/ dataptr,
2112 /*data_len*/ sizeof(*longdata),
2116 /*tag_type*/ CTL_TAG_SIMPLE,
2120 ctl_scsi_read_capacity_16(io,
2121 /*data_ptr*/ dataptr,
2122 /*data_len*/ sizeof(*longdata),
2126 /*tag_type*/ CTL_TAG_SIMPLE,
2131 io->io_hdr.nexus.initid = id;
2132 io->io_hdr.nexus.targ_target.id = target;
2133 io->io_hdr.nexus.targ_lun = lun;
2135 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2140 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2144 if (cdbsize == 10) {
2146 data = (struct scsi_read_capacity_data *)dataptr;
2148 maxlba = scsi_4btoul(data->addr);
2149 blocksize = scsi_4btoul(data->length);
2151 if (maxlba == 0xffffffff) {
2156 longdata=(struct scsi_read_capacity_data_long *)dataptr;
2158 maxlba = scsi_8btou64(longdata->addr);
2159 blocksize = scsi_4btoul(longdata->length);
2162 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
2163 (uintmax_t)maxlba, blocksize);
2165 ctl_io_error_print(io, NULL, stderr);
2168 ctl_scsi_free_io(io);
2170 if (dataptr != NULL)
2177 cctl_read_write(int fd, int target, int lun, int iid, int retries,
2178 int argc, char **argv, char *combinedopt,
2179 ctladm_cmdfunction command)
2183 int file_fd, do_stdio;
2184 int cdbsize = -1, databytes;
2186 char *filename = NULL;
2187 int datalen = -1, blocksize = -1;
2199 io = ctl_scsi_alloc_io(id);
2201 warn("%s: can't allocate memory\n", __func__);
2205 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2208 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
2211 blocksize = strtoul(optarg, NULL, 0);
2214 cdbsize = strtoul(optarg, NULL, 0);
2217 datalen = strtoul(optarg, NULL, 0);
2220 filename = strdup(optarg);
2223 lba = strtoull(optarg, NULL, 0);
2230 if (filename == NULL) {
2231 warnx("%s: you must supply a filename using -f", __func__);
2236 if (datalen == -1) {
2237 warnx("%s: you must specify the data length with -d", __func__);
2243 warnx("%s: you must specify the LBA with -l", __func__);
2248 if (blocksize == -1) {
2249 warnx("%s: you must specify the blocksize with -b", __func__);
2254 if (cdbsize != -1) {
2262 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
2263 "10, 12 or 16", __func__, cdbsize);
2266 break; /* NOTREACHED */
2271 databytes = datalen * blocksize;
2272 dataptr = (uint8_t *)malloc(databytes);
2274 if (dataptr == NULL) {
2275 warn("%s: can't allocate %d bytes\n", __func__, databytes);
2279 if (strcmp(filename, "-") == 0) {
2280 if (command == CTLADM_CMD_READ)
2281 file_fd = STDOUT_FILENO;
2283 file_fd = STDIN_FILENO;
2286 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
2287 if (file_fd == -1) {
2288 warn("%s: can't open file %s", __func__, filename);
2294 memset(dataptr, 0, databytes);
2296 if (command == CTLADM_CMD_WRITE) {
2299 bytes_read = read(file_fd, dataptr, databytes);
2300 if (bytes_read == -1) {
2301 warn("%s: error reading file %s", __func__, filename);
2305 if (bytes_read != databytes) {
2306 warnx("%s: only read %d bytes from file %s",
2307 __func__, bytes_read, filename);
2312 ctl_scsi_read_write(io,
2313 /*data_ptr*/ dataptr,
2314 /*data_len*/ databytes,
2315 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
2317 /*minimum_cdb_size*/ cdbsize,
2319 /*num_blocks*/ datalen,
2320 /*tag_type*/ CTL_TAG_SIMPLE,
2323 io->io_hdr.nexus.targ_target.id = target;
2324 io->io_hdr.nexus.targ_lun = lun;
2325 io->io_hdr.nexus.initid = id;
2327 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2332 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2333 && (command == CTLADM_CMD_READ)) {
2336 bytes_written = write(file_fd, dataptr, databytes);
2337 if (bytes_written == -1) {
2338 warn("%s: can't write to %s", __func__, filename);
2341 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
2342 ctl_io_error_print(io, NULL, stderr);
2347 ctl_scsi_free_io(io);
2349 if (dataptr != NULL)
2360 cctl_get_luns(int fd, int target, int lun, int iid, int retries, struct
2361 scsi_report_luns_data **lun_data, uint32_t *num_luns)
2372 io = ctl_scsi_alloc_io(id);
2374 warnx("%s: can't allocate memory", __func__);
2379 * lun_data includes space for 1 lun, allocate space for 4 initially.
2380 * If that isn't enough, we'll allocate more.
2384 lun_datalen = sizeof(*lun_data) +
2385 (nluns * sizeof(struct scsi_report_luns_lundata));
2386 *lun_data = malloc(lun_datalen);
2388 if (*lun_data == NULL) {
2389 warnx("%s: can't allocate memory", __func__);
2390 ctl_scsi_free_io(io);
2394 ctl_scsi_report_luns(io,
2395 /*data_ptr*/ (uint8_t *)*lun_data,
2396 /*data_len*/ lun_datalen,
2397 /*select_report*/ RPL_REPORT_ALL,
2398 /*tag_type*/ CTL_TAG_SIMPLE,
2401 io->io_hdr.nexus.initid = id;
2402 io->io_hdr.nexus.targ_target.id = target;
2403 io->io_hdr.nexus.targ_lun = lun;
2405 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2410 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2411 uint32_t returned_len, returned_luns;
2413 returned_len = scsi_4btoul((*lun_data)->length);
2414 returned_luns = returned_len / 8;
2415 if (returned_luns > nluns) {
2416 nluns = returned_luns;
2420 /* These should be the same */
2421 *num_luns = MIN(returned_luns, nluns);
2423 ctl_io_error_print(io, NULL, stderr);
2427 ctl_scsi_free_io(io);
2433 cctl_report_luns(int fd, int target, int lun, int iid, int retries)
2435 struct scsi_report_luns_data *lun_data;
2436 uint32_t num_luns, i;
2441 if ((retval = cctl_get_luns(fd, target, lun, iid, retries, &lun_data,
2445 fprintf(stdout, "%u LUNs returned\n", num_luns);
2446 for (i = 0; i < num_luns; i++) {
2450 * XXX KDM figure out a way to share this code with
2453 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
2454 case RPL_LUNDATA_ATYP_PERIPH:
2455 lun_val = lun_data->luns[i].lundata[1];
2457 case RPL_LUNDATA_ATYP_FLAT:
2458 lun_val = (lun_data->luns[i].lundata[0] &
2459 RPL_LUNDATA_FLAT_LUN_MASK) |
2460 (lun_data->luns[i].lundata[1] <<
2461 RPL_LUNDATA_FLAT_LUN_BITS);
2463 case RPL_LUNDATA_ATYP_LUN:
2464 case RPL_LUNDATA_ATYP_EXTLUN:
2466 fprintf(stdout, "Unsupported LUN format %d\n",
2467 lun_data->luns[i].lundata[0] &
2468 RPL_LUNDATA_ATYP_MASK);
2475 fprintf(stdout, "%d\n", lun_val);
2479 if (lun_data != NULL)
2486 cctl_tur(int fd, int target, int lun, int iid, int retries)
2493 io = ctl_scsi_alloc_io(id);
2495 fprintf(stderr, "can't allocate memory\n");
2500 /* tag_type */ CTL_TAG_SIMPLE,
2503 io->io_hdr.nexus.targ_target.id = target;
2504 io->io_hdr.nexus.targ_lun = lun;
2505 io->io_hdr.nexus.initid = id;
2507 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2508 ctl_scsi_free_io(io);
2512 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2513 fprintf(stdout, "Unit is ready\n");
2515 ctl_io_error_print(io, NULL, stderr);
2521 cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
2522 char *path_str, int path_len,
2523 struct scsi_inquiry_data *inq_data)
2533 io = ctl_scsi_alloc_io(id);
2535 warnx("cctl_inquiry: can't allocate memory\n");
2539 ctl_scsi_inquiry(/*io*/ io,
2540 /*data_ptr*/ (uint8_t *)inq_data,
2541 /*data_len*/ sizeof(*inq_data),
2544 /*tag_type*/ CTL_TAG_SIMPLE,
2547 io->io_hdr.nexus.targ_target.id = target;
2548 io->io_hdr.nexus.targ_lun = lun;
2549 io->io_hdr.nexus.initid = id;
2551 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2556 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
2558 ctl_io_error_print(io, NULL, stderr);
2559 } else if (path_str != NULL)
2560 ctl_scsi_path_string(io, path_str, path_len);
2563 ctl_scsi_free_io(io);
2569 cctl_inquiry(int fd, int target, int lun, int iid, int retries)
2571 struct scsi_inquiry_data *inq_data;
2577 inq_data = malloc(sizeof(*inq_data));
2578 if (inq_data == NULL) {
2579 warnx("%s: can't allocate inquiry data", __func__);
2584 if ((retval = cctl_get_inquiry(fd, target, lun, iid, retries, scsi_path,
2585 sizeof(scsi_path), inq_data)) != 0)
2588 printf("%s", scsi_path);
2589 scsi_print_inquiry(inq_data);
2592 if (inq_data != NULL)
2599 cctl_req_sense(int fd, int target, int lun, int iid, int retries)
2602 struct scsi_sense_data *sense_data;
2610 io = ctl_scsi_alloc_io(id);
2612 warnx("cctl_req_sense: can't allocate memory\n");
2615 sense_data = malloc(sizeof(*sense_data));
2616 memset(sense_data, 0, sizeof(*sense_data));
2618 ctl_scsi_request_sense(/*io*/ io,
2619 /*data_ptr*/ (uint8_t *)sense_data,
2620 /*data_len*/ sizeof(*sense_data),
2622 /*tag_type*/ CTL_TAG_SIMPLE,
2625 io->io_hdr.nexus.targ_target.id = target;
2626 io->io_hdr.nexus.targ_lun = lun;
2627 io->io_hdr.nexus.initid = id;
2629 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2634 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2635 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2636 io->scsiio.sense_len = sizeof(*sense_data);
2637 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2639 ctl_io_error_print(io, NULL, stderr);
2643 ctl_scsi_free_io(io);
2650 cctl_report_target_port_group(int fd, int target, int lun, int initiator)
2662 io = ctl_scsi_alloc_io(id);
2664 warn("%s: can't allocate memory", __func__);
2669 dataptr = (uint8_t *)malloc(datalen);
2670 if (dataptr == NULL) {
2671 warn("%s: can't allocate %d bytes", __func__, datalen);
2676 memset(dataptr, 0, datalen);
2678 ctl_scsi_maintenance_in(/*io*/ io,
2679 /*data_ptr*/ dataptr,
2680 /*data_len*/ datalen,
2681 /*action*/ SA_RPRT_TRGT_GRP,
2682 /*tag_type*/ CTL_TAG_SIMPLE,
2685 io->io_hdr.nexus.targ_target.id = target;
2686 io->io_hdr.nexus.targ_lun = lun;
2687 io->io_hdr.nexus.initid = id;
2689 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2694 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2695 int returned_len, used_len;
2697 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2699 for (used_len = 0; used_len < returned_len; used_len++) {
2700 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2701 if (((used_len+1) % 8) == 0)
2702 fprintf(stdout, "\n");
2704 fprintf(stdout, "\n");
2706 ctl_io_error_print(io, NULL, stderr);
2709 ctl_scsi_free_io(io);
2711 if (dataptr != NULL)
2718 cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator)
2730 io = ctl_scsi_alloc_io(id);
2732 warn("%s: can't allocate memory", __func__);
2737 dataptr = (uint8_t *)malloc(datalen);
2738 if (dataptr == NULL) {
2739 warn("%s: can't allocate %d bytes", __func__, datalen);
2744 memset(dataptr, 0, datalen);
2746 ctl_scsi_inquiry(/*io*/ io,
2747 /*data_ptr*/ dataptr,
2748 /*data_len*/ datalen,
2750 /*page_code*/ SVPD_DEVICE_ID,
2751 /*tag_type*/ CTL_TAG_SIMPLE,
2754 io->io_hdr.nexus.targ_target.id = target;
2755 io->io_hdr.nexus.targ_lun = lun;
2756 io->io_hdr.nexus.initid = id;
2758 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2763 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2764 int returned_len, used_len;
2766 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2768 for (used_len = 0; used_len < returned_len; used_len++) {
2769 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2770 if (((used_len+1) % 8) == 0)
2771 fprintf(stdout, "\n");
2773 fprintf(stdout, "\n");
2775 ctl_io_error_print(io, NULL, stderr);
2778 ctl_scsi_free_io(io);
2780 if (dataptr != NULL)
2787 cctl_persistent_reserve_in(int fd, int target, int lun, int initiator,
2788 int argc, char **argv, char *combinedopt,
2803 io = ctl_scsi_alloc_io(id);
2805 warn("%s: can't allocate memory", __func__);
2809 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2812 action = strtol(optarg, NULL, 0);
2819 if (action < 0 || action > 2) {
2820 warn("action must be specified and in the range: 0-2");
2827 dataptr = (uint8_t *)malloc(datalen);
2828 if (dataptr == NULL) {
2829 warn("%s: can't allocate %d bytes", __func__, datalen);
2834 memset(dataptr, 0, datalen);
2836 ctl_scsi_persistent_res_in(io,
2837 /*data_ptr*/ dataptr,
2838 /*data_len*/ datalen,
2840 /*tag_type*/ CTL_TAG_SIMPLE,
2843 io->io_hdr.nexus.targ_target.id = target;
2844 io->io_hdr.nexus.targ_lun = lun;
2845 io->io_hdr.nexus.initid = id;
2847 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2852 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2853 int returned_len, used_len;
2859 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2860 returned_len = min(returned_len, 256);
2863 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2869 warnx("%s: invalid action %d", __func__, action);
2871 break; /* NOTREACHED */
2874 for (used_len = 0; used_len < returned_len; used_len++) {
2875 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2876 if (((used_len+1) % 8) == 0)
2877 fprintf(stdout, "\n");
2879 fprintf(stdout, "\n");
2881 ctl_io_error_print(io, NULL, stderr);
2884 ctl_scsi_free_io(io);
2886 if (dataptr != NULL)
2893 cctl_persistent_reserve_out(int fd, int target, int lun, int initiator,
2894 int argc, char **argv, char *combinedopt,
2900 uint64_t key = 0, sa_key = 0;
2901 int action = -1, restype = -1;
2910 io = ctl_scsi_alloc_io(id);
2912 warn("%s: can't allocate memory", __func__);
2916 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2919 action = strtol(optarg, NULL, 0);
2922 key = strtoull(optarg, NULL, 0);
2925 restype = strtol(optarg, NULL, 0);
2928 sa_key = strtoull(optarg, NULL, 0);
2934 if (action < 0 || action > 5) {
2935 warn("action must be specified and in the range: 0-5");
2940 if (restype < 0 || restype > 5) {
2941 if (action != 0 && action != 5 && action != 3) {
2942 warn("'restype' must specified and in the range: 0-5");
2949 dataptr = (uint8_t *)malloc(datalen);
2950 if (dataptr == NULL) {
2951 warn("%s: can't allocate %d bytes", __func__, datalen);
2956 memset(dataptr, 0, datalen);
2958 ctl_scsi_persistent_res_out(io,
2959 /*data_ptr*/ dataptr,
2960 /*data_len*/ datalen,
2965 /*tag_type*/ CTL_TAG_SIMPLE,
2968 io->io_hdr.nexus.targ_target.id = target;
2969 io->io_hdr.nexus.targ_lun = lun;
2970 io->io_hdr.nexus.initid = id;
2972 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2976 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2978 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2979 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2980 "successfully\n", scsi_path);
2982 ctl_io_error_print(io, NULL, stderr);
2985 ctl_scsi_free_io(io);
2987 if (dataptr != NULL)
2993 struct cctl_req_option {
2998 STAILQ_ENTRY(cctl_req_option) links;
3002 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
3004 struct ctl_lun_req req;
3005 int device_type = -1;
3006 uint64_t lun_size = 0;
3007 uint32_t blocksize = 0, req_lun_id = 0;
3008 char *serial_num = NULL;
3009 char *device_id = NULL;
3010 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
3011 char *backend_name = NULL;
3012 STAILQ_HEAD(, cctl_req_option) option_list;
3013 int num_options = 0;
3016 STAILQ_INIT(&option_list);
3018 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3021 backend_name = strdup(optarg);
3024 blocksize = strtoul(optarg, NULL, 0);
3028 device_id = strdup(optarg);
3031 req_lun_id = strtoul(optarg, NULL, 0);
3035 struct cctl_req_option *option;
3039 tmpstr = strdup(optarg);
3040 name = strsep(&tmpstr, "=");
3042 warnx("%s: option -o takes \"name=value\""
3043 "argument", __func__);
3047 value = strsep(&tmpstr, "=");
3048 if (value == NULL) {
3049 warnx("%s: option -o takes \"name=value\""
3050 "argument", __func__);
3054 option = malloc(sizeof(*option));
3055 if (option == NULL) {
3056 warn("%s: error allocating %zd bytes",
3057 __func__, sizeof(*option));
3061 option->name = strdup(name);
3062 option->namelen = strlen(name) + 1;
3063 option->value = strdup(value);
3064 option->vallen = strlen(value) + 1;
3067 STAILQ_INSERT_TAIL(&option_list, option, links);
3072 if (strcasecmp(optarg, "auto") != 0) {
3073 retval = expand_number(optarg, &lun_size);
3075 warn("%s: invalid -s argument",
3084 serial_num = strdup(optarg);
3087 device_type = strtoul(optarg, NULL, 0);
3094 if (backend_name == NULL) {
3095 warnx("%s: backend name (-b) must be specified", __func__);
3100 bzero(&req, sizeof(req));
3102 strlcpy(req.backend, backend_name, sizeof(req.backend));
3103 req.reqtype = CTL_LUNREQ_CREATE;
3105 if (blocksize_set != 0)
3106 req.reqdata.create.blocksize_bytes = blocksize;
3108 if (lun_size_set != 0)
3109 req.reqdata.create.lun_size_bytes = lun_size;
3111 if (lun_id_set != 0) {
3112 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
3113 req.reqdata.create.req_lun_id = req_lun_id;
3116 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
3118 if (device_type != -1)
3119 req.reqdata.create.device_type = device_type;
3121 req.reqdata.create.device_type = T_DIRECT;
3123 if (serial_num != NULL) {
3124 strlcpy(req.reqdata.create.serial_num, serial_num,
3125 sizeof(req.reqdata.create.serial_num));
3126 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
3129 if (device_id != NULL) {
3130 strlcpy(req.reqdata.create.device_id, device_id,
3131 sizeof(req.reqdata.create.device_id));
3132 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
3135 req.num_be_args = num_options;
3136 if (num_options > 0) {
3137 struct cctl_req_option *option, *next_option;
3140 req.be_args = malloc(num_options * sizeof(*req.be_args));
3141 if (req.be_args == NULL) {
3142 warn("%s: error allocating %zd bytes", __func__,
3143 num_options * sizeof(*req.be_args));
3148 for (i = 0, option = STAILQ_FIRST(&option_list);
3149 i < num_options; i++, option = next_option) {
3150 next_option = STAILQ_NEXT(option, links);
3152 req.be_args[i].namelen = option->namelen;
3153 req.be_args[i].name = strdup(option->name);
3154 req.be_args[i].vallen = option->vallen;
3155 req.be_args[i].value = strdup(option->value);
3157 * XXX KDM do we want a way to specify a writeable
3158 * flag of some sort? Do we want a way to specify
3161 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3163 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3166 free(option->value);
3171 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3172 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3177 switch (req.status) {
3179 warnx("LUN creation error: %s", req.error_str);
3182 case CTL_LUN_WARNING:
3183 warnx("LUN creation warning: %s", req.error_str);
3188 warnx("unknown LUN creation status: %d", req.status);
3193 fprintf(stdout, "LUN created successfully\n");
3194 fprintf(stdout, "backend: %s\n", req.backend);
3195 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
3196 fprintf(stdout, "LUN size: %ju bytes\n",
3197 (uintmax_t)req.reqdata.create.lun_size_bytes);
3198 fprintf(stdout, "blocksize %u bytes\n",
3199 req.reqdata.create.blocksize_bytes);
3200 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
3201 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
3202 fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
3209 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
3211 struct ctl_lun_req req;
3212 uint32_t lun_id = 0;
3214 char *backend_name = NULL;
3215 STAILQ_HEAD(, cctl_req_option) option_list;
3216 int num_options = 0;
3219 STAILQ_INIT(&option_list);
3221 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3224 backend_name = strdup(optarg);
3227 lun_id = strtoul(optarg, NULL, 0);
3231 struct cctl_req_option *option;
3235 tmpstr = strdup(optarg);
3236 name = strsep(&tmpstr, "=");
3238 warnx("%s: option -o takes \"name=value\""
3239 "argument", __func__);
3243 value = strsep(&tmpstr, "=");
3244 if (value == NULL) {
3245 warnx("%s: option -o takes \"name=value\""
3246 "argument", __func__);
3250 option = malloc(sizeof(*option));
3251 if (option == NULL) {
3252 warn("%s: error allocating %zd bytes",
3253 __func__, sizeof(*option));
3257 option->name = strdup(name);
3258 option->namelen = strlen(name) + 1;
3259 option->value = strdup(value);
3260 option->vallen = strlen(value) + 1;
3263 STAILQ_INSERT_TAIL(&option_list, option, links);
3272 if (backend_name == NULL)
3273 errx(1, "%s: backend name (-b) must be specified", __func__);
3275 if (lun_id_set == 0)
3276 errx(1, "%s: LUN id (-l) must be specified", __func__);
3278 bzero(&req, sizeof(req));
3280 strlcpy(req.backend, backend_name, sizeof(req.backend));
3281 req.reqtype = CTL_LUNREQ_RM;
3283 req.reqdata.rm.lun_id = lun_id;
3285 req.num_be_args = num_options;
3286 if (num_options > 0) {
3287 struct cctl_req_option *option, *next_option;
3290 req.be_args = malloc(num_options * sizeof(*req.be_args));
3291 if (req.be_args == NULL) {
3292 warn("%s: error allocating %zd bytes", __func__,
3293 num_options * sizeof(*req.be_args));
3298 for (i = 0, option = STAILQ_FIRST(&option_list);
3299 i < num_options; i++, option = next_option) {
3300 next_option = STAILQ_NEXT(option, links);
3302 req.be_args[i].namelen = option->namelen;
3303 req.be_args[i].name = strdup(option->name);
3304 req.be_args[i].vallen = option->vallen;
3305 req.be_args[i].value = strdup(option->value);
3307 * XXX KDM do we want a way to specify a writeable
3308 * flag of some sort? Do we want a way to specify
3311 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3313 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3316 free(option->value);
3321 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3322 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3327 switch (req.status) {
3329 warnx("LUN removal error: %s", req.error_str);
3332 case CTL_LUN_WARNING:
3333 warnx("LUN removal warning: %s", req.error_str);
3338 warnx("unknown LUN removal status: %d", req.status);
3343 printf("LUN %d removed successfully\n", lun_id);
3350 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
3352 struct ctl_lun_req req;
3353 uint64_t lun_size = 0;
3354 uint32_t lun_id = 0;
3355 int lun_id_set = 0, lun_size_set = 0;
3356 char *backend_name = NULL;
3359 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3362 backend_name = strdup(optarg);
3365 lun_id = strtoul(optarg, NULL, 0);
3369 if (strcasecmp(optarg, "auto") != 0) {
3370 retval = expand_number(optarg, &lun_size);
3372 warn("%s: invalid -s argument",
3385 if (backend_name == NULL)
3386 errx(1, "%s: backend name (-b) must be specified", __func__);
3388 if (lun_id_set == 0)
3389 errx(1, "%s: LUN id (-l) must be specified", __func__);
3391 if (lun_size_set == 0)
3392 errx(1, "%s: size (-s) must be specified", __func__);
3394 bzero(&req, sizeof(req));
3396 strlcpy(req.backend, backend_name, sizeof(req.backend));
3397 req.reqtype = CTL_LUNREQ_MODIFY;
3399 req.reqdata.modify.lun_id = lun_id;
3400 req.reqdata.modify.lun_size_bytes = lun_size;
3402 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3403 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3408 switch (req.status) {
3410 warnx("LUN modification error: %s", req.error_str);
3413 case CTL_LUN_WARNING:
3414 warnx("LUN modification warning: %s", req.error_str);
3419 warnx("unknown LUN modification status: %d", req.status);
3424 printf("LUN %d modified successfully\n", lun_id);
3430 struct cctl_islist_conn {
3433 char *initiator_addr;
3434 char *initiator_alias;
3437 char *header_digest;
3439 char *max_data_segment_length;;
3442 STAILQ_ENTRY(cctl_islist_conn) links;
3445 struct cctl_islist_data {
3447 STAILQ_HEAD(,cctl_islist_conn) conn_list;
3448 struct cctl_islist_conn *cur_conn;
3450 struct sbuf *cur_sb[32];
3454 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
3457 struct cctl_islist_data *islist;
3458 struct cctl_islist_conn *cur_conn;
3460 islist = (struct cctl_islist_data *)user_data;
3461 cur_conn = islist->cur_conn;
3463 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
3464 sizeof(islist->cur_sb[0])))
3465 errx(1, "%s: too many nesting levels, %zd max", __func__,
3466 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
3468 islist->cur_sb[islist->level] = sbuf_new_auto();
3469 if (islist->cur_sb[islist->level] == NULL)
3470 err(1, "%s: Unable to allocate sbuf", __func__);
3472 if (strcmp(name, "connection") == 0) {
3473 if (cur_conn != NULL)
3474 errx(1, "%s: improper connection element nesting",
3477 cur_conn = calloc(1, sizeof(*cur_conn));
3478 if (cur_conn == NULL)
3479 err(1, "%s: cannot allocate %zd bytes", __func__,
3482 islist->num_conns++;
3483 islist->cur_conn = cur_conn;
3485 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
3487 for (i = 0; attr[i] != NULL; i += 2) {
3488 if (strcmp(attr[i], "id") == 0) {
3489 cur_conn->connection_id =
3490 strtoull(attr[i+1], NULL, 0);
3493 "%s: invalid connection attribute %s = %s",
3494 __func__, attr[i], attr[i+1]);
3501 cctl_islist_end_element(void *user_data, const char *name)
3503 struct cctl_islist_data *islist;
3504 struct cctl_islist_conn *cur_conn;
3507 islist = (struct cctl_islist_data *)user_data;
3508 cur_conn = islist->cur_conn;
3510 if ((cur_conn == NULL)
3511 && (strcmp(name, "ctlislist") != 0))
3512 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
3514 if (islist->cur_sb[islist->level] == NULL)
3515 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3516 islist->level, name);
3518 sbuf_finish(islist->cur_sb[islist->level]);
3519 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
3521 err(1, "%s can't allocate %zd bytes for string", __func__,
3522 sbuf_len(islist->cur_sb[islist->level]));
3524 sbuf_delete(islist->cur_sb[islist->level]);
3525 islist->cur_sb[islist->level] = NULL;
3528 if (strcmp(name, "initiator") == 0) {
3529 cur_conn->initiator = str;
3531 } else if (strcmp(name, "initiator_addr") == 0) {
3532 cur_conn->initiator_addr = str;
3534 } else if (strcmp(name, "initiator_alias") == 0) {
3535 cur_conn->initiator_alias = str;
3537 } else if (strcmp(name, "target") == 0) {
3538 cur_conn->target = str;
3540 } else if (strcmp(name, "target_alias") == 0) {
3541 cur_conn->target_alias = str;
3543 } else if (strcmp(name, "header_digest") == 0) {
3544 cur_conn->header_digest = str;
3546 } else if (strcmp(name, "data_digest") == 0) {
3547 cur_conn->data_digest = str;
3549 } else if (strcmp(name, "max_data_segment_length") == 0) {
3550 cur_conn->max_data_segment_length = str;
3552 } else if (strcmp(name, "immediate_data") == 0) {
3553 cur_conn->immediate_data = atoi(str);
3554 } else if (strcmp(name, "iser") == 0) {
3555 cur_conn->iser = atoi(str);
3556 } else if (strcmp(name, "connection") == 0) {
3557 islist->cur_conn = NULL;
3558 } else if (strcmp(name, "ctlislist") == 0) {
3560 errx(1, "unknown element %s", name);
3566 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
3568 struct cctl_islist_data *islist;
3570 islist = (struct cctl_islist_data *)user_data;
3572 sbuf_bcat(islist->cur_sb[islist->level], str, len);
3576 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
3578 struct ctl_iscsi req;
3579 struct cctl_islist_data islist;
3580 struct cctl_islist_conn *conn;
3585 int c, retval, verbose = 0;
3590 bzero(&islist, sizeof(islist));
3591 STAILQ_INIT(&islist.conn_list);
3593 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3607 conn_str = malloc(conn_len);
3609 bzero(&req, sizeof(req));
3610 req.type = CTL_ISCSI_LIST;
3611 req.data.list.alloc_len = conn_len;
3612 req.data.list.conn_xml = conn_str;
3614 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3615 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3620 if (req.status == CTL_ISCSI_ERROR) {
3621 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
3622 __func__, req.error_str);
3623 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
3624 conn_len = conn_len << 1;
3628 if (dump_xml != 0) {
3629 printf("%s", conn_str);
3633 parser = XML_ParserCreate(NULL);
3634 if (parser == NULL) {
3635 warn("%s: Unable to create XML parser", __func__);
3640 XML_SetUserData(parser, &islist);
3641 XML_SetElementHandler(parser, cctl_islist_start_element,
3642 cctl_islist_end_element);
3643 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3645 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3646 XML_ParserFree(parser);
3653 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3654 printf("Session ID: %d\n", conn->connection_id);
3655 printf("Initiator name: %s\n", conn->initiator);
3656 printf("Initiator portal: %s\n", conn->initiator_addr);
3657 printf("Initiator alias: %s\n", conn->initiator_alias);
3658 printf("Target name: %s\n", conn->target);
3659 printf("Target alias: %s\n", conn->target_alias);
3660 printf("Header digest: %s\n", conn->header_digest);
3661 printf("Data digest: %s\n", conn->data_digest);
3662 printf("DataSegmentLen: %s\n", conn->max_data_segment_length);
3663 printf("ImmediateData: %s\n", conn->immediate_data ? "Yes" : "No");
3664 printf("iSER (RDMA): %s\n", conn->iser ? "Yes" : "No");
3668 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3670 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3671 printf("%4u %-16s %-36s %-36s\n",
3672 conn->connection_id, conn->initiator_addr, conn->initiator,
3683 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3685 struct ctl_iscsi req;
3687 int all = 0, connection_id = -1, nargs = 0;
3688 char *initiator_name = NULL, *initiator_addr = NULL;
3690 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3697 connection_id = strtoul(optarg, NULL, 0);
3701 initiator_name = strdup(optarg);
3702 if (initiator_name == NULL)
3703 err(1, "%s: strdup", __func__);
3707 initiator_addr = strdup(optarg);
3708 if (initiator_addr == NULL)
3709 err(1, "%s: strdup", __func__);
3718 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3721 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3724 bzero(&req, sizeof(req));
3725 req.type = CTL_ISCSI_LOGOUT;
3726 req.data.logout.connection_id = connection_id;
3727 if (initiator_addr != NULL)
3728 strlcpy(req.data.logout.initiator_addr,
3729 initiator_addr, sizeof(req.data.logout.initiator_addr));
3730 if (initiator_name != NULL)
3731 strlcpy(req.data.logout.initiator_name,
3732 initiator_name, sizeof(req.data.logout.initiator_name));
3734 req.data.logout.all = 1;
3736 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3737 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3742 if (req.status != CTL_ISCSI_OK) {
3743 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3744 __func__, req.error_str);
3749 printf("iSCSI logout requests submitted\n");
3756 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3758 struct ctl_iscsi req;
3760 int all = 0, connection_id = -1, nargs = 0;
3761 char *initiator_name = NULL, *initiator_addr = NULL;
3763 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3770 connection_id = strtoul(optarg, NULL, 0);
3774 initiator_name = strdup(optarg);
3775 if (initiator_name == NULL)
3776 err(1, "%s: strdup", __func__);
3780 initiator_addr = strdup(optarg);
3781 if (initiator_addr == NULL)
3782 err(1, "%s: strdup", __func__);
3791 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3794 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3797 bzero(&req, sizeof(req));
3798 req.type = CTL_ISCSI_TERMINATE;
3799 req.data.terminate.connection_id = connection_id;
3800 if (initiator_addr != NULL)
3801 strlcpy(req.data.terminate.initiator_addr,
3802 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3803 if (initiator_name != NULL)
3804 strlcpy(req.data.terminate.initiator_name,
3805 initiator_name, sizeof(req.data.terminate.initiator_name));
3807 req.data.terminate.all = 1;
3809 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3810 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3815 if (req.status != CTL_ISCSI_OK) {
3816 warnx("%s: error returned from CTL iSCSI connection "
3817 "termination request:\n%s", __func__, req.error_str);
3822 printf("iSCSI connections terminated\n");
3829 * Name/value pair used for per-LUN attributes.
3831 struct cctl_lun_nv {
3834 STAILQ_ENTRY(cctl_lun_nv) links;
3838 * Backend LUN information.
3843 uint64_t size_blocks;
3845 char *serial_number;
3847 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3848 STAILQ_ENTRY(cctl_lun) links;
3851 struct cctl_devlist_data {
3853 STAILQ_HEAD(,cctl_lun) lun_list;
3854 struct cctl_lun *cur_lun;
3856 struct sbuf *cur_sb[32];
3860 cctl_start_element(void *user_data, const char *name, const char **attr)
3863 struct cctl_devlist_data *devlist;
3864 struct cctl_lun *cur_lun;
3866 devlist = (struct cctl_devlist_data *)user_data;
3867 cur_lun = devlist->cur_lun;
3869 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3870 sizeof(devlist->cur_sb[0])))
3871 errx(1, "%s: too many nesting levels, %zd max", __func__,
3872 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3874 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3875 if (devlist->cur_sb[devlist->level] == NULL)
3876 err(1, "%s: Unable to allocate sbuf", __func__);
3878 if (strcmp(name, "lun") == 0) {
3879 if (cur_lun != NULL)
3880 errx(1, "%s: improper lun element nesting", __func__);
3882 cur_lun = calloc(1, sizeof(*cur_lun));
3883 if (cur_lun == NULL)
3884 err(1, "%s: cannot allocate %zd bytes", __func__,
3887 devlist->num_luns++;
3888 devlist->cur_lun = cur_lun;
3890 STAILQ_INIT(&cur_lun->attr_list);
3891 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3893 for (i = 0; attr[i] != NULL; i += 2) {
3894 if (strcmp(attr[i], "id") == 0) {
3895 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3897 errx(1, "%s: invalid LUN attribute %s = %s",
3898 __func__, attr[i], attr[i+1]);
3905 cctl_end_element(void *user_data, const char *name)
3907 struct cctl_devlist_data *devlist;
3908 struct cctl_lun *cur_lun;
3911 devlist = (struct cctl_devlist_data *)user_data;
3912 cur_lun = devlist->cur_lun;
3914 if ((cur_lun == NULL)
3915 && (strcmp(name, "ctllunlist") != 0))
3916 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3918 if (devlist->cur_sb[devlist->level] == NULL)
3919 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3920 devlist->level, name);
3922 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3923 err(1, "%s: sbuf_finish", __func__);
3924 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3926 err(1, "%s can't allocate %zd bytes for string", __func__,
3927 sbuf_len(devlist->cur_sb[devlist->level]));
3929 if (strlen(str) == 0) {
3934 sbuf_delete(devlist->cur_sb[devlist->level]);
3935 devlist->cur_sb[devlist->level] = NULL;
3938 if (strcmp(name, "backend_type") == 0) {
3939 cur_lun->backend_type = str;
3941 } else if (strcmp(name, "size") == 0) {
3942 cur_lun->size_blocks = strtoull(str, NULL, 0);
3943 } else if (strcmp(name, "blocksize") == 0) {
3944 cur_lun->blocksize = strtoul(str, NULL, 0);
3945 } else if (strcmp(name, "serial_number") == 0) {
3946 cur_lun->serial_number = str;
3948 } else if (strcmp(name, "device_id") == 0) {
3949 cur_lun->device_id = str;
3951 } else if (strcmp(name, "lun") == 0) {
3952 devlist->cur_lun = NULL;
3953 } else if (strcmp(name, "ctllunlist") == 0) {
3956 struct cctl_lun_nv *nv;
3958 nv = calloc(1, sizeof(*nv));
3960 err(1, "%s: can't allocate %zd bytes for nv pair",
3961 __func__, sizeof(*nv));
3963 nv->name = strdup(name);
3964 if (nv->name == NULL)
3965 err(1, "%s: can't allocated %zd bytes for string",
3966 __func__, strlen(name));
3970 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3977 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3979 struct cctl_devlist_data *devlist;
3981 devlist = (struct cctl_devlist_data *)user_data;
3983 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3987 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3989 struct ctl_lun_list list;
3990 struct cctl_devlist_data devlist;
3991 struct cctl_lun *lun;
3997 char *backend = NULL;
4003 bzero(&devlist, sizeof(devlist));
4004 STAILQ_INIT(&devlist.lun_list);
4006 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4009 backend = strdup(optarg);
4023 lun_str = malloc(lun_len);
4025 bzero(&list, sizeof(list));
4026 list.alloc_len = lun_len;
4027 list.status = CTL_LUN_LIST_NONE;
4028 list.lun_xml = lun_str;
4030 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
4031 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
4036 if (list.status == CTL_LUN_LIST_ERROR) {
4037 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
4038 __func__, list.error_str);
4039 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
4040 lun_len = lun_len << 1;
4044 if (dump_xml != 0) {
4045 printf("%s", lun_str);
4049 parser = XML_ParserCreate(NULL);
4050 if (parser == NULL) {
4051 warn("%s: Unable to create XML parser", __func__);
4056 XML_SetUserData(parser, &devlist);
4057 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
4058 XML_SetCharacterDataHandler(parser, cctl_char_handler);
4060 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
4061 XML_ParserFree(parser);
4067 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
4068 "Serial Number", "Device ID");
4069 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
4070 struct cctl_lun_nv *nv;
4072 if ((backend != NULL)
4073 && (strcmp(lun->backend_type, backend) != 0))
4076 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
4077 (uintmax_t)lun->lun_id,
4078 lun->backend_type, (uintmax_t)lun->size_blocks,
4079 lun->blocksize, lun->serial_number, lun->device_id);
4084 STAILQ_FOREACH(nv, &lun->attr_list, links) {
4085 printf(" %s=%s\n", nv->name, nv->value);
4100 char *frontend_type;
4103 char *target, *port;
4104 STAILQ_HEAD(,cctl_lun_nv) init_list;
4105 STAILQ_HEAD(,cctl_lun_nv) attr_list;
4106 STAILQ_ENTRY(cctl_port) links;
4109 struct cctl_portlist_data {
4111 STAILQ_HEAD(,cctl_port) port_list;
4112 struct cctl_port *cur_port;
4114 struct sbuf *cur_sb[32];
4118 cctl_start_pelement(void *user_data, const char *name, const char **attr)
4121 struct cctl_portlist_data *portlist;
4122 struct cctl_port *cur_port;
4124 portlist = (struct cctl_portlist_data *)user_data;
4125 cur_port = portlist->cur_port;
4127 if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
4128 sizeof(portlist->cur_sb[0])))
4129 errx(1, "%s: too many nesting levels, %zd max", __func__,
4130 sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
4132 portlist->cur_sb[portlist->level] = sbuf_new_auto();
4133 if (portlist->cur_sb[portlist->level] == NULL)
4134 err(1, "%s: Unable to allocate sbuf", __func__);
4136 if (strcmp(name, "targ_port") == 0) {
4137 if (cur_port != NULL)
4138 errx(1, "%s: improper port element nesting", __func__);
4140 cur_port = calloc(1, sizeof(*cur_port));
4141 if (cur_port == NULL)
4142 err(1, "%s: cannot allocate %zd bytes", __func__,
4145 portlist->num_ports++;
4146 portlist->cur_port = cur_port;
4148 STAILQ_INIT(&cur_port->init_list);
4149 STAILQ_INIT(&cur_port->attr_list);
4150 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
4152 for (i = 0; attr[i] != NULL; i += 2) {
4153 if (strcmp(attr[i], "id") == 0) {
4154 cur_port->port_id = strtoull(attr[i+1], NULL, 0);
4156 errx(1, "%s: invalid LUN attribute %s = %s",
4157 __func__, attr[i], attr[i+1]);
4164 cctl_end_pelement(void *user_data, const char *name)
4166 struct cctl_portlist_data *portlist;
4167 struct cctl_port *cur_port;
4170 portlist = (struct cctl_portlist_data *)user_data;
4171 cur_port = portlist->cur_port;
4173 if ((cur_port == NULL)
4174 && (strcmp(name, "ctlportlist") != 0))
4175 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
4177 if (portlist->cur_sb[portlist->level] == NULL)
4178 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
4179 portlist->level, name);
4181 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
4182 err(1, "%s: sbuf_finish", __func__);
4183 str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
4185 err(1, "%s can't allocate %zd bytes for string", __func__,
4186 sbuf_len(portlist->cur_sb[portlist->level]));
4188 if (strlen(str) == 0) {
4193 sbuf_delete(portlist->cur_sb[portlist->level]);
4194 portlist->cur_sb[portlist->level] = NULL;
4197 if (strcmp(name, "frontend_type") == 0) {
4198 cur_port->frontend_type = str;
4200 } else if (strcmp(name, "port_name") == 0) {
4201 cur_port->name = str;
4203 } else if (strcmp(name, "online") == 0) {
4204 cur_port->online = str;
4206 } else if (strcmp(name, "physical_port") == 0) {
4207 cur_port->pp = strtoull(str, NULL, 0);
4208 } else if (strcmp(name, "virtual_port") == 0) {
4209 cur_port->vp = strtoull(str, NULL, 0);
4210 } else if (strcmp(name, "target") == 0) {
4211 cur_port->target = str;
4213 } else if (strcmp(name, "port") == 0) {
4214 cur_port->port = str;
4216 } else if (strcmp(name, "targ_port") == 0) {
4217 portlist->cur_port = NULL;
4218 } else if (strcmp(name, "ctlportlist") == 0) {
4221 struct cctl_lun_nv *nv;
4223 nv = calloc(1, sizeof(*nv));
4225 err(1, "%s: can't allocate %zd bytes for nv pair",
4226 __func__, sizeof(*nv));
4228 nv->name = strdup(name);
4229 if (nv->name == NULL)
4230 err(1, "%s: can't allocated %zd bytes for string",
4231 __func__, strlen(name));
4235 if (strcmp(name, "initiator") == 0)
4236 STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links);
4238 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
4245 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
4247 struct cctl_portlist_data *portlist;
4249 portlist = (struct cctl_portlist_data *)user_data;
4251 sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
4255 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
4257 struct ctl_lun_list list;
4258 struct cctl_portlist_data portlist;
4259 struct cctl_port *port;
4265 char *frontend = NULL;
4266 uint64_t portarg = UINT64_MAX;
4267 int verbose = 0, init = 0, quiet = 0;
4272 bzero(&portlist, sizeof(portlist));
4273 STAILQ_INIT(&portlist.port_list);
4275 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4278 frontend = strdup(optarg);
4284 portarg = strtoll(optarg, NULL, 0);
4301 port_str = malloc(port_len);
4303 bzero(&list, sizeof(list));
4304 list.alloc_len = port_len;
4305 list.status = CTL_LUN_LIST_NONE;
4306 list.lun_xml = port_str;
4308 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
4309 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
4314 if (list.status == CTL_LUN_LIST_ERROR) {
4315 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
4316 __func__, list.error_str);
4317 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
4318 port_len = port_len << 1;
4322 if (dump_xml != 0) {
4323 printf("%s", port_str);
4327 parser = XML_ParserCreate(NULL);
4328 if (parser == NULL) {
4329 warn("%s: Unable to create XML parser", __func__);
4334 XML_SetUserData(parser, &portlist);
4335 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
4336 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
4338 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
4339 XML_ParserFree(parser);
4346 printf("Port Online Frontend Name pp vp\n");
4347 STAILQ_FOREACH(port, &portlist.port_list, links) {
4348 struct cctl_lun_nv *nv;
4350 if ((frontend != NULL)
4351 && (strcmp(port->frontend_type, frontend) != 0))
4354 if ((portarg != UINT64_MAX) && (portarg != port->port_id))
4357 printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n",
4358 (uintmax_t)port->port_id, port->online,
4359 port->frontend_type, port->name, port->pp, port->vp,
4360 port->port ? port->port : "");
4362 if (init || verbose) {
4364 printf(" Target: %s\n", port->target);
4365 STAILQ_FOREACH(nv, &port->init_list, links) {
4366 printf(" Initiator: %s\n", nv->value);
4371 STAILQ_FOREACH(nv, &port->attr_list, links) {
4372 printf(" %s=%s\n", nv->name, nv->value);
4385 fprintf(error ? stderr : stdout,
4387 "Primary commands:\n"
4388 " ctladm tur [dev_id][general options]\n"
4389 " ctladm inquiry [dev_id][general options]\n"
4390 " ctladm devid [dev_id][general options]\n"
4391 " ctladm reqsense [dev_id][general options]\n"
4392 " ctladm reportluns [dev_id][general options]\n"
4393 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
4394 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4395 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
4396 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4397 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
4398 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
4399 " [-d] [-S subpage] [-c cdbsize]\n"
4400 " ctladm prin [dev_id][general options] <-a action>\n"
4401 " ctladm prout [dev_id][general options] <-a action>\n"
4402 " <-r restype] [-k key] [-s sa_key]\n"
4403 " ctladm rtpg [dev_id][general options]\n"
4404 " ctladm start [dev_id][general options] [-i] [-o]\n"
4405 " ctladm stop [dev_id][general options] [-i] [-o]\n"
4406 " ctladm synccache [dev_id][general options] [-l lba]\n"
4407 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
4408 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
4409 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
4410 " [-S serial_num] [-t dev_type]\n"
4411 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
4412 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
4413 " ctladm devlist [-b backend] [-v] [-x]\n"
4414 " ctladm shutdown\n"
4416 " ctladm hardstop\n"
4417 " ctladm hardstart\n"
4419 " ctladm bbrread [dev_id] <-l lba> <-d datalen>\n"
4420 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
4422 " ctladm realsync <on|off|query>\n"
4423 " ctladm setsync [dev_id] <-i interval>\n"
4424 " ctladm getsync [dev_id]\n"
4425 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
4426 " [-s len fmt [args]] [-c] [-d delete_id]\n"
4427 " ctladm port <-l | -o <on|off> | [-w wwnn][-W wwpn]>\n"
4428 " [-p targ_port] [-t port_type] [-q] [-x]\n"
4429 " ctladm portlist [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n"
4430 " ctladm islist [-v | -x]\n"
4431 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
4432 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
4434 " ctladm dumpstructs\n"
4436 "General Options:\n"
4437 "-I intiator_id : defaults to 7, used to change the initiator id\n"
4438 "-C retries : specify the number of times to retry this command\n"
4439 "-D devicename : specify the device to operate on\n"
4440 " : (default is %s)\n"
4441 "read/write options:\n"
4442 "-l lba : logical block address\n"
4443 "-d len : read/write length, in blocks\n"
4444 "-f file|- : write/read data to/from file or stdout/stdin\n"
4445 "-b blocksize : block size, in bytes\n"
4446 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
4447 "-N : do not copy data to/from userland\n"
4448 "readcapacity options:\n"
4449 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4450 "modesense options:\n"
4451 "-m page : specify the mode page to view\n"
4452 "-l : request a list of supported pages\n"
4453 "-P pc : specify the page control value: 0-3 (current,\n"
4454 " changeable, default, saved, respectively)\n"
4455 "-d : disable block descriptors for mode sense\n"
4456 "-S subpage : specify a subpage\n"
4457 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
4458 "persistent reserve in options:\n"
4459 "-a action : specify the action value: 0-2 (read key, read\n"
4460 " reservation, read capabilities, respectively)\n"
4461 "persistent reserve out options:\n"
4462 "-a action : specify the action value: 0-5 (register, reserve,\n"
4463 " release, clear, preempt, register and ignore)\n"
4464 "-k key : key value\n"
4465 "-s sa_key : service action value\n"
4466 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
4467 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
4468 "start/stop options:\n"
4469 "-i : set the immediate bit (CTL does not support this)\n"
4470 "-o : set the on/offline bit\n"
4471 "synccache options:\n"
4472 "-l lba : set the starting LBA\n"
4473 "-b blockcount : set the length to sync in blocks\n"
4474 "-r : set the relative addressing bit\n"
4475 "-i : set the immediate bit\n"
4476 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4478 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4479 "-B blocksize : LUN blocksize in bytes (some backends)\n"
4480 "-d device_id : SCSI VPD page 0x83 ID\n"
4481 "-l lun_id : requested LUN number\n"
4482 "-o name=value : backend-specific options, multiple allowed\n"
4483 "-s size_bytes : LUN size in bytes (some backends)\n"
4484 "-S serial_num : SCSI VPD page 0x80 serial number\n"
4485 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
4487 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4488 "-l lun_id : LUN number to delete\n"
4489 "-o name=value : backend-specific options, multiple allowed\n"
4490 "devlist options:\n"
4491 "-b backend : list devices from specified backend only\n"
4492 "-v : be verbose, show backend attributes\n"
4493 "-x : dump raw XML\n"
4495 "-l datamove|done : delay command at datamove or done phase\n"
4496 "-T oneshot : delay one command, then resume normal completion\n"
4497 "-T cont : delay all commands\n"
4498 "-t secs : number of seconds to delay\n"
4500 "-i error_action : action to perform\n"
4501 "-p pattern : command pattern to look for\n"
4502 "-r lba,len : LBA range for pattern\n"
4503 "-s len fmt [args] : sense data for custom sense action\n"
4504 "-c : continuous operation\n"
4505 "-d delete_id : error id to delete\n"
4507 "-l : list frontend ports\n"
4508 "-o on|off : turn frontend ports on or off\n"
4509 "-w wwnn : set WWNN for one frontend\n"
4510 "-W wwpn : set WWPN for one frontend\n"
4511 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
4512 "-p targ_port : specify target port number\n"
4513 "-q : omit header in list output\n"
4514 "-x : output port list in XML format\n"
4515 "portlist options:\n"
4516 "-f fronetnd : specify frontend type\n"
4517 "-i : report target and initiators addresses\n"
4518 "-p targ_port : specify target port number\n"
4519 "-q : omit header in list output\n"
4520 "-v : verbose output (report all port options)\n"
4521 "-x : output port list in XML format\n"
4522 "bbrread options:\n"
4523 "-l lba : starting LBA\n"
4524 "-d datalen : length, in bytes, to read\n",
4529 main(int argc, char **argv)
4532 ctladm_cmdfunction command;
4533 ctladm_cmdargs cmdargs;
4534 ctladm_optret optreturn;
4536 const char *mainopt = "C:D:I:";
4537 const char *subopt = NULL;
4538 char combinedopt[256];
4547 cmdargs = CTLADM_ARG_NONE;
4548 command = CTLADM_CMD_HELP;
4563 * Get the base option.
4565 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4567 if (optreturn == CC_OR_AMBIGUOUS) {
4568 warnx("ambiguous option %s", argv[1]);
4571 } else if (optreturn == CC_OR_NOT_FOUND) {
4572 warnx("option %s not found", argv[1]);
4577 if (cmdargs & CTLADM_ARG_NEED_TL) {
4579 || (!isdigit(argv[2][0]))) {
4580 warnx("option %s requires a target:lun argument",
4585 retval = cctl_parse_tl(argv[2], &target, &lun);
4587 errx(1, "invalid target:lun argument %s", argv[2]);
4589 cmdargs |= CTLADM_ARG_TARG_LUN;
4594 * Ahh, getopt(3) is a pain.
4596 * This is a gross hack. There really aren't many other good
4597 * options (excuse the pun) for parsing options in a situation like
4598 * this. getopt is kinda braindead, so you end up having to run
4599 * through the options twice, and give each invocation of getopt
4600 * the option string for the other invocation.
4602 * You would think that you could just have two groups of options.
4603 * The first group would get parsed by the first invocation of
4604 * getopt, and the second group would get parsed by the second
4605 * invocation of getopt. It doesn't quite work out that way. When
4606 * the first invocation of getopt finishes, it leaves optind pointing
4607 * to the argument _after_ the first argument in the second group.
4608 * So when the second invocation of getopt comes around, it doesn't
4609 * recognize the first argument it gets and then bails out.
4611 * A nice alternative would be to have a flag for getopt that says
4612 * "just keep parsing arguments even when you encounter an unknown
4613 * argument", but there isn't one. So there's no real clean way to
4614 * easily parse two sets of arguments without having one invocation
4615 * of getopt know about the other.
4617 * Without this hack, the first invocation of getopt would work as
4618 * long as the generic arguments are first, but the second invocation
4619 * (in the subfunction) would fail in one of two ways. In the case
4620 * where you don't set optreset, it would fail because optind may be
4621 * pointing to the argument after the one it should be pointing at.
4622 * In the case where you do set optreset, and reset optind, it would
4623 * fail because getopt would run into the first set of options, which
4624 * it doesn't understand.
4626 * All of this would "sort of" work if you could somehow figure out
4627 * whether optind had been incremented one option too far. The
4628 * mechanics of that, however, are more daunting than just giving
4629 * both invocations all of the expect options for either invocation.
4631 * Needless to say, I wouldn't mind if someone invented a better
4632 * (non-GPL!) command line parsing interface than getopt. I
4633 * wouldn't mind if someone added more knobs to getopt to make it
4634 * work better. Who knows, I may talk myself into doing it someday,
4635 * if the standards weenies let me. As it is, it just leads to
4636 * hackery like this and causes people to avoid it in some cases.
4638 * KDM, September 8th, 1998
4641 sprintf(combinedopt, "%s%s", mainopt, subopt);
4643 sprintf(combinedopt, "%s", mainopt);
4646 * Start getopt processing at argv[2/3], since we've already
4647 * accepted argv[1..2] as the command name, and as a possible
4653 * Now we run through the argument list looking for generic
4654 * options, and ignoring options that possibly belong to
4657 while ((c = getopt(argc, argv, combinedopt))!= -1){
4660 cmdargs |= CTLADM_ARG_RETRIES;
4661 retries = strtol(optarg, NULL, 0);
4664 device = strdup(optarg);
4665 cmdargs |= CTLADM_ARG_DEVICE;
4668 cmdargs |= CTLADM_ARG_INITIATOR;
4669 initid = strtol(optarg, NULL, 0);
4676 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4683 * Default to opening the CTL device for now.
4685 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4686 && (command != CTLADM_CMD_HELP)) {
4687 device = strdup(CTL_DEFAULT_DEV);
4688 cmdargs |= CTLADM_ARG_DEVICE;
4691 if ((cmdargs & CTLADM_ARG_DEVICE)
4692 && (command != CTLADM_CMD_HELP)) {
4693 fd = open(device, O_RDWR);
4694 if (fd == -1 && errno == ENOENT) {
4695 saved_errno = errno;
4696 retval = kldload("ctl");
4698 fd = open(device, O_RDWR);
4700 errno = saved_errno;
4703 fprintf(stderr, "%s: error opening %s: %s\n",
4704 argv[0], device, strerror(errno));
4708 } else if ((command != CTLADM_CMD_HELP)
4709 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4710 fprintf(stderr, "%s: you must specify a device with the "
4711 "--device argument for this command\n", argv[0]);
4712 command = CTLADM_CMD_HELP;
4717 case CTLADM_CMD_TUR:
4718 retval = cctl_tur(fd, target, lun, initid, retries);
4720 case CTLADM_CMD_INQUIRY:
4721 retval = cctl_inquiry(fd, target, lun, initid, retries);
4723 case CTLADM_CMD_REQ_SENSE:
4724 retval = cctl_req_sense(fd, target, lun, initid, retries);
4726 case CTLADM_CMD_REPORT_LUNS:
4727 retval = cctl_report_luns(fd, target, lun, initid, retries);
4729 case CTLADM_CMD_CREATE:
4730 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4733 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4735 case CTLADM_CMD_DEVLIST:
4736 retval = cctl_devlist(fd, argc, argv, combinedopt);
4738 case CTLADM_CMD_READ:
4739 case CTLADM_CMD_WRITE:
4740 retval = cctl_read_write(fd, target, lun, initid, retries,
4741 argc, argv, combinedopt, command);
4743 case CTLADM_CMD_PORT:
4744 retval = cctl_port(fd, argc, argv, combinedopt);
4746 case CTLADM_CMD_PORTLIST:
4747 retval = cctl_portlist(fd, argc, argv, combinedopt);
4749 case CTLADM_CMD_READCAPACITY:
4750 retval = cctl_read_capacity(fd, target, lun, initid, retries,
4751 argc, argv, combinedopt);
4753 case CTLADM_CMD_MODESENSE:
4754 retval = cctl_mode_sense(fd, target, lun, initid, retries,
4755 argc, argv, combinedopt);
4757 case CTLADM_CMD_START:
4758 case CTLADM_CMD_STOP:
4759 retval = cctl_start_stop(fd, target, lun, initid, retries,
4760 (command == CTLADM_CMD_START) ? 1 : 0,
4761 argc, argv, combinedopt);
4763 case CTLADM_CMD_SYNC_CACHE:
4764 retval = cctl_sync_cache(fd, target, lun, initid, retries,
4765 argc, argv, combinedopt);
4767 case CTLADM_CMD_SHUTDOWN:
4768 case CTLADM_CMD_STARTUP:
4769 retval = cctl_startup_shutdown(fd, target, lun, initid,
4772 case CTLADM_CMD_HARDSTOP:
4773 case CTLADM_CMD_HARDSTART:
4774 retval = cctl_hardstopstart(fd, command);
4776 case CTLADM_CMD_BBRREAD:
4777 retval = cctl_bbrread(fd, target, lun, initid, argc, argv,
4780 case CTLADM_CMD_LUNLIST:
4781 retval = cctl_lunlist(fd);
4783 case CTLADM_CMD_DELAY:
4784 retval = cctl_delay(fd, target, lun, argc, argv, combinedopt);
4786 case CTLADM_CMD_REALSYNC:
4787 retval = cctl_realsync(fd, argc, argv);
4789 case CTLADM_CMD_SETSYNC:
4790 case CTLADM_CMD_GETSYNC:
4791 retval = cctl_getsetsync(fd, target, lun, command,
4792 argc, argv, combinedopt);
4794 case CTLADM_CMD_ERR_INJECT:
4795 retval = cctl_error_inject(fd, target, lun, argc, argv,
4798 case CTLADM_CMD_DUMPOOA:
4799 retval = cctl_dump_ooa(fd, argc, argv);
4801 case CTLADM_CMD_DUMPSTRUCTS:
4802 retval = cctl_dump_structs(fd, cmdargs);
4804 case CTLADM_CMD_PRES_IN:
4805 retval = cctl_persistent_reserve_in(fd, target, lun, initid,
4806 argc, argv, combinedopt,
4809 case CTLADM_CMD_PRES_OUT:
4810 retval = cctl_persistent_reserve_out(fd, target, lun, initid,
4811 argc, argv, combinedopt,
4814 case CTLADM_CMD_INQ_VPD_DEVID:
4815 retval = cctl_inquiry_vpd_devid(fd, target, lun, initid);
4817 case CTLADM_CMD_RTPG:
4818 retval = cctl_report_target_port_group(fd, target, lun, initid);
4820 case CTLADM_CMD_MODIFY:
4821 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4823 case CTLADM_CMD_ISLIST:
4824 retval = cctl_islist(fd, argc, argv, combinedopt);
4826 case CTLADM_CMD_ISLOGOUT:
4827 retval = cctl_islogout(fd, argc, argv, combinedopt);
4829 case CTLADM_CMD_ISTERMINATE:
4830 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4832 case CTLADM_CMD_HELP: