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 "block/scsi.h"
37 #include <iscsi/iscsi.h>
38 #include <iscsi/scsi-lowlevel.h>
42 #include <block/scsi.h>
45 typedef struct IscsiLun
{
46 struct iscsi_context
*iscsi
;
48 enum scsi_inquiry_peripheral_device_type type
;
55 typedef struct IscsiAIOCB
{
56 BlockDriverAIOCB common
;
60 struct scsi_task
*task
;
72 #define NOP_INTERVAL 5000
73 #define MAX_NOP_FAILURES 3
74 #define ISCSI_CMD_RETRIES 5
81 qemu_bh_delete(acb
->bh
);
86 if (acb
->canceled
== 0) {
87 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
90 if (acb
->task
!= NULL
) {
91 scsi_free_scsi_task(acb
->task
);
95 qemu_aio_release(acb
);
99 iscsi_schedule_bh(IscsiAIOCB
*acb
)
104 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
105 qemu_bh_schedule(acb
->bh
);
110 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
113 IscsiAIOCB
*acb
= private_data
;
115 acb
->status
= -ECANCELED
;
116 iscsi_schedule_bh(acb
);
120 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
122 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
123 IscsiLun
*iscsilun
= acb
->iscsilun
;
125 if (acb
->status
!= -EINPROGRESS
) {
131 /* send a task mgmt call to the target to cancel the task on the target */
132 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
133 iscsi_abort_task_cb
, acb
);
135 while (acb
->status
== -EINPROGRESS
) {
140 static const AIOCBInfo iscsi_aiocb_info
= {
141 .aiocb_size
= sizeof(IscsiAIOCB
),
142 .cancel
= iscsi_aio_cancel
,
146 static void iscsi_process_read(void *arg
);
147 static void iscsi_process_write(void *arg
);
149 static int iscsi_process_flush(void *arg
)
151 IscsiLun
*iscsilun
= arg
;
153 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
157 iscsi_set_events(IscsiLun
*iscsilun
)
159 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
162 /* We always register a read handler. */
164 ev
|= iscsi_which_events(iscsi
);
165 if (ev
!= iscsilun
->events
) {
166 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
168 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
174 iscsilun
->events
= ev
;
178 iscsi_process_read(void *arg
)
180 IscsiLun
*iscsilun
= arg
;
181 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
183 iscsi_service(iscsi
, POLLIN
);
184 iscsi_set_events(iscsilun
);
188 iscsi_process_write(void *arg
)
190 IscsiLun
*iscsilun
= arg
;
191 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
193 iscsi_service(iscsi
, POLLOUT
);
194 iscsi_set_events(iscsilun
);
198 iscsi_aio_writev_acb(IscsiAIOCB
*acb
);
201 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
202 void *command_data
, void *opaque
)
204 IscsiAIOCB
*acb
= opaque
;
206 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
211 if (acb
->canceled
!= 0) {
217 if (status
== SCSI_STATUS_CHECK_CONDITION
218 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
219 && acb
->retries
-- > 0) {
220 scsi_free_scsi_task(acb
->task
);
222 if (iscsi_aio_writev_acb(acb
) == 0) {
223 iscsi_set_events(acb
->iscsilun
);
227 error_report("Failed to write16 data to iSCSI lun. %s",
228 iscsi_get_error(iscsi
));
232 iscsi_schedule_bh(acb
);
235 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
237 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
240 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
242 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
245 static bool is_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
248 if ((sector_num
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
||
249 (nb_sectors
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
) {
250 error_report("iSCSI misaligned request: "
251 "iscsilun->block_size %u, sector_num %" PRIi64
253 iscsilun
->block_size
, sector_num
, nb_sectors
);
260 iscsi_aio_writev_acb(IscsiAIOCB
*acb
)
262 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
264 uint32_t num_sectors
;
266 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
267 struct iscsi_data data
;
273 acb
->status
= -EINPROGRESS
;
276 /* this will allow us to get rid of 'buf' completely */
277 size
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
279 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
280 data
.size
= MIN(size
, acb
->qiov
->size
);
282 /* if the iovec only contains one buffer we can pass it directly */
283 if (acb
->qiov
->niov
== 1) {
284 data
.data
= acb
->qiov
->iov
[0].iov_base
;
286 acb
->buf
= g_malloc(data
.size
);
287 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, data
.size
);
288 data
.data
= acb
->buf
;
292 acb
->task
= malloc(sizeof(struct scsi_task
));
293 if (acb
->task
== NULL
) {
294 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
295 "command. %s", iscsi_get_error(iscsi
));
298 memset(acb
->task
, 0, sizeof(struct scsi_task
));
300 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
301 acb
->task
->cdb_size
= 16;
302 acb
->task
->cdb
[0] = 0x8a;
303 lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
304 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
305 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
306 num_sectors
= sector_qemu2lun(acb
->nb_sectors
, acb
->iscsilun
);
307 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
308 acb
->task
->expxferlen
= size
;
310 #if defined(LIBISCSI_FEATURE_IOVECTOR)
311 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
312 iscsi_aio_write16_cb
,
316 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
317 iscsi_aio_write16_cb
,
322 scsi_free_scsi_task(acb
->task
);
327 #if defined(LIBISCSI_FEATURE_IOVECTOR)
328 scsi_task_set_iov_out(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
334 static BlockDriverAIOCB
*
335 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
336 QEMUIOVector
*qiov
, int nb_sectors
,
337 BlockDriverCompletionFunc
*cb
,
340 IscsiLun
*iscsilun
= bs
->opaque
;
343 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
347 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
348 trace_iscsi_aio_writev(iscsilun
->iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
350 acb
->iscsilun
= iscsilun
;
352 acb
->nb_sectors
= nb_sectors
;
353 acb
->sector_num
= sector_num
;
354 acb
->retries
= ISCSI_CMD_RETRIES
;
356 if (iscsi_aio_writev_acb(acb
) != 0) {
357 qemu_aio_release(acb
);
361 iscsi_set_events(iscsilun
);
366 iscsi_aio_readv_acb(IscsiAIOCB
*acb
);
369 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
370 void *command_data
, void *opaque
)
372 IscsiAIOCB
*acb
= opaque
;
374 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
376 if (acb
->canceled
!= 0) {
382 if (status
== SCSI_STATUS_CHECK_CONDITION
383 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
384 && acb
->retries
-- > 0) {
385 scsi_free_scsi_task(acb
->task
);
387 if (iscsi_aio_readv_acb(acb
) == 0) {
388 iscsi_set_events(acb
->iscsilun
);
392 error_report("Failed to read16 data from iSCSI lun. %s",
393 iscsi_get_error(iscsi
));
397 iscsi_schedule_bh(acb
);
401 iscsi_aio_readv_acb(IscsiAIOCB
*acb
)
403 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
406 uint32_t num_sectors
;
408 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
414 acb
->status
= -EINPROGRESS
;
417 size
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
419 acb
->task
= malloc(sizeof(struct scsi_task
));
420 if (acb
->task
== NULL
) {
421 error_report("iSCSI: Failed to allocate task for scsi READ16 "
422 "command. %s", iscsi_get_error(iscsi
));
425 memset(acb
->task
, 0, sizeof(struct scsi_task
));
427 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
428 acb
->task
->expxferlen
= size
;
429 lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
430 num_sectors
= sector_qemu2lun(acb
->nb_sectors
, acb
->iscsilun
);
432 switch (acb
->iscsilun
->type
) {
434 acb
->task
->cdb_size
= 16;
435 acb
->task
->cdb
[0] = 0x88;
436 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
437 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
438 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
441 acb
->task
->cdb_size
= 10;
442 acb
->task
->cdb
[0] = 0x28;
443 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
444 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
448 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
453 scsi_free_scsi_task(acb
->task
);
457 #if defined(LIBISCSI_FEATURE_IOVECTOR)
458 scsi_task_set_iov_in(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
460 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
461 scsi_task_add_data_in_buffer(acb
->task
,
462 acb
->qiov
->iov
[i
].iov_len
,
463 acb
->qiov
->iov
[i
].iov_base
);
469 static BlockDriverAIOCB
*
470 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
471 QEMUIOVector
*qiov
, int nb_sectors
,
472 BlockDriverCompletionFunc
*cb
,
475 IscsiLun
*iscsilun
= bs
->opaque
;
478 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
482 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
483 trace_iscsi_aio_readv(iscsilun
->iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
485 acb
->nb_sectors
= nb_sectors
;
486 acb
->sector_num
= sector_num
;
487 acb
->iscsilun
= iscsilun
;
489 acb
->retries
= ISCSI_CMD_RETRIES
;
491 if (iscsi_aio_readv_acb(acb
) != 0) {
492 qemu_aio_release(acb
);
496 iscsi_set_events(iscsilun
);
501 iscsi_aio_flush_acb(IscsiAIOCB
*acb
);
504 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
505 void *command_data
, void *opaque
)
507 IscsiAIOCB
*acb
= opaque
;
509 if (acb
->canceled
!= 0) {
515 if (status
== SCSI_STATUS_CHECK_CONDITION
516 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
517 && acb
->retries
-- > 0) {
518 scsi_free_scsi_task(acb
->task
);
520 if (iscsi_aio_flush_acb(acb
) == 0) {
521 iscsi_set_events(acb
->iscsilun
);
525 error_report("Failed to sync10 data on iSCSI lun. %s",
526 iscsi_get_error(iscsi
));
530 iscsi_schedule_bh(acb
);
534 iscsi_aio_flush_acb(IscsiAIOCB
*acb
)
536 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
540 acb
->status
= -EINPROGRESS
;
543 acb
->task
= iscsi_synchronizecache10_task(iscsi
, acb
->iscsilun
->lun
,
545 iscsi_synccache10_cb
,
547 if (acb
->task
== NULL
) {
548 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
549 iscsi_get_error(iscsi
));
556 static BlockDriverAIOCB
*
557 iscsi_aio_flush(BlockDriverState
*bs
,
558 BlockDriverCompletionFunc
*cb
, void *opaque
)
560 IscsiLun
*iscsilun
= bs
->opaque
;
564 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
566 acb
->iscsilun
= iscsilun
;
567 acb
->retries
= ISCSI_CMD_RETRIES
;
569 if (iscsi_aio_flush_acb(acb
) != 0) {
570 qemu_aio_release(acb
);
574 iscsi_set_events(iscsilun
);
579 static int iscsi_aio_discard_acb(IscsiAIOCB
*acb
);
582 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
583 void *command_data
, void *opaque
)
585 IscsiAIOCB
*acb
= opaque
;
587 if (acb
->canceled
!= 0) {
593 if (status
== SCSI_STATUS_CHECK_CONDITION
594 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
595 && acb
->retries
-- > 0) {
596 scsi_free_scsi_task(acb
->task
);
598 if (iscsi_aio_discard_acb(acb
) == 0) {
599 iscsi_set_events(acb
->iscsilun
);
603 error_report("Failed to unmap data on iSCSI lun. %s",
604 iscsi_get_error(iscsi
));
608 iscsi_schedule_bh(acb
);
611 static int iscsi_aio_discard_acb(IscsiAIOCB
*acb
) {
612 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
613 struct unmap_list list
[1];
617 acb
->status
= -EINPROGRESS
;
620 list
[0].lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
621 list
[0].num
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
/ acb
->iscsilun
->block_size
;
623 acb
->task
= iscsi_unmap_task(iscsi
, acb
->iscsilun
->lun
,
627 if (acb
->task
== NULL
) {
628 error_report("iSCSI: Failed to send unmap command. %s",
629 iscsi_get_error(iscsi
));
636 static BlockDriverAIOCB
*
637 iscsi_aio_discard(BlockDriverState
*bs
,
638 int64_t sector_num
, int nb_sectors
,
639 BlockDriverCompletionFunc
*cb
, void *opaque
)
641 IscsiLun
*iscsilun
= bs
->opaque
;
644 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
646 acb
->iscsilun
= iscsilun
;
647 acb
->nb_sectors
= nb_sectors
;
648 acb
->sector_num
= sector_num
;
649 acb
->retries
= ISCSI_CMD_RETRIES
;
651 if (iscsi_aio_discard_acb(acb
) != 0) {
652 qemu_aio_release(acb
);
656 iscsi_set_events(iscsilun
);
663 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
664 void *command_data
, void *opaque
)
666 IscsiAIOCB
*acb
= opaque
;
671 if (acb
->canceled
!= 0) {
677 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
678 iscsi_get_error(iscsi
));
682 acb
->ioh
->driver_status
= 0;
683 acb
->ioh
->host_status
= 0;
686 #define SG_ERR_DRIVER_SENSE 0x08
688 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
691 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
693 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
694 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
695 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
696 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
699 iscsi_schedule_bh(acb
);
702 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
703 unsigned long int req
, void *buf
,
704 BlockDriverCompletionFunc
*cb
, void *opaque
)
706 IscsiLun
*iscsilun
= bs
->opaque
;
707 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
708 struct iscsi_data data
;
711 assert(req
== SG_IO
);
713 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
715 acb
->iscsilun
= iscsilun
;
718 acb
->status
= -EINPROGRESS
;
722 acb
->task
= malloc(sizeof(struct scsi_task
));
723 if (acb
->task
== NULL
) {
724 error_report("iSCSI: Failed to allocate task for scsi command. %s",
725 iscsi_get_error(iscsi
));
726 qemu_aio_release(acb
);
729 memset(acb
->task
, 0, sizeof(struct scsi_task
));
731 switch (acb
->ioh
->dxfer_direction
) {
732 case SG_DXFER_TO_DEV
:
733 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
735 case SG_DXFER_FROM_DEV
:
736 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
739 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
743 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
744 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
745 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
748 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
749 if (acb
->ioh
->iovec_count
== 0) {
750 data
.data
= acb
->ioh
->dxferp
;
751 data
.size
= acb
->ioh
->dxfer_len
;
753 #if defined(LIBISCSI_FEATURE_IOVECTOR)
754 scsi_task_set_iov_out(acb
->task
,
755 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
756 acb
->ioh
->iovec_count
);
758 struct iovec
*iov
= (struct iovec
*)acb
->ioh
->dxferp
;
760 acb
->buf
= g_malloc(acb
->ioh
->dxfer_len
);
761 data
.data
= acb
->buf
;
762 data
.size
= iov_to_buf(iov
, acb
->ioh
->iovec_count
, 0,
763 acb
->buf
, acb
->ioh
->dxfer_len
);
768 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
770 (data
.size
> 0) ? &data
: NULL
,
772 scsi_free_scsi_task(acb
->task
);
773 qemu_aio_release(acb
);
777 /* tell libiscsi to read straight into the buffer we got from ioctl */
778 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
779 if (acb
->ioh
->iovec_count
== 0) {
780 scsi_task_add_data_in_buffer(acb
->task
,
784 #if defined(LIBISCSI_FEATURE_IOVECTOR)
785 scsi_task_set_iov_in(acb
->task
,
786 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
787 acb
->ioh
->iovec_count
);
790 for (i
= 0; i
< acb
->ioh
->iovec_count
; i
++) {
791 struct iovec
*iov
= (struct iovec
*)acb
->ioh
->dxferp
;
793 scsi_task_add_data_in_buffer(acb
->task
,
801 iscsi_set_events(iscsilun
);
807 static void ioctl_cb(void *opaque
, int status
)
809 int *p_status
= opaque
;
813 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
815 IscsiLun
*iscsilun
= bs
->opaque
;
819 case SG_GET_VERSION_NUM
:
823 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
826 status
= -EINPROGRESS
;
827 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
829 while (status
== -EINPROGRESS
) {
842 iscsi_getlength(BlockDriverState
*bs
)
844 IscsiLun
*iscsilun
= bs
->opaque
;
847 len
= iscsilun
->num_blocks
;
848 len
*= iscsilun
->block_size
;
853 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
857 const char *user
= NULL
;
858 const char *password
= NULL
;
860 list
= qemu_find_opts("iscsi");
865 opts
= qemu_opts_find(list
, target
);
867 opts
= QTAILQ_FIRST(&list
->head
);
873 user
= qemu_opt_get(opts
, "user");
878 password
= qemu_opt_get(opts
, "password");
880 error_report("CHAP username specified but no password was given");
884 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
885 error_report("Failed to set initiator username and password");
892 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
896 const char *digest
= NULL
;
898 list
= qemu_find_opts("iscsi");
903 opts
= qemu_opts_find(list
, target
);
905 opts
= QTAILQ_FIRST(&list
->head
);
911 digest
= qemu_opt_get(opts
, "header-digest");
916 if (!strcmp(digest
, "CRC32C")) {
917 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
918 } else if (!strcmp(digest
, "NONE")) {
919 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
920 } else if (!strcmp(digest
, "CRC32C-NONE")) {
921 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
922 } else if (!strcmp(digest
, "NONE-CRC32C")) {
923 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
925 error_report("Invalid header-digest setting : %s", digest
);
929 static char *parse_initiator_name(const char *target
)
933 const char *name
= NULL
;
934 const char *iscsi_name
= qemu_get_vm_name();
936 list
= qemu_find_opts("iscsi");
938 opts
= qemu_opts_find(list
, target
);
940 opts
= QTAILQ_FIRST(&list
->head
);
943 name
= qemu_opt_get(opts
, "initiator-name");
948 return g_strdup(name
);
950 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
951 iscsi_name
? ":" : "",
952 iscsi_name
? iscsi_name
: "");
956 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
957 static void iscsi_nop_timed_event(void *opaque
)
959 IscsiLun
*iscsilun
= opaque
;
961 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) > MAX_NOP_FAILURES
) {
962 error_report("iSCSI: NOP timeout. Reconnecting...");
963 iscsi_reconnect(iscsilun
->iscsi
);
966 if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
967 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
971 qemu_mod_timer(iscsilun
->nop_timer
, qemu_get_clock_ms(rt_clock
) + NOP_INTERVAL
);
972 iscsi_set_events(iscsilun
);
976 static int iscsi_readcapacity_sync(IscsiLun
*iscsilun
)
978 struct scsi_task
*task
= NULL
;
979 struct scsi_readcapacity10
*rc10
= NULL
;
980 struct scsi_readcapacity16
*rc16
= NULL
;
982 int retries
= ISCSI_CMD_RETRIES
;
986 scsi_free_scsi_task(task
);
990 switch (iscsilun
->type
) {
992 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
993 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
994 rc16
= scsi_datain_unmarshall(task
);
996 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
999 iscsilun
->block_size
= rc16
->block_length
;
1000 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1005 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1006 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1007 rc10
= scsi_datain_unmarshall(task
);
1009 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
1012 iscsilun
->block_size
= rc10
->block_size
;
1013 if (rc10
->lba
== 0) {
1014 /* blank disk loaded */
1015 iscsilun
->num_blocks
= 0;
1017 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1025 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1026 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1029 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1030 error_report("iSCSI: failed to send readcapacity10 command.");
1034 scsi_free_scsi_task(task
);
1039 /* TODO Convert to fine grained options */
1040 static QemuOptsList runtime_opts
= {
1042 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1046 .type
= QEMU_OPT_STRING
,
1047 .help
= "URL to the iscsi image",
1049 { /* end of list */ }
1054 * We support iscsi url's on the form
1055 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1057 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1059 IscsiLun
*iscsilun
= bs
->opaque
;
1060 struct iscsi_context
*iscsi
= NULL
;
1061 struct iscsi_url
*iscsi_url
= NULL
;
1062 struct scsi_task
*task
= NULL
;
1063 struct scsi_inquiry_standard
*inq
= NULL
;
1064 char *initiator_name
= NULL
;
1066 Error
*local_err
= NULL
;
1067 const char *filename
;
1070 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
1071 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1072 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1073 "of 512", BDRV_SECTOR_SIZE
);
1077 opts
= qemu_opts_create_nofail(&runtime_opts
);
1078 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1079 if (error_is_set(&local_err
)) {
1080 qerror_report_err(local_err
);
1081 error_free(local_err
);
1086 filename
= qemu_opt_get(opts
, "filename");
1089 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1090 if (iscsi_url
== NULL
) {
1091 error_report("Failed to parse URL : %s", filename
);
1096 memset(iscsilun
, 0, sizeof(IscsiLun
));
1098 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1100 iscsi
= iscsi_create_context(initiator_name
);
1101 if (iscsi
== NULL
) {
1102 error_report("iSCSI: Failed to create iSCSI context.");
1107 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1108 error_report("iSCSI: Failed to set target name.");
1113 if (iscsi_url
->user
!= NULL
) {
1114 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1117 error_report("Failed to set initiator username and password");
1123 /* check if we got CHAP username/password via the options */
1124 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
1125 error_report("iSCSI: Failed to set CHAP user/password");
1130 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1131 error_report("iSCSI: Failed to set session type to normal.");
1136 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1138 /* check if we got HEADER_DIGEST via the options */
1139 parse_header_digest(iscsi
, iscsi_url
->target
);
1141 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1142 error_report("iSCSI: Failed to connect to LUN : %s",
1143 iscsi_get_error(iscsi
));
1148 iscsilun
->iscsi
= iscsi
;
1149 iscsilun
->lun
= iscsi_url
->lun
;
1151 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
1153 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1154 error_report("iSCSI: failed to send inquiry command.");
1159 inq
= scsi_datain_unmarshall(task
);
1161 error_report("iSCSI: Failed to unmarshall inquiry data.");
1166 iscsilun
->type
= inq
->periperal_device_type
;
1168 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1171 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1173 /* Medium changer or tape. We dont have any emulation for this so this must
1174 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1175 * to read from the device to guess the image format.
1177 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1178 iscsilun
->type
== TYPE_TAPE
) {
1182 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1183 /* Set up a timer for sending out iSCSI NOPs */
1184 iscsilun
->nop_timer
= qemu_new_timer_ms(rt_clock
, iscsi_nop_timed_event
, iscsilun
);
1185 qemu_mod_timer(iscsilun
->nop_timer
, qemu_get_clock_ms(rt_clock
) + NOP_INTERVAL
);
1189 qemu_opts_del(opts
);
1190 if (initiator_name
!= NULL
) {
1191 g_free(initiator_name
);
1193 if (iscsi_url
!= NULL
) {
1194 iscsi_destroy_url(iscsi_url
);
1197 scsi_free_scsi_task(task
);
1201 if (iscsi
!= NULL
) {
1202 iscsi_destroy_context(iscsi
);
1204 memset(iscsilun
, 0, sizeof(IscsiLun
));
1209 static void iscsi_close(BlockDriverState
*bs
)
1211 IscsiLun
*iscsilun
= bs
->opaque
;
1212 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1214 if (iscsilun
->nop_timer
) {
1215 qemu_del_timer(iscsilun
->nop_timer
);
1216 qemu_free_timer(iscsilun
->nop_timer
);
1218 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
1219 iscsi_destroy_context(iscsi
);
1220 memset(iscsilun
, 0, sizeof(IscsiLun
));
1223 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1225 IscsiLun
*iscsilun
= bs
->opaque
;
1228 if (iscsilun
->type
!= TYPE_DISK
) {
1232 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1236 if (offset
> iscsi_getlength(bs
)) {
1243 static int iscsi_has_zero_init(BlockDriverState
*bs
)
1248 static int iscsi_create(const char *filename
, QEMUOptionParameter
*options
)
1251 int64_t total_size
= 0;
1252 BlockDriverState bs
;
1253 IscsiLun
*iscsilun
= NULL
;
1256 memset(&bs
, 0, sizeof(BlockDriverState
));
1258 /* Read out options */
1259 while (options
&& options
->name
) {
1260 if (!strcmp(options
->name
, "size")) {
1261 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1266 bs
.opaque
= g_malloc0(sizeof(struct IscsiLun
));
1267 iscsilun
= bs
.opaque
;
1269 bs_options
= qdict_new();
1270 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1271 ret
= iscsi_open(&bs
, bs_options
, 0);
1272 QDECREF(bs_options
);
1277 if (iscsilun
->nop_timer
) {
1278 qemu_del_timer(iscsilun
->nop_timer
);
1279 qemu_free_timer(iscsilun
->nop_timer
);
1281 if (iscsilun
->type
!= TYPE_DISK
) {
1285 if (bs
.total_sectors
< total_size
) {
1292 if (iscsilun
->iscsi
!= NULL
) {
1293 iscsi_destroy_context(iscsilun
->iscsi
);
1299 static QEMUOptionParameter iscsi_create_options
[] = {
1301 .name
= BLOCK_OPT_SIZE
,
1303 .help
= "Virtual disk size"
1308 static BlockDriver bdrv_iscsi
= {
1309 .format_name
= "iscsi",
1310 .protocol_name
= "iscsi",
1312 .instance_size
= sizeof(IscsiLun
),
1313 .bdrv_file_open
= iscsi_open
,
1314 .bdrv_close
= iscsi_close
,
1315 .bdrv_create
= iscsi_create
,
1316 .create_options
= iscsi_create_options
,
1318 .bdrv_getlength
= iscsi_getlength
,
1319 .bdrv_truncate
= iscsi_truncate
,
1321 .bdrv_aio_readv
= iscsi_aio_readv
,
1322 .bdrv_aio_writev
= iscsi_aio_writev
,
1323 .bdrv_aio_flush
= iscsi_aio_flush
,
1325 .bdrv_aio_discard
= iscsi_aio_discard
,
1326 .bdrv_has_zero_init
= iscsi_has_zero_init
,
1329 .bdrv_ioctl
= iscsi_ioctl
,
1330 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1334 static QemuOptsList qemu_iscsi_opts
= {
1336 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1340 .type
= QEMU_OPT_STRING
,
1341 .help
= "username for CHAP authentication to target",
1344 .type
= QEMU_OPT_STRING
,
1345 .help
= "password for CHAP authentication to target",
1347 .name
= "header-digest",
1348 .type
= QEMU_OPT_STRING
,
1349 .help
= "HeaderDigest setting. "
1350 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1352 .name
= "initiator-name",
1353 .type
= QEMU_OPT_STRING
,
1354 .help
= "Initiator iqn name to use when connecting",
1356 { /* end of list */ }
1360 static void iscsi_block_init(void)
1362 bdrv_register(&bdrv_iscsi
);
1363 qemu_add_opts(&qemu_iscsi_opts
);
1366 block_init(iscsi_block_init
);