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