1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk/bdev.h" 37 #include "spdk/env.h" 38 #include "spdk/fd.h" 39 #include "spdk/thread.h" 40 #include "spdk/json.h" 41 #include "spdk/util.h" 42 #include "spdk/rpc.h" 43 #include "spdk/string.h" 44 #include "spdk/iscsi_spec.h" 45 46 #include "spdk/log.h" 47 #include "spdk/bdev_module.h" 48 49 #include "iscsi/iscsi.h" 50 #include "iscsi/scsi-lowlevel.h" 51 52 #include "bdev_iscsi.h" 53 54 struct bdev_iscsi_lun; 55 56 #define BDEV_ISCSI_CONNECTION_POLL_US 500 /* 0.5 ms */ 57 #define BDEV_ISCSI_NO_MAIN_CH_POLL_US 10000 /* 10ms */ 58 59 #define DEFAULT_INITIATOR_NAME "iqn.2016-06.io.spdk:init" 60 61 static int bdev_iscsi_initialize(void); 62 static TAILQ_HEAD(, bdev_iscsi_conn_req) g_iscsi_conn_req = TAILQ_HEAD_INITIALIZER( 63 g_iscsi_conn_req); 64 static struct spdk_poller *g_conn_poller = NULL; 65 66 struct bdev_iscsi_io { 67 struct spdk_thread *submit_td; 68 enum spdk_bdev_io_status status; 69 int scsi_status; 70 enum spdk_scsi_sense sk; 71 uint8_t asc; 72 uint8_t ascq; 73 }; 74 75 struct bdev_iscsi_lun { 76 struct spdk_bdev bdev; 77 struct iscsi_context *context; 78 char *initiator_iqn; 79 int lun_id; 80 char *url; 81 pthread_mutex_t mutex; 82 uint32_t ch_count; 83 struct spdk_thread *main_td; 84 struct spdk_poller *no_main_ch_poller; 85 struct spdk_thread *no_main_ch_poller_td; 86 bool unmap_supported; 87 struct spdk_poller *poller; 88 }; 89 90 struct bdev_iscsi_io_channel { 91 struct bdev_iscsi_lun *lun; 92 }; 93 94 struct bdev_iscsi_conn_req { 95 char *url; 96 char *bdev_name; 97 char *initiator_iqn; 98 struct iscsi_context *context; 99 spdk_bdev_iscsi_create_cb create_cb; 100 void *create_cb_arg; 101 bool unmap_supported; 102 int lun; 103 int status; 104 TAILQ_ENTRY(bdev_iscsi_conn_req) link; 105 }; 106 107 static void 108 complete_conn_req(struct bdev_iscsi_conn_req *req, struct spdk_bdev *bdev, 109 int status) 110 { 111 TAILQ_REMOVE(&g_iscsi_conn_req, req, link); 112 req->create_cb(req->create_cb_arg, bdev, status); 113 114 /* 115 * we are still running in the context of iscsi_service() 116 * so do not tear down its data structures here 117 */ 118 req->status = status; 119 } 120 121 static int 122 bdev_iscsi_get_ctx_size(void) 123 { 124 return sizeof(struct bdev_iscsi_io); 125 } 126 127 static void 128 _iscsi_free_lun(void *arg) 129 { 130 struct bdev_iscsi_lun *lun = arg; 131 132 assert(lun != NULL); 133 iscsi_destroy_context(lun->context); 134 pthread_mutex_destroy(&lun->mutex); 135 free(lun->bdev.name); 136 free(lun->url); 137 free(lun->initiator_iqn); 138 139 spdk_bdev_destruct_done(&lun->bdev, 0); 140 free(lun); 141 } 142 143 static void 144 _bdev_iscsi_conn_req_free(struct bdev_iscsi_conn_req *req) 145 { 146 free(req->initiator_iqn); 147 free(req->bdev_name); 148 free(req->url); 149 /* destroy will call iscsi_disconnect() implicitly if connected */ 150 iscsi_destroy_context(req->context); 151 free(req); 152 } 153 154 static void 155 bdev_iscsi_finish(void) 156 { 157 struct bdev_iscsi_conn_req *req, *tmp; 158 159 /* clear out pending connection requests here. We cannot 160 * simply set the state to a non SCSI_STATUS_GOOD state as 161 * the connection poller wont run anymore 162 */ 163 TAILQ_FOREACH_SAFE(req, &g_iscsi_conn_req, link, tmp) { 164 _bdev_iscsi_conn_req_free(req); 165 } 166 167 if (g_conn_poller) { 168 spdk_poller_unregister(&g_conn_poller); 169 } 170 } 171 172 static struct spdk_bdev_module g_iscsi_bdev_module = { 173 .name = "iscsi", 174 .module_init = bdev_iscsi_initialize, 175 .module_fini = bdev_iscsi_finish, 176 .get_ctx_size = bdev_iscsi_get_ctx_size, 177 }; 178 179 SPDK_BDEV_MODULE_REGISTER(iscsi, &g_iscsi_bdev_module); 180 181 static void 182 _bdev_iscsi_io_complete(void *_iscsi_io) 183 { 184 struct bdev_iscsi_io *iscsi_io = _iscsi_io; 185 186 if (iscsi_io->status == SPDK_BDEV_IO_STATUS_SUCCESS) { 187 spdk_bdev_io_complete_scsi_status(spdk_bdev_io_from_ctx(iscsi_io), iscsi_io->scsi_status, 188 iscsi_io->sk, iscsi_io->asc, iscsi_io->ascq); 189 } else { 190 spdk_bdev_io_complete(spdk_bdev_io_from_ctx(iscsi_io), iscsi_io->status); 191 } 192 } 193 194 static void 195 bdev_iscsi_io_complete(struct bdev_iscsi_io *iscsi_io, enum spdk_bdev_io_status status) 196 { 197 iscsi_io->status = status; 198 if (iscsi_io->submit_td != NULL) { 199 spdk_thread_send_msg(iscsi_io->submit_td, _bdev_iscsi_io_complete, iscsi_io); 200 } else { 201 _bdev_iscsi_io_complete(iscsi_io); 202 } 203 } 204 205 /* Common call back function for read/write/flush command */ 206 static void 207 bdev_iscsi_command_cb(struct iscsi_context *context, int status, void *_task, void *_iscsi_io) 208 { 209 struct scsi_task *task = _task; 210 struct bdev_iscsi_io *iscsi_io = _iscsi_io; 211 212 iscsi_io->scsi_status = status; 213 iscsi_io->sk = (uint8_t)task->sense.key; 214 iscsi_io->asc = (task->sense.ascq >> 8) & 0xFF; 215 iscsi_io->ascq = task->sense.ascq & 0xFF; 216 217 scsi_free_scsi_task(task); 218 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_SUCCESS); 219 } 220 221 static void 222 bdev_iscsi_readv(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, 223 struct iovec *iov, int iovcnt, uint64_t nbytes, uint64_t lba) 224 { 225 struct scsi_task *task; 226 227 SPDK_DEBUGLOG(iscsi_init, "read %d iovs size %lu to lba: %#lx\n", 228 iovcnt, nbytes, lba); 229 230 task = iscsi_read16_task(lun->context, lun->lun_id, lba, nbytes, lun->bdev.blocklen, 0, 0, 0, 0, 0, 231 bdev_iscsi_command_cb, iscsi_io); 232 if (task == NULL) { 233 SPDK_ERRLOG("failed to get read16_task\n"); 234 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 235 return; 236 } 237 238 #if defined(LIBISCSI_FEATURE_IOVECTOR) 239 scsi_task_set_iov_in(task, (struct scsi_iovec *)iov, iovcnt); 240 #else 241 int i; 242 for (i = 0; i < iovcnt; i++) { 243 scsi_task_add_data_in_buffer(task, iov[i].iov_len, iov[i].iov_base); 244 } 245 #endif 246 } 247 248 static void 249 bdev_iscsi_writev(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, 250 struct iovec *iov, int iovcnt, uint64_t nbytes, uint64_t lba) 251 { 252 struct scsi_task *task; 253 254 SPDK_DEBUGLOG(iscsi_init, "write %d iovs size %lu to lba: %#lx\n", 255 iovcnt, nbytes, lba); 256 257 task = iscsi_write16_task(lun->context, lun->lun_id, lba, NULL, nbytes, lun->bdev.blocklen, 0, 0, 0, 258 0, 0, 259 bdev_iscsi_command_cb, iscsi_io); 260 if (task == NULL) { 261 SPDK_ERRLOG("failed to get write16_task\n"); 262 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 263 return; 264 } 265 266 #if defined(LIBISCSI_FEATURE_IOVECTOR) 267 scsi_task_set_iov_out(task, (struct scsi_iovec *)iov, iovcnt); 268 #else 269 int i; 270 for (i = 0; i < iovcnt; i++) { 271 scsi_task_add_data_in_buffer(task, iov[i].iov_len, iov[i].iov_base); 272 } 273 #endif 274 } 275 276 static void 277 bdev_iscsi_destruct_cb(void *ctx) 278 { 279 struct bdev_iscsi_lun *lun = ctx; 280 281 spdk_poller_unregister(&lun->no_main_ch_poller); 282 spdk_io_device_unregister(lun, _iscsi_free_lun); 283 } 284 285 static int 286 bdev_iscsi_destruct(void *ctx) 287 { 288 struct bdev_iscsi_lun *lun = ctx; 289 290 assert(lun->no_main_ch_poller_td); 291 spdk_thread_send_msg(lun->no_main_ch_poller_td, bdev_iscsi_destruct_cb, lun); 292 return 1; 293 } 294 295 static void 296 bdev_iscsi_flush(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, uint32_t num_blocks, 297 int immed, uint64_t lba) 298 { 299 struct scsi_task *task; 300 301 task = iscsi_synchronizecache16_task(lun->context, lun->lun_id, lba, 302 num_blocks, 0, immed, bdev_iscsi_command_cb, iscsi_io); 303 if (task == NULL) { 304 SPDK_ERRLOG("failed to get sync16_task\n"); 305 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 306 return; 307 } 308 } 309 310 static void 311 bdev_iscsi_unmap(struct bdev_iscsi_lun *lun, struct bdev_iscsi_io *iscsi_io, 312 uint64_t lba, uint64_t num_blocks) 313 { 314 struct scsi_task *task; 315 struct unmap_list list[1]; 316 317 list[0].lba = lba; 318 list[0].num = num_blocks; 319 task = iscsi_unmap_task(lun->context, 0, 0, 0, list, 1, 320 bdev_iscsi_command_cb, iscsi_io); 321 if (task == NULL) { 322 SPDK_ERRLOG("failed to get unmap_task\n"); 323 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 324 return; 325 } 326 } 327 328 static void 329 bdev_iscsi_reset_cb(struct iscsi_context *context __attribute__((unused)), int status, 330 void *command_data, void *private_data) 331 { 332 uint32_t tmf_response; 333 struct bdev_iscsi_io *iscsi_io = private_data; 334 335 tmf_response = *(uint32_t *)command_data; 336 if (tmf_response == ISCSI_TASK_FUNC_RESP_COMPLETE) { 337 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_SUCCESS); 338 } else { 339 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 340 } 341 } 342 343 static void 344 _bdev_iscsi_reset(void *_bdev_io) 345 { 346 int rc; 347 struct spdk_bdev_io *bdev_io = _bdev_io; 348 struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; 349 struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx; 350 struct iscsi_context *context = lun->context; 351 352 rc = iscsi_task_mgmt_lun_reset_async(context, lun->lun_id, 353 bdev_iscsi_reset_cb, iscsi_io); 354 if (rc != 0) { 355 SPDK_ERRLOG("failed to do iscsi reset\n"); 356 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 357 return; 358 } 359 } 360 361 static void 362 bdev_iscsi_reset(struct spdk_bdev_io *bdev_io) 363 { 364 struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; 365 spdk_thread_send_msg(lun->main_td, _bdev_iscsi_reset, bdev_io); 366 } 367 368 static int 369 bdev_iscsi_poll_lun(void *_lun) 370 { 371 struct bdev_iscsi_lun *lun = _lun; 372 struct pollfd pfd = {}; 373 374 pfd.fd = iscsi_get_fd(lun->context); 375 pfd.events = iscsi_which_events(lun->context); 376 377 if (poll(&pfd, 1, 0) < 0) { 378 SPDK_ERRLOG("poll failed\n"); 379 return SPDK_POLLER_IDLE; 380 } 381 382 if (pfd.revents != 0) { 383 if (iscsi_service(lun->context, pfd.revents) < 0) { 384 SPDK_ERRLOG("iscsi_service failed: %s\n", iscsi_get_error(lun->context)); 385 } 386 387 return SPDK_POLLER_BUSY; 388 } 389 390 return SPDK_POLLER_IDLE; 391 } 392 393 static int 394 bdev_iscsi_no_main_ch_poll(void *arg) 395 { 396 struct bdev_iscsi_lun *lun = arg; 397 enum spdk_thread_poller_rc rc = SPDK_POLLER_IDLE; 398 399 if (pthread_mutex_trylock(&lun->mutex)) { 400 /* Don't care about the error code here. */ 401 return SPDK_POLLER_IDLE; 402 } 403 404 if (lun->ch_count == 0) { 405 rc = bdev_iscsi_poll_lun(arg); 406 } 407 408 pthread_mutex_unlock(&lun->mutex); 409 return rc; 410 } 411 412 static void 413 bdev_iscsi_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, 414 bool success) 415 { 416 if (!success) { 417 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 418 return; 419 } 420 421 bdev_iscsi_readv((struct bdev_iscsi_lun *)bdev_io->bdev->ctxt, 422 (struct bdev_iscsi_io *)bdev_io->driver_ctx, 423 bdev_io->u.bdev.iovs, 424 bdev_io->u.bdev.iovcnt, 425 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen, 426 bdev_io->u.bdev.offset_blocks); 427 } 428 429 static void _bdev_iscsi_submit_request(void *_bdev_io) 430 { 431 struct spdk_bdev_io *bdev_io = _bdev_io; 432 struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx; 433 struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; 434 435 switch (bdev_io->type) { 436 case SPDK_BDEV_IO_TYPE_READ: 437 spdk_bdev_io_get_buf(bdev_io, bdev_iscsi_get_buf_cb, 438 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 439 break; 440 441 case SPDK_BDEV_IO_TYPE_WRITE: 442 bdev_iscsi_writev(lun, iscsi_io, 443 bdev_io->u.bdev.iovs, 444 bdev_io->u.bdev.iovcnt, 445 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen, 446 bdev_io->u.bdev.offset_blocks); 447 break; 448 case SPDK_BDEV_IO_TYPE_FLUSH: 449 bdev_iscsi_flush(lun, iscsi_io, 450 bdev_io->u.bdev.num_blocks, 451 ISCSI_IMMEDIATE_DATA_NO, 452 bdev_io->u.bdev.offset_blocks); 453 break; 454 case SPDK_BDEV_IO_TYPE_RESET: 455 bdev_iscsi_reset(bdev_io); 456 break; 457 case SPDK_BDEV_IO_TYPE_UNMAP: 458 bdev_iscsi_unmap(lun, iscsi_io, 459 bdev_io->u.bdev.offset_blocks, 460 bdev_io->u.bdev.num_blocks); 461 break; 462 default: 463 bdev_iscsi_io_complete(iscsi_io, SPDK_BDEV_IO_STATUS_FAILED); 464 break; 465 } 466 } 467 468 static void bdev_iscsi_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) 469 { 470 struct spdk_thread *submit_td = spdk_io_channel_get_thread(_ch); 471 struct bdev_iscsi_io *iscsi_io = (struct bdev_iscsi_io *)bdev_io->driver_ctx; 472 struct bdev_iscsi_lun *lun = (struct bdev_iscsi_lun *)bdev_io->bdev->ctxt; 473 474 if (lun->main_td != submit_td) { 475 iscsi_io->submit_td = submit_td; 476 spdk_thread_send_msg(lun->main_td, _bdev_iscsi_submit_request, bdev_io); 477 return; 478 } else { 479 iscsi_io->submit_td = NULL; 480 } 481 482 _bdev_iscsi_submit_request(bdev_io); 483 } 484 485 static bool 486 bdev_iscsi_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) 487 { 488 struct bdev_iscsi_lun *lun = ctx; 489 490 switch (io_type) { 491 case SPDK_BDEV_IO_TYPE_READ: 492 case SPDK_BDEV_IO_TYPE_WRITE: 493 case SPDK_BDEV_IO_TYPE_FLUSH: 494 case SPDK_BDEV_IO_TYPE_RESET: 495 return true; 496 497 case SPDK_BDEV_IO_TYPE_UNMAP: 498 return lun->unmap_supported; 499 default: 500 return false; 501 } 502 } 503 504 static int 505 bdev_iscsi_create_cb(void *io_device, void *ctx_buf) 506 { 507 struct bdev_iscsi_io_channel *ch = ctx_buf; 508 struct bdev_iscsi_lun *lun = io_device; 509 510 pthread_mutex_lock(&lun->mutex); 511 if (lun->ch_count == 0) { 512 assert(lun->main_td == NULL); 513 lun->main_td = spdk_get_thread(); 514 lun->poller = SPDK_POLLER_REGISTER(bdev_iscsi_poll_lun, lun, 0); 515 ch->lun = lun; 516 } 517 lun->ch_count++; 518 pthread_mutex_unlock(&lun->mutex); 519 520 return 0; 521 } 522 523 static void 524 _iscsi_destroy_cb(void *ctx) 525 { 526 struct bdev_iscsi_lun *lun = ctx; 527 528 pthread_mutex_lock(&lun->mutex); 529 530 assert(lun->main_td == spdk_get_thread()); 531 assert(lun->ch_count > 0); 532 533 lun->ch_count--; 534 if (lun->ch_count > 0) { 535 pthread_mutex_unlock(&lun->mutex); 536 return; 537 } 538 539 lun->main_td = NULL; 540 spdk_poller_unregister(&lun->poller); 541 542 pthread_mutex_unlock(&lun->mutex); 543 } 544 545 static void 546 bdev_iscsi_destroy_cb(void *io_device, void *ctx_buf) 547 { 548 struct bdev_iscsi_lun *lun = io_device; 549 struct spdk_thread *thread; 550 551 pthread_mutex_lock(&lun->mutex); 552 lun->ch_count--; 553 if (lun->ch_count == 0) { 554 assert(lun->main_td != NULL); 555 556 if (lun->main_td != spdk_get_thread()) { 557 /* The final channel was destroyed on a different thread 558 * than where the first channel was created. Pass a message 559 * to the main thread to unregister the poller. */ 560 lun->ch_count++; 561 thread = lun->main_td; 562 pthread_mutex_unlock(&lun->mutex); 563 spdk_thread_send_msg(thread, _iscsi_destroy_cb, lun); 564 return; 565 } 566 567 lun->main_td = NULL; 568 spdk_poller_unregister(&lun->poller); 569 } 570 pthread_mutex_unlock(&lun->mutex); 571 } 572 573 static struct spdk_io_channel * 574 bdev_iscsi_get_io_channel(void *ctx) 575 { 576 struct bdev_iscsi_lun *lun = ctx; 577 578 return spdk_get_io_channel(lun); 579 } 580 581 static int 582 bdev_iscsi_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 583 { 584 struct bdev_iscsi_lun *lun = ctx; 585 586 spdk_json_write_named_object_begin(w, "iscsi"); 587 spdk_json_write_named_string(w, "initiator_name", lun->initiator_iqn); 588 spdk_json_write_named_string(w, "url", lun->url); 589 spdk_json_write_object_end(w); 590 591 return 0; 592 } 593 594 static void 595 bdev_iscsi_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 596 { 597 struct bdev_iscsi_lun *lun = bdev->ctxt; 598 599 pthread_mutex_lock(&lun->mutex); 600 spdk_json_write_object_begin(w); 601 602 spdk_json_write_named_string(w, "method", "bdev_iscsi_create"); 603 604 spdk_json_write_named_object_begin(w, "params"); 605 spdk_json_write_named_string(w, "name", bdev->name); 606 spdk_json_write_named_string(w, "initiator_iqn", lun->initiator_iqn); 607 spdk_json_write_named_string(w, "url", lun->url); 608 spdk_json_write_object_end(w); 609 610 spdk_json_write_object_end(w); 611 pthread_mutex_unlock(&lun->mutex); 612 } 613 614 static const struct spdk_bdev_fn_table iscsi_fn_table = { 615 .destruct = bdev_iscsi_destruct, 616 .submit_request = bdev_iscsi_submit_request, 617 .io_type_supported = bdev_iscsi_io_type_supported, 618 .get_io_channel = bdev_iscsi_get_io_channel, 619 .dump_info_json = bdev_iscsi_dump_info_json, 620 .write_config_json = bdev_iscsi_write_config_json, 621 }; 622 623 static int 624 create_iscsi_lun(struct iscsi_context *context, int lun_id, char *url, char *initiator_iqn, 625 char *name, 626 uint64_t num_blocks, uint32_t block_size, struct spdk_bdev **bdev, bool unmap_supported) 627 { 628 struct bdev_iscsi_lun *lun; 629 int rc; 630 631 lun = calloc(sizeof(*lun), 1); 632 if (!lun) { 633 SPDK_ERRLOG("Unable to allocate enough memory for iscsi backend\n"); 634 return -ENOMEM; 635 } 636 637 lun->context = context; 638 lun->lun_id = lun_id; 639 lun->url = url; 640 lun->initiator_iqn = initiator_iqn; 641 642 pthread_mutex_init(&lun->mutex, NULL); 643 644 lun->bdev.name = name; 645 lun->bdev.product_name = "iSCSI LUN"; 646 lun->bdev.module = &g_iscsi_bdev_module; 647 lun->bdev.blocklen = block_size; 648 lun->bdev.blockcnt = num_blocks; 649 lun->bdev.ctxt = lun; 650 lun->unmap_supported = unmap_supported; 651 652 lun->bdev.fn_table = &iscsi_fn_table; 653 654 spdk_io_device_register(lun, bdev_iscsi_create_cb, bdev_iscsi_destroy_cb, 655 sizeof(struct bdev_iscsi_io_channel), 656 name); 657 rc = spdk_bdev_register(&lun->bdev); 658 if (rc) { 659 spdk_io_device_unregister(lun, NULL); 660 pthread_mutex_destroy(&lun->mutex); 661 free(lun); 662 return rc; 663 } 664 665 lun->no_main_ch_poller_td = spdk_get_thread(); 666 lun->no_main_ch_poller = SPDK_POLLER_REGISTER(bdev_iscsi_no_main_ch_poll, lun, 667 BDEV_ISCSI_NO_MAIN_CH_POLL_US); 668 669 *bdev = &lun->bdev; 670 return 0; 671 } 672 673 static void 674 iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status, 675 void *command_data, void *private_data) 676 { 677 struct bdev_iscsi_conn_req *req = private_data; 678 struct scsi_readcapacity16 *readcap16; 679 struct spdk_bdev *bdev = NULL; 680 struct scsi_task *task = command_data; 681 682 if (status != SPDK_SCSI_STATUS_GOOD) { 683 SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(iscsi)); 684 goto ret; 685 } 686 687 readcap16 = scsi_datain_unmarshall(task); 688 if (!readcap16) { 689 status = -ENOMEM; 690 goto ret; 691 } 692 693 status = create_iscsi_lun(req->context, req->lun, req->url, req->initiator_iqn, req->bdev_name, 694 readcap16->returned_lba + 1, readcap16->block_length, &bdev, req->unmap_supported); 695 if (status) { 696 SPDK_ERRLOG("Unable to create iscsi bdev: %s (%d)\n", spdk_strerror(-status), status); 697 } 698 699 ret: 700 scsi_free_scsi_task(task); 701 complete_conn_req(req, bdev, status); 702 } 703 704 static void 705 bdev_iscsi_inquiry_cb(struct iscsi_context *context, int status, void *_task, void *private_data) 706 { 707 struct scsi_task *task = _task; 708 struct scsi_inquiry_logical_block_provisioning *lbp_inq = NULL; 709 struct bdev_iscsi_conn_req *req = private_data; 710 711 if (status == SPDK_SCSI_STATUS_GOOD) { 712 lbp_inq = scsi_datain_unmarshall(task); 713 if (lbp_inq != NULL && lbp_inq->lbpu) { 714 req->unmap_supported = true; 715 } 716 } 717 718 task = iscsi_readcapacity16_task(context, req->lun, iscsi_readcapacity16_cb, req); 719 if (task) { 720 return; 721 } 722 723 SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context)); 724 complete_conn_req(req, NULL, status); 725 } 726 727 static void 728 iscsi_connect_cb(struct iscsi_context *iscsi, int status, 729 void *command_data, void *private_data) 730 { 731 struct bdev_iscsi_conn_req *req = private_data; 732 struct scsi_task *task; 733 734 if (status != SPDK_SCSI_STATUS_GOOD) { 735 goto ret; 736 } 737 738 task = iscsi_inquiry_task(iscsi, req->lun, 1, 739 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, 740 255, bdev_iscsi_inquiry_cb, req); 741 if (task) { 742 return; 743 } 744 745 ret: 746 SPDK_ERRLOG("iSCSI error: %s\n", iscsi_get_error(req->context)); 747 complete_conn_req(req, NULL, status); 748 } 749 750 static int 751 iscsi_bdev_conn_poll(void *arg) 752 { 753 struct bdev_iscsi_conn_req *req, *tmp; 754 struct pollfd pfd; 755 struct iscsi_context *context; 756 757 if (TAILQ_EMPTY(&g_iscsi_conn_req)) { 758 return SPDK_POLLER_IDLE; 759 } 760 761 TAILQ_FOREACH_SAFE(req, &g_iscsi_conn_req, link, tmp) { 762 context = req->context; 763 pfd.fd = iscsi_get_fd(context); 764 pfd.events = iscsi_which_events(context); 765 pfd.revents = 0; 766 if (poll(&pfd, 1, 0) < 0) { 767 SPDK_ERRLOG("poll failed\n"); 768 return SPDK_POLLER_BUSY; 769 } 770 771 if (pfd.revents != 0) { 772 if (iscsi_service(context, pfd.revents) < 0) { 773 SPDK_ERRLOG("iscsi_service failed: %s\n", iscsi_get_error(context)); 774 } 775 } 776 777 if (req->status == 0) { 778 /* 779 * The request completed successfully. 780 */ 781 free(req); 782 } else if (req->status > 0) { 783 /* 784 * An error has occurred during connecting. This req has already 785 * been removed from the g_iscsi_conn_req list, but we needed to 786 * wait until iscsi_service unwound before we could free the req. 787 */ 788 _bdev_iscsi_conn_req_free(req); 789 } 790 } 791 return SPDK_POLLER_BUSY; 792 } 793 794 int 795 create_iscsi_disk(const char *bdev_name, const char *url, const char *initiator_iqn, 796 spdk_bdev_iscsi_create_cb cb_fn, void *cb_arg) 797 { 798 struct bdev_iscsi_conn_req *req; 799 struct iscsi_url *iscsi_url = NULL; 800 int rc; 801 802 if (!bdev_name || !url || !initiator_iqn || strlen(initiator_iqn) == 0 || !cb_fn) { 803 return -EINVAL; 804 } 805 806 req = calloc(1, sizeof(struct bdev_iscsi_conn_req)); 807 if (!req) { 808 SPDK_ERRLOG("Cannot allocate pointer of struct bdev_iscsi_conn_req\n"); 809 return -ENOMEM; 810 } 811 812 req->status = SCSI_STATUS_GOOD; 813 req->bdev_name = strdup(bdev_name); 814 req->url = strdup(url); 815 req->initiator_iqn = strdup(initiator_iqn); 816 req->context = iscsi_create_context(initiator_iqn); 817 if (!req->bdev_name || !req->url || !req->initiator_iqn || !req->context) { 818 SPDK_ERRLOG("Out of memory\n"); 819 rc = -ENOMEM; 820 goto err; 821 } 822 823 req->create_cb = cb_fn; 824 req->create_cb_arg = cb_arg; 825 826 iscsi_url = iscsi_parse_full_url(req->context, url); 827 if (iscsi_url == NULL) { 828 SPDK_ERRLOG("could not parse URL: %s\n", iscsi_get_error(req->context)); 829 rc = -EINVAL; 830 goto err; 831 } 832 833 req->lun = iscsi_url->lun; 834 rc = iscsi_set_session_type(req->context, ISCSI_SESSION_NORMAL); 835 rc = rc ? rc : iscsi_set_header_digest(req->context, ISCSI_HEADER_DIGEST_NONE); 836 rc = rc ? rc : iscsi_set_targetname(req->context, iscsi_url->target); 837 rc = rc ? rc : iscsi_full_connect_async(req->context, iscsi_url->portal, iscsi_url->lun, 838 iscsi_connect_cb, req); 839 if (rc == 0 && iscsi_url->user[0] != '\0') { 840 rc = iscsi_set_initiator_username_pwd(req->context, iscsi_url->user, iscsi_url->passwd); 841 } 842 843 if (rc < 0) { 844 SPDK_ERRLOG("Failed to connect provided URL=%s: %s\n", url, iscsi_get_error(req->context)); 845 goto err; 846 } 847 848 iscsi_destroy_url(iscsi_url); 849 req->status = -1; 850 TAILQ_INSERT_TAIL(&g_iscsi_conn_req, req, link); 851 if (!g_conn_poller) { 852 g_conn_poller = SPDK_POLLER_REGISTER(iscsi_bdev_conn_poll, NULL, BDEV_ISCSI_CONNECTION_POLL_US); 853 } 854 855 return 0; 856 857 err: 858 /* iscsi_destroy_url() is not NULL-proof */ 859 if (iscsi_url) { 860 iscsi_destroy_url(iscsi_url); 861 } 862 863 if (req->context) { 864 iscsi_destroy_context(req->context); 865 } 866 867 free(req->initiator_iqn); 868 free(req->bdev_name); 869 free(req->url); 870 free(req); 871 return rc; 872 } 873 874 void 875 delete_iscsi_disk(struct spdk_bdev *bdev, spdk_delete_iscsi_complete cb_fn, void *cb_arg) 876 { 877 if (!bdev || bdev->module != &g_iscsi_bdev_module) { 878 cb_fn(cb_arg, -ENODEV); 879 return; 880 } 881 882 spdk_bdev_unregister(bdev, cb_fn, cb_arg); 883 } 884 885 static int 886 bdev_iscsi_initialize(void) 887 { 888 return 0; 889 } 890 891 SPDK_LOG_REGISTER_COMPONENT(iscsi_init) 892