pseries: Add support for new KVM hash table control call
[qemu/rayw.git] / block / iscsi.c
blobd0b1a10ee4f61da9f6937d74ef9f32377b99dcaa
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_bh_cb(void *p)
78 IscsiAIOCB *acb = p;
80 qemu_bh_delete(acb->bh);
82 if (acb->canceled == 0) {
83 acb->common.cb(acb->common.opaque, acb->status);
86 if (acb->task != NULL) {
87 scsi_free_scsi_task(acb->task);
88 acb->task = NULL;
91 qemu_aio_release(acb);
94 static void
95 iscsi_schedule_bh(IscsiAIOCB *acb)
97 if (acb->bh) {
98 return;
100 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
101 qemu_bh_schedule(acb->bh);
105 static void
106 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
107 void *private_data)
109 IscsiAIOCB *acb = private_data;
111 acb->status = -ECANCELED;
112 iscsi_schedule_bh(acb);
115 static void
116 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
118 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
119 IscsiLun *iscsilun = acb->iscsilun;
121 if (acb->status != -EINPROGRESS) {
122 return;
125 acb->canceled = 1;
127 /* send a task mgmt call to the target to cancel the task on the target */
128 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
129 iscsi_abort_task_cb, acb);
131 while (acb->status == -EINPROGRESS) {
132 qemu_aio_wait();
136 static AIOPool iscsi_aio_pool = {
137 .aiocb_size = sizeof(IscsiAIOCB),
138 .cancel = iscsi_aio_cancel,
142 static void iscsi_process_read(void *arg);
143 static void iscsi_process_write(void *arg);
145 static int iscsi_process_flush(void *arg)
147 IscsiLun *iscsilun = arg;
149 return iscsi_queue_length(iscsilun->iscsi) > 0;
152 static void
153 iscsi_set_events(IscsiLun *iscsilun)
155 struct iscsi_context *iscsi = iscsilun->iscsi;
156 int ev;
158 /* We always register a read handler. */
159 ev = POLLIN;
160 ev |= iscsi_which_events(iscsi);
161 if (ev != iscsilun->events) {
162 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
163 iscsi_process_read,
164 (ev & POLLOUT) ? iscsi_process_write : NULL,
165 iscsi_process_flush,
166 iscsilun);
170 iscsilun->events = ev;
173 static void
174 iscsi_process_read(void *arg)
176 IscsiLun *iscsilun = arg;
177 struct iscsi_context *iscsi = iscsilun->iscsi;
179 iscsi_service(iscsi, POLLIN);
180 iscsi_set_events(iscsilun);
183 static void
184 iscsi_process_write(void *arg)
186 IscsiLun *iscsilun = arg;
187 struct iscsi_context *iscsi = iscsilun->iscsi;
189 iscsi_service(iscsi, POLLOUT);
190 iscsi_set_events(iscsilun);
194 static void
195 iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
196 void *command_data, void *opaque)
198 IscsiAIOCB *acb = opaque;
200 trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
202 g_free(acb->buf);
204 if (acb->canceled != 0) {
205 return;
208 acb->status = 0;
209 if (status < 0) {
210 error_report("Failed to write16 data to iSCSI lun. %s",
211 iscsi_get_error(iscsi));
212 acb->status = -EIO;
215 iscsi_schedule_bh(acb);
218 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
220 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
223 static BlockDriverAIOCB *
224 iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
225 QEMUIOVector *qiov, int nb_sectors,
226 BlockDriverCompletionFunc *cb,
227 void *opaque)
229 IscsiLun *iscsilun = bs->opaque;
230 struct iscsi_context *iscsi = iscsilun->iscsi;
231 IscsiAIOCB *acb;
232 size_t size;
233 uint32_t num_sectors;
234 uint64_t lba;
235 struct iscsi_data data;
237 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
238 trace_iscsi_aio_writev(iscsi, sector_num, nb_sectors, opaque, acb);
240 acb->iscsilun = iscsilun;
241 acb->qiov = qiov;
243 acb->canceled = 0;
244 acb->bh = NULL;
245 acb->status = -EINPROGRESS;
247 /* XXX we should pass the iovec to write16 to avoid the extra copy */
248 /* this will allow us to get rid of 'buf' completely */
249 size = nb_sectors * BDRV_SECTOR_SIZE;
250 acb->buf = g_malloc(size);
251 qemu_iovec_to_buf(acb->qiov, 0, acb->buf, size);
253 acb->task = malloc(sizeof(struct scsi_task));
254 if (acb->task == NULL) {
255 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
256 "command. %s", iscsi_get_error(iscsi));
257 qemu_aio_release(acb);
258 return NULL;
260 memset(acb->task, 0, sizeof(struct scsi_task));
262 acb->task->xfer_dir = SCSI_XFER_WRITE;
263 acb->task->cdb_size = 16;
264 acb->task->cdb[0] = 0x8a;
265 lba = sector_qemu2lun(sector_num, iscsilun);
266 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
267 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
268 num_sectors = size / iscsilun->block_size;
269 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
270 acb->task->expxferlen = size;
272 data.data = acb->buf;
273 data.size = size;
275 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
276 iscsi_aio_write16_cb,
277 &data,
278 acb) != 0) {
279 scsi_free_scsi_task(acb->task);
280 g_free(acb->buf);
281 qemu_aio_release(acb);
282 return NULL;
285 iscsi_set_events(iscsilun);
287 return &acb->common;
290 static void
291 iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
292 void *command_data, void *opaque)
294 IscsiAIOCB *acb = opaque;
296 trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
298 if (acb->canceled != 0) {
299 return;
302 acb->status = 0;
303 if (status != 0) {
304 error_report("Failed to read16 data from iSCSI lun. %s",
305 iscsi_get_error(iscsi));
306 acb->status = -EIO;
309 iscsi_schedule_bh(acb);
312 static BlockDriverAIOCB *
313 iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
314 QEMUIOVector *qiov, int nb_sectors,
315 BlockDriverCompletionFunc *cb,
316 void *opaque)
318 IscsiLun *iscsilun = bs->opaque;
319 struct iscsi_context *iscsi = iscsilun->iscsi;
320 IscsiAIOCB *acb;
321 size_t qemu_read_size;
322 int i;
323 uint64_t lba;
324 uint32_t num_sectors;
326 qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
328 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
329 trace_iscsi_aio_readv(iscsi, sector_num, nb_sectors, opaque, acb);
331 acb->iscsilun = iscsilun;
332 acb->qiov = qiov;
334 acb->canceled = 0;
335 acb->bh = NULL;
336 acb->status = -EINPROGRESS;
337 acb->read_size = qemu_read_size;
338 acb->buf = NULL;
340 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
341 * may be misaligned to the LUN, so we may need to read some extra
342 * data.
344 acb->read_offset = 0;
345 if (iscsilun->block_size > BDRV_SECTOR_SIZE) {
346 uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num;
348 acb->read_offset = bdrv_offset % iscsilun->block_size;
351 num_sectors = (qemu_read_size + iscsilun->block_size
352 + acb->read_offset - 1)
353 / iscsilun->block_size;
355 acb->task = malloc(sizeof(struct scsi_task));
356 if (acb->task == NULL) {
357 error_report("iSCSI: Failed to allocate task for scsi READ16 "
358 "command. %s", iscsi_get_error(iscsi));
359 qemu_aio_release(acb);
360 return NULL;
362 memset(acb->task, 0, sizeof(struct scsi_task));
364 acb->task->xfer_dir = SCSI_XFER_READ;
365 lba = sector_qemu2lun(sector_num, iscsilun);
366 acb->task->expxferlen = qemu_read_size;
368 switch (iscsilun->type) {
369 case TYPE_DISK:
370 acb->task->cdb_size = 16;
371 acb->task->cdb[0] = 0x88;
372 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
373 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
374 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
375 break;
376 default:
377 acb->task->cdb_size = 10;
378 acb->task->cdb[0] = 0x28;
379 *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
380 *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
381 break;
384 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
385 iscsi_aio_read16_cb,
386 NULL,
387 acb) != 0) {
388 scsi_free_scsi_task(acb->task);
389 qemu_aio_release(acb);
390 return NULL;
393 for (i = 0; i < acb->qiov->niov; i++) {
394 scsi_task_add_data_in_buffer(acb->task,
395 acb->qiov->iov[i].iov_len,
396 acb->qiov->iov[i].iov_base);
399 iscsi_set_events(iscsilun);
401 return &acb->common;
405 static void
406 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
407 void *command_data, void *opaque)
409 IscsiAIOCB *acb = opaque;
411 if (acb->canceled != 0) {
412 return;
415 acb->status = 0;
416 if (status < 0) {
417 error_report("Failed to sync10 data on iSCSI lun. %s",
418 iscsi_get_error(iscsi));
419 acb->status = -EIO;
422 iscsi_schedule_bh(acb);
425 static BlockDriverAIOCB *
426 iscsi_aio_flush(BlockDriverState *bs,
427 BlockDriverCompletionFunc *cb, void *opaque)
429 IscsiLun *iscsilun = bs->opaque;
430 struct iscsi_context *iscsi = iscsilun->iscsi;
431 IscsiAIOCB *acb;
433 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
435 acb->iscsilun = iscsilun;
436 acb->canceled = 0;
437 acb->bh = NULL;
438 acb->status = -EINPROGRESS;
440 acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun,
441 0, 0, 0, 0,
442 iscsi_synccache10_cb,
443 acb);
444 if (acb->task == NULL) {
445 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
446 iscsi_get_error(iscsi));
447 qemu_aio_release(acb);
448 return NULL;
451 iscsi_set_events(iscsilun);
453 return &acb->common;
456 static void
457 iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
458 void *command_data, void *opaque)
460 IscsiAIOCB *acb = opaque;
462 if (acb->canceled != 0) {
463 return;
466 acb->status = 0;
467 if (status < 0) {
468 error_report("Failed to unmap data on iSCSI lun. %s",
469 iscsi_get_error(iscsi));
470 acb->status = -EIO;
473 iscsi_schedule_bh(acb);
476 static BlockDriverAIOCB *
477 iscsi_aio_discard(BlockDriverState *bs,
478 int64_t sector_num, int nb_sectors,
479 BlockDriverCompletionFunc *cb, void *opaque)
481 IscsiLun *iscsilun = bs->opaque;
482 struct iscsi_context *iscsi = iscsilun->iscsi;
483 IscsiAIOCB *acb;
484 struct unmap_list list[1];
486 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
488 acb->iscsilun = iscsilun;
489 acb->canceled = 0;
490 acb->bh = NULL;
491 acb->status = -EINPROGRESS;
493 list[0].lba = sector_qemu2lun(sector_num, iscsilun);
494 list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size;
496 acb->task = iscsi_unmap_task(iscsi, iscsilun->lun,
497 0, 0, &list[0], 1,
498 iscsi_unmap_cb,
499 acb);
500 if (acb->task == NULL) {
501 error_report("iSCSI: Failed to send unmap command. %s",
502 iscsi_get_error(iscsi));
503 qemu_aio_release(acb);
504 return NULL;
507 iscsi_set_events(iscsilun);
509 return &acb->common;
512 #ifdef __linux__
513 static void
514 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
515 void *command_data, void *opaque)
517 IscsiAIOCB *acb = opaque;
519 if (acb->canceled != 0) {
520 return;
523 acb->status = 0;
524 if (status < 0) {
525 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
526 iscsi_get_error(iscsi));
527 acb->status = -EIO;
530 acb->ioh->driver_status = 0;
531 acb->ioh->host_status = 0;
532 acb->ioh->resid = 0;
534 #define SG_ERR_DRIVER_SENSE 0x08
536 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
537 int ss;
539 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
541 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
542 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
543 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
544 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
547 iscsi_schedule_bh(acb);
550 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
551 unsigned long int req, void *buf,
552 BlockDriverCompletionFunc *cb, void *opaque)
554 IscsiLun *iscsilun = bs->opaque;
555 struct iscsi_context *iscsi = iscsilun->iscsi;
556 struct iscsi_data data;
557 IscsiAIOCB *acb;
559 assert(req == SG_IO);
561 acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
563 acb->iscsilun = iscsilun;
564 acb->canceled = 0;
565 acb->bh = NULL;
566 acb->status = -EINPROGRESS;
567 acb->buf = NULL;
568 acb->ioh = buf;
570 acb->task = malloc(sizeof(struct scsi_task));
571 if (acb->task == NULL) {
572 error_report("iSCSI: Failed to allocate task for scsi command. %s",
573 iscsi_get_error(iscsi));
574 qemu_aio_release(acb);
575 return NULL;
577 memset(acb->task, 0, sizeof(struct scsi_task));
579 switch (acb->ioh->dxfer_direction) {
580 case SG_DXFER_TO_DEV:
581 acb->task->xfer_dir = SCSI_XFER_WRITE;
582 break;
583 case SG_DXFER_FROM_DEV:
584 acb->task->xfer_dir = SCSI_XFER_READ;
585 break;
586 default:
587 acb->task->xfer_dir = SCSI_XFER_NONE;
588 break;
591 acb->task->cdb_size = acb->ioh->cmd_len;
592 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
593 acb->task->expxferlen = acb->ioh->dxfer_len;
595 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
596 data.data = acb->ioh->dxferp;
597 data.size = acb->ioh->dxfer_len;
599 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
600 iscsi_aio_ioctl_cb,
601 (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
602 &data : NULL,
603 acb) != 0) {
604 scsi_free_scsi_task(acb->task);
605 qemu_aio_release(acb);
606 return NULL;
609 /* tell libiscsi to read straight into the buffer we got from ioctl */
610 if (acb->task->xfer_dir == SCSI_XFER_READ) {
611 scsi_task_add_data_in_buffer(acb->task,
612 acb->ioh->dxfer_len,
613 acb->ioh->dxferp);
616 iscsi_set_events(iscsilun);
618 return &acb->common;
622 static void ioctl_cb(void *opaque, int status)
624 int *p_status = opaque;
625 *p_status = status;
628 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
630 IscsiLun *iscsilun = bs->opaque;
631 int status;
633 switch (req) {
634 case SG_GET_VERSION_NUM:
635 *(int *)buf = 30000;
636 break;
637 case SG_GET_SCSI_ID:
638 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
639 break;
640 case SG_IO:
641 status = -EINPROGRESS;
642 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
644 while (status == -EINPROGRESS) {
645 qemu_aio_wait();
648 return 0;
649 default:
650 return -1;
652 return 0;
654 #endif
656 static int64_t
657 iscsi_getlength(BlockDriverState *bs)
659 IscsiLun *iscsilun = bs->opaque;
660 int64_t len;
662 len = iscsilun->num_blocks;
663 len *= iscsilun->block_size;
665 return len;
668 static void
669 iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status,
670 void *command_data, void *opaque)
672 struct IscsiTask *itask = opaque;
673 struct scsi_readcapacity16 *rc16;
674 struct scsi_task *task = command_data;
676 if (status != 0) {
677 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
678 iscsi_get_error(iscsi));
679 itask->status = 1;
680 itask->complete = 1;
681 scsi_free_scsi_task(task);
682 return;
685 rc16 = scsi_datain_unmarshall(task);
686 if (rc16 == NULL) {
687 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
688 itask->status = 1;
689 itask->complete = 1;
690 scsi_free_scsi_task(task);
691 return;
694 itask->iscsilun->block_size = rc16->block_length;
695 itask->iscsilun->num_blocks = rc16->returned_lba + 1;
696 itask->bs->total_sectors = itask->iscsilun->num_blocks *
697 itask->iscsilun->block_size / BDRV_SECTOR_SIZE ;
699 itask->status = 0;
700 itask->complete = 1;
701 scsi_free_scsi_task(task);
704 static void
705 iscsi_readcapacity10_cb(struct iscsi_context *iscsi, int status,
706 void *command_data, void *opaque)
708 struct IscsiTask *itask = opaque;
709 struct scsi_readcapacity10 *rc10;
710 struct scsi_task *task = command_data;
712 if (status != 0) {
713 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
714 iscsi_get_error(iscsi));
715 itask->status = 1;
716 itask->complete = 1;
717 scsi_free_scsi_task(task);
718 return;
721 rc10 = scsi_datain_unmarshall(task);
722 if (rc10 == NULL) {
723 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
724 itask->status = 1;
725 itask->complete = 1;
726 scsi_free_scsi_task(task);
727 return;
730 itask->iscsilun->block_size = rc10->block_size;
731 if (rc10->lba == 0) {
732 /* blank disk loaded */
733 itask->iscsilun->num_blocks = 0;
734 } else {
735 itask->iscsilun->num_blocks = rc10->lba + 1;
737 itask->bs->total_sectors = itask->iscsilun->num_blocks *
738 itask->iscsilun->block_size / BDRV_SECTOR_SIZE ;
740 itask->status = 0;
741 itask->complete = 1;
742 scsi_free_scsi_task(task);
745 static void
746 iscsi_inquiry_cb(struct iscsi_context *iscsi, int status, void *command_data,
747 void *opaque)
749 struct IscsiTask *itask = opaque;
750 struct scsi_task *task = command_data;
751 struct scsi_inquiry_standard *inq;
753 if (status != 0) {
754 itask->status = 1;
755 itask->complete = 1;
756 scsi_free_scsi_task(task);
757 return;
760 inq = scsi_datain_unmarshall(task);
761 if (inq == NULL) {
762 error_report("iSCSI: Failed to unmarshall inquiry data.");
763 itask->status = 1;
764 itask->complete = 1;
765 scsi_free_scsi_task(task);
766 return;
769 itask->iscsilun->type = inq->periperal_device_type;
771 scsi_free_scsi_task(task);
773 switch (itask->iscsilun->type) {
774 case TYPE_DISK:
775 task = iscsi_readcapacity16_task(iscsi, itask->iscsilun->lun,
776 iscsi_readcapacity16_cb, opaque);
777 if (task == NULL) {
778 error_report("iSCSI: failed to send readcapacity16 command.");
779 itask->status = 1;
780 itask->complete = 1;
781 return;
783 break;
784 case TYPE_ROM:
785 task = iscsi_readcapacity10_task(iscsi, itask->iscsilun->lun,
786 0, 0,
787 iscsi_readcapacity10_cb, opaque);
788 if (task == NULL) {
789 error_report("iSCSI: failed to send readcapacity16 command.");
790 itask->status = 1;
791 itask->complete = 1;
792 return;
794 break;
795 default:
796 itask->status = 0;
797 itask->complete = 1;
801 static void
802 iscsi_connect_cb(struct iscsi_context *iscsi, int status, void *command_data,
803 void *opaque)
805 struct IscsiTask *itask = opaque;
806 struct scsi_task *task;
808 if (status != 0) {
809 itask->status = 1;
810 itask->complete = 1;
811 return;
814 task = iscsi_inquiry_task(iscsi, itask->iscsilun->lun,
815 0, 0, 36,
816 iscsi_inquiry_cb, opaque);
817 if (task == NULL) {
818 error_report("iSCSI: failed to send inquiry command.");
819 itask->status = 1;
820 itask->complete = 1;
821 return;
825 static int parse_chap(struct iscsi_context *iscsi, const char *target)
827 QemuOptsList *list;
828 QemuOpts *opts;
829 const char *user = NULL;
830 const char *password = NULL;
832 list = qemu_find_opts("iscsi");
833 if (!list) {
834 return 0;
837 opts = qemu_opts_find(list, target);
838 if (opts == NULL) {
839 opts = QTAILQ_FIRST(&list->head);
840 if (!opts) {
841 return 0;
845 user = qemu_opt_get(opts, "user");
846 if (!user) {
847 return 0;
850 password = qemu_opt_get(opts, "password");
851 if (!password) {
852 error_report("CHAP username specified but no password was given");
853 return -1;
856 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
857 error_report("Failed to set initiator username and password");
858 return -1;
861 return 0;
864 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
866 QemuOptsList *list;
867 QemuOpts *opts;
868 const char *digest = NULL;
870 list = qemu_find_opts("iscsi");
871 if (!list) {
872 return;
875 opts = qemu_opts_find(list, target);
876 if (opts == NULL) {
877 opts = QTAILQ_FIRST(&list->head);
878 if (!opts) {
879 return;
883 digest = qemu_opt_get(opts, "header-digest");
884 if (!digest) {
885 return;
888 if (!strcmp(digest, "CRC32C")) {
889 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
890 } else if (!strcmp(digest, "NONE")) {
891 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
892 } else if (!strcmp(digest, "CRC32C-NONE")) {
893 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
894 } else if (!strcmp(digest, "NONE-CRC32C")) {
895 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
896 } else {
897 error_report("Invalid header-digest setting : %s", digest);
901 static char *parse_initiator_name(const char *target)
903 QemuOptsList *list;
904 QemuOpts *opts;
905 const char *name = NULL;
906 const char *iscsi_name = qemu_get_vm_name();
908 list = qemu_find_opts("iscsi");
909 if (list) {
910 opts = qemu_opts_find(list, target);
911 if (!opts) {
912 opts = QTAILQ_FIRST(&list->head);
914 if (opts) {
915 name = qemu_opt_get(opts, "initiator-name");
919 if (name) {
920 return g_strdup(name);
921 } else {
922 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
923 iscsi_name ? ":" : "",
924 iscsi_name ? iscsi_name : "");
929 * We support iscsi url's on the form
930 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
932 static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)
934 IscsiLun *iscsilun = bs->opaque;
935 struct iscsi_context *iscsi = NULL;
936 struct iscsi_url *iscsi_url = NULL;
937 struct IscsiTask task;
938 char *initiator_name = NULL;
939 int ret;
941 if ((BDRV_SECTOR_SIZE % 512) != 0) {
942 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
943 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
944 "of 512", BDRV_SECTOR_SIZE);
945 return -EINVAL;
948 iscsi_url = iscsi_parse_full_url(iscsi, filename);
949 if (iscsi_url == NULL) {
950 error_report("Failed to parse URL : %s %s", filename,
951 iscsi_get_error(iscsi));
952 ret = -EINVAL;
953 goto out;
956 memset(iscsilun, 0, sizeof(IscsiLun));
958 initiator_name = parse_initiator_name(iscsi_url->target);
960 iscsi = iscsi_create_context(initiator_name);
961 if (iscsi == NULL) {
962 error_report("iSCSI: Failed to create iSCSI context.");
963 ret = -ENOMEM;
964 goto out;
967 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
968 error_report("iSCSI: Failed to set target name.");
969 ret = -EINVAL;
970 goto out;
973 if (iscsi_url->user != NULL) {
974 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
975 iscsi_url->passwd);
976 if (ret != 0) {
977 error_report("Failed to set initiator username and password");
978 ret = -EINVAL;
979 goto out;
983 /* check if we got CHAP username/password via the options */
984 if (parse_chap(iscsi, iscsi_url->target) != 0) {
985 error_report("iSCSI: Failed to set CHAP user/password");
986 ret = -EINVAL;
987 goto out;
990 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
991 error_report("iSCSI: Failed to set session type to normal.");
992 ret = -EINVAL;
993 goto out;
996 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
998 /* check if we got HEADER_DIGEST via the options */
999 parse_header_digest(iscsi, iscsi_url->target);
1001 task.iscsilun = iscsilun;
1002 task.status = 0;
1003 task.complete = 0;
1004 task.bs = bs;
1006 iscsilun->iscsi = iscsi;
1007 iscsilun->lun = iscsi_url->lun;
1009 if (iscsi_full_connect_async(iscsi, iscsi_url->portal, iscsi_url->lun,
1010 iscsi_connect_cb, &task)
1011 != 0) {
1012 error_report("iSCSI: Failed to start async connect.");
1013 ret = -EINVAL;
1014 goto out;
1017 while (!task.complete) {
1018 iscsi_set_events(iscsilun);
1019 qemu_aio_wait();
1021 if (task.status != 0) {
1022 error_report("iSCSI: Failed to connect to LUN : %s",
1023 iscsi_get_error(iscsi));
1024 ret = -EINVAL;
1025 goto out;
1028 /* Medium changer or tape. We dont have any emulation for this so this must
1029 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1030 * to read from the device to guess the image format.
1032 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1033 iscsilun->type == TYPE_TAPE) {
1034 bs->sg = 1;
1037 ret = 0;
1039 out:
1040 if (initiator_name != NULL) {
1041 g_free(initiator_name);
1043 if (iscsi_url != NULL) {
1044 iscsi_destroy_url(iscsi_url);
1047 if (ret) {
1048 if (iscsi != NULL) {
1049 iscsi_destroy_context(iscsi);
1051 memset(iscsilun, 0, sizeof(IscsiLun));
1053 return ret;
1056 static void iscsi_close(BlockDriverState *bs)
1058 IscsiLun *iscsilun = bs->opaque;
1059 struct iscsi_context *iscsi = iscsilun->iscsi;
1061 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
1062 iscsi_destroy_context(iscsi);
1063 memset(iscsilun, 0, sizeof(IscsiLun));
1066 static BlockDriver bdrv_iscsi = {
1067 .format_name = "iscsi",
1068 .protocol_name = "iscsi",
1070 .instance_size = sizeof(IscsiLun),
1071 .bdrv_file_open = iscsi_open,
1072 .bdrv_close = iscsi_close,
1074 .bdrv_getlength = iscsi_getlength,
1076 .bdrv_aio_readv = iscsi_aio_readv,
1077 .bdrv_aio_writev = iscsi_aio_writev,
1078 .bdrv_aio_flush = iscsi_aio_flush,
1080 .bdrv_aio_discard = iscsi_aio_discard,
1082 #ifdef __linux__
1083 .bdrv_ioctl = iscsi_ioctl,
1084 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1085 #endif
1088 static void iscsi_block_init(void)
1090 bdrv_register(&bdrv_iscsi);
1093 block_init(iscsi_block_init);