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