2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2013 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 "config-host.h"
29 #include <arpa/inet.h>
30 #include "qemu-common.h"
31 #include "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "block/block_int.h"
35 #include "block/scsi.h"
37 #include "sysemu/sysemu.h"
38 #include "qmp-commands.h"
40 #include <iscsi/iscsi.h>
41 #include <iscsi/scsi-lowlevel.h>
45 #include <block/scsi.h>
48 typedef struct IscsiLun
{
49 struct iscsi_context
*iscsi
;
51 enum scsi_inquiry_peripheral_device_type type
;
58 uint8_t has_write_same
;
59 struct scsi_inquiry_logical_block_provisioning lbp
;
60 struct scsi_inquiry_block_limits bl
;
61 unsigned char *zeroblock
;
64 typedef struct IscsiTask
{
69 struct scsi_task
*task
;
74 typedef struct IscsiAIOCB
{
75 BlockDriverAIOCB common
;
79 struct scsi_task
*task
;
91 #define NOP_INTERVAL 5000
92 #define MAX_NOP_FAILURES 3
93 #define ISCSI_CMD_RETRIES 5
100 qemu_bh_delete(acb
->bh
);
105 if (acb
->canceled
== 0) {
106 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
109 if (acb
->task
!= NULL
) {
110 scsi_free_scsi_task(acb
->task
);
114 qemu_aio_release(acb
);
118 iscsi_schedule_bh(IscsiAIOCB
*acb
)
123 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
124 qemu_bh_schedule(acb
->bh
);
127 static void iscsi_co_generic_bh_cb(void *opaque
)
129 struct IscsiTask
*iTask
= opaque
;
130 qemu_bh_delete(iTask
->bh
);
131 qemu_coroutine_enter(iTask
->co
, NULL
);
135 iscsi_co_generic_cb(struct iscsi_context
*iscsi
, int status
,
136 void *command_data
, void *opaque
)
138 struct IscsiTask
*iTask
= opaque
;
139 struct scsi_task
*task
= command_data
;
142 iTask
->status
= status
;
146 if (iTask
->retries
-- > 0 && status
== SCSI_STATUS_CHECK_CONDITION
147 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
152 if (status
!= SCSI_STATUS_GOOD
) {
153 error_report("iSCSI: Failure. %s", iscsi_get_error(iscsi
));
158 iTask
->bh
= qemu_bh_new(iscsi_co_generic_bh_cb
, iTask
);
159 qemu_bh_schedule(iTask
->bh
);
163 static void iscsi_co_init_iscsitask(IscsiLun
*iscsilun
, struct IscsiTask
*iTask
)
165 *iTask
= (struct IscsiTask
) {
166 .co
= qemu_coroutine_self(),
167 .retries
= ISCSI_CMD_RETRIES
,
172 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
175 IscsiAIOCB
*acb
= private_data
;
177 acb
->status
= -ECANCELED
;
178 iscsi_schedule_bh(acb
);
182 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
184 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
185 IscsiLun
*iscsilun
= acb
->iscsilun
;
187 if (acb
->status
!= -EINPROGRESS
) {
193 /* send a task mgmt call to the target to cancel the task on the target */
194 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
195 iscsi_abort_task_cb
, acb
);
197 while (acb
->status
== -EINPROGRESS
) {
202 static const AIOCBInfo iscsi_aiocb_info
= {
203 .aiocb_size
= sizeof(IscsiAIOCB
),
204 .cancel
= iscsi_aio_cancel
,
208 static void iscsi_process_read(void *arg
);
209 static void iscsi_process_write(void *arg
);
212 iscsi_set_events(IscsiLun
*iscsilun
)
214 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
217 /* We always register a read handler. */
219 ev
|= iscsi_which_events(iscsi
);
220 if (ev
!= iscsilun
->events
) {
221 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
223 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
228 iscsilun
->events
= ev
;
232 iscsi_process_read(void *arg
)
234 IscsiLun
*iscsilun
= arg
;
235 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
237 iscsi_service(iscsi
, POLLIN
);
238 iscsi_set_events(iscsilun
);
242 iscsi_process_write(void *arg
)
244 IscsiLun
*iscsilun
= arg
;
245 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
247 iscsi_service(iscsi
, POLLOUT
);
248 iscsi_set_events(iscsilun
);
251 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
253 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
256 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
258 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
261 static bool is_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
264 if ((sector_num
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
||
265 (nb_sectors
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
) {
266 error_report("iSCSI misaligned request: "
267 "iscsilun->block_size %u, sector_num %" PRIi64
269 iscsilun
->block_size
, sector_num
, nb_sectors
);
275 static int coroutine_fn
iscsi_co_writev(BlockDriverState
*bs
,
276 int64_t sector_num
, int nb_sectors
,
279 IscsiLun
*iscsilun
= bs
->opaque
;
280 struct IscsiTask iTask
;
282 uint32_t num_sectors
;
283 uint8_t *data
= NULL
;
286 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
290 lba
= sector_qemu2lun(sector_num
, iscsilun
);
291 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
292 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
293 /* if the iovec only contains one buffer we can pass it directly */
294 if (iov
->niov
== 1) {
295 data
= iov
->iov
[0].iov_base
;
297 size_t size
= MIN(nb_sectors
* BDRV_SECTOR_SIZE
, iov
->size
);
298 buf
= g_malloc(size
);
299 qemu_iovec_to_buf(iov
, 0, buf
, size
);
303 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
305 iTask
.task
= iscsi_write16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
306 data
, num_sectors
* iscsilun
->block_size
,
307 iscsilun
->block_size
, 0, 0, 0, 0, 0,
308 iscsi_co_generic_cb
, &iTask
);
309 if (iTask
.task
== NULL
) {
313 #if defined(LIBISCSI_FEATURE_IOVECTOR)
314 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
317 while (!iTask
.complete
) {
318 iscsi_set_events(iscsilun
);
319 qemu_coroutine_yield();
322 if (iTask
.task
!= NULL
) {
323 scsi_free_scsi_task(iTask
.task
);
327 if (iTask
.do_retry
) {
333 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
340 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
341 int64_t sector_num
, int nb_sectors
,
344 IscsiLun
*iscsilun
= bs
->opaque
;
345 struct IscsiTask iTask
;
347 uint32_t num_sectors
;
348 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
352 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
356 lba
= sector_qemu2lun(sector_num
, iscsilun
);
357 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
359 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
361 switch (iscsilun
->type
) {
363 iTask
.task
= iscsi_read16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
364 num_sectors
* iscsilun
->block_size
,
365 iscsilun
->block_size
, 0, 0, 0, 0, 0,
366 iscsi_co_generic_cb
, &iTask
);
369 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
370 num_sectors
* iscsilun
->block_size
,
371 iscsilun
->block_size
,
372 #if !defined(CONFIG_LIBISCSI_1_4) /* API change from 1.4.0 to 1.5.0 */
375 iscsi_co_generic_cb
, &iTask
);
378 if (iTask
.task
== NULL
) {
381 #if defined(LIBISCSI_FEATURE_IOVECTOR)
382 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
384 for (i
= 0; i
< iov
->niov
; i
++) {
385 scsi_task_add_data_in_buffer(iTask
.task
,
387 iov
->iov
[i
].iov_base
);
391 while (!iTask
.complete
) {
392 iscsi_set_events(iscsilun
);
393 qemu_coroutine_yield();
396 if (iTask
.task
!= NULL
) {
397 scsi_free_scsi_task(iTask
.task
);
401 if (iTask
.do_retry
) {
405 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
412 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
414 IscsiLun
*iscsilun
= bs
->opaque
;
415 struct IscsiTask iTask
;
417 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
420 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
421 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
425 while (!iTask
.complete
) {
426 iscsi_set_events(iscsilun
);
427 qemu_coroutine_yield();
430 if (iTask
.task
!= NULL
) {
431 scsi_free_scsi_task(iTask
.task
);
435 if (iTask
.do_retry
) {
439 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
448 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
449 void *command_data
, void *opaque
)
451 IscsiAIOCB
*acb
= opaque
;
456 if (acb
->canceled
!= 0) {
462 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
463 iscsi_get_error(iscsi
));
467 acb
->ioh
->driver_status
= 0;
468 acb
->ioh
->host_status
= 0;
471 #define SG_ERR_DRIVER_SENSE 0x08
473 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
476 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
478 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
479 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
480 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
481 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
484 iscsi_schedule_bh(acb
);
487 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
488 unsigned long int req
, void *buf
,
489 BlockDriverCompletionFunc
*cb
, void *opaque
)
491 IscsiLun
*iscsilun
= bs
->opaque
;
492 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
493 struct iscsi_data data
;
496 assert(req
== SG_IO
);
498 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
500 acb
->iscsilun
= iscsilun
;
503 acb
->status
= -EINPROGRESS
;
507 acb
->task
= malloc(sizeof(struct scsi_task
));
508 if (acb
->task
== NULL
) {
509 error_report("iSCSI: Failed to allocate task for scsi command. %s",
510 iscsi_get_error(iscsi
));
511 qemu_aio_release(acb
);
514 memset(acb
->task
, 0, sizeof(struct scsi_task
));
516 switch (acb
->ioh
->dxfer_direction
) {
517 case SG_DXFER_TO_DEV
:
518 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
520 case SG_DXFER_FROM_DEV
:
521 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
524 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
528 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
529 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
530 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
533 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
534 if (acb
->ioh
->iovec_count
== 0) {
535 data
.data
= acb
->ioh
->dxferp
;
536 data
.size
= acb
->ioh
->dxfer_len
;
538 #if defined(LIBISCSI_FEATURE_IOVECTOR)
539 scsi_task_set_iov_out(acb
->task
,
540 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
541 acb
->ioh
->iovec_count
);
543 struct iovec
*iov
= (struct iovec
*)acb
->ioh
->dxferp
;
545 acb
->buf
= g_malloc(acb
->ioh
->dxfer_len
);
546 data
.data
= acb
->buf
;
547 data
.size
= iov_to_buf(iov
, acb
->ioh
->iovec_count
, 0,
548 acb
->buf
, acb
->ioh
->dxfer_len
);
553 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
555 (data
.size
> 0) ? &data
: NULL
,
557 scsi_free_scsi_task(acb
->task
);
558 qemu_aio_release(acb
);
562 /* tell libiscsi to read straight into the buffer we got from ioctl */
563 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
564 if (acb
->ioh
->iovec_count
== 0) {
565 scsi_task_add_data_in_buffer(acb
->task
,
569 #if defined(LIBISCSI_FEATURE_IOVECTOR)
570 scsi_task_set_iov_in(acb
->task
,
571 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
572 acb
->ioh
->iovec_count
);
575 for (i
= 0; i
< acb
->ioh
->iovec_count
; i
++) {
576 struct iovec
*iov
= (struct iovec
*)acb
->ioh
->dxferp
;
578 scsi_task_add_data_in_buffer(acb
->task
,
586 iscsi_set_events(iscsilun
);
592 static void ioctl_cb(void *opaque
, int status
)
594 int *p_status
= opaque
;
598 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
600 IscsiLun
*iscsilun
= bs
->opaque
;
604 case SG_GET_VERSION_NUM
:
608 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
611 status
= -EINPROGRESS
;
612 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
614 while (status
== -EINPROGRESS
) {
627 iscsi_getlength(BlockDriverState
*bs
)
629 IscsiLun
*iscsilun
= bs
->opaque
;
632 len
= iscsilun
->num_blocks
;
633 len
*= iscsilun
->block_size
;
638 #if defined(LIBISCSI_FEATURE_IOVECTOR)
640 static int64_t coroutine_fn
iscsi_co_get_block_status(BlockDriverState
*bs
,
642 int nb_sectors
, int *pnum
)
644 IscsiLun
*iscsilun
= bs
->opaque
;
645 struct scsi_get_lba_status
*lbas
= NULL
;
646 struct scsi_lba_status_descriptor
*lbasd
= NULL
;
647 struct IscsiTask iTask
;
650 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
652 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
657 /* default to all sectors allocated */
658 ret
= BDRV_BLOCK_DATA
;
659 ret
|= (sector_num
<< BDRV_SECTOR_BITS
) | BDRV_BLOCK_OFFSET_VALID
;
662 /* LUN does not support logical block provisioning */
663 if (iscsilun
->lbpme
== 0) {
668 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
669 sector_qemu2lun(sector_num
, iscsilun
),
670 8 + 16, iscsi_co_generic_cb
,
676 while (!iTask
.complete
) {
677 iscsi_set_events(iscsilun
);
678 qemu_coroutine_yield();
681 if (iTask
.do_retry
) {
682 if (iTask
.task
!= NULL
) {
683 scsi_free_scsi_task(iTask
.task
);
689 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
690 /* in case the get_lba_status_callout fails (i.e.
691 * because the device is busy or the cmd is not
692 * supported) we pretend all blocks are allocated
693 * for backwards compatibility */
697 lbas
= scsi_datain_unmarshall(iTask
.task
);
703 lbasd
= &lbas
->descriptors
[0];
705 if (sector_qemu2lun(sector_num
, iscsilun
) != lbasd
->lba
) {
710 *pnum
= sector_lun2qemu(lbasd
->num_blocks
, iscsilun
);
711 if (*pnum
> nb_sectors
) {
715 if (lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_DEALLOCATED
||
716 lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_ANCHORED
) {
717 ret
&= ~BDRV_BLOCK_DATA
;
718 if (iscsilun
->lbprz
) {
719 ret
|= BDRV_BLOCK_ZERO
;
724 if (iTask
.task
!= NULL
) {
725 scsi_free_scsi_task(iTask
.task
);
730 #endif /* LIBISCSI_FEATURE_IOVECTOR */
733 coroutine_fn
iscsi_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
736 IscsiLun
*iscsilun
= bs
->opaque
;
737 struct IscsiTask iTask
;
738 struct unmap_list list
;
740 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
744 if (!iscsilun
->lbp
.lbpu
) {
745 /* UNMAP is not supported by the target */
749 list
.lba
= sector_qemu2lun(sector_num
, iscsilun
);
750 list
.num
= sector_qemu2lun(nb_sectors
, iscsilun
);
752 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
754 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
755 iscsi_co_generic_cb
, &iTask
) == NULL
) {
759 while (!iTask
.complete
) {
760 iscsi_set_events(iscsilun
);
761 qemu_coroutine_yield();
764 if (iTask
.task
!= NULL
) {
765 scsi_free_scsi_task(iTask
.task
);
769 if (iTask
.do_retry
) {
773 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
) {
774 /* the target might fail with a check condition if it
775 is not happy with the alignment of the UNMAP request
776 we silently fail in this case */
780 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
787 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
790 coroutine_fn
iscsi_co_write_zeroes(BlockDriverState
*bs
, int64_t sector_num
,
791 int nb_sectors
, BdrvRequestFlags flags
)
793 IscsiLun
*iscsilun
= bs
->opaque
;
794 struct IscsiTask iTask
;
798 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
802 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
803 /* WRITE SAME without UNMAP is not supported by the target */
807 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->lbp
.lbpws
) {
808 /* WRITE SAME with UNMAP is not supported by the target */
812 lba
= sector_qemu2lun(sector_num
, iscsilun
);
813 nb_blocks
= sector_qemu2lun(nb_sectors
, iscsilun
);
815 if (iscsilun
->zeroblock
== NULL
) {
816 iscsilun
->zeroblock
= g_malloc0(iscsilun
->block_size
);
819 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
821 if (iscsi_writesame16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
822 iscsilun
->zeroblock
, iscsilun
->block_size
,
823 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
824 0, 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
828 while (!iTask
.complete
) {
829 iscsi_set_events(iscsilun
);
830 qemu_coroutine_yield();
833 if (iTask
.task
!= NULL
) {
834 scsi_free_scsi_task(iTask
.task
);
838 if (iTask
.do_retry
) {
842 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
843 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
&&
844 iTask
.task
->sense
.key
== SCSI_SENSE_ILLEGAL_REQUEST
&&
845 iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
) {
846 /* WRITE SAME is not supported by the target */
847 iscsilun
->has_write_same
= false;
857 #endif /* SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED */
859 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
863 const char *user
= NULL
;
864 const char *password
= NULL
;
866 list
= qemu_find_opts("iscsi");
871 opts
= qemu_opts_find(list
, target
);
873 opts
= QTAILQ_FIRST(&list
->head
);
879 user
= qemu_opt_get(opts
, "user");
884 password
= qemu_opt_get(opts
, "password");
886 error_report("CHAP username specified but no password was given");
890 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
891 error_report("Failed to set initiator username and password");
898 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
902 const char *digest
= NULL
;
904 list
= qemu_find_opts("iscsi");
909 opts
= qemu_opts_find(list
, target
);
911 opts
= QTAILQ_FIRST(&list
->head
);
917 digest
= qemu_opt_get(opts
, "header-digest");
922 if (!strcmp(digest
, "CRC32C")) {
923 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
924 } else if (!strcmp(digest
, "NONE")) {
925 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
926 } else if (!strcmp(digest
, "CRC32C-NONE")) {
927 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
928 } else if (!strcmp(digest
, "NONE-CRC32C")) {
929 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
931 error_report("Invalid header-digest setting : %s", digest
);
935 static char *parse_initiator_name(const char *target
)
943 list
= qemu_find_opts("iscsi");
945 opts
= qemu_opts_find(list
, target
);
947 opts
= QTAILQ_FIRST(&list
->head
);
950 name
= qemu_opt_get(opts
, "initiator-name");
952 return g_strdup(name
);
957 uuid_info
= qmp_query_uuid(NULL
);
958 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
959 name
= qemu_get_vm_name();
961 name
= uuid_info
->UUID
;
963 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
964 name
? ":" : "", name
? name
: "");
965 qapi_free_UuidInfo(uuid_info
);
969 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
970 static void iscsi_nop_timed_event(void *opaque
)
972 IscsiLun
*iscsilun
= opaque
;
974 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) > MAX_NOP_FAILURES
) {
975 error_report("iSCSI: NOP timeout. Reconnecting...");
976 iscsi_reconnect(iscsilun
->iscsi
);
979 if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
980 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
984 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
985 iscsi_set_events(iscsilun
);
989 static int iscsi_readcapacity_sync(IscsiLun
*iscsilun
)
991 struct scsi_task
*task
= NULL
;
992 struct scsi_readcapacity10
*rc10
= NULL
;
993 struct scsi_readcapacity16
*rc16
= NULL
;
995 int retries
= ISCSI_CMD_RETRIES
;
999 scsi_free_scsi_task(task
);
1003 switch (iscsilun
->type
) {
1005 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
1006 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1007 rc16
= scsi_datain_unmarshall(task
);
1009 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
1012 iscsilun
->block_size
= rc16
->block_length
;
1013 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1014 iscsilun
->lbpme
= rc16
->lbpme
;
1015 iscsilun
->lbprz
= rc16
->lbprz
;
1020 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1021 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1022 rc10
= scsi_datain_unmarshall(task
);
1024 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
1027 iscsilun
->block_size
= rc10
->block_size
;
1028 if (rc10
->lba
== 0) {
1029 /* blank disk loaded */
1030 iscsilun
->num_blocks
= 0;
1032 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1040 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1041 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1044 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1045 error_report("iSCSI: failed to send readcapacity10 command.");
1049 scsi_free_scsi_task(task
);
1054 /* TODO Convert to fine grained options */
1055 static QemuOptsList runtime_opts
= {
1057 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1061 .type
= QEMU_OPT_STRING
,
1062 .help
= "URL to the iscsi image",
1064 { /* end of list */ }
1068 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
,
1069 int lun
, int evpd
, int pc
) {
1071 struct scsi_task
*task
= NULL
;
1072 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, 64);
1073 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1076 full_size
= scsi_datain_getfullsize(task
);
1077 if (full_size
> task
->datain
.size
) {
1078 scsi_free_scsi_task(task
);
1080 /* we need more data for the full list */
1081 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, full_size
);
1082 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1090 error_report("iSCSI: Inquiry command failed : %s",
1091 iscsi_get_error(iscsi
));
1093 scsi_free_scsi_task(task
);
1100 * We support iscsi url's on the form
1101 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1103 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1106 IscsiLun
*iscsilun
= bs
->opaque
;
1107 struct iscsi_context
*iscsi
= NULL
;
1108 struct iscsi_url
*iscsi_url
= NULL
;
1109 struct scsi_task
*task
= NULL
;
1110 struct scsi_inquiry_standard
*inq
= NULL
;
1111 char *initiator_name
= NULL
;
1113 Error
*local_err
= NULL
;
1114 const char *filename
;
1117 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
1118 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1119 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1120 "of 512", BDRV_SECTOR_SIZE
);
1124 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1125 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1126 if (error_is_set(&local_err
)) {
1127 qerror_report_err(local_err
);
1128 error_free(local_err
);
1133 filename
= qemu_opt_get(opts
, "filename");
1136 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1137 if (iscsi_url
== NULL
) {
1138 error_report("Failed to parse URL : %s", filename
);
1143 memset(iscsilun
, 0, sizeof(IscsiLun
));
1145 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1147 iscsi
= iscsi_create_context(initiator_name
);
1148 if (iscsi
== NULL
) {
1149 error_report("iSCSI: Failed to create iSCSI context.");
1154 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1155 error_report("iSCSI: Failed to set target name.");
1160 if (iscsi_url
->user
!= NULL
) {
1161 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1164 error_report("Failed to set initiator username and password");
1170 /* check if we got CHAP username/password via the options */
1171 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
1172 error_report("iSCSI: Failed to set CHAP user/password");
1177 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1178 error_report("iSCSI: Failed to set session type to normal.");
1183 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1185 /* check if we got HEADER_DIGEST via the options */
1186 parse_header_digest(iscsi
, iscsi_url
->target
);
1188 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1189 error_report("iSCSI: Failed to connect to LUN : %s",
1190 iscsi_get_error(iscsi
));
1195 iscsilun
->iscsi
= iscsi
;
1196 iscsilun
->lun
= iscsi_url
->lun
;
1198 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
1200 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1201 error_report("iSCSI: failed to send inquiry command.");
1206 inq
= scsi_datain_unmarshall(task
);
1208 error_report("iSCSI: Failed to unmarshall inquiry data.");
1213 iscsilun
->type
= inq
->periperal_device_type
;
1214 iscsilun
->has_write_same
= true;
1216 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1219 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1221 /* Medium changer or tape. We dont have any emulation for this so this must
1222 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1223 * to read from the device to guess the image format.
1225 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1226 iscsilun
->type
== TYPE_TAPE
) {
1230 if (iscsilun
->lbpme
) {
1231 struct scsi_inquiry_logical_block_provisioning
*inq_lbp
;
1232 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1233 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
);
1238 inq_lbp
= scsi_datain_unmarshall(task
);
1239 if (inq_lbp
== NULL
) {
1240 error_report("iSCSI: failed to unmarshall inquiry datain blob");
1244 memcpy(&iscsilun
->lbp
, inq_lbp
,
1245 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1246 scsi_free_scsi_task(task
);
1250 if (iscsilun
->lbp
.lbpu
|| iscsilun
->lbp
.lbpws
) {
1251 struct scsi_inquiry_block_limits
*inq_bl
;
1252 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1253 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
);
1258 inq_bl
= scsi_datain_unmarshall(task
);
1259 if (inq_bl
== NULL
) {
1260 error_report("iSCSI: failed to unmarshall inquiry datain blob");
1264 memcpy(&iscsilun
->bl
, inq_bl
,
1265 sizeof(struct scsi_inquiry_block_limits
));
1266 scsi_free_scsi_task(task
);
1269 if (iscsilun
->bl
.max_unmap
< 0xffffffff) {
1270 bs
->bl
.max_discard
= sector_lun2qemu(iscsilun
->bl
.max_unmap
,
1273 bs
->bl
.discard_alignment
= sector_lun2qemu(iscsilun
->bl
.opt_unmap_gran
,
1276 if (iscsilun
->bl
.max_ws_len
< 0xffffffff) {
1277 bs
->bl
.max_write_zeroes
= sector_lun2qemu(iscsilun
->bl
.max_ws_len
,
1280 bs
->bl
.write_zeroes_alignment
= sector_lun2qemu(iscsilun
->bl
.opt_unmap_gran
,
1283 bs
->bl
.opt_transfer_length
= sector_lun2qemu(iscsilun
->bl
.opt_xfer_len
,
1287 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1288 /* Set up a timer for sending out iSCSI NOPs */
1289 iscsilun
->nop_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, iscsi_nop_timed_event
, iscsilun
);
1290 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1294 qemu_opts_del(opts
);
1295 if (initiator_name
!= NULL
) {
1296 g_free(initiator_name
);
1298 if (iscsi_url
!= NULL
) {
1299 iscsi_destroy_url(iscsi_url
);
1302 scsi_free_scsi_task(task
);
1306 if (iscsi
!= NULL
) {
1307 iscsi_destroy_context(iscsi
);
1309 memset(iscsilun
, 0, sizeof(IscsiLun
));
1314 static void iscsi_close(BlockDriverState
*bs
)
1316 IscsiLun
*iscsilun
= bs
->opaque
;
1317 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1319 if (iscsilun
->nop_timer
) {
1320 timer_del(iscsilun
->nop_timer
);
1321 timer_free(iscsilun
->nop_timer
);
1323 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
);
1324 iscsi_destroy_context(iscsi
);
1325 g_free(iscsilun
->zeroblock
);
1326 memset(iscsilun
, 0, sizeof(IscsiLun
));
1329 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1331 IscsiLun
*iscsilun
= bs
->opaque
;
1334 if (iscsilun
->type
!= TYPE_DISK
) {
1338 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1342 if (offset
> iscsi_getlength(bs
)) {
1349 static int iscsi_create(const char *filename
, QEMUOptionParameter
*options
,
1353 int64_t total_size
= 0;
1354 BlockDriverState
*bs
;
1355 IscsiLun
*iscsilun
= NULL
;
1360 /* Read out options */
1361 while (options
&& options
->name
) {
1362 if (!strcmp(options
->name
, "size")) {
1363 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1368 bs
->opaque
= g_malloc0(sizeof(struct IscsiLun
));
1369 iscsilun
= bs
->opaque
;
1371 bs_options
= qdict_new();
1372 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1373 ret
= iscsi_open(bs
, bs_options
, 0, NULL
);
1374 QDECREF(bs_options
);
1379 if (iscsilun
->nop_timer
) {
1380 timer_del(iscsilun
->nop_timer
);
1381 timer_free(iscsilun
->nop_timer
);
1383 if (iscsilun
->type
!= TYPE_DISK
) {
1387 if (bs
->total_sectors
< total_size
) {
1394 if (iscsilun
->iscsi
!= NULL
) {
1395 iscsi_destroy_context(iscsilun
->iscsi
);
1403 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1405 IscsiLun
*iscsilun
= bs
->opaque
;
1406 bdi
->unallocated_blocks_are_zero
= !!iscsilun
->lbprz
;
1407 bdi
->can_write_zeroes_with_unmap
= iscsilun
->lbprz
&& iscsilun
->lbp
.lbpws
;
1408 /* Guess the internal cluster (page) size of the iscsi target by the means
1409 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1410 * reasonable size for bdi->cluster_size */
1411 if (iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
>= 64 * 1024 &&
1412 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
<= 16 * 1024 * 1024) {
1413 bdi
->cluster_size
= iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
1418 static QEMUOptionParameter iscsi_create_options
[] = {
1420 .name
= BLOCK_OPT_SIZE
,
1422 .help
= "Virtual disk size"
1427 static BlockDriver bdrv_iscsi
= {
1428 .format_name
= "iscsi",
1429 .protocol_name
= "iscsi",
1431 .instance_size
= sizeof(IscsiLun
),
1432 .bdrv_needs_filename
= true,
1433 .bdrv_file_open
= iscsi_open
,
1434 .bdrv_close
= iscsi_close
,
1435 .bdrv_create
= iscsi_create
,
1436 .create_options
= iscsi_create_options
,
1438 .bdrv_getlength
= iscsi_getlength
,
1439 .bdrv_get_info
= iscsi_get_info
,
1440 .bdrv_truncate
= iscsi_truncate
,
1442 #if defined(LIBISCSI_FEATURE_IOVECTOR)
1443 .bdrv_co_get_block_status
= iscsi_co_get_block_status
,
1445 .bdrv_co_discard
= iscsi_co_discard
,
1446 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
1447 .bdrv_co_write_zeroes
= iscsi_co_write_zeroes
,
1449 .bdrv_co_readv
= iscsi_co_readv
,
1450 .bdrv_co_writev
= iscsi_co_writev
,
1451 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
1454 .bdrv_ioctl
= iscsi_ioctl
,
1455 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1459 static QemuOptsList qemu_iscsi_opts
= {
1461 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1465 .type
= QEMU_OPT_STRING
,
1466 .help
= "username for CHAP authentication to target",
1469 .type
= QEMU_OPT_STRING
,
1470 .help
= "password for CHAP authentication to target",
1472 .name
= "header-digest",
1473 .type
= QEMU_OPT_STRING
,
1474 .help
= "HeaderDigest setting. "
1475 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1477 .name
= "initiator-name",
1478 .type
= QEMU_OPT_STRING
,
1479 .help
= "Initiator iqn name to use when connecting",
1481 { /* end of list */ }
1485 static void iscsi_block_init(void)
1487 bdrv_register(&bdrv_iscsi
);
1488 qemu_add_opts(&qemu_iscsi_opts
);
1491 block_init(iscsi_block_init
);