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/queue.h>
64 #include <cam/scsi/scsi_all.h>
65 #include <cam/scsi/scsi_message.h>
66 #include <cam/ctl/ctl.h>
67 #include <cam/ctl/ctl_io.h>
68 #include <cam/ctl/ctl_backend.h>
69 #include <cam/ctl/ctl_ioctl.h>
70 #include <cam/ctl/ctl_util.h>
71 #include <cam/ctl/ctl_scsi_all.h>
79 #define min(x,y) (x < y) ? x : y
86 CTLADM_CMD_REPORT_LUNS,
96 CTLADM_CMD_READCAPACITY,
99 CTLADM_CMD_DUMPSTRUCTS,
102 CTLADM_CMD_SYNC_CACHE,
105 CTLADM_CMD_ERR_INJECT,
108 CTLADM_CMD_INQ_VPD_DEVID,
113 CTLADM_CMD_ISTERMINATE,
115 } ctladm_cmdfunction;
118 CTLADM_ARG_NONE = 0x0000000,
119 CTLADM_ARG_AUTOSENSE = 0x0000001,
120 CTLADM_ARG_DEVICE = 0x0000002,
121 CTLADM_ARG_ARRAYSIZE = 0x0000004,
122 CTLADM_ARG_BACKEND = 0x0000008,
123 CTLADM_ARG_CDBSIZE = 0x0000010,
124 CTLADM_ARG_DATALEN = 0x0000020,
125 CTLADM_ARG_FILENAME = 0x0000040,
126 CTLADM_ARG_LBA = 0x0000080,
127 CTLADM_ARG_PC = 0x0000100,
128 CTLADM_ARG_PAGE_CODE = 0x0000200,
129 CTLADM_ARG_PAGE_LIST = 0x0000400,
130 CTLADM_ARG_SUBPAGE = 0x0000800,
131 CTLADM_ARG_PAGELIST = 0x0001000,
132 CTLADM_ARG_DBD = 0x0002000,
133 CTLADM_ARG_TARG_LUN = 0x0004000,
134 CTLADM_ARG_BLOCKSIZE = 0x0008000,
135 CTLADM_ARG_IMMED = 0x0010000,
136 CTLADM_ARG_RELADR = 0x0020000,
137 CTLADM_ARG_RETRIES = 0x0040000,
138 CTLADM_ARG_ONOFFLINE = 0x0080000,
139 CTLADM_ARG_ONESHOT = 0x0100000,
140 CTLADM_ARG_TIMEOUT = 0x0200000,
141 CTLADM_ARG_INITIATOR = 0x0400000,
142 CTLADM_ARG_NOCOPY = 0x0800000,
143 CTLADM_ARG_NEED_TL = 0x1000000
149 ctladm_cmdargs argnum;
159 static const char rw_opts[] = "Nb:c:d:f:l:";
160 static const char startstop_opts[] = "i";
162 static struct ctladm_opts option_table[] = {
163 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
164 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
165 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
166 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
167 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
168 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
169 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
170 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
171 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
172 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
173 {"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"},
174 {"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"},
175 {"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"},
176 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
177 {"lunmap", CTLADM_CMD_LUNMAP, CTLADM_ARG_NONE, "p:l:L:"},
178 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
179 {"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:o:s:"},
180 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
181 {"portlist", CTLADM_CMD_PORTLIST, CTLADM_ARG_NONE, "f:ilp:qvx"},
182 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
183 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
184 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
185 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
186 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
187 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
188 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
189 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
190 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
191 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
192 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
193 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
194 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
195 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
196 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
201 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
202 ctladm_cmdargs *argnum, const char **subopt);
203 static int cctl_dump_ooa(int fd, int argc, char **argv);
204 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
205 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
206 static int cctl_delay(int fd, int lun, int argc, char **argv,
208 static int cctl_lunlist(int fd);
209 static int cctl_sync_cache(int fd, int lun, int iid, int retries,
210 int argc, char **argv, char *combinedopt);
211 static int cctl_start_stop(int fd, int lun, int iid, int retries,
212 int start, int argc, char **argv, char *combinedopt);
213 static int cctl_mode_sense(int fd, int lun, int iid, int retries,
214 int argc, char **argv, char *combinedopt);
215 static int cctl_read_capacity(int fd, int lun, int iid,
216 int retries, int argc, char **argv,
218 static int cctl_read_write(int fd, int lun, int iid, int retries,
219 int argc, char **argv, char *combinedopt,
220 ctladm_cmdfunction command);
221 static int cctl_get_luns(int fd, int lun, int iid, int retries,
222 struct scsi_report_luns_data **lun_data,
224 static int cctl_report_luns(int fd, int lun, int iid, int retries);
225 static int cctl_tur(int fd, int lun, int iid, int retries);
226 static int cctl_get_inquiry(int fd, int lun, int iid, int retries,
227 char *path_str, int path_len,
228 struct scsi_inquiry_data *inq_data);
229 static int cctl_inquiry(int fd, int lun, int iid, int retries);
230 static int cctl_req_sense(int fd, int lun, int iid, int retries);
231 static int cctl_persistent_reserve_in(int fd, int lun,
232 int initiator, int argc, char **argv,
233 char *combinedopt, int retry_count);
234 static int cctl_persistent_reserve_out(int fd, int lun,
235 int initiator, int argc, char **argv,
236 char *combinedopt, int retry_count);
237 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
238 static int cctl_inquiry_vpd_devid(int fd, int lun, int initiator);
239 static int cctl_report_target_port_group(int fd, int lun, int initiator);
240 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
241 static int cctl_portlist(int fd, int argc, char **argv, char *combinedopt);
244 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
245 ctladm_cmdargs *argnum, const char **subopt)
247 struct ctladm_opts *opts;
250 for (opts = table; (opts != NULL) && (opts->optname != NULL);
252 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
253 *cmdnum = opts->cmdnum;
254 *argnum = opts->argnum;
255 *subopt = opts->subopt;
257 if (strcmp(opts->optname, arg) == 0)
258 return (CC_OR_FOUND);
260 if (++num_matches > 1)
261 return(CC_OR_AMBIGUOUS);
268 return(CC_OR_NOT_FOUND);
272 cctl_dump_ooa(int fd, int argc, char **argv)
275 long double cmd_latency;
276 int num_entries, len, lun = -1, retval = 0;
281 if ((argc > 2) && (isdigit(argv[2][0])))
282 lun = strtol(argv[2], NULL, 0);
285 len = num_entries * sizeof(struct ctl_ooa_entry);
286 bzero(&ooa, sizeof(ooa));
287 ooa.entries = malloc(len);
288 if (ooa.entries == NULL) {
289 warn("%s: error mallocing %d bytes", __func__, len);
295 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
297 ooa.alloc_num = num_entries;
298 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
299 warn("%s: CTL_GET_OOA ioctl failed", __func__);
304 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
305 num_entries = num_entries * 2;
311 if (ooa.status != CTL_OOA_OK) {
312 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
318 fprintf(stdout, "Dumping OOA queues\n");
319 for (i = 0; i < ooa.fill_num; i++) {
320 struct ctl_ooa_entry *entry;
321 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
322 struct bintime delta_bt;
325 entry = &ooa.entries[i];
327 delta_bt = ooa.cur_bt;
328 bintime_sub(&delta_bt, &entry->start_bt);
329 bintime2timespec(&delta_bt, &ts);
330 cmd_latency = ts.tv_sec * 1000;
332 cmd_latency += ts.tv_nsec / 1000000;
334 fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
336 (intmax_t)entry->lun_num, entry->tag_num,
337 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
339 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
340 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
342 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
344 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
345 scsi_op_desc(entry->cdb[0], NULL),
346 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
349 fprintf(stdout, "OOA queues dump done\n");
357 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
359 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
374 static struct ctladm_opts cctl_fe_table[] = {
375 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
376 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
377 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
378 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
379 {"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
380 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
385 cctl_port(int fd, int argc, char **argv, char *combinedopt)
388 int32_t targ_port = -1;
390 int wwnn_set = 0, wwpn_set = 0;
391 uint64_t wwnn = 0, wwpn = 0;
392 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
393 struct ctl_port_entry entry;
394 ctl_port_type port_type = CTL_PORT_NONE;
395 int quiet = 0, xml = 0;
397 while ((c = getopt(argc, argv, combinedopt)) != -1) {
400 if (port_mode != CCTL_PORT_MODE_NONE)
403 port_mode = CCTL_PORT_MODE_LIST;
406 if (port_mode != CCTL_PORT_MODE_NONE)
409 if (strcasecmp(optarg, "on") == 0)
410 port_mode = CCTL_PORT_MODE_ON;
411 else if (strcasecmp(optarg, "off") == 0)
412 port_mode = CCTL_PORT_MODE_OFF;
414 warnx("Invalid -o argument %s, \"on\" or "
415 "\"off\" are the only valid args",
422 targ_port = strtol(optarg, NULL, 0);
428 ctladm_optret optret;
429 ctladm_cmdargs argnum;
431 ctl_port_type tmp_port_type;
433 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
435 if (optret == CC_OR_AMBIGUOUS) {
436 warnx("%s: ambiguous frontend type %s",
440 } else if (optret == CC_OR_NOT_FOUND) {
441 warnx("%s: invalid frontend type %s",
447 port_type |= tmp_port_type;
451 if ((port_mode != CCTL_PORT_MODE_NONE)
452 && (port_mode != CCTL_PORT_MODE_SET))
455 port_mode = CCTL_PORT_MODE_SET;
457 wwnn = strtoull(optarg, NULL, 0);
461 if ((port_mode != CCTL_PORT_MODE_NONE)
462 && (port_mode != CCTL_PORT_MODE_SET))
465 port_mode = CCTL_PORT_MODE_SET;
467 wwpn = strtoull(optarg, NULL, 0);
477 * The user can specify either one or more frontend types (-t), or
478 * a specific frontend, but not both.
480 * If the user didn't specify a frontend type or number, set it to
481 * all. This is primarily needed for the enable/disable ioctls.
482 * This will be a no-op for the listing code. For the set ioctl,
483 * we'll throw an error, since that only works on one port at a time.
485 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
486 warnx("%s: can only specify one of -t or -n", __func__);
489 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
490 port_type = CTL_PORT_ALL;
492 bzero(&entry, sizeof(entry));
495 * These are needed for all but list/dump mode.
497 entry.port_type = port_type;
498 entry.targ_port = targ_port;
501 case CCTL_PORT_MODE_LIST: {
511 argvx[argcx++] = argx;
513 argvx[argcx++] = argq;
514 cctl_portlist(fd, argcx, argvx, opts);
517 case CCTL_PORT_MODE_SET:
518 if (targ_port == -1) {
519 warnx("%s: -w and -W require -n", __func__);
525 entry.flags |= CTL_PORT_WWNN_VALID;
529 entry.flags |= CTL_PORT_WWPN_VALID;
533 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
534 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
539 case CCTL_PORT_MODE_ON:
540 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
541 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
545 fprintf(stdout, "Front End Ports enabled\n");
547 case CCTL_PORT_MODE_OFF:
548 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
549 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
553 fprintf(stdout, "Front End Ports disabled\n");
556 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
567 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
572 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
575 if (ioctl(fd, CTL_IO, io) == -1) {
576 warn("%s: error sending CTL_IO ioctl", func);
579 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
586 cctl_delay(int fd, int lun, int argc, char **argv,
589 struct ctl_io_delay_info delay_info;
590 char *delayloc = NULL;
591 char *delaytype = NULL;
598 memset(&delay_info, 0, sizeof(delay_info));
600 while ((c = getopt(argc, argv, combinedopt)) != -1) {
603 delaytype = strdup(optarg);
606 delayloc = strdup(optarg);
609 delaytime = strtoul(optarg, NULL, 0);
614 if (delaytime == -1) {
615 warnx("%s: you must specify the delaytime with -t", __func__);
620 if (strcasecmp(delayloc, "datamove") == 0)
621 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
622 else if (strcasecmp(delayloc, "done") == 0)
623 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
625 warnx("%s: invalid delay location %s", __func__, delayloc);
630 if ((delaytype == NULL)
631 || (strcmp(delaytype, "oneshot") == 0))
632 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
633 else if (strcmp(delaytype, "cont") == 0)
634 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
636 warnx("%s: invalid delay type %s", __func__, delaytype);
641 delay_info.lun_id = lun;
642 delay_info.delay_secs = delaytime;
644 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
645 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
649 switch (delay_info.status) {
650 case CTL_DELAY_STATUS_NONE:
651 warnx("%s: no delay status??", __func__);
654 case CTL_DELAY_STATUS_OK:
656 case CTL_DELAY_STATUS_INVALID_LUN:
657 warnx("%s: invalid lun %d", __func__, lun);
660 case CTL_DELAY_STATUS_INVALID_TYPE:
661 warnx("%s: invalid delay type %d", __func__,
662 delay_info.delay_type);
665 case CTL_DELAY_STATUS_INVALID_LOC:
666 warnx("%s: delay location %s not implemented?", __func__,
670 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
671 warnx("%s: delay not implemented in the kernel", __func__);
672 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
676 warnx("%s: unknown delay return status %d", __func__,
688 static struct ctladm_opts cctl_err_types[] = {
689 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
690 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
691 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
692 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
697 static struct ctladm_opts cctl_err_patterns[] = {
698 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
699 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
700 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
701 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
702 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
703 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
704 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
706 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
712 cctl_error_inject(int fd, uint32_t lun, int argc, char **argv,
716 struct ctl_error_desc err_desc;
719 uint64_t delete_id = 0;
720 int delete_id_set = 0;
726 bzero(&err_desc, sizeof(err_desc));
727 err_desc.lun_id = lun;
729 while ((c = getopt(argc, argv, combinedopt)) != -1) {
735 delete_id = strtoull(optarg, NULL, 0);
740 ctladm_optret optret;
741 ctladm_cmdargs argnum;
745 ctl_lun_error err_type;
747 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
748 warnx("%s: can't specify multiple -i "
749 "arguments", __func__);
753 optret = getoption(cctl_err_types, optarg,
754 &err_type, &argnum, &subopt);
755 err_desc.lun_error = err_type;
757 ctl_lun_error_pattern pattern;
759 optret = getoption(cctl_err_patterns, optarg,
760 &pattern, &argnum, &subopt);
761 err_desc.error_pattern |= pattern;
764 if (optret == CC_OR_AMBIGUOUS) {
765 warnx("%s: ambiguous argument %s", __func__,
769 } else if (optret == CC_OR_NOT_FOUND) {
770 warnx("%s: argument %s not found", __func__,
778 char *tmpstr, *tmpstr2;
780 tmpstr = strdup(optarg);
781 if (tmpstr == NULL) {
782 warn("%s: error duplicating string %s",
788 tmpstr2 = strsep(&tmpstr, ",");
789 if (tmpstr2 == NULL) {
790 warnx("%s: invalid -r argument %s", __func__,
796 lba = strtoull(tmpstr2, NULL, 0);
797 tmpstr2 = strsep(&tmpstr, ",");
798 if (tmpstr2 == NULL) {
799 warnx("%s: no len argument for -r lba,len, got"
800 " %s", __func__, optarg);
805 len = strtoul(tmpstr2, NULL, 0);
810 struct get_hook hook;
813 sense_len = strtol(optarg, NULL, 0);
814 if (sense_len <= 0) {
815 warnx("invalid number of sense bytes %d",
821 sense_len = MIN(sense_len, SSD_FULL_SIZE);
823 hook.argc = argc - optind;
824 hook.argv = argv + optind;
827 sensestr = cget(&hook, NULL);
828 if ((sensestr != NULL)
829 && (sensestr[0] == '-')) {
833 (uint8_t *)&err_desc.custom_sense,
834 sense_len, sensestr, iget, &hook);
844 if (delete_id_set != 0) {
845 err_desc.serial = delete_id;
846 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
847 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
854 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
855 warnx("%s: error injection command (-i) needed",
859 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
860 && (sense_len == 0)) {
861 warnx("%s: custom error requires -s", __func__);
867 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
870 * If fd_sense is set, we need to read the sense data the user
871 * wants returned from stdin.
875 int amt_to_read = sense_len;
876 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
878 for (amt_read = 0; amt_to_read > 0;
879 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
880 if (amt_read == -1) {
881 warn("error reading sense data from stdin");
885 amt_to_read -= amt_read;
890 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
891 warnx("%s: command pattern (-p) needed", __func__);
897 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
899 * We could check here to see whether it's a read/write
900 * command, but that will be pointless once we allow
901 * custom patterns. At that point, the user could specify
902 * a READ(6) CDB type, and we wouldn't have an easy way here
903 * to verify whether range checking is possible there. The
904 * user will just figure it out when his error never gets
908 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
909 warnx("%s: need read and/or write pattern if range "
910 "is specified", __func__);
915 err_desc.lba_range.lba = lba;
916 err_desc.lba_range.len = len;
919 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
920 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
923 printf("Error injection succeeded, serial number is %ju\n",
924 (uintmax_t)err_desc.serial);
934 struct scsi_report_luns_data *lun_data;
935 struct scsi_inquiry_data *inq_data;
945 * XXX KDM assuming LUN 0 is fine, but we may need to change this
946 * if we ever acquire the ability to have multiple targets.
948 if ((retval = cctl_get_luns(fd, /*lun*/ 0, initid,
949 /*retries*/ 2, &lun_data, &num_luns)) != 0)
952 inq_data = malloc(sizeof(*inq_data));
953 if (inq_data == NULL) {
954 warn("%s: couldn't allocate memory for inquiry data\n",
959 for (i = 0; i < num_luns; i++) {
963 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
964 case RPL_LUNDATA_ATYP_PERIPH:
965 lun_val = lun_data->luns[i].lundata[1];
967 case RPL_LUNDATA_ATYP_FLAT:
968 lun_val = (lun_data->luns[i].lundata[0] &
969 RPL_LUNDATA_FLAT_LUN_MASK) |
970 (lun_data->luns[i].lundata[1] <<
971 RPL_LUNDATA_FLAT_LUN_BITS);
973 case RPL_LUNDATA_ATYP_LUN:
974 case RPL_LUNDATA_ATYP_EXTLUN:
976 fprintf(stdout, "Unsupported LUN format %d\n",
977 lun_data->luns[i].lundata[0] &
978 RPL_LUNDATA_ATYP_MASK);
985 if ((retval = cctl_get_inquiry(fd, lun_val, initid,
986 /*retries*/ 2, scsi_path,
991 printf("%s", scsi_path);
992 scsi_print_inquiry(inq_data);
996 if (lun_data != NULL)
999 if (inq_data != NULL)
1006 cctl_sync_cache(int fd, int lun, int iid, int retries,
1007 int argc, char **argv, char *combinedopt)
1012 uint64_t our_lba = 0;
1013 uint32_t our_block_count = 0;
1014 int reladr = 0, immed = 0;
1019 io = ctl_scsi_alloc_io(iid);
1021 warnx("%s: can't allocate memory", __func__);
1025 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1028 our_block_count = strtoul(optarg, NULL, 0);
1031 cdb_size = strtol(optarg, NULL, 0);
1037 our_lba = strtoull(optarg, NULL, 0);
1047 if (cdb_size != -1) {
1053 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1054 "and 16", __func__, cdb_size);
1057 break; /* NOTREACHED */
1062 ctl_scsi_sync_cache(/*io*/ io,
1065 /*minimum_cdb_size*/ cdb_size,
1066 /*starting_lba*/ our_lba,
1067 /*block_count*/ our_block_count,
1068 /*tag_type*/ CTL_TAG_SIMPLE,
1071 io->io_hdr.nexus.targ_lun = lun;
1072 io->io_hdr.nexus.initid = iid;
1074 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1079 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1080 fprintf(stdout, "Cache synchronized successfully\n");
1082 ctl_io_error_print(io, NULL, stderr);
1084 ctl_scsi_free_io(io);
1090 cctl_start_stop(int fd, int lun, int iid, int retries, int start,
1091 int argc, char **argv, char *combinedopt)
1100 io = ctl_scsi_alloc_io(iid);
1102 warnx("%s: can't allocate memory", __func__);
1106 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1116 * Use an ordered tag for the stop command, to guarantee that any
1117 * pending I/O will finish before the stop command executes. This
1118 * would normally be the case anyway, since CTL will basically
1119 * treat the start/stop command as an ordered command with respect
1120 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1122 ctl_scsi_start_stop(/*io*/ io,
1125 /*immediate*/ immed,
1126 /*power_conditions*/ SSS_PC_START_VALID,
1127 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1131 io->io_hdr.nexus.targ_lun = lun;
1132 io->io_hdr.nexus.initid = iid;
1134 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1139 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1140 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1141 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1142 (start) ? "started" : "stopped");
1144 ctl_io_error_print(io, NULL, stderr);
1147 ctl_scsi_free_io(io);
1153 cctl_mode_sense(int fd, int lun, int iid, int retries,
1154 int argc, char **argv, char *combinedopt)
1159 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1168 io = ctl_scsi_alloc_io(iid);
1170 warn("%s: can't allocate memory", __func__);
1174 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1177 pc = strtoul(optarg, NULL, 0);
1180 subpage = strtoul(optarg, NULL, 0);
1189 page_code = strtoul(optarg, NULL, 0);
1192 cdbsize = strtol(optarg, NULL, 0);
1199 if (((list == 0) && (page_code == -1))
1200 || ((list != 0) && (page_code != -1))) {
1201 warnx("%s: you must specify either a page code (-m) or -l",
1207 if ((page_code != -1)
1208 && ((page_code > SMS_ALL_PAGES_PAGE)
1209 || (page_code < 0))) {
1210 warnx("%s: page code %d is out of range", __func__,
1217 page_code = SMS_ALL_PAGES_PAGE;
1219 warnx("%s: arg -P makes no sense with -l",
1224 if (subpage != -1) {
1225 warnx("%s: arg -S makes no sense with -l", __func__);
1232 pc = SMS_PAGE_CTRL_CURRENT;
1236 warnx("%s: page control value %d is out of range: 0-3",
1246 || (subpage < 0))) {
1247 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1258 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1259 "and 10", __func__, cdbsize);
1275 dataptr = (uint8_t *)malloc(datalen);
1276 if (dataptr == NULL) {
1277 warn("%s: can't allocate %d bytes", __func__, datalen);
1282 memset(dataptr, 0, datalen);
1284 ctl_scsi_mode_sense(io,
1285 /*data_ptr*/ dataptr,
1286 /*data_len*/ datalen,
1289 /*page_code*/ page_code,
1291 /*subpage*/ subpage,
1292 /*minimum_cdb_size*/ cdbsize,
1293 /*tag_type*/ CTL_TAG_SIMPLE,
1296 io->io_hdr.nexus.targ_lun = lun;
1297 io->io_hdr.nexus.initid = iid;
1299 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1304 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1305 int pages_len, used_len;
1306 uint32_t returned_len;
1309 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1310 struct scsi_mode_hdr_6 *hdr6;
1313 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1315 returned_len = hdr6->datalen + 1;
1316 bdlen = hdr6->block_descr_len;
1318 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1320 struct scsi_mode_hdr_10 *hdr10;
1323 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
1325 returned_len = scsi_2btoul(hdr10->datalen) + 2;
1326 bdlen = scsi_2btoul(hdr10->block_descr_len);
1328 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
1330 /* just in case they can give us more than we allocated for */
1331 returned_len = min(returned_len, datalen);
1332 pages_len = returned_len - (ndataptr - dataptr);
1334 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
1335 returned_len, pages_len);
1338 fprintf(stdout, "Supported mode pages:\n");
1339 for (used_len = 0; used_len < pages_len;) {
1340 struct scsi_mode_page_header *header;
1342 header = (struct scsi_mode_page_header *)
1343 &ndataptr[used_len];
1344 fprintf(stdout, "%d\n", header->page_code);
1345 used_len += header->page_length + 2;
1348 for (used_len = 0; used_len < pages_len; used_len++) {
1349 fprintf(stdout, "0x%x ", ndataptr[used_len]);
1350 if (((used_len+1) % 16) == 0)
1351 fprintf(stdout, "\n");
1353 fprintf(stdout, "\n");
1356 ctl_io_error_print(io, NULL, stderr);
1359 ctl_scsi_free_io(io);
1361 if (dataptr != NULL)
1368 cctl_read_capacity(int fd, int lun, int iid, int retries,
1369 int argc, char **argv, char *combinedopt)
1372 struct scsi_read_capacity_data *data;
1373 struct scsi_read_capacity_data_long *longdata;
1374 int cdbsize = -1, retval;
1382 io = ctl_scsi_alloc_io(iid);
1384 warn("%s: can't allocate memory\n", __func__);
1388 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1391 cdbsize = strtol(optarg, NULL, 0);
1397 if (cdbsize != -1) {
1403 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1404 "and 16", __func__, cdbsize);
1407 break; /* NOTREACHED */
1412 dataptr = (uint8_t *)malloc(sizeof(*longdata));
1413 if (dataptr == NULL) {
1414 warn("%s: can't allocate %zd bytes\n", __func__,
1419 memset(dataptr, 0, sizeof(*longdata));
1425 ctl_scsi_read_capacity(io,
1426 /*data_ptr*/ dataptr,
1427 /*data_len*/ sizeof(*longdata),
1431 /*tag_type*/ CTL_TAG_SIMPLE,
1435 ctl_scsi_read_capacity_16(io,
1436 /*data_ptr*/ dataptr,
1437 /*data_len*/ sizeof(*longdata),
1441 /*tag_type*/ CTL_TAG_SIMPLE,
1446 io->io_hdr.nexus.initid = iid;
1447 io->io_hdr.nexus.targ_lun = lun;
1449 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1454 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1458 if (cdbsize == 10) {
1460 data = (struct scsi_read_capacity_data *)dataptr;
1462 maxlba = scsi_4btoul(data->addr);
1463 blocksize = scsi_4btoul(data->length);
1465 if (maxlba == 0xffffffff) {
1470 longdata=(struct scsi_read_capacity_data_long *)dataptr;
1472 maxlba = scsi_8btou64(longdata->addr);
1473 blocksize = scsi_4btoul(longdata->length);
1476 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
1477 (uintmax_t)maxlba, blocksize);
1479 ctl_io_error_print(io, NULL, stderr);
1482 ctl_scsi_free_io(io);
1484 if (dataptr != NULL)
1491 cctl_read_write(int fd, int lun, int iid, int retries,
1492 int argc, char **argv, char *combinedopt,
1493 ctladm_cmdfunction command)
1496 int file_fd, do_stdio;
1497 int cdbsize = -1, databytes;
1499 char *filename = NULL;
1500 int datalen = -1, blocksize = -1;
1511 io = ctl_scsi_alloc_io(iid);
1513 warn("%s: can't allocate memory\n", __func__);
1517 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1520 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
1523 blocksize = strtoul(optarg, NULL, 0);
1526 cdbsize = strtoul(optarg, NULL, 0);
1529 datalen = strtoul(optarg, NULL, 0);
1532 filename = strdup(optarg);
1535 lba = strtoull(optarg, NULL, 0);
1542 if (filename == NULL) {
1543 warnx("%s: you must supply a filename using -f", __func__);
1548 if (datalen == -1) {
1549 warnx("%s: you must specify the data length with -d", __func__);
1555 warnx("%s: you must specify the LBA with -l", __func__);
1560 if (blocksize == -1) {
1561 warnx("%s: you must specify the blocksize with -b", __func__);
1566 if (cdbsize != -1) {
1574 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
1575 "10, 12 or 16", __func__, cdbsize);
1578 break; /* NOTREACHED */
1583 databytes = datalen * blocksize;
1584 dataptr = (uint8_t *)malloc(databytes);
1586 if (dataptr == NULL) {
1587 warn("%s: can't allocate %d bytes\n", __func__, databytes);
1591 if (strcmp(filename, "-") == 0) {
1592 if (command == CTLADM_CMD_READ)
1593 file_fd = STDOUT_FILENO;
1595 file_fd = STDIN_FILENO;
1598 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1599 if (file_fd == -1) {
1600 warn("%s: can't open file %s", __func__, filename);
1606 memset(dataptr, 0, databytes);
1608 if (command == CTLADM_CMD_WRITE) {
1611 bytes_read = read(file_fd, dataptr, databytes);
1612 if (bytes_read == -1) {
1613 warn("%s: error reading file %s", __func__, filename);
1617 if (bytes_read != databytes) {
1618 warnx("%s: only read %d bytes from file %s",
1619 __func__, bytes_read, filename);
1624 ctl_scsi_read_write(io,
1625 /*data_ptr*/ dataptr,
1626 /*data_len*/ databytes,
1627 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
1629 /*minimum_cdb_size*/ cdbsize,
1631 /*num_blocks*/ datalen,
1632 /*tag_type*/ CTL_TAG_SIMPLE,
1635 io->io_hdr.nexus.targ_lun = lun;
1636 io->io_hdr.nexus.initid = iid;
1638 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1643 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1644 && (command == CTLADM_CMD_READ)) {
1647 bytes_written = write(file_fd, dataptr, databytes);
1648 if (bytes_written == -1) {
1649 warn("%s: can't write to %s", __func__, filename);
1652 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1653 ctl_io_error_print(io, NULL, stderr);
1658 ctl_scsi_free_io(io);
1660 if (dataptr != NULL)
1671 cctl_get_luns(int fd, int lun, int iid, int retries, struct
1672 scsi_report_luns_data **lun_data, uint32_t *num_luns)
1681 io = ctl_scsi_alloc_io(iid);
1683 warnx("%s: can't allocate memory", __func__);
1688 * lun_data includes space for 1 lun, allocate space for 4 initially.
1689 * If that isn't enough, we'll allocate more.
1693 lun_datalen = sizeof(*lun_data) +
1694 (nluns * sizeof(struct scsi_report_luns_lundata));
1695 *lun_data = malloc(lun_datalen);
1697 if (*lun_data == NULL) {
1698 warnx("%s: can't allocate memory", __func__);
1699 ctl_scsi_free_io(io);
1703 ctl_scsi_report_luns(io,
1704 /*data_ptr*/ (uint8_t *)*lun_data,
1705 /*data_len*/ lun_datalen,
1706 /*select_report*/ RPL_REPORT_ALL,
1707 /*tag_type*/ CTL_TAG_SIMPLE,
1710 io->io_hdr.nexus.initid = iid;
1711 io->io_hdr.nexus.targ_lun = lun;
1713 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1718 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1719 uint32_t returned_len, returned_luns;
1721 returned_len = scsi_4btoul((*lun_data)->length);
1722 returned_luns = returned_len / 8;
1723 if (returned_luns > nluns) {
1724 nluns = returned_luns;
1728 /* These should be the same */
1729 *num_luns = MIN(returned_luns, nluns);
1731 ctl_io_error_print(io, NULL, stderr);
1735 ctl_scsi_free_io(io);
1741 cctl_report_luns(int fd, int lun, int iid, int retries)
1743 struct scsi_report_luns_data *lun_data;
1744 uint32_t num_luns, i;
1749 if ((retval = cctl_get_luns(fd, lun, iid, retries, &lun_data,
1753 fprintf(stdout, "%u LUNs returned\n", num_luns);
1754 for (i = 0; i < num_luns; i++) {
1758 * XXX KDM figure out a way to share this code with
1761 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1762 case RPL_LUNDATA_ATYP_PERIPH:
1763 lun_val = lun_data->luns[i].lundata[1];
1765 case RPL_LUNDATA_ATYP_FLAT:
1766 lun_val = (lun_data->luns[i].lundata[0] &
1767 RPL_LUNDATA_FLAT_LUN_MASK) |
1768 (lun_data->luns[i].lundata[1] <<
1769 RPL_LUNDATA_FLAT_LUN_BITS);
1771 case RPL_LUNDATA_ATYP_LUN:
1772 case RPL_LUNDATA_ATYP_EXTLUN:
1774 fprintf(stdout, "Unsupported LUN format %d\n",
1775 lun_data->luns[i].lundata[0] &
1776 RPL_LUNDATA_ATYP_MASK);
1783 fprintf(stdout, "%d\n", lun_val);
1787 if (lun_data != NULL)
1794 cctl_tur(int fd, int lun, int iid, int retries)
1798 io = ctl_scsi_alloc_io(iid);
1800 fprintf(stderr, "can't allocate memory\n");
1805 /* tag_type */ CTL_TAG_SIMPLE,
1808 io->io_hdr.nexus.targ_lun = lun;
1809 io->io_hdr.nexus.initid = iid;
1811 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1812 ctl_scsi_free_io(io);
1816 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1817 fprintf(stdout, "Unit is ready\n");
1819 ctl_io_error_print(io, NULL, stderr);
1825 cctl_get_inquiry(int fd, int lun, int iid, int retries,
1826 char *path_str, int path_len,
1827 struct scsi_inquiry_data *inq_data)
1834 io = ctl_scsi_alloc_io(iid);
1836 warnx("cctl_inquiry: can't allocate memory\n");
1840 ctl_scsi_inquiry(/*io*/ io,
1841 /*data_ptr*/ (uint8_t *)inq_data,
1842 /*data_len*/ sizeof(*inq_data),
1845 /*tag_type*/ CTL_TAG_SIMPLE,
1848 io->io_hdr.nexus.targ_lun = lun;
1849 io->io_hdr.nexus.initid = iid;
1851 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1856 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
1858 ctl_io_error_print(io, NULL, stderr);
1859 } else if (path_str != NULL)
1860 ctl_scsi_path_string(io, path_str, path_len);
1863 ctl_scsi_free_io(io);
1869 cctl_inquiry(int fd, int lun, int iid, int retries)
1871 struct scsi_inquiry_data *inq_data;
1875 inq_data = malloc(sizeof(*inq_data));
1876 if (inq_data == NULL) {
1877 warnx("%s: can't allocate inquiry data", __func__);
1882 if ((retval = cctl_get_inquiry(fd, lun, iid, retries, scsi_path,
1883 sizeof(scsi_path), inq_data)) != 0)
1886 printf("%s", scsi_path);
1887 scsi_print_inquiry(inq_data);
1890 if (inq_data != NULL)
1897 cctl_req_sense(int fd, int lun, int iid, int retries)
1900 struct scsi_sense_data *sense_data;
1905 io = ctl_scsi_alloc_io(iid);
1907 warnx("cctl_req_sense: can't allocate memory\n");
1910 sense_data = malloc(sizeof(*sense_data));
1911 memset(sense_data, 0, sizeof(*sense_data));
1913 ctl_scsi_request_sense(/*io*/ io,
1914 /*data_ptr*/ (uint8_t *)sense_data,
1915 /*data_len*/ sizeof(*sense_data),
1917 /*tag_type*/ CTL_TAG_SIMPLE,
1920 io->io_hdr.nexus.targ_lun = lun;
1921 io->io_hdr.nexus.initid = iid;
1923 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1928 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1929 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
1930 io->scsiio.sense_len = sizeof(*sense_data);
1931 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
1933 ctl_io_error_print(io, NULL, stderr);
1937 ctl_scsi_free_io(io);
1944 cctl_report_target_port_group(int fd, int lun, int iid)
1954 io = ctl_scsi_alloc_io(iid);
1956 warn("%s: can't allocate memory", __func__);
1961 dataptr = (uint8_t *)malloc(datalen);
1962 if (dataptr == NULL) {
1963 warn("%s: can't allocate %d bytes", __func__, datalen);
1968 memset(dataptr, 0, datalen);
1970 ctl_scsi_maintenance_in(/*io*/ io,
1971 /*data_ptr*/ dataptr,
1972 /*data_len*/ datalen,
1973 /*action*/ SA_RPRT_TRGT_GRP,
1974 /*tag_type*/ CTL_TAG_SIMPLE,
1977 io->io_hdr.nexus.targ_lun = lun;
1978 io->io_hdr.nexus.initid = iid;
1980 if (cctl_do_io(fd, 0, io, __func__) != 0) {
1985 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1986 int returned_len, used_len;
1988 returned_len = scsi_4btoul(&dataptr[0]) + 4;
1990 for (used_len = 0; used_len < returned_len; used_len++) {
1991 fprintf(stdout, "0x%02x ", dataptr[used_len]);
1992 if (((used_len+1) % 8) == 0)
1993 fprintf(stdout, "\n");
1995 fprintf(stdout, "\n");
1997 ctl_io_error_print(io, NULL, stderr);
2000 ctl_scsi_free_io(io);
2002 if (dataptr != NULL)
2009 cctl_inquiry_vpd_devid(int fd, int lun, int iid)
2019 io = ctl_scsi_alloc_io(iid);
2021 warn("%s: can't allocate memory", __func__);
2026 dataptr = (uint8_t *)malloc(datalen);
2027 if (dataptr == NULL) {
2028 warn("%s: can't allocate %d bytes", __func__, datalen);
2033 memset(dataptr, 0, datalen);
2035 ctl_scsi_inquiry(/*io*/ io,
2036 /*data_ptr*/ dataptr,
2037 /*data_len*/ datalen,
2039 /*page_code*/ SVPD_DEVICE_ID,
2040 /*tag_type*/ CTL_TAG_SIMPLE,
2043 io->io_hdr.nexus.targ_lun = lun;
2044 io->io_hdr.nexus.initid = iid;
2046 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2051 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2052 int returned_len, used_len;
2054 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2056 for (used_len = 0; used_len < returned_len; used_len++) {
2057 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2058 if (((used_len+1) % 8) == 0)
2059 fprintf(stdout, "\n");
2061 fprintf(stdout, "\n");
2063 ctl_io_error_print(io, NULL, stderr);
2066 ctl_scsi_free_io(io);
2068 if (dataptr != NULL)
2075 cctl_persistent_reserve_in(int fd, int lun, int iid,
2076 int argc, char **argv, char *combinedopt,
2089 io = ctl_scsi_alloc_io(iid);
2091 warn("%s: can't allocate memory", __func__);
2095 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2098 action = strtol(optarg, NULL, 0);
2105 if (action < 0 || action > 2) {
2106 warn("action must be specified and in the range: 0-2");
2113 dataptr = (uint8_t *)malloc(datalen);
2114 if (dataptr == NULL) {
2115 warn("%s: can't allocate %d bytes", __func__, datalen);
2120 memset(dataptr, 0, datalen);
2122 ctl_scsi_persistent_res_in(io,
2123 /*data_ptr*/ dataptr,
2124 /*data_len*/ datalen,
2126 /*tag_type*/ CTL_TAG_SIMPLE,
2129 io->io_hdr.nexus.targ_lun = lun;
2130 io->io_hdr.nexus.initid = iid;
2132 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2137 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2138 int returned_len, used_len;
2142 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2143 returned_len = min(returned_len, 256);
2146 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2152 warnx("%s: invalid action %d", __func__, action);
2154 break; /* NOTREACHED */
2157 for (used_len = 0; used_len < returned_len; used_len++) {
2158 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2159 if (((used_len+1) % 8) == 0)
2160 fprintf(stdout, "\n");
2162 fprintf(stdout, "\n");
2164 ctl_io_error_print(io, NULL, stderr);
2167 ctl_scsi_free_io(io);
2169 if (dataptr != NULL)
2176 cctl_persistent_reserve_out(int fd, int lun, int iid,
2177 int argc, char **argv, char *combinedopt,
2182 uint64_t key = 0, sa_key = 0;
2183 int action = -1, restype = -1;
2191 io = ctl_scsi_alloc_io(iid);
2193 warn("%s: can't allocate memory", __func__);
2197 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2200 action = strtol(optarg, NULL, 0);
2203 key = strtoull(optarg, NULL, 0);
2206 restype = strtol(optarg, NULL, 0);
2209 sa_key = strtoull(optarg, NULL, 0);
2215 if (action < 0 || action > 5) {
2216 warn("action must be specified and in the range: 0-5");
2221 if (restype < 0 || restype > 5) {
2222 if (action != 0 && action != 5 && action != 3) {
2223 warn("'restype' must specified and in the range: 0-5");
2230 dataptr = (uint8_t *)malloc(datalen);
2231 if (dataptr == NULL) {
2232 warn("%s: can't allocate %d bytes", __func__, datalen);
2237 memset(dataptr, 0, datalen);
2239 ctl_scsi_persistent_res_out(io,
2240 /*data_ptr*/ dataptr,
2241 /*data_len*/ datalen,
2246 /*tag_type*/ CTL_TAG_SIMPLE,
2249 io->io_hdr.nexus.targ_lun = lun;
2250 io->io_hdr.nexus.initid = iid;
2252 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2256 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2258 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2259 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2260 "successfully\n", scsi_path);
2262 ctl_io_error_print(io, NULL, stderr);
2265 ctl_scsi_free_io(io);
2267 if (dataptr != NULL)
2273 struct cctl_req_option {
2278 STAILQ_ENTRY(cctl_req_option) links;
2282 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2284 struct ctl_lun_req req;
2285 int device_type = -1;
2286 uint64_t lun_size = 0;
2287 uint32_t blocksize = 0, req_lun_id = 0;
2288 char *serial_num = NULL;
2289 char *device_id = NULL;
2290 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
2291 char *backend_name = NULL;
2292 STAILQ_HEAD(, cctl_req_option) option_list;
2293 int num_options = 0;
2296 STAILQ_INIT(&option_list);
2298 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2301 backend_name = strdup(optarg);
2304 blocksize = strtoul(optarg, NULL, 0);
2308 device_id = strdup(optarg);
2311 req_lun_id = strtoul(optarg, NULL, 0);
2315 struct cctl_req_option *option;
2319 tmpstr = strdup(optarg);
2320 name = strsep(&tmpstr, "=");
2322 warnx("%s: option -o takes \"name=value\""
2323 "argument", __func__);
2327 value = strsep(&tmpstr, "=");
2328 if (value == NULL) {
2329 warnx("%s: option -o takes \"name=value\""
2330 "argument", __func__);
2334 option = malloc(sizeof(*option));
2335 if (option == NULL) {
2336 warn("%s: error allocating %zd bytes",
2337 __func__, sizeof(*option));
2341 option->name = strdup(name);
2342 option->namelen = strlen(name) + 1;
2343 option->value = strdup(value);
2344 option->vallen = strlen(value) + 1;
2347 STAILQ_INSERT_TAIL(&option_list, option, links);
2352 if (strcasecmp(optarg, "auto") != 0) {
2353 retval = expand_number(optarg, &lun_size);
2355 warn("%s: invalid -s argument",
2364 serial_num = strdup(optarg);
2367 device_type = strtoul(optarg, NULL, 0);
2374 if (backend_name == NULL) {
2375 warnx("%s: backend name (-b) must be specified", __func__);
2380 bzero(&req, sizeof(req));
2382 strlcpy(req.backend, backend_name, sizeof(req.backend));
2383 req.reqtype = CTL_LUNREQ_CREATE;
2385 if (blocksize_set != 0)
2386 req.reqdata.create.blocksize_bytes = blocksize;
2388 if (lun_size_set != 0)
2389 req.reqdata.create.lun_size_bytes = lun_size;
2391 if (lun_id_set != 0) {
2392 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
2393 req.reqdata.create.req_lun_id = req_lun_id;
2396 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
2398 if (device_type != -1)
2399 req.reqdata.create.device_type = device_type;
2401 req.reqdata.create.device_type = T_DIRECT;
2403 if (serial_num != NULL) {
2404 strlcpy(req.reqdata.create.serial_num, serial_num,
2405 sizeof(req.reqdata.create.serial_num));
2406 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
2409 if (device_id != NULL) {
2410 strlcpy(req.reqdata.create.device_id, device_id,
2411 sizeof(req.reqdata.create.device_id));
2412 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
2415 req.num_be_args = num_options;
2416 if (num_options > 0) {
2417 struct cctl_req_option *option, *next_option;
2420 req.be_args = malloc(num_options * sizeof(*req.be_args));
2421 if (req.be_args == NULL) {
2422 warn("%s: error allocating %zd bytes", __func__,
2423 num_options * sizeof(*req.be_args));
2428 for (i = 0, option = STAILQ_FIRST(&option_list);
2429 i < num_options; i++, option = next_option) {
2430 next_option = STAILQ_NEXT(option, links);
2432 req.be_args[i].namelen = option->namelen;
2433 req.be_args[i].name = strdup(option->name);
2434 req.be_args[i].vallen = option->vallen;
2435 req.be_args[i].value = strdup(option->value);
2437 * XXX KDM do we want a way to specify a writeable
2438 * flag of some sort? Do we want a way to specify
2441 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
2443 STAILQ_REMOVE(&option_list, option, cctl_req_option,
2446 free(option->value);
2451 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
2452 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2457 switch (req.status) {
2459 warnx("LUN creation error: %s", req.error_str);
2462 case CTL_LUN_WARNING:
2463 warnx("LUN creation warning: %s", req.error_str);
2468 warnx("unknown LUN creation status: %d", req.status);
2473 fprintf(stdout, "LUN created successfully\n");
2474 fprintf(stdout, "backend: %s\n", req.backend);
2475 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
2476 fprintf(stdout, "LUN size: %ju bytes\n",
2477 (uintmax_t)req.reqdata.create.lun_size_bytes);
2478 fprintf(stdout, "blocksize %u bytes\n",
2479 req.reqdata.create.blocksize_bytes);
2480 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
2481 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
2482 fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
2489 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
2491 struct ctl_lun_req req;
2492 uint32_t lun_id = 0;
2494 char *backend_name = NULL;
2495 STAILQ_HEAD(, cctl_req_option) option_list;
2496 int num_options = 0;
2499 STAILQ_INIT(&option_list);
2501 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2504 backend_name = strdup(optarg);
2507 lun_id = strtoul(optarg, NULL, 0);
2511 struct cctl_req_option *option;
2515 tmpstr = strdup(optarg);
2516 name = strsep(&tmpstr, "=");
2518 warnx("%s: option -o takes \"name=value\""
2519 "argument", __func__);
2523 value = strsep(&tmpstr, "=");
2524 if (value == NULL) {
2525 warnx("%s: option -o takes \"name=value\""
2526 "argument", __func__);
2530 option = malloc(sizeof(*option));
2531 if (option == NULL) {
2532 warn("%s: error allocating %zd bytes",
2533 __func__, sizeof(*option));
2537 option->name = strdup(name);
2538 option->namelen = strlen(name) + 1;
2539 option->value = strdup(value);
2540 option->vallen = strlen(value) + 1;
2543 STAILQ_INSERT_TAIL(&option_list, option, links);
2552 if (backend_name == NULL)
2553 errx(1, "%s: backend name (-b) must be specified", __func__);
2555 if (lun_id_set == 0)
2556 errx(1, "%s: LUN id (-l) must be specified", __func__);
2558 bzero(&req, sizeof(req));
2560 strlcpy(req.backend, backend_name, sizeof(req.backend));
2561 req.reqtype = CTL_LUNREQ_RM;
2563 req.reqdata.rm.lun_id = lun_id;
2565 req.num_be_args = num_options;
2566 if (num_options > 0) {
2567 struct cctl_req_option *option, *next_option;
2570 req.be_args = malloc(num_options * sizeof(*req.be_args));
2571 if (req.be_args == NULL) {
2572 warn("%s: error allocating %zd bytes", __func__,
2573 num_options * sizeof(*req.be_args));
2578 for (i = 0, option = STAILQ_FIRST(&option_list);
2579 i < num_options; i++, option = next_option) {
2580 next_option = STAILQ_NEXT(option, links);
2582 req.be_args[i].namelen = option->namelen;
2583 req.be_args[i].name = strdup(option->name);
2584 req.be_args[i].vallen = option->vallen;
2585 req.be_args[i].value = strdup(option->value);
2587 * XXX KDM do we want a way to specify a writeable
2588 * flag of some sort? Do we want a way to specify
2591 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
2593 STAILQ_REMOVE(&option_list, option, cctl_req_option,
2596 free(option->value);
2601 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
2602 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2607 switch (req.status) {
2609 warnx("LUN removal error: %s", req.error_str);
2612 case CTL_LUN_WARNING:
2613 warnx("LUN removal warning: %s", req.error_str);
2618 warnx("unknown LUN removal status: %d", req.status);
2623 printf("LUN %d removed successfully\n", lun_id);
2630 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
2632 struct ctl_lun_req req;
2633 uint64_t lun_size = 0;
2634 uint32_t lun_id = 0;
2635 int lun_id_set = 0, lun_size_set = 0;
2636 char *backend_name = NULL;
2637 STAILQ_HEAD(, cctl_req_option) option_list;
2638 int num_options = 0;
2641 STAILQ_INIT(&option_list);
2642 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2645 backend_name = strdup(optarg);
2648 lun_id = strtoul(optarg, NULL, 0);
2652 struct cctl_req_option *option;
2656 tmpstr = strdup(optarg);
2657 name = strsep(&tmpstr, "=");
2659 warnx("%s: option -o takes \"name=value\""
2660 "argument", __func__);
2664 value = strsep(&tmpstr, "=");
2665 if (value == NULL) {
2666 warnx("%s: option -o takes \"name=value\""
2667 "argument", __func__);
2671 option = malloc(sizeof(*option));
2672 if (option == NULL) {
2673 warn("%s: error allocating %zd bytes",
2674 __func__, sizeof(*option));
2678 option->name = strdup(name);
2679 option->namelen = strlen(name) + 1;
2680 option->value = strdup(value);
2681 option->vallen = strlen(value) + 1;
2684 STAILQ_INSERT_TAIL(&option_list, option, links);
2689 if (strcasecmp(optarg, "auto") != 0) {
2690 retval = expand_number(optarg, &lun_size);
2692 warn("%s: invalid -s argument",
2705 if (backend_name == NULL)
2706 errx(1, "%s: backend name (-b) must be specified", __func__);
2708 if (lun_id_set == 0)
2709 errx(1, "%s: LUN id (-l) must be specified", __func__);
2711 if (lun_size_set == 0 && num_options == 0)
2712 errx(1, "%s: size (-s) or options (-o) must be specified",
2715 bzero(&req, sizeof(req));
2717 strlcpy(req.backend, backend_name, sizeof(req.backend));
2718 req.reqtype = CTL_LUNREQ_MODIFY;
2720 req.reqdata.modify.lun_id = lun_id;
2721 req.reqdata.modify.lun_size_bytes = lun_size;
2723 req.num_be_args = num_options;
2724 if (num_options > 0) {
2725 struct cctl_req_option *option, *next_option;
2728 req.be_args = malloc(num_options * sizeof(*req.be_args));
2729 if (req.be_args == NULL) {
2730 warn("%s: error allocating %zd bytes", __func__,
2731 num_options * sizeof(*req.be_args));
2736 for (i = 0, option = STAILQ_FIRST(&option_list);
2737 i < num_options; i++, option = next_option) {
2738 next_option = STAILQ_NEXT(option, links);
2740 req.be_args[i].namelen = option->namelen;
2741 req.be_args[i].name = strdup(option->name);
2742 req.be_args[i].vallen = option->vallen;
2743 req.be_args[i].value = strdup(option->value);
2745 * XXX KDM do we want a way to specify a writeable
2746 * flag of some sort? Do we want a way to specify
2749 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
2751 STAILQ_REMOVE(&option_list, option, cctl_req_option,
2754 free(option->value);
2759 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
2760 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
2765 switch (req.status) {
2767 warnx("LUN modification error: %s", req.error_str);
2770 case CTL_LUN_WARNING:
2771 warnx("LUN modification warning: %s", req.error_str);
2776 warnx("unknown LUN modification status: %d", req.status);
2781 printf("LUN %d modified successfully\n", lun_id);
2787 struct cctl_islist_conn {
2790 char *initiator_addr;
2791 char *initiator_alias;
2794 char *header_digest;
2796 char *max_data_segment_length;;
2799 STAILQ_ENTRY(cctl_islist_conn) links;
2802 struct cctl_islist_data {
2804 STAILQ_HEAD(,cctl_islist_conn) conn_list;
2805 struct cctl_islist_conn *cur_conn;
2807 struct sbuf *cur_sb[32];
2811 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
2814 struct cctl_islist_data *islist;
2815 struct cctl_islist_conn *cur_conn;
2817 islist = (struct cctl_islist_data *)user_data;
2818 cur_conn = islist->cur_conn;
2820 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
2821 sizeof(islist->cur_sb[0])))
2822 errx(1, "%s: too many nesting levels, %zd max", __func__,
2823 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
2825 islist->cur_sb[islist->level] = sbuf_new_auto();
2826 if (islist->cur_sb[islist->level] == NULL)
2827 err(1, "%s: Unable to allocate sbuf", __func__);
2829 if (strcmp(name, "connection") == 0) {
2830 if (cur_conn != NULL)
2831 errx(1, "%s: improper connection element nesting",
2834 cur_conn = calloc(1, sizeof(*cur_conn));
2835 if (cur_conn == NULL)
2836 err(1, "%s: cannot allocate %zd bytes", __func__,
2839 islist->num_conns++;
2840 islist->cur_conn = cur_conn;
2842 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
2844 for (i = 0; attr[i] != NULL; i += 2) {
2845 if (strcmp(attr[i], "id") == 0) {
2846 cur_conn->connection_id =
2847 strtoull(attr[i+1], NULL, 0);
2850 "%s: invalid connection attribute %s = %s",
2851 __func__, attr[i], attr[i+1]);
2858 cctl_islist_end_element(void *user_data, const char *name)
2860 struct cctl_islist_data *islist;
2861 struct cctl_islist_conn *cur_conn;
2864 islist = (struct cctl_islist_data *)user_data;
2865 cur_conn = islist->cur_conn;
2867 if ((cur_conn == NULL)
2868 && (strcmp(name, "ctlislist") != 0))
2869 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
2871 if (islist->cur_sb[islist->level] == NULL)
2872 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
2873 islist->level, name);
2875 sbuf_finish(islist->cur_sb[islist->level]);
2876 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
2878 err(1, "%s can't allocate %zd bytes for string", __func__,
2879 sbuf_len(islist->cur_sb[islist->level]));
2881 sbuf_delete(islist->cur_sb[islist->level]);
2882 islist->cur_sb[islist->level] = NULL;
2885 if (strcmp(name, "initiator") == 0) {
2886 cur_conn->initiator = str;
2888 } else if (strcmp(name, "initiator_addr") == 0) {
2889 cur_conn->initiator_addr = str;
2891 } else if (strcmp(name, "initiator_alias") == 0) {
2892 cur_conn->initiator_alias = str;
2894 } else if (strcmp(name, "target") == 0) {
2895 cur_conn->target = str;
2897 } else if (strcmp(name, "target_alias") == 0) {
2898 cur_conn->target_alias = str;
2900 } else if (strcmp(name, "target_portal_group_tag") == 0) {
2901 } else if (strcmp(name, "header_digest") == 0) {
2902 cur_conn->header_digest = str;
2904 } else if (strcmp(name, "data_digest") == 0) {
2905 cur_conn->data_digest = str;
2907 } else if (strcmp(name, "max_data_segment_length") == 0) {
2908 cur_conn->max_data_segment_length = str;
2910 } else if (strcmp(name, "immediate_data") == 0) {
2911 cur_conn->immediate_data = atoi(str);
2912 } else if (strcmp(name, "iser") == 0) {
2913 cur_conn->iser = atoi(str);
2914 } else if (strcmp(name, "connection") == 0) {
2915 islist->cur_conn = NULL;
2916 } else if (strcmp(name, "ctlislist") == 0) {
2920 * Unknown element; ignore it for forward compatiblity.
2928 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
2930 struct cctl_islist_data *islist;
2932 islist = (struct cctl_islist_data *)user_data;
2934 sbuf_bcat(islist->cur_sb[islist->level], str, len);
2938 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
2940 struct ctl_iscsi req;
2941 struct cctl_islist_data islist;
2942 struct cctl_islist_conn *conn;
2947 int c, retval, verbose = 0;
2952 bzero(&islist, sizeof(islist));
2953 STAILQ_INIT(&islist.conn_list);
2955 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2969 conn_str = malloc(conn_len);
2971 bzero(&req, sizeof(req));
2972 req.type = CTL_ISCSI_LIST;
2973 req.data.list.alloc_len = conn_len;
2974 req.data.list.conn_xml = conn_str;
2976 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
2977 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
2982 if (req.status == CTL_ISCSI_ERROR) {
2983 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
2984 __func__, req.error_str);
2985 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
2986 conn_len = conn_len << 1;
2990 if (dump_xml != 0) {
2991 printf("%s", conn_str);
2995 parser = XML_ParserCreate(NULL);
2996 if (parser == NULL) {
2997 warn("%s: Unable to create XML parser", __func__);
3002 XML_SetUserData(parser, &islist);
3003 XML_SetElementHandler(parser, cctl_islist_start_element,
3004 cctl_islist_end_element);
3005 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3007 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3009 warnx("%s: Unable to parse XML: Error %d", __func__,
3010 XML_GetErrorCode(parser));
3011 XML_ParserFree(parser);
3016 XML_ParserFree(parser);
3019 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3020 printf("Session ID: %d\n", conn->connection_id);
3021 printf("Initiator name: %s\n", conn->initiator);
3022 printf("Initiator portal: %s\n", conn->initiator_addr);
3023 printf("Initiator alias: %s\n", conn->initiator_alias);
3024 printf("Target name: %s\n", conn->target);
3025 printf("Target alias: %s\n", conn->target_alias);
3026 printf("Header digest: %s\n", conn->header_digest);
3027 printf("Data digest: %s\n", conn->data_digest);
3028 printf("DataSegmentLen: %s\n", conn->max_data_segment_length);
3029 printf("ImmediateData: %s\n", conn->immediate_data ? "Yes" : "No");
3030 printf("iSER (RDMA): %s\n", conn->iser ? "Yes" : "No");
3034 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3036 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3037 printf("%4u %-16s %-36s %-36s\n",
3038 conn->connection_id, conn->initiator_addr, conn->initiator,
3049 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3051 struct ctl_iscsi req;
3053 int all = 0, connection_id = -1, nargs = 0;
3054 char *initiator_name = NULL, *initiator_addr = NULL;
3056 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3063 connection_id = strtoul(optarg, NULL, 0);
3067 initiator_name = strdup(optarg);
3068 if (initiator_name == NULL)
3069 err(1, "%s: strdup", __func__);
3073 initiator_addr = strdup(optarg);
3074 if (initiator_addr == NULL)
3075 err(1, "%s: strdup", __func__);
3084 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3087 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3090 bzero(&req, sizeof(req));
3091 req.type = CTL_ISCSI_LOGOUT;
3092 req.data.logout.connection_id = connection_id;
3093 if (initiator_addr != NULL)
3094 strlcpy(req.data.logout.initiator_addr,
3095 initiator_addr, sizeof(req.data.logout.initiator_addr));
3096 if (initiator_name != NULL)
3097 strlcpy(req.data.logout.initiator_name,
3098 initiator_name, sizeof(req.data.logout.initiator_name));
3100 req.data.logout.all = 1;
3102 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3103 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3108 if (req.status != CTL_ISCSI_OK) {
3109 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3110 __func__, req.error_str);
3115 printf("iSCSI logout requests submitted\n");
3122 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3124 struct ctl_iscsi req;
3126 int all = 0, connection_id = -1, nargs = 0;
3127 char *initiator_name = NULL, *initiator_addr = NULL;
3129 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3136 connection_id = strtoul(optarg, NULL, 0);
3140 initiator_name = strdup(optarg);
3141 if (initiator_name == NULL)
3142 err(1, "%s: strdup", __func__);
3146 initiator_addr = strdup(optarg);
3147 if (initiator_addr == NULL)
3148 err(1, "%s: strdup", __func__);
3157 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3160 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3163 bzero(&req, sizeof(req));
3164 req.type = CTL_ISCSI_TERMINATE;
3165 req.data.terminate.connection_id = connection_id;
3166 if (initiator_addr != NULL)
3167 strlcpy(req.data.terminate.initiator_addr,
3168 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3169 if (initiator_name != NULL)
3170 strlcpy(req.data.terminate.initiator_name,
3171 initiator_name, sizeof(req.data.terminate.initiator_name));
3173 req.data.terminate.all = 1;
3175 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3176 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3181 if (req.status != CTL_ISCSI_OK) {
3182 warnx("%s: error returned from CTL iSCSI connection "
3183 "termination request:\n%s", __func__, req.error_str);
3188 printf("iSCSI connections terminated\n");
3195 * Name/value pair used for per-LUN attributes.
3197 struct cctl_lun_nv {
3200 STAILQ_ENTRY(cctl_lun_nv) links;
3204 * Backend LUN information.
3209 uint64_t size_blocks;
3211 char *serial_number;
3213 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3214 STAILQ_ENTRY(cctl_lun) links;
3217 struct cctl_devlist_data {
3219 STAILQ_HEAD(,cctl_lun) lun_list;
3220 struct cctl_lun *cur_lun;
3222 struct sbuf *cur_sb[32];
3226 cctl_start_element(void *user_data, const char *name, const char **attr)
3229 struct cctl_devlist_data *devlist;
3230 struct cctl_lun *cur_lun;
3232 devlist = (struct cctl_devlist_data *)user_data;
3233 cur_lun = devlist->cur_lun;
3235 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3236 sizeof(devlist->cur_sb[0])))
3237 errx(1, "%s: too many nesting levels, %zd max", __func__,
3238 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3240 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3241 if (devlist->cur_sb[devlist->level] == NULL)
3242 err(1, "%s: Unable to allocate sbuf", __func__);
3244 if (strcmp(name, "lun") == 0) {
3245 if (cur_lun != NULL)
3246 errx(1, "%s: improper lun element nesting", __func__);
3248 cur_lun = calloc(1, sizeof(*cur_lun));
3249 if (cur_lun == NULL)
3250 err(1, "%s: cannot allocate %zd bytes", __func__,
3253 devlist->num_luns++;
3254 devlist->cur_lun = cur_lun;
3256 STAILQ_INIT(&cur_lun->attr_list);
3257 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3259 for (i = 0; attr[i] != NULL; i += 2) {
3260 if (strcmp(attr[i], "id") == 0) {
3261 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3263 errx(1, "%s: invalid LUN attribute %s = %s",
3264 __func__, attr[i], attr[i+1]);
3271 cctl_end_element(void *user_data, const char *name)
3273 struct cctl_devlist_data *devlist;
3274 struct cctl_lun *cur_lun;
3277 devlist = (struct cctl_devlist_data *)user_data;
3278 cur_lun = devlist->cur_lun;
3280 if ((cur_lun == NULL)
3281 && (strcmp(name, "ctllunlist") != 0))
3282 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3284 if (devlist->cur_sb[devlist->level] == NULL)
3285 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3286 devlist->level, name);
3288 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3289 err(1, "%s: sbuf_finish", __func__);
3290 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3292 err(1, "%s can't allocate %zd bytes for string", __func__,
3293 sbuf_len(devlist->cur_sb[devlist->level]));
3295 if (strlen(str) == 0) {
3300 sbuf_delete(devlist->cur_sb[devlist->level]);
3301 devlist->cur_sb[devlist->level] = NULL;
3304 if (strcmp(name, "backend_type") == 0) {
3305 cur_lun->backend_type = str;
3307 } else if (strcmp(name, "size") == 0) {
3308 cur_lun->size_blocks = strtoull(str, NULL, 0);
3309 } else if (strcmp(name, "blocksize") == 0) {
3310 cur_lun->blocksize = strtoul(str, NULL, 0);
3311 } else if (strcmp(name, "serial_number") == 0) {
3312 cur_lun->serial_number = str;
3314 } else if (strcmp(name, "device_id") == 0) {
3315 cur_lun->device_id = str;
3317 } else if (strcmp(name, "lun") == 0) {
3318 devlist->cur_lun = NULL;
3319 } else if (strcmp(name, "ctllunlist") == 0) {
3322 struct cctl_lun_nv *nv;
3324 nv = calloc(1, sizeof(*nv));
3326 err(1, "%s: can't allocate %zd bytes for nv pair",
3327 __func__, sizeof(*nv));
3329 nv->name = strdup(name);
3330 if (nv->name == NULL)
3331 err(1, "%s: can't allocated %zd bytes for string",
3332 __func__, strlen(name));
3336 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3343 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3345 struct cctl_devlist_data *devlist;
3347 devlist = (struct cctl_devlist_data *)user_data;
3349 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3353 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3355 struct ctl_lun_list list;
3356 struct cctl_devlist_data devlist;
3357 struct cctl_lun *lun;
3363 char *backend = NULL;
3369 bzero(&devlist, sizeof(devlist));
3370 STAILQ_INIT(&devlist.lun_list);
3372 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3375 backend = strdup(optarg);
3389 lun_str = malloc(lun_len);
3391 bzero(&list, sizeof(list));
3392 list.alloc_len = lun_len;
3393 list.status = CTL_LUN_LIST_NONE;
3394 list.lun_xml = lun_str;
3396 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
3397 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
3402 if (list.status == CTL_LUN_LIST_ERROR) {
3403 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
3404 __func__, list.error_str);
3405 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3406 lun_len = lun_len << 1;
3410 if (dump_xml != 0) {
3411 printf("%s", lun_str);
3415 parser = XML_ParserCreate(NULL);
3416 if (parser == NULL) {
3417 warn("%s: Unable to create XML parser", __func__);
3422 XML_SetUserData(parser, &devlist);
3423 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
3424 XML_SetCharacterDataHandler(parser, cctl_char_handler);
3426 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
3428 warnx("%s: Unable to parse XML: Error %d", __func__,
3429 XML_GetErrorCode(parser));
3430 XML_ParserFree(parser);
3435 XML_ParserFree(parser);
3437 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
3438 "Serial Number", "Device ID");
3439 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
3440 struct cctl_lun_nv *nv;
3442 if ((backend != NULL)
3443 && (strcmp(lun->backend_type, backend) != 0))
3446 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
3447 (uintmax_t)lun->lun_id,
3448 lun->backend_type, (uintmax_t)lun->size_blocks,
3449 lun->blocksize, lun->serial_number, lun->device_id);
3454 STAILQ_FOREACH(nv, &lun->attr_list, links) {
3455 printf(" %s=%s\n", nv->name, nv->value);
3470 char *frontend_type;
3473 char *target, *port, *lun_map;
3474 STAILQ_HEAD(,cctl_lun_nv) init_list;
3475 STAILQ_HEAD(,cctl_lun_nv) lun_list;
3476 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3477 STAILQ_ENTRY(cctl_port) links;
3480 struct cctl_portlist_data {
3482 STAILQ_HEAD(,cctl_port) port_list;
3483 struct cctl_port *cur_port;
3486 struct sbuf *cur_sb[32];
3490 cctl_start_pelement(void *user_data, const char *name, const char **attr)
3493 struct cctl_portlist_data *portlist;
3494 struct cctl_port *cur_port;
3496 portlist = (struct cctl_portlist_data *)user_data;
3497 cur_port = portlist->cur_port;
3499 if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
3500 sizeof(portlist->cur_sb[0])))
3501 errx(1, "%s: too many nesting levels, %zd max", __func__,
3502 sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
3504 portlist->cur_sb[portlist->level] = sbuf_new_auto();
3505 if (portlist->cur_sb[portlist->level] == NULL)
3506 err(1, "%s: Unable to allocate sbuf", __func__);
3508 portlist->cur_id = 0;
3509 for (i = 0; attr[i] != NULL; i += 2) {
3510 if (strcmp(attr[i], "id") == 0) {
3511 portlist->cur_id = strtoull(attr[i+1], NULL, 0);
3516 if (strcmp(name, "targ_port") == 0) {
3517 if (cur_port != NULL)
3518 errx(1, "%s: improper port element nesting", __func__);
3520 cur_port = calloc(1, sizeof(*cur_port));
3521 if (cur_port == NULL)
3522 err(1, "%s: cannot allocate %zd bytes", __func__,
3525 portlist->num_ports++;
3526 portlist->cur_port = cur_port;
3528 STAILQ_INIT(&cur_port->init_list);
3529 STAILQ_INIT(&cur_port->lun_list);
3530 STAILQ_INIT(&cur_port->attr_list);
3531 cur_port->port_id = portlist->cur_id;
3532 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
3537 cctl_end_pelement(void *user_data, const char *name)
3539 struct cctl_portlist_data *portlist;
3540 struct cctl_port *cur_port;
3543 portlist = (struct cctl_portlist_data *)user_data;
3544 cur_port = portlist->cur_port;
3546 if ((cur_port == NULL)
3547 && (strcmp(name, "ctlportlist") != 0))
3548 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
3550 if (portlist->cur_sb[portlist->level] == NULL)
3551 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3552 portlist->level, name);
3554 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
3555 err(1, "%s: sbuf_finish", __func__);
3556 str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
3558 err(1, "%s can't allocate %zd bytes for string", __func__,
3559 sbuf_len(portlist->cur_sb[portlist->level]));
3561 if (strlen(str) == 0) {
3566 sbuf_delete(portlist->cur_sb[portlist->level]);
3567 portlist->cur_sb[portlist->level] = NULL;
3570 if (strcmp(name, "frontend_type") == 0) {
3571 cur_port->frontend_type = str;
3573 } else if (strcmp(name, "port_name") == 0) {
3574 cur_port->name = str;
3576 } else if (strcmp(name, "online") == 0) {
3577 cur_port->online = str;
3579 } else if (strcmp(name, "physical_port") == 0) {
3580 cur_port->pp = strtoull(str, NULL, 0);
3581 } else if (strcmp(name, "virtual_port") == 0) {
3582 cur_port->vp = strtoull(str, NULL, 0);
3583 } else if (strcmp(name, "target") == 0) {
3584 cur_port->target = str;
3586 } else if (strcmp(name, "port") == 0) {
3587 cur_port->port = str;
3589 } else if (strcmp(name, "lun_map") == 0) {
3590 cur_port->lun_map = str;
3592 } else if (strcmp(name, "targ_port") == 0) {
3593 portlist->cur_port = NULL;
3594 } else if (strcmp(name, "ctlportlist") == 0) {
3597 struct cctl_lun_nv *nv;
3599 nv = calloc(1, sizeof(*nv));
3601 err(1, "%s: can't allocate %zd bytes for nv pair",
3602 __func__, sizeof(*nv));
3604 if (strcmp(name, "initiator") == 0 ||
3605 strcmp(name, "lun") == 0)
3606 asprintf(&nv->name, "%ju", portlist->cur_id);
3608 nv->name = strdup(name);
3609 if (nv->name == NULL)
3610 err(1, "%s: can't allocated %zd bytes for string",
3611 __func__, strlen(name));
3615 if (strcmp(name, "initiator") == 0)
3616 STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links);
3617 else if (strcmp(name, "lun") == 0)
3618 STAILQ_INSERT_TAIL(&cur_port->lun_list, nv, links);
3620 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
3627 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
3629 struct cctl_portlist_data *portlist;
3631 portlist = (struct cctl_portlist_data *)user_data;
3633 sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
3637 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
3639 struct ctl_lun_list list;
3640 struct cctl_portlist_data portlist;
3641 struct cctl_port *port;
3647 char *frontend = NULL;
3648 uint64_t portarg = UINT64_MAX;
3649 int verbose = 0, init = 0, lun = 0, quiet = 0;
3654 bzero(&portlist, sizeof(portlist));
3655 STAILQ_INIT(&portlist.port_list);
3657 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3660 frontend = strdup(optarg);
3669 portarg = strtoll(optarg, NULL, 0);
3686 port_str = malloc(port_len);
3688 bzero(&list, sizeof(list));
3689 list.alloc_len = port_len;
3690 list.status = CTL_LUN_LIST_NONE;
3691 list.lun_xml = port_str;
3693 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
3694 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
3699 if (list.status == CTL_LUN_LIST_ERROR) {
3700 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
3701 __func__, list.error_str);
3702 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3703 port_len = port_len << 1;
3707 if (dump_xml != 0) {
3708 printf("%s", port_str);
3712 parser = XML_ParserCreate(NULL);
3713 if (parser == NULL) {
3714 warn("%s: Unable to create XML parser", __func__);
3719 XML_SetUserData(parser, &portlist);
3720 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
3721 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
3723 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
3725 warnx("%s: Unable to parse XML: Error %d", __func__,
3726 XML_GetErrorCode(parser));
3727 XML_ParserFree(parser);
3732 XML_ParserFree(parser);
3735 printf("Port Online Frontend Name pp vp\n");
3736 STAILQ_FOREACH(port, &portlist.port_list, links) {
3737 struct cctl_lun_nv *nv;
3739 if ((frontend != NULL)
3740 && (strcmp(port->frontend_type, frontend) != 0))
3743 if ((portarg != UINT64_MAX) && (portarg != port->port_id))
3746 printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n",
3747 (uintmax_t)port->port_id, port->online,
3748 port->frontend_type, port->name, port->pp, port->vp,
3749 port->port ? port->port : "");
3751 if (init || verbose) {
3753 printf(" Target: %s\n", port->target);
3754 STAILQ_FOREACH(nv, &port->init_list, links) {
3755 printf(" Initiator %s: %s\n",
3756 nv->name, nv->value);
3760 if (lun || verbose) {
3761 if (port->lun_map) {
3762 STAILQ_FOREACH(nv, &port->lun_list, links)
3763 printf(" LUN %s: %s\n",
3764 nv->name, nv->value);
3765 if (STAILQ_EMPTY(&port->lun_list))
3766 printf(" No LUNs mapped\n");
3768 printf(" All LUNs mapped\n");
3772 STAILQ_FOREACH(nv, &port->attr_list, links) {
3773 printf(" %s=%s\n", nv->name, nv->value);
3784 cctl_lunmap(int fd, int argc, char **argv, char *combinedopt)
3786 struct ctl_lun_map lm;
3790 lm.port = UINT32_MAX;
3791 lm.plun = UINT32_MAX;
3792 lm.lun = UINT32_MAX;
3794 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3797 lm.port = strtoll(optarg, NULL, 0);
3800 lm.plun = strtoll(optarg, NULL, 0);
3803 lm.lun = strtoll(optarg, NULL, 0);
3810 if (ioctl(fd, CTL_LUN_MAP, &lm) == -1) {
3811 warn("%s: error issuing CTL_LUN_MAP ioctl", __func__);
3821 fprintf(error ? stderr : stdout,
3823 "Primary commands:\n"
3824 " ctladm tur [dev_id][general options]\n"
3825 " ctladm inquiry [dev_id][general options]\n"
3826 " ctladm devid [dev_id][general options]\n"
3827 " ctladm reqsense [dev_id][general options]\n"
3828 " ctladm reportluns [dev_id][general options]\n"
3829 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
3830 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3831 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
3832 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3833 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
3834 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
3835 " [-d] [-S subpage] [-c cdbsize]\n"
3836 " ctladm prin [dev_id][general options] <-a action>\n"
3837 " ctladm prout [dev_id][general options] <-a action>\n"
3838 " <-r restype] [-k key] [-s sa_key]\n"
3839 " ctladm rtpg [dev_id][general options]\n"
3840 " ctladm start [dev_id][general options] [-i] [-o]\n"
3841 " ctladm stop [dev_id][general options] [-i] [-o]\n"
3842 " ctladm synccache [dev_id][general options] [-l lba]\n"
3843 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
3844 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
3845 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
3846 " [-S serial_num] [-t dev_type]\n"
3847 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
3848 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
3849 " ctladm devlist [-b backend] [-v] [-x]\n"
3851 " ctladm lunmap -p targ_port [-l pLUN] [-L cLUN]\n"
3852 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
3854 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
3855 " [-s len fmt [args]] [-c] [-d delete_id]\n"
3856 " ctladm port <-o <on|off> | [-w wwnn][-W wwpn]>\n"
3857 " [-p targ_port] [-t port_type]\n"
3858 " ctladm portlist [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n"
3859 " ctladm islist [-v | -x]\n"
3860 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
3861 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
3863 " ctladm dumpstructs\n"
3865 "General Options:\n"
3866 "-I intiator_id : defaults to 7, used to change the initiator id\n"
3867 "-C retries : specify the number of times to retry this command\n"
3868 "-D devicename : specify the device to operate on\n"
3869 " : (default is %s)\n"
3870 "read/write options:\n"
3871 "-l lba : logical block address\n"
3872 "-d len : read/write length, in blocks\n"
3873 "-f file|- : write/read data to/from file or stdout/stdin\n"
3874 "-b blocksize : block size, in bytes\n"
3875 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
3876 "-N : do not copy data to/from userland\n"
3877 "readcapacity options:\n"
3878 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3879 "modesense options:\n"
3880 "-m page : specify the mode page to view\n"
3881 "-l : request a list of supported pages\n"
3882 "-P pc : specify the page control value: 0-3 (current,\n"
3883 " changeable, default, saved, respectively)\n"
3884 "-d : disable block descriptors for mode sense\n"
3885 "-S subpage : specify a subpage\n"
3886 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
3887 "persistent reserve in options:\n"
3888 "-a action : specify the action value: 0-2 (read key, read\n"
3889 " reservation, read capabilities, respectively)\n"
3890 "persistent reserve out options:\n"
3891 "-a action : specify the action value: 0-5 (register, reserve,\n"
3892 " release, clear, preempt, register and ignore)\n"
3893 "-k key : key value\n"
3894 "-s sa_key : service action value\n"
3895 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
3896 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
3897 "start/stop options:\n"
3898 "-i : set the immediate bit (CTL does not support this)\n"
3899 "-o : set the on/offline bit\n"
3900 "synccache options:\n"
3901 "-l lba : set the starting LBA\n"
3902 "-b blockcount : set the length to sync in blocks\n"
3903 "-r : set the relative addressing bit\n"
3904 "-i : set the immediate bit\n"
3905 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
3907 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3908 "-B blocksize : LUN blocksize in bytes (some backends)\n"
3909 "-d device_id : SCSI VPD page 0x83 ID\n"
3910 "-l lun_id : requested LUN number\n"
3911 "-o name=value : backend-specific options, multiple allowed\n"
3912 "-s size_bytes : LUN size in bytes (some backends)\n"
3913 "-S serial_num : SCSI VPD page 0x80 serial number\n"
3914 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
3916 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
3917 "-l lun_id : LUN number to delete\n"
3918 "-o name=value : backend-specific options, multiple allowed\n"
3919 "devlist options:\n"
3920 "-b backend : list devices from specified backend only\n"
3921 "-v : be verbose, show backend attributes\n"
3922 "-x : dump raw XML\n"
3924 "-l datamove|done : delay command at datamove or done phase\n"
3925 "-T oneshot : delay one command, then resume normal completion\n"
3926 "-T cont : delay all commands\n"
3927 "-t secs : number of seconds to delay\n"
3929 "-i error_action : action to perform\n"
3930 "-p pattern : command pattern to look for\n"
3931 "-r lba,len : LBA range for pattern\n"
3932 "-s len fmt [args] : sense data for custom sense action\n"
3933 "-c : continuous operation\n"
3934 "-d delete_id : error id to delete\n"
3936 "-l : list frontend ports\n"
3937 "-o on|off : turn frontend ports on or off\n"
3938 "-w wwnn : set WWNN for one frontend\n"
3939 "-W wwpn : set WWPN for one frontend\n"
3940 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
3941 "-p targ_port : specify target port number\n"
3942 "-q : omit header in list output\n"
3943 "-x : output port list in XML format\n"
3944 "portlist options:\n"
3945 "-f fronetnd : specify frontend type\n"
3946 "-i : report target and initiators addresses\n"
3947 "-l : report LUN mapping\n"
3948 "-p targ_port : specify target port number\n"
3949 "-q : omit header in list output\n"
3950 "-v : verbose output (report all port options)\n"
3951 "-x : output port list in XML format\n"
3953 "-p targ_port : specify target port number\n"
3954 "-L pLUN : specify port-visible LUN\n"
3955 "-L cLUN : specify CTL LUN\n",
3960 main(int argc, char **argv)
3963 ctladm_cmdfunction command;
3964 ctladm_cmdargs cmdargs;
3965 ctladm_optret optreturn;
3967 const char *mainopt = "C:D:I:";
3968 const char *subopt = NULL;
3969 char combinedopt[256];
3978 cmdargs = CTLADM_ARG_NONE;
3979 command = CTLADM_CMD_HELP;
3993 * Get the base option.
3995 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
3997 if (optreturn == CC_OR_AMBIGUOUS) {
3998 warnx("ambiguous option %s", argv[1]);
4001 } else if (optreturn == CC_OR_NOT_FOUND) {
4002 warnx("option %s not found", argv[1]);
4007 if (cmdargs & CTLADM_ARG_NEED_TL) {
4008 if ((argc < 3) || (!isdigit(argv[2][0]))) {
4009 warnx("option %s requires a lun argument",
4014 lun = strtol(argv[2], NULL, 0);
4016 cmdargs |= CTLADM_ARG_TARG_LUN;
4021 * Ahh, getopt(3) is a pain.
4023 * This is a gross hack. There really aren't many other good
4024 * options (excuse the pun) for parsing options in a situation like
4025 * this. getopt is kinda braindead, so you end up having to run
4026 * through the options twice, and give each invocation of getopt
4027 * the option string for the other invocation.
4029 * You would think that you could just have two groups of options.
4030 * The first group would get parsed by the first invocation of
4031 * getopt, and the second group would get parsed by the second
4032 * invocation of getopt. It doesn't quite work out that way. When
4033 * the first invocation of getopt finishes, it leaves optind pointing
4034 * to the argument _after_ the first argument in the second group.
4035 * So when the second invocation of getopt comes around, it doesn't
4036 * recognize the first argument it gets and then bails out.
4038 * A nice alternative would be to have a flag for getopt that says
4039 * "just keep parsing arguments even when you encounter an unknown
4040 * argument", but there isn't one. So there's no real clean way to
4041 * easily parse two sets of arguments without having one invocation
4042 * of getopt know about the other.
4044 * Without this hack, the first invocation of getopt would work as
4045 * long as the generic arguments are first, but the second invocation
4046 * (in the subfunction) would fail in one of two ways. In the case
4047 * where you don't set optreset, it would fail because optind may be
4048 * pointing to the argument after the one it should be pointing at.
4049 * In the case where you do set optreset, and reset optind, it would
4050 * fail because getopt would run into the first set of options, which
4051 * it doesn't understand.
4053 * All of this would "sort of" work if you could somehow figure out
4054 * whether optind had been incremented one option too far. The
4055 * mechanics of that, however, are more daunting than just giving
4056 * both invocations all of the expect options for either invocation.
4058 * Needless to say, I wouldn't mind if someone invented a better
4059 * (non-GPL!) command line parsing interface than getopt. I
4060 * wouldn't mind if someone added more knobs to getopt to make it
4061 * work better. Who knows, I may talk myself into doing it someday,
4062 * if the standards weenies let me. As it is, it just leads to
4063 * hackery like this and causes people to avoid it in some cases.
4065 * KDM, September 8th, 1998
4068 sprintf(combinedopt, "%s%s", mainopt, subopt);
4070 sprintf(combinedopt, "%s", mainopt);
4073 * Start getopt processing at argv[2/3], since we've already
4074 * accepted argv[1..2] as the command name, and as a possible
4080 * Now we run through the argument list looking for generic
4081 * options, and ignoring options that possibly belong to
4084 while ((c = getopt(argc, argv, combinedopt))!= -1){
4087 cmdargs |= CTLADM_ARG_RETRIES;
4088 retries = strtol(optarg, NULL, 0);
4091 device = strdup(optarg);
4092 cmdargs |= CTLADM_ARG_DEVICE;
4095 cmdargs |= CTLADM_ARG_INITIATOR;
4096 initid = strtol(optarg, NULL, 0);
4103 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4110 * Default to opening the CTL device for now.
4112 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4113 && (command != CTLADM_CMD_HELP)) {
4114 device = strdup(CTL_DEFAULT_DEV);
4115 cmdargs |= CTLADM_ARG_DEVICE;
4118 if ((cmdargs & CTLADM_ARG_DEVICE)
4119 && (command != CTLADM_CMD_HELP)) {
4120 fd = open(device, O_RDWR);
4121 if (fd == -1 && errno == ENOENT) {
4122 saved_errno = errno;
4123 retval = kldload("ctl");
4125 fd = open(device, O_RDWR);
4127 errno = saved_errno;
4130 fprintf(stderr, "%s: error opening %s: %s\n",
4131 argv[0], device, strerror(errno));
4135 } else if ((command != CTLADM_CMD_HELP)
4136 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4137 fprintf(stderr, "%s: you must specify a device with the "
4138 "--device argument for this command\n", argv[0]);
4139 command = CTLADM_CMD_HELP;
4144 case CTLADM_CMD_TUR:
4145 retval = cctl_tur(fd, lun, initid, retries);
4147 case CTLADM_CMD_INQUIRY:
4148 retval = cctl_inquiry(fd, lun, initid, retries);
4150 case CTLADM_CMD_REQ_SENSE:
4151 retval = cctl_req_sense(fd, lun, initid, retries);
4153 case CTLADM_CMD_REPORT_LUNS:
4154 retval = cctl_report_luns(fd, lun, initid, retries);
4156 case CTLADM_CMD_CREATE:
4157 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4160 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4162 case CTLADM_CMD_DEVLIST:
4163 retval = cctl_devlist(fd, argc, argv, combinedopt);
4165 case CTLADM_CMD_READ:
4166 case CTLADM_CMD_WRITE:
4167 retval = cctl_read_write(fd, lun, initid, retries,
4168 argc, argv, combinedopt, command);
4170 case CTLADM_CMD_PORT:
4171 retval = cctl_port(fd, argc, argv, combinedopt);
4173 case CTLADM_CMD_PORTLIST:
4174 retval = cctl_portlist(fd, argc, argv, combinedopt);
4176 case CTLADM_CMD_LUNMAP:
4177 retval = cctl_lunmap(fd, argc, argv, combinedopt);
4179 case CTLADM_CMD_READCAPACITY:
4180 retval = cctl_read_capacity(fd, lun, initid, retries,
4181 argc, argv, combinedopt);
4183 case CTLADM_CMD_MODESENSE:
4184 retval = cctl_mode_sense(fd, lun, initid, retries,
4185 argc, argv, combinedopt);
4187 case CTLADM_CMD_START:
4188 case CTLADM_CMD_STOP:
4189 retval = cctl_start_stop(fd, lun, initid, retries,
4190 (command == CTLADM_CMD_START) ? 1 : 0,
4191 argc, argv, combinedopt);
4193 case CTLADM_CMD_SYNC_CACHE:
4194 retval = cctl_sync_cache(fd, lun, initid, retries,
4195 argc, argv, combinedopt);
4197 case CTLADM_CMD_LUNLIST:
4198 retval = cctl_lunlist(fd);
4200 case CTLADM_CMD_DELAY:
4201 retval = cctl_delay(fd, lun, argc, argv, combinedopt);
4203 case CTLADM_CMD_ERR_INJECT:
4204 retval = cctl_error_inject(fd, lun, argc, argv,
4207 case CTLADM_CMD_DUMPOOA:
4208 retval = cctl_dump_ooa(fd, argc, argv);
4210 case CTLADM_CMD_DUMPSTRUCTS:
4211 retval = cctl_dump_structs(fd, cmdargs);
4213 case CTLADM_CMD_PRES_IN:
4214 retval = cctl_persistent_reserve_in(fd, lun, initid,
4215 argc, argv, combinedopt,
4218 case CTLADM_CMD_PRES_OUT:
4219 retval = cctl_persistent_reserve_out(fd, lun, initid,
4220 argc, argv, combinedopt,
4223 case CTLADM_CMD_INQ_VPD_DEVID:
4224 retval = cctl_inquiry_vpd_devid(fd, lun, initid);
4226 case CTLADM_CMD_RTPG:
4227 retval = cctl_report_target_port_group(fd, lun, initid);
4229 case CTLADM_CMD_MODIFY:
4230 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4232 case CTLADM_CMD_ISLIST:
4233 retval = cctl_islist(fd, argc, argv, combinedopt);
4235 case CTLADM_CMD_ISLOGOUT:
4236 retval = cctl_islogout(fd, argc, argv, combinedopt);
4238 case CTLADM_CMD_ISTERMINATE:
4239 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4241 case CTLADM_CMD_HELP: