2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2015 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>
49 #include <block/scsi.h>
52 typedef struct IscsiLun
{
53 struct iscsi_context
*iscsi
;
54 AioContext
*aio_context
;
56 enum scsi_inquiry_peripheral_device_type type
;
61 QEMUTimer
*event_timer
;
62 struct scsi_inquiry_logical_block_provisioning lbp
;
63 struct scsi_inquiry_block_limits bl
;
64 unsigned char *zeroblock
;
65 unsigned long *allocationmap
;
73 bool request_timed_out
;
76 typedef struct IscsiTask
{
81 struct scsi_task
*task
;
85 QEMUTimer retry_timer
;
89 typedef struct IscsiAIOCB
{
94 struct scsi_task
*task
;
105 /* libiscsi uses time_t so its enough to process events every second */
106 #define EVENT_INTERVAL 1000
107 #define NOP_INTERVAL 5000
108 #define MAX_NOP_FAILURES 3
109 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
110 static const unsigned iscsi_retry_times
[] = {8, 32, 128, 512, 2048, 8192, 32768};
112 /* this threshold is a trade-off knob to choose between
113 * the potential additional overhead of an extra GET_LBA_STATUS request
114 * vs. unnecessarily reading a lot of zero sectors over the wire.
115 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
116 * sectors we check the allocation status of the area covered by the
117 * request first if the allocationmap indicates that the area might be
119 #define ISCSI_CHECKALLOC_THRES 64
126 qemu_bh_delete(acb
->bh
);
131 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
133 if (acb
->task
!= NULL
) {
134 scsi_free_scsi_task(acb
->task
);
142 iscsi_schedule_bh(IscsiAIOCB
*acb
)
147 acb
->bh
= aio_bh_new(acb
->iscsilun
->aio_context
, iscsi_bh_cb
, acb
);
148 qemu_bh_schedule(acb
->bh
);
151 static void iscsi_co_generic_bh_cb(void *opaque
)
153 struct IscsiTask
*iTask
= opaque
;
155 qemu_bh_delete(iTask
->bh
);
156 qemu_coroutine_enter(iTask
->co
, NULL
);
159 static void iscsi_retry_timer_expired(void *opaque
)
161 struct IscsiTask
*iTask
= opaque
;
164 qemu_coroutine_enter(iTask
->co
, NULL
);
168 static inline unsigned exp_random(double mean
)
170 return -mean
* log((double)rand() / RAND_MAX
);
173 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
174 * libiscsi 1.10.0, together with other constants we need. Use it as
175 * a hint that we have to define them ourselves if needed, to keep the
176 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
177 * the test because SCSI_STATUS_* is an enum.
179 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
180 * an enum, check against the LIBISCSI_API_VERSION macro, which was
181 * introduced in 1.11.0. If it is present, there is no need to define
184 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
185 !defined(LIBISCSI_API_VERSION)
186 #define SCSI_STATUS_TASK_SET_FULL 0x28
187 #define SCSI_STATUS_TIMEOUT 0x0f000002
188 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
189 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
192 static int iscsi_translate_sense(struct scsi_sense
*sense
)
196 switch (sense
->key
) {
197 case SCSI_SENSE_NOT_READY
:
199 case SCSI_SENSE_DATA_PROTECTION
:
201 case SCSI_SENSE_COMMAND_ABORTED
:
203 case SCSI_SENSE_ILLEGAL_REQUEST
:
209 switch (sense
->ascq
) {
210 case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR
:
211 case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
:
212 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
:
213 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST
:
216 case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE
:
219 case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED
:
222 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT
:
223 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED
:
224 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN
:
227 case SCSI_SENSE_ASCQ_WRITE_PROTECTED
:
238 iscsi_co_generic_cb(struct iscsi_context
*iscsi
, int status
,
239 void *command_data
, void *opaque
)
241 struct IscsiTask
*iTask
= opaque
;
242 struct scsi_task
*task
= command_data
;
244 iTask
->status
= status
;
248 if (status
!= SCSI_STATUS_GOOD
) {
249 if (iTask
->retries
++ < ISCSI_CMD_RETRIES
) {
250 if (status
== SCSI_STATUS_CHECK_CONDITION
251 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
252 error_report("iSCSI CheckCondition: %s",
253 iscsi_get_error(iscsi
));
257 if (status
== SCSI_STATUS_BUSY
||
258 status
== SCSI_STATUS_TIMEOUT
||
259 status
== SCSI_STATUS_TASK_SET_FULL
) {
260 unsigned retry_time
=
261 exp_random(iscsi_retry_times
[iTask
->retries
- 1]);
262 if (status
== SCSI_STATUS_TIMEOUT
) {
263 /* make sure the request is rescheduled AFTER the
264 * reconnect is initiated */
265 retry_time
= EVENT_INTERVAL
* 2;
266 iTask
->iscsilun
->request_timed_out
= true;
268 error_report("iSCSI Busy/TaskSetFull/TimeOut"
269 " (retry #%u in %u ms): %s",
270 iTask
->retries
, retry_time
,
271 iscsi_get_error(iscsi
));
272 aio_timer_init(iTask
->iscsilun
->aio_context
,
273 &iTask
->retry_timer
, QEMU_CLOCK_REALTIME
,
274 SCALE_MS
, iscsi_retry_timer_expired
, iTask
);
275 timer_mod(&iTask
->retry_timer
,
276 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + retry_time
);
281 iTask
->err_code
= iscsi_translate_sense(&task
->sense
);
282 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi
));
287 iTask
->bh
= aio_bh_new(iTask
->iscsilun
->aio_context
,
288 iscsi_co_generic_bh_cb
, iTask
);
289 qemu_bh_schedule(iTask
->bh
);
295 static void iscsi_co_init_iscsitask(IscsiLun
*iscsilun
, struct IscsiTask
*iTask
)
297 *iTask
= (struct IscsiTask
) {
298 .co
= qemu_coroutine_self(),
299 .iscsilun
= iscsilun
,
304 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
307 IscsiAIOCB
*acb
= private_data
;
309 acb
->status
= -ECANCELED
;
310 iscsi_schedule_bh(acb
);
314 iscsi_aio_cancel(BlockAIOCB
*blockacb
)
316 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
317 IscsiLun
*iscsilun
= acb
->iscsilun
;
319 if (acb
->status
!= -EINPROGRESS
) {
323 /* send a task mgmt call to the target to cancel the task on the target */
324 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
325 iscsi_abort_task_cb
, acb
);
329 static const AIOCBInfo iscsi_aiocb_info
= {
330 .aiocb_size
= sizeof(IscsiAIOCB
),
331 .cancel_async
= iscsi_aio_cancel
,
335 static void iscsi_process_read(void *arg
);
336 static void iscsi_process_write(void *arg
);
339 iscsi_set_events(IscsiLun
*iscsilun
)
341 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
342 int ev
= iscsi_which_events(iscsi
);
344 if (ev
!= iscsilun
->events
) {
345 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsi
),
347 (ev
& POLLIN
) ? iscsi_process_read
: NULL
,
348 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
350 iscsilun
->events
= ev
;
354 static void iscsi_timed_check_events(void *opaque
)
356 IscsiLun
*iscsilun
= opaque
;
358 /* check for timed out requests */
359 iscsi_service(iscsilun
->iscsi
, 0);
361 if (iscsilun
->request_timed_out
) {
362 iscsilun
->request_timed_out
= false;
363 iscsi_reconnect(iscsilun
->iscsi
);
366 /* newer versions of libiscsi may return zero events. Ensure we are able
367 * to return to service once this situation changes. */
368 iscsi_set_events(iscsilun
);
370 timer_mod(iscsilun
->event_timer
,
371 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
375 iscsi_process_read(void *arg
)
377 IscsiLun
*iscsilun
= arg
;
378 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
380 iscsi_service(iscsi
, POLLIN
);
381 iscsi_set_events(iscsilun
);
385 iscsi_process_write(void *arg
)
387 IscsiLun
*iscsilun
= arg
;
388 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
390 iscsi_service(iscsi
, POLLOUT
);
391 iscsi_set_events(iscsilun
);
394 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
396 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
399 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
401 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
404 static bool is_byte_request_lun_aligned(int64_t offset
, int count
,
407 if (offset
% iscsilun
->block_size
|| count
% iscsilun
->block_size
) {
408 error_report("iSCSI misaligned request: "
409 "iscsilun->block_size %u, offset %" PRIi64
411 iscsilun
->block_size
, offset
, count
);
417 static bool is_sector_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
420 assert(nb_sectors
<= BDRV_REQUEST_MAX_SECTORS
);
421 return is_byte_request_lun_aligned(sector_num
<< BDRV_SECTOR_BITS
,
422 nb_sectors
<< BDRV_SECTOR_BITS
,
426 static unsigned long *iscsi_allocationmap_init(IscsiLun
*iscsilun
)
428 return bitmap_try_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun
->num_blocks
,
430 iscsilun
->cluster_sectors
));
433 static void iscsi_allocationmap_set(IscsiLun
*iscsilun
, int64_t sector_num
,
436 if (iscsilun
->allocationmap
== NULL
) {
439 bitmap_set(iscsilun
->allocationmap
,
440 sector_num
/ iscsilun
->cluster_sectors
,
441 DIV_ROUND_UP(nb_sectors
, iscsilun
->cluster_sectors
));
444 static void iscsi_allocationmap_clear(IscsiLun
*iscsilun
, int64_t sector_num
,
447 int64_t cluster_num
, nb_clusters
;
448 if (iscsilun
->allocationmap
== NULL
) {
451 cluster_num
= DIV_ROUND_UP(sector_num
, iscsilun
->cluster_sectors
);
452 nb_clusters
= (sector_num
+ nb_sectors
) / iscsilun
->cluster_sectors
454 if (nb_clusters
> 0) {
455 bitmap_clear(iscsilun
->allocationmap
, cluster_num
, nb_clusters
);
459 static int coroutine_fn
460 iscsi_co_writev_flags(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
461 QEMUIOVector
*iov
, int flags
)
463 IscsiLun
*iscsilun
= bs
->opaque
;
464 struct IscsiTask iTask
;
466 uint32_t num_sectors
;
467 bool fua
= flags
& BDRV_REQ_FUA
;
470 assert(iscsilun
->dpofua
);
472 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
476 if (bs
->bl
.max_transfer_length
&& nb_sectors
> bs
->bl
.max_transfer_length
) {
477 error_report("iSCSI Error: Write of %d sectors exceeds max_xfer_len "
478 "of %d sectors", nb_sectors
, bs
->bl
.max_transfer_length
);
482 lba
= sector_qemu2lun(sector_num
, iscsilun
);
483 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
484 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
486 if (iscsilun
->use_16_for_rw
) {
487 iTask
.task
= iscsi_write16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
488 NULL
, num_sectors
* iscsilun
->block_size
,
489 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
490 iscsi_co_generic_cb
, &iTask
);
492 iTask
.task
= iscsi_write10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
493 NULL
, num_sectors
* iscsilun
->block_size
,
494 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
495 iscsi_co_generic_cb
, &iTask
);
497 if (iTask
.task
== NULL
) {
500 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
502 while (!iTask
.complete
) {
503 iscsi_set_events(iscsilun
);
504 qemu_coroutine_yield();
507 if (iTask
.task
!= NULL
) {
508 scsi_free_scsi_task(iTask
.task
);
512 if (iTask
.do_retry
) {
517 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
518 return iTask
.err_code
;
521 iscsi_allocationmap_set(iscsilun
, sector_num
, nb_sectors
);
527 static bool iscsi_allocationmap_is_allocated(IscsiLun
*iscsilun
,
528 int64_t sector_num
, int nb_sectors
)
531 if (iscsilun
->allocationmap
== NULL
) {
534 size
= DIV_ROUND_UP(sector_num
+ nb_sectors
, iscsilun
->cluster_sectors
);
535 return !(find_next_bit(iscsilun
->allocationmap
, size
,
536 sector_num
/ iscsilun
->cluster_sectors
) == size
);
539 static int64_t coroutine_fn
iscsi_co_get_block_status(BlockDriverState
*bs
,
541 int nb_sectors
, int *pnum
,
542 BlockDriverState
**file
)
544 IscsiLun
*iscsilun
= bs
->opaque
;
545 struct scsi_get_lba_status
*lbas
= NULL
;
546 struct scsi_lba_status_descriptor
*lbasd
= NULL
;
547 struct IscsiTask iTask
;
550 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
552 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
557 /* default to all sectors allocated */
558 ret
= BDRV_BLOCK_DATA
;
559 ret
|= (sector_num
<< BDRV_SECTOR_BITS
) | BDRV_BLOCK_OFFSET_VALID
;
562 /* LUN does not support logical block provisioning */
563 if (!iscsilun
->lbpme
) {
568 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
569 sector_qemu2lun(sector_num
, iscsilun
),
570 8 + 16, iscsi_co_generic_cb
,
576 while (!iTask
.complete
) {
577 iscsi_set_events(iscsilun
);
578 qemu_coroutine_yield();
581 if (iTask
.do_retry
) {
582 if (iTask
.task
!= NULL
) {
583 scsi_free_scsi_task(iTask
.task
);
590 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
591 /* in case the get_lba_status_callout fails (i.e.
592 * because the device is busy or the cmd is not
593 * supported) we pretend all blocks are allocated
594 * for backwards compatibility */
598 lbas
= scsi_datain_unmarshall(iTask
.task
);
604 lbasd
= &lbas
->descriptors
[0];
606 if (sector_qemu2lun(sector_num
, iscsilun
) != lbasd
->lba
) {
611 *pnum
= sector_lun2qemu(lbasd
->num_blocks
, iscsilun
);
613 if (lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_DEALLOCATED
||
614 lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_ANCHORED
) {
615 ret
&= ~BDRV_BLOCK_DATA
;
616 if (iscsilun
->lbprz
) {
617 ret
|= BDRV_BLOCK_ZERO
;
621 if (ret
& BDRV_BLOCK_ZERO
) {
622 iscsi_allocationmap_clear(iscsilun
, sector_num
, *pnum
);
624 iscsi_allocationmap_set(iscsilun
, sector_num
, *pnum
);
627 if (*pnum
> nb_sectors
) {
631 if (iTask
.task
!= NULL
) {
632 scsi_free_scsi_task(iTask
.task
);
634 if (ret
> 0 && ret
& BDRV_BLOCK_OFFSET_VALID
) {
640 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
641 int64_t sector_num
, int nb_sectors
,
644 IscsiLun
*iscsilun
= bs
->opaque
;
645 struct IscsiTask iTask
;
647 uint32_t num_sectors
;
649 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
653 if (bs
->bl
.max_transfer_length
&& nb_sectors
> bs
->bl
.max_transfer_length
) {
654 error_report("iSCSI Error: Read of %d sectors exceeds max_xfer_len "
655 "of %d sectors", nb_sectors
, bs
->bl
.max_transfer_length
);
659 if (iscsilun
->lbprz
&& nb_sectors
>= ISCSI_CHECKALLOC_THRES
&&
660 !iscsi_allocationmap_is_allocated(iscsilun
, sector_num
, nb_sectors
)) {
663 BlockDriverState
*file
;
664 ret
= iscsi_co_get_block_status(bs
, sector_num
,
665 BDRV_REQUEST_MAX_SECTORS
, &pnum
, &file
);
669 if (ret
& BDRV_BLOCK_ZERO
&& pnum
>= nb_sectors
) {
670 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
675 lba
= sector_qemu2lun(sector_num
, iscsilun
);
676 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
678 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
680 if (iscsilun
->use_16_for_rw
) {
681 iTask
.task
= iscsi_read16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
682 num_sectors
* iscsilun
->block_size
,
683 iscsilun
->block_size
, 0, 0, 0, 0, 0,
684 iscsi_co_generic_cb
, &iTask
);
686 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
687 num_sectors
* iscsilun
->block_size
,
688 iscsilun
->block_size
,
690 iscsi_co_generic_cb
, &iTask
);
692 if (iTask
.task
== NULL
) {
695 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
697 while (!iTask
.complete
) {
698 iscsi_set_events(iscsilun
);
699 qemu_coroutine_yield();
702 if (iTask
.task
!= NULL
) {
703 scsi_free_scsi_task(iTask
.task
);
707 if (iTask
.do_retry
) {
712 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
713 return iTask
.err_code
;
719 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
721 IscsiLun
*iscsilun
= bs
->opaque
;
722 struct IscsiTask iTask
;
724 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
726 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
727 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
731 while (!iTask
.complete
) {
732 iscsi_set_events(iscsilun
);
733 qemu_coroutine_yield();
736 if (iTask
.task
!= NULL
) {
737 scsi_free_scsi_task(iTask
.task
);
741 if (iTask
.do_retry
) {
746 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
747 return iTask
.err_code
;
755 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
756 void *command_data
, void *opaque
)
758 IscsiAIOCB
*acb
= opaque
;
765 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
766 iscsi_get_error(iscsi
));
767 acb
->status
= iscsi_translate_sense(&acb
->task
->sense
);
770 acb
->ioh
->driver_status
= 0;
771 acb
->ioh
->host_status
= 0;
773 acb
->ioh
->status
= status
;
775 #define SG_ERR_DRIVER_SENSE 0x08
777 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
780 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
782 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
783 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
784 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
785 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
788 iscsi_schedule_bh(acb
);
791 static void iscsi_ioctl_bh_completion(void *opaque
)
793 IscsiAIOCB
*acb
= opaque
;
795 qemu_bh_delete(acb
->bh
);
796 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
800 static void iscsi_ioctl_handle_emulated(IscsiAIOCB
*acb
, int req
, void *buf
)
802 BlockDriverState
*bs
= acb
->common
.bs
;
803 IscsiLun
*iscsilun
= bs
->opaque
;
807 case SG_GET_VERSION_NUM
:
811 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
817 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
),
818 iscsi_ioctl_bh_completion
, acb
);
820 qemu_bh_schedule(acb
->bh
);
823 static BlockAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
824 unsigned long int req
, void *buf
,
825 BlockCompletionFunc
*cb
, void *opaque
)
827 IscsiLun
*iscsilun
= bs
->opaque
;
828 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
829 struct iscsi_data data
;
832 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
834 acb
->iscsilun
= iscsilun
;
836 acb
->status
= -EINPROGRESS
;
841 iscsi_ioctl_handle_emulated(acb
, req
, buf
);
845 if (acb
->ioh
->cmd_len
> SCSI_CDB_MAX_SIZE
) {
846 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
847 acb
->ioh
->cmd_len
, SCSI_CDB_MAX_SIZE
);
852 acb
->task
= malloc(sizeof(struct scsi_task
));
853 if (acb
->task
== NULL
) {
854 error_report("iSCSI: Failed to allocate task for scsi command. %s",
855 iscsi_get_error(iscsi
));
859 memset(acb
->task
, 0, sizeof(struct scsi_task
));
861 switch (acb
->ioh
->dxfer_direction
) {
862 case SG_DXFER_TO_DEV
:
863 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
865 case SG_DXFER_FROM_DEV
:
866 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
869 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
873 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
874 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
875 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
878 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
879 if (acb
->ioh
->iovec_count
== 0) {
880 data
.data
= acb
->ioh
->dxferp
;
881 data
.size
= acb
->ioh
->dxfer_len
;
883 scsi_task_set_iov_out(acb
->task
,
884 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
885 acb
->ioh
->iovec_count
);
889 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
891 (data
.size
> 0) ? &data
: NULL
,
893 scsi_free_scsi_task(acb
->task
);
898 /* tell libiscsi to read straight into the buffer we got from ioctl */
899 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
900 if (acb
->ioh
->iovec_count
== 0) {
901 scsi_task_add_data_in_buffer(acb
->task
,
905 scsi_task_set_iov_in(acb
->task
,
906 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
907 acb
->ioh
->iovec_count
);
911 iscsi_set_events(iscsilun
);
919 iscsi_getlength(BlockDriverState
*bs
)
921 IscsiLun
*iscsilun
= bs
->opaque
;
924 len
= iscsilun
->num_blocks
;
925 len
*= iscsilun
->block_size
;
931 coroutine_fn
iscsi_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
934 IscsiLun
*iscsilun
= bs
->opaque
;
935 struct IscsiTask iTask
;
936 struct unmap_list list
;
938 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
942 if (!iscsilun
->lbp
.lbpu
) {
943 /* UNMAP is not supported by the target */
947 list
.lba
= sector_qemu2lun(sector_num
, iscsilun
);
948 list
.num
= sector_qemu2lun(nb_sectors
, iscsilun
);
950 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
952 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
953 iscsi_co_generic_cb
, &iTask
) == NULL
) {
957 while (!iTask
.complete
) {
958 iscsi_set_events(iscsilun
);
959 qemu_coroutine_yield();
962 if (iTask
.task
!= NULL
) {
963 scsi_free_scsi_task(iTask
.task
);
967 if (iTask
.do_retry
) {
972 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
) {
973 /* the target might fail with a check condition if it
974 is not happy with the alignment of the UNMAP request
975 we silently fail in this case */
979 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
980 return iTask
.err_code
;
983 iscsi_allocationmap_clear(iscsilun
, sector_num
, nb_sectors
);
989 coroutine_fn
iscsi_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
990 int count
, BdrvRequestFlags flags
)
992 IscsiLun
*iscsilun
= bs
->opaque
;
993 struct IscsiTask iTask
;
996 bool use_16_for_ws
= iscsilun
->use_16_for_rw
;
998 if (!is_byte_request_lun_aligned(offset
, count
, iscsilun
)) {
1002 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1003 if (!use_16_for_ws
&& !iscsilun
->lbp
.lbpws10
) {
1004 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1005 use_16_for_ws
= true;
1007 if (use_16_for_ws
&& !iscsilun
->lbp
.lbpws
) {
1008 /* WRITESAME16 with UNMAP is not supported by the target,
1009 * fall back and try WRITESAME10/16 without UNMAP */
1010 flags
&= ~BDRV_REQ_MAY_UNMAP
;
1011 use_16_for_ws
= iscsilun
->use_16_for_rw
;
1015 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
1016 /* WRITESAME without UNMAP is not supported by the target */
1020 lba
= offset
/ iscsilun
->block_size
;
1021 nb_blocks
= count
/ iscsilun
->block_size
;
1023 if (iscsilun
->zeroblock
== NULL
) {
1024 iscsilun
->zeroblock
= g_try_malloc0(iscsilun
->block_size
);
1025 if (iscsilun
->zeroblock
== NULL
) {
1030 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1032 if (use_16_for_ws
) {
1033 iTask
.task
= iscsi_writesame16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1034 iscsilun
->zeroblock
, iscsilun
->block_size
,
1035 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1036 0, 0, iscsi_co_generic_cb
, &iTask
);
1038 iTask
.task
= iscsi_writesame10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1039 iscsilun
->zeroblock
, iscsilun
->block_size
,
1040 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1041 0, 0, iscsi_co_generic_cb
, &iTask
);
1043 if (iTask
.task
== NULL
) {
1047 while (!iTask
.complete
) {
1048 iscsi_set_events(iscsilun
);
1049 qemu_coroutine_yield();
1052 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
&&
1053 iTask
.task
->sense
.key
== SCSI_SENSE_ILLEGAL_REQUEST
&&
1054 (iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
||
1055 iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
)) {
1056 /* WRITE SAME is not supported by the target */
1057 iscsilun
->has_write_same
= false;
1058 scsi_free_scsi_task(iTask
.task
);
1062 if (iTask
.task
!= NULL
) {
1063 scsi_free_scsi_task(iTask
.task
);
1067 if (iTask
.do_retry
) {
1072 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1073 return iTask
.err_code
;
1076 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1077 iscsi_allocationmap_clear(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1078 count
>> BDRV_SECTOR_BITS
);
1080 iscsi_allocationmap_set(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1081 count
>> BDRV_SECTOR_BITS
);
1087 static void parse_chap(struct iscsi_context
*iscsi
, const char *target
,
1092 const char *user
= NULL
;
1093 const char *password
= NULL
;
1094 const char *secretid
;
1095 char *secret
= NULL
;
1097 list
= qemu_find_opts("iscsi");
1102 opts
= qemu_opts_find(list
, target
);
1104 opts
= QTAILQ_FIRST(&list
->head
);
1110 user
= qemu_opt_get(opts
, "user");
1115 secretid
= qemu_opt_get(opts
, "password-secret");
1116 password
= qemu_opt_get(opts
, "password");
1117 if (secretid
&& password
) {
1118 error_setg(errp
, "'password' and 'password-secret' properties are "
1119 "mutually exclusive");
1123 secret
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
1128 } else if (!password
) {
1129 error_setg(errp
, "CHAP username specified but no password was given");
1133 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
1134 error_setg(errp
, "Failed to set initiator username and password");
1140 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
,
1145 const char *digest
= NULL
;
1147 list
= qemu_find_opts("iscsi");
1152 opts
= qemu_opts_find(list
, target
);
1154 opts
= QTAILQ_FIRST(&list
->head
);
1160 digest
= qemu_opt_get(opts
, "header-digest");
1165 if (!strcmp(digest
, "CRC32C")) {
1166 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
1167 } else if (!strcmp(digest
, "NONE")) {
1168 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
1169 } else if (!strcmp(digest
, "CRC32C-NONE")) {
1170 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
1171 } else if (!strcmp(digest
, "NONE-CRC32C")) {
1172 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1174 error_setg(errp
, "Invalid header-digest setting : %s", digest
);
1178 static char *parse_initiator_name(const char *target
)
1184 UuidInfo
*uuid_info
;
1186 list
= qemu_find_opts("iscsi");
1188 opts
= qemu_opts_find(list
, target
);
1190 opts
= QTAILQ_FIRST(&list
->head
);
1193 name
= qemu_opt_get(opts
, "initiator-name");
1195 return g_strdup(name
);
1200 uuid_info
= qmp_query_uuid(NULL
);
1201 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
1202 name
= qemu_get_vm_name();
1204 name
= uuid_info
->UUID
;
1206 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1207 name
? ":" : "", name
? name
: "");
1208 qapi_free_UuidInfo(uuid_info
);
1212 static int parse_timeout(const char *target
)
1216 const char *timeout
;
1218 list
= qemu_find_opts("iscsi");
1220 opts
= qemu_opts_find(list
, target
);
1222 opts
= QTAILQ_FIRST(&list
->head
);
1225 timeout
= qemu_opt_get(opts
, "timeout");
1227 return atoi(timeout
);
1235 static void iscsi_nop_timed_event(void *opaque
)
1237 IscsiLun
*iscsilun
= opaque
;
1239 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) >= MAX_NOP_FAILURES
) {
1240 error_report("iSCSI: NOP timeout. Reconnecting...");
1241 iscsilun
->request_timed_out
= true;
1242 } else if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
1243 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1247 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1248 iscsi_set_events(iscsilun
);
1251 static void iscsi_readcapacity_sync(IscsiLun
*iscsilun
, Error
**errp
)
1253 struct scsi_task
*task
= NULL
;
1254 struct scsi_readcapacity10
*rc10
= NULL
;
1255 struct scsi_readcapacity16
*rc16
= NULL
;
1256 int retries
= ISCSI_CMD_RETRIES
;
1260 scsi_free_scsi_task(task
);
1264 switch (iscsilun
->type
) {
1266 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
1267 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1268 rc16
= scsi_datain_unmarshall(task
);
1270 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity16 data.");
1272 iscsilun
->block_size
= rc16
->block_length
;
1273 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1274 iscsilun
->lbpme
= !!rc16
->lbpme
;
1275 iscsilun
->lbprz
= !!rc16
->lbprz
;
1276 iscsilun
->use_16_for_rw
= (rc16
->returned_lba
> 0xffffffff);
1280 if (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1281 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
1284 /* Fall through and try READ CAPACITY(10) instead. */
1286 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1287 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1288 rc10
= scsi_datain_unmarshall(task
);
1290 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity10 data.");
1292 iscsilun
->block_size
= rc10
->block_size
;
1293 if (rc10
->lba
== 0) {
1294 /* blank disk loaded */
1295 iscsilun
->num_blocks
= 0;
1297 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1305 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1306 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1309 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1310 error_setg(errp
, "iSCSI: failed to send readcapacity10/16 command");
1311 } else if (!iscsilun
->block_size
||
1312 iscsilun
->block_size
% BDRV_SECTOR_SIZE
) {
1313 error_setg(errp
, "iSCSI: the target returned an invalid "
1314 "block size of %d.", iscsilun
->block_size
);
1317 scsi_free_scsi_task(task
);
1321 /* TODO Convert to fine grained options */
1322 static QemuOptsList runtime_opts
= {
1324 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1328 .type
= QEMU_OPT_STRING
,
1329 .help
= "URL to the iscsi image",
1331 { /* end of list */ }
1335 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
, int lun
,
1336 int evpd
, int pc
, void **inq
, Error
**errp
)
1339 struct scsi_task
*task
= NULL
;
1340 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, 64);
1341 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1344 full_size
= scsi_datain_getfullsize(task
);
1345 if (full_size
> task
->datain
.size
) {
1346 scsi_free_scsi_task(task
);
1348 /* we need more data for the full list */
1349 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, full_size
);
1350 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1355 *inq
= scsi_datain_unmarshall(task
);
1357 error_setg(errp
, "iSCSI: failed to unmarshall inquiry datain blob");
1364 error_setg(errp
, "iSCSI: Inquiry command failed : %s",
1365 iscsi_get_error(iscsi
));
1368 scsi_free_scsi_task(task
);
1373 static void iscsi_detach_aio_context(BlockDriverState
*bs
)
1375 IscsiLun
*iscsilun
= bs
->opaque
;
1377 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsilun
->iscsi
),
1378 false, NULL
, NULL
, NULL
);
1379 iscsilun
->events
= 0;
1381 if (iscsilun
->nop_timer
) {
1382 timer_del(iscsilun
->nop_timer
);
1383 timer_free(iscsilun
->nop_timer
);
1384 iscsilun
->nop_timer
= NULL
;
1386 if (iscsilun
->event_timer
) {
1387 timer_del(iscsilun
->event_timer
);
1388 timer_free(iscsilun
->event_timer
);
1389 iscsilun
->event_timer
= NULL
;
1393 static void iscsi_attach_aio_context(BlockDriverState
*bs
,
1394 AioContext
*new_context
)
1396 IscsiLun
*iscsilun
= bs
->opaque
;
1398 iscsilun
->aio_context
= new_context
;
1399 iscsi_set_events(iscsilun
);
1401 /* Set up a timer for sending out iSCSI NOPs */
1402 iscsilun
->nop_timer
= aio_timer_new(iscsilun
->aio_context
,
1403 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1404 iscsi_nop_timed_event
, iscsilun
);
1405 timer_mod(iscsilun
->nop_timer
,
1406 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1408 /* Set up a timer for periodic calls to iscsi_set_events and to
1409 * scan for command timeout */
1410 iscsilun
->event_timer
= aio_timer_new(iscsilun
->aio_context
,
1411 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1412 iscsi_timed_check_events
, iscsilun
);
1413 timer_mod(iscsilun
->event_timer
,
1414 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
1417 static void iscsi_modesense_sync(IscsiLun
*iscsilun
)
1419 struct scsi_task
*task
;
1420 struct scsi_mode_sense
*ms
= NULL
;
1421 iscsilun
->write_protected
= false;
1422 iscsilun
->dpofua
= false;
1424 task
= iscsi_modesense6_sync(iscsilun
->iscsi
, iscsilun
->lun
,
1425 1, SCSI_MODESENSE_PC_CURRENT
,
1428 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1429 iscsi_get_error(iscsilun
->iscsi
));
1433 if (task
->status
!= SCSI_STATUS_GOOD
) {
1434 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1437 ms
= scsi_datain_unmarshall(task
);
1439 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1440 iscsi_get_error(iscsilun
->iscsi
));
1443 iscsilun
->write_protected
= ms
->device_specific_parameter
& 0x80;
1444 iscsilun
->dpofua
= ms
->device_specific_parameter
& 0x10;
1448 scsi_free_scsi_task(task
);
1453 * We support iscsi url's on the form
1454 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1456 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1459 IscsiLun
*iscsilun
= bs
->opaque
;
1460 struct iscsi_context
*iscsi
= NULL
;
1461 struct iscsi_url
*iscsi_url
= NULL
;
1462 struct scsi_task
*task
= NULL
;
1463 struct scsi_inquiry_standard
*inq
= NULL
;
1464 struct scsi_inquiry_supported_pages
*inq_vpd
;
1465 char *initiator_name
= NULL
;
1467 Error
*local_err
= NULL
;
1468 const char *filename
;
1469 int i
, ret
= 0, timeout
= 0;
1471 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1472 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1474 error_propagate(errp
, local_err
);
1479 filename
= qemu_opt_get(opts
, "filename");
1481 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1482 if (iscsi_url
== NULL
) {
1483 error_setg(errp
, "Failed to parse URL : %s", filename
);
1488 memset(iscsilun
, 0, sizeof(IscsiLun
));
1490 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1492 iscsi
= iscsi_create_context(initiator_name
);
1493 if (iscsi
== NULL
) {
1494 error_setg(errp
, "iSCSI: Failed to create iSCSI context.");
1499 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1500 error_setg(errp
, "iSCSI: Failed to set target name.");
1505 if (iscsi_url
->user
[0] != '\0') {
1506 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1509 error_setg(errp
, "Failed to set initiator username and password");
1515 /* check if we got CHAP username/password via the options */
1516 parse_chap(iscsi
, iscsi_url
->target
, &local_err
);
1517 if (local_err
!= NULL
) {
1518 error_propagate(errp
, local_err
);
1523 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1524 error_setg(errp
, "iSCSI: Failed to set session type to normal.");
1529 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1531 /* check if we got HEADER_DIGEST via the options */
1532 parse_header_digest(iscsi
, iscsi_url
->target
, &local_err
);
1533 if (local_err
!= NULL
) {
1534 error_propagate(errp
, local_err
);
1539 /* timeout handling is broken in libiscsi before 1.15.0 */
1540 timeout
= parse_timeout(iscsi_url
->target
);
1541 #if defined(LIBISCSI_API_VERSION) && LIBISCSI_API_VERSION >= 20150621
1542 iscsi_set_timeout(iscsi
, timeout
);
1545 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1549 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1550 error_setg(errp
, "iSCSI: Failed to connect to LUN : %s",
1551 iscsi_get_error(iscsi
));
1556 iscsilun
->iscsi
= iscsi
;
1557 iscsilun
->aio_context
= bdrv_get_aio_context(bs
);
1558 iscsilun
->lun
= iscsi_url
->lun
;
1559 iscsilun
->has_write_same
= true;
1561 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0,
1562 (void **) &inq
, errp
);
1567 iscsilun
->type
= inq
->periperal_device_type
;
1568 scsi_free_scsi_task(task
);
1571 iscsi_modesense_sync(iscsilun
);
1572 if (iscsilun
->dpofua
) {
1573 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1575 bs
->supported_zero_flags
= BDRV_REQ_MAY_UNMAP
;
1577 /* Check the write protect flag of the LUN if we want to write */
1578 if (iscsilun
->type
== TYPE_DISK
&& (flags
& BDRV_O_RDWR
) &&
1579 iscsilun
->write_protected
) {
1580 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1585 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1586 if (local_err
!= NULL
) {
1587 error_propagate(errp
, local_err
);
1591 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1592 bs
->request_alignment
= iscsilun
->block_size
;
1594 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1595 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1596 * will try to read from the device to guess the image format.
1598 if (iscsilun
->type
!= TYPE_DISK
&& iscsilun
->type
!= TYPE_ROM
) {
1602 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1603 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES
,
1604 (void **) &inq_vpd
, errp
);
1609 for (i
= 0; i
< inq_vpd
->num_pages
; i
++) {
1610 struct scsi_task
*inq_task
;
1611 struct scsi_inquiry_logical_block_provisioning
*inq_lbp
;
1612 struct scsi_inquiry_block_limits
*inq_bl
;
1613 switch (inq_vpd
->pages
[i
]) {
1614 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
:
1615 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1616 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
,
1617 (void **) &inq_lbp
, errp
);
1618 if (inq_task
== NULL
) {
1622 memcpy(&iscsilun
->lbp
, inq_lbp
,
1623 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1624 scsi_free_scsi_task(inq_task
);
1626 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
:
1627 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1628 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
,
1629 (void **) &inq_bl
, errp
);
1630 if (inq_task
== NULL
) {
1634 memcpy(&iscsilun
->bl
, inq_bl
,
1635 sizeof(struct scsi_inquiry_block_limits
));
1636 scsi_free_scsi_task(inq_task
);
1642 scsi_free_scsi_task(task
);
1645 iscsi_attach_aio_context(bs
, iscsilun
->aio_context
);
1647 /* Guess the internal cluster (page) size of the iscsi target by the means
1648 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1649 * reasonable size */
1650 if (iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
>= 4 * 1024 &&
1651 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
<= 16 * 1024 * 1024) {
1652 iscsilun
->cluster_sectors
= (iscsilun
->bl
.opt_unmap_gran
*
1653 iscsilun
->block_size
) >> BDRV_SECTOR_BITS
;
1654 if (iscsilun
->lbprz
) {
1655 iscsilun
->allocationmap
= iscsi_allocationmap_init(iscsilun
);
1656 if (iscsilun
->allocationmap
== NULL
) {
1663 qemu_opts_del(opts
);
1664 g_free(initiator_name
);
1665 if (iscsi_url
!= NULL
) {
1666 iscsi_destroy_url(iscsi_url
);
1669 scsi_free_scsi_task(task
);
1673 if (iscsi
!= NULL
) {
1674 if (iscsi_is_logged_in(iscsi
)) {
1675 iscsi_logout_sync(iscsi
);
1677 iscsi_destroy_context(iscsi
);
1679 memset(iscsilun
, 0, sizeof(IscsiLun
));
1684 static void iscsi_close(BlockDriverState
*bs
)
1686 IscsiLun
*iscsilun
= bs
->opaque
;
1687 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1689 iscsi_detach_aio_context(bs
);
1690 if (iscsi_is_logged_in(iscsi
)) {
1691 iscsi_logout_sync(iscsi
);
1693 iscsi_destroy_context(iscsi
);
1694 g_free(iscsilun
->zeroblock
);
1695 g_free(iscsilun
->allocationmap
);
1696 memset(iscsilun
, 0, sizeof(IscsiLun
));
1699 static int sector_limits_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
1701 return MIN(sector_lun2qemu(sector
, iscsilun
), INT_MAX
/ 2 + 1);
1704 static void iscsi_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1706 /* We don't actually refresh here, but just return data queried in
1707 * iscsi_open(): iscsi targets don't change their limits. */
1709 IscsiLun
*iscsilun
= bs
->opaque
;
1710 uint32_t max_xfer_len
= iscsilun
->use_16_for_rw
? 0xffffffff : 0xffff;
1712 if (iscsilun
->bl
.max_xfer_len
) {
1713 max_xfer_len
= MIN(max_xfer_len
, iscsilun
->bl
.max_xfer_len
);
1716 bs
->bl
.max_transfer_length
= sector_limits_lun2qemu(max_xfer_len
, iscsilun
);
1718 if (iscsilun
->lbp
.lbpu
) {
1719 if (iscsilun
->bl
.max_unmap
< 0xffffffff) {
1720 bs
->bl
.max_discard
=
1721 sector_limits_lun2qemu(iscsilun
->bl
.max_unmap
, iscsilun
);
1723 bs
->bl
.discard_alignment
=
1724 sector_limits_lun2qemu(iscsilun
->bl
.opt_unmap_gran
, iscsilun
);
1726 bs
->bl
.discard_alignment
= iscsilun
->block_size
>> BDRV_SECTOR_BITS
;
1729 if (iscsilun
->bl
.max_ws_len
< 0xffffffff / iscsilun
->block_size
) {
1730 bs
->bl
.max_pwrite_zeroes
=
1731 iscsilun
->bl
.max_ws_len
* iscsilun
->block_size
;
1733 if (iscsilun
->lbp
.lbpws
) {
1734 bs
->bl
.pwrite_zeroes_alignment
=
1735 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
1737 bs
->bl
.pwrite_zeroes_alignment
= iscsilun
->block_size
;
1739 bs
->bl
.opt_transfer_length
=
1740 sector_limits_lun2qemu(iscsilun
->bl
.opt_xfer_len
, iscsilun
);
1743 /* Note that this will not re-establish a connection with an iSCSI target - it
1744 * is effectively a NOP. */
1745 static int iscsi_reopen_prepare(BDRVReopenState
*state
,
1746 BlockReopenQueue
*queue
, Error
**errp
)
1748 IscsiLun
*iscsilun
= state
->bs
->opaque
;
1750 if (state
->flags
& BDRV_O_RDWR
&& iscsilun
->write_protected
) {
1751 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1757 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1759 IscsiLun
*iscsilun
= bs
->opaque
;
1760 Error
*local_err
= NULL
;
1762 if (iscsilun
->type
!= TYPE_DISK
) {
1766 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1767 if (local_err
!= NULL
) {
1768 error_free(local_err
);
1772 if (offset
> iscsi_getlength(bs
)) {
1776 if (iscsilun
->allocationmap
!= NULL
) {
1777 g_free(iscsilun
->allocationmap
);
1778 iscsilun
->allocationmap
= iscsi_allocationmap_init(iscsilun
);
1784 static int iscsi_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1787 int64_t total_size
= 0;
1788 BlockDriverState
*bs
;
1789 IscsiLun
*iscsilun
= NULL
;
1794 /* Read out options */
1795 total_size
= DIV_ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1797 bs
->opaque
= g_new0(struct IscsiLun
, 1);
1798 iscsilun
= bs
->opaque
;
1800 bs_options
= qdict_new();
1801 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1802 ret
= iscsi_open(bs
, bs_options
, 0, NULL
);
1803 QDECREF(bs_options
);
1808 iscsi_detach_aio_context(bs
);
1809 if (iscsilun
->type
!= TYPE_DISK
) {
1813 if (bs
->total_sectors
< total_size
) {
1820 if (iscsilun
->iscsi
!= NULL
) {
1821 iscsi_destroy_context(iscsilun
->iscsi
);
1829 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1831 IscsiLun
*iscsilun
= bs
->opaque
;
1832 bdi
->unallocated_blocks_are_zero
= iscsilun
->lbprz
;
1833 bdi
->can_write_zeroes_with_unmap
= iscsilun
->lbprz
&& iscsilun
->lbp
.lbpws
;
1834 bdi
->cluster_size
= iscsilun
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1838 static QemuOptsList iscsi_create_opts
= {
1839 .name
= "iscsi-create-opts",
1840 .head
= QTAILQ_HEAD_INITIALIZER(iscsi_create_opts
.head
),
1843 .name
= BLOCK_OPT_SIZE
,
1844 .type
= QEMU_OPT_SIZE
,
1845 .help
= "Virtual disk size"
1847 { /* end of list */ }
1851 static BlockDriver bdrv_iscsi
= {
1852 .format_name
= "iscsi",
1853 .protocol_name
= "iscsi",
1855 .instance_size
= sizeof(IscsiLun
),
1856 .bdrv_needs_filename
= true,
1857 .bdrv_file_open
= iscsi_open
,
1858 .bdrv_close
= iscsi_close
,
1859 .bdrv_create
= iscsi_create
,
1860 .create_opts
= &iscsi_create_opts
,
1861 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
1863 .bdrv_getlength
= iscsi_getlength
,
1864 .bdrv_get_info
= iscsi_get_info
,
1865 .bdrv_truncate
= iscsi_truncate
,
1866 .bdrv_refresh_limits
= iscsi_refresh_limits
,
1868 .bdrv_co_get_block_status
= iscsi_co_get_block_status
,
1869 .bdrv_co_discard
= iscsi_co_discard
,
1870 .bdrv_co_pwrite_zeroes
= iscsi_co_pwrite_zeroes
,
1871 .bdrv_co_readv
= iscsi_co_readv
,
1872 .bdrv_co_writev_flags
= iscsi_co_writev_flags
,
1873 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
1876 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1879 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
1880 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
1883 static QemuOptsList qemu_iscsi_opts
= {
1885 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1889 .type
= QEMU_OPT_STRING
,
1890 .help
= "username for CHAP authentication to target",
1893 .type
= QEMU_OPT_STRING
,
1894 .help
= "password for CHAP authentication to target",
1896 .name
= "password-secret",
1897 .type
= QEMU_OPT_STRING
,
1898 .help
= "ID of the secret providing password for CHAP "
1899 "authentication to target",
1901 .name
= "header-digest",
1902 .type
= QEMU_OPT_STRING
,
1903 .help
= "HeaderDigest setting. "
1904 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1906 .name
= "initiator-name",
1907 .type
= QEMU_OPT_STRING
,
1908 .help
= "Initiator iqn name to use when connecting",
1911 .type
= QEMU_OPT_NUMBER
,
1912 .help
= "Request timeout in seconds (default 0 = no timeout)",
1914 { /* end of list */ }
1918 static void iscsi_block_init(void)
1920 bdrv_register(&bdrv_iscsi
);
1921 qemu_add_opts(&qemu_iscsi_opts
);
1924 block_init(iscsi_block_init
);