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 "qemu-common.h"
29 #include "qemu-error.h"
30 #include "block_int.h"
33 #include <iscsi/iscsi.h>
34 #include <iscsi/scsi-lowlevel.h>
37 typedef struct IscsiLun
{
38 struct iscsi_context
*iscsi
;
41 unsigned long num_blocks
;
44 typedef struct IscsiAIOCB
{
45 BlockDriverAIOCB common
;
49 struct scsi_task
*task
;
65 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
71 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
73 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
74 IscsiLun
*iscsilun
= acb
->iscsilun
;
76 acb
->common
.cb(acb
->common
.opaque
, -ECANCELED
);
79 /* send a task mgmt call to the target to cancel the task on the target */
80 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
81 iscsi_abort_task_cb
, NULL
);
83 /* then also cancel the task locally in libiscsi */
84 iscsi_scsi_task_cancel(iscsilun
->iscsi
, acb
->task
);
87 static AIOPool iscsi_aio_pool
= {
88 .aiocb_size
= sizeof(IscsiAIOCB
),
89 .cancel
= iscsi_aio_cancel
,
93 static void iscsi_process_read(void *arg
);
94 static void iscsi_process_write(void *arg
);
96 static int iscsi_process_flush(void *arg
)
98 IscsiLun
*iscsilun
= arg
;
100 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
104 iscsi_set_events(IscsiLun
*iscsilun
)
106 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
108 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), iscsi_process_read
,
109 (iscsi_which_events(iscsi
) & POLLOUT
)
110 ? iscsi_process_write
: NULL
,
111 iscsi_process_flush
, iscsilun
);
115 iscsi_process_read(void *arg
)
117 IscsiLun
*iscsilun
= arg
;
118 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
120 iscsi_service(iscsi
, POLLIN
);
121 iscsi_set_events(iscsilun
);
125 iscsi_process_write(void *arg
)
127 IscsiLun
*iscsilun
= arg
;
128 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
130 iscsi_service(iscsi
, POLLOUT
);
131 iscsi_set_events(iscsilun
);
136 iscsi_schedule_bh(QEMUBHFunc
*cb
, IscsiAIOCB
*acb
)
138 acb
->bh
= qemu_bh_new(cb
, acb
);
140 error_report("oom: could not create iscsi bh");
144 qemu_bh_schedule(acb
->bh
);
149 iscsi_readv_writev_bh_cb(void *p
)
153 qemu_bh_delete(acb
->bh
);
155 if (acb
->canceled
== 0) {
156 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
159 qemu_aio_release(acb
);
164 iscsi_aio_write10_cb(struct iscsi_context
*iscsi
, int status
,
165 void *command_data
, void *opaque
)
167 IscsiAIOCB
*acb
= opaque
;
169 trace_iscsi_aio_write10_cb(iscsi
, status
, acb
, acb
->canceled
);
173 if (acb
->canceled
!= 0) {
174 qemu_aio_release(acb
);
175 scsi_free_scsi_task(acb
->task
);
182 error_report("Failed to write10 data to iSCSI lun. %s",
183 iscsi_get_error(iscsi
));
187 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
188 scsi_free_scsi_task(acb
->task
);
192 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
194 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
197 static BlockDriverAIOCB
*
198 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
199 QEMUIOVector
*qiov
, int nb_sectors
,
200 BlockDriverCompletionFunc
*cb
,
203 IscsiLun
*iscsilun
= bs
->opaque
;
204 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
209 /* set FUA on writes when cache mode is write through */
210 if (!(bs
->open_flags
& BDRV_O_CACHE_WB
)) {
214 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
215 trace_iscsi_aio_writev(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
217 acb
->iscsilun
= iscsilun
;
222 /* XXX we should pass the iovec to write10 to avoid the extra copy */
223 /* this will allow us to get rid of 'buf' completely */
224 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
225 acb
->buf
= g_malloc(size
);
226 qemu_iovec_to_buffer(acb
->qiov
, acb
->buf
);
227 acb
->task
= iscsi_write10_task(iscsi
, iscsilun
->lun
, acb
->buf
, size
,
228 sector_qemu2lun(sector_num
, iscsilun
),
229 fua
, 0, iscsilun
->block_size
,
230 iscsi_aio_write10_cb
, acb
);
231 if (acb
->task
== NULL
) {
232 error_report("iSCSI: Failed to send write10 command. %s",
233 iscsi_get_error(iscsi
));
235 qemu_aio_release(acb
);
239 iscsi_set_events(iscsilun
);
245 iscsi_aio_read10_cb(struct iscsi_context
*iscsi
, int status
,
246 void *command_data
, void *opaque
)
248 IscsiAIOCB
*acb
= opaque
;
250 trace_iscsi_aio_read10_cb(iscsi
, status
, acb
, acb
->canceled
);
252 if (acb
->canceled
!= 0) {
253 qemu_aio_release(acb
);
254 scsi_free_scsi_task(acb
->task
);
261 error_report("Failed to read10 data from iSCSI lun. %s",
262 iscsi_get_error(iscsi
));
266 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
267 scsi_free_scsi_task(acb
->task
);
271 static BlockDriverAIOCB
*
272 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
273 QEMUIOVector
*qiov
, int nb_sectors
,
274 BlockDriverCompletionFunc
*cb
,
277 IscsiLun
*iscsilun
= bs
->opaque
;
278 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
280 size_t qemu_read_size
, lun_read_size
;
283 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
285 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
286 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
288 acb
->iscsilun
= iscsilun
;
292 acb
->read_size
= qemu_read_size
;
295 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
296 * may be misaligned to the LUN, so we may need to read some extra
299 acb
->read_offset
= 0;
300 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
301 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
303 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
306 lun_read_size
= (qemu_read_size
+ iscsilun
->block_size
307 + acb
->read_offset
- 1)
308 / iscsilun
->block_size
* iscsilun
->block_size
;
309 acb
->task
= iscsi_read10_task(iscsi
, iscsilun
->lun
,
310 sector_qemu2lun(sector_num
, iscsilun
),
311 lun_read_size
, iscsilun
->block_size
,
312 iscsi_aio_read10_cb
, acb
);
313 if (acb
->task
== NULL
) {
314 error_report("iSCSI: Failed to send read10 command. %s",
315 iscsi_get_error(iscsi
));
316 qemu_aio_release(acb
);
320 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
321 scsi_task_add_data_in_buffer(acb
->task
,
322 acb
->qiov
->iov
[i
].iov_len
,
323 acb
->qiov
->iov
[i
].iov_base
);
326 iscsi_set_events(iscsilun
);
333 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
334 void *command_data
, void *opaque
)
336 IscsiAIOCB
*acb
= opaque
;
338 if (acb
->canceled
!= 0) {
339 qemu_aio_release(acb
);
340 scsi_free_scsi_task(acb
->task
);
347 error_report("Failed to sync10 data on iSCSI lun. %s",
348 iscsi_get_error(iscsi
));
352 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
353 scsi_free_scsi_task(acb
->task
);
357 static BlockDriverAIOCB
*
358 iscsi_aio_flush(BlockDriverState
*bs
,
359 BlockDriverCompletionFunc
*cb
, void *opaque
)
361 IscsiLun
*iscsilun
= bs
->opaque
;
362 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
365 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
367 acb
->iscsilun
= iscsilun
;
370 acb
->task
= iscsi_synchronizecache10_task(iscsi
, iscsilun
->lun
,
372 iscsi_synccache10_cb
,
374 if (acb
->task
== NULL
) {
375 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
376 iscsi_get_error(iscsi
));
377 qemu_aio_release(acb
);
381 iscsi_set_events(iscsilun
);
387 iscsi_getlength(BlockDriverState
*bs
)
389 IscsiLun
*iscsilun
= bs
->opaque
;
392 len
= iscsilun
->num_blocks
;
393 len
*= iscsilun
->block_size
;
399 iscsi_readcapacity10_cb(struct iscsi_context
*iscsi
, int status
,
400 void *command_data
, void *opaque
)
402 struct IscsiTask
*itask
= opaque
;
403 struct scsi_readcapacity10
*rc10
;
404 struct scsi_task
*task
= command_data
;
407 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
408 iscsi_get_error(iscsi
));
411 scsi_free_scsi_task(task
);
415 rc10
= scsi_datain_unmarshall(task
);
417 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
420 scsi_free_scsi_task(task
);
424 itask
->iscsilun
->block_size
= rc10
->block_size
;
425 itask
->iscsilun
->num_blocks
= rc10
->lba
;
426 itask
->bs
->total_sectors
= (uint64_t)rc10
->lba
*
427 rc10
->block_size
/ BDRV_SECTOR_SIZE
;
431 scsi_free_scsi_task(task
);
436 iscsi_connect_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
439 struct IscsiTask
*itask
= opaque
;
440 struct scsi_task
*task
;
448 task
= iscsi_readcapacity10_task(iscsi
, itask
->iscsilun
->lun
, 0, 0,
449 iscsi_readcapacity10_cb
, opaque
);
451 error_report("iSCSI: failed to send readcapacity command.");
458 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
462 const char *user
= NULL
;
463 const char *password
= NULL
;
465 list
= qemu_find_opts("iscsi");
470 opts
= qemu_opts_find(list
, target
);
472 opts
= QTAILQ_FIRST(&list
->head
);
478 user
= qemu_opt_get(opts
, "user");
483 password
= qemu_opt_get(opts
, "password");
485 error_report("CHAP username specified but no password was given");
489 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
490 error_report("Failed to set initiator username and password");
497 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
501 const char *digest
= NULL
;
503 list
= qemu_find_opts("iscsi");
508 opts
= qemu_opts_find(list
, target
);
510 opts
= QTAILQ_FIRST(&list
->head
);
516 digest
= qemu_opt_get(opts
, "header-digest");
521 if (!strcmp(digest
, "CRC32C")) {
522 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
523 } else if (!strcmp(digest
, "NONE")) {
524 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
525 } else if (!strcmp(digest
, "CRC32C-NONE")) {
526 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
527 } else if (!strcmp(digest
, "NONE-CRC32C")) {
528 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
530 error_report("Invalid header-digest setting : %s", digest
);
534 static char *parse_initiator_name(const char *target
)
538 const char *name
= NULL
;
540 list
= qemu_find_opts("iscsi");
542 return g_strdup("iqn.2008-11.org.linux-kvm");
545 opts
= qemu_opts_find(list
, target
);
547 opts
= QTAILQ_FIRST(&list
->head
);
549 return g_strdup("iqn.2008-11.org.linux-kvm");
553 name
= qemu_opt_get(opts
, "initiator-name");
555 return g_strdup("iqn.2008-11.org.linux-kvm");
558 return g_strdup(name
);
562 * We support iscsi url's on the form
563 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
565 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
567 IscsiLun
*iscsilun
= bs
->opaque
;
568 struct iscsi_context
*iscsi
= NULL
;
569 struct iscsi_url
*iscsi_url
= NULL
;
570 struct IscsiTask task
;
571 char *initiator_name
= NULL
;
574 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
575 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
576 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
577 "of 512", BDRV_SECTOR_SIZE
);
581 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
582 if (iscsi_url
== NULL
) {
583 error_report("Failed to parse URL : %s %s", filename
,
584 iscsi_get_error(iscsi
));
589 memset(iscsilun
, 0, sizeof(IscsiLun
));
591 initiator_name
= parse_initiator_name(iscsi_url
->target
);
593 iscsi
= iscsi_create_context(initiator_name
);
595 error_report("iSCSI: Failed to create iSCSI context.");
600 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
601 error_report("iSCSI: Failed to set target name.");
606 if (iscsi_url
->user
!= NULL
) {
607 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
610 error_report("Failed to set initiator username and password");
616 /* check if we got CHAP username/password via the options */
617 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
618 error_report("iSCSI: Failed to set CHAP user/password");
623 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
624 error_report("iSCSI: Failed to set session type to normal.");
629 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
631 /* check if we got HEADER_DIGEST via the options */
632 parse_header_digest(iscsi
, iscsi_url
->target
);
634 task
.iscsilun
= iscsilun
;
639 iscsilun
->iscsi
= iscsi
;
640 iscsilun
->lun
= iscsi_url
->lun
;
642 if (iscsi_full_connect_async(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
,
643 iscsi_connect_cb
, &task
)
645 error_report("iSCSI: Failed to start async connect.");
650 while (!task
.complete
) {
651 iscsi_set_events(iscsilun
);
654 if (task
.status
!= 0) {
655 error_report("iSCSI: Failed to connect to LUN : %s",
656 iscsi_get_error(iscsi
));
661 if (iscsi_url
!= NULL
) {
662 iscsi_destroy_url(iscsi_url
);
667 if (initiator_name
!= NULL
) {
668 g_free(initiator_name
);
670 if (iscsi_url
!= NULL
) {
671 iscsi_destroy_url(iscsi_url
);
674 iscsi_destroy_context(iscsi
);
676 memset(iscsilun
, 0, sizeof(IscsiLun
));
680 static void iscsi_close(BlockDriverState
*bs
)
682 IscsiLun
*iscsilun
= bs
->opaque
;
683 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
685 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
686 iscsi_destroy_context(iscsi
);
687 memset(iscsilun
, 0, sizeof(IscsiLun
));
690 static BlockDriver bdrv_iscsi
= {
691 .format_name
= "iscsi",
692 .protocol_name
= "iscsi",
694 .instance_size
= sizeof(IscsiLun
),
695 .bdrv_file_open
= iscsi_open
,
696 .bdrv_close
= iscsi_close
,
698 .bdrv_getlength
= iscsi_getlength
,
700 .bdrv_aio_readv
= iscsi_aio_readv
,
701 .bdrv_aio_writev
= iscsi_aio_writev
,
702 .bdrv_aio_flush
= iscsi_aio_flush
,
705 static void iscsi_block_init(void)
707 bdrv_register(&bdrv_iscsi
);
710 block_init(iscsi_block_init
);