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