unset RAMBlock idstr when unregister MemoryRegion
[qemu/ar7.git] / block / iscsi.c
blob6f87605e72f0c9bc366dd08c5da32f67dec742f4
1 /*
2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2014 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 "config-host.h"
28 #include <poll.h>
29 #include <arpa/inet.h>
30 #include "qemu-common.h"
31 #include "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "qemu/bitops.h"
34 #include "qemu/bitmap.h"
35 #include "block/block_int.h"
36 #include "trace.h"
37 #include "block/scsi.h"
38 #include "qemu/iov.h"
39 #include "sysemu/sysemu.h"
40 #include "qmp-commands.h"
42 #include <iscsi/iscsi.h>
43 #include <iscsi/scsi-lowlevel.h>
45 #ifdef __linux__
46 #include <scsi/sg.h>
47 #include <block/scsi.h>
48 #endif
50 typedef struct IscsiLun {
51 struct iscsi_context *iscsi;
52 AioContext *aio_context;
53 int lun;
54 enum scsi_inquiry_peripheral_device_type type;
55 int block_size;
56 uint64_t num_blocks;
57 int events;
58 QEMUTimer *nop_timer;
59 uint8_t lbpme;
60 uint8_t lbprz;
61 uint8_t has_write_same;
62 struct scsi_inquiry_logical_block_provisioning lbp;
63 struct scsi_inquiry_block_limits bl;
64 unsigned char *zeroblock;
65 unsigned long *allocationmap;
66 int cluster_sectors;
67 } IscsiLun;
69 typedef struct IscsiTask {
70 int status;
71 int complete;
72 int retries;
73 int do_retry;
74 struct scsi_task *task;
75 Coroutine *co;
76 QEMUBH *bh;
77 IscsiLun *iscsilun;
78 } IscsiTask;
80 typedef struct IscsiAIOCB {
81 BlockDriverAIOCB common;
82 QEMUIOVector *qiov;
83 QEMUBH *bh;
84 IscsiLun *iscsilun;
85 struct scsi_task *task;
86 uint8_t *buf;
87 int status;
88 int canceled;
89 int retries;
90 int64_t sector_num;
91 int nb_sectors;
92 #ifdef __linux__
93 sg_io_hdr_t *ioh;
94 #endif
95 } IscsiAIOCB;
97 #define NOP_INTERVAL 5000
98 #define MAX_NOP_FAILURES 3
99 #define ISCSI_CMD_RETRIES 5
101 /* this threshhold is a trade-off knob to choose between
102 * the potential additional overhead of an extra GET_LBA_STATUS request
103 * vs. unnecessarily reading a lot of zero sectors over the wire.
104 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
105 * sectors we check the allocation status of the area covered by the
106 * request first if the allocationmap indicates that the area might be
107 * unallocated. */
108 #define ISCSI_CHECKALLOC_THRES 64
110 static void
111 iscsi_bh_cb(void *p)
113 IscsiAIOCB *acb = p;
115 qemu_bh_delete(acb->bh);
117 g_free(acb->buf);
118 acb->buf = NULL;
120 if (acb->canceled == 0) {
121 acb->common.cb(acb->common.opaque, acb->status);
124 if (acb->task != NULL) {
125 scsi_free_scsi_task(acb->task);
126 acb->task = NULL;
129 qemu_aio_release(acb);
132 static void
133 iscsi_schedule_bh(IscsiAIOCB *acb)
135 if (acb->bh) {
136 return;
138 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
139 qemu_bh_schedule(acb->bh);
142 static void iscsi_co_generic_bh_cb(void *opaque)
144 struct IscsiTask *iTask = opaque;
145 qemu_bh_delete(iTask->bh);
146 qemu_coroutine_enter(iTask->co, NULL);
149 static void
150 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
151 void *command_data, void *opaque)
153 struct IscsiTask *iTask = opaque;
154 struct scsi_task *task = command_data;
156 iTask->complete = 1;
157 iTask->status = status;
158 iTask->do_retry = 0;
159 iTask->task = task;
161 if (iTask->retries-- > 0 && status == SCSI_STATUS_CHECK_CONDITION
162 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
163 error_report("iSCSI CheckCondition: %s", iscsi_get_error(iscsi));
164 iTask->do_retry = 1;
165 goto out;
168 if (status != SCSI_STATUS_GOOD) {
169 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi));
172 out:
173 if (iTask->co) {
174 iTask->bh = aio_bh_new(iTask->iscsilun->aio_context,
175 iscsi_co_generic_bh_cb, iTask);
176 qemu_bh_schedule(iTask->bh);
180 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
182 *iTask = (struct IscsiTask) {
183 .co = qemu_coroutine_self(),
184 .retries = ISCSI_CMD_RETRIES,
185 .iscsilun = iscsilun,
189 static void
190 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
191 void *private_data)
193 IscsiAIOCB *acb = private_data;
195 acb->status = -ECANCELED;
196 iscsi_schedule_bh(acb);
199 static void
200 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
202 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
203 IscsiLun *iscsilun = acb->iscsilun;
205 if (acb->status != -EINPROGRESS) {
206 return;
209 acb->canceled = 1;
211 /* send a task mgmt call to the target to cancel the task on the target */
212 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
213 iscsi_abort_task_cb, acb);
215 while (acb->status == -EINPROGRESS) {
216 aio_poll(iscsilun->aio_context, true);
220 static const AIOCBInfo iscsi_aiocb_info = {
221 .aiocb_size = sizeof(IscsiAIOCB),
222 .cancel = iscsi_aio_cancel,
226 static void iscsi_process_read(void *arg);
227 static void iscsi_process_write(void *arg);
229 static void
230 iscsi_set_events(IscsiLun *iscsilun)
232 struct iscsi_context *iscsi = iscsilun->iscsi;
233 int ev;
235 /* We always register a read handler. */
236 ev = POLLIN;
237 ev |= iscsi_which_events(iscsi);
238 if (ev != iscsilun->events) {
239 aio_set_fd_handler(iscsilun->aio_context,
240 iscsi_get_fd(iscsi),
241 iscsi_process_read,
242 (ev & POLLOUT) ? iscsi_process_write : NULL,
243 iscsilun);
247 iscsilun->events = ev;
250 static void
251 iscsi_process_read(void *arg)
253 IscsiLun *iscsilun = arg;
254 struct iscsi_context *iscsi = iscsilun->iscsi;
256 iscsi_service(iscsi, POLLIN);
257 iscsi_set_events(iscsilun);
260 static void
261 iscsi_process_write(void *arg)
263 IscsiLun *iscsilun = arg;
264 struct iscsi_context *iscsi = iscsilun->iscsi;
266 iscsi_service(iscsi, POLLOUT);
267 iscsi_set_events(iscsilun);
270 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
272 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
275 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
277 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
280 static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
281 IscsiLun *iscsilun)
283 if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
284 (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
285 error_report("iSCSI misaligned request: "
286 "iscsilun->block_size %u, sector_num %" PRIi64
287 ", nb_sectors %d",
288 iscsilun->block_size, sector_num, nb_sectors);
289 return 0;
291 return 1;
294 static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num,
295 int nb_sectors)
297 if (iscsilun->allocationmap == NULL) {
298 return;
300 bitmap_set(iscsilun->allocationmap,
301 sector_num / iscsilun->cluster_sectors,
302 DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors));
305 static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num,
306 int nb_sectors)
308 int64_t cluster_num, nb_clusters;
309 if (iscsilun->allocationmap == NULL) {
310 return;
312 cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors);
313 nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors
314 - cluster_num;
315 if (nb_clusters > 0) {
316 bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters);
320 static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,
321 int64_t sector_num, int nb_sectors,
322 QEMUIOVector *iov)
324 IscsiLun *iscsilun = bs->opaque;
325 struct IscsiTask iTask;
326 uint64_t lba;
327 uint32_t num_sectors;
328 uint8_t *data = NULL;
329 uint8_t *buf = NULL;
331 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
332 return -EINVAL;
335 lba = sector_qemu2lun(sector_num, iscsilun);
336 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
337 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
338 /* if the iovec only contains one buffer we can pass it directly */
339 if (iov->niov == 1) {
340 data = iov->iov[0].iov_base;
341 } else {
342 size_t size = MIN(nb_sectors * BDRV_SECTOR_SIZE, iov->size);
343 buf = g_malloc(size);
344 qemu_iovec_to_buf(iov, 0, buf, size);
345 data = buf;
347 #endif
348 iscsi_co_init_iscsitask(iscsilun, &iTask);
349 retry:
350 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
351 data, num_sectors * iscsilun->block_size,
352 iscsilun->block_size, 0, 0, 0, 0, 0,
353 iscsi_co_generic_cb, &iTask);
354 if (iTask.task == NULL) {
355 g_free(buf);
356 return -ENOMEM;
358 #if defined(LIBISCSI_FEATURE_IOVECTOR)
359 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
360 iov->niov);
361 #endif
362 while (!iTask.complete) {
363 iscsi_set_events(iscsilun);
364 qemu_coroutine_yield();
367 if (iTask.task != NULL) {
368 scsi_free_scsi_task(iTask.task);
369 iTask.task = NULL;
372 if (iTask.do_retry) {
373 iTask.complete = 0;
374 goto retry;
377 g_free(buf);
379 if (iTask.status != SCSI_STATUS_GOOD) {
380 return -EIO;
383 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
385 return 0;
389 #if defined(LIBISCSI_FEATURE_IOVECTOR)
390 static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun,
391 int64_t sector_num, int nb_sectors)
393 unsigned long size;
394 if (iscsilun->allocationmap == NULL) {
395 return true;
397 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
398 return !(find_next_bit(iscsilun->allocationmap, size,
399 sector_num / iscsilun->cluster_sectors) == size);
402 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
403 int64_t sector_num,
404 int nb_sectors, int *pnum)
406 IscsiLun *iscsilun = bs->opaque;
407 struct scsi_get_lba_status *lbas = NULL;
408 struct scsi_lba_status_descriptor *lbasd = NULL;
409 struct IscsiTask iTask;
410 int64_t ret;
412 iscsi_co_init_iscsitask(iscsilun, &iTask);
414 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
415 ret = -EINVAL;
416 goto out;
419 /* default to all sectors allocated */
420 ret = BDRV_BLOCK_DATA;
421 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
422 *pnum = nb_sectors;
424 /* LUN does not support logical block provisioning */
425 if (iscsilun->lbpme == 0) {
426 goto out;
429 retry:
430 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
431 sector_qemu2lun(sector_num, iscsilun),
432 8 + 16, iscsi_co_generic_cb,
433 &iTask) == NULL) {
434 ret = -ENOMEM;
435 goto out;
438 while (!iTask.complete) {
439 iscsi_set_events(iscsilun);
440 qemu_coroutine_yield();
443 if (iTask.do_retry) {
444 if (iTask.task != NULL) {
445 scsi_free_scsi_task(iTask.task);
446 iTask.task = NULL;
448 iTask.complete = 0;
449 goto retry;
452 if (iTask.status != SCSI_STATUS_GOOD) {
453 /* in case the get_lba_status_callout fails (i.e.
454 * because the device is busy or the cmd is not
455 * supported) we pretend all blocks are allocated
456 * for backwards compatibility */
457 goto out;
460 lbas = scsi_datain_unmarshall(iTask.task);
461 if (lbas == NULL) {
462 ret = -EIO;
463 goto out;
466 lbasd = &lbas->descriptors[0];
468 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
469 ret = -EIO;
470 goto out;
473 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
475 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
476 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
477 ret &= ~BDRV_BLOCK_DATA;
478 if (iscsilun->lbprz) {
479 ret |= BDRV_BLOCK_ZERO;
483 if (ret & BDRV_BLOCK_ZERO) {
484 iscsi_allocationmap_clear(iscsilun, sector_num, *pnum);
485 } else {
486 iscsi_allocationmap_set(iscsilun, sector_num, *pnum);
489 if (*pnum > nb_sectors) {
490 *pnum = nb_sectors;
492 out:
493 if (iTask.task != NULL) {
494 scsi_free_scsi_task(iTask.task);
496 return ret;
499 #endif /* LIBISCSI_FEATURE_IOVECTOR */
502 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
503 int64_t sector_num, int nb_sectors,
504 QEMUIOVector *iov)
506 IscsiLun *iscsilun = bs->opaque;
507 struct IscsiTask iTask;
508 uint64_t lba;
509 uint32_t num_sectors;
510 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
511 int i;
512 #endif
514 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
515 return -EINVAL;
518 #if defined(LIBISCSI_FEATURE_IOVECTOR)
519 if (iscsilun->lbprz && nb_sectors >= ISCSI_CHECKALLOC_THRES &&
520 !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
521 int64_t ret;
522 int pnum;
523 ret = iscsi_co_get_block_status(bs, sector_num, INT_MAX, &pnum);
524 if (ret < 0) {
525 return ret;
527 if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) {
528 qemu_iovec_memset(iov, 0, 0x00, iov->size);
529 return 0;
532 #endif
534 lba = sector_qemu2lun(sector_num, iscsilun);
535 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
537 iscsi_co_init_iscsitask(iscsilun, &iTask);
538 retry:
539 switch (iscsilun->type) {
540 case TYPE_DISK:
541 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
542 num_sectors * iscsilun->block_size,
543 iscsilun->block_size, 0, 0, 0, 0, 0,
544 iscsi_co_generic_cb, &iTask);
545 break;
546 default:
547 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
548 num_sectors * iscsilun->block_size,
549 iscsilun->block_size,
550 #if !defined(CONFIG_LIBISCSI_1_4) /* API change from 1.4.0 to 1.5.0 */
551 0, 0, 0, 0, 0,
552 #endif
553 iscsi_co_generic_cb, &iTask);
554 break;
556 if (iTask.task == NULL) {
557 return -ENOMEM;
559 #if defined(LIBISCSI_FEATURE_IOVECTOR)
560 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
561 #else
562 for (i = 0; i < iov->niov; i++) {
563 scsi_task_add_data_in_buffer(iTask.task,
564 iov->iov[i].iov_len,
565 iov->iov[i].iov_base);
567 #endif
569 while (!iTask.complete) {
570 iscsi_set_events(iscsilun);
571 qemu_coroutine_yield();
574 if (iTask.task != NULL) {
575 scsi_free_scsi_task(iTask.task);
576 iTask.task = NULL;
579 if (iTask.do_retry) {
580 iTask.complete = 0;
581 goto retry;
584 if (iTask.status != SCSI_STATUS_GOOD) {
585 return -EIO;
588 return 0;
591 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
593 IscsiLun *iscsilun = bs->opaque;
594 struct IscsiTask iTask;
596 if (bs->sg) {
597 return 0;
600 iscsi_co_init_iscsitask(iscsilun, &iTask);
602 retry:
603 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
604 0, iscsi_co_generic_cb, &iTask) == NULL) {
605 return -ENOMEM;
608 while (!iTask.complete) {
609 iscsi_set_events(iscsilun);
610 qemu_coroutine_yield();
613 if (iTask.task != NULL) {
614 scsi_free_scsi_task(iTask.task);
615 iTask.task = NULL;
618 if (iTask.do_retry) {
619 iTask.complete = 0;
620 goto retry;
623 if (iTask.status != SCSI_STATUS_GOOD) {
624 return -EIO;
627 return 0;
630 #ifdef __linux__
631 static void
632 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
633 void *command_data, void *opaque)
635 IscsiAIOCB *acb = opaque;
637 g_free(acb->buf);
638 acb->buf = NULL;
640 if (acb->canceled != 0) {
641 return;
644 acb->status = 0;
645 if (status < 0) {
646 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
647 iscsi_get_error(iscsi));
648 acb->status = -EIO;
651 acb->ioh->driver_status = 0;
652 acb->ioh->host_status = 0;
653 acb->ioh->resid = 0;
655 #define SG_ERR_DRIVER_SENSE 0x08
657 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
658 int ss;
660 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
662 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
663 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
664 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
665 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
668 iscsi_schedule_bh(acb);
671 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
672 unsigned long int req, void *buf,
673 BlockDriverCompletionFunc *cb, void *opaque)
675 IscsiLun *iscsilun = bs->opaque;
676 struct iscsi_context *iscsi = iscsilun->iscsi;
677 struct iscsi_data data;
678 IscsiAIOCB *acb;
680 assert(req == SG_IO);
682 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
684 acb->iscsilun = iscsilun;
685 acb->canceled = 0;
686 acb->bh = NULL;
687 acb->status = -EINPROGRESS;
688 acb->buf = NULL;
689 acb->ioh = buf;
691 acb->task = malloc(sizeof(struct scsi_task));
692 if (acb->task == NULL) {
693 error_report("iSCSI: Failed to allocate task for scsi command. %s",
694 iscsi_get_error(iscsi));
695 qemu_aio_release(acb);
696 return NULL;
698 memset(acb->task, 0, sizeof(struct scsi_task));
700 switch (acb->ioh->dxfer_direction) {
701 case SG_DXFER_TO_DEV:
702 acb->task->xfer_dir = SCSI_XFER_WRITE;
703 break;
704 case SG_DXFER_FROM_DEV:
705 acb->task->xfer_dir = SCSI_XFER_READ;
706 break;
707 default:
708 acb->task->xfer_dir = SCSI_XFER_NONE;
709 break;
712 acb->task->cdb_size = acb->ioh->cmd_len;
713 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
714 acb->task->expxferlen = acb->ioh->dxfer_len;
716 data.size = 0;
717 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
718 if (acb->ioh->iovec_count == 0) {
719 data.data = acb->ioh->dxferp;
720 data.size = acb->ioh->dxfer_len;
721 } else {
722 #if defined(LIBISCSI_FEATURE_IOVECTOR)
723 scsi_task_set_iov_out(acb->task,
724 (struct scsi_iovec *) acb->ioh->dxferp,
725 acb->ioh->iovec_count);
726 #else
727 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
729 acb->buf = g_malloc(acb->ioh->dxfer_len);
730 data.data = acb->buf;
731 data.size = iov_to_buf(iov, acb->ioh->iovec_count, 0,
732 acb->buf, acb->ioh->dxfer_len);
733 #endif
737 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
738 iscsi_aio_ioctl_cb,
739 (data.size > 0) ? &data : NULL,
740 acb) != 0) {
741 scsi_free_scsi_task(acb->task);
742 qemu_aio_release(acb);
743 return NULL;
746 /* tell libiscsi to read straight into the buffer we got from ioctl */
747 if (acb->task->xfer_dir == SCSI_XFER_READ) {
748 if (acb->ioh->iovec_count == 0) {
749 scsi_task_add_data_in_buffer(acb->task,
750 acb->ioh->dxfer_len,
751 acb->ioh->dxferp);
752 } else {
753 #if defined(LIBISCSI_FEATURE_IOVECTOR)
754 scsi_task_set_iov_in(acb->task,
755 (struct scsi_iovec *) acb->ioh->dxferp,
756 acb->ioh->iovec_count);
757 #else
758 int i;
759 for (i = 0; i < acb->ioh->iovec_count; i++) {
760 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
762 scsi_task_add_data_in_buffer(acb->task,
763 iov[i].iov_len,
764 iov[i].iov_base);
766 #endif
770 iscsi_set_events(iscsilun);
772 return &acb->common;
776 static void ioctl_cb(void *opaque, int status)
778 int *p_status = opaque;
779 *p_status = status;
782 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
784 IscsiLun *iscsilun = bs->opaque;
785 int status;
787 switch (req) {
788 case SG_GET_VERSION_NUM:
789 *(int *)buf = 30000;
790 break;
791 case SG_GET_SCSI_ID:
792 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
793 break;
794 case SG_IO:
795 status = -EINPROGRESS;
796 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
798 while (status == -EINPROGRESS) {
799 aio_poll(iscsilun->aio_context, true);
802 return 0;
803 default:
804 return -1;
806 return 0;
808 #endif
810 static int64_t
811 iscsi_getlength(BlockDriverState *bs)
813 IscsiLun *iscsilun = bs->opaque;
814 int64_t len;
816 len = iscsilun->num_blocks;
817 len *= iscsilun->block_size;
819 return len;
822 static int
823 coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
824 int nb_sectors)
826 IscsiLun *iscsilun = bs->opaque;
827 struct IscsiTask iTask;
828 struct unmap_list list;
830 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
831 return -EINVAL;
834 if (!iscsilun->lbp.lbpu) {
835 /* UNMAP is not supported by the target */
836 return 0;
839 list.lba = sector_qemu2lun(sector_num, iscsilun);
840 list.num = sector_qemu2lun(nb_sectors, iscsilun);
842 iscsi_co_init_iscsitask(iscsilun, &iTask);
843 retry:
844 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
845 iscsi_co_generic_cb, &iTask) == NULL) {
846 return -ENOMEM;
849 while (!iTask.complete) {
850 iscsi_set_events(iscsilun);
851 qemu_coroutine_yield();
854 if (iTask.task != NULL) {
855 scsi_free_scsi_task(iTask.task);
856 iTask.task = NULL;
859 if (iTask.do_retry) {
860 iTask.complete = 0;
861 goto retry;
864 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
865 /* the target might fail with a check condition if it
866 is not happy with the alignment of the UNMAP request
867 we silently fail in this case */
868 return 0;
871 if (iTask.status != SCSI_STATUS_GOOD) {
872 return -EIO;
875 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
877 return 0;
880 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
882 static int
883 coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
884 int nb_sectors, BdrvRequestFlags flags)
886 IscsiLun *iscsilun = bs->opaque;
887 struct IscsiTask iTask;
888 uint64_t lba;
889 uint32_t nb_blocks;
891 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
892 return -EINVAL;
895 if ((flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->lbp.lbpws) {
896 /* WRITE SAME with UNMAP is not supported by the target,
897 * fall back and try WRITE SAME without UNMAP */
898 flags &= ~BDRV_REQ_MAY_UNMAP;
901 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
902 /* WRITE SAME without UNMAP is not supported by the target */
903 return -ENOTSUP;
906 lba = sector_qemu2lun(sector_num, iscsilun);
907 nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
909 if (iscsilun->zeroblock == NULL) {
910 iscsilun->zeroblock = g_malloc0(iscsilun->block_size);
913 iscsi_co_init_iscsitask(iscsilun, &iTask);
914 retry:
915 if (iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
916 iscsilun->zeroblock, iscsilun->block_size,
917 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
918 0, 0, iscsi_co_generic_cb, &iTask) == NULL) {
919 return -ENOMEM;
922 while (!iTask.complete) {
923 iscsi_set_events(iscsilun);
924 qemu_coroutine_yield();
927 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
928 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
929 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
930 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
931 /* WRITE SAME is not supported by the target */
932 iscsilun->has_write_same = false;
933 scsi_free_scsi_task(iTask.task);
934 return -ENOTSUP;
937 if (iTask.task != NULL) {
938 scsi_free_scsi_task(iTask.task);
939 iTask.task = NULL;
942 if (iTask.do_retry) {
943 iTask.complete = 0;
944 goto retry;
947 if (iTask.status != SCSI_STATUS_GOOD) {
948 return -EIO;
951 if (flags & BDRV_REQ_MAY_UNMAP) {
952 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
953 } else {
954 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
957 return 0;
960 #endif /* SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED */
962 static void parse_chap(struct iscsi_context *iscsi, const char *target,
963 Error **errp)
965 QemuOptsList *list;
966 QemuOpts *opts;
967 const char *user = NULL;
968 const char *password = NULL;
970 list = qemu_find_opts("iscsi");
971 if (!list) {
972 return;
975 opts = qemu_opts_find(list, target);
976 if (opts == NULL) {
977 opts = QTAILQ_FIRST(&list->head);
978 if (!opts) {
979 return;
983 user = qemu_opt_get(opts, "user");
984 if (!user) {
985 return;
988 password = qemu_opt_get(opts, "password");
989 if (!password) {
990 error_setg(errp, "CHAP username specified but no password was given");
991 return;
994 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
995 error_setg(errp, "Failed to set initiator username and password");
999 static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
1000 Error **errp)
1002 QemuOptsList *list;
1003 QemuOpts *opts;
1004 const char *digest = NULL;
1006 list = qemu_find_opts("iscsi");
1007 if (!list) {
1008 return;
1011 opts = qemu_opts_find(list, target);
1012 if (opts == NULL) {
1013 opts = QTAILQ_FIRST(&list->head);
1014 if (!opts) {
1015 return;
1019 digest = qemu_opt_get(opts, "header-digest");
1020 if (!digest) {
1021 return;
1024 if (!strcmp(digest, "CRC32C")) {
1025 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1026 } else if (!strcmp(digest, "NONE")) {
1027 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1028 } else if (!strcmp(digest, "CRC32C-NONE")) {
1029 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1030 } else if (!strcmp(digest, "NONE-CRC32C")) {
1031 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1032 } else {
1033 error_setg(errp, "Invalid header-digest setting : %s", digest);
1037 static char *parse_initiator_name(const char *target)
1039 QemuOptsList *list;
1040 QemuOpts *opts;
1041 const char *name;
1042 char *iscsi_name;
1043 UuidInfo *uuid_info;
1045 list = qemu_find_opts("iscsi");
1046 if (list) {
1047 opts = qemu_opts_find(list, target);
1048 if (!opts) {
1049 opts = QTAILQ_FIRST(&list->head);
1051 if (opts) {
1052 name = qemu_opt_get(opts, "initiator-name");
1053 if (name) {
1054 return g_strdup(name);
1059 uuid_info = qmp_query_uuid(NULL);
1060 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1061 name = qemu_get_vm_name();
1062 } else {
1063 name = uuid_info->UUID;
1065 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1066 name ? ":" : "", name ? name : "");
1067 qapi_free_UuidInfo(uuid_info);
1068 return iscsi_name;
1071 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1072 static void iscsi_nop_timed_event(void *opaque)
1074 IscsiLun *iscsilun = opaque;
1076 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
1077 error_report("iSCSI: NOP timeout. Reconnecting...");
1078 iscsi_reconnect(iscsilun->iscsi);
1081 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1082 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1083 return;
1086 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1087 iscsi_set_events(iscsilun);
1089 #endif
1091 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
1093 struct scsi_task *task = NULL;
1094 struct scsi_readcapacity10 *rc10 = NULL;
1095 struct scsi_readcapacity16 *rc16 = NULL;
1096 int retries = ISCSI_CMD_RETRIES;
1098 do {
1099 if (task != NULL) {
1100 scsi_free_scsi_task(task);
1101 task = NULL;
1104 switch (iscsilun->type) {
1105 case TYPE_DISK:
1106 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1107 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1108 rc16 = scsi_datain_unmarshall(task);
1109 if (rc16 == NULL) {
1110 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1111 } else {
1112 iscsilun->block_size = rc16->block_length;
1113 iscsilun->num_blocks = rc16->returned_lba + 1;
1114 iscsilun->lbpme = rc16->lbpme;
1115 iscsilun->lbprz = rc16->lbprz;
1118 break;
1119 case TYPE_ROM:
1120 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1121 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1122 rc10 = scsi_datain_unmarshall(task);
1123 if (rc10 == NULL) {
1124 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1125 } else {
1126 iscsilun->block_size = rc10->block_size;
1127 if (rc10->lba == 0) {
1128 /* blank disk loaded */
1129 iscsilun->num_blocks = 0;
1130 } else {
1131 iscsilun->num_blocks = rc10->lba + 1;
1135 break;
1136 default:
1137 return;
1139 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1140 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1141 && retries-- > 0);
1143 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1144 error_setg(errp, "iSCSI: failed to send readcapacity10 command.");
1146 if (task) {
1147 scsi_free_scsi_task(task);
1151 /* TODO Convert to fine grained options */
1152 static QemuOptsList runtime_opts = {
1153 .name = "iscsi",
1154 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1155 .desc = {
1157 .name = "filename",
1158 .type = QEMU_OPT_STRING,
1159 .help = "URL to the iscsi image",
1161 { /* end of list */ }
1165 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
1166 int evpd, int pc, void **inq, Error **errp)
1168 int full_size;
1169 struct scsi_task *task = NULL;
1170 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1171 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1172 goto fail;
1174 full_size = scsi_datain_getfullsize(task);
1175 if (full_size > task->datain.size) {
1176 scsi_free_scsi_task(task);
1178 /* we need more data for the full list */
1179 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1180 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1181 goto fail;
1185 *inq = scsi_datain_unmarshall(task);
1186 if (*inq == NULL) {
1187 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1188 goto fail_with_err;
1191 return task;
1193 fail:
1194 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1195 iscsi_get_error(iscsi));
1196 fail_with_err:
1197 if (task != NULL) {
1198 scsi_free_scsi_task(task);
1200 return NULL;
1203 static void iscsi_detach_aio_context(BlockDriverState *bs)
1205 IscsiLun *iscsilun = bs->opaque;
1207 aio_set_fd_handler(iscsilun->aio_context,
1208 iscsi_get_fd(iscsilun->iscsi),
1209 NULL, NULL, NULL);
1210 iscsilun->events = 0;
1212 if (iscsilun->nop_timer) {
1213 timer_del(iscsilun->nop_timer);
1214 timer_free(iscsilun->nop_timer);
1215 iscsilun->nop_timer = NULL;
1219 static void iscsi_attach_aio_context(BlockDriverState *bs,
1220 AioContext *new_context)
1222 IscsiLun *iscsilun = bs->opaque;
1224 iscsilun->aio_context = new_context;
1225 iscsi_set_events(iscsilun);
1227 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1228 /* Set up a timer for sending out iSCSI NOPs */
1229 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1230 QEMU_CLOCK_REALTIME, SCALE_MS,
1231 iscsi_nop_timed_event, iscsilun);
1232 timer_mod(iscsilun->nop_timer,
1233 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1234 #endif
1238 * We support iscsi url's on the form
1239 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1241 * Note: flags are currently not used by iscsi_open. If this function
1242 * is changed such that flags are used, please examine iscsi_reopen_prepare()
1243 * to see if needs to be changed as well.
1245 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1246 Error **errp)
1248 IscsiLun *iscsilun = bs->opaque;
1249 struct iscsi_context *iscsi = NULL;
1250 struct iscsi_url *iscsi_url = NULL;
1251 struct scsi_task *task = NULL;
1252 struct scsi_inquiry_standard *inq = NULL;
1253 struct scsi_inquiry_supported_pages *inq_vpd;
1254 char *initiator_name = NULL;
1255 QemuOpts *opts;
1256 Error *local_err = NULL;
1257 const char *filename;
1258 int i, ret;
1260 if ((BDRV_SECTOR_SIZE % 512) != 0) {
1261 error_setg(errp, "iSCSI: Invalid BDRV_SECTOR_SIZE. "
1262 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1263 "of 512", BDRV_SECTOR_SIZE);
1264 return -EINVAL;
1267 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1268 qemu_opts_absorb_qdict(opts, options, &local_err);
1269 if (local_err) {
1270 error_propagate(errp, local_err);
1271 ret = -EINVAL;
1272 goto out;
1275 filename = qemu_opt_get(opts, "filename");
1277 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1278 if (iscsi_url == NULL) {
1279 error_setg(errp, "Failed to parse URL : %s", filename);
1280 ret = -EINVAL;
1281 goto out;
1284 memset(iscsilun, 0, sizeof(IscsiLun));
1286 initiator_name = parse_initiator_name(iscsi_url->target);
1288 iscsi = iscsi_create_context(initiator_name);
1289 if (iscsi == NULL) {
1290 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
1291 ret = -ENOMEM;
1292 goto out;
1295 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1296 error_setg(errp, "iSCSI: Failed to set target name.");
1297 ret = -EINVAL;
1298 goto out;
1301 if (iscsi_url->user != NULL) {
1302 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1303 iscsi_url->passwd);
1304 if (ret != 0) {
1305 error_setg(errp, "Failed to set initiator username and password");
1306 ret = -EINVAL;
1307 goto out;
1311 /* check if we got CHAP username/password via the options */
1312 parse_chap(iscsi, iscsi_url->target, &local_err);
1313 if (local_err != NULL) {
1314 error_propagate(errp, local_err);
1315 ret = -EINVAL;
1316 goto out;
1319 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1320 error_setg(errp, "iSCSI: Failed to set session type to normal.");
1321 ret = -EINVAL;
1322 goto out;
1325 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1327 /* check if we got HEADER_DIGEST via the options */
1328 parse_header_digest(iscsi, iscsi_url->target, &local_err);
1329 if (local_err != NULL) {
1330 error_propagate(errp, local_err);
1331 ret = -EINVAL;
1332 goto out;
1335 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1336 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
1337 iscsi_get_error(iscsi));
1338 ret = -EINVAL;
1339 goto out;
1342 iscsilun->iscsi = iscsi;
1343 iscsilun->aio_context = bdrv_get_aio_context(bs);
1344 iscsilun->lun = iscsi_url->lun;
1345 iscsilun->has_write_same = true;
1347 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1348 (void **) &inq, errp);
1349 if (task == NULL) {
1350 ret = -EINVAL;
1351 goto out;
1353 iscsilun->type = inq->periperal_device_type;
1354 scsi_free_scsi_task(task);
1355 task = NULL;
1357 iscsi_readcapacity_sync(iscsilun, &local_err);
1358 if (local_err != NULL) {
1359 error_propagate(errp, local_err);
1360 ret = -EINVAL;
1361 goto out;
1363 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1364 bs->request_alignment = iscsilun->block_size;
1366 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1367 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1368 * will try to read from the device to guess the image format.
1370 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
1371 bs->sg = 1;
1374 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1375 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1376 (void **) &inq_vpd, errp);
1377 if (task == NULL) {
1378 ret = -EINVAL;
1379 goto out;
1381 for (i = 0; i < inq_vpd->num_pages; i++) {
1382 struct scsi_task *inq_task;
1383 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1384 struct scsi_inquiry_block_limits *inq_bl;
1385 switch (inq_vpd->pages[i]) {
1386 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1387 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1388 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1389 (void **) &inq_lbp, errp);
1390 if (inq_task == NULL) {
1391 ret = -EINVAL;
1392 goto out;
1394 memcpy(&iscsilun->lbp, inq_lbp,
1395 sizeof(struct scsi_inquiry_logical_block_provisioning));
1396 scsi_free_scsi_task(inq_task);
1397 break;
1398 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1399 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1400 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1401 (void **) &inq_bl, errp);
1402 if (inq_task == NULL) {
1403 ret = -EINVAL;
1404 goto out;
1406 memcpy(&iscsilun->bl, inq_bl,
1407 sizeof(struct scsi_inquiry_block_limits));
1408 scsi_free_scsi_task(inq_task);
1409 break;
1410 default:
1411 break;
1414 scsi_free_scsi_task(task);
1415 task = NULL;
1417 iscsi_attach_aio_context(bs, iscsilun->aio_context);
1419 /* Guess the internal cluster (page) size of the iscsi target by the means
1420 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1421 * reasonable size */
1422 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
1423 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1424 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
1425 iscsilun->block_size) >> BDRV_SECTOR_BITS;
1426 #if defined(LIBISCSI_FEATURE_IOVECTOR)
1427 if (iscsilun->lbprz && !(bs->open_flags & BDRV_O_NOCACHE)) {
1428 iscsilun->allocationmap =
1429 bitmap_new(DIV_ROUND_UP(bs->total_sectors,
1430 iscsilun->cluster_sectors));
1432 #endif
1435 out:
1436 qemu_opts_del(opts);
1437 g_free(initiator_name);
1438 if (iscsi_url != NULL) {
1439 iscsi_destroy_url(iscsi_url);
1441 if (task != NULL) {
1442 scsi_free_scsi_task(task);
1445 if (ret) {
1446 if (iscsi != NULL) {
1447 iscsi_destroy_context(iscsi);
1449 memset(iscsilun, 0, sizeof(IscsiLun));
1451 return ret;
1454 static void iscsi_close(BlockDriverState *bs)
1456 IscsiLun *iscsilun = bs->opaque;
1457 struct iscsi_context *iscsi = iscsilun->iscsi;
1459 iscsi_detach_aio_context(bs);
1460 iscsi_destroy_context(iscsi);
1461 g_free(iscsilun->zeroblock);
1462 g_free(iscsilun->allocationmap);
1463 memset(iscsilun, 0, sizeof(IscsiLun));
1466 static int iscsi_refresh_limits(BlockDriverState *bs)
1468 IscsiLun *iscsilun = bs->opaque;
1470 /* We don't actually refresh here, but just return data queried in
1471 * iscsi_open(): iscsi targets don't change their limits. */
1472 if (iscsilun->lbp.lbpu) {
1473 if (iscsilun->bl.max_unmap < 0xffffffff) {
1474 bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap,
1475 iscsilun);
1477 bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1478 iscsilun);
1481 if (iscsilun->bl.max_ws_len < 0xffffffff) {
1482 bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len,
1483 iscsilun);
1485 if (iscsilun->lbp.lbpws) {
1486 bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1487 iscsilun);
1489 bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len,
1490 iscsilun);
1491 return 0;
1494 /* Since iscsi_open() ignores bdrv_flags, there is nothing to do here in
1495 * prepare. Note that this will not re-establish a connection with an iSCSI
1496 * target - it is effectively a NOP. */
1497 static int iscsi_reopen_prepare(BDRVReopenState *state,
1498 BlockReopenQueue *queue, Error **errp)
1500 /* NOP */
1501 return 0;
1504 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1506 IscsiLun *iscsilun = bs->opaque;
1507 Error *local_err = NULL;
1509 if (iscsilun->type != TYPE_DISK) {
1510 return -ENOTSUP;
1513 iscsi_readcapacity_sync(iscsilun, &local_err);
1514 if (local_err != NULL) {
1515 error_free(local_err);
1516 return -EIO;
1519 if (offset > iscsi_getlength(bs)) {
1520 return -EINVAL;
1523 if (iscsilun->allocationmap != NULL) {
1524 g_free(iscsilun->allocationmap);
1525 iscsilun->allocationmap =
1526 bitmap_new(DIV_ROUND_UP(bs->total_sectors,
1527 iscsilun->cluster_sectors));
1530 return 0;
1533 static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp)
1535 int ret = 0;
1536 int64_t total_size = 0;
1537 BlockDriverState *bs;
1538 IscsiLun *iscsilun = NULL;
1539 QDict *bs_options;
1541 bs = bdrv_new("", &error_abort);
1543 /* Read out options */
1544 total_size =
1545 qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE;
1546 bs->opaque = g_malloc0(sizeof(struct IscsiLun));
1547 iscsilun = bs->opaque;
1549 bs_options = qdict_new();
1550 qdict_put(bs_options, "filename", qstring_from_str(filename));
1551 ret = iscsi_open(bs, bs_options, 0, NULL);
1552 QDECREF(bs_options);
1554 if (ret != 0) {
1555 goto out;
1557 iscsi_detach_aio_context(bs);
1558 if (iscsilun->type != TYPE_DISK) {
1559 ret = -ENODEV;
1560 goto out;
1562 if (bs->total_sectors < total_size) {
1563 ret = -ENOSPC;
1564 goto out;
1567 ret = 0;
1568 out:
1569 if (iscsilun->iscsi != NULL) {
1570 iscsi_destroy_context(iscsilun->iscsi);
1572 g_free(bs->opaque);
1573 bs->opaque = NULL;
1574 bdrv_unref(bs);
1575 return ret;
1578 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1580 IscsiLun *iscsilun = bs->opaque;
1581 bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz;
1582 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
1583 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
1584 return 0;
1587 static QemuOptsList iscsi_create_opts = {
1588 .name = "iscsi-create-opts",
1589 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
1590 .desc = {
1592 .name = BLOCK_OPT_SIZE,
1593 .type = QEMU_OPT_SIZE,
1594 .help = "Virtual disk size"
1596 { /* end of list */ }
1600 static BlockDriver bdrv_iscsi = {
1601 .format_name = "iscsi",
1602 .protocol_name = "iscsi",
1604 .instance_size = sizeof(IscsiLun),
1605 .bdrv_needs_filename = true,
1606 .bdrv_file_open = iscsi_open,
1607 .bdrv_close = iscsi_close,
1608 .bdrv_create = iscsi_create,
1609 .create_opts = &iscsi_create_opts,
1610 .bdrv_reopen_prepare = iscsi_reopen_prepare,
1612 .bdrv_getlength = iscsi_getlength,
1613 .bdrv_get_info = iscsi_get_info,
1614 .bdrv_truncate = iscsi_truncate,
1615 .bdrv_refresh_limits = iscsi_refresh_limits,
1617 #if defined(LIBISCSI_FEATURE_IOVECTOR)
1618 .bdrv_co_get_block_status = iscsi_co_get_block_status,
1619 #endif
1620 .bdrv_co_discard = iscsi_co_discard,
1621 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
1622 .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
1623 #endif
1624 .bdrv_co_readv = iscsi_co_readv,
1625 .bdrv_co_writev = iscsi_co_writev,
1626 .bdrv_co_flush_to_disk = iscsi_co_flush,
1628 #ifdef __linux__
1629 .bdrv_ioctl = iscsi_ioctl,
1630 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1631 #endif
1633 .bdrv_detach_aio_context = iscsi_detach_aio_context,
1634 .bdrv_attach_aio_context = iscsi_attach_aio_context,
1637 static QemuOptsList qemu_iscsi_opts = {
1638 .name = "iscsi",
1639 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1640 .desc = {
1642 .name = "user",
1643 .type = QEMU_OPT_STRING,
1644 .help = "username for CHAP authentication to target",
1646 .name = "password",
1647 .type = QEMU_OPT_STRING,
1648 .help = "password for CHAP authentication to target",
1650 .name = "header-digest",
1651 .type = QEMU_OPT_STRING,
1652 .help = "HeaderDigest setting. "
1653 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1655 .name = "initiator-name",
1656 .type = QEMU_OPT_STRING,
1657 .help = "Initiator iqn name to use when connecting",
1659 { /* end of list */ }
1663 static void iscsi_block_init(void)
1665 bdrv_register(&bdrv_iscsi);
1666 qemu_add_opts(&qemu_iscsi_opts);
1669 block_init(iscsi_block_init);