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