ne2000: add bootindex to qom property
[qemu-kvm.git] / block / iscsi.c
blob3a01de0edba838aa3d56c8ef3cc8214b76b0e596
1 /*
2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2014 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 <math.h>
30 #include <arpa/inet.h>
31 #include "qemu-common.h"
32 #include "qemu/config-file.h"
33 #include "qemu/error-report.h"
34 #include "qemu/bitops.h"
35 #include "qemu/bitmap.h"
36 #include "block/block_int.h"
37 #include "block/scsi.h"
38 #include "qemu/iov.h"
39 #include "sysemu/sysemu.h"
40 #include "qmp-commands.h"
42 #include <iscsi/iscsi.h>
43 #include <iscsi/scsi-lowlevel.h>
45 #ifdef __linux__
46 #include <scsi/sg.h>
47 #include <block/scsi.h>
48 #endif
50 typedef struct IscsiLun {
51 struct iscsi_context *iscsi;
52 AioContext *aio_context;
53 int lun;
54 enum scsi_inquiry_peripheral_device_type type;
55 int block_size;
56 uint64_t num_blocks;
57 int events;
58 QEMUTimer *nop_timer;
59 uint8_t lbpme;
60 uint8_t lbprz;
61 uint8_t has_write_same;
62 struct scsi_inquiry_logical_block_provisioning lbp;
63 struct scsi_inquiry_block_limits bl;
64 unsigned char *zeroblock;
65 unsigned long *allocationmap;
66 int cluster_sectors;
67 bool use_16_for_rw;
68 } IscsiLun;
70 typedef struct IscsiTask {
71 int status;
72 int complete;
73 int retries;
74 int do_retry;
75 struct scsi_task *task;
76 Coroutine *co;
77 QEMUBH *bh;
78 IscsiLun *iscsilun;
79 QEMUTimer retry_timer;
80 } IscsiTask;
82 typedef struct IscsiAIOCB {
83 BlockDriverAIOCB common;
84 QEMUIOVector *qiov;
85 QEMUBH *bh;
86 IscsiLun *iscsilun;
87 struct scsi_task *task;
88 uint8_t *buf;
89 int status;
90 int64_t sector_num;
91 int nb_sectors;
92 #ifdef __linux__
93 sg_io_hdr_t *ioh;
94 #endif
95 } IscsiAIOCB;
97 #define NOP_INTERVAL 5000
98 #define MAX_NOP_FAILURES 3
99 #define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
100 static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048};
102 /* this threshold is a trade-off knob to choose between
103 * the potential additional overhead of an extra GET_LBA_STATUS request
104 * vs. unnecessarily reading a lot of zero sectors over the wire.
105 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
106 * sectors we check the allocation status of the area covered by the
107 * request first if the allocationmap indicates that the area might be
108 * unallocated. */
109 #define ISCSI_CHECKALLOC_THRES 64
111 static void
112 iscsi_bh_cb(void *p)
114 IscsiAIOCB *acb = p;
116 qemu_bh_delete(acb->bh);
118 g_free(acb->buf);
119 acb->buf = NULL;
121 acb->common.cb(acb->common.opaque, acb->status);
123 if (acb->task != NULL) {
124 scsi_free_scsi_task(acb->task);
125 acb->task = NULL;
128 qemu_aio_unref(acb);
131 static void
132 iscsi_schedule_bh(IscsiAIOCB *acb)
134 if (acb->bh) {
135 return;
137 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
138 qemu_bh_schedule(acb->bh);
141 static void iscsi_co_generic_bh_cb(void *opaque)
143 struct IscsiTask *iTask = opaque;
144 iTask->complete = 1;
145 qemu_bh_delete(iTask->bh);
146 qemu_coroutine_enter(iTask->co, NULL);
149 static void iscsi_retry_timer_expired(void *opaque)
151 struct IscsiTask *iTask = opaque;
152 iTask->complete = 1;
153 if (iTask->co) {
154 qemu_coroutine_enter(iTask->co, NULL);
158 static inline unsigned exp_random(double mean)
160 return -mean * log((double)rand() / RAND_MAX);
163 static void
164 iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
165 void *command_data, void *opaque)
167 struct IscsiTask *iTask = opaque;
168 struct scsi_task *task = command_data;
170 iTask->status = status;
171 iTask->do_retry = 0;
172 iTask->task = task;
174 if (status != SCSI_STATUS_GOOD) {
175 if (iTask->retries++ < ISCSI_CMD_RETRIES) {
176 if (status == SCSI_STATUS_CHECK_CONDITION
177 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
178 error_report("iSCSI CheckCondition: %s",
179 iscsi_get_error(iscsi));
180 iTask->do_retry = 1;
181 goto out;
183 if (status == SCSI_STATUS_BUSY) {
184 unsigned retry_time =
185 exp_random(iscsi_retry_times[iTask->retries - 1]);
186 error_report("iSCSI Busy (retry #%u in %u ms): %s",
187 iTask->retries, retry_time,
188 iscsi_get_error(iscsi));
189 aio_timer_init(iTask->iscsilun->aio_context,
190 &iTask->retry_timer, QEMU_CLOCK_REALTIME,
191 SCALE_MS, iscsi_retry_timer_expired, iTask);
192 timer_mod(&iTask->retry_timer,
193 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time);
194 iTask->do_retry = 1;
195 return;
198 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi));
201 out:
202 if (iTask->co) {
203 iTask->bh = aio_bh_new(iTask->iscsilun->aio_context,
204 iscsi_co_generic_bh_cb, iTask);
205 qemu_bh_schedule(iTask->bh);
206 } else {
207 iTask->complete = 1;
211 static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
213 *iTask = (struct IscsiTask) {
214 .co = qemu_coroutine_self(),
215 .iscsilun = iscsilun,
219 static void
220 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
221 void *private_data)
223 IscsiAIOCB *acb = private_data;
225 acb->status = -ECANCELED;
226 iscsi_schedule_bh(acb);
229 static void
230 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
232 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
233 IscsiLun *iscsilun = acb->iscsilun;
235 if (acb->status != -EINPROGRESS) {
236 return;
239 /* send a task mgmt call to the target to cancel the task on the target */
240 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
241 iscsi_abort_task_cb, acb);
245 static const AIOCBInfo iscsi_aiocb_info = {
246 .aiocb_size = sizeof(IscsiAIOCB),
247 .cancel_async = iscsi_aio_cancel,
251 static void iscsi_process_read(void *arg);
252 static void iscsi_process_write(void *arg);
254 static void
255 iscsi_set_events(IscsiLun *iscsilun)
257 struct iscsi_context *iscsi = iscsilun->iscsi;
258 int ev;
260 /* We always register a read handler. */
261 ev = POLLIN;
262 ev |= iscsi_which_events(iscsi);
263 if (ev != iscsilun->events) {
264 aio_set_fd_handler(iscsilun->aio_context,
265 iscsi_get_fd(iscsi),
266 iscsi_process_read,
267 (ev & POLLOUT) ? iscsi_process_write : NULL,
268 iscsilun);
272 iscsilun->events = ev;
275 static void
276 iscsi_process_read(void *arg)
278 IscsiLun *iscsilun = arg;
279 struct iscsi_context *iscsi = iscsilun->iscsi;
281 iscsi_service(iscsi, POLLIN);
282 iscsi_set_events(iscsilun);
285 static void
286 iscsi_process_write(void *arg)
288 IscsiLun *iscsilun = arg;
289 struct iscsi_context *iscsi = iscsilun->iscsi;
291 iscsi_service(iscsi, POLLOUT);
292 iscsi_set_events(iscsilun);
295 static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
297 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
300 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
302 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
305 static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
306 IscsiLun *iscsilun)
308 if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
309 (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
310 error_report("iSCSI misaligned request: "
311 "iscsilun->block_size %u, sector_num %" PRIi64
312 ", nb_sectors %d",
313 iscsilun->block_size, sector_num, nb_sectors);
314 return 0;
316 return 1;
319 static unsigned long *iscsi_allocationmap_init(IscsiLun *iscsilun)
321 return bitmap_try_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks,
322 iscsilun),
323 iscsilun->cluster_sectors));
326 static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num,
327 int nb_sectors)
329 if (iscsilun->allocationmap == NULL) {
330 return;
332 bitmap_set(iscsilun->allocationmap,
333 sector_num / iscsilun->cluster_sectors,
334 DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors));
337 static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num,
338 int nb_sectors)
340 int64_t cluster_num, nb_clusters;
341 if (iscsilun->allocationmap == NULL) {
342 return;
344 cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors);
345 nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors
346 - cluster_num;
347 if (nb_clusters > 0) {
348 bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters);
352 static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,
353 int64_t sector_num, int nb_sectors,
354 QEMUIOVector *iov)
356 IscsiLun *iscsilun = bs->opaque;
357 struct IscsiTask iTask;
358 uint64_t lba;
359 uint32_t num_sectors;
361 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
362 return -EINVAL;
365 lba = sector_qemu2lun(sector_num, iscsilun);
366 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
367 iscsi_co_init_iscsitask(iscsilun, &iTask);
368 retry:
369 if (iscsilun->use_16_for_rw) {
370 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
371 NULL, num_sectors * iscsilun->block_size,
372 iscsilun->block_size, 0, 0, 0, 0, 0,
373 iscsi_co_generic_cb, &iTask);
374 } else {
375 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,
376 NULL, num_sectors * iscsilun->block_size,
377 iscsilun->block_size, 0, 0, 0, 0, 0,
378 iscsi_co_generic_cb, &iTask);
380 if (iTask.task == NULL) {
381 return -ENOMEM;
383 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
384 iov->niov);
385 while (!iTask.complete) {
386 iscsi_set_events(iscsilun);
387 qemu_coroutine_yield();
390 if (iTask.task != NULL) {
391 scsi_free_scsi_task(iTask.task);
392 iTask.task = NULL;
395 if (iTask.do_retry) {
396 iTask.complete = 0;
397 goto retry;
400 if (iTask.status != SCSI_STATUS_GOOD) {
401 return -EIO;
404 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
406 return 0;
410 static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun,
411 int64_t sector_num, int nb_sectors)
413 unsigned long size;
414 if (iscsilun->allocationmap == NULL) {
415 return true;
417 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
418 return !(find_next_bit(iscsilun->allocationmap, size,
419 sector_num / iscsilun->cluster_sectors) == size);
422 static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
423 int64_t sector_num,
424 int nb_sectors, int *pnum)
426 IscsiLun *iscsilun = bs->opaque;
427 struct scsi_get_lba_status *lbas = NULL;
428 struct scsi_lba_status_descriptor *lbasd = NULL;
429 struct IscsiTask iTask;
430 int64_t ret;
432 iscsi_co_init_iscsitask(iscsilun, &iTask);
434 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
435 ret = -EINVAL;
436 goto out;
439 /* default to all sectors allocated */
440 ret = BDRV_BLOCK_DATA;
441 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
442 *pnum = nb_sectors;
444 /* LUN does not support logical block provisioning */
445 if (iscsilun->lbpme == 0) {
446 goto out;
449 retry:
450 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
451 sector_qemu2lun(sector_num, iscsilun),
452 8 + 16, iscsi_co_generic_cb,
453 &iTask) == NULL) {
454 ret = -ENOMEM;
455 goto out;
458 while (!iTask.complete) {
459 iscsi_set_events(iscsilun);
460 qemu_coroutine_yield();
463 if (iTask.do_retry) {
464 if (iTask.task != NULL) {
465 scsi_free_scsi_task(iTask.task);
466 iTask.task = NULL;
468 iTask.complete = 0;
469 goto retry;
472 if (iTask.status != SCSI_STATUS_GOOD) {
473 /* in case the get_lba_status_callout fails (i.e.
474 * because the device is busy or the cmd is not
475 * supported) we pretend all blocks are allocated
476 * for backwards compatibility */
477 goto out;
480 lbas = scsi_datain_unmarshall(iTask.task);
481 if (lbas == NULL) {
482 ret = -EIO;
483 goto out;
486 lbasd = &lbas->descriptors[0];
488 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
489 ret = -EIO;
490 goto out;
493 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
495 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
496 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
497 ret &= ~BDRV_BLOCK_DATA;
498 if (iscsilun->lbprz) {
499 ret |= BDRV_BLOCK_ZERO;
503 if (ret & BDRV_BLOCK_ZERO) {
504 iscsi_allocationmap_clear(iscsilun, sector_num, *pnum);
505 } else {
506 iscsi_allocationmap_set(iscsilun, sector_num, *pnum);
509 if (*pnum > nb_sectors) {
510 *pnum = nb_sectors;
512 out:
513 if (iTask.task != NULL) {
514 scsi_free_scsi_task(iTask.task);
516 return ret;
519 static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
520 int64_t sector_num, int nb_sectors,
521 QEMUIOVector *iov)
523 IscsiLun *iscsilun = bs->opaque;
524 struct IscsiTask iTask;
525 uint64_t lba;
526 uint32_t num_sectors;
528 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
529 return -EINVAL;
532 if (iscsilun->lbprz && nb_sectors >= ISCSI_CHECKALLOC_THRES &&
533 !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
534 int64_t ret;
535 int pnum;
536 ret = iscsi_co_get_block_status(bs, sector_num, INT_MAX, &pnum);
537 if (ret < 0) {
538 return ret;
540 if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) {
541 qemu_iovec_memset(iov, 0, 0x00, iov->size);
542 return 0;
546 lba = sector_qemu2lun(sector_num, iscsilun);
547 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
549 iscsi_co_init_iscsitask(iscsilun, &iTask);
550 retry:
551 if (iscsilun->use_16_for_rw) {
552 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
553 num_sectors * iscsilun->block_size,
554 iscsilun->block_size, 0, 0, 0, 0, 0,
555 iscsi_co_generic_cb, &iTask);
556 } else {
557 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
558 num_sectors * iscsilun->block_size,
559 iscsilun->block_size,
560 0, 0, 0, 0, 0,
561 iscsi_co_generic_cb, &iTask);
563 if (iTask.task == NULL) {
564 return -ENOMEM;
566 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
568 while (!iTask.complete) {
569 iscsi_set_events(iscsilun);
570 qemu_coroutine_yield();
573 if (iTask.task != NULL) {
574 scsi_free_scsi_task(iTask.task);
575 iTask.task = NULL;
578 if (iTask.do_retry) {
579 iTask.complete = 0;
580 goto retry;
583 if (iTask.status != SCSI_STATUS_GOOD) {
584 return -EIO;
587 return 0;
590 static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
592 IscsiLun *iscsilun = bs->opaque;
593 struct IscsiTask iTask;
595 if (bs->sg) {
596 return 0;
599 iscsi_co_init_iscsitask(iscsilun, &iTask);
601 retry:
602 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
603 0, iscsi_co_generic_cb, &iTask) == NULL) {
604 return -ENOMEM;
607 while (!iTask.complete) {
608 iscsi_set_events(iscsilun);
609 qemu_coroutine_yield();
612 if (iTask.task != NULL) {
613 scsi_free_scsi_task(iTask.task);
614 iTask.task = NULL;
617 if (iTask.do_retry) {
618 iTask.complete = 0;
619 goto retry;
622 if (iTask.status != SCSI_STATUS_GOOD) {
623 return -EIO;
626 return 0;
629 #ifdef __linux__
630 static void
631 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
632 void *command_data, void *opaque)
634 IscsiAIOCB *acb = opaque;
636 g_free(acb->buf);
637 acb->buf = NULL;
639 acb->status = 0;
640 if (status < 0) {
641 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
642 iscsi_get_error(iscsi));
643 acb->status = -EIO;
646 acb->ioh->driver_status = 0;
647 acb->ioh->host_status = 0;
648 acb->ioh->resid = 0;
650 #define SG_ERR_DRIVER_SENSE 0x08
652 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
653 int ss;
655 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
657 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
658 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
659 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
660 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
663 iscsi_schedule_bh(acb);
666 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
667 unsigned long int req, void *buf,
668 BlockDriverCompletionFunc *cb, void *opaque)
670 IscsiLun *iscsilun = bs->opaque;
671 struct iscsi_context *iscsi = iscsilun->iscsi;
672 struct iscsi_data data;
673 IscsiAIOCB *acb;
675 assert(req == SG_IO);
677 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
679 acb->iscsilun = iscsilun;
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_unref(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 scsi_task_set_iov_out(acb->task,
717 (struct scsi_iovec *) acb->ioh->dxferp,
718 acb->ioh->iovec_count);
722 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
723 iscsi_aio_ioctl_cb,
724 (data.size > 0) ? &data : NULL,
725 acb) != 0) {
726 scsi_free_scsi_task(acb->task);
727 qemu_aio_unref(acb);
728 return NULL;
731 /* tell libiscsi to read straight into the buffer we got from ioctl */
732 if (acb->task->xfer_dir == SCSI_XFER_READ) {
733 if (acb->ioh->iovec_count == 0) {
734 scsi_task_add_data_in_buffer(acb->task,
735 acb->ioh->dxfer_len,
736 acb->ioh->dxferp);
737 } else {
738 scsi_task_set_iov_in(acb->task,
739 (struct scsi_iovec *) acb->ioh->dxferp,
740 acb->ioh->iovec_count);
744 iscsi_set_events(iscsilun);
746 return &acb->common;
749 static void ioctl_cb(void *opaque, int status)
751 int *p_status = opaque;
752 *p_status = status;
755 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
757 IscsiLun *iscsilun = bs->opaque;
758 int status;
760 switch (req) {
761 case SG_GET_VERSION_NUM:
762 *(int *)buf = 30000;
763 break;
764 case SG_GET_SCSI_ID:
765 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
766 break;
767 case SG_IO:
768 status = -EINPROGRESS;
769 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
771 while (status == -EINPROGRESS) {
772 aio_poll(iscsilun->aio_context, true);
775 return 0;
776 default:
777 return -1;
779 return 0;
781 #endif
783 static int64_t
784 iscsi_getlength(BlockDriverState *bs)
786 IscsiLun *iscsilun = bs->opaque;
787 int64_t len;
789 len = iscsilun->num_blocks;
790 len *= iscsilun->block_size;
792 return len;
795 static int
796 coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
797 int nb_sectors)
799 IscsiLun *iscsilun = bs->opaque;
800 struct IscsiTask iTask;
801 struct unmap_list list;
803 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
804 return -EINVAL;
807 if (!iscsilun->lbp.lbpu) {
808 /* UNMAP is not supported by the target */
809 return 0;
812 list.lba = sector_qemu2lun(sector_num, iscsilun);
813 list.num = sector_qemu2lun(nb_sectors, iscsilun);
815 iscsi_co_init_iscsitask(iscsilun, &iTask);
816 retry:
817 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
818 iscsi_co_generic_cb, &iTask) == NULL) {
819 return -ENOMEM;
822 while (!iTask.complete) {
823 iscsi_set_events(iscsilun);
824 qemu_coroutine_yield();
827 if (iTask.task != NULL) {
828 scsi_free_scsi_task(iTask.task);
829 iTask.task = NULL;
832 if (iTask.do_retry) {
833 iTask.complete = 0;
834 goto retry;
837 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
838 /* the target might fail with a check condition if it
839 is not happy with the alignment of the UNMAP request
840 we silently fail in this case */
841 return 0;
844 if (iTask.status != SCSI_STATUS_GOOD) {
845 return -EIO;
848 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
850 return 0;
853 static int
854 coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
855 int nb_sectors, BdrvRequestFlags flags)
857 IscsiLun *iscsilun = bs->opaque;
858 struct IscsiTask iTask;
859 uint64_t lba;
860 uint32_t nb_blocks;
861 bool use_16_for_ws = iscsilun->use_16_for_rw;
863 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
864 return -EINVAL;
867 if (flags & BDRV_REQ_MAY_UNMAP) {
868 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
869 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
870 use_16_for_ws = true;
872 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
873 /* WRITESAME16 with UNMAP is not supported by the target,
874 * fall back and try WRITESAME10/16 without UNMAP */
875 flags &= ~BDRV_REQ_MAY_UNMAP;
876 use_16_for_ws = iscsilun->use_16_for_rw;
880 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
881 /* WRITESAME without UNMAP is not supported by the target */
882 return -ENOTSUP;
885 lba = sector_qemu2lun(sector_num, iscsilun);
886 nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
888 if (iscsilun->zeroblock == NULL) {
889 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size);
890 if (iscsilun->zeroblock == NULL) {
891 return -ENOMEM;
895 iscsi_co_init_iscsitask(iscsilun, &iTask);
896 retry:
897 if (use_16_for_ws) {
898 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
899 iscsilun->zeroblock, iscsilun->block_size,
900 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
901 0, 0, iscsi_co_generic_cb, &iTask);
902 } else {
903 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
904 iscsilun->zeroblock, iscsilun->block_size,
905 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
906 0, 0, iscsi_co_generic_cb, &iTask);
908 if (iTask.task == NULL) {
909 return -ENOMEM;
912 while (!iTask.complete) {
913 iscsi_set_events(iscsilun);
914 qemu_coroutine_yield();
917 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
918 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
919 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
920 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
921 /* WRITE SAME is not supported by the target */
922 iscsilun->has_write_same = false;
923 scsi_free_scsi_task(iTask.task);
924 return -ENOTSUP;
927 if (iTask.task != NULL) {
928 scsi_free_scsi_task(iTask.task);
929 iTask.task = NULL;
932 if (iTask.do_retry) {
933 iTask.complete = 0;
934 goto retry;
937 if (iTask.status != SCSI_STATUS_GOOD) {
938 return -EIO;
941 if (flags & BDRV_REQ_MAY_UNMAP) {
942 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
943 } else {
944 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
947 return 0;
950 static void parse_chap(struct iscsi_context *iscsi, const char *target,
951 Error **errp)
953 QemuOptsList *list;
954 QemuOpts *opts;
955 const char *user = NULL;
956 const char *password = NULL;
958 list = qemu_find_opts("iscsi");
959 if (!list) {
960 return;
963 opts = qemu_opts_find(list, target);
964 if (opts == NULL) {
965 opts = QTAILQ_FIRST(&list->head);
966 if (!opts) {
967 return;
971 user = qemu_opt_get(opts, "user");
972 if (!user) {
973 return;
976 password = qemu_opt_get(opts, "password");
977 if (!password) {
978 error_setg(errp, "CHAP username specified but no password was given");
979 return;
982 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
983 error_setg(errp, "Failed to set initiator username and password");
987 static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
988 Error **errp)
990 QemuOptsList *list;
991 QemuOpts *opts;
992 const char *digest = NULL;
994 list = qemu_find_opts("iscsi");
995 if (!list) {
996 return;
999 opts = qemu_opts_find(list, target);
1000 if (opts == NULL) {
1001 opts = QTAILQ_FIRST(&list->head);
1002 if (!opts) {
1003 return;
1007 digest = qemu_opt_get(opts, "header-digest");
1008 if (!digest) {
1009 return;
1012 if (!strcmp(digest, "CRC32C")) {
1013 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1014 } else if (!strcmp(digest, "NONE")) {
1015 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1016 } else if (!strcmp(digest, "CRC32C-NONE")) {
1017 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1018 } else if (!strcmp(digest, "NONE-CRC32C")) {
1019 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1020 } else {
1021 error_setg(errp, "Invalid header-digest setting : %s", digest);
1025 static char *parse_initiator_name(const char *target)
1027 QemuOptsList *list;
1028 QemuOpts *opts;
1029 const char *name;
1030 char *iscsi_name;
1031 UuidInfo *uuid_info;
1033 list = qemu_find_opts("iscsi");
1034 if (list) {
1035 opts = qemu_opts_find(list, target);
1036 if (!opts) {
1037 opts = QTAILQ_FIRST(&list->head);
1039 if (opts) {
1040 name = qemu_opt_get(opts, "initiator-name");
1041 if (name) {
1042 return g_strdup(name);
1047 uuid_info = qmp_query_uuid(NULL);
1048 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1049 name = qemu_get_vm_name();
1050 } else {
1051 name = uuid_info->UUID;
1053 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1054 name ? ":" : "", name ? name : "");
1055 qapi_free_UuidInfo(uuid_info);
1056 return iscsi_name;
1059 static void iscsi_nop_timed_event(void *opaque)
1061 IscsiLun *iscsilun = opaque;
1063 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
1064 error_report("iSCSI: NOP timeout. Reconnecting...");
1065 iscsi_reconnect(iscsilun->iscsi);
1068 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1069 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1070 return;
1073 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1074 iscsi_set_events(iscsilun);
1077 static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
1079 struct scsi_task *task = NULL;
1080 struct scsi_readcapacity10 *rc10 = NULL;
1081 struct scsi_readcapacity16 *rc16 = NULL;
1082 int retries = ISCSI_CMD_RETRIES;
1084 do {
1085 if (task != NULL) {
1086 scsi_free_scsi_task(task);
1087 task = NULL;
1090 switch (iscsilun->type) {
1091 case TYPE_DISK:
1092 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1093 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1094 rc16 = scsi_datain_unmarshall(task);
1095 if (rc16 == NULL) {
1096 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1097 } else {
1098 iscsilun->block_size = rc16->block_length;
1099 iscsilun->num_blocks = rc16->returned_lba + 1;
1100 iscsilun->lbpme = rc16->lbpme;
1101 iscsilun->lbprz = rc16->lbprz;
1102 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
1105 break;
1106 case TYPE_ROM:
1107 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1108 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1109 rc10 = scsi_datain_unmarshall(task);
1110 if (rc10 == NULL) {
1111 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1112 } else {
1113 iscsilun->block_size = rc10->block_size;
1114 if (rc10->lba == 0) {
1115 /* blank disk loaded */
1116 iscsilun->num_blocks = 0;
1117 } else {
1118 iscsilun->num_blocks = rc10->lba + 1;
1122 break;
1123 default:
1124 return;
1126 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1127 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1128 && retries-- > 0);
1130 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1131 error_setg(errp, "iSCSI: failed to send readcapacity10 command.");
1133 if (task) {
1134 scsi_free_scsi_task(task);
1138 /* TODO Convert to fine grained options */
1139 static QemuOptsList runtime_opts = {
1140 .name = "iscsi",
1141 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1142 .desc = {
1144 .name = "filename",
1145 .type = QEMU_OPT_STRING,
1146 .help = "URL to the iscsi image",
1148 { /* end of list */ }
1152 static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
1153 int evpd, int pc, void **inq, Error **errp)
1155 int full_size;
1156 struct scsi_task *task = NULL;
1157 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1158 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1159 goto fail;
1161 full_size = scsi_datain_getfullsize(task);
1162 if (full_size > task->datain.size) {
1163 scsi_free_scsi_task(task);
1165 /* we need more data for the full list */
1166 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1167 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1168 goto fail;
1172 *inq = scsi_datain_unmarshall(task);
1173 if (*inq == NULL) {
1174 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1175 goto fail_with_err;
1178 return task;
1180 fail:
1181 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1182 iscsi_get_error(iscsi));
1183 fail_with_err:
1184 if (task != NULL) {
1185 scsi_free_scsi_task(task);
1187 return NULL;
1190 static void iscsi_detach_aio_context(BlockDriverState *bs)
1192 IscsiLun *iscsilun = bs->opaque;
1194 aio_set_fd_handler(iscsilun->aio_context,
1195 iscsi_get_fd(iscsilun->iscsi),
1196 NULL, NULL, NULL);
1197 iscsilun->events = 0;
1199 if (iscsilun->nop_timer) {
1200 timer_del(iscsilun->nop_timer);
1201 timer_free(iscsilun->nop_timer);
1202 iscsilun->nop_timer = NULL;
1206 static void iscsi_attach_aio_context(BlockDriverState *bs,
1207 AioContext *new_context)
1209 IscsiLun *iscsilun = bs->opaque;
1211 iscsilun->aio_context = new_context;
1212 iscsi_set_events(iscsilun);
1214 /* Set up a timer for sending out iSCSI NOPs */
1215 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1216 QEMU_CLOCK_REALTIME, SCALE_MS,
1217 iscsi_nop_timed_event, iscsilun);
1218 timer_mod(iscsilun->nop_timer,
1219 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1223 * We support iscsi url's on the form
1224 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1226 * Note: flags are currently not used by iscsi_open. If this function
1227 * is changed such that flags are used, please examine iscsi_reopen_prepare()
1228 * to see if needs to be changed as well.
1230 static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1231 Error **errp)
1233 IscsiLun *iscsilun = bs->opaque;
1234 struct iscsi_context *iscsi = NULL;
1235 struct iscsi_url *iscsi_url = NULL;
1236 struct scsi_task *task = NULL;
1237 struct scsi_inquiry_standard *inq = NULL;
1238 struct scsi_inquiry_supported_pages *inq_vpd;
1239 char *initiator_name = NULL;
1240 QemuOpts *opts;
1241 Error *local_err = NULL;
1242 const char *filename;
1243 int i, ret;
1245 if ((BDRV_SECTOR_SIZE % 512) != 0) {
1246 error_setg(errp, "iSCSI: Invalid BDRV_SECTOR_SIZE. "
1247 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1248 "of 512", BDRV_SECTOR_SIZE);
1249 return -EINVAL;
1252 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
1253 qemu_opts_absorb_qdict(opts, options, &local_err);
1254 if (local_err) {
1255 error_propagate(errp, local_err);
1256 ret = -EINVAL;
1257 goto out;
1260 filename = qemu_opt_get(opts, "filename");
1262 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1263 if (iscsi_url == NULL) {
1264 error_setg(errp, "Failed to parse URL : %s", filename);
1265 ret = -EINVAL;
1266 goto out;
1269 memset(iscsilun, 0, sizeof(IscsiLun));
1271 initiator_name = parse_initiator_name(iscsi_url->target);
1273 iscsi = iscsi_create_context(initiator_name);
1274 if (iscsi == NULL) {
1275 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
1276 ret = -ENOMEM;
1277 goto out;
1280 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1281 error_setg(errp, "iSCSI: Failed to set target name.");
1282 ret = -EINVAL;
1283 goto out;
1286 if (iscsi_url->user != NULL) {
1287 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1288 iscsi_url->passwd);
1289 if (ret != 0) {
1290 error_setg(errp, "Failed to set initiator username and password");
1291 ret = -EINVAL;
1292 goto out;
1296 /* check if we got CHAP username/password via the options */
1297 parse_chap(iscsi, iscsi_url->target, &local_err);
1298 if (local_err != NULL) {
1299 error_propagate(errp, local_err);
1300 ret = -EINVAL;
1301 goto out;
1304 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1305 error_setg(errp, "iSCSI: Failed to set session type to normal.");
1306 ret = -EINVAL;
1307 goto out;
1310 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1312 /* check if we got HEADER_DIGEST via the options */
1313 parse_header_digest(iscsi, iscsi_url->target, &local_err);
1314 if (local_err != NULL) {
1315 error_propagate(errp, local_err);
1316 ret = -EINVAL;
1317 goto out;
1320 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1321 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
1322 iscsi_get_error(iscsi));
1323 ret = -EINVAL;
1324 goto out;
1327 iscsilun->iscsi = iscsi;
1328 iscsilun->aio_context = bdrv_get_aio_context(bs);
1329 iscsilun->lun = iscsi_url->lun;
1330 iscsilun->has_write_same = true;
1332 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1333 (void **) &inq, errp);
1334 if (task == NULL) {
1335 ret = -EINVAL;
1336 goto out;
1338 iscsilun->type = inq->periperal_device_type;
1339 scsi_free_scsi_task(task);
1340 task = NULL;
1342 iscsi_readcapacity_sync(iscsilun, &local_err);
1343 if (local_err != NULL) {
1344 error_propagate(errp, local_err);
1345 ret = -EINVAL;
1346 goto out;
1348 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1349 bs->request_alignment = iscsilun->block_size;
1351 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1352 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1353 * will try to read from the device to guess the image format.
1355 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
1356 bs->sg = 1;
1359 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1360 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1361 (void **) &inq_vpd, errp);
1362 if (task == NULL) {
1363 ret = -EINVAL;
1364 goto out;
1366 for (i = 0; i < inq_vpd->num_pages; i++) {
1367 struct scsi_task *inq_task;
1368 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1369 struct scsi_inquiry_block_limits *inq_bl;
1370 switch (inq_vpd->pages[i]) {
1371 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1372 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1373 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1374 (void **) &inq_lbp, errp);
1375 if (inq_task == NULL) {
1376 ret = -EINVAL;
1377 goto out;
1379 memcpy(&iscsilun->lbp, inq_lbp,
1380 sizeof(struct scsi_inquiry_logical_block_provisioning));
1381 scsi_free_scsi_task(inq_task);
1382 break;
1383 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1384 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1385 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1386 (void **) &inq_bl, errp);
1387 if (inq_task == NULL) {
1388 ret = -EINVAL;
1389 goto out;
1391 memcpy(&iscsilun->bl, inq_bl,
1392 sizeof(struct scsi_inquiry_block_limits));
1393 scsi_free_scsi_task(inq_task);
1394 break;
1395 default:
1396 break;
1399 scsi_free_scsi_task(task);
1400 task = NULL;
1402 iscsi_attach_aio_context(bs, iscsilun->aio_context);
1404 /* Guess the internal cluster (page) size of the iscsi target by the means
1405 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1406 * reasonable size */
1407 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
1408 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1409 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
1410 iscsilun->block_size) >> BDRV_SECTOR_BITS;
1411 if (iscsilun->lbprz && !(bs->open_flags & BDRV_O_NOCACHE)) {
1412 iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun);
1413 if (iscsilun->allocationmap == NULL) {
1414 ret = -ENOMEM;
1419 out:
1420 qemu_opts_del(opts);
1421 g_free(initiator_name);
1422 if (iscsi_url != NULL) {
1423 iscsi_destroy_url(iscsi_url);
1425 if (task != NULL) {
1426 scsi_free_scsi_task(task);
1429 if (ret) {
1430 if (iscsi != NULL) {
1431 iscsi_destroy_context(iscsi);
1433 memset(iscsilun, 0, sizeof(IscsiLun));
1435 return ret;
1438 static void iscsi_close(BlockDriverState *bs)
1440 IscsiLun *iscsilun = bs->opaque;
1441 struct iscsi_context *iscsi = iscsilun->iscsi;
1443 iscsi_detach_aio_context(bs);
1444 iscsi_destroy_context(iscsi);
1445 g_free(iscsilun->zeroblock);
1446 g_free(iscsilun->allocationmap);
1447 memset(iscsilun, 0, sizeof(IscsiLun));
1450 static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)
1452 IscsiLun *iscsilun = bs->opaque;
1454 /* We don't actually refresh here, but just return data queried in
1455 * iscsi_open(): iscsi targets don't change their limits. */
1456 if (iscsilun->lbp.lbpu) {
1457 if (iscsilun->bl.max_unmap < 0xffffffff) {
1458 bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap,
1459 iscsilun);
1461 bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1462 iscsilun);
1465 if (iscsilun->bl.max_ws_len < 0xffffffff) {
1466 bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len,
1467 iscsilun);
1469 if (iscsilun->lbp.lbpws) {
1470 bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1471 iscsilun);
1473 bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len,
1474 iscsilun);
1477 /* Since iscsi_open() ignores bdrv_flags, there is nothing to do here in
1478 * prepare. Note that this will not re-establish a connection with an iSCSI
1479 * target - it is effectively a NOP. */
1480 static int iscsi_reopen_prepare(BDRVReopenState *state,
1481 BlockReopenQueue *queue, Error **errp)
1483 /* NOP */
1484 return 0;
1487 static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1489 IscsiLun *iscsilun = bs->opaque;
1490 Error *local_err = NULL;
1492 if (iscsilun->type != TYPE_DISK) {
1493 return -ENOTSUP;
1496 iscsi_readcapacity_sync(iscsilun, &local_err);
1497 if (local_err != NULL) {
1498 error_free(local_err);
1499 return -EIO;
1502 if (offset > iscsi_getlength(bs)) {
1503 return -EINVAL;
1506 if (iscsilun->allocationmap != NULL) {
1507 g_free(iscsilun->allocationmap);
1508 iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun);
1511 return 0;
1514 static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp)
1516 int ret = 0;
1517 int64_t total_size = 0;
1518 BlockDriverState *bs;
1519 IscsiLun *iscsilun = NULL;
1520 QDict *bs_options;
1522 bs = bdrv_new("", &error_abort);
1524 /* Read out options */
1525 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1526 BDRV_SECTOR_SIZE);
1527 bs->opaque = g_new0(struct IscsiLun, 1);
1528 iscsilun = bs->opaque;
1530 bs_options = qdict_new();
1531 qdict_put(bs_options, "filename", qstring_from_str(filename));
1532 ret = iscsi_open(bs, bs_options, 0, NULL);
1533 QDECREF(bs_options);
1535 if (ret != 0) {
1536 goto out;
1538 iscsi_detach_aio_context(bs);
1539 if (iscsilun->type != TYPE_DISK) {
1540 ret = -ENODEV;
1541 goto out;
1543 if (bs->total_sectors < total_size) {
1544 ret = -ENOSPC;
1545 goto out;
1548 ret = 0;
1549 out:
1550 if (iscsilun->iscsi != NULL) {
1551 iscsi_destroy_context(iscsilun->iscsi);
1553 g_free(bs->opaque);
1554 bs->opaque = NULL;
1555 bdrv_unref(bs);
1556 return ret;
1559 static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1561 IscsiLun *iscsilun = bs->opaque;
1562 bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz;
1563 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
1564 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
1565 return 0;
1568 static QemuOptsList iscsi_create_opts = {
1569 .name = "iscsi-create-opts",
1570 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
1571 .desc = {
1573 .name = BLOCK_OPT_SIZE,
1574 .type = QEMU_OPT_SIZE,
1575 .help = "Virtual disk size"
1577 { /* end of list */ }
1581 static BlockDriver bdrv_iscsi = {
1582 .format_name = "iscsi",
1583 .protocol_name = "iscsi",
1585 .instance_size = sizeof(IscsiLun),
1586 .bdrv_needs_filename = true,
1587 .bdrv_file_open = iscsi_open,
1588 .bdrv_close = iscsi_close,
1589 .bdrv_create = iscsi_create,
1590 .create_opts = &iscsi_create_opts,
1591 .bdrv_reopen_prepare = iscsi_reopen_prepare,
1593 .bdrv_getlength = iscsi_getlength,
1594 .bdrv_get_info = iscsi_get_info,
1595 .bdrv_truncate = iscsi_truncate,
1596 .bdrv_refresh_limits = iscsi_refresh_limits,
1598 .bdrv_co_get_block_status = iscsi_co_get_block_status,
1599 .bdrv_co_discard = iscsi_co_discard,
1600 .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
1601 .bdrv_co_readv = iscsi_co_readv,
1602 .bdrv_co_writev = iscsi_co_writev,
1603 .bdrv_co_flush_to_disk = iscsi_co_flush,
1605 #ifdef __linux__
1606 .bdrv_ioctl = iscsi_ioctl,
1607 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1608 #endif
1610 .bdrv_detach_aio_context = iscsi_detach_aio_context,
1611 .bdrv_attach_aio_context = iscsi_attach_aio_context,
1614 static QemuOptsList qemu_iscsi_opts = {
1615 .name = "iscsi",
1616 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1617 .desc = {
1619 .name = "user",
1620 .type = QEMU_OPT_STRING,
1621 .help = "username for CHAP authentication to target",
1623 .name = "password",
1624 .type = QEMU_OPT_STRING,
1625 .help = "password for CHAP authentication to target",
1627 .name = "header-digest",
1628 .type = QEMU_OPT_STRING,
1629 .help = "HeaderDigest setting. "
1630 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1632 .name = "initiator-name",
1633 .type = QEMU_OPT_STRING,
1634 .help = "Initiator iqn name to use when connecting",
1636 { /* end of list */ }
1640 static void iscsi_block_init(void)
1642 bdrv_register(&bdrv_iscsi);
1643 qemu_add_opts(&qemu_iscsi_opts);
1646 block_init(iscsi_block_init);