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