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