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
;
76 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
79 IscsiAIOCB
*acb
= (IscsiAIOCB
*)private_data
;
81 scsi_free_scsi_task(acb
->task
);
86 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
88 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
89 IscsiLun
*iscsilun
= acb
->iscsilun
;
93 acb
->common
.cb(acb
->common
.opaque
, -ECANCELED
);
95 /* send a task mgmt call to the target to cancel the task on the target
96 * this also cancels the task in libiscsi
98 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
99 iscsi_abort_task_cb
, &acb
);
102 static AIOPool iscsi_aio_pool
= {
103 .aiocb_size
= sizeof(IscsiAIOCB
),
104 .cancel
= iscsi_aio_cancel
,
108 static void iscsi_process_read(void *arg
);
109 static void iscsi_process_write(void *arg
);
111 static int iscsi_process_flush(void *arg
)
113 IscsiLun
*iscsilun
= arg
;
115 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
119 iscsi_set_events(IscsiLun
*iscsilun
)
121 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
124 /* We always register a read handler. */
126 ev
|= iscsi_which_events(iscsi
);
127 if (ev
!= iscsilun
->events
) {
128 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
130 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
136 /* If we just added an event, the callback might be delayed
137 * unless we call qemu_notify_event().
139 if (ev
& ~iscsilun
->events
) {
142 iscsilun
->events
= ev
;
146 iscsi_process_read(void *arg
)
148 IscsiLun
*iscsilun
= arg
;
149 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
151 iscsi_service(iscsi
, POLLIN
);
152 iscsi_set_events(iscsilun
);
156 iscsi_process_write(void *arg
)
158 IscsiLun
*iscsilun
= arg
;
159 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
161 iscsi_service(iscsi
, POLLOUT
);
162 iscsi_set_events(iscsilun
);
167 iscsi_schedule_bh(QEMUBHFunc
*cb
, IscsiAIOCB
*acb
)
169 acb
->bh
= qemu_bh_new(cb
, acb
);
171 error_report("oom: could not create iscsi bh");
175 qemu_bh_schedule(acb
->bh
);
180 iscsi_readv_writev_bh_cb(void *p
)
184 qemu_bh_delete(acb
->bh
);
186 if (!acb
->canceled
) {
187 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
190 qemu_aio_release(acb
);
196 scsi_free_scsi_task(acb
->task
);
202 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
203 void *command_data
, void *opaque
)
205 IscsiAIOCB
*acb
= opaque
;
207 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
212 qemu_aio_release(acb
);
218 error_report("Failed to write16 data to iSCSI lun. %s",
219 iscsi_get_error(iscsi
));
223 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
226 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
228 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
231 static BlockDriverAIOCB
*
232 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
233 QEMUIOVector
*qiov
, int nb_sectors
,
234 BlockDriverCompletionFunc
*cb
,
237 IscsiLun
*iscsilun
= bs
->opaque
;
238 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
241 uint32_t num_sectors
;
243 struct iscsi_data data
;
245 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
246 trace_iscsi_aio_writev(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
248 acb
->iscsilun
= iscsilun
;
253 /* XXX we should pass the iovec to write16 to avoid the extra copy */
254 /* this will allow us to get rid of 'buf' completely */
255 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
256 acb
->buf
= g_malloc(size
);
257 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, size
);
259 acb
->task
= malloc(sizeof(struct scsi_task
));
260 if (acb
->task
== NULL
) {
261 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
262 "command. %s", iscsi_get_error(iscsi
));
263 qemu_aio_release(acb
);
266 memset(acb
->task
, 0, sizeof(struct scsi_task
));
268 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
269 acb
->task
->cdb_size
= 16;
270 acb
->task
->cdb
[0] = 0x8a;
271 if (!(bs
->open_flags
& BDRV_O_CACHE_WB
)) {
272 /* set FUA on writes when cache mode is write through */
273 acb
->task
->cdb
[1] |= 0x04;
275 lba
= sector_qemu2lun(sector_num
, iscsilun
);
276 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
277 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
278 num_sectors
= size
/ iscsilun
->block_size
;
279 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
280 acb
->task
->expxferlen
= size
;
282 data
.data
= acb
->buf
;
285 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
286 iscsi_aio_write16_cb
,
289 scsi_free_scsi_task(acb
->task
);
291 qemu_aio_release(acb
);
295 iscsi_set_events(iscsilun
);
301 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
302 void *command_data
, void *opaque
)
304 IscsiAIOCB
*acb
= opaque
;
306 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
309 qemu_aio_release(acb
);
315 error_report("Failed to read16 data from iSCSI lun. %s",
316 iscsi_get_error(iscsi
));
320 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
323 static BlockDriverAIOCB
*
324 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
325 QEMUIOVector
*qiov
, int nb_sectors
,
326 BlockDriverCompletionFunc
*cb
,
329 IscsiLun
*iscsilun
= bs
->opaque
;
330 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
332 size_t qemu_read_size
;
335 uint32_t num_sectors
;
337 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
339 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
340 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
342 acb
->iscsilun
= iscsilun
;
346 acb
->read_size
= qemu_read_size
;
349 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
350 * may be misaligned to the LUN, so we may need to read some extra
353 acb
->read_offset
= 0;
354 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
355 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
357 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
360 num_sectors
= (qemu_read_size
+ iscsilun
->block_size
361 + acb
->read_offset
- 1)
362 / iscsilun
->block_size
;
364 acb
->task
= malloc(sizeof(struct scsi_task
));
365 if (acb
->task
== NULL
) {
366 error_report("iSCSI: Failed to allocate task for scsi READ16 "
367 "command. %s", iscsi_get_error(iscsi
));
368 qemu_aio_release(acb
);
371 memset(acb
->task
, 0, sizeof(struct scsi_task
));
373 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
374 lba
= sector_qemu2lun(sector_num
, iscsilun
);
375 acb
->task
->expxferlen
= qemu_read_size
;
377 switch (iscsilun
->type
) {
379 acb
->task
->cdb_size
= 16;
380 acb
->task
->cdb
[0] = 0x88;
381 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
382 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
383 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
386 acb
->task
->cdb_size
= 10;
387 acb
->task
->cdb
[0] = 0x28;
388 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
389 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
393 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
397 scsi_free_scsi_task(acb
->task
);
398 qemu_aio_release(acb
);
402 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
403 scsi_task_add_data_in_buffer(acb
->task
,
404 acb
->qiov
->iov
[i
].iov_len
,
405 acb
->qiov
->iov
[i
].iov_base
);
408 iscsi_set_events(iscsilun
);
415 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
416 void *command_data
, void *opaque
)
418 IscsiAIOCB
*acb
= opaque
;
421 qemu_aio_release(acb
);
427 error_report("Failed to sync10 data on iSCSI lun. %s",
428 iscsi_get_error(iscsi
));
432 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
435 static BlockDriverAIOCB
*
436 iscsi_aio_flush(BlockDriverState
*bs
,
437 BlockDriverCompletionFunc
*cb
, void *opaque
)
439 IscsiLun
*iscsilun
= bs
->opaque
;
440 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
443 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
445 acb
->iscsilun
= iscsilun
;
448 acb
->task
= iscsi_synchronizecache10_task(iscsi
, iscsilun
->lun
,
450 iscsi_synccache10_cb
,
452 if (acb
->task
== NULL
) {
453 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
454 iscsi_get_error(iscsi
));
455 qemu_aio_release(acb
);
459 iscsi_set_events(iscsilun
);
465 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
466 void *command_data
, void *opaque
)
468 IscsiAIOCB
*acb
= opaque
;
471 qemu_aio_release(acb
);
477 error_report("Failed to unmap data on iSCSI lun. %s",
478 iscsi_get_error(iscsi
));
482 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
485 static BlockDriverAIOCB
*
486 iscsi_aio_discard(BlockDriverState
*bs
,
487 int64_t sector_num
, int nb_sectors
,
488 BlockDriverCompletionFunc
*cb
, void *opaque
)
490 IscsiLun
*iscsilun
= bs
->opaque
;
491 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
493 struct unmap_list list
[1];
495 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
497 acb
->iscsilun
= iscsilun
;
500 list
[0].lba
= sector_qemu2lun(sector_num
, iscsilun
);
501 list
[0].num
= nb_sectors
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
503 acb
->task
= iscsi_unmap_task(iscsi
, iscsilun
->lun
,
507 if (acb
->task
== NULL
) {
508 error_report("iSCSI: Failed to send unmap command. %s",
509 iscsi_get_error(iscsi
));
510 qemu_aio_release(acb
);
514 iscsi_set_events(iscsilun
);
521 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
522 void *command_data
, void *opaque
)
524 IscsiAIOCB
*acb
= opaque
;
527 qemu_aio_release(acb
);
533 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
534 iscsi_get_error(iscsi
));
538 acb
->ioh
->driver_status
= 0;
539 acb
->ioh
->host_status
= 0;
542 #define SG_ERR_DRIVER_SENSE 0x08
544 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
547 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
549 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
550 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
551 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
552 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
555 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
558 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
559 unsigned long int req
, void *buf
,
560 BlockDriverCompletionFunc
*cb
, void *opaque
)
562 IscsiLun
*iscsilun
= bs
->opaque
;
563 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
564 struct iscsi_data data
;
567 assert(req
== SG_IO
);
569 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
571 acb
->iscsilun
= iscsilun
;
576 acb
->task
= malloc(sizeof(struct scsi_task
));
577 if (acb
->task
== NULL
) {
578 error_report("iSCSI: Failed to allocate task for scsi command. %s",
579 iscsi_get_error(iscsi
));
580 qemu_aio_release(acb
);
583 memset(acb
->task
, 0, sizeof(struct scsi_task
));
585 switch (acb
->ioh
->dxfer_direction
) {
586 case SG_DXFER_TO_DEV
:
587 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
589 case SG_DXFER_FROM_DEV
:
590 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
593 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
597 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
598 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
599 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
601 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
602 data
.data
= acb
->ioh
->dxferp
;
603 data
.size
= acb
->ioh
->dxfer_len
;
605 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
607 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
610 scsi_free_scsi_task(acb
->task
);
611 qemu_aio_release(acb
);
615 /* tell libiscsi to read straight into the buffer we got from ioctl */
616 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
617 scsi_task_add_data_in_buffer(acb
->task
,
622 iscsi_set_events(iscsilun
);
627 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
629 IscsiLun
*iscsilun
= bs
->opaque
;
632 case SG_GET_VERSION_NUM
:
636 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
646 iscsi_getlength(BlockDriverState
*bs
)
648 IscsiLun
*iscsilun
= bs
->opaque
;
651 len
= iscsilun
->num_blocks
;
652 len
*= iscsilun
->block_size
;
658 iscsi_readcapacity16_cb(struct iscsi_context
*iscsi
, int status
,
659 void *command_data
, void *opaque
)
661 struct IscsiTask
*itask
= opaque
;
662 struct scsi_readcapacity16
*rc16
;
663 struct scsi_task
*task
= command_data
;
666 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
667 iscsi_get_error(iscsi
));
670 scsi_free_scsi_task(task
);
674 rc16
= scsi_datain_unmarshall(task
);
676 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
679 scsi_free_scsi_task(task
);
683 itask
->iscsilun
->block_size
= rc16
->block_length
;
684 itask
->iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
685 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
686 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
690 scsi_free_scsi_task(task
);
694 iscsi_readcapacity10_cb(struct iscsi_context
*iscsi
, int status
,
695 void *command_data
, void *opaque
)
697 struct IscsiTask
*itask
= opaque
;
698 struct scsi_readcapacity10
*rc10
;
699 struct scsi_task
*task
= command_data
;
702 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
703 iscsi_get_error(iscsi
));
706 scsi_free_scsi_task(task
);
710 rc10
= scsi_datain_unmarshall(task
);
712 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
715 scsi_free_scsi_task(task
);
719 itask
->iscsilun
->block_size
= rc10
->block_size
;
720 itask
->iscsilun
->num_blocks
= rc10
->lba
+ 1;
721 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
722 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
726 scsi_free_scsi_task(task
);
730 iscsi_inquiry_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
733 struct IscsiTask
*itask
= opaque
;
734 struct scsi_task
*task
= command_data
;
735 struct scsi_inquiry_standard
*inq
;
740 scsi_free_scsi_task(task
);
744 inq
= scsi_datain_unmarshall(task
);
746 error_report("iSCSI: Failed to unmarshall inquiry data.");
749 scsi_free_scsi_task(task
);
753 itask
->iscsilun
->type
= inq
->periperal_device_type
;
755 scsi_free_scsi_task(task
);
757 switch (itask
->iscsilun
->type
) {
759 task
= iscsi_readcapacity16_task(iscsi
, itask
->iscsilun
->lun
,
760 iscsi_readcapacity16_cb
, opaque
);
762 error_report("iSCSI: failed to send readcapacity16 command.");
769 task
= iscsi_readcapacity10_task(iscsi
, itask
->iscsilun
->lun
,
771 iscsi_readcapacity10_cb
, opaque
);
773 error_report("iSCSI: failed to send readcapacity16 command.");
786 iscsi_connect_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
789 struct IscsiTask
*itask
= opaque
;
790 struct scsi_task
*task
;
798 task
= iscsi_inquiry_task(iscsi
, itask
->iscsilun
->lun
,
800 iscsi_inquiry_cb
, opaque
);
802 error_report("iSCSI: failed to send inquiry command.");
809 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
813 const char *user
= NULL
;
814 const char *password
= NULL
;
816 list
= qemu_find_opts("iscsi");
821 opts
= qemu_opts_find(list
, target
);
823 opts
= QTAILQ_FIRST(&list
->head
);
829 user
= qemu_opt_get(opts
, "user");
834 password
= qemu_opt_get(opts
, "password");
836 error_report("CHAP username specified but no password was given");
840 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
841 error_report("Failed to set initiator username and password");
848 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
852 const char *digest
= NULL
;
854 list
= qemu_find_opts("iscsi");
859 opts
= qemu_opts_find(list
, target
);
861 opts
= QTAILQ_FIRST(&list
->head
);
867 digest
= qemu_opt_get(opts
, "header-digest");
872 if (!strcmp(digest
, "CRC32C")) {
873 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
874 } else if (!strcmp(digest
, "NONE")) {
875 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
876 } else if (!strcmp(digest
, "CRC32C-NONE")) {
877 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
878 } else if (!strcmp(digest
, "NONE-CRC32C")) {
879 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
881 error_report("Invalid header-digest setting : %s", digest
);
885 static char *parse_initiator_name(const char *target
)
889 const char *name
= NULL
;
890 const char *iscsi_name
= qemu_get_vm_name();
892 list
= qemu_find_opts("iscsi");
894 opts
= qemu_opts_find(list
, target
);
896 opts
= QTAILQ_FIRST(&list
->head
);
899 name
= qemu_opt_get(opts
, "initiator-name");
904 return g_strdup(name
);
906 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
907 iscsi_name
? ":" : "",
908 iscsi_name
? iscsi_name
: "");
913 * We support iscsi url's on the form
914 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
916 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
918 IscsiLun
*iscsilun
= bs
->opaque
;
919 struct iscsi_context
*iscsi
= NULL
;
920 struct iscsi_url
*iscsi_url
= NULL
;
921 struct IscsiTask task
;
922 char *initiator_name
= NULL
;
925 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
926 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
927 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
928 "of 512", BDRV_SECTOR_SIZE
);
932 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
933 if (iscsi_url
== NULL
) {
934 error_report("Failed to parse URL : %s %s", filename
,
935 iscsi_get_error(iscsi
));
940 memset(iscsilun
, 0, sizeof(IscsiLun
));
942 initiator_name
= parse_initiator_name(iscsi_url
->target
);
944 iscsi
= iscsi_create_context(initiator_name
);
946 error_report("iSCSI: Failed to create iSCSI context.");
951 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
952 error_report("iSCSI: Failed to set target name.");
957 if (iscsi_url
->user
!= NULL
) {
958 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
961 error_report("Failed to set initiator username and password");
967 /* check if we got CHAP username/password via the options */
968 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
969 error_report("iSCSI: Failed to set CHAP user/password");
974 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
975 error_report("iSCSI: Failed to set session type to normal.");
980 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
982 /* check if we got HEADER_DIGEST via the options */
983 parse_header_digest(iscsi
, iscsi_url
->target
);
985 task
.iscsilun
= iscsilun
;
990 iscsilun
->iscsi
= iscsi
;
991 iscsilun
->lun
= iscsi_url
->lun
;
993 if (iscsi_full_connect_async(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
,
994 iscsi_connect_cb
, &task
)
996 error_report("iSCSI: Failed to start async connect.");
1001 while (!task
.complete
) {
1002 iscsi_set_events(iscsilun
);
1005 if (task
.status
!= 0) {
1006 error_report("iSCSI: Failed to connect to LUN : %s",
1007 iscsi_get_error(iscsi
));
1012 /* Medium changer or tape. We dont have any emulation for this so this must
1013 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1014 * to read from the device to guess the image format.
1016 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1017 iscsilun
->type
== TYPE_TAPE
) {
1024 if (initiator_name
!= NULL
) {
1025 g_free(initiator_name
);
1027 if (iscsi_url
!= NULL
) {
1028 iscsi_destroy_url(iscsi_url
);
1032 if (iscsi
!= NULL
) {
1033 iscsi_destroy_context(iscsi
);
1035 memset(iscsilun
, 0, sizeof(IscsiLun
));
1040 static void iscsi_close(BlockDriverState
*bs
)
1042 IscsiLun
*iscsilun
= bs
->opaque
;
1043 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1045 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
1046 iscsi_destroy_context(iscsi
);
1047 memset(iscsilun
, 0, sizeof(IscsiLun
));
1050 static BlockDriver bdrv_iscsi
= {
1051 .format_name
= "iscsi",
1052 .protocol_name
= "iscsi",
1054 .instance_size
= sizeof(IscsiLun
),
1055 .bdrv_file_open
= iscsi_open
,
1056 .bdrv_close
= iscsi_close
,
1058 .bdrv_getlength
= iscsi_getlength
,
1060 .bdrv_aio_readv
= iscsi_aio_readv
,
1061 .bdrv_aio_writev
= iscsi_aio_writev
,
1062 .bdrv_aio_flush
= iscsi_aio_flush
,
1064 .bdrv_aio_discard
= iscsi_aio_discard
,
1067 .bdrv_ioctl
= iscsi_ioctl
,
1068 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1072 static void iscsi_block_init(void)
1074 bdrv_register(&bdrv_iscsi
);
1077 block_init(iscsi_block_init
);