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-error.h"
31 #include "block_int.h"
33 #include "hw/scsi-defs.h"
35 #include <iscsi/iscsi.h>
36 #include <iscsi/scsi-lowlevel.h>
40 #include <hw/scsi-defs.h>
43 typedef struct IscsiLun
{
44 struct iscsi_context
*iscsi
;
46 enum scsi_inquiry_peripheral_device_type type
;
52 typedef struct IscsiAIOCB
{
53 BlockDriverAIOCB common
;
57 struct scsi_task
*task
;
80 qemu_bh_delete(acb
->bh
);
82 if (acb
->canceled
== 0) {
83 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
86 if (acb
->task
!= NULL
) {
87 scsi_free_scsi_task(acb
->task
);
91 qemu_aio_release(acb
);
95 iscsi_schedule_bh(IscsiAIOCB
*acb
)
100 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
101 qemu_bh_schedule(acb
->bh
);
106 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
109 IscsiAIOCB
*acb
= private_data
;
111 acb
->status
= -ECANCELED
;
112 iscsi_schedule_bh(acb
);
116 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
118 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
119 IscsiLun
*iscsilun
= acb
->iscsilun
;
121 if (acb
->status
!= -EINPROGRESS
) {
127 /* send a task mgmt call to the target to cancel the task on the target */
128 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
129 iscsi_abort_task_cb
, acb
);
131 while (acb
->status
== -EINPROGRESS
) {
136 static AIOPool iscsi_aio_pool
= {
137 .aiocb_size
= sizeof(IscsiAIOCB
),
138 .cancel
= iscsi_aio_cancel
,
142 static void iscsi_process_read(void *arg
);
143 static void iscsi_process_write(void *arg
);
145 static int iscsi_process_flush(void *arg
)
147 IscsiLun
*iscsilun
= arg
;
149 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
153 iscsi_set_events(IscsiLun
*iscsilun
)
155 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
158 /* We always register a read handler. */
160 ev
|= iscsi_which_events(iscsi
);
161 if (ev
!= iscsilun
->events
) {
162 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
164 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
170 iscsilun
->events
= ev
;
174 iscsi_process_read(void *arg
)
176 IscsiLun
*iscsilun
= arg
;
177 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
179 iscsi_service(iscsi
, POLLIN
);
180 iscsi_set_events(iscsilun
);
184 iscsi_process_write(void *arg
)
186 IscsiLun
*iscsilun
= arg
;
187 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
189 iscsi_service(iscsi
, POLLOUT
);
190 iscsi_set_events(iscsilun
);
195 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
196 void *command_data
, void *opaque
)
198 IscsiAIOCB
*acb
= opaque
;
200 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
204 if (acb
->canceled
!= 0) {
210 error_report("Failed to write16 data to iSCSI lun. %s",
211 iscsi_get_error(iscsi
));
215 iscsi_schedule_bh(acb
);
218 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
220 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
223 static BlockDriverAIOCB
*
224 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
225 QEMUIOVector
*qiov
, int nb_sectors
,
226 BlockDriverCompletionFunc
*cb
,
229 IscsiLun
*iscsilun
= bs
->opaque
;
230 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
233 uint32_t num_sectors
;
235 struct iscsi_data data
;
237 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
238 trace_iscsi_aio_writev(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
240 acb
->iscsilun
= iscsilun
;
245 acb
->status
= -EINPROGRESS
;
247 /* XXX we should pass the iovec to write16 to avoid the extra copy */
248 /* this will allow us to get rid of 'buf' completely */
249 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
250 acb
->buf
= g_malloc(size
);
251 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, size
);
253 acb
->task
= malloc(sizeof(struct scsi_task
));
254 if (acb
->task
== NULL
) {
255 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
256 "command. %s", iscsi_get_error(iscsi
));
257 qemu_aio_release(acb
);
260 memset(acb
->task
, 0, sizeof(struct scsi_task
));
262 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
263 acb
->task
->cdb_size
= 16;
264 acb
->task
->cdb
[0] = 0x8a;
265 lba
= sector_qemu2lun(sector_num
, iscsilun
);
266 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
267 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
268 num_sectors
= size
/ iscsilun
->block_size
;
269 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
270 acb
->task
->expxferlen
= size
;
272 data
.data
= acb
->buf
;
275 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
276 iscsi_aio_write16_cb
,
279 scsi_free_scsi_task(acb
->task
);
281 qemu_aio_release(acb
);
285 iscsi_set_events(iscsilun
);
291 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
292 void *command_data
, void *opaque
)
294 IscsiAIOCB
*acb
= opaque
;
296 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
298 if (acb
->canceled
!= 0) {
304 error_report("Failed to read16 data from iSCSI lun. %s",
305 iscsi_get_error(iscsi
));
309 iscsi_schedule_bh(acb
);
312 static BlockDriverAIOCB
*
313 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
314 QEMUIOVector
*qiov
, int nb_sectors
,
315 BlockDriverCompletionFunc
*cb
,
318 IscsiLun
*iscsilun
= bs
->opaque
;
319 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
321 size_t qemu_read_size
;
324 uint32_t num_sectors
;
326 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
328 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
329 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
331 acb
->iscsilun
= iscsilun
;
336 acb
->status
= -EINPROGRESS
;
337 acb
->read_size
= qemu_read_size
;
340 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
341 * may be misaligned to the LUN, so we may need to read some extra
344 acb
->read_offset
= 0;
345 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
346 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
348 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
351 num_sectors
= (qemu_read_size
+ iscsilun
->block_size
352 + acb
->read_offset
- 1)
353 / iscsilun
->block_size
;
355 acb
->task
= malloc(sizeof(struct scsi_task
));
356 if (acb
->task
== NULL
) {
357 error_report("iSCSI: Failed to allocate task for scsi READ16 "
358 "command. %s", iscsi_get_error(iscsi
));
359 qemu_aio_release(acb
);
362 memset(acb
->task
, 0, sizeof(struct scsi_task
));
364 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
365 lba
= sector_qemu2lun(sector_num
, iscsilun
);
366 acb
->task
->expxferlen
= qemu_read_size
;
368 switch (iscsilun
->type
) {
370 acb
->task
->cdb_size
= 16;
371 acb
->task
->cdb
[0] = 0x88;
372 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
373 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
374 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
377 acb
->task
->cdb_size
= 10;
378 acb
->task
->cdb
[0] = 0x28;
379 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
380 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
384 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
388 scsi_free_scsi_task(acb
->task
);
389 qemu_aio_release(acb
);
393 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
394 scsi_task_add_data_in_buffer(acb
->task
,
395 acb
->qiov
->iov
[i
].iov_len
,
396 acb
->qiov
->iov
[i
].iov_base
);
399 iscsi_set_events(iscsilun
);
406 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
407 void *command_data
, void *opaque
)
409 IscsiAIOCB
*acb
= opaque
;
411 if (acb
->canceled
!= 0) {
417 error_report("Failed to sync10 data on iSCSI lun. %s",
418 iscsi_get_error(iscsi
));
422 iscsi_schedule_bh(acb
);
425 static BlockDriverAIOCB
*
426 iscsi_aio_flush(BlockDriverState
*bs
,
427 BlockDriverCompletionFunc
*cb
, void *opaque
)
429 IscsiLun
*iscsilun
= bs
->opaque
;
430 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
433 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
435 acb
->iscsilun
= iscsilun
;
438 acb
->status
= -EINPROGRESS
;
440 acb
->task
= iscsi_synchronizecache10_task(iscsi
, iscsilun
->lun
,
442 iscsi_synccache10_cb
,
444 if (acb
->task
== NULL
) {
445 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
446 iscsi_get_error(iscsi
));
447 qemu_aio_release(acb
);
451 iscsi_set_events(iscsilun
);
457 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
458 void *command_data
, void *opaque
)
460 IscsiAIOCB
*acb
= opaque
;
462 if (acb
->canceled
!= 0) {
468 error_report("Failed to unmap data on iSCSI lun. %s",
469 iscsi_get_error(iscsi
));
473 iscsi_schedule_bh(acb
);
476 static BlockDriverAIOCB
*
477 iscsi_aio_discard(BlockDriverState
*bs
,
478 int64_t sector_num
, int nb_sectors
,
479 BlockDriverCompletionFunc
*cb
, void *opaque
)
481 IscsiLun
*iscsilun
= bs
->opaque
;
482 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
484 struct unmap_list list
[1];
486 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
488 acb
->iscsilun
= iscsilun
;
491 acb
->status
= -EINPROGRESS
;
493 list
[0].lba
= sector_qemu2lun(sector_num
, iscsilun
);
494 list
[0].num
= nb_sectors
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
496 acb
->task
= iscsi_unmap_task(iscsi
, iscsilun
->lun
,
500 if (acb
->task
== NULL
) {
501 error_report("iSCSI: Failed to send unmap command. %s",
502 iscsi_get_error(iscsi
));
503 qemu_aio_release(acb
);
507 iscsi_set_events(iscsilun
);
514 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
515 void *command_data
, void *opaque
)
517 IscsiAIOCB
*acb
= opaque
;
519 if (acb
->canceled
!= 0) {
525 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
526 iscsi_get_error(iscsi
));
530 acb
->ioh
->driver_status
= 0;
531 acb
->ioh
->host_status
= 0;
534 #define SG_ERR_DRIVER_SENSE 0x08
536 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
539 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
541 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
542 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
543 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
544 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
547 iscsi_schedule_bh(acb
);
550 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
551 unsigned long int req
, void *buf
,
552 BlockDriverCompletionFunc
*cb
, void *opaque
)
554 IscsiLun
*iscsilun
= bs
->opaque
;
555 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
556 struct iscsi_data data
;
559 assert(req
== SG_IO
);
561 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
563 acb
->iscsilun
= iscsilun
;
566 acb
->status
= -EINPROGRESS
;
570 acb
->task
= malloc(sizeof(struct scsi_task
));
571 if (acb
->task
== NULL
) {
572 error_report("iSCSI: Failed to allocate task for scsi command. %s",
573 iscsi_get_error(iscsi
));
574 qemu_aio_release(acb
);
577 memset(acb
->task
, 0, sizeof(struct scsi_task
));
579 switch (acb
->ioh
->dxfer_direction
) {
580 case SG_DXFER_TO_DEV
:
581 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
583 case SG_DXFER_FROM_DEV
:
584 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
587 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
591 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
592 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
593 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
595 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
596 data
.data
= acb
->ioh
->dxferp
;
597 data
.size
= acb
->ioh
->dxfer_len
;
599 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
601 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
604 scsi_free_scsi_task(acb
->task
);
605 qemu_aio_release(acb
);
609 /* tell libiscsi to read straight into the buffer we got from ioctl */
610 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
611 scsi_task_add_data_in_buffer(acb
->task
,
616 iscsi_set_events(iscsilun
);
622 static void ioctl_cb(void *opaque
, int status
)
624 int *p_status
= opaque
;
628 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
630 IscsiLun
*iscsilun
= bs
->opaque
;
634 case SG_GET_VERSION_NUM
:
638 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
641 status
= -EINPROGRESS
;
642 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
644 while (status
== -EINPROGRESS
) {
657 iscsi_getlength(BlockDriverState
*bs
)
659 IscsiLun
*iscsilun
= bs
->opaque
;
662 len
= iscsilun
->num_blocks
;
663 len
*= iscsilun
->block_size
;
669 iscsi_readcapacity16_cb(struct iscsi_context
*iscsi
, int status
,
670 void *command_data
, void *opaque
)
672 struct IscsiTask
*itask
= opaque
;
673 struct scsi_readcapacity16
*rc16
;
674 struct scsi_task
*task
= command_data
;
677 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
678 iscsi_get_error(iscsi
));
681 scsi_free_scsi_task(task
);
685 rc16
= scsi_datain_unmarshall(task
);
687 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
690 scsi_free_scsi_task(task
);
694 itask
->iscsilun
->block_size
= rc16
->block_length
;
695 itask
->iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
696 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
697 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
701 scsi_free_scsi_task(task
);
705 iscsi_readcapacity10_cb(struct iscsi_context
*iscsi
, int status
,
706 void *command_data
, void *opaque
)
708 struct IscsiTask
*itask
= opaque
;
709 struct scsi_readcapacity10
*rc10
;
710 struct scsi_task
*task
= command_data
;
713 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
714 iscsi_get_error(iscsi
));
717 scsi_free_scsi_task(task
);
721 rc10
= scsi_datain_unmarshall(task
);
723 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
726 scsi_free_scsi_task(task
);
730 itask
->iscsilun
->block_size
= rc10
->block_size
;
731 if (rc10
->lba
== 0) {
732 /* blank disk loaded */
733 itask
->iscsilun
->num_blocks
= 0;
735 itask
->iscsilun
->num_blocks
= rc10
->lba
+ 1;
737 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
738 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
742 scsi_free_scsi_task(task
);
746 iscsi_inquiry_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
749 struct IscsiTask
*itask
= opaque
;
750 struct scsi_task
*task
= command_data
;
751 struct scsi_inquiry_standard
*inq
;
756 scsi_free_scsi_task(task
);
760 inq
= scsi_datain_unmarshall(task
);
762 error_report("iSCSI: Failed to unmarshall inquiry data.");
765 scsi_free_scsi_task(task
);
769 itask
->iscsilun
->type
= inq
->periperal_device_type
;
771 scsi_free_scsi_task(task
);
773 switch (itask
->iscsilun
->type
) {
775 task
= iscsi_readcapacity16_task(iscsi
, itask
->iscsilun
->lun
,
776 iscsi_readcapacity16_cb
, opaque
);
778 error_report("iSCSI: failed to send readcapacity16 command.");
785 task
= iscsi_readcapacity10_task(iscsi
, itask
->iscsilun
->lun
,
787 iscsi_readcapacity10_cb
, opaque
);
789 error_report("iSCSI: failed to send readcapacity16 command.");
802 iscsi_connect_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
805 struct IscsiTask
*itask
= opaque
;
806 struct scsi_task
*task
;
814 task
= iscsi_inquiry_task(iscsi
, itask
->iscsilun
->lun
,
816 iscsi_inquiry_cb
, opaque
);
818 error_report("iSCSI: failed to send inquiry command.");
825 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
829 const char *user
= NULL
;
830 const char *password
= NULL
;
832 list
= qemu_find_opts("iscsi");
837 opts
= qemu_opts_find(list
, target
);
839 opts
= QTAILQ_FIRST(&list
->head
);
845 user
= qemu_opt_get(opts
, "user");
850 password
= qemu_opt_get(opts
, "password");
852 error_report("CHAP username specified but no password was given");
856 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
857 error_report("Failed to set initiator username and password");
864 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
868 const char *digest
= NULL
;
870 list
= qemu_find_opts("iscsi");
875 opts
= qemu_opts_find(list
, target
);
877 opts
= QTAILQ_FIRST(&list
->head
);
883 digest
= qemu_opt_get(opts
, "header-digest");
888 if (!strcmp(digest
, "CRC32C")) {
889 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
890 } else if (!strcmp(digest
, "NONE")) {
891 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
892 } else if (!strcmp(digest
, "CRC32C-NONE")) {
893 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
894 } else if (!strcmp(digest
, "NONE-CRC32C")) {
895 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
897 error_report("Invalid header-digest setting : %s", digest
);
901 static char *parse_initiator_name(const char *target
)
905 const char *name
= NULL
;
906 const char *iscsi_name
= qemu_get_vm_name();
908 list
= qemu_find_opts("iscsi");
910 opts
= qemu_opts_find(list
, target
);
912 opts
= QTAILQ_FIRST(&list
->head
);
915 name
= qemu_opt_get(opts
, "initiator-name");
920 return g_strdup(name
);
922 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
923 iscsi_name
? ":" : "",
924 iscsi_name
? iscsi_name
: "");
929 * We support iscsi url's on the form
930 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
932 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
934 IscsiLun
*iscsilun
= bs
->opaque
;
935 struct iscsi_context
*iscsi
= NULL
;
936 struct iscsi_url
*iscsi_url
= NULL
;
937 struct IscsiTask task
;
938 char *initiator_name
= NULL
;
941 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
942 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
943 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
944 "of 512", BDRV_SECTOR_SIZE
);
948 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
949 if (iscsi_url
== NULL
) {
950 error_report("Failed to parse URL : %s %s", filename
,
951 iscsi_get_error(iscsi
));
956 memset(iscsilun
, 0, sizeof(IscsiLun
));
958 initiator_name
= parse_initiator_name(iscsi_url
->target
);
960 iscsi
= iscsi_create_context(initiator_name
);
962 error_report("iSCSI: Failed to create iSCSI context.");
967 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
968 error_report("iSCSI: Failed to set target name.");
973 if (iscsi_url
->user
!= NULL
) {
974 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
977 error_report("Failed to set initiator username and password");
983 /* check if we got CHAP username/password via the options */
984 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
985 error_report("iSCSI: Failed to set CHAP user/password");
990 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
991 error_report("iSCSI: Failed to set session type to normal.");
996 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
998 /* check if we got HEADER_DIGEST via the options */
999 parse_header_digest(iscsi
, iscsi_url
->target
);
1001 task
.iscsilun
= iscsilun
;
1006 iscsilun
->iscsi
= iscsi
;
1007 iscsilun
->lun
= iscsi_url
->lun
;
1009 if (iscsi_full_connect_async(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
,
1010 iscsi_connect_cb
, &task
)
1012 error_report("iSCSI: Failed to start async connect.");
1017 while (!task
.complete
) {
1018 iscsi_set_events(iscsilun
);
1021 if (task
.status
!= 0) {
1022 error_report("iSCSI: Failed to connect to LUN : %s",
1023 iscsi_get_error(iscsi
));
1028 /* Medium changer or tape. We dont have any emulation for this so this must
1029 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1030 * to read from the device to guess the image format.
1032 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1033 iscsilun
->type
== TYPE_TAPE
) {
1040 if (initiator_name
!= NULL
) {
1041 g_free(initiator_name
);
1043 if (iscsi_url
!= NULL
) {
1044 iscsi_destroy_url(iscsi_url
);
1048 if (iscsi
!= NULL
) {
1049 iscsi_destroy_context(iscsi
);
1051 memset(iscsilun
, 0, sizeof(IscsiLun
));
1056 static void iscsi_close(BlockDriverState
*bs
)
1058 IscsiLun
*iscsilun
= bs
->opaque
;
1059 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1061 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
1062 iscsi_destroy_context(iscsi
);
1063 memset(iscsilun
, 0, sizeof(IscsiLun
));
1066 static BlockDriver bdrv_iscsi
= {
1067 .format_name
= "iscsi",
1068 .protocol_name
= "iscsi",
1070 .instance_size
= sizeof(IscsiLun
),
1071 .bdrv_file_open
= iscsi_open
,
1072 .bdrv_close
= iscsi_close
,
1074 .bdrv_getlength
= iscsi_getlength
,
1076 .bdrv_aio_readv
= iscsi_aio_readv
,
1077 .bdrv_aio_writev
= iscsi_aio_writev
,
1078 .bdrv_aio_flush
= iscsi_aio_flush
,
1080 .bdrv_aio_discard
= iscsi_aio_discard
,
1083 .bdrv_ioctl
= iscsi_ioctl
,
1084 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1088 static void iscsi_block_init(void)
1090 bdrv_register(&bdrv_iscsi
);
1093 block_init(iscsi_block_init
);