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