2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3 * Copyright (c) 1997-2007 Kenneth D. Merry
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
35 * $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.c#4 $
38 * CAM Target Layer exercise program.
40 * Author: Ken Merry <ken@FreeBSD.org>
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/callout.h>
48 #include <sys/ioctl.h>
49 #include <sys/linker.h>
50 #include <sys/module.h>
51 #include <sys/queue.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_message.h>
67 #include <cam/ctl/ctl.h>
68 #include <cam/ctl/ctl_io.h>
69 #include <cam/ctl/ctl_backend.h>
70 #include <cam/ctl/ctl_ioctl.h>
71 #include <cam/ctl/ctl_util.h>
72 #include <cam/ctl/ctl_scsi_all.h>
80 #define min(x,y) (x < y) ? x : y
87 CTLADM_CMD_REPORT_LUNS,
97 CTLADM_CMD_READCAPACITY,
100 CTLADM_CMD_DUMPSTRUCTS,
103 CTLADM_CMD_SYNC_CACHE,
106 CTLADM_CMD_ERR_INJECT,
109 CTLADM_CMD_INQ_VPD_DEVID,
114 CTLADM_CMD_ISTERMINATE,
116 } ctladm_cmdfunction;
119 CTLADM_ARG_NONE = 0x0000000,
120 CTLADM_ARG_AUTOSENSE = 0x0000001,
121 CTLADM_ARG_DEVICE = 0x0000002,
122 CTLADM_ARG_ARRAYSIZE = 0x0000004,
123 CTLADM_ARG_BACKEND = 0x0000008,
124 CTLADM_ARG_CDBSIZE = 0x0000010,
125 CTLADM_ARG_DATALEN = 0x0000020,
126 CTLADM_ARG_FILENAME = 0x0000040,
127 CTLADM_ARG_LBA = 0x0000080,
128 CTLADM_ARG_PC = 0x0000100,
129 CTLADM_ARG_PAGE_CODE = 0x0000200,
130 CTLADM_ARG_PAGE_LIST = 0x0000400,
131 CTLADM_ARG_SUBPAGE = 0x0000800,
132 CTLADM_ARG_PAGELIST = 0x0001000,
133 CTLADM_ARG_DBD = 0x0002000,
134 CTLADM_ARG_TARG_LUN = 0x0004000,
135 CTLADM_ARG_BLOCKSIZE = 0x0008000,
136 CTLADM_ARG_IMMED = 0x0010000,
137 CTLADM_ARG_RELADR = 0x0020000,
138 CTLADM_ARG_RETRIES = 0x0040000,
139 CTLADM_ARG_ONOFFLINE = 0x0080000,
140 CTLADM_ARG_ONESHOT = 0x0100000,
141 CTLADM_ARG_TIMEOUT = 0x0200000,
142 CTLADM_ARG_INITIATOR = 0x0400000,
143 CTLADM_ARG_NOCOPY = 0x0800000,
144 CTLADM_ARG_NEED_TL = 0x1000000
150 ctladm_cmdargs argnum;
160 static const char rw_opts[] = "Nb:c:d:f:l:";
161 static const char startstop_opts[] = "i";
163 static struct ctladm_opts option_table[] = {
164 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
165 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
166 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
167 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
168 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
169 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
170 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
171 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
172 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
173 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
174 {"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"},
175 {"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"},
176 {"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"},
177 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
178 {"lunmap", CTLADM_CMD_LUNMAP, CTLADM_ARG_NONE, "p:l:L:"},
179 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
180 {"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:o:s:"},
181 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
182 {"portlist", CTLADM_CMD_PORTLIST, CTLADM_ARG_NONE, "f:ilp:qvx"},
183 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
184 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
185 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
186 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
187 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
188 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
189 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
190 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
191 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
192 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
193 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
194 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
195 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
196 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
197 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
202 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
203 ctladm_cmdargs *argnum, const char **subopt);
204 static int cctl_dump_ooa(int fd, int argc, char **argv);
205 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
206 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
207 static int cctl_delay(int fd, int lun, int argc, char **argv,
209 static int cctl_lunlist(int fd);
210 static int cctl_sync_cache(int fd, int lun, int iid, int retries,
211 int argc, char **argv, char *combinedopt);
212 static int cctl_start_stop(int fd, int lun, int iid, int retries,
213 int start, int argc, char **argv, char *combinedopt);
214 static int cctl_mode_sense(int fd, int lun, int iid, int retries,
215 int argc, char **argv, char *combinedopt);
216 static int cctl_read_capacity(int fd, int lun, int iid,
217 int retries, int argc, char **argv,
219 static int cctl_read_write(int fd, int lun, int iid, int retries,
220 int argc, char **argv, char *combinedopt,
221 ctladm_cmdfunction command);
222 static int cctl_get_luns(int fd, int lun, int iid, int retries,
223 struct scsi_report_luns_data **lun_data,
225 static int cctl_report_luns(int fd, int lun, int iid, int retries);
226 static int cctl_tur(int fd, int lun, int iid, int retries);
227 static int cctl_get_inquiry(int fd, int lun, int iid, int retries,
228 char *path_str, int path_len,
229 struct scsi_inquiry_data *inq_data);
230 static int cctl_inquiry(int fd, int lun, int iid, int retries);
231 static int cctl_req_sense(int fd, int lun, int iid, int retries);
232 static int cctl_persistent_reserve_in(int fd, int lun,
233 int initiator, int argc, char **argv,
234 char *combinedopt, int retry_count);
235 static int cctl_persistent_reserve_out(int fd, int lun,
236 int initiator, int argc, char **argv,
237 char *combinedopt, int retry_count);
238 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
239 static int cctl_inquiry_vpd_devid(int fd, int lun, int initiator);
240 static int cctl_report_target_port_group(int fd, int lun, int initiator);
241 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
242 static int cctl_portlist(int fd, int argc, char **argv, char *combinedopt);
245 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
246 ctladm_cmdargs *argnum, const char **subopt)
248 struct ctladm_opts *opts;
251 for (opts = table; (opts != NULL) && (opts->optname != NULL);
253 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
254 *cmdnum = opts->cmdnum;
255 *argnum = opts->argnum;
256 *subopt = opts->subopt;
258 if (strcmp(opts->optname, arg) == 0)
259 return (CC_OR_FOUND);
261 if (++num_matches > 1)
262 return(CC_OR_AMBIGUOUS);
269 return(CC_OR_NOT_FOUND);
273 cctl_dump_ooa(int fd, int argc, char **argv)
276 long double cmd_latency;
277 int num_entries, len, lun = -1, retval = 0;
282 if ((argc > 2) && (isdigit(argv[2][0])))
283 lun = strtol(argv[2], NULL, 0);
286 len = num_entries * sizeof(struct ctl_ooa_entry);
287 bzero(&ooa, sizeof(ooa));
288 ooa.entries = malloc(len);
289 if (ooa.entries == NULL) {
290 warn("%s: error mallocing %d bytes", __func__, len);
296 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
298 ooa.alloc_num = num_entries;
299 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
300 warn("%s: CTL_GET_OOA ioctl failed", __func__);
305 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
306 num_entries = num_entries * 2;
312 if (ooa.status != CTL_OOA_OK) {
313 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
319 fprintf(stdout, "Dumping OOA queues\n");
320 for (i = 0; i < ooa.fill_num; i++) {
321 struct ctl_ooa_entry *entry;
322 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
323 struct bintime delta_bt;
326 entry = &ooa.entries[i];
328 delta_bt = ooa.cur_bt;
329 bintime_sub(&delta_bt, &entry->start_bt);
330 bintime2timespec(&delta_bt, &ts);
331 cmd_latency = ts.tv_sec * 1000;
333 cmd_latency += ts.tv_nsec / 1000000;
335 fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
337 (intmax_t)entry->lun_num, entry->tag_num,
338 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
340 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
341 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
343 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
345 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
346 scsi_op_desc(entry->cdb[0], NULL),
347 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
350 fprintf(stdout, "OOA queues dump done\n");
358 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
360 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
375 static struct ctladm_opts cctl_fe_table[] = {
376 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
377 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
378 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
379 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
380 {"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
381 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
386 cctl_port(int fd, int argc, char **argv, char *combinedopt)
389 int32_t targ_port = -1;
391 int wwnn_set = 0, wwpn_set = 0;
392 uint64_t wwnn = 0, wwpn = 0;
393 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
394 struct ctl_port_entry entry;
395 ctl_port_type port_type = CTL_PORT_NONE;
396 int quiet = 0, xml = 0;
398 while ((c = getopt(argc, argv, combinedopt)) != -1) {
401 if (port_mode != CCTL_PORT_MODE_NONE)
404 port_mode = CCTL_PORT_MODE_LIST;
407 if (port_mode != CCTL_PORT_MODE_NONE)
410 if (strcasecmp(optarg, "on") == 0)
411 port_mode = CCTL_PORT_MODE_ON;
412 else if (strcasecmp(optarg, "off") == 0)
413 port_mode = CCTL_PORT_MODE_OFF;
415 warnx("Invalid -o argument %s, \"on\" or "
416 "\"off\" are the only valid args",
423 targ_port = strtol(optarg, NULL, 0);
429 ctladm_optret optret;
430 ctladm_cmdargs argnum;
432 ctl_port_type tmp_port_type;
434 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
436 if (optret == CC_OR_AMBIGUOUS) {
437 warnx("%s: ambiguous frontend type %s",
441 } else if (optret == CC_OR_NOT_FOUND) {
442 warnx("%s: invalid frontend type %s",
448 port_type |= tmp_port_type;
452 if ((port_mode != CCTL_PORT_MODE_NONE)
453 && (port_mode != CCTL_PORT_MODE_SET))
456 port_mode = CCTL_PORT_MODE_SET;
458 wwnn = strtoull(optarg, NULL, 0);
462 if ((port_mode != CCTL_PORT_MODE_NONE)
463 && (port_mode != CCTL_PORT_MODE_SET))
466 port_mode = CCTL_PORT_MODE_SET;
468 wwpn = strtoull(optarg, NULL, 0);
478 * The user can specify either one or more frontend types (-t), or
479 * a specific frontend, but not both.
481 * If the user didn't specify a frontend type or number, set it to
482 * all. This is primarily needed for the enable/disable ioctls.
483 * This will be a no-op for the listing code. For the set ioctl,
484 * we'll throw an error, since that only works on one port at a time.
486 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
487 warnx("%s: can only specify one of -t or -n", __func__);
490 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
491 port_type = CTL_PORT_ALL;
493 bzero(&entry, sizeof(entry));
496 * These are needed for all but list/dump mode.
498 entry.port_type = port_type;
499 entry.targ_port = targ_port;
502 case CCTL_PORT_MODE_LIST: {
512 argvx[argcx++] = argx;
514 argvx[argcx++] = argq;
515 cctl_portlist(fd, argcx, argvx, opts);
518 case CCTL_PORT_MODE_SET:
519 if (targ_port == -1) {
520 warnx("%s: -w and -W require -n", __func__);
526 entry.flags |= CTL_PORT_WWNN_VALID;
530 entry.flags |= CTL_PORT_WWPN_VALID;
534 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
535 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
540 case CCTL_PORT_MODE_ON:
541 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
542 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
546 fprintf(stdout, "Front End Ports enabled\n");
548 case CCTL_PORT_MODE_OFF:
549 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
550 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
554 fprintf(stdout, "Front End Ports disabled\n");
557 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
568 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
573 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
576 if (ioctl(fd, CTL_IO, io) == -1) {
577 warn("%s: error sending CTL_IO ioctl", func);
580 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
587 cctl_delay(int fd, int lun, int argc, char **argv,
590 struct ctl_io_delay_info delay_info;
591 char *delayloc = NULL;
592 char *delaytype = NULL;
599 memset(&delay_info, 0, sizeof(delay_info));
601 while ((c = getopt(argc, argv, combinedopt)) != -1) {
604 delaytype = strdup(optarg);
607 delayloc = strdup(optarg);
610 delaytime = strtoul(optarg, NULL, 0);
615 if (delaytime == -1) {
616 warnx("%s: you must specify the delaytime with -t", __func__);
621 if (strcasecmp(delayloc, "datamove") == 0)
622 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
623 else if (strcasecmp(delayloc, "done") == 0)
624 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
626 warnx("%s: invalid delay location %s", __func__, delayloc);
631 if ((delaytype == NULL)
632 || (strcmp(delaytype, "oneshot") == 0))
633 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
634 else if (strcmp(delaytype, "cont") == 0)
635 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
637 warnx("%s: invalid delay type %s", __func__, delaytype);
642 delay_info.lun_id = lun;
643 delay_info.delay_secs = delaytime;
645 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
646 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
650 switch (delay_info.status) {
651 case CTL_DELAY_STATUS_NONE:
652 warnx("%s: no delay status??", __func__);
655 case CTL_DELAY_STATUS_OK:
657 case CTL_DELAY_STATUS_INVALID_LUN:
658 warnx("%s: invalid lun %d", __func__, lun);
661 case CTL_DELAY_STATUS_INVALID_TYPE:
662 warnx("%s: invalid delay type %d", __func__,
663 delay_info.delay_type);
666 case CTL_DELAY_STATUS_INVALID_LOC:
667 warnx("%s: delay location %s not implemented?", __func__,
671 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
672 warnx("%s: delay not implemented in the kernel", __func__);
673 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
677 warnx("%s: unknown delay return status %d", __func__,
689 static struct ctladm_opts cctl_err_types[] = {
690 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
691 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
692 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
693 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
698 static struct ctladm_opts cctl_err_patterns[] = {
699 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
700 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
701 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
702 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
703 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
704 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
705 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
707 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
713 cctl_error_inject(int fd, uint32_t lun, int argc, char **argv,
717 struct ctl_error_desc err_desc;
720 uint64_t delete_id = 0;
721 int delete_id_set = 0;
727 bzero(&err_desc, sizeof(err_desc));
728 err_desc.lun_id = lun;
730 while ((c = getopt(argc, argv, combinedopt)) != -1) {
736 delete_id = strtoull(optarg, NULL, 0);
741 ctladm_optret optret;
742 ctladm_cmdargs argnum;
746 ctl_lun_error err_type;
748 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
749 warnx("%s: can't specify multiple -i "
750 "arguments", __func__);
754 optret = getoption(cctl_err_types, optarg,
755 &err_type, &argnum, &subopt);
756 err_desc.lun_error = err_type;
758 ctl_lun_error_pattern pattern;
760 optret = getoption(cctl_err_patterns, optarg,
761 &pattern, &argnum, &subopt);
762 err_desc.error_pattern |= pattern;
765 if (optret == CC_OR_AMBIGUOUS) {
766 warnx("%s: ambiguous argument %s", __func__,
770 } else if (optret == CC_OR_NOT_FOUND) {
771 warnx("%s: argument %s not found", __func__,
779 char *tmpstr, *tmpstr2;
781 tmpstr = strdup(optarg);
782 if (tmpstr == NULL) {
783 warn("%s: error duplicating string %s",
789 tmpstr2 = strsep(&tmpstr, ",");
790 if (tmpstr2 == NULL) {
791 warnx("%s: invalid -r argument %s", __func__,
797 lba = strtoull(tmpstr2, NULL, 0);
798 tmpstr2 = strsep(&tmpstr, ",");
799 if (tmpstr2 == NULL) {
800 warnx("%s: no len argument for -r lba,len, got"
801 " %s", __func__, optarg);
806 len = strtoul(tmpstr2, NULL, 0);
811 struct get_hook hook;
814 sense_len = strtol(optarg, NULL, 0);
815 if (sense_len <= 0) {
816 warnx("invalid number of sense bytes %d",
822 sense_len = MIN(sense_len, SSD_FULL_SIZE);
824 hook.argc = argc - optind;
825 hook.argv = argv + optind;
828 sensestr = cget(&hook, NULL);
829 if ((sensestr != NULL)
830 && (sensestr[0] == '-')) {
834 (uint8_t *)&err_desc.custom_sense,
835 sense_len, sensestr, iget, &hook);
845 if (delete_id_set != 0) {
846 err_desc.serial = delete_id;
847 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
848 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
855 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
856 warnx("%s: error injection command (-i) needed",
860 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
861 && (sense_len == 0)) {
862 warnx("%s: custom error requires -s", __func__);
868 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
871 * If fd_sense is set, we need to read the sense data the user
872 * wants returned from stdin.
876 int amt_to_read = sense_len;
877 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
879 for (amt_read = 0; amt_to_read > 0;
880 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
881 if (amt_read == -1) {
882 warn("error reading sense data from stdin");
886 amt_to_read -= amt_read;
891 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
892 warnx("%s: command pattern (-p) needed", __func__);
898 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
900 * We could check here to see whether it's a read/write
901 * command, but that will be pointless once we allow
902 * custom patterns. At that point, the user could specify
903 * a READ(6) CDB type, and we wouldn't have an easy way here
904 * to verify whether range checking is possible there. The
905 * user will just figure it out when his error never gets
909 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
910 warnx("%s: need read and/or write pattern if range "
911 "is specified", __func__);
916 err_desc.lba_range.lba = lba;
917 err_desc.lba_range.len = len;
920 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
921 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
924 printf("Error injection succeeded, serial number is %ju\n",
925 (uintmax_t)err_desc.serial);
935 struct scsi_report_luns_data *lun_data;
936 struct scsi_inquiry_data *inq_data;
946 * XXX KDM assuming LUN 0 is fine, but we may need to change this
947 * if we ever acquire the ability to have multiple targets.
949 if ((retval = cctl_get_luns(fd, /*lun*/ 0, initid,
950 /*retries*/ 2, &lun_data, &num_luns)) != 0)
953 inq_data = malloc(sizeof(*inq_data));
954 if (inq_data == NULL) {
955 warn("%s: couldn't allocate memory for inquiry data\n",
960 for (i = 0; i < num_luns; i++) {
964 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
965 case RPL_LUNDATA_ATYP_PERIPH:
966 lun_val = lun_data->luns[i].lundata[1];
968 case RPL_LUNDATA_ATYP_FLAT:
969 lun_val = (lun_data->luns[i].lundata[0] &
970 RPL_LUNDATA_FLAT_LUN_MASK) |
971 (lun_data->luns[i].lundata[1] <<
972 RPL_LUNDATA_FLAT_LUN_BITS);
974 case RPL_LUNDATA_ATYP_LUN:
975 case RPL_LUNDATA_ATYP_EXTLUN:
977 fprintf(stdout, "Unsupported LUN format %d\n",
978 lun_data->luns[i].lundata[0] &
979 RPL_LUNDATA_ATYP_MASK);
986 if ((retval = cctl_get_inquiry(fd, lun_val, initid,
987 /*retries*/ 2, scsi_path,
992 printf("%s", scsi_path);
993 scsi_print_inquiry(inq_data);
997 if (lun_data != NULL)
1000 if (inq_data != NULL)
1007 cctl_sync_cache(int fd, int lun, int iid, int retries,
1008 int argc, char **argv, char *combinedopt)
1013 uint64_t our_lba = 0;
1014 uint32_t our_block_count = 0;
1015 int reladr = 0, immed = 0;
1020 io = ctl_scsi_alloc_io(iid);
1022 warnx("%s: can't allocate memory", __func__);
1026 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1029 our_block_count = strtoul(optarg, NULL, 0);
1032 cdb_size = strtol(optarg, NULL, 0);
1038 our_lba = strtoull(optarg, NULL, 0);
1048 if (cdb_size != -1) {
1054 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1055 "and 16", __func__, cdb_size);
1058 break; /* NOTREACHED */
1063 ctl_scsi_sync_cache(/*io*/ io,
1066 /*minimum_cdb_size*/ cdb_size,
1067 /*starting_lba*/ our_lba,
1068 /*block_count*/ our_block_count,
1069 /*tag_type*/ CTL_TAG_SIMPLE,
1072 io->io_hdr.nexus.targ_lun = lun;
1073 io->io_hdr.nexus.initid = iid;
1075 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1080 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1081 fprintf(stdout, "Cache synchronized successfully\n");
1083 ctl_io_error_print(io, NULL, stderr);
1085 ctl_scsi_free_io(io);
1091 cctl_start_stop(int fd, int lun, int iid, int retries, int start,
1092 int argc, char **argv, char *combinedopt)
1101 io = ctl_scsi_alloc_io(iid);
1103 warnx("%s: can't allocate memory", __func__);
1107 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1117 * Use an ordered tag for the stop command, to guarantee that any
1118 * pending I/O will finish before the stop command executes. This
1119 * would normally be the case anyway, since CTL will basically
1120 * treat the start/stop command as an ordered command with respect
1121 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1123 ctl_scsi_start_stop(/*io*/ io,
1126 /*immediate*/ immed,
1127 /*power_conditions*/ SSS_PC_START_VALID,
1128 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1132 io->io_hdr.nexus.targ_lun = lun;
1133 io->io_hdr.nexus.initid = iid;
1135 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1140 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1141 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1142 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1143 (start) ? "started" : "stopped");
1145 ctl_io_error_print(io, NULL, stderr);
1148 ctl_scsi_free_io(io);
1154 cctl_mode_sense(int fd, int lun, int iid, int retries,
1155 int argc, char **argv, char *combinedopt)
1160 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1169 io = ctl_scsi_alloc_io(iid);
1171 warn("%s: can't allocate memory", __func__);
1175 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1178 pc = strtoul(optarg, NULL, 0);
1181 subpage = strtoul(optarg, NULL, 0);
1190 page_code = strtoul(optarg, NULL, 0);
1193 cdbsize = strtol(optarg, NULL, 0);
1200 if (((list == 0) && (page_code == -1))
1201 || ((list != 0) && (page_code != -1))) {
1202 warnx("%s: you must specify either a page code (-m) or -l",
1208 if ((page_code != -1)
1209 && ((page_code > SMS_ALL_PAGES_PAGE)
1210 || (page_code < 0))) {
1211 warnx("%s: page code %d is out of range", __func__,
1218 page_code = SMS_ALL_PAGES_PAGE;
1220 warnx("%s: arg -P makes no sense with -l",
1225 if (subpage != -1) {
1226 warnx("%s: arg -S makes no sense with -l", __func__);
1233 pc = SMS_PAGE_CTRL_CURRENT;
1237 warnx("%s: page control value %d is out of range: 0-3",
1247 || (subpage < 0))) {
1248 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1259 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1260 "and 10", __func__, cdbsize);
1276 dataptr = (uint8_t *)malloc(datalen);
1277 if (dataptr == NULL) {
1278 warn("%s: can't allocate %d bytes", __func__, datalen);
1283 memset(dataptr, 0, datalen);
1285 ctl_scsi_mode_sense(io,
1286 /*data_ptr*/ dataptr,
1287 /*data_len*/ datalen,
1290 /*page_code*/ page_code,
1292 /*subpage*/ subpage,
1293 /*minimum_cdb_size*/ cdbsize,
1294 /*tag_type*/ CTL_TAG_SIMPLE,
1297 io->io_hdr.nexus.targ_lun = lun;
1298 io->io_hdr.nexus.initid = iid;
1300 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1305 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1306 int pages_len, used_len;
1307 uint32_t returned_len;
1310 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1311 struct scsi_mode_hdr_6 *hdr6;
1314 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1316 returned_len = hdr6->datalen + 1;
1317 bdlen = hdr6->block_descr_len;
1319 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1321 struct scsi_mode_hdr_10 *hdr10;
1324 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
1326 returned_len = scsi_2btoul(hdr10->datalen) + 2;
1327 bdlen = scsi_2btoul(hdr10->block_descr_len);
1329 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
1331 /* just in case they can give us more than we allocated for */
1332 returned_len = min(returned_len, datalen);
1333 pages_len = returned_len - (ndataptr - dataptr);
1335 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
1336 returned_len, pages_len);
1339 fprintf(stdout, "Supported mode pages:\n");
1340 for (used_len = 0; used_len < pages_len;) {
1341 struct scsi_mode_page_header *header;
1343 header = (struct scsi_mode_page_header *)
1344 &ndataptr[used_len];
1345 fprintf(stdout, "%d\n", header->page_code);
1346 used_len += header->page_length + 2;
1349 for (used_len = 0; used_len < pages_len; used_len++) {
1350 fprintf(stdout, "0x%x ", ndataptr[used_len]);
1351 if (((used_len+1) % 16) == 0)
1352 fprintf(stdout, "\n");
1354 fprintf(stdout, "\n");
1357 ctl_io_error_print(io, NULL, stderr);
1360 ctl_scsi_free_io(io);
1362 if (dataptr != NULL)
1369 cctl_read_capacity(int fd, int lun, int iid, int retries,
1370 int argc, char **argv, char *combinedopt)
1373 struct scsi_read_capacity_data *data;
1374 struct scsi_read_capacity_data_long *longdata;
1375 int cdbsize = -1, retval;
1383 io = ctl_scsi_alloc_io(iid);
1385 warn("%s: can't allocate memory\n", __func__);
1389 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1392 cdbsize = strtol(optarg, NULL, 0);
1398 if (cdbsize != -1) {
1404 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1405 "and 16", __func__, cdbsize);
1408 break; /* NOTREACHED */
1413 dataptr = (uint8_t *)malloc(sizeof(*longdata));
1414 if (dataptr == NULL) {
1415 warn("%s: can't allocate %zd bytes\n", __func__,
1420 memset(dataptr, 0, sizeof(*longdata));
1426 ctl_scsi_read_capacity(io,
1427 /*data_ptr*/ dataptr,
1428 /*data_len*/ sizeof(*longdata),
1432 /*tag_type*/ CTL_TAG_SIMPLE,
1436 ctl_scsi_read_capacity_16(io,
1437 /*data_ptr*/ dataptr,
1438 /*data_len*/ sizeof(*longdata),
1442 /*tag_type*/ CTL_TAG_SIMPLE,
1447 io->io_hdr.nexus.initid = iid;
1448 io->io_hdr.nexus.targ_lun = lun;
1450 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1455 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1459 if (cdbsize == 10) {
1461 data = (struct scsi_read_capacity_data *)dataptr;
1463 maxlba = scsi_4btoul(data->addr);
1464 blocksize = scsi_4btoul(data->length);
1466 if (maxlba == 0xffffffff) {
1471 longdata=(struct scsi_read_capacity_data_long *)dataptr;
1473 maxlba = scsi_8btou64(longdata->addr);
1474 blocksize = scsi_4btoul(longdata->length);
1477 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
1478 (uintmax_t)maxlba, blocksize);
1480 ctl_io_error_print(io, NULL, stderr);
1483 ctl_scsi_free_io(io);
1485 if (dataptr != NULL)
1492 cctl_read_write(int fd, int lun, int iid, int retries,
1493 int argc, char **argv, char *combinedopt,
1494 ctladm_cmdfunction command)
1497 int file_fd, do_stdio;
1498 int cdbsize = -1, databytes;
1500 char *filename = NULL;
1501 int datalen = -1, blocksize = -1;
1512 io = ctl_scsi_alloc_io(iid);
1514 warn("%s: can't allocate memory\n", __func__);
1518 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1521 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
1524 blocksize = strtoul(optarg, NULL, 0);
1527 cdbsize = strtoul(optarg, NULL, 0);
1530 datalen = strtoul(optarg, NULL, 0);
1533 filename = strdup(optarg);
1536 lba = strtoull(optarg, NULL, 0);
1543 if (filename == NULL) {
1544 warnx("%s: you must supply a filename using -f", __func__);
1549 if (datalen == -1) {
1550 warnx("%s: you must specify the data length with -d", __func__);
1556 warnx("%s: you must specify the LBA with -l", __func__);
1561 if (blocksize == -1) {
1562 warnx("%s: you must specify the blocksize with -b", __func__);
1567 if (cdbsize != -1) {
1575 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
1576 "10, 12 or 16", __func__, cdbsize);
1579 break; /* NOTREACHED */
1584 databytes = datalen * blocksize;
1585 dataptr = (uint8_t *)malloc(databytes);
1587 if (dataptr == NULL) {
1588 warn("%s: can't allocate %d bytes\n", __func__, databytes);
1592 if (strcmp(filename, "-") == 0) {
1593 if (command == CTLADM_CMD_READ)
1594 file_fd = STDOUT_FILENO;
1596 file_fd = STDIN_FILENO;
1599 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1600 if (file_fd == -1) {
1601 warn("%s: can't open file %s", __func__, filename);
1607 memset(dataptr, 0, databytes);
1609 if (command == CTLADM_CMD_WRITE) {
1612 bytes_read = read(file_fd, dataptr, databytes);
1613 if (bytes_read == -1) {
1614 warn("%s: error reading file %s", __func__, filename);
1618 if (bytes_read != databytes) {
1619 warnx("%s: only read %d bytes from file %s",
1620 __func__, bytes_read, filename);
1625 ctl_scsi_read_write(io,
1626 /*data_ptr*/ dataptr,
1627 /*data_len*/ databytes,
1628 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
1630 /*minimum_cdb_size*/ cdbsize,
1632 /*num_blocks*/ datalen,
1633 /*tag_type*/ CTL_TAG_SIMPLE,
1636 io->io_hdr.nexus.targ_lun = lun;
1637 io->io_hdr.nexus.initid = iid;
1639 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1644 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1645 && (command == CTLADM_CMD_READ)) {
1648 bytes_written = write(file_fd, dataptr, databytes);
1649 if (bytes_written == -1) {
1650 warn("%s: can't write to %s", __func__, filename);
1653 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1654 ctl_io_error_print(io, NULL, stderr);
1659 ctl_scsi_free_io(io);
1661 if (dataptr != NULL)
1672 cctl_get_luns(int fd, int lun, int iid, int retries, struct
1673 scsi_report_luns_data **lun_data, uint32_t *num_luns)
1682 io = ctl_scsi_alloc_io(iid);
1684 warnx("%s: can't allocate memory", __func__);
1689 * lun_data includes space for 1 lun, allocate space for 4 initially.
1690 * If that isn't enough, we'll allocate more.
1694 lun_datalen = sizeof(*lun_data) +
1695 (nluns * sizeof(struct scsi_report_luns_lundata));
1696 *lun_data = malloc(lun_datalen);
1698 if (*lun_data == NULL) {
1699 warnx("%s: can't allocate memory", __func__);
1700 ctl_scsi_free_io(io);
1704 ctl_scsi_report_luns(io,
1705 /*data_ptr*/ (uint8_t *)*lun_data,
1706 /*data_len*/ lun_datalen,
1707 /*select_report*/ RPL_REPORT_ALL,
1708 /*tag_type*/ CTL_TAG_SIMPLE,
1711 io->io_hdr.nexus.initid = iid;
1712 io->io_hdr.nexus.targ_lun = lun;
1714 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1719 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1720 uint32_t returned_len, returned_luns;
1722 returned_len = scsi_4btoul((*lun_data)->length);
1723 returned_luns = returned_len / 8;
1724 if (returned_luns > nluns) {
1725 nluns = returned_luns;
1729 /* These should be the same */
1730 *num_luns = MIN(returned_luns, nluns);
1732 ctl_io_error_print(io, NULL, stderr);
1736 ctl_scsi_free_io(io);
1742 cctl_report_luns(int fd, int lun, int iid, int retries)
1744 struct scsi_report_luns_data *lun_data;
1745 uint32_t num_luns, i;
1750 if ((retval = cctl_get_luns(fd, lun, iid, retries, &lun_data,
1754 fprintf(stdout, "%u LUNs returned\n", num_luns);
1755 for (i = 0; i < num_luns; i++) {
1759 * XXX KDM figure out a way to share this code with
1762 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1763 case RPL_LUNDATA_ATYP_PERIPH:
1764 lun_val = lun_data->luns[i].lundata[1];
1766 case RPL_LUNDATA_ATYP_FLAT:
1767 lun_val = (lun_data->luns[i].lundata[0] &
1768 RPL_LUNDATA_FLAT_LUN_MASK) |
1769 (lun_data->luns[i].lundata[1] <<
1770 RPL_LUNDATA_FLAT_LUN_BITS);
1772 case RPL_LUNDATA_ATYP_LUN:
1773 case RPL_LUNDATA_ATYP_EXTLUN:
1775 fprintf(stdout, "Unsupported LUN format %d\n",
1776 lun_data->luns[i].lundata[0] &
1777 RPL_LUNDATA_ATYP_MASK);
1784 fprintf(stdout, "%d\n", lun_val);
1788 if (lun_data != NULL)
1795 cctl_tur(int fd, int lun, int iid, int retries)
1799 io = ctl_scsi_alloc_io(iid);
1801 fprintf(stderr, "can't allocate memory\n");
1806 /* tag_type */ CTL_TAG_SIMPLE,
1809 io->io_hdr.nexus.targ_lun = lun;
1810 io->io_hdr.nexus.initid = iid;
1812 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1813 ctl_scsi_free_io(io);
1817 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1818 fprintf(stdout, "Unit is ready\n");
1820 ctl_io_error_print(io, NULL, stderr);
1826 cctl_get_inquiry(int fd, int lun, int iid, int retries,
1827 char *path_str, int path_len,
1828 struct scsi_inquiry_data *inq_data)
1835 io = ctl_scsi_alloc_io(iid);
1837 warnx("cctl_inquiry: can't allocate memory\n");
1841 ctl_scsi_inquiry(/*io*/ io,
1842 /*data_ptr*/ (uint8_t *)inq_data,
1843 /*data_len*/ sizeof(*inq_data),
1846 /*tag_type*/ CTL_TAG_SIMPLE,
1849 io->io_hdr.nexus.targ_lun = lun;
1850 io->io_hdr.nexus.initid = iid;
1852 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1857 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
1859 ctl_io_error_print(io, NULL, stderr);
1860 } else if (path_str != NULL)
1861 ctl_scsi_path_string(io, path_str, path_len);
1864 ctl_scsi_free_io(io);
1870 cctl_inquiry(int fd, int lun, int iid, int retries)
1872 struct scsi_inquiry_data *inq_data;
1876 inq_data = malloc(sizeof(*inq_data));
1877 if (inq_data == NULL) {
1878 warnx("%s: can't allocate inquiry data", __func__);
1883 if ((retval = cctl_get_inquiry(fd, lun, iid, retries, scsi_path,
1884 sizeof(scsi_path), inq_data)) != 0)
1887 printf("%s", scsi_path);
1888 scsi_print_inquiry(inq_data);
1891 if (inq_data != NULL)
1898 cctl_req_sense(int fd, int lun, int iid, int retries)
1901 struct scsi_sense_data *sense_data;
1906 io = ctl_scsi_alloc_io(iid);
1908 warnx("cctl_req_sense: can't allocate memory\n");
1911 sense_data = malloc(sizeof(*sense_data));
1912 memset(sense_data, 0, sizeof(*sense_data));
1914 ctl_scsi_request_sense(/*io*/ io,
1915 /*data_ptr*/ (uint8_t *)sense_data,
1916 /*data_len*/ sizeof(*sense_data),
1918 /*tag_type*/ CTL_TAG_SIMPLE,
1921 io->io_hdr.nexus.targ_lun = lun;
1922 io->io_hdr.nexus.initid = iid;
1924 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1929 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1930 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
1931 io->scsiio.sense_len = sizeof(*sense_data);
1932 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
1934 ctl_io_error_print(io, NULL, stderr);
1938 ctl_scsi_free_io(io);
1945 cctl_report_target_port_group(int fd, int lun, int iid)
1955 io = ctl_scsi_alloc_io(iid);
1957 warn("%s: can't allocate memory", __func__);
1962 dataptr = (uint8_t *)malloc(datalen);
1963 if (dataptr == NULL) {
1964 warn("%s: can't allocate %d bytes", __func__, datalen);
1969 memset(dataptr, 0, datalen);
1971 ctl_scsi_maintenance_in(/*io*/ io,
1972 /*data_ptr*/ dataptr,
1973 /*data_len*/ datalen,
1974 /*action*/ SA_RPRT_TRGT_GRP,
1975 /*tag_type*/ CTL_TAG_SIMPLE,
1978 io->io_hdr.nexus.targ_lun = lun;
1979 io->io_hdr.nexus.initid = iid;
1981 if (cctl_do_io(fd, 0, io, __func__) != 0) {
1986 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1987 int returned_len, used_len;
1989 returned_len = scsi_4btoul(&dataptr[0]) + 4;
1991 for (used_len = 0; used_len < returned_len; used_len++) {
1992 fprintf(stdout, "0x%02x ", dataptr[used_len]);
1993 if (((used_len+1) % 8) == 0)
1994 fprintf(stdout, "\n");
1996 fprintf(stdout, "\n");
1998 ctl_io_error_print(io, NULL, stderr);
2001 ctl_scsi_free_io(io);
2003 if (dataptr != NULL)
2010 cctl_inquiry_vpd_devid(int fd, int lun, int iid)
2020 io = ctl_scsi_alloc_io(iid);
2022 warn("%s: can't allocate memory", __func__);
2027 dataptr = (uint8_t *)malloc(datalen);
2028 if (dataptr == NULL) {
2029 warn("%s: can't allocate %d bytes", __func__, datalen);
2034 memset(dataptr, 0, datalen);
2036 ctl_scsi_inquiry(/*io*/ io,
2037 /*data_ptr*/ dataptr,
2038 /*data_len*/ datalen,
2040 /*page_code*/ SVPD_DEVICE_ID,
2041 /*tag_type*/ CTL_TAG_SIMPLE,
2044 io->io_hdr.nexus.targ_lun = lun;
2045 io->io_hdr.nexus.initid = iid;
2047 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2052 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2053 int returned_len, used_len;
2055 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2057 for (used_len = 0; used_len < returned_len; used_len++) {
2058 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2059 if (((used_len+1) % 8) == 0)
2060 fprintf(stdout, "\n");
2062 fprintf(stdout, "\n");
2064 ctl_io_error_print(io, NULL, stderr);
2067 ctl_scsi_free_io(io);
2069 if (dataptr != NULL)
2076 cctl_persistent_reserve_in(int fd, int lun, int iid,
2077 int argc, char **argv, char *combinedopt,
2090 io = ctl_scsi_alloc_io(iid);
2092 warn("%s: can't allocate memory", __func__);
2096 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2099 action = strtol(optarg, NULL, 0);
2106 if (action < 0 || action > 2) {
2107 warn("action must be specified and in the range: 0-2");
2114 dataptr = (uint8_t *)malloc(datalen);
2115 if (dataptr == NULL) {
2116 warn("%s: can't allocate %d bytes", __func__, datalen);
2121 memset(dataptr, 0, datalen);
2123 ctl_scsi_persistent_res_in(io,
2124 /*data_ptr*/ dataptr,
2125 /*data_len*/ datalen,
2127 /*tag_type*/ CTL_TAG_SIMPLE,
2130 io->io_hdr.nexus.targ_lun = lun;
2131 io->io_hdr.nexus.initid = iid;
2133 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2138 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2139 int returned_len, used_len;
2143 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2144 returned_len = min(returned_len, 256);
2147 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2153 warnx("%s: invalid action %d", __func__, action);
2155 break; /* NOTREACHED */
2158 for (used_len = 0; used_len < returned_len; used_len++) {
2159 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2160 if (((used_len+1) % 8) == 0)
2161 fprintf(stdout, "\n");
2163 fprintf(stdout, "\n");
2165 ctl_io_error_print(io, NULL, stderr);
2168 ctl_scsi_free_io(io);
2170 if (dataptr != NULL)
2177 cctl_persistent_reserve_out(int fd, int lun, int iid,
2178 int argc, char **argv, char *combinedopt,
2183 uint64_t key = 0, sa_key = 0;
2184 int action = -1, restype = -1;
2192 io = ctl_scsi_alloc_io(iid);
2194 warn("%s: can't allocate memory", __func__);
2198 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2201 action = strtol(optarg, NULL, 0);
2204 key = strtoull(optarg, NULL, 0);
2207 restype = strtol(optarg, NULL, 0);
2210 sa_key = strtoull(optarg, NULL, 0);
2216 if (action < 0 || action > 5) {
2217 warn("action must be specified and in the range: 0-5");
2222 if (restype < 0 || restype > 5) {
2223 if (action != 0 && action != 5 && action != 3) {
2224 warn("'restype' must specified and in the range: 0-5");
2231 dataptr = (uint8_t *)malloc(datalen);
2232 if (dataptr == NULL) {
2233 warn("%s: can't allocate %d bytes", __func__, datalen);
2238 memset(dataptr, 0, datalen);
2240 ctl_scsi_persistent_res_out(io,
2241 /*data_ptr*/ dataptr,
2242 /*data_len*/ datalen,
2247 /*tag_type*/ CTL_TAG_SIMPLE,
2250 io->io_hdr.nexus.targ_lun = lun;
2251 io->io_hdr.nexus.initid = iid;
2253 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2257 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2259 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2260 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2261 "successfully\n", scsi_path);
2263 ctl_io_error_print(io, NULL, stderr);
2266 ctl_scsi_free_io(io);
2268 if (dataptr != NULL)
2274 struct cctl_req_option {
2279 STAILQ_ENTRY(cctl_req_option) links;
2283 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2285 struct ctl_lun_req req;
2286 int device_type = -1;
2287 uint64_t lun_size = 0;
2288 uint32_t blocksize = 0, req_lun_id = 0;
2289 char *serial_num = NULL;
2290 char *device_id = NULL;
2291 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
2292 char *backend_name = NULL;
2293 STAILQ_HEAD(, cctl_req_option) option_list;
2294 int num_options = 0;
2297 STAILQ_INIT(&option_list);
2299 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2302 backend_name = strdup(optarg);
2305 blocksize = strtoul(optarg, NULL, 0);
2309 device_id = strdup(optarg);
2312 req_lun_id = strtoul(optarg, NULL, 0);
2316 struct cctl_req_option *option;
2320 tmpstr = strdup(optarg);
2321 name = strsep(&tmpstr, "=");
2323 warnx("%s: option -o takes \"name=value\""
2324 "argument", __func__);
2328 value = strsep(&tmpstr, "=");
2329 if (value == NULL) {
2330 warnx("%s: option -o takes \"name=value\""
2331 "argument", __func__);
2335 option = malloc(sizeof(*option));
2336 if (option == NULL) {
2337 warn("%s: error allocating %zd bytes",
2338 __func__, sizeof(*option));
2342 option->name = strdup(name);
2343 option->namelen = strlen(name) + 1;
2344 option->value = strdup(value);
2345 option->vallen = strlen(value) + 1;
2348 STAILQ_INSERT_TAIL(&option_list, option, links);
2353 if (strcasecmp(optarg, "auto") != 0) {
2354 retval = expand_number(optarg, &lun_size);
2356 warn("%s: invalid -s argument",
2365 serial_num = strdup(optarg);
2368 device_type = strtoul(optarg, NULL, 0);
2375 if (backend_name == NULL) {
2376 warnx("%s: backend name (-b) must be specified", __func__);
2381 bzero(&req, sizeof(req));
2383 strlcpy(req.backend, backend_name, sizeof(req.backend));
2384 req.reqtype = CTL_LUNREQ_CREATE;
2386 if (blocksize_set != 0)
2387 req.reqdata.create.blocksize_bytes = blocksize;
2389 if (lun_size_set != 0)
2390 req.reqdata.create.lun_size_bytes = lun_size;
2392 if (lun_id_set != 0) {
2393 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
2394 req.reqdata.create.req_lun_id = req_lun_id;
2397 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
2399 if (device_type != -1)
2400 req.reqdata.create.device_type = device_type;
2402 req.reqdata.create.device_type = T_DIRECT;
2404 if (serial_num != NULL) {
2405 strlcpy(req.reqdata.create.serial_num, serial_num,
2406 sizeof(req.reqdata.create.serial_num));
2407 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
2410 if (device_id != NULL) {
2411 strlcpy(req.reqdata.create.device_id, device_id,
2412 sizeof(req.reqdata.create.device_id));
2413 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
2416 req.num_be_args = num_options;
2417 if (num_options > 0) {
2418 struct cctl_req_option *option, *next_option;
2421 req.be_args = malloc(num_options * sizeof(*req.be_args));
2422 if (req.be_args == NULL) {
2423 warn("%s: error allocating %zd bytes", __func__,
2424 num_options * sizeof(*req.be_args));
2429 for (i = 0, option = STAILQ_FIRST(&option_list);
2430 i < num_options; i++, option = next_option) {
2431 next_option = STAILQ_NEXT(option, links);
2433 req.be_args[i].namelen = option->namelen;
2434 req.be_args[i].name = strdup(option->name);
2435 req.be_args[i].vallen = option->vallen;
2436 req.be_args[i].value = strdup(option->value);
2438 * XXX KDM do we want a way to specify a writeable
2439 * flag of some sort? Do we want a way to specify
2442 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
2444 STAILQ_REMOVE(&option_list, option, cctl_req_option,
2447 free(option->value);
2452 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
2453 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2458 switch (req.status) {
2460 warnx("LUN creation error: %s", req.error_str);
2463 case CTL_LUN_WARNING:
2464 warnx("LUN creation warning: %s", req.error_str);
2469 warnx("unknown LUN creation status: %d", req.status);
2474 fprintf(stdout, "LUN created successfully\n");
2475 fprintf(stdout, "backend: %s\n", req.backend);
2476 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
2477 fprintf(stdout, "LUN size: %ju bytes\n",
2478 (uintmax_t)req.reqdata.create.lun_size_bytes);
2479 fprintf(stdout, "blocksize %u bytes\n",
2480 req.reqdata.create.blocksize_bytes);
2481 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
2482 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
2483 fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
2490 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
2492 struct ctl_lun_req req;
2493 uint32_t lun_id = 0;
2495 char *backend_name = NULL;
2496 STAILQ_HEAD(, cctl_req_option) option_list;
2497 int num_options = 0;
2500 STAILQ_INIT(&option_list);
2502 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2505 backend_name = strdup(optarg);
2508 lun_id = strtoul(optarg, NULL, 0);
2512 struct cctl_req_option *option;
2516 tmpstr = strdup(optarg);
2517 name = strsep(&tmpstr, "=");
2519 warnx("%s: option -o takes \"name=value\""
2520 "argument", __func__);
2524 value = strsep(&tmpstr, "=");
2525 if (value == NULL) {
2526 warnx("%s: option -o takes \"name=value\""
2527 "argument", __func__);
2531 option = malloc(sizeof(*option));
2532 if (option == NULL) {
2533 warn("%s: error allocating %zd bytes",
2534 __func__, sizeof(*option));
2538 option->name = strdup(name);
2539 option->namelen = strlen(name) + 1;
2540 option->value = strdup(value);
2541 option->vallen = strlen(value) + 1;
2544 STAILQ_INSERT_TAIL(&option_list, option, links);
2553 if (backend_name == NULL)
2554 errx(1, "%s: backend name (-b) must be specified", __func__);
2556 if (lun_id_set == 0)
2557 errx(1, "%s: LUN id (-l) must be specified", __func__);
2559 bzero(&req, sizeof(req));
2561 strlcpy(req.backend, backend_name, sizeof(req.backend));
2562 req.reqtype = CTL_LUNREQ_RM;
2564 req.reqdata.rm.lun_id = lun_id;
2566 req.num_be_args = num_options;
2567 if (num_options > 0) {
2568 struct cctl_req_option *option, *next_option;
2571 req.be_args = malloc(num_options * sizeof(*req.be_args));
2572 if (req.be_args == NULL) {
2573 warn("%s: error allocating %zd bytes", __func__,
2574 num_options * sizeof(*req.be_args));
2579 for (i = 0, option = STAILQ_FIRST(&option_list);
2580 i < num_options; i++, option = next_option) {
2581 next_option = STAILQ_NEXT(option, links);
2583 req.be_args[i].namelen = option->namelen;
2584 req.be_args[i].name = strdup(option->name);
2585 req.be_args[i].vallen = option->vallen;
2586 req.be_args[i].value = strdup(option->value);
2588 * XXX KDM do we want a way to specify a writeable
2589 * flag of some sort? Do we want a way to specify
2592 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
2594 STAILQ_REMOVE(&option_list, option, cctl_req_option,
2597 free(option->value);
2602 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
2603 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2608 switch (req.status) {
2610 warnx("LUN removal error: %s", req.error_str);
2613 case CTL_LUN_WARNING:
2614 warnx("LUN removal warning: %s", req.error_str);
2619 warnx("unknown LUN removal status: %d", req.status);
2624 printf("LUN %d removed successfully\n", lun_id);
2631 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
2633 struct ctl_lun_req req;
2634 uint64_t lun_size = 0;
2635 uint32_t lun_id = 0;
2636 int lun_id_set = 0, lun_size_set = 0;
2637 char *backend_name = NULL;
2638 STAILQ_HEAD(, cctl_req_option) option_list;
2639 int num_options = 0;
2642 STAILQ_INIT(&option_list);
2643 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2646 backend_name = strdup(optarg);
2649 lun_id = strtoul(optarg, NULL, 0);
2653 struct cctl_req_option *option;
2657 tmpstr = strdup(optarg);
2658 name = strsep(&tmpstr, "=");
2660 warnx("%s: option -o takes \"name=value\""
2661 "argument", __func__);
2665 value = strsep(&tmpstr, "=");
2666 if (value == NULL) {
2667 warnx("%s: option -o takes \"name=value\""
2668 "argument", __func__);
2672 option = malloc(sizeof(*option));
2673 if (option == NULL) {
2674 warn("%s: error allocating %zd bytes",
2675 __func__, sizeof(*option));
2679 option->name = strdup(name);
2680 option->namelen = strlen(name) + 1;
2681 option->value = strdup(value);
2682 option->vallen = strlen(value) + 1;
2685 STAILQ_INSERT_TAIL(&option_list, option, links);
2690 if (strcasecmp(optarg, "auto") != 0) {
2691 retval = expand_number(optarg, &lun_size);
2693 warn("%s: invalid -s argument",
2706 if (backend_name == NULL)
2707 errx(1, "%s: backend name (-b) must be specified", __func__);
2709 if (lun_id_set == 0)
2710 errx(1, "%s: LUN id (-l) must be specified", __func__);
2712 if (lun_size_set == 0 && num_options == 0)
2713 errx(1, "%s: size (-s) or options (-o) must be specified",
2716 bzero(&req, sizeof(req));
2718 strlcpy(req.backend, backend_name, sizeof(req.backend));
2719 req.reqtype = CTL_LUNREQ_MODIFY;
2721 req.reqdata.modify.lun_id = lun_id;
2722 req.reqdata.modify.lun_size_bytes = lun_size;
2724 req.num_be_args = num_options;
2725 if (num_options > 0) {
2726 struct cctl_req_option *option, *next_option;
2729 req.be_args = malloc(num_options * sizeof(*req.be_args));
2730 if (req.be_args == NULL) {
2731 warn("%s: error allocating %zd bytes", __func__,
2732 num_options * sizeof(*req.be_args));
2737 for (i = 0, option = STAILQ_FIRST(&option_list);
2738 i < num_options; i++, option = next_option) {
2739 next_option = STAILQ_NEXT(option, links);
2741 req.be_args[i].namelen = option->namelen;
2742 req.be_args[i].name = strdup(option->name);
2743 req.be_args[i].vallen = option->vallen;
2744 req.be_args[i].value = strdup(option->value);
2746 * XXX KDM do we want a way to specify a writeable
2747 * flag of some sort? Do we want a way to specify
2750 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
2752 STAILQ_REMOVE(&option_list, option, cctl_req_option,
2755 free(option->value);
2760 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
2761 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2766 switch (req.status) {
2768 warnx("LUN modification error: %s", req.error_str);
2771 case CTL_LUN_WARNING:
2772 warnx("LUN modification warning: %s", req.error_str);
2777 warnx("unknown LUN modification status: %d", req.status);
2782 printf("LUN %d modified successfully\n", lun_id);
2788 struct cctl_islist_conn {
2791 char *initiator_addr;
2792 char *initiator_alias;
2795 char *header_digest;
2797 char *max_recv_data_segment_length;
2798 char *max_send_data_segment_length;
2799 char *max_burst_length;
2800 char *first_burst_length;
2804 STAILQ_ENTRY(cctl_islist_conn) links;
2807 struct cctl_islist_data {
2809 STAILQ_HEAD(,cctl_islist_conn) conn_list;
2810 struct cctl_islist_conn *cur_conn;
2812 struct sbuf *cur_sb[32];
2816 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
2819 struct cctl_islist_data *islist;
2820 struct cctl_islist_conn *cur_conn;
2822 islist = (struct cctl_islist_data *)user_data;
2823 cur_conn = islist->cur_conn;
2825 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
2826 sizeof(islist->cur_sb[0])))
2827 errx(1, "%s: too many nesting levels, %zd max", __func__,
2828 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
2830 islist->cur_sb[islist->level] = sbuf_new_auto();
2831 if (islist->cur_sb[islist->level] == NULL)
2832 err(1, "%s: Unable to allocate sbuf", __func__);
2834 if (strcmp(name, "connection") == 0) {
2835 if (cur_conn != NULL)
2836 errx(1, "%s: improper connection element nesting",
2839 cur_conn = calloc(1, sizeof(*cur_conn));
2840 if (cur_conn == NULL)
2841 err(1, "%s: cannot allocate %zd bytes", __func__,
2844 islist->num_conns++;
2845 islist->cur_conn = cur_conn;
2847 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
2849 for (i = 0; attr[i] != NULL; i += 2) {
2850 if (strcmp(attr[i], "id") == 0) {
2851 cur_conn->connection_id =
2852 strtoull(attr[i+1], NULL, 0);
2855 "%s: invalid connection attribute %s = %s",
2856 __func__, attr[i], attr[i+1]);
2863 cctl_islist_end_element(void *user_data, const char *name)
2865 struct cctl_islist_data *islist;
2866 struct cctl_islist_conn *cur_conn;
2869 islist = (struct cctl_islist_data *)user_data;
2870 cur_conn = islist->cur_conn;
2872 if ((cur_conn == NULL)
2873 && (strcmp(name, "ctlislist") != 0))
2874 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
2876 if (islist->cur_sb[islist->level] == NULL)
2877 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
2878 islist->level, name);
2880 sbuf_finish(islist->cur_sb[islist->level]);
2881 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
2883 err(1, "%s can't allocate %zd bytes for string", __func__,
2884 sbuf_len(islist->cur_sb[islist->level]));
2886 sbuf_delete(islist->cur_sb[islist->level]);
2887 islist->cur_sb[islist->level] = NULL;
2890 if (strcmp(name, "initiator") == 0) {
2891 cur_conn->initiator = str;
2893 } else if (strcmp(name, "initiator_addr") == 0) {
2894 cur_conn->initiator_addr = str;
2896 } else if (strcmp(name, "initiator_alias") == 0) {
2897 cur_conn->initiator_alias = str;
2899 } else if (strcmp(name, "target") == 0) {
2900 cur_conn->target = str;
2902 } else if (strcmp(name, "target_alias") == 0) {
2903 cur_conn->target_alias = str;
2905 } else if (strcmp(name, "target_portal_group_tag") == 0) {
2906 } else if (strcmp(name, "header_digest") == 0) {
2907 cur_conn->header_digest = str;
2909 } else if (strcmp(name, "data_digest") == 0) {
2910 cur_conn->data_digest = str;
2912 } else if (strcmp(name, "max_recv_data_segment_length") == 0) {
2913 cur_conn->max_recv_data_segment_length = str;
2915 } else if (strcmp(name, "max_send_data_segment_length") == 0) {
2916 cur_conn->max_send_data_segment_length = str;
2918 } else if (strcmp(name, "max_burst_length") == 0) {
2919 cur_conn->max_burst_length = str;
2921 } else if (strcmp(name, "first_burst_length") == 0) {
2922 cur_conn->first_burst_length = str;
2924 } else if (strcmp(name, "offload") == 0) {
2925 cur_conn->offload = str;
2927 } else if (strcmp(name, "immediate_data") == 0) {
2928 cur_conn->immediate_data = atoi(str);
2929 } else if (strcmp(name, "iser") == 0) {
2930 cur_conn->iser = atoi(str);
2931 } else if (strcmp(name, "connection") == 0) {
2932 islist->cur_conn = NULL;
2933 } else if (strcmp(name, "ctlislist") == 0) {
2937 * Unknown element; ignore it for forward compatibility.
2945 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
2947 struct cctl_islist_data *islist;
2949 islist = (struct cctl_islist_data *)user_data;
2951 sbuf_bcat(islist->cur_sb[islist->level], str, len);
2955 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
2957 struct ctl_iscsi req;
2958 struct cctl_islist_data islist;
2959 struct cctl_islist_conn *conn;
2964 int c, retval, verbose = 0;
2969 bzero(&islist, sizeof(islist));
2970 STAILQ_INIT(&islist.conn_list);
2972 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2986 conn_str = malloc(conn_len);
2988 bzero(&req, sizeof(req));
2989 req.type = CTL_ISCSI_LIST;
2990 req.data.list.alloc_len = conn_len;
2991 req.data.list.conn_xml = conn_str;
2993 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
2994 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
2999 if (req.status == CTL_ISCSI_ERROR) {
3000 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
3001 __func__, req.error_str);
3002 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
3003 conn_len = conn_len << 1;
3007 if (dump_xml != 0) {
3008 printf("%s", conn_str);
3012 parser = XML_ParserCreate(NULL);
3013 if (parser == NULL) {
3014 warn("%s: Unable to create XML parser", __func__);
3019 XML_SetUserData(parser, &islist);
3020 XML_SetElementHandler(parser, cctl_islist_start_element,
3021 cctl_islist_end_element);
3022 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3024 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3026 warnx("%s: Unable to parse XML: Error %d", __func__,
3027 XML_GetErrorCode(parser));
3028 XML_ParserFree(parser);
3033 XML_ParserFree(parser);
3036 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3037 printf("%-25s %d\n", "Session ID:", conn->connection_id);
3038 printf("%-25s %s\n", "Initiator name:", conn->initiator);
3039 printf("%-25s %s\n", "Initiator portal:", conn->initiator_addr);
3040 printf("%-25s %s\n", "Initiator alias:", conn->initiator_alias);
3041 printf("%-25s %s\n", "Target name:", conn->target);
3042 printf("%-25s %s\n", "Target alias:", conn->target_alias);
3043 printf("%-25s %s\n", "Header digest:", conn->header_digest);
3044 printf("%-25s %s\n", "Data digest:", conn->data_digest);
3045 printf("%-25s %s\n", "MaxRecvDataSegmentLength:", conn->max_recv_data_segment_length);
3046 printf("%-25s %s\n", "MaxSendDataSegmentLength:", conn->max_send_data_segment_length);
3047 printf("%-25s %s\n", "MaxBurstLen:", conn->max_burst_length);
3048 printf("%-25s %s\n", "FirstBurstLen:", conn->first_burst_length);
3049 printf("%-25s %s\n", "ImmediateData:", conn->immediate_data ? "Yes" : "No");
3050 printf("%-25s %s\n", "iSER (RDMA):", conn->iser ? "Yes" : "No");
3051 printf("%-25s %s\n", "Offload driver:", conn->offload);
3055 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3057 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3058 printf("%4u %-16s %-36s %-36s\n",
3059 conn->connection_id, conn->initiator_addr, conn->initiator,
3070 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3072 struct ctl_iscsi req;
3074 int all = 0, connection_id = -1, nargs = 0;
3075 char *initiator_name = NULL, *initiator_addr = NULL;
3077 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3084 connection_id = strtoul(optarg, NULL, 0);
3088 initiator_name = strdup(optarg);
3089 if (initiator_name == NULL)
3090 err(1, "%s: strdup", __func__);
3094 initiator_addr = strdup(optarg);
3095 if (initiator_addr == NULL)
3096 err(1, "%s: strdup", __func__);
3105 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3108 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3111 bzero(&req, sizeof(req));
3112 req.type = CTL_ISCSI_LOGOUT;
3113 req.data.logout.connection_id = connection_id;
3114 if (initiator_addr != NULL)
3115 strlcpy(req.data.logout.initiator_addr,
3116 initiator_addr, sizeof(req.data.logout.initiator_addr));
3117 if (initiator_name != NULL)
3118 strlcpy(req.data.logout.initiator_name,
3119 initiator_name, sizeof(req.data.logout.initiator_name));
3121 req.data.logout.all = 1;
3123 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3124 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3129 if (req.status != CTL_ISCSI_OK) {
3130 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3131 __func__, req.error_str);
3136 printf("iSCSI logout requests submitted\n");
3143 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3145 struct ctl_iscsi req;
3147 int all = 0, connection_id = -1, nargs = 0;
3148 char *initiator_name = NULL, *initiator_addr = NULL;
3150 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3157 connection_id = strtoul(optarg, NULL, 0);
3161 initiator_name = strdup(optarg);
3162 if (initiator_name == NULL)
3163 err(1, "%s: strdup", __func__);
3167 initiator_addr = strdup(optarg);
3168 if (initiator_addr == NULL)
3169 err(1, "%s: strdup", __func__);
3178 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3181 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3184 bzero(&req, sizeof(req));
3185 req.type = CTL_ISCSI_TERMINATE;
3186 req.data.terminate.connection_id = connection_id;
3187 if (initiator_addr != NULL)
3188 strlcpy(req.data.terminate.initiator_addr,
3189 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3190 if (initiator_name != NULL)
3191 strlcpy(req.data.terminate.initiator_name,
3192 initiator_name, sizeof(req.data.terminate.initiator_name));
3194 req.data.terminate.all = 1;
3196 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3197 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3202 if (req.status != CTL_ISCSI_OK) {
3203 warnx("%s: error returned from CTL iSCSI connection "
3204 "termination request:\n%s", __func__, req.error_str);
3209 printf("iSCSI connections terminated\n");
3216 * Name/value pair used for per-LUN attributes.
3218 struct cctl_lun_nv {
3221 STAILQ_ENTRY(cctl_lun_nv) links;
3225 * Backend LUN information.
3230 uint64_t size_blocks;
3232 char *serial_number;
3234 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3235 STAILQ_ENTRY(cctl_lun) links;
3238 struct cctl_devlist_data {
3240 STAILQ_HEAD(,cctl_lun) lun_list;
3241 struct cctl_lun *cur_lun;
3243 struct sbuf *cur_sb[32];
3247 cctl_start_element(void *user_data, const char *name, const char **attr)
3250 struct cctl_devlist_data *devlist;
3251 struct cctl_lun *cur_lun;
3253 devlist = (struct cctl_devlist_data *)user_data;
3254 cur_lun = devlist->cur_lun;
3256 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3257 sizeof(devlist->cur_sb[0])))
3258 errx(1, "%s: too many nesting levels, %zd max", __func__,
3259 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3261 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3262 if (devlist->cur_sb[devlist->level] == NULL)
3263 err(1, "%s: Unable to allocate sbuf", __func__);
3265 if (strcmp(name, "lun") == 0) {
3266 if (cur_lun != NULL)
3267 errx(1, "%s: improper lun element nesting", __func__);
3269 cur_lun = calloc(1, sizeof(*cur_lun));
3270 if (cur_lun == NULL)
3271 err(1, "%s: cannot allocate %zd bytes", __func__,
3274 devlist->num_luns++;
3275 devlist->cur_lun = cur_lun;
3277 STAILQ_INIT(&cur_lun->attr_list);
3278 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3280 for (i = 0; attr[i] != NULL; i += 2) {
3281 if (strcmp(attr[i], "id") == 0) {
3282 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3284 errx(1, "%s: invalid LUN attribute %s = %s",
3285 __func__, attr[i], attr[i+1]);
3292 cctl_end_element(void *user_data, const char *name)
3294 struct cctl_devlist_data *devlist;
3295 struct cctl_lun *cur_lun;
3298 devlist = (struct cctl_devlist_data *)user_data;
3299 cur_lun = devlist->cur_lun;
3301 if ((cur_lun == NULL)
3302 && (strcmp(name, "ctllunlist") != 0))
3303 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3305 if (devlist->cur_sb[devlist->level] == NULL)
3306 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3307 devlist->level, name);
3309 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3310 err(1, "%s: sbuf_finish", __func__);
3311 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3313 err(1, "%s can't allocate %zd bytes for string", __func__,
3314 sbuf_len(devlist->cur_sb[devlist->level]));
3316 if (strlen(str) == 0) {
3321 sbuf_delete(devlist->cur_sb[devlist->level]);
3322 devlist->cur_sb[devlist->level] = NULL;
3325 if (strcmp(name, "backend_type") == 0) {
3326 cur_lun->backend_type = str;
3328 } else if (strcmp(name, "size") == 0) {
3329 cur_lun->size_blocks = strtoull(str, NULL, 0);
3330 } else if (strcmp(name, "blocksize") == 0) {
3331 cur_lun->blocksize = strtoul(str, NULL, 0);
3332 } else if (strcmp(name, "serial_number") == 0) {
3333 cur_lun->serial_number = str;
3335 } else if (strcmp(name, "device_id") == 0) {
3336 cur_lun->device_id = str;
3338 } else if (strcmp(name, "lun") == 0) {
3339 devlist->cur_lun = NULL;
3340 } else if (strcmp(name, "ctllunlist") == 0) {
3343 struct cctl_lun_nv *nv;
3345 nv = calloc(1, sizeof(*nv));
3347 err(1, "%s: can't allocate %zd bytes for nv pair",
3348 __func__, sizeof(*nv));
3350 nv->name = strdup(name);
3351 if (nv->name == NULL)
3352 err(1, "%s: can't allocated %zd bytes for string",
3353 __func__, strlen(name));
3357 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3364 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3366 struct cctl_devlist_data *devlist;
3368 devlist = (struct cctl_devlist_data *)user_data;
3370 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3374 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3376 struct ctl_lun_list list;
3377 struct cctl_devlist_data devlist;
3378 struct cctl_lun *lun;
3384 char *backend = NULL;
3390 bzero(&devlist, sizeof(devlist));
3391 STAILQ_INIT(&devlist.lun_list);
3393 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3396 backend = strdup(optarg);
3410 lun_str = malloc(lun_len);
3412 bzero(&list, sizeof(list));
3413 list.alloc_len = lun_len;
3414 list.status = CTL_LUN_LIST_NONE;
3415 list.lun_xml = lun_str;
3417 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
3418 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
3423 if (list.status == CTL_LUN_LIST_ERROR) {
3424 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
3425 __func__, list.error_str);
3426 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3427 lun_len = lun_len << 1;
3431 if (dump_xml != 0) {
3432 printf("%s", lun_str);
3436 parser = XML_ParserCreate(NULL);
3437 if (parser == NULL) {
3438 warn("%s: Unable to create XML parser", __func__);
3443 XML_SetUserData(parser, &devlist);
3444 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
3445 XML_SetCharacterDataHandler(parser, cctl_char_handler);
3447 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
3449 warnx("%s: Unable to parse XML: Error %d", __func__,
3450 XML_GetErrorCode(parser));
3451 XML_ParserFree(parser);
3456 XML_ParserFree(parser);
3458 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
3459 "Serial Number", "Device ID");
3460 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
3461 struct cctl_lun_nv *nv;
3463 if ((backend != NULL)
3464 && (strcmp(lun->backend_type, backend) != 0))
3467 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
3468 (uintmax_t)lun->lun_id,
3469 lun->backend_type, (uintmax_t)lun->size_blocks,
3470 lun->blocksize, lun->serial_number, lun->device_id);
3475 STAILQ_FOREACH(nv, &lun->attr_list, links) {
3476 printf(" %s=%s\n", nv->name, nv->value);
3491 char *frontend_type;
3494 char *target, *port, *lun_map;
3495 STAILQ_HEAD(,cctl_lun_nv) init_list;
3496 STAILQ_HEAD(,cctl_lun_nv) lun_list;
3497 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3498 STAILQ_ENTRY(cctl_port) links;
3501 struct cctl_portlist_data {
3503 STAILQ_HEAD(,cctl_port) port_list;
3504 struct cctl_port *cur_port;
3507 struct sbuf *cur_sb[32];
3511 cctl_start_pelement(void *user_data, const char *name, const char **attr)
3514 struct cctl_portlist_data *portlist;
3515 struct cctl_port *cur_port;
3517 portlist = (struct cctl_portlist_data *)user_data;
3518 cur_port = portlist->cur_port;
3520 if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
3521 sizeof(portlist->cur_sb[0])))
3522 errx(1, "%s: too many nesting levels, %zd max", __func__,
3523 sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
3525 portlist->cur_sb[portlist->level] = sbuf_new_auto();
3526 if (portlist->cur_sb[portlist->level] == NULL)
3527 err(1, "%s: Unable to allocate sbuf", __func__);
3529 portlist->cur_id = 0;
3530 for (i = 0; attr[i] != NULL; i += 2) {
3531 if (strcmp(attr[i], "id") == 0) {
3532 portlist->cur_id = strtoull(attr[i+1], NULL, 0);
3537 if (strcmp(name, "targ_port") == 0) {
3538 if (cur_port != NULL)
3539 errx(1, "%s: improper port element nesting", __func__);
3541 cur_port = calloc(1, sizeof(*cur_port));
3542 if (cur_port == NULL)
3543 err(1, "%s: cannot allocate %zd bytes", __func__,
3546 portlist->num_ports++;
3547 portlist->cur_port = cur_port;
3549 STAILQ_INIT(&cur_port->init_list);
3550 STAILQ_INIT(&cur_port->lun_list);
3551 STAILQ_INIT(&cur_port->attr_list);
3552 cur_port->port_id = portlist->cur_id;
3553 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
3558 cctl_end_pelement(void *user_data, const char *name)
3560 struct cctl_portlist_data *portlist;
3561 struct cctl_port *cur_port;
3564 portlist = (struct cctl_portlist_data *)user_data;
3565 cur_port = portlist->cur_port;
3567 if ((cur_port == NULL)
3568 && (strcmp(name, "ctlportlist") != 0))
3569 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
3571 if (portlist->cur_sb[portlist->level] == NULL)
3572 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3573 portlist->level, name);
3575 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
3576 err(1, "%s: sbuf_finish", __func__);
3577 str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
3579 err(1, "%s can't allocate %zd bytes for string", __func__,
3580 sbuf_len(portlist->cur_sb[portlist->level]));
3582 if (strlen(str) == 0) {
3587 sbuf_delete(portlist->cur_sb[portlist->level]);
3588 portlist->cur_sb[portlist->level] = NULL;
3591 if (strcmp(name, "frontend_type") == 0) {
3592 cur_port->frontend_type = str;
3594 } else if (strcmp(name, "port_name") == 0) {
3595 cur_port->name = str;
3597 } else if (strcmp(name, "online") == 0) {
3598 cur_port->online = str;
3600 } else if (strcmp(name, "physical_port") == 0) {
3601 cur_port->pp = strtoull(str, NULL, 0);
3602 } else if (strcmp(name, "virtual_port") == 0) {
3603 cur_port->vp = strtoull(str, NULL, 0);
3604 } else if (strcmp(name, "target") == 0) {
3605 cur_port->target = str;
3607 } else if (strcmp(name, "port") == 0) {
3608 cur_port->port = str;
3610 } else if (strcmp(name, "lun_map") == 0) {
3611 cur_port->lun_map = str;
3613 } else if (strcmp(name, "targ_port") == 0) {
3614 portlist->cur_port = NULL;
3615 } else if (strcmp(name, "ctlportlist") == 0) {
3618 struct cctl_lun_nv *nv;
3620 nv = calloc(1, sizeof(*nv));
3622 err(1, "%s: can't allocate %zd bytes for nv pair",
3623 __func__, sizeof(*nv));
3625 if (strcmp(name, "initiator") == 0 ||
3626 strcmp(name, "lun") == 0)
3627 asprintf(&nv->name, "%ju", portlist->cur_id);
3629 nv->name = strdup(name);
3630 if (nv->name == NULL)
3631 err(1, "%s: can't allocated %zd bytes for string",
3632 __func__, strlen(name));
3636 if (strcmp(name, "initiator") == 0)
3637 STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links);
3638 else if (strcmp(name, "lun") == 0)
3639 STAILQ_INSERT_TAIL(&cur_port->lun_list, nv, links);
3641 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
3648 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
3650 struct cctl_portlist_data *portlist;
3652 portlist = (struct cctl_portlist_data *)user_data;
3654 sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
3658 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
3660 struct ctl_lun_list list;
3661 struct cctl_portlist_data portlist;
3662 struct cctl_port *port;
3668 char *frontend = NULL;
3669 uint64_t portarg = UINT64_MAX;
3670 int verbose = 0, init = 0, lun = 0, quiet = 0;
3675 bzero(&portlist, sizeof(portlist));
3676 STAILQ_INIT(&portlist.port_list);
3678 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3681 frontend = strdup(optarg);
3690 portarg = strtoll(optarg, NULL, 0);
3707 port_str = malloc(port_len);
3709 bzero(&list, sizeof(list));
3710 list.alloc_len = port_len;
3711 list.status = CTL_LUN_LIST_NONE;
3712 list.lun_xml = port_str;
3714 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
3715 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
3720 if (list.status == CTL_LUN_LIST_ERROR) {
3721 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
3722 __func__, list.error_str);
3723 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3724 port_len = port_len << 1;
3728 if (dump_xml != 0) {
3729 printf("%s", port_str);
3733 parser = XML_ParserCreate(NULL);
3734 if (parser == NULL) {
3735 warn("%s: Unable to create XML parser", __func__);
3740 XML_SetUserData(parser, &portlist);
3741 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
3742 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
3744 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
3746 warnx("%s: Unable to parse XML: Error %d", __func__,
3747 XML_GetErrorCode(parser));
3748 XML_ParserFree(parser);
3753 XML_ParserFree(parser);
3756 printf("Port Online Frontend Name pp vp\n");
3757 STAILQ_FOREACH(port, &portlist.port_list, links) {
3758 struct cctl_lun_nv *nv;
3760 if ((frontend != NULL)
3761 && (strcmp(port->frontend_type, frontend) != 0))
3764 if ((portarg != UINT64_MAX) && (portarg != port->port_id))
3767 printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n",
3768 (uintmax_t)port->port_id, port->online,
3769 port->frontend_type, port->name, port->pp, port->vp,
3770 port->port ? port->port : "");
3772 if (init || verbose) {
3774 printf(" Target: %s\n", port->target);
3775 STAILQ_FOREACH(nv, &port->init_list, links) {
3776 printf(" Initiator %s: %s\n",
3777 nv->name, nv->value);
3781 if (lun || verbose) {
3782 if (port->lun_map) {
3783 STAILQ_FOREACH(nv, &port->lun_list, links)
3784 printf(" LUN %s: %s\n",
3785 nv->name, nv->value);
3786 if (STAILQ_EMPTY(&port->lun_list))
3787 printf(" No LUNs mapped\n");
3789 printf(" All LUNs mapped\n");
3793 STAILQ_FOREACH(nv, &port->attr_list, links) {
3794 printf(" %s=%s\n", nv->name, nv->value);
3805 cctl_lunmap(int fd, int argc, char **argv, char *combinedopt)
3807 struct ctl_lun_map lm;
3811 lm.port = UINT32_MAX;
3812 lm.plun = UINT32_MAX;
3813 lm.lun = UINT32_MAX;
3815 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3818 lm.port = strtoll(optarg, NULL, 0);
3821 lm.plun = strtoll(optarg, NULL, 0);
3824 lm.lun = strtoll(optarg, NULL, 0);
3831 if (ioctl(fd, CTL_LUN_MAP, &lm) == -1) {
3832 warn("%s: error issuing CTL_LUN_MAP ioctl", __func__);
3842 fprintf(error ? stderr : stdout,
3844 "Primary commands:\n"
3845 " ctladm tur [dev_id][general options]\n"
3846 " ctladm inquiry [dev_id][general options]\n"
3847 " ctladm devid [dev_id][general options]\n"
3848 " ctladm reqsense [dev_id][general options]\n"
3849 " ctladm reportluns [dev_id][general options]\n"
3850 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
3851 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3852 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
3853 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3854 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
3855 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
3856 " [-d] [-S subpage] [-c cdbsize]\n"
3857 " ctladm prin [dev_id][general options] <-a action>\n"
3858 " ctladm prout [dev_id][general options] <-a action>\n"
3859 " <-r restype] [-k key] [-s sa_key]\n"
3860 " ctladm rtpg [dev_id][general options]\n"
3861 " ctladm start [dev_id][general options] [-i] [-o]\n"
3862 " ctladm stop [dev_id][general options] [-i] [-o]\n"
3863 " ctladm synccache [dev_id][general options] [-l lba]\n"
3864 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
3865 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
3866 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
3867 " [-S serial_num] [-t dev_type]\n"
3868 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
3869 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
3870 " ctladm devlist [-b backend] [-v] [-x]\n"
3872 " ctladm lunmap -p targ_port [-l pLUN] [-L cLUN]\n"
3873 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
3875 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
3876 " [-s len fmt [args]] [-c] [-d delete_id]\n"
3877 " ctladm port <-o <on|off> | [-w wwnn][-W wwpn]>\n"
3878 " [-p targ_port] [-t port_type]\n"
3879 " ctladm portlist [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n"
3880 " ctladm islist [-v | -x]\n"
3881 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
3882 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
3884 " ctladm dumpstructs\n"
3886 "General Options:\n"
3887 "-I intiator_id : defaults to 7, used to change the initiator id\n"
3888 "-C retries : specify the number of times to retry this command\n"
3889 "-D devicename : specify the device to operate on\n"
3890 " : (default is %s)\n"
3891 "read/write options:\n"
3892 "-l lba : logical block address\n"
3893 "-d len : read/write length, in blocks\n"
3894 "-f file|- : write/read data to/from file or stdout/stdin\n"
3895 "-b blocksize : block size, in bytes\n"
3896 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
3897 "-N : do not copy data to/from userland\n"
3898 "readcapacity options:\n"
3899 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3900 "modesense options:\n"
3901 "-m page : specify the mode page to view\n"
3902 "-l : request a list of supported pages\n"
3903 "-P pc : specify the page control value: 0-3 (current,\n"
3904 " changeable, default, saved, respectively)\n"
3905 "-d : disable block descriptors for mode sense\n"
3906 "-S subpage : specify a subpage\n"
3907 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
3908 "persistent reserve in options:\n"
3909 "-a action : specify the action value: 0-2 (read key, read\n"
3910 " reservation, read capabilities, respectively)\n"
3911 "persistent reserve out options:\n"
3912 "-a action : specify the action value: 0-5 (register, reserve,\n"
3913 " release, clear, preempt, register and ignore)\n"
3914 "-k key : key value\n"
3915 "-s sa_key : service action value\n"
3916 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
3917 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
3918 "start/stop options:\n"
3919 "-i : set the immediate bit (CTL does not support this)\n"
3920 "-o : set the on/offline bit\n"
3921 "synccache options:\n"
3922 "-l lba : set the starting LBA\n"
3923 "-b blockcount : set the length to sync in blocks\n"
3924 "-r : set the relative addressing bit\n"
3925 "-i : set the immediate bit\n"
3926 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3928 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3929 "-B blocksize : LUN blocksize in bytes (some backends)\n"
3930 "-d device_id : SCSI VPD page 0x83 ID\n"
3931 "-l lun_id : requested LUN number\n"
3932 "-o name=value : backend-specific options, multiple allowed\n"
3933 "-s size_bytes : LUN size in bytes (some backends)\n"
3934 "-S serial_num : SCSI VPD page 0x80 serial number\n"
3935 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
3937 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3938 "-l lun_id : LUN number to delete\n"
3939 "-o name=value : backend-specific options, multiple allowed\n"
3940 "devlist options:\n"
3941 "-b backend : list devices from specified backend only\n"
3942 "-v : be verbose, show backend attributes\n"
3943 "-x : dump raw XML\n"
3945 "-l datamove|done : delay command at datamove or done phase\n"
3946 "-T oneshot : delay one command, then resume normal completion\n"
3947 "-T cont : delay all commands\n"
3948 "-t secs : number of seconds to delay\n"
3950 "-i error_action : action to perform\n"
3951 "-p pattern : command pattern to look for\n"
3952 "-r lba,len : LBA range for pattern\n"
3953 "-s len fmt [args] : sense data for custom sense action\n"
3954 "-c : continuous operation\n"
3955 "-d delete_id : error id to delete\n"
3957 "-l : list frontend ports\n"
3958 "-o on|off : turn frontend ports on or off\n"
3959 "-w wwnn : set WWNN for one frontend\n"
3960 "-W wwpn : set WWPN for one frontend\n"
3961 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
3962 "-p targ_port : specify target port number\n"
3963 "-q : omit header in list output\n"
3964 "-x : output port list in XML format\n"
3965 "portlist options:\n"
3966 "-f frontend : specify frontend type\n"
3967 "-i : report target and initiators addresses\n"
3968 "-l : report LUN mapping\n"
3969 "-p targ_port : specify target port number\n"
3970 "-q : omit header in list output\n"
3971 "-v : verbose output (report all port options)\n"
3972 "-x : output port list in XML format\n"
3974 "-p targ_port : specify target port number\n"
3975 "-L pLUN : specify port-visible LUN\n"
3976 "-L cLUN : specify CTL LUN\n",
3981 main(int argc, char **argv)
3984 ctladm_cmdfunction command;
3985 ctladm_cmdargs cmdargs;
3986 ctladm_optret optreturn;
3988 const char *mainopt = "C:D:I:";
3989 const char *subopt = NULL;
3990 char combinedopt[256];
3999 cmdargs = CTLADM_ARG_NONE;
4000 command = CTLADM_CMD_HELP;
4014 * Get the base option.
4016 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4018 if (optreturn == CC_OR_AMBIGUOUS) {
4019 warnx("ambiguous option %s", argv[1]);
4022 } else if (optreturn == CC_OR_NOT_FOUND) {
4023 warnx("option %s not found", argv[1]);
4028 if (cmdargs & CTLADM_ARG_NEED_TL) {
4029 if ((argc < 3) || (!isdigit(argv[2][0]))) {
4030 warnx("option %s requires a lun argument",
4035 lun = strtol(argv[2], NULL, 0);
4037 cmdargs |= CTLADM_ARG_TARG_LUN;
4042 * Ahh, getopt(3) is a pain.
4044 * This is a gross hack. There really aren't many other good
4045 * options (excuse the pun) for parsing options in a situation like
4046 * this. getopt is kinda braindead, so you end up having to run
4047 * through the options twice, and give each invocation of getopt
4048 * the option string for the other invocation.
4050 * You would think that you could just have two groups of options.
4051 * The first group would get parsed by the first invocation of
4052 * getopt, and the second group would get parsed by the second
4053 * invocation of getopt. It doesn't quite work out that way. When
4054 * the first invocation of getopt finishes, it leaves optind pointing
4055 * to the argument _after_ the first argument in the second group.
4056 * So when the second invocation of getopt comes around, it doesn't
4057 * recognize the first argument it gets and then bails out.
4059 * A nice alternative would be to have a flag for getopt that says
4060 * "just keep parsing arguments even when you encounter an unknown
4061 * argument", but there isn't one. So there's no real clean way to
4062 * easily parse two sets of arguments without having one invocation
4063 * of getopt know about the other.
4065 * Without this hack, the first invocation of getopt would work as
4066 * long as the generic arguments are first, but the second invocation
4067 * (in the subfunction) would fail in one of two ways. In the case
4068 * where you don't set optreset, it would fail because optind may be
4069 * pointing to the argument after the one it should be pointing at.
4070 * In the case where you do set optreset, and reset optind, it would
4071 * fail because getopt would run into the first set of options, which
4072 * it doesn't understand.
4074 * All of this would "sort of" work if you could somehow figure out
4075 * whether optind had been incremented one option too far. The
4076 * mechanics of that, however, are more daunting than just giving
4077 * both invocations all of the expect options for either invocation.
4079 * Needless to say, I wouldn't mind if someone invented a better
4080 * (non-GPL!) command line parsing interface than getopt. I
4081 * wouldn't mind if someone added more knobs to getopt to make it
4082 * work better. Who knows, I may talk myself into doing it someday,
4083 * if the standards weenies let me. As it is, it just leads to
4084 * hackery like this and causes people to avoid it in some cases.
4086 * KDM, September 8th, 1998
4089 sprintf(combinedopt, "%s%s", mainopt, subopt);
4091 sprintf(combinedopt, "%s", mainopt);
4094 * Start getopt processing at argv[2/3], since we've already
4095 * accepted argv[1..2] as the command name, and as a possible
4101 * Now we run through the argument list looking for generic
4102 * options, and ignoring options that possibly belong to
4105 while ((c = getopt(argc, argv, combinedopt))!= -1){
4108 cmdargs |= CTLADM_ARG_RETRIES;
4109 retries = strtol(optarg, NULL, 0);
4112 device = strdup(optarg);
4113 cmdargs |= CTLADM_ARG_DEVICE;
4116 cmdargs |= CTLADM_ARG_INITIATOR;
4117 initid = strtol(optarg, NULL, 0);
4124 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4131 * Default to opening the CTL device for now.
4133 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4134 && (command != CTLADM_CMD_HELP)) {
4135 device = strdup(CTL_DEFAULT_DEV);
4136 cmdargs |= CTLADM_ARG_DEVICE;
4139 if ((cmdargs & CTLADM_ARG_DEVICE)
4140 && (command != CTLADM_CMD_HELP)) {
4141 fd = open(device, O_RDWR);
4142 if (fd == -1 && errno == ENOENT) {
4143 saved_errno = errno;
4144 retval = kldload("ctl");
4146 fd = open(device, O_RDWR);
4148 errno = saved_errno;
4151 fprintf(stderr, "%s: error opening %s: %s\n",
4152 argv[0], device, strerror(errno));
4158 if (modfind("cfiscsi") == -1 &&
4159 kldload("cfiscsi") == -1)
4160 warn("couldn't load cfiscsi");
4163 } else if ((command != CTLADM_CMD_HELP)
4164 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4165 fprintf(stderr, "%s: you must specify a device with the "
4166 "--device argument for this command\n", argv[0]);
4167 command = CTLADM_CMD_HELP;
4172 case CTLADM_CMD_TUR:
4173 retval = cctl_tur(fd, lun, initid, retries);
4175 case CTLADM_CMD_INQUIRY:
4176 retval = cctl_inquiry(fd, lun, initid, retries);
4178 case CTLADM_CMD_REQ_SENSE:
4179 retval = cctl_req_sense(fd, lun, initid, retries);
4181 case CTLADM_CMD_REPORT_LUNS:
4182 retval = cctl_report_luns(fd, lun, initid, retries);
4184 case CTLADM_CMD_CREATE:
4185 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4188 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4190 case CTLADM_CMD_DEVLIST:
4191 retval = cctl_devlist(fd, argc, argv, combinedopt);
4193 case CTLADM_CMD_READ:
4194 case CTLADM_CMD_WRITE:
4195 retval = cctl_read_write(fd, lun, initid, retries,
4196 argc, argv, combinedopt, command);
4198 case CTLADM_CMD_PORT:
4199 retval = cctl_port(fd, argc, argv, combinedopt);
4201 case CTLADM_CMD_PORTLIST:
4202 retval = cctl_portlist(fd, argc, argv, combinedopt);
4204 case CTLADM_CMD_LUNMAP:
4205 retval = cctl_lunmap(fd, argc, argv, combinedopt);
4207 case CTLADM_CMD_READCAPACITY:
4208 retval = cctl_read_capacity(fd, lun, initid, retries,
4209 argc, argv, combinedopt);
4211 case CTLADM_CMD_MODESENSE:
4212 retval = cctl_mode_sense(fd, lun, initid, retries,
4213 argc, argv, combinedopt);
4215 case CTLADM_CMD_START:
4216 case CTLADM_CMD_STOP:
4217 retval = cctl_start_stop(fd, lun, initid, retries,
4218 (command == CTLADM_CMD_START) ? 1 : 0,
4219 argc, argv, combinedopt);
4221 case CTLADM_CMD_SYNC_CACHE:
4222 retval = cctl_sync_cache(fd, lun, initid, retries,
4223 argc, argv, combinedopt);
4225 case CTLADM_CMD_LUNLIST:
4226 retval = cctl_lunlist(fd);
4228 case CTLADM_CMD_DELAY:
4229 retval = cctl_delay(fd, lun, argc, argv, combinedopt);
4231 case CTLADM_CMD_ERR_INJECT:
4232 retval = cctl_error_inject(fd, lun, argc, argv,
4235 case CTLADM_CMD_DUMPOOA:
4236 retval = cctl_dump_ooa(fd, argc, argv);
4238 case CTLADM_CMD_DUMPSTRUCTS:
4239 retval = cctl_dump_structs(fd, cmdargs);
4241 case CTLADM_CMD_PRES_IN:
4242 retval = cctl_persistent_reserve_in(fd, lun, initid,
4243 argc, argv, combinedopt,
4246 case CTLADM_CMD_PRES_OUT:
4247 retval = cctl_persistent_reserve_out(fd, lun, initid,
4248 argc, argv, combinedopt,
4251 case CTLADM_CMD_INQ_VPD_DEVID:
4252 retval = cctl_inquiry_vpd_devid(fd, lun, initid);
4254 case CTLADM_CMD_RTPG:
4255 retval = cctl_report_target_port_group(fd, lun, initid);
4257 case CTLADM_CMD_MODIFY:
4258 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4260 case CTLADM_CMD_ISLIST:
4261 retval = cctl_islist(fd, argc, argv, combinedopt);
4263 case CTLADM_CMD_ISLOGOUT:
4264 retval = cctl_islogout(fd, argc, argv, combinedopt);
4266 case CTLADM_CMD_ISTERMINATE:
4267 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4269 case CTLADM_CMD_HELP: