target-arm: aarch64: add support for ld lit
[qemu/cris-port.git] / block / iscsi.c
blobfa69408df94851b47ad5ad539fbb9a4122342067
1 /*
2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2013 Peter Lieven <pl@kamp.de>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "config-host.h"
28 #include <poll.h>
29 #include <arpa/inet.h>
30 #include "qemu-common.h"
31 #include "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "block/block_int.h"
34 #include "trace.h"
35 #include "block/scsi.h"
36 #include "qemu/iov.h"
37 #include "sysemu/sysemu.h"
38 #include "qmp-commands.h"
40 #include <iscsi/iscsi.h>
41 #include <iscsi/scsi-lowlevel.h>
43 #ifdef __linux__
44 #include <scsi/sg.h>
45 #include <block/scsi.h>
46 #endif
48 typedef struct IscsiLun {
49 struct iscsi_context *iscsi;
50 int lun;
51 enum scsi_inquiry_peripheral_device_type type;
52 int block_size;
53 uint64_t num_blocks;
54 int events;
55 QEMUTimer *nop_timer;
56 uint8_t lbpme;
57 uint8_t lbprz;
58 uint8_t has_write_same;
59 struct scsi_inquiry_logical_block_provisioning lbp;
60 struct scsi_inquiry_block_limits bl;
61 unsigned char *zeroblock;
62 } IscsiLun;
64 typedef struct IscsiTask {
65 int status;
66 int complete;
67 int retries;
68 int do_retry;
69 struct scsi_task *task;
70 Coroutine *co;
71 } IscsiTask;
73 typedef struct IscsiAIOCB {
74 BlockDriverAIOCB common;
75 QEMUIOVector *qiov;
76 QEMUBH *bh;
77 IscsiLun *iscsilun;
78 struct scsi_task *task;
79 uint8_t *buf;
80 int status;
81 int canceled;
82 int retries;
83 int64_t sector_num;
84 int nb_sectors;
85 #ifdef __linux__
86 sg_io_hdr_t *ioh;
87 #endif
88 } IscsiAIOCB;
90 #define NOP_INTERVAL 5000
91 #define MAX_NOP_FAILURES 3
92 #define ISCSI_CMD_RETRIES 5
94 static void
95 iscsi_bh_cb(void *p)
97 IscsiAIOCB *acb = p;
99 qemu_bh_delete(acb->bh);
101 g_free(acb->buf);
102 acb->buf = NULL;
104 if (acb->canceled == 0) {
105 acb->common.cb(acb->common.opaque, acb->status);
108 if (acb->task != NULL) {
109 scsi_free_scsi_task(acb->task);
110 acb->task = NULL;
113 qemu_aio_release(acb);
116 static void
117 iscsi_schedule_bh(IscsiAIOCB *acb)
119 if (acb->bh) {
120 return;
122 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
123 qemu_bh_schedule(acb->bh);
126 static void
127 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
128 void *command_data, void *opaque)
130 struct IscsiTask *iTask = opaque;
131 struct scsi_task *task = command_data;
133 iTask->complete = 1;
134 iTask->status = status;
135 iTask->do_retry = 0;
136 iTask->task = task;
138 if (iTask->retries-- > 0 && status == SCSI_STATUS_CHECK_CONDITION
139 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
140 iTask->do_retry = 1;
141 goto out;
144 if (status != SCSI_STATUS_GOOD) {
145 error_report("iSCSI: Failure. %s", iscsi_get_error(iscsi));
148 out:
149 if (iTask->co) {
150 qemu_coroutine_enter(iTask->co, NULL);
154 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
156 *iTask = (struct IscsiTask) {
157 .co = qemu_coroutine_self(),
158 .retries = ISCSI_CMD_RETRIES,
162 static void
163 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
164 void *private_data)
166 IscsiAIOCB *acb = private_data;
168 acb->status = -ECANCELED;
169 iscsi_schedule_bh(acb);
172 static void
173 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
175 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
176 IscsiLun *iscsilun = acb->iscsilun;
178 if (acb->status != -EINPROGRESS) {
179 return;
182 acb->canceled = 1;
184 /* send a task mgmt call to the target to cancel the task on the target */
185 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
186 iscsi_abort_task_cb, acb);
188 while (acb->status == -EINPROGRESS) {
189 qemu_aio_wait();
193 static const AIOCBInfo iscsi_aiocb_info = {
194 .aiocb_size = sizeof(IscsiAIOCB),
195 .cancel = iscsi_aio_cancel,
199 static void iscsi_process_read(void *arg);
200 static void iscsi_process_write(void *arg);
202 static void
203 iscsi_set_events(IscsiLun *iscsilun)
205 struct iscsi_context *iscsi = iscsilun->iscsi;
206 int ev;
208 /* We always register a read handler. */
209 ev = POLLIN;
210 ev |= iscsi_which_events(iscsi);
211 if (ev != iscsilun->events) {
212 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
213 iscsi_process_read,
214 (ev & POLLOUT) ? iscsi_process_write : NULL,
215 iscsilun);
219 iscsilun->events = ev;
222 static void
223 iscsi_process_read(void *arg)
225 IscsiLun *iscsilun = arg;
226 struct iscsi_context *iscsi = iscsilun->iscsi;
228 iscsi_service(iscsi, POLLIN);
229 iscsi_set_events(iscsilun);
232 static void
233 iscsi_process_write(void *arg)
235 IscsiLun *iscsilun = arg;
236 struct iscsi_context *iscsi = iscsilun->iscsi;
238 iscsi_service(iscsi, POLLOUT);
239 iscsi_set_events(iscsilun);
242 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
244 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
247 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
249 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
252 static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
253 IscsiLun *iscsilun)
255 if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
256 (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
257 error_report("iSCSI misaligned request: "
258 "iscsilun->block_size %u, sector_num %" PRIi64
259 ", nb_sectors %d",
260 iscsilun->block_size, sector_num, nb_sectors);
261 return 0;
263 return 1;
266 static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,
267 int64_t sector_num, int nb_sectors,
268 QEMUIOVector *iov)
270 IscsiLun *iscsilun = bs->opaque;
271 struct IscsiTask iTask;
272 uint64_t lba;
273 uint32_t num_sectors;
274 uint8_t *data = NULL;
275 uint8_t *buf = NULL;
277 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
278 return -EINVAL;
281 lba = sector_qemu2lun(sector_num, iscsilun);
282 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
283 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
284 /* if the iovec only contains one buffer we can pass it directly */
285 if (iov->niov == 1) {
286 data = iov->iov[0].iov_base;
287 } else {
288 size_t size = MIN(nb_sectors * BDRV_SECTOR_SIZE, iov->size);
289 buf = g_malloc(size);
290 qemu_iovec_to_buf(iov, 0, buf, size);
291 data = buf;
293 #endif
294 iscsi_co_init_iscsitask(iscsilun, &iTask);
295 retry:
296 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
297 data, num_sectors * iscsilun->block_size,
298 iscsilun->block_size, 0, 0, 0, 0, 0,
299 iscsi_co_generic_cb, &iTask);
300 if (iTask.task == NULL) {
301 g_free(buf);
302 return -EIO;
304 #if defined(LIBISCSI_FEATURE_IOVECTOR)
305 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
306 iov->niov);
307 #endif
308 while (!iTask.complete) {
309 iscsi_set_events(iscsilun);
310 qemu_coroutine_yield();
313 if (iTask.task != NULL) {
314 scsi_free_scsi_task(iTask.task);
315 iTask.task = NULL;
318 if (iTask.do_retry) {
319 goto retry;
322 g_free(buf);
324 if (iTask.status != SCSI_STATUS_GOOD) {
325 return -EIO;
328 return 0;
331 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
332 int64_t sector_num, int nb_sectors,
333 QEMUIOVector *iov)
335 IscsiLun *iscsilun = bs->opaque;
336 struct IscsiTask iTask;
337 uint64_t lba;
338 uint32_t num_sectors;
339 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
340 int i;
341 #endif
343 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
344 return -EINVAL;
347 lba = sector_qemu2lun(sector_num, iscsilun);
348 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
350 iscsi_co_init_iscsitask(iscsilun, &iTask);
351 retry:
352 switch (iscsilun->type) {
353 case TYPE_DISK:
354 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
355 num_sectors * iscsilun->block_size,
356 iscsilun->block_size, 0, 0, 0, 0, 0,
357 iscsi_co_generic_cb, &iTask);
358 break;
359 default:
360 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
361 num_sectors * iscsilun->block_size,
362 iscsilun->block_size, 0, 0, 0, 0, 0,
363 iscsi_co_generic_cb, &iTask);
364 break;
366 if (iTask.task == NULL) {
367 return -EIO;
369 #if defined(LIBISCSI_FEATURE_IOVECTOR)
370 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
371 #else
372 for (i = 0; i < iov->niov; i++) {
373 scsi_task_add_data_in_buffer(iTask.task,
374 iov->iov[i].iov_len,
375 iov->iov[i].iov_base);
377 #endif
379 while (!iTask.complete) {
380 iscsi_set_events(iscsilun);
381 qemu_coroutine_yield();
384 if (iTask.task != NULL) {
385 scsi_free_scsi_task(iTask.task);
386 iTask.task = NULL;
389 if (iTask.do_retry) {
390 goto retry;
393 if (iTask.status != SCSI_STATUS_GOOD) {
394 return -EIO;
397 return 0;
400 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
402 IscsiLun *iscsilun = bs->opaque;
403 struct IscsiTask iTask;
405 iscsi_co_init_iscsitask(iscsilun, &iTask);
407 retry:
408 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
409 0, iscsi_co_generic_cb, &iTask) == NULL) {
410 return -EIO;
413 while (!iTask.complete) {
414 iscsi_set_events(iscsilun);
415 qemu_coroutine_yield();
418 if (iTask.task != NULL) {
419 scsi_free_scsi_task(iTask.task);
420 iTask.task = NULL;
423 if (iTask.do_retry) {
424 goto retry;
427 if (iTask.status != SCSI_STATUS_GOOD) {
428 return -EIO;
431 return 0;
434 #ifdef __linux__
435 static void
436 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
437 void *command_data, void *opaque)
439 IscsiAIOCB *acb = opaque;
441 g_free(acb->buf);
442 acb->buf = NULL;
444 if (acb->canceled != 0) {
445 return;
448 acb->status = 0;
449 if (status < 0) {
450 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
451 iscsi_get_error(iscsi));
452 acb->status = -EIO;
455 acb->ioh->driver_status = 0;
456 acb->ioh->host_status = 0;
457 acb->ioh->resid = 0;
459 #define SG_ERR_DRIVER_SENSE 0x08
461 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
462 int ss;
464 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
466 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
467 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
468 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
469 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
472 iscsi_schedule_bh(acb);
475 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
476 unsigned long int req, void *buf,
477 BlockDriverCompletionFunc *cb, void *opaque)
479 IscsiLun *iscsilun = bs->opaque;
480 struct iscsi_context *iscsi = iscsilun->iscsi;
481 struct iscsi_data data;
482 IscsiAIOCB *acb;
484 assert(req == SG_IO);
486 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
488 acb->iscsilun = iscsilun;
489 acb->canceled = 0;
490 acb->bh = NULL;
491 acb->status = -EINPROGRESS;
492 acb->buf = NULL;
493 acb->ioh = buf;
495 acb->task = malloc(sizeof(struct scsi_task));
496 if (acb->task == NULL) {
497 error_report("iSCSI: Failed to allocate task for scsi command. %s",
498 iscsi_get_error(iscsi));
499 qemu_aio_release(acb);
500 return NULL;
502 memset(acb->task, 0, sizeof(struct scsi_task));
504 switch (acb->ioh->dxfer_direction) {
505 case SG_DXFER_TO_DEV:
506 acb->task->xfer_dir = SCSI_XFER_WRITE;
507 break;
508 case SG_DXFER_FROM_DEV:
509 acb->task->xfer_dir = SCSI_XFER_READ;
510 break;
511 default:
512 acb->task->xfer_dir = SCSI_XFER_NONE;
513 break;
516 acb->task->cdb_size = acb->ioh->cmd_len;
517 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
518 acb->task->expxferlen = acb->ioh->dxfer_len;
520 data.size = 0;
521 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
522 if (acb->ioh->iovec_count == 0) {
523 data.data = acb->ioh->dxferp;
524 data.size = acb->ioh->dxfer_len;
525 } else {
526 #if defined(LIBISCSI_FEATURE_IOVECTOR)
527 scsi_task_set_iov_out(acb->task,
528 (struct scsi_iovec *) acb->ioh->dxferp,
529 acb->ioh->iovec_count);
530 #else
531 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
533 acb->buf = g_malloc(acb->ioh->dxfer_len);
534 data.data = acb->buf;
535 data.size = iov_to_buf(iov, acb->ioh->iovec_count, 0,
536 acb->buf, acb->ioh->dxfer_len);
537 #endif
541 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
542 iscsi_aio_ioctl_cb,
543 (data.size > 0) ? &data : NULL,
544 acb) != 0) {
545 scsi_free_scsi_task(acb->task);
546 qemu_aio_release(acb);
547 return NULL;
550 /* tell libiscsi to read straight into the buffer we got from ioctl */
551 if (acb->task->xfer_dir == SCSI_XFER_READ) {
552 if (acb->ioh->iovec_count == 0) {
553 scsi_task_add_data_in_buffer(acb->task,
554 acb->ioh->dxfer_len,
555 acb->ioh->dxferp);
556 } else {
557 #if defined(LIBISCSI_FEATURE_IOVECTOR)
558 scsi_task_set_iov_in(acb->task,
559 (struct scsi_iovec *) acb->ioh->dxferp,
560 acb->ioh->iovec_count);
561 #else
562 int i;
563 for (i = 0; i < acb->ioh->iovec_count; i++) {
564 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
566 scsi_task_add_data_in_buffer(acb->task,
567 iov[i].iov_len,
568 iov[i].iov_base);
570 #endif
574 iscsi_set_events(iscsilun);
576 return &acb->common;
580 static void ioctl_cb(void *opaque, int status)
582 int *p_status = opaque;
583 *p_status = status;
586 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
588 IscsiLun *iscsilun = bs->opaque;
589 int status;
591 switch (req) {
592 case SG_GET_VERSION_NUM:
593 *(int *)buf = 30000;
594 break;
595 case SG_GET_SCSI_ID:
596 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
597 break;
598 case SG_IO:
599 status = -EINPROGRESS;
600 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
602 while (status == -EINPROGRESS) {
603 qemu_aio_wait();
606 return 0;
607 default:
608 return -1;
610 return 0;
612 #endif
614 static int64_t
615 iscsi_getlength(BlockDriverState *bs)
617 IscsiLun *iscsilun = bs->opaque;
618 int64_t len;
620 len = iscsilun->num_blocks;
621 len *= iscsilun->block_size;
623 return len;
626 #if defined(LIBISCSI_FEATURE_IOVECTOR)
628 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
629 int64_t sector_num,
630 int nb_sectors, int *pnum)
632 IscsiLun *iscsilun = bs->opaque;
633 struct scsi_get_lba_status *lbas = NULL;
634 struct scsi_lba_status_descriptor *lbasd = NULL;
635 struct IscsiTask iTask;
636 int64_t ret;
638 iscsi_co_init_iscsitask(iscsilun, &iTask);
640 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
641 ret = -EINVAL;
642 goto out;
645 /* default to all sectors allocated */
646 ret = BDRV_BLOCK_DATA;
647 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
648 *pnum = nb_sectors;
650 /* LUN does not support logical block provisioning */
651 if (iscsilun->lbpme == 0) {
652 goto out;
655 retry:
656 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
657 sector_qemu2lun(sector_num, iscsilun),
658 8 + 16, iscsi_co_generic_cb,
659 &iTask) == NULL) {
660 ret = -EIO;
661 goto out;
664 while (!iTask.complete) {
665 iscsi_set_events(iscsilun);
666 qemu_coroutine_yield();
669 if (iTask.do_retry) {
670 if (iTask.task != NULL) {
671 scsi_free_scsi_task(iTask.task);
672 iTask.task = NULL;
674 goto retry;
677 if (iTask.status != SCSI_STATUS_GOOD) {
678 /* in case the get_lba_status_callout fails (i.e.
679 * because the device is busy or the cmd is not
680 * supported) we pretend all blocks are allocated
681 * for backwards compatibility */
682 goto out;
685 lbas = scsi_datain_unmarshall(iTask.task);
686 if (lbas == NULL) {
687 ret = -EIO;
688 goto out;
691 lbasd = &lbas->descriptors[0];
693 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
694 ret = -EIO;
695 goto out;
698 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
699 if (*pnum > nb_sectors) {
700 *pnum = nb_sectors;
703 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
704 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
705 ret &= ~BDRV_BLOCK_DATA;
706 if (iscsilun->lbprz) {
707 ret |= BDRV_BLOCK_ZERO;
711 out:
712 if (iTask.task != NULL) {
713 scsi_free_scsi_task(iTask.task);
715 return ret;
718 #endif /* LIBISCSI_FEATURE_IOVECTOR */
720 static int
721 coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
722 int nb_sectors)
724 IscsiLun *iscsilun = bs->opaque;
725 struct IscsiTask iTask;
726 struct unmap_list list;
728 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
729 return -EINVAL;
732 if (!iscsilun->lbp.lbpu) {
733 /* UNMAP is not supported by the target */
734 return 0;
737 list.lba = sector_qemu2lun(sector_num, iscsilun);
738 list.num = sector_qemu2lun(nb_sectors, iscsilun);
740 iscsi_co_init_iscsitask(iscsilun, &iTask);
741 retry:
742 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
743 iscsi_co_generic_cb, &iTask) == NULL) {
744 return -EIO;
747 while (!iTask.complete) {
748 iscsi_set_events(iscsilun);
749 qemu_coroutine_yield();
752 if (iTask.task != NULL) {
753 scsi_free_scsi_task(iTask.task);
754 iTask.task = NULL;
757 if (iTask.do_retry) {
758 goto retry;
761 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
762 /* the target might fail with a check condition if it
763 is not happy with the alignment of the UNMAP request
764 we silently fail in this case */
765 return 0;
768 if (iTask.status != SCSI_STATUS_GOOD) {
769 return -EIO;
772 return 0;
775 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
777 static int
778 coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
779 int nb_sectors, BdrvRequestFlags flags)
781 IscsiLun *iscsilun = bs->opaque;
782 struct IscsiTask iTask;
783 uint64_t lba;
784 uint32_t nb_blocks;
786 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
787 return -EINVAL;
790 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
791 /* WRITE SAME without UNMAP is not supported by the target */
792 return -ENOTSUP;
795 if ((flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->lbp.lbpws) {
796 /* WRITE SAME with UNMAP is not supported by the target */
797 return -ENOTSUP;
800 lba = sector_qemu2lun(sector_num, iscsilun);
801 nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
803 if (iscsilun->zeroblock == NULL) {
804 iscsilun->zeroblock = g_malloc0(iscsilun->block_size);
807 iscsi_co_init_iscsitask(iscsilun, &iTask);
808 retry:
809 if (iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
810 iscsilun->zeroblock, iscsilun->block_size,
811 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
812 0, 0, iscsi_co_generic_cb, &iTask) == NULL) {
813 return -EIO;
816 while (!iTask.complete) {
817 iscsi_set_events(iscsilun);
818 qemu_coroutine_yield();
821 if (iTask.task != NULL) {
822 scsi_free_scsi_task(iTask.task);
823 iTask.task = NULL;
826 if (iTask.do_retry) {
827 goto retry;
830 if (iTask.status != SCSI_STATUS_GOOD) {
831 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
832 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
833 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE) {
834 /* WRITE SAME is not supported by the target */
835 iscsilun->has_write_same = false;
836 return -ENOTSUP;
839 return -EIO;
842 return 0;
845 #endif /* SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED */
847 static int parse_chap(struct iscsi_context *iscsi, const char *target)
849 QemuOptsList *list;
850 QemuOpts *opts;
851 const char *user = NULL;
852 const char *password = NULL;
854 list = qemu_find_opts("iscsi");
855 if (!list) {
856 return 0;
859 opts = qemu_opts_find(list, target);
860 if (opts == NULL) {
861 opts = QTAILQ_FIRST(&list->head);
862 if (!opts) {
863 return 0;
867 user = qemu_opt_get(opts, "user");
868 if (!user) {
869 return 0;
872 password = qemu_opt_get(opts, "password");
873 if (!password) {
874 error_report("CHAP username specified but no password was given");
875 return -1;
878 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
879 error_report("Failed to set initiator username and password");
880 return -1;
883 return 0;
886 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
888 QemuOptsList *list;
889 QemuOpts *opts;
890 const char *digest = NULL;
892 list = qemu_find_opts("iscsi");
893 if (!list) {
894 return;
897 opts = qemu_opts_find(list, target);
898 if (opts == NULL) {
899 opts = QTAILQ_FIRST(&list->head);
900 if (!opts) {
901 return;
905 digest = qemu_opt_get(opts, "header-digest");
906 if (!digest) {
907 return;
910 if (!strcmp(digest, "CRC32C")) {
911 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
912 } else if (!strcmp(digest, "NONE")) {
913 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
914 } else if (!strcmp(digest, "CRC32C-NONE")) {
915 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
916 } else if (!strcmp(digest, "NONE-CRC32C")) {
917 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
918 } else {
919 error_report("Invalid header-digest setting : %s", digest);
923 static char *parse_initiator_name(const char *target)
925 QemuOptsList *list;
926 QemuOpts *opts;
927 const char *name;
928 char *iscsi_name;
929 UuidInfo *uuid_info;
931 list = qemu_find_opts("iscsi");
932 if (list) {
933 opts = qemu_opts_find(list, target);
934 if (!opts) {
935 opts = QTAILQ_FIRST(&list->head);
937 if (opts) {
938 name = qemu_opt_get(opts, "initiator-name");
939 if (name) {
940 return g_strdup(name);
945 uuid_info = qmp_query_uuid(NULL);
946 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
947 name = qemu_get_vm_name();
948 } else {
949 name = uuid_info->UUID;
951 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
952 name ? ":" : "", name ? name : "");
953 qapi_free_UuidInfo(uuid_info);
954 return iscsi_name;
957 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
958 static void iscsi_nop_timed_event(void *opaque)
960 IscsiLun *iscsilun = opaque;
962 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
963 error_report("iSCSI: NOP timeout. Reconnecting...");
964 iscsi_reconnect(iscsilun->iscsi);
967 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
968 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
969 return;
972 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
973 iscsi_set_events(iscsilun);
975 #endif
977 static int iscsi_readcapacity_sync(IscsiLun *iscsilun)
979 struct scsi_task *task = NULL;
980 struct scsi_readcapacity10 *rc10 = NULL;
981 struct scsi_readcapacity16 *rc16 = NULL;
982 int ret = 0;
983 int retries = ISCSI_CMD_RETRIES;
985 do {
986 if (task != NULL) {
987 scsi_free_scsi_task(task);
988 task = NULL;
991 switch (iscsilun->type) {
992 case TYPE_DISK:
993 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
994 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
995 rc16 = scsi_datain_unmarshall(task);
996 if (rc16 == NULL) {
997 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
998 ret = -EINVAL;
999 } else {
1000 iscsilun->block_size = rc16->block_length;
1001 iscsilun->num_blocks = rc16->returned_lba + 1;
1002 iscsilun->lbpme = rc16->lbpme;
1003 iscsilun->lbprz = rc16->lbprz;
1006 break;
1007 case TYPE_ROM:
1008 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1009 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1010 rc10 = scsi_datain_unmarshall(task);
1011 if (rc10 == NULL) {
1012 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
1013 ret = -EINVAL;
1014 } else {
1015 iscsilun->block_size = rc10->block_size;
1016 if (rc10->lba == 0) {
1017 /* blank disk loaded */
1018 iscsilun->num_blocks = 0;
1019 } else {
1020 iscsilun->num_blocks = rc10->lba + 1;
1024 break;
1025 default:
1026 return 0;
1028 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1029 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1030 && retries-- > 0);
1032 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1033 error_report("iSCSI: failed to send readcapacity10 command.");
1034 ret = -EINVAL;
1036 if (task) {
1037 scsi_free_scsi_task(task);
1039 return ret;
1042 /* TODO Convert to fine grained options */
1043 static QemuOptsList runtime_opts = {
1044 .name = "iscsi",
1045 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1046 .desc = {
1048 .name = "filename",
1049 .type = QEMU_OPT_STRING,
1050 .help = "URL to the iscsi image",
1052 { /* end of list */ }
1056 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi,
1057 int lun, int evpd, int pc) {
1058 int full_size;
1059 struct scsi_task *task = NULL;
1060 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1061 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1062 goto fail;
1064 full_size = scsi_datain_getfullsize(task);
1065 if (full_size > task->datain.size) {
1066 scsi_free_scsi_task(task);
1068 /* we need more data for the full list */
1069 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1070 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1071 goto fail;
1075 return task;
1077 fail:
1078 error_report("iSCSI: Inquiry command failed : %s",
1079 iscsi_get_error(iscsi));
1080 if (task) {
1081 scsi_free_scsi_task(task);
1082 return NULL;
1084 return NULL;
1088 * We support iscsi url's on the form
1089 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1091 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1092 Error **errp)
1094 IscsiLun *iscsilun = bs->opaque;
1095 struct iscsi_context *iscsi = NULL;
1096 struct iscsi_url *iscsi_url = NULL;
1097 struct scsi_task *task = NULL;
1098 struct scsi_inquiry_standard *inq = NULL;
1099 char *initiator_name = NULL;
1100 QemuOpts *opts;
1101 Error *local_err = NULL;
1102 const char *filename;
1103 int ret;
1105 if ((BDRV_SECTOR_SIZE % 512) != 0) {
1106 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1107 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1108 "of 512", BDRV_SECTOR_SIZE);
1109 return -EINVAL;
1112 opts = qemu_opts_create_nofail(&runtime_opts);
1113 qemu_opts_absorb_qdict(opts, options, &local_err);
1114 if (error_is_set(&local_err)) {
1115 qerror_report_err(local_err);
1116 error_free(local_err);
1117 ret = -EINVAL;
1118 goto out;
1121 filename = qemu_opt_get(opts, "filename");
1124 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1125 if (iscsi_url == NULL) {
1126 error_report("Failed to parse URL : %s", filename);
1127 ret = -EINVAL;
1128 goto out;
1131 memset(iscsilun, 0, sizeof(IscsiLun));
1133 initiator_name = parse_initiator_name(iscsi_url->target);
1135 iscsi = iscsi_create_context(initiator_name);
1136 if (iscsi == NULL) {
1137 error_report("iSCSI: Failed to create iSCSI context.");
1138 ret = -ENOMEM;
1139 goto out;
1142 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1143 error_report("iSCSI: Failed to set target name.");
1144 ret = -EINVAL;
1145 goto out;
1148 if (iscsi_url->user != NULL) {
1149 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1150 iscsi_url->passwd);
1151 if (ret != 0) {
1152 error_report("Failed to set initiator username and password");
1153 ret = -EINVAL;
1154 goto out;
1158 /* check if we got CHAP username/password via the options */
1159 if (parse_chap(iscsi, iscsi_url->target) != 0) {
1160 error_report("iSCSI: Failed to set CHAP user/password");
1161 ret = -EINVAL;
1162 goto out;
1165 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1166 error_report("iSCSI: Failed to set session type to normal.");
1167 ret = -EINVAL;
1168 goto out;
1171 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1173 /* check if we got HEADER_DIGEST via the options */
1174 parse_header_digest(iscsi, iscsi_url->target);
1176 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1177 error_report("iSCSI: Failed to connect to LUN : %s",
1178 iscsi_get_error(iscsi));
1179 ret = -EINVAL;
1180 goto out;
1183 iscsilun->iscsi = iscsi;
1184 iscsilun->lun = iscsi_url->lun;
1186 task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
1188 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1189 error_report("iSCSI: failed to send inquiry command.");
1190 ret = -EINVAL;
1191 goto out;
1194 inq = scsi_datain_unmarshall(task);
1195 if (inq == NULL) {
1196 error_report("iSCSI: Failed to unmarshall inquiry data.");
1197 ret = -EINVAL;
1198 goto out;
1201 iscsilun->type = inq->periperal_device_type;
1202 iscsilun->has_write_same = true;
1204 if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1205 goto out;
1207 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1209 /* Medium changer or tape. We dont have any emulation for this so this must
1210 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1211 * to read from the device to guess the image format.
1213 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1214 iscsilun->type == TYPE_TAPE) {
1215 bs->sg = 1;
1218 if (iscsilun->lbpme) {
1219 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1220 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1221 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING);
1222 if (task == NULL) {
1223 ret = -EINVAL;
1224 goto out;
1226 inq_lbp = scsi_datain_unmarshall(task);
1227 if (inq_lbp == NULL) {
1228 error_report("iSCSI: failed to unmarshall inquiry datain blob");
1229 ret = -EINVAL;
1230 goto out;
1232 memcpy(&iscsilun->lbp, inq_lbp,
1233 sizeof(struct scsi_inquiry_logical_block_provisioning));
1234 scsi_free_scsi_task(task);
1235 task = NULL;
1238 if (iscsilun->lbp.lbpu || iscsilun->lbp.lbpws) {
1239 struct scsi_inquiry_block_limits *inq_bl;
1240 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1241 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS);
1242 if (task == NULL) {
1243 ret = -EINVAL;
1244 goto out;
1246 inq_bl = scsi_datain_unmarshall(task);
1247 if (inq_bl == NULL) {
1248 error_report("iSCSI: failed to unmarshall inquiry datain blob");
1249 ret = -EINVAL;
1250 goto out;
1252 memcpy(&iscsilun->bl, inq_bl,
1253 sizeof(struct scsi_inquiry_block_limits));
1254 scsi_free_scsi_task(task);
1255 task = NULL;
1257 if (iscsilun->bl.max_unmap < 0xffffffff) {
1258 bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap,
1259 iscsilun);
1261 bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1262 iscsilun);
1264 if (iscsilun->bl.max_ws_len < 0xffffffff) {
1265 bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len,
1266 iscsilun);
1268 bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1269 iscsilun);
1271 bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len,
1272 iscsilun);
1275 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1276 /* Set up a timer for sending out iSCSI NOPs */
1277 iscsilun->nop_timer = timer_new_ms(QEMU_CLOCK_REALTIME, iscsi_nop_timed_event, iscsilun);
1278 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1279 #endif
1281 out:
1282 qemu_opts_del(opts);
1283 if (initiator_name != NULL) {
1284 g_free(initiator_name);
1286 if (iscsi_url != NULL) {
1287 iscsi_destroy_url(iscsi_url);
1289 if (task != NULL) {
1290 scsi_free_scsi_task(task);
1293 if (ret) {
1294 if (iscsi != NULL) {
1295 iscsi_destroy_context(iscsi);
1297 memset(iscsilun, 0, sizeof(IscsiLun));
1299 return ret;
1302 static void iscsi_close(BlockDriverState *bs)
1304 IscsiLun *iscsilun = bs->opaque;
1305 struct iscsi_context *iscsi = iscsilun->iscsi;
1307 if (iscsilun->nop_timer) {
1308 timer_del(iscsilun->nop_timer);
1309 timer_free(iscsilun->nop_timer);
1311 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL);
1312 iscsi_destroy_context(iscsi);
1313 g_free(iscsilun->zeroblock);
1314 memset(iscsilun, 0, sizeof(IscsiLun));
1317 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1319 IscsiLun *iscsilun = bs->opaque;
1320 int ret = 0;
1322 if (iscsilun->type != TYPE_DISK) {
1323 return -ENOTSUP;
1326 if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1327 return ret;
1330 if (offset > iscsi_getlength(bs)) {
1331 return -EINVAL;
1334 return 0;
1337 static int iscsi_create(const char *filename, QEMUOptionParameter *options,
1338 Error **errp)
1340 int ret = 0;
1341 int64_t total_size = 0;
1342 BlockDriverState *bs;
1343 IscsiLun *iscsilun = NULL;
1344 QDict *bs_options;
1346 bs = bdrv_new("");
1348 /* Read out options */
1349 while (options && options->name) {
1350 if (!strcmp(options->name, "size")) {
1351 total_size = options->value.n / BDRV_SECTOR_SIZE;
1353 options++;
1356 bs->opaque = g_malloc0(sizeof(struct IscsiLun));
1357 iscsilun = bs->opaque;
1359 bs_options = qdict_new();
1360 qdict_put(bs_options, "filename", qstring_from_str(filename));
1361 ret = iscsi_open(bs, bs_options, 0, NULL);
1362 QDECREF(bs_options);
1364 if (ret != 0) {
1365 goto out;
1367 if (iscsilun->nop_timer) {
1368 timer_del(iscsilun->nop_timer);
1369 timer_free(iscsilun->nop_timer);
1371 if (iscsilun->type != TYPE_DISK) {
1372 ret = -ENODEV;
1373 goto out;
1375 if (bs->total_sectors < total_size) {
1376 ret = -ENOSPC;
1377 goto out;
1380 ret = 0;
1381 out:
1382 if (iscsilun->iscsi != NULL) {
1383 iscsi_destroy_context(iscsilun->iscsi);
1385 g_free(bs->opaque);
1386 bs->opaque = NULL;
1387 bdrv_unref(bs);
1388 return ret;
1391 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1393 IscsiLun *iscsilun = bs->opaque;
1394 bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz;
1395 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
1396 /* Guess the internal cluster (page) size of the iscsi target by the means
1397 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1398 * reasonable size for bdi->cluster_size */
1399 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 64 * 1024 &&
1400 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1401 bdi->cluster_size = iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
1403 return 0;
1406 static QEMUOptionParameter iscsi_create_options[] = {
1408 .name = BLOCK_OPT_SIZE,
1409 .type = OPT_SIZE,
1410 .help = "Virtual disk size"
1412 { NULL }
1415 static BlockDriver bdrv_iscsi = {
1416 .format_name = "iscsi",
1417 .protocol_name = "iscsi",
1419 .instance_size = sizeof(IscsiLun),
1420 .bdrv_needs_filename = true,
1421 .bdrv_file_open = iscsi_open,
1422 .bdrv_close = iscsi_close,
1423 .bdrv_create = iscsi_create,
1424 .create_options = iscsi_create_options,
1426 .bdrv_getlength = iscsi_getlength,
1427 .bdrv_get_info = iscsi_get_info,
1428 .bdrv_truncate = iscsi_truncate,
1430 #if defined(LIBISCSI_FEATURE_IOVECTOR)
1431 .bdrv_co_get_block_status = iscsi_co_get_block_status,
1432 #endif
1433 .bdrv_co_discard = iscsi_co_discard,
1434 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
1435 .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
1436 #endif
1437 .bdrv_co_readv = iscsi_co_readv,
1438 .bdrv_co_writev = iscsi_co_writev,
1439 .bdrv_co_flush_to_disk = iscsi_co_flush,
1441 #ifdef __linux__
1442 .bdrv_ioctl = iscsi_ioctl,
1443 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1444 #endif
1447 static QemuOptsList qemu_iscsi_opts = {
1448 .name = "iscsi",
1449 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1450 .desc = {
1452 .name = "user",
1453 .type = QEMU_OPT_STRING,
1454 .help = "username for CHAP authentication to target",
1456 .name = "password",
1457 .type = QEMU_OPT_STRING,
1458 .help = "password for CHAP authentication to target",
1460 .name = "header-digest",
1461 .type = QEMU_OPT_STRING,
1462 .help = "HeaderDigest setting. "
1463 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1465 .name = "initiator-name",
1466 .type = QEMU_OPT_STRING,
1467 .help = "Initiator iqn name to use when connecting",
1469 { /* end of list */ }
1473 static void iscsi_block_init(void)
1475 bdrv_register(&bdrv_iscsi);
1476 qemu_add_opts(&qemu_iscsi_opts);
1479 block_init(iscsi_block_init);