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