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
;
73 qemu_bh_delete(acb
->bh
);
75 if (acb
->canceled
== 0) {
76 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
79 if (acb
->task
!= NULL
) {
80 scsi_free_scsi_task(acb
->task
);
84 qemu_aio_release(acb
);
88 iscsi_schedule_bh(IscsiAIOCB
*acb
)
93 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
94 qemu_bh_schedule(acb
->bh
);
99 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
102 IscsiAIOCB
*acb
= private_data
;
104 acb
->status
= -ECANCELED
;
105 iscsi_schedule_bh(acb
);
109 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
111 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
112 IscsiLun
*iscsilun
= acb
->iscsilun
;
114 if (acb
->status
!= -EINPROGRESS
) {
120 /* send a task mgmt call to the target to cancel the task on the target */
121 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
122 iscsi_abort_task_cb
, acb
);
124 while (acb
->status
== -EINPROGRESS
) {
129 static const AIOCBInfo iscsi_aiocb_info
= {
130 .aiocb_size
= sizeof(IscsiAIOCB
),
131 .cancel
= iscsi_aio_cancel
,
135 static void iscsi_process_read(void *arg
);
136 static void iscsi_process_write(void *arg
);
138 static int iscsi_process_flush(void *arg
)
140 IscsiLun
*iscsilun
= arg
;
142 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
146 iscsi_set_events(IscsiLun
*iscsilun
)
148 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
151 /* We always register a read handler. */
153 ev
|= iscsi_which_events(iscsi
);
154 if (ev
!= iscsilun
->events
) {
155 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
157 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
163 iscsilun
->events
= ev
;
167 iscsi_process_read(void *arg
)
169 IscsiLun
*iscsilun
= arg
;
170 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
172 iscsi_service(iscsi
, POLLIN
);
173 iscsi_set_events(iscsilun
);
177 iscsi_process_write(void *arg
)
179 IscsiLun
*iscsilun
= arg
;
180 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
182 iscsi_service(iscsi
, POLLOUT
);
183 iscsi_set_events(iscsilun
);
188 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
189 void *command_data
, void *opaque
)
191 IscsiAIOCB
*acb
= opaque
;
193 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
197 if (acb
->canceled
!= 0) {
203 error_report("Failed to write16 data to iSCSI lun. %s",
204 iscsi_get_error(iscsi
));
208 iscsi_schedule_bh(acb
);
211 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
213 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
216 static BlockDriverAIOCB
*
217 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
218 QEMUIOVector
*qiov
, int nb_sectors
,
219 BlockDriverCompletionFunc
*cb
,
222 IscsiLun
*iscsilun
= bs
->opaque
;
223 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
226 uint32_t num_sectors
;
228 struct iscsi_data data
;
230 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
231 trace_iscsi_aio_writev(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
233 acb
->iscsilun
= iscsilun
;
238 acb
->status
= -EINPROGRESS
;
240 /* XXX we should pass the iovec to write16 to avoid the extra copy */
241 /* this will allow us to get rid of 'buf' completely */
242 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
243 acb
->buf
= g_malloc(size
);
244 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, size
);
246 acb
->task
= malloc(sizeof(struct scsi_task
));
247 if (acb
->task
== NULL
) {
248 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
249 "command. %s", iscsi_get_error(iscsi
));
250 qemu_aio_release(acb
);
253 memset(acb
->task
, 0, sizeof(struct scsi_task
));
255 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
256 acb
->task
->cdb_size
= 16;
257 acb
->task
->cdb
[0] = 0x8a;
258 lba
= sector_qemu2lun(sector_num
, iscsilun
);
259 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
260 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
261 num_sectors
= size
/ iscsilun
->block_size
;
262 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
263 acb
->task
->expxferlen
= size
;
265 data
.data
= acb
->buf
;
268 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
269 iscsi_aio_write16_cb
,
272 scsi_free_scsi_task(acb
->task
);
274 qemu_aio_release(acb
);
278 iscsi_set_events(iscsilun
);
284 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
285 void *command_data
, void *opaque
)
287 IscsiAIOCB
*acb
= opaque
;
289 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
291 if (acb
->canceled
!= 0) {
297 error_report("Failed to read16 data from iSCSI lun. %s",
298 iscsi_get_error(iscsi
));
302 iscsi_schedule_bh(acb
);
305 static BlockDriverAIOCB
*
306 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
307 QEMUIOVector
*qiov
, int nb_sectors
,
308 BlockDriverCompletionFunc
*cb
,
311 IscsiLun
*iscsilun
= bs
->opaque
;
312 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
314 size_t qemu_read_size
;
317 uint32_t num_sectors
;
319 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
321 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
322 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
324 acb
->iscsilun
= iscsilun
;
329 acb
->status
= -EINPROGRESS
;
330 acb
->read_size
= qemu_read_size
;
333 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
334 * may be misaligned to the LUN, so we may need to read some extra
337 acb
->read_offset
= 0;
338 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
339 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
341 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
344 num_sectors
= (qemu_read_size
+ iscsilun
->block_size
345 + acb
->read_offset
- 1)
346 / iscsilun
->block_size
;
348 acb
->task
= malloc(sizeof(struct scsi_task
));
349 if (acb
->task
== NULL
) {
350 error_report("iSCSI: Failed to allocate task for scsi READ16 "
351 "command. %s", iscsi_get_error(iscsi
));
352 qemu_aio_release(acb
);
355 memset(acb
->task
, 0, sizeof(struct scsi_task
));
357 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
358 lba
= sector_qemu2lun(sector_num
, iscsilun
);
359 acb
->task
->expxferlen
= qemu_read_size
;
361 switch (iscsilun
->type
) {
363 acb
->task
->cdb_size
= 16;
364 acb
->task
->cdb
[0] = 0x88;
365 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
366 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
367 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
370 acb
->task
->cdb_size
= 10;
371 acb
->task
->cdb
[0] = 0x28;
372 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
373 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
377 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
381 scsi_free_scsi_task(acb
->task
);
382 qemu_aio_release(acb
);
386 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
387 scsi_task_add_data_in_buffer(acb
->task
,
388 acb
->qiov
->iov
[i
].iov_len
,
389 acb
->qiov
->iov
[i
].iov_base
);
392 iscsi_set_events(iscsilun
);
399 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
400 void *command_data
, void *opaque
)
402 IscsiAIOCB
*acb
= opaque
;
404 if (acb
->canceled
!= 0) {
410 error_report("Failed to sync10 data on iSCSI lun. %s",
411 iscsi_get_error(iscsi
));
415 iscsi_schedule_bh(acb
);
418 static BlockDriverAIOCB
*
419 iscsi_aio_flush(BlockDriverState
*bs
,
420 BlockDriverCompletionFunc
*cb
, void *opaque
)
422 IscsiLun
*iscsilun
= bs
->opaque
;
423 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
426 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
428 acb
->iscsilun
= iscsilun
;
431 acb
->status
= -EINPROGRESS
;
433 acb
->task
= iscsi_synchronizecache10_task(iscsi
, iscsilun
->lun
,
435 iscsi_synccache10_cb
,
437 if (acb
->task
== NULL
) {
438 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
439 iscsi_get_error(iscsi
));
440 qemu_aio_release(acb
);
444 iscsi_set_events(iscsilun
);
450 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
451 void *command_data
, void *opaque
)
453 IscsiAIOCB
*acb
= opaque
;
455 if (acb
->canceled
!= 0) {
461 error_report("Failed to unmap data on iSCSI lun. %s",
462 iscsi_get_error(iscsi
));
466 iscsi_schedule_bh(acb
);
469 static BlockDriverAIOCB
*
470 iscsi_aio_discard(BlockDriverState
*bs
,
471 int64_t sector_num
, int nb_sectors
,
472 BlockDriverCompletionFunc
*cb
, void *opaque
)
474 IscsiLun
*iscsilun
= bs
->opaque
;
475 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
477 struct unmap_list list
[1];
479 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
481 acb
->iscsilun
= iscsilun
;
484 acb
->status
= -EINPROGRESS
;
486 list
[0].lba
= sector_qemu2lun(sector_num
, iscsilun
);
487 list
[0].num
= nb_sectors
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
489 acb
->task
= iscsi_unmap_task(iscsi
, iscsilun
->lun
,
493 if (acb
->task
== NULL
) {
494 error_report("iSCSI: Failed to send unmap command. %s",
495 iscsi_get_error(iscsi
));
496 qemu_aio_release(acb
);
500 iscsi_set_events(iscsilun
);
507 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
508 void *command_data
, void *opaque
)
510 IscsiAIOCB
*acb
= opaque
;
512 if (acb
->canceled
!= 0) {
518 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
519 iscsi_get_error(iscsi
));
523 acb
->ioh
->driver_status
= 0;
524 acb
->ioh
->host_status
= 0;
527 #define SG_ERR_DRIVER_SENSE 0x08
529 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
532 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
534 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
535 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
536 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
537 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
540 iscsi_schedule_bh(acb
);
543 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
544 unsigned long int req
, void *buf
,
545 BlockDriverCompletionFunc
*cb
, void *opaque
)
547 IscsiLun
*iscsilun
= bs
->opaque
;
548 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
549 struct iscsi_data data
;
552 assert(req
== SG_IO
);
554 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
556 acb
->iscsilun
= iscsilun
;
559 acb
->status
= -EINPROGRESS
;
563 acb
->task
= malloc(sizeof(struct scsi_task
));
564 if (acb
->task
== NULL
) {
565 error_report("iSCSI: Failed to allocate task for scsi command. %s",
566 iscsi_get_error(iscsi
));
567 qemu_aio_release(acb
);
570 memset(acb
->task
, 0, sizeof(struct scsi_task
));
572 switch (acb
->ioh
->dxfer_direction
) {
573 case SG_DXFER_TO_DEV
:
574 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
576 case SG_DXFER_FROM_DEV
:
577 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
580 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
584 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
585 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
586 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
588 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
589 data
.data
= acb
->ioh
->dxferp
;
590 data
.size
= acb
->ioh
->dxfer_len
;
592 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
594 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
597 scsi_free_scsi_task(acb
->task
);
598 qemu_aio_release(acb
);
602 /* tell libiscsi to read straight into the buffer we got from ioctl */
603 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
604 scsi_task_add_data_in_buffer(acb
->task
,
609 iscsi_set_events(iscsilun
);
615 static void ioctl_cb(void *opaque
, int status
)
617 int *p_status
= opaque
;
621 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
623 IscsiLun
*iscsilun
= bs
->opaque
;
627 case SG_GET_VERSION_NUM
:
631 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
634 status
= -EINPROGRESS
;
635 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
637 while (status
== -EINPROGRESS
) {
650 iscsi_getlength(BlockDriverState
*bs
)
652 IscsiLun
*iscsilun
= bs
->opaque
;
655 len
= iscsilun
->num_blocks
;
656 len
*= iscsilun
->block_size
;
661 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
665 const char *user
= NULL
;
666 const char *password
= NULL
;
668 list
= qemu_find_opts("iscsi");
673 opts
= qemu_opts_find(list
, target
);
675 opts
= QTAILQ_FIRST(&list
->head
);
681 user
= qemu_opt_get(opts
, "user");
686 password
= qemu_opt_get(opts
, "password");
688 error_report("CHAP username specified but no password was given");
692 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
693 error_report("Failed to set initiator username and password");
700 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
704 const char *digest
= NULL
;
706 list
= qemu_find_opts("iscsi");
711 opts
= qemu_opts_find(list
, target
);
713 opts
= QTAILQ_FIRST(&list
->head
);
719 digest
= qemu_opt_get(opts
, "header-digest");
724 if (!strcmp(digest
, "CRC32C")) {
725 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
726 } else if (!strcmp(digest
, "NONE")) {
727 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
728 } else if (!strcmp(digest
, "CRC32C-NONE")) {
729 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
730 } else if (!strcmp(digest
, "NONE-CRC32C")) {
731 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
733 error_report("Invalid header-digest setting : %s", digest
);
737 static char *parse_initiator_name(const char *target
)
741 const char *name
= NULL
;
742 const char *iscsi_name
= qemu_get_vm_name();
744 list
= qemu_find_opts("iscsi");
746 opts
= qemu_opts_find(list
, target
);
748 opts
= QTAILQ_FIRST(&list
->head
);
751 name
= qemu_opt_get(opts
, "initiator-name");
756 return g_strdup(name
);
758 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
759 iscsi_name
? ":" : "",
760 iscsi_name
? iscsi_name
: "");
765 * We support iscsi url's on the form
766 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
768 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
770 IscsiLun
*iscsilun
= bs
->opaque
;
771 struct iscsi_context
*iscsi
= NULL
;
772 struct iscsi_url
*iscsi_url
= NULL
;
773 struct scsi_task
*task
= NULL
;
774 struct scsi_inquiry_standard
*inq
= NULL
;
775 struct scsi_readcapacity10
*rc10
= NULL
;
776 struct scsi_readcapacity16
*rc16
= NULL
;
777 char *initiator_name
= NULL
;
780 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
781 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
782 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
783 "of 512", BDRV_SECTOR_SIZE
);
787 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
788 if (iscsi_url
== NULL
) {
789 error_report("Failed to parse URL : %s", filename
);
794 memset(iscsilun
, 0, sizeof(IscsiLun
));
796 initiator_name
= parse_initiator_name(iscsi_url
->target
);
798 iscsi
= iscsi_create_context(initiator_name
);
800 error_report("iSCSI: Failed to create iSCSI context.");
805 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
806 error_report("iSCSI: Failed to set target name.");
811 if (iscsi_url
->user
!= NULL
) {
812 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
815 error_report("Failed to set initiator username and password");
821 /* check if we got CHAP username/password via the options */
822 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
823 error_report("iSCSI: Failed to set CHAP user/password");
828 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
829 error_report("iSCSI: Failed to set session type to normal.");
834 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
836 /* check if we got HEADER_DIGEST via the options */
837 parse_header_digest(iscsi
, iscsi_url
->target
);
839 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
840 error_report("iSCSI: Failed to connect to LUN : %s",
841 iscsi_get_error(iscsi
));
846 iscsilun
->iscsi
= iscsi
;
847 iscsilun
->lun
= iscsi_url
->lun
;
849 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
851 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
852 error_report("iSCSI: failed to send inquiry command.");
857 inq
= scsi_datain_unmarshall(task
);
859 error_report("iSCSI: Failed to unmarshall inquiry data.");
864 iscsilun
->type
= inq
->periperal_device_type
;
866 scsi_free_scsi_task(task
);
868 switch (iscsilun
->type
) {
870 task
= iscsi_readcapacity16_sync(iscsi
, iscsilun
->lun
);
871 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
872 error_report("iSCSI: failed to send readcapacity16 command.");
876 rc16
= scsi_datain_unmarshall(task
);
878 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
882 iscsilun
->block_size
= rc16
->block_length
;
883 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
886 task
= iscsi_readcapacity10_sync(iscsi
, iscsilun
->lun
, 0, 0);
887 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
888 error_report("iSCSI: failed to send readcapacity10 command.");
892 rc10
= scsi_datain_unmarshall(task
);
894 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
898 iscsilun
->block_size
= rc10
->block_size
;
899 if (rc10
->lba
== 0) {
900 /* blank disk loaded */
901 iscsilun
->num_blocks
= 0;
903 iscsilun
->num_blocks
= rc10
->lba
+ 1;
910 bs
->total_sectors
= iscsilun
->num_blocks
*
911 iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
913 /* Medium changer or tape. We dont have any emulation for this so this must
914 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
915 * to read from the device to guess the image format.
917 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
918 iscsilun
->type
== TYPE_TAPE
) {
925 if (initiator_name
!= NULL
) {
926 g_free(initiator_name
);
928 if (iscsi_url
!= NULL
) {
929 iscsi_destroy_url(iscsi_url
);
932 scsi_free_scsi_task(task
);
937 iscsi_destroy_context(iscsi
);
939 memset(iscsilun
, 0, sizeof(IscsiLun
));
944 static void iscsi_close(BlockDriverState
*bs
)
946 IscsiLun
*iscsilun
= bs
->opaque
;
947 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
949 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
950 iscsi_destroy_context(iscsi
);
951 memset(iscsilun
, 0, sizeof(IscsiLun
));
954 static int iscsi_has_zero_init(BlockDriverState
*bs
)
959 static BlockDriver bdrv_iscsi
= {
960 .format_name
= "iscsi",
961 .protocol_name
= "iscsi",
963 .instance_size
= sizeof(IscsiLun
),
964 .bdrv_file_open
= iscsi_open
,
965 .bdrv_close
= iscsi_close
,
967 .bdrv_getlength
= iscsi_getlength
,
969 .bdrv_aio_readv
= iscsi_aio_readv
,
970 .bdrv_aio_writev
= iscsi_aio_writev
,
971 .bdrv_aio_flush
= iscsi_aio_flush
,
973 .bdrv_aio_discard
= iscsi_aio_discard
,
974 .bdrv_has_zero_init
= iscsi_has_zero_init
,
977 .bdrv_ioctl
= iscsi_ioctl
,
978 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
982 static void iscsi_block_init(void)
984 bdrv_register(&bdrv_iscsi
);
987 block_init(iscsi_block_init
);