1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) Intel Corporation. 3 * All rights reserved. 4 */ 5 6 #include "spdk/stdinc.h" 7 8 #include "spdk/bdev.h" 9 #include "spdk/env.h" 10 #include "spdk/fd.h" 11 #include "spdk/thread.h" 12 #include "spdk/json.h" 13 #include "spdk/util.h" 14 #include "spdk/rpc.h" 15 #include "spdk/string.h" 16 #include "spdk/iscsi_spec.h" 17 18 #include "spdk/log.h" 19 #include "spdk/bdev_module.h" 20 21 #include "iscsi/iscsi.h" 22 #include "iscsi/scsi-lowlevel.h" 23 24 #include "bdev_iscsi.h" 25 26 struct bdev_iscsi_lun; 27 28 #define BDEV_ISCSI_CONNECTION_POLL_US 500 /* 0.5 ms */ 29 #define BDEV_ISCSI_NO_MAIN_CH_POLL_US 10000 /* 10ms */ 30 31 #define DEFAULT_INITIATOR_NAME "iqn.2016-06.io.spdk:init" 32 33 /* MAXIMUM UNMAP LBA COUNT: 34 * indicates the maximum number of LBAs that may be unmapped 35 * by an UNMAP command. 36 */ 37 #define BDEV_ISCSI_DEFAULT_MAX_UNMAP_LBA_COUNT (32768) 38 39 /* MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT: 40 * indicates the maximum number of UNMAP block descriptors that 41 * shall be contained in the parameter data transferred to the 42 * device server for an UNMAP command. 43 */ 44 #define BDEV_ISCSI_MAX_UNMAP_BLOCK_DESCS_COUNT (1) 45 46 static int bdev_iscsi_initialize(void); 47 static void bdev_iscsi_readcapacity16(struct iscsi_context *context, struct bdev_iscsi_lun *lun); 48 static void _bdev_iscsi_submit_request(void *_bdev_io); 49 50 static TAILQ_HEAD(, bdev_iscsi_conn_req) g_iscsi_conn_req = TAILQ_HEAD_INITIALIZER( 51 g_iscsi_conn_req); 52 static struct spdk_poller *g_conn_poller = NULL; 53 54 struct bdev_iscsi_io { 55 struct spdk_thread *submit_td; 56 struct bdev_iscsi_lun *lun; 57 enum spdk_bdev_io_status status; 58 int scsi_status; 59 enum spdk_scsi_sense sk; 60 uint8_t asc; 61 uint8_t ascq; 62 }; 63 64 struct bdev_iscsi_lun { 65 struct spdk_bdev bdev; 66 struct iscsi_context *context; 67 char *initiator_iqn; 68 int lun_id; 69 char *url; 70 pthread_mutex_t mutex; 71 uint32_t ch_count; 72 struct spdk_thread *main_td; 73 struct spdk_poller *no_main_ch_poller; 74 struct spdk_thread *no_main_ch_poller_td; 75 bool unmap_supported; 76 uint32_t max_unmap; 77 struct spdk_poller *poller; 78 }; 79 80 struct bdev_iscsi_io_channel { 81 struct bdev_iscsi_lun *lun; 82 }; 83 84 struct bdev_iscsi_conn_req { 85 char *url; 86 char *bdev_name; 87 char *initiator_iqn; 88 struct iscsi_context *context; 89 spdk_bdev_iscsi_create_cb create_cb; 90 void *create_cb_arg; 91 bool unmap_supported; 92 uint32_t max_unmap; 93 int lun; 94 int status; 95 TAILQ_ENTRY(bdev_iscsi_conn_req) link; 96 }; 97 98 static void 99 complete_conn_req(struct bdev_iscsi_conn_req *req, struct spdk_bdev *bdev, 100 int status) 101 { 102 TAILQ_REMOVE(&g_iscsi_conn_req, req, link); 103 req->create_cb(req->create_cb_arg, bdev, status); 104 105 /* 106 * we are still running in the context of iscsi_service() 107 * so do not tear down its data structures here 108 */ 109 req->status = status; 110 } 111 112 static int 113 bdev_iscsi_get_ctx_size(void) 114 { 115 return sizeof(struct bdev_iscsi_io); 116 } 117 118 static void 119 _iscsi_free_lun(void *arg) 120 { 121 struct bdev_iscsi_lun *lun = arg; 122 123 assert(lun != NULL); 124 iscsi_destroy_context(lun->context); 125 pthread_mutex_destroy(&lun->mutex); 126 free(lun->bdev.name); 127 free(lun->url); 128 free(lun->initiator_iqn); 129 130 spdk_bdev_destruct_done(&lun->bdev, 0); 131 free(lun); 132 } 133 134 static void 135 _bdev_iscsi_conn_req_free(struct bdev_iscsi_conn_req *req) 136 { 137 free(req->initiator_iqn); 138 free(req->bdev_name); 139 free(req->url); 140 /* destroy will call iscsi_disconnect() implicitly if connected */ 141 iscsi_destroy_context(req->context); 142 free(req); 143 } 144 145 static void 146 bdev_iscsi_finish(void) 147 { 148 struct bdev_iscsi_conn_req *req, *tmp; 149 150 /* clear out pending connection requests here. We cannot 151 * simply set the state to a non SCSI_STATUS_GOOD state as 152 * the connection poller wont run anymore 153 */ 154 TAILQ_FOREACH_SAFE(req, &g_iscsi_conn_req, link, tmp) { 155 _bdev_iscsi_conn_req_free(req); 156 } 157 158 if (g_conn_poller) { 159 spdk_poller_unregister(&g_conn_poller); 160 } 161 } 162 163 static struct spdk_bdev_module g_iscsi_bdev_module = { 164 .name = "iscsi", 165 .module_init = bdev_iscsi_initialize, 166 .module_fini = bdev_iscsi_finish, 167 .get_ctx_size = bdev_iscsi_get_ctx_size, 168 }; 169 170 SPDK_BDEV_MODULE_REGISTER(iscsi, &g_iscsi_bdev_module); 171 172 static void 173 _bdev_iscsi_io_complete(void *_iscsi_io) 174 { 175 struct bdev_iscsi_io *iscsi_io = _iscsi_io; 176 177 if (iscsi_io->status == SPDK_BDEV_IO_STATUS_SUCCESS) { 178 spdk_bdev_io_complete_scsi_status(spdk_bdev_io_from_ctx(iscsi_io), iscsi_io->scsi_status, 179 iscsi_io->sk, iscsi_io->asc, iscsi_io->ascq); 180 } else { 181 spdk_bdev_io_complete(spdk_bdev_io_from_ctx(iscsi_io), iscsi_io->status); 182 } 183 } 184 185 static void 186 bdev_iscsi_io_complete(struct bdev_iscsi_io *iscsi_io, enum spdk_bdev_io_status status) 187 { 188 iscsi_io->status = status; 189 if (iscsi_io->submit_td != NULL) { 190 spdk_thread_send_msg(iscsi_io->submit_td, _bdev_iscsi_io_complete, iscsi_io); 191 } else { 192 _bdev_iscsi_io_complete(iscsi_io); 193 } 194 } 195 196 static bool 197 _bdev_iscsi_is_size_change(int status, struct scsi_task *task) 198 { 199 if (status == SPDK_SCSI_STATUS_CHECK_CONDITION && 200 (uint8_t)task->sense.key == SPDK_SCSI_SENSE_UNIT_ATTENTION && 201 task->sense.ascq == 0x2a09) { 202 /* ASCQ: SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED (0x2a09) */ 203 return true; 204 } 205 206 return false; 207 } 208 209 /* Common call back function for read/write/flush command */ 210 static void 211 bdev_iscsi_command_cb(struct iscsi_context *context, int status, void *_task, void *_iscsi_io) 212 { 213 struct scsi_task *task = _task; 214 struct bdev_iscsi_io *iscsi_io = _iscsi_io; 215 struct spdk_bdev_io *bdev_io; 216 217 iscsi_io->scsi_status = status; 218 iscsi_io->sk = (uint8_t)task->sense.key; 219 iscsi_io->asc = (task->sense.ascq >> 8) & 0xFF; 220 iscsi_io->ascq = task->sense.ascq & 0xFF; 221 222 scsi_free_scsi_task(task); 223 224 if (_bdev_iscsi_is_size_change(status, task)) { 225 bdev_iscsi_readcapacity16(context, iscsi_io->lun); 226 227 /* Retry this failed IO immediately */ 228 bdev_io = spdk_bdev_io_from_ctx(iscsi_io); 229 if (iscsi_io->submit_td != NULL) { 230 spdk_thread_send_msg(iscsi_io->lun->main_td, 231 _bdev_iscsi_submit_request, bdev_io); 232 } else { 233 _bdev_iscsi_submit_request(bdev_io); 234 } 235 } else { 236 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_SUCCESS); 237 } 238 } 239 240 static int 241 bdev_iscsi_resize(struct spdk_bdev *bdev, const uint64_t new_size_in_block) 242 { 243 int rc; 244 245 assert(bdev->module == &g_iscsi_bdev_module); 246 247 if (new_size_in_block <= bdev->blockcnt) { 248 SPDK_ERRLOG("The new bdev size must be larger than current bdev size.\n"); 249 return -EINVAL; 250 } 251 252 rc = spdk_bdev_notify_blockcnt_change(bdev, new_size_in_block); 253 if (rc != 0) { 254 SPDK_ERRLOG("failed to notify block cnt change.\n"); 255 return rc; 256 } 257 258 return 0; 259 } 260 261 static void 262 bdev_iscsi_readcapacity16_cb(struct iscsi_context *context, int status, void *_task, 263 void *private_data) 264 { 265 struct bdev_iscsi_lun *lun = private_data; 266 struct scsi_readcapacity16 *readcap16; 267 struct scsi_task *task = _task; 268 uint64_t size_in_block = 0; 269 int rc; 270 271 if (status != SPDK_SCSI_STATUS_GOOD) { 272 SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(context)); 273 goto ret; 274 } 275 276 readcap16 = scsi_datain_unmarshall(task); 277 if (!readcap16) { 278 SPDK_ERRLOG("Read capacity error\n"); 279 goto ret; 280 } 281 282 size_in_block = readcap16->returned_lba + 1; 283 284 rc = bdev_iscsi_resize(&lun->bdev, size_in_block); 285 if (rc != 0) { 286 SPDK_ERRLOG("Bdev (%s) resize error: %d\n", lun->bdev.name, rc); 287 } 288 289 ret: 290 scsi_free_scsi_task(task); 291 } 292 293 static void 294 bdev_iscsi_readcapacity16(struct iscsi_context *context, struct bdev_iscsi_lun *lun) 295 { 296 struct scsi_task *task; 297 298 task = iscsi_readcapacity16_task(context, lun->lun_id, 299 bdev_iscsi_readcapacity16_cb, lun); 300 if (task == NULL) { 301 SPDK_ERRLOG("failed to get readcapacity16_task\n"); 302 } 303 } 304 305 static void 306 bdev_iscsi_readv(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, 307 struct iovec *iov, int iovcnt, uint64_t nbytes, uint64_t lba) 308 { 309 struct scsi_task *task; 310 311 SPDK_DEBUGLOG(iscsi_init, "read %d iovs size %lu to lba: %#lx\n", 312 iovcnt, nbytes, lba); 313 314 task = iscsi_read16_task(lun->context, lun->lun_id, lba, nbytes, lun->bdev.blocklen, 0, 0, 0, 0, 0, 315 bdev_iscsi_command_cb, iscsi_io); 316 if (task == NULL) { 317 SPDK_ERRLOG("failed to get read16_task\n"); 318 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 319 return; 320 } 321 322 #if defined(LIBISCSI_FEATURE_IOVECTOR) 323 scsi_task_set_iov_in(task, (struct scsi_iovec *)iov, iovcnt); 324 #else 325 int i; 326 for (i = 0; i < iovcnt; i++) { 327 scsi_task_add_data_in_buffer(task, iov[i].iov_len, iov[i].iov_base); 328 } 329 #endif 330 } 331 332 static void 333 bdev_iscsi_writev(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, 334 struct iovec *iov, int iovcnt, uint64_t nbytes, uint64_t lba) 335 { 336 struct scsi_task *task; 337 338 SPDK_DEBUGLOG(iscsi_init, "write %d iovs size %lu to lba: %#lx\n", 339 iovcnt, nbytes, lba); 340 341 task = iscsi_write16_task(lun->context, lun->lun_id, lba, NULL, nbytes, lun->bdev.blocklen, 0, 0, 0, 342 0, 0, 343 bdev_iscsi_command_cb, iscsi_io); 344 if (task == NULL) { 345 SPDK_ERRLOG("failed to get write16_task\n"); 346 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 347 return; 348 } 349 350 #if defined(LIBISCSI_FEATURE_IOVECTOR) 351 scsi_task_set_iov_out(task, (struct scsi_iovec *)iov, iovcnt); 352 #else 353 int i; 354 for (i = 0; i < iovcnt; i++) { 355 scsi_task_add_data_in_buffer(task, iov[i].iov_len, iov[i].iov_base); 356 } 357 #endif 358 } 359 360 static void 361 bdev_iscsi_destruct_cb(void *ctx) 362 { 363 struct bdev_iscsi_lun *lun = ctx; 364 365 spdk_poller_unregister(&lun->no_main_ch_poller); 366 spdk_io_device_unregister(lun, _iscsi_free_lun); 367 } 368 369 static int 370 bdev_iscsi_destruct(void *ctx) 371 { 372 struct bdev_iscsi_lun *lun = ctx; 373 374 assert(lun->no_main_ch_poller_td); 375 spdk_thread_send_msg(lun->no_main_ch_poller_td, bdev_iscsi_destruct_cb, lun); 376 return 1; 377 } 378 379 static void 380 bdev_iscsi_flush(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, uint32_t num_blocks, 381 int immed, uint64_t lba) 382 { 383 struct scsi_task *task; 384 385 task = iscsi_synchronizecache16_task(lun->context, lun->lun_id, lba, 386 num_blocks, 0, immed, bdev_iscsi_command_cb, iscsi_io); 387 if (task == NULL) { 388 SPDK_ERRLOG("failed to get sync16_task\n"); 389 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 390 return; 391 } 392 } 393 394 static void 395 bdev_iscsi_unmap(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, 396 uint64_t lba, uint64_t num_blocks) 397 { 398 struct scsi_task *task; 399 struct unmap_list list[BDEV_ISCSI_MAX_UNMAP_BLOCK_DESCS_COUNT] = {}; 400 struct unmap_list *entry; 401 uint32_t num_unmap_list; 402 uint64_t offset, remaining, unmap_blocks; 403 404 num_unmap_list = spdk_divide_round_up(num_blocks, lun->max_unmap); 405 if (num_unmap_list > BDEV_ISCSI_MAX_UNMAP_BLOCK_DESCS_COUNT) { 406 SPDK_ERRLOG("Too many unmap entries\n"); 407 goto failed; 408 } 409 410 remaining = num_blocks; 411 offset = lba; 412 num_unmap_list = 0; 413 entry = &list[0]; 414 415 do { 416 unmap_blocks = spdk_min(remaining, lun->max_unmap); 417 entry->lba = offset; 418 entry->num = unmap_blocks; 419 num_unmap_list++; 420 remaining -= unmap_blocks; 421 offset += unmap_blocks; 422 entry++; 423 } while (remaining > 0); 424 425 task = iscsi_unmap_task(lun->context, 0, 0, 0, list, num_unmap_list, 426 bdev_iscsi_command_cb, iscsi_io); 427 if (task != NULL) { 428 return; 429 } 430 SPDK_ERRLOG("failed to get unmap_task\n"); 431 432 failed: 433 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 434 } 435 436 static void 437 bdev_iscsi_reset_cb(struct iscsi_context *context __attribute__((unused)), int status, 438 void *command_data, void *private_data) 439 { 440 uint32_t tmf_response; 441 struct bdev_iscsi_io *iscsi_io = private_data; 442 443 tmf_response = *(uint32_t *)command_data; 444 if (tmf_response == ISCSI_TASK_FUNC_RESP_COMPLETE) { 445 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_SUCCESS); 446 } else { 447 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 448 } 449 } 450 451 static void 452 _bdev_iscsi_reset(void *_bdev_io) 453 { 454 int rc; 455 struct spdk_bdev_io *bdev_io = _bdev_io; 456 struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; 457 struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx; 458 struct iscsi_context *context = lun->context; 459 460 rc = iscsi_task_mgmt_lun_reset_async(context, lun->lun_id, 461 bdev_iscsi_reset_cb, iscsi_io); 462 if (rc != 0) { 463 SPDK_ERRLOG("failed to do iscsi reset\n"); 464 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 465 return; 466 } 467 } 468 469 static void 470 bdev_iscsi_reset(struct spdk_bdev_io *bdev_io) 471 { 472 struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; 473 spdk_thread_send_msg(lun->main_td, _bdev_iscsi_reset, bdev_io); 474 } 475 476 static int 477 bdev_iscsi_poll_lun(void *_lun) 478 { 479 struct bdev_iscsi_lun *lun = _lun; 480 struct pollfd pfd = {}; 481 482 pfd.fd = iscsi_get_fd(lun->context); 483 pfd.events = iscsi_which_events(lun->context); 484 485 if (poll(&pfd, 1, 0) < 0) { 486 SPDK_ERRLOG("poll failed\n"); 487 return SPDK_POLLER_IDLE; 488 } 489 490 if (pfd.revents != 0) { 491 if (iscsi_service(lun->context, pfd.revents) < 0) { 492 SPDK_ERRLOG("iscsi_service failed: %s\n", iscsi_get_error(lun->context)); 493 } 494 495 return SPDK_POLLER_BUSY; 496 } 497 498 return SPDK_POLLER_IDLE; 499 } 500 501 static int 502 bdev_iscsi_no_main_ch_poll(void *arg) 503 { 504 struct bdev_iscsi_lun *lun = arg; 505 enum spdk_thread_poller_rc rc = SPDK_POLLER_IDLE; 506 507 if (pthread_mutex_trylock(&lun->mutex)) { 508 /* Don't care about the error code here. */ 509 return SPDK_POLLER_IDLE; 510 } 511 512 if (lun->ch_count == 0) { 513 rc = bdev_iscsi_poll_lun(arg); 514 } 515 516 pthread_mutex_unlock(&lun->mutex); 517 return rc; 518 } 519 520 static void 521 bdev_iscsi_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, 522 bool success) 523 { 524 if (!success) { 525 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 526 return; 527 } 528 529 bdev_iscsi_readv((struct bdev_iscsi_lun *)bdev_io->bdev->ctxt, 530 (struct bdev_iscsi_io *)bdev_io->driver_ctx, 531 bdev_io->u.bdev.iovs, 532 bdev_io->u.bdev.iovcnt, 533 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen, 534 bdev_io->u.bdev.offset_blocks); 535 } 536 537 static void _bdev_iscsi_submit_request(void *_bdev_io) 538 { 539 struct spdk_bdev_io *bdev_io = _bdev_io; 540 struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx; 541 struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; 542 543 switch (bdev_io->type) { 544 case SPDK_BDEV_IO_TYPE_READ: 545 spdk_bdev_io_get_buf(bdev_io, bdev_iscsi_get_buf_cb, 546 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 547 break; 548 549 case SPDK_BDEV_IO_TYPE_WRITE: 550 bdev_iscsi_writev(lun, iscsi_io, 551 bdev_io->u.bdev.iovs, 552 bdev_io->u.bdev.iovcnt, 553 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen, 554 bdev_io->u.bdev.offset_blocks); 555 break; 556 case SPDK_BDEV_IO_TYPE_FLUSH: 557 bdev_iscsi_flush(lun, iscsi_io, 558 bdev_io->u.bdev.num_blocks, 559 ISCSI_IMMEDIATE_DATA_NO, 560 bdev_io->u.bdev.offset_blocks); 561 break; 562 case SPDK_BDEV_IO_TYPE_RESET: 563 bdev_iscsi_reset(bdev_io); 564 break; 565 case SPDK_BDEV_IO_TYPE_UNMAP: 566 bdev_iscsi_unmap(lun, iscsi_io, 567 bdev_io->u.bdev.offset_blocks, 568 bdev_io->u.bdev.num_blocks); 569 break; 570 default: 571 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 572 break; 573 } 574 } 575 576 static void bdev_iscsi_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) 577 { 578 struct spdk_thread *submit_td = spdk_io_channel_get_thread(_ch); 579 struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx; 580 struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; 581 582 iscsi_io->lun = lun; 583 584 if (lun->main_td != submit_td) { 585 iscsi_io->submit_td = submit_td; 586 spdk_thread_send_msg(lun->main_td, _bdev_iscsi_submit_request, bdev_io); 587 return; 588 } else { 589 iscsi_io->submit_td = NULL; 590 } 591 592 _bdev_iscsi_submit_request(bdev_io); 593 } 594 595 static bool 596 bdev_iscsi_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) 597 { 598 struct bdev_iscsi_lun *lun = ctx; 599 600 switch (io_type) { 601 case SPDK_BDEV_IO_TYPE_READ: 602 case SPDK_BDEV_IO_TYPE_WRITE: 603 case SPDK_BDEV_IO_TYPE_FLUSH: 604 case SPDK_BDEV_IO_TYPE_RESET: 605 return true; 606 607 case SPDK_BDEV_IO_TYPE_UNMAP: 608 return lun->unmap_supported; 609 default: 610 return false; 611 } 612 } 613 614 static int 615 bdev_iscsi_create_cb(void *io_device, void *ctx_buf) 616 { 617 struct bdev_iscsi_io_channel *ch = ctx_buf; 618 struct bdev_iscsi_lun *lun = io_device; 619 620 pthread_mutex_lock(&lun->mutex); 621 if (lun->ch_count == 0) { 622 assert(lun->main_td == NULL); 623 lun->main_td = spdk_get_thread(); 624 lun->poller = SPDK_POLLER_REGISTER(bdev_iscsi_poll_lun, lun, 0); 625 ch->lun = lun; 626 } 627 lun->ch_count++; 628 pthread_mutex_unlock(&lun->mutex); 629 630 return 0; 631 } 632 633 static void 634 _iscsi_destroy_cb(void *ctx) 635 { 636 struct bdev_iscsi_lun *lun = ctx; 637 638 pthread_mutex_lock(&lun->mutex); 639 640 assert(lun->main_td == spdk_get_thread()); 641 assert(lun->ch_count > 0); 642 643 lun->ch_count--; 644 if (lun->ch_count > 0) { 645 pthread_mutex_unlock(&lun->mutex); 646 return; 647 } 648 649 lun->main_td = NULL; 650 spdk_poller_unregister(&lun->poller); 651 652 pthread_mutex_unlock(&lun->mutex); 653 } 654 655 static void 656 bdev_iscsi_destroy_cb(void *io_device, void *ctx_buf) 657 { 658 struct bdev_iscsi_lun *lun = io_device; 659 struct spdk_thread *thread; 660 661 pthread_mutex_lock(&lun->mutex); 662 lun->ch_count--; 663 if (lun->ch_count == 0) { 664 assert(lun->main_td != NULL); 665 666 if (lun->main_td != spdk_get_thread()) { 667 /* The final channel was destroyed on a different thread 668 * than where the first channel was created. Pass a message 669 * to the main thread to unregister the poller. */ 670 lun->ch_count++; 671 thread = lun->main_td; 672 pthread_mutex_unlock(&lun->mutex); 673 spdk_thread_send_msg(thread, _iscsi_destroy_cb, lun); 674 return; 675 } 676 677 lun->main_td = NULL; 678 spdk_poller_unregister(&lun->poller); 679 } 680 pthread_mutex_unlock(&lun->mutex); 681 } 682 683 static struct spdk_io_channel * 684 bdev_iscsi_get_io_channel(void *ctx) 685 { 686 struct bdev_iscsi_lun *lun = ctx; 687 688 return spdk_get_io_channel(lun); 689 } 690 691 static int 692 bdev_iscsi_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 693 { 694 struct bdev_iscsi_lun *lun = ctx; 695 696 spdk_json_write_named_object_begin(w, "iscsi"); 697 spdk_json_write_named_string(w, "initiator_name", lun->initiator_iqn); 698 spdk_json_write_named_string(w, "url", lun->url); 699 spdk_json_write_object_end(w); 700 701 return 0; 702 } 703 704 static void 705 bdev_iscsi_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 706 { 707 struct bdev_iscsi_lun *lun = bdev->ctxt; 708 709 pthread_mutex_lock(&lun->mutex); 710 spdk_json_write_object_begin(w); 711 712 spdk_json_write_named_string(w, "method", "bdev_iscsi_create"); 713 714 spdk_json_write_named_object_begin(w, "params"); 715 spdk_json_write_named_string(w, "name", bdev->name); 716 spdk_json_write_named_string(w, "initiator_iqn", lun->initiator_iqn); 717 spdk_json_write_named_string(w, "url", lun->url); 718 spdk_json_write_object_end(w); 719 720 spdk_json_write_object_end(w); 721 pthread_mutex_unlock(&lun->mutex); 722 } 723 724 static const struct spdk_bdev_fn_table iscsi_fn_table = { 725 .destruct = bdev_iscsi_destruct, 726 .submit_request = bdev_iscsi_submit_request, 727 .io_type_supported = bdev_iscsi_io_type_supported, 728 .get_io_channel = bdev_iscsi_get_io_channel, 729 .dump_info_json = bdev_iscsi_dump_info_json, 730 .write_config_json = bdev_iscsi_write_config_json, 731 }; 732 733 static int 734 create_iscsi_lun(struct bdev_iscsi_conn_req *req, uint64_t num_blocks, 735 uint32_t block_size, struct spdk_bdev **bdev, uint8_t lbppbe) 736 { 737 struct bdev_iscsi_lun *lun; 738 int rc; 739 740 lun = calloc(sizeof(*lun), 1); 741 if (!lun) { 742 SPDK_ERRLOG("Unable to allocate enough memory for iscsi backend\n"); 743 return -ENOMEM; 744 } 745 746 lun->context = req->context; 747 lun->lun_id = req->lun; 748 lun->url = req->url; 749 lun->initiator_iqn = req->initiator_iqn; 750 751 pthread_mutex_init(&lun->mutex, NULL); 752 753 lun->bdev.name = req->bdev_name; 754 lun->bdev.product_name = "iSCSI LUN"; 755 lun->bdev.module = &g_iscsi_bdev_module; 756 lun->bdev.blocklen = block_size; 757 lun->bdev.phys_blocklen = block_size * (1 << lbppbe); 758 lun->bdev.blockcnt = num_blocks; 759 lun->bdev.ctxt = lun; 760 lun->unmap_supported = req->unmap_supported; 761 if (lun->unmap_supported) { 762 lun->max_unmap = req->max_unmap; 763 lun->bdev.max_unmap = req->max_unmap; 764 lun->bdev.max_unmap_segments = BDEV_ISCSI_MAX_UNMAP_BLOCK_DESCS_COUNT; 765 } 766 767 lun->bdev.fn_table = &iscsi_fn_table; 768 769 spdk_io_device_register(lun, bdev_iscsi_create_cb, bdev_iscsi_destroy_cb, 770 sizeof(struct bdev_iscsi_io_channel), 771 req->bdev_name); 772 rc = spdk_bdev_register(&lun->bdev); 773 if (rc) { 774 spdk_io_device_unregister(lun, NULL); 775 pthread_mutex_destroy(&lun->mutex); 776 free(lun); 777 return rc; 778 } 779 780 lun->no_main_ch_poller_td = spdk_get_thread(); 781 lun->no_main_ch_poller = SPDK_POLLER_REGISTER(bdev_iscsi_no_main_ch_poll, lun, 782 BDEV_ISCSI_NO_MAIN_CH_POLL_US); 783 784 *bdev = &lun->bdev; 785 return 0; 786 } 787 788 static void 789 iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status, 790 void *command_data, void *private_data) 791 { 792 struct bdev_iscsi_conn_req *req = private_data; 793 struct scsi_readcapacity16 *readcap16; 794 struct spdk_bdev *bdev = NULL; 795 struct scsi_task *task = command_data; 796 struct scsi_task *retry_task = NULL; 797 798 if (status != SPDK_SCSI_STATUS_GOOD) { 799 SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(iscsi)); 800 if (_bdev_iscsi_is_size_change(status, task)) { 801 scsi_free_scsi_task(task); 802 retry_task = iscsi_readcapacity16_task(iscsi, req->lun, 803 iscsi_readcapacity16_cb, req); 804 if (retry_task) { 805 return; 806 } 807 } 808 goto ret; 809 } 810 811 readcap16 = scsi_datain_unmarshall(task); 812 if (!readcap16) { 813 status = -ENOMEM; 814 goto ret; 815 } 816 817 status = create_iscsi_lun(req, readcap16->returned_lba + 1, readcap16->block_length, &bdev, 818 readcap16->lbppbe); 819 if (status) { 820 SPDK_ERRLOG("Unable to create iscsi bdev: %s (%d)\n", spdk_strerror(-status), status); 821 } 822 823 ret: 824 scsi_free_scsi_task(task); 825 complete_conn_req(req, bdev, status); 826 } 827 828 static void 829 bdev_iscsi_inquiry_bl_cb(struct iscsi_context *context, int status, void *_task, void *private_data) 830 { 831 struct scsi_task *task = _task; 832 struct scsi_inquiry_block_limits *bl_inq = NULL; 833 struct bdev_iscsi_conn_req *req = private_data; 834 835 if (status == SPDK_SCSI_STATUS_GOOD) { 836 bl_inq = scsi_datain_unmarshall(task); 837 if (bl_inq != NULL) { 838 if (!bl_inq->max_unmap) { 839 SPDK_ERRLOG("Invalid max_unmap, use the default\n"); 840 req->max_unmap = BDEV_ISCSI_DEFAULT_MAX_UNMAP_LBA_COUNT; 841 } else { 842 req->max_unmap = bl_inq->max_unmap; 843 } 844 } 845 } 846 847 scsi_free_scsi_task(task); 848 task = iscsi_readcapacity16_task(context, req->lun, iscsi_readcapacity16_cb, req); 849 if (task) { 850 return; 851 } 852 853 SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context)); 854 complete_conn_req(req, NULL, status); 855 } 856 857 static void 858 bdev_iscsi_inquiry_lbp_cb(struct iscsi_context *context, int status, void *_task, 859 void *private_data) 860 { 861 struct scsi_task *task = _task; 862 struct scsi_inquiry_logical_block_provisioning *lbp_inq = NULL; 863 struct bdev_iscsi_conn_req *req = private_data; 864 865 if (status == SPDK_SCSI_STATUS_GOOD) { 866 lbp_inq = scsi_datain_unmarshall(task); 867 if (lbp_inq != NULL && lbp_inq->lbpu) { 868 req->unmap_supported = true; 869 scsi_free_scsi_task(task); 870 871 task = iscsi_inquiry_task(context, req->lun, 1, 872 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS, 873 255, bdev_iscsi_inquiry_bl_cb, req); 874 if (task) { 875 return; 876 } 877 } 878 } else { 879 scsi_free_scsi_task(task); 880 } 881 882 task = iscsi_readcapacity16_task(context, req->lun, iscsi_readcapacity16_cb, req); 883 if (task) { 884 return; 885 } 886 887 SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context)); 888 complete_conn_req(req, NULL, status); 889 } 890 891 static void 892 iscsi_connect_cb(struct iscsi_context *iscsi, int status, 893 void *command_data, void *private_data) 894 { 895 struct bdev_iscsi_conn_req *req = private_data; 896 struct scsi_task *task; 897 898 if (status != SPDK_SCSI_STATUS_GOOD) { 899 goto ret; 900 } 901 902 task = iscsi_inquiry_task(iscsi, req->lun, 1, 903 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, 904 255, bdev_iscsi_inquiry_lbp_cb, req); 905 if (task) { 906 return; 907 } 908 909 ret: 910 SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context)); 911 complete_conn_req(req, NULL, status); 912 } 913 914 static int 915 iscsi_bdev_conn_poll(void *arg) 916 { 917 struct bdev_iscsi_conn_req *req, *tmp; 918 struct pollfd pfd; 919 struct iscsi_context *context; 920 921 if (TAILQ_EMPTY(&g_iscsi_conn_req)) { 922 spdk_poller_unregister(&g_conn_poller); 923 return SPDK_POLLER_IDLE; 924 } 925 926 TAILQ_FOREACH_SAFE(req, &g_iscsi_conn_req, link, tmp) { 927 context = req->context; 928 pfd.fd = iscsi_get_fd(context); 929 pfd.events = iscsi_which_events(context); 930 pfd.revents = 0; 931 if (poll(&pfd, 1, 0) < 0) { 932 SPDK_ERRLOG("poll failed\n"); 933 return SPDK_POLLER_BUSY; 934 } 935 936 if (pfd.revents != 0) { 937 if (iscsi_service(context, pfd.revents) < 0) { 938 SPDK_ERRLOG("iscsi_service failed: %s\n", iscsi_get_error(context)); 939 } 940 } 941 942 if (req->status == 0) { 943 /* 944 * The request completed successfully. 945 */ 946 free(req); 947 } else if (req->status > 0) { 948 /* 949 * An error has occurred during connecting. This req has already 950 * been removed from the g_iscsi_conn_req list, but we needed to 951 * wait until iscsi_service unwound before we could free the req. 952 */ 953 _bdev_iscsi_conn_req_free(req); 954 } 955 } 956 return SPDK_POLLER_BUSY; 957 } 958 959 int 960 create_iscsi_disk(const char *bdev_name, const char *url, const char *initiator_iqn, 961 spdk_bdev_iscsi_create_cb cb_fn, void *cb_arg) 962 { 963 struct bdev_iscsi_conn_req *req; 964 struct iscsi_url *iscsi_url = NULL; 965 int rc; 966 967 if (!bdev_name || !url || !initiator_iqn || strlen(initiator_iqn) == 0 || !cb_fn) { 968 return -EINVAL; 969 } 970 971 req = calloc(1, sizeof(struct bdev_iscsi_conn_req)); 972 if (!req) { 973 SPDK_ERRLOG("Cannot allocate pointer of struct bdev_iscsi_conn_req\n"); 974 return -ENOMEM; 975 } 976 977 req->status = SCSI_STATUS_GOOD; 978 req->bdev_name = strdup(bdev_name); 979 req->url = strdup(url); 980 req->initiator_iqn = strdup(initiator_iqn); 981 req->context = iscsi_create_context(initiator_iqn); 982 if (!req->bdev_name || !req->url || !req->initiator_iqn || !req->context) { 983 SPDK_ERRLOG("Out of memory\n"); 984 rc = -ENOMEM; 985 goto err; 986 } 987 988 req->create_cb = cb_fn; 989 req->create_cb_arg = cb_arg; 990 991 iscsi_url = iscsi_parse_full_url(req->context, url); 992 if (iscsi_url == NULL) { 993 SPDK_ERRLOG("could not parse URL: %s\n", iscsi_get_error(req->context)); 994 rc = -EINVAL; 995 goto err; 996 } 997 998 req->lun = iscsi_url->lun; 999 rc = iscsi_set_session_type(req->context, ISCSI_SESSION_NORMAL); 1000 rc = rc ? rc : iscsi_set_header_digest(req->context, ISCSI_HEADER_DIGEST_NONE); 1001 rc = rc ? rc : iscsi_set_targetname(req->context, iscsi_url->target); 1002 rc = rc ? rc : iscsi_full_connect_async(req->context, iscsi_url->portal, iscsi_url->lun, 1003 iscsi_connect_cb, req); 1004 if (rc == 0 && iscsi_url->user[0] != '\0') { 1005 rc = iscsi_set_initiator_username_pwd(req->context, iscsi_url->user, iscsi_url->passwd); 1006 } 1007 1008 if (rc < 0) { 1009 SPDK_ERRLOG("Failed to connect provided URL=%s: %s\n", url, iscsi_get_error(req->context)); 1010 goto err; 1011 } 1012 1013 iscsi_destroy_url(iscsi_url); 1014 req->status = -1; 1015 TAILQ_INSERT_TAIL(&g_iscsi_conn_req, req, link); 1016 if (!g_conn_poller) { 1017 g_conn_poller = SPDK_POLLER_REGISTER(iscsi_bdev_conn_poll, NULL, BDEV_ISCSI_CONNECTION_POLL_US); 1018 } 1019 1020 return 0; 1021 1022 err: 1023 /* iscsi_destroy_url() is not NULL-proof */ 1024 if (iscsi_url) { 1025 iscsi_destroy_url(iscsi_url); 1026 } 1027 1028 if (req->context) { 1029 iscsi_destroy_context(req->context); 1030 } 1031 1032 free(req->initiator_iqn); 1033 free(req->bdev_name); 1034 free(req->url); 1035 free(req); 1036 return rc; 1037 } 1038 1039 void 1040 delete_iscsi_disk(const char *bdev_name, spdk_delete_iscsi_complete cb_fn, void *cb_arg) 1041 { 1042 int rc; 1043 1044 rc = spdk_bdev_unregister_by_name(bdev_name, &g_iscsi_bdev_module, cb_fn, cb_arg); 1045 if (rc != 0) { 1046 cb_fn(cb_arg, rc); 1047 } 1048 } 1049 1050 static int 1051 bdev_iscsi_initialize(void) 1052 { 1053 return 0; 1054 } 1055 1056 SPDK_LOG_REGISTER_COMPONENT(iscsi_init) 1057