2 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
48 #include <sys/callout.h>
49 #include <sys/ioctl.h>
50 #include <sys/linker.h>
51 #include <sys/module.h>
52 #include <sys/queue.h>
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/scsi/scsi_message.h>
69 #include <cam/ctl/ctl.h>
70 #include <cam/ctl/ctl_io.h>
71 #include <cam/ctl/ctl_backend.h>
72 #include <cam/ctl/ctl_ioctl.h>
73 #include <cam/ctl/ctl_util.h>
74 #include <cam/ctl/ctl_scsi_all.h>
82 #define min(x,y) (x < y) ? x : y
89 CTLADM_CMD_REPORT_LUNS,
99 CTLADM_CMD_READCAPACITY,
100 CTLADM_CMD_MODESENSE,
102 CTLADM_CMD_DUMPSTRUCTS,
105 CTLADM_CMD_SYNC_CACHE,
108 CTLADM_CMD_ERR_INJECT,
111 CTLADM_CMD_INQ_VPD_DEVID,
116 CTLADM_CMD_ISTERMINATE,
118 } ctladm_cmdfunction;
121 CTLADM_ARG_NONE = 0x0000000,
122 CTLADM_ARG_AUTOSENSE = 0x0000001,
123 CTLADM_ARG_DEVICE = 0x0000002,
124 CTLADM_ARG_ARRAYSIZE = 0x0000004,
125 CTLADM_ARG_BACKEND = 0x0000008,
126 CTLADM_ARG_CDBSIZE = 0x0000010,
127 CTLADM_ARG_DATALEN = 0x0000020,
128 CTLADM_ARG_FILENAME = 0x0000040,
129 CTLADM_ARG_LBA = 0x0000080,
130 CTLADM_ARG_PC = 0x0000100,
131 CTLADM_ARG_PAGE_CODE = 0x0000200,
132 CTLADM_ARG_PAGE_LIST = 0x0000400,
133 CTLADM_ARG_SUBPAGE = 0x0000800,
134 CTLADM_ARG_PAGELIST = 0x0001000,
135 CTLADM_ARG_DBD = 0x0002000,
136 CTLADM_ARG_TARG_LUN = 0x0004000,
137 CTLADM_ARG_BLOCKSIZE = 0x0008000,
138 CTLADM_ARG_IMMED = 0x0010000,
139 CTLADM_ARG_RELADR = 0x0020000,
140 CTLADM_ARG_RETRIES = 0x0040000,
141 CTLADM_ARG_ONOFFLINE = 0x0080000,
142 CTLADM_ARG_ONESHOT = 0x0100000,
143 CTLADM_ARG_TIMEOUT = 0x0200000,
144 CTLADM_ARG_INITIATOR = 0x0400000,
145 CTLADM_ARG_NOCOPY = 0x0800000,
146 CTLADM_ARG_NEED_TL = 0x1000000
152 ctladm_cmdargs argnum;
162 static const char rw_opts[] = "Nb:c:d:f:l:";
163 static const char startstop_opts[] = "i";
165 static struct ctladm_opts option_table[] = {
166 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
167 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
168 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
169 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
170 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
171 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
172 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
173 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
174 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
175 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
176 {"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"},
177 {"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"},
178 {"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"},
179 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
180 {"lunmap", CTLADM_CMD_LUNMAP, CTLADM_ARG_NONE, "p:l:L:"},
181 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
182 {"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:o:s:"},
183 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:O:d:crp:qt:w:W:x"},
184 {"portlist", CTLADM_CMD_PORTLIST, CTLADM_ARG_NONE, "f:ilp:qvx"},
185 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
186 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
187 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
188 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
189 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
190 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
191 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
192 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
193 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
194 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
195 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
196 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
197 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
198 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
199 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
204 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
205 ctladm_cmdargs *argnum, const char **subopt);
206 static int cctl_dump_ooa(int fd, int argc, char **argv);
207 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
208 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
209 static int cctl_delay(int fd, int lun, int argc, char **argv,
211 static int cctl_lunlist(int fd);
212 static int cctl_sync_cache(int fd, int lun, int iid, int retries,
213 int argc, char **argv, char *combinedopt);
214 static int cctl_start_stop(int fd, int lun, int iid, int retries,
215 int start, int argc, char **argv, char *combinedopt);
216 static int cctl_mode_sense(int fd, int lun, int iid, int retries,
217 int argc, char **argv, char *combinedopt);
218 static int cctl_read_capacity(int fd, int lun, int iid,
219 int retries, int argc, char **argv,
221 static int cctl_read_write(int fd, int lun, int iid, int retries,
222 int argc, char **argv, char *combinedopt,
223 ctladm_cmdfunction command);
224 static int cctl_get_luns(int fd, int lun, int iid, int retries,
225 struct scsi_report_luns_data **lun_data,
227 static int cctl_report_luns(int fd, int lun, int iid, int retries);
228 static int cctl_tur(int fd, int lun, int iid, int retries);
229 static int cctl_get_inquiry(int fd, int lun, int iid, int retries,
230 char *path_str, int path_len,
231 struct scsi_inquiry_data *inq_data);
232 static int cctl_inquiry(int fd, int lun, int iid, int retries);
233 static int cctl_req_sense(int fd, int lun, int iid, int retries);
234 static int cctl_persistent_reserve_in(int fd, int lun,
235 int initiator, int argc, char **argv,
236 char *combinedopt, int retry_count);
237 static int cctl_persistent_reserve_out(int fd, int lun,
238 int initiator, int argc, char **argv,
239 char *combinedopt, int retry_count);
240 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
241 static int cctl_inquiry_vpd_devid(int fd, int lun, int initiator);
242 static int cctl_report_target_port_group(int fd, int lun, int initiator);
243 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
244 static int cctl_portlist(int fd, int argc, char **argv, char *combinedopt);
247 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
248 ctladm_cmdargs *argnum, const char **subopt)
250 struct ctladm_opts *opts;
253 for (opts = table; (opts != NULL) && (opts->optname != NULL);
255 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
256 *cmdnum = opts->cmdnum;
257 *argnum = opts->argnum;
258 *subopt = opts->subopt;
260 if (strcmp(opts->optname, arg) == 0)
261 return (CC_OR_FOUND);
263 if (++num_matches > 1)
264 return(CC_OR_AMBIGUOUS);
271 return(CC_OR_NOT_FOUND);
275 cctl_dump_ooa(int fd, int argc, char **argv)
278 long double cmd_latency;
279 int num_entries, len, lun = -1, retval = 0;
284 if ((argc > 2) && (isdigit(argv[2][0])))
285 lun = strtol(argv[2], NULL, 0);
288 len = num_entries * sizeof(struct ctl_ooa_entry);
289 bzero(&ooa, sizeof(ooa));
290 ooa.entries = malloc(len);
291 if (ooa.entries == NULL) {
292 warn("%s: error mallocing %d bytes", __func__, len);
298 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
300 ooa.alloc_num = num_entries;
301 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
302 warn("%s: CTL_GET_OOA ioctl failed", __func__);
307 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
308 num_entries = num_entries * 2;
314 if (ooa.status != CTL_OOA_OK) {
315 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
321 fprintf(stdout, "Dumping OOA queues\n");
322 for (i = 0; i < ooa.fill_num; i++) {
323 struct ctl_ooa_entry *entry;
324 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
325 struct bintime delta_bt;
328 entry = &ooa.entries[i];
330 delta_bt = ooa.cur_bt;
331 bintime_sub(&delta_bt, &entry->start_bt);
332 bintime2timespec(&delta_bt, &ts);
333 cmd_latency = ts.tv_sec * 1000;
335 cmd_latency += ts.tv_nsec / 1000000;
337 fprintf(stdout, "LUN %jd tag 0x%jx%s%s%s%s%s%s%s: %s. CDB: %s "
339 (intmax_t)entry->lun_num, entry->tag_num,
340 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
342 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
343 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
345 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
346 (entry->cmd_flags & CTL_OOACMD_FLAG_STATUS_QUEUED) ?
347 " STATUSQUEUED" : "",
348 (entry->cmd_flags & CTL_OOACMD_FLAG_STATUS_SENT) ? " STATUS" : "",
349 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
351 scsi_op_desc(entry->cdb[0], NULL),
352 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
355 fprintf(stdout, "OOA queues dump done\n");
363 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
365 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
378 CCTL_PORT_MODE_CREATE,
379 CCTL_PORT_MODE_REMOVE
382 static struct ctladm_opts cctl_fe_table[] = {
383 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
384 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
385 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
386 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
387 {"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
388 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
393 cctl_port(int fd, int argc, char **argv, char *combinedopt)
396 int32_t targ_port = -1;
398 int wwnn_set = 0, wwpn_set = 0;
399 uint64_t wwnn = 0, wwpn = 0;
400 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
401 struct ctl_port_entry entry;
404 nvlist_t *option_list;
405 ctl_port_type port_type = CTL_PORT_NONE;
406 int quiet = 0, xml = 0;
408 option_list = nvlist_create(0);
409 if (option_list == NULL)
410 err(1, "%s: unable to allocate nvlist", __func__);
412 while ((c = getopt(argc, argv, combinedopt)) != -1) {
415 if (port_mode != CCTL_PORT_MODE_NONE)
418 port_mode = CCTL_PORT_MODE_LIST;
421 port_mode = CCTL_PORT_MODE_CREATE;
424 port_mode = CCTL_PORT_MODE_REMOVE;
427 if (port_mode != CCTL_PORT_MODE_NONE)
430 if (strcasecmp(optarg, "on") == 0)
431 port_mode = CCTL_PORT_MODE_ON;
432 else if (strcasecmp(optarg, "off") == 0)
433 port_mode = CCTL_PORT_MODE_OFF;
435 warnx("Invalid -o argument %s, \"on\" or "
436 "\"off\" are the only valid args",
446 tmpstr = strdup(optarg);
447 name = strsep(&tmpstr, "=");
449 warnx("%s: option -O takes \"name=value\""
450 "argument", __func__);
454 value = strsep(&tmpstr, "=");
456 warnx("%s: option -O takes \"name=value\""
457 "argument", __func__);
463 nvlist_add_string(option_list, name, value);
467 if (driver != NULL) {
468 warnx("%s: option -d cannot be specified twice",
474 driver = strdup(optarg);
477 targ_port = strtol(optarg, NULL, 0);
483 ctladm_optret optret;
484 ctladm_cmdargs argnum;
486 ctl_port_type tmp_port_type;
488 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
490 if (optret == CC_OR_AMBIGUOUS) {
491 warnx("%s: ambiguous frontend type %s",
495 } else if (optret == CC_OR_NOT_FOUND) {
496 warnx("%s: invalid frontend type %s",
502 port_type |= tmp_port_type;
506 if ((port_mode != CCTL_PORT_MODE_NONE)
507 && (port_mode != CCTL_PORT_MODE_SET))
510 port_mode = CCTL_PORT_MODE_SET;
512 wwnn = strtoull(optarg, NULL, 0);
516 if ((port_mode != CCTL_PORT_MODE_NONE)
517 && (port_mode != CCTL_PORT_MODE_SET))
520 port_mode = CCTL_PORT_MODE_SET;
522 wwpn = strtoull(optarg, NULL, 0);
532 driver = strdup("ioctl");
535 * The user can specify either one or more frontend types (-t), or
536 * a specific frontend, but not both.
538 * If the user didn't specify a frontend type or number, set it to
539 * all. This is primarily needed for the enable/disable ioctls.
540 * This will be a no-op for the listing code. For the set ioctl,
541 * we'll throw an error, since that only works on one port at a time.
543 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
544 warnx("%s: can only specify one of -t or -n", __func__);
547 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
548 port_type = CTL_PORT_ALL;
550 bzero(&entry, sizeof(entry));
553 * These are needed for all but list/dump mode.
555 entry.port_type = port_type;
556 entry.targ_port = targ_port;
559 case CCTL_PORT_MODE_LIST: {
569 argvx[argcx++] = argx;
571 argvx[argcx++] = argq;
572 cctl_portlist(fd, argcx, argvx, opts);
575 case CCTL_PORT_MODE_REMOVE:
576 if (targ_port == -1) {
577 warnx("%s: -r require -p", __func__);
581 case CCTL_PORT_MODE_CREATE: {
582 bzero(&req, sizeof(req));
583 strlcpy(req.driver, driver, sizeof(req.driver));
585 if (port_mode == CCTL_PORT_MODE_REMOVE) {
586 req.reqtype = CTL_REQ_REMOVE;
587 nvlist_add_stringf(option_list, "port_id", "%d",
590 req.reqtype = CTL_REQ_CREATE;
592 req.args = nvlist_pack(option_list, &req.args_len);
593 if (req.args == NULL) {
594 warn("%s: error packing nvlist", __func__);
599 retval = ioctl(fd, CTL_PORT_REQ, &req);
602 warn("%s: CTL_PORT_REQ ioctl failed", __func__);
607 switch (req.status) {
609 warnx("error: %s", req.error_str);
612 case CTL_LUN_WARNING:
613 warnx("warning: %s", req.error_str);
618 warnx("unknown status: %d", req.status);
625 case CCTL_PORT_MODE_SET:
626 if (targ_port == -1) {
627 warnx("%s: -w and -W require -n", __func__);
633 entry.flags |= CTL_PORT_WWNN_VALID;
637 entry.flags |= CTL_PORT_WWPN_VALID;
641 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
642 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
647 case CCTL_PORT_MODE_ON:
648 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
649 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
653 fprintf(stdout, "Front End Ports enabled\n");
655 case CCTL_PORT_MODE_OFF:
656 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
657 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
661 fprintf(stdout, "Front End Ports disabled\n");
664 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
671 nvlist_destroy(option_list);
676 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
681 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
684 if (ioctl(fd, CTL_IO, io) == -1) {
685 warn("%s: error sending CTL_IO ioctl", func);
688 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
695 cctl_delay(int fd, int lun, int argc, char **argv,
698 struct ctl_io_delay_info delay_info;
699 char *delayloc = NULL;
700 char *delaytype = NULL;
707 memset(&delay_info, 0, sizeof(delay_info));
709 while ((c = getopt(argc, argv, combinedopt)) != -1) {
712 delaytype = strdup(optarg);
715 delayloc = strdup(optarg);
718 delaytime = strtoul(optarg, NULL, 0);
723 if (delaytime == -1) {
724 warnx("%s: you must specify the delaytime with -t", __func__);
729 if (strcasecmp(delayloc, "datamove") == 0)
730 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
731 else if (strcasecmp(delayloc, "done") == 0)
732 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
734 warnx("%s: invalid delay location %s", __func__, delayloc);
739 if ((delaytype == NULL)
740 || (strcmp(delaytype, "oneshot") == 0))
741 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
742 else if (strcmp(delaytype, "cont") == 0)
743 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
745 warnx("%s: invalid delay type %s", __func__, delaytype);
750 delay_info.lun_id = lun;
751 delay_info.delay_secs = delaytime;
753 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
754 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
758 switch (delay_info.status) {
759 case CTL_DELAY_STATUS_NONE:
760 warnx("%s: no delay status??", __func__);
763 case CTL_DELAY_STATUS_OK:
765 case CTL_DELAY_STATUS_INVALID_LUN:
766 warnx("%s: invalid lun %d", __func__, lun);
769 case CTL_DELAY_STATUS_INVALID_TYPE:
770 warnx("%s: invalid delay type %d", __func__,
771 delay_info.delay_type);
774 case CTL_DELAY_STATUS_INVALID_LOC:
775 warnx("%s: delay location %s not implemented?", __func__,
779 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
780 warnx("%s: delay not implemented in the kernel", __func__);
781 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
785 warnx("%s: unknown delay return status %d", __func__,
797 static struct ctladm_opts cctl_err_types[] = {
798 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
799 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
800 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
801 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
806 static struct ctladm_opts cctl_err_patterns[] = {
807 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
808 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
809 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
810 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
811 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
812 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
813 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
815 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
821 cctl_error_inject(int fd, uint32_t lun, int argc, char **argv,
825 struct ctl_error_desc err_desc;
828 uint64_t delete_id = 0;
829 int delete_id_set = 0;
835 bzero(&err_desc, sizeof(err_desc));
836 err_desc.lun_id = lun;
838 while ((c = getopt(argc, argv, combinedopt)) != -1) {
844 delete_id = strtoull(optarg, NULL, 0);
849 ctladm_optret optret;
850 ctladm_cmdargs argnum;
854 ctl_lun_error err_type;
856 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
857 warnx("%s: can't specify multiple -i "
858 "arguments", __func__);
862 optret = getoption(cctl_err_types, optarg,
863 &err_type, &argnum, &subopt);
864 err_desc.lun_error = err_type;
866 ctl_lun_error_pattern pattern;
868 optret = getoption(cctl_err_patterns, optarg,
869 &pattern, &argnum, &subopt);
870 err_desc.error_pattern |= pattern;
873 if (optret == CC_OR_AMBIGUOUS) {
874 warnx("%s: ambiguous argument %s", __func__,
878 } else if (optret == CC_OR_NOT_FOUND) {
879 warnx("%s: argument %s not found", __func__,
887 char *tmpstr, *tmpstr2;
889 tmpstr = strdup(optarg);
890 if (tmpstr == NULL) {
891 warn("%s: error duplicating string %s",
897 tmpstr2 = strsep(&tmpstr, ",");
898 if (tmpstr2 == NULL) {
899 warnx("%s: invalid -r argument %s", __func__,
905 lba = strtoull(tmpstr2, NULL, 0);
906 tmpstr2 = strsep(&tmpstr, ",");
907 if (tmpstr2 == NULL) {
908 warnx("%s: no len argument for -r lba,len, got"
909 " %s", __func__, optarg);
914 len = strtoul(tmpstr2, NULL, 0);
919 struct get_hook hook;
922 sense_len = strtol(optarg, NULL, 0);
923 if (sense_len <= 0) {
924 warnx("invalid number of sense bytes %d",
930 sense_len = MIN(sense_len, SSD_FULL_SIZE);
932 hook.argc = argc - optind;
933 hook.argv = argv + optind;
936 sensestr = cget(&hook, NULL);
937 if ((sensestr != NULL)
938 && (sensestr[0] == '-')) {
942 (uint8_t *)&err_desc.custom_sense,
943 sense_len, sensestr, iget, &hook);
953 if (delete_id_set != 0) {
954 err_desc.serial = delete_id;
955 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
956 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
963 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
964 warnx("%s: error injection command (-i) needed",
968 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
969 && (sense_len == 0)) {
970 warnx("%s: custom error requires -s", __func__);
976 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
979 * If fd_sense is set, we need to read the sense data the user
980 * wants returned from stdin.
984 int amt_to_read = sense_len;
985 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
987 for (amt_read = 0; amt_to_read > 0;
988 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
989 if (amt_read == -1) {
990 warn("error reading sense data from stdin");
994 amt_to_read -= amt_read;
999 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1000 warnx("%s: command pattern (-p) needed", __func__);
1006 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1008 * We could check here to see whether it's a read/write
1009 * command, but that will be pointless once we allow
1010 * custom patterns. At that point, the user could specify
1011 * a READ(6) CDB type, and we wouldn't have an easy way here
1012 * to verify whether range checking is possible there. The
1013 * user will just figure it out when his error never gets
1017 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1018 warnx("%s: need read and/or write pattern if range "
1019 "is specified", __func__);
1024 err_desc.lba_range.lba = lba;
1025 err_desc.lba_range.len = len;
1028 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1029 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1032 printf("Error injection succeeded, serial number is %ju\n",
1033 (uintmax_t)err_desc.serial);
1041 cctl_lunlist(int fd)
1043 struct scsi_report_luns_data *lun_data;
1044 struct scsi_inquiry_data *inq_data;
1054 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1055 * if we ever acquire the ability to have multiple targets.
1057 if ((retval = cctl_get_luns(fd, /*lun*/ 0, initid,
1058 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1061 inq_data = malloc(sizeof(*inq_data));
1062 if (inq_data == NULL) {
1063 warn("%s: couldn't allocate memory for inquiry data\n",
1068 for (i = 0; i < num_luns; i++) {
1072 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1073 case RPL_LUNDATA_ATYP_PERIPH:
1074 lun_val = lun_data->luns[i].lundata[1];
1076 case RPL_LUNDATA_ATYP_FLAT:
1077 lun_val = (lun_data->luns[i].lundata[0] &
1078 RPL_LUNDATA_FLAT_LUN_MASK) |
1079 (lun_data->luns[i].lundata[1] <<
1080 RPL_LUNDATA_FLAT_LUN_BITS);
1082 case RPL_LUNDATA_ATYP_LUN:
1083 case RPL_LUNDATA_ATYP_EXTLUN:
1085 fprintf(stdout, "Unsupported LUN format %d\n",
1086 lun_data->luns[i].lundata[0] &
1087 RPL_LUNDATA_ATYP_MASK);
1094 if ((retval = cctl_get_inquiry(fd, lun_val, initid,
1095 /*retries*/ 2, scsi_path,
1100 printf("%s", scsi_path);
1101 scsi_print_inquiry(inq_data);
1105 if (lun_data != NULL)
1108 if (inq_data != NULL)
1115 cctl_sync_cache(int fd, int lun, int iid, int retries,
1116 int argc, char **argv, char *combinedopt)
1121 uint64_t our_lba = 0;
1122 uint32_t our_block_count = 0;
1123 int reladr = 0, immed = 0;
1128 io = ctl_scsi_alloc_io(iid);
1130 warnx("%s: can't allocate memory", __func__);
1134 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1137 our_block_count = strtoul(optarg, NULL, 0);
1140 cdb_size = strtol(optarg, NULL, 0);
1146 our_lba = strtoull(optarg, NULL, 0);
1156 if (cdb_size != -1) {
1162 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1163 "and 16", __func__, cdb_size);
1166 break; /* NOTREACHED */
1171 ctl_scsi_sync_cache(/*io*/ io,
1174 /*minimum_cdb_size*/ cdb_size,
1175 /*starting_lba*/ our_lba,
1176 /*block_count*/ our_block_count,
1177 /*tag_type*/ CTL_TAG_SIMPLE,
1180 io->io_hdr.nexus.targ_lun = lun;
1181 io->io_hdr.nexus.initid = iid;
1183 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1188 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1189 fprintf(stdout, "Cache synchronized successfully\n");
1191 ctl_io_error_print(io, NULL, stderr);
1193 ctl_scsi_free_io(io);
1199 cctl_start_stop(int fd, int lun, int iid, int retries, int start,
1200 int argc, char **argv, char *combinedopt)
1209 io = ctl_scsi_alloc_io(iid);
1211 warnx("%s: can't allocate memory", __func__);
1215 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1225 * Use an ordered tag for the stop command, to guarantee that any
1226 * pending I/O will finish before the stop command executes. This
1227 * would normally be the case anyway, since CTL will basically
1228 * treat the start/stop command as an ordered command with respect
1229 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1231 ctl_scsi_start_stop(/*io*/ io,
1234 /*immediate*/ immed,
1235 /*power_conditions*/ SSS_PC_START_VALID,
1236 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1240 io->io_hdr.nexus.targ_lun = lun;
1241 io->io_hdr.nexus.initid = iid;
1243 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1248 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1249 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1250 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1251 (start) ? "started" : "stopped");
1253 ctl_io_error_print(io, NULL, stderr);
1256 ctl_scsi_free_io(io);
1262 cctl_mode_sense(int fd, int lun, int iid, int retries,
1263 int argc, char **argv, char *combinedopt)
1268 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1277 io = ctl_scsi_alloc_io(iid);
1279 warn("%s: can't allocate memory", __func__);
1283 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1286 pc = strtoul(optarg, NULL, 0);
1289 subpage = strtoul(optarg, NULL, 0);
1298 page_code = strtoul(optarg, NULL, 0);
1301 cdbsize = strtol(optarg, NULL, 0);
1308 if (((list == 0) && (page_code == -1))
1309 || ((list != 0) && (page_code != -1))) {
1310 warnx("%s: you must specify either a page code (-m) or -l",
1316 if ((page_code != -1)
1317 && ((page_code > SMS_ALL_PAGES_PAGE)
1318 || (page_code < 0))) {
1319 warnx("%s: page code %d is out of range", __func__,
1326 page_code = SMS_ALL_PAGES_PAGE;
1328 warnx("%s: arg -P makes no sense with -l",
1333 if (subpage != -1) {
1334 warnx("%s: arg -S makes no sense with -l", __func__);
1341 pc = SMS_PAGE_CTRL_CURRENT;
1345 warnx("%s: page control value %d is out of range: 0-3",
1355 || (subpage < 0))) {
1356 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1367 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1368 "and 10", __func__, cdbsize);
1384 dataptr = (uint8_t *)malloc(datalen);
1385 if (dataptr == NULL) {
1386 warn("%s: can't allocate %d bytes", __func__, datalen);
1391 memset(dataptr, 0, datalen);
1393 ctl_scsi_mode_sense(io,
1394 /*data_ptr*/ dataptr,
1395 /*data_len*/ datalen,
1398 /*page_code*/ page_code,
1400 /*subpage*/ subpage,
1401 /*minimum_cdb_size*/ cdbsize,
1402 /*tag_type*/ CTL_TAG_SIMPLE,
1405 io->io_hdr.nexus.targ_lun = lun;
1406 io->io_hdr.nexus.initid = iid;
1408 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1413 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1414 int pages_len, used_len;
1415 uint32_t returned_len;
1418 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1419 struct scsi_mode_hdr_6 *hdr6;
1422 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1424 returned_len = hdr6->datalen + 1;
1425 bdlen = hdr6->block_descr_len;
1427 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1429 struct scsi_mode_hdr_10 *hdr10;
1432 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
1434 returned_len = scsi_2btoul(hdr10->datalen) + 2;
1435 bdlen = scsi_2btoul(hdr10->block_descr_len);
1437 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
1439 /* just in case they can give us more than we allocated for */
1440 returned_len = min(returned_len, datalen);
1441 pages_len = returned_len - (ndataptr - dataptr);
1443 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
1444 returned_len, pages_len);
1447 fprintf(stdout, "Supported mode pages:\n");
1448 for (used_len = 0; used_len < pages_len;) {
1449 struct scsi_mode_page_header *header;
1451 header = (struct scsi_mode_page_header *)
1452 &ndataptr[used_len];
1453 fprintf(stdout, "%d\n", header->page_code);
1454 used_len += header->page_length + 2;
1457 for (used_len = 0; used_len < pages_len; used_len++) {
1458 fprintf(stdout, "0x%x ", ndataptr[used_len]);
1459 if (((used_len+1) % 16) == 0)
1460 fprintf(stdout, "\n");
1462 fprintf(stdout, "\n");
1465 ctl_io_error_print(io, NULL, stderr);
1468 ctl_scsi_free_io(io);
1470 if (dataptr != NULL)
1477 cctl_read_capacity(int fd, int lun, int iid, int retries,
1478 int argc, char **argv, char *combinedopt)
1481 struct scsi_read_capacity_data *data;
1482 struct scsi_read_capacity_data_long *longdata;
1483 int cdbsize = -1, retval;
1491 io = ctl_scsi_alloc_io(iid);
1493 warn("%s: can't allocate memory\n", __func__);
1497 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1500 cdbsize = strtol(optarg, NULL, 0);
1506 if (cdbsize != -1) {
1512 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1513 "and 16", __func__, cdbsize);
1516 break; /* NOTREACHED */
1521 dataptr = (uint8_t *)malloc(sizeof(*longdata));
1522 if (dataptr == NULL) {
1523 warn("%s: can't allocate %zd bytes\n", __func__,
1528 memset(dataptr, 0, sizeof(*longdata));
1534 ctl_scsi_read_capacity(io,
1535 /*data_ptr*/ dataptr,
1536 /*data_len*/ sizeof(*longdata),
1540 /*tag_type*/ CTL_TAG_SIMPLE,
1544 ctl_scsi_read_capacity_16(io,
1545 /*data_ptr*/ dataptr,
1546 /*data_len*/ sizeof(*longdata),
1550 /*tag_type*/ CTL_TAG_SIMPLE,
1555 io->io_hdr.nexus.initid = iid;
1556 io->io_hdr.nexus.targ_lun = lun;
1558 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1563 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1567 if (cdbsize == 10) {
1569 data = (struct scsi_read_capacity_data *)dataptr;
1571 maxlba = scsi_4btoul(data->addr);
1572 blocksize = scsi_4btoul(data->length);
1574 if (maxlba == 0xffffffff) {
1579 longdata=(struct scsi_read_capacity_data_long *)dataptr;
1581 maxlba = scsi_8btou64(longdata->addr);
1582 blocksize = scsi_4btoul(longdata->length);
1585 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
1586 (uintmax_t)maxlba, blocksize);
1588 ctl_io_error_print(io, NULL, stderr);
1591 ctl_scsi_free_io(io);
1593 if (dataptr != NULL)
1600 cctl_read_write(int fd, int lun, int iid, int retries,
1601 int argc, char **argv, char *combinedopt,
1602 ctladm_cmdfunction command)
1605 int file_fd, do_stdio;
1606 int cdbsize = -1, databytes;
1608 char *filename = NULL;
1609 int datalen = -1, blocksize = -1;
1620 io = ctl_scsi_alloc_io(iid);
1622 warn("%s: can't allocate memory\n", __func__);
1626 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1629 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
1632 blocksize = strtoul(optarg, NULL, 0);
1635 cdbsize = strtoul(optarg, NULL, 0);
1638 datalen = strtoul(optarg, NULL, 0);
1641 filename = strdup(optarg);
1644 lba = strtoull(optarg, NULL, 0);
1651 if (filename == NULL) {
1652 warnx("%s: you must supply a filename using -f", __func__);
1657 if (datalen == -1) {
1658 warnx("%s: you must specify the data length with -d", __func__);
1664 warnx("%s: you must specify the LBA with -l", __func__);
1669 if (blocksize == -1) {
1670 warnx("%s: you must specify the blocksize with -b", __func__);
1675 if (cdbsize != -1) {
1683 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
1684 "10, 12 or 16", __func__, cdbsize);
1687 break; /* NOTREACHED */
1692 databytes = datalen * blocksize;
1693 dataptr = (uint8_t *)malloc(databytes);
1695 if (dataptr == NULL) {
1696 warn("%s: can't allocate %d bytes\n", __func__, databytes);
1700 if (strcmp(filename, "-") == 0) {
1701 if (command == CTLADM_CMD_READ)
1702 file_fd = STDOUT_FILENO;
1704 file_fd = STDIN_FILENO;
1707 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1708 if (file_fd == -1) {
1709 warn("%s: can't open file %s", __func__, filename);
1715 memset(dataptr, 0, databytes);
1717 if (command == CTLADM_CMD_WRITE) {
1720 bytes_read = read(file_fd, dataptr, databytes);
1721 if (bytes_read == -1) {
1722 warn("%s: error reading file %s", __func__, filename);
1726 if (bytes_read != databytes) {
1727 warnx("%s: only read %d bytes from file %s",
1728 __func__, bytes_read, filename);
1733 ctl_scsi_read_write(io,
1734 /*data_ptr*/ dataptr,
1735 /*data_len*/ databytes,
1736 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
1738 /*minimum_cdb_size*/ cdbsize,
1740 /*num_blocks*/ datalen,
1741 /*tag_type*/ CTL_TAG_SIMPLE,
1744 io->io_hdr.nexus.targ_lun = lun;
1745 io->io_hdr.nexus.initid = iid;
1747 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1752 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1753 && (command == CTLADM_CMD_READ)) {
1756 bytes_written = write(file_fd, dataptr, databytes);
1757 if (bytes_written == -1) {
1758 warn("%s: can't write to %s", __func__, filename);
1761 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1762 ctl_io_error_print(io, NULL, stderr);
1767 ctl_scsi_free_io(io);
1769 if (dataptr != NULL)
1780 cctl_get_luns(int fd, int lun, int iid, int retries, struct
1781 scsi_report_luns_data **lun_data, uint32_t *num_luns)
1790 io = ctl_scsi_alloc_io(iid);
1792 warnx("%s: can't allocate memory", __func__);
1797 * lun_data includes space for 1 lun, allocate space for 4 initially.
1798 * If that isn't enough, we'll allocate more.
1802 lun_datalen = sizeof(*lun_data) +
1803 (nluns * sizeof(struct scsi_report_luns_lundata));
1804 *lun_data = malloc(lun_datalen);
1806 if (*lun_data == NULL) {
1807 warnx("%s: can't allocate memory", __func__);
1808 ctl_scsi_free_io(io);
1812 ctl_scsi_report_luns(io,
1813 /*data_ptr*/ (uint8_t *)*lun_data,
1814 /*data_len*/ lun_datalen,
1815 /*select_report*/ RPL_REPORT_ALL,
1816 /*tag_type*/ CTL_TAG_SIMPLE,
1819 io->io_hdr.nexus.initid = iid;
1820 io->io_hdr.nexus.targ_lun = lun;
1822 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1827 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1828 uint32_t returned_len, returned_luns;
1830 returned_len = scsi_4btoul((*lun_data)->length);
1831 returned_luns = returned_len / 8;
1832 if (returned_luns > nluns) {
1833 nluns = returned_luns;
1837 /* These should be the same */
1838 *num_luns = MIN(returned_luns, nluns);
1840 ctl_io_error_print(io, NULL, stderr);
1844 ctl_scsi_free_io(io);
1850 cctl_report_luns(int fd, int lun, int iid, int retries)
1852 struct scsi_report_luns_data *lun_data;
1853 uint32_t num_luns, i;
1858 if ((retval = cctl_get_luns(fd, lun, iid, retries, &lun_data,
1862 fprintf(stdout, "%u LUNs returned\n", num_luns);
1863 for (i = 0; i < num_luns; i++) {
1867 * XXX KDM figure out a way to share this code with
1870 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1871 case RPL_LUNDATA_ATYP_PERIPH:
1872 lun_val = lun_data->luns[i].lundata[1];
1874 case RPL_LUNDATA_ATYP_FLAT:
1875 lun_val = (lun_data->luns[i].lundata[0] &
1876 RPL_LUNDATA_FLAT_LUN_MASK) |
1877 (lun_data->luns[i].lundata[1] <<
1878 RPL_LUNDATA_FLAT_LUN_BITS);
1880 case RPL_LUNDATA_ATYP_LUN:
1881 case RPL_LUNDATA_ATYP_EXTLUN:
1883 fprintf(stdout, "Unsupported LUN format %d\n",
1884 lun_data->luns[i].lundata[0] &
1885 RPL_LUNDATA_ATYP_MASK);
1892 fprintf(stdout, "%d\n", lun_val);
1896 if (lun_data != NULL)
1903 cctl_tur(int fd, int lun, int iid, int retries)
1907 io = ctl_scsi_alloc_io(iid);
1909 fprintf(stderr, "can't allocate memory\n");
1914 /* tag_type */ CTL_TAG_SIMPLE,
1917 io->io_hdr.nexus.targ_lun = lun;
1918 io->io_hdr.nexus.initid = iid;
1920 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1921 ctl_scsi_free_io(io);
1925 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1926 fprintf(stdout, "Unit is ready\n");
1928 ctl_io_error_print(io, NULL, stderr);
1934 cctl_get_inquiry(int fd, int lun, int iid, int retries,
1935 char *path_str, int path_len,
1936 struct scsi_inquiry_data *inq_data)
1943 io = ctl_scsi_alloc_io(iid);
1945 warnx("cctl_inquiry: can't allocate memory\n");
1949 ctl_scsi_inquiry(/*io*/ io,
1950 /*data_ptr*/ (uint8_t *)inq_data,
1951 /*data_len*/ sizeof(*inq_data),
1954 /*tag_type*/ CTL_TAG_SIMPLE,
1957 io->io_hdr.nexus.targ_lun = lun;
1958 io->io_hdr.nexus.initid = iid;
1960 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1965 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
1967 ctl_io_error_print(io, NULL, stderr);
1968 } else if (path_str != NULL)
1969 ctl_scsi_path_string(io, path_str, path_len);
1972 ctl_scsi_free_io(io);
1978 cctl_inquiry(int fd, int lun, int iid, int retries)
1980 struct scsi_inquiry_data *inq_data;
1984 inq_data = malloc(sizeof(*inq_data));
1985 if (inq_data == NULL) {
1986 warnx("%s: can't allocate inquiry data", __func__);
1991 if ((retval = cctl_get_inquiry(fd, lun, iid, retries, scsi_path,
1992 sizeof(scsi_path), inq_data)) != 0)
1995 printf("%s", scsi_path);
1996 scsi_print_inquiry(inq_data);
1999 if (inq_data != NULL)
2006 cctl_req_sense(int fd, int lun, int iid, int retries)
2009 struct scsi_sense_data *sense_data;
2014 io = ctl_scsi_alloc_io(iid);
2016 warnx("cctl_req_sense: can't allocate memory\n");
2019 sense_data = malloc(sizeof(*sense_data));
2020 memset(sense_data, 0, sizeof(*sense_data));
2022 ctl_scsi_request_sense(/*io*/ io,
2023 /*data_ptr*/ (uint8_t *)sense_data,
2024 /*data_len*/ sizeof(*sense_data),
2026 /*tag_type*/ CTL_TAG_SIMPLE,
2029 io->io_hdr.nexus.targ_lun = lun;
2030 io->io_hdr.nexus.initid = iid;
2032 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2037 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2038 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2039 io->scsiio.sense_len = sizeof(*sense_data);
2040 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2042 ctl_io_error_print(io, NULL, stderr);
2046 ctl_scsi_free_io(io);
2053 cctl_report_target_port_group(int fd, int lun, int iid)
2063 io = ctl_scsi_alloc_io(iid);
2065 warn("%s: can't allocate memory", __func__);
2070 dataptr = (uint8_t *)malloc(datalen);
2071 if (dataptr == NULL) {
2072 warn("%s: can't allocate %d bytes", __func__, datalen);
2077 memset(dataptr, 0, datalen);
2079 ctl_scsi_maintenance_in(/*io*/ io,
2080 /*data_ptr*/ dataptr,
2081 /*data_len*/ datalen,
2082 /*action*/ SA_RPRT_TRGT_GRP,
2083 /*tag_type*/ CTL_TAG_SIMPLE,
2086 io->io_hdr.nexus.targ_lun = lun;
2087 io->io_hdr.nexus.initid = iid;
2089 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2094 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2095 int returned_len, used_len;
2097 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2099 for (used_len = 0; used_len < returned_len; used_len++) {
2100 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2101 if (((used_len+1) % 8) == 0)
2102 fprintf(stdout, "\n");
2104 fprintf(stdout, "\n");
2106 ctl_io_error_print(io, NULL, stderr);
2109 ctl_scsi_free_io(io);
2111 if (dataptr != NULL)
2118 cctl_inquiry_vpd_devid(int fd, int lun, int iid)
2128 io = ctl_scsi_alloc_io(iid);
2130 warn("%s: can't allocate memory", __func__);
2135 dataptr = (uint8_t *)malloc(datalen);
2136 if (dataptr == NULL) {
2137 warn("%s: can't allocate %d bytes", __func__, datalen);
2142 memset(dataptr, 0, datalen);
2144 ctl_scsi_inquiry(/*io*/ io,
2145 /*data_ptr*/ dataptr,
2146 /*data_len*/ datalen,
2148 /*page_code*/ SVPD_DEVICE_ID,
2149 /*tag_type*/ CTL_TAG_SIMPLE,
2152 io->io_hdr.nexus.targ_lun = lun;
2153 io->io_hdr.nexus.initid = iid;
2155 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2160 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2161 int returned_len, used_len;
2163 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2165 for (used_len = 0; used_len < returned_len; used_len++) {
2166 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2167 if (((used_len+1) % 8) == 0)
2168 fprintf(stdout, "\n");
2170 fprintf(stdout, "\n");
2172 ctl_io_error_print(io, NULL, stderr);
2175 ctl_scsi_free_io(io);
2177 if (dataptr != NULL)
2184 cctl_persistent_reserve_in(int fd, int lun, int iid,
2185 int argc, char **argv, char *combinedopt,
2198 io = ctl_scsi_alloc_io(iid);
2200 warn("%s: can't allocate memory", __func__);
2204 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2207 action = strtol(optarg, NULL, 0);
2214 if (action < 0 || action > 2) {
2215 warn("action must be specified and in the range: 0-2");
2222 dataptr = (uint8_t *)malloc(datalen);
2223 if (dataptr == NULL) {
2224 warn("%s: can't allocate %d bytes", __func__, datalen);
2229 memset(dataptr, 0, datalen);
2231 ctl_scsi_persistent_res_in(io,
2232 /*data_ptr*/ dataptr,
2233 /*data_len*/ datalen,
2235 /*tag_type*/ CTL_TAG_SIMPLE,
2238 io->io_hdr.nexus.targ_lun = lun;
2239 io->io_hdr.nexus.initid = iid;
2241 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2246 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2247 int returned_len, used_len;
2251 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2252 returned_len = min(returned_len, 256);
2255 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2261 warnx("%s: invalid action %d", __func__, action);
2263 break; /* NOTREACHED */
2266 for (used_len = 0; used_len < returned_len; used_len++) {
2267 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2268 if (((used_len+1) % 8) == 0)
2269 fprintf(stdout, "\n");
2271 fprintf(stdout, "\n");
2273 ctl_io_error_print(io, NULL, stderr);
2276 ctl_scsi_free_io(io);
2278 if (dataptr != NULL)
2285 cctl_persistent_reserve_out(int fd, int lun, int iid,
2286 int argc, char **argv, char *combinedopt,
2291 uint64_t key = 0, sa_key = 0;
2292 int action = -1, restype = -1;
2300 io = ctl_scsi_alloc_io(iid);
2302 warn("%s: can't allocate memory", __func__);
2306 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2309 action = strtol(optarg, NULL, 0);
2312 key = strtoull(optarg, NULL, 0);
2315 restype = strtol(optarg, NULL, 0);
2318 sa_key = strtoull(optarg, NULL, 0);
2324 if (action < 0 || action > 5) {
2325 warn("action must be specified and in the range: 0-5");
2330 if (restype < 0 || restype > 5) {
2331 if (action != 0 && action != 5 && action != 3) {
2332 warn("'restype' must specified and in the range: 0-5");
2339 dataptr = (uint8_t *)malloc(datalen);
2340 if (dataptr == NULL) {
2341 warn("%s: can't allocate %d bytes", __func__, datalen);
2346 memset(dataptr, 0, datalen);
2348 ctl_scsi_persistent_res_out(io,
2349 /*data_ptr*/ dataptr,
2350 /*data_len*/ datalen,
2355 /*tag_type*/ CTL_TAG_SIMPLE,
2358 io->io_hdr.nexus.targ_lun = lun;
2359 io->io_hdr.nexus.initid = iid;
2361 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2365 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2367 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2368 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2369 "successfully\n", scsi_path);
2371 ctl_io_error_print(io, NULL, stderr);
2374 ctl_scsi_free_io(io);
2376 if (dataptr != NULL)
2383 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2385 struct ctl_lun_req req;
2386 int device_type = -1;
2387 uint64_t lun_size = 0;
2388 uint32_t blocksize = 0, req_lun_id = 0;
2389 char *serial_num = NULL;
2390 char *device_id = NULL;
2391 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
2392 char *backend_name = NULL;
2393 nvlist_t *option_list;
2396 option_list = nvlist_create(0);
2397 if (option_list == NULL)
2398 err(1, "%s: unable to allocate nvlist", __func__);
2400 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2403 backend_name = strdup(optarg);
2406 blocksize = strtoul(optarg, NULL, 0);
2410 device_id = strdup(optarg);
2413 req_lun_id = strtoul(optarg, NULL, 0);
2420 tmpstr = strdup(optarg);
2421 name = strsep(&tmpstr, "=");
2423 warnx("%s: option -o takes \"name=value\""
2424 "argument", __func__);
2428 value = strsep(&tmpstr, "=");
2429 if (value == NULL) {
2430 warnx("%s: option -o takes \"name=value\""
2431 "argument", __func__);
2436 nvlist_add_string(option_list, name, value);
2440 if (strcasecmp(optarg, "auto") != 0) {
2441 retval = expand_number(optarg, &lun_size);
2443 warn("%s: invalid -s argument",
2452 serial_num = strdup(optarg);
2455 device_type = strtoul(optarg, NULL, 0);
2462 if (backend_name == NULL) {
2463 warnx("%s: backend name (-b) must be specified", __func__);
2468 bzero(&req, sizeof(req));
2470 strlcpy(req.backend, backend_name, sizeof(req.backend));
2471 req.reqtype = CTL_LUNREQ_CREATE;
2473 if (blocksize_set != 0)
2474 req.reqdata.create.blocksize_bytes = blocksize;
2476 if (lun_size_set != 0)
2477 req.reqdata.create.lun_size_bytes = lun_size;
2479 if (lun_id_set != 0) {
2480 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
2481 req.reqdata.create.req_lun_id = req_lun_id;
2484 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
2486 if (device_type != -1)
2487 req.reqdata.create.device_type = device_type;
2489 req.reqdata.create.device_type = T_DIRECT;
2491 if (serial_num != NULL) {
2492 strlcpy(req.reqdata.create.serial_num, serial_num,
2493 sizeof(req.reqdata.create.serial_num));
2494 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
2497 if (device_id != NULL) {
2498 strlcpy(req.reqdata.create.device_id, device_id,
2499 sizeof(req.reqdata.create.device_id));
2500 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
2503 req.args = nvlist_pack(option_list, &req.args_len);
2504 if (req.args == NULL) {
2505 warn("%s: error packing nvlist", __func__);
2510 retval = ioctl(fd, CTL_LUN_REQ, &req);
2513 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2518 switch (req.status) {
2520 warnx("LUN creation error: %s", req.error_str);
2523 case CTL_LUN_WARNING:
2524 warnx("LUN creation warning: %s", req.error_str);
2529 warnx("unknown LUN creation status: %d", req.status);
2534 fprintf(stdout, "LUN created successfully\n");
2535 fprintf(stdout, "backend: %s\n", req.backend);
2536 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
2537 fprintf(stdout, "LUN size: %ju bytes\n",
2538 (uintmax_t)req.reqdata.create.lun_size_bytes);
2539 fprintf(stdout, "blocksize %u bytes\n",
2540 req.reqdata.create.blocksize_bytes);
2541 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
2542 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
2543 fprintf(stdout, "Device ID: %s\n", req.reqdata.create.device_id);
2546 nvlist_destroy(option_list);
2551 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
2553 struct ctl_lun_req req;
2554 uint32_t lun_id = 0;
2556 char *backend_name = NULL;
2557 nvlist_t *option_list;
2560 option_list = nvlist_create(0);
2561 if (option_list == NULL)
2562 err(1, "%s: unable to allocate nvlist", __func__);
2564 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2567 backend_name = strdup(optarg);
2570 lun_id = strtoul(optarg, NULL, 0);
2577 tmpstr = strdup(optarg);
2578 name = strsep(&tmpstr, "=");
2580 warnx("%s: option -o takes \"name=value\""
2581 "argument", __func__);
2585 value = strsep(&tmpstr, "=");
2586 if (value == NULL) {
2587 warnx("%s: option -o takes \"name=value\""
2588 "argument", __func__);
2593 nvlist_add_string(option_list, name, value);
2601 if (backend_name == NULL)
2602 errx(1, "%s: backend name (-b) must be specified", __func__);
2604 if (lun_id_set == 0)
2605 errx(1, "%s: LUN id (-l) must be specified", __func__);
2607 bzero(&req, sizeof(req));
2609 strlcpy(req.backend, backend_name, sizeof(req.backend));
2610 req.reqtype = CTL_LUNREQ_RM;
2612 req.reqdata.rm.lun_id = lun_id;
2614 req.args = nvlist_pack(option_list, &req.args_len);
2615 if (req.args == NULL) {
2616 warn("%s: error packing nvlist", __func__);
2621 retval = ioctl(fd, CTL_LUN_REQ, &req);
2624 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2629 switch (req.status) {
2631 warnx("LUN removal error: %s", req.error_str);
2634 case CTL_LUN_WARNING:
2635 warnx("LUN removal warning: %s", req.error_str);
2640 warnx("unknown LUN removal status: %d", req.status);
2645 printf("LUN %d removed successfully\n", lun_id);
2648 nvlist_destroy(option_list);
2653 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
2655 struct ctl_lun_req req;
2656 uint64_t lun_size = 0;
2657 uint32_t lun_id = 0;
2658 int lun_id_set = 0, lun_size_set = 0;
2659 char *backend_name = NULL;
2660 nvlist_t *option_list;
2663 option_list = nvlist_create(0);
2664 if (option_list == NULL)
2665 err(1, "%s: unable to allocate nvlist", __func__);
2667 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2670 backend_name = strdup(optarg);
2673 lun_id = strtoul(optarg, NULL, 0);
2680 tmpstr = strdup(optarg);
2681 name = strsep(&tmpstr, "=");
2683 warnx("%s: option -o takes \"name=value\""
2684 "argument", __func__);
2688 value = strsep(&tmpstr, "=");
2689 if (value == NULL) {
2690 warnx("%s: option -o takes \"name=value\""
2691 "argument", __func__);
2696 nvlist_add_string(option_list, name, value);
2700 if (strcasecmp(optarg, "auto") != 0) {
2701 retval = expand_number(optarg, &lun_size);
2703 warn("%s: invalid -s argument",
2716 if (backend_name == NULL)
2717 errx(1, "%s: backend name (-b) must be specified", __func__);
2719 if (lun_id_set == 0)
2720 errx(1, "%s: LUN id (-l) must be specified", __func__);
2722 if (lun_size_set == 0 && nvlist_empty(option_list))
2723 errx(1, "%s: size (-s) or options (-o) must be specified",
2726 bzero(&req, sizeof(req));
2728 strlcpy(req.backend, backend_name, sizeof(req.backend));
2729 req.reqtype = CTL_LUNREQ_MODIFY;
2731 req.reqdata.modify.lun_id = lun_id;
2732 req.reqdata.modify.lun_size_bytes = lun_size;
2734 req.args = nvlist_pack(option_list, &req.args_len);
2735 if (req.args == NULL) {
2736 warn("%s: error packing nvlist", __func__);
2741 retval = ioctl(fd, CTL_LUN_REQ, &req);
2744 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2749 switch (req.status) {
2751 warnx("LUN modification error: %s", req.error_str);
2754 case CTL_LUN_WARNING:
2755 warnx("LUN modification warning: %s", req.error_str);
2760 warnx("unknown LUN modification status: %d", req.status);
2765 printf("LUN %d modified successfully\n", lun_id);
2768 nvlist_destroy(option_list);
2772 struct cctl_islist_conn {
2775 char *initiator_addr;
2776 char *initiator_alias;
2779 char *header_digest;
2781 char *max_recv_data_segment_length;
2782 char *max_send_data_segment_length;
2783 char *max_burst_length;
2784 char *first_burst_length;
2788 STAILQ_ENTRY(cctl_islist_conn) links;
2791 struct cctl_islist_data {
2793 STAILQ_HEAD(,cctl_islist_conn) conn_list;
2794 struct cctl_islist_conn *cur_conn;
2796 struct sbuf *cur_sb[32];
2800 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
2803 struct cctl_islist_data *islist;
2804 struct cctl_islist_conn *cur_conn;
2806 islist = (struct cctl_islist_data *)user_data;
2807 cur_conn = islist->cur_conn;
2809 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
2810 sizeof(islist->cur_sb[0])))
2811 errx(1, "%s: too many nesting levels, %zd max", __func__,
2812 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
2814 islist->cur_sb[islist->level] = sbuf_new_auto();
2815 if (islist->cur_sb[islist->level] == NULL)
2816 err(1, "%s: Unable to allocate sbuf", __func__);
2818 if (strcmp(name, "connection") == 0) {
2819 if (cur_conn != NULL)
2820 errx(1, "%s: improper connection element nesting",
2823 cur_conn = calloc(1, sizeof(*cur_conn));
2824 if (cur_conn == NULL)
2825 err(1, "%s: cannot allocate %zd bytes", __func__,
2828 islist->num_conns++;
2829 islist->cur_conn = cur_conn;
2831 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
2833 for (i = 0; attr[i] != NULL; i += 2) {
2834 if (strcmp(attr[i], "id") == 0) {
2835 cur_conn->connection_id =
2836 strtoull(attr[i+1], NULL, 0);
2839 "%s: invalid connection attribute %s = %s",
2840 __func__, attr[i], attr[i+1]);
2847 cctl_islist_end_element(void *user_data, const char *name)
2849 struct cctl_islist_data *islist;
2850 struct cctl_islist_conn *cur_conn;
2853 islist = (struct cctl_islist_data *)user_data;
2854 cur_conn = islist->cur_conn;
2856 if ((cur_conn == NULL)
2857 && (strcmp(name, "ctlislist") != 0))
2858 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
2860 if (islist->cur_sb[islist->level] == NULL)
2861 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
2862 islist->level, name);
2864 sbuf_finish(islist->cur_sb[islist->level]);
2865 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
2867 err(1, "%s can't allocate %zd bytes for string", __func__,
2868 sbuf_len(islist->cur_sb[islist->level]));
2870 sbuf_delete(islist->cur_sb[islist->level]);
2871 islist->cur_sb[islist->level] = NULL;
2874 if (strcmp(name, "initiator") == 0) {
2875 cur_conn->initiator = str;
2877 } else if (strcmp(name, "initiator_addr") == 0) {
2878 cur_conn->initiator_addr = str;
2880 } else if (strcmp(name, "initiator_alias") == 0) {
2881 cur_conn->initiator_alias = str;
2883 } else if (strcmp(name, "target") == 0) {
2884 cur_conn->target = str;
2886 } else if (strcmp(name, "target_alias") == 0) {
2887 cur_conn->target_alias = str;
2889 } else if (strcmp(name, "target_portal_group_tag") == 0) {
2890 } else if (strcmp(name, "header_digest") == 0) {
2891 cur_conn->header_digest = str;
2893 } else if (strcmp(name, "data_digest") == 0) {
2894 cur_conn->data_digest = str;
2896 } else if (strcmp(name, "max_recv_data_segment_length") == 0) {
2897 cur_conn->max_recv_data_segment_length = str;
2899 } else if (strcmp(name, "max_send_data_segment_length") == 0) {
2900 cur_conn->max_send_data_segment_length = str;
2902 } else if (strcmp(name, "max_burst_length") == 0) {
2903 cur_conn->max_burst_length = str;
2905 } else if (strcmp(name, "first_burst_length") == 0) {
2906 cur_conn->first_burst_length = str;
2908 } else if (strcmp(name, "offload") == 0) {
2909 cur_conn->offload = str;
2911 } else if (strcmp(name, "immediate_data") == 0) {
2912 cur_conn->immediate_data = atoi(str);
2913 } else if (strcmp(name, "iser") == 0) {
2914 cur_conn->iser = atoi(str);
2915 } else if (strcmp(name, "connection") == 0) {
2916 islist->cur_conn = NULL;
2917 } else if (strcmp(name, "ctlislist") == 0) {
2921 * Unknown element; ignore it for forward compatibility.
2929 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
2931 struct cctl_islist_data *islist;
2933 islist = (struct cctl_islist_data *)user_data;
2935 sbuf_bcat(islist->cur_sb[islist->level], str, len);
2939 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
2941 struct ctl_iscsi req;
2942 struct cctl_islist_data islist;
2943 struct cctl_islist_conn *conn;
2948 int c, retval, verbose = 0;
2953 bzero(&islist, sizeof(islist));
2954 STAILQ_INIT(&islist.conn_list);
2956 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2970 conn_str = malloc(conn_len);
2972 bzero(&req, sizeof(req));
2973 req.type = CTL_ISCSI_LIST;
2974 req.data.list.alloc_len = conn_len;
2975 req.data.list.conn_xml = conn_str;
2977 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
2978 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
2983 if (req.status == CTL_ISCSI_ERROR) {
2984 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
2985 __func__, req.error_str);
2986 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
2987 conn_len = conn_len << 1;
2991 if (dump_xml != 0) {
2992 printf("%s", conn_str);
2996 parser = XML_ParserCreate(NULL);
2997 if (parser == NULL) {
2998 warn("%s: Unable to create XML parser", __func__);
3003 XML_SetUserData(parser, &islist);
3004 XML_SetElementHandler(parser, cctl_islist_start_element,
3005 cctl_islist_end_element);
3006 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3008 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3010 warnx("%s: Unable to parse XML: Error %d", __func__,
3011 XML_GetErrorCode(parser));
3012 XML_ParserFree(parser);
3017 XML_ParserFree(parser);
3020 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3021 printf("%-25s %d\n", "Session ID:", conn->connection_id);
3022 printf("%-25s %s\n", "Initiator name:", conn->initiator);
3023 printf("%-25s %s\n", "Initiator portal:", conn->initiator_addr);
3024 printf("%-25s %s\n", "Initiator alias:", conn->initiator_alias);
3025 printf("%-25s %s\n", "Target name:", conn->target);
3026 printf("%-25s %s\n", "Target alias:", conn->target_alias);
3027 printf("%-25s %s\n", "Header digest:", conn->header_digest);
3028 printf("%-25s %s\n", "Data digest:", conn->data_digest);
3029 printf("%-25s %s\n", "MaxRecvDataSegmentLength:", conn->max_recv_data_segment_length);
3030 printf("%-25s %s\n", "MaxSendDataSegmentLength:", conn->max_send_data_segment_length);
3031 printf("%-25s %s\n", "MaxBurstLen:", conn->max_burst_length);
3032 printf("%-25s %s\n", "FirstBurstLen:", conn->first_burst_length);
3033 printf("%-25s %s\n", "ImmediateData:", conn->immediate_data ? "Yes" : "No");
3034 printf("%-25s %s\n", "iSER (RDMA):", conn->iser ? "Yes" : "No");
3035 printf("%-25s %s\n", "Offload driver:", conn->offload);
3039 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3041 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3042 printf("%4u %-16s %-36s %-36s\n",
3043 conn->connection_id, conn->initiator_addr, conn->initiator,
3054 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3056 struct ctl_iscsi req;
3058 int all = 0, connection_id = -1, nargs = 0;
3059 char *initiator_name = NULL, *initiator_addr = NULL;
3061 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3068 connection_id = strtoul(optarg, NULL, 0);
3072 initiator_name = strdup(optarg);
3073 if (initiator_name == NULL)
3074 err(1, "%s: strdup", __func__);
3078 initiator_addr = strdup(optarg);
3079 if (initiator_addr == NULL)
3080 err(1, "%s: strdup", __func__);
3089 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3092 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3095 bzero(&req, sizeof(req));
3096 req.type = CTL_ISCSI_LOGOUT;
3097 req.data.logout.connection_id = connection_id;
3098 if (initiator_addr != NULL)
3099 strlcpy(req.data.logout.initiator_addr,
3100 initiator_addr, sizeof(req.data.logout.initiator_addr));
3101 if (initiator_name != NULL)
3102 strlcpy(req.data.logout.initiator_name,
3103 initiator_name, sizeof(req.data.logout.initiator_name));
3105 req.data.logout.all = 1;
3107 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3108 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3113 if (req.status != CTL_ISCSI_OK) {
3114 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3115 __func__, req.error_str);
3120 printf("iSCSI logout requests submitted\n");
3127 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3129 struct ctl_iscsi req;
3131 int all = 0, connection_id = -1, nargs = 0;
3132 char *initiator_name = NULL, *initiator_addr = NULL;
3134 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3141 connection_id = strtoul(optarg, NULL, 0);
3145 initiator_name = strdup(optarg);
3146 if (initiator_name == NULL)
3147 err(1, "%s: strdup", __func__);
3151 initiator_addr = strdup(optarg);
3152 if (initiator_addr == NULL)
3153 err(1, "%s: strdup", __func__);
3162 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3165 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3168 bzero(&req, sizeof(req));
3169 req.type = CTL_ISCSI_TERMINATE;
3170 req.data.terminate.connection_id = connection_id;
3171 if (initiator_addr != NULL)
3172 strlcpy(req.data.terminate.initiator_addr,
3173 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3174 if (initiator_name != NULL)
3175 strlcpy(req.data.terminate.initiator_name,
3176 initiator_name, sizeof(req.data.terminate.initiator_name));
3178 req.data.terminate.all = 1;
3180 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3181 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3186 if (req.status != CTL_ISCSI_OK) {
3187 warnx("%s: error returned from CTL iSCSI connection "
3188 "termination request:\n%s", __func__, req.error_str);
3193 printf("iSCSI connections terminated\n");
3200 * Name/value pair used for per-LUN attributes.
3202 struct cctl_lun_nv {
3205 STAILQ_ENTRY(cctl_lun_nv) links;
3209 * Backend LUN information.
3214 uint64_t size_blocks;
3216 char *serial_number;
3218 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3219 STAILQ_ENTRY(cctl_lun) links;
3222 struct cctl_devlist_data {
3224 STAILQ_HEAD(,cctl_lun) lun_list;
3225 struct cctl_lun *cur_lun;
3227 struct sbuf *cur_sb[32];
3231 cctl_start_element(void *user_data, const char *name, const char **attr)
3234 struct cctl_devlist_data *devlist;
3235 struct cctl_lun *cur_lun;
3237 devlist = (struct cctl_devlist_data *)user_data;
3238 cur_lun = devlist->cur_lun;
3240 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3241 sizeof(devlist->cur_sb[0])))
3242 errx(1, "%s: too many nesting levels, %zd max", __func__,
3243 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3245 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3246 if (devlist->cur_sb[devlist->level] == NULL)
3247 err(1, "%s: Unable to allocate sbuf", __func__);
3249 if (strcmp(name, "lun") == 0) {
3250 if (cur_lun != NULL)
3251 errx(1, "%s: improper lun element nesting", __func__);
3253 cur_lun = calloc(1, sizeof(*cur_lun));
3254 if (cur_lun == NULL)
3255 err(1, "%s: cannot allocate %zd bytes", __func__,
3258 devlist->num_luns++;
3259 devlist->cur_lun = cur_lun;
3261 STAILQ_INIT(&cur_lun->attr_list);
3262 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3264 for (i = 0; attr[i] != NULL; i += 2) {
3265 if (strcmp(attr[i], "id") == 0) {
3266 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3268 errx(1, "%s: invalid LUN attribute %s = %s",
3269 __func__, attr[i], attr[i+1]);
3276 cctl_end_element(void *user_data, const char *name)
3278 struct cctl_devlist_data *devlist;
3279 struct cctl_lun *cur_lun;
3282 devlist = (struct cctl_devlist_data *)user_data;
3283 cur_lun = devlist->cur_lun;
3285 if ((cur_lun == NULL)
3286 && (strcmp(name, "ctllunlist") != 0))
3287 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3289 if (devlist->cur_sb[devlist->level] == NULL)
3290 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3291 devlist->level, name);
3293 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3294 err(1, "%s: sbuf_finish", __func__);
3295 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3297 err(1, "%s can't allocate %zd bytes for string", __func__,
3298 sbuf_len(devlist->cur_sb[devlist->level]));
3300 if (strlen(str) == 0) {
3305 sbuf_delete(devlist->cur_sb[devlist->level]);
3306 devlist->cur_sb[devlist->level] = NULL;
3309 if (strcmp(name, "backend_type") == 0) {
3310 cur_lun->backend_type = str;
3312 } else if (strcmp(name, "size") == 0) {
3313 cur_lun->size_blocks = strtoull(str, NULL, 0);
3314 } else if (strcmp(name, "blocksize") == 0) {
3315 cur_lun->blocksize = strtoul(str, NULL, 0);
3316 } else if (strcmp(name, "serial_number") == 0) {
3317 cur_lun->serial_number = str;
3319 } else if (strcmp(name, "device_id") == 0) {
3320 cur_lun->device_id = str;
3322 } else if (strcmp(name, "lun") == 0) {
3323 devlist->cur_lun = NULL;
3324 } else if (strcmp(name, "ctllunlist") == 0) {
3327 struct cctl_lun_nv *nv;
3329 nv = calloc(1, sizeof(*nv));
3331 err(1, "%s: can't allocate %zd bytes for nv pair",
3332 __func__, sizeof(*nv));
3334 nv->name = strdup(name);
3335 if (nv->name == NULL)
3336 err(1, "%s: can't allocated %zd bytes for string",
3337 __func__, strlen(name));
3341 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3348 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3350 struct cctl_devlist_data *devlist;
3352 devlist = (struct cctl_devlist_data *)user_data;
3354 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3358 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3360 struct ctl_lun_list list;
3361 struct cctl_devlist_data devlist;
3362 struct cctl_lun *lun;
3368 char *backend = NULL;
3374 bzero(&devlist, sizeof(devlist));
3375 STAILQ_INIT(&devlist.lun_list);
3377 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3380 backend = strdup(optarg);
3394 lun_str = malloc(lun_len);
3396 bzero(&list, sizeof(list));
3397 list.alloc_len = lun_len;
3398 list.status = CTL_LUN_LIST_NONE;
3399 list.lun_xml = lun_str;
3401 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
3402 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
3407 if (list.status == CTL_LUN_LIST_ERROR) {
3408 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
3409 __func__, list.error_str);
3410 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3411 lun_len = lun_len << 1;
3415 if (dump_xml != 0) {
3416 printf("%s", lun_str);
3420 parser = XML_ParserCreate(NULL);
3421 if (parser == NULL) {
3422 warn("%s: Unable to create XML parser", __func__);
3427 XML_SetUserData(parser, &devlist);
3428 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
3429 XML_SetCharacterDataHandler(parser, cctl_char_handler);
3431 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
3433 warnx("%s: Unable to parse XML: Error %d", __func__,
3434 XML_GetErrorCode(parser));
3435 XML_ParserFree(parser);
3440 XML_ParserFree(parser);
3442 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
3443 "Serial Number", "Device ID");
3444 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
3445 struct cctl_lun_nv *nv;
3447 if ((backend != NULL)
3448 && (strcmp(lun->backend_type, backend) != 0))
3451 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
3452 (uintmax_t)lun->lun_id,
3453 lun->backend_type, (uintmax_t)lun->size_blocks,
3454 lun->blocksize, lun->serial_number, lun->device_id);
3459 STAILQ_FOREACH(nv, &lun->attr_list, links) {
3460 printf(" %s=%s\n", nv->name, nv->value);
3475 char *frontend_type;
3478 char *target, *port, *lun_map;
3479 STAILQ_HEAD(,cctl_lun_nv) init_list;
3480 STAILQ_HEAD(,cctl_lun_nv) lun_list;
3481 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3482 STAILQ_ENTRY(cctl_port) links;
3485 struct cctl_portlist_data {
3487 STAILQ_HEAD(,cctl_port) port_list;
3488 struct cctl_port *cur_port;
3491 struct sbuf *cur_sb[32];
3495 cctl_start_pelement(void *user_data, const char *name, const char **attr)
3498 struct cctl_portlist_data *portlist;
3499 struct cctl_port *cur_port;
3501 portlist = (struct cctl_portlist_data *)user_data;
3502 cur_port = portlist->cur_port;
3504 if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
3505 sizeof(portlist->cur_sb[0])))
3506 errx(1, "%s: too many nesting levels, %zd max", __func__,
3507 sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
3509 portlist->cur_sb[portlist->level] = sbuf_new_auto();
3510 if (portlist->cur_sb[portlist->level] == NULL)
3511 err(1, "%s: Unable to allocate sbuf", __func__);
3513 portlist->cur_id = 0;
3514 for (i = 0; attr[i] != NULL; i += 2) {
3515 if (strcmp(attr[i], "id") == 0) {
3516 portlist->cur_id = strtoull(attr[i+1], NULL, 0);
3521 if (strcmp(name, "targ_port") == 0) {
3522 if (cur_port != NULL)
3523 errx(1, "%s: improper port element nesting", __func__);
3525 cur_port = calloc(1, sizeof(*cur_port));
3526 if (cur_port == NULL)
3527 err(1, "%s: cannot allocate %zd bytes", __func__,
3530 portlist->num_ports++;
3531 portlist->cur_port = cur_port;
3533 STAILQ_INIT(&cur_port->init_list);
3534 STAILQ_INIT(&cur_port->lun_list);
3535 STAILQ_INIT(&cur_port->attr_list);
3536 cur_port->port_id = portlist->cur_id;
3537 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
3542 cctl_end_pelement(void *user_data, const char *name)
3544 struct cctl_portlist_data *portlist;
3545 struct cctl_port *cur_port;
3548 portlist = (struct cctl_portlist_data *)user_data;
3549 cur_port = portlist->cur_port;
3551 if ((cur_port == NULL)
3552 && (strcmp(name, "ctlportlist") != 0))
3553 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
3555 if (portlist->cur_sb[portlist->level] == NULL)
3556 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3557 portlist->level, name);
3559 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
3560 err(1, "%s: sbuf_finish", __func__);
3561 str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
3563 err(1, "%s can't allocate %zd bytes for string", __func__,
3564 sbuf_len(portlist->cur_sb[portlist->level]));
3566 if (strlen(str) == 0) {
3571 sbuf_delete(portlist->cur_sb[portlist->level]);
3572 portlist->cur_sb[portlist->level] = NULL;
3575 if (strcmp(name, "frontend_type") == 0) {
3576 cur_port->frontend_type = str;
3578 } else if (strcmp(name, "port_name") == 0) {
3579 cur_port->name = str;
3581 } else if (strcmp(name, "online") == 0) {
3582 cur_port->online = str;
3584 } else if (strcmp(name, "physical_port") == 0) {
3585 cur_port->pp = strtoull(str, NULL, 0);
3586 } else if (strcmp(name, "virtual_port") == 0) {
3587 cur_port->vp = strtoull(str, NULL, 0);
3588 } else if (strcmp(name, "target") == 0) {
3589 cur_port->target = str;
3591 } else if (strcmp(name, "port") == 0) {
3592 cur_port->port = str;
3594 } else if (strcmp(name, "lun_map") == 0) {
3595 cur_port->lun_map = str;
3597 } else if (strcmp(name, "targ_port") == 0) {
3598 portlist->cur_port = NULL;
3599 } else if (strcmp(name, "ctlportlist") == 0) {
3602 struct cctl_lun_nv *nv;
3604 nv = calloc(1, sizeof(*nv));
3606 err(1, "%s: can't allocate %zd bytes for nv pair",
3607 __func__, sizeof(*nv));
3609 if (strcmp(name, "initiator") == 0 ||
3610 strcmp(name, "lun") == 0)
3611 asprintf(&nv->name, "%ju", portlist->cur_id);
3613 nv->name = strdup(name);
3614 if (nv->name == NULL)
3615 err(1, "%s: can't allocated %zd bytes for string",
3616 __func__, strlen(name));
3620 if (strcmp(name, "initiator") == 0)
3621 STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links);
3622 else if (strcmp(name, "lun") == 0)
3623 STAILQ_INSERT_TAIL(&cur_port->lun_list, nv, links);
3625 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
3632 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
3634 struct cctl_portlist_data *portlist;
3636 portlist = (struct cctl_portlist_data *)user_data;
3638 sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
3642 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
3644 struct ctl_lun_list list;
3645 struct cctl_portlist_data portlist;
3646 struct cctl_port *port;
3648 char *port_str = NULL;
3652 char *frontend = NULL;
3653 uint64_t portarg = UINT64_MAX;
3654 int verbose = 0, init = 0, lun = 0, quiet = 0;
3659 bzero(&portlist, sizeof(portlist));
3660 STAILQ_INIT(&portlist.port_list);
3662 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3665 frontend = strdup(optarg);
3674 portarg = strtoll(optarg, NULL, 0);
3691 port_str = (char *)realloc(port_str, port_len);
3693 bzero(&list, sizeof(list));
3694 list.alloc_len = port_len;
3695 list.status = CTL_LUN_LIST_NONE;
3696 list.lun_xml = port_str;
3698 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
3699 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
3704 if (list.status == CTL_LUN_LIST_ERROR) {
3705 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
3706 __func__, list.error_str);
3707 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3708 port_len = port_len << 1;
3712 if (dump_xml != 0) {
3713 printf("%s", port_str);
3717 parser = XML_ParserCreate(NULL);
3718 if (parser == NULL) {
3719 warn("%s: Unable to create XML parser", __func__);
3724 XML_SetUserData(parser, &portlist);
3725 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
3726 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
3728 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
3730 warnx("%s: Unable to parse XML: Error %d", __func__,
3731 XML_GetErrorCode(parser));
3732 XML_ParserFree(parser);
3737 XML_ParserFree(parser);
3740 printf("Port Online Frontend Name pp vp\n");
3741 STAILQ_FOREACH(port, &portlist.port_list, links) {
3742 struct cctl_lun_nv *nv;
3744 if ((frontend != NULL)
3745 && (strcmp(port->frontend_type, frontend) != 0))
3748 if ((portarg != UINT64_MAX) && (portarg != port->port_id))
3751 printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n",
3752 (uintmax_t)port->port_id, port->online,
3753 port->frontend_type, port->name, port->pp, port->vp,
3754 port->port ? port->port : "");
3756 if (init || verbose) {
3758 printf(" Target: %s\n", port->target);
3759 STAILQ_FOREACH(nv, &port->init_list, links) {
3760 printf(" Initiator %s: %s\n",
3761 nv->name, nv->value);
3765 if (lun || verbose) {
3766 if (port->lun_map) {
3767 STAILQ_FOREACH(nv, &port->lun_list, links)
3768 printf(" LUN %s: %s\n",
3769 nv->name, nv->value);
3770 if (STAILQ_EMPTY(&port->lun_list))
3771 printf(" No LUNs mapped\n");
3773 printf(" All LUNs mapped\n");
3777 STAILQ_FOREACH(nv, &port->attr_list, links) {
3778 printf(" %s=%s\n", nv->name, nv->value);
3789 cctl_lunmap(int fd, int argc, char **argv, char *combinedopt)
3791 struct ctl_lun_map lm;
3795 lm.port = UINT32_MAX;
3796 lm.plun = UINT32_MAX;
3797 lm.lun = UINT32_MAX;
3799 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3802 lm.port = strtoll(optarg, NULL, 0);
3805 lm.plun = strtoll(optarg, NULL, 0);
3808 lm.lun = strtoll(optarg, NULL, 0);
3815 if (ioctl(fd, CTL_LUN_MAP, &lm) == -1) {
3816 warn("%s: error issuing CTL_LUN_MAP ioctl", __func__);
3826 fprintf(error ? stderr : stdout,
3828 "Primary commands:\n"
3829 " ctladm tur [dev_id][general options]\n"
3830 " ctladm inquiry [dev_id][general options]\n"
3831 " ctladm devid [dev_id][general options]\n"
3832 " ctladm reqsense [dev_id][general options]\n"
3833 " ctladm reportluns [dev_id][general options]\n"
3834 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
3835 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3836 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
3837 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3838 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
3839 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
3840 " [-d] [-S subpage] [-c cdbsize]\n"
3841 " ctladm prin [dev_id][general options] <-a action>\n"
3842 " ctladm prout [dev_id][general options] <-a action>\n"
3843 " <-r restype] [-k key] [-s sa_key]\n"
3844 " ctladm rtpg [dev_id][general options]\n"
3845 " ctladm start [dev_id][general options] [-i] [-o]\n"
3846 " ctladm stop [dev_id][general options] [-i] [-o]\n"
3847 " ctladm synccache [dev_id][general options] [-l lba]\n"
3848 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
3849 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
3850 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
3851 " [-S serial_num] [-t dev_type]\n"
3852 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
3853 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
3854 " ctladm devlist [-b backend] [-v] [-x]\n"
3856 " ctladm lunmap -p targ_port [-l pLUN] [-L cLUN]\n"
3857 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
3859 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
3860 " [-s len fmt [args]] [-c] [-d delete_id]\n"
3861 " ctladm port <-o <on|off> | [-w wwnn][-W wwpn]>\n"
3862 " [-p targ_port] [-t port_type]\n"
3863 " <-c> [-d driver] [-O name=value]\n"
3864 " <-r> <-p targ_port>\n"
3865 " ctladm portlist [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n"
3866 " ctladm islist [-v | -x]\n"
3867 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
3868 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
3870 " ctladm dumpstructs\n"
3872 "General Options:\n"
3873 "-I intiator_id : defaults to 7, used to change the initiator id\n"
3874 "-C retries : specify the number of times to retry this command\n"
3875 "-D devicename : specify the device to operate on\n"
3876 " : (default is %s)\n"
3877 "read/write options:\n"
3878 "-l lba : logical block address\n"
3879 "-d len : read/write length, in blocks\n"
3880 "-f file|- : write/read data to/from file or stdout/stdin\n"
3881 "-b blocksize : block size, in bytes\n"
3882 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
3883 "-N : do not copy data to/from userland\n"
3884 "readcapacity options:\n"
3885 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3886 "modesense options:\n"
3887 "-m page : specify the mode page to view\n"
3888 "-l : request a list of supported pages\n"
3889 "-P pc : specify the page control value: 0-3 (current,\n"
3890 " changeable, default, saved, respectively)\n"
3891 "-d : disable block descriptors for mode sense\n"
3892 "-S subpage : specify a subpage\n"
3893 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
3894 "persistent reserve in options:\n"
3895 "-a action : specify the action value: 0-2 (read key, read\n"
3896 " reservation, read capabilities, respectively)\n"
3897 "persistent reserve out options:\n"
3898 "-a action : specify the action value: 0-5 (register, reserve,\n"
3899 " release, clear, preempt, register and ignore)\n"
3900 "-k key : key value\n"
3901 "-s sa_key : service action value\n"
3902 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
3903 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
3904 "start/stop options:\n"
3905 "-i : set the immediate bit (CTL does not support this)\n"
3906 "-o : set the on/offline bit\n"
3907 "synccache options:\n"
3908 "-l lba : set the starting LBA\n"
3909 "-b blockcount : set the length to sync in blocks\n"
3910 "-r : set the relative addressing bit\n"
3911 "-i : set the immediate bit\n"
3912 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3914 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3915 "-B blocksize : LUN blocksize in bytes (some backends)\n"
3916 "-d device_id : SCSI VPD page 0x83 ID\n"
3917 "-l lun_id : requested LUN number\n"
3918 "-o name=value : backend-specific options, multiple allowed\n"
3919 "-s size_bytes : LUN size in bytes (some backends)\n"
3920 "-S serial_num : SCSI VPD page 0x80 serial number\n"
3921 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
3923 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3924 "-l lun_id : LUN number to delete\n"
3925 "-o name=value : backend-specific options, multiple allowed\n"
3926 "devlist options:\n"
3927 "-b backend : list devices from specified backend only\n"
3928 "-v : be verbose, show backend attributes\n"
3929 "-x : dump raw XML\n"
3931 "-l datamove|done : delay command at datamove or done phase\n"
3932 "-T oneshot : delay one command, then resume normal completion\n"
3933 "-T cont : delay all commands\n"
3934 "-t secs : number of seconds to delay\n"
3936 "-i error_action : action to perform\n"
3937 "-p pattern : command pattern to look for\n"
3938 "-r lba,len : LBA range for pattern\n"
3939 "-s len fmt [args] : sense data for custom sense action\n"
3940 "-c : continuous operation\n"
3941 "-d delete_id : error id to delete\n"
3943 "-c : create new ioctl or iscsi frontend port\n"
3944 "-d : specify ioctl or iscsi frontend type\n"
3945 "-l : list frontend ports\n"
3946 "-o on|off : turn frontend ports on or off\n"
3947 "-O pp|vp : create new frontend port using pp and/or vp\n"
3948 "-w wwnn : set WWNN for one frontend\n"
3949 "-W wwpn : set WWPN for one frontend\n"
3950 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
3951 "-p targ_port : specify target port number\n"
3952 "-r : remove frontend port\n"
3953 "-q : omit header in list output\n"
3954 "-x : output port list in XML format\n"
3955 "portlist options:\n"
3956 "-f frontend : specify frontend type\n"
3957 "-i : report target and initiators addresses\n"
3958 "-l : report LUN mapping\n"
3959 "-p targ_port : specify target port number\n"
3960 "-q : omit header in list output\n"
3961 "-v : verbose output (report all port options)\n"
3962 "-x : output port list in XML format\n"
3964 "-p targ_port : specify target port number\n"
3965 "-l pLUN : specify port-visible LUN\n"
3966 "-L cLUN : specify CTL LUN\n",
3971 main(int argc, char **argv)
3974 ctladm_cmdfunction command;
3975 ctladm_cmdargs cmdargs;
3976 ctladm_optret optreturn;
3978 const char *mainopt = "C:D:I:";
3979 const char *subopt = NULL;
3980 char combinedopt[256];
3989 cmdargs = CTLADM_ARG_NONE;
3990 command = CTLADM_CMD_HELP;
4004 * Get the base option.
4006 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4008 if (optreturn == CC_OR_AMBIGUOUS) {
4009 warnx("ambiguous option %s", argv[1]);
4012 } else if (optreturn == CC_OR_NOT_FOUND) {
4013 warnx("option %s not found", argv[1]);
4018 if (cmdargs & CTLADM_ARG_NEED_TL) {
4019 if ((argc < 3) || (!isdigit(argv[2][0]))) {
4020 warnx("option %s requires a lun argument",
4025 lun = strtol(argv[2], NULL, 0);
4027 cmdargs |= CTLADM_ARG_TARG_LUN;
4032 * Ahh, getopt(3) is a pain.
4034 * This is a gross hack. There really aren't many other good
4035 * options (excuse the pun) for parsing options in a situation like
4036 * this. getopt is kinda braindead, so you end up having to run
4037 * through the options twice, and give each invocation of getopt
4038 * the option string for the other invocation.
4040 * You would think that you could just have two groups of options.
4041 * The first group would get parsed by the first invocation of
4042 * getopt, and the second group would get parsed by the second
4043 * invocation of getopt. It doesn't quite work out that way. When
4044 * the first invocation of getopt finishes, it leaves optind pointing
4045 * to the argument _after_ the first argument in the second group.
4046 * So when the second invocation of getopt comes around, it doesn't
4047 * recognize the first argument it gets and then bails out.
4049 * A nice alternative would be to have a flag for getopt that says
4050 * "just keep parsing arguments even when you encounter an unknown
4051 * argument", but there isn't one. So there's no real clean way to
4052 * easily parse two sets of arguments without having one invocation
4053 * of getopt know about the other.
4055 * Without this hack, the first invocation of getopt would work as
4056 * long as the generic arguments are first, but the second invocation
4057 * (in the subfunction) would fail in one of two ways. In the case
4058 * where you don't set optreset, it would fail because optind may be
4059 * pointing to the argument after the one it should be pointing at.
4060 * In the case where you do set optreset, and reset optind, it would
4061 * fail because getopt would run into the first set of options, which
4062 * it doesn't understand.
4064 * All of this would "sort of" work if you could somehow figure out
4065 * whether optind had been incremented one option too far. The
4066 * mechanics of that, however, are more daunting than just giving
4067 * both invocations all of the expect options for either invocation.
4069 * Needless to say, I wouldn't mind if someone invented a better
4070 * (non-GPL!) command line parsing interface than getopt. I
4071 * wouldn't mind if someone added more knobs to getopt to make it
4072 * work better. Who knows, I may talk myself into doing it someday,
4073 * if the standards weenies let me. As it is, it just leads to
4074 * hackery like this and causes people to avoid it in some cases.
4076 * KDM, September 8th, 1998
4079 sprintf(combinedopt, "%s%s", mainopt, subopt);
4081 sprintf(combinedopt, "%s", mainopt);
4084 * Start getopt processing at argv[2/3], since we've already
4085 * accepted argv[1..2] as the command name, and as a possible
4091 * Now we run through the argument list looking for generic
4092 * options, and ignoring options that possibly belong to
4095 while ((c = getopt(argc, argv, combinedopt))!= -1){
4098 cmdargs |= CTLADM_ARG_RETRIES;
4099 retries = strtol(optarg, NULL, 0);
4102 device = strdup(optarg);
4103 cmdargs |= CTLADM_ARG_DEVICE;
4106 cmdargs |= CTLADM_ARG_INITIATOR;
4107 initid = strtol(optarg, NULL, 0);
4114 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4121 * Default to opening the CTL device for now.
4123 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4124 && (command != CTLADM_CMD_HELP)) {
4125 device = strdup(CTL_DEFAULT_DEV);
4126 cmdargs |= CTLADM_ARG_DEVICE;
4129 if ((cmdargs & CTLADM_ARG_DEVICE)
4130 && (command != CTLADM_CMD_HELP)) {
4131 fd = open(device, O_RDWR);
4132 if (fd == -1 && errno == ENOENT) {
4133 saved_errno = errno;
4134 retval = kldload("ctl");
4136 fd = open(device, O_RDWR);
4138 errno = saved_errno;
4141 fprintf(stderr, "%s: error opening %s: %s\n",
4142 argv[0], device, strerror(errno));
4148 if (modfind("cfiscsi") == -1 &&
4149 kldload("cfiscsi") == -1)
4150 warn("couldn't load cfiscsi");
4153 } else if ((command != CTLADM_CMD_HELP)
4154 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4155 fprintf(stderr, "%s: you must specify a device with the "
4156 "--device argument for this command\n", argv[0]);
4157 command = CTLADM_CMD_HELP;
4162 case CTLADM_CMD_TUR:
4163 retval = cctl_tur(fd, lun, initid, retries);
4165 case CTLADM_CMD_INQUIRY:
4166 retval = cctl_inquiry(fd, lun, initid, retries);
4168 case CTLADM_CMD_REQ_SENSE:
4169 retval = cctl_req_sense(fd, lun, initid, retries);
4171 case CTLADM_CMD_REPORT_LUNS:
4172 retval = cctl_report_luns(fd, lun, initid, retries);
4174 case CTLADM_CMD_CREATE:
4175 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4178 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4180 case CTLADM_CMD_DEVLIST:
4181 retval = cctl_devlist(fd, argc, argv, combinedopt);
4183 case CTLADM_CMD_READ:
4184 case CTLADM_CMD_WRITE:
4185 retval = cctl_read_write(fd, lun, initid, retries,
4186 argc, argv, combinedopt, command);
4188 case CTLADM_CMD_PORT:
4189 retval = cctl_port(fd, argc, argv, combinedopt);
4191 case CTLADM_CMD_PORTLIST:
4192 retval = cctl_portlist(fd, argc, argv, combinedopt);
4194 case CTLADM_CMD_LUNMAP:
4195 retval = cctl_lunmap(fd, argc, argv, combinedopt);
4197 case CTLADM_CMD_READCAPACITY:
4198 retval = cctl_read_capacity(fd, lun, initid, retries,
4199 argc, argv, combinedopt);
4201 case CTLADM_CMD_MODESENSE:
4202 retval = cctl_mode_sense(fd, lun, initid, retries,
4203 argc, argv, combinedopt);
4205 case CTLADM_CMD_START:
4206 case CTLADM_CMD_STOP:
4207 retval = cctl_start_stop(fd, lun, initid, retries,
4208 (command == CTLADM_CMD_START) ? 1 : 0,
4209 argc, argv, combinedopt);
4211 case CTLADM_CMD_SYNC_CACHE:
4212 retval = cctl_sync_cache(fd, lun, initid, retries,
4213 argc, argv, combinedopt);
4215 case CTLADM_CMD_LUNLIST:
4216 retval = cctl_lunlist(fd);
4218 case CTLADM_CMD_DELAY:
4219 retval = cctl_delay(fd, lun, argc, argv, combinedopt);
4221 case CTLADM_CMD_ERR_INJECT:
4222 retval = cctl_error_inject(fd, lun, argc, argv,
4225 case CTLADM_CMD_DUMPOOA:
4226 retval = cctl_dump_ooa(fd, argc, argv);
4228 case CTLADM_CMD_DUMPSTRUCTS:
4229 retval = cctl_dump_structs(fd, cmdargs);
4231 case CTLADM_CMD_PRES_IN:
4232 retval = cctl_persistent_reserve_in(fd, lun, initid,
4233 argc, argv, combinedopt,
4236 case CTLADM_CMD_PRES_OUT:
4237 retval = cctl_persistent_reserve_out(fd, lun, initid,
4238 argc, argv, combinedopt,
4241 case CTLADM_CMD_INQ_VPD_DEVID:
4242 retval = cctl_inquiry_vpd_devid(fd, lun, initid);
4244 case CTLADM_CMD_RTPG:
4245 retval = cctl_report_target_port_group(fd, lun, initid);
4247 case CTLADM_CMD_MODIFY:
4248 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4250 case CTLADM_CMD_ISLIST:
4251 retval = cctl_islist(fd, argc, argv, combinedopt);
4253 case CTLADM_CMD_ISLOGOUT:
4254 retval = cctl_islogout(fd, argc, argv, combinedopt);
4256 case CTLADM_CMD_ISTERMINATE:
4257 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4259 case CTLADM_CMD_HELP: