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