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