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 "block/qdict.h"
37 #include "scsi/constants.h"
39 #include "qemu/option.h"
40 #include "qemu/uuid.h"
41 #include "qapi/error.h"
42 #include "qapi/qapi-commands-misc.h"
43 #include "qapi/qmp/qdict.h"
44 #include "qapi/qmp/qstring.h"
45 #include "crypto/secret.h"
46 #include "scsi/utils.h"
49 /* Conflict between scsi/utils.h and libiscsi! :( */
50 #define SCSI_XFER_NONE ISCSI_XFER_NONE
51 #include <iscsi/iscsi.h>
52 #include <iscsi/scsi-lowlevel.h>
54 QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE
!= (int)ISCSI_XFER_NONE
);
60 typedef struct IscsiLun
{
61 struct iscsi_context
*iscsi
;
62 AioContext
*aio_context
;
64 enum scsi_inquiry_peripheral_device_type type
;
69 QEMUTimer
*event_timer
;
71 struct scsi_inquiry_logical_block_provisioning lbp
;
72 struct scsi_inquiry_block_limits bl
;
73 struct scsi_inquiry_device_designator
*dd
;
74 unsigned char *zeroblock
;
75 /* The allocmap tracks which clusters (pages) on the iSCSI target are
76 * allocated and which are not. In case a target returns zeros for
77 * unallocated pages (iscsilun->lprz) we can directly return zeros instead
78 * of reading zeros over the wire if a read request falls within an
79 * unallocated block. As there are 3 possible states we need 2 bitmaps to
80 * track. allocmap_valid keeps track if QEMU's information about a page is
81 * valid. allocmap tracks if a page is allocated or not. In case QEMU has no
82 * valid information about a page the corresponding allocmap entry should be
83 * switched to unallocated as well to force a new lookup of the allocation
84 * status as lookups are generally skipped if a page is suspect to be
85 * allocated. If a iSCSI target is opened with cache.direct = on the
86 * allocmap_valid does not exist turning all cached information invalid so
87 * that a fresh lookup is made for any page even if allocmap entry returns
88 * it's unallocated. */
89 unsigned long *allocmap
;
90 unsigned long *allocmap_valid
;
99 bool request_timed_out
;
102 typedef struct IscsiTask
{
107 struct scsi_task
*task
;
110 QEMUTimer retry_timer
;
115 typedef struct IscsiAIOCB
{
119 struct scsi_task
*task
;
130 /* libiscsi uses time_t so its enough to process events every second */
131 #define EVENT_INTERVAL 1000
132 #define NOP_INTERVAL 5000
133 #define MAX_NOP_FAILURES 3
134 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
135 static const unsigned iscsi_retry_times
[] = {8, 32, 128, 512, 2048, 8192, 32768};
137 /* this threshold is a trade-off knob to choose between
138 * the potential additional overhead of an extra GET_LBA_STATUS request
139 * vs. unnecessarily reading a lot of zero sectors over the wire.
140 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
141 * sectors we check the allocation status of the area covered by the
142 * request first if the allocationmap indicates that the area might be
144 #define ISCSI_CHECKALLOC_THRES 64
151 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
,
291 /* Called (via iscsi_service) with QemuMutex held. */
293 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
296 IscsiAIOCB
*acb
= private_data
;
298 /* If the command callback hasn't been called yet, drop the task */
300 /* Call iscsi_aio_ioctl_cb() with SCSI_STATUS_CANCELLED */
301 iscsi_scsi_cancel_task(iscsi
, acb
->task
);
304 qemu_aio_unref(acb
); /* acquired in iscsi_aio_cancel() */
308 iscsi_aio_cancel(BlockAIOCB
*blockacb
)
310 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
311 IscsiLun
*iscsilun
= acb
->iscsilun
;
313 qemu_mutex_lock(&iscsilun
->mutex
);
315 /* If it was cancelled or completed already, our work is done here */
316 if (acb
->cancelled
|| acb
->status
!= -EINPROGRESS
) {
317 qemu_mutex_unlock(&iscsilun
->mutex
);
321 acb
->cancelled
= true;
323 qemu_aio_ref(acb
); /* released in iscsi_abort_task_cb() */
325 /* send a task mgmt call to the target to cancel the task on the target */
326 if (iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
327 iscsi_abort_task_cb
, acb
) < 0) {
328 qemu_aio_unref(acb
); /* since iscsi_abort_task_cb() won't be called */
331 qemu_mutex_unlock(&iscsilun
->mutex
);
334 static const AIOCBInfo iscsi_aiocb_info
= {
335 .aiocb_size
= sizeof(IscsiAIOCB
),
336 .cancel_async
= iscsi_aio_cancel
,
340 static void iscsi_process_read(void *arg
);
341 static void iscsi_process_write(void *arg
);
343 /* Called with QemuMutex held. */
345 iscsi_set_events(IscsiLun
*iscsilun
)
347 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
348 int ev
= iscsi_which_events(iscsi
);
350 if (ev
!= iscsilun
->events
) {
351 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsi
),
353 (ev
& POLLIN
) ? iscsi_process_read
: NULL
,
354 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
357 iscsilun
->events
= ev
;
361 static void iscsi_timed_check_events(void *opaque
)
363 IscsiLun
*iscsilun
= opaque
;
365 qemu_mutex_lock(&iscsilun
->mutex
);
367 /* check for timed out requests */
368 iscsi_service(iscsilun
->iscsi
, 0);
370 if (iscsilun
->request_timed_out
) {
371 iscsilun
->request_timed_out
= false;
372 iscsi_reconnect(iscsilun
->iscsi
);
375 /* newer versions of libiscsi may return zero events. Ensure we are able
376 * to return to service once this situation changes. */
377 iscsi_set_events(iscsilun
);
379 qemu_mutex_unlock(&iscsilun
->mutex
);
381 timer_mod(iscsilun
->event_timer
,
382 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
386 iscsi_process_read(void *arg
)
388 IscsiLun
*iscsilun
= arg
;
389 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
391 qemu_mutex_lock(&iscsilun
->mutex
);
392 iscsi_service(iscsi
, POLLIN
);
393 iscsi_set_events(iscsilun
);
394 qemu_mutex_unlock(&iscsilun
->mutex
);
398 iscsi_process_write(void *arg
)
400 IscsiLun
*iscsilun
= arg
;
401 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
403 qemu_mutex_lock(&iscsilun
->mutex
);
404 iscsi_service(iscsi
, POLLOUT
);
405 iscsi_set_events(iscsilun
);
406 qemu_mutex_unlock(&iscsilun
->mutex
);
409 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
411 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
414 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
416 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
419 static bool is_byte_request_lun_aligned(int64_t offset
, int count
,
422 if (offset
% iscsilun
->block_size
|| count
% iscsilun
->block_size
) {
423 error_report("iSCSI misaligned request: "
424 "iscsilun->block_size %u, offset %" PRIi64
426 iscsilun
->block_size
, offset
, count
);
432 static bool is_sector_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
435 assert(nb_sectors
<= BDRV_REQUEST_MAX_SECTORS
);
436 return is_byte_request_lun_aligned(sector_num
<< BDRV_SECTOR_BITS
,
437 nb_sectors
<< BDRV_SECTOR_BITS
,
441 static void iscsi_allocmap_free(IscsiLun
*iscsilun
)
443 g_free(iscsilun
->allocmap
);
444 g_free(iscsilun
->allocmap_valid
);
445 iscsilun
->allocmap
= NULL
;
446 iscsilun
->allocmap_valid
= NULL
;
450 static int iscsi_allocmap_init(IscsiLun
*iscsilun
, int open_flags
)
452 iscsi_allocmap_free(iscsilun
);
454 assert(iscsilun
->cluster_size
);
455 iscsilun
->allocmap_size
=
456 DIV_ROUND_UP(iscsilun
->num_blocks
* iscsilun
->block_size
,
457 iscsilun
->cluster_size
);
459 iscsilun
->allocmap
= bitmap_try_new(iscsilun
->allocmap_size
);
460 if (!iscsilun
->allocmap
) {
464 if (open_flags
& BDRV_O_NOCACHE
) {
465 /* when cache.direct = on all allocmap entries are
466 * treated as invalid to force a relookup of the block
467 * status on every read request */
471 iscsilun
->allocmap_valid
= bitmap_try_new(iscsilun
->allocmap_size
);
472 if (!iscsilun
->allocmap_valid
) {
473 /* if we are under memory pressure free the allocmap as well */
474 iscsi_allocmap_free(iscsilun
);
482 iscsi_allocmap_update(IscsiLun
*iscsilun
, int64_t offset
,
483 int64_t bytes
, bool allocated
, bool valid
)
485 int64_t cl_num_expanded
, nb_cls_expanded
, cl_num_shrunk
, nb_cls_shrunk
;
487 if (iscsilun
->allocmap
== NULL
) {
490 /* expand to entirely contain all affected clusters */
491 assert(iscsilun
->cluster_size
);
492 cl_num_expanded
= offset
/ iscsilun
->cluster_size
;
493 nb_cls_expanded
= DIV_ROUND_UP(offset
+ bytes
,
494 iscsilun
->cluster_size
) - cl_num_expanded
;
495 /* shrink to touch only completely contained clusters */
496 cl_num_shrunk
= DIV_ROUND_UP(offset
, iscsilun
->cluster_size
);
497 nb_cls_shrunk
= (offset
+ bytes
) / iscsilun
->cluster_size
- cl_num_shrunk
;
499 bitmap_set(iscsilun
->allocmap
, cl_num_expanded
, nb_cls_expanded
);
501 if (nb_cls_shrunk
> 0) {
502 bitmap_clear(iscsilun
->allocmap
, cl_num_shrunk
, nb_cls_shrunk
);
506 if (iscsilun
->allocmap_valid
== NULL
) {
510 if (nb_cls_shrunk
> 0) {
511 bitmap_set(iscsilun
->allocmap_valid
, cl_num_shrunk
, nb_cls_shrunk
);
514 bitmap_clear(iscsilun
->allocmap_valid
, cl_num_expanded
,
520 iscsi_allocmap_set_allocated(IscsiLun
*iscsilun
, int64_t offset
,
523 iscsi_allocmap_update(iscsilun
, offset
, bytes
, true, true);
527 iscsi_allocmap_set_unallocated(IscsiLun
*iscsilun
, int64_t offset
,
530 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
531 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
533 iscsi_allocmap_update(iscsilun
, offset
, bytes
, false, true);
536 static void iscsi_allocmap_set_invalid(IscsiLun
*iscsilun
, int64_t offset
,
539 iscsi_allocmap_update(iscsilun
, offset
, bytes
, false, false);
542 static void iscsi_allocmap_invalidate(IscsiLun
*iscsilun
)
544 if (iscsilun
->allocmap
) {
545 bitmap_zero(iscsilun
->allocmap
, iscsilun
->allocmap_size
);
547 if (iscsilun
->allocmap_valid
) {
548 bitmap_zero(iscsilun
->allocmap_valid
, iscsilun
->allocmap_size
);
553 iscsi_allocmap_is_allocated(IscsiLun
*iscsilun
, int64_t offset
,
557 if (iscsilun
->allocmap
== NULL
) {
560 assert(iscsilun
->cluster_size
);
561 size
= DIV_ROUND_UP(offset
+ bytes
, iscsilun
->cluster_size
);
562 return !(find_next_bit(iscsilun
->allocmap
, size
,
563 offset
/ iscsilun
->cluster_size
) == size
);
566 static inline bool iscsi_allocmap_is_valid(IscsiLun
*iscsilun
,
567 int64_t offset
, int64_t bytes
)
570 if (iscsilun
->allocmap_valid
== NULL
) {
573 assert(iscsilun
->cluster_size
);
574 size
= DIV_ROUND_UP(offset
+ bytes
, iscsilun
->cluster_size
);
575 return (find_next_zero_bit(iscsilun
->allocmap_valid
, size
,
576 offset
/ iscsilun
->cluster_size
) == size
);
579 static void coroutine_fn
iscsi_co_wait_for_task(IscsiTask
*iTask
,
582 while (!iTask
->complete
) {
583 iscsi_set_events(iscsilun
);
584 qemu_mutex_unlock(&iscsilun
->mutex
);
585 qemu_coroutine_yield();
586 qemu_mutex_lock(&iscsilun
->mutex
);
590 static int coroutine_fn
591 iscsi_co_writev(BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
,
592 QEMUIOVector
*iov
, int flags
)
594 IscsiLun
*iscsilun
= bs
->opaque
;
595 struct IscsiTask iTask
;
597 uint32_t num_sectors
;
598 bool fua
= flags
& BDRV_REQ_FUA
;
602 assert(iscsilun
->dpofua
);
604 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
608 if (bs
->bl
.max_transfer
) {
609 assert(nb_sectors
<< BDRV_SECTOR_BITS
<= bs
->bl
.max_transfer
);
612 lba
= sector_qemu2lun(sector_num
, iscsilun
);
613 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
614 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
615 qemu_mutex_lock(&iscsilun
->mutex
);
617 if (iscsilun
->use_16_for_rw
) {
618 #if LIBISCSI_API_VERSION >= (20160603)
619 iTask
.task
= iscsi_write16_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
620 NULL
, num_sectors
* iscsilun
->block_size
,
621 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
622 iscsi_co_generic_cb
, &iTask
,
623 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
625 iTask
.task
= iscsi_write10_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
626 NULL
, num_sectors
* iscsilun
->block_size
,
627 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
628 iscsi_co_generic_cb
, &iTask
,
629 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
632 iTask
.task
= iscsi_write16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
633 NULL
, num_sectors
* iscsilun
->block_size
,
634 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
635 iscsi_co_generic_cb
, &iTask
);
637 iTask
.task
= iscsi_write10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
638 NULL
, num_sectors
* iscsilun
->block_size
,
639 iscsilun
->block_size
, 0, 0, fua
, 0, 0,
640 iscsi_co_generic_cb
, &iTask
);
643 if (iTask
.task
== NULL
) {
644 qemu_mutex_unlock(&iscsilun
->mutex
);
647 #if LIBISCSI_API_VERSION < (20160603)
648 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
651 iscsi_co_wait_for_task(&iTask
, iscsilun
);
653 if (iTask
.task
!= NULL
) {
654 scsi_free_scsi_task(iTask
.task
);
658 if (iTask
.do_retry
) {
663 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
664 iscsi_allocmap_set_invalid(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
665 nb_sectors
* BDRV_SECTOR_SIZE
);
666 error_report("iSCSI WRITE10/16 failed at lba %" PRIu64
": %s", lba
,
672 iscsi_allocmap_set_allocated(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
673 nb_sectors
* BDRV_SECTOR_SIZE
);
676 qemu_mutex_unlock(&iscsilun
->mutex
);
677 g_free(iTask
.err_str
);
683 static int coroutine_fn
iscsi_co_block_status(BlockDriverState
*bs
,
684 bool want_zero
, int64_t offset
,
685 int64_t bytes
, int64_t *pnum
,
687 BlockDriverState
**file
)
689 IscsiLun
*iscsilun
= bs
->opaque
;
690 struct scsi_get_lba_status
*lbas
= NULL
;
691 struct scsi_lba_status_descriptor
*lbasd
= NULL
;
692 struct IscsiTask iTask
;
696 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
698 assert(QEMU_IS_ALIGNED(offset
| bytes
, iscsilun
->block_size
));
700 /* default to all sectors allocated */
701 ret
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
707 /* LUN does not support logical block provisioning */
708 if (!iscsilun
->lbpme
) {
712 lba
= offset
/ iscsilun
->block_size
;
714 qemu_mutex_lock(&iscsilun
->mutex
);
716 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
717 lba
, 8 + 16, iscsi_co_generic_cb
,
722 iscsi_co_wait_for_task(&iTask
, iscsilun
);
724 if (iTask
.do_retry
) {
725 if (iTask
.task
!= NULL
) {
726 scsi_free_scsi_task(iTask
.task
);
733 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
734 /* in case the get_lba_status_callout fails (i.e.
735 * because the device is busy or the cmd is not
736 * supported) we pretend all blocks are allocated
737 * for backwards compatibility */
738 error_report("iSCSI GET_LBA_STATUS failed at lba %" PRIu64
": %s",
743 lbas
= scsi_datain_unmarshall(iTask
.task
);
749 lbasd
= &lbas
->descriptors
[0];
751 if (lba
!= lbasd
->lba
) {
756 *pnum
= (int64_t) lbasd
->num_blocks
* iscsilun
->block_size
;
758 if (lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_DEALLOCATED
||
759 lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_ANCHORED
) {
760 ret
&= ~BDRV_BLOCK_DATA
;
761 if (iscsilun
->lbprz
) {
762 ret
|= BDRV_BLOCK_ZERO
;
766 if (ret
& BDRV_BLOCK_ZERO
) {
767 iscsi_allocmap_set_unallocated(iscsilun
, offset
, *pnum
);
769 iscsi_allocmap_set_allocated(iscsilun
, offset
, *pnum
);
776 qemu_mutex_unlock(&iscsilun
->mutex
);
777 g_free(iTask
.err_str
);
779 if (iTask
.task
!= NULL
) {
780 scsi_free_scsi_task(iTask
.task
);
782 if (ret
> 0 && ret
& BDRV_BLOCK_OFFSET_VALID
&& file
) {
788 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
789 int64_t sector_num
, int nb_sectors
,
792 IscsiLun
*iscsilun
= bs
->opaque
;
793 struct IscsiTask iTask
;
795 uint32_t num_sectors
;
798 if (!is_sector_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
802 if (bs
->bl
.max_transfer
) {
803 assert(nb_sectors
<< BDRV_SECTOR_BITS
<= bs
->bl
.max_transfer
);
806 /* if cache.direct is off and we have a valid entry in our allocation map
807 * we can skip checking the block status and directly return zeroes if
808 * the request falls within an unallocated area */
809 if (iscsi_allocmap_is_valid(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
810 nb_sectors
* BDRV_SECTOR_SIZE
) &&
811 !iscsi_allocmap_is_allocated(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
812 nb_sectors
* BDRV_SECTOR_SIZE
)) {
813 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
817 if (nb_sectors
>= ISCSI_CHECKALLOC_THRES
&&
818 !iscsi_allocmap_is_valid(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
819 nb_sectors
* BDRV_SECTOR_SIZE
) &&
820 !iscsi_allocmap_is_allocated(iscsilun
, sector_num
* BDRV_SECTOR_SIZE
,
821 nb_sectors
* BDRV_SECTOR_SIZE
)) {
823 /* check the block status from the beginning of the cluster
824 * containing the start sector */
828 assert(iscsilun
->cluster_size
);
829 head
= (sector_num
* BDRV_SECTOR_SIZE
) % iscsilun
->cluster_size
;
830 ret
= iscsi_co_block_status(bs
, true,
831 sector_num
* BDRV_SECTOR_SIZE
- head
,
832 BDRV_REQUEST_MAX_BYTES
, &pnum
, NULL
, NULL
);
836 /* if the whole request falls into an unallocated area we can avoid
837 * reading and directly return zeroes instead */
838 if (ret
& BDRV_BLOCK_ZERO
&&
839 pnum
>= nb_sectors
* BDRV_SECTOR_SIZE
+ head
) {
840 qemu_iovec_memset(iov
, 0, 0x00, iov
->size
);
845 lba
= sector_qemu2lun(sector_num
, iscsilun
);
846 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
848 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
849 qemu_mutex_lock(&iscsilun
->mutex
);
851 if (iscsilun
->use_16_for_rw
) {
852 #if LIBISCSI_API_VERSION >= (20160603)
853 iTask
.task
= iscsi_read16_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
854 num_sectors
* iscsilun
->block_size
,
855 iscsilun
->block_size
, 0, 0, 0, 0, 0,
856 iscsi_co_generic_cb
, &iTask
,
857 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
859 iTask
.task
= iscsi_read10_iov_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
860 num_sectors
* iscsilun
->block_size
,
861 iscsilun
->block_size
,
863 iscsi_co_generic_cb
, &iTask
,
864 (struct scsi_iovec
*)iov
->iov
, iov
->niov
);
867 iTask
.task
= iscsi_read16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
868 num_sectors
* iscsilun
->block_size
,
869 iscsilun
->block_size
, 0, 0, 0, 0, 0,
870 iscsi_co_generic_cb
, &iTask
);
872 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
873 num_sectors
* iscsilun
->block_size
,
874 iscsilun
->block_size
,
876 iscsi_co_generic_cb
, &iTask
);
879 if (iTask
.task
== NULL
) {
880 qemu_mutex_unlock(&iscsilun
->mutex
);
883 #if LIBISCSI_API_VERSION < (20160603)
884 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
887 iscsi_co_wait_for_task(&iTask
, iscsilun
);
888 if (iTask
.task
!= NULL
) {
889 scsi_free_scsi_task(iTask
.task
);
893 if (iTask
.do_retry
) {
898 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
899 error_report("iSCSI READ10/16 failed at lba %" PRIu64
": %s",
904 qemu_mutex_unlock(&iscsilun
->mutex
);
905 g_free(iTask
.err_str
);
909 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
911 IscsiLun
*iscsilun
= bs
->opaque
;
912 struct IscsiTask iTask
;
915 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
916 qemu_mutex_lock(&iscsilun
->mutex
);
918 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
919 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
920 qemu_mutex_unlock(&iscsilun
->mutex
);
924 iscsi_co_wait_for_task(&iTask
, iscsilun
);
926 if (iTask
.task
!= NULL
) {
927 scsi_free_scsi_task(iTask
.task
);
931 if (iTask
.do_retry
) {
936 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
937 error_report("iSCSI SYNCHRONIZECACHE10 failed: %s", iTask
.err_str
);
941 qemu_mutex_unlock(&iscsilun
->mutex
);
942 g_free(iTask
.err_str
);
947 /* Called (via iscsi_service) with QemuMutex held. */
949 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
950 void *command_data
, void *opaque
)
952 IscsiAIOCB
*acb
= opaque
;
954 if (status
== SCSI_STATUS_CANCELLED
) {
956 acb
->status
= -ECANCELED
;
957 iscsi_schedule_bh(acb
);
964 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
965 iscsi_get_error(iscsi
));
966 acb
->status
= iscsi_translate_sense(&acb
->task
->sense
);
969 acb
->ioh
->driver_status
= 0;
970 acb
->ioh
->host_status
= 0;
972 acb
->ioh
->status
= status
;
974 #define SG_ERR_DRIVER_SENSE 0x08
976 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
979 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
981 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
982 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
983 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
984 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
987 iscsi_schedule_bh(acb
);
990 static void iscsi_ioctl_bh_completion(void *opaque
)
992 IscsiAIOCB
*acb
= opaque
;
994 qemu_bh_delete(acb
->bh
);
995 acb
->common
.cb(acb
->common
.opaque
, acb
->ret
);
999 static void iscsi_ioctl_handle_emulated(IscsiAIOCB
*acb
, int req
, void *buf
)
1001 BlockDriverState
*bs
= acb
->common
.bs
;
1002 IscsiLun
*iscsilun
= bs
->opaque
;
1006 case SG_GET_VERSION_NUM
:
1007 *(int *)buf
= 30000;
1009 case SG_GET_SCSI_ID
:
1010 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
1016 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
),
1017 iscsi_ioctl_bh_completion
, acb
);
1019 qemu_bh_schedule(acb
->bh
);
1022 static BlockAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
1023 unsigned long int req
, void *buf
,
1024 BlockCompletionFunc
*cb
, void *opaque
)
1026 IscsiLun
*iscsilun
= bs
->opaque
;
1027 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1028 struct iscsi_data data
;
1031 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
1033 acb
->iscsilun
= iscsilun
;
1035 acb
->status
= -EINPROGRESS
;
1037 acb
->cancelled
= false;
1040 iscsi_ioctl_handle_emulated(acb
, req
, buf
);
1041 return &acb
->common
;
1044 if (acb
->ioh
->cmd_len
> SCSI_CDB_MAX_SIZE
) {
1045 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
1046 acb
->ioh
->cmd_len
, SCSI_CDB_MAX_SIZE
);
1047 qemu_aio_unref(acb
);
1051 acb
->task
= malloc(sizeof(struct scsi_task
));
1052 if (acb
->task
== NULL
) {
1053 error_report("iSCSI: Failed to allocate task for scsi command. %s",
1054 iscsi_get_error(iscsi
));
1055 qemu_aio_unref(acb
);
1058 memset(acb
->task
, 0, sizeof(struct scsi_task
));
1060 switch (acb
->ioh
->dxfer_direction
) {
1061 case SG_DXFER_TO_DEV
:
1062 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
1064 case SG_DXFER_FROM_DEV
:
1065 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
1068 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
1072 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
1073 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
1074 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
1077 qemu_mutex_lock(&iscsilun
->mutex
);
1078 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
1079 if (acb
->ioh
->iovec_count
== 0) {
1080 data
.data
= acb
->ioh
->dxferp
;
1081 data
.size
= acb
->ioh
->dxfer_len
;
1083 scsi_task_set_iov_out(acb
->task
,
1084 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1085 acb
->ioh
->iovec_count
);
1089 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
1091 (data
.size
> 0) ? &data
: NULL
,
1093 qemu_mutex_unlock(&iscsilun
->mutex
);
1094 scsi_free_scsi_task(acb
->task
);
1095 qemu_aio_unref(acb
);
1099 /* tell libiscsi to read straight into the buffer we got from ioctl */
1100 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
1101 if (acb
->ioh
->iovec_count
== 0) {
1102 scsi_task_add_data_in_buffer(acb
->task
,
1103 acb
->ioh
->dxfer_len
,
1106 scsi_task_set_iov_in(acb
->task
,
1107 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
1108 acb
->ioh
->iovec_count
);
1112 iscsi_set_events(iscsilun
);
1113 qemu_mutex_unlock(&iscsilun
->mutex
);
1115 return &acb
->common
;
1121 iscsi_getlength(BlockDriverState
*bs
)
1123 IscsiLun
*iscsilun
= bs
->opaque
;
1126 len
= iscsilun
->num_blocks
;
1127 len
*= iscsilun
->block_size
;
1133 coroutine_fn
iscsi_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int bytes
)
1135 IscsiLun
*iscsilun
= bs
->opaque
;
1136 struct IscsiTask iTask
;
1137 struct unmap_list list
;
1140 if (!is_byte_request_lun_aligned(offset
, bytes
, iscsilun
)) {
1144 if (!iscsilun
->lbp
.lbpu
) {
1145 /* UNMAP is not supported by the target */
1149 list
.lba
= offset
/ iscsilun
->block_size
;
1150 list
.num
= bytes
/ iscsilun
->block_size
;
1152 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1153 qemu_mutex_lock(&iscsilun
->mutex
);
1155 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
1156 iscsi_co_generic_cb
, &iTask
) == NULL
) {
1161 iscsi_co_wait_for_task(&iTask
, iscsilun
);
1163 if (iTask
.task
!= NULL
) {
1164 scsi_free_scsi_task(iTask
.task
);
1168 if (iTask
.do_retry
) {
1173 iscsi_allocmap_set_invalid(iscsilun
, offset
, bytes
);
1175 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
) {
1176 /* the target might fail with a check condition if it
1177 is not happy with the alignment of the UNMAP request
1178 we silently fail in this case */
1182 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1183 error_report("iSCSI UNMAP failed at lba %" PRIu64
": %s",
1184 list
.lba
, iTask
.err_str
);
1190 qemu_mutex_unlock(&iscsilun
->mutex
);
1191 g_free(iTask
.err_str
);
1196 coroutine_fn
iscsi_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1197 int bytes
, BdrvRequestFlags flags
)
1199 IscsiLun
*iscsilun
= bs
->opaque
;
1200 struct IscsiTask iTask
;
1203 bool use_16_for_ws
= iscsilun
->use_16_for_rw
;
1206 if (!is_byte_request_lun_aligned(offset
, bytes
, iscsilun
)) {
1210 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1211 if (!use_16_for_ws
&& !iscsilun
->lbp
.lbpws10
) {
1212 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1213 use_16_for_ws
= true;
1215 if (use_16_for_ws
&& !iscsilun
->lbp
.lbpws
) {
1216 /* WRITESAME16 with UNMAP is not supported by the target,
1217 * fall back and try WRITESAME10/16 without UNMAP */
1218 flags
&= ~BDRV_REQ_MAY_UNMAP
;
1219 use_16_for_ws
= iscsilun
->use_16_for_rw
;
1223 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
1224 /* WRITESAME without UNMAP is not supported by the target */
1228 lba
= offset
/ iscsilun
->block_size
;
1229 nb_blocks
= bytes
/ iscsilun
->block_size
;
1231 if (iscsilun
->zeroblock
== NULL
) {
1232 iscsilun
->zeroblock
= g_try_malloc0(iscsilun
->block_size
);
1233 if (iscsilun
->zeroblock
== NULL
) {
1238 qemu_mutex_lock(&iscsilun
->mutex
);
1239 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
1241 if (use_16_for_ws
) {
1242 iTask
.task
= iscsi_writesame16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1243 iscsilun
->zeroblock
, iscsilun
->block_size
,
1244 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1245 0, 0, iscsi_co_generic_cb
, &iTask
);
1247 iTask
.task
= iscsi_writesame10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
1248 iscsilun
->zeroblock
, iscsilun
->block_size
,
1249 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
1250 0, 0, iscsi_co_generic_cb
, &iTask
);
1252 if (iTask
.task
== NULL
) {
1253 qemu_mutex_unlock(&iscsilun
->mutex
);
1257 iscsi_co_wait_for_task(&iTask
, iscsilun
);
1259 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
&&
1260 iTask
.task
->sense
.key
== SCSI_SENSE_ILLEGAL_REQUEST
&&
1261 (iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
||
1262 iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB
)) {
1263 /* WRITE SAME is not supported by the target */
1264 iscsilun
->has_write_same
= false;
1265 scsi_free_scsi_task(iTask
.task
);
1270 if (iTask
.task
!= NULL
) {
1271 scsi_free_scsi_task(iTask
.task
);
1275 if (iTask
.do_retry
) {
1280 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
1281 iscsi_allocmap_set_invalid(iscsilun
, offset
, bytes
);
1282 error_report("iSCSI WRITESAME10/16 failed at lba %" PRIu64
": %s",
1283 lba
, iTask
.err_str
);
1288 if (flags
& BDRV_REQ_MAY_UNMAP
) {
1289 iscsi_allocmap_set_invalid(iscsilun
, offset
, bytes
);
1291 iscsi_allocmap_set_allocated(iscsilun
, offset
, bytes
);
1295 qemu_mutex_unlock(&iscsilun
->mutex
);
1296 g_free(iTask
.err_str
);
1300 static void apply_chap(struct iscsi_context
*iscsi
, QemuOpts
*opts
,
1303 const char *user
= NULL
;
1304 const char *password
= NULL
;
1305 const char *secretid
;
1306 char *secret
= NULL
;
1308 user
= qemu_opt_get(opts
, "user");
1313 secretid
= qemu_opt_get(opts
, "password-secret");
1314 password
= qemu_opt_get(opts
, "password");
1315 if (secretid
&& password
) {
1316 error_setg(errp
, "'password' and 'password-secret' properties are "
1317 "mutually exclusive");
1321 secret
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
1326 } else if (!password
) {
1327 error_setg(errp
, "CHAP username specified but no password was given");
1331 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
1332 error_setg(errp
, "Failed to set initiator username and password");
1338 static void apply_header_digest(struct iscsi_context
*iscsi
, QemuOpts
*opts
,
1341 const char *digest
= NULL
;
1343 digest
= qemu_opt_get(opts
, "header-digest");
1345 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1346 } else if (!strcmp(digest
, "crc32c")) {
1347 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
1348 } else if (!strcmp(digest
, "none")) {
1349 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
1350 } else if (!strcmp(digest
, "crc32c-none")) {
1351 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
1352 } else if (!strcmp(digest
, "none-crc32c")) {
1353 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1355 error_setg(errp
, "Invalid header-digest setting : %s", digest
);
1359 static char *get_initiator_name(QemuOpts
*opts
)
1363 UuidInfo
*uuid_info
;
1365 name
= qemu_opt_get(opts
, "initiator-name");
1367 return g_strdup(name
);
1370 uuid_info
= qmp_query_uuid(NULL
);
1371 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
1372 name
= qemu_get_vm_name();
1374 name
= uuid_info
->UUID
;
1376 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1377 name
? ":" : "", name
? name
: "");
1378 qapi_free_UuidInfo(uuid_info
);
1382 static void iscsi_nop_timed_event(void *opaque
)
1384 IscsiLun
*iscsilun
= opaque
;
1386 qemu_mutex_lock(&iscsilun
->mutex
);
1387 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) >= MAX_NOP_FAILURES
) {
1388 error_report("iSCSI: NOP timeout. Reconnecting...");
1389 iscsilun
->request_timed_out
= true;
1390 } else if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
1391 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1395 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1396 iscsi_set_events(iscsilun
);
1399 qemu_mutex_unlock(&iscsilun
->mutex
);
1402 static void iscsi_readcapacity_sync(IscsiLun
*iscsilun
, Error
**errp
)
1404 struct scsi_task
*task
= NULL
;
1405 struct scsi_readcapacity10
*rc10
= NULL
;
1406 struct scsi_readcapacity16
*rc16
= NULL
;
1407 int retries
= ISCSI_CMD_RETRIES
;
1411 scsi_free_scsi_task(task
);
1415 switch (iscsilun
->type
) {
1417 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
1418 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1419 rc16
= scsi_datain_unmarshall(task
);
1421 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity16 data.");
1423 iscsilun
->block_size
= rc16
->block_length
;
1424 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1425 iscsilun
->lbpme
= !!rc16
->lbpme
;
1426 iscsilun
->lbprz
= !!rc16
->lbprz
;
1427 iscsilun
->use_16_for_rw
= (rc16
->returned_lba
> 0xffffffff);
1431 if (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1432 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
1435 /* Fall through and try READ CAPACITY(10) instead. */
1437 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1438 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1439 rc10
= scsi_datain_unmarshall(task
);
1441 error_setg(errp
, "iSCSI: Failed to unmarshall readcapacity10 data.");
1443 iscsilun
->block_size
= rc10
->block_size
;
1444 if (rc10
->lba
== 0) {
1445 /* blank disk loaded */
1446 iscsilun
->num_blocks
= 0;
1448 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1456 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1457 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1460 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1461 error_setg(errp
, "iSCSI: failed to send readcapacity10/16 command");
1462 } else if (!iscsilun
->block_size
||
1463 iscsilun
->block_size
% BDRV_SECTOR_SIZE
) {
1464 error_setg(errp
, "iSCSI: the target returned an invalid "
1465 "block size of %d.", iscsilun
->block_size
);
1468 scsi_free_scsi_task(task
);
1472 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
, int lun
,
1473 int evpd
, int pc
, void **inq
, Error
**errp
)
1476 struct scsi_task
*task
= NULL
;
1477 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, 64);
1478 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1481 full_size
= scsi_datain_getfullsize(task
);
1482 if (full_size
> task
->datain
.size
) {
1483 scsi_free_scsi_task(task
);
1485 /* we need more data for the full list */
1486 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, full_size
);
1487 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1492 *inq
= scsi_datain_unmarshall(task
);
1494 error_setg(errp
, "iSCSI: failed to unmarshall inquiry datain blob");
1501 error_setg(errp
, "iSCSI: Inquiry command failed : %s",
1502 iscsi_get_error(iscsi
));
1505 scsi_free_scsi_task(task
);
1510 static void iscsi_detach_aio_context(BlockDriverState
*bs
)
1512 IscsiLun
*iscsilun
= bs
->opaque
;
1514 aio_set_fd_handler(iscsilun
->aio_context
, iscsi_get_fd(iscsilun
->iscsi
),
1515 false, NULL
, NULL
, NULL
, NULL
);
1516 iscsilun
->events
= 0;
1518 if (iscsilun
->nop_timer
) {
1519 timer_del(iscsilun
->nop_timer
);
1520 timer_free(iscsilun
->nop_timer
);
1521 iscsilun
->nop_timer
= NULL
;
1523 if (iscsilun
->event_timer
) {
1524 timer_del(iscsilun
->event_timer
);
1525 timer_free(iscsilun
->event_timer
);
1526 iscsilun
->event_timer
= NULL
;
1530 static void iscsi_attach_aio_context(BlockDriverState
*bs
,
1531 AioContext
*new_context
)
1533 IscsiLun
*iscsilun
= bs
->opaque
;
1535 iscsilun
->aio_context
= new_context
;
1536 iscsi_set_events(iscsilun
);
1538 /* Set up a timer for sending out iSCSI NOPs */
1539 iscsilun
->nop_timer
= aio_timer_new(iscsilun
->aio_context
,
1540 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1541 iscsi_nop_timed_event
, iscsilun
);
1542 timer_mod(iscsilun
->nop_timer
,
1543 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1545 /* Set up a timer for periodic calls to iscsi_set_events and to
1546 * scan for command timeout */
1547 iscsilun
->event_timer
= aio_timer_new(iscsilun
->aio_context
,
1548 QEMU_CLOCK_REALTIME
, SCALE_MS
,
1549 iscsi_timed_check_events
, iscsilun
);
1550 timer_mod(iscsilun
->event_timer
,
1551 qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + EVENT_INTERVAL
);
1554 static void iscsi_modesense_sync(IscsiLun
*iscsilun
)
1556 struct scsi_task
*task
;
1557 struct scsi_mode_sense
*ms
= NULL
;
1558 iscsilun
->write_protected
= false;
1559 iscsilun
->dpofua
= false;
1561 task
= iscsi_modesense6_sync(iscsilun
->iscsi
, iscsilun
->lun
,
1562 1, SCSI_MODESENSE_PC_CURRENT
,
1565 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1566 iscsi_get_error(iscsilun
->iscsi
));
1570 if (task
->status
!= SCSI_STATUS_GOOD
) {
1571 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1574 ms
= scsi_datain_unmarshall(task
);
1576 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1577 iscsi_get_error(iscsilun
->iscsi
));
1580 iscsilun
->write_protected
= ms
->device_specific_parameter
& 0x80;
1581 iscsilun
->dpofua
= ms
->device_specific_parameter
& 0x10;
1585 scsi_free_scsi_task(task
);
1589 static void iscsi_parse_iscsi_option(const char *target
, QDict
*options
)
1593 const char *user
, *password
, *password_secret
, *initiator_name
,
1594 *header_digest
, *timeout
;
1596 list
= qemu_find_opts("iscsi");
1601 opts
= qemu_opts_find(list
, target
);
1603 opts
= QTAILQ_FIRST(&list
->head
);
1609 user
= qemu_opt_get(opts
, "user");
1611 qdict_set_default_str(options
, "user", user
);
1614 password
= qemu_opt_get(opts
, "password");
1616 qdict_set_default_str(options
, "password", password
);
1619 password_secret
= qemu_opt_get(opts
, "password-secret");
1620 if (password_secret
) {
1621 qdict_set_default_str(options
, "password-secret", password_secret
);
1624 initiator_name
= qemu_opt_get(opts
, "initiator-name");
1625 if (initiator_name
) {
1626 qdict_set_default_str(options
, "initiator-name", initiator_name
);
1629 header_digest
= qemu_opt_get(opts
, "header-digest");
1630 if (header_digest
) {
1631 /* -iscsi takes upper case values, but QAPI only supports lower case
1632 * enum constant names, so we have to convert here. */
1633 char *qapi_value
= g_ascii_strdown(header_digest
, -1);
1634 qdict_set_default_str(options
, "header-digest", qapi_value
);
1638 timeout
= qemu_opt_get(opts
, "timeout");
1640 qdict_set_default_str(options
, "timeout", timeout
);
1645 * We support iscsi url's on the form
1646 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1648 static void iscsi_parse_filename(const char *filename
, QDict
*options
,
1651 struct iscsi_url
*iscsi_url
;
1652 const char *transport_name
;
1655 iscsi_url
= iscsi_parse_full_url(NULL
, filename
);
1656 if (iscsi_url
== NULL
) {
1657 error_setg(errp
, "Failed to parse URL : %s", filename
);
1661 #if LIBISCSI_API_VERSION >= (20160603)
1662 switch (iscsi_url
->transport
) {
1664 transport_name
= "tcp";
1666 case ISER_TRANSPORT
:
1667 transport_name
= "iser";
1670 error_setg(errp
, "Unknown transport type (%d)",
1671 iscsi_url
->transport
);
1675 transport_name
= "tcp";
1678 qdict_set_default_str(options
, "transport", transport_name
);
1679 qdict_set_default_str(options
, "portal", iscsi_url
->portal
);
1680 qdict_set_default_str(options
, "target", iscsi_url
->target
);
1682 lun_str
= g_strdup_printf("%d", iscsi_url
->lun
);
1683 qdict_set_default_str(options
, "lun", lun_str
);
1686 /* User/password from -iscsi take precedence over those from the URL */
1687 iscsi_parse_iscsi_option(iscsi_url
->target
, options
);
1689 if (iscsi_url
->user
[0] != '\0') {
1690 qdict_set_default_str(options
, "user", iscsi_url
->user
);
1691 qdict_set_default_str(options
, "password", iscsi_url
->passwd
);
1694 iscsi_destroy_url(iscsi_url
);
1697 static QemuOptsList runtime_opts
= {
1699 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1702 .name
= "transport",
1703 .type
= QEMU_OPT_STRING
,
1707 .type
= QEMU_OPT_STRING
,
1711 .type
= QEMU_OPT_STRING
,
1715 .type
= QEMU_OPT_STRING
,
1719 .type
= QEMU_OPT_STRING
,
1722 .name
= "password-secret",
1723 .type
= QEMU_OPT_STRING
,
1727 .type
= QEMU_OPT_NUMBER
,
1730 .name
= "initiator-name",
1731 .type
= QEMU_OPT_STRING
,
1734 .name
= "header-digest",
1735 .type
= QEMU_OPT_STRING
,
1739 .type
= QEMU_OPT_NUMBER
,
1741 { /* end of list */ }
1745 static void iscsi_save_designator(IscsiLun
*lun
,
1746 struct scsi_inquiry_device_identification
*inq_di
)
1748 struct scsi_inquiry_device_designator
*desig
, *copy
= NULL
;
1750 for (desig
= inq_di
->designators
; desig
; desig
= desig
->next
) {
1751 if (desig
->association
||
1752 desig
->designator_type
> SCSI_DESIGNATOR_TYPE_NAA
) {
1755 /* NAA works better than T10 vendor ID based designator. */
1756 if (!copy
|| copy
->designator_type
< desig
->designator_type
) {
1761 lun
->dd
= g_new(struct scsi_inquiry_device_designator
, 1);
1763 lun
->dd
->next
= NULL
;
1764 lun
->dd
->designator
= g_malloc(copy
->designator_length
);
1765 memcpy(lun
->dd
->designator
, copy
->designator
, copy
->designator_length
);
1769 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1772 IscsiLun
*iscsilun
= bs
->opaque
;
1773 struct iscsi_context
*iscsi
= NULL
;
1774 struct scsi_task
*task
= NULL
;
1775 struct scsi_inquiry_standard
*inq
= NULL
;
1776 struct scsi_inquiry_supported_pages
*inq_vpd
;
1777 char *initiator_name
= NULL
;
1779 Error
*local_err
= NULL
;
1780 const char *transport_name
, *portal
, *target
;
1781 #if LIBISCSI_API_VERSION >= (20160603)
1782 enum iscsi_transport_type transport
;
1784 int i
, ret
= 0, timeout
= 0, lun
;
1786 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
1787 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1789 error_propagate(errp
, local_err
);
1794 transport_name
= qemu_opt_get(opts
, "transport");
1795 portal
= qemu_opt_get(opts
, "portal");
1796 target
= qemu_opt_get(opts
, "target");
1797 lun
= qemu_opt_get_number(opts
, "lun", 0);
1799 if (!transport_name
|| !portal
|| !target
) {
1800 error_setg(errp
, "Need all of transport, portal and target options");
1805 if (!strcmp(transport_name
, "tcp")) {
1806 #if LIBISCSI_API_VERSION >= (20160603)
1807 transport
= TCP_TRANSPORT
;
1808 } else if (!strcmp(transport_name
, "iser")) {
1809 transport
= ISER_TRANSPORT
;
1811 /* TCP is what older libiscsi versions always use */
1814 error_setg(errp
, "Unknown transport: %s", transport_name
);
1819 memset(iscsilun
, 0, sizeof(IscsiLun
));
1821 initiator_name
= get_initiator_name(opts
);
1823 iscsi
= iscsi_create_context(initiator_name
);
1824 if (iscsi
== NULL
) {
1825 error_setg(errp
, "iSCSI: Failed to create iSCSI context.");
1829 #if LIBISCSI_API_VERSION >= (20160603)
1830 if (iscsi_init_transport(iscsi
, transport
)) {
1831 error_setg(errp
, ("Error initializing transport."));
1836 if (iscsi_set_targetname(iscsi
, target
)) {
1837 error_setg(errp
, "iSCSI: Failed to set target name.");
1842 /* check if we got CHAP username/password via the options */
1843 apply_chap(iscsi
, opts
, &local_err
);
1844 if (local_err
!= NULL
) {
1845 error_propagate(errp
, local_err
);
1850 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1851 error_setg(errp
, "iSCSI: Failed to set session type to normal.");
1856 /* check if we got HEADER_DIGEST via the options */
1857 apply_header_digest(iscsi
, opts
, &local_err
);
1858 if (local_err
!= NULL
) {
1859 error_propagate(errp
, local_err
);
1864 /* timeout handling is broken in libiscsi before 1.15.0 */
1865 timeout
= qemu_opt_get_number(opts
, "timeout", 0);
1866 #if LIBISCSI_API_VERSION >= 20150621
1867 iscsi_set_timeout(iscsi
, timeout
);
1870 warn_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1874 if (iscsi_full_connect_sync(iscsi
, portal
, lun
) != 0) {
1875 error_setg(errp
, "iSCSI: Failed to connect to LUN : %s",
1876 iscsi_get_error(iscsi
));
1881 iscsilun
->iscsi
= iscsi
;
1882 iscsilun
->aio_context
= bdrv_get_aio_context(bs
);
1883 iscsilun
->lun
= lun
;
1884 iscsilun
->has_write_same
= true;
1886 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0,
1887 (void **) &inq
, errp
);
1892 iscsilun
->type
= inq
->periperal_device_type
;
1893 scsi_free_scsi_task(task
);
1896 iscsi_modesense_sync(iscsilun
);
1897 if (iscsilun
->dpofua
) {
1898 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1901 /* Check the write protect flag of the LUN if we want to write */
1902 if (iscsilun
->type
== TYPE_DISK
&& (flags
& BDRV_O_RDWR
) &&
1903 iscsilun
->write_protected
) {
1904 ret
= bdrv_apply_auto_read_only(bs
, "LUN is write protected", errp
);
1908 flags
&= ~BDRV_O_RDWR
;
1911 iscsi_readcapacity_sync(iscsilun
, &local_err
);
1912 if (local_err
!= NULL
) {
1913 error_propagate(errp
, local_err
);
1917 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1919 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1920 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1921 * will try to read from the device to guess the image format.
1923 if (iscsilun
->type
!= TYPE_DISK
&& iscsilun
->type
!= TYPE_ROM
) {
1927 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1928 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES
,
1929 (void **) &inq_vpd
, errp
);
1934 for (i
= 0; i
< inq_vpd
->num_pages
; i
++) {
1935 struct scsi_task
*inq_task
;
1936 struct scsi_inquiry_logical_block_provisioning
*inq_lbp
;
1937 struct scsi_inquiry_block_limits
*inq_bl
;
1938 struct scsi_inquiry_device_identification
*inq_di
;
1939 switch (inq_vpd
->pages
[i
]) {
1940 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
:
1941 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1942 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
,
1943 (void **) &inq_lbp
, errp
);
1944 if (inq_task
== NULL
) {
1948 memcpy(&iscsilun
->lbp
, inq_lbp
,
1949 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1950 scsi_free_scsi_task(inq_task
);
1952 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
:
1953 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1954 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
,
1955 (void **) &inq_bl
, errp
);
1956 if (inq_task
== NULL
) {
1960 memcpy(&iscsilun
->bl
, inq_bl
,
1961 sizeof(struct scsi_inquiry_block_limits
));
1962 scsi_free_scsi_task(inq_task
);
1964 case SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION
:
1965 inq_task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1966 SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION
,
1967 (void **) &inq_di
, errp
);
1968 if (inq_task
== NULL
) {
1972 iscsi_save_designator(iscsilun
, inq_di
);
1973 scsi_free_scsi_task(inq_task
);
1979 scsi_free_scsi_task(task
);
1982 qemu_mutex_init(&iscsilun
->mutex
);
1983 iscsi_attach_aio_context(bs
, iscsilun
->aio_context
);
1985 /* Guess the internal cluster (page) size of the iscsi target by the means
1986 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1987 * reasonable size */
1988 if (iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
>= 4 * 1024 &&
1989 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
<= 16 * 1024 * 1024) {
1990 iscsilun
->cluster_size
= iscsilun
->bl
.opt_unmap_gran
*
1991 iscsilun
->block_size
;
1992 if (iscsilun
->lbprz
) {
1993 ret
= iscsi_allocmap_init(iscsilun
, bs
->open_flags
);
1997 if (iscsilun
->lbprz
&& iscsilun
->lbp
.lbpws
) {
1998 bs
->supported_zero_flags
= BDRV_REQ_MAY_UNMAP
;
2002 qemu_opts_del(opts
);
2003 g_free(initiator_name
);
2005 scsi_free_scsi_task(task
);
2009 if (iscsi
!= NULL
) {
2010 if (iscsi_is_logged_in(iscsi
)) {
2011 iscsi_logout_sync(iscsi
);
2013 iscsi_destroy_context(iscsi
);
2015 memset(iscsilun
, 0, sizeof(IscsiLun
));
2021 static void iscsi_close(BlockDriverState
*bs
)
2023 IscsiLun
*iscsilun
= bs
->opaque
;
2024 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
2026 iscsi_detach_aio_context(bs
);
2027 if (iscsi_is_logged_in(iscsi
)) {
2028 iscsi_logout_sync(iscsi
);
2030 iscsi_destroy_context(iscsi
);
2032 g_free(iscsilun
->dd
->designator
);
2033 g_free(iscsilun
->dd
);
2035 g_free(iscsilun
->zeroblock
);
2036 iscsi_allocmap_free(iscsilun
);
2037 qemu_mutex_destroy(&iscsilun
->mutex
);
2038 memset(iscsilun
, 0, sizeof(IscsiLun
));
2041 static void iscsi_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
2043 /* We don't actually refresh here, but just return data queried in
2044 * iscsi_open(): iscsi targets don't change their limits. */
2046 IscsiLun
*iscsilun
= bs
->opaque
;
2047 uint64_t max_xfer_len
= iscsilun
->use_16_for_rw
? 0xffffffff : 0xffff;
2048 unsigned int block_size
= MAX(BDRV_SECTOR_SIZE
, iscsilun
->block_size
);
2050 assert(iscsilun
->block_size
>= BDRV_SECTOR_SIZE
|| bs
->sg
);
2052 bs
->bl
.request_alignment
= block_size
;
2054 if (iscsilun
->bl
.max_xfer_len
) {
2055 max_xfer_len
= MIN(max_xfer_len
, iscsilun
->bl
.max_xfer_len
);
2058 if (max_xfer_len
* block_size
< INT_MAX
) {
2059 bs
->bl
.max_transfer
= max_xfer_len
* iscsilun
->block_size
;
2062 if (iscsilun
->lbp
.lbpu
) {
2063 if (iscsilun
->bl
.max_unmap
< 0xffffffff / block_size
) {
2064 bs
->bl
.max_pdiscard
=
2065 iscsilun
->bl
.max_unmap
* iscsilun
->block_size
;
2067 bs
->bl
.pdiscard_alignment
=
2068 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
2070 bs
->bl
.pdiscard_alignment
= iscsilun
->block_size
;
2073 if (iscsilun
->bl
.max_ws_len
< 0xffffffff / block_size
) {
2074 bs
->bl
.max_pwrite_zeroes
=
2075 iscsilun
->bl
.max_ws_len
* iscsilun
->block_size
;
2077 if (iscsilun
->lbp
.lbpws
) {
2078 bs
->bl
.pwrite_zeroes_alignment
=
2079 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
2081 bs
->bl
.pwrite_zeroes_alignment
= iscsilun
->block_size
;
2083 if (iscsilun
->bl
.opt_xfer_len
&&
2084 iscsilun
->bl
.opt_xfer_len
< INT_MAX
/ block_size
) {
2085 bs
->bl
.opt_transfer
= pow2floor(iscsilun
->bl
.opt_xfer_len
*
2086 iscsilun
->block_size
);
2090 /* Note that this will not re-establish a connection with an iSCSI target - it
2091 * is effectively a NOP. */
2092 static int iscsi_reopen_prepare(BDRVReopenState
*state
,
2093 BlockReopenQueue
*queue
, Error
**errp
)
2095 IscsiLun
*iscsilun
= state
->bs
->opaque
;
2097 if (state
->flags
& BDRV_O_RDWR
&& iscsilun
->write_protected
) {
2098 error_setg(errp
, "Cannot open a write protected LUN as read-write");
2104 static void iscsi_reopen_commit(BDRVReopenState
*reopen_state
)
2106 IscsiLun
*iscsilun
= reopen_state
->bs
->opaque
;
2108 /* the cache.direct status might have changed */
2109 if (iscsilun
->allocmap
!= NULL
) {
2110 iscsi_allocmap_init(iscsilun
, reopen_state
->flags
);
2114 static int coroutine_fn
iscsi_co_truncate(BlockDriverState
*bs
, int64_t offset
,
2115 PreallocMode prealloc
, Error
**errp
)
2117 IscsiLun
*iscsilun
= bs
->opaque
;
2118 Error
*local_err
= NULL
;
2120 if (prealloc
!= PREALLOC_MODE_OFF
) {
2121 error_setg(errp
, "Unsupported preallocation mode '%s'",
2122 PreallocMode_str(prealloc
));
2126 if (iscsilun
->type
!= TYPE_DISK
) {
2127 error_setg(errp
, "Cannot resize non-disk iSCSI devices");
2131 iscsi_readcapacity_sync(iscsilun
, &local_err
);
2132 if (local_err
!= NULL
) {
2133 error_propagate(errp
, local_err
);
2137 if (offset
> iscsi_getlength(bs
)) {
2138 error_setg(errp
, "Cannot grow iSCSI devices");
2142 if (iscsilun
->allocmap
!= NULL
) {
2143 iscsi_allocmap_init(iscsilun
, bs
->open_flags
);
2149 static int coroutine_fn
iscsi_co_create_opts(const char *filename
, QemuOpts
*opts
,
2153 int64_t total_size
= 0;
2154 BlockDriverState
*bs
;
2155 IscsiLun
*iscsilun
= NULL
;
2157 Error
*local_err
= NULL
;
2161 /* Read out options */
2162 total_size
= DIV_ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
2164 bs
->opaque
= g_new0(struct IscsiLun
, 1);
2165 iscsilun
= bs
->opaque
;
2167 bs_options
= qdict_new();
2168 iscsi_parse_filename(filename
, bs_options
, &local_err
);
2170 error_propagate(errp
, local_err
);
2173 ret
= iscsi_open(bs
, bs_options
, 0, NULL
);
2175 qobject_unref(bs_options
);
2180 iscsi_detach_aio_context(bs
);
2181 if (iscsilun
->type
!= TYPE_DISK
) {
2185 if (bs
->total_sectors
< total_size
) {
2192 if (iscsilun
->iscsi
!= NULL
) {
2193 iscsi_destroy_context(iscsilun
->iscsi
);
2201 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
2203 IscsiLun
*iscsilun
= bs
->opaque
;
2204 bdi
->unallocated_blocks_are_zero
= iscsilun
->lbprz
;
2205 bdi
->cluster_size
= iscsilun
->cluster_size
;
2209 static void coroutine_fn
iscsi_co_invalidate_cache(BlockDriverState
*bs
,
2212 IscsiLun
*iscsilun
= bs
->opaque
;
2213 iscsi_allocmap_invalidate(iscsilun
);
2216 static int coroutine_fn
iscsi_co_copy_range_from(BlockDriverState
*bs
,
2218 uint64_t src_offset
,
2220 uint64_t dst_offset
,
2222 BdrvRequestFlags read_flags
,
2223 BdrvRequestFlags write_flags
)
2225 return bdrv_co_copy_range_to(src
, src_offset
, dst
, dst_offset
, bytes
,
2226 read_flags
, write_flags
);
2229 static struct scsi_task
*iscsi_xcopy_task(int param_len
)
2231 struct scsi_task
*task
;
2233 task
= g_new0(struct scsi_task
, 1);
2235 task
->cdb
[0] = EXTENDED_COPY
;
2236 task
->cdb
[10] = (param_len
>> 24) & 0xFF;
2237 task
->cdb
[11] = (param_len
>> 16) & 0xFF;
2238 task
->cdb
[12] = (param_len
>> 8) & 0xFF;
2239 task
->cdb
[13] = param_len
& 0xFF;
2240 task
->cdb_size
= 16;
2241 task
->xfer_dir
= SCSI_XFER_WRITE
;
2242 task
->expxferlen
= param_len
;
2247 static void iscsi_populate_target_desc(unsigned char *desc
, IscsiLun
*lun
)
2249 struct scsi_inquiry_device_designator
*dd
= lun
->dd
;
2251 memset(desc
, 0, 32);
2252 desc
[0] = 0xE4; /* IDENT_DESCR_TGT_DESCR */
2253 desc
[4] = dd
->code_set
;
2254 desc
[5] = (dd
->designator_type
& 0xF)
2255 | ((dd
->association
& 3) << 4);
2256 desc
[7] = dd
->designator_length
;
2257 memcpy(desc
+ 8, dd
->designator
, MIN(dd
->designator_length
, 20));
2260 desc
[29] = (lun
->block_size
>> 16) & 0xFF;
2261 desc
[30] = (lun
->block_size
>> 8) & 0xFF;
2262 desc
[31] = lun
->block_size
& 0xFF;
2265 static void iscsi_xcopy_desc_hdr(uint8_t *hdr
, int dc
, int cat
, int src_index
,
2268 hdr
[0] = 0x02; /* BLK_TO_BLK_SEG_DESCR */
2269 hdr
[1] = ((dc
<< 1) | cat
) & 0xFF;
2270 hdr
[2] = (XCOPY_BLK2BLK_SEG_DESC_SIZE
>> 8) & 0xFF;
2271 /* don't account for the first 4 bytes in descriptor header*/
2272 hdr
[3] = (XCOPY_BLK2BLK_SEG_DESC_SIZE
- 4 /* SEG_DESC_SRC_INDEX_OFFSET */) & 0xFF;
2273 hdr
[4] = (src_index
>> 8) & 0xFF;
2274 hdr
[5] = src_index
& 0xFF;
2275 hdr
[6] = (dst_index
>> 8) & 0xFF;
2276 hdr
[7] = dst_index
& 0xFF;
2279 static void iscsi_xcopy_populate_desc(uint8_t *desc
, int dc
, int cat
,
2280 int src_index
, int dst_index
, int num_blks
,
2281 uint64_t src_lba
, uint64_t dst_lba
)
2283 iscsi_xcopy_desc_hdr(desc
, dc
, cat
, src_index
, dst_index
);
2285 /* The caller should verify the request size */
2286 assert(num_blks
< 65536);
2287 desc
[10] = (num_blks
>> 8) & 0xFF;
2288 desc
[11] = num_blks
& 0xFF;
2289 desc
[12] = (src_lba
>> 56) & 0xFF;
2290 desc
[13] = (src_lba
>> 48) & 0xFF;
2291 desc
[14] = (src_lba
>> 40) & 0xFF;
2292 desc
[15] = (src_lba
>> 32) & 0xFF;
2293 desc
[16] = (src_lba
>> 24) & 0xFF;
2294 desc
[17] = (src_lba
>> 16) & 0xFF;
2295 desc
[18] = (src_lba
>> 8) & 0xFF;
2296 desc
[19] = src_lba
& 0xFF;
2297 desc
[20] = (dst_lba
>> 56) & 0xFF;
2298 desc
[21] = (dst_lba
>> 48) & 0xFF;
2299 desc
[22] = (dst_lba
>> 40) & 0xFF;
2300 desc
[23] = (dst_lba
>> 32) & 0xFF;
2301 desc
[24] = (dst_lba
>> 24) & 0xFF;
2302 desc
[25] = (dst_lba
>> 16) & 0xFF;
2303 desc
[26] = (dst_lba
>> 8) & 0xFF;
2304 desc
[27] = dst_lba
& 0xFF;
2307 static void iscsi_xcopy_populate_header(unsigned char *buf
, int list_id
, int str
,
2308 int list_id_usage
, int prio
,
2310 int seg_desc_len
, int inline_data_len
)
2313 buf
[1] = ((str
& 1) << 5) | ((list_id_usage
& 3) << 3) | (prio
& 7);
2314 buf
[2] = (tgt_desc_len
>> 8) & 0xFF;
2315 buf
[3] = tgt_desc_len
& 0xFF;
2316 buf
[8] = (seg_desc_len
>> 24) & 0xFF;
2317 buf
[9] = (seg_desc_len
>> 16) & 0xFF;
2318 buf
[10] = (seg_desc_len
>> 8) & 0xFF;
2319 buf
[11] = seg_desc_len
& 0xFF;
2320 buf
[12] = (inline_data_len
>> 24) & 0xFF;
2321 buf
[13] = (inline_data_len
>> 16) & 0xFF;
2322 buf
[14] = (inline_data_len
>> 8) & 0xFF;
2323 buf
[15] = inline_data_len
& 0xFF;
2326 static void iscsi_xcopy_data(struct iscsi_data
*data
,
2327 IscsiLun
*src
, int64_t src_lba
,
2328 IscsiLun
*dst
, int64_t dst_lba
,
2329 uint16_t num_blocks
)
2332 const int src_offset
= XCOPY_DESC_OFFSET
;
2333 const int dst_offset
= XCOPY_DESC_OFFSET
+ IDENT_DESCR_TGT_DESCR_SIZE
;
2334 const int seg_offset
= dst_offset
+ IDENT_DESCR_TGT_DESCR_SIZE
;
2336 data
->size
= XCOPY_DESC_OFFSET
+
2337 IDENT_DESCR_TGT_DESCR_SIZE
* 2 +
2338 XCOPY_BLK2BLK_SEG_DESC_SIZE
;
2339 data
->data
= g_malloc0(data
->size
);
2342 /* Initialise the parameter list header */
2343 iscsi_xcopy_populate_header(buf
, 1, 0, 2 /* LIST_ID_USAGE_DISCARD */,
2344 0, 2 * IDENT_DESCR_TGT_DESCR_SIZE
,
2345 XCOPY_BLK2BLK_SEG_DESC_SIZE
,
2348 /* Initialise CSCD list with one src + one dst descriptor */
2349 iscsi_populate_target_desc(&buf
[src_offset
], src
);
2350 iscsi_populate_target_desc(&buf
[dst_offset
], dst
);
2352 /* Initialise one segment descriptor */
2353 iscsi_xcopy_populate_desc(&buf
[seg_offset
], 0, 0, 0, 1, num_blocks
,
2357 static int coroutine_fn
iscsi_co_copy_range_to(BlockDriverState
*bs
,
2359 uint64_t src_offset
,
2361 uint64_t dst_offset
,
2363 BdrvRequestFlags read_flags
,
2364 BdrvRequestFlags write_flags
)
2366 IscsiLun
*dst_lun
= dst
->bs
->opaque
;
2368 struct IscsiTask iscsi_task
;
2369 struct iscsi_data data
;
2373 if (src
->bs
->drv
->bdrv_co_copy_range_to
!= iscsi_co_copy_range_to
) {
2376 src_lun
= src
->bs
->opaque
;
2378 if (!src_lun
->dd
|| !dst_lun
->dd
) {
2381 if (!is_byte_request_lun_aligned(dst_offset
, bytes
, dst_lun
)) {
2384 if (!is_byte_request_lun_aligned(src_offset
, bytes
, src_lun
)) {
2387 if (dst_lun
->block_size
!= src_lun
->block_size
||
2388 !dst_lun
->block_size
) {
2392 block_size
= dst_lun
->block_size
;
2393 if (bytes
/ block_size
> 65535) {
2397 iscsi_xcopy_data(&data
,
2398 src_lun
, src_offset
/ block_size
,
2399 dst_lun
, dst_offset
/ block_size
,
2400 bytes
/ block_size
);
2402 iscsi_co_init_iscsitask(dst_lun
, &iscsi_task
);
2404 qemu_mutex_lock(&dst_lun
->mutex
);
2405 iscsi_task
.task
= iscsi_xcopy_task(data
.size
);
2407 if (iscsi_scsi_command_async(dst_lun
->iscsi
, dst_lun
->lun
,
2408 iscsi_task
.task
, iscsi_co_generic_cb
,
2410 &iscsi_task
) != 0) {
2415 iscsi_co_wait_for_task(&iscsi_task
, dst_lun
);
2417 if (iscsi_task
.do_retry
) {
2418 iscsi_task
.complete
= 0;
2422 if (iscsi_task
.status
!= SCSI_STATUS_GOOD
) {
2423 r
= iscsi_task
.err_code
;
2429 trace_iscsi_xcopy(src_lun
, src_offset
, dst_lun
, dst_offset
, bytes
, r
);
2430 g_free(iscsi_task
.task
);
2431 qemu_mutex_unlock(&dst_lun
->mutex
);
2432 g_free(iscsi_task
.err_str
);
2436 static QemuOptsList iscsi_create_opts
= {
2437 .name
= "iscsi-create-opts",
2438 .head
= QTAILQ_HEAD_INITIALIZER(iscsi_create_opts
.head
),
2441 .name
= BLOCK_OPT_SIZE
,
2442 .type
= QEMU_OPT_SIZE
,
2443 .help
= "Virtual disk size"
2445 { /* end of list */ }
2449 static BlockDriver bdrv_iscsi
= {
2450 .format_name
= "iscsi",
2451 .protocol_name
= "iscsi",
2453 .instance_size
= sizeof(IscsiLun
),
2454 .bdrv_parse_filename
= iscsi_parse_filename
,
2455 .bdrv_file_open
= iscsi_open
,
2456 .bdrv_close
= iscsi_close
,
2457 .bdrv_co_create_opts
= iscsi_co_create_opts
,
2458 .create_opts
= &iscsi_create_opts
,
2459 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
2460 .bdrv_reopen_commit
= iscsi_reopen_commit
,
2461 .bdrv_co_invalidate_cache
= iscsi_co_invalidate_cache
,
2463 .bdrv_getlength
= iscsi_getlength
,
2464 .bdrv_get_info
= iscsi_get_info
,
2465 .bdrv_co_truncate
= iscsi_co_truncate
,
2466 .bdrv_refresh_limits
= iscsi_refresh_limits
,
2468 .bdrv_co_block_status
= iscsi_co_block_status
,
2469 .bdrv_co_pdiscard
= iscsi_co_pdiscard
,
2470 .bdrv_co_copy_range_from
= iscsi_co_copy_range_from
,
2471 .bdrv_co_copy_range_to
= iscsi_co_copy_range_to
,
2472 .bdrv_co_pwrite_zeroes
= iscsi_co_pwrite_zeroes
,
2473 .bdrv_co_readv
= iscsi_co_readv
,
2474 .bdrv_co_writev
= iscsi_co_writev
,
2475 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
2478 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
2481 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
2482 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
2485 #if LIBISCSI_API_VERSION >= (20160603)
2486 static BlockDriver bdrv_iser
= {
2487 .format_name
= "iser",
2488 .protocol_name
= "iser",
2490 .instance_size
= sizeof(IscsiLun
),
2491 .bdrv_parse_filename
= iscsi_parse_filename
,
2492 .bdrv_file_open
= iscsi_open
,
2493 .bdrv_close
= iscsi_close
,
2494 .bdrv_co_create_opts
= iscsi_co_create_opts
,
2495 .create_opts
= &iscsi_create_opts
,
2496 .bdrv_reopen_prepare
= iscsi_reopen_prepare
,
2497 .bdrv_reopen_commit
= iscsi_reopen_commit
,
2498 .bdrv_co_invalidate_cache
= iscsi_co_invalidate_cache
,
2500 .bdrv_getlength
= iscsi_getlength
,
2501 .bdrv_get_info
= iscsi_get_info
,
2502 .bdrv_co_truncate
= iscsi_co_truncate
,
2503 .bdrv_refresh_limits
= iscsi_refresh_limits
,
2505 .bdrv_co_block_status
= iscsi_co_block_status
,
2506 .bdrv_co_pdiscard
= iscsi_co_pdiscard
,
2507 .bdrv_co_copy_range_from
= iscsi_co_copy_range_from
,
2508 .bdrv_co_copy_range_to
= iscsi_co_copy_range_to
,
2509 .bdrv_co_pwrite_zeroes
= iscsi_co_pwrite_zeroes
,
2510 .bdrv_co_readv
= iscsi_co_readv
,
2511 .bdrv_co_writev
= iscsi_co_writev
,
2512 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
2515 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
2518 .bdrv_detach_aio_context
= iscsi_detach_aio_context
,
2519 .bdrv_attach_aio_context
= iscsi_attach_aio_context
,
2523 static void iscsi_block_init(void)
2525 bdrv_register(&bdrv_iscsi
);
2526 #if LIBISCSI_API_VERSION >= (20160603)
2527 bdrv_register(&bdrv_iser
);
2531 block_init(iscsi_block_init
);