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