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