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