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