2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2017 Peter Lieven <pl@kamp.de>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #include "qemu/osdep.h"
30 #include <arpa/inet.h>
31 #include "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "qemu/bitops.h"
34 #include "qemu/bitmap.h"
35 #include "block/block_int.h"
36 #include "scsi/constants.h"
38 #include "qemu/option.h"
39 #include "qemu/uuid.h"
40 #include "qapi/error.h"
41 #include "qapi/qapi-commands-misc.h"
42 #include "qapi/qmp/qdict.h"
43 #include "qapi/qmp/qstring.h"
44 #include "crypto/secret.h"
45 #include "scsi/utils.h"
47 /* Conflict between scsi/utils.h and libiscsi! :( */
48 #define SCSI_XFER_NONE ISCSI_XFER_NONE
49 #include <iscsi/iscsi.h>
50 #include <iscsi/scsi-lowlevel.h>
52 QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE != (int)ISCSI_XFER_NONE);
58 typedef struct IscsiLun {
59 struct iscsi_context *iscsi;
60 AioContext *aio_context;
62 enum scsi_inquiry_peripheral_device_type type;
67 QEMUTimer *event_timer;
69 struct scsi_inquiry_logical_block_provisioning lbp;
70 struct scsi_inquiry_block_limits bl;
71 struct scsi_inquiry_device_designator *dd;
72 unsigned char *zeroblock;
73 /* The allocmap tracks which clusters (pages) on the iSCSI target are
74 * allocated and which are not. In case a target returns zeros for
75 * unallocated pages (iscsilun->lprz) we can directly return zeros instead
76 * of reading zeros over the wire if a read request falls within an
77 * unallocated block. As there are 3 possible states we need 2 bitmaps to
78 * track. allocmap_valid keeps track if QEMU's information about a page is
79 * valid. allocmap tracks if a page is allocated or not. In case QEMU has no
80 * valid information about a page the corresponding allocmap entry should be
81 * switched to unallocated as well to force a new lookup of the allocation
82 * status as lookups are generally skipped if a page is suspect to be
83 * allocated. If a iSCSI target is opened with cache.direct = on the
84 * allocmap_valid does not exist turning all cached information invalid so
85 * that a fresh lookup is made for any page even if allocmap entry returns
86 * it's unallocated. */
87 unsigned long *allocmap;
88 unsigned long *allocmap_valid;
97 bool request_timed_out;
100 typedef struct IscsiTask {
105 struct scsi_task *task;
108 QEMUTimer retry_timer;
113 typedef struct IscsiAIOCB {
117 struct scsi_task *task;
128 /* libiscsi uses time_t so its enough to process events every second */
129 #define EVENT_INTERVAL 1000
130 #define NOP_INTERVAL 5000
131 #define MAX_NOP_FAILURES 3
132 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
133 static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768};
135 /* this threshold is a trade-off knob to choose between
136 * the potential additional overhead of an extra GET_LBA_STATUS request
137 * vs. unnecessarily reading a lot of zero sectors over the wire.
138 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
139 * sectors we check the allocation status of the area covered by the
140 * request first if the allocationmap indicates that the area might be
142 #define ISCSI_CHECKALLOC_THRES 64
149 qemu_bh_delete(acb->bh);
154 acb->common.cb(acb->common.opaque, acb->status);
156 if (acb->task != NULL) {
157 scsi_free_scsi_task(acb->task);
165 iscsi_schedule_bh(IscsiAIOCB *acb)
170 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
171 qemu_bh_schedule(acb->bh);
174 static void iscsi_co_generic_bh_cb(void *opaque)
176 struct IscsiTask *iTask = opaque;
179 aio_co_wake(iTask->co);
182 static void iscsi_retry_timer_expired(void *opaque)
184 struct IscsiTask *iTask = opaque;
187 aio_co_wake(iTask->co);
191 static inline unsigned exp_random(double mean)
193 return -mean * log((double)rand() / RAND_MAX);
196 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
197 * libiscsi 1.10.0, together with other constants we need. Use it as
198 * a hint that we have to define them ourselves if needed, to keep the
199 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
200 * the test because SCSI_STATUS_* is an enum.
202 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
203 * an enum, check against the LIBISCSI_API_VERSION macro, which was
204 * introduced in 1.11.0. If it is present, there is no need to define
207 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
208 !defined(LIBISCSI_API_VERSION)
209 #define SCSI_STATUS_TASK_SET_FULL 0x28
210 #define SCSI_STATUS_TIMEOUT 0x0f000002
211 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
212 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
215 #ifndef LIBISCSI_API_VERSION
216 #define LIBISCSI_API_VERSION 20130701
219 static int iscsi_translate_sense(struct scsi_sense *sense)
221 return - scsi_sense_to_errno(sense->key,
222 (sense->ascq & 0xFF00) >> 8,
226 /* Called (via iscsi_service) with QemuMutex held. */
228 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
229 void *command_data, void *opaque)
231 struct IscsiTask *iTask = opaque;
232 struct scsi_task *task = command_data;
234 iTask->status = status;
238 if (status != SCSI_STATUS_GOOD) {
239 if (iTask->retries++ < ISCSI_CMD_RETRIES) {
240 if (status == SCSI_STATUS_CHECK_CONDITION
241 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
242 error_report("iSCSI CheckCondition: %s",
243 iscsi_get_error(iscsi));
247 if (status == SCSI_STATUS_BUSY ||
248 status == SCSI_STATUS_TIMEOUT ||
249 status == SCSI_STATUS_TASK_SET_FULL) {
250 unsigned retry_time =
251 exp_random(iscsi_retry_times[iTask->retries - 1]);
252 if (status == SCSI_STATUS_TIMEOUT) {
253 /* make sure the request is rescheduled AFTER the
254 * reconnect is initiated */
255 retry_time = EVENT_INTERVAL * 2;
256 iTask->iscsilun->request_timed_out = true;
258 error_report("iSCSI Busy/TaskSetFull/TimeOut"
259 " (retry #%u in %u ms): %s",
260 iTask->retries, retry_time,
261 iscsi_get_error(iscsi));
262 aio_timer_init(iTask->iscsilun->aio_context,
263 &iTask->retry_timer, QEMU_CLOCK_REALTIME,
264 SCALE_MS, iscsi_retry_timer_expired, iTask);
265 timer_mod(&iTask->retry_timer,
266 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time);
271 iTask->err_code = iscsi_translate_sense(&task->sense);
272 iTask->err_str = g_strdup(iscsi_get_error(iscsi));
277 aio_bh_schedule_oneshot(iTask->iscsilun->aio_context,
278 iscsi_co_generic_bh_cb, iTask);
284 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
286 *iTask = (struct IscsiTask) {
287 .co = qemu_coroutine_self(),
288 .iscsilun = iscsilun,
293 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
296 IscsiAIOCB *acb = private_data;
298 acb->status = -ECANCELED;
299 iscsi_schedule_bh(acb);
303 iscsi_aio_cancel(BlockAIOCB *blockacb)
305 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
306 IscsiLun *iscsilun = acb->iscsilun;
308 if (acb->status != -EINPROGRESS) {
312 /* send a task mgmt call to the target to cancel the task on the target */
313 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
314 iscsi_abort_task_cb, acb);
318 static const AIOCBInfo iscsi_aiocb_info = {
319 .aiocb_size = sizeof(IscsiAIOCB),
320 .cancel_async = iscsi_aio_cancel,
324 static void iscsi_process_read(void *arg);
325 static void iscsi_process_write(void *arg);
327 /* Called with QemuMutex held. */
329 iscsi_set_events(IscsiLun *iscsilun)
331 struct iscsi_context *iscsi = iscsilun->iscsi;
332 int ev = iscsi_which_events(iscsi);
334 if (ev != iscsilun->events) {
335 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi),
337 (ev & POLLIN) ? iscsi_process_read : NULL,
338 (ev & POLLOUT) ? iscsi_process_write : NULL,
341 iscsilun->events = ev;
345 static void iscsi_timed_check_events(void *opaque)
347 IscsiLun *iscsilun = opaque;
349 /* check for timed out requests */
350 iscsi_service(iscsilun->iscsi, 0);
352 if (iscsilun->request_timed_out) {
353 iscsilun->request_timed_out = false;
354 iscsi_reconnect(iscsilun->iscsi);
357 /* newer versions of libiscsi may return zero events. Ensure we are able
358 * to return to service once this situation changes. */
359 iscsi_set_events(iscsilun);
361 timer_mod(iscsilun->event_timer,
362 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
366 iscsi_process_read(void *arg)
368 IscsiLun *iscsilun = arg;
369 struct iscsi_context *iscsi = iscsilun->iscsi;
371 qemu_mutex_lock(&iscsilun->mutex);
372 iscsi_service(iscsi, POLLIN);
373 iscsi_set_events(iscsilun);
374 qemu_mutex_unlock(&iscsilun->mutex);
378 iscsi_process_write(void *arg)
380 IscsiLun *iscsilun = arg;
381 struct iscsi_context *iscsi = iscsilun->iscsi;
383 qemu_mutex_lock(&iscsilun->mutex);
384 iscsi_service(iscsi, POLLOUT);
385 iscsi_set_events(iscsilun);
386 qemu_mutex_unlock(&iscsilun->mutex);
389 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
391 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
394 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
396 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
399 static bool is_byte_request_lun_aligned(int64_t offset, int count,
402 if (offset % iscsilun->block_size || count % iscsilun->block_size) {
403 error_report("iSCSI misaligned request: "
404 "iscsilun->block_size %u, offset %" PRIi64
406 iscsilun->block_size, offset, count);
412 static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors,
415 assert(nb_sectors <= BDRV_REQUEST_MAX_SECTORS);
416 return is_byte_request_lun_aligned(sector_num << BDRV_SECTOR_BITS,
417 nb_sectors << BDRV_SECTOR_BITS,
421 static void iscsi_allocmap_free(IscsiLun *iscsilun)
423 g_free(iscsilun->allocmap);
424 g_free(iscsilun->allocmap_valid);
425 iscsilun->allocmap = NULL;
426 iscsilun->allocmap_valid = NULL;
430 static int iscsi_allocmap_init(IscsiLun *iscsilun, int open_flags)
432 iscsi_allocmap_free(iscsilun);
434 assert(iscsilun->cluster_size);
435 iscsilun->allocmap_size =
436 DIV_ROUND_UP(iscsilun->num_blocks * iscsilun->block_size,
437 iscsilun->cluster_size);
439 iscsilun->allocmap = bitmap_try_new(iscsilun->allocmap_size);
440 if (!iscsilun->allocmap) {
444 if (open_flags & BDRV_O_NOCACHE) {
445 /* when cache.direct = on all allocmap entries are
446 * treated as invalid to force a relookup of the block
447 * status on every read request */
451 iscsilun->allocmap_valid = bitmap_try_new(iscsilun->allocmap_size);
452 if (!iscsilun->allocmap_valid) {
453 /* if we are under memory pressure free the allocmap as well */
454 iscsi_allocmap_free(iscsilun);
462 iscsi_allocmap_update(IscsiLun *iscsilun, int64_t offset,
463 int64_t bytes, bool allocated, bool valid)
465 int64_t cl_num_expanded, nb_cls_expanded, cl_num_shrunk, nb_cls_shrunk;
467 if (iscsilun->allocmap == NULL) {
470 /* expand to entirely contain all affected clusters */
471 assert(iscsilun->cluster_size);
472 cl_num_expanded = offset / iscsilun->cluster_size;
473 nb_cls_expanded = DIV_ROUND_UP(offset + bytes,
474 iscsilun->cluster_size) - cl_num_expanded;
475 /* shrink to touch only completely contained clusters */
476 cl_num_shrunk = DIV_ROUND_UP(offset, iscsilun->cluster_size);
477 nb_cls_shrunk = (offset + bytes) / iscsilun->cluster_size - cl_num_shrunk;
479 bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded);
481 if (nb_cls_shrunk > 0) {
482 bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
486 if (iscsilun->allocmap_valid == NULL) {
490 if (nb_cls_shrunk > 0) {
491 bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
494 bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded,
500 iscsi_allocmap_set_allocated(IscsiLun *iscsilun, int64_t offset,
503 iscsi_allocmap_update(iscsilun, offset, bytes, true, true);
507 iscsi_allocmap_set_unallocated(IscsiLun *iscsilun, int64_t offset,
510 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
511 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
513 iscsi_allocmap_update(iscsilun, offset, bytes, false, true);
516 static void iscsi_allocmap_set_invalid(IscsiLun *iscsilun, int64_t offset,
519 iscsi_allocmap_update(iscsilun, offset, bytes, false, false);
522 static void iscsi_allocmap_invalidate(IscsiLun *iscsilun)
524 if (iscsilun->allocmap) {
525 bitmap_zero(iscsilun->allocmap, iscsilun->allocmap_size);
527 if (iscsilun->allocmap_valid) {
528 bitmap_zero(iscsilun->allocmap_valid, iscsilun->allocmap_size);
533 iscsi_allocmap_is_allocated(IscsiLun *iscsilun, int64_t offset,
537 if (iscsilun->allocmap == NULL) {
540 assert(iscsilun->cluster_size);
541 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size);
542 return !(find_next_bit(iscsilun->allocmap, size,
543 offset / iscsilun->cluster_size) == size);
546 static inline bool iscsi_allocmap_is_valid(IscsiLun *iscsilun,
547 int64_t offset, int64_t bytes)
550 if (iscsilun->allocmap_valid == NULL) {
553 assert(iscsilun->cluster_size);
554 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size);
555 return (find_next_zero_bit(iscsilun->allocmap_valid, size,
556 offset / iscsilun->cluster_size) == size);
559 static int coroutine_fn
560 iscsi_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
561 QEMUIOVector *iov, int flags)
563 IscsiLun *iscsilun = bs->opaque;
564 struct IscsiTask iTask;
566 uint32_t num_sectors;
567 bool fua = flags & BDRV_REQ_FUA;
571 assert(iscsilun->dpofua);
573 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
577 if (bs->bl.max_transfer) {
578 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
581 lba = sector_qemu2lun(sector_num, iscsilun);
582 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
583 iscsi_co_init_iscsitask(iscsilun, &iTask);
584 qemu_mutex_lock(&iscsilun->mutex);
586 if (iscsilun->use_16_for_rw) {
587 #if LIBISCSI_API_VERSION >= (20160603)
588 iTask.task = iscsi_write16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
589 NULL, num_sectors * iscsilun->block_size,
590 iscsilun->block_size, 0, 0, fua, 0, 0,
591 iscsi_co_generic_cb, &iTask,
592 (struct scsi_iovec *)iov->iov, iov->niov);
594 iTask.task = iscsi_write10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
595 NULL, num_sectors * iscsilun->block_size,
596 iscsilun->block_size, 0, 0, fua, 0, 0,
597 iscsi_co_generic_cb, &iTask,
598 (struct scsi_iovec *)iov->iov, iov->niov);
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);
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);
612 if (iTask.task == NULL) {
613 qemu_mutex_unlock(&iscsilun->mutex);
616 #if LIBISCSI_API_VERSION < (20160603)
617 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
620 while (!iTask.complete) {
621 iscsi_set_events(iscsilun);
622 qemu_mutex_unlock(&iscsilun->mutex);
623 qemu_coroutine_yield();
624 qemu_mutex_lock(&iscsilun->mutex);
627 if (iTask.task != NULL) {
628 scsi_free_scsi_task(iTask.task);
632 if (iTask.do_retry) {
637 if (iTask.status != SCSI_STATUS_GOOD) {
638 iscsi_allocmap_set_invalid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
639 nb_sectors * BDRV_SECTOR_SIZE);
640 error_report("iSCSI WRITE10/16 failed at lba %" PRIu64 ": %s", lba,
646 iscsi_allocmap_set_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
647 nb_sectors * BDRV_SECTOR_SIZE);
650 qemu_mutex_unlock(&iscsilun->mutex);
651 g_free(iTask.err_str);
657 static int coroutine_fn iscsi_co_block_status(BlockDriverState *bs,
658 bool want_zero, int64_t offset,
659 int64_t bytes, int64_t *pnum,
661 BlockDriverState **file)
663 IscsiLun *iscsilun = bs->opaque;
664 struct scsi_get_lba_status *lbas = NULL;
665 struct scsi_lba_status_descriptor *lbasd = NULL;
666 struct IscsiTask iTask;
670 iscsi_co_init_iscsitask(iscsilun, &iTask);
672 assert(QEMU_IS_ALIGNED(offset | bytes, iscsilun->block_size));
674 /* default to all sectors allocated */
675 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
681 /* LUN does not support logical block provisioning */
682 if (!iscsilun->lbpme) {
686 lba = offset / iscsilun->block_size;
688 qemu_mutex_lock(&iscsilun->mutex);
690 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
691 lba, 8 + 16, iscsi_co_generic_cb,
697 while (!iTask.complete) {
698 iscsi_set_events(iscsilun);
699 qemu_mutex_unlock(&iscsilun->mutex);
700 qemu_coroutine_yield();
701 qemu_mutex_lock(&iscsilun->mutex);
704 if (iTask.do_retry) {
705 if (iTask.task != NULL) {
706 scsi_free_scsi_task(iTask.task);
713 if (iTask.status != SCSI_STATUS_GOOD) {
714 /* in case the get_lba_status_callout fails (i.e.
715 * because the device is busy or the cmd is not
716 * supported) we pretend all blocks are allocated
717 * for backwards compatibility */
718 error_report("iSCSI GET_LBA_STATUS failed at lba %" PRIu64 ": %s",
723 lbas = scsi_datain_unmarshall(iTask.task);
729 lbasd = &lbas->descriptors[0];
731 if (lba != lbasd->lba) {
736 *pnum = lbasd->num_blocks * iscsilun->block_size;
738 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
739 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
740 ret &= ~BDRV_BLOCK_DATA;
741 if (iscsilun->lbprz) {
742 ret |= BDRV_BLOCK_ZERO;
746 if (ret & BDRV_BLOCK_ZERO) {
747 iscsi_allocmap_set_unallocated(iscsilun, offset, *pnum);
749 iscsi_allocmap_set_allocated(iscsilun, offset, *pnum);
756 qemu_mutex_unlock(&iscsilun->mutex);
757 g_free(iTask.err_str);
759 if (iTask.task != NULL) {
760 scsi_free_scsi_task(iTask.task);
762 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID && file) {
768 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
769 int64_t sector_num, int nb_sectors,
772 IscsiLun *iscsilun = bs->opaque;
773 struct IscsiTask iTask;
775 uint32_t num_sectors;
778 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
782 if (bs->bl.max_transfer) {
783 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
786 /* if cache.direct is off and we have a valid entry in our allocation map
787 * we can skip checking the block status and directly return zeroes if
788 * the request falls within an unallocated area */
789 if (iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
790 nb_sectors * BDRV_SECTOR_SIZE) &&
791 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
792 nb_sectors * BDRV_SECTOR_SIZE)) {
793 qemu_iovec_memset(iov, 0, 0x00, iov->size);
797 if (nb_sectors >= ISCSI_CHECKALLOC_THRES &&
798 !iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
799 nb_sectors * BDRV_SECTOR_SIZE) &&
800 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
801 nb_sectors * BDRV_SECTOR_SIZE)) {
803 /* check the block status from the beginning of the cluster
804 * containing the start sector */
808 assert(iscsilun->cluster_size);
809 head = (sector_num * BDRV_SECTOR_SIZE) % iscsilun->cluster_size;
810 ret = iscsi_co_block_status(bs, true,
811 sector_num * BDRV_SECTOR_SIZE - head,
812 BDRV_REQUEST_MAX_BYTES, &pnum, NULL, NULL);
816 /* if the whole request falls into an unallocated area we can avoid
817 * reading and directly return zeroes instead */
818 if (ret & BDRV_BLOCK_ZERO &&
819 pnum >= nb_sectors * BDRV_SECTOR_SIZE + head) {
820 qemu_iovec_memset(iov, 0, 0x00, iov->size);
825 lba = sector_qemu2lun(sector_num, iscsilun);
826 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
828 iscsi_co_init_iscsitask(iscsilun, &iTask);
829 qemu_mutex_lock(&iscsilun->mutex);
831 if (iscsilun->use_16_for_rw) {
832 #if LIBISCSI_API_VERSION >= (20160603)
833 iTask.task = iscsi_read16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
834 num_sectors * iscsilun->block_size,
835 iscsilun->block_size, 0, 0, 0, 0, 0,
836 iscsi_co_generic_cb, &iTask,
837 (struct scsi_iovec *)iov->iov, iov->niov);
839 iTask.task = iscsi_read10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
840 num_sectors * iscsilun->block_size,
841 iscsilun->block_size,
843 iscsi_co_generic_cb, &iTask,
844 (struct scsi_iovec *)iov->iov, iov->niov);
847 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
848 num_sectors * iscsilun->block_size,
849 iscsilun->block_size, 0, 0, 0, 0, 0,
850 iscsi_co_generic_cb, &iTask);
852 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
853 num_sectors * iscsilun->block_size,
854 iscsilun->block_size,
856 iscsi_co_generic_cb, &iTask);
859 if (iTask.task == NULL) {
860 qemu_mutex_unlock(&iscsilun->mutex);
863 #if LIBISCSI_API_VERSION < (20160603)
864 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
866 while (!iTask.complete) {
867 iscsi_set_events(iscsilun);
868 qemu_mutex_unlock(&iscsilun->mutex);
869 qemu_coroutine_yield();
870 qemu_mutex_lock(&iscsilun->mutex);
873 if (iTask.task != NULL) {
874 scsi_free_scsi_task(iTask.task);
878 if (iTask.do_retry) {
883 if (iTask.status != SCSI_STATUS_GOOD) {
884 error_report("iSCSI READ10/16 failed at lba %" PRIu64 ": %s",
889 qemu_mutex_unlock(&iscsilun->mutex);
890 g_free(iTask.err_str);
894 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
896 IscsiLun *iscsilun = bs->opaque;
897 struct IscsiTask iTask;
900 iscsi_co_init_iscsitask(iscsilun, &iTask);
901 qemu_mutex_lock(&iscsilun->mutex);
903 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
904 0, iscsi_co_generic_cb, &iTask) == NULL) {
905 qemu_mutex_unlock(&iscsilun->mutex);
909 while (!iTask.complete) {
910 iscsi_set_events(iscsilun);
911 qemu_mutex_unlock(&iscsilun->mutex);
912 qemu_coroutine_yield();
913 qemu_mutex_lock(&iscsilun->mutex);
916 if (iTask.task != NULL) {
917 scsi_free_scsi_task(iTask.task);
921 if (iTask.do_retry) {
926 if (iTask.status != SCSI_STATUS_GOOD) {
927 error_report("iSCSI SYNCHRONIZECACHE10 failed: %s", iTask.err_str);
931 qemu_mutex_unlock(&iscsilun->mutex);
932 g_free(iTask.err_str);
937 /* Called (via iscsi_service) with QemuMutex held. */
939 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
940 void *command_data, void *opaque)
942 IscsiAIOCB *acb = opaque;
949 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
950 iscsi_get_error(iscsi));
951 acb->status = iscsi_translate_sense(&acb->task->sense);
954 acb->ioh->driver_status = 0;
955 acb->ioh->host_status = 0;
957 acb->ioh->status = status;
959 #define SG_ERR_DRIVER_SENSE 0x08
961 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
964 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
966 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
967 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
968 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
969 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
972 iscsi_schedule_bh(acb);
975 static void iscsi_ioctl_bh_completion(void *opaque)
977 IscsiAIOCB *acb = opaque;
979 qemu_bh_delete(acb->bh);
980 acb->common.cb(acb->common.opaque, acb->ret);
984 static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf)
986 BlockDriverState *bs = acb->common.bs;
987 IscsiLun *iscsilun = bs->opaque;
991 case SG_GET_VERSION_NUM:
995 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
1001 acb->bh = aio_bh_new(bdrv_get_aio_context(bs),
1002 iscsi_ioctl_bh_completion, acb);
1004 qemu_bh_schedule(acb->bh);
1007 static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
1008 unsigned long int req, void *buf,
1009 BlockCompletionFunc *cb, void *opaque)
1011 IscsiLun *iscsilun = bs->opaque;
1012 struct iscsi_context *iscsi = iscsilun->iscsi;
1013 struct iscsi_data data;
1016 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
1018 acb->iscsilun = iscsilun;
1020 acb->status = -EINPROGRESS;
1025 iscsi_ioctl_handle_emulated(acb, req, buf);
1026 return &acb->common;
1029 if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) {
1030 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
1031 acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE);
1032 qemu_aio_unref(acb);
1036 acb->task = malloc(sizeof(struct scsi_task));
1037 if (acb->task == NULL) {
1038 error_report("iSCSI: Failed to allocate task for scsi command. %s",
1039 iscsi_get_error(iscsi));
1040 qemu_aio_unref(acb);
1043 memset(acb->task, 0, sizeof(struct scsi_task));
1045 switch (acb->ioh->dxfer_direction) {
1046 case SG_DXFER_TO_DEV:
1047 acb->task->xfer_dir = SCSI_XFER_WRITE;
1049 case SG_DXFER_FROM_DEV:
1050 acb->task->xfer_dir = SCSI_XFER_READ;
1053 acb->task->xfer_dir = SCSI_XFER_NONE;
1057 acb->task->cdb_size = acb->ioh->cmd_len;
1058 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
1059 acb->task->expxferlen = acb->ioh->dxfer_len;
1062 qemu_mutex_lock(&iscsilun->mutex);
1063 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
1064 if (acb->ioh->iovec_count == 0) {
1065 data.data = acb->ioh->dxferp;
1066 data.size = acb->ioh->dxfer_len;
1068 scsi_task_set_iov_out(acb->task,
1069 (struct scsi_iovec *) acb->ioh->dxferp,
1070 acb->ioh->iovec_count);
1074 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
1076 (data.size > 0) ? &data : NULL,
1078 qemu_mutex_unlock(&iscsilun->mutex);
1079 scsi_free_scsi_task(acb->task);
1080 qemu_aio_unref(acb);
1084 /* tell libiscsi to read straight into the buffer we got from ioctl */
1085 if (acb->task->xfer_dir == SCSI_XFER_READ) {
1086 if (acb->ioh->iovec_count == 0) {
1087 scsi_task_add_data_in_buffer(acb->task,
1088 acb->ioh->dxfer_len,
1091 scsi_task_set_iov_in(acb->task,
1092 (struct scsi_iovec *) acb->ioh->dxferp,
1093 acb->ioh->iovec_count);
1097 iscsi_set_events(iscsilun);
1098 qemu_mutex_unlock(&iscsilun->mutex);
1100 return &acb->common;
1106 iscsi_getlength(BlockDriverState *bs)
1108 IscsiLun *iscsilun = bs->opaque;
1111 len = iscsilun->num_blocks;
1112 len *= iscsilun->block_size;
1118 coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
1120 IscsiLun *iscsilun = bs->opaque;
1121 struct IscsiTask iTask;
1122 struct unmap_list list;
1125 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) {
1129 if (!iscsilun->lbp.lbpu) {
1130 /* UNMAP is not supported by the target */
1134 list.lba = offset / iscsilun->block_size;
1135 list.num = bytes / iscsilun->block_size;
1137 iscsi_co_init_iscsitask(iscsilun, &iTask);
1138 qemu_mutex_lock(&iscsilun->mutex);
1140 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
1141 iscsi_co_generic_cb, &iTask) == NULL) {
1146 while (!iTask.complete) {
1147 iscsi_set_events(iscsilun);
1148 qemu_mutex_unlock(&iscsilun->mutex);
1149 qemu_coroutine_yield();
1150 qemu_mutex_lock(&iscsilun->mutex);
1153 if (iTask.task != NULL) {
1154 scsi_free_scsi_task(iTask.task);
1158 if (iTask.do_retry) {
1163 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
1165 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
1166 /* the target might fail with a check condition if it
1167 is not happy with the alignment of the UNMAP request
1168 we silently fail in this case */
1172 if (iTask.status != SCSI_STATUS_GOOD) {
1173 error_report("iSCSI UNMAP failed at lba %" PRIu64 ": %s",
1174 list.lba, iTask.err_str);
1180 qemu_mutex_unlock(&iscsilun->mutex);
1181 g_free(iTask.err_str);
1186 coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1187 int bytes, BdrvRequestFlags flags)
1189 IscsiLun *iscsilun = bs->opaque;
1190 struct IscsiTask iTask;
1193 bool use_16_for_ws = iscsilun->use_16_for_rw;
1196 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) {
1200 if (flags & BDRV_REQ_MAY_UNMAP) {
1201 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
1202 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1203 use_16_for_ws = true;
1205 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
1206 /* WRITESAME16 with UNMAP is not supported by the target,
1207 * fall back and try WRITESAME10/16 without UNMAP */
1208 flags &= ~BDRV_REQ_MAY_UNMAP;
1209 use_16_for_ws = iscsilun->use_16_for_rw;
1213 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
1214 /* WRITESAME without UNMAP is not supported by the target */
1218 lba = offset / iscsilun->block_size;
1219 nb_blocks = bytes / iscsilun->block_size;
1221 if (iscsilun->zeroblock == NULL) {
1222 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size);
1223 if (iscsilun->zeroblock == NULL) {
1228 qemu_mutex_lock(&iscsilun->mutex);
1229 iscsi_co_init_iscsitask(iscsilun, &iTask);
1231 if (use_16_for_ws) {
1232 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1233 iscsilun->zeroblock, iscsilun->block_size,
1234 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1235 0, 0, iscsi_co_generic_cb, &iTask);
1237 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
1238 iscsilun->zeroblock, iscsilun->block_size,
1239 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1240 0, 0, iscsi_co_generic_cb, &iTask);
1242 if (iTask.task == NULL) {
1243 qemu_mutex_unlock(&iscsilun->mutex);
1247 while (!iTask.complete) {
1248 iscsi_set_events(iscsilun);
1249 qemu_mutex_unlock(&iscsilun->mutex);
1250 qemu_coroutine_yield();
1251 qemu_mutex_lock(&iscsilun->mutex);
1254 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1255 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
1256 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
1257 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
1258 /* WRITE SAME is not supported by the target */
1259 iscsilun->has_write_same = false;
1260 scsi_free_scsi_task(iTask.task);
1265 if (iTask.task != NULL) {
1266 scsi_free_scsi_task(iTask.task);
1270 if (iTask.do_retry) {
1275 if (iTask.status != SCSI_STATUS_GOOD) {
1276 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
1277 error_report("iSCSI WRITESAME10/16 failed at lba %" PRIu64 ": %s",
1278 lba, iTask.err_str);
1283 if (flags & BDRV_REQ_MAY_UNMAP) {
1284 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
1286 iscsi_allocmap_set_allocated(iscsilun, offset, bytes);
1290 qemu_mutex_unlock(&iscsilun->mutex);
1291 g_free(iTask.err_str);
1295 static void apply_chap(struct iscsi_context *iscsi, QemuOpts *opts,
1298 const char *user = NULL;
1299 const char *password = NULL;
1300 const char *secretid;
1301 char *secret = NULL;
1303 user = qemu_opt_get(opts, "user");
1308 secretid = qemu_opt_get(opts, "password-secret");
1309 password = qemu_opt_get(opts, "password");
1310 if (secretid && password) {
1311 error_setg(errp, "'password' and 'password-secret' properties are "
1312 "mutually exclusive");
1316 secret = qcrypto_secret_lookup_as_utf8(secretid, errp);
1321 } else if (!password) {
1322 error_setg(errp, "CHAP username specified but no password was given");
1326 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
1327 error_setg(errp, "Failed to set initiator username and password");
1333 static void apply_header_digest(struct iscsi_context *iscsi, QemuOpts *opts,
1336 const char *digest = NULL;
1338 digest = qemu_opt_get(opts, "header-digest");
1340 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1341 } else if (!strcmp(digest, "crc32c")) {
1342 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1343 } else if (!strcmp(digest, "none")) {
1344 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1345 } else if (!strcmp(digest, "crc32c-none")) {
1346 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1347 } else if (!strcmp(digest, "none-crc32c")) {
1348 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1350 error_setg(errp, "Invalid header-digest setting : %s", digest);
1354 static char *get_initiator_name(QemuOpts *opts)
1358 UuidInfo *uuid_info;
1360 name = qemu_opt_get(opts, "initiator-name");
1362 return g_strdup(name);
1365 uuid_info = qmp_query_uuid(NULL);
1366 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1367 name = qemu_get_vm_name();
1369 name = uuid_info->UUID;
1371 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1372 name ? ":" : "", name ? name : "");
1373 qapi_free_UuidInfo(uuid_info);
1377 static void iscsi_nop_timed_event(void *opaque)
1379 IscsiLun *iscsilun = opaque;
1381 qemu_mutex_lock(&iscsilun->mutex);
1382 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) {
1383 error_report("iSCSI: NOP timeout. Reconnecting...");
1384 iscsilun->request_timed_out = true;
1385 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1386 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1390 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1391 iscsi_set_events(iscsilun);
1394 qemu_mutex_unlock(&iscsilun->mutex);
1397 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
1399 struct scsi_task *task = NULL;
1400 struct scsi_readcapacity10 *rc10 = NULL;
1401 struct scsi_readcapacity16 *rc16 = NULL;
1402 int retries = ISCSI_CMD_RETRIES;
1406 scsi_free_scsi_task(task);
1410 switch (iscsilun->type) {
1412 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1413 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1414 rc16 = scsi_datain_unmarshall(task);
1416 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1418 iscsilun->block_size = rc16->block_length;
1419 iscsilun->num_blocks = rc16->returned_lba + 1;
1420 iscsilun->lbpme = !!rc16->lbpme;
1421 iscsilun->lbprz = !!rc16->lbprz;
1422 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
1426 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1427 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
1430 /* Fall through and try READ CAPACITY(10) instead. */
1432 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1433 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1434 rc10 = scsi_datain_unmarshall(task);
1436 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1438 iscsilun->block_size = rc10->block_size;
1439 if (rc10->lba == 0) {
1440 /* blank disk loaded */
1441 iscsilun->num_blocks = 0;
1443 iscsilun->num_blocks = rc10->lba + 1;
1451 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1452 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1455 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1456 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command");
1457 } else if (!iscsilun->block_size ||
1458 iscsilun->block_size % BDRV_SECTOR_SIZE) {
1459 error_setg(errp, "iSCSI: the target returned an invalid "
1460 "block size of %d.", iscsilun->block_size);
1463 scsi_free_scsi_task(task);
1467 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
1468 int evpd, int pc, void **inq, Error **errp)
1471 struct scsi_task *task = NULL;
1472 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1473 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1476 full_size = scsi_datain_getfullsize(task);
1477 if (full_size > task->datain.size) {
1478 scsi_free_scsi_task(task);
1480 /* we need more data for the full list */
1481 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1482 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1487 *inq = scsi_datain_unmarshall(task);
1489 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1496 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1497 iscsi_get_error(iscsi));
1500 scsi_free_scsi_task(task);
1505 static void iscsi_detach_aio_context(BlockDriverState *bs)
1507 IscsiLun *iscsilun = bs->opaque;
1509 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi),
1510 false, NULL, NULL, NULL, NULL);
1511 iscsilun->events = 0;
1513 if (iscsilun->nop_timer) {
1514 timer_del(iscsilun->nop_timer);
1515 timer_free(iscsilun->nop_timer);
1516 iscsilun->nop_timer = NULL;
1518 if (iscsilun->event_timer) {
1519 timer_del(iscsilun->event_timer);
1520 timer_free(iscsilun->event_timer);
1521 iscsilun->event_timer = NULL;
1525 static void iscsi_attach_aio_context(BlockDriverState *bs,
1526 AioContext *new_context)
1528 IscsiLun *iscsilun = bs->opaque;
1530 iscsilun->aio_context = new_context;
1531 iscsi_set_events(iscsilun);
1533 /* Set up a timer for sending out iSCSI NOPs */
1534 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1535 QEMU_CLOCK_REALTIME, SCALE_MS,
1536 iscsi_nop_timed_event, iscsilun);
1537 timer_mod(iscsilun->nop_timer,
1538 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1540 /* Set up a timer for periodic calls to iscsi_set_events and to
1541 * scan for command timeout */
1542 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context,
1543 QEMU_CLOCK_REALTIME, SCALE_MS,
1544 iscsi_timed_check_events, iscsilun);
1545 timer_mod(iscsilun->event_timer,
1546 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
1549 static void iscsi_modesense_sync(IscsiLun *iscsilun)
1551 struct scsi_task *task;
1552 struct scsi_mode_sense *ms = NULL;
1553 iscsilun->write_protected = false;
1554 iscsilun->dpofua = false;
1556 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1557 1, SCSI_MODESENSE_PC_CURRENT,
1560 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1561 iscsi_get_error(iscsilun->iscsi));
1565 if (task->status != SCSI_STATUS_GOOD) {
1566 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1569 ms = scsi_datain_unmarshall(task);
1571 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1572 iscsi_get_error(iscsilun->iscsi));
1575 iscsilun->write_protected = ms->device_specific_parameter & 0x80;
1576 iscsilun->dpofua = ms->device_specific_parameter & 0x10;
1580 scsi_free_scsi_task(task);
1584 static void iscsi_parse_iscsi_option(const char *target, QDict *options)
1588 const char *user, *password, *password_secret, *initiator_name,
1589 *header_digest, *timeout;
1591 list = qemu_find_opts("iscsi");
1596 opts = qemu_opts_find(list, target);
1598 opts = QTAILQ_FIRST(&list->head);
1604 user = qemu_opt_get(opts, "user");
1606 qdict_set_default_str(options, "user", user);
1609 password = qemu_opt_get(opts, "password");
1611 qdict_set_default_str(options, "password", password);
1614 password_secret = qemu_opt_get(opts, "password-secret");
1615 if (password_secret) {
1616 qdict_set_default_str(options, "password-secret", password_secret);
1619 initiator_name = qemu_opt_get(opts, "initiator-name");
1620 if (initiator_name) {
1621 qdict_set_default_str(options, "initiator-name", initiator_name);
1624 header_digest = qemu_opt_get(opts, "header-digest");
1625 if (header_digest) {
1626 /* -iscsi takes upper case values, but QAPI only supports lower case
1627 * enum constant names, so we have to convert here. */
1628 char *qapi_value = g_ascii_strdown(header_digest, -1);
1629 qdict_set_default_str(options, "header-digest", qapi_value);
1633 timeout = qemu_opt_get(opts, "timeout");
1635 qdict_set_default_str(options, "timeout", timeout);
1640 * We support iscsi url's on the form
1641 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1643 static void iscsi_parse_filename(const char *filename, QDict *options,
1646 struct iscsi_url *iscsi_url;
1647 const char *transport_name;
1650 iscsi_url = iscsi_parse_full_url(NULL, filename);
1651 if (iscsi_url == NULL) {
1652 error_setg(errp, "Failed to parse URL : %s", filename);
1656 #if LIBISCSI_API_VERSION >= (20160603)
1657 switch (iscsi_url->transport) {
1659 transport_name = "tcp";
1661 case ISER_TRANSPORT:
1662 transport_name = "iser";
1665 error_setg(errp, "Unknown transport type (%d)",
1666 iscsi_url->transport);
1670 transport_name = "tcp";
1673 qdict_set_default_str(options, "transport", transport_name);
1674 qdict_set_default_str(options, "portal", iscsi_url->portal);
1675 qdict_set_default_str(options, "target", iscsi_url->target);
1677 lun_str = g_strdup_printf("%d", iscsi_url->lun);
1678 qdict_set_default_str(options, "lun", lun_str);
1681 /* User/password from -iscsi take precedence over those from the URL */
1682 iscsi_parse_iscsi_option(iscsi_url->target, options);
1684 if (iscsi_url->user[0] != '\0') {
1685 qdict_set_default_str(options, "user", iscsi_url->user);
1686 qdict_set_default_str(options, "password", iscsi_url->passwd);
1689 iscsi_destroy_url(iscsi_url);
1692 static QemuOptsList runtime_opts = {
1694 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1697 .name = "transport",
1698 .type = QEMU_OPT_STRING,
1702 .type = QEMU_OPT_STRING,
1706 .type = QEMU_OPT_STRING,
1710 .type = QEMU_OPT_STRING,
1714 .type = QEMU_OPT_STRING,
1717 .name = "password-secret",
1718 .type = QEMU_OPT_STRING,
1722 .type = QEMU_OPT_NUMBER,
1725 .name = "initiator-name",
1726 .type = QEMU_OPT_STRING,
1729 .name = "header-digest",
1730 .type = QEMU_OPT_STRING,
1734 .type = QEMU_OPT_NUMBER,
1738 .type = QEMU_OPT_STRING,
1740 { /* end of list */ }
1744 static void iscsi_save_designator(IscsiLun *lun,
1745 struct scsi_inquiry_device_identification *inq_di)
1747 struct scsi_inquiry_device_designator *desig, *copy = NULL;
1749 for (desig = inq_di->designators; desig; desig = desig->next) {
1750 if (desig->association ||
1751 desig->designator_type > SCSI_DESIGNATOR_TYPE_NAA) {
1754 /* NAA works better than T10 vendor ID based designator. */
1755 if (!copy || copy->designator_type < desig->designator_type) {
1760 lun->dd = g_new(struct scsi_inquiry_device_designator, 1);
1762 lun->dd->next = NULL;
1763 lun->dd->designator = g_malloc(copy->designator_length);
1764 memcpy(lun->dd->designator, copy->designator, copy->designator_length);
1768 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1771 IscsiLun *iscsilun = bs->opaque;
1772 struct iscsi_context *iscsi = NULL;
1773 struct scsi_task *task = NULL;
1774 struct scsi_inquiry_standard *inq = NULL;
1775 struct scsi_inquiry_supported_pages *inq_vpd;
1776 char *initiator_name = NULL;
1778 Error *local_err = NULL;
1779 const char *transport_name, *portal, *target, *filename;
1780 #if LIBISCSI_API_VERSION >= (20160603)
1781 enum iscsi_transport_type transport;
1783 int i, ret = 0, timeout = 0, lun;
1785 /* If we are given a filename, parse the filename, with precedence given to
1786 * filename encoded options */
1787 filename = qdict_get_try_str(options, "filename");
1789 warn_report("'filename' option specified. "
1790 "This is an unsupported option, and may be deprecated "
1792 iscsi_parse_filename(filename, options, &local_err);
1795 error_propagate(errp, local_err);
1800 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1801 qemu_opts_absorb_qdict(opts, options, &local_err);
1803 error_propagate(errp, local_err);
1808 transport_name = qemu_opt_get(opts, "transport");
1809 portal = qemu_opt_get(opts, "portal");
1810 target = qemu_opt_get(opts, "target");
1811 lun = qemu_opt_get_number(opts, "lun", 0);
1813 if (!transport_name || !portal || !target) {
1814 error_setg(errp, "Need all of transport, portal and target options");
1819 if (!strcmp(transport_name, "tcp")) {
1820 #if LIBISCSI_API_VERSION >= (20160603)
1821 transport = TCP_TRANSPORT;
1822 } else if (!strcmp(transport_name, "iser")) {
1823 transport = ISER_TRANSPORT;
1825 /* TCP is what older libiscsi versions always use */
1828 error_setg(errp, "Unknown transport: %s", transport_name);
1833 memset(iscsilun, 0, sizeof(IscsiLun));
1835 initiator_name = get_initiator_name(opts);
1837 iscsi = iscsi_create_context(initiator_name);
1838 if (iscsi == NULL) {
1839 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
1843 #if LIBISCSI_API_VERSION >= (20160603)
1844 if (iscsi_init_transport(iscsi, transport)) {
1845 error_setg(errp, ("Error initializing transport."));
1850 if (iscsi_set_targetname(iscsi, target)) {
1851 error_setg(errp, "iSCSI: Failed to set target name.");
1856 /* check if we got CHAP username/password via the options */
1857 apply_chap(iscsi, opts, &local_err);
1858 if (local_err != NULL) {
1859 error_propagate(errp, local_err);
1864 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1865 error_setg(errp, "iSCSI: Failed to set session type to normal.");
1870 /* check if we got HEADER_DIGEST via the options */
1871 apply_header_digest(iscsi, opts, &local_err);
1872 if (local_err != NULL) {
1873 error_propagate(errp, local_err);
1878 /* timeout handling is broken in libiscsi before 1.15.0 */
1879 timeout = qemu_opt_get_number(opts, "timeout", 0);
1880 #if LIBISCSI_API_VERSION >= 20150621
1881 iscsi_set_timeout(iscsi, timeout);
1884 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1888 if (iscsi_full_connect_sync(iscsi, portal, lun) != 0) {
1889 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
1890 iscsi_get_error(iscsi));
1895 iscsilun->iscsi = iscsi;
1896 iscsilun->aio_context = bdrv_get_aio_context(bs);
1897 iscsilun->lun = lun;
1898 iscsilun->has_write_same = true;
1900 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1901 (void **) &inq, errp);
1906 iscsilun->type = inq->periperal_device_type;
1907 scsi_free_scsi_task(task);
1910 iscsi_modesense_sync(iscsilun);
1911 if (iscsilun->dpofua) {
1912 bs->supported_write_flags = BDRV_REQ_FUA;
1915 /* Check the write protect flag of the LUN if we want to write */
1916 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) &&
1917 iscsilun->write_protected) {
1918 error_setg(errp, "Cannot open a write protected LUN as read-write");
1923 iscsi_readcapacity_sync(iscsilun, &local_err);
1924 if (local_err != NULL) {
1925 error_propagate(errp, local_err);
1929 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1931 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1932 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1933 * will try to read from the device to guess the image format.
1935 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
1939 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1940 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1941 (void **) &inq_vpd, errp);
1946 for (i = 0; i < inq_vpd->num_pages; i++) {
1947 struct scsi_task *inq_task;
1948 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1949 struct scsi_inquiry_block_limits *inq_bl;
1950 struct scsi_inquiry_device_identification *inq_di;
1951 switch (inq_vpd->pages[i]) {
1952 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1953 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1954 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1955 (void **) &inq_lbp, errp);
1956 if (inq_task == NULL) {
1960 memcpy(&iscsilun->lbp, inq_lbp,
1961 sizeof(struct scsi_inquiry_logical_block_provisioning));
1962 scsi_free_scsi_task(inq_task);
1964 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1965 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1966 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1967 (void **) &inq_bl, errp);
1968 if (inq_task == NULL) {
1972 memcpy(&iscsilun->bl, inq_bl,
1973 sizeof(struct scsi_inquiry_block_limits));
1974 scsi_free_scsi_task(inq_task);
1976 case SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION:
1977 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1978 SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION,
1979 (void **) &inq_di, errp);
1980 if (inq_task == NULL) {
1984 iscsi_save_designator(iscsilun, inq_di);
1985 scsi_free_scsi_task(inq_task);
1991 scsi_free_scsi_task(task);
1994 qemu_mutex_init(&iscsilun->mutex);
1995 iscsi_attach_aio_context(bs, iscsilun->aio_context);
1997 /* Guess the internal cluster (page) size of the iscsi target by the means
1998 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1999 * reasonable size */
2000 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
2001 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
2002 iscsilun->cluster_size = iscsilun->bl.opt_unmap_gran *
2003 iscsilun->block_size;
2004 if (iscsilun->lbprz) {
2005 ret = iscsi_allocmap_init(iscsilun, bs->open_flags);
2009 if (iscsilun->lbprz && iscsilun->lbp.lbpws) {
2010 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
2014 qemu_opts_del(opts);
2015 g_free(initiator_name);
2017 scsi_free_scsi_task(task);
2021 if (iscsi != NULL) {
2022 if (iscsi_is_logged_in(iscsi)) {
2023 iscsi_logout_sync(iscsi);
2025 iscsi_destroy_context(iscsi);
2027 memset(iscsilun, 0, sizeof(IscsiLun));
2033 static void iscsi_close(BlockDriverState *bs)
2035 IscsiLun *iscsilun = bs->opaque;
2036 struct iscsi_context *iscsi = iscsilun->iscsi;
2038 iscsi_detach_aio_context(bs);
2039 if (iscsi_is_logged_in(iscsi)) {
2040 iscsi_logout_sync(iscsi);
2042 iscsi_destroy_context(iscsi);
2044 g_free(iscsilun->dd->designator);
2045 g_free(iscsilun->dd);
2047 g_free(iscsilun->zeroblock);
2048 iscsi_allocmap_free(iscsilun);
2049 qemu_mutex_destroy(&iscsilun->mutex);
2050 memset(iscsilun, 0, sizeof(IscsiLun));
2053 static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)
2055 /* We don't actually refresh here, but just return data queried in
2056 * iscsi_open(): iscsi targets don't change their limits. */
2058 IscsiLun *iscsilun = bs->opaque;
2059 uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff;
2060 unsigned int block_size = MAX(BDRV_SECTOR_SIZE, iscsilun->block_size);
2062 assert(iscsilun->block_size >= BDRV_SECTOR_SIZE || bs->sg);
2064 bs->bl.request_alignment = block_size;
2066 if (iscsilun->bl.max_xfer_len) {
2067 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len);
2070 if (max_xfer_len * block_size < INT_MAX) {
2071 bs->bl.max_transfer = max_xfer_len * iscsilun->block_size;
2074 if (iscsilun->lbp.lbpu) {
2075 if (iscsilun->bl.max_unmap < 0xffffffff / block_size) {
2076 bs->bl.max_pdiscard =
2077 iscsilun->bl.max_unmap * iscsilun->block_size;
2079 bs->bl.pdiscard_alignment =
2080 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
2082 bs->bl.pdiscard_alignment = iscsilun->block_size;
2085 if (iscsilun->bl.max_ws_len < 0xffffffff / block_size) {
2086 bs->bl.max_pwrite_zeroes =
2087 iscsilun->bl.max_ws_len * iscsilun->block_size;
2089 if (iscsilun->lbp.lbpws) {
2090 bs->bl.pwrite_zeroes_alignment =
2091 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
2093 bs->bl.pwrite_zeroes_alignment = iscsilun->block_size;
2095 if (iscsilun->bl.opt_xfer_len &&
2096 iscsilun->bl.opt_xfer_len < INT_MAX / block_size) {
2097 bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len *
2098 iscsilun->block_size);
2102 /* Note that this will not re-establish a connection with an iSCSI target - it
2103 * is effectively a NOP. */
2104 static int iscsi_reopen_prepare(BDRVReopenState *state,
2105 BlockReopenQueue *queue, Error **errp)
2107 IscsiLun *iscsilun = state->bs->opaque;
2109 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) {
2110 error_setg(errp, "Cannot open a write protected LUN as read-write");
2116 static void iscsi_reopen_commit(BDRVReopenState *reopen_state)
2118 IscsiLun *iscsilun = reopen_state->bs->opaque;
2120 /* the cache.direct status might have changed */
2121 if (iscsilun->allocmap != NULL) {
2122 iscsi_allocmap_init(iscsilun, reopen_state->flags);
2126 static int iscsi_truncate(BlockDriverState *bs, int64_t offset,
2127 PreallocMode prealloc, Error **errp)
2129 IscsiLun *iscsilun = bs->opaque;
2130 Error *local_err = NULL;
2132 if (prealloc != PREALLOC_MODE_OFF) {
2133 error_setg(errp, "Unsupported preallocation mode '%s'",
2134 PreallocMode_str(prealloc));
2138 if (iscsilun->type != TYPE_DISK) {
2139 error_setg(errp, "Cannot resize non-disk iSCSI devices");
2143 iscsi_readcapacity_sync(iscsilun, &local_err);
2144 if (local_err != NULL) {
2145 error_propagate(errp, local_err);
2149 if (offset > iscsi_getlength(bs)) {
2150 error_setg(errp, "Cannot grow iSCSI devices");
2154 if (iscsilun->allocmap != NULL) {
2155 iscsi_allocmap_init(iscsilun, bs->open_flags);
2161 static int coroutine_fn iscsi_co_create_opts(const char *filename, QemuOpts *opts,
2165 int64_t total_size = 0;
2166 BlockDriverState *bs;
2167 IscsiLun *iscsilun = NULL;
2169 Error *local_err = NULL;
2173 /* Read out options */
2174 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2176 bs->opaque = g_new0(struct IscsiLun, 1);
2177 iscsilun = bs->opaque;
2179 bs_options = qdict_new();
2180 iscsi_parse_filename(filename, bs_options, &local_err);
2182 error_propagate(errp, local_err);
2185 ret = iscsi_open(bs, bs_options, 0, NULL);
2187 qobject_unref(bs_options);
2192 iscsi_detach_aio_context(bs);
2193 if (iscsilun->type != TYPE_DISK) {
2197 if (bs->total_sectors < total_size) {
2204 if (iscsilun->iscsi != NULL) {
2205 iscsi_destroy_context(iscsilun->iscsi);
2213 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2215 IscsiLun *iscsilun = bs->opaque;
2216 bdi->unallocated_blocks_are_zero = iscsilun->lbprz;
2217 bdi->cluster_size = iscsilun->cluster_size;
2221 static void coroutine_fn iscsi_co_invalidate_cache(BlockDriverState *bs,
2224 IscsiLun *iscsilun = bs->opaque;
2225 iscsi_allocmap_invalidate(iscsilun);
2228 static QemuOptsList iscsi_create_opts = {
2229 .name = "iscsi-create-opts",
2230 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
2233 .name = BLOCK_OPT_SIZE,
2234 .type = QEMU_OPT_SIZE,
2235 .help = "Virtual disk size"
2237 { /* end of list */ }
2241 static BlockDriver bdrv_iscsi = {
2242 .format_name = "iscsi",
2243 .protocol_name = "iscsi",
2245 .instance_size = sizeof(IscsiLun),
2246 .bdrv_parse_filename = iscsi_parse_filename,
2247 .bdrv_file_open = iscsi_open,
2248 .bdrv_close = iscsi_close,
2249 .bdrv_co_create_opts = iscsi_co_create_opts,
2250 .create_opts = &iscsi_create_opts,
2251 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2252 .bdrv_reopen_commit = iscsi_reopen_commit,
2253 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache,
2255 .bdrv_getlength = iscsi_getlength,
2256 .bdrv_get_info = iscsi_get_info,
2257 .bdrv_truncate = iscsi_truncate,
2258 .bdrv_refresh_limits = iscsi_refresh_limits,
2260 .bdrv_co_block_status = iscsi_co_block_status,
2261 .bdrv_co_pdiscard = iscsi_co_pdiscard,
2262 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2263 .bdrv_co_readv = iscsi_co_readv,
2264 .bdrv_co_writev = iscsi_co_writev,
2265 .bdrv_co_flush_to_disk = iscsi_co_flush,
2268 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2271 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2272 .bdrv_attach_aio_context = iscsi_attach_aio_context,
2275 #if LIBISCSI_API_VERSION >= (20160603)
2276 static BlockDriver bdrv_iser = {
2277 .format_name = "iser",
2278 .protocol_name = "iser",
2280 .instance_size = sizeof(IscsiLun),
2281 .bdrv_parse_filename = iscsi_parse_filename,
2282 .bdrv_file_open = iscsi_open,
2283 .bdrv_close = iscsi_close,
2284 .bdrv_co_create_opts = iscsi_co_create_opts,
2285 .create_opts = &iscsi_create_opts,
2286 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2287 .bdrv_reopen_commit = iscsi_reopen_commit,
2288 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache,
2290 .bdrv_getlength = iscsi_getlength,
2291 .bdrv_get_info = iscsi_get_info,
2292 .bdrv_truncate = iscsi_truncate,
2293 .bdrv_refresh_limits = iscsi_refresh_limits,
2295 .bdrv_co_block_status = iscsi_co_block_status,
2296 .bdrv_co_pdiscard = iscsi_co_pdiscard,
2297 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2298 .bdrv_co_readv = iscsi_co_readv,
2299 .bdrv_co_writev = iscsi_co_writev,
2300 .bdrv_co_flush_to_disk = iscsi_co_flush,
2303 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2306 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2307 .bdrv_attach_aio_context = iscsi_attach_aio_context,
2311 static void iscsi_block_init(void)
2313 bdrv_register(&bdrv_iscsi);
2314 #if LIBISCSI_API_VERSION >= (20160603)
2315 bdrv_register(&bdrv_iser);
2319 block_init(iscsi_block_init);