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