]> git.proxmox.com Git - mirror_qemu.git/blame - block/iscsi.c
arm/smmuv3: Fix missing VMSD terminator
[mirror_qemu.git] / block / iscsi.c
CommitLineData
c589b249
RS
1/*
2 * QEMU Block driver for iSCSI images
3 *
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
aef172ff 5 * Copyright (c) 2012-2017 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
80c71a24 26#include "qemu/osdep.h"
c589b249
RS
27
28#include <poll.h>
efc6de0d 29#include <math.h>
f4dfa67f 30#include <arpa/inet.h>
1de7afc9
PB
31#include "qemu/config-file.h"
32#include "qemu/error-report.h"
b03c3805
PL
33#include "qemu/bitops.h"
34#include "qemu/bitmap.h"
737e150e 35#include "block/block_int.h"
609f45ea 36#include "block/qdict.h"
08e2c9f1 37#include "scsi/constants.h"
0a53f010 38#include "qemu/iov.h"
922a01a0 39#include "qemu/option.h"
cea25275 40#include "qemu/uuid.h"
e688df6b 41#include "qapi/error.h"
112ed241 42#include "qapi/qapi-commands-misc.h"
452fcdbc 43#include "qapi/qmp/qdict.h"
d49b6836 44#include "qapi/qmp/qstring.h"
b189346e 45#include "crypto/secret.h"
e5b5728c 46#include "scsi/utils.h"
ecc983a5 47#include "trace.h"
c589b249 48
e5b5728c
PB
49/* Conflict between scsi/utils.h and libiscsi! :( */
50#define SCSI_XFER_NONE ISCSI_XFER_NONE
c589b249
RS
51#include <iscsi/iscsi.h>
52#include <iscsi/scsi-lowlevel.h>
e5b5728c
PB
53#undef SCSI_XFER_NONE
54QEMU_BUILD_BUG_ON((int)SCSI_XFER_NONE != (int)ISCSI_XFER_NONE);
c589b249 55
98392453
RS
56#ifdef __linux__
57#include <scsi/sg.h>
98392453 58#endif
c589b249
RS
59
60typedef struct IscsiLun {
61 struct iscsi_context *iscsi;
80cf6257 62 AioContext *aio_context;
c589b249 63 int lun;
dbfff6d7 64 enum scsi_inquiry_peripheral_device_type type;
c589b249 65 int block_size;
c7b4a952 66 uint64_t num_blocks;
c9b9f682 67 int events;
5b5d34ec 68 QEMUTimer *nop_timer;
05b685fb 69 QEMUTimer *event_timer;
d045c466 70 QemuMutex mutex;
f18a7cbb
PL
71 struct scsi_inquiry_logical_block_provisioning lbp;
72 struct scsi_inquiry_block_limits bl;
cc9743c2 73 struct scsi_inquiry_device_designator *dd;
d4cd9615 74 unsigned char *zeroblock;
e1123a3b
PL
75 /* The allocmap tracks which clusters (pages) on the iSCSI target are
76 * allocated and which are not. In case a target returns zeros for
77 * unallocated pages (iscsilun->lprz) we can directly return zeros instead
78 * of reading zeros over the wire if a read request falls within an
79 * unallocated block. As there are 3 possible states we need 2 bitmaps to
80 * track. allocmap_valid keeps track if QEMU's information about a page is
81 * valid. allocmap tracks if a page is allocated or not. In case QEMU has no
82 * valid information about a page the corresponding allocmap entry should be
83 * switched to unallocated as well to force a new lookup of the allocation
84 * status as lookups are generally skipped if a page is suspect to be
85 * allocated. If a iSCSI target is opened with cache.direct = on the
86 * allocmap_valid does not exist turning all cached information invalid so
87 * that a fresh lookup is made for any page even if allocmap entry returns
88 * it's unallocated. */
89 unsigned long *allocmap;
90 unsigned long *allocmap_valid;
91 long allocmap_size;
ba059e7b 92 int cluster_size;
9281fe9e 93 bool use_16_for_rw;
43ae8fb1 94 bool write_protected;
0a386e48
PL
95 bool lbpme;
96 bool lbprz;
752ce451 97 bool dpofua;
0a386e48 98 bool has_write_same;
5dd7a535 99 bool request_timed_out;
c589b249
RS
100} IscsiLun;
101
54a5c1d5
PL
102typedef struct IscsiTask {
103 int status;
104 int complete;
105 int retries;
106 int do_retry;
107 struct scsi_task *task;
108 Coroutine *co;
80cf6257 109 IscsiLun *iscsilun;
efc6de0d 110 QEMUTimer retry_timer;
e01dd3da 111 int err_code;
e38bc234 112 char *err_str;
54a5c1d5
PL
113} IscsiTask;
114
c589b249 115typedef struct IscsiAIOCB {
7c84b1b8 116 BlockAIOCB common;
c589b249
RS
117 QEMUBH *bh;
118 IscsiLun *iscsilun;
119 struct scsi_task *task;
120 uint8_t *buf;
121 int status;
1dde716e
PL
122 int64_t sector_num;
123 int nb_sectors;
4bb17ab5 124 int ret;
98392453
RS
125#ifdef __linux__
126 sg_io_hdr_t *ioh;
127#endif
c589b249
RS
128} IscsiAIOCB;
129
5dd7a535
PL
130/* libiscsi uses time_t so its enough to process events every second */
131#define EVENT_INTERVAL 1000
5b5d34ec
PL
132#define NOP_INTERVAL 5000
133#define MAX_NOP_FAILURES 3
efc6de0d 134#define ISCSI_CMD_RETRIES ARRAY_SIZE(iscsi_retry_times)
59dd0a22 135static const unsigned iscsi_retry_times[] = {8, 32, 128, 512, 2048, 8192, 32768};
5b5d34ec 136
5d831be2 137/* this threshold is a trade-off knob to choose between
5917af81
PL
138 * the potential additional overhead of an extra GET_LBA_STATUS request
139 * vs. unnecessarily reading a lot of zero sectors over the wire.
140 * If a read request is greater or equal than ISCSI_CHECKALLOC_THRES
141 * sectors we check the allocation status of the area covered by the
142 * request first if the allocationmap indicates that the area might be
143 * unallocated. */
144#define ISCSI_CHECKALLOC_THRES 64
5b5d34ec 145
27cbd828 146static void
cfb3f506 147iscsi_bh_cb(void *p)
27cbd828
PB
148{
149 IscsiAIOCB *acb = p;
150
151 qemu_bh_delete(acb->bh);
152
4790b03d
PB
153 g_free(acb->buf);
154 acb->buf = NULL;
155
722d9333 156 acb->common.cb(acb->common.opaque, acb->status);
27cbd828 157
1bd075f2
PB
158 if (acb->task != NULL) {
159 scsi_free_scsi_task(acb->task);
160 acb->task = NULL;
161 }
162
8007429a 163 qemu_aio_unref(acb);
27cbd828
PB
164}
165
cfb3f506
PB
166static void
167iscsi_schedule_bh(IscsiAIOCB *acb)
27cbd828 168{
1bd075f2
PB
169 if (acb->bh) {
170 return;
171 }
80cf6257 172 acb->bh = aio_bh_new(acb->iscsilun->aio_context, iscsi_bh_cb, acb);
27cbd828 173 qemu_bh_schedule(acb->bh);
27cbd828
PB
174}
175
8b9dfe90
PL
176static void iscsi_co_generic_bh_cb(void *opaque)
177{
178 struct IscsiTask *iTask = opaque;
1919631e 179
fcd470d8 180 iTask->complete = 1;
1919631e 181 aio_co_wake(iTask->co);
8b9dfe90
PL
182}
183
efc6de0d
PL
184static void iscsi_retry_timer_expired(void *opaque)
185{
186 struct IscsiTask *iTask = opaque;
fcd470d8 187 iTask->complete = 1;
efc6de0d 188 if (iTask->co) {
2f47da5f 189 aio_co_wake(iTask->co);
efc6de0d
PL
190 }
191}
192
193static inline unsigned exp_random(double mean)
194{
195 return -mean * log((double)rand() / RAND_MAX);
196}
197
e01dd3da
FZ
198/* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST was introduced in
199 * libiscsi 1.10.0, together with other constants we need. Use it as
200 * a hint that we have to define them ourselves if needed, to keep the
201 * minimum required libiscsi version at 1.9.0. We use an ASCQ macro for
202 * the test because SCSI_STATUS_* is an enum.
203 *
204 * To guard against future changes where SCSI_SENSE_ASCQ_* also becomes
205 * an enum, check against the LIBISCSI_API_VERSION macro, which was
206 * introduced in 1.11.0. If it is present, there is no need to define
207 * anything.
208 */
209#if !defined(SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST) && \
210 !defined(LIBISCSI_API_VERSION)
211#define SCSI_STATUS_TASK_SET_FULL 0x28
212#define SCSI_STATUS_TIMEOUT 0x0f000002
213#define SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST 0x2600
214#define SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR 0x1a00
9049736e
PL
215#endif
216
583ec22e
RS
217#ifndef LIBISCSI_API_VERSION
218#define LIBISCSI_API_VERSION 20130701
219#endif
220
e01dd3da
FZ
221static int iscsi_translate_sense(struct scsi_sense *sense)
222{
28751358
FZ
223 return - scsi_sense_to_errno(sense->key,
224 (sense->ascq & 0xFF00) >> 8,
225 sense->ascq & 0xFF);
e01dd3da
FZ
226}
227
d045c466 228/* Called (via iscsi_service) with QemuMutex held. */
54a5c1d5
PL
229static void
230iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
231 void *command_data, void *opaque)
232{
233 struct IscsiTask *iTask = opaque;
234 struct scsi_task *task = command_data;
235
54a5c1d5
PL
236 iTask->status = status;
237 iTask->do_retry = 0;
238 iTask->task = task;
239
54a5c1d5 240 if (status != SCSI_STATUS_GOOD) {
efc6de0d
PL
241 if (iTask->retries++ < ISCSI_CMD_RETRIES) {
242 if (status == SCSI_STATUS_CHECK_CONDITION
243 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
244 error_report("iSCSI CheckCondition: %s",
245 iscsi_get_error(iscsi));
246 iTask->do_retry = 1;
247 goto out;
248 }
9049736e 249 if (status == SCSI_STATUS_BUSY ||
e01dd3da
FZ
250 status == SCSI_STATUS_TIMEOUT ||
251 status == SCSI_STATUS_TASK_SET_FULL) {
efc6de0d
PL
252 unsigned retry_time =
253 exp_random(iscsi_retry_times[iTask->retries - 1]);
e01dd3da 254 if (status == SCSI_STATUS_TIMEOUT) {
5dd7a535
PL
255 /* make sure the request is rescheduled AFTER the
256 * reconnect is initiated */
257 retry_time = EVENT_INTERVAL * 2;
258 iTask->iscsilun->request_timed_out = true;
259 }
260 error_report("iSCSI Busy/TaskSetFull/TimeOut"
261 " (retry #%u in %u ms): %s",
efc6de0d
PL
262 iTask->retries, retry_time,
263 iscsi_get_error(iscsi));
264 aio_timer_init(iTask->iscsilun->aio_context,
265 &iTask->retry_timer, QEMU_CLOCK_REALTIME,
266 SCALE_MS, iscsi_retry_timer_expired, iTask);
267 timer_mod(&iTask->retry_timer,
268 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + retry_time);
269 iTask->do_retry = 1;
270 return;
271 }
272 }
e01dd3da 273 iTask->err_code = iscsi_translate_sense(&task->sense);
e38bc234 274 iTask->err_str = g_strdup(iscsi_get_error(iscsi));
54a5c1d5
PL
275 }
276
277out:
278 if (iTask->co) {
fffb6e12
PB
279 aio_bh_schedule_oneshot(iTask->iscsilun->aio_context,
280 iscsi_co_generic_bh_cb, iTask);
fcd470d8
PL
281 } else {
282 iTask->complete = 1;
54a5c1d5
PL
283 }
284}
285
286static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
287{
288 *iTask = (struct IscsiTask) {
efc6de0d 289 .co = qemu_coroutine_self(),
efc6de0d 290 .iscsilun = iscsilun,
54a5c1d5
PL
291 };
292}
27cbd828 293
c589b249
RS
294static void
295iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
296 void *private_data)
297{
1bd075f2
PB
298 IscsiAIOCB *acb = private_data;
299
300 acb->status = -ECANCELED;
301 iscsi_schedule_bh(acb);
c589b249
RS
302}
303
304static void
7c84b1b8 305iscsi_aio_cancel(BlockAIOCB *blockacb)
c589b249
RS
306{
307 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
308 IscsiLun *iscsilun = acb->iscsilun;
309
1bd075f2
PB
310 if (acb->status != -EINPROGRESS) {
311 return;
312 }
313
b2090919 314 /* send a task mgmt call to the target to cancel the task on the target */
64e69e80 315 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
1bd075f2 316 iscsi_abort_task_cb, acb);
b2090919 317
c589b249
RS
318}
319
d7331bed 320static const AIOCBInfo iscsi_aiocb_info = {
c589b249 321 .aiocb_size = sizeof(IscsiAIOCB),
722d9333 322 .cancel_async = iscsi_aio_cancel,
c589b249
RS
323};
324
325
326static void iscsi_process_read(void *arg);
327static void iscsi_process_write(void *arg);
328
d045c466 329/* Called with QemuMutex held. */
c589b249
RS
330static void
331iscsi_set_events(IscsiLun *iscsilun)
332{
333 struct iscsi_context *iscsi = iscsilun->iscsi;
05b685fb 334 int ev = iscsi_which_events(iscsi);
c9b9f682 335
c9b9f682 336 if (ev != iscsilun->events) {
dca21ef2
FZ
337 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsi),
338 false,
05b685fb 339 (ev & POLLIN) ? iscsi_process_read : NULL,
80cf6257 340 (ev & POLLOUT) ? iscsi_process_write : NULL,
f6a51c84 341 NULL,
80cf6257 342 iscsilun);
05b685fb
PL
343 iscsilun->events = ev;
344 }
05b685fb 345}
c9b9f682 346
5dd7a535 347static void iscsi_timed_check_events(void *opaque)
05b685fb
PL
348{
349 IscsiLun *iscsilun = opaque;
5dd7a535
PL
350
351 /* check for timed out requests */
352 iscsi_service(iscsilun->iscsi, 0);
353
354 if (iscsilun->request_timed_out) {
355 iscsilun->request_timed_out = false;
356 iscsi_reconnect(iscsilun->iscsi);
357 }
358
359 /* newer versions of libiscsi may return zero events. Ensure we are able
360 * to return to service once this situation changes. */
05b685fb 361 iscsi_set_events(iscsilun);
5dd7a535
PL
362
363 timer_mod(iscsilun->event_timer,
364 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
c589b249
RS
365}
366
367static void
368iscsi_process_read(void *arg)
369{
370 IscsiLun *iscsilun = arg;
371 struct iscsi_context *iscsi = iscsilun->iscsi;
372
d045c466 373 qemu_mutex_lock(&iscsilun->mutex);
c589b249
RS
374 iscsi_service(iscsi, POLLIN);
375 iscsi_set_events(iscsilun);
d045c466 376 qemu_mutex_unlock(&iscsilun->mutex);
c589b249
RS
377}
378
379static void
380iscsi_process_write(void *arg)
381{
382 IscsiLun *iscsilun = arg;
383 struct iscsi_context *iscsi = iscsilun->iscsi;
384
d045c466 385 qemu_mutex_lock(&iscsilun->mutex);
c589b249
RS
386 iscsi_service(iscsi, POLLOUT);
387 iscsi_set_events(iscsilun);
d045c466 388 qemu_mutex_unlock(&iscsilun->mutex);
c589b249
RS
389}
390
0777b5dd
PL
391static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
392{
393 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
394}
395
c589b249
RS
396static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
397{
398 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
399}
400
94d047a3
EB
401static bool is_byte_request_lun_aligned(int64_t offset, int count,
402 IscsiLun *iscsilun)
91bea4e2 403{
94d047a3
EB
404 if (offset % iscsilun->block_size || count % iscsilun->block_size) {
405 error_report("iSCSI misaligned request: "
406 "iscsilun->block_size %u, offset %" PRIi64
407 ", count %d",
408 iscsilun->block_size, offset, count);
409 return false;
410 }
411 return true;
412}
413
414static bool is_sector_request_lun_aligned(int64_t sector_num, int nb_sectors,
415 IscsiLun *iscsilun)
416{
0ead9312 417 assert(nb_sectors <= BDRV_REQUEST_MAX_SECTORS);
94d047a3
EB
418 return is_byte_request_lun_aligned(sector_num << BDRV_SECTOR_BITS,
419 nb_sectors << BDRV_SECTOR_BITS,
420 iscsilun);
91bea4e2
PL
421}
422
e1123a3b 423static void iscsi_allocmap_free(IscsiLun *iscsilun)
a9fe4c95 424{
e1123a3b
PL
425 g_free(iscsilun->allocmap);
426 g_free(iscsilun->allocmap_valid);
427 iscsilun->allocmap = NULL;
428 iscsilun->allocmap_valid = NULL;
a9fe4c95
PL
429}
430
e1123a3b
PL
431
432static int iscsi_allocmap_init(IscsiLun *iscsilun, int open_flags)
b03c3805 433{
e1123a3b
PL
434 iscsi_allocmap_free(iscsilun);
435
ba059e7b 436 assert(iscsilun->cluster_size);
e1123a3b 437 iscsilun->allocmap_size =
ba059e7b
EB
438 DIV_ROUND_UP(iscsilun->num_blocks * iscsilun->block_size,
439 iscsilun->cluster_size);
e1123a3b
PL
440
441 iscsilun->allocmap = bitmap_try_new(iscsilun->allocmap_size);
442 if (!iscsilun->allocmap) {
443 return -ENOMEM;
b03c3805 444 }
e1123a3b
PL
445
446 if (open_flags & BDRV_O_NOCACHE) {
ba059e7b 447 /* when cache.direct = on all allocmap entries are
e1123a3b
PL
448 * treated as invalid to force a relookup of the block
449 * status on every read request */
450 return 0;
451 }
452
453 iscsilun->allocmap_valid = bitmap_try_new(iscsilun->allocmap_size);
454 if (!iscsilun->allocmap_valid) {
455 /* if we are under memory pressure free the allocmap as well */
456 iscsi_allocmap_free(iscsilun);
457 return -ENOMEM;
458 }
459
460 return 0;
b03c3805
PL
461}
462
e1123a3b 463static void
04a408fb
EB
464iscsi_allocmap_update(IscsiLun *iscsilun, int64_t offset,
465 int64_t bytes, bool allocated, bool valid)
b03c3805 466{
e1123a3b
PL
467 int64_t cl_num_expanded, nb_cls_expanded, cl_num_shrunk, nb_cls_shrunk;
468
469 if (iscsilun->allocmap == NULL) {
b03c3805
PL
470 return;
471 }
e1123a3b 472 /* expand to entirely contain all affected clusters */
04a408fb
EB
473 assert(iscsilun->cluster_size);
474 cl_num_expanded = offset / iscsilun->cluster_size;
475 nb_cls_expanded = DIV_ROUND_UP(offset + bytes,
476 iscsilun->cluster_size) - cl_num_expanded;
e1123a3b 477 /* shrink to touch only completely contained clusters */
04a408fb
EB
478 cl_num_shrunk = DIV_ROUND_UP(offset, iscsilun->cluster_size);
479 nb_cls_shrunk = (offset + bytes) / iscsilun->cluster_size - cl_num_shrunk;
e1123a3b
PL
480 if (allocated) {
481 bitmap_set(iscsilun->allocmap, cl_num_expanded, nb_cls_expanded);
482 } else {
1da45e0c
PL
483 if (nb_cls_shrunk > 0) {
484 bitmap_clear(iscsilun->allocmap, cl_num_shrunk, nb_cls_shrunk);
485 }
e1123a3b
PL
486 }
487
488 if (iscsilun->allocmap_valid == NULL) {
489 return;
490 }
491 if (valid) {
1da45e0c
PL
492 if (nb_cls_shrunk > 0) {
493 bitmap_set(iscsilun->allocmap_valid, cl_num_shrunk, nb_cls_shrunk);
494 }
e1123a3b
PL
495 } else {
496 bitmap_clear(iscsilun->allocmap_valid, cl_num_expanded,
497 nb_cls_expanded);
498 }
499}
500
501static void
04a408fb
EB
502iscsi_allocmap_set_allocated(IscsiLun *iscsilun, int64_t offset,
503 int64_t bytes)
e1123a3b 504{
04a408fb 505 iscsi_allocmap_update(iscsilun, offset, bytes, true, true);
e1123a3b
PL
506}
507
508static void
04a408fb
EB
509iscsi_allocmap_set_unallocated(IscsiLun *iscsilun, int64_t offset,
510 int64_t bytes)
e1123a3b
PL
511{
512 /* Note: if cache.direct=on the fifth argument to iscsi_allocmap_update
513 * is ignored, so this will in effect be an iscsi_allocmap_set_invalid.
514 */
04a408fb 515 iscsi_allocmap_update(iscsilun, offset, bytes, false, true);
e1123a3b
PL
516}
517
04a408fb
EB
518static void iscsi_allocmap_set_invalid(IscsiLun *iscsilun, int64_t offset,
519 int64_t bytes)
e1123a3b 520{
04a408fb 521 iscsi_allocmap_update(iscsilun, offset, bytes, false, false);
e1123a3b
PL
522}
523
524static void iscsi_allocmap_invalidate(IscsiLun *iscsilun)
525{
526 if (iscsilun->allocmap) {
527 bitmap_zero(iscsilun->allocmap, iscsilun->allocmap_size);
528 }
529 if (iscsilun->allocmap_valid) {
530 bitmap_zero(iscsilun->allocmap_valid, iscsilun->allocmap_size);
531 }
532}
533
534static inline bool
04a408fb
EB
535iscsi_allocmap_is_allocated(IscsiLun *iscsilun, int64_t offset,
536 int64_t bytes)
e1123a3b
PL
537{
538 unsigned long size;
539 if (iscsilun->allocmap == NULL) {
540 return true;
b03c3805 541 }
ba059e7b 542 assert(iscsilun->cluster_size);
04a408fb 543 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size);
e1123a3b 544 return !(find_next_bit(iscsilun->allocmap, size,
04a408fb 545 offset / iscsilun->cluster_size) == size);
e1123a3b
PL
546}
547
548static inline bool iscsi_allocmap_is_valid(IscsiLun *iscsilun,
04a408fb 549 int64_t offset, int64_t bytes)
e1123a3b
PL
550{
551 unsigned long size;
552 if (iscsilun->allocmap_valid == NULL) {
553 return false;
554 }
ba059e7b 555 assert(iscsilun->cluster_size);
04a408fb 556 size = DIV_ROUND_UP(offset + bytes, iscsilun->cluster_size);
e1123a3b 557 return (find_next_zero_bit(iscsilun->allocmap_valid, size,
04a408fb 558 offset / iscsilun->cluster_size) == size);
b03c3805
PL
559}
560
66e75c03
FZ
561static void coroutine_fn iscsi_co_wait_for_task(IscsiTask *iTask,
562 IscsiLun *iscsilun)
563{
564 while (!iTask->complete) {
565 iscsi_set_events(iscsilun);
566 qemu_mutex_unlock(&iscsilun->mutex);
567 qemu_coroutine_yield();
568 qemu_mutex_lock(&iscsilun->mutex);
569 }
570}
571
9f0eb9e1 572static int coroutine_fn
e18a58b4
EB
573iscsi_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
574 QEMUIOVector *iov, int flags)
c589b249 575{
063c3378
PL
576 IscsiLun *iscsilun = bs->opaque;
577 struct IscsiTask iTask;
f4dfa67f 578 uint64_t lba;
063c3378 579 uint32_t num_sectors;
4df863f3 580 bool fua = flags & BDRV_REQ_FUA;
d045c466 581 int r = 0;
c589b249 582
4df863f3
EB
583 if (fua) {
584 assert(iscsilun->dpofua);
585 }
94d047a3 586 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
063c3378
PL
587 return -EINVAL;
588 }
7371d56f 589
6bd01f14
EB
590 if (bs->bl.max_transfer) {
591 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
dc9e7163
PL
592 }
593
063c3378
PL
594 lba = sector_qemu2lun(sector_num, iscsilun);
595 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
063c3378 596 iscsi_co_init_iscsitask(iscsilun, &iTask);
d045c466 597 qemu_mutex_lock(&iscsilun->mutex);
063c3378 598retry:
9281fe9e 599 if (iscsilun->use_16_for_rw) {
583ec22e
RS
600#if LIBISCSI_API_VERSION >= (20160603)
601 iTask.task = iscsi_write16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
602 NULL, num_sectors * iscsilun->block_size,
603 iscsilun->block_size, 0, 0, fua, 0, 0,
604 iscsi_co_generic_cb, &iTask,
605 (struct scsi_iovec *)iov->iov, iov->niov);
606 } else {
607 iTask.task = iscsi_write10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
608 NULL, num_sectors * iscsilun->block_size,
609 iscsilun->block_size, 0, 0, fua, 0, 0,
610 iscsi_co_generic_cb, &iTask,
611 (struct scsi_iovec *)iov->iov, iov->niov);
612 }
613#else
9281fe9e 614 iTask.task = iscsi_write16_task(iscsilun->iscsi, iscsilun->lun, lba,
8c215a9f 615 NULL, num_sectors * iscsilun->block_size,
73b5394e 616 iscsilun->block_size, 0, 0, fua, 0, 0,
9281fe9e
PL
617 iscsi_co_generic_cb, &iTask);
618 } else {
619 iTask.task = iscsi_write10_task(iscsilun->iscsi, iscsilun->lun, lba,
8c215a9f 620 NULL, num_sectors * iscsilun->block_size,
73b5394e 621 iscsilun->block_size, 0, 0, fua, 0, 0,
9281fe9e
PL
622 iscsi_co_generic_cb, &iTask);
623 }
583ec22e 624#endif
063c3378 625 if (iTask.task == NULL) {
f6eb0b31 626 qemu_mutex_unlock(&iscsilun->mutex);
92397116 627 return -ENOMEM;
f4dfa67f 628 }
583ec22e 629#if LIBISCSI_API_VERSION < (20160603)
063c3378
PL
630 scsi_task_set_iov_out(iTask.task, (struct scsi_iovec *) iov->iov,
631 iov->niov);
583ec22e 632#endif
66e75c03 633 iscsi_co_wait_for_task(&iTask, iscsilun);
c589b249 634
063c3378
PL
635 if (iTask.task != NULL) {
636 scsi_free_scsi_task(iTask.task);
637 iTask.task = NULL;
91bea4e2
PL
638 }
639
063c3378 640 if (iTask.do_retry) {
837c3901 641 iTask.complete = 0;
063c3378 642 goto retry;
1dde716e
PL
643 }
644
063c3378 645 if (iTask.status != SCSI_STATUS_GOOD) {
04a408fb
EB
646 iscsi_allocmap_set_invalid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
647 nb_sectors * BDRV_SECTOR_SIZE);
e38bc234
PL
648 error_report("iSCSI WRITE10/16 failed at lba %" PRIu64 ": %s", lba,
649 iTask.err_str);
d045c466
PB
650 r = iTask.err_code;
651 goto out_unlock;
c589b249
RS
652 }
653
04a408fb
EB
654 iscsi_allocmap_set_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
655 nb_sectors * BDRV_SECTOR_SIZE);
b03c3805 656
d045c466
PB
657out_unlock:
658 qemu_mutex_unlock(&iscsilun->mutex);
e38bc234 659 g_free(iTask.err_str);
d045c466 660 return r;
c589b249
RS
661}
662
b03c3805 663
b03c3805 664
92809c36
EB
665static int coroutine_fn iscsi_co_block_status(BlockDriverState *bs,
666 bool want_zero, int64_t offset,
667 int64_t bytes, int64_t *pnum,
668 int64_t *map,
669 BlockDriverState **file)
b03c3805
PL
670{
671 IscsiLun *iscsilun = bs->opaque;
672 struct scsi_get_lba_status *lbas = NULL;
673 struct scsi_lba_status_descriptor *lbasd = NULL;
674 struct IscsiTask iTask;
e38bc234 675 uint64_t lba;
92809c36 676 int ret;
b03c3805 677
79f9c75e
PL
678 iscsi_co_init_iscsitask(iscsilun, &iTask);
679
92809c36 680 assert(QEMU_IS_ALIGNED(offset | bytes, iscsilun->block_size));
b03c3805
PL
681
682 /* default to all sectors allocated */
92809c36
EB
683 ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
684 if (map) {
685 *map = offset;
686 }
687 *pnum = bytes;
b03c3805
PL
688
689 /* LUN does not support logical block provisioning */
0a386e48 690 if (!iscsilun->lbpme) {
b03c3805
PL
691 goto out;
692 }
693
92809c36 694 lba = offset / iscsilun->block_size;
e38bc234 695
d045c466 696 qemu_mutex_lock(&iscsilun->mutex);
b03c3805
PL
697retry:
698 if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
e38bc234 699 lba, 8 + 16, iscsi_co_generic_cb,
b03c3805
PL
700 &iTask) == NULL) {
701 ret = -ENOMEM;
d045c466 702 goto out_unlock;
b03c3805 703 }
66e75c03 704 iscsi_co_wait_for_task(&iTask, iscsilun);
b03c3805
PL
705
706 if (iTask.do_retry) {
707 if (iTask.task != NULL) {
708 scsi_free_scsi_task(iTask.task);
709 iTask.task = NULL;
710 }
711 iTask.complete = 0;
712 goto retry;
713 }
714
715 if (iTask.status != SCSI_STATUS_GOOD) {
716 /* in case the get_lba_status_callout fails (i.e.
717 * because the device is busy or the cmd is not
718 * supported) we pretend all blocks are allocated
719 * for backwards compatibility */
e38bc234
PL
720 error_report("iSCSI GET_LBA_STATUS failed at lba %" PRIu64 ": %s",
721 lba, iTask.err_str);
d045c466 722 goto out_unlock;
b03c3805
PL
723 }
724
725 lbas = scsi_datain_unmarshall(iTask.task);
726 if (lbas == NULL) {
727 ret = -EIO;
d045c466 728 goto out_unlock;
b03c3805
PL
729 }
730
731 lbasd = &lbas->descriptors[0];
732
92809c36 733 if (lba != lbasd->lba) {
b03c3805 734 ret = -EIO;
d045c466 735 goto out_unlock;
b03c3805
PL
736 }
737
8ee1cef4 738 *pnum = (int64_t) lbasd->num_blocks * iscsilun->block_size;
b03c3805
PL
739
740 if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
741 lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
742 ret &= ~BDRV_BLOCK_DATA;
743 if (iscsilun->lbprz) {
744 ret |= BDRV_BLOCK_ZERO;
745 }
746 }
747
748 if (ret & BDRV_BLOCK_ZERO) {
92809c36 749 iscsi_allocmap_set_unallocated(iscsilun, offset, *pnum);
b03c3805 750 } else {
92809c36 751 iscsi_allocmap_set_allocated(iscsilun, offset, *pnum);
b03c3805
PL
752 }
753
92809c36
EB
754 if (*pnum > bytes) {
755 *pnum = bytes;
b03c3805 756 }
d045c466
PB
757out_unlock:
758 qemu_mutex_unlock(&iscsilun->mutex);
e38bc234 759 g_free(iTask.err_str);
b03c3805
PL
760out:
761 if (iTask.task != NULL) {
762 scsi_free_scsi_task(iTask.task);
763 }
92809c36 764 if (ret > 0 && ret & BDRV_BLOCK_OFFSET_VALID && file) {
3399833f
FZ
765 *file = bs;
766 }
b03c3805
PL
767 return ret;
768}
769
063c3378
PL
770static int coroutine_fn iscsi_co_readv(BlockDriverState *bs,
771 int64_t sector_num, int nb_sectors,
772 QEMUIOVector *iov)
c589b249 773{
063c3378
PL
774 IscsiLun *iscsilun = bs->opaque;
775 struct IscsiTask iTask;
1dde716e
PL
776 uint64_t lba;
777 uint32_t num_sectors;
e38bc234 778 int r = 0;
c589b249 779
94d047a3 780 if (!is_sector_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
063c3378 781 return -EINVAL;
f4dfa67f 782 }
f4dfa67f 783
6bd01f14
EB
784 if (bs->bl.max_transfer) {
785 assert(nb_sectors << BDRV_SECTOR_BITS <= bs->bl.max_transfer);
dc9e7163
PL
786 }
787
e1123a3b
PL
788 /* if cache.direct is off and we have a valid entry in our allocation map
789 * we can skip checking the block status and directly return zeroes if
790 * the request falls within an unallocated area */
04a408fb
EB
791 if (iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
792 nb_sectors * BDRV_SECTOR_SIZE) &&
793 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
794 nb_sectors * BDRV_SECTOR_SIZE)) {
e1123a3b
PL
795 qemu_iovec_memset(iov, 0, 0x00, iov->size);
796 return 0;
797 }
798
799 if (nb_sectors >= ISCSI_CHECKALLOC_THRES &&
04a408fb
EB
800 !iscsi_allocmap_is_valid(iscsilun, sector_num * BDRV_SECTOR_SIZE,
801 nb_sectors * BDRV_SECTOR_SIZE) &&
802 !iscsi_allocmap_is_allocated(iscsilun, sector_num * BDRV_SECTOR_SIZE,
803 nb_sectors * BDRV_SECTOR_SIZE)) {
92809c36 804 int64_t pnum;
e1123a3b
PL
805 /* check the block status from the beginning of the cluster
806 * containing the start sector */
92809c36
EB
807 int64_t head;
808 int ret;
809
810 assert(iscsilun->cluster_size);
811 head = (sector_num * BDRV_SECTOR_SIZE) % iscsilun->cluster_size;
812 ret = iscsi_co_block_status(bs, true,
813 sector_num * BDRV_SECTOR_SIZE - head,
814 BDRV_REQUEST_MAX_BYTES, &pnum, NULL, NULL);
b03c3805
PL
815 if (ret < 0) {
816 return ret;
817 }
e1123a3b 818 /* if the whole request falls into an unallocated area we can avoid
ba059e7b 819 * reading and directly return zeroes instead */
e1123a3b 820 if (ret & BDRV_BLOCK_ZERO &&
92809c36 821 pnum >= nb_sectors * BDRV_SECTOR_SIZE + head) {
b03c3805
PL
822 qemu_iovec_memset(iov, 0, 0x00, iov->size);
823 return 0;
824 }
825 }
b03c3805 826
063c3378
PL
827 lba = sector_qemu2lun(sector_num, iscsilun);
828 num_sectors = sector_qemu2lun(nb_sectors, iscsilun);
f4dfa67f 829
063c3378 830 iscsi_co_init_iscsitask(iscsilun, &iTask);
d045c466 831 qemu_mutex_lock(&iscsilun->mutex);
063c3378 832retry:
9281fe9e 833 if (iscsilun->use_16_for_rw) {
583ec22e
RS
834#if LIBISCSI_API_VERSION >= (20160603)
835 iTask.task = iscsi_read16_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
836 num_sectors * iscsilun->block_size,
837 iscsilun->block_size, 0, 0, 0, 0, 0,
838 iscsi_co_generic_cb, &iTask,
839 (struct scsi_iovec *)iov->iov, iov->niov);
840 } else {
841 iTask.task = iscsi_read10_iov_task(iscsilun->iscsi, iscsilun->lun, lba,
842 num_sectors * iscsilun->block_size,
843 iscsilun->block_size,
844 0, 0, 0, 0, 0,
845 iscsi_co_generic_cb, &iTask,
846 (struct scsi_iovec *)iov->iov, iov->niov);
847 }
848#else
063c3378
PL
849 iTask.task = iscsi_read16_task(iscsilun->iscsi, iscsilun->lun, lba,
850 num_sectors * iscsilun->block_size,
851 iscsilun->block_size, 0, 0, 0, 0, 0,
852 iscsi_co_generic_cb, &iTask);
9281fe9e 853 } else {
063c3378
PL
854 iTask.task = iscsi_read10_task(iscsilun->iscsi, iscsilun->lun, lba,
855 num_sectors * iscsilun->block_size,
219c2521 856 iscsilun->block_size,
219c2521 857 0, 0, 0, 0, 0,
063c3378 858 iscsi_co_generic_cb, &iTask);
f4dfa67f 859 }
583ec22e 860#endif
063c3378 861 if (iTask.task == NULL) {
f6eb0b31 862 qemu_mutex_unlock(&iscsilun->mutex);
92397116 863 return -ENOMEM;
c589b249 864 }
583ec22e 865#if LIBISCSI_API_VERSION < (20160603)
063c3378 866 scsi_task_set_iov_in(iTask.task, (struct scsi_iovec *) iov->iov, iov->niov);
583ec22e 867#endif
c589b249 868
66e75c03 869 iscsi_co_wait_for_task(&iTask, iscsilun);
063c3378
PL
870 if (iTask.task != NULL) {
871 scsi_free_scsi_task(iTask.task);
872 iTask.task = NULL;
c589b249
RS
873 }
874
063c3378 875 if (iTask.do_retry) {
837c3901 876 iTask.complete = 0;
063c3378 877 goto retry;
c589b249
RS
878 }
879
063c3378 880 if (iTask.status != SCSI_STATUS_GOOD) {
e38bc234
PL
881 error_report("iSCSI READ10/16 failed at lba %" PRIu64 ": %s",
882 lba, iTask.err_str);
883 r = iTask.err_code;
1dde716e
PL
884 }
885
e38bc234
PL
886 qemu_mutex_unlock(&iscsilun->mutex);
887 g_free(iTask.err_str);
888 return r;
1dde716e
PL
889}
890
063c3378 891static int coroutine_fn iscsi_co_flush(BlockDriverState *bs)
1dde716e
PL
892{
893 IscsiLun *iscsilun = bs->opaque;
063c3378 894 struct IscsiTask iTask;
e38bc234 895 int r = 0;
1dde716e 896
73b5394e 897 iscsi_co_init_iscsitask(iscsilun, &iTask);
d045c466 898 qemu_mutex_lock(&iscsilun->mutex);
063c3378
PL
899retry:
900 if (iscsi_synchronizecache10_task(iscsilun->iscsi, iscsilun->lun, 0, 0, 0,
901 0, iscsi_co_generic_cb, &iTask) == NULL) {
f6eb0b31 902 qemu_mutex_unlock(&iscsilun->mutex);
92397116 903 return -ENOMEM;
063c3378 904 }
1dde716e 905
66e75c03 906 iscsi_co_wait_for_task(&iTask, iscsilun);
1dde716e 907
063c3378
PL
908 if (iTask.task != NULL) {
909 scsi_free_scsi_task(iTask.task);
910 iTask.task = NULL;
c589b249
RS
911 }
912
063c3378 913 if (iTask.do_retry) {
837c3901 914 iTask.complete = 0;
063c3378
PL
915 goto retry;
916 }
c589b249 917
063c3378 918 if (iTask.status != SCSI_STATUS_GOOD) {
e38bc234
PL
919 error_report("iSCSI SYNCHRONIZECACHE10 failed: %s", iTask.err_str);
920 r = iTask.err_code;
063c3378
PL
921 }
922
e38bc234
PL
923 qemu_mutex_unlock(&iscsilun->mutex);
924 g_free(iTask.err_str);
925 return r;
c589b249
RS
926}
927
98392453 928#ifdef __linux__
d045c466 929/* Called (via iscsi_service) with QemuMutex held. */
98392453
RS
930static void
931iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
932 void *command_data, void *opaque)
933{
934 IscsiAIOCB *acb = opaque;
935
0a53f010
RS
936 g_free(acb->buf);
937 acb->buf = NULL;
938
98392453
RS
939 acb->status = 0;
940 if (status < 0) {
941 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
942 iscsi_get_error(iscsi));
e01dd3da 943 acb->status = iscsi_translate_sense(&acb->task->sense);
98392453
RS
944 }
945
946 acb->ioh->driver_status = 0;
947 acb->ioh->host_status = 0;
948 acb->ioh->resid = 0;
644c6869 949 acb->ioh->status = status;
98392453
RS
950
951#define SG_ERR_DRIVER_SENSE 0x08
952
953 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
954 int ss;
955
956 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
957
958 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
959 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
960 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
961 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
962 }
963
cfb3f506 964 iscsi_schedule_bh(acb);
98392453
RS
965}
966
4bb17ab5
FZ
967static void iscsi_ioctl_bh_completion(void *opaque)
968{
969 IscsiAIOCB *acb = opaque;
970
971 qemu_bh_delete(acb->bh);
972 acb->common.cb(acb->common.opaque, acb->ret);
973 qemu_aio_unref(acb);
974}
975
976static void iscsi_ioctl_handle_emulated(IscsiAIOCB *acb, int req, void *buf)
977{
978 BlockDriverState *bs = acb->common.bs;
979 IscsiLun *iscsilun = bs->opaque;
980 int ret = 0;
981
982 switch (req) {
983 case SG_GET_VERSION_NUM:
984 *(int *)buf = 30000;
985 break;
986 case SG_GET_SCSI_ID:
987 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
988 break;
989 default:
990 ret = -EINVAL;
991 }
992 assert(!acb->bh);
993 acb->bh = aio_bh_new(bdrv_get_aio_context(bs),
994 iscsi_ioctl_bh_completion, acb);
995 acb->ret = ret;
996 qemu_bh_schedule(acb->bh);
997}
998
7c84b1b8 999static BlockAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
98392453 1000 unsigned long int req, void *buf,
097310b5 1001 BlockCompletionFunc *cb, void *opaque)
98392453
RS
1002{
1003 IscsiLun *iscsilun = bs->opaque;
1004 struct iscsi_context *iscsi = iscsilun->iscsi;
1005 struct iscsi_data data;
1006 IscsiAIOCB *acb;
1007
d7331bed 1008 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
98392453
RS
1009
1010 acb->iscsilun = iscsilun;
1bd075f2
PB
1011 acb->bh = NULL;
1012 acb->status = -EINPROGRESS;
98392453
RS
1013 acb->buf = NULL;
1014 acb->ioh = buf;
1015
4bb17ab5
FZ
1016 if (req != SG_IO) {
1017 iscsi_ioctl_handle_emulated(acb, req, buf);
1018 return &acb->common;
1019 }
1020
a6b3167f
PL
1021 if (acb->ioh->cmd_len > SCSI_CDB_MAX_SIZE) {
1022 error_report("iSCSI: ioctl error CDB exceeds max size (%d > %d)",
1023 acb->ioh->cmd_len, SCSI_CDB_MAX_SIZE);
1024 qemu_aio_unref(acb);
1025 return NULL;
1026 }
1027
98392453
RS
1028 acb->task = malloc(sizeof(struct scsi_task));
1029 if (acb->task == NULL) {
1030 error_report("iSCSI: Failed to allocate task for scsi command. %s",
1031 iscsi_get_error(iscsi));
8007429a 1032 qemu_aio_unref(acb);
98392453
RS
1033 return NULL;
1034 }
1035 memset(acb->task, 0, sizeof(struct scsi_task));
1036
1037 switch (acb->ioh->dxfer_direction) {
1038 case SG_DXFER_TO_DEV:
1039 acb->task->xfer_dir = SCSI_XFER_WRITE;
1040 break;
1041 case SG_DXFER_FROM_DEV:
1042 acb->task->xfer_dir = SCSI_XFER_READ;
1043 break;
1044 default:
1045 acb->task->xfer_dir = SCSI_XFER_NONE;
1046 break;
1047 }
1048
1049 acb->task->cdb_size = acb->ioh->cmd_len;
1050 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
1051 acb->task->expxferlen = acb->ioh->dxfer_len;
1052
0a53f010 1053 data.size = 0;
d045c466 1054 qemu_mutex_lock(&iscsilun->mutex);
98392453 1055 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
0a53f010
RS
1056 if (acb->ioh->iovec_count == 0) {
1057 data.data = acb->ioh->dxferp;
1058 data.size = acb->ioh->dxfer_len;
1059 } else {
0a53f010
RS
1060 scsi_task_set_iov_out(acb->task,
1061 (struct scsi_iovec *) acb->ioh->dxferp,
1062 acb->ioh->iovec_count);
0a53f010 1063 }
98392453 1064 }
0a53f010 1065
98392453
RS
1066 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
1067 iscsi_aio_ioctl_cb,
0a53f010 1068 (data.size > 0) ? &data : NULL,
98392453 1069 acb) != 0) {
d045c466 1070 qemu_mutex_unlock(&iscsilun->mutex);
98392453 1071 scsi_free_scsi_task(acb->task);
8007429a 1072 qemu_aio_unref(acb);
98392453
RS
1073 return NULL;
1074 }
1075
1076 /* tell libiscsi to read straight into the buffer we got from ioctl */
1077 if (acb->task->xfer_dir == SCSI_XFER_READ) {
0a53f010
RS
1078 if (acb->ioh->iovec_count == 0) {
1079 scsi_task_add_data_in_buffer(acb->task,
1080 acb->ioh->dxfer_len,
1081 acb->ioh->dxferp);
1082 } else {
0a53f010
RS
1083 scsi_task_set_iov_in(acb->task,
1084 (struct scsi_iovec *) acb->ioh->dxferp,
1085 acb->ioh->iovec_count);
0a53f010 1086 }
98392453
RS
1087 }
1088
1089 iscsi_set_events(iscsilun);
d045c466 1090 qemu_mutex_unlock(&iscsilun->mutex);
98392453
RS
1091
1092 return &acb->common;
1093}
1094
98392453
RS
1095#endif
1096
c589b249
RS
1097static int64_t
1098iscsi_getlength(BlockDriverState *bs)
1099{
1100 IscsiLun *iscsilun = bs->opaque;
1101 int64_t len;
1102
1103 len = iscsilun->num_blocks;
1104 len *= iscsilun->block_size;
1105
1106 return len;
1107}
1108
65f3e339 1109static int
f5a5ca79 1110coroutine_fn iscsi_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
65f3e339
PL
1111{
1112 IscsiLun *iscsilun = bs->opaque;
1113 struct IscsiTask iTask;
1114 struct unmap_list list;
d045c466 1115 int r = 0;
65f3e339 1116
f5a5ca79 1117 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) {
49228d1e
EB
1118 return -ENOTSUP;
1119 }
65f3e339
PL
1120
1121 if (!iscsilun->lbp.lbpu) {
1122 /* UNMAP is not supported by the target */
1123 return 0;
1124 }
1125
97c7e85c 1126 list.lba = offset / iscsilun->block_size;
f5a5ca79 1127 list.num = bytes / iscsilun->block_size;
65f3e339 1128
01a6a238 1129 iscsi_co_init_iscsitask(iscsilun, &iTask);
d045c466 1130 qemu_mutex_lock(&iscsilun->mutex);
65f3e339 1131retry:
01a6a238 1132 if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
97c7e85c 1133 iscsi_co_generic_cb, &iTask) == NULL) {
d045c466
PB
1134 r = -ENOMEM;
1135 goto out_unlock;
01a6a238 1136 }
65f3e339 1137
66e75c03 1138 iscsi_co_wait_for_task(&iTask, iscsilun);
65f3e339 1139
01a6a238
PL
1140 if (iTask.task != NULL) {
1141 scsi_free_scsi_task(iTask.task);
1142 iTask.task = NULL;
1143 }
65f3e339 1144
01a6a238 1145 if (iTask.do_retry) {
837c3901 1146 iTask.complete = 0;
01a6a238
PL
1147 goto retry;
1148 }
65f3e339 1149
04a408fb 1150 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
aef172ff 1151
01a6a238
PL
1152 if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
1153 /* the target might fail with a check condition if it
1154 is not happy with the alignment of the UNMAP request
1155 we silently fail in this case */
d045c466 1156 goto out_unlock;
01a6a238 1157 }
65f3e339 1158
01a6a238 1159 if (iTask.status != SCSI_STATUS_GOOD) {
e38bc234
PL
1160 error_report("iSCSI UNMAP failed at lba %" PRIu64 ": %s",
1161 list.lba, iTask.err_str);
d045c466
PB
1162 r = iTask.err_code;
1163 goto out_unlock;
65f3e339
PL
1164 }
1165
d045c466
PB
1166out_unlock:
1167 qemu_mutex_unlock(&iscsilun->mutex);
e38bc234 1168 g_free(iTask.err_str);
d045c466 1169 return r;
65f3e339
PL
1170}
1171
d4cd9615 1172static int
94d047a3 1173coroutine_fn iscsi_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
f5a5ca79 1174 int bytes, BdrvRequestFlags flags)
d4cd9615
PL
1175{
1176 IscsiLun *iscsilun = bs->opaque;
1177 struct IscsiTask iTask;
1178 uint64_t lba;
1179 uint32_t nb_blocks;
9281fe9e 1180 bool use_16_for_ws = iscsilun->use_16_for_rw;
d045c466 1181 int r = 0;
d4cd9615 1182
f5a5ca79 1183 if (!is_byte_request_lun_aligned(offset, bytes, iscsilun)) {
94d047a3 1184 return -ENOTSUP;
d4cd9615
PL
1185 }
1186
9281fe9e
PL
1187 if (flags & BDRV_REQ_MAY_UNMAP) {
1188 if (!use_16_for_ws && !iscsilun->lbp.lbpws10) {
1189 /* WRITESAME10 with UNMAP is unsupported try WRITESAME16 */
1190 use_16_for_ws = true;
1191 }
1192 if (use_16_for_ws && !iscsilun->lbp.lbpws) {
1193 /* WRITESAME16 with UNMAP is not supported by the target,
1194 * fall back and try WRITESAME10/16 without UNMAP */
1195 flags &= ~BDRV_REQ_MAY_UNMAP;
1196 use_16_for_ws = iscsilun->use_16_for_rw;
1197 }
fa6252b0
PB
1198 }
1199
dbe5c58f 1200 if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
9281fe9e 1201 /* WRITESAME without UNMAP is not supported by the target */
d4cd9615
PL
1202 return -ENOTSUP;
1203 }
1204
94d047a3 1205 lba = offset / iscsilun->block_size;
f5a5ca79 1206 nb_blocks = bytes / iscsilun->block_size;
d4cd9615
PL
1207
1208 if (iscsilun->zeroblock == NULL) {
4d5a3f88
KW
1209 iscsilun->zeroblock = g_try_malloc0(iscsilun->block_size);
1210 if (iscsilun->zeroblock == NULL) {
1211 return -ENOMEM;
1212 }
d4cd9615
PL
1213 }
1214
d045c466 1215 qemu_mutex_lock(&iscsilun->mutex);
d4cd9615
PL
1216 iscsi_co_init_iscsitask(iscsilun, &iTask);
1217retry:
9281fe9e
PL
1218 if (use_16_for_ws) {
1219 iTask.task = iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1220 iscsilun->zeroblock, iscsilun->block_size,
1221 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1222 0, 0, iscsi_co_generic_cb, &iTask);
1223 } else {
1224 iTask.task = iscsi_writesame10_task(iscsilun->iscsi, iscsilun->lun, lba,
1225 iscsilun->zeroblock, iscsilun->block_size,
1226 nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1227 0, 0, iscsi_co_generic_cb, &iTask);
1228 }
1229 if (iTask.task == NULL) {
f6eb0b31 1230 qemu_mutex_unlock(&iscsilun->mutex);
92397116 1231 return -ENOMEM;
d4cd9615
PL
1232 }
1233
66e75c03 1234 iscsi_co_wait_for_task(&iTask, iscsilun);
d4cd9615 1235
d9738fd2
PL
1236 if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1237 iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
27898a5d
PB
1238 (iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE ||
1239 iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB)) {
d9738fd2
PL
1240 /* WRITE SAME is not supported by the target */
1241 iscsilun->has_write_same = false;
1242 scsi_free_scsi_task(iTask.task);
d045c466
PB
1243 r = -ENOTSUP;
1244 goto out_unlock;
d9738fd2
PL
1245 }
1246
d4cd9615
PL
1247 if (iTask.task != NULL) {
1248 scsi_free_scsi_task(iTask.task);
1249 iTask.task = NULL;
1250 }
1251
1252 if (iTask.do_retry) {
837c3901 1253 iTask.complete = 0;
d4cd9615
PL
1254 goto retry;
1255 }
1256
1257 if (iTask.status != SCSI_STATUS_GOOD) {
04a408fb 1258 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
e38bc234
PL
1259 error_report("iSCSI WRITESAME10/16 failed at lba %" PRIu64 ": %s",
1260 lba, iTask.err_str);
d045c466
PB
1261 r = iTask.err_code;
1262 goto out_unlock;
d4cd9615
PL
1263 }
1264
b03c3805 1265 if (flags & BDRV_REQ_MAY_UNMAP) {
04a408fb 1266 iscsi_allocmap_set_invalid(iscsilun, offset, bytes);
b03c3805 1267 } else {
04a408fb 1268 iscsi_allocmap_set_allocated(iscsilun, offset, bytes);
b03c3805
PL
1269 }
1270
d045c466
PB
1271out_unlock:
1272 qemu_mutex_unlock(&iscsilun->mutex);
e38bc234 1273 g_free(iTask.err_str);
d045c466 1274 return r;
d4cd9615
PL
1275}
1276
43171420 1277static void apply_chap(struct iscsi_context *iscsi, QemuOpts *opts,
f2917853 1278 Error **errp)
f9dadc98 1279{
f9dadc98
RS
1280 const char *user = NULL;
1281 const char *password = NULL;
b189346e
DB
1282 const char *secretid;
1283 char *secret = NULL;
f9dadc98 1284
f9dadc98
RS
1285 user = qemu_opt_get(opts, "user");
1286 if (!user) {
f2917853 1287 return;
f9dadc98
RS
1288 }
1289
b189346e 1290 secretid = qemu_opt_get(opts, "password-secret");
f9dadc98 1291 password = qemu_opt_get(opts, "password");
b189346e
DB
1292 if (secretid && password) {
1293 error_setg(errp, "'password' and 'password-secret' properties are "
1294 "mutually exclusive");
1295 return;
1296 }
1297 if (secretid) {
1298 secret = qcrypto_secret_lookup_as_utf8(secretid, errp);
1299 if (!secret) {
1300 return;
1301 }
1302 password = secret;
1303 } else if (!password) {
f2917853
PB
1304 error_setg(errp, "CHAP username specified but no password was given");
1305 return;
f9dadc98
RS
1306 }
1307
1308 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
f2917853 1309 error_setg(errp, "Failed to set initiator username and password");
f9dadc98 1310 }
b189346e
DB
1311
1312 g_free(secret);
f9dadc98
RS
1313}
1314
81aa2a0f 1315static void apply_header_digest(struct iscsi_context *iscsi, QemuOpts *opts,
f2917853 1316 Error **errp)
f9dadc98 1317{
f9dadc98
RS
1318 const char *digest = NULL;
1319
f9dadc98
RS
1320 digest = qemu_opt_get(opts, "header-digest");
1321 if (!digest) {
81aa2a0f 1322 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
31eb1202 1323 } else if (!strcmp(digest, "crc32c")) {
f9dadc98 1324 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
31eb1202 1325 } else if (!strcmp(digest, "none")) {
f9dadc98 1326 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
31eb1202 1327 } else if (!strcmp(digest, "crc32c-none")) {
f9dadc98 1328 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
31eb1202 1329 } else if (!strcmp(digest, "none-crc32c")) {
f9dadc98
RS
1330 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1331 } else {
f2917853 1332 error_setg(errp, "Invalid header-digest setting : %s", digest);
f9dadc98
RS
1333 }
1334}
1335
d4e79929 1336static char *get_initiator_name(QemuOpts *opts)
f9dadc98 1337{
5accc840
PB
1338 const char *name;
1339 char *iscsi_name;
1340 UuidInfo *uuid_info;
f9dadc98 1341
d4e79929
KW
1342 name = qemu_opt_get(opts, "initiator-name");
1343 if (name) {
1344 return g_strdup(name);
f9dadc98
RS
1345 }
1346
5accc840
PB
1347 uuid_info = qmp_query_uuid(NULL);
1348 if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1349 name = qemu_get_vm_name();
f2ef4a6d 1350 } else {
5accc840 1351 name = uuid_info->UUID;
f9dadc98 1352 }
5accc840
PB
1353 iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1354 name ? ":" : "", name ? name : "");
1355 qapi_free_UuidInfo(uuid_info);
1356 return iscsi_name;
f9dadc98
RS
1357}
1358
5b5d34ec
PL
1359static void iscsi_nop_timed_event(void *opaque)
1360{
1361 IscsiLun *iscsilun = opaque;
1362
d045c466 1363 qemu_mutex_lock(&iscsilun->mutex);
5dd7a535 1364 if (iscsi_get_nops_in_flight(iscsilun->iscsi) >= MAX_NOP_FAILURES) {
5b5d34ec 1365 error_report("iSCSI: NOP timeout. Reconnecting...");
5dd7a535
PL
1366 iscsilun->request_timed_out = true;
1367 } else if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
5b5d34ec 1368 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
2f47da5f 1369 goto out;
5b5d34ec
PL
1370 }
1371
bc72ad67 1372 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
5b5d34ec 1373 iscsi_set_events(iscsilun);
2f47da5f
PB
1374
1375out:
d045c466 1376 qemu_mutex_unlock(&iscsilun->mutex);
5b5d34ec 1377}
5b5d34ec 1378
f2917853 1379static void iscsi_readcapacity_sync(IscsiLun *iscsilun, Error **errp)
cb1b83e7
PL
1380{
1381 struct scsi_task *task = NULL;
1382 struct scsi_readcapacity10 *rc10 = NULL;
1383 struct scsi_readcapacity16 *rc16 = NULL;
cb1b83e7
PL
1384 int retries = ISCSI_CMD_RETRIES;
1385
1288844e
PB
1386 do {
1387 if (task != NULL) {
1388 scsi_free_scsi_task(task);
1389 task = NULL;
cb1b83e7 1390 }
1288844e
PB
1391
1392 switch (iscsilun->type) {
1393 case TYPE_DISK:
1394 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1395 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1396 rc16 = scsi_datain_unmarshall(task);
1397 if (rc16 == NULL) {
f2917853 1398 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity16 data.");
1288844e
PB
1399 } else {
1400 iscsilun->block_size = rc16->block_length;
1401 iscsilun->num_blocks = rc16->returned_lba + 1;
0a386e48
PL
1402 iscsilun->lbpme = !!rc16->lbpme;
1403 iscsilun->lbprz = !!rc16->lbprz;
9281fe9e 1404 iscsilun->use_16_for_rw = (rc16->returned_lba > 0xffffffff);
1288844e 1405 }
1cb6d137 1406 break;
1288844e 1407 }
1cb6d137
ZL
1408 if (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1409 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
1410 break;
1411 }
1412 /* Fall through and try READ CAPACITY(10) instead. */
1288844e
PB
1413 case TYPE_ROM:
1414 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1415 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1416 rc10 = scsi_datain_unmarshall(task);
1417 if (rc10 == NULL) {
f2917853 1418 error_setg(errp, "iSCSI: Failed to unmarshall readcapacity10 data.");
1288844e
PB
1419 } else {
1420 iscsilun->block_size = rc10->block_size;
1421 if (rc10->lba == 0) {
1422 /* blank disk loaded */
1423 iscsilun->num_blocks = 0;
1424 } else {
1425 iscsilun->num_blocks = rc10->lba + 1;
1426 }
1427 }
1428 }
1429 break;
1430 default:
f2917853 1431 return;
cb1b83e7 1432 }
1288844e
PB
1433 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1434 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1435 && retries-- > 0);
cb1b83e7 1436
1288844e 1437 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
240125bc 1438 error_setg(errp, "iSCSI: failed to send readcapacity10/16 command");
6d1f252d
PL
1439 } else if (!iscsilun->block_size ||
1440 iscsilun->block_size % BDRV_SECTOR_SIZE) {
1441 error_setg(errp, "iSCSI: the target returned an invalid "
1442 "block size of %d.", iscsilun->block_size);
1288844e 1443 }
cb1b83e7
PL
1444 if (task) {
1445 scsi_free_scsi_task(task);
1446 }
cb1b83e7
PL
1447}
1448
35cb1748 1449static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi, int lun,
24d3bd67 1450 int evpd, int pc, void **inq, Error **errp)
35cb1748
PB
1451{
1452 int full_size;
1453 struct scsi_task *task = NULL;
1454 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1455 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1456 goto fail;
1457 }
1458 full_size = scsi_datain_getfullsize(task);
1459 if (full_size > task->datain.size) {
1460 scsi_free_scsi_task(task);
1461
1462 /* we need more data for the full list */
1463 task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
f18a7cbb
PL
1464 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1465 goto fail;
1466 }
35cb1748 1467 }
f18a7cbb 1468
24d3bd67
PL
1469 *inq = scsi_datain_unmarshall(task);
1470 if (*inq == NULL) {
1471 error_setg(errp, "iSCSI: failed to unmarshall inquiry datain blob");
172fc4dd 1472 goto fail_with_err;
24d3bd67
PL
1473 }
1474
35cb1748 1475 return task;
f18a7cbb
PL
1476
1477fail:
172fc4dd
MA
1478 error_setg(errp, "iSCSI: Inquiry command failed : %s",
1479 iscsi_get_error(iscsi));
1480fail_with_err:
24d3bd67 1481 if (task != NULL) {
35cb1748 1482 scsi_free_scsi_task(task);
35cb1748
PB
1483 }
1484 return NULL;
f18a7cbb
PL
1485}
1486
80cf6257
SH
1487static void iscsi_detach_aio_context(BlockDriverState *bs)
1488{
1489 IscsiLun *iscsilun = bs->opaque;
1490
dca21ef2 1491 aio_set_fd_handler(iscsilun->aio_context, iscsi_get_fd(iscsilun->iscsi),
f6a51c84 1492 false, NULL, NULL, NULL, NULL);
80cf6257
SH
1493 iscsilun->events = 0;
1494
1495 if (iscsilun->nop_timer) {
1496 timer_del(iscsilun->nop_timer);
1497 timer_free(iscsilun->nop_timer);
1498 iscsilun->nop_timer = NULL;
1499 }
05b685fb
PL
1500 if (iscsilun->event_timer) {
1501 timer_del(iscsilun->event_timer);
1502 timer_free(iscsilun->event_timer);
1503 iscsilun->event_timer = NULL;
1504 }
80cf6257
SH
1505}
1506
1507static void iscsi_attach_aio_context(BlockDriverState *bs,
1508 AioContext *new_context)
1509{
1510 IscsiLun *iscsilun = bs->opaque;
1511
1512 iscsilun->aio_context = new_context;
1513 iscsi_set_events(iscsilun);
1514
80cf6257
SH
1515 /* Set up a timer for sending out iSCSI NOPs */
1516 iscsilun->nop_timer = aio_timer_new(iscsilun->aio_context,
1517 QEMU_CLOCK_REALTIME, SCALE_MS,
1518 iscsi_nop_timed_event, iscsilun);
1519 timer_mod(iscsilun->nop_timer,
1520 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
05b685fb 1521
5dd7a535
PL
1522 /* Set up a timer for periodic calls to iscsi_set_events and to
1523 * scan for command timeout */
05b685fb
PL
1524 iscsilun->event_timer = aio_timer_new(iscsilun->aio_context,
1525 QEMU_CLOCK_REALTIME, SCALE_MS,
5dd7a535
PL
1526 iscsi_timed_check_events, iscsilun);
1527 timer_mod(iscsilun->event_timer,
1528 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + EVENT_INTERVAL);
80cf6257
SH
1529}
1530
7191f208 1531static void iscsi_modesense_sync(IscsiLun *iscsilun)
c1d4096b
FZ
1532{
1533 struct scsi_task *task;
1534 struct scsi_mode_sense *ms = NULL;
7191f208 1535 iscsilun->write_protected = false;
752ce451 1536 iscsilun->dpofua = false;
c1d4096b
FZ
1537
1538 task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun,
1539 1, SCSI_MODESENSE_PC_CURRENT,
1540 0x3F, 0, 255);
1541 if (task == NULL) {
1542 error_report("iSCSI: Failed to send MODE_SENSE(6) command: %s",
1543 iscsi_get_error(iscsilun->iscsi));
1544 goto out;
1545 }
1546
1547 if (task->status != SCSI_STATUS_GOOD) {
1548 error_report("iSCSI: Failed MODE_SENSE(6), LUN assumed writable");
1549 goto out;
1550 }
1551 ms = scsi_datain_unmarshall(task);
1552 if (!ms) {
1553 error_report("iSCSI: Failed to unmarshall MODE_SENSE(6) data: %s",
1554 iscsi_get_error(iscsilun->iscsi));
1555 goto out;
1556 }
7191f208 1557 iscsilun->write_protected = ms->device_specific_parameter & 0x80;
752ce451 1558 iscsilun->dpofua = ms->device_specific_parameter & 0x10;
c1d4096b
FZ
1559
1560out:
1561 if (task) {
1562 scsi_free_scsi_task(task);
1563 }
c1d4096b
FZ
1564}
1565
43171420
KW
1566static void iscsi_parse_iscsi_option(const char *target, QDict *options)
1567{
1568 QemuOptsList *list;
1569 QemuOpts *opts;
81aa2a0f 1570 const char *user, *password, *password_secret, *initiator_name,
1d560104 1571 *header_digest, *timeout;
43171420
KW
1572
1573 list = qemu_find_opts("iscsi");
1574 if (!list) {
1575 return;
1576 }
1577
1578 opts = qemu_opts_find(list, target);
1579 if (opts == NULL) {
1580 opts = QTAILQ_FIRST(&list->head);
1581 if (!opts) {
1582 return;
1583 }
1584 }
1585
1586 user = qemu_opt_get(opts, "user");
1587 if (user) {
1588 qdict_set_default_str(options, "user", user);
1589 }
1590
1591 password = qemu_opt_get(opts, "password");
1592 if (password) {
1593 qdict_set_default_str(options, "password", password);
1594 }
1595
1596 password_secret = qemu_opt_get(opts, "password-secret");
1597 if (password_secret) {
1598 qdict_set_default_str(options, "password-secret", password_secret);
1599 }
d4e79929
KW
1600
1601 initiator_name = qemu_opt_get(opts, "initiator-name");
1602 if (initiator_name) {
1603 qdict_set_default_str(options, "initiator-name", initiator_name);
1604 }
81aa2a0f
KW
1605
1606 header_digest = qemu_opt_get(opts, "header-digest");
1607 if (header_digest) {
31eb1202
KW
1608 /* -iscsi takes upper case values, but QAPI only supports lower case
1609 * enum constant names, so we have to convert here. */
1610 char *qapi_value = g_ascii_strdown(header_digest, -1);
1611 qdict_set_default_str(options, "header-digest", qapi_value);
1612 g_free(qapi_value);
81aa2a0f 1613 }
1d560104
KW
1614
1615 timeout = qemu_opt_get(opts, "timeout");
1616 if (timeout) {
1617 qdict_set_default_str(options, "timeout", timeout);
1618 }
43171420
KW
1619}
1620
c589b249
RS
1621/*
1622 * We support iscsi url's on the form
1623 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1624 */
d5895fcb
KW
1625static void iscsi_parse_filename(const char *filename, QDict *options,
1626 Error **errp)
1627{
1628 struct iscsi_url *iscsi_url;
1629 const char *transport_name;
1630 char *lun_str;
1631
1632 iscsi_url = iscsi_parse_full_url(NULL, filename);
1633 if (iscsi_url == NULL) {
1634 error_setg(errp, "Failed to parse URL : %s", filename);
1635 return;
1636 }
1637
1638#if LIBISCSI_API_VERSION >= (20160603)
1639 switch (iscsi_url->transport) {
1640 case TCP_TRANSPORT:
1641 transport_name = "tcp";
1642 break;
1643 case ISER_TRANSPORT:
1644 transport_name = "iser";
1645 break;
1646 default:
1647 error_setg(errp, "Unknown transport type (%d)",
1648 iscsi_url->transport);
1649 return;
1650 }
1651#else
1652 transport_name = "tcp";
1653#endif
1654
1655 qdict_set_default_str(options, "transport", transport_name);
1656 qdict_set_default_str(options, "portal", iscsi_url->portal);
1657 qdict_set_default_str(options, "target", iscsi_url->target);
1658
1659 lun_str = g_strdup_printf("%d", iscsi_url->lun);
1660 qdict_set_default_str(options, "lun", lun_str);
1661 g_free(lun_str);
1662
43171420
KW
1663 /* User/password from -iscsi take precedence over those from the URL */
1664 iscsi_parse_iscsi_option(iscsi_url->target, options);
1665
d5895fcb
KW
1666 if (iscsi_url->user[0] != '\0') {
1667 qdict_set_default_str(options, "user", iscsi_url->user);
1668 qdict_set_default_str(options, "password", iscsi_url->passwd);
1669 }
1670
1671 iscsi_destroy_url(iscsi_url);
1672}
1673
d5895fcb
KW
1674static QemuOptsList runtime_opts = {
1675 .name = "iscsi",
1676 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1677 .desc = {
1678 {
1679 .name = "transport",
1680 .type = QEMU_OPT_STRING,
1681 },
1682 {
1683 .name = "portal",
1684 .type = QEMU_OPT_STRING,
1685 },
1686 {
1687 .name = "target",
1688 .type = QEMU_OPT_STRING,
1689 },
1690 {
1691 .name = "user",
1692 .type = QEMU_OPT_STRING,
1693 },
1694 {
1695 .name = "password",
1696 .type = QEMU_OPT_STRING,
1697 },
43171420
KW
1698 {
1699 .name = "password-secret",
1700 .type = QEMU_OPT_STRING,
1701 },
d5895fcb
KW
1702 {
1703 .name = "lun",
1704 .type = QEMU_OPT_NUMBER,
1705 },
d4e79929
KW
1706 {
1707 .name = "initiator-name",
1708 .type = QEMU_OPT_STRING,
1709 },
81aa2a0f
KW
1710 {
1711 .name = "header-digest",
1712 .type = QEMU_OPT_STRING,
1713 },
1d560104
KW
1714 {
1715 .name = "timeout",
1716 .type = QEMU_OPT_NUMBER,
1717 },
d5895fcb
KW
1718 { /* end of list */ }
1719 },
1720};
1721
cc9743c2
FZ
1722static void iscsi_save_designator(IscsiLun *lun,
1723 struct scsi_inquiry_device_identification *inq_di)
1724{
1725 struct scsi_inquiry_device_designator *desig, *copy = NULL;
1726
1727 for (desig = inq_di->designators; desig; desig = desig->next) {
1728 if (desig->association ||
1729 desig->designator_type > SCSI_DESIGNATOR_TYPE_NAA) {
1730 continue;
1731 }
1732 /* NAA works better than T10 vendor ID based designator. */
1733 if (!copy || copy->designator_type < desig->designator_type) {
1734 copy = desig;
1735 }
1736 }
1737 if (copy) {
1738 lun->dd = g_new(struct scsi_inquiry_device_designator, 1);
1739 *lun->dd = *copy;
1740 lun->dd->next = NULL;
1741 lun->dd->designator = g_malloc(copy->designator_length);
1742 memcpy(lun->dd->designator, copy->designator, copy->designator_length);
1743 }
1744}
1745
015a1036
HR
1746static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1747 Error **errp)
c589b249
RS
1748{
1749 IscsiLun *iscsilun = bs->opaque;
1750 struct iscsi_context *iscsi = NULL;
e829b0bb
PL
1751 struct scsi_task *task = NULL;
1752 struct scsi_inquiry_standard *inq = NULL;
24d3bd67 1753 struct scsi_inquiry_supported_pages *inq_vpd;
f9dadc98 1754 char *initiator_name = NULL;
60beb341
KW
1755 QemuOpts *opts;
1756 Error *local_err = NULL;
deadbb8e 1757 const char *transport_name, *portal, *target;
d5895fcb
KW
1758#if LIBISCSI_API_VERSION >= (20160603)
1759 enum iscsi_transport_type transport;
1760#endif
1761 int i, ret = 0, timeout = 0, lun;
c589b249 1762
87ea75d5 1763 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
60beb341 1764 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 1765 if (local_err) {
f2917853 1766 error_propagate(errp, local_err);
60beb341
KW
1767 ret = -EINVAL;
1768 goto out;
1769 }
1770
d5895fcb
KW
1771 transport_name = qemu_opt_get(opts, "transport");
1772 portal = qemu_opt_get(opts, "portal");
1773 target = qemu_opt_get(opts, "target");
d5895fcb 1774 lun = qemu_opt_get_number(opts, "lun", 0);
60beb341 1775
d5895fcb
KW
1776 if (!transport_name || !portal || !target) {
1777 error_setg(errp, "Need all of transport, portal and target options");
1778 ret = -EINVAL;
1779 goto out;
1780 }
d5895fcb
KW
1781
1782 if (!strcmp(transport_name, "tcp")) {
1783#if LIBISCSI_API_VERSION >= (20160603)
1784 transport = TCP_TRANSPORT;
1785 } else if (!strcmp(transport_name, "iser")) {
1786 transport = ISER_TRANSPORT;
1787#else
1788 /* TCP is what older libiscsi versions always use */
1789#endif
1790 } else {
1791 error_setg(errp, "Unknown transport: %s", transport_name);
c589b249 1792 ret = -EINVAL;
b93c94f7 1793 goto out;
c589b249
RS
1794 }
1795
f9dadc98
RS
1796 memset(iscsilun, 0, sizeof(IscsiLun));
1797
d4e79929 1798 initiator_name = get_initiator_name(opts);
f9dadc98
RS
1799
1800 iscsi = iscsi_create_context(initiator_name);
1801 if (iscsi == NULL) {
f2917853 1802 error_setg(errp, "iSCSI: Failed to create iSCSI context.");
f9dadc98 1803 ret = -ENOMEM;
b93c94f7 1804 goto out;
f9dadc98 1805 }
e0ae4987 1806#if LIBISCSI_API_VERSION >= (20160603)
d5895fcb 1807 if (iscsi_init_transport(iscsi, transport)) {
e0ae4987
RS
1808 error_setg(errp, ("Error initializing transport."));
1809 ret = -EINVAL;
1810 goto out;
1811 }
1812#endif
d5895fcb 1813 if (iscsi_set_targetname(iscsi, target)) {
f2917853 1814 error_setg(errp, "iSCSI: Failed to set target name.");
c589b249 1815 ret = -EINVAL;
b93c94f7 1816 goto out;
c589b249
RS
1817 }
1818
f9dadc98 1819 /* check if we got CHAP username/password via the options */
43171420 1820 apply_chap(iscsi, opts, &local_err);
f2917853
PB
1821 if (local_err != NULL) {
1822 error_propagate(errp, local_err);
f9dadc98 1823 ret = -EINVAL;
b93c94f7 1824 goto out;
f9dadc98
RS
1825 }
1826
c589b249 1827 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
f2917853 1828 error_setg(errp, "iSCSI: Failed to set session type to normal.");
c589b249 1829 ret = -EINVAL;
b93c94f7 1830 goto out;
c589b249
RS
1831 }
1832
f9dadc98 1833 /* check if we got HEADER_DIGEST via the options */
81aa2a0f 1834 apply_header_digest(iscsi, opts, &local_err);
f2917853
PB
1835 if (local_err != NULL) {
1836 error_propagate(errp, local_err);
1837 ret = -EINVAL;
1838 goto out;
1839 }
f9dadc98 1840
9049736e 1841 /* timeout handling is broken in libiscsi before 1.15.0 */
1d560104 1842 timeout = qemu_opt_get_number(opts, "timeout", 0);
583ec22e 1843#if LIBISCSI_API_VERSION >= 20150621
9049736e
PL
1844 iscsi_set_timeout(iscsi, timeout);
1845#else
1846 if (timeout) {
1847 error_report("iSCSI: ignoring timeout value for libiscsi <1.15.0");
1848 }
1849#endif
5dd7a535 1850
d5895fcb 1851 if (iscsi_full_connect_sync(iscsi, portal, lun) != 0) {
f2917853 1852 error_setg(errp, "iSCSI: Failed to connect to LUN : %s",
e829b0bb
PL
1853 iscsi_get_error(iscsi));
1854 ret = -EINVAL;
1855 goto out;
1856 }
c589b249
RS
1857
1858 iscsilun->iscsi = iscsi;
80cf6257 1859 iscsilun->aio_context = bdrv_get_aio_context(bs);
d5895fcb 1860 iscsilun->lun = lun;
24d3bd67 1861 iscsilun->has_write_same = true;
c589b249 1862
24d3bd67
PL
1863 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 0, 0,
1864 (void **) &inq, errp);
1865 if (task == NULL) {
c589b249 1866 ret = -EINVAL;
b93c94f7 1867 goto out;
c589b249 1868 }
e829b0bb 1869 iscsilun->type = inq->periperal_device_type;
24d3bd67
PL
1870 scsi_free_scsi_task(task);
1871 task = NULL;
e829b0bb 1872
7191f208 1873 iscsi_modesense_sync(iscsilun);
4df863f3
EB
1874 if (iscsilun->dpofua) {
1875 bs->supported_write_flags = BDRV_REQ_FUA;
1876 }
7191f208 1877
c1d4096b
FZ
1878 /* Check the write protect flag of the LUN if we want to write */
1879 if (iscsilun->type == TYPE_DISK && (flags & BDRV_O_RDWR) &&
43ae8fb1 1880 iscsilun->write_protected) {
c1d4096b
FZ
1881 error_setg(errp, "Cannot open a write protected LUN as read-write");
1882 ret = -EACCES;
1883 goto out;
1884 }
1885
f2917853
PB
1886 iscsi_readcapacity_sync(iscsilun, &local_err);
1887 if (local_err != NULL) {
1888 error_propagate(errp, local_err);
cd82b6fb 1889 ret = -EINVAL;
cb1b83e7 1890 goto out;
e829b0bb 1891 }
0777b5dd 1892 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
e829b0bb 1893
f47c3f5a
KW
1894 /* We don't have any emulation for devices other than disks and CD-ROMs, so
1895 * this must be sg ioctl compatible. We force it to be sg, otherwise qemu
1896 * will try to read from the device to guess the image format.
622695a4 1897 */
f47c3f5a 1898 if (iscsilun->type != TYPE_DISK && iscsilun->type != TYPE_ROM) {
54115412 1899 bs->sg = true;
622695a4
RS
1900 }
1901
24d3bd67
PL
1902 task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1903 SCSI_INQUIRY_PAGECODE_SUPPORTED_VPD_PAGES,
1904 (void **) &inq_vpd, errp);
1905 if (task == NULL) {
1906 ret = -EINVAL;
1907 goto out;
f18a7cbb 1908 }
24d3bd67
PL
1909 for (i = 0; i < inq_vpd->num_pages; i++) {
1910 struct scsi_task *inq_task;
1911 struct scsi_inquiry_logical_block_provisioning *inq_lbp;
f18a7cbb 1912 struct scsi_inquiry_block_limits *inq_bl;
cc9743c2 1913 struct scsi_inquiry_device_identification *inq_di;
24d3bd67
PL
1914 switch (inq_vpd->pages[i]) {
1915 case SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING:
1916 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1917 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING,
1918 (void **) &inq_lbp, errp);
1919 if (inq_task == NULL) {
1920 ret = -EINVAL;
1921 goto out;
1922 }
1923 memcpy(&iscsilun->lbp, inq_lbp,
1924 sizeof(struct scsi_inquiry_logical_block_provisioning));
1925 scsi_free_scsi_task(inq_task);
1926 break;
1927 case SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS:
1928 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1929 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS,
1930 (void **) &inq_bl, errp);
1931 if (inq_task == NULL) {
1932 ret = -EINVAL;
1933 goto out;
1934 }
1935 memcpy(&iscsilun->bl, inq_bl,
1936 sizeof(struct scsi_inquiry_block_limits));
1937 scsi_free_scsi_task(inq_task);
1938 break;
cc9743c2
FZ
1939 case SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION:
1940 inq_task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1941 SCSI_INQUIRY_PAGECODE_DEVICE_IDENTIFICATION,
1942 (void **) &inq_di, errp);
1943 if (inq_task == NULL) {
1944 ret = -EINVAL;
1945 goto out;
1946 }
1947 iscsi_save_designator(iscsilun, inq_di);
1948 scsi_free_scsi_task(inq_task);
1949 break;
24d3bd67
PL
1950 default:
1951 break;
f18a7cbb 1952 }
f18a7cbb 1953 }
24d3bd67
PL
1954 scsi_free_scsi_task(task);
1955 task = NULL;
f18a7cbb 1956
d045c466 1957 qemu_mutex_init(&iscsilun->mutex);
80cf6257 1958 iscsi_attach_aio_context(bs, iscsilun->aio_context);
5b5d34ec 1959
b03c3805
PL
1960 /* Guess the internal cluster (page) size of the iscsi target by the means
1961 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1962 * reasonable size */
3d2acaa3 1963 if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 4 * 1024 &&
b03c3805 1964 iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
ba059e7b
EB
1965 iscsilun->cluster_size = iscsilun->bl.opt_unmap_gran *
1966 iscsilun->block_size;
9eac3622 1967 if (iscsilun->lbprz) {
e1123a3b 1968 ret = iscsi_allocmap_init(iscsilun, bs->open_flags);
b03c3805 1969 }
b03c3805
PL
1970 }
1971
e24d813b
EB
1972 if (iscsilun->lbprz && iscsilun->lbp.lbpws) {
1973 bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
1974 }
1975
b93c94f7 1976out:
60beb341 1977 qemu_opts_del(opts);
f7047c2d 1978 g_free(initiator_name);
e829b0bb
PL
1979 if (task != NULL) {
1980 scsi_free_scsi_task(task);
1981 }
b93c94f7
PB
1982
1983 if (ret) {
1984 if (iscsi != NULL) {
20474e9a
PL
1985 if (iscsi_is_logged_in(iscsi)) {
1986 iscsi_logout_sync(iscsi);
1987 }
b93c94f7
PB
1988 iscsi_destroy_context(iscsi);
1989 }
1990 memset(iscsilun, 0, sizeof(IscsiLun));
c589b249 1991 }
deadbb8e 1992
c589b249
RS
1993 return ret;
1994}
1995
1996static void iscsi_close(BlockDriverState *bs)
1997{
1998 IscsiLun *iscsilun = bs->opaque;
1999 struct iscsi_context *iscsi = iscsilun->iscsi;
2000
80cf6257 2001 iscsi_detach_aio_context(bs);
20474e9a
PL
2002 if (iscsi_is_logged_in(iscsi)) {
2003 iscsi_logout_sync(iscsi);
2004 }
c589b249 2005 iscsi_destroy_context(iscsi);
cc9743c2
FZ
2006 if (iscsilun->dd) {
2007 g_free(iscsilun->dd->designator);
2008 g_free(iscsilun->dd);
2009 }
d4cd9615 2010 g_free(iscsilun->zeroblock);
e1123a3b 2011 iscsi_allocmap_free(iscsilun);
d045c466 2012 qemu_mutex_destroy(&iscsilun->mutex);
c589b249
RS
2013 memset(iscsilun, 0, sizeof(IscsiLun));
2014}
2015
52f6fa14
PL
2016static void iscsi_refresh_limits(BlockDriverState *bs, Error **errp)
2017{
d34682cd
KW
2018 /* We don't actually refresh here, but just return data queried in
2019 * iscsi_open(): iscsi targets don't change their limits. */
52f6fa14
PL
2020
2021 IscsiLun *iscsilun = bs->opaque;
5def6b80 2022 uint64_t max_xfer_len = iscsilun->use_16_for_rw ? 0xffffffff : 0xffff;
95eaa785 2023 unsigned int block_size = MAX(BDRV_SECTOR_SIZE, iscsilun->block_size);
52f6fa14 2024
95eaa785
EB
2025 assert(iscsilun->block_size >= BDRV_SECTOR_SIZE || bs->sg);
2026
2027 bs->bl.request_alignment = block_size;
c8b3b998 2028
52f6fa14
PL
2029 if (iscsilun->bl.max_xfer_len) {
2030 max_xfer_len = MIN(max_xfer_len, iscsilun->bl.max_xfer_len);
2031 }
2032
95eaa785 2033 if (max_xfer_len * block_size < INT_MAX) {
5def6b80
EB
2034 bs->bl.max_transfer = max_xfer_len * iscsilun->block_size;
2035 }
52f6fa14 2036
c97ca29d 2037 if (iscsilun->lbp.lbpu) {
95eaa785 2038 if (iscsilun->bl.max_unmap < 0xffffffff / block_size) {
b9f7855a
EB
2039 bs->bl.max_pdiscard =
2040 iscsilun->bl.max_unmap * iscsilun->block_size;
d34682cd 2041 }
b9f7855a
EB
2042 bs->bl.pdiscard_alignment =
2043 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
8b184744 2044 } else {
b9f7855a 2045 bs->bl.pdiscard_alignment = iscsilun->block_size;
c97ca29d 2046 }
d34682cd 2047
95eaa785 2048 if (iscsilun->bl.max_ws_len < 0xffffffff / block_size) {
cf081fca
EB
2049 bs->bl.max_pwrite_zeroes =
2050 iscsilun->bl.max_ws_len * iscsilun->block_size;
c97ca29d
PB
2051 }
2052 if (iscsilun->lbp.lbpws) {
cf081fca
EB
2053 bs->bl.pwrite_zeroes_alignment =
2054 iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
8b184744 2055 } else {
cf081fca 2056 bs->bl.pwrite_zeroes_alignment = iscsilun->block_size;
d34682cd 2057 }
5def6b80 2058 if (iscsilun->bl.opt_xfer_len &&
95eaa785 2059 iscsilun->bl.opt_xfer_len < INT_MAX / block_size) {
5def6b80
EB
2060 bs->bl.opt_transfer = pow2floor(iscsilun->bl.opt_xfer_len *
2061 iscsilun->block_size);
2062 }
e9f526ab 2063}
d34682cd 2064
43ae8fb1
FZ
2065/* Note that this will not re-establish a connection with an iSCSI target - it
2066 * is effectively a NOP. */
dc6afb99
JC
2067static int iscsi_reopen_prepare(BDRVReopenState *state,
2068 BlockReopenQueue *queue, Error **errp)
2069{
43ae8fb1
FZ
2070 IscsiLun *iscsilun = state->bs->opaque;
2071
2072 if (state->flags & BDRV_O_RDWR && iscsilun->write_protected) {
2073 error_setg(errp, "Cannot open a write protected LUN as read-write");
2074 return -EACCES;
2075 }
d34682cd
KW
2076 return 0;
2077}
2078
e1123a3b
PL
2079static void iscsi_reopen_commit(BDRVReopenState *reopen_state)
2080{
2081 IscsiLun *iscsilun = reopen_state->bs->opaque;
2082
2083 /* the cache.direct status might have changed */
2084 if (iscsilun->allocmap != NULL) {
2085 iscsi_allocmap_init(iscsilun, reopen_state->flags);
2086 }
2087}
2088
061ca8a3
KW
2089static int coroutine_fn iscsi_co_truncate(BlockDriverState *bs, int64_t offset,
2090 PreallocMode prealloc, Error **errp)
cb1b83e7
PL
2091{
2092 IscsiLun *iscsilun = bs->opaque;
f2917853 2093 Error *local_err = NULL;
cb1b83e7 2094
8243ccb7
HR
2095 if (prealloc != PREALLOC_MODE_OFF) {
2096 error_setg(errp, "Unsupported preallocation mode '%s'",
977c736f 2097 PreallocMode_str(prealloc));
8243ccb7
HR
2098 return -ENOTSUP;
2099 }
2100
cb1b83e7 2101 if (iscsilun->type != TYPE_DISK) {
f59adb32 2102 error_setg(errp, "Cannot resize non-disk iSCSI devices");
cb1b83e7
PL
2103 return -ENOTSUP;
2104 }
2105
f2917853
PB
2106 iscsi_readcapacity_sync(iscsilun, &local_err);
2107 if (local_err != NULL) {
4bff28b8 2108 error_propagate(errp, local_err);
f2917853 2109 return -EIO;
cb1b83e7
PL
2110 }
2111
2112 if (offset > iscsi_getlength(bs)) {
f59adb32 2113 error_setg(errp, "Cannot grow iSCSI devices");
cb1b83e7
PL
2114 return -EINVAL;
2115 }
2116
e1123a3b
PL
2117 if (iscsilun->allocmap != NULL) {
2118 iscsi_allocmap_init(iscsilun, bs->open_flags);
b03c3805
PL
2119 }
2120
cb1b83e7
PL
2121 return 0;
2122}
2123
efc75e2a
SH
2124static int coroutine_fn iscsi_co_create_opts(const char *filename, QemuOpts *opts,
2125 Error **errp)
de8864e5
PL
2126{
2127 int ret = 0;
2128 int64_t total_size = 0;
13c91cb7 2129 BlockDriverState *bs;
de8864e5 2130 IscsiLun *iscsilun = NULL;
60beb341 2131 QDict *bs_options;
2ec9a782 2132 Error *local_err = NULL;
de8864e5 2133
e4e9986b 2134 bs = bdrv_new();
de8864e5
PL
2135
2136 /* Read out options */
c2eb918e
HT
2137 total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
2138 BDRV_SECTOR_SIZE);
5839e53b 2139 bs->opaque = g_new0(struct IscsiLun, 1);
13c91cb7 2140 iscsilun = bs->opaque;
de8864e5 2141
60beb341 2142 bs_options = qdict_new();
2ec9a782
FZ
2143 iscsi_parse_filename(filename, bs_options, &local_err);
2144 if (local_err) {
2145 error_propagate(errp, local_err);
2146 ret = -EINVAL;
2147 } else {
2148 ret = iscsi_open(bs, bs_options, 0, NULL);
2149 }
cb3e7f08 2150 qobject_unref(bs_options);
60beb341 2151
de8864e5
PL
2152 if (ret != 0) {
2153 goto out;
2154 }
80cf6257 2155 iscsi_detach_aio_context(bs);
de8864e5
PL
2156 if (iscsilun->type != TYPE_DISK) {
2157 ret = -ENODEV;
2158 goto out;
2159 }
13c91cb7 2160 if (bs->total_sectors < total_size) {
de8864e5 2161 ret = -ENOSPC;
d3bda7bc 2162 goto out;
de8864e5
PL
2163 }
2164
2165 ret = 0;
2166out:
2167 if (iscsilun->iscsi != NULL) {
2168 iscsi_destroy_context(iscsilun->iscsi);
2169 }
13c91cb7
FZ
2170 g_free(bs->opaque);
2171 bs->opaque = NULL;
4f6fd349 2172 bdrv_unref(bs);
de8864e5
PL
2173 return ret;
2174}
2175
186d4f2b
PL
2176static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2177{
2178 IscsiLun *iscsilun = bs->opaque;
0a386e48 2179 bdi->unallocated_blocks_are_zero = iscsilun->lbprz;
ba059e7b 2180 bdi->cluster_size = iscsilun->cluster_size;
186d4f2b
PL
2181 return 0;
2182}
2183
2b148f39
PB
2184static void coroutine_fn iscsi_co_invalidate_cache(BlockDriverState *bs,
2185 Error **errp)
e1123a3b
PL
2186{
2187 IscsiLun *iscsilun = bs->opaque;
2188 iscsi_allocmap_invalidate(iscsilun);
2189}
2190
604dfaaa
FZ
2191static int coroutine_fn iscsi_co_copy_range_from(BlockDriverState *bs,
2192 BdrvChild *src,
2193 uint64_t src_offset,
2194 BdrvChild *dst,
2195 uint64_t dst_offset,
2196 uint64_t bytes,
67b51fb9
VSO
2197 BdrvRequestFlags read_flags,
2198 BdrvRequestFlags write_flags)
604dfaaa 2199{
67b51fb9
VSO
2200 return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
2201 read_flags, write_flags);
604dfaaa
FZ
2202}
2203
2204static struct scsi_task *iscsi_xcopy_task(int param_len)
2205{
2206 struct scsi_task *task;
2207
2208 task = g_new0(struct scsi_task, 1);
2209
2210 task->cdb[0] = EXTENDED_COPY;
2211 task->cdb[10] = (param_len >> 24) & 0xFF;
2212 task->cdb[11] = (param_len >> 16) & 0xFF;
2213 task->cdb[12] = (param_len >> 8) & 0xFF;
2214 task->cdb[13] = param_len & 0xFF;
2215 task->cdb_size = 16;
2216 task->xfer_dir = SCSI_XFER_WRITE;
2217 task->expxferlen = param_len;
2218
2219 return task;
2220}
2221
2222static void iscsi_populate_target_desc(unsigned char *desc, IscsiLun *lun)
2223{
2224 struct scsi_inquiry_device_designator *dd = lun->dd;
2225
2226 memset(desc, 0, 32);
2227 desc[0] = 0xE4; /* IDENT_DESCR_TGT_DESCR */
2228 desc[4] = dd->code_set;
2229 desc[5] = (dd->designator_type & 0xF)
2230 | ((dd->association & 3) << 4);
2231 desc[7] = dd->designator_length;
1439b9c1 2232 memcpy(desc + 8, dd->designator, MIN(dd->designator_length, 20));
604dfaaa
FZ
2233
2234 desc[28] = 0;
2235 desc[29] = (lun->block_size >> 16) & 0xFF;
2236 desc[30] = (lun->block_size >> 8) & 0xFF;
2237 desc[31] = lun->block_size & 0xFF;
2238}
2239
2240static void iscsi_xcopy_desc_hdr(uint8_t *hdr, int dc, int cat, int src_index,
2241 int dst_index)
2242{
2243 hdr[0] = 0x02; /* BLK_TO_BLK_SEG_DESCR */
2244 hdr[1] = ((dc << 1) | cat) & 0xFF;
2245 hdr[2] = (XCOPY_BLK2BLK_SEG_DESC_SIZE >> 8) & 0xFF;
2246 /* don't account for the first 4 bytes in descriptor header*/
2247 hdr[3] = (XCOPY_BLK2BLK_SEG_DESC_SIZE - 4 /* SEG_DESC_SRC_INDEX_OFFSET */) & 0xFF;
2248 hdr[4] = (src_index >> 8) & 0xFF;
2249 hdr[5] = src_index & 0xFF;
2250 hdr[6] = (dst_index >> 8) & 0xFF;
2251 hdr[7] = dst_index & 0xFF;
2252}
2253
2254static void iscsi_xcopy_populate_desc(uint8_t *desc, int dc, int cat,
2255 int src_index, int dst_index, int num_blks,
2256 uint64_t src_lba, uint64_t dst_lba)
2257{
2258 iscsi_xcopy_desc_hdr(desc, dc, cat, src_index, dst_index);
2259
2260 /* The caller should verify the request size */
2261 assert(num_blks < 65536);
2262 desc[10] = (num_blks >> 8) & 0xFF;
2263 desc[11] = num_blks & 0xFF;
2264 desc[12] = (src_lba >> 56) & 0xFF;
2265 desc[13] = (src_lba >> 48) & 0xFF;
2266 desc[14] = (src_lba >> 40) & 0xFF;
2267 desc[15] = (src_lba >> 32) & 0xFF;
2268 desc[16] = (src_lba >> 24) & 0xFF;
2269 desc[17] = (src_lba >> 16) & 0xFF;
2270 desc[18] = (src_lba >> 8) & 0xFF;
2271 desc[19] = src_lba & 0xFF;
2272 desc[20] = (dst_lba >> 56) & 0xFF;
2273 desc[21] = (dst_lba >> 48) & 0xFF;
2274 desc[22] = (dst_lba >> 40) & 0xFF;
2275 desc[23] = (dst_lba >> 32) & 0xFF;
2276 desc[24] = (dst_lba >> 24) & 0xFF;
2277 desc[25] = (dst_lba >> 16) & 0xFF;
2278 desc[26] = (dst_lba >> 8) & 0xFF;
2279 desc[27] = dst_lba & 0xFF;
2280}
2281
2282static void iscsi_xcopy_populate_header(unsigned char *buf, int list_id, int str,
2283 int list_id_usage, int prio,
2284 int tgt_desc_len,
2285 int seg_desc_len, int inline_data_len)
2286{
2287 buf[0] = list_id;
2288 buf[1] = ((str & 1) << 5) | ((list_id_usage & 3) << 3) | (prio & 7);
2289 buf[2] = (tgt_desc_len >> 8) & 0xFF;
2290 buf[3] = tgt_desc_len & 0xFF;
2291 buf[8] = (seg_desc_len >> 24) & 0xFF;
2292 buf[9] = (seg_desc_len >> 16) & 0xFF;
2293 buf[10] = (seg_desc_len >> 8) & 0xFF;
2294 buf[11] = seg_desc_len & 0xFF;
2295 buf[12] = (inline_data_len >> 24) & 0xFF;
2296 buf[13] = (inline_data_len >> 16) & 0xFF;
2297 buf[14] = (inline_data_len >> 8) & 0xFF;
2298 buf[15] = inline_data_len & 0xFF;
2299}
2300
2301static void iscsi_xcopy_data(struct iscsi_data *data,
2302 IscsiLun *src, int64_t src_lba,
2303 IscsiLun *dst, int64_t dst_lba,
2304 uint16_t num_blocks)
2305{
2306 uint8_t *buf;
2307 const int src_offset = XCOPY_DESC_OFFSET;
2308 const int dst_offset = XCOPY_DESC_OFFSET + IDENT_DESCR_TGT_DESCR_SIZE;
2309 const int seg_offset = dst_offset + IDENT_DESCR_TGT_DESCR_SIZE;
2310
2311 data->size = XCOPY_DESC_OFFSET +
2312 IDENT_DESCR_TGT_DESCR_SIZE * 2 +
2313 XCOPY_BLK2BLK_SEG_DESC_SIZE;
2314 data->data = g_malloc0(data->size);
2315 buf = data->data;
2316
2317 /* Initialise the parameter list header */
2318 iscsi_xcopy_populate_header(buf, 1, 0, 2 /* LIST_ID_USAGE_DISCARD */,
2319 0, 2 * IDENT_DESCR_TGT_DESCR_SIZE,
2320 XCOPY_BLK2BLK_SEG_DESC_SIZE,
2321 0);
2322
2323 /* Initialise CSCD list with one src + one dst descriptor */
2324 iscsi_populate_target_desc(&buf[src_offset], src);
2325 iscsi_populate_target_desc(&buf[dst_offset], dst);
2326
2327 /* Initialise one segment descriptor */
2328 iscsi_xcopy_populate_desc(&buf[seg_offset], 0, 0, 0, 1, num_blocks,
2329 src_lba, dst_lba);
2330}
2331
2332static int coroutine_fn iscsi_co_copy_range_to(BlockDriverState *bs,
2333 BdrvChild *src,
2334 uint64_t src_offset,
2335 BdrvChild *dst,
2336 uint64_t dst_offset,
2337 uint64_t bytes,
67b51fb9
VSO
2338 BdrvRequestFlags read_flags,
2339 BdrvRequestFlags write_flags)
604dfaaa
FZ
2340{
2341 IscsiLun *dst_lun = dst->bs->opaque;
2342 IscsiLun *src_lun;
2343 struct IscsiTask iscsi_task;
2344 struct iscsi_data data;
2345 int r = 0;
2346 int block_size;
2347
2348 if (src->bs->drv->bdrv_co_copy_range_to != iscsi_co_copy_range_to) {
2349 return -ENOTSUP;
2350 }
2351 src_lun = src->bs->opaque;
2352
2353 if (!src_lun->dd || !dst_lun->dd) {
2354 return -ENOTSUP;
2355 }
2356 if (!is_byte_request_lun_aligned(dst_offset, bytes, dst_lun)) {
2357 return -ENOTSUP;
2358 }
2359 if (!is_byte_request_lun_aligned(src_offset, bytes, src_lun)) {
2360 return -ENOTSUP;
2361 }
2362 if (dst_lun->block_size != src_lun->block_size ||
2363 !dst_lun->block_size) {
2364 return -ENOTSUP;
2365 }
2366
2367 block_size = dst_lun->block_size;
2368 if (bytes / block_size > 65535) {
2369 return -ENOTSUP;
2370 }
2371
2372 iscsi_xcopy_data(&data,
2373 src_lun, src_offset / block_size,
2374 dst_lun, dst_offset / block_size,
2375 bytes / block_size);
2376
2377 iscsi_co_init_iscsitask(dst_lun, &iscsi_task);
2378
2379 qemu_mutex_lock(&dst_lun->mutex);
2380 iscsi_task.task = iscsi_xcopy_task(data.size);
2381retry:
2382 if (iscsi_scsi_command_async(dst_lun->iscsi, dst_lun->lun,
2383 iscsi_task.task, iscsi_co_generic_cb,
2384 &data,
2385 &iscsi_task) != 0) {
2386 r = -EIO;
2387 goto out_unlock;
2388 }
2389
2390 iscsi_co_wait_for_task(&iscsi_task, dst_lun);
2391
2392 if (iscsi_task.do_retry) {
2393 iscsi_task.complete = 0;
2394 goto retry;
2395 }
2396
2397 if (iscsi_task.status != SCSI_STATUS_GOOD) {
2398 r = iscsi_task.err_code;
2399 goto out_unlock;
2400 }
2401
2402out_unlock:
ecc983a5
FZ
2403
2404 trace_iscsi_xcopy(src_lun, src_offset, dst_lun, dst_offset, bytes, r);
604dfaaa
FZ
2405 g_free(iscsi_task.task);
2406 qemu_mutex_unlock(&dst_lun->mutex);
2407 g_free(iscsi_task.err_str);
2408 return r;
2409}
2410
a59479e3
CL
2411static QemuOptsList iscsi_create_opts = {
2412 .name = "iscsi-create-opts",
2413 .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
2414 .desc = {
2415 {
2416 .name = BLOCK_OPT_SIZE,
2417 .type = QEMU_OPT_SIZE,
2418 .help = "Virtual disk size"
2419 },
2420 { /* end of list */ }
2421 }
de8864e5
PL
2422};
2423
c589b249
RS
2424static BlockDriver bdrv_iscsi = {
2425 .format_name = "iscsi",
2426 .protocol_name = "iscsi",
2427
d5895fcb
KW
2428 .instance_size = sizeof(IscsiLun),
2429 .bdrv_parse_filename = iscsi_parse_filename,
2430 .bdrv_file_open = iscsi_open,
2431 .bdrv_close = iscsi_close,
efc75e2a 2432 .bdrv_co_create_opts = iscsi_co_create_opts,
d5895fcb
KW
2433 .create_opts = &iscsi_create_opts,
2434 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2435 .bdrv_reopen_commit = iscsi_reopen_commit,
2b148f39 2436 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache,
c589b249
RS
2437
2438 .bdrv_getlength = iscsi_getlength,
186d4f2b 2439 .bdrv_get_info = iscsi_get_info,
061ca8a3 2440 .bdrv_co_truncate = iscsi_co_truncate,
d34682cd 2441 .bdrv_refresh_limits = iscsi_refresh_limits,
c589b249 2442
92809c36 2443 .bdrv_co_block_status = iscsi_co_block_status,
97c7e85c 2444 .bdrv_co_pdiscard = iscsi_co_pdiscard,
604dfaaa
FZ
2445 .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
2446 .bdrv_co_copy_range_to = iscsi_co_copy_range_to,
94d047a3 2447 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
063c3378 2448 .bdrv_co_readv = iscsi_co_readv,
e18a58b4 2449 .bdrv_co_writev = iscsi_co_writev,
063c3378 2450 .bdrv_co_flush_to_disk = iscsi_co_flush,
fa6acb0c 2451
98392453 2452#ifdef __linux__
98392453
RS
2453 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2454#endif
80cf6257
SH
2455
2456 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2457 .bdrv_attach_aio_context = iscsi_attach_aio_context,
c589b249
RS
2458};
2459
e0ae4987
RS
2460#if LIBISCSI_API_VERSION >= (20160603)
2461static BlockDriver bdrv_iser = {
2462 .format_name = "iser",
2463 .protocol_name = "iser",
2464
d5895fcb
KW
2465 .instance_size = sizeof(IscsiLun),
2466 .bdrv_parse_filename = iscsi_parse_filename,
2467 .bdrv_file_open = iscsi_open,
2468 .bdrv_close = iscsi_close,
efc75e2a 2469 .bdrv_co_create_opts = iscsi_co_create_opts,
d5895fcb
KW
2470 .create_opts = &iscsi_create_opts,
2471 .bdrv_reopen_prepare = iscsi_reopen_prepare,
2472 .bdrv_reopen_commit = iscsi_reopen_commit,
b16a7cfa 2473 .bdrv_co_invalidate_cache = iscsi_co_invalidate_cache,
e0ae4987
RS
2474
2475 .bdrv_getlength = iscsi_getlength,
2476 .bdrv_get_info = iscsi_get_info,
061ca8a3 2477 .bdrv_co_truncate = iscsi_co_truncate,
e0ae4987
RS
2478 .bdrv_refresh_limits = iscsi_refresh_limits,
2479
92809c36 2480 .bdrv_co_block_status = iscsi_co_block_status,
e0ae4987 2481 .bdrv_co_pdiscard = iscsi_co_pdiscard,
604dfaaa
FZ
2482 .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
2483 .bdrv_co_copy_range_to = iscsi_co_copy_range_to,
e0ae4987
RS
2484 .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
2485 .bdrv_co_readv = iscsi_co_readv,
e18a58b4 2486 .bdrv_co_writev = iscsi_co_writev,
e0ae4987
RS
2487 .bdrv_co_flush_to_disk = iscsi_co_flush,
2488
2489#ifdef __linux__
2490 .bdrv_aio_ioctl = iscsi_aio_ioctl,
2491#endif
2492
2493 .bdrv_detach_aio_context = iscsi_detach_aio_context,
2494 .bdrv_attach_aio_context = iscsi_attach_aio_context,
2495};
2496#endif
2497
c589b249
RS
2498static void iscsi_block_init(void)
2499{
2500 bdrv_register(&bdrv_iscsi);
e0ae4987
RS
2501#if LIBISCSI_API_VERSION >= (20160603)
2502 bdrv_register(&bdrv_iser);
2503#endif
c589b249
RS
2504}
2505
2506block_init(iscsi_block_init);