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/ioctl.h>
47 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/linker.h>
51 #include <sys/queue.h>
52 #include <sys/callout.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_frontend_internal.h>
70 #include <cam/ctl/ctl_backend.h>
71 #include <cam/ctl/ctl_ioctl.h>
72 #include <cam/ctl/ctl_backend_block.h>
73 #include <cam/ctl/ctl_util.h>
74 #include <cam/ctl/ctl_scsi_all.h>
82 #define min(x,y) (x < y) ? x : y
89 CTLADM_CMD_REPORT_LUNS,
98 CTLADM_CMD_READCAPACITY,
101 CTLADM_CMD_DUMPSTRUCTS,
104 CTLADM_CMD_SYNC_CACHE,
109 CTLADM_CMD_HARDSTART,
114 CTLADM_CMD_ERR_INJECT,
118 CTLADM_CMD_INQ_VPD_DEVID,
123 CTLADM_CMD_ISTERMINATE
124 } ctladm_cmdfunction;
127 CTLADM_ARG_NONE = 0x0000000,
128 CTLADM_ARG_AUTOSENSE = 0x0000001,
129 CTLADM_ARG_DEVICE = 0x0000002,
130 CTLADM_ARG_ARRAYSIZE = 0x0000004,
131 CTLADM_ARG_BACKEND = 0x0000008,
132 CTLADM_ARG_CDBSIZE = 0x0000010,
133 CTLADM_ARG_DATALEN = 0x0000020,
134 CTLADM_ARG_FILENAME = 0x0000040,
135 CTLADM_ARG_LBA = 0x0000080,
136 CTLADM_ARG_PC = 0x0000100,
137 CTLADM_ARG_PAGE_CODE = 0x0000200,
138 CTLADM_ARG_PAGE_LIST = 0x0000400,
139 CTLADM_ARG_SUBPAGE = 0x0000800,
140 CTLADM_ARG_PAGELIST = 0x0001000,
141 CTLADM_ARG_DBD = 0x0002000,
142 CTLADM_ARG_TARG_LUN = 0x0004000,
143 CTLADM_ARG_BLOCKSIZE = 0x0008000,
144 CTLADM_ARG_IMMED = 0x0010000,
145 CTLADM_ARG_RELADR = 0x0020000,
146 CTLADM_ARG_RETRIES = 0x0040000,
147 CTLADM_ARG_ONOFFLINE = 0x0080000,
148 CTLADM_ARG_ONESHOT = 0x0100000,
149 CTLADM_ARG_TIMEOUT = 0x0200000,
150 CTLADM_ARG_INITIATOR = 0x0400000,
151 CTLADM_ARG_NOCOPY = 0x0800000,
152 CTLADM_ARG_NEED_TL = 0x1000000
158 ctladm_cmdargs argnum;
168 static const char rw_opts[] = "Nb:c:d:f:l:";
169 static const char startstop_opts[] = "io";
171 static struct ctladm_opts option_table[] = {
172 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
173 {"bbrread", CTLADM_CMD_BBRREAD, CTLADM_ARG_NEED_TL, "d:l:"},
174 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
175 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
176 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
177 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
178 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
179 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
180 {"getsync", CTLADM_CMD_GETSYNC, CTLADM_ARG_NEED_TL, NULL},
181 {"hardstart", CTLADM_CMD_HARDSTART, CTLADM_ARG_NONE, NULL},
182 {"hardstop", CTLADM_CMD_HARDSTOP, CTLADM_ARG_NONE, NULL},
183 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
184 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
185 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
186 {"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"},
187 {"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"},
188 {"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"},
189 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
190 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
191 {"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:s:"},
192 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
193 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
194 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
195 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
196 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
197 {"realsync", CTLADM_CMD_REALSYNC, CTLADM_ARG_NONE, NULL},
198 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
199 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
200 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
201 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
202 {"setsync", CTLADM_CMD_SETSYNC, CTLADM_ARG_NEED_TL, "i:"},
203 {"shutdown", CTLADM_CMD_SHUTDOWN, CTLADM_ARG_NONE, NULL},
204 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
205 {"startup", CTLADM_CMD_STARTUP, CTLADM_ARG_NONE, NULL},
206 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
207 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
208 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
209 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
210 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
211 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
216 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
217 ctladm_cmdargs *argnum, const char **subopt);
218 static int cctl_parse_tl(char *str, int *target, int *lun);
219 static int cctl_dump_ooa(int fd, int argc, char **argv);
220 static int cctl_port_dump(int fd, int quiet, int xml, int32_t fe_num,
221 ctl_port_type port_type);
222 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
223 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
224 static int cctl_delay(int fd, int target, int lun, int argc, char **argv,
226 static int cctl_lunlist(int fd);
227 static void cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len);
228 static void cctl_cfi_bbr_statusstr(cfi_bbrread_status, char *str, int str_len);
229 static int cctl_hardstopstart(int fd, ctladm_cmdfunction command);
230 static int cctl_bbrread(int fd, int target, int lun, int iid, int argc,
231 char **argv, char *combinedopt);
232 static int cctl_startup_shutdown(int fd, int target, int lun, int iid,
233 ctladm_cmdfunction command);
234 static int cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
235 int argc, char **argv, char *combinedopt);
236 static int cctl_start_stop(int fd, int target, int lun, int iid, int retries,
237 int start, int argc, char **argv, char *combinedopt);
238 static int cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
239 int argc, char **argv, char *combinedopt);
240 static int cctl_read_capacity(int fd, int target, int lun, int iid,
241 int retries, int argc, char **argv,
243 static int cctl_read_write(int fd, int target, int lun, int iid, int retries,
244 int argc, char **argv, char *combinedopt,
245 ctladm_cmdfunction command);
246 static int cctl_get_luns(int fd, int target, int lun, int iid, int retries,
247 struct scsi_report_luns_data **lun_data,
249 static int cctl_report_luns(int fd, int target, int lun, int iid, int retries);
250 static int cctl_tur(int fd, int target, int lun, int iid, int retries);
251 static int cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
252 char *path_str, int path_len,
253 struct scsi_inquiry_data *inq_data);
254 static int cctl_inquiry(int fd, int target, int lun, int iid, int retries);
255 static int cctl_req_sense(int fd, int target, int lun, int iid, int retries);
256 static int cctl_persistent_reserve_in(int fd, int target, int lun,
257 int initiator, int argc, char **argv,
258 char *combinedopt, int retry_count);
259 static int cctl_persistent_reserve_out(int fd, int target, int lun,
260 int initiator, int argc, char **argv,
261 char *combinedopt, int retry_count);
262 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
263 static int cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator);
264 static int cctl_report_target_port_group(int fd, int target, int lun,
266 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
269 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
270 ctladm_cmdargs *argnum, const char **subopt)
272 struct ctladm_opts *opts;
275 for (opts = table; (opts != NULL) && (opts->optname != NULL);
277 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
278 *cmdnum = opts->cmdnum;
279 *argnum = opts->argnum;
280 *subopt = opts->subopt;
282 if (strcmp(opts->optname, arg) == 0)
283 return (CC_OR_FOUND);
285 if (++num_matches > 1)
286 return(CC_OR_AMBIGUOUS);
293 return(CC_OR_NOT_FOUND);
298 cctl_parse_tl(char *str, int *target, int *lun)
305 while (isspace(*str) && (*str != '\0'))
308 tmpstr = (char *)strtok(str, ":");
309 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
310 *target = strtol(tmpstr, NULL, 0);
311 tmpstr = (char *)strtok(NULL, ":");
312 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
313 *lun = strtol(tmpstr, NULL, 0);
323 cctl_dump_ooa(int fd, int argc, char **argv)
326 long double cmd_latency;
327 int num_entries, len;
328 int target = -1, lun = -1;
335 && (isdigit(argv[2][0]))) {
336 retval = cctl_parse_tl(argv[2], &target, &lun);
338 warnx("invalid target:lun argument %s", argv[2]);
342 len = num_entries * sizeof(struct ctl_ooa_entry);
344 bzero(&ooa, sizeof(ooa));
346 ooa.entries = malloc(len);
348 if (ooa.entries == NULL) {
349 warn("%s: error mallocing %d bytes", __func__, len);
356 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
359 ooa.alloc_num = num_entries;
360 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
361 warn("%s: CTL_GET_OOA ioctl failed", __func__);
366 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
367 num_entries = num_entries * 2;
373 if (ooa.status != CTL_OOA_OK) {
374 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
380 fprintf(stdout, "Dumping OOA queues\n");
381 for (i = 0; i < ooa.fill_num; i++) {
382 struct ctl_ooa_entry *entry;
383 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
384 struct bintime delta_bt;
387 entry = &ooa.entries[i];
389 delta_bt = ooa.cur_bt;
390 bintime_sub(&delta_bt, &entry->start_bt);
391 bintime2timespec(&delta_bt, &ts);
392 cmd_latency = ts.tv_sec * 1000;
394 cmd_latency += ts.tv_nsec / 1000000;
396 fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
398 (intmax_t)entry->lun_num, entry->tag_num,
399 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
401 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
402 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
404 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
406 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
407 scsi_op_desc(entry->cdb[0], NULL),
408 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
411 fprintf(stdout, "OOA queues dump done\n");
413 if (ioctl(fd, CTL_DUMP_OOA) == -1) {
414 warn("%s: CTL_DUMP_OOA ioctl failed", __func__);
426 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
428 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
436 cctl_port_dump(int fd, int quiet, int xml, int32_t targ_port,
437 ctl_port_type port_type)
439 struct ctl_port_list port_list;
440 struct ctl_port_entry *entries;
441 struct sbuf *sb = NULL;
450 entries = malloc(sizeof(*entries) * num_entries);
451 bzero(&port_list, sizeof(port_list));
452 port_list.entries = entries;
453 port_list.alloc_num = num_entries;
454 port_list.alloc_len = num_entries * sizeof(*entries);
455 if (ioctl(fd, CTL_GET_PORT_LIST, &port_list) != 0) {
456 warn("%s: CTL_GET_PORT_LIST ioctl failed", __func__);
459 if (port_list.status == CTL_PORT_LIST_NEED_MORE_SPACE) {
460 printf("%s: allocated %d, need %d, retrying\n", __func__,
461 num_entries, port_list.fill_num + port_list.dropped_num);
463 num_entries = port_list.fill_num + port_list.dropped_num;
469 printf("Port Online Type Name pp vp %-18s %-18s\n",
473 sb = sbuf_new_auto();
474 sbuf_printf(sb, "<ctlfelist>\n");
476 for (i = 0; i < port_list.fill_num; i++) {
477 struct ctl_port_entry *entry;
482 switch (entry->port_type) {
492 case CTL_PORT_INTERNAL:
507 * If the user specified a frontend number or a particular
508 * frontend type, only print out that particular frontend
511 if ((targ_port != -1)
512 && (targ_port != entry->targ_port))
514 else if ((port_type != CTL_PORT_NONE)
515 && ((port_type & entry->port_type) == 0))
521 printf("Num: %ju Type: %s (%#x) Name: %s Physical Port: %d "
522 "Virtual Port: %d\n", (uintmax_t)entry->fe_num, type,
523 entry->port_type, entry->fe_name, entry->physical_port,
524 entry->virtual_port);
525 printf("WWNN %#jx WWPN %#jx Online: %s\n",
526 (uintmax_t)entry->wwnn, (uintmax_t)entry->wwpn,
527 (entry->online) ? "YES" : "NO" );
530 printf("%-4d %-6s %-8s %-12s %-2d %-2d %#-18jx "
532 entry->targ_port, (entry->online) ? "YES" : "NO",
533 type, entry->port_name, entry->physical_port,
534 entry->virtual_port, (uintmax_t)entry->wwnn,
535 (uintmax_t)entry->wwpn);
537 sbuf_printf(sb, "<targ_port id=\"%d\">\n",
539 sbuf_printf(sb, "<online>%s</online>\n",
540 (entry->online) ? "YES" : "NO");
541 sbuf_printf(sb, "<port_type>%s</port_type>\n", type);
542 sbuf_printf(sb, "<port_name>%s</port_name>\n",
544 sbuf_printf(sb, "<physical_port>%d</physical_port>\n",
545 entry->physical_port);
546 sbuf_printf(sb, "<virtual_port>%d</virtual_port>\n",
547 entry->virtual_port);
548 sbuf_printf(sb, "<wwnn>%#jx</wwnn>\n",
549 (uintmax_t)entry->wwnn);
550 sbuf_printf(sb, "<wwpn>%#jx</wwpn>\n",
551 (uintmax_t)entry->wwpn);
552 sbuf_printf(sb, "</targ_port>\n");
557 sbuf_printf(sb, "</ctlfelist>\n");
558 if (sbuf_finish(sb) != 0)
559 err(1, "%s: sbuf_finish", __func__);
560 printf("%s", sbuf_data(sb));
565 * Give some indication that we didn't find the frontend or
566 * frontend type requested by the user. We could print something
567 * out, but it would probably be better to hide that behind a
571 && ((targ_port != -1)
572 || (port_type != CTL_PORT_NONE)))
586 static struct ctladm_opts cctl_fe_table[] = {
587 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
588 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
589 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
590 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
591 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
596 cctl_port(int fd, int argc, char **argv, char *combinedopt)
599 int32_t targ_port = -1;
601 int wwnn_set = 0, wwpn_set = 0;
602 uint64_t wwnn = 0, wwpn = 0;
603 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
604 struct ctl_port_entry entry;
605 ctl_port_type port_type = CTL_PORT_NONE;
606 int quiet = 0, xml = 0;
608 while ((c = getopt(argc, argv, combinedopt)) != -1) {
611 if (port_mode != CCTL_PORT_MODE_NONE)
614 port_mode = CCTL_PORT_MODE_LIST;
617 if (port_mode != CCTL_PORT_MODE_NONE)
620 if (strcasecmp(optarg, "on") == 0)
621 port_mode = CCTL_PORT_MODE_ON;
622 else if (strcasecmp(optarg, "off") == 0)
623 port_mode = CCTL_PORT_MODE_OFF;
625 warnx("Invalid -o argument %s, \"on\" or "
626 "\"off\" are the only valid args",
633 targ_port = strtol(optarg, NULL, 0);
639 ctladm_optret optret;
640 ctladm_cmdargs argnum;
642 ctl_port_type tmp_port_type;
644 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
646 if (optret == CC_OR_AMBIGUOUS) {
647 warnx("%s: ambiguous frontend type %s",
651 } else if (optret == CC_OR_NOT_FOUND) {
652 warnx("%s: invalid frontend type %s",
658 port_type |= tmp_port_type;
662 if ((port_mode != CCTL_PORT_MODE_NONE)
663 && (port_mode != CCTL_PORT_MODE_SET))
666 port_mode = CCTL_PORT_MODE_SET;
668 wwnn = strtoull(optarg, NULL, 0);
672 if ((port_mode != CCTL_PORT_MODE_NONE)
673 && (port_mode != CCTL_PORT_MODE_SET))
676 port_mode = CCTL_PORT_MODE_SET;
678 wwpn = strtoull(optarg, NULL, 0);
688 * The user can specify either one or more frontend types (-t), or
689 * a specific frontend, but not both.
691 * If the user didn't specify a frontend type or number, set it to
692 * all. This is primarily needed for the enable/disable ioctls.
693 * This will be a no-op for the listing code. For the set ioctl,
694 * we'll throw an error, since that only works on one port at a time.
696 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
697 warnx("%s: can only specify one of -t or -n", __func__);
700 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
701 port_type = CTL_PORT_ALL;
703 bzero(&entry, sizeof(entry));
706 * These are needed for all but list/dump mode.
708 entry.port_type = port_type;
709 entry.targ_port = targ_port;
712 case CCTL_PORT_MODE_LIST:
713 cctl_port_dump(fd, quiet, xml, targ_port, port_type);
715 case CCTL_PORT_MODE_SET:
716 if (targ_port == -1) {
717 warnx("%s: -w and -W require -n", __func__);
723 entry.flags |= CTL_PORT_WWNN_VALID;
727 entry.flags |= CTL_PORT_WWPN_VALID;
731 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
732 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
737 case CCTL_PORT_MODE_ON:
738 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
739 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
743 fprintf(stdout, "Front End Ports enabled\n");
745 case CCTL_PORT_MODE_OFF:
746 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
747 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
751 fprintf(stdout, "Front End Ports disabled\n");
754 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
765 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
770 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
773 if (ioctl(fd, CTL_IO, io) == -1) {
774 warn("%s: error sending CTL_IO ioctl", func);
777 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
784 cctl_delay(int fd, int target, int lun, int argc, char **argv,
787 struct ctl_io_delay_info delay_info;
788 char *delayloc = NULL;
789 char *delaytype = NULL;
796 memset(&delay_info, 0, sizeof(delay_info));
798 while ((c = getopt(argc, argv, combinedopt)) != -1) {
801 delaytype = strdup(optarg);
804 delayloc = strdup(optarg);
807 delaytime = strtoul(optarg, NULL, 0);
812 if (delaytime == -1) {
813 warnx("%s: you must specify the delaytime with -t", __func__);
818 if (strcasecmp(delayloc, "datamove") == 0)
819 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
820 else if (strcasecmp(delayloc, "done") == 0)
821 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
823 warnx("%s: invalid delay location %s", __func__, delayloc);
828 if ((delaytype == NULL)
829 || (strcmp(delaytype, "oneshot") == 0))
830 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
831 else if (strcmp(delaytype, "cont") == 0)
832 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
834 warnx("%s: invalid delay type %s", __func__, delaytype);
839 delay_info.target_id = target;
840 delay_info.lun_id = lun;
841 delay_info.delay_secs = delaytime;
843 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
844 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
848 switch (delay_info.status) {
849 case CTL_DELAY_STATUS_NONE:
850 warnx("%s: no delay status??", __func__);
853 case CTL_DELAY_STATUS_OK:
855 case CTL_DELAY_STATUS_INVALID_LUN:
856 warnx("%s: invalid lun %d", __func__, lun);
859 case CTL_DELAY_STATUS_INVALID_TYPE:
860 warnx("%s: invalid delay type %d", __func__,
861 delay_info.delay_type);
864 case CTL_DELAY_STATUS_INVALID_LOC:
865 warnx("%s: delay location %s not implemented?", __func__,
869 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
870 warnx("%s: delay not implemented in the kernel", __func__);
871 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
875 warnx("%s: unknown delay return status %d", __func__,
882 /* delayloc should never be NULL, but just in case...*/
883 if (delayloc != NULL)
890 cctl_realsync(int fd, int argc, char **argv)
899 warnx("%s %s takes exactly one argument", argv[0], argv[1]);
906 if (strncasecmp(syncarg, "query", min(strlen(syncarg),
907 strlen("query"))) == 0) {
908 if (ioctl(fd, CTL_REALSYNC_GET, &syncstate) == -1) {
909 warn("%s: CTL_REALSYNC_GET ioctl failed", __func__);
913 fprintf(stdout, "SYNCHRONIZE CACHE support is: ");
916 fprintf(stdout, "OFF\n");
919 fprintf(stdout, "ON\n");
922 fprintf(stdout, "unknown (%d)\n", syncstate);
926 } else if (strcasecmp(syncarg, "on") == 0) {
928 } else if (strcasecmp(syncarg, "off") == 0) {
931 warnx("%s: invalid realsync argument %s", __func__, syncarg);
936 if (ioctl(fd, CTL_REALSYNC_SET, &syncstate) == -1) {
937 warn("%s: CTL_REALSYNC_SET ioctl failed", __func__);
946 cctl_getsetsync(int fd, int target, int lun, ctladm_cmdfunction command,
947 int argc, char **argv, char *combinedopt)
949 struct ctl_sync_info sync_info;
951 int sync_interval = -1;
957 memset(&sync_info, 0, sizeof(sync_info));
958 sync_info.target_id = target;
959 sync_info.lun_id = lun;
961 while ((c = getopt(argc, argv, combinedopt)) != -1) {
964 sync_interval = strtoul(optarg, NULL, 0);
971 if (command == CTLADM_CMD_SETSYNC) {
972 if (sync_interval == -1) {
973 warnx("%s: you must specify the sync interval with -i",
978 sync_info.sync_interval = sync_interval;
979 ioctl_cmd = CTL_SETSYNC;
981 ioctl_cmd = CTL_GETSYNC;
984 if (ioctl(fd, ioctl_cmd, &sync_info) == -1) {
985 warn("%s: CTL_%sSYNC ioctl failed", __func__,
986 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET");
991 switch (sync_info.status) {
993 if (command == CTLADM_CMD_GETSYNC) {
994 fprintf(stdout, "%d:%d: sync interval: %d\n",
995 target, lun, sync_info.sync_interval);
998 case CTL_GS_SYNC_NO_LUN:
999 warnx("%s: unknown target:LUN %d:%d", __func__, target, lun);
1002 case CTL_GS_SYNC_NONE:
1004 warnx("%s: unknown CTL_%sSYNC status %d", __func__,
1005 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET",
1014 static struct ctladm_opts cctl_err_types[] = {
1015 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
1016 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
1017 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
1018 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
1023 static struct ctladm_opts cctl_err_patterns[] = {
1024 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
1025 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
1026 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1027 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1028 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
1029 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
1030 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
1032 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
1038 cctl_error_inject(int fd, uint32_t target, uint32_t lun, int argc, char **argv,
1042 struct ctl_error_desc err_desc;
1045 uint64_t delete_id = 0;
1046 int delete_id_set = 0;
1052 bzero(&err_desc, sizeof(err_desc));
1053 err_desc.target_id = target;
1054 err_desc.lun_id = lun;
1056 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1062 delete_id = strtoull(optarg, NULL, 0);
1067 ctladm_optret optret;
1068 ctladm_cmdargs argnum;
1072 ctl_lun_error err_type;
1074 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
1075 warnx("%s: can't specify multiple -i "
1076 "arguments", __func__);
1080 optret = getoption(cctl_err_types, optarg,
1081 &err_type, &argnum, &subopt);
1082 err_desc.lun_error = err_type;
1084 ctl_lun_error_pattern pattern;
1086 optret = getoption(cctl_err_patterns, optarg,
1087 &pattern, &argnum, &subopt);
1088 err_desc.error_pattern |= pattern;
1091 if (optret == CC_OR_AMBIGUOUS) {
1092 warnx("%s: ambiguous argument %s", __func__,
1096 } else if (optret == CC_OR_NOT_FOUND) {
1097 warnx("%s: argument %s not found", __func__,
1105 char *tmpstr, *tmpstr2;
1107 tmpstr = strdup(optarg);
1108 if (tmpstr == NULL) {
1109 warn("%s: error duplicating string %s",
1115 tmpstr2 = strsep(&tmpstr, ",");
1116 if (tmpstr2 == NULL) {
1117 warnx("%s: invalid -r argument %s", __func__,
1123 lba = strtoull(tmpstr2, NULL, 0);
1124 tmpstr2 = strsep(&tmpstr, ",");
1125 if (tmpstr2 == NULL) {
1126 warnx("%s: no len argument for -r lba,len, got"
1127 " %s", __func__, optarg);
1132 len = strtoul(tmpstr2, NULL, 0);
1137 struct get_hook hook;
1140 sense_len = strtol(optarg, NULL, 0);
1141 if (sense_len <= 0) {
1142 warnx("invalid number of sense bytes %d",
1148 sense_len = MIN(sense_len, SSD_FULL_SIZE);
1150 hook.argc = argc - optind;
1151 hook.argv = argv + optind;
1154 sensestr = cget(&hook, NULL);
1155 if ((sensestr != NULL)
1156 && (sensestr[0] == '-')) {
1160 (uint8_t *)&err_desc.custom_sense,
1161 sense_len, sensestr, iget, &hook);
1171 if (delete_id_set != 0) {
1172 err_desc.serial = delete_id;
1173 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
1174 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
1181 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
1182 warnx("%s: error injection command (-i) needed",
1186 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
1187 && (sense_len == 0)) {
1188 warnx("%s: custom error requires -s", __func__);
1193 if (continuous != 0)
1194 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
1197 * If fd_sense is set, we need to read the sense data the user
1198 * wants returned from stdin.
1200 if (fd_sense == 1) {
1202 int amt_to_read = sense_len;
1203 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
1205 for (amt_read = 0; amt_to_read > 0;
1206 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1207 if (amt_read == -1) {
1208 warn("error reading sense data from stdin");
1212 amt_to_read -= amt_read;
1213 buf_ptr += amt_read;
1217 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1218 warnx("%s: command pattern (-p) needed", __func__);
1224 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1226 * We could check here to see whether it's a read/write
1227 * command, but that will be pointless once we allow
1228 * custom patterns. At that point, the user could specify
1229 * a READ(6) CDB type, and we wouldn't have an easy way here
1230 * to verify whether range checking is possible there. The
1231 * user will just figure it out when his error never gets
1235 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1236 warnx("%s: need read and/or write pattern if range "
1237 "is specified", __func__);
1242 err_desc.lba_range.lba = lba;
1243 err_desc.lba_range.len = len;
1246 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1247 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1250 printf("Error injection succeeded, serial number is %ju\n",
1251 (uintmax_t)err_desc.serial);
1259 cctl_lunlist(int fd)
1261 struct scsi_report_luns_data *lun_data;
1262 struct scsi_inquiry_data *inq_data;
1276 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1277 * if we ever acquire the ability to have multiple targets.
1279 if ((retval = cctl_get_luns(fd, target, /*lun*/ 0, initid,
1280 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1283 inq_data = malloc(sizeof(*inq_data));
1284 if (inq_data == NULL) {
1285 warn("%s: couldn't allocate memory for inquiry data\n",
1290 for (i = 0; i < num_luns; i++) {
1294 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1295 case RPL_LUNDATA_ATYP_PERIPH:
1296 lun_val = lun_data->luns[i].lundata[1];
1298 case RPL_LUNDATA_ATYP_FLAT:
1299 lun_val = (lun_data->luns[i].lundata[0] &
1300 RPL_LUNDATA_FLAT_LUN_MASK) |
1301 (lun_data->luns[i].lundata[1] <<
1302 RPL_LUNDATA_FLAT_LUN_BITS);
1304 case RPL_LUNDATA_ATYP_LUN:
1305 case RPL_LUNDATA_ATYP_EXTLUN:
1307 fprintf(stdout, "Unsupported LUN format %d\n",
1308 lun_data->luns[i].lundata[0] &
1309 RPL_LUNDATA_ATYP_MASK);
1316 if ((retval = cctl_get_inquiry(fd, target, lun_val, initid,
1317 /*retries*/ 2, scsi_path,
1322 printf("%s", scsi_path);
1323 scsi_print_inquiry(inq_data);
1327 if (lun_data != NULL)
1330 if (inq_data != NULL)
1337 cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len)
1340 case CFI_MT_PORT_OFFLINE:
1341 snprintf(str, str_len, "Port Offline");
1344 snprintf(str, str_len, "Error");
1346 case CFI_MT_SUCCESS:
1347 snprintf(str, str_len, "Success");
1350 snprintf(str, str_len, "None??");
1353 snprintf(str, str_len, "Unknown status: %d", status);
1359 cctl_cfi_bbr_statusstr(cfi_bbrread_status status, char *str, int str_len)
1362 case CFI_BBR_SUCCESS:
1363 snprintf(str, str_len, "Success");
1365 case CFI_BBR_LUN_UNCONFIG:
1366 snprintf(str, str_len, "LUN not configured");
1368 case CFI_BBR_NO_LUN:
1369 snprintf(str, str_len, "LUN does not exist");
1371 case CFI_BBR_NO_MEM:
1372 snprintf(str, str_len, "Memory allocation error");
1374 case CFI_BBR_BAD_LEN:
1375 snprintf(str, str_len, "Length is not a multiple of blocksize");
1377 case CFI_BBR_RESERV_CONFLICT:
1378 snprintf(str, str_len, "Reservation conflict");
1380 case CFI_BBR_LUN_STOPPED:
1381 snprintf(str, str_len, "LUN is powered off");
1383 case CFI_BBR_LUN_OFFLINE_CTL:
1384 snprintf(str, str_len, "LUN is offline");
1386 case CFI_BBR_LUN_OFFLINE_RC:
1387 snprintf(str, str_len, "RAIDCore array is offline (double "
1390 case CFI_BBR_SCSI_ERROR:
1391 snprintf(str, str_len, "SCSI Error");
1394 snprintf(str, str_len, "Error");
1397 snprintf(str, str_len, "Unknown status: %d", status);
1403 cctl_hardstopstart(int fd, ctladm_cmdfunction command)
1405 struct ctl_hard_startstop_info hs_info;
1406 char error_str[256];
1412 if (command == CTLADM_CMD_HARDSTART)
1417 if (ioctl(fd, (do_start == 1) ? CTL_HARD_START : CTL_HARD_STOP,
1419 warn("%s: CTL_HARD_%s ioctl failed", __func__,
1420 (do_start == 1) ? "START" : "STOP");
1425 fprintf(stdout, "Hard %s Status: ", (command == CTLADM_CMD_HARDSTOP) ?
1427 cctl_cfi_mt_statusstr(hs_info.status, error_str, sizeof(error_str));
1428 fprintf(stdout, "%s\n", error_str);
1429 fprintf(stdout, "Total LUNs: %d\n", hs_info.total_luns);
1430 fprintf(stdout, "LUNs complete: %d\n", hs_info.luns_complete);
1431 fprintf(stdout, "LUNs failed: %d\n", hs_info.luns_failed);
1438 cctl_bbrread(int fd, int target __unused, int lun, int iid __unused,
1439 int argc, char **argv, char *combinedopt)
1441 struct ctl_bbrread_info bbr_info;
1442 char error_str[256];
1451 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1454 datalen = strtoul(optarg, NULL, 0);
1457 lba = strtoull(optarg, NULL, 0);
1466 warnx("%s: you must specify an LBA with -l", __func__);
1471 if (datalen == -1) {
1472 warnx("%s: you must specify a length with -d", __func__);
1477 bbr_info.lun_num = lun;
1480 * XXX KDM get the blocksize first??
1482 if ((datalen % 512) != 0) {
1483 warnx("%s: data length %d is not a multiple of 512 bytes",
1488 bbr_info.len = datalen;
1490 if (ioctl(fd, CTL_BBRREAD, &bbr_info) == -1) {
1491 warn("%s: CTL_BBRREAD ioctl failed", __func__);
1495 cctl_cfi_mt_statusstr(bbr_info.status, error_str, sizeof(error_str));
1496 fprintf(stdout, "BBR Read Overall Status: %s\n", error_str);
1497 cctl_cfi_bbr_statusstr(bbr_info.bbr_status, error_str,
1499 fprintf(stdout, "BBR Read Status: %s\n", error_str);
1501 * XXX KDM should we bother printing out SCSI status if we get
1502 * CFI_BBR_SCSI_ERROR back?
1504 * Return non-zero if this fails?
1511 cctl_startup_shutdown(int fd, int target, int lun, int iid,
1512 ctladm_cmdfunction command)
1516 struct scsi_report_luns_data *lun_data;
1517 struct scsi_inquiry_data *inq_data;
1527 * - step through each lun, do an inquiry
1528 * - check OOA queue on direct access luns
1529 * - send stop with offline bit to each direct access device with a
1531 * - if we get a reservation conflict, reset the LUN to clear it
1532 * and reissue the stop with the offline bit set
1537 io = ctl_scsi_alloc_io(id);
1539 warnx("%s: can't allocate memory", __func__);
1543 if ((retval = cctl_get_luns(fd, target, lun, iid, /*retries*/ 2,
1544 &lun_data, &num_luns)) != 0)
1547 inq_data = malloc(sizeof(*inq_data));
1548 if (inq_data == NULL) {
1549 warn("%s: couldn't allocate memory for inquiry data\n",
1554 for (i = 0; i < num_luns; i++) {
1559 * XXX KDM figure out a way to share this code with
1562 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1563 case RPL_LUNDATA_ATYP_PERIPH:
1564 lun_val = lun_data->luns[i].lundata[1];
1566 case RPL_LUNDATA_ATYP_FLAT:
1567 lun_val = (lun_data->luns[i].lundata[0] &
1568 RPL_LUNDATA_FLAT_LUN_MASK) |
1569 (lun_data->luns[i].lundata[1] <<
1570 RPL_LUNDATA_FLAT_LUN_BITS);
1572 case RPL_LUNDATA_ATYP_LUN:
1573 case RPL_LUNDATA_ATYP_EXTLUN:
1575 fprintf(stdout, "Unsupported LUN format %d\n",
1576 lun_data->luns[i].lundata[0] &
1577 RPL_LUNDATA_ATYP_MASK);
1584 if ((retval = cctl_get_inquiry(fd, target, lun_val, iid,
1585 /*retries*/ 2, scsi_path,
1590 printf("%s", scsi_path);
1591 scsi_print_inquiry(inq_data);
1593 * We only want to shutdown direct access devices.
1595 if (SID_TYPE(inq_data) != T_DIRECT) {
1596 printf("%s LUN is not direct access, skipped\n",
1601 if (command == CTLADM_CMD_SHUTDOWN) {
1602 struct ctl_ooa_info ooa_info;
1604 ooa_info.target_id = target;
1605 ooa_info.lun_id = lun_val;
1607 if (ioctl(fd, CTL_CHECK_OOA, &ooa_info) == -1) {
1608 printf("%s CTL_CHECK_OOA ioctl failed\n",
1613 if (ooa_info.status != CTL_OOA_SUCCESS) {
1614 printf("%s CTL_CHECK_OOA returned status %d\n",
1615 scsi_path, ooa_info.status);
1618 if (ooa_info.num_entries != 0) {
1619 printf("%s %d entr%s in the OOA queue, "
1620 "skipping shutdown\n", scsi_path,
1621 ooa_info.num_entries,
1622 (ooa_info.num_entries > 1)?"ies" : "y" );
1627 ctl_scsi_start_stop(/*io*/ io,
1628 /*start*/(command == CTLADM_CMD_STARTUP) ?
1632 /*power_conditions*/ SSS_PC_START_VALID,
1635 (command == CTLADM_CMD_STARTUP) ?
1636 CTL_TAG_SIMPLE :CTL_TAG_ORDERED,
1639 io->io_hdr.nexus.targ_target.id = target;
1640 io->io_hdr.nexus.targ_lun = lun_val;
1641 io->io_hdr.nexus.initid = id;
1643 if (cctl_do_io(fd, /*retries*/ 3, io, __func__) != 0) {
1648 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1649 ctl_io_error_print(io, inq_data, stderr);
1651 printf("%s LUN is now %s\n", scsi_path,
1652 (command == CTLADM_CMD_STARTUP) ? "online" :
1657 if (lun_data != NULL)
1660 if (inq_data != NULL)
1664 ctl_scsi_free_io(io);
1670 cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
1671 int argc, char **argv, char *combinedopt)
1677 uint64_t our_lba = 0;
1678 uint32_t our_block_count = 0;
1679 int reladr = 0, immed = 0;
1685 io = ctl_scsi_alloc_io(id);
1687 warnx("%s: can't allocate memory", __func__);
1691 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1694 our_block_count = strtoul(optarg, NULL, 0);
1697 cdb_size = strtol(optarg, NULL, 0);
1703 our_lba = strtoull(optarg, NULL, 0);
1713 if (cdb_size != -1) {
1719 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1720 "and 16", __func__, cdb_size);
1723 break; /* NOTREACHED */
1728 ctl_scsi_sync_cache(/*io*/ io,
1731 /*minimum_cdb_size*/ cdb_size,
1732 /*starting_lba*/ our_lba,
1733 /*block_count*/ our_block_count,
1734 /*tag_type*/ CTL_TAG_SIMPLE,
1737 io->io_hdr.nexus.targ_target.id = target;
1738 io->io_hdr.nexus.targ_lun = lun;
1739 io->io_hdr.nexus.initid = id;
1741 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1746 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1747 fprintf(stdout, "Cache synchronized successfully\n");
1749 ctl_io_error_print(io, NULL, stderr);
1751 ctl_scsi_free_io(io);
1757 cctl_start_stop(int fd, int target, int lun, int iid, int retries, int start,
1758 int argc, char **argv, char *combinedopt)
1763 int immed = 0, onoffline = 0;
1769 io = ctl_scsi_alloc_io(id);
1771 warnx("%s: can't allocate memory", __func__);
1775 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1788 * Use an ordered tag for the stop command, to guarantee that any
1789 * pending I/O will finish before the stop command executes. This
1790 * would normally be the case anyway, since CTL will basically
1791 * treat the start/stop command as an ordered command with respect
1792 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1794 ctl_scsi_start_stop(/*io*/ io,
1797 /*immediate*/ immed,
1798 /*power_conditions*/ SSS_PC_START_VALID,
1799 /*onoffline*/ onoffline,
1800 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1804 io->io_hdr.nexus.targ_target.id = target;
1805 io->io_hdr.nexus.targ_lun = lun;
1806 io->io_hdr.nexus.initid = id;
1808 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1813 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1814 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1815 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1816 (start) ? "started" : "stopped");
1818 ctl_io_error_print(io, NULL, stderr);
1821 ctl_scsi_free_io(io);
1827 cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
1828 int argc, char **argv, char *combinedopt)
1834 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1844 io = ctl_scsi_alloc_io(id);
1846 warn("%s: can't allocate memory", __func__);
1850 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1853 pc = strtoul(optarg, NULL, 0);
1856 subpage = strtoul(optarg, NULL, 0);
1865 page_code = strtoul(optarg, NULL, 0);
1868 cdbsize = strtol(optarg, NULL, 0);
1875 if (((list == 0) && (page_code == -1))
1876 || ((list != 0) && (page_code != -1))) {
1877 warnx("%s: you must specify either a page code (-m) or -l",
1883 if ((page_code != -1)
1884 && ((page_code > SMS_ALL_PAGES_PAGE)
1885 || (page_code < 0))) {
1886 warnx("%s: page code %d is out of range", __func__,
1893 page_code = SMS_ALL_PAGES_PAGE;
1895 warnx("%s: arg -P makes no sense with -l",
1900 if (subpage != -1) {
1901 warnx("%s: arg -S makes no sense with -l", __func__);
1908 pc = SMS_PAGE_CTRL_CURRENT;
1912 warnx("%s: page control value %d is out of range: 0-3",
1922 || (subpage < 0))) {
1923 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1934 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1935 "and 10", __func__, cdbsize);
1951 dataptr = (uint8_t *)malloc(datalen);
1952 if (dataptr == NULL) {
1953 warn("%s: can't allocate %d bytes", __func__, datalen);
1958 memset(dataptr, 0, datalen);
1960 ctl_scsi_mode_sense(io,
1961 /*data_ptr*/ dataptr,
1962 /*data_len*/ datalen,
1965 /*page_code*/ page_code,
1967 /*subpage*/ subpage,
1968 /*minimum_cdb_size*/ cdbsize,
1969 /*tag_type*/ CTL_TAG_SIMPLE,
1972 io->io_hdr.nexus.targ_target.id = target;
1973 io->io_hdr.nexus.targ_lun = lun;
1974 io->io_hdr.nexus.initid = id;
1976 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1981 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1982 int pages_len, used_len;
1983 uint32_t returned_len;
1986 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1987 struct scsi_mode_hdr_6 *hdr6;
1990 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1992 returned_len = hdr6->datalen + 1;
1993 bdlen = hdr6->block_descr_len;
1995 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1997 struct scsi_mode_hdr_10 *hdr10;
2000 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
2002 returned_len = scsi_2btoul(hdr10->datalen) + 2;
2003 bdlen = scsi_2btoul(hdr10->block_descr_len);
2005 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
2007 /* just in case they can give us more than we allocated for */
2008 returned_len = min(returned_len, datalen);
2009 pages_len = returned_len - (ndataptr - dataptr);
2011 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
2012 returned_len, pages_len);
2015 fprintf(stdout, "Supported mode pages:\n");
2016 for (used_len = 0; used_len < pages_len;) {
2017 struct scsi_mode_page_header *header;
2019 header = (struct scsi_mode_page_header *)
2020 &ndataptr[used_len];
2021 fprintf(stdout, "%d\n", header->page_code);
2022 used_len += header->page_length + 2;
2025 for (used_len = 0; used_len < pages_len; used_len++) {
2026 fprintf(stdout, "0x%x ", ndataptr[used_len]);
2027 if (((used_len+1) % 16) == 0)
2028 fprintf(stdout, "\n");
2030 fprintf(stdout, "\n");
2033 ctl_io_error_print(io, NULL, stderr);
2036 ctl_scsi_free_io(io);
2038 if (dataptr != NULL)
2045 cctl_read_capacity(int fd, int target, int lun, int iid, int retries,
2046 int argc, char **argv, char *combinedopt)
2050 struct scsi_read_capacity_data *data;
2051 struct scsi_read_capacity_data_long *longdata;
2052 int cdbsize = -1, retval;
2061 io = ctl_scsi_alloc_io(id);
2063 warn("%s: can't allocate memory\n", __func__);
2067 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2070 cdbsize = strtol(optarg, NULL, 0);
2076 if (cdbsize != -1) {
2082 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
2083 "and 16", __func__, cdbsize);
2086 break; /* NOTREACHED */
2091 dataptr = (uint8_t *)malloc(sizeof(*longdata));
2092 if (dataptr == NULL) {
2093 warn("%s: can't allocate %zd bytes\n", __func__,
2098 memset(dataptr, 0, sizeof(*longdata));
2104 ctl_scsi_read_capacity(io,
2105 /*data_ptr*/ dataptr,
2106 /*data_len*/ sizeof(*longdata),
2110 /*tag_type*/ CTL_TAG_SIMPLE,
2114 ctl_scsi_read_capacity_16(io,
2115 /*data_ptr*/ dataptr,
2116 /*data_len*/ sizeof(*longdata),
2120 /*tag_type*/ CTL_TAG_SIMPLE,
2125 io->io_hdr.nexus.initid = id;
2126 io->io_hdr.nexus.targ_target.id = target;
2127 io->io_hdr.nexus.targ_lun = lun;
2129 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2134 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2138 if (cdbsize == 10) {
2140 data = (struct scsi_read_capacity_data *)dataptr;
2142 maxlba = scsi_4btoul(data->addr);
2143 blocksize = scsi_4btoul(data->length);
2145 if (maxlba == 0xffffffff) {
2150 longdata=(struct scsi_read_capacity_data_long *)dataptr;
2152 maxlba = scsi_8btou64(longdata->addr);
2153 blocksize = scsi_4btoul(longdata->length);
2156 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
2157 (uintmax_t)maxlba, blocksize);
2159 ctl_io_error_print(io, NULL, stderr);
2162 ctl_scsi_free_io(io);
2164 if (dataptr != NULL)
2171 cctl_read_write(int fd, int target, int lun, int iid, int retries,
2172 int argc, char **argv, char *combinedopt,
2173 ctladm_cmdfunction command)
2177 int file_fd, do_stdio;
2178 int cdbsize = -1, databytes;
2180 char *filename = NULL;
2181 int datalen = -1, blocksize = -1;
2193 io = ctl_scsi_alloc_io(id);
2195 warn("%s: can't allocate memory\n", __func__);
2199 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2202 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
2205 blocksize = strtoul(optarg, NULL, 0);
2208 cdbsize = strtoul(optarg, NULL, 0);
2211 datalen = strtoul(optarg, NULL, 0);
2214 filename = strdup(optarg);
2217 lba = strtoull(optarg, NULL, 0);
2224 if (filename == NULL) {
2225 warnx("%s: you must supply a filename using -f", __func__);
2230 if (datalen == -1) {
2231 warnx("%s: you must specify the data length with -d", __func__);
2237 warnx("%s: you must specify the LBA with -l", __func__);
2242 if (blocksize == -1) {
2243 warnx("%s: you must specify the blocksize with -b", __func__);
2248 if (cdbsize != -1) {
2256 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
2257 "10, 12 or 16", __func__, cdbsize);
2260 break; /* NOTREACHED */
2265 databytes = datalen * blocksize;
2266 dataptr = (uint8_t *)malloc(databytes);
2268 if (dataptr == NULL) {
2269 warn("%s: can't allocate %d bytes\n", __func__, databytes);
2273 if (strcmp(filename, "-") == 0) {
2274 if (command == CTLADM_CMD_READ)
2275 file_fd = STDOUT_FILENO;
2277 file_fd = STDIN_FILENO;
2280 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
2281 if (file_fd == -1) {
2282 warn("%s: can't open file %s", __func__, filename);
2288 memset(dataptr, 0, databytes);
2290 if (command == CTLADM_CMD_WRITE) {
2293 bytes_read = read(file_fd, dataptr, databytes);
2294 if (bytes_read == -1) {
2295 warn("%s: error reading file %s", __func__, filename);
2299 if (bytes_read != databytes) {
2300 warnx("%s: only read %d bytes from file %s",
2301 __func__, bytes_read, filename);
2306 ctl_scsi_read_write(io,
2307 /*data_ptr*/ dataptr,
2308 /*data_len*/ databytes,
2309 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
2311 /*minimum_cdb_size*/ cdbsize,
2313 /*num_blocks*/ datalen,
2314 /*tag_type*/ CTL_TAG_SIMPLE,
2317 io->io_hdr.nexus.targ_target.id = target;
2318 io->io_hdr.nexus.targ_lun = lun;
2319 io->io_hdr.nexus.initid = id;
2321 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2326 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2327 && (command == CTLADM_CMD_READ)) {
2330 bytes_written = write(file_fd, dataptr, databytes);
2331 if (bytes_written == -1) {
2332 warn("%s: can't write to %s", __func__, filename);
2335 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
2336 ctl_io_error_print(io, NULL, stderr);
2341 ctl_scsi_free_io(io);
2343 if (dataptr != NULL)
2354 cctl_get_luns(int fd, int target, int lun, int iid, int retries, struct
2355 scsi_report_luns_data **lun_data, uint32_t *num_luns)
2366 io = ctl_scsi_alloc_io(id);
2368 warnx("%s: can't allocate memory", __func__);
2373 * lun_data includes space for 1 lun, allocate space for 4 initially.
2374 * If that isn't enough, we'll allocate more.
2378 lun_datalen = sizeof(*lun_data) +
2379 (nluns * sizeof(struct scsi_report_luns_lundata));
2380 *lun_data = malloc(lun_datalen);
2382 if (*lun_data == NULL) {
2383 warnx("%s: can't allocate memory", __func__);
2384 ctl_scsi_free_io(io);
2388 ctl_scsi_report_luns(io,
2389 /*data_ptr*/ (uint8_t *)*lun_data,
2390 /*data_len*/ lun_datalen,
2391 /*select_report*/ RPL_REPORT_ALL,
2392 /*tag_type*/ CTL_TAG_SIMPLE,
2395 io->io_hdr.nexus.initid = id;
2396 io->io_hdr.nexus.targ_target.id = target;
2397 io->io_hdr.nexus.targ_lun = lun;
2399 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2404 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2405 uint32_t returned_len, returned_luns;
2407 returned_len = scsi_4btoul((*lun_data)->length);
2408 returned_luns = returned_len / 8;
2409 if (returned_luns > nluns) {
2410 nluns = returned_luns;
2414 /* These should be the same */
2415 *num_luns = MIN(returned_luns, nluns);
2417 ctl_io_error_print(io, NULL, stderr);
2421 ctl_scsi_free_io(io);
2427 cctl_report_luns(int fd, int target, int lun, int iid, int retries)
2429 struct scsi_report_luns_data *lun_data;
2430 uint32_t num_luns, i;
2435 if ((retval = cctl_get_luns(fd, target, lun, iid, retries, &lun_data,
2439 fprintf(stdout, "%u LUNs returned\n", num_luns);
2440 for (i = 0; i < num_luns; i++) {
2444 * XXX KDM figure out a way to share this code with
2447 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
2448 case RPL_LUNDATA_ATYP_PERIPH:
2449 lun_val = lun_data->luns[i].lundata[1];
2451 case RPL_LUNDATA_ATYP_FLAT:
2452 lun_val = (lun_data->luns[i].lundata[0] &
2453 RPL_LUNDATA_FLAT_LUN_MASK) |
2454 (lun_data->luns[i].lundata[1] <<
2455 RPL_LUNDATA_FLAT_LUN_BITS);
2457 case RPL_LUNDATA_ATYP_LUN:
2458 case RPL_LUNDATA_ATYP_EXTLUN:
2460 fprintf(stdout, "Unsupported LUN format %d\n",
2461 lun_data->luns[i].lundata[0] &
2462 RPL_LUNDATA_ATYP_MASK);
2469 fprintf(stdout, "%d\n", lun_val);
2473 if (lun_data != NULL)
2480 cctl_tur(int fd, int target, int lun, int iid, int retries)
2487 io = ctl_scsi_alloc_io(id);
2489 fprintf(stderr, "can't allocate memory\n");
2494 /* tag_type */ CTL_TAG_SIMPLE,
2497 io->io_hdr.nexus.targ_target.id = target;
2498 io->io_hdr.nexus.targ_lun = lun;
2499 io->io_hdr.nexus.initid = id;
2501 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2502 ctl_scsi_free_io(io);
2506 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2507 fprintf(stdout, "Unit is ready\n");
2509 ctl_io_error_print(io, NULL, stderr);
2515 cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
2516 char *path_str, int path_len,
2517 struct scsi_inquiry_data *inq_data)
2527 io = ctl_scsi_alloc_io(id);
2529 warnx("cctl_inquiry: can't allocate memory\n");
2533 ctl_scsi_inquiry(/*io*/ io,
2534 /*data_ptr*/ (uint8_t *)inq_data,
2535 /*data_len*/ sizeof(*inq_data),
2538 /*tag_type*/ CTL_TAG_SIMPLE,
2541 io->io_hdr.nexus.targ_target.id = target;
2542 io->io_hdr.nexus.targ_lun = lun;
2543 io->io_hdr.nexus.initid = id;
2545 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2550 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
2552 ctl_io_error_print(io, NULL, stderr);
2553 } else if (path_str != NULL)
2554 ctl_scsi_path_string(io, path_str, path_len);
2557 ctl_scsi_free_io(io);
2563 cctl_inquiry(int fd, int target, int lun, int iid, int retries)
2565 struct scsi_inquiry_data *inq_data;
2571 inq_data = malloc(sizeof(*inq_data));
2572 if (inq_data == NULL) {
2573 warnx("%s: can't allocate inquiry data", __func__);
2578 if ((retval = cctl_get_inquiry(fd, target, lun, iid, retries, scsi_path,
2579 sizeof(scsi_path), inq_data)) != 0)
2582 printf("%s", scsi_path);
2583 scsi_print_inquiry(inq_data);
2586 if (inq_data != NULL)
2593 cctl_req_sense(int fd, int target, int lun, int iid, int retries)
2596 struct scsi_sense_data *sense_data;
2604 io = ctl_scsi_alloc_io(id);
2606 warnx("cctl_req_sense: can't allocate memory\n");
2609 sense_data = malloc(sizeof(*sense_data));
2610 memset(sense_data, 0, sizeof(*sense_data));
2612 ctl_scsi_request_sense(/*io*/ io,
2613 /*data_ptr*/ (uint8_t *)sense_data,
2614 /*data_len*/ sizeof(*sense_data),
2616 /*tag_type*/ CTL_TAG_SIMPLE,
2619 io->io_hdr.nexus.targ_target.id = target;
2620 io->io_hdr.nexus.targ_lun = lun;
2621 io->io_hdr.nexus.initid = id;
2623 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2628 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2629 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2630 io->scsiio.sense_len = sizeof(*sense_data);
2631 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2633 ctl_io_error_print(io, NULL, stderr);
2637 ctl_scsi_free_io(io);
2644 cctl_report_target_port_group(int fd, int target, int lun, int initiator)
2656 io = ctl_scsi_alloc_io(id);
2658 warn("%s: can't allocate memory", __func__);
2663 dataptr = (uint8_t *)malloc(datalen);
2664 if (dataptr == NULL) {
2665 warn("%s: can't allocate %d bytes", __func__, datalen);
2670 memset(dataptr, 0, datalen);
2672 ctl_scsi_maintenance_in(/*io*/ io,
2673 /*data_ptr*/ dataptr,
2674 /*data_len*/ datalen,
2675 /*action*/ SA_RPRT_TRGT_GRP,
2676 /*tag_type*/ CTL_TAG_SIMPLE,
2679 io->io_hdr.nexus.targ_target.id = target;
2680 io->io_hdr.nexus.targ_lun = lun;
2681 io->io_hdr.nexus.initid = id;
2683 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2688 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2689 int returned_len, used_len;
2691 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2693 for (used_len = 0; used_len < returned_len; used_len++) {
2694 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2695 if (((used_len+1) % 8) == 0)
2696 fprintf(stdout, "\n");
2698 fprintf(stdout, "\n");
2700 ctl_io_error_print(io, NULL, stderr);
2703 ctl_scsi_free_io(io);
2705 if (dataptr != NULL)
2712 cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator)
2724 io = ctl_scsi_alloc_io(id);
2726 warn("%s: can't allocate memory", __func__);
2731 dataptr = (uint8_t *)malloc(datalen);
2732 if (dataptr == NULL) {
2733 warn("%s: can't allocate %d bytes", __func__, datalen);
2738 memset(dataptr, 0, datalen);
2740 ctl_scsi_inquiry(/*io*/ io,
2741 /*data_ptr*/ dataptr,
2742 /*data_len*/ datalen,
2744 /*page_code*/ SVPD_DEVICE_ID,
2745 /*tag_type*/ CTL_TAG_SIMPLE,
2748 io->io_hdr.nexus.targ_target.id = target;
2749 io->io_hdr.nexus.targ_lun = lun;
2750 io->io_hdr.nexus.initid = id;
2752 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2757 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2758 int returned_len, used_len;
2760 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2762 for (used_len = 0; used_len < returned_len; used_len++) {
2763 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2764 if (((used_len+1) % 8) == 0)
2765 fprintf(stdout, "\n");
2767 fprintf(stdout, "\n");
2769 ctl_io_error_print(io, NULL, stderr);
2772 ctl_scsi_free_io(io);
2774 if (dataptr != NULL)
2781 cctl_persistent_reserve_in(int fd, int target, int lun, int initiator,
2782 int argc, char **argv, char *combinedopt,
2797 io = ctl_scsi_alloc_io(id);
2799 warn("%s: can't allocate memory", __func__);
2803 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2806 action = strtol(optarg, NULL, 0);
2813 if (action < 0 || action > 2) {
2814 warn("action must be specified and in the range: 0-2");
2821 dataptr = (uint8_t *)malloc(datalen);
2822 if (dataptr == NULL) {
2823 warn("%s: can't allocate %d bytes", __func__, datalen);
2828 memset(dataptr, 0, datalen);
2830 ctl_scsi_persistent_res_in(io,
2831 /*data_ptr*/ dataptr,
2832 /*data_len*/ datalen,
2834 /*tag_type*/ CTL_TAG_SIMPLE,
2837 io->io_hdr.nexus.targ_target.id = target;
2838 io->io_hdr.nexus.targ_lun = lun;
2839 io->io_hdr.nexus.initid = id;
2841 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2846 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2847 int returned_len, used_len;
2853 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2854 returned_len = min(returned_len, 256);
2857 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2863 warnx("%s: invalid action %d", __func__, action);
2865 break; /* NOTREACHED */
2868 for (used_len = 0; used_len < returned_len; used_len++) {
2869 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2870 if (((used_len+1) % 8) == 0)
2871 fprintf(stdout, "\n");
2873 fprintf(stdout, "\n");
2875 ctl_io_error_print(io, NULL, stderr);
2878 ctl_scsi_free_io(io);
2880 if (dataptr != NULL)
2887 cctl_persistent_reserve_out(int fd, int target, int lun, int initiator,
2888 int argc, char **argv, char *combinedopt,
2894 uint64_t key = 0, sa_key = 0;
2895 int action = -1, restype = -1;
2904 io = ctl_scsi_alloc_io(id);
2906 warn("%s: can't allocate memory", __func__);
2910 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2913 action = strtol(optarg, NULL, 0);
2916 key = strtoull(optarg, NULL, 0);
2919 restype = strtol(optarg, NULL, 0);
2922 sa_key = strtoull(optarg, NULL, 0);
2928 if (action < 0 || action > 5) {
2929 warn("action must be specified and in the range: 0-5");
2934 if (restype < 0 || restype > 5) {
2935 if (action != 0 && action != 5 && action != 3) {
2936 warn("'restype' must specified and in the range: 0-5");
2943 dataptr = (uint8_t *)malloc(datalen);
2944 if (dataptr == NULL) {
2945 warn("%s: can't allocate %d bytes", __func__, datalen);
2950 memset(dataptr, 0, datalen);
2952 ctl_scsi_persistent_res_out(io,
2953 /*data_ptr*/ dataptr,
2954 /*data_len*/ datalen,
2959 /*tag_type*/ CTL_TAG_SIMPLE,
2962 io->io_hdr.nexus.targ_target.id = target;
2963 io->io_hdr.nexus.targ_lun = lun;
2964 io->io_hdr.nexus.initid = id;
2966 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2970 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2972 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2973 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2974 "successfully\n", scsi_path);
2976 ctl_io_error_print(io, NULL, stderr);
2979 ctl_scsi_free_io(io);
2981 if (dataptr != NULL)
2987 struct cctl_req_option {
2992 STAILQ_ENTRY(cctl_req_option) links;
2996 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2998 struct ctl_lun_req req;
2999 int device_type = -1;
3000 uint64_t lun_size = 0;
3001 uint32_t blocksize = 0, req_lun_id = 0;
3002 char *serial_num = NULL;
3003 char *device_id = NULL;
3004 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
3005 char *backend_name = NULL;
3006 STAILQ_HEAD(, cctl_req_option) option_list;
3007 int num_options = 0;
3010 STAILQ_INIT(&option_list);
3012 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3015 backend_name = strdup(optarg);
3018 blocksize = strtoul(optarg, NULL, 0);
3022 device_id = strdup(optarg);
3025 req_lun_id = strtoul(optarg, NULL, 0);
3029 struct cctl_req_option *option;
3033 tmpstr = strdup(optarg);
3034 name = strsep(&tmpstr, "=");
3036 warnx("%s: option -o takes \"name=value\""
3037 "argument", __func__);
3041 value = strsep(&tmpstr, "=");
3042 if (value == NULL) {
3043 warnx("%s: option -o takes \"name=value\""
3044 "argument", __func__);
3048 option = malloc(sizeof(*option));
3049 if (option == NULL) {
3050 warn("%s: error allocating %zd bytes",
3051 __func__, sizeof(*option));
3055 option->name = strdup(name);
3056 option->namelen = strlen(name) + 1;
3057 option->value = strdup(value);
3058 option->vallen = strlen(value) + 1;
3061 STAILQ_INSERT_TAIL(&option_list, option, links);
3066 if (strcasecmp(optarg, "auto") != 0) {
3067 retval = expand_number(optarg, &lun_size);
3069 warn("%s: invalid -s argument",
3078 serial_num = strdup(optarg);
3081 device_type = strtoul(optarg, NULL, 0);
3088 if (backend_name == NULL) {
3089 warnx("%s: backend name (-b) must be specified", __func__);
3094 bzero(&req, sizeof(req));
3096 strlcpy(req.backend, backend_name, sizeof(req.backend));
3097 req.reqtype = CTL_LUNREQ_CREATE;
3099 if (blocksize_set != 0)
3100 req.reqdata.create.blocksize_bytes = blocksize;
3102 if (lun_size_set != 0)
3103 req.reqdata.create.lun_size_bytes = lun_size;
3105 if (lun_id_set != 0) {
3106 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
3107 req.reqdata.create.req_lun_id = req_lun_id;
3110 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
3112 if (device_type != -1)
3113 req.reqdata.create.device_type = device_type;
3115 req.reqdata.create.device_type = T_DIRECT;
3117 if (serial_num != NULL) {
3118 strlcpy(req.reqdata.create.serial_num, serial_num,
3119 sizeof(req.reqdata.create.serial_num));
3120 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
3123 if (device_id != NULL) {
3124 strlcpy(req.reqdata.create.device_id, device_id,
3125 sizeof(req.reqdata.create.device_id));
3126 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
3129 req.num_be_args = num_options;
3130 if (num_options > 0) {
3131 struct cctl_req_option *option, *next_option;
3134 req.be_args = malloc(num_options * sizeof(*req.be_args));
3135 if (req.be_args == NULL) {
3136 warn("%s: error allocating %zd bytes", __func__,
3137 num_options * sizeof(*req.be_args));
3142 for (i = 0, option = STAILQ_FIRST(&option_list);
3143 i < num_options; i++, option = next_option) {
3144 next_option = STAILQ_NEXT(option, links);
3146 req.be_args[i].namelen = option->namelen;
3147 req.be_args[i].name = strdup(option->name);
3148 req.be_args[i].vallen = option->vallen;
3149 req.be_args[i].value = strdup(option->value);
3151 * XXX KDM do we want a way to specify a writeable
3152 * flag of some sort? Do we want a way to specify
3155 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3157 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3160 free(option->value);
3165 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3166 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3171 if (req.status == CTL_LUN_ERROR) {
3172 warnx("%s: error returned from LUN creation request:\n%s",
3173 __func__, req.error_str);
3176 } else if (req.status != CTL_LUN_OK) {
3177 warnx("%s: unknown LUN creation request status %d",
3178 __func__, req.status);
3183 fprintf(stdout, "LUN created successfully\n");
3184 fprintf(stdout, "backend: %s\n", req.backend);
3185 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
3186 fprintf(stdout, "LUN size: %ju bytes\n",
3187 (uintmax_t)req.reqdata.create.lun_size_bytes);
3188 fprintf(stdout, "blocksize %u bytes\n",
3189 req.reqdata.create.blocksize_bytes);
3190 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
3191 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
3192 fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
3199 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
3201 struct ctl_lun_req req;
3202 uint32_t lun_id = 0;
3204 char *backend_name = NULL;
3205 STAILQ_HEAD(, cctl_req_option) option_list;
3206 int num_options = 0;
3209 STAILQ_INIT(&option_list);
3211 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3214 backend_name = strdup(optarg);
3217 lun_id = strtoul(optarg, NULL, 0);
3221 struct cctl_req_option *option;
3225 tmpstr = strdup(optarg);
3226 name = strsep(&tmpstr, "=");
3228 warnx("%s: option -o takes \"name=value\""
3229 "argument", __func__);
3233 value = strsep(&tmpstr, "=");
3234 if (value == NULL) {
3235 warnx("%s: option -o takes \"name=value\""
3236 "argument", __func__);
3240 option = malloc(sizeof(*option));
3241 if (option == NULL) {
3242 warn("%s: error allocating %zd bytes",
3243 __func__, sizeof(*option));
3247 option->name = strdup(name);
3248 option->namelen = strlen(name) + 1;
3249 option->value = strdup(value);
3250 option->vallen = strlen(value) + 1;
3253 STAILQ_INSERT_TAIL(&option_list, option, links);
3262 if (backend_name == NULL)
3263 errx(1, "%s: backend name (-b) must be specified", __func__);
3265 if (lun_id_set == 0)
3266 errx(1, "%s: LUN id (-l) must be specified", __func__);
3268 bzero(&req, sizeof(req));
3270 strlcpy(req.backend, backend_name, sizeof(req.backend));
3271 req.reqtype = CTL_LUNREQ_RM;
3273 req.reqdata.rm.lun_id = lun_id;
3275 req.num_be_args = num_options;
3276 if (num_options > 0) {
3277 struct cctl_req_option *option, *next_option;
3280 req.be_args = malloc(num_options * sizeof(*req.be_args));
3281 if (req.be_args == NULL) {
3282 warn("%s: error allocating %zd bytes", __func__,
3283 num_options * sizeof(*req.be_args));
3288 for (i = 0, option = STAILQ_FIRST(&option_list);
3289 i < num_options; i++, option = next_option) {
3290 next_option = STAILQ_NEXT(option, links);
3292 req.be_args[i].namelen = option->namelen;
3293 req.be_args[i].name = strdup(option->name);
3294 req.be_args[i].vallen = option->vallen;
3295 req.be_args[i].value = strdup(option->value);
3297 * XXX KDM do we want a way to specify a writeable
3298 * flag of some sort? Do we want a way to specify
3301 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3303 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3306 free(option->value);
3311 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3312 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3317 if (req.status == CTL_LUN_ERROR) {
3318 warnx("%s: error returned from LUN removal request:\n%s",
3319 __func__, req.error_str);
3322 } else if (req.status != CTL_LUN_OK) {
3323 warnx("%s: unknown LUN removal request status %d",
3324 __func__, req.status);
3329 printf("LUN %d deleted successfully\n", lun_id);
3336 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
3338 struct ctl_lun_req req;
3339 uint64_t lun_size = 0;
3340 uint32_t lun_id = 0;
3341 int lun_id_set = 0, lun_size_set = 0;
3342 char *backend_name = NULL;
3345 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3348 backend_name = strdup(optarg);
3351 lun_id = strtoul(optarg, NULL, 0);
3355 if (strcasecmp(optarg, "auto") != 0) {
3356 retval = expand_number(optarg, &lun_size);
3358 warn("%s: invalid -s argument",
3371 if (backend_name == NULL)
3372 errx(1, "%s: backend name (-b) must be specified", __func__);
3374 if (lun_id_set == 0)
3375 errx(1, "%s: LUN id (-l) must be specified", __func__);
3377 if (lun_size_set == 0)
3378 errx(1, "%s: size (-s) must be specified", __func__);
3380 bzero(&req, sizeof(req));
3382 strlcpy(req.backend, backend_name, sizeof(req.backend));
3383 req.reqtype = CTL_LUNREQ_MODIFY;
3385 req.reqdata.modify.lun_id = lun_id;
3386 req.reqdata.modify.lun_size_bytes = lun_size;
3388 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3389 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3394 if (req.status == CTL_LUN_ERROR) {
3395 warnx("%s: error returned from LUN modification request:\n%s",
3396 __func__, req.error_str);
3399 } else if (req.status != CTL_LUN_OK) {
3400 warnx("%s: unknown LUN modification request status %d",
3401 __func__, req.status);
3406 printf("LUN %d modified successfully\n", lun_id);
3412 struct cctl_islist_conn {
3415 char *initiator_addr;
3416 char *initiator_alias;
3419 char *header_digest;
3421 char *max_data_segment_length;;
3424 STAILQ_ENTRY(cctl_islist_conn) links;
3427 struct cctl_islist_data {
3429 STAILQ_HEAD(,cctl_islist_conn) conn_list;
3430 struct cctl_islist_conn *cur_conn;
3432 struct sbuf *cur_sb[32];
3436 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
3439 struct cctl_islist_data *islist;
3440 struct cctl_islist_conn *cur_conn;
3442 islist = (struct cctl_islist_data *)user_data;
3443 cur_conn = islist->cur_conn;
3445 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
3446 sizeof(islist->cur_sb[0])))
3447 errx(1, "%s: too many nesting levels, %zd max", __func__,
3448 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
3450 islist->cur_sb[islist->level] = sbuf_new_auto();
3451 if (islist->cur_sb[islist->level] == NULL)
3452 err(1, "%s: Unable to allocate sbuf", __func__);
3454 if (strcmp(name, "connection") == 0) {
3455 if (cur_conn != NULL)
3456 errx(1, "%s: improper connection element nesting",
3459 cur_conn = calloc(1, sizeof(*cur_conn));
3460 if (cur_conn == NULL)
3461 err(1, "%s: cannot allocate %zd bytes", __func__,
3464 islist->num_conns++;
3465 islist->cur_conn = cur_conn;
3467 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
3469 for (i = 0; attr[i] != NULL; i += 2) {
3470 if (strcmp(attr[i], "id") == 0) {
3471 cur_conn->connection_id =
3472 strtoull(attr[i+1], NULL, 0);
3475 "%s: invalid connection attribute %s = %s",
3476 __func__, attr[i], attr[i+1]);
3483 cctl_islist_end_element(void *user_data, const char *name)
3485 struct cctl_islist_data *islist;
3486 struct cctl_islist_conn *cur_conn;
3489 islist = (struct cctl_islist_data *)user_data;
3490 cur_conn = islist->cur_conn;
3492 if ((cur_conn == NULL)
3493 && (strcmp(name, "ctlislist") != 0))
3494 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
3496 if (islist->cur_sb[islist->level] == NULL)
3497 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3498 islist->level, name);
3500 sbuf_finish(islist->cur_sb[islist->level]);
3501 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
3503 err(1, "%s can't allocate %zd bytes for string", __func__,
3504 sbuf_len(islist->cur_sb[islist->level]));
3506 sbuf_delete(islist->cur_sb[islist->level]);
3507 islist->cur_sb[islist->level] = NULL;
3510 if (strcmp(name, "initiator") == 0) {
3511 cur_conn->initiator = str;
3513 } else if (strcmp(name, "initiator_addr") == 0) {
3514 cur_conn->initiator_addr = str;
3516 } else if (strcmp(name, "initiator_alias") == 0) {
3517 cur_conn->initiator_alias = str;
3519 } else if (strcmp(name, "target") == 0) {
3520 cur_conn->target = str;
3522 } else if (strcmp(name, "target_alias") == 0) {
3523 cur_conn->target_alias = str;
3525 } else if (strcmp(name, "header_digest") == 0) {
3526 cur_conn->header_digest = str;
3528 } else if (strcmp(name, "data_digest") == 0) {
3529 cur_conn->data_digest = str;
3531 } else if (strcmp(name, "max_data_segment_length") == 0) {
3532 cur_conn->max_data_segment_length = str;
3534 } else if (strcmp(name, "immediate_data") == 0) {
3535 cur_conn->immediate_data = atoi(str);
3536 } else if (strcmp(name, "iser") == 0) {
3537 cur_conn->iser = atoi(str);
3538 } else if (strcmp(name, "connection") == 0) {
3539 islist->cur_conn = NULL;
3540 } else if (strcmp(name, "ctlislist") == 0) {
3542 errx(1, "unknown element %s", name);
3548 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
3550 struct cctl_islist_data *islist;
3552 islist = (struct cctl_islist_data *)user_data;
3554 sbuf_bcat(islist->cur_sb[islist->level], str, len);
3558 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
3560 struct ctl_iscsi req;
3561 struct cctl_islist_data islist;
3562 struct cctl_islist_conn *conn;
3567 int c, retval, verbose = 0;
3572 bzero(&islist, sizeof(islist));
3573 STAILQ_INIT(&islist.conn_list);
3575 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3589 conn_str = malloc(conn_len);
3591 bzero(&req, sizeof(req));
3592 req.type = CTL_ISCSI_LIST;
3593 req.data.list.alloc_len = conn_len;
3594 req.data.list.conn_xml = conn_str;
3596 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3597 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3602 if (req.status == CTL_ISCSI_ERROR) {
3603 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
3604 __func__, req.error_str);
3605 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
3606 conn_len = conn_len << 1;
3610 if (dump_xml != 0) {
3611 printf("%s", conn_str);
3615 parser = XML_ParserCreate(NULL);
3616 if (parser == NULL) {
3617 warn("%s: Unable to create XML parser", __func__);
3622 XML_SetUserData(parser, &islist);
3623 XML_SetElementHandler(parser, cctl_islist_start_element,
3624 cctl_islist_end_element);
3625 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3627 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3628 XML_ParserFree(parser);
3635 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3636 printf("Session ID: %d\n", conn->connection_id);
3637 printf("Initiator name: %s\n", conn->initiator);
3638 printf("Initiator portal: %s\n", conn->initiator_addr);
3639 printf("Initiator alias: %s\n", conn->initiator_alias);
3640 printf("Target name: %s\n", conn->target);
3641 printf("Target alias: %s\n", conn->target_alias);
3642 printf("Header digest: %s\n", conn->header_digest);
3643 printf("Data digest: %s\n", conn->data_digest);
3644 printf("DataSegmentLen: %s\n", conn->max_data_segment_length);
3645 printf("ImmediateData: %s\n", conn->immediate_data ? "Yes" : "No");
3646 printf("iSER (RDMA): %s\n", conn->iser ? "Yes" : "No");
3650 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3652 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3653 printf("%4u %-16s %-36s %-36s\n",
3654 conn->connection_id, conn->initiator_addr, conn->initiator,
3665 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3667 struct ctl_iscsi req;
3669 int all = 0, connection_id = -1, nargs = 0;
3670 char *initiator_name = NULL, *initiator_addr = NULL;
3672 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3679 connection_id = strtoul(optarg, NULL, 0);
3683 initiator_name = strdup(optarg);
3684 if (initiator_name == NULL)
3685 err(1, "%s: strdup", __func__);
3689 initiator_addr = strdup(optarg);
3690 if (initiator_addr == NULL)
3691 err(1, "%s: strdup", __func__);
3700 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3703 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3706 bzero(&req, sizeof(req));
3707 req.type = CTL_ISCSI_LOGOUT;
3708 req.data.logout.connection_id = connection_id;
3709 if (initiator_addr != NULL)
3710 strlcpy(req.data.logout.initiator_addr,
3711 initiator_addr, sizeof(req.data.logout.initiator_addr));
3712 if (initiator_name != NULL)
3713 strlcpy(req.data.logout.initiator_name,
3714 initiator_name, sizeof(req.data.logout.initiator_name));
3716 req.data.logout.all = 1;
3718 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3719 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3724 if (req.status != CTL_ISCSI_OK) {
3725 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3726 __func__, req.error_str);
3731 printf("iSCSI logout requests submitted\n");
3738 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3740 struct ctl_iscsi req;
3742 int all = 0, connection_id = -1, nargs = 0;
3743 char *initiator_name = NULL, *initiator_addr = NULL;
3745 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3752 connection_id = strtoul(optarg, NULL, 0);
3756 initiator_name = strdup(optarg);
3757 if (initiator_name == NULL)
3758 err(1, "%s: strdup", __func__);
3762 initiator_addr = strdup(optarg);
3763 if (initiator_addr == NULL)
3764 err(1, "%s: strdup", __func__);
3773 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3776 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3779 bzero(&req, sizeof(req));
3780 req.type = CTL_ISCSI_TERMINATE;
3781 req.data.terminate.connection_id = connection_id;
3782 if (initiator_addr != NULL)
3783 strlcpy(req.data.terminate.initiator_addr,
3784 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3785 if (initiator_name != NULL)
3786 strlcpy(req.data.terminate.initiator_name,
3787 initiator_name, sizeof(req.data.terminate.initiator_name));
3789 req.data.terminate.all = 1;
3791 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3792 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3797 if (req.status != CTL_ISCSI_OK) {
3798 warnx("%s: error returned from CTL iSCSI connection "
3799 "termination request:\n%s", __func__, req.error_str);
3804 printf("iSCSI connections terminated\n");
3811 * Name/value pair used for per-LUN attributes.
3813 struct cctl_lun_nv {
3816 STAILQ_ENTRY(cctl_lun_nv) links;
3820 * Backend LUN information.
3825 uint64_t size_blocks;
3827 char *serial_number;
3829 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3830 STAILQ_ENTRY(cctl_lun) links;
3833 struct cctl_devlist_data {
3835 STAILQ_HEAD(,cctl_lun) lun_list;
3836 struct cctl_lun *cur_lun;
3838 struct sbuf *cur_sb[32];
3842 cctl_start_element(void *user_data, const char *name, const char **attr)
3845 struct cctl_devlist_data *devlist;
3846 struct cctl_lun *cur_lun;
3848 devlist = (struct cctl_devlist_data *)user_data;
3849 cur_lun = devlist->cur_lun;
3851 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3852 sizeof(devlist->cur_sb[0])))
3853 errx(1, "%s: too many nesting levels, %zd max", __func__,
3854 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3856 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3857 if (devlist->cur_sb[devlist->level] == NULL)
3858 err(1, "%s: Unable to allocate sbuf", __func__);
3860 if (strcmp(name, "lun") == 0) {
3861 if (cur_lun != NULL)
3862 errx(1, "%s: improper lun element nesting", __func__);
3864 cur_lun = calloc(1, sizeof(*cur_lun));
3865 if (cur_lun == NULL)
3866 err(1, "%s: cannot allocate %zd bytes", __func__,
3869 devlist->num_luns++;
3870 devlist->cur_lun = cur_lun;
3872 STAILQ_INIT(&cur_lun->attr_list);
3873 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3875 for (i = 0; attr[i] != NULL; i += 2) {
3876 if (strcmp(attr[i], "id") == 0) {
3877 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3879 errx(1, "%s: invalid LUN attribute %s = %s",
3880 __func__, attr[i], attr[i+1]);
3887 cctl_end_element(void *user_data, const char *name)
3889 struct cctl_devlist_data *devlist;
3890 struct cctl_lun *cur_lun;
3893 devlist = (struct cctl_devlist_data *)user_data;
3894 cur_lun = devlist->cur_lun;
3896 if ((cur_lun == NULL)
3897 && (strcmp(name, "ctllunlist") != 0))
3898 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3900 if (devlist->cur_sb[devlist->level] == NULL)
3901 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3902 devlist->level, name);
3904 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3905 err(1, "%s: sbuf_finish", __func__);
3906 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3908 err(1, "%s can't allocate %zd bytes for string", __func__,
3909 sbuf_len(devlist->cur_sb[devlist->level]));
3911 if (strlen(str) == 0) {
3916 sbuf_delete(devlist->cur_sb[devlist->level]);
3917 devlist->cur_sb[devlist->level] = NULL;
3920 if (strcmp(name, "backend_type") == 0) {
3921 cur_lun->backend_type = str;
3923 } else if (strcmp(name, "size") == 0) {
3924 cur_lun->size_blocks = strtoull(str, NULL, 0);
3925 } else if (strcmp(name, "blocksize") == 0) {
3926 cur_lun->blocksize = strtoul(str, NULL, 0);
3927 } else if (strcmp(name, "serial_number") == 0) {
3928 cur_lun->serial_number = str;
3930 } else if (strcmp(name, "device_id") == 0) {
3931 cur_lun->device_id = str;
3933 } else if (strcmp(name, "lun") == 0) {
3934 devlist->cur_lun = NULL;
3935 } else if (strcmp(name, "ctllunlist") == 0) {
3938 struct cctl_lun_nv *nv;
3940 nv = calloc(1, sizeof(*nv));
3942 err(1, "%s: can't allocate %zd bytes for nv pair",
3943 __func__, sizeof(*nv));
3945 nv->name = strdup(name);
3946 if (nv->name == NULL)
3947 err(1, "%s: can't allocated %zd bytes for string",
3948 __func__, strlen(name));
3952 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3959 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3961 struct cctl_devlist_data *devlist;
3963 devlist = (struct cctl_devlist_data *)user_data;
3965 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3969 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3971 struct ctl_lun_list list;
3972 struct cctl_devlist_data devlist;
3973 struct cctl_lun *lun;
3979 char *backend = NULL;
3985 bzero(&devlist, sizeof(devlist));
3986 STAILQ_INIT(&devlist.lun_list);
3988 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3991 backend = strdup(optarg);
4005 lun_str = malloc(lun_len);
4007 bzero(&list, sizeof(list));
4008 list.alloc_len = lun_len;
4009 list.status = CTL_LUN_LIST_NONE;
4010 list.lun_xml = lun_str;
4012 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
4013 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
4018 if (list.status == CTL_LUN_LIST_ERROR) {
4019 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
4020 __func__, list.error_str);
4021 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
4022 lun_len = lun_len << 1;
4026 if (dump_xml != 0) {
4027 printf("%s", lun_str);
4031 parser = XML_ParserCreate(NULL);
4032 if (parser == NULL) {
4033 warn("%s: Unable to create XML parser", __func__);
4038 XML_SetUserData(parser, &devlist);
4039 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
4040 XML_SetCharacterDataHandler(parser, cctl_char_handler);
4042 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
4043 XML_ParserFree(parser);
4049 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
4050 "Serial Number", "Device ID");
4051 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
4052 struct cctl_lun_nv *nv;
4054 if ((backend != NULL)
4055 && (strcmp(lun->backend_type, backend) != 0))
4058 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
4059 (uintmax_t)lun->lun_id,
4060 lun->backend_type, (uintmax_t)lun->size_blocks,
4061 lun->blocksize, lun->serial_number, lun->device_id);
4066 STAILQ_FOREACH(nv, &lun->attr_list, links) {
4067 printf(" %s=%s\n", nv->name, nv->value);
4079 fprintf(error ? stderr : stdout,
4081 "Primary commands:\n"
4082 " ctladm tur [dev_id][general options]\n"
4083 " ctladm inquiry [dev_id][general options]\n"
4084 " ctladm devid [dev_id][general options]\n"
4085 " ctladm reqsense [dev_id][general options]\n"
4086 " ctladm reportluns [dev_id][general options]\n"
4087 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
4088 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4089 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
4090 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4091 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
4092 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
4093 " [-d] [-S subpage] [-c cdbsize]\n"
4094 " ctladm prin [dev_id][general options] <-a action>\n"
4095 " ctladm prout [dev_id][general options] <-a action>\n"
4096 " <-r restype] [-k key] [-s sa_key]\n"
4097 " ctladm rtpg [dev_id][general options]\n"
4098 " ctladm start [dev_id][general options] [-i] [-o]\n"
4099 " ctladm stop [dev_id][general options] [-i] [-o]\n"
4100 " ctladm synccache [dev_id][general options] [-l lba]\n"
4101 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
4102 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
4103 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
4104 " [-S serial_num] [-t dev_type]\n"
4105 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
4106 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
4107 " ctladm devlist [-b][-v][-x]\n"
4108 " ctladm shutdown\n"
4110 " ctladm hardstop\n"
4111 " ctladm hardstart\n"
4113 " ctladm bbrread [dev_id] <-l lba> <-d datalen>\n"
4114 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
4116 " ctladm realsync <on|off|query>\n"
4117 " ctladm setsync [dev_id] <-i interval>\n"
4118 " ctladm getsync [dev_id]\n"
4119 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
4120 " [-s len fmt [args]] [-c] [-d delete_id]\n"
4121 " ctladm port <-l | -o <on|off> | [-w wwnn][-W wwpn]>\n"
4122 " [-p targ_port] [-t port_type] [-q] [-x]\n"
4123 " ctladm islist [-v | -x]\n"
4124 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
4125 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
4127 " ctladm dumpstructs\n"
4129 "General Options:\n"
4130 "-I intiator_id : defaults to 7, used to change the initiator id\n"
4131 "-C retries : specify the number of times to retry this command\n"
4132 "-D devicename : specify the device to operate on\n"
4133 " : (default is %s)\n"
4134 "read/write options:\n"
4135 "-l lba : logical block address\n"
4136 "-d len : read/write length, in blocks\n"
4137 "-f file|- : write/read data to/from file or stdout/stdin\n"
4138 "-b blocksize : block size, in bytes\n"
4139 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
4140 "-N : do not copy data to/from userland\n"
4141 "readcapacity options:\n"
4142 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4143 "modesense options:\n"
4144 "-m page : specify the mode page to view\n"
4145 "-l : request a list of supported pages\n"
4146 "-P pc : specify the page control value: 0-3 (current,\n"
4147 " changeable, default, saved, respectively)\n"
4148 "-d : disable block descriptors for mode sense\n"
4149 "-S subpage : specify a subpage\n"
4150 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
4151 "persistent reserve in options:\n"
4152 "-a action : specify the action value: 0-2 (read key, read\n"
4153 " reservation, read capabilities, respectively)\n"
4154 "persistent reserve out options:\n"
4155 "-a action : specify the action value: 0-5 (register, reserve,\n"
4156 " release, clear, preempt, register and ignore)\n"
4157 "-k key : key value\n"
4158 "-s sa_key : service action value\n"
4159 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
4160 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
4161 "start/stop options:\n"
4162 "-i : set the immediate bit (CTL does not support this)\n"
4163 "-o : set the on/offline bit\n"
4164 "synccache options:\n"
4165 "-l lba : set the starting LBA\n"
4166 "-b blockcount : set the length to sync in blocks\n"
4167 "-r : set the relative addressing bit\n"
4168 "-i : set the immediate bit\n"
4169 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4171 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4172 "-B blocksize : LUN blocksize in bytes (some backends)\n"
4173 "-d device_id : SCSI VPD page 0x83 ID\n"
4174 "-l lun_id : requested LUN number\n"
4175 "-o name=value : backend-specific options, multiple allowed\n"
4176 "-s size_bytes : LUN size in bytes (some backends)\n"
4177 "-S serial_num : SCSI VPD page 0x80 serial number\n"
4178 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
4180 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4181 "-l lun_id : LUN number to delete\n"
4182 "-o name=value : backend-specific options, multiple allowed\n"
4183 "devlist options:\n"
4184 "-b backend : list devices from specified backend only\n"
4185 "-v : be verbose, show backend attributes\n"
4186 "-x : dump raw XML\n"
4188 "-l datamove|done : delay command at datamove or done phase\n"
4189 "-T oneshot : delay one command, then resume normal completion\n"
4190 "-T cont : delay all commands\n"
4191 "-t secs : number of seconds to delay\n"
4193 "-i error_action : action to perform\n"
4194 "-p pattern : command pattern to look for\n"
4195 "-r lba,len : LBA range for pattern\n"
4196 "-s len fmt [args] : sense data for custom sense action\n"
4197 "-c : continuous operation\n"
4198 "-d delete_id : error id to delete\n"
4200 "-l : list frontend ports\n"
4201 "-o on|off : turn frontend ports on or off\n"
4202 "-w wwnn : set WWNN for one frontend\n"
4203 "-W wwpn : set WWPN for one frontend\n"
4204 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
4205 "-p targ_port : specify target port number\n"
4206 "-q : omit header in list output\n"
4207 "-x : output port list in XML format\n"
4208 "bbrread options:\n"
4209 "-l lba : starting LBA\n"
4210 "-d datalen : length, in bytes, to read\n",
4215 main(int argc, char **argv)
4218 ctladm_cmdfunction command;
4219 ctladm_cmdargs cmdargs;
4220 ctladm_optret optreturn;
4222 const char *mainopt = "C:D:I:";
4223 const char *subopt = NULL;
4224 char combinedopt[256];
4233 cmdargs = CTLADM_ARG_NONE;
4234 command = CTLADM_CMD_HELP;
4249 * Get the base option.
4251 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4253 if (optreturn == CC_OR_AMBIGUOUS) {
4254 warnx("ambiguous option %s", argv[1]);
4257 } else if (optreturn == CC_OR_NOT_FOUND) {
4258 warnx("option %s not found", argv[1]);
4263 if (cmdargs & CTLADM_ARG_NEED_TL) {
4265 || (!isdigit(argv[2][0]))) {
4266 warnx("option %s requires a target:lun argument",
4271 retval = cctl_parse_tl(argv[2], &target, &lun);
4273 errx(1, "invalid target:lun argument %s", argv[2]);
4275 cmdargs |= CTLADM_ARG_TARG_LUN;
4280 * Ahh, getopt(3) is a pain.
4282 * This is a gross hack. There really aren't many other good
4283 * options (excuse the pun) for parsing options in a situation like
4284 * this. getopt is kinda braindead, so you end up having to run
4285 * through the options twice, and give each invocation of getopt
4286 * the option string for the other invocation.
4288 * You would think that you could just have two groups of options.
4289 * The first group would get parsed by the first invocation of
4290 * getopt, and the second group would get parsed by the second
4291 * invocation of getopt. It doesn't quite work out that way. When
4292 * the first invocation of getopt finishes, it leaves optind pointing
4293 * to the argument _after_ the first argument in the second group.
4294 * So when the second invocation of getopt comes around, it doesn't
4295 * recognize the first argument it gets and then bails out.
4297 * A nice alternative would be to have a flag for getopt that says
4298 * "just keep parsing arguments even when you encounter an unknown
4299 * argument", but there isn't one. So there's no real clean way to
4300 * easily parse two sets of arguments without having one invocation
4301 * of getopt know about the other.
4303 * Without this hack, the first invocation of getopt would work as
4304 * long as the generic arguments are first, but the second invocation
4305 * (in the subfunction) would fail in one of two ways. In the case
4306 * where you don't set optreset, it would fail because optind may be
4307 * pointing to the argument after the one it should be pointing at.
4308 * In the case where you do set optreset, and reset optind, it would
4309 * fail because getopt would run into the first set of options, which
4310 * it doesn't understand.
4312 * All of this would "sort of" work if you could somehow figure out
4313 * whether optind had been incremented one option too far. The
4314 * mechanics of that, however, are more daunting than just giving
4315 * both invocations all of the expect options for either invocation.
4317 * Needless to say, I wouldn't mind if someone invented a better
4318 * (non-GPL!) command line parsing interface than getopt. I
4319 * wouldn't mind if someone added more knobs to getopt to make it
4320 * work better. Who knows, I may talk myself into doing it someday,
4321 * if the standards weenies let me. As it is, it just leads to
4322 * hackery like this and causes people to avoid it in some cases.
4324 * KDM, September 8th, 1998
4327 sprintf(combinedopt, "%s%s", mainopt, subopt);
4329 sprintf(combinedopt, "%s", mainopt);
4332 * Start getopt processing at argv[2/3], since we've already
4333 * accepted argv[1..2] as the command name, and as a possible
4339 * Now we run through the argument list looking for generic
4340 * options, and ignoring options that possibly belong to
4343 while ((c = getopt(argc, argv, combinedopt))!= -1){
4346 cmdargs |= CTLADM_ARG_RETRIES;
4347 retries = strtol(optarg, NULL, 0);
4350 device = strdup(optarg);
4351 cmdargs |= CTLADM_ARG_DEVICE;
4354 cmdargs |= CTLADM_ARG_INITIATOR;
4355 initid = strtol(optarg, NULL, 0);
4362 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4369 * Default to opening the CTL device for now.
4371 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4372 && (command != CTLADM_CMD_HELP)) {
4373 device = strdup(CTL_DEFAULT_DEV);
4374 cmdargs |= CTLADM_ARG_DEVICE;
4377 if ((cmdargs & CTLADM_ARG_DEVICE)
4378 && (command != CTLADM_CMD_HELP)) {
4379 fd = open(device, O_RDWR);
4380 if (fd == -1 && errno == ENOENT) {
4381 saved_errno = errno;
4382 retval = kldload("ctl");
4384 fd = open(device, O_RDWR);
4386 errno = saved_errno;
4389 fprintf(stderr, "%s: error opening %s: %s\n",
4390 argv[0], device, strerror(errno));
4394 } else if ((command != CTLADM_CMD_HELP)
4395 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4396 fprintf(stderr, "%s: you must specify a device with the "
4397 "--device argument for this command\n", argv[0]);
4398 command = CTLADM_CMD_HELP;
4403 case CTLADM_CMD_TUR:
4404 retval = cctl_tur(fd, target, lun, initid, retries);
4406 case CTLADM_CMD_INQUIRY:
4407 retval = cctl_inquiry(fd, target, lun, initid, retries);
4409 case CTLADM_CMD_REQ_SENSE:
4410 retval = cctl_req_sense(fd, target, lun, initid, retries);
4412 case CTLADM_CMD_REPORT_LUNS:
4413 retval = cctl_report_luns(fd, target, lun, initid, retries);
4415 case CTLADM_CMD_CREATE:
4416 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4419 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4421 case CTLADM_CMD_DEVLIST:
4422 retval = cctl_devlist(fd, argc, argv, combinedopt);
4424 case CTLADM_CMD_READ:
4425 case CTLADM_CMD_WRITE:
4426 retval = cctl_read_write(fd, target, lun, initid, retries,
4427 argc, argv, combinedopt, command);
4429 case CTLADM_CMD_PORT:
4430 retval = cctl_port(fd, argc, argv, combinedopt);
4432 case CTLADM_CMD_READCAPACITY:
4433 retval = cctl_read_capacity(fd, target, lun, initid, retries,
4434 argc, argv, combinedopt);
4436 case CTLADM_CMD_MODESENSE:
4437 retval = cctl_mode_sense(fd, target, lun, initid, retries,
4438 argc, argv, combinedopt);
4440 case CTLADM_CMD_START:
4441 case CTLADM_CMD_STOP:
4442 retval = cctl_start_stop(fd, target, lun, initid, retries,
4443 (command == CTLADM_CMD_START) ? 1 : 0,
4444 argc, argv, combinedopt);
4446 case CTLADM_CMD_SYNC_CACHE:
4447 retval = cctl_sync_cache(fd, target, lun, initid, retries,
4448 argc, argv, combinedopt);
4450 case CTLADM_CMD_SHUTDOWN:
4451 case CTLADM_CMD_STARTUP:
4452 retval = cctl_startup_shutdown(fd, target, lun, initid,
4455 case CTLADM_CMD_HARDSTOP:
4456 case CTLADM_CMD_HARDSTART:
4457 retval = cctl_hardstopstart(fd, command);
4459 case CTLADM_CMD_BBRREAD:
4460 retval = cctl_bbrread(fd, target, lun, initid, argc, argv,
4463 case CTLADM_CMD_LUNLIST:
4464 retval = cctl_lunlist(fd);
4466 case CTLADM_CMD_DELAY:
4467 retval = cctl_delay(fd, target, lun, argc, argv, combinedopt);
4469 case CTLADM_CMD_REALSYNC:
4470 retval = cctl_realsync(fd, argc, argv);
4472 case CTLADM_CMD_SETSYNC:
4473 case CTLADM_CMD_GETSYNC:
4474 retval = cctl_getsetsync(fd, target, lun, command,
4475 argc, argv, combinedopt);
4477 case CTLADM_CMD_ERR_INJECT:
4478 retval = cctl_error_inject(fd, target, lun, argc, argv,
4481 case CTLADM_CMD_DUMPOOA:
4482 retval = cctl_dump_ooa(fd, argc, argv);
4484 case CTLADM_CMD_DUMPSTRUCTS:
4485 retval = cctl_dump_structs(fd, cmdargs);
4487 case CTLADM_CMD_PRES_IN:
4488 retval = cctl_persistent_reserve_in(fd, target, lun, initid,
4489 argc, argv, combinedopt,
4492 case CTLADM_CMD_PRES_OUT:
4493 retval = cctl_persistent_reserve_out(fd, target, lun, initid,
4494 argc, argv, combinedopt,
4497 case CTLADM_CMD_INQ_VPD_DEVID:
4498 retval = cctl_inquiry_vpd_devid(fd, target, lun, initid);
4500 case CTLADM_CMD_RTPG:
4501 retval = cctl_report_target_port_group(fd, target, lun, initid);
4503 case CTLADM_CMD_MODIFY:
4504 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4506 case CTLADM_CMD_ISLIST:
4507 retval = cctl_islist(fd, argc, argv, combinedopt);
4509 case CTLADM_CMD_ISLOGOUT:
4510 retval = cctl_islogout(fd, argc, argv, combinedopt);
4512 case CTLADM_CMD_ISTERMINATE:
4513 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4515 case CTLADM_CMD_HELP: