memory: access FlatView from a local variable
[qemu/ar7.git] / block / iscsi.c
blob0bbf0b18b4ea0e0fabe3e91a620e598c8734acb9
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 "block/scsi.h"
36 #include <iscsi/iscsi.h>
37 #include <iscsi/scsi-lowlevel.h>
39 #ifdef __linux__
40 #include <scsi/sg.h>
41 #include <block/scsi.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 QEMUTimer *nop_timer;
52 } IscsiLun;
54 typedef struct IscsiAIOCB {
55 BlockDriverAIOCB common;
56 QEMUIOVector *qiov;
57 QEMUBH *bh;
58 IscsiLun *iscsilun;
59 struct scsi_task *task;
60 uint8_t *buf;
61 int status;
62 int canceled;
63 int retries;
64 size_t read_size;
65 size_t read_offset;
66 int64_t sector_num;
67 int nb_sectors;
68 #ifdef __linux__
69 sg_io_hdr_t *ioh;
70 #endif
71 } IscsiAIOCB;
73 #define NOP_INTERVAL 5000
74 #define MAX_NOP_FAILURES 3
75 #define ISCSI_CMD_RETRIES 5
77 static void
78 iscsi_bh_cb(void *p)
80 IscsiAIOCB *acb = p;
82 qemu_bh_delete(acb->bh);
84 g_free(acb->buf);
85 acb->buf = NULL;
87 if (acb->canceled == 0) {
88 acb->common.cb(acb->common.opaque, acb->status);
91 if (acb->task != NULL) {
92 scsi_free_scsi_task(acb->task);
93 acb->task = NULL;
96 qemu_aio_release(acb);
99 static void
100 iscsi_schedule_bh(IscsiAIOCB *acb)
102 if (acb->bh) {
103 return;
105 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
106 qemu_bh_schedule(acb->bh);
110 static void
111 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
112 void *private_data)
114 IscsiAIOCB *acb = private_data;
116 acb->status = -ECANCELED;
117 iscsi_schedule_bh(acb);
120 static void
121 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
123 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
124 IscsiLun *iscsilun = acb->iscsilun;
126 if (acb->status != -EINPROGRESS) {
127 return;
130 acb->canceled = 1;
132 /* send a task mgmt call to the target to cancel the task on the target */
133 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
134 iscsi_abort_task_cb, acb);
136 while (acb->status == -EINPROGRESS) {
137 qemu_aio_wait();
141 static const AIOCBInfo iscsi_aiocb_info = {
142 .aiocb_size = sizeof(IscsiAIOCB),
143 .cancel = iscsi_aio_cancel,
147 static void iscsi_process_read(void *arg);
148 static void iscsi_process_write(void *arg);
150 static int iscsi_process_flush(void *arg)
152 IscsiLun *iscsilun = arg;
154 return iscsi_queue_length(iscsilun->iscsi) > 0;
157 static void
158 iscsi_set_events(IscsiLun *iscsilun)
160 struct iscsi_context *iscsi = iscsilun->iscsi;
161 int ev;
163 /* We always register a read handler. */
164 ev = POLLIN;
165 ev |= iscsi_which_events(iscsi);
166 if (ev != iscsilun->events) {
167 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
168 iscsi_process_read,
169 (ev & POLLOUT) ? iscsi_process_write : NULL,
170 iscsi_process_flush,
171 iscsilun);
175 iscsilun->events = ev;
178 static void
179 iscsi_process_read(void *arg)
181 IscsiLun *iscsilun = arg;
182 struct iscsi_context *iscsi = iscsilun->iscsi;
184 iscsi_service(iscsi, POLLIN);
185 iscsi_set_events(iscsilun);
188 static void
189 iscsi_process_write(void *arg)
191 IscsiLun *iscsilun = arg;
192 struct iscsi_context *iscsi = iscsilun->iscsi;
194 iscsi_service(iscsi, POLLOUT);
195 iscsi_set_events(iscsilun);
198 static int
199 iscsi_aio_writev_acb(IscsiAIOCB *acb);
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);
210 acb->buf = NULL;
212 if (acb->canceled != 0) {
213 return;
216 acb->status = 0;
217 if (status != 0) {
218 if (status == SCSI_STATUS_CHECK_CONDITION
219 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
220 && acb->retries-- > 0) {
221 scsi_free_scsi_task(acb->task);
222 acb->task = NULL;
223 if (iscsi_aio_writev_acb(acb) == 0) {
224 iscsi_set_events(acb->iscsilun);
225 return;
228 error_report("Failed to write16 data to iSCSI lun. %s",
229 iscsi_get_error(iscsi));
230 acb->status = -EIO;
233 iscsi_schedule_bh(acb);
236 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
238 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
241 static int
242 iscsi_aio_writev_acb(IscsiAIOCB *acb)
244 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
245 size_t size;
246 uint32_t num_sectors;
247 uint64_t lba;
248 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
249 struct iscsi_data data;
250 #endif
251 int ret;
253 acb->canceled = 0;
254 acb->bh = NULL;
255 acb->status = -EINPROGRESS;
256 acb->buf = NULL;
258 /* this will allow us to get rid of 'buf' completely */
259 size = acb->nb_sectors * BDRV_SECTOR_SIZE;
261 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
262 data.size = MIN(size, acb->qiov->size);
264 /* if the iovec only contains one buffer we can pass it directly */
265 if (acb->qiov->niov == 1) {
266 data.data = acb->qiov->iov[0].iov_base;
267 } else {
268 acb->buf = g_malloc(data.size);
269 qemu_iovec_to_buf(acb->qiov, 0, acb->buf, data.size);
270 data.data = acb->buf;
272 #endif
274 acb->task = malloc(sizeof(struct scsi_task));
275 if (acb->task == NULL) {
276 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
277 "command. %s", iscsi_get_error(iscsi));
278 return -1;
280 memset(acb->task, 0, sizeof(struct scsi_task));
282 acb->task->xfer_dir = SCSI_XFER_WRITE;
283 acb->task->cdb_size = 16;
284 acb->task->cdb[0] = 0x8a;
285 lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
286 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
287 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
288 num_sectors = size / acb->iscsilun->block_size;
289 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
290 acb->task->expxferlen = size;
292 #if defined(LIBISCSI_FEATURE_IOVECTOR)
293 ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
294 iscsi_aio_write16_cb,
295 NULL,
296 acb);
297 #else
298 ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
299 iscsi_aio_write16_cb,
300 &data,
301 acb);
302 #endif
303 if (ret != 0) {
304 scsi_free_scsi_task(acb->task);
305 g_free(acb->buf);
306 return -1;
309 #if defined(LIBISCSI_FEATURE_IOVECTOR)
310 scsi_task_set_iov_out(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
311 #endif
313 return 0;
316 static BlockDriverAIOCB *
317 iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
318 QEMUIOVector *qiov, int nb_sectors,
319 BlockDriverCompletionFunc *cb,
320 void *opaque)
322 IscsiLun *iscsilun = bs->opaque;
323 IscsiAIOCB *acb;
325 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
326 trace_iscsi_aio_writev(iscsilun->iscsi, sector_num, nb_sectors, opaque, acb);
328 acb->iscsilun = iscsilun;
329 acb->qiov = qiov;
330 acb->nb_sectors = nb_sectors;
331 acb->sector_num = sector_num;
332 acb->retries = ISCSI_CMD_RETRIES;
334 if (iscsi_aio_writev_acb(acb) != 0) {
335 qemu_aio_release(acb);
336 return NULL;
339 iscsi_set_events(iscsilun);
340 return &acb->common;
343 static int
344 iscsi_aio_readv_acb(IscsiAIOCB *acb);
346 static void
347 iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
348 void *command_data, void *opaque)
350 IscsiAIOCB *acb = opaque;
352 trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
354 if (acb->canceled != 0) {
355 return;
358 acb->status = 0;
359 if (status != 0) {
360 if (status == SCSI_STATUS_CHECK_CONDITION
361 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
362 && acb->retries-- > 0) {
363 scsi_free_scsi_task(acb->task);
364 acb->task = NULL;
365 if (iscsi_aio_readv_acb(acb) == 0) {
366 iscsi_set_events(acb->iscsilun);
367 return;
370 error_report("Failed to read16 data from iSCSI lun. %s",
371 iscsi_get_error(iscsi));
372 acb->status = -EIO;
375 iscsi_schedule_bh(acb);
378 static int
379 iscsi_aio_readv_acb(IscsiAIOCB *acb)
381 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
382 uint64_t lba;
383 uint32_t num_sectors;
384 int ret;
385 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
386 int i;
387 #endif
389 acb->canceled = 0;
390 acb->bh = NULL;
391 acb->status = -EINPROGRESS;
392 acb->buf = NULL;
394 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
395 * may be misaligned to the LUN, so we may need to read some extra
396 * data.
398 acb->read_offset = 0;
399 if (acb->iscsilun->block_size > BDRV_SECTOR_SIZE) {
400 uint64_t bdrv_offset = BDRV_SECTOR_SIZE * acb->sector_num;
402 acb->read_offset = bdrv_offset % acb->iscsilun->block_size;
405 num_sectors = (acb->read_size + acb->iscsilun->block_size
406 + acb->read_offset - 1)
407 / acb->iscsilun->block_size;
409 acb->task = malloc(sizeof(struct scsi_task));
410 if (acb->task == NULL) {
411 error_report("iSCSI: Failed to allocate task for scsi READ16 "
412 "command. %s", iscsi_get_error(iscsi));
413 return -1;
415 memset(acb->task, 0, sizeof(struct scsi_task));
417 acb->task->xfer_dir = SCSI_XFER_READ;
418 lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
419 acb->task->expxferlen = acb->read_size;
421 switch (acb->iscsilun->type) {
422 case TYPE_DISK:
423 acb->task->cdb_size = 16;
424 acb->task->cdb[0] = 0x88;
425 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
426 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
427 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
428 break;
429 default:
430 acb->task->cdb_size = 10;
431 acb->task->cdb[0] = 0x28;
432 *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
433 *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
434 break;
437 ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
438 iscsi_aio_read16_cb,
439 NULL,
440 acb);
441 if (ret != 0) {
442 scsi_free_scsi_task(acb->task);
443 return -1;
446 #if defined(LIBISCSI_FEATURE_IOVECTOR)
447 scsi_task_set_iov_in(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
448 #else
449 for (i = 0; i < acb->qiov->niov; i++) {
450 scsi_task_add_data_in_buffer(acb->task,
451 acb->qiov->iov[i].iov_len,
452 acb->qiov->iov[i].iov_base);
454 #endif
455 return 0;
458 static BlockDriverAIOCB *
459 iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
460 QEMUIOVector *qiov, int nb_sectors,
461 BlockDriverCompletionFunc *cb,
462 void *opaque)
464 IscsiLun *iscsilun = bs->opaque;
465 IscsiAIOCB *acb;
467 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
468 trace_iscsi_aio_readv(iscsilun->iscsi, sector_num, nb_sectors, opaque, acb);
470 acb->nb_sectors = nb_sectors;
471 acb->sector_num = sector_num;
472 acb->iscsilun = iscsilun;
473 acb->qiov = qiov;
474 acb->read_size = BDRV_SECTOR_SIZE * (size_t)acb->nb_sectors;
475 acb->retries = ISCSI_CMD_RETRIES;
477 if (iscsi_aio_readv_acb(acb) != 0) {
478 qemu_aio_release(acb);
479 return NULL;
482 iscsi_set_events(iscsilun);
483 return &acb->common;
486 static int
487 iscsi_aio_flush_acb(IscsiAIOCB *acb);
489 static void
490 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
491 void *command_data, void *opaque)
493 IscsiAIOCB *acb = opaque;
495 if (acb->canceled != 0) {
496 return;
499 acb->status = 0;
500 if (status != 0) {
501 if (status == SCSI_STATUS_CHECK_CONDITION
502 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
503 && acb->retries-- > 0) {
504 scsi_free_scsi_task(acb->task);
505 acb->task = NULL;
506 if (iscsi_aio_flush_acb(acb) == 0) {
507 iscsi_set_events(acb->iscsilun);
508 return;
511 error_report("Failed to sync10 data on iSCSI lun. %s",
512 iscsi_get_error(iscsi));
513 acb->status = -EIO;
516 iscsi_schedule_bh(acb);
519 static int
520 iscsi_aio_flush_acb(IscsiAIOCB *acb)
522 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
524 acb->canceled = 0;
525 acb->bh = NULL;
526 acb->status = -EINPROGRESS;
527 acb->buf = NULL;
529 acb->task = iscsi_synchronizecache10_task(iscsi, acb->iscsilun->lun,
530 0, 0, 0, 0,
531 iscsi_synccache10_cb,
532 acb);
533 if (acb->task == NULL) {
534 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
535 iscsi_get_error(iscsi));
536 return -1;
539 return 0;
542 static BlockDriverAIOCB *
543 iscsi_aio_flush(BlockDriverState *bs,
544 BlockDriverCompletionFunc *cb, void *opaque)
546 IscsiLun *iscsilun = bs->opaque;
548 IscsiAIOCB *acb;
550 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
552 acb->iscsilun = iscsilun;
553 acb->retries = ISCSI_CMD_RETRIES;
555 if (iscsi_aio_flush_acb(acb) != 0) {
556 qemu_aio_release(acb);
557 return NULL;
560 iscsi_set_events(iscsilun);
562 return &acb->common;
565 static int iscsi_aio_discard_acb(IscsiAIOCB *acb);
567 static void
568 iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
569 void *command_data, void *opaque)
571 IscsiAIOCB *acb = opaque;
573 if (acb->canceled != 0) {
574 return;
577 acb->status = 0;
578 if (status != 0) {
579 if (status == SCSI_STATUS_CHECK_CONDITION
580 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
581 && acb->retries-- > 0) {
582 scsi_free_scsi_task(acb->task);
583 acb->task = NULL;
584 if (iscsi_aio_discard_acb(acb) == 0) {
585 iscsi_set_events(acb->iscsilun);
586 return;
589 error_report("Failed to unmap data on iSCSI lun. %s",
590 iscsi_get_error(iscsi));
591 acb->status = -EIO;
594 iscsi_schedule_bh(acb);
597 static int iscsi_aio_discard_acb(IscsiAIOCB *acb) {
598 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
599 struct unmap_list list[1];
601 acb->canceled = 0;
602 acb->bh = NULL;
603 acb->status = -EINPROGRESS;
604 acb->buf = NULL;
606 list[0].lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
607 list[0].num = acb->nb_sectors * BDRV_SECTOR_SIZE / acb->iscsilun->block_size;
609 acb->task = iscsi_unmap_task(iscsi, acb->iscsilun->lun,
610 0, 0, &list[0], 1,
611 iscsi_unmap_cb,
612 acb);
613 if (acb->task == NULL) {
614 error_report("iSCSI: Failed to send unmap command. %s",
615 iscsi_get_error(iscsi));
616 return -1;
619 return 0;
622 static BlockDriverAIOCB *
623 iscsi_aio_discard(BlockDriverState *bs,
624 int64_t sector_num, int nb_sectors,
625 BlockDriverCompletionFunc *cb, void *opaque)
627 IscsiLun *iscsilun = bs->opaque;
628 IscsiAIOCB *acb;
630 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
632 acb->iscsilun = iscsilun;
633 acb->nb_sectors = nb_sectors;
634 acb->sector_num = sector_num;
635 acb->retries = ISCSI_CMD_RETRIES;
637 if (iscsi_aio_discard_acb(acb) != 0) {
638 qemu_aio_release(acb);
639 return NULL;
642 iscsi_set_events(iscsilun);
644 return &acb->common;
647 #ifdef __linux__
648 static void
649 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
650 void *command_data, void *opaque)
652 IscsiAIOCB *acb = opaque;
654 if (acb->canceled != 0) {
655 return;
658 acb->status = 0;
659 if (status < 0) {
660 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
661 iscsi_get_error(iscsi));
662 acb->status = -EIO;
665 acb->ioh->driver_status = 0;
666 acb->ioh->host_status = 0;
667 acb->ioh->resid = 0;
669 #define SG_ERR_DRIVER_SENSE 0x08
671 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
672 int ss;
674 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
676 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
677 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
678 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
679 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
682 iscsi_schedule_bh(acb);
685 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
686 unsigned long int req, void *buf,
687 BlockDriverCompletionFunc *cb, void *opaque)
689 IscsiLun *iscsilun = bs->opaque;
690 struct iscsi_context *iscsi = iscsilun->iscsi;
691 struct iscsi_data data;
692 IscsiAIOCB *acb;
694 assert(req == SG_IO);
696 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
698 acb->iscsilun = iscsilun;
699 acb->canceled = 0;
700 acb->bh = NULL;
701 acb->status = -EINPROGRESS;
702 acb->buf = NULL;
703 acb->ioh = buf;
705 acb->task = malloc(sizeof(struct scsi_task));
706 if (acb->task == NULL) {
707 error_report("iSCSI: Failed to allocate task for scsi command. %s",
708 iscsi_get_error(iscsi));
709 qemu_aio_release(acb);
710 return NULL;
712 memset(acb->task, 0, sizeof(struct scsi_task));
714 switch (acb->ioh->dxfer_direction) {
715 case SG_DXFER_TO_DEV:
716 acb->task->xfer_dir = SCSI_XFER_WRITE;
717 break;
718 case SG_DXFER_FROM_DEV:
719 acb->task->xfer_dir = SCSI_XFER_READ;
720 break;
721 default:
722 acb->task->xfer_dir = SCSI_XFER_NONE;
723 break;
726 acb->task->cdb_size = acb->ioh->cmd_len;
727 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
728 acb->task->expxferlen = acb->ioh->dxfer_len;
730 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
731 data.data = acb->ioh->dxferp;
732 data.size = acb->ioh->dxfer_len;
734 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
735 iscsi_aio_ioctl_cb,
736 (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
737 &data : NULL,
738 acb) != 0) {
739 scsi_free_scsi_task(acb->task);
740 qemu_aio_release(acb);
741 return NULL;
744 /* tell libiscsi to read straight into the buffer we got from ioctl */
745 if (acb->task->xfer_dir == SCSI_XFER_READ) {
746 scsi_task_add_data_in_buffer(acb->task,
747 acb->ioh->dxfer_len,
748 acb->ioh->dxferp);
751 iscsi_set_events(iscsilun);
753 return &acb->common;
757 static void ioctl_cb(void *opaque, int status)
759 int *p_status = opaque;
760 *p_status = status;
763 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
765 IscsiLun *iscsilun = bs->opaque;
766 int status;
768 switch (req) {
769 case SG_GET_VERSION_NUM:
770 *(int *)buf = 30000;
771 break;
772 case SG_GET_SCSI_ID:
773 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
774 break;
775 case SG_IO:
776 status = -EINPROGRESS;
777 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
779 while (status == -EINPROGRESS) {
780 qemu_aio_wait();
783 return 0;
784 default:
785 return -1;
787 return 0;
789 #endif
791 static int64_t
792 iscsi_getlength(BlockDriverState *bs)
794 IscsiLun *iscsilun = bs->opaque;
795 int64_t len;
797 len = iscsilun->num_blocks;
798 len *= iscsilun->block_size;
800 return len;
803 static int parse_chap(struct iscsi_context *iscsi, const char *target)
805 QemuOptsList *list;
806 QemuOpts *opts;
807 const char *user = NULL;
808 const char *password = NULL;
810 list = qemu_find_opts("iscsi");
811 if (!list) {
812 return 0;
815 opts = qemu_opts_find(list, target);
816 if (opts == NULL) {
817 opts = QTAILQ_FIRST(&list->head);
818 if (!opts) {
819 return 0;
823 user = qemu_opt_get(opts, "user");
824 if (!user) {
825 return 0;
828 password = qemu_opt_get(opts, "password");
829 if (!password) {
830 error_report("CHAP username specified but no password was given");
831 return -1;
834 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
835 error_report("Failed to set initiator username and password");
836 return -1;
839 return 0;
842 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
844 QemuOptsList *list;
845 QemuOpts *opts;
846 const char *digest = NULL;
848 list = qemu_find_opts("iscsi");
849 if (!list) {
850 return;
853 opts = qemu_opts_find(list, target);
854 if (opts == NULL) {
855 opts = QTAILQ_FIRST(&list->head);
856 if (!opts) {
857 return;
861 digest = qemu_opt_get(opts, "header-digest");
862 if (!digest) {
863 return;
866 if (!strcmp(digest, "CRC32C")) {
867 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
868 } else if (!strcmp(digest, "NONE")) {
869 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
870 } else if (!strcmp(digest, "CRC32C-NONE")) {
871 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
872 } else if (!strcmp(digest, "NONE-CRC32C")) {
873 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
874 } else {
875 error_report("Invalid header-digest setting : %s", digest);
879 static char *parse_initiator_name(const char *target)
881 QemuOptsList *list;
882 QemuOpts *opts;
883 const char *name = NULL;
884 const char *iscsi_name = qemu_get_vm_name();
886 list = qemu_find_opts("iscsi");
887 if (list) {
888 opts = qemu_opts_find(list, target);
889 if (!opts) {
890 opts = QTAILQ_FIRST(&list->head);
892 if (opts) {
893 name = qemu_opt_get(opts, "initiator-name");
897 if (name) {
898 return g_strdup(name);
899 } else {
900 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
901 iscsi_name ? ":" : "",
902 iscsi_name ? iscsi_name : "");
906 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
907 static void iscsi_nop_timed_event(void *opaque)
909 IscsiLun *iscsilun = opaque;
911 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
912 error_report("iSCSI: NOP timeout. Reconnecting...");
913 iscsi_reconnect(iscsilun->iscsi);
916 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
917 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
918 return;
921 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
922 iscsi_set_events(iscsilun);
924 #endif
926 static int iscsi_readcapacity_sync(IscsiLun *iscsilun)
928 struct scsi_task *task = NULL;
929 struct scsi_readcapacity10 *rc10 = NULL;
930 struct scsi_readcapacity16 *rc16 = NULL;
931 int ret = 0;
932 int retries = ISCSI_CMD_RETRIES;
934 do {
935 if (task != NULL) {
936 scsi_free_scsi_task(task);
937 task = NULL;
940 switch (iscsilun->type) {
941 case TYPE_DISK:
942 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
943 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
944 rc16 = scsi_datain_unmarshall(task);
945 if (rc16 == NULL) {
946 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
947 ret = -EINVAL;
948 } else {
949 iscsilun->block_size = rc16->block_length;
950 iscsilun->num_blocks = rc16->returned_lba + 1;
953 break;
954 case TYPE_ROM:
955 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
956 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
957 rc10 = scsi_datain_unmarshall(task);
958 if (rc10 == NULL) {
959 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
960 ret = -EINVAL;
961 } else {
962 iscsilun->block_size = rc10->block_size;
963 if (rc10->lba == 0) {
964 /* blank disk loaded */
965 iscsilun->num_blocks = 0;
966 } else {
967 iscsilun->num_blocks = rc10->lba + 1;
971 break;
972 default:
973 return 0;
975 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
976 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
977 && retries-- > 0);
979 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
980 error_report("iSCSI: failed to send readcapacity10 command.");
981 ret = -EINVAL;
983 if (task) {
984 scsi_free_scsi_task(task);
986 return ret;
989 /* TODO Convert to fine grained options */
990 static QemuOptsList runtime_opts = {
991 .name = "iscsi",
992 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
993 .desc = {
995 .name = "filename",
996 .type = QEMU_OPT_STRING,
997 .help = "URL to the iscsi image",
999 { /* end of list */ }
1004 * We support iscsi url's on the form
1005 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1007 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags)
1009 IscsiLun *iscsilun = bs->opaque;
1010 struct iscsi_context *iscsi = NULL;
1011 struct iscsi_url *iscsi_url = NULL;
1012 struct scsi_task *task = NULL;
1013 struct scsi_inquiry_standard *inq = NULL;
1014 char *initiator_name = NULL;
1015 QemuOpts *opts;
1016 Error *local_err = NULL;
1017 const char *filename;
1018 int ret;
1020 if ((BDRV_SECTOR_SIZE % 512) != 0) {
1021 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1022 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1023 "of 512", BDRV_SECTOR_SIZE);
1024 return -EINVAL;
1027 opts = qemu_opts_create_nofail(&runtime_opts);
1028 qemu_opts_absorb_qdict(opts, options, &local_err);
1029 if (error_is_set(&local_err)) {
1030 qerror_report_err(local_err);
1031 error_free(local_err);
1032 ret = -EINVAL;
1033 goto out;
1036 filename = qemu_opt_get(opts, "filename");
1039 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1040 if (iscsi_url == NULL) {
1041 error_report("Failed to parse URL : %s", filename);
1042 ret = -EINVAL;
1043 goto out;
1046 memset(iscsilun, 0, sizeof(IscsiLun));
1048 initiator_name = parse_initiator_name(iscsi_url->target);
1050 iscsi = iscsi_create_context(initiator_name);
1051 if (iscsi == NULL) {
1052 error_report("iSCSI: Failed to create iSCSI context.");
1053 ret = -ENOMEM;
1054 goto out;
1057 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1058 error_report("iSCSI: Failed to set target name.");
1059 ret = -EINVAL;
1060 goto out;
1063 if (iscsi_url->user != NULL) {
1064 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1065 iscsi_url->passwd);
1066 if (ret != 0) {
1067 error_report("Failed to set initiator username and password");
1068 ret = -EINVAL;
1069 goto out;
1073 /* check if we got CHAP username/password via the options */
1074 if (parse_chap(iscsi, iscsi_url->target) != 0) {
1075 error_report("iSCSI: Failed to set CHAP user/password");
1076 ret = -EINVAL;
1077 goto out;
1080 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1081 error_report("iSCSI: Failed to set session type to normal.");
1082 ret = -EINVAL;
1083 goto out;
1086 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1088 /* check if we got HEADER_DIGEST via the options */
1089 parse_header_digest(iscsi, iscsi_url->target);
1091 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1092 error_report("iSCSI: Failed to connect to LUN : %s",
1093 iscsi_get_error(iscsi));
1094 ret = -EINVAL;
1095 goto out;
1098 iscsilun->iscsi = iscsi;
1099 iscsilun->lun = iscsi_url->lun;
1101 task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
1103 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1104 error_report("iSCSI: failed to send inquiry command.");
1105 ret = -EINVAL;
1106 goto out;
1109 inq = scsi_datain_unmarshall(task);
1110 if (inq == NULL) {
1111 error_report("iSCSI: Failed to unmarshall inquiry data.");
1112 ret = -EINVAL;
1113 goto out;
1116 iscsilun->type = inq->periperal_device_type;
1118 if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1119 goto out;
1121 bs->total_sectors = iscsilun->num_blocks *
1122 iscsilun->block_size / BDRV_SECTOR_SIZE ;
1124 /* Medium changer or tape. We dont have any emulation for this so this must
1125 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1126 * to read from the device to guess the image format.
1128 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1129 iscsilun->type == TYPE_TAPE) {
1130 bs->sg = 1;
1133 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1134 /* Set up a timer for sending out iSCSI NOPs */
1135 iscsilun->nop_timer = qemu_new_timer_ms(rt_clock, iscsi_nop_timed_event, iscsilun);
1136 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
1137 #endif
1139 out:
1140 qemu_opts_del(opts);
1141 if (initiator_name != NULL) {
1142 g_free(initiator_name);
1144 if (iscsi_url != NULL) {
1145 iscsi_destroy_url(iscsi_url);
1147 if (task != NULL) {
1148 scsi_free_scsi_task(task);
1151 if (ret) {
1152 if (iscsi != NULL) {
1153 iscsi_destroy_context(iscsi);
1155 memset(iscsilun, 0, sizeof(IscsiLun));
1157 return ret;
1160 static void iscsi_close(BlockDriverState *bs)
1162 IscsiLun *iscsilun = bs->opaque;
1163 struct iscsi_context *iscsi = iscsilun->iscsi;
1165 if (iscsilun->nop_timer) {
1166 qemu_del_timer(iscsilun->nop_timer);
1167 qemu_free_timer(iscsilun->nop_timer);
1169 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
1170 iscsi_destroy_context(iscsi);
1171 memset(iscsilun, 0, sizeof(IscsiLun));
1174 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1176 IscsiLun *iscsilun = bs->opaque;
1177 int ret = 0;
1179 if (iscsilun->type != TYPE_DISK) {
1180 return -ENOTSUP;
1183 if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1184 return ret;
1187 if (offset > iscsi_getlength(bs)) {
1188 return -EINVAL;
1191 return 0;
1194 static int iscsi_has_zero_init(BlockDriverState *bs)
1196 return 0;
1199 static int iscsi_create(const char *filename, QEMUOptionParameter *options)
1201 int ret = 0;
1202 int64_t total_size = 0;
1203 BlockDriverState bs;
1204 IscsiLun *iscsilun = NULL;
1205 QDict *bs_options;
1207 memset(&bs, 0, sizeof(BlockDriverState));
1209 /* Read out options */
1210 while (options && options->name) {
1211 if (!strcmp(options->name, "size")) {
1212 total_size = options->value.n / BDRV_SECTOR_SIZE;
1214 options++;
1217 bs.opaque = g_malloc0(sizeof(struct IscsiLun));
1218 iscsilun = bs.opaque;
1220 bs_options = qdict_new();
1221 qdict_put(bs_options, "filename", qstring_from_str(filename));
1222 ret = iscsi_open(&bs, bs_options, 0);
1223 QDECREF(bs_options);
1225 if (ret != 0) {
1226 goto out;
1228 if (iscsilun->nop_timer) {
1229 qemu_del_timer(iscsilun->nop_timer);
1230 qemu_free_timer(iscsilun->nop_timer);
1232 if (iscsilun->type != TYPE_DISK) {
1233 ret = -ENODEV;
1234 goto out;
1236 if (bs.total_sectors < total_size) {
1237 ret = -ENOSPC;
1240 ret = 0;
1241 out:
1242 if (iscsilun->iscsi != NULL) {
1243 iscsi_destroy_context(iscsilun->iscsi);
1245 g_free(bs.opaque);
1246 return ret;
1249 static QEMUOptionParameter iscsi_create_options[] = {
1251 .name = BLOCK_OPT_SIZE,
1252 .type = OPT_SIZE,
1253 .help = "Virtual disk size"
1255 { NULL }
1258 static BlockDriver bdrv_iscsi = {
1259 .format_name = "iscsi",
1260 .protocol_name = "iscsi",
1262 .instance_size = sizeof(IscsiLun),
1263 .bdrv_file_open = iscsi_open,
1264 .bdrv_close = iscsi_close,
1265 .bdrv_create = iscsi_create,
1266 .create_options = iscsi_create_options,
1268 .bdrv_getlength = iscsi_getlength,
1269 .bdrv_truncate = iscsi_truncate,
1271 .bdrv_aio_readv = iscsi_aio_readv,
1272 .bdrv_aio_writev = iscsi_aio_writev,
1273 .bdrv_aio_flush = iscsi_aio_flush,
1275 .bdrv_aio_discard = iscsi_aio_discard,
1276 .bdrv_has_zero_init = iscsi_has_zero_init,
1278 #ifdef __linux__
1279 .bdrv_ioctl = iscsi_ioctl,
1280 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1281 #endif
1284 static QemuOptsList qemu_iscsi_opts = {
1285 .name = "iscsi",
1286 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1287 .desc = {
1289 .name = "user",
1290 .type = QEMU_OPT_STRING,
1291 .help = "username for CHAP authentication to target",
1293 .name = "password",
1294 .type = QEMU_OPT_STRING,
1295 .help = "password for CHAP authentication to target",
1297 .name = "header-digest",
1298 .type = QEMU_OPT_STRING,
1299 .help = "HeaderDigest setting. "
1300 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1302 .name = "initiator-name",
1303 .type = QEMU_OPT_STRING,
1304 .help = "Initiator iqn name to use when connecting",
1306 { /* end of list */ }
1310 static void iscsi_block_init(void)
1312 bdrv_register(&bdrv_iscsi);
1313 qemu_add_opts(&qemu_iscsi_opts);
1316 block_init(iscsi_block_init);