xref: /openbmc/qemu/block/iscsi.c (revision 94c3db85)
1 /*
2  * QEMU Block driver for iSCSI images
3  *
4  * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "config-host.h"
26 
27 #include <poll.h>
28 #include <arpa/inet.h>
29 #include "qemu-common.h"
30 #include "qemu-error.h"
31 #include "block_int.h"
32 #include "trace.h"
33 #include "hw/scsi-defs.h"
34 
35 #include <iscsi/iscsi.h>
36 #include <iscsi/scsi-lowlevel.h>
37 
38 #ifdef __linux__
39 #include <scsi/sg.h>
40 #include <hw/scsi-defs.h>
41 #endif
42 
43 typedef struct IscsiLun {
44     struct iscsi_context *iscsi;
45     int lun;
46     enum scsi_inquiry_peripheral_device_type type;
47     int block_size;
48     uint64_t num_blocks;
49     int events;
50 } IscsiLun;
51 
52 typedef struct IscsiAIOCB {
53     BlockDriverAIOCB common;
54     QEMUIOVector *qiov;
55     QEMUBH *bh;
56     IscsiLun *iscsilun;
57     struct scsi_task *task;
58     uint8_t *buf;
59     int status;
60     int canceled;
61     size_t read_size;
62     size_t read_offset;
63 #ifdef __linux__
64     sg_io_hdr_t *ioh;
65 #endif
66 } IscsiAIOCB;
67 
68 struct IscsiTask {
69     IscsiLun *iscsilun;
70     BlockDriverState *bs;
71     int status;
72     int complete;
73 };
74 
75 static void
76 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
77                     void *private_data)
78 {
79 }
80 
81 static void
82 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
83 {
84     IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
85     IscsiLun *iscsilun = acb->iscsilun;
86 
87     acb->common.cb(acb->common.opaque, -ECANCELED);
88     acb->canceled = 1;
89 
90     /* send a task mgmt call to the target to cancel the task on the target */
91     iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
92                                      iscsi_abort_task_cb, NULL);
93 
94     /* then also cancel the task locally in libiscsi */
95     iscsi_scsi_task_cancel(iscsilun->iscsi, acb->task);
96 }
97 
98 static AIOPool iscsi_aio_pool = {
99     .aiocb_size         = sizeof(IscsiAIOCB),
100     .cancel             = iscsi_aio_cancel,
101 };
102 
103 
104 static void iscsi_process_read(void *arg);
105 static void iscsi_process_write(void *arg);
106 
107 static int iscsi_process_flush(void *arg)
108 {
109     IscsiLun *iscsilun = arg;
110 
111     return iscsi_queue_length(iscsilun->iscsi) > 0;
112 }
113 
114 static void
115 iscsi_set_events(IscsiLun *iscsilun)
116 {
117     struct iscsi_context *iscsi = iscsilun->iscsi;
118     int ev;
119 
120     /* We always register a read handler.  */
121     ev = POLLIN;
122     ev |= iscsi_which_events(iscsi);
123     if (ev != iscsilun->events) {
124         qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
125                       iscsi_process_read,
126                       (ev & POLLOUT) ? iscsi_process_write : NULL,
127                       iscsi_process_flush,
128                       iscsilun);
129 
130     }
131 
132     /* If we just added an event, the callback might be delayed
133      * unless we call qemu_notify_event().
134      */
135     if (ev & ~iscsilun->events) {
136         qemu_notify_event();
137     }
138     iscsilun->events = ev;
139 }
140 
141 static void
142 iscsi_process_read(void *arg)
143 {
144     IscsiLun *iscsilun = arg;
145     struct iscsi_context *iscsi = iscsilun->iscsi;
146 
147     iscsi_service(iscsi, POLLIN);
148     iscsi_set_events(iscsilun);
149 }
150 
151 static void
152 iscsi_process_write(void *arg)
153 {
154     IscsiLun *iscsilun = arg;
155     struct iscsi_context *iscsi = iscsilun->iscsi;
156 
157     iscsi_service(iscsi, POLLOUT);
158     iscsi_set_events(iscsilun);
159 }
160 
161 
162 static int
163 iscsi_schedule_bh(QEMUBHFunc *cb, IscsiAIOCB *acb)
164 {
165     acb->bh = qemu_bh_new(cb, acb);
166     if (!acb->bh) {
167         error_report("oom: could not create iscsi bh");
168         return -EIO;
169     }
170 
171     qemu_bh_schedule(acb->bh);
172     return 0;
173 }
174 
175 static void
176 iscsi_readv_writev_bh_cb(void *p)
177 {
178     IscsiAIOCB *acb = p;
179 
180     qemu_bh_delete(acb->bh);
181 
182     if (acb->canceled == 0) {
183         acb->common.cb(acb->common.opaque, acb->status);
184     }
185 
186     qemu_aio_release(acb);
187 }
188 
189 
190 static void
191 iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
192                      void *command_data, void *opaque)
193 {
194     IscsiAIOCB *acb = opaque;
195 
196     trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
197 
198     g_free(acb->buf);
199 
200     if (acb->canceled != 0) {
201         qemu_aio_release(acb);
202         scsi_free_scsi_task(acb->task);
203         acb->task = NULL;
204         return;
205     }
206 
207     acb->status = 0;
208     if (status < 0) {
209         error_report("Failed to write16 data to iSCSI lun. %s",
210                      iscsi_get_error(iscsi));
211         acb->status = -EIO;
212     }
213 
214     iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
215     scsi_free_scsi_task(acb->task);
216     acb->task = NULL;
217 }
218 
219 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
220 {
221     return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
222 }
223 
224 static BlockDriverAIOCB *
225 iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
226                  QEMUIOVector *qiov, int nb_sectors,
227                  BlockDriverCompletionFunc *cb,
228                  void *opaque)
229 {
230     IscsiLun *iscsilun = bs->opaque;
231     struct iscsi_context *iscsi = iscsilun->iscsi;
232     IscsiAIOCB *acb;
233     size_t size;
234     uint32_t num_sectors;
235     uint64_t lba;
236     struct iscsi_data data;
237 
238     acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
239     trace_iscsi_aio_writev(iscsi, sector_num, nb_sectors, opaque, acb);
240 
241     acb->iscsilun = iscsilun;
242     acb->qiov     = qiov;
243 
244     acb->canceled   = 0;
245 
246     /* XXX we should pass the iovec to write16 to avoid the extra copy */
247     /* this will allow us to get rid of 'buf' completely */
248     size = nb_sectors * BDRV_SECTOR_SIZE;
249     acb->buf = g_malloc(size);
250     qemu_iovec_to_buf(acb->qiov, 0, acb->buf, size);
251 
252     acb->task = malloc(sizeof(struct scsi_task));
253     if (acb->task == NULL) {
254         error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
255                      "command. %s", iscsi_get_error(iscsi));
256         qemu_aio_release(acb);
257         return NULL;
258     }
259     memset(acb->task, 0, sizeof(struct scsi_task));
260 
261     acb->task->xfer_dir = SCSI_XFER_WRITE;
262     acb->task->cdb_size = 16;
263     acb->task->cdb[0] = 0x8a;
264     if (!(bs->open_flags & BDRV_O_CACHE_WB)) {
265         /* set FUA on writes when cache mode is write through */
266         acb->task->cdb[1] |= 0x04;
267     }
268     lba = sector_qemu2lun(sector_num, iscsilun);
269     *(uint32_t *)&acb->task->cdb[2]  = htonl(lba >> 32);
270     *(uint32_t *)&acb->task->cdb[6]  = htonl(lba & 0xffffffff);
271     num_sectors = size / iscsilun->block_size;
272     *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
273     acb->task->expxferlen = size;
274 
275     data.data = acb->buf;
276     data.size = size;
277 
278     if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
279                                  iscsi_aio_write16_cb,
280                                  &data,
281                                  acb) != 0) {
282         scsi_free_scsi_task(acb->task);
283         g_free(acb->buf);
284         qemu_aio_release(acb);
285         return NULL;
286     }
287 
288     iscsi_set_events(iscsilun);
289 
290     return &acb->common;
291 }
292 
293 static void
294 iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
295                     void *command_data, void *opaque)
296 {
297     IscsiAIOCB *acb = opaque;
298 
299     trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
300 
301     if (acb->canceled != 0) {
302         qemu_aio_release(acb);
303         scsi_free_scsi_task(acb->task);
304         acb->task = NULL;
305         return;
306     }
307 
308     acb->status = 0;
309     if (status != 0) {
310         error_report("Failed to read16 data from iSCSI lun. %s",
311                      iscsi_get_error(iscsi));
312         acb->status = -EIO;
313     }
314 
315     iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
316     scsi_free_scsi_task(acb->task);
317     acb->task = NULL;
318 }
319 
320 static BlockDriverAIOCB *
321 iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
322                 QEMUIOVector *qiov, int nb_sectors,
323                 BlockDriverCompletionFunc *cb,
324                 void *opaque)
325 {
326     IscsiLun *iscsilun = bs->opaque;
327     struct iscsi_context *iscsi = iscsilun->iscsi;
328     IscsiAIOCB *acb;
329     size_t qemu_read_size;
330     int i;
331     uint64_t lba;
332     uint32_t num_sectors;
333 
334     qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
335 
336     acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
337     trace_iscsi_aio_readv(iscsi, sector_num, nb_sectors, opaque, acb);
338 
339     acb->iscsilun = iscsilun;
340     acb->qiov     = qiov;
341 
342     acb->canceled    = 0;
343     acb->read_size   = qemu_read_size;
344     acb->buf         = NULL;
345 
346     /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
347      * may be misaligned to the LUN, so we may need to read some extra
348      * data.
349      */
350     acb->read_offset = 0;
351     if (iscsilun->block_size > BDRV_SECTOR_SIZE) {
352         uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num;
353 
354         acb->read_offset  = bdrv_offset % iscsilun->block_size;
355     }
356 
357     num_sectors  = (qemu_read_size + iscsilun->block_size
358                     + acb->read_offset - 1)
359                     / iscsilun->block_size;
360 
361     acb->task = malloc(sizeof(struct scsi_task));
362     if (acb->task == NULL) {
363         error_report("iSCSI: Failed to allocate task for scsi READ16 "
364                      "command. %s", iscsi_get_error(iscsi));
365         qemu_aio_release(acb);
366         return NULL;
367     }
368     memset(acb->task, 0, sizeof(struct scsi_task));
369 
370     acb->task->xfer_dir = SCSI_XFER_READ;
371     lba = sector_qemu2lun(sector_num, iscsilun);
372     acb->task->expxferlen = qemu_read_size;
373 
374     switch (iscsilun->type) {
375     case TYPE_DISK:
376         acb->task->cdb_size = 16;
377         acb->task->cdb[0]  = 0x88;
378         *(uint32_t *)&acb->task->cdb[2]  = htonl(lba >> 32);
379         *(uint32_t *)&acb->task->cdb[6]  = htonl(lba & 0xffffffff);
380         *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
381         break;
382     default:
383         acb->task->cdb_size = 10;
384         acb->task->cdb[0]  = 0x28;
385         *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
386         *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
387         break;
388     }
389 
390     if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
391                                  iscsi_aio_read16_cb,
392                                  NULL,
393                                  acb) != 0) {
394         scsi_free_scsi_task(acb->task);
395         qemu_aio_release(acb);
396         return NULL;
397     }
398 
399     for (i = 0; i < acb->qiov->niov; i++) {
400         scsi_task_add_data_in_buffer(acb->task,
401                 acb->qiov->iov[i].iov_len,
402                 acb->qiov->iov[i].iov_base);
403     }
404 
405     iscsi_set_events(iscsilun);
406 
407     return &acb->common;
408 }
409 
410 
411 static void
412 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
413                      void *command_data, void *opaque)
414 {
415     IscsiAIOCB *acb = opaque;
416 
417     if (acb->canceled != 0) {
418         qemu_aio_release(acb);
419         scsi_free_scsi_task(acb->task);
420         acb->task = NULL;
421         return;
422     }
423 
424     acb->status = 0;
425     if (status < 0) {
426         error_report("Failed to sync10 data on iSCSI lun. %s",
427                      iscsi_get_error(iscsi));
428         acb->status = -EIO;
429     }
430 
431     iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
432     scsi_free_scsi_task(acb->task);
433     acb->task = NULL;
434 }
435 
436 static BlockDriverAIOCB *
437 iscsi_aio_flush(BlockDriverState *bs,
438                 BlockDriverCompletionFunc *cb, void *opaque)
439 {
440     IscsiLun *iscsilun = bs->opaque;
441     struct iscsi_context *iscsi = iscsilun->iscsi;
442     IscsiAIOCB *acb;
443 
444     acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
445 
446     acb->iscsilun = iscsilun;
447     acb->canceled   = 0;
448 
449     acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun,
450                                          0, 0, 0, 0,
451                                          iscsi_synccache10_cb,
452                                          acb);
453     if (acb->task == NULL) {
454         error_report("iSCSI: Failed to send synchronizecache10 command. %s",
455                      iscsi_get_error(iscsi));
456         qemu_aio_release(acb);
457         return NULL;
458     }
459 
460     iscsi_set_events(iscsilun);
461 
462     return &acb->common;
463 }
464 
465 static void
466 iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
467                      void *command_data, void *opaque)
468 {
469     IscsiAIOCB *acb = opaque;
470 
471     if (acb->canceled != 0) {
472         qemu_aio_release(acb);
473         scsi_free_scsi_task(acb->task);
474         acb->task = NULL;
475         return;
476     }
477 
478     acb->status = 0;
479     if (status < 0) {
480         error_report("Failed to unmap data on iSCSI lun. %s",
481                      iscsi_get_error(iscsi));
482         acb->status = -EIO;
483     }
484 
485     iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
486     scsi_free_scsi_task(acb->task);
487     acb->task = NULL;
488 }
489 
490 static BlockDriverAIOCB *
491 iscsi_aio_discard(BlockDriverState *bs,
492                   int64_t sector_num, int nb_sectors,
493                   BlockDriverCompletionFunc *cb, void *opaque)
494 {
495     IscsiLun *iscsilun = bs->opaque;
496     struct iscsi_context *iscsi = iscsilun->iscsi;
497     IscsiAIOCB *acb;
498     struct unmap_list list[1];
499 
500     acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
501 
502     acb->iscsilun = iscsilun;
503     acb->canceled   = 0;
504 
505     list[0].lba = sector_qemu2lun(sector_num, iscsilun);
506     list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size;
507 
508     acb->task = iscsi_unmap_task(iscsi, iscsilun->lun,
509                                  0, 0, &list[0], 1,
510                                  iscsi_unmap_cb,
511                                  acb);
512     if (acb->task == NULL) {
513         error_report("iSCSI: Failed to send unmap command. %s",
514                      iscsi_get_error(iscsi));
515         qemu_aio_release(acb);
516         return NULL;
517     }
518 
519     iscsi_set_events(iscsilun);
520 
521     return &acb->common;
522 }
523 
524 #ifdef __linux__
525 static void
526 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
527                      void *command_data, void *opaque)
528 {
529     IscsiAIOCB *acb = opaque;
530 
531     if (acb->canceled != 0) {
532         qemu_aio_release(acb);
533         scsi_free_scsi_task(acb->task);
534         acb->task = NULL;
535         return;
536     }
537 
538     acb->status = 0;
539     if (status < 0) {
540         error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
541                      iscsi_get_error(iscsi));
542         acb->status = -EIO;
543     }
544 
545     acb->ioh->driver_status = 0;
546     acb->ioh->host_status   = 0;
547     acb->ioh->resid         = 0;
548 
549 #define SG_ERR_DRIVER_SENSE    0x08
550 
551     if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
552         int ss;
553 
554         acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
555 
556         acb->ioh->sb_len_wr = acb->task->datain.size - 2;
557         ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
558              acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
559         memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
560     }
561 
562     iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
563     scsi_free_scsi_task(acb->task);
564     acb->task = NULL;
565 }
566 
567 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
568         unsigned long int req, void *buf,
569         BlockDriverCompletionFunc *cb, void *opaque)
570 {
571     IscsiLun *iscsilun = bs->opaque;
572     struct iscsi_context *iscsi = iscsilun->iscsi;
573     struct iscsi_data data;
574     IscsiAIOCB *acb;
575 
576     assert(req == SG_IO);
577 
578     acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
579 
580     acb->iscsilun = iscsilun;
581     acb->canceled    = 0;
582     acb->buf         = NULL;
583     acb->ioh         = buf;
584 
585     acb->task = malloc(sizeof(struct scsi_task));
586     if (acb->task == NULL) {
587         error_report("iSCSI: Failed to allocate task for scsi command. %s",
588                      iscsi_get_error(iscsi));
589         qemu_aio_release(acb);
590         return NULL;
591     }
592     memset(acb->task, 0, sizeof(struct scsi_task));
593 
594     switch (acb->ioh->dxfer_direction) {
595     case SG_DXFER_TO_DEV:
596         acb->task->xfer_dir = SCSI_XFER_WRITE;
597         break;
598     case SG_DXFER_FROM_DEV:
599         acb->task->xfer_dir = SCSI_XFER_READ;
600         break;
601     default:
602         acb->task->xfer_dir = SCSI_XFER_NONE;
603         break;
604     }
605 
606     acb->task->cdb_size = acb->ioh->cmd_len;
607     memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
608     acb->task->expxferlen = acb->ioh->dxfer_len;
609 
610     if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
611         data.data = acb->ioh->dxferp;
612         data.size = acb->ioh->dxfer_len;
613     }
614     if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
615                                  iscsi_aio_ioctl_cb,
616                                  (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
617                                      &data : NULL,
618                                  acb) != 0) {
619         scsi_free_scsi_task(acb->task);
620         qemu_aio_release(acb);
621         return NULL;
622     }
623 
624     /* tell libiscsi to read straight into the buffer we got from ioctl */
625     if (acb->task->xfer_dir == SCSI_XFER_READ) {
626         scsi_task_add_data_in_buffer(acb->task,
627                                      acb->ioh->dxfer_len,
628                                      acb->ioh->dxferp);
629     }
630 
631     iscsi_set_events(iscsilun);
632 
633     return &acb->common;
634 }
635 
636 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
637 {
638     IscsiLun *iscsilun = bs->opaque;
639 
640     switch (req) {
641     case SG_GET_VERSION_NUM:
642         *(int *)buf = 30000;
643         break;
644     case SG_GET_SCSI_ID:
645         ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
646         break;
647     default:
648         return -1;
649     }
650     return 0;
651 }
652 #endif
653 
654 static int64_t
655 iscsi_getlength(BlockDriverState *bs)
656 {
657     IscsiLun *iscsilun = bs->opaque;
658     int64_t len;
659 
660     len  = iscsilun->num_blocks;
661     len *= iscsilun->block_size;
662 
663     return len;
664 }
665 
666 static void
667 iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status,
668                         void *command_data, void *opaque)
669 {
670     struct IscsiTask *itask = opaque;
671     struct scsi_readcapacity16 *rc16;
672     struct scsi_task *task = command_data;
673 
674     if (status != 0) {
675         error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
676                      iscsi_get_error(iscsi));
677         itask->status   = 1;
678         itask->complete = 1;
679         scsi_free_scsi_task(task);
680         return;
681     }
682 
683     rc16 = scsi_datain_unmarshall(task);
684     if (rc16 == NULL) {
685         error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
686         itask->status   = 1;
687         itask->complete = 1;
688         scsi_free_scsi_task(task);
689         return;
690     }
691 
692     itask->iscsilun->block_size = rc16->block_length;
693     itask->iscsilun->num_blocks = rc16->returned_lba + 1;
694     itask->bs->total_sectors    = itask->iscsilun->num_blocks *
695                                itask->iscsilun->block_size / BDRV_SECTOR_SIZE ;
696 
697     itask->status   = 0;
698     itask->complete = 1;
699     scsi_free_scsi_task(task);
700 }
701 
702 static void
703 iscsi_readcapacity10_cb(struct iscsi_context *iscsi, int status,
704                         void *command_data, void *opaque)
705 {
706     struct IscsiTask *itask = opaque;
707     struct scsi_readcapacity10 *rc10;
708     struct scsi_task *task = command_data;
709 
710     if (status != 0) {
711         error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
712                      iscsi_get_error(iscsi));
713         itask->status   = 1;
714         itask->complete = 1;
715         scsi_free_scsi_task(task);
716         return;
717     }
718 
719     rc10 = scsi_datain_unmarshall(task);
720     if (rc10 == NULL) {
721         error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
722         itask->status   = 1;
723         itask->complete = 1;
724         scsi_free_scsi_task(task);
725         return;
726     }
727 
728     itask->iscsilun->block_size = rc10->block_size;
729     itask->iscsilun->num_blocks = rc10->lba + 1;
730     itask->bs->total_sectors    = itask->iscsilun->num_blocks *
731                                itask->iscsilun->block_size / BDRV_SECTOR_SIZE ;
732 
733     itask->status   = 0;
734     itask->complete = 1;
735     scsi_free_scsi_task(task);
736 }
737 
738 static void
739 iscsi_inquiry_cb(struct iscsi_context *iscsi, int status, void *command_data,
740                  void *opaque)
741 {
742     struct IscsiTask *itask = opaque;
743     struct scsi_task *task = command_data;
744     struct scsi_inquiry_standard *inq;
745 
746     if (status != 0) {
747         itask->status   = 1;
748         itask->complete = 1;
749         scsi_free_scsi_task(task);
750         return;
751     }
752 
753     inq = scsi_datain_unmarshall(task);
754     if (inq == NULL) {
755         error_report("iSCSI: Failed to unmarshall inquiry data.");
756         itask->status   = 1;
757         itask->complete = 1;
758         scsi_free_scsi_task(task);
759         return;
760     }
761 
762     itask->iscsilun->type = inq->periperal_device_type;
763 
764     scsi_free_scsi_task(task);
765 
766     switch (itask->iscsilun->type) {
767     case TYPE_DISK:
768         task = iscsi_readcapacity16_task(iscsi, itask->iscsilun->lun,
769                                    iscsi_readcapacity16_cb, opaque);
770         if (task == NULL) {
771             error_report("iSCSI: failed to send readcapacity16 command.");
772             itask->status   = 1;
773             itask->complete = 1;
774             return;
775         }
776         break;
777     case TYPE_ROM:
778         task = iscsi_readcapacity10_task(iscsi, itask->iscsilun->lun,
779                                    0, 0,
780                                    iscsi_readcapacity10_cb, opaque);
781         if (task == NULL) {
782             error_report("iSCSI: failed to send readcapacity16 command.");
783             itask->status   = 1;
784             itask->complete = 1;
785             return;
786         }
787         break;
788     default:
789         itask->status   = 0;
790         itask->complete = 1;
791     }
792 }
793 
794 static void
795 iscsi_connect_cb(struct iscsi_context *iscsi, int status, void *command_data,
796                  void *opaque)
797 {
798     struct IscsiTask *itask = opaque;
799     struct scsi_task *task;
800 
801     if (status != 0) {
802         itask->status   = 1;
803         itask->complete = 1;
804         return;
805     }
806 
807     task = iscsi_inquiry_task(iscsi, itask->iscsilun->lun,
808                               0, 0, 36,
809                               iscsi_inquiry_cb, opaque);
810     if (task == NULL) {
811         error_report("iSCSI: failed to send inquiry command.");
812         itask->status   = 1;
813         itask->complete = 1;
814         return;
815     }
816 }
817 
818 static int parse_chap(struct iscsi_context *iscsi, const char *target)
819 {
820     QemuOptsList *list;
821     QemuOpts *opts;
822     const char *user = NULL;
823     const char *password = NULL;
824 
825     list = qemu_find_opts("iscsi");
826     if (!list) {
827         return 0;
828     }
829 
830     opts = qemu_opts_find(list, target);
831     if (opts == NULL) {
832         opts = QTAILQ_FIRST(&list->head);
833         if (!opts) {
834             return 0;
835         }
836     }
837 
838     user = qemu_opt_get(opts, "user");
839     if (!user) {
840         return 0;
841     }
842 
843     password = qemu_opt_get(opts, "password");
844     if (!password) {
845         error_report("CHAP username specified but no password was given");
846         return -1;
847     }
848 
849     if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
850         error_report("Failed to set initiator username and password");
851         return -1;
852     }
853 
854     return 0;
855 }
856 
857 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
858 {
859     QemuOptsList *list;
860     QemuOpts *opts;
861     const char *digest = NULL;
862 
863     list = qemu_find_opts("iscsi");
864     if (!list) {
865         return;
866     }
867 
868     opts = qemu_opts_find(list, target);
869     if (opts == NULL) {
870         opts = QTAILQ_FIRST(&list->head);
871         if (!opts) {
872             return;
873         }
874     }
875 
876     digest = qemu_opt_get(opts, "header-digest");
877     if (!digest) {
878         return;
879     }
880 
881     if (!strcmp(digest, "CRC32C")) {
882         iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
883     } else if (!strcmp(digest, "NONE")) {
884         iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
885     } else if (!strcmp(digest, "CRC32C-NONE")) {
886         iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
887     } else if (!strcmp(digest, "NONE-CRC32C")) {
888         iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
889     } else {
890         error_report("Invalid header-digest setting : %s", digest);
891     }
892 }
893 
894 static char *parse_initiator_name(const char *target)
895 {
896     QemuOptsList *list;
897     QemuOpts *opts;
898     const char *name = NULL;
899 
900     list = qemu_find_opts("iscsi");
901     if (!list) {
902         return g_strdup("iqn.2008-11.org.linux-kvm");
903     }
904 
905     opts = qemu_opts_find(list, target);
906     if (opts == NULL) {
907         opts = QTAILQ_FIRST(&list->head);
908         if (!opts) {
909             return g_strdup("iqn.2008-11.org.linux-kvm");
910         }
911     }
912 
913     name = qemu_opt_get(opts, "initiator-name");
914     if (!name) {
915         return g_strdup("iqn.2008-11.org.linux-kvm");
916     }
917 
918     return g_strdup(name);
919 }
920 
921 /*
922  * We support iscsi url's on the form
923  * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
924  */
925 static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)
926 {
927     IscsiLun *iscsilun = bs->opaque;
928     struct iscsi_context *iscsi = NULL;
929     struct iscsi_url *iscsi_url = NULL;
930     struct IscsiTask task;
931     char *initiator_name = NULL;
932     int ret;
933 
934     if ((BDRV_SECTOR_SIZE % 512) != 0) {
935         error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
936                      "BDRV_SECTOR_SIZE(%lld) is not a multiple "
937                      "of 512", BDRV_SECTOR_SIZE);
938         return -EINVAL;
939     }
940 
941     iscsi_url = iscsi_parse_full_url(iscsi, filename);
942     if (iscsi_url == NULL) {
943         error_report("Failed to parse URL : %s %s", filename,
944                      iscsi_get_error(iscsi));
945         ret = -EINVAL;
946         goto failed;
947     }
948 
949     memset(iscsilun, 0, sizeof(IscsiLun));
950 
951     initiator_name = parse_initiator_name(iscsi_url->target);
952 
953     iscsi = iscsi_create_context(initiator_name);
954     if (iscsi == NULL) {
955         error_report("iSCSI: Failed to create iSCSI context.");
956         ret = -ENOMEM;
957         goto failed;
958     }
959 
960     if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
961         error_report("iSCSI: Failed to set target name.");
962         ret = -EINVAL;
963         goto failed;
964     }
965 
966     if (iscsi_url->user != NULL) {
967         ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
968                                               iscsi_url->passwd);
969         if (ret != 0) {
970             error_report("Failed to set initiator username and password");
971             ret = -EINVAL;
972             goto failed;
973         }
974     }
975 
976     /* check if we got CHAP username/password via the options */
977     if (parse_chap(iscsi, iscsi_url->target) != 0) {
978         error_report("iSCSI: Failed to set CHAP user/password");
979         ret = -EINVAL;
980         goto failed;
981     }
982 
983     if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
984         error_report("iSCSI: Failed to set session type to normal.");
985         ret = -EINVAL;
986         goto failed;
987     }
988 
989     iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
990 
991     /* check if we got HEADER_DIGEST via the options */
992     parse_header_digest(iscsi, iscsi_url->target);
993 
994     task.iscsilun = iscsilun;
995     task.status = 0;
996     task.complete = 0;
997     task.bs = bs;
998 
999     iscsilun->iscsi = iscsi;
1000     iscsilun->lun   = iscsi_url->lun;
1001 
1002     if (iscsi_full_connect_async(iscsi, iscsi_url->portal, iscsi_url->lun,
1003                                  iscsi_connect_cb, &task)
1004         != 0) {
1005         error_report("iSCSI: Failed to start async connect.");
1006         ret = -EINVAL;
1007         goto failed;
1008     }
1009 
1010     while (!task.complete) {
1011         iscsi_set_events(iscsilun);
1012         qemu_aio_wait();
1013     }
1014     if (task.status != 0) {
1015         error_report("iSCSI: Failed to connect to LUN : %s",
1016                      iscsi_get_error(iscsi));
1017         ret = -EINVAL;
1018         goto failed;
1019     }
1020 
1021     if (iscsi_url != NULL) {
1022         iscsi_destroy_url(iscsi_url);
1023     }
1024 
1025     /* Medium changer or tape. We dont have any emulation for this so this must
1026      * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1027      * to read from the device to guess the image format.
1028      */
1029     if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1030         iscsilun->type == TYPE_TAPE) {
1031         bs->sg = 1;
1032     }
1033 
1034     return 0;
1035 
1036 failed:
1037     if (initiator_name != NULL) {
1038         g_free(initiator_name);
1039     }
1040     if (iscsi_url != NULL) {
1041         iscsi_destroy_url(iscsi_url);
1042     }
1043     if (iscsi != NULL) {
1044         iscsi_destroy_context(iscsi);
1045     }
1046     memset(iscsilun, 0, sizeof(IscsiLun));
1047     return ret;
1048 }
1049 
1050 static void iscsi_close(BlockDriverState *bs)
1051 {
1052     IscsiLun *iscsilun = bs->opaque;
1053     struct iscsi_context *iscsi = iscsilun->iscsi;
1054 
1055     qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
1056     iscsi_destroy_context(iscsi);
1057     memset(iscsilun, 0, sizeof(IscsiLun));
1058 }
1059 
1060 static BlockDriver bdrv_iscsi = {
1061     .format_name     = "iscsi",
1062     .protocol_name   = "iscsi",
1063 
1064     .instance_size   = sizeof(IscsiLun),
1065     .bdrv_file_open  = iscsi_open,
1066     .bdrv_close      = iscsi_close,
1067 
1068     .bdrv_getlength  = iscsi_getlength,
1069 
1070     .bdrv_aio_readv  = iscsi_aio_readv,
1071     .bdrv_aio_writev = iscsi_aio_writev,
1072     .bdrv_aio_flush  = iscsi_aio_flush,
1073 
1074     .bdrv_aio_discard = iscsi_aio_discard,
1075 
1076 #ifdef __linux__
1077     .bdrv_ioctl       = iscsi_ioctl,
1078     .bdrv_aio_ioctl   = iscsi_aio_ioctl,
1079 #endif
1080 };
1081 
1082 static void iscsi_block_init(void)
1083 {
1084     bdrv_register(&bdrv_iscsi);
1085 }
1086 
1087 block_init(iscsi_block_init);
1088