1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2003, 2004 Silicon Graphics International Corp. 5 * Copyright (c) 1997-2007 Kenneth D. Merry 6 * Copyright (c) 2012 The FreeBSD Foundation 7 * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org> 8 * All rights reserved. 9 * 10 * Portions of this software were developed by Edward Tomasz Napierala 11 * under sponsorship from the FreeBSD Foundation. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions, and the following disclaimer, 18 * without modification. 19 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 20 * substantially similar to the "NO WARRANTY" disclaimer below 21 * ("Disclaimer") and any redistribution must be conditioned upon 22 * including a substantially similar Disclaimer requirement for further 23 * binary redistribution. 24 * 25 * NO WARRANTY 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGES. 37 * 38 * $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.c#4 $ 39 */ 40 /* 41 * CAM Target Layer exercise program. 42 * 43 * Author: Ken Merry <ken@FreeBSD.org> 44 */ 45 46 #include <sys/param.h> 47 #include <sys/callout.h> 48 #include <sys/ioctl.h> 49 #include <sys/linker.h> 50 #include <sys/module.h> 51 #include <sys/queue.h> 52 #include <sys/sbuf.h> 53 #include <sys/nv.h> 54 #include <sys/stat.h> 55 #include <bsdxml.h> 56 #include <ctype.h> 57 #include <err.h> 58 #include <errno.h> 59 #include <fcntl.h> 60 #include <getopt.h> 61 #include <stdlib.h> 62 #include <stdint.h> 63 #include <stdio.h> 64 #include <string.h> 65 #include <unistd.h> 66 #include <cam/scsi/scsi_all.h> 67 #include <cam/scsi/scsi_message.h> 68 #include <cam/ctl/ctl.h> 69 #include <cam/ctl/ctl_io.h> 70 #include <cam/ctl/ctl_backend.h> 71 #include <cam/ctl/ctl_ioctl.h> 72 #include <cam/ctl/ctl_util.h> 73 #include <cam/ctl/ctl_scsi_all.h> 74 #include <dev/nvmf/nvmf_proto.h> 75 #include <camlib.h> 76 #include <libutil.h> 77 #include "ctladm.h" 78 79 #ifdef min 80 #undef min 81 #endif 82 #define min(x,y) (x < y) ? x : y 83 84 typedef enum { 85 CTLADM_CMD_TUR, 86 CTLADM_CMD_INQUIRY, 87 CTLADM_CMD_REQ_SENSE, 88 CTLADM_CMD_ARRAYLIST, 89 CTLADM_CMD_REPORT_LUNS, 90 CTLADM_CMD_HELP, 91 CTLADM_CMD_DEVLIST, 92 CTLADM_CMD_ADDDEV, 93 CTLADM_CMD_RM, 94 CTLADM_CMD_CREATE, 95 CTLADM_CMD_READ, 96 CTLADM_CMD_WRITE, 97 CTLADM_CMD_PORT, 98 CTLADM_CMD_PORTLIST, 99 CTLADM_CMD_READCAPACITY, 100 CTLADM_CMD_MODESENSE, 101 CTLADM_CMD_DUMPOOA, 102 CTLADM_CMD_DUMPSTRUCTS, 103 CTLADM_CMD_START, 104 CTLADM_CMD_STOP, 105 CTLADM_CMD_SYNC_CACHE, 106 CTLADM_CMD_LUNLIST, 107 CTLADM_CMD_DELAY, 108 CTLADM_CMD_ERR_INJECT, 109 CTLADM_CMD_PRES_IN, 110 CTLADM_CMD_PRES_OUT, 111 CTLADM_CMD_INQ_VPD_DEVID, 112 CTLADM_CMD_RTPG, 113 CTLADM_CMD_MODIFY, 114 CTLADM_CMD_ISLIST, 115 CTLADM_CMD_ISLOGOUT, 116 CTLADM_CMD_ISTERMINATE, 117 CTLADM_CMD_LUNMAP, 118 CTLADM_CMD_NVLIST, 119 CTLADM_CMD_NVTERMINATE 120 } ctladm_cmdfunction; 121 122 typedef enum { 123 CTLADM_ARG_NONE = 0x0000000, 124 CTLADM_ARG_AUTOSENSE = 0x0000001, 125 CTLADM_ARG_DEVICE = 0x0000002, 126 CTLADM_ARG_ARRAYSIZE = 0x0000004, 127 CTLADM_ARG_BACKEND = 0x0000008, 128 CTLADM_ARG_CDBSIZE = 0x0000010, 129 CTLADM_ARG_DATALEN = 0x0000020, 130 CTLADM_ARG_FILENAME = 0x0000040, 131 CTLADM_ARG_LBA = 0x0000080, 132 CTLADM_ARG_PC = 0x0000100, 133 CTLADM_ARG_PAGE_CODE = 0x0000200, 134 CTLADM_ARG_PAGE_LIST = 0x0000400, 135 CTLADM_ARG_SUBPAGE = 0x0000800, 136 CTLADM_ARG_PAGELIST = 0x0001000, 137 CTLADM_ARG_DBD = 0x0002000, 138 CTLADM_ARG_TARG_LUN = 0x0004000, 139 CTLADM_ARG_BLOCKSIZE = 0x0008000, 140 CTLADM_ARG_IMMED = 0x0010000, 141 CTLADM_ARG_RELADR = 0x0020000, 142 CTLADM_ARG_RETRIES = 0x0040000, 143 CTLADM_ARG_ONOFFLINE = 0x0080000, 144 CTLADM_ARG_ONESHOT = 0x0100000, 145 CTLADM_ARG_TIMEOUT = 0x0200000, 146 CTLADM_ARG_INITIATOR = 0x0400000, 147 CTLADM_ARG_NOCOPY = 0x0800000, 148 CTLADM_ARG_NEED_TL = 0x1000000 149 } ctladm_cmdargs; 150 151 struct ctladm_opts { 152 const char *optname; 153 uint32_t cmdnum; 154 ctladm_cmdargs argnum; 155 const char *subopt; 156 }; 157 158 typedef enum { 159 CC_OR_NOT_FOUND, 160 CC_OR_AMBIGUOUS, 161 CC_OR_FOUND 162 } ctladm_optret; 163 164 static const char rw_opts[] = "Nb:c:d:f:l:"; 165 static const char startstop_opts[] = "i"; 166 167 static struct ctladm_opts option_table[] = { 168 {"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL}, 169 {"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"}, 170 {"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"}, 171 {"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL}, 172 {"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"}, 173 {"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL}, 174 {"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL}, 175 {"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL}, 176 {"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"}, 177 {"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL}, 178 {"islist", CTLADM_CMD_ISLIST, CTLADM_ARG_NONE, "vx"}, 179 {"islogout", CTLADM_CMD_ISLOGOUT, CTLADM_ARG_NONE, "ac:i:p:"}, 180 {"isterminate", CTLADM_CMD_ISTERMINATE, CTLADM_ARG_NONE, "ac:i:p:"}, 181 {"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL}, 182 {"lunmap", CTLADM_CMD_LUNMAP, CTLADM_ARG_NONE, "p:l:L:"}, 183 {"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"}, 184 {"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:o:s:"}, 185 {"nvlist", CTLADM_CMD_NVLIST, CTLADM_ARG_NONE, "vx"}, 186 {"nvterminate", CTLADM_CMD_NVTERMINATE, CTLADM_ARG_NONE, "ac:h:"}, 187 {"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:O:d:crp:qt:w:W:x"}, 188 {"portlist", CTLADM_CMD_PORTLIST, CTLADM_ARG_NONE, "f:ilp:qvx"}, 189 {"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"}, 190 {"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"}, 191 {"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts}, 192 {"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"}, 193 {"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"}, 194 {"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL}, 195 {"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL}, 196 {"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL}, 197 {"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts}, 198 {"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts}, 199 {"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"}, 200 {"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL}, 201 {"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts}, 202 {"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL}, 203 {"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL}, 204 {NULL, 0, 0, NULL} 205 }; 206 207 208 ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum, 209 ctladm_cmdargs *argnum, const char **subopt); 210 static int cctl_dump_ooa(int fd, int argc, char **argv); 211 static int cctl_port(int fd, int argc, char **argv, char *combinedopt); 212 static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func); 213 static int cctl_delay(int fd, int lun, int argc, char **argv, 214 char *combinedopt); 215 static int cctl_lunlist(int fd); 216 static int cctl_sync_cache(int fd, int lun, int iid, int retries, 217 int argc, char **argv, char *combinedopt); 218 static int cctl_start_stop(int fd, int lun, int iid, int retries, 219 int start, int argc, char **argv, char *combinedopt); 220 static int cctl_mode_sense(int fd, int lun, int iid, int retries, 221 int argc, char **argv, char *combinedopt); 222 static int cctl_read_capacity(int fd, int lun, int iid, 223 int retries, int argc, char **argv, 224 char *combinedopt); 225 static int cctl_read_write(int fd, int lun, int iid, int retries, 226 int argc, char **argv, char *combinedopt, 227 ctladm_cmdfunction command); 228 static int cctl_get_luns(int fd, int lun, int iid, int retries, 229 struct scsi_report_luns_data **lun_data, 230 uint32_t *num_luns); 231 static int cctl_report_luns(int fd, int lun, int iid, int retries); 232 static int cctl_tur(int fd, int lun, int iid, int retries); 233 static int cctl_get_inquiry(int fd, int lun, int iid, int retries, 234 char *path_str, int path_len, 235 struct scsi_inquiry_data *inq_data); 236 static int cctl_inquiry(int fd, int lun, int iid, int retries); 237 static int cctl_req_sense(int fd, int lun, int iid, int retries); 238 static int cctl_persistent_reserve_in(int fd, int lun, 239 int initiator, int argc, char **argv, 240 char *combinedopt, int retry_count); 241 static int cctl_persistent_reserve_out(int fd, int lun, 242 int initiator, int argc, char **argv, 243 char *combinedopt, int retry_count); 244 static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt); 245 static int cctl_inquiry_vpd_devid(int fd, int lun, int initiator); 246 static int cctl_report_target_port_group(int fd, int lun, int initiator); 247 static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt); 248 static int cctl_portlist(int fd, int argc, char **argv, char *combinedopt); 249 250 ctladm_optret 251 getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum, 252 ctladm_cmdargs *argnum, const char **subopt) 253 { 254 struct ctladm_opts *opts; 255 int num_matches = 0; 256 257 for (opts = table; (opts != NULL) && (opts->optname != NULL); 258 opts++) { 259 if (strncmp(opts->optname, arg, strlen(arg)) == 0) { 260 *cmdnum = opts->cmdnum; 261 *argnum = opts->argnum; 262 *subopt = opts->subopt; 263 264 if (strcmp(opts->optname, arg) == 0) 265 return (CC_OR_FOUND); 266 267 if (++num_matches > 1) 268 return(CC_OR_AMBIGUOUS); 269 } 270 } 271 272 if (num_matches > 0) 273 return(CC_OR_FOUND); 274 else 275 return(CC_OR_NOT_FOUND); 276 } 277 278 static int 279 cctl_dump_ooa(int fd, int argc, char **argv) 280 { 281 struct ctl_ooa ooa; 282 long double cmd_latency; 283 int num_entries, len, lun = -1, retval = 0; 284 unsigned int i; 285 286 num_entries = 104; 287 288 if ((argc > 2) && (isdigit(argv[2][0]))) 289 lun = strtol(argv[2], NULL, 0); 290 retry: 291 292 len = num_entries * sizeof(struct ctl_ooa_entry); 293 bzero(&ooa, sizeof(ooa)); 294 ooa.entries = malloc(len); 295 if (ooa.entries == NULL) { 296 warn("%s: error mallocing %d bytes", __func__, len); 297 return (1); 298 } 299 if (lun >= 0) { 300 ooa.lun_num = lun; 301 } else 302 ooa.flags |= CTL_OOA_FLAG_ALL_LUNS; 303 ooa.alloc_len = len; 304 ooa.alloc_num = num_entries; 305 if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) { 306 warn("%s: CTL_GET_OOA ioctl failed", __func__); 307 retval = 1; 308 goto bailout; 309 } 310 311 if (ooa.status == CTL_OOA_NEED_MORE_SPACE) { 312 num_entries = num_entries * 2; 313 free(ooa.entries); 314 ooa.entries = NULL; 315 goto retry; 316 } 317 318 if (ooa.status != CTL_OOA_OK) { 319 warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__, 320 ooa.status); 321 retval = 1; 322 goto bailout; 323 } 324 325 fprintf(stdout, "Dumping OOA queues\n"); 326 for (i = 0; i < ooa.fill_num; i++) { 327 struct ctl_ooa_entry *entry; 328 char cdb_str[(SCSI_MAX_CDBLEN * 3) +1]; 329 struct bintime delta_bt; 330 struct timespec ts; 331 332 entry = &ooa.entries[i]; 333 334 delta_bt = ooa.cur_bt; 335 bintime_sub(&delta_bt, &entry->start_bt); 336 bintime2timespec(&delta_bt, &ts); 337 cmd_latency = ts.tv_sec * 1000; 338 if (ts.tv_nsec > 0) 339 cmd_latency += ts.tv_nsec / 1000000; 340 341 fprintf(stdout, "LUN %jd tag 0x%jx%s%s%s%s%s%s%s: %s. CDB: %s " 342 "(%0.0Lf ms)\n", 343 (intmax_t)entry->lun_num, entry->tag_num, 344 (entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ? 345 " BLOCKED" : "", 346 (entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"", 347 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ? 348 " DMAQUEUED" : "", 349 (entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "", 350 (entry->cmd_flags & CTL_OOACMD_FLAG_STATUS_QUEUED) ? 351 " STATUSQUEUED" : "", 352 (entry->cmd_flags & CTL_OOACMD_FLAG_STATUS_SENT) ? " STATUS" : "", 353 (entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ? 354 " ABORT" : "", 355 scsi_op_desc(entry->cdb[0], NULL), 356 scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)), 357 cmd_latency); 358 } 359 fprintf(stdout, "OOA queues dump done\n"); 360 361 bailout: 362 free(ooa.entries); 363 return (retval); 364 } 365 366 static int 367 cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused) 368 { 369 if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) { 370 warn(__func__); 371 return (1); 372 } 373 return (0); 374 } 375 376 typedef enum { 377 CCTL_PORT_MODE_NONE, 378 CCTL_PORT_MODE_LIST, 379 CCTL_PORT_MODE_SET, 380 CCTL_PORT_MODE_ON, 381 CCTL_PORT_MODE_OFF, 382 CCTL_PORT_MODE_CREATE, 383 CCTL_PORT_MODE_REMOVE 384 } cctl_port_mode; 385 386 static struct ctladm_opts cctl_fe_table[] = { 387 {"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL}, 388 {"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL}, 389 {"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL}, 390 {"iscsi", CTL_PORT_ISCSI, CTLADM_ARG_NONE, NULL}, 391 {"nvmf", CTL_PORT_NVMF, CTLADM_ARG_NONE, NULL}, 392 {"sas", CTL_PORT_SAS, CTLADM_ARG_NONE, NULL}, 393 {"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL}, 394 {NULL, 0, 0, NULL} 395 }; 396 397 static int 398 cctl_port(int fd, int argc, char **argv, char *combinedopt) 399 { 400 char result_buf[1024]; 401 int c; 402 uint64_t created_port = -1; 403 int32_t targ_port = -1; 404 int retval = 0; 405 int wwnn_set = 0, wwpn_set = 0; 406 uint64_t wwnn = 0, wwpn = 0; 407 cctl_port_mode port_mode = CCTL_PORT_MODE_NONE; 408 struct ctl_port_entry entry; 409 struct ctl_req req; 410 char *driver = NULL; 411 nvlist_t *option_list; 412 ctl_port_type port_type = CTL_PORT_NONE; 413 int quiet = 0, xml = 0; 414 415 option_list = nvlist_create(0); 416 if (option_list == NULL) 417 err(1, "%s: unable to allocate nvlist", __func__); 418 419 while ((c = getopt(argc, argv, combinedopt)) != -1) { 420 switch (c) { 421 case 'l': 422 if (port_mode != CCTL_PORT_MODE_NONE) 423 goto bailout_badarg; 424 425 port_mode = CCTL_PORT_MODE_LIST; 426 break; 427 case 'c': 428 port_mode = CCTL_PORT_MODE_CREATE; 429 break; 430 case 'r': 431 port_mode = CCTL_PORT_MODE_REMOVE; 432 break; 433 case 'o': 434 if (port_mode != CCTL_PORT_MODE_NONE) 435 goto bailout_badarg; 436 437 if (strcasecmp(optarg, "on") == 0) 438 port_mode = CCTL_PORT_MODE_ON; 439 else if (strcasecmp(optarg, "off") == 0) 440 port_mode = CCTL_PORT_MODE_OFF; 441 else { 442 warnx("Invalid -o argument %s, \"on\" or " 443 "\"off\" are the only valid args", 444 optarg); 445 retval = 1; 446 goto bailout; 447 } 448 break; 449 case 'O': { 450 char *tmpstr; 451 char *name, *value; 452 453 tmpstr = strdup(optarg); 454 name = strsep(&tmpstr, "="); 455 if (name == NULL) { 456 warnx("%s: option -O takes \"name=value\"" 457 "argument", __func__); 458 retval = 1; 459 goto bailout; 460 } 461 value = strsep(&tmpstr, "="); 462 if (value == NULL) { 463 warnx("%s: option -O takes \"name=value\"" 464 "argument", __func__); 465 retval = 1; 466 goto bailout; 467 } 468 469 free(tmpstr); 470 nvlist_add_string(option_list, name, value); 471 break; 472 } 473 case 'd': 474 if (driver != NULL) { 475 warnx("%s: option -d cannot be specified twice", 476 __func__); 477 retval = 1; 478 goto bailout; 479 } 480 481 driver = strdup(optarg); 482 break; 483 case 'p': 484 targ_port = strtol(optarg, NULL, 0); 485 break; 486 case 'q': 487 quiet = 1; 488 break; 489 case 't': { 490 ctladm_optret optret; 491 ctladm_cmdargs argnum; 492 const char *subopt; 493 ctl_port_type tmp_port_type; 494 495 optret = getoption(cctl_fe_table, optarg, &tmp_port_type, 496 &argnum, &subopt); 497 if (optret == CC_OR_AMBIGUOUS) { 498 warnx("%s: ambiguous frontend type %s", 499 __func__, optarg); 500 retval = 1; 501 goto bailout; 502 } else if (optret == CC_OR_NOT_FOUND) { 503 warnx("%s: invalid frontend type %s", 504 __func__, optarg); 505 retval = 1; 506 goto bailout; 507 } 508 509 port_type |= tmp_port_type; 510 break; 511 } 512 case 'w': 513 if ((port_mode != CCTL_PORT_MODE_NONE) 514 && (port_mode != CCTL_PORT_MODE_SET)) 515 goto bailout_badarg; 516 517 port_mode = CCTL_PORT_MODE_SET; 518 519 wwnn = strtoull(optarg, NULL, 0); 520 wwnn_set = 1; 521 break; 522 case 'W': 523 if ((port_mode != CCTL_PORT_MODE_NONE) 524 && (port_mode != CCTL_PORT_MODE_SET)) 525 goto bailout_badarg; 526 527 port_mode = CCTL_PORT_MODE_SET; 528 529 wwpn = strtoull(optarg, NULL, 0); 530 wwpn_set = 1; 531 break; 532 case 'x': 533 xml = 1; 534 break; 535 } 536 } 537 538 if (driver == NULL) 539 driver = strdup("ioctl"); 540 541 /* 542 * The user can specify either one or more frontend types (-t), or 543 * a specific frontend, but not both. 544 * 545 * If the user didn't specify a frontend type or number, set it to 546 * all. This is primarily needed for the enable/disable ioctls. 547 * This will be a no-op for the listing code. For the set ioctl, 548 * we'll throw an error, since that only works on one port at a time. 549 */ 550 if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) { 551 warnx("%s: can only specify one of -t or -p", __func__); 552 retval = 1; 553 goto bailout; 554 } else if ((targ_port == -1) && (port_type == CTL_PORT_NONE)) 555 port_type = CTL_PORT_ALL; 556 557 bzero(&entry, sizeof(entry)); 558 559 /* 560 * These are needed for all but list/dump mode. 561 */ 562 entry.port_type = port_type; 563 entry.targ_port = targ_port; 564 565 switch (port_mode) { 566 case CCTL_PORT_MODE_LIST: { 567 char opts[] = "xq"; 568 char argx[] = "-x"; 569 char argq[] = "-q"; 570 char *argvx[2]; 571 int argcx = 0; 572 573 optind = 0; 574 optreset = 1; 575 if (xml) 576 argvx[argcx++] = argx; 577 if (quiet) 578 argvx[argcx++] = argq; 579 cctl_portlist(fd, argcx, argvx, opts); 580 break; 581 } 582 case CCTL_PORT_MODE_REMOVE: 583 /* FALLTHROUGH */ 584 case CCTL_PORT_MODE_CREATE: { 585 bzero(&req, sizeof(req)); 586 strlcpy(req.driver, driver, sizeof(req.driver)); 587 req.result = result_buf; 588 req.result_len = sizeof(result_buf); 589 590 if (port_mode == CCTL_PORT_MODE_REMOVE) { 591 req.reqtype = CTL_REQ_REMOVE; 592 if (targ_port != -1) 593 nvlist_add_stringf(option_list, "port_id", "%d", 594 targ_port); 595 } else 596 req.reqtype = CTL_REQ_CREATE; 597 598 req.args = nvlist_pack(option_list, &req.args_len); 599 if (req.args == NULL) { 600 warn("%s: error packing nvlist", __func__); 601 retval = 1; 602 goto bailout; 603 } 604 605 retval = ioctl(fd, CTL_PORT_REQ, &req); 606 free(req.args); 607 if (retval == -1) { 608 warn("%s: CTL_PORT_REQ ioctl failed", __func__); 609 retval = 1; 610 goto bailout; 611 } 612 613 switch (req.status) { 614 case CTL_LUN_ERROR: 615 warnx("error: %s", req.error_str); 616 retval = 1; 617 goto bailout; 618 case CTL_LUN_WARNING: 619 warnx("warning: %s", req.error_str); 620 break; 621 case CTL_LUN_OK: 622 if (port_mode == CCTL_PORT_MODE_CREATE) { 623 req.result_nvl = nvlist_unpack(result_buf, req.result_len, 0); 624 if (req.result_nvl == NULL) { 625 warnx("error unpacking result nvlist"); 626 break; 627 } 628 created_port = nvlist_get_number(req.result_nvl, "port_id"); 629 printf("Port created successfully\n" 630 "frontend: %s\n" 631 "port: %ju\n", driver, 632 (uintmax_t) created_port); 633 nvlist_destroy(req.result_nvl); 634 } else 635 printf("Port destroyed successfully\n"); 636 break; 637 default: 638 warnx("unknown status: %d", req.status); 639 retval = 1; 640 goto bailout; 641 } 642 643 break; 644 } 645 case CCTL_PORT_MODE_SET: 646 if (targ_port == -1) { 647 warnx("%s: -w and -W require -p", __func__); 648 retval = 1; 649 goto bailout; 650 } 651 652 if (wwnn_set) { 653 entry.flags |= CTL_PORT_WWNN_VALID; 654 entry.wwnn = wwnn; 655 } 656 if (wwpn_set) { 657 entry.flags |= CTL_PORT_WWPN_VALID; 658 entry.wwpn = wwpn; 659 } 660 661 if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) { 662 warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__); 663 retval = 1; 664 goto bailout; 665 } 666 break; 667 case CCTL_PORT_MODE_ON: 668 if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) { 669 warn("%s: CTL_ENABLE_PORT ioctl failed", __func__); 670 retval = 1; 671 goto bailout; 672 } 673 fprintf(stdout, "Front End Ports enabled\n"); 674 break; 675 case CCTL_PORT_MODE_OFF: 676 if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) { 677 warn("%s: CTL_DISABLE_PORT ioctl failed", __func__); 678 retval = 1; 679 goto bailout; 680 } 681 fprintf(stdout, "Front End Ports disabled\n"); 682 break; 683 default: 684 warnx("%s: one of -l, -o or -w/-W must be specified", __func__); 685 retval = 1; 686 goto bailout; 687 break; 688 } 689 690 bailout: 691 nvlist_destroy(option_list); 692 free(driver); 693 return (retval); 694 695 bailout_badarg: 696 warnx("%s: only one of -c, -r, -l, -o or -w/-W may be specified", 697 __func__); 698 return (1); 699 } 700 701 static int 702 cctl_do_io(int fd, int retries, union ctl_io *io, const char *func) 703 { 704 do { 705 if (ioctl(fd, CTL_IO, io) == -1) { 706 warn("%s: error sending CTL_IO ioctl", func); 707 return (-1); 708 } 709 } while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 710 && (retries-- > 0)); 711 712 return (0); 713 } 714 715 static int 716 cctl_delay(int fd, int lun, int argc, char **argv, 717 char *combinedopt) 718 { 719 struct ctl_io_delay_info delay_info; 720 char *delayloc = NULL; 721 char *delaytype = NULL; 722 int delaytime = -1; 723 int retval; 724 int c; 725 726 retval = 0; 727 728 memset(&delay_info, 0, sizeof(delay_info)); 729 730 while ((c = getopt(argc, argv, combinedopt)) != -1) { 731 switch (c) { 732 case 'T': 733 delaytype = strdup(optarg); 734 break; 735 case 'l': 736 delayloc = strdup(optarg); 737 break; 738 case 't': 739 delaytime = strtoul(optarg, NULL, 0); 740 break; 741 } 742 } 743 744 if (delaytime == -1) { 745 warnx("%s: you must specify the delaytime with -t", __func__); 746 retval = 1; 747 goto bailout; 748 } 749 750 if (strcasecmp(delayloc, "datamove") == 0) 751 delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE; 752 else if (strcasecmp(delayloc, "done") == 0) 753 delay_info.delay_loc = CTL_DELAY_LOC_DONE; 754 else { 755 warnx("%s: invalid delay location %s", __func__, delayloc); 756 retval = 1; 757 goto bailout; 758 } 759 760 if ((delaytype == NULL) 761 || (strcmp(delaytype, "oneshot") == 0)) 762 delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT; 763 else if (strcmp(delaytype, "cont") == 0) 764 delay_info.delay_type = CTL_DELAY_TYPE_CONT; 765 else { 766 warnx("%s: invalid delay type %s", __func__, delaytype); 767 retval = 1; 768 goto bailout; 769 } 770 771 delay_info.lun_id = lun; 772 delay_info.delay_secs = delaytime; 773 774 if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) { 775 warn("%s: CTL_DELAY_IO ioctl failed", __func__); 776 retval = 1; 777 goto bailout; 778 } 779 switch (delay_info.status) { 780 case CTL_DELAY_STATUS_NONE: 781 warnx("%s: no delay status??", __func__); 782 retval = 1; 783 break; 784 case CTL_DELAY_STATUS_OK: 785 break; 786 case CTL_DELAY_STATUS_INVALID_LUN: 787 warnx("%s: invalid lun %d", __func__, lun); 788 retval = 1; 789 break; 790 case CTL_DELAY_STATUS_INVALID_TYPE: 791 warnx("%s: invalid delay type %d", __func__, 792 delay_info.delay_type); 793 retval = 1; 794 break; 795 case CTL_DELAY_STATUS_INVALID_LOC: 796 warnx("%s: delay location %s not implemented?", __func__, 797 delayloc); 798 retval = 1; 799 break; 800 case CTL_DELAY_STATUS_NOT_IMPLEMENTED: 801 warnx("%s: delay not implemented in the kernel", __func__); 802 warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__); 803 retval = 1; 804 break; 805 default: 806 warnx("%s: unknown delay return status %d", __func__, 807 delay_info.status); 808 retval = 1; 809 break; 810 } 811 812 bailout: 813 free(delayloc); 814 free(delaytype); 815 return (retval); 816 } 817 818 static struct ctladm_opts cctl_err_types[] = { 819 {"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL}, 820 {"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL}, 821 {"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL}, 822 {"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL}, 823 {NULL, 0, 0, NULL} 824 825 }; 826 827 static struct ctladm_opts cctl_err_patterns[] = { 828 {"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL}, 829 {"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL}, 830 {"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL}, 831 {"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL}, 832 {"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL}, 833 {"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL}, 834 {"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL}, 835 #if 0 836 {"cmd", CTL_LUN_PAT_CMD, CTLADM_ARG_NONE, NULL}, 837 #endif 838 {NULL, 0, 0, NULL} 839 }; 840 841 static int 842 cctl_error_inject(int fd, uint32_t lun, int argc, char **argv, 843 char *combinedopt) 844 { 845 int retval = 0; 846 struct ctl_error_desc err_desc; 847 uint64_t lba = 0; 848 uint32_t len = 0; 849 uint64_t delete_id = 0; 850 int delete_id_set = 0; 851 int continuous = 0; 852 int sense_len = 0; 853 int fd_sense = 0; 854 int c; 855 856 bzero(&err_desc, sizeof(err_desc)); 857 err_desc.lun_id = lun; 858 859 while ((c = getopt(argc, argv, combinedopt)) != -1) { 860 switch (c) { 861 case 'c': 862 continuous = 1; 863 break; 864 case 'd': 865 delete_id = strtoull(optarg, NULL, 0); 866 delete_id_set = 1; 867 break; 868 case 'i': 869 case 'p': { 870 ctladm_optret optret; 871 ctladm_cmdargs argnum; 872 const char *subopt; 873 874 if (c == 'i') { 875 ctl_lun_error err_type; 876 877 if (err_desc.lun_error != CTL_LUN_INJ_NONE) { 878 warnx("%s: can't specify multiple -i " 879 "arguments", __func__); 880 retval = 1; 881 goto bailout; 882 } 883 optret = getoption(cctl_err_types, optarg, 884 &err_type, &argnum, &subopt); 885 err_desc.lun_error = err_type; 886 } else { 887 ctl_lun_error_pattern pattern; 888 889 optret = getoption(cctl_err_patterns, optarg, 890 &pattern, &argnum, &subopt); 891 err_desc.error_pattern |= pattern; 892 } 893 894 if (optret == CC_OR_AMBIGUOUS) { 895 warnx("%s: ambiguous argument %s", __func__, 896 optarg); 897 retval = 1; 898 goto bailout; 899 } else if (optret == CC_OR_NOT_FOUND) { 900 warnx("%s: argument %s not found", __func__, 901 optarg); 902 retval = 1; 903 goto bailout; 904 } 905 break; 906 } 907 case 'r': { 908 char *tmpstr, *tmpstr2; 909 910 tmpstr = strdup(optarg); 911 if (tmpstr == NULL) { 912 warn("%s: error duplicating string %s", 913 __func__, optarg); 914 retval = 1; 915 goto bailout; 916 } 917 918 tmpstr2 = strsep(&tmpstr, ","); 919 if (tmpstr2 == NULL) { 920 warnx("%s: invalid -r argument %s", __func__, 921 optarg); 922 retval = 1; 923 free(tmpstr); 924 goto bailout; 925 } 926 lba = strtoull(tmpstr2, NULL, 0); 927 tmpstr2 = strsep(&tmpstr, ","); 928 if (tmpstr2 == NULL) { 929 warnx("%s: no len argument for -r lba,len, got" 930 " %s", __func__, optarg); 931 retval = 1; 932 free(tmpstr); 933 goto bailout; 934 } 935 len = strtoul(tmpstr2, NULL, 0); 936 free(tmpstr); 937 break; 938 } 939 case 's': { 940 struct get_hook hook; 941 char *sensestr; 942 943 sense_len = strtol(optarg, NULL, 0); 944 if (sense_len <= 0) { 945 warnx("invalid number of sense bytes %d", 946 sense_len); 947 retval = 1; 948 goto bailout; 949 } 950 951 sense_len = MIN(sense_len, SSD_FULL_SIZE); 952 953 hook.argc = argc - optind; 954 hook.argv = argv + optind; 955 hook.got = 0; 956 957 sensestr = cget(&hook, NULL); 958 if ((sensestr != NULL) 959 && (sensestr[0] == '-')) { 960 fd_sense = 1; 961 } else { 962 buff_encode_visit( 963 (uint8_t *)&err_desc.custom_sense, 964 sense_len, sensestr, iget, &hook); 965 } 966 optind += hook.got; 967 break; 968 } 969 default: 970 break; 971 } 972 } 973 974 if (delete_id_set != 0) { 975 err_desc.serial = delete_id; 976 if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) { 977 warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl", 978 __func__); 979 retval = 1; 980 } 981 goto bailout; 982 } 983 984 if (err_desc.lun_error == CTL_LUN_INJ_NONE) { 985 warnx("%s: error injection command (-i) needed", 986 __func__); 987 retval = 1; 988 goto bailout; 989 } else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM) 990 && (sense_len == 0)) { 991 warnx("%s: custom error requires -s", __func__); 992 retval = 1; 993 goto bailout; 994 } 995 996 if (continuous != 0) 997 err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS; 998 999 /* 1000 * If fd_sense is set, we need to read the sense data the user 1001 * wants returned from stdin. 1002 */ 1003 if (fd_sense == 1) { 1004 ssize_t amt_read; 1005 int amt_to_read = sense_len; 1006 u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense; 1007 1008 for (amt_read = 0; amt_to_read > 0; 1009 amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) { 1010 if (amt_read == -1) { 1011 warn("error reading sense data from stdin"); 1012 retval = 1; 1013 goto bailout; 1014 } 1015 amt_to_read -= amt_read; 1016 buf_ptr += amt_read; 1017 } 1018 } 1019 1020 if (err_desc.error_pattern == CTL_LUN_PAT_NONE) { 1021 warnx("%s: command pattern (-p) needed", __func__); 1022 retval = 1; 1023 goto bailout; 1024 } 1025 1026 if (len != 0) { 1027 err_desc.error_pattern |= CTL_LUN_PAT_RANGE; 1028 /* 1029 * We could check here to see whether it's a read/write 1030 * command, but that will be pointless once we allow 1031 * custom patterns. At that point, the user could specify 1032 * a READ(6) CDB type, and we wouldn't have an easy way here 1033 * to verify whether range checking is possible there. The 1034 * user will just figure it out when his error never gets 1035 * executed. 1036 */ 1037 #if 0 1038 if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) { 1039 warnx("%s: need read and/or write pattern if range " 1040 "is specified", __func__); 1041 retval = 1; 1042 goto bailout; 1043 } 1044 #endif 1045 err_desc.lba_range.lba = lba; 1046 err_desc.lba_range.len = len; 1047 } 1048 1049 if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) { 1050 warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__); 1051 retval = 1; 1052 } else { 1053 printf("Error injection succeeded, serial number is %ju\n", 1054 (uintmax_t)err_desc.serial); 1055 } 1056 bailout: 1057 1058 return (retval); 1059 } 1060 1061 static int 1062 cctl_lunlist(int fd) 1063 { 1064 struct scsi_report_luns_data *lun_data; 1065 struct scsi_inquiry_data *inq_data; 1066 uint32_t num_luns; 1067 int initid; 1068 unsigned int i; 1069 int retval; 1070 1071 inq_data = NULL; 1072 initid = 7; 1073 1074 /* 1075 * XXX KDM assuming LUN 0 is fine, but we may need to change this 1076 * if we ever acquire the ability to have multiple targets. 1077 */ 1078 if ((retval = cctl_get_luns(fd, /*lun*/ 0, initid, 1079 /*retries*/ 2, &lun_data, &num_luns)) != 0) 1080 goto bailout; 1081 1082 inq_data = malloc(sizeof(*inq_data)); 1083 if (inq_data == NULL) { 1084 warn("%s: couldn't allocate memory for inquiry data\n", 1085 __func__); 1086 retval = 1; 1087 goto bailout; 1088 } 1089 for (i = 0; i < num_luns; i++) { 1090 char scsi_path[40]; 1091 int lun_val; 1092 1093 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) { 1094 case RPL_LUNDATA_ATYP_PERIPH: 1095 lun_val = lun_data->luns[i].lundata[1]; 1096 break; 1097 case RPL_LUNDATA_ATYP_FLAT: 1098 lun_val = (lun_data->luns[i].lundata[0] & 1099 RPL_LUNDATA_FLAT_LUN_MASK) | 1100 (lun_data->luns[i].lundata[1] << 1101 RPL_LUNDATA_FLAT_LUN_BITS); 1102 break; 1103 case RPL_LUNDATA_ATYP_LUN: 1104 case RPL_LUNDATA_ATYP_EXTLUN: 1105 default: 1106 fprintf(stdout, "Unsupported LUN format %d\n", 1107 lun_data->luns[i].lundata[0] & 1108 RPL_LUNDATA_ATYP_MASK); 1109 lun_val = -1; 1110 break; 1111 } 1112 if (lun_val == -1) 1113 continue; 1114 1115 if ((retval = cctl_get_inquiry(fd, lun_val, initid, 1116 /*retries*/ 2, scsi_path, 1117 sizeof(scsi_path), 1118 inq_data)) != 0) { 1119 goto bailout; 1120 } 1121 printf("%s", scsi_path); 1122 scsi_print_inquiry(inq_data); 1123 } 1124 bailout: 1125 1126 if (lun_data != NULL) 1127 free(lun_data); 1128 1129 if (inq_data != NULL) 1130 free(inq_data); 1131 1132 return (retval); 1133 } 1134 1135 static int 1136 cctl_sync_cache(int fd, int lun, int iid, int retries, 1137 int argc, char **argv, char *combinedopt) 1138 { 1139 union ctl_io *io; 1140 int cdb_size = -1; 1141 int retval; 1142 uint64_t our_lba = 0; 1143 uint32_t our_block_count = 0; 1144 int reladr = 0, immed = 0; 1145 int c; 1146 1147 retval = 0; 1148 1149 io = ctl_scsi_alloc_io(iid); 1150 if (io == NULL) { 1151 warnx("%s: can't allocate memory", __func__); 1152 return (1); 1153 } 1154 1155 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1156 switch (c) { 1157 case 'b': 1158 our_block_count = strtoul(optarg, NULL, 0); 1159 break; 1160 case 'c': 1161 cdb_size = strtol(optarg, NULL, 0); 1162 break; 1163 case 'i': 1164 immed = 1; 1165 break; 1166 case 'l': 1167 our_lba = strtoull(optarg, NULL, 0); 1168 break; 1169 case 'r': 1170 reladr = 1; 1171 break; 1172 default: 1173 break; 1174 } 1175 } 1176 1177 if (cdb_size != -1) { 1178 switch (cdb_size) { 1179 case 10: 1180 case 16: 1181 break; 1182 default: 1183 warnx("%s: invalid cdbsize %d, valid sizes are 10 " 1184 "and 16", __func__, cdb_size); 1185 retval = 1; 1186 goto bailout; 1187 break; /* NOTREACHED */ 1188 } 1189 } else 1190 cdb_size = 10; 1191 1192 ctl_scsi_sync_cache(/*io*/ io, 1193 /*immed*/ immed, 1194 /*reladr*/ reladr, 1195 /*minimum_cdb_size*/ cdb_size, 1196 /*starting_lba*/ our_lba, 1197 /*block_count*/ our_block_count, 1198 /*tag_type*/ CTL_TAG_SIMPLE, 1199 /*control*/ 0); 1200 1201 io->io_hdr.nexus.targ_lun = lun; 1202 io->io_hdr.nexus.initid = iid; 1203 1204 if (cctl_do_io(fd, retries, io, __func__) != 0) { 1205 retval = 1; 1206 goto bailout; 1207 } 1208 1209 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 1210 fprintf(stdout, "Cache synchronized successfully\n"); 1211 } else 1212 ctl_io_error_print(io, NULL, stderr); 1213 bailout: 1214 ctl_scsi_free_io(io); 1215 1216 return (retval); 1217 } 1218 1219 static int 1220 cctl_start_stop(int fd, int lun, int iid, int retries, int start, 1221 int argc, char **argv, char *combinedopt) 1222 { 1223 union ctl_io *io; 1224 char scsi_path[40]; 1225 int immed = 0; 1226 int retval, c; 1227 1228 retval = 0; 1229 1230 io = ctl_scsi_alloc_io(iid); 1231 if (io == NULL) { 1232 warnx("%s: can't allocate memory", __func__); 1233 return (1); 1234 } 1235 1236 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1237 switch (c) { 1238 case 'i': 1239 immed = 1; 1240 break; 1241 default: 1242 break; 1243 } 1244 } 1245 /* 1246 * Use an ordered tag for the stop command, to guarantee that any 1247 * pending I/O will finish before the stop command executes. This 1248 * would normally be the case anyway, since CTL will basically 1249 * treat the start/stop command as an ordered command with respect 1250 * to any other command except an INQUIRY. (See ctl_ser_table.c.) 1251 */ 1252 ctl_scsi_start_stop(/*io*/ io, 1253 /*start*/ start, 1254 /*load_eject*/ 0, 1255 /*immediate*/ immed, 1256 /*power_conditions*/ SSS_PC_START_VALID, 1257 /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE : 1258 CTL_TAG_ORDERED, 1259 /*control*/ 0); 1260 1261 io->io_hdr.nexus.targ_lun = lun; 1262 io->io_hdr.nexus.initid = iid; 1263 1264 if (cctl_do_io(fd, retries, io, __func__) != 0) { 1265 retval = 1; 1266 goto bailout; 1267 } 1268 1269 ctl_scsi_path_string(&io->io_hdr, scsi_path, sizeof(scsi_path)); 1270 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 1271 fprintf(stdout, "%s LUN %s successfully\n", scsi_path, 1272 (start) ? "started" : "stopped"); 1273 } else 1274 ctl_io_error_print(io, NULL, stderr); 1275 1276 bailout: 1277 ctl_scsi_free_io(io); 1278 1279 return (retval); 1280 } 1281 1282 static int 1283 cctl_mode_sense(int fd, int lun, int iid, int retries, 1284 int argc, char **argv, char *combinedopt) 1285 { 1286 union ctl_io *io; 1287 uint32_t datalen; 1288 uint8_t *dataptr; 1289 int pc = -1, cdbsize, retval, dbd = 0, subpage = -1; 1290 int list = 0; 1291 int page_code = -1; 1292 int c; 1293 1294 cdbsize = 0; 1295 retval = 0; 1296 dataptr = NULL; 1297 1298 io = ctl_scsi_alloc_io(iid); 1299 if (io == NULL) { 1300 warn("%s: can't allocate memory", __func__); 1301 return (1); 1302 } 1303 1304 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1305 switch (c) { 1306 case 'P': 1307 pc = strtoul(optarg, NULL, 0); 1308 break; 1309 case 'S': 1310 subpage = strtoul(optarg, NULL, 0); 1311 break; 1312 case 'd': 1313 dbd = 1; 1314 break; 1315 case 'l': 1316 list = 1; 1317 break; 1318 case 'm': 1319 page_code = strtoul(optarg, NULL, 0); 1320 break; 1321 case 'c': 1322 cdbsize = strtol(optarg, NULL, 0); 1323 break; 1324 default: 1325 break; 1326 } 1327 } 1328 1329 if (((list == 0) && (page_code == -1)) 1330 || ((list != 0) && (page_code != -1))) { 1331 warnx("%s: you must specify either a page code (-m) or -l", 1332 __func__); 1333 retval = 1; 1334 goto bailout; 1335 } 1336 1337 if ((page_code != -1) 1338 && ((page_code > SMS_ALL_PAGES_PAGE) 1339 || (page_code < 0))) { 1340 warnx("%s: page code %d is out of range", __func__, 1341 page_code); 1342 retval = 1; 1343 goto bailout; 1344 } 1345 1346 if (list == 1) { 1347 page_code = SMS_ALL_PAGES_PAGE; 1348 if (pc != -1) { 1349 warnx("%s: arg -P makes no sense with -l", 1350 __func__); 1351 retval = 1; 1352 goto bailout; 1353 } 1354 if (subpage != -1) { 1355 warnx("%s: arg -S makes no sense with -l", __func__); 1356 retval = 1; 1357 goto bailout; 1358 } 1359 } 1360 1361 if (pc == -1) 1362 pc = SMS_PAGE_CTRL_CURRENT; 1363 else { 1364 if ((pc > 3) 1365 || (pc < 0)) { 1366 warnx("%s: page control value %d is out of range: 0-3", 1367 __func__, pc); 1368 retval = 1; 1369 goto bailout; 1370 } 1371 } 1372 1373 1374 if ((subpage != -1) 1375 && ((subpage > 255) 1376 || (subpage < 0))) { 1377 warnx("%s: subpage code %d is out of range: 0-255", __func__, 1378 subpage); 1379 retval = 1; 1380 goto bailout; 1381 } 1382 if (cdbsize != 0) { 1383 switch (cdbsize) { 1384 case 6: 1385 case 10: 1386 break; 1387 default: 1388 warnx("%s: invalid cdbsize %d, valid sizes are 6 " 1389 "and 10", __func__, cdbsize); 1390 retval = 1; 1391 goto bailout; 1392 break; 1393 } 1394 } else 1395 cdbsize = 6; 1396 1397 if (subpage == -1) 1398 subpage = 0; 1399 1400 if (cdbsize == 6) 1401 datalen = 255; 1402 else 1403 datalen = 65535; 1404 1405 dataptr = (uint8_t *)malloc(datalen); 1406 if (dataptr == NULL) { 1407 warn("%s: can't allocate %d bytes", __func__, datalen); 1408 retval = 1; 1409 goto bailout; 1410 } 1411 1412 memset(dataptr, 0, datalen); 1413 1414 ctl_scsi_mode_sense(io, 1415 /*data_ptr*/ dataptr, 1416 /*data_len*/ datalen, 1417 /*dbd*/ dbd, 1418 /*llbaa*/ 0, 1419 /*page_code*/ page_code, 1420 /*pc*/ pc << 6, 1421 /*subpage*/ subpage, 1422 /*minimum_cdb_size*/ cdbsize, 1423 /*tag_type*/ CTL_TAG_SIMPLE, 1424 /*control*/ 0); 1425 1426 io->io_hdr.nexus.targ_lun = lun; 1427 io->io_hdr.nexus.initid = iid; 1428 1429 if (cctl_do_io(fd, retries, io, __func__) != 0) { 1430 retval = 1; 1431 goto bailout; 1432 } 1433 1434 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 1435 int pages_len, used_len; 1436 uint32_t returned_len; 1437 uint8_t *ndataptr; 1438 1439 if (io->scsiio.cdb[0] == MODE_SENSE_6) { 1440 struct scsi_mode_hdr_6 *hdr6; 1441 int bdlen; 1442 1443 hdr6 = (struct scsi_mode_hdr_6 *)dataptr; 1444 1445 returned_len = hdr6->datalen + 1; 1446 bdlen = hdr6->block_descr_len; 1447 1448 ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen); 1449 } else { 1450 struct scsi_mode_hdr_10 *hdr10; 1451 int bdlen; 1452 1453 hdr10 = (struct scsi_mode_hdr_10 *)dataptr; 1454 1455 returned_len = scsi_2btoul(hdr10->datalen) + 2; 1456 bdlen = scsi_2btoul(hdr10->block_descr_len); 1457 1458 ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen); 1459 } 1460 /* just in case they can give us more than we allocated for */ 1461 returned_len = min(returned_len, datalen); 1462 pages_len = returned_len - (ndataptr - dataptr); 1463 #if 0 1464 fprintf(stdout, "returned_len = %d, pages_len = %d\n", 1465 returned_len, pages_len); 1466 #endif 1467 if (list == 1) { 1468 fprintf(stdout, "Supported mode pages:\n"); 1469 for (used_len = 0; used_len < pages_len;) { 1470 struct scsi_mode_page_header *header; 1471 1472 header = (struct scsi_mode_page_header *) 1473 &ndataptr[used_len]; 1474 fprintf(stdout, "%d\n", header->page_code); 1475 used_len += header->page_length + 2; 1476 } 1477 } else { 1478 for (used_len = 0; used_len < pages_len; used_len++) { 1479 fprintf(stdout, "0x%x ", ndataptr[used_len]); 1480 if (((used_len+1) % 16) == 0) 1481 fprintf(stdout, "\n"); 1482 } 1483 fprintf(stdout, "\n"); 1484 } 1485 } else 1486 ctl_io_error_print(io, NULL, stderr); 1487 bailout: 1488 1489 ctl_scsi_free_io(io); 1490 1491 if (dataptr != NULL) 1492 free(dataptr); 1493 1494 return (retval); 1495 } 1496 1497 static int 1498 cctl_read_capacity(int fd, int lun, int iid, int retries, 1499 int argc, char **argv, char *combinedopt) 1500 { 1501 union ctl_io *io; 1502 struct scsi_read_capacity_data *data; 1503 struct scsi_read_capacity_data_long *longdata; 1504 int cdbsize = -1, retval; 1505 uint8_t *dataptr; 1506 int c; 1507 1508 cdbsize = 10; 1509 dataptr = NULL; 1510 retval = 0; 1511 1512 io = ctl_scsi_alloc_io(iid); 1513 if (io == NULL) { 1514 warn("%s: can't allocate memory\n", __func__); 1515 return (1); 1516 } 1517 1518 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1519 switch (c) { 1520 case 'c': 1521 cdbsize = strtol(optarg, NULL, 0); 1522 break; 1523 default: 1524 break; 1525 } 1526 } 1527 if (cdbsize != -1) { 1528 switch (cdbsize) { 1529 case 10: 1530 case 16: 1531 break; 1532 default: 1533 warnx("%s: invalid cdbsize %d, valid sizes are 10 " 1534 "and 16", __func__, cdbsize); 1535 retval = 1; 1536 goto bailout; 1537 break; /* NOTREACHED */ 1538 } 1539 } else 1540 cdbsize = 10; 1541 1542 dataptr = (uint8_t *)malloc(sizeof(*longdata)); 1543 if (dataptr == NULL) { 1544 warn("%s: can't allocate %zd bytes\n", __func__, 1545 sizeof(*longdata)); 1546 retval = 1; 1547 goto bailout; 1548 } 1549 memset(dataptr, 0, sizeof(*longdata)); 1550 1551 retry: 1552 1553 switch (cdbsize) { 1554 case 10: 1555 ctl_scsi_read_capacity(io, 1556 /*data_ptr*/ dataptr, 1557 /*data_len*/ sizeof(*longdata), 1558 /*addr*/ 0, 1559 /*reladr*/ 0, 1560 /*pmi*/ 0, 1561 /*tag_type*/ CTL_TAG_SIMPLE, 1562 /*control*/ 0); 1563 break; 1564 case 16: 1565 ctl_scsi_read_capacity_16(io, 1566 /*data_ptr*/ dataptr, 1567 /*data_len*/ sizeof(*longdata), 1568 /*addr*/ 0, 1569 /*reladr*/ 0, 1570 /*pmi*/ 0, 1571 /*tag_type*/ CTL_TAG_SIMPLE, 1572 /*control*/ 0); 1573 break; 1574 } 1575 1576 io->io_hdr.nexus.initid = iid; 1577 io->io_hdr.nexus.targ_lun = lun; 1578 1579 if (cctl_do_io(fd, retries, io, __func__) != 0) { 1580 retval = 1; 1581 goto bailout; 1582 } 1583 1584 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 1585 uint64_t maxlba; 1586 uint32_t blocksize; 1587 1588 if (cdbsize == 10) { 1589 1590 data = (struct scsi_read_capacity_data *)dataptr; 1591 1592 maxlba = scsi_4btoul(data->addr); 1593 blocksize = scsi_4btoul(data->length); 1594 1595 if (maxlba == 0xffffffff) { 1596 cdbsize = 16; 1597 goto retry; 1598 } 1599 } else { 1600 longdata=(struct scsi_read_capacity_data_long *)dataptr; 1601 1602 maxlba = scsi_8btou64(longdata->addr); 1603 blocksize = scsi_4btoul(longdata->length); 1604 } 1605 1606 fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n", 1607 (uintmax_t)maxlba, blocksize); 1608 } else { 1609 ctl_io_error_print(io, NULL, stderr); 1610 } 1611 bailout: 1612 ctl_scsi_free_io(io); 1613 1614 if (dataptr != NULL) 1615 free(dataptr); 1616 1617 return (retval); 1618 } 1619 1620 static int 1621 cctl_read_write(int fd, int lun, int iid, int retries, 1622 int argc, char **argv, char *combinedopt, 1623 ctladm_cmdfunction command) 1624 { 1625 union ctl_io *io; 1626 int file_fd, do_stdio; 1627 int cdbsize = -1, databytes; 1628 uint8_t *dataptr; 1629 char *filename = NULL; 1630 int datalen = -1, blocksize = -1; 1631 uint64_t lba = 0; 1632 int lba_set = 0; 1633 int retval; 1634 int c; 1635 1636 retval = 0; 1637 do_stdio = 0; 1638 dataptr = NULL; 1639 file_fd = -1; 1640 1641 io = ctl_scsi_alloc_io(iid); 1642 if (io == NULL) { 1643 warn("%s: can't allocate memory\n", __func__); 1644 return (1); 1645 } 1646 1647 while ((c = getopt(argc, argv, combinedopt)) != -1) { 1648 switch (c) { 1649 case 'N': 1650 io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE; 1651 break; 1652 case 'b': 1653 blocksize = strtoul(optarg, NULL, 0); 1654 break; 1655 case 'c': 1656 cdbsize = strtoul(optarg, NULL, 0); 1657 break; 1658 case 'd': 1659 datalen = strtoul(optarg, NULL, 0); 1660 break; 1661 case 'f': 1662 filename = strdup(optarg); 1663 break; 1664 case 'l': 1665 lba = strtoull(optarg, NULL, 0); 1666 lba_set = 1; 1667 break; 1668 default: 1669 break; 1670 } 1671 } 1672 if (filename == NULL) { 1673 warnx("%s: you must supply a filename using -f", __func__); 1674 retval = 1; 1675 goto bailout; 1676 } 1677 1678 if (datalen == -1) { 1679 warnx("%s: you must specify the data length with -d", __func__); 1680 retval = 1; 1681 goto bailout; 1682 } 1683 1684 if (lba_set == 0) { 1685 warnx("%s: you must specify the LBA with -l", __func__); 1686 retval = 1; 1687 goto bailout; 1688 } 1689 1690 if (blocksize == -1) { 1691 warnx("%s: you must specify the blocksize with -b", __func__); 1692 retval = 1; 1693 goto bailout; 1694 } 1695 1696 if (cdbsize != -1) { 1697 switch (cdbsize) { 1698 case 6: 1699 case 10: 1700 case 12: 1701 case 16: 1702 break; 1703 default: 1704 warnx("%s: invalid cdbsize %d, valid sizes are 6, " 1705 "10, 12 or 16", __func__, cdbsize); 1706 retval = 1; 1707 goto bailout; 1708 break; /* NOTREACHED */ 1709 } 1710 } else 1711 cdbsize = 6; 1712 1713 databytes = datalen * blocksize; 1714 dataptr = (uint8_t *)malloc(databytes); 1715 1716 if (dataptr == NULL) { 1717 warn("%s: can't allocate %d bytes\n", __func__, databytes); 1718 retval = 1; 1719 goto bailout; 1720 } 1721 if (strcmp(filename, "-") == 0) { 1722 if (command == CTLADM_CMD_READ) 1723 file_fd = STDOUT_FILENO; 1724 else 1725 file_fd = STDIN_FILENO; 1726 do_stdio = 1; 1727 } else { 1728 file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); 1729 if (file_fd == -1) { 1730 warn("%s: can't open file %s", __func__, filename); 1731 retval = 1; 1732 goto bailout; 1733 } 1734 } 1735 1736 memset(dataptr, 0, databytes); 1737 1738 if (command == CTLADM_CMD_WRITE) { 1739 int bytes_read; 1740 1741 bytes_read = read(file_fd, dataptr, databytes); 1742 if (bytes_read == -1) { 1743 warn("%s: error reading file %s", __func__, filename); 1744 retval = 1; 1745 goto bailout; 1746 } 1747 if (bytes_read != databytes) { 1748 warnx("%s: only read %d bytes from file %s", 1749 __func__, bytes_read, filename); 1750 retval = 1; 1751 goto bailout; 1752 } 1753 } 1754 ctl_scsi_read_write(io, 1755 /*data_ptr*/ dataptr, 1756 /*data_len*/ databytes, 1757 /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0, 1758 /*byte2*/ 0, 1759 /*minimum_cdb_size*/ cdbsize, 1760 /*lba*/ lba, 1761 /*num_blocks*/ datalen, 1762 /*tag_type*/ CTL_TAG_SIMPLE, 1763 /*control*/ 0); 1764 1765 io->io_hdr.nexus.targ_lun = lun; 1766 io->io_hdr.nexus.initid = iid; 1767 1768 if (cctl_do_io(fd, retries, io, __func__) != 0) { 1769 retval = 1; 1770 goto bailout; 1771 } 1772 1773 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 1774 && (command == CTLADM_CMD_READ)) { 1775 int bytes_written; 1776 1777 bytes_written = write(file_fd, dataptr, databytes); 1778 if (bytes_written == -1) { 1779 warn("%s: can't write to %s", __func__, filename); 1780 goto bailout; 1781 } 1782 } else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 1783 ctl_io_error_print(io, NULL, stderr); 1784 1785 1786 bailout: 1787 1788 ctl_scsi_free_io(io); 1789 1790 if (dataptr != NULL) 1791 free(dataptr); 1792 1793 if ((do_stdio == 0) 1794 && (file_fd != -1)) 1795 close(file_fd); 1796 1797 return (retval); 1798 } 1799 1800 static int 1801 cctl_get_luns(int fd, int lun, int iid, int retries, struct 1802 scsi_report_luns_data **lun_data, uint32_t *num_luns) 1803 { 1804 union ctl_io *io; 1805 uint32_t nluns; 1806 int lun_datalen; 1807 int retval; 1808 1809 retval = 0; 1810 1811 io = ctl_scsi_alloc_io(iid); 1812 if (io == NULL) { 1813 warnx("%s: can't allocate memory", __func__); 1814 return (1); 1815 } 1816 1817 /* 1818 * lun_data includes space for 1 lun, allocate space for 4 initially. 1819 * If that isn't enough, we'll allocate more. 1820 */ 1821 nluns = 4; 1822 retry: 1823 lun_datalen = sizeof(*lun_data) + 1824 (nluns * sizeof(struct scsi_report_luns_lundata)); 1825 *lun_data = malloc(lun_datalen); 1826 1827 if (*lun_data == NULL) { 1828 warnx("%s: can't allocate memory", __func__); 1829 ctl_scsi_free_io(io); 1830 return (1); 1831 } 1832 1833 ctl_scsi_report_luns(io, 1834 /*data_ptr*/ (uint8_t *)*lun_data, 1835 /*data_len*/ lun_datalen, 1836 /*select_report*/ RPL_REPORT_ALL, 1837 /*tag_type*/ CTL_TAG_SIMPLE, 1838 /*control*/ 0); 1839 1840 io->io_hdr.nexus.initid = iid; 1841 io->io_hdr.nexus.targ_lun = lun; 1842 1843 if (cctl_do_io(fd, retries, io, __func__) != 0) { 1844 retval = 1; 1845 goto bailout; 1846 } 1847 1848 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 1849 uint32_t returned_len, returned_luns; 1850 1851 returned_len = scsi_4btoul((*lun_data)->length); 1852 returned_luns = returned_len / 8; 1853 if (returned_luns > nluns) { 1854 nluns = returned_luns; 1855 free(*lun_data); 1856 goto retry; 1857 } 1858 /* These should be the same */ 1859 *num_luns = MIN(returned_luns, nluns); 1860 } else { 1861 ctl_io_error_print(io, NULL, stderr); 1862 retval = 1; 1863 } 1864 bailout: 1865 ctl_scsi_free_io(io); 1866 1867 return (retval); 1868 } 1869 1870 static int 1871 cctl_report_luns(int fd, int lun, int iid, int retries) 1872 { 1873 struct scsi_report_luns_data *lun_data; 1874 uint32_t num_luns, i; 1875 int retval; 1876 1877 lun_data = NULL; 1878 1879 if ((retval = cctl_get_luns(fd, lun, iid, retries, &lun_data, 1880 &num_luns)) != 0) 1881 goto bailout; 1882 1883 fprintf(stdout, "%u LUNs returned\n", num_luns); 1884 for (i = 0; i < num_luns; i++) { 1885 int lun_val; 1886 1887 /* 1888 * XXX KDM figure out a way to share this code with 1889 * cctl_lunlist()? 1890 */ 1891 switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) { 1892 case RPL_LUNDATA_ATYP_PERIPH: 1893 lun_val = lun_data->luns[i].lundata[1]; 1894 break; 1895 case RPL_LUNDATA_ATYP_FLAT: 1896 lun_val = (lun_data->luns[i].lundata[0] & 1897 RPL_LUNDATA_FLAT_LUN_MASK) | 1898 (lun_data->luns[i].lundata[1] << 1899 RPL_LUNDATA_FLAT_LUN_BITS); 1900 break; 1901 case RPL_LUNDATA_ATYP_LUN: 1902 case RPL_LUNDATA_ATYP_EXTLUN: 1903 default: 1904 fprintf(stdout, "Unsupported LUN format %d\n", 1905 lun_data->luns[i].lundata[0] & 1906 RPL_LUNDATA_ATYP_MASK); 1907 lun_val = -1; 1908 break; 1909 } 1910 if (lun_val == -1) 1911 continue; 1912 1913 fprintf(stdout, "%d\n", lun_val); 1914 } 1915 1916 bailout: 1917 if (lun_data != NULL) 1918 free(lun_data); 1919 1920 return (retval); 1921 } 1922 1923 static int 1924 cctl_tur(int fd, int lun, int iid, int retries) 1925 { 1926 union ctl_io *io; 1927 1928 io = ctl_scsi_alloc_io(iid); 1929 if (io == NULL) { 1930 fprintf(stderr, "can't allocate memory\n"); 1931 return (1); 1932 } 1933 1934 ctl_scsi_tur(io, 1935 /* tag_type */ CTL_TAG_SIMPLE, 1936 /* control */ 0); 1937 1938 io->io_hdr.nexus.targ_lun = lun; 1939 io->io_hdr.nexus.initid = iid; 1940 1941 if (cctl_do_io(fd, retries, io, __func__) != 0) { 1942 ctl_scsi_free_io(io); 1943 return (1); 1944 } 1945 1946 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 1947 fprintf(stdout, "Unit is ready\n"); 1948 else 1949 ctl_io_error_print(io, NULL, stderr); 1950 1951 return (0); 1952 } 1953 1954 static int 1955 cctl_get_inquiry(int fd, int lun, int iid, int retries, 1956 char *path_str, int path_len, 1957 struct scsi_inquiry_data *inq_data) 1958 { 1959 union ctl_io *io; 1960 int retval; 1961 1962 retval = 0; 1963 1964 io = ctl_scsi_alloc_io(iid); 1965 if (io == NULL) { 1966 warnx("cctl_inquiry: can't allocate memory\n"); 1967 return (1); 1968 } 1969 1970 ctl_scsi_inquiry(/*io*/ io, 1971 /*data_ptr*/ (uint8_t *)inq_data, 1972 /*data_len*/ sizeof(*inq_data), 1973 /*byte2*/ 0, 1974 /*page_code*/ 0, 1975 /*tag_type*/ CTL_TAG_SIMPLE, 1976 /*control*/ 0); 1977 1978 io->io_hdr.nexus.targ_lun = lun; 1979 io->io_hdr.nexus.initid = iid; 1980 1981 if (cctl_do_io(fd, retries, io, __func__) != 0) { 1982 retval = 1; 1983 goto bailout; 1984 } 1985 1986 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 1987 retval = 1; 1988 ctl_io_error_print(io, NULL, stderr); 1989 } else if (path_str != NULL) 1990 ctl_scsi_path_string(&io->io_hdr, path_str, path_len); 1991 1992 bailout: 1993 ctl_scsi_free_io(io); 1994 1995 return (retval); 1996 } 1997 1998 static int 1999 cctl_inquiry(int fd, int lun, int iid, int retries) 2000 { 2001 struct scsi_inquiry_data *inq_data; 2002 char scsi_path[40]; 2003 int retval; 2004 2005 inq_data = malloc(sizeof(*inq_data)); 2006 if (inq_data == NULL) { 2007 warnx("%s: can't allocate inquiry data", __func__); 2008 retval = 1; 2009 goto bailout; 2010 } 2011 2012 if ((retval = cctl_get_inquiry(fd, lun, iid, retries, scsi_path, 2013 sizeof(scsi_path), inq_data)) != 0) 2014 goto bailout; 2015 2016 printf("%s", scsi_path); 2017 scsi_print_inquiry(inq_data); 2018 2019 bailout: 2020 if (inq_data != NULL) 2021 free(inq_data); 2022 2023 return (retval); 2024 } 2025 2026 static int 2027 cctl_req_sense(int fd, int lun, int iid, int retries) 2028 { 2029 union ctl_io *io; 2030 struct scsi_sense_data *sense_data; 2031 int retval; 2032 2033 retval = 0; 2034 2035 io = ctl_scsi_alloc_io(iid); 2036 if (io == NULL) { 2037 warnx("cctl_req_sense: can't allocate memory\n"); 2038 return (1); 2039 } 2040 sense_data = malloc(sizeof(*sense_data)); 2041 memset(sense_data, 0, sizeof(*sense_data)); 2042 2043 ctl_scsi_request_sense(/*io*/ io, 2044 /*data_ptr*/ (uint8_t *)sense_data, 2045 /*data_len*/ sizeof(*sense_data), 2046 /*byte2*/ 0, 2047 /*tag_type*/ CTL_TAG_SIMPLE, 2048 /*control*/ 0); 2049 2050 io->io_hdr.nexus.targ_lun = lun; 2051 io->io_hdr.nexus.initid = iid; 2052 2053 if (cctl_do_io(fd, retries, io, __func__) != 0) { 2054 retval = 1; 2055 goto bailout; 2056 } 2057 2058 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 2059 bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data)); 2060 io->scsiio.sense_len = sizeof(*sense_data); 2061 ctl_scsi_sense_print(&io->scsiio, NULL, stdout); 2062 } else 2063 ctl_io_error_print(io, NULL, stderr); 2064 2065 bailout: 2066 2067 ctl_scsi_free_io(io); 2068 free(sense_data); 2069 2070 return (retval); 2071 } 2072 2073 static int 2074 cctl_report_target_port_group(int fd, int lun, int iid) 2075 { 2076 union ctl_io *io; 2077 uint32_t datalen; 2078 uint8_t *dataptr; 2079 int retval; 2080 2081 dataptr = NULL; 2082 retval = 0; 2083 2084 io = ctl_scsi_alloc_io(iid); 2085 if (io == NULL) { 2086 warn("%s: can't allocate memory", __func__); 2087 return (1); 2088 } 2089 2090 datalen = 64; 2091 dataptr = (uint8_t *)malloc(datalen); 2092 if (dataptr == NULL) { 2093 warn("%s: can't allocate %d bytes", __func__, datalen); 2094 retval = 1; 2095 goto bailout; 2096 } 2097 2098 memset(dataptr, 0, datalen); 2099 2100 ctl_scsi_maintenance_in(/*io*/ io, 2101 /*data_ptr*/ dataptr, 2102 /*data_len*/ datalen, 2103 /*action*/ SA_RPRT_TRGT_GRP, 2104 /*tag_type*/ CTL_TAG_SIMPLE, 2105 /*control*/ 0); 2106 2107 io->io_hdr.nexus.targ_lun = lun; 2108 io->io_hdr.nexus.initid = iid; 2109 2110 if (cctl_do_io(fd, 0, io, __func__) != 0) { 2111 retval = 1; 2112 goto bailout; 2113 } 2114 2115 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 2116 int returned_len, used_len; 2117 2118 returned_len = scsi_4btoul(&dataptr[0]) + 4; 2119 2120 for (used_len = 0; used_len < returned_len; used_len++) { 2121 fprintf(stdout, "0x%02x ", dataptr[used_len]); 2122 if (((used_len+1) % 8) == 0) 2123 fprintf(stdout, "\n"); 2124 } 2125 fprintf(stdout, "\n"); 2126 } else 2127 ctl_io_error_print(io, NULL, stderr); 2128 2129 bailout: 2130 ctl_scsi_free_io(io); 2131 2132 if (dataptr != NULL) 2133 free(dataptr); 2134 2135 return (retval); 2136 } 2137 2138 static int 2139 cctl_inquiry_vpd_devid(int fd, int lun, int iid) 2140 { 2141 union ctl_io *io; 2142 uint32_t datalen; 2143 uint8_t *dataptr; 2144 int retval; 2145 2146 retval = 0; 2147 dataptr = NULL; 2148 2149 io = ctl_scsi_alloc_io(iid); 2150 if (io == NULL) { 2151 warn("%s: can't allocate memory", __func__); 2152 return (1); 2153 } 2154 2155 datalen = 256; 2156 dataptr = (uint8_t *)malloc(datalen); 2157 if (dataptr == NULL) { 2158 warn("%s: can't allocate %d bytes", __func__, datalen); 2159 retval = 1; 2160 goto bailout; 2161 } 2162 2163 memset(dataptr, 0, datalen); 2164 2165 ctl_scsi_inquiry(/*io*/ io, 2166 /*data_ptr*/ dataptr, 2167 /*data_len*/ datalen, 2168 /*byte2*/ SI_EVPD, 2169 /*page_code*/ SVPD_DEVICE_ID, 2170 /*tag_type*/ CTL_TAG_SIMPLE, 2171 /*control*/ 0); 2172 2173 io->io_hdr.nexus.targ_lun = lun; 2174 io->io_hdr.nexus.initid = iid; 2175 2176 if (cctl_do_io(fd, 0, io, __func__) != 0) { 2177 retval = 1; 2178 goto bailout; 2179 } 2180 2181 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 2182 int returned_len, used_len; 2183 2184 returned_len = scsi_2btoul(&dataptr[2]) + 4; 2185 2186 for (used_len = 0; used_len < returned_len; used_len++) { 2187 fprintf(stdout, "0x%02x ", dataptr[used_len]); 2188 if (((used_len+1) % 8) == 0) 2189 fprintf(stdout, "\n"); 2190 } 2191 fprintf(stdout, "\n"); 2192 } else 2193 ctl_io_error_print(io, NULL, stderr); 2194 2195 bailout: 2196 ctl_scsi_free_io(io); 2197 2198 if (dataptr != NULL) 2199 free(dataptr); 2200 2201 return (retval); 2202 } 2203 2204 static int 2205 cctl_persistent_reserve_in(int fd, int lun, int iid, 2206 int argc, char **argv, char *combinedopt, 2207 int retry_count) 2208 { 2209 union ctl_io *io; 2210 uint32_t datalen; 2211 uint8_t *dataptr; 2212 int action = -1; 2213 int retval; 2214 int c; 2215 2216 retval = 0; 2217 dataptr = NULL; 2218 2219 io = ctl_scsi_alloc_io(iid); 2220 if (io == NULL) { 2221 warn("%s: can't allocate memory", __func__); 2222 return (1); 2223 } 2224 2225 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2226 switch (c) { 2227 case 'a': 2228 action = strtol(optarg, NULL, 0); 2229 break; 2230 default: 2231 break; 2232 } 2233 } 2234 2235 if (action < 0 || action > 2) { 2236 warn("action must be specified and in the range: 0-2"); 2237 retval = 1; 2238 goto bailout; 2239 } 2240 2241 2242 datalen = 256; 2243 dataptr = (uint8_t *)malloc(datalen); 2244 if (dataptr == NULL) { 2245 warn("%s: can't allocate %d bytes", __func__, datalen); 2246 retval = 1; 2247 goto bailout; 2248 } 2249 2250 memset(dataptr, 0, datalen); 2251 2252 ctl_scsi_persistent_res_in(io, 2253 /*data_ptr*/ dataptr, 2254 /*data_len*/ datalen, 2255 /*action*/ action, 2256 /*tag_type*/ CTL_TAG_SIMPLE, 2257 /*control*/ 0); 2258 2259 io->io_hdr.nexus.targ_lun = lun; 2260 io->io_hdr.nexus.initid = iid; 2261 2262 if (cctl_do_io(fd, retry_count, io, __func__) != 0) { 2263 retval = 1; 2264 goto bailout; 2265 } 2266 2267 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 2268 int returned_len, used_len; 2269 2270 switch (action) { 2271 case 0: 2272 returned_len = scsi_4btoul(&dataptr[4]) + 8; 2273 returned_len = min(returned_len, 256); 2274 break; 2275 case 1: 2276 returned_len = scsi_4btoul(&dataptr[4]) + 8; 2277 break; 2278 case 2: 2279 returned_len = 8; 2280 break; 2281 default: 2282 warnx("%s: invalid action %d", __func__, action); 2283 goto bailout; 2284 break; /* NOTREACHED */ 2285 } 2286 2287 for (used_len = 0; used_len < returned_len; used_len++) { 2288 fprintf(stdout, "0x%02x ", dataptr[used_len]); 2289 if (((used_len+1) % 8) == 0) 2290 fprintf(stdout, "\n"); 2291 } 2292 fprintf(stdout, "\n"); 2293 } else 2294 ctl_io_error_print(io, NULL, stderr); 2295 2296 bailout: 2297 ctl_scsi_free_io(io); 2298 2299 if (dataptr != NULL) 2300 free(dataptr); 2301 2302 return (retval); 2303 } 2304 2305 static int 2306 cctl_persistent_reserve_out(int fd, int lun, int iid, 2307 int argc, char **argv, char *combinedopt, 2308 int retry_count) 2309 { 2310 union ctl_io *io; 2311 uint32_t datalen; 2312 uint64_t key = 0, sa_key = 0; 2313 int action = -1, restype = -1; 2314 uint8_t *dataptr; 2315 int retval; 2316 int c; 2317 2318 retval = 0; 2319 dataptr = NULL; 2320 2321 io = ctl_scsi_alloc_io(iid); 2322 if (io == NULL) { 2323 warn("%s: can't allocate memory", __func__); 2324 return (1); 2325 } 2326 2327 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2328 switch (c) { 2329 case 'a': 2330 action = strtol(optarg, NULL, 0); 2331 break; 2332 case 'k': 2333 key = strtoull(optarg, NULL, 0); 2334 break; 2335 case 'r': 2336 restype = strtol(optarg, NULL, 0); 2337 break; 2338 case 's': 2339 sa_key = strtoull(optarg, NULL, 0); 2340 break; 2341 default: 2342 break; 2343 } 2344 } 2345 if (action < 0 || action > 5) { 2346 warn("action must be specified and in the range: 0-5"); 2347 retval = 1; 2348 goto bailout; 2349 } 2350 2351 if (restype < 0 || restype > 5) { 2352 if (action != 0 && action != 5 && action != 3) { 2353 warn("'restype' must specified and in the range: 0-5"); 2354 retval = 1; 2355 goto bailout; 2356 } 2357 } 2358 2359 datalen = 24; 2360 dataptr = (uint8_t *)malloc(datalen); 2361 if (dataptr == NULL) { 2362 warn("%s: can't allocate %d bytes", __func__, datalen); 2363 retval = 1; 2364 goto bailout; 2365 } 2366 2367 memset(dataptr, 0, datalen); 2368 2369 ctl_scsi_persistent_res_out(io, 2370 /*data_ptr*/ dataptr, 2371 /*data_len*/ datalen, 2372 /*action*/ action, 2373 /*type*/ restype, 2374 /*key*/ key, 2375 /*sa key*/ sa_key, 2376 /*tag_type*/ CTL_TAG_SIMPLE, 2377 /*control*/ 0); 2378 2379 io->io_hdr.nexus.targ_lun = lun; 2380 io->io_hdr.nexus.initid = iid; 2381 2382 if (cctl_do_io(fd, retry_count, io, __func__) != 0) { 2383 retval = 1; 2384 goto bailout; 2385 } 2386 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) { 2387 char scsi_path[40]; 2388 ctl_scsi_path_string(&io->io_hdr, scsi_path, sizeof(scsi_path)); 2389 fprintf( stdout, "%sPERSISTENT RESERVE OUT executed " 2390 "successfully\n", scsi_path); 2391 } else 2392 ctl_io_error_print(io, NULL, stderr); 2393 2394 bailout: 2395 ctl_scsi_free_io(io); 2396 2397 if (dataptr != NULL) 2398 free(dataptr); 2399 2400 return (retval); 2401 } 2402 2403 static int 2404 cctl_create_lun(int fd, int argc, char **argv, char *combinedopt) 2405 { 2406 struct ctl_lun_req req; 2407 int device_type = -1; 2408 uint64_t lun_size = 0; 2409 uint32_t blocksize = 0, req_lun_id = 0; 2410 char *serial_num = NULL; 2411 char *device_id = NULL; 2412 int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0; 2413 char *backend_name = NULL; 2414 nvlist_t *option_list; 2415 int retval = 0, c; 2416 2417 option_list = nvlist_create(0); 2418 if (option_list == NULL) 2419 err(1, "%s: unable to allocate nvlist", __func__); 2420 2421 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2422 switch (c) { 2423 case 'b': 2424 backend_name = strdup(optarg); 2425 break; 2426 case 'B': 2427 blocksize = strtoul(optarg, NULL, 0); 2428 blocksize_set = 1; 2429 break; 2430 case 'd': 2431 device_id = strdup(optarg); 2432 break; 2433 case 'l': 2434 req_lun_id = strtoul(optarg, NULL, 0); 2435 lun_id_set = 1; 2436 break; 2437 case 'o': { 2438 char *tmpstr; 2439 char *name, *value; 2440 2441 tmpstr = strdup(optarg); 2442 name = strsep(&tmpstr, "="); 2443 if (name == NULL) { 2444 warnx("%s: option -o takes \"name=value\"" 2445 "argument", __func__); 2446 retval = 1; 2447 goto bailout; 2448 } 2449 value = strsep(&tmpstr, "="); 2450 if (value == NULL) { 2451 warnx("%s: option -o takes \"name=value\"" 2452 "argument", __func__); 2453 retval = 1; 2454 goto bailout; 2455 } 2456 free(tmpstr); 2457 nvlist_add_string(option_list, name, value); 2458 break; 2459 } 2460 case 's': 2461 if (strcasecmp(optarg, "auto") != 0) { 2462 retval = expand_number(optarg, &lun_size); 2463 if (retval != 0) { 2464 warn("%s: invalid -s argument", 2465 __func__); 2466 retval = 1; 2467 goto bailout; 2468 } 2469 } 2470 lun_size_set = 1; 2471 break; 2472 case 'S': 2473 serial_num = strdup(optarg); 2474 break; 2475 case 't': 2476 device_type = strtoul(optarg, NULL, 0); 2477 break; 2478 default: 2479 break; 2480 } 2481 } 2482 2483 if (backend_name == NULL) { 2484 warnx("%s: backend name (-b) must be specified", __func__); 2485 retval = 1; 2486 goto bailout; 2487 } 2488 2489 bzero(&req, sizeof(req)); 2490 2491 strlcpy(req.backend, backend_name, sizeof(req.backend)); 2492 req.reqtype = CTL_LUNREQ_CREATE; 2493 2494 if (blocksize_set != 0) 2495 req.reqdata.create.blocksize_bytes = blocksize; 2496 2497 if (lun_size_set != 0) 2498 req.reqdata.create.lun_size_bytes = lun_size; 2499 2500 if (lun_id_set != 0) { 2501 req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ; 2502 req.reqdata.create.req_lun_id = req_lun_id; 2503 } 2504 2505 req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE; 2506 2507 if (device_type != -1) 2508 req.reqdata.create.device_type = device_type; 2509 else 2510 req.reqdata.create.device_type = T_DIRECT; 2511 2512 if (serial_num != NULL) { 2513 strlcpy(req.reqdata.create.serial_num, serial_num, 2514 sizeof(req.reqdata.create.serial_num)); 2515 req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM; 2516 } 2517 2518 if (device_id != NULL) { 2519 strlcpy(req.reqdata.create.device_id, device_id, 2520 sizeof(req.reqdata.create.device_id)); 2521 req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID; 2522 } 2523 2524 req.args = nvlist_pack(option_list, &req.args_len); 2525 if (req.args == NULL) { 2526 warn("%s: error packing nvlist", __func__); 2527 retval = 1; 2528 goto bailout; 2529 } 2530 2531 retval = ioctl(fd, CTL_LUN_REQ, &req); 2532 free(req.args); 2533 if (retval == -1) { 2534 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__); 2535 retval = 1; 2536 goto bailout; 2537 } 2538 2539 switch (req.status) { 2540 case CTL_LUN_ERROR: 2541 warnx("LUN creation error: %s", req.error_str); 2542 retval = 1; 2543 goto bailout; 2544 case CTL_LUN_WARNING: 2545 warnx("LUN creation warning: %s", req.error_str); 2546 break; 2547 case CTL_LUN_OK: 2548 break; 2549 default: 2550 warnx("unknown LUN creation status: %d", req.status); 2551 retval = 1; 2552 goto bailout; 2553 } 2554 2555 fprintf(stdout, "LUN created successfully\n"); 2556 fprintf(stdout, "backend: %s\n", req.backend); 2557 fprintf(stdout, "device type: %d\n",req.reqdata.create.device_type); 2558 fprintf(stdout, "LUN size: %ju bytes\n", 2559 (uintmax_t)req.reqdata.create.lun_size_bytes); 2560 fprintf(stdout, "blocksize %u bytes\n", 2561 req.reqdata.create.blocksize_bytes); 2562 fprintf(stdout, "LUN ID: %d\n", req.reqdata.create.req_lun_id); 2563 fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num); 2564 fprintf(stdout, "Device ID: %s\n", req.reqdata.create.device_id); 2565 2566 bailout: 2567 nvlist_destroy(option_list); 2568 return (retval); 2569 } 2570 2571 static int 2572 cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt) 2573 { 2574 struct ctl_lun_req req; 2575 uint32_t lun_id = 0; 2576 int lun_id_set = 0; 2577 char *backend_name = NULL; 2578 nvlist_t *option_list; 2579 int retval = 0, c; 2580 2581 option_list = nvlist_create(0); 2582 if (option_list == NULL) 2583 err(1, "%s: unable to allocate nvlist", __func__); 2584 2585 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2586 switch (c) { 2587 case 'b': 2588 backend_name = strdup(optarg); 2589 break; 2590 case 'l': 2591 lun_id = strtoul(optarg, NULL, 0); 2592 lun_id_set = 1; 2593 break; 2594 case 'o': { 2595 char *tmpstr; 2596 char *name, *value; 2597 2598 tmpstr = strdup(optarg); 2599 name = strsep(&tmpstr, "="); 2600 if (name == NULL) { 2601 warnx("%s: option -o takes \"name=value\"" 2602 "argument", __func__); 2603 retval = 1; 2604 goto bailout; 2605 } 2606 value = strsep(&tmpstr, "="); 2607 if (value == NULL) { 2608 warnx("%s: option -o takes \"name=value\"" 2609 "argument", __func__); 2610 retval = 1; 2611 goto bailout; 2612 } 2613 free(tmpstr); 2614 nvlist_add_string(option_list, name, value); 2615 break; 2616 } 2617 default: 2618 break; 2619 } 2620 } 2621 2622 if (backend_name == NULL) 2623 errx(1, "%s: backend name (-b) must be specified", __func__); 2624 2625 if (lun_id_set == 0) 2626 errx(1, "%s: LUN id (-l) must be specified", __func__); 2627 2628 bzero(&req, sizeof(req)); 2629 2630 strlcpy(req.backend, backend_name, sizeof(req.backend)); 2631 req.reqtype = CTL_LUNREQ_RM; 2632 2633 req.reqdata.rm.lun_id = lun_id; 2634 2635 req.args = nvlist_pack(option_list, &req.args_len); 2636 if (req.args == NULL) { 2637 warn("%s: error packing nvlist", __func__); 2638 retval = 1; 2639 goto bailout; 2640 } 2641 2642 retval = ioctl(fd, CTL_LUN_REQ, &req); 2643 free(req.args); 2644 if (retval == -1) { 2645 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__); 2646 retval = 1; 2647 goto bailout; 2648 } 2649 2650 switch (req.status) { 2651 case CTL_LUN_ERROR: 2652 warnx("LUN removal error: %s", req.error_str); 2653 retval = 1; 2654 goto bailout; 2655 case CTL_LUN_WARNING: 2656 warnx("LUN removal warning: %s", req.error_str); 2657 break; 2658 case CTL_LUN_OK: 2659 break; 2660 default: 2661 warnx("unknown LUN removal status: %d", req.status); 2662 retval = 1; 2663 goto bailout; 2664 } 2665 2666 printf("LUN %d removed successfully\n", lun_id); 2667 2668 bailout: 2669 nvlist_destroy(option_list); 2670 return (retval); 2671 } 2672 2673 static int 2674 cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt) 2675 { 2676 struct ctl_lun_req req; 2677 uint64_t lun_size = 0; 2678 uint32_t lun_id = 0; 2679 int lun_id_set = 0, lun_size_set = 0; 2680 char *backend_name = NULL; 2681 nvlist_t *option_list; 2682 int retval = 0, c; 2683 2684 option_list = nvlist_create(0); 2685 if (option_list == NULL) 2686 err(1, "%s: unable to allocate nvlist", __func__); 2687 2688 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2689 switch (c) { 2690 case 'b': 2691 backend_name = strdup(optarg); 2692 break; 2693 case 'l': 2694 lun_id = strtoul(optarg, NULL, 0); 2695 lun_id_set = 1; 2696 break; 2697 case 'o': { 2698 char *tmpstr; 2699 char *name, *value; 2700 2701 tmpstr = strdup(optarg); 2702 name = strsep(&tmpstr, "="); 2703 if (name == NULL) { 2704 warnx("%s: option -o takes \"name=value\"" 2705 "argument", __func__); 2706 retval = 1; 2707 goto bailout; 2708 } 2709 value = strsep(&tmpstr, "="); 2710 if (value == NULL) { 2711 warnx("%s: option -o takes \"name=value\"" 2712 "argument", __func__); 2713 retval = 1; 2714 goto bailout; 2715 } 2716 free(tmpstr); 2717 nvlist_add_string(option_list, name, value); 2718 break; 2719 } 2720 case 's': 2721 if (strcasecmp(optarg, "auto") != 0) { 2722 retval = expand_number(optarg, &lun_size); 2723 if (retval != 0) { 2724 warn("%s: invalid -s argument", 2725 __func__); 2726 retval = 1; 2727 goto bailout; 2728 } 2729 } 2730 lun_size_set = 1; 2731 break; 2732 default: 2733 break; 2734 } 2735 } 2736 2737 if (backend_name == NULL) 2738 errx(1, "%s: backend name (-b) must be specified", __func__); 2739 2740 if (lun_id_set == 0) 2741 errx(1, "%s: LUN id (-l) must be specified", __func__); 2742 2743 if (lun_size_set == 0 && nvlist_empty(option_list)) 2744 errx(1, "%s: size (-s) or options (-o) must be specified", 2745 __func__); 2746 2747 bzero(&req, sizeof(req)); 2748 2749 strlcpy(req.backend, backend_name, sizeof(req.backend)); 2750 req.reqtype = CTL_LUNREQ_MODIFY; 2751 2752 req.reqdata.modify.lun_id = lun_id; 2753 req.reqdata.modify.lun_size_bytes = lun_size; 2754 2755 req.args = nvlist_pack(option_list, &req.args_len); 2756 if (req.args == NULL) { 2757 warn("%s: error packing nvlist", __func__); 2758 retval = 1; 2759 goto bailout; 2760 } 2761 2762 retval = ioctl(fd, CTL_LUN_REQ, &req); 2763 free(req.args); 2764 if (retval == -1) { 2765 warn("%s: error issuing CTL_LUN_REQ ioctl", __func__); 2766 retval = 1; 2767 goto bailout; 2768 } 2769 2770 switch (req.status) { 2771 case CTL_LUN_ERROR: 2772 warnx("LUN modification error: %s", req.error_str); 2773 retval = 1; 2774 goto bailout; 2775 case CTL_LUN_WARNING: 2776 warnx("LUN modification warning: %s", req.error_str); 2777 break; 2778 case CTL_LUN_OK: 2779 break; 2780 default: 2781 warnx("unknown LUN modification status: %d", req.status); 2782 retval = 1; 2783 goto bailout; 2784 } 2785 2786 printf("LUN %d modified successfully\n", lun_id); 2787 2788 bailout: 2789 nvlist_destroy(option_list); 2790 return (retval); 2791 } 2792 2793 struct cctl_islist_conn { 2794 int connection_id; 2795 char *initiator; 2796 char *initiator_addr; 2797 char *initiator_alias; 2798 char *target; 2799 char *target_alias; 2800 char *header_digest; 2801 char *data_digest; 2802 char *max_recv_data_segment_length; 2803 char *max_send_data_segment_length; 2804 char *max_burst_length; 2805 char *first_burst_length; 2806 char *offload; 2807 int immediate_data; 2808 int iser; 2809 STAILQ_ENTRY(cctl_islist_conn) links; 2810 }; 2811 2812 struct cctl_islist_data { 2813 int num_conns; 2814 STAILQ_HEAD(,cctl_islist_conn) conn_list; 2815 struct cctl_islist_conn *cur_conn; 2816 int level; 2817 struct sbuf *cur_sb[32]; 2818 }; 2819 2820 static void 2821 cctl_islist_start_element(void *user_data, const char *name, const char **attr) 2822 { 2823 int i; 2824 struct cctl_islist_data *islist; 2825 struct cctl_islist_conn *cur_conn; 2826 2827 islist = (struct cctl_islist_data *)user_data; 2828 cur_conn = islist->cur_conn; 2829 islist->level++; 2830 if ((u_int)islist->level >= nitems(islist->cur_sb)) 2831 errx(1, "%s: too many nesting levels, %zd max", __func__, 2832 nitems(islist->cur_sb)); 2833 2834 islist->cur_sb[islist->level] = sbuf_new_auto(); 2835 if (islist->cur_sb[islist->level] == NULL) 2836 err(1, "%s: Unable to allocate sbuf", __func__); 2837 2838 if (strcmp(name, "connection") == 0) { 2839 if (cur_conn != NULL) 2840 errx(1, "%s: improper connection element nesting", 2841 __func__); 2842 2843 cur_conn = calloc(1, sizeof(*cur_conn)); 2844 if (cur_conn == NULL) 2845 err(1, "%s: cannot allocate %zd bytes", __func__, 2846 sizeof(*cur_conn)); 2847 2848 islist->num_conns++; 2849 islist->cur_conn = cur_conn; 2850 2851 STAILQ_INSERT_TAIL(&islist->conn_list, cur_conn, links); 2852 2853 for (i = 0; attr[i] != NULL; i += 2) { 2854 if (strcmp(attr[i], "id") == 0) { 2855 cur_conn->connection_id = 2856 strtoull(attr[i+1], NULL, 0); 2857 } else { 2858 errx(1, 2859 "%s: invalid connection attribute %s = %s", 2860 __func__, attr[i], attr[i+1]); 2861 } 2862 } 2863 } 2864 } 2865 2866 static void 2867 cctl_islist_end_element(void *user_data, const char *name) 2868 { 2869 struct cctl_islist_data *islist; 2870 struct cctl_islist_conn *cur_conn; 2871 char *str; 2872 2873 islist = (struct cctl_islist_data *)user_data; 2874 cur_conn = islist->cur_conn; 2875 2876 if ((cur_conn == NULL) 2877 && (strcmp(name, "ctlislist") != 0)) 2878 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name); 2879 2880 if (islist->cur_sb[islist->level] == NULL) 2881 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 2882 islist->level, name); 2883 2884 sbuf_finish(islist->cur_sb[islist->level]); 2885 str = strdup(sbuf_data(islist->cur_sb[islist->level])); 2886 if (str == NULL) 2887 err(1, "%s can't allocate %zd bytes for string", __func__, 2888 sbuf_len(islist->cur_sb[islist->level])); 2889 2890 sbuf_delete(islist->cur_sb[islist->level]); 2891 islist->cur_sb[islist->level] = NULL; 2892 islist->level--; 2893 2894 if (strcmp(name, "initiator") == 0) { 2895 cur_conn->initiator = str; 2896 str = NULL; 2897 } else if (strcmp(name, "initiator_addr") == 0) { 2898 cur_conn->initiator_addr = str; 2899 str = NULL; 2900 } else if (strcmp(name, "initiator_alias") == 0) { 2901 cur_conn->initiator_alias = str; 2902 str = NULL; 2903 } else if (strcmp(name, "target") == 0) { 2904 cur_conn->target = str; 2905 str = NULL; 2906 } else if (strcmp(name, "target_alias") == 0) { 2907 cur_conn->target_alias = str; 2908 str = NULL; 2909 } else if (strcmp(name, "target_portal_group_tag") == 0) { 2910 } else if (strcmp(name, "header_digest") == 0) { 2911 cur_conn->header_digest = str; 2912 str = NULL; 2913 } else if (strcmp(name, "data_digest") == 0) { 2914 cur_conn->data_digest = str; 2915 str = NULL; 2916 } else if (strcmp(name, "max_recv_data_segment_length") == 0) { 2917 cur_conn->max_recv_data_segment_length = str; 2918 str = NULL; 2919 } else if (strcmp(name, "max_send_data_segment_length") == 0) { 2920 cur_conn->max_send_data_segment_length = str; 2921 str = NULL; 2922 } else if (strcmp(name, "max_burst_length") == 0) { 2923 cur_conn->max_burst_length = str; 2924 str = NULL; 2925 } else if (strcmp(name, "first_burst_length") == 0) { 2926 cur_conn->first_burst_length = str; 2927 str = NULL; 2928 } else if (strcmp(name, "offload") == 0) { 2929 cur_conn->offload = str; 2930 str = NULL; 2931 } else if (strcmp(name, "immediate_data") == 0) { 2932 cur_conn->immediate_data = atoi(str); 2933 } else if (strcmp(name, "iser") == 0) { 2934 cur_conn->iser = atoi(str); 2935 } else if (strcmp(name, "connection") == 0) { 2936 islist->cur_conn = NULL; 2937 } else if (strcmp(name, "ctlislist") == 0) { 2938 /* Nothing. */ 2939 } else { 2940 /* 2941 * Unknown element; ignore it for forward compatibility. 2942 */ 2943 } 2944 2945 free(str); 2946 } 2947 2948 static void 2949 cctl_islist_char_handler(void *user_data, const XML_Char *str, int len) 2950 { 2951 struct cctl_islist_data *islist; 2952 2953 islist = (struct cctl_islist_data *)user_data; 2954 2955 sbuf_bcat(islist->cur_sb[islist->level], str, len); 2956 } 2957 2958 static int 2959 cctl_islist(int fd, int argc, char **argv, char *combinedopt) 2960 { 2961 struct ctl_iscsi req; 2962 struct cctl_islist_data islist; 2963 struct cctl_islist_conn *conn; 2964 XML_Parser parser; 2965 char *conn_str; 2966 int conn_len; 2967 int dump_xml = 0; 2968 int c, retval, verbose = 0; 2969 2970 retval = 0; 2971 conn_len = 4096; 2972 2973 bzero(&islist, sizeof(islist)); 2974 STAILQ_INIT(&islist.conn_list); 2975 2976 while ((c = getopt(argc, argv, combinedopt)) != -1) { 2977 switch (c) { 2978 case 'v': 2979 verbose = 1; 2980 break; 2981 case 'x': 2982 dump_xml = 1; 2983 break; 2984 default: 2985 break; 2986 } 2987 } 2988 2989 retry: 2990 conn_str = malloc(conn_len); 2991 2992 bzero(&req, sizeof(req)); 2993 req.type = CTL_ISCSI_LIST; 2994 req.data.list.alloc_len = conn_len; 2995 req.data.list.conn_xml = conn_str; 2996 2997 if (ioctl(fd, CTL_ISCSI, &req) == -1) { 2998 warn("%s: error issuing CTL_ISCSI ioctl", __func__); 2999 retval = 1; 3000 goto bailout; 3001 } 3002 3003 if (req.status == CTL_ISCSI_ERROR) { 3004 warnx("%s: error returned from CTL_ISCSI ioctl:\n%s", 3005 __func__, req.error_str); 3006 } else if (req.status == CTL_ISCSI_LIST_NEED_MORE_SPACE) { 3007 conn_len = conn_len << 1; 3008 goto retry; 3009 } 3010 3011 if (dump_xml != 0) { 3012 printf("%s", conn_str); 3013 goto bailout; 3014 } 3015 3016 parser = XML_ParserCreate(NULL); 3017 if (parser == NULL) { 3018 warn("%s: Unable to create XML parser", __func__); 3019 retval = 1; 3020 goto bailout; 3021 } 3022 3023 XML_SetUserData(parser, &islist); 3024 XML_SetElementHandler(parser, cctl_islist_start_element, 3025 cctl_islist_end_element); 3026 XML_SetCharacterDataHandler(parser, cctl_islist_char_handler); 3027 3028 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1); 3029 if (retval != 1) { 3030 warnx("%s: Unable to parse XML: Error %d", __func__, 3031 XML_GetErrorCode(parser)); 3032 XML_ParserFree(parser); 3033 retval = 1; 3034 goto bailout; 3035 } 3036 retval = 0; 3037 XML_ParserFree(parser); 3038 3039 if (verbose != 0) { 3040 STAILQ_FOREACH(conn, &islist.conn_list, links) { 3041 printf("%-25s %d\n", "Session ID:", conn->connection_id); 3042 printf("%-25s %s\n", "Initiator name:", conn->initiator); 3043 printf("%-25s %s\n", "Initiator portal:", conn->initiator_addr); 3044 printf("%-25s %s\n", "Initiator alias:", conn->initiator_alias); 3045 printf("%-25s %s\n", "Target name:", conn->target); 3046 printf("%-25s %s\n", "Target alias:", conn->target_alias); 3047 printf("%-25s %s\n", "Header digest:", conn->header_digest); 3048 printf("%-25s %s\n", "Data digest:", conn->data_digest); 3049 printf("%-25s %s\n", "MaxRecvDataSegmentLength:", conn->max_recv_data_segment_length); 3050 printf("%-25s %s\n", "MaxSendDataSegmentLength:", conn->max_send_data_segment_length); 3051 printf("%-25s %s\n", "MaxBurstLen:", conn->max_burst_length); 3052 printf("%-25s %s\n", "FirstBurstLen:", conn->first_burst_length); 3053 printf("%-25s %s\n", "ImmediateData:", conn->immediate_data ? "Yes" : "No"); 3054 printf("%-25s %s\n", "iSER (RDMA):", conn->iser ? "Yes" : "No"); 3055 printf("%-25s %s\n", "Offload driver:", conn->offload); 3056 printf("\n"); 3057 } 3058 } else { 3059 printf("%4s %-16s %-36s %-36s\n", "ID", "Portal", "Initiator name", 3060 "Target name"); 3061 STAILQ_FOREACH(conn, &islist.conn_list, links) { 3062 printf("%4u %-16s %-36s %-36s\n", 3063 conn->connection_id, conn->initiator_addr, conn->initiator, 3064 conn->target); 3065 } 3066 } 3067 bailout: 3068 free(conn_str); 3069 3070 return (retval); 3071 } 3072 3073 static int 3074 cctl_islogout(int fd, int argc, char **argv, char *combinedopt) 3075 { 3076 struct ctl_iscsi req; 3077 int retval = 0, c; 3078 int all = 0, connection_id = -1, nargs = 0; 3079 char *initiator_name = NULL, *initiator_addr = NULL; 3080 3081 while ((c = getopt(argc, argv, combinedopt)) != -1) { 3082 switch (c) { 3083 case 'a': 3084 all = 1; 3085 nargs++; 3086 break; 3087 case 'c': 3088 connection_id = strtoul(optarg, NULL, 0); 3089 nargs++; 3090 break; 3091 case 'i': 3092 initiator_name = strdup(optarg); 3093 if (initiator_name == NULL) 3094 err(1, "%s: strdup", __func__); 3095 nargs++; 3096 break; 3097 case 'p': 3098 initiator_addr = strdup(optarg); 3099 if (initiator_addr == NULL) 3100 err(1, "%s: strdup", __func__); 3101 nargs++; 3102 break; 3103 default: 3104 break; 3105 } 3106 } 3107 3108 if (nargs == 0) 3109 errx(1, "%s: either -a, -c, -i, or -p must be specified", 3110 __func__); 3111 if (nargs > 1) 3112 errx(1, "%s: only one of -a, -c, -i, or -p may be specified", 3113 __func__); 3114 3115 bzero(&req, sizeof(req)); 3116 req.type = CTL_ISCSI_LOGOUT; 3117 req.data.logout.connection_id = connection_id; 3118 if (initiator_addr != NULL) 3119 strlcpy(req.data.logout.initiator_addr, 3120 initiator_addr, sizeof(req.data.logout.initiator_addr)); 3121 if (initiator_name != NULL) 3122 strlcpy(req.data.logout.initiator_name, 3123 initiator_name, sizeof(req.data.logout.initiator_name)); 3124 if (all != 0) 3125 req.data.logout.all = 1; 3126 3127 if (ioctl(fd, CTL_ISCSI, &req) == -1) { 3128 warn("%s: error issuing CTL_ISCSI ioctl", __func__); 3129 retval = 1; 3130 goto bailout; 3131 } 3132 3133 if (req.status != CTL_ISCSI_OK) { 3134 warnx("%s: error returned from CTL iSCSI logout request:\n%s", 3135 __func__, req.error_str); 3136 retval = 1; 3137 goto bailout; 3138 } 3139 3140 printf("iSCSI logout requests submitted\n"); 3141 3142 bailout: 3143 return (retval); 3144 } 3145 3146 static int 3147 cctl_isterminate(int fd, int argc, char **argv, char *combinedopt) 3148 { 3149 struct ctl_iscsi req; 3150 int retval = 0, c; 3151 int all = 0, connection_id = -1, nargs = 0; 3152 char *initiator_name = NULL, *initiator_addr = NULL; 3153 3154 while ((c = getopt(argc, argv, combinedopt)) != -1) { 3155 switch (c) { 3156 case 'a': 3157 all = 1; 3158 nargs++; 3159 break; 3160 case 'c': 3161 connection_id = strtoul(optarg, NULL, 0); 3162 nargs++; 3163 break; 3164 case 'i': 3165 initiator_name = strdup(optarg); 3166 if (initiator_name == NULL) 3167 err(1, "%s: strdup", __func__); 3168 nargs++; 3169 break; 3170 case 'p': 3171 initiator_addr = strdup(optarg); 3172 if (initiator_addr == NULL) 3173 err(1, "%s: strdup", __func__); 3174 nargs++; 3175 break; 3176 default: 3177 break; 3178 } 3179 } 3180 3181 if (nargs == 0) 3182 errx(1, "%s: either -a, -c, -i, or -p must be specified", 3183 __func__); 3184 if (nargs > 1) 3185 errx(1, "%s: only one of -a, -c, -i, or -p may be specified", 3186 __func__); 3187 3188 bzero(&req, sizeof(req)); 3189 req.type = CTL_ISCSI_TERMINATE; 3190 req.data.terminate.connection_id = connection_id; 3191 if (initiator_addr != NULL) 3192 strlcpy(req.data.terminate.initiator_addr, 3193 initiator_addr, sizeof(req.data.terminate.initiator_addr)); 3194 if (initiator_name != NULL) 3195 strlcpy(req.data.terminate.initiator_name, 3196 initiator_name, sizeof(req.data.terminate.initiator_name)); 3197 if (all != 0) 3198 req.data.terminate.all = 1; 3199 3200 if (ioctl(fd, CTL_ISCSI, &req) == -1) { 3201 warn("%s: error issuing CTL_ISCSI ioctl", __func__); 3202 retval = 1; 3203 goto bailout; 3204 } 3205 3206 if (req.status != CTL_ISCSI_OK) { 3207 warnx("%s: error returned from CTL iSCSI connection " 3208 "termination request:\n%s", __func__, req.error_str); 3209 retval = 1; 3210 goto bailout; 3211 } 3212 3213 printf("iSCSI connections terminated\n"); 3214 3215 bailout: 3216 return (retval); 3217 } 3218 3219 /* 3220 * Name/value pair used for per-LUN attributes. 3221 */ 3222 struct cctl_lun_nv { 3223 char *name; 3224 char *value; 3225 STAILQ_ENTRY(cctl_lun_nv) links; 3226 }; 3227 3228 /* 3229 * Backend LUN information. 3230 */ 3231 struct cctl_lun { 3232 uint64_t lun_id; 3233 char *backend_type; 3234 uint64_t size_blocks; 3235 uint32_t blocksize; 3236 char *serial_number; 3237 char *device_id; 3238 STAILQ_HEAD(,cctl_lun_nv) attr_list; 3239 STAILQ_ENTRY(cctl_lun) links; 3240 }; 3241 3242 struct cctl_devlist_data { 3243 int num_luns; 3244 STAILQ_HEAD(,cctl_lun) lun_list; 3245 struct cctl_lun *cur_lun; 3246 int level; 3247 struct sbuf *cur_sb[32]; 3248 }; 3249 3250 static void 3251 cctl_start_element(void *user_data, const char *name, const char **attr) 3252 { 3253 int i; 3254 struct cctl_devlist_data *devlist; 3255 struct cctl_lun *cur_lun; 3256 3257 devlist = (struct cctl_devlist_data *)user_data; 3258 cur_lun = devlist->cur_lun; 3259 devlist->level++; 3260 if ((u_int)devlist->level >= nitems(devlist->cur_sb)) 3261 errx(1, "%s: too many nesting levels, %zd max", __func__, 3262 nitems(devlist->cur_sb)); 3263 3264 devlist->cur_sb[devlist->level] = sbuf_new_auto(); 3265 if (devlist->cur_sb[devlist->level] == NULL) 3266 err(1, "%s: Unable to allocate sbuf", __func__); 3267 3268 if (strcmp(name, "lun") == 0) { 3269 if (cur_lun != NULL) 3270 errx(1, "%s: improper lun element nesting", __func__); 3271 3272 cur_lun = calloc(1, sizeof(*cur_lun)); 3273 if (cur_lun == NULL) 3274 err(1, "%s: cannot allocate %zd bytes", __func__, 3275 sizeof(*cur_lun)); 3276 3277 devlist->num_luns++; 3278 devlist->cur_lun = cur_lun; 3279 3280 STAILQ_INIT(&cur_lun->attr_list); 3281 STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links); 3282 3283 for (i = 0; attr[i] != NULL; i += 2) { 3284 if (strcmp(attr[i], "id") == 0) { 3285 cur_lun->lun_id = strtoull(attr[i+1], NULL, 0); 3286 } else { 3287 errx(1, "%s: invalid LUN attribute %s = %s", 3288 __func__, attr[i], attr[i+1]); 3289 } 3290 } 3291 } 3292 } 3293 3294 static void 3295 cctl_end_element(void *user_data, const char *name) 3296 { 3297 struct cctl_devlist_data *devlist; 3298 struct cctl_lun *cur_lun; 3299 char *str; 3300 3301 devlist = (struct cctl_devlist_data *)user_data; 3302 cur_lun = devlist->cur_lun; 3303 3304 if ((cur_lun == NULL) 3305 && (strcmp(name, "ctllunlist") != 0)) 3306 errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name); 3307 3308 if (devlist->cur_sb[devlist->level] == NULL) 3309 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 3310 devlist->level, name); 3311 3312 if (sbuf_finish(devlist->cur_sb[devlist->level]) != 0) 3313 err(1, "%s: sbuf_finish", __func__); 3314 str = strdup(sbuf_data(devlist->cur_sb[devlist->level])); 3315 if (str == NULL) 3316 err(1, "%s can't allocate %zd bytes for string", __func__, 3317 sbuf_len(devlist->cur_sb[devlist->level])); 3318 3319 if (strlen(str) == 0) { 3320 free(str); 3321 str = NULL; 3322 } 3323 3324 sbuf_delete(devlist->cur_sb[devlist->level]); 3325 devlist->cur_sb[devlist->level] = NULL; 3326 devlist->level--; 3327 3328 if (strcmp(name, "backend_type") == 0) { 3329 cur_lun->backend_type = str; 3330 str = NULL; 3331 } else if (strcmp(name, "size") == 0) { 3332 cur_lun->size_blocks = strtoull(str, NULL, 0); 3333 } else if (strcmp(name, "blocksize") == 0) { 3334 cur_lun->blocksize = strtoul(str, NULL, 0); 3335 } else if (strcmp(name, "serial_number") == 0) { 3336 cur_lun->serial_number = str; 3337 str = NULL; 3338 } else if (strcmp(name, "device_id") == 0) { 3339 cur_lun->device_id = str; 3340 str = NULL; 3341 } else if (strcmp(name, "lun") == 0) { 3342 devlist->cur_lun = NULL; 3343 } else if (strcmp(name, "ctllunlist") == 0) { 3344 /* Nothing. */ 3345 } else { 3346 struct cctl_lun_nv *nv; 3347 3348 nv = calloc(1, sizeof(*nv)); 3349 if (nv == NULL) 3350 err(1, "%s: can't allocate %zd bytes for nv pair", 3351 __func__, sizeof(*nv)); 3352 3353 nv->name = strdup(name); 3354 if (nv->name == NULL) 3355 err(1, "%s: can't allocated %zd bytes for string", 3356 __func__, strlen(name)); 3357 3358 nv->value = str; 3359 str = NULL; 3360 STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links); 3361 } 3362 3363 free(str); 3364 } 3365 3366 static void 3367 cctl_char_handler(void *user_data, const XML_Char *str, int len) 3368 { 3369 struct cctl_devlist_data *devlist; 3370 3371 devlist = (struct cctl_devlist_data *)user_data; 3372 3373 sbuf_bcat(devlist->cur_sb[devlist->level], str, len); 3374 } 3375 3376 static int 3377 cctl_devlist(int fd, int argc, char **argv, char *combinedopt) 3378 { 3379 struct ctl_lun_list list; 3380 struct cctl_devlist_data devlist; 3381 struct cctl_lun *lun; 3382 XML_Parser parser; 3383 char *lun_str; 3384 int lun_len; 3385 int dump_xml = 0; 3386 int retval, c; 3387 char *backend = NULL; 3388 int verbose = 0; 3389 3390 retval = 0; 3391 lun_len = 4096; 3392 3393 bzero(&devlist, sizeof(devlist)); 3394 STAILQ_INIT(&devlist.lun_list); 3395 3396 while ((c = getopt(argc, argv, combinedopt)) != -1) { 3397 switch (c) { 3398 case 'b': 3399 backend = strdup(optarg); 3400 break; 3401 case 'v': 3402 verbose++; 3403 break; 3404 case 'x': 3405 dump_xml = 1; 3406 break; 3407 default: 3408 break; 3409 } 3410 } 3411 3412 retry: 3413 lun_str = malloc(lun_len); 3414 3415 bzero(&list, sizeof(list)); 3416 list.alloc_len = lun_len; 3417 list.status = CTL_LUN_LIST_NONE; 3418 list.lun_xml = lun_str; 3419 3420 if (ioctl(fd, CTL_LUN_LIST, &list) == -1) { 3421 warn("%s: error issuing CTL_LUN_LIST ioctl", __func__); 3422 retval = 1; 3423 goto bailout; 3424 } 3425 3426 if (list.status == CTL_LUN_LIST_ERROR) { 3427 warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s", 3428 __func__, list.error_str); 3429 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 3430 lun_len = lun_len << 1; 3431 goto retry; 3432 } 3433 3434 if (dump_xml != 0) { 3435 printf("%s", lun_str); 3436 goto bailout; 3437 } 3438 3439 parser = XML_ParserCreate(NULL); 3440 if (parser == NULL) { 3441 warn("%s: Unable to create XML parser", __func__); 3442 retval = 1; 3443 goto bailout; 3444 } 3445 3446 XML_SetUserData(parser, &devlist); 3447 XML_SetElementHandler(parser, cctl_start_element, cctl_end_element); 3448 XML_SetCharacterDataHandler(parser, cctl_char_handler); 3449 3450 retval = XML_Parse(parser, lun_str, strlen(lun_str), 1); 3451 if (retval != 1) { 3452 warnx("%s: Unable to parse XML: Error %d", __func__, 3453 XML_GetErrorCode(parser)); 3454 XML_ParserFree(parser); 3455 retval = 1; 3456 goto bailout; 3457 } 3458 retval = 0; 3459 XML_ParserFree(parser); 3460 3461 printf("LUN Backend %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS", 3462 "Serial Number", "Device ID"); 3463 STAILQ_FOREACH(lun, &devlist.lun_list, links) { 3464 struct cctl_lun_nv *nv; 3465 3466 if ((backend != NULL) 3467 && (strcmp(lun->backend_type, backend) != 0)) 3468 continue; 3469 3470 printf("%3ju %-8s %18ju %4u %-16s %-16s\n", 3471 (uintmax_t)lun->lun_id, 3472 lun->backend_type, (uintmax_t)lun->size_blocks, 3473 lun->blocksize, lun->serial_number, lun->device_id); 3474 3475 if (verbose == 0) 3476 continue; 3477 3478 STAILQ_FOREACH(nv, &lun->attr_list, links) { 3479 printf(" %s=%s\n", nv->name, nv->value); 3480 } 3481 } 3482 bailout: 3483 free(lun_str); 3484 3485 return (retval); 3486 } 3487 3488 /* 3489 * Port information. 3490 */ 3491 struct cctl_port { 3492 uint64_t port_id; 3493 char *online; 3494 char *frontend_type; 3495 char *name; 3496 int pp, vp; 3497 char *target, *port, *lun_map; 3498 STAILQ_HEAD(,cctl_lun_nv) init_list; 3499 STAILQ_HEAD(,cctl_lun_nv) lun_list; 3500 STAILQ_HEAD(,cctl_lun_nv) attr_list; 3501 STAILQ_ENTRY(cctl_port) links; 3502 }; 3503 3504 struct cctl_portlist_data { 3505 int num_ports; 3506 STAILQ_HEAD(,cctl_port) port_list; 3507 struct cctl_port *cur_port; 3508 int level; 3509 uint64_t cur_id; 3510 struct sbuf *cur_sb[32]; 3511 }; 3512 3513 static void 3514 cctl_start_pelement(void *user_data, const char *name, const char **attr) 3515 { 3516 int i; 3517 struct cctl_portlist_data *portlist; 3518 struct cctl_port *cur_port; 3519 3520 portlist = (struct cctl_portlist_data *)user_data; 3521 cur_port = portlist->cur_port; 3522 portlist->level++; 3523 if ((u_int)portlist->level >= nitems(portlist->cur_sb)) 3524 errx(1, "%s: too many nesting levels, %zd max", __func__, 3525 nitems(portlist->cur_sb)); 3526 3527 portlist->cur_sb[portlist->level] = sbuf_new_auto(); 3528 if (portlist->cur_sb[portlist->level] == NULL) 3529 err(1, "%s: Unable to allocate sbuf", __func__); 3530 3531 portlist->cur_id = 0; 3532 for (i = 0; attr[i] != NULL; i += 2) { 3533 if (strcmp(attr[i], "id") == 0) { 3534 portlist->cur_id = strtoull(attr[i+1], NULL, 0); 3535 break; 3536 } 3537 } 3538 3539 if (strcmp(name, "targ_port") == 0) { 3540 if (cur_port != NULL) 3541 errx(1, "%s: improper port element nesting", __func__); 3542 3543 cur_port = calloc(1, sizeof(*cur_port)); 3544 if (cur_port == NULL) 3545 err(1, "%s: cannot allocate %zd bytes", __func__, 3546 sizeof(*cur_port)); 3547 3548 portlist->num_ports++; 3549 portlist->cur_port = cur_port; 3550 3551 STAILQ_INIT(&cur_port->init_list); 3552 STAILQ_INIT(&cur_port->lun_list); 3553 STAILQ_INIT(&cur_port->attr_list); 3554 cur_port->port_id = portlist->cur_id; 3555 STAILQ_INSERT_TAIL(&portlist->port_list, cur_port, links); 3556 } 3557 } 3558 3559 static void 3560 cctl_end_pelement(void *user_data, const char *name) 3561 { 3562 struct cctl_portlist_data *portlist; 3563 struct cctl_port *cur_port; 3564 char *str; 3565 3566 portlist = (struct cctl_portlist_data *)user_data; 3567 cur_port = portlist->cur_port; 3568 3569 if ((cur_port == NULL) 3570 && (strcmp(name, "ctlportlist") != 0)) 3571 errx(1, "%s: cur_port == NULL! (name = %s)", __func__, name); 3572 3573 if (portlist->cur_sb[portlist->level] == NULL) 3574 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 3575 portlist->level, name); 3576 3577 if (sbuf_finish(portlist->cur_sb[portlist->level]) != 0) 3578 err(1, "%s: sbuf_finish", __func__); 3579 str = strdup(sbuf_data(portlist->cur_sb[portlist->level])); 3580 if (str == NULL) 3581 err(1, "%s can't allocate %zd bytes for string", __func__, 3582 sbuf_len(portlist->cur_sb[portlist->level])); 3583 3584 if (strlen(str) == 0) { 3585 free(str); 3586 str = NULL; 3587 } 3588 3589 sbuf_delete(portlist->cur_sb[portlist->level]); 3590 portlist->cur_sb[portlist->level] = NULL; 3591 portlist->level--; 3592 3593 if (strcmp(name, "frontend_type") == 0) { 3594 cur_port->frontend_type = str; 3595 str = NULL; 3596 } else if (strcmp(name, "port_name") == 0) { 3597 cur_port->name = str; 3598 str = NULL; 3599 } else if (strcmp(name, "online") == 0) { 3600 cur_port->online = str; 3601 str = NULL; 3602 } else if (strcmp(name, "physical_port") == 0) { 3603 cur_port->pp = strtoull(str, NULL, 0); 3604 } else if (strcmp(name, "virtual_port") == 0) { 3605 cur_port->vp = strtoull(str, NULL, 0); 3606 } else if (strcmp(name, "target") == 0) { 3607 cur_port->target = str; 3608 str = NULL; 3609 } else if (strcmp(name, "port") == 0) { 3610 cur_port->port = str; 3611 str = NULL; 3612 } else if (strcmp(name, "lun_map") == 0) { 3613 cur_port->lun_map = str; 3614 str = NULL; 3615 } else if (strcmp(name, "targ_port") == 0) { 3616 portlist->cur_port = NULL; 3617 } else if (strcmp(name, "ctlportlist") == 0) { 3618 /* Nothing. */ 3619 } else { 3620 struct cctl_lun_nv *nv; 3621 3622 nv = calloc(1, sizeof(*nv)); 3623 if (nv == NULL) 3624 err(1, "%s: can't allocate %zd bytes for nv pair", 3625 __func__, sizeof(*nv)); 3626 3627 if (strcmp(name, "initiator") == 0 || 3628 strcmp(name, "lun") == 0) 3629 asprintf(&nv->name, "%ju", portlist->cur_id); 3630 else 3631 nv->name = strdup(name); 3632 if (nv->name == NULL) 3633 err(1, "%s: can't allocated %zd bytes for string", 3634 __func__, strlen(name)); 3635 3636 nv->value = str; 3637 str = NULL; 3638 if (strcmp(name, "initiator") == 0) 3639 STAILQ_INSERT_TAIL(&cur_port->init_list, nv, links); 3640 else if (strcmp(name, "lun") == 0) 3641 STAILQ_INSERT_TAIL(&cur_port->lun_list, nv, links); 3642 else 3643 STAILQ_INSERT_TAIL(&cur_port->attr_list, nv, links); 3644 } 3645 3646 free(str); 3647 } 3648 3649 static void 3650 cctl_char_phandler(void *user_data, const XML_Char *str, int len) 3651 { 3652 struct cctl_portlist_data *portlist; 3653 3654 portlist = (struct cctl_portlist_data *)user_data; 3655 3656 sbuf_bcat(portlist->cur_sb[portlist->level], str, len); 3657 } 3658 3659 static int 3660 cctl_portlist(int fd, int argc, char **argv, char *combinedopt) 3661 { 3662 struct ctl_lun_list list; 3663 struct cctl_portlist_data portlist; 3664 struct cctl_port *port; 3665 XML_Parser parser; 3666 char *port_str = NULL; 3667 int port_len; 3668 int dump_xml = 0; 3669 int retval, c; 3670 char *frontend = NULL; 3671 uint64_t portarg = UINT64_MAX; 3672 int verbose = 0, init = 0, lun = 0, quiet = 0; 3673 3674 retval = 0; 3675 port_len = 4096; 3676 3677 bzero(&portlist, sizeof(portlist)); 3678 STAILQ_INIT(&portlist.port_list); 3679 3680 while ((c = getopt(argc, argv, combinedopt)) != -1) { 3681 switch (c) { 3682 case 'f': 3683 frontend = strdup(optarg); 3684 break; 3685 case 'i': 3686 init++; 3687 break; 3688 case 'l': 3689 lun++; 3690 break; 3691 case 'p': 3692 portarg = strtoll(optarg, NULL, 0); 3693 break; 3694 case 'q': 3695 quiet++; 3696 break; 3697 case 'v': 3698 verbose++; 3699 break; 3700 case 'x': 3701 dump_xml = 1; 3702 break; 3703 default: 3704 break; 3705 } 3706 } 3707 3708 retry: 3709 port_str = (char *)realloc(port_str, port_len); 3710 3711 bzero(&list, sizeof(list)); 3712 list.alloc_len = port_len; 3713 list.status = CTL_LUN_LIST_NONE; 3714 list.lun_xml = port_str; 3715 3716 if (ioctl(fd, CTL_PORT_LIST, &list) == -1) { 3717 warn("%s: error issuing CTL_PORT_LIST ioctl", __func__); 3718 retval = 1; 3719 goto bailout; 3720 } 3721 3722 if (list.status == CTL_LUN_LIST_ERROR) { 3723 warnx("%s: error returned from CTL_PORT_LIST ioctl:\n%s", 3724 __func__, list.error_str); 3725 } else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) { 3726 port_len = port_len << 1; 3727 goto retry; 3728 } 3729 3730 if (dump_xml != 0) { 3731 printf("%s", port_str); 3732 goto bailout; 3733 } 3734 3735 parser = XML_ParserCreate(NULL); 3736 if (parser == NULL) { 3737 warn("%s: Unable to create XML parser", __func__); 3738 retval = 1; 3739 goto bailout; 3740 } 3741 3742 XML_SetUserData(parser, &portlist); 3743 XML_SetElementHandler(parser, cctl_start_pelement, cctl_end_pelement); 3744 XML_SetCharacterDataHandler(parser, cctl_char_phandler); 3745 3746 retval = XML_Parse(parser, port_str, strlen(port_str), 1); 3747 if (retval != 1) { 3748 warnx("%s: Unable to parse XML: Error %d", __func__, 3749 XML_GetErrorCode(parser)); 3750 XML_ParserFree(parser); 3751 retval = 1; 3752 goto bailout; 3753 } 3754 retval = 0; 3755 XML_ParserFree(parser); 3756 3757 if (quiet == 0) 3758 printf("Port Online Frontend Name pp vp\n"); 3759 STAILQ_FOREACH(port, &portlist.port_list, links) { 3760 struct cctl_lun_nv *nv; 3761 3762 if ((frontend != NULL) 3763 && (strcmp(port->frontend_type, frontend) != 0)) 3764 continue; 3765 3766 if ((portarg != UINT64_MAX) && (portarg != port->port_id)) 3767 continue; 3768 3769 printf("%-4ju %-6s %-8s %-8s %-2d %-2d %s\n", 3770 (uintmax_t)port->port_id, port->online, 3771 port->frontend_type, port->name, port->pp, port->vp, 3772 port->port ? port->port : ""); 3773 3774 if (init || verbose) { 3775 if (port->target) 3776 printf(" Target: %s\n", port->target); 3777 STAILQ_FOREACH(nv, &port->init_list, links) { 3778 printf(" Initiator %s: %s\n", 3779 nv->name, nv->value); 3780 } 3781 } 3782 3783 if (lun || verbose) { 3784 if (port->lun_map) { 3785 STAILQ_FOREACH(nv, &port->lun_list, links) 3786 printf(" LUN %s: %s\n", 3787 nv->name, nv->value); 3788 if (STAILQ_EMPTY(&port->lun_list)) 3789 printf(" No LUNs mapped\n"); 3790 } else 3791 printf(" All LUNs mapped\n"); 3792 } 3793 3794 if (verbose) { 3795 STAILQ_FOREACH(nv, &port->attr_list, links) { 3796 printf(" %s=%s\n", nv->name, nv->value); 3797 } 3798 } 3799 } 3800 bailout: 3801 free(port_str); 3802 3803 return (retval); 3804 } 3805 3806 static int 3807 cctl_lunmap(int fd, int argc, char **argv, char *combinedopt) 3808 { 3809 struct ctl_lun_map lm; 3810 int retval = 0, c; 3811 3812 retval = 0; 3813 lm.port = UINT32_MAX; 3814 lm.plun = UINT32_MAX; 3815 lm.lun = UINT32_MAX; 3816 3817 while ((c = getopt(argc, argv, combinedopt)) != -1) { 3818 switch (c) { 3819 case 'p': 3820 lm.port = strtoll(optarg, NULL, 0); 3821 break; 3822 case 'l': 3823 lm.plun = strtoll(optarg, NULL, 0); 3824 break; 3825 case 'L': 3826 lm.lun = strtoll(optarg, NULL, 0); 3827 break; 3828 default: 3829 break; 3830 } 3831 } 3832 3833 if (ioctl(fd, CTL_LUN_MAP, &lm) == -1) { 3834 warn("%s: error issuing CTL_LUN_MAP ioctl", __func__); 3835 retval = 1; 3836 } 3837 3838 return (retval); 3839 } 3840 3841 struct cctl_nvlist_conn { 3842 int connection_id; 3843 char *hostnqn; 3844 char *subnqn; 3845 int trtype; 3846 STAILQ_ENTRY(cctl_nvlist_conn) links; 3847 }; 3848 3849 struct cctl_nvlist_data { 3850 int num_conns; 3851 STAILQ_HEAD(,cctl_nvlist_conn) conn_list; 3852 struct cctl_nvlist_conn *cur_conn; 3853 u_int level; 3854 struct sbuf *cur_sb[32]; 3855 }; 3856 3857 static void 3858 cctl_nvlist_start_element(void *user_data, const char *name, const char **attr) 3859 { 3860 int i; 3861 struct cctl_nvlist_data *nvlist; 3862 struct cctl_nvlist_conn *cur_conn; 3863 3864 nvlist = (struct cctl_nvlist_data *)user_data; 3865 cur_conn = nvlist->cur_conn; 3866 nvlist->level++; 3867 if ((u_int)nvlist->level >= nitems(nvlist->cur_sb)) 3868 errx(1, "%s: too many nesting levels, %zd max", __func__, 3869 nitems(nvlist->cur_sb)); 3870 3871 nvlist->cur_sb[nvlist->level] = sbuf_new_auto(); 3872 if (nvlist->cur_sb[nvlist->level] == NULL) 3873 err(1, "%s: Unable to allocate sbuf", __func__); 3874 3875 if (strcmp(name, "connection") == 0) { 3876 if (cur_conn != NULL) 3877 errx(1, "%s: improper connection element nesting", 3878 __func__); 3879 3880 cur_conn = calloc(1, sizeof(*cur_conn)); 3881 if (cur_conn == NULL) 3882 err(1, "%s: cannot allocate %zd bytes", __func__, 3883 sizeof(*cur_conn)); 3884 3885 nvlist->num_conns++; 3886 nvlist->cur_conn = cur_conn; 3887 3888 STAILQ_INSERT_TAIL(&nvlist->conn_list, cur_conn, links); 3889 3890 for (i = 0; attr[i] != NULL; i += 2) { 3891 if (strcmp(attr[i], "id") == 0) { 3892 cur_conn->connection_id = 3893 strtoull(attr[i+1], NULL, 0); 3894 } else { 3895 errx(1, 3896 "%s: invalid connection attribute %s = %s", 3897 __func__, attr[i], attr[i+1]); 3898 } 3899 } 3900 } 3901 } 3902 3903 static void 3904 cctl_nvlist_end_element(void *user_data, const char *name) 3905 { 3906 struct cctl_nvlist_data *nvlist; 3907 struct cctl_nvlist_conn *cur_conn; 3908 char *str; 3909 3910 nvlist = (struct cctl_nvlist_data *)user_data; 3911 cur_conn = nvlist->cur_conn; 3912 3913 if ((cur_conn == NULL) && (strcmp(name, "ctlnvmflist") != 0)) 3914 errx(1, "%s: cur_conn == NULL! (name = %s)", __func__, name); 3915 3916 if (nvlist->cur_sb[nvlist->level] == NULL) 3917 errx(1, "%s: no valid sbuf at level %d (name %s)", __func__, 3918 nvlist->level, name); 3919 3920 sbuf_finish(nvlist->cur_sb[nvlist->level]); 3921 str = strdup(sbuf_data(nvlist->cur_sb[nvlist->level])); 3922 if (str == NULL) 3923 err(1, "%s can't allocate %zd bytes for string", __func__, 3924 sbuf_len(nvlist->cur_sb[nvlist->level])); 3925 3926 sbuf_delete(nvlist->cur_sb[nvlist->level]); 3927 nvlist->cur_sb[nvlist->level] = NULL; 3928 nvlist->level--; 3929 3930 if (strcmp(name, "hostnqn") == 0) { 3931 cur_conn->hostnqn = str; 3932 str = NULL; 3933 } else if (strcmp(name, "subnqn") == 0) { 3934 cur_conn->subnqn = str; 3935 str = NULL; 3936 } else if (strcmp(name, "trtype") == 0) { 3937 cur_conn->trtype = atoi(str); 3938 } else if (strcmp(name, "connection") == 0) { 3939 nvlist->cur_conn = NULL; 3940 } else if (strcmp(name, "ctlnvmflist") == 0) { 3941 /* Nothing. */ 3942 } else { 3943 /* 3944 * Unknown element; ignore it for forward compatibility. 3945 */ 3946 } 3947 3948 free(str); 3949 } 3950 3951 static void 3952 cctl_nvlist_char_handler(void *user_data, const XML_Char *str, int len) 3953 { 3954 struct cctl_nvlist_data *nvlist; 3955 3956 nvlist = (struct cctl_nvlist_data *)user_data; 3957 3958 sbuf_bcat(nvlist->cur_sb[nvlist->level], str, len); 3959 } 3960 3961 static const char * 3962 nvmf_transport_descr(u_int trtype) 3963 { 3964 static char buf[16]; 3965 3966 switch (trtype) { 3967 case NVMF_TRTYPE_RDMA: 3968 return ("RDMA"); 3969 case NVMF_TRTYPE_FC: 3970 return ("Fibre Channel"); 3971 case NVMF_TRTYPE_TCP: 3972 return ("TCP"); 3973 default: 3974 snprintf(buf, sizeof(buf), "%#x", trtype); 3975 return (buf); 3976 } 3977 } 3978 3979 static int 3980 cctl_nvlist(int fd, int argc, char **argv, char *combinedopt) 3981 { 3982 struct ctl_nvmf req; 3983 struct cctl_nvlist_data nvlist; 3984 struct cctl_nvlist_conn *conn; 3985 XML_Parser parser; 3986 char *conn_str; 3987 int conn_len; 3988 int dump_xml = 0; 3989 int c, retval, verbose = 0; 3990 3991 retval = 0; 3992 conn_len = 4096; 3993 3994 bzero(&nvlist, sizeof(nvlist)); 3995 STAILQ_INIT(&nvlist.conn_list); 3996 3997 while ((c = getopt(argc, argv, combinedopt)) != -1) { 3998 switch (c) { 3999 case 'v': 4000 verbose = 1; 4001 break; 4002 case 'x': 4003 dump_xml = 1; 4004 break; 4005 default: 4006 break; 4007 } 4008 } 4009 4010 retry: 4011 conn_str = malloc(conn_len); 4012 4013 bzero(&req, sizeof(req)); 4014 req.type = CTL_NVMF_LIST; 4015 req.data.list.alloc_len = conn_len; 4016 req.data.list.conn_xml = conn_str; 4017 4018 if (ioctl(fd, CTL_NVMF, &req) == -1) { 4019 warn("%s: error issuing CTL_NVMF ioctl", __func__); 4020 retval = 1; 4021 goto bailout; 4022 } 4023 4024 if (req.status == CTL_NVMF_ERROR) { 4025 warnx("%s: error returned from CTL_NVMF ioctl:\n%s", 4026 __func__, req.error_str); 4027 } else if (req.status == CTL_NVMF_LIST_NEED_MORE_SPACE) { 4028 conn_len = conn_len << 1; 4029 goto retry; 4030 } 4031 4032 if (dump_xml != 0) { 4033 printf("%s", conn_str); 4034 goto bailout; 4035 } 4036 4037 parser = XML_ParserCreate(NULL); 4038 if (parser == NULL) { 4039 warn("%s: Unable to create XML parser", __func__); 4040 retval = 1; 4041 goto bailout; 4042 } 4043 4044 XML_SetUserData(parser, &nvlist); 4045 XML_SetElementHandler(parser, cctl_nvlist_start_element, 4046 cctl_nvlist_end_element); 4047 XML_SetCharacterDataHandler(parser, cctl_nvlist_char_handler); 4048 4049 retval = XML_Parse(parser, conn_str, strlen(conn_str), 1); 4050 if (retval != 1) { 4051 warnx("%s: Unable to parse XML: Error %d", __func__, 4052 XML_GetErrorCode(parser)); 4053 XML_ParserFree(parser); 4054 retval = 1; 4055 goto bailout; 4056 } 4057 retval = 0; 4058 XML_ParserFree(parser); 4059 4060 if (verbose != 0) { 4061 STAILQ_FOREACH(conn, &nvlist.conn_list, links) { 4062 printf("%-25s %d\n", "Controller ID:", conn->connection_id); 4063 printf("%-25s %s\n", "Host NQN:", conn->hostnqn); 4064 printf("%-25s %s\n", "Subsystem NQN:", conn->subnqn); 4065 printf("%-25s %s\n", "Transport:", 4066 nvmf_transport_descr(conn->trtype)); 4067 printf("\n"); 4068 } 4069 } else { 4070 printf("%4s %-16s %-36s %-36s\n", "ID", "Transport", "HostNQN", 4071 "SubNQN"); 4072 STAILQ_FOREACH(conn, &nvlist.conn_list, links) { 4073 printf("%4u %-16s %-36s %-36s\n", 4074 conn->connection_id, 4075 nvmf_transport_descr(conn->trtype), 4076 conn->hostnqn, conn->subnqn); 4077 } 4078 } 4079 bailout: 4080 free(conn_str); 4081 4082 return (retval); 4083 } 4084 4085 static int 4086 cctl_nvterminate(int fd, int argc, char **argv, char *combinedopt) 4087 { 4088 struct ctl_nvmf req; 4089 int retval = 0, c; 4090 int all = 0, cntlid = -1, nargs = 0; 4091 char *hostnqn = NULL; 4092 4093 while ((c = getopt(argc, argv, combinedopt)) != -1) { 4094 switch (c) { 4095 case 'a': 4096 all = 1; 4097 nargs++; 4098 break; 4099 case 'c': 4100 cntlid = strtoul(optarg, NULL, 0); 4101 nargs++; 4102 break; 4103 case 'h': 4104 hostnqn = strdup(optarg); 4105 if (hostnqn == NULL) 4106 err(1, "%s: strdup", __func__); 4107 nargs++; 4108 break; 4109 default: 4110 break; 4111 } 4112 } 4113 4114 if (nargs == 0) 4115 errx(1, "%s: either -a, -c, or -h must be specified", 4116 __func__); 4117 if (nargs > 1) 4118 errx(1, "%s: only one of -a, -c, or -h may be specified", 4119 __func__); 4120 4121 bzero(&req, sizeof(req)); 4122 req.type = CTL_NVMF_TERMINATE; 4123 req.data.terminate.cntlid = cntlid; 4124 if (hostnqn != NULL) 4125 strlcpy(req.data.terminate.hostnqn, 4126 hostnqn, sizeof(req.data.terminate.hostnqn)); 4127 if (all != 0) 4128 req.data.terminate.all = 1; 4129 4130 if (ioctl(fd, CTL_NVMF, &req) == -1) { 4131 warn("%s: error issuing CTL_NVMF ioctl", __func__); 4132 retval = 1; 4133 goto bailout; 4134 } 4135 4136 if (req.status != CTL_NVMF_OK) { 4137 warnx("%s: error returned from CTL NVMeoF connection " 4138 "termination request:\n%s", __func__, req.error_str); 4139 retval = 1; 4140 goto bailout; 4141 } 4142 4143 printf("NVMeoF connections terminated\n"); 4144 4145 bailout: 4146 return (retval); 4147 } 4148 4149 void 4150 usage(int error) 4151 { 4152 fprintf(error ? stderr : stdout, 4153 "Usage:\n" 4154 "Primary commands:\n" 4155 " ctladm tur [dev_id][general options]\n" 4156 " ctladm inquiry [dev_id][general options]\n" 4157 " ctladm devid [dev_id][general options]\n" 4158 " ctladm reqsense [dev_id][general options]\n" 4159 " ctladm reportluns [dev_id][general options]\n" 4160 " ctladm read [dev_id][general options] <-l lba> <-d len>\n" 4161 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n" 4162 " ctladm write [dev_id][general options] <-l lba> <-d len>\n" 4163 " <-f file|-> <-b blocksize> [-c cdbsize][-N]\n" 4164 " ctladm readcap [dev_id][general options] [-c cdbsize]\n" 4165 " ctladm modesense [dev_id][general options] <-m page|-l> [-P pc]\n" 4166 " [-d] [-S subpage] [-c cdbsize]\n" 4167 " ctladm prin [dev_id][general options] <-a action>\n" 4168 " ctladm prout [dev_id][general options] <-a action>\n" 4169 " <-r restype] [-k key] [-s sa_key]\n" 4170 " ctladm rtpg [dev_id][general options]\n" 4171 " ctladm start [dev_id][general options] [-i] [-o]\n" 4172 " ctladm stop [dev_id][general options] [-i] [-o]\n" 4173 " ctladm synccache [dev_id][general options] [-l lba]\n" 4174 " [-b blockcount] [-r] [-i] [-c cdbsize]\n" 4175 " ctladm create <-b backend> [-B blocksize] [-d device_id]\n" 4176 " [-l lun_id] [-o name=value] [-s size_bytes]\n" 4177 " [-S serial_num] [-t dev_type]\n" 4178 " ctladm remove <-b backend> <-l lun_id> [-o name=value]\n" 4179 " ctladm modify <-b backend> <-l lun_id> <-s size_bytes>\n" 4180 " ctladm devlist [-b backend] [-v] [-x]\n" 4181 " ctladm lunlist\n" 4182 " ctladm lunmap -p targ_port [-l pLUN] [-L cLUN]\n" 4183 " ctladm delay [dev_id] <-l datamove|done> [-T oneshot|cont]\n" 4184 " [-t secs]\n" 4185 " ctladm inject [dev_id] <-i action> <-p pattern> [-r lba,len]\n" 4186 " [-s len fmt [args]] [-c] [-d delete_id]\n" 4187 " ctladm port <-o <on|off> | [-w wwnn][-W wwpn]>\n" 4188 " [-p targ_port] [-t port_type]\n" 4189 " <-c> [-d driver] [-O name=value]\n" 4190 " <-r> <-p targ_port>\n" 4191 " ctladm portlist [-f frontend] [-i] [-p targ_port] [-q] [-v] [-x]\n" 4192 " ctladm islist [-v | -x]\n" 4193 " ctladm islogout <-a | -c connection-id | -i name | -p portal>\n" 4194 " ctladm isterminate <-a | -c connection-id | -i name | -p portal>\n" 4195 " ctladm nvlist [-v | -x]\n" 4196 " ctladm nvterminate <-a | -c controller-id | -h name>\n" 4197 " ctladm dumpooa\n" 4198 " ctladm dumpstructs\n" 4199 " ctladm help\n" 4200 "General Options:\n" 4201 "-I initiator_id : defaults to 7, used to change the initiator id\n" 4202 "-C retries : specify the number of times to retry this command\n" 4203 "-D devicename : specify the device to operate on\n" 4204 " : (default is %s)\n" 4205 "read/write options:\n" 4206 "-l lba : logical block address\n" 4207 "-d len : read/write length, in blocks\n" 4208 "-f file|- : write/read data to/from file or stdout/stdin\n" 4209 "-b blocksize : block size, in bytes\n" 4210 "-c cdbsize : specify minimum cdb size: 6, 10, 12 or 16\n" 4211 "-N : do not copy data to/from userland\n" 4212 "readcapacity options:\n" 4213 "-c cdbsize : specify minimum cdb size: 10 or 16\n" 4214 "modesense options:\n" 4215 "-m page : specify the mode page to view\n" 4216 "-l : request a list of supported pages\n" 4217 "-P pc : specify the page control value: 0-3 (current,\n" 4218 " changeable, default, saved, respectively)\n" 4219 "-d : disable block descriptors for mode sense\n" 4220 "-S subpage : specify a subpage\n" 4221 "-c cdbsize : specify minimum cdb size: 6 or 10\n" 4222 "persistent reserve in options:\n" 4223 "-a action : specify the action value: 0-2 (read key, read\n" 4224 " reservation, read capabilities, respectively)\n" 4225 "persistent reserve out options:\n" 4226 "-a action : specify the action value: 0-5 (register, reserve,\n" 4227 " release, clear, preempt, register and ignore)\n" 4228 "-k key : key value\n" 4229 "-s sa_key : service action value\n" 4230 "-r restype : specify the reservation type: 0-5(wr ex, ex ac,\n" 4231 " wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n" 4232 "start/stop options:\n" 4233 "-i : set the immediate bit (CTL does not support this)\n" 4234 "-o : set the on/offline bit\n" 4235 "synccache options:\n" 4236 "-l lba : set the starting LBA\n" 4237 "-b blockcount : set the length to sync in blocks\n" 4238 "-r : set the relative addressing bit\n" 4239 "-i : set the immediate bit\n" 4240 "-c cdbsize : specify minimum cdb size: 10 or 16\n" 4241 "create options:\n" 4242 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n" 4243 "-B blocksize : LUN blocksize in bytes (some backends)\n" 4244 "-d device_id : SCSI VPD page 0x83 ID\n" 4245 "-l lun_id : requested LUN number\n" 4246 "-o name=value : backend-specific options, multiple allowed\n" 4247 "-s size_bytes : LUN size in bytes (some backends)\n" 4248 "-S serial_num : SCSI VPD page 0x80 serial number\n" 4249 "-t dev_type : SCSI device type (0=disk, 3=processor)\n" 4250 "remove options:\n" 4251 "-b backend : backend name (\"block\", \"ramdisk\", etc.)\n" 4252 "-l lun_id : LUN number to delete\n" 4253 "-o name=value : backend-specific options, multiple allowed\n" 4254 "devlist options:\n" 4255 "-b backend : list devices from specified backend only\n" 4256 "-v : be verbose, show backend attributes\n" 4257 "-x : dump raw XML\n" 4258 "delay options:\n" 4259 "-l datamove|done : delay command at datamove or done phase\n" 4260 "-T oneshot : delay one command, then resume normal completion\n" 4261 "-T cont : delay all commands\n" 4262 "-t secs : number of seconds to delay\n" 4263 "inject options:\n" 4264 "-i error_action : action to perform\n" 4265 "-p pattern : command pattern to look for\n" 4266 "-r lba,len : LBA range for pattern\n" 4267 "-s len fmt [args] : sense data for custom sense action\n" 4268 "-c : continuous operation\n" 4269 "-d delete_id : error id to delete\n" 4270 "port options:\n" 4271 "-c : create new ioctl or iscsi frontend port\n" 4272 "-d : specify ioctl or iscsi frontend type\n" 4273 "-l : list frontend ports\n" 4274 "-o on|off : turn frontend ports on or off\n" 4275 "-O pp|vp : create new frontend port using pp and/or vp\n" 4276 "-w wwnn : set WWNN for one frontend\n" 4277 "-W wwpn : set WWPN for one frontend\n" 4278 "-t port_type : specify fc, scsi, ioctl, internal frontend type\n" 4279 "-p targ_port : specify target port number\n" 4280 "-r : remove frontend port\n" 4281 "-q : omit header in list output\n" 4282 "-x : output port list in XML format\n" 4283 "portlist options:\n" 4284 "-f frontend : specify frontend type\n" 4285 "-i : report target and initiators addresses\n" 4286 "-l : report LUN mapping\n" 4287 "-p targ_port : specify target port number\n" 4288 "-q : omit header in list output\n" 4289 "-v : verbose output (report all port options)\n" 4290 "-x : output port list in XML format\n" 4291 "lunmap options:\n" 4292 "-p targ_port : specify target port number\n" 4293 "-l pLUN : specify port-visible LUN\n" 4294 "-L cLUN : specify CTL LUN\n", 4295 CTL_DEFAULT_DEV); 4296 } 4297 4298 int 4299 main(int argc, char **argv) 4300 { 4301 int c; 4302 ctladm_cmdfunction command; 4303 ctladm_cmdargs cmdargs; 4304 ctladm_optret optreturn; 4305 char *device; 4306 const char *mainopt = "C:D:I:"; 4307 const char *subopt = NULL; 4308 char combinedopt[256]; 4309 int lun; 4310 int optstart = 2; 4311 int retval, fd; 4312 int retries; 4313 int initid; 4314 int saved_errno; 4315 4316 retval = 0; 4317 cmdargs = CTLADM_ARG_NONE; 4318 command = CTLADM_CMD_HELP; 4319 device = NULL; 4320 fd = -1; 4321 retries = 0; 4322 lun = 0; 4323 initid = 7; 4324 4325 if (argc < 2) { 4326 usage(1); 4327 retval = 1; 4328 goto bailout; 4329 } 4330 4331 /* 4332 * Get the base option. 4333 */ 4334 optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt); 4335 4336 if (optreturn == CC_OR_AMBIGUOUS) { 4337 warnx("ambiguous option %s", argv[1]); 4338 usage(0); 4339 exit(1); 4340 } else if (optreturn == CC_OR_NOT_FOUND) { 4341 warnx("option %s not found", argv[1]); 4342 usage(0); 4343 exit(1); 4344 } 4345 4346 if (cmdargs & CTLADM_ARG_NEED_TL) { 4347 if ((argc < 3) || (!isdigit(argv[2][0]))) { 4348 warnx("option %s requires a lun argument", 4349 argv[1]); 4350 usage(0); 4351 exit(1); 4352 } 4353 lun = strtol(argv[2], NULL, 0); 4354 4355 cmdargs |= CTLADM_ARG_TARG_LUN; 4356 optstart++; 4357 } 4358 4359 /* 4360 * Ahh, getopt(3) is a pain. 4361 * 4362 * This is a gross hack. There really aren't many other good 4363 * options (excuse the pun) for parsing options in a situation like 4364 * this. getopt is kinda braindead, so you end up having to run 4365 * through the options twice, and give each invocation of getopt 4366 * the option string for the other invocation. 4367 * 4368 * You would think that you could just have two groups of options. 4369 * The first group would get parsed by the first invocation of 4370 * getopt, and the second group would get parsed by the second 4371 * invocation of getopt. It doesn't quite work out that way. When 4372 * the first invocation of getopt finishes, it leaves optind pointing 4373 * to the argument _after_ the first argument in the second group. 4374 * So when the second invocation of getopt comes around, it doesn't 4375 * recognize the first argument it gets and then bails out. 4376 * 4377 * A nice alternative would be to have a flag for getopt that says 4378 * "just keep parsing arguments even when you encounter an unknown 4379 * argument", but there isn't one. So there's no real clean way to 4380 * easily parse two sets of arguments without having one invocation 4381 * of getopt know about the other. 4382 * 4383 * Without this hack, the first invocation of getopt would work as 4384 * long as the generic arguments are first, but the second invocation 4385 * (in the subfunction) would fail in one of two ways. In the case 4386 * where you don't set optreset, it would fail because optind may be 4387 * pointing to the argument after the one it should be pointing at. 4388 * In the case where you do set optreset, and reset optind, it would 4389 * fail because getopt would run into the first set of options, which 4390 * it doesn't understand. 4391 * 4392 * All of this would "sort of" work if you could somehow figure out 4393 * whether optind had been incremented one option too far. The 4394 * mechanics of that, however, are more daunting than just giving 4395 * both invocations all of the expect options for either invocation. 4396 * 4397 * Needless to say, I wouldn't mind if someone invented a better 4398 * (non-GPL!) command line parsing interface than getopt. I 4399 * wouldn't mind if someone added more knobs to getopt to make it 4400 * work better. Who knows, I may talk myself into doing it someday, 4401 * if the standards weenies let me. As it is, it just leads to 4402 * hackery like this and causes people to avoid it in some cases. 4403 * 4404 * KDM, September 8th, 1998 4405 */ 4406 if (subopt != NULL) 4407 sprintf(combinedopt, "%s%s", mainopt, subopt); 4408 else 4409 sprintf(combinedopt, "%s", mainopt); 4410 4411 /* 4412 * Start getopt processing at argv[2/3], since we've already 4413 * accepted argv[1..2] as the command name, and as a possible 4414 * device name. 4415 */ 4416 optind = optstart; 4417 4418 /* 4419 * Now we run through the argument list looking for generic 4420 * options, and ignoring options that possibly belong to 4421 * subfunctions. 4422 */ 4423 while ((c = getopt(argc, argv, combinedopt))!= -1){ 4424 switch (c) { 4425 case 'C': 4426 cmdargs |= CTLADM_ARG_RETRIES; 4427 retries = strtol(optarg, NULL, 0); 4428 break; 4429 case 'D': 4430 device = strdup(optarg); 4431 cmdargs |= CTLADM_ARG_DEVICE; 4432 break; 4433 case 'I': 4434 cmdargs |= CTLADM_ARG_INITIATOR; 4435 initid = strtol(optarg, NULL, 0); 4436 break; 4437 default: 4438 break; 4439 } 4440 } 4441 4442 if ((cmdargs & CTLADM_ARG_INITIATOR) == 0) 4443 initid = 7; 4444 4445 optind = optstart; 4446 optreset = 1; 4447 4448 /* 4449 * Default to opening the CTL device for now. 4450 */ 4451 if (((cmdargs & CTLADM_ARG_DEVICE) == 0) 4452 && (command != CTLADM_CMD_HELP)) { 4453 device = strdup(CTL_DEFAULT_DEV); 4454 cmdargs |= CTLADM_ARG_DEVICE; 4455 } 4456 4457 if ((cmdargs & CTLADM_ARG_DEVICE) 4458 && (command != CTLADM_CMD_HELP)) { 4459 fd = open(device, O_RDWR); 4460 if (fd == -1 && errno == ENOENT) { 4461 saved_errno = errno; 4462 retval = kldload("ctl"); 4463 if (retval != -1) 4464 fd = open(device, O_RDWR); 4465 else 4466 errno = saved_errno; 4467 } 4468 if (fd == -1) { 4469 fprintf(stderr, "%s: error opening %s: %s\n", 4470 argv[0], device, strerror(errno)); 4471 retval = 1; 4472 goto bailout; 4473 } 4474 #ifdef WANT_ISCSI 4475 switch (command) { 4476 case CTLADM_CMD_ISLIST: 4477 case CTLADM_CMD_ISLOGOUT: 4478 case CTLADM_CMD_ISTERMINATE: 4479 if (modfind("cfiscsi") == -1 && 4480 kldload("cfiscsi") == -1) 4481 warn("couldn't load cfiscsi"); 4482 break; 4483 default: 4484 break; 4485 } 4486 #endif 4487 } else if ((command != CTLADM_CMD_HELP) 4488 && ((cmdargs & CTLADM_ARG_DEVICE) == 0)) { 4489 fprintf(stderr, "%s: you must specify a device with the " 4490 "--device argument for this command\n", argv[0]); 4491 command = CTLADM_CMD_HELP; 4492 retval = 1; 4493 } 4494 4495 switch (command) { 4496 case CTLADM_CMD_TUR: 4497 retval = cctl_tur(fd, lun, initid, retries); 4498 break; 4499 case CTLADM_CMD_INQUIRY: 4500 retval = cctl_inquiry(fd, lun, initid, retries); 4501 break; 4502 case CTLADM_CMD_REQ_SENSE: 4503 retval = cctl_req_sense(fd, lun, initid, retries); 4504 break; 4505 case CTLADM_CMD_REPORT_LUNS: 4506 retval = cctl_report_luns(fd, lun, initid, retries); 4507 break; 4508 case CTLADM_CMD_CREATE: 4509 retval = cctl_create_lun(fd, argc, argv, combinedopt); 4510 break; 4511 case CTLADM_CMD_RM: 4512 retval = cctl_rm_lun(fd, argc, argv, combinedopt); 4513 break; 4514 case CTLADM_CMD_DEVLIST: 4515 retval = cctl_devlist(fd, argc, argv, combinedopt); 4516 break; 4517 case CTLADM_CMD_READ: 4518 case CTLADM_CMD_WRITE: 4519 retval = cctl_read_write(fd, lun, initid, retries, 4520 argc, argv, combinedopt, command); 4521 break; 4522 case CTLADM_CMD_PORT: 4523 retval = cctl_port(fd, argc, argv, combinedopt); 4524 break; 4525 case CTLADM_CMD_PORTLIST: 4526 retval = cctl_portlist(fd, argc, argv, combinedopt); 4527 break; 4528 case CTLADM_CMD_LUNMAP: 4529 retval = cctl_lunmap(fd, argc, argv, combinedopt); 4530 break; 4531 case CTLADM_CMD_READCAPACITY: 4532 retval = cctl_read_capacity(fd, lun, initid, retries, 4533 argc, argv, combinedopt); 4534 break; 4535 case CTLADM_CMD_MODESENSE: 4536 retval = cctl_mode_sense(fd, lun, initid, retries, 4537 argc, argv, combinedopt); 4538 break; 4539 case CTLADM_CMD_START: 4540 case CTLADM_CMD_STOP: 4541 retval = cctl_start_stop(fd, lun, initid, retries, 4542 (command == CTLADM_CMD_START) ? 1 : 0, 4543 argc, argv, combinedopt); 4544 break; 4545 case CTLADM_CMD_SYNC_CACHE: 4546 retval = cctl_sync_cache(fd, lun, initid, retries, 4547 argc, argv, combinedopt); 4548 break; 4549 case CTLADM_CMD_LUNLIST: 4550 retval = cctl_lunlist(fd); 4551 break; 4552 case CTLADM_CMD_DELAY: 4553 retval = cctl_delay(fd, lun, argc, argv, combinedopt); 4554 break; 4555 case CTLADM_CMD_ERR_INJECT: 4556 retval = cctl_error_inject(fd, lun, argc, argv, 4557 combinedopt); 4558 break; 4559 case CTLADM_CMD_DUMPOOA: 4560 retval = cctl_dump_ooa(fd, argc, argv); 4561 break; 4562 case CTLADM_CMD_DUMPSTRUCTS: 4563 retval = cctl_dump_structs(fd, cmdargs); 4564 break; 4565 case CTLADM_CMD_PRES_IN: 4566 retval = cctl_persistent_reserve_in(fd, lun, initid, 4567 argc, argv, combinedopt, 4568 retries); 4569 break; 4570 case CTLADM_CMD_PRES_OUT: 4571 retval = cctl_persistent_reserve_out(fd, lun, initid, 4572 argc, argv, combinedopt, 4573 retries); 4574 break; 4575 case CTLADM_CMD_INQ_VPD_DEVID: 4576 retval = cctl_inquiry_vpd_devid(fd, lun, initid); 4577 break; 4578 case CTLADM_CMD_RTPG: 4579 retval = cctl_report_target_port_group(fd, lun, initid); 4580 break; 4581 case CTLADM_CMD_MODIFY: 4582 retval = cctl_modify_lun(fd, argc, argv, combinedopt); 4583 break; 4584 case CTLADM_CMD_ISLIST: 4585 retval = cctl_islist(fd, argc, argv, combinedopt); 4586 break; 4587 case CTLADM_CMD_ISLOGOUT: 4588 retval = cctl_islogout(fd, argc, argv, combinedopt); 4589 break; 4590 case CTLADM_CMD_ISTERMINATE: 4591 retval = cctl_isterminate(fd, argc, argv, combinedopt); 4592 break; 4593 case CTLADM_CMD_NVLIST: 4594 retval = cctl_nvlist(fd, argc, argv, combinedopt); 4595 break; 4596 case CTLADM_CMD_NVTERMINATE: 4597 retval = cctl_nvterminate(fd, argc, argv, combinedopt); 4598 break; 4599 case CTLADM_CMD_HELP: 4600 default: 4601 usage(retval); 4602 break; 4603 } 4604 bailout: 4605 4606 if (fd != -1) 4607 close(fd); 4608 4609 exit (retval); 4610 } 4611 4612 /* 4613 * vim: ts=8 4614 */ 4615