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
;
53 typedef struct IscsiAIOCB
{
54 BlockDriverAIOCB common
;
58 struct scsi_task
*task
;
74 qemu_bh_delete(acb
->bh
);
76 if (acb
->canceled
== 0) {
77 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
80 if (acb
->task
!= NULL
) {
81 scsi_free_scsi_task(acb
->task
);
85 qemu_aio_release(acb
);
89 iscsi_schedule_bh(IscsiAIOCB
*acb
)
94 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
95 qemu_bh_schedule(acb
->bh
);
100 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
103 IscsiAIOCB
*acb
= private_data
;
105 acb
->status
= -ECANCELED
;
106 iscsi_schedule_bh(acb
);
110 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
112 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
113 IscsiLun
*iscsilun
= acb
->iscsilun
;
115 if (acb
->status
!= -EINPROGRESS
) {
121 /* send a task mgmt call to the target to cancel the task on the target */
122 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
123 iscsi_abort_task_cb
, acb
);
125 while (acb
->status
== -EINPROGRESS
) {
130 static const AIOCBInfo iscsi_aiocb_info
= {
131 .aiocb_size
= sizeof(IscsiAIOCB
),
132 .cancel
= iscsi_aio_cancel
,
136 static void iscsi_process_read(void *arg
);
137 static void iscsi_process_write(void *arg
);
139 static int iscsi_process_flush(void *arg
)
141 IscsiLun
*iscsilun
= arg
;
143 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
147 iscsi_set_events(IscsiLun
*iscsilun
)
149 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
152 /* We always register a read handler. */
154 ev
|= iscsi_which_events(iscsi
);
155 if (ev
!= iscsilun
->events
) {
156 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
158 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
164 iscsilun
->events
= ev
;
168 iscsi_process_read(void *arg
)
170 IscsiLun
*iscsilun
= arg
;
171 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
173 iscsi_service(iscsi
, POLLIN
);
174 iscsi_set_events(iscsilun
);
178 iscsi_process_write(void *arg
)
180 IscsiLun
*iscsilun
= arg
;
181 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
183 iscsi_service(iscsi
, POLLOUT
);
184 iscsi_set_events(iscsilun
);
189 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
190 void *command_data
, void *opaque
)
192 IscsiAIOCB
*acb
= opaque
;
194 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
198 if (acb
->canceled
!= 0) {
204 error_report("Failed to write16 data to iSCSI lun. %s",
205 iscsi_get_error(iscsi
));
209 iscsi_schedule_bh(acb
);
212 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
214 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
217 static BlockDriverAIOCB
*
218 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
219 QEMUIOVector
*qiov
, int nb_sectors
,
220 BlockDriverCompletionFunc
*cb
,
223 IscsiLun
*iscsilun
= bs
->opaque
;
224 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
227 uint32_t num_sectors
;
229 struct iscsi_data data
;
231 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
232 trace_iscsi_aio_writev(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
234 acb
->iscsilun
= iscsilun
;
239 acb
->status
= -EINPROGRESS
;
241 /* XXX we should pass the iovec to write16 to avoid the extra copy */
242 /* this will allow us to get rid of 'buf' completely */
243 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
244 acb
->buf
= g_malloc(size
);
245 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, size
);
247 acb
->task
= malloc(sizeof(struct scsi_task
));
248 if (acb
->task
== NULL
) {
249 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
250 "command. %s", iscsi_get_error(iscsi
));
251 qemu_aio_release(acb
);
254 memset(acb
->task
, 0, sizeof(struct scsi_task
));
256 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
257 acb
->task
->cdb_size
= 16;
258 acb
->task
->cdb
[0] = 0x8a;
259 lba
= sector_qemu2lun(sector_num
, iscsilun
);
260 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
261 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
262 num_sectors
= size
/ iscsilun
->block_size
;
263 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
264 acb
->task
->expxferlen
= size
;
266 data
.data
= acb
->buf
;
269 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
270 iscsi_aio_write16_cb
,
273 scsi_free_scsi_task(acb
->task
);
275 qemu_aio_release(acb
);
279 iscsi_set_events(iscsilun
);
285 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
286 void *command_data
, void *opaque
)
288 IscsiAIOCB
*acb
= opaque
;
290 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
292 if (acb
->canceled
!= 0) {
298 error_report("Failed to read16 data from iSCSI lun. %s",
299 iscsi_get_error(iscsi
));
303 iscsi_schedule_bh(acb
);
306 static BlockDriverAIOCB
*
307 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
308 QEMUIOVector
*qiov
, int nb_sectors
,
309 BlockDriverCompletionFunc
*cb
,
312 IscsiLun
*iscsilun
= bs
->opaque
;
313 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
315 size_t qemu_read_size
;
318 uint32_t num_sectors
;
320 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
322 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
323 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
325 acb
->iscsilun
= iscsilun
;
330 acb
->status
= -EINPROGRESS
;
331 acb
->read_size
= qemu_read_size
;
334 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
335 * may be misaligned to the LUN, so we may need to read some extra
338 acb
->read_offset
= 0;
339 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
340 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
342 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
345 num_sectors
= (qemu_read_size
+ iscsilun
->block_size
346 + acb
->read_offset
- 1)
347 / iscsilun
->block_size
;
349 acb
->task
= malloc(sizeof(struct scsi_task
));
350 if (acb
->task
== NULL
) {
351 error_report("iSCSI: Failed to allocate task for scsi READ16 "
352 "command. %s", iscsi_get_error(iscsi
));
353 qemu_aio_release(acb
);
356 memset(acb
->task
, 0, sizeof(struct scsi_task
));
358 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
359 lba
= sector_qemu2lun(sector_num
, iscsilun
);
360 acb
->task
->expxferlen
= qemu_read_size
;
362 switch (iscsilun
->type
) {
364 acb
->task
->cdb_size
= 16;
365 acb
->task
->cdb
[0] = 0x88;
366 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
367 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
368 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
371 acb
->task
->cdb_size
= 10;
372 acb
->task
->cdb
[0] = 0x28;
373 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
374 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
378 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
382 scsi_free_scsi_task(acb
->task
);
383 qemu_aio_release(acb
);
387 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
388 scsi_task_add_data_in_buffer(acb
->task
,
389 acb
->qiov
->iov
[i
].iov_len
,
390 acb
->qiov
->iov
[i
].iov_base
);
393 iscsi_set_events(iscsilun
);
400 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
401 void *command_data
, void *opaque
)
403 IscsiAIOCB
*acb
= opaque
;
405 if (acb
->canceled
!= 0) {
411 error_report("Failed to sync10 data on iSCSI lun. %s",
412 iscsi_get_error(iscsi
));
416 iscsi_schedule_bh(acb
);
419 static BlockDriverAIOCB
*
420 iscsi_aio_flush(BlockDriverState
*bs
,
421 BlockDriverCompletionFunc
*cb
, void *opaque
)
423 IscsiLun
*iscsilun
= bs
->opaque
;
424 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
427 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
429 acb
->iscsilun
= iscsilun
;
432 acb
->status
= -EINPROGRESS
;
434 acb
->task
= iscsi_synchronizecache10_task(iscsi
, iscsilun
->lun
,
436 iscsi_synccache10_cb
,
438 if (acb
->task
== NULL
) {
439 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
440 iscsi_get_error(iscsi
));
441 qemu_aio_release(acb
);
445 iscsi_set_events(iscsilun
);
451 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
452 void *command_data
, void *opaque
)
454 IscsiAIOCB
*acb
= opaque
;
456 if (acb
->canceled
!= 0) {
462 error_report("Failed to unmap data on iSCSI lun. %s",
463 iscsi_get_error(iscsi
));
467 iscsi_schedule_bh(acb
);
470 static BlockDriverAIOCB
*
471 iscsi_aio_discard(BlockDriverState
*bs
,
472 int64_t sector_num
, int nb_sectors
,
473 BlockDriverCompletionFunc
*cb
, void *opaque
)
475 IscsiLun
*iscsilun
= bs
->opaque
;
476 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
478 struct unmap_list list
[1];
480 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
482 acb
->iscsilun
= iscsilun
;
485 acb
->status
= -EINPROGRESS
;
487 list
[0].lba
= sector_qemu2lun(sector_num
, iscsilun
);
488 list
[0].num
= nb_sectors
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
490 acb
->task
= iscsi_unmap_task(iscsi
, iscsilun
->lun
,
494 if (acb
->task
== NULL
) {
495 error_report("iSCSI: Failed to send unmap command. %s",
496 iscsi_get_error(iscsi
));
497 qemu_aio_release(acb
);
501 iscsi_set_events(iscsilun
);
508 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
509 void *command_data
, void *opaque
)
511 IscsiAIOCB
*acb
= opaque
;
513 if (acb
->canceled
!= 0) {
519 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
520 iscsi_get_error(iscsi
));
524 acb
->ioh
->driver_status
= 0;
525 acb
->ioh
->host_status
= 0;
528 #define SG_ERR_DRIVER_SENSE 0x08
530 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
533 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
535 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
536 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
537 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
538 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
541 iscsi_schedule_bh(acb
);
544 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
545 unsigned long int req
, void *buf
,
546 BlockDriverCompletionFunc
*cb
, void *opaque
)
548 IscsiLun
*iscsilun
= bs
->opaque
;
549 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
550 struct iscsi_data data
;
553 assert(req
== SG_IO
);
555 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
557 acb
->iscsilun
= iscsilun
;
560 acb
->status
= -EINPROGRESS
;
564 acb
->task
= malloc(sizeof(struct scsi_task
));
565 if (acb
->task
== NULL
) {
566 error_report("iSCSI: Failed to allocate task for scsi command. %s",
567 iscsi_get_error(iscsi
));
568 qemu_aio_release(acb
);
571 memset(acb
->task
, 0, sizeof(struct scsi_task
));
573 switch (acb
->ioh
->dxfer_direction
) {
574 case SG_DXFER_TO_DEV
:
575 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
577 case SG_DXFER_FROM_DEV
:
578 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
581 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
585 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
586 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
587 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
589 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
590 data
.data
= acb
->ioh
->dxferp
;
591 data
.size
= acb
->ioh
->dxfer_len
;
593 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
595 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
598 scsi_free_scsi_task(acb
->task
);
599 qemu_aio_release(acb
);
603 /* tell libiscsi to read straight into the buffer we got from ioctl */
604 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
605 scsi_task_add_data_in_buffer(acb
->task
,
610 iscsi_set_events(iscsilun
);
616 static void ioctl_cb(void *opaque
, int status
)
618 int *p_status
= opaque
;
622 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
624 IscsiLun
*iscsilun
= bs
->opaque
;
628 case SG_GET_VERSION_NUM
:
632 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
635 status
= -EINPROGRESS
;
636 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
638 while (status
== -EINPROGRESS
) {
651 iscsi_getlength(BlockDriverState
*bs
)
653 IscsiLun
*iscsilun
= bs
->opaque
;
656 len
= iscsilun
->num_blocks
;
657 len
*= iscsilun
->block_size
;
662 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
666 const char *user
= NULL
;
667 const char *password
= NULL
;
669 list
= qemu_find_opts("iscsi");
674 opts
= qemu_opts_find(list
, target
);
676 opts
= QTAILQ_FIRST(&list
->head
);
682 user
= qemu_opt_get(opts
, "user");
687 password
= qemu_opt_get(opts
, "password");
689 error_report("CHAP username specified but no password was given");
693 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
694 error_report("Failed to set initiator username and password");
701 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
705 const char *digest
= NULL
;
707 list
= qemu_find_opts("iscsi");
712 opts
= qemu_opts_find(list
, target
);
714 opts
= QTAILQ_FIRST(&list
->head
);
720 digest
= qemu_opt_get(opts
, "header-digest");
725 if (!strcmp(digest
, "CRC32C")) {
726 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
727 } else if (!strcmp(digest
, "NONE")) {
728 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
729 } else if (!strcmp(digest
, "CRC32C-NONE")) {
730 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
731 } else if (!strcmp(digest
, "NONE-CRC32C")) {
732 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
734 error_report("Invalid header-digest setting : %s", digest
);
738 static char *parse_initiator_name(const char *target
)
742 const char *name
= NULL
;
743 const char *iscsi_name
= qemu_get_vm_name();
745 list
= qemu_find_opts("iscsi");
747 opts
= qemu_opts_find(list
, target
);
749 opts
= QTAILQ_FIRST(&list
->head
);
752 name
= qemu_opt_get(opts
, "initiator-name");
757 return g_strdup(name
);
759 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
760 iscsi_name
? ":" : "",
761 iscsi_name
? iscsi_name
: "");
766 * We support iscsi url's on the form
767 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
769 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
771 IscsiLun
*iscsilun
= bs
->opaque
;
772 struct iscsi_context
*iscsi
= NULL
;
773 struct iscsi_url
*iscsi_url
= NULL
;
774 struct scsi_task
*task
= NULL
;
775 struct scsi_inquiry_standard
*inq
= NULL
;
776 struct scsi_readcapacity10
*rc10
= NULL
;
777 struct scsi_readcapacity16
*rc16
= NULL
;
778 char *initiator_name
= NULL
;
781 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
782 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
783 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
784 "of 512", BDRV_SECTOR_SIZE
);
788 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
789 if (iscsi_url
== NULL
) {
790 error_report("Failed to parse URL : %s", filename
);
795 memset(iscsilun
, 0, sizeof(IscsiLun
));
797 initiator_name
= parse_initiator_name(iscsi_url
->target
);
799 iscsi
= iscsi_create_context(initiator_name
);
801 error_report("iSCSI: Failed to create iSCSI context.");
806 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
807 error_report("iSCSI: Failed to set target name.");
812 if (iscsi_url
->user
!= NULL
) {
813 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
816 error_report("Failed to set initiator username and password");
822 /* check if we got CHAP username/password via the options */
823 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
824 error_report("iSCSI: Failed to set CHAP user/password");
829 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
830 error_report("iSCSI: Failed to set session type to normal.");
835 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
837 /* check if we got HEADER_DIGEST via the options */
838 parse_header_digest(iscsi
, iscsi_url
->target
);
840 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
841 error_report("iSCSI: Failed to connect to LUN : %s",
842 iscsi_get_error(iscsi
));
847 iscsilun
->iscsi
= iscsi
;
848 iscsilun
->lun
= iscsi_url
->lun
;
850 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
852 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
853 error_report("iSCSI: failed to send inquiry command.");
858 inq
= scsi_datain_unmarshall(task
);
860 error_report("iSCSI: Failed to unmarshall inquiry data.");
865 iscsilun
->type
= inq
->periperal_device_type
;
867 scsi_free_scsi_task(task
);
869 switch (iscsilun
->type
) {
871 task
= iscsi_readcapacity16_sync(iscsi
, iscsilun
->lun
);
872 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
873 error_report("iSCSI: failed to send readcapacity16 command.");
877 rc16
= scsi_datain_unmarshall(task
);
879 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
883 iscsilun
->block_size
= rc16
->block_length
;
884 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
887 task
= iscsi_readcapacity10_sync(iscsi
, iscsilun
->lun
, 0, 0);
888 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
889 error_report("iSCSI: failed to send readcapacity10 command.");
893 rc10
= scsi_datain_unmarshall(task
);
895 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
899 iscsilun
->block_size
= rc10
->block_size
;
900 if (rc10
->lba
== 0) {
901 /* blank disk loaded */
902 iscsilun
->num_blocks
= 0;
904 iscsilun
->num_blocks
= rc10
->lba
+ 1;
911 bs
->total_sectors
= iscsilun
->num_blocks
*
912 iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
914 /* Medium changer or tape. We dont have any emulation for this so this must
915 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
916 * to read from the device to guess the image format.
918 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
919 iscsilun
->type
== TYPE_TAPE
) {
926 if (initiator_name
!= NULL
) {
927 g_free(initiator_name
);
929 if (iscsi_url
!= NULL
) {
930 iscsi_destroy_url(iscsi_url
);
933 scsi_free_scsi_task(task
);
938 iscsi_destroy_context(iscsi
);
940 memset(iscsilun
, 0, sizeof(IscsiLun
));
945 static void iscsi_close(BlockDriverState
*bs
)
947 IscsiLun
*iscsilun
= bs
->opaque
;
948 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
950 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
951 iscsi_destroy_context(iscsi
);
952 memset(iscsilun
, 0, sizeof(IscsiLun
));
955 static int iscsi_has_zero_init(BlockDriverState
*bs
)
960 static BlockDriver bdrv_iscsi
= {
961 .format_name
= "iscsi",
962 .protocol_name
= "iscsi",
964 .instance_size
= sizeof(IscsiLun
),
965 .bdrv_file_open
= iscsi_open
,
966 .bdrv_close
= iscsi_close
,
968 .bdrv_getlength
= iscsi_getlength
,
970 .bdrv_aio_readv
= iscsi_aio_readv
,
971 .bdrv_aio_writev
= iscsi_aio_writev
,
972 .bdrv_aio_flush
= iscsi_aio_flush
,
974 .bdrv_aio_discard
= iscsi_aio_discard
,
975 .bdrv_has_zero_init
= iscsi_has_zero_init
,
978 .bdrv_ioctl
= iscsi_ioctl
,
979 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
983 static void iscsi_block_init(void)
985 bdrv_register(&bdrv_iscsi
);
988 block_init(iscsi_block_init
);