qapi: add 'query-target' command to return target arch
[qemu-kvm.git] / block / iscsi.c
blobbb9cf82459c0c573d45f2b46b8942896c5c120b8
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-error.h"
31 #include "block_int.h"
32 #include "trace.h"
33 #include "hw/scsi-defs.h"
35 #include <iscsi/iscsi.h>
36 #include <iscsi/scsi-lowlevel.h>
38 #ifdef __linux__
39 #include <scsi/sg.h>
40 #include <hw/scsi-defs.h>
41 #endif
43 typedef struct IscsiLun {
44 struct iscsi_context *iscsi;
45 int lun;
46 enum scsi_inquiry_peripheral_device_type type;
47 int block_size;
48 uint64_t num_blocks;
49 int events;
50 } IscsiLun;
52 typedef struct IscsiAIOCB {
53 BlockDriverAIOCB common;
54 QEMUIOVector *qiov;
55 QEMUBH *bh;
56 IscsiLun *iscsilun;
57 struct scsi_task *task;
58 uint8_t *buf;
59 int status;
60 int canceled;
61 size_t read_size;
62 size_t read_offset;
63 #ifdef __linux__
64 sg_io_hdr_t *ioh;
65 #endif
66 } IscsiAIOCB;
68 struct IscsiTask {
69 IscsiLun *iscsilun;
70 BlockDriverState *bs;
71 int status;
72 int complete;
75 static void
76 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
77 void *private_data)
79 IscsiAIOCB *acb = (IscsiAIOCB *)private_data;
81 scsi_free_scsi_task(acb->task);
82 acb->task = NULL;
85 static void
86 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
88 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
89 IscsiLun *iscsilun = acb->iscsilun;
91 acb->canceled = 1;
93 acb->common.cb(acb->common.opaque, -ECANCELED);
95 /* send a task mgmt call to the target to cancel the task on the target
96 * this also cancels the task in libiscsi
98 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
99 iscsi_abort_task_cb, &acb);
102 static AIOPool iscsi_aio_pool = {
103 .aiocb_size = sizeof(IscsiAIOCB),
104 .cancel = iscsi_aio_cancel,
108 static void iscsi_process_read(void *arg);
109 static void iscsi_process_write(void *arg);
111 static int iscsi_process_flush(void *arg)
113 IscsiLun *iscsilun = arg;
115 return iscsi_queue_length(iscsilun->iscsi) > 0;
118 static void
119 iscsi_set_events(IscsiLun *iscsilun)
121 struct iscsi_context *iscsi = iscsilun->iscsi;
122 int ev;
124 /* We always register a read handler. */
125 ev = POLLIN;
126 ev |= iscsi_which_events(iscsi);
127 if (ev != iscsilun->events) {
128 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
129 iscsi_process_read,
130 (ev & POLLOUT) ? iscsi_process_write : NULL,
131 iscsi_process_flush,
132 iscsilun);
136 /* If we just added an event, the callback might be delayed
137 * unless we call qemu_notify_event().
139 if (ev & ~iscsilun->events) {
140 qemu_notify_event();
142 iscsilun->events = ev;
145 static void
146 iscsi_process_read(void *arg)
148 IscsiLun *iscsilun = arg;
149 struct iscsi_context *iscsi = iscsilun->iscsi;
151 iscsi_service(iscsi, POLLIN);
152 iscsi_set_events(iscsilun);
155 static void
156 iscsi_process_write(void *arg)
158 IscsiLun *iscsilun = arg;
159 struct iscsi_context *iscsi = iscsilun->iscsi;
161 iscsi_service(iscsi, POLLOUT);
162 iscsi_set_events(iscsilun);
166 static int
167 iscsi_schedule_bh(QEMUBHFunc *cb, IscsiAIOCB *acb)
169 acb->bh = qemu_bh_new(cb, acb);
170 if (!acb->bh) {
171 error_report("oom: could not create iscsi bh");
172 return -EIO;
175 qemu_bh_schedule(acb->bh);
176 return 0;
179 static void
180 iscsi_readv_writev_bh_cb(void *p)
182 IscsiAIOCB *acb = p;
184 qemu_bh_delete(acb->bh);
186 if (!acb->canceled) {
187 acb->common.cb(acb->common.opaque, acb->status);
190 qemu_aio_release(acb);
192 if (acb->canceled) {
193 return;
196 scsi_free_scsi_task(acb->task);
197 acb->task = NULL;
201 static void
202 iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
203 void *command_data, void *opaque)
205 IscsiAIOCB *acb = opaque;
207 trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
209 g_free(acb->buf);
211 if (acb->canceled) {
212 qemu_aio_release(acb);
213 return;
216 acb->status = 0;
217 if (status < 0) {
218 error_report("Failed to write16 data to iSCSI lun. %s",
219 iscsi_get_error(iscsi));
220 acb->status = -EIO;
223 iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
226 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
228 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
231 static BlockDriverAIOCB *
232 iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
233 QEMUIOVector *qiov, int nb_sectors,
234 BlockDriverCompletionFunc *cb,
235 void *opaque)
237 IscsiLun *iscsilun = bs->opaque;
238 struct iscsi_context *iscsi = iscsilun->iscsi;
239 IscsiAIOCB *acb;
240 size_t size;
241 uint32_t num_sectors;
242 uint64_t lba;
243 struct iscsi_data data;
245 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
246 trace_iscsi_aio_writev(iscsi, sector_num, nb_sectors, opaque, acb);
248 acb->iscsilun = iscsilun;
249 acb->qiov = qiov;
251 acb->canceled = 0;
253 /* XXX we should pass the iovec to write16 to avoid the extra copy */
254 /* this will allow us to get rid of 'buf' completely */
255 size = nb_sectors * BDRV_SECTOR_SIZE;
256 acb->buf = g_malloc(size);
257 qemu_iovec_to_buf(acb->qiov, 0, acb->buf, size);
259 acb->task = malloc(sizeof(struct scsi_task));
260 if (acb->task == NULL) {
261 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
262 "command. %s", iscsi_get_error(iscsi));
263 qemu_aio_release(acb);
264 return NULL;
266 memset(acb->task, 0, sizeof(struct scsi_task));
268 acb->task->xfer_dir = SCSI_XFER_WRITE;
269 acb->task->cdb_size = 16;
270 acb->task->cdb[0] = 0x8a;
271 if (!(bs->open_flags & BDRV_O_CACHE_WB)) {
272 /* set FUA on writes when cache mode is write through */
273 acb->task->cdb[1] |= 0x04;
275 lba = sector_qemu2lun(sector_num, iscsilun);
276 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
277 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
278 num_sectors = size / iscsilun->block_size;
279 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
280 acb->task->expxferlen = size;
282 data.data = acb->buf;
283 data.size = size;
285 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
286 iscsi_aio_write16_cb,
287 &data,
288 acb) != 0) {
289 scsi_free_scsi_task(acb->task);
290 g_free(acb->buf);
291 qemu_aio_release(acb);
292 return NULL;
295 iscsi_set_events(iscsilun);
297 return &acb->common;
300 static void
301 iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
302 void *command_data, void *opaque)
304 IscsiAIOCB *acb = opaque;
306 trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
308 if (acb->canceled) {
309 qemu_aio_release(acb);
310 return;
313 acb->status = 0;
314 if (status != 0) {
315 error_report("Failed to read16 data from iSCSI lun. %s",
316 iscsi_get_error(iscsi));
317 acb->status = -EIO;
320 iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
323 static BlockDriverAIOCB *
324 iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
325 QEMUIOVector *qiov, int nb_sectors,
326 BlockDriverCompletionFunc *cb,
327 void *opaque)
329 IscsiLun *iscsilun = bs->opaque;
330 struct iscsi_context *iscsi = iscsilun->iscsi;
331 IscsiAIOCB *acb;
332 size_t qemu_read_size;
333 int i;
334 uint64_t lba;
335 uint32_t num_sectors;
337 qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
339 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
340 trace_iscsi_aio_readv(iscsi, sector_num, nb_sectors, opaque, acb);
342 acb->iscsilun = iscsilun;
343 acb->qiov = qiov;
345 acb->canceled = 0;
346 acb->read_size = qemu_read_size;
347 acb->buf = NULL;
349 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
350 * may be misaligned to the LUN, so we may need to read some extra
351 * data.
353 acb->read_offset = 0;
354 if (iscsilun->block_size > BDRV_SECTOR_SIZE) {
355 uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num;
357 acb->read_offset = bdrv_offset % iscsilun->block_size;
360 num_sectors = (qemu_read_size + iscsilun->block_size
361 + acb->read_offset - 1)
362 / iscsilun->block_size;
364 acb->task = malloc(sizeof(struct scsi_task));
365 if (acb->task == NULL) {
366 error_report("iSCSI: Failed to allocate task for scsi READ16 "
367 "command. %s", iscsi_get_error(iscsi));
368 qemu_aio_release(acb);
369 return NULL;
371 memset(acb->task, 0, sizeof(struct scsi_task));
373 acb->task->xfer_dir = SCSI_XFER_READ;
374 lba = sector_qemu2lun(sector_num, iscsilun);
375 acb->task->expxferlen = qemu_read_size;
377 switch (iscsilun->type) {
378 case TYPE_DISK:
379 acb->task->cdb_size = 16;
380 acb->task->cdb[0] = 0x88;
381 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
382 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
383 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
384 break;
385 default:
386 acb->task->cdb_size = 10;
387 acb->task->cdb[0] = 0x28;
388 *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
389 *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
390 break;
393 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
394 iscsi_aio_read16_cb,
395 NULL,
396 acb) != 0) {
397 scsi_free_scsi_task(acb->task);
398 qemu_aio_release(acb);
399 return NULL;
402 for (i = 0; i < acb->qiov->niov; i++) {
403 scsi_task_add_data_in_buffer(acb->task,
404 acb->qiov->iov[i].iov_len,
405 acb->qiov->iov[i].iov_base);
408 iscsi_set_events(iscsilun);
410 return &acb->common;
414 static void
415 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
416 void *command_data, void *opaque)
418 IscsiAIOCB *acb = opaque;
420 if (acb->canceled) {
421 qemu_aio_release(acb);
422 return;
425 acb->status = 0;
426 if (status < 0) {
427 error_report("Failed to sync10 data on iSCSI lun. %s",
428 iscsi_get_error(iscsi));
429 acb->status = -EIO;
432 iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
435 static BlockDriverAIOCB *
436 iscsi_aio_flush(BlockDriverState *bs,
437 BlockDriverCompletionFunc *cb, void *opaque)
439 IscsiLun *iscsilun = bs->opaque;
440 struct iscsi_context *iscsi = iscsilun->iscsi;
441 IscsiAIOCB *acb;
443 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
445 acb->iscsilun = iscsilun;
446 acb->canceled = 0;
448 acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun,
449 0, 0, 0, 0,
450 iscsi_synccache10_cb,
451 acb);
452 if (acb->task == NULL) {
453 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
454 iscsi_get_error(iscsi));
455 qemu_aio_release(acb);
456 return NULL;
459 iscsi_set_events(iscsilun);
461 return &acb->common;
464 static void
465 iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
466 void *command_data, void *opaque)
468 IscsiAIOCB *acb = opaque;
470 if (acb->canceled) {
471 qemu_aio_release(acb);
472 return;
475 acb->status = 0;
476 if (status < 0) {
477 error_report("Failed to unmap data on iSCSI lun. %s",
478 iscsi_get_error(iscsi));
479 acb->status = -EIO;
482 iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
485 static BlockDriverAIOCB *
486 iscsi_aio_discard(BlockDriverState *bs,
487 int64_t sector_num, int nb_sectors,
488 BlockDriverCompletionFunc *cb, void *opaque)
490 IscsiLun *iscsilun = bs->opaque;
491 struct iscsi_context *iscsi = iscsilun->iscsi;
492 IscsiAIOCB *acb;
493 struct unmap_list list[1];
495 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
497 acb->iscsilun = iscsilun;
498 acb->canceled = 0;
500 list[0].lba = sector_qemu2lun(sector_num, iscsilun);
501 list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size;
503 acb->task = iscsi_unmap_task(iscsi, iscsilun->lun,
504 0, 0, &list[0], 1,
505 iscsi_unmap_cb,
506 acb);
507 if (acb->task == NULL) {
508 error_report("iSCSI: Failed to send unmap command. %s",
509 iscsi_get_error(iscsi));
510 qemu_aio_release(acb);
511 return NULL;
514 iscsi_set_events(iscsilun);
516 return &acb->common;
519 #ifdef __linux__
520 static void
521 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
522 void *command_data, void *opaque)
524 IscsiAIOCB *acb = opaque;
526 if (acb->canceled) {
527 qemu_aio_release(acb);
528 return;
531 acb->status = 0;
532 if (status < 0) {
533 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
534 iscsi_get_error(iscsi));
535 acb->status = -EIO;
538 acb->ioh->driver_status = 0;
539 acb->ioh->host_status = 0;
540 acb->ioh->resid = 0;
542 #define SG_ERR_DRIVER_SENSE 0x08
544 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
545 int ss;
547 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
549 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
550 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
551 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
552 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
555 iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
558 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
559 unsigned long int req, void *buf,
560 BlockDriverCompletionFunc *cb, void *opaque)
562 IscsiLun *iscsilun = bs->opaque;
563 struct iscsi_context *iscsi = iscsilun->iscsi;
564 struct iscsi_data data;
565 IscsiAIOCB *acb;
567 assert(req == SG_IO);
569 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
571 acb->iscsilun = iscsilun;
572 acb->canceled = 0;
573 acb->buf = NULL;
574 acb->ioh = buf;
576 acb->task = malloc(sizeof(struct scsi_task));
577 if (acb->task == NULL) {
578 error_report("iSCSI: Failed to allocate task for scsi command. %s",
579 iscsi_get_error(iscsi));
580 qemu_aio_release(acb);
581 return NULL;
583 memset(acb->task, 0, sizeof(struct scsi_task));
585 switch (acb->ioh->dxfer_direction) {
586 case SG_DXFER_TO_DEV:
587 acb->task->xfer_dir = SCSI_XFER_WRITE;
588 break;
589 case SG_DXFER_FROM_DEV:
590 acb->task->xfer_dir = SCSI_XFER_READ;
591 break;
592 default:
593 acb->task->xfer_dir = SCSI_XFER_NONE;
594 break;
597 acb->task->cdb_size = acb->ioh->cmd_len;
598 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
599 acb->task->expxferlen = acb->ioh->dxfer_len;
601 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
602 data.data = acb->ioh->dxferp;
603 data.size = acb->ioh->dxfer_len;
605 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
606 iscsi_aio_ioctl_cb,
607 (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
608 &data : NULL,
609 acb) != 0) {
610 scsi_free_scsi_task(acb->task);
611 qemu_aio_release(acb);
612 return NULL;
615 /* tell libiscsi to read straight into the buffer we got from ioctl */
616 if (acb->task->xfer_dir == SCSI_XFER_READ) {
617 scsi_task_add_data_in_buffer(acb->task,
618 acb->ioh->dxfer_len,
619 acb->ioh->dxferp);
622 iscsi_set_events(iscsilun);
624 return &acb->common;
627 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
629 IscsiLun *iscsilun = bs->opaque;
631 switch (req) {
632 case SG_GET_VERSION_NUM:
633 *(int *)buf = 30000;
634 break;
635 case SG_GET_SCSI_ID:
636 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
637 break;
638 default:
639 return -1;
641 return 0;
643 #endif
645 static int64_t
646 iscsi_getlength(BlockDriverState *bs)
648 IscsiLun *iscsilun = bs->opaque;
649 int64_t len;
651 len = iscsilun->num_blocks;
652 len *= iscsilun->block_size;
654 return len;
657 static void
658 iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status,
659 void *command_data, void *opaque)
661 struct IscsiTask *itask = opaque;
662 struct scsi_readcapacity16 *rc16;
663 struct scsi_task *task = command_data;
665 if (status != 0) {
666 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
667 iscsi_get_error(iscsi));
668 itask->status = 1;
669 itask->complete = 1;
670 scsi_free_scsi_task(task);
671 return;
674 rc16 = scsi_datain_unmarshall(task);
675 if (rc16 == NULL) {
676 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
677 itask->status = 1;
678 itask->complete = 1;
679 scsi_free_scsi_task(task);
680 return;
683 itask->iscsilun->block_size = rc16->block_length;
684 itask->iscsilun->num_blocks = rc16->returned_lba + 1;
685 itask->bs->total_sectors = itask->iscsilun->num_blocks *
686 itask->iscsilun->block_size / BDRV_SECTOR_SIZE ;
688 itask->status = 0;
689 itask->complete = 1;
690 scsi_free_scsi_task(task);
693 static void
694 iscsi_readcapacity10_cb(struct iscsi_context *iscsi, int status,
695 void *command_data, void *opaque)
697 struct IscsiTask *itask = opaque;
698 struct scsi_readcapacity10 *rc10;
699 struct scsi_task *task = command_data;
701 if (status != 0) {
702 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
703 iscsi_get_error(iscsi));
704 itask->status = 1;
705 itask->complete = 1;
706 scsi_free_scsi_task(task);
707 return;
710 rc10 = scsi_datain_unmarshall(task);
711 if (rc10 == NULL) {
712 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
713 itask->status = 1;
714 itask->complete = 1;
715 scsi_free_scsi_task(task);
716 return;
719 itask->iscsilun->block_size = rc10->block_size;
720 itask->iscsilun->num_blocks = rc10->lba + 1;
721 itask->bs->total_sectors = itask->iscsilun->num_blocks *
722 itask->iscsilun->block_size / BDRV_SECTOR_SIZE ;
724 itask->status = 0;
725 itask->complete = 1;
726 scsi_free_scsi_task(task);
729 static void
730 iscsi_inquiry_cb(struct iscsi_context *iscsi, int status, void *command_data,
731 void *opaque)
733 struct IscsiTask *itask = opaque;
734 struct scsi_task *task = command_data;
735 struct scsi_inquiry_standard *inq;
737 if (status != 0) {
738 itask->status = 1;
739 itask->complete = 1;
740 scsi_free_scsi_task(task);
741 return;
744 inq = scsi_datain_unmarshall(task);
745 if (inq == NULL) {
746 error_report("iSCSI: Failed to unmarshall inquiry data.");
747 itask->status = 1;
748 itask->complete = 1;
749 scsi_free_scsi_task(task);
750 return;
753 itask->iscsilun->type = inq->periperal_device_type;
755 scsi_free_scsi_task(task);
757 switch (itask->iscsilun->type) {
758 case TYPE_DISK:
759 task = iscsi_readcapacity16_task(iscsi, itask->iscsilun->lun,
760 iscsi_readcapacity16_cb, opaque);
761 if (task == NULL) {
762 error_report("iSCSI: failed to send readcapacity16 command.");
763 itask->status = 1;
764 itask->complete = 1;
765 return;
767 break;
768 case TYPE_ROM:
769 task = iscsi_readcapacity10_task(iscsi, itask->iscsilun->lun,
770 0, 0,
771 iscsi_readcapacity10_cb, opaque);
772 if (task == NULL) {
773 error_report("iSCSI: failed to send readcapacity16 command.");
774 itask->status = 1;
775 itask->complete = 1;
776 return;
778 break;
779 default:
780 itask->status = 0;
781 itask->complete = 1;
785 static void
786 iscsi_connect_cb(struct iscsi_context *iscsi, int status, void *command_data,
787 void *opaque)
789 struct IscsiTask *itask = opaque;
790 struct scsi_task *task;
792 if (status != 0) {
793 itask->status = 1;
794 itask->complete = 1;
795 return;
798 task = iscsi_inquiry_task(iscsi, itask->iscsilun->lun,
799 0, 0, 36,
800 iscsi_inquiry_cb, opaque);
801 if (task == NULL) {
802 error_report("iSCSI: failed to send inquiry command.");
803 itask->status = 1;
804 itask->complete = 1;
805 return;
809 static int parse_chap(struct iscsi_context *iscsi, const char *target)
811 QemuOptsList *list;
812 QemuOpts *opts;
813 const char *user = NULL;
814 const char *password = NULL;
816 list = qemu_find_opts("iscsi");
817 if (!list) {
818 return 0;
821 opts = qemu_opts_find(list, target);
822 if (opts == NULL) {
823 opts = QTAILQ_FIRST(&list->head);
824 if (!opts) {
825 return 0;
829 user = qemu_opt_get(opts, "user");
830 if (!user) {
831 return 0;
834 password = qemu_opt_get(opts, "password");
835 if (!password) {
836 error_report("CHAP username specified but no password was given");
837 return -1;
840 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
841 error_report("Failed to set initiator username and password");
842 return -1;
845 return 0;
848 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
850 QemuOptsList *list;
851 QemuOpts *opts;
852 const char *digest = NULL;
854 list = qemu_find_opts("iscsi");
855 if (!list) {
856 return;
859 opts = qemu_opts_find(list, target);
860 if (opts == NULL) {
861 opts = QTAILQ_FIRST(&list->head);
862 if (!opts) {
863 return;
867 digest = qemu_opt_get(opts, "header-digest");
868 if (!digest) {
869 return;
872 if (!strcmp(digest, "CRC32C")) {
873 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
874 } else if (!strcmp(digest, "NONE")) {
875 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
876 } else if (!strcmp(digest, "CRC32C-NONE")) {
877 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
878 } else if (!strcmp(digest, "NONE-CRC32C")) {
879 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
880 } else {
881 error_report("Invalid header-digest setting : %s", digest);
885 static char *parse_initiator_name(const char *target)
887 QemuOptsList *list;
888 QemuOpts *opts;
889 const char *name = NULL;
890 const char *iscsi_name = qemu_get_vm_name();
892 list = qemu_find_opts("iscsi");
893 if (list) {
894 opts = qemu_opts_find(list, target);
895 if (!opts) {
896 opts = QTAILQ_FIRST(&list->head);
898 if (opts) {
899 name = qemu_opt_get(opts, "initiator-name");
903 if (name) {
904 return g_strdup(name);
905 } else {
906 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
907 iscsi_name ? ":" : "",
908 iscsi_name ? iscsi_name : "");
913 * We support iscsi url's on the form
914 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
916 static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)
918 IscsiLun *iscsilun = bs->opaque;
919 struct iscsi_context *iscsi = NULL;
920 struct iscsi_url *iscsi_url = NULL;
921 struct IscsiTask task;
922 char *initiator_name = NULL;
923 int ret;
925 if ((BDRV_SECTOR_SIZE % 512) != 0) {
926 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
927 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
928 "of 512", BDRV_SECTOR_SIZE);
929 return -EINVAL;
932 iscsi_url = iscsi_parse_full_url(iscsi, filename);
933 if (iscsi_url == NULL) {
934 error_report("Failed to parse URL : %s %s", filename,
935 iscsi_get_error(iscsi));
936 ret = -EINVAL;
937 goto out;
940 memset(iscsilun, 0, sizeof(IscsiLun));
942 initiator_name = parse_initiator_name(iscsi_url->target);
944 iscsi = iscsi_create_context(initiator_name);
945 if (iscsi == NULL) {
946 error_report("iSCSI: Failed to create iSCSI context.");
947 ret = -ENOMEM;
948 goto out;
951 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
952 error_report("iSCSI: Failed to set target name.");
953 ret = -EINVAL;
954 goto out;
957 if (iscsi_url->user != NULL) {
958 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
959 iscsi_url->passwd);
960 if (ret != 0) {
961 error_report("Failed to set initiator username and password");
962 ret = -EINVAL;
963 goto out;
967 /* check if we got CHAP username/password via the options */
968 if (parse_chap(iscsi, iscsi_url->target) != 0) {
969 error_report("iSCSI: Failed to set CHAP user/password");
970 ret = -EINVAL;
971 goto out;
974 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
975 error_report("iSCSI: Failed to set session type to normal.");
976 ret = -EINVAL;
977 goto out;
980 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
982 /* check if we got HEADER_DIGEST via the options */
983 parse_header_digest(iscsi, iscsi_url->target);
985 task.iscsilun = iscsilun;
986 task.status = 0;
987 task.complete = 0;
988 task.bs = bs;
990 iscsilun->iscsi = iscsi;
991 iscsilun->lun = iscsi_url->lun;
993 if (iscsi_full_connect_async(iscsi, iscsi_url->portal, iscsi_url->lun,
994 iscsi_connect_cb, &task)
995 != 0) {
996 error_report("iSCSI: Failed to start async connect.");
997 ret = -EINVAL;
998 goto out;
1001 while (!task.complete) {
1002 iscsi_set_events(iscsilun);
1003 qemu_aio_wait();
1005 if (task.status != 0) {
1006 error_report("iSCSI: Failed to connect to LUN : %s",
1007 iscsi_get_error(iscsi));
1008 ret = -EINVAL;
1009 goto out;
1012 /* Medium changer or tape. We dont have any emulation for this so this must
1013 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1014 * to read from the device to guess the image format.
1016 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1017 iscsilun->type == TYPE_TAPE) {
1018 bs->sg = 1;
1021 ret = 0;
1023 out:
1024 if (initiator_name != NULL) {
1025 g_free(initiator_name);
1027 if (iscsi_url != NULL) {
1028 iscsi_destroy_url(iscsi_url);
1031 if (ret) {
1032 if (iscsi != NULL) {
1033 iscsi_destroy_context(iscsi);
1035 memset(iscsilun, 0, sizeof(IscsiLun));
1037 return ret;
1040 static void iscsi_close(BlockDriverState *bs)
1042 IscsiLun *iscsilun = bs->opaque;
1043 struct iscsi_context *iscsi = iscsilun->iscsi;
1045 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
1046 iscsi_destroy_context(iscsi);
1047 memset(iscsilun, 0, sizeof(IscsiLun));
1050 static BlockDriver bdrv_iscsi = {
1051 .format_name = "iscsi",
1052 .protocol_name = "iscsi",
1054 .instance_size = sizeof(IscsiLun),
1055 .bdrv_file_open = iscsi_open,
1056 .bdrv_close = iscsi_close,
1058 .bdrv_getlength = iscsi_getlength,
1060 .bdrv_aio_readv = iscsi_aio_readv,
1061 .bdrv_aio_writev = iscsi_aio_writev,
1062 .bdrv_aio_flush = iscsi_aio_flush,
1064 .bdrv_aio_discard = iscsi_aio_discard,
1066 #ifdef __linux__
1067 .bdrv_ioctl = iscsi_ioctl,
1068 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1069 #endif
1072 static void iscsi_block_init(void)
1074 bdrv_register(&bdrv_iscsi);
1077 block_init(iscsi_block_init);