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,
99 CTLADM_CMD_READCAPACITY,
100 CTLADM_CMD_MODESENSE,
102 CTLADM_CMD_DUMPSTRUCTS,
105 CTLADM_CMD_SYNC_CACHE,
110 CTLADM_CMD_HARDSTART,
115 CTLADM_CMD_ERR_INJECT,
119 CTLADM_CMD_INQ_VPD_DEVID,
124 CTLADM_CMD_ISTERMINATE,
126 } ctladm_cmdfunction;
129 CTLADM_ARG_NONE = 0x0000000,
130 CTLADM_ARG_AUTOSENSE = 0x0000001,
131 CTLADM_ARG_DEVICE = 0x0000002,
132 CTLADM_ARG_ARRAYSIZE = 0x0000004,
133 CTLADM_ARG_BACKEND = 0x0000008,
134 CTLADM_ARG_CDBSIZE = 0x0000010,
135 CTLADM_ARG_DATALEN = 0x0000020,
136 CTLADM_ARG_FILENAME = 0x0000040,
137 CTLADM_ARG_LBA = 0x0000080,
138 CTLADM_ARG_PC = 0x0000100,
139 CTLADM_ARG_PAGE_CODE = 0x0000200,
140 CTLADM_ARG_PAGE_LIST = 0x0000400,
141 CTLADM_ARG_SUBPAGE = 0x0000800,
142 CTLADM_ARG_PAGELIST = 0x0001000,
143 CTLADM_ARG_DBD = 0x0002000,
144 CTLADM_ARG_TARG_LUN = 0x0004000,
145 CTLADM_ARG_BLOCKSIZE = 0x0008000,
146 CTLADM_ARG_IMMED = 0x0010000,
147 CTLADM_ARG_RELADR = 0x0020000,
148 CTLADM_ARG_RETRIES = 0x0040000,
149 CTLADM_ARG_ONOFFLINE = 0x0080000,
150 CTLADM_ARG_ONESHOT = 0x0100000,
151 CTLADM_ARG_TIMEOUT = 0x0200000,
152 CTLADM_ARG_INITIATOR = 0x0400000,
153 CTLADM_ARG_NOCOPY = 0x0800000,
154 CTLADM_ARG_NEED_TL = 0x1000000
160 ctladm_cmdargs argnum;
170 static const char rw_opts[] = "Nb:c:d:f:l:";
171 static const char startstop_opts[] = "io";
173 static struct ctladm_opts option_table[] = {
174 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
175 {"bbrread", CTLADM_CMD_BBRREAD, CTLADM_ARG_NEED_TL, "d:l:"},
176 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
177 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
178 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
179 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
180 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
181 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
182 {"getsync", CTLADM_CMD_GETSYNC, CTLADM_ARG_NEED_TL, NULL},
183 {"hardstart", CTLADM_CMD_HARDSTART, CTLADM_ARG_NONE, NULL},
184 {"hardstop", CTLADM_CMD_HARDSTOP, CTLADM_ARG_NONE, NULL},
185 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
186 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
187 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
188 {"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"},
189 {"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"},
190 {"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"},
191 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
192 {"lunmap", CTLADM_CMD_LUNMAP, CTLADM_ARG_NONE, "p:l:L:"},
193 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
194 {"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:s:"},
195 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
196 {"portlist", CTLADM_CMD_PORTLIST, CTLADM_ARG_NONE, "f:ilp:qvx"},
197 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
198 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
199 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
200 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
201 {"realsync", CTLADM_CMD_REALSYNC, CTLADM_ARG_NONE, NULL},
202 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
203 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
204 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
205 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
206 {"setsync", CTLADM_CMD_SETSYNC, CTLADM_ARG_NEED_TL, "i:"},
207 {"shutdown", CTLADM_CMD_SHUTDOWN, CTLADM_ARG_NONE, NULL},
208 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
209 {"startup", CTLADM_CMD_STARTUP, CTLADM_ARG_NONE, NULL},
210 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
211 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
212 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
213 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
214 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
215 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
220 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
221 ctladm_cmdargs *argnum, const char **subopt);
222 static int cctl_parse_tl(char *str, int *target, int *lun);
223 static int cctl_dump_ooa(int fd, int argc, char **argv);
224 static int cctl_port_dump(int fd, int quiet, int xml, int32_t fe_num,
225 ctl_port_type port_type);
226 static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
227 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
228 static int cctl_delay(int fd, int target, int lun, int argc, char **argv,
230 static int cctl_lunlist(int fd);
231 static void cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len);
232 static void cctl_cfi_bbr_statusstr(cfi_bbrread_status, char *str, int str_len);
233 static int cctl_hardstopstart(int fd, ctladm_cmdfunction command);
234 static int cctl_bbrread(int fd, int target, int lun, int iid, int argc,
235 char **argv, char *combinedopt);
236 static int cctl_startup_shutdown(int fd, int target, int lun, int iid,
237 ctladm_cmdfunction command);
238 static int cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
239 int argc, char **argv, char *combinedopt);
240 static int cctl_start_stop(int fd, int target, int lun, int iid, int retries,
241 int start, int argc, char **argv, char *combinedopt);
242 static int cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
243 int argc, char **argv, char *combinedopt);
244 static int cctl_read_capacity(int fd, int target, int lun, int iid,
245 int retries, int argc, char **argv,
247 static int cctl_read_write(int fd, int target, int lun, int iid, int retries,
248 int argc, char **argv, char *combinedopt,
249 ctladm_cmdfunction command);
250 static int cctl_get_luns(int fd, int target, int lun, int iid, int retries,
251 struct scsi_report_luns_data **lun_data,
253 static int cctl_report_luns(int fd, int target, int lun, int iid, int retries);
254 static int cctl_tur(int fd, int target, int lun, int iid, int retries);
255 static int cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
256 char *path_str, int path_len,
257 struct scsi_inquiry_data *inq_data);
258 static int cctl_inquiry(int fd, int target, int lun, int iid, int retries);
259 static int cctl_req_sense(int fd, int target, int lun, int iid, int retries);
260 static int cctl_persistent_reserve_in(int fd, int target, int lun,
261 int initiator, int argc, char **argv,
262 char *combinedopt, int retry_count);
263 static int cctl_persistent_reserve_out(int fd, int target, int lun,
264 int initiator, int argc, char **argv,
265 char *combinedopt, int retry_count);
266 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
267 static int cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator);
268 static int cctl_report_target_port_group(int fd, int target, int lun,
270 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
273 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
274 ctladm_cmdargs *argnum, const char **subopt)
276 struct ctladm_opts *opts;
279 for (opts = table; (opts != NULL) && (opts->optname != NULL);
281 if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
282 *cmdnum = opts->cmdnum;
283 *argnum = opts->argnum;
284 *subopt = opts->subopt;
286 if (strcmp(opts->optname, arg) == 0)
287 return (CC_OR_FOUND);
289 if (++num_matches > 1)
290 return(CC_OR_AMBIGUOUS);
297 return(CC_OR_NOT_FOUND);
302 cctl_parse_tl(char *str, int *target, int *lun)
309 while (isspace(*str) && (*str != '\0'))
312 tmpstr = (char *)strtok(str, ":");
313 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
314 *target = strtol(tmpstr, NULL, 0);
315 tmpstr = (char *)strtok(NULL, ":");
316 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
317 *lun = strtol(tmpstr, NULL, 0);
327 cctl_dump_ooa(int fd, int argc, char **argv)
330 long double cmd_latency;
331 int num_entries, len;
332 int target = -1, lun = -1;
339 && (isdigit(argv[2][0]))) {
340 retval = cctl_parse_tl(argv[2], &target, &lun);
342 warnx("invalid target:lun argument %s", argv[2]);
346 len = num_entries * sizeof(struct ctl_ooa_entry);
348 bzero(&ooa, sizeof(ooa));
350 ooa.entries = malloc(len);
352 if (ooa.entries == NULL) {
353 warn("%s: error mallocing %d bytes", __func__, len);
360 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
363 ooa.alloc_num = num_entries;
364 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
365 warn("%s: CTL_GET_OOA ioctl failed", __func__);
370 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
371 num_entries = num_entries * 2;
377 if (ooa.status != CTL_OOA_OK) {
378 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
384 fprintf(stdout, "Dumping OOA queues\n");
385 for (i = 0; i < ooa.fill_num; i++) {
386 struct ctl_ooa_entry *entry;
387 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
388 struct bintime delta_bt;
391 entry = &ooa.entries[i];
393 delta_bt = ooa.cur_bt;
394 bintime_sub(&delta_bt, &entry->start_bt);
395 bintime2timespec(&delta_bt, &ts);
396 cmd_latency = ts.tv_sec * 1000;
398 cmd_latency += ts.tv_nsec / 1000000;
400 fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
402 (intmax_t)entry->lun_num, entry->tag_num,
403 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
405 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
406 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
408 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
410 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
411 scsi_op_desc(entry->cdb[0], NULL),
412 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
415 fprintf(stdout, "OOA queues dump done\n");
417 if (ioctl(fd, CTL_DUMP_OOA) == -1) {
418 warn("%s: CTL_DUMP_OOA ioctl failed", __func__);
430 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
432 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
440 cctl_port_dump(int fd, int quiet, int xml, int32_t targ_port,
441 ctl_port_type port_type)
443 struct ctl_port_list port_list;
444 struct ctl_port_entry *entries;
445 struct sbuf *sb = NULL;
454 entries = malloc(sizeof(*entries) * num_entries);
455 bzero(&port_list, sizeof(port_list));
456 port_list.entries = entries;
457 port_list.alloc_num = num_entries;
458 port_list.alloc_len = num_entries * sizeof(*entries);
459 if (ioctl(fd, CTL_GET_PORT_LIST, &port_list) != 0) {
460 warn("%s: CTL_GET_PORT_LIST ioctl failed", __func__);
463 if (port_list.status == CTL_PORT_LIST_NEED_MORE_SPACE) {
464 printf("%s: allocated %d, need %d, retrying\n", __func__,
465 num_entries, port_list.fill_num + port_list.dropped_num);
467 num_entries = port_list.fill_num + port_list.dropped_num;
473 printf("Port Online Type Name pp vp %-18s %-18s\n",
477 sb = sbuf_new_auto();
478 sbuf_printf(sb, "<ctlfelist>\n");
480 for (i = 0; i < port_list.fill_num; i++) {
481 struct ctl_port_entry *entry;
486 switch (entry->port_type) {
496 case CTL_PORT_INTERNAL:
514 * If the user specified a frontend number or a particular
515 * frontend type, only print out that particular frontend
518 if ((targ_port != -1)
519 && (targ_port != entry->targ_port))
521 else if ((port_type != CTL_PORT_NONE)
522 && ((port_type & entry->port_type) == 0))
528 printf("Num: %ju Type: %s (%#x) Name: %s Physical Port: %d "
529 "Virtual Port: %d\n", (uintmax_t)entry->fe_num, type,
530 entry->port_type, entry->fe_name, entry->physical_port,
531 entry->virtual_port);
532 printf("WWNN %#jx WWPN %#jx Online: %s\n",
533 (uintmax_t)entry->wwnn, (uintmax_t)entry->wwpn,
534 (entry->online) ? "YES" : "NO" );
537 printf("%-4d %-6s %-8s %-12s %-2d %-2d %#-18jx "
539 entry->targ_port, (entry->online) ? "YES" : "NO",
540 type, entry->port_name, entry->physical_port,
541 entry->virtual_port, (uintmax_t)entry->wwnn,
542 (uintmax_t)entry->wwpn);
544 sbuf_printf(sb, "<targ_port id=\"%d\">\n",
546 sbuf_printf(sb, "<online>%s</online>\n",
547 (entry->online) ? "YES" : "NO");
548 sbuf_printf(sb, "<port_type>%s</port_type>\n", type);
549 sbuf_printf(sb, "<port_name>%s</port_name>\n",
551 sbuf_printf(sb, "<physical_port>%d</physical_port>\n",
552 entry->physical_port);
553 sbuf_printf(sb, "<virtual_port>%d</virtual_port>\n",
554 entry->virtual_port);
555 sbuf_printf(sb, "<wwnn>%#jx</wwnn>\n",
556 (uintmax_t)entry->wwnn);
557 sbuf_printf(sb, "<wwpn>%#jx</wwpn>\n",
558 (uintmax_t)entry->wwpn);
559 sbuf_printf(sb, "</targ_port>\n");
564 sbuf_printf(sb, "</ctlfelist>\n");
565 if (sbuf_finish(sb) != 0)
566 err(1, "%s: sbuf_finish", __func__);
567 printf("%s", sbuf_data(sb));
572 * Give some indication that we didn't find the frontend or
573 * frontend type requested by the user. We could print something
574 * out, but it would probably be better to hide that behind a
578 && ((targ_port != -1)
579 || (port_type != CTL_PORT_NONE)))
593 static struct ctladm_opts cctl_fe_table[] = {
594 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
595 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
596 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
597 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL},
598 {"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL},
599 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
604 cctl_port(int fd, int argc, char **argv, char *combinedopt)
607 int32_t targ_port = -1;
609 int wwnn_set = 0, wwpn_set = 0;
610 uint64_t wwnn = 0, wwpn = 0;
611 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
612 struct ctl_port_entry entry;
613 ctl_port_type port_type = CTL_PORT_NONE;
614 int quiet = 0, xml = 0;
616 while ((c = getopt(argc, argv, combinedopt)) != -1) {
619 if (port_mode != CCTL_PORT_MODE_NONE)
622 port_mode = CCTL_PORT_MODE_LIST;
625 if (port_mode != CCTL_PORT_MODE_NONE)
628 if (strcasecmp(optarg, "on") == 0)
629 port_mode = CCTL_PORT_MODE_ON;
630 else if (strcasecmp(optarg, "off") == 0)
631 port_mode = CCTL_PORT_MODE_OFF;
633 warnx("Invalid -o argument %s, \"on\" or "
634 "\"off\" are the only valid args",
641 targ_port = strtol(optarg, NULL, 0);
647 ctladm_optret optret;
648 ctladm_cmdargs argnum;
650 ctl_port_type tmp_port_type;
652 optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
654 if (optret == CC_OR_AMBIGUOUS) {
655 warnx("%s: ambiguous frontend type %s",
659 } else if (optret == CC_OR_NOT_FOUND) {
660 warnx("%s: invalid frontend type %s",
666 port_type |= tmp_port_type;
670 if ((port_mode != CCTL_PORT_MODE_NONE)
671 && (port_mode != CCTL_PORT_MODE_SET))
674 port_mode = CCTL_PORT_MODE_SET;
676 wwnn = strtoull(optarg, NULL, 0);
680 if ((port_mode != CCTL_PORT_MODE_NONE)
681 && (port_mode != CCTL_PORT_MODE_SET))
684 port_mode = CCTL_PORT_MODE_SET;
686 wwpn = strtoull(optarg, NULL, 0);
696 * The user can specify either one or more frontend types (-t), or
697 * a specific frontend, but not both.
699 * If the user didn't specify a frontend type or number, set it to
700 * all. This is primarily needed for the enable/disable ioctls.
701 * This will be a no-op for the listing code. For the set ioctl,
702 * we'll throw an error, since that only works on one port at a time.
704 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
705 warnx("%s: can only specify one of -t or -n", __func__);
708 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
709 port_type = CTL_PORT_ALL;
711 bzero(&entry, sizeof(entry));
714 * These are needed for all but list/dump mode.
716 entry.port_type = port_type;
717 entry.targ_port = targ_port;
720 case CCTL_PORT_MODE_LIST:
721 cctl_port_dump(fd, quiet, xml, targ_port, port_type);
723 case CCTL_PORT_MODE_SET:
724 if (targ_port == -1) {
725 warnx("%s: -w and -W require -n", __func__);
731 entry.flags |= CTL_PORT_WWNN_VALID;
735 entry.flags |= CTL_PORT_WWPN_VALID;
739 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
740 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
745 case CCTL_PORT_MODE_ON:
746 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
747 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
751 fprintf(stdout, "Front End Ports enabled\n");
753 case CCTL_PORT_MODE_OFF:
754 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
755 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
759 fprintf(stdout, "Front End Ports disabled\n");
762 warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
773 warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
778 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
781 if (ioctl(fd, CTL_IO, io) == -1) {
782 warn("%s: error sending CTL_IO ioctl", func);
785 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
792 cctl_delay(int fd, int target, int lun, int argc, char **argv,
795 struct ctl_io_delay_info delay_info;
796 char *delayloc = NULL;
797 char *delaytype = NULL;
804 memset(&delay_info, 0, sizeof(delay_info));
806 while ((c = getopt(argc, argv, combinedopt)) != -1) {
809 delaytype = strdup(optarg);
812 delayloc = strdup(optarg);
815 delaytime = strtoul(optarg, NULL, 0);
820 if (delaytime == -1) {
821 warnx("%s: you must specify the delaytime with -t", __func__);
826 if (strcasecmp(delayloc, "datamove") == 0)
827 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
828 else if (strcasecmp(delayloc, "done") == 0)
829 delay_info.delay_loc = CTL_DELAY_LOC_DONE;
831 warnx("%s: invalid delay location %s", __func__, delayloc);
836 if ((delaytype == NULL)
837 || (strcmp(delaytype, "oneshot") == 0))
838 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
839 else if (strcmp(delaytype, "cont") == 0)
840 delay_info.delay_type = CTL_DELAY_TYPE_CONT;
842 warnx("%s: invalid delay type %s", __func__, delaytype);
847 delay_info.target_id = target;
848 delay_info.lun_id = lun;
849 delay_info.delay_secs = delaytime;
851 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
852 warn("%s: CTL_DELAY_IO ioctl failed", __func__);
856 switch (delay_info.status) {
857 case CTL_DELAY_STATUS_NONE:
858 warnx("%s: no delay status??", __func__);
861 case CTL_DELAY_STATUS_OK:
863 case CTL_DELAY_STATUS_INVALID_LUN:
864 warnx("%s: invalid lun %d", __func__, lun);
867 case CTL_DELAY_STATUS_INVALID_TYPE:
868 warnx("%s: invalid delay type %d", __func__,
869 delay_info.delay_type);
872 case CTL_DELAY_STATUS_INVALID_LOC:
873 warnx("%s: delay location %s not implemented?", __func__,
877 case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
878 warnx("%s: delay not implemented in the kernel", __func__);
879 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
883 warnx("%s: unknown delay return status %d", __func__,
890 /* delayloc should never be NULL, but just in case...*/
891 if (delayloc != NULL)
898 cctl_realsync(int fd, int argc, char **argv)
907 warnx("%s %s takes exactly one argument", argv[0], argv[1]);
914 if (strncasecmp(syncarg, "query", min(strlen(syncarg),
915 strlen("query"))) == 0) {
916 if (ioctl(fd, CTL_REALSYNC_GET, &syncstate) == -1) {
917 warn("%s: CTL_REALSYNC_GET ioctl failed", __func__);
921 fprintf(stdout, "SYNCHRONIZE CACHE support is: ");
924 fprintf(stdout, "OFF\n");
927 fprintf(stdout, "ON\n");
930 fprintf(stdout, "unknown (%d)\n", syncstate);
934 } else if (strcasecmp(syncarg, "on") == 0) {
936 } else if (strcasecmp(syncarg, "off") == 0) {
939 warnx("%s: invalid realsync argument %s", __func__, syncarg);
944 if (ioctl(fd, CTL_REALSYNC_SET, &syncstate) == -1) {
945 warn("%s: CTL_REALSYNC_SET ioctl failed", __func__);
954 cctl_getsetsync(int fd, int target, int lun, ctladm_cmdfunction command,
955 int argc, char **argv, char *combinedopt)
957 struct ctl_sync_info sync_info;
959 int sync_interval = -1;
965 memset(&sync_info, 0, sizeof(sync_info));
966 sync_info.target_id = target;
967 sync_info.lun_id = lun;
969 while ((c = getopt(argc, argv, combinedopt)) != -1) {
972 sync_interval = strtoul(optarg, NULL, 0);
979 if (command == CTLADM_CMD_SETSYNC) {
980 if (sync_interval == -1) {
981 warnx("%s: you must specify the sync interval with -i",
986 sync_info.sync_interval = sync_interval;
987 ioctl_cmd = CTL_SETSYNC;
989 ioctl_cmd = CTL_GETSYNC;
992 if (ioctl(fd, ioctl_cmd, &sync_info) == -1) {
993 warn("%s: CTL_%sSYNC ioctl failed", __func__,
994 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET");
999 switch (sync_info.status) {
1000 case CTL_GS_SYNC_OK:
1001 if (command == CTLADM_CMD_GETSYNC) {
1002 fprintf(stdout, "%d:%d: sync interval: %d\n",
1003 target, lun, sync_info.sync_interval);
1006 case CTL_GS_SYNC_NO_LUN:
1007 warnx("%s: unknown target:LUN %d:%d", __func__, target, lun);
1010 case CTL_GS_SYNC_NONE:
1012 warnx("%s: unknown CTL_%sSYNC status %d", __func__,
1013 (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET",
1022 static struct ctladm_opts cctl_err_types[] = {
1023 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
1024 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
1025 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
1026 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
1031 static struct ctladm_opts cctl_err_patterns[] = {
1032 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
1033 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
1034 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1035 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1036 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
1037 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
1038 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
1040 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL},
1046 cctl_error_inject(int fd, uint32_t target, uint32_t lun, int argc, char **argv,
1050 struct ctl_error_desc err_desc;
1053 uint64_t delete_id = 0;
1054 int delete_id_set = 0;
1060 bzero(&err_desc, sizeof(err_desc));
1061 err_desc.target_id = target;
1062 err_desc.lun_id = lun;
1064 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1070 delete_id = strtoull(optarg, NULL, 0);
1075 ctladm_optret optret;
1076 ctladm_cmdargs argnum;
1080 ctl_lun_error err_type;
1082 if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
1083 warnx("%s: can't specify multiple -i "
1084 "arguments", __func__);
1088 optret = getoption(cctl_err_types, optarg,
1089 &err_type, &argnum, &subopt);
1090 err_desc.lun_error = err_type;
1092 ctl_lun_error_pattern pattern;
1094 optret = getoption(cctl_err_patterns, optarg,
1095 &pattern, &argnum, &subopt);
1096 err_desc.error_pattern |= pattern;
1099 if (optret == CC_OR_AMBIGUOUS) {
1100 warnx("%s: ambiguous argument %s", __func__,
1104 } else if (optret == CC_OR_NOT_FOUND) {
1105 warnx("%s: argument %s not found", __func__,
1113 char *tmpstr, *tmpstr2;
1115 tmpstr = strdup(optarg);
1116 if (tmpstr == NULL) {
1117 warn("%s: error duplicating string %s",
1123 tmpstr2 = strsep(&tmpstr, ",");
1124 if (tmpstr2 == NULL) {
1125 warnx("%s: invalid -r argument %s", __func__,
1131 lba = strtoull(tmpstr2, NULL, 0);
1132 tmpstr2 = strsep(&tmpstr, ",");
1133 if (tmpstr2 == NULL) {
1134 warnx("%s: no len argument for -r lba,len, got"
1135 " %s", __func__, optarg);
1140 len = strtoul(tmpstr2, NULL, 0);
1145 struct get_hook hook;
1148 sense_len = strtol(optarg, NULL, 0);
1149 if (sense_len <= 0) {
1150 warnx("invalid number of sense bytes %d",
1156 sense_len = MIN(sense_len, SSD_FULL_SIZE);
1158 hook.argc = argc - optind;
1159 hook.argv = argv + optind;
1162 sensestr = cget(&hook, NULL);
1163 if ((sensestr != NULL)
1164 && (sensestr[0] == '-')) {
1168 (uint8_t *)&err_desc.custom_sense,
1169 sense_len, sensestr, iget, &hook);
1179 if (delete_id_set != 0) {
1180 err_desc.serial = delete_id;
1181 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
1182 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
1189 if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
1190 warnx("%s: error injection command (-i) needed",
1194 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
1195 && (sense_len == 0)) {
1196 warnx("%s: custom error requires -s", __func__);
1201 if (continuous != 0)
1202 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
1205 * If fd_sense is set, we need to read the sense data the user
1206 * wants returned from stdin.
1208 if (fd_sense == 1) {
1210 int amt_to_read = sense_len;
1211 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
1213 for (amt_read = 0; amt_to_read > 0;
1214 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1215 if (amt_read == -1) {
1216 warn("error reading sense data from stdin");
1220 amt_to_read -= amt_read;
1221 buf_ptr += amt_read;
1225 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1226 warnx("%s: command pattern (-p) needed", __func__);
1232 err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1234 * We could check here to see whether it's a read/write
1235 * command, but that will be pointless once we allow
1236 * custom patterns. At that point, the user could specify
1237 * a READ(6) CDB type, and we wouldn't have an easy way here
1238 * to verify whether range checking is possible there. The
1239 * user will just figure it out when his error never gets
1243 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1244 warnx("%s: need read and/or write pattern if range "
1245 "is specified", __func__);
1250 err_desc.lba_range.lba = lba;
1251 err_desc.lba_range.len = len;
1254 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1255 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1258 printf("Error injection succeeded, serial number is %ju\n",
1259 (uintmax_t)err_desc.serial);
1267 cctl_lunlist(int fd)
1269 struct scsi_report_luns_data *lun_data;
1270 struct scsi_inquiry_data *inq_data;
1284 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1285 * if we ever acquire the ability to have multiple targets.
1287 if ((retval = cctl_get_luns(fd, target, /*lun*/ 0, initid,
1288 /*retries*/ 2, &lun_data, &num_luns)) != 0)
1291 inq_data = malloc(sizeof(*inq_data));
1292 if (inq_data == NULL) {
1293 warn("%s: couldn't allocate memory for inquiry data\n",
1298 for (i = 0; i < num_luns; i++) {
1302 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1303 case RPL_LUNDATA_ATYP_PERIPH:
1304 lun_val = lun_data->luns[i].lundata[1];
1306 case RPL_LUNDATA_ATYP_FLAT:
1307 lun_val = (lun_data->luns[i].lundata[0] &
1308 RPL_LUNDATA_FLAT_LUN_MASK) |
1309 (lun_data->luns[i].lundata[1] <<
1310 RPL_LUNDATA_FLAT_LUN_BITS);
1312 case RPL_LUNDATA_ATYP_LUN:
1313 case RPL_LUNDATA_ATYP_EXTLUN:
1315 fprintf(stdout, "Unsupported LUN format %d\n",
1316 lun_data->luns[i].lundata[0] &
1317 RPL_LUNDATA_ATYP_MASK);
1324 if ((retval = cctl_get_inquiry(fd, target, lun_val, initid,
1325 /*retries*/ 2, scsi_path,
1330 printf("%s", scsi_path);
1331 scsi_print_inquiry(inq_data);
1335 if (lun_data != NULL)
1338 if (inq_data != NULL)
1345 cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len)
1348 case CFI_MT_PORT_OFFLINE:
1349 snprintf(str, str_len, "Port Offline");
1352 snprintf(str, str_len, "Error");
1354 case CFI_MT_SUCCESS:
1355 snprintf(str, str_len, "Success");
1358 snprintf(str, str_len, "None??");
1361 snprintf(str, str_len, "Unknown status: %d", status);
1367 cctl_cfi_bbr_statusstr(cfi_bbrread_status status, char *str, int str_len)
1370 case CFI_BBR_SUCCESS:
1371 snprintf(str, str_len, "Success");
1373 case CFI_BBR_LUN_UNCONFIG:
1374 snprintf(str, str_len, "LUN not configured");
1376 case CFI_BBR_NO_LUN:
1377 snprintf(str, str_len, "LUN does not exist");
1379 case CFI_BBR_NO_MEM:
1380 snprintf(str, str_len, "Memory allocation error");
1382 case CFI_BBR_BAD_LEN:
1383 snprintf(str, str_len, "Length is not a multiple of blocksize");
1385 case CFI_BBR_RESERV_CONFLICT:
1386 snprintf(str, str_len, "Reservation conflict");
1388 case CFI_BBR_LUN_STOPPED:
1389 snprintf(str, str_len, "LUN is powered off");
1391 case CFI_BBR_LUN_OFFLINE_CTL:
1392 snprintf(str, str_len, "LUN is offline");
1394 case CFI_BBR_LUN_OFFLINE_RC:
1395 snprintf(str, str_len, "RAIDCore array is offline (double "
1398 case CFI_BBR_SCSI_ERROR:
1399 snprintf(str, str_len, "SCSI Error");
1402 snprintf(str, str_len, "Error");
1405 snprintf(str, str_len, "Unknown status: %d", status);
1411 cctl_hardstopstart(int fd, ctladm_cmdfunction command)
1413 struct ctl_hard_startstop_info hs_info;
1414 char error_str[256];
1420 if (command == CTLADM_CMD_HARDSTART)
1425 if (ioctl(fd, (do_start == 1) ? CTL_HARD_START : CTL_HARD_STOP,
1427 warn("%s: CTL_HARD_%s ioctl failed", __func__,
1428 (do_start == 1) ? "START" : "STOP");
1433 fprintf(stdout, "Hard %s Status: ", (command == CTLADM_CMD_HARDSTOP) ?
1435 cctl_cfi_mt_statusstr(hs_info.status, error_str, sizeof(error_str));
1436 fprintf(stdout, "%s\n", error_str);
1437 fprintf(stdout, "Total LUNs: %d\n", hs_info.total_luns);
1438 fprintf(stdout, "LUNs complete: %d\n", hs_info.luns_complete);
1439 fprintf(stdout, "LUNs failed: %d\n", hs_info.luns_failed);
1446 cctl_bbrread(int fd, int target __unused, int lun, int iid __unused,
1447 int argc, char **argv, char *combinedopt)
1449 struct ctl_bbrread_info bbr_info;
1450 char error_str[256];
1459 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1462 datalen = strtoul(optarg, NULL, 0);
1465 lba = strtoull(optarg, NULL, 0);
1474 warnx("%s: you must specify an LBA with -l", __func__);
1479 if (datalen == -1) {
1480 warnx("%s: you must specify a length with -d", __func__);
1485 bbr_info.lun_num = lun;
1488 * XXX KDM get the blocksize first??
1490 if ((datalen % 512) != 0) {
1491 warnx("%s: data length %d is not a multiple of 512 bytes",
1496 bbr_info.len = datalen;
1498 if (ioctl(fd, CTL_BBRREAD, &bbr_info) == -1) {
1499 warn("%s: CTL_BBRREAD ioctl failed", __func__);
1503 cctl_cfi_mt_statusstr(bbr_info.status, error_str, sizeof(error_str));
1504 fprintf(stdout, "BBR Read Overall Status: %s\n", error_str);
1505 cctl_cfi_bbr_statusstr(bbr_info.bbr_status, error_str,
1507 fprintf(stdout, "BBR Read Status: %s\n", error_str);
1509 * XXX KDM should we bother printing out SCSI status if we get
1510 * CFI_BBR_SCSI_ERROR back?
1512 * Return non-zero if this fails?
1519 cctl_startup_shutdown(int fd, int target, int lun, int iid,
1520 ctladm_cmdfunction command)
1524 struct scsi_report_luns_data *lun_data;
1525 struct scsi_inquiry_data *inq_data;
1535 * - step through each lun, do an inquiry
1536 * - check OOA queue on direct access luns
1537 * - send stop with offline bit to each direct access device with a
1539 * - if we get a reservation conflict, reset the LUN to clear it
1540 * and reissue the stop with the offline bit set
1545 io = ctl_scsi_alloc_io(id);
1547 warnx("%s: can't allocate memory", __func__);
1551 if ((retval = cctl_get_luns(fd, target, lun, iid, /*retries*/ 2,
1552 &lun_data, &num_luns)) != 0)
1555 inq_data = malloc(sizeof(*inq_data));
1556 if (inq_data == NULL) {
1557 warn("%s: couldn't allocate memory for inquiry data\n",
1562 for (i = 0; i < num_luns; i++) {
1567 * XXX KDM figure out a way to share this code with
1570 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1571 case RPL_LUNDATA_ATYP_PERIPH:
1572 lun_val = lun_data->luns[i].lundata[1];
1574 case RPL_LUNDATA_ATYP_FLAT:
1575 lun_val = (lun_data->luns[i].lundata[0] &
1576 RPL_LUNDATA_FLAT_LUN_MASK) |
1577 (lun_data->luns[i].lundata[1] <<
1578 RPL_LUNDATA_FLAT_LUN_BITS);
1580 case RPL_LUNDATA_ATYP_LUN:
1581 case RPL_LUNDATA_ATYP_EXTLUN:
1583 fprintf(stdout, "Unsupported LUN format %d\n",
1584 lun_data->luns[i].lundata[0] &
1585 RPL_LUNDATA_ATYP_MASK);
1592 if ((retval = cctl_get_inquiry(fd, target, lun_val, iid,
1593 /*retries*/ 2, scsi_path,
1598 printf("%s", scsi_path);
1599 scsi_print_inquiry(inq_data);
1601 * We only want to shutdown direct access devices.
1603 if (SID_TYPE(inq_data) != T_DIRECT) {
1604 printf("%s LUN is not direct access, skipped\n",
1609 if (command == CTLADM_CMD_SHUTDOWN) {
1610 struct ctl_ooa_info ooa_info;
1612 ooa_info.target_id = target;
1613 ooa_info.lun_id = lun_val;
1615 if (ioctl(fd, CTL_CHECK_OOA, &ooa_info) == -1) {
1616 printf("%s CTL_CHECK_OOA ioctl failed\n",
1621 if (ooa_info.status != CTL_OOA_SUCCESS) {
1622 printf("%s CTL_CHECK_OOA returned status %d\n",
1623 scsi_path, ooa_info.status);
1626 if (ooa_info.num_entries != 0) {
1627 printf("%s %d entr%s in the OOA queue, "
1628 "skipping shutdown\n", scsi_path,
1629 ooa_info.num_entries,
1630 (ooa_info.num_entries > 1)?"ies" : "y" );
1635 ctl_scsi_start_stop(/*io*/ io,
1636 /*start*/(command == CTLADM_CMD_STARTUP) ?
1640 /*power_conditions*/ SSS_PC_START_VALID,
1643 (command == CTLADM_CMD_STARTUP) ?
1644 CTL_TAG_SIMPLE :CTL_TAG_ORDERED,
1647 io->io_hdr.nexus.targ_target.id = target;
1648 io->io_hdr.nexus.targ_lun = lun_val;
1649 io->io_hdr.nexus.initid = id;
1651 if (cctl_do_io(fd, /*retries*/ 3, io, __func__) != 0) {
1656 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1657 ctl_io_error_print(io, inq_data, stderr);
1659 printf("%s LUN is now %s\n", scsi_path,
1660 (command == CTLADM_CMD_STARTUP) ? "online" :
1665 if (lun_data != NULL)
1668 if (inq_data != NULL)
1672 ctl_scsi_free_io(io);
1678 cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
1679 int argc, char **argv, char *combinedopt)
1685 uint64_t our_lba = 0;
1686 uint32_t our_block_count = 0;
1687 int reladr = 0, immed = 0;
1693 io = ctl_scsi_alloc_io(id);
1695 warnx("%s: can't allocate memory", __func__);
1699 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1702 our_block_count = strtoul(optarg, NULL, 0);
1705 cdb_size = strtol(optarg, NULL, 0);
1711 our_lba = strtoull(optarg, NULL, 0);
1721 if (cdb_size != -1) {
1727 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1728 "and 16", __func__, cdb_size);
1731 break; /* NOTREACHED */
1736 ctl_scsi_sync_cache(/*io*/ io,
1739 /*minimum_cdb_size*/ cdb_size,
1740 /*starting_lba*/ our_lba,
1741 /*block_count*/ our_block_count,
1742 /*tag_type*/ CTL_TAG_SIMPLE,
1745 io->io_hdr.nexus.targ_target.id = target;
1746 io->io_hdr.nexus.targ_lun = lun;
1747 io->io_hdr.nexus.initid = id;
1749 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1754 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1755 fprintf(stdout, "Cache synchronized successfully\n");
1757 ctl_io_error_print(io, NULL, stderr);
1759 ctl_scsi_free_io(io);
1765 cctl_start_stop(int fd, int target, int lun, int iid, int retries, int start,
1766 int argc, char **argv, char *combinedopt)
1771 int immed = 0, onoffline = 0;
1777 io = ctl_scsi_alloc_io(id);
1779 warnx("%s: can't allocate memory", __func__);
1783 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1796 * Use an ordered tag for the stop command, to guarantee that any
1797 * pending I/O will finish before the stop command executes. This
1798 * would normally be the case anyway, since CTL will basically
1799 * treat the start/stop command as an ordered command with respect
1800 * to any other command except an INQUIRY. (See ctl_ser_table.c.)
1802 ctl_scsi_start_stop(/*io*/ io,
1805 /*immediate*/ immed,
1806 /*power_conditions*/ SSS_PC_START_VALID,
1807 /*onoffline*/ onoffline,
1808 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1812 io->io_hdr.nexus.targ_target.id = target;
1813 io->io_hdr.nexus.targ_lun = lun;
1814 io->io_hdr.nexus.initid = id;
1816 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1821 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1822 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1823 fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1824 (start) ? "started" : "stopped");
1826 ctl_io_error_print(io, NULL, stderr);
1829 ctl_scsi_free_io(io);
1835 cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
1836 int argc, char **argv, char *combinedopt)
1842 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1852 io = ctl_scsi_alloc_io(id);
1854 warn("%s: can't allocate memory", __func__);
1858 while ((c = getopt(argc, argv, combinedopt)) != -1) {
1861 pc = strtoul(optarg, NULL, 0);
1864 subpage = strtoul(optarg, NULL, 0);
1873 page_code = strtoul(optarg, NULL, 0);
1876 cdbsize = strtol(optarg, NULL, 0);
1883 if (((list == 0) && (page_code == -1))
1884 || ((list != 0) && (page_code != -1))) {
1885 warnx("%s: you must specify either a page code (-m) or -l",
1891 if ((page_code != -1)
1892 && ((page_code > SMS_ALL_PAGES_PAGE)
1893 || (page_code < 0))) {
1894 warnx("%s: page code %d is out of range", __func__,
1901 page_code = SMS_ALL_PAGES_PAGE;
1903 warnx("%s: arg -P makes no sense with -l",
1908 if (subpage != -1) {
1909 warnx("%s: arg -S makes no sense with -l", __func__);
1916 pc = SMS_PAGE_CTRL_CURRENT;
1920 warnx("%s: page control value %d is out of range: 0-3",
1930 || (subpage < 0))) {
1931 warnx("%s: subpage code %d is out of range: 0-255", __func__,
1942 warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1943 "and 10", __func__, cdbsize);
1959 dataptr = (uint8_t *)malloc(datalen);
1960 if (dataptr == NULL) {
1961 warn("%s: can't allocate %d bytes", __func__, datalen);
1966 memset(dataptr, 0, datalen);
1968 ctl_scsi_mode_sense(io,
1969 /*data_ptr*/ dataptr,
1970 /*data_len*/ datalen,
1973 /*page_code*/ page_code,
1975 /*subpage*/ subpage,
1976 /*minimum_cdb_size*/ cdbsize,
1977 /*tag_type*/ CTL_TAG_SIMPLE,
1980 io->io_hdr.nexus.targ_target.id = target;
1981 io->io_hdr.nexus.targ_lun = lun;
1982 io->io_hdr.nexus.initid = id;
1984 if (cctl_do_io(fd, retries, io, __func__) != 0) {
1989 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1990 int pages_len, used_len;
1991 uint32_t returned_len;
1994 if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1995 struct scsi_mode_hdr_6 *hdr6;
1998 hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
2000 returned_len = hdr6->datalen + 1;
2001 bdlen = hdr6->block_descr_len;
2003 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
2005 struct scsi_mode_hdr_10 *hdr10;
2008 hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
2010 returned_len = scsi_2btoul(hdr10->datalen) + 2;
2011 bdlen = scsi_2btoul(hdr10->block_descr_len);
2013 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
2015 /* just in case they can give us more than we allocated for */
2016 returned_len = min(returned_len, datalen);
2017 pages_len = returned_len - (ndataptr - dataptr);
2019 fprintf(stdout, "returned_len = %d, pages_len = %d\n",
2020 returned_len, pages_len);
2023 fprintf(stdout, "Supported mode pages:\n");
2024 for (used_len = 0; used_len < pages_len;) {
2025 struct scsi_mode_page_header *header;
2027 header = (struct scsi_mode_page_header *)
2028 &ndataptr[used_len];
2029 fprintf(stdout, "%d\n", header->page_code);
2030 used_len += header->page_length + 2;
2033 for (used_len = 0; used_len < pages_len; used_len++) {
2034 fprintf(stdout, "0x%x ", ndataptr[used_len]);
2035 if (((used_len+1) % 16) == 0)
2036 fprintf(stdout, "\n");
2038 fprintf(stdout, "\n");
2041 ctl_io_error_print(io, NULL, stderr);
2044 ctl_scsi_free_io(io);
2046 if (dataptr != NULL)
2053 cctl_read_capacity(int fd, int target, int lun, int iid, int retries,
2054 int argc, char **argv, char *combinedopt)
2058 struct scsi_read_capacity_data *data;
2059 struct scsi_read_capacity_data_long *longdata;
2060 int cdbsize = -1, retval;
2069 io = ctl_scsi_alloc_io(id);
2071 warn("%s: can't allocate memory\n", __func__);
2075 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2078 cdbsize = strtol(optarg, NULL, 0);
2084 if (cdbsize != -1) {
2090 warnx("%s: invalid cdbsize %d, valid sizes are 10 "
2091 "and 16", __func__, cdbsize);
2094 break; /* NOTREACHED */
2099 dataptr = (uint8_t *)malloc(sizeof(*longdata));
2100 if (dataptr == NULL) {
2101 warn("%s: can't allocate %zd bytes\n", __func__,
2106 memset(dataptr, 0, sizeof(*longdata));
2112 ctl_scsi_read_capacity(io,
2113 /*data_ptr*/ dataptr,
2114 /*data_len*/ sizeof(*longdata),
2118 /*tag_type*/ CTL_TAG_SIMPLE,
2122 ctl_scsi_read_capacity_16(io,
2123 /*data_ptr*/ dataptr,
2124 /*data_len*/ sizeof(*longdata),
2128 /*tag_type*/ CTL_TAG_SIMPLE,
2133 io->io_hdr.nexus.initid = id;
2134 io->io_hdr.nexus.targ_target.id = target;
2135 io->io_hdr.nexus.targ_lun = lun;
2137 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2142 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2146 if (cdbsize == 10) {
2148 data = (struct scsi_read_capacity_data *)dataptr;
2150 maxlba = scsi_4btoul(data->addr);
2151 blocksize = scsi_4btoul(data->length);
2153 if (maxlba == 0xffffffff) {
2158 longdata=(struct scsi_read_capacity_data_long *)dataptr;
2160 maxlba = scsi_8btou64(longdata->addr);
2161 blocksize = scsi_4btoul(longdata->length);
2164 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
2165 (uintmax_t)maxlba, blocksize);
2167 ctl_io_error_print(io, NULL, stderr);
2170 ctl_scsi_free_io(io);
2172 if (dataptr != NULL)
2179 cctl_read_write(int fd, int target, int lun, int iid, int retries,
2180 int argc, char **argv, char *combinedopt,
2181 ctladm_cmdfunction command)
2185 int file_fd, do_stdio;
2186 int cdbsize = -1, databytes;
2188 char *filename = NULL;
2189 int datalen = -1, blocksize = -1;
2201 io = ctl_scsi_alloc_io(id);
2203 warn("%s: can't allocate memory\n", __func__);
2207 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2210 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
2213 blocksize = strtoul(optarg, NULL, 0);
2216 cdbsize = strtoul(optarg, NULL, 0);
2219 datalen = strtoul(optarg, NULL, 0);
2222 filename = strdup(optarg);
2225 lba = strtoull(optarg, NULL, 0);
2232 if (filename == NULL) {
2233 warnx("%s: you must supply a filename using -f", __func__);
2238 if (datalen == -1) {
2239 warnx("%s: you must specify the data length with -d", __func__);
2245 warnx("%s: you must specify the LBA with -l", __func__);
2250 if (blocksize == -1) {
2251 warnx("%s: you must specify the blocksize with -b", __func__);
2256 if (cdbsize != -1) {
2264 warnx("%s: invalid cdbsize %d, valid sizes are 6, "
2265 "10, 12 or 16", __func__, cdbsize);
2268 break; /* NOTREACHED */
2273 databytes = datalen * blocksize;
2274 dataptr = (uint8_t *)malloc(databytes);
2276 if (dataptr == NULL) {
2277 warn("%s: can't allocate %d bytes\n", __func__, databytes);
2281 if (strcmp(filename, "-") == 0) {
2282 if (command == CTLADM_CMD_READ)
2283 file_fd = STDOUT_FILENO;
2285 file_fd = STDIN_FILENO;
2288 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
2289 if (file_fd == -1) {
2290 warn("%s: can't open file %s", __func__, filename);
2296 memset(dataptr, 0, databytes);
2298 if (command == CTLADM_CMD_WRITE) {
2301 bytes_read = read(file_fd, dataptr, databytes);
2302 if (bytes_read == -1) {
2303 warn("%s: error reading file %s", __func__, filename);
2307 if (bytes_read != databytes) {
2308 warnx("%s: only read %d bytes from file %s",
2309 __func__, bytes_read, filename);
2314 ctl_scsi_read_write(io,
2315 /*data_ptr*/ dataptr,
2316 /*data_len*/ databytes,
2317 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
2319 /*minimum_cdb_size*/ cdbsize,
2321 /*num_blocks*/ datalen,
2322 /*tag_type*/ CTL_TAG_SIMPLE,
2325 io->io_hdr.nexus.targ_target.id = target;
2326 io->io_hdr.nexus.targ_lun = lun;
2327 io->io_hdr.nexus.initid = id;
2329 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2334 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2335 && (command == CTLADM_CMD_READ)) {
2338 bytes_written = write(file_fd, dataptr, databytes);
2339 if (bytes_written == -1) {
2340 warn("%s: can't write to %s", __func__, filename);
2343 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
2344 ctl_io_error_print(io, NULL, stderr);
2349 ctl_scsi_free_io(io);
2351 if (dataptr != NULL)
2362 cctl_get_luns(int fd, int target, int lun, int iid, int retries, struct
2363 scsi_report_luns_data **lun_data, uint32_t *num_luns)
2374 io = ctl_scsi_alloc_io(id);
2376 warnx("%s: can't allocate memory", __func__);
2381 * lun_data includes space for 1 lun, allocate space for 4 initially.
2382 * If that isn't enough, we'll allocate more.
2386 lun_datalen = sizeof(*lun_data) +
2387 (nluns * sizeof(struct scsi_report_luns_lundata));
2388 *lun_data = malloc(lun_datalen);
2390 if (*lun_data == NULL) {
2391 warnx("%s: can't allocate memory", __func__);
2392 ctl_scsi_free_io(io);
2396 ctl_scsi_report_luns(io,
2397 /*data_ptr*/ (uint8_t *)*lun_data,
2398 /*data_len*/ lun_datalen,
2399 /*select_report*/ RPL_REPORT_ALL,
2400 /*tag_type*/ CTL_TAG_SIMPLE,
2403 io->io_hdr.nexus.initid = id;
2404 io->io_hdr.nexus.targ_target.id = target;
2405 io->io_hdr.nexus.targ_lun = lun;
2407 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2412 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2413 uint32_t returned_len, returned_luns;
2415 returned_len = scsi_4btoul((*lun_data)->length);
2416 returned_luns = returned_len / 8;
2417 if (returned_luns > nluns) {
2418 nluns = returned_luns;
2422 /* These should be the same */
2423 *num_luns = MIN(returned_luns, nluns);
2425 ctl_io_error_print(io, NULL, stderr);
2429 ctl_scsi_free_io(io);
2435 cctl_report_luns(int fd, int target, int lun, int iid, int retries)
2437 struct scsi_report_luns_data *lun_data;
2438 uint32_t num_luns, i;
2443 if ((retval = cctl_get_luns(fd, target, lun, iid, retries, &lun_data,
2447 fprintf(stdout, "%u LUNs returned\n", num_luns);
2448 for (i = 0; i < num_luns; i++) {
2452 * XXX KDM figure out a way to share this code with
2455 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
2456 case RPL_LUNDATA_ATYP_PERIPH:
2457 lun_val = lun_data->luns[i].lundata[1];
2459 case RPL_LUNDATA_ATYP_FLAT:
2460 lun_val = (lun_data->luns[i].lundata[0] &
2461 RPL_LUNDATA_FLAT_LUN_MASK) |
2462 (lun_data->luns[i].lundata[1] <<
2463 RPL_LUNDATA_FLAT_LUN_BITS);
2465 case RPL_LUNDATA_ATYP_LUN:
2466 case RPL_LUNDATA_ATYP_EXTLUN:
2468 fprintf(stdout, "Unsupported LUN format %d\n",
2469 lun_data->luns[i].lundata[0] &
2470 RPL_LUNDATA_ATYP_MASK);
2477 fprintf(stdout, "%d\n", lun_val);
2481 if (lun_data != NULL)
2488 cctl_tur(int fd, int target, int lun, int iid, int retries)
2495 io = ctl_scsi_alloc_io(id);
2497 fprintf(stderr, "can't allocate memory\n");
2502 /* tag_type */ CTL_TAG_SIMPLE,
2505 io->io_hdr.nexus.targ_target.id = target;
2506 io->io_hdr.nexus.targ_lun = lun;
2507 io->io_hdr.nexus.initid = id;
2509 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2510 ctl_scsi_free_io(io);
2514 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2515 fprintf(stdout, "Unit is ready\n");
2517 ctl_io_error_print(io, NULL, stderr);
2523 cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
2524 char *path_str, int path_len,
2525 struct scsi_inquiry_data *inq_data)
2535 io = ctl_scsi_alloc_io(id);
2537 warnx("cctl_inquiry: can't allocate memory\n");
2541 ctl_scsi_inquiry(/*io*/ io,
2542 /*data_ptr*/ (uint8_t *)inq_data,
2543 /*data_len*/ sizeof(*inq_data),
2546 /*tag_type*/ CTL_TAG_SIMPLE,
2549 io->io_hdr.nexus.targ_target.id = target;
2550 io->io_hdr.nexus.targ_lun = lun;
2551 io->io_hdr.nexus.initid = id;
2553 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2558 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
2560 ctl_io_error_print(io, NULL, stderr);
2561 } else if (path_str != NULL)
2562 ctl_scsi_path_string(io, path_str, path_len);
2565 ctl_scsi_free_io(io);
2571 cctl_inquiry(int fd, int target, int lun, int iid, int retries)
2573 struct scsi_inquiry_data *inq_data;
2579 inq_data = malloc(sizeof(*inq_data));
2580 if (inq_data == NULL) {
2581 warnx("%s: can't allocate inquiry data", __func__);
2586 if ((retval = cctl_get_inquiry(fd, target, lun, iid, retries, scsi_path,
2587 sizeof(scsi_path), inq_data)) != 0)
2590 printf("%s", scsi_path);
2591 scsi_print_inquiry(inq_data);
2594 if (inq_data != NULL)
2601 cctl_req_sense(int fd, int target, int lun, int iid, int retries)
2604 struct scsi_sense_data *sense_data;
2612 io = ctl_scsi_alloc_io(id);
2614 warnx("cctl_req_sense: can't allocate memory\n");
2617 sense_data = malloc(sizeof(*sense_data));
2618 memset(sense_data, 0, sizeof(*sense_data));
2620 ctl_scsi_request_sense(/*io*/ io,
2621 /*data_ptr*/ (uint8_t *)sense_data,
2622 /*data_len*/ sizeof(*sense_data),
2624 /*tag_type*/ CTL_TAG_SIMPLE,
2627 io->io_hdr.nexus.targ_target.id = target;
2628 io->io_hdr.nexus.targ_lun = lun;
2629 io->io_hdr.nexus.initid = id;
2631 if (cctl_do_io(fd, retries, io, __func__) != 0) {
2636 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2637 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2638 io->scsiio.sense_len = sizeof(*sense_data);
2639 ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2641 ctl_io_error_print(io, NULL, stderr);
2645 ctl_scsi_free_io(io);
2652 cctl_report_target_port_group(int fd, int target, int lun, int initiator)
2664 io = ctl_scsi_alloc_io(id);
2666 warn("%s: can't allocate memory", __func__);
2671 dataptr = (uint8_t *)malloc(datalen);
2672 if (dataptr == NULL) {
2673 warn("%s: can't allocate %d bytes", __func__, datalen);
2678 memset(dataptr, 0, datalen);
2680 ctl_scsi_maintenance_in(/*io*/ io,
2681 /*data_ptr*/ dataptr,
2682 /*data_len*/ datalen,
2683 /*action*/ SA_RPRT_TRGT_GRP,
2684 /*tag_type*/ CTL_TAG_SIMPLE,
2687 io->io_hdr.nexus.targ_target.id = target;
2688 io->io_hdr.nexus.targ_lun = lun;
2689 io->io_hdr.nexus.initid = id;
2691 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2696 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2697 int returned_len, used_len;
2699 returned_len = scsi_4btoul(&dataptr[0]) + 4;
2701 for (used_len = 0; used_len < returned_len; used_len++) {
2702 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2703 if (((used_len+1) % 8) == 0)
2704 fprintf(stdout, "\n");
2706 fprintf(stdout, "\n");
2708 ctl_io_error_print(io, NULL, stderr);
2711 ctl_scsi_free_io(io);
2713 if (dataptr != NULL)
2720 cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator)
2732 io = ctl_scsi_alloc_io(id);
2734 warn("%s: can't allocate memory", __func__);
2739 dataptr = (uint8_t *)malloc(datalen);
2740 if (dataptr == NULL) {
2741 warn("%s: can't allocate %d bytes", __func__, datalen);
2746 memset(dataptr, 0, datalen);
2748 ctl_scsi_inquiry(/*io*/ io,
2749 /*data_ptr*/ dataptr,
2750 /*data_len*/ datalen,
2752 /*page_code*/ SVPD_DEVICE_ID,
2753 /*tag_type*/ CTL_TAG_SIMPLE,
2756 io->io_hdr.nexus.targ_target.id = target;
2757 io->io_hdr.nexus.targ_lun = lun;
2758 io->io_hdr.nexus.initid = id;
2760 if (cctl_do_io(fd, 0, io, __func__) != 0) {
2765 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2766 int returned_len, used_len;
2768 returned_len = scsi_2btoul(&dataptr[2]) + 4;
2770 for (used_len = 0; used_len < returned_len; used_len++) {
2771 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2772 if (((used_len+1) % 8) == 0)
2773 fprintf(stdout, "\n");
2775 fprintf(stdout, "\n");
2777 ctl_io_error_print(io, NULL, stderr);
2780 ctl_scsi_free_io(io);
2782 if (dataptr != NULL)
2789 cctl_persistent_reserve_in(int fd, int target, int lun, int initiator,
2790 int argc, char **argv, char *combinedopt,
2805 io = ctl_scsi_alloc_io(id);
2807 warn("%s: can't allocate memory", __func__);
2811 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2814 action = strtol(optarg, NULL, 0);
2821 if (action < 0 || action > 2) {
2822 warn("action must be specified and in the range: 0-2");
2829 dataptr = (uint8_t *)malloc(datalen);
2830 if (dataptr == NULL) {
2831 warn("%s: can't allocate %d bytes", __func__, datalen);
2836 memset(dataptr, 0, datalen);
2838 ctl_scsi_persistent_res_in(io,
2839 /*data_ptr*/ dataptr,
2840 /*data_len*/ datalen,
2842 /*tag_type*/ CTL_TAG_SIMPLE,
2845 io->io_hdr.nexus.targ_target.id = target;
2846 io->io_hdr.nexus.targ_lun = lun;
2847 io->io_hdr.nexus.initid = id;
2849 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2854 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2855 int returned_len, used_len;
2861 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2862 returned_len = min(returned_len, 256);
2865 returned_len = scsi_4btoul(&dataptr[4]) + 8;
2871 warnx("%s: invalid action %d", __func__, action);
2873 break; /* NOTREACHED */
2876 for (used_len = 0; used_len < returned_len; used_len++) {
2877 fprintf(stdout, "0x%02x ", dataptr[used_len]);
2878 if (((used_len+1) % 8) == 0)
2879 fprintf(stdout, "\n");
2881 fprintf(stdout, "\n");
2883 ctl_io_error_print(io, NULL, stderr);
2886 ctl_scsi_free_io(io);
2888 if (dataptr != NULL)
2895 cctl_persistent_reserve_out(int fd, int target, int lun, int initiator,
2896 int argc, char **argv, char *combinedopt,
2902 uint64_t key = 0, sa_key = 0;
2903 int action = -1, restype = -1;
2912 io = ctl_scsi_alloc_io(id);
2914 warn("%s: can't allocate memory", __func__);
2918 while ((c = getopt(argc, argv, combinedopt)) != -1) {
2921 action = strtol(optarg, NULL, 0);
2924 key = strtoull(optarg, NULL, 0);
2927 restype = strtol(optarg, NULL, 0);
2930 sa_key = strtoull(optarg, NULL, 0);
2936 if (action < 0 || action > 5) {
2937 warn("action must be specified and in the range: 0-5");
2942 if (restype < 0 || restype > 5) {
2943 if (action != 0 && action != 5 && action != 3) {
2944 warn("'restype' must specified and in the range: 0-5");
2951 dataptr = (uint8_t *)malloc(datalen);
2952 if (dataptr == NULL) {
2953 warn("%s: can't allocate %d bytes", __func__, datalen);
2958 memset(dataptr, 0, datalen);
2960 ctl_scsi_persistent_res_out(io,
2961 /*data_ptr*/ dataptr,
2962 /*data_len*/ datalen,
2967 /*tag_type*/ CTL_TAG_SIMPLE,
2970 io->io_hdr.nexus.targ_target.id = target;
2971 io->io_hdr.nexus.targ_lun = lun;
2972 io->io_hdr.nexus.initid = id;
2974 if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2978 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2980 ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2981 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2982 "successfully\n", scsi_path);
2984 ctl_io_error_print(io, NULL, stderr);
2987 ctl_scsi_free_io(io);
2989 if (dataptr != NULL)
2995 struct cctl_req_option {
3000 STAILQ_ENTRY(cctl_req_option) links;
3004 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
3006 struct ctl_lun_req req;
3007 int device_type = -1;
3008 uint64_t lun_size = 0;
3009 uint32_t blocksize = 0, req_lun_id = 0;
3010 char *serial_num = NULL;
3011 char *device_id = NULL;
3012 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
3013 char *backend_name = NULL;
3014 STAILQ_HEAD(, cctl_req_option) option_list;
3015 int num_options = 0;
3018 STAILQ_INIT(&option_list);
3020 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3023 backend_name = strdup(optarg);
3026 blocksize = strtoul(optarg, NULL, 0);
3030 device_id = strdup(optarg);
3033 req_lun_id = strtoul(optarg, NULL, 0);
3037 struct cctl_req_option *option;
3041 tmpstr = strdup(optarg);
3042 name = strsep(&tmpstr, "=");
3044 warnx("%s: option -o takes \"name=value\""
3045 "argument", __func__);
3049 value = strsep(&tmpstr, "=");
3050 if (value == NULL) {
3051 warnx("%s: option -o takes \"name=value\""
3052 "argument", __func__);
3056 option = malloc(sizeof(*option));
3057 if (option == NULL) {
3058 warn("%s: error allocating %zd bytes",
3059 __func__, sizeof(*option));
3063 option->name = strdup(name);
3064 option->namelen = strlen(name) + 1;
3065 option->value = strdup(value);
3066 option->vallen = strlen(value) + 1;
3069 STAILQ_INSERT_TAIL(&option_list, option, links);
3074 if (strcasecmp(optarg, "auto") != 0) {
3075 retval = expand_number(optarg, &lun_size);
3077 warn("%s: invalid -s argument",
3086 serial_num = strdup(optarg);
3089 device_type = strtoul(optarg, NULL, 0);
3096 if (backend_name == NULL) {
3097 warnx("%s: backend name (-b) must be specified", __func__);
3102 bzero(&req, sizeof(req));
3104 strlcpy(req.backend, backend_name, sizeof(req.backend));
3105 req.reqtype = CTL_LUNREQ_CREATE;
3107 if (blocksize_set != 0)
3108 req.reqdata.create.blocksize_bytes = blocksize;
3110 if (lun_size_set != 0)
3111 req.reqdata.create.lun_size_bytes = lun_size;
3113 if (lun_id_set != 0) {
3114 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
3115 req.reqdata.create.req_lun_id = req_lun_id;
3118 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
3120 if (device_type != -1)
3121 req.reqdata.create.device_type = device_type;
3123 req.reqdata.create.device_type = T_DIRECT;
3125 if (serial_num != NULL) {
3126 strlcpy(req.reqdata.create.serial_num, serial_num,
3127 sizeof(req.reqdata.create.serial_num));
3128 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
3131 if (device_id != NULL) {
3132 strlcpy(req.reqdata.create.device_id, device_id,
3133 sizeof(req.reqdata.create.device_id));
3134 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
3137 req.num_be_args = num_options;
3138 if (num_options > 0) {
3139 struct cctl_req_option *option, *next_option;
3142 req.be_args = malloc(num_options * sizeof(*req.be_args));
3143 if (req.be_args == NULL) {
3144 warn("%s: error allocating %zd bytes", __func__,
3145 num_options * sizeof(*req.be_args));
3150 for (i = 0, option = STAILQ_FIRST(&option_list);
3151 i < num_options; i++, option = next_option) {
3152 next_option = STAILQ_NEXT(option, links);
3154 req.be_args[i].namelen = option->namelen;
3155 req.be_args[i].name = strdup(option->name);
3156 req.be_args[i].vallen = option->vallen;
3157 req.be_args[i].value = strdup(option->value);
3159 * XXX KDM do we want a way to specify a writeable
3160 * flag of some sort? Do we want a way to specify
3163 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3165 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3168 free(option->value);
3173 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3174 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3179 switch (req.status) {
3181 warnx("LUN creation error: %s", req.error_str);
3184 case CTL_LUN_WARNING:
3185 warnx("LUN creation warning: %s", req.error_str);
3190 warnx("unknown LUN creation status: %d", req.status);
3195 fprintf(stdout, "LUN created successfully\n");
3196 fprintf(stdout, "backend: %s\n", req.backend);
3197 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type);
3198 fprintf(stdout, "LUN size: %ju bytes\n",
3199 (uintmax_t)req.reqdata.create.lun_size_bytes);
3200 fprintf(stdout, "blocksize %u bytes\n",
3201 req.reqdata.create.blocksize_bytes);
3202 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id);
3203 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
3204 fprintf(stdout, "Device ID; %s\n", req.reqdata.create.device_id);
3211 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
3213 struct ctl_lun_req req;
3214 uint32_t lun_id = 0;
3216 char *backend_name = NULL;
3217 STAILQ_HEAD(, cctl_req_option) option_list;
3218 int num_options = 0;
3221 STAILQ_INIT(&option_list);
3223 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3226 backend_name = strdup(optarg);
3229 lun_id = strtoul(optarg, NULL, 0);
3233 struct cctl_req_option *option;
3237 tmpstr = strdup(optarg);
3238 name = strsep(&tmpstr, "=");
3240 warnx("%s: option -o takes \"name=value\""
3241 "argument", __func__);
3245 value = strsep(&tmpstr, "=");
3246 if (value == NULL) {
3247 warnx("%s: option -o takes \"name=value\""
3248 "argument", __func__);
3252 option = malloc(sizeof(*option));
3253 if (option == NULL) {
3254 warn("%s: error allocating %zd bytes",
3255 __func__, sizeof(*option));
3259 option->name = strdup(name);
3260 option->namelen = strlen(name) + 1;
3261 option->value = strdup(value);
3262 option->vallen = strlen(value) + 1;
3265 STAILQ_INSERT_TAIL(&option_list, option, links);
3274 if (backend_name == NULL)
3275 errx(1, "%s: backend name (-b) must be specified", __func__);
3277 if (lun_id_set == 0)
3278 errx(1, "%s: LUN id (-l) must be specified", __func__);
3280 bzero(&req, sizeof(req));
3282 strlcpy(req.backend, backend_name, sizeof(req.backend));
3283 req.reqtype = CTL_LUNREQ_RM;
3285 req.reqdata.rm.lun_id = lun_id;
3287 req.num_be_args = num_options;
3288 if (num_options > 0) {
3289 struct cctl_req_option *option, *next_option;
3292 req.be_args = malloc(num_options * sizeof(*req.be_args));
3293 if (req.be_args == NULL) {
3294 warn("%s: error allocating %zd bytes", __func__,
3295 num_options * sizeof(*req.be_args));
3300 for (i = 0, option = STAILQ_FIRST(&option_list);
3301 i < num_options; i++, option = next_option) {
3302 next_option = STAILQ_NEXT(option, links);
3304 req.be_args[i].namelen = option->namelen;
3305 req.be_args[i].name = strdup(option->name);
3306 req.be_args[i].vallen = option->vallen;
3307 req.be_args[i].value = strdup(option->value);
3309 * XXX KDM do we want a way to specify a writeable
3310 * flag of some sort? Do we want a way to specify
3313 req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3315 STAILQ_REMOVE(&option_list, option, cctl_req_option,
3318 free(option->value);
3323 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3324 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3329 switch (req.status) {
3331 warnx("LUN removal error: %s", req.error_str);
3334 case CTL_LUN_WARNING:
3335 warnx("LUN removal warning: %s", req.error_str);
3340 warnx("unknown LUN removal status: %d", req.status);
3345 printf("LUN %d removed successfully\n", lun_id);
3352 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
3354 struct ctl_lun_req req;
3355 uint64_t lun_size = 0;
3356 uint32_t lun_id = 0;
3357 int lun_id_set = 0, lun_size_set = 0;
3358 char *backend_name = NULL;
3361 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3364 backend_name = strdup(optarg);
3367 lun_id = strtoul(optarg, NULL, 0);
3371 if (strcasecmp(optarg, "auto") != 0) {
3372 retval = expand_number(optarg, &lun_size);
3374 warn("%s: invalid -s argument",
3387 if (backend_name == NULL)
3388 errx(1, "%s: backend name (-b) must be specified", __func__);
3390 if (lun_id_set == 0)
3391 errx(1, "%s: LUN id (-l) must be specified", __func__);
3393 if (lun_size_set == 0)
3394 errx(1, "%s: size (-s) must be specified", __func__);
3396 bzero(&req, sizeof(req));
3398 strlcpy(req.backend, backend_name, sizeof(req.backend));
3399 req.reqtype = CTL_LUNREQ_MODIFY;
3401 req.reqdata.modify.lun_id = lun_id;
3402 req.reqdata.modify.lun_size_bytes = lun_size;
3404 if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3405 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3410 switch (req.status) {
3412 warnx("LUN modification error: %s", req.error_str);
3415 case CTL_LUN_WARNING:
3416 warnx("LUN modification warning: %s", req.error_str);
3421 warnx("unknown LUN modification status: %d", req.status);
3426 printf("LUN %d modified successfully\n", lun_id);
3432 struct cctl_islist_conn {
3435 char *initiator_addr;
3436 char *initiator_alias;
3439 char *header_digest;
3441 char *max_data_segment_length;;
3444 STAILQ_ENTRY(cctl_islist_conn) links;
3447 struct cctl_islist_data {
3449 STAILQ_HEAD(,cctl_islist_conn) conn_list;
3450 struct cctl_islist_conn *cur_conn;
3452 struct sbuf *cur_sb[32];
3456 cctl_islist_start_element(void *user_data, const char *name, const char **attr)
3459 struct cctl_islist_data *islist;
3460 struct cctl_islist_conn *cur_conn;
3462 islist = (struct cctl_islist_data *)user_data;
3463 cur_conn = islist->cur_conn;
3465 if ((u_int)islist->level >= (sizeof(islist->cur_sb) /
3466 sizeof(islist->cur_sb[0])))
3467 errx(1, "%s: too many nesting levels, %zd max", __func__,
3468 sizeof(islist->cur_sb) / sizeof(islist->cur_sb[0]));
3470 islist->cur_sb[islist->level] = sbuf_new_auto();
3471 if (islist->cur_sb[islist->level] == NULL)
3472 err(1, "%s: Unable to allocate sbuf", __func__);
3474 if (strcmp(name, "connection") == 0) {
3475 if (cur_conn != NULL)
3476 errx(1, "%s: improper connection element nesting",
3479 cur_conn = calloc(1, sizeof(*cur_conn));
3480 if (cur_conn == NULL)
3481 err(1, "%s: cannot allocate %zd bytes", __func__,
3484 islist->num_conns++;
3485 islist->cur_conn = cur_conn;
3487 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links);
3489 for (i = 0; attr[i] != NULL; i += 2) {
3490 if (strcmp(attr[i], "id") == 0) {
3491 cur_conn->connection_id =
3492 strtoull(attr[i+1], NULL, 0);
3495 "%s: invalid connection attribute %s = %s",
3496 __func__, attr[i], attr[i+1]);
3503 cctl_islist_end_element(void *user_data, const char *name)
3505 struct cctl_islist_data *islist;
3506 struct cctl_islist_conn *cur_conn;
3509 islist = (struct cctl_islist_data *)user_data;
3510 cur_conn = islist->cur_conn;
3512 if ((cur_conn == NULL)
3513 && (strcmp(name, "ctlislist") != 0))
3514 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name);
3516 if (islist->cur_sb[islist->level] == NULL)
3517 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3518 islist->level, name);
3520 sbuf_finish(islist->cur_sb[islist->level]);
3521 str = strdup(sbuf_data(islist->cur_sb[islist->level]));
3523 err(1, "%s can't allocate %zd bytes for string", __func__,
3524 sbuf_len(islist->cur_sb[islist->level]));
3526 sbuf_delete(islist->cur_sb[islist->level]);
3527 islist->cur_sb[islist->level] = NULL;
3530 if (strcmp(name, "initiator") == 0) {
3531 cur_conn->initiator = str;
3533 } else if (strcmp(name, "initiator_addr") == 0) {
3534 cur_conn->initiator_addr = str;
3536 } else if (strcmp(name, "initiator_alias") == 0) {
3537 cur_conn->initiator_alias = str;
3539 } else if (strcmp(name, "target") == 0) {
3540 cur_conn->target = str;
3542 } else if (strcmp(name, "target_alias") == 0) {
3543 cur_conn->target_alias = str;
3545 } else if (strcmp(name, "target_portal_group_tag") == 0) {
3546 } else if (strcmp(name, "header_digest") == 0) {
3547 cur_conn->header_digest = str;
3549 } else if (strcmp(name, "data_digest") == 0) {
3550 cur_conn->data_digest = str;
3552 } else if (strcmp(name, "max_data_segment_length") == 0) {
3553 cur_conn->max_data_segment_length = str;
3555 } else if (strcmp(name, "immediate_data") == 0) {
3556 cur_conn->immediate_data = atoi(str);
3557 } else if (strcmp(name, "iser") == 0) {
3558 cur_conn->iser = atoi(str);
3559 } else if (strcmp(name, "connection") == 0) {
3560 islist->cur_conn = NULL;
3561 } else if (strcmp(name, "ctlislist") == 0) {
3565 * Unknown element; ignore it for forward compatiblity.
3573 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len)
3575 struct cctl_islist_data *islist;
3577 islist = (struct cctl_islist_data *)user_data;
3579 sbuf_bcat(islist->cur_sb[islist->level], str, len);
3583 cctl_islist(int fd, int argc, char **argv, char *combinedopt)
3585 struct ctl_iscsi req;
3586 struct cctl_islist_data islist;
3587 struct cctl_islist_conn *conn;
3592 int c, retval, verbose = 0;
3597 bzero(&islist, sizeof(islist));
3598 STAILQ_INIT(&islist.conn_list);
3600 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3614 conn_str = malloc(conn_len);
3616 bzero(&req, sizeof(req));
3617 req.type = CTL_ISCSI_LIST;
3618 req.data.list.alloc_len = conn_len;
3619 req.data.list.conn_xml = conn_str;
3621 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3622 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3627 if (req.status == CTL_ISCSI_ERROR) {
3628 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s",
3629 __func__, req.error_str);
3630 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) {
3631 conn_len = conn_len << 1;
3635 if (dump_xml != 0) {
3636 printf("%s", conn_str);
3640 parser = XML_ParserCreate(NULL);
3641 if (parser == NULL) {
3642 warn("%s: Unable to create XML parser", __func__);
3647 XML_SetUserData(parser, &islist);
3648 XML_SetElementHandler(parser, cctl_islist_start_element,
3649 cctl_islist_end_element);
3650 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler);
3652 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1);
3654 warnx("%s: Unable to parse XML: Error %d", __func__,
3655 XML_GetErrorCode(parser));
3656 XML_ParserFree(parser);
3660 XML_ParserFree(parser);
3663 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3664 printf("Session ID: %d\n", conn->connection_id);
3665 printf("Initiator name: %s\n", conn->initiator);
3666 printf("Initiator portal: %s\n", conn->initiator_addr);
3667 printf("Initiator alias: %s\n", conn->initiator_alias);
3668 printf("Target name: %s\n", conn->target);
3669 printf("Target alias: %s\n", conn->target_alias);
3670 printf("Header digest: %s\n", conn->header_digest);
3671 printf("Data digest: %s\n", conn->data_digest);
3672 printf("DataSegmentLen: %s\n", conn->max_data_segment_length);
3673 printf("ImmediateData: %s\n", conn->immediate_data ? "Yes" : "No");
3674 printf("iSER (RDMA): %s\n", conn->iser ? "Yes" : "No");
3678 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name",
3680 STAILQ_FOREACH(conn, &islist.conn_list, links) {
3681 printf("%4u %-16s %-36s %-36s\n",
3682 conn->connection_id, conn->initiator_addr, conn->initiator,
3693 cctl_islogout(int fd, int argc, char **argv, char *combinedopt)
3695 struct ctl_iscsi req;
3697 int all = 0, connection_id = -1, nargs = 0;
3698 char *initiator_name = NULL, *initiator_addr = NULL;
3700 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3707 connection_id = strtoul(optarg, NULL, 0);
3711 initiator_name = strdup(optarg);
3712 if (initiator_name == NULL)
3713 err(1, "%s: strdup", __func__);
3717 initiator_addr = strdup(optarg);
3718 if (initiator_addr == NULL)
3719 err(1, "%s: strdup", __func__);
3728 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3731 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3734 bzero(&req, sizeof(req));
3735 req.type = CTL_ISCSI_LOGOUT;
3736 req.data.logout.connection_id = connection_id;
3737 if (initiator_addr != NULL)
3738 strlcpy(req.data.logout.initiator_addr,
3739 initiator_addr, sizeof(req.data.logout.initiator_addr));
3740 if (initiator_name != NULL)
3741 strlcpy(req.data.logout.initiator_name,
3742 initiator_name, sizeof(req.data.logout.initiator_name));
3744 req.data.logout.all = 1;
3746 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3747 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3752 if (req.status != CTL_ISCSI_OK) {
3753 warnx("%s: error returned from CTL iSCSI logout request:\n%s",
3754 __func__, req.error_str);
3759 printf("iSCSI logout requests submitted\n");
3766 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt)
3768 struct ctl_iscsi req;
3770 int all = 0, connection_id = -1, nargs = 0;
3771 char *initiator_name = NULL, *initiator_addr = NULL;
3773 while ((c = getopt(argc, argv, combinedopt)) != -1) {
3780 connection_id = strtoul(optarg, NULL, 0);
3784 initiator_name = strdup(optarg);
3785 if (initiator_name == NULL)
3786 err(1, "%s: strdup", __func__);
3790 initiator_addr = strdup(optarg);
3791 if (initiator_addr == NULL)
3792 err(1, "%s: strdup", __func__);
3801 errx(1, "%s: either -a, -c, -i, or -p must be specified",
3804 errx(1, "%s: only one of -a, -c, -i, or -p may be specified",
3807 bzero(&req, sizeof(req));
3808 req.type = CTL_ISCSI_TERMINATE;
3809 req.data.terminate.connection_id = connection_id;
3810 if (initiator_addr != NULL)
3811 strlcpy(req.data.terminate.initiator_addr,
3812 initiator_addr, sizeof(req.data.terminate.initiator_addr));
3813 if (initiator_name != NULL)
3814 strlcpy(req.data.terminate.initiator_name,
3815 initiator_name, sizeof(req.data.terminate.initiator_name));
3817 req.data.terminate.all = 1;
3819 if (ioctl(fd, CTL_ISCSI, &req) == -1) {
3820 warn("%s: error issuing CTL_ISCSI ioctl", __func__);
3825 if (req.status != CTL_ISCSI_OK) {
3826 warnx("%s: error returned from CTL iSCSI connection "
3827 "termination request:\n%s", __func__, req.error_str);
3832 printf("iSCSI connections terminated\n");
3839 * Name/value pair used for per-LUN attributes.
3841 struct cctl_lun_nv {
3844 STAILQ_ENTRY(cctl_lun_nv) links;
3848 * Backend LUN information.
3853 uint64_t size_blocks;
3855 char *serial_number;
3857 STAILQ_HEAD(,cctl_lun_nv) attr_list;
3858 STAILQ_ENTRY(cctl_lun) links;
3861 struct cctl_devlist_data {
3863 STAILQ_HEAD(,cctl_lun) lun_list;
3864 struct cctl_lun *cur_lun;
3866 struct sbuf *cur_sb[32];
3870 cctl_start_element(void *user_data, const char *name, const char **attr)
3873 struct cctl_devlist_data *devlist;
3874 struct cctl_lun *cur_lun;
3876 devlist = (struct cctl_devlist_data *)user_data;
3877 cur_lun = devlist->cur_lun;
3879 if ((u_int)devlist->level >= (sizeof(devlist->cur_sb) /
3880 sizeof(devlist->cur_sb[0])))
3881 errx(1, "%s: too many nesting levels, %zd max", __func__,
3882 sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3884 devlist->cur_sb[devlist->level] = sbuf_new_auto();
3885 if (devlist->cur_sb[devlist->level] == NULL)
3886 err(1, "%s: Unable to allocate sbuf", __func__);
3888 if (strcmp(name, "lun") == 0) {
3889 if (cur_lun != NULL)
3890 errx(1, "%s: improper lun element nesting", __func__);
3892 cur_lun = calloc(1, sizeof(*cur_lun));
3893 if (cur_lun == NULL)
3894 err(1, "%s: cannot allocate %zd bytes", __func__,
3897 devlist->num_luns++;
3898 devlist->cur_lun = cur_lun;
3900 STAILQ_INIT(&cur_lun->attr_list);
3901 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3903 for (i = 0; attr[i] != NULL; i += 2) {
3904 if (strcmp(attr[i], "id") == 0) {
3905 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3907 errx(1, "%s: invalid LUN attribute %s = %s",
3908 __func__, attr[i], attr[i+1]);
3915 cctl_end_element(void *user_data, const char *name)
3917 struct cctl_devlist_data *devlist;
3918 struct cctl_lun *cur_lun;
3921 devlist = (struct cctl_devlist_data *)user_data;
3922 cur_lun = devlist->cur_lun;
3924 if ((cur_lun == NULL)
3925 && (strcmp(name, "ctllunlist") != 0))
3926 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3928 if (devlist->cur_sb[devlist->level] == NULL)
3929 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3930 devlist->level, name);
3932 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0)
3933 err(1, "%s: sbuf_finish", __func__);
3934 str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3936 err(1, "%s can't allocate %zd bytes for string", __func__,
3937 sbuf_len(devlist->cur_sb[devlist->level]));
3939 if (strlen(str) == 0) {
3944 sbuf_delete(devlist->cur_sb[devlist->level]);
3945 devlist->cur_sb[devlist->level] = NULL;
3948 if (strcmp(name, "backend_type") == 0) {
3949 cur_lun->backend_type = str;
3951 } else if (strcmp(name, "size") == 0) {
3952 cur_lun->size_blocks = strtoull(str, NULL, 0);
3953 } else if (strcmp(name, "blocksize") == 0) {
3954 cur_lun->blocksize = strtoul(str, NULL, 0);
3955 } else if (strcmp(name, "serial_number") == 0) {
3956 cur_lun->serial_number = str;
3958 } else if (strcmp(name, "device_id") == 0) {
3959 cur_lun->device_id = str;
3961 } else if (strcmp(name, "lun") == 0) {
3962 devlist->cur_lun = NULL;
3963 } else if (strcmp(name, "ctllunlist") == 0) {
3966 struct cctl_lun_nv *nv;
3968 nv = calloc(1, sizeof(*nv));
3970 err(1, "%s: can't allocate %zd bytes for nv pair",
3971 __func__, sizeof(*nv));
3973 nv->name = strdup(name);
3974 if (nv->name == NULL)
3975 err(1, "%s: can't allocated %zd bytes for string",
3976 __func__, strlen(name));
3980 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3987 cctl_char_handler(void *user_data, const XML_Char *str, int len)
3989 struct cctl_devlist_data *devlist;
3991 devlist = (struct cctl_devlist_data *)user_data;
3993 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3997 cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3999 struct ctl_lun_list list;
4000 struct cctl_devlist_data devlist;
4001 struct cctl_lun *lun;
4007 char *backend = NULL;
4013 bzero(&devlist, sizeof(devlist));
4014 STAILQ_INIT(&devlist.lun_list);
4016 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4019 backend = strdup(optarg);
4033 lun_str = malloc(lun_len);
4035 bzero(&list, sizeof(list));
4036 list.alloc_len = lun_len;
4037 list.status = CTL_LUN_LIST_NONE;
4038 list.lun_xml = lun_str;
4040 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
4041 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
4046 if (list.status == CTL_LUN_LIST_ERROR) {
4047 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
4048 __func__, list.error_str);
4049 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
4050 lun_len = lun_len << 1;
4054 if (dump_xml != 0) {
4055 printf("%s", lun_str);
4059 parser = XML_ParserCreate(NULL);
4060 if (parser == NULL) {
4061 warn("%s: Unable to create XML parser", __func__);
4066 XML_SetUserData(parser, &devlist);
4067 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
4068 XML_SetCharacterDataHandler(parser, cctl_char_handler);
4070 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
4072 warnx("%s: Unable to parse XML: Error %d", __func__,
4073 XML_GetErrorCode(parser));
4074 XML_ParserFree(parser);
4078 XML_ParserFree(parser);
4080 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
4081 "Serial Number", "Device ID");
4082 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
4083 struct cctl_lun_nv *nv;
4085 if ((backend != NULL)
4086 && (strcmp(lun->backend_type, backend) != 0))
4089 printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
4090 (uintmax_t)lun->lun_id,
4091 lun->backend_type, (uintmax_t)lun->size_blocks,
4092 lun->blocksize, lun->serial_number, lun->device_id);
4097 STAILQ_FOREACH(nv, &lun->attr_list, links) {
4098 printf(" %s=%s\n", nv->name, nv->value);
4113 char *frontend_type;
4116 char *target, *port, *lun_map;
4117 STAILQ_HEAD(,cctl_lun_nv) init_list;
4118 STAILQ_HEAD(,cctl_lun_nv) lun_list;
4119 STAILQ_HEAD(,cctl_lun_nv) attr_list;
4120 STAILQ_ENTRY(cctl_port) links;
4123 struct cctl_portlist_data {
4125 STAILQ_HEAD(,cctl_port) port_list;
4126 struct cctl_port *cur_port;
4129 struct sbuf *cur_sb[32];
4133 cctl_start_pelement(void *user_data, const char *name, const char **attr)
4136 struct cctl_portlist_data *portlist;
4137 struct cctl_port *cur_port;
4139 portlist = (struct cctl_portlist_data *)user_data;
4140 cur_port = portlist->cur_port;
4142 if ((u_int)portlist->level >= (sizeof(portlist->cur_sb) /
4143 sizeof(portlist->cur_sb[0])))
4144 errx(1, "%s: too many nesting levels, %zd max", __func__,
4145 sizeof(portlist->cur_sb) / sizeof(portlist->cur_sb[0]));
4147 portlist->cur_sb[portlist->level] = sbuf_new_auto();
4148 if (portlist->cur_sb[portlist->level] == NULL)
4149 err(1, "%s: Unable to allocate sbuf", __func__);
4151 portlist->cur_id = 0;
4152 for (i = 0; attr[i] != NULL; i += 2) {
4153 if (strcmp(attr[i], "id") == 0) {
4154 portlist->cur_id = strtoull(attr[i+1], NULL, 0);
4159 if (strcmp(name, "targ_port") == 0) {
4160 if (cur_port != NULL)
4161 errx(1, "%s: improper port element nesting", __func__);
4163 cur_port = calloc(1, sizeof(*cur_port));
4164 if (cur_port == NULL)
4165 err(1, "%s: cannot allocate %zd bytes", __func__,
4168 portlist->num_ports++;
4169 portlist->cur_port = cur_port;
4171 STAILQ_INIT(&cur_port->init_list);
4172 STAILQ_INIT(&cur_port->lun_list);
4173 STAILQ_INIT(&cur_port->attr_list);
4174 cur_port->port_id = portlist->cur_id;
4175 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links);
4180 cctl_end_pelement(void *user_data, const char *name)
4182 struct cctl_portlist_data *portlist;
4183 struct cctl_port *cur_port;
4186 portlist = (struct cctl_portlist_data *)user_data;
4187 cur_port = portlist->cur_port;
4189 if ((cur_port == NULL)
4190 && (strcmp(name, "ctlportlist") != 0))
4191 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name);
4193 if (portlist->cur_sb[portlist->level] == NULL)
4194 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
4195 portlist->level, name);
4197 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0)
4198 err(1, "%s: sbuf_finish", __func__);
4199 str = strdup(sbuf_data(portlist->cur_sb[portlist->level]));
4201 err(1, "%s can't allocate %zd bytes for string", __func__,
4202 sbuf_len(portlist->cur_sb[portlist->level]));
4204 if (strlen(str) == 0) {
4209 sbuf_delete(portlist->cur_sb[portlist->level]);
4210 portlist->cur_sb[portlist->level] = NULL;
4213 if (strcmp(name, "frontend_type") == 0) {
4214 cur_port->frontend_type = str;
4216 } else if (strcmp(name, "port_name") == 0) {
4217 cur_port->name = str;
4219 } else if (strcmp(name, "online") == 0) {
4220 cur_port->online = str;
4222 } else if (strcmp(name, "physical_port") == 0) {
4223 cur_port->pp = strtoull(str, NULL, 0);
4224 } else if (strcmp(name, "virtual_port") == 0) {
4225 cur_port->vp = strtoull(str, NULL, 0);
4226 } else if (strcmp(name, "target") == 0) {
4227 cur_port->target = str;
4229 } else if (strcmp(name, "port") == 0) {
4230 cur_port->port = str;
4232 } else if (strcmp(name, "lun_map") == 0) {
4233 cur_port->lun_map = str;
4235 } else if (strcmp(name, "targ_port") == 0) {
4236 portlist->cur_port = NULL;
4237 } else if (strcmp(name, "ctlportlist") == 0) {
4240 struct cctl_lun_nv *nv;
4242 nv = calloc(1, sizeof(*nv));
4244 err(1, "%s: can't allocate %zd bytes for nv pair",
4245 __func__, sizeof(*nv));
4247 if (strcmp(name, "initiator") == 0 ||
4248 strcmp(name, "lun") == 0)
4249 asprintf(&nv->name, "%ju", portlist->cur_id);
4251 nv->name = strdup(name);
4252 if (nv->name == NULL)
4253 err(1, "%s: can't allocated %zd bytes for string",
4254 __func__, strlen(name));
4258 if (strcmp(name, "initiator") == 0)
4259 STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links);
4260 else if (strcmp(name, "lun") == 0)
4261 STAILQ_INSERT_TAIL(&cur_port->lun_list, nv, links);
4263 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links);
4270 cctl_char_phandler(void *user_data, const XML_Char *str, int len)
4272 struct cctl_portlist_data *portlist;
4274 portlist = (struct cctl_portlist_data *)user_data;
4276 sbuf_bcat(portlist->cur_sb[portlist->level], str, len);
4280 cctl_portlist(int fd, int argc, char **argv, char *combinedopt)
4282 struct ctl_lun_list list;
4283 struct cctl_portlist_data portlist;
4284 struct cctl_port *port;
4290 char *frontend = NULL;
4291 uint64_t portarg = UINT64_MAX;
4292 int verbose = 0, init = 0, lun = 0, quiet = 0;
4297 bzero(&portlist, sizeof(portlist));
4298 STAILQ_INIT(&portlist.port_list);
4300 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4303 frontend = strdup(optarg);
4312 portarg = strtoll(optarg, NULL, 0);
4329 port_str = malloc(port_len);
4331 bzero(&list, sizeof(list));
4332 list.alloc_len = port_len;
4333 list.status = CTL_LUN_LIST_NONE;
4334 list.lun_xml = port_str;
4336 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) {
4337 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__);
4342 if (list.status == CTL_LUN_LIST_ERROR) {
4343 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s",
4344 __func__, list.error_str);
4345 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
4346 port_len = port_len << 1;
4350 if (dump_xml != 0) {
4351 printf("%s", port_str);
4355 parser = XML_ParserCreate(NULL);
4356 if (parser == NULL) {
4357 warn("%s: Unable to create XML parser", __func__);
4362 XML_SetUserData(parser, &portlist);
4363 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement);
4364 XML_SetCharacterDataHandler(parser, cctl_char_phandler);
4366 retval = XML_Parse(parser, port_str, strlen(port_str), 1);
4368 warnx("%s: Unable to parse XML: Error %d", __func__,
4369 XML_GetErrorCode(parser));
4370 XML_ParserFree(parser);
4374 XML_ParserFree(parser);
4377 printf("Port Online Frontend Name pp vp\n");
4378 STAILQ_FOREACH(port, &portlist.port_list, links) {
4379 struct cctl_lun_nv *nv;
4381 if ((frontend != NULL)
4382 && (strcmp(port->frontend_type, frontend) != 0))
4385 if ((portarg != UINT64_MAX) && (portarg != port->port_id))
4388 printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n",
4389 (uintmax_t)port->port_id, port->online,
4390 port->frontend_type, port->name, port->pp, port->vp,
4391 port->port ? port->port : "");
4393 if (init || verbose) {
4395 printf(" Target: %s\n", port->target);
4396 STAILQ_FOREACH(nv, &port->init_list, links) {
4397 printf(" Initiator %s: %s\n",
4398 nv->name, nv->value);
4402 if (lun || verbose) {
4403 if (port->lun_map) {
4404 STAILQ_FOREACH(nv, &port->lun_list, links)
4405 printf(" LUN %s: %s\n",
4406 nv->name, nv->value);
4407 if (STAILQ_EMPTY(&port->lun_list))
4408 printf(" No LUNs mapped\n");
4410 printf(" All LUNs mapped\n");
4414 STAILQ_FOREACH(nv, &port->attr_list, links) {
4415 printf(" %s=%s\n", nv->name, nv->value);
4426 cctl_lunmap(int fd, int argc, char **argv, char *combinedopt)
4428 struct ctl_lun_map lm;
4432 lm.port = UINT32_MAX;
4433 lm.plun = UINT32_MAX;
4434 lm.lun = UINT32_MAX;
4436 while ((c = getopt(argc, argv, combinedopt)) != -1) {
4439 lm.port = strtoll(optarg, NULL, 0);
4442 lm.plun = strtoll(optarg, NULL, 0);
4445 lm.lun = strtoll(optarg, NULL, 0);
4452 if (ioctl(fd, CTL_LUN_MAP, &lm) == -1) {
4453 warn("%s: error issuing CTL_LUN_MAP ioctl", __func__);
4463 fprintf(error ? stderr : stdout,
4465 "Primary commands:\n"
4466 " ctladm tur [dev_id][general options]\n"
4467 " ctladm inquiry [dev_id][general options]\n"
4468 " ctladm devid [dev_id][general options]\n"
4469 " ctladm reqsense [dev_id][general options]\n"
4470 " ctladm reportluns [dev_id][general options]\n"
4471 " ctladm read [dev_id][general options] <-l lba> <-d len>\n"
4472 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4473 " ctladm write [dev_id][general options] <-l lba> <-d len>\n"
4474 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
4475 " ctladm readcap [dev_id][general options] [-c cdbsize]\n"
4476 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n"
4477 " [-d] [-S subpage] [-c cdbsize]\n"
4478 " ctladm prin [dev_id][general options] <-a action>\n"
4479 " ctladm prout [dev_id][general options] <-a action>\n"
4480 " <-r restype] [-k key] [-s sa_key]\n"
4481 " ctladm rtpg [dev_id][general options]\n"
4482 " ctladm start [dev_id][general options] [-i] [-o]\n"
4483 " ctladm stop [dev_id][general options] [-i] [-o]\n"
4484 " ctladm synccache [dev_id][general options] [-l lba]\n"
4485 " [-b blockcount] [-r] [-i] [-c cdbsize]\n"
4486 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n"
4487 " [-l lun_id] [-o name=value] [-s size_bytes]\n"
4488 " [-S serial_num] [-t dev_type]\n"
4489 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n"
4490 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n"
4491 " ctladm devlist [-b backend] [-v] [-x]\n"
4492 " ctladm shutdown\n"
4494 " ctladm hardstop\n"
4495 " ctladm hardstart\n"
4497 " ctladm lunmap -p targ_port [-l pLUN] [-L cLUN]\n"
4498 " ctladm bbrread [dev_id] <-l lba> <-d datalen>\n"
4499 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
4501 " ctladm realsync <on|off|query>\n"
4502 " ctladm setsync [dev_id] <-i interval>\n"
4503 " ctladm getsync [dev_id]\n"
4504 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
4505 " [-s len fmt [args]] [-c] [-d delete_id]\n"
4506 " ctladm port <-l | -o <on|off> | [-w wwnn][-W wwpn]>\n"
4507 " [-p targ_port] [-t port_type] [-q] [-x]\n"
4508 " ctladm portlist [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n"
4509 " ctladm islist [-v | -x]\n"
4510 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n"
4511 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n"
4513 " ctladm dumpstructs\n"
4515 "General Options:\n"
4516 "-I intiator_id : defaults to 7, used to change the initiator id\n"
4517 "-C retries : specify the number of times to retry this command\n"
4518 "-D devicename : specify the device to operate on\n"
4519 " : (default is %s)\n"
4520 "read/write options:\n"
4521 "-l lba : logical block address\n"
4522 "-d len : read/write length, in blocks\n"
4523 "-f file|- : write/read data to/from file or stdout/stdin\n"
4524 "-b blocksize : block size, in bytes\n"
4525 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n"
4526 "-N : do not copy data to/from userland\n"
4527 "readcapacity options:\n"
4528 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4529 "modesense options:\n"
4530 "-m page : specify the mode page to view\n"
4531 "-l : request a list of supported pages\n"
4532 "-P pc : specify the page control value: 0-3 (current,\n"
4533 " changeable, default, saved, respectively)\n"
4534 "-d : disable block descriptors for mode sense\n"
4535 "-S subpage : specify a subpage\n"
4536 "-c cdbsize : specify minimum cdb size: 6 or 10\n"
4537 "persistent reserve in options:\n"
4538 "-a action : specify the action value: 0-2 (read key, read\n"
4539 " reservation, read capabilities, respectively)\n"
4540 "persistent reserve out options:\n"
4541 "-a action : specify the action value: 0-5 (register, reserve,\n"
4542 " release, clear, preempt, register and ignore)\n"
4543 "-k key : key value\n"
4544 "-s sa_key : service action value\n"
4545 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n"
4546 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
4547 "start/stop options:\n"
4548 "-i : set the immediate bit (CTL does not support this)\n"
4549 "-o : set the on/offline bit\n"
4550 "synccache options:\n"
4551 "-l lba : set the starting LBA\n"
4552 "-b blockcount : set the length to sync in blocks\n"
4553 "-r : set the relative addressing bit\n"
4554 "-i : set the immediate bit\n"
4555 "-c cdbsize : specify minimum cdb size: 10 or 16\n"
4557 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4558 "-B blocksize : LUN blocksize in bytes (some backends)\n"
4559 "-d device_id : SCSI VPD page 0x83 ID\n"
4560 "-l lun_id : requested LUN number\n"
4561 "-o name=value : backend-specific options, multiple allowed\n"
4562 "-s size_bytes : LUN size in bytes (some backends)\n"
4563 "-S serial_num : SCSI VPD page 0x80 serial number\n"
4564 "-t dev_type : SCSI device type (0=disk, 3=processor)\n"
4566 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n"
4567 "-l lun_id : LUN number to delete\n"
4568 "-o name=value : backend-specific options, multiple allowed\n"
4569 "devlist options:\n"
4570 "-b backend : list devices from specified backend only\n"
4571 "-v : be verbose, show backend attributes\n"
4572 "-x : dump raw XML\n"
4574 "-l datamove|done : delay command at datamove or done phase\n"
4575 "-T oneshot : delay one command, then resume normal completion\n"
4576 "-T cont : delay all commands\n"
4577 "-t secs : number of seconds to delay\n"
4579 "-i error_action : action to perform\n"
4580 "-p pattern : command pattern to look for\n"
4581 "-r lba,len : LBA range for pattern\n"
4582 "-s len fmt [args] : sense data for custom sense action\n"
4583 "-c : continuous operation\n"
4584 "-d delete_id : error id to delete\n"
4586 "-l : list frontend ports\n"
4587 "-o on|off : turn frontend ports on or off\n"
4588 "-w wwnn : set WWNN for one frontend\n"
4589 "-W wwpn : set WWPN for one frontend\n"
4590 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n"
4591 "-p targ_port : specify target port number\n"
4592 "-q : omit header in list output\n"
4593 "-x : output port list in XML format\n"
4594 "portlist options:\n"
4595 "-f fronetnd : specify frontend type\n"
4596 "-i : report target and initiators addresses\n"
4597 "-l : report LUN mapping\n"
4598 "-p targ_port : specify target port number\n"
4599 "-q : omit header in list output\n"
4600 "-v : verbose output (report all port options)\n"
4601 "-x : output port list in XML format\n"
4603 "-p targ_port : specify target port number\n"
4604 "-L pLUN : specify port-visible LUN\n"
4605 "-L cLUN : specify CTL LUN\n"
4606 "bbrread options:\n"
4607 "-l lba : starting LBA\n"
4608 "-d datalen : length, in bytes, to read\n",
4613 main(int argc, char **argv)
4616 ctladm_cmdfunction command;
4617 ctladm_cmdargs cmdargs;
4618 ctladm_optret optreturn;
4620 const char *mainopt = "C:D:I:";
4621 const char *subopt = NULL;
4622 char combinedopt[256];
4631 cmdargs = CTLADM_ARG_NONE;
4632 command = CTLADM_CMD_HELP;
4647 * Get the base option.
4649 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
4651 if (optreturn == CC_OR_AMBIGUOUS) {
4652 warnx("ambiguous option %s", argv[1]);
4655 } else if (optreturn == CC_OR_NOT_FOUND) {
4656 warnx("option %s not found", argv[1]);
4661 if (cmdargs & CTLADM_ARG_NEED_TL) {
4663 || (!isdigit(argv[2][0]))) {
4664 warnx("option %s requires a target:lun argument",
4669 retval = cctl_parse_tl(argv[2], &target, &lun);
4671 errx(1, "invalid target:lun argument %s", argv[2]);
4673 cmdargs |= CTLADM_ARG_TARG_LUN;
4678 * Ahh, getopt(3) is a pain.
4680 * This is a gross hack. There really aren't many other good
4681 * options (excuse the pun) for parsing options in a situation like
4682 * this. getopt is kinda braindead, so you end up having to run
4683 * through the options twice, and give each invocation of getopt
4684 * the option string for the other invocation.
4686 * You would think that you could just have two groups of options.
4687 * The first group would get parsed by the first invocation of
4688 * getopt, and the second group would get parsed by the second
4689 * invocation of getopt. It doesn't quite work out that way. When
4690 * the first invocation of getopt finishes, it leaves optind pointing
4691 * to the argument _after_ the first argument in the second group.
4692 * So when the second invocation of getopt comes around, it doesn't
4693 * recognize the first argument it gets and then bails out.
4695 * A nice alternative would be to have a flag for getopt that says
4696 * "just keep parsing arguments even when you encounter an unknown
4697 * argument", but there isn't one. So there's no real clean way to
4698 * easily parse two sets of arguments without having one invocation
4699 * of getopt know about the other.
4701 * Without this hack, the first invocation of getopt would work as
4702 * long as the generic arguments are first, but the second invocation
4703 * (in the subfunction) would fail in one of two ways. In the case
4704 * where you don't set optreset, it would fail because optind may be
4705 * pointing to the argument after the one it should be pointing at.
4706 * In the case where you do set optreset, and reset optind, it would
4707 * fail because getopt would run into the first set of options, which
4708 * it doesn't understand.
4710 * All of this would "sort of" work if you could somehow figure out
4711 * whether optind had been incremented one option too far. The
4712 * mechanics of that, however, are more daunting than just giving
4713 * both invocations all of the expect options for either invocation.
4715 * Needless to say, I wouldn't mind if someone invented a better
4716 * (non-GPL!) command line parsing interface than getopt. I
4717 * wouldn't mind if someone added more knobs to getopt to make it
4718 * work better. Who knows, I may talk myself into doing it someday,
4719 * if the standards weenies let me. As it is, it just leads to
4720 * hackery like this and causes people to avoid it in some cases.
4722 * KDM, September 8th, 1998
4725 sprintf(combinedopt, "%s%s", mainopt, subopt);
4727 sprintf(combinedopt, "%s", mainopt);
4730 * Start getopt processing at argv[2/3], since we've already
4731 * accepted argv[1..2] as the command name, and as a possible
4737 * Now we run through the argument list looking for generic
4738 * options, and ignoring options that possibly belong to
4741 while ((c = getopt(argc, argv, combinedopt))!= -1){
4744 cmdargs |= CTLADM_ARG_RETRIES;
4745 retries = strtol(optarg, NULL, 0);
4748 device = strdup(optarg);
4749 cmdargs |= CTLADM_ARG_DEVICE;
4752 cmdargs |= CTLADM_ARG_INITIATOR;
4753 initid = strtol(optarg, NULL, 0);
4760 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
4767 * Default to opening the CTL device for now.
4769 if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
4770 && (command != CTLADM_CMD_HELP)) {
4771 device = strdup(CTL_DEFAULT_DEV);
4772 cmdargs |= CTLADM_ARG_DEVICE;
4775 if ((cmdargs & CTLADM_ARG_DEVICE)
4776 && (command != CTLADM_CMD_HELP)) {
4777 fd = open(device, O_RDWR);
4778 if (fd == -1 && errno == ENOENT) {
4779 saved_errno = errno;
4780 retval = kldload("ctl");
4782 fd = open(device, O_RDWR);
4784 errno = saved_errno;
4787 fprintf(stderr, "%s: error opening %s: %s\n",
4788 argv[0], device, strerror(errno));
4792 } else if ((command != CTLADM_CMD_HELP)
4793 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
4794 fprintf(stderr, "%s: you must specify a device with the "
4795 "--device argument for this command\n", argv[0]);
4796 command = CTLADM_CMD_HELP;
4801 case CTLADM_CMD_TUR:
4802 retval = cctl_tur(fd, target, lun, initid, retries);
4804 case CTLADM_CMD_INQUIRY:
4805 retval = cctl_inquiry(fd, target, lun, initid, retries);
4807 case CTLADM_CMD_REQ_SENSE:
4808 retval = cctl_req_sense(fd, target, lun, initid, retries);
4810 case CTLADM_CMD_REPORT_LUNS:
4811 retval = cctl_report_luns(fd, target, lun, initid, retries);
4813 case CTLADM_CMD_CREATE:
4814 retval = cctl_create_lun(fd, argc, argv, combinedopt);
4817 retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4819 case CTLADM_CMD_DEVLIST:
4820 retval = cctl_devlist(fd, argc, argv, combinedopt);
4822 case CTLADM_CMD_READ:
4823 case CTLADM_CMD_WRITE:
4824 retval = cctl_read_write(fd, target, lun, initid, retries,
4825 argc, argv, combinedopt, command);
4827 case CTLADM_CMD_PORT:
4828 retval = cctl_port(fd, argc, argv, combinedopt);
4830 case CTLADM_CMD_PORTLIST:
4831 retval = cctl_portlist(fd, argc, argv, combinedopt);
4833 case CTLADM_CMD_LUNMAP:
4834 retval = cctl_lunmap(fd, argc, argv, combinedopt);
4836 case CTLADM_CMD_READCAPACITY:
4837 retval = cctl_read_capacity(fd, target, lun, initid, retries,
4838 argc, argv, combinedopt);
4840 case CTLADM_CMD_MODESENSE:
4841 retval = cctl_mode_sense(fd, target, lun, initid, retries,
4842 argc, argv, combinedopt);
4844 case CTLADM_CMD_START:
4845 case CTLADM_CMD_STOP:
4846 retval = cctl_start_stop(fd, target, lun, initid, retries,
4847 (command == CTLADM_CMD_START) ? 1 : 0,
4848 argc, argv, combinedopt);
4850 case CTLADM_CMD_SYNC_CACHE:
4851 retval = cctl_sync_cache(fd, target, lun, initid, retries,
4852 argc, argv, combinedopt);
4854 case CTLADM_CMD_SHUTDOWN:
4855 case CTLADM_CMD_STARTUP:
4856 retval = cctl_startup_shutdown(fd, target, lun, initid,
4859 case CTLADM_CMD_HARDSTOP:
4860 case CTLADM_CMD_HARDSTART:
4861 retval = cctl_hardstopstart(fd, command);
4863 case CTLADM_CMD_BBRREAD:
4864 retval = cctl_bbrread(fd, target, lun, initid, argc, argv,
4867 case CTLADM_CMD_LUNLIST:
4868 retval = cctl_lunlist(fd);
4870 case CTLADM_CMD_DELAY:
4871 retval = cctl_delay(fd, target, lun, argc, argv, combinedopt);
4873 case CTLADM_CMD_REALSYNC:
4874 retval = cctl_realsync(fd, argc, argv);
4876 case CTLADM_CMD_SETSYNC:
4877 case CTLADM_CMD_GETSYNC:
4878 retval = cctl_getsetsync(fd, target, lun, command,
4879 argc, argv, combinedopt);
4881 case CTLADM_CMD_ERR_INJECT:
4882 retval = cctl_error_inject(fd, target, lun, argc, argv,
4885 case CTLADM_CMD_DUMPOOA:
4886 retval = cctl_dump_ooa(fd, argc, argv);
4888 case CTLADM_CMD_DUMPSTRUCTS:
4889 retval = cctl_dump_structs(fd, cmdargs);
4891 case CTLADM_CMD_PRES_IN:
4892 retval = cctl_persistent_reserve_in(fd, target, lun, initid,
4893 argc, argv, combinedopt,
4896 case CTLADM_CMD_PRES_OUT:
4897 retval = cctl_persistent_reserve_out(fd, target, lun, initid,
4898 argc, argv, combinedopt,
4901 case CTLADM_CMD_INQ_VPD_DEVID:
4902 retval = cctl_inquiry_vpd_devid(fd, target, lun, initid);
4904 case CTLADM_CMD_RTPG:
4905 retval = cctl_report_target_port_group(fd, target, lun, initid);
4907 case CTLADM_CMD_MODIFY:
4908 retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4910 case CTLADM_CMD_ISLIST:
4911 retval = cctl_islist(fd, argc, argv, combinedopt);
4913 case CTLADM_CMD_ISLOGOUT:
4914 retval = cctl_islogout(fd, argc, argv, combinedopt);
4916 case CTLADM_CMD_ISTERMINATE:
4917 retval = cctl_isterminate(fd, argc, argv, combinedopt);
4919 case CTLADM_CMD_HELP: