1 /* 2 * QEMU Block driver for iSCSI images 3 * 4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com> 5 * Copyright (c) 2012-2017 Peter Lieven <pl@kamp.de> 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #include "qemu/osdep.h" 27 28 #include <poll.h> 29 #include <math.h> 30 #include <arpa/inet.h> 31 #include "sysemu/sysemu.h" 32 #include "qemu/config-file.h" 33 #include "qemu/error-report.h" 34 #include "qemu/bitops.h" 35 #include "qemu/bitmap.h" 36 #include "block/block-io.h" 37 #include "block/block_int.h" 38 #include "block/qdict.h" 39 #include "scsi/constants.h" 40 #include "qemu/iov.h" 41 #include "qemu/module.h" 42 #include "qemu/option.h" 43 #include "qemu/uuid.h" 44 #include "sysemu/replay.h" 45 #include "qapi/error.h" 46 #include "qapi/qapi-commands-machine.h" 47 #include "qapi/qmp/qdict.h" 48 #include "qapi/qmp/qstring.h" 49 #include "crypto/secret.h" 50 #include "scsi/utils.h" 51 #include "trace.h" 52 53 /* Conflict between scsi/utils.h and libiscsi! :( */ 54 #define SCSI_XFER_NONE ISCSI_XFER_NONE 55 #include <iscsi/iscsi.h> 56 #define inline __attribute__((gnu_inline)) /* required for libiscsi v1.9.0 */ 57 #include <iscsi/scsi-lowlevel.h> 58 #undef inline 59 #undef SCSI_XFER_NONE 60 QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE != (int)ISCSI_XFER_NONE); 61 62 #ifdef __linux__ 63 #include <scsi/sg.h> 64 #endif 65 66 typedef struct IscsiLun { 67 struct iscsi_context *iscsi; 68 AioContext *aio_context; 69 int lun; 70 enum scsi_inquiry_peripheral_device_type type; 71 int block_size; 72 uint64_t num_blocks; 73 int events; 74 QEMUTimer *nop_timer; 75 QEMUTimer *event_timer; 76 QemuMutex mutex; 77 struct scsi_inquiry_logical_block_provisioning lbp; 78 struct scsi_inquiry_block_limits bl; 79 struct scsi_inquiry_device_designator *dd; 80 unsigned char *zeroblock; 81 /* The allocmap tracks which clusters (pages) on the iSCSI target are 82 * allocated and which are not. In case a target returns zeros for 83 * unallocated pages (iscsilun->lprz) we can directly return zeros instead 84 * of reading zeros over the wire if a read request falls within an 85 * unallocated block. As there are 3 possible states we need 2 bitmaps to 86 * track. allocmap_valid keeps track if QEMU's information about a page is 87 * valid. allocmap tracks if a page is allocated or not. In case QEMU has no 88 * valid information about a page the corresponding allocmap entry should be 89 * switched to unallocated as well to force a new lookup of the allocation 90 * status as lookups are generally skipped if a page is suspect to be 91 * allocated. If a iSCSI target is opened with cache.direct = on the 92 * allocmap_valid does not exist turning all cached information invalid so 93 * that a fresh lookup is made for any page even if allocmap entry returns 94 * it's unallocated. */ 95 unsigned long *allocmap; 96 unsigned long *allocmap_valid; 97 long allocmap_size; 98 int cluster_size; 99 bool use_16_for_rw; 100 bool write_protected; 101 bool lbpme; 102 bool lbprz; 103 bool dpofua; 104 bool has_write_same; 105 bool request_timed_out; 106 } IscsiLun; 107 108 typedef struct IscsiTask { 109 int status; 110 int complete; 111 int retries; 112 int do_retry; 113 struct scsi_task *task; 114 Coroutine *co; 115 IscsiLun *iscsilun; 116 QEMUTimer retry_timer; 117 int err_code; 118 char *err_str; 119 } IscsiTask; 120 121 typedef struct IscsiAIOCB { 122 BlockAIOCB common; 123 QEMUBH *bh; 124 IscsiLun *iscsilun; 125 struct scsi_task *task; 126 int status; 127 int64_t sector_num; 128 int nb_sectors; 129 int ret; 130 #ifdef __linux__ 131 sg_io_hdr_t *ioh; 132 #endif 133 bool cancelled; 134 } IscsiAIOCB; 135 136 /* libiscsi uses time_t so its enough to process events every second */ 137 #define EVENT_INTERVAL 1000 138 #define NOP_INTERVAL 5000 139 #define MAX_NOP_FAILURES 3 140 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times) 141 static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768}; 142 143 /* this threshold is a trade-off knob to choose between 144 * the potential additional overhead of an extra GET_LBA_STATUS request 145 * vs. unnecessarily reading a lot of zero sectors over the wire. 146 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES 147 * sectors we check the allocation status of the area covered by the 148 * request first if the allocationmap indicates that the area might be 149 * unallocated. */ 150 #define ISCSI_CHECKALLOC_THRES 64 151 152 #ifdef __linux__ 153 154 static void 155 iscsi_bh_cb(void *p) 156 { 157 IscsiAIOCB *acb = p; 158 159 qemu_bh_delete(acb->bh); 160 161 acb->common.cb(acb->common.opaque, acb->status); 162 163 if (acb->task != NULL) { 164 scsi_free_scsi_task(acb->task); 165 acb->task = NULL; 166 } 167 168 qemu_aio_unref(acb); 169 } 170 171 static void 172 iscsi_schedule_bh(IscsiAIOCB *acb) 173 { 174 if (acb->bh) { 175 return; 176 } 177 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb); 178 qemu_bh_schedule(acb->bh); 179 } 180 181 #endif 182 183 static void iscsi_co_generic_bh_cb(void *opaque) 184 { 185 struct IscsiTask *iTask = opaque; 186 187 iTask->complete = 1; 188 aio_co_wake(iTask->co); 189 } 190 191 static void iscsi_retry_timer_expired(void *opaque) 192 { 193 struct IscsiTask *iTask = opaque; 194 iTask->complete = 1; 195 if (iTask->co) { 196 aio_co_wake(iTask->co); 197 } 198 } 199 200 static inline unsigned exp_random(double mean) 201 { 202 return -mean * log((double)rand() / RAND_MAX); 203 } 204 205 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in 206 * libiscsi 1.10.0, together with other constants we need. Use it as 207 * a hint that we have to define them ourselves if needed, to keep the 208 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for 209 * the test because SCSI_STATUS_* is an enum. 210 * 211 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes 212 * an enum, check against the LIBISCSI_API_VERSION macro, which was 213 * introduced in 1.11.0. If it is present, there is no need to define 214 * anything. 215 */ 216 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \ 217 !defined(LIBISCSI_API_VERSION) 218 #define SCSI_STATUS_TASK_SET_FULL 0x28 219 #define SCSI_STATUS_TIMEOUT 0x0f000002 220 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600 221 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00 222 #endif 223 224 #ifndef LIBISCSI_API_VERSION 225 #define LIBISCSI_API_VERSION 20130701 226 #endif 227 228 static int iscsi_translate_sense(struct scsi_sense *sense) 229 { 230 return scsi_sense_to_errno(sense->key, 231 (sense->ascq & 0xFF00) >> 8, 232 sense->ascq & 0xFF); 233 } 234 235 /* Called (via iscsi_service) with QemuMutex held. */ 236 static void 237 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status, 238 void *command_data, void *opaque) 239 { 240 struct IscsiTask *iTask = opaque; 241 struct scsi_task *task = command_data; 242 243 iTask->status = status; 244 iTask->do_retry = 0; 245 iTask->err_code = 0; 246 iTask->task = task; 247 248 if (status != SCSI_STATUS_GOOD) { 249 iTask->err_code = -EIO; 250 if (iTask->retries++ < ISCSI_CMD_RETRIES) { 251 if (status == SCSI_STATUS_BUSY || 252 status == SCSI_STATUS_TIMEOUT || 253 status == SCSI_STATUS_TASK_SET_FULL) { 254 unsigned retry_time = 255 exp_random(iscsi_retry_times[iTask->retries - 1]); 256 if (status == SCSI_STATUS_TIMEOUT) { 257 /* make sure the request is rescheduled AFTER the 258 * reconnect is initiated */ 259 retry_time = EVENT_INTERVAL * 2; 260 iTask->iscsilun->request_timed_out = true; 261 } 262 error_report("iSCSI Busy/TaskSetFull/TimeOut" 263 " (retry #%u in %u ms): %s", 264 iTask->retries, retry_time, 265 iscsi_get_error(iscsi)); 266 aio_timer_init(iTask->iscsilun->aio_context, 267 &iTask->retry_timer, QEMU_CLOCK_REALTIME, 268 SCALE_MS, iscsi_retry_timer_expired, iTask); 269 timer_mod(&iTask->retry_timer, 270 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time); 271 iTask->do_retry = 1; 272 } else if (status == SCSI_STATUS_CHECK_CONDITION) { 273 int error = iscsi_translate_sense(&task->sense); 274 if (error == EAGAIN) { 275 error_report("iSCSI CheckCondition: %s", 276 iscsi_get_error(iscsi)); 277 iTask->do_retry = 1; 278 } else { 279 iTask->err_code = -error; 280 iTask->err_str = g_strdup(iscsi_get_error(iscsi)); 281 } 282 } 283 } 284 } 285 286 if (iTask->co) { 287 replay_bh_schedule_oneshot_event(iTask->iscsilun->aio_context, 288 iscsi_co_generic_bh_cb, iTask); 289 } else { 290 iTask->complete = 1; 291 } 292 } 293 294 static void coroutine_fn 295 iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask) 296 { 297 *iTask = (struct IscsiTask) { 298 .co = qemu_coroutine_self(), 299 .iscsilun = iscsilun, 300 }; 301 } 302 303 #ifdef __linux__ 304 305 /* Called (via iscsi_service) with QemuMutex held. */ 306 static void 307 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data, 308 void *private_data) 309 { 310 IscsiAIOCB *acb = private_data; 311 312 /* If the command callback hasn't been called yet, drop the task */ 313 if (!acb->bh) { 314 /* Call iscsi_aio_ioctl_cb() with SCSI_STATUS_CANCELLED */ 315 iscsi_scsi_cancel_task(iscsi, acb->task); 316 } 317 318 qemu_aio_unref(acb); /* acquired in iscsi_aio_cancel() */ 319 } 320 321 static void 322 iscsi_aio_cancel(BlockAIOCB *blockacb) 323 { 324 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb; 325 IscsiLun *iscsilun = acb->iscsilun; 326 327 WITH_QEMU_LOCK_GUARD(&iscsilun->mutex) { 328 329 /* If it was cancelled or completed already, our work is done here */ 330 if (acb->cancelled || acb->status != -EINPROGRESS) { 331 return; 332 } 333 334 acb->cancelled = true; 335 336 qemu_aio_ref(acb); /* released in iscsi_abort_task_cb() */ 337 338 /* send a task mgmt call to the target to cancel the task on the target */ 339 if (iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, 340 iscsi_abort_task_cb, acb) < 0) { 341 qemu_aio_unref(acb); /* since iscsi_abort_task_cb() won't be called */ 342 } 343 } 344 } 345 346 static const AIOCBInfo iscsi_aiocb_info = { 347 .aiocb_size = sizeof(IscsiAIOCB), 348 .cancel_async = iscsi_aio_cancel, 349 }; 350 351 #endif 352 353 static void iscsi_process_read(void *arg); 354 static void iscsi_process_write(void *arg); 355 356 /* Called with QemuMutex held. */ 357 static void 358 iscsi_set_events(IscsiLun *iscsilun) 359 { 360 struct iscsi_context *iscsi = iscsilun->iscsi; 361 int ev = iscsi_which_events(iscsi); 362 363 if (ev != iscsilun->events) { 364 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi), 365 false, 366 (ev & POLLIN) ? iscsi_process_read : NULL, 367 (ev & POLLOUT) ? iscsi_process_write : NULL, 368 NULL, NULL, 369 iscsilun); 370 iscsilun->events = ev; 371 } 372 } 373 374 static void iscsi_timed_check_events(void *opaque) 375 { 376 IscsiLun *iscsilun = opaque; 377 378 WITH_QEMU_LOCK_GUARD(&iscsilun->mutex) { 379 /* check for timed out requests */ 380 iscsi_service(iscsilun->iscsi, 0); 381 382 if (iscsilun->request_timed_out) { 383 iscsilun->request_timed_out = false; 384 iscsi_reconnect(iscsilun->iscsi); 385 } 386 387 /* 388 * newer versions of libiscsi may return zero events. Ensure we are 389 * able to return to service once this situation changes. 390 */ 391 iscsi_set_events(iscsilun); 392 } 393 394 timer_mod(iscsilun->event_timer, 395 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); 396 } 397 398 static void 399 iscsi_process_read(void *arg) 400 { 401 IscsiLun *iscsilun = arg; 402 struct iscsi_context *iscsi = iscsilun->iscsi; 403 404 qemu_mutex_lock(&iscsilun->mutex); 405 iscsi_service(iscsi, POLLIN); 406 iscsi_set_events(iscsilun); 407 qemu_mutex_unlock(&iscsilun->mutex); 408 } 409 410 static void 411 iscsi_process_write(void *arg) 412 { 413 IscsiLun *iscsilun = arg; 414 struct iscsi_context *iscsi = iscsilun->iscsi; 415 416 qemu_mutex_lock(&iscsilun->mutex); 417 iscsi_service(iscsi, POLLOUT); 418 iscsi_set_events(iscsilun); 419 qemu_mutex_unlock(&iscsilun->mutex); 420 } 421 422 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun) 423 { 424 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE; 425 } 426 427 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun) 428 { 429 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size; 430 } 431 432 static bool is_byte_request_lun_aligned(int64_t offset, int64_t bytes, 433 IscsiLun *iscsilun) 434 { 435 if (offset % iscsilun->block_size || bytes % iscsilun->block_size) { 436 error_report("iSCSI misaligned request: " 437 "iscsilun->block_size %u, offset %" PRIi64 438 ", bytes %" PRIi64, 439 iscsilun->block_size, offset, bytes); 440 return false; 441 } 442 return true; 443 } 444 445 static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors, 446 IscsiLun *iscsilun) 447 { 448 assert(nb_sectors <= BDRV_REQUEST_MAX_SECTORS); 449 return is_byte_request_lun_aligned(sector_num << BDRV_SECTOR_BITS, 450 nb_sectors << BDRV_SECTOR_BITS, 451 iscsilun); 452 } 453 454 static void iscsi_allocmap_free(IscsiLun *iscsilun) 455 { 456 g_free(iscsilun->allocmap); 457 g_free(iscsilun->allocmap_valid); 458 iscsilun->allocmap = NULL; 459 iscsilun->allocmap_valid = NULL; 460 } 461 462 463 static int iscsi_allocmap_init(IscsiLun *iscsilun, int open_flags) 464 { 465 iscsi_allocmap_free(iscsilun); 466 467 assert(iscsilun->cluster_size); 468 iscsilun->allocmap_size = 469 DIV_ROUND_UP(iscsilun->num_blocks * iscsilun->block_size, 470 iscsilun->cluster_size); 471 472 iscsilun->allocmap = bitmap_try_new(iscsilun->allocmap_size); 473 if (!iscsilun->allocmap) { 474 return -ENOMEM; 475 } 476 477 if (open_flags & BDRV_O_NOCACHE) { 478 /* when cache.direct = on all allocmap entries are 479 * treated as invalid to force a relookup of the block 480 * status on every read request */ 481 return 0; 482 } 483 484 iscsilun->allocmap_valid = bitmap_try_new(iscsilun->allocmap_size); 485 if (!iscsilun->allocmap_valid) { 486 /* if we are under memory pressure free the allocmap as well */ 487 iscsi_allocmap_free(iscsilun); 488 return -ENOMEM; 489 } 490 491 return 0; 492 } 493 494 static void 495 iscsi_allocmap_update(IscsiLun *iscsilun, int64_t offset, 496 int64_t bytes, bool allocated, bool valid) 497 { 498 int64_t cl_num_expanded, nb_cls_expanded, cl_num_shrunk, nb_cls_shrunk; 499 500 if (iscsilun->allocmap == NULL) { 501 return; 502 } 503 /* expand to entirely contain all affected clusters */ 504 assert(iscsilun->cluster_size); 505 cl_num_expanded = offset / iscsilun->cluster_size; 506 nb_cls_expanded = DIV_ROUND_UP(offset + bytes, 507 iscsilun->cluster_size) - cl_num_expanded; 508 /* shrink to touch only completely contained clusters */ 509 cl_num_shrunk = DIV_ROUND_UP(offset, iscsilun->cluster_size); 510 nb_cls_shrunk = (offset + bytes) / iscsilun->cluster_size - cl_num_shrunk; 511 if (allocated) { 512 bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded); 513 } else { 514 if (nb_cls_shrunk > 0) { 515 bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk); 516 } 517 } 518 519 if (iscsilun->allocmap_valid == NULL) { 520 return; 521 } 522 if (valid) { 523 if (nb_cls_shrunk > 0) { 524 bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk); 525 } 526 } else { 527 bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded, 528 nb_cls_expanded); 529 } 530 } 531 532 static void 533 iscsi_allocmap_set_allocated(IscsiLun *iscsilun, int64_t offset, 534 int64_t bytes) 535 { 536 iscsi_allocmap_update(iscsilun, offset, bytes, true, true); 537 } 538 539 static void 540 iscsi_allocmap_set_unallocated(IscsiLun *iscsilun, int64_t offset, 541 int64_t bytes) 542 { 543 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update 544 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid. 545 */ 546 iscsi_allocmap_update(iscsilun, offset, bytes, false, true); 547 } 548 549 static void iscsi_allocmap_set_invalid(IscsiLun *iscsilun, int64_t offset, 550 int64_t bytes) 551 { 552 iscsi_allocmap_update(iscsilun, offset, bytes, false, false); 553 } 554 555 static void iscsi_allocmap_invalidate(IscsiLun *iscsilun) 556 { 557 if (iscsilun->allocmap) { 558 bitmap_zero(iscsilun->allocmap, iscsilun->allocmap_size); 559 } 560 if (iscsilun->allocmap_valid) { 561 bitmap_zero(iscsilun->allocmap_valid, iscsilun->allocmap_size); 562 } 563 } 564 565 static inline bool 566 iscsi_allocmap_is_allocated(IscsiLun *iscsilun, int64_t offset, 567 int64_t bytes) 568 { 569 unsigned long size; 570 if (iscsilun->allocmap == NULL) { 571 return true; 572 } 573 assert(iscsilun->cluster_size); 574 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size); 575 return !(find_next_bit(iscsilun->allocmap, size, 576 offset / iscsilun->cluster_size) == size); 577 } 578 579 static inline bool iscsi_allocmap_is_valid(IscsiLun *iscsilun, 580 int64_t offset, int64_t bytes) 581 { 582 unsigned long size; 583 if (iscsilun->allocmap_valid == NULL) { 584 return false; 585 } 586 assert(iscsilun->cluster_size); 587 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size); 588 return (find_next_zero_bit(iscsilun->allocmap_valid, size, 589 offset / iscsilun->cluster_size) == size); 590 } 591 592 static void coroutine_fn iscsi_co_wait_for_task(IscsiTask *iTask, 593 IscsiLun *iscsilun) 594 { 595 while (!iTask->complete) { 596 iscsi_set_events(iscsilun); 597 qemu_mutex_unlock(&iscsilun->mutex); 598 qemu_coroutine_yield(); 599 qemu_mutex_lock(&iscsilun->mutex); 600 } 601 } 602 603 static int coroutine_fn 604 iscsi_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 605 QEMUIOVector *iov, int flags) 606 { 607 IscsiLun *iscsilun = bs->opaque; 608 struct IscsiTask iTask; 609 uint64_t lba; 610 uint32_t num_sectors; 611 bool fua = flags & BDRV_REQ_FUA; 612 int r = 0; 613 614 if (fua) { 615 assert(iscsilun->dpofua); 616 } 617 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { 618 return -EINVAL; 619 } 620 621 if (bs->bl.max_transfer) { 622 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer); 623 } 624 625 lba = sector_qemu2lun(sector_num, iscsilun); 626 num_sectors = sector_qemu2lun(nb_sectors, iscsilun); 627 iscsi_co_init_iscsitask(iscsilun, &iTask); 628 qemu_mutex_lock(&iscsilun->mutex); 629 retry: 630 if (iscsilun->use_16_for_rw) { 631 #if LIBISCSI_API_VERSION >= (20160603) 632 iTask.task = iscsi_write16_iov_task(iscsilun->iscsi, iscsilun->lun, lba, 633 NULL, num_sectors * iscsilun->block_size, 634 iscsilun->block_size, 0, 0, fua, 0, 0, 635 iscsi_co_generic_cb, &iTask, 636 (struct scsi_iovec *)iov->iov, iov->niov); 637 } else { 638 iTask.task = iscsi_write10_iov_task(iscsilun->iscsi, iscsilun->lun, lba, 639 NULL, num_sectors * iscsilun->block_size, 640 iscsilun->block_size, 0, 0, fua, 0, 0, 641 iscsi_co_generic_cb, &iTask, 642 (struct scsi_iovec *)iov->iov, iov->niov); 643 } 644 #else 645 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba, 646 NULL, num_sectors * iscsilun->block_size, 647 iscsilun->block_size, 0, 0, fua, 0, 0, 648 iscsi_co_generic_cb, &iTask); 649 } else { 650 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba, 651 NULL, num_sectors * iscsilun->block_size, 652 iscsilun->block_size, 0, 0, fua, 0, 0, 653 iscsi_co_generic_cb, &iTask); 654 } 655 #endif 656 if (iTask.task == NULL) { 657 qemu_mutex_unlock(&iscsilun->mutex); 658 return -ENOMEM; 659 } 660 #if LIBISCSI_API_VERSION < (20160603) 661 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov, 662 iov->niov); 663 #endif 664 iscsi_co_wait_for_task(&iTask, iscsilun); 665 666 if (iTask.task != NULL) { 667 scsi_free_scsi_task(iTask.task); 668 iTask.task = NULL; 669 } 670 671 if (iTask.do_retry) { 672 iTask.complete = 0; 673 goto retry; 674 } 675 676 if (iTask.status != SCSI_STATUS_GOOD) { 677 iscsi_allocmap_set_invalid(iscsilun, sector_num * BDRV_SECTOR_SIZE, 678 nb_sectors * BDRV_SECTOR_SIZE); 679 error_report("iSCSI WRITE10/16 failed at lba %" PRIu64 ": %s", lba, 680 iTask.err_str); 681 r = iTask.err_code; 682 goto out_unlock; 683 } 684 685 iscsi_allocmap_set_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE, 686 nb_sectors * BDRV_SECTOR_SIZE); 687 688 out_unlock: 689 qemu_mutex_unlock(&iscsilun->mutex); 690 g_free(iTask.err_str); 691 return r; 692 } 693 694 695 696 static int coroutine_fn iscsi_co_block_status(BlockDriverState *bs, 697 bool want_zero, int64_t offset, 698 int64_t bytes, int64_t *pnum, 699 int64_t *map, 700 BlockDriverState **file) 701 { 702 IscsiLun *iscsilun = bs->opaque; 703 struct scsi_get_lba_status *lbas = NULL; 704 struct scsi_lba_status_descriptor *lbasd = NULL; 705 struct IscsiTask iTask; 706 uint64_t lba, max_bytes; 707 int ret; 708 709 iscsi_co_init_iscsitask(iscsilun, &iTask); 710 711 assert(QEMU_IS_ALIGNED(offset | bytes, iscsilun->block_size)); 712 713 /* default to all sectors allocated */ 714 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; 715 if (map) { 716 *map = offset; 717 } 718 *pnum = bytes; 719 720 /* LUN does not support logical block provisioning */ 721 if (!iscsilun->lbpme) { 722 goto out; 723 } 724 725 lba = offset / iscsilun->block_size; 726 max_bytes = (iscsilun->num_blocks - lba) * iscsilun->block_size; 727 728 qemu_mutex_lock(&iscsilun->mutex); 729 retry: 730 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun, 731 lba, 8 + 16, iscsi_co_generic_cb, 732 &iTask) == NULL) { 733 ret = -ENOMEM; 734 goto out_unlock; 735 } 736 iscsi_co_wait_for_task(&iTask, iscsilun); 737 738 if (iTask.do_retry) { 739 if (iTask.task != NULL) { 740 scsi_free_scsi_task(iTask.task); 741 iTask.task = NULL; 742 } 743 iTask.complete = 0; 744 goto retry; 745 } 746 747 if (iTask.status != SCSI_STATUS_GOOD) { 748 /* in case the get_lba_status_callout fails (i.e. 749 * because the device is busy or the cmd is not 750 * supported) we pretend all blocks are allocated 751 * for backwards compatibility */ 752 error_report("iSCSI GET_LBA_STATUS failed at lba %" PRIu64 ": %s", 753 lba, iTask.err_str); 754 goto out_unlock; 755 } 756 757 lbas = scsi_datain_unmarshall(iTask.task); 758 if (lbas == NULL || lbas->num_descriptors == 0) { 759 ret = -EIO; 760 goto out_unlock; 761 } 762 763 lbasd = &lbas->descriptors[0]; 764 765 if (lba != lbasd->lba) { 766 ret = -EIO; 767 goto out_unlock; 768 } 769 770 *pnum = MIN((int64_t) lbasd->num_blocks * iscsilun->block_size, max_bytes); 771 772 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED || 773 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) { 774 ret &= ~BDRV_BLOCK_DATA; 775 if (iscsilun->lbprz) { 776 ret |= BDRV_BLOCK_ZERO; 777 } 778 } 779 780 if (ret & BDRV_BLOCK_ZERO) { 781 iscsi_allocmap_set_unallocated(iscsilun, offset, *pnum); 782 } else { 783 iscsi_allocmap_set_allocated(iscsilun, offset, *pnum); 784 } 785 786 out_unlock: 787 qemu_mutex_unlock(&iscsilun->mutex); 788 g_free(iTask.err_str); 789 out: 790 if (iTask.task != NULL) { 791 scsi_free_scsi_task(iTask.task); 792 } 793 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID && file) { 794 *file = bs; 795 } 796 return ret; 797 } 798 799 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs, 800 int64_t sector_num, int nb_sectors, 801 QEMUIOVector *iov) 802 { 803 IscsiLun *iscsilun = bs->opaque; 804 struct IscsiTask iTask; 805 uint64_t lba; 806 uint32_t num_sectors; 807 int r = 0; 808 809 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { 810 return -EINVAL; 811 } 812 813 if (bs->bl.max_transfer) { 814 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer); 815 } 816 817 /* if cache.direct is off and we have a valid entry in our allocation map 818 * we can skip checking the block status and directly return zeroes if 819 * the request falls within an unallocated area */ 820 if (iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE, 821 nb_sectors * BDRV_SECTOR_SIZE) && 822 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE, 823 nb_sectors * BDRV_SECTOR_SIZE)) { 824 qemu_iovec_memset(iov, 0, 0x00, iov->size); 825 return 0; 826 } 827 828 if (nb_sectors >= ISCSI_CHECKALLOC_THRES && 829 !iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE, 830 nb_sectors * BDRV_SECTOR_SIZE) && 831 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE, 832 nb_sectors * BDRV_SECTOR_SIZE)) { 833 int64_t pnum; 834 /* check the block status from the beginning of the cluster 835 * containing the start sector */ 836 int64_t head; 837 int ret; 838 839 assert(iscsilun->cluster_size); 840 head = (sector_num * BDRV_SECTOR_SIZE) % iscsilun->cluster_size; 841 ret = iscsi_co_block_status(bs, true, 842 sector_num * BDRV_SECTOR_SIZE - head, 843 BDRV_REQUEST_MAX_BYTES, &pnum, NULL, NULL); 844 if (ret < 0) { 845 return ret; 846 } 847 /* if the whole request falls into an unallocated area we can avoid 848 * reading and directly return zeroes instead */ 849 if (ret & BDRV_BLOCK_ZERO && 850 pnum >= nb_sectors * BDRV_SECTOR_SIZE + head) { 851 qemu_iovec_memset(iov, 0, 0x00, iov->size); 852 return 0; 853 } 854 } 855 856 lba = sector_qemu2lun(sector_num, iscsilun); 857 num_sectors = sector_qemu2lun(nb_sectors, iscsilun); 858 859 iscsi_co_init_iscsitask(iscsilun, &iTask); 860 qemu_mutex_lock(&iscsilun->mutex); 861 retry: 862 if (iscsilun->use_16_for_rw) { 863 #if LIBISCSI_API_VERSION >= (20160603) 864 iTask.task = iscsi_read16_iov_task(iscsilun->iscsi, iscsilun->lun, lba, 865 num_sectors * iscsilun->block_size, 866 iscsilun->block_size, 0, 0, 0, 0, 0, 867 iscsi_co_generic_cb, &iTask, 868 (struct scsi_iovec *)iov->iov, iov->niov); 869 } else { 870 iTask.task = iscsi_read10_iov_task(iscsilun->iscsi, iscsilun->lun, lba, 871 num_sectors * iscsilun->block_size, 872 iscsilun->block_size, 873 0, 0, 0, 0, 0, 874 iscsi_co_generic_cb, &iTask, 875 (struct scsi_iovec *)iov->iov, iov->niov); 876 } 877 #else 878 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba, 879 num_sectors * iscsilun->block_size, 880 iscsilun->block_size, 0, 0, 0, 0, 0, 881 iscsi_co_generic_cb, &iTask); 882 } else { 883 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba, 884 num_sectors * iscsilun->block_size, 885 iscsilun->block_size, 886 0, 0, 0, 0, 0, 887 iscsi_co_generic_cb, &iTask); 888 } 889 #endif 890 if (iTask.task == NULL) { 891 qemu_mutex_unlock(&iscsilun->mutex); 892 return -ENOMEM; 893 } 894 #if LIBISCSI_API_VERSION < (20160603) 895 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov); 896 #endif 897 898 iscsi_co_wait_for_task(&iTask, iscsilun); 899 if (iTask.task != NULL) { 900 scsi_free_scsi_task(iTask.task); 901 iTask.task = NULL; 902 } 903 904 if (iTask.do_retry) { 905 iTask.complete = 0; 906 goto retry; 907 } 908 909 if (iTask.status != SCSI_STATUS_GOOD) { 910 error_report("iSCSI READ10/16 failed at lba %" PRIu64 ": %s", 911 lba, iTask.err_str); 912 r = iTask.err_code; 913 } 914 915 qemu_mutex_unlock(&iscsilun->mutex); 916 g_free(iTask.err_str); 917 return r; 918 } 919 920 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs) 921 { 922 IscsiLun *iscsilun = bs->opaque; 923 struct IscsiTask iTask; 924 int r = 0; 925 926 iscsi_co_init_iscsitask(iscsilun, &iTask); 927 qemu_mutex_lock(&iscsilun->mutex); 928 retry: 929 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0, 930 0, iscsi_co_generic_cb, &iTask) == NULL) { 931 qemu_mutex_unlock(&iscsilun->mutex); 932 return -ENOMEM; 933 } 934 935 iscsi_co_wait_for_task(&iTask, iscsilun); 936 937 if (iTask.task != NULL) { 938 scsi_free_scsi_task(iTask.task); 939 iTask.task = NULL; 940 } 941 942 if (iTask.do_retry) { 943 iTask.complete = 0; 944 goto retry; 945 } 946 947 if (iTask.status != SCSI_STATUS_GOOD) { 948 error_report("iSCSI SYNCHRONIZECACHE10 failed: %s", iTask.err_str); 949 r = iTask.err_code; 950 } 951 952 qemu_mutex_unlock(&iscsilun->mutex); 953 g_free(iTask.err_str); 954 return r; 955 } 956 957 #ifdef __linux__ 958 /* Called (via iscsi_service) with QemuMutex held. */ 959 static void 960 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, 961 void *command_data, void *opaque) 962 { 963 IscsiAIOCB *acb = opaque; 964 965 if (status == SCSI_STATUS_CANCELLED) { 966 if (!acb->bh) { 967 acb->status = -ECANCELED; 968 iscsi_schedule_bh(acb); 969 } 970 return; 971 } 972 973 acb->status = 0; 974 if (status < 0) { 975 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s", 976 iscsi_get_error(iscsi)); 977 acb->status = -iscsi_translate_sense(&acb->task->sense); 978 } 979 980 acb->ioh->driver_status = 0; 981 acb->ioh->host_status = 0; 982 acb->ioh->resid = 0; 983 acb->ioh->status = status; 984 985 #define SG_ERR_DRIVER_SENSE 0x08 986 987 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) { 988 int ss; 989 990 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE; 991 992 acb->ioh->sb_len_wr = acb->task->datain.size - 2; 993 ss = MIN(acb->ioh->mx_sb_len, acb->ioh->sb_len_wr); 994 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss); 995 } 996 997 iscsi_schedule_bh(acb); 998 } 999 1000 static void iscsi_ioctl_bh_completion(void *opaque) 1001 { 1002 IscsiAIOCB *acb = opaque; 1003 1004 qemu_bh_delete(acb->bh); 1005 acb->common.cb(acb->common.opaque, acb->ret); 1006 qemu_aio_unref(acb); 1007 } 1008 1009 static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf) 1010 { 1011 BlockDriverState *bs = acb->common.bs; 1012 IscsiLun *iscsilun = bs->opaque; 1013 int ret = 0; 1014 1015 switch (req) { 1016 case SG_GET_VERSION_NUM: 1017 *(int *)buf = 30000; 1018 break; 1019 case SG_GET_SCSI_ID: 1020 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type; 1021 break; 1022 default: 1023 ret = -EINVAL; 1024 } 1025 assert(!acb->bh); 1026 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), 1027 iscsi_ioctl_bh_completion, acb); 1028 acb->ret = ret; 1029 qemu_bh_schedule(acb->bh); 1030 } 1031 1032 static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, 1033 unsigned long int req, void *buf, 1034 BlockCompletionFunc *cb, void *opaque) 1035 { 1036 IscsiLun *iscsilun = bs->opaque; 1037 struct iscsi_context *iscsi = iscsilun->iscsi; 1038 struct iscsi_data data; 1039 IscsiAIOCB *acb; 1040 1041 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque); 1042 1043 acb->iscsilun = iscsilun; 1044 acb->bh = NULL; 1045 acb->status = -EINPROGRESS; 1046 acb->ioh = buf; 1047 acb->cancelled = false; 1048 1049 if (req != SG_IO) { 1050 iscsi_ioctl_handle_emulated(acb, req, buf); 1051 return &acb->common; 1052 } 1053 1054 if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) { 1055 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)", 1056 acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE); 1057 qemu_aio_unref(acb); 1058 return NULL; 1059 } 1060 1061 acb->task = malloc(sizeof(struct scsi_task)); 1062 if (acb->task == NULL) { 1063 error_report("iSCSI: Failed to allocate task for scsi command. %s", 1064 iscsi_get_error(iscsi)); 1065 qemu_aio_unref(acb); 1066 return NULL; 1067 } 1068 memset(acb->task, 0, sizeof(struct scsi_task)); 1069 1070 switch (acb->ioh->dxfer_direction) { 1071 case SG_DXFER_TO_DEV: 1072 acb->task->xfer_dir = SCSI_XFER_WRITE; 1073 break; 1074 case SG_DXFER_FROM_DEV: 1075 acb->task->xfer_dir = SCSI_XFER_READ; 1076 break; 1077 default: 1078 acb->task->xfer_dir = SCSI_XFER_NONE; 1079 break; 1080 } 1081 1082 acb->task->cdb_size = acb->ioh->cmd_len; 1083 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len); 1084 acb->task->expxferlen = acb->ioh->dxfer_len; 1085 1086 data.size = 0; 1087 qemu_mutex_lock(&iscsilun->mutex); 1088 if (acb->task->xfer_dir == SCSI_XFER_WRITE) { 1089 if (acb->ioh->iovec_count == 0) { 1090 data.data = acb->ioh->dxferp; 1091 data.size = acb->ioh->dxfer_len; 1092 } else { 1093 scsi_task_set_iov_out(acb->task, 1094 (struct scsi_iovec *) acb->ioh->dxferp, 1095 acb->ioh->iovec_count); 1096 } 1097 } 1098 1099 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, 1100 iscsi_aio_ioctl_cb, 1101 (data.size > 0) ? &data : NULL, 1102 acb) != 0) { 1103 qemu_mutex_unlock(&iscsilun->mutex); 1104 scsi_free_scsi_task(acb->task); 1105 qemu_aio_unref(acb); 1106 return NULL; 1107 } 1108 1109 /* tell libiscsi to read straight into the buffer we got from ioctl */ 1110 if (acb->task->xfer_dir == SCSI_XFER_READ) { 1111 if (acb->ioh->iovec_count == 0) { 1112 scsi_task_add_data_in_buffer(acb->task, 1113 acb->ioh->dxfer_len, 1114 acb->ioh->dxferp); 1115 } else { 1116 scsi_task_set_iov_in(acb->task, 1117 (struct scsi_iovec *) acb->ioh->dxferp, 1118 acb->ioh->iovec_count); 1119 } 1120 } 1121 1122 iscsi_set_events(iscsilun); 1123 qemu_mutex_unlock(&iscsilun->mutex); 1124 1125 return &acb->common; 1126 } 1127 1128 #endif 1129 1130 static int64_t coroutine_fn 1131 iscsi_co_getlength(BlockDriverState *bs) 1132 { 1133 IscsiLun *iscsilun = bs->opaque; 1134 int64_t len; 1135 1136 len = iscsilun->num_blocks; 1137 len *= iscsilun->block_size; 1138 1139 return len; 1140 } 1141 1142 static int 1143 coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs, int64_t offset, 1144 int64_t bytes) 1145 { 1146 IscsiLun *iscsilun = bs->opaque; 1147 struct IscsiTask iTask; 1148 struct unmap_list list; 1149 int r = 0; 1150 1151 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) { 1152 return -ENOTSUP; 1153 } 1154 1155 if (!iscsilun->lbp.lbpu) { 1156 /* UNMAP is not supported by the target */ 1157 return 0; 1158 } 1159 1160 /* 1161 * We don't want to overflow list.num which is uint32_t. 1162 * We rely on our max_pdiscard. 1163 */ 1164 assert(bytes / iscsilun->block_size <= UINT32_MAX); 1165 1166 list.lba = offset / iscsilun->block_size; 1167 list.num = bytes / iscsilun->block_size; 1168 1169 iscsi_co_init_iscsitask(iscsilun, &iTask); 1170 qemu_mutex_lock(&iscsilun->mutex); 1171 retry: 1172 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1, 1173 iscsi_co_generic_cb, &iTask) == NULL) { 1174 r = -ENOMEM; 1175 goto out_unlock; 1176 } 1177 1178 iscsi_co_wait_for_task(&iTask, iscsilun); 1179 1180 if (iTask.task != NULL) { 1181 scsi_free_scsi_task(iTask.task); 1182 iTask.task = NULL; 1183 } 1184 1185 if (iTask.do_retry) { 1186 iTask.complete = 0; 1187 goto retry; 1188 } 1189 1190 iscsi_allocmap_set_invalid(iscsilun, offset, bytes); 1191 1192 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) { 1193 /* the target might fail with a check condition if it 1194 is not happy with the alignment of the UNMAP request 1195 we silently fail in this case */ 1196 goto out_unlock; 1197 } 1198 1199 if (iTask.status != SCSI_STATUS_GOOD) { 1200 error_report("iSCSI UNMAP failed at lba %" PRIu64 ": %s", 1201 list.lba, iTask.err_str); 1202 r = iTask.err_code; 1203 goto out_unlock; 1204 } 1205 1206 out_unlock: 1207 qemu_mutex_unlock(&iscsilun->mutex); 1208 g_free(iTask.err_str); 1209 return r; 1210 } 1211 1212 static int 1213 coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, 1214 int64_t bytes, BdrvRequestFlags flags) 1215 { 1216 IscsiLun *iscsilun = bs->opaque; 1217 struct IscsiTask iTask; 1218 uint64_t lba; 1219 uint64_t nb_blocks; 1220 bool use_16_for_ws = iscsilun->use_16_for_rw; 1221 int r = 0; 1222 1223 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) { 1224 return -ENOTSUP; 1225 } 1226 1227 if (flags & BDRV_REQ_MAY_UNMAP) { 1228 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) { 1229 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */ 1230 use_16_for_ws = true; 1231 } 1232 if (use_16_for_ws && !iscsilun->lbp.lbpws) { 1233 /* WRITESAME16 with UNMAP is not supported by the target, 1234 * fall back and try WRITESAME10/16 without UNMAP */ 1235 flags &= ~BDRV_REQ_MAY_UNMAP; 1236 use_16_for_ws = iscsilun->use_16_for_rw; 1237 } 1238 } 1239 1240 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) { 1241 /* WRITESAME without UNMAP is not supported by the target */ 1242 return -ENOTSUP; 1243 } 1244 1245 lba = offset / iscsilun->block_size; 1246 nb_blocks = bytes / iscsilun->block_size; 1247 1248 if (iscsilun->zeroblock == NULL) { 1249 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size); 1250 if (iscsilun->zeroblock == NULL) { 1251 return -ENOMEM; 1252 } 1253 } 1254 1255 qemu_mutex_lock(&iscsilun->mutex); 1256 iscsi_co_init_iscsitask(iscsilun, &iTask); 1257 retry: 1258 if (use_16_for_ws) { 1259 /* 1260 * iscsi_writesame16_task num_blocks argument is uint32_t. We rely here 1261 * on our max_pwrite_zeroes limit. 1262 */ 1263 assert(nb_blocks <= UINT32_MAX); 1264 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba, 1265 iscsilun->zeroblock, iscsilun->block_size, 1266 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), 1267 0, 0, iscsi_co_generic_cb, &iTask); 1268 } else { 1269 /* 1270 * iscsi_writesame10_task num_blocks argument is uint16_t. We rely here 1271 * on our max_pwrite_zeroes limit. 1272 */ 1273 assert(nb_blocks <= UINT16_MAX); 1274 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba, 1275 iscsilun->zeroblock, iscsilun->block_size, 1276 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), 1277 0, 0, iscsi_co_generic_cb, &iTask); 1278 } 1279 if (iTask.task == NULL) { 1280 qemu_mutex_unlock(&iscsilun->mutex); 1281 return -ENOMEM; 1282 } 1283 1284 iscsi_co_wait_for_task(&iTask, iscsilun); 1285 1286 if (iTask.status == SCSI_STATUS_CHECK_CONDITION && 1287 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST && 1288 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE || 1289 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) { 1290 /* WRITE SAME is not supported by the target */ 1291 iscsilun->has_write_same = false; 1292 scsi_free_scsi_task(iTask.task); 1293 r = -ENOTSUP; 1294 goto out_unlock; 1295 } 1296 1297 if (iTask.task != NULL) { 1298 scsi_free_scsi_task(iTask.task); 1299 iTask.task = NULL; 1300 } 1301 1302 if (iTask.do_retry) { 1303 iTask.complete = 0; 1304 goto retry; 1305 } 1306 1307 if (iTask.status != SCSI_STATUS_GOOD) { 1308 iscsi_allocmap_set_invalid(iscsilun, offset, bytes); 1309 error_report("iSCSI WRITESAME10/16 failed at lba %" PRIu64 ": %s", 1310 lba, iTask.err_str); 1311 r = iTask.err_code; 1312 goto out_unlock; 1313 } 1314 1315 if (flags & BDRV_REQ_MAY_UNMAP) { 1316 iscsi_allocmap_set_invalid(iscsilun, offset, bytes); 1317 } else { 1318 iscsi_allocmap_set_allocated(iscsilun, offset, bytes); 1319 } 1320 1321 out_unlock: 1322 qemu_mutex_unlock(&iscsilun->mutex); 1323 g_free(iTask.err_str); 1324 return r; 1325 } 1326 1327 static void apply_chap(struct iscsi_context *iscsi, QemuOpts *opts, 1328 Error **errp) 1329 { 1330 const char *user = NULL; 1331 const char *password = NULL; 1332 const char *secretid; 1333 char *secret = NULL; 1334 1335 user = qemu_opt_get(opts, "user"); 1336 if (!user) { 1337 return; 1338 } 1339 1340 secretid = qemu_opt_get(opts, "password-secret"); 1341 password = qemu_opt_get(opts, "password"); 1342 if (secretid && password) { 1343 error_setg(errp, "'password' and 'password-secret' properties are " 1344 "mutually exclusive"); 1345 return; 1346 } 1347 if (secretid) { 1348 secret = qcrypto_secret_lookup_as_utf8(secretid, errp); 1349 if (!secret) { 1350 return; 1351 } 1352 password = secret; 1353 } else if (!password) { 1354 error_setg(errp, "CHAP username specified but no password was given"); 1355 return; 1356 } 1357 1358 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) { 1359 error_setg(errp, "Failed to set initiator username and password"); 1360 } 1361 1362 g_free(secret); 1363 } 1364 1365 static void apply_header_digest(struct iscsi_context *iscsi, QemuOpts *opts, 1366 Error **errp) 1367 { 1368 const char *digest = NULL; 1369 1370 digest = qemu_opt_get(opts, "header-digest"); 1371 if (!digest) { 1372 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); 1373 } else if (!strcmp(digest, "crc32c")) { 1374 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C); 1375 } else if (!strcmp(digest, "none")) { 1376 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE); 1377 } else if (!strcmp(digest, "crc32c-none")) { 1378 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE); 1379 } else if (!strcmp(digest, "none-crc32c")) { 1380 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); 1381 } else { 1382 error_setg(errp, "Invalid header-digest setting : %s", digest); 1383 } 1384 } 1385 1386 static char *get_initiator_name(QemuOpts *opts) 1387 { 1388 const char *name; 1389 char *iscsi_name; 1390 UuidInfo *uuid_info; 1391 1392 name = qemu_opt_get(opts, "initiator-name"); 1393 if (name) { 1394 return g_strdup(name); 1395 } 1396 1397 uuid_info = qmp_query_uuid(NULL); 1398 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) { 1399 name = qemu_get_vm_name(); 1400 } else { 1401 name = uuid_info->UUID; 1402 } 1403 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s", 1404 name ? ":" : "", name ? name : ""); 1405 qapi_free_UuidInfo(uuid_info); 1406 return iscsi_name; 1407 } 1408 1409 static void iscsi_nop_timed_event(void *opaque) 1410 { 1411 IscsiLun *iscsilun = opaque; 1412 1413 QEMU_LOCK_GUARD(&iscsilun->mutex); 1414 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) { 1415 error_report("iSCSI: NOP timeout. Reconnecting..."); 1416 iscsilun->request_timed_out = true; 1417 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) { 1418 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages."); 1419 return; 1420 } 1421 1422 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); 1423 iscsi_set_events(iscsilun); 1424 } 1425 1426 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp) 1427 { 1428 struct scsi_task *task = NULL; 1429 struct scsi_readcapacity10 *rc10 = NULL; 1430 struct scsi_readcapacity16 *rc16 = NULL; 1431 int retries = ISCSI_CMD_RETRIES; 1432 1433 do { 1434 if (task != NULL) { 1435 scsi_free_scsi_task(task); 1436 task = NULL; 1437 } 1438 1439 switch (iscsilun->type) { 1440 case TYPE_DISK: 1441 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun); 1442 if (task != NULL && task->status == SCSI_STATUS_GOOD) { 1443 rc16 = scsi_datain_unmarshall(task); 1444 if (rc16 == NULL) { 1445 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data."); 1446 } else { 1447 iscsilun->block_size = rc16->block_length; 1448 iscsilun->num_blocks = rc16->returned_lba + 1; 1449 iscsilun->lbpme = !!rc16->lbpme; 1450 iscsilun->lbprz = !!rc16->lbprz; 1451 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff); 1452 } 1453 break; 1454 } 1455 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION 1456 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { 1457 break; 1458 } 1459 /* Fall through and try READ CAPACITY(10) instead. */ 1460 case TYPE_ROM: 1461 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0); 1462 if (task != NULL && task->status == SCSI_STATUS_GOOD) { 1463 rc10 = scsi_datain_unmarshall(task); 1464 if (rc10 == NULL) { 1465 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data."); 1466 } else { 1467 iscsilun->block_size = rc10->block_size; 1468 if (rc10->lba == 0) { 1469 /* blank disk loaded */ 1470 iscsilun->num_blocks = 0; 1471 } else { 1472 iscsilun->num_blocks = rc10->lba + 1; 1473 } 1474 } 1475 } 1476 break; 1477 default: 1478 return; 1479 } 1480 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION 1481 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION 1482 && retries-- > 0); 1483 1484 if (task == NULL || task->status != SCSI_STATUS_GOOD) { 1485 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command"); 1486 } else if (!iscsilun->block_size || 1487 iscsilun->block_size % BDRV_SECTOR_SIZE) { 1488 error_setg(errp, "iSCSI: the target returned an invalid " 1489 "block size of %d.", iscsilun->block_size); 1490 } 1491 if (task) { 1492 scsi_free_scsi_task(task); 1493 } 1494 } 1495 1496 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun, 1497 int evpd, int pc, void **inq, Error **errp) 1498 { 1499 int full_size; 1500 struct scsi_task *task = NULL; 1501 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64); 1502 if (task == NULL || task->status != SCSI_STATUS_GOOD) { 1503 goto fail; 1504 } 1505 full_size = scsi_datain_getfullsize(task); 1506 if (full_size > task->datain.size) { 1507 scsi_free_scsi_task(task); 1508 1509 /* we need more data for the full list */ 1510 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size); 1511 if (task == NULL || task->status != SCSI_STATUS_GOOD) { 1512 goto fail; 1513 } 1514 } 1515 1516 *inq = scsi_datain_unmarshall(task); 1517 if (*inq == NULL) { 1518 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob"); 1519 goto fail_with_err; 1520 } 1521 1522 return task; 1523 1524 fail: 1525 error_setg(errp, "iSCSI: Inquiry command failed : %s", 1526 iscsi_get_error(iscsi)); 1527 fail_with_err: 1528 if (task != NULL) { 1529 scsi_free_scsi_task(task); 1530 } 1531 return NULL; 1532 } 1533 1534 static void iscsi_detach_aio_context(BlockDriverState *bs) 1535 { 1536 IscsiLun *iscsilun = bs->opaque; 1537 1538 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi), 1539 false, NULL, NULL, NULL, NULL, NULL); 1540 iscsilun->events = 0; 1541 1542 if (iscsilun->nop_timer) { 1543 timer_free(iscsilun->nop_timer); 1544 iscsilun->nop_timer = NULL; 1545 } 1546 if (iscsilun->event_timer) { 1547 timer_free(iscsilun->event_timer); 1548 iscsilun->event_timer = NULL; 1549 } 1550 } 1551 1552 static void iscsi_attach_aio_context(BlockDriverState *bs, 1553 AioContext *new_context) 1554 { 1555 IscsiLun *iscsilun = bs->opaque; 1556 1557 iscsilun->aio_context = new_context; 1558 iscsi_set_events(iscsilun); 1559 1560 /* Set up a timer for sending out iSCSI NOPs */ 1561 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context, 1562 QEMU_CLOCK_REALTIME, SCALE_MS, 1563 iscsi_nop_timed_event, iscsilun); 1564 timer_mod(iscsilun->nop_timer, 1565 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); 1566 1567 /* Set up a timer for periodic calls to iscsi_set_events and to 1568 * scan for command timeout */ 1569 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context, 1570 QEMU_CLOCK_REALTIME, SCALE_MS, 1571 iscsi_timed_check_events, iscsilun); 1572 timer_mod(iscsilun->event_timer, 1573 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); 1574 } 1575 1576 static void iscsi_modesense_sync(IscsiLun *iscsilun) 1577 { 1578 struct scsi_task *task; 1579 struct scsi_mode_sense *ms = NULL; 1580 iscsilun->write_protected = false; 1581 iscsilun->dpofua = false; 1582 1583 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun, 1584 1, SCSI_MODESENSE_PC_CURRENT, 1585 0x3F, 0, 255); 1586 if (task == NULL) { 1587 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s", 1588 iscsi_get_error(iscsilun->iscsi)); 1589 goto out; 1590 } 1591 1592 if (task->status != SCSI_STATUS_GOOD) { 1593 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable"); 1594 goto out; 1595 } 1596 ms = scsi_datain_unmarshall(task); 1597 if (!ms) { 1598 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s", 1599 iscsi_get_error(iscsilun->iscsi)); 1600 goto out; 1601 } 1602 iscsilun->write_protected = ms->device_specific_parameter & 0x80; 1603 iscsilun->dpofua = ms->device_specific_parameter & 0x10; 1604 1605 out: 1606 if (task) { 1607 scsi_free_scsi_task(task); 1608 } 1609 } 1610 1611 static void iscsi_parse_iscsi_option(const char *target, QDict *options) 1612 { 1613 QemuOptsList *list; 1614 QemuOpts *opts; 1615 const char *user, *password, *password_secret, *initiator_name, 1616 *header_digest, *timeout; 1617 1618 list = qemu_find_opts("iscsi"); 1619 if (!list) { 1620 return; 1621 } 1622 1623 opts = qemu_opts_find(list, target); 1624 if (opts == NULL) { 1625 opts = QTAILQ_FIRST(&list->head); 1626 if (!opts) { 1627 return; 1628 } 1629 } 1630 1631 user = qemu_opt_get(opts, "user"); 1632 if (user) { 1633 qdict_set_default_str(options, "user", user); 1634 } 1635 1636 password = qemu_opt_get(opts, "password"); 1637 if (password) { 1638 qdict_set_default_str(options, "password", password); 1639 } 1640 1641 password_secret = qemu_opt_get(opts, "password-secret"); 1642 if (password_secret) { 1643 qdict_set_default_str(options, "password-secret", password_secret); 1644 } 1645 1646 initiator_name = qemu_opt_get(opts, "initiator-name"); 1647 if (initiator_name) { 1648 qdict_set_default_str(options, "initiator-name", initiator_name); 1649 } 1650 1651 header_digest = qemu_opt_get(opts, "header-digest"); 1652 if (header_digest) { 1653 /* -iscsi takes upper case values, but QAPI only supports lower case 1654 * enum constant names, so we have to convert here. */ 1655 char *qapi_value = g_ascii_strdown(header_digest, -1); 1656 qdict_set_default_str(options, "header-digest", qapi_value); 1657 g_free(qapi_value); 1658 } 1659 1660 timeout = qemu_opt_get(opts, "timeout"); 1661 if (timeout) { 1662 qdict_set_default_str(options, "timeout", timeout); 1663 } 1664 } 1665 1666 /* 1667 * We support iscsi url's on the form 1668 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> 1669 */ 1670 static void iscsi_parse_filename(const char *filename, QDict *options, 1671 Error **errp) 1672 { 1673 struct iscsi_url *iscsi_url; 1674 const char *transport_name; 1675 char *lun_str; 1676 1677 iscsi_url = iscsi_parse_full_url(NULL, filename); 1678 if (iscsi_url == NULL) { 1679 error_setg(errp, "Failed to parse URL : %s", filename); 1680 return; 1681 } 1682 1683 #if LIBISCSI_API_VERSION >= (20160603) 1684 switch (iscsi_url->transport) { 1685 case TCP_TRANSPORT: 1686 transport_name = "tcp"; 1687 break; 1688 case ISER_TRANSPORT: 1689 transport_name = "iser"; 1690 break; 1691 default: 1692 error_setg(errp, "Unknown transport type (%d)", 1693 iscsi_url->transport); 1694 return; 1695 } 1696 #else 1697 transport_name = "tcp"; 1698 #endif 1699 1700 qdict_set_default_str(options, "transport", transport_name); 1701 qdict_set_default_str(options, "portal", iscsi_url->portal); 1702 qdict_set_default_str(options, "target", iscsi_url->target); 1703 1704 lun_str = g_strdup_printf("%d", iscsi_url->lun); 1705 qdict_set_default_str(options, "lun", lun_str); 1706 g_free(lun_str); 1707 1708 /* User/password from -iscsi take precedence over those from the URL */ 1709 iscsi_parse_iscsi_option(iscsi_url->target, options); 1710 1711 if (iscsi_url->user[0] != '\0') { 1712 qdict_set_default_str(options, "user", iscsi_url->user); 1713 qdict_set_default_str(options, "password", iscsi_url->passwd); 1714 } 1715 1716 iscsi_destroy_url(iscsi_url); 1717 } 1718 1719 static QemuOptsList runtime_opts = { 1720 .name = "iscsi", 1721 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 1722 .desc = { 1723 { 1724 .name = "transport", 1725 .type = QEMU_OPT_STRING, 1726 }, 1727 { 1728 .name = "portal", 1729 .type = QEMU_OPT_STRING, 1730 }, 1731 { 1732 .name = "target", 1733 .type = QEMU_OPT_STRING, 1734 }, 1735 { 1736 .name = "user", 1737 .type = QEMU_OPT_STRING, 1738 }, 1739 { 1740 .name = "password", 1741 .type = QEMU_OPT_STRING, 1742 }, 1743 { 1744 .name = "password-secret", 1745 .type = QEMU_OPT_STRING, 1746 }, 1747 { 1748 .name = "lun", 1749 .type = QEMU_OPT_NUMBER, 1750 }, 1751 { 1752 .name = "initiator-name", 1753 .type = QEMU_OPT_STRING, 1754 }, 1755 { 1756 .name = "header-digest", 1757 .type = QEMU_OPT_STRING, 1758 }, 1759 { 1760 .name = "timeout", 1761 .type = QEMU_OPT_NUMBER, 1762 }, 1763 { /* end of list */ } 1764 }, 1765 }; 1766 1767 static void iscsi_save_designator(IscsiLun *lun, 1768 struct scsi_inquiry_device_identification *inq_di) 1769 { 1770 struct scsi_inquiry_device_designator *desig, *copy = NULL; 1771 1772 for (desig = inq_di->designators; desig; desig = desig->next) { 1773 if (desig->association || 1774 desig->designator_type > SCSI_DESIGNATOR_TYPE_NAA) { 1775 continue; 1776 } 1777 /* NAA works better than T10 vendor ID based designator. */ 1778 if (!copy || copy->designator_type < desig->designator_type) { 1779 copy = desig; 1780 } 1781 } 1782 if (copy) { 1783 lun->dd = g_new(struct scsi_inquiry_device_designator, 1); 1784 *lun->dd = *copy; 1785 lun->dd->next = NULL; 1786 lun->dd->designator = g_malloc(copy->designator_length); 1787 memcpy(lun->dd->designator, copy->designator, copy->designator_length); 1788 } 1789 } 1790 1791 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, 1792 Error **errp) 1793 { 1794 IscsiLun *iscsilun = bs->opaque; 1795 struct iscsi_context *iscsi = NULL; 1796 struct scsi_task *task = NULL; 1797 struct scsi_inquiry_standard *inq = NULL; 1798 struct scsi_inquiry_supported_pages *inq_vpd; 1799 char *initiator_name = NULL; 1800 QemuOpts *opts; 1801 Error *local_err = NULL; 1802 const char *transport_name, *portal, *target; 1803 #if LIBISCSI_API_VERSION >= (20160603) 1804 enum iscsi_transport_type transport; 1805 #endif 1806 int i, ret = 0, timeout = 0, lun; 1807 1808 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 1809 if (!qemu_opts_absorb_qdict(opts, options, errp)) { 1810 ret = -EINVAL; 1811 goto out; 1812 } 1813 1814 transport_name = qemu_opt_get(opts, "transport"); 1815 portal = qemu_opt_get(opts, "portal"); 1816 target = qemu_opt_get(opts, "target"); 1817 lun = qemu_opt_get_number(opts, "lun", 0); 1818 1819 if (!transport_name || !portal || !target) { 1820 error_setg(errp, "Need all of transport, portal and target options"); 1821 ret = -EINVAL; 1822 goto out; 1823 } 1824 1825 if (!strcmp(transport_name, "tcp")) { 1826 #if LIBISCSI_API_VERSION >= (20160603) 1827 transport = TCP_TRANSPORT; 1828 } else if (!strcmp(transport_name, "iser")) { 1829 transport = ISER_TRANSPORT; 1830 #else 1831 /* TCP is what older libiscsi versions always use */ 1832 #endif 1833 } else { 1834 error_setg(errp, "Unknown transport: %s", transport_name); 1835 ret = -EINVAL; 1836 goto out; 1837 } 1838 1839 memset(iscsilun, 0, sizeof(IscsiLun)); 1840 1841 initiator_name = get_initiator_name(opts); 1842 1843 iscsi = iscsi_create_context(initiator_name); 1844 if (iscsi == NULL) { 1845 error_setg(errp, "iSCSI: Failed to create iSCSI context."); 1846 ret = -ENOMEM; 1847 goto out; 1848 } 1849 #if LIBISCSI_API_VERSION >= (20160603) 1850 if (iscsi_init_transport(iscsi, transport)) { 1851 error_setg(errp, ("Error initializing transport.")); 1852 ret = -EINVAL; 1853 goto out; 1854 } 1855 #endif 1856 if (iscsi_set_targetname(iscsi, target)) { 1857 error_setg(errp, "iSCSI: Failed to set target name."); 1858 ret = -EINVAL; 1859 goto out; 1860 } 1861 1862 /* check if we got CHAP username/password via the options */ 1863 apply_chap(iscsi, opts, &local_err); 1864 if (local_err != NULL) { 1865 error_propagate(errp, local_err); 1866 ret = -EINVAL; 1867 goto out; 1868 } 1869 1870 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { 1871 error_setg(errp, "iSCSI: Failed to set session type to normal."); 1872 ret = -EINVAL; 1873 goto out; 1874 } 1875 1876 /* check if we got HEADER_DIGEST via the options */ 1877 apply_header_digest(iscsi, opts, &local_err); 1878 if (local_err != NULL) { 1879 error_propagate(errp, local_err); 1880 ret = -EINVAL; 1881 goto out; 1882 } 1883 1884 /* timeout handling is broken in libiscsi before 1.15.0 */ 1885 timeout = qemu_opt_get_number(opts, "timeout", 0); 1886 #if LIBISCSI_API_VERSION >= 20150621 1887 iscsi_set_timeout(iscsi, timeout); 1888 #else 1889 if (timeout) { 1890 warn_report("iSCSI: ignoring timeout value for libiscsi <1.15.0"); 1891 } 1892 #endif 1893 1894 if (iscsi_full_connect_sync(iscsi, portal, lun) != 0) { 1895 error_setg(errp, "iSCSI: Failed to connect to LUN : %s", 1896 iscsi_get_error(iscsi)); 1897 ret = -EINVAL; 1898 goto out; 1899 } 1900 1901 iscsilun->iscsi = iscsi; 1902 iscsilun->aio_context = bdrv_get_aio_context(bs); 1903 iscsilun->lun = lun; 1904 iscsilun->has_write_same = true; 1905 1906 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0, 1907 (void **) &inq, errp); 1908 if (task == NULL) { 1909 ret = -EINVAL; 1910 goto out; 1911 } 1912 iscsilun->type = inq->periperal_device_type; 1913 scsi_free_scsi_task(task); 1914 task = NULL; 1915 1916 iscsi_modesense_sync(iscsilun); 1917 if (iscsilun->dpofua) { 1918 bs->supported_write_flags = BDRV_REQ_FUA; 1919 } 1920 1921 /* Check the write protect flag of the LUN if we want to write */ 1922 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) && 1923 iscsilun->write_protected) { 1924 ret = bdrv_apply_auto_read_only(bs, "LUN is write protected", errp); 1925 if (ret < 0) { 1926 goto out; 1927 } 1928 flags &= ~BDRV_O_RDWR; 1929 } 1930 1931 iscsi_readcapacity_sync(iscsilun, &local_err); 1932 if (local_err != NULL) { 1933 error_propagate(errp, local_err); 1934 ret = -EINVAL; 1935 goto out; 1936 } 1937 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun); 1938 1939 /* We don't have any emulation for devices other than disks and CD-ROMs, so 1940 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu 1941 * will try to read from the device to guess the image format. 1942 */ 1943 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) { 1944 bs->sg = true; 1945 } 1946 1947 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, 1948 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES, 1949 (void **) &inq_vpd, errp); 1950 if (task == NULL) { 1951 ret = -EINVAL; 1952 goto out; 1953 } 1954 for (i = 0; i < inq_vpd->num_pages; i++) { 1955 struct scsi_task *inq_task; 1956 struct scsi_inquiry_logical_block_provisioning *inq_lbp; 1957 struct scsi_inquiry_block_limits *inq_bl; 1958 struct scsi_inquiry_device_identification *inq_di; 1959 switch (inq_vpd->pages[i]) { 1960 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING: 1961 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, 1962 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, 1963 (void **) &inq_lbp, errp); 1964 if (inq_task == NULL) { 1965 ret = -EINVAL; 1966 goto out; 1967 } 1968 memcpy(&iscsilun->lbp, inq_lbp, 1969 sizeof(struct scsi_inquiry_logical_block_provisioning)); 1970 scsi_free_scsi_task(inq_task); 1971 break; 1972 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS: 1973 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, 1974 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS, 1975 (void **) &inq_bl, errp); 1976 if (inq_task == NULL) { 1977 ret = -EINVAL; 1978 goto out; 1979 } 1980 memcpy(&iscsilun->bl, inq_bl, 1981 sizeof(struct scsi_inquiry_block_limits)); 1982 scsi_free_scsi_task(inq_task); 1983 break; 1984 case SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION: 1985 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, 1986 SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION, 1987 (void **) &inq_di, errp); 1988 if (inq_task == NULL) { 1989 ret = -EINVAL; 1990 goto out; 1991 } 1992 iscsi_save_designator(iscsilun, inq_di); 1993 scsi_free_scsi_task(inq_task); 1994 break; 1995 default: 1996 break; 1997 } 1998 } 1999 scsi_free_scsi_task(task); 2000 task = NULL; 2001 2002 qemu_mutex_init(&iscsilun->mutex); 2003 iscsi_attach_aio_context(bs, iscsilun->aio_context); 2004 2005 /* Guess the internal cluster (page) size of the iscsi target by the means 2006 * of opt_unmap_gran. Transfer the unmap granularity only if it has a 2007 * reasonable size */ 2008 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 && 2009 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) { 2010 iscsilun->cluster_size = iscsilun->bl.opt_unmap_gran * 2011 iscsilun->block_size; 2012 if (iscsilun->lbprz) { 2013 ret = iscsi_allocmap_init(iscsilun, flags); 2014 } 2015 } 2016 2017 if (iscsilun->lbprz && iscsilun->lbp.lbpws) { 2018 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP; 2019 } 2020 2021 out: 2022 qemu_opts_del(opts); 2023 g_free(initiator_name); 2024 if (task != NULL) { 2025 scsi_free_scsi_task(task); 2026 } 2027 2028 if (ret) { 2029 if (iscsi != NULL) { 2030 if (iscsi_is_logged_in(iscsi)) { 2031 iscsi_logout_sync(iscsi); 2032 } 2033 iscsi_destroy_context(iscsi); 2034 } 2035 memset(iscsilun, 0, sizeof(IscsiLun)); 2036 } 2037 2038 return ret; 2039 } 2040 2041 static void iscsi_close(BlockDriverState *bs) 2042 { 2043 IscsiLun *iscsilun = bs->opaque; 2044 struct iscsi_context *iscsi = iscsilun->iscsi; 2045 2046 iscsi_detach_aio_context(bs); 2047 if (iscsi_is_logged_in(iscsi)) { 2048 iscsi_logout_sync(iscsi); 2049 } 2050 iscsi_destroy_context(iscsi); 2051 if (iscsilun->dd) { 2052 g_free(iscsilun->dd->designator); 2053 g_free(iscsilun->dd); 2054 } 2055 g_free(iscsilun->zeroblock); 2056 iscsi_allocmap_free(iscsilun); 2057 qemu_mutex_destroy(&iscsilun->mutex); 2058 memset(iscsilun, 0, sizeof(IscsiLun)); 2059 } 2060 2061 static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp) 2062 { 2063 /* We don't actually refresh here, but just return data queried in 2064 * iscsi_open(): iscsi targets don't change their limits. */ 2065 2066 IscsiLun *iscsilun = bs->opaque; 2067 uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff; 2068 unsigned int block_size = MAX(BDRV_SECTOR_SIZE, iscsilun->block_size); 2069 2070 assert(iscsilun->block_size >= BDRV_SECTOR_SIZE || bdrv_is_sg(bs)); 2071 2072 bs->bl.request_alignment = block_size; 2073 2074 if (iscsilun->bl.max_xfer_len) { 2075 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len); 2076 } 2077 2078 if (max_xfer_len * block_size < INT_MAX) { 2079 bs->bl.max_transfer = max_xfer_len * iscsilun->block_size; 2080 } 2081 2082 if (iscsilun->lbp.lbpu) { 2083 bs->bl.max_pdiscard = 2084 MIN_NON_ZERO(iscsilun->bl.max_unmap * iscsilun->block_size, 2085 (uint64_t)UINT32_MAX * iscsilun->block_size); 2086 bs->bl.pdiscard_alignment = 2087 iscsilun->bl.opt_unmap_gran * iscsilun->block_size; 2088 } else { 2089 bs->bl.pdiscard_alignment = iscsilun->block_size; 2090 } 2091 2092 bs->bl.max_pwrite_zeroes = 2093 MIN_NON_ZERO(iscsilun->bl.max_ws_len * iscsilun->block_size, 2094 max_xfer_len * iscsilun->block_size); 2095 2096 if (iscsilun->lbp.lbpws) { 2097 bs->bl.pwrite_zeroes_alignment = 2098 iscsilun->bl.opt_unmap_gran * iscsilun->block_size; 2099 } else { 2100 bs->bl.pwrite_zeroes_alignment = iscsilun->block_size; 2101 } 2102 if (iscsilun->bl.opt_xfer_len && 2103 iscsilun->bl.opt_xfer_len < INT_MAX / block_size) { 2104 bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len * 2105 iscsilun->block_size); 2106 } 2107 } 2108 2109 /* Note that this will not re-establish a connection with an iSCSI target - it 2110 * is effectively a NOP. */ 2111 static int iscsi_reopen_prepare(BDRVReopenState *state, 2112 BlockReopenQueue *queue, Error **errp) 2113 { 2114 IscsiLun *iscsilun = state->bs->opaque; 2115 2116 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) { 2117 error_setg(errp, "Cannot open a write protected LUN as read-write"); 2118 return -EACCES; 2119 } 2120 return 0; 2121 } 2122 2123 static void iscsi_reopen_commit(BDRVReopenState *reopen_state) 2124 { 2125 IscsiLun *iscsilun = reopen_state->bs->opaque; 2126 2127 /* the cache.direct status might have changed */ 2128 if (iscsilun->allocmap != NULL) { 2129 iscsi_allocmap_init(iscsilun, reopen_state->flags); 2130 } 2131 } 2132 2133 static int coroutine_fn iscsi_co_truncate(BlockDriverState *bs, int64_t offset, 2134 bool exact, PreallocMode prealloc, 2135 BdrvRequestFlags flags, Error **errp) 2136 { 2137 IscsiLun *iscsilun = bs->opaque; 2138 int64_t cur_length; 2139 Error *local_err = NULL; 2140 2141 if (prealloc != PREALLOC_MODE_OFF) { 2142 error_setg(errp, "Unsupported preallocation mode '%s'", 2143 PreallocMode_str(prealloc)); 2144 return -ENOTSUP; 2145 } 2146 2147 if (iscsilun->type != TYPE_DISK) { 2148 error_setg(errp, "Cannot resize non-disk iSCSI devices"); 2149 return -ENOTSUP; 2150 } 2151 2152 iscsi_readcapacity_sync(iscsilun, &local_err); 2153 if (local_err != NULL) { 2154 error_propagate(errp, local_err); 2155 return -EIO; 2156 } 2157 2158 cur_length = iscsi_co_getlength(bs); 2159 if (offset != cur_length && exact) { 2160 error_setg(errp, "Cannot resize iSCSI devices"); 2161 return -ENOTSUP; 2162 } else if (offset > cur_length) { 2163 error_setg(errp, "Cannot grow iSCSI devices"); 2164 return -EINVAL; 2165 } 2166 2167 if (iscsilun->allocmap != NULL) { 2168 iscsi_allocmap_init(iscsilun, bs->open_flags); 2169 } 2170 2171 return 0; 2172 } 2173 2174 static int coroutine_fn 2175 iscsi_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2176 { 2177 IscsiLun *iscsilun = bs->opaque; 2178 bdi->cluster_size = iscsilun->cluster_size; 2179 return 0; 2180 } 2181 2182 static void coroutine_fn iscsi_co_invalidate_cache(BlockDriverState *bs, 2183 Error **errp) 2184 { 2185 IscsiLun *iscsilun = bs->opaque; 2186 iscsi_allocmap_invalidate(iscsilun); 2187 } 2188 2189 static int coroutine_fn iscsi_co_copy_range_from(BlockDriverState *bs, 2190 BdrvChild *src, 2191 int64_t src_offset, 2192 BdrvChild *dst, 2193 int64_t dst_offset, 2194 int64_t bytes, 2195 BdrvRequestFlags read_flags, 2196 BdrvRequestFlags write_flags) 2197 { 2198 return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, 2199 read_flags, write_flags); 2200 } 2201 2202 static struct scsi_task *iscsi_xcopy_task(int param_len) 2203 { 2204 struct scsi_task *task; 2205 2206 task = g_new0(struct scsi_task, 1); 2207 2208 task->cdb[0] = EXTENDED_COPY; 2209 task->cdb[10] = (param_len >> 24) & 0xFF; 2210 task->cdb[11] = (param_len >> 16) & 0xFF; 2211 task->cdb[12] = (param_len >> 8) & 0xFF; 2212 task->cdb[13] = param_len & 0xFF; 2213 task->cdb_size = 16; 2214 task->xfer_dir = SCSI_XFER_WRITE; 2215 task->expxferlen = param_len; 2216 2217 return task; 2218 } 2219 2220 static void iscsi_populate_target_desc(unsigned char *desc, IscsiLun *lun) 2221 { 2222 struct scsi_inquiry_device_designator *dd = lun->dd; 2223 2224 memset(desc, 0, 32); 2225 desc[0] = 0xE4; /* IDENT_DESCR_TGT_DESCR */ 2226 desc[4] = dd->code_set; 2227 desc[5] = (dd->designator_type & 0xF) 2228 | ((dd->association & 3) << 4); 2229 desc[7] = dd->designator_length; 2230 memcpy(desc + 8, dd->designator, MIN(dd->designator_length, 20)); 2231 2232 desc[28] = 0; 2233 desc[29] = (lun->block_size >> 16) & 0xFF; 2234 desc[30] = (lun->block_size >> 8) & 0xFF; 2235 desc[31] = lun->block_size & 0xFF; 2236 } 2237 2238 static void iscsi_xcopy_desc_hdr(uint8_t *hdr, int dc, int cat, int src_index, 2239 int dst_index) 2240 { 2241 hdr[0] = 0x02; /* BLK_TO_BLK_SEG_DESCR */ 2242 hdr[1] = ((dc << 1) | cat) & 0xFF; 2243 hdr[2] = (XCOPY_BLK2BLK_SEG_DESC_SIZE >> 8) & 0xFF; 2244 /* don't account for the first 4 bytes in descriptor header*/ 2245 hdr[3] = (XCOPY_BLK2BLK_SEG_DESC_SIZE - 4 /* SEG_DESC_SRC_INDEX_OFFSET */) & 0xFF; 2246 hdr[4] = (src_index >> 8) & 0xFF; 2247 hdr[5] = src_index & 0xFF; 2248 hdr[6] = (dst_index >> 8) & 0xFF; 2249 hdr[7] = dst_index & 0xFF; 2250 } 2251 2252 static void iscsi_xcopy_populate_desc(uint8_t *desc, int dc, int cat, 2253 int src_index, int dst_index, int num_blks, 2254 uint64_t src_lba, uint64_t dst_lba) 2255 { 2256 iscsi_xcopy_desc_hdr(desc, dc, cat, src_index, dst_index); 2257 2258 /* The caller should verify the request size */ 2259 assert(num_blks < 65536); 2260 desc[10] = (num_blks >> 8) & 0xFF; 2261 desc[11] = num_blks & 0xFF; 2262 desc[12] = (src_lba >> 56) & 0xFF; 2263 desc[13] = (src_lba >> 48) & 0xFF; 2264 desc[14] = (src_lba >> 40) & 0xFF; 2265 desc[15] = (src_lba >> 32) & 0xFF; 2266 desc[16] = (src_lba >> 24) & 0xFF; 2267 desc[17] = (src_lba >> 16) & 0xFF; 2268 desc[18] = (src_lba >> 8) & 0xFF; 2269 desc[19] = src_lba & 0xFF; 2270 desc[20] = (dst_lba >> 56) & 0xFF; 2271 desc[21] = (dst_lba >> 48) & 0xFF; 2272 desc[22] = (dst_lba >> 40) & 0xFF; 2273 desc[23] = (dst_lba >> 32) & 0xFF; 2274 desc[24] = (dst_lba >> 24) & 0xFF; 2275 desc[25] = (dst_lba >> 16) & 0xFF; 2276 desc[26] = (dst_lba >> 8) & 0xFF; 2277 desc[27] = dst_lba & 0xFF; 2278 } 2279 2280 static void iscsi_xcopy_populate_header(unsigned char *buf, int list_id, int str, 2281 int list_id_usage, int prio, 2282 int tgt_desc_len, 2283 int seg_desc_len, int inline_data_len) 2284 { 2285 buf[0] = list_id; 2286 buf[1] = ((str & 1) << 5) | ((list_id_usage & 3) << 3) | (prio & 7); 2287 buf[2] = (tgt_desc_len >> 8) & 0xFF; 2288 buf[3] = tgt_desc_len & 0xFF; 2289 buf[8] = (seg_desc_len >> 24) & 0xFF; 2290 buf[9] = (seg_desc_len >> 16) & 0xFF; 2291 buf[10] = (seg_desc_len >> 8) & 0xFF; 2292 buf[11] = seg_desc_len & 0xFF; 2293 buf[12] = (inline_data_len >> 24) & 0xFF; 2294 buf[13] = (inline_data_len >> 16) & 0xFF; 2295 buf[14] = (inline_data_len >> 8) & 0xFF; 2296 buf[15] = inline_data_len & 0xFF; 2297 } 2298 2299 static void iscsi_xcopy_data(struct iscsi_data *data, 2300 IscsiLun *src, int64_t src_lba, 2301 IscsiLun *dst, int64_t dst_lba, 2302 uint16_t num_blocks) 2303 { 2304 uint8_t *buf; 2305 const int src_offset = XCOPY_DESC_OFFSET; 2306 const int dst_offset = XCOPY_DESC_OFFSET + IDENT_DESCR_TGT_DESCR_SIZE; 2307 const int seg_offset = dst_offset + IDENT_DESCR_TGT_DESCR_SIZE; 2308 2309 data->size = XCOPY_DESC_OFFSET + 2310 IDENT_DESCR_TGT_DESCR_SIZE * 2 + 2311 XCOPY_BLK2BLK_SEG_DESC_SIZE; 2312 data->data = g_malloc0(data->size); 2313 buf = data->data; 2314 2315 /* Initialise the parameter list header */ 2316 iscsi_xcopy_populate_header(buf, 1, 0, 2 /* LIST_ID_USAGE_DISCARD */, 2317 0, 2 * IDENT_DESCR_TGT_DESCR_SIZE, 2318 XCOPY_BLK2BLK_SEG_DESC_SIZE, 2319 0); 2320 2321 /* Initialise CSCD list with one src + one dst descriptor */ 2322 iscsi_populate_target_desc(&buf[src_offset], src); 2323 iscsi_populate_target_desc(&buf[dst_offset], dst); 2324 2325 /* Initialise one segment descriptor */ 2326 iscsi_xcopy_populate_desc(&buf[seg_offset], 0, 0, 0, 1, num_blocks, 2327 src_lba, dst_lba); 2328 } 2329 2330 static int coroutine_fn iscsi_co_copy_range_to(BlockDriverState *bs, 2331 BdrvChild *src, 2332 int64_t src_offset, 2333 BdrvChild *dst, 2334 int64_t dst_offset, 2335 int64_t bytes, 2336 BdrvRequestFlags read_flags, 2337 BdrvRequestFlags write_flags) 2338 { 2339 IscsiLun *dst_lun = dst->bs->opaque; 2340 IscsiLun *src_lun; 2341 struct IscsiTask iscsi_task; 2342 struct iscsi_data data; 2343 int r = 0; 2344 int block_size; 2345 2346 if (src->bs->drv->bdrv_co_copy_range_to != iscsi_co_copy_range_to) { 2347 return -ENOTSUP; 2348 } 2349 src_lun = src->bs->opaque; 2350 2351 if (!src_lun->dd || !dst_lun->dd) { 2352 return -ENOTSUP; 2353 } 2354 if (!is_byte_request_lun_aligned(dst_offset, bytes, dst_lun)) { 2355 return -ENOTSUP; 2356 } 2357 if (!is_byte_request_lun_aligned(src_offset, bytes, src_lun)) { 2358 return -ENOTSUP; 2359 } 2360 if (dst_lun->block_size != src_lun->block_size || 2361 !dst_lun->block_size) { 2362 return -ENOTSUP; 2363 } 2364 2365 block_size = dst_lun->block_size; 2366 if (bytes / block_size > 65535) { 2367 return -ENOTSUP; 2368 } 2369 2370 iscsi_xcopy_data(&data, 2371 src_lun, src_offset / block_size, 2372 dst_lun, dst_offset / block_size, 2373 bytes / block_size); 2374 2375 iscsi_co_init_iscsitask(dst_lun, &iscsi_task); 2376 2377 qemu_mutex_lock(&dst_lun->mutex); 2378 iscsi_task.task = iscsi_xcopy_task(data.size); 2379 retry: 2380 if (iscsi_scsi_command_async(dst_lun->iscsi, dst_lun->lun, 2381 iscsi_task.task, iscsi_co_generic_cb, 2382 &data, 2383 &iscsi_task) != 0) { 2384 r = -EIO; 2385 goto out_unlock; 2386 } 2387 2388 iscsi_co_wait_for_task(&iscsi_task, dst_lun); 2389 2390 if (iscsi_task.do_retry) { 2391 iscsi_task.complete = 0; 2392 goto retry; 2393 } 2394 2395 if (iscsi_task.status != SCSI_STATUS_GOOD) { 2396 r = iscsi_task.err_code; 2397 goto out_unlock; 2398 } 2399 2400 out_unlock: 2401 2402 trace_iscsi_xcopy(src_lun, src_offset, dst_lun, dst_offset, bytes, r); 2403 g_free(iscsi_task.task); 2404 qemu_mutex_unlock(&dst_lun->mutex); 2405 g_free(iscsi_task.err_str); 2406 return r; 2407 } 2408 2409 2410 static const char *const iscsi_strong_runtime_opts[] = { 2411 "transport", 2412 "portal", 2413 "target", 2414 "user", 2415 "password", 2416 "password-secret", 2417 "lun", 2418 "initiator-name", 2419 "header-digest", 2420 2421 NULL 2422 }; 2423 2424 static BlockDriver bdrv_iscsi = { 2425 .format_name = "iscsi", 2426 .protocol_name = "iscsi", 2427 2428 .instance_size = sizeof(IscsiLun), 2429 .bdrv_parse_filename = iscsi_parse_filename, 2430 .bdrv_file_open = iscsi_open, 2431 .bdrv_close = iscsi_close, 2432 .bdrv_co_create_opts = bdrv_co_create_opts_simple, 2433 .create_opts = &bdrv_create_opts_simple, 2434 .bdrv_reopen_prepare = iscsi_reopen_prepare, 2435 .bdrv_reopen_commit = iscsi_reopen_commit, 2436 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache, 2437 2438 .bdrv_co_getlength = iscsi_co_getlength, 2439 .bdrv_co_get_info = iscsi_co_get_info, 2440 .bdrv_co_truncate = iscsi_co_truncate, 2441 .bdrv_refresh_limits = iscsi_refresh_limits, 2442 2443 .bdrv_co_block_status = iscsi_co_block_status, 2444 .bdrv_co_pdiscard = iscsi_co_pdiscard, 2445 .bdrv_co_copy_range_from = iscsi_co_copy_range_from, 2446 .bdrv_co_copy_range_to = iscsi_co_copy_range_to, 2447 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes, 2448 .bdrv_co_readv = iscsi_co_readv, 2449 .bdrv_co_writev = iscsi_co_writev, 2450 .bdrv_co_flush_to_disk = iscsi_co_flush, 2451 2452 #ifdef __linux__ 2453 .bdrv_aio_ioctl = iscsi_aio_ioctl, 2454 #endif 2455 2456 .bdrv_detach_aio_context = iscsi_detach_aio_context, 2457 .bdrv_attach_aio_context = iscsi_attach_aio_context, 2458 2459 .strong_runtime_opts = iscsi_strong_runtime_opts, 2460 }; 2461 2462 #if LIBISCSI_API_VERSION >= (20160603) 2463 static BlockDriver bdrv_iser = { 2464 .format_name = "iser", 2465 .protocol_name = "iser", 2466 2467 .instance_size = sizeof(IscsiLun), 2468 .bdrv_parse_filename = iscsi_parse_filename, 2469 .bdrv_file_open = iscsi_open, 2470 .bdrv_close = iscsi_close, 2471 .bdrv_co_create_opts = bdrv_co_create_opts_simple, 2472 .create_opts = &bdrv_create_opts_simple, 2473 .bdrv_reopen_prepare = iscsi_reopen_prepare, 2474 .bdrv_reopen_commit = iscsi_reopen_commit, 2475 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache, 2476 2477 .bdrv_co_getlength = iscsi_co_getlength, 2478 .bdrv_co_get_info = iscsi_co_get_info, 2479 .bdrv_co_truncate = iscsi_co_truncate, 2480 .bdrv_refresh_limits = iscsi_refresh_limits, 2481 2482 .bdrv_co_block_status = iscsi_co_block_status, 2483 .bdrv_co_pdiscard = iscsi_co_pdiscard, 2484 .bdrv_co_copy_range_from = iscsi_co_copy_range_from, 2485 .bdrv_co_copy_range_to = iscsi_co_copy_range_to, 2486 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes, 2487 .bdrv_co_readv = iscsi_co_readv, 2488 .bdrv_co_writev = iscsi_co_writev, 2489 .bdrv_co_flush_to_disk = iscsi_co_flush, 2490 2491 #ifdef __linux__ 2492 .bdrv_aio_ioctl = iscsi_aio_ioctl, 2493 #endif 2494 2495 .bdrv_detach_aio_context = iscsi_detach_aio_context, 2496 .bdrv_attach_aio_context = iscsi_attach_aio_context, 2497 2498 .strong_runtime_opts = iscsi_strong_runtime_opts, 2499 }; 2500 #endif 2501 2502 static void iscsi_block_init(void) 2503 { 2504 bdrv_register(&bdrv_iscsi); 2505 #if LIBISCSI_API_VERSION >= (20160603) 2506 bdrv_register(&bdrv_iser); 2507 #endif 2508 } 2509 2510 block_init(iscsi_block_init); 2511