ioapic: register IOMMU IEC notifier for ioapic
[qemu/cris-port.git] / block / iscsi.c
blob129c3afa68547c64c67e5e2f54c3932840cb4c45
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 nb_sectors << BDRV_SECTOR_BITS > bs->bl.max_transfer) {
591 error_report("iSCSI Error: Write of %d sectors exceeds max_xfer_len "
592 "of %" PRIu32 " bytes", nb_sectors, bs->bl.max_transfer);
593 return -EINVAL;
596 lba = sector_qemu2lun(sector_num, iscsilun);
597 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
598 iscsi_co_init_iscsitask(iscsilun, &iTask);
599 retry:
600 if (iscsilun->use_16_for_rw) {
601 iTask.task = iscsi_write16_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 } else {
606 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,
607 NULL, num_sectors * iscsilun->block_size,
608 iscsilun->block_size, 0, 0, fua, 0, 0,
609 iscsi_co_generic_cb, &iTask);
611 if (iTask.task == NULL) {
612 return -ENOMEM;
614 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
615 iov->niov);
616 while (!iTask.complete) {
617 iscsi_set_events(iscsilun);
618 qemu_coroutine_yield();
621 if (iTask.task != NULL) {
622 scsi_free_scsi_task(iTask.task);
623 iTask.task = NULL;
626 if (iTask.do_retry) {
627 iTask.complete = 0;
628 goto retry;
631 if (iTask.status != SCSI_STATUS_GOOD) {
632 iscsi_allocmap_set_invalid(iscsilun, sector_num, nb_sectors);
633 return iTask.err_code;
636 iscsi_allocmap_set_allocated(iscsilun, sector_num, nb_sectors);
638 return 0;
643 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
644 int64_t sector_num,
645 int nb_sectors, int *pnum,
646 BlockDriverState **file)
648 IscsiLun *iscsilun = bs->opaque;
649 struct scsi_get_lba_status *lbas = NULL;
650 struct scsi_lba_status_descriptor *lbasd = NULL;
651 struct IscsiTask iTask;
652 int64_t ret;
654 iscsi_co_init_iscsitask(iscsilun, &iTask);
656 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
657 ret = -EINVAL;
658 goto out;
661 /* default to all sectors allocated */
662 ret = BDRV_BLOCK_DATA;
663 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
664 *pnum = nb_sectors;
666 /* LUN does not support logical block provisioning */
667 if (!iscsilun->lbpme) {
668 goto out;
671 retry:
672 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
673 sector_qemu2lun(sector_num, iscsilun),
674 8 + 16, iscsi_co_generic_cb,
675 &iTask) == NULL) {
676 ret = -ENOMEM;
677 goto out;
680 while (!iTask.complete) {
681 iscsi_set_events(iscsilun);
682 qemu_coroutine_yield();
685 if (iTask.do_retry) {
686 if (iTask.task != NULL) {
687 scsi_free_scsi_task(iTask.task);
688 iTask.task = NULL;
690 iTask.complete = 0;
691 goto retry;
694 if (iTask.status != SCSI_STATUS_GOOD) {
695 /* in case the get_lba_status_callout fails (i.e.
696 * because the device is busy or the cmd is not
697 * supported) we pretend all blocks are allocated
698 * for backwards compatibility */
699 goto out;
702 lbas = scsi_datain_unmarshall(iTask.task);
703 if (lbas == NULL) {
704 ret = -EIO;
705 goto out;
708 lbasd = &lbas->descriptors[0];
710 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
711 ret = -EIO;
712 goto out;
715 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
717 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
718 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
719 ret &= ~BDRV_BLOCK_DATA;
720 if (iscsilun->lbprz) {
721 ret |= BDRV_BLOCK_ZERO;
725 if (ret & BDRV_BLOCK_ZERO) {
726 iscsi_allocmap_set_unallocated(iscsilun, sector_num, *pnum);
727 } else {
728 iscsi_allocmap_set_allocated(iscsilun, sector_num, *pnum);
731 if (*pnum > nb_sectors) {
732 *pnum = nb_sectors;
734 out:
735 if (iTask.task != NULL) {
736 scsi_free_scsi_task(iTask.task);
738 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) {
739 *file = bs;
741 return ret;
744 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
745 int64_t sector_num, int nb_sectors,
746 QEMUIOVector *iov)
748 IscsiLun *iscsilun = bs->opaque;
749 struct IscsiTask iTask;
750 uint64_t lba;
751 uint32_t num_sectors;
753 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
754 return -EINVAL;
757 if (bs->bl.max_transfer &&
758 nb_sectors << BDRV_SECTOR_BITS > bs->bl.max_transfer) {
759 error_report("iSCSI Error: Read of %d sectors exceeds max_xfer_len "
760 "of %" PRIu32 " bytes", nb_sectors, bs->bl.max_transfer);
761 return -EINVAL;
764 /* if cache.direct is off and we have a valid entry in our allocation map
765 * we can skip checking the block status and directly return zeroes if
766 * the request falls within an unallocated area */
767 if (iscsi_allocmap_is_valid(iscsilun, sector_num, nb_sectors) &&
768 !iscsi_allocmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
769 qemu_iovec_memset(iov, 0, 0x00, iov->size);
770 return 0;
773 if (nb_sectors >= ISCSI_CHECKALLOC_THRES &&
774 !iscsi_allocmap_is_valid(iscsilun, sector_num, nb_sectors) &&
775 !iscsi_allocmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
776 int pnum;
777 BlockDriverState *file;
778 /* check the block status from the beginning of the cluster
779 * containing the start sector */
780 int64_t ret = iscsi_co_get_block_status(bs,
781 sector_num - sector_num % iscsilun->cluster_sectors,
782 BDRV_REQUEST_MAX_SECTORS, &pnum, &file);
783 if (ret < 0) {
784 return ret;
786 /* if the whole request falls into an unallocated area we can avoid
787 * to read and directly return zeroes instead */
788 if (ret & BDRV_BLOCK_ZERO &&
789 pnum >= nb_sectors + sector_num % iscsilun->cluster_sectors) {
790 qemu_iovec_memset(iov, 0, 0x00, iov->size);
791 return 0;
795 lba = sector_qemu2lun(sector_num, iscsilun);
796 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
798 iscsi_co_init_iscsitask(iscsilun, &iTask);
799 retry:
800 if (iscsilun->use_16_for_rw) {
801 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
802 num_sectors * iscsilun->block_size,
803 iscsilun->block_size, 0, 0, 0, 0, 0,
804 iscsi_co_generic_cb, &iTask);
805 } else {
806 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
807 num_sectors * iscsilun->block_size,
808 iscsilun->block_size,
809 0, 0, 0, 0, 0,
810 iscsi_co_generic_cb, &iTask);
812 if (iTask.task == NULL) {
813 return -ENOMEM;
815 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
817 while (!iTask.complete) {
818 iscsi_set_events(iscsilun);
819 qemu_coroutine_yield();
822 if (iTask.task != NULL) {
823 scsi_free_scsi_task(iTask.task);
824 iTask.task = NULL;
827 if (iTask.do_retry) {
828 iTask.complete = 0;
829 goto retry;
832 if (iTask.status != SCSI_STATUS_GOOD) {
833 return iTask.err_code;
836 return 0;
839 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
841 IscsiLun *iscsilun = bs->opaque;
842 struct IscsiTask iTask;
844 iscsi_co_init_iscsitask(iscsilun, &iTask);
845 retry:
846 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
847 0, iscsi_co_generic_cb, &iTask) == NULL) {
848 return -ENOMEM;
851 while (!iTask.complete) {
852 iscsi_set_events(iscsilun);
853 qemu_coroutine_yield();
856 if (iTask.task != NULL) {
857 scsi_free_scsi_task(iTask.task);
858 iTask.task = NULL;
861 if (iTask.do_retry) {
862 iTask.complete = 0;
863 goto retry;
866 if (iTask.status != SCSI_STATUS_GOOD) {
867 return iTask.err_code;
870 return 0;
873 #ifdef __linux__
874 static void
875 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
876 void *command_data, void *opaque)
878 IscsiAIOCB *acb = opaque;
880 g_free(acb->buf);
881 acb->buf = NULL;
883 acb->status = 0;
884 if (status < 0) {
885 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
886 iscsi_get_error(iscsi));
887 acb->status = iscsi_translate_sense(&acb->task->sense);
890 acb->ioh->driver_status = 0;
891 acb->ioh->host_status = 0;
892 acb->ioh->resid = 0;
893 acb->ioh->status = status;
895 #define SG_ERR_DRIVER_SENSE 0x08
897 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
898 int ss;
900 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
902 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
903 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
904 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
905 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
908 iscsi_schedule_bh(acb);
911 static void iscsi_ioctl_bh_completion(void *opaque)
913 IscsiAIOCB *acb = opaque;
915 qemu_bh_delete(acb->bh);
916 acb->common.cb(acb->common.opaque, acb->ret);
917 qemu_aio_unref(acb);
920 static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf)
922 BlockDriverState *bs = acb->common.bs;
923 IscsiLun *iscsilun = bs->opaque;
924 int ret = 0;
926 switch (req) {
927 case SG_GET_VERSION_NUM:
928 *(int *)buf = 30000;
929 break;
930 case SG_GET_SCSI_ID:
931 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
932 break;
933 default:
934 ret = -EINVAL;
936 assert(!acb->bh);
937 acb->bh = aio_bh_new(bdrv_get_aio_context(bs),
938 iscsi_ioctl_bh_completion, acb);
939 acb->ret = ret;
940 qemu_bh_schedule(acb->bh);
943 static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
944 unsigned long int req, void *buf,
945 BlockCompletionFunc *cb, void *opaque)
947 IscsiLun *iscsilun = bs->opaque;
948 struct iscsi_context *iscsi = iscsilun->iscsi;
949 struct iscsi_data data;
950 IscsiAIOCB *acb;
952 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
954 acb->iscsilun = iscsilun;
955 acb->bh = NULL;
956 acb->status = -EINPROGRESS;
957 acb->buf = NULL;
958 acb->ioh = buf;
960 if (req != SG_IO) {
961 iscsi_ioctl_handle_emulated(acb, req, buf);
962 return &acb->common;
965 if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) {
966 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
967 acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE);
968 qemu_aio_unref(acb);
969 return NULL;
972 acb->task = malloc(sizeof(struct scsi_task));
973 if (acb->task == NULL) {
974 error_report("iSCSI: Failed to allocate task for scsi command. %s",
975 iscsi_get_error(iscsi));
976 qemu_aio_unref(acb);
977 return NULL;
979 memset(acb->task, 0, sizeof(struct scsi_task));
981 switch (acb->ioh->dxfer_direction) {
982 case SG_DXFER_TO_DEV:
983 acb->task->xfer_dir = SCSI_XFER_WRITE;
984 break;
985 case SG_DXFER_FROM_DEV:
986 acb->task->xfer_dir = SCSI_XFER_READ;
987 break;
988 default:
989 acb->task->xfer_dir = SCSI_XFER_NONE;
990 break;
993 acb->task->cdb_size = acb->ioh->cmd_len;
994 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
995 acb->task->expxferlen = acb->ioh->dxfer_len;
997 data.size = 0;
998 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
999 if (acb->ioh->iovec_count == 0) {
1000 data.data = acb->ioh->dxferp;
1001 data.size = acb->ioh->dxfer_len;
1002 } else {
1003 scsi_task_set_iov_out(acb->task,
1004 (struct scsi_iovec *) acb->ioh->dxferp,
1005 acb->ioh->iovec_count);
1009 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
1010 iscsi_aio_ioctl_cb,
1011 (data.size > 0) ? &data : NULL,
1012 acb) != 0) {
1013 scsi_free_scsi_task(acb->task);
1014 qemu_aio_unref(acb);
1015 return NULL;
1018 /* tell libiscsi to read straight into the buffer we got from ioctl */
1019 if (acb->task->xfer_dir == SCSI_XFER_READ) {
1020 if (acb->ioh->iovec_count == 0) {
1021 scsi_task_add_data_in_buffer(acb->task,
1022 acb->ioh->dxfer_len,
1023 acb->ioh->dxferp);
1024 } else {
1025 scsi_task_set_iov_in(acb->task,
1026 (struct scsi_iovec *) acb->ioh->dxferp,
1027 acb->ioh->iovec_count);
1031 iscsi_set_events(iscsilun);
1033 return &acb->common;
1036 #endif
1038 static int64_t
1039 iscsi_getlength(BlockDriverState *bs)
1041 IscsiLun *iscsilun = bs->opaque;
1042 int64_t len;
1044 len = iscsilun->num_blocks;
1045 len *= iscsilun->block_size;
1047 return len;
1050 static int
1051 coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
1052 int nb_sectors)
1054 IscsiLun *iscsilun = bs->opaque;
1055 struct IscsiTask iTask;
1056 struct unmap_list list;
1058 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
1059 return -EINVAL;
1062 if (!iscsilun->lbp.lbpu) {
1063 /* UNMAP is not supported by the target */
1064 return 0;
1067 list.lba = sector_qemu2lun(sector_num, iscsilun);
1068 list.num = sector_qemu2lun(nb_sectors, iscsilun);
1070 iscsi_co_init_iscsitask(iscsilun, &iTask);
1071 retry:
1072 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
1073 iscsi_co_generic_cb, &iTask) == NULL) {
1074 return -ENOMEM;
1077 while (!iTask.complete) {
1078 iscsi_set_events(iscsilun);
1079 qemu_coroutine_yield();
1082 if (iTask.task != NULL) {
1083 scsi_free_scsi_task(iTask.task);
1084 iTask.task = NULL;
1087 if (iTask.do_retry) {
1088 iTask.complete = 0;
1089 goto retry;
1092 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
1093 /* the target might fail with a check condition if it
1094 is not happy with the alignment of the UNMAP request
1095 we silently fail in this case */
1096 return 0;
1099 if (iTask.status != SCSI_STATUS_GOOD) {
1100 return iTask.err_code;
1103 iscsi_allocmap_set_invalid(iscsilun, sector_num, nb_sectors);
1105 return 0;
1108 static int
1109 coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1110 int count, BdrvRequestFlags flags)
1112 IscsiLun *iscsilun = bs->opaque;
1113 struct IscsiTask iTask;
1114 uint64_t lba;
1115 uint32_t nb_blocks;
1116 bool use_16_for_ws = iscsilun->use_16_for_rw;
1118 if (!is_byte_request_lun_aligned(offset, count, iscsilun)) {
1119 return -ENOTSUP;
1122 if (flags & BDRV_REQ_MAY_UNMAP) {
1123 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
1124 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1125 use_16_for_ws = true;
1127 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
1128 /* WRITESAME16 with UNMAP is not supported by the target,
1129 * fall back and try WRITESAME10/16 without UNMAP */
1130 flags &= ~BDRV_REQ_MAY_UNMAP;
1131 use_16_for_ws = iscsilun->use_16_for_rw;
1135 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
1136 /* WRITESAME without UNMAP is not supported by the target */
1137 return -ENOTSUP;
1140 lba = offset / iscsilun->block_size;
1141 nb_blocks = count / iscsilun->block_size;
1143 if (iscsilun->zeroblock == NULL) {
1144 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size);
1145 if (iscsilun->zeroblock == NULL) {
1146 return -ENOMEM;
1150 iscsi_co_init_iscsitask(iscsilun, &iTask);
1151 retry:
1152 if (use_16_for_ws) {
1153 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1154 iscsilun->zeroblock, iscsilun->block_size,
1155 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1156 0, 0, iscsi_co_generic_cb, &iTask);
1157 } else {
1158 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
1159 iscsilun->zeroblock, iscsilun->block_size,
1160 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1161 0, 0, iscsi_co_generic_cb, &iTask);
1163 if (iTask.task == NULL) {
1164 return -ENOMEM;
1167 while (!iTask.complete) {
1168 iscsi_set_events(iscsilun);
1169 qemu_coroutine_yield();
1172 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1173 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
1174 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
1175 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
1176 /* WRITE SAME is not supported by the target */
1177 iscsilun->has_write_same = false;
1178 scsi_free_scsi_task(iTask.task);
1179 return -ENOTSUP;
1182 if (iTask.task != NULL) {
1183 scsi_free_scsi_task(iTask.task);
1184 iTask.task = NULL;
1187 if (iTask.do_retry) {
1188 iTask.complete = 0;
1189 goto retry;
1192 if (iTask.status != SCSI_STATUS_GOOD) {
1193 iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS,
1194 count >> BDRV_SECTOR_BITS);
1195 return iTask.err_code;
1198 if (flags & BDRV_REQ_MAY_UNMAP) {
1199 iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS,
1200 count >> BDRV_SECTOR_BITS);
1201 } else {
1202 iscsi_allocmap_set_allocated(iscsilun, offset >> BDRV_SECTOR_BITS,
1203 count >> BDRV_SECTOR_BITS);
1206 return 0;
1209 static void parse_chap(struct iscsi_context *iscsi, const char *target,
1210 Error **errp)
1212 QemuOptsList *list;
1213 QemuOpts *opts;
1214 const char *user = NULL;
1215 const char *password = NULL;
1216 const char *secretid;
1217 char *secret = NULL;
1219 list = qemu_find_opts("iscsi");
1220 if (!list) {
1221 return;
1224 opts = qemu_opts_find(list, target);
1225 if (opts == NULL) {
1226 opts = QTAILQ_FIRST(&list->head);
1227 if (!opts) {
1228 return;
1232 user = qemu_opt_get(opts, "user");
1233 if (!user) {
1234 return;
1237 secretid = qemu_opt_get(opts, "password-secret");
1238 password = qemu_opt_get(opts, "password");
1239 if (secretid && password) {
1240 error_setg(errp, "'password' and 'password-secret' properties are "
1241 "mutually exclusive");
1242 return;
1244 if (secretid) {
1245 secret = qcrypto_secret_lookup_as_utf8(secretid, errp);
1246 if (!secret) {
1247 return;
1249 password = secret;
1250 } else if (!password) {
1251 error_setg(errp, "CHAP username specified but no password was given");
1252 return;
1255 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
1256 error_setg(errp, "Failed to set initiator username and password");
1259 g_free(secret);
1262 static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
1263 Error **errp)
1265 QemuOptsList *list;
1266 QemuOpts *opts;
1267 const char *digest = NULL;
1269 list = qemu_find_opts("iscsi");
1270 if (!list) {
1271 return;
1274 opts = qemu_opts_find(list, target);
1275 if (opts == NULL) {
1276 opts = QTAILQ_FIRST(&list->head);
1277 if (!opts) {
1278 return;
1282 digest = qemu_opt_get(opts, "header-digest");
1283 if (!digest) {
1284 return;
1287 if (!strcmp(digest, "CRC32C")) {
1288 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1289 } else if (!strcmp(digest, "NONE")) {
1290 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1291 } else if (!strcmp(digest, "CRC32C-NONE")) {
1292 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1293 } else if (!strcmp(digest, "NONE-CRC32C")) {
1294 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1295 } else {
1296 error_setg(errp, "Invalid header-digest setting : %s", digest);
1300 static char *parse_initiator_name(const char *target)
1302 QemuOptsList *list;
1303 QemuOpts *opts;
1304 const char *name;
1305 char *iscsi_name;
1306 UuidInfo *uuid_info;
1308 list = qemu_find_opts("iscsi");
1309 if (list) {
1310 opts = qemu_opts_find(list, target);
1311 if (!opts) {
1312 opts = QTAILQ_FIRST(&list->head);
1314 if (opts) {
1315 name = qemu_opt_get(opts, "initiator-name");
1316 if (name) {
1317 return g_strdup(name);
1322 uuid_info = qmp_query_uuid(NULL);
1323 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1324 name = qemu_get_vm_name();
1325 } else {
1326 name = uuid_info->UUID;
1328 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1329 name ? ":" : "", name ? name : "");
1330 qapi_free_UuidInfo(uuid_info);
1331 return iscsi_name;
1334 static int parse_timeout(const char *target)
1336 QemuOptsList *list;
1337 QemuOpts *opts;
1338 const char *timeout;
1340 list = qemu_find_opts("iscsi");
1341 if (list) {
1342 opts = qemu_opts_find(list, target);
1343 if (!opts) {
1344 opts = QTAILQ_FIRST(&list->head);
1346 if (opts) {
1347 timeout = qemu_opt_get(opts, "timeout");
1348 if (timeout) {
1349 return atoi(timeout);
1354 return 0;
1357 static void iscsi_nop_timed_event(void *opaque)
1359 IscsiLun *iscsilun = opaque;
1361 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) {
1362 error_report("iSCSI: NOP timeout. Reconnecting...");
1363 iscsilun->request_timed_out = true;
1364 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1365 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1366 return;
1369 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1370 iscsi_set_events(iscsilun);
1373 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
1375 struct scsi_task *task = NULL;
1376 struct scsi_readcapacity10 *rc10 = NULL;
1377 struct scsi_readcapacity16 *rc16 = NULL;
1378 int retries = ISCSI_CMD_RETRIES;
1380 do {
1381 if (task != NULL) {
1382 scsi_free_scsi_task(task);
1383 task = NULL;
1386 switch (iscsilun->type) {
1387 case TYPE_DISK:
1388 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1389 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1390 rc16 = scsi_datain_unmarshall(task);
1391 if (rc16 == NULL) {
1392 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1393 } else {
1394 iscsilun->block_size = rc16->block_length;
1395 iscsilun->num_blocks = rc16->returned_lba + 1;
1396 iscsilun->lbpme = !!rc16->lbpme;
1397 iscsilun->lbprz = !!rc16->lbprz;
1398 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
1400 break;
1402 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1403 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
1404 break;
1406 /* Fall through and try READ CAPACITY(10) instead. */
1407 case TYPE_ROM:
1408 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1409 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1410 rc10 = scsi_datain_unmarshall(task);
1411 if (rc10 == NULL) {
1412 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1413 } else {
1414 iscsilun->block_size = rc10->block_size;
1415 if (rc10->lba == 0) {
1416 /* blank disk loaded */
1417 iscsilun->num_blocks = 0;
1418 } else {
1419 iscsilun->num_blocks = rc10->lba + 1;
1423 break;
1424 default:
1425 return;
1427 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1428 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1429 && retries-- > 0);
1431 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1432 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command");
1433 } else if (!iscsilun->block_size ||
1434 iscsilun->block_size % BDRV_SECTOR_SIZE) {
1435 error_setg(errp, "iSCSI: the target returned an invalid "
1436 "block size of %d.", iscsilun->block_size);
1438 if (task) {
1439 scsi_free_scsi_task(task);
1443 /* TODO Convert to fine grained options */
1444 static QemuOptsList runtime_opts = {
1445 .name = "iscsi",
1446 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1447 .desc = {
1449 .name = "filename",
1450 .type = QEMU_OPT_STRING,
1451 .help = "URL to the iscsi image",
1453 { /* end of list */ }
1457 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
1458 int evpd, int pc, void **inq, Error **errp)
1460 int full_size;
1461 struct scsi_task *task = NULL;
1462 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1463 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1464 goto fail;
1466 full_size = scsi_datain_getfullsize(task);
1467 if (full_size > task->datain.size) {
1468 scsi_free_scsi_task(task);
1470 /* we need more data for the full list */
1471 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1472 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1473 goto fail;
1477 *inq = scsi_datain_unmarshall(task);
1478 if (*inq == NULL) {
1479 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1480 goto fail_with_err;
1483 return task;
1485 fail:
1486 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1487 iscsi_get_error(iscsi));
1488 fail_with_err:
1489 if (task != NULL) {
1490 scsi_free_scsi_task(task);
1492 return NULL;
1495 static void iscsi_detach_aio_context(BlockDriverState *bs)
1497 IscsiLun *iscsilun = bs->opaque;
1499 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi),
1500 false, NULL, NULL, NULL);
1501 iscsilun->events = 0;
1503 if (iscsilun->nop_timer) {
1504 timer_del(iscsilun->nop_timer);
1505 timer_free(iscsilun->nop_timer);
1506 iscsilun->nop_timer = NULL;
1508 if (iscsilun->event_timer) {
1509 timer_del(iscsilun->event_timer);
1510 timer_free(iscsilun->event_timer);
1511 iscsilun->event_timer = NULL;
1515 static void iscsi_attach_aio_context(BlockDriverState *bs,
1516 AioContext *new_context)
1518 IscsiLun *iscsilun = bs->opaque;
1520 iscsilun->aio_context = new_context;
1521 iscsi_set_events(iscsilun);
1523 /* Set up a timer for sending out iSCSI NOPs */
1524 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1525 QEMU_CLOCK_REALTIME, SCALE_MS,
1526 iscsi_nop_timed_event, iscsilun);
1527 timer_mod(iscsilun->nop_timer,
1528 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1530 /* Set up a timer for periodic calls to iscsi_set_events and to
1531 * scan for command timeout */
1532 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context,
1533 QEMU_CLOCK_REALTIME, SCALE_MS,
1534 iscsi_timed_check_events, iscsilun);
1535 timer_mod(iscsilun->event_timer,
1536 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
1539 static void iscsi_modesense_sync(IscsiLun *iscsilun)
1541 struct scsi_task *task;
1542 struct scsi_mode_sense *ms = NULL;
1543 iscsilun->write_protected = false;
1544 iscsilun->dpofua = false;
1546 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1547 1, SCSI_MODESENSE_PC_CURRENT,
1548 0x3F, 0, 255);
1549 if (task == NULL) {
1550 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1551 iscsi_get_error(iscsilun->iscsi));
1552 goto out;
1555 if (task->status != SCSI_STATUS_GOOD) {
1556 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1557 goto out;
1559 ms = scsi_datain_unmarshall(task);
1560 if (!ms) {
1561 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1562 iscsi_get_error(iscsilun->iscsi));
1563 goto out;
1565 iscsilun->write_protected = ms->device_specific_parameter & 0x80;
1566 iscsilun->dpofua = ms->device_specific_parameter & 0x10;
1568 out:
1569 if (task) {
1570 scsi_free_scsi_task(task);
1575 * We support iscsi url's on the form
1576 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1578 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1579 Error **errp)
1581 IscsiLun *iscsilun = bs->opaque;
1582 struct iscsi_context *iscsi = NULL;
1583 struct iscsi_url *iscsi_url = NULL;
1584 struct scsi_task *task = NULL;
1585 struct scsi_inquiry_standard *inq = NULL;
1586 struct scsi_inquiry_supported_pages *inq_vpd;
1587 char *initiator_name = NULL;
1588 QemuOpts *opts;
1589 Error *local_err = NULL;
1590 const char *filename;
1591 int i, ret = 0, timeout = 0;
1593 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1594 qemu_opts_absorb_qdict(opts, options, &local_err);
1595 if (local_err) {
1596 error_propagate(errp, local_err);
1597 ret = -EINVAL;
1598 goto out;
1601 filename = qemu_opt_get(opts, "filename");
1603 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1604 if (iscsi_url == NULL) {
1605 error_setg(errp, "Failed to parse URL : %s", filename);
1606 ret = -EINVAL;
1607 goto out;
1610 memset(iscsilun, 0, sizeof(IscsiLun));
1612 initiator_name = parse_initiator_name(iscsi_url->target);
1614 iscsi = iscsi_create_context(initiator_name);
1615 if (iscsi == NULL) {
1616 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
1617 ret = -ENOMEM;
1618 goto out;
1621 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1622 error_setg(errp, "iSCSI: Failed to set target name.");
1623 ret = -EINVAL;
1624 goto out;
1627 if (iscsi_url->user[0] != '\0') {
1628 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1629 iscsi_url->passwd);
1630 if (ret != 0) {
1631 error_setg(errp, "Failed to set initiator username and password");
1632 ret = -EINVAL;
1633 goto out;
1637 /* check if we got CHAP username/password via the options */
1638 parse_chap(iscsi, iscsi_url->target, &local_err);
1639 if (local_err != NULL) {
1640 error_propagate(errp, local_err);
1641 ret = -EINVAL;
1642 goto out;
1645 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1646 error_setg(errp, "iSCSI: Failed to set session type to normal.");
1647 ret = -EINVAL;
1648 goto out;
1651 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1653 /* check if we got HEADER_DIGEST via the options */
1654 parse_header_digest(iscsi, iscsi_url->target, &local_err);
1655 if (local_err != NULL) {
1656 error_propagate(errp, local_err);
1657 ret = -EINVAL;
1658 goto out;
1661 /* timeout handling is broken in libiscsi before 1.15.0 */
1662 timeout = parse_timeout(iscsi_url->target);
1663 #if defined(LIBISCSI_API_VERSION) && LIBISCSI_API_VERSION >= 20150621
1664 iscsi_set_timeout(iscsi, timeout);
1665 #else
1666 if (timeout) {
1667 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1669 #endif
1671 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1672 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
1673 iscsi_get_error(iscsi));
1674 ret = -EINVAL;
1675 goto out;
1678 iscsilun->iscsi = iscsi;
1679 iscsilun->aio_context = bdrv_get_aio_context(bs);
1680 iscsilun->lun = iscsi_url->lun;
1681 iscsilun->has_write_same = true;
1683 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1684 (void **) &inq, errp);
1685 if (task == NULL) {
1686 ret = -EINVAL;
1687 goto out;
1689 iscsilun->type = inq->periperal_device_type;
1690 scsi_free_scsi_task(task);
1691 task = NULL;
1693 iscsi_modesense_sync(iscsilun);
1694 if (iscsilun->dpofua) {
1695 bs->supported_write_flags = BDRV_REQ_FUA;
1697 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
1699 /* Check the write protect flag of the LUN if we want to write */
1700 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) &&
1701 iscsilun->write_protected) {
1702 error_setg(errp, "Cannot open a write protected LUN as read-write");
1703 ret = -EACCES;
1704 goto out;
1707 iscsi_readcapacity_sync(iscsilun, &local_err);
1708 if (local_err != NULL) {
1709 error_propagate(errp, local_err);
1710 ret = -EINVAL;
1711 goto out;
1713 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1715 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1716 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1717 * will try to read from the device to guess the image format.
1719 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
1720 bs->sg = true;
1723 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1724 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1725 (void **) &inq_vpd, errp);
1726 if (task == NULL) {
1727 ret = -EINVAL;
1728 goto out;
1730 for (i = 0; i < inq_vpd->num_pages; i++) {
1731 struct scsi_task *inq_task;
1732 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1733 struct scsi_inquiry_block_limits *inq_bl;
1734 switch (inq_vpd->pages[i]) {
1735 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1736 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1737 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1738 (void **) &inq_lbp, errp);
1739 if (inq_task == NULL) {
1740 ret = -EINVAL;
1741 goto out;
1743 memcpy(&iscsilun->lbp, inq_lbp,
1744 sizeof(struct scsi_inquiry_logical_block_provisioning));
1745 scsi_free_scsi_task(inq_task);
1746 break;
1747 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1748 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1749 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1750 (void **) &inq_bl, errp);
1751 if (inq_task == NULL) {
1752 ret = -EINVAL;
1753 goto out;
1755 memcpy(&iscsilun->bl, inq_bl,
1756 sizeof(struct scsi_inquiry_block_limits));
1757 scsi_free_scsi_task(inq_task);
1758 break;
1759 default:
1760 break;
1763 scsi_free_scsi_task(task);
1764 task = NULL;
1766 iscsi_attach_aio_context(bs, iscsilun->aio_context);
1768 /* Guess the internal cluster (page) size of the iscsi target by the means
1769 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1770 * reasonable size */
1771 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
1772 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1773 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
1774 iscsilun->block_size) >> BDRV_SECTOR_BITS;
1775 if (iscsilun->lbprz) {
1776 ret = iscsi_allocmap_init(iscsilun, bs->open_flags);
1780 out:
1781 qemu_opts_del(opts);
1782 g_free(initiator_name);
1783 if (iscsi_url != NULL) {
1784 iscsi_destroy_url(iscsi_url);
1786 if (task != NULL) {
1787 scsi_free_scsi_task(task);
1790 if (ret) {
1791 if (iscsi != NULL) {
1792 if (iscsi_is_logged_in(iscsi)) {
1793 iscsi_logout_sync(iscsi);
1795 iscsi_destroy_context(iscsi);
1797 memset(iscsilun, 0, sizeof(IscsiLun));
1799 return ret;
1802 static void iscsi_close(BlockDriverState *bs)
1804 IscsiLun *iscsilun = bs->opaque;
1805 struct iscsi_context *iscsi = iscsilun->iscsi;
1807 iscsi_detach_aio_context(bs);
1808 if (iscsi_is_logged_in(iscsi)) {
1809 iscsi_logout_sync(iscsi);
1811 iscsi_destroy_context(iscsi);
1812 g_free(iscsilun->zeroblock);
1813 iscsi_allocmap_free(iscsilun);
1814 memset(iscsilun, 0, sizeof(IscsiLun));
1817 static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)
1819 /* We don't actually refresh here, but just return data queried in
1820 * iscsi_open(): iscsi targets don't change their limits. */
1822 IscsiLun *iscsilun = bs->opaque;
1823 uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff;
1825 bs->bl.request_alignment = iscsilun->block_size;
1827 if (iscsilun->bl.max_xfer_len) {
1828 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len);
1831 if (max_xfer_len * iscsilun->block_size < INT_MAX) {
1832 bs->bl.max_transfer = max_xfer_len * iscsilun->block_size;
1835 if (iscsilun->lbp.lbpu) {
1836 if (iscsilun->bl.max_unmap < 0xffffffff / iscsilun->block_size) {
1837 bs->bl.max_pdiscard =
1838 iscsilun->bl.max_unmap * iscsilun->block_size;
1840 bs->bl.pdiscard_alignment =
1841 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
1842 } else {
1843 bs->bl.pdiscard_alignment = iscsilun->block_size;
1846 if (iscsilun->bl.max_ws_len < 0xffffffff / iscsilun->block_size) {
1847 bs->bl.max_pwrite_zeroes =
1848 iscsilun->bl.max_ws_len * iscsilun->block_size;
1850 if (iscsilun->lbp.lbpws) {
1851 bs->bl.pwrite_zeroes_alignment =
1852 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
1853 } else {
1854 bs->bl.pwrite_zeroes_alignment = iscsilun->block_size;
1856 if (iscsilun->bl.opt_xfer_len &&
1857 iscsilun->bl.opt_xfer_len < INT_MAX / iscsilun->block_size) {
1858 bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len *
1859 iscsilun->block_size);
1863 /* Note that this will not re-establish a connection with an iSCSI target - it
1864 * is effectively a NOP. */
1865 static int iscsi_reopen_prepare(BDRVReopenState *state,
1866 BlockReopenQueue *queue, Error **errp)
1868 IscsiLun *iscsilun = state->bs->opaque;
1870 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) {
1871 error_setg(errp, "Cannot open a write protected LUN as read-write");
1872 return -EACCES;
1874 return 0;
1877 static void iscsi_reopen_commit(BDRVReopenState *reopen_state)
1879 IscsiLun *iscsilun = reopen_state->bs->opaque;
1881 /* the cache.direct status might have changed */
1882 if (iscsilun->allocmap != NULL) {
1883 iscsi_allocmap_init(iscsilun, reopen_state->flags);
1887 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1889 IscsiLun *iscsilun = bs->opaque;
1890 Error *local_err = NULL;
1892 if (iscsilun->type != TYPE_DISK) {
1893 return -ENOTSUP;
1896 iscsi_readcapacity_sync(iscsilun, &local_err);
1897 if (local_err != NULL) {
1898 error_free(local_err);
1899 return -EIO;
1902 if (offset > iscsi_getlength(bs)) {
1903 return -EINVAL;
1906 if (iscsilun->allocmap != NULL) {
1907 iscsi_allocmap_init(iscsilun, bs->open_flags);
1910 return 0;
1913 static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp)
1915 int ret = 0;
1916 int64_t total_size = 0;
1917 BlockDriverState *bs;
1918 IscsiLun *iscsilun = NULL;
1919 QDict *bs_options;
1921 bs = bdrv_new();
1923 /* Read out options */
1924 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1925 BDRV_SECTOR_SIZE);
1926 bs->opaque = g_new0(struct IscsiLun, 1);
1927 iscsilun = bs->opaque;
1929 bs_options = qdict_new();
1930 qdict_put(bs_options, "filename", qstring_from_str(filename));
1931 ret = iscsi_open(bs, bs_options, 0, NULL);
1932 QDECREF(bs_options);
1934 if (ret != 0) {
1935 goto out;
1937 iscsi_detach_aio_context(bs);
1938 if (iscsilun->type != TYPE_DISK) {
1939 ret = -ENODEV;
1940 goto out;
1942 if (bs->total_sectors < total_size) {
1943 ret = -ENOSPC;
1944 goto out;
1947 ret = 0;
1948 out:
1949 if (iscsilun->iscsi != NULL) {
1950 iscsi_destroy_context(iscsilun->iscsi);
1952 g_free(bs->opaque);
1953 bs->opaque = NULL;
1954 bdrv_unref(bs);
1955 return ret;
1958 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1960 IscsiLun *iscsilun = bs->opaque;
1961 bdi->unallocated_blocks_are_zero = iscsilun->lbprz;
1962 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
1963 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
1964 return 0;
1967 static void iscsi_invalidate_cache(BlockDriverState *bs,
1968 Error **errp)
1970 IscsiLun *iscsilun = bs->opaque;
1971 iscsi_allocmap_invalidate(iscsilun);
1974 static QemuOptsList iscsi_create_opts = {
1975 .name = "iscsi-create-opts",
1976 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
1977 .desc = {
1979 .name = BLOCK_OPT_SIZE,
1980 .type = QEMU_OPT_SIZE,
1981 .help = "Virtual disk size"
1983 { /* end of list */ }
1987 static BlockDriver bdrv_iscsi = {
1988 .format_name = "iscsi",
1989 .protocol_name = "iscsi",
1991 .instance_size = sizeof(IscsiLun),
1992 .bdrv_needs_filename = true,
1993 .bdrv_file_open = iscsi_open,
1994 .bdrv_close = iscsi_close,
1995 .bdrv_create = iscsi_create,
1996 .create_opts = &iscsi_create_opts,
1997 .bdrv_reopen_prepare = iscsi_reopen_prepare,
1998 .bdrv_reopen_commit = iscsi_reopen_commit,
1999 .bdrv_invalidate_cache = iscsi_invalidate_cache,
2001 .bdrv_getlength = iscsi_getlength,
2002 .bdrv_get_info = iscsi_get_info,
2003 .bdrv_truncate = iscsi_truncate,
2004 .bdrv_refresh_limits = iscsi_refresh_limits,
2006 .bdrv_co_get_block_status = iscsi_co_get_block_status,
2007 .bdrv_co_discard = iscsi_co_discard,
2008 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2009 .bdrv_co_readv = iscsi_co_readv,
2010 .bdrv_co_writev_flags = iscsi_co_writev_flags,
2011 .bdrv_co_flush_to_disk = iscsi_co_flush,
2013 #ifdef __linux__
2014 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2015 #endif
2017 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2018 .bdrv_attach_aio_context = iscsi_attach_aio_context,
2021 static QemuOptsList qemu_iscsi_opts = {
2022 .name = "iscsi",
2023 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
2024 .desc = {
2026 .name = "user",
2027 .type = QEMU_OPT_STRING,
2028 .help = "username for CHAP authentication to target",
2030 .name = "password",
2031 .type = QEMU_OPT_STRING,
2032 .help = "password for CHAP authentication to target",
2034 .name = "password-secret",
2035 .type = QEMU_OPT_STRING,
2036 .help = "ID of the secret providing password for CHAP "
2037 "authentication to target",
2039 .name = "header-digest",
2040 .type = QEMU_OPT_STRING,
2041 .help = "HeaderDigest setting. "
2042 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
2044 .name = "initiator-name",
2045 .type = QEMU_OPT_STRING,
2046 .help = "Initiator iqn name to use when connecting",
2048 .name = "timeout",
2049 .type = QEMU_OPT_NUMBER,
2050 .help = "Request timeout in seconds (default 0 = no timeout)",
2052 { /* end of list */ }
2056 static void iscsi_block_init(void)
2058 bdrv_register(&bdrv_iscsi);
2059 qemu_add_opts(&qemu_iscsi_opts);
2062 block_init(iscsi_block_init);