2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2014 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"
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"
42 #include <iscsi/iscsi.h>
43 #include <iscsi/scsi-lowlevel.h>
47 #include <block/scsi.h>
50 typedef struct IscsiLun
{
51 struct iscsi_context
*iscsi
;
52 AioContext
*aio_context
;
54 enum scsi_inquiry_peripheral_device_type type
;
59 QEMUTimer
*event_timer
;
60 struct scsi_inquiry_logical_block_provisioning lbp
;
61 struct scsi_inquiry_block_limits bl
;
62 unsigned char *zeroblock
;
63 unsigned long *allocationmap
;
72 typedef struct IscsiTask
{
77 struct scsi_task
*task
;
81 QEMUTimer retry_timer
;
84 typedef struct IscsiAIOCB
{
89 struct scsi_task
*task
;
99 #define EVENT_INTERVAL 250
100 #define NOP_INTERVAL 5000
101 #define MAX_NOP_FAILURES 3
102 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
103 static const unsigned iscsi_retry_times
[] = {8, 32, 128, 512, 2048};
105 /* this threshold is a trade-off knob to choose between
106 * the potential additional overhead of an extra GET_LBA_STATUS request
107 * vs. unnecessarily reading a lot of zero sectors over the wire.
108 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
109 * sectors we check the allocation status of the area covered by the
110 * request first if the allocationmap indicates that the area might be
112 #define ISCSI_CHECKALLOC_THRES 64
119 qemu_bh_delete(acb
->bh
);
124 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
126 if (acb
->task
!= NULL
) {
127 scsi_free_scsi_task(acb
->task
);
135 iscsi_schedule_bh(IscsiAIOCB
*acb
)
140 acb
->bh
= aio_bh_new(acb
->iscsilun
->aio_context
, iscsi_bh_cb
, acb
);
141 qemu_bh_schedule(acb
->bh
);
144 static void iscsi_co_generic_bh_cb(void *opaque
)
146 struct IscsiTask
*iTask
= opaque
;
148 qemu_bh_delete(iTask
->bh
);
149 qemu_coroutine_enter(iTask
->co
, NULL
);
152 static void iscsi_retry_timer_expired(void *opaque
)
154 struct IscsiTask
*iTask
= opaque
;
157 qemu_coroutine_enter(iTask
->co
, NULL
);
161 static inline unsigned exp_random(double mean
)
163 return -mean
* log((double)rand() / RAND_MAX
);
167 iscsi_co_generic_cb(struct iscsi_context
*iscsi
, int status
,
168 void *command_data
, void *opaque
)
170 struct IscsiTask
*iTask
= opaque
;
171 struct scsi_task
*task
= command_data
;
173 iTask
->status
= status
;
177 if (status
!= SCSI_STATUS_GOOD
) {
178 if (iTask
->retries
++ < ISCSI_CMD_RETRIES
) {
179 if (status
== SCSI_STATUS_CHECK_CONDITION
180 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
181 error_report("iSCSI CheckCondition: %s",
182 iscsi_get_error(iscsi
));
186 if (status
== SCSI_STATUS_BUSY
) {
187 unsigned retry_time
=
188 exp_random(iscsi_retry_times
[iTask
->retries
- 1]);
189 error_report("iSCSI Busy (retry #%u in %u ms): %s",
190 iTask
->retries
, retry_time
,
191 iscsi_get_error(iscsi
));
192 aio_timer_init(iTask
->iscsilun
->aio_context
,
193 &iTask
->retry_timer
, QEMU_CLOCK_REALTIME
,
194 SCALE_MS
, iscsi_retry_timer_expired
, iTask
);
195 timer_mod(&iTask
->retry_timer
,
196 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + retry_time
);
201 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi
));
206 iTask
->bh
= aio_bh_new(iTask
->iscsilun
->aio_context
,
207 iscsi_co_generic_bh_cb
, iTask
);
208 qemu_bh_schedule(iTask
->bh
);
214 static void iscsi_co_init_iscsitask(IscsiLun
*iscsilun
, struct IscsiTask
*iTask
)
216 *iTask
= (struct IscsiTask
) {
217 .co
= qemu_coroutine_self(),
218 .iscsilun
= iscsilun
,
223 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
226 IscsiAIOCB
*acb
= private_data
;
228 acb
->status
= -ECANCELED
;
229 iscsi_schedule_bh(acb
);
233 iscsi_aio_cancel(BlockAIOCB
*blockacb
)
235 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
236 IscsiLun
*iscsilun
= acb
->iscsilun
;
238 if (acb
->status
!= -EINPROGRESS
) {
242 /* send a task mgmt call to the target to cancel the task on the target */
243 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
244 iscsi_abort_task_cb
, acb
);
248 static const AIOCBInfo iscsi_aiocb_info
= {
249 .aiocb_size
= sizeof(IscsiAIOCB
),
250 .cancel_async
= iscsi_aio_cancel
,
254 static void iscsi_process_read(void *arg
);
255 static void iscsi_process_write(void *arg
);
258 iscsi_set_events(IscsiLun
*iscsilun
)
260 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
261 int ev
= iscsi_which_events(iscsi
);
263 if (ev
!= iscsilun
->events
) {
264 aio_set_fd_handler(iscsilun
->aio_context
,
266 (ev
& POLLIN
) ? iscsi_process_read
: NULL
,
267 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
269 iscsilun
->events
= ev
;
272 /* newer versions of libiscsi may return zero events. In this
273 * case start a timer to ensure we are able to return to service
274 * once this situation changes. */
276 timer_mod(iscsilun
->event_timer
,
277 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
281 static void iscsi_timed_set_events(void *opaque
)
283 IscsiLun
*iscsilun
= opaque
;
284 iscsi_set_events(iscsilun
);
288 iscsi_process_read(void *arg
)
290 IscsiLun
*iscsilun
= arg
;
291 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
293 iscsi_service(iscsi
, POLLIN
);
294 iscsi_set_events(iscsilun
);
298 iscsi_process_write(void *arg
)
300 IscsiLun
*iscsilun
= arg
;
301 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
303 iscsi_service(iscsi
, POLLOUT
);
304 iscsi_set_events(iscsilun
);
307 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
309 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
312 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
314 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
317 static bool is_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
320 if ((sector_num
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
||
321 (nb_sectors
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
) {
322 error_report("iSCSI misaligned request: "
323 "iscsilun->block_size %u, sector_num %" PRIi64
325 iscsilun
->block_size
, sector_num
, nb_sectors
);
331 static unsigned long *iscsi_allocationmap_init(IscsiLun
*iscsilun
)
333 return bitmap_try_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun
->num_blocks
,
335 iscsilun
->cluster_sectors
));
338 static void iscsi_allocationmap_set(IscsiLun
*iscsilun
, int64_t sector_num
,
341 if (iscsilun
->allocationmap
== NULL
) {
344 bitmap_set(iscsilun
->allocationmap
,
345 sector_num
/ iscsilun
->cluster_sectors
,
346 DIV_ROUND_UP(nb_sectors
, iscsilun
->cluster_sectors
));
349 static void iscsi_allocationmap_clear(IscsiLun
*iscsilun
, int64_t sector_num
,
352 int64_t cluster_num
, nb_clusters
;
353 if (iscsilun
->allocationmap
== NULL
) {
356 cluster_num
= DIV_ROUND_UP(sector_num
, iscsilun
->cluster_sectors
);
357 nb_clusters
= (sector_num
+ nb_sectors
) / iscsilun
->cluster_sectors
359 if (nb_clusters
> 0) {
360 bitmap_clear(iscsilun
->allocationmap
, cluster_num
, nb_clusters
);
364 static int coroutine_fn
iscsi_co_writev(BlockDriverState
*bs
,
365 int64_t sector_num
, int nb_sectors
,
368 IscsiLun
*iscsilun
= bs
->opaque
;
369 struct IscsiTask iTask
;
371 uint32_t num_sectors
;
373 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
377 if (bs
->bl
.max_transfer_length
&& nb_sectors
> bs
->bl
.max_transfer_length
) {
378 error_report("iSCSI Error: Write of %d sectors exceeds max_xfer_len "
379 "of %d sectors", nb_sectors
, bs
->bl
.max_transfer_length
);
383 lba
= sector_qemu2lun(sector_num
, iscsilun
);
384 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
385 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
387 if (iscsilun
->use_16_for_rw
) {
388 iTask
.task
= iscsi_write16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
389 NULL
, num_sectors
* iscsilun
->block_size
,
390 iscsilun
->block_size
, 0, 0, 0, 0, 0,
391 iscsi_co_generic_cb
, &iTask
);
393 iTask
.task
= iscsi_write10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
394 NULL
, num_sectors
* iscsilun
->block_size
,
395 iscsilun
->block_size
, 0, 0, 0, 0, 0,
396 iscsi_co_generic_cb
, &iTask
);
398 if (iTask
.task
== NULL
) {
401 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
403 while (!iTask
.complete
) {
404 iscsi_set_events(iscsilun
);
405 qemu_coroutine_yield();
408 if (iTask
.task
!= NULL
) {
409 scsi_free_scsi_task(iTask
.task
);
413 if (iTask
.do_retry
) {
418 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
422 iscsi_allocationmap_set(iscsilun
, sector_num
, nb_sectors
);
428 static bool iscsi_allocationmap_is_allocated(IscsiLun
*iscsilun
,
429 int64_t sector_num
, int nb_sectors
)
432 if (iscsilun
->allocationmap
== NULL
) {
435 size
= DIV_ROUND_UP(sector_num
+ nb_sectors
, iscsilun
->cluster_sectors
);
436 return !(find_next_bit(iscsilun
->allocationmap
, size
,
437 sector_num
/ iscsilun
->cluster_sectors
) == size
);
440 static int64_t coroutine_fn
iscsi_co_get_block_status(BlockDriverState
*bs
,
442 int nb_sectors
, int *pnum
)
444 IscsiLun
*iscsilun
= bs
->opaque
;
445 struct scsi_get_lba_status
*lbas
= NULL
;
446 struct scsi_lba_status_descriptor
*lbasd
= NULL
;
447 struct IscsiTask iTask
;
450 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
452 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
457 /* default to all sectors allocated */
458 ret
= BDRV_BLOCK_DATA
;
459 ret
|= (sector_num
<< BDRV_SECTOR_BITS
) | BDRV_BLOCK_OFFSET_VALID
;
462 /* LUN does not support logical block provisioning */
463 if (!iscsilun
->lbpme
) {
468 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
469 sector_qemu2lun(sector_num
, iscsilun
),
470 8 + 16, iscsi_co_generic_cb
,
476 while (!iTask
.complete
) {
477 iscsi_set_events(iscsilun
);
478 qemu_coroutine_yield();
481 if (iTask
.do_retry
) {
482 if (iTask
.task
!= NULL
) {
483 scsi_free_scsi_task(iTask
.task
);
490 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
491 /* in case the get_lba_status_callout fails (i.e.
492 * because the device is busy or the cmd is not
493 * supported) we pretend all blocks are allocated
494 * for backwards compatibility */
498 lbas
= scsi_datain_unmarshall(iTask
.task
);
504 lbasd
= &lbas
->descriptors
[0];
506 if (sector_qemu2lun(sector_num
, iscsilun
) != lbasd
->lba
) {
511 *pnum
= sector_lun2qemu(lbasd
->num_blocks
, iscsilun
);
513 if (lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_DEALLOCATED
||
514 lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_ANCHORED
) {
515 ret
&= ~BDRV_BLOCK_DATA
;
516 if (iscsilun
->lbprz
) {
517 ret
|= BDRV_BLOCK_ZERO
;
521 if (ret
& BDRV_BLOCK_ZERO
) {
522 iscsi_allocationmap_clear(iscsilun
, sector_num
, *pnum
);
524 iscsi_allocationmap_set(iscsilun
, sector_num
, *pnum
);
527 if (*pnum
> nb_sectors
) {
531 if (iTask
.task
!= NULL
) {
532 scsi_free_scsi_task(iTask
.task
);
537 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
538 int64_t sector_num
, int nb_sectors
,
541 IscsiLun
*iscsilun
= bs
->opaque
;
542 struct IscsiTask iTask
;
544 uint32_t num_sectors
;
546 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
550 if (bs
->bl
.max_transfer_length
&& nb_sectors
> bs
->bl
.max_transfer_length
) {
551 error_report("iSCSI Error: Read of %d sectors exceeds max_xfer_len "
552 "of %d sectors", nb_sectors
, bs
->bl
.max_transfer_length
);
556 if (iscsilun
->lbprz
&& nb_sectors
>= ISCSI_CHECKALLOC_THRES
&&
557 !iscsi_allocationmap_is_allocated(iscsilun
, sector_num
, nb_sectors
)) {
560 ret
= iscsi_co_get_block_status(bs
, sector_num
, INT_MAX
, &pnum
);
564 if (ret
& BDRV_BLOCK_ZERO
&& pnum
>= nb_sectors
) {
565 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
570 lba
= sector_qemu2lun(sector_num
, iscsilun
);
571 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
573 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
575 if (iscsilun
->use_16_for_rw
) {
576 iTask
.task
= iscsi_read16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
577 num_sectors
* iscsilun
->block_size
,
578 iscsilun
->block_size
, 0, 0, 0, 0, 0,
579 iscsi_co_generic_cb
, &iTask
);
581 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
582 num_sectors
* iscsilun
->block_size
,
583 iscsilun
->block_size
,
585 iscsi_co_generic_cb
, &iTask
);
587 if (iTask
.task
== NULL
) {
590 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
592 while (!iTask
.complete
) {
593 iscsi_set_events(iscsilun
);
594 qemu_coroutine_yield();
597 if (iTask
.task
!= NULL
) {
598 scsi_free_scsi_task(iTask
.task
);
602 if (iTask
.do_retry
) {
607 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
614 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
616 IscsiLun
*iscsilun
= bs
->opaque
;
617 struct IscsiTask iTask
;
623 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
626 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
627 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
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
) {
655 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
656 void *command_data
, void *opaque
)
658 IscsiAIOCB
*acb
= opaque
;
665 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
666 iscsi_get_error(iscsi
));
670 acb
->ioh
->driver_status
= 0;
671 acb
->ioh
->host_status
= 0;
674 #define SG_ERR_DRIVER_SENSE 0x08
676 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
679 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
681 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
682 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
683 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
684 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
687 iscsi_schedule_bh(acb
);
690 static BlockAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
691 unsigned long int req
, void *buf
,
692 BlockCompletionFunc
*cb
, void *opaque
)
694 IscsiLun
*iscsilun
= bs
->opaque
;
695 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
696 struct iscsi_data data
;
699 assert(req
== SG_IO
);
701 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
703 acb
->iscsilun
= iscsilun
;
705 acb
->status
= -EINPROGRESS
;
709 acb
->task
= malloc(sizeof(struct scsi_task
));
710 if (acb
->task
== NULL
) {
711 error_report("iSCSI: Failed to allocate task for scsi command. %s",
712 iscsi_get_error(iscsi
));
716 memset(acb
->task
, 0, sizeof(struct scsi_task
));
718 switch (acb
->ioh
->dxfer_direction
) {
719 case SG_DXFER_TO_DEV
:
720 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
722 case SG_DXFER_FROM_DEV
:
723 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
726 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
730 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
731 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
732 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
735 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
736 if (acb
->ioh
->iovec_count
== 0) {
737 data
.data
= acb
->ioh
->dxferp
;
738 data
.size
= acb
->ioh
->dxfer_len
;
740 scsi_task_set_iov_out(acb
->task
,
741 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
742 acb
->ioh
->iovec_count
);
746 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
748 (data
.size
> 0) ? &data
: NULL
,
750 scsi_free_scsi_task(acb
->task
);
755 /* tell libiscsi to read straight into the buffer we got from ioctl */
756 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
757 if (acb
->ioh
->iovec_count
== 0) {
758 scsi_task_add_data_in_buffer(acb
->task
,
762 scsi_task_set_iov_in(acb
->task
,
763 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
764 acb
->ioh
->iovec_count
);
768 iscsi_set_events(iscsilun
);
773 static void ioctl_cb(void *opaque
, int status
)
775 int *p_status
= opaque
;
779 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
781 IscsiLun
*iscsilun
= bs
->opaque
;
785 case SG_GET_VERSION_NUM
:
789 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
792 status
= -EINPROGRESS
;
793 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
795 while (status
== -EINPROGRESS
) {
796 aio_poll(iscsilun
->aio_context
, true);
808 iscsi_getlength(BlockDriverState
*bs
)
810 IscsiLun
*iscsilun
= bs
->opaque
;
813 len
= iscsilun
->num_blocks
;
814 len
*= iscsilun
->block_size
;
820 coroutine_fn
iscsi_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
823 IscsiLun
*iscsilun
= bs
->opaque
;
824 struct IscsiTask iTask
;
825 struct unmap_list list
;
827 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
831 if (!iscsilun
->lbp
.lbpu
) {
832 /* UNMAP is not supported by the target */
836 list
.lba
= sector_qemu2lun(sector_num
, iscsilun
);
837 list
.num
= sector_qemu2lun(nb_sectors
, iscsilun
);
839 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
841 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
842 iscsi_co_generic_cb
, &iTask
) == NULL
) {
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_CHECK_CONDITION
) {
862 /* the target might fail with a check condition if it
863 is not happy with the alignment of the UNMAP request
864 we silently fail in this case */
868 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
872 iscsi_allocationmap_clear(iscsilun
, sector_num
, nb_sectors
);
878 coroutine_fn
iscsi_co_write_zeroes(BlockDriverState
*bs
, int64_t sector_num
,
879 int nb_sectors
, BdrvRequestFlags flags
)
881 IscsiLun
*iscsilun
= bs
->opaque
;
882 struct IscsiTask iTask
;
885 bool use_16_for_ws
= iscsilun
->use_16_for_rw
;
887 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
891 if (flags
& BDRV_REQ_MAY_UNMAP
) {
892 if (!use_16_for_ws
&& !iscsilun
->lbp
.lbpws10
) {
893 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
894 use_16_for_ws
= true;
896 if (use_16_for_ws
&& !iscsilun
->lbp
.lbpws
) {
897 /* WRITESAME16 with UNMAP is not supported by the target,
898 * fall back and try WRITESAME10/16 without UNMAP */
899 flags
&= ~BDRV_REQ_MAY_UNMAP
;
900 use_16_for_ws
= iscsilun
->use_16_for_rw
;
904 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
905 /* WRITESAME without UNMAP is not supported by the target */
909 lba
= sector_qemu2lun(sector_num
, iscsilun
);
910 nb_blocks
= sector_qemu2lun(nb_sectors
, iscsilun
);
912 if (iscsilun
->zeroblock
== NULL
) {
913 iscsilun
->zeroblock
= g_try_malloc0(iscsilun
->block_size
);
914 if (iscsilun
->zeroblock
== NULL
) {
919 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
922 iTask
.task
= iscsi_writesame16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
923 iscsilun
->zeroblock
, iscsilun
->block_size
,
924 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
925 0, 0, iscsi_co_generic_cb
, &iTask
);
927 iTask
.task
= iscsi_writesame10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
928 iscsilun
->zeroblock
, iscsilun
->block_size
,
929 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
930 0, 0, iscsi_co_generic_cb
, &iTask
);
932 if (iTask
.task
== NULL
) {
936 while (!iTask
.complete
) {
937 iscsi_set_events(iscsilun
);
938 qemu_coroutine_yield();
941 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
&&
942 iTask
.task
->sense
.key
== SCSI_SENSE_ILLEGAL_REQUEST
&&
943 (iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
||
944 iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
)) {
945 /* WRITE SAME is not supported by the target */
946 iscsilun
->has_write_same
= false;
947 scsi_free_scsi_task(iTask
.task
);
951 if (iTask
.task
!= NULL
) {
952 scsi_free_scsi_task(iTask
.task
);
956 if (iTask
.do_retry
) {
961 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
965 if (flags
& BDRV_REQ_MAY_UNMAP
) {
966 iscsi_allocationmap_clear(iscsilun
, sector_num
, nb_sectors
);
968 iscsi_allocationmap_set(iscsilun
, sector_num
, nb_sectors
);
974 static void parse_chap(struct iscsi_context
*iscsi
, const char *target
,
979 const char *user
= NULL
;
980 const char *password
= NULL
;
982 list
= qemu_find_opts("iscsi");
987 opts
= qemu_opts_find(list
, target
);
989 opts
= QTAILQ_FIRST(&list
->head
);
995 user
= qemu_opt_get(opts
, "user");
1000 password
= qemu_opt_get(opts
, "password");
1002 error_setg(errp
, "CHAP username specified but no password was given");
1006 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
1007 error_setg(errp
, "Failed to set initiator username and password");
1011 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
,
1016 const char *digest
= NULL
;
1018 list
= qemu_find_opts("iscsi");
1023 opts
= qemu_opts_find(list
, target
);
1025 opts
= QTAILQ_FIRST(&list
->head
);
1031 digest
= qemu_opt_get(opts
, "header-digest");
1036 if (!strcmp(digest
, "CRC32C")) {
1037 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
1038 } else if (!strcmp(digest
, "NONE")) {
1039 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
1040 } else if (!strcmp(digest
, "CRC32C-NONE")) {
1041 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
1042 } else if (!strcmp(digest
, "NONE-CRC32C")) {
1043 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1045 error_setg(errp
, "Invalid header-digest setting : %s", digest
);
1049 static char *parse_initiator_name(const char *target
)
1055 UuidInfo
*uuid_info
;
1057 list
= qemu_find_opts("iscsi");
1059 opts
= qemu_opts_find(list
, target
);
1061 opts
= QTAILQ_FIRST(&list
->head
);
1064 name
= qemu_opt_get(opts
, "initiator-name");
1066 return g_strdup(name
);
1071 uuid_info
= qmp_query_uuid(NULL
);
1072 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
1073 name
= qemu_get_vm_name();
1075 name
= uuid_info
->UUID
;
1077 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1078 name
? ":" : "", name
? name
: "");
1079 qapi_free_UuidInfo(uuid_info
);
1083 static void iscsi_nop_timed_event(void *opaque
)
1085 IscsiLun
*iscsilun
= opaque
;
1087 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) > MAX_NOP_FAILURES
) {
1088 error_report("iSCSI: NOP timeout. Reconnecting...");
1089 iscsi_reconnect(iscsilun
->iscsi
);
1092 if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
1093 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1097 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1098 iscsi_set_events(iscsilun
);
1101 static void iscsi_readcapacity_sync(IscsiLun
*iscsilun
, Error
**errp
)
1103 struct scsi_task
*task
= NULL
;
1104 struct scsi_readcapacity10
*rc10
= NULL
;
1105 struct scsi_readcapacity16
*rc16
= NULL
;
1106 int retries
= ISCSI_CMD_RETRIES
;
1110 scsi_free_scsi_task(task
);
1114 switch (iscsilun
->type
) {
1116 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
1117 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1118 rc16
= scsi_datain_unmarshall(task
);
1120 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity16 data.");
1122 iscsilun
->block_size
= rc16
->block_length
;
1123 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1124 iscsilun
->lbpme
= !!rc16
->lbpme
;
1125 iscsilun
->lbprz
= !!rc16
->lbprz
;
1126 iscsilun
->use_16_for_rw
= (rc16
->returned_lba
> 0xffffffff);
1131 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1132 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1133 rc10
= scsi_datain_unmarshall(task
);
1135 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity10 data.");
1137 iscsilun
->block_size
= rc10
->block_size
;
1138 if (rc10
->lba
== 0) {
1139 /* blank disk loaded */
1140 iscsilun
->num_blocks
= 0;
1142 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1150 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1151 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1154 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1155 error_setg(errp
, "iSCSI: failed to send readcapacity10 command.");
1158 scsi_free_scsi_task(task
);
1162 /* TODO Convert to fine grained options */
1163 static QemuOptsList runtime_opts
= {
1165 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1169 .type
= QEMU_OPT_STRING
,
1170 .help
= "URL to the iscsi image",
1172 { /* end of list */ }
1176 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
, int lun
,
1177 int evpd
, int pc
, void **inq
, Error
**errp
)
1180 struct scsi_task
*task
= NULL
;
1181 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, 64);
1182 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1185 full_size
= scsi_datain_getfullsize(task
);
1186 if (full_size
> task
->datain
.size
) {
1187 scsi_free_scsi_task(task
);
1189 /* we need more data for the full list */
1190 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, full_size
);
1191 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1196 *inq
= scsi_datain_unmarshall(task
);
1198 error_setg(errp
, "iSCSI: failed to unmarshall inquiry datain blob");
1205 error_setg(errp
, "iSCSI: Inquiry command failed : %s",
1206 iscsi_get_error(iscsi
));
1209 scsi_free_scsi_task(task
);
1214 static void iscsi_detach_aio_context(BlockDriverState
*bs
)
1216 IscsiLun
*iscsilun
= bs
->opaque
;
1218 aio_set_fd_handler(iscsilun
->aio_context
,
1219 iscsi_get_fd(iscsilun
->iscsi
),
1221 iscsilun
->events
= 0;
1223 if (iscsilun
->nop_timer
) {
1224 timer_del(iscsilun
->nop_timer
);
1225 timer_free(iscsilun
->nop_timer
);
1226 iscsilun
->nop_timer
= NULL
;
1228 if (iscsilun
->event_timer
) {
1229 timer_del(iscsilun
->event_timer
);
1230 timer_free(iscsilun
->event_timer
);
1231 iscsilun
->event_timer
= NULL
;
1235 static void iscsi_attach_aio_context(BlockDriverState
*bs
,
1236 AioContext
*new_context
)
1238 IscsiLun
*iscsilun
= bs
->opaque
;
1240 iscsilun
->aio_context
= new_context
;
1241 iscsi_set_events(iscsilun
);
1243 /* Set up a timer for sending out iSCSI NOPs */
1244 iscsilun
->nop_timer
= aio_timer_new(iscsilun
->aio_context
,
1245 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1246 iscsi_nop_timed_event
, iscsilun
);
1247 timer_mod(iscsilun
->nop_timer
,
1248 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1250 /* Prepare a timer for a delayed call to iscsi_set_events */
1251 iscsilun
->event_timer
= aio_timer_new(iscsilun
->aio_context
,
1252 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1253 iscsi_timed_set_events
, iscsilun
);
1256 static void iscsi_modesense_sync(IscsiLun
*iscsilun
)
1258 struct scsi_task
*task
;
1259 struct scsi_mode_sense
*ms
= NULL
;
1260 iscsilun
->write_protected
= false;
1262 task
= iscsi_modesense6_sync(iscsilun
->iscsi
, iscsilun
->lun
,
1263 1, SCSI_MODESENSE_PC_CURRENT
,
1266 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1267 iscsi_get_error(iscsilun
->iscsi
));
1271 if (task
->status
!= SCSI_STATUS_GOOD
) {
1272 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1275 ms
= scsi_datain_unmarshall(task
);
1277 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1278 iscsi_get_error(iscsilun
->iscsi
));
1281 iscsilun
->write_protected
= ms
->device_specific_parameter
& 0x80;
1285 scsi_free_scsi_task(task
);
1290 * We support iscsi url's on the form
1291 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1293 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1296 IscsiLun
*iscsilun
= bs
->opaque
;
1297 struct iscsi_context
*iscsi
= NULL
;
1298 struct iscsi_url
*iscsi_url
= NULL
;
1299 struct scsi_task
*task
= NULL
;
1300 struct scsi_inquiry_standard
*inq
= NULL
;
1301 struct scsi_inquiry_supported_pages
*inq_vpd
;
1302 char *initiator_name
= NULL
;
1304 Error
*local_err
= NULL
;
1305 const char *filename
;
1308 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
1309 error_setg(errp
, "iSCSI: Invalid BDRV_SECTOR_SIZE. "
1310 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1311 "of 512", BDRV_SECTOR_SIZE
);
1315 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1316 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1318 error_propagate(errp
, local_err
);
1323 filename
= qemu_opt_get(opts
, "filename");
1325 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1326 if (iscsi_url
== NULL
) {
1327 error_setg(errp
, "Failed to parse URL : %s", filename
);
1332 memset(iscsilun
, 0, sizeof(IscsiLun
));
1334 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1336 iscsi
= iscsi_create_context(initiator_name
);
1337 if (iscsi
== NULL
) {
1338 error_setg(errp
, "iSCSI: Failed to create iSCSI context.");
1343 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1344 error_setg(errp
, "iSCSI: Failed to set target name.");
1349 if (iscsi_url
->user
[0] != '\0') {
1350 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1353 error_setg(errp
, "Failed to set initiator username and password");
1359 /* check if we got CHAP username/password via the options */
1360 parse_chap(iscsi
, iscsi_url
->target
, &local_err
);
1361 if (local_err
!= NULL
) {
1362 error_propagate(errp
, local_err
);
1367 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1368 error_setg(errp
, "iSCSI: Failed to set session type to normal.");
1373 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1375 /* check if we got HEADER_DIGEST via the options */
1376 parse_header_digest(iscsi
, iscsi_url
->target
, &local_err
);
1377 if (local_err
!= NULL
) {
1378 error_propagate(errp
, local_err
);
1383 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1384 error_setg(errp
, "iSCSI: Failed to connect to LUN : %s",
1385 iscsi_get_error(iscsi
));
1390 iscsilun
->iscsi
= iscsi
;
1391 iscsilun
->aio_context
= bdrv_get_aio_context(bs
);
1392 iscsilun
->lun
= iscsi_url
->lun
;
1393 iscsilun
->has_write_same
= true;
1395 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0,
1396 (void **) &inq
, errp
);
1401 iscsilun
->type
= inq
->periperal_device_type
;
1402 scsi_free_scsi_task(task
);
1405 iscsi_modesense_sync(iscsilun
);
1407 /* Check the write protect flag of the LUN if we want to write */
1408 if (iscsilun
->type
== TYPE_DISK
&& (flags
& BDRV_O_RDWR
) &&
1409 iscsilun
->write_protected
) {
1410 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1415 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1416 if (local_err
!= NULL
) {
1417 error_propagate(errp
, local_err
);
1421 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1422 bs
->request_alignment
= iscsilun
->block_size
;
1424 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1425 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1426 * will try to read from the device to guess the image format.
1428 if (iscsilun
->type
!= TYPE_DISK
&& iscsilun
->type
!= TYPE_ROM
) {
1432 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1433 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES
,
1434 (void **) &inq_vpd
, errp
);
1439 for (i
= 0; i
< inq_vpd
->num_pages
; i
++) {
1440 struct scsi_task
*inq_task
;
1441 struct scsi_inquiry_logical_block_provisioning
*inq_lbp
;
1442 struct scsi_inquiry_block_limits
*inq_bl
;
1443 switch (inq_vpd
->pages
[i
]) {
1444 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
:
1445 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1446 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
,
1447 (void **) &inq_lbp
, errp
);
1448 if (inq_task
== NULL
) {
1452 memcpy(&iscsilun
->lbp
, inq_lbp
,
1453 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1454 scsi_free_scsi_task(inq_task
);
1456 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
:
1457 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1458 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
,
1459 (void **) &inq_bl
, errp
);
1460 if (inq_task
== NULL
) {
1464 memcpy(&iscsilun
->bl
, inq_bl
,
1465 sizeof(struct scsi_inquiry_block_limits
));
1466 scsi_free_scsi_task(inq_task
);
1472 scsi_free_scsi_task(task
);
1475 iscsi_attach_aio_context(bs
, iscsilun
->aio_context
);
1477 /* Guess the internal cluster (page) size of the iscsi target by the means
1478 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1479 * reasonable size */
1480 if (iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
>= 4 * 1024 &&
1481 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
<= 16 * 1024 * 1024) {
1482 iscsilun
->cluster_sectors
= (iscsilun
->bl
.opt_unmap_gran
*
1483 iscsilun
->block_size
) >> BDRV_SECTOR_BITS
;
1484 if (iscsilun
->lbprz
&& !(bs
->open_flags
& BDRV_O_NOCACHE
)) {
1485 iscsilun
->allocationmap
= iscsi_allocationmap_init(iscsilun
);
1486 if (iscsilun
->allocationmap
== NULL
) {
1493 qemu_opts_del(opts
);
1494 g_free(initiator_name
);
1495 if (iscsi_url
!= NULL
) {
1496 iscsi_destroy_url(iscsi_url
);
1499 scsi_free_scsi_task(task
);
1503 if (iscsi
!= NULL
) {
1504 if (iscsi_is_logged_in(iscsi
)) {
1505 iscsi_logout_sync(iscsi
);
1507 iscsi_destroy_context(iscsi
);
1509 memset(iscsilun
, 0, sizeof(IscsiLun
));
1514 static void iscsi_close(BlockDriverState
*bs
)
1516 IscsiLun
*iscsilun
= bs
->opaque
;
1517 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1519 iscsi_detach_aio_context(bs
);
1520 if (iscsi_is_logged_in(iscsi
)) {
1521 iscsi_logout_sync(iscsi
);
1523 iscsi_destroy_context(iscsi
);
1524 g_free(iscsilun
->zeroblock
);
1525 g_free(iscsilun
->allocationmap
);
1526 memset(iscsilun
, 0, sizeof(IscsiLun
));
1529 static int sector_limits_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
1531 return MIN(sector_lun2qemu(sector
, iscsilun
), INT_MAX
/ 2 + 1);
1534 static void iscsi_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1536 /* We don't actually refresh here, but just return data queried in
1537 * iscsi_open(): iscsi targets don't change their limits. */
1539 IscsiLun
*iscsilun
= bs
->opaque
;
1540 uint32_t max_xfer_len
= iscsilun
->use_16_for_rw
? 0xffffffff : 0xffff;
1542 if (iscsilun
->bl
.max_xfer_len
) {
1543 max_xfer_len
= MIN(max_xfer_len
, iscsilun
->bl
.max_xfer_len
);
1546 bs
->bl
.max_transfer_length
= sector_limits_lun2qemu(max_xfer_len
, iscsilun
);
1548 if (iscsilun
->lbp
.lbpu
) {
1549 if (iscsilun
->bl
.max_unmap
< 0xffffffff) {
1550 bs
->bl
.max_discard
=
1551 sector_limits_lun2qemu(iscsilun
->bl
.max_unmap
, iscsilun
);
1553 bs
->bl
.discard_alignment
=
1554 sector_limits_lun2qemu(iscsilun
->bl
.opt_unmap_gran
, iscsilun
);
1557 if (iscsilun
->bl
.max_ws_len
< 0xffffffff) {
1558 bs
->bl
.max_write_zeroes
=
1559 sector_limits_lun2qemu(iscsilun
->bl
.max_ws_len
, iscsilun
);
1561 if (iscsilun
->lbp
.lbpws
) {
1562 bs
->bl
.write_zeroes_alignment
=
1563 sector_limits_lun2qemu(iscsilun
->bl
.opt_unmap_gran
, iscsilun
);
1565 bs
->bl
.opt_transfer_length
=
1566 sector_limits_lun2qemu(iscsilun
->bl
.opt_xfer_len
, iscsilun
);
1569 /* Note that this will not re-establish a connection with an iSCSI target - it
1570 * is effectively a NOP. */
1571 static int iscsi_reopen_prepare(BDRVReopenState
*state
,
1572 BlockReopenQueue
*queue
, Error
**errp
)
1574 IscsiLun
*iscsilun
= state
->bs
->opaque
;
1576 if (state
->flags
& BDRV_O_RDWR
&& iscsilun
->write_protected
) {
1577 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1583 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1585 IscsiLun
*iscsilun
= bs
->opaque
;
1586 Error
*local_err
= NULL
;
1588 if (iscsilun
->type
!= TYPE_DISK
) {
1592 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1593 if (local_err
!= NULL
) {
1594 error_free(local_err
);
1598 if (offset
> iscsi_getlength(bs
)) {
1602 if (iscsilun
->allocationmap
!= NULL
) {
1603 g_free(iscsilun
->allocationmap
);
1604 iscsilun
->allocationmap
= iscsi_allocationmap_init(iscsilun
);
1610 static int iscsi_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1613 int64_t total_size
= 0;
1614 BlockDriverState
*bs
;
1615 IscsiLun
*iscsilun
= NULL
;
1620 /* Read out options */
1621 total_size
= DIV_ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1623 bs
->opaque
= g_new0(struct IscsiLun
, 1);
1624 iscsilun
= bs
->opaque
;
1626 bs_options
= qdict_new();
1627 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1628 ret
= iscsi_open(bs
, bs_options
, 0, NULL
);
1629 QDECREF(bs_options
);
1634 iscsi_detach_aio_context(bs
);
1635 if (iscsilun
->type
!= TYPE_DISK
) {
1639 if (bs
->total_sectors
< total_size
) {
1646 if (iscsilun
->iscsi
!= NULL
) {
1647 iscsi_destroy_context(iscsilun
->iscsi
);
1655 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1657 IscsiLun
*iscsilun
= bs
->opaque
;
1658 bdi
->unallocated_blocks_are_zero
= iscsilun
->lbprz
;
1659 bdi
->can_write_zeroes_with_unmap
= iscsilun
->lbprz
&& iscsilun
->lbp
.lbpws
;
1660 bdi
->cluster_size
= iscsilun
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1664 static QemuOptsList iscsi_create_opts
= {
1665 .name
= "iscsi-create-opts",
1666 .head
= QTAILQ_HEAD_INITIALIZER(iscsi_create_opts
.head
),
1669 .name
= BLOCK_OPT_SIZE
,
1670 .type
= QEMU_OPT_SIZE
,
1671 .help
= "Virtual disk size"
1673 { /* end of list */ }
1677 static BlockDriver bdrv_iscsi
= {
1678 .format_name
= "iscsi",
1679 .protocol_name
= "iscsi",
1681 .instance_size
= sizeof(IscsiLun
),
1682 .bdrv_needs_filename
= true,
1683 .bdrv_file_open
= iscsi_open
,
1684 .bdrv_close
= iscsi_close
,
1685 .bdrv_create
= iscsi_create
,
1686 .create_opts
= &iscsi_create_opts
,
1687 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
1689 .bdrv_getlength
= iscsi_getlength
,
1690 .bdrv_get_info
= iscsi_get_info
,
1691 .bdrv_truncate
= iscsi_truncate
,
1692 .bdrv_refresh_limits
= iscsi_refresh_limits
,
1694 .bdrv_co_get_block_status
= iscsi_co_get_block_status
,
1695 .bdrv_co_discard
= iscsi_co_discard
,
1696 .bdrv_co_write_zeroes
= iscsi_co_write_zeroes
,
1697 .bdrv_co_readv
= iscsi_co_readv
,
1698 .bdrv_co_writev
= iscsi_co_writev
,
1699 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
1702 .bdrv_ioctl
= iscsi_ioctl
,
1703 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1706 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
1707 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
1710 static QemuOptsList qemu_iscsi_opts
= {
1712 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1716 .type
= QEMU_OPT_STRING
,
1717 .help
= "username for CHAP authentication to target",
1720 .type
= QEMU_OPT_STRING
,
1721 .help
= "password for CHAP authentication to target",
1723 .name
= "header-digest",
1724 .type
= QEMU_OPT_STRING
,
1725 .help
= "HeaderDigest setting. "
1726 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1728 .name
= "initiator-name",
1729 .type
= QEMU_OPT_STRING
,
1730 .help
= "Initiator iqn name to use when connecting",
1732 { /* end of list */ }
1736 static void iscsi_block_init(void)
1738 bdrv_register(&bdrv_iscsi
);
1739 qemu_add_opts(&qemu_iscsi_opts
);
1742 block_init(iscsi_block_init
);