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