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 AIOPool iscsi_aio_pool
= {
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_aio_pool
, 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 if (!(bs
->open_flags
& BDRV_O_CACHE_WB
)) {
259 /* set FUA on writes when cache mode is write through */
260 acb
->task
->cdb
[1] |= 0x04;
262 lba
= sector_qemu2lun(sector_num
, iscsilun
);
263 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
264 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
265 num_sectors
= size
/ iscsilun
->block_size
;
266 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
267 acb
->task
->expxferlen
= size
;
269 data
.data
= acb
->buf
;
272 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
273 iscsi_aio_write16_cb
,
276 scsi_free_scsi_task(acb
->task
);
278 qemu_aio_release(acb
);
282 iscsi_set_events(iscsilun
);
288 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
289 void *command_data
, void *opaque
)
291 IscsiAIOCB
*acb
= opaque
;
293 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
295 if (acb
->canceled
!= 0) {
301 error_report("Failed to read16 data from iSCSI lun. %s",
302 iscsi_get_error(iscsi
));
306 iscsi_schedule_bh(acb
);
309 static BlockDriverAIOCB
*
310 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
311 QEMUIOVector
*qiov
, int nb_sectors
,
312 BlockDriverCompletionFunc
*cb
,
315 IscsiLun
*iscsilun
= bs
->opaque
;
316 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
318 size_t qemu_read_size
;
321 uint32_t num_sectors
;
323 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
325 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
326 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
328 acb
->iscsilun
= iscsilun
;
333 acb
->status
= -EINPROGRESS
;
334 acb
->read_size
= qemu_read_size
;
337 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
338 * may be misaligned to the LUN, so we may need to read some extra
341 acb
->read_offset
= 0;
342 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
343 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
345 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
348 num_sectors
= (qemu_read_size
+ iscsilun
->block_size
349 + acb
->read_offset
- 1)
350 / iscsilun
->block_size
;
352 acb
->task
= malloc(sizeof(struct scsi_task
));
353 if (acb
->task
== NULL
) {
354 error_report("iSCSI: Failed to allocate task for scsi READ16 "
355 "command. %s", iscsi_get_error(iscsi
));
356 qemu_aio_release(acb
);
359 memset(acb
->task
, 0, sizeof(struct scsi_task
));
361 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
362 lba
= sector_qemu2lun(sector_num
, iscsilun
);
363 acb
->task
->expxferlen
= qemu_read_size
;
365 switch (iscsilun
->type
) {
367 acb
->task
->cdb_size
= 16;
368 acb
->task
->cdb
[0] = 0x88;
369 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
370 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
371 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
374 acb
->task
->cdb_size
= 10;
375 acb
->task
->cdb
[0] = 0x28;
376 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
377 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
381 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
385 scsi_free_scsi_task(acb
->task
);
386 qemu_aio_release(acb
);
390 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
391 scsi_task_add_data_in_buffer(acb
->task
,
392 acb
->qiov
->iov
[i
].iov_len
,
393 acb
->qiov
->iov
[i
].iov_base
);
396 iscsi_set_events(iscsilun
);
403 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
404 void *command_data
, void *opaque
)
406 IscsiAIOCB
*acb
= opaque
;
408 if (acb
->canceled
!= 0) {
414 error_report("Failed to sync10 data on iSCSI lun. %s",
415 iscsi_get_error(iscsi
));
419 iscsi_schedule_bh(acb
);
422 static BlockDriverAIOCB
*
423 iscsi_aio_flush(BlockDriverState
*bs
,
424 BlockDriverCompletionFunc
*cb
, void *opaque
)
426 IscsiLun
*iscsilun
= bs
->opaque
;
427 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
430 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
432 acb
->iscsilun
= iscsilun
;
435 acb
->status
= -EINPROGRESS
;
437 acb
->task
= iscsi_synchronizecache10_task(iscsi
, iscsilun
->lun
,
439 iscsi_synccache10_cb
,
441 if (acb
->task
== NULL
) {
442 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
443 iscsi_get_error(iscsi
));
444 qemu_aio_release(acb
);
448 iscsi_set_events(iscsilun
);
454 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
455 void *command_data
, void *opaque
)
457 IscsiAIOCB
*acb
= opaque
;
459 if (acb
->canceled
!= 0) {
465 error_report("Failed to unmap data on iSCSI lun. %s",
466 iscsi_get_error(iscsi
));
470 iscsi_schedule_bh(acb
);
473 static BlockDriverAIOCB
*
474 iscsi_aio_discard(BlockDriverState
*bs
,
475 int64_t sector_num
, int nb_sectors
,
476 BlockDriverCompletionFunc
*cb
, void *opaque
)
478 IscsiLun
*iscsilun
= bs
->opaque
;
479 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
481 struct unmap_list list
[1];
483 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
485 acb
->iscsilun
= iscsilun
;
488 acb
->status
= -EINPROGRESS
;
490 list
[0].lba
= sector_qemu2lun(sector_num
, iscsilun
);
491 list
[0].num
= nb_sectors
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
493 acb
->task
= iscsi_unmap_task(iscsi
, iscsilun
->lun
,
497 if (acb
->task
== NULL
) {
498 error_report("iSCSI: Failed to send unmap command. %s",
499 iscsi_get_error(iscsi
));
500 qemu_aio_release(acb
);
504 iscsi_set_events(iscsilun
);
511 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
512 void *command_data
, void *opaque
)
514 IscsiAIOCB
*acb
= opaque
;
516 if (acb
->canceled
!= 0) {
522 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
523 iscsi_get_error(iscsi
));
527 acb
->ioh
->driver_status
= 0;
528 acb
->ioh
->host_status
= 0;
531 #define SG_ERR_DRIVER_SENSE 0x08
533 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
536 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
538 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
539 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
540 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
541 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
544 iscsi_schedule_bh(acb
);
547 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
548 unsigned long int req
, void *buf
,
549 BlockDriverCompletionFunc
*cb
, void *opaque
)
551 IscsiLun
*iscsilun
= bs
->opaque
;
552 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
553 struct iscsi_data data
;
556 assert(req
== SG_IO
);
558 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
560 acb
->iscsilun
= iscsilun
;
563 acb
->status
= -EINPROGRESS
;
567 acb
->task
= malloc(sizeof(struct scsi_task
));
568 if (acb
->task
== NULL
) {
569 error_report("iSCSI: Failed to allocate task for scsi command. %s",
570 iscsi_get_error(iscsi
));
571 qemu_aio_release(acb
);
574 memset(acb
->task
, 0, sizeof(struct scsi_task
));
576 switch (acb
->ioh
->dxfer_direction
) {
577 case SG_DXFER_TO_DEV
:
578 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
580 case SG_DXFER_FROM_DEV
:
581 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
584 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
588 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
589 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
590 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
592 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
593 data
.data
= acb
->ioh
->dxferp
;
594 data
.size
= acb
->ioh
->dxfer_len
;
596 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
598 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
601 scsi_free_scsi_task(acb
->task
);
602 qemu_aio_release(acb
);
606 /* tell libiscsi to read straight into the buffer we got from ioctl */
607 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
608 scsi_task_add_data_in_buffer(acb
->task
,
613 iscsi_set_events(iscsilun
);
619 static void ioctl_cb(void *opaque
, int status
)
621 int *p_status
= opaque
;
625 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
627 IscsiLun
*iscsilun
= bs
->opaque
;
631 case SG_GET_VERSION_NUM
:
635 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
638 status
= -EINPROGRESS
;
639 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
641 while (status
== -EINPROGRESS
) {
654 iscsi_getlength(BlockDriverState
*bs
)
656 IscsiLun
*iscsilun
= bs
->opaque
;
659 len
= iscsilun
->num_blocks
;
660 len
*= iscsilun
->block_size
;
665 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
669 const char *user
= NULL
;
670 const char *password
= NULL
;
672 list
= qemu_find_opts("iscsi");
677 opts
= qemu_opts_find(list
, target
);
679 opts
= QTAILQ_FIRST(&list
->head
);
685 user
= qemu_opt_get(opts
, "user");
690 password
= qemu_opt_get(opts
, "password");
692 error_report("CHAP username specified but no password was given");
696 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
697 error_report("Failed to set initiator username and password");
704 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
708 const char *digest
= NULL
;
710 list
= qemu_find_opts("iscsi");
715 opts
= qemu_opts_find(list
, target
);
717 opts
= QTAILQ_FIRST(&list
->head
);
723 digest
= qemu_opt_get(opts
, "header-digest");
728 if (!strcmp(digest
, "CRC32C")) {
729 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
730 } else if (!strcmp(digest
, "NONE")) {
731 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
732 } else if (!strcmp(digest
, "CRC32C-NONE")) {
733 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
734 } else if (!strcmp(digest
, "NONE-CRC32C")) {
735 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
737 error_report("Invalid header-digest setting : %s", digest
);
741 static char *parse_initiator_name(const char *target
)
745 const char *name
= NULL
;
746 const char *iscsi_name
= qemu_get_vm_name();
748 list
= qemu_find_opts("iscsi");
750 opts
= qemu_opts_find(list
, target
);
752 opts
= QTAILQ_FIRST(&list
->head
);
755 name
= qemu_opt_get(opts
, "initiator-name");
760 return g_strdup(name
);
762 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
763 iscsi_name
? ":" : "",
764 iscsi_name
? iscsi_name
: "");
769 * We support iscsi url's on the form
770 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
772 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
774 IscsiLun
*iscsilun
= bs
->opaque
;
775 struct iscsi_context
*iscsi
= NULL
;
776 struct iscsi_url
*iscsi_url
= NULL
;
777 struct scsi_task
*task
= NULL
;
778 struct scsi_inquiry_standard
*inq
= NULL
;
779 struct scsi_readcapacity10
*rc10
= NULL
;
780 struct scsi_readcapacity16
*rc16
= NULL
;
781 char *initiator_name
= NULL
;
784 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
785 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
786 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
787 "of 512", BDRV_SECTOR_SIZE
);
791 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
792 if (iscsi_url
== NULL
) {
793 error_report("Failed to parse URL : %s", filename
);
798 memset(iscsilun
, 0, sizeof(IscsiLun
));
800 initiator_name
= parse_initiator_name(iscsi_url
->target
);
802 iscsi
= iscsi_create_context(initiator_name
);
804 error_report("iSCSI: Failed to create iSCSI context.");
809 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
810 error_report("iSCSI: Failed to set target name.");
815 if (iscsi_url
->user
!= NULL
) {
816 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
819 error_report("Failed to set initiator username and password");
825 /* check if we got CHAP username/password via the options */
826 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
827 error_report("iSCSI: Failed to set CHAP user/password");
832 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
833 error_report("iSCSI: Failed to set session type to normal.");
838 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
840 /* check if we got HEADER_DIGEST via the options */
841 parse_header_digest(iscsi
, iscsi_url
->target
);
843 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
844 error_report("iSCSI: Failed to connect to LUN : %s",
845 iscsi_get_error(iscsi
));
850 iscsilun
->iscsi
= iscsi
;
851 iscsilun
->lun
= iscsi_url
->lun
;
853 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
855 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
856 error_report("iSCSI: failed to send inquiry command.");
861 inq
= scsi_datain_unmarshall(task
);
863 error_report("iSCSI: Failed to unmarshall inquiry data.");
868 iscsilun
->type
= inq
->periperal_device_type
;
870 scsi_free_scsi_task(task
);
872 switch (iscsilun
->type
) {
874 task
= iscsi_readcapacity16_sync(iscsi
, iscsilun
->lun
);
875 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
876 error_report("iSCSI: failed to send readcapacity16 command.");
880 rc16
= scsi_datain_unmarshall(task
);
882 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
886 iscsilun
->block_size
= rc16
->block_length
;
887 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
890 task
= iscsi_readcapacity10_sync(iscsi
, iscsilun
->lun
, 0, 0);
891 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
892 error_report("iSCSI: failed to send readcapacity10 command.");
896 rc10
= scsi_datain_unmarshall(task
);
898 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
902 iscsilun
->block_size
= rc10
->block_size
;
903 if (rc10
->lba
== 0) {
904 /* blank disk loaded */
905 iscsilun
->num_blocks
= 0;
907 iscsilun
->num_blocks
= rc10
->lba
+ 1;
914 bs
->total_sectors
= iscsilun
->num_blocks
*
915 iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
917 /* Medium changer or tape. We dont have any emulation for this so this must
918 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
919 * to read from the device to guess the image format.
921 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
922 iscsilun
->type
== TYPE_TAPE
) {
929 if (initiator_name
!= NULL
) {
930 g_free(initiator_name
);
932 if (iscsi_url
!= NULL
) {
933 iscsi_destroy_url(iscsi_url
);
936 scsi_free_scsi_task(task
);
941 iscsi_destroy_context(iscsi
);
943 memset(iscsilun
, 0, sizeof(IscsiLun
));
948 static void iscsi_close(BlockDriverState
*bs
)
950 IscsiLun
*iscsilun
= bs
->opaque
;
951 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
953 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
954 iscsi_destroy_context(iscsi
);
955 memset(iscsilun
, 0, sizeof(IscsiLun
));
958 static BlockDriver bdrv_iscsi
= {
959 .format_name
= "iscsi",
960 .protocol_name
= "iscsi",
962 .instance_size
= sizeof(IscsiLun
),
963 .bdrv_file_open
= iscsi_open
,
964 .bdrv_close
= iscsi_close
,
966 .bdrv_getlength
= iscsi_getlength
,
968 .bdrv_aio_readv
= iscsi_aio_readv
,
969 .bdrv_aio_writev
= iscsi_aio_writev
,
970 .bdrv_aio_flush
= iscsi_aio_flush
,
972 .bdrv_aio_discard
= iscsi_aio_discard
,
975 .bdrv_ioctl
= iscsi_ioctl
,
976 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
980 static void iscsi_block_init(void)
982 bdrv_register(&bdrv_iscsi
);
985 block_init(iscsi_block_init
);