megasas: remove buildtime strings
[qemu/ar7.git] / block / iscsi.c
blob1b4af77a7f6fd3d02928ca43205a4040fc083538
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 int lun;
53 enum scsi_inquiry_peripheral_device_type type;
54 int block_size;
55 uint64_t num_blocks;
56 int events;
57 QEMUTimer *nop_timer;
58 uint8_t lbpme;
59 uint8_t lbprz;
60 uint8_t has_write_same;
61 struct scsi_inquiry_logical_block_provisioning lbp;
62 struct scsi_inquiry_block_limits bl;
63 unsigned char *zeroblock;
64 unsigned long *allocationmap;
65 int cluster_sectors;
66 } IscsiLun;
68 typedef struct IscsiTask {
69 int status;
70 int complete;
71 int retries;
72 int do_retry;
73 struct scsi_task *task;
74 Coroutine *co;
75 QEMUBH *bh;
76 } IscsiTask;
78 typedef struct IscsiAIOCB {
79 BlockDriverAIOCB common;
80 QEMUIOVector *qiov;
81 QEMUBH *bh;
82 IscsiLun *iscsilun;
83 struct scsi_task *task;
84 uint8_t *buf;
85 int status;
86 int canceled;
87 int retries;
88 int64_t sector_num;
89 int nb_sectors;
90 #ifdef __linux__
91 sg_io_hdr_t *ioh;
92 #endif
93 } IscsiAIOCB;
95 #define NOP_INTERVAL 5000
96 #define MAX_NOP_FAILURES 3
97 #define ISCSI_CMD_RETRIES 5
99 /* this threshhold is a trade-off knob to choose between
100 * the potential additional overhead of an extra GET_LBA_STATUS request
101 * vs. unnecessarily reading a lot of zero sectors over the wire.
102 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
103 * sectors we check the allocation status of the area covered by the
104 * request first if the allocationmap indicates that the area might be
105 * unallocated. */
106 #define ISCSI_CHECKALLOC_THRES 64
108 static void
109 iscsi_bh_cb(void *p)
111 IscsiAIOCB *acb = p;
113 qemu_bh_delete(acb->bh);
115 g_free(acb->buf);
116 acb->buf = NULL;
118 if (acb->canceled == 0) {
119 acb->common.cb(acb->common.opaque, acb->status);
122 if (acb->task != NULL) {
123 scsi_free_scsi_task(acb->task);
124 acb->task = NULL;
127 qemu_aio_release(acb);
130 static void
131 iscsi_schedule_bh(IscsiAIOCB *acb)
133 if (acb->bh) {
134 return;
136 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
137 qemu_bh_schedule(acb->bh);
140 static void iscsi_co_generic_bh_cb(void *opaque)
142 struct IscsiTask *iTask = opaque;
143 qemu_bh_delete(iTask->bh);
144 qemu_coroutine_enter(iTask->co, NULL);
147 static void
148 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
149 void *command_data, void *opaque)
151 struct IscsiTask *iTask = opaque;
152 struct scsi_task *task = command_data;
154 iTask->complete = 1;
155 iTask->status = status;
156 iTask->do_retry = 0;
157 iTask->task = task;
159 if (iTask->retries-- > 0 && status == SCSI_STATUS_CHECK_CONDITION
160 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
161 error_report("iSCSI CheckCondition: %s", iscsi_get_error(iscsi));
162 iTask->do_retry = 1;
163 goto out;
166 if (status != SCSI_STATUS_GOOD) {
167 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi));
170 out:
171 if (iTask->co) {
172 iTask->bh = qemu_bh_new(iscsi_co_generic_bh_cb, iTask);
173 qemu_bh_schedule(iTask->bh);
177 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
179 *iTask = (struct IscsiTask) {
180 .co = qemu_coroutine_self(),
181 .retries = ISCSI_CMD_RETRIES,
185 static void
186 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
187 void *private_data)
189 IscsiAIOCB *acb = private_data;
191 acb->status = -ECANCELED;
192 iscsi_schedule_bh(acb);
195 static void
196 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
198 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
199 IscsiLun *iscsilun = acb->iscsilun;
201 if (acb->status != -EINPROGRESS) {
202 return;
205 acb->canceled = 1;
207 /* send a task mgmt call to the target to cancel the task on the target */
208 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
209 iscsi_abort_task_cb, acb);
211 while (acb->status == -EINPROGRESS) {
212 qemu_aio_wait();
216 static const AIOCBInfo iscsi_aiocb_info = {
217 .aiocb_size = sizeof(IscsiAIOCB),
218 .cancel = iscsi_aio_cancel,
222 static void iscsi_process_read(void *arg);
223 static void iscsi_process_write(void *arg);
225 static void
226 iscsi_set_events(IscsiLun *iscsilun)
228 struct iscsi_context *iscsi = iscsilun->iscsi;
229 int ev;
231 /* We always register a read handler. */
232 ev = POLLIN;
233 ev |= iscsi_which_events(iscsi);
234 if (ev != iscsilun->events) {
235 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
236 iscsi_process_read,
237 (ev & POLLOUT) ? iscsi_process_write : NULL,
238 iscsilun);
242 iscsilun->events = ev;
245 static void
246 iscsi_process_read(void *arg)
248 IscsiLun *iscsilun = arg;
249 struct iscsi_context *iscsi = iscsilun->iscsi;
251 iscsi_service(iscsi, POLLIN);
252 iscsi_set_events(iscsilun);
255 static void
256 iscsi_process_write(void *arg)
258 IscsiLun *iscsilun = arg;
259 struct iscsi_context *iscsi = iscsilun->iscsi;
261 iscsi_service(iscsi, POLLOUT);
262 iscsi_set_events(iscsilun);
265 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
267 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
270 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
272 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
275 static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
276 IscsiLun *iscsilun)
278 if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
279 (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
280 error_report("iSCSI misaligned request: "
281 "iscsilun->block_size %u, sector_num %" PRIi64
282 ", nb_sectors %d",
283 iscsilun->block_size, sector_num, nb_sectors);
284 return 0;
286 return 1;
289 static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num,
290 int nb_sectors)
292 if (iscsilun->allocationmap == NULL) {
293 return;
295 bitmap_set(iscsilun->allocationmap,
296 sector_num / iscsilun->cluster_sectors,
297 DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors));
300 static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num,
301 int nb_sectors)
303 int64_t cluster_num, nb_clusters;
304 if (iscsilun->allocationmap == NULL) {
305 return;
307 cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors);
308 nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors
309 - cluster_num;
310 if (nb_clusters > 0) {
311 bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters);
315 static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,
316 int64_t sector_num, int nb_sectors,
317 QEMUIOVector *iov)
319 IscsiLun *iscsilun = bs->opaque;
320 struct IscsiTask iTask;
321 uint64_t lba;
322 uint32_t num_sectors;
323 uint8_t *data = NULL;
324 uint8_t *buf = NULL;
326 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
327 return -EINVAL;
330 lba = sector_qemu2lun(sector_num, iscsilun);
331 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
332 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
333 /* if the iovec only contains one buffer we can pass it directly */
334 if (iov->niov == 1) {
335 data = iov->iov[0].iov_base;
336 } else {
337 size_t size = MIN(nb_sectors * BDRV_SECTOR_SIZE, iov->size);
338 buf = g_malloc(size);
339 qemu_iovec_to_buf(iov, 0, buf, size);
340 data = buf;
342 #endif
343 iscsi_co_init_iscsitask(iscsilun, &iTask);
344 retry:
345 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
346 data, num_sectors * iscsilun->block_size,
347 iscsilun->block_size, 0, 0, 0, 0, 0,
348 iscsi_co_generic_cb, &iTask);
349 if (iTask.task == NULL) {
350 g_free(buf);
351 return -ENOMEM;
353 #if defined(LIBISCSI_FEATURE_IOVECTOR)
354 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
355 iov->niov);
356 #endif
357 while (!iTask.complete) {
358 iscsi_set_events(iscsilun);
359 qemu_coroutine_yield();
362 if (iTask.task != NULL) {
363 scsi_free_scsi_task(iTask.task);
364 iTask.task = NULL;
367 if (iTask.do_retry) {
368 iTask.complete = 0;
369 goto retry;
372 g_free(buf);
374 if (iTask.status != SCSI_STATUS_GOOD) {
375 return -EIO;
378 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
380 return 0;
384 #if defined(LIBISCSI_FEATURE_IOVECTOR)
385 static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun,
386 int64_t sector_num, int nb_sectors)
388 unsigned long size;
389 if (iscsilun->allocationmap == NULL) {
390 return true;
392 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
393 return !(find_next_bit(iscsilun->allocationmap, size,
394 sector_num / iscsilun->cluster_sectors) == size);
397 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
398 int64_t sector_num,
399 int nb_sectors, int *pnum)
401 IscsiLun *iscsilun = bs->opaque;
402 struct scsi_get_lba_status *lbas = NULL;
403 struct scsi_lba_status_descriptor *lbasd = NULL;
404 struct IscsiTask iTask;
405 int64_t ret;
407 iscsi_co_init_iscsitask(iscsilun, &iTask);
409 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
410 ret = -EINVAL;
411 goto out;
414 /* default to all sectors allocated */
415 ret = BDRV_BLOCK_DATA;
416 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
417 *pnum = nb_sectors;
419 /* LUN does not support logical block provisioning */
420 if (iscsilun->lbpme == 0) {
421 goto out;
424 retry:
425 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
426 sector_qemu2lun(sector_num, iscsilun),
427 8 + 16, iscsi_co_generic_cb,
428 &iTask) == NULL) {
429 ret = -ENOMEM;
430 goto out;
433 while (!iTask.complete) {
434 iscsi_set_events(iscsilun);
435 qemu_coroutine_yield();
438 if (iTask.do_retry) {
439 if (iTask.task != NULL) {
440 scsi_free_scsi_task(iTask.task);
441 iTask.task = NULL;
443 iTask.complete = 0;
444 goto retry;
447 if (iTask.status != SCSI_STATUS_GOOD) {
448 /* in case the get_lba_status_callout fails (i.e.
449 * because the device is busy or the cmd is not
450 * supported) we pretend all blocks are allocated
451 * for backwards compatibility */
452 goto out;
455 lbas = scsi_datain_unmarshall(iTask.task);
456 if (lbas == NULL) {
457 ret = -EIO;
458 goto out;
461 lbasd = &lbas->descriptors[0];
463 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
464 ret = -EIO;
465 goto out;
468 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
470 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
471 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
472 ret &= ~BDRV_BLOCK_DATA;
473 if (iscsilun->lbprz) {
474 ret |= BDRV_BLOCK_ZERO;
478 if (ret & BDRV_BLOCK_ZERO) {
479 iscsi_allocationmap_clear(iscsilun, sector_num, *pnum);
480 } else {
481 iscsi_allocationmap_set(iscsilun, sector_num, *pnum);
484 if (*pnum > nb_sectors) {
485 *pnum = nb_sectors;
487 out:
488 if (iTask.task != NULL) {
489 scsi_free_scsi_task(iTask.task);
491 return ret;
494 #endif /* LIBISCSI_FEATURE_IOVECTOR */
497 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
498 int64_t sector_num, int nb_sectors,
499 QEMUIOVector *iov)
501 IscsiLun *iscsilun = bs->opaque;
502 struct IscsiTask iTask;
503 uint64_t lba;
504 uint32_t num_sectors;
505 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
506 int i;
507 #endif
509 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
510 return -EINVAL;
513 #if defined(LIBISCSI_FEATURE_IOVECTOR)
514 if (iscsilun->lbprz && nb_sectors >= ISCSI_CHECKALLOC_THRES &&
515 !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
516 int64_t ret;
517 int pnum;
518 ret = iscsi_co_get_block_status(bs, sector_num, INT_MAX, &pnum);
519 if (ret < 0) {
520 return ret;
522 if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) {
523 qemu_iovec_memset(iov, 0, 0x00, iov->size);
524 return 0;
527 #endif
529 lba = sector_qemu2lun(sector_num, iscsilun);
530 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
532 iscsi_co_init_iscsitask(iscsilun, &iTask);
533 retry:
534 switch (iscsilun->type) {
535 case TYPE_DISK:
536 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
537 num_sectors * iscsilun->block_size,
538 iscsilun->block_size, 0, 0, 0, 0, 0,
539 iscsi_co_generic_cb, &iTask);
540 break;
541 default:
542 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
543 num_sectors * iscsilun->block_size,
544 iscsilun->block_size,
545 #if !defined(CONFIG_LIBISCSI_1_4) /* API change from 1.4.0 to 1.5.0 */
546 0, 0, 0, 0, 0,
547 #endif
548 iscsi_co_generic_cb, &iTask);
549 break;
551 if (iTask.task == NULL) {
552 return -ENOMEM;
554 #if defined(LIBISCSI_FEATURE_IOVECTOR)
555 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
556 #else
557 for (i = 0; i < iov->niov; i++) {
558 scsi_task_add_data_in_buffer(iTask.task,
559 iov->iov[i].iov_len,
560 iov->iov[i].iov_base);
562 #endif
564 while (!iTask.complete) {
565 iscsi_set_events(iscsilun);
566 qemu_coroutine_yield();
569 if (iTask.task != NULL) {
570 scsi_free_scsi_task(iTask.task);
571 iTask.task = NULL;
574 if (iTask.do_retry) {
575 iTask.complete = 0;
576 goto retry;
579 if (iTask.status != SCSI_STATUS_GOOD) {
580 return -EIO;
583 return 0;
586 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
588 IscsiLun *iscsilun = bs->opaque;
589 struct IscsiTask iTask;
591 if (bs->sg) {
592 return 0;
595 iscsi_co_init_iscsitask(iscsilun, &iTask);
597 retry:
598 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
599 0, iscsi_co_generic_cb, &iTask) == NULL) {
600 return -ENOMEM;
603 while (!iTask.complete) {
604 iscsi_set_events(iscsilun);
605 qemu_coroutine_yield();
608 if (iTask.task != NULL) {
609 scsi_free_scsi_task(iTask.task);
610 iTask.task = NULL;
613 if (iTask.do_retry) {
614 iTask.complete = 0;
615 goto retry;
618 if (iTask.status != SCSI_STATUS_GOOD) {
619 return -EIO;
622 return 0;
625 #ifdef __linux__
626 static void
627 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
628 void *command_data, void *opaque)
630 IscsiAIOCB *acb = opaque;
632 g_free(acb->buf);
633 acb->buf = NULL;
635 if (acb->canceled != 0) {
636 return;
639 acb->status = 0;
640 if (status < 0) {
641 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
642 iscsi_get_error(iscsi));
643 acb->status = -EIO;
646 acb->ioh->driver_status = 0;
647 acb->ioh->host_status = 0;
648 acb->ioh->resid = 0;
650 #define SG_ERR_DRIVER_SENSE 0x08
652 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
653 int ss;
655 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
657 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
658 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
659 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
660 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
663 iscsi_schedule_bh(acb);
666 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
667 unsigned long int req, void *buf,
668 BlockDriverCompletionFunc *cb, void *opaque)
670 IscsiLun *iscsilun = bs->opaque;
671 struct iscsi_context *iscsi = iscsilun->iscsi;
672 struct iscsi_data data;
673 IscsiAIOCB *acb;
675 assert(req == SG_IO);
677 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
679 acb->iscsilun = iscsilun;
680 acb->canceled = 0;
681 acb->bh = NULL;
682 acb->status = -EINPROGRESS;
683 acb->buf = NULL;
684 acb->ioh = buf;
686 acb->task = malloc(sizeof(struct scsi_task));
687 if (acb->task == NULL) {
688 error_report("iSCSI: Failed to allocate task for scsi command. %s",
689 iscsi_get_error(iscsi));
690 qemu_aio_release(acb);
691 return NULL;
693 memset(acb->task, 0, sizeof(struct scsi_task));
695 switch (acb->ioh->dxfer_direction) {
696 case SG_DXFER_TO_DEV:
697 acb->task->xfer_dir = SCSI_XFER_WRITE;
698 break;
699 case SG_DXFER_FROM_DEV:
700 acb->task->xfer_dir = SCSI_XFER_READ;
701 break;
702 default:
703 acb->task->xfer_dir = SCSI_XFER_NONE;
704 break;
707 acb->task->cdb_size = acb->ioh->cmd_len;
708 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
709 acb->task->expxferlen = acb->ioh->dxfer_len;
711 data.size = 0;
712 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
713 if (acb->ioh->iovec_count == 0) {
714 data.data = acb->ioh->dxferp;
715 data.size = acb->ioh->dxfer_len;
716 } else {
717 #if defined(LIBISCSI_FEATURE_IOVECTOR)
718 scsi_task_set_iov_out(acb->task,
719 (struct scsi_iovec *) acb->ioh->dxferp,
720 acb->ioh->iovec_count);
721 #else
722 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
724 acb->buf = g_malloc(acb->ioh->dxfer_len);
725 data.data = acb->buf;
726 data.size = iov_to_buf(iov, acb->ioh->iovec_count, 0,
727 acb->buf, acb->ioh->dxfer_len);
728 #endif
732 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
733 iscsi_aio_ioctl_cb,
734 (data.size > 0) ? &data : NULL,
735 acb) != 0) {
736 scsi_free_scsi_task(acb->task);
737 qemu_aio_release(acb);
738 return NULL;
741 /* tell libiscsi to read straight into the buffer we got from ioctl */
742 if (acb->task->xfer_dir == SCSI_XFER_READ) {
743 if (acb->ioh->iovec_count == 0) {
744 scsi_task_add_data_in_buffer(acb->task,
745 acb->ioh->dxfer_len,
746 acb->ioh->dxferp);
747 } else {
748 #if defined(LIBISCSI_FEATURE_IOVECTOR)
749 scsi_task_set_iov_in(acb->task,
750 (struct scsi_iovec *) acb->ioh->dxferp,
751 acb->ioh->iovec_count);
752 #else
753 int i;
754 for (i = 0; i < acb->ioh->iovec_count; i++) {
755 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
757 scsi_task_add_data_in_buffer(acb->task,
758 iov[i].iov_len,
759 iov[i].iov_base);
761 #endif
765 iscsi_set_events(iscsilun);
767 return &acb->common;
771 static void ioctl_cb(void *opaque, int status)
773 int *p_status = opaque;
774 *p_status = status;
777 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
779 IscsiLun *iscsilun = bs->opaque;
780 int status;
782 switch (req) {
783 case SG_GET_VERSION_NUM:
784 *(int *)buf = 30000;
785 break;
786 case SG_GET_SCSI_ID:
787 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
788 break;
789 case SG_IO:
790 status = -EINPROGRESS;
791 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
793 while (status == -EINPROGRESS) {
794 qemu_aio_wait();
797 return 0;
798 default:
799 return -1;
801 return 0;
803 #endif
805 static int64_t
806 iscsi_getlength(BlockDriverState *bs)
808 IscsiLun *iscsilun = bs->opaque;
809 int64_t len;
811 len = iscsilun->num_blocks;
812 len *= iscsilun->block_size;
814 return len;
817 static int
818 coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
819 int nb_sectors)
821 IscsiLun *iscsilun = bs->opaque;
822 struct IscsiTask iTask;
823 struct unmap_list list;
825 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
826 return -EINVAL;
829 if (!iscsilun->lbp.lbpu) {
830 /* UNMAP is not supported by the target */
831 return 0;
834 list.lba = sector_qemu2lun(sector_num, iscsilun);
835 list.num = sector_qemu2lun(nb_sectors, iscsilun);
837 iscsi_co_init_iscsitask(iscsilun, &iTask);
838 retry:
839 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
840 iscsi_co_generic_cb, &iTask) == NULL) {
841 return -ENOMEM;
844 while (!iTask.complete) {
845 iscsi_set_events(iscsilun);
846 qemu_coroutine_yield();
849 if (iTask.task != NULL) {
850 scsi_free_scsi_task(iTask.task);
851 iTask.task = NULL;
854 if (iTask.do_retry) {
855 iTask.complete = 0;
856 goto retry;
859 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
860 /* the target might fail with a check condition if it
861 is not happy with the alignment of the UNMAP request
862 we silently fail in this case */
863 return 0;
866 if (iTask.status != SCSI_STATUS_GOOD) {
867 return -EIO;
870 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
872 return 0;
875 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
877 static int
878 coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
879 int nb_sectors, BdrvRequestFlags flags)
881 IscsiLun *iscsilun = bs->opaque;
882 struct IscsiTask iTask;
883 uint64_t lba;
884 uint32_t nb_blocks;
886 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
887 return -EINVAL;
890 if ((flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->lbp.lbpws) {
891 /* WRITE SAME with UNMAP is not supported by the target,
892 * fall back and try WRITE SAME without UNMAP */
893 flags &= ~BDRV_REQ_MAY_UNMAP;
896 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
897 /* WRITE SAME without UNMAP is not supported by the target */
898 return -ENOTSUP;
901 lba = sector_qemu2lun(sector_num, iscsilun);
902 nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
904 if (iscsilun->zeroblock == NULL) {
905 iscsilun->zeroblock = g_malloc0(iscsilun->block_size);
908 iscsi_co_init_iscsitask(iscsilun, &iTask);
909 retry:
910 if (iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
911 iscsilun->zeroblock, iscsilun->block_size,
912 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
913 0, 0, iscsi_co_generic_cb, &iTask) == NULL) {
914 return -ENOMEM;
917 while (!iTask.complete) {
918 iscsi_set_events(iscsilun);
919 qemu_coroutine_yield();
922 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
923 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
924 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
925 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
926 /* WRITE SAME is not supported by the target */
927 iscsilun->has_write_same = false;
928 scsi_free_scsi_task(iTask.task);
929 return -ENOTSUP;
932 if (iTask.task != NULL) {
933 scsi_free_scsi_task(iTask.task);
934 iTask.task = NULL;
937 if (iTask.do_retry) {
938 iTask.complete = 0;
939 goto retry;
942 if (iTask.status != SCSI_STATUS_GOOD) {
943 return -EIO;
946 if (flags & BDRV_REQ_MAY_UNMAP) {
947 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
948 } else {
949 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
952 return 0;
955 #endif /* SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED */
957 static void parse_chap(struct iscsi_context *iscsi, const char *target,
958 Error **errp)
960 QemuOptsList *list;
961 QemuOpts *opts;
962 const char *user = NULL;
963 const char *password = NULL;
965 list = qemu_find_opts("iscsi");
966 if (!list) {
967 return;
970 opts = qemu_opts_find(list, target);
971 if (opts == NULL) {
972 opts = QTAILQ_FIRST(&list->head);
973 if (!opts) {
974 return;
978 user = qemu_opt_get(opts, "user");
979 if (!user) {
980 return;
983 password = qemu_opt_get(opts, "password");
984 if (!password) {
985 error_setg(errp, "CHAP username specified but no password was given");
986 return;
989 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
990 error_setg(errp, "Failed to set initiator username and password");
994 static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
995 Error **errp)
997 QemuOptsList *list;
998 QemuOpts *opts;
999 const char *digest = NULL;
1001 list = qemu_find_opts("iscsi");
1002 if (!list) {
1003 return;
1006 opts = qemu_opts_find(list, target);
1007 if (opts == NULL) {
1008 opts = QTAILQ_FIRST(&list->head);
1009 if (!opts) {
1010 return;
1014 digest = qemu_opt_get(opts, "header-digest");
1015 if (!digest) {
1016 return;
1019 if (!strcmp(digest, "CRC32C")) {
1020 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1021 } else if (!strcmp(digest, "NONE")) {
1022 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1023 } else if (!strcmp(digest, "CRC32C-NONE")) {
1024 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1025 } else if (!strcmp(digest, "NONE-CRC32C")) {
1026 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1027 } else {
1028 error_setg(errp, "Invalid header-digest setting : %s", digest);
1032 static char *parse_initiator_name(const char *target)
1034 QemuOptsList *list;
1035 QemuOpts *opts;
1036 const char *name;
1037 char *iscsi_name;
1038 UuidInfo *uuid_info;
1040 list = qemu_find_opts("iscsi");
1041 if (list) {
1042 opts = qemu_opts_find(list, target);
1043 if (!opts) {
1044 opts = QTAILQ_FIRST(&list->head);
1046 if (opts) {
1047 name = qemu_opt_get(opts, "initiator-name");
1048 if (name) {
1049 return g_strdup(name);
1054 uuid_info = qmp_query_uuid(NULL);
1055 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1056 name = qemu_get_vm_name();
1057 } else {
1058 name = uuid_info->UUID;
1060 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1061 name ? ":" : "", name ? name : "");
1062 qapi_free_UuidInfo(uuid_info);
1063 return iscsi_name;
1066 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1067 static void iscsi_nop_timed_event(void *opaque)
1069 IscsiLun *iscsilun = opaque;
1071 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
1072 error_report("iSCSI: NOP timeout. Reconnecting...");
1073 iscsi_reconnect(iscsilun->iscsi);
1076 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1077 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1078 return;
1081 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1082 iscsi_set_events(iscsilun);
1084 #endif
1086 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
1088 struct scsi_task *task = NULL;
1089 struct scsi_readcapacity10 *rc10 = NULL;
1090 struct scsi_readcapacity16 *rc16 = NULL;
1091 int retries = ISCSI_CMD_RETRIES;
1093 do {
1094 if (task != NULL) {
1095 scsi_free_scsi_task(task);
1096 task = NULL;
1099 switch (iscsilun->type) {
1100 case TYPE_DISK:
1101 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1102 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1103 rc16 = scsi_datain_unmarshall(task);
1104 if (rc16 == NULL) {
1105 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1106 } else {
1107 iscsilun->block_size = rc16->block_length;
1108 iscsilun->num_blocks = rc16->returned_lba + 1;
1109 iscsilun->lbpme = rc16->lbpme;
1110 iscsilun->lbprz = rc16->lbprz;
1113 break;
1114 case TYPE_ROM:
1115 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1116 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1117 rc10 = scsi_datain_unmarshall(task);
1118 if (rc10 == NULL) {
1119 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1120 } else {
1121 iscsilun->block_size = rc10->block_size;
1122 if (rc10->lba == 0) {
1123 /* blank disk loaded */
1124 iscsilun->num_blocks = 0;
1125 } else {
1126 iscsilun->num_blocks = rc10->lba + 1;
1130 break;
1131 default:
1132 return;
1134 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1135 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1136 && retries-- > 0);
1138 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1139 error_setg(errp, "iSCSI: failed to send readcapacity10 command.");
1141 if (task) {
1142 scsi_free_scsi_task(task);
1146 /* TODO Convert to fine grained options */
1147 static QemuOptsList runtime_opts = {
1148 .name = "iscsi",
1149 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1150 .desc = {
1152 .name = "filename",
1153 .type = QEMU_OPT_STRING,
1154 .help = "URL to the iscsi image",
1156 { /* end of list */ }
1160 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
1161 int evpd, int pc, void **inq, Error **errp)
1163 int full_size;
1164 struct scsi_task *task = NULL;
1165 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1166 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1167 goto fail;
1169 full_size = scsi_datain_getfullsize(task);
1170 if (full_size > task->datain.size) {
1171 scsi_free_scsi_task(task);
1173 /* we need more data for the full list */
1174 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1175 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1176 goto fail;
1180 *inq = scsi_datain_unmarshall(task);
1181 if (*inq == NULL) {
1182 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1183 goto fail;
1186 return task;
1188 fail:
1189 if (!error_is_set(errp)) {
1190 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1191 iscsi_get_error(iscsi));
1193 if (task != NULL) {
1194 scsi_free_scsi_task(task);
1196 return NULL;
1200 * We support iscsi url's on the form
1201 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1203 * Note: flags are currently not used by iscsi_open. If this function
1204 * is changed such that flags are used, please examine iscsi_reopen_prepare()
1205 * to see if needs to be changed as well.
1207 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1208 Error **errp)
1210 IscsiLun *iscsilun = bs->opaque;
1211 struct iscsi_context *iscsi = NULL;
1212 struct iscsi_url *iscsi_url = NULL;
1213 struct scsi_task *task = NULL;
1214 struct scsi_inquiry_standard *inq = NULL;
1215 struct scsi_inquiry_supported_pages *inq_vpd;
1216 char *initiator_name = NULL;
1217 QemuOpts *opts;
1218 Error *local_err = NULL;
1219 const char *filename;
1220 int i, ret;
1222 if ((BDRV_SECTOR_SIZE % 512) != 0) {
1223 error_setg(errp, "iSCSI: Invalid BDRV_SECTOR_SIZE. "
1224 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1225 "of 512", BDRV_SECTOR_SIZE);
1226 return -EINVAL;
1229 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1230 qemu_opts_absorb_qdict(opts, options, &local_err);
1231 if (local_err) {
1232 error_propagate(errp, local_err);
1233 ret = -EINVAL;
1234 goto out;
1237 filename = qemu_opt_get(opts, "filename");
1239 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1240 if (iscsi_url == NULL) {
1241 error_setg(errp, "Failed to parse URL : %s", filename);
1242 ret = -EINVAL;
1243 goto out;
1246 memset(iscsilun, 0, sizeof(IscsiLun));
1248 initiator_name = parse_initiator_name(iscsi_url->target);
1250 iscsi = iscsi_create_context(initiator_name);
1251 if (iscsi == NULL) {
1252 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
1253 ret = -ENOMEM;
1254 goto out;
1257 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1258 error_setg(errp, "iSCSI: Failed to set target name.");
1259 ret = -EINVAL;
1260 goto out;
1263 if (iscsi_url->user != NULL) {
1264 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1265 iscsi_url->passwd);
1266 if (ret != 0) {
1267 error_setg(errp, "Failed to set initiator username and password");
1268 ret = -EINVAL;
1269 goto out;
1273 /* check if we got CHAP username/password via the options */
1274 parse_chap(iscsi, iscsi_url->target, &local_err);
1275 if (local_err != NULL) {
1276 error_propagate(errp, local_err);
1277 ret = -EINVAL;
1278 goto out;
1281 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1282 error_setg(errp, "iSCSI: Failed to set session type to normal.");
1283 ret = -EINVAL;
1284 goto out;
1287 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1289 /* check if we got HEADER_DIGEST via the options */
1290 parse_header_digest(iscsi, iscsi_url->target, &local_err);
1291 if (local_err != NULL) {
1292 error_propagate(errp, local_err);
1293 ret = -EINVAL;
1294 goto out;
1297 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1298 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
1299 iscsi_get_error(iscsi));
1300 ret = -EINVAL;
1301 goto out;
1304 iscsilun->iscsi = iscsi;
1305 iscsilun->lun = iscsi_url->lun;
1306 iscsilun->has_write_same = true;
1308 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1309 (void **) &inq, errp);
1310 if (task == NULL) {
1311 ret = -EINVAL;
1312 goto out;
1314 iscsilun->type = inq->periperal_device_type;
1315 scsi_free_scsi_task(task);
1316 task = NULL;
1318 iscsi_readcapacity_sync(iscsilun, &local_err);
1319 if (local_err != NULL) {
1320 error_propagate(errp, local_err);
1321 ret = -EINVAL;
1322 goto out;
1324 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1325 bs->request_alignment = iscsilun->block_size;
1327 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1328 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1329 * will try to read from the device to guess the image format.
1331 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
1332 bs->sg = 1;
1335 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1336 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1337 (void **) &inq_vpd, errp);
1338 if (task == NULL) {
1339 ret = -EINVAL;
1340 goto out;
1342 for (i = 0; i < inq_vpd->num_pages; i++) {
1343 struct scsi_task *inq_task;
1344 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1345 struct scsi_inquiry_block_limits *inq_bl;
1346 switch (inq_vpd->pages[i]) {
1347 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1348 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1349 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1350 (void **) &inq_lbp, errp);
1351 if (inq_task == NULL) {
1352 ret = -EINVAL;
1353 goto out;
1355 memcpy(&iscsilun->lbp, inq_lbp,
1356 sizeof(struct scsi_inquiry_logical_block_provisioning));
1357 scsi_free_scsi_task(inq_task);
1358 break;
1359 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1360 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1361 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1362 (void **) &inq_bl, errp);
1363 if (inq_task == NULL) {
1364 ret = -EINVAL;
1365 goto out;
1367 memcpy(&iscsilun->bl, inq_bl,
1368 sizeof(struct scsi_inquiry_block_limits));
1369 scsi_free_scsi_task(inq_task);
1370 break;
1371 default:
1372 break;
1375 scsi_free_scsi_task(task);
1376 task = NULL;
1378 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1379 /* Set up a timer for sending out iSCSI NOPs */
1380 iscsilun->nop_timer = timer_new_ms(QEMU_CLOCK_REALTIME, iscsi_nop_timed_event, iscsilun);
1381 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1382 #endif
1384 /* Guess the internal cluster (page) size of the iscsi target by the means
1385 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1386 * reasonable size */
1387 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
1388 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1389 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
1390 iscsilun->block_size) >> BDRV_SECTOR_BITS;
1391 #if defined(LIBISCSI_FEATURE_IOVECTOR)
1392 if (iscsilun->lbprz && !(bs->open_flags & BDRV_O_NOCACHE)) {
1393 iscsilun->allocationmap =
1394 bitmap_new(DIV_ROUND_UP(bs->total_sectors,
1395 iscsilun->cluster_sectors));
1397 #endif
1400 out:
1401 qemu_opts_del(opts);
1402 if (initiator_name != NULL) {
1403 g_free(initiator_name);
1405 if (iscsi_url != NULL) {
1406 iscsi_destroy_url(iscsi_url);
1408 if (task != NULL) {
1409 scsi_free_scsi_task(task);
1412 if (ret) {
1413 if (iscsi != NULL) {
1414 iscsi_destroy_context(iscsi);
1416 memset(iscsilun, 0, sizeof(IscsiLun));
1418 return ret;
1421 static void iscsi_close(BlockDriverState *bs)
1423 IscsiLun *iscsilun = bs->opaque;
1424 struct iscsi_context *iscsi = iscsilun->iscsi;
1426 if (iscsilun->nop_timer) {
1427 timer_del(iscsilun->nop_timer);
1428 timer_free(iscsilun->nop_timer);
1430 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL);
1431 iscsi_destroy_context(iscsi);
1432 g_free(iscsilun->zeroblock);
1433 g_free(iscsilun->allocationmap);
1434 memset(iscsilun, 0, sizeof(IscsiLun));
1437 static int iscsi_refresh_limits(BlockDriverState *bs)
1439 IscsiLun *iscsilun = bs->opaque;
1441 /* We don't actually refresh here, but just return data queried in
1442 * iscsi_open(): iscsi targets don't change their limits. */
1443 if (iscsilun->lbp.lbpu) {
1444 if (iscsilun->bl.max_unmap < 0xffffffff) {
1445 bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap,
1446 iscsilun);
1448 bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1449 iscsilun);
1452 if (iscsilun->bl.max_ws_len < 0xffffffff) {
1453 bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len,
1454 iscsilun);
1456 if (iscsilun->lbp.lbpws) {
1457 bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1458 iscsilun);
1460 bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len,
1461 iscsilun);
1462 return 0;
1465 /* Since iscsi_open() ignores bdrv_flags, there is nothing to do here in
1466 * prepare. Note that this will not re-establish a connection with an iSCSI
1467 * target - it is effectively a NOP. */
1468 static int iscsi_reopen_prepare(BDRVReopenState *state,
1469 BlockReopenQueue *queue, Error **errp)
1471 /* NOP */
1472 return 0;
1475 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1477 IscsiLun *iscsilun = bs->opaque;
1478 Error *local_err = NULL;
1480 if (iscsilun->type != TYPE_DISK) {
1481 return -ENOTSUP;
1484 iscsi_readcapacity_sync(iscsilun, &local_err);
1485 if (local_err != NULL) {
1486 error_free(local_err);
1487 return -EIO;
1490 if (offset > iscsi_getlength(bs)) {
1491 return -EINVAL;
1494 if (iscsilun->allocationmap != NULL) {
1495 g_free(iscsilun->allocationmap);
1496 iscsilun->allocationmap =
1497 bitmap_new(DIV_ROUND_UP(bs->total_sectors,
1498 iscsilun->cluster_sectors));
1501 return 0;
1504 static int iscsi_create(const char *filename, QEMUOptionParameter *options,
1505 Error **errp)
1507 int ret = 0;
1508 int64_t total_size = 0;
1509 BlockDriverState *bs;
1510 IscsiLun *iscsilun = NULL;
1511 QDict *bs_options;
1513 bs = bdrv_new("", &error_abort);
1515 /* Read out options */
1516 while (options && options->name) {
1517 if (!strcmp(options->name, "size")) {
1518 total_size = options->value.n / BDRV_SECTOR_SIZE;
1520 options++;
1523 bs->opaque = g_malloc0(sizeof(struct IscsiLun));
1524 iscsilun = bs->opaque;
1526 bs_options = qdict_new();
1527 qdict_put(bs_options, "filename", qstring_from_str(filename));
1528 ret = iscsi_open(bs, bs_options, 0, NULL);
1529 QDECREF(bs_options);
1531 if (ret != 0) {
1532 goto out;
1534 if (iscsilun->nop_timer) {
1535 timer_del(iscsilun->nop_timer);
1536 timer_free(iscsilun->nop_timer);
1538 if (iscsilun->type != TYPE_DISK) {
1539 ret = -ENODEV;
1540 goto out;
1542 if (bs->total_sectors < total_size) {
1543 ret = -ENOSPC;
1544 goto out;
1547 ret = 0;
1548 out:
1549 if (iscsilun->iscsi != NULL) {
1550 iscsi_destroy_context(iscsilun->iscsi);
1552 g_free(bs->opaque);
1553 bs->opaque = NULL;
1554 bdrv_unref(bs);
1555 return ret;
1558 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1560 IscsiLun *iscsilun = bs->opaque;
1561 bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz;
1562 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
1563 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
1564 return 0;
1567 static QEMUOptionParameter iscsi_create_options[] = {
1569 .name = BLOCK_OPT_SIZE,
1570 .type = OPT_SIZE,
1571 .help = "Virtual disk size"
1573 { NULL }
1576 static BlockDriver bdrv_iscsi = {
1577 .format_name = "iscsi",
1578 .protocol_name = "iscsi",
1580 .instance_size = sizeof(IscsiLun),
1581 .bdrv_needs_filename = true,
1582 .bdrv_file_open = iscsi_open,
1583 .bdrv_close = iscsi_close,
1584 .bdrv_create = iscsi_create,
1585 .create_options = iscsi_create_options,
1586 .bdrv_reopen_prepare = iscsi_reopen_prepare,
1588 .bdrv_getlength = iscsi_getlength,
1589 .bdrv_get_info = iscsi_get_info,
1590 .bdrv_truncate = iscsi_truncate,
1591 .bdrv_refresh_limits = iscsi_refresh_limits,
1593 #if defined(LIBISCSI_FEATURE_IOVECTOR)
1594 .bdrv_co_get_block_status = iscsi_co_get_block_status,
1595 #endif
1596 .bdrv_co_discard = iscsi_co_discard,
1597 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
1598 .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
1599 #endif
1600 .bdrv_co_readv = iscsi_co_readv,
1601 .bdrv_co_writev = iscsi_co_writev,
1602 .bdrv_co_flush_to_disk = iscsi_co_flush,
1604 #ifdef __linux__
1605 .bdrv_ioctl = iscsi_ioctl,
1606 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1607 #endif
1610 static QemuOptsList qemu_iscsi_opts = {
1611 .name = "iscsi",
1612 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1613 .desc = {
1615 .name = "user",
1616 .type = QEMU_OPT_STRING,
1617 .help = "username for CHAP authentication to target",
1619 .name = "password",
1620 .type = QEMU_OPT_STRING,
1621 .help = "password for CHAP authentication to target",
1623 .name = "header-digest",
1624 .type = QEMU_OPT_STRING,
1625 .help = "HeaderDigest setting. "
1626 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1628 .name = "initiator-name",
1629 .type = QEMU_OPT_STRING,
1630 .help = "Initiator iqn name to use when connecting",
1632 { /* end of list */ }
1636 static void iscsi_block_init(void)
1638 bdrv_register(&bdrv_iscsi);
1639 qemu_add_opts(&qemu_iscsi_opts);
1642 block_init(iscsi_block_init);