]> git.proxmox.com Git - mirror_qemu.git/blame - block/iscsi.c
block/iscsi: speed up read for unallocated sectors
[mirror_qemu.git] / block / iscsi.c
CommitLineData
c589b249
RS
1/*
2 * QEMU Block driver for iSCSI images
3 *
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
2af8a1a7 5 * Copyright (c) 2012-2013 Peter Lieven <pl@kamp.de>
c589b249
RS
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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.
24 */
25
26#include "config-host.h"
27
28#include <poll.h>
f4dfa67f 29#include <arpa/inet.h>
c589b249 30#include "qemu-common.h"
1de7afc9
PB
31#include "qemu/config-file.h"
32#include "qemu/error-report.h"
b03c3805
PL
33#include "qemu/bitops.h"
34#include "qemu/bitmap.h"
737e150e 35#include "block/block_int.h"
c589b249 36#include "trace.h"
0d09e41a 37#include "block/scsi.h"
0a53f010 38#include "qemu/iov.h"
5accc840
PB
39#include "sysemu/sysemu.h"
40#include "qmp-commands.h"
c589b249
RS
41
42#include <iscsi/iscsi.h>
43#include <iscsi/scsi-lowlevel.h>
44
98392453
RS
45#ifdef __linux__
46#include <scsi/sg.h>
0d09e41a 47#include <block/scsi.h>
98392453 48#endif
c589b249
RS
49
50typedef struct IscsiLun {
51 struct iscsi_context *iscsi;
52 int lun;
dbfff6d7 53 enum scsi_inquiry_peripheral_device_type type;
c589b249 54 int block_size;
c7b4a952 55 uint64_t num_blocks;
c9b9f682 56 int events;
5b5d34ec 57 QEMUTimer *nop_timer;
f18a7cbb
PL
58 uint8_t lbpme;
59 uint8_t lbprz;
fa6252b0 60 uint8_t has_write_same;
f18a7cbb
PL
61 struct scsi_inquiry_logical_block_provisioning lbp;
62 struct scsi_inquiry_block_limits bl;
d4cd9615 63 unsigned char *zeroblock;
b03c3805
PL
64 unsigned long *allocationmap;
65 int cluster_sectors;
c589b249
RS
66} IscsiLun;
67
54a5c1d5
PL
68typedef struct IscsiTask {
69 int status;
70 int complete;
71 int retries;
72 int do_retry;
73 struct scsi_task *task;
74 Coroutine *co;
8b9dfe90 75 QEMUBH *bh;
54a5c1d5
PL
76} IscsiTask;
77
c589b249
RS
78typedef struct IscsiAIOCB {
79 BlockDriverAIOCB common;
80 QEMUIOVector *qiov;
81 QEMUBH *bh;
82 IscsiLun *iscsilun;
83 struct scsi_task *task;
84 uint8_t *buf;
85 int status;
86 int canceled;
1dde716e 87 int retries;
1dde716e
PL
88 int64_t sector_num;
89 int nb_sectors;
98392453
RS
90#ifdef __linux__
91 sg_io_hdr_t *ioh;
92#endif
c589b249
RS
93} IscsiAIOCB;
94
5b5d34ec
PL
95#define NOP_INTERVAL 5000
96#define MAX_NOP_FAILURES 3
1dde716e 97#define ISCSI_CMD_RETRIES 5
b03c3805 98#define ISCSI_CHECKALLOC_THRES 63
5b5d34ec 99
27cbd828 100static void
cfb3f506 101iscsi_bh_cb(void *p)
27cbd828
PB
102{
103 IscsiAIOCB *acb = p;
104
105 qemu_bh_delete(acb->bh);
106
4790b03d
PB
107 g_free(acb->buf);
108 acb->buf = NULL;
109
27cbd828
PB
110 if (acb->canceled == 0) {
111 acb->common.cb(acb->common.opaque, acb->status);
112 }
113
1bd075f2
PB
114 if (acb->task != NULL) {
115 scsi_free_scsi_task(acb->task);
116 acb->task = NULL;
117 }
118
27cbd828
PB
119 qemu_aio_release(acb);
120}
121
cfb3f506
PB
122static void
123iscsi_schedule_bh(IscsiAIOCB *acb)
27cbd828 124{
1bd075f2
PB
125 if (acb->bh) {
126 return;
127 }
cfb3f506 128 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
27cbd828 129 qemu_bh_schedule(acb->bh);
27cbd828
PB
130}
131
8b9dfe90
PL
132static void iscsi_co_generic_bh_cb(void *opaque)
133{
134 struct IscsiTask *iTask = opaque;
135 qemu_bh_delete(iTask->bh);
136 qemu_coroutine_enter(iTask->co, NULL);
137}
138
54a5c1d5
PL
139static void
140iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
141 void *command_data, void *opaque)
142{
143 struct IscsiTask *iTask = opaque;
144 struct scsi_task *task = command_data;
145
146 iTask->complete = 1;
147 iTask->status = status;
148 iTask->do_retry = 0;
149 iTask->task = task;
150
151 if (iTask->retries-- > 0 && status == SCSI_STATUS_CHECK_CONDITION
152 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
837c3901 153 error_report("iSCSI CheckCondition: %s", iscsi_get_error(iscsi));
54a5c1d5
PL
154 iTask->do_retry = 1;
155 goto out;
156 }
157
158 if (status != SCSI_STATUS_GOOD) {
837c3901 159 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi));
54a5c1d5
PL
160 }
161
162out:
163 if (iTask->co) {
8b9dfe90
PL
164 iTask->bh = qemu_bh_new(iscsi_co_generic_bh_cb, iTask);
165 qemu_bh_schedule(iTask->bh);
54a5c1d5
PL
166 }
167}
168
169static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
170{
171 *iTask = (struct IscsiTask) {
172 .co = qemu_coroutine_self(),
173 .retries = ISCSI_CMD_RETRIES,
174 };
175}
27cbd828 176
c589b249
RS
177static void
178iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
179 void *private_data)
180{
1bd075f2
PB
181 IscsiAIOCB *acb = private_data;
182
183 acb->status = -ECANCELED;
184 iscsi_schedule_bh(acb);
c589b249
RS
185}
186
187static void
188iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
189{
190 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
191 IscsiLun *iscsilun = acb->iscsilun;
192
1bd075f2
PB
193 if (acb->status != -EINPROGRESS) {
194 return;
195 }
196
b2090919 197 acb->canceled = 1;
c589b249 198
b2090919 199 /* send a task mgmt call to the target to cancel the task on the target */
64e69e80 200 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
1bd075f2 201 iscsi_abort_task_cb, acb);
b2090919 202
1bd075f2
PB
203 while (acb->status == -EINPROGRESS) {
204 qemu_aio_wait();
205 }
c589b249
RS
206}
207
d7331bed 208static const AIOCBInfo iscsi_aiocb_info = {
c589b249
RS
209 .aiocb_size = sizeof(IscsiAIOCB),
210 .cancel = iscsi_aio_cancel,
211};
212
213
214static void iscsi_process_read(void *arg);
215static void iscsi_process_write(void *arg);
216
c589b249
RS
217static void
218iscsi_set_events(IscsiLun *iscsilun)
219{
220 struct iscsi_context *iscsi = iscsilun->iscsi;
c9b9f682
RS
221 int ev;
222
223 /* We always register a read handler. */
224 ev = POLLIN;
225 ev |= iscsi_which_events(iscsi);
226 if (ev != iscsilun->events) {
227 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
228 iscsi_process_read,
229 (ev & POLLOUT) ? iscsi_process_write : NULL,
c9b9f682
RS
230 iscsilun);
231
232 }
233
c9b9f682 234 iscsilun->events = ev;
c589b249
RS
235}
236
237static void
238iscsi_process_read(void *arg)
239{
240 IscsiLun *iscsilun = arg;
241 struct iscsi_context *iscsi = iscsilun->iscsi;
242
243 iscsi_service(iscsi, POLLIN);
244 iscsi_set_events(iscsilun);
245}
246
247static void
248iscsi_process_write(void *arg)
249{
250 IscsiLun *iscsilun = arg;
251 struct iscsi_context *iscsi = iscsilun->iscsi;
252
253 iscsi_service(iscsi, POLLOUT);
254 iscsi_set_events(iscsilun);
255}
256
0777b5dd
PL
257static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
258{
259 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
260}
261
c589b249
RS
262static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
263{
264 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
265}
266
91bea4e2
PL
267static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
268 IscsiLun *iscsilun)
269{
270 if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
271 (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
f5075224
RJ
272 error_report("iSCSI misaligned request: "
273 "iscsilun->block_size %u, sector_num %" PRIi64
274 ", nb_sectors %d",
91bea4e2
PL
275 iscsilun->block_size, sector_num, nb_sectors);
276 return 0;
277 }
278 return 1;
279}
280
b03c3805
PL
281static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num,
282 int nb_sectors)
283{
284 if (iscsilun->allocationmap == NULL) {
285 return;
286 }
287 bitmap_set(iscsilun->allocationmap,
288 sector_num / iscsilun->cluster_sectors,
289 DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors));
290}
291
292static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num,
293 int nb_sectors)
294{
295 int64_t cluster_num, nb_clusters;
296 if (iscsilun->allocationmap == NULL) {
297 return;
298 }
299 cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors);
300 nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors
301 - cluster_num;
302 if (nb_clusters > 0) {
303 bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters);
304 }
305}
306
063c3378
PL
307static int coroutine_fn iscsi_co_writev(BlockDriverState *bs,
308 int64_t sector_num, int nb_sectors,
309 QEMUIOVector *iov)
c589b249 310{
063c3378
PL
311 IscsiLun *iscsilun = bs->opaque;
312 struct IscsiTask iTask;
f4dfa67f 313 uint64_t lba;
063c3378
PL
314 uint32_t num_sectors;
315 uint8_t *data = NULL;
316 uint8_t *buf = NULL;
c589b249 317
063c3378
PL
318 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
319 return -EINVAL;
320 }
7371d56f 321
063c3378
PL
322 lba = sector_qemu2lun(sector_num, iscsilun);
323 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
7371d56f 324#if !defined(LIBISCSI_FEATURE_IOVECTOR)
4cc841b5 325 /* if the iovec only contains one buffer we can pass it directly */
063c3378
PL
326 if (iov->niov == 1) {
327 data = iov->iov[0].iov_base;
4cc841b5 328 } else {
063c3378
PL
329 size_t size = MIN(nb_sectors * BDRV_SECTOR_SIZE, iov->size);
330 buf = g_malloc(size);
331 qemu_iovec_to_buf(iov, 0, buf, size);
332 data = buf;
4cc841b5 333 }
7371d56f 334#endif
063c3378
PL
335 iscsi_co_init_iscsitask(iscsilun, &iTask);
336retry:
337 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
338 data, num_sectors * iscsilun->block_size,
339 iscsilun->block_size, 0, 0, 0, 0, 0,
340 iscsi_co_generic_cb, &iTask);
341 if (iTask.task == NULL) {
342 g_free(buf);
92397116 343 return -ENOMEM;
f4dfa67f 344 }
7371d56f 345#if defined(LIBISCSI_FEATURE_IOVECTOR)
063c3378
PL
346 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
347 iov->niov);
7371d56f 348#endif
063c3378
PL
349 while (!iTask.complete) {
350 iscsi_set_events(iscsilun);
351 qemu_coroutine_yield();
c589b249
RS
352 }
353
063c3378
PL
354 if (iTask.task != NULL) {
355 scsi_free_scsi_task(iTask.task);
356 iTask.task = NULL;
91bea4e2
PL
357 }
358
063c3378 359 if (iTask.do_retry) {
837c3901 360 iTask.complete = 0;
063c3378 361 goto retry;
1dde716e
PL
362 }
363
063c3378 364 g_free(buf);
c589b249 365
063c3378
PL
366 if (iTask.status != SCSI_STATUS_GOOD) {
367 return -EIO;
c589b249
RS
368 }
369
b03c3805
PL
370 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
371
063c3378 372 return 0;
c589b249
RS
373}
374
b03c3805
PL
375
376static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun,
377 int64_t sector_num, int nb_sectors)
378{
379 unsigned long size;
380 if (iscsilun->allocationmap == NULL) {
381 return true;
382 }
383 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
384 return !(find_next_bit(iscsilun->allocationmap, size,
385 sector_num / iscsilun->cluster_sectors) == size);
386}
387
388
389#if defined(LIBISCSI_FEATURE_IOVECTOR)
390
391static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
392 int64_t sector_num,
393 int nb_sectors, int *pnum)
394{
395 IscsiLun *iscsilun = bs->opaque;
396 struct scsi_get_lba_status *lbas = NULL;
397 struct scsi_lba_status_descriptor *lbasd = NULL;
398 struct IscsiTask iTask;
399 int64_t ret;
400
401 iscsi_co_init_iscsitask(iscsilun, &iTask);
402
403 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
404 ret = -EINVAL;
405 goto out;
406 }
407
408 /* default to all sectors allocated */
409 ret = BDRV_BLOCK_DATA;
410 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
411 *pnum = nb_sectors;
412
413 /* LUN does not support logical block provisioning */
414 if (iscsilun->lbpme == 0) {
415 goto out;
416 }
417
418retry:
419 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
420 sector_qemu2lun(sector_num, iscsilun),
421 8 + 16, iscsi_co_generic_cb,
422 &iTask) == NULL) {
423 ret = -ENOMEM;
424 goto out;
425 }
426
427 while (!iTask.complete) {
428 iscsi_set_events(iscsilun);
429 qemu_coroutine_yield();
430 }
431
432 if (iTask.do_retry) {
433 if (iTask.task != NULL) {
434 scsi_free_scsi_task(iTask.task);
435 iTask.task = NULL;
436 }
437 iTask.complete = 0;
438 goto retry;
439 }
440
441 if (iTask.status != SCSI_STATUS_GOOD) {
442 /* in case the get_lba_status_callout fails (i.e.
443 * because the device is busy or the cmd is not
444 * supported) we pretend all blocks are allocated
445 * for backwards compatibility */
446 goto out;
447 }
448
449 lbas = scsi_datain_unmarshall(iTask.task);
450 if (lbas == NULL) {
451 ret = -EIO;
452 goto out;
453 }
454
455 lbasd = &lbas->descriptors[0];
456
457 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
458 ret = -EIO;
459 goto out;
460 }
461
462 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
463
464 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
465 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
466 ret &= ~BDRV_BLOCK_DATA;
467 if (iscsilun->lbprz) {
468 ret |= BDRV_BLOCK_ZERO;
469 }
470 }
471
472 if (ret & BDRV_BLOCK_ZERO) {
473 iscsi_allocationmap_clear(iscsilun, sector_num, *pnum);
474 } else {
475 iscsi_allocationmap_set(iscsilun, sector_num, *pnum);
476 }
477
478 if (*pnum > nb_sectors) {
479 *pnum = nb_sectors;
480 }
481out:
482 if (iTask.task != NULL) {
483 scsi_free_scsi_task(iTask.task);
484 }
485 return ret;
486}
487
488#endif /* LIBISCSI_FEATURE_IOVECTOR */
489
490
063c3378
PL
491static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
492 int64_t sector_num, int nb_sectors,
493 QEMUIOVector *iov)
c589b249 494{
063c3378
PL
495 IscsiLun *iscsilun = bs->opaque;
496 struct IscsiTask iTask;
1dde716e
PL
497 uint64_t lba;
498 uint32_t num_sectors;
7371d56f 499#if !defined(LIBISCSI_FEATURE_IOVECTOR)
c589b249 500 int i;
7371d56f 501#endif
c589b249 502
063c3378
PL
503 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
504 return -EINVAL;
f4dfa67f 505 }
f4dfa67f 506
b03c3805
PL
507#if defined(LIBISCSI_FEATURE_IOVECTOR)
508 if (iscsilun->lbprz && nb_sectors > ISCSI_CHECKALLOC_THRES &&
509 !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
510 int64_t ret;
511 int pnum;
512 ret = iscsi_co_get_block_status(bs, sector_num, INT_MAX, &pnum);
513 if (ret < 0) {
514 return ret;
515 }
516 if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) {
517 qemu_iovec_memset(iov, 0, 0x00, iov->size);
518 return 0;
519 }
520 }
521#endif
522
063c3378
PL
523 lba = sector_qemu2lun(sector_num, iscsilun);
524 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
f4dfa67f 525
063c3378
PL
526 iscsi_co_init_iscsitask(iscsilun, &iTask);
527retry:
528 switch (iscsilun->type) {
f4dfa67f 529 case TYPE_DISK:
063c3378
PL
530 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
531 num_sectors * iscsilun->block_size,
532 iscsilun->block_size, 0, 0, 0, 0, 0,
533 iscsi_co_generic_cb, &iTask);
f4dfa67f
RS
534 break;
535 default:
063c3378
PL
536 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
537 num_sectors * iscsilun->block_size,
219c2521
SW
538 iscsilun->block_size,
539#if !defined(CONFIG_LIBISCSI_1_4) /* API change from 1.4.0 to 1.5.0 */
540 0, 0, 0, 0, 0,
541#endif
063c3378 542 iscsi_co_generic_cb, &iTask);
f4dfa67f
RS
543 break;
544 }
063c3378 545 if (iTask.task == NULL) {
92397116 546 return -ENOMEM;
c589b249 547 }
7371d56f 548#if defined(LIBISCSI_FEATURE_IOVECTOR)
063c3378 549 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
7371d56f 550#else
063c3378
PL
551 for (i = 0; i < iov->niov; i++) {
552 scsi_task_add_data_in_buffer(iTask.task,
553 iov->iov[i].iov_len,
554 iov->iov[i].iov_base);
c589b249 555 }
7371d56f 556#endif
91bea4e2 557
063c3378
PL
558 while (!iTask.complete) {
559 iscsi_set_events(iscsilun);
560 qemu_coroutine_yield();
1dde716e 561 }
c589b249 562
063c3378
PL
563 if (iTask.task != NULL) {
564 scsi_free_scsi_task(iTask.task);
565 iTask.task = NULL;
c589b249
RS
566 }
567
063c3378 568 if (iTask.do_retry) {
837c3901 569 iTask.complete = 0;
063c3378 570 goto retry;
c589b249
RS
571 }
572
063c3378
PL
573 if (iTask.status != SCSI_STATUS_GOOD) {
574 return -EIO;
1dde716e
PL
575 }
576
577 return 0;
578}
579
063c3378 580static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
1dde716e
PL
581{
582 IscsiLun *iscsilun = bs->opaque;
063c3378 583 struct IscsiTask iTask;
1dde716e 584
b2f9c08a
PB
585 if (bs->sg) {
586 return 0;
587 }
588
063c3378 589 iscsi_co_init_iscsitask(iscsilun, &iTask);
1dde716e 590
063c3378
PL
591retry:
592 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
593 0, iscsi_co_generic_cb, &iTask) == NULL) {
92397116 594 return -ENOMEM;
063c3378 595 }
1dde716e 596
063c3378
PL
597 while (!iTask.complete) {
598 iscsi_set_events(iscsilun);
599 qemu_coroutine_yield();
600 }
1dde716e 601
063c3378
PL
602 if (iTask.task != NULL) {
603 scsi_free_scsi_task(iTask.task);
604 iTask.task = NULL;
c589b249
RS
605 }
606
063c3378 607 if (iTask.do_retry) {
837c3901 608 iTask.complete = 0;
063c3378
PL
609 goto retry;
610 }
c589b249 611
063c3378
PL
612 if (iTask.status != SCSI_STATUS_GOOD) {
613 return -EIO;
614 }
615
616 return 0;
c589b249
RS
617}
618
98392453
RS
619#ifdef __linux__
620static void
621iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
622 void *command_data, void *opaque)
623{
624 IscsiAIOCB *acb = opaque;
625
0a53f010
RS
626 g_free(acb->buf);
627 acb->buf = NULL;
628
b2090919 629 if (acb->canceled != 0) {
98392453
RS
630 return;
631 }
632
633 acb->status = 0;
634 if (status < 0) {
635 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
636 iscsi_get_error(iscsi));
637 acb->status = -EIO;
638 }
639
640 acb->ioh->driver_status = 0;
641 acb->ioh->host_status = 0;
642 acb->ioh->resid = 0;
643
644#define SG_ERR_DRIVER_SENSE 0x08
645
646 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
647 int ss;
648
649 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
650
651 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
652 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
653 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
654 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
655 }
656
cfb3f506 657 iscsi_schedule_bh(acb);
98392453
RS
658}
659
660static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
661 unsigned long int req, void *buf,
662 BlockDriverCompletionFunc *cb, void *opaque)
663{
664 IscsiLun *iscsilun = bs->opaque;
665 struct iscsi_context *iscsi = iscsilun->iscsi;
666 struct iscsi_data data;
667 IscsiAIOCB *acb;
668
669 assert(req == SG_IO);
670
d7331bed 671 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
98392453
RS
672
673 acb->iscsilun = iscsilun;
674 acb->canceled = 0;
1bd075f2
PB
675 acb->bh = NULL;
676 acb->status = -EINPROGRESS;
98392453
RS
677 acb->buf = NULL;
678 acb->ioh = buf;
679
680 acb->task = malloc(sizeof(struct scsi_task));
681 if (acb->task == NULL) {
682 error_report("iSCSI: Failed to allocate task for scsi command. %s",
683 iscsi_get_error(iscsi));
684 qemu_aio_release(acb);
685 return NULL;
686 }
687 memset(acb->task, 0, sizeof(struct scsi_task));
688
689 switch (acb->ioh->dxfer_direction) {
690 case SG_DXFER_TO_DEV:
691 acb->task->xfer_dir = SCSI_XFER_WRITE;
692 break;
693 case SG_DXFER_FROM_DEV:
694 acb->task->xfer_dir = SCSI_XFER_READ;
695 break;
696 default:
697 acb->task->xfer_dir = SCSI_XFER_NONE;
698 break;
699 }
700
701 acb->task->cdb_size = acb->ioh->cmd_len;
702 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
703 acb->task->expxferlen = acb->ioh->dxfer_len;
704
0a53f010 705 data.size = 0;
98392453 706 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
0a53f010
RS
707 if (acb->ioh->iovec_count == 0) {
708 data.data = acb->ioh->dxferp;
709 data.size = acb->ioh->dxfer_len;
710 } else {
711#if defined(LIBISCSI_FEATURE_IOVECTOR)
712 scsi_task_set_iov_out(acb->task,
713 (struct scsi_iovec *) acb->ioh->dxferp,
714 acb->ioh->iovec_count);
715#else
716 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
717
718 acb->buf = g_malloc(acb->ioh->dxfer_len);
719 data.data = acb->buf;
720 data.size = iov_to_buf(iov, acb->ioh->iovec_count, 0,
721 acb->buf, acb->ioh->dxfer_len);
722#endif
723 }
98392453 724 }
0a53f010 725
98392453
RS
726 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
727 iscsi_aio_ioctl_cb,
0a53f010 728 (data.size > 0) ? &data : NULL,
98392453
RS
729 acb) != 0) {
730 scsi_free_scsi_task(acb->task);
731 qemu_aio_release(acb);
732 return NULL;
733 }
734
735 /* tell libiscsi to read straight into the buffer we got from ioctl */
736 if (acb->task->xfer_dir == SCSI_XFER_READ) {
0a53f010
RS
737 if (acb->ioh->iovec_count == 0) {
738 scsi_task_add_data_in_buffer(acb->task,
739 acb->ioh->dxfer_len,
740 acb->ioh->dxferp);
741 } else {
742#if defined(LIBISCSI_FEATURE_IOVECTOR)
743 scsi_task_set_iov_in(acb->task,
744 (struct scsi_iovec *) acb->ioh->dxferp,
745 acb->ioh->iovec_count);
746#else
747 int i;
748 for (i = 0; i < acb->ioh->iovec_count; i++) {
749 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
750
751 scsi_task_add_data_in_buffer(acb->task,
752 iov[i].iov_len,
753 iov[i].iov_base);
754 }
755#endif
756 }
98392453
RS
757 }
758
759 iscsi_set_events(iscsilun);
760
761 return &acb->common;
762}
763
f1a12821
RS
764
765static void ioctl_cb(void *opaque, int status)
766{
767 int *p_status = opaque;
768 *p_status = status;
769}
770
98392453
RS
771static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
772{
773 IscsiLun *iscsilun = bs->opaque;
f1a12821 774 int status;
98392453
RS
775
776 switch (req) {
777 case SG_GET_VERSION_NUM:
778 *(int *)buf = 30000;
779 break;
780 case SG_GET_SCSI_ID:
781 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
782 break;
f1a12821
RS
783 case SG_IO:
784 status = -EINPROGRESS;
785 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
786
787 while (status == -EINPROGRESS) {
788 qemu_aio_wait();
789 }
790
791 return 0;
98392453
RS
792 default:
793 return -1;
794 }
795 return 0;
796}
797#endif
798
c589b249
RS
799static int64_t
800iscsi_getlength(BlockDriverState *bs)
801{
802 IscsiLun *iscsilun = bs->opaque;
803 int64_t len;
804
805 len = iscsilun->num_blocks;
806 len *= iscsilun->block_size;
807
808 return len;
809}
810
65f3e339
PL
811static int
812coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
813 int nb_sectors)
814{
815 IscsiLun *iscsilun = bs->opaque;
816 struct IscsiTask iTask;
817 struct unmap_list list;
65f3e339
PL
818
819 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
820 return -EINVAL;
821 }
822
823 if (!iscsilun->lbp.lbpu) {
824 /* UNMAP is not supported by the target */
825 return 0;
826 }
827
828 list.lba = sector_qemu2lun(sector_num, iscsilun);
01a6a238 829 list.num = sector_qemu2lun(nb_sectors, iscsilun);
65f3e339 830
01a6a238 831 iscsi_co_init_iscsitask(iscsilun, &iTask);
65f3e339 832retry:
01a6a238
PL
833 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
834 iscsi_co_generic_cb, &iTask) == NULL) {
92397116 835 return -ENOMEM;
01a6a238 836 }
65f3e339 837
01a6a238
PL
838 while (!iTask.complete) {
839 iscsi_set_events(iscsilun);
840 qemu_coroutine_yield();
841 }
65f3e339 842
01a6a238
PL
843 if (iTask.task != NULL) {
844 scsi_free_scsi_task(iTask.task);
845 iTask.task = NULL;
846 }
65f3e339 847
01a6a238 848 if (iTask.do_retry) {
837c3901 849 iTask.complete = 0;
01a6a238
PL
850 goto retry;
851 }
65f3e339 852
01a6a238
PL
853 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
854 /* the target might fail with a check condition if it
855 is not happy with the alignment of the UNMAP request
856 we silently fail in this case */
857 return 0;
858 }
65f3e339 859
01a6a238
PL
860 if (iTask.status != SCSI_STATUS_GOOD) {
861 return -EIO;
65f3e339
PL
862 }
863
b03c3805
PL
864 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
865
65f3e339
PL
866 return 0;
867}
868
d4cd9615
PL
869#if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
870
871static int
872coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
873 int nb_sectors, BdrvRequestFlags flags)
874{
875 IscsiLun *iscsilun = bs->opaque;
876 struct IscsiTask iTask;
877 uint64_t lba;
878 uint32_t nb_blocks;
879
880 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
881 return -EINVAL;
882 }
883
dbe5c58f
PL
884 if ((flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->lbp.lbpws) {
885 /* WRITE SAME with UNMAP is not supported by the target,
886 * fall back and try WRITE SAME without UNMAP */
887 flags &= ~BDRV_REQ_MAY_UNMAP;
fa6252b0
PB
888 }
889
dbe5c58f
PL
890 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
891 /* WRITE SAME without UNMAP is not supported by the target */
d4cd9615
PL
892 return -ENOTSUP;
893 }
894
895 lba = sector_qemu2lun(sector_num, iscsilun);
896 nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
897
898 if (iscsilun->zeroblock == NULL) {
899 iscsilun->zeroblock = g_malloc0(iscsilun->block_size);
900 }
901
902 iscsi_co_init_iscsitask(iscsilun, &iTask);
903retry:
904 if (iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
905 iscsilun->zeroblock, iscsilun->block_size,
906 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
907 0, 0, iscsi_co_generic_cb, &iTask) == NULL) {
92397116 908 return -ENOMEM;
d4cd9615
PL
909 }
910
911 while (!iTask.complete) {
912 iscsi_set_events(iscsilun);
913 qemu_coroutine_yield();
914 }
915
d9738fd2
PL
916 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
917 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
27898a5d
PB
918 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
919 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
d9738fd2
PL
920 /* WRITE SAME is not supported by the target */
921 iscsilun->has_write_same = false;
922 scsi_free_scsi_task(iTask.task);
923 return -ENOTSUP;
924 }
925
d4cd9615
PL
926 if (iTask.task != NULL) {
927 scsi_free_scsi_task(iTask.task);
928 iTask.task = NULL;
929 }
930
931 if (iTask.do_retry) {
837c3901 932 iTask.complete = 0;
d4cd9615
PL
933 goto retry;
934 }
935
936 if (iTask.status != SCSI_STATUS_GOOD) {
937 return -EIO;
938 }
939
b03c3805
PL
940 if (flags & BDRV_REQ_MAY_UNMAP) {
941 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
942 } else {
943 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
944 }
945
d4cd9615
PL
946 return 0;
947}
948
949#endif /* SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED */
950
f2917853
PB
951static void parse_chap(struct iscsi_context *iscsi, const char *target,
952 Error **errp)
f9dadc98
RS
953{
954 QemuOptsList *list;
955 QemuOpts *opts;
956 const char *user = NULL;
957 const char *password = NULL;
958
959 list = qemu_find_opts("iscsi");
960 if (!list) {
f2917853 961 return;
f9dadc98
RS
962 }
963
964 opts = qemu_opts_find(list, target);
965 if (opts == NULL) {
966 opts = QTAILQ_FIRST(&list->head);
967 if (!opts) {
f2917853 968 return;
f9dadc98
RS
969 }
970 }
971
972 user = qemu_opt_get(opts, "user");
973 if (!user) {
f2917853 974 return;
f9dadc98
RS
975 }
976
977 password = qemu_opt_get(opts, "password");
978 if (!password) {
f2917853
PB
979 error_setg(errp, "CHAP username specified but no password was given");
980 return;
f9dadc98
RS
981 }
982
983 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
f2917853 984 error_setg(errp, "Failed to set initiator username and password");
f9dadc98 985 }
f9dadc98
RS
986}
987
f2917853
PB
988static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
989 Error **errp)
f9dadc98
RS
990{
991 QemuOptsList *list;
992 QemuOpts *opts;
993 const char *digest = NULL;
994
995 list = qemu_find_opts("iscsi");
996 if (!list) {
997 return;
998 }
999
1000 opts = qemu_opts_find(list, target);
1001 if (opts == NULL) {
1002 opts = QTAILQ_FIRST(&list->head);
1003 if (!opts) {
1004 return;
1005 }
1006 }
1007
1008 digest = qemu_opt_get(opts, "header-digest");
1009 if (!digest) {
1010 return;
1011 }
1012
1013 if (!strcmp(digest, "CRC32C")) {
1014 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1015 } else if (!strcmp(digest, "NONE")) {
1016 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1017 } else if (!strcmp(digest, "CRC32C-NONE")) {
1018 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1019 } else if (!strcmp(digest, "NONE-CRC32C")) {
1020 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1021 } else {
f2917853 1022 error_setg(errp, "Invalid header-digest setting : %s", digest);
f9dadc98
RS
1023 }
1024}
1025
1026static char *parse_initiator_name(const char *target)
1027{
1028 QemuOptsList *list;
1029 QemuOpts *opts;
5accc840
PB
1030 const char *name;
1031 char *iscsi_name;
1032 UuidInfo *uuid_info;
f9dadc98
RS
1033
1034 list = qemu_find_opts("iscsi");
f2ef4a6d
PB
1035 if (list) {
1036 opts = qemu_opts_find(list, target);
f9dadc98 1037 if (!opts) {
f2ef4a6d
PB
1038 opts = QTAILQ_FIRST(&list->head);
1039 }
1040 if (opts) {
1041 name = qemu_opt_get(opts, "initiator-name");
5accc840
PB
1042 if (name) {
1043 return g_strdup(name);
1044 }
f9dadc98
RS
1045 }
1046 }
1047
5accc840
PB
1048 uuid_info = qmp_query_uuid(NULL);
1049 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1050 name = qemu_get_vm_name();
f2ef4a6d 1051 } else {
5accc840 1052 name = uuid_info->UUID;
f9dadc98 1053 }
5accc840
PB
1054 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1055 name ? ":" : "", name ? name : "");
1056 qapi_free_UuidInfo(uuid_info);
1057 return iscsi_name;
f9dadc98
RS
1058}
1059
5b5d34ec
PL
1060#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1061static void iscsi_nop_timed_event(void *opaque)
1062{
1063 IscsiLun *iscsilun = opaque;
1064
1065 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
1066 error_report("iSCSI: NOP timeout. Reconnecting...");
1067 iscsi_reconnect(iscsilun->iscsi);
1068 }
1069
1070 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1071 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1072 return;
1073 }
1074
bc72ad67 1075 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
5b5d34ec
PL
1076 iscsi_set_events(iscsilun);
1077}
1078#endif
1079
f2917853 1080static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
cb1b83e7
PL
1081{
1082 struct scsi_task *task = NULL;
1083 struct scsi_readcapacity10 *rc10 = NULL;
1084 struct scsi_readcapacity16 *rc16 = NULL;
cb1b83e7
PL
1085 int retries = ISCSI_CMD_RETRIES;
1086
1288844e
PB
1087 do {
1088 if (task != NULL) {
1089 scsi_free_scsi_task(task);
1090 task = NULL;
cb1b83e7 1091 }
1288844e
PB
1092
1093 switch (iscsilun->type) {
1094 case TYPE_DISK:
1095 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1096 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1097 rc16 = scsi_datain_unmarshall(task);
1098 if (rc16 == NULL) {
f2917853 1099 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1288844e
PB
1100 } else {
1101 iscsilun->block_size = rc16->block_length;
1102 iscsilun->num_blocks = rc16->returned_lba + 1;
f18a7cbb
PL
1103 iscsilun->lbpme = rc16->lbpme;
1104 iscsilun->lbprz = rc16->lbprz;
1288844e
PB
1105 }
1106 }
1107 break;
1108 case TYPE_ROM:
1109 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1110 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1111 rc10 = scsi_datain_unmarshall(task);
1112 if (rc10 == NULL) {
f2917853 1113 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1288844e
PB
1114 } else {
1115 iscsilun->block_size = rc10->block_size;
1116 if (rc10->lba == 0) {
1117 /* blank disk loaded */
1118 iscsilun->num_blocks = 0;
1119 } else {
1120 iscsilun->num_blocks = rc10->lba + 1;
1121 }
1122 }
1123 }
1124 break;
1125 default:
f2917853 1126 return;
cb1b83e7 1127 }
1288844e
PB
1128 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1129 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1130 && retries-- > 0);
cb1b83e7 1131
1288844e 1132 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
f2917853 1133 error_setg(errp, "iSCSI: failed to send readcapacity10 command.");
1288844e 1134 }
cb1b83e7
PL
1135 if (task) {
1136 scsi_free_scsi_task(task);
1137 }
cb1b83e7
PL
1138}
1139
60beb341
KW
1140/* TODO Convert to fine grained options */
1141static QemuOptsList runtime_opts = {
1142 .name = "iscsi",
1143 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1144 .desc = {
1145 {
1146 .name = "filename",
1147 .type = QEMU_OPT_STRING,
1148 .help = "URL to the iscsi image",
1149 },
1150 { /* end of list */ }
1151 },
1152};
1153
35cb1748 1154static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
24d3bd67 1155 int evpd, int pc, void **inq, Error **errp)
35cb1748
PB
1156{
1157 int full_size;
1158 struct scsi_task *task = NULL;
1159 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1160 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1161 goto fail;
1162 }
1163 full_size = scsi_datain_getfullsize(task);
1164 if (full_size > task->datain.size) {
1165 scsi_free_scsi_task(task);
1166
1167 /* we need more data for the full list */
1168 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
f18a7cbb
PL
1169 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1170 goto fail;
1171 }
35cb1748 1172 }
f18a7cbb 1173
24d3bd67
PL
1174 *inq = scsi_datain_unmarshall(task);
1175 if (*inq == NULL) {
1176 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
1177 goto fail;
1178 }
1179
35cb1748 1180 return task;
f18a7cbb
PL
1181
1182fail:
cbee81f6
FZ
1183 if (!error_is_set(errp)) {
1184 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1185 iscsi_get_error(iscsi));
1186 }
24d3bd67 1187 if (task != NULL) {
35cb1748 1188 scsi_free_scsi_task(task);
35cb1748
PB
1189 }
1190 return NULL;
f18a7cbb
PL
1191}
1192
c589b249
RS
1193/*
1194 * We support iscsi url's on the form
1195 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
28f106af
JC
1196 *
1197 * Note: flags are currently not used by iscsi_open. If this function
1198 * is changed such that flags are used, please examine iscsi_reopen_prepare()
1199 * to see if needs to be changed as well.
c589b249 1200 */
015a1036
HR
1201static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1202 Error **errp)
c589b249
RS
1203{
1204 IscsiLun *iscsilun = bs->opaque;
1205 struct iscsi_context *iscsi = NULL;
1206 struct iscsi_url *iscsi_url = NULL;
e829b0bb
PL
1207 struct scsi_task *task = NULL;
1208 struct scsi_inquiry_standard *inq = NULL;
24d3bd67 1209 struct scsi_inquiry_supported_pages *inq_vpd;
f9dadc98 1210 char *initiator_name = NULL;
60beb341
KW
1211 QemuOpts *opts;
1212 Error *local_err = NULL;
1213 const char *filename;
24d3bd67 1214 int i, ret;
c589b249
RS
1215
1216 if ((BDRV_SECTOR_SIZE % 512) != 0) {
f2917853
PB
1217 error_setg(errp, "iSCSI: Invalid BDRV_SECTOR_SIZE. "
1218 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1219 "of 512", BDRV_SECTOR_SIZE);
c589b249
RS
1220 return -EINVAL;
1221 }
1222
87ea75d5 1223 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
60beb341 1224 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 1225 if (local_err) {
f2917853 1226 error_propagate(errp, local_err);
60beb341
KW
1227 ret = -EINVAL;
1228 goto out;
1229 }
1230
1231 filename = qemu_opt_get(opts, "filename");
1232
c589b249
RS
1233 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1234 if (iscsi_url == NULL) {
f2917853 1235 error_setg(errp, "Failed to parse URL : %s", filename);
c589b249 1236 ret = -EINVAL;
b93c94f7 1237 goto out;
c589b249
RS
1238 }
1239
f9dadc98
RS
1240 memset(iscsilun, 0, sizeof(IscsiLun));
1241
1242 initiator_name = parse_initiator_name(iscsi_url->target);
1243
1244 iscsi = iscsi_create_context(initiator_name);
1245 if (iscsi == NULL) {
f2917853 1246 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
f9dadc98 1247 ret = -ENOMEM;
b93c94f7 1248 goto out;
f9dadc98
RS
1249 }
1250
c589b249 1251 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
f2917853 1252 error_setg(errp, "iSCSI: Failed to set target name.");
c589b249 1253 ret = -EINVAL;
b93c94f7 1254 goto out;
c589b249
RS
1255 }
1256
1257 if (iscsi_url->user != NULL) {
1258 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1259 iscsi_url->passwd);
1260 if (ret != 0) {
f2917853 1261 error_setg(errp, "Failed to set initiator username and password");
c589b249 1262 ret = -EINVAL;
b93c94f7 1263 goto out;
c589b249
RS
1264 }
1265 }
f9dadc98
RS
1266
1267 /* check if we got CHAP username/password via the options */
f2917853
PB
1268 parse_chap(iscsi, iscsi_url->target, &local_err);
1269 if (local_err != NULL) {
1270 error_propagate(errp, local_err);
f9dadc98 1271 ret = -EINVAL;
b93c94f7 1272 goto out;
f9dadc98
RS
1273 }
1274
c589b249 1275 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
f2917853 1276 error_setg(errp, "iSCSI: Failed to set session type to normal.");
c589b249 1277 ret = -EINVAL;
b93c94f7 1278 goto out;
c589b249
RS
1279 }
1280
1281 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1282
f9dadc98 1283 /* check if we got HEADER_DIGEST via the options */
f2917853
PB
1284 parse_header_digest(iscsi, iscsi_url->target, &local_err);
1285 if (local_err != NULL) {
1286 error_propagate(errp, local_err);
1287 ret = -EINVAL;
1288 goto out;
1289 }
f9dadc98 1290
e829b0bb 1291 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
f2917853 1292 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
e829b0bb
PL
1293 iscsi_get_error(iscsi));
1294 ret = -EINVAL;
1295 goto out;
1296 }
c589b249
RS
1297
1298 iscsilun->iscsi = iscsi;
1299 iscsilun->lun = iscsi_url->lun;
24d3bd67 1300 iscsilun->has_write_same = true;
c589b249 1301
24d3bd67
PL
1302 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1303 (void **) &inq, errp);
1304 if (task == NULL) {
c589b249 1305 ret = -EINVAL;
b93c94f7 1306 goto out;
c589b249 1307 }
e829b0bb 1308 iscsilun->type = inq->periperal_device_type;
24d3bd67
PL
1309 scsi_free_scsi_task(task);
1310 task = NULL;
e829b0bb 1311
f2917853
PB
1312 iscsi_readcapacity_sync(iscsilun, &local_err);
1313 if (local_err != NULL) {
1314 error_propagate(errp, local_err);
cd82b6fb 1315 ret = -EINVAL;
cb1b83e7 1316 goto out;
e829b0bb 1317 }
0777b5dd 1318 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
2c9880c4 1319 bs->request_alignment = iscsilun->block_size;
e829b0bb 1320
f47c3f5a
KW
1321 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1322 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1323 * will try to read from the device to guess the image format.
622695a4 1324 */
f47c3f5a 1325 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
622695a4
RS
1326 bs->sg = 1;
1327 }
1328
24d3bd67
PL
1329 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1330 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1331 (void **) &inq_vpd, errp);
1332 if (task == NULL) {
1333 ret = -EINVAL;
1334 goto out;
f18a7cbb 1335 }
24d3bd67
PL
1336 for (i = 0; i < inq_vpd->num_pages; i++) {
1337 struct scsi_task *inq_task;
1338 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
f18a7cbb 1339 struct scsi_inquiry_block_limits *inq_bl;
24d3bd67
PL
1340 switch (inq_vpd->pages[i]) {
1341 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1342 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1343 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1344 (void **) &inq_lbp, errp);
1345 if (inq_task == NULL) {
1346 ret = -EINVAL;
1347 goto out;
1348 }
1349 memcpy(&iscsilun->lbp, inq_lbp,
1350 sizeof(struct scsi_inquiry_logical_block_provisioning));
1351 scsi_free_scsi_task(inq_task);
1352 break;
1353 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1354 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1355 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1356 (void **) &inq_bl, errp);
1357 if (inq_task == NULL) {
1358 ret = -EINVAL;
1359 goto out;
1360 }
1361 memcpy(&iscsilun->bl, inq_bl,
1362 sizeof(struct scsi_inquiry_block_limits));
1363 scsi_free_scsi_task(inq_task);
1364 break;
1365 default:
1366 break;
f18a7cbb 1367 }
f18a7cbb 1368 }
24d3bd67
PL
1369 scsi_free_scsi_task(task);
1370 task = NULL;
f18a7cbb 1371
5b5d34ec
PL
1372#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1373 /* Set up a timer for sending out iSCSI NOPs */
bc72ad67
AB
1374 iscsilun->nop_timer = timer_new_ms(QEMU_CLOCK_REALTIME, iscsi_nop_timed_event, iscsilun);
1375 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
5b5d34ec
PL
1376#endif
1377
b03c3805
PL
1378 /* Guess the internal cluster (page) size of the iscsi target by the means
1379 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1380 * reasonable size */
1381 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 64 * 1024 &&
1382 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1383 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
1384 iscsilun->block_size) >> BDRV_SECTOR_BITS;
1385#if defined(LIBISCSI_FEATURE_IOVECTOR)
1386 if (iscsilun->lbprz && !(bs->open_flags & BDRV_O_NOCACHE)) {
1387 iscsilun->allocationmap =
1388 bitmap_new(DIV_ROUND_UP(bs->total_sectors,
1389 iscsilun->cluster_sectors));
1390 }
1391#endif
1392 }
1393
b93c94f7 1394out:
60beb341 1395 qemu_opts_del(opts);
f9dadc98
RS
1396 if (initiator_name != NULL) {
1397 g_free(initiator_name);
1398 }
c589b249
RS
1399 if (iscsi_url != NULL) {
1400 iscsi_destroy_url(iscsi_url);
1401 }
e829b0bb
PL
1402 if (task != NULL) {
1403 scsi_free_scsi_task(task);
1404 }
b93c94f7
PB
1405
1406 if (ret) {
1407 if (iscsi != NULL) {
1408 iscsi_destroy_context(iscsi);
1409 }
1410 memset(iscsilun, 0, sizeof(IscsiLun));
c589b249 1411 }
c589b249
RS
1412 return ret;
1413}
1414
1415static void iscsi_close(BlockDriverState *bs)
1416{
1417 IscsiLun *iscsilun = bs->opaque;
1418 struct iscsi_context *iscsi = iscsilun->iscsi;
1419
5b5d34ec 1420 if (iscsilun->nop_timer) {
bc72ad67
AB
1421 timer_del(iscsilun->nop_timer);
1422 timer_free(iscsilun->nop_timer);
5b5d34ec 1423 }
f2e5dca4 1424 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL);
c589b249 1425 iscsi_destroy_context(iscsi);
d4cd9615 1426 g_free(iscsilun->zeroblock);
b03c3805 1427 g_free(iscsilun->allocationmap);
c589b249
RS
1428 memset(iscsilun, 0, sizeof(IscsiLun));
1429}
1430
d34682cd
KW
1431static int iscsi_refresh_limits(BlockDriverState *bs)
1432{
1433 IscsiLun *iscsilun = bs->opaque;
1434
1435 /* We don't actually refresh here, but just return data queried in
1436 * iscsi_open(): iscsi targets don't change their limits. */
c97ca29d 1437 if (iscsilun->lbp.lbpu) {
d34682cd
KW
1438 if (iscsilun->bl.max_unmap < 0xffffffff) {
1439 bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap,
1440 iscsilun);
1441 }
1442 bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1443 iscsilun);
c97ca29d 1444 }
d34682cd 1445
c97ca29d
PB
1446 if (iscsilun->bl.max_ws_len < 0xffffffff) {
1447 bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len,
1448 iscsilun);
1449 }
1450 if (iscsilun->lbp.lbpws) {
d34682cd
KW
1451 bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1452 iscsilun);
d34682cd 1453 }
5d259fc7
PL
1454 bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len,
1455 iscsilun);
e9f526ab
AL
1456 return 0;
1457}
d34682cd 1458
28f106af
JC
1459/* Since iscsi_open() ignores bdrv_flags, there is nothing to do here in
1460 * prepare. Note that this will not re-establish a connection with an iSCSI
1461 * target - it is effectively a NOP. */
dc6afb99
JC
1462static int iscsi_reopen_prepare(BDRVReopenState *state,
1463 BlockReopenQueue *queue, Error **errp)
1464{
28f106af 1465 /* NOP */
d34682cd
KW
1466 return 0;
1467}
1468
cb1b83e7
PL
1469static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1470{
1471 IscsiLun *iscsilun = bs->opaque;
f2917853 1472 Error *local_err = NULL;
cb1b83e7
PL
1473
1474 if (iscsilun->type != TYPE_DISK) {
1475 return -ENOTSUP;
1476 }
1477
f2917853
PB
1478 iscsi_readcapacity_sync(iscsilun, &local_err);
1479 if (local_err != NULL) {
1480 error_free(local_err);
1481 return -EIO;
cb1b83e7
PL
1482 }
1483
1484 if (offset > iscsi_getlength(bs)) {
1485 return -EINVAL;
1486 }
1487
b03c3805
PL
1488 if (iscsilun->allocationmap != NULL) {
1489 g_free(iscsilun->allocationmap);
1490 iscsilun->allocationmap =
1491 bitmap_new(DIV_ROUND_UP(bs->total_sectors,
1492 iscsilun->cluster_sectors));
1493 }
1494
cb1b83e7
PL
1495 return 0;
1496}
1497
d5124c00
HR
1498static int iscsi_create(const char *filename, QEMUOptionParameter *options,
1499 Error **errp)
de8864e5
PL
1500{
1501 int ret = 0;
1502 int64_t total_size = 0;
13c91cb7 1503 BlockDriverState *bs;
de8864e5 1504 IscsiLun *iscsilun = NULL;
60beb341 1505 QDict *bs_options;
de8864e5 1506
98522f63 1507 bs = bdrv_new("", &error_abort);
de8864e5
PL
1508
1509 /* Read out options */
1510 while (options && options->name) {
1511 if (!strcmp(options->name, "size")) {
1512 total_size = options->value.n / BDRV_SECTOR_SIZE;
1513 }
1514 options++;
1515 }
1516
13c91cb7
FZ
1517 bs->opaque = g_malloc0(sizeof(struct IscsiLun));
1518 iscsilun = bs->opaque;
de8864e5 1519
60beb341
KW
1520 bs_options = qdict_new();
1521 qdict_put(bs_options, "filename", qstring_from_str(filename));
015a1036 1522 ret = iscsi_open(bs, bs_options, 0, NULL);
60beb341
KW
1523 QDECREF(bs_options);
1524
de8864e5
PL
1525 if (ret != 0) {
1526 goto out;
1527 }
5b5d34ec 1528 if (iscsilun->nop_timer) {
bc72ad67
AB
1529 timer_del(iscsilun->nop_timer);
1530 timer_free(iscsilun->nop_timer);
5b5d34ec 1531 }
de8864e5
PL
1532 if (iscsilun->type != TYPE_DISK) {
1533 ret = -ENODEV;
1534 goto out;
1535 }
13c91cb7 1536 if (bs->total_sectors < total_size) {
de8864e5 1537 ret = -ENOSPC;
d3bda7bc 1538 goto out;
de8864e5
PL
1539 }
1540
1541 ret = 0;
1542out:
1543 if (iscsilun->iscsi != NULL) {
1544 iscsi_destroy_context(iscsilun->iscsi);
1545 }
13c91cb7
FZ
1546 g_free(bs->opaque);
1547 bs->opaque = NULL;
4f6fd349 1548 bdrv_unref(bs);
de8864e5
PL
1549 return ret;
1550}
1551
186d4f2b
PL
1552static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1553{
1554 IscsiLun *iscsilun = bs->opaque;
1555 bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz;
1556 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
b03c3805 1557 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
186d4f2b
PL
1558 return 0;
1559}
1560
de8864e5
PL
1561static QEMUOptionParameter iscsi_create_options[] = {
1562 {
1563 .name = BLOCK_OPT_SIZE,
1564 .type = OPT_SIZE,
1565 .help = "Virtual disk size"
1566 },
1567 { NULL }
1568};
1569
c589b249
RS
1570static BlockDriver bdrv_iscsi = {
1571 .format_name = "iscsi",
1572 .protocol_name = "iscsi",
1573
1574 .instance_size = sizeof(IscsiLun),
030be321 1575 .bdrv_needs_filename = true,
c589b249
RS
1576 .bdrv_file_open = iscsi_open,
1577 .bdrv_close = iscsi_close,
de8864e5
PL
1578 .bdrv_create = iscsi_create,
1579 .create_options = iscsi_create_options,
dc6afb99 1580 .bdrv_reopen_prepare = iscsi_reopen_prepare,
c589b249
RS
1581
1582 .bdrv_getlength = iscsi_getlength,
186d4f2b 1583 .bdrv_get_info = iscsi_get_info,
cb1b83e7 1584 .bdrv_truncate = iscsi_truncate,
d34682cd 1585 .bdrv_refresh_limits = iscsi_refresh_limits,
c589b249 1586
24c7608a 1587#if defined(LIBISCSI_FEATURE_IOVECTOR)
54a5c1d5 1588 .bdrv_co_get_block_status = iscsi_co_get_block_status,
f35c934a 1589#endif
65f3e339 1590 .bdrv_co_discard = iscsi_co_discard,
d4cd9615
PL
1591#if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
1592 .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
1593#endif
063c3378
PL
1594 .bdrv_co_readv = iscsi_co_readv,
1595 .bdrv_co_writev = iscsi_co_writev,
1596 .bdrv_co_flush_to_disk = iscsi_co_flush,
fa6acb0c 1597
98392453
RS
1598#ifdef __linux__
1599 .bdrv_ioctl = iscsi_ioctl,
1600 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1601#endif
c589b249
RS
1602};
1603
4d454574
PB
1604static QemuOptsList qemu_iscsi_opts = {
1605 .name = "iscsi",
1606 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1607 .desc = {
1608 {
1609 .name = "user",
1610 .type = QEMU_OPT_STRING,
1611 .help = "username for CHAP authentication to target",
1612 },{
1613 .name = "password",
1614 .type = QEMU_OPT_STRING,
1615 .help = "password for CHAP authentication to target",
1616 },{
1617 .name = "header-digest",
1618 .type = QEMU_OPT_STRING,
1619 .help = "HeaderDigest setting. "
1620 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1621 },{
1622 .name = "initiator-name",
1623 .type = QEMU_OPT_STRING,
1624 .help = "Initiator iqn name to use when connecting",
1625 },
1626 { /* end of list */ }
1627 },
1628};
1629
c589b249
RS
1630static void iscsi_block_init(void)
1631{
1632 bdrv_register(&bdrv_iscsi);
4d454574 1633 qemu_add_opts(&qemu_iscsi_opts);
c589b249
RS
1634}
1635
1636block_init(iscsi_block_init);