iscsi: Rely on block layer to break up large requests
[qemu/ar7.git] / block / iscsi.c
blob5602abdc6aae2a152c77faa33b09928f28c20064
1 /*
2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2016 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-common.h"
32 #include "qemu/config-file.h"
33 #include "qemu/error-report.h"
34 #include "qemu/bitops.h"
35 #include "qemu/bitmap.h"
36 #include "block/block_int.h"
37 #include "block/scsi.h"
38 #include "qemu/iov.h"
39 #include "sysemu/sysemu.h"
40 #include "qmp-commands.h"
41 #include "qapi/qmp/qstring.h"
42 #include "crypto/secret.h"
44 #include <iscsi/iscsi.h>
45 #include <iscsi/scsi-lowlevel.h>
47 #ifdef __linux__
48 #include <scsi/sg.h>
49 #endif
51 typedef struct IscsiLun {
52 struct iscsi_context *iscsi;
53 AioContext *aio_context;
54 int lun;
55 enum scsi_inquiry_peripheral_device_type type;
56 int block_size;
57 uint64_t num_blocks;
58 int events;
59 QEMUTimer *nop_timer;
60 QEMUTimer *event_timer;
61 struct scsi_inquiry_logical_block_provisioning lbp;
62 struct scsi_inquiry_block_limits bl;
63 unsigned char *zeroblock;
64 /* The allocmap tracks which clusters (pages) on the iSCSI target are
65 * allocated and which are not. In case a target returns zeros for
66 * unallocated pages (iscsilun->lprz) we can directly return zeros instead
67 * of reading zeros over the wire if a read request falls within an
68 * unallocated block. As there are 3 possible states we need 2 bitmaps to
69 * track. allocmap_valid keeps track if QEMU's information about a page is
70 * valid. allocmap tracks if a page is allocated or not. In case QEMU has no
71 * valid information about a page the corresponding allocmap entry should be
72 * switched to unallocated as well to force a new lookup of the allocation
73 * status as lookups are generally skipped if a page is suspect to be
74 * allocated. If a iSCSI target is opened with cache.direct = on the
75 * allocmap_valid does not exist turning all cached information invalid so
76 * that a fresh lookup is made for any page even if allocmap entry returns
77 * it's unallocated. */
78 unsigned long *allocmap;
79 unsigned long *allocmap_valid;
80 long allocmap_size;
81 int cluster_sectors;
82 bool use_16_for_rw;
83 bool write_protected;
84 bool lbpme;
85 bool lbprz;
86 bool dpofua;
87 bool has_write_same;
88 bool request_timed_out;
89 } IscsiLun;
91 typedef struct IscsiTask {
92 int status;
93 int complete;
94 int retries;
95 int do_retry;
96 struct scsi_task *task;
97 Coroutine *co;
98 QEMUBH *bh;
99 IscsiLun *iscsilun;
100 QEMUTimer retry_timer;
101 int err_code;
102 } IscsiTask;
104 typedef struct IscsiAIOCB {
105 BlockAIOCB common;
106 QEMUIOVector *qiov;
107 QEMUBH *bh;
108 IscsiLun *iscsilun;
109 struct scsi_task *task;
110 uint8_t *buf;
111 int status;
112 int64_t sector_num;
113 int nb_sectors;
114 int ret;
115 #ifdef __linux__
116 sg_io_hdr_t *ioh;
117 #endif
118 } IscsiAIOCB;
120 /* libiscsi uses time_t so its enough to process events every second */
121 #define EVENT_INTERVAL 1000
122 #define NOP_INTERVAL 5000
123 #define MAX_NOP_FAILURES 3
124 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
125 static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768};
127 /* this threshold is a trade-off knob to choose between
128 * the potential additional overhead of an extra GET_LBA_STATUS request
129 * vs. unnecessarily reading a lot of zero sectors over the wire.
130 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
131 * sectors we check the allocation status of the area covered by the
132 * request first if the allocationmap indicates that the area might be
133 * unallocated. */
134 #define ISCSI_CHECKALLOC_THRES 64
136 static void
137 iscsi_bh_cb(void *p)
139 IscsiAIOCB *acb = p;
141 qemu_bh_delete(acb->bh);
143 g_free(acb->buf);
144 acb->buf = NULL;
146 acb->common.cb(acb->common.opaque, acb->status);
148 if (acb->task != NULL) {
149 scsi_free_scsi_task(acb->task);
150 acb->task = NULL;
153 qemu_aio_unref(acb);
156 static void
157 iscsi_schedule_bh(IscsiAIOCB *acb)
159 if (acb->bh) {
160 return;
162 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
163 qemu_bh_schedule(acb->bh);
166 static void iscsi_co_generic_bh_cb(void *opaque)
168 struct IscsiTask *iTask = opaque;
169 iTask->complete = 1;
170 qemu_bh_delete(iTask->bh);
171 qemu_coroutine_enter(iTask->co);
174 static void iscsi_retry_timer_expired(void *opaque)
176 struct IscsiTask *iTask = opaque;
177 iTask->complete = 1;
178 if (iTask->co) {
179 qemu_coroutine_enter(iTask->co);
183 static inline unsigned exp_random(double mean)
185 return -mean * log((double)rand() / RAND_MAX);
188 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
189 * libiscsi 1.10.0, together with other constants we need. Use it as
190 * a hint that we have to define them ourselves if needed, to keep the
191 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
192 * the test because SCSI_STATUS_* is an enum.
194 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
195 * an enum, check against the LIBISCSI_API_VERSION macro, which was
196 * introduced in 1.11.0. If it is present, there is no need to define
197 * anything.
199 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
200 !defined(LIBISCSI_API_VERSION)
201 #define SCSI_STATUS_TASK_SET_FULL 0x28
202 #define SCSI_STATUS_TIMEOUT 0x0f000002
203 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
204 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
205 #endif
207 static int iscsi_translate_sense(struct scsi_sense *sense)
209 int ret;
211 switch (sense->key) {
212 case SCSI_SENSE_NOT_READY:
213 return -EBUSY;
214 case SCSI_SENSE_DATA_PROTECTION:
215 return -EACCES;
216 case SCSI_SENSE_COMMAND_ABORTED:
217 return -ECANCELED;
218 case SCSI_SENSE_ILLEGAL_REQUEST:
219 /* Parse ASCQ */
220 break;
221 default:
222 return -EIO;
224 switch (sense->ascq) {
225 case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR:
226 case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE:
227 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB:
228 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST:
229 ret = -EINVAL;
230 break;
231 case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE:
232 ret = -ENOSPC;
233 break;
234 case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED:
235 ret = -ENOTSUP;
236 break;
237 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT:
238 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED:
239 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN:
240 ret = -ENOMEDIUM;
241 break;
242 case SCSI_SENSE_ASCQ_WRITE_PROTECTED:
243 ret = -EACCES;
244 break;
245 default:
246 ret = -EIO;
247 break;
249 return ret;
252 static void
253 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
254 void *command_data, void *opaque)
256 struct IscsiTask *iTask = opaque;
257 struct scsi_task *task = command_data;
259 iTask->status = status;
260 iTask->do_retry = 0;
261 iTask->task = task;
263 if (status != SCSI_STATUS_GOOD) {
264 if (iTask->retries++ < ISCSI_CMD_RETRIES) {
265 if (status == SCSI_STATUS_CHECK_CONDITION
266 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
267 error_report("iSCSI CheckCondition: %s",
268 iscsi_get_error(iscsi));
269 iTask->do_retry = 1;
270 goto out;
272 if (status == SCSI_STATUS_BUSY ||
273 status == SCSI_STATUS_TIMEOUT ||
274 status == SCSI_STATUS_TASK_SET_FULL) {
275 unsigned retry_time =
276 exp_random(iscsi_retry_times[iTask->retries - 1]);
277 if (status == SCSI_STATUS_TIMEOUT) {
278 /* make sure the request is rescheduled AFTER the
279 * reconnect is initiated */
280 retry_time = EVENT_INTERVAL * 2;
281 iTask->iscsilun->request_timed_out = true;
283 error_report("iSCSI Busy/TaskSetFull/TimeOut"
284 " (retry #%u in %u ms): %s",
285 iTask->retries, retry_time,
286 iscsi_get_error(iscsi));
287 aio_timer_init(iTask->iscsilun->aio_context,
288 &iTask->retry_timer, QEMU_CLOCK_REALTIME,
289 SCALE_MS, iscsi_retry_timer_expired, iTask);
290 timer_mod(&iTask->retry_timer,
291 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time);
292 iTask->do_retry = 1;
293 return;
296 iTask->err_code = iscsi_translate_sense(&task->sense);
297 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi));
300 out:
301 if (iTask->co) {
302 iTask->bh = aio_bh_new(iTask->iscsilun->aio_context,
303 iscsi_co_generic_bh_cb, iTask);
304 qemu_bh_schedule(iTask->bh);
305 } else {
306 iTask->complete = 1;
310 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
312 *iTask = (struct IscsiTask) {
313 .co = qemu_coroutine_self(),
314 .iscsilun = iscsilun,
318 static void
319 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
320 void *private_data)
322 IscsiAIOCB *acb = private_data;
324 acb->status = -ECANCELED;
325 iscsi_schedule_bh(acb);
328 static void
329 iscsi_aio_cancel(BlockAIOCB *blockacb)
331 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
332 IscsiLun *iscsilun = acb->iscsilun;
334 if (acb->status != -EINPROGRESS) {
335 return;
338 /* send a task mgmt call to the target to cancel the task on the target */
339 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
340 iscsi_abort_task_cb, acb);
344 static const AIOCBInfo iscsi_aiocb_info = {
345 .aiocb_size = sizeof(IscsiAIOCB),
346 .cancel_async = iscsi_aio_cancel,
350 static void iscsi_process_read(void *arg);
351 static void iscsi_process_write(void *arg);
353 static void
354 iscsi_set_events(IscsiLun *iscsilun)
356 struct iscsi_context *iscsi = iscsilun->iscsi;
357 int ev = iscsi_which_events(iscsi);
359 if (ev != iscsilun->events) {
360 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi),
361 false,
362 (ev & POLLIN) ? iscsi_process_read : NULL,
363 (ev & POLLOUT) ? iscsi_process_write : NULL,
364 iscsilun);
365 iscsilun->events = ev;
369 static void iscsi_timed_check_events(void *opaque)
371 IscsiLun *iscsilun = opaque;
373 /* check for timed out requests */
374 iscsi_service(iscsilun->iscsi, 0);
376 if (iscsilun->request_timed_out) {
377 iscsilun->request_timed_out = false;
378 iscsi_reconnect(iscsilun->iscsi);
381 /* newer versions of libiscsi may return zero events. Ensure we are able
382 * to return to service once this situation changes. */
383 iscsi_set_events(iscsilun);
385 timer_mod(iscsilun->event_timer,
386 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
389 static void
390 iscsi_process_read(void *arg)
392 IscsiLun *iscsilun = arg;
393 struct iscsi_context *iscsi = iscsilun->iscsi;
395 iscsi_service(iscsi, POLLIN);
396 iscsi_set_events(iscsilun);
399 static void
400 iscsi_process_write(void *arg)
402 IscsiLun *iscsilun = arg;
403 struct iscsi_context *iscsi = iscsilun->iscsi;
405 iscsi_service(iscsi, POLLOUT);
406 iscsi_set_events(iscsilun);
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,
420 IscsiLun *iscsilun)
422 if (offset % iscsilun->block_size || count % iscsilun->block_size) {
423 error_report("iSCSI misaligned request: "
424 "iscsilun->block_size %u, offset %" PRIi64
425 ", count %d",
426 iscsilun->block_size, offset, count);
427 return false;
429 return true;
432 static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors,
433 IscsiLun *iscsilun)
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,
438 iscsilun);
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 iscsilun->allocmap_size =
455 DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks, iscsilun),
456 iscsilun->cluster_sectors);
458 iscsilun->allocmap = bitmap_try_new(iscsilun->allocmap_size);
459 if (!iscsilun->allocmap) {
460 return -ENOMEM;
463 if (open_flags & BDRV_O_NOCACHE) {
464 /* in case that cache.direct = on all allocmap entries are
465 * treated as invalid to force a relookup of the block
466 * status on every read request */
467 return 0;
470 iscsilun->allocmap_valid = bitmap_try_new(iscsilun->allocmap_size);
471 if (!iscsilun->allocmap_valid) {
472 /* if we are under memory pressure free the allocmap as well */
473 iscsi_allocmap_free(iscsilun);
474 return -ENOMEM;
477 return 0;
480 static void
481 iscsi_allocmap_update(IscsiLun *iscsilun, int64_t sector_num,
482 int nb_sectors, bool allocated, bool valid)
484 int64_t cl_num_expanded, nb_cls_expanded, cl_num_shrunk, nb_cls_shrunk;
486 if (iscsilun->allocmap == NULL) {
487 return;
489 /* expand to entirely contain all affected clusters */
490 cl_num_expanded = sector_num / iscsilun->cluster_sectors;
491 nb_cls_expanded = DIV_ROUND_UP(sector_num + nb_sectors,
492 iscsilun->cluster_sectors) - cl_num_expanded;
493 /* shrink to touch only completely contained clusters */
494 cl_num_shrunk = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors);
495 nb_cls_shrunk = (sector_num + nb_sectors) / iscsilun->cluster_sectors
496 - cl_num_shrunk;
497 if (allocated) {
498 bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded);
499 } else {
500 bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
503 if (iscsilun->allocmap_valid == NULL) {
504 return;
506 if (valid) {
507 bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
508 } else {
509 bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded,
510 nb_cls_expanded);
514 static void
515 iscsi_allocmap_set_allocated(IscsiLun *iscsilun, int64_t sector_num,
516 int nb_sectors)
518 iscsi_allocmap_update(iscsilun, sector_num, nb_sectors, true, true);
521 static void
522 iscsi_allocmap_set_unallocated(IscsiLun *iscsilun, int64_t sector_num,
523 int nb_sectors)
525 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
526 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
528 iscsi_allocmap_update(iscsilun, sector_num, nb_sectors, false, true);
531 static void iscsi_allocmap_set_invalid(IscsiLun *iscsilun, int64_t sector_num,
532 int nb_sectors)
534 iscsi_allocmap_update(iscsilun, sector_num, nb_sectors, false, false);
537 static void iscsi_allocmap_invalidate(IscsiLun *iscsilun)
539 if (iscsilun->allocmap) {
540 bitmap_zero(iscsilun->allocmap, iscsilun->allocmap_size);
542 if (iscsilun->allocmap_valid) {
543 bitmap_zero(iscsilun->allocmap_valid, iscsilun->allocmap_size);
547 static inline bool
548 iscsi_allocmap_is_allocated(IscsiLun *iscsilun, int64_t sector_num,
549 int nb_sectors)
551 unsigned long size;
552 if (iscsilun->allocmap == NULL) {
553 return true;
555 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
556 return !(find_next_bit(iscsilun->allocmap, size,
557 sector_num / iscsilun->cluster_sectors) == size);
560 static inline bool iscsi_allocmap_is_valid(IscsiLun *iscsilun,
561 int64_t sector_num, int nb_sectors)
563 unsigned long size;
564 if (iscsilun->allocmap_valid == NULL) {
565 return false;
567 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
568 return (find_next_zero_bit(iscsilun->allocmap_valid, size,
569 sector_num / iscsilun->cluster_sectors) == size);
572 static int coroutine_fn
573 iscsi_co_writev_flags(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;
582 if (fua) {
583 assert(iscsilun->dpofua);
585 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
586 return -EINVAL;
589 if (bs->bl.max_transfer) {
590 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
593 lba = sector_qemu2lun(sector_num, iscsilun);
594 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
595 iscsi_co_init_iscsitask(iscsilun, &iTask);
596 retry:
597 if (iscsilun->use_16_for_rw) {
598 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
599 NULL, num_sectors * iscsilun->block_size,
600 iscsilun->block_size, 0, 0, fua, 0, 0,
601 iscsi_co_generic_cb, &iTask);
602 } else {
603 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,
604 NULL, num_sectors * iscsilun->block_size,
605 iscsilun->block_size, 0, 0, fua, 0, 0,
606 iscsi_co_generic_cb, &iTask);
608 if (iTask.task == NULL) {
609 return -ENOMEM;
611 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
612 iov->niov);
613 while (!iTask.complete) {
614 iscsi_set_events(iscsilun);
615 qemu_coroutine_yield();
618 if (iTask.task != NULL) {
619 scsi_free_scsi_task(iTask.task);
620 iTask.task = NULL;
623 if (iTask.do_retry) {
624 iTask.complete = 0;
625 goto retry;
628 if (iTask.status != SCSI_STATUS_GOOD) {
629 iscsi_allocmap_set_invalid(iscsilun, sector_num, nb_sectors);
630 return iTask.err_code;
633 iscsi_allocmap_set_allocated(iscsilun, sector_num, nb_sectors);
635 return 0;
640 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
641 int64_t sector_num,
642 int nb_sectors, int *pnum,
643 BlockDriverState **file)
645 IscsiLun *iscsilun = bs->opaque;
646 struct scsi_get_lba_status *lbas = NULL;
647 struct scsi_lba_status_descriptor *lbasd = NULL;
648 struct IscsiTask iTask;
649 int64_t ret;
651 iscsi_co_init_iscsitask(iscsilun, &iTask);
653 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
654 ret = -EINVAL;
655 goto out;
658 /* default to all sectors allocated */
659 ret = BDRV_BLOCK_DATA;
660 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
661 *pnum = nb_sectors;
663 /* LUN does not support logical block provisioning */
664 if (!iscsilun->lbpme) {
665 goto out;
668 retry:
669 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
670 sector_qemu2lun(sector_num, iscsilun),
671 8 + 16, iscsi_co_generic_cb,
672 &iTask) == NULL) {
673 ret = -ENOMEM;
674 goto out;
677 while (!iTask.complete) {
678 iscsi_set_events(iscsilun);
679 qemu_coroutine_yield();
682 if (iTask.do_retry) {
683 if (iTask.task != NULL) {
684 scsi_free_scsi_task(iTask.task);
685 iTask.task = NULL;
687 iTask.complete = 0;
688 goto retry;
691 if (iTask.status != SCSI_STATUS_GOOD) {
692 /* in case the get_lba_status_callout fails (i.e.
693 * because the device is busy or the cmd is not
694 * supported) we pretend all blocks are allocated
695 * for backwards compatibility */
696 goto out;
699 lbas = scsi_datain_unmarshall(iTask.task);
700 if (lbas == NULL) {
701 ret = -EIO;
702 goto out;
705 lbasd = &lbas->descriptors[0];
707 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
708 ret = -EIO;
709 goto out;
712 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
714 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
715 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
716 ret &= ~BDRV_BLOCK_DATA;
717 if (iscsilun->lbprz) {
718 ret |= BDRV_BLOCK_ZERO;
722 if (ret & BDRV_BLOCK_ZERO) {
723 iscsi_allocmap_set_unallocated(iscsilun, sector_num, *pnum);
724 } else {
725 iscsi_allocmap_set_allocated(iscsilun, sector_num, *pnum);
728 if (*pnum > nb_sectors) {
729 *pnum = nb_sectors;
731 out:
732 if (iTask.task != NULL) {
733 scsi_free_scsi_task(iTask.task);
735 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) {
736 *file = bs;
738 return ret;
741 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
742 int64_t sector_num, int nb_sectors,
743 QEMUIOVector *iov)
745 IscsiLun *iscsilun = bs->opaque;
746 struct IscsiTask iTask;
747 uint64_t lba;
748 uint32_t num_sectors;
750 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
751 return -EINVAL;
754 if (bs->bl.max_transfer) {
755 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
758 /* if cache.direct is off and we have a valid entry in our allocation map
759 * we can skip checking the block status and directly return zeroes if
760 * the request falls within an unallocated area */
761 if (iscsi_allocmap_is_valid(iscsilun, sector_num, nb_sectors) &&
762 !iscsi_allocmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
763 qemu_iovec_memset(iov, 0, 0x00, iov->size);
764 return 0;
767 if (nb_sectors >= ISCSI_CHECKALLOC_THRES &&
768 !iscsi_allocmap_is_valid(iscsilun, sector_num, nb_sectors) &&
769 !iscsi_allocmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
770 int pnum;
771 BlockDriverState *file;
772 /* check the block status from the beginning of the cluster
773 * containing the start sector */
774 int64_t ret = iscsi_co_get_block_status(bs,
775 sector_num - sector_num % iscsilun->cluster_sectors,
776 BDRV_REQUEST_MAX_SECTORS, &pnum, &file);
777 if (ret < 0) {
778 return ret;
780 /* if the whole request falls into an unallocated area we can avoid
781 * to read and directly return zeroes instead */
782 if (ret & BDRV_BLOCK_ZERO &&
783 pnum >= nb_sectors + sector_num % iscsilun->cluster_sectors) {
784 qemu_iovec_memset(iov, 0, 0x00, iov->size);
785 return 0;
789 lba = sector_qemu2lun(sector_num, iscsilun);
790 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
792 iscsi_co_init_iscsitask(iscsilun, &iTask);
793 retry:
794 if (iscsilun->use_16_for_rw) {
795 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
796 num_sectors * iscsilun->block_size,
797 iscsilun->block_size, 0, 0, 0, 0, 0,
798 iscsi_co_generic_cb, &iTask);
799 } else {
800 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
801 num_sectors * iscsilun->block_size,
802 iscsilun->block_size,
803 0, 0, 0, 0, 0,
804 iscsi_co_generic_cb, &iTask);
806 if (iTask.task == NULL) {
807 return -ENOMEM;
809 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
811 while (!iTask.complete) {
812 iscsi_set_events(iscsilun);
813 qemu_coroutine_yield();
816 if (iTask.task != NULL) {
817 scsi_free_scsi_task(iTask.task);
818 iTask.task = NULL;
821 if (iTask.do_retry) {
822 iTask.complete = 0;
823 goto retry;
826 if (iTask.status != SCSI_STATUS_GOOD) {
827 return iTask.err_code;
830 return 0;
833 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
835 IscsiLun *iscsilun = bs->opaque;
836 struct IscsiTask iTask;
838 iscsi_co_init_iscsitask(iscsilun, &iTask);
839 retry:
840 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
841 0, iscsi_co_generic_cb, &iTask) == NULL) {
842 return -ENOMEM;
845 while (!iTask.complete) {
846 iscsi_set_events(iscsilun);
847 qemu_coroutine_yield();
850 if (iTask.task != NULL) {
851 scsi_free_scsi_task(iTask.task);
852 iTask.task = NULL;
855 if (iTask.do_retry) {
856 iTask.complete = 0;
857 goto retry;
860 if (iTask.status != SCSI_STATUS_GOOD) {
861 return iTask.err_code;
864 return 0;
867 #ifdef __linux__
868 static void
869 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
870 void *command_data, void *opaque)
872 IscsiAIOCB *acb = opaque;
874 g_free(acb->buf);
875 acb->buf = NULL;
877 acb->status = 0;
878 if (status < 0) {
879 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
880 iscsi_get_error(iscsi));
881 acb->status = iscsi_translate_sense(&acb->task->sense);
884 acb->ioh->driver_status = 0;
885 acb->ioh->host_status = 0;
886 acb->ioh->resid = 0;
887 acb->ioh->status = status;
889 #define SG_ERR_DRIVER_SENSE 0x08
891 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
892 int ss;
894 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
896 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
897 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
898 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
899 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
902 iscsi_schedule_bh(acb);
905 static void iscsi_ioctl_bh_completion(void *opaque)
907 IscsiAIOCB *acb = opaque;
909 qemu_bh_delete(acb->bh);
910 acb->common.cb(acb->common.opaque, acb->ret);
911 qemu_aio_unref(acb);
914 static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf)
916 BlockDriverState *bs = acb->common.bs;
917 IscsiLun *iscsilun = bs->opaque;
918 int ret = 0;
920 switch (req) {
921 case SG_GET_VERSION_NUM:
922 *(int *)buf = 30000;
923 break;
924 case SG_GET_SCSI_ID:
925 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
926 break;
927 default:
928 ret = -EINVAL;
930 assert(!acb->bh);
931 acb->bh = aio_bh_new(bdrv_get_aio_context(bs),
932 iscsi_ioctl_bh_completion, acb);
933 acb->ret = ret;
934 qemu_bh_schedule(acb->bh);
937 static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
938 unsigned long int req, void *buf,
939 BlockCompletionFunc *cb, void *opaque)
941 IscsiLun *iscsilun = bs->opaque;
942 struct iscsi_context *iscsi = iscsilun->iscsi;
943 struct iscsi_data data;
944 IscsiAIOCB *acb;
946 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
948 acb->iscsilun = iscsilun;
949 acb->bh = NULL;
950 acb->status = -EINPROGRESS;
951 acb->buf = NULL;
952 acb->ioh = buf;
954 if (req != SG_IO) {
955 iscsi_ioctl_handle_emulated(acb, req, buf);
956 return &acb->common;
959 if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) {
960 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
961 acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE);
962 qemu_aio_unref(acb);
963 return NULL;
966 acb->task = malloc(sizeof(struct scsi_task));
967 if (acb->task == NULL) {
968 error_report("iSCSI: Failed to allocate task for scsi command. %s",
969 iscsi_get_error(iscsi));
970 qemu_aio_unref(acb);
971 return NULL;
973 memset(acb->task, 0, sizeof(struct scsi_task));
975 switch (acb->ioh->dxfer_direction) {
976 case SG_DXFER_TO_DEV:
977 acb->task->xfer_dir = SCSI_XFER_WRITE;
978 break;
979 case SG_DXFER_FROM_DEV:
980 acb->task->xfer_dir = SCSI_XFER_READ;
981 break;
982 default:
983 acb->task->xfer_dir = SCSI_XFER_NONE;
984 break;
987 acb->task->cdb_size = acb->ioh->cmd_len;
988 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
989 acb->task->expxferlen = acb->ioh->dxfer_len;
991 data.size = 0;
992 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
993 if (acb->ioh->iovec_count == 0) {
994 data.data = acb->ioh->dxferp;
995 data.size = acb->ioh->dxfer_len;
996 } else {
997 scsi_task_set_iov_out(acb->task,
998 (struct scsi_iovec *) acb->ioh->dxferp,
999 acb->ioh->iovec_count);
1003 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
1004 iscsi_aio_ioctl_cb,
1005 (data.size > 0) ? &data : NULL,
1006 acb) != 0) {
1007 scsi_free_scsi_task(acb->task);
1008 qemu_aio_unref(acb);
1009 return NULL;
1012 /* tell libiscsi to read straight into the buffer we got from ioctl */
1013 if (acb->task->xfer_dir == SCSI_XFER_READ) {
1014 if (acb->ioh->iovec_count == 0) {
1015 scsi_task_add_data_in_buffer(acb->task,
1016 acb->ioh->dxfer_len,
1017 acb->ioh->dxferp);
1018 } else {
1019 scsi_task_set_iov_in(acb->task,
1020 (struct scsi_iovec *) acb->ioh->dxferp,
1021 acb->ioh->iovec_count);
1025 iscsi_set_events(iscsilun);
1027 return &acb->common;
1030 #endif
1032 static int64_t
1033 iscsi_getlength(BlockDriverState *bs)
1035 IscsiLun *iscsilun = bs->opaque;
1036 int64_t len;
1038 len = iscsilun->num_blocks;
1039 len *= iscsilun->block_size;
1041 return len;
1044 static int
1045 coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
1046 int nb_sectors)
1048 IscsiLun *iscsilun = bs->opaque;
1049 struct IscsiTask iTask;
1050 struct unmap_list list;
1052 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
1053 return -EINVAL;
1056 if (!iscsilun->lbp.lbpu) {
1057 /* UNMAP is not supported by the target */
1058 return 0;
1061 list.lba = sector_qemu2lun(sector_num, iscsilun);
1062 list.num = sector_qemu2lun(nb_sectors, iscsilun);
1064 iscsi_co_init_iscsitask(iscsilun, &iTask);
1065 retry:
1066 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
1067 iscsi_co_generic_cb, &iTask) == NULL) {
1068 return -ENOMEM;
1071 while (!iTask.complete) {
1072 iscsi_set_events(iscsilun);
1073 qemu_coroutine_yield();
1076 if (iTask.task != NULL) {
1077 scsi_free_scsi_task(iTask.task);
1078 iTask.task = NULL;
1081 if (iTask.do_retry) {
1082 iTask.complete = 0;
1083 goto retry;
1086 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
1087 /* the target might fail with a check condition if it
1088 is not happy with the alignment of the UNMAP request
1089 we silently fail in this case */
1090 return 0;
1093 if (iTask.status != SCSI_STATUS_GOOD) {
1094 return iTask.err_code;
1097 iscsi_allocmap_set_invalid(iscsilun, sector_num, nb_sectors);
1099 return 0;
1102 static int
1103 coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1104 int count, BdrvRequestFlags flags)
1106 IscsiLun *iscsilun = bs->opaque;
1107 struct IscsiTask iTask;
1108 uint64_t lba;
1109 uint32_t nb_blocks;
1110 bool use_16_for_ws = iscsilun->use_16_for_rw;
1112 if (!is_byte_request_lun_aligned(offset, count, iscsilun)) {
1113 return -ENOTSUP;
1116 if (flags & BDRV_REQ_MAY_UNMAP) {
1117 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
1118 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1119 use_16_for_ws = true;
1121 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
1122 /* WRITESAME16 with UNMAP is not supported by the target,
1123 * fall back and try WRITESAME10/16 without UNMAP */
1124 flags &= ~BDRV_REQ_MAY_UNMAP;
1125 use_16_for_ws = iscsilun->use_16_for_rw;
1129 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
1130 /* WRITESAME without UNMAP is not supported by the target */
1131 return -ENOTSUP;
1134 lba = offset / iscsilun->block_size;
1135 nb_blocks = count / iscsilun->block_size;
1137 if (iscsilun->zeroblock == NULL) {
1138 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size);
1139 if (iscsilun->zeroblock == NULL) {
1140 return -ENOMEM;
1144 iscsi_co_init_iscsitask(iscsilun, &iTask);
1145 retry:
1146 if (use_16_for_ws) {
1147 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1148 iscsilun->zeroblock, iscsilun->block_size,
1149 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1150 0, 0, iscsi_co_generic_cb, &iTask);
1151 } else {
1152 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
1153 iscsilun->zeroblock, iscsilun->block_size,
1154 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1155 0, 0, iscsi_co_generic_cb, &iTask);
1157 if (iTask.task == NULL) {
1158 return -ENOMEM;
1161 while (!iTask.complete) {
1162 iscsi_set_events(iscsilun);
1163 qemu_coroutine_yield();
1166 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1167 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
1168 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
1169 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
1170 /* WRITE SAME is not supported by the target */
1171 iscsilun->has_write_same = false;
1172 scsi_free_scsi_task(iTask.task);
1173 return -ENOTSUP;
1176 if (iTask.task != NULL) {
1177 scsi_free_scsi_task(iTask.task);
1178 iTask.task = NULL;
1181 if (iTask.do_retry) {
1182 iTask.complete = 0;
1183 goto retry;
1186 if (iTask.status != SCSI_STATUS_GOOD) {
1187 iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS,
1188 count >> BDRV_SECTOR_BITS);
1189 return iTask.err_code;
1192 if (flags & BDRV_REQ_MAY_UNMAP) {
1193 iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS,
1194 count >> BDRV_SECTOR_BITS);
1195 } else {
1196 iscsi_allocmap_set_allocated(iscsilun, offset >> BDRV_SECTOR_BITS,
1197 count >> BDRV_SECTOR_BITS);
1200 return 0;
1203 static void parse_chap(struct iscsi_context *iscsi, const char *target,
1204 Error **errp)
1206 QemuOptsList *list;
1207 QemuOpts *opts;
1208 const char *user = NULL;
1209 const char *password = NULL;
1210 const char *secretid;
1211 char *secret = NULL;
1213 list = qemu_find_opts("iscsi");
1214 if (!list) {
1215 return;
1218 opts = qemu_opts_find(list, target);
1219 if (opts == NULL) {
1220 opts = QTAILQ_FIRST(&list->head);
1221 if (!opts) {
1222 return;
1226 user = qemu_opt_get(opts, "user");
1227 if (!user) {
1228 return;
1231 secretid = qemu_opt_get(opts, "password-secret");
1232 password = qemu_opt_get(opts, "password");
1233 if (secretid && password) {
1234 error_setg(errp, "'password' and 'password-secret' properties are "
1235 "mutually exclusive");
1236 return;
1238 if (secretid) {
1239 secret = qcrypto_secret_lookup_as_utf8(secretid, errp);
1240 if (!secret) {
1241 return;
1243 password = secret;
1244 } else if (!password) {
1245 error_setg(errp, "CHAP username specified but no password was given");
1246 return;
1249 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
1250 error_setg(errp, "Failed to set initiator username and password");
1253 g_free(secret);
1256 static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
1257 Error **errp)
1259 QemuOptsList *list;
1260 QemuOpts *opts;
1261 const char *digest = NULL;
1263 list = qemu_find_opts("iscsi");
1264 if (!list) {
1265 return;
1268 opts = qemu_opts_find(list, target);
1269 if (opts == NULL) {
1270 opts = QTAILQ_FIRST(&list->head);
1271 if (!opts) {
1272 return;
1276 digest = qemu_opt_get(opts, "header-digest");
1277 if (!digest) {
1278 return;
1281 if (!strcmp(digest, "CRC32C")) {
1282 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1283 } else if (!strcmp(digest, "NONE")) {
1284 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1285 } else if (!strcmp(digest, "CRC32C-NONE")) {
1286 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1287 } else if (!strcmp(digest, "NONE-CRC32C")) {
1288 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1289 } else {
1290 error_setg(errp, "Invalid header-digest setting : %s", digest);
1294 static char *parse_initiator_name(const char *target)
1296 QemuOptsList *list;
1297 QemuOpts *opts;
1298 const char *name;
1299 char *iscsi_name;
1300 UuidInfo *uuid_info;
1302 list = qemu_find_opts("iscsi");
1303 if (list) {
1304 opts = qemu_opts_find(list, target);
1305 if (!opts) {
1306 opts = QTAILQ_FIRST(&list->head);
1308 if (opts) {
1309 name = qemu_opt_get(opts, "initiator-name");
1310 if (name) {
1311 return g_strdup(name);
1316 uuid_info = qmp_query_uuid(NULL);
1317 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1318 name = qemu_get_vm_name();
1319 } else {
1320 name = uuid_info->UUID;
1322 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1323 name ? ":" : "", name ? name : "");
1324 qapi_free_UuidInfo(uuid_info);
1325 return iscsi_name;
1328 static int parse_timeout(const char *target)
1330 QemuOptsList *list;
1331 QemuOpts *opts;
1332 const char *timeout;
1334 list = qemu_find_opts("iscsi");
1335 if (list) {
1336 opts = qemu_opts_find(list, target);
1337 if (!opts) {
1338 opts = QTAILQ_FIRST(&list->head);
1340 if (opts) {
1341 timeout = qemu_opt_get(opts, "timeout");
1342 if (timeout) {
1343 return atoi(timeout);
1348 return 0;
1351 static void iscsi_nop_timed_event(void *opaque)
1353 IscsiLun *iscsilun = opaque;
1355 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) {
1356 error_report("iSCSI: NOP timeout. Reconnecting...");
1357 iscsilun->request_timed_out = true;
1358 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1359 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1360 return;
1363 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1364 iscsi_set_events(iscsilun);
1367 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
1369 struct scsi_task *task = NULL;
1370 struct scsi_readcapacity10 *rc10 = NULL;
1371 struct scsi_readcapacity16 *rc16 = NULL;
1372 int retries = ISCSI_CMD_RETRIES;
1374 do {
1375 if (task != NULL) {
1376 scsi_free_scsi_task(task);
1377 task = NULL;
1380 switch (iscsilun->type) {
1381 case TYPE_DISK:
1382 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1383 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1384 rc16 = scsi_datain_unmarshall(task);
1385 if (rc16 == NULL) {
1386 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1387 } else {
1388 iscsilun->block_size = rc16->block_length;
1389 iscsilun->num_blocks = rc16->returned_lba + 1;
1390 iscsilun->lbpme = !!rc16->lbpme;
1391 iscsilun->lbprz = !!rc16->lbprz;
1392 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
1394 break;
1396 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1397 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
1398 break;
1400 /* Fall through and try READ CAPACITY(10) instead. */
1401 case TYPE_ROM:
1402 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1403 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1404 rc10 = scsi_datain_unmarshall(task);
1405 if (rc10 == NULL) {
1406 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1407 } else {
1408 iscsilun->block_size = rc10->block_size;
1409 if (rc10->lba == 0) {
1410 /* blank disk loaded */
1411 iscsilun->num_blocks = 0;
1412 } else {
1413 iscsilun->num_blocks = rc10->lba + 1;
1417 break;
1418 default:
1419 return;
1421 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1422 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1423 && retries-- > 0);
1425 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1426 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command");
1427 } else if (!iscsilun->block_size ||
1428 iscsilun->block_size % BDRV_SECTOR_SIZE) {
1429 error_setg(errp, "iSCSI: the target returned an invalid "
1430 "block size of %d.", iscsilun->block_size);
1432 if (task) {
1433 scsi_free_scsi_task(task);
1437 /* TODO Convert to fine grained options */
1438 static QemuOptsList runtime_opts = {
1439 .name = "iscsi",
1440 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1441 .desc = {
1443 .name = "filename",
1444 .type = QEMU_OPT_STRING,
1445 .help = "URL to the iscsi image",
1447 { /* end of list */ }
1451 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
1452 int evpd, int pc, void **inq, Error **errp)
1454 int full_size;
1455 struct scsi_task *task = NULL;
1456 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1457 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1458 goto fail;
1460 full_size = scsi_datain_getfullsize(task);
1461 if (full_size > task->datain.size) {
1462 scsi_free_scsi_task(task);
1464 /* we need more data for the full list */
1465 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1466 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1467 goto fail;
1471 *inq = scsi_datain_unmarshall(task);
1472 if (*inq == NULL) {
1473 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1474 goto fail_with_err;
1477 return task;
1479 fail:
1480 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1481 iscsi_get_error(iscsi));
1482 fail_with_err:
1483 if (task != NULL) {
1484 scsi_free_scsi_task(task);
1486 return NULL;
1489 static void iscsi_detach_aio_context(BlockDriverState *bs)
1491 IscsiLun *iscsilun = bs->opaque;
1493 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi),
1494 false, NULL, NULL, NULL);
1495 iscsilun->events = 0;
1497 if (iscsilun->nop_timer) {
1498 timer_del(iscsilun->nop_timer);
1499 timer_free(iscsilun->nop_timer);
1500 iscsilun->nop_timer = NULL;
1502 if (iscsilun->event_timer) {
1503 timer_del(iscsilun->event_timer);
1504 timer_free(iscsilun->event_timer);
1505 iscsilun->event_timer = NULL;
1509 static void iscsi_attach_aio_context(BlockDriverState *bs,
1510 AioContext *new_context)
1512 IscsiLun *iscsilun = bs->opaque;
1514 iscsilun->aio_context = new_context;
1515 iscsi_set_events(iscsilun);
1517 /* Set up a timer for sending out iSCSI NOPs */
1518 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1519 QEMU_CLOCK_REALTIME, SCALE_MS,
1520 iscsi_nop_timed_event, iscsilun);
1521 timer_mod(iscsilun->nop_timer,
1522 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1524 /* Set up a timer for periodic calls to iscsi_set_events and to
1525 * scan for command timeout */
1526 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context,
1527 QEMU_CLOCK_REALTIME, SCALE_MS,
1528 iscsi_timed_check_events, iscsilun);
1529 timer_mod(iscsilun->event_timer,
1530 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
1533 static void iscsi_modesense_sync(IscsiLun *iscsilun)
1535 struct scsi_task *task;
1536 struct scsi_mode_sense *ms = NULL;
1537 iscsilun->write_protected = false;
1538 iscsilun->dpofua = false;
1540 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1541 1, SCSI_MODESENSE_PC_CURRENT,
1542 0x3F, 0, 255);
1543 if (task == NULL) {
1544 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1545 iscsi_get_error(iscsilun->iscsi));
1546 goto out;
1549 if (task->status != SCSI_STATUS_GOOD) {
1550 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1551 goto out;
1553 ms = scsi_datain_unmarshall(task);
1554 if (!ms) {
1555 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1556 iscsi_get_error(iscsilun->iscsi));
1557 goto out;
1559 iscsilun->write_protected = ms->device_specific_parameter & 0x80;
1560 iscsilun->dpofua = ms->device_specific_parameter & 0x10;
1562 out:
1563 if (task) {
1564 scsi_free_scsi_task(task);
1569 * We support iscsi url's on the form
1570 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1572 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1573 Error **errp)
1575 IscsiLun *iscsilun = bs->opaque;
1576 struct iscsi_context *iscsi = NULL;
1577 struct iscsi_url *iscsi_url = NULL;
1578 struct scsi_task *task = NULL;
1579 struct scsi_inquiry_standard *inq = NULL;
1580 struct scsi_inquiry_supported_pages *inq_vpd;
1581 char *initiator_name = NULL;
1582 QemuOpts *opts;
1583 Error *local_err = NULL;
1584 const char *filename;
1585 int i, ret = 0, timeout = 0;
1587 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1588 qemu_opts_absorb_qdict(opts, options, &local_err);
1589 if (local_err) {
1590 error_propagate(errp, local_err);
1591 ret = -EINVAL;
1592 goto out;
1595 filename = qemu_opt_get(opts, "filename");
1597 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1598 if (iscsi_url == NULL) {
1599 error_setg(errp, "Failed to parse URL : %s", filename);
1600 ret = -EINVAL;
1601 goto out;
1604 memset(iscsilun, 0, sizeof(IscsiLun));
1606 initiator_name = parse_initiator_name(iscsi_url->target);
1608 iscsi = iscsi_create_context(initiator_name);
1609 if (iscsi == NULL) {
1610 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
1611 ret = -ENOMEM;
1612 goto out;
1615 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1616 error_setg(errp, "iSCSI: Failed to set target name.");
1617 ret = -EINVAL;
1618 goto out;
1621 if (iscsi_url->user[0] != '\0') {
1622 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1623 iscsi_url->passwd);
1624 if (ret != 0) {
1625 error_setg(errp, "Failed to set initiator username and password");
1626 ret = -EINVAL;
1627 goto out;
1631 /* check if we got CHAP username/password via the options */
1632 parse_chap(iscsi, iscsi_url->target, &local_err);
1633 if (local_err != NULL) {
1634 error_propagate(errp, local_err);
1635 ret = -EINVAL;
1636 goto out;
1639 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1640 error_setg(errp, "iSCSI: Failed to set session type to normal.");
1641 ret = -EINVAL;
1642 goto out;
1645 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1647 /* check if we got HEADER_DIGEST via the options */
1648 parse_header_digest(iscsi, iscsi_url->target, &local_err);
1649 if (local_err != NULL) {
1650 error_propagate(errp, local_err);
1651 ret = -EINVAL;
1652 goto out;
1655 /* timeout handling is broken in libiscsi before 1.15.0 */
1656 timeout = parse_timeout(iscsi_url->target);
1657 #if defined(LIBISCSI_API_VERSION) && LIBISCSI_API_VERSION >= 20150621
1658 iscsi_set_timeout(iscsi, timeout);
1659 #else
1660 if (timeout) {
1661 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1663 #endif
1665 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1666 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
1667 iscsi_get_error(iscsi));
1668 ret = -EINVAL;
1669 goto out;
1672 iscsilun->iscsi = iscsi;
1673 iscsilun->aio_context = bdrv_get_aio_context(bs);
1674 iscsilun->lun = iscsi_url->lun;
1675 iscsilun->has_write_same = true;
1677 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1678 (void **) &inq, errp);
1679 if (task == NULL) {
1680 ret = -EINVAL;
1681 goto out;
1683 iscsilun->type = inq->periperal_device_type;
1684 scsi_free_scsi_task(task);
1685 task = NULL;
1687 iscsi_modesense_sync(iscsilun);
1688 if (iscsilun->dpofua) {
1689 bs->supported_write_flags = BDRV_REQ_FUA;
1691 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
1693 /* Check the write protect flag of the LUN if we want to write */
1694 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) &&
1695 iscsilun->write_protected) {
1696 error_setg(errp, "Cannot open a write protected LUN as read-write");
1697 ret = -EACCES;
1698 goto out;
1701 iscsi_readcapacity_sync(iscsilun, &local_err);
1702 if (local_err != NULL) {
1703 error_propagate(errp, local_err);
1704 ret = -EINVAL;
1705 goto out;
1707 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1709 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1710 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1711 * will try to read from the device to guess the image format.
1713 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
1714 bs->sg = true;
1717 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1718 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1719 (void **) &inq_vpd, errp);
1720 if (task == NULL) {
1721 ret = -EINVAL;
1722 goto out;
1724 for (i = 0; i < inq_vpd->num_pages; i++) {
1725 struct scsi_task *inq_task;
1726 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1727 struct scsi_inquiry_block_limits *inq_bl;
1728 switch (inq_vpd->pages[i]) {
1729 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1730 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1731 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1732 (void **) &inq_lbp, errp);
1733 if (inq_task == NULL) {
1734 ret = -EINVAL;
1735 goto out;
1737 memcpy(&iscsilun->lbp, inq_lbp,
1738 sizeof(struct scsi_inquiry_logical_block_provisioning));
1739 scsi_free_scsi_task(inq_task);
1740 break;
1741 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1742 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1743 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1744 (void **) &inq_bl, errp);
1745 if (inq_task == NULL) {
1746 ret = -EINVAL;
1747 goto out;
1749 memcpy(&iscsilun->bl, inq_bl,
1750 sizeof(struct scsi_inquiry_block_limits));
1751 scsi_free_scsi_task(inq_task);
1752 break;
1753 default:
1754 break;
1757 scsi_free_scsi_task(task);
1758 task = NULL;
1760 iscsi_attach_aio_context(bs, iscsilun->aio_context);
1762 /* Guess the internal cluster (page) size of the iscsi target by the means
1763 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1764 * reasonable size */
1765 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
1766 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1767 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
1768 iscsilun->block_size) >> BDRV_SECTOR_BITS;
1769 if (iscsilun->lbprz) {
1770 ret = iscsi_allocmap_init(iscsilun, bs->open_flags);
1774 out:
1775 qemu_opts_del(opts);
1776 g_free(initiator_name);
1777 if (iscsi_url != NULL) {
1778 iscsi_destroy_url(iscsi_url);
1780 if (task != NULL) {
1781 scsi_free_scsi_task(task);
1784 if (ret) {
1785 if (iscsi != NULL) {
1786 if (iscsi_is_logged_in(iscsi)) {
1787 iscsi_logout_sync(iscsi);
1789 iscsi_destroy_context(iscsi);
1791 memset(iscsilun, 0, sizeof(IscsiLun));
1793 return ret;
1796 static void iscsi_close(BlockDriverState *bs)
1798 IscsiLun *iscsilun = bs->opaque;
1799 struct iscsi_context *iscsi = iscsilun->iscsi;
1801 iscsi_detach_aio_context(bs);
1802 if (iscsi_is_logged_in(iscsi)) {
1803 iscsi_logout_sync(iscsi);
1805 iscsi_destroy_context(iscsi);
1806 g_free(iscsilun->zeroblock);
1807 iscsi_allocmap_free(iscsilun);
1808 memset(iscsilun, 0, sizeof(IscsiLun));
1811 static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)
1813 /* We don't actually refresh here, but just return data queried in
1814 * iscsi_open(): iscsi targets don't change their limits. */
1816 IscsiLun *iscsilun = bs->opaque;
1817 uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff;
1819 bs->bl.request_alignment = iscsilun->block_size;
1821 if (iscsilun->bl.max_xfer_len) {
1822 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len);
1825 if (max_xfer_len * iscsilun->block_size < INT_MAX) {
1826 bs->bl.max_transfer = max_xfer_len * iscsilun->block_size;
1829 if (iscsilun->lbp.lbpu) {
1830 if (iscsilun->bl.max_unmap < 0xffffffff / iscsilun->block_size) {
1831 bs->bl.max_pdiscard =
1832 iscsilun->bl.max_unmap * iscsilun->block_size;
1834 bs->bl.pdiscard_alignment =
1835 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
1836 } else {
1837 bs->bl.pdiscard_alignment = iscsilun->block_size;
1840 if (iscsilun->bl.max_ws_len < 0xffffffff / iscsilun->block_size) {
1841 bs->bl.max_pwrite_zeroes =
1842 iscsilun->bl.max_ws_len * iscsilun->block_size;
1844 if (iscsilun->lbp.lbpws) {
1845 bs->bl.pwrite_zeroes_alignment =
1846 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
1847 } else {
1848 bs->bl.pwrite_zeroes_alignment = iscsilun->block_size;
1850 if (iscsilun->bl.opt_xfer_len &&
1851 iscsilun->bl.opt_xfer_len < INT_MAX / iscsilun->block_size) {
1852 bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len *
1853 iscsilun->block_size);
1857 /* Note that this will not re-establish a connection with an iSCSI target - it
1858 * is effectively a NOP. */
1859 static int iscsi_reopen_prepare(BDRVReopenState *state,
1860 BlockReopenQueue *queue, Error **errp)
1862 IscsiLun *iscsilun = state->bs->opaque;
1864 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) {
1865 error_setg(errp, "Cannot open a write protected LUN as read-write");
1866 return -EACCES;
1868 return 0;
1871 static void iscsi_reopen_commit(BDRVReopenState *reopen_state)
1873 IscsiLun *iscsilun = reopen_state->bs->opaque;
1875 /* the cache.direct status might have changed */
1876 if (iscsilun->allocmap != NULL) {
1877 iscsi_allocmap_init(iscsilun, reopen_state->flags);
1881 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1883 IscsiLun *iscsilun = bs->opaque;
1884 Error *local_err = NULL;
1886 if (iscsilun->type != TYPE_DISK) {
1887 return -ENOTSUP;
1890 iscsi_readcapacity_sync(iscsilun, &local_err);
1891 if (local_err != NULL) {
1892 error_free(local_err);
1893 return -EIO;
1896 if (offset > iscsi_getlength(bs)) {
1897 return -EINVAL;
1900 if (iscsilun->allocmap != NULL) {
1901 iscsi_allocmap_init(iscsilun, bs->open_flags);
1904 return 0;
1907 static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp)
1909 int ret = 0;
1910 int64_t total_size = 0;
1911 BlockDriverState *bs;
1912 IscsiLun *iscsilun = NULL;
1913 QDict *bs_options;
1915 bs = bdrv_new();
1917 /* Read out options */
1918 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1919 BDRV_SECTOR_SIZE);
1920 bs->opaque = g_new0(struct IscsiLun, 1);
1921 iscsilun = bs->opaque;
1923 bs_options = qdict_new();
1924 qdict_put(bs_options, "filename", qstring_from_str(filename));
1925 ret = iscsi_open(bs, bs_options, 0, NULL);
1926 QDECREF(bs_options);
1928 if (ret != 0) {
1929 goto out;
1931 iscsi_detach_aio_context(bs);
1932 if (iscsilun->type != TYPE_DISK) {
1933 ret = -ENODEV;
1934 goto out;
1936 if (bs->total_sectors < total_size) {
1937 ret = -ENOSPC;
1938 goto out;
1941 ret = 0;
1942 out:
1943 if (iscsilun->iscsi != NULL) {
1944 iscsi_destroy_context(iscsilun->iscsi);
1946 g_free(bs->opaque);
1947 bs->opaque = NULL;
1948 bdrv_unref(bs);
1949 return ret;
1952 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1954 IscsiLun *iscsilun = bs->opaque;
1955 bdi->unallocated_blocks_are_zero = iscsilun->lbprz;
1956 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
1957 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
1958 return 0;
1961 static void iscsi_invalidate_cache(BlockDriverState *bs,
1962 Error **errp)
1964 IscsiLun *iscsilun = bs->opaque;
1965 iscsi_allocmap_invalidate(iscsilun);
1968 static QemuOptsList iscsi_create_opts = {
1969 .name = "iscsi-create-opts",
1970 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
1971 .desc = {
1973 .name = BLOCK_OPT_SIZE,
1974 .type = QEMU_OPT_SIZE,
1975 .help = "Virtual disk size"
1977 { /* end of list */ }
1981 static BlockDriver bdrv_iscsi = {
1982 .format_name = "iscsi",
1983 .protocol_name = "iscsi",
1985 .instance_size = sizeof(IscsiLun),
1986 .bdrv_needs_filename = true,
1987 .bdrv_file_open = iscsi_open,
1988 .bdrv_close = iscsi_close,
1989 .bdrv_create = iscsi_create,
1990 .create_opts = &iscsi_create_opts,
1991 .bdrv_reopen_prepare = iscsi_reopen_prepare,
1992 .bdrv_reopen_commit = iscsi_reopen_commit,
1993 .bdrv_invalidate_cache = iscsi_invalidate_cache,
1995 .bdrv_getlength = iscsi_getlength,
1996 .bdrv_get_info = iscsi_get_info,
1997 .bdrv_truncate = iscsi_truncate,
1998 .bdrv_refresh_limits = iscsi_refresh_limits,
2000 .bdrv_co_get_block_status = iscsi_co_get_block_status,
2001 .bdrv_co_discard = iscsi_co_discard,
2002 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2003 .bdrv_co_readv = iscsi_co_readv,
2004 .bdrv_co_writev_flags = iscsi_co_writev_flags,
2005 .bdrv_co_flush_to_disk = iscsi_co_flush,
2007 #ifdef __linux__
2008 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2009 #endif
2011 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2012 .bdrv_attach_aio_context = iscsi_attach_aio_context,
2015 static QemuOptsList qemu_iscsi_opts = {
2016 .name = "iscsi",
2017 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
2018 .desc = {
2020 .name = "user",
2021 .type = QEMU_OPT_STRING,
2022 .help = "username for CHAP authentication to target",
2024 .name = "password",
2025 .type = QEMU_OPT_STRING,
2026 .help = "password for CHAP authentication to target",
2028 .name = "password-secret",
2029 .type = QEMU_OPT_STRING,
2030 .help = "ID of the secret providing password for CHAP "
2031 "authentication to target",
2033 .name = "header-digest",
2034 .type = QEMU_OPT_STRING,
2035 .help = "HeaderDigest setting. "
2036 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
2038 .name = "initiator-name",
2039 .type = QEMU_OPT_STRING,
2040 .help = "Initiator iqn name to use when connecting",
2042 .name = "timeout",
2043 .type = QEMU_OPT_NUMBER,
2044 .help = "Request timeout in seconds (default 0 = no timeout)",
2046 { /* end of list */ }
2050 static void iscsi_block_init(void)
2052 bdrv_register(&bdrv_iscsi);
2053 qemu_add_opts(&qemu_iscsi_opts);
2056 block_init(iscsi_block_init);