tpm: use loop iterator to set sts data field
[qemu/ar7.git] / block / iscsi.c
blob727dee50bf8a73de0f314d3c95fb73f1c913fa37
1 /*
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
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
28 #include <poll.h>
29 #include <math.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"
38 #include "qemu/iov.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"
47 #include "trace.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>
53 #undef SCSI_XFER_NONE
54 QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE != (int)ISCSI_XFER_NONE);
56 #ifdef __linux__
57 #include <scsi/sg.h>
58 #endif
60 typedef struct IscsiLun {
61 struct iscsi_context *iscsi;
62 AioContext *aio_context;
63 int lun;
64 enum scsi_inquiry_peripheral_device_type type;
65 int block_size;
66 uint64_t num_blocks;
67 int events;
68 QEMUTimer *nop_timer;
69 QEMUTimer *event_timer;
70 QemuMutex mutex;
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;
91 long allocmap_size;
92 int cluster_size;
93 bool use_16_for_rw;
94 bool write_protected;
95 bool lbpme;
96 bool lbprz;
97 bool dpofua;
98 bool has_write_same;
99 bool request_timed_out;
100 } IscsiLun;
102 typedef struct IscsiTask {
103 int status;
104 int complete;
105 int retries;
106 int do_retry;
107 struct scsi_task *task;
108 Coroutine *co;
109 IscsiLun *iscsilun;
110 QEMUTimer retry_timer;
111 int err_code;
112 char *err_str;
113 } IscsiTask;
115 typedef struct IscsiAIOCB {
116 BlockAIOCB common;
117 QEMUBH *bh;
118 IscsiLun *iscsilun;
119 struct scsi_task *task;
120 uint8_t *buf;
121 int status;
122 int64_t sector_num;
123 int nb_sectors;
124 int ret;
125 #ifdef __linux__
126 sg_io_hdr_t *ioh;
127 #endif
128 } IscsiAIOCB;
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
143 * unallocated. */
144 #define ISCSI_CHECKALLOC_THRES 64
146 static void
147 iscsi_bh_cb(void *p)
149 IscsiAIOCB *acb = p;
151 qemu_bh_delete(acb->bh);
153 g_free(acb->buf);
154 acb->buf = NULL;
156 acb->common.cb(acb->common.opaque, acb->status);
158 if (acb->task != NULL) {
159 scsi_free_scsi_task(acb->task);
160 acb->task = NULL;
163 qemu_aio_unref(acb);
166 static void
167 iscsi_schedule_bh(IscsiAIOCB *acb)
169 if (acb->bh) {
170 return;
172 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
173 qemu_bh_schedule(acb->bh);
176 static void iscsi_co_generic_bh_cb(void *opaque)
178 struct IscsiTask *iTask = opaque;
180 iTask->complete = 1;
181 aio_co_wake(iTask->co);
184 static void iscsi_retry_timer_expired(void *opaque)
186 struct IscsiTask *iTask = opaque;
187 iTask->complete = 1;
188 if (iTask->co) {
189 aio_co_wake(iTask->co);
193 static inline unsigned exp_random(double mean)
195 return -mean * log((double)rand() / RAND_MAX);
198 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
199 * libiscsi 1.10.0, together with other constants we need. Use it as
200 * a hint that we have to define them ourselves if needed, to keep the
201 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
202 * the test because SCSI_STATUS_* is an enum.
204 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
205 * an enum, check against the LIBISCSI_API_VERSION macro, which was
206 * introduced in 1.11.0. If it is present, there is no need to define
207 * anything.
209 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
210 !defined(LIBISCSI_API_VERSION)
211 #define SCSI_STATUS_TASK_SET_FULL 0x28
212 #define SCSI_STATUS_TIMEOUT 0x0f000002
213 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
214 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
215 #endif
217 #ifndef LIBISCSI_API_VERSION
218 #define LIBISCSI_API_VERSION 20130701
219 #endif
221 static int iscsi_translate_sense(struct scsi_sense *sense)
223 return - scsi_sense_to_errno(sense->key,
224 (sense->ascq & 0xFF00) >> 8,
225 sense->ascq & 0xFF);
228 /* Called (via iscsi_service) with QemuMutex held. */
229 static void
230 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
231 void *command_data, void *opaque)
233 struct IscsiTask *iTask = opaque;
234 struct scsi_task *task = command_data;
236 iTask->status = status;
237 iTask->do_retry = 0;
238 iTask->task = task;
240 if (status != SCSI_STATUS_GOOD) {
241 if (iTask->retries++ < ISCSI_CMD_RETRIES) {
242 if (status == SCSI_STATUS_CHECK_CONDITION
243 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
244 error_report("iSCSI CheckCondition: %s",
245 iscsi_get_error(iscsi));
246 iTask->do_retry = 1;
247 goto out;
249 if (status == SCSI_STATUS_BUSY ||
250 status == SCSI_STATUS_TIMEOUT ||
251 status == SCSI_STATUS_TASK_SET_FULL) {
252 unsigned retry_time =
253 exp_random(iscsi_retry_times[iTask->retries - 1]);
254 if (status == SCSI_STATUS_TIMEOUT) {
255 /* make sure the request is rescheduled AFTER the
256 * reconnect is initiated */
257 retry_time = EVENT_INTERVAL * 2;
258 iTask->iscsilun->request_timed_out = true;
260 error_report("iSCSI Busy/TaskSetFull/TimeOut"
261 " (retry #%u in %u ms): %s",
262 iTask->retries, retry_time,
263 iscsi_get_error(iscsi));
264 aio_timer_init(iTask->iscsilun->aio_context,
265 &iTask->retry_timer, QEMU_CLOCK_REALTIME,
266 SCALE_MS, iscsi_retry_timer_expired, iTask);
267 timer_mod(&iTask->retry_timer,
268 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time);
269 iTask->do_retry = 1;
270 return;
273 iTask->err_code = iscsi_translate_sense(&task->sense);
274 iTask->err_str = g_strdup(iscsi_get_error(iscsi));
277 out:
278 if (iTask->co) {
279 aio_bh_schedule_oneshot(iTask->iscsilun->aio_context,
280 iscsi_co_generic_bh_cb, iTask);
281 } else {
282 iTask->complete = 1;
286 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
288 *iTask = (struct IscsiTask) {
289 .co = qemu_coroutine_self(),
290 .iscsilun = iscsilun,
294 static void
295 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
296 void *private_data)
298 IscsiAIOCB *acb = private_data;
300 acb->status = -ECANCELED;
301 iscsi_schedule_bh(acb);
304 static void
305 iscsi_aio_cancel(BlockAIOCB *blockacb)
307 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
308 IscsiLun *iscsilun = acb->iscsilun;
310 if (acb->status != -EINPROGRESS) {
311 return;
314 /* send a task mgmt call to the target to cancel the task on the target */
315 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
316 iscsi_abort_task_cb, acb);
320 static const AIOCBInfo iscsi_aiocb_info = {
321 .aiocb_size = sizeof(IscsiAIOCB),
322 .cancel_async = iscsi_aio_cancel,
326 static void iscsi_process_read(void *arg);
327 static void iscsi_process_write(void *arg);
329 /* Called with QemuMutex held. */
330 static void
331 iscsi_set_events(IscsiLun *iscsilun)
333 struct iscsi_context *iscsi = iscsilun->iscsi;
334 int ev = iscsi_which_events(iscsi);
336 if (ev != iscsilun->events) {
337 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi),
338 false,
339 (ev & POLLIN) ? iscsi_process_read : NULL,
340 (ev & POLLOUT) ? iscsi_process_write : NULL,
341 NULL,
342 iscsilun);
343 iscsilun->events = ev;
347 static void iscsi_timed_check_events(void *opaque)
349 IscsiLun *iscsilun = opaque;
351 /* check for timed out requests */
352 iscsi_service(iscsilun->iscsi, 0);
354 if (iscsilun->request_timed_out) {
355 iscsilun->request_timed_out = false;
356 iscsi_reconnect(iscsilun->iscsi);
359 /* newer versions of libiscsi may return zero events. Ensure we are able
360 * to return to service once this situation changes. */
361 iscsi_set_events(iscsilun);
363 timer_mod(iscsilun->event_timer,
364 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
367 static void
368 iscsi_process_read(void *arg)
370 IscsiLun *iscsilun = arg;
371 struct iscsi_context *iscsi = iscsilun->iscsi;
373 qemu_mutex_lock(&iscsilun->mutex);
374 iscsi_service(iscsi, POLLIN);
375 iscsi_set_events(iscsilun);
376 qemu_mutex_unlock(&iscsilun->mutex);
379 static void
380 iscsi_process_write(void *arg)
382 IscsiLun *iscsilun = arg;
383 struct iscsi_context *iscsi = iscsilun->iscsi;
385 qemu_mutex_lock(&iscsilun->mutex);
386 iscsi_service(iscsi, POLLOUT);
387 iscsi_set_events(iscsilun);
388 qemu_mutex_unlock(&iscsilun->mutex);
391 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
393 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
396 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
398 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
401 static bool is_byte_request_lun_aligned(int64_t offset, int count,
402 IscsiLun *iscsilun)
404 if (offset % iscsilun->block_size || count % iscsilun->block_size) {
405 error_report("iSCSI misaligned request: "
406 "iscsilun->block_size %u, offset %" PRIi64
407 ", count %d",
408 iscsilun->block_size, offset, count);
409 return false;
411 return true;
414 static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors,
415 IscsiLun *iscsilun)
417 assert(nb_sectors <= BDRV_REQUEST_MAX_SECTORS);
418 return is_byte_request_lun_aligned(sector_num << BDRV_SECTOR_BITS,
419 nb_sectors << BDRV_SECTOR_BITS,
420 iscsilun);
423 static void iscsi_allocmap_free(IscsiLun *iscsilun)
425 g_free(iscsilun->allocmap);
426 g_free(iscsilun->allocmap_valid);
427 iscsilun->allocmap = NULL;
428 iscsilun->allocmap_valid = NULL;
432 static int iscsi_allocmap_init(IscsiLun *iscsilun, int open_flags)
434 iscsi_allocmap_free(iscsilun);
436 assert(iscsilun->cluster_size);
437 iscsilun->allocmap_size =
438 DIV_ROUND_UP(iscsilun->num_blocks * iscsilun->block_size,
439 iscsilun->cluster_size);
441 iscsilun->allocmap = bitmap_try_new(iscsilun->allocmap_size);
442 if (!iscsilun->allocmap) {
443 return -ENOMEM;
446 if (open_flags & BDRV_O_NOCACHE) {
447 /* when cache.direct = on all allocmap entries are
448 * treated as invalid to force a relookup of the block
449 * status on every read request */
450 return 0;
453 iscsilun->allocmap_valid = bitmap_try_new(iscsilun->allocmap_size);
454 if (!iscsilun->allocmap_valid) {
455 /* if we are under memory pressure free the allocmap as well */
456 iscsi_allocmap_free(iscsilun);
457 return -ENOMEM;
460 return 0;
463 static void
464 iscsi_allocmap_update(IscsiLun *iscsilun, int64_t offset,
465 int64_t bytes, bool allocated, bool valid)
467 int64_t cl_num_expanded, nb_cls_expanded, cl_num_shrunk, nb_cls_shrunk;
469 if (iscsilun->allocmap == NULL) {
470 return;
472 /* expand to entirely contain all affected clusters */
473 assert(iscsilun->cluster_size);
474 cl_num_expanded = offset / iscsilun->cluster_size;
475 nb_cls_expanded = DIV_ROUND_UP(offset + bytes,
476 iscsilun->cluster_size) - cl_num_expanded;
477 /* shrink to touch only completely contained clusters */
478 cl_num_shrunk = DIV_ROUND_UP(offset, iscsilun->cluster_size);
479 nb_cls_shrunk = (offset + bytes) / iscsilun->cluster_size - cl_num_shrunk;
480 if (allocated) {
481 bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded);
482 } else {
483 if (nb_cls_shrunk > 0) {
484 bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
488 if (iscsilun->allocmap_valid == NULL) {
489 return;
491 if (valid) {
492 if (nb_cls_shrunk > 0) {
493 bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
495 } else {
496 bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded,
497 nb_cls_expanded);
501 static void
502 iscsi_allocmap_set_allocated(IscsiLun *iscsilun, int64_t offset,
503 int64_t bytes)
505 iscsi_allocmap_update(iscsilun, offset, bytes, true, true);
508 static void
509 iscsi_allocmap_set_unallocated(IscsiLun *iscsilun, int64_t offset,
510 int64_t bytes)
512 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
513 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
515 iscsi_allocmap_update(iscsilun, offset, bytes, false, true);
518 static void iscsi_allocmap_set_invalid(IscsiLun *iscsilun, int64_t offset,
519 int64_t bytes)
521 iscsi_allocmap_update(iscsilun, offset, bytes, false, false);
524 static void iscsi_allocmap_invalidate(IscsiLun *iscsilun)
526 if (iscsilun->allocmap) {
527 bitmap_zero(iscsilun->allocmap, iscsilun->allocmap_size);
529 if (iscsilun->allocmap_valid) {
530 bitmap_zero(iscsilun->allocmap_valid, iscsilun->allocmap_size);
534 static inline bool
535 iscsi_allocmap_is_allocated(IscsiLun *iscsilun, int64_t offset,
536 int64_t bytes)
538 unsigned long size;
539 if (iscsilun->allocmap == NULL) {
540 return true;
542 assert(iscsilun->cluster_size);
543 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size);
544 return !(find_next_bit(iscsilun->allocmap, size,
545 offset / iscsilun->cluster_size) == size);
548 static inline bool iscsi_allocmap_is_valid(IscsiLun *iscsilun,
549 int64_t offset, int64_t bytes)
551 unsigned long size;
552 if (iscsilun->allocmap_valid == NULL) {
553 return false;
555 assert(iscsilun->cluster_size);
556 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size);
557 return (find_next_zero_bit(iscsilun->allocmap_valid, size,
558 offset / iscsilun->cluster_size) == size);
561 static void coroutine_fn iscsi_co_wait_for_task(IscsiTask *iTask,
562 IscsiLun *iscsilun)
564 while (!iTask->complete) {
565 iscsi_set_events(iscsilun);
566 qemu_mutex_unlock(&iscsilun->mutex);
567 qemu_coroutine_yield();
568 qemu_mutex_lock(&iscsilun->mutex);
572 static int coroutine_fn
573 iscsi_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
574 QEMUIOVector *iov, int flags)
576 IscsiLun *iscsilun = bs->opaque;
577 struct IscsiTask iTask;
578 uint64_t lba;
579 uint32_t num_sectors;
580 bool fua = flags & BDRV_REQ_FUA;
581 int r = 0;
583 if (fua) {
584 assert(iscsilun->dpofua);
586 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
587 return -EINVAL;
590 if (bs->bl.max_transfer) {
591 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
594 lba = sector_qemu2lun(sector_num, iscsilun);
595 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
596 iscsi_co_init_iscsitask(iscsilun, &iTask);
597 qemu_mutex_lock(&iscsilun->mutex);
598 retry:
599 if (iscsilun->use_16_for_rw) {
600 #if LIBISCSI_API_VERSION >= (20160603)
601 iTask.task = iscsi_write16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
602 NULL, num_sectors * iscsilun->block_size,
603 iscsilun->block_size, 0, 0, fua, 0, 0,
604 iscsi_co_generic_cb, &iTask,
605 (struct scsi_iovec *)iov->iov, iov->niov);
606 } else {
607 iTask.task = iscsi_write10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
608 NULL, num_sectors * iscsilun->block_size,
609 iscsilun->block_size, 0, 0, fua, 0, 0,
610 iscsi_co_generic_cb, &iTask,
611 (struct scsi_iovec *)iov->iov, iov->niov);
613 #else
614 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
615 NULL, num_sectors * iscsilun->block_size,
616 iscsilun->block_size, 0, 0, fua, 0, 0,
617 iscsi_co_generic_cb, &iTask);
618 } else {
619 iTask.task = iscsi_write10_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);
624 #endif
625 if (iTask.task == NULL) {
626 qemu_mutex_unlock(&iscsilun->mutex);
627 return -ENOMEM;
629 #if LIBISCSI_API_VERSION < (20160603)
630 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
631 iov->niov);
632 #endif
633 iscsi_co_wait_for_task(&iTask, iscsilun);
635 if (iTask.task != NULL) {
636 scsi_free_scsi_task(iTask.task);
637 iTask.task = NULL;
640 if (iTask.do_retry) {
641 iTask.complete = 0;
642 goto retry;
645 if (iTask.status != SCSI_STATUS_GOOD) {
646 iscsi_allocmap_set_invalid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
647 nb_sectors * BDRV_SECTOR_SIZE);
648 error_report("iSCSI WRITE10/16 failed at lba %" PRIu64 ": %s", lba,
649 iTask.err_str);
650 r = iTask.err_code;
651 goto out_unlock;
654 iscsi_allocmap_set_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
655 nb_sectors * BDRV_SECTOR_SIZE);
657 out_unlock:
658 qemu_mutex_unlock(&iscsilun->mutex);
659 g_free(iTask.err_str);
660 return r;
665 static int coroutine_fn iscsi_co_block_status(BlockDriverState *bs,
666 bool want_zero, int64_t offset,
667 int64_t bytes, int64_t *pnum,
668 int64_t *map,
669 BlockDriverState **file)
671 IscsiLun *iscsilun = bs->opaque;
672 struct scsi_get_lba_status *lbas = NULL;
673 struct scsi_lba_status_descriptor *lbasd = NULL;
674 struct IscsiTask iTask;
675 uint64_t lba;
676 int ret;
678 iscsi_co_init_iscsitask(iscsilun, &iTask);
680 assert(QEMU_IS_ALIGNED(offset | bytes, iscsilun->block_size));
682 /* default to all sectors allocated */
683 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
684 if (map) {
685 *map = offset;
687 *pnum = bytes;
689 /* LUN does not support logical block provisioning */
690 if (!iscsilun->lbpme) {
691 goto out;
694 lba = offset / iscsilun->block_size;
696 qemu_mutex_lock(&iscsilun->mutex);
697 retry:
698 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
699 lba, 8 + 16, iscsi_co_generic_cb,
700 &iTask) == NULL) {
701 ret = -ENOMEM;
702 goto out_unlock;
704 iscsi_co_wait_for_task(&iTask, iscsilun);
706 if (iTask.do_retry) {
707 if (iTask.task != NULL) {
708 scsi_free_scsi_task(iTask.task);
709 iTask.task = NULL;
711 iTask.complete = 0;
712 goto retry;
715 if (iTask.status != SCSI_STATUS_GOOD) {
716 /* in case the get_lba_status_callout fails (i.e.
717 * because the device is busy or the cmd is not
718 * supported) we pretend all blocks are allocated
719 * for backwards compatibility */
720 error_report("iSCSI GET_LBA_STATUS failed at lba %" PRIu64 ": %s",
721 lba, iTask.err_str);
722 goto out_unlock;
725 lbas = scsi_datain_unmarshall(iTask.task);
726 if (lbas == NULL) {
727 ret = -EIO;
728 goto out_unlock;
731 lbasd = &lbas->descriptors[0];
733 if (lba != lbasd->lba) {
734 ret = -EIO;
735 goto out_unlock;
738 *pnum = (int64_t) lbasd->num_blocks * iscsilun->block_size;
740 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
741 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
742 ret &= ~BDRV_BLOCK_DATA;
743 if (iscsilun->lbprz) {
744 ret |= BDRV_BLOCK_ZERO;
748 if (ret & BDRV_BLOCK_ZERO) {
749 iscsi_allocmap_set_unallocated(iscsilun, offset, *pnum);
750 } else {
751 iscsi_allocmap_set_allocated(iscsilun, offset, *pnum);
754 if (*pnum > bytes) {
755 *pnum = bytes;
757 out_unlock:
758 qemu_mutex_unlock(&iscsilun->mutex);
759 g_free(iTask.err_str);
760 out:
761 if (iTask.task != NULL) {
762 scsi_free_scsi_task(iTask.task);
764 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID && file) {
765 *file = bs;
767 return ret;
770 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
771 int64_t sector_num, int nb_sectors,
772 QEMUIOVector *iov)
774 IscsiLun *iscsilun = bs->opaque;
775 struct IscsiTask iTask;
776 uint64_t lba;
777 uint32_t num_sectors;
778 int r = 0;
780 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
781 return -EINVAL;
784 if (bs->bl.max_transfer) {
785 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
788 /* if cache.direct is off and we have a valid entry in our allocation map
789 * we can skip checking the block status and directly return zeroes if
790 * the request falls within an unallocated area */
791 if (iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
792 nb_sectors * BDRV_SECTOR_SIZE) &&
793 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
794 nb_sectors * BDRV_SECTOR_SIZE)) {
795 qemu_iovec_memset(iov, 0, 0x00, iov->size);
796 return 0;
799 if (nb_sectors >= ISCSI_CHECKALLOC_THRES &&
800 !iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
801 nb_sectors * BDRV_SECTOR_SIZE) &&
802 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
803 nb_sectors * BDRV_SECTOR_SIZE)) {
804 int64_t pnum;
805 /* check the block status from the beginning of the cluster
806 * containing the start sector */
807 int64_t head;
808 int ret;
810 assert(iscsilun->cluster_size);
811 head = (sector_num * BDRV_SECTOR_SIZE) % iscsilun->cluster_size;
812 ret = iscsi_co_block_status(bs, true,
813 sector_num * BDRV_SECTOR_SIZE - head,
814 BDRV_REQUEST_MAX_BYTES, &pnum, NULL, NULL);
815 if (ret < 0) {
816 return ret;
818 /* if the whole request falls into an unallocated area we can avoid
819 * reading and directly return zeroes instead */
820 if (ret & BDRV_BLOCK_ZERO &&
821 pnum >= nb_sectors * BDRV_SECTOR_SIZE + head) {
822 qemu_iovec_memset(iov, 0, 0x00, iov->size);
823 return 0;
827 lba = sector_qemu2lun(sector_num, iscsilun);
828 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
830 iscsi_co_init_iscsitask(iscsilun, &iTask);
831 qemu_mutex_lock(&iscsilun->mutex);
832 retry:
833 if (iscsilun->use_16_for_rw) {
834 #if LIBISCSI_API_VERSION >= (20160603)
835 iTask.task = iscsi_read16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
836 num_sectors * iscsilun->block_size,
837 iscsilun->block_size, 0, 0, 0, 0, 0,
838 iscsi_co_generic_cb, &iTask,
839 (struct scsi_iovec *)iov->iov, iov->niov);
840 } else {
841 iTask.task = iscsi_read10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
842 num_sectors * iscsilun->block_size,
843 iscsilun->block_size,
844 0, 0, 0, 0, 0,
845 iscsi_co_generic_cb, &iTask,
846 (struct scsi_iovec *)iov->iov, iov->niov);
848 #else
849 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
850 num_sectors * iscsilun->block_size,
851 iscsilun->block_size, 0, 0, 0, 0, 0,
852 iscsi_co_generic_cb, &iTask);
853 } else {
854 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
855 num_sectors * iscsilun->block_size,
856 iscsilun->block_size,
857 0, 0, 0, 0, 0,
858 iscsi_co_generic_cb, &iTask);
860 #endif
861 if (iTask.task == NULL) {
862 qemu_mutex_unlock(&iscsilun->mutex);
863 return -ENOMEM;
865 #if LIBISCSI_API_VERSION < (20160603)
866 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
867 #endif
869 iscsi_co_wait_for_task(&iTask, iscsilun);
870 if (iTask.task != NULL) {
871 scsi_free_scsi_task(iTask.task);
872 iTask.task = NULL;
875 if (iTask.do_retry) {
876 iTask.complete = 0;
877 goto retry;
880 if (iTask.status != SCSI_STATUS_GOOD) {
881 error_report("iSCSI READ10/16 failed at lba %" PRIu64 ": %s",
882 lba, iTask.err_str);
883 r = iTask.err_code;
886 qemu_mutex_unlock(&iscsilun->mutex);
887 g_free(iTask.err_str);
888 return r;
891 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
893 IscsiLun *iscsilun = bs->opaque;
894 struct IscsiTask iTask;
895 int r = 0;
897 iscsi_co_init_iscsitask(iscsilun, &iTask);
898 qemu_mutex_lock(&iscsilun->mutex);
899 retry:
900 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
901 0, iscsi_co_generic_cb, &iTask) == NULL) {
902 qemu_mutex_unlock(&iscsilun->mutex);
903 return -ENOMEM;
906 iscsi_co_wait_for_task(&iTask, iscsilun);
908 if (iTask.task != NULL) {
909 scsi_free_scsi_task(iTask.task);
910 iTask.task = NULL;
913 if (iTask.do_retry) {
914 iTask.complete = 0;
915 goto retry;
918 if (iTask.status != SCSI_STATUS_GOOD) {
919 error_report("iSCSI SYNCHRONIZECACHE10 failed: %s", iTask.err_str);
920 r = iTask.err_code;
923 qemu_mutex_unlock(&iscsilun->mutex);
924 g_free(iTask.err_str);
925 return r;
928 #ifdef __linux__
929 /* Called (via iscsi_service) with QemuMutex held. */
930 static void
931 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
932 void *command_data, void *opaque)
934 IscsiAIOCB *acb = opaque;
936 g_free(acb->buf);
937 acb->buf = NULL;
939 acb->status = 0;
940 if (status < 0) {
941 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
942 iscsi_get_error(iscsi));
943 acb->status = iscsi_translate_sense(&acb->task->sense);
946 acb->ioh->driver_status = 0;
947 acb->ioh->host_status = 0;
948 acb->ioh->resid = 0;
949 acb->ioh->status = status;
951 #define SG_ERR_DRIVER_SENSE 0x08
953 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
954 int ss;
956 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
958 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
959 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
960 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
961 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
964 iscsi_schedule_bh(acb);
967 static void iscsi_ioctl_bh_completion(void *opaque)
969 IscsiAIOCB *acb = opaque;
971 qemu_bh_delete(acb->bh);
972 acb->common.cb(acb->common.opaque, acb->ret);
973 qemu_aio_unref(acb);
976 static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf)
978 BlockDriverState *bs = acb->common.bs;
979 IscsiLun *iscsilun = bs->opaque;
980 int ret = 0;
982 switch (req) {
983 case SG_GET_VERSION_NUM:
984 *(int *)buf = 30000;
985 break;
986 case SG_GET_SCSI_ID:
987 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
988 break;
989 default:
990 ret = -EINVAL;
992 assert(!acb->bh);
993 acb->bh = aio_bh_new(bdrv_get_aio_context(bs),
994 iscsi_ioctl_bh_completion, acb);
995 acb->ret = ret;
996 qemu_bh_schedule(acb->bh);
999 static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
1000 unsigned long int req, void *buf,
1001 BlockCompletionFunc *cb, void *opaque)
1003 IscsiLun *iscsilun = bs->opaque;
1004 struct iscsi_context *iscsi = iscsilun->iscsi;
1005 struct iscsi_data data;
1006 IscsiAIOCB *acb;
1008 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
1010 acb->iscsilun = iscsilun;
1011 acb->bh = NULL;
1012 acb->status = -EINPROGRESS;
1013 acb->buf = NULL;
1014 acb->ioh = buf;
1016 if (req != SG_IO) {
1017 iscsi_ioctl_handle_emulated(acb, req, buf);
1018 return &acb->common;
1021 if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) {
1022 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
1023 acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE);
1024 qemu_aio_unref(acb);
1025 return NULL;
1028 acb->task = malloc(sizeof(struct scsi_task));
1029 if (acb->task == NULL) {
1030 error_report("iSCSI: Failed to allocate task for scsi command. %s",
1031 iscsi_get_error(iscsi));
1032 qemu_aio_unref(acb);
1033 return NULL;
1035 memset(acb->task, 0, sizeof(struct scsi_task));
1037 switch (acb->ioh->dxfer_direction) {
1038 case SG_DXFER_TO_DEV:
1039 acb->task->xfer_dir = SCSI_XFER_WRITE;
1040 break;
1041 case SG_DXFER_FROM_DEV:
1042 acb->task->xfer_dir = SCSI_XFER_READ;
1043 break;
1044 default:
1045 acb->task->xfer_dir = SCSI_XFER_NONE;
1046 break;
1049 acb->task->cdb_size = acb->ioh->cmd_len;
1050 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
1051 acb->task->expxferlen = acb->ioh->dxfer_len;
1053 data.size = 0;
1054 qemu_mutex_lock(&iscsilun->mutex);
1055 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
1056 if (acb->ioh->iovec_count == 0) {
1057 data.data = acb->ioh->dxferp;
1058 data.size = acb->ioh->dxfer_len;
1059 } else {
1060 scsi_task_set_iov_out(acb->task,
1061 (struct scsi_iovec *) acb->ioh->dxferp,
1062 acb->ioh->iovec_count);
1066 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
1067 iscsi_aio_ioctl_cb,
1068 (data.size > 0) ? &data : NULL,
1069 acb) != 0) {
1070 qemu_mutex_unlock(&iscsilun->mutex);
1071 scsi_free_scsi_task(acb->task);
1072 qemu_aio_unref(acb);
1073 return NULL;
1076 /* tell libiscsi to read straight into the buffer we got from ioctl */
1077 if (acb->task->xfer_dir == SCSI_XFER_READ) {
1078 if (acb->ioh->iovec_count == 0) {
1079 scsi_task_add_data_in_buffer(acb->task,
1080 acb->ioh->dxfer_len,
1081 acb->ioh->dxferp);
1082 } else {
1083 scsi_task_set_iov_in(acb->task,
1084 (struct scsi_iovec *) acb->ioh->dxferp,
1085 acb->ioh->iovec_count);
1089 iscsi_set_events(iscsilun);
1090 qemu_mutex_unlock(&iscsilun->mutex);
1092 return &acb->common;
1095 #endif
1097 static int64_t
1098 iscsi_getlength(BlockDriverState *bs)
1100 IscsiLun *iscsilun = bs->opaque;
1101 int64_t len;
1103 len = iscsilun->num_blocks;
1104 len *= iscsilun->block_size;
1106 return len;
1109 static int
1110 coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
1112 IscsiLun *iscsilun = bs->opaque;
1113 struct IscsiTask iTask;
1114 struct unmap_list list;
1115 int r = 0;
1117 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) {
1118 return -ENOTSUP;
1121 if (!iscsilun->lbp.lbpu) {
1122 /* UNMAP is not supported by the target */
1123 return 0;
1126 list.lba = offset / iscsilun->block_size;
1127 list.num = bytes / iscsilun->block_size;
1129 iscsi_co_init_iscsitask(iscsilun, &iTask);
1130 qemu_mutex_lock(&iscsilun->mutex);
1131 retry:
1132 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
1133 iscsi_co_generic_cb, &iTask) == NULL) {
1134 r = -ENOMEM;
1135 goto out_unlock;
1138 iscsi_co_wait_for_task(&iTask, iscsilun);
1140 if (iTask.task != NULL) {
1141 scsi_free_scsi_task(iTask.task);
1142 iTask.task = NULL;
1145 if (iTask.do_retry) {
1146 iTask.complete = 0;
1147 goto retry;
1150 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
1152 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
1153 /* the target might fail with a check condition if it
1154 is not happy with the alignment of the UNMAP request
1155 we silently fail in this case */
1156 goto out_unlock;
1159 if (iTask.status != SCSI_STATUS_GOOD) {
1160 error_report("iSCSI UNMAP failed at lba %" PRIu64 ": %s",
1161 list.lba, iTask.err_str);
1162 r = iTask.err_code;
1163 goto out_unlock;
1166 out_unlock:
1167 qemu_mutex_unlock(&iscsilun->mutex);
1168 g_free(iTask.err_str);
1169 return r;
1172 static int
1173 coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1174 int bytes, BdrvRequestFlags flags)
1176 IscsiLun *iscsilun = bs->opaque;
1177 struct IscsiTask iTask;
1178 uint64_t lba;
1179 uint32_t nb_blocks;
1180 bool use_16_for_ws = iscsilun->use_16_for_rw;
1181 int r = 0;
1183 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) {
1184 return -ENOTSUP;
1187 if (flags & BDRV_REQ_MAY_UNMAP) {
1188 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
1189 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1190 use_16_for_ws = true;
1192 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
1193 /* WRITESAME16 with UNMAP is not supported by the target,
1194 * fall back and try WRITESAME10/16 without UNMAP */
1195 flags &= ~BDRV_REQ_MAY_UNMAP;
1196 use_16_for_ws = iscsilun->use_16_for_rw;
1200 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
1201 /* WRITESAME without UNMAP is not supported by the target */
1202 return -ENOTSUP;
1205 lba = offset / iscsilun->block_size;
1206 nb_blocks = bytes / iscsilun->block_size;
1208 if (iscsilun->zeroblock == NULL) {
1209 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size);
1210 if (iscsilun->zeroblock == NULL) {
1211 return -ENOMEM;
1215 qemu_mutex_lock(&iscsilun->mutex);
1216 iscsi_co_init_iscsitask(iscsilun, &iTask);
1217 retry:
1218 if (use_16_for_ws) {
1219 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1220 iscsilun->zeroblock, iscsilun->block_size,
1221 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1222 0, 0, iscsi_co_generic_cb, &iTask);
1223 } else {
1224 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
1225 iscsilun->zeroblock, iscsilun->block_size,
1226 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1227 0, 0, iscsi_co_generic_cb, &iTask);
1229 if (iTask.task == NULL) {
1230 qemu_mutex_unlock(&iscsilun->mutex);
1231 return -ENOMEM;
1234 iscsi_co_wait_for_task(&iTask, iscsilun);
1236 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1237 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
1238 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
1239 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
1240 /* WRITE SAME is not supported by the target */
1241 iscsilun->has_write_same = false;
1242 scsi_free_scsi_task(iTask.task);
1243 r = -ENOTSUP;
1244 goto out_unlock;
1247 if (iTask.task != NULL) {
1248 scsi_free_scsi_task(iTask.task);
1249 iTask.task = NULL;
1252 if (iTask.do_retry) {
1253 iTask.complete = 0;
1254 goto retry;
1257 if (iTask.status != SCSI_STATUS_GOOD) {
1258 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
1259 error_report("iSCSI WRITESAME10/16 failed at lba %" PRIu64 ": %s",
1260 lba, iTask.err_str);
1261 r = iTask.err_code;
1262 goto out_unlock;
1265 if (flags & BDRV_REQ_MAY_UNMAP) {
1266 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
1267 } else {
1268 iscsi_allocmap_set_allocated(iscsilun, offset, bytes);
1271 out_unlock:
1272 qemu_mutex_unlock(&iscsilun->mutex);
1273 g_free(iTask.err_str);
1274 return r;
1277 static void apply_chap(struct iscsi_context *iscsi, QemuOpts *opts,
1278 Error **errp)
1280 const char *user = NULL;
1281 const char *password = NULL;
1282 const char *secretid;
1283 char *secret = NULL;
1285 user = qemu_opt_get(opts, "user");
1286 if (!user) {
1287 return;
1290 secretid = qemu_opt_get(opts, "password-secret");
1291 password = qemu_opt_get(opts, "password");
1292 if (secretid && password) {
1293 error_setg(errp, "'password' and 'password-secret' properties are "
1294 "mutually exclusive");
1295 return;
1297 if (secretid) {
1298 secret = qcrypto_secret_lookup_as_utf8(secretid, errp);
1299 if (!secret) {
1300 return;
1302 password = secret;
1303 } else if (!password) {
1304 error_setg(errp, "CHAP username specified but no password was given");
1305 return;
1308 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
1309 error_setg(errp, "Failed to set initiator username and password");
1312 g_free(secret);
1315 static void apply_header_digest(struct iscsi_context *iscsi, QemuOpts *opts,
1316 Error **errp)
1318 const char *digest = NULL;
1320 digest = qemu_opt_get(opts, "header-digest");
1321 if (!digest) {
1322 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1323 } else if (!strcmp(digest, "crc32c")) {
1324 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1325 } else if (!strcmp(digest, "none")) {
1326 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1327 } else if (!strcmp(digest, "crc32c-none")) {
1328 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1329 } else if (!strcmp(digest, "none-crc32c")) {
1330 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1331 } else {
1332 error_setg(errp, "Invalid header-digest setting : %s", digest);
1336 static char *get_initiator_name(QemuOpts *opts)
1338 const char *name;
1339 char *iscsi_name;
1340 UuidInfo *uuid_info;
1342 name = qemu_opt_get(opts, "initiator-name");
1343 if (name) {
1344 return g_strdup(name);
1347 uuid_info = qmp_query_uuid(NULL);
1348 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1349 name = qemu_get_vm_name();
1350 } else {
1351 name = uuid_info->UUID;
1353 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1354 name ? ":" : "", name ? name : "");
1355 qapi_free_UuidInfo(uuid_info);
1356 return iscsi_name;
1359 static void iscsi_nop_timed_event(void *opaque)
1361 IscsiLun *iscsilun = opaque;
1363 qemu_mutex_lock(&iscsilun->mutex);
1364 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) {
1365 error_report("iSCSI: NOP timeout. Reconnecting...");
1366 iscsilun->request_timed_out = true;
1367 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1368 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1369 goto out;
1372 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1373 iscsi_set_events(iscsilun);
1375 out:
1376 qemu_mutex_unlock(&iscsilun->mutex);
1379 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
1381 struct scsi_task *task = NULL;
1382 struct scsi_readcapacity10 *rc10 = NULL;
1383 struct scsi_readcapacity16 *rc16 = NULL;
1384 int retries = ISCSI_CMD_RETRIES;
1386 do {
1387 if (task != NULL) {
1388 scsi_free_scsi_task(task);
1389 task = NULL;
1392 switch (iscsilun->type) {
1393 case TYPE_DISK:
1394 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1395 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1396 rc16 = scsi_datain_unmarshall(task);
1397 if (rc16 == NULL) {
1398 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1399 } else {
1400 iscsilun->block_size = rc16->block_length;
1401 iscsilun->num_blocks = rc16->returned_lba + 1;
1402 iscsilun->lbpme = !!rc16->lbpme;
1403 iscsilun->lbprz = !!rc16->lbprz;
1404 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
1406 break;
1408 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1409 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
1410 break;
1412 /* Fall through and try READ CAPACITY(10) instead. */
1413 case TYPE_ROM:
1414 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1415 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1416 rc10 = scsi_datain_unmarshall(task);
1417 if (rc10 == NULL) {
1418 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1419 } else {
1420 iscsilun->block_size = rc10->block_size;
1421 if (rc10->lba == 0) {
1422 /* blank disk loaded */
1423 iscsilun->num_blocks = 0;
1424 } else {
1425 iscsilun->num_blocks = rc10->lba + 1;
1429 break;
1430 default:
1431 return;
1433 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1434 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1435 && retries-- > 0);
1437 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1438 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command");
1439 } else if (!iscsilun->block_size ||
1440 iscsilun->block_size % BDRV_SECTOR_SIZE) {
1441 error_setg(errp, "iSCSI: the target returned an invalid "
1442 "block size of %d.", iscsilun->block_size);
1444 if (task) {
1445 scsi_free_scsi_task(task);
1449 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
1450 int evpd, int pc, void **inq, Error **errp)
1452 int full_size;
1453 struct scsi_task *task = NULL;
1454 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1455 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1456 goto fail;
1458 full_size = scsi_datain_getfullsize(task);
1459 if (full_size > task->datain.size) {
1460 scsi_free_scsi_task(task);
1462 /* we need more data for the full list */
1463 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1464 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1465 goto fail;
1469 *inq = scsi_datain_unmarshall(task);
1470 if (*inq == NULL) {
1471 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1472 goto fail_with_err;
1475 return task;
1477 fail:
1478 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1479 iscsi_get_error(iscsi));
1480 fail_with_err:
1481 if (task != NULL) {
1482 scsi_free_scsi_task(task);
1484 return NULL;
1487 static void iscsi_detach_aio_context(BlockDriverState *bs)
1489 IscsiLun *iscsilun = bs->opaque;
1491 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi),
1492 false, NULL, NULL, NULL, NULL);
1493 iscsilun->events = 0;
1495 if (iscsilun->nop_timer) {
1496 timer_del(iscsilun->nop_timer);
1497 timer_free(iscsilun->nop_timer);
1498 iscsilun->nop_timer = NULL;
1500 if (iscsilun->event_timer) {
1501 timer_del(iscsilun->event_timer);
1502 timer_free(iscsilun->event_timer);
1503 iscsilun->event_timer = NULL;
1507 static void iscsi_attach_aio_context(BlockDriverState *bs,
1508 AioContext *new_context)
1510 IscsiLun *iscsilun = bs->opaque;
1512 iscsilun->aio_context = new_context;
1513 iscsi_set_events(iscsilun);
1515 /* Set up a timer for sending out iSCSI NOPs */
1516 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1517 QEMU_CLOCK_REALTIME, SCALE_MS,
1518 iscsi_nop_timed_event, iscsilun);
1519 timer_mod(iscsilun->nop_timer,
1520 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1522 /* Set up a timer for periodic calls to iscsi_set_events and to
1523 * scan for command timeout */
1524 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context,
1525 QEMU_CLOCK_REALTIME, SCALE_MS,
1526 iscsi_timed_check_events, iscsilun);
1527 timer_mod(iscsilun->event_timer,
1528 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
1531 static void iscsi_modesense_sync(IscsiLun *iscsilun)
1533 struct scsi_task *task;
1534 struct scsi_mode_sense *ms = NULL;
1535 iscsilun->write_protected = false;
1536 iscsilun->dpofua = false;
1538 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1539 1, SCSI_MODESENSE_PC_CURRENT,
1540 0x3F, 0, 255);
1541 if (task == NULL) {
1542 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1543 iscsi_get_error(iscsilun->iscsi));
1544 goto out;
1547 if (task->status != SCSI_STATUS_GOOD) {
1548 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1549 goto out;
1551 ms = scsi_datain_unmarshall(task);
1552 if (!ms) {
1553 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1554 iscsi_get_error(iscsilun->iscsi));
1555 goto out;
1557 iscsilun->write_protected = ms->device_specific_parameter & 0x80;
1558 iscsilun->dpofua = ms->device_specific_parameter & 0x10;
1560 out:
1561 if (task) {
1562 scsi_free_scsi_task(task);
1566 static void iscsi_parse_iscsi_option(const char *target, QDict *options)
1568 QemuOptsList *list;
1569 QemuOpts *opts;
1570 const char *user, *password, *password_secret, *initiator_name,
1571 *header_digest, *timeout;
1573 list = qemu_find_opts("iscsi");
1574 if (!list) {
1575 return;
1578 opts = qemu_opts_find(list, target);
1579 if (opts == NULL) {
1580 opts = QTAILQ_FIRST(&list->head);
1581 if (!opts) {
1582 return;
1586 user = qemu_opt_get(opts, "user");
1587 if (user) {
1588 qdict_set_default_str(options, "user", user);
1591 password = qemu_opt_get(opts, "password");
1592 if (password) {
1593 qdict_set_default_str(options, "password", password);
1596 password_secret = qemu_opt_get(opts, "password-secret");
1597 if (password_secret) {
1598 qdict_set_default_str(options, "password-secret", password_secret);
1601 initiator_name = qemu_opt_get(opts, "initiator-name");
1602 if (initiator_name) {
1603 qdict_set_default_str(options, "initiator-name", initiator_name);
1606 header_digest = qemu_opt_get(opts, "header-digest");
1607 if (header_digest) {
1608 /* -iscsi takes upper case values, but QAPI only supports lower case
1609 * enum constant names, so we have to convert here. */
1610 char *qapi_value = g_ascii_strdown(header_digest, -1);
1611 qdict_set_default_str(options, "header-digest", qapi_value);
1612 g_free(qapi_value);
1615 timeout = qemu_opt_get(opts, "timeout");
1616 if (timeout) {
1617 qdict_set_default_str(options, "timeout", timeout);
1622 * We support iscsi url's on the form
1623 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1625 static void iscsi_parse_filename(const char *filename, QDict *options,
1626 Error **errp)
1628 struct iscsi_url *iscsi_url;
1629 const char *transport_name;
1630 char *lun_str;
1632 iscsi_url = iscsi_parse_full_url(NULL, filename);
1633 if (iscsi_url == NULL) {
1634 error_setg(errp, "Failed to parse URL : %s", filename);
1635 return;
1638 #if LIBISCSI_API_VERSION >= (20160603)
1639 switch (iscsi_url->transport) {
1640 case TCP_TRANSPORT:
1641 transport_name = "tcp";
1642 break;
1643 case ISER_TRANSPORT:
1644 transport_name = "iser";
1645 break;
1646 default:
1647 error_setg(errp, "Unknown transport type (%d)",
1648 iscsi_url->transport);
1649 return;
1651 #else
1652 transport_name = "tcp";
1653 #endif
1655 qdict_set_default_str(options, "transport", transport_name);
1656 qdict_set_default_str(options, "portal", iscsi_url->portal);
1657 qdict_set_default_str(options, "target", iscsi_url->target);
1659 lun_str = g_strdup_printf("%d", iscsi_url->lun);
1660 qdict_set_default_str(options, "lun", lun_str);
1661 g_free(lun_str);
1663 /* User/password from -iscsi take precedence over those from the URL */
1664 iscsi_parse_iscsi_option(iscsi_url->target, options);
1666 if (iscsi_url->user[0] != '\0') {
1667 qdict_set_default_str(options, "user", iscsi_url->user);
1668 qdict_set_default_str(options, "password", iscsi_url->passwd);
1671 iscsi_destroy_url(iscsi_url);
1674 static QemuOptsList runtime_opts = {
1675 .name = "iscsi",
1676 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1677 .desc = {
1679 .name = "transport",
1680 .type = QEMU_OPT_STRING,
1683 .name = "portal",
1684 .type = QEMU_OPT_STRING,
1687 .name = "target",
1688 .type = QEMU_OPT_STRING,
1691 .name = "user",
1692 .type = QEMU_OPT_STRING,
1695 .name = "password",
1696 .type = QEMU_OPT_STRING,
1699 .name = "password-secret",
1700 .type = QEMU_OPT_STRING,
1703 .name = "lun",
1704 .type = QEMU_OPT_NUMBER,
1707 .name = "initiator-name",
1708 .type = QEMU_OPT_STRING,
1711 .name = "header-digest",
1712 .type = QEMU_OPT_STRING,
1715 .name = "timeout",
1716 .type = QEMU_OPT_NUMBER,
1718 { /* end of list */ }
1722 static void iscsi_save_designator(IscsiLun *lun,
1723 struct scsi_inquiry_device_identification *inq_di)
1725 struct scsi_inquiry_device_designator *desig, *copy = NULL;
1727 for (desig = inq_di->designators; desig; desig = desig->next) {
1728 if (desig->association ||
1729 desig->designator_type > SCSI_DESIGNATOR_TYPE_NAA) {
1730 continue;
1732 /* NAA works better than T10 vendor ID based designator. */
1733 if (!copy || copy->designator_type < desig->designator_type) {
1734 copy = desig;
1737 if (copy) {
1738 lun->dd = g_new(struct scsi_inquiry_device_designator, 1);
1739 *lun->dd = *copy;
1740 lun->dd->next = NULL;
1741 lun->dd->designator = g_malloc(copy->designator_length);
1742 memcpy(lun->dd->designator, copy->designator, copy->designator_length);
1746 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1747 Error **errp)
1749 IscsiLun *iscsilun = bs->opaque;
1750 struct iscsi_context *iscsi = NULL;
1751 struct scsi_task *task = NULL;
1752 struct scsi_inquiry_standard *inq = NULL;
1753 struct scsi_inquiry_supported_pages *inq_vpd;
1754 char *initiator_name = NULL;
1755 QemuOpts *opts;
1756 Error *local_err = NULL;
1757 const char *transport_name, *portal, *target;
1758 #if LIBISCSI_API_VERSION >= (20160603)
1759 enum iscsi_transport_type transport;
1760 #endif
1761 int i, ret = 0, timeout = 0, lun;
1763 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1764 qemu_opts_absorb_qdict(opts, options, &local_err);
1765 if (local_err) {
1766 error_propagate(errp, local_err);
1767 ret = -EINVAL;
1768 goto out;
1771 transport_name = qemu_opt_get(opts, "transport");
1772 portal = qemu_opt_get(opts, "portal");
1773 target = qemu_opt_get(opts, "target");
1774 lun = qemu_opt_get_number(opts, "lun", 0);
1776 if (!transport_name || !portal || !target) {
1777 error_setg(errp, "Need all of transport, portal and target options");
1778 ret = -EINVAL;
1779 goto out;
1782 if (!strcmp(transport_name, "tcp")) {
1783 #if LIBISCSI_API_VERSION >= (20160603)
1784 transport = TCP_TRANSPORT;
1785 } else if (!strcmp(transport_name, "iser")) {
1786 transport = ISER_TRANSPORT;
1787 #else
1788 /* TCP is what older libiscsi versions always use */
1789 #endif
1790 } else {
1791 error_setg(errp, "Unknown transport: %s", transport_name);
1792 ret = -EINVAL;
1793 goto out;
1796 memset(iscsilun, 0, sizeof(IscsiLun));
1798 initiator_name = get_initiator_name(opts);
1800 iscsi = iscsi_create_context(initiator_name);
1801 if (iscsi == NULL) {
1802 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
1803 ret = -ENOMEM;
1804 goto out;
1806 #if LIBISCSI_API_VERSION >= (20160603)
1807 if (iscsi_init_transport(iscsi, transport)) {
1808 error_setg(errp, ("Error initializing transport."));
1809 ret = -EINVAL;
1810 goto out;
1812 #endif
1813 if (iscsi_set_targetname(iscsi, target)) {
1814 error_setg(errp, "iSCSI: Failed to set target name.");
1815 ret = -EINVAL;
1816 goto out;
1819 /* check if we got CHAP username/password via the options */
1820 apply_chap(iscsi, opts, &local_err);
1821 if (local_err != NULL) {
1822 error_propagate(errp, local_err);
1823 ret = -EINVAL;
1824 goto out;
1827 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1828 error_setg(errp, "iSCSI: Failed to set session type to normal.");
1829 ret = -EINVAL;
1830 goto out;
1833 /* check if we got HEADER_DIGEST via the options */
1834 apply_header_digest(iscsi, opts, &local_err);
1835 if (local_err != NULL) {
1836 error_propagate(errp, local_err);
1837 ret = -EINVAL;
1838 goto out;
1841 /* timeout handling is broken in libiscsi before 1.15.0 */
1842 timeout = qemu_opt_get_number(opts, "timeout", 0);
1843 #if LIBISCSI_API_VERSION >= 20150621
1844 iscsi_set_timeout(iscsi, timeout);
1845 #else
1846 if (timeout) {
1847 warn_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1849 #endif
1851 if (iscsi_full_connect_sync(iscsi, portal, lun) != 0) {
1852 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
1853 iscsi_get_error(iscsi));
1854 ret = -EINVAL;
1855 goto out;
1858 iscsilun->iscsi = iscsi;
1859 iscsilun->aio_context = bdrv_get_aio_context(bs);
1860 iscsilun->lun = lun;
1861 iscsilun->has_write_same = true;
1863 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1864 (void **) &inq, errp);
1865 if (task == NULL) {
1866 ret = -EINVAL;
1867 goto out;
1869 iscsilun->type = inq->periperal_device_type;
1870 scsi_free_scsi_task(task);
1871 task = NULL;
1873 iscsi_modesense_sync(iscsilun);
1874 if (iscsilun->dpofua) {
1875 bs->supported_write_flags = BDRV_REQ_FUA;
1878 /* Check the write protect flag of the LUN if we want to write */
1879 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) &&
1880 iscsilun->write_protected) {
1881 ret = bdrv_apply_auto_read_only(bs, "LUN is write protected", errp);
1882 if (ret < 0) {
1883 goto out;
1885 flags &= ~BDRV_O_RDWR;
1888 iscsi_readcapacity_sync(iscsilun, &local_err);
1889 if (local_err != NULL) {
1890 error_propagate(errp, local_err);
1891 ret = -EINVAL;
1892 goto out;
1894 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1896 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1897 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1898 * will try to read from the device to guess the image format.
1900 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
1901 bs->sg = true;
1904 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1905 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1906 (void **) &inq_vpd, errp);
1907 if (task == NULL) {
1908 ret = -EINVAL;
1909 goto out;
1911 for (i = 0; i < inq_vpd->num_pages; i++) {
1912 struct scsi_task *inq_task;
1913 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1914 struct scsi_inquiry_block_limits *inq_bl;
1915 struct scsi_inquiry_device_identification *inq_di;
1916 switch (inq_vpd->pages[i]) {
1917 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1918 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1919 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1920 (void **) &inq_lbp, errp);
1921 if (inq_task == NULL) {
1922 ret = -EINVAL;
1923 goto out;
1925 memcpy(&iscsilun->lbp, inq_lbp,
1926 sizeof(struct scsi_inquiry_logical_block_provisioning));
1927 scsi_free_scsi_task(inq_task);
1928 break;
1929 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1930 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1931 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1932 (void **) &inq_bl, errp);
1933 if (inq_task == NULL) {
1934 ret = -EINVAL;
1935 goto out;
1937 memcpy(&iscsilun->bl, inq_bl,
1938 sizeof(struct scsi_inquiry_block_limits));
1939 scsi_free_scsi_task(inq_task);
1940 break;
1941 case SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION:
1942 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1943 SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION,
1944 (void **) &inq_di, errp);
1945 if (inq_task == NULL) {
1946 ret = -EINVAL;
1947 goto out;
1949 iscsi_save_designator(iscsilun, inq_di);
1950 scsi_free_scsi_task(inq_task);
1951 break;
1952 default:
1953 break;
1956 scsi_free_scsi_task(task);
1957 task = NULL;
1959 qemu_mutex_init(&iscsilun->mutex);
1960 iscsi_attach_aio_context(bs, iscsilun->aio_context);
1962 /* Guess the internal cluster (page) size of the iscsi target by the means
1963 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1964 * reasonable size */
1965 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
1966 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1967 iscsilun->cluster_size = iscsilun->bl.opt_unmap_gran *
1968 iscsilun->block_size;
1969 if (iscsilun->lbprz) {
1970 ret = iscsi_allocmap_init(iscsilun, bs->open_flags);
1974 if (iscsilun->lbprz && iscsilun->lbp.lbpws) {
1975 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
1978 out:
1979 qemu_opts_del(opts);
1980 g_free(initiator_name);
1981 if (task != NULL) {
1982 scsi_free_scsi_task(task);
1985 if (ret) {
1986 if (iscsi != NULL) {
1987 if (iscsi_is_logged_in(iscsi)) {
1988 iscsi_logout_sync(iscsi);
1990 iscsi_destroy_context(iscsi);
1992 memset(iscsilun, 0, sizeof(IscsiLun));
1995 return ret;
1998 static void iscsi_close(BlockDriverState *bs)
2000 IscsiLun *iscsilun = bs->opaque;
2001 struct iscsi_context *iscsi = iscsilun->iscsi;
2003 iscsi_detach_aio_context(bs);
2004 if (iscsi_is_logged_in(iscsi)) {
2005 iscsi_logout_sync(iscsi);
2007 iscsi_destroy_context(iscsi);
2008 if (iscsilun->dd) {
2009 g_free(iscsilun->dd->designator);
2010 g_free(iscsilun->dd);
2012 g_free(iscsilun->zeroblock);
2013 iscsi_allocmap_free(iscsilun);
2014 qemu_mutex_destroy(&iscsilun->mutex);
2015 memset(iscsilun, 0, sizeof(IscsiLun));
2018 static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)
2020 /* We don't actually refresh here, but just return data queried in
2021 * iscsi_open(): iscsi targets don't change their limits. */
2023 IscsiLun *iscsilun = bs->opaque;
2024 uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff;
2025 unsigned int block_size = MAX(BDRV_SECTOR_SIZE, iscsilun->block_size);
2027 assert(iscsilun->block_size >= BDRV_SECTOR_SIZE || bs->sg);
2029 bs->bl.request_alignment = block_size;
2031 if (iscsilun->bl.max_xfer_len) {
2032 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len);
2035 if (max_xfer_len * block_size < INT_MAX) {
2036 bs->bl.max_transfer = max_xfer_len * iscsilun->block_size;
2039 if (iscsilun->lbp.lbpu) {
2040 if (iscsilun->bl.max_unmap < 0xffffffff / block_size) {
2041 bs->bl.max_pdiscard =
2042 iscsilun->bl.max_unmap * iscsilun->block_size;
2044 bs->bl.pdiscard_alignment =
2045 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
2046 } else {
2047 bs->bl.pdiscard_alignment = iscsilun->block_size;
2050 if (iscsilun->bl.max_ws_len < 0xffffffff / block_size) {
2051 bs->bl.max_pwrite_zeroes =
2052 iscsilun->bl.max_ws_len * iscsilun->block_size;
2054 if (iscsilun->lbp.lbpws) {
2055 bs->bl.pwrite_zeroes_alignment =
2056 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
2057 } else {
2058 bs->bl.pwrite_zeroes_alignment = iscsilun->block_size;
2060 if (iscsilun->bl.opt_xfer_len &&
2061 iscsilun->bl.opt_xfer_len < INT_MAX / block_size) {
2062 bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len *
2063 iscsilun->block_size);
2067 /* Note that this will not re-establish a connection with an iSCSI target - it
2068 * is effectively a NOP. */
2069 static int iscsi_reopen_prepare(BDRVReopenState *state,
2070 BlockReopenQueue *queue, Error **errp)
2072 IscsiLun *iscsilun = state->bs->opaque;
2074 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) {
2075 error_setg(errp, "Cannot open a write protected LUN as read-write");
2076 return -EACCES;
2078 return 0;
2081 static void iscsi_reopen_commit(BDRVReopenState *reopen_state)
2083 IscsiLun *iscsilun = reopen_state->bs->opaque;
2085 /* the cache.direct status might have changed */
2086 if (iscsilun->allocmap != NULL) {
2087 iscsi_allocmap_init(iscsilun, reopen_state->flags);
2091 static int coroutine_fn iscsi_co_truncate(BlockDriverState *bs, int64_t offset,
2092 PreallocMode prealloc, Error **errp)
2094 IscsiLun *iscsilun = bs->opaque;
2095 Error *local_err = NULL;
2097 if (prealloc != PREALLOC_MODE_OFF) {
2098 error_setg(errp, "Unsupported preallocation mode '%s'",
2099 PreallocMode_str(prealloc));
2100 return -ENOTSUP;
2103 if (iscsilun->type != TYPE_DISK) {
2104 error_setg(errp, "Cannot resize non-disk iSCSI devices");
2105 return -ENOTSUP;
2108 iscsi_readcapacity_sync(iscsilun, &local_err);
2109 if (local_err != NULL) {
2110 error_propagate(errp, local_err);
2111 return -EIO;
2114 if (offset > iscsi_getlength(bs)) {
2115 error_setg(errp, "Cannot grow iSCSI devices");
2116 return -EINVAL;
2119 if (iscsilun->allocmap != NULL) {
2120 iscsi_allocmap_init(iscsilun, bs->open_flags);
2123 return 0;
2126 static int coroutine_fn iscsi_co_create_opts(const char *filename, QemuOpts *opts,
2127 Error **errp)
2129 int ret = 0;
2130 int64_t total_size = 0;
2131 BlockDriverState *bs;
2132 IscsiLun *iscsilun = NULL;
2133 QDict *bs_options;
2134 Error *local_err = NULL;
2136 bs = bdrv_new();
2138 /* Read out options */
2139 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2140 BDRV_SECTOR_SIZE);
2141 bs->opaque = g_new0(struct IscsiLun, 1);
2142 iscsilun = bs->opaque;
2144 bs_options = qdict_new();
2145 iscsi_parse_filename(filename, bs_options, &local_err);
2146 if (local_err) {
2147 error_propagate(errp, local_err);
2148 ret = -EINVAL;
2149 } else {
2150 ret = iscsi_open(bs, bs_options, 0, NULL);
2152 qobject_unref(bs_options);
2154 if (ret != 0) {
2155 goto out;
2157 iscsi_detach_aio_context(bs);
2158 if (iscsilun->type != TYPE_DISK) {
2159 ret = -ENODEV;
2160 goto out;
2162 if (bs->total_sectors < total_size) {
2163 ret = -ENOSPC;
2164 goto out;
2167 ret = 0;
2168 out:
2169 if (iscsilun->iscsi != NULL) {
2170 iscsi_destroy_context(iscsilun->iscsi);
2172 g_free(bs->opaque);
2173 bs->opaque = NULL;
2174 bdrv_unref(bs);
2175 return ret;
2178 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2180 IscsiLun *iscsilun = bs->opaque;
2181 bdi->unallocated_blocks_are_zero = iscsilun->lbprz;
2182 bdi->cluster_size = iscsilun->cluster_size;
2183 return 0;
2186 static void coroutine_fn iscsi_co_invalidate_cache(BlockDriverState *bs,
2187 Error **errp)
2189 IscsiLun *iscsilun = bs->opaque;
2190 iscsi_allocmap_invalidate(iscsilun);
2193 static int coroutine_fn iscsi_co_copy_range_from(BlockDriverState *bs,
2194 BdrvChild *src,
2195 uint64_t src_offset,
2196 BdrvChild *dst,
2197 uint64_t dst_offset,
2198 uint64_t bytes,
2199 BdrvRequestFlags read_flags,
2200 BdrvRequestFlags write_flags)
2202 return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
2203 read_flags, write_flags);
2206 static struct scsi_task *iscsi_xcopy_task(int param_len)
2208 struct scsi_task *task;
2210 task = g_new0(struct scsi_task, 1);
2212 task->cdb[0] = EXTENDED_COPY;
2213 task->cdb[10] = (param_len >> 24) & 0xFF;
2214 task->cdb[11] = (param_len >> 16) & 0xFF;
2215 task->cdb[12] = (param_len >> 8) & 0xFF;
2216 task->cdb[13] = param_len & 0xFF;
2217 task->cdb_size = 16;
2218 task->xfer_dir = SCSI_XFER_WRITE;
2219 task->expxferlen = param_len;
2221 return task;
2224 static void iscsi_populate_target_desc(unsigned char *desc, IscsiLun *lun)
2226 struct scsi_inquiry_device_designator *dd = lun->dd;
2228 memset(desc, 0, 32);
2229 desc[0] = 0xE4; /* IDENT_DESCR_TGT_DESCR */
2230 desc[4] = dd->code_set;
2231 desc[5] = (dd->designator_type & 0xF)
2232 | ((dd->association & 3) << 4);
2233 desc[7] = dd->designator_length;
2234 memcpy(desc + 8, dd->designator, MIN(dd->designator_length, 20));
2236 desc[28] = 0;
2237 desc[29] = (lun->block_size >> 16) & 0xFF;
2238 desc[30] = (lun->block_size >> 8) & 0xFF;
2239 desc[31] = lun->block_size & 0xFF;
2242 static void iscsi_xcopy_desc_hdr(uint8_t *hdr, int dc, int cat, int src_index,
2243 int dst_index)
2245 hdr[0] = 0x02; /* BLK_TO_BLK_SEG_DESCR */
2246 hdr[1] = ((dc << 1) | cat) & 0xFF;
2247 hdr[2] = (XCOPY_BLK2BLK_SEG_DESC_SIZE >> 8) & 0xFF;
2248 /* don't account for the first 4 bytes in descriptor header*/
2249 hdr[3] = (XCOPY_BLK2BLK_SEG_DESC_SIZE - 4 /* SEG_DESC_SRC_INDEX_OFFSET */) & 0xFF;
2250 hdr[4] = (src_index >> 8) & 0xFF;
2251 hdr[5] = src_index & 0xFF;
2252 hdr[6] = (dst_index >> 8) & 0xFF;
2253 hdr[7] = dst_index & 0xFF;
2256 static void iscsi_xcopy_populate_desc(uint8_t *desc, int dc, int cat,
2257 int src_index, int dst_index, int num_blks,
2258 uint64_t src_lba, uint64_t dst_lba)
2260 iscsi_xcopy_desc_hdr(desc, dc, cat, src_index, dst_index);
2262 /* The caller should verify the request size */
2263 assert(num_blks < 65536);
2264 desc[10] = (num_blks >> 8) & 0xFF;
2265 desc[11] = num_blks & 0xFF;
2266 desc[12] = (src_lba >> 56) & 0xFF;
2267 desc[13] = (src_lba >> 48) & 0xFF;
2268 desc[14] = (src_lba >> 40) & 0xFF;
2269 desc[15] = (src_lba >> 32) & 0xFF;
2270 desc[16] = (src_lba >> 24) & 0xFF;
2271 desc[17] = (src_lba >> 16) & 0xFF;
2272 desc[18] = (src_lba >> 8) & 0xFF;
2273 desc[19] = src_lba & 0xFF;
2274 desc[20] = (dst_lba >> 56) & 0xFF;
2275 desc[21] = (dst_lba >> 48) & 0xFF;
2276 desc[22] = (dst_lba >> 40) & 0xFF;
2277 desc[23] = (dst_lba >> 32) & 0xFF;
2278 desc[24] = (dst_lba >> 24) & 0xFF;
2279 desc[25] = (dst_lba >> 16) & 0xFF;
2280 desc[26] = (dst_lba >> 8) & 0xFF;
2281 desc[27] = dst_lba & 0xFF;
2284 static void iscsi_xcopy_populate_header(unsigned char *buf, int list_id, int str,
2285 int list_id_usage, int prio,
2286 int tgt_desc_len,
2287 int seg_desc_len, int inline_data_len)
2289 buf[0] = list_id;
2290 buf[1] = ((str & 1) << 5) | ((list_id_usage & 3) << 3) | (prio & 7);
2291 buf[2] = (tgt_desc_len >> 8) & 0xFF;
2292 buf[3] = tgt_desc_len & 0xFF;
2293 buf[8] = (seg_desc_len >> 24) & 0xFF;
2294 buf[9] = (seg_desc_len >> 16) & 0xFF;
2295 buf[10] = (seg_desc_len >> 8) & 0xFF;
2296 buf[11] = seg_desc_len & 0xFF;
2297 buf[12] = (inline_data_len >> 24) & 0xFF;
2298 buf[13] = (inline_data_len >> 16) & 0xFF;
2299 buf[14] = (inline_data_len >> 8) & 0xFF;
2300 buf[15] = inline_data_len & 0xFF;
2303 static void iscsi_xcopy_data(struct iscsi_data *data,
2304 IscsiLun *src, int64_t src_lba,
2305 IscsiLun *dst, int64_t dst_lba,
2306 uint16_t num_blocks)
2308 uint8_t *buf;
2309 const int src_offset = XCOPY_DESC_OFFSET;
2310 const int dst_offset = XCOPY_DESC_OFFSET + IDENT_DESCR_TGT_DESCR_SIZE;
2311 const int seg_offset = dst_offset + IDENT_DESCR_TGT_DESCR_SIZE;
2313 data->size = XCOPY_DESC_OFFSET +
2314 IDENT_DESCR_TGT_DESCR_SIZE * 2 +
2315 XCOPY_BLK2BLK_SEG_DESC_SIZE;
2316 data->data = g_malloc0(data->size);
2317 buf = data->data;
2319 /* Initialise the parameter list header */
2320 iscsi_xcopy_populate_header(buf, 1, 0, 2 /* LIST_ID_USAGE_DISCARD */,
2321 0, 2 * IDENT_DESCR_TGT_DESCR_SIZE,
2322 XCOPY_BLK2BLK_SEG_DESC_SIZE,
2325 /* Initialise CSCD list with one src + one dst descriptor */
2326 iscsi_populate_target_desc(&buf[src_offset], src);
2327 iscsi_populate_target_desc(&buf[dst_offset], dst);
2329 /* Initialise one segment descriptor */
2330 iscsi_xcopy_populate_desc(&buf[seg_offset], 0, 0, 0, 1, num_blocks,
2331 src_lba, dst_lba);
2334 static int coroutine_fn iscsi_co_copy_range_to(BlockDriverState *bs,
2335 BdrvChild *src,
2336 uint64_t src_offset,
2337 BdrvChild *dst,
2338 uint64_t dst_offset,
2339 uint64_t bytes,
2340 BdrvRequestFlags read_flags,
2341 BdrvRequestFlags write_flags)
2343 IscsiLun *dst_lun = dst->bs->opaque;
2344 IscsiLun *src_lun;
2345 struct IscsiTask iscsi_task;
2346 struct iscsi_data data;
2347 int r = 0;
2348 int block_size;
2350 if (src->bs->drv->bdrv_co_copy_range_to != iscsi_co_copy_range_to) {
2351 return -ENOTSUP;
2353 src_lun = src->bs->opaque;
2355 if (!src_lun->dd || !dst_lun->dd) {
2356 return -ENOTSUP;
2358 if (!is_byte_request_lun_aligned(dst_offset, bytes, dst_lun)) {
2359 return -ENOTSUP;
2361 if (!is_byte_request_lun_aligned(src_offset, bytes, src_lun)) {
2362 return -ENOTSUP;
2364 if (dst_lun->block_size != src_lun->block_size ||
2365 !dst_lun->block_size) {
2366 return -ENOTSUP;
2369 block_size = dst_lun->block_size;
2370 if (bytes / block_size > 65535) {
2371 return -ENOTSUP;
2374 iscsi_xcopy_data(&data,
2375 src_lun, src_offset / block_size,
2376 dst_lun, dst_offset / block_size,
2377 bytes / block_size);
2379 iscsi_co_init_iscsitask(dst_lun, &iscsi_task);
2381 qemu_mutex_lock(&dst_lun->mutex);
2382 iscsi_task.task = iscsi_xcopy_task(data.size);
2383 retry:
2384 if (iscsi_scsi_command_async(dst_lun->iscsi, dst_lun->lun,
2385 iscsi_task.task, iscsi_co_generic_cb,
2386 &data,
2387 &iscsi_task) != 0) {
2388 r = -EIO;
2389 goto out_unlock;
2392 iscsi_co_wait_for_task(&iscsi_task, dst_lun);
2394 if (iscsi_task.do_retry) {
2395 iscsi_task.complete = 0;
2396 goto retry;
2399 if (iscsi_task.status != SCSI_STATUS_GOOD) {
2400 r = iscsi_task.err_code;
2401 goto out_unlock;
2404 out_unlock:
2406 trace_iscsi_xcopy(src_lun, src_offset, dst_lun, dst_offset, bytes, r);
2407 g_free(iscsi_task.task);
2408 qemu_mutex_unlock(&dst_lun->mutex);
2409 g_free(iscsi_task.err_str);
2410 return r;
2413 static QemuOptsList iscsi_create_opts = {
2414 .name = "iscsi-create-opts",
2415 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
2416 .desc = {
2418 .name = BLOCK_OPT_SIZE,
2419 .type = QEMU_OPT_SIZE,
2420 .help = "Virtual disk size"
2422 { /* end of list */ }
2426 static BlockDriver bdrv_iscsi = {
2427 .format_name = "iscsi",
2428 .protocol_name = "iscsi",
2430 .instance_size = sizeof(IscsiLun),
2431 .bdrv_parse_filename = iscsi_parse_filename,
2432 .bdrv_file_open = iscsi_open,
2433 .bdrv_close = iscsi_close,
2434 .bdrv_co_create_opts = iscsi_co_create_opts,
2435 .create_opts = &iscsi_create_opts,
2436 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2437 .bdrv_reopen_commit = iscsi_reopen_commit,
2438 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache,
2440 .bdrv_getlength = iscsi_getlength,
2441 .bdrv_get_info = iscsi_get_info,
2442 .bdrv_co_truncate = iscsi_co_truncate,
2443 .bdrv_refresh_limits = iscsi_refresh_limits,
2445 .bdrv_co_block_status = iscsi_co_block_status,
2446 .bdrv_co_pdiscard = iscsi_co_pdiscard,
2447 .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
2448 .bdrv_co_copy_range_to = iscsi_co_copy_range_to,
2449 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2450 .bdrv_co_readv = iscsi_co_readv,
2451 .bdrv_co_writev = iscsi_co_writev,
2452 .bdrv_co_flush_to_disk = iscsi_co_flush,
2454 #ifdef __linux__
2455 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2456 #endif
2458 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2459 .bdrv_attach_aio_context = iscsi_attach_aio_context,
2462 #if LIBISCSI_API_VERSION >= (20160603)
2463 static BlockDriver bdrv_iser = {
2464 .format_name = "iser",
2465 .protocol_name = "iser",
2467 .instance_size = sizeof(IscsiLun),
2468 .bdrv_parse_filename = iscsi_parse_filename,
2469 .bdrv_file_open = iscsi_open,
2470 .bdrv_close = iscsi_close,
2471 .bdrv_co_create_opts = iscsi_co_create_opts,
2472 .create_opts = &iscsi_create_opts,
2473 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2474 .bdrv_reopen_commit = iscsi_reopen_commit,
2475 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache,
2477 .bdrv_getlength = iscsi_getlength,
2478 .bdrv_get_info = iscsi_get_info,
2479 .bdrv_co_truncate = iscsi_co_truncate,
2480 .bdrv_refresh_limits = iscsi_refresh_limits,
2482 .bdrv_co_block_status = iscsi_co_block_status,
2483 .bdrv_co_pdiscard = iscsi_co_pdiscard,
2484 .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
2485 .bdrv_co_copy_range_to = iscsi_co_copy_range_to,
2486 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2487 .bdrv_co_readv = iscsi_co_readv,
2488 .bdrv_co_writev = iscsi_co_writev,
2489 .bdrv_co_flush_to_disk = iscsi_co_flush,
2491 #ifdef __linux__
2492 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2493 #endif
2495 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2496 .bdrv_attach_aio_context = iscsi_attach_aio_context,
2498 #endif
2500 static void iscsi_block_init(void)
2502 bdrv_register(&bdrv_iscsi);
2503 #if LIBISCSI_API_VERSION >= (20160603)
2504 bdrv_register(&bdrv_iser);
2505 #endif
2508 block_init(iscsi_block_init);