2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2017 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/config-file.h"
32 #include "qemu/error-report.h"
33 #include "qemu/bitops.h"
34 #include "qemu/bitmap.h"
35 #include "block/block_int.h"
36 #include "scsi/constants.h"
38 #include "qemu/option.h"
39 #include "qemu/uuid.h"
40 #include "qmp-commands.h"
41 #include "qapi/error.h"
42 #include "qapi/qmp/qdict.h"
43 #include "qapi/qmp/qstring.h"
44 #include "crypto/secret.h"
45 #include "scsi/utils.h"
47 /* Conflict between scsi/utils.h and libiscsi! :( */
48 #define SCSI_XFER_NONE ISCSI_XFER_NONE
49 #include <iscsi/iscsi.h>
50 #include <iscsi/scsi-lowlevel.h>
52 QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE
!= (int)ISCSI_XFER_NONE
);
58 typedef struct IscsiLun
{
59 struct iscsi_context
*iscsi
;
60 AioContext
*aio_context
;
62 enum scsi_inquiry_peripheral_device_type type
;
67 QEMUTimer
*event_timer
;
69 struct scsi_inquiry_logical_block_provisioning lbp
;
70 struct scsi_inquiry_block_limits bl
;
71 unsigned char *zeroblock
;
72 /* The allocmap tracks which clusters (pages) on the iSCSI target are
73 * allocated and which are not. In case a target returns zeros for
74 * unallocated pages (iscsilun->lprz) we can directly return zeros instead
75 * of reading zeros over the wire if a read request falls within an
76 * unallocated block. As there are 3 possible states we need 2 bitmaps to
77 * track. allocmap_valid keeps track if QEMU's information about a page is
78 * valid. allocmap tracks if a page is allocated or not. In case QEMU has no
79 * valid information about a page the corresponding allocmap entry should be
80 * switched to unallocated as well to force a new lookup of the allocation
81 * status as lookups are generally skipped if a page is suspect to be
82 * allocated. If a iSCSI target is opened with cache.direct = on the
83 * allocmap_valid does not exist turning all cached information invalid so
84 * that a fresh lookup is made for any page even if allocmap entry returns
85 * it's unallocated. */
86 unsigned long *allocmap
;
87 unsigned long *allocmap_valid
;
96 bool request_timed_out
;
99 typedef struct IscsiTask
{
104 struct scsi_task
*task
;
107 QEMUTimer retry_timer
;
112 typedef struct IscsiAIOCB
{
116 struct scsi_task
*task
;
127 /* libiscsi uses time_t so its enough to process events every second */
128 #define EVENT_INTERVAL 1000
129 #define NOP_INTERVAL 5000
130 #define MAX_NOP_FAILURES 3
131 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
132 static const unsigned iscsi_retry_times
[] = {8, 32, 128, 512, 2048, 8192, 32768};
134 /* this threshold is a trade-off knob to choose between
135 * the potential additional overhead of an extra GET_LBA_STATUS request
136 * vs. unnecessarily reading a lot of zero sectors over the wire.
137 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
138 * sectors we check the allocation status of the area covered by the
139 * request first if the allocationmap indicates that the area might be
141 #define ISCSI_CHECKALLOC_THRES 64
148 qemu_bh_delete(acb
->bh
);
153 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
155 if (acb
->task
!= NULL
) {
156 scsi_free_scsi_task(acb
->task
);
164 iscsi_schedule_bh(IscsiAIOCB
*acb
)
169 acb
->bh
= aio_bh_new(acb
->iscsilun
->aio_context
, iscsi_bh_cb
, acb
);
170 qemu_bh_schedule(acb
->bh
);
173 static void iscsi_co_generic_bh_cb(void *opaque
)
175 struct IscsiTask
*iTask
= opaque
;
178 aio_co_wake(iTask
->co
);
181 static void iscsi_retry_timer_expired(void *opaque
)
183 struct IscsiTask
*iTask
= opaque
;
186 aio_co_wake(iTask
->co
);
190 static inline unsigned exp_random(double mean
)
192 return -mean
* log((double)rand() / RAND_MAX
);
195 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
196 * libiscsi 1.10.0, together with other constants we need. Use it as
197 * a hint that we have to define them ourselves if needed, to keep the
198 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
199 * the test because SCSI_STATUS_* is an enum.
201 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
202 * an enum, check against the LIBISCSI_API_VERSION macro, which was
203 * introduced in 1.11.0. If it is present, there is no need to define
206 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
207 !defined(LIBISCSI_API_VERSION)
208 #define SCSI_STATUS_TASK_SET_FULL 0x28
209 #define SCSI_STATUS_TIMEOUT 0x0f000002
210 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
211 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
214 #ifndef LIBISCSI_API_VERSION
215 #define LIBISCSI_API_VERSION 20130701
218 static int iscsi_translate_sense(struct scsi_sense
*sense
)
220 return - scsi_sense_to_errno(sense
->key
,
221 (sense
->ascq
& 0xFF00) >> 8,
225 /* Called (via iscsi_service) with QemuMutex held. */
227 iscsi_co_generic_cb(struct iscsi_context
*iscsi
, int status
,
228 void *command_data
, void *opaque
)
230 struct IscsiTask
*iTask
= opaque
;
231 struct scsi_task
*task
= command_data
;
233 iTask
->status
= status
;
237 if (status
!= SCSI_STATUS_GOOD
) {
238 if (iTask
->retries
++ < ISCSI_CMD_RETRIES
) {
239 if (status
== SCSI_STATUS_CHECK_CONDITION
240 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
241 error_report("iSCSI CheckCondition: %s",
242 iscsi_get_error(iscsi
));
246 if (status
== SCSI_STATUS_BUSY
||
247 status
== SCSI_STATUS_TIMEOUT
||
248 status
== SCSI_STATUS_TASK_SET_FULL
) {
249 unsigned retry_time
=
250 exp_random(iscsi_retry_times
[iTask
->retries
- 1]);
251 if (status
== SCSI_STATUS_TIMEOUT
) {
252 /* make sure the request is rescheduled AFTER the
253 * reconnect is initiated */
254 retry_time
= EVENT_INTERVAL
* 2;
255 iTask
->iscsilun
->request_timed_out
= true;
257 error_report("iSCSI Busy/TaskSetFull/TimeOut"
258 " (retry #%u in %u ms): %s",
259 iTask
->retries
, retry_time
,
260 iscsi_get_error(iscsi
));
261 aio_timer_init(iTask
->iscsilun
->aio_context
,
262 &iTask
->retry_timer
, QEMU_CLOCK_REALTIME
,
263 SCALE_MS
, iscsi_retry_timer_expired
, iTask
);
264 timer_mod(&iTask
->retry_timer
,
265 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + retry_time
);
270 iTask
->err_code
= iscsi_translate_sense(&task
->sense
);
271 iTask
->err_str
= g_strdup(iscsi_get_error(iscsi
));
276 aio_bh_schedule_oneshot(iTask
->iscsilun
->aio_context
,
277 iscsi_co_generic_bh_cb
, iTask
);
283 static void iscsi_co_init_iscsitask(IscsiLun
*iscsilun
, struct IscsiTask
*iTask
)
285 *iTask
= (struct IscsiTask
) {
286 .co
= qemu_coroutine_self(),
287 .iscsilun
= iscsilun
,
292 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
295 IscsiAIOCB
*acb
= private_data
;
297 acb
->status
= -ECANCELED
;
298 iscsi_schedule_bh(acb
);
302 iscsi_aio_cancel(BlockAIOCB
*blockacb
)
304 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
305 IscsiLun
*iscsilun
= acb
->iscsilun
;
307 if (acb
->status
!= -EINPROGRESS
) {
311 /* send a task mgmt call to the target to cancel the task on the target */
312 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
313 iscsi_abort_task_cb
, acb
);
317 static const AIOCBInfo iscsi_aiocb_info
= {
318 .aiocb_size
= sizeof(IscsiAIOCB
),
319 .cancel_async
= iscsi_aio_cancel
,
323 static void iscsi_process_read(void *arg
);
324 static void iscsi_process_write(void *arg
);
326 /* Called with QemuMutex held. */
328 iscsi_set_events(IscsiLun
*iscsilun
)
330 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
331 int ev
= iscsi_which_events(iscsi
);
333 if (ev
!= iscsilun
->events
) {
334 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsi
),
336 (ev
& POLLIN
) ? iscsi_process_read
: NULL
,
337 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
340 iscsilun
->events
= ev
;
344 static void iscsi_timed_check_events(void *opaque
)
346 IscsiLun
*iscsilun
= opaque
;
348 /* check for timed out requests */
349 iscsi_service(iscsilun
->iscsi
, 0);
351 if (iscsilun
->request_timed_out
) {
352 iscsilun
->request_timed_out
= false;
353 iscsi_reconnect(iscsilun
->iscsi
);
356 /* newer versions of libiscsi may return zero events. Ensure we are able
357 * to return to service once this situation changes. */
358 iscsi_set_events(iscsilun
);
360 timer_mod(iscsilun
->event_timer
,
361 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
365 iscsi_process_read(void *arg
)
367 IscsiLun
*iscsilun
= arg
;
368 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
370 qemu_mutex_lock(&iscsilun
->mutex
);
371 iscsi_service(iscsi
, POLLIN
);
372 iscsi_set_events(iscsilun
);
373 qemu_mutex_unlock(&iscsilun
->mutex
);
377 iscsi_process_write(void *arg
)
379 IscsiLun
*iscsilun
= arg
;
380 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
382 qemu_mutex_lock(&iscsilun
->mutex
);
383 iscsi_service(iscsi
, POLLOUT
);
384 iscsi_set_events(iscsilun
);
385 qemu_mutex_unlock(&iscsilun
->mutex
);
388 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
390 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
393 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
395 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
398 static bool is_byte_request_lun_aligned(int64_t offset
, int count
,
401 if (offset
% iscsilun
->block_size
|| count
% iscsilun
->block_size
) {
402 error_report("iSCSI misaligned request: "
403 "iscsilun->block_size %u, offset %" PRIi64
405 iscsilun
->block_size
, offset
, count
);
411 static bool is_sector_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
414 assert(nb_sectors
<= BDRV_REQUEST_MAX_SECTORS
);
415 return is_byte_request_lun_aligned(sector_num
<< BDRV_SECTOR_BITS
,
416 nb_sectors
<< BDRV_SECTOR_BITS
,
420 static void iscsi_allocmap_free(IscsiLun
*iscsilun
)
422 g_free(iscsilun
->allocmap
);
423 g_free(iscsilun
->allocmap_valid
);
424 iscsilun
->allocmap
= NULL
;
425 iscsilun
->allocmap_valid
= NULL
;
429 static int iscsi_allocmap_init(IscsiLun
*iscsilun
, int open_flags
)
431 iscsi_allocmap_free(iscsilun
);
433 assert(iscsilun
->cluster_size
);
434 iscsilun
->allocmap_size
=
435 DIV_ROUND_UP(iscsilun
->num_blocks
* iscsilun
->block_size
,
436 iscsilun
->cluster_size
);
438 iscsilun
->allocmap
= bitmap_try_new(iscsilun
->allocmap_size
);
439 if (!iscsilun
->allocmap
) {
443 if (open_flags
& BDRV_O_NOCACHE
) {
444 /* when cache.direct = on all allocmap entries are
445 * treated as invalid to force a relookup of the block
446 * status on every read request */
450 iscsilun
->allocmap_valid
= bitmap_try_new(iscsilun
->allocmap_size
);
451 if (!iscsilun
->allocmap_valid
) {
452 /* if we are under memory pressure free the allocmap as well */
453 iscsi_allocmap_free(iscsilun
);
461 iscsi_allocmap_update(IscsiLun
*iscsilun
, int64_t sector_num
,
462 int nb_sectors
, bool allocated
, bool valid
)
464 int64_t cl_num_expanded
, nb_cls_expanded
, cl_num_shrunk
, nb_cls_shrunk
;
465 int cluster_sectors
= iscsilun
->cluster_size
>> BDRV_SECTOR_BITS
;
467 if (iscsilun
->allocmap
== NULL
) {
470 /* expand to entirely contain all affected clusters */
471 assert(cluster_sectors
);
472 cl_num_expanded
= sector_num
/ cluster_sectors
;
473 nb_cls_expanded
= DIV_ROUND_UP(sector_num
+ nb_sectors
,
474 cluster_sectors
) - cl_num_expanded
;
475 /* shrink to touch only completely contained clusters */
476 cl_num_shrunk
= DIV_ROUND_UP(sector_num
, cluster_sectors
);
477 nb_cls_shrunk
= (sector_num
+ nb_sectors
) / cluster_sectors
480 bitmap_set(iscsilun
->allocmap
, cl_num_expanded
, nb_cls_expanded
);
482 if (nb_cls_shrunk
> 0) {
483 bitmap_clear(iscsilun
->allocmap
, cl_num_shrunk
, nb_cls_shrunk
);
487 if (iscsilun
->allocmap_valid
== NULL
) {
491 if (nb_cls_shrunk
> 0) {
492 bitmap_set(iscsilun
->allocmap_valid
, cl_num_shrunk
, nb_cls_shrunk
);
495 bitmap_clear(iscsilun
->allocmap_valid
, cl_num_expanded
,
501 iscsi_allocmap_set_allocated(IscsiLun
*iscsilun
, int64_t sector_num
,
504 iscsi_allocmap_update(iscsilun
, sector_num
, nb_sectors
, true, true);
508 iscsi_allocmap_set_unallocated(IscsiLun
*iscsilun
, int64_t sector_num
,
511 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
512 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
514 iscsi_allocmap_update(iscsilun
, sector_num
, nb_sectors
, false, true);
517 static void iscsi_allocmap_set_invalid(IscsiLun
*iscsilun
, int64_t sector_num
,
520 iscsi_allocmap_update(iscsilun
, sector_num
, nb_sectors
, false, false);
523 static void iscsi_allocmap_invalidate(IscsiLun
*iscsilun
)
525 if (iscsilun
->allocmap
) {
526 bitmap_zero(iscsilun
->allocmap
, iscsilun
->allocmap_size
);
528 if (iscsilun
->allocmap_valid
) {
529 bitmap_zero(iscsilun
->allocmap_valid
, iscsilun
->allocmap_size
);
534 iscsi_allocmap_is_allocated(IscsiLun
*iscsilun
, int64_t sector_num
,
538 if (iscsilun
->allocmap
== NULL
) {
541 assert(iscsilun
->cluster_size
);
542 size
= DIV_ROUND_UP(sector_num
+ nb_sectors
,
543 iscsilun
->cluster_size
>> BDRV_SECTOR_BITS
);
544 return !(find_next_bit(iscsilun
->allocmap
, size
,
545 sector_num
* BDRV_SECTOR_SIZE
/
546 iscsilun
->cluster_size
) == size
);
549 static inline bool iscsi_allocmap_is_valid(IscsiLun
*iscsilun
,
550 int64_t sector_num
, int nb_sectors
)
553 if (iscsilun
->allocmap_valid
== NULL
) {
556 assert(iscsilun
->cluster_size
);
557 size
= DIV_ROUND_UP(sector_num
+ nb_sectors
,
558 iscsilun
->cluster_size
>> BDRV_SECTOR_BITS
);
559 return (find_next_zero_bit(iscsilun
->allocmap_valid
, size
,
560 sector_num
* BDRV_SECTOR_SIZE
/
561 iscsilun
->cluster_size
) == size
);
564 static int coroutine_fn
565 iscsi_co_writev_flags(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
566 QEMUIOVector
*iov
, int flags
)
568 IscsiLun
*iscsilun
= bs
->opaque
;
569 struct IscsiTask iTask
;
571 uint32_t num_sectors
;
572 bool fua
= flags
& BDRV_REQ_FUA
;
576 assert(iscsilun
->dpofua
);
578 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
582 if (bs
->bl
.max_transfer
) {
583 assert(nb_sectors
<< BDRV_SECTOR_BITS
<= bs
->bl
.max_transfer
);
586 lba
= sector_qemu2lun(sector_num
, iscsilun
);
587 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
588 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
589 qemu_mutex_lock(&iscsilun
->mutex
);
591 if (iscsilun
->use_16_for_rw
) {
592 #if LIBISCSI_API_VERSION >= (20160603)
593 iTask
.task
= iscsi_write16_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
594 NULL
, num_sectors
* iscsilun
->block_size
,
595 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
596 iscsi_co_generic_cb
, &iTask
,
597 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
599 iTask
.task
= iscsi_write10_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
600 NULL
, num_sectors
* iscsilun
->block_size
,
601 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
602 iscsi_co_generic_cb
, &iTask
,
603 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
606 iTask
.task
= iscsi_write16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
607 NULL
, num_sectors
* iscsilun
->block_size
,
608 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
609 iscsi_co_generic_cb
, &iTask
);
611 iTask
.task
= iscsi_write10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
612 NULL
, num_sectors
* iscsilun
->block_size
,
613 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
614 iscsi_co_generic_cb
, &iTask
);
617 if (iTask
.task
== NULL
) {
618 qemu_mutex_unlock(&iscsilun
->mutex
);
621 #if LIBISCSI_API_VERSION < (20160603)
622 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
625 while (!iTask
.complete
) {
626 iscsi_set_events(iscsilun
);
627 qemu_mutex_unlock(&iscsilun
->mutex
);
628 qemu_coroutine_yield();
629 qemu_mutex_lock(&iscsilun
->mutex
);
632 if (iTask
.task
!= NULL
) {
633 scsi_free_scsi_task(iTask
.task
);
637 if (iTask
.do_retry
) {
642 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
643 iscsi_allocmap_set_invalid(iscsilun
, sector_num
, nb_sectors
);
644 error_report("iSCSI WRITE10/16 failed at lba %" PRIu64
": %s", lba
,
650 iscsi_allocmap_set_allocated(iscsilun
, sector_num
, nb_sectors
);
653 qemu_mutex_unlock(&iscsilun
->mutex
);
654 g_free(iTask
.err_str
);
660 static int64_t coroutine_fn
iscsi_co_get_block_status(BlockDriverState
*bs
,
662 int nb_sectors
, int *pnum
,
663 BlockDriverState
**file
)
665 IscsiLun
*iscsilun
= bs
->opaque
;
666 struct scsi_get_lba_status
*lbas
= NULL
;
667 struct scsi_lba_status_descriptor
*lbasd
= NULL
;
668 struct IscsiTask iTask
;
672 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
674 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
679 /* default to all sectors allocated */
680 ret
= BDRV_BLOCK_DATA
;
681 ret
|= (sector_num
<< BDRV_SECTOR_BITS
) | BDRV_BLOCK_OFFSET_VALID
;
684 /* LUN does not support logical block provisioning */
685 if (!iscsilun
->lbpme
) {
689 lba
= sector_qemu2lun(sector_num
, iscsilun
);
691 qemu_mutex_lock(&iscsilun
->mutex
);
693 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
694 lba
, 8 + 16, iscsi_co_generic_cb
,
700 while (!iTask
.complete
) {
701 iscsi_set_events(iscsilun
);
702 qemu_mutex_unlock(&iscsilun
->mutex
);
703 qemu_coroutine_yield();
704 qemu_mutex_lock(&iscsilun
->mutex
);
707 if (iTask
.do_retry
) {
708 if (iTask
.task
!= NULL
) {
709 scsi_free_scsi_task(iTask
.task
);
716 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
717 /* in case the get_lba_status_callout fails (i.e.
718 * because the device is busy or the cmd is not
719 * supported) we pretend all blocks are allocated
720 * for backwards compatibility */
721 error_report("iSCSI GET_LBA_STATUS failed at lba %" PRIu64
": %s",
726 lbas
= scsi_datain_unmarshall(iTask
.task
);
732 lbasd
= &lbas
->descriptors
[0];
734 if (sector_qemu2lun(sector_num
, iscsilun
) != lbasd
->lba
) {
739 *pnum
= sector_lun2qemu(lbasd
->num_blocks
, iscsilun
);
741 if (lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_DEALLOCATED
||
742 lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_ANCHORED
) {
743 ret
&= ~BDRV_BLOCK_DATA
;
744 if (iscsilun
->lbprz
) {
745 ret
|= BDRV_BLOCK_ZERO
;
749 if (ret
& BDRV_BLOCK_ZERO
) {
750 iscsi_allocmap_set_unallocated(iscsilun
, sector_num
, *pnum
);
752 iscsi_allocmap_set_allocated(iscsilun
, sector_num
, *pnum
);
755 if (*pnum
> nb_sectors
) {
759 qemu_mutex_unlock(&iscsilun
->mutex
);
760 g_free(iTask
.err_str
);
762 if (iTask
.task
!= NULL
) {
763 scsi_free_scsi_task(iTask
.task
);
765 if (ret
> 0 && ret
& BDRV_BLOCK_OFFSET_VALID
) {
771 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
772 int64_t sector_num
, int nb_sectors
,
775 IscsiLun
*iscsilun
= bs
->opaque
;
776 struct IscsiTask iTask
;
778 uint32_t num_sectors
;
781 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
785 if (bs
->bl
.max_transfer
) {
786 assert(nb_sectors
<< BDRV_SECTOR_BITS
<= bs
->bl
.max_transfer
);
789 /* if cache.direct is off and we have a valid entry in our allocation map
790 * we can skip checking the block status and directly return zeroes if
791 * the request falls within an unallocated area */
792 if (iscsi_allocmap_is_valid(iscsilun
, sector_num
, nb_sectors
) &&
793 !iscsi_allocmap_is_allocated(iscsilun
, sector_num
, nb_sectors
)) {
794 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
798 if (nb_sectors
>= ISCSI_CHECKALLOC_THRES
&&
799 !iscsi_allocmap_is_valid(iscsilun
, sector_num
, nb_sectors
) &&
800 !iscsi_allocmap_is_allocated(iscsilun
, sector_num
, nb_sectors
)) {
802 BlockDriverState
*file
;
803 /* check the block status from the beginning of the cluster
804 * containing the start sector */
805 int cluster_sectors
= iscsilun
->cluster_size
>> BDRV_SECTOR_BITS
;
809 assert(cluster_sectors
);
810 head
= sector_num
% cluster_sectors
;
811 ret
= iscsi_co_get_block_status(bs
, sector_num
- head
,
812 BDRV_REQUEST_MAX_SECTORS
, &pnum
,
817 /* if the whole request falls into an unallocated area we can avoid
818 * reading and directly return zeroes instead */
819 if (ret
& BDRV_BLOCK_ZERO
&& pnum
>= nb_sectors
+ head
) {
820 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
825 lba
= sector_qemu2lun(sector_num
, iscsilun
);
826 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
828 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
829 qemu_mutex_lock(&iscsilun
->mutex
);
831 if (iscsilun
->use_16_for_rw
) {
832 #if LIBISCSI_API_VERSION >= (20160603)
833 iTask
.task
= iscsi_read16_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
834 num_sectors
* iscsilun
->block_size
,
835 iscsilun
->block_size
, 0, 0, 0, 0, 0,
836 iscsi_co_generic_cb
, &iTask
,
837 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
839 iTask
.task
= iscsi_read10_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
840 num_sectors
* iscsilun
->block_size
,
841 iscsilun
->block_size
,
843 iscsi_co_generic_cb
, &iTask
,
844 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
847 iTask
.task
= iscsi_read16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
848 num_sectors
* iscsilun
->block_size
,
849 iscsilun
->block_size
, 0, 0, 0, 0, 0,
850 iscsi_co_generic_cb
, &iTask
);
852 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
853 num_sectors
* iscsilun
->block_size
,
854 iscsilun
->block_size
,
856 iscsi_co_generic_cb
, &iTask
);
859 if (iTask
.task
== NULL
) {
860 qemu_mutex_unlock(&iscsilun
->mutex
);
863 #if LIBISCSI_API_VERSION < (20160603)
864 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
866 while (!iTask
.complete
) {
867 iscsi_set_events(iscsilun
);
868 qemu_mutex_unlock(&iscsilun
->mutex
);
869 qemu_coroutine_yield();
870 qemu_mutex_lock(&iscsilun
->mutex
);
873 if (iTask
.task
!= NULL
) {
874 scsi_free_scsi_task(iTask
.task
);
878 if (iTask
.do_retry
) {
883 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
884 error_report("iSCSI READ10/16 failed at lba %" PRIu64
": %s",
889 qemu_mutex_unlock(&iscsilun
->mutex
);
890 g_free(iTask
.err_str
);
894 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
896 IscsiLun
*iscsilun
= bs
->opaque
;
897 struct IscsiTask iTask
;
900 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
901 qemu_mutex_lock(&iscsilun
->mutex
);
903 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
904 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
905 qemu_mutex_unlock(&iscsilun
->mutex
);
909 while (!iTask
.complete
) {
910 iscsi_set_events(iscsilun
);
911 qemu_mutex_unlock(&iscsilun
->mutex
);
912 qemu_coroutine_yield();
913 qemu_mutex_lock(&iscsilun
->mutex
);
916 if (iTask
.task
!= NULL
) {
917 scsi_free_scsi_task(iTask
.task
);
921 if (iTask
.do_retry
) {
926 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
927 error_report("iSCSI SYNCHRONIZECACHE10 failed: %s", iTask
.err_str
);
931 qemu_mutex_unlock(&iscsilun
->mutex
);
932 g_free(iTask
.err_str
);
937 /* Called (via iscsi_service) with QemuMutex held. */
939 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
940 void *command_data
, void *opaque
)
942 IscsiAIOCB
*acb
= opaque
;
949 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
950 iscsi_get_error(iscsi
));
951 acb
->status
= iscsi_translate_sense(&acb
->task
->sense
);
954 acb
->ioh
->driver_status
= 0;
955 acb
->ioh
->host_status
= 0;
957 acb
->ioh
->status
= status
;
959 #define SG_ERR_DRIVER_SENSE 0x08
961 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
964 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
966 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
967 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
968 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
969 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
972 iscsi_schedule_bh(acb
);
975 static void iscsi_ioctl_bh_completion(void *opaque
)
977 IscsiAIOCB
*acb
= opaque
;
979 qemu_bh_delete(acb
->bh
);
980 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
984 static void iscsi_ioctl_handle_emulated(IscsiAIOCB
*acb
, int req
, void *buf
)
986 BlockDriverState
*bs
= acb
->common
.bs
;
987 IscsiLun
*iscsilun
= bs
->opaque
;
991 case SG_GET_VERSION_NUM
:
995 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
1001 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
),
1002 iscsi_ioctl_bh_completion
, acb
);
1004 qemu_bh_schedule(acb
->bh
);
1007 static BlockAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
1008 unsigned long int req
, void *buf
,
1009 BlockCompletionFunc
*cb
, void *opaque
)
1011 IscsiLun
*iscsilun
= bs
->opaque
;
1012 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1013 struct iscsi_data data
;
1016 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
1018 acb
->iscsilun
= iscsilun
;
1020 acb
->status
= -EINPROGRESS
;
1025 iscsi_ioctl_handle_emulated(acb
, req
, buf
);
1026 return &acb
->common
;
1029 if (acb
->ioh
->cmd_len
> SCSI_CDB_MAX_SIZE
) {
1030 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
1031 acb
->ioh
->cmd_len
, SCSI_CDB_MAX_SIZE
);
1032 qemu_aio_unref(acb
);
1036 acb
->task
= malloc(sizeof(struct scsi_task
));
1037 if (acb
->task
== NULL
) {
1038 error_report("iSCSI: Failed to allocate task for scsi command. %s",
1039 iscsi_get_error(iscsi
));
1040 qemu_aio_unref(acb
);
1043 memset(acb
->task
, 0, sizeof(struct scsi_task
));
1045 switch (acb
->ioh
->dxfer_direction
) {
1046 case SG_DXFER_TO_DEV
:
1047 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
1049 case SG_DXFER_FROM_DEV
:
1050 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
1053 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
1057 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
1058 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
1059 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
1062 qemu_mutex_lock(&iscsilun
->mutex
);
1063 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
1064 if (acb
->ioh
->iovec_count
== 0) {
1065 data
.data
= acb
->ioh
->dxferp
;
1066 data
.size
= acb
->ioh
->dxfer_len
;
1068 scsi_task_set_iov_out(acb
->task
,
1069 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1070 acb
->ioh
->iovec_count
);
1074 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
1076 (data
.size
> 0) ? &data
: NULL
,
1078 qemu_mutex_unlock(&iscsilun
->mutex
);
1079 scsi_free_scsi_task(acb
->task
);
1080 qemu_aio_unref(acb
);
1084 /* tell libiscsi to read straight into the buffer we got from ioctl */
1085 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
1086 if (acb
->ioh
->iovec_count
== 0) {
1087 scsi_task_add_data_in_buffer(acb
->task
,
1088 acb
->ioh
->dxfer_len
,
1091 scsi_task_set_iov_in(acb
->task
,
1092 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1093 acb
->ioh
->iovec_count
);
1097 iscsi_set_events(iscsilun
);
1098 qemu_mutex_unlock(&iscsilun
->mutex
);
1100 return &acb
->common
;
1106 iscsi_getlength(BlockDriverState
*bs
)
1108 IscsiLun
*iscsilun
= bs
->opaque
;
1111 len
= iscsilun
->num_blocks
;
1112 len
*= iscsilun
->block_size
;
1118 coroutine_fn
iscsi_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int bytes
)
1120 IscsiLun
*iscsilun
= bs
->opaque
;
1121 struct IscsiTask iTask
;
1122 struct unmap_list list
;
1125 if (!is_byte_request_lun_aligned(offset
, bytes
, iscsilun
)) {
1129 if (!iscsilun
->lbp
.lbpu
) {
1130 /* UNMAP is not supported by the target */
1134 list
.lba
= offset
/ iscsilun
->block_size
;
1135 list
.num
= bytes
/ iscsilun
->block_size
;
1137 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1138 qemu_mutex_lock(&iscsilun
->mutex
);
1140 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
1141 iscsi_co_generic_cb
, &iTask
) == NULL
) {
1146 while (!iTask
.complete
) {
1147 iscsi_set_events(iscsilun
);
1148 qemu_mutex_unlock(&iscsilun
->mutex
);
1149 qemu_coroutine_yield();
1150 qemu_mutex_lock(&iscsilun
->mutex
);
1153 if (iTask
.task
!= NULL
) {
1154 scsi_free_scsi_task(iTask
.task
);
1158 if (iTask
.do_retry
) {
1163 iscsi_allocmap_set_invalid(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1164 bytes
>> BDRV_SECTOR_BITS
);
1166 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
) {
1167 /* the target might fail with a check condition if it
1168 is not happy with the alignment of the UNMAP request
1169 we silently fail in this case */
1173 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1174 error_report("iSCSI UNMAP failed at lba %" PRIu64
": %s",
1175 list
.lba
, iTask
.err_str
);
1181 qemu_mutex_unlock(&iscsilun
->mutex
);
1182 g_free(iTask
.err_str
);
1187 coroutine_fn
iscsi_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1188 int bytes
, BdrvRequestFlags flags
)
1190 IscsiLun
*iscsilun
= bs
->opaque
;
1191 struct IscsiTask iTask
;
1194 bool use_16_for_ws
= iscsilun
->use_16_for_rw
;
1197 if (!is_byte_request_lun_aligned(offset
, bytes
, iscsilun
)) {
1201 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1202 if (!use_16_for_ws
&& !iscsilun
->lbp
.lbpws10
) {
1203 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1204 use_16_for_ws
= true;
1206 if (use_16_for_ws
&& !iscsilun
->lbp
.lbpws
) {
1207 /* WRITESAME16 with UNMAP is not supported by the target,
1208 * fall back and try WRITESAME10/16 without UNMAP */
1209 flags
&= ~BDRV_REQ_MAY_UNMAP
;
1210 use_16_for_ws
= iscsilun
->use_16_for_rw
;
1214 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
1215 /* WRITESAME without UNMAP is not supported by the target */
1219 lba
= offset
/ iscsilun
->block_size
;
1220 nb_blocks
= bytes
/ iscsilun
->block_size
;
1222 if (iscsilun
->zeroblock
== NULL
) {
1223 iscsilun
->zeroblock
= g_try_malloc0(iscsilun
->block_size
);
1224 if (iscsilun
->zeroblock
== NULL
) {
1229 qemu_mutex_lock(&iscsilun
->mutex
);
1230 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1232 if (use_16_for_ws
) {
1233 iTask
.task
= iscsi_writesame16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1234 iscsilun
->zeroblock
, iscsilun
->block_size
,
1235 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1236 0, 0, iscsi_co_generic_cb
, &iTask
);
1238 iTask
.task
= iscsi_writesame10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1239 iscsilun
->zeroblock
, iscsilun
->block_size
,
1240 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1241 0, 0, iscsi_co_generic_cb
, &iTask
);
1243 if (iTask
.task
== NULL
) {
1244 qemu_mutex_unlock(&iscsilun
->mutex
);
1248 while (!iTask
.complete
) {
1249 iscsi_set_events(iscsilun
);
1250 qemu_mutex_unlock(&iscsilun
->mutex
);
1251 qemu_coroutine_yield();
1252 qemu_mutex_lock(&iscsilun
->mutex
);
1255 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
&&
1256 iTask
.task
->sense
.key
== SCSI_SENSE_ILLEGAL_REQUEST
&&
1257 (iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
||
1258 iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
)) {
1259 /* WRITE SAME is not supported by the target */
1260 iscsilun
->has_write_same
= false;
1261 scsi_free_scsi_task(iTask
.task
);
1266 if (iTask
.task
!= NULL
) {
1267 scsi_free_scsi_task(iTask
.task
);
1271 if (iTask
.do_retry
) {
1276 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1277 iscsi_allocmap_set_invalid(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1278 bytes
>> BDRV_SECTOR_BITS
);
1279 error_report("iSCSI WRITESAME10/16 failed at lba %" PRIu64
": %s",
1280 lba
, iTask
.err_str
);
1285 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1286 iscsi_allocmap_set_invalid(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1287 bytes
>> BDRV_SECTOR_BITS
);
1289 iscsi_allocmap_set_allocated(iscsilun
, offset
>> BDRV_SECTOR_BITS
,
1290 bytes
>> BDRV_SECTOR_BITS
);
1294 qemu_mutex_unlock(&iscsilun
->mutex
);
1295 g_free(iTask
.err_str
);
1299 static void apply_chap(struct iscsi_context
*iscsi
, QemuOpts
*opts
,
1302 const char *user
= NULL
;
1303 const char *password
= NULL
;
1304 const char *secretid
;
1305 char *secret
= NULL
;
1307 user
= qemu_opt_get(opts
, "user");
1312 secretid
= qemu_opt_get(opts
, "password-secret");
1313 password
= qemu_opt_get(opts
, "password");
1314 if (secretid
&& password
) {
1315 error_setg(errp
, "'password' and 'password-secret' properties are "
1316 "mutually exclusive");
1320 secret
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
1325 } else if (!password
) {
1326 error_setg(errp
, "CHAP username specified but no password was given");
1330 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
1331 error_setg(errp
, "Failed to set initiator username and password");
1337 static void apply_header_digest(struct iscsi_context
*iscsi
, QemuOpts
*opts
,
1340 const char *digest
= NULL
;
1342 digest
= qemu_opt_get(opts
, "header-digest");
1344 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1345 } else if (!strcmp(digest
, "crc32c")) {
1346 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
1347 } else if (!strcmp(digest
, "none")) {
1348 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
1349 } else if (!strcmp(digest
, "crc32c-none")) {
1350 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
1351 } else if (!strcmp(digest
, "none-crc32c")) {
1352 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1354 error_setg(errp
, "Invalid header-digest setting : %s", digest
);
1358 static char *get_initiator_name(QemuOpts
*opts
)
1362 UuidInfo
*uuid_info
;
1364 name
= qemu_opt_get(opts
, "initiator-name");
1366 return g_strdup(name
);
1369 uuid_info
= qmp_query_uuid(NULL
);
1370 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
1371 name
= qemu_get_vm_name();
1373 name
= uuid_info
->UUID
;
1375 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1376 name
? ":" : "", name
? name
: "");
1377 qapi_free_UuidInfo(uuid_info
);
1381 static void iscsi_nop_timed_event(void *opaque
)
1383 IscsiLun
*iscsilun
= opaque
;
1385 qemu_mutex_lock(&iscsilun
->mutex
);
1386 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) >= MAX_NOP_FAILURES
) {
1387 error_report("iSCSI: NOP timeout. Reconnecting...");
1388 iscsilun
->request_timed_out
= true;
1389 } else if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
1390 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1394 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1395 iscsi_set_events(iscsilun
);
1398 qemu_mutex_unlock(&iscsilun
->mutex
);
1401 static void iscsi_readcapacity_sync(IscsiLun
*iscsilun
, Error
**errp
)
1403 struct scsi_task
*task
= NULL
;
1404 struct scsi_readcapacity10
*rc10
= NULL
;
1405 struct scsi_readcapacity16
*rc16
= NULL
;
1406 int retries
= ISCSI_CMD_RETRIES
;
1410 scsi_free_scsi_task(task
);
1414 switch (iscsilun
->type
) {
1416 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
1417 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1418 rc16
= scsi_datain_unmarshall(task
);
1420 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity16 data.");
1422 iscsilun
->block_size
= rc16
->block_length
;
1423 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1424 iscsilun
->lbpme
= !!rc16
->lbpme
;
1425 iscsilun
->lbprz
= !!rc16
->lbprz
;
1426 iscsilun
->use_16_for_rw
= (rc16
->returned_lba
> 0xffffffff);
1430 if (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1431 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
1434 /* Fall through and try READ CAPACITY(10) instead. */
1436 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1437 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1438 rc10
= scsi_datain_unmarshall(task
);
1440 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity10 data.");
1442 iscsilun
->block_size
= rc10
->block_size
;
1443 if (rc10
->lba
== 0) {
1444 /* blank disk loaded */
1445 iscsilun
->num_blocks
= 0;
1447 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1455 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1456 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1459 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1460 error_setg(errp
, "iSCSI: failed to send readcapacity10/16 command");
1461 } else if (!iscsilun
->block_size
||
1462 iscsilun
->block_size
% BDRV_SECTOR_SIZE
) {
1463 error_setg(errp
, "iSCSI: the target returned an invalid "
1464 "block size of %d.", iscsilun
->block_size
);
1467 scsi_free_scsi_task(task
);
1471 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
, int lun
,
1472 int evpd
, int pc
, void **inq
, Error
**errp
)
1475 struct scsi_task
*task
= NULL
;
1476 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, 64);
1477 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1480 full_size
= scsi_datain_getfullsize(task
);
1481 if (full_size
> task
->datain
.size
) {
1482 scsi_free_scsi_task(task
);
1484 /* we need more data for the full list */
1485 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, full_size
);
1486 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1491 *inq
= scsi_datain_unmarshall(task
);
1493 error_setg(errp
, "iSCSI: failed to unmarshall inquiry datain blob");
1500 error_setg(errp
, "iSCSI: Inquiry command failed : %s",
1501 iscsi_get_error(iscsi
));
1504 scsi_free_scsi_task(task
);
1509 static void iscsi_detach_aio_context(BlockDriverState
*bs
)
1511 IscsiLun
*iscsilun
= bs
->opaque
;
1513 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsilun
->iscsi
),
1514 false, NULL
, NULL
, NULL
, NULL
);
1515 iscsilun
->events
= 0;
1517 if (iscsilun
->nop_timer
) {
1518 timer_del(iscsilun
->nop_timer
);
1519 timer_free(iscsilun
->nop_timer
);
1520 iscsilun
->nop_timer
= NULL
;
1522 if (iscsilun
->event_timer
) {
1523 timer_del(iscsilun
->event_timer
);
1524 timer_free(iscsilun
->event_timer
);
1525 iscsilun
->event_timer
= NULL
;
1529 static void iscsi_attach_aio_context(BlockDriverState
*bs
,
1530 AioContext
*new_context
)
1532 IscsiLun
*iscsilun
= bs
->opaque
;
1534 iscsilun
->aio_context
= new_context
;
1535 iscsi_set_events(iscsilun
);
1537 /* Set up a timer for sending out iSCSI NOPs */
1538 iscsilun
->nop_timer
= aio_timer_new(iscsilun
->aio_context
,
1539 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1540 iscsi_nop_timed_event
, iscsilun
);
1541 timer_mod(iscsilun
->nop_timer
,
1542 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1544 /* Set up a timer for periodic calls to iscsi_set_events and to
1545 * scan for command timeout */
1546 iscsilun
->event_timer
= aio_timer_new(iscsilun
->aio_context
,
1547 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1548 iscsi_timed_check_events
, iscsilun
);
1549 timer_mod(iscsilun
->event_timer
,
1550 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
1553 static void iscsi_modesense_sync(IscsiLun
*iscsilun
)
1555 struct scsi_task
*task
;
1556 struct scsi_mode_sense
*ms
= NULL
;
1557 iscsilun
->write_protected
= false;
1558 iscsilun
->dpofua
= false;
1560 task
= iscsi_modesense6_sync(iscsilun
->iscsi
, iscsilun
->lun
,
1561 1, SCSI_MODESENSE_PC_CURRENT
,
1564 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1565 iscsi_get_error(iscsilun
->iscsi
));
1569 if (task
->status
!= SCSI_STATUS_GOOD
) {
1570 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1573 ms
= scsi_datain_unmarshall(task
);
1575 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1576 iscsi_get_error(iscsilun
->iscsi
));
1579 iscsilun
->write_protected
= ms
->device_specific_parameter
& 0x80;
1580 iscsilun
->dpofua
= ms
->device_specific_parameter
& 0x10;
1584 scsi_free_scsi_task(task
);
1588 static void iscsi_parse_iscsi_option(const char *target
, QDict
*options
)
1592 const char *user
, *password
, *password_secret
, *initiator_name
,
1593 *header_digest
, *timeout
;
1595 list
= qemu_find_opts("iscsi");
1600 opts
= qemu_opts_find(list
, target
);
1602 opts
= QTAILQ_FIRST(&list
->head
);
1608 user
= qemu_opt_get(opts
, "user");
1610 qdict_set_default_str(options
, "user", user
);
1613 password
= qemu_opt_get(opts
, "password");
1615 qdict_set_default_str(options
, "password", password
);
1618 password_secret
= qemu_opt_get(opts
, "password-secret");
1619 if (password_secret
) {
1620 qdict_set_default_str(options
, "password-secret", password_secret
);
1623 initiator_name
= qemu_opt_get(opts
, "initiator-name");
1624 if (initiator_name
) {
1625 qdict_set_default_str(options
, "initiator-name", initiator_name
);
1628 header_digest
= qemu_opt_get(opts
, "header-digest");
1629 if (header_digest
) {
1630 /* -iscsi takes upper case values, but QAPI only supports lower case
1631 * enum constant names, so we have to convert here. */
1632 char *qapi_value
= g_ascii_strdown(header_digest
, -1);
1633 qdict_set_default_str(options
, "header-digest", qapi_value
);
1637 timeout
= qemu_opt_get(opts
, "timeout");
1639 qdict_set_default_str(options
, "timeout", timeout
);
1644 * We support iscsi url's on the form
1645 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1647 static void iscsi_parse_filename(const char *filename
, QDict
*options
,
1650 struct iscsi_url
*iscsi_url
;
1651 const char *transport_name
;
1654 iscsi_url
= iscsi_parse_full_url(NULL
, filename
);
1655 if (iscsi_url
== NULL
) {
1656 error_setg(errp
, "Failed to parse URL : %s", filename
);
1660 #if LIBISCSI_API_VERSION >= (20160603)
1661 switch (iscsi_url
->transport
) {
1663 transport_name
= "tcp";
1665 case ISER_TRANSPORT
:
1666 transport_name
= "iser";
1669 error_setg(errp
, "Unknown transport type (%d)",
1670 iscsi_url
->transport
);
1674 transport_name
= "tcp";
1677 qdict_set_default_str(options
, "transport", transport_name
);
1678 qdict_set_default_str(options
, "portal", iscsi_url
->portal
);
1679 qdict_set_default_str(options
, "target", iscsi_url
->target
);
1681 lun_str
= g_strdup_printf("%d", iscsi_url
->lun
);
1682 qdict_set_default_str(options
, "lun", lun_str
);
1685 /* User/password from -iscsi take precedence over those from the URL */
1686 iscsi_parse_iscsi_option(iscsi_url
->target
, options
);
1688 if (iscsi_url
->user
[0] != '\0') {
1689 qdict_set_default_str(options
, "user", iscsi_url
->user
);
1690 qdict_set_default_str(options
, "password", iscsi_url
->passwd
);
1693 iscsi_destroy_url(iscsi_url
);
1696 static QemuOptsList runtime_opts
= {
1698 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1701 .name
= "transport",
1702 .type
= QEMU_OPT_STRING
,
1706 .type
= QEMU_OPT_STRING
,
1710 .type
= QEMU_OPT_STRING
,
1714 .type
= QEMU_OPT_STRING
,
1718 .type
= QEMU_OPT_STRING
,
1721 .name
= "password-secret",
1722 .type
= QEMU_OPT_STRING
,
1726 .type
= QEMU_OPT_NUMBER
,
1729 .name
= "initiator-name",
1730 .type
= QEMU_OPT_STRING
,
1733 .name
= "header-digest",
1734 .type
= QEMU_OPT_STRING
,
1738 .type
= QEMU_OPT_NUMBER
,
1742 .type
= QEMU_OPT_STRING
,
1744 { /* end of list */ }
1748 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1751 IscsiLun
*iscsilun
= bs
->opaque
;
1752 struct iscsi_context
*iscsi
= NULL
;
1753 struct scsi_task
*task
= NULL
;
1754 struct scsi_inquiry_standard
*inq
= NULL
;
1755 struct scsi_inquiry_supported_pages
*inq_vpd
;
1756 char *initiator_name
= NULL
;
1758 Error
*local_err
= NULL
;
1759 const char *transport_name
, *portal
, *target
, *filename
;
1760 #if LIBISCSI_API_VERSION >= (20160603)
1761 enum iscsi_transport_type transport
;
1763 int i
, ret
= 0, timeout
= 0, lun
;
1765 /* If we are given a filename, parse the filename, with precedence given to
1766 * filename encoded options */
1767 filename
= qdict_get_try_str(options
, "filename");
1769 warn_report("'filename' option specified. "
1770 "This is an unsupported option, and may be deprecated "
1772 iscsi_parse_filename(filename
, options
, &local_err
);
1775 error_propagate(errp
, local_err
);
1780 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1781 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1783 error_propagate(errp
, local_err
);
1788 transport_name
= qemu_opt_get(opts
, "transport");
1789 portal
= qemu_opt_get(opts
, "portal");
1790 target
= qemu_opt_get(opts
, "target");
1791 lun
= qemu_opt_get_number(opts
, "lun", 0);
1793 if (!transport_name
|| !portal
|| !target
) {
1794 error_setg(errp
, "Need all of transport, portal and target options");
1799 if (!strcmp(transport_name
, "tcp")) {
1800 #if LIBISCSI_API_VERSION >= (20160603)
1801 transport
= TCP_TRANSPORT
;
1802 } else if (!strcmp(transport_name
, "iser")) {
1803 transport
= ISER_TRANSPORT
;
1805 /* TCP is what older libiscsi versions always use */
1808 error_setg(errp
, "Unknown transport: %s", transport_name
);
1813 memset(iscsilun
, 0, sizeof(IscsiLun
));
1815 initiator_name
= get_initiator_name(opts
);
1817 iscsi
= iscsi_create_context(initiator_name
);
1818 if (iscsi
== NULL
) {
1819 error_setg(errp
, "iSCSI: Failed to create iSCSI context.");
1823 #if LIBISCSI_API_VERSION >= (20160603)
1824 if (iscsi_init_transport(iscsi
, transport
)) {
1825 error_setg(errp
, ("Error initializing transport."));
1830 if (iscsi_set_targetname(iscsi
, target
)) {
1831 error_setg(errp
, "iSCSI: Failed to set target name.");
1836 /* check if we got CHAP username/password via the options */
1837 apply_chap(iscsi
, opts
, &local_err
);
1838 if (local_err
!= NULL
) {
1839 error_propagate(errp
, local_err
);
1844 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1845 error_setg(errp
, "iSCSI: Failed to set session type to normal.");
1850 /* check if we got HEADER_DIGEST via the options */
1851 apply_header_digest(iscsi
, opts
, &local_err
);
1852 if (local_err
!= NULL
) {
1853 error_propagate(errp
, local_err
);
1858 /* timeout handling is broken in libiscsi before 1.15.0 */
1859 timeout
= qemu_opt_get_number(opts
, "timeout", 0);
1860 #if LIBISCSI_API_VERSION >= 20150621
1861 iscsi_set_timeout(iscsi
, timeout
);
1864 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1868 if (iscsi_full_connect_sync(iscsi
, portal
, lun
) != 0) {
1869 error_setg(errp
, "iSCSI: Failed to connect to LUN : %s",
1870 iscsi_get_error(iscsi
));
1875 iscsilun
->iscsi
= iscsi
;
1876 iscsilun
->aio_context
= bdrv_get_aio_context(bs
);
1877 iscsilun
->lun
= lun
;
1878 iscsilun
->has_write_same
= true;
1880 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0,
1881 (void **) &inq
, errp
);
1886 iscsilun
->type
= inq
->periperal_device_type
;
1887 scsi_free_scsi_task(task
);
1890 iscsi_modesense_sync(iscsilun
);
1891 if (iscsilun
->dpofua
) {
1892 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1895 /* Check the write protect flag of the LUN if we want to write */
1896 if (iscsilun
->type
== TYPE_DISK
&& (flags
& BDRV_O_RDWR
) &&
1897 iscsilun
->write_protected
) {
1898 error_setg(errp
, "Cannot open a write protected LUN as read-write");
1903 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1904 if (local_err
!= NULL
) {
1905 error_propagate(errp
, local_err
);
1909 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1911 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1912 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1913 * will try to read from the device to guess the image format.
1915 if (iscsilun
->type
!= TYPE_DISK
&& iscsilun
->type
!= TYPE_ROM
) {
1919 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1920 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES
,
1921 (void **) &inq_vpd
, errp
);
1926 for (i
= 0; i
< inq_vpd
->num_pages
; i
++) {
1927 struct scsi_task
*inq_task
;
1928 struct scsi_inquiry_logical_block_provisioning
*inq_lbp
;
1929 struct scsi_inquiry_block_limits
*inq_bl
;
1930 switch (inq_vpd
->pages
[i
]) {
1931 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
:
1932 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1933 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
,
1934 (void **) &inq_lbp
, errp
);
1935 if (inq_task
== NULL
) {
1939 memcpy(&iscsilun
->lbp
, inq_lbp
,
1940 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1941 scsi_free_scsi_task(inq_task
);
1943 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
:
1944 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1945 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
,
1946 (void **) &inq_bl
, errp
);
1947 if (inq_task
== NULL
) {
1951 memcpy(&iscsilun
->bl
, inq_bl
,
1952 sizeof(struct scsi_inquiry_block_limits
));
1953 scsi_free_scsi_task(inq_task
);
1959 scsi_free_scsi_task(task
);
1962 qemu_mutex_init(&iscsilun
->mutex
);
1963 iscsi_attach_aio_context(bs
, iscsilun
->aio_context
);
1965 /* Guess the internal cluster (page) size of the iscsi target by the means
1966 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1967 * reasonable size */
1968 if (iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
>= 4 * 1024 &&
1969 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
<= 16 * 1024 * 1024) {
1970 iscsilun
->cluster_size
= iscsilun
->bl
.opt_unmap_gran
*
1971 iscsilun
->block_size
;
1972 if (iscsilun
->lbprz
) {
1973 ret
= iscsi_allocmap_init(iscsilun
, bs
->open_flags
);
1977 if (iscsilun
->lbprz
&& iscsilun
->lbp
.lbpws
) {
1978 bs
->supported_zero_flags
= BDRV_REQ_MAY_UNMAP
;
1982 qemu_opts_del(opts
);
1983 g_free(initiator_name
);
1985 scsi_free_scsi_task(task
);
1989 if (iscsi
!= NULL
) {
1990 if (iscsi_is_logged_in(iscsi
)) {
1991 iscsi_logout_sync(iscsi
);
1993 iscsi_destroy_context(iscsi
);
1995 memset(iscsilun
, 0, sizeof(IscsiLun
));
2001 static void iscsi_close(BlockDriverState
*bs
)
2003 IscsiLun
*iscsilun
= bs
->opaque
;
2004 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
2006 iscsi_detach_aio_context(bs
);
2007 if (iscsi_is_logged_in(iscsi
)) {
2008 iscsi_logout_sync(iscsi
);
2010 iscsi_destroy_context(iscsi
);
2011 g_free(iscsilun
->zeroblock
);
2012 iscsi_allocmap_free(iscsilun
);
2013 qemu_mutex_destroy(&iscsilun
->mutex
);
2014 memset(iscsilun
, 0, sizeof(IscsiLun
));
2017 static void iscsi_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
2019 /* We don't actually refresh here, but just return data queried in
2020 * iscsi_open(): iscsi targets don't change their limits. */
2022 IscsiLun
*iscsilun
= bs
->opaque
;
2023 uint64_t max_xfer_len
= iscsilun
->use_16_for_rw
? 0xffffffff : 0xffff;
2024 unsigned int block_size
= MAX(BDRV_SECTOR_SIZE
, iscsilun
->block_size
);
2026 assert(iscsilun
->block_size
>= BDRV_SECTOR_SIZE
|| bs
->sg
);
2028 bs
->bl
.request_alignment
= block_size
;
2030 if (iscsilun
->bl
.max_xfer_len
) {
2031 max_xfer_len
= MIN(max_xfer_len
, iscsilun
->bl
.max_xfer_len
);
2034 if (max_xfer_len
* block_size
< INT_MAX
) {
2035 bs
->bl
.max_transfer
= max_xfer_len
* iscsilun
->block_size
;
2038 if (iscsilun
->lbp
.lbpu
) {
2039 if (iscsilun
->bl
.max_unmap
< 0xffffffff / block_size
) {
2040 bs
->bl
.max_pdiscard
=
2041 iscsilun
->bl
.max_unmap
* iscsilun
->block_size
;
2043 bs
->bl
.pdiscard_alignment
=
2044 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
2046 bs
->bl
.pdiscard_alignment
= iscsilun
->block_size
;
2049 if (iscsilun
->bl
.max_ws_len
< 0xffffffff / block_size
) {
2050 bs
->bl
.max_pwrite_zeroes
=
2051 iscsilun
->bl
.max_ws_len
* iscsilun
->block_size
;
2053 if (iscsilun
->lbp
.lbpws
) {
2054 bs
->bl
.pwrite_zeroes_alignment
=
2055 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
2057 bs
->bl
.pwrite_zeroes_alignment
= iscsilun
->block_size
;
2059 if (iscsilun
->bl
.opt_xfer_len
&&
2060 iscsilun
->bl
.opt_xfer_len
< INT_MAX
/ block_size
) {
2061 bs
->bl
.opt_transfer
= pow2floor(iscsilun
->bl
.opt_xfer_len
*
2062 iscsilun
->block_size
);
2066 /* Note that this will not re-establish a connection with an iSCSI target - it
2067 * is effectively a NOP. */
2068 static int iscsi_reopen_prepare(BDRVReopenState
*state
,
2069 BlockReopenQueue
*queue
, Error
**errp
)
2071 IscsiLun
*iscsilun
= state
->bs
->opaque
;
2073 if (state
->flags
& BDRV_O_RDWR
&& iscsilun
->write_protected
) {
2074 error_setg(errp
, "Cannot open a write protected LUN as read-write");
2080 static void iscsi_reopen_commit(BDRVReopenState
*reopen_state
)
2082 IscsiLun
*iscsilun
= reopen_state
->bs
->opaque
;
2084 /* the cache.direct status might have changed */
2085 if (iscsilun
->allocmap
!= NULL
) {
2086 iscsi_allocmap_init(iscsilun
, reopen_state
->flags
);
2090 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
,
2091 PreallocMode prealloc
, Error
**errp
)
2093 IscsiLun
*iscsilun
= bs
->opaque
;
2094 Error
*local_err
= NULL
;
2096 if (prealloc
!= PREALLOC_MODE_OFF
) {
2097 error_setg(errp
, "Unsupported preallocation mode '%s'",
2098 PreallocMode_str(prealloc
));
2102 if (iscsilun
->type
!= TYPE_DISK
) {
2103 error_setg(errp
, "Cannot resize non-disk iSCSI devices");
2107 iscsi_readcapacity_sync(iscsilun
, &local_err
);
2108 if (local_err
!= NULL
) {
2109 error_propagate(errp
, local_err
);
2113 if (offset
> iscsi_getlength(bs
)) {
2114 error_setg(errp
, "Cannot grow iSCSI devices");
2118 if (iscsilun
->allocmap
!= NULL
) {
2119 iscsi_allocmap_init(iscsilun
, bs
->open_flags
);
2125 static int iscsi_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
2128 int64_t total_size
= 0;
2129 BlockDriverState
*bs
;
2130 IscsiLun
*iscsilun
= NULL
;
2132 Error
*local_err
= NULL
;
2136 /* Read out options */
2137 total_size
= DIV_ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2139 bs
->opaque
= g_new0(struct IscsiLun
, 1);
2140 iscsilun
= bs
->opaque
;
2142 bs_options
= qdict_new();
2143 iscsi_parse_filename(filename
, bs_options
, &local_err
);
2145 error_propagate(errp
, local_err
);
2148 ret
= iscsi_open(bs
, bs_options
, 0, NULL
);
2150 QDECREF(bs_options
);
2155 iscsi_detach_aio_context(bs
);
2156 if (iscsilun
->type
!= TYPE_DISK
) {
2160 if (bs
->total_sectors
< total_size
) {
2167 if (iscsilun
->iscsi
!= NULL
) {
2168 iscsi_destroy_context(iscsilun
->iscsi
);
2176 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2178 IscsiLun
*iscsilun
= bs
->opaque
;
2179 bdi
->unallocated_blocks_are_zero
= iscsilun
->lbprz
;
2180 bdi
->cluster_size
= iscsilun
->cluster_size
;
2184 static void iscsi_invalidate_cache(BlockDriverState
*bs
,
2187 IscsiLun
*iscsilun
= bs
->opaque
;
2188 iscsi_allocmap_invalidate(iscsilun
);
2191 static QemuOptsList iscsi_create_opts
= {
2192 .name
= "iscsi-create-opts",
2193 .head
= QTAILQ_HEAD_INITIALIZER(iscsi_create_opts
.head
),
2196 .name
= BLOCK_OPT_SIZE
,
2197 .type
= QEMU_OPT_SIZE
,
2198 .help
= "Virtual disk size"
2200 { /* end of list */ }
2204 static BlockDriver bdrv_iscsi
= {
2205 .format_name
= "iscsi",
2206 .protocol_name
= "iscsi",
2208 .instance_size
= sizeof(IscsiLun
),
2209 .bdrv_parse_filename
= iscsi_parse_filename
,
2210 .bdrv_file_open
= iscsi_open
,
2211 .bdrv_close
= iscsi_close
,
2212 .bdrv_create
= iscsi_create
,
2213 .create_opts
= &iscsi_create_opts
,
2214 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
2215 .bdrv_reopen_commit
= iscsi_reopen_commit
,
2216 .bdrv_invalidate_cache
= iscsi_invalidate_cache
,
2218 .bdrv_getlength
= iscsi_getlength
,
2219 .bdrv_get_info
= iscsi_get_info
,
2220 .bdrv_truncate
= iscsi_truncate
,
2221 .bdrv_refresh_limits
= iscsi_refresh_limits
,
2223 .bdrv_co_get_block_status
= iscsi_co_get_block_status
,
2224 .bdrv_co_pdiscard
= iscsi_co_pdiscard
,
2225 .bdrv_co_pwrite_zeroes
= iscsi_co_pwrite_zeroes
,
2226 .bdrv_co_readv
= iscsi_co_readv
,
2227 .bdrv_co_writev_flags
= iscsi_co_writev_flags
,
2228 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
2231 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
2234 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
2235 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
2238 #if LIBISCSI_API_VERSION >= (20160603)
2239 static BlockDriver bdrv_iser
= {
2240 .format_name
= "iser",
2241 .protocol_name
= "iser",
2243 .instance_size
= sizeof(IscsiLun
),
2244 .bdrv_parse_filename
= iscsi_parse_filename
,
2245 .bdrv_file_open
= iscsi_open
,
2246 .bdrv_close
= iscsi_close
,
2247 .bdrv_create
= iscsi_create
,
2248 .create_opts
= &iscsi_create_opts
,
2249 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
2250 .bdrv_reopen_commit
= iscsi_reopen_commit
,
2251 .bdrv_invalidate_cache
= iscsi_invalidate_cache
,
2253 .bdrv_getlength
= iscsi_getlength
,
2254 .bdrv_get_info
= iscsi_get_info
,
2255 .bdrv_truncate
= iscsi_truncate
,
2256 .bdrv_refresh_limits
= iscsi_refresh_limits
,
2258 .bdrv_co_get_block_status
= iscsi_co_get_block_status
,
2259 .bdrv_co_pdiscard
= iscsi_co_pdiscard
,
2260 .bdrv_co_pwrite_zeroes
= iscsi_co_pwrite_zeroes
,
2261 .bdrv_co_readv
= iscsi_co_readv
,
2262 .bdrv_co_writev_flags
= iscsi_co_writev_flags
,
2263 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
2266 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
2269 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
2270 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
2274 static void iscsi_block_init(void)
2276 bdrv_register(&bdrv_iscsi
);
2277 #if LIBISCSI_API_VERSION >= (20160603)
2278 bdrv_register(&bdrv_iser
);
2282 block_init(iscsi_block_init
);