block: explicitly acquire aiocontext in bottom halves that need it
[qemu/ar7.git] / block / iscsi.c
blob4fb43c2859668943303d0090244a6eea1852628b
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 "qemu/uuid.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 IscsiLun *iscsilun;
99 QEMUTimer retry_timer;
100 int err_code;
101 } IscsiTask;
103 typedef struct IscsiAIOCB {
104 BlockAIOCB common;
105 QEMUIOVector *qiov;
106 QEMUBH *bh;
107 IscsiLun *iscsilun;
108 struct scsi_task *task;
109 uint8_t *buf;
110 int status;
111 int64_t sector_num;
112 int nb_sectors;
113 int ret;
114 #ifdef __linux__
115 sg_io_hdr_t *ioh;
116 #endif
117 } IscsiAIOCB;
119 /* libiscsi uses time_t so its enough to process events every second */
120 #define EVENT_INTERVAL 1000
121 #define NOP_INTERVAL 5000
122 #define MAX_NOP_FAILURES 3
123 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
124 static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768};
126 /* this threshold is a trade-off knob to choose between
127 * the potential additional overhead of an extra GET_LBA_STATUS request
128 * vs. unnecessarily reading a lot of zero sectors over the wire.
129 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
130 * sectors we check the allocation status of the area covered by the
131 * request first if the allocationmap indicates that the area might be
132 * unallocated. */
133 #define ISCSI_CHECKALLOC_THRES 64
135 static void
136 iscsi_bh_cb(void *p)
138 IscsiAIOCB *acb = p;
139 AioContext *ctx = bdrv_get_aio_context(acb->common.bs);
141 qemu_bh_delete(acb->bh);
143 g_free(acb->buf);
144 acb->buf = NULL;
146 aio_context_acquire(ctx);
147 acb->common.cb(acb->common.opaque, acb->status);
148 aio_context_release(ctx);
150 if (acb->task != NULL) {
151 scsi_free_scsi_task(acb->task);
152 acb->task = NULL;
155 qemu_aio_unref(acb);
158 static void
159 iscsi_schedule_bh(IscsiAIOCB *acb)
161 if (acb->bh) {
162 return;
164 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
165 qemu_bh_schedule(acb->bh);
168 static void iscsi_co_generic_bh_cb(void *opaque)
170 struct IscsiTask *iTask = opaque;
172 iTask->complete = 1;
173 aio_co_wake(iTask->co);
176 static void iscsi_retry_timer_expired(void *opaque)
178 struct IscsiTask *iTask = opaque;
179 iTask->complete = 1;
180 if (iTask->co) {
181 aio_co_wake(iTask->co);
185 static inline unsigned exp_random(double mean)
187 return -mean * log((double)rand() / RAND_MAX);
190 /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
191 * libiscsi 1.10.0, together with other constants we need. Use it as
192 * a hint that we have to define them ourselves if needed, to keep the
193 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
194 * the test because SCSI_STATUS_* is an enum.
196 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
197 * an enum, check against the LIBISCSI_API_VERSION macro, which was
198 * introduced in 1.11.0. If it is present, there is no need to define
199 * anything.
201 #if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
202 !defined(LIBISCSI_API_VERSION)
203 #define SCSI_STATUS_TASK_SET_FULL 0x28
204 #define SCSI_STATUS_TIMEOUT 0x0f000002
205 #define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
206 #define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
207 #endif
209 #ifndef LIBISCSI_API_VERSION
210 #define LIBISCSI_API_VERSION 20130701
211 #endif
213 static int iscsi_translate_sense(struct scsi_sense *sense)
215 int ret;
217 switch (sense->key) {
218 case SCSI_SENSE_NOT_READY:
219 return -EBUSY;
220 case SCSI_SENSE_DATA_PROTECTION:
221 return -EACCES;
222 case SCSI_SENSE_COMMAND_ABORTED:
223 return -ECANCELED;
224 case SCSI_SENSE_ILLEGAL_REQUEST:
225 /* Parse ASCQ */
226 break;
227 default:
228 return -EIO;
230 switch (sense->ascq) {
231 case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR:
232 case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE:
233 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB:
234 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST:
235 ret = -EINVAL;
236 break;
237 case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE:
238 ret = -ENOSPC;
239 break;
240 case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED:
241 ret = -ENOTSUP;
242 break;
243 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT:
244 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED:
245 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN:
246 ret = -ENOMEDIUM;
247 break;
248 case SCSI_SENSE_ASCQ_WRITE_PROTECTED:
249 ret = -EACCES;
250 break;
251 default:
252 ret = -EIO;
253 break;
255 return ret;
258 static void
259 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
260 void *command_data, void *opaque)
262 struct IscsiTask *iTask = opaque;
263 struct scsi_task *task = command_data;
265 iTask->status = status;
266 iTask->do_retry = 0;
267 iTask->task = task;
269 if (status != SCSI_STATUS_GOOD) {
270 if (iTask->retries++ < ISCSI_CMD_RETRIES) {
271 if (status == SCSI_STATUS_CHECK_CONDITION
272 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
273 error_report("iSCSI CheckCondition: %s",
274 iscsi_get_error(iscsi));
275 iTask->do_retry = 1;
276 goto out;
278 if (status == SCSI_STATUS_BUSY ||
279 status == SCSI_STATUS_TIMEOUT ||
280 status == SCSI_STATUS_TASK_SET_FULL) {
281 unsigned retry_time =
282 exp_random(iscsi_retry_times[iTask->retries - 1]);
283 if (status == SCSI_STATUS_TIMEOUT) {
284 /* make sure the request is rescheduled AFTER the
285 * reconnect is initiated */
286 retry_time = EVENT_INTERVAL * 2;
287 iTask->iscsilun->request_timed_out = true;
289 error_report("iSCSI Busy/TaskSetFull/TimeOut"
290 " (retry #%u in %u ms): %s",
291 iTask->retries, retry_time,
292 iscsi_get_error(iscsi));
293 aio_timer_init(iTask->iscsilun->aio_context,
294 &iTask->retry_timer, QEMU_CLOCK_REALTIME,
295 SCALE_MS, iscsi_retry_timer_expired, iTask);
296 timer_mod(&iTask->retry_timer,
297 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time);
298 iTask->do_retry = 1;
299 return;
302 iTask->err_code = iscsi_translate_sense(&task->sense);
303 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi));
306 out:
307 if (iTask->co) {
308 aio_bh_schedule_oneshot(iTask->iscsilun->aio_context,
309 iscsi_co_generic_bh_cb, iTask);
310 } else {
311 iTask->complete = 1;
315 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
317 *iTask = (struct IscsiTask) {
318 .co = qemu_coroutine_self(),
319 .iscsilun = iscsilun,
323 static void
324 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
325 void *private_data)
327 IscsiAIOCB *acb = private_data;
329 acb->status = -ECANCELED;
330 iscsi_schedule_bh(acb);
333 static void
334 iscsi_aio_cancel(BlockAIOCB *blockacb)
336 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
337 IscsiLun *iscsilun = acb->iscsilun;
339 if (acb->status != -EINPROGRESS) {
340 return;
343 /* send a task mgmt call to the target to cancel the task on the target */
344 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
345 iscsi_abort_task_cb, acb);
349 static const AIOCBInfo iscsi_aiocb_info = {
350 .aiocb_size = sizeof(IscsiAIOCB),
351 .cancel_async = iscsi_aio_cancel,
355 static void iscsi_process_read(void *arg);
356 static void iscsi_process_write(void *arg);
358 static void
359 iscsi_set_events(IscsiLun *iscsilun)
361 struct iscsi_context *iscsi = iscsilun->iscsi;
362 int ev = iscsi_which_events(iscsi);
364 if (ev != iscsilun->events) {
365 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi),
366 false,
367 (ev & POLLIN) ? iscsi_process_read : NULL,
368 (ev & POLLOUT) ? iscsi_process_write : NULL,
369 NULL,
370 iscsilun);
371 iscsilun->events = ev;
375 static void iscsi_timed_check_events(void *opaque)
377 IscsiLun *iscsilun = opaque;
379 /* check for timed out requests */
380 iscsi_service(iscsilun->iscsi, 0);
382 if (iscsilun->request_timed_out) {
383 iscsilun->request_timed_out = false;
384 iscsi_reconnect(iscsilun->iscsi);
387 /* newer versions of libiscsi may return zero events. Ensure we are able
388 * to return to service once this situation changes. */
389 iscsi_set_events(iscsilun);
391 timer_mod(iscsilun->event_timer,
392 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
395 static void
396 iscsi_process_read(void *arg)
398 IscsiLun *iscsilun = arg;
399 struct iscsi_context *iscsi = iscsilun->iscsi;
401 aio_context_acquire(iscsilun->aio_context);
402 iscsi_service(iscsi, POLLIN);
403 iscsi_set_events(iscsilun);
404 aio_context_release(iscsilun->aio_context);
407 static void
408 iscsi_process_write(void *arg)
410 IscsiLun *iscsilun = arg;
411 struct iscsi_context *iscsi = iscsilun->iscsi;
413 aio_context_acquire(iscsilun->aio_context);
414 iscsi_service(iscsi, POLLOUT);
415 iscsi_set_events(iscsilun);
416 aio_context_release(iscsilun->aio_context);
419 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
421 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
424 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
426 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
429 static bool is_byte_request_lun_aligned(int64_t offset, int count,
430 IscsiLun *iscsilun)
432 if (offset % iscsilun->block_size || count % iscsilun->block_size) {
433 error_report("iSCSI misaligned request: "
434 "iscsilun->block_size %u, offset %" PRIi64
435 ", count %d",
436 iscsilun->block_size, offset, count);
437 return false;
439 return true;
442 static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors,
443 IscsiLun *iscsilun)
445 assert(nb_sectors <= BDRV_REQUEST_MAX_SECTORS);
446 return is_byte_request_lun_aligned(sector_num << BDRV_SECTOR_BITS,
447 nb_sectors << BDRV_SECTOR_BITS,
448 iscsilun);
451 static void iscsi_allocmap_free(IscsiLun *iscsilun)
453 g_free(iscsilun->allocmap);
454 g_free(iscsilun->allocmap_valid);
455 iscsilun->allocmap = NULL;
456 iscsilun->allocmap_valid = NULL;
460 static int iscsi_allocmap_init(IscsiLun *iscsilun, int open_flags)
462 iscsi_allocmap_free(iscsilun);
464 iscsilun->allocmap_size =
465 DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks, iscsilun),
466 iscsilun->cluster_sectors);
468 iscsilun->allocmap = bitmap_try_new(iscsilun->allocmap_size);
469 if (!iscsilun->allocmap) {
470 return -ENOMEM;
473 if (open_flags & BDRV_O_NOCACHE) {
474 /* in case that cache.direct = on all allocmap entries are
475 * treated as invalid to force a relookup of the block
476 * status on every read request */
477 return 0;
480 iscsilun->allocmap_valid = bitmap_try_new(iscsilun->allocmap_size);
481 if (!iscsilun->allocmap_valid) {
482 /* if we are under memory pressure free the allocmap as well */
483 iscsi_allocmap_free(iscsilun);
484 return -ENOMEM;
487 return 0;
490 static void
491 iscsi_allocmap_update(IscsiLun *iscsilun, int64_t sector_num,
492 int nb_sectors, bool allocated, bool valid)
494 int64_t cl_num_expanded, nb_cls_expanded, cl_num_shrunk, nb_cls_shrunk;
496 if (iscsilun->allocmap == NULL) {
497 return;
499 /* expand to entirely contain all affected clusters */
500 cl_num_expanded = sector_num / iscsilun->cluster_sectors;
501 nb_cls_expanded = DIV_ROUND_UP(sector_num + nb_sectors,
502 iscsilun->cluster_sectors) - cl_num_expanded;
503 /* shrink to touch only completely contained clusters */
504 cl_num_shrunk = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors);
505 nb_cls_shrunk = (sector_num + nb_sectors) / iscsilun->cluster_sectors
506 - cl_num_shrunk;
507 if (allocated) {
508 bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded);
509 } else {
510 if (nb_cls_shrunk > 0) {
511 bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
515 if (iscsilun->allocmap_valid == NULL) {
516 return;
518 if (valid) {
519 if (nb_cls_shrunk > 0) {
520 bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
522 } else {
523 bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded,
524 nb_cls_expanded);
528 static void
529 iscsi_allocmap_set_allocated(IscsiLun *iscsilun, int64_t sector_num,
530 int nb_sectors)
532 iscsi_allocmap_update(iscsilun, sector_num, nb_sectors, true, true);
535 static void
536 iscsi_allocmap_set_unallocated(IscsiLun *iscsilun, int64_t sector_num,
537 int nb_sectors)
539 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
540 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
542 iscsi_allocmap_update(iscsilun, sector_num, nb_sectors, false, true);
545 static void iscsi_allocmap_set_invalid(IscsiLun *iscsilun, int64_t sector_num,
546 int nb_sectors)
548 iscsi_allocmap_update(iscsilun, sector_num, nb_sectors, false, false);
551 static void iscsi_allocmap_invalidate(IscsiLun *iscsilun)
553 if (iscsilun->allocmap) {
554 bitmap_zero(iscsilun->allocmap, iscsilun->allocmap_size);
556 if (iscsilun->allocmap_valid) {
557 bitmap_zero(iscsilun->allocmap_valid, iscsilun->allocmap_size);
561 static inline bool
562 iscsi_allocmap_is_allocated(IscsiLun *iscsilun, int64_t sector_num,
563 int nb_sectors)
565 unsigned long size;
566 if (iscsilun->allocmap == NULL) {
567 return true;
569 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
570 return !(find_next_bit(iscsilun->allocmap, size,
571 sector_num / iscsilun->cluster_sectors) == size);
574 static inline bool iscsi_allocmap_is_valid(IscsiLun *iscsilun,
575 int64_t sector_num, int nb_sectors)
577 unsigned long size;
578 if (iscsilun->allocmap_valid == NULL) {
579 return false;
581 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
582 return (find_next_zero_bit(iscsilun->allocmap_valid, size,
583 sector_num / iscsilun->cluster_sectors) == size);
586 static int coroutine_fn
587 iscsi_co_writev_flags(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
588 QEMUIOVector *iov, int flags)
590 IscsiLun *iscsilun = bs->opaque;
591 struct IscsiTask iTask;
592 uint64_t lba;
593 uint32_t num_sectors;
594 bool fua = flags & BDRV_REQ_FUA;
596 if (fua) {
597 assert(iscsilun->dpofua);
599 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
600 return -EINVAL;
603 if (bs->bl.max_transfer) {
604 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
607 lba = sector_qemu2lun(sector_num, iscsilun);
608 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
609 iscsi_co_init_iscsitask(iscsilun, &iTask);
610 retry:
611 if (iscsilun->use_16_for_rw) {
612 #if LIBISCSI_API_VERSION >= (20160603)
613 iTask.task = iscsi_write16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
614 NULL, num_sectors * iscsilun->block_size,
615 iscsilun->block_size, 0, 0, fua, 0, 0,
616 iscsi_co_generic_cb, &iTask,
617 (struct scsi_iovec *)iov->iov, iov->niov);
618 } else {
619 iTask.task = iscsi_write10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
620 NULL, num_sectors * iscsilun->block_size,
621 iscsilun->block_size, 0, 0, fua, 0, 0,
622 iscsi_co_generic_cb, &iTask,
623 (struct scsi_iovec *)iov->iov, iov->niov);
625 #else
626 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
627 NULL, num_sectors * iscsilun->block_size,
628 iscsilun->block_size, 0, 0, fua, 0, 0,
629 iscsi_co_generic_cb, &iTask);
630 } else {
631 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,
632 NULL, num_sectors * iscsilun->block_size,
633 iscsilun->block_size, 0, 0, fua, 0, 0,
634 iscsi_co_generic_cb, &iTask);
636 #endif
637 if (iTask.task == NULL) {
638 return -ENOMEM;
640 #if LIBISCSI_API_VERSION < (20160603)
641 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
642 iov->niov);
643 #endif
644 while (!iTask.complete) {
645 iscsi_set_events(iscsilun);
646 qemu_coroutine_yield();
649 if (iTask.task != NULL) {
650 scsi_free_scsi_task(iTask.task);
651 iTask.task = NULL;
654 if (iTask.do_retry) {
655 iTask.complete = 0;
656 goto retry;
659 if (iTask.status != SCSI_STATUS_GOOD) {
660 iscsi_allocmap_set_invalid(iscsilun, sector_num, nb_sectors);
661 return iTask.err_code;
664 iscsi_allocmap_set_allocated(iscsilun, sector_num, nb_sectors);
666 return 0;
671 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
672 int64_t sector_num,
673 int nb_sectors, int *pnum,
674 BlockDriverState **file)
676 IscsiLun *iscsilun = bs->opaque;
677 struct scsi_get_lba_status *lbas = NULL;
678 struct scsi_lba_status_descriptor *lbasd = NULL;
679 struct IscsiTask iTask;
680 int64_t ret;
682 iscsi_co_init_iscsitask(iscsilun, &iTask);
684 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
685 ret = -EINVAL;
686 goto out;
689 /* default to all sectors allocated */
690 ret = BDRV_BLOCK_DATA;
691 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
692 *pnum = nb_sectors;
694 /* LUN does not support logical block provisioning */
695 if (!iscsilun->lbpme) {
696 goto out;
699 retry:
700 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
701 sector_qemu2lun(sector_num, iscsilun),
702 8 + 16, iscsi_co_generic_cb,
703 &iTask) == NULL) {
704 ret = -ENOMEM;
705 goto out;
708 while (!iTask.complete) {
709 iscsi_set_events(iscsilun);
710 qemu_coroutine_yield();
713 if (iTask.do_retry) {
714 if (iTask.task != NULL) {
715 scsi_free_scsi_task(iTask.task);
716 iTask.task = NULL;
718 iTask.complete = 0;
719 goto retry;
722 if (iTask.status != SCSI_STATUS_GOOD) {
723 /* in case the get_lba_status_callout fails (i.e.
724 * because the device is busy or the cmd is not
725 * supported) we pretend all blocks are allocated
726 * for backwards compatibility */
727 goto out;
730 lbas = scsi_datain_unmarshall(iTask.task);
731 if (lbas == NULL) {
732 ret = -EIO;
733 goto out;
736 lbasd = &lbas->descriptors[0];
738 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
739 ret = -EIO;
740 goto out;
743 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
745 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
746 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
747 ret &= ~BDRV_BLOCK_DATA;
748 if (iscsilun->lbprz) {
749 ret |= BDRV_BLOCK_ZERO;
753 if (ret & BDRV_BLOCK_ZERO) {
754 iscsi_allocmap_set_unallocated(iscsilun, sector_num, *pnum);
755 } else {
756 iscsi_allocmap_set_allocated(iscsilun, sector_num, *pnum);
759 if (*pnum > nb_sectors) {
760 *pnum = nb_sectors;
762 out:
763 if (iTask.task != NULL) {
764 scsi_free_scsi_task(iTask.task);
766 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) {
767 *file = bs;
769 return ret;
772 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
773 int64_t sector_num, int nb_sectors,
774 QEMUIOVector *iov)
776 IscsiLun *iscsilun = bs->opaque;
777 struct IscsiTask iTask;
778 uint64_t lba;
779 uint32_t num_sectors;
781 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
782 return -EINVAL;
785 if (bs->bl.max_transfer) {
786 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
789 /* if cache.direct is off and we have a valid entry in our allocation map
790 * we can skip checking the block status and directly return zeroes if
791 * the request falls within an unallocated area */
792 if (iscsi_allocmap_is_valid(iscsilun, sector_num, nb_sectors) &&
793 !iscsi_allocmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
794 qemu_iovec_memset(iov, 0, 0x00, iov->size);
795 return 0;
798 if (nb_sectors >= ISCSI_CHECKALLOC_THRES &&
799 !iscsi_allocmap_is_valid(iscsilun, sector_num, nb_sectors) &&
800 !iscsi_allocmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
801 int pnum;
802 BlockDriverState *file;
803 /* check the block status from the beginning of the cluster
804 * containing the start sector */
805 int64_t ret = iscsi_co_get_block_status(bs,
806 sector_num - sector_num % iscsilun->cluster_sectors,
807 BDRV_REQUEST_MAX_SECTORS, &pnum, &file);
808 if (ret < 0) {
809 return ret;
811 /* if the whole request falls into an unallocated area we can avoid
812 * to read and directly return zeroes instead */
813 if (ret & BDRV_BLOCK_ZERO &&
814 pnum >= nb_sectors + sector_num % iscsilun->cluster_sectors) {
815 qemu_iovec_memset(iov, 0, 0x00, iov->size);
816 return 0;
820 lba = sector_qemu2lun(sector_num, iscsilun);
821 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
823 iscsi_co_init_iscsitask(iscsilun, &iTask);
824 retry:
825 if (iscsilun->use_16_for_rw) {
826 #if LIBISCSI_API_VERSION >= (20160603)
827 iTask.task = iscsi_read16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
828 num_sectors * iscsilun->block_size,
829 iscsilun->block_size, 0, 0, 0, 0, 0,
830 iscsi_co_generic_cb, &iTask,
831 (struct scsi_iovec *)iov->iov, iov->niov);
832 } else {
833 iTask.task = iscsi_read10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
834 num_sectors * iscsilun->block_size,
835 iscsilun->block_size,
836 0, 0, 0, 0, 0,
837 iscsi_co_generic_cb, &iTask,
838 (struct scsi_iovec *)iov->iov, iov->niov);
840 #else
841 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
842 num_sectors * iscsilun->block_size,
843 iscsilun->block_size, 0, 0, 0, 0, 0,
844 iscsi_co_generic_cb, &iTask);
845 } else {
846 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
847 num_sectors * iscsilun->block_size,
848 iscsilun->block_size,
849 0, 0, 0, 0, 0,
850 iscsi_co_generic_cb, &iTask);
852 #endif
853 if (iTask.task == NULL) {
854 return -ENOMEM;
856 #if LIBISCSI_API_VERSION < (20160603)
857 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
858 #endif
859 while (!iTask.complete) {
860 iscsi_set_events(iscsilun);
861 qemu_coroutine_yield();
864 if (iTask.task != NULL) {
865 scsi_free_scsi_task(iTask.task);
866 iTask.task = NULL;
869 if (iTask.do_retry) {
870 iTask.complete = 0;
871 goto retry;
874 if (iTask.status != SCSI_STATUS_GOOD) {
875 return iTask.err_code;
878 return 0;
881 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
883 IscsiLun *iscsilun = bs->opaque;
884 struct IscsiTask iTask;
886 iscsi_co_init_iscsitask(iscsilun, &iTask);
887 retry:
888 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
889 0, iscsi_co_generic_cb, &iTask) == NULL) {
890 return -ENOMEM;
893 while (!iTask.complete) {
894 iscsi_set_events(iscsilun);
895 qemu_coroutine_yield();
898 if (iTask.task != NULL) {
899 scsi_free_scsi_task(iTask.task);
900 iTask.task = NULL;
903 if (iTask.do_retry) {
904 iTask.complete = 0;
905 goto retry;
908 if (iTask.status != SCSI_STATUS_GOOD) {
909 return iTask.err_code;
912 return 0;
915 #ifdef __linux__
916 static void
917 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
918 void *command_data, void *opaque)
920 IscsiAIOCB *acb = opaque;
922 g_free(acb->buf);
923 acb->buf = NULL;
925 acb->status = 0;
926 if (status < 0) {
927 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
928 iscsi_get_error(iscsi));
929 acb->status = iscsi_translate_sense(&acb->task->sense);
932 acb->ioh->driver_status = 0;
933 acb->ioh->host_status = 0;
934 acb->ioh->resid = 0;
935 acb->ioh->status = status;
937 #define SG_ERR_DRIVER_SENSE 0x08
939 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
940 int ss;
942 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
944 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
945 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
946 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
947 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
950 iscsi_schedule_bh(acb);
953 static void iscsi_ioctl_bh_completion(void *opaque)
955 IscsiAIOCB *acb = opaque;
957 qemu_bh_delete(acb->bh);
958 acb->common.cb(acb->common.opaque, acb->ret);
959 qemu_aio_unref(acb);
962 static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf)
964 BlockDriverState *bs = acb->common.bs;
965 IscsiLun *iscsilun = bs->opaque;
966 int ret = 0;
968 switch (req) {
969 case SG_GET_VERSION_NUM:
970 *(int *)buf = 30000;
971 break;
972 case SG_GET_SCSI_ID:
973 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
974 break;
975 default:
976 ret = -EINVAL;
978 assert(!acb->bh);
979 acb->bh = aio_bh_new(bdrv_get_aio_context(bs),
980 iscsi_ioctl_bh_completion, acb);
981 acb->ret = ret;
982 qemu_bh_schedule(acb->bh);
985 static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
986 unsigned long int req, void *buf,
987 BlockCompletionFunc *cb, void *opaque)
989 IscsiLun *iscsilun = bs->opaque;
990 struct iscsi_context *iscsi = iscsilun->iscsi;
991 struct iscsi_data data;
992 IscsiAIOCB *acb;
994 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
996 acb->iscsilun = iscsilun;
997 acb->bh = NULL;
998 acb->status = -EINPROGRESS;
999 acb->buf = NULL;
1000 acb->ioh = buf;
1002 if (req != SG_IO) {
1003 iscsi_ioctl_handle_emulated(acb, req, buf);
1004 return &acb->common;
1007 if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) {
1008 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
1009 acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE);
1010 qemu_aio_unref(acb);
1011 return NULL;
1014 acb->task = malloc(sizeof(struct scsi_task));
1015 if (acb->task == NULL) {
1016 error_report("iSCSI: Failed to allocate task for scsi command. %s",
1017 iscsi_get_error(iscsi));
1018 qemu_aio_unref(acb);
1019 return NULL;
1021 memset(acb->task, 0, sizeof(struct scsi_task));
1023 switch (acb->ioh->dxfer_direction) {
1024 case SG_DXFER_TO_DEV:
1025 acb->task->xfer_dir = SCSI_XFER_WRITE;
1026 break;
1027 case SG_DXFER_FROM_DEV:
1028 acb->task->xfer_dir = SCSI_XFER_READ;
1029 break;
1030 default:
1031 acb->task->xfer_dir = SCSI_XFER_NONE;
1032 break;
1035 acb->task->cdb_size = acb->ioh->cmd_len;
1036 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
1037 acb->task->expxferlen = acb->ioh->dxfer_len;
1039 data.size = 0;
1040 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
1041 if (acb->ioh->iovec_count == 0) {
1042 data.data = acb->ioh->dxferp;
1043 data.size = acb->ioh->dxfer_len;
1044 } else {
1045 scsi_task_set_iov_out(acb->task,
1046 (struct scsi_iovec *) acb->ioh->dxferp,
1047 acb->ioh->iovec_count);
1051 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
1052 iscsi_aio_ioctl_cb,
1053 (data.size > 0) ? &data : NULL,
1054 acb) != 0) {
1055 scsi_free_scsi_task(acb->task);
1056 qemu_aio_unref(acb);
1057 return NULL;
1060 /* tell libiscsi to read straight into the buffer we got from ioctl */
1061 if (acb->task->xfer_dir == SCSI_XFER_READ) {
1062 if (acb->ioh->iovec_count == 0) {
1063 scsi_task_add_data_in_buffer(acb->task,
1064 acb->ioh->dxfer_len,
1065 acb->ioh->dxferp);
1066 } else {
1067 scsi_task_set_iov_in(acb->task,
1068 (struct scsi_iovec *) acb->ioh->dxferp,
1069 acb->ioh->iovec_count);
1073 iscsi_set_events(iscsilun);
1075 return &acb->common;
1078 #endif
1080 static int64_t
1081 iscsi_getlength(BlockDriverState *bs)
1083 IscsiLun *iscsilun = bs->opaque;
1084 int64_t len;
1086 len = iscsilun->num_blocks;
1087 len *= iscsilun->block_size;
1089 return len;
1092 static int
1093 coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs, int64_t offset, int count)
1095 IscsiLun *iscsilun = bs->opaque;
1096 struct IscsiTask iTask;
1097 struct unmap_list list;
1099 if (!is_byte_request_lun_aligned(offset, count, iscsilun)) {
1100 return -ENOTSUP;
1103 if (!iscsilun->lbp.lbpu) {
1104 /* UNMAP is not supported by the target */
1105 return 0;
1108 list.lba = offset / iscsilun->block_size;
1109 list.num = count / iscsilun->block_size;
1111 iscsi_co_init_iscsitask(iscsilun, &iTask);
1112 retry:
1113 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
1114 iscsi_co_generic_cb, &iTask) == NULL) {
1115 return -ENOMEM;
1118 while (!iTask.complete) {
1119 iscsi_set_events(iscsilun);
1120 qemu_coroutine_yield();
1123 if (iTask.task != NULL) {
1124 scsi_free_scsi_task(iTask.task);
1125 iTask.task = NULL;
1128 if (iTask.do_retry) {
1129 iTask.complete = 0;
1130 goto retry;
1133 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
1134 /* the target might fail with a check condition if it
1135 is not happy with the alignment of the UNMAP request
1136 we silently fail in this case */
1137 return 0;
1140 if (iTask.status != SCSI_STATUS_GOOD) {
1141 return iTask.err_code;
1144 iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS,
1145 count >> BDRV_SECTOR_BITS);
1147 return 0;
1150 static int
1151 coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1152 int count, BdrvRequestFlags flags)
1154 IscsiLun *iscsilun = bs->opaque;
1155 struct IscsiTask iTask;
1156 uint64_t lba;
1157 uint32_t nb_blocks;
1158 bool use_16_for_ws = iscsilun->use_16_for_rw;
1160 if (!is_byte_request_lun_aligned(offset, count, iscsilun)) {
1161 return -ENOTSUP;
1164 if (flags & BDRV_REQ_MAY_UNMAP) {
1165 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
1166 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1167 use_16_for_ws = true;
1169 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
1170 /* WRITESAME16 with UNMAP is not supported by the target,
1171 * fall back and try WRITESAME10/16 without UNMAP */
1172 flags &= ~BDRV_REQ_MAY_UNMAP;
1173 use_16_for_ws = iscsilun->use_16_for_rw;
1177 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
1178 /* WRITESAME without UNMAP is not supported by the target */
1179 return -ENOTSUP;
1182 lba = offset / iscsilun->block_size;
1183 nb_blocks = count / iscsilun->block_size;
1185 if (iscsilun->zeroblock == NULL) {
1186 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size);
1187 if (iscsilun->zeroblock == NULL) {
1188 return -ENOMEM;
1192 iscsi_co_init_iscsitask(iscsilun, &iTask);
1193 retry:
1194 if (use_16_for_ws) {
1195 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1196 iscsilun->zeroblock, iscsilun->block_size,
1197 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1198 0, 0, iscsi_co_generic_cb, &iTask);
1199 } else {
1200 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
1201 iscsilun->zeroblock, iscsilun->block_size,
1202 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1203 0, 0, iscsi_co_generic_cb, &iTask);
1205 if (iTask.task == NULL) {
1206 return -ENOMEM;
1209 while (!iTask.complete) {
1210 iscsi_set_events(iscsilun);
1211 qemu_coroutine_yield();
1214 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1215 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
1216 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
1217 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
1218 /* WRITE SAME is not supported by the target */
1219 iscsilun->has_write_same = false;
1220 scsi_free_scsi_task(iTask.task);
1221 return -ENOTSUP;
1224 if (iTask.task != NULL) {
1225 scsi_free_scsi_task(iTask.task);
1226 iTask.task = NULL;
1229 if (iTask.do_retry) {
1230 iTask.complete = 0;
1231 goto retry;
1234 if (iTask.status != SCSI_STATUS_GOOD) {
1235 iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS,
1236 count >> BDRV_SECTOR_BITS);
1237 return iTask.err_code;
1240 if (flags & BDRV_REQ_MAY_UNMAP) {
1241 iscsi_allocmap_set_invalid(iscsilun, offset >> BDRV_SECTOR_BITS,
1242 count >> BDRV_SECTOR_BITS);
1243 } else {
1244 iscsi_allocmap_set_allocated(iscsilun, offset >> BDRV_SECTOR_BITS,
1245 count >> BDRV_SECTOR_BITS);
1248 return 0;
1251 static void parse_chap(struct iscsi_context *iscsi, const char *target,
1252 Error **errp)
1254 QemuOptsList *list;
1255 QemuOpts *opts;
1256 const char *user = NULL;
1257 const char *password = NULL;
1258 const char *secretid;
1259 char *secret = NULL;
1261 list = qemu_find_opts("iscsi");
1262 if (!list) {
1263 return;
1266 opts = qemu_opts_find(list, target);
1267 if (opts == NULL) {
1268 opts = QTAILQ_FIRST(&list->head);
1269 if (!opts) {
1270 return;
1274 user = qemu_opt_get(opts, "user");
1275 if (!user) {
1276 return;
1279 secretid = qemu_opt_get(opts, "password-secret");
1280 password = qemu_opt_get(opts, "password");
1281 if (secretid && password) {
1282 error_setg(errp, "'password' and 'password-secret' properties are "
1283 "mutually exclusive");
1284 return;
1286 if (secretid) {
1287 secret = qcrypto_secret_lookup_as_utf8(secretid, errp);
1288 if (!secret) {
1289 return;
1291 password = secret;
1292 } else if (!password) {
1293 error_setg(errp, "CHAP username specified but no password was given");
1294 return;
1297 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
1298 error_setg(errp, "Failed to set initiator username and password");
1301 g_free(secret);
1304 static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
1305 Error **errp)
1307 QemuOptsList *list;
1308 QemuOpts *opts;
1309 const char *digest = NULL;
1311 list = qemu_find_opts("iscsi");
1312 if (!list) {
1313 return;
1316 opts = qemu_opts_find(list, target);
1317 if (opts == NULL) {
1318 opts = QTAILQ_FIRST(&list->head);
1319 if (!opts) {
1320 return;
1324 digest = qemu_opt_get(opts, "header-digest");
1325 if (!digest) {
1326 return;
1329 if (!strcmp(digest, "CRC32C")) {
1330 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1331 } else if (!strcmp(digest, "NONE")) {
1332 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1333 } else if (!strcmp(digest, "CRC32C-NONE")) {
1334 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1335 } else if (!strcmp(digest, "NONE-CRC32C")) {
1336 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1337 } else {
1338 error_setg(errp, "Invalid header-digest setting : %s", digest);
1342 static char *parse_initiator_name(const char *target)
1344 QemuOptsList *list;
1345 QemuOpts *opts;
1346 const char *name;
1347 char *iscsi_name;
1348 UuidInfo *uuid_info;
1350 list = qemu_find_opts("iscsi");
1351 if (list) {
1352 opts = qemu_opts_find(list, target);
1353 if (!opts) {
1354 opts = QTAILQ_FIRST(&list->head);
1356 if (opts) {
1357 name = qemu_opt_get(opts, "initiator-name");
1358 if (name) {
1359 return g_strdup(name);
1364 uuid_info = qmp_query_uuid(NULL);
1365 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1366 name = qemu_get_vm_name();
1367 } else {
1368 name = uuid_info->UUID;
1370 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1371 name ? ":" : "", name ? name : "");
1372 qapi_free_UuidInfo(uuid_info);
1373 return iscsi_name;
1376 static int parse_timeout(const char *target)
1378 QemuOptsList *list;
1379 QemuOpts *opts;
1380 const char *timeout;
1382 list = qemu_find_opts("iscsi");
1383 if (list) {
1384 opts = qemu_opts_find(list, target);
1385 if (!opts) {
1386 opts = QTAILQ_FIRST(&list->head);
1388 if (opts) {
1389 timeout = qemu_opt_get(opts, "timeout");
1390 if (timeout) {
1391 return atoi(timeout);
1396 return 0;
1399 static void iscsi_nop_timed_event(void *opaque)
1401 IscsiLun *iscsilun = opaque;
1403 aio_context_acquire(iscsilun->aio_context);
1404 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) {
1405 error_report("iSCSI: NOP timeout. Reconnecting...");
1406 iscsilun->request_timed_out = true;
1407 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1408 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1409 goto out;
1412 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1413 iscsi_set_events(iscsilun);
1415 out:
1416 aio_context_release(iscsilun->aio_context);
1419 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
1421 struct scsi_task *task = NULL;
1422 struct scsi_readcapacity10 *rc10 = NULL;
1423 struct scsi_readcapacity16 *rc16 = NULL;
1424 int retries = ISCSI_CMD_RETRIES;
1426 do {
1427 if (task != NULL) {
1428 scsi_free_scsi_task(task);
1429 task = NULL;
1432 switch (iscsilun->type) {
1433 case TYPE_DISK:
1434 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1435 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1436 rc16 = scsi_datain_unmarshall(task);
1437 if (rc16 == NULL) {
1438 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1439 } else {
1440 iscsilun->block_size = rc16->block_length;
1441 iscsilun->num_blocks = rc16->returned_lba + 1;
1442 iscsilun->lbpme = !!rc16->lbpme;
1443 iscsilun->lbprz = !!rc16->lbprz;
1444 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
1446 break;
1448 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1449 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
1450 break;
1452 /* Fall through and try READ CAPACITY(10) instead. */
1453 case TYPE_ROM:
1454 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1455 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1456 rc10 = scsi_datain_unmarshall(task);
1457 if (rc10 == NULL) {
1458 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1459 } else {
1460 iscsilun->block_size = rc10->block_size;
1461 if (rc10->lba == 0) {
1462 /* blank disk loaded */
1463 iscsilun->num_blocks = 0;
1464 } else {
1465 iscsilun->num_blocks = rc10->lba + 1;
1469 break;
1470 default:
1471 return;
1473 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1474 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1475 && retries-- > 0);
1477 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1478 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command");
1479 } else if (!iscsilun->block_size ||
1480 iscsilun->block_size % BDRV_SECTOR_SIZE) {
1481 error_setg(errp, "iSCSI: the target returned an invalid "
1482 "block size of %d.", iscsilun->block_size);
1484 if (task) {
1485 scsi_free_scsi_task(task);
1489 /* TODO Convert to fine grained options */
1490 static QemuOptsList runtime_opts = {
1491 .name = "iscsi",
1492 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1493 .desc = {
1495 .name = "filename",
1496 .type = QEMU_OPT_STRING,
1497 .help = "URL to the iscsi image",
1499 { /* end of list */ }
1503 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
1504 int evpd, int pc, void **inq, Error **errp)
1506 int full_size;
1507 struct scsi_task *task = NULL;
1508 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1509 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1510 goto fail;
1512 full_size = scsi_datain_getfullsize(task);
1513 if (full_size > task->datain.size) {
1514 scsi_free_scsi_task(task);
1516 /* we need more data for the full list */
1517 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1518 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1519 goto fail;
1523 *inq = scsi_datain_unmarshall(task);
1524 if (*inq == NULL) {
1525 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1526 goto fail_with_err;
1529 return task;
1531 fail:
1532 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1533 iscsi_get_error(iscsi));
1534 fail_with_err:
1535 if (task != NULL) {
1536 scsi_free_scsi_task(task);
1538 return NULL;
1541 static void iscsi_detach_aio_context(BlockDriverState *bs)
1543 IscsiLun *iscsilun = bs->opaque;
1545 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi),
1546 false, NULL, NULL, NULL, NULL);
1547 iscsilun->events = 0;
1549 if (iscsilun->nop_timer) {
1550 timer_del(iscsilun->nop_timer);
1551 timer_free(iscsilun->nop_timer);
1552 iscsilun->nop_timer = NULL;
1554 if (iscsilun->event_timer) {
1555 timer_del(iscsilun->event_timer);
1556 timer_free(iscsilun->event_timer);
1557 iscsilun->event_timer = NULL;
1561 static void iscsi_attach_aio_context(BlockDriverState *bs,
1562 AioContext *new_context)
1564 IscsiLun *iscsilun = bs->opaque;
1566 iscsilun->aio_context = new_context;
1567 iscsi_set_events(iscsilun);
1569 /* Set up a timer for sending out iSCSI NOPs */
1570 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1571 QEMU_CLOCK_REALTIME, SCALE_MS,
1572 iscsi_nop_timed_event, iscsilun);
1573 timer_mod(iscsilun->nop_timer,
1574 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1576 /* Set up a timer for periodic calls to iscsi_set_events and to
1577 * scan for command timeout */
1578 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context,
1579 QEMU_CLOCK_REALTIME, SCALE_MS,
1580 iscsi_timed_check_events, iscsilun);
1581 timer_mod(iscsilun->event_timer,
1582 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
1585 static void iscsi_modesense_sync(IscsiLun *iscsilun)
1587 struct scsi_task *task;
1588 struct scsi_mode_sense *ms = NULL;
1589 iscsilun->write_protected = false;
1590 iscsilun->dpofua = false;
1592 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1593 1, SCSI_MODESENSE_PC_CURRENT,
1594 0x3F, 0, 255);
1595 if (task == NULL) {
1596 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1597 iscsi_get_error(iscsilun->iscsi));
1598 goto out;
1601 if (task->status != SCSI_STATUS_GOOD) {
1602 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1603 goto out;
1605 ms = scsi_datain_unmarshall(task);
1606 if (!ms) {
1607 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1608 iscsi_get_error(iscsilun->iscsi));
1609 goto out;
1611 iscsilun->write_protected = ms->device_specific_parameter & 0x80;
1612 iscsilun->dpofua = ms->device_specific_parameter & 0x10;
1614 out:
1615 if (task) {
1616 scsi_free_scsi_task(task);
1621 * We support iscsi url's on the form
1622 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1624 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1625 Error **errp)
1627 IscsiLun *iscsilun = bs->opaque;
1628 struct iscsi_context *iscsi = NULL;
1629 struct iscsi_url *iscsi_url = NULL;
1630 struct scsi_task *task = NULL;
1631 struct scsi_inquiry_standard *inq = NULL;
1632 struct scsi_inquiry_supported_pages *inq_vpd;
1633 char *initiator_name = NULL;
1634 QemuOpts *opts;
1635 Error *local_err = NULL;
1636 const char *filename;
1637 int i, ret = 0, timeout = 0;
1639 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1640 qemu_opts_absorb_qdict(opts, options, &local_err);
1641 if (local_err) {
1642 error_propagate(errp, local_err);
1643 ret = -EINVAL;
1644 goto out;
1647 filename = qemu_opt_get(opts, "filename");
1649 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1650 if (iscsi_url == NULL) {
1651 error_setg(errp, "Failed to parse URL : %s", filename);
1652 ret = -EINVAL;
1653 goto out;
1656 memset(iscsilun, 0, sizeof(IscsiLun));
1658 initiator_name = parse_initiator_name(iscsi_url->target);
1660 iscsi = iscsi_create_context(initiator_name);
1661 if (iscsi == NULL) {
1662 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
1663 ret = -ENOMEM;
1664 goto out;
1666 #if LIBISCSI_API_VERSION >= (20160603)
1667 if (iscsi_init_transport(iscsi, iscsi_url->transport)) {
1668 error_setg(errp, ("Error initializing transport."));
1669 ret = -EINVAL;
1670 goto out;
1672 #endif
1673 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1674 error_setg(errp, "iSCSI: Failed to set target name.");
1675 ret = -EINVAL;
1676 goto out;
1679 if (iscsi_url->user[0] != '\0') {
1680 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1681 iscsi_url->passwd);
1682 if (ret != 0) {
1683 error_setg(errp, "Failed to set initiator username and password");
1684 ret = -EINVAL;
1685 goto out;
1689 /* check if we got CHAP username/password via the options */
1690 parse_chap(iscsi, iscsi_url->target, &local_err);
1691 if (local_err != NULL) {
1692 error_propagate(errp, local_err);
1693 ret = -EINVAL;
1694 goto out;
1697 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1698 error_setg(errp, "iSCSI: Failed to set session type to normal.");
1699 ret = -EINVAL;
1700 goto out;
1703 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1705 /* check if we got HEADER_DIGEST via the options */
1706 parse_header_digest(iscsi, iscsi_url->target, &local_err);
1707 if (local_err != NULL) {
1708 error_propagate(errp, local_err);
1709 ret = -EINVAL;
1710 goto out;
1713 /* timeout handling is broken in libiscsi before 1.15.0 */
1714 timeout = parse_timeout(iscsi_url->target);
1715 #if LIBISCSI_API_VERSION >= 20150621
1716 iscsi_set_timeout(iscsi, timeout);
1717 #else
1718 if (timeout) {
1719 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1721 #endif
1723 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1724 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
1725 iscsi_get_error(iscsi));
1726 ret = -EINVAL;
1727 goto out;
1730 iscsilun->iscsi = iscsi;
1731 iscsilun->aio_context = bdrv_get_aio_context(bs);
1732 iscsilun->lun = iscsi_url->lun;
1733 iscsilun->has_write_same = true;
1735 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1736 (void **) &inq, errp);
1737 if (task == NULL) {
1738 ret = -EINVAL;
1739 goto out;
1741 iscsilun->type = inq->periperal_device_type;
1742 scsi_free_scsi_task(task);
1743 task = NULL;
1745 iscsi_modesense_sync(iscsilun);
1746 if (iscsilun->dpofua) {
1747 bs->supported_write_flags = BDRV_REQ_FUA;
1749 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
1751 /* Check the write protect flag of the LUN if we want to write */
1752 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) &&
1753 iscsilun->write_protected) {
1754 error_setg(errp, "Cannot open a write protected LUN as read-write");
1755 ret = -EACCES;
1756 goto out;
1759 iscsi_readcapacity_sync(iscsilun, &local_err);
1760 if (local_err != NULL) {
1761 error_propagate(errp, local_err);
1762 ret = -EINVAL;
1763 goto out;
1765 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1767 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1768 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1769 * will try to read from the device to guess the image format.
1771 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
1772 bs->sg = true;
1775 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1776 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1777 (void **) &inq_vpd, errp);
1778 if (task == NULL) {
1779 ret = -EINVAL;
1780 goto out;
1782 for (i = 0; i < inq_vpd->num_pages; i++) {
1783 struct scsi_task *inq_task;
1784 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1785 struct scsi_inquiry_block_limits *inq_bl;
1786 switch (inq_vpd->pages[i]) {
1787 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1788 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1789 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1790 (void **) &inq_lbp, errp);
1791 if (inq_task == NULL) {
1792 ret = -EINVAL;
1793 goto out;
1795 memcpy(&iscsilun->lbp, inq_lbp,
1796 sizeof(struct scsi_inquiry_logical_block_provisioning));
1797 scsi_free_scsi_task(inq_task);
1798 break;
1799 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1800 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1801 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1802 (void **) &inq_bl, errp);
1803 if (inq_task == NULL) {
1804 ret = -EINVAL;
1805 goto out;
1807 memcpy(&iscsilun->bl, inq_bl,
1808 sizeof(struct scsi_inquiry_block_limits));
1809 scsi_free_scsi_task(inq_task);
1810 break;
1811 default:
1812 break;
1815 scsi_free_scsi_task(task);
1816 task = NULL;
1818 iscsi_attach_aio_context(bs, iscsilun->aio_context);
1820 /* Guess the internal cluster (page) size of the iscsi target by the means
1821 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1822 * reasonable size */
1823 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
1824 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1825 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
1826 iscsilun->block_size) >> BDRV_SECTOR_BITS;
1827 if (iscsilun->lbprz) {
1828 ret = iscsi_allocmap_init(iscsilun, bs->open_flags);
1832 out:
1833 qemu_opts_del(opts);
1834 g_free(initiator_name);
1835 if (iscsi_url != NULL) {
1836 iscsi_destroy_url(iscsi_url);
1838 if (task != NULL) {
1839 scsi_free_scsi_task(task);
1842 if (ret) {
1843 if (iscsi != NULL) {
1844 if (iscsi_is_logged_in(iscsi)) {
1845 iscsi_logout_sync(iscsi);
1847 iscsi_destroy_context(iscsi);
1849 memset(iscsilun, 0, sizeof(IscsiLun));
1851 return ret;
1854 static void iscsi_close(BlockDriverState *bs)
1856 IscsiLun *iscsilun = bs->opaque;
1857 struct iscsi_context *iscsi = iscsilun->iscsi;
1859 iscsi_detach_aio_context(bs);
1860 if (iscsi_is_logged_in(iscsi)) {
1861 iscsi_logout_sync(iscsi);
1863 iscsi_destroy_context(iscsi);
1864 g_free(iscsilun->zeroblock);
1865 iscsi_allocmap_free(iscsilun);
1866 memset(iscsilun, 0, sizeof(IscsiLun));
1869 static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)
1871 /* We don't actually refresh here, but just return data queried in
1872 * iscsi_open(): iscsi targets don't change their limits. */
1874 IscsiLun *iscsilun = bs->opaque;
1875 uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff;
1876 unsigned int block_size = MAX(BDRV_SECTOR_SIZE, iscsilun->block_size);
1878 assert(iscsilun->block_size >= BDRV_SECTOR_SIZE || bs->sg);
1880 bs->bl.request_alignment = block_size;
1882 if (iscsilun->bl.max_xfer_len) {
1883 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len);
1886 if (max_xfer_len * block_size < INT_MAX) {
1887 bs->bl.max_transfer = max_xfer_len * iscsilun->block_size;
1890 if (iscsilun->lbp.lbpu) {
1891 if (iscsilun->bl.max_unmap < 0xffffffff / block_size) {
1892 bs->bl.max_pdiscard =
1893 iscsilun->bl.max_unmap * iscsilun->block_size;
1895 bs->bl.pdiscard_alignment =
1896 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
1897 } else {
1898 bs->bl.pdiscard_alignment = iscsilun->block_size;
1901 if (iscsilun->bl.max_ws_len < 0xffffffff / block_size) {
1902 bs->bl.max_pwrite_zeroes =
1903 iscsilun->bl.max_ws_len * iscsilun->block_size;
1905 if (iscsilun->lbp.lbpws) {
1906 bs->bl.pwrite_zeroes_alignment =
1907 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
1908 } else {
1909 bs->bl.pwrite_zeroes_alignment = iscsilun->block_size;
1911 if (iscsilun->bl.opt_xfer_len &&
1912 iscsilun->bl.opt_xfer_len < INT_MAX / block_size) {
1913 bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len *
1914 iscsilun->block_size);
1918 /* Note that this will not re-establish a connection with an iSCSI target - it
1919 * is effectively a NOP. */
1920 static int iscsi_reopen_prepare(BDRVReopenState *state,
1921 BlockReopenQueue *queue, Error **errp)
1923 IscsiLun *iscsilun = state->bs->opaque;
1925 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) {
1926 error_setg(errp, "Cannot open a write protected LUN as read-write");
1927 return -EACCES;
1929 return 0;
1932 static void iscsi_reopen_commit(BDRVReopenState *reopen_state)
1934 IscsiLun *iscsilun = reopen_state->bs->opaque;
1936 /* the cache.direct status might have changed */
1937 if (iscsilun->allocmap != NULL) {
1938 iscsi_allocmap_init(iscsilun, reopen_state->flags);
1942 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1944 IscsiLun *iscsilun = bs->opaque;
1945 Error *local_err = NULL;
1947 if (iscsilun->type != TYPE_DISK) {
1948 return -ENOTSUP;
1951 iscsi_readcapacity_sync(iscsilun, &local_err);
1952 if (local_err != NULL) {
1953 error_free(local_err);
1954 return -EIO;
1957 if (offset > iscsi_getlength(bs)) {
1958 return -EINVAL;
1961 if (iscsilun->allocmap != NULL) {
1962 iscsi_allocmap_init(iscsilun, bs->open_flags);
1965 return 0;
1968 static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp)
1970 int ret = 0;
1971 int64_t total_size = 0;
1972 BlockDriverState *bs;
1973 IscsiLun *iscsilun = NULL;
1974 QDict *bs_options;
1976 bs = bdrv_new();
1978 /* Read out options */
1979 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1980 BDRV_SECTOR_SIZE);
1981 bs->opaque = g_new0(struct IscsiLun, 1);
1982 iscsilun = bs->opaque;
1984 bs_options = qdict_new();
1985 qdict_put(bs_options, "filename", qstring_from_str(filename));
1986 ret = iscsi_open(bs, bs_options, 0, NULL);
1987 QDECREF(bs_options);
1989 if (ret != 0) {
1990 goto out;
1992 iscsi_detach_aio_context(bs);
1993 if (iscsilun->type != TYPE_DISK) {
1994 ret = -ENODEV;
1995 goto out;
1997 if (bs->total_sectors < total_size) {
1998 ret = -ENOSPC;
1999 goto out;
2002 ret = 0;
2003 out:
2004 if (iscsilun->iscsi != NULL) {
2005 iscsi_destroy_context(iscsilun->iscsi);
2007 g_free(bs->opaque);
2008 bs->opaque = NULL;
2009 bdrv_unref(bs);
2010 return ret;
2013 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2015 IscsiLun *iscsilun = bs->opaque;
2016 bdi->unallocated_blocks_are_zero = iscsilun->lbprz;
2017 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
2018 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
2019 return 0;
2022 static void iscsi_invalidate_cache(BlockDriverState *bs,
2023 Error **errp)
2025 IscsiLun *iscsilun = bs->opaque;
2026 iscsi_allocmap_invalidate(iscsilun);
2029 static QemuOptsList iscsi_create_opts = {
2030 .name = "iscsi-create-opts",
2031 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
2032 .desc = {
2034 .name = BLOCK_OPT_SIZE,
2035 .type = QEMU_OPT_SIZE,
2036 .help = "Virtual disk size"
2038 { /* end of list */ }
2042 static BlockDriver bdrv_iscsi = {
2043 .format_name = "iscsi",
2044 .protocol_name = "iscsi",
2046 .instance_size = sizeof(IscsiLun),
2047 .bdrv_needs_filename = true,
2048 .bdrv_file_open = iscsi_open,
2049 .bdrv_close = iscsi_close,
2050 .bdrv_create = iscsi_create,
2051 .create_opts = &iscsi_create_opts,
2052 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2053 .bdrv_reopen_commit = iscsi_reopen_commit,
2054 .bdrv_invalidate_cache = iscsi_invalidate_cache,
2056 .bdrv_getlength = iscsi_getlength,
2057 .bdrv_get_info = iscsi_get_info,
2058 .bdrv_truncate = iscsi_truncate,
2059 .bdrv_refresh_limits = iscsi_refresh_limits,
2061 .bdrv_co_get_block_status = iscsi_co_get_block_status,
2062 .bdrv_co_pdiscard = iscsi_co_pdiscard,
2063 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2064 .bdrv_co_readv = iscsi_co_readv,
2065 .bdrv_co_writev_flags = iscsi_co_writev_flags,
2066 .bdrv_co_flush_to_disk = iscsi_co_flush,
2068 #ifdef __linux__
2069 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2070 #endif
2072 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2073 .bdrv_attach_aio_context = iscsi_attach_aio_context,
2076 #if LIBISCSI_API_VERSION >= (20160603)
2077 static BlockDriver bdrv_iser = {
2078 .format_name = "iser",
2079 .protocol_name = "iser",
2081 .instance_size = sizeof(IscsiLun),
2082 .bdrv_needs_filename = true,
2083 .bdrv_file_open = iscsi_open,
2084 .bdrv_close = iscsi_close,
2085 .bdrv_create = iscsi_create,
2086 .create_opts = &iscsi_create_opts,
2087 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2088 .bdrv_reopen_commit = iscsi_reopen_commit,
2089 .bdrv_invalidate_cache = iscsi_invalidate_cache,
2091 .bdrv_getlength = iscsi_getlength,
2092 .bdrv_get_info = iscsi_get_info,
2093 .bdrv_truncate = iscsi_truncate,
2094 .bdrv_refresh_limits = iscsi_refresh_limits,
2096 .bdrv_co_get_block_status = iscsi_co_get_block_status,
2097 .bdrv_co_pdiscard = iscsi_co_pdiscard,
2098 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2099 .bdrv_co_readv = iscsi_co_readv,
2100 .bdrv_co_writev_flags = iscsi_co_writev_flags,
2101 .bdrv_co_flush_to_disk = iscsi_co_flush,
2103 #ifdef __linux__
2104 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2105 #endif
2107 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2108 .bdrv_attach_aio_context = iscsi_attach_aio_context,
2110 #endif
2112 static void iscsi_block_init(void)
2114 bdrv_register(&bdrv_iscsi);
2115 #if LIBISCSI_API_VERSION >= (20160603)
2116 bdrv_register(&bdrv_iser);
2117 #endif
2120 block_init(iscsi_block_init);