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"
43 #include <iscsi/iscsi.h>
44 #include <iscsi/scsi-lowlevel.h>
48 #include <block/scsi.h>
51 typedef struct IscsiLun
{
52 struct iscsi_context
*iscsi
;
53 AioContext
*aio_context
;
55 enum scsi_inquiry_peripheral_device_type type
;
60 QEMUTimer
*event_timer
;
61 struct scsi_inquiry_logical_block_provisioning lbp
;
62 struct scsi_inquiry_block_limits bl
;
63 unsigned char *zeroblock
;
64 unsigned long *allocationmap
;
72 bool force_next_flush
;
73 bool request_timed_out
;
76 typedef struct IscsiTask
{
81 struct scsi_task
*task
;
85 QEMUTimer retry_timer
;
86 bool force_next_flush
;
90 typedef struct IscsiAIOCB
{
95 struct scsi_task
*task
;
106 /* libiscsi uses time_t so its enough to process events every second */
107 #define EVENT_INTERVAL 1000
108 #define NOP_INTERVAL 5000
109 #define MAX_NOP_FAILURES 3
110 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
111 static const unsigned iscsi_retry_times
[] = {8, 32, 128, 512, 2048, 8192, 32768};
113 /* this threshold is a trade-off knob to choose between
114 * the potential additional overhead of an extra GET_LBA_STATUS request
115 * vs. unnecessarily reading a lot of zero sectors over the wire.
116 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
117 * sectors we check the allocation status of the area covered by the
118 * request first if the allocationmap indicates that the area might be
120 #define ISCSI_CHECKALLOC_THRES 64
127 qemu_bh_delete(acb
->bh
);
132 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
134 if (acb
->task
!= NULL
) {
135 scsi_free_scsi_task(acb
->task
);
143 iscsi_schedule_bh(IscsiAIOCB
*acb
)
148 acb
->bh
= aio_bh_new(acb
->iscsilun
->aio_context
, iscsi_bh_cb
, acb
);
149 qemu_bh_schedule(acb
->bh
);
152 static void iscsi_co_generic_bh_cb(void *opaque
)
154 struct IscsiTask
*iTask
= opaque
;
156 qemu_bh_delete(iTask
->bh
);
157 qemu_coroutine_enter(iTask
->co
, NULL
);
160 static void iscsi_retry_timer_expired(void *opaque
)
162 struct IscsiTask
*iTask
= opaque
;
165 qemu_coroutine_enter(iTask
->co
, NULL
);
169 static inline unsigned exp_random(double mean
)
171 return -mean
* log((double)rand() / RAND_MAX
);
174 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
175 * libiscsi 1.10.0, together with other constants we need. Use it as
176 * a hint that we have to define them ourselves if needed, to keep the
177 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
178 * the test because SCSI_STATUS_* is an enum.
180 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
181 * an enum, check against the LIBISCSI_API_VERSION macro, which was
182 * introduced in 1.11.0. If it is present, there is no need to define
185 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
186 !defined(LIBISCSI_API_VERSION)
187 #define SCSI_STATUS_TASK_SET_FULL 0x28
188 #define SCSI_STATUS_TIMEOUT 0x0f000002
189 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
190 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
193 static int iscsi_translate_sense(struct scsi_sense
*sense
)
197 switch (sense
->key
) {
198 case SCSI_SENSE_NOT_READY
:
200 case SCSI_SENSE_DATA_PROTECTION
:
202 case SCSI_SENSE_COMMAND_ABORTED
:
204 case SCSI_SENSE_ILLEGAL_REQUEST
:
210 switch (sense
->ascq
) {
211 case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR
:
212 case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
:
213 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
:
214 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST
:
217 case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE
:
220 case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED
:
223 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT
:
224 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED
:
225 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN
:
228 case SCSI_SENSE_ASCQ_WRITE_PROTECTED
:
239 iscsi_co_generic_cb(struct iscsi_context
*iscsi
, int status
,
240 void *command_data
, void *opaque
)
242 struct IscsiTask
*iTask
= opaque
;
243 struct scsi_task
*task
= command_data
;
245 iTask
->status
= status
;
249 if (status
!= SCSI_STATUS_GOOD
) {
250 if (iTask
->retries
++ < ISCSI_CMD_RETRIES
) {
251 if (status
== SCSI_STATUS_CHECK_CONDITION
252 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
253 error_report("iSCSI CheckCondition: %s",
254 iscsi_get_error(iscsi
));
258 if (status
== SCSI_STATUS_BUSY
||
259 status
== SCSI_STATUS_TIMEOUT
||
260 status
== SCSI_STATUS_TASK_SET_FULL
) {
261 unsigned retry_time
=
262 exp_random(iscsi_retry_times
[iTask
->retries
- 1]);
263 if (status
== SCSI_STATUS_TIMEOUT
) {
264 /* make sure the request is rescheduled AFTER the
265 * reconnect is initiated */
266 retry_time
= EVENT_INTERVAL
* 2;
267 iTask
->iscsilun
->request_timed_out
= true;
269 error_report("iSCSI Busy/TaskSetFull/TimeOut"
270 " (retry #%u in %u ms): %s",
271 iTask
->retries
, retry_time
,
272 iscsi_get_error(iscsi
));
273 aio_timer_init(iTask
->iscsilun
->aio_context
,
274 &iTask
->retry_timer
, QEMU_CLOCK_REALTIME
,
275 SCALE_MS
, iscsi_retry_timer_expired
, iTask
);
276 timer_mod(&iTask
->retry_timer
,
277 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + retry_time
);
282 iTask
->err_code
= iscsi_translate_sense(&task
->sense
);
283 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi
));
285 iTask
->iscsilun
->force_next_flush
|= iTask
->force_next_flush
;
290 iTask
->bh
= aio_bh_new(iTask
->iscsilun
->aio_context
,
291 iscsi_co_generic_bh_cb
, iTask
);
292 qemu_bh_schedule(iTask
->bh
);
298 static void iscsi_co_init_iscsitask(IscsiLun
*iscsilun
, struct IscsiTask
*iTask
)
300 *iTask
= (struct IscsiTask
) {
301 .co
= qemu_coroutine_self(),
302 .iscsilun
= iscsilun
,
307 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
310 IscsiAIOCB
*acb
= private_data
;
312 acb
->status
= -ECANCELED
;
313 iscsi_schedule_bh(acb
);
317 iscsi_aio_cancel(BlockAIOCB
*blockacb
)
319 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
320 IscsiLun
*iscsilun
= acb
->iscsilun
;
322 if (acb
->status
!= -EINPROGRESS
) {
326 /* send a task mgmt call to the target to cancel the task on the target */
327 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
328 iscsi_abort_task_cb
, acb
);
332 static const AIOCBInfo iscsi_aiocb_info
= {
333 .aiocb_size
= sizeof(IscsiAIOCB
),
334 .cancel_async
= iscsi_aio_cancel
,
338 static void iscsi_process_read(void *arg
);
339 static void iscsi_process_write(void *arg
);
342 iscsi_set_events(IscsiLun
*iscsilun
)
344 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
345 int ev
= iscsi_which_events(iscsi
);
347 if (ev
!= iscsilun
->events
) {
348 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsi
),
350 (ev
& POLLIN
) ? iscsi_process_read
: NULL
,
351 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
353 iscsilun
->events
= ev
;
357 static void iscsi_timed_check_events(void *opaque
)
359 IscsiLun
*iscsilun
= opaque
;
361 /* check for timed out requests */
362 iscsi_service(iscsilun
->iscsi
, 0);
364 if (iscsilun
->request_timed_out
) {
365 iscsilun
->request_timed_out
= false;
366 iscsi_reconnect(iscsilun
->iscsi
);
369 /* newer versions of libiscsi may return zero events. Ensure we are able
370 * to return to service once this situation changes. */
371 iscsi_set_events(iscsilun
);
373 timer_mod(iscsilun
->event_timer
,
374 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
378 iscsi_process_read(void *arg
)
380 IscsiLun
*iscsilun
= arg
;
381 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
383 iscsi_service(iscsi
, POLLIN
);
384 iscsi_set_events(iscsilun
);
388 iscsi_process_write(void *arg
)
390 IscsiLun
*iscsilun
= arg
;
391 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
393 iscsi_service(iscsi
, POLLOUT
);
394 iscsi_set_events(iscsilun
);
397 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
399 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
402 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
404 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
407 static bool is_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
410 if ((sector_num
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
||
411 (nb_sectors
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
) {
412 error_report("iSCSI misaligned request: "
413 "iscsilun->block_size %u, sector_num %" PRIi64
415 iscsilun
->block_size
, sector_num
, nb_sectors
);
421 static unsigned long *iscsi_allocationmap_init(IscsiLun
*iscsilun
)
423 return bitmap_try_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun
->num_blocks
,
425 iscsilun
->cluster_sectors
));
428 static void iscsi_allocationmap_set(IscsiLun
*iscsilun
, int64_t sector_num
,
431 if (iscsilun
->allocationmap
== NULL
) {
434 bitmap_set(iscsilun
->allocationmap
,
435 sector_num
/ iscsilun
->cluster_sectors
,
436 DIV_ROUND_UP(nb_sectors
, iscsilun
->cluster_sectors
));
439 static void iscsi_allocationmap_clear(IscsiLun
*iscsilun
, int64_t sector_num
,
442 int64_t cluster_num
, nb_clusters
;
443 if (iscsilun
->allocationmap
== NULL
) {
446 cluster_num
= DIV_ROUND_UP(sector_num
, iscsilun
->cluster_sectors
);
447 nb_clusters
= (sector_num
+ nb_sectors
) / iscsilun
->cluster_sectors
449 if (nb_clusters
> 0) {
450 bitmap_clear(iscsilun
->allocationmap
, cluster_num
, nb_clusters
);
454 static int coroutine_fn
iscsi_co_writev(BlockDriverState
*bs
,
455 int64_t sector_num
, int nb_sectors
,
458 IscsiLun
*iscsilun
= bs
->opaque
;
459 struct IscsiTask iTask
;
461 uint32_t num_sectors
;
464 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
468 if (bs
->bl
.max_transfer_length
&& nb_sectors
> bs
->bl
.max_transfer_length
) {
469 error_report("iSCSI Error: Write of %d sectors exceeds max_xfer_len "
470 "of %d sectors", nb_sectors
, bs
->bl
.max_transfer_length
);
474 lba
= sector_qemu2lun(sector_num
, iscsilun
);
475 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
476 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
478 fua
= iscsilun
->dpofua
&& !bs
->enable_write_cache
;
479 iTask
.force_next_flush
= !fua
;
480 if (iscsilun
->use_16_for_rw
) {
481 iTask
.task
= iscsi_write16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
482 NULL
, num_sectors
* iscsilun
->block_size
,
483 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
484 iscsi_co_generic_cb
, &iTask
);
486 iTask
.task
= iscsi_write10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
487 NULL
, num_sectors
* iscsilun
->block_size
,
488 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
489 iscsi_co_generic_cb
, &iTask
);
491 if (iTask
.task
== NULL
) {
494 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
496 while (!iTask
.complete
) {
497 iscsi_set_events(iscsilun
);
498 qemu_coroutine_yield();
501 if (iTask
.task
!= NULL
) {
502 scsi_free_scsi_task(iTask
.task
);
506 if (iTask
.do_retry
) {
511 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
512 return iTask
.err_code
;
515 iscsi_allocationmap_set(iscsilun
, sector_num
, nb_sectors
);
521 static bool iscsi_allocationmap_is_allocated(IscsiLun
*iscsilun
,
522 int64_t sector_num
, int nb_sectors
)
525 if (iscsilun
->allocationmap
== NULL
) {
528 size
= DIV_ROUND_UP(sector_num
+ nb_sectors
, iscsilun
->cluster_sectors
);
529 return !(find_next_bit(iscsilun
->allocationmap
, size
,
530 sector_num
/ iscsilun
->cluster_sectors
) == size
);
533 static int64_t coroutine_fn
iscsi_co_get_block_status(BlockDriverState
*bs
,
535 int nb_sectors
, int *pnum
,
536 BlockDriverState
**file
)
538 IscsiLun
*iscsilun
= bs
->opaque
;
539 struct scsi_get_lba_status
*lbas
= NULL
;
540 struct scsi_lba_status_descriptor
*lbasd
= NULL
;
541 struct IscsiTask iTask
;
544 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
546 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
551 /* default to all sectors allocated */
552 ret
= BDRV_BLOCK_DATA
;
553 ret
|= (sector_num
<< BDRV_SECTOR_BITS
) | BDRV_BLOCK_OFFSET_VALID
;
556 /* LUN does not support logical block provisioning */
557 if (!iscsilun
->lbpme
) {
562 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
563 sector_qemu2lun(sector_num
, iscsilun
),
564 8 + 16, iscsi_co_generic_cb
,
570 while (!iTask
.complete
) {
571 iscsi_set_events(iscsilun
);
572 qemu_coroutine_yield();
575 if (iTask
.do_retry
) {
576 if (iTask
.task
!= NULL
) {
577 scsi_free_scsi_task(iTask
.task
);
584 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
585 /* in case the get_lba_status_callout fails (i.e.
586 * because the device is busy or the cmd is not
587 * supported) we pretend all blocks are allocated
588 * for backwards compatibility */
592 lbas
= scsi_datain_unmarshall(iTask
.task
);
598 lbasd
= &lbas
->descriptors
[0];
600 if (sector_qemu2lun(sector_num
, iscsilun
) != lbasd
->lba
) {
605 *pnum
= sector_lun2qemu(lbasd
->num_blocks
, iscsilun
);
607 if (lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_DEALLOCATED
||
608 lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_ANCHORED
) {
609 ret
&= ~BDRV_BLOCK_DATA
;
610 if (iscsilun
->lbprz
) {
611 ret
|= BDRV_BLOCK_ZERO
;
615 if (ret
& BDRV_BLOCK_ZERO
) {
616 iscsi_allocationmap_clear(iscsilun
, sector_num
, *pnum
);
618 iscsi_allocationmap_set(iscsilun
, sector_num
, *pnum
);
621 if (*pnum
> nb_sectors
) {
625 if (iTask
.task
!= NULL
) {
626 scsi_free_scsi_task(iTask
.task
);
628 if (ret
> 0 && ret
& BDRV_BLOCK_OFFSET_VALID
) {
634 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
635 int64_t sector_num
, int nb_sectors
,
638 IscsiLun
*iscsilun
= bs
->opaque
;
639 struct IscsiTask iTask
;
641 uint32_t num_sectors
;
643 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
647 if (bs
->bl
.max_transfer_length
&& nb_sectors
> bs
->bl
.max_transfer_length
) {
648 error_report("iSCSI Error: Read of %d sectors exceeds max_xfer_len "
649 "of %d sectors", nb_sectors
, bs
->bl
.max_transfer_length
);
653 if (iscsilun
->lbprz
&& nb_sectors
>= ISCSI_CHECKALLOC_THRES
&&
654 !iscsi_allocationmap_is_allocated(iscsilun
, sector_num
, nb_sectors
)) {
657 BlockDriverState
*file
;
658 ret
= iscsi_co_get_block_status(bs
, sector_num
, INT_MAX
, &pnum
, &file
);
662 if (ret
& BDRV_BLOCK_ZERO
&& pnum
>= nb_sectors
) {
663 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
668 lba
= sector_qemu2lun(sector_num
, iscsilun
);
669 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
671 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
673 if (iscsilun
->use_16_for_rw
) {
674 iTask
.task
= iscsi_read16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
675 num_sectors
* iscsilun
->block_size
,
676 iscsilun
->block_size
, 0, 0, 0, 0, 0,
677 iscsi_co_generic_cb
, &iTask
);
679 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
680 num_sectors
* iscsilun
->block_size
,
681 iscsilun
->block_size
,
683 iscsi_co_generic_cb
, &iTask
);
685 if (iTask
.task
== NULL
) {
688 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
690 while (!iTask
.complete
) {
691 iscsi_set_events(iscsilun
);
692 qemu_coroutine_yield();
695 if (iTask
.task
!= NULL
) {
696 scsi_free_scsi_task(iTask
.task
);
700 if (iTask
.do_retry
) {
705 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
706 return iTask
.err_code
;
712 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
714 IscsiLun
*iscsilun
= bs
->opaque
;
715 struct IscsiTask iTask
;
717 if (!iscsilun
->force_next_flush
) {
720 iscsilun
->force_next_flush
= false;
722 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
724 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
725 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
729 while (!iTask
.complete
) {
730 iscsi_set_events(iscsilun
);
731 qemu_coroutine_yield();
734 if (iTask
.task
!= NULL
) {
735 scsi_free_scsi_task(iTask
.task
);
739 if (iTask
.do_retry
) {
744 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
745 return iTask
.err_code
;
753 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
754 void *command_data
, void *opaque
)
756 IscsiAIOCB
*acb
= opaque
;
763 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
764 iscsi_get_error(iscsi
));
765 acb
->status
= iscsi_translate_sense(&acb
->task
->sense
);
768 acb
->ioh
->driver_status
= 0;
769 acb
->ioh
->host_status
= 0;
772 #define SG_ERR_DRIVER_SENSE 0x08
774 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
777 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
779 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
780 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
781 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
782 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
785 iscsi_schedule_bh(acb
);
788 static void iscsi_ioctl_bh_completion(void *opaque
)
790 IscsiAIOCB
*acb
= opaque
;
792 qemu_bh_delete(acb
->bh
);
793 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
797 static void iscsi_ioctl_handle_emulated(IscsiAIOCB
*acb
, int req
, void *buf
)
799 BlockDriverState
*bs
= acb
->common
.bs
;
800 IscsiLun
*iscsilun
= bs
->opaque
;
804 case SG_GET_VERSION_NUM
:
808 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
814 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
),
815 iscsi_ioctl_bh_completion
, acb
);
817 qemu_bh_schedule(acb
->bh
);
820 static BlockAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
821 unsigned long int req
, void *buf
,
822 BlockCompletionFunc
*cb
, void *opaque
)
824 IscsiLun
*iscsilun
= bs
->opaque
;
825 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
826 struct iscsi_data data
;
829 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
831 acb
->iscsilun
= iscsilun
;
833 acb
->status
= -EINPROGRESS
;
838 iscsi_ioctl_handle_emulated(acb
, req
, buf
);
842 acb
->task
= malloc(sizeof(struct scsi_task
));
843 if (acb
->task
== NULL
) {
844 error_report("iSCSI: Failed to allocate task for scsi command. %s",
845 iscsi_get_error(iscsi
));
849 memset(acb
->task
, 0, sizeof(struct scsi_task
));
851 switch (acb
->ioh
->dxfer_direction
) {
852 case SG_DXFER_TO_DEV
:
853 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
855 case SG_DXFER_FROM_DEV
:
856 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
859 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
863 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
864 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
865 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
868 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
869 if (acb
->ioh
->iovec_count
== 0) {
870 data
.data
= acb
->ioh
->dxferp
;
871 data
.size
= acb
->ioh
->dxfer_len
;
873 scsi_task_set_iov_out(acb
->task
,
874 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
875 acb
->ioh
->iovec_count
);
879 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
881 (data
.size
> 0) ? &data
: NULL
,
883 scsi_free_scsi_task(acb
->task
);
888 /* tell libiscsi to read straight into the buffer we got from ioctl */
889 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
890 if (acb
->ioh
->iovec_count
== 0) {
891 scsi_task_add_data_in_buffer(acb
->task
,
895 scsi_task_set_iov_in(acb
->task
,
896 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
897 acb
->ioh
->iovec_count
);
901 iscsi_set_events(iscsilun
);
909 iscsi_getlength(BlockDriverState
*bs
)
911 IscsiLun
*iscsilun
= bs
->opaque
;
914 len
= iscsilun
->num_blocks
;
915 len
*= iscsilun
->block_size
;
921 coroutine_fn
iscsi_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
924 IscsiLun
*iscsilun
= bs
->opaque
;
925 struct IscsiTask iTask
;
926 struct unmap_list list
;
928 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
932 if (!iscsilun
->lbp
.lbpu
) {
933 /* UNMAP is not supported by the target */
937 list
.lba
= sector_qemu2lun(sector_num
, iscsilun
);
938 list
.num
= sector_qemu2lun(nb_sectors
, iscsilun
);
940 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
942 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
943 iscsi_co_generic_cb
, &iTask
) == NULL
) {
947 while (!iTask
.complete
) {
948 iscsi_set_events(iscsilun
);
949 qemu_coroutine_yield();
952 if (iTask
.task
!= NULL
) {
953 scsi_free_scsi_task(iTask
.task
);
957 if (iTask
.do_retry
) {
962 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
) {
963 /* the target might fail with a check condition if it
964 is not happy with the alignment of the UNMAP request
965 we silently fail in this case */
969 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
970 return iTask
.err_code
;
973 iscsi_allocationmap_clear(iscsilun
, sector_num
, nb_sectors
);
979 coroutine_fn
iscsi_co_write_zeroes(BlockDriverState
*bs
, int64_t sector_num
,
980 int nb_sectors
, BdrvRequestFlags flags
)
982 IscsiLun
*iscsilun
= bs
->opaque
;
983 struct IscsiTask iTask
;
986 bool use_16_for_ws
= iscsilun
->use_16_for_rw
;
988 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
992 if (flags
& BDRV_REQ_MAY_UNMAP
) {
993 if (!use_16_for_ws
&& !iscsilun
->lbp
.lbpws10
) {
994 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
995 use_16_for_ws
= true;
997 if (use_16_for_ws
&& !iscsilun
->lbp
.lbpws
) {
998 /* WRITESAME16 with UNMAP is not supported by the target,
999 * fall back and try WRITESAME10/16 without UNMAP */
1000 flags
&= ~BDRV_REQ_MAY_UNMAP
;
1001 use_16_for_ws
= iscsilun
->use_16_for_rw
;
1005 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
1006 /* WRITESAME without UNMAP is not supported by the target */
1010 lba
= sector_qemu2lun(sector_num
, iscsilun
);
1011 nb_blocks
= sector_qemu2lun(nb_sectors
, iscsilun
);
1013 if (iscsilun
->zeroblock
== NULL
) {
1014 iscsilun
->zeroblock
= g_try_malloc0(iscsilun
->block_size
);
1015 if (iscsilun
->zeroblock
== NULL
) {
1020 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1021 iTask
.force_next_flush
= true;
1023 if (use_16_for_ws
) {
1024 iTask
.task
= iscsi_writesame16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1025 iscsilun
->zeroblock
, iscsilun
->block_size
,
1026 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1027 0, 0, iscsi_co_generic_cb
, &iTask
);
1029 iTask
.task
= iscsi_writesame10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1030 iscsilun
->zeroblock
, iscsilun
->block_size
,
1031 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1032 0, 0, iscsi_co_generic_cb
, &iTask
);
1034 if (iTask
.task
== NULL
) {
1038 while (!iTask
.complete
) {
1039 iscsi_set_events(iscsilun
);
1040 qemu_coroutine_yield();
1043 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
&&
1044 iTask
.task
->sense
.key
== SCSI_SENSE_ILLEGAL_REQUEST
&&
1045 (iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
||
1046 iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
)) {
1047 /* WRITE SAME is not supported by the target */
1048 iscsilun
->has_write_same
= false;
1049 scsi_free_scsi_task(iTask
.task
);
1053 if (iTask
.task
!= NULL
) {
1054 scsi_free_scsi_task(iTask
.task
);
1058 if (iTask
.do_retry
) {
1063 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1064 return iTask
.err_code
;
1067 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1068 iscsi_allocationmap_clear(iscsilun
, sector_num
, nb_sectors
);
1070 iscsi_allocationmap_set(iscsilun
, sector_num
, nb_sectors
);
1076 static void parse_chap(struct iscsi_context
*iscsi
, const char *target
,
1081 const char *user
= NULL
;
1082 const char *password
= NULL
;
1084 list
= qemu_find_opts("iscsi");
1089 opts
= qemu_opts_find(list
, target
);
1091 opts
= QTAILQ_FIRST(&list
->head
);
1097 user
= qemu_opt_get(opts
, "user");
1102 password
= qemu_opt_get(opts
, "password");
1104 error_setg(errp
, "CHAP username specified but no password was given");
1108 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
1109 error_setg(errp
, "Failed to set initiator username and password");
1113 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
,
1118 const char *digest
= NULL
;
1120 list
= qemu_find_opts("iscsi");
1125 opts
= qemu_opts_find(list
, target
);
1127 opts
= QTAILQ_FIRST(&list
->head
);
1133 digest
= qemu_opt_get(opts
, "header-digest");
1138 if (!strcmp(digest
, "CRC32C")) {
1139 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
1140 } else if (!strcmp(digest
, "NONE")) {
1141 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
1142 } else if (!strcmp(digest
, "CRC32C-NONE")) {
1143 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
1144 } else if (!strcmp(digest
, "NONE-CRC32C")) {
1145 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1147 error_setg(errp
, "Invalid header-digest setting : %s", digest
);
1151 static char *parse_initiator_name(const char *target
)
1157 UuidInfo
*uuid_info
;
1159 list
= qemu_find_opts("iscsi");
1161 opts
= qemu_opts_find(list
, target
);
1163 opts
= QTAILQ_FIRST(&list
->head
);
1166 name
= qemu_opt_get(opts
, "initiator-name");
1168 return g_strdup(name
);
1173 uuid_info
= qmp_query_uuid(NULL
);
1174 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
1175 name
= qemu_get_vm_name();
1177 name
= uuid_info
->UUID
;
1179 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1180 name
? ":" : "", name
? name
: "");
1181 qapi_free_UuidInfo(uuid_info
);
1185 static int parse_timeout(const char *target
)
1189 const char *timeout
;
1191 list
= qemu_find_opts("iscsi");
1193 opts
= qemu_opts_find(list
, target
);
1195 opts
= QTAILQ_FIRST(&list
->head
);
1198 timeout
= qemu_opt_get(opts
, "timeout");
1200 return atoi(timeout
);
1208 static void iscsi_nop_timed_event(void *opaque
)
1210 IscsiLun
*iscsilun
= opaque
;
1212 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) >= MAX_NOP_FAILURES
) {
1213 error_report("iSCSI: NOP timeout. Reconnecting...");
1214 iscsilun
->request_timed_out
= true;
1215 } else if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
1216 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1220 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1221 iscsi_set_events(iscsilun
);
1224 static void iscsi_readcapacity_sync(IscsiLun
*iscsilun
, Error
**errp
)
1226 struct scsi_task
*task
= NULL
;
1227 struct scsi_readcapacity10
*rc10
= NULL
;
1228 struct scsi_readcapacity16
*rc16
= NULL
;
1229 int retries
= ISCSI_CMD_RETRIES
;
1233 scsi_free_scsi_task(task
);
1237 switch (iscsilun
->type
) {
1239 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
1240 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1241 rc16
= scsi_datain_unmarshall(task
);
1243 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity16 data.");
1245 iscsilun
->block_size
= rc16
->block_length
;
1246 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1247 iscsilun
->lbpme
= !!rc16
->lbpme
;
1248 iscsilun
->lbprz
= !!rc16
->lbprz
;
1249 iscsilun
->use_16_for_rw
= (rc16
->returned_lba
> 0xffffffff);
1253 if (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1254 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
1257 /* Fall through and try READ CAPACITY(10) instead. */
1259 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1260 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1261 rc10
= scsi_datain_unmarshall(task
);
1263 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity10 data.");
1265 iscsilun
->block_size
= rc10
->block_size
;
1266 if (rc10
->lba
== 0) {
1267 /* blank disk loaded */
1268 iscsilun
->num_blocks
= 0;
1270 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1278 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1279 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1282 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1283 error_setg(errp
, "iSCSI: failed to send readcapacity10/16 command");
1284 } else if (!iscsilun
->block_size
||
1285 iscsilun
->block_size
% BDRV_SECTOR_SIZE
) {
1286 error_setg(errp
, "iSCSI: the target returned an invalid "
1287 "block size of %d.", iscsilun
->block_size
);
1290 scsi_free_scsi_task(task
);
1294 /* TODO Convert to fine grained options */
1295 static QemuOptsList runtime_opts
= {
1297 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1301 .type
= QEMU_OPT_STRING
,
1302 .help
= "URL to the iscsi image",
1304 { /* end of list */ }
1308 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
, int lun
,
1309 int evpd
, int pc
, void **inq
, Error
**errp
)
1312 struct scsi_task
*task
= NULL
;
1313 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, 64);
1314 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1317 full_size
= scsi_datain_getfullsize(task
);
1318 if (full_size
> task
->datain
.size
) {
1319 scsi_free_scsi_task(task
);
1321 /* we need more data for the full list */
1322 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, full_size
);
1323 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1328 *inq
= scsi_datain_unmarshall(task
);
1330 error_setg(errp
, "iSCSI: failed to unmarshall inquiry datain blob");
1337 error_setg(errp
, "iSCSI: Inquiry command failed : %s",
1338 iscsi_get_error(iscsi
));
1341 scsi_free_scsi_task(task
);
1346 static void iscsi_detach_aio_context(BlockDriverState
*bs
)
1348 IscsiLun
*iscsilun
= bs
->opaque
;
1350 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsilun
->iscsi
),
1351 false, NULL
, NULL
, NULL
);
1352 iscsilun
->events
= 0;
1354 if (iscsilun
->nop_timer
) {
1355 timer_del(iscsilun
->nop_timer
);
1356 timer_free(iscsilun
->nop_timer
);
1357 iscsilun
->nop_timer
= NULL
;
1359 if (iscsilun
->event_timer
) {
1360 timer_del(iscsilun
->event_timer
);
1361 timer_free(iscsilun
->event_timer
);
1362 iscsilun
->event_timer
= NULL
;
1366 static void iscsi_attach_aio_context(BlockDriverState
*bs
,
1367 AioContext
*new_context
)
1369 IscsiLun
*iscsilun
= bs
->opaque
;
1371 iscsilun
->aio_context
= new_context
;
1372 iscsi_set_events(iscsilun
);
1374 /* Set up a timer for sending out iSCSI NOPs */
1375 iscsilun
->nop_timer
= aio_timer_new(iscsilun
->aio_context
,
1376 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1377 iscsi_nop_timed_event
, iscsilun
);
1378 timer_mod(iscsilun
->nop_timer
,
1379 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1381 /* Set up a timer for periodic calls to iscsi_set_events and to
1382 * scan for command timeout */
1383 iscsilun
->event_timer
= aio_timer_new(iscsilun
->aio_context
,
1384 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1385 iscsi_timed_check_events
, iscsilun
);
1386 timer_mod(iscsilun
->event_timer
,
1387 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
1390 static void iscsi_modesense_sync(IscsiLun
*iscsilun
)
1392 struct scsi_task
*task
;
1393 struct scsi_mode_sense
*ms
= NULL
;
1394 iscsilun
->write_protected
= false;
1395 iscsilun
->dpofua
= false;
1397 task
= iscsi_modesense6_sync(iscsilun
->iscsi
, iscsilun
->lun
,
1398 1, SCSI_MODESENSE_PC_CURRENT
,
1401 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1402 iscsi_get_error(iscsilun
->iscsi
));
1406 if (task
->status
!= SCSI_STATUS_GOOD
) {
1407 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1410 ms
= scsi_datain_unmarshall(task
);
1412 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1413 iscsi_get_error(iscsilun
->iscsi
));
1416 iscsilun
->write_protected
= ms
->device_specific_parameter
& 0x80;
1417 iscsilun
->dpofua
= ms
->device_specific_parameter
& 0x10;
1421 scsi_free_scsi_task(task
);
1426 * We support iscsi url's on the form
1427 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1429 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1432 IscsiLun
*iscsilun
= bs
->opaque
;
1433 struct iscsi_context
*iscsi
= NULL
;
1434 struct iscsi_url
*iscsi_url
= NULL
;
1435 struct scsi_task
*task
= NULL
;
1436 struct scsi_inquiry_standard
*inq
= NULL
;
1437 struct scsi_inquiry_supported_pages
*inq_vpd
;
1438 char *initiator_name
= NULL
;
1440 Error
*local_err
= NULL
;
1441 const char *filename
;
1442 int i
, ret
= 0, timeout
= 0;
1444 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1445 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1447 error_propagate(errp
, local_err
);
1452 filename
= qemu_opt_get(opts
, "filename");
1454 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1455 if (iscsi_url
== NULL
) {
1456 error_setg(errp
, "Failed to parse URL : %s", filename
);
1461 memset(iscsilun
, 0, sizeof(IscsiLun
));
1463 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1465 iscsi
= iscsi_create_context(initiator_name
);
1466 if (iscsi
== NULL
) {
1467 error_setg(errp
, "iSCSI: Failed to create iSCSI context.");
1472 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1473 error_setg(errp
, "iSCSI: Failed to set target name.");
1478 if (iscsi_url
->user
[0] != '\0') {
1479 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1482 error_setg(errp
, "Failed to set initiator username and password");
1488 /* check if we got CHAP username/password via the options */
1489 parse_chap(iscsi
, iscsi_url
->target
, &local_err
);
1490 if (local_err
!= NULL
) {
1491 error_propagate(errp
, local_err
);
1496 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1497 error_setg(errp
, "iSCSI: Failed to set session type to normal.");
1502 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1504 /* check if we got HEADER_DIGEST via the options */
1505 parse_header_digest(iscsi
, iscsi_url
->target
, &local_err
);
1506 if (local_err
!= NULL
) {
1507 error_propagate(errp
, local_err
);
1512 /* timeout handling is broken in libiscsi before 1.15.0 */
1513 timeout
= parse_timeout(iscsi_url
->target
);
1514 #if defined(LIBISCSI_API_VERSION) && LIBISCSI_API_VERSION >= 20150621
1515 iscsi_set_timeout(iscsi
, timeout
);
1518 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1522 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1523 error_setg(errp
, "iSCSI: Failed to connect to LUN : %s",
1524 iscsi_get_error(iscsi
));
1529 iscsilun
->iscsi
= iscsi
;
1530 iscsilun
->aio_context
= bdrv_get_aio_context(bs
);
1531 iscsilun
->lun
= iscsi_url
->lun
;
1532 iscsilun
->has_write_same
= true;
1534 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0,
1535 (void **) &inq
, errp
);
1540 iscsilun
->type
= inq
->periperal_device_type
;
1541 scsi_free_scsi_task(task
);
1544 iscsi_modesense_sync(iscsilun
);
1546 /* Check the write protect flag of the LUN if we want to write */
1547 if (iscsilun
->type
== TYPE_DISK
&& (flags
& BDRV_O_RDWR
) &&
1548 iscsilun
->write_protected
) {
1549 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1554 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1555 if (local_err
!= NULL
) {
1556 error_propagate(errp
, local_err
);
1560 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1561 bs
->request_alignment
= iscsilun
->block_size
;
1563 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1564 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1565 * will try to read from the device to guess the image format.
1567 if (iscsilun
->type
!= TYPE_DISK
&& iscsilun
->type
!= TYPE_ROM
) {
1571 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1572 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES
,
1573 (void **) &inq_vpd
, errp
);
1578 for (i
= 0; i
< inq_vpd
->num_pages
; i
++) {
1579 struct scsi_task
*inq_task
;
1580 struct scsi_inquiry_logical_block_provisioning
*inq_lbp
;
1581 struct scsi_inquiry_block_limits
*inq_bl
;
1582 switch (inq_vpd
->pages
[i
]) {
1583 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
:
1584 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1585 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
,
1586 (void **) &inq_lbp
, errp
);
1587 if (inq_task
== NULL
) {
1591 memcpy(&iscsilun
->lbp
, inq_lbp
,
1592 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1593 scsi_free_scsi_task(inq_task
);
1595 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
:
1596 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1597 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
,
1598 (void **) &inq_bl
, errp
);
1599 if (inq_task
== NULL
) {
1603 memcpy(&iscsilun
->bl
, inq_bl
,
1604 sizeof(struct scsi_inquiry_block_limits
));
1605 scsi_free_scsi_task(inq_task
);
1611 scsi_free_scsi_task(task
);
1614 iscsi_attach_aio_context(bs
, iscsilun
->aio_context
);
1616 /* Guess the internal cluster (page) size of the iscsi target by the means
1617 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1618 * reasonable size */
1619 if (iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
>= 4 * 1024 &&
1620 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
<= 16 * 1024 * 1024) {
1621 iscsilun
->cluster_sectors
= (iscsilun
->bl
.opt_unmap_gran
*
1622 iscsilun
->block_size
) >> BDRV_SECTOR_BITS
;
1623 if (iscsilun
->lbprz
) {
1624 iscsilun
->allocationmap
= iscsi_allocationmap_init(iscsilun
);
1625 if (iscsilun
->allocationmap
== NULL
) {
1632 qemu_opts_del(opts
);
1633 g_free(initiator_name
);
1634 if (iscsi_url
!= NULL
) {
1635 iscsi_destroy_url(iscsi_url
);
1638 scsi_free_scsi_task(task
);
1642 if (iscsi
!= NULL
) {
1643 if (iscsi_is_logged_in(iscsi
)) {
1644 iscsi_logout_sync(iscsi
);
1646 iscsi_destroy_context(iscsi
);
1648 memset(iscsilun
, 0, sizeof(IscsiLun
));
1653 static void iscsi_close(BlockDriverState
*bs
)
1655 IscsiLun
*iscsilun
= bs
->opaque
;
1656 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1658 iscsi_detach_aio_context(bs
);
1659 if (iscsi_is_logged_in(iscsi
)) {
1660 iscsi_logout_sync(iscsi
);
1662 iscsi_destroy_context(iscsi
);
1663 g_free(iscsilun
->zeroblock
);
1664 g_free(iscsilun
->allocationmap
);
1665 memset(iscsilun
, 0, sizeof(IscsiLun
));
1668 static int sector_limits_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
1670 return MIN(sector_lun2qemu(sector
, iscsilun
), INT_MAX
/ 2 + 1);
1673 static void iscsi_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1675 /* We don't actually refresh here, but just return data queried in
1676 * iscsi_open(): iscsi targets don't change their limits. */
1678 IscsiLun
*iscsilun
= bs
->opaque
;
1679 uint32_t max_xfer_len
= iscsilun
->use_16_for_rw
? 0xffffffff : 0xffff;
1681 if (iscsilun
->bl
.max_xfer_len
) {
1682 max_xfer_len
= MIN(max_xfer_len
, iscsilun
->bl
.max_xfer_len
);
1685 bs
->bl
.max_transfer_length
= sector_limits_lun2qemu(max_xfer_len
, iscsilun
);
1687 if (iscsilun
->lbp
.lbpu
) {
1688 if (iscsilun
->bl
.max_unmap
< 0xffffffff) {
1689 bs
->bl
.max_discard
=
1690 sector_limits_lun2qemu(iscsilun
->bl
.max_unmap
, iscsilun
);
1692 bs
->bl
.discard_alignment
=
1693 sector_limits_lun2qemu(iscsilun
->bl
.opt_unmap_gran
, iscsilun
);
1696 if (iscsilun
->bl
.max_ws_len
< 0xffffffff) {
1697 bs
->bl
.max_write_zeroes
=
1698 sector_limits_lun2qemu(iscsilun
->bl
.max_ws_len
, iscsilun
);
1700 if (iscsilun
->lbp
.lbpws
) {
1701 bs
->bl
.write_zeroes_alignment
=
1702 sector_limits_lun2qemu(iscsilun
->bl
.opt_unmap_gran
, iscsilun
);
1704 bs
->bl
.opt_transfer_length
=
1705 sector_limits_lun2qemu(iscsilun
->bl
.opt_xfer_len
, iscsilun
);
1708 /* Note that this will not re-establish a connection with an iSCSI target - it
1709 * is effectively a NOP. */
1710 static int iscsi_reopen_prepare(BDRVReopenState
*state
,
1711 BlockReopenQueue
*queue
, Error
**errp
)
1713 IscsiLun
*iscsilun
= state
->bs
->opaque
;
1715 if (state
->flags
& BDRV_O_RDWR
&& iscsilun
->write_protected
) {
1716 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1722 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1724 IscsiLun
*iscsilun
= bs
->opaque
;
1725 Error
*local_err
= NULL
;
1727 if (iscsilun
->type
!= TYPE_DISK
) {
1731 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1732 if (local_err
!= NULL
) {
1733 error_free(local_err
);
1737 if (offset
> iscsi_getlength(bs
)) {
1741 if (iscsilun
->allocationmap
!= NULL
) {
1742 g_free(iscsilun
->allocationmap
);
1743 iscsilun
->allocationmap
= iscsi_allocationmap_init(iscsilun
);
1749 static int iscsi_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
1752 int64_t total_size
= 0;
1753 BlockDriverState
*bs
;
1754 IscsiLun
*iscsilun
= NULL
;
1759 /* Read out options */
1760 total_size
= DIV_ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1762 bs
->opaque
= g_new0(struct IscsiLun
, 1);
1763 iscsilun
= bs
->opaque
;
1765 bs_options
= qdict_new();
1766 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1767 ret
= iscsi_open(bs
, bs_options
, 0, NULL
);
1768 QDECREF(bs_options
);
1773 iscsi_detach_aio_context(bs
);
1774 if (iscsilun
->type
!= TYPE_DISK
) {
1778 if (bs
->total_sectors
< total_size
) {
1785 if (iscsilun
->iscsi
!= NULL
) {
1786 iscsi_destroy_context(iscsilun
->iscsi
);
1794 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1796 IscsiLun
*iscsilun
= bs
->opaque
;
1797 bdi
->unallocated_blocks_are_zero
= iscsilun
->lbprz
;
1798 bdi
->can_write_zeroes_with_unmap
= iscsilun
->lbprz
&& iscsilun
->lbp
.lbpws
;
1799 bdi
->cluster_size
= iscsilun
->cluster_sectors
* BDRV_SECTOR_SIZE
;
1803 static QemuOptsList iscsi_create_opts
= {
1804 .name
= "iscsi-create-opts",
1805 .head
= QTAILQ_HEAD_INITIALIZER(iscsi_create_opts
.head
),
1808 .name
= BLOCK_OPT_SIZE
,
1809 .type
= QEMU_OPT_SIZE
,
1810 .help
= "Virtual disk size"
1812 { /* end of list */ }
1816 static BlockDriver bdrv_iscsi
= {
1817 .format_name
= "iscsi",
1818 .protocol_name
= "iscsi",
1820 .instance_size
= sizeof(IscsiLun
),
1821 .bdrv_needs_filename
= true,
1822 .bdrv_file_open
= iscsi_open
,
1823 .bdrv_close
= iscsi_close
,
1824 .bdrv_create
= iscsi_create
,
1825 .create_opts
= &iscsi_create_opts
,
1826 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
1828 .bdrv_getlength
= iscsi_getlength
,
1829 .bdrv_get_info
= iscsi_get_info
,
1830 .bdrv_truncate
= iscsi_truncate
,
1831 .bdrv_refresh_limits
= iscsi_refresh_limits
,
1833 .bdrv_co_get_block_status
= iscsi_co_get_block_status
,
1834 .bdrv_co_discard
= iscsi_co_discard
,
1835 .bdrv_co_write_zeroes
= iscsi_co_write_zeroes
,
1836 .bdrv_co_readv
= iscsi_co_readv
,
1837 .bdrv_co_writev
= iscsi_co_writev
,
1838 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
1841 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1844 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
1845 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
1848 static QemuOptsList qemu_iscsi_opts
= {
1850 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1854 .type
= QEMU_OPT_STRING
,
1855 .help
= "username for CHAP authentication to target",
1858 .type
= QEMU_OPT_STRING
,
1859 .help
= "password for CHAP authentication to target",
1861 .name
= "header-digest",
1862 .type
= QEMU_OPT_STRING
,
1863 .help
= "HeaderDigest setting. "
1864 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1866 .name
= "initiator-name",
1867 .type
= QEMU_OPT_STRING
,
1868 .help
= "Initiator iqn name to use when connecting",
1871 .type
= QEMU_OPT_NUMBER
,
1872 .help
= "Request timeout in seconds (default 0 = no timeout)",
1874 { /* end of list */ }
1878 static void iscsi_block_init(void)
1880 bdrv_register(&bdrv_iscsi
);
1881 qemu_add_opts(&qemu_iscsi_opts
);
1884 block_init(iscsi_block_init
);