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