2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "config-host.h"
28 #include <arpa/inet.h>
29 #include "qemu-common.h"
30 #include "qemu/config-file.h"
31 #include "qemu/error-report.h"
32 #include "block/block_int.h"
34 #include "hw/scsi-defs.h"
36 #include <iscsi/iscsi.h>
37 #include <iscsi/scsi-lowlevel.h>
41 #include <hw/scsi-defs.h>
44 typedef struct IscsiLun {
45 struct iscsi_context *iscsi;
47 enum scsi_inquiry_peripheral_device_type type;
54 typedef struct IscsiAIOCB {
55 BlockDriverAIOCB common;
59 struct scsi_task *task;
70 #define NOP_INTERVAL 5000
71 #define MAX_NOP_FAILURES 3
78 qemu_bh_delete(acb->bh);
80 if (acb->canceled == 0) {
81 acb->common.cb(acb->common.opaque, acb->status);
84 if (acb->task != NULL) {
85 scsi_free_scsi_task(acb->task);
89 qemu_aio_release(acb);
93 iscsi_schedule_bh(IscsiAIOCB *acb)
98 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
99 qemu_bh_schedule(acb->bh);
104 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
107 IscsiAIOCB *acb = private_data;
109 acb->status = -ECANCELED;
110 iscsi_schedule_bh(acb);
114 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
116 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
117 IscsiLun *iscsilun = acb->iscsilun;
119 if (acb->status != -EINPROGRESS) {
125 /* send a task mgmt call to the target to cancel the task on the target */
126 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
127 iscsi_abort_task_cb, acb);
129 while (acb->status == -EINPROGRESS) {
134 static const AIOCBInfo iscsi_aiocb_info = {
135 .aiocb_size = sizeof(IscsiAIOCB),
136 .cancel = iscsi_aio_cancel,
140 static void iscsi_process_read(void *arg);
141 static void iscsi_process_write(void *arg);
143 static int iscsi_process_flush(void *arg)
145 IscsiLun *iscsilun = arg;
147 return iscsi_queue_length(iscsilun->iscsi) > 0;
151 iscsi_set_events(IscsiLun *iscsilun)
153 struct iscsi_context *iscsi = iscsilun->iscsi;
156 /* We always register a read handler. */
158 ev |= iscsi_which_events(iscsi);
159 if (ev != iscsilun->events) {
160 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
162 (ev & POLLOUT) ? iscsi_process_write : NULL,
168 iscsilun->events = ev;
172 iscsi_process_read(void *arg)
174 IscsiLun *iscsilun = arg;
175 struct iscsi_context *iscsi = iscsilun->iscsi;
177 iscsi_service(iscsi, POLLIN);
178 iscsi_set_events(iscsilun);
182 iscsi_process_write(void *arg)
184 IscsiLun *iscsilun = arg;
185 struct iscsi_context *iscsi = iscsilun->iscsi;
187 iscsi_service(iscsi, POLLOUT);
188 iscsi_set_events(iscsilun);
193 iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
194 void *command_data, void *opaque)
196 IscsiAIOCB *acb = opaque;
198 trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
202 if (acb->canceled != 0) {
208 error_report("Failed to write16 data to iSCSI lun. %s",
209 iscsi_get_error(iscsi));
213 iscsi_schedule_bh(acb);
216 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
218 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
221 static BlockDriverAIOCB *
222 iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
223 QEMUIOVector *qiov, int nb_sectors,
224 BlockDriverCompletionFunc *cb,
227 IscsiLun *iscsilun = bs->opaque;
228 struct iscsi_context *iscsi = iscsilun->iscsi;
231 uint32_t num_sectors;
233 struct iscsi_data data;
235 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
236 trace_iscsi_aio_writev(iscsi, sector_num, nb_sectors, opaque, acb);
238 acb->iscsilun = iscsilun;
243 acb->status = -EINPROGRESS;
245 /* XXX we should pass the iovec to write16 to avoid the extra copy */
246 /* this will allow us to get rid of 'buf' completely */
247 size = nb_sectors * BDRV_SECTOR_SIZE;
248 data.size = MIN(size, acb->qiov->size);
250 /* if the iovec only contains one buffer we can pass it directly */
251 if (acb->qiov->niov == 1) {
253 data.data = acb->qiov->iov[0].iov_base;
255 acb->buf = g_malloc(data.size);
256 qemu_iovec_to_buf(acb->qiov, 0, acb->buf, data.size);
257 data.data = acb->buf;
260 acb->task = malloc(sizeof(struct scsi_task));
261 if (acb->task == NULL) {
262 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
263 "command. %s", iscsi_get_error(iscsi));
264 qemu_aio_release(acb);
267 memset(acb->task, 0, sizeof(struct scsi_task));
269 acb->task->xfer_dir = SCSI_XFER_WRITE;
270 acb->task->cdb_size = 16;
271 acb->task->cdb[0] = 0x8a;
272 lba = sector_qemu2lun(sector_num, iscsilun);
273 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
274 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
275 num_sectors = size / iscsilun->block_size;
276 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
277 acb->task->expxferlen = size;
279 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
280 iscsi_aio_write16_cb,
283 scsi_free_scsi_task(acb->task);
285 qemu_aio_release(acb);
289 iscsi_set_events(iscsilun);
295 iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
296 void *command_data, void *opaque)
298 IscsiAIOCB *acb = opaque;
300 trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
302 if (acb->canceled != 0) {
308 error_report("Failed to read16 data from iSCSI lun. %s",
309 iscsi_get_error(iscsi));
313 iscsi_schedule_bh(acb);
316 static BlockDriverAIOCB *
317 iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
318 QEMUIOVector *qiov, int nb_sectors,
319 BlockDriverCompletionFunc *cb,
322 IscsiLun *iscsilun = bs->opaque;
323 struct iscsi_context *iscsi = iscsilun->iscsi;
325 size_t qemu_read_size;
328 uint32_t num_sectors;
330 qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
332 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
333 trace_iscsi_aio_readv(iscsi, sector_num, nb_sectors, opaque, acb);
335 acb->iscsilun = iscsilun;
340 acb->status = -EINPROGRESS;
341 acb->read_size = qemu_read_size;
344 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
345 * may be misaligned to the LUN, so we may need to read some extra
348 acb->read_offset = 0;
349 if (iscsilun->block_size > BDRV_SECTOR_SIZE) {
350 uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num;
352 acb->read_offset = bdrv_offset % iscsilun->block_size;
355 num_sectors = (qemu_read_size + iscsilun->block_size
356 + acb->read_offset - 1)
357 / iscsilun->block_size;
359 acb->task = malloc(sizeof(struct scsi_task));
360 if (acb->task == NULL) {
361 error_report("iSCSI: Failed to allocate task for scsi READ16 "
362 "command. %s", iscsi_get_error(iscsi));
363 qemu_aio_release(acb);
366 memset(acb->task, 0, sizeof(struct scsi_task));
368 acb->task->xfer_dir = SCSI_XFER_READ;
369 lba = sector_qemu2lun(sector_num, iscsilun);
370 acb->task->expxferlen = qemu_read_size;
372 switch (iscsilun->type) {
374 acb->task->cdb_size = 16;
375 acb->task->cdb[0] = 0x88;
376 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
377 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
378 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
381 acb->task->cdb_size = 10;
382 acb->task->cdb[0] = 0x28;
383 *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
384 *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
388 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
392 scsi_free_scsi_task(acb->task);
393 qemu_aio_release(acb);
397 for (i = 0; i < acb->qiov->niov; i++) {
398 scsi_task_add_data_in_buffer(acb->task,
399 acb->qiov->iov[i].iov_len,
400 acb->qiov->iov[i].iov_base);
403 iscsi_set_events(iscsilun);
410 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
411 void *command_data, void *opaque)
413 IscsiAIOCB *acb = opaque;
415 if (acb->canceled != 0) {
421 error_report("Failed to sync10 data on iSCSI lun. %s",
422 iscsi_get_error(iscsi));
426 iscsi_schedule_bh(acb);
429 static BlockDriverAIOCB *
430 iscsi_aio_flush(BlockDriverState *bs,
431 BlockDriverCompletionFunc *cb, void *opaque)
433 IscsiLun *iscsilun = bs->opaque;
434 struct iscsi_context *iscsi = iscsilun->iscsi;
437 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
439 acb->iscsilun = iscsilun;
442 acb->status = -EINPROGRESS;
444 acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun,
446 iscsi_synccache10_cb,
448 if (acb->task == NULL) {
449 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
450 iscsi_get_error(iscsi));
451 qemu_aio_release(acb);
455 iscsi_set_events(iscsilun);
461 iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
462 void *command_data, void *opaque)
464 IscsiAIOCB *acb = opaque;
466 if (acb->canceled != 0) {
472 error_report("Failed to unmap data on iSCSI lun. %s",
473 iscsi_get_error(iscsi));
477 iscsi_schedule_bh(acb);
480 static BlockDriverAIOCB *
481 iscsi_aio_discard(BlockDriverState *bs,
482 int64_t sector_num, int nb_sectors,
483 BlockDriverCompletionFunc *cb, void *opaque)
485 IscsiLun *iscsilun = bs->opaque;
486 struct iscsi_context *iscsi = iscsilun->iscsi;
488 struct unmap_list list[1];
490 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
492 acb->iscsilun = iscsilun;
495 acb->status = -EINPROGRESS;
497 list[0].lba = sector_qemu2lun(sector_num, iscsilun);
498 list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size;
500 acb->task = iscsi_unmap_task(iscsi, iscsilun->lun,
504 if (acb->task == NULL) {
505 error_report("iSCSI: Failed to send unmap command. %s",
506 iscsi_get_error(iscsi));
507 qemu_aio_release(acb);
511 iscsi_set_events(iscsilun);
518 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
519 void *command_data, void *opaque)
521 IscsiAIOCB *acb = opaque;
523 if (acb->canceled != 0) {
529 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
530 iscsi_get_error(iscsi));
534 acb->ioh->driver_status = 0;
535 acb->ioh->host_status = 0;
538 #define SG_ERR_DRIVER_SENSE 0x08
540 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
543 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
545 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
546 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
547 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
548 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
551 iscsi_schedule_bh(acb);
554 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
555 unsigned long int req, void *buf,
556 BlockDriverCompletionFunc *cb, void *opaque)
558 IscsiLun *iscsilun = bs->opaque;
559 struct iscsi_context *iscsi = iscsilun->iscsi;
560 struct iscsi_data data;
563 assert(req == SG_IO);
565 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
567 acb->iscsilun = iscsilun;
570 acb->status = -EINPROGRESS;
574 acb->task = malloc(sizeof(struct scsi_task));
575 if (acb->task == NULL) {
576 error_report("iSCSI: Failed to allocate task for scsi command. %s",
577 iscsi_get_error(iscsi));
578 qemu_aio_release(acb);
581 memset(acb->task, 0, sizeof(struct scsi_task));
583 switch (acb->ioh->dxfer_direction) {
584 case SG_DXFER_TO_DEV:
585 acb->task->xfer_dir = SCSI_XFER_WRITE;
587 case SG_DXFER_FROM_DEV:
588 acb->task->xfer_dir = SCSI_XFER_READ;
591 acb->task->xfer_dir = SCSI_XFER_NONE;
595 acb->task->cdb_size = acb->ioh->cmd_len;
596 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
597 acb->task->expxferlen = acb->ioh->dxfer_len;
599 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
600 data.data = acb->ioh->dxferp;
601 data.size = acb->ioh->dxfer_len;
603 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
605 (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
608 scsi_free_scsi_task(acb->task);
609 qemu_aio_release(acb);
613 /* tell libiscsi to read straight into the buffer we got from ioctl */
614 if (acb->task->xfer_dir == SCSI_XFER_READ) {
615 scsi_task_add_data_in_buffer(acb->task,
620 iscsi_set_events(iscsilun);
626 static void ioctl_cb(void *opaque, int status)
628 int *p_status = opaque;
632 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
634 IscsiLun *iscsilun = bs->opaque;
638 case SG_GET_VERSION_NUM:
642 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
645 status = -EINPROGRESS;
646 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
648 while (status == -EINPROGRESS) {
661 iscsi_getlength(BlockDriverState *bs)
663 IscsiLun *iscsilun = bs->opaque;
666 len = iscsilun->num_blocks;
667 len *= iscsilun->block_size;
672 static int parse_chap(struct iscsi_context *iscsi, const char *target)
676 const char *user = NULL;
677 const char *password = NULL;
679 list = qemu_find_opts("iscsi");
684 opts = qemu_opts_find(list, target);
686 opts = QTAILQ_FIRST(&list->head);
692 user = qemu_opt_get(opts, "user");
697 password = qemu_opt_get(opts, "password");
699 error_report("CHAP username specified but no password was given");
703 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
704 error_report("Failed to set initiator username and password");
711 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
715 const char *digest = NULL;
717 list = qemu_find_opts("iscsi");
722 opts = qemu_opts_find(list, target);
724 opts = QTAILQ_FIRST(&list->head);
730 digest = qemu_opt_get(opts, "header-digest");
735 if (!strcmp(digest, "CRC32C")) {
736 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
737 } else if (!strcmp(digest, "NONE")) {
738 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
739 } else if (!strcmp(digest, "CRC32C-NONE")) {
740 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
741 } else if (!strcmp(digest, "NONE-CRC32C")) {
742 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
744 error_report("Invalid header-digest setting : %s", digest);
748 static char *parse_initiator_name(const char *target)
752 const char *name = NULL;
753 const char *iscsi_name = qemu_get_vm_name();
755 list = qemu_find_opts("iscsi");
757 opts = qemu_opts_find(list, target);
759 opts = QTAILQ_FIRST(&list->head);
762 name = qemu_opt_get(opts, "initiator-name");
767 return g_strdup(name);
769 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
770 iscsi_name ? ":" : "",
771 iscsi_name ? iscsi_name : "");
775 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
776 static void iscsi_nop_timed_event(void *opaque)
778 IscsiLun *iscsilun = opaque;
780 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
781 error_report("iSCSI: NOP timeout. Reconnecting...");
782 iscsi_reconnect(iscsilun->iscsi);
785 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
786 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
790 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
791 iscsi_set_events(iscsilun);
796 * We support iscsi url's on the form
797 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
799 static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)
801 IscsiLun *iscsilun = bs->opaque;
802 struct iscsi_context *iscsi = NULL;
803 struct iscsi_url *iscsi_url = NULL;
804 struct scsi_task *task = NULL;
805 struct scsi_inquiry_standard *inq = NULL;
806 struct scsi_readcapacity10 *rc10 = NULL;
807 struct scsi_readcapacity16 *rc16 = NULL;
808 char *initiator_name = NULL;
811 if ((BDRV_SECTOR_SIZE % 512) != 0) {
812 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
813 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
814 "of 512", BDRV_SECTOR_SIZE);
818 iscsi_url = iscsi_parse_full_url(iscsi, filename);
819 if (iscsi_url == NULL) {
820 error_report("Failed to parse URL : %s", filename);
825 memset(iscsilun, 0, sizeof(IscsiLun));
827 initiator_name = parse_initiator_name(iscsi_url->target);
829 iscsi = iscsi_create_context(initiator_name);
831 error_report("iSCSI: Failed to create iSCSI context.");
836 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
837 error_report("iSCSI: Failed to set target name.");
842 if (iscsi_url->user != NULL) {
843 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
846 error_report("Failed to set initiator username and password");
852 /* check if we got CHAP username/password via the options */
853 if (parse_chap(iscsi, iscsi_url->target) != 0) {
854 error_report("iSCSI: Failed to set CHAP user/password");
859 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
860 error_report("iSCSI: Failed to set session type to normal.");
865 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
867 /* check if we got HEADER_DIGEST via the options */
868 parse_header_digest(iscsi, iscsi_url->target);
870 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
871 error_report("iSCSI: Failed to connect to LUN : %s",
872 iscsi_get_error(iscsi));
877 iscsilun->iscsi = iscsi;
878 iscsilun->lun = iscsi_url->lun;
880 task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
882 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
883 error_report("iSCSI: failed to send inquiry command.");
888 inq = scsi_datain_unmarshall(task);
890 error_report("iSCSI: Failed to unmarshall inquiry data.");
895 iscsilun->type = inq->periperal_device_type;
897 scsi_free_scsi_task(task);
899 switch (iscsilun->type) {
901 task = iscsi_readcapacity16_sync(iscsi, iscsilun->lun);
902 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
903 error_report("iSCSI: failed to send readcapacity16 command.");
907 rc16 = scsi_datain_unmarshall(task);
909 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
913 iscsilun->block_size = rc16->block_length;
914 iscsilun->num_blocks = rc16->returned_lba + 1;
917 task = iscsi_readcapacity10_sync(iscsi, iscsilun->lun, 0, 0);
918 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
919 error_report("iSCSI: failed to send readcapacity10 command.");
923 rc10 = scsi_datain_unmarshall(task);
925 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
929 iscsilun->block_size = rc10->block_size;
930 if (rc10->lba == 0) {
931 /* blank disk loaded */
932 iscsilun->num_blocks = 0;
934 iscsilun->num_blocks = rc10->lba + 1;
941 bs->total_sectors = iscsilun->num_blocks *
942 iscsilun->block_size / BDRV_SECTOR_SIZE ;
944 /* Medium changer or tape. We dont have any emulation for this so this must
945 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
946 * to read from the device to guess the image format.
948 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
949 iscsilun->type == TYPE_TAPE) {
955 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
956 /* Set up a timer for sending out iSCSI NOPs */
957 iscsilun->nop_timer = qemu_new_timer_ms(rt_clock, iscsi_nop_timed_event, iscsilun);
958 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
962 if (initiator_name != NULL) {
963 g_free(initiator_name);
965 if (iscsi_url != NULL) {
966 iscsi_destroy_url(iscsi_url);
969 scsi_free_scsi_task(task);
974 iscsi_destroy_context(iscsi);
976 memset(iscsilun, 0, sizeof(IscsiLun));
981 static void iscsi_close(BlockDriverState *bs)
983 IscsiLun *iscsilun = bs->opaque;
984 struct iscsi_context *iscsi = iscsilun->iscsi;
986 if (iscsilun->nop_timer) {
987 qemu_del_timer(iscsilun->nop_timer);
988 qemu_free_timer(iscsilun->nop_timer);
990 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
991 iscsi_destroy_context(iscsi);
992 memset(iscsilun, 0, sizeof(IscsiLun));
995 static int iscsi_has_zero_init(BlockDriverState *bs)
1000 static int iscsi_create(const char *filename, QEMUOptionParameter *options)
1003 int64_t total_size = 0;
1004 BlockDriverState bs;
1005 IscsiLun *iscsilun = NULL;
1007 memset(&bs, 0, sizeof(BlockDriverState));
1009 /* Read out options */
1010 while (options && options->name) {
1011 if (!strcmp(options->name, "size")) {
1012 total_size = options->value.n / BDRV_SECTOR_SIZE;
1017 bs.opaque = g_malloc0(sizeof(struct IscsiLun));
1018 iscsilun = bs.opaque;
1020 ret = iscsi_open(&bs, filename, 0);
1024 if (iscsilun->nop_timer) {
1025 qemu_del_timer(iscsilun->nop_timer);
1026 qemu_free_timer(iscsilun->nop_timer);
1028 if (iscsilun->type != TYPE_DISK) {
1032 if (bs.total_sectors < total_size) {
1038 if (iscsilun->iscsi != NULL) {
1039 iscsi_destroy_context(iscsilun->iscsi);
1045 static QEMUOptionParameter iscsi_create_options[] = {
1047 .name = BLOCK_OPT_SIZE,
1049 .help = "Virtual disk size"
1054 static BlockDriver bdrv_iscsi = {
1055 .format_name = "iscsi",
1056 .protocol_name = "iscsi",
1058 .instance_size = sizeof(IscsiLun),
1059 .bdrv_file_open = iscsi_open,
1060 .bdrv_close = iscsi_close,
1061 .bdrv_create = iscsi_create,
1062 .create_options = iscsi_create_options,
1064 .bdrv_getlength = iscsi_getlength,
1066 .bdrv_aio_readv = iscsi_aio_readv,
1067 .bdrv_aio_writev = iscsi_aio_writev,
1068 .bdrv_aio_flush = iscsi_aio_flush,
1070 .bdrv_aio_discard = iscsi_aio_discard,
1071 .bdrv_has_zero_init = iscsi_has_zero_init,
1074 .bdrv_ioctl = iscsi_ioctl,
1075 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1079 static QemuOptsList qemu_iscsi_opts = {
1081 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1085 .type = QEMU_OPT_STRING,
1086 .help = "username for CHAP authentication to target",
1089 .type = QEMU_OPT_STRING,
1090 .help = "password for CHAP authentication to target",
1092 .name = "header-digest",
1093 .type = QEMU_OPT_STRING,
1094 .help = "HeaderDigest setting. "
1095 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1097 .name = "initiator-name",
1098 .type = QEMU_OPT_STRING,
1099 .help = "Initiator iqn name to use when connecting",
1101 { /* end of list */ }
1105 static void iscsi_block_init(void)
1107 bdrv_register(&bdrv_iscsi);
1108 qemu_add_opts(&qemu_iscsi_opts);
1111 block_init(iscsi_block_init);