seabios: update to 1.7.2 release
[qemu/opensuse.git] / block / iscsi.c
blobf08cf9663b4dc8b4c92eb976d68264f4fcd610bf
1 /*
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
22 * THE SOFTWARE.
25 #include "config-host.h"
27 #include <poll.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"
33 #include "trace.h"
34 #include "hw/scsi-defs.h"
36 #include <iscsi/iscsi.h>
37 #include <iscsi/scsi-lowlevel.h>
39 #ifdef __linux__
40 #include <scsi/sg.h>
41 #include <hw/scsi-defs.h>
42 #endif
44 typedef struct IscsiLun {
45 struct iscsi_context *iscsi;
46 int lun;
47 enum scsi_inquiry_peripheral_device_type type;
48 int block_size;
49 uint64_t num_blocks;
50 int events;
51 } IscsiLun;
53 typedef struct IscsiAIOCB {
54 BlockDriverAIOCB common;
55 QEMUIOVector *qiov;
56 QEMUBH *bh;
57 IscsiLun *iscsilun;
58 struct scsi_task *task;
59 uint8_t *buf;
60 int status;
61 int canceled;
62 size_t read_size;
63 size_t read_offset;
64 #ifdef __linux__
65 sg_io_hdr_t *ioh;
66 #endif
67 } IscsiAIOCB;
69 static void
70 iscsi_bh_cb(void *p)
72 IscsiAIOCB *acb = p;
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);
82 acb->task = NULL;
85 qemu_aio_release(acb);
88 static void
89 iscsi_schedule_bh(IscsiAIOCB *acb)
91 if (acb->bh) {
92 return;
94 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
95 qemu_bh_schedule(acb->bh);
99 static void
100 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
101 void *private_data)
103 IscsiAIOCB *acb = private_data;
105 acb->status = -ECANCELED;
106 iscsi_schedule_bh(acb);
109 static void
110 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
112 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
113 IscsiLun *iscsilun = acb->iscsilun;
115 if (acb->status != -EINPROGRESS) {
116 return;
119 acb->canceled = 1;
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) {
126 qemu_aio_wait();
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;
146 static void
147 iscsi_set_events(IscsiLun *iscsilun)
149 struct iscsi_context *iscsi = iscsilun->iscsi;
150 int ev;
152 /* We always register a read handler. */
153 ev = POLLIN;
154 ev |= iscsi_which_events(iscsi);
155 if (ev != iscsilun->events) {
156 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
157 iscsi_process_read,
158 (ev & POLLOUT) ? iscsi_process_write : NULL,
159 iscsi_process_flush,
160 iscsilun);
164 iscsilun->events = ev;
167 static void
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);
177 static void
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);
188 static void
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);
196 g_free(acb->buf);
198 if (acb->canceled != 0) {
199 return;
202 acb->status = 0;
203 if (status < 0) {
204 error_report("Failed to write16 data to iSCSI lun. %s",
205 iscsi_get_error(iscsi));
206 acb->status = -EIO;
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,
221 void *opaque)
223 IscsiLun *iscsilun = bs->opaque;
224 struct iscsi_context *iscsi = iscsilun->iscsi;
225 IscsiAIOCB *acb;
226 size_t size;
227 uint32_t num_sectors;
228 uint64_t lba;
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;
235 acb->qiov = qiov;
237 acb->canceled = 0;
238 acb->bh = NULL;
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);
252 return NULL;
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;
267 data.size = size;
269 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
270 iscsi_aio_write16_cb,
271 &data,
272 acb) != 0) {
273 scsi_free_scsi_task(acb->task);
274 g_free(acb->buf);
275 qemu_aio_release(acb);
276 return NULL;
279 iscsi_set_events(iscsilun);
281 return &acb->common;
284 static void
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) {
293 return;
296 acb->status = 0;
297 if (status != 0) {
298 error_report("Failed to read16 data from iSCSI lun. %s",
299 iscsi_get_error(iscsi));
300 acb->status = -EIO;
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,
310 void *opaque)
312 IscsiLun *iscsilun = bs->opaque;
313 struct iscsi_context *iscsi = iscsilun->iscsi;
314 IscsiAIOCB *acb;
315 size_t qemu_read_size;
316 int i;
317 uint64_t lba;
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;
326 acb->qiov = qiov;
328 acb->canceled = 0;
329 acb->bh = NULL;
330 acb->status = -EINPROGRESS;
331 acb->read_size = qemu_read_size;
332 acb->buf = NULL;
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
336 * data.
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);
354 return NULL;
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) {
363 case TYPE_DISK:
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);
369 break;
370 default:
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);
375 break;
378 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
379 iscsi_aio_read16_cb,
380 NULL,
381 acb) != 0) {
382 scsi_free_scsi_task(acb->task);
383 qemu_aio_release(acb);
384 return NULL;
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);
395 return &acb->common;
399 static void
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) {
406 return;
409 acb->status = 0;
410 if (status < 0) {
411 error_report("Failed to sync10 data on iSCSI lun. %s",
412 iscsi_get_error(iscsi));
413 acb->status = -EIO;
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;
425 IscsiAIOCB *acb;
427 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
429 acb->iscsilun = iscsilun;
430 acb->canceled = 0;
431 acb->bh = NULL;
432 acb->status = -EINPROGRESS;
434 acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun,
435 0, 0, 0, 0,
436 iscsi_synccache10_cb,
437 acb);
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);
442 return NULL;
445 iscsi_set_events(iscsilun);
447 return &acb->common;
450 static void
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) {
457 return;
460 acb->status = 0;
461 if (status < 0) {
462 error_report("Failed to unmap data on iSCSI lun. %s",
463 iscsi_get_error(iscsi));
464 acb->status = -EIO;
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;
477 IscsiAIOCB *acb;
478 struct unmap_list list[1];
480 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
482 acb->iscsilun = iscsilun;
483 acb->canceled = 0;
484 acb->bh = NULL;
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,
491 0, 0, &list[0], 1,
492 iscsi_unmap_cb,
493 acb);
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);
498 return NULL;
501 iscsi_set_events(iscsilun);
503 return &acb->common;
506 #ifdef __linux__
507 static void
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) {
514 return;
517 acb->status = 0;
518 if (status < 0) {
519 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
520 iscsi_get_error(iscsi));
521 acb->status = -EIO;
524 acb->ioh->driver_status = 0;
525 acb->ioh->host_status = 0;
526 acb->ioh->resid = 0;
528 #define SG_ERR_DRIVER_SENSE 0x08
530 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
531 int ss;
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;
551 IscsiAIOCB *acb;
553 assert(req == SG_IO);
555 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
557 acb->iscsilun = iscsilun;
558 acb->canceled = 0;
559 acb->bh = NULL;
560 acb->status = -EINPROGRESS;
561 acb->buf = NULL;
562 acb->ioh = buf;
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);
569 return NULL;
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;
576 break;
577 case SG_DXFER_FROM_DEV:
578 acb->task->xfer_dir = SCSI_XFER_READ;
579 break;
580 default:
581 acb->task->xfer_dir = SCSI_XFER_NONE;
582 break;
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,
594 iscsi_aio_ioctl_cb,
595 (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
596 &data : NULL,
597 acb) != 0) {
598 scsi_free_scsi_task(acb->task);
599 qemu_aio_release(acb);
600 return NULL;
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,
606 acb->ioh->dxfer_len,
607 acb->ioh->dxferp);
610 iscsi_set_events(iscsilun);
612 return &acb->common;
616 static void ioctl_cb(void *opaque, int status)
618 int *p_status = opaque;
619 *p_status = status;
622 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
624 IscsiLun *iscsilun = bs->opaque;
625 int status;
627 switch (req) {
628 case SG_GET_VERSION_NUM:
629 *(int *)buf = 30000;
630 break;
631 case SG_GET_SCSI_ID:
632 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
633 break;
634 case SG_IO:
635 status = -EINPROGRESS;
636 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
638 while (status == -EINPROGRESS) {
639 qemu_aio_wait();
642 return 0;
643 default:
644 return -1;
646 return 0;
648 #endif
650 static int64_t
651 iscsi_getlength(BlockDriverState *bs)
653 IscsiLun *iscsilun = bs->opaque;
654 int64_t len;
656 len = iscsilun->num_blocks;
657 len *= iscsilun->block_size;
659 return len;
662 static int parse_chap(struct iscsi_context *iscsi, const char *target)
664 QemuOptsList *list;
665 QemuOpts *opts;
666 const char *user = NULL;
667 const char *password = NULL;
669 list = qemu_find_opts("iscsi");
670 if (!list) {
671 return 0;
674 opts = qemu_opts_find(list, target);
675 if (opts == NULL) {
676 opts = QTAILQ_FIRST(&list->head);
677 if (!opts) {
678 return 0;
682 user = qemu_opt_get(opts, "user");
683 if (!user) {
684 return 0;
687 password = qemu_opt_get(opts, "password");
688 if (!password) {
689 error_report("CHAP username specified but no password was given");
690 return -1;
693 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
694 error_report("Failed to set initiator username and password");
695 return -1;
698 return 0;
701 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
703 QemuOptsList *list;
704 QemuOpts *opts;
705 const char *digest = NULL;
707 list = qemu_find_opts("iscsi");
708 if (!list) {
709 return;
712 opts = qemu_opts_find(list, target);
713 if (opts == NULL) {
714 opts = QTAILQ_FIRST(&list->head);
715 if (!opts) {
716 return;
720 digest = qemu_opt_get(opts, "header-digest");
721 if (!digest) {
722 return;
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);
733 } else {
734 error_report("Invalid header-digest setting : %s", digest);
738 static char *parse_initiator_name(const char *target)
740 QemuOptsList *list;
741 QemuOpts *opts;
742 const char *name = NULL;
743 const char *iscsi_name = qemu_get_vm_name();
745 list = qemu_find_opts("iscsi");
746 if (list) {
747 opts = qemu_opts_find(list, target);
748 if (!opts) {
749 opts = QTAILQ_FIRST(&list->head);
751 if (opts) {
752 name = qemu_opt_get(opts, "initiator-name");
756 if (name) {
757 return g_strdup(name);
758 } else {
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;
779 int ret;
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);
785 return -EINVAL;
788 iscsi_url = iscsi_parse_full_url(iscsi, filename);
789 if (iscsi_url == NULL) {
790 error_report("Failed to parse URL : %s", filename);
791 ret = -EINVAL;
792 goto out;
795 memset(iscsilun, 0, sizeof(IscsiLun));
797 initiator_name = parse_initiator_name(iscsi_url->target);
799 iscsi = iscsi_create_context(initiator_name);
800 if (iscsi == NULL) {
801 error_report("iSCSI: Failed to create iSCSI context.");
802 ret = -ENOMEM;
803 goto out;
806 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
807 error_report("iSCSI: Failed to set target name.");
808 ret = -EINVAL;
809 goto out;
812 if (iscsi_url->user != NULL) {
813 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
814 iscsi_url->passwd);
815 if (ret != 0) {
816 error_report("Failed to set initiator username and password");
817 ret = -EINVAL;
818 goto out;
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");
825 ret = -EINVAL;
826 goto out;
829 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
830 error_report("iSCSI: Failed to set session type to normal.");
831 ret = -EINVAL;
832 goto out;
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));
843 ret = -EINVAL;
844 goto out;
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.");
854 ret = -EINVAL;
855 goto out;
858 inq = scsi_datain_unmarshall(task);
859 if (inq == NULL) {
860 error_report("iSCSI: Failed to unmarshall inquiry data.");
861 ret = -EINVAL;
862 goto out;
865 iscsilun->type = inq->periperal_device_type;
867 scsi_free_scsi_task(task);
869 switch (iscsilun->type) {
870 case TYPE_DISK:
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.");
874 ret = -EINVAL;
875 goto out;
877 rc16 = scsi_datain_unmarshall(task);
878 if (rc16 == NULL) {
879 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
880 ret = -EINVAL;
881 goto out;
883 iscsilun->block_size = rc16->block_length;
884 iscsilun->num_blocks = rc16->returned_lba + 1;
885 break;
886 case TYPE_ROM:
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.");
890 ret = -EINVAL;
891 goto out;
893 rc10 = scsi_datain_unmarshall(task);
894 if (rc10 == NULL) {
895 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
896 ret = -EINVAL;
897 goto out;
899 iscsilun->block_size = rc10->block_size;
900 if (rc10->lba == 0) {
901 /* blank disk loaded */
902 iscsilun->num_blocks = 0;
903 } else {
904 iscsilun->num_blocks = rc10->lba + 1;
906 break;
907 default:
908 break;
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) {
920 bs->sg = 1;
923 ret = 0;
925 out:
926 if (initiator_name != NULL) {
927 g_free(initiator_name);
929 if (iscsi_url != NULL) {
930 iscsi_destroy_url(iscsi_url);
932 if (task != NULL) {
933 scsi_free_scsi_task(task);
936 if (ret) {
937 if (iscsi != NULL) {
938 iscsi_destroy_context(iscsi);
940 memset(iscsilun, 0, sizeof(IscsiLun));
942 return ret;
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)
957 return 0;
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,
977 #ifdef __linux__
978 .bdrv_ioctl = iscsi_ioctl,
979 .bdrv_aio_ioctl = iscsi_aio_ioctl,
980 #endif
983 static QemuOptsList qemu_iscsi_opts = {
984 .name = "iscsi",
985 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
986 .desc = {
988 .name = "user",
989 .type = QEMU_OPT_STRING,
990 .help = "username for CHAP authentication to target",
992 .name = "password",
993 .type = QEMU_OPT_STRING,
994 .help = "password for CHAP authentication to target",
996 .name = "header-digest",
997 .type = QEMU_OPT_STRING,
998 .help = "HeaderDigest setting. "
999 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1001 .name = "initiator-name",
1002 .type = QEMU_OPT_STRING,
1003 .help = "Initiator iqn name to use when connecting",
1005 { /* end of list */ }
1009 static void iscsi_block_init(void)
1011 bdrv_register(&bdrv_iscsi);
1012 qemu_add_opts(&qemu_iscsi_opts);
1015 block_init(iscsi_block_init);