block/iscsi: bump libiscsi requirement to 1.9.0
[qemu/kevin.git] / block / iscsi.c
blob83c7992fc797e48820c3f8950b9ad5344d186a8c
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 <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 "trace.h"
38 #include "block/scsi.h"
39 #include "qemu/iov.h"
40 #include "sysemu/sysemu.h"
41 #include "qmp-commands.h"
43 #include <iscsi/iscsi.h>
44 #include <iscsi/scsi-lowlevel.h>
46 #ifdef __linux__
47 #include <scsi/sg.h>
48 #include <block/scsi.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 uint8_t lbpme;
61 uint8_t lbprz;
62 uint8_t has_write_same;
63 struct scsi_inquiry_logical_block_provisioning lbp;
64 struct scsi_inquiry_block_limits bl;
65 unsigned char *zeroblock;
66 unsigned long *allocationmap;
67 int cluster_sectors;
68 bool use_16_for_rw;
69 } IscsiLun;
71 typedef struct IscsiTask {
72 int status;
73 int complete;
74 int retries;
75 int do_retry;
76 struct scsi_task *task;
77 Coroutine *co;
78 QEMUBH *bh;
79 IscsiLun *iscsilun;
80 QEMUTimer retry_timer;
81 } IscsiTask;
83 typedef struct IscsiAIOCB {
84 BlockDriverAIOCB common;
85 QEMUIOVector *qiov;
86 QEMUBH *bh;
87 IscsiLun *iscsilun;
88 struct scsi_task *task;
89 uint8_t *buf;
90 int status;
91 int canceled;
92 int64_t sector_num;
93 int nb_sectors;
94 #ifdef __linux__
95 sg_io_hdr_t *ioh;
96 #endif
97 } IscsiAIOCB;
99 #define NOP_INTERVAL 5000
100 #define MAX_NOP_FAILURES 3
101 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
102 static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048};
104 /* this threshhold is a trade-off knob to choose between
105 * the potential additional overhead of an extra GET_LBA_STATUS request
106 * vs. unnecessarily reading a lot of zero sectors over the wire.
107 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
108 * sectors we check the allocation status of the area covered by the
109 * request first if the allocationmap indicates that the area might be
110 * unallocated. */
111 #define ISCSI_CHECKALLOC_THRES 64
113 static void
114 iscsi_bh_cb(void *p)
116 IscsiAIOCB *acb = p;
118 qemu_bh_delete(acb->bh);
120 g_free(acb->buf);
121 acb->buf = NULL;
123 if (acb->canceled == 0) {
124 acb->common.cb(acb->common.opaque, acb->status);
127 if (acb->task != NULL) {
128 scsi_free_scsi_task(acb->task);
129 acb->task = NULL;
132 qemu_aio_release(acb);
135 static void
136 iscsi_schedule_bh(IscsiAIOCB *acb)
138 if (acb->bh) {
139 return;
141 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
142 qemu_bh_schedule(acb->bh);
145 static void iscsi_co_generic_bh_cb(void *opaque)
147 struct IscsiTask *iTask = opaque;
148 iTask->complete = 1;
149 qemu_bh_delete(iTask->bh);
150 qemu_coroutine_enter(iTask->co, NULL);
153 static void iscsi_retry_timer_expired(void *opaque)
155 struct IscsiTask *iTask = opaque;
156 iTask->complete = 1;
157 if (iTask->co) {
158 qemu_coroutine_enter(iTask->co, NULL);
162 static inline unsigned exp_random(double mean)
164 return -mean * log((double)rand() / RAND_MAX);
167 static void
168 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
169 void *command_data, void *opaque)
171 struct IscsiTask *iTask = opaque;
172 struct scsi_task *task = command_data;
174 iTask->status = status;
175 iTask->do_retry = 0;
176 iTask->task = task;
178 if (status != SCSI_STATUS_GOOD) {
179 if (iTask->retries++ < ISCSI_CMD_RETRIES) {
180 if (status == SCSI_STATUS_CHECK_CONDITION
181 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
182 error_report("iSCSI CheckCondition: %s",
183 iscsi_get_error(iscsi));
184 iTask->do_retry = 1;
185 goto out;
187 if (status == SCSI_STATUS_BUSY) {
188 unsigned retry_time =
189 exp_random(iscsi_retry_times[iTask->retries - 1]);
190 error_report("iSCSI Busy (retry #%u in %u ms): %s",
191 iTask->retries, retry_time,
192 iscsi_get_error(iscsi));
193 aio_timer_init(iTask->iscsilun->aio_context,
194 &iTask->retry_timer, QEMU_CLOCK_REALTIME,
195 SCALE_MS, iscsi_retry_timer_expired, iTask);
196 timer_mod(&iTask->retry_timer,
197 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time);
198 iTask->do_retry = 1;
199 return;
202 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi));
205 out:
206 if (iTask->co) {
207 iTask->bh = aio_bh_new(iTask->iscsilun->aio_context,
208 iscsi_co_generic_bh_cb, iTask);
209 qemu_bh_schedule(iTask->bh);
210 } else {
211 iTask->complete = 1;
215 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
217 *iTask = (struct IscsiTask) {
218 .co = qemu_coroutine_self(),
219 .retries = ISCSI_CMD_RETRIES,
220 .iscsilun = iscsilun,
224 static void
225 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
226 void *private_data)
228 IscsiAIOCB *acb = private_data;
230 acb->status = -ECANCELED;
231 iscsi_schedule_bh(acb);
234 static void
235 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
237 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
238 IscsiLun *iscsilun = acb->iscsilun;
240 if (acb->status != -EINPROGRESS) {
241 return;
244 acb->canceled = 1;
246 /* send a task mgmt call to the target to cancel the task on the target */
247 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
248 iscsi_abort_task_cb, acb);
250 while (acb->status == -EINPROGRESS) {
251 aio_poll(iscsilun->aio_context, true);
255 static const AIOCBInfo iscsi_aiocb_info = {
256 .aiocb_size = sizeof(IscsiAIOCB),
257 .cancel = iscsi_aio_cancel,
261 static void iscsi_process_read(void *arg);
262 static void iscsi_process_write(void *arg);
264 static void
265 iscsi_set_events(IscsiLun *iscsilun)
267 struct iscsi_context *iscsi = iscsilun->iscsi;
268 int ev;
270 /* We always register a read handler. */
271 ev = POLLIN;
272 ev |= iscsi_which_events(iscsi);
273 if (ev != iscsilun->events) {
274 aio_set_fd_handler(iscsilun->aio_context,
275 iscsi_get_fd(iscsi),
276 iscsi_process_read,
277 (ev & POLLOUT) ? iscsi_process_write : NULL,
278 iscsilun);
282 iscsilun->events = ev;
285 static void
286 iscsi_process_read(void *arg)
288 IscsiLun *iscsilun = arg;
289 struct iscsi_context *iscsi = iscsilun->iscsi;
291 iscsi_service(iscsi, POLLIN);
292 iscsi_set_events(iscsilun);
295 static void
296 iscsi_process_write(void *arg)
298 IscsiLun *iscsilun = arg;
299 struct iscsi_context *iscsi = iscsilun->iscsi;
301 iscsi_service(iscsi, POLLOUT);
302 iscsi_set_events(iscsilun);
305 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
307 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
310 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
312 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
315 static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
316 IscsiLun *iscsilun)
318 if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
319 (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
320 error_report("iSCSI misaligned request: "
321 "iscsilun->block_size %u, sector_num %" PRIi64
322 ", nb_sectors %d",
323 iscsilun->block_size, sector_num, nb_sectors);
324 return 0;
326 return 1;
329 static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num,
330 int nb_sectors)
332 if (iscsilun->allocationmap == NULL) {
333 return;
335 bitmap_set(iscsilun->allocationmap,
336 sector_num / iscsilun->cluster_sectors,
337 DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors));
340 static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num,
341 int nb_sectors)
343 int64_t cluster_num, nb_clusters;
344 if (iscsilun->allocationmap == NULL) {
345 return;
347 cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors);
348 nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors
349 - cluster_num;
350 if (nb_clusters > 0) {
351 bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters);
355 static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,
356 int64_t sector_num, int nb_sectors,
357 QEMUIOVector *iov)
359 IscsiLun *iscsilun = bs->opaque;
360 struct IscsiTask iTask;
361 uint64_t lba;
362 uint32_t num_sectors;
363 uint8_t *data = NULL;
364 uint8_t *buf = NULL;
366 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
367 return -EINVAL;
370 lba = sector_qemu2lun(sector_num, iscsilun);
371 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
372 iscsi_co_init_iscsitask(iscsilun, &iTask);
373 retry:
374 if (iscsilun->use_16_for_rw) {
375 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
376 data, num_sectors * iscsilun->block_size,
377 iscsilun->block_size, 0, 0, 0, 0, 0,
378 iscsi_co_generic_cb, &iTask);
379 } else {
380 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,
381 data, num_sectors * iscsilun->block_size,
382 iscsilun->block_size, 0, 0, 0, 0, 0,
383 iscsi_co_generic_cb, &iTask);
385 if (iTask.task == NULL) {
386 g_free(buf);
387 return -ENOMEM;
389 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
390 iov->niov);
391 while (!iTask.complete) {
392 iscsi_set_events(iscsilun);
393 qemu_coroutine_yield();
396 if (iTask.task != NULL) {
397 scsi_free_scsi_task(iTask.task);
398 iTask.task = NULL;
401 if (iTask.do_retry) {
402 iTask.complete = 0;
403 goto retry;
406 g_free(buf);
408 if (iTask.status != SCSI_STATUS_GOOD) {
409 return -EIO;
412 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
414 return 0;
418 static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun,
419 int64_t sector_num, int nb_sectors)
421 unsigned long size;
422 if (iscsilun->allocationmap == NULL) {
423 return true;
425 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
426 return !(find_next_bit(iscsilun->allocationmap, size,
427 sector_num / iscsilun->cluster_sectors) == size);
430 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
431 int64_t sector_num,
432 int nb_sectors, int *pnum)
434 IscsiLun *iscsilun = bs->opaque;
435 struct scsi_get_lba_status *lbas = NULL;
436 struct scsi_lba_status_descriptor *lbasd = NULL;
437 struct IscsiTask iTask;
438 int64_t ret;
440 iscsi_co_init_iscsitask(iscsilun, &iTask);
442 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
443 ret = -EINVAL;
444 goto out;
447 /* default to all sectors allocated */
448 ret = BDRV_BLOCK_DATA;
449 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
450 *pnum = nb_sectors;
452 /* LUN does not support logical block provisioning */
453 if (iscsilun->lbpme == 0) {
454 goto out;
457 retry:
458 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
459 sector_qemu2lun(sector_num, iscsilun),
460 8 + 16, iscsi_co_generic_cb,
461 &iTask) == NULL) {
462 ret = -ENOMEM;
463 goto out;
466 while (!iTask.complete) {
467 iscsi_set_events(iscsilun);
468 qemu_coroutine_yield();
471 if (iTask.do_retry) {
472 if (iTask.task != NULL) {
473 scsi_free_scsi_task(iTask.task);
474 iTask.task = NULL;
476 iTask.complete = 0;
477 goto retry;
480 if (iTask.status != SCSI_STATUS_GOOD) {
481 /* in case the get_lba_status_callout fails (i.e.
482 * because the device is busy or the cmd is not
483 * supported) we pretend all blocks are allocated
484 * for backwards compatibility */
485 goto out;
488 lbas = scsi_datain_unmarshall(iTask.task);
489 if (lbas == NULL) {
490 ret = -EIO;
491 goto out;
494 lbasd = &lbas->descriptors[0];
496 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
497 ret = -EIO;
498 goto out;
501 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
503 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
504 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
505 ret &= ~BDRV_BLOCK_DATA;
506 if (iscsilun->lbprz) {
507 ret |= BDRV_BLOCK_ZERO;
511 if (ret & BDRV_BLOCK_ZERO) {
512 iscsi_allocationmap_clear(iscsilun, sector_num, *pnum);
513 } else {
514 iscsi_allocationmap_set(iscsilun, sector_num, *pnum);
517 if (*pnum > nb_sectors) {
518 *pnum = nb_sectors;
520 out:
521 if (iTask.task != NULL) {
522 scsi_free_scsi_task(iTask.task);
524 return ret;
527 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
528 int64_t sector_num, int nb_sectors,
529 QEMUIOVector *iov)
531 IscsiLun *iscsilun = bs->opaque;
532 struct IscsiTask iTask;
533 uint64_t lba;
534 uint32_t num_sectors;
536 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
537 return -EINVAL;
540 if (iscsilun->lbprz && nb_sectors >= ISCSI_CHECKALLOC_THRES &&
541 !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
542 int64_t ret;
543 int pnum;
544 ret = iscsi_co_get_block_status(bs, sector_num, INT_MAX, &pnum);
545 if (ret < 0) {
546 return ret;
548 if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) {
549 qemu_iovec_memset(iov, 0, 0x00, iov->size);
550 return 0;
554 lba = sector_qemu2lun(sector_num, iscsilun);
555 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
557 iscsi_co_init_iscsitask(iscsilun, &iTask);
558 retry:
559 if (iscsilun->use_16_for_rw) {
560 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
561 num_sectors * iscsilun->block_size,
562 iscsilun->block_size, 0, 0, 0, 0, 0,
563 iscsi_co_generic_cb, &iTask);
564 } else {
565 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
566 num_sectors * iscsilun->block_size,
567 iscsilun->block_size,
568 0, 0, 0, 0, 0,
569 iscsi_co_generic_cb, &iTask);
571 if (iTask.task == NULL) {
572 return -ENOMEM;
574 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
576 while (!iTask.complete) {
577 iscsi_set_events(iscsilun);
578 qemu_coroutine_yield();
581 if (iTask.task != NULL) {
582 scsi_free_scsi_task(iTask.task);
583 iTask.task = NULL;
586 if (iTask.do_retry) {
587 iTask.complete = 0;
588 goto retry;
591 if (iTask.status != SCSI_STATUS_GOOD) {
592 return -EIO;
595 return 0;
598 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
600 IscsiLun *iscsilun = bs->opaque;
601 struct IscsiTask iTask;
603 if (bs->sg) {
604 return 0;
607 iscsi_co_init_iscsitask(iscsilun, &iTask);
609 retry:
610 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
611 0, iscsi_co_generic_cb, &iTask) == NULL) {
612 return -ENOMEM;
615 while (!iTask.complete) {
616 iscsi_set_events(iscsilun);
617 qemu_coroutine_yield();
620 if (iTask.task != NULL) {
621 scsi_free_scsi_task(iTask.task);
622 iTask.task = NULL;
625 if (iTask.do_retry) {
626 iTask.complete = 0;
627 goto retry;
630 if (iTask.status != SCSI_STATUS_GOOD) {
631 return -EIO;
634 return 0;
637 #ifdef __linux__
638 static void
639 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
640 void *command_data, void *opaque)
642 IscsiAIOCB *acb = opaque;
644 g_free(acb->buf);
645 acb->buf = NULL;
647 if (acb->canceled != 0) {
648 return;
651 acb->status = 0;
652 if (status < 0) {
653 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
654 iscsi_get_error(iscsi));
655 acb->status = -EIO;
658 acb->ioh->driver_status = 0;
659 acb->ioh->host_status = 0;
660 acb->ioh->resid = 0;
662 #define SG_ERR_DRIVER_SENSE 0x08
664 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
665 int ss;
667 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
669 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
670 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
671 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
672 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
675 iscsi_schedule_bh(acb);
678 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
679 unsigned long int req, void *buf,
680 BlockDriverCompletionFunc *cb, void *opaque)
682 IscsiLun *iscsilun = bs->opaque;
683 struct iscsi_context *iscsi = iscsilun->iscsi;
684 struct iscsi_data data;
685 IscsiAIOCB *acb;
687 assert(req == SG_IO);
689 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
691 acb->iscsilun = iscsilun;
692 acb->canceled = 0;
693 acb->bh = NULL;
694 acb->status = -EINPROGRESS;
695 acb->buf = NULL;
696 acb->ioh = buf;
698 acb->task = malloc(sizeof(struct scsi_task));
699 if (acb->task == NULL) {
700 error_report("iSCSI: Failed to allocate task for scsi command. %s",
701 iscsi_get_error(iscsi));
702 qemu_aio_release(acb);
703 return NULL;
705 memset(acb->task, 0, sizeof(struct scsi_task));
707 switch (acb->ioh->dxfer_direction) {
708 case SG_DXFER_TO_DEV:
709 acb->task->xfer_dir = SCSI_XFER_WRITE;
710 break;
711 case SG_DXFER_FROM_DEV:
712 acb->task->xfer_dir = SCSI_XFER_READ;
713 break;
714 default:
715 acb->task->xfer_dir = SCSI_XFER_NONE;
716 break;
719 acb->task->cdb_size = acb->ioh->cmd_len;
720 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
721 acb->task->expxferlen = acb->ioh->dxfer_len;
723 data.size = 0;
724 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
725 if (acb->ioh->iovec_count == 0) {
726 data.data = acb->ioh->dxferp;
727 data.size = acb->ioh->dxfer_len;
728 } else {
729 scsi_task_set_iov_out(acb->task,
730 (struct scsi_iovec *) acb->ioh->dxferp,
731 acb->ioh->iovec_count);
735 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
736 iscsi_aio_ioctl_cb,
737 (data.size > 0) ? &data : NULL,
738 acb) != 0) {
739 scsi_free_scsi_task(acb->task);
740 qemu_aio_release(acb);
741 return NULL;
744 /* tell libiscsi to read straight into the buffer we got from ioctl */
745 if (acb->task->xfer_dir == SCSI_XFER_READ) {
746 if (acb->ioh->iovec_count == 0) {
747 scsi_task_add_data_in_buffer(acb->task,
748 acb->ioh->dxfer_len,
749 acb->ioh->dxferp);
750 } else {
751 scsi_task_set_iov_in(acb->task,
752 (struct scsi_iovec *) acb->ioh->dxferp,
753 acb->ioh->iovec_count);
757 iscsi_set_events(iscsilun);
759 return &acb->common;
762 static void ioctl_cb(void *opaque, int status)
764 int *p_status = opaque;
765 *p_status = status;
768 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
770 IscsiLun *iscsilun = bs->opaque;
771 int status;
773 switch (req) {
774 case SG_GET_VERSION_NUM:
775 *(int *)buf = 30000;
776 break;
777 case SG_GET_SCSI_ID:
778 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
779 break;
780 case SG_IO:
781 status = -EINPROGRESS;
782 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
784 while (status == -EINPROGRESS) {
785 aio_poll(iscsilun->aio_context, true);
788 return 0;
789 default:
790 return -1;
792 return 0;
794 #endif
796 static int64_t
797 iscsi_getlength(BlockDriverState *bs)
799 IscsiLun *iscsilun = bs->opaque;
800 int64_t len;
802 len = iscsilun->num_blocks;
803 len *= iscsilun->block_size;
805 return len;
808 static int
809 coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
810 int nb_sectors)
812 IscsiLun *iscsilun = bs->opaque;
813 struct IscsiTask iTask;
814 struct unmap_list list;
816 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
817 return -EINVAL;
820 if (!iscsilun->lbp.lbpu) {
821 /* UNMAP is not supported by the target */
822 return 0;
825 list.lba = sector_qemu2lun(sector_num, iscsilun);
826 list.num = sector_qemu2lun(nb_sectors, iscsilun);
828 iscsi_co_init_iscsitask(iscsilun, &iTask);
829 retry:
830 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
831 iscsi_co_generic_cb, &iTask) == NULL) {
832 return -ENOMEM;
835 while (!iTask.complete) {
836 iscsi_set_events(iscsilun);
837 qemu_coroutine_yield();
840 if (iTask.task != NULL) {
841 scsi_free_scsi_task(iTask.task);
842 iTask.task = NULL;
845 if (iTask.do_retry) {
846 iTask.complete = 0;
847 goto retry;
850 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
851 /* the target might fail with a check condition if it
852 is not happy with the alignment of the UNMAP request
853 we silently fail in this case */
854 return 0;
857 if (iTask.status != SCSI_STATUS_GOOD) {
858 return -EIO;
861 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
863 return 0;
866 static int
867 coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
868 int nb_sectors, BdrvRequestFlags flags)
870 IscsiLun *iscsilun = bs->opaque;
871 struct IscsiTask iTask;
872 uint64_t lba;
873 uint32_t nb_blocks;
874 bool use_16_for_ws = iscsilun->use_16_for_rw;
876 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
877 return -EINVAL;
880 if (flags & BDRV_REQ_MAY_UNMAP) {
881 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
882 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
883 use_16_for_ws = true;
885 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
886 /* WRITESAME16 with UNMAP is not supported by the target,
887 * fall back and try WRITESAME10/16 without UNMAP */
888 flags &= ~BDRV_REQ_MAY_UNMAP;
889 use_16_for_ws = iscsilun->use_16_for_rw;
893 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
894 /* WRITESAME without UNMAP is not supported by the target */
895 return -ENOTSUP;
898 lba = sector_qemu2lun(sector_num, iscsilun);
899 nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
901 if (iscsilun->zeroblock == NULL) {
902 iscsilun->zeroblock = g_malloc0(iscsilun->block_size);
905 iscsi_co_init_iscsitask(iscsilun, &iTask);
906 retry:
907 if (use_16_for_ws) {
908 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
909 iscsilun->zeroblock, iscsilun->block_size,
910 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
911 0, 0, iscsi_co_generic_cb, &iTask);
912 } else {
913 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
914 iscsilun->zeroblock, iscsilun->block_size,
915 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
916 0, 0, iscsi_co_generic_cb, &iTask);
918 if (iTask.task == 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 static void parse_chap(struct iscsi_context *iscsi, const char *target,
961 Error **errp)
963 QemuOptsList *list;
964 QemuOpts *opts;
965 const char *user = NULL;
966 const char *password = NULL;
968 list = qemu_find_opts("iscsi");
969 if (!list) {
970 return;
973 opts = qemu_opts_find(list, target);
974 if (opts == NULL) {
975 opts = QTAILQ_FIRST(&list->head);
976 if (!opts) {
977 return;
981 user = qemu_opt_get(opts, "user");
982 if (!user) {
983 return;
986 password = qemu_opt_get(opts, "password");
987 if (!password) {
988 error_setg(errp, "CHAP username specified but no password was given");
989 return;
992 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
993 error_setg(errp, "Failed to set initiator username and password");
997 static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
998 Error **errp)
1000 QemuOptsList *list;
1001 QemuOpts *opts;
1002 const char *digest = NULL;
1004 list = qemu_find_opts("iscsi");
1005 if (!list) {
1006 return;
1009 opts = qemu_opts_find(list, target);
1010 if (opts == NULL) {
1011 opts = QTAILQ_FIRST(&list->head);
1012 if (!opts) {
1013 return;
1017 digest = qemu_opt_get(opts, "header-digest");
1018 if (!digest) {
1019 return;
1022 if (!strcmp(digest, "CRC32C")) {
1023 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1024 } else if (!strcmp(digest, "NONE")) {
1025 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1026 } else if (!strcmp(digest, "CRC32C-NONE")) {
1027 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1028 } else if (!strcmp(digest, "NONE-CRC32C")) {
1029 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1030 } else {
1031 error_setg(errp, "Invalid header-digest setting : %s", digest);
1035 static char *parse_initiator_name(const char *target)
1037 QemuOptsList *list;
1038 QemuOpts *opts;
1039 const char *name;
1040 char *iscsi_name;
1041 UuidInfo *uuid_info;
1043 list = qemu_find_opts("iscsi");
1044 if (list) {
1045 opts = qemu_opts_find(list, target);
1046 if (!opts) {
1047 opts = QTAILQ_FIRST(&list->head);
1049 if (opts) {
1050 name = qemu_opt_get(opts, "initiator-name");
1051 if (name) {
1052 return g_strdup(name);
1057 uuid_info = qmp_query_uuid(NULL);
1058 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1059 name = qemu_get_vm_name();
1060 } else {
1061 name = uuid_info->UUID;
1063 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1064 name ? ":" : "", name ? name : "");
1065 qapi_free_UuidInfo(uuid_info);
1066 return iscsi_name;
1069 static void iscsi_nop_timed_event(void *opaque)
1071 IscsiLun *iscsilun = opaque;
1073 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
1074 error_report("iSCSI: NOP timeout. Reconnecting...");
1075 iscsi_reconnect(iscsilun->iscsi);
1078 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1079 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1080 return;
1083 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1084 iscsi_set_events(iscsilun);
1087 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
1089 struct scsi_task *task = NULL;
1090 struct scsi_readcapacity10 *rc10 = NULL;
1091 struct scsi_readcapacity16 *rc16 = NULL;
1092 int retries = ISCSI_CMD_RETRIES;
1094 do {
1095 if (task != NULL) {
1096 scsi_free_scsi_task(task);
1097 task = NULL;
1100 switch (iscsilun->type) {
1101 case TYPE_DISK:
1102 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1103 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1104 rc16 = scsi_datain_unmarshall(task);
1105 if (rc16 == NULL) {
1106 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1107 } else {
1108 iscsilun->block_size = rc16->block_length;
1109 iscsilun->num_blocks = rc16->returned_lba + 1;
1110 iscsilun->lbpme = rc16->lbpme;
1111 iscsilun->lbprz = rc16->lbprz;
1112 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
1115 break;
1116 case TYPE_ROM:
1117 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1118 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1119 rc10 = scsi_datain_unmarshall(task);
1120 if (rc10 == NULL) {
1121 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1122 } else {
1123 iscsilun->block_size = rc10->block_size;
1124 if (rc10->lba == 0) {
1125 /* blank disk loaded */
1126 iscsilun->num_blocks = 0;
1127 } else {
1128 iscsilun->num_blocks = rc10->lba + 1;
1132 break;
1133 default:
1134 return;
1136 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1137 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1138 && retries-- > 0);
1140 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1141 error_setg(errp, "iSCSI: failed to send readcapacity10 command.");
1143 if (task) {
1144 scsi_free_scsi_task(task);
1148 /* TODO Convert to fine grained options */
1149 static QemuOptsList runtime_opts = {
1150 .name = "iscsi",
1151 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1152 .desc = {
1154 .name = "filename",
1155 .type = QEMU_OPT_STRING,
1156 .help = "URL to the iscsi image",
1158 { /* end of list */ }
1162 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
1163 int evpd, int pc, void **inq, Error **errp)
1165 int full_size;
1166 struct scsi_task *task = NULL;
1167 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1168 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1169 goto fail;
1171 full_size = scsi_datain_getfullsize(task);
1172 if (full_size > task->datain.size) {
1173 scsi_free_scsi_task(task);
1175 /* we need more data for the full list */
1176 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1177 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1178 goto fail;
1182 *inq = scsi_datain_unmarshall(task);
1183 if (*inq == NULL) {
1184 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1185 goto fail_with_err;
1188 return task;
1190 fail:
1191 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1192 iscsi_get_error(iscsi));
1193 fail_with_err:
1194 if (task != NULL) {
1195 scsi_free_scsi_task(task);
1197 return NULL;
1200 static void iscsi_detach_aio_context(BlockDriverState *bs)
1202 IscsiLun *iscsilun = bs->opaque;
1204 aio_set_fd_handler(iscsilun->aio_context,
1205 iscsi_get_fd(iscsilun->iscsi),
1206 NULL, NULL, NULL);
1207 iscsilun->events = 0;
1209 if (iscsilun->nop_timer) {
1210 timer_del(iscsilun->nop_timer);
1211 timer_free(iscsilun->nop_timer);
1212 iscsilun->nop_timer = NULL;
1216 static void iscsi_attach_aio_context(BlockDriverState *bs,
1217 AioContext *new_context)
1219 IscsiLun *iscsilun = bs->opaque;
1221 iscsilun->aio_context = new_context;
1222 iscsi_set_events(iscsilun);
1224 /* Set up a timer for sending out iSCSI NOPs */
1225 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1226 QEMU_CLOCK_REALTIME, SCALE_MS,
1227 iscsi_nop_timed_event, iscsilun);
1228 timer_mod(iscsilun->nop_timer,
1229 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1233 * We support iscsi url's on the form
1234 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1236 * Note: flags are currently not used by iscsi_open. If this function
1237 * is changed such that flags are used, please examine iscsi_reopen_prepare()
1238 * to see if needs to be changed as well.
1240 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1241 Error **errp)
1243 IscsiLun *iscsilun = bs->opaque;
1244 struct iscsi_context *iscsi = NULL;
1245 struct iscsi_url *iscsi_url = NULL;
1246 struct scsi_task *task = NULL;
1247 struct scsi_inquiry_standard *inq = NULL;
1248 struct scsi_inquiry_supported_pages *inq_vpd;
1249 char *initiator_name = NULL;
1250 QemuOpts *opts;
1251 Error *local_err = NULL;
1252 const char *filename;
1253 int i, ret;
1255 if ((BDRV_SECTOR_SIZE % 512) != 0) {
1256 error_setg(errp, "iSCSI: Invalid BDRV_SECTOR_SIZE. "
1257 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1258 "of 512", BDRV_SECTOR_SIZE);
1259 return -EINVAL;
1262 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1263 qemu_opts_absorb_qdict(opts, options, &local_err);
1264 if (local_err) {
1265 error_propagate(errp, local_err);
1266 ret = -EINVAL;
1267 goto out;
1270 filename = qemu_opt_get(opts, "filename");
1272 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1273 if (iscsi_url == NULL) {
1274 error_setg(errp, "Failed to parse URL : %s", filename);
1275 ret = -EINVAL;
1276 goto out;
1279 memset(iscsilun, 0, sizeof(IscsiLun));
1281 initiator_name = parse_initiator_name(iscsi_url->target);
1283 iscsi = iscsi_create_context(initiator_name);
1284 if (iscsi == NULL) {
1285 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
1286 ret = -ENOMEM;
1287 goto out;
1290 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1291 error_setg(errp, "iSCSI: Failed to set target name.");
1292 ret = -EINVAL;
1293 goto out;
1296 if (iscsi_url->user != NULL) {
1297 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1298 iscsi_url->passwd);
1299 if (ret != 0) {
1300 error_setg(errp, "Failed to set initiator username and password");
1301 ret = -EINVAL;
1302 goto out;
1306 /* check if we got CHAP username/password via the options */
1307 parse_chap(iscsi, iscsi_url->target, &local_err);
1308 if (local_err != NULL) {
1309 error_propagate(errp, local_err);
1310 ret = -EINVAL;
1311 goto out;
1314 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1315 error_setg(errp, "iSCSI: Failed to set session type to normal.");
1316 ret = -EINVAL;
1317 goto out;
1320 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1322 /* check if we got HEADER_DIGEST via the options */
1323 parse_header_digest(iscsi, iscsi_url->target, &local_err);
1324 if (local_err != NULL) {
1325 error_propagate(errp, local_err);
1326 ret = -EINVAL;
1327 goto out;
1330 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1331 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
1332 iscsi_get_error(iscsi));
1333 ret = -EINVAL;
1334 goto out;
1337 iscsilun->iscsi = iscsi;
1338 iscsilun->aio_context = bdrv_get_aio_context(bs);
1339 iscsilun->lun = iscsi_url->lun;
1340 iscsilun->has_write_same = true;
1342 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1343 (void **) &inq, errp);
1344 if (task == NULL) {
1345 ret = -EINVAL;
1346 goto out;
1348 iscsilun->type = inq->periperal_device_type;
1349 scsi_free_scsi_task(task);
1350 task = NULL;
1352 iscsi_readcapacity_sync(iscsilun, &local_err);
1353 if (local_err != NULL) {
1354 error_propagate(errp, local_err);
1355 ret = -EINVAL;
1356 goto out;
1358 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1359 bs->request_alignment = iscsilun->block_size;
1361 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1362 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1363 * will try to read from the device to guess the image format.
1365 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
1366 bs->sg = 1;
1369 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1370 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1371 (void **) &inq_vpd, errp);
1372 if (task == NULL) {
1373 ret = -EINVAL;
1374 goto out;
1376 for (i = 0; i < inq_vpd->num_pages; i++) {
1377 struct scsi_task *inq_task;
1378 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1379 struct scsi_inquiry_block_limits *inq_bl;
1380 switch (inq_vpd->pages[i]) {
1381 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1382 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1383 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1384 (void **) &inq_lbp, errp);
1385 if (inq_task == NULL) {
1386 ret = -EINVAL;
1387 goto out;
1389 memcpy(&iscsilun->lbp, inq_lbp,
1390 sizeof(struct scsi_inquiry_logical_block_provisioning));
1391 scsi_free_scsi_task(inq_task);
1392 break;
1393 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1394 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1395 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1396 (void **) &inq_bl, errp);
1397 if (inq_task == NULL) {
1398 ret = -EINVAL;
1399 goto out;
1401 memcpy(&iscsilun->bl, inq_bl,
1402 sizeof(struct scsi_inquiry_block_limits));
1403 scsi_free_scsi_task(inq_task);
1404 break;
1405 default:
1406 break;
1409 scsi_free_scsi_task(task);
1410 task = NULL;
1412 iscsi_attach_aio_context(bs, iscsilun->aio_context);
1414 /* Guess the internal cluster (page) size of the iscsi target by the means
1415 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1416 * reasonable size */
1417 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
1418 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1419 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
1420 iscsilun->block_size) >> BDRV_SECTOR_BITS;
1421 if (iscsilun->lbprz && !(bs->open_flags & BDRV_O_NOCACHE)) {
1422 iscsilun->allocationmap =
1423 bitmap_new(DIV_ROUND_UP(bs->total_sectors,
1424 iscsilun->cluster_sectors));
1428 out:
1429 qemu_opts_del(opts);
1430 g_free(initiator_name);
1431 if (iscsi_url != NULL) {
1432 iscsi_destroy_url(iscsi_url);
1434 if (task != NULL) {
1435 scsi_free_scsi_task(task);
1438 if (ret) {
1439 if (iscsi != NULL) {
1440 iscsi_destroy_context(iscsi);
1442 memset(iscsilun, 0, sizeof(IscsiLun));
1444 return ret;
1447 static void iscsi_close(BlockDriverState *bs)
1449 IscsiLun *iscsilun = bs->opaque;
1450 struct iscsi_context *iscsi = iscsilun->iscsi;
1452 iscsi_detach_aio_context(bs);
1453 iscsi_destroy_context(iscsi);
1454 g_free(iscsilun->zeroblock);
1455 g_free(iscsilun->allocationmap);
1456 memset(iscsilun, 0, sizeof(IscsiLun));
1459 static int iscsi_refresh_limits(BlockDriverState *bs)
1461 IscsiLun *iscsilun = bs->opaque;
1463 /* We don't actually refresh here, but just return data queried in
1464 * iscsi_open(): iscsi targets don't change their limits. */
1465 if (iscsilun->lbp.lbpu) {
1466 if (iscsilun->bl.max_unmap < 0xffffffff) {
1467 bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap,
1468 iscsilun);
1470 bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1471 iscsilun);
1474 if (iscsilun->bl.max_ws_len < 0xffffffff) {
1475 bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len,
1476 iscsilun);
1478 if (iscsilun->lbp.lbpws) {
1479 bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1480 iscsilun);
1482 bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len,
1483 iscsilun);
1484 return 0;
1487 /* Since iscsi_open() ignores bdrv_flags, there is nothing to do here in
1488 * prepare. Note that this will not re-establish a connection with an iSCSI
1489 * target - it is effectively a NOP. */
1490 static int iscsi_reopen_prepare(BDRVReopenState *state,
1491 BlockReopenQueue *queue, Error **errp)
1493 /* NOP */
1494 return 0;
1497 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1499 IscsiLun *iscsilun = bs->opaque;
1500 Error *local_err = NULL;
1502 if (iscsilun->type != TYPE_DISK) {
1503 return -ENOTSUP;
1506 iscsi_readcapacity_sync(iscsilun, &local_err);
1507 if (local_err != NULL) {
1508 error_free(local_err);
1509 return -EIO;
1512 if (offset > iscsi_getlength(bs)) {
1513 return -EINVAL;
1516 if (iscsilun->allocationmap != NULL) {
1517 g_free(iscsilun->allocationmap);
1518 iscsilun->allocationmap =
1519 bitmap_new(DIV_ROUND_UP(bs->total_sectors,
1520 iscsilun->cluster_sectors));
1523 return 0;
1526 static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp)
1528 int ret = 0;
1529 int64_t total_size = 0;
1530 BlockDriverState *bs;
1531 IscsiLun *iscsilun = NULL;
1532 QDict *bs_options;
1534 bs = bdrv_new("", &error_abort);
1536 /* Read out options */
1537 total_size =
1538 qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0) / BDRV_SECTOR_SIZE;
1539 bs->opaque = g_malloc0(sizeof(struct IscsiLun));
1540 iscsilun = bs->opaque;
1542 bs_options = qdict_new();
1543 qdict_put(bs_options, "filename", qstring_from_str(filename));
1544 ret = iscsi_open(bs, bs_options, 0, NULL);
1545 QDECREF(bs_options);
1547 if (ret != 0) {
1548 goto out;
1550 iscsi_detach_aio_context(bs);
1551 if (iscsilun->type != TYPE_DISK) {
1552 ret = -ENODEV;
1553 goto out;
1555 if (bs->total_sectors < total_size) {
1556 ret = -ENOSPC;
1557 goto out;
1560 ret = 0;
1561 out:
1562 if (iscsilun->iscsi != NULL) {
1563 iscsi_destroy_context(iscsilun->iscsi);
1565 g_free(bs->opaque);
1566 bs->opaque = NULL;
1567 bdrv_unref(bs);
1568 return ret;
1571 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1573 IscsiLun *iscsilun = bs->opaque;
1574 bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz;
1575 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
1576 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
1577 return 0;
1580 static QemuOptsList iscsi_create_opts = {
1581 .name = "iscsi-create-opts",
1582 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
1583 .desc = {
1585 .name = BLOCK_OPT_SIZE,
1586 .type = QEMU_OPT_SIZE,
1587 .help = "Virtual disk size"
1589 { /* end of list */ }
1593 static BlockDriver bdrv_iscsi = {
1594 .format_name = "iscsi",
1595 .protocol_name = "iscsi",
1597 .instance_size = sizeof(IscsiLun),
1598 .bdrv_needs_filename = true,
1599 .bdrv_file_open = iscsi_open,
1600 .bdrv_close = iscsi_close,
1601 .bdrv_create = iscsi_create,
1602 .create_opts = &iscsi_create_opts,
1603 .bdrv_reopen_prepare = iscsi_reopen_prepare,
1605 .bdrv_getlength = iscsi_getlength,
1606 .bdrv_get_info = iscsi_get_info,
1607 .bdrv_truncate = iscsi_truncate,
1608 .bdrv_refresh_limits = iscsi_refresh_limits,
1610 .bdrv_co_get_block_status = iscsi_co_get_block_status,
1611 .bdrv_co_discard = iscsi_co_discard,
1612 .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
1613 .bdrv_co_readv = iscsi_co_readv,
1614 .bdrv_co_writev = iscsi_co_writev,
1615 .bdrv_co_flush_to_disk = iscsi_co_flush,
1617 #ifdef __linux__
1618 .bdrv_ioctl = iscsi_ioctl,
1619 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1620 #endif
1622 .bdrv_detach_aio_context = iscsi_detach_aio_context,
1623 .bdrv_attach_aio_context = iscsi_attach_aio_context,
1626 static QemuOptsList qemu_iscsi_opts = {
1627 .name = "iscsi",
1628 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1629 .desc = {
1631 .name = "user",
1632 .type = QEMU_OPT_STRING,
1633 .help = "username for CHAP authentication to target",
1635 .name = "password",
1636 .type = QEMU_OPT_STRING,
1637 .help = "password for CHAP authentication to target",
1639 .name = "header-digest",
1640 .type = QEMU_OPT_STRING,
1641 .help = "HeaderDigest setting. "
1642 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1644 .name = "initiator-name",
1645 .type = QEMU_OPT_STRING,
1646 .help = "Initiator iqn name to use when connecting",
1648 { /* end of list */ }
1652 static void iscsi_block_init(void)
1654 bdrv_register(&bdrv_iscsi);
1655 qemu_add_opts(&qemu_iscsi_opts);
1658 block_init(iscsi_block_init);