2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2016 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-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"
39 #include "sysemu/sysemu.h"
40 #include "qmp-commands.h"
41 #include "qapi/qmp/qstring.h"
42 #include "crypto/secret.h"
44 #include <iscsi/iscsi.h>
45 #include <iscsi/scsi-lowlevel.h>
51 typedef struct IscsiLun
{
52 struct iscsi_context
*iscsi
;
53 AioContext
*aio_context
;
55 enum scsi_inquiry_peripheral_device_type type
;
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
;
88 bool request_timed_out
;
91 typedef struct IscsiTask
{
96 struct scsi_task
*task
;
100 QEMUTimer retry_timer
;
104 typedef struct IscsiAIOCB
{
109 struct scsi_task
*task
;
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};
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
134 #define ISCSI_CHECKALLOC_THRES 64
141 qemu_bh_delete(acb
->bh
);
146 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
148 if (acb
->task
!= NULL
) {
149 scsi_free_scsi_task(acb
->task
);
157 iscsi_schedule_bh(IscsiAIOCB
*acb
)
162 acb
->bh
= aio_bh_new(acb
->iscsilun
->aio_context
, iscsi_bh_cb
, acb
);
163 qemu_bh_schedule(acb
->bh
);
166 static void iscsi_co_generic_bh_cb(void *opaque
)
168 struct IscsiTask
*iTask
= opaque
;
170 qemu_bh_delete(iTask
->bh
);
171 qemu_coroutine_enter(iTask
->co
);
174 static void iscsi_retry_timer_expired(void *opaque
)
176 struct IscsiTask
*iTask
= opaque
;
179 qemu_coroutine_enter(iTask
->co
);
183 static inline unsigned exp_random(double mean
)
185 return -mean
* log((double)rand() / RAND_MAX
);
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.
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
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
207 static int iscsi_translate_sense(struct scsi_sense
*sense
)
211 switch (sense
->key
) {
212 case SCSI_SENSE_NOT_READY
:
214 case SCSI_SENSE_DATA_PROTECTION
:
216 case SCSI_SENSE_COMMAND_ABORTED
:
218 case SCSI_SENSE_ILLEGAL_REQUEST
:
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
:
231 case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE
:
234 case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED
:
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
:
242 case SCSI_SENSE_ASCQ_WRITE_PROTECTED
:
253 iscsi_co_generic_cb(struct iscsi_context
*iscsi
, int status
,
254 void *command_data
, void *opaque
)
256 struct IscsiTask
*iTask
= opaque
;
257 struct scsi_task
*task
= command_data
;
259 iTask
->status
= status
;
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
));
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;
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
);
296 iTask
->err_code
= iscsi_translate_sense(&task
->sense
);
297 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi
));
302 iTask
->bh
= aio_bh_new(iTask
->iscsilun
->aio_context
,
303 iscsi_co_generic_bh_cb
, iTask
);
304 qemu_bh_schedule(iTask
->bh
);
310 static void iscsi_co_init_iscsitask(IscsiLun
*iscsilun
, struct IscsiTask
*iTask
)
312 *iTask
= (struct IscsiTask
) {
313 .co
= qemu_coroutine_self(),
314 .iscsilun
= iscsilun
,
319 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
322 IscsiAIOCB
*acb
= private_data
;
324 acb
->status
= -ECANCELED
;
325 iscsi_schedule_bh(acb
);
329 iscsi_aio_cancel(BlockAIOCB
*blockacb
)
331 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
332 IscsiLun
*iscsilun
= acb
->iscsilun
;
334 if (acb
->status
!= -EINPROGRESS
) {
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
);
344 static const AIOCBInfo iscsi_aiocb_info
= {
345 .aiocb_size
= sizeof(IscsiAIOCB
),
346 .cancel_async
= iscsi_aio_cancel
,
350 static void iscsi_process_read(void *arg
);
351 static void iscsi_process_write(void *arg
);
354 iscsi_set_events(IscsiLun
*iscsilun
)
356 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
357 int ev
= iscsi_which_events(iscsi
);
359 if (ev
!= iscsilun
->events
) {
360 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsi
),
362 (ev
& POLLIN
) ? iscsi_process_read
: NULL
,
363 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
365 iscsilun
->events
= ev
;
369 static void iscsi_timed_check_events(void *opaque
)
371 IscsiLun
*iscsilun
= opaque
;
373 /* check for timed out requests */
374 iscsi_service(iscsilun
->iscsi
, 0);
376 if (iscsilun
->request_timed_out
) {
377 iscsilun
->request_timed_out
= false;
378 iscsi_reconnect(iscsilun
->iscsi
);
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
);
385 timer_mod(iscsilun
->event_timer
,
386 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
390 iscsi_process_read(void *arg
)
392 IscsiLun
*iscsilun
= arg
;
393 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
395 iscsi_service(iscsi
, POLLIN
);
396 iscsi_set_events(iscsilun
);
400 iscsi_process_write(void *arg
)
402 IscsiLun
*iscsilun
= arg
;
403 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
405 iscsi_service(iscsi
, POLLOUT
);
406 iscsi_set_events(iscsilun
);
409 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
411 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
414 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
416 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
419 static bool is_byte_request_lun_aligned(int64_t offset
, int count
,
422 if (offset
% iscsilun
->block_size
|| count
% iscsilun
->block_size
) {
423 error_report("iSCSI misaligned request: "
424 "iscsilun->block_size %u, offset %" PRIi64
426 iscsilun
->block_size
, offset
, count
);
432 static bool is_sector_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
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
,
441 static void iscsi_allocmap_free(IscsiLun
*iscsilun
)
443 g_free(iscsilun
->allocmap
);
444 g_free(iscsilun
->allocmap_valid
);
445 iscsilun
->allocmap
= NULL
;
446 iscsilun
->allocmap_valid
= NULL
;
450 static int iscsi_allocmap_init(IscsiLun
*iscsilun
, int open_flags
)
452 iscsi_allocmap_free(iscsilun
);
454 iscsilun
->allocmap_size
=
455 DIV_ROUND_UP(sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
),
456 iscsilun
->cluster_sectors
);
458 iscsilun
->allocmap
= bitmap_try_new(iscsilun
->allocmap_size
);
459 if (!iscsilun
->allocmap
) {
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 */
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
);
481 iscsi_allocmap_update(IscsiLun
*iscsilun
, int64_t sector_num
,
482 int nb_sectors
, bool allocated
, bool valid
)
484 int64_t cl_num_expanded
, nb_cls_expanded
, cl_num_shrunk
, nb_cls_shrunk
;
486 if (iscsilun
->allocmap
== NULL
) {
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
498 bitmap_set(iscsilun
->allocmap
, cl_num_expanded
, nb_cls_expanded
);
500 bitmap_clear(iscsilun
->allocmap
, cl_num_shrunk
, nb_cls_shrunk
);
503 if (iscsilun
->allocmap_valid
== NULL
) {
507 bitmap_set(iscsilun
->allocmap_valid
, cl_num_shrunk
, nb_cls_shrunk
);
509 bitmap_clear(iscsilun
->allocmap_valid
, cl_num_expanded
,
515 iscsi_allocmap_set_allocated(IscsiLun
*iscsilun
, int64_t sector_num
,
518 iscsi_allocmap_update(iscsilun
, sector_num
, nb_sectors
, true, true);
522 iscsi_allocmap_set_unallocated(IscsiLun
*iscsilun
, int64_t sector_num
,
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.
528 iscsi_allocmap_update(iscsilun
, sector_num
, nb_sectors
, false, true);
531 static void iscsi_allocmap_set_invalid(IscsiLun
*iscsilun
, int64_t sector_num
,
534 iscsi_allocmap_update(iscsilun
, sector_num
, nb_sectors
, false, false);
537 static void iscsi_allocmap_invalidate(IscsiLun
*iscsilun
)
539 if (iscsilun
->allocmap
) {
540 bitmap_zero(iscsilun
->allocmap
, iscsilun
->allocmap_size
);
542 if (iscsilun
->allocmap_valid
) {
543 bitmap_zero(iscsilun
->allocmap_valid
, iscsilun
->allocmap_size
);
548 iscsi_allocmap_is_allocated(IscsiLun
*iscsilun
, int64_t sector_num
,
552 if (iscsilun
->allocmap
== NULL
) {
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
);
560 static inline bool iscsi_allocmap_is_valid(IscsiLun
*iscsilun
,
561 int64_t sector_num
, int nb_sectors
)
564 if (iscsilun
->allocmap_valid
== NULL
) {
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
);
572 static int coroutine_fn
573 iscsi_co_writev_flags(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
574 QEMUIOVector
*iov
, int flags
)
576 IscsiLun
*iscsilun
= bs
->opaque
;
577 struct IscsiTask iTask
;
579 uint32_t num_sectors
;
580 bool fua
= flags
& BDRV_REQ_FUA
;
583 assert(iscsilun
->dpofua
);
585 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
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
);
596 lba
= sector_qemu2lun(sector_num
, iscsilun
);
597 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
598 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
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
);
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
);
611 if (iTask
.task
== NULL
) {
614 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
616 while (!iTask
.complete
) {
617 iscsi_set_events(iscsilun
);
618 qemu_coroutine_yield();
621 if (iTask
.task
!= NULL
) {
622 scsi_free_scsi_task(iTask
.task
);
626 if (iTask
.do_retry
) {
631 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
632 iscsi_allocmap_set_invalid(iscsilun
, sector_num
, nb_sectors
);
633 return iTask
.err_code
;
636 iscsi_allocmap_set_allocated(iscsilun
, sector_num
, nb_sectors
);
643 static int64_t coroutine_fn
iscsi_co_get_block_status(BlockDriverState
*bs
,
645 int nb_sectors
, int *pnum
,
646 BlockDriverState
**file
)
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
;
654 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
656 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
661 /* default to all sectors allocated */
662 ret
= BDRV_BLOCK_DATA
;
663 ret
|= (sector_num
<< BDRV_SECTOR_BITS
) | BDRV_BLOCK_OFFSET_VALID
;
666 /* LUN does not support logical block provisioning */
667 if (!iscsilun
->lbpme
) {
672 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
673 sector_qemu2lun(sector_num
, iscsilun
),
674 8 + 16, iscsi_co_generic_cb
,
680 while (!iTask
.complete
) {
681 iscsi_set_events(iscsilun
);
682 qemu_coroutine_yield();
685 if (iTask
.do_retry
) {
686 if (iTask
.task
!= NULL
) {
687 scsi_free_scsi_task(iTask
.task
);
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 */
702 lbas
= scsi_datain_unmarshall(iTask
.task
);
708 lbasd
= &lbas
->descriptors
[0];
710 if (sector_qemu2lun(sector_num
, iscsilun
) != lbasd
->lba
) {
715 *pnum
= sector_lun2qemu(lbasd
->num_blocks
, iscsilun
);
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
;
725 if (ret
& BDRV_BLOCK_ZERO
) {
726 iscsi_allocmap_set_unallocated(iscsilun
, sector_num
, *pnum
);
728 iscsi_allocmap_set_allocated(iscsilun
, sector_num
, *pnum
);
731 if (*pnum
> nb_sectors
) {
735 if (iTask
.task
!= NULL
) {
736 scsi_free_scsi_task(iTask
.task
);
738 if (ret
> 0 && ret
& BDRV_BLOCK_OFFSET_VALID
) {
744 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
745 int64_t sector_num
, int nb_sectors
,
748 IscsiLun
*iscsilun
= bs
->opaque
;
749 struct IscsiTask iTask
;
751 uint32_t num_sectors
;
753 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
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
);
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
);
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
)) {
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
);
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
);
795 lba
= sector_qemu2lun(sector_num
, iscsilun
);
796 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
798 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
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
);
806 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
807 num_sectors
* iscsilun
->block_size
,
808 iscsilun
->block_size
,
810 iscsi_co_generic_cb
, &iTask
);
812 if (iTask
.task
== NULL
) {
815 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
817 while (!iTask
.complete
) {
818 iscsi_set_events(iscsilun
);
819 qemu_coroutine_yield();
822 if (iTask
.task
!= NULL
) {
823 scsi_free_scsi_task(iTask
.task
);
827 if (iTask
.do_retry
) {
832 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
833 return iTask
.err_code
;
839 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
841 IscsiLun
*iscsilun
= bs
->opaque
;
842 struct IscsiTask iTask
;
844 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
846 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
847 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
851 while (!iTask
.complete
) {
852 iscsi_set_events(iscsilun
);
853 qemu_coroutine_yield();
856 if (iTask
.task
!= NULL
) {
857 scsi_free_scsi_task(iTask
.task
);
861 if (iTask
.do_retry
) {
866 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
867 return iTask
.err_code
;
875 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
876 void *command_data
, void *opaque
)
878 IscsiAIOCB
*acb
= opaque
;
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
);
890 acb
->ioh
->driver_status
= 0;
891 acb
->ioh
->host_status
= 0;
893 acb
->ioh
->status
= status
;
895 #define SG_ERR_DRIVER_SENSE 0x08
897 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
900 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
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
);
908 iscsi_schedule_bh(acb
);
911 static void iscsi_ioctl_bh_completion(void *opaque
)
913 IscsiAIOCB
*acb
= opaque
;
915 qemu_bh_delete(acb
->bh
);
916 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
920 static void iscsi_ioctl_handle_emulated(IscsiAIOCB
*acb
, int req
, void *buf
)
922 BlockDriverState
*bs
= acb
->common
.bs
;
923 IscsiLun
*iscsilun
= bs
->opaque
;
927 case SG_GET_VERSION_NUM
:
931 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
937 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
),
938 iscsi_ioctl_bh_completion
, acb
);
940 qemu_bh_schedule(acb
->bh
);
943 static BlockAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
944 unsigned long int req
, void *buf
,
945 BlockCompletionFunc
*cb
, void *opaque
)
947 IscsiLun
*iscsilun
= bs
->opaque
;
948 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
949 struct iscsi_data data
;
952 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
954 acb
->iscsilun
= iscsilun
;
956 acb
->status
= -EINPROGRESS
;
961 iscsi_ioctl_handle_emulated(acb
, req
, buf
);
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
);
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
));
979 memset(acb
->task
, 0, sizeof(struct scsi_task
));
981 switch (acb
->ioh
->dxfer_direction
) {
982 case SG_DXFER_TO_DEV
:
983 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
985 case SG_DXFER_FROM_DEV
:
986 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
989 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
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
;
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
;
1003 scsi_task_set_iov_out(acb
->task
,
1004 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1005 acb
->ioh
->iovec_count
);
1009 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
1011 (data
.size
> 0) ? &data
: NULL
,
1013 scsi_free_scsi_task(acb
->task
);
1014 qemu_aio_unref(acb
);
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
,
1025 scsi_task_set_iov_in(acb
->task
,
1026 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1027 acb
->ioh
->iovec_count
);
1031 iscsi_set_events(iscsilun
);
1033 return &acb
->common
;
1039 iscsi_getlength(BlockDriverState
*bs
)
1041 IscsiLun
*iscsilun
= bs
->opaque
;
1044 len
= iscsilun
->num_blocks
;
1045 len
*= iscsilun
->block_size
;
1051 coroutine_fn
iscsi_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
1054 IscsiLun
*iscsilun
= bs
->opaque
;
1055 struct IscsiTask iTask
;
1056 struct unmap_list list
;
1058 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
1062 if (!iscsilun
->lbp
.lbpu
) {
1063 /* UNMAP is not supported by the target */
1067 list
.lba
= sector_qemu2lun(sector_num
, iscsilun
);
1068 list
.num
= sector_qemu2lun(nb_sectors
, iscsilun
);
1070 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1072 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
1073 iscsi_co_generic_cb
, &iTask
) == NULL
) {
1077 while (!iTask
.complete
) {
1078 iscsi_set_events(iscsilun
);
1079 qemu_coroutine_yield();
1082 if (iTask
.task
!= NULL
) {
1083 scsi_free_scsi_task(iTask
.task
);
1087 if (iTask
.do_retry
) {
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 */
1099 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1100 return iTask
.err_code
;
1103 iscsi_allocmap_set_invalid(iscsilun
, sector_num
, nb_sectors
);
1109 coroutine_fn
iscsi_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1110 int count
, BdrvRequestFlags flags
)
1112 IscsiLun
*iscsilun
= bs
->opaque
;
1113 struct IscsiTask iTask
;
1116 bool use_16_for_ws
= iscsilun
->use_16_for_rw
;
1118 if (!is_byte_request_lun_aligned(offset
, count
, iscsilun
)) {
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;
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
;
1135 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
1136 /* WRITESAME without UNMAP is not supported by the target */
1140 lba
= offset
/ iscsilun
->block_size
;
1141 nb_blocks
= count
/ iscsilun
->block_size
;
1143 if (iscsilun
->zeroblock
== NULL
) {
1144 iscsilun
->zeroblock
= g_try_malloc0(iscsilun
->block_size
);
1145 if (iscsilun
->zeroblock
== NULL
) {
1150 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
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
);
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
);
1163 if (iTask
.task
== NULL
) {
1167 while (!iTask
.complete
) {
1168 iscsi_set_events(iscsilun
);
1169 qemu_coroutine_yield();
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
);
1182 if (iTask
.task
!= NULL
) {
1183 scsi_free_scsi_task(iTask
.task
);
1187 if (iTask
.do_retry
) {
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
;
1198 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1199 iscsi_allocmap_set_invalid(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1200 count
>> BDRV_SECTOR_BITS
);
1202 iscsi_allocmap_set_allocated(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1203 count
>> BDRV_SECTOR_BITS
);
1209 static void parse_chap(struct iscsi_context
*iscsi
, const char *target
,
1214 const char *user
= NULL
;
1215 const char *password
= NULL
;
1216 const char *secretid
;
1217 char *secret
= NULL
;
1219 list
= qemu_find_opts("iscsi");
1224 opts
= qemu_opts_find(list
, target
);
1226 opts
= QTAILQ_FIRST(&list
->head
);
1232 user
= qemu_opt_get(opts
, "user");
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");
1245 secret
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
1250 } else if (!password
) {
1251 error_setg(errp
, "CHAP username specified but no password was given");
1255 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
1256 error_setg(errp
, "Failed to set initiator username and password");
1262 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
,
1267 const char *digest
= NULL
;
1269 list
= qemu_find_opts("iscsi");
1274 opts
= qemu_opts_find(list
, target
);
1276 opts
= QTAILQ_FIRST(&list
->head
);
1282 digest
= qemu_opt_get(opts
, "header-digest");
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
);
1296 error_setg(errp
, "Invalid header-digest setting : %s", digest
);
1300 static char *parse_initiator_name(const char *target
)
1306 UuidInfo
*uuid_info
;
1308 list
= qemu_find_opts("iscsi");
1310 opts
= qemu_opts_find(list
, target
);
1312 opts
= QTAILQ_FIRST(&list
->head
);
1315 name
= qemu_opt_get(opts
, "initiator-name");
1317 return g_strdup(name
);
1322 uuid_info
= qmp_query_uuid(NULL
);
1323 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
1324 name
= qemu_get_vm_name();
1326 name
= uuid_info
->UUID
;
1328 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1329 name
? ":" : "", name
? name
: "");
1330 qapi_free_UuidInfo(uuid_info
);
1334 static int parse_timeout(const char *target
)
1338 const char *timeout
;
1340 list
= qemu_find_opts("iscsi");
1342 opts
= qemu_opts_find(list
, target
);
1344 opts
= QTAILQ_FIRST(&list
->head
);
1347 timeout
= qemu_opt_get(opts
, "timeout");
1349 return atoi(timeout
);
1357 static void iscsi_nop_timed_event(void *opaque
)
1359 IscsiLun
*iscsilun
= opaque
;
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.");
1369 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1370 iscsi_set_events(iscsilun
);
1373 static void iscsi_readcapacity_sync(IscsiLun
*iscsilun
, Error
**errp
)
1375 struct scsi_task
*task
= NULL
;
1376 struct scsi_readcapacity10
*rc10
= NULL
;
1377 struct scsi_readcapacity16
*rc16
= NULL
;
1378 int retries
= ISCSI_CMD_RETRIES
;
1382 scsi_free_scsi_task(task
);
1386 switch (iscsilun
->type
) {
1388 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
1389 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1390 rc16
= scsi_datain_unmarshall(task
);
1392 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity16 data.");
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);
1402 if (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1403 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
1406 /* Fall through and try READ CAPACITY(10) instead. */
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
);
1412 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity10 data.");
1414 iscsilun
->block_size
= rc10
->block_size
;
1415 if (rc10
->lba
== 0) {
1416 /* blank disk loaded */
1417 iscsilun
->num_blocks
= 0;
1419 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1427 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1428 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
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
);
1439 scsi_free_scsi_task(task
);
1443 /* TODO Convert to fine grained options */
1444 static QemuOptsList runtime_opts
= {
1446 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1450 .type
= QEMU_OPT_STRING
,
1451 .help
= "URL to the iscsi image",
1453 { /* end of list */ }
1457 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
, int lun
,
1458 int evpd
, int pc
, void **inq
, Error
**errp
)
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
) {
1466 full_size
= scsi_datain_getfullsize(task
);
1467 if (full_size
> task
->datain
.size
) {
1468 scsi_free_scsi_task(task
);
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
) {
1477 *inq
= scsi_datain_unmarshall(task
);
1479 error_setg(errp
, "iSCSI: failed to unmarshall inquiry datain blob");
1486 error_setg(errp
, "iSCSI: Inquiry command failed : %s",
1487 iscsi_get_error(iscsi
));
1490 scsi_free_scsi_task(task
);
1495 static void iscsi_detach_aio_context(BlockDriverState
*bs
)
1497 IscsiLun
*iscsilun
= bs
->opaque
;
1499 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsilun
->iscsi
),
1500 false, NULL
, NULL
, NULL
);
1501 iscsilun
->events
= 0;
1503 if (iscsilun
->nop_timer
) {
1504 timer_del(iscsilun
->nop_timer
);
1505 timer_free(iscsilun
->nop_timer
);
1506 iscsilun
->nop_timer
= NULL
;
1508 if (iscsilun
->event_timer
) {
1509 timer_del(iscsilun
->event_timer
);
1510 timer_free(iscsilun
->event_timer
);
1511 iscsilun
->event_timer
= NULL
;
1515 static void iscsi_attach_aio_context(BlockDriverState
*bs
,
1516 AioContext
*new_context
)
1518 IscsiLun
*iscsilun
= bs
->opaque
;
1520 iscsilun
->aio_context
= new_context
;
1521 iscsi_set_events(iscsilun
);
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
);
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
);
1539 static void iscsi_modesense_sync(IscsiLun
*iscsilun
)
1541 struct scsi_task
*task
;
1542 struct scsi_mode_sense
*ms
= NULL
;
1543 iscsilun
->write_protected
= false;
1544 iscsilun
->dpofua
= false;
1546 task
= iscsi_modesense6_sync(iscsilun
->iscsi
, iscsilun
->lun
,
1547 1, SCSI_MODESENSE_PC_CURRENT
,
1550 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1551 iscsi_get_error(iscsilun
->iscsi
));
1555 if (task
->status
!= SCSI_STATUS_GOOD
) {
1556 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1559 ms
= scsi_datain_unmarshall(task
);
1561 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1562 iscsi_get_error(iscsilun
->iscsi
));
1565 iscsilun
->write_protected
= ms
->device_specific_parameter
& 0x80;
1566 iscsilun
->dpofua
= ms
->device_specific_parameter
& 0x10;
1570 scsi_free_scsi_task(task
);
1575 * We support iscsi url's on the form
1576 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1578 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
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
;
1589 Error
*local_err
= NULL
;
1590 const char *filename
;
1591 int i
, ret
= 0, timeout
= 0;
1593 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1594 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1596 error_propagate(errp
, local_err
);
1601 filename
= qemu_opt_get(opts
, "filename");
1603 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1604 if (iscsi_url
== NULL
) {
1605 error_setg(errp
, "Failed to parse URL : %s", filename
);
1610 memset(iscsilun
, 0, sizeof(IscsiLun
));
1612 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1614 iscsi
= iscsi_create_context(initiator_name
);
1615 if (iscsi
== NULL
) {
1616 error_setg(errp
, "iSCSI: Failed to create iSCSI context.");
1621 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1622 error_setg(errp
, "iSCSI: Failed to set target name.");
1627 if (iscsi_url
->user
[0] != '\0') {
1628 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1631 error_setg(errp
, "Failed to set initiator username and password");
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
);
1645 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1646 error_setg(errp
, "iSCSI: Failed to set session type to normal.");
1651 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
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
);
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
);
1667 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
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
));
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;
1683 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0,
1684 (void **) &inq
, errp
);
1689 iscsilun
->type
= inq
->periperal_device_type
;
1690 scsi_free_scsi_task(task
);
1693 iscsi_modesense_sync(iscsilun
);
1694 if (iscsilun
->dpofua
) {
1695 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1697 bs
->supported_zero_flags
= BDRV_REQ_MAY_UNMAP
;
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");
1707 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1708 if (local_err
!= NULL
) {
1709 error_propagate(errp
, local_err
);
1713 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
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.
1719 if (iscsilun
->type
!= TYPE_DISK
&& iscsilun
->type
!= TYPE_ROM
) {
1723 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1724 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES
,
1725 (void **) &inq_vpd
, errp
);
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
) {
1743 memcpy(&iscsilun
->lbp
, inq_lbp
,
1744 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1745 scsi_free_scsi_task(inq_task
);
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
) {
1755 memcpy(&iscsilun
->bl
, inq_bl
,
1756 sizeof(struct scsi_inquiry_block_limits
));
1757 scsi_free_scsi_task(inq_task
);
1763 scsi_free_scsi_task(task
);
1766 iscsi_attach_aio_context(bs
, iscsilun
->aio_context
);
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
);
1781 qemu_opts_del(opts
);
1782 g_free(initiator_name
);
1783 if (iscsi_url
!= NULL
) {
1784 iscsi_destroy_url(iscsi_url
);
1787 scsi_free_scsi_task(task
);
1791 if (iscsi
!= NULL
) {
1792 if (iscsi_is_logged_in(iscsi
)) {
1793 iscsi_logout_sync(iscsi
);
1795 iscsi_destroy_context(iscsi
);
1797 memset(iscsilun
, 0, sizeof(IscsiLun
));
1802 static void iscsi_close(BlockDriverState
*bs
)
1804 IscsiLun
*iscsilun
= bs
->opaque
;
1805 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1807 iscsi_detach_aio_context(bs
);
1808 if (iscsi_is_logged_in(iscsi
)) {
1809 iscsi_logout_sync(iscsi
);
1811 iscsi_destroy_context(iscsi
);
1812 g_free(iscsilun
->zeroblock
);
1813 iscsi_allocmap_free(iscsilun
);
1814 memset(iscsilun
, 0, sizeof(IscsiLun
));
1817 static void iscsi_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1819 /* We don't actually refresh here, but just return data queried in
1820 * iscsi_open(): iscsi targets don't change their limits. */
1822 IscsiLun
*iscsilun
= bs
->opaque
;
1823 uint64_t max_xfer_len
= iscsilun
->use_16_for_rw
? 0xffffffff : 0xffff;
1825 bs
->bl
.request_alignment
= iscsilun
->block_size
;
1827 if (iscsilun
->bl
.max_xfer_len
) {
1828 max_xfer_len
= MIN(max_xfer_len
, iscsilun
->bl
.max_xfer_len
);
1831 if (max_xfer_len
* iscsilun
->block_size
< INT_MAX
) {
1832 bs
->bl
.max_transfer
= max_xfer_len
* iscsilun
->block_size
;
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
;
1840 bs
->bl
.pdiscard_alignment
=
1841 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
1843 bs
->bl
.pdiscard_alignment
= iscsilun
->block_size
;
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
;
1850 if (iscsilun
->lbp
.lbpws
) {
1851 bs
->bl
.pwrite_zeroes_alignment
=
1852 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
1854 bs
->bl
.pwrite_zeroes_alignment
= iscsilun
->block_size
;
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
);
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
)
1868 IscsiLun
*iscsilun
= state
->bs
->opaque
;
1870 if (state
->flags
& BDRV_O_RDWR
&& iscsilun
->write_protected
) {
1871 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1877 static void iscsi_reopen_commit(BDRVReopenState
*reopen_state
)
1879 IscsiLun
*iscsilun
= reopen_state
->bs
->opaque
;
1881 /* the cache.direct status might have changed */
1882 if (iscsilun
->allocmap
!= NULL
) {
1883 iscsi_allocmap_init(iscsilun
, reopen_state
->flags
);
1887 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1889 IscsiLun
*iscsilun
= bs
->opaque
;
1890 Error
*local_err
= NULL
;
1892 if (iscsilun
->type
!= TYPE_DISK
) {
1896 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1897 if (local_err
!= NULL
) {
1898 error_free(local_err
);
1902 if (offset
> iscsi_getlength(bs
)) {
1906 if (iscsilun
->allocmap
!= NULL
) {
1907 iscsi_allocmap_init(iscsilun
, bs
->open_flags
);
1913 static int iscsi_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1916 int64_t total_size
= 0;
1917 BlockDriverState
*bs
;
1918 IscsiLun
*iscsilun
= NULL
;
1923 /* Read out options */
1924 total_size
= DIV_ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1926 bs
->opaque
= g_new0(struct IscsiLun
, 1);
1927 iscsilun
= bs
->opaque
;
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
);
1937 iscsi_detach_aio_context(bs
);
1938 if (iscsilun
->type
!= TYPE_DISK
) {
1942 if (bs
->total_sectors
< total_size
) {
1949 if (iscsilun
->iscsi
!= NULL
) {
1950 iscsi_destroy_context(iscsilun
->iscsi
);
1958 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
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
;
1967 static void iscsi_invalidate_cache(BlockDriverState
*bs
,
1970 IscsiLun
*iscsilun
= bs
->opaque
;
1971 iscsi_allocmap_invalidate(iscsilun
);
1974 static QemuOptsList iscsi_create_opts
= {
1975 .name
= "iscsi-create-opts",
1976 .head
= QTAILQ_HEAD_INITIALIZER(iscsi_create_opts
.head
),
1979 .name
= BLOCK_OPT_SIZE
,
1980 .type
= QEMU_OPT_SIZE
,
1981 .help
= "Virtual disk size"
1983 { /* end of list */ }
1987 static BlockDriver bdrv_iscsi
= {
1988 .format_name
= "iscsi",
1989 .protocol_name
= "iscsi",
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
,
2001 .bdrv_getlength
= iscsi_getlength
,
2002 .bdrv_get_info
= iscsi_get_info
,
2003 .bdrv_truncate
= iscsi_truncate
,
2004 .bdrv_refresh_limits
= iscsi_refresh_limits
,
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
,
2014 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
2017 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
2018 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
2021 static QemuOptsList qemu_iscsi_opts
= {
2023 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
2027 .type
= QEMU_OPT_STRING
,
2028 .help
= "username for CHAP authentication to target",
2031 .type
= QEMU_OPT_STRING
,
2032 .help
= "password for CHAP authentication to target",
2034 .name
= "password-secret",
2035 .type
= QEMU_OPT_STRING
,
2036 .help
= "ID of the secret providing password for CHAP "
2037 "authentication to target",
2039 .name
= "header-digest",
2040 .type
= QEMU_OPT_STRING
,
2041 .help
= "HeaderDigest setting. "
2042 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
2044 .name
= "initiator-name",
2045 .type
= QEMU_OPT_STRING
,
2046 .help
= "Initiator iqn name to use when connecting",
2049 .type
= QEMU_OPT_NUMBER
,
2050 .help
= "Request timeout in seconds (default 0 = no timeout)",
2052 { /* end of list */ }
2056 static void iscsi_block_init(void)
2058 bdrv_register(&bdrv_iscsi
);
2059 qemu_add_opts(&qemu_iscsi_opts
);
2062 block_init(iscsi_block_init
);