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