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-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/qdict.h"
38 #include "scsi/constants.h"
40 #include "qemu/module.h"
41 #include "qemu/option.h"
42 #include "qemu/uuid.h"
43 #include "sysemu/replay.h"
44 #include "qapi/error.h"
45 #include "qapi/qapi-commands-misc.h"
46 #include "qapi/qmp/qdict.h"
47 #include "qapi/qmp/qstring.h"
48 #include "crypto/secret.h"
49 #include "scsi/utils.h"
52 /* Conflict between scsi/utils.h and libiscsi! :( */
53 #define SCSI_XFER_NONE ISCSI_XFER_NONE
54 #include <iscsi/iscsi.h>
55 #define inline __attribute__((gnu_inline)) /* required for libiscsi v1.9.0 */
56 #include <iscsi/scsi-lowlevel.h>
59 QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE
!= (int)ISCSI_XFER_NONE
);
65 typedef struct IscsiLun
{
66 struct iscsi_context
*iscsi
;
67 AioContext
*aio_context
;
69 enum scsi_inquiry_peripheral_device_type type
;
74 QEMUTimer
*event_timer
;
76 struct scsi_inquiry_logical_block_provisioning lbp
;
77 struct scsi_inquiry_block_limits bl
;
78 struct scsi_inquiry_device_designator
*dd
;
79 unsigned char *zeroblock
;
80 /* The allocmap tracks which clusters (pages) on the iSCSI target are
81 * allocated and which are not. In case a target returns zeros for
82 * unallocated pages (iscsilun->lprz) we can directly return zeros instead
83 * of reading zeros over the wire if a read request falls within an
84 * unallocated block. As there are 3 possible states we need 2 bitmaps to
85 * track. allocmap_valid keeps track if QEMU's information about a page is
86 * valid. allocmap tracks if a page is allocated or not. In case QEMU has no
87 * valid information about a page the corresponding allocmap entry should be
88 * switched to unallocated as well to force a new lookup of the allocation
89 * status as lookups are generally skipped if a page is suspect to be
90 * allocated. If a iSCSI target is opened with cache.direct = on the
91 * allocmap_valid does not exist turning all cached information invalid so
92 * that a fresh lookup is made for any page even if allocmap entry returns
93 * it's unallocated. */
94 unsigned long *allocmap
;
95 unsigned long *allocmap_valid
;
104 bool request_timed_out
;
107 typedef struct IscsiTask
{
112 struct scsi_task
*task
;
115 QEMUTimer retry_timer
;
120 typedef struct IscsiAIOCB
{
124 struct scsi_task
*task
;
135 /* libiscsi uses time_t so its enough to process events every second */
136 #define EVENT_INTERVAL 1000
137 #define NOP_INTERVAL 5000
138 #define MAX_NOP_FAILURES 3
139 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
140 static const unsigned iscsi_retry_times
[] = {8, 32, 128, 512, 2048, 8192, 32768};
142 /* this threshold is a trade-off knob to choose between
143 * the potential additional overhead of an extra GET_LBA_STATUS request
144 * vs. unnecessarily reading a lot of zero sectors over the wire.
145 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
146 * sectors we check the allocation status of the area covered by the
147 * request first if the allocationmap indicates that the area might be
149 #define ISCSI_CHECKALLOC_THRES 64
158 qemu_bh_delete(acb
->bh
);
160 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
162 if (acb
->task
!= NULL
) {
163 scsi_free_scsi_task(acb
->task
);
171 iscsi_schedule_bh(IscsiAIOCB
*acb
)
176 acb
->bh
= aio_bh_new(acb
->iscsilun
->aio_context
, iscsi_bh_cb
, acb
);
177 qemu_bh_schedule(acb
->bh
);
182 static void iscsi_co_generic_bh_cb(void *opaque
)
184 struct IscsiTask
*iTask
= opaque
;
187 aio_co_wake(iTask
->co
);
190 static void iscsi_retry_timer_expired(void *opaque
)
192 struct IscsiTask
*iTask
= opaque
;
195 aio_co_wake(iTask
->co
);
199 static inline unsigned exp_random(double mean
)
201 return -mean
* log((double)rand() / RAND_MAX
);
204 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
205 * libiscsi 1.10.0, together with other constants we need. Use it as
206 * a hint that we have to define them ourselves if needed, to keep the
207 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
208 * the test because SCSI_STATUS_* is an enum.
210 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
211 * an enum, check against the LIBISCSI_API_VERSION macro, which was
212 * introduced in 1.11.0. If it is present, there is no need to define
215 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
216 !defined(LIBISCSI_API_VERSION)
217 #define SCSI_STATUS_TASK_SET_FULL 0x28
218 #define SCSI_STATUS_TIMEOUT 0x0f000002
219 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
220 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
223 #ifndef LIBISCSI_API_VERSION
224 #define LIBISCSI_API_VERSION 20130701
227 static int iscsi_translate_sense(struct scsi_sense
*sense
)
229 return scsi_sense_to_errno(sense
->key
,
230 (sense
->ascq
& 0xFF00) >> 8,
234 /* Called (via iscsi_service) with QemuMutex held. */
236 iscsi_co_generic_cb(struct iscsi_context
*iscsi
, int status
,
237 void *command_data
, void *opaque
)
239 struct IscsiTask
*iTask
= opaque
;
240 struct scsi_task
*task
= command_data
;
242 iTask
->status
= status
;
246 if (status
!= SCSI_STATUS_GOOD
) {
247 if (iTask
->retries
++ < ISCSI_CMD_RETRIES
) {
248 if (status
== SCSI_STATUS_BUSY
||
249 status
== SCSI_STATUS_TIMEOUT
||
250 status
== SCSI_STATUS_TASK_SET_FULL
) {
251 unsigned retry_time
=
252 exp_random(iscsi_retry_times
[iTask
->retries
- 1]);
253 if (status
== SCSI_STATUS_TIMEOUT
) {
254 /* make sure the request is rescheduled AFTER the
255 * reconnect is initiated */
256 retry_time
= EVENT_INTERVAL
* 2;
257 iTask
->iscsilun
->request_timed_out
= true;
259 error_report("iSCSI Busy/TaskSetFull/TimeOut"
260 " (retry #%u in %u ms): %s",
261 iTask
->retries
, retry_time
,
262 iscsi_get_error(iscsi
));
263 aio_timer_init(iTask
->iscsilun
->aio_context
,
264 &iTask
->retry_timer
, QEMU_CLOCK_REALTIME
,
265 SCALE_MS
, iscsi_retry_timer_expired
, iTask
);
266 timer_mod(&iTask
->retry_timer
,
267 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + retry_time
);
270 } else if (status
== SCSI_STATUS_CHECK_CONDITION
) {
271 int error
= iscsi_translate_sense(&task
->sense
);
272 if (error
== EAGAIN
) {
273 error_report("iSCSI CheckCondition: %s",
274 iscsi_get_error(iscsi
));
277 iTask
->err_code
= -error
;
278 iTask
->err_str
= g_strdup(iscsi_get_error(iscsi
));
284 replay_bh_schedule_oneshot_event(iTask
->iscsilun
->aio_context
,
285 iscsi_co_generic_bh_cb
, iTask
);
291 static void iscsi_co_init_iscsitask(IscsiLun
*iscsilun
, struct IscsiTask
*iTask
)
293 *iTask
= (struct IscsiTask
) {
294 .co
= qemu_coroutine_self(),
295 .iscsilun
= iscsilun
,
301 /* Called (via iscsi_service) with QemuMutex held. */
303 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
306 IscsiAIOCB
*acb
= private_data
;
308 /* If the command callback hasn't been called yet, drop the task */
310 /* Call iscsi_aio_ioctl_cb() with SCSI_STATUS_CANCELLED */
311 iscsi_scsi_cancel_task(iscsi
, acb
->task
);
314 qemu_aio_unref(acb
); /* acquired in iscsi_aio_cancel() */
318 iscsi_aio_cancel(BlockAIOCB
*blockacb
)
320 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
321 IscsiLun
*iscsilun
= acb
->iscsilun
;
323 qemu_mutex_lock(&iscsilun
->mutex
);
325 /* If it was cancelled or completed already, our work is done here */
326 if (acb
->cancelled
|| acb
->status
!= -EINPROGRESS
) {
327 qemu_mutex_unlock(&iscsilun
->mutex
);
331 acb
->cancelled
= true;
333 qemu_aio_ref(acb
); /* released in iscsi_abort_task_cb() */
335 /* send a task mgmt call to the target to cancel the task on the target */
336 if (iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
337 iscsi_abort_task_cb
, acb
) < 0) {
338 qemu_aio_unref(acb
); /* since iscsi_abort_task_cb() won't be called */
341 qemu_mutex_unlock(&iscsilun
->mutex
);
344 static const AIOCBInfo iscsi_aiocb_info
= {
345 .aiocb_size
= sizeof(IscsiAIOCB
),
346 .cancel_async
= iscsi_aio_cancel
,
351 static void iscsi_process_read(void *arg
);
352 static void iscsi_process_write(void *arg
);
354 /* Called with QemuMutex held. */
356 iscsi_set_events(IscsiLun
*iscsilun
)
358 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
359 int ev
= iscsi_which_events(iscsi
);
361 if (ev
!= iscsilun
->events
) {
362 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsi
),
364 (ev
& POLLIN
) ? iscsi_process_read
: NULL
,
365 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
368 iscsilun
->events
= ev
;
372 static void iscsi_timed_check_events(void *opaque
)
374 IscsiLun
*iscsilun
= opaque
;
376 qemu_mutex_lock(&iscsilun
->mutex
);
378 /* check for timed out requests */
379 iscsi_service(iscsilun
->iscsi
, 0);
381 if (iscsilun
->request_timed_out
) {
382 iscsilun
->request_timed_out
= false;
383 iscsi_reconnect(iscsilun
->iscsi
);
386 /* newer versions of libiscsi may return zero events. Ensure we are able
387 * to return to service once this situation changes. */
388 iscsi_set_events(iscsilun
);
390 qemu_mutex_unlock(&iscsilun
->mutex
);
392 timer_mod(iscsilun
->event_timer
,
393 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
397 iscsi_process_read(void *arg
)
399 IscsiLun
*iscsilun
= arg
;
400 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
402 qemu_mutex_lock(&iscsilun
->mutex
);
403 iscsi_service(iscsi
, POLLIN
);
404 iscsi_set_events(iscsilun
);
405 qemu_mutex_unlock(&iscsilun
->mutex
);
409 iscsi_process_write(void *arg
)
411 IscsiLun
*iscsilun
= arg
;
412 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
414 qemu_mutex_lock(&iscsilun
->mutex
);
415 iscsi_service(iscsi
, POLLOUT
);
416 iscsi_set_events(iscsilun
);
417 qemu_mutex_unlock(&iscsilun
->mutex
);
420 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
422 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
425 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
427 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
430 static bool is_byte_request_lun_aligned(int64_t offset
, int count
,
433 if (offset
% iscsilun
->block_size
|| count
% iscsilun
->block_size
) {
434 error_report("iSCSI misaligned request: "
435 "iscsilun->block_size %u, offset %" PRIi64
437 iscsilun
->block_size
, offset
, count
);
443 static bool is_sector_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
446 assert(nb_sectors
<= BDRV_REQUEST_MAX_SECTORS
);
447 return is_byte_request_lun_aligned(sector_num
<< BDRV_SECTOR_BITS
,
448 nb_sectors
<< BDRV_SECTOR_BITS
,
452 static void iscsi_allocmap_free(IscsiLun
*iscsilun
)
454 g_free(iscsilun
->allocmap
);
455 g_free(iscsilun
->allocmap_valid
);
456 iscsilun
->allocmap
= NULL
;
457 iscsilun
->allocmap_valid
= NULL
;
461 static int iscsi_allocmap_init(IscsiLun
*iscsilun
, int open_flags
)
463 iscsi_allocmap_free(iscsilun
);
465 assert(iscsilun
->cluster_size
);
466 iscsilun
->allocmap_size
=
467 DIV_ROUND_UP(iscsilun
->num_blocks
* iscsilun
->block_size
,
468 iscsilun
->cluster_size
);
470 iscsilun
->allocmap
= bitmap_try_new(iscsilun
->allocmap_size
);
471 if (!iscsilun
->allocmap
) {
475 if (open_flags
& BDRV_O_NOCACHE
) {
476 /* when cache.direct = on all allocmap entries are
477 * treated as invalid to force a relookup of the block
478 * status on every read request */
482 iscsilun
->allocmap_valid
= bitmap_try_new(iscsilun
->allocmap_size
);
483 if (!iscsilun
->allocmap_valid
) {
484 /* if we are under memory pressure free the allocmap as well */
485 iscsi_allocmap_free(iscsilun
);
493 iscsi_allocmap_update(IscsiLun
*iscsilun
, int64_t offset
,
494 int64_t bytes
, bool allocated
, bool valid
)
496 int64_t cl_num_expanded
, nb_cls_expanded
, cl_num_shrunk
, nb_cls_shrunk
;
498 if (iscsilun
->allocmap
== NULL
) {
501 /* expand to entirely contain all affected clusters */
502 assert(iscsilun
->cluster_size
);
503 cl_num_expanded
= offset
/ iscsilun
->cluster_size
;
504 nb_cls_expanded
= DIV_ROUND_UP(offset
+ bytes
,
505 iscsilun
->cluster_size
) - cl_num_expanded
;
506 /* shrink to touch only completely contained clusters */
507 cl_num_shrunk
= DIV_ROUND_UP(offset
, iscsilun
->cluster_size
);
508 nb_cls_shrunk
= (offset
+ bytes
) / iscsilun
->cluster_size
- cl_num_shrunk
;
510 bitmap_set(iscsilun
->allocmap
, cl_num_expanded
, nb_cls_expanded
);
512 if (nb_cls_shrunk
> 0) {
513 bitmap_clear(iscsilun
->allocmap
, cl_num_shrunk
, nb_cls_shrunk
);
517 if (iscsilun
->allocmap_valid
== NULL
) {
521 if (nb_cls_shrunk
> 0) {
522 bitmap_set(iscsilun
->allocmap_valid
, cl_num_shrunk
, nb_cls_shrunk
);
525 bitmap_clear(iscsilun
->allocmap_valid
, cl_num_expanded
,
531 iscsi_allocmap_set_allocated(IscsiLun
*iscsilun
, int64_t offset
,
534 iscsi_allocmap_update(iscsilun
, offset
, bytes
, true, true);
538 iscsi_allocmap_set_unallocated(IscsiLun
*iscsilun
, int64_t offset
,
541 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
542 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
544 iscsi_allocmap_update(iscsilun
, offset
, bytes
, false, true);
547 static void iscsi_allocmap_set_invalid(IscsiLun
*iscsilun
, int64_t offset
,
550 iscsi_allocmap_update(iscsilun
, offset
, bytes
, false, false);
553 static void iscsi_allocmap_invalidate(IscsiLun
*iscsilun
)
555 if (iscsilun
->allocmap
) {
556 bitmap_zero(iscsilun
->allocmap
, iscsilun
->allocmap_size
);
558 if (iscsilun
->allocmap_valid
) {
559 bitmap_zero(iscsilun
->allocmap_valid
, iscsilun
->allocmap_size
);
564 iscsi_allocmap_is_allocated(IscsiLun
*iscsilun
, int64_t offset
,
568 if (iscsilun
->allocmap
== NULL
) {
571 assert(iscsilun
->cluster_size
);
572 size
= DIV_ROUND_UP(offset
+ bytes
, iscsilun
->cluster_size
);
573 return !(find_next_bit(iscsilun
->allocmap
, size
,
574 offset
/ iscsilun
->cluster_size
) == size
);
577 static inline bool iscsi_allocmap_is_valid(IscsiLun
*iscsilun
,
578 int64_t offset
, int64_t bytes
)
581 if (iscsilun
->allocmap_valid
== NULL
) {
584 assert(iscsilun
->cluster_size
);
585 size
= DIV_ROUND_UP(offset
+ bytes
, iscsilun
->cluster_size
);
586 return (find_next_zero_bit(iscsilun
->allocmap_valid
, size
,
587 offset
/ iscsilun
->cluster_size
) == size
);
590 static void coroutine_fn
iscsi_co_wait_for_task(IscsiTask
*iTask
,
593 while (!iTask
->complete
) {
594 iscsi_set_events(iscsilun
);
595 qemu_mutex_unlock(&iscsilun
->mutex
);
596 qemu_coroutine_yield();
597 qemu_mutex_lock(&iscsilun
->mutex
);
601 static int coroutine_fn
602 iscsi_co_writev(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
603 QEMUIOVector
*iov
, int flags
)
605 IscsiLun
*iscsilun
= bs
->opaque
;
606 struct IscsiTask iTask
;
608 uint32_t num_sectors
;
609 bool fua
= flags
& BDRV_REQ_FUA
;
613 assert(iscsilun
->dpofua
);
615 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
619 if (bs
->bl
.max_transfer
) {
620 assert(nb_sectors
<< BDRV_SECTOR_BITS
<= bs
->bl
.max_transfer
);
623 lba
= sector_qemu2lun(sector_num
, iscsilun
);
624 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
625 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
626 qemu_mutex_lock(&iscsilun
->mutex
);
628 if (iscsilun
->use_16_for_rw
) {
629 #if LIBISCSI_API_VERSION >= (20160603)
630 iTask
.task
= iscsi_write16_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
631 NULL
, num_sectors
* iscsilun
->block_size
,
632 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
633 iscsi_co_generic_cb
, &iTask
,
634 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
636 iTask
.task
= iscsi_write10_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
637 NULL
, num_sectors
* iscsilun
->block_size
,
638 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
639 iscsi_co_generic_cb
, &iTask
,
640 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
643 iTask
.task
= iscsi_write16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
644 NULL
, num_sectors
* iscsilun
->block_size
,
645 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
646 iscsi_co_generic_cb
, &iTask
);
648 iTask
.task
= iscsi_write10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
649 NULL
, num_sectors
* iscsilun
->block_size
,
650 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
651 iscsi_co_generic_cb
, &iTask
);
654 if (iTask
.task
== NULL
) {
655 qemu_mutex_unlock(&iscsilun
->mutex
);
658 #if LIBISCSI_API_VERSION < (20160603)
659 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
662 iscsi_co_wait_for_task(&iTask
, iscsilun
);
664 if (iTask
.task
!= NULL
) {
665 scsi_free_scsi_task(iTask
.task
);
669 if (iTask
.do_retry
) {
674 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
675 iscsi_allocmap_set_invalid(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
676 nb_sectors
* BDRV_SECTOR_SIZE
);
677 error_report("iSCSI WRITE10/16 failed at lba %" PRIu64
": %s", lba
,
683 iscsi_allocmap_set_allocated(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
684 nb_sectors
* BDRV_SECTOR_SIZE
);
687 qemu_mutex_unlock(&iscsilun
->mutex
);
688 g_free(iTask
.err_str
);
694 static int coroutine_fn
iscsi_co_block_status(BlockDriverState
*bs
,
695 bool want_zero
, int64_t offset
,
696 int64_t bytes
, int64_t *pnum
,
698 BlockDriverState
**file
)
700 IscsiLun
*iscsilun
= bs
->opaque
;
701 struct scsi_get_lba_status
*lbas
= NULL
;
702 struct scsi_lba_status_descriptor
*lbasd
= NULL
;
703 struct IscsiTask iTask
;
704 uint64_t lba
, max_bytes
;
707 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
709 assert(QEMU_IS_ALIGNED(offset
| bytes
, iscsilun
->block_size
));
711 /* default to all sectors allocated */
712 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
718 /* LUN does not support logical block provisioning */
719 if (!iscsilun
->lbpme
) {
723 lba
= offset
/ iscsilun
->block_size
;
724 max_bytes
= (iscsilun
->num_blocks
- lba
) * iscsilun
->block_size
;
726 qemu_mutex_lock(&iscsilun
->mutex
);
728 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
729 lba
, 8 + 16, iscsi_co_generic_cb
,
734 iscsi_co_wait_for_task(&iTask
, iscsilun
);
736 if (iTask
.do_retry
) {
737 if (iTask
.task
!= NULL
) {
738 scsi_free_scsi_task(iTask
.task
);
745 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
746 /* in case the get_lba_status_callout fails (i.e.
747 * because the device is busy or the cmd is not
748 * supported) we pretend all blocks are allocated
749 * for backwards compatibility */
750 error_report("iSCSI GET_LBA_STATUS failed at lba %" PRIu64
": %s",
755 lbas
= scsi_datain_unmarshall(iTask
.task
);
756 if (lbas
== NULL
|| lbas
->num_descriptors
== 0) {
761 lbasd
= &lbas
->descriptors
[0];
763 if (lba
!= lbasd
->lba
) {
768 *pnum
= MIN((int64_t) lbasd
->num_blocks
* iscsilun
->block_size
, max_bytes
);
770 if (lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_DEALLOCATED
||
771 lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_ANCHORED
) {
772 ret
&= ~BDRV_BLOCK_DATA
;
773 if (iscsilun
->lbprz
) {
774 ret
|= BDRV_BLOCK_ZERO
;
778 if (ret
& BDRV_BLOCK_ZERO
) {
779 iscsi_allocmap_set_unallocated(iscsilun
, offset
, *pnum
);
781 iscsi_allocmap_set_allocated(iscsilun
, offset
, *pnum
);
788 qemu_mutex_unlock(&iscsilun
->mutex
);
789 g_free(iTask
.err_str
);
791 if (iTask
.task
!= NULL
) {
792 scsi_free_scsi_task(iTask
.task
);
794 if (ret
> 0 && ret
& BDRV_BLOCK_OFFSET_VALID
&& file
) {
800 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
801 int64_t sector_num
, int nb_sectors
,
804 IscsiLun
*iscsilun
= bs
->opaque
;
805 struct IscsiTask iTask
;
807 uint32_t num_sectors
;
810 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
814 if (bs
->bl
.max_transfer
) {
815 assert(nb_sectors
<< BDRV_SECTOR_BITS
<= bs
->bl
.max_transfer
);
818 /* if cache.direct is off and we have a valid entry in our allocation map
819 * we can skip checking the block status and directly return zeroes if
820 * the request falls within an unallocated area */
821 if (iscsi_allocmap_is_valid(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
822 nb_sectors
* BDRV_SECTOR_SIZE
) &&
823 !iscsi_allocmap_is_allocated(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
824 nb_sectors
* BDRV_SECTOR_SIZE
)) {
825 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
829 if (nb_sectors
>= ISCSI_CHECKALLOC_THRES
&&
830 !iscsi_allocmap_is_valid(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
831 nb_sectors
* BDRV_SECTOR_SIZE
) &&
832 !iscsi_allocmap_is_allocated(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
833 nb_sectors
* BDRV_SECTOR_SIZE
)) {
835 /* check the block status from the beginning of the cluster
836 * containing the start sector */
840 assert(iscsilun
->cluster_size
);
841 head
= (sector_num
* BDRV_SECTOR_SIZE
) % iscsilun
->cluster_size
;
842 ret
= iscsi_co_block_status(bs
, true,
843 sector_num
* BDRV_SECTOR_SIZE
- head
,
844 BDRV_REQUEST_MAX_BYTES
, &pnum
, NULL
, NULL
);
848 /* if the whole request falls into an unallocated area we can avoid
849 * reading and directly return zeroes instead */
850 if (ret
& BDRV_BLOCK_ZERO
&&
851 pnum
>= nb_sectors
* BDRV_SECTOR_SIZE
+ head
) {
852 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
857 lba
= sector_qemu2lun(sector_num
, iscsilun
);
858 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
860 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
861 qemu_mutex_lock(&iscsilun
->mutex
);
863 if (iscsilun
->use_16_for_rw
) {
864 #if LIBISCSI_API_VERSION >= (20160603)
865 iTask
.task
= iscsi_read16_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
866 num_sectors
* iscsilun
->block_size
,
867 iscsilun
->block_size
, 0, 0, 0, 0, 0,
868 iscsi_co_generic_cb
, &iTask
,
869 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
871 iTask
.task
= iscsi_read10_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
872 num_sectors
* iscsilun
->block_size
,
873 iscsilun
->block_size
,
875 iscsi_co_generic_cb
, &iTask
,
876 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
879 iTask
.task
= iscsi_read16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
880 num_sectors
* iscsilun
->block_size
,
881 iscsilun
->block_size
, 0, 0, 0, 0, 0,
882 iscsi_co_generic_cb
, &iTask
);
884 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
885 num_sectors
* iscsilun
->block_size
,
886 iscsilun
->block_size
,
888 iscsi_co_generic_cb
, &iTask
);
891 if (iTask
.task
== NULL
) {
892 qemu_mutex_unlock(&iscsilun
->mutex
);
895 #if LIBISCSI_API_VERSION < (20160603)
896 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
899 iscsi_co_wait_for_task(&iTask
, iscsilun
);
900 if (iTask
.task
!= NULL
) {
901 scsi_free_scsi_task(iTask
.task
);
905 if (iTask
.do_retry
) {
910 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
911 error_report("iSCSI READ10/16 failed at lba %" PRIu64
": %s",
916 qemu_mutex_unlock(&iscsilun
->mutex
);
917 g_free(iTask
.err_str
);
921 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
923 IscsiLun
*iscsilun
= bs
->opaque
;
924 struct IscsiTask iTask
;
927 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
928 qemu_mutex_lock(&iscsilun
->mutex
);
930 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
931 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
932 qemu_mutex_unlock(&iscsilun
->mutex
);
936 iscsi_co_wait_for_task(&iTask
, iscsilun
);
938 if (iTask
.task
!= NULL
) {
939 scsi_free_scsi_task(iTask
.task
);
943 if (iTask
.do_retry
) {
948 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
949 error_report("iSCSI SYNCHRONIZECACHE10 failed: %s", iTask
.err_str
);
953 qemu_mutex_unlock(&iscsilun
->mutex
);
954 g_free(iTask
.err_str
);
959 /* Called (via iscsi_service) with QemuMutex held. */
961 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
962 void *command_data
, void *opaque
)
964 IscsiAIOCB
*acb
= opaque
;
966 if (status
== SCSI_STATUS_CANCELLED
) {
968 acb
->status
= -ECANCELED
;
969 iscsi_schedule_bh(acb
);
976 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
977 iscsi_get_error(iscsi
));
978 acb
->status
= -iscsi_translate_sense(&acb
->task
->sense
);
981 acb
->ioh
->driver_status
= 0;
982 acb
->ioh
->host_status
= 0;
984 acb
->ioh
->status
= status
;
986 #define SG_ERR_DRIVER_SENSE 0x08
988 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
991 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
993 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
994 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
995 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
996 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
999 iscsi_schedule_bh(acb
);
1002 static void iscsi_ioctl_bh_completion(void *opaque
)
1004 IscsiAIOCB
*acb
= opaque
;
1006 qemu_bh_delete(acb
->bh
);
1007 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
1008 qemu_aio_unref(acb
);
1011 static void iscsi_ioctl_handle_emulated(IscsiAIOCB
*acb
, int req
, void *buf
)
1013 BlockDriverState
*bs
= acb
->common
.bs
;
1014 IscsiLun
*iscsilun
= bs
->opaque
;
1018 case SG_GET_VERSION_NUM
:
1019 *(int *)buf
= 30000;
1021 case SG_GET_SCSI_ID
:
1022 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
1028 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
),
1029 iscsi_ioctl_bh_completion
, acb
);
1031 qemu_bh_schedule(acb
->bh
);
1034 static BlockAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
1035 unsigned long int req
, void *buf
,
1036 BlockCompletionFunc
*cb
, void *opaque
)
1038 IscsiLun
*iscsilun
= bs
->opaque
;
1039 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1040 struct iscsi_data data
;
1043 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
1045 acb
->iscsilun
= iscsilun
;
1047 acb
->status
= -EINPROGRESS
;
1049 acb
->cancelled
= false;
1052 iscsi_ioctl_handle_emulated(acb
, req
, buf
);
1053 return &acb
->common
;
1056 if (acb
->ioh
->cmd_len
> SCSI_CDB_MAX_SIZE
) {
1057 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
1058 acb
->ioh
->cmd_len
, SCSI_CDB_MAX_SIZE
);
1059 qemu_aio_unref(acb
);
1063 acb
->task
= malloc(sizeof(struct scsi_task
));
1064 if (acb
->task
== NULL
) {
1065 error_report("iSCSI: Failed to allocate task for scsi command. %s",
1066 iscsi_get_error(iscsi
));
1067 qemu_aio_unref(acb
);
1070 memset(acb
->task
, 0, sizeof(struct scsi_task
));
1072 switch (acb
->ioh
->dxfer_direction
) {
1073 case SG_DXFER_TO_DEV
:
1074 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
1076 case SG_DXFER_FROM_DEV
:
1077 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
1080 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
1084 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
1085 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
1086 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
1089 qemu_mutex_lock(&iscsilun
->mutex
);
1090 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
1091 if (acb
->ioh
->iovec_count
== 0) {
1092 data
.data
= acb
->ioh
->dxferp
;
1093 data
.size
= acb
->ioh
->dxfer_len
;
1095 scsi_task_set_iov_out(acb
->task
,
1096 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1097 acb
->ioh
->iovec_count
);
1101 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
1103 (data
.size
> 0) ? &data
: NULL
,
1105 qemu_mutex_unlock(&iscsilun
->mutex
);
1106 scsi_free_scsi_task(acb
->task
);
1107 qemu_aio_unref(acb
);
1111 /* tell libiscsi to read straight into the buffer we got from ioctl */
1112 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
1113 if (acb
->ioh
->iovec_count
== 0) {
1114 scsi_task_add_data_in_buffer(acb
->task
,
1115 acb
->ioh
->dxfer_len
,
1118 scsi_task_set_iov_in(acb
->task
,
1119 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1120 acb
->ioh
->iovec_count
);
1124 iscsi_set_events(iscsilun
);
1125 qemu_mutex_unlock(&iscsilun
->mutex
);
1127 return &acb
->common
;
1133 iscsi_getlength(BlockDriverState
*bs
)
1135 IscsiLun
*iscsilun
= bs
->opaque
;
1138 len
= iscsilun
->num_blocks
;
1139 len
*= iscsilun
->block_size
;
1145 coroutine_fn
iscsi_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int bytes
)
1147 IscsiLun
*iscsilun
= bs
->opaque
;
1148 struct IscsiTask iTask
;
1149 struct unmap_list list
;
1152 if (!is_byte_request_lun_aligned(offset
, bytes
, iscsilun
)) {
1156 if (!iscsilun
->lbp
.lbpu
) {
1157 /* UNMAP is not supported by the target */
1161 list
.lba
= offset
/ iscsilun
->block_size
;
1162 list
.num
= bytes
/ iscsilun
->block_size
;
1164 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1165 qemu_mutex_lock(&iscsilun
->mutex
);
1167 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
1168 iscsi_co_generic_cb
, &iTask
) == NULL
) {
1173 iscsi_co_wait_for_task(&iTask
, iscsilun
);
1175 if (iTask
.task
!= NULL
) {
1176 scsi_free_scsi_task(iTask
.task
);
1180 if (iTask
.do_retry
) {
1185 iscsi_allocmap_set_invalid(iscsilun
, offset
, bytes
);
1187 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
) {
1188 /* the target might fail with a check condition if it
1189 is not happy with the alignment of the UNMAP request
1190 we silently fail in this case */
1194 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1195 error_report("iSCSI UNMAP failed at lba %" PRIu64
": %s",
1196 list
.lba
, iTask
.err_str
);
1202 qemu_mutex_unlock(&iscsilun
->mutex
);
1203 g_free(iTask
.err_str
);
1208 coroutine_fn
iscsi_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1209 int bytes
, BdrvRequestFlags flags
)
1211 IscsiLun
*iscsilun
= bs
->opaque
;
1212 struct IscsiTask iTask
;
1215 bool use_16_for_ws
= iscsilun
->use_16_for_rw
;
1218 if (!is_byte_request_lun_aligned(offset
, bytes
, iscsilun
)) {
1222 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1223 if (!use_16_for_ws
&& !iscsilun
->lbp
.lbpws10
) {
1224 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1225 use_16_for_ws
= true;
1227 if (use_16_for_ws
&& !iscsilun
->lbp
.lbpws
) {
1228 /* WRITESAME16 with UNMAP is not supported by the target,
1229 * fall back and try WRITESAME10/16 without UNMAP */
1230 flags
&= ~BDRV_REQ_MAY_UNMAP
;
1231 use_16_for_ws
= iscsilun
->use_16_for_rw
;
1235 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
1236 /* WRITESAME without UNMAP is not supported by the target */
1240 lba
= offset
/ iscsilun
->block_size
;
1241 nb_blocks
= bytes
/ iscsilun
->block_size
;
1243 if (iscsilun
->zeroblock
== NULL
) {
1244 iscsilun
->zeroblock
= g_try_malloc0(iscsilun
->block_size
);
1245 if (iscsilun
->zeroblock
== NULL
) {
1250 qemu_mutex_lock(&iscsilun
->mutex
);
1251 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1253 if (use_16_for_ws
) {
1254 iTask
.task
= iscsi_writesame16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1255 iscsilun
->zeroblock
, iscsilun
->block_size
,
1256 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1257 0, 0, iscsi_co_generic_cb
, &iTask
);
1259 iTask
.task
= iscsi_writesame10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1260 iscsilun
->zeroblock
, iscsilun
->block_size
,
1261 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1262 0, 0, iscsi_co_generic_cb
, &iTask
);
1264 if (iTask
.task
== NULL
) {
1265 qemu_mutex_unlock(&iscsilun
->mutex
);
1269 iscsi_co_wait_for_task(&iTask
, iscsilun
);
1271 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
&&
1272 iTask
.task
->sense
.key
== SCSI_SENSE_ILLEGAL_REQUEST
&&
1273 (iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
||
1274 iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
)) {
1275 /* WRITE SAME is not supported by the target */
1276 iscsilun
->has_write_same
= false;
1277 scsi_free_scsi_task(iTask
.task
);
1282 if (iTask
.task
!= NULL
) {
1283 scsi_free_scsi_task(iTask
.task
);
1287 if (iTask
.do_retry
) {
1292 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1293 iscsi_allocmap_set_invalid(iscsilun
, offset
, bytes
);
1294 error_report("iSCSI WRITESAME10/16 failed at lba %" PRIu64
": %s",
1295 lba
, iTask
.err_str
);
1300 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1301 iscsi_allocmap_set_invalid(iscsilun
, offset
, bytes
);
1303 iscsi_allocmap_set_allocated(iscsilun
, offset
, bytes
);
1307 qemu_mutex_unlock(&iscsilun
->mutex
);
1308 g_free(iTask
.err_str
);
1312 static void apply_chap(struct iscsi_context
*iscsi
, QemuOpts
*opts
,
1315 const char *user
= NULL
;
1316 const char *password
= NULL
;
1317 const char *secretid
;
1318 char *secret
= NULL
;
1320 user
= qemu_opt_get(opts
, "user");
1325 secretid
= qemu_opt_get(opts
, "password-secret");
1326 password
= qemu_opt_get(opts
, "password");
1327 if (secretid
&& password
) {
1328 error_setg(errp
, "'password' and 'password-secret' properties are "
1329 "mutually exclusive");
1333 secret
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
1338 } else if (!password
) {
1339 error_setg(errp
, "CHAP username specified but no password was given");
1343 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
1344 error_setg(errp
, "Failed to set initiator username and password");
1350 static void apply_header_digest(struct iscsi_context
*iscsi
, QemuOpts
*opts
,
1353 const char *digest
= NULL
;
1355 digest
= qemu_opt_get(opts
, "header-digest");
1357 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1358 } else if (!strcmp(digest
, "crc32c")) {
1359 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
1360 } else if (!strcmp(digest
, "none")) {
1361 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
1362 } else if (!strcmp(digest
, "crc32c-none")) {
1363 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
1364 } else if (!strcmp(digest
, "none-crc32c")) {
1365 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1367 error_setg(errp
, "Invalid header-digest setting : %s", digest
);
1371 static char *get_initiator_name(QemuOpts
*opts
)
1375 UuidInfo
*uuid_info
;
1377 name
= qemu_opt_get(opts
, "initiator-name");
1379 return g_strdup(name
);
1382 uuid_info
= qmp_query_uuid(NULL
);
1383 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
1384 name
= qemu_get_vm_name();
1386 name
= uuid_info
->UUID
;
1388 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1389 name
? ":" : "", name
? name
: "");
1390 qapi_free_UuidInfo(uuid_info
);
1394 static void iscsi_nop_timed_event(void *opaque
)
1396 IscsiLun
*iscsilun
= opaque
;
1398 qemu_mutex_lock(&iscsilun
->mutex
);
1399 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) >= MAX_NOP_FAILURES
) {
1400 error_report("iSCSI: NOP timeout. Reconnecting...");
1401 iscsilun
->request_timed_out
= true;
1402 } else if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
1403 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1407 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1408 iscsi_set_events(iscsilun
);
1411 qemu_mutex_unlock(&iscsilun
->mutex
);
1414 static void iscsi_readcapacity_sync(IscsiLun
*iscsilun
, Error
**errp
)
1416 struct scsi_task
*task
= NULL
;
1417 struct scsi_readcapacity10
*rc10
= NULL
;
1418 struct scsi_readcapacity16
*rc16
= NULL
;
1419 int retries
= ISCSI_CMD_RETRIES
;
1423 scsi_free_scsi_task(task
);
1427 switch (iscsilun
->type
) {
1429 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
1430 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1431 rc16
= scsi_datain_unmarshall(task
);
1433 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity16 data.");
1435 iscsilun
->block_size
= rc16
->block_length
;
1436 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1437 iscsilun
->lbpme
= !!rc16
->lbpme
;
1438 iscsilun
->lbprz
= !!rc16
->lbprz
;
1439 iscsilun
->use_16_for_rw
= (rc16
->returned_lba
> 0xffffffff);
1443 if (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1444 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
1447 /* Fall through and try READ CAPACITY(10) instead. */
1449 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1450 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1451 rc10
= scsi_datain_unmarshall(task
);
1453 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity10 data.");
1455 iscsilun
->block_size
= rc10
->block_size
;
1456 if (rc10
->lba
== 0) {
1457 /* blank disk loaded */
1458 iscsilun
->num_blocks
= 0;
1460 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1468 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1469 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1472 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1473 error_setg(errp
, "iSCSI: failed to send readcapacity10/16 command");
1474 } else if (!iscsilun
->block_size
||
1475 iscsilun
->block_size
% BDRV_SECTOR_SIZE
) {
1476 error_setg(errp
, "iSCSI: the target returned an invalid "
1477 "block size of %d.", iscsilun
->block_size
);
1480 scsi_free_scsi_task(task
);
1484 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
, int lun
,
1485 int evpd
, int pc
, void **inq
, Error
**errp
)
1488 struct scsi_task
*task
= NULL
;
1489 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, 64);
1490 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1493 full_size
= scsi_datain_getfullsize(task
);
1494 if (full_size
> task
->datain
.size
) {
1495 scsi_free_scsi_task(task
);
1497 /* we need more data for the full list */
1498 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, full_size
);
1499 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1504 *inq
= scsi_datain_unmarshall(task
);
1506 error_setg(errp
, "iSCSI: failed to unmarshall inquiry datain blob");
1513 error_setg(errp
, "iSCSI: Inquiry command failed : %s",
1514 iscsi_get_error(iscsi
));
1517 scsi_free_scsi_task(task
);
1522 static void iscsi_detach_aio_context(BlockDriverState
*bs
)
1524 IscsiLun
*iscsilun
= bs
->opaque
;
1526 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsilun
->iscsi
),
1527 false, NULL
, NULL
, NULL
, NULL
);
1528 iscsilun
->events
= 0;
1530 if (iscsilun
->nop_timer
) {
1531 timer_del(iscsilun
->nop_timer
);
1532 timer_free(iscsilun
->nop_timer
);
1533 iscsilun
->nop_timer
= NULL
;
1535 if (iscsilun
->event_timer
) {
1536 timer_del(iscsilun
->event_timer
);
1537 timer_free(iscsilun
->event_timer
);
1538 iscsilun
->event_timer
= NULL
;
1542 static void iscsi_attach_aio_context(BlockDriverState
*bs
,
1543 AioContext
*new_context
)
1545 IscsiLun
*iscsilun
= bs
->opaque
;
1547 iscsilun
->aio_context
= new_context
;
1548 iscsi_set_events(iscsilun
);
1550 /* Set up a timer for sending out iSCSI NOPs */
1551 iscsilun
->nop_timer
= aio_timer_new(iscsilun
->aio_context
,
1552 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1553 iscsi_nop_timed_event
, iscsilun
);
1554 timer_mod(iscsilun
->nop_timer
,
1555 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1557 /* Set up a timer for periodic calls to iscsi_set_events and to
1558 * scan for command timeout */
1559 iscsilun
->event_timer
= aio_timer_new(iscsilun
->aio_context
,
1560 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1561 iscsi_timed_check_events
, iscsilun
);
1562 timer_mod(iscsilun
->event_timer
,
1563 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
1566 static void iscsi_modesense_sync(IscsiLun
*iscsilun
)
1568 struct scsi_task
*task
;
1569 struct scsi_mode_sense
*ms
= NULL
;
1570 iscsilun
->write_protected
= false;
1571 iscsilun
->dpofua
= false;
1573 task
= iscsi_modesense6_sync(iscsilun
->iscsi
, iscsilun
->lun
,
1574 1, SCSI_MODESENSE_PC_CURRENT
,
1577 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1578 iscsi_get_error(iscsilun
->iscsi
));
1582 if (task
->status
!= SCSI_STATUS_GOOD
) {
1583 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1586 ms
= scsi_datain_unmarshall(task
);
1588 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1589 iscsi_get_error(iscsilun
->iscsi
));
1592 iscsilun
->write_protected
= ms
->device_specific_parameter
& 0x80;
1593 iscsilun
->dpofua
= ms
->device_specific_parameter
& 0x10;
1597 scsi_free_scsi_task(task
);
1601 static void iscsi_parse_iscsi_option(const char *target
, QDict
*options
)
1605 const char *user
, *password
, *password_secret
, *initiator_name
,
1606 *header_digest
, *timeout
;
1608 list
= qemu_find_opts("iscsi");
1613 opts
= qemu_opts_find(list
, target
);
1615 opts
= QTAILQ_FIRST(&list
->head
);
1621 user
= qemu_opt_get(opts
, "user");
1623 qdict_set_default_str(options
, "user", user
);
1626 password
= qemu_opt_get(opts
, "password");
1628 qdict_set_default_str(options
, "password", password
);
1631 password_secret
= qemu_opt_get(opts
, "password-secret");
1632 if (password_secret
) {
1633 qdict_set_default_str(options
, "password-secret", password_secret
);
1636 initiator_name
= qemu_opt_get(opts
, "initiator-name");
1637 if (initiator_name
) {
1638 qdict_set_default_str(options
, "initiator-name", initiator_name
);
1641 header_digest
= qemu_opt_get(opts
, "header-digest");
1642 if (header_digest
) {
1643 /* -iscsi takes upper case values, but QAPI only supports lower case
1644 * enum constant names, so we have to convert here. */
1645 char *qapi_value
= g_ascii_strdown(header_digest
, -1);
1646 qdict_set_default_str(options
, "header-digest", qapi_value
);
1650 timeout
= qemu_opt_get(opts
, "timeout");
1652 qdict_set_default_str(options
, "timeout", timeout
);
1657 * We support iscsi url's on the form
1658 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1660 static void iscsi_parse_filename(const char *filename
, QDict
*options
,
1663 struct iscsi_url
*iscsi_url
;
1664 const char *transport_name
;
1667 iscsi_url
= iscsi_parse_full_url(NULL
, filename
);
1668 if (iscsi_url
== NULL
) {
1669 error_setg(errp
, "Failed to parse URL : %s", filename
);
1673 #if LIBISCSI_API_VERSION >= (20160603)
1674 switch (iscsi_url
->transport
) {
1676 transport_name
= "tcp";
1678 case ISER_TRANSPORT
:
1679 transport_name
= "iser";
1682 error_setg(errp
, "Unknown transport type (%d)",
1683 iscsi_url
->transport
);
1687 transport_name
= "tcp";
1690 qdict_set_default_str(options
, "transport", transport_name
);
1691 qdict_set_default_str(options
, "portal", iscsi_url
->portal
);
1692 qdict_set_default_str(options
, "target", iscsi_url
->target
);
1694 lun_str
= g_strdup_printf("%d", iscsi_url
->lun
);
1695 qdict_set_default_str(options
, "lun", lun_str
);
1698 /* User/password from -iscsi take precedence over those from the URL */
1699 iscsi_parse_iscsi_option(iscsi_url
->target
, options
);
1701 if (iscsi_url
->user
[0] != '\0') {
1702 qdict_set_default_str(options
, "user", iscsi_url
->user
);
1703 qdict_set_default_str(options
, "password", iscsi_url
->passwd
);
1706 iscsi_destroy_url(iscsi_url
);
1709 static QemuOptsList runtime_opts
= {
1711 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1714 .name
= "transport",
1715 .type
= QEMU_OPT_STRING
,
1719 .type
= QEMU_OPT_STRING
,
1723 .type
= QEMU_OPT_STRING
,
1727 .type
= QEMU_OPT_STRING
,
1731 .type
= QEMU_OPT_STRING
,
1734 .name
= "password-secret",
1735 .type
= QEMU_OPT_STRING
,
1739 .type
= QEMU_OPT_NUMBER
,
1742 .name
= "initiator-name",
1743 .type
= QEMU_OPT_STRING
,
1746 .name
= "header-digest",
1747 .type
= QEMU_OPT_STRING
,
1751 .type
= QEMU_OPT_NUMBER
,
1753 { /* end of list */ }
1757 static void iscsi_save_designator(IscsiLun
*lun
,
1758 struct scsi_inquiry_device_identification
*inq_di
)
1760 struct scsi_inquiry_device_designator
*desig
, *copy
= NULL
;
1762 for (desig
= inq_di
->designators
; desig
; desig
= desig
->next
) {
1763 if (desig
->association
||
1764 desig
->designator_type
> SCSI_DESIGNATOR_TYPE_NAA
) {
1767 /* NAA works better than T10 vendor ID based designator. */
1768 if (!copy
|| copy
->designator_type
< desig
->designator_type
) {
1773 lun
->dd
= g_new(struct scsi_inquiry_device_designator
, 1);
1775 lun
->dd
->next
= NULL
;
1776 lun
->dd
->designator
= g_malloc(copy
->designator_length
);
1777 memcpy(lun
->dd
->designator
, copy
->designator
, copy
->designator_length
);
1781 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1784 IscsiLun
*iscsilun
= bs
->opaque
;
1785 struct iscsi_context
*iscsi
= NULL
;
1786 struct scsi_task
*task
= NULL
;
1787 struct scsi_inquiry_standard
*inq
= NULL
;
1788 struct scsi_inquiry_supported_pages
*inq_vpd
;
1789 char *initiator_name
= NULL
;
1791 Error
*local_err
= NULL
;
1792 const char *transport_name
, *portal
, *target
;
1793 #if LIBISCSI_API_VERSION >= (20160603)
1794 enum iscsi_transport_type transport
;
1796 int i
, ret
= 0, timeout
= 0, lun
;
1798 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1799 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1801 error_propagate(errp
, local_err
);
1806 transport_name
= qemu_opt_get(opts
, "transport");
1807 portal
= qemu_opt_get(opts
, "portal");
1808 target
= qemu_opt_get(opts
, "target");
1809 lun
= qemu_opt_get_number(opts
, "lun", 0);
1811 if (!transport_name
|| !portal
|| !target
) {
1812 error_setg(errp
, "Need all of transport, portal and target options");
1817 if (!strcmp(transport_name
, "tcp")) {
1818 #if LIBISCSI_API_VERSION >= (20160603)
1819 transport
= TCP_TRANSPORT
;
1820 } else if (!strcmp(transport_name
, "iser")) {
1821 transport
= ISER_TRANSPORT
;
1823 /* TCP is what older libiscsi versions always use */
1826 error_setg(errp
, "Unknown transport: %s", transport_name
);
1831 memset(iscsilun
, 0, sizeof(IscsiLun
));
1833 initiator_name
= get_initiator_name(opts
);
1835 iscsi
= iscsi_create_context(initiator_name
);
1836 if (iscsi
== NULL
) {
1837 error_setg(errp
, "iSCSI: Failed to create iSCSI context.");
1841 #if LIBISCSI_API_VERSION >= (20160603)
1842 if (iscsi_init_transport(iscsi
, transport
)) {
1843 error_setg(errp
, ("Error initializing transport."));
1848 if (iscsi_set_targetname(iscsi
, target
)) {
1849 error_setg(errp
, "iSCSI: Failed to set target name.");
1854 /* check if we got CHAP username/password via the options */
1855 apply_chap(iscsi
, opts
, &local_err
);
1856 if (local_err
!= NULL
) {
1857 error_propagate(errp
, local_err
);
1862 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1863 error_setg(errp
, "iSCSI: Failed to set session type to normal.");
1868 /* check if we got HEADER_DIGEST via the options */
1869 apply_header_digest(iscsi
, opts
, &local_err
);
1870 if (local_err
!= NULL
) {
1871 error_propagate(errp
, local_err
);
1876 /* timeout handling is broken in libiscsi before 1.15.0 */
1877 timeout
= qemu_opt_get_number(opts
, "timeout", 0);
1878 #if LIBISCSI_API_VERSION >= 20150621
1879 iscsi_set_timeout(iscsi
, timeout
);
1882 warn_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1886 if (iscsi_full_connect_sync(iscsi
, portal
, lun
) != 0) {
1887 error_setg(errp
, "iSCSI: Failed to connect to LUN : %s",
1888 iscsi_get_error(iscsi
));
1893 iscsilun
->iscsi
= iscsi
;
1894 iscsilun
->aio_context
= bdrv_get_aio_context(bs
);
1895 iscsilun
->lun
= lun
;
1896 iscsilun
->has_write_same
= true;
1898 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0,
1899 (void **) &inq
, errp
);
1904 iscsilun
->type
= inq
->periperal_device_type
;
1905 scsi_free_scsi_task(task
);
1908 iscsi_modesense_sync(iscsilun
);
1909 if (iscsilun
->dpofua
) {
1910 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1913 /* Check the write protect flag of the LUN if we want to write */
1914 if (iscsilun
->type
== TYPE_DISK
&& (flags
& BDRV_O_RDWR
) &&
1915 iscsilun
->write_protected
) {
1916 ret
= bdrv_apply_auto_read_only(bs
, "LUN is write protected", errp
);
1920 flags
&= ~BDRV_O_RDWR
;
1923 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1924 if (local_err
!= NULL
) {
1925 error_propagate(errp
, local_err
);
1929 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1931 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1932 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1933 * will try to read from the device to guess the image format.
1935 if (iscsilun
->type
!= TYPE_DISK
&& iscsilun
->type
!= TYPE_ROM
) {
1939 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1940 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES
,
1941 (void **) &inq_vpd
, errp
);
1946 for (i
= 0; i
< inq_vpd
->num_pages
; i
++) {
1947 struct scsi_task
*inq_task
;
1948 struct scsi_inquiry_logical_block_provisioning
*inq_lbp
;
1949 struct scsi_inquiry_block_limits
*inq_bl
;
1950 struct scsi_inquiry_device_identification
*inq_di
;
1951 switch (inq_vpd
->pages
[i
]) {
1952 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
:
1953 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1954 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
,
1955 (void **) &inq_lbp
, errp
);
1956 if (inq_task
== NULL
) {
1960 memcpy(&iscsilun
->lbp
, inq_lbp
,
1961 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1962 scsi_free_scsi_task(inq_task
);
1964 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
:
1965 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1966 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
,
1967 (void **) &inq_bl
, errp
);
1968 if (inq_task
== NULL
) {
1972 memcpy(&iscsilun
->bl
, inq_bl
,
1973 sizeof(struct scsi_inquiry_block_limits
));
1974 scsi_free_scsi_task(inq_task
);
1976 case SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION
:
1977 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1978 SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION
,
1979 (void **) &inq_di
, errp
);
1980 if (inq_task
== NULL
) {
1984 iscsi_save_designator(iscsilun
, inq_di
);
1985 scsi_free_scsi_task(inq_task
);
1991 scsi_free_scsi_task(task
);
1994 qemu_mutex_init(&iscsilun
->mutex
);
1995 iscsi_attach_aio_context(bs
, iscsilun
->aio_context
);
1997 /* Guess the internal cluster (page) size of the iscsi target by the means
1998 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1999 * reasonable size */
2000 if (iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
>= 4 * 1024 &&
2001 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
<= 16 * 1024 * 1024) {
2002 iscsilun
->cluster_size
= iscsilun
->bl
.opt_unmap_gran
*
2003 iscsilun
->block_size
;
2004 if (iscsilun
->lbprz
) {
2005 ret
= iscsi_allocmap_init(iscsilun
, bs
->open_flags
);
2009 if (iscsilun
->lbprz
&& iscsilun
->lbp
.lbpws
) {
2010 bs
->supported_zero_flags
= BDRV_REQ_MAY_UNMAP
;
2014 qemu_opts_del(opts
);
2015 g_free(initiator_name
);
2017 scsi_free_scsi_task(task
);
2021 if (iscsi
!= NULL
) {
2022 if (iscsi_is_logged_in(iscsi
)) {
2023 iscsi_logout_sync(iscsi
);
2025 iscsi_destroy_context(iscsi
);
2027 memset(iscsilun
, 0, sizeof(IscsiLun
));
2033 static void iscsi_close(BlockDriverState
*bs
)
2035 IscsiLun
*iscsilun
= bs
->opaque
;
2036 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
2038 iscsi_detach_aio_context(bs
);
2039 if (iscsi_is_logged_in(iscsi
)) {
2040 iscsi_logout_sync(iscsi
);
2042 iscsi_destroy_context(iscsi
);
2044 g_free(iscsilun
->dd
->designator
);
2045 g_free(iscsilun
->dd
);
2047 g_free(iscsilun
->zeroblock
);
2048 iscsi_allocmap_free(iscsilun
);
2049 qemu_mutex_destroy(&iscsilun
->mutex
);
2050 memset(iscsilun
, 0, sizeof(IscsiLun
));
2053 static void iscsi_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
2055 /* We don't actually refresh here, but just return data queried in
2056 * iscsi_open(): iscsi targets don't change their limits. */
2058 IscsiLun
*iscsilun
= bs
->opaque
;
2059 uint64_t max_xfer_len
= iscsilun
->use_16_for_rw
? 0xffffffff : 0xffff;
2060 unsigned int block_size
= MAX(BDRV_SECTOR_SIZE
, iscsilun
->block_size
);
2062 assert(iscsilun
->block_size
>= BDRV_SECTOR_SIZE
|| bs
->sg
);
2064 bs
->bl
.request_alignment
= block_size
;
2066 if (iscsilun
->bl
.max_xfer_len
) {
2067 max_xfer_len
= MIN(max_xfer_len
, iscsilun
->bl
.max_xfer_len
);
2070 if (max_xfer_len
* block_size
< INT_MAX
) {
2071 bs
->bl
.max_transfer
= max_xfer_len
* iscsilun
->block_size
;
2074 if (iscsilun
->lbp
.lbpu
) {
2075 if (iscsilun
->bl
.max_unmap
< 0xffffffff / block_size
) {
2076 bs
->bl
.max_pdiscard
=
2077 iscsilun
->bl
.max_unmap
* iscsilun
->block_size
;
2079 bs
->bl
.pdiscard_alignment
=
2080 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
2082 bs
->bl
.pdiscard_alignment
= iscsilun
->block_size
;
2085 if (iscsilun
->bl
.max_ws_len
< 0xffffffff / block_size
) {
2086 bs
->bl
.max_pwrite_zeroes
=
2087 iscsilun
->bl
.max_ws_len
* iscsilun
->block_size
;
2089 if (iscsilun
->lbp
.lbpws
) {
2090 bs
->bl
.pwrite_zeroes_alignment
=
2091 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
2093 bs
->bl
.pwrite_zeroes_alignment
= iscsilun
->block_size
;
2095 if (iscsilun
->bl
.opt_xfer_len
&&
2096 iscsilun
->bl
.opt_xfer_len
< INT_MAX
/ block_size
) {
2097 bs
->bl
.opt_transfer
= pow2floor(iscsilun
->bl
.opt_xfer_len
*
2098 iscsilun
->block_size
);
2102 /* Note that this will not re-establish a connection with an iSCSI target - it
2103 * is effectively a NOP. */
2104 static int iscsi_reopen_prepare(BDRVReopenState
*state
,
2105 BlockReopenQueue
*queue
, Error
**errp
)
2107 IscsiLun
*iscsilun
= state
->bs
->opaque
;
2109 if (state
->flags
& BDRV_O_RDWR
&& iscsilun
->write_protected
) {
2110 error_setg(errp
, "Cannot open a write protected LUN as read-write");
2116 static void iscsi_reopen_commit(BDRVReopenState
*reopen_state
)
2118 IscsiLun
*iscsilun
= reopen_state
->bs
->opaque
;
2120 /* the cache.direct status might have changed */
2121 if (iscsilun
->allocmap
!= NULL
) {
2122 iscsi_allocmap_init(iscsilun
, reopen_state
->flags
);
2126 static int coroutine_fn
iscsi_co_truncate(BlockDriverState
*bs
, int64_t offset
,
2127 bool exact
, PreallocMode prealloc
,
2130 IscsiLun
*iscsilun
= bs
->opaque
;
2132 Error
*local_err
= NULL
;
2134 if (prealloc
!= PREALLOC_MODE_OFF
) {
2135 error_setg(errp
, "Unsupported preallocation mode '%s'",
2136 PreallocMode_str(prealloc
));
2140 if (iscsilun
->type
!= TYPE_DISK
) {
2141 error_setg(errp
, "Cannot resize non-disk iSCSI devices");
2145 iscsi_readcapacity_sync(iscsilun
, &local_err
);
2146 if (local_err
!= NULL
) {
2147 error_propagate(errp
, local_err
);
2151 cur_length
= iscsi_getlength(bs
);
2152 if (offset
!= cur_length
&& exact
) {
2153 error_setg(errp
, "Cannot resize iSCSI devices");
2155 } else if (offset
> cur_length
) {
2156 error_setg(errp
, "Cannot grow iSCSI devices");
2160 if (iscsilun
->allocmap
!= NULL
) {
2161 iscsi_allocmap_init(iscsilun
, bs
->open_flags
);
2167 static int coroutine_fn
iscsi_co_create_opts(const char *filename
, QemuOpts
*opts
,
2171 int64_t total_size
= 0;
2172 BlockDriverState
*bs
;
2173 IscsiLun
*iscsilun
= NULL
;
2175 Error
*local_err
= NULL
;
2179 /* Read out options */
2180 total_size
= DIV_ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2182 bs
->opaque
= g_new0(struct IscsiLun
, 1);
2183 iscsilun
= bs
->opaque
;
2185 bs_options
= qdict_new();
2186 iscsi_parse_filename(filename
, bs_options
, &local_err
);
2188 error_propagate(errp
, local_err
);
2191 ret
= iscsi_open(bs
, bs_options
, 0, NULL
);
2193 qobject_unref(bs_options
);
2198 iscsi_detach_aio_context(bs
);
2199 if (iscsilun
->type
!= TYPE_DISK
) {
2203 if (bs
->total_sectors
< total_size
) {
2210 if (iscsilun
->iscsi
!= NULL
) {
2211 iscsi_destroy_context(iscsilun
->iscsi
);
2219 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2221 IscsiLun
*iscsilun
= bs
->opaque
;
2222 bdi
->unallocated_blocks_are_zero
= iscsilun
->lbprz
;
2223 bdi
->cluster_size
= iscsilun
->cluster_size
;
2227 static void coroutine_fn
iscsi_co_invalidate_cache(BlockDriverState
*bs
,
2230 IscsiLun
*iscsilun
= bs
->opaque
;
2231 iscsi_allocmap_invalidate(iscsilun
);
2234 static int coroutine_fn
iscsi_co_copy_range_from(BlockDriverState
*bs
,
2236 uint64_t src_offset
,
2238 uint64_t dst_offset
,
2240 BdrvRequestFlags read_flags
,
2241 BdrvRequestFlags write_flags
)
2243 return bdrv_co_copy_range_to(src
, src_offset
, dst
, dst_offset
, bytes
,
2244 read_flags
, write_flags
);
2247 static struct scsi_task
*iscsi_xcopy_task(int param_len
)
2249 struct scsi_task
*task
;
2251 task
= g_new0(struct scsi_task
, 1);
2253 task
->cdb
[0] = EXTENDED_COPY
;
2254 task
->cdb
[10] = (param_len
>> 24) & 0xFF;
2255 task
->cdb
[11] = (param_len
>> 16) & 0xFF;
2256 task
->cdb
[12] = (param_len
>> 8) & 0xFF;
2257 task
->cdb
[13] = param_len
& 0xFF;
2258 task
->cdb_size
= 16;
2259 task
->xfer_dir
= SCSI_XFER_WRITE
;
2260 task
->expxferlen
= param_len
;
2265 static void iscsi_populate_target_desc(unsigned char *desc
, IscsiLun
*lun
)
2267 struct scsi_inquiry_device_designator
*dd
= lun
->dd
;
2269 memset(desc
, 0, 32);
2270 desc
[0] = 0xE4; /* IDENT_DESCR_TGT_DESCR */
2271 desc
[4] = dd
->code_set
;
2272 desc
[5] = (dd
->designator_type
& 0xF)
2273 | ((dd
->association
& 3) << 4);
2274 desc
[7] = dd
->designator_length
;
2275 memcpy(desc
+ 8, dd
->designator
, MIN(dd
->designator_length
, 20));
2278 desc
[29] = (lun
->block_size
>> 16) & 0xFF;
2279 desc
[30] = (lun
->block_size
>> 8) & 0xFF;
2280 desc
[31] = lun
->block_size
& 0xFF;
2283 static void iscsi_xcopy_desc_hdr(uint8_t *hdr
, int dc
, int cat
, int src_index
,
2286 hdr
[0] = 0x02; /* BLK_TO_BLK_SEG_DESCR */
2287 hdr
[1] = ((dc
<< 1) | cat
) & 0xFF;
2288 hdr
[2] = (XCOPY_BLK2BLK_SEG_DESC_SIZE
>> 8) & 0xFF;
2289 /* don't account for the first 4 bytes in descriptor header*/
2290 hdr
[3] = (XCOPY_BLK2BLK_SEG_DESC_SIZE
- 4 /* SEG_DESC_SRC_INDEX_OFFSET */) & 0xFF;
2291 hdr
[4] = (src_index
>> 8) & 0xFF;
2292 hdr
[5] = src_index
& 0xFF;
2293 hdr
[6] = (dst_index
>> 8) & 0xFF;
2294 hdr
[7] = dst_index
& 0xFF;
2297 static void iscsi_xcopy_populate_desc(uint8_t *desc
, int dc
, int cat
,
2298 int src_index
, int dst_index
, int num_blks
,
2299 uint64_t src_lba
, uint64_t dst_lba
)
2301 iscsi_xcopy_desc_hdr(desc
, dc
, cat
, src_index
, dst_index
);
2303 /* The caller should verify the request size */
2304 assert(num_blks
< 65536);
2305 desc
[10] = (num_blks
>> 8) & 0xFF;
2306 desc
[11] = num_blks
& 0xFF;
2307 desc
[12] = (src_lba
>> 56) & 0xFF;
2308 desc
[13] = (src_lba
>> 48) & 0xFF;
2309 desc
[14] = (src_lba
>> 40) & 0xFF;
2310 desc
[15] = (src_lba
>> 32) & 0xFF;
2311 desc
[16] = (src_lba
>> 24) & 0xFF;
2312 desc
[17] = (src_lba
>> 16) & 0xFF;
2313 desc
[18] = (src_lba
>> 8) & 0xFF;
2314 desc
[19] = src_lba
& 0xFF;
2315 desc
[20] = (dst_lba
>> 56) & 0xFF;
2316 desc
[21] = (dst_lba
>> 48) & 0xFF;
2317 desc
[22] = (dst_lba
>> 40) & 0xFF;
2318 desc
[23] = (dst_lba
>> 32) & 0xFF;
2319 desc
[24] = (dst_lba
>> 24) & 0xFF;
2320 desc
[25] = (dst_lba
>> 16) & 0xFF;
2321 desc
[26] = (dst_lba
>> 8) & 0xFF;
2322 desc
[27] = dst_lba
& 0xFF;
2325 static void iscsi_xcopy_populate_header(unsigned char *buf
, int list_id
, int str
,
2326 int list_id_usage
, int prio
,
2328 int seg_desc_len
, int inline_data_len
)
2331 buf
[1] = ((str
& 1) << 5) | ((list_id_usage
& 3) << 3) | (prio
& 7);
2332 buf
[2] = (tgt_desc_len
>> 8) & 0xFF;
2333 buf
[3] = tgt_desc_len
& 0xFF;
2334 buf
[8] = (seg_desc_len
>> 24) & 0xFF;
2335 buf
[9] = (seg_desc_len
>> 16) & 0xFF;
2336 buf
[10] = (seg_desc_len
>> 8) & 0xFF;
2337 buf
[11] = seg_desc_len
& 0xFF;
2338 buf
[12] = (inline_data_len
>> 24) & 0xFF;
2339 buf
[13] = (inline_data_len
>> 16) & 0xFF;
2340 buf
[14] = (inline_data_len
>> 8) & 0xFF;
2341 buf
[15] = inline_data_len
& 0xFF;
2344 static void iscsi_xcopy_data(struct iscsi_data
*data
,
2345 IscsiLun
*src
, int64_t src_lba
,
2346 IscsiLun
*dst
, int64_t dst_lba
,
2347 uint16_t num_blocks
)
2350 const int src_offset
= XCOPY_DESC_OFFSET
;
2351 const int dst_offset
= XCOPY_DESC_OFFSET
+ IDENT_DESCR_TGT_DESCR_SIZE
;
2352 const int seg_offset
= dst_offset
+ IDENT_DESCR_TGT_DESCR_SIZE
;
2354 data
->size
= XCOPY_DESC_OFFSET
+
2355 IDENT_DESCR_TGT_DESCR_SIZE
* 2 +
2356 XCOPY_BLK2BLK_SEG_DESC_SIZE
;
2357 data
->data
= g_malloc0(data
->size
);
2360 /* Initialise the parameter list header */
2361 iscsi_xcopy_populate_header(buf
, 1, 0, 2 /* LIST_ID_USAGE_DISCARD */,
2362 0, 2 * IDENT_DESCR_TGT_DESCR_SIZE
,
2363 XCOPY_BLK2BLK_SEG_DESC_SIZE
,
2366 /* Initialise CSCD list with one src + one dst descriptor */
2367 iscsi_populate_target_desc(&buf
[src_offset
], src
);
2368 iscsi_populate_target_desc(&buf
[dst_offset
], dst
);
2370 /* Initialise one segment descriptor */
2371 iscsi_xcopy_populate_desc(&buf
[seg_offset
], 0, 0, 0, 1, num_blocks
,
2375 static int coroutine_fn
iscsi_co_copy_range_to(BlockDriverState
*bs
,
2377 uint64_t src_offset
,
2379 uint64_t dst_offset
,
2381 BdrvRequestFlags read_flags
,
2382 BdrvRequestFlags write_flags
)
2384 IscsiLun
*dst_lun
= dst
->bs
->opaque
;
2386 struct IscsiTask iscsi_task
;
2387 struct iscsi_data data
;
2391 if (src
->bs
->drv
->bdrv_co_copy_range_to
!= iscsi_co_copy_range_to
) {
2394 src_lun
= src
->bs
->opaque
;
2396 if (!src_lun
->dd
|| !dst_lun
->dd
) {
2399 if (!is_byte_request_lun_aligned(dst_offset
, bytes
, dst_lun
)) {
2402 if (!is_byte_request_lun_aligned(src_offset
, bytes
, src_lun
)) {
2405 if (dst_lun
->block_size
!= src_lun
->block_size
||
2406 !dst_lun
->block_size
) {
2410 block_size
= dst_lun
->block_size
;
2411 if (bytes
/ block_size
> 65535) {
2415 iscsi_xcopy_data(&data
,
2416 src_lun
, src_offset
/ block_size
,
2417 dst_lun
, dst_offset
/ block_size
,
2418 bytes
/ block_size
);
2420 iscsi_co_init_iscsitask(dst_lun
, &iscsi_task
);
2422 qemu_mutex_lock(&dst_lun
->mutex
);
2423 iscsi_task
.task
= iscsi_xcopy_task(data
.size
);
2425 if (iscsi_scsi_command_async(dst_lun
->iscsi
, dst_lun
->lun
,
2426 iscsi_task
.task
, iscsi_co_generic_cb
,
2428 &iscsi_task
) != 0) {
2433 iscsi_co_wait_for_task(&iscsi_task
, dst_lun
);
2435 if (iscsi_task
.do_retry
) {
2436 iscsi_task
.complete
= 0;
2440 if (iscsi_task
.status
!= SCSI_STATUS_GOOD
) {
2441 r
= iscsi_task
.err_code
;
2447 trace_iscsi_xcopy(src_lun
, src_offset
, dst_lun
, dst_offset
, bytes
, r
);
2448 g_free(iscsi_task
.task
);
2449 qemu_mutex_unlock(&dst_lun
->mutex
);
2450 g_free(iscsi_task
.err_str
);
2454 static QemuOptsList iscsi_create_opts
= {
2455 .name
= "iscsi-create-opts",
2456 .head
= QTAILQ_HEAD_INITIALIZER(iscsi_create_opts
.head
),
2459 .name
= BLOCK_OPT_SIZE
,
2460 .type
= QEMU_OPT_SIZE
,
2461 .help
= "Virtual disk size"
2463 { /* end of list */ }
2467 static const char *const iscsi_strong_runtime_opts
[] = {
2481 static BlockDriver bdrv_iscsi
= {
2482 .format_name
= "iscsi",
2483 .protocol_name
= "iscsi",
2485 .instance_size
= sizeof(IscsiLun
),
2486 .bdrv_parse_filename
= iscsi_parse_filename
,
2487 .bdrv_file_open
= iscsi_open
,
2488 .bdrv_close
= iscsi_close
,
2489 .bdrv_co_create_opts
= iscsi_co_create_opts
,
2490 .create_opts
= &iscsi_create_opts
,
2491 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
2492 .bdrv_reopen_commit
= iscsi_reopen_commit
,
2493 .bdrv_co_invalidate_cache
= iscsi_co_invalidate_cache
,
2495 .bdrv_getlength
= iscsi_getlength
,
2496 .bdrv_get_info
= iscsi_get_info
,
2497 .bdrv_co_truncate
= iscsi_co_truncate
,
2498 .bdrv_refresh_limits
= iscsi_refresh_limits
,
2500 .bdrv_co_block_status
= iscsi_co_block_status
,
2501 .bdrv_co_pdiscard
= iscsi_co_pdiscard
,
2502 .bdrv_co_copy_range_from
= iscsi_co_copy_range_from
,
2503 .bdrv_co_copy_range_to
= iscsi_co_copy_range_to
,
2504 .bdrv_co_pwrite_zeroes
= iscsi_co_pwrite_zeroes
,
2505 .bdrv_co_readv
= iscsi_co_readv
,
2506 .bdrv_co_writev
= iscsi_co_writev
,
2507 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
2510 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
2513 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
2514 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
2516 .strong_runtime_opts
= iscsi_strong_runtime_opts
,
2519 #if LIBISCSI_API_VERSION >= (20160603)
2520 static BlockDriver bdrv_iser
= {
2521 .format_name
= "iser",
2522 .protocol_name
= "iser",
2524 .instance_size
= sizeof(IscsiLun
),
2525 .bdrv_parse_filename
= iscsi_parse_filename
,
2526 .bdrv_file_open
= iscsi_open
,
2527 .bdrv_close
= iscsi_close
,
2528 .bdrv_co_create_opts
= iscsi_co_create_opts
,
2529 .create_opts
= &iscsi_create_opts
,
2530 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
2531 .bdrv_reopen_commit
= iscsi_reopen_commit
,
2532 .bdrv_co_invalidate_cache
= iscsi_co_invalidate_cache
,
2534 .bdrv_getlength
= iscsi_getlength
,
2535 .bdrv_get_info
= iscsi_get_info
,
2536 .bdrv_co_truncate
= iscsi_co_truncate
,
2537 .bdrv_refresh_limits
= iscsi_refresh_limits
,
2539 .bdrv_co_block_status
= iscsi_co_block_status
,
2540 .bdrv_co_pdiscard
= iscsi_co_pdiscard
,
2541 .bdrv_co_copy_range_from
= iscsi_co_copy_range_from
,
2542 .bdrv_co_copy_range_to
= iscsi_co_copy_range_to
,
2543 .bdrv_co_pwrite_zeroes
= iscsi_co_pwrite_zeroes
,
2544 .bdrv_co_readv
= iscsi_co_readv
,
2545 .bdrv_co_writev
= iscsi_co_writev
,
2546 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
2549 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
2552 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
2553 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
2555 .strong_runtime_opts
= iscsi_strong_runtime_opts
,
2559 static void iscsi_block_init(void)
2561 bdrv_register(&bdrv_iscsi
);
2562 #if LIBISCSI_API_VERSION >= (20160603)
2563 bdrv_register(&bdrv_iser
);
2567 block_init(iscsi_block_init
);