1 /* 2 * QEMU Block driver for iSCSI images 3 * 4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com> 5 * Copyright (c) 2012-2015 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/scsi.h" 38 #include "qemu/iov.h" 39 #include "sysemu/sysemu.h" 40 #include "qmp-commands.h" 41 #include "qapi/qmp/qstring.h" 42 #include "crypto/secret.h" 43 44 #include <iscsi/iscsi.h> 45 #include <iscsi/scsi-lowlevel.h> 46 47 #ifdef __linux__ 48 #include <scsi/sg.h> 49 #endif 50 51 typedef struct IscsiLun { 52 struct iscsi_context *iscsi; 53 AioContext *aio_context; 54 int lun; 55 enum scsi_inquiry_peripheral_device_type type; 56 int block_size; 57 uint64_t num_blocks; 58 int events; 59 QEMUTimer *nop_timer; 60 QEMUTimer *event_timer; 61 struct scsi_inquiry_logical_block_provisioning lbp; 62 struct scsi_inquiry_block_limits bl; 63 unsigned char *zeroblock; 64 unsigned long *allocationmap; 65 int cluster_sectors; 66 bool use_16_for_rw; 67 bool write_protected; 68 bool lbpme; 69 bool lbprz; 70 bool dpofua; 71 bool has_write_same; 72 bool request_timed_out; 73 } IscsiLun; 74 75 typedef struct IscsiTask { 76 int status; 77 int complete; 78 int retries; 79 int do_retry; 80 struct scsi_task *task; 81 Coroutine *co; 82 QEMUBH *bh; 83 IscsiLun *iscsilun; 84 QEMUTimer retry_timer; 85 int err_code; 86 } IscsiTask; 87 88 typedef struct IscsiAIOCB { 89 BlockAIOCB common; 90 QEMUIOVector *qiov; 91 QEMUBH *bh; 92 IscsiLun *iscsilun; 93 struct scsi_task *task; 94 uint8_t *buf; 95 int status; 96 int64_t sector_num; 97 int nb_sectors; 98 int ret; 99 #ifdef __linux__ 100 sg_io_hdr_t *ioh; 101 #endif 102 } IscsiAIOCB; 103 104 /* libiscsi uses time_t so its enough to process events every second */ 105 #define EVENT_INTERVAL 1000 106 #define NOP_INTERVAL 5000 107 #define MAX_NOP_FAILURES 3 108 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times) 109 static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768}; 110 111 /* this threshold is a trade-off knob to choose between 112 * the potential additional overhead of an extra GET_LBA_STATUS request 113 * vs. unnecessarily reading a lot of zero sectors over the wire. 114 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES 115 * sectors we check the allocation status of the area covered by the 116 * request first if the allocationmap indicates that the area might be 117 * unallocated. */ 118 #define ISCSI_CHECKALLOC_THRES 64 119 120 static void 121 iscsi_bh_cb(void *p) 122 { 123 IscsiAIOCB *acb = p; 124 125 qemu_bh_delete(acb->bh); 126 127 g_free(acb->buf); 128 acb->buf = NULL; 129 130 acb->common.cb(acb->common.opaque, acb->status); 131 132 if (acb->task != NULL) { 133 scsi_free_scsi_task(acb->task); 134 acb->task = NULL; 135 } 136 137 qemu_aio_unref(acb); 138 } 139 140 static void 141 iscsi_schedule_bh(IscsiAIOCB *acb) 142 { 143 if (acb->bh) { 144 return; 145 } 146 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb); 147 qemu_bh_schedule(acb->bh); 148 } 149 150 static void iscsi_co_generic_bh_cb(void *opaque) 151 { 152 struct IscsiTask *iTask = opaque; 153 iTask->complete = 1; 154 qemu_bh_delete(iTask->bh); 155 qemu_coroutine_enter(iTask->co, NULL); 156 } 157 158 static void iscsi_retry_timer_expired(void *opaque) 159 { 160 struct IscsiTask *iTask = opaque; 161 iTask->complete = 1; 162 if (iTask->co) { 163 qemu_coroutine_enter(iTask->co, NULL); 164 } 165 } 166 167 static inline unsigned exp_random(double mean) 168 { 169 return -mean * log((double)rand() / RAND_MAX); 170 } 171 172 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in 173 * libiscsi 1.10.0, together with other constants we need. Use it as 174 * a hint that we have to define them ourselves if needed, to keep the 175 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for 176 * the test because SCSI_STATUS_* is an enum. 177 * 178 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes 179 * an enum, check against the LIBISCSI_API_VERSION macro, which was 180 * introduced in 1.11.0. If it is present, there is no need to define 181 * anything. 182 */ 183 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \ 184 !defined(LIBISCSI_API_VERSION) 185 #define SCSI_STATUS_TASK_SET_FULL 0x28 186 #define SCSI_STATUS_TIMEOUT 0x0f000002 187 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600 188 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00 189 #endif 190 191 static int iscsi_translate_sense(struct scsi_sense *sense) 192 { 193 int ret; 194 195 switch (sense->key) { 196 case SCSI_SENSE_NOT_READY: 197 return -EBUSY; 198 case SCSI_SENSE_DATA_PROTECTION: 199 return -EACCES; 200 case SCSI_SENSE_COMMAND_ABORTED: 201 return -ECANCELED; 202 case SCSI_SENSE_ILLEGAL_REQUEST: 203 /* Parse ASCQ */ 204 break; 205 default: 206 return -EIO; 207 } 208 switch (sense->ascq) { 209 case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR: 210 case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE: 211 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB: 212 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST: 213 ret = -EINVAL; 214 break; 215 case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE: 216 ret = -ENOSPC; 217 break; 218 case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED: 219 ret = -ENOTSUP; 220 break; 221 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT: 222 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED: 223 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN: 224 ret = -ENOMEDIUM; 225 break; 226 case SCSI_SENSE_ASCQ_WRITE_PROTECTED: 227 ret = -EACCES; 228 break; 229 default: 230 ret = -EIO; 231 break; 232 } 233 return ret; 234 } 235 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->task = task; 246 247 if (status != SCSI_STATUS_GOOD) { 248 if (iTask->retries++ < ISCSI_CMD_RETRIES) { 249 if (status == SCSI_STATUS_CHECK_CONDITION 250 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { 251 error_report("iSCSI CheckCondition: %s", 252 iscsi_get_error(iscsi)); 253 iTask->do_retry = 1; 254 goto out; 255 } 256 if (status == SCSI_STATUS_BUSY || 257 status == SCSI_STATUS_TIMEOUT || 258 status == SCSI_STATUS_TASK_SET_FULL) { 259 unsigned retry_time = 260 exp_random(iscsi_retry_times[iTask->retries - 1]); 261 if (status == SCSI_STATUS_TIMEOUT) { 262 /* make sure the request is rescheduled AFTER the 263 * reconnect is initiated */ 264 retry_time = EVENT_INTERVAL * 2; 265 iTask->iscsilun->request_timed_out = true; 266 } 267 error_report("iSCSI Busy/TaskSetFull/TimeOut" 268 " (retry #%u in %u ms): %s", 269 iTask->retries, retry_time, 270 iscsi_get_error(iscsi)); 271 aio_timer_init(iTask->iscsilun->aio_context, 272 &iTask->retry_timer, QEMU_CLOCK_REALTIME, 273 SCALE_MS, iscsi_retry_timer_expired, iTask); 274 timer_mod(&iTask->retry_timer, 275 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time); 276 iTask->do_retry = 1; 277 return; 278 } 279 } 280 iTask->err_code = iscsi_translate_sense(&task->sense); 281 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi)); 282 } 283 284 out: 285 if (iTask->co) { 286 iTask->bh = aio_bh_new(iTask->iscsilun->aio_context, 287 iscsi_co_generic_bh_cb, iTask); 288 qemu_bh_schedule(iTask->bh); 289 } else { 290 iTask->complete = 1; 291 } 292 } 293 294 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask) 295 { 296 *iTask = (struct IscsiTask) { 297 .co = qemu_coroutine_self(), 298 .iscsilun = iscsilun, 299 }; 300 } 301 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 acb->status = -ECANCELED; 309 iscsi_schedule_bh(acb); 310 } 311 312 static void 313 iscsi_aio_cancel(BlockAIOCB *blockacb) 314 { 315 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb; 316 IscsiLun *iscsilun = acb->iscsilun; 317 318 if (acb->status != -EINPROGRESS) { 319 return; 320 } 321 322 /* send a task mgmt call to the target to cancel the task on the target */ 323 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task, 324 iscsi_abort_task_cb, acb); 325 326 } 327 328 static const AIOCBInfo iscsi_aiocb_info = { 329 .aiocb_size = sizeof(IscsiAIOCB), 330 .cancel_async = iscsi_aio_cancel, 331 }; 332 333 334 static void iscsi_process_read(void *arg); 335 static void iscsi_process_write(void *arg); 336 337 static void 338 iscsi_set_events(IscsiLun *iscsilun) 339 { 340 struct iscsi_context *iscsi = iscsilun->iscsi; 341 int ev = iscsi_which_events(iscsi); 342 343 if (ev != iscsilun->events) { 344 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi), 345 false, 346 (ev & POLLIN) ? iscsi_process_read : NULL, 347 (ev & POLLOUT) ? iscsi_process_write : NULL, 348 iscsilun); 349 iscsilun->events = ev; 350 } 351 } 352 353 static void iscsi_timed_check_events(void *opaque) 354 { 355 IscsiLun *iscsilun = opaque; 356 357 /* check for timed out requests */ 358 iscsi_service(iscsilun->iscsi, 0); 359 360 if (iscsilun->request_timed_out) { 361 iscsilun->request_timed_out = false; 362 iscsi_reconnect(iscsilun->iscsi); 363 } 364 365 /* newer versions of libiscsi may return zero events. Ensure we are able 366 * to return to service once this situation changes. */ 367 iscsi_set_events(iscsilun); 368 369 timer_mod(iscsilun->event_timer, 370 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); 371 } 372 373 static void 374 iscsi_process_read(void *arg) 375 { 376 IscsiLun *iscsilun = arg; 377 struct iscsi_context *iscsi = iscsilun->iscsi; 378 379 iscsi_service(iscsi, POLLIN); 380 iscsi_set_events(iscsilun); 381 } 382 383 static void 384 iscsi_process_write(void *arg) 385 { 386 IscsiLun *iscsilun = arg; 387 struct iscsi_context *iscsi = iscsilun->iscsi; 388 389 iscsi_service(iscsi, POLLOUT); 390 iscsi_set_events(iscsilun); 391 } 392 393 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun) 394 { 395 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE; 396 } 397 398 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun) 399 { 400 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size; 401 } 402 403 static bool is_byte_request_lun_aligned(int64_t offset, int count, 404 IscsiLun *iscsilun) 405 { 406 if (offset % iscsilun->block_size || count % iscsilun->block_size) { 407 error_report("iSCSI misaligned request: " 408 "iscsilun->block_size %u, offset %" PRIi64 409 ", count %d", 410 iscsilun->block_size, offset, count); 411 return false; 412 } 413 return true; 414 } 415 416 static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors, 417 IscsiLun *iscsilun) 418 { 419 assert(nb_sectors <= BDRV_REQUEST_MAX_SECTORS); 420 return is_byte_request_lun_aligned(sector_num << BDRV_SECTOR_BITS, 421 nb_sectors << BDRV_SECTOR_BITS, 422 iscsilun); 423 } 424 425 static unsigned long *iscsi_allocationmap_init(IscsiLun *iscsilun) 426 { 427 return bitmap_try_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks, 428 iscsilun), 429 iscsilun->cluster_sectors)); 430 } 431 432 static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num, 433 int nb_sectors) 434 { 435 if (iscsilun->allocationmap == NULL) { 436 return; 437 } 438 bitmap_set(iscsilun->allocationmap, 439 sector_num / iscsilun->cluster_sectors, 440 DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors)); 441 } 442 443 static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num, 444 int nb_sectors) 445 { 446 int64_t cluster_num, nb_clusters; 447 if (iscsilun->allocationmap == NULL) { 448 return; 449 } 450 cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors); 451 nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors 452 - cluster_num; 453 if (nb_clusters > 0) { 454 bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters); 455 } 456 } 457 458 static int coroutine_fn 459 iscsi_co_writev_flags(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 460 QEMUIOVector *iov, int flags) 461 { 462 IscsiLun *iscsilun = bs->opaque; 463 struct IscsiTask iTask; 464 uint64_t lba; 465 uint32_t num_sectors; 466 bool fua = flags & BDRV_REQ_FUA; 467 468 if (fua) { 469 assert(iscsilun->dpofua); 470 } 471 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { 472 return -EINVAL; 473 } 474 475 if (bs->bl.max_transfer && 476 nb_sectors << BDRV_SECTOR_BITS > bs->bl.max_transfer) { 477 error_report("iSCSI Error: Write of %d sectors exceeds max_xfer_len " 478 "of %" PRIu32 " bytes", nb_sectors, bs->bl.max_transfer); 479 return -EINVAL; 480 } 481 482 lba = sector_qemu2lun(sector_num, iscsilun); 483 num_sectors = sector_qemu2lun(nb_sectors, iscsilun); 484 iscsi_co_init_iscsitask(iscsilun, &iTask); 485 retry: 486 if (iscsilun->use_16_for_rw) { 487 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba, 488 NULL, num_sectors * iscsilun->block_size, 489 iscsilun->block_size, 0, 0, fua, 0, 0, 490 iscsi_co_generic_cb, &iTask); 491 } else { 492 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba, 493 NULL, num_sectors * iscsilun->block_size, 494 iscsilun->block_size, 0, 0, fua, 0, 0, 495 iscsi_co_generic_cb, &iTask); 496 } 497 if (iTask.task == NULL) { 498 return -ENOMEM; 499 } 500 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov, 501 iov->niov); 502 while (!iTask.complete) { 503 iscsi_set_events(iscsilun); 504 qemu_coroutine_yield(); 505 } 506 507 if (iTask.task != NULL) { 508 scsi_free_scsi_task(iTask.task); 509 iTask.task = NULL; 510 } 511 512 if (iTask.do_retry) { 513 iTask.complete = 0; 514 goto retry; 515 } 516 517 if (iTask.status != SCSI_STATUS_GOOD) { 518 return iTask.err_code; 519 } 520 521 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors); 522 523 return 0; 524 } 525 526 527 static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun, 528 int64_t sector_num, int nb_sectors) 529 { 530 unsigned long size; 531 if (iscsilun->allocationmap == NULL) { 532 return true; 533 } 534 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors); 535 return !(find_next_bit(iscsilun->allocationmap, size, 536 sector_num / iscsilun->cluster_sectors) == size); 537 } 538 539 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs, 540 int64_t sector_num, 541 int nb_sectors, int *pnum, 542 BlockDriverState **file) 543 { 544 IscsiLun *iscsilun = bs->opaque; 545 struct scsi_get_lba_status *lbas = NULL; 546 struct scsi_lba_status_descriptor *lbasd = NULL; 547 struct IscsiTask iTask; 548 int64_t ret; 549 550 iscsi_co_init_iscsitask(iscsilun, &iTask); 551 552 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { 553 ret = -EINVAL; 554 goto out; 555 } 556 557 /* default to all sectors allocated */ 558 ret = BDRV_BLOCK_DATA; 559 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID; 560 *pnum = nb_sectors; 561 562 /* LUN does not support logical block provisioning */ 563 if (!iscsilun->lbpme) { 564 goto out; 565 } 566 567 retry: 568 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun, 569 sector_qemu2lun(sector_num, iscsilun), 570 8 + 16, iscsi_co_generic_cb, 571 &iTask) == NULL) { 572 ret = -ENOMEM; 573 goto out; 574 } 575 576 while (!iTask.complete) { 577 iscsi_set_events(iscsilun); 578 qemu_coroutine_yield(); 579 } 580 581 if (iTask.do_retry) { 582 if (iTask.task != NULL) { 583 scsi_free_scsi_task(iTask.task); 584 iTask.task = NULL; 585 } 586 iTask.complete = 0; 587 goto retry; 588 } 589 590 if (iTask.status != SCSI_STATUS_GOOD) { 591 /* in case the get_lba_status_callout fails (i.e. 592 * because the device is busy or the cmd is not 593 * supported) we pretend all blocks are allocated 594 * for backwards compatibility */ 595 goto out; 596 } 597 598 lbas = scsi_datain_unmarshall(iTask.task); 599 if (lbas == NULL) { 600 ret = -EIO; 601 goto out; 602 } 603 604 lbasd = &lbas->descriptors[0]; 605 606 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) { 607 ret = -EIO; 608 goto out; 609 } 610 611 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun); 612 613 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED || 614 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) { 615 ret &= ~BDRV_BLOCK_DATA; 616 if (iscsilun->lbprz) { 617 ret |= BDRV_BLOCK_ZERO; 618 } 619 } 620 621 if (ret & BDRV_BLOCK_ZERO) { 622 iscsi_allocationmap_clear(iscsilun, sector_num, *pnum); 623 } else { 624 iscsi_allocationmap_set(iscsilun, sector_num, *pnum); 625 } 626 627 if (*pnum > nb_sectors) { 628 *pnum = nb_sectors; 629 } 630 out: 631 if (iTask.task != NULL) { 632 scsi_free_scsi_task(iTask.task); 633 } 634 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) { 635 *file = bs; 636 } 637 return ret; 638 } 639 640 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs, 641 int64_t sector_num, int nb_sectors, 642 QEMUIOVector *iov) 643 { 644 IscsiLun *iscsilun = bs->opaque; 645 struct IscsiTask iTask; 646 uint64_t lba; 647 uint32_t num_sectors; 648 649 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { 650 return -EINVAL; 651 } 652 653 if (bs->bl.max_transfer && 654 nb_sectors << BDRV_SECTOR_BITS > bs->bl.max_transfer) { 655 error_report("iSCSI Error: Read of %d sectors exceeds max_xfer_len " 656 "of %" PRIu32 " bytes", nb_sectors, bs->bl.max_transfer); 657 return -EINVAL; 658 } 659 660 if (iscsilun->lbprz && nb_sectors >= ISCSI_CHECKALLOC_THRES && 661 !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) { 662 int64_t ret; 663 int pnum; 664 BlockDriverState *file; 665 ret = iscsi_co_get_block_status(bs, sector_num, 666 BDRV_REQUEST_MAX_SECTORS, &pnum, &file); 667 if (ret < 0) { 668 return ret; 669 } 670 if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) { 671 qemu_iovec_memset(iov, 0, 0x00, iov->size); 672 return 0; 673 } 674 } 675 676 lba = sector_qemu2lun(sector_num, iscsilun); 677 num_sectors = sector_qemu2lun(nb_sectors, iscsilun); 678 679 iscsi_co_init_iscsitask(iscsilun, &iTask); 680 retry: 681 if (iscsilun->use_16_for_rw) { 682 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba, 683 num_sectors * iscsilun->block_size, 684 iscsilun->block_size, 0, 0, 0, 0, 0, 685 iscsi_co_generic_cb, &iTask); 686 } else { 687 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba, 688 num_sectors * iscsilun->block_size, 689 iscsilun->block_size, 690 0, 0, 0, 0, 0, 691 iscsi_co_generic_cb, &iTask); 692 } 693 if (iTask.task == NULL) { 694 return -ENOMEM; 695 } 696 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov); 697 698 while (!iTask.complete) { 699 iscsi_set_events(iscsilun); 700 qemu_coroutine_yield(); 701 } 702 703 if (iTask.task != NULL) { 704 scsi_free_scsi_task(iTask.task); 705 iTask.task = NULL; 706 } 707 708 if (iTask.do_retry) { 709 iTask.complete = 0; 710 goto retry; 711 } 712 713 if (iTask.status != SCSI_STATUS_GOOD) { 714 return iTask.err_code; 715 } 716 717 return 0; 718 } 719 720 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs) 721 { 722 IscsiLun *iscsilun = bs->opaque; 723 struct IscsiTask iTask; 724 725 iscsi_co_init_iscsitask(iscsilun, &iTask); 726 retry: 727 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0, 728 0, iscsi_co_generic_cb, &iTask) == NULL) { 729 return -ENOMEM; 730 } 731 732 while (!iTask.complete) { 733 iscsi_set_events(iscsilun); 734 qemu_coroutine_yield(); 735 } 736 737 if (iTask.task != NULL) { 738 scsi_free_scsi_task(iTask.task); 739 iTask.task = NULL; 740 } 741 742 if (iTask.do_retry) { 743 iTask.complete = 0; 744 goto retry; 745 } 746 747 if (iTask.status != SCSI_STATUS_GOOD) { 748 return iTask.err_code; 749 } 750 751 return 0; 752 } 753 754 #ifdef __linux__ 755 static void 756 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status, 757 void *command_data, void *opaque) 758 { 759 IscsiAIOCB *acb = opaque; 760 761 g_free(acb->buf); 762 acb->buf = NULL; 763 764 acb->status = 0; 765 if (status < 0) { 766 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s", 767 iscsi_get_error(iscsi)); 768 acb->status = iscsi_translate_sense(&acb->task->sense); 769 } 770 771 acb->ioh->driver_status = 0; 772 acb->ioh->host_status = 0; 773 acb->ioh->resid = 0; 774 acb->ioh->status = status; 775 776 #define SG_ERR_DRIVER_SENSE 0x08 777 778 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) { 779 int ss; 780 781 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE; 782 783 acb->ioh->sb_len_wr = acb->task->datain.size - 2; 784 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ? 785 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr; 786 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss); 787 } 788 789 iscsi_schedule_bh(acb); 790 } 791 792 static void iscsi_ioctl_bh_completion(void *opaque) 793 { 794 IscsiAIOCB *acb = opaque; 795 796 qemu_bh_delete(acb->bh); 797 acb->common.cb(acb->common.opaque, acb->ret); 798 qemu_aio_unref(acb); 799 } 800 801 static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf) 802 { 803 BlockDriverState *bs = acb->common.bs; 804 IscsiLun *iscsilun = bs->opaque; 805 int ret = 0; 806 807 switch (req) { 808 case SG_GET_VERSION_NUM: 809 *(int *)buf = 30000; 810 break; 811 case SG_GET_SCSI_ID: 812 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type; 813 break; 814 default: 815 ret = -EINVAL; 816 } 817 assert(!acb->bh); 818 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), 819 iscsi_ioctl_bh_completion, acb); 820 acb->ret = ret; 821 qemu_bh_schedule(acb->bh); 822 } 823 824 static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs, 825 unsigned long int req, void *buf, 826 BlockCompletionFunc *cb, void *opaque) 827 { 828 IscsiLun *iscsilun = bs->opaque; 829 struct iscsi_context *iscsi = iscsilun->iscsi; 830 struct iscsi_data data; 831 IscsiAIOCB *acb; 832 833 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque); 834 835 acb->iscsilun = iscsilun; 836 acb->bh = NULL; 837 acb->status = -EINPROGRESS; 838 acb->buf = NULL; 839 acb->ioh = buf; 840 841 if (req != SG_IO) { 842 iscsi_ioctl_handle_emulated(acb, req, buf); 843 return &acb->common; 844 } 845 846 if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) { 847 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)", 848 acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE); 849 qemu_aio_unref(acb); 850 return NULL; 851 } 852 853 acb->task = malloc(sizeof(struct scsi_task)); 854 if (acb->task == NULL) { 855 error_report("iSCSI: Failed to allocate task for scsi command. %s", 856 iscsi_get_error(iscsi)); 857 qemu_aio_unref(acb); 858 return NULL; 859 } 860 memset(acb->task, 0, sizeof(struct scsi_task)); 861 862 switch (acb->ioh->dxfer_direction) { 863 case SG_DXFER_TO_DEV: 864 acb->task->xfer_dir = SCSI_XFER_WRITE; 865 break; 866 case SG_DXFER_FROM_DEV: 867 acb->task->xfer_dir = SCSI_XFER_READ; 868 break; 869 default: 870 acb->task->xfer_dir = SCSI_XFER_NONE; 871 break; 872 } 873 874 acb->task->cdb_size = acb->ioh->cmd_len; 875 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len); 876 acb->task->expxferlen = acb->ioh->dxfer_len; 877 878 data.size = 0; 879 if (acb->task->xfer_dir == SCSI_XFER_WRITE) { 880 if (acb->ioh->iovec_count == 0) { 881 data.data = acb->ioh->dxferp; 882 data.size = acb->ioh->dxfer_len; 883 } else { 884 scsi_task_set_iov_out(acb->task, 885 (struct scsi_iovec *) acb->ioh->dxferp, 886 acb->ioh->iovec_count); 887 } 888 } 889 890 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task, 891 iscsi_aio_ioctl_cb, 892 (data.size > 0) ? &data : NULL, 893 acb) != 0) { 894 scsi_free_scsi_task(acb->task); 895 qemu_aio_unref(acb); 896 return NULL; 897 } 898 899 /* tell libiscsi to read straight into the buffer we got from ioctl */ 900 if (acb->task->xfer_dir == SCSI_XFER_READ) { 901 if (acb->ioh->iovec_count == 0) { 902 scsi_task_add_data_in_buffer(acb->task, 903 acb->ioh->dxfer_len, 904 acb->ioh->dxferp); 905 } else { 906 scsi_task_set_iov_in(acb->task, 907 (struct scsi_iovec *) acb->ioh->dxferp, 908 acb->ioh->iovec_count); 909 } 910 } 911 912 iscsi_set_events(iscsilun); 913 914 return &acb->common; 915 } 916 917 #endif 918 919 static int64_t 920 iscsi_getlength(BlockDriverState *bs) 921 { 922 IscsiLun *iscsilun = bs->opaque; 923 int64_t len; 924 925 len = iscsilun->num_blocks; 926 len *= iscsilun->block_size; 927 928 return len; 929 } 930 931 static int 932 coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num, 933 int nb_sectors) 934 { 935 IscsiLun *iscsilun = bs->opaque; 936 struct IscsiTask iTask; 937 struct unmap_list list; 938 939 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) { 940 return -EINVAL; 941 } 942 943 if (!iscsilun->lbp.lbpu) { 944 /* UNMAP is not supported by the target */ 945 return 0; 946 } 947 948 list.lba = sector_qemu2lun(sector_num, iscsilun); 949 list.num = sector_qemu2lun(nb_sectors, iscsilun); 950 951 iscsi_co_init_iscsitask(iscsilun, &iTask); 952 retry: 953 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1, 954 iscsi_co_generic_cb, &iTask) == NULL) { 955 return -ENOMEM; 956 } 957 958 while (!iTask.complete) { 959 iscsi_set_events(iscsilun); 960 qemu_coroutine_yield(); 961 } 962 963 if (iTask.task != NULL) { 964 scsi_free_scsi_task(iTask.task); 965 iTask.task = NULL; 966 } 967 968 if (iTask.do_retry) { 969 iTask.complete = 0; 970 goto retry; 971 } 972 973 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) { 974 /* the target might fail with a check condition if it 975 is not happy with the alignment of the UNMAP request 976 we silently fail in this case */ 977 return 0; 978 } 979 980 if (iTask.status != SCSI_STATUS_GOOD) { 981 return iTask.err_code; 982 } 983 984 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors); 985 986 return 0; 987 } 988 989 static int 990 coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, 991 int count, BdrvRequestFlags flags) 992 { 993 IscsiLun *iscsilun = bs->opaque; 994 struct IscsiTask iTask; 995 uint64_t lba; 996 uint32_t nb_blocks; 997 bool use_16_for_ws = iscsilun->use_16_for_rw; 998 999 if (!is_byte_request_lun_aligned(offset, count, iscsilun)) { 1000 return -ENOTSUP; 1001 } 1002 1003 if (flags & BDRV_REQ_MAY_UNMAP) { 1004 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) { 1005 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */ 1006 use_16_for_ws = true; 1007 } 1008 if (use_16_for_ws && !iscsilun->lbp.lbpws) { 1009 /* WRITESAME16 with UNMAP is not supported by the target, 1010 * fall back and try WRITESAME10/16 without UNMAP */ 1011 flags &= ~BDRV_REQ_MAY_UNMAP; 1012 use_16_for_ws = iscsilun->use_16_for_rw; 1013 } 1014 } 1015 1016 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) { 1017 /* WRITESAME without UNMAP is not supported by the target */ 1018 return -ENOTSUP; 1019 } 1020 1021 lba = offset / iscsilun->block_size; 1022 nb_blocks = count / iscsilun->block_size; 1023 1024 if (iscsilun->zeroblock == NULL) { 1025 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size); 1026 if (iscsilun->zeroblock == NULL) { 1027 return -ENOMEM; 1028 } 1029 } 1030 1031 iscsi_co_init_iscsitask(iscsilun, &iTask); 1032 retry: 1033 if (use_16_for_ws) { 1034 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba, 1035 iscsilun->zeroblock, iscsilun->block_size, 1036 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), 1037 0, 0, iscsi_co_generic_cb, &iTask); 1038 } else { 1039 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba, 1040 iscsilun->zeroblock, iscsilun->block_size, 1041 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP), 1042 0, 0, iscsi_co_generic_cb, &iTask); 1043 } 1044 if (iTask.task == NULL) { 1045 return -ENOMEM; 1046 } 1047 1048 while (!iTask.complete) { 1049 iscsi_set_events(iscsilun); 1050 qemu_coroutine_yield(); 1051 } 1052 1053 if (iTask.status == SCSI_STATUS_CHECK_CONDITION && 1054 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST && 1055 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE || 1056 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) { 1057 /* WRITE SAME is not supported by the target */ 1058 iscsilun->has_write_same = false; 1059 scsi_free_scsi_task(iTask.task); 1060 return -ENOTSUP; 1061 } 1062 1063 if (iTask.task != NULL) { 1064 scsi_free_scsi_task(iTask.task); 1065 iTask.task = NULL; 1066 } 1067 1068 if (iTask.do_retry) { 1069 iTask.complete = 0; 1070 goto retry; 1071 } 1072 1073 if (iTask.status != SCSI_STATUS_GOOD) { 1074 return iTask.err_code; 1075 } 1076 1077 if (flags & BDRV_REQ_MAY_UNMAP) { 1078 iscsi_allocationmap_clear(iscsilun, offset >> BDRV_SECTOR_BITS, 1079 count >> BDRV_SECTOR_BITS); 1080 } else { 1081 iscsi_allocationmap_set(iscsilun, offset >> BDRV_SECTOR_BITS, 1082 count >> BDRV_SECTOR_BITS); 1083 } 1084 1085 return 0; 1086 } 1087 1088 static void parse_chap(struct iscsi_context *iscsi, const char *target, 1089 Error **errp) 1090 { 1091 QemuOptsList *list; 1092 QemuOpts *opts; 1093 const char *user = NULL; 1094 const char *password = NULL; 1095 const char *secretid; 1096 char *secret = NULL; 1097 1098 list = qemu_find_opts("iscsi"); 1099 if (!list) { 1100 return; 1101 } 1102 1103 opts = qemu_opts_find(list, target); 1104 if (opts == NULL) { 1105 opts = QTAILQ_FIRST(&list->head); 1106 if (!opts) { 1107 return; 1108 } 1109 } 1110 1111 user = qemu_opt_get(opts, "user"); 1112 if (!user) { 1113 return; 1114 } 1115 1116 secretid = qemu_opt_get(opts, "password-secret"); 1117 password = qemu_opt_get(opts, "password"); 1118 if (secretid && password) { 1119 error_setg(errp, "'password' and 'password-secret' properties are " 1120 "mutually exclusive"); 1121 return; 1122 } 1123 if (secretid) { 1124 secret = qcrypto_secret_lookup_as_utf8(secretid, errp); 1125 if (!secret) { 1126 return; 1127 } 1128 password = secret; 1129 } else if (!password) { 1130 error_setg(errp, "CHAP username specified but no password was given"); 1131 return; 1132 } 1133 1134 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) { 1135 error_setg(errp, "Failed to set initiator username and password"); 1136 } 1137 1138 g_free(secret); 1139 } 1140 1141 static void parse_header_digest(struct iscsi_context *iscsi, const char *target, 1142 Error **errp) 1143 { 1144 QemuOptsList *list; 1145 QemuOpts *opts; 1146 const char *digest = NULL; 1147 1148 list = qemu_find_opts("iscsi"); 1149 if (!list) { 1150 return; 1151 } 1152 1153 opts = qemu_opts_find(list, target); 1154 if (opts == NULL) { 1155 opts = QTAILQ_FIRST(&list->head); 1156 if (!opts) { 1157 return; 1158 } 1159 } 1160 1161 digest = qemu_opt_get(opts, "header-digest"); 1162 if (!digest) { 1163 return; 1164 } 1165 1166 if (!strcmp(digest, "CRC32C")) { 1167 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C); 1168 } else if (!strcmp(digest, "NONE")) { 1169 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE); 1170 } else if (!strcmp(digest, "CRC32C-NONE")) { 1171 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE); 1172 } else if (!strcmp(digest, "NONE-CRC32C")) { 1173 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); 1174 } else { 1175 error_setg(errp, "Invalid header-digest setting : %s", digest); 1176 } 1177 } 1178 1179 static char *parse_initiator_name(const char *target) 1180 { 1181 QemuOptsList *list; 1182 QemuOpts *opts; 1183 const char *name; 1184 char *iscsi_name; 1185 UuidInfo *uuid_info; 1186 1187 list = qemu_find_opts("iscsi"); 1188 if (list) { 1189 opts = qemu_opts_find(list, target); 1190 if (!opts) { 1191 opts = QTAILQ_FIRST(&list->head); 1192 } 1193 if (opts) { 1194 name = qemu_opt_get(opts, "initiator-name"); 1195 if (name) { 1196 return g_strdup(name); 1197 } 1198 } 1199 } 1200 1201 uuid_info = qmp_query_uuid(NULL); 1202 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) { 1203 name = qemu_get_vm_name(); 1204 } else { 1205 name = uuid_info->UUID; 1206 } 1207 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s", 1208 name ? ":" : "", name ? name : ""); 1209 qapi_free_UuidInfo(uuid_info); 1210 return iscsi_name; 1211 } 1212 1213 static int parse_timeout(const char *target) 1214 { 1215 QemuOptsList *list; 1216 QemuOpts *opts; 1217 const char *timeout; 1218 1219 list = qemu_find_opts("iscsi"); 1220 if (list) { 1221 opts = qemu_opts_find(list, target); 1222 if (!opts) { 1223 opts = QTAILQ_FIRST(&list->head); 1224 } 1225 if (opts) { 1226 timeout = qemu_opt_get(opts, "timeout"); 1227 if (timeout) { 1228 return atoi(timeout); 1229 } 1230 } 1231 } 1232 1233 return 0; 1234 } 1235 1236 static void iscsi_nop_timed_event(void *opaque) 1237 { 1238 IscsiLun *iscsilun = opaque; 1239 1240 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) { 1241 error_report("iSCSI: NOP timeout. Reconnecting..."); 1242 iscsilun->request_timed_out = true; 1243 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) { 1244 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages."); 1245 return; 1246 } 1247 1248 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); 1249 iscsi_set_events(iscsilun); 1250 } 1251 1252 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp) 1253 { 1254 struct scsi_task *task = NULL; 1255 struct scsi_readcapacity10 *rc10 = NULL; 1256 struct scsi_readcapacity16 *rc16 = NULL; 1257 int retries = ISCSI_CMD_RETRIES; 1258 1259 do { 1260 if (task != NULL) { 1261 scsi_free_scsi_task(task); 1262 task = NULL; 1263 } 1264 1265 switch (iscsilun->type) { 1266 case TYPE_DISK: 1267 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun); 1268 if (task != NULL && task->status == SCSI_STATUS_GOOD) { 1269 rc16 = scsi_datain_unmarshall(task); 1270 if (rc16 == NULL) { 1271 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data."); 1272 } else { 1273 iscsilun->block_size = rc16->block_length; 1274 iscsilun->num_blocks = rc16->returned_lba + 1; 1275 iscsilun->lbpme = !!rc16->lbpme; 1276 iscsilun->lbprz = !!rc16->lbprz; 1277 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff); 1278 } 1279 break; 1280 } 1281 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION 1282 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) { 1283 break; 1284 } 1285 /* Fall through and try READ CAPACITY(10) instead. */ 1286 case TYPE_ROM: 1287 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0); 1288 if (task != NULL && task->status == SCSI_STATUS_GOOD) { 1289 rc10 = scsi_datain_unmarshall(task); 1290 if (rc10 == NULL) { 1291 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data."); 1292 } else { 1293 iscsilun->block_size = rc10->block_size; 1294 if (rc10->lba == 0) { 1295 /* blank disk loaded */ 1296 iscsilun->num_blocks = 0; 1297 } else { 1298 iscsilun->num_blocks = rc10->lba + 1; 1299 } 1300 } 1301 } 1302 break; 1303 default: 1304 return; 1305 } 1306 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION 1307 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION 1308 && retries-- > 0); 1309 1310 if (task == NULL || task->status != SCSI_STATUS_GOOD) { 1311 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command"); 1312 } else if (!iscsilun->block_size || 1313 iscsilun->block_size % BDRV_SECTOR_SIZE) { 1314 error_setg(errp, "iSCSI: the target returned an invalid " 1315 "block size of %d.", iscsilun->block_size); 1316 } 1317 if (task) { 1318 scsi_free_scsi_task(task); 1319 } 1320 } 1321 1322 /* TODO Convert to fine grained options */ 1323 static QemuOptsList runtime_opts = { 1324 .name = "iscsi", 1325 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 1326 .desc = { 1327 { 1328 .name = "filename", 1329 .type = QEMU_OPT_STRING, 1330 .help = "URL to the iscsi image", 1331 }, 1332 { /* end of list */ } 1333 }, 1334 }; 1335 1336 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun, 1337 int evpd, int pc, void **inq, Error **errp) 1338 { 1339 int full_size; 1340 struct scsi_task *task = NULL; 1341 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64); 1342 if (task == NULL || task->status != SCSI_STATUS_GOOD) { 1343 goto fail; 1344 } 1345 full_size = scsi_datain_getfullsize(task); 1346 if (full_size > task->datain.size) { 1347 scsi_free_scsi_task(task); 1348 1349 /* we need more data for the full list */ 1350 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size); 1351 if (task == NULL || task->status != SCSI_STATUS_GOOD) { 1352 goto fail; 1353 } 1354 } 1355 1356 *inq = scsi_datain_unmarshall(task); 1357 if (*inq == NULL) { 1358 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob"); 1359 goto fail_with_err; 1360 } 1361 1362 return task; 1363 1364 fail: 1365 error_setg(errp, "iSCSI: Inquiry command failed : %s", 1366 iscsi_get_error(iscsi)); 1367 fail_with_err: 1368 if (task != NULL) { 1369 scsi_free_scsi_task(task); 1370 } 1371 return NULL; 1372 } 1373 1374 static void iscsi_detach_aio_context(BlockDriverState *bs) 1375 { 1376 IscsiLun *iscsilun = bs->opaque; 1377 1378 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi), 1379 false, NULL, NULL, NULL); 1380 iscsilun->events = 0; 1381 1382 if (iscsilun->nop_timer) { 1383 timer_del(iscsilun->nop_timer); 1384 timer_free(iscsilun->nop_timer); 1385 iscsilun->nop_timer = NULL; 1386 } 1387 if (iscsilun->event_timer) { 1388 timer_del(iscsilun->event_timer); 1389 timer_free(iscsilun->event_timer); 1390 iscsilun->event_timer = NULL; 1391 } 1392 } 1393 1394 static void iscsi_attach_aio_context(BlockDriverState *bs, 1395 AioContext *new_context) 1396 { 1397 IscsiLun *iscsilun = bs->opaque; 1398 1399 iscsilun->aio_context = new_context; 1400 iscsi_set_events(iscsilun); 1401 1402 /* Set up a timer for sending out iSCSI NOPs */ 1403 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context, 1404 QEMU_CLOCK_REALTIME, SCALE_MS, 1405 iscsi_nop_timed_event, iscsilun); 1406 timer_mod(iscsilun->nop_timer, 1407 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL); 1408 1409 /* Set up a timer for periodic calls to iscsi_set_events and to 1410 * scan for command timeout */ 1411 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context, 1412 QEMU_CLOCK_REALTIME, SCALE_MS, 1413 iscsi_timed_check_events, iscsilun); 1414 timer_mod(iscsilun->event_timer, 1415 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL); 1416 } 1417 1418 static void iscsi_modesense_sync(IscsiLun *iscsilun) 1419 { 1420 struct scsi_task *task; 1421 struct scsi_mode_sense *ms = NULL; 1422 iscsilun->write_protected = false; 1423 iscsilun->dpofua = false; 1424 1425 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun, 1426 1, SCSI_MODESENSE_PC_CURRENT, 1427 0x3F, 0, 255); 1428 if (task == NULL) { 1429 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s", 1430 iscsi_get_error(iscsilun->iscsi)); 1431 goto out; 1432 } 1433 1434 if (task->status != SCSI_STATUS_GOOD) { 1435 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable"); 1436 goto out; 1437 } 1438 ms = scsi_datain_unmarshall(task); 1439 if (!ms) { 1440 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s", 1441 iscsi_get_error(iscsilun->iscsi)); 1442 goto out; 1443 } 1444 iscsilun->write_protected = ms->device_specific_parameter & 0x80; 1445 iscsilun->dpofua = ms->device_specific_parameter & 0x10; 1446 1447 out: 1448 if (task) { 1449 scsi_free_scsi_task(task); 1450 } 1451 } 1452 1453 /* 1454 * We support iscsi url's on the form 1455 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun> 1456 */ 1457 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, 1458 Error **errp) 1459 { 1460 IscsiLun *iscsilun = bs->opaque; 1461 struct iscsi_context *iscsi = NULL; 1462 struct iscsi_url *iscsi_url = NULL; 1463 struct scsi_task *task = NULL; 1464 struct scsi_inquiry_standard *inq = NULL; 1465 struct scsi_inquiry_supported_pages *inq_vpd; 1466 char *initiator_name = NULL; 1467 QemuOpts *opts; 1468 Error *local_err = NULL; 1469 const char *filename; 1470 int i, ret = 0, timeout = 0; 1471 1472 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 1473 qemu_opts_absorb_qdict(opts, options, &local_err); 1474 if (local_err) { 1475 error_propagate(errp, local_err); 1476 ret = -EINVAL; 1477 goto out; 1478 } 1479 1480 filename = qemu_opt_get(opts, "filename"); 1481 1482 iscsi_url = iscsi_parse_full_url(iscsi, filename); 1483 if (iscsi_url == NULL) { 1484 error_setg(errp, "Failed to parse URL : %s", filename); 1485 ret = -EINVAL; 1486 goto out; 1487 } 1488 1489 memset(iscsilun, 0, sizeof(IscsiLun)); 1490 1491 initiator_name = parse_initiator_name(iscsi_url->target); 1492 1493 iscsi = iscsi_create_context(initiator_name); 1494 if (iscsi == NULL) { 1495 error_setg(errp, "iSCSI: Failed to create iSCSI context."); 1496 ret = -ENOMEM; 1497 goto out; 1498 } 1499 1500 if (iscsi_set_targetname(iscsi, iscsi_url->target)) { 1501 error_setg(errp, "iSCSI: Failed to set target name."); 1502 ret = -EINVAL; 1503 goto out; 1504 } 1505 1506 if (iscsi_url->user[0] != '\0') { 1507 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user, 1508 iscsi_url->passwd); 1509 if (ret != 0) { 1510 error_setg(errp, "Failed to set initiator username and password"); 1511 ret = -EINVAL; 1512 goto out; 1513 } 1514 } 1515 1516 /* check if we got CHAP username/password via the options */ 1517 parse_chap(iscsi, iscsi_url->target, &local_err); 1518 if (local_err != NULL) { 1519 error_propagate(errp, local_err); 1520 ret = -EINVAL; 1521 goto out; 1522 } 1523 1524 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { 1525 error_setg(errp, "iSCSI: Failed to set session type to normal."); 1526 ret = -EINVAL; 1527 goto out; 1528 } 1529 1530 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C); 1531 1532 /* check if we got HEADER_DIGEST via the options */ 1533 parse_header_digest(iscsi, iscsi_url->target, &local_err); 1534 if (local_err != NULL) { 1535 error_propagate(errp, local_err); 1536 ret = -EINVAL; 1537 goto out; 1538 } 1539 1540 /* timeout handling is broken in libiscsi before 1.15.0 */ 1541 timeout = parse_timeout(iscsi_url->target); 1542 #if defined(LIBISCSI_API_VERSION) && LIBISCSI_API_VERSION >= 20150621 1543 iscsi_set_timeout(iscsi, timeout); 1544 #else 1545 if (timeout) { 1546 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0"); 1547 } 1548 #endif 1549 1550 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) { 1551 error_setg(errp, "iSCSI: Failed to connect to LUN : %s", 1552 iscsi_get_error(iscsi)); 1553 ret = -EINVAL; 1554 goto out; 1555 } 1556 1557 iscsilun->iscsi = iscsi; 1558 iscsilun->aio_context = bdrv_get_aio_context(bs); 1559 iscsilun->lun = iscsi_url->lun; 1560 iscsilun->has_write_same = true; 1561 1562 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0, 1563 (void **) &inq, errp); 1564 if (task == NULL) { 1565 ret = -EINVAL; 1566 goto out; 1567 } 1568 iscsilun->type = inq->periperal_device_type; 1569 scsi_free_scsi_task(task); 1570 task = NULL; 1571 1572 iscsi_modesense_sync(iscsilun); 1573 if (iscsilun->dpofua) { 1574 bs->supported_write_flags = BDRV_REQ_FUA; 1575 } 1576 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP; 1577 1578 /* Check the write protect flag of the LUN if we want to write */ 1579 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) && 1580 iscsilun->write_protected) { 1581 error_setg(errp, "Cannot open a write protected LUN as read-write"); 1582 ret = -EACCES; 1583 goto out; 1584 } 1585 1586 iscsi_readcapacity_sync(iscsilun, &local_err); 1587 if (local_err != NULL) { 1588 error_propagate(errp, local_err); 1589 ret = -EINVAL; 1590 goto out; 1591 } 1592 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun); 1593 1594 /* We don't have any emulation for devices other than disks and CD-ROMs, so 1595 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu 1596 * will try to read from the device to guess the image format. 1597 */ 1598 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) { 1599 bs->sg = true; 1600 } 1601 1602 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, 1603 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES, 1604 (void **) &inq_vpd, errp); 1605 if (task == NULL) { 1606 ret = -EINVAL; 1607 goto out; 1608 } 1609 for (i = 0; i < inq_vpd->num_pages; i++) { 1610 struct scsi_task *inq_task; 1611 struct scsi_inquiry_logical_block_provisioning *inq_lbp; 1612 struct scsi_inquiry_block_limits *inq_bl; 1613 switch (inq_vpd->pages[i]) { 1614 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING: 1615 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, 1616 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING, 1617 (void **) &inq_lbp, errp); 1618 if (inq_task == NULL) { 1619 ret = -EINVAL; 1620 goto out; 1621 } 1622 memcpy(&iscsilun->lbp, inq_lbp, 1623 sizeof(struct scsi_inquiry_logical_block_provisioning)); 1624 scsi_free_scsi_task(inq_task); 1625 break; 1626 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS: 1627 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1, 1628 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS, 1629 (void **) &inq_bl, errp); 1630 if (inq_task == NULL) { 1631 ret = -EINVAL; 1632 goto out; 1633 } 1634 memcpy(&iscsilun->bl, inq_bl, 1635 sizeof(struct scsi_inquiry_block_limits)); 1636 scsi_free_scsi_task(inq_task); 1637 break; 1638 default: 1639 break; 1640 } 1641 } 1642 scsi_free_scsi_task(task); 1643 task = NULL; 1644 1645 iscsi_attach_aio_context(bs, iscsilun->aio_context); 1646 1647 /* Guess the internal cluster (page) size of the iscsi target by the means 1648 * of opt_unmap_gran. Transfer the unmap granularity only if it has a 1649 * reasonable size */ 1650 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 && 1651 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) { 1652 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran * 1653 iscsilun->block_size) >> BDRV_SECTOR_BITS; 1654 if (iscsilun->lbprz) { 1655 iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun); 1656 if (iscsilun->allocationmap == NULL) { 1657 ret = -ENOMEM; 1658 } 1659 } 1660 } 1661 1662 out: 1663 qemu_opts_del(opts); 1664 g_free(initiator_name); 1665 if (iscsi_url != NULL) { 1666 iscsi_destroy_url(iscsi_url); 1667 } 1668 if (task != NULL) { 1669 scsi_free_scsi_task(task); 1670 } 1671 1672 if (ret) { 1673 if (iscsi != NULL) { 1674 if (iscsi_is_logged_in(iscsi)) { 1675 iscsi_logout_sync(iscsi); 1676 } 1677 iscsi_destroy_context(iscsi); 1678 } 1679 memset(iscsilun, 0, sizeof(IscsiLun)); 1680 } 1681 return ret; 1682 } 1683 1684 static void iscsi_close(BlockDriverState *bs) 1685 { 1686 IscsiLun *iscsilun = bs->opaque; 1687 struct iscsi_context *iscsi = iscsilun->iscsi; 1688 1689 iscsi_detach_aio_context(bs); 1690 if (iscsi_is_logged_in(iscsi)) { 1691 iscsi_logout_sync(iscsi); 1692 } 1693 iscsi_destroy_context(iscsi); 1694 g_free(iscsilun->zeroblock); 1695 g_free(iscsilun->allocationmap); 1696 memset(iscsilun, 0, sizeof(IscsiLun)); 1697 } 1698 1699 static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp) 1700 { 1701 /* We don't actually refresh here, but just return data queried in 1702 * iscsi_open(): iscsi targets don't change their limits. */ 1703 1704 IscsiLun *iscsilun = bs->opaque; 1705 uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff; 1706 1707 bs->bl.request_alignment = iscsilun->block_size; 1708 1709 if (iscsilun->bl.max_xfer_len) { 1710 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len); 1711 } 1712 1713 if (max_xfer_len * iscsilun->block_size < INT_MAX) { 1714 bs->bl.max_transfer = max_xfer_len * iscsilun->block_size; 1715 } 1716 1717 if (iscsilun->lbp.lbpu) { 1718 if (iscsilun->bl.max_unmap < 0xffffffff / iscsilun->block_size) { 1719 bs->bl.max_pdiscard = 1720 iscsilun->bl.max_unmap * iscsilun->block_size; 1721 } 1722 bs->bl.pdiscard_alignment = 1723 iscsilun->bl.opt_unmap_gran * iscsilun->block_size; 1724 } else { 1725 bs->bl.pdiscard_alignment = iscsilun->block_size; 1726 } 1727 1728 if (iscsilun->bl.max_ws_len < 0xffffffff / iscsilun->block_size) { 1729 bs->bl.max_pwrite_zeroes = 1730 iscsilun->bl.max_ws_len * iscsilun->block_size; 1731 } 1732 if (iscsilun->lbp.lbpws) { 1733 bs->bl.pwrite_zeroes_alignment = 1734 iscsilun->bl.opt_unmap_gran * iscsilun->block_size; 1735 } else { 1736 bs->bl.pwrite_zeroes_alignment = iscsilun->block_size; 1737 } 1738 if (iscsilun->bl.opt_xfer_len && 1739 iscsilun->bl.opt_xfer_len < INT_MAX / iscsilun->block_size) { 1740 bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len * 1741 iscsilun->block_size); 1742 } 1743 } 1744 1745 /* Note that this will not re-establish a connection with an iSCSI target - it 1746 * is effectively a NOP. */ 1747 static int iscsi_reopen_prepare(BDRVReopenState *state, 1748 BlockReopenQueue *queue, Error **errp) 1749 { 1750 IscsiLun *iscsilun = state->bs->opaque; 1751 1752 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) { 1753 error_setg(errp, "Cannot open a write protected LUN as read-write"); 1754 return -EACCES; 1755 } 1756 return 0; 1757 } 1758 1759 static int iscsi_truncate(BlockDriverState *bs, int64_t offset) 1760 { 1761 IscsiLun *iscsilun = bs->opaque; 1762 Error *local_err = NULL; 1763 1764 if (iscsilun->type != TYPE_DISK) { 1765 return -ENOTSUP; 1766 } 1767 1768 iscsi_readcapacity_sync(iscsilun, &local_err); 1769 if (local_err != NULL) { 1770 error_free(local_err); 1771 return -EIO; 1772 } 1773 1774 if (offset > iscsi_getlength(bs)) { 1775 return -EINVAL; 1776 } 1777 1778 if (iscsilun->allocationmap != NULL) { 1779 g_free(iscsilun->allocationmap); 1780 iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun); 1781 } 1782 1783 return 0; 1784 } 1785 1786 static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp) 1787 { 1788 int ret = 0; 1789 int64_t total_size = 0; 1790 BlockDriverState *bs; 1791 IscsiLun *iscsilun = NULL; 1792 QDict *bs_options; 1793 1794 bs = bdrv_new(); 1795 1796 /* Read out options */ 1797 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), 1798 BDRV_SECTOR_SIZE); 1799 bs->opaque = g_new0(struct IscsiLun, 1); 1800 iscsilun = bs->opaque; 1801 1802 bs_options = qdict_new(); 1803 qdict_put(bs_options, "filename", qstring_from_str(filename)); 1804 ret = iscsi_open(bs, bs_options, 0, NULL); 1805 QDECREF(bs_options); 1806 1807 if (ret != 0) { 1808 goto out; 1809 } 1810 iscsi_detach_aio_context(bs); 1811 if (iscsilun->type != TYPE_DISK) { 1812 ret = -ENODEV; 1813 goto out; 1814 } 1815 if (bs->total_sectors < total_size) { 1816 ret = -ENOSPC; 1817 goto out; 1818 } 1819 1820 ret = 0; 1821 out: 1822 if (iscsilun->iscsi != NULL) { 1823 iscsi_destroy_context(iscsilun->iscsi); 1824 } 1825 g_free(bs->opaque); 1826 bs->opaque = NULL; 1827 bdrv_unref(bs); 1828 return ret; 1829 } 1830 1831 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1832 { 1833 IscsiLun *iscsilun = bs->opaque; 1834 bdi->unallocated_blocks_are_zero = iscsilun->lbprz; 1835 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws; 1836 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE; 1837 return 0; 1838 } 1839 1840 static QemuOptsList iscsi_create_opts = { 1841 .name = "iscsi-create-opts", 1842 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head), 1843 .desc = { 1844 { 1845 .name = BLOCK_OPT_SIZE, 1846 .type = QEMU_OPT_SIZE, 1847 .help = "Virtual disk size" 1848 }, 1849 { /* end of list */ } 1850 } 1851 }; 1852 1853 static BlockDriver bdrv_iscsi = { 1854 .format_name = "iscsi", 1855 .protocol_name = "iscsi", 1856 1857 .instance_size = sizeof(IscsiLun), 1858 .bdrv_needs_filename = true, 1859 .bdrv_file_open = iscsi_open, 1860 .bdrv_close = iscsi_close, 1861 .bdrv_create = iscsi_create, 1862 .create_opts = &iscsi_create_opts, 1863 .bdrv_reopen_prepare = iscsi_reopen_prepare, 1864 1865 .bdrv_getlength = iscsi_getlength, 1866 .bdrv_get_info = iscsi_get_info, 1867 .bdrv_truncate = iscsi_truncate, 1868 .bdrv_refresh_limits = iscsi_refresh_limits, 1869 1870 .bdrv_co_get_block_status = iscsi_co_get_block_status, 1871 .bdrv_co_discard = iscsi_co_discard, 1872 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes, 1873 .bdrv_co_readv = iscsi_co_readv, 1874 .bdrv_co_writev_flags = iscsi_co_writev_flags, 1875 .bdrv_co_flush_to_disk = iscsi_co_flush, 1876 1877 #ifdef __linux__ 1878 .bdrv_aio_ioctl = iscsi_aio_ioctl, 1879 #endif 1880 1881 .bdrv_detach_aio_context = iscsi_detach_aio_context, 1882 .bdrv_attach_aio_context = iscsi_attach_aio_context, 1883 }; 1884 1885 static QemuOptsList qemu_iscsi_opts = { 1886 .name = "iscsi", 1887 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head), 1888 .desc = { 1889 { 1890 .name = "user", 1891 .type = QEMU_OPT_STRING, 1892 .help = "username for CHAP authentication to target", 1893 },{ 1894 .name = "password", 1895 .type = QEMU_OPT_STRING, 1896 .help = "password for CHAP authentication to target", 1897 },{ 1898 .name = "password-secret", 1899 .type = QEMU_OPT_STRING, 1900 .help = "ID of the secret providing password for CHAP " 1901 "authentication to target", 1902 },{ 1903 .name = "header-digest", 1904 .type = QEMU_OPT_STRING, 1905 .help = "HeaderDigest setting. " 1906 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}", 1907 },{ 1908 .name = "initiator-name", 1909 .type = QEMU_OPT_STRING, 1910 .help = "Initiator iqn name to use when connecting", 1911 },{ 1912 .name = "timeout", 1913 .type = QEMU_OPT_NUMBER, 1914 .help = "Request timeout in seconds (default 0 = no timeout)", 1915 }, 1916 { /* end of list */ } 1917 }, 1918 }; 1919 1920 static void iscsi_block_init(void) 1921 { 1922 bdrv_register(&bdrv_iscsi); 1923 qemu_add_opts(&qemu_iscsi_opts); 1924 } 1925 1926 block_init(iscsi_block_init); 1927