2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
5 * Copyright (c) 1997-2007 Kenneth D. Merry
6 * Copyright (c) 2012 The FreeBSD Foundation
7 * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org>
10 * Portions of this software were developed by Edward Tomasz Napierala
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions, and the following disclaimer,
18 * without modification.
19 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
20 * substantially similar to the "NO WARRANTY" disclaimer below
21 * ("Disclaimer") and any redistribution must be conditioned upon
22 * including a substantially similar Disclaimer requirement for further
23 * binary redistribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGES.
38 * $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.c#4 $
41 * CAM Target Layer exercise program.
43 * Author: Ken Merry <ken@FreeBSD.org>
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
50 #include <sys/callout.h>
51 #include <sys/ioctl.h>
52 #include <sys/linker.h>
53 #include <sys/module.h>
54 #include <sys/queue.h>
69 #include <cam/scsi/scsi_all.h>
70 #include <cam/scsi/scsi_message.h>
71 #include <cam/ctl/ctl.h>
72 #include <cam/ctl/ctl_io.h>
73 #include <cam/ctl/ctl_backend.h>
74 #include <cam/ctl/ctl_ioctl.h>
75 #include <cam/ctl/ctl_util.h>
76 #include <cam/ctl/ctl_scsi_all.h>
84 #define min(x,y) (x < y) ? x : y
91 CTLADM_CMD_REPORT_LUNS,
101 CTLADM_CMD_READCAPACITY,
102 CTLADM_CMD_MODESENSE,
104 CTLADM_CMD_DUMPSTRUCTS,
107 CTLADM_CMD_SYNC_CACHE,
110 CTLADM_CMD_ERR_INJECT,
113 CTLADM_CMD_INQ_VPD_DEVID,
118 CTLADM_CMD_ISTERMINATE,
120 } ctladm_cmdfunction;
123 CTLADM_ARG_NONE = 0x0000000,
124 CTLADM_ARG_AUTOSENSE = 0x0000001,
125 CTLADM_ARG_DEVICE = 0x0000002,
126 CTLADM_ARG_ARRAYSIZE = 0x0000004,
127 CTLADM_ARG_BACKEND = 0x0000008,
128 CTLADM_ARG_CDBSIZE = 0x0000010,
129 CTLADM_ARG_DATALEN = 0x0000020,
130 CTLADM_ARG_FILENAME = 0x0000040,
131 CTLADM_ARG_LBA = 0x0000080,
132 CTLADM_ARG_PC = 0x0000100,
133 CTLADM_ARG_PAGE_CODE = 0x0000200,
134 CTLADM_ARG_PAGE_LIST = 0x0000400,
135 CTLADM_ARG_SUBPAGE = 0x0000800,
136 CTLADM_ARG_PAGELIST = 0x0001000,
137 CTLADM_ARG_DBD = 0x0002000,
138 CTLADM_ARG_TARG_LUN = 0x0004000,
139 CTLADM_ARG_BLOCKSIZE = 0x0008000,
140 CTLADM_ARG_IMMED = 0x0010000,
141 CTLADM_ARG_RELADR = 0x0020000,
142 CTLADM_ARG_RETRIES = 0x0040000,
143 CTLADM_ARG_ONOFFLINE = 0x0080000,
144 CTLADM_ARG_ONESHOT = 0x0100000,
145 CTLADM_ARG_TIMEOUT = 0x0200000,
146 CTLADM_ARG_INITIATOR = 0x0400000,
147 CTLADM_ARG_NOCOPY = 0x0800000,
148 CTLADM_ARG_NEED_TL = 0x1000000
154 ctladm_cmdargs argnum;
164 static const char rw_opts[] = "Nb:c:d:f:l:";
165 static const char startstop_opts[] = "i";
167 static struct ctladm_opts option_table[] = {
168 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
169 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
170 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
171 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
172 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
173 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
174 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
175 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
176 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
177 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
178 {"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"},
179 {"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"},
180 {"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"},
181 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
182 {"lunmap", CTLADM_CMD_LUNMAP, CTLADM_ARG_NONE, "p:l:L:"},
183 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
184 {"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:o:s:"},
185 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:O:d:crp:qt:w:W:x"},
186 {"portlist", CTLADM_CMD_PORTLIST, CTLADM_ARG_NONE, "f:ilp:qvx"},
187 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
188 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
189 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
190 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
191 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
192 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
193 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
194 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
195 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
196 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
197 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
198 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
199 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
200 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
201 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
206 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
207 ctladm_cmdargs *argnum, const char **subopt);
208 static int cctl_dump_ooa(int fd, int argc, char **argv);
209 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
210 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
211 static int cctl_delay(int fd, int lun, int argc, char **argv,
213 static int cctl_lunlist(int fd);
214 static int cctl_sync_cache(int fd, int lun, int iid, int retries,
215 int argc, char **argv, char *combinedopt);
216 static int cctl_start_stop(int fd, int lun, int iid, int retries,
217 int start, int argc, char **argv, char *combinedopt);
218 static int cctl_mode_sense(int fd, int lun, int iid, int retries,
219 int argc, char **argv, char *combinedopt);
220 static int cctl_read_capacity(int fd, int lun, int iid,
221 int retries, int argc, char **argv,
223 static int cctl_read_write(int fd, int lun, int iid, int retries,
224 int argc, char **argv, char *combinedopt,
225 ctladm_cmdfunction command);
226 static int cctl_get_luns(int fd, int lun, int iid, int retries,
227 struct scsi_report_luns_data **lun_data,
229 static int cctl_report_luns(int fd, int lun, int iid, int retries);
230 static int cctl_tur(int fd, int lun, int iid, int retries);
231 static int cctl_get_inquiry(int fd, int lun, int iid, int retries,
232 char *path_str, int path_len,
233 struct scsi_inquiry_data *inq_data);
234 static int cctl_inquiry(int fd, int lun, int iid, int retries);
235 static int cctl_req_sense(int fd, int lun, int iid, int retries);
236 static int cctl_persistent_reserve_in(int fd, int lun,
237 int initiator, int argc, char **argv,
238 char *combinedopt, int retry_count);
239 static int cctl_persistent_reserve_out(int fd, int lun,
240 int initiator, int argc, char **argv,
241 char *combinedopt, int retry_count);
242 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
243 static int cctl_inquiry_vpd_devid(int fd, int lun, int initiator);
244 static int cctl_report_target_port_group(int fd, int lun, int initiator);
245 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
246 static int cctl_portlist(int fd, int argc, char **argv, char *combinedopt);
249 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
250 ctladm_cmdargs *argnum, const char **subopt)
252 struct ctladm_opts *opts;
255 for (opts = table; (opts != NULL) && (opts->optname != NULL);
257 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
258 *cmdnum = opts->cmdnum;
259 *argnum = opts->argnum;
260 *subopt = opts->subopt;
262 if (strcmp(opts->optname, arg) == 0)
263 return (CC_OR_FOUND);
265 if (++num_matches > 1)
266 return(CC_OR_AMBIGUOUS);
273 return(CC_OR_NOT_FOUND);
277 cctl_dump_ooa(int fd, int argc, char **argv)
280 long double cmd_latency;
281 int num_entries, len, lun = -1, retval = 0;
286 if ((argc > 2) && (isdigit(argv[2][0])))
287 lun = strtol(argv[2], NULL, 0);
290 len = num_entries * sizeof(struct ctl_ooa_entry);
291 bzero(&ooa, sizeof(ooa));
292 ooa.entries = malloc(len);
293 if (ooa.entries == NULL) {
294 warn("%s: error mallocing %d bytes", __func__, len);
300 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
302 ooa.alloc_num = num_entries;
303 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
304 warn("%s: CTL_GET_OOA ioctl failed", __func__);
309 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
310 num_entries = num_entries * 2;
316 if (ooa.status != CTL_OOA_OK) {
317 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
323 fprintf(stdout, "Dumping OOA queues\n");
324 for (i = 0; i < ooa.fill_num; i++) {
325 struct ctl_ooa_entry *entry;
326 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
327 struct bintime delta_bt;
330 entry = &ooa.entries[i];
332 delta_bt = ooa.cur_bt;
333 bintime_sub(&delta_bt, &entry->start_bt);
334 bintime2timespec(&delta_bt, &ts);
335 cmd_latency = ts.tv_sec * 1000;
337 cmd_latency += ts.tv_nsec / 1000000;
339 fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
341 (intmax_t)entry->lun_num, entry->tag_num,
342 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
344 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
345 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
347 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
349 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
350 scsi_op_desc(entry->cdb[0], NULL),
351 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
354 fprintf(stdout, "OOA queues dump done\n");
362 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
364 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
377 CCTL_PORT_MODE_CREATE,
378 CCTL_PORT_MODE_REMOVE
381 static struct ctladm_opts cctl_fe_table[] = {
382 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
383 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
384 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
385 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
386 {"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
387 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
392 cctl_port(int fd, int argc, char **argv, char *combinedopt)
395 int32_t targ_port = -1;
397 int wwnn_set = 0, wwpn_set = 0;
398 uint64_t wwnn = 0, wwpn = 0;
399 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
400 struct ctl_port_entry entry;
403 nvlist_t *option_list;
404 ctl_port_type port_type = CTL_PORT_NONE;
405 int quiet = 0, xml = 0;
407 option_list = nvlist_create(0);
408 if (option_list == NULL)
409 err(1, "%s: unable to allocate nvlist", __func__);
411 while ((c = getopt(argc, argv, combinedopt)) != -1) {
414 if (port_mode != CCTL_PORT_MODE_NONE)
417 port_mode = CCTL_PORT_MODE_LIST;
420 port_mode = CCTL_PORT_MODE_CREATE;
423 port_mode = CCTL_PORT_MODE_REMOVE;
426 if (port_mode != CCTL_PORT_MODE_NONE)
429 if (strcasecmp(optarg, "on") == 0)
430 port_mode = CCTL_PORT_MODE_ON;
431 else if (strcasecmp(optarg, "off") == 0)
432 port_mode = CCTL_PORT_MODE_OFF;
434 warnx("Invalid -o argument %s, \"on\" or "
435 "\"off\" are the only valid args",
445 tmpstr = strdup(optarg);
446 name = strsep(&tmpstr, "=");
448 warnx("%s: option -O takes \"name=value\""
449 "argument", __func__);
453 value = strsep(&tmpstr, "=");
455 warnx("%s: option -O takes \"name=value\""
456 "argument", __func__);
462 nvlist_add_string(option_list, name, value);
466 if (driver != NULL) {
467 warnx("%s: option -d cannot be specified twice",
473 driver = strdup(optarg);
476 targ_port = strtol(optarg, NULL, 0);
482 ctladm_optret optret;
483 ctladm_cmdargs argnum;
485 ctl_port_type tmp_port_type;
487 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
489 if (optret == CC_OR_AMBIGUOUS) {
490 warnx("%s: ambiguous frontend type %s",
494 } else if (optret == CC_OR_NOT_FOUND) {
495 warnx("%s: invalid frontend type %s",
501 port_type |= tmp_port_type;
505 if ((port_mode != CCTL_PORT_MODE_NONE)
506 && (port_mode != CCTL_PORT_MODE_SET))
509 port_mode = CCTL_PORT_MODE_SET;
511 wwnn = strtoull(optarg, NULL, 0);
515 if ((port_mode != CCTL_PORT_MODE_NONE)
516 && (port_mode != CCTL_PORT_MODE_SET))
519 port_mode = CCTL_PORT_MODE_SET;
521 wwpn = strtoull(optarg, NULL, 0);
531 driver = strdup("ioctl");
534 * The user can specify either one or more frontend types (-t), or
535 * a specific frontend, but not both.
537 * If the user didn't specify a frontend type or number, set it to
538 * all. This is primarily needed for the enable/disable ioctls.
539 * This will be a no-op for the listing code. For the set ioctl,
540 * we'll throw an error, since that only works on one port at a time.
542 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
543 warnx("%s: can only specify one of -t or -n", __func__);
546 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
547 port_type = CTL_PORT_ALL;
549 bzero(&entry, sizeof(entry));
552 * These are needed for all but list/dump mode.
554 entry.port_type = port_type;
555 entry.targ_port = targ_port;
558 case CCTL_PORT_MODE_LIST: {
568 argvx[argcx++] = argx;
570 argvx[argcx++] = argq;
571 cctl_portlist(fd, argcx, argvx, opts);
574 case CCTL_PORT_MODE_REMOVE:
575 if (targ_port == -1) {
576 warnx("%s: -r require -p", __func__);
580 case CCTL_PORT_MODE_CREATE: {
581 bzero(&req, sizeof(req));
582 strlcpy(req.driver, driver, sizeof(req.driver));
584 if (port_mode == CCTL_PORT_MODE_REMOVE) {
585 req.reqtype = CTL_REQ_REMOVE;
586 nvlist_add_stringf(option_list, "port_id", "%d",
589 req.reqtype = CTL_REQ_CREATE;
591 req.args = nvlist_pack(option_list, &req.args_len);
592 if (req.args == NULL) {
593 warn("%s: error packing nvlist", __func__);
598 retval = ioctl(fd, CTL_PORT_REQ, &req);
601 warn("%s: CTL_PORT_REQ ioctl failed", __func__);
606 switch (req.status) {
608 warnx("error: %s", req.error_str);
611 case CTL_LUN_WARNING:
612 warnx("warning: %s", req.error_str);
617 warnx("unknown status: %d", req.status);
624 case CCTL_PORT_MODE_SET:
625 if (targ_port == -1) {
626 warnx("%s: -w and -W require -n", __func__);
632 entry.flags |= CTL_PORT_WWNN_VALID;
636 entry.flags |= CTL_PORT_WWPN_VALID;
640 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
641 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
646 case CCTL_PORT_MODE_ON:
647 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
648 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
652 fprintf(stdout, "Front End Ports enabled\n");
654 case CCTL_PORT_MODE_OFF:
655 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
656 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
660 fprintf(stdout, "Front End Ports disabled\n");
663 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
670 nvlist_destroy(option_list);
675 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
680 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
683 if (ioctl(fd, CTL_IO, io) == -1) {
684 warn("%s: error sending CTL_IO ioctl", func);
687 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
694 cctl_delay(int fd, int lun, int argc, char **argv,
697 struct ctl_io_delay_info delay_info;
698 char *delayloc = NULL;
699 char *delaytype = NULL;
706 memset(&delay_info, 0, sizeof(delay_info));
708 while ((c = getopt(argc, argv, combinedopt)) != -1) {
711 delaytype = strdup(optarg);
714 delayloc = strdup(optarg);
717 delaytime = strtoul(optarg, NULL, 0);
722 if (delaytime == -1) {
723 warnx("%s: you must specify the delaytime with -t", __func__);
728 if (strcasecmp(delayloc, "datamove") == 0)
729 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
730 else if (strcasecmp(delayloc, "done") == 0)
731 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
733 warnx("%s: invalid delay location %s", __func__, delayloc);
738 if ((delaytype == NULL)
739 || (strcmp(delaytype, "oneshot") == 0))
740 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
741 else if (strcmp(delaytype, "cont") == 0)
742 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
744 warnx("%s: invalid delay type %s", __func__, delaytype);
749 delay_info.lun_id = lun;
750 delay_info.delay_secs = delaytime;
752 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
753 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
757 switch (delay_info.status) {
758 case CTL_DELAY_STATUS_NONE:
759 warnx("%s: no delay status??", __func__);
762 case CTL_DELAY_STATUS_OK:
764 case CTL_DELAY_STATUS_INVALID_LUN:
765 warnx("%s: invalid lun %d", __func__, lun);
768 case CTL_DELAY_STATUS_INVALID_TYPE:
769 warnx("%s: invalid delay type %d", __func__,
770 delay_info.delay_type);
773 case CTL_DELAY_STATUS_INVALID_LOC:
774 warnx("%s: delay location %s not implemented?", __func__,
778 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
779 warnx("%s: delay not implemented in the kernel", __func__);
780 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
784 warnx("%s: unknown delay return status %d", __func__,
796 static struct ctladm_opts cctl_err_types[] = {
797 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
798 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
799 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
800 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
805 static struct ctladm_opts cctl_err_patterns[] = {
806 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
807 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
808 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
809 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
810 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
811 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
812 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
814 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
820 cctl_error_inject(int fd, uint32_t lun, int argc, char **argv,
824 struct ctl_error_desc err_desc;
827 uint64_t delete_id = 0;
828 int delete_id_set = 0;
834 bzero(&err_desc, sizeof(err_desc));
835 err_desc.lun_id = lun;
837 while ((c = getopt(argc, argv, combinedopt)) != -1) {
843 delete_id = strtoull(optarg, NULL, 0);
848 ctladm_optret optret;
849 ctladm_cmdargs argnum;
853 ctl_lun_error err_type;
855 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
856 warnx("%s: can't specify multiple -i "
857 "arguments", __func__);
861 optret = getoption(cctl_err_types, optarg,
862 &err_type, &argnum, &subopt);
863 err_desc.lun_error = err_type;
865 ctl_lun_error_pattern pattern;
867 optret = getoption(cctl_err_patterns, optarg,
868 &pattern, &argnum, &subopt);
869 err_desc.error_pattern |= pattern;
872 if (optret == CC_OR_AMBIGUOUS) {
873 warnx("%s: ambiguous argument %s", __func__,
877 } else if (optret == CC_OR_NOT_FOUND) {
878 warnx("%s: argument %s not found", __func__,
886 char *tmpstr, *tmpstr2;
888 tmpstr = strdup(optarg);
889 if (tmpstr == NULL) {
890 warn("%s: error duplicating string %s",
896 tmpstr2 = strsep(&tmpstr, ",");
897 if (tmpstr2 == NULL) {
898 warnx("%s: invalid -r argument %s", __func__,
904 lba = strtoull(tmpstr2, NULL, 0);
905 tmpstr2 = strsep(&tmpstr, ",");
906 if (tmpstr2 == NULL) {
907 warnx("%s: no len argument for -r lba,len, got"
908 " %s", __func__, optarg);
913 len = strtoul(tmpstr2, NULL, 0);
918 struct get_hook hook;
921 sense_len = strtol(optarg, NULL, 0);
922 if (sense_len <= 0) {
923 warnx("invalid number of sense bytes %d",
929 sense_len = MIN(sense_len, SSD_FULL_SIZE);
931 hook.argc = argc - optind;
932 hook.argv = argv + optind;
935 sensestr = cget(&hook, NULL);
936 if ((sensestr != NULL)
937 && (sensestr[0] == '-')) {
941 (uint8_t *)&err_desc.custom_sense,
942 sense_len, sensestr, iget, &hook);
952 if (delete_id_set != 0) {
953 err_desc.serial = delete_id;
954 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
955 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
962 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
963 warnx("%s: error injection command (-i) needed",
967 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
968 && (sense_len == 0)) {
969 warnx("%s: custom error requires -s", __func__);
975 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
978 * If fd_sense is set, we need to read the sense data the user
979 * wants returned from stdin.
983 int amt_to_read = sense_len;
984 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
986 for (amt_read = 0; amt_to_read > 0;
987 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
988 if (amt_read == -1) {
989 warn("error reading sense data from stdin");
993 amt_to_read -= amt_read;
998 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
999 warnx("%s: command pattern (-p) needed", __func__);
1005 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1007 * We could check here to see whether it's a read/write
1008 * command, but that will be pointless once we allow
1009 * custom patterns. At that point, the user could specify
1010 * a READ(6) CDB type, and we wouldn't have an easy way here
1011 * to verify whether range checking is possible there. The
1012 * user will just figure it out when his error never gets
1016 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1017 warnx("%s: need read and/or write pattern if range "
1018 "is specified", __func__);
1023 err_desc.lba_range.lba = lba;
1024 err_desc.lba_range.len = len;
1027 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1028 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1031 printf("Error injection succeeded, serial number is %ju\n",
1032 (uintmax_t)err_desc.serial);
1040 cctl_lunlist(int fd)
1042 struct scsi_report_luns_data *lun_data;
1043 struct scsi_inquiry_data *inq_data;
1053 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1054 * if we ever acquire the ability to have multiple targets.
1056 if ((retval = cctl_get_luns(fd, /*lun*/ 0, initid,
1057 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1060 inq_data = malloc(sizeof(*inq_data));
1061 if (inq_data == NULL) {
1062 warn("%s: couldn't allocate memory for inquiry data\n",
1067 for (i = 0; i < num_luns; i++) {
1071 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1072 case RPL_LUNDATA_ATYP_PERIPH:
1073 lun_val = lun_data->luns[i].lundata[1];
1075 case RPL_LUNDATA_ATYP_FLAT:
1076 lun_val = (lun_data->luns[i].lundata[0] &
1077 RPL_LUNDATA_FLAT_LUN_MASK) |
1078 (lun_data->luns[i].lundata[1] <<
1079 RPL_LUNDATA_FLAT_LUN_BITS);
1081 case RPL_LUNDATA_ATYP_LUN:
1082 case RPL_LUNDATA_ATYP_EXTLUN:
1084 fprintf(stdout, "Unsupported LUN format %d\n",
1085 lun_data->luns[i].lundata[0] &
1086 RPL_LUNDATA_ATYP_MASK);
1093 if ((retval = cctl_get_inquiry(fd, lun_val, initid,
1094 /*retries*/ 2, scsi_path,
1099 printf("%s", scsi_path);
1100 scsi_print_inquiry(inq_data);
1104 if (lun_data != NULL)
1107 if (inq_data != NULL)
1114 cctl_sync_cache(int fd, int lun, int iid, int retries,
1115 int argc, char **argv, char *combinedopt)
1120 uint64_t our_lba = 0;
1121 uint32_t our_block_count = 0;
1122 int reladr = 0, immed = 0;
1127 io = ctl_scsi_alloc_io(iid);
1129 warnx("%s: can't allocate memory", __func__);
1133 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1136 our_block_count = strtoul(optarg, NULL, 0);
1139 cdb_size = strtol(optarg, NULL, 0);
1145 our_lba = strtoull(optarg, NULL, 0);
1155 if (cdb_size != -1) {
1161 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1162 "and 16", __func__, cdb_size);
1165 break; /* NOTREACHED */
1170 ctl_scsi_sync_cache(/*io*/ io,
1173 /*minimum_cdb_size*/ cdb_size,
1174 /*starting_lba*/ our_lba,
1175 /*block_count*/ our_block_count,
1176 /*tag_type*/ CTL_TAG_SIMPLE,
1179 io->io_hdr.nexus.targ_lun = lun;
1180 io->io_hdr.nexus.initid = iid;
1182 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1187 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1188 fprintf(stdout, "Cache synchronized successfully\n");
1190 ctl_io_error_print(io, NULL, stderr);
1192 ctl_scsi_free_io(io);
1198 cctl_start_stop(int fd, int lun, int iid, int retries, int start,
1199 int argc, char **argv, char *combinedopt)
1208 io = ctl_scsi_alloc_io(iid);
1210 warnx("%s: can't allocate memory", __func__);
1214 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1224 * Use an ordered tag for the stop command, to guarantee that any
1225 * pending I/O will finish before the stop command executes. This
1226 * would normally be the case anyway, since CTL will basically
1227 * treat the start/stop command as an ordered command with respect
1228 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1230 ctl_scsi_start_stop(/*io*/ io,
1233 /*immediate*/ immed,
1234 /*power_conditions*/ SSS_PC_START_VALID,
1235 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1239 io->io_hdr.nexus.targ_lun = lun;
1240 io->io_hdr.nexus.initid = iid;
1242 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1247 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1248 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1249 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1250 (start) ? "started" : "stopped");
1252 ctl_io_error_print(io, NULL, stderr);
1255 ctl_scsi_free_io(io);
1261 cctl_mode_sense(int fd, int lun, int iid, int retries,
1262 int argc, char **argv, char *combinedopt)
1267 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1276 io = ctl_scsi_alloc_io(iid);
1278 warn("%s: can't allocate memory", __func__);
1282 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1285 pc = strtoul(optarg, NULL, 0);
1288 subpage = strtoul(optarg, NULL, 0);
1297 page_code = strtoul(optarg, NULL, 0);
1300 cdbsize = strtol(optarg, NULL, 0);
1307 if (((list == 0) && (page_code == -1))
1308 || ((list != 0) && (page_code != -1))) {
1309 warnx("%s: you must specify either a page code (-m) or -l",
1315 if ((page_code != -1)
1316 && ((page_code > SMS_ALL_PAGES_PAGE)
1317 || (page_code < 0))) {
1318 warnx("%s: page code %d is out of range", __func__,
1325 page_code = SMS_ALL_PAGES_PAGE;
1327 warnx("%s: arg -P makes no sense with -l",
1332 if (subpage != -1) {
1333 warnx("%s: arg -S makes no sense with -l", __func__);
1340 pc = SMS_PAGE_CTRL_CURRENT;
1344 warnx("%s: page control value %d is out of range: 0-3",
1354 || (subpage < 0))) {
1355 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1366 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1367 "and 10", __func__, cdbsize);
1383 dataptr = (uint8_t *)malloc(datalen);
1384 if (dataptr == NULL) {
1385 warn("%s: can't allocate %d bytes", __func__, datalen);
1390 memset(dataptr, 0, datalen);
1392 ctl_scsi_mode_sense(io,
1393 /*data_ptr*/ dataptr,
1394 /*data_len*/ datalen,
1397 /*page_code*/ page_code,
1399 /*subpage*/ subpage,
1400 /*minimum_cdb_size*/ cdbsize,
1401 /*tag_type*/ CTL_TAG_SIMPLE,
1404 io->io_hdr.nexus.targ_lun = lun;
1405 io->io_hdr.nexus.initid = iid;
1407 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1412 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1413 int pages_len, used_len;
1414 uint32_t returned_len;
1417 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1418 struct scsi_mode_hdr_6 *hdr6;
1421 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1423 returned_len = hdr6->datalen + 1;
1424 bdlen = hdr6->block_descr_len;
1426 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1428 struct scsi_mode_hdr_10 *hdr10;
1431 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
1433 returned_len = scsi_2btoul(hdr10->datalen) + 2;
1434 bdlen = scsi_2btoul(hdr10->block_descr_len);
1436 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
1438 /* just in case they can give us more than we allocated for */
1439 returned_len = min(returned_len, datalen);
1440 pages_len = returned_len - (ndataptr - dataptr);
1442 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
1443 returned_len, pages_len);
1446 fprintf(stdout, "Supported mode pages:\n");
1447 for (used_len = 0; used_len < pages_len;) {
1448 struct scsi_mode_page_header *header;
1450 header = (struct scsi_mode_page_header *)
1451 &ndataptr[used_len];
1452 fprintf(stdout, "%d\n", header->page_code);
1453 used_len += header->page_length + 2;
1456 for (used_len = 0; used_len < pages_len; used_len++) {
1457 fprintf(stdout, "0x%x ", ndataptr[used_len]);
1458 if (((used_len+1) % 16) == 0)
1459 fprintf(stdout, "\n");
1461 fprintf(stdout, "\n");
1464 ctl_io_error_print(io, NULL, stderr);
1467 ctl_scsi_free_io(io);
1469 if (dataptr != NULL)
1476 cctl_read_capacity(int fd, int lun, int iid, int retries,
1477 int argc, char **argv, char *combinedopt)
1480 struct scsi_read_capacity_data *data;
1481 struct scsi_read_capacity_data_long *longdata;
1482 int cdbsize = -1, retval;
1490 io = ctl_scsi_alloc_io(iid);
1492 warn("%s: can't allocate memory\n", __func__);
1496 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1499 cdbsize = strtol(optarg, NULL, 0);
1505 if (cdbsize != -1) {
1511 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1512 "and 16", __func__, cdbsize);
1515 break; /* NOTREACHED */
1520 dataptr = (uint8_t *)malloc(sizeof(*longdata));
1521 if (dataptr == NULL) {
1522 warn("%s: can't allocate %zd bytes\n", __func__,
1527 memset(dataptr, 0, sizeof(*longdata));
1533 ctl_scsi_read_capacity(io,
1534 /*data_ptr*/ dataptr,
1535 /*data_len*/ sizeof(*longdata),
1539 /*tag_type*/ CTL_TAG_SIMPLE,
1543 ctl_scsi_read_capacity_16(io,
1544 /*data_ptr*/ dataptr,
1545 /*data_len*/ sizeof(*longdata),
1549 /*tag_type*/ CTL_TAG_SIMPLE,
1554 io->io_hdr.nexus.initid = iid;
1555 io->io_hdr.nexus.targ_lun = lun;
1557 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1562 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1566 if (cdbsize == 10) {
1568 data = (struct scsi_read_capacity_data *)dataptr;
1570 maxlba = scsi_4btoul(data->addr);
1571 blocksize = scsi_4btoul(data->length);
1573 if (maxlba == 0xffffffff) {
1578 longdata=(struct scsi_read_capacity_data_long *)dataptr;
1580 maxlba = scsi_8btou64(longdata->addr);
1581 blocksize = scsi_4btoul(longdata->length);
1584 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
1585 (uintmax_t)maxlba, blocksize);
1587 ctl_io_error_print(io, NULL, stderr);
1590 ctl_scsi_free_io(io);
1592 if (dataptr != NULL)
1599 cctl_read_write(int fd, int lun, int iid, int retries,
1600 int argc, char **argv, char *combinedopt,
1601 ctladm_cmdfunction command)
1604 int file_fd, do_stdio;
1605 int cdbsize = -1, databytes;
1607 char *filename = NULL;
1608 int datalen = -1, blocksize = -1;
1619 io = ctl_scsi_alloc_io(iid);
1621 warn("%s: can't allocate memory\n", __func__);
1625 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1628 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
1631 blocksize = strtoul(optarg, NULL, 0);
1634 cdbsize = strtoul(optarg, NULL, 0);
1637 datalen = strtoul(optarg, NULL, 0);
1640 filename = strdup(optarg);
1643 lba = strtoull(optarg, NULL, 0);
1650 if (filename == NULL) {
1651 warnx("%s: you must supply a filename using -f", __func__);
1656 if (datalen == -1) {
1657 warnx("%s: you must specify the data length with -d", __func__);
1663 warnx("%s: you must specify the LBA with -l", __func__);
1668 if (blocksize == -1) {
1669 warnx("%s: you must specify the blocksize with -b", __func__);
1674 if (cdbsize != -1) {
1682 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
1683 "10, 12 or 16", __func__, cdbsize);
1686 break; /* NOTREACHED */
1691 databytes = datalen * blocksize;
1692 dataptr = (uint8_t *)malloc(databytes);
1694 if (dataptr == NULL) {
1695 warn("%s: can't allocate %d bytes\n", __func__, databytes);
1699 if (strcmp(filename, "-") == 0) {
1700 if (command == CTLADM_CMD_READ)
1701 file_fd = STDOUT_FILENO;
1703 file_fd = STDIN_FILENO;
1706 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1707 if (file_fd == -1) {
1708 warn("%s: can't open file %s", __func__, filename);
1714 memset(dataptr, 0, databytes);
1716 if (command == CTLADM_CMD_WRITE) {
1719 bytes_read = read(file_fd, dataptr, databytes);
1720 if (bytes_read == -1) {
1721 warn("%s: error reading file %s", __func__, filename);
1725 if (bytes_read != databytes) {
1726 warnx("%s: only read %d bytes from file %s",
1727 __func__, bytes_read, filename);
1732 ctl_scsi_read_write(io,
1733 /*data_ptr*/ dataptr,
1734 /*data_len*/ databytes,
1735 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
1737 /*minimum_cdb_size*/ cdbsize,
1739 /*num_blocks*/ datalen,
1740 /*tag_type*/ CTL_TAG_SIMPLE,
1743 io->io_hdr.nexus.targ_lun = lun;
1744 io->io_hdr.nexus.initid = iid;
1746 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1751 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1752 && (command == CTLADM_CMD_READ)) {
1755 bytes_written = write(file_fd, dataptr, databytes);
1756 if (bytes_written == -1) {
1757 warn("%s: can't write to %s", __func__, filename);
1760 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1761 ctl_io_error_print(io, NULL, stderr);
1766 ctl_scsi_free_io(io);
1768 if (dataptr != NULL)
1779 cctl_get_luns(int fd, int lun, int iid, int retries, struct
1780 scsi_report_luns_data **lun_data, uint32_t *num_luns)
1789 io = ctl_scsi_alloc_io(iid);
1791 warnx("%s: can't allocate memory", __func__);
1796 * lun_data includes space for 1 lun, allocate space for 4 initially.
1797 * If that isn't enough, we'll allocate more.
1801 lun_datalen = sizeof(*lun_data) +
1802 (nluns * sizeof(struct scsi_report_luns_lundata));
1803 *lun_data = malloc(lun_datalen);
1805 if (*lun_data == NULL) {
1806 warnx("%s: can't allocate memory", __func__);
1807 ctl_scsi_free_io(io);
1811 ctl_scsi_report_luns(io,
1812 /*data_ptr*/ (uint8_t *)*lun_data,
1813 /*data_len*/ lun_datalen,
1814 /*select_report*/ RPL_REPORT_ALL,
1815 /*tag_type*/ CTL_TAG_SIMPLE,
1818 io->io_hdr.nexus.initid = iid;
1819 io->io_hdr.nexus.targ_lun = lun;
1821 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1826 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1827 uint32_t returned_len, returned_luns;
1829 returned_len = scsi_4btoul((*lun_data)->length);
1830 returned_luns = returned_len / 8;
1831 if (returned_luns > nluns) {
1832 nluns = returned_luns;
1836 /* These should be the same */
1837 *num_luns = MIN(returned_luns, nluns);
1839 ctl_io_error_print(io, NULL, stderr);
1843 ctl_scsi_free_io(io);
1849 cctl_report_luns(int fd, int lun, int iid, int retries)
1851 struct scsi_report_luns_data *lun_data;
1852 uint32_t num_luns, i;
1857 if ((retval = cctl_get_luns(fd, lun, iid, retries, &lun_data,
1861 fprintf(stdout, "%u LUNs returned\n", num_luns);
1862 for (i = 0; i < num_luns; i++) {
1866 * XXX KDM figure out a way to share this code with
1869 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1870 case RPL_LUNDATA_ATYP_PERIPH:
1871 lun_val = lun_data->luns[i].lundata[1];
1873 case RPL_LUNDATA_ATYP_FLAT:
1874 lun_val = (lun_data->luns[i].lundata[0] &
1875 RPL_LUNDATA_FLAT_LUN_MASK) |
1876 (lun_data->luns[i].lundata[1] <<
1877 RPL_LUNDATA_FLAT_LUN_BITS);
1879 case RPL_LUNDATA_ATYP_LUN:
1880 case RPL_LUNDATA_ATYP_EXTLUN:
1882 fprintf(stdout, "Unsupported LUN format %d\n",
1883 lun_data->luns[i].lundata[0] &
1884 RPL_LUNDATA_ATYP_MASK);
1891 fprintf(stdout, "%d\n", lun_val);
1895 if (lun_data != NULL)
1902 cctl_tur(int fd, int lun, int iid, int retries)
1906 io = ctl_scsi_alloc_io(iid);
1908 fprintf(stderr, "can't allocate memory\n");
1913 /* tag_type */ CTL_TAG_SIMPLE,
1916 io->io_hdr.nexus.targ_lun = lun;
1917 io->io_hdr.nexus.initid = iid;
1919 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1920 ctl_scsi_free_io(io);
1924 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1925 fprintf(stdout, "Unit is ready\n");
1927 ctl_io_error_print(io, NULL, stderr);
1933 cctl_get_inquiry(int fd, int lun, int iid, int retries,
1934 char *path_str, int path_len,
1935 struct scsi_inquiry_data *inq_data)
1942 io = ctl_scsi_alloc_io(iid);
1944 warnx("cctl_inquiry: can't allocate memory\n");
1948 ctl_scsi_inquiry(/*io*/ io,
1949 /*data_ptr*/ (uint8_t *)inq_data,
1950 /*data_len*/ sizeof(*inq_data),
1953 /*tag_type*/ CTL_TAG_SIMPLE,
1956 io->io_hdr.nexus.targ_lun = lun;
1957 io->io_hdr.nexus.initid = iid;
1959 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1964 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
1966 ctl_io_error_print(io, NULL, stderr);
1967 } else if (path_str != NULL)
1968 ctl_scsi_path_string(io, path_str, path_len);
1971 ctl_scsi_free_io(io);
1977 cctl_inquiry(int fd, int lun, int iid, int retries)
1979 struct scsi_inquiry_data *inq_data;
1983 inq_data = malloc(sizeof(*inq_data));
1984 if (inq_data == NULL) {
1985 warnx("%s: can't allocate inquiry data", __func__);
1990 if ((retval = cctl_get_inquiry(fd, lun, iid, retries, scsi_path,
1991 sizeof(scsi_path), inq_data)) != 0)
1994 printf("%s", scsi_path);
1995 scsi_print_inquiry(inq_data);
1998 if (inq_data != NULL)
2005 cctl_req_sense(int fd, int lun, int iid, int retries)
2008 struct scsi_sense_data *sense_data;
2013 io = ctl_scsi_alloc_io(iid);
2015 warnx("cctl_req_sense: can't allocate memory\n");
2018 sense_data = malloc(sizeof(*sense_data));
2019 memset(sense_data, 0, sizeof(*sense_data));
2021 ctl_scsi_request_sense(/*io*/ io,
2022 /*data_ptr*/ (uint8_t *)sense_data,
2023 /*data_len*/ sizeof(*sense_data),
2025 /*tag_type*/ CTL_TAG_SIMPLE,
2028 io->io_hdr.nexus.targ_lun = lun;
2029 io->io_hdr.nexus.initid = iid;
2031 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2036 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2037 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2038 io->scsiio.sense_len = sizeof(*sense_data);
2039 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2041 ctl_io_error_print(io, NULL, stderr);
2045 ctl_scsi_free_io(io);
2052 cctl_report_target_port_group(int fd, int lun, int iid)
2062 io = ctl_scsi_alloc_io(iid);
2064 warn("%s: can't allocate memory", __func__);
2069 dataptr = (uint8_t *)malloc(datalen);
2070 if (dataptr == NULL) {
2071 warn("%s: can't allocate %d bytes", __func__, datalen);
2076 memset(dataptr, 0, datalen);
2078 ctl_scsi_maintenance_in(/*io*/ io,
2079 /*data_ptr*/ dataptr,
2080 /*data_len*/ datalen,
2081 /*action*/ SA_RPRT_TRGT_GRP,
2082 /*tag_type*/ CTL_TAG_SIMPLE,
2085 io->io_hdr.nexus.targ_lun = lun;
2086 io->io_hdr.nexus.initid = iid;
2088 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2093 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2094 int returned_len, used_len;
2096 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2098 for (used_len = 0; used_len < returned_len; used_len++) {
2099 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2100 if (((used_len+1) % 8) == 0)
2101 fprintf(stdout, "\n");
2103 fprintf(stdout, "\n");
2105 ctl_io_error_print(io, NULL, stderr);
2108 ctl_scsi_free_io(io);
2110 if (dataptr != NULL)
2117 cctl_inquiry_vpd_devid(int fd, int lun, int iid)
2127 io = ctl_scsi_alloc_io(iid);
2129 warn("%s: can't allocate memory", __func__);
2134 dataptr = (uint8_t *)malloc(datalen);
2135 if (dataptr == NULL) {
2136 warn("%s: can't allocate %d bytes", __func__, datalen);
2141 memset(dataptr, 0, datalen);
2143 ctl_scsi_inquiry(/*io*/ io,
2144 /*data_ptr*/ dataptr,
2145 /*data_len*/ datalen,
2147 /*page_code*/ SVPD_DEVICE_ID,
2148 /*tag_type*/ CTL_TAG_SIMPLE,
2151 io->io_hdr.nexus.targ_lun = lun;
2152 io->io_hdr.nexus.initid = iid;
2154 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2159 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2160 int returned_len, used_len;
2162 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2164 for (used_len = 0; used_len < returned_len; used_len++) {
2165 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2166 if (((used_len+1) % 8) == 0)
2167 fprintf(stdout, "\n");
2169 fprintf(stdout, "\n");
2171 ctl_io_error_print(io, NULL, stderr);
2174 ctl_scsi_free_io(io);
2176 if (dataptr != NULL)
2183 cctl_persistent_reserve_in(int fd, int lun, int iid,
2184 int argc, char **argv, char *combinedopt,
2197 io = ctl_scsi_alloc_io(iid);
2199 warn("%s: can't allocate memory", __func__);
2203 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2206 action = strtol(optarg, NULL, 0);
2213 if (action < 0 || action > 2) {
2214 warn("action must be specified and in the range: 0-2");
2221 dataptr = (uint8_t *)malloc(datalen);
2222 if (dataptr == NULL) {
2223 warn("%s: can't allocate %d bytes", __func__, datalen);
2228 memset(dataptr, 0, datalen);
2230 ctl_scsi_persistent_res_in(io,
2231 /*data_ptr*/ dataptr,
2232 /*data_len*/ datalen,
2234 /*tag_type*/ CTL_TAG_SIMPLE,
2237 io->io_hdr.nexus.targ_lun = lun;
2238 io->io_hdr.nexus.initid = iid;
2240 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2245 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2246 int returned_len, used_len;
2250 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2251 returned_len = min(returned_len, 256);
2254 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2260 warnx("%s: invalid action %d", __func__, action);
2262 break; /* NOTREACHED */
2265 for (used_len = 0; used_len < returned_len; used_len++) {
2266 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2267 if (((used_len+1) % 8) == 0)
2268 fprintf(stdout, "\n");
2270 fprintf(stdout, "\n");
2272 ctl_io_error_print(io, NULL, stderr);
2275 ctl_scsi_free_io(io);
2277 if (dataptr != NULL)
2284 cctl_persistent_reserve_out(int fd, int lun, int iid,
2285 int argc, char **argv, char *combinedopt,
2290 uint64_t key = 0, sa_key = 0;
2291 int action = -1, restype = -1;
2299 io = ctl_scsi_alloc_io(iid);
2301 warn("%s: can't allocate memory", __func__);
2305 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2308 action = strtol(optarg, NULL, 0);
2311 key = strtoull(optarg, NULL, 0);
2314 restype = strtol(optarg, NULL, 0);
2317 sa_key = strtoull(optarg, NULL, 0);
2323 if (action < 0 || action > 5) {
2324 warn("action must be specified and in the range: 0-5");
2329 if (restype < 0 || restype > 5) {
2330 if (action != 0 && action != 5 && action != 3) {
2331 warn("'restype' must specified and in the range: 0-5");
2338 dataptr = (uint8_t *)malloc(datalen);
2339 if (dataptr == NULL) {
2340 warn("%s: can't allocate %d bytes", __func__, datalen);
2345 memset(dataptr, 0, datalen);
2347 ctl_scsi_persistent_res_out(io,
2348 /*data_ptr*/ dataptr,
2349 /*data_len*/ datalen,
2354 /*tag_type*/ CTL_TAG_SIMPLE,
2357 io->io_hdr.nexus.targ_lun = lun;
2358 io->io_hdr.nexus.initid = iid;
2360 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2364 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2366 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2367 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2368 "successfully\n", scsi_path);
2370 ctl_io_error_print(io, NULL, stderr);
2373 ctl_scsi_free_io(io);
2375 if (dataptr != NULL)
2382 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2384 struct ctl_lun_req req;
2385 int device_type = -1;
2386 uint64_t lun_size = 0;
2387 uint32_t blocksize = 0, req_lun_id = 0;
2388 char *serial_num = NULL;
2389 char *device_id = NULL;
2390 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
2391 char *backend_name = NULL;
2392 nvlist_t *option_list;
2395 option_list = nvlist_create(0);
2396 if (option_list == NULL)
2397 err(1, "%s: unable to allocate nvlist", __func__);
2399 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2402 backend_name = strdup(optarg);
2405 blocksize = strtoul(optarg, NULL, 0);
2409 device_id = strdup(optarg);
2412 req_lun_id = strtoul(optarg, NULL, 0);
2419 tmpstr = strdup(optarg);
2420 name = strsep(&tmpstr, "=");
2422 warnx("%s: option -o takes \"name=value\""
2423 "argument", __func__);
2427 value = strsep(&tmpstr, "=");
2428 if (value == NULL) {
2429 warnx("%s: option -o takes \"name=value\""
2430 "argument", __func__);
2435 nvlist_add_string(option_list, name, value);
2439 if (strcasecmp(optarg, "auto") != 0) {
2440 retval = expand_number(optarg, &lun_size);
2442 warn("%s: invalid -s argument",
2451 serial_num = strdup(optarg);
2454 device_type = strtoul(optarg, NULL, 0);
2461 if (backend_name == NULL) {
2462 warnx("%s: backend name (-b) must be specified", __func__);
2467 bzero(&req, sizeof(req));
2469 strlcpy(req.backend, backend_name, sizeof(req.backend));
2470 req.reqtype = CTL_LUNREQ_CREATE;
2472 if (blocksize_set != 0)
2473 req.reqdata.create.blocksize_bytes = blocksize;
2475 if (lun_size_set != 0)
2476 req.reqdata.create.lun_size_bytes = lun_size;
2478 if (lun_id_set != 0) {
2479 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
2480 req.reqdata.create.req_lun_id = req_lun_id;
2483 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
2485 if (device_type != -1)
2486 req.reqdata.create.device_type = device_type;
2488 req.reqdata.create.device_type = T_DIRECT;
2490 if (serial_num != NULL) {
2491 strlcpy(req.reqdata.create.serial_num, serial_num,
2492 sizeof(req.reqdata.create.serial_num));
2493 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
2496 if (device_id != NULL) {
2497 strlcpy(req.reqdata.create.device_id, device_id,
2498 sizeof(req.reqdata.create.device_id));
2499 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
2502 req.args = nvlist_pack(option_list, &req.args_len);
2503 if (req.args == NULL) {
2504 warn("%s: error packing nvlist", __func__);
2509 retval = ioctl(fd, CTL_LUN_REQ, &req);
2512 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2517 switch (req.status) {
2519 warnx("LUN creation error: %s", req.error_str);
2522 case CTL_LUN_WARNING:
2523 warnx("LUN creation warning: %s", req.error_str);
2528 warnx("unknown LUN creation status: %d", req.status);
2533 fprintf(stdout, "LUN created successfully\n");
2534 fprintf(stdout, "backend: %s\n", req.backend);
2535 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
2536 fprintf(stdout, "LUN size: %ju bytes\n",
2537 (uintmax_t)req.reqdata.create.lun_size_bytes);
2538 fprintf(stdout, "blocksize %u bytes\n",
2539 req.reqdata.create.blocksize_bytes);
2540 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
2541 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
2542 fprintf(stdout, "Device ID: %s\n", req.reqdata.create.device_id);
2545 nvlist_destroy(option_list);
2550 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
2552 struct ctl_lun_req req;
2553 uint32_t lun_id = 0;
2555 char *backend_name = NULL;
2556 nvlist_t *option_list;
2559 option_list = nvlist_create(0);
2560 if (option_list == NULL)
2561 err(1, "%s: unable to allocate nvlist", __func__);
2563 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2566 backend_name = strdup(optarg);
2569 lun_id = strtoul(optarg, NULL, 0);
2576 tmpstr = strdup(optarg);
2577 name = strsep(&tmpstr, "=");
2579 warnx("%s: option -o takes \"name=value\""
2580 "argument", __func__);
2584 value = strsep(&tmpstr, "=");
2585 if (value == NULL) {
2586 warnx("%s: option -o takes \"name=value\""
2587 "argument", __func__);
2592 nvlist_add_string(option_list, name, value);
2600 if (backend_name == NULL)
2601 errx(1, "%s: backend name (-b) must be specified", __func__);
2603 if (lun_id_set == 0)
2604 errx(1, "%s: LUN id (-l) must be specified", __func__);
2606 bzero(&req, sizeof(req));
2608 strlcpy(req.backend, backend_name, sizeof(req.backend));
2609 req.reqtype = CTL_LUNREQ_RM;
2611 req.reqdata.rm.lun_id = lun_id;
2613 req.args = nvlist_pack(option_list, &req.args_len);
2614 if (req.args == NULL) {
2615 warn("%s: error packing nvlist", __func__);
2620 retval = ioctl(fd, CTL_LUN_REQ, &req);
2623 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2628 switch (req.status) {
2630 warnx("LUN removal error: %s", req.error_str);
2633 case CTL_LUN_WARNING:
2634 warnx("LUN removal warning: %s", req.error_str);
2639 warnx("unknown LUN removal status: %d", req.status);
2644 printf("LUN %d removed successfully\n", lun_id);
2647 nvlist_destroy(option_list);
2652 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
2654 struct ctl_lun_req req;
2655 uint64_t lun_size = 0;
2656 uint32_t lun_id = 0;
2657 int lun_id_set = 0, lun_size_set = 0;
2658 char *backend_name = NULL;
2659 nvlist_t *option_list;
2662 option_list = nvlist_create(0);
2663 if (option_list == NULL)
2664 err(1, "%s: unable to allocate nvlist", __func__);
2666 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2669 backend_name = strdup(optarg);
2672 lun_id = strtoul(optarg, NULL, 0);
2679 tmpstr = strdup(optarg);
2680 name = strsep(&tmpstr, "=");
2682 warnx("%s: option -o takes \"name=value\""
2683 "argument", __func__);
2687 value = strsep(&tmpstr, "=");
2688 if (value == NULL) {
2689 warnx("%s: option -o takes \"name=value\""
2690 "argument", __func__);
2695 nvlist_add_string(option_list, name, value);
2699 if (strcasecmp(optarg, "auto") != 0) {
2700 retval = expand_number(optarg, &lun_size);
2702 warn("%s: invalid -s argument",
2715 if (backend_name == NULL)
2716 errx(1, "%s: backend name (-b) must be specified", __func__);
2718 if (lun_id_set == 0)
2719 errx(1, "%s: LUN id (-l) must be specified", __func__);
2721 if (lun_size_set == 0 && nvlist_empty(option_list))
2722 errx(1, "%s: size (-s) or options (-o) must be specified",
2725 bzero(&req, sizeof(req));
2727 strlcpy(req.backend, backend_name, sizeof(req.backend));
2728 req.reqtype = CTL_LUNREQ_MODIFY;
2730 req.reqdata.modify.lun_id = lun_id;
2731 req.reqdata.modify.lun_size_bytes = lun_size;
2733 req.args = nvlist_pack(option_list, &req.args_len);
2734 if (req.args == NULL) {
2735 warn("%s: error packing nvlist", __func__);
2740 retval = ioctl(fd, CTL_LUN_REQ, &req);
2743 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2748 switch (req.status) {
2750 warnx("LUN modification error: %s", req.error_str);
2753 case CTL_LUN_WARNING:
2754 warnx("LUN modification warning: %s", req.error_str);
2759 warnx("unknown LUN modification status: %d", req.status);
2764 printf("LUN %d modified successfully\n", lun_id);
2767 nvlist_destroy(option_list);
2771 struct cctl_islist_conn {
2774 char *initiator_addr;
2775 char *initiator_alias;
2778 char *header_digest;
2780 char *max_recv_data_segment_length;
2781 char *max_send_data_segment_length;
2782 char *max_burst_length;
2783 char *first_burst_length;
2787 STAILQ_ENTRY(cctl_islist_conn) links;
2790 struct cctl_islist_data {
2792 STAILQ_HEAD(,cctl_islist_conn) conn_list;
2793 struct cctl_islist_conn *cur_conn;
2795 struct sbuf *cur_sb[32];
2799 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
2802 struct cctl_islist_data *islist;
2803 struct cctl_islist_conn *cur_conn;
2805 islist = (struct cctl_islist_data *)user_data;
2806 cur_conn = islist->cur_conn;
2808 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
2809 sizeof(islist->cur_sb[0])))
2810 errx(1, "%s: too many nesting levels, %zd max", __func__,
2811 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
2813 islist->cur_sb[islist->level] = sbuf_new_auto();
2814 if (islist->cur_sb[islist->level] == NULL)
2815 err(1, "%s: Unable to allocate sbuf", __func__);
2817 if (strcmp(name, "connection") == 0) {
2818 if (cur_conn != NULL)
2819 errx(1, "%s: improper connection element nesting",
2822 cur_conn = calloc(1, sizeof(*cur_conn));
2823 if (cur_conn == NULL)
2824 err(1, "%s: cannot allocate %zd bytes", __func__,
2827 islist->num_conns++;
2828 islist->cur_conn = cur_conn;
2830 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
2832 for (i = 0; attr[i] != NULL; i += 2) {
2833 if (strcmp(attr[i], "id") == 0) {
2834 cur_conn->connection_id =
2835 strtoull(attr[i+1], NULL, 0);
2838 "%s: invalid connection attribute %s = %s",
2839 __func__, attr[i], attr[i+1]);
2846 cctl_islist_end_element(void *user_data, const char *name)
2848 struct cctl_islist_data *islist;
2849 struct cctl_islist_conn *cur_conn;
2852 islist = (struct cctl_islist_data *)user_data;
2853 cur_conn = islist->cur_conn;
2855 if ((cur_conn == NULL)
2856 && (strcmp(name, "ctlislist") != 0))
2857 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
2859 if (islist->cur_sb[islist->level] == NULL)
2860 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
2861 islist->level, name);
2863 sbuf_finish(islist->cur_sb[islist->level]);
2864 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
2866 err(1, "%s can't allocate %zd bytes for string", __func__,
2867 sbuf_len(islist->cur_sb[islist->level]));
2869 sbuf_delete(islist->cur_sb[islist->level]);
2870 islist->cur_sb[islist->level] = NULL;
2873 if (strcmp(name, "initiator") == 0) {
2874 cur_conn->initiator = str;
2876 } else if (strcmp(name, "initiator_addr") == 0) {
2877 cur_conn->initiator_addr = str;
2879 } else if (strcmp(name, "initiator_alias") == 0) {
2880 cur_conn->initiator_alias = str;
2882 } else if (strcmp(name, "target") == 0) {
2883 cur_conn->target = str;
2885 } else if (strcmp(name, "target_alias") == 0) {
2886 cur_conn->target_alias = str;
2888 } else if (strcmp(name, "target_portal_group_tag") == 0) {
2889 } else if (strcmp(name, "header_digest") == 0) {
2890 cur_conn->header_digest = str;
2892 } else if (strcmp(name, "data_digest") == 0) {
2893 cur_conn->data_digest = str;
2895 } else if (strcmp(name, "max_recv_data_segment_length") == 0) {
2896 cur_conn->max_recv_data_segment_length = str;
2898 } else if (strcmp(name, "max_send_data_segment_length") == 0) {
2899 cur_conn->max_send_data_segment_length = str;
2901 } else if (strcmp(name, "max_burst_length") == 0) {
2902 cur_conn->max_burst_length = str;
2904 } else if (strcmp(name, "first_burst_length") == 0) {
2905 cur_conn->first_burst_length = str;
2907 } else if (strcmp(name, "offload") == 0) {
2908 cur_conn->offload = str;
2910 } else if (strcmp(name, "immediate_data") == 0) {
2911 cur_conn->immediate_data = atoi(str);
2912 } else if (strcmp(name, "iser") == 0) {
2913 cur_conn->iser = atoi(str);
2914 } else if (strcmp(name, "connection") == 0) {
2915 islist->cur_conn = NULL;
2916 } else if (strcmp(name, "ctlislist") == 0) {
2920 * Unknown element; ignore it for forward compatibility.
2928 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
2930 struct cctl_islist_data *islist;
2932 islist = (struct cctl_islist_data *)user_data;
2934 sbuf_bcat(islist->cur_sb[islist->level], str, len);
2938 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
2940 struct ctl_iscsi req;
2941 struct cctl_islist_data islist;
2942 struct cctl_islist_conn *conn;
2947 int c, retval, verbose = 0;
2952 bzero(&islist, sizeof(islist));
2953 STAILQ_INIT(&islist.conn_list);
2955 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2969 conn_str = malloc(conn_len);
2971 bzero(&req, sizeof(req));
2972 req.type = CTL_ISCSI_LIST;
2973 req.data.list.alloc_len = conn_len;
2974 req.data.list.conn_xml = conn_str;
2976 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
2977 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
2982 if (req.status == CTL_ISCSI_ERROR) {
2983 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
2984 __func__, req.error_str);
2985 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
2986 conn_len = conn_len << 1;
2990 if (dump_xml != 0) {
2991 printf("%s", conn_str);
2995 parser = XML_ParserCreate(NULL);
2996 if (parser == NULL) {
2997 warn("%s: Unable to create XML parser", __func__);
3002 XML_SetUserData(parser, &islist);
3003 XML_SetElementHandler(parser, cctl_islist_start_element,
3004 cctl_islist_end_element);
3005 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3007 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3009 warnx("%s: Unable to parse XML: Error %d", __func__,
3010 XML_GetErrorCode(parser));
3011 XML_ParserFree(parser);
3016 XML_ParserFree(parser);
3019 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3020 printf("%-25s %d\n", "Session ID:", conn->connection_id);
3021 printf("%-25s %s\n", "Initiator name:", conn->initiator);
3022 printf("%-25s %s\n", "Initiator portal:", conn->initiator_addr);
3023 printf("%-25s %s\n", "Initiator alias:", conn->initiator_alias);
3024 printf("%-25s %s\n", "Target name:", conn->target);
3025 printf("%-25s %s\n", "Target alias:", conn->target_alias);
3026 printf("%-25s %s\n", "Header digest:", conn->header_digest);
3027 printf("%-25s %s\n", "Data digest:", conn->data_digest);
3028 printf("%-25s %s\n", "MaxRecvDataSegmentLength:", conn->max_recv_data_segment_length);
3029 printf("%-25s %s\n", "MaxSendDataSegmentLength:", conn->max_send_data_segment_length);
3030 printf("%-25s %s\n", "MaxBurstLen:", conn->max_burst_length);
3031 printf("%-25s %s\n", "FirstBurstLen:", conn->first_burst_length);
3032 printf("%-25s %s\n", "ImmediateData:", conn->immediate_data ? "Yes" : "No");
3033 printf("%-25s %s\n", "iSER (RDMA):", conn->iser ? "Yes" : "No");
3034 printf("%-25s %s\n", "Offload driver:", conn->offload);
3038 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3040 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3041 printf("%4u %-16s %-36s %-36s\n",
3042 conn->connection_id, conn->initiator_addr, conn->initiator,
3053 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3055 struct ctl_iscsi req;
3057 int all = 0, connection_id = -1, nargs = 0;
3058 char *initiator_name = NULL, *initiator_addr = NULL;
3060 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3067 connection_id = strtoul(optarg, NULL, 0);
3071 initiator_name = strdup(optarg);
3072 if (initiator_name == NULL)
3073 err(1, "%s: strdup", __func__);
3077 initiator_addr = strdup(optarg);
3078 if (initiator_addr == NULL)
3079 err(1, "%s: strdup", __func__);
3088 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3091 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3094 bzero(&req, sizeof(req));
3095 req.type = CTL_ISCSI_LOGOUT;
3096 req.data.logout.connection_id = connection_id;
3097 if (initiator_addr != NULL)
3098 strlcpy(req.data.logout.initiator_addr,
3099 initiator_addr, sizeof(req.data.logout.initiator_addr));
3100 if (initiator_name != NULL)
3101 strlcpy(req.data.logout.initiator_name,
3102 initiator_name, sizeof(req.data.logout.initiator_name));
3104 req.data.logout.all = 1;
3106 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3107 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3112 if (req.status != CTL_ISCSI_OK) {
3113 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3114 __func__, req.error_str);
3119 printf("iSCSI logout requests submitted\n");
3126 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3128 struct ctl_iscsi req;
3130 int all = 0, connection_id = -1, nargs = 0;
3131 char *initiator_name = NULL, *initiator_addr = NULL;
3133 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3140 connection_id = strtoul(optarg, NULL, 0);
3144 initiator_name = strdup(optarg);
3145 if (initiator_name == NULL)
3146 err(1, "%s: strdup", __func__);
3150 initiator_addr = strdup(optarg);
3151 if (initiator_addr == NULL)
3152 err(1, "%s: strdup", __func__);
3161 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3164 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3167 bzero(&req, sizeof(req));
3168 req.type = CTL_ISCSI_TERMINATE;
3169 req.data.terminate.connection_id = connection_id;
3170 if (initiator_addr != NULL)
3171 strlcpy(req.data.terminate.initiator_addr,
3172 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3173 if (initiator_name != NULL)
3174 strlcpy(req.data.terminate.initiator_name,
3175 initiator_name, sizeof(req.data.terminate.initiator_name));
3177 req.data.terminate.all = 1;
3179 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3180 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3185 if (req.status != CTL_ISCSI_OK) {
3186 warnx("%s: error returned from CTL iSCSI connection "
3187 "termination request:\n%s", __func__, req.error_str);
3192 printf("iSCSI connections terminated\n");
3199 * Name/value pair used for per-LUN attributes.
3201 struct cctl_lun_nv {
3204 STAILQ_ENTRY(cctl_lun_nv) links;
3208 * Backend LUN information.
3213 uint64_t size_blocks;
3215 char *serial_number;
3217 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3218 STAILQ_ENTRY(cctl_lun) links;
3221 struct cctl_devlist_data {
3223 STAILQ_HEAD(,cctl_lun) lun_list;
3224 struct cctl_lun *cur_lun;
3226 struct sbuf *cur_sb[32];
3230 cctl_start_element(void *user_data, const char *name, const char **attr)
3233 struct cctl_devlist_data *devlist;
3234 struct cctl_lun *cur_lun;
3236 devlist = (struct cctl_devlist_data *)user_data;
3237 cur_lun = devlist->cur_lun;
3239 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3240 sizeof(devlist->cur_sb[0])))
3241 errx(1, "%s: too many nesting levels, %zd max", __func__,
3242 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3244 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3245 if (devlist->cur_sb[devlist->level] == NULL)
3246 err(1, "%s: Unable to allocate sbuf", __func__);
3248 if (strcmp(name, "lun") == 0) {
3249 if (cur_lun != NULL)
3250 errx(1, "%s: improper lun element nesting", __func__);
3252 cur_lun = calloc(1, sizeof(*cur_lun));
3253 if (cur_lun == NULL)
3254 err(1, "%s: cannot allocate %zd bytes", __func__,
3257 devlist->num_luns++;
3258 devlist->cur_lun = cur_lun;
3260 STAILQ_INIT(&cur_lun->attr_list);
3261 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3263 for (i = 0; attr[i] != NULL; i += 2) {
3264 if (strcmp(attr[i], "id") == 0) {
3265 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3267 errx(1, "%s: invalid LUN attribute %s = %s",
3268 __func__, attr[i], attr[i+1]);
3275 cctl_end_element(void *user_data, const char *name)
3277 struct cctl_devlist_data *devlist;
3278 struct cctl_lun *cur_lun;
3281 devlist = (struct cctl_devlist_data *)user_data;
3282 cur_lun = devlist->cur_lun;
3284 if ((cur_lun == NULL)
3285 && (strcmp(name, "ctllunlist") != 0))
3286 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3288 if (devlist->cur_sb[devlist->level] == NULL)
3289 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3290 devlist->level, name);
3292 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3293 err(1, "%s: sbuf_finish", __func__);
3294 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3296 err(1, "%s can't allocate %zd bytes for string", __func__,
3297 sbuf_len(devlist->cur_sb[devlist->level]));
3299 if (strlen(str) == 0) {
3304 sbuf_delete(devlist->cur_sb[devlist->level]);
3305 devlist->cur_sb[devlist->level] = NULL;
3308 if (strcmp(name, "backend_type") == 0) {
3309 cur_lun->backend_type = str;
3311 } else if (strcmp(name, "size") == 0) {
3312 cur_lun->size_blocks = strtoull(str, NULL, 0);
3313 } else if (strcmp(name, "blocksize") == 0) {
3314 cur_lun->blocksize = strtoul(str, NULL, 0);
3315 } else if (strcmp(name, "serial_number") == 0) {
3316 cur_lun->serial_number = str;
3318 } else if (strcmp(name, "device_id") == 0) {
3319 cur_lun->device_id = str;
3321 } else if (strcmp(name, "lun") == 0) {
3322 devlist->cur_lun = NULL;
3323 } else if (strcmp(name, "ctllunlist") == 0) {
3326 struct cctl_lun_nv *nv;
3328 nv = calloc(1, sizeof(*nv));
3330 err(1, "%s: can't allocate %zd bytes for nv pair",
3331 __func__, sizeof(*nv));
3333 nv->name = strdup(name);
3334 if (nv->name == NULL)
3335 err(1, "%s: can't allocated %zd bytes for string",
3336 __func__, strlen(name));
3340 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3347 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3349 struct cctl_devlist_data *devlist;
3351 devlist = (struct cctl_devlist_data *)user_data;
3353 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3357 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3359 struct ctl_lun_list list;
3360 struct cctl_devlist_data devlist;
3361 struct cctl_lun *lun;
3367 char *backend = NULL;
3373 bzero(&devlist, sizeof(devlist));
3374 STAILQ_INIT(&devlist.lun_list);
3376 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3379 backend = strdup(optarg);
3393 lun_str = malloc(lun_len);
3395 bzero(&list, sizeof(list));
3396 list.alloc_len = lun_len;
3397 list.status = CTL_LUN_LIST_NONE;
3398 list.lun_xml = lun_str;
3400 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
3401 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
3406 if (list.status == CTL_LUN_LIST_ERROR) {
3407 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
3408 __func__, list.error_str);
3409 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3410 lun_len = lun_len << 1;
3414 if (dump_xml != 0) {
3415 printf("%s", lun_str);
3419 parser = XML_ParserCreate(NULL);
3420 if (parser == NULL) {
3421 warn("%s: Unable to create XML parser", __func__);
3426 XML_SetUserData(parser, &devlist);
3427 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
3428 XML_SetCharacterDataHandler(parser, cctl_char_handler);
3430 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
3432 warnx("%s: Unable to parse XML: Error %d", __func__,
3433 XML_GetErrorCode(parser));
3434 XML_ParserFree(parser);
3439 XML_ParserFree(parser);
3441 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
3442 "Serial Number", "Device ID");
3443 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
3444 struct cctl_lun_nv *nv;
3446 if ((backend != NULL)
3447 && (strcmp(lun->backend_type, backend) != 0))
3450 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
3451 (uintmax_t)lun->lun_id,
3452 lun->backend_type, (uintmax_t)lun->size_blocks,
3453 lun->blocksize, lun->serial_number, lun->device_id);
3458 STAILQ_FOREACH(nv, &lun->attr_list, links) {
3459 printf(" %s=%s\n", nv->name, nv->value);
3474 char *frontend_type;
3477 char *target, *port, *lun_map;
3478 STAILQ_HEAD(,cctl_lun_nv) init_list;
3479 STAILQ_HEAD(,cctl_lun_nv) lun_list;
3480 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3481 STAILQ_ENTRY(cctl_port) links;
3484 struct cctl_portlist_data {
3486 STAILQ_HEAD(,cctl_port) port_list;
3487 struct cctl_port *cur_port;
3490 struct sbuf *cur_sb[32];
3494 cctl_start_pelement(void *user_data, const char *name, const char **attr)
3497 struct cctl_portlist_data *portlist;
3498 struct cctl_port *cur_port;
3500 portlist = (struct cctl_portlist_data *)user_data;
3501 cur_port = portlist->cur_port;
3503 if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
3504 sizeof(portlist->cur_sb[0])))
3505 errx(1, "%s: too many nesting levels, %zd max", __func__,
3506 sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
3508 portlist->cur_sb[portlist->level] = sbuf_new_auto();
3509 if (portlist->cur_sb[portlist->level] == NULL)
3510 err(1, "%s: Unable to allocate sbuf", __func__);
3512 portlist->cur_id = 0;
3513 for (i = 0; attr[i] != NULL; i += 2) {
3514 if (strcmp(attr[i], "id") == 0) {
3515 portlist->cur_id = strtoull(attr[i+1], NULL, 0);
3520 if (strcmp(name, "targ_port") == 0) {
3521 if (cur_port != NULL)
3522 errx(1, "%s: improper port element nesting", __func__);
3524 cur_port = calloc(1, sizeof(*cur_port));
3525 if (cur_port == NULL)
3526 err(1, "%s: cannot allocate %zd bytes", __func__,
3529 portlist->num_ports++;
3530 portlist->cur_port = cur_port;
3532 STAILQ_INIT(&cur_port->init_list);
3533 STAILQ_INIT(&cur_port->lun_list);
3534 STAILQ_INIT(&cur_port->attr_list);
3535 cur_port->port_id = portlist->cur_id;
3536 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
3541 cctl_end_pelement(void *user_data, const char *name)
3543 struct cctl_portlist_data *portlist;
3544 struct cctl_port *cur_port;
3547 portlist = (struct cctl_portlist_data *)user_data;
3548 cur_port = portlist->cur_port;
3550 if ((cur_port == NULL)
3551 && (strcmp(name, "ctlportlist") != 0))
3552 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
3554 if (portlist->cur_sb[portlist->level] == NULL)
3555 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3556 portlist->level, name);
3558 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
3559 err(1, "%s: sbuf_finish", __func__);
3560 str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
3562 err(1, "%s can't allocate %zd bytes for string", __func__,
3563 sbuf_len(portlist->cur_sb[portlist->level]));
3565 if (strlen(str) == 0) {
3570 sbuf_delete(portlist->cur_sb[portlist->level]);
3571 portlist->cur_sb[portlist->level] = NULL;
3574 if (strcmp(name, "frontend_type") == 0) {
3575 cur_port->frontend_type = str;
3577 } else if (strcmp(name, "port_name") == 0) {
3578 cur_port->name = str;
3580 } else if (strcmp(name, "online") == 0) {
3581 cur_port->online = str;
3583 } else if (strcmp(name, "physical_port") == 0) {
3584 cur_port->pp = strtoull(str, NULL, 0);
3585 } else if (strcmp(name, "virtual_port") == 0) {
3586 cur_port->vp = strtoull(str, NULL, 0);
3587 } else if (strcmp(name, "target") == 0) {
3588 cur_port->target = str;
3590 } else if (strcmp(name, "port") == 0) {
3591 cur_port->port = str;
3593 } else if (strcmp(name, "lun_map") == 0) {
3594 cur_port->lun_map = str;
3596 } else if (strcmp(name, "targ_port") == 0) {
3597 portlist->cur_port = NULL;
3598 } else if (strcmp(name, "ctlportlist") == 0) {
3601 struct cctl_lun_nv *nv;
3603 nv = calloc(1, sizeof(*nv));
3605 err(1, "%s: can't allocate %zd bytes for nv pair",
3606 __func__, sizeof(*nv));
3608 if (strcmp(name, "initiator") == 0 ||
3609 strcmp(name, "lun") == 0)
3610 asprintf(&nv->name, "%ju", portlist->cur_id);
3612 nv->name = strdup(name);
3613 if (nv->name == NULL)
3614 err(1, "%s: can't allocated %zd bytes for string",
3615 __func__, strlen(name));
3619 if (strcmp(name, "initiator") == 0)
3620 STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links);
3621 else if (strcmp(name, "lun") == 0)
3622 STAILQ_INSERT_TAIL(&cur_port->lun_list, nv, links);
3624 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
3631 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
3633 struct cctl_portlist_data *portlist;
3635 portlist = (struct cctl_portlist_data *)user_data;
3637 sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
3641 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
3643 struct ctl_lun_list list;
3644 struct cctl_portlist_data portlist;
3645 struct cctl_port *port;
3647 char *port_str = NULL;
3651 char *frontend = NULL;
3652 uint64_t portarg = UINT64_MAX;
3653 int verbose = 0, init = 0, lun = 0, quiet = 0;
3658 bzero(&portlist, sizeof(portlist));
3659 STAILQ_INIT(&portlist.port_list);
3661 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3664 frontend = strdup(optarg);
3673 portarg = strtoll(optarg, NULL, 0);
3690 port_str = (char *)realloc(port_str, port_len);
3692 bzero(&list, sizeof(list));
3693 list.alloc_len = port_len;
3694 list.status = CTL_LUN_LIST_NONE;
3695 list.lun_xml = port_str;
3697 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
3698 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
3703 if (list.status == CTL_LUN_LIST_ERROR) {
3704 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
3705 __func__, list.error_str);
3706 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3707 port_len = port_len << 1;
3711 if (dump_xml != 0) {
3712 printf("%s", port_str);
3716 parser = XML_ParserCreate(NULL);
3717 if (parser == NULL) {
3718 warn("%s: Unable to create XML parser", __func__);
3723 XML_SetUserData(parser, &portlist);
3724 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
3725 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
3727 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
3729 warnx("%s: Unable to parse XML: Error %d", __func__,
3730 XML_GetErrorCode(parser));
3731 XML_ParserFree(parser);
3736 XML_ParserFree(parser);
3739 printf("Port Online Frontend Name pp vp\n");
3740 STAILQ_FOREACH(port, &portlist.port_list, links) {
3741 struct cctl_lun_nv *nv;
3743 if ((frontend != NULL)
3744 && (strcmp(port->frontend_type, frontend) != 0))
3747 if ((portarg != UINT64_MAX) && (portarg != port->port_id))
3750 printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n",
3751 (uintmax_t)port->port_id, port->online,
3752 port->frontend_type, port->name, port->pp, port->vp,
3753 port->port ? port->port : "");
3755 if (init || verbose) {
3757 printf(" Target: %s\n", port->target);
3758 STAILQ_FOREACH(nv, &port->init_list, links) {
3759 printf(" Initiator %s: %s\n",
3760 nv->name, nv->value);
3764 if (lun || verbose) {
3765 if (port->lun_map) {
3766 STAILQ_FOREACH(nv, &port->lun_list, links)
3767 printf(" LUN %s: %s\n",
3768 nv->name, nv->value);
3769 if (STAILQ_EMPTY(&port->lun_list))
3770 printf(" No LUNs mapped\n");
3772 printf(" All LUNs mapped\n");
3776 STAILQ_FOREACH(nv, &port->attr_list, links) {
3777 printf(" %s=%s\n", nv->name, nv->value);
3788 cctl_lunmap(int fd, int argc, char **argv, char *combinedopt)
3790 struct ctl_lun_map lm;
3794 lm.port = UINT32_MAX;
3795 lm.plun = UINT32_MAX;
3796 lm.lun = UINT32_MAX;
3798 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3801 lm.port = strtoll(optarg, NULL, 0);
3804 lm.plun = strtoll(optarg, NULL, 0);
3807 lm.lun = strtoll(optarg, NULL, 0);
3814 if (ioctl(fd, CTL_LUN_MAP, &lm) == -1) {
3815 warn("%s: error issuing CTL_LUN_MAP ioctl", __func__);
3825 fprintf(error ? stderr : stdout,
3827 "Primary commands:\n"
3828 " ctladm tur [dev_id][general options]\n"
3829 " ctladm inquiry [dev_id][general options]\n"
3830 " ctladm devid [dev_id][general options]\n"
3831 " ctladm reqsense [dev_id][general options]\n"
3832 " ctladm reportluns [dev_id][general options]\n"
3833 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
3834 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3835 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
3836 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3837 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
3838 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
3839 " [-d] [-S subpage] [-c cdbsize]\n"
3840 " ctladm prin [dev_id][general options] <-a action>\n"
3841 " ctladm prout [dev_id][general options] <-a action>\n"
3842 " <-r restype] [-k key] [-s sa_key]\n"
3843 " ctladm rtpg [dev_id][general options]\n"
3844 " ctladm start [dev_id][general options] [-i] [-o]\n"
3845 " ctladm stop [dev_id][general options] [-i] [-o]\n"
3846 " ctladm synccache [dev_id][general options] [-l lba]\n"
3847 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
3848 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
3849 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
3850 " [-S serial_num] [-t dev_type]\n"
3851 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
3852 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
3853 " ctladm devlist [-b backend] [-v] [-x]\n"
3855 " ctladm lunmap -p targ_port [-l pLUN] [-L cLUN]\n"
3856 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
3858 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
3859 " [-s len fmt [args]] [-c] [-d delete_id]\n"
3860 " ctladm port <-o <on|off> | [-w wwnn][-W wwpn]>\n"
3861 " [-p targ_port] [-t port_type]\n"
3862 " <-c> [-d driver] [-O name=value]\n"
3863 " <-r> <-p targ_port>\n"
3864 " ctladm portlist [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n"
3865 " ctladm islist [-v | -x]\n"
3866 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
3867 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
3869 " ctladm dumpstructs\n"
3871 "General Options:\n"
3872 "-I intiator_id : defaults to 7, used to change the initiator id\n"
3873 "-C retries : specify the number of times to retry this command\n"
3874 "-D devicename : specify the device to operate on\n"
3875 " : (default is %s)\n"
3876 "read/write options:\n"
3877 "-l lba : logical block address\n"
3878 "-d len : read/write length, in blocks\n"
3879 "-f file|- : write/read data to/from file or stdout/stdin\n"
3880 "-b blocksize : block size, in bytes\n"
3881 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
3882 "-N : do not copy data to/from userland\n"
3883 "readcapacity options:\n"
3884 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3885 "modesense options:\n"
3886 "-m page : specify the mode page to view\n"
3887 "-l : request a list of supported pages\n"
3888 "-P pc : specify the page control value: 0-3 (current,\n"
3889 " changeable, default, saved, respectively)\n"
3890 "-d : disable block descriptors for mode sense\n"
3891 "-S subpage : specify a subpage\n"
3892 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
3893 "persistent reserve in options:\n"
3894 "-a action : specify the action value: 0-2 (read key, read\n"
3895 " reservation, read capabilities, respectively)\n"
3896 "persistent reserve out options:\n"
3897 "-a action : specify the action value: 0-5 (register, reserve,\n"
3898 " release, clear, preempt, register and ignore)\n"
3899 "-k key : key value\n"
3900 "-s sa_key : service action value\n"
3901 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
3902 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
3903 "start/stop options:\n"
3904 "-i : set the immediate bit (CTL does not support this)\n"
3905 "-o : set the on/offline bit\n"
3906 "synccache options:\n"
3907 "-l lba : set the starting LBA\n"
3908 "-b blockcount : set the length to sync in blocks\n"
3909 "-r : set the relative addressing bit\n"
3910 "-i : set the immediate bit\n"
3911 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3913 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3914 "-B blocksize : LUN blocksize in bytes (some backends)\n"
3915 "-d device_id : SCSI VPD page 0x83 ID\n"
3916 "-l lun_id : requested LUN number\n"
3917 "-o name=value : backend-specific options, multiple allowed\n"
3918 "-s size_bytes : LUN size in bytes (some backends)\n"
3919 "-S serial_num : SCSI VPD page 0x80 serial number\n"
3920 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
3922 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3923 "-l lun_id : LUN number to delete\n"
3924 "-o name=value : backend-specific options, multiple allowed\n"
3925 "devlist options:\n"
3926 "-b backend : list devices from specified backend only\n"
3927 "-v : be verbose, show backend attributes\n"
3928 "-x : dump raw XML\n"
3930 "-l datamove|done : delay command at datamove or done phase\n"
3931 "-T oneshot : delay one command, then resume normal completion\n"
3932 "-T cont : delay all commands\n"
3933 "-t secs : number of seconds to delay\n"
3935 "-i error_action : action to perform\n"
3936 "-p pattern : command pattern to look for\n"
3937 "-r lba,len : LBA range for pattern\n"
3938 "-s len fmt [args] : sense data for custom sense action\n"
3939 "-c : continuous operation\n"
3940 "-d delete_id : error id to delete\n"
3942 "-c : create new ioctl or iscsi frontend port\n"
3943 "-d : specify ioctl or iscsi frontend type\n"
3944 "-l : list frontend ports\n"
3945 "-o on|off : turn frontend ports on or off\n"
3946 "-O pp|vp : create new frontend port using pp and/or vp\n"
3947 "-w wwnn : set WWNN for one frontend\n"
3948 "-W wwpn : set WWPN for one frontend\n"
3949 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
3950 "-p targ_port : specify target port number\n"
3951 "-r : remove frontend port\n"
3952 "-q : omit header in list output\n"
3953 "-x : output port list in XML format\n"
3954 "portlist options:\n"
3955 "-f frontend : specify frontend type\n"
3956 "-i : report target and initiators addresses\n"
3957 "-l : report LUN mapping\n"
3958 "-p targ_port : specify target port number\n"
3959 "-q : omit header in list output\n"
3960 "-v : verbose output (report all port options)\n"
3961 "-x : output port list in XML format\n"
3963 "-p targ_port : specify target port number\n"
3964 "-L pLUN : specify port-visible LUN\n"
3965 "-L cLUN : specify CTL LUN\n",
3970 main(int argc, char **argv)
3973 ctladm_cmdfunction command;
3974 ctladm_cmdargs cmdargs;
3975 ctladm_optret optreturn;
3977 const char *mainopt = "C:D:I:";
3978 const char *subopt = NULL;
3979 char combinedopt[256];
3988 cmdargs = CTLADM_ARG_NONE;
3989 command = CTLADM_CMD_HELP;
4003 * Get the base option.
4005 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4007 if (optreturn == CC_OR_AMBIGUOUS) {
4008 warnx("ambiguous option %s", argv[1]);
4011 } else if (optreturn == CC_OR_NOT_FOUND) {
4012 warnx("option %s not found", argv[1]);
4017 if (cmdargs & CTLADM_ARG_NEED_TL) {
4018 if ((argc < 3) || (!isdigit(argv[2][0]))) {
4019 warnx("option %s requires a lun argument",
4024 lun = strtol(argv[2], NULL, 0);
4026 cmdargs |= CTLADM_ARG_TARG_LUN;
4031 * Ahh, getopt(3) is a pain.
4033 * This is a gross hack. There really aren't many other good
4034 * options (excuse the pun) for parsing options in a situation like
4035 * this. getopt is kinda braindead, so you end up having to run
4036 * through the options twice, and give each invocation of getopt
4037 * the option string for the other invocation.
4039 * You would think that you could just have two groups of options.
4040 * The first group would get parsed by the first invocation of
4041 * getopt, and the second group would get parsed by the second
4042 * invocation of getopt. It doesn't quite work out that way. When
4043 * the first invocation of getopt finishes, it leaves optind pointing
4044 * to the argument _after_ the first argument in the second group.
4045 * So when the second invocation of getopt comes around, it doesn't
4046 * recognize the first argument it gets and then bails out.
4048 * A nice alternative would be to have a flag for getopt that says
4049 * "just keep parsing arguments even when you encounter an unknown
4050 * argument", but there isn't one. So there's no real clean way to
4051 * easily parse two sets of arguments without having one invocation
4052 * of getopt know about the other.
4054 * Without this hack, the first invocation of getopt would work as
4055 * long as the generic arguments are first, but the second invocation
4056 * (in the subfunction) would fail in one of two ways. In the case
4057 * where you don't set optreset, it would fail because optind may be
4058 * pointing to the argument after the one it should be pointing at.
4059 * In the case where you do set optreset, and reset optind, it would
4060 * fail because getopt would run into the first set of options, which
4061 * it doesn't understand.
4063 * All of this would "sort of" work if you could somehow figure out
4064 * whether optind had been incremented one option too far. The
4065 * mechanics of that, however, are more daunting than just giving
4066 * both invocations all of the expect options for either invocation.
4068 * Needless to say, I wouldn't mind if someone invented a better
4069 * (non-GPL!) command line parsing interface than getopt. I
4070 * wouldn't mind if someone added more knobs to getopt to make it
4071 * work better. Who knows, I may talk myself into doing it someday,
4072 * if the standards weenies let me. As it is, it just leads to
4073 * hackery like this and causes people to avoid it in some cases.
4075 * KDM, September 8th, 1998
4078 sprintf(combinedopt, "%s%s", mainopt, subopt);
4080 sprintf(combinedopt, "%s", mainopt);
4083 * Start getopt processing at argv[2/3], since we've already
4084 * accepted argv[1..2] as the command name, and as a possible
4090 * Now we run through the argument list looking for generic
4091 * options, and ignoring options that possibly belong to
4094 while ((c = getopt(argc, argv, combinedopt))!= -1){
4097 cmdargs |= CTLADM_ARG_RETRIES;
4098 retries = strtol(optarg, NULL, 0);
4101 device = strdup(optarg);
4102 cmdargs |= CTLADM_ARG_DEVICE;
4105 cmdargs |= CTLADM_ARG_INITIATOR;
4106 initid = strtol(optarg, NULL, 0);
4113 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4120 * Default to opening the CTL device for now.
4122 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4123 && (command != CTLADM_CMD_HELP)) {
4124 device = strdup(CTL_DEFAULT_DEV);
4125 cmdargs |= CTLADM_ARG_DEVICE;
4128 if ((cmdargs & CTLADM_ARG_DEVICE)
4129 && (command != CTLADM_CMD_HELP)) {
4130 fd = open(device, O_RDWR);
4131 if (fd == -1 && errno == ENOENT) {
4132 saved_errno = errno;
4133 retval = kldload("ctl");
4135 fd = open(device, O_RDWR);
4137 errno = saved_errno;
4140 fprintf(stderr, "%s: error opening %s: %s\n",
4141 argv[0], device, strerror(errno));
4147 if (modfind("cfiscsi") == -1 &&
4148 kldload("cfiscsi") == -1)
4149 warn("couldn't load cfiscsi");
4152 } else if ((command != CTLADM_CMD_HELP)
4153 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4154 fprintf(stderr, "%s: you must specify a device with the "
4155 "--device argument for this command\n", argv[0]);
4156 command = CTLADM_CMD_HELP;
4161 case CTLADM_CMD_TUR:
4162 retval = cctl_tur(fd, lun, initid, retries);
4164 case CTLADM_CMD_INQUIRY:
4165 retval = cctl_inquiry(fd, lun, initid, retries);
4167 case CTLADM_CMD_REQ_SENSE:
4168 retval = cctl_req_sense(fd, lun, initid, retries);
4170 case CTLADM_CMD_REPORT_LUNS:
4171 retval = cctl_report_luns(fd, lun, initid, retries);
4173 case CTLADM_CMD_CREATE:
4174 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4177 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4179 case CTLADM_CMD_DEVLIST:
4180 retval = cctl_devlist(fd, argc, argv, combinedopt);
4182 case CTLADM_CMD_READ:
4183 case CTLADM_CMD_WRITE:
4184 retval = cctl_read_write(fd, lun, initid, retries,
4185 argc, argv, combinedopt, command);
4187 case CTLADM_CMD_PORT:
4188 retval = cctl_port(fd, argc, argv, combinedopt);
4190 case CTLADM_CMD_PORTLIST:
4191 retval = cctl_portlist(fd, argc, argv, combinedopt);
4193 case CTLADM_CMD_LUNMAP:
4194 retval = cctl_lunmap(fd, argc, argv, combinedopt);
4196 case CTLADM_CMD_READCAPACITY:
4197 retval = cctl_read_capacity(fd, lun, initid, retries,
4198 argc, argv, combinedopt);
4200 case CTLADM_CMD_MODESENSE:
4201 retval = cctl_mode_sense(fd, lun, initid, retries,
4202 argc, argv, combinedopt);
4204 case CTLADM_CMD_START:
4205 case CTLADM_CMD_STOP:
4206 retval = cctl_start_stop(fd, lun, initid, retries,
4207 (command == CTLADM_CMD_START) ? 1 : 0,
4208 argc, argv, combinedopt);
4210 case CTLADM_CMD_SYNC_CACHE:
4211 retval = cctl_sync_cache(fd, lun, initid, retries,
4212 argc, argv, combinedopt);
4214 case CTLADM_CMD_LUNLIST:
4215 retval = cctl_lunlist(fd);
4217 case CTLADM_CMD_DELAY:
4218 retval = cctl_delay(fd, lun, argc, argv, combinedopt);
4220 case CTLADM_CMD_ERR_INJECT:
4221 retval = cctl_error_inject(fd, lun, argc, argv,
4224 case CTLADM_CMD_DUMPOOA:
4225 retval = cctl_dump_ooa(fd, argc, argv);
4227 case CTLADM_CMD_DUMPSTRUCTS:
4228 retval = cctl_dump_structs(fd, cmdargs);
4230 case CTLADM_CMD_PRES_IN:
4231 retval = cctl_persistent_reserve_in(fd, lun, initid,
4232 argc, argv, combinedopt,
4235 case CTLADM_CMD_PRES_OUT:
4236 retval = cctl_persistent_reserve_out(fd, lun, initid,
4237 argc, argv, combinedopt,
4240 case CTLADM_CMD_INQ_VPD_DEVID:
4241 retval = cctl_inquiry_vpd_devid(fd, lun, initid);
4243 case CTLADM_CMD_RTPG:
4244 retval = cctl_report_target_port_group(fd, lun, initid);
4246 case CTLADM_CMD_MODIFY:
4247 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4249 case CTLADM_CMD_ISLIST:
4250 retval = cctl_islist(fd, argc, argv, combinedopt);
4252 case CTLADM_CMD_ISLOGOUT:
4253 retval = cctl_islogout(fd, argc, argv, combinedopt);
4255 case CTLADM_CMD_ISTERMINATE:
4256 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4258 case CTLADM_CMD_HELP: