net/cadence_gem: Fix register w1c logic
[qemu/ar7.git] / block / iscsi.c
blob829d444733040b7b3599e123847cd070ae58e0b3
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 int
243 iscsi_aio_writev_acb(IscsiAIOCB *acb);
245 static void
246 iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
247 void *command_data, void *opaque)
249 IscsiAIOCB *acb = opaque;
251 trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
253 g_free(acb->buf);
254 acb->buf = NULL;
256 if (acb->canceled != 0) {
257 return;
260 acb->status = 0;
261 if (status != 0) {
262 if (status == SCSI_STATUS_CHECK_CONDITION
263 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
264 && acb->retries-- > 0) {
265 scsi_free_scsi_task(acb->task);
266 acb->task = NULL;
267 if (iscsi_aio_writev_acb(acb) == 0) {
268 iscsi_set_events(acb->iscsilun);
269 return;
272 error_report("Failed to write16 data to iSCSI lun. %s",
273 iscsi_get_error(iscsi));
274 acb->status = -EIO;
277 iscsi_schedule_bh(acb);
280 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
282 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
285 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
287 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
290 static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
291 IscsiLun *iscsilun)
293 if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
294 (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
295 error_report("iSCSI misaligned request: "
296 "iscsilun->block_size %u, sector_num %" PRIi64
297 ", nb_sectors %d",
298 iscsilun->block_size, sector_num, nb_sectors);
299 return 0;
301 return 1;
304 static int
305 iscsi_aio_writev_acb(IscsiAIOCB *acb)
307 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
308 size_t size;
309 uint32_t num_sectors;
310 uint64_t lba;
311 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
312 struct iscsi_data data;
313 #endif
314 int ret;
316 acb->canceled = 0;
317 acb->bh = NULL;
318 acb->status = -EINPROGRESS;
319 acb->buf = NULL;
321 /* this will allow us to get rid of 'buf' completely */
322 size = acb->nb_sectors * BDRV_SECTOR_SIZE;
324 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
325 data.size = MIN(size, acb->qiov->size);
327 /* if the iovec only contains one buffer we can pass it directly */
328 if (acb->qiov->niov == 1) {
329 data.data = acb->qiov->iov[0].iov_base;
330 } else {
331 acb->buf = g_malloc(data.size);
332 qemu_iovec_to_buf(acb->qiov, 0, acb->buf, data.size);
333 data.data = acb->buf;
335 #endif
337 acb->task = malloc(sizeof(struct scsi_task));
338 if (acb->task == NULL) {
339 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
340 "command. %s", iscsi_get_error(iscsi));
341 return -1;
343 memset(acb->task, 0, sizeof(struct scsi_task));
345 acb->task->xfer_dir = SCSI_XFER_WRITE;
346 acb->task->cdb_size = 16;
347 acb->task->cdb[0] = 0x8a;
348 lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
349 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
350 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
351 num_sectors = sector_qemu2lun(acb->nb_sectors, acb->iscsilun);
352 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
353 acb->task->expxferlen = size;
355 #if defined(LIBISCSI_FEATURE_IOVECTOR)
356 ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
357 iscsi_aio_write16_cb,
358 NULL,
359 acb);
360 #else
361 ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
362 iscsi_aio_write16_cb,
363 &data,
364 acb);
365 #endif
366 if (ret != 0) {
367 scsi_free_scsi_task(acb->task);
368 g_free(acb->buf);
369 return -1;
372 #if defined(LIBISCSI_FEATURE_IOVECTOR)
373 scsi_task_set_iov_out(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
374 #endif
376 return 0;
379 static BlockDriverAIOCB *
380 iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
381 QEMUIOVector *qiov, int nb_sectors,
382 BlockDriverCompletionFunc *cb,
383 void *opaque)
385 IscsiLun *iscsilun = bs->opaque;
386 IscsiAIOCB *acb;
388 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
389 return NULL;
392 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
393 trace_iscsi_aio_writev(iscsilun->iscsi, sector_num, nb_sectors, opaque, acb);
395 acb->iscsilun = iscsilun;
396 acb->qiov = qiov;
397 acb->nb_sectors = nb_sectors;
398 acb->sector_num = sector_num;
399 acb->retries = ISCSI_CMD_RETRIES;
401 if (iscsi_aio_writev_acb(acb) != 0) {
402 qemu_aio_release(acb);
403 return NULL;
406 iscsi_set_events(iscsilun);
407 return &acb->common;
410 static int
411 iscsi_aio_readv_acb(IscsiAIOCB *acb);
413 static void
414 iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
415 void *command_data, void *opaque)
417 IscsiAIOCB *acb = opaque;
419 trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
421 if (acb->canceled != 0) {
422 return;
425 acb->status = 0;
426 if (status != 0) {
427 if (status == SCSI_STATUS_CHECK_CONDITION
428 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
429 && acb->retries-- > 0) {
430 scsi_free_scsi_task(acb->task);
431 acb->task = NULL;
432 if (iscsi_aio_readv_acb(acb) == 0) {
433 iscsi_set_events(acb->iscsilun);
434 return;
437 error_report("Failed to read16 data from iSCSI lun. %s",
438 iscsi_get_error(iscsi));
439 acb->status = -EIO;
442 iscsi_schedule_bh(acb);
445 static int
446 iscsi_aio_readv_acb(IscsiAIOCB *acb)
448 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
449 size_t size;
450 uint64_t lba;
451 uint32_t num_sectors;
452 int ret;
453 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
454 int i;
455 #endif
457 acb->canceled = 0;
458 acb->bh = NULL;
459 acb->status = -EINPROGRESS;
460 acb->buf = NULL;
462 size = acb->nb_sectors * BDRV_SECTOR_SIZE;
464 acb->task = malloc(sizeof(struct scsi_task));
465 if (acb->task == NULL) {
466 error_report("iSCSI: Failed to allocate task for scsi READ16 "
467 "command. %s", iscsi_get_error(iscsi));
468 return -1;
470 memset(acb->task, 0, sizeof(struct scsi_task));
472 acb->task->xfer_dir = SCSI_XFER_READ;
473 acb->task->expxferlen = size;
474 lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
475 num_sectors = sector_qemu2lun(acb->nb_sectors, acb->iscsilun);
477 switch (acb->iscsilun->type) {
478 case TYPE_DISK:
479 acb->task->cdb_size = 16;
480 acb->task->cdb[0] = 0x88;
481 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
482 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
483 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
484 break;
485 default:
486 acb->task->cdb_size = 10;
487 acb->task->cdb[0] = 0x28;
488 *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
489 *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
490 break;
493 ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
494 iscsi_aio_read16_cb,
495 NULL,
496 acb);
497 if (ret != 0) {
498 scsi_free_scsi_task(acb->task);
499 return -1;
502 #if defined(LIBISCSI_FEATURE_IOVECTOR)
503 scsi_task_set_iov_in(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
504 #else
505 for (i = 0; i < acb->qiov->niov; i++) {
506 scsi_task_add_data_in_buffer(acb->task,
507 acb->qiov->iov[i].iov_len,
508 acb->qiov->iov[i].iov_base);
510 #endif
511 return 0;
514 static BlockDriverAIOCB *
515 iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
516 QEMUIOVector *qiov, int nb_sectors,
517 BlockDriverCompletionFunc *cb,
518 void *opaque)
520 IscsiLun *iscsilun = bs->opaque;
521 IscsiAIOCB *acb;
523 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
524 return NULL;
527 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
528 trace_iscsi_aio_readv(iscsilun->iscsi, sector_num, nb_sectors, opaque, acb);
530 acb->nb_sectors = nb_sectors;
531 acb->sector_num = sector_num;
532 acb->iscsilun = iscsilun;
533 acb->qiov = qiov;
534 acb->retries = ISCSI_CMD_RETRIES;
536 if (iscsi_aio_readv_acb(acb) != 0) {
537 qemu_aio_release(acb);
538 return NULL;
541 iscsi_set_events(iscsilun);
542 return &acb->common;
545 static int
546 iscsi_aio_flush_acb(IscsiAIOCB *acb);
548 static void
549 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
550 void *command_data, void *opaque)
552 IscsiAIOCB *acb = opaque;
554 if (acb->canceled != 0) {
555 return;
558 acb->status = 0;
559 if (status != 0) {
560 if (status == SCSI_STATUS_CHECK_CONDITION
561 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
562 && acb->retries-- > 0) {
563 scsi_free_scsi_task(acb->task);
564 acb->task = NULL;
565 if (iscsi_aio_flush_acb(acb) == 0) {
566 iscsi_set_events(acb->iscsilun);
567 return;
570 error_report("Failed to sync10 data on iSCSI lun. %s",
571 iscsi_get_error(iscsi));
572 acb->status = -EIO;
575 iscsi_schedule_bh(acb);
578 static int
579 iscsi_aio_flush_acb(IscsiAIOCB *acb)
581 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
583 acb->canceled = 0;
584 acb->bh = NULL;
585 acb->status = -EINPROGRESS;
586 acb->buf = NULL;
588 acb->task = iscsi_synchronizecache10_task(iscsi, acb->iscsilun->lun,
589 0, 0, 0, 0,
590 iscsi_synccache10_cb,
591 acb);
592 if (acb->task == NULL) {
593 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
594 iscsi_get_error(iscsi));
595 return -1;
598 return 0;
601 static BlockDriverAIOCB *
602 iscsi_aio_flush(BlockDriverState *bs,
603 BlockDriverCompletionFunc *cb, void *opaque)
605 IscsiLun *iscsilun = bs->opaque;
607 IscsiAIOCB *acb;
609 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
611 acb->iscsilun = iscsilun;
612 acb->retries = ISCSI_CMD_RETRIES;
614 if (iscsi_aio_flush_acb(acb) != 0) {
615 qemu_aio_release(acb);
616 return NULL;
619 iscsi_set_events(iscsilun);
621 return &acb->common;
624 #ifdef __linux__
625 static void
626 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
627 void *command_data, void *opaque)
629 IscsiAIOCB *acb = opaque;
631 g_free(acb->buf);
632 acb->buf = NULL;
634 if (acb->canceled != 0) {
635 return;
638 acb->status = 0;
639 if (status < 0) {
640 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
641 iscsi_get_error(iscsi));
642 acb->status = -EIO;
645 acb->ioh->driver_status = 0;
646 acb->ioh->host_status = 0;
647 acb->ioh->resid = 0;
649 #define SG_ERR_DRIVER_SENSE 0x08
651 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
652 int ss;
654 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
656 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
657 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
658 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
659 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
662 iscsi_schedule_bh(acb);
665 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
666 unsigned long int req, void *buf,
667 BlockDriverCompletionFunc *cb, void *opaque)
669 IscsiLun *iscsilun = bs->opaque;
670 struct iscsi_context *iscsi = iscsilun->iscsi;
671 struct iscsi_data data;
672 IscsiAIOCB *acb;
674 assert(req == SG_IO);
676 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
678 acb->iscsilun = iscsilun;
679 acb->canceled = 0;
680 acb->bh = NULL;
681 acb->status = -EINPROGRESS;
682 acb->buf = NULL;
683 acb->ioh = buf;
685 acb->task = malloc(sizeof(struct scsi_task));
686 if (acb->task == NULL) {
687 error_report("iSCSI: Failed to allocate task for scsi command. %s",
688 iscsi_get_error(iscsi));
689 qemu_aio_release(acb);
690 return NULL;
692 memset(acb->task, 0, sizeof(struct scsi_task));
694 switch (acb->ioh->dxfer_direction) {
695 case SG_DXFER_TO_DEV:
696 acb->task->xfer_dir = SCSI_XFER_WRITE;
697 break;
698 case SG_DXFER_FROM_DEV:
699 acb->task->xfer_dir = SCSI_XFER_READ;
700 break;
701 default:
702 acb->task->xfer_dir = SCSI_XFER_NONE;
703 break;
706 acb->task->cdb_size = acb->ioh->cmd_len;
707 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
708 acb->task->expxferlen = acb->ioh->dxfer_len;
710 data.size = 0;
711 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
712 if (acb->ioh->iovec_count == 0) {
713 data.data = acb->ioh->dxferp;
714 data.size = acb->ioh->dxfer_len;
715 } else {
716 #if defined(LIBISCSI_FEATURE_IOVECTOR)
717 scsi_task_set_iov_out(acb->task,
718 (struct scsi_iovec *) acb->ioh->dxferp,
719 acb->ioh->iovec_count);
720 #else
721 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
723 acb->buf = g_malloc(acb->ioh->dxfer_len);
724 data.data = acb->buf;
725 data.size = iov_to_buf(iov, acb->ioh->iovec_count, 0,
726 acb->buf, acb->ioh->dxfer_len);
727 #endif
731 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
732 iscsi_aio_ioctl_cb,
733 (data.size > 0) ? &data : NULL,
734 acb) != 0) {
735 scsi_free_scsi_task(acb->task);
736 qemu_aio_release(acb);
737 return NULL;
740 /* tell libiscsi to read straight into the buffer we got from ioctl */
741 if (acb->task->xfer_dir == SCSI_XFER_READ) {
742 if (acb->ioh->iovec_count == 0) {
743 scsi_task_add_data_in_buffer(acb->task,
744 acb->ioh->dxfer_len,
745 acb->ioh->dxferp);
746 } else {
747 #if defined(LIBISCSI_FEATURE_IOVECTOR)
748 scsi_task_set_iov_in(acb->task,
749 (struct scsi_iovec *) acb->ioh->dxferp,
750 acb->ioh->iovec_count);
751 #else
752 int i;
753 for (i = 0; i < acb->ioh->iovec_count; i++) {
754 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
756 scsi_task_add_data_in_buffer(acb->task,
757 iov[i].iov_len,
758 iov[i].iov_base);
760 #endif
764 iscsi_set_events(iscsilun);
766 return &acb->common;
770 static void ioctl_cb(void *opaque, int status)
772 int *p_status = opaque;
773 *p_status = status;
776 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
778 IscsiLun *iscsilun = bs->opaque;
779 int status;
781 switch (req) {
782 case SG_GET_VERSION_NUM:
783 *(int *)buf = 30000;
784 break;
785 case SG_GET_SCSI_ID:
786 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
787 break;
788 case SG_IO:
789 status = -EINPROGRESS;
790 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
792 while (status == -EINPROGRESS) {
793 qemu_aio_wait();
796 return 0;
797 default:
798 return -1;
800 return 0;
802 #endif
804 static int64_t
805 iscsi_getlength(BlockDriverState *bs)
807 IscsiLun *iscsilun = bs->opaque;
808 int64_t len;
810 len = iscsilun->num_blocks;
811 len *= iscsilun->block_size;
813 return len;
816 #if defined(LIBISCSI_FEATURE_IOVECTOR)
818 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
819 int64_t sector_num,
820 int nb_sectors, int *pnum)
822 IscsiLun *iscsilun = bs->opaque;
823 struct scsi_get_lba_status *lbas = NULL;
824 struct scsi_lba_status_descriptor *lbasd = NULL;
825 struct IscsiTask iTask;
826 int64_t ret;
828 iscsi_co_init_iscsitask(iscsilun, &iTask);
830 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
831 ret = -EINVAL;
832 goto out;
835 /* default to all sectors allocated */
836 ret = BDRV_BLOCK_DATA;
837 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
838 *pnum = nb_sectors;
840 /* LUN does not support logical block provisioning */
841 if (iscsilun->lbpme == 0) {
842 goto out;
845 retry:
846 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
847 sector_qemu2lun(sector_num, iscsilun),
848 8 + 16, iscsi_co_generic_cb,
849 &iTask) == NULL) {
850 ret = -EIO;
851 goto out;
854 while (!iTask.complete) {
855 iscsi_set_events(iscsilun);
856 qemu_coroutine_yield();
859 if (iTask.do_retry) {
860 if (iTask.task != NULL) {
861 scsi_free_scsi_task(iTask.task);
862 iTask.task = NULL;
864 goto retry;
867 if (iTask.status != SCSI_STATUS_GOOD) {
868 /* in case the get_lba_status_callout fails (i.e.
869 * because the device is busy or the cmd is not
870 * supported) we pretend all blocks are allocated
871 * for backwards compatibility */
872 goto out;
875 lbas = scsi_datain_unmarshall(iTask.task);
876 if (lbas == NULL) {
877 ret = -EIO;
878 goto out;
881 lbasd = &lbas->descriptors[0];
883 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
884 ret = -EIO;
885 goto out;
888 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
889 if (*pnum > nb_sectors) {
890 *pnum = nb_sectors;
893 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
894 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
895 ret &= ~BDRV_BLOCK_DATA;
896 if (iscsilun->lbprz) {
897 ret |= BDRV_BLOCK_ZERO;
901 out:
902 if (iTask.task != NULL) {
903 scsi_free_scsi_task(iTask.task);
905 return ret;
908 #endif /* LIBISCSI_FEATURE_IOVECTOR */
910 static int
911 coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
912 int nb_sectors)
914 IscsiLun *iscsilun = bs->opaque;
915 struct IscsiTask iTask;
916 struct unmap_list list;
918 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
919 return -EINVAL;
922 if (!iscsilun->lbp.lbpu) {
923 /* UNMAP is not supported by the target */
924 return 0;
927 list.lba = sector_qemu2lun(sector_num, iscsilun);
928 list.num = sector_qemu2lun(nb_sectors, iscsilun);
930 iscsi_co_init_iscsitask(iscsilun, &iTask);
931 retry:
932 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
933 iscsi_co_generic_cb, &iTask) == NULL) {
934 return -EIO;
937 while (!iTask.complete) {
938 iscsi_set_events(iscsilun);
939 qemu_coroutine_yield();
942 if (iTask.task != NULL) {
943 scsi_free_scsi_task(iTask.task);
944 iTask.task = NULL;
947 if (iTask.do_retry) {
948 goto retry;
951 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
952 /* the target might fail with a check condition if it
953 is not happy with the alignment of the UNMAP request
954 we silently fail in this case */
955 return 0;
958 if (iTask.status != SCSI_STATUS_GOOD) {
959 return -EIO;
962 return 0;
965 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
967 static int
968 coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
969 int nb_sectors, BdrvRequestFlags flags)
971 IscsiLun *iscsilun = bs->opaque;
972 struct IscsiTask iTask;
973 uint64_t lba;
974 uint32_t nb_blocks;
976 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
977 return -EINVAL;
980 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
981 /* WRITE SAME without UNMAP is not supported by the target */
982 return -ENOTSUP;
985 if ((flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->lbp.lbpws) {
986 /* WRITE SAME with UNMAP is not supported by the target */
987 return -ENOTSUP;
990 lba = sector_qemu2lun(sector_num, iscsilun);
991 nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
993 if (iscsilun->zeroblock == NULL) {
994 iscsilun->zeroblock = g_malloc0(iscsilun->block_size);
997 iscsi_co_init_iscsitask(iscsilun, &iTask);
998 retry:
999 if (iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1000 iscsilun->zeroblock, iscsilun->block_size,
1001 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1002 0, 0, iscsi_co_generic_cb, &iTask) == NULL) {
1003 return -EIO;
1006 while (!iTask.complete) {
1007 iscsi_set_events(iscsilun);
1008 qemu_coroutine_yield();
1011 if (iTask.task != NULL) {
1012 scsi_free_scsi_task(iTask.task);
1013 iTask.task = NULL;
1016 if (iTask.do_retry) {
1017 goto retry;
1020 if (iTask.status != SCSI_STATUS_GOOD) {
1021 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1022 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
1023 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE) {
1024 /* WRITE SAME is not supported by the target */
1025 iscsilun->has_write_same = false;
1026 return -ENOTSUP;
1029 return -EIO;
1032 return 0;
1035 #endif /* SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED */
1037 static int parse_chap(struct iscsi_context *iscsi, const char *target)
1039 QemuOptsList *list;
1040 QemuOpts *opts;
1041 const char *user = NULL;
1042 const char *password = NULL;
1044 list = qemu_find_opts("iscsi");
1045 if (!list) {
1046 return 0;
1049 opts = qemu_opts_find(list, target);
1050 if (opts == NULL) {
1051 opts = QTAILQ_FIRST(&list->head);
1052 if (!opts) {
1053 return 0;
1057 user = qemu_opt_get(opts, "user");
1058 if (!user) {
1059 return 0;
1062 password = qemu_opt_get(opts, "password");
1063 if (!password) {
1064 error_report("CHAP username specified but no password was given");
1065 return -1;
1068 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
1069 error_report("Failed to set initiator username and password");
1070 return -1;
1073 return 0;
1076 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
1078 QemuOptsList *list;
1079 QemuOpts *opts;
1080 const char *digest = NULL;
1082 list = qemu_find_opts("iscsi");
1083 if (!list) {
1084 return;
1087 opts = qemu_opts_find(list, target);
1088 if (opts == NULL) {
1089 opts = QTAILQ_FIRST(&list->head);
1090 if (!opts) {
1091 return;
1095 digest = qemu_opt_get(opts, "header-digest");
1096 if (!digest) {
1097 return;
1100 if (!strcmp(digest, "CRC32C")) {
1101 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1102 } else if (!strcmp(digest, "NONE")) {
1103 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1104 } else if (!strcmp(digest, "CRC32C-NONE")) {
1105 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1106 } else if (!strcmp(digest, "NONE-CRC32C")) {
1107 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1108 } else {
1109 error_report("Invalid header-digest setting : %s", digest);
1113 static char *parse_initiator_name(const char *target)
1115 QemuOptsList *list;
1116 QemuOpts *opts;
1117 const char *name;
1118 char *iscsi_name;
1119 UuidInfo *uuid_info;
1121 list = qemu_find_opts("iscsi");
1122 if (list) {
1123 opts = qemu_opts_find(list, target);
1124 if (!opts) {
1125 opts = QTAILQ_FIRST(&list->head);
1127 if (opts) {
1128 name = qemu_opt_get(opts, "initiator-name");
1129 if (name) {
1130 return g_strdup(name);
1135 uuid_info = qmp_query_uuid(NULL);
1136 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1137 name = qemu_get_vm_name();
1138 } else {
1139 name = uuid_info->UUID;
1141 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1142 name ? ":" : "", name ? name : "");
1143 qapi_free_UuidInfo(uuid_info);
1144 return iscsi_name;
1147 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1148 static void iscsi_nop_timed_event(void *opaque)
1150 IscsiLun *iscsilun = opaque;
1152 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
1153 error_report("iSCSI: NOP timeout. Reconnecting...");
1154 iscsi_reconnect(iscsilun->iscsi);
1157 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1158 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1159 return;
1162 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1163 iscsi_set_events(iscsilun);
1165 #endif
1167 static int iscsi_readcapacity_sync(IscsiLun *iscsilun)
1169 struct scsi_task *task = NULL;
1170 struct scsi_readcapacity10 *rc10 = NULL;
1171 struct scsi_readcapacity16 *rc16 = NULL;
1172 int ret = 0;
1173 int retries = ISCSI_CMD_RETRIES;
1175 do {
1176 if (task != NULL) {
1177 scsi_free_scsi_task(task);
1178 task = NULL;
1181 switch (iscsilun->type) {
1182 case TYPE_DISK:
1183 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1184 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1185 rc16 = scsi_datain_unmarshall(task);
1186 if (rc16 == NULL) {
1187 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
1188 ret = -EINVAL;
1189 } else {
1190 iscsilun->block_size = rc16->block_length;
1191 iscsilun->num_blocks = rc16->returned_lba + 1;
1192 iscsilun->lbpme = rc16->lbpme;
1193 iscsilun->lbprz = rc16->lbprz;
1196 break;
1197 case TYPE_ROM:
1198 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1199 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1200 rc10 = scsi_datain_unmarshall(task);
1201 if (rc10 == NULL) {
1202 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
1203 ret = -EINVAL;
1204 } else {
1205 iscsilun->block_size = rc10->block_size;
1206 if (rc10->lba == 0) {
1207 /* blank disk loaded */
1208 iscsilun->num_blocks = 0;
1209 } else {
1210 iscsilun->num_blocks = rc10->lba + 1;
1214 break;
1215 default:
1216 return 0;
1218 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1219 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1220 && retries-- > 0);
1222 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1223 error_report("iSCSI: failed to send readcapacity10 command.");
1224 ret = -EINVAL;
1226 if (task) {
1227 scsi_free_scsi_task(task);
1229 return ret;
1232 /* TODO Convert to fine grained options */
1233 static QemuOptsList runtime_opts = {
1234 .name = "iscsi",
1235 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1236 .desc = {
1238 .name = "filename",
1239 .type = QEMU_OPT_STRING,
1240 .help = "URL to the iscsi image",
1242 { /* end of list */ }
1246 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi,
1247 int lun, int evpd, int pc) {
1248 int full_size;
1249 struct scsi_task *task = NULL;
1250 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1251 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1252 goto fail;
1254 full_size = scsi_datain_getfullsize(task);
1255 if (full_size > task->datain.size) {
1256 scsi_free_scsi_task(task);
1258 /* we need more data for the full list */
1259 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1260 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1261 goto fail;
1265 return task;
1267 fail:
1268 error_report("iSCSI: Inquiry command failed : %s",
1269 iscsi_get_error(iscsi));
1270 if (task) {
1271 scsi_free_scsi_task(task);
1272 return NULL;
1274 return NULL;
1278 * We support iscsi url's on the form
1279 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1281 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1282 Error **errp)
1284 IscsiLun *iscsilun = bs->opaque;
1285 struct iscsi_context *iscsi = NULL;
1286 struct iscsi_url *iscsi_url = NULL;
1287 struct scsi_task *task = NULL;
1288 struct scsi_inquiry_standard *inq = NULL;
1289 char *initiator_name = NULL;
1290 QemuOpts *opts;
1291 Error *local_err = NULL;
1292 const char *filename;
1293 int ret;
1295 if ((BDRV_SECTOR_SIZE % 512) != 0) {
1296 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1297 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1298 "of 512", BDRV_SECTOR_SIZE);
1299 return -EINVAL;
1302 opts = qemu_opts_create_nofail(&runtime_opts);
1303 qemu_opts_absorb_qdict(opts, options, &local_err);
1304 if (error_is_set(&local_err)) {
1305 qerror_report_err(local_err);
1306 error_free(local_err);
1307 ret = -EINVAL;
1308 goto out;
1311 filename = qemu_opt_get(opts, "filename");
1314 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1315 if (iscsi_url == NULL) {
1316 error_report("Failed to parse URL : %s", filename);
1317 ret = -EINVAL;
1318 goto out;
1321 memset(iscsilun, 0, sizeof(IscsiLun));
1323 initiator_name = parse_initiator_name(iscsi_url->target);
1325 iscsi = iscsi_create_context(initiator_name);
1326 if (iscsi == NULL) {
1327 error_report("iSCSI: Failed to create iSCSI context.");
1328 ret = -ENOMEM;
1329 goto out;
1332 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1333 error_report("iSCSI: Failed to set target name.");
1334 ret = -EINVAL;
1335 goto out;
1338 if (iscsi_url->user != NULL) {
1339 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1340 iscsi_url->passwd);
1341 if (ret != 0) {
1342 error_report("Failed to set initiator username and password");
1343 ret = -EINVAL;
1344 goto out;
1348 /* check if we got CHAP username/password via the options */
1349 if (parse_chap(iscsi, iscsi_url->target) != 0) {
1350 error_report("iSCSI: Failed to set CHAP user/password");
1351 ret = -EINVAL;
1352 goto out;
1355 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1356 error_report("iSCSI: Failed to set session type to normal.");
1357 ret = -EINVAL;
1358 goto out;
1361 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1363 /* check if we got HEADER_DIGEST via the options */
1364 parse_header_digest(iscsi, iscsi_url->target);
1366 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1367 error_report("iSCSI: Failed to connect to LUN : %s",
1368 iscsi_get_error(iscsi));
1369 ret = -EINVAL;
1370 goto out;
1373 iscsilun->iscsi = iscsi;
1374 iscsilun->lun = iscsi_url->lun;
1376 task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
1378 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1379 error_report("iSCSI: failed to send inquiry command.");
1380 ret = -EINVAL;
1381 goto out;
1384 inq = scsi_datain_unmarshall(task);
1385 if (inq == NULL) {
1386 error_report("iSCSI: Failed to unmarshall inquiry data.");
1387 ret = -EINVAL;
1388 goto out;
1391 iscsilun->type = inq->periperal_device_type;
1392 iscsilun->has_write_same = true;
1394 if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1395 goto out;
1397 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1399 /* Medium changer or tape. We dont have any emulation for this so this must
1400 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1401 * to read from the device to guess the image format.
1403 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1404 iscsilun->type == TYPE_TAPE) {
1405 bs->sg = 1;
1408 if (iscsilun->lbpme) {
1409 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1410 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1411 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING);
1412 if (task == NULL) {
1413 ret = -EINVAL;
1414 goto out;
1416 inq_lbp = scsi_datain_unmarshall(task);
1417 if (inq_lbp == NULL) {
1418 error_report("iSCSI: failed to unmarshall inquiry datain blob");
1419 ret = -EINVAL;
1420 goto out;
1422 memcpy(&iscsilun->lbp, inq_lbp,
1423 sizeof(struct scsi_inquiry_logical_block_provisioning));
1424 scsi_free_scsi_task(task);
1425 task = NULL;
1428 if (iscsilun->lbp.lbpu || iscsilun->lbp.lbpws) {
1429 struct scsi_inquiry_block_limits *inq_bl;
1430 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1431 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS);
1432 if (task == NULL) {
1433 ret = -EINVAL;
1434 goto out;
1436 inq_bl = scsi_datain_unmarshall(task);
1437 if (inq_bl == NULL) {
1438 error_report("iSCSI: failed to unmarshall inquiry datain blob");
1439 ret = -EINVAL;
1440 goto out;
1442 memcpy(&iscsilun->bl, inq_bl,
1443 sizeof(struct scsi_inquiry_block_limits));
1444 scsi_free_scsi_task(task);
1445 task = NULL;
1447 if (iscsilun->bl.max_unmap < 0xffffffff) {
1448 bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap,
1449 iscsilun);
1451 bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1452 iscsilun);
1454 if (iscsilun->bl.max_ws_len < 0xffffffff) {
1455 bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len,
1456 iscsilun);
1458 bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1459 iscsilun);
1461 bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len,
1462 iscsilun);
1465 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1466 /* Set up a timer for sending out iSCSI NOPs */
1467 iscsilun->nop_timer = timer_new_ms(QEMU_CLOCK_REALTIME, iscsi_nop_timed_event, iscsilun);
1468 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1469 #endif
1471 out:
1472 qemu_opts_del(opts);
1473 if (initiator_name != NULL) {
1474 g_free(initiator_name);
1476 if (iscsi_url != NULL) {
1477 iscsi_destroy_url(iscsi_url);
1479 if (task != NULL) {
1480 scsi_free_scsi_task(task);
1483 if (ret) {
1484 if (iscsi != NULL) {
1485 iscsi_destroy_context(iscsi);
1487 memset(iscsilun, 0, sizeof(IscsiLun));
1489 return ret;
1492 static void iscsi_close(BlockDriverState *bs)
1494 IscsiLun *iscsilun = bs->opaque;
1495 struct iscsi_context *iscsi = iscsilun->iscsi;
1497 if (iscsilun->nop_timer) {
1498 timer_del(iscsilun->nop_timer);
1499 timer_free(iscsilun->nop_timer);
1501 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL);
1502 iscsi_destroy_context(iscsi);
1503 g_free(iscsilun->zeroblock);
1504 memset(iscsilun, 0, sizeof(IscsiLun));
1507 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1509 IscsiLun *iscsilun = bs->opaque;
1510 int ret = 0;
1512 if (iscsilun->type != TYPE_DISK) {
1513 return -ENOTSUP;
1516 if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1517 return ret;
1520 if (offset > iscsi_getlength(bs)) {
1521 return -EINVAL;
1524 return 0;
1527 static int iscsi_create(const char *filename, QEMUOptionParameter *options,
1528 Error **errp)
1530 int ret = 0;
1531 int64_t total_size = 0;
1532 BlockDriverState *bs;
1533 IscsiLun *iscsilun = NULL;
1534 QDict *bs_options;
1536 bs = bdrv_new("");
1538 /* Read out options */
1539 while (options && options->name) {
1540 if (!strcmp(options->name, "size")) {
1541 total_size = options->value.n / BDRV_SECTOR_SIZE;
1543 options++;
1546 bs->opaque = g_malloc0(sizeof(struct IscsiLun));
1547 iscsilun = bs->opaque;
1549 bs_options = qdict_new();
1550 qdict_put(bs_options, "filename", qstring_from_str(filename));
1551 ret = iscsi_open(bs, bs_options, 0, NULL);
1552 QDECREF(bs_options);
1554 if (ret != 0) {
1555 goto out;
1557 if (iscsilun->nop_timer) {
1558 timer_del(iscsilun->nop_timer);
1559 timer_free(iscsilun->nop_timer);
1561 if (iscsilun->type != TYPE_DISK) {
1562 ret = -ENODEV;
1563 goto out;
1565 if (bs->total_sectors < total_size) {
1566 ret = -ENOSPC;
1567 goto out;
1570 ret = 0;
1571 out:
1572 if (iscsilun->iscsi != NULL) {
1573 iscsi_destroy_context(iscsilun->iscsi);
1575 g_free(bs->opaque);
1576 bs->opaque = NULL;
1577 bdrv_unref(bs);
1578 return ret;
1581 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1583 IscsiLun *iscsilun = bs->opaque;
1584 bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz;
1585 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
1586 /* Guess the internal cluster (page) size of the iscsi target by the means
1587 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1588 * reasonable size for bdi->cluster_size */
1589 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 64 * 1024 &&
1590 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1591 bdi->cluster_size = iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
1593 return 0;
1596 static QEMUOptionParameter iscsi_create_options[] = {
1598 .name = BLOCK_OPT_SIZE,
1599 .type = OPT_SIZE,
1600 .help = "Virtual disk size"
1602 { NULL }
1605 static BlockDriver bdrv_iscsi = {
1606 .format_name = "iscsi",
1607 .protocol_name = "iscsi",
1609 .instance_size = sizeof(IscsiLun),
1610 .bdrv_needs_filename = true,
1611 .bdrv_file_open = iscsi_open,
1612 .bdrv_close = iscsi_close,
1613 .bdrv_create = iscsi_create,
1614 .create_options = iscsi_create_options,
1616 .bdrv_getlength = iscsi_getlength,
1617 .bdrv_get_info = iscsi_get_info,
1618 .bdrv_truncate = iscsi_truncate,
1620 #if defined(LIBISCSI_FEATURE_IOVECTOR)
1621 .bdrv_co_get_block_status = iscsi_co_get_block_status,
1622 #endif
1623 .bdrv_co_discard = iscsi_co_discard,
1624 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
1625 .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
1626 #endif
1628 .bdrv_aio_readv = iscsi_aio_readv,
1629 .bdrv_aio_writev = iscsi_aio_writev,
1630 .bdrv_aio_flush = iscsi_aio_flush,
1632 #ifdef __linux__
1633 .bdrv_ioctl = iscsi_ioctl,
1634 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1635 #endif
1638 static QemuOptsList qemu_iscsi_opts = {
1639 .name = "iscsi",
1640 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1641 .desc = {
1643 .name = "user",
1644 .type = QEMU_OPT_STRING,
1645 .help = "username for CHAP authentication to target",
1647 .name = "password",
1648 .type = QEMU_OPT_STRING,
1649 .help = "password for CHAP authentication to target",
1651 .name = "header-digest",
1652 .type = QEMU_OPT_STRING,
1653 .help = "HeaderDigest setting. "
1654 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1656 .name = "initiator-name",
1657 .type = QEMU_OPT_STRING,
1658 .help = "Initiator iqn name to use when connecting",
1660 { /* end of list */ }
1664 static void iscsi_block_init(void)
1666 bdrv_register(&bdrv_iscsi);
1667 qemu_add_opts(&qemu_iscsi_opts);
1670 block_init(iscsi_block_init);