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 "qemu/uuid.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
;
99 QEMUTimer retry_timer
;
103 typedef struct IscsiAIOCB
{
108 struct scsi_task
*task
;
119 /* libiscsi uses time_t so its enough to process events every second */
120 #define EVENT_INTERVAL 1000
121 #define NOP_INTERVAL 5000
122 #define MAX_NOP_FAILURES 3
123 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
124 static const unsigned iscsi_retry_times
[] = {8, 32, 128, 512, 2048, 8192, 32768};
126 /* this threshold is a trade-off knob to choose between
127 * the potential additional overhead of an extra GET_LBA_STATUS request
128 * vs. unnecessarily reading a lot of zero sectors over the wire.
129 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
130 * sectors we check the allocation status of the area covered by the
131 * request first if the allocationmap indicates that the area might be
133 #define ISCSI_CHECKALLOC_THRES 64
140 qemu_bh_delete(acb
->bh
);
145 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
147 if (acb
->task
!= NULL
) {
148 scsi_free_scsi_task(acb
->task
);
156 iscsi_schedule_bh(IscsiAIOCB
*acb
)
161 acb
->bh
= aio_bh_new(acb
->iscsilun
->aio_context
, iscsi_bh_cb
, acb
);
162 qemu_bh_schedule(acb
->bh
);
165 static void iscsi_co_generic_bh_cb(void *opaque
)
167 struct IscsiTask
*iTask
= opaque
;
169 qemu_coroutine_enter(iTask
->co
);
172 static void iscsi_retry_timer_expired(void *opaque
)
174 struct IscsiTask
*iTask
= opaque
;
177 qemu_coroutine_enter(iTask
->co
);
181 static inline unsigned exp_random(double mean
)
183 return -mean
* log((double)rand() / RAND_MAX
);
186 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
187 * libiscsi 1.10.0, together with other constants we need. Use it as
188 * a hint that we have to define them ourselves if needed, to keep the
189 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
190 * the test because SCSI_STATUS_* is an enum.
192 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
193 * an enum, check against the LIBISCSI_API_VERSION macro, which was
194 * introduced in 1.11.0. If it is present, there is no need to define
197 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
198 !defined(LIBISCSI_API_VERSION)
199 #define SCSI_STATUS_TASK_SET_FULL 0x28
200 #define SCSI_STATUS_TIMEOUT 0x0f000002
201 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
202 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
205 #ifndef LIBISCSI_API_VERSION
206 #define LIBISCSI_API_VERSION 20130701
209 static int iscsi_translate_sense(struct scsi_sense
*sense
)
213 switch (sense
->key
) {
214 case SCSI_SENSE_NOT_READY
:
216 case SCSI_SENSE_DATA_PROTECTION
:
218 case SCSI_SENSE_COMMAND_ABORTED
:
220 case SCSI_SENSE_ILLEGAL_REQUEST
:
226 switch (sense
->ascq
) {
227 case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR
:
228 case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
:
229 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
:
230 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST
:
233 case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE
:
236 case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED
:
239 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT
:
240 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED
:
241 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN
:
244 case SCSI_SENSE_ASCQ_WRITE_PROTECTED
:
255 iscsi_co_generic_cb(struct iscsi_context
*iscsi
, int status
,
256 void *command_data
, void *opaque
)
258 struct IscsiTask
*iTask
= opaque
;
259 struct scsi_task
*task
= command_data
;
261 iTask
->status
= status
;
265 if (status
!= SCSI_STATUS_GOOD
) {
266 if (iTask
->retries
++ < ISCSI_CMD_RETRIES
) {
267 if (status
== SCSI_STATUS_CHECK_CONDITION
268 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
269 error_report("iSCSI CheckCondition: %s",
270 iscsi_get_error(iscsi
));
274 if (status
== SCSI_STATUS_BUSY
||
275 status
== SCSI_STATUS_TIMEOUT
||
276 status
== SCSI_STATUS_TASK_SET_FULL
) {
277 unsigned retry_time
=
278 exp_random(iscsi_retry_times
[iTask
->retries
- 1]);
279 if (status
== SCSI_STATUS_TIMEOUT
) {
280 /* make sure the request is rescheduled AFTER the
281 * reconnect is initiated */
282 retry_time
= EVENT_INTERVAL
* 2;
283 iTask
->iscsilun
->request_timed_out
= true;
285 error_report("iSCSI Busy/TaskSetFull/TimeOut"
286 " (retry #%u in %u ms): %s",
287 iTask
->retries
, retry_time
,
288 iscsi_get_error(iscsi
));
289 aio_timer_init(iTask
->iscsilun
->aio_context
,
290 &iTask
->retry_timer
, QEMU_CLOCK_REALTIME
,
291 SCALE_MS
, iscsi_retry_timer_expired
, iTask
);
292 timer_mod(&iTask
->retry_timer
,
293 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + retry_time
);
298 iTask
->err_code
= iscsi_translate_sense(&task
->sense
);
299 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi
));
304 aio_bh_schedule_oneshot(iTask
->iscsilun
->aio_context
,
305 iscsi_co_generic_bh_cb
, iTask
);
311 static void iscsi_co_init_iscsitask(IscsiLun
*iscsilun
, struct IscsiTask
*iTask
)
313 *iTask
= (struct IscsiTask
) {
314 .co
= qemu_coroutine_self(),
315 .iscsilun
= iscsilun
,
320 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
323 IscsiAIOCB
*acb
= private_data
;
325 acb
->status
= -ECANCELED
;
326 iscsi_schedule_bh(acb
);
330 iscsi_aio_cancel(BlockAIOCB
*blockacb
)
332 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
333 IscsiLun
*iscsilun
= acb
->iscsilun
;
335 if (acb
->status
!= -EINPROGRESS
) {
339 /* send a task mgmt call to the target to cancel the task on the target */
340 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
341 iscsi_abort_task_cb
, acb
);
345 static const AIOCBInfo iscsi_aiocb_info
= {
346 .aiocb_size
= sizeof(IscsiAIOCB
),
347 .cancel_async
= iscsi_aio_cancel
,
351 static void iscsi_process_read(void *arg
);
352 static void iscsi_process_write(void *arg
);
355 iscsi_set_events(IscsiLun
*iscsilun
)
357 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
358 int ev
= iscsi_which_events(iscsi
);
360 if (ev
!= iscsilun
->events
) {
361 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsi
),
363 (ev
& POLLIN
) ? iscsi_process_read
: NULL
,
364 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
366 iscsilun
->events
= ev
;
370 static void iscsi_timed_check_events(void *opaque
)
372 IscsiLun
*iscsilun
= opaque
;
374 /* check for timed out requests */
375 iscsi_service(iscsilun
->iscsi
, 0);
377 if (iscsilun
->request_timed_out
) {
378 iscsilun
->request_timed_out
= false;
379 iscsi_reconnect(iscsilun
->iscsi
);
382 /* newer versions of libiscsi may return zero events. Ensure we are able
383 * to return to service once this situation changes. */
384 iscsi_set_events(iscsilun
);
386 timer_mod(iscsilun
->event_timer
,
387 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
391 iscsi_process_read(void *arg
)
393 IscsiLun
*iscsilun
= arg
;
394 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
396 iscsi_service(iscsi
, POLLIN
);
397 iscsi_set_events(iscsilun
);
401 iscsi_process_write(void *arg
)
403 IscsiLun
*iscsilun
= arg
;
404 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
406 iscsi_service(iscsi
, POLLOUT
);
407 iscsi_set_events(iscsilun
);
410 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
412 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
415 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
417 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
420 static bool is_byte_request_lun_aligned(int64_t offset
, int count
,
423 if (offset
% iscsilun
->block_size
|| count
% iscsilun
->block_size
) {
424 error_report("iSCSI misaligned request: "
425 "iscsilun->block_size %u, offset %" PRIi64
427 iscsilun
->block_size
, offset
, count
);
433 static bool is_sector_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
436 assert(nb_sectors
<= BDRV_REQUEST_MAX_SECTORS
);
437 return is_byte_request_lun_aligned(sector_num
<< BDRV_SECTOR_BITS
,
438 nb_sectors
<< BDRV_SECTOR_BITS
,
442 static void iscsi_allocmap_free(IscsiLun
*iscsilun
)
444 g_free(iscsilun
->allocmap
);
445 g_free(iscsilun
->allocmap_valid
);
446 iscsilun
->allocmap
= NULL
;
447 iscsilun
->allocmap_valid
= NULL
;
451 static int iscsi_allocmap_init(IscsiLun
*iscsilun
, int open_flags
)
453 iscsi_allocmap_free(iscsilun
);
455 iscsilun
->allocmap_size
=
456 DIV_ROUND_UP(sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
),
457 iscsilun
->cluster_sectors
);
459 iscsilun
->allocmap
= bitmap_try_new(iscsilun
->allocmap_size
);
460 if (!iscsilun
->allocmap
) {
464 if (open_flags
& BDRV_O_NOCACHE
) {
465 /* in case that cache.direct = on all allocmap entries are
466 * treated as invalid to force a relookup of the block
467 * status on every read request */
471 iscsilun
->allocmap_valid
= bitmap_try_new(iscsilun
->allocmap_size
);
472 if (!iscsilun
->allocmap_valid
) {
473 /* if we are under memory pressure free the allocmap as well */
474 iscsi_allocmap_free(iscsilun
);
482 iscsi_allocmap_update(IscsiLun
*iscsilun
, int64_t sector_num
,
483 int nb_sectors
, bool allocated
, bool valid
)
485 int64_t cl_num_expanded
, nb_cls_expanded
, cl_num_shrunk
, nb_cls_shrunk
;
487 if (iscsilun
->allocmap
== NULL
) {
490 /* expand to entirely contain all affected clusters */
491 cl_num_expanded
= sector_num
/ iscsilun
->cluster_sectors
;
492 nb_cls_expanded
= DIV_ROUND_UP(sector_num
+ nb_sectors
,
493 iscsilun
->cluster_sectors
) - cl_num_expanded
;
494 /* shrink to touch only completely contained clusters */
495 cl_num_shrunk
= DIV_ROUND_UP(sector_num
, iscsilun
->cluster_sectors
);
496 nb_cls_shrunk
= (sector_num
+ nb_sectors
) / iscsilun
->cluster_sectors
499 bitmap_set(iscsilun
->allocmap
, cl_num_expanded
, nb_cls_expanded
);
501 bitmap_clear(iscsilun
->allocmap
, cl_num_shrunk
, nb_cls_shrunk
);
504 if (iscsilun
->allocmap_valid
== NULL
) {
508 bitmap_set(iscsilun
->allocmap_valid
, cl_num_shrunk
, nb_cls_shrunk
);
510 bitmap_clear(iscsilun
->allocmap_valid
, cl_num_expanded
,
516 iscsi_allocmap_set_allocated(IscsiLun
*iscsilun
, int64_t sector_num
,
519 iscsi_allocmap_update(iscsilun
, sector_num
, nb_sectors
, true, true);
523 iscsi_allocmap_set_unallocated(IscsiLun
*iscsilun
, int64_t sector_num
,
526 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
527 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
529 iscsi_allocmap_update(iscsilun
, sector_num
, nb_sectors
, false, true);
532 static void iscsi_allocmap_set_invalid(IscsiLun
*iscsilun
, int64_t sector_num
,
535 iscsi_allocmap_update(iscsilun
, sector_num
, nb_sectors
, false, false);
538 static void iscsi_allocmap_invalidate(IscsiLun
*iscsilun
)
540 if (iscsilun
->allocmap
) {
541 bitmap_zero(iscsilun
->allocmap
, iscsilun
->allocmap_size
);
543 if (iscsilun
->allocmap_valid
) {
544 bitmap_zero(iscsilun
->allocmap_valid
, iscsilun
->allocmap_size
);
549 iscsi_allocmap_is_allocated(IscsiLun
*iscsilun
, int64_t sector_num
,
553 if (iscsilun
->allocmap
== NULL
) {
556 size
= DIV_ROUND_UP(sector_num
+ nb_sectors
, iscsilun
->cluster_sectors
);
557 return !(find_next_bit(iscsilun
->allocmap
, size
,
558 sector_num
/ iscsilun
->cluster_sectors
) == size
);
561 static inline bool iscsi_allocmap_is_valid(IscsiLun
*iscsilun
,
562 int64_t sector_num
, int nb_sectors
)
565 if (iscsilun
->allocmap_valid
== NULL
) {
568 size
= DIV_ROUND_UP(sector_num
+ nb_sectors
, iscsilun
->cluster_sectors
);
569 return (find_next_zero_bit(iscsilun
->allocmap_valid
, size
,
570 sector_num
/ iscsilun
->cluster_sectors
) == size
);
573 static int coroutine_fn
574 iscsi_co_writev_flags(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
575 QEMUIOVector
*iov
, int flags
)
577 IscsiLun
*iscsilun
= bs
->opaque
;
578 struct IscsiTask iTask
;
580 uint32_t num_sectors
;
581 bool fua
= flags
& BDRV_REQ_FUA
;
584 assert(iscsilun
->dpofua
);
586 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
590 if (bs
->bl
.max_transfer
) {
591 assert(nb_sectors
<< BDRV_SECTOR_BITS
<= bs
->bl
.max_transfer
);
594 lba
= sector_qemu2lun(sector_num
, iscsilun
);
595 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
596 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
598 if (iscsilun
->use_16_for_rw
) {
599 #if LIBISCSI_API_VERSION >= (20160603)
600 iTask
.task
= iscsi_write16_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
601 NULL
, num_sectors
* iscsilun
->block_size
,
602 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
603 iscsi_co_generic_cb
, &iTask
,
604 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
606 iTask
.task
= iscsi_write10_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
607 NULL
, num_sectors
* iscsilun
->block_size
,
608 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
609 iscsi_co_generic_cb
, &iTask
,
610 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
613 iTask
.task
= iscsi_write16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
614 NULL
, num_sectors
* iscsilun
->block_size
,
615 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
616 iscsi_co_generic_cb
, &iTask
);
618 iTask
.task
= iscsi_write10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
619 NULL
, num_sectors
* iscsilun
->block_size
,
620 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
621 iscsi_co_generic_cb
, &iTask
);
624 if (iTask
.task
== NULL
) {
627 #if LIBISCSI_API_VERSION < (20160603)
628 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
631 while (!iTask
.complete
) {
632 iscsi_set_events(iscsilun
);
633 qemu_coroutine_yield();
636 if (iTask
.task
!= NULL
) {
637 scsi_free_scsi_task(iTask
.task
);
641 if (iTask
.do_retry
) {
646 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
647 iscsi_allocmap_set_invalid(iscsilun
, sector_num
, nb_sectors
);
648 return iTask
.err_code
;
651 iscsi_allocmap_set_allocated(iscsilun
, sector_num
, nb_sectors
);
658 static int64_t coroutine_fn
iscsi_co_get_block_status(BlockDriverState
*bs
,
660 int nb_sectors
, int *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
;
669 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
671 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
676 /* default to all sectors allocated */
677 ret
= BDRV_BLOCK_DATA
;
678 ret
|= (sector_num
<< BDRV_SECTOR_BITS
) | BDRV_BLOCK_OFFSET_VALID
;
681 /* LUN does not support logical block provisioning */
682 if (!iscsilun
->lbpme
) {
687 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
688 sector_qemu2lun(sector_num
, iscsilun
),
689 8 + 16, iscsi_co_generic_cb
,
695 while (!iTask
.complete
) {
696 iscsi_set_events(iscsilun
);
697 qemu_coroutine_yield();
700 if (iTask
.do_retry
) {
701 if (iTask
.task
!= NULL
) {
702 scsi_free_scsi_task(iTask
.task
);
709 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
710 /* in case the get_lba_status_callout fails (i.e.
711 * because the device is busy or the cmd is not
712 * supported) we pretend all blocks are allocated
713 * for backwards compatibility */
717 lbas
= scsi_datain_unmarshall(iTask
.task
);
723 lbasd
= &lbas
->descriptors
[0];
725 if (sector_qemu2lun(sector_num
, iscsilun
) != lbasd
->lba
) {
730 *pnum
= sector_lun2qemu(lbasd
->num_blocks
, iscsilun
);
732 if (lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_DEALLOCATED
||
733 lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_ANCHORED
) {
734 ret
&= ~BDRV_BLOCK_DATA
;
735 if (iscsilun
->lbprz
) {
736 ret
|= BDRV_BLOCK_ZERO
;
740 if (ret
& BDRV_BLOCK_ZERO
) {
741 iscsi_allocmap_set_unallocated(iscsilun
, sector_num
, *pnum
);
743 iscsi_allocmap_set_allocated(iscsilun
, sector_num
, *pnum
);
746 if (*pnum
> nb_sectors
) {
750 if (iTask
.task
!= NULL
) {
751 scsi_free_scsi_task(iTask
.task
);
753 if (ret
> 0 && ret
& BDRV_BLOCK_OFFSET_VALID
) {
759 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
760 int64_t sector_num
, int nb_sectors
,
763 IscsiLun
*iscsilun
= bs
->opaque
;
764 struct IscsiTask iTask
;
766 uint32_t num_sectors
;
768 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
772 if (bs
->bl
.max_transfer
) {
773 assert(nb_sectors
<< BDRV_SECTOR_BITS
<= bs
->bl
.max_transfer
);
776 /* if cache.direct is off and we have a valid entry in our allocation map
777 * we can skip checking the block status and directly return zeroes if
778 * the request falls within an unallocated area */
779 if (iscsi_allocmap_is_valid(iscsilun
, sector_num
, nb_sectors
) &&
780 !iscsi_allocmap_is_allocated(iscsilun
, sector_num
, nb_sectors
)) {
781 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
785 if (nb_sectors
>= ISCSI_CHECKALLOC_THRES
&&
786 !iscsi_allocmap_is_valid(iscsilun
, sector_num
, nb_sectors
) &&
787 !iscsi_allocmap_is_allocated(iscsilun
, sector_num
, nb_sectors
)) {
789 BlockDriverState
*file
;
790 /* check the block status from the beginning of the cluster
791 * containing the start sector */
792 int64_t ret
= iscsi_co_get_block_status(bs
,
793 sector_num
- sector_num
% iscsilun
->cluster_sectors
,
794 BDRV_REQUEST_MAX_SECTORS
, &pnum
, &file
);
798 /* if the whole request falls into an unallocated area we can avoid
799 * to read and directly return zeroes instead */
800 if (ret
& BDRV_BLOCK_ZERO
&&
801 pnum
>= nb_sectors
+ sector_num
% iscsilun
->cluster_sectors
) {
802 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
807 lba
= sector_qemu2lun(sector_num
, iscsilun
);
808 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
810 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
812 if (iscsilun
->use_16_for_rw
) {
813 #if LIBISCSI_API_VERSION >= (20160603)
814 iTask
.task
= iscsi_read16_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
815 num_sectors
* iscsilun
->block_size
,
816 iscsilun
->block_size
, 0, 0, 0, 0, 0,
817 iscsi_co_generic_cb
, &iTask
,
818 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
820 iTask
.task
= iscsi_read10_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
821 num_sectors
* iscsilun
->block_size
,
822 iscsilun
->block_size
,
824 iscsi_co_generic_cb
, &iTask
,
825 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
828 iTask
.task
= iscsi_read16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
829 num_sectors
* iscsilun
->block_size
,
830 iscsilun
->block_size
, 0, 0, 0, 0, 0,
831 iscsi_co_generic_cb
, &iTask
);
833 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
834 num_sectors
* iscsilun
->block_size
,
835 iscsilun
->block_size
,
837 iscsi_co_generic_cb
, &iTask
);
840 if (iTask
.task
== NULL
) {
843 #if LIBISCSI_API_VERSION < (20160603)
844 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
846 while (!iTask
.complete
) {
847 iscsi_set_events(iscsilun
);
848 qemu_coroutine_yield();
851 if (iTask
.task
!= NULL
) {
852 scsi_free_scsi_task(iTask
.task
);
856 if (iTask
.do_retry
) {
861 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
862 return iTask
.err_code
;
868 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
870 IscsiLun
*iscsilun
= bs
->opaque
;
871 struct IscsiTask iTask
;
873 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
875 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
876 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
880 while (!iTask
.complete
) {
881 iscsi_set_events(iscsilun
);
882 qemu_coroutine_yield();
885 if (iTask
.task
!= NULL
) {
886 scsi_free_scsi_task(iTask
.task
);
890 if (iTask
.do_retry
) {
895 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
896 return iTask
.err_code
;
904 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
905 void *command_data
, void *opaque
)
907 IscsiAIOCB
*acb
= opaque
;
914 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
915 iscsi_get_error(iscsi
));
916 acb
->status
= iscsi_translate_sense(&acb
->task
->sense
);
919 acb
->ioh
->driver_status
= 0;
920 acb
->ioh
->host_status
= 0;
922 acb
->ioh
->status
= status
;
924 #define SG_ERR_DRIVER_SENSE 0x08
926 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
929 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
931 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
932 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
933 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
934 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
937 iscsi_schedule_bh(acb
);
940 static void iscsi_ioctl_bh_completion(void *opaque
)
942 IscsiAIOCB
*acb
= opaque
;
944 qemu_bh_delete(acb
->bh
);
945 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
949 static void iscsi_ioctl_handle_emulated(IscsiAIOCB
*acb
, int req
, void *buf
)
951 BlockDriverState
*bs
= acb
->common
.bs
;
952 IscsiLun
*iscsilun
= bs
->opaque
;
956 case SG_GET_VERSION_NUM
:
960 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
966 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
),
967 iscsi_ioctl_bh_completion
, acb
);
969 qemu_bh_schedule(acb
->bh
);
972 static BlockAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
973 unsigned long int req
, void *buf
,
974 BlockCompletionFunc
*cb
, void *opaque
)
976 IscsiLun
*iscsilun
= bs
->opaque
;
977 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
978 struct iscsi_data data
;
981 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
983 acb
->iscsilun
= iscsilun
;
985 acb
->status
= -EINPROGRESS
;
990 iscsi_ioctl_handle_emulated(acb
, req
, buf
);
994 if (acb
->ioh
->cmd_len
> SCSI_CDB_MAX_SIZE
) {
995 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
996 acb
->ioh
->cmd_len
, SCSI_CDB_MAX_SIZE
);
1001 acb
->task
= malloc(sizeof(struct scsi_task
));
1002 if (acb
->task
== NULL
) {
1003 error_report("iSCSI: Failed to allocate task for scsi command. %s",
1004 iscsi_get_error(iscsi
));
1005 qemu_aio_unref(acb
);
1008 memset(acb
->task
, 0, sizeof(struct scsi_task
));
1010 switch (acb
->ioh
->dxfer_direction
) {
1011 case SG_DXFER_TO_DEV
:
1012 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
1014 case SG_DXFER_FROM_DEV
:
1015 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
1018 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
1022 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
1023 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
1024 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
1027 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
1028 if (acb
->ioh
->iovec_count
== 0) {
1029 data
.data
= acb
->ioh
->dxferp
;
1030 data
.size
= acb
->ioh
->dxfer_len
;
1032 scsi_task_set_iov_out(acb
->task
,
1033 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1034 acb
->ioh
->iovec_count
);
1038 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
1040 (data
.size
> 0) ? &data
: NULL
,
1042 scsi_free_scsi_task(acb
->task
);
1043 qemu_aio_unref(acb
);
1047 /* tell libiscsi to read straight into the buffer we got from ioctl */
1048 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
1049 if (acb
->ioh
->iovec_count
== 0) {
1050 scsi_task_add_data_in_buffer(acb
->task
,
1051 acb
->ioh
->dxfer_len
,
1054 scsi_task_set_iov_in(acb
->task
,
1055 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1056 acb
->ioh
->iovec_count
);
1060 iscsi_set_events(iscsilun
);
1062 return &acb
->common
;
1068 iscsi_getlength(BlockDriverState
*bs
)
1070 IscsiLun
*iscsilun
= bs
->opaque
;
1073 len
= iscsilun
->num_blocks
;
1074 len
*= iscsilun
->block_size
;
1080 coroutine_fn
iscsi_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int count
)
1082 IscsiLun
*iscsilun
= bs
->opaque
;
1083 struct IscsiTask iTask
;
1084 struct unmap_list list
;
1086 if (!is_byte_request_lun_aligned(offset
, count
, iscsilun
)) {
1090 if (!iscsilun
->lbp
.lbpu
) {
1091 /* UNMAP is not supported by the target */
1095 list
.lba
= offset
/ iscsilun
->block_size
;
1096 list
.num
= count
/ iscsilun
->block_size
;
1098 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1100 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
1101 iscsi_co_generic_cb
, &iTask
) == NULL
) {
1105 while (!iTask
.complete
) {
1106 iscsi_set_events(iscsilun
);
1107 qemu_coroutine_yield();
1110 if (iTask
.task
!= NULL
) {
1111 scsi_free_scsi_task(iTask
.task
);
1115 if (iTask
.do_retry
) {
1120 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
) {
1121 /* the target might fail with a check condition if it
1122 is not happy with the alignment of the UNMAP request
1123 we silently fail in this case */
1127 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1128 return iTask
.err_code
;
1131 iscsi_allocmap_set_invalid(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1132 count
>> BDRV_SECTOR_BITS
);
1138 coroutine_fn
iscsi_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1139 int count
, BdrvRequestFlags flags
)
1141 IscsiLun
*iscsilun
= bs
->opaque
;
1142 struct IscsiTask iTask
;
1145 bool use_16_for_ws
= iscsilun
->use_16_for_rw
;
1147 if (!is_byte_request_lun_aligned(offset
, count
, iscsilun
)) {
1151 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1152 if (!use_16_for_ws
&& !iscsilun
->lbp
.lbpws10
) {
1153 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1154 use_16_for_ws
= true;
1156 if (use_16_for_ws
&& !iscsilun
->lbp
.lbpws
) {
1157 /* WRITESAME16 with UNMAP is not supported by the target,
1158 * fall back and try WRITESAME10/16 without UNMAP */
1159 flags
&= ~BDRV_REQ_MAY_UNMAP
;
1160 use_16_for_ws
= iscsilun
->use_16_for_rw
;
1164 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
1165 /* WRITESAME without UNMAP is not supported by the target */
1169 lba
= offset
/ iscsilun
->block_size
;
1170 nb_blocks
= count
/ iscsilun
->block_size
;
1172 if (iscsilun
->zeroblock
== NULL
) {
1173 iscsilun
->zeroblock
= g_try_malloc0(iscsilun
->block_size
);
1174 if (iscsilun
->zeroblock
== NULL
) {
1179 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1181 if (use_16_for_ws
) {
1182 iTask
.task
= iscsi_writesame16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1183 iscsilun
->zeroblock
, iscsilun
->block_size
,
1184 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1185 0, 0, iscsi_co_generic_cb
, &iTask
);
1187 iTask
.task
= iscsi_writesame10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1188 iscsilun
->zeroblock
, iscsilun
->block_size
,
1189 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1190 0, 0, iscsi_co_generic_cb
, &iTask
);
1192 if (iTask
.task
== NULL
) {
1196 while (!iTask
.complete
) {
1197 iscsi_set_events(iscsilun
);
1198 qemu_coroutine_yield();
1201 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
&&
1202 iTask
.task
->sense
.key
== SCSI_SENSE_ILLEGAL_REQUEST
&&
1203 (iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
||
1204 iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
)) {
1205 /* WRITE SAME is not supported by the target */
1206 iscsilun
->has_write_same
= false;
1207 scsi_free_scsi_task(iTask
.task
);
1211 if (iTask
.task
!= NULL
) {
1212 scsi_free_scsi_task(iTask
.task
);
1216 if (iTask
.do_retry
) {
1221 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1222 iscsi_allocmap_set_invalid(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1223 count
>> BDRV_SECTOR_BITS
);
1224 return iTask
.err_code
;
1227 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1228 iscsi_allocmap_set_invalid(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1229 count
>> BDRV_SECTOR_BITS
);
1231 iscsi_allocmap_set_allocated(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1232 count
>> BDRV_SECTOR_BITS
);
1238 static void parse_chap(struct iscsi_context
*iscsi
, const char *target
,
1243 const char *user
= NULL
;
1244 const char *password
= NULL
;
1245 const char *secretid
;
1246 char *secret
= NULL
;
1248 list
= qemu_find_opts("iscsi");
1253 opts
= qemu_opts_find(list
, target
);
1255 opts
= QTAILQ_FIRST(&list
->head
);
1261 user
= qemu_opt_get(opts
, "user");
1266 secretid
= qemu_opt_get(opts
, "password-secret");
1267 password
= qemu_opt_get(opts
, "password");
1268 if (secretid
&& password
) {
1269 error_setg(errp
, "'password' and 'password-secret' properties are "
1270 "mutually exclusive");
1274 secret
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
1279 } else if (!password
) {
1280 error_setg(errp
, "CHAP username specified but no password was given");
1284 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
1285 error_setg(errp
, "Failed to set initiator username and password");
1291 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
,
1296 const char *digest
= NULL
;
1298 list
= qemu_find_opts("iscsi");
1303 opts
= qemu_opts_find(list
, target
);
1305 opts
= QTAILQ_FIRST(&list
->head
);
1311 digest
= qemu_opt_get(opts
, "header-digest");
1316 if (!strcmp(digest
, "CRC32C")) {
1317 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
1318 } else if (!strcmp(digest
, "NONE")) {
1319 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
1320 } else if (!strcmp(digest
, "CRC32C-NONE")) {
1321 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
1322 } else if (!strcmp(digest
, "NONE-CRC32C")) {
1323 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1325 error_setg(errp
, "Invalid header-digest setting : %s", digest
);
1329 static char *parse_initiator_name(const char *target
)
1335 UuidInfo
*uuid_info
;
1337 list
= qemu_find_opts("iscsi");
1339 opts
= qemu_opts_find(list
, target
);
1341 opts
= QTAILQ_FIRST(&list
->head
);
1344 name
= qemu_opt_get(opts
, "initiator-name");
1346 return g_strdup(name
);
1351 uuid_info
= qmp_query_uuid(NULL
);
1352 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
1353 name
= qemu_get_vm_name();
1355 name
= uuid_info
->UUID
;
1357 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1358 name
? ":" : "", name
? name
: "");
1359 qapi_free_UuidInfo(uuid_info
);
1363 static int parse_timeout(const char *target
)
1367 const char *timeout
;
1369 list
= qemu_find_opts("iscsi");
1371 opts
= qemu_opts_find(list
, target
);
1373 opts
= QTAILQ_FIRST(&list
->head
);
1376 timeout
= qemu_opt_get(opts
, "timeout");
1378 return atoi(timeout
);
1386 static void iscsi_nop_timed_event(void *opaque
)
1388 IscsiLun
*iscsilun
= opaque
;
1390 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) >= MAX_NOP_FAILURES
) {
1391 error_report("iSCSI: NOP timeout. Reconnecting...");
1392 iscsilun
->request_timed_out
= true;
1393 } else if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
1394 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1398 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1399 iscsi_set_events(iscsilun
);
1402 static void iscsi_readcapacity_sync(IscsiLun
*iscsilun
, Error
**errp
)
1404 struct scsi_task
*task
= NULL
;
1405 struct scsi_readcapacity10
*rc10
= NULL
;
1406 struct scsi_readcapacity16
*rc16
= NULL
;
1407 int retries
= ISCSI_CMD_RETRIES
;
1411 scsi_free_scsi_task(task
);
1415 switch (iscsilun
->type
) {
1417 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
1418 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1419 rc16
= scsi_datain_unmarshall(task
);
1421 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity16 data.");
1423 iscsilun
->block_size
= rc16
->block_length
;
1424 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1425 iscsilun
->lbpme
= !!rc16
->lbpme
;
1426 iscsilun
->lbprz
= !!rc16
->lbprz
;
1427 iscsilun
->use_16_for_rw
= (rc16
->returned_lba
> 0xffffffff);
1431 if (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1432 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
1435 /* Fall through and try READ CAPACITY(10) instead. */
1437 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1438 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1439 rc10
= scsi_datain_unmarshall(task
);
1441 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity10 data.");
1443 iscsilun
->block_size
= rc10
->block_size
;
1444 if (rc10
->lba
== 0) {
1445 /* blank disk loaded */
1446 iscsilun
->num_blocks
= 0;
1448 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1456 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1457 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1460 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1461 error_setg(errp
, "iSCSI: failed to send readcapacity10/16 command");
1462 } else if (!iscsilun
->block_size
||
1463 iscsilun
->block_size
% BDRV_SECTOR_SIZE
) {
1464 error_setg(errp
, "iSCSI: the target returned an invalid "
1465 "block size of %d.", iscsilun
->block_size
);
1468 scsi_free_scsi_task(task
);
1472 /* TODO Convert to fine grained options */
1473 static QemuOptsList runtime_opts
= {
1475 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1479 .type
= QEMU_OPT_STRING
,
1480 .help
= "URL to the iscsi image",
1482 { /* end of list */ }
1486 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
, int lun
,
1487 int evpd
, int pc
, void **inq
, Error
**errp
)
1490 struct scsi_task
*task
= NULL
;
1491 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, 64);
1492 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1495 full_size
= scsi_datain_getfullsize(task
);
1496 if (full_size
> task
->datain
.size
) {
1497 scsi_free_scsi_task(task
);
1499 /* we need more data for the full list */
1500 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, full_size
);
1501 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1506 *inq
= scsi_datain_unmarshall(task
);
1508 error_setg(errp
, "iSCSI: failed to unmarshall inquiry datain blob");
1515 error_setg(errp
, "iSCSI: Inquiry command failed : %s",
1516 iscsi_get_error(iscsi
));
1519 scsi_free_scsi_task(task
);
1524 static void iscsi_detach_aio_context(BlockDriverState
*bs
)
1526 IscsiLun
*iscsilun
= bs
->opaque
;
1528 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsilun
->iscsi
),
1529 false, NULL
, NULL
, NULL
);
1530 iscsilun
->events
= 0;
1532 if (iscsilun
->nop_timer
) {
1533 timer_del(iscsilun
->nop_timer
);
1534 timer_free(iscsilun
->nop_timer
);
1535 iscsilun
->nop_timer
= NULL
;
1537 if (iscsilun
->event_timer
) {
1538 timer_del(iscsilun
->event_timer
);
1539 timer_free(iscsilun
->event_timer
);
1540 iscsilun
->event_timer
= NULL
;
1544 static void iscsi_attach_aio_context(BlockDriverState
*bs
,
1545 AioContext
*new_context
)
1547 IscsiLun
*iscsilun
= bs
->opaque
;
1549 iscsilun
->aio_context
= new_context
;
1550 iscsi_set_events(iscsilun
);
1552 /* Set up a timer for sending out iSCSI NOPs */
1553 iscsilun
->nop_timer
= aio_timer_new(iscsilun
->aio_context
,
1554 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1555 iscsi_nop_timed_event
, iscsilun
);
1556 timer_mod(iscsilun
->nop_timer
,
1557 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1559 /* Set up a timer for periodic calls to iscsi_set_events and to
1560 * scan for command timeout */
1561 iscsilun
->event_timer
= aio_timer_new(iscsilun
->aio_context
,
1562 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1563 iscsi_timed_check_events
, iscsilun
);
1564 timer_mod(iscsilun
->event_timer
,
1565 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
1568 static void iscsi_modesense_sync(IscsiLun
*iscsilun
)
1570 struct scsi_task
*task
;
1571 struct scsi_mode_sense
*ms
= NULL
;
1572 iscsilun
->write_protected
= false;
1573 iscsilun
->dpofua
= false;
1575 task
= iscsi_modesense6_sync(iscsilun
->iscsi
, iscsilun
->lun
,
1576 1, SCSI_MODESENSE_PC_CURRENT
,
1579 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1580 iscsi_get_error(iscsilun
->iscsi
));
1584 if (task
->status
!= SCSI_STATUS_GOOD
) {
1585 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1588 ms
= scsi_datain_unmarshall(task
);
1590 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1591 iscsi_get_error(iscsilun
->iscsi
));
1594 iscsilun
->write_protected
= ms
->device_specific_parameter
& 0x80;
1595 iscsilun
->dpofua
= ms
->device_specific_parameter
& 0x10;
1599 scsi_free_scsi_task(task
);
1604 * We support iscsi url's on the form
1605 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1607 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1610 IscsiLun
*iscsilun
= bs
->opaque
;
1611 struct iscsi_context
*iscsi
= NULL
;
1612 struct iscsi_url
*iscsi_url
= NULL
;
1613 struct scsi_task
*task
= NULL
;
1614 struct scsi_inquiry_standard
*inq
= NULL
;
1615 struct scsi_inquiry_supported_pages
*inq_vpd
;
1616 char *initiator_name
= NULL
;
1618 Error
*local_err
= NULL
;
1619 const char *filename
;
1620 int i
, ret
= 0, timeout
= 0;
1622 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1623 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1625 error_propagate(errp
, local_err
);
1630 filename
= qemu_opt_get(opts
, "filename");
1632 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1633 if (iscsi_url
== NULL
) {
1634 error_setg(errp
, "Failed to parse URL : %s", filename
);
1639 memset(iscsilun
, 0, sizeof(IscsiLun
));
1641 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1643 iscsi
= iscsi_create_context(initiator_name
);
1644 if (iscsi
== NULL
) {
1645 error_setg(errp
, "iSCSI: Failed to create iSCSI context.");
1649 #if LIBISCSI_API_VERSION >= (20160603)
1650 if (iscsi_init_transport(iscsi
, iscsi_url
->transport
)) {
1651 error_setg(errp
, ("Error initializing transport."));
1656 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1657 error_setg(errp
, "iSCSI: Failed to set target name.");
1662 if (iscsi_url
->user
[0] != '\0') {
1663 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1666 error_setg(errp
, "Failed to set initiator username and password");
1672 /* check if we got CHAP username/password via the options */
1673 parse_chap(iscsi
, iscsi_url
->target
, &local_err
);
1674 if (local_err
!= NULL
) {
1675 error_propagate(errp
, local_err
);
1680 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1681 error_setg(errp
, "iSCSI: Failed to set session type to normal.");
1686 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1688 /* check if we got HEADER_DIGEST via the options */
1689 parse_header_digest(iscsi
, iscsi_url
->target
, &local_err
);
1690 if (local_err
!= NULL
) {
1691 error_propagate(errp
, local_err
);
1696 /* timeout handling is broken in libiscsi before 1.15.0 */
1697 timeout
= parse_timeout(iscsi_url
->target
);
1698 #if LIBISCSI_API_VERSION >= 20150621
1699 iscsi_set_timeout(iscsi
, timeout
);
1702 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1706 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1707 error_setg(errp
, "iSCSI: Failed to connect to LUN : %s",
1708 iscsi_get_error(iscsi
));
1713 iscsilun
->iscsi
= iscsi
;
1714 iscsilun
->aio_context
= bdrv_get_aio_context(bs
);
1715 iscsilun
->lun
= iscsi_url
->lun
;
1716 iscsilun
->has_write_same
= true;
1718 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0,
1719 (void **) &inq
, errp
);
1724 iscsilun
->type
= inq
->periperal_device_type
;
1725 scsi_free_scsi_task(task
);
1728 iscsi_modesense_sync(iscsilun
);
1729 if (iscsilun
->dpofua
) {
1730 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1732 bs
->supported_zero_flags
= BDRV_REQ_MAY_UNMAP
;
1734 /* Check the write protect flag of the LUN if we want to write */
1735 if (iscsilun
->type
== TYPE_DISK
&& (flags
& BDRV_O_RDWR
) &&
1736 iscsilun
->write_protected
) {
1737 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1742 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1743 if (local_err
!= NULL
) {
1744 error_propagate(errp
, local_err
);
1748 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1750 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1751 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1752 * will try to read from the device to guess the image format.
1754 if (iscsilun
->type
!= TYPE_DISK
&& iscsilun
->type
!= TYPE_ROM
) {
1758 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1759 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES
,
1760 (void **) &inq_vpd
, errp
);
1765 for (i
= 0; i
< inq_vpd
->num_pages
; i
++) {
1766 struct scsi_task
*inq_task
;
1767 struct scsi_inquiry_logical_block_provisioning
*inq_lbp
;
1768 struct scsi_inquiry_block_limits
*inq_bl
;
1769 switch (inq_vpd
->pages
[i
]) {
1770 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
:
1771 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1772 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
,
1773 (void **) &inq_lbp
, errp
);
1774 if (inq_task
== NULL
) {
1778 memcpy(&iscsilun
->lbp
, inq_lbp
,
1779 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1780 scsi_free_scsi_task(inq_task
);
1782 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
:
1783 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1784 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
,
1785 (void **) &inq_bl
, errp
);
1786 if (inq_task
== NULL
) {
1790 memcpy(&iscsilun
->bl
, inq_bl
,
1791 sizeof(struct scsi_inquiry_block_limits
));
1792 scsi_free_scsi_task(inq_task
);
1798 scsi_free_scsi_task(task
);
1801 iscsi_attach_aio_context(bs
, iscsilun
->aio_context
);
1803 /* Guess the internal cluster (page) size of the iscsi target by the means
1804 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1805 * reasonable size */
1806 if (iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
>= 4 * 1024 &&
1807 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
<= 16 * 1024 * 1024) {
1808 iscsilun
->cluster_sectors
= (iscsilun
->bl
.opt_unmap_gran
*
1809 iscsilun
->block_size
) >> BDRV_SECTOR_BITS
;
1810 if (iscsilun
->lbprz
) {
1811 ret
= iscsi_allocmap_init(iscsilun
, bs
->open_flags
);
1816 qemu_opts_del(opts
);
1817 g_free(initiator_name
);
1818 if (iscsi_url
!= NULL
) {
1819 iscsi_destroy_url(iscsi_url
);
1822 scsi_free_scsi_task(task
);
1826 if (iscsi
!= NULL
) {
1827 if (iscsi_is_logged_in(iscsi
)) {
1828 iscsi_logout_sync(iscsi
);
1830 iscsi_destroy_context(iscsi
);
1832 memset(iscsilun
, 0, sizeof(IscsiLun
));
1837 static void iscsi_close(BlockDriverState
*bs
)
1839 IscsiLun
*iscsilun
= bs
->opaque
;
1840 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1842 iscsi_detach_aio_context(bs
);
1843 if (iscsi_is_logged_in(iscsi
)) {
1844 iscsi_logout_sync(iscsi
);
1846 iscsi_destroy_context(iscsi
);
1847 g_free(iscsilun
->zeroblock
);
1848 iscsi_allocmap_free(iscsilun
);
1849 memset(iscsilun
, 0, sizeof(IscsiLun
));
1852 static void iscsi_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1854 /* We don't actually refresh here, but just return data queried in
1855 * iscsi_open(): iscsi targets don't change their limits. */
1857 IscsiLun
*iscsilun
= bs
->opaque
;
1858 uint64_t max_xfer_len
= iscsilun
->use_16_for_rw
? 0xffffffff : 0xffff;
1859 unsigned int block_size
= MAX(BDRV_SECTOR_SIZE
, iscsilun
->block_size
);
1861 assert(iscsilun
->block_size
>= BDRV_SECTOR_SIZE
|| bs
->sg
);
1863 bs
->bl
.request_alignment
= block_size
;
1865 if (iscsilun
->bl
.max_xfer_len
) {
1866 max_xfer_len
= MIN(max_xfer_len
, iscsilun
->bl
.max_xfer_len
);
1869 if (max_xfer_len
* block_size
< INT_MAX
) {
1870 bs
->bl
.max_transfer
= max_xfer_len
* iscsilun
->block_size
;
1873 if (iscsilun
->lbp
.lbpu
) {
1874 if (iscsilun
->bl
.max_unmap
< 0xffffffff / block_size
) {
1875 bs
->bl
.max_pdiscard
=
1876 iscsilun
->bl
.max_unmap
* iscsilun
->block_size
;
1878 bs
->bl
.pdiscard_alignment
=
1879 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
1881 bs
->bl
.pdiscard_alignment
= iscsilun
->block_size
;
1884 if (iscsilun
->bl
.max_ws_len
< 0xffffffff / block_size
) {
1885 bs
->bl
.max_pwrite_zeroes
=
1886 iscsilun
->bl
.max_ws_len
* iscsilun
->block_size
;
1888 if (iscsilun
->lbp
.lbpws
) {
1889 bs
->bl
.pwrite_zeroes_alignment
=
1890 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
1892 bs
->bl
.pwrite_zeroes_alignment
= iscsilun
->block_size
;
1894 if (iscsilun
->bl
.opt_xfer_len
&&
1895 iscsilun
->bl
.opt_xfer_len
< INT_MAX
/ block_size
) {
1896 bs
->bl
.opt_transfer
= pow2floor(iscsilun
->bl
.opt_xfer_len
*
1897 iscsilun
->block_size
);
1901 /* Note that this will not re-establish a connection with an iSCSI target - it
1902 * is effectively a NOP. */
1903 static int iscsi_reopen_prepare(BDRVReopenState
*state
,
1904 BlockReopenQueue
*queue
, Error
**errp
)
1906 IscsiLun
*iscsilun
= state
->bs
->opaque
;
1908 if (state
->flags
& BDRV_O_RDWR
&& iscsilun
->write_protected
) {
1909 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1915 static void iscsi_reopen_commit(BDRVReopenState
*reopen_state
)
1917 IscsiLun
*iscsilun
= reopen_state
->bs
->opaque
;
1919 /* the cache.direct status might have changed */
1920 if (iscsilun
->allocmap
!= NULL
) {
1921 iscsi_allocmap_init(iscsilun
, reopen_state
->flags
);
1925 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1927 IscsiLun
*iscsilun
= bs
->opaque
;
1928 Error
*local_err
= NULL
;
1930 if (iscsilun
->type
!= TYPE_DISK
) {
1934 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1935 if (local_err
!= NULL
) {
1936 error_free(local_err
);
1940 if (offset
> iscsi_getlength(bs
)) {
1944 if (iscsilun
->allocmap
!= NULL
) {
1945 iscsi_allocmap_init(iscsilun
, bs
->open_flags
);
1951 static int iscsi_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1954 int64_t total_size
= 0;
1955 BlockDriverState
*bs
;
1956 IscsiLun
*iscsilun
= NULL
;
1961 /* Read out options */
1962 total_size
= DIV_ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1964 bs
->opaque
= g_new0(struct IscsiLun
, 1);
1965 iscsilun
= bs
->opaque
;
1967 bs_options
= qdict_new();
1968 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1969 ret
= iscsi_open(bs
, bs_options
, 0, NULL
);
1970 QDECREF(bs_options
);
1975 iscsi_detach_aio_context(bs
);
1976 if (iscsilun
->type
!= TYPE_DISK
) {
1980 if (bs
->total_sectors
< total_size
) {
1987 if (iscsilun
->iscsi
!= NULL
) {
1988 iscsi_destroy_context(iscsilun
->iscsi
);
1996 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1998 IscsiLun
*iscsilun
= bs
->opaque
;
1999 bdi
->unallocated_blocks_are_zero
= iscsilun
->lbprz
;
2000 bdi
->can_write_zeroes_with_unmap
= iscsilun
->lbprz
&& iscsilun
->lbp
.lbpws
;
2001 bdi
->cluster_size
= iscsilun
->cluster_sectors
* BDRV_SECTOR_SIZE
;
2005 static void iscsi_invalidate_cache(BlockDriverState
*bs
,
2008 IscsiLun
*iscsilun
= bs
->opaque
;
2009 iscsi_allocmap_invalidate(iscsilun
);
2012 static QemuOptsList iscsi_create_opts
= {
2013 .name
= "iscsi-create-opts",
2014 .head
= QTAILQ_HEAD_INITIALIZER(iscsi_create_opts
.head
),
2017 .name
= BLOCK_OPT_SIZE
,
2018 .type
= QEMU_OPT_SIZE
,
2019 .help
= "Virtual disk size"
2021 { /* end of list */ }
2025 static BlockDriver bdrv_iscsi
= {
2026 .format_name
= "iscsi",
2027 .protocol_name
= "iscsi",
2029 .instance_size
= sizeof(IscsiLun
),
2030 .bdrv_needs_filename
= true,
2031 .bdrv_file_open
= iscsi_open
,
2032 .bdrv_close
= iscsi_close
,
2033 .bdrv_create
= iscsi_create
,
2034 .create_opts
= &iscsi_create_opts
,
2035 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
2036 .bdrv_reopen_commit
= iscsi_reopen_commit
,
2037 .bdrv_invalidate_cache
= iscsi_invalidate_cache
,
2039 .bdrv_getlength
= iscsi_getlength
,
2040 .bdrv_get_info
= iscsi_get_info
,
2041 .bdrv_truncate
= iscsi_truncate
,
2042 .bdrv_refresh_limits
= iscsi_refresh_limits
,
2044 .bdrv_co_get_block_status
= iscsi_co_get_block_status
,
2045 .bdrv_co_pdiscard
= iscsi_co_pdiscard
,
2046 .bdrv_co_pwrite_zeroes
= iscsi_co_pwrite_zeroes
,
2047 .bdrv_co_readv
= iscsi_co_readv
,
2048 .bdrv_co_writev_flags
= iscsi_co_writev_flags
,
2049 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
2052 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
2055 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
2056 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
2059 #if LIBISCSI_API_VERSION >= (20160603)
2060 static BlockDriver bdrv_iser
= {
2061 .format_name
= "iser",
2062 .protocol_name
= "iser",
2064 .instance_size
= sizeof(IscsiLun
),
2065 .bdrv_needs_filename
= true,
2066 .bdrv_file_open
= iscsi_open
,
2067 .bdrv_close
= iscsi_close
,
2068 .bdrv_create
= iscsi_create
,
2069 .create_opts
= &iscsi_create_opts
,
2070 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
2071 .bdrv_reopen_commit
= iscsi_reopen_commit
,
2072 .bdrv_invalidate_cache
= iscsi_invalidate_cache
,
2074 .bdrv_getlength
= iscsi_getlength
,
2075 .bdrv_get_info
= iscsi_get_info
,
2076 .bdrv_truncate
= iscsi_truncate
,
2077 .bdrv_refresh_limits
= iscsi_refresh_limits
,
2079 .bdrv_co_get_block_status
= iscsi_co_get_block_status
,
2080 .bdrv_co_pdiscard
= iscsi_co_pdiscard
,
2081 .bdrv_co_pwrite_zeroes
= iscsi_co_pwrite_zeroes
,
2082 .bdrv_co_readv
= iscsi_co_readv
,
2083 .bdrv_co_writev_flags
= iscsi_co_writev_flags
,
2084 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
2087 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
2090 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
2091 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
2095 static void iscsi_block_init(void)
2097 bdrv_register(&bdrv_iscsi
);
2098 #if LIBISCSI_API_VERSION >= (20160603)
2099 bdrv_register(&bdrv_iser
);
2103 block_init(iscsi_block_init
);