]> Git Repo - qemu.git/blame - block/iscsi.c
block: Honor BDRV_REQ_FUA during write_zeroes
[qemu.git] / block / iscsi.c
CommitLineData
c589b249
RS
1/*
2 * QEMU Block driver for iSCSI images
3 *
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <[email protected]>
03e40fef 5 * Copyright (c) 2012-2015 Peter Lieven <[email protected]>
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
80c71a24 26#include "qemu/osdep.h"
c589b249
RS
27
28#include <poll.h>
efc6de0d 29#include <math.h>
f4dfa67f 30#include <arpa/inet.h>
c589b249 31#include "qemu-common.h"
1de7afc9
PB
32#include "qemu/config-file.h"
33#include "qemu/error-report.h"
b03c3805
PL
34#include "qemu/bitops.h"
35#include "qemu/bitmap.h"
737e150e 36#include "block/block_int.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"
d49b6836 41#include "qapi/qmp/qstring.h"
b189346e 42#include "crypto/secret.h"
c589b249
RS
43
44#include <iscsi/iscsi.h>
45#include <iscsi/scsi-lowlevel.h>
46
98392453
RS
47#ifdef __linux__
48#include <scsi/sg.h>
0d09e41a 49#include <block/scsi.h>
98392453 50#endif
c589b249
RS
51
52typedef struct IscsiLun {
53 struct iscsi_context *iscsi;
80cf6257 54 AioContext *aio_context;
c589b249 55 int lun;
dbfff6d7 56 enum scsi_inquiry_peripheral_device_type type;
c589b249 57 int block_size;
c7b4a952 58 uint64_t num_blocks;
c9b9f682 59 int events;
5b5d34ec 60 QEMUTimer *nop_timer;
05b685fb 61 QEMUTimer *event_timer;
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;
9281fe9e 67 bool use_16_for_rw;
43ae8fb1 68 bool write_protected;
0a386e48
PL
69 bool lbpme;
70 bool lbprz;
752ce451 71 bool dpofua;
0a386e48 72 bool has_write_same;
5dd7a535 73 bool request_timed_out;
c589b249
RS
74} IscsiLun;
75
54a5c1d5
PL
76typedef struct IscsiTask {
77 int status;
78 int complete;
79 int retries;
80 int do_retry;
81 struct scsi_task *task;
82 Coroutine *co;
8b9dfe90 83 QEMUBH *bh;
80cf6257 84 IscsiLun *iscsilun;
efc6de0d 85 QEMUTimer retry_timer;
e01dd3da 86 int err_code;
54a5c1d5
PL
87} IscsiTask;
88
c589b249 89typedef struct IscsiAIOCB {
7c84b1b8 90 BlockAIOCB common;
c589b249
RS
91 QEMUIOVector *qiov;
92 QEMUBH *bh;
93 IscsiLun *iscsilun;
94 struct scsi_task *task;
95 uint8_t *buf;
96 int status;
1dde716e
PL
97 int64_t sector_num;
98 int nb_sectors;
4bb17ab5 99 int ret;
98392453
RS
100#ifdef __linux__
101 sg_io_hdr_t *ioh;
102#endif
c589b249
RS
103} IscsiAIOCB;
104
5dd7a535
PL
105/* libiscsi uses time_t so its enough to process events every second */
106#define EVENT_INTERVAL 1000
5b5d34ec
PL
107#define NOP_INTERVAL 5000
108#define MAX_NOP_FAILURES 3
efc6de0d 109#define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
59dd0a22 110static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768};
5b5d34ec 111
5d831be2 112/* this threshold is a trade-off knob to choose between
5917af81
PL
113 * the potential additional overhead of an extra GET_LBA_STATUS request
114 * vs. unnecessarily reading a lot of zero sectors over the wire.
115 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
116 * sectors we check the allocation status of the area covered by the
117 * request first if the allocationmap indicates that the area might be
118 * unallocated. */
119#define ISCSI_CHECKALLOC_THRES 64
5b5d34ec 120
27cbd828 121static void
cfb3f506 122iscsi_bh_cb(void *p)
27cbd828
PB
123{
124 IscsiAIOCB *acb = p;
125
126 qemu_bh_delete(acb->bh);
127
4790b03d
PB
128 g_free(acb->buf);
129 acb->buf = NULL;
130
722d9333 131 acb->common.cb(acb->common.opaque, acb->status);
27cbd828 132
1bd075f2
PB
133 if (acb->task != NULL) {
134 scsi_free_scsi_task(acb->task);
135 acb->task = NULL;
136 }
137
8007429a 138 qemu_aio_unref(acb);
27cbd828
PB
139}
140
cfb3f506
PB
141static void
142iscsi_schedule_bh(IscsiAIOCB *acb)
27cbd828 143{
1bd075f2
PB
144 if (acb->bh) {
145 return;
146 }
80cf6257 147 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
27cbd828 148 qemu_bh_schedule(acb->bh);
27cbd828
PB
149}
150
8b9dfe90
PL
151static void iscsi_co_generic_bh_cb(void *opaque)
152{
153 struct IscsiTask *iTask = opaque;
fcd470d8 154 iTask->complete = 1;
8b9dfe90
PL
155 qemu_bh_delete(iTask->bh);
156 qemu_coroutine_enter(iTask->co, NULL);
157}
158
efc6de0d
PL
159static void iscsi_retry_timer_expired(void *opaque)
160{
161 struct IscsiTask *iTask = opaque;
fcd470d8 162 iTask->complete = 1;
efc6de0d
PL
163 if (iTask->co) {
164 qemu_coroutine_enter(iTask->co, NULL);
165 }
166}
167
168static inline unsigned exp_random(double mean)
169{
170 return -mean * log((double)rand() / RAND_MAX);
171}
172
e01dd3da
FZ
173/* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
174 * libiscsi 1.10.0, together with other constants we need. Use it as
175 * a hint that we have to define them ourselves if needed, to keep the
176 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
177 * the test because SCSI_STATUS_* is an enum.
178 *
179 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
180 * an enum, check against the LIBISCSI_API_VERSION macro, which was
181 * introduced in 1.11.0. If it is present, there is no need to define
182 * anything.
183 */
184#if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
185 !defined(LIBISCSI_API_VERSION)
186#define SCSI_STATUS_TASK_SET_FULL 0x28
187#define SCSI_STATUS_TIMEOUT 0x0f000002
188#define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
189#define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
9049736e
PL
190#endif
191
e01dd3da
FZ
192static int iscsi_translate_sense(struct scsi_sense *sense)
193{
194 int ret;
195
196 switch (sense->key) {
197 case SCSI_SENSE_NOT_READY:
198 return -EBUSY;
199 case SCSI_SENSE_DATA_PROTECTION:
200 return -EACCES;
201 case SCSI_SENSE_COMMAND_ABORTED:
202 return -ECANCELED;
203 case SCSI_SENSE_ILLEGAL_REQUEST:
204 /* Parse ASCQ */
205 break;
206 default:
207 return -EIO;
208 }
209 switch (sense->ascq) {
210 case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR:
211 case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE:
212 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB:
213 case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST:
214 ret = -EINVAL;
215 break;
216 case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE:
217 ret = -ENOSPC;
218 break;
219 case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED:
220 ret = -ENOTSUP;
221 break;
222 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT:
223 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED:
224 case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN:
225 ret = -ENOMEDIUM;
226 break;
227 case SCSI_SENSE_ASCQ_WRITE_PROTECTED:
228 ret = -EACCES;
229 break;
230 default:
231 ret = -EIO;
232 break;
233 }
234 return ret;
235}
236
54a5c1d5
PL
237static void
238iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
239 void *command_data, void *opaque)
240{
241 struct IscsiTask *iTask = opaque;
242 struct scsi_task *task = command_data;
243
54a5c1d5
PL
244 iTask->status = status;
245 iTask->do_retry = 0;
246 iTask->task = task;
247
54a5c1d5 248 if (status != SCSI_STATUS_GOOD) {
efc6de0d
PL
249 if (iTask->retries++ < ISCSI_CMD_RETRIES) {
250 if (status == SCSI_STATUS_CHECK_CONDITION
251 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
252 error_report("iSCSI CheckCondition: %s",
253 iscsi_get_error(iscsi));
254 iTask->do_retry = 1;
255 goto out;
256 }
9049736e 257 if (status == SCSI_STATUS_BUSY ||
e01dd3da
FZ
258 status == SCSI_STATUS_TIMEOUT ||
259 status == SCSI_STATUS_TASK_SET_FULL) {
efc6de0d
PL
260 unsigned retry_time =
261 exp_random(iscsi_retry_times[iTask->retries - 1]);
e01dd3da 262 if (status == SCSI_STATUS_TIMEOUT) {
5dd7a535
PL
263 /* make sure the request is rescheduled AFTER the
264 * reconnect is initiated */
265 retry_time = EVENT_INTERVAL * 2;
266 iTask->iscsilun->request_timed_out = true;
267 }
268 error_report("iSCSI Busy/TaskSetFull/TimeOut"
269 " (retry #%u in %u ms): %s",
efc6de0d
PL
270 iTask->retries, retry_time,
271 iscsi_get_error(iscsi));
272 aio_timer_init(iTask->iscsilun->aio_context,
273 &iTask->retry_timer, QEMU_CLOCK_REALTIME,
274 SCALE_MS, iscsi_retry_timer_expired, iTask);
275 timer_mod(&iTask->retry_timer,
276 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time);
277 iTask->do_retry = 1;
278 return;
279 }
280 }
e01dd3da 281 iTask->err_code = iscsi_translate_sense(&task->sense);
837c3901 282 error_report("iSCSI Failure: %s", iscsi_get_error(iscsi));
54a5c1d5
PL
283 }
284
285out:
286 if (iTask->co) {
80cf6257
SH
287 iTask->bh = aio_bh_new(iTask->iscsilun->aio_context,
288 iscsi_co_generic_bh_cb, iTask);
8b9dfe90 289 qemu_bh_schedule(iTask->bh);
fcd470d8
PL
290 } else {
291 iTask->complete = 1;
54a5c1d5
PL
292 }
293}
294
295static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
296{
297 *iTask = (struct IscsiTask) {
efc6de0d 298 .co = qemu_coroutine_self(),
efc6de0d 299 .iscsilun = iscsilun,
54a5c1d5
PL
300 };
301}
27cbd828 302
c589b249
RS
303static void
304iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
305 void *private_data)
306{
1bd075f2
PB
307 IscsiAIOCB *acb = private_data;
308
309 acb->status = -ECANCELED;
310 iscsi_schedule_bh(acb);
c589b249
RS
311}
312
313static void
7c84b1b8 314iscsi_aio_cancel(BlockAIOCB *blockacb)
c589b249
RS
315{
316 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
317 IscsiLun *iscsilun = acb->iscsilun;
318
1bd075f2
PB
319 if (acb->status != -EINPROGRESS) {
320 return;
321 }
322
b2090919 323 /* send a task mgmt call to the target to cancel the task on the target */
64e69e80 324 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
1bd075f2 325 iscsi_abort_task_cb, acb);
b2090919 326
c589b249
RS
327}
328
d7331bed 329static const AIOCBInfo iscsi_aiocb_info = {
c589b249 330 .aiocb_size = sizeof(IscsiAIOCB),
722d9333 331 .cancel_async = iscsi_aio_cancel,
c589b249
RS
332};
333
334
335static void iscsi_process_read(void *arg);
336static void iscsi_process_write(void *arg);
337
c589b249
RS
338static void
339iscsi_set_events(IscsiLun *iscsilun)
340{
341 struct iscsi_context *iscsi = iscsilun->iscsi;
05b685fb 342 int ev = iscsi_which_events(iscsi);
c9b9f682 343
c9b9f682 344 if (ev != iscsilun->events) {
dca21ef2
FZ
345 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi),
346 false,
05b685fb 347 (ev & POLLIN) ? iscsi_process_read : NULL,
80cf6257
SH
348 (ev & POLLOUT) ? iscsi_process_write : NULL,
349 iscsilun);
05b685fb
PL
350 iscsilun->events = ev;
351 }
05b685fb 352}
c9b9f682 353
5dd7a535 354static void iscsi_timed_check_events(void *opaque)
05b685fb
PL
355{
356 IscsiLun *iscsilun = opaque;
5dd7a535
PL
357
358 /* check for timed out requests */
359 iscsi_service(iscsilun->iscsi, 0);
360
361 if (iscsilun->request_timed_out) {
362 iscsilun->request_timed_out = false;
363 iscsi_reconnect(iscsilun->iscsi);
364 }
365
366 /* newer versions of libiscsi may return zero events. Ensure we are able
367 * to return to service once this situation changes. */
05b685fb 368 iscsi_set_events(iscsilun);
5dd7a535
PL
369
370 timer_mod(iscsilun->event_timer,
371 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
c589b249
RS
372}
373
374static void
375iscsi_process_read(void *arg)
376{
377 IscsiLun *iscsilun = arg;
378 struct iscsi_context *iscsi = iscsilun->iscsi;
379
380 iscsi_service(iscsi, POLLIN);
381 iscsi_set_events(iscsilun);
382}
383
384static void
385iscsi_process_write(void *arg)
386{
387 IscsiLun *iscsilun = arg;
388 struct iscsi_context *iscsi = iscsilun->iscsi;
389
390 iscsi_service(iscsi, POLLOUT);
391 iscsi_set_events(iscsilun);
392}
393
0777b5dd
PL
394static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
395{
396 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
397}
398
c589b249
RS
399static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
400{
401 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
402}
403
91bea4e2
PL
404static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
405 IscsiLun *iscsilun)
406{
407 if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
408 (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
f5075224
RJ
409 error_report("iSCSI misaligned request: "
410 "iscsilun->block_size %u, sector_num %" PRIi64
411 ", nb_sectors %d",
91bea4e2
PL
412 iscsilun->block_size, sector_num, nb_sectors);
413 return 0;
414 }
415 return 1;
416}
417
a9fe4c95
PL
418static unsigned long *iscsi_allocationmap_init(IscsiLun *iscsilun)
419{
420 return bitmap_try_new(DIV_ROUND_UP(sector_lun2qemu(iscsilun->num_blocks,
421 iscsilun),
422 iscsilun->cluster_sectors));
423}
424
b03c3805
PL
425static void iscsi_allocationmap_set(IscsiLun *iscsilun, int64_t sector_num,
426 int nb_sectors)
427{
428 if (iscsilun->allocationmap == NULL) {
429 return;
430 }
431 bitmap_set(iscsilun->allocationmap,
432 sector_num / iscsilun->cluster_sectors,
433 DIV_ROUND_UP(nb_sectors, iscsilun->cluster_sectors));
434}
435
436static void iscsi_allocationmap_clear(IscsiLun *iscsilun, int64_t sector_num,
437 int nb_sectors)
438{
439 int64_t cluster_num, nb_clusters;
440 if (iscsilun->allocationmap == NULL) {
441 return;
442 }
443 cluster_num = DIV_ROUND_UP(sector_num, iscsilun->cluster_sectors);
444 nb_clusters = (sector_num + nb_sectors) / iscsilun->cluster_sectors
445 - cluster_num;
446 if (nb_clusters > 0) {
447 bitmap_clear(iscsilun->allocationmap, cluster_num, nb_clusters);
448 }
449}
450
9f0eb9e1
KW
451static int coroutine_fn
452iscsi_co_writev_flags(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
453 QEMUIOVector *iov, int flags)
c589b249 454{
063c3378
PL
455 IscsiLun *iscsilun = bs->opaque;
456 struct IscsiTask iTask;
f4dfa67f 457 uint64_t lba;
063c3378 458 uint32_t num_sectors;
4df863f3 459 bool fua = flags & BDRV_REQ_FUA;
c589b249 460
4df863f3
EB
461 if (fua) {
462 assert(iscsilun->dpofua);
463 }
063c3378
PL
464 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
465 return -EINVAL;
466 }
7371d56f 467
dc9e7163
PL
468 if (bs->bl.max_transfer_length && nb_sectors > bs->bl.max_transfer_length) {
469 error_report("iSCSI Error: Write of %d sectors exceeds max_xfer_len "
470 "of %d sectors", nb_sectors, bs->bl.max_transfer_length);
471 return -EINVAL;
472 }
473
063c3378
PL
474 lba = sector_qemu2lun(sector_num, iscsilun);
475 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
063c3378
PL
476 iscsi_co_init_iscsitask(iscsilun, &iTask);
477retry:
9281fe9e
PL
478 if (iscsilun->use_16_for_rw) {
479 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
8c215a9f 480 NULL, num_sectors * iscsilun->block_size,
73b5394e 481 iscsilun->block_size, 0, 0, fua, 0, 0,
9281fe9e
PL
482 iscsi_co_generic_cb, &iTask);
483 } else {
484 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,
8c215a9f 485 NULL, num_sectors * iscsilun->block_size,
73b5394e 486 iscsilun->block_size, 0, 0, fua, 0, 0,
9281fe9e
PL
487 iscsi_co_generic_cb, &iTask);
488 }
063c3378 489 if (iTask.task == NULL) {
92397116 490 return -ENOMEM;
f4dfa67f 491 }
063c3378
PL
492 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
493 iov->niov);
063c3378
PL
494 while (!iTask.complete) {
495 iscsi_set_events(iscsilun);
496 qemu_coroutine_yield();
c589b249
RS
497 }
498
063c3378
PL
499 if (iTask.task != NULL) {
500 scsi_free_scsi_task(iTask.task);
501 iTask.task = NULL;
91bea4e2
PL
502 }
503
063c3378 504 if (iTask.do_retry) {
837c3901 505 iTask.complete = 0;
063c3378 506 goto retry;
1dde716e
PL
507 }
508
063c3378 509 if (iTask.status != SCSI_STATUS_GOOD) {
e01dd3da 510 return iTask.err_code;
c589b249
RS
511 }
512
b03c3805
PL
513 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
514
063c3378 515 return 0;
c589b249
RS
516}
517
b03c3805
PL
518
519static bool iscsi_allocationmap_is_allocated(IscsiLun *iscsilun,
520 int64_t sector_num, int nb_sectors)
521{
522 unsigned long size;
523 if (iscsilun->allocationmap == NULL) {
524 return true;
525 }
526 size = DIV_ROUND_UP(sector_num + nb_sectors, iscsilun->cluster_sectors);
527 return !(find_next_bit(iscsilun->allocationmap, size,
528 sector_num / iscsilun->cluster_sectors) == size);
529}
530
b03c3805
PL
531static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
532 int64_t sector_num,
67a0fd2a
FZ
533 int nb_sectors, int *pnum,
534 BlockDriverState **file)
b03c3805
PL
535{
536 IscsiLun *iscsilun = bs->opaque;
537 struct scsi_get_lba_status *lbas = NULL;
538 struct scsi_lba_status_descriptor *lbasd = NULL;
539 struct IscsiTask iTask;
540 int64_t ret;
541
542 iscsi_co_init_iscsitask(iscsilun, &iTask);
543
544 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
545 ret = -EINVAL;
546 goto out;
547 }
548
549 /* default to all sectors allocated */
550 ret = BDRV_BLOCK_DATA;
551 ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
552 *pnum = nb_sectors;
553
554 /* LUN does not support logical block provisioning */
0a386e48 555 if (!iscsilun->lbpme) {
b03c3805
PL
556 goto out;
557 }
558
559retry:
560 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
561 sector_qemu2lun(sector_num, iscsilun),
562 8 + 16, iscsi_co_generic_cb,
563 &iTask) == NULL) {
564 ret = -ENOMEM;
565 goto out;
566 }
567
568 while (!iTask.complete) {
569 iscsi_set_events(iscsilun);
570 qemu_coroutine_yield();
571 }
572
573 if (iTask.do_retry) {
574 if (iTask.task != NULL) {
575 scsi_free_scsi_task(iTask.task);
576 iTask.task = NULL;
577 }
578 iTask.complete = 0;
579 goto retry;
580 }
581
582 if (iTask.status != SCSI_STATUS_GOOD) {
583 /* in case the get_lba_status_callout fails (i.e.
584 * because the device is busy or the cmd is not
585 * supported) we pretend all blocks are allocated
586 * for backwards compatibility */
587 goto out;
588 }
589
590 lbas = scsi_datain_unmarshall(iTask.task);
591 if (lbas == NULL) {
592 ret = -EIO;
593 goto out;
594 }
595
596 lbasd = &lbas->descriptors[0];
597
598 if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
599 ret = -EIO;
600 goto out;
601 }
602
603 *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
604
605 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
606 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
607 ret &= ~BDRV_BLOCK_DATA;
608 if (iscsilun->lbprz) {
609 ret |= BDRV_BLOCK_ZERO;
610 }
611 }
612
613 if (ret & BDRV_BLOCK_ZERO) {
614 iscsi_allocationmap_clear(iscsilun, sector_num, *pnum);
615 } else {
616 iscsi_allocationmap_set(iscsilun, sector_num, *pnum);
617 }
618
619 if (*pnum > nb_sectors) {
620 *pnum = nb_sectors;
621 }
622out:
623 if (iTask.task != NULL) {
624 scsi_free_scsi_task(iTask.task);
625 }
3399833f
FZ
626 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID) {
627 *file = bs;
628 }
b03c3805
PL
629 return ret;
630}
631
063c3378
PL
632static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
633 int64_t sector_num, int nb_sectors,
634 QEMUIOVector *iov)
c589b249 635{
063c3378
PL
636 IscsiLun *iscsilun = bs->opaque;
637 struct IscsiTask iTask;
1dde716e
PL
638 uint64_t lba;
639 uint32_t num_sectors;
c589b249 640
063c3378
PL
641 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
642 return -EINVAL;
f4dfa67f 643 }
f4dfa67f 644
dc9e7163
PL
645 if (bs->bl.max_transfer_length && nb_sectors > bs->bl.max_transfer_length) {
646 error_report("iSCSI Error: Read of %d sectors exceeds max_xfer_len "
647 "of %d sectors", nb_sectors, bs->bl.max_transfer_length);
648 return -EINVAL;
649 }
650
5917af81 651 if (iscsilun->lbprz && nb_sectors >= ISCSI_CHECKALLOC_THRES &&
b03c3805
PL
652 !iscsi_allocationmap_is_allocated(iscsilun, sector_num, nb_sectors)) {
653 int64_t ret;
654 int pnum;
67a0fd2a
FZ
655 BlockDriverState *file;
656 ret = iscsi_co_get_block_status(bs, sector_num, INT_MAX, &pnum, &file);
b03c3805
PL
657 if (ret < 0) {
658 return ret;
659 }
660 if (ret & BDRV_BLOCK_ZERO && pnum >= nb_sectors) {
661 qemu_iovec_memset(iov, 0, 0x00, iov->size);
662 return 0;
663 }
664 }
b03c3805 665
063c3378
PL
666 lba = sector_qemu2lun(sector_num, iscsilun);
667 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
f4dfa67f 668
063c3378
PL
669 iscsi_co_init_iscsitask(iscsilun, &iTask);
670retry:
9281fe9e 671 if (iscsilun->use_16_for_rw) {
063c3378
PL
672 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
673 num_sectors * iscsilun->block_size,
674 iscsilun->block_size, 0, 0, 0, 0, 0,
675 iscsi_co_generic_cb, &iTask);
9281fe9e 676 } else {
063c3378
PL
677 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
678 num_sectors * iscsilun->block_size,
219c2521 679 iscsilun->block_size,
219c2521 680 0, 0, 0, 0, 0,
063c3378 681 iscsi_co_generic_cb, &iTask);
f4dfa67f 682 }
063c3378 683 if (iTask.task == NULL) {
92397116 684 return -ENOMEM;
c589b249 685 }
063c3378 686 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
91bea4e2 687
063c3378
PL
688 while (!iTask.complete) {
689 iscsi_set_events(iscsilun);
690 qemu_coroutine_yield();
1dde716e 691 }
c589b249 692
063c3378
PL
693 if (iTask.task != NULL) {
694 scsi_free_scsi_task(iTask.task);
695 iTask.task = NULL;
c589b249
RS
696 }
697
063c3378 698 if (iTask.do_retry) {
837c3901 699 iTask.complete = 0;
063c3378 700 goto retry;
c589b249
RS
701 }
702
063c3378 703 if (iTask.status != SCSI_STATUS_GOOD) {
e01dd3da 704 return iTask.err_code;
1dde716e
PL
705 }
706
707 return 0;
708}
709
063c3378 710static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
1dde716e
PL
711{
712 IscsiLun *iscsilun = bs->opaque;
063c3378 713 struct IscsiTask iTask;
1dde716e 714
73b5394e 715 iscsi_co_init_iscsitask(iscsilun, &iTask);
063c3378
PL
716retry:
717 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
718 0, iscsi_co_generic_cb, &iTask) == NULL) {
92397116 719 return -ENOMEM;
063c3378 720 }
1dde716e 721
063c3378
PL
722 while (!iTask.complete) {
723 iscsi_set_events(iscsilun);
724 qemu_coroutine_yield();
725 }
1dde716e 726
063c3378
PL
727 if (iTask.task != NULL) {
728 scsi_free_scsi_task(iTask.task);
729 iTask.task = NULL;
c589b249
RS
730 }
731
063c3378 732 if (iTask.do_retry) {
837c3901 733 iTask.complete = 0;
063c3378
PL
734 goto retry;
735 }
c589b249 736
063c3378 737 if (iTask.status != SCSI_STATUS_GOOD) {
e01dd3da 738 return iTask.err_code;
063c3378
PL
739 }
740
741 return 0;
c589b249
RS
742}
743
98392453
RS
744#ifdef __linux__
745static void
746iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
747 void *command_data, void *opaque)
748{
749 IscsiAIOCB *acb = opaque;
750
0a53f010
RS
751 g_free(acb->buf);
752 acb->buf = NULL;
753
98392453
RS
754 acb->status = 0;
755 if (status < 0) {
756 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
757 iscsi_get_error(iscsi));
e01dd3da 758 acb->status = iscsi_translate_sense(&acb->task->sense);
98392453
RS
759 }
760
761 acb->ioh->driver_status = 0;
762 acb->ioh->host_status = 0;
763 acb->ioh->resid = 0;
764
765#define SG_ERR_DRIVER_SENSE 0x08
766
767 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
768 int ss;
769
770 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
771
772 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
773 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
774 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
775 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
776 }
777
cfb3f506 778 iscsi_schedule_bh(acb);
98392453
RS
779}
780
4bb17ab5
FZ
781static void iscsi_ioctl_bh_completion(void *opaque)
782{
783 IscsiAIOCB *acb = opaque;
784
785 qemu_bh_delete(acb->bh);
786 acb->common.cb(acb->common.opaque, acb->ret);
787 qemu_aio_unref(acb);
788}
789
790static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf)
791{
792 BlockDriverState *bs = acb->common.bs;
793 IscsiLun *iscsilun = bs->opaque;
794 int ret = 0;
795
796 switch (req) {
797 case SG_GET_VERSION_NUM:
798 *(int *)buf = 30000;
799 break;
800 case SG_GET_SCSI_ID:
801 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
802 break;
803 default:
804 ret = -EINVAL;
805 }
806 assert(!acb->bh);
807 acb->bh = aio_bh_new(bdrv_get_aio_context(bs),
808 iscsi_ioctl_bh_completion, acb);
809 acb->ret = ret;
810 qemu_bh_schedule(acb->bh);
811}
812
7c84b1b8 813static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
98392453 814 unsigned long int req, void *buf,
097310b5 815 BlockCompletionFunc *cb, void *opaque)
98392453
RS
816{
817 IscsiLun *iscsilun = bs->opaque;
818 struct iscsi_context *iscsi = iscsilun->iscsi;
819 struct iscsi_data data;
820 IscsiAIOCB *acb;
821
d7331bed 822 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
98392453
RS
823
824 acb->iscsilun = iscsilun;
1bd075f2
PB
825 acb->bh = NULL;
826 acb->status = -EINPROGRESS;
98392453
RS
827 acb->buf = NULL;
828 acb->ioh = buf;
829
4bb17ab5
FZ
830 if (req != SG_IO) {
831 iscsi_ioctl_handle_emulated(acb, req, buf);
832 return &acb->common;
833 }
834
98392453
RS
835 acb->task = malloc(sizeof(struct scsi_task));
836 if (acb->task == NULL) {
837 error_report("iSCSI: Failed to allocate task for scsi command. %s",
838 iscsi_get_error(iscsi));
8007429a 839 qemu_aio_unref(acb);
98392453
RS
840 return NULL;
841 }
842 memset(acb->task, 0, sizeof(struct scsi_task));
843
844 switch (acb->ioh->dxfer_direction) {
845 case SG_DXFER_TO_DEV:
846 acb->task->xfer_dir = SCSI_XFER_WRITE;
847 break;
848 case SG_DXFER_FROM_DEV:
849 acb->task->xfer_dir = SCSI_XFER_READ;
850 break;
851 default:
852 acb->task->xfer_dir = SCSI_XFER_NONE;
853 break;
854 }
855
856 acb->task->cdb_size = acb->ioh->cmd_len;
857 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
858 acb->task->expxferlen = acb->ioh->dxfer_len;
859
0a53f010 860 data.size = 0;
98392453 861 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
0a53f010
RS
862 if (acb->ioh->iovec_count == 0) {
863 data.data = acb->ioh->dxferp;
864 data.size = acb->ioh->dxfer_len;
865 } else {
0a53f010
RS
866 scsi_task_set_iov_out(acb->task,
867 (struct scsi_iovec *) acb->ioh->dxferp,
868 acb->ioh->iovec_count);
0a53f010 869 }
98392453 870 }
0a53f010 871
98392453
RS
872 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
873 iscsi_aio_ioctl_cb,
0a53f010 874 (data.size > 0) ? &data : NULL,
98392453
RS
875 acb) != 0) {
876 scsi_free_scsi_task(acb->task);
8007429a 877 qemu_aio_unref(acb);
98392453
RS
878 return NULL;
879 }
880
881 /* tell libiscsi to read straight into the buffer we got from ioctl */
882 if (acb->task->xfer_dir == SCSI_XFER_READ) {
0a53f010
RS
883 if (acb->ioh->iovec_count == 0) {
884 scsi_task_add_data_in_buffer(acb->task,
885 acb->ioh->dxfer_len,
886 acb->ioh->dxferp);
887 } else {
0a53f010
RS
888 scsi_task_set_iov_in(acb->task,
889 (struct scsi_iovec *) acb->ioh->dxferp,
890 acb->ioh->iovec_count);
0a53f010 891 }
98392453
RS
892 }
893
894 iscsi_set_events(iscsilun);
895
896 return &acb->common;
897}
898
98392453
RS
899#endif
900
c589b249
RS
901static int64_t
902iscsi_getlength(BlockDriverState *bs)
903{
904 IscsiLun *iscsilun = bs->opaque;
905 int64_t len;
906
907 len = iscsilun->num_blocks;
908 len *= iscsilun->block_size;
909
910 return len;
911}
912
65f3e339
PL
913static int
914coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
915 int nb_sectors)
916{
917 IscsiLun *iscsilun = bs->opaque;
918 struct IscsiTask iTask;
919 struct unmap_list list;
65f3e339
PL
920
921 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
922 return -EINVAL;
923 }
924
925 if (!iscsilun->lbp.lbpu) {
926 /* UNMAP is not supported by the target */
927 return 0;
928 }
929
930 list.lba = sector_qemu2lun(sector_num, iscsilun);
01a6a238 931 list.num = sector_qemu2lun(nb_sectors, iscsilun);
65f3e339 932
01a6a238 933 iscsi_co_init_iscsitask(iscsilun, &iTask);
65f3e339 934retry:
01a6a238
PL
935 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
936 iscsi_co_generic_cb, &iTask) == NULL) {
92397116 937 return -ENOMEM;
01a6a238 938 }
65f3e339 939
01a6a238
PL
940 while (!iTask.complete) {
941 iscsi_set_events(iscsilun);
942 qemu_coroutine_yield();
943 }
65f3e339 944
01a6a238
PL
945 if (iTask.task != NULL) {
946 scsi_free_scsi_task(iTask.task);
947 iTask.task = NULL;
948 }
65f3e339 949
01a6a238 950 if (iTask.do_retry) {
837c3901 951 iTask.complete = 0;
01a6a238
PL
952 goto retry;
953 }
65f3e339 954
01a6a238
PL
955 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
956 /* the target might fail with a check condition if it
957 is not happy with the alignment of the UNMAP request
958 we silently fail in this case */
959 return 0;
960 }
65f3e339 961
01a6a238 962 if (iTask.status != SCSI_STATUS_GOOD) {
e01dd3da 963 return iTask.err_code;
65f3e339
PL
964 }
965
b03c3805
PL
966 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
967
65f3e339
PL
968 return 0;
969}
970
d4cd9615
PL
971static int
972coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
973 int nb_sectors, BdrvRequestFlags flags)
974{
975 IscsiLun *iscsilun = bs->opaque;
976 struct IscsiTask iTask;
977 uint64_t lba;
978 uint32_t nb_blocks;
9281fe9e 979 bool use_16_for_ws = iscsilun->use_16_for_rw;
d4cd9615
PL
980
981 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
982 return -EINVAL;
983 }
984
9281fe9e
PL
985 if (flags & BDRV_REQ_MAY_UNMAP) {
986 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
987 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
988 use_16_for_ws = true;
989 }
990 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
991 /* WRITESAME16 with UNMAP is not supported by the target,
992 * fall back and try WRITESAME10/16 without UNMAP */
993 flags &= ~BDRV_REQ_MAY_UNMAP;
994 use_16_for_ws = iscsilun->use_16_for_rw;
995 }
fa6252b0
PB
996 }
997
dbe5c58f 998 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
9281fe9e 999 /* WRITESAME without UNMAP is not supported by the target */
d4cd9615
PL
1000 return -ENOTSUP;
1001 }
1002
1003 lba = sector_qemu2lun(sector_num, iscsilun);
1004 nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
1005
1006 if (iscsilun->zeroblock == NULL) {
4d5a3f88
KW
1007 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size);
1008 if (iscsilun->zeroblock == NULL) {
1009 return -ENOMEM;
1010 }
d4cd9615
PL
1011 }
1012
1013 iscsi_co_init_iscsitask(iscsilun, &iTask);
1014retry:
9281fe9e
PL
1015 if (use_16_for_ws) {
1016 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1017 iscsilun->zeroblock, iscsilun->block_size,
1018 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1019 0, 0, iscsi_co_generic_cb, &iTask);
1020 } else {
1021 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
1022 iscsilun->zeroblock, iscsilun->block_size,
1023 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1024 0, 0, iscsi_co_generic_cb, &iTask);
1025 }
1026 if (iTask.task == NULL) {
92397116 1027 return -ENOMEM;
d4cd9615
PL
1028 }
1029
1030 while (!iTask.complete) {
1031 iscsi_set_events(iscsilun);
1032 qemu_coroutine_yield();
1033 }
1034
d9738fd2
PL
1035 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1036 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
27898a5d
PB
1037 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
1038 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
d9738fd2
PL
1039 /* WRITE SAME is not supported by the target */
1040 iscsilun->has_write_same = false;
1041 scsi_free_scsi_task(iTask.task);
1042 return -ENOTSUP;
1043 }
1044
d4cd9615
PL
1045 if (iTask.task != NULL) {
1046 scsi_free_scsi_task(iTask.task);
1047 iTask.task = NULL;
1048 }
1049
1050 if (iTask.do_retry) {
837c3901 1051 iTask.complete = 0;
d4cd9615
PL
1052 goto retry;
1053 }
1054
1055 if (iTask.status != SCSI_STATUS_GOOD) {
e01dd3da 1056 return iTask.err_code;
d4cd9615
PL
1057 }
1058
b03c3805
PL
1059 if (flags & BDRV_REQ_MAY_UNMAP) {
1060 iscsi_allocationmap_clear(iscsilun, sector_num, nb_sectors);
1061 } else {
1062 iscsi_allocationmap_set(iscsilun, sector_num, nb_sectors);
1063 }
1064
d4cd9615
PL
1065 return 0;
1066}
1067
f2917853
PB
1068static void parse_chap(struct iscsi_context *iscsi, const char *target,
1069 Error **errp)
f9dadc98
RS
1070{
1071 QemuOptsList *list;
1072 QemuOpts *opts;
1073 const char *user = NULL;
1074 const char *password = NULL;
b189346e
DB
1075 const char *secretid;
1076 char *secret = NULL;
f9dadc98
RS
1077
1078 list = qemu_find_opts("iscsi");
1079 if (!list) {
f2917853 1080 return;
f9dadc98
RS
1081 }
1082
1083 opts = qemu_opts_find(list, target);
1084 if (opts == NULL) {
1085 opts = QTAILQ_FIRST(&list->head);
1086 if (!opts) {
f2917853 1087 return;
f9dadc98
RS
1088 }
1089 }
1090
1091 user = qemu_opt_get(opts, "user");
1092 if (!user) {
f2917853 1093 return;
f9dadc98
RS
1094 }
1095
b189346e 1096 secretid = qemu_opt_get(opts, "password-secret");
f9dadc98 1097 password = qemu_opt_get(opts, "password");
b189346e
DB
1098 if (secretid && password) {
1099 error_setg(errp, "'password' and 'password-secret' properties are "
1100 "mutually exclusive");
1101 return;
1102 }
1103 if (secretid) {
1104 secret = qcrypto_secret_lookup_as_utf8(secretid, errp);
1105 if (!secret) {
1106 return;
1107 }
1108 password = secret;
1109 } else if (!password) {
f2917853
PB
1110 error_setg(errp, "CHAP username specified but no password was given");
1111 return;
f9dadc98
RS
1112 }
1113
1114 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
f2917853 1115 error_setg(errp, "Failed to set initiator username and password");
f9dadc98 1116 }
b189346e
DB
1117
1118 g_free(secret);
f9dadc98
RS
1119}
1120
f2917853
PB
1121static void parse_header_digest(struct iscsi_context *iscsi, const char *target,
1122 Error **errp)
f9dadc98
RS
1123{
1124 QemuOptsList *list;
1125 QemuOpts *opts;
1126 const char *digest = NULL;
1127
1128 list = qemu_find_opts("iscsi");
1129 if (!list) {
1130 return;
1131 }
1132
1133 opts = qemu_opts_find(list, target);
1134 if (opts == NULL) {
1135 opts = QTAILQ_FIRST(&list->head);
1136 if (!opts) {
1137 return;
1138 }
1139 }
1140
1141 digest = qemu_opt_get(opts, "header-digest");
1142 if (!digest) {
1143 return;
1144 }
1145
1146 if (!strcmp(digest, "CRC32C")) {
1147 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1148 } else if (!strcmp(digest, "NONE")) {
1149 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1150 } else if (!strcmp(digest, "CRC32C-NONE")) {
1151 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1152 } else if (!strcmp(digest, "NONE-CRC32C")) {
1153 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1154 } else {
f2917853 1155 error_setg(errp, "Invalid header-digest setting : %s", digest);
f9dadc98
RS
1156 }
1157}
1158
1159static char *parse_initiator_name(const char *target)
1160{
1161 QemuOptsList *list;
1162 QemuOpts *opts;
5accc840
PB
1163 const char *name;
1164 char *iscsi_name;
1165 UuidInfo *uuid_info;
f9dadc98
RS
1166
1167 list = qemu_find_opts("iscsi");
f2ef4a6d
PB
1168 if (list) {
1169 opts = qemu_opts_find(list, target);
f9dadc98 1170 if (!opts) {
f2ef4a6d
PB
1171 opts = QTAILQ_FIRST(&list->head);
1172 }
1173 if (opts) {
1174 name = qemu_opt_get(opts, "initiator-name");
5accc840
PB
1175 if (name) {
1176 return g_strdup(name);
1177 }
f9dadc98
RS
1178 }
1179 }
1180
5accc840
PB
1181 uuid_info = qmp_query_uuid(NULL);
1182 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1183 name = qemu_get_vm_name();
f2ef4a6d 1184 } else {
5accc840 1185 name = uuid_info->UUID;
f9dadc98 1186 }
5accc840
PB
1187 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1188 name ? ":" : "", name ? name : "");
1189 qapi_free_UuidInfo(uuid_info);
1190 return iscsi_name;
f9dadc98
RS
1191}
1192
5dd7a535
PL
1193static int parse_timeout(const char *target)
1194{
1195 QemuOptsList *list;
1196 QemuOpts *opts;
1197 const char *timeout;
1198
1199 list = qemu_find_opts("iscsi");
1200 if (list) {
1201 opts = qemu_opts_find(list, target);
1202 if (!opts) {
1203 opts = QTAILQ_FIRST(&list->head);
1204 }
1205 if (opts) {
1206 timeout = qemu_opt_get(opts, "timeout");
1207 if (timeout) {
1208 return atoi(timeout);
1209 }
1210 }
1211 }
1212
1213 return 0;
1214}
1215
5b5d34ec
PL
1216static void iscsi_nop_timed_event(void *opaque)
1217{
1218 IscsiLun *iscsilun = opaque;
1219
5dd7a535 1220 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) {
5b5d34ec 1221 error_report("iSCSI: NOP timeout. Reconnecting...");
5dd7a535
PL
1222 iscsilun->request_timed_out = true;
1223 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
5b5d34ec
PL
1224 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1225 return;
1226 }
1227
bc72ad67 1228 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
5b5d34ec
PL
1229 iscsi_set_events(iscsilun);
1230}
5b5d34ec 1231
f2917853 1232static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
cb1b83e7
PL
1233{
1234 struct scsi_task *task = NULL;
1235 struct scsi_readcapacity10 *rc10 = NULL;
1236 struct scsi_readcapacity16 *rc16 = NULL;
cb1b83e7
PL
1237 int retries = ISCSI_CMD_RETRIES;
1238
1288844e
PB
1239 do {
1240 if (task != NULL) {
1241 scsi_free_scsi_task(task);
1242 task = NULL;
cb1b83e7 1243 }
1288844e
PB
1244
1245 switch (iscsilun->type) {
1246 case TYPE_DISK:
1247 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1248 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1249 rc16 = scsi_datain_unmarshall(task);
1250 if (rc16 == NULL) {
f2917853 1251 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1288844e
PB
1252 } else {
1253 iscsilun->block_size = rc16->block_length;
1254 iscsilun->num_blocks = rc16->returned_lba + 1;
0a386e48
PL
1255 iscsilun->lbpme = !!rc16->lbpme;
1256 iscsilun->lbprz = !!rc16->lbprz;
9281fe9e 1257 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
1288844e 1258 }
1cb6d137 1259 break;
1288844e 1260 }
1cb6d137
ZL
1261 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1262 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
1263 break;
1264 }
1265 /* Fall through and try READ CAPACITY(10) instead. */
1288844e
PB
1266 case TYPE_ROM:
1267 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1268 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1269 rc10 = scsi_datain_unmarshall(task);
1270 if (rc10 == NULL) {
f2917853 1271 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1288844e
PB
1272 } else {
1273 iscsilun->block_size = rc10->block_size;
1274 if (rc10->lba == 0) {
1275 /* blank disk loaded */
1276 iscsilun->num_blocks = 0;
1277 } else {
1278 iscsilun->num_blocks = rc10->lba + 1;
1279 }
1280 }
1281 }
1282 break;
1283 default:
f2917853 1284 return;
cb1b83e7 1285 }
1288844e
PB
1286 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1287 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1288 && retries-- > 0);
cb1b83e7 1289
1288844e 1290 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
240125bc 1291 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command");
6d1f252d
PL
1292 } else if (!iscsilun->block_size ||
1293 iscsilun->block_size % BDRV_SECTOR_SIZE) {
1294 error_setg(errp, "iSCSI: the target returned an invalid "
1295 "block size of %d.", iscsilun->block_size);
1288844e 1296 }
cb1b83e7
PL
1297 if (task) {
1298 scsi_free_scsi_task(task);
1299 }
cb1b83e7
PL
1300}
1301
60beb341
KW
1302/* TODO Convert to fine grained options */
1303static QemuOptsList runtime_opts = {
1304 .name = "iscsi",
1305 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1306 .desc = {
1307 {
1308 .name = "filename",
1309 .type = QEMU_OPT_STRING,
1310 .help = "URL to the iscsi image",
1311 },
1312 { /* end of list */ }
1313 },
1314};
1315
35cb1748 1316static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
24d3bd67 1317 int evpd, int pc, void **inq, Error **errp)
35cb1748
PB
1318{
1319 int full_size;
1320 struct scsi_task *task = NULL;
1321 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1322 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1323 goto fail;
1324 }
1325 full_size = scsi_datain_getfullsize(task);
1326 if (full_size > task->datain.size) {
1327 scsi_free_scsi_task(task);
1328
1329 /* we need more data for the full list */
1330 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
f18a7cbb
PL
1331 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1332 goto fail;
1333 }
35cb1748 1334 }
f18a7cbb 1335
24d3bd67
PL
1336 *inq = scsi_datain_unmarshall(task);
1337 if (*inq == NULL) {
1338 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
172fc4dd 1339 goto fail_with_err;
24d3bd67
PL
1340 }
1341
35cb1748 1342 return task;
f18a7cbb
PL
1343
1344fail:
172fc4dd
MA
1345 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1346 iscsi_get_error(iscsi));
1347fail_with_err:
24d3bd67 1348 if (task != NULL) {
35cb1748 1349 scsi_free_scsi_task(task);
35cb1748
PB
1350 }
1351 return NULL;
f18a7cbb
PL
1352}
1353
80cf6257
SH
1354static void iscsi_detach_aio_context(BlockDriverState *bs)
1355{
1356 IscsiLun *iscsilun = bs->opaque;
1357
dca21ef2
FZ
1358 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi),
1359 false, NULL, NULL, NULL);
80cf6257
SH
1360 iscsilun->events = 0;
1361
1362 if (iscsilun->nop_timer) {
1363 timer_del(iscsilun->nop_timer);
1364 timer_free(iscsilun->nop_timer);
1365 iscsilun->nop_timer = NULL;
1366 }
05b685fb
PL
1367 if (iscsilun->event_timer) {
1368 timer_del(iscsilun->event_timer);
1369 timer_free(iscsilun->event_timer);
1370 iscsilun->event_timer = NULL;
1371 }
80cf6257
SH
1372}
1373
1374static void iscsi_attach_aio_context(BlockDriverState *bs,
1375 AioContext *new_context)
1376{
1377 IscsiLun *iscsilun = bs->opaque;
1378
1379 iscsilun->aio_context = new_context;
1380 iscsi_set_events(iscsilun);
1381
80cf6257
SH
1382 /* Set up a timer for sending out iSCSI NOPs */
1383 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1384 QEMU_CLOCK_REALTIME, SCALE_MS,
1385 iscsi_nop_timed_event, iscsilun);
1386 timer_mod(iscsilun->nop_timer,
1387 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
05b685fb 1388
5dd7a535
PL
1389 /* Set up a timer for periodic calls to iscsi_set_events and to
1390 * scan for command timeout */
05b685fb
PL
1391 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context,
1392 QEMU_CLOCK_REALTIME, SCALE_MS,
5dd7a535
PL
1393 iscsi_timed_check_events, iscsilun);
1394 timer_mod(iscsilun->event_timer,
1395 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
80cf6257
SH
1396}
1397
7191f208 1398static void iscsi_modesense_sync(IscsiLun *iscsilun)
c1d4096b
FZ
1399{
1400 struct scsi_task *task;
1401 struct scsi_mode_sense *ms = NULL;
7191f208 1402 iscsilun->write_protected = false;
752ce451 1403 iscsilun->dpofua = false;
c1d4096b
FZ
1404
1405 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1406 1, SCSI_MODESENSE_PC_CURRENT,
1407 0x3F, 0, 255);
1408 if (task == NULL) {
1409 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1410 iscsi_get_error(iscsilun->iscsi));
1411 goto out;
1412 }
1413
1414 if (task->status != SCSI_STATUS_GOOD) {
1415 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1416 goto out;
1417 }
1418 ms = scsi_datain_unmarshall(task);
1419 if (!ms) {
1420 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1421 iscsi_get_error(iscsilun->iscsi));
1422 goto out;
1423 }
7191f208 1424 iscsilun->write_protected = ms->device_specific_parameter & 0x80;
752ce451 1425 iscsilun->dpofua = ms->device_specific_parameter & 0x10;
c1d4096b
FZ
1426
1427out:
1428 if (task) {
1429 scsi_free_scsi_task(task);
1430 }
c1d4096b
FZ
1431}
1432
c589b249
RS
1433/*
1434 * We support iscsi url's on the form
1435 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1436 */
015a1036
HR
1437static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1438 Error **errp)
c589b249
RS
1439{
1440 IscsiLun *iscsilun = bs->opaque;
1441 struct iscsi_context *iscsi = NULL;
1442 struct iscsi_url *iscsi_url = NULL;
e829b0bb
PL
1443 struct scsi_task *task = NULL;
1444 struct scsi_inquiry_standard *inq = NULL;
24d3bd67 1445 struct scsi_inquiry_supported_pages *inq_vpd;
f9dadc98 1446 char *initiator_name = NULL;
60beb341
KW
1447 QemuOpts *opts;
1448 Error *local_err = NULL;
1449 const char *filename;
9049736e 1450 int i, ret = 0, timeout = 0;
c589b249 1451
87ea75d5 1452 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
60beb341 1453 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 1454 if (local_err) {
f2917853 1455 error_propagate(errp, local_err);
60beb341
KW
1456 ret = -EINVAL;
1457 goto out;
1458 }
1459
1460 filename = qemu_opt_get(opts, "filename");
1461
c589b249
RS
1462 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1463 if (iscsi_url == NULL) {
f2917853 1464 error_setg(errp, "Failed to parse URL : %s", filename);
c589b249 1465 ret = -EINVAL;
b93c94f7 1466 goto out;
c589b249
RS
1467 }
1468
f9dadc98
RS
1469 memset(iscsilun, 0, sizeof(IscsiLun));
1470
1471 initiator_name = parse_initiator_name(iscsi_url->target);
1472
1473 iscsi = iscsi_create_context(initiator_name);
1474 if (iscsi == NULL) {
f2917853 1475 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
f9dadc98 1476 ret = -ENOMEM;
b93c94f7 1477 goto out;
f9dadc98
RS
1478 }
1479
c589b249 1480 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
f2917853 1481 error_setg(errp, "iSCSI: Failed to set target name.");
c589b249 1482 ret = -EINVAL;
b93c94f7 1483 goto out;
c589b249
RS
1484 }
1485
532cee41 1486 if (iscsi_url->user[0] != '\0') {
c589b249
RS
1487 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1488 iscsi_url->passwd);
1489 if (ret != 0) {
f2917853 1490 error_setg(errp, "Failed to set initiator username and password");
c589b249 1491 ret = -EINVAL;
b93c94f7 1492 goto out;
c589b249
RS
1493 }
1494 }
f9dadc98
RS
1495
1496 /* check if we got CHAP username/password via the options */
f2917853
PB
1497 parse_chap(iscsi, iscsi_url->target, &local_err);
1498 if (local_err != NULL) {
1499 error_propagate(errp, local_err);
f9dadc98 1500 ret = -EINVAL;
b93c94f7 1501 goto out;
f9dadc98
RS
1502 }
1503
c589b249 1504 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
f2917853 1505 error_setg(errp, "iSCSI: Failed to set session type to normal.");
c589b249 1506 ret = -EINVAL;
b93c94f7 1507 goto out;
c589b249
RS
1508 }
1509
1510 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1511
f9dadc98 1512 /* check if we got HEADER_DIGEST via the options */
f2917853
PB
1513 parse_header_digest(iscsi, iscsi_url->target, &local_err);
1514 if (local_err != NULL) {
1515 error_propagate(errp, local_err);
1516 ret = -EINVAL;
1517 goto out;
1518 }
f9dadc98 1519
9049736e
PL
1520 /* timeout handling is broken in libiscsi before 1.15.0 */
1521 timeout = parse_timeout(iscsi_url->target);
1522#if defined(LIBISCSI_API_VERSION) && LIBISCSI_API_VERSION >= 20150621
1523 iscsi_set_timeout(iscsi, timeout);
1524#else
1525 if (timeout) {
1526 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1527 }
1528#endif
5dd7a535 1529
e829b0bb 1530 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
f2917853 1531 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
e829b0bb
PL
1532 iscsi_get_error(iscsi));
1533 ret = -EINVAL;
1534 goto out;
1535 }
c589b249
RS
1536
1537 iscsilun->iscsi = iscsi;
80cf6257 1538 iscsilun->aio_context = bdrv_get_aio_context(bs);
c589b249 1539 iscsilun->lun = iscsi_url->lun;
24d3bd67 1540 iscsilun->has_write_same = true;
c589b249 1541
24d3bd67
PL
1542 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1543 (void **) &inq, errp);
1544 if (task == NULL) {
c589b249 1545 ret = -EINVAL;
b93c94f7 1546 goto out;
c589b249 1547 }
e829b0bb 1548 iscsilun->type = inq->periperal_device_type;
24d3bd67
PL
1549 scsi_free_scsi_task(task);
1550 task = NULL;
e829b0bb 1551
7191f208 1552 iscsi_modesense_sync(iscsilun);
4df863f3
EB
1553 if (iscsilun->dpofua) {
1554 bs->supported_write_flags = BDRV_REQ_FUA;
1555 }
465fe887 1556 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
7191f208 1557
c1d4096b
FZ
1558 /* Check the write protect flag of the LUN if we want to write */
1559 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) &&
43ae8fb1 1560 iscsilun->write_protected) {
c1d4096b
FZ
1561 error_setg(errp, "Cannot open a write protected LUN as read-write");
1562 ret = -EACCES;
1563 goto out;
1564 }
1565
f2917853
PB
1566 iscsi_readcapacity_sync(iscsilun, &local_err);
1567 if (local_err != NULL) {
1568 error_propagate(errp, local_err);
cd82b6fb 1569 ret = -EINVAL;
cb1b83e7 1570 goto out;
e829b0bb 1571 }
0777b5dd 1572 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
2c9880c4 1573 bs->request_alignment = iscsilun->block_size;
e829b0bb 1574
f47c3f5a
KW
1575 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1576 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1577 * will try to read from the device to guess the image format.
622695a4 1578 */
f47c3f5a 1579 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
622695a4
RS
1580 bs->sg = 1;
1581 }
1582
24d3bd67
PL
1583 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1584 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1585 (void **) &inq_vpd, errp);
1586 if (task == NULL) {
1587 ret = -EINVAL;
1588 goto out;
f18a7cbb 1589 }
24d3bd67
PL
1590 for (i = 0; i < inq_vpd->num_pages; i++) {
1591 struct scsi_task *inq_task;
1592 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
f18a7cbb 1593 struct scsi_inquiry_block_limits *inq_bl;
24d3bd67
PL
1594 switch (inq_vpd->pages[i]) {
1595 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1596 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1597 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1598 (void **) &inq_lbp, errp);
1599 if (inq_task == NULL) {
1600 ret = -EINVAL;
1601 goto out;
1602 }
1603 memcpy(&iscsilun->lbp, inq_lbp,
1604 sizeof(struct scsi_inquiry_logical_block_provisioning));
1605 scsi_free_scsi_task(inq_task);
1606 break;
1607 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1608 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1609 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1610 (void **) &inq_bl, errp);
1611 if (inq_task == NULL) {
1612 ret = -EINVAL;
1613 goto out;
1614 }
1615 memcpy(&iscsilun->bl, inq_bl,
1616 sizeof(struct scsi_inquiry_block_limits));
1617 scsi_free_scsi_task(inq_task);
1618 break;
1619 default:
1620 break;
f18a7cbb 1621 }
f18a7cbb 1622 }
24d3bd67
PL
1623 scsi_free_scsi_task(task);
1624 task = NULL;
f18a7cbb 1625
80cf6257 1626 iscsi_attach_aio_context(bs, iscsilun->aio_context);
5b5d34ec 1627
b03c3805
PL
1628 /* Guess the internal cluster (page) size of the iscsi target by the means
1629 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1630 * reasonable size */
3d2acaa3 1631 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
b03c3805
PL
1632 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1633 iscsilun->cluster_sectors = (iscsilun->bl.opt_unmap_gran *
1634 iscsilun->block_size) >> BDRV_SECTOR_BITS;
9eac3622 1635 if (iscsilun->lbprz) {
a9fe4c95
PL
1636 iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun);
1637 if (iscsilun->allocationmap == NULL) {
1638 ret = -ENOMEM;
1639 }
b03c3805 1640 }
b03c3805
PL
1641 }
1642
b93c94f7 1643out:
60beb341 1644 qemu_opts_del(opts);
f7047c2d 1645 g_free(initiator_name);
c589b249
RS
1646 if (iscsi_url != NULL) {
1647 iscsi_destroy_url(iscsi_url);
1648 }
e829b0bb
PL
1649 if (task != NULL) {
1650 scsi_free_scsi_task(task);
1651 }
b93c94f7
PB
1652
1653 if (ret) {
1654 if (iscsi != NULL) {
20474e9a
PL
1655 if (iscsi_is_logged_in(iscsi)) {
1656 iscsi_logout_sync(iscsi);
1657 }
b93c94f7
PB
1658 iscsi_destroy_context(iscsi);
1659 }
1660 memset(iscsilun, 0, sizeof(IscsiLun));
c589b249 1661 }
c589b249
RS
1662 return ret;
1663}
1664
1665static void iscsi_close(BlockDriverState *bs)
1666{
1667 IscsiLun *iscsilun = bs->opaque;
1668 struct iscsi_context *iscsi = iscsilun->iscsi;
1669
80cf6257 1670 iscsi_detach_aio_context(bs);
20474e9a
PL
1671 if (iscsi_is_logged_in(iscsi)) {
1672 iscsi_logout_sync(iscsi);
1673 }
c589b249 1674 iscsi_destroy_context(iscsi);
d4cd9615 1675 g_free(iscsilun->zeroblock);
b03c3805 1676 g_free(iscsilun->allocationmap);
c589b249
RS
1677 memset(iscsilun, 0, sizeof(IscsiLun));
1678}
1679
52f6fa14 1680static int sector_limits_lun2qemu(int64_t sector, IscsiLun *iscsilun)
d34682cd 1681{
52f6fa14
PL
1682 return MIN(sector_lun2qemu(sector, iscsilun), INT_MAX / 2 + 1);
1683}
d34682cd 1684
52f6fa14
PL
1685static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)
1686{
d34682cd
KW
1687 /* We don't actually refresh here, but just return data queried in
1688 * iscsi_open(): iscsi targets don't change their limits. */
52f6fa14
PL
1689
1690 IscsiLun *iscsilun = bs->opaque;
1691 uint32_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff;
1692
1693 if (iscsilun->bl.max_xfer_len) {
1694 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len);
1695 }
1696
1697 bs->bl.max_transfer_length = sector_limits_lun2qemu(max_xfer_len, iscsilun);
1698
c97ca29d 1699 if (iscsilun->lbp.lbpu) {
d34682cd 1700 if (iscsilun->bl.max_unmap < 0xffffffff) {
3dab1551
PL
1701 bs->bl.max_discard =
1702 sector_limits_lun2qemu(iscsilun->bl.max_unmap, iscsilun);
d34682cd 1703 }
3dab1551
PL
1704 bs->bl.discard_alignment =
1705 sector_limits_lun2qemu(iscsilun->bl.opt_unmap_gran, iscsilun);
c97ca29d 1706 }
d34682cd 1707
c97ca29d 1708 if (iscsilun->bl.max_ws_len < 0xffffffff) {
3dab1551
PL
1709 bs->bl.max_write_zeroes =
1710 sector_limits_lun2qemu(iscsilun->bl.max_ws_len, iscsilun);
c97ca29d
PB
1711 }
1712 if (iscsilun->lbp.lbpws) {
3dab1551
PL
1713 bs->bl.write_zeroes_alignment =
1714 sector_limits_lun2qemu(iscsilun->bl.opt_unmap_gran, iscsilun);
d34682cd 1715 }
3dab1551
PL
1716 bs->bl.opt_transfer_length =
1717 sector_limits_lun2qemu(iscsilun->bl.opt_xfer_len, iscsilun);
e9f526ab 1718}
d34682cd 1719
43ae8fb1
FZ
1720/* Note that this will not re-establish a connection with an iSCSI target - it
1721 * is effectively a NOP. */
dc6afb99
JC
1722static int iscsi_reopen_prepare(BDRVReopenState *state,
1723 BlockReopenQueue *queue, Error **errp)
1724{
43ae8fb1
FZ
1725 IscsiLun *iscsilun = state->bs->opaque;
1726
1727 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) {
1728 error_setg(errp, "Cannot open a write protected LUN as read-write");
1729 return -EACCES;
1730 }
d34682cd
KW
1731 return 0;
1732}
1733
cb1b83e7
PL
1734static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1735{
1736 IscsiLun *iscsilun = bs->opaque;
f2917853 1737 Error *local_err = NULL;
cb1b83e7
PL
1738
1739 if (iscsilun->type != TYPE_DISK) {
1740 return -ENOTSUP;
1741 }
1742
f2917853
PB
1743 iscsi_readcapacity_sync(iscsilun, &local_err);
1744 if (local_err != NULL) {
1745 error_free(local_err);
1746 return -EIO;
cb1b83e7
PL
1747 }
1748
1749 if (offset > iscsi_getlength(bs)) {
1750 return -EINVAL;
1751 }
1752
b03c3805
PL
1753 if (iscsilun->allocationmap != NULL) {
1754 g_free(iscsilun->allocationmap);
a9fe4c95 1755 iscsilun->allocationmap = iscsi_allocationmap_init(iscsilun);
b03c3805
PL
1756 }
1757
cb1b83e7
PL
1758 return 0;
1759}
1760
a59479e3 1761static int iscsi_create(const char *filename, QemuOpts *opts, Error **errp)
de8864e5
PL
1762{
1763 int ret = 0;
1764 int64_t total_size = 0;
13c91cb7 1765 BlockDriverState *bs;
de8864e5 1766 IscsiLun *iscsilun = NULL;
60beb341 1767 QDict *bs_options;
de8864e5 1768
e4e9986b 1769 bs = bdrv_new();
de8864e5
PL
1770
1771 /* Read out options */
c2eb918e
HT
1772 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1773 BDRV_SECTOR_SIZE);
5839e53b 1774 bs->opaque = g_new0(struct IscsiLun, 1);
13c91cb7 1775 iscsilun = bs->opaque;
de8864e5 1776
60beb341
KW
1777 bs_options = qdict_new();
1778 qdict_put(bs_options, "filename", qstring_from_str(filename));
015a1036 1779 ret = iscsi_open(bs, bs_options, 0, NULL);
60beb341
KW
1780 QDECREF(bs_options);
1781
de8864e5
PL
1782 if (ret != 0) {
1783 goto out;
1784 }
80cf6257 1785 iscsi_detach_aio_context(bs);
de8864e5
PL
1786 if (iscsilun->type != TYPE_DISK) {
1787 ret = -ENODEV;
1788 goto out;
1789 }
13c91cb7 1790 if (bs->total_sectors < total_size) {
de8864e5 1791 ret = -ENOSPC;
d3bda7bc 1792 goto out;
de8864e5
PL
1793 }
1794
1795 ret = 0;
1796out:
1797 if (iscsilun->iscsi != NULL) {
1798 iscsi_destroy_context(iscsilun->iscsi);
1799 }
13c91cb7
FZ
1800 g_free(bs->opaque);
1801 bs->opaque = NULL;
4f6fd349 1802 bdrv_unref(bs);
de8864e5
PL
1803 return ret;
1804}
1805
186d4f2b
PL
1806static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1807{
1808 IscsiLun *iscsilun = bs->opaque;
0a386e48 1809 bdi->unallocated_blocks_are_zero = iscsilun->lbprz;
186d4f2b 1810 bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
b03c3805 1811 bdi->cluster_size = iscsilun->cluster_sectors * BDRV_SECTOR_SIZE;
186d4f2b
PL
1812 return 0;
1813}
1814
a59479e3
CL
1815static QemuOptsList iscsi_create_opts = {
1816 .name = "iscsi-create-opts",
1817 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
1818 .desc = {
1819 {
1820 .name = BLOCK_OPT_SIZE,
1821 .type = QEMU_OPT_SIZE,
1822 .help = "Virtual disk size"
1823 },
1824 { /* end of list */ }
1825 }
de8864e5
PL
1826};
1827
c589b249
RS
1828static BlockDriver bdrv_iscsi = {
1829 .format_name = "iscsi",
1830 .protocol_name = "iscsi",
1831
1832 .instance_size = sizeof(IscsiLun),
030be321 1833 .bdrv_needs_filename = true,
c589b249
RS
1834 .bdrv_file_open = iscsi_open,
1835 .bdrv_close = iscsi_close,
c282e1fd 1836 .bdrv_create = iscsi_create,
a59479e3 1837 .create_opts = &iscsi_create_opts,
dc6afb99 1838 .bdrv_reopen_prepare = iscsi_reopen_prepare,
c589b249
RS
1839
1840 .bdrv_getlength = iscsi_getlength,
186d4f2b 1841 .bdrv_get_info = iscsi_get_info,
cb1b83e7 1842 .bdrv_truncate = iscsi_truncate,
d34682cd 1843 .bdrv_refresh_limits = iscsi_refresh_limits,
c589b249 1844
54a5c1d5 1845 .bdrv_co_get_block_status = iscsi_co_get_block_status,
65f3e339 1846 .bdrv_co_discard = iscsi_co_discard,
d4cd9615 1847 .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
063c3378 1848 .bdrv_co_readv = iscsi_co_readv,
9f0eb9e1 1849 .bdrv_co_writev_flags = iscsi_co_writev_flags,
063c3378 1850 .bdrv_co_flush_to_disk = iscsi_co_flush,
fa6acb0c 1851
98392453 1852#ifdef __linux__
98392453
RS
1853 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1854#endif
80cf6257
SH
1855
1856 .bdrv_detach_aio_context = iscsi_detach_aio_context,
1857 .bdrv_attach_aio_context = iscsi_attach_aio_context,
c589b249
RS
1858};
1859
4d454574
PB
1860static QemuOptsList qemu_iscsi_opts = {
1861 .name = "iscsi",
1862 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1863 .desc = {
1864 {
1865 .name = "user",
1866 .type = QEMU_OPT_STRING,
1867 .help = "username for CHAP authentication to target",
1868 },{
1869 .name = "password",
1870 .type = QEMU_OPT_STRING,
1871 .help = "password for CHAP authentication to target",
b189346e
DB
1872 },{
1873 .name = "password-secret",
1874 .type = QEMU_OPT_STRING,
1875 .help = "ID of the secret providing password for CHAP "
1876 "authentication to target",
4d454574
PB
1877 },{
1878 .name = "header-digest",
1879 .type = QEMU_OPT_STRING,
1880 .help = "HeaderDigest setting. "
1881 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1882 },{
1883 .name = "initiator-name",
1884 .type = QEMU_OPT_STRING,
1885 .help = "Initiator iqn name to use when connecting",
5dd7a535
PL
1886 },{
1887 .name = "timeout",
1888 .type = QEMU_OPT_NUMBER,
1889 .help = "Request timeout in seconds (default 0 = no timeout)",
4d454574
PB
1890 },
1891 { /* end of list */ }
1892 },
1893};
1894
c589b249
RS
1895static void iscsi_block_init(void)
1896{
1897 bdrv_register(&bdrv_iscsi);
4d454574 1898 qemu_add_opts(&qemu_iscsi_opts);
c589b249
RS
1899}
1900
1901block_init(iscsi_block_init);
This page took 0.587816 seconds and 4 git commands to generate.