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%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_RTR) ? " RTR" :"",
345 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
347 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
348 (entry->cmd_flags & CTL_OOACMD_FLAG_STATUS_QUEUED) ?
349 " STATUSQUEUED" : "",
350 (entry->cmd_flags & CTL_OOACMD_FLAG_STATUS_SENT) ? " STATUS" : "",
351 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
353 scsi_op_desc(entry->cdb[0], NULL),
354 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
357 fprintf(stdout, "OOA queues dump done\n");
365 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
367 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
380 CCTL_PORT_MODE_CREATE,
381 CCTL_PORT_MODE_REMOVE
384 static struct ctladm_opts cctl_fe_table[] = {
385 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
386 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
387 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
388 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
389 {"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
390 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
395 cctl_port(int fd, int argc, char **argv, char *combinedopt)
398 int32_t targ_port = -1;
400 int wwnn_set = 0, wwpn_set = 0;
401 uint64_t wwnn = 0, wwpn = 0;
402 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
403 struct ctl_port_entry entry;
406 nvlist_t *option_list;
407 ctl_port_type port_type = CTL_PORT_NONE;
408 int quiet = 0, xml = 0;
410 option_list = nvlist_create(0);
411 if (option_list == NULL)
412 err(1, "%s: unable to allocate nvlist", __func__);
414 while ((c = getopt(argc, argv, combinedopt)) != -1) {
417 if (port_mode != CCTL_PORT_MODE_NONE)
420 port_mode = CCTL_PORT_MODE_LIST;
423 port_mode = CCTL_PORT_MODE_CREATE;
426 port_mode = CCTL_PORT_MODE_REMOVE;
429 if (port_mode != CCTL_PORT_MODE_NONE)
432 if (strcasecmp(optarg, "on") == 0)
433 port_mode = CCTL_PORT_MODE_ON;
434 else if (strcasecmp(optarg, "off") == 0)
435 port_mode = CCTL_PORT_MODE_OFF;
437 warnx("Invalid -o argument %s, \"on\" or "
438 "\"off\" are the only valid args",
448 tmpstr = strdup(optarg);
449 name = strsep(&tmpstr, "=");
451 warnx("%s: option -O takes \"name=value\""
452 "argument", __func__);
456 value = strsep(&tmpstr, "=");
458 warnx("%s: option -O takes \"name=value\""
459 "argument", __func__);
465 nvlist_add_string(option_list, name, value);
469 if (driver != NULL) {
470 warnx("%s: option -d cannot be specified twice",
476 driver = strdup(optarg);
479 targ_port = strtol(optarg, NULL, 0);
485 ctladm_optret optret;
486 ctladm_cmdargs argnum;
488 ctl_port_type tmp_port_type;
490 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
492 if (optret == CC_OR_AMBIGUOUS) {
493 warnx("%s: ambiguous frontend type %s",
497 } else if (optret == CC_OR_NOT_FOUND) {
498 warnx("%s: invalid frontend type %s",
504 port_type |= tmp_port_type;
508 if ((port_mode != CCTL_PORT_MODE_NONE)
509 && (port_mode != CCTL_PORT_MODE_SET))
512 port_mode = CCTL_PORT_MODE_SET;
514 wwnn = strtoull(optarg, NULL, 0);
518 if ((port_mode != CCTL_PORT_MODE_NONE)
519 && (port_mode != CCTL_PORT_MODE_SET))
522 port_mode = CCTL_PORT_MODE_SET;
524 wwpn = strtoull(optarg, NULL, 0);
534 driver = strdup("ioctl");
537 * The user can specify either one or more frontend types (-t), or
538 * a specific frontend, but not both.
540 * If the user didn't specify a frontend type or number, set it to
541 * all. This is primarily needed for the enable/disable ioctls.
542 * This will be a no-op for the listing code. For the set ioctl,
543 * we'll throw an error, since that only works on one port at a time.
545 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
546 warnx("%s: can only specify one of -t or -n", __func__);
549 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
550 port_type = CTL_PORT_ALL;
552 bzero(&entry, sizeof(entry));
555 * These are needed for all but list/dump mode.
557 entry.port_type = port_type;
558 entry.targ_port = targ_port;
561 case CCTL_PORT_MODE_LIST: {
571 argvx[argcx++] = argx;
573 argvx[argcx++] = argq;
574 cctl_portlist(fd, argcx, argvx, opts);
577 case CCTL_PORT_MODE_REMOVE:
578 if (targ_port == -1) {
579 warnx("%s: -r require -p", __func__);
583 case CCTL_PORT_MODE_CREATE: {
584 bzero(&req, sizeof(req));
585 strlcpy(req.driver, driver, sizeof(req.driver));
587 if (port_mode == CCTL_PORT_MODE_REMOVE) {
588 req.reqtype = CTL_REQ_REMOVE;
589 nvlist_add_stringf(option_list, "port_id", "%d",
592 req.reqtype = CTL_REQ_CREATE;
594 req.args = nvlist_pack(option_list, &req.args_len);
595 if (req.args == NULL) {
596 warn("%s: error packing nvlist", __func__);
601 retval = ioctl(fd, CTL_PORT_REQ, &req);
604 warn("%s: CTL_PORT_REQ ioctl failed", __func__);
609 switch (req.status) {
611 warnx("error: %s", req.error_str);
614 case CTL_LUN_WARNING:
615 warnx("warning: %s", req.error_str);
620 warnx("unknown status: %d", req.status);
627 case CCTL_PORT_MODE_SET:
628 if (targ_port == -1) {
629 warnx("%s: -w and -W require -n", __func__);
635 entry.flags |= CTL_PORT_WWNN_VALID;
639 entry.flags |= CTL_PORT_WWPN_VALID;
643 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
644 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
649 case CCTL_PORT_MODE_ON:
650 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
651 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
655 fprintf(stdout, "Front End Ports enabled\n");
657 case CCTL_PORT_MODE_OFF:
658 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
659 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
663 fprintf(stdout, "Front End Ports disabled\n");
666 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
673 nvlist_destroy(option_list);
678 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
683 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
686 if (ioctl(fd, CTL_IO, io) == -1) {
687 warn("%s: error sending CTL_IO ioctl", func);
690 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
697 cctl_delay(int fd, int lun, int argc, char **argv,
700 struct ctl_io_delay_info delay_info;
701 char *delayloc = NULL;
702 char *delaytype = NULL;
709 memset(&delay_info, 0, sizeof(delay_info));
711 while ((c = getopt(argc, argv, combinedopt)) != -1) {
714 delaytype = strdup(optarg);
717 delayloc = strdup(optarg);
720 delaytime = strtoul(optarg, NULL, 0);
725 if (delaytime == -1) {
726 warnx("%s: you must specify the delaytime with -t", __func__);
731 if (strcasecmp(delayloc, "datamove") == 0)
732 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
733 else if (strcasecmp(delayloc, "done") == 0)
734 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
736 warnx("%s: invalid delay location %s", __func__, delayloc);
741 if ((delaytype == NULL)
742 || (strcmp(delaytype, "oneshot") == 0))
743 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
744 else if (strcmp(delaytype, "cont") == 0)
745 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
747 warnx("%s: invalid delay type %s", __func__, delaytype);
752 delay_info.lun_id = lun;
753 delay_info.delay_secs = delaytime;
755 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
756 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
760 switch (delay_info.status) {
761 case CTL_DELAY_STATUS_NONE:
762 warnx("%s: no delay status??", __func__);
765 case CTL_DELAY_STATUS_OK:
767 case CTL_DELAY_STATUS_INVALID_LUN:
768 warnx("%s: invalid lun %d", __func__, lun);
771 case CTL_DELAY_STATUS_INVALID_TYPE:
772 warnx("%s: invalid delay type %d", __func__,
773 delay_info.delay_type);
776 case CTL_DELAY_STATUS_INVALID_LOC:
777 warnx("%s: delay location %s not implemented?", __func__,
781 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
782 warnx("%s: delay not implemented in the kernel", __func__);
783 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
787 warnx("%s: unknown delay return status %d", __func__,
799 static struct ctladm_opts cctl_err_types[] = {
800 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
801 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
802 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
803 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
808 static struct ctladm_opts cctl_err_patterns[] = {
809 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
810 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
811 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
812 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
813 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
814 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
815 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
817 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
823 cctl_error_inject(int fd, uint32_t lun, int argc, char **argv,
827 struct ctl_error_desc err_desc;
830 uint64_t delete_id = 0;
831 int delete_id_set = 0;
837 bzero(&err_desc, sizeof(err_desc));
838 err_desc.lun_id = lun;
840 while ((c = getopt(argc, argv, combinedopt)) != -1) {
846 delete_id = strtoull(optarg, NULL, 0);
851 ctladm_optret optret;
852 ctladm_cmdargs argnum;
856 ctl_lun_error err_type;
858 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
859 warnx("%s: can't specify multiple -i "
860 "arguments", __func__);
864 optret = getoption(cctl_err_types, optarg,
865 &err_type, &argnum, &subopt);
866 err_desc.lun_error = err_type;
868 ctl_lun_error_pattern pattern;
870 optret = getoption(cctl_err_patterns, optarg,
871 &pattern, &argnum, &subopt);
872 err_desc.error_pattern |= pattern;
875 if (optret == CC_OR_AMBIGUOUS) {
876 warnx("%s: ambiguous argument %s", __func__,
880 } else if (optret == CC_OR_NOT_FOUND) {
881 warnx("%s: argument %s not found", __func__,
889 char *tmpstr, *tmpstr2;
891 tmpstr = strdup(optarg);
892 if (tmpstr == NULL) {
893 warn("%s: error duplicating string %s",
899 tmpstr2 = strsep(&tmpstr, ",");
900 if (tmpstr2 == NULL) {
901 warnx("%s: invalid -r argument %s", __func__,
907 lba = strtoull(tmpstr2, NULL, 0);
908 tmpstr2 = strsep(&tmpstr, ",");
909 if (tmpstr2 == NULL) {
910 warnx("%s: no len argument for -r lba,len, got"
911 " %s", __func__, optarg);
916 len = strtoul(tmpstr2, NULL, 0);
921 struct get_hook hook;
924 sense_len = strtol(optarg, NULL, 0);
925 if (sense_len <= 0) {
926 warnx("invalid number of sense bytes %d",
932 sense_len = MIN(sense_len, SSD_FULL_SIZE);
934 hook.argc = argc - optind;
935 hook.argv = argv + optind;
938 sensestr = cget(&hook, NULL);
939 if ((sensestr != NULL)
940 && (sensestr[0] == '-')) {
944 (uint8_t *)&err_desc.custom_sense,
945 sense_len, sensestr, iget, &hook);
955 if (delete_id_set != 0) {
956 err_desc.serial = delete_id;
957 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
958 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
965 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
966 warnx("%s: error injection command (-i) needed",
970 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
971 && (sense_len == 0)) {
972 warnx("%s: custom error requires -s", __func__);
978 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
981 * If fd_sense is set, we need to read the sense data the user
982 * wants returned from stdin.
986 int amt_to_read = sense_len;
987 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
989 for (amt_read = 0; amt_to_read > 0;
990 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
991 if (amt_read == -1) {
992 warn("error reading sense data from stdin");
996 amt_to_read -= amt_read;
1001 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1002 warnx("%s: command pattern (-p) needed", __func__);
1008 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1010 * We could check here to see whether it's a read/write
1011 * command, but that will be pointless once we allow
1012 * custom patterns. At that point, the user could specify
1013 * a READ(6) CDB type, and we wouldn't have an easy way here
1014 * to verify whether range checking is possible there. The
1015 * user will just figure it out when his error never gets
1019 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1020 warnx("%s: need read and/or write pattern if range "
1021 "is specified", __func__);
1026 err_desc.lba_range.lba = lba;
1027 err_desc.lba_range.len = len;
1030 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1031 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1034 printf("Error injection succeeded, serial number is %ju\n",
1035 (uintmax_t)err_desc.serial);
1043 cctl_lunlist(int fd)
1045 struct scsi_report_luns_data *lun_data;
1046 struct scsi_inquiry_data *inq_data;
1056 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1057 * if we ever acquire the ability to have multiple targets.
1059 if ((retval = cctl_get_luns(fd, /*lun*/ 0, initid,
1060 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1063 inq_data = malloc(sizeof(*inq_data));
1064 if (inq_data == NULL) {
1065 warn("%s: couldn't allocate memory for inquiry data\n",
1070 for (i = 0; i < num_luns; i++) {
1074 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1075 case RPL_LUNDATA_ATYP_PERIPH:
1076 lun_val = lun_data->luns[i].lundata[1];
1078 case RPL_LUNDATA_ATYP_FLAT:
1079 lun_val = (lun_data->luns[i].lundata[0] &
1080 RPL_LUNDATA_FLAT_LUN_MASK) |
1081 (lun_data->luns[i].lundata[1] <<
1082 RPL_LUNDATA_FLAT_LUN_BITS);
1084 case RPL_LUNDATA_ATYP_LUN:
1085 case RPL_LUNDATA_ATYP_EXTLUN:
1087 fprintf(stdout, "Unsupported LUN format %d\n",
1088 lun_data->luns[i].lundata[0] &
1089 RPL_LUNDATA_ATYP_MASK);
1096 if ((retval = cctl_get_inquiry(fd, lun_val, initid,
1097 /*retries*/ 2, scsi_path,
1102 printf("%s", scsi_path);
1103 scsi_print_inquiry(inq_data);
1107 if (lun_data != NULL)
1110 if (inq_data != NULL)
1117 cctl_sync_cache(int fd, int lun, int iid, int retries,
1118 int argc, char **argv, char *combinedopt)
1123 uint64_t our_lba = 0;
1124 uint32_t our_block_count = 0;
1125 int reladr = 0, immed = 0;
1130 io = ctl_scsi_alloc_io(iid);
1132 warnx("%s: can't allocate memory", __func__);
1136 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1139 our_block_count = strtoul(optarg, NULL, 0);
1142 cdb_size = strtol(optarg, NULL, 0);
1148 our_lba = strtoull(optarg, NULL, 0);
1158 if (cdb_size != -1) {
1164 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1165 "and 16", __func__, cdb_size);
1168 break; /* NOTREACHED */
1173 ctl_scsi_sync_cache(/*io*/ io,
1176 /*minimum_cdb_size*/ cdb_size,
1177 /*starting_lba*/ our_lba,
1178 /*block_count*/ our_block_count,
1179 /*tag_type*/ CTL_TAG_SIMPLE,
1182 io->io_hdr.nexus.targ_lun = lun;
1183 io->io_hdr.nexus.initid = iid;
1185 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1190 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1191 fprintf(stdout, "Cache synchronized successfully\n");
1193 ctl_io_error_print(io, NULL, stderr);
1195 ctl_scsi_free_io(io);
1201 cctl_start_stop(int fd, int lun, int iid, int retries, int start,
1202 int argc, char **argv, char *combinedopt)
1211 io = ctl_scsi_alloc_io(iid);
1213 warnx("%s: can't allocate memory", __func__);
1217 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1227 * Use an ordered tag for the stop command, to guarantee that any
1228 * pending I/O will finish before the stop command executes. This
1229 * would normally be the case anyway, since CTL will basically
1230 * treat the start/stop command as an ordered command with respect
1231 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1233 ctl_scsi_start_stop(/*io*/ io,
1236 /*immediate*/ immed,
1237 /*power_conditions*/ SSS_PC_START_VALID,
1238 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1242 io->io_hdr.nexus.targ_lun = lun;
1243 io->io_hdr.nexus.initid = iid;
1245 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1250 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1251 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1252 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1253 (start) ? "started" : "stopped");
1255 ctl_io_error_print(io, NULL, stderr);
1258 ctl_scsi_free_io(io);
1264 cctl_mode_sense(int fd, int lun, int iid, int retries,
1265 int argc, char **argv, char *combinedopt)
1270 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1279 io = ctl_scsi_alloc_io(iid);
1281 warn("%s: can't allocate memory", __func__);
1285 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1288 pc = strtoul(optarg, NULL, 0);
1291 subpage = strtoul(optarg, NULL, 0);
1300 page_code = strtoul(optarg, NULL, 0);
1303 cdbsize = strtol(optarg, NULL, 0);
1310 if (((list == 0) && (page_code == -1))
1311 || ((list != 0) && (page_code != -1))) {
1312 warnx("%s: you must specify either a page code (-m) or -l",
1318 if ((page_code != -1)
1319 && ((page_code > SMS_ALL_PAGES_PAGE)
1320 || (page_code < 0))) {
1321 warnx("%s: page code %d is out of range", __func__,
1328 page_code = SMS_ALL_PAGES_PAGE;
1330 warnx("%s: arg -P makes no sense with -l",
1335 if (subpage != -1) {
1336 warnx("%s: arg -S makes no sense with -l", __func__);
1343 pc = SMS_PAGE_CTRL_CURRENT;
1347 warnx("%s: page control value %d is out of range: 0-3",
1357 || (subpage < 0))) {
1358 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1369 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1370 "and 10", __func__, cdbsize);
1386 dataptr = (uint8_t *)malloc(datalen);
1387 if (dataptr == NULL) {
1388 warn("%s: can't allocate %d bytes", __func__, datalen);
1393 memset(dataptr, 0, datalen);
1395 ctl_scsi_mode_sense(io,
1396 /*data_ptr*/ dataptr,
1397 /*data_len*/ datalen,
1400 /*page_code*/ page_code,
1402 /*subpage*/ subpage,
1403 /*minimum_cdb_size*/ cdbsize,
1404 /*tag_type*/ CTL_TAG_SIMPLE,
1407 io->io_hdr.nexus.targ_lun = lun;
1408 io->io_hdr.nexus.initid = iid;
1410 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1415 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1416 int pages_len, used_len;
1417 uint32_t returned_len;
1420 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1421 struct scsi_mode_hdr_6 *hdr6;
1424 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1426 returned_len = hdr6->datalen + 1;
1427 bdlen = hdr6->block_descr_len;
1429 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1431 struct scsi_mode_hdr_10 *hdr10;
1434 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
1436 returned_len = scsi_2btoul(hdr10->datalen) + 2;
1437 bdlen = scsi_2btoul(hdr10->block_descr_len);
1439 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
1441 /* just in case they can give us more than we allocated for */
1442 returned_len = min(returned_len, datalen);
1443 pages_len = returned_len - (ndataptr - dataptr);
1445 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
1446 returned_len, pages_len);
1449 fprintf(stdout, "Supported mode pages:\n");
1450 for (used_len = 0; used_len < pages_len;) {
1451 struct scsi_mode_page_header *header;
1453 header = (struct scsi_mode_page_header *)
1454 &ndataptr[used_len];
1455 fprintf(stdout, "%d\n", header->page_code);
1456 used_len += header->page_length + 2;
1459 for (used_len = 0; used_len < pages_len; used_len++) {
1460 fprintf(stdout, "0x%x ", ndataptr[used_len]);
1461 if (((used_len+1) % 16) == 0)
1462 fprintf(stdout, "\n");
1464 fprintf(stdout, "\n");
1467 ctl_io_error_print(io, NULL, stderr);
1470 ctl_scsi_free_io(io);
1472 if (dataptr != NULL)
1479 cctl_read_capacity(int fd, int lun, int iid, int retries,
1480 int argc, char **argv, char *combinedopt)
1483 struct scsi_read_capacity_data *data;
1484 struct scsi_read_capacity_data_long *longdata;
1485 int cdbsize = -1, retval;
1493 io = ctl_scsi_alloc_io(iid);
1495 warn("%s: can't allocate memory\n", __func__);
1499 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1502 cdbsize = strtol(optarg, NULL, 0);
1508 if (cdbsize != -1) {
1514 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1515 "and 16", __func__, cdbsize);
1518 break; /* NOTREACHED */
1523 dataptr = (uint8_t *)malloc(sizeof(*longdata));
1524 if (dataptr == NULL) {
1525 warn("%s: can't allocate %zd bytes\n", __func__,
1530 memset(dataptr, 0, sizeof(*longdata));
1536 ctl_scsi_read_capacity(io,
1537 /*data_ptr*/ dataptr,
1538 /*data_len*/ sizeof(*longdata),
1542 /*tag_type*/ CTL_TAG_SIMPLE,
1546 ctl_scsi_read_capacity_16(io,
1547 /*data_ptr*/ dataptr,
1548 /*data_len*/ sizeof(*longdata),
1552 /*tag_type*/ CTL_TAG_SIMPLE,
1557 io->io_hdr.nexus.initid = iid;
1558 io->io_hdr.nexus.targ_lun = lun;
1560 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1565 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1569 if (cdbsize == 10) {
1571 data = (struct scsi_read_capacity_data *)dataptr;
1573 maxlba = scsi_4btoul(data->addr);
1574 blocksize = scsi_4btoul(data->length);
1576 if (maxlba == 0xffffffff) {
1581 longdata=(struct scsi_read_capacity_data_long *)dataptr;
1583 maxlba = scsi_8btou64(longdata->addr);
1584 blocksize = scsi_4btoul(longdata->length);
1587 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
1588 (uintmax_t)maxlba, blocksize);
1590 ctl_io_error_print(io, NULL, stderr);
1593 ctl_scsi_free_io(io);
1595 if (dataptr != NULL)
1602 cctl_read_write(int fd, int lun, int iid, int retries,
1603 int argc, char **argv, char *combinedopt,
1604 ctladm_cmdfunction command)
1607 int file_fd, do_stdio;
1608 int cdbsize = -1, databytes;
1610 char *filename = NULL;
1611 int datalen = -1, blocksize = -1;
1622 io = ctl_scsi_alloc_io(iid);
1624 warn("%s: can't allocate memory\n", __func__);
1628 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1631 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
1634 blocksize = strtoul(optarg, NULL, 0);
1637 cdbsize = strtoul(optarg, NULL, 0);
1640 datalen = strtoul(optarg, NULL, 0);
1643 filename = strdup(optarg);
1646 lba = strtoull(optarg, NULL, 0);
1653 if (filename == NULL) {
1654 warnx("%s: you must supply a filename using -f", __func__);
1659 if (datalen == -1) {
1660 warnx("%s: you must specify the data length with -d", __func__);
1666 warnx("%s: you must specify the LBA with -l", __func__);
1671 if (blocksize == -1) {
1672 warnx("%s: you must specify the blocksize with -b", __func__);
1677 if (cdbsize != -1) {
1685 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
1686 "10, 12 or 16", __func__, cdbsize);
1689 break; /* NOTREACHED */
1694 databytes = datalen * blocksize;
1695 dataptr = (uint8_t *)malloc(databytes);
1697 if (dataptr == NULL) {
1698 warn("%s: can't allocate %d bytes\n", __func__, databytes);
1702 if (strcmp(filename, "-") == 0) {
1703 if (command == CTLADM_CMD_READ)
1704 file_fd = STDOUT_FILENO;
1706 file_fd = STDIN_FILENO;
1709 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1710 if (file_fd == -1) {
1711 warn("%s: can't open file %s", __func__, filename);
1717 memset(dataptr, 0, databytes);
1719 if (command == CTLADM_CMD_WRITE) {
1722 bytes_read = read(file_fd, dataptr, databytes);
1723 if (bytes_read == -1) {
1724 warn("%s: error reading file %s", __func__, filename);
1728 if (bytes_read != databytes) {
1729 warnx("%s: only read %d bytes from file %s",
1730 __func__, bytes_read, filename);
1735 ctl_scsi_read_write(io,
1736 /*data_ptr*/ dataptr,
1737 /*data_len*/ databytes,
1738 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
1740 /*minimum_cdb_size*/ cdbsize,
1742 /*num_blocks*/ datalen,
1743 /*tag_type*/ CTL_TAG_SIMPLE,
1746 io->io_hdr.nexus.targ_lun = lun;
1747 io->io_hdr.nexus.initid = iid;
1749 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1754 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1755 && (command == CTLADM_CMD_READ)) {
1758 bytes_written = write(file_fd, dataptr, databytes);
1759 if (bytes_written == -1) {
1760 warn("%s: can't write to %s", __func__, filename);
1763 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1764 ctl_io_error_print(io, NULL, stderr);
1769 ctl_scsi_free_io(io);
1771 if (dataptr != NULL)
1782 cctl_get_luns(int fd, int lun, int iid, int retries, struct
1783 scsi_report_luns_data **lun_data, uint32_t *num_luns)
1792 io = ctl_scsi_alloc_io(iid);
1794 warnx("%s: can't allocate memory", __func__);
1799 * lun_data includes space for 1 lun, allocate space for 4 initially.
1800 * If that isn't enough, we'll allocate more.
1804 lun_datalen = sizeof(*lun_data) +
1805 (nluns * sizeof(struct scsi_report_luns_lundata));
1806 *lun_data = malloc(lun_datalen);
1808 if (*lun_data == NULL) {
1809 warnx("%s: can't allocate memory", __func__);
1810 ctl_scsi_free_io(io);
1814 ctl_scsi_report_luns(io,
1815 /*data_ptr*/ (uint8_t *)*lun_data,
1816 /*data_len*/ lun_datalen,
1817 /*select_report*/ RPL_REPORT_ALL,
1818 /*tag_type*/ CTL_TAG_SIMPLE,
1821 io->io_hdr.nexus.initid = iid;
1822 io->io_hdr.nexus.targ_lun = lun;
1824 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1829 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1830 uint32_t returned_len, returned_luns;
1832 returned_len = scsi_4btoul((*lun_data)->length);
1833 returned_luns = returned_len / 8;
1834 if (returned_luns > nluns) {
1835 nluns = returned_luns;
1839 /* These should be the same */
1840 *num_luns = MIN(returned_luns, nluns);
1842 ctl_io_error_print(io, NULL, stderr);
1846 ctl_scsi_free_io(io);
1852 cctl_report_luns(int fd, int lun, int iid, int retries)
1854 struct scsi_report_luns_data *lun_data;
1855 uint32_t num_luns, i;
1860 if ((retval = cctl_get_luns(fd, lun, iid, retries, &lun_data,
1864 fprintf(stdout, "%u LUNs returned\n", num_luns);
1865 for (i = 0; i < num_luns; i++) {
1869 * XXX KDM figure out a way to share this code with
1872 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1873 case RPL_LUNDATA_ATYP_PERIPH:
1874 lun_val = lun_data->luns[i].lundata[1];
1876 case RPL_LUNDATA_ATYP_FLAT:
1877 lun_val = (lun_data->luns[i].lundata[0] &
1878 RPL_LUNDATA_FLAT_LUN_MASK) |
1879 (lun_data->luns[i].lundata[1] <<
1880 RPL_LUNDATA_FLAT_LUN_BITS);
1882 case RPL_LUNDATA_ATYP_LUN:
1883 case RPL_LUNDATA_ATYP_EXTLUN:
1885 fprintf(stdout, "Unsupported LUN format %d\n",
1886 lun_data->luns[i].lundata[0] &
1887 RPL_LUNDATA_ATYP_MASK);
1894 fprintf(stdout, "%d\n", lun_val);
1898 if (lun_data != NULL)
1905 cctl_tur(int fd, int lun, int iid, int retries)
1909 io = ctl_scsi_alloc_io(iid);
1911 fprintf(stderr, "can't allocate memory\n");
1916 /* tag_type */ CTL_TAG_SIMPLE,
1919 io->io_hdr.nexus.targ_lun = lun;
1920 io->io_hdr.nexus.initid = iid;
1922 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1923 ctl_scsi_free_io(io);
1927 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1928 fprintf(stdout, "Unit is ready\n");
1930 ctl_io_error_print(io, NULL, stderr);
1936 cctl_get_inquiry(int fd, int lun, int iid, int retries,
1937 char *path_str, int path_len,
1938 struct scsi_inquiry_data *inq_data)
1945 io = ctl_scsi_alloc_io(iid);
1947 warnx("cctl_inquiry: can't allocate memory\n");
1951 ctl_scsi_inquiry(/*io*/ io,
1952 /*data_ptr*/ (uint8_t *)inq_data,
1953 /*data_len*/ sizeof(*inq_data),
1956 /*tag_type*/ CTL_TAG_SIMPLE,
1959 io->io_hdr.nexus.targ_lun = lun;
1960 io->io_hdr.nexus.initid = iid;
1962 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1967 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
1969 ctl_io_error_print(io, NULL, stderr);
1970 } else if (path_str != NULL)
1971 ctl_scsi_path_string(io, path_str, path_len);
1974 ctl_scsi_free_io(io);
1980 cctl_inquiry(int fd, int lun, int iid, int retries)
1982 struct scsi_inquiry_data *inq_data;
1986 inq_data = malloc(sizeof(*inq_data));
1987 if (inq_data == NULL) {
1988 warnx("%s: can't allocate inquiry data", __func__);
1993 if ((retval = cctl_get_inquiry(fd, lun, iid, retries, scsi_path,
1994 sizeof(scsi_path), inq_data)) != 0)
1997 printf("%s", scsi_path);
1998 scsi_print_inquiry(inq_data);
2001 if (inq_data != NULL)
2008 cctl_req_sense(int fd, int lun, int iid, int retries)
2011 struct scsi_sense_data *sense_data;
2016 io = ctl_scsi_alloc_io(iid);
2018 warnx("cctl_req_sense: can't allocate memory\n");
2021 sense_data = malloc(sizeof(*sense_data));
2022 memset(sense_data, 0, sizeof(*sense_data));
2024 ctl_scsi_request_sense(/*io*/ io,
2025 /*data_ptr*/ (uint8_t *)sense_data,
2026 /*data_len*/ sizeof(*sense_data),
2028 /*tag_type*/ CTL_TAG_SIMPLE,
2031 io->io_hdr.nexus.targ_lun = lun;
2032 io->io_hdr.nexus.initid = iid;
2034 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2039 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2040 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2041 io->scsiio.sense_len = sizeof(*sense_data);
2042 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2044 ctl_io_error_print(io, NULL, stderr);
2048 ctl_scsi_free_io(io);
2055 cctl_report_target_port_group(int fd, int lun, int iid)
2065 io = ctl_scsi_alloc_io(iid);
2067 warn("%s: can't allocate memory", __func__);
2072 dataptr = (uint8_t *)malloc(datalen);
2073 if (dataptr == NULL) {
2074 warn("%s: can't allocate %d bytes", __func__, datalen);
2079 memset(dataptr, 0, datalen);
2081 ctl_scsi_maintenance_in(/*io*/ io,
2082 /*data_ptr*/ dataptr,
2083 /*data_len*/ datalen,
2084 /*action*/ SA_RPRT_TRGT_GRP,
2085 /*tag_type*/ CTL_TAG_SIMPLE,
2088 io->io_hdr.nexus.targ_lun = lun;
2089 io->io_hdr.nexus.initid = iid;
2091 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2096 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2097 int returned_len, used_len;
2099 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2101 for (used_len = 0; used_len < returned_len; used_len++) {
2102 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2103 if (((used_len+1) % 8) == 0)
2104 fprintf(stdout, "\n");
2106 fprintf(stdout, "\n");
2108 ctl_io_error_print(io, NULL, stderr);
2111 ctl_scsi_free_io(io);
2113 if (dataptr != NULL)
2120 cctl_inquiry_vpd_devid(int fd, int lun, int iid)
2130 io = ctl_scsi_alloc_io(iid);
2132 warn("%s: can't allocate memory", __func__);
2137 dataptr = (uint8_t *)malloc(datalen);
2138 if (dataptr == NULL) {
2139 warn("%s: can't allocate %d bytes", __func__, datalen);
2144 memset(dataptr, 0, datalen);
2146 ctl_scsi_inquiry(/*io*/ io,
2147 /*data_ptr*/ dataptr,
2148 /*data_len*/ datalen,
2150 /*page_code*/ SVPD_DEVICE_ID,
2151 /*tag_type*/ CTL_TAG_SIMPLE,
2154 io->io_hdr.nexus.targ_lun = lun;
2155 io->io_hdr.nexus.initid = iid;
2157 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2162 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2163 int returned_len, used_len;
2165 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2167 for (used_len = 0; used_len < returned_len; used_len++) {
2168 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2169 if (((used_len+1) % 8) == 0)
2170 fprintf(stdout, "\n");
2172 fprintf(stdout, "\n");
2174 ctl_io_error_print(io, NULL, stderr);
2177 ctl_scsi_free_io(io);
2179 if (dataptr != NULL)
2186 cctl_persistent_reserve_in(int fd, int lun, int iid,
2187 int argc, char **argv, char *combinedopt,
2200 io = ctl_scsi_alloc_io(iid);
2202 warn("%s: can't allocate memory", __func__);
2206 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2209 action = strtol(optarg, NULL, 0);
2216 if (action < 0 || action > 2) {
2217 warn("action must be specified and in the range: 0-2");
2224 dataptr = (uint8_t *)malloc(datalen);
2225 if (dataptr == NULL) {
2226 warn("%s: can't allocate %d bytes", __func__, datalen);
2231 memset(dataptr, 0, datalen);
2233 ctl_scsi_persistent_res_in(io,
2234 /*data_ptr*/ dataptr,
2235 /*data_len*/ datalen,
2237 /*tag_type*/ CTL_TAG_SIMPLE,
2240 io->io_hdr.nexus.targ_lun = lun;
2241 io->io_hdr.nexus.initid = iid;
2243 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2248 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2249 int returned_len, used_len;
2253 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2254 returned_len = min(returned_len, 256);
2257 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2263 warnx("%s: invalid action %d", __func__, action);
2265 break; /* NOTREACHED */
2268 for (used_len = 0; used_len < returned_len; used_len++) {
2269 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2270 if (((used_len+1) % 8) == 0)
2271 fprintf(stdout, "\n");
2273 fprintf(stdout, "\n");
2275 ctl_io_error_print(io, NULL, stderr);
2278 ctl_scsi_free_io(io);
2280 if (dataptr != NULL)
2287 cctl_persistent_reserve_out(int fd, int lun, int iid,
2288 int argc, char **argv, char *combinedopt,
2293 uint64_t key = 0, sa_key = 0;
2294 int action = -1, restype = -1;
2302 io = ctl_scsi_alloc_io(iid);
2304 warn("%s: can't allocate memory", __func__);
2308 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2311 action = strtol(optarg, NULL, 0);
2314 key = strtoull(optarg, NULL, 0);
2317 restype = strtol(optarg, NULL, 0);
2320 sa_key = strtoull(optarg, NULL, 0);
2326 if (action < 0 || action > 5) {
2327 warn("action must be specified and in the range: 0-5");
2332 if (restype < 0 || restype > 5) {
2333 if (action != 0 && action != 5 && action != 3) {
2334 warn("'restype' must specified and in the range: 0-5");
2341 dataptr = (uint8_t *)malloc(datalen);
2342 if (dataptr == NULL) {
2343 warn("%s: can't allocate %d bytes", __func__, datalen);
2348 memset(dataptr, 0, datalen);
2350 ctl_scsi_persistent_res_out(io,
2351 /*data_ptr*/ dataptr,
2352 /*data_len*/ datalen,
2357 /*tag_type*/ CTL_TAG_SIMPLE,
2360 io->io_hdr.nexus.targ_lun = lun;
2361 io->io_hdr.nexus.initid = iid;
2363 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2367 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2369 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2370 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2371 "successfully\n", scsi_path);
2373 ctl_io_error_print(io, NULL, stderr);
2376 ctl_scsi_free_io(io);
2378 if (dataptr != NULL)
2385 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2387 struct ctl_lun_req req;
2388 int device_type = -1;
2389 uint64_t lun_size = 0;
2390 uint32_t blocksize = 0, req_lun_id = 0;
2391 char *serial_num = NULL;
2392 char *device_id = NULL;
2393 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
2394 char *backend_name = NULL;
2395 nvlist_t *option_list;
2398 option_list = nvlist_create(0);
2399 if (option_list == NULL)
2400 err(1, "%s: unable to allocate nvlist", __func__);
2402 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2405 backend_name = strdup(optarg);
2408 blocksize = strtoul(optarg, NULL, 0);
2412 device_id = strdup(optarg);
2415 req_lun_id = strtoul(optarg, NULL, 0);
2422 tmpstr = strdup(optarg);
2423 name = strsep(&tmpstr, "=");
2425 warnx("%s: option -o takes \"name=value\""
2426 "argument", __func__);
2430 value = strsep(&tmpstr, "=");
2431 if (value == NULL) {
2432 warnx("%s: option -o takes \"name=value\""
2433 "argument", __func__);
2438 nvlist_add_string(option_list, name, value);
2442 if (strcasecmp(optarg, "auto") != 0) {
2443 retval = expand_number(optarg, &lun_size);
2445 warn("%s: invalid -s argument",
2454 serial_num = strdup(optarg);
2457 device_type = strtoul(optarg, NULL, 0);
2464 if (backend_name == NULL) {
2465 warnx("%s: backend name (-b) must be specified", __func__);
2470 bzero(&req, sizeof(req));
2472 strlcpy(req.backend, backend_name, sizeof(req.backend));
2473 req.reqtype = CTL_LUNREQ_CREATE;
2475 if (blocksize_set != 0)
2476 req.reqdata.create.blocksize_bytes = blocksize;
2478 if (lun_size_set != 0)
2479 req.reqdata.create.lun_size_bytes = lun_size;
2481 if (lun_id_set != 0) {
2482 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
2483 req.reqdata.create.req_lun_id = req_lun_id;
2486 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
2488 if (device_type != -1)
2489 req.reqdata.create.device_type = device_type;
2491 req.reqdata.create.device_type = T_DIRECT;
2493 if (serial_num != NULL) {
2494 strlcpy(req.reqdata.create.serial_num, serial_num,
2495 sizeof(req.reqdata.create.serial_num));
2496 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
2499 if (device_id != NULL) {
2500 strlcpy(req.reqdata.create.device_id, device_id,
2501 sizeof(req.reqdata.create.device_id));
2502 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
2505 req.args = nvlist_pack(option_list, &req.args_len);
2506 if (req.args == NULL) {
2507 warn("%s: error packing nvlist", __func__);
2512 retval = ioctl(fd, CTL_LUN_REQ, &req);
2515 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2520 switch (req.status) {
2522 warnx("LUN creation error: %s", req.error_str);
2525 case CTL_LUN_WARNING:
2526 warnx("LUN creation warning: %s", req.error_str);
2531 warnx("unknown LUN creation status: %d", req.status);
2536 fprintf(stdout, "LUN created successfully\n");
2537 fprintf(stdout, "backend: %s\n", req.backend);
2538 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
2539 fprintf(stdout, "LUN size: %ju bytes\n",
2540 (uintmax_t)req.reqdata.create.lun_size_bytes);
2541 fprintf(stdout, "blocksize %u bytes\n",
2542 req.reqdata.create.blocksize_bytes);
2543 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
2544 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
2545 fprintf(stdout, "Device ID: %s\n", req.reqdata.create.device_id);
2548 nvlist_destroy(option_list);
2553 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
2555 struct ctl_lun_req req;
2556 uint32_t lun_id = 0;
2558 char *backend_name = NULL;
2559 nvlist_t *option_list;
2562 option_list = nvlist_create(0);
2563 if (option_list == NULL)
2564 err(1, "%s: unable to allocate nvlist", __func__);
2566 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2569 backend_name = strdup(optarg);
2572 lun_id = strtoul(optarg, NULL, 0);
2579 tmpstr = strdup(optarg);
2580 name = strsep(&tmpstr, "=");
2582 warnx("%s: option -o takes \"name=value\""
2583 "argument", __func__);
2587 value = strsep(&tmpstr, "=");
2588 if (value == NULL) {
2589 warnx("%s: option -o takes \"name=value\""
2590 "argument", __func__);
2595 nvlist_add_string(option_list, name, value);
2603 if (backend_name == NULL)
2604 errx(1, "%s: backend name (-b) must be specified", __func__);
2606 if (lun_id_set == 0)
2607 errx(1, "%s: LUN id (-l) must be specified", __func__);
2609 bzero(&req, sizeof(req));
2611 strlcpy(req.backend, backend_name, sizeof(req.backend));
2612 req.reqtype = CTL_LUNREQ_RM;
2614 req.reqdata.rm.lun_id = lun_id;
2616 req.args = nvlist_pack(option_list, &req.args_len);
2617 if (req.args == NULL) {
2618 warn("%s: error packing nvlist", __func__);
2623 retval = ioctl(fd, CTL_LUN_REQ, &req);
2626 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2631 switch (req.status) {
2633 warnx("LUN removal error: %s", req.error_str);
2636 case CTL_LUN_WARNING:
2637 warnx("LUN removal warning: %s", req.error_str);
2642 warnx("unknown LUN removal status: %d", req.status);
2647 printf("LUN %d removed successfully\n", lun_id);
2650 nvlist_destroy(option_list);
2655 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
2657 struct ctl_lun_req req;
2658 uint64_t lun_size = 0;
2659 uint32_t lun_id = 0;
2660 int lun_id_set = 0, lun_size_set = 0;
2661 char *backend_name = NULL;
2662 nvlist_t *option_list;
2665 option_list = nvlist_create(0);
2666 if (option_list == NULL)
2667 err(1, "%s: unable to allocate nvlist", __func__);
2669 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2672 backend_name = strdup(optarg);
2675 lun_id = strtoul(optarg, NULL, 0);
2682 tmpstr = strdup(optarg);
2683 name = strsep(&tmpstr, "=");
2685 warnx("%s: option -o takes \"name=value\""
2686 "argument", __func__);
2690 value = strsep(&tmpstr, "=");
2691 if (value == NULL) {
2692 warnx("%s: option -o takes \"name=value\""
2693 "argument", __func__);
2698 nvlist_add_string(option_list, name, value);
2702 if (strcasecmp(optarg, "auto") != 0) {
2703 retval = expand_number(optarg, &lun_size);
2705 warn("%s: invalid -s argument",
2718 if (backend_name == NULL)
2719 errx(1, "%s: backend name (-b) must be specified", __func__);
2721 if (lun_id_set == 0)
2722 errx(1, "%s: LUN id (-l) must be specified", __func__);
2724 if (lun_size_set == 0 && nvlist_empty(option_list))
2725 errx(1, "%s: size (-s) or options (-o) must be specified",
2728 bzero(&req, sizeof(req));
2730 strlcpy(req.backend, backend_name, sizeof(req.backend));
2731 req.reqtype = CTL_LUNREQ_MODIFY;
2733 req.reqdata.modify.lun_id = lun_id;
2734 req.reqdata.modify.lun_size_bytes = lun_size;
2736 req.args = nvlist_pack(option_list, &req.args_len);
2737 if (req.args == NULL) {
2738 warn("%s: error packing nvlist", __func__);
2743 retval = ioctl(fd, CTL_LUN_REQ, &req);
2746 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2751 switch (req.status) {
2753 warnx("LUN modification error: %s", req.error_str);
2756 case CTL_LUN_WARNING:
2757 warnx("LUN modification warning: %s", req.error_str);
2762 warnx("unknown LUN modification status: %d", req.status);
2767 printf("LUN %d modified successfully\n", lun_id);
2770 nvlist_destroy(option_list);
2774 struct cctl_islist_conn {
2777 char *initiator_addr;
2778 char *initiator_alias;
2781 char *header_digest;
2783 char *max_recv_data_segment_length;
2784 char *max_send_data_segment_length;
2785 char *max_burst_length;
2786 char *first_burst_length;
2790 STAILQ_ENTRY(cctl_islist_conn) links;
2793 struct cctl_islist_data {
2795 STAILQ_HEAD(,cctl_islist_conn) conn_list;
2796 struct cctl_islist_conn *cur_conn;
2798 struct sbuf *cur_sb[32];
2802 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
2805 struct cctl_islist_data *islist;
2806 struct cctl_islist_conn *cur_conn;
2808 islist = (struct cctl_islist_data *)user_data;
2809 cur_conn = islist->cur_conn;
2811 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
2812 sizeof(islist->cur_sb[0])))
2813 errx(1, "%s: too many nesting levels, %zd max", __func__,
2814 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
2816 islist->cur_sb[islist->level] = sbuf_new_auto();
2817 if (islist->cur_sb[islist->level] == NULL)
2818 err(1, "%s: Unable to allocate sbuf", __func__);
2820 if (strcmp(name, "connection") == 0) {
2821 if (cur_conn != NULL)
2822 errx(1, "%s: improper connection element nesting",
2825 cur_conn = calloc(1, sizeof(*cur_conn));
2826 if (cur_conn == NULL)
2827 err(1, "%s: cannot allocate %zd bytes", __func__,
2830 islist->num_conns++;
2831 islist->cur_conn = cur_conn;
2833 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
2835 for (i = 0; attr[i] != NULL; i += 2) {
2836 if (strcmp(attr[i], "id") == 0) {
2837 cur_conn->connection_id =
2838 strtoull(attr[i+1], NULL, 0);
2841 "%s: invalid connection attribute %s = %s",
2842 __func__, attr[i], attr[i+1]);
2849 cctl_islist_end_element(void *user_data, const char *name)
2851 struct cctl_islist_data *islist;
2852 struct cctl_islist_conn *cur_conn;
2855 islist = (struct cctl_islist_data *)user_data;
2856 cur_conn = islist->cur_conn;
2858 if ((cur_conn == NULL)
2859 && (strcmp(name, "ctlislist") != 0))
2860 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
2862 if (islist->cur_sb[islist->level] == NULL)
2863 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
2864 islist->level, name);
2866 sbuf_finish(islist->cur_sb[islist->level]);
2867 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
2869 err(1, "%s can't allocate %zd bytes for string", __func__,
2870 sbuf_len(islist->cur_sb[islist->level]));
2872 sbuf_delete(islist->cur_sb[islist->level]);
2873 islist->cur_sb[islist->level] = NULL;
2876 if (strcmp(name, "initiator") == 0) {
2877 cur_conn->initiator = str;
2879 } else if (strcmp(name, "initiator_addr") == 0) {
2880 cur_conn->initiator_addr = str;
2882 } else if (strcmp(name, "initiator_alias") == 0) {
2883 cur_conn->initiator_alias = str;
2885 } else if (strcmp(name, "target") == 0) {
2886 cur_conn->target = str;
2888 } else if (strcmp(name, "target_alias") == 0) {
2889 cur_conn->target_alias = str;
2891 } else if (strcmp(name, "target_portal_group_tag") == 0) {
2892 } else if (strcmp(name, "header_digest") == 0) {
2893 cur_conn->header_digest = str;
2895 } else if (strcmp(name, "data_digest") == 0) {
2896 cur_conn->data_digest = str;
2898 } else if (strcmp(name, "max_recv_data_segment_length") == 0) {
2899 cur_conn->max_recv_data_segment_length = str;
2901 } else if (strcmp(name, "max_send_data_segment_length") == 0) {
2902 cur_conn->max_send_data_segment_length = str;
2904 } else if (strcmp(name, "max_burst_length") == 0) {
2905 cur_conn->max_burst_length = str;
2907 } else if (strcmp(name, "first_burst_length") == 0) {
2908 cur_conn->first_burst_length = str;
2910 } else if (strcmp(name, "offload") == 0) {
2911 cur_conn->offload = str;
2913 } else if (strcmp(name, "immediate_data") == 0) {
2914 cur_conn->immediate_data = atoi(str);
2915 } else if (strcmp(name, "iser") == 0) {
2916 cur_conn->iser = atoi(str);
2917 } else if (strcmp(name, "connection") == 0) {
2918 islist->cur_conn = NULL;
2919 } else if (strcmp(name, "ctlislist") == 0) {
2923 * Unknown element; ignore it for forward compatibility.
2931 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
2933 struct cctl_islist_data *islist;
2935 islist = (struct cctl_islist_data *)user_data;
2937 sbuf_bcat(islist->cur_sb[islist->level], str, len);
2941 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
2943 struct ctl_iscsi req;
2944 struct cctl_islist_data islist;
2945 struct cctl_islist_conn *conn;
2950 int c, retval, verbose = 0;
2955 bzero(&islist, sizeof(islist));
2956 STAILQ_INIT(&islist.conn_list);
2958 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2972 conn_str = malloc(conn_len);
2974 bzero(&req, sizeof(req));
2975 req.type = CTL_ISCSI_LIST;
2976 req.data.list.alloc_len = conn_len;
2977 req.data.list.conn_xml = conn_str;
2979 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
2980 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
2985 if (req.status == CTL_ISCSI_ERROR) {
2986 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
2987 __func__, req.error_str);
2988 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
2989 conn_len = conn_len << 1;
2993 if (dump_xml != 0) {
2994 printf("%s", conn_str);
2998 parser = XML_ParserCreate(NULL);
2999 if (parser == NULL) {
3000 warn("%s: Unable to create XML parser", __func__);
3005 XML_SetUserData(parser, &islist);
3006 XML_SetElementHandler(parser, cctl_islist_start_element,
3007 cctl_islist_end_element);
3008 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3010 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3012 warnx("%s: Unable to parse XML: Error %d", __func__,
3013 XML_GetErrorCode(parser));
3014 XML_ParserFree(parser);
3019 XML_ParserFree(parser);
3022 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3023 printf("%-25s %d\n", "Session ID:", conn->connection_id);
3024 printf("%-25s %s\n", "Initiator name:", conn->initiator);
3025 printf("%-25s %s\n", "Initiator portal:", conn->initiator_addr);
3026 printf("%-25s %s\n", "Initiator alias:", conn->initiator_alias);
3027 printf("%-25s %s\n", "Target name:", conn->target);
3028 printf("%-25s %s\n", "Target alias:", conn->target_alias);
3029 printf("%-25s %s\n", "Header digest:", conn->header_digest);
3030 printf("%-25s %s\n", "Data digest:", conn->data_digest);
3031 printf("%-25s %s\n", "MaxRecvDataSegmentLength:", conn->max_recv_data_segment_length);
3032 printf("%-25s %s\n", "MaxSendDataSegmentLength:", conn->max_send_data_segment_length);
3033 printf("%-25s %s\n", "MaxBurstLen:", conn->max_burst_length);
3034 printf("%-25s %s\n", "FirstBurstLen:", conn->first_burst_length);
3035 printf("%-25s %s\n", "ImmediateData:", conn->immediate_data ? "Yes" : "No");
3036 printf("%-25s %s\n", "iSER (RDMA):", conn->iser ? "Yes" : "No");
3037 printf("%-25s %s\n", "Offload driver:", conn->offload);
3041 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3043 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3044 printf("%4u %-16s %-36s %-36s\n",
3045 conn->connection_id, conn->initiator_addr, conn->initiator,
3056 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3058 struct ctl_iscsi req;
3060 int all = 0, connection_id = -1, nargs = 0;
3061 char *initiator_name = NULL, *initiator_addr = NULL;
3063 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3070 connection_id = strtoul(optarg, NULL, 0);
3074 initiator_name = strdup(optarg);
3075 if (initiator_name == NULL)
3076 err(1, "%s: strdup", __func__);
3080 initiator_addr = strdup(optarg);
3081 if (initiator_addr == NULL)
3082 err(1, "%s: strdup", __func__);
3091 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3094 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3097 bzero(&req, sizeof(req));
3098 req.type = CTL_ISCSI_LOGOUT;
3099 req.data.logout.connection_id = connection_id;
3100 if (initiator_addr != NULL)
3101 strlcpy(req.data.logout.initiator_addr,
3102 initiator_addr, sizeof(req.data.logout.initiator_addr));
3103 if (initiator_name != NULL)
3104 strlcpy(req.data.logout.initiator_name,
3105 initiator_name, sizeof(req.data.logout.initiator_name));
3107 req.data.logout.all = 1;
3109 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3110 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3115 if (req.status != CTL_ISCSI_OK) {
3116 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3117 __func__, req.error_str);
3122 printf("iSCSI logout requests submitted\n");
3129 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3131 struct ctl_iscsi req;
3133 int all = 0, connection_id = -1, nargs = 0;
3134 char *initiator_name = NULL, *initiator_addr = NULL;
3136 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3143 connection_id = strtoul(optarg, NULL, 0);
3147 initiator_name = strdup(optarg);
3148 if (initiator_name == NULL)
3149 err(1, "%s: strdup", __func__);
3153 initiator_addr = strdup(optarg);
3154 if (initiator_addr == NULL)
3155 err(1, "%s: strdup", __func__);
3164 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3167 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3170 bzero(&req, sizeof(req));
3171 req.type = CTL_ISCSI_TERMINATE;
3172 req.data.terminate.connection_id = connection_id;
3173 if (initiator_addr != NULL)
3174 strlcpy(req.data.terminate.initiator_addr,
3175 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3176 if (initiator_name != NULL)
3177 strlcpy(req.data.terminate.initiator_name,
3178 initiator_name, sizeof(req.data.terminate.initiator_name));
3180 req.data.terminate.all = 1;
3182 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3183 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3188 if (req.status != CTL_ISCSI_OK) {
3189 warnx("%s: error returned from CTL iSCSI connection "
3190 "termination request:\n%s", __func__, req.error_str);
3195 printf("iSCSI connections terminated\n");
3202 * Name/value pair used for per-LUN attributes.
3204 struct cctl_lun_nv {
3207 STAILQ_ENTRY(cctl_lun_nv) links;
3211 * Backend LUN information.
3216 uint64_t size_blocks;
3218 char *serial_number;
3220 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3221 STAILQ_ENTRY(cctl_lun) links;
3224 struct cctl_devlist_data {
3226 STAILQ_HEAD(,cctl_lun) lun_list;
3227 struct cctl_lun *cur_lun;
3229 struct sbuf *cur_sb[32];
3233 cctl_start_element(void *user_data, const char *name, const char **attr)
3236 struct cctl_devlist_data *devlist;
3237 struct cctl_lun *cur_lun;
3239 devlist = (struct cctl_devlist_data *)user_data;
3240 cur_lun = devlist->cur_lun;
3242 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3243 sizeof(devlist->cur_sb[0])))
3244 errx(1, "%s: too many nesting levels, %zd max", __func__,
3245 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3247 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3248 if (devlist->cur_sb[devlist->level] == NULL)
3249 err(1, "%s: Unable to allocate sbuf", __func__);
3251 if (strcmp(name, "lun") == 0) {
3252 if (cur_lun != NULL)
3253 errx(1, "%s: improper lun element nesting", __func__);
3255 cur_lun = calloc(1, sizeof(*cur_lun));
3256 if (cur_lun == NULL)
3257 err(1, "%s: cannot allocate %zd bytes", __func__,
3260 devlist->num_luns++;
3261 devlist->cur_lun = cur_lun;
3263 STAILQ_INIT(&cur_lun->attr_list);
3264 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3266 for (i = 0; attr[i] != NULL; i += 2) {
3267 if (strcmp(attr[i], "id") == 0) {
3268 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3270 errx(1, "%s: invalid LUN attribute %s = %s",
3271 __func__, attr[i], attr[i+1]);
3278 cctl_end_element(void *user_data, const char *name)
3280 struct cctl_devlist_data *devlist;
3281 struct cctl_lun *cur_lun;
3284 devlist = (struct cctl_devlist_data *)user_data;
3285 cur_lun = devlist->cur_lun;
3287 if ((cur_lun == NULL)
3288 && (strcmp(name, "ctllunlist") != 0))
3289 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3291 if (devlist->cur_sb[devlist->level] == NULL)
3292 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3293 devlist->level, name);
3295 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3296 err(1, "%s: sbuf_finish", __func__);
3297 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3299 err(1, "%s can't allocate %zd bytes for string", __func__,
3300 sbuf_len(devlist->cur_sb[devlist->level]));
3302 if (strlen(str) == 0) {
3307 sbuf_delete(devlist->cur_sb[devlist->level]);
3308 devlist->cur_sb[devlist->level] = NULL;
3311 if (strcmp(name, "backend_type") == 0) {
3312 cur_lun->backend_type = str;
3314 } else if (strcmp(name, "size") == 0) {
3315 cur_lun->size_blocks = strtoull(str, NULL, 0);
3316 } else if (strcmp(name, "blocksize") == 0) {
3317 cur_lun->blocksize = strtoul(str, NULL, 0);
3318 } else if (strcmp(name, "serial_number") == 0) {
3319 cur_lun->serial_number = str;
3321 } else if (strcmp(name, "device_id") == 0) {
3322 cur_lun->device_id = str;
3324 } else if (strcmp(name, "lun") == 0) {
3325 devlist->cur_lun = NULL;
3326 } else if (strcmp(name, "ctllunlist") == 0) {
3329 struct cctl_lun_nv *nv;
3331 nv = calloc(1, sizeof(*nv));
3333 err(1, "%s: can't allocate %zd bytes for nv pair",
3334 __func__, sizeof(*nv));
3336 nv->name = strdup(name);
3337 if (nv->name == NULL)
3338 err(1, "%s: can't allocated %zd bytes for string",
3339 __func__, strlen(name));
3343 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3350 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3352 struct cctl_devlist_data *devlist;
3354 devlist = (struct cctl_devlist_data *)user_data;
3356 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3360 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3362 struct ctl_lun_list list;
3363 struct cctl_devlist_data devlist;
3364 struct cctl_lun *lun;
3370 char *backend = NULL;
3376 bzero(&devlist, sizeof(devlist));
3377 STAILQ_INIT(&devlist.lun_list);
3379 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3382 backend = strdup(optarg);
3396 lun_str = malloc(lun_len);
3398 bzero(&list, sizeof(list));
3399 list.alloc_len = lun_len;
3400 list.status = CTL_LUN_LIST_NONE;
3401 list.lun_xml = lun_str;
3403 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
3404 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
3409 if (list.status == CTL_LUN_LIST_ERROR) {
3410 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
3411 __func__, list.error_str);
3412 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3413 lun_len = lun_len << 1;
3417 if (dump_xml != 0) {
3418 printf("%s", lun_str);
3422 parser = XML_ParserCreate(NULL);
3423 if (parser == NULL) {
3424 warn("%s: Unable to create XML parser", __func__);
3429 XML_SetUserData(parser, &devlist);
3430 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
3431 XML_SetCharacterDataHandler(parser, cctl_char_handler);
3433 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
3435 warnx("%s: Unable to parse XML: Error %d", __func__,
3436 XML_GetErrorCode(parser));
3437 XML_ParserFree(parser);
3442 XML_ParserFree(parser);
3444 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
3445 "Serial Number", "Device ID");
3446 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
3447 struct cctl_lun_nv *nv;
3449 if ((backend != NULL)
3450 && (strcmp(lun->backend_type, backend) != 0))
3453 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
3454 (uintmax_t)lun->lun_id,
3455 lun->backend_type, (uintmax_t)lun->size_blocks,
3456 lun->blocksize, lun->serial_number, lun->device_id);
3461 STAILQ_FOREACH(nv, &lun->attr_list, links) {
3462 printf(" %s=%s\n", nv->name, nv->value);
3477 char *frontend_type;
3480 char *target, *port, *lun_map;
3481 STAILQ_HEAD(,cctl_lun_nv) init_list;
3482 STAILQ_HEAD(,cctl_lun_nv) lun_list;
3483 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3484 STAILQ_ENTRY(cctl_port) links;
3487 struct cctl_portlist_data {
3489 STAILQ_HEAD(,cctl_port) port_list;
3490 struct cctl_port *cur_port;
3493 struct sbuf *cur_sb[32];
3497 cctl_start_pelement(void *user_data, const char *name, const char **attr)
3500 struct cctl_portlist_data *portlist;
3501 struct cctl_port *cur_port;
3503 portlist = (struct cctl_portlist_data *)user_data;
3504 cur_port = portlist->cur_port;
3506 if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
3507 sizeof(portlist->cur_sb[0])))
3508 errx(1, "%s: too many nesting levels, %zd max", __func__,
3509 sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
3511 portlist->cur_sb[portlist->level] = sbuf_new_auto();
3512 if (portlist->cur_sb[portlist->level] == NULL)
3513 err(1, "%s: Unable to allocate sbuf", __func__);
3515 portlist->cur_id = 0;
3516 for (i = 0; attr[i] != NULL; i += 2) {
3517 if (strcmp(attr[i], "id") == 0) {
3518 portlist->cur_id = strtoull(attr[i+1], NULL, 0);
3523 if (strcmp(name, "targ_port") == 0) {
3524 if (cur_port != NULL)
3525 errx(1, "%s: improper port element nesting", __func__);
3527 cur_port = calloc(1, sizeof(*cur_port));
3528 if (cur_port == NULL)
3529 err(1, "%s: cannot allocate %zd bytes", __func__,
3532 portlist->num_ports++;
3533 portlist->cur_port = cur_port;
3535 STAILQ_INIT(&cur_port->init_list);
3536 STAILQ_INIT(&cur_port->lun_list);
3537 STAILQ_INIT(&cur_port->attr_list);
3538 cur_port->port_id = portlist->cur_id;
3539 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
3544 cctl_end_pelement(void *user_data, const char *name)
3546 struct cctl_portlist_data *portlist;
3547 struct cctl_port *cur_port;
3550 portlist = (struct cctl_portlist_data *)user_data;
3551 cur_port = portlist->cur_port;
3553 if ((cur_port == NULL)
3554 && (strcmp(name, "ctlportlist") != 0))
3555 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
3557 if (portlist->cur_sb[portlist->level] == NULL)
3558 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3559 portlist->level, name);
3561 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
3562 err(1, "%s: sbuf_finish", __func__);
3563 str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
3565 err(1, "%s can't allocate %zd bytes for string", __func__,
3566 sbuf_len(portlist->cur_sb[portlist->level]));
3568 if (strlen(str) == 0) {
3573 sbuf_delete(portlist->cur_sb[portlist->level]);
3574 portlist->cur_sb[portlist->level] = NULL;
3577 if (strcmp(name, "frontend_type") == 0) {
3578 cur_port->frontend_type = str;
3580 } else if (strcmp(name, "port_name") == 0) {
3581 cur_port->name = str;
3583 } else if (strcmp(name, "online") == 0) {
3584 cur_port->online = str;
3586 } else if (strcmp(name, "physical_port") == 0) {
3587 cur_port->pp = strtoull(str, NULL, 0);
3588 } else if (strcmp(name, "virtual_port") == 0) {
3589 cur_port->vp = strtoull(str, NULL, 0);
3590 } else if (strcmp(name, "target") == 0) {
3591 cur_port->target = str;
3593 } else if (strcmp(name, "port") == 0) {
3594 cur_port->port = str;
3596 } else if (strcmp(name, "lun_map") == 0) {
3597 cur_port->lun_map = str;
3599 } else if (strcmp(name, "targ_port") == 0) {
3600 portlist->cur_port = NULL;
3601 } else if (strcmp(name, "ctlportlist") == 0) {
3604 struct cctl_lun_nv *nv;
3606 nv = calloc(1, sizeof(*nv));
3608 err(1, "%s: can't allocate %zd bytes for nv pair",
3609 __func__, sizeof(*nv));
3611 if (strcmp(name, "initiator") == 0 ||
3612 strcmp(name, "lun") == 0)
3613 asprintf(&nv->name, "%ju", portlist->cur_id);
3615 nv->name = strdup(name);
3616 if (nv->name == NULL)
3617 err(1, "%s: can't allocated %zd bytes for string",
3618 __func__, strlen(name));
3622 if (strcmp(name, "initiator") == 0)
3623 STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links);
3624 else if (strcmp(name, "lun") == 0)
3625 STAILQ_INSERT_TAIL(&cur_port->lun_list, nv, links);
3627 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
3634 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
3636 struct cctl_portlist_data *portlist;
3638 portlist = (struct cctl_portlist_data *)user_data;
3640 sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
3644 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
3646 struct ctl_lun_list list;
3647 struct cctl_portlist_data portlist;
3648 struct cctl_port *port;
3650 char *port_str = NULL;
3654 char *frontend = NULL;
3655 uint64_t portarg = UINT64_MAX;
3656 int verbose = 0, init = 0, lun = 0, quiet = 0;
3661 bzero(&portlist, sizeof(portlist));
3662 STAILQ_INIT(&portlist.port_list);
3664 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3667 frontend = strdup(optarg);
3676 portarg = strtoll(optarg, NULL, 0);
3693 port_str = (char *)realloc(port_str, port_len);
3695 bzero(&list, sizeof(list));
3696 list.alloc_len = port_len;
3697 list.status = CTL_LUN_LIST_NONE;
3698 list.lun_xml = port_str;
3700 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
3701 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
3706 if (list.status == CTL_LUN_LIST_ERROR) {
3707 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
3708 __func__, list.error_str);
3709 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3710 port_len = port_len << 1;
3714 if (dump_xml != 0) {
3715 printf("%s", port_str);
3719 parser = XML_ParserCreate(NULL);
3720 if (parser == NULL) {
3721 warn("%s: Unable to create XML parser", __func__);
3726 XML_SetUserData(parser, &portlist);
3727 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
3728 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
3730 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
3732 warnx("%s: Unable to parse XML: Error %d", __func__,
3733 XML_GetErrorCode(parser));
3734 XML_ParserFree(parser);
3739 XML_ParserFree(parser);
3742 printf("Port Online Frontend Name pp vp\n");
3743 STAILQ_FOREACH(port, &portlist.port_list, links) {
3744 struct cctl_lun_nv *nv;
3746 if ((frontend != NULL)
3747 && (strcmp(port->frontend_type, frontend) != 0))
3750 if ((portarg != UINT64_MAX) && (portarg != port->port_id))
3753 printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n",
3754 (uintmax_t)port->port_id, port->online,
3755 port->frontend_type, port->name, port->pp, port->vp,
3756 port->port ? port->port : "");
3758 if (init || verbose) {
3760 printf(" Target: %s\n", port->target);
3761 STAILQ_FOREACH(nv, &port->init_list, links) {
3762 printf(" Initiator %s: %s\n",
3763 nv->name, nv->value);
3767 if (lun || verbose) {
3768 if (port->lun_map) {
3769 STAILQ_FOREACH(nv, &port->lun_list, links)
3770 printf(" LUN %s: %s\n",
3771 nv->name, nv->value);
3772 if (STAILQ_EMPTY(&port->lun_list))
3773 printf(" No LUNs mapped\n");
3775 printf(" All LUNs mapped\n");
3779 STAILQ_FOREACH(nv, &port->attr_list, links) {
3780 printf(" %s=%s\n", nv->name, nv->value);
3791 cctl_lunmap(int fd, int argc, char **argv, char *combinedopt)
3793 struct ctl_lun_map lm;
3797 lm.port = UINT32_MAX;
3798 lm.plun = UINT32_MAX;
3799 lm.lun = UINT32_MAX;
3801 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3804 lm.port = strtoll(optarg, NULL, 0);
3807 lm.plun = strtoll(optarg, NULL, 0);
3810 lm.lun = strtoll(optarg, NULL, 0);
3817 if (ioctl(fd, CTL_LUN_MAP, &lm) == -1) {
3818 warn("%s: error issuing CTL_LUN_MAP ioctl", __func__);
3828 fprintf(error ? stderr : stdout,
3830 "Primary commands:\n"
3831 " ctladm tur [dev_id][general options]\n"
3832 " ctladm inquiry [dev_id][general options]\n"
3833 " ctladm devid [dev_id][general options]\n"
3834 " ctladm reqsense [dev_id][general options]\n"
3835 " ctladm reportluns [dev_id][general options]\n"
3836 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
3837 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3838 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
3839 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3840 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
3841 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
3842 " [-d] [-S subpage] [-c cdbsize]\n"
3843 " ctladm prin [dev_id][general options] <-a action>\n"
3844 " ctladm prout [dev_id][general options] <-a action>\n"
3845 " <-r restype] [-k key] [-s sa_key]\n"
3846 " ctladm rtpg [dev_id][general options]\n"
3847 " ctladm start [dev_id][general options] [-i] [-o]\n"
3848 " ctladm stop [dev_id][general options] [-i] [-o]\n"
3849 " ctladm synccache [dev_id][general options] [-l lba]\n"
3850 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
3851 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
3852 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
3853 " [-S serial_num] [-t dev_type]\n"
3854 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
3855 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
3856 " ctladm devlist [-b backend] [-v] [-x]\n"
3858 " ctladm lunmap -p targ_port [-l pLUN] [-L cLUN]\n"
3859 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
3861 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
3862 " [-s len fmt [args]] [-c] [-d delete_id]\n"
3863 " ctladm port <-o <on|off> | [-w wwnn][-W wwpn]>\n"
3864 " [-p targ_port] [-t port_type]\n"
3865 " <-c> [-d driver] [-O name=value]\n"
3866 " <-r> <-p targ_port>\n"
3867 " ctladm portlist [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n"
3868 " ctladm islist [-v | -x]\n"
3869 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
3870 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
3872 " ctladm dumpstructs\n"
3874 "General Options:\n"
3875 "-I intiator_id : defaults to 7, used to change the initiator id\n"
3876 "-C retries : specify the number of times to retry this command\n"
3877 "-D devicename : specify the device to operate on\n"
3878 " : (default is %s)\n"
3879 "read/write options:\n"
3880 "-l lba : logical block address\n"
3881 "-d len : read/write length, in blocks\n"
3882 "-f file|- : write/read data to/from file or stdout/stdin\n"
3883 "-b blocksize : block size, in bytes\n"
3884 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
3885 "-N : do not copy data to/from userland\n"
3886 "readcapacity options:\n"
3887 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3888 "modesense options:\n"
3889 "-m page : specify the mode page to view\n"
3890 "-l : request a list of supported pages\n"
3891 "-P pc : specify the page control value: 0-3 (current,\n"
3892 " changeable, default, saved, respectively)\n"
3893 "-d : disable block descriptors for mode sense\n"
3894 "-S subpage : specify a subpage\n"
3895 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
3896 "persistent reserve in options:\n"
3897 "-a action : specify the action value: 0-2 (read key, read\n"
3898 " reservation, read capabilities, respectively)\n"
3899 "persistent reserve out options:\n"
3900 "-a action : specify the action value: 0-5 (register, reserve,\n"
3901 " release, clear, preempt, register and ignore)\n"
3902 "-k key : key value\n"
3903 "-s sa_key : service action value\n"
3904 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
3905 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
3906 "start/stop options:\n"
3907 "-i : set the immediate bit (CTL does not support this)\n"
3908 "-o : set the on/offline bit\n"
3909 "synccache options:\n"
3910 "-l lba : set the starting LBA\n"
3911 "-b blockcount : set the length to sync in blocks\n"
3912 "-r : set the relative addressing bit\n"
3913 "-i : set the immediate bit\n"
3914 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3916 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3917 "-B blocksize : LUN blocksize in bytes (some backends)\n"
3918 "-d device_id : SCSI VPD page 0x83 ID\n"
3919 "-l lun_id : requested LUN number\n"
3920 "-o name=value : backend-specific options, multiple allowed\n"
3921 "-s size_bytes : LUN size in bytes (some backends)\n"
3922 "-S serial_num : SCSI VPD page 0x80 serial number\n"
3923 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
3925 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3926 "-l lun_id : LUN number to delete\n"
3927 "-o name=value : backend-specific options, multiple allowed\n"
3928 "devlist options:\n"
3929 "-b backend : list devices from specified backend only\n"
3930 "-v : be verbose, show backend attributes\n"
3931 "-x : dump raw XML\n"
3933 "-l datamove|done : delay command at datamove or done phase\n"
3934 "-T oneshot : delay one command, then resume normal completion\n"
3935 "-T cont : delay all commands\n"
3936 "-t secs : number of seconds to delay\n"
3938 "-i error_action : action to perform\n"
3939 "-p pattern : command pattern to look for\n"
3940 "-r lba,len : LBA range for pattern\n"
3941 "-s len fmt [args] : sense data for custom sense action\n"
3942 "-c : continuous operation\n"
3943 "-d delete_id : error id to delete\n"
3945 "-c : create new ioctl or iscsi frontend port\n"
3946 "-d : specify ioctl or iscsi frontend type\n"
3947 "-l : list frontend ports\n"
3948 "-o on|off : turn frontend ports on or off\n"
3949 "-O pp|vp : create new frontend port using pp and/or vp\n"
3950 "-w wwnn : set WWNN for one frontend\n"
3951 "-W wwpn : set WWPN for one frontend\n"
3952 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
3953 "-p targ_port : specify target port number\n"
3954 "-r : remove frontend port\n"
3955 "-q : omit header in list output\n"
3956 "-x : output port list in XML format\n"
3957 "portlist options:\n"
3958 "-f frontend : specify frontend type\n"
3959 "-i : report target and initiators addresses\n"
3960 "-l : report LUN mapping\n"
3961 "-p targ_port : specify target port number\n"
3962 "-q : omit header in list output\n"
3963 "-v : verbose output (report all port options)\n"
3964 "-x : output port list in XML format\n"
3966 "-p targ_port : specify target port number\n"
3967 "-L pLUN : specify port-visible LUN\n"
3968 "-L cLUN : specify CTL LUN\n",
3973 main(int argc, char **argv)
3976 ctladm_cmdfunction command;
3977 ctladm_cmdargs cmdargs;
3978 ctladm_optret optreturn;
3980 const char *mainopt = "C:D:I:";
3981 const char *subopt = NULL;
3982 char combinedopt[256];
3991 cmdargs = CTLADM_ARG_NONE;
3992 command = CTLADM_CMD_HELP;
4006 * Get the base option.
4008 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4010 if (optreturn == CC_OR_AMBIGUOUS) {
4011 warnx("ambiguous option %s", argv[1]);
4014 } else if (optreturn == CC_OR_NOT_FOUND) {
4015 warnx("option %s not found", argv[1]);
4020 if (cmdargs & CTLADM_ARG_NEED_TL) {
4021 if ((argc < 3) || (!isdigit(argv[2][0]))) {
4022 warnx("option %s requires a lun argument",
4027 lun = strtol(argv[2], NULL, 0);
4029 cmdargs |= CTLADM_ARG_TARG_LUN;
4034 * Ahh, getopt(3) is a pain.
4036 * This is a gross hack. There really aren't many other good
4037 * options (excuse the pun) for parsing options in a situation like
4038 * this. getopt is kinda braindead, so you end up having to run
4039 * through the options twice, and give each invocation of getopt
4040 * the option string for the other invocation.
4042 * You would think that you could just have two groups of options.
4043 * The first group would get parsed by the first invocation of
4044 * getopt, and the second group would get parsed by the second
4045 * invocation of getopt. It doesn't quite work out that way. When
4046 * the first invocation of getopt finishes, it leaves optind pointing
4047 * to the argument _after_ the first argument in the second group.
4048 * So when the second invocation of getopt comes around, it doesn't
4049 * recognize the first argument it gets and then bails out.
4051 * A nice alternative would be to have a flag for getopt that says
4052 * "just keep parsing arguments even when you encounter an unknown
4053 * argument", but there isn't one. So there's no real clean way to
4054 * easily parse two sets of arguments without having one invocation
4055 * of getopt know about the other.
4057 * Without this hack, the first invocation of getopt would work as
4058 * long as the generic arguments are first, but the second invocation
4059 * (in the subfunction) would fail in one of two ways. In the case
4060 * where you don't set optreset, it would fail because optind may be
4061 * pointing to the argument after the one it should be pointing at.
4062 * In the case where you do set optreset, and reset optind, it would
4063 * fail because getopt would run into the first set of options, which
4064 * it doesn't understand.
4066 * All of this would "sort of" work if you could somehow figure out
4067 * whether optind had been incremented one option too far. The
4068 * mechanics of that, however, are more daunting than just giving
4069 * both invocations all of the expect options for either invocation.
4071 * Needless to say, I wouldn't mind if someone invented a better
4072 * (non-GPL!) command line parsing interface than getopt. I
4073 * wouldn't mind if someone added more knobs to getopt to make it
4074 * work better. Who knows, I may talk myself into doing it someday,
4075 * if the standards weenies let me. As it is, it just leads to
4076 * hackery like this and causes people to avoid it in some cases.
4078 * KDM, September 8th, 1998
4081 sprintf(combinedopt, "%s%s", mainopt, subopt);
4083 sprintf(combinedopt, "%s", mainopt);
4086 * Start getopt processing at argv[2/3], since we've already
4087 * accepted argv[1..2] as the command name, and as a possible
4093 * Now we run through the argument list looking for generic
4094 * options, and ignoring options that possibly belong to
4097 while ((c = getopt(argc, argv, combinedopt))!= -1){
4100 cmdargs |= CTLADM_ARG_RETRIES;
4101 retries = strtol(optarg, NULL, 0);
4104 device = strdup(optarg);
4105 cmdargs |= CTLADM_ARG_DEVICE;
4108 cmdargs |= CTLADM_ARG_INITIATOR;
4109 initid = strtol(optarg, NULL, 0);
4116 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4123 * Default to opening the CTL device for now.
4125 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4126 && (command != CTLADM_CMD_HELP)) {
4127 device = strdup(CTL_DEFAULT_DEV);
4128 cmdargs |= CTLADM_ARG_DEVICE;
4131 if ((cmdargs & CTLADM_ARG_DEVICE)
4132 && (command != CTLADM_CMD_HELP)) {
4133 fd = open(device, O_RDWR);
4134 if (fd == -1 && errno == ENOENT) {
4135 saved_errno = errno;
4136 retval = kldload("ctl");
4138 fd = open(device, O_RDWR);
4140 errno = saved_errno;
4143 fprintf(stderr, "%s: error opening %s: %s\n",
4144 argv[0], device, strerror(errno));
4150 if (modfind("cfiscsi") == -1 &&
4151 kldload("cfiscsi") == -1)
4152 warn("couldn't load cfiscsi");
4155 } else if ((command != CTLADM_CMD_HELP)
4156 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4157 fprintf(stderr, "%s: you must specify a device with the "
4158 "--device argument for this command\n", argv[0]);
4159 command = CTLADM_CMD_HELP;
4164 case CTLADM_CMD_TUR:
4165 retval = cctl_tur(fd, lun, initid, retries);
4167 case CTLADM_CMD_INQUIRY:
4168 retval = cctl_inquiry(fd, lun, initid, retries);
4170 case CTLADM_CMD_REQ_SENSE:
4171 retval = cctl_req_sense(fd, lun, initid, retries);
4173 case CTLADM_CMD_REPORT_LUNS:
4174 retval = cctl_report_luns(fd, lun, initid, retries);
4176 case CTLADM_CMD_CREATE:
4177 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4180 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4182 case CTLADM_CMD_DEVLIST:
4183 retval = cctl_devlist(fd, argc, argv, combinedopt);
4185 case CTLADM_CMD_READ:
4186 case CTLADM_CMD_WRITE:
4187 retval = cctl_read_write(fd, lun, initid, retries,
4188 argc, argv, combinedopt, command);
4190 case CTLADM_CMD_PORT:
4191 retval = cctl_port(fd, argc, argv, combinedopt);
4193 case CTLADM_CMD_PORTLIST:
4194 retval = cctl_portlist(fd, argc, argv, combinedopt);
4196 case CTLADM_CMD_LUNMAP:
4197 retval = cctl_lunmap(fd, argc, argv, combinedopt);
4199 case CTLADM_CMD_READCAPACITY:
4200 retval = cctl_read_capacity(fd, lun, initid, retries,
4201 argc, argv, combinedopt);
4203 case CTLADM_CMD_MODESENSE:
4204 retval = cctl_mode_sense(fd, lun, initid, retries,
4205 argc, argv, combinedopt);
4207 case CTLADM_CMD_START:
4208 case CTLADM_CMD_STOP:
4209 retval = cctl_start_stop(fd, lun, initid, retries,
4210 (command == CTLADM_CMD_START) ? 1 : 0,
4211 argc, argv, combinedopt);
4213 case CTLADM_CMD_SYNC_CACHE:
4214 retval = cctl_sync_cache(fd, lun, initid, retries,
4215 argc, argv, combinedopt);
4217 case CTLADM_CMD_LUNLIST:
4218 retval = cctl_lunlist(fd);
4220 case CTLADM_CMD_DELAY:
4221 retval = cctl_delay(fd, lun, argc, argv, combinedopt);
4223 case CTLADM_CMD_ERR_INJECT:
4224 retval = cctl_error_inject(fd, lun, argc, argv,
4227 case CTLADM_CMD_DUMPOOA:
4228 retval = cctl_dump_ooa(fd, argc, argv);
4230 case CTLADM_CMD_DUMPSTRUCTS:
4231 retval = cctl_dump_structs(fd, cmdargs);
4233 case CTLADM_CMD_PRES_IN:
4234 retval = cctl_persistent_reserve_in(fd, lun, initid,
4235 argc, argv, combinedopt,
4238 case CTLADM_CMD_PRES_OUT:
4239 retval = cctl_persistent_reserve_out(fd, lun, initid,
4240 argc, argv, combinedopt,
4243 case CTLADM_CMD_INQ_VPD_DEVID:
4244 retval = cctl_inquiry_vpd_devid(fd, lun, initid);
4246 case CTLADM_CMD_RTPG:
4247 retval = cctl_report_target_port_group(fd, lun, initid);
4249 case CTLADM_CMD_MODIFY:
4250 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4252 case CTLADM_CMD_ISLIST:
4253 retval = cctl_islist(fd, argc, argv, combinedopt);
4255 case CTLADM_CMD_ISLOGOUT:
4256 retval = cctl_islogout(fd, argc, argv, combinedopt);
4258 case CTLADM_CMD_ISTERMINATE:
4259 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4261 case CTLADM_CMD_HELP: