]> Git Repo - qemu.git/blob - block/iscsi.c
Merge remote-tracking branch 'kwolf/for-anthony' into staging
[qemu.git] / block / iscsi.c
1 /*
2  * QEMU Block driver for iSCSI images
3  *
4  * Copyright (c) 2010-2011 Ronnie Sahlberg <[email protected]>
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_buffer(acb->qiov, acb->buf);
251
252
253     acb->task = malloc(sizeof(struct scsi_task));
254     if (acb->task == NULL) {
255         error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
256                      "command. %s", iscsi_get_error(iscsi));
257         qemu_aio_release(acb);
258         return NULL;
259     }
260     memset(acb->task, 0, sizeof(struct scsi_task));
261
262     acb->task->xfer_dir = SCSI_XFER_WRITE;
263     acb->task->cdb_size = 16;
264     acb->task->cdb[0] = 0x8a;
265     if (!(bs->open_flags & BDRV_O_CACHE_WB)) {
266         /* set FUA on writes when cache mode is write through */
267         acb->task->cdb[1] |= 0x04;
268     }
269     lba = sector_qemu2lun(sector_num, iscsilun);
270     *(uint32_t *)&acb->task->cdb[2]  = htonl(lba >> 32);
271     *(uint32_t *)&acb->task->cdb[6]  = htonl(lba & 0xffffffff);
272     num_sectors = size / iscsilun->block_size;
273     *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
274     acb->task->expxferlen = size;
275
276     data.data = acb->buf;
277     data.size = size;
278
279     if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
280                                  iscsi_aio_write16_cb,
281                                  &data,
282                                  acb) != 0) {
283         scsi_free_scsi_task(acb->task);
284         g_free(acb->buf);
285         qemu_aio_release(acb);
286         return NULL;
287     }
288
289     iscsi_set_events(iscsilun);
290
291     return &acb->common;
292 }
293
294 static void
295 iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
296                     void *command_data, void *opaque)
297 {
298     IscsiAIOCB *acb = opaque;
299
300     trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
301
302     if (acb->canceled != 0) {
303         qemu_aio_release(acb);
304         scsi_free_scsi_task(acb->task);
305         acb->task = NULL;
306         return;
307     }
308
309     acb->status = 0;
310     if (status != 0) {
311         error_report("Failed to read16 data from iSCSI lun. %s",
312                      iscsi_get_error(iscsi));
313         acb->status = -EIO;
314     }
315
316     iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
317     scsi_free_scsi_task(acb->task);
318     acb->task = NULL;
319 }
320
321 static BlockDriverAIOCB *
322 iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
323                 QEMUIOVector *qiov, int nb_sectors,
324                 BlockDriverCompletionFunc *cb,
325                 void *opaque)
326 {
327     IscsiLun *iscsilun = bs->opaque;
328     struct iscsi_context *iscsi = iscsilun->iscsi;
329     IscsiAIOCB *acb;
330     size_t qemu_read_size;
331     int i;
332     uint64_t lba;
333     uint32_t num_sectors;
334
335     qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
336
337     acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
338     trace_iscsi_aio_readv(iscsi, sector_num, nb_sectors, opaque, acb);
339
340     acb->iscsilun = iscsilun;
341     acb->qiov     = qiov;
342
343     acb->canceled    = 0;
344     acb->read_size   = qemu_read_size;
345     acb->buf         = NULL;
346
347     /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
348      * may be misaligned to the LUN, so we may need to read some extra
349      * data.
350      */
351     acb->read_offset = 0;
352     if (iscsilun->block_size > BDRV_SECTOR_SIZE) {
353         uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num;
354
355         acb->read_offset  = bdrv_offset % iscsilun->block_size;
356     }
357
358     num_sectors  = (qemu_read_size + iscsilun->block_size
359                     + acb->read_offset - 1)
360                     / iscsilun->block_size;
361
362     acb->task = malloc(sizeof(struct scsi_task));
363     if (acb->task == NULL) {
364         error_report("iSCSI: Failed to allocate task for scsi READ16 "
365                      "command. %s", iscsi_get_error(iscsi));
366         qemu_aio_release(acb);
367         return NULL;
368     }
369     memset(acb->task, 0, sizeof(struct scsi_task));
370
371     acb->task->xfer_dir = SCSI_XFER_READ;
372     lba = sector_qemu2lun(sector_num, iscsilun);
373     acb->task->expxferlen = qemu_read_size;
374
375     switch (iscsilun->type) {
376     case TYPE_DISK:
377         acb->task->cdb_size = 16;
378         acb->task->cdb[0]  = 0x88;
379         *(uint32_t *)&acb->task->cdb[2]  = htonl(lba >> 32);
380         *(uint32_t *)&acb->task->cdb[6]  = htonl(lba & 0xffffffff);
381         *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
382         break;
383     default:
384         acb->task->cdb_size = 10;
385         acb->task->cdb[0]  = 0x28;
386         *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
387         *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
388         break;
389     }
390     
391     if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
392                                  iscsi_aio_read16_cb,
393                                  NULL,
394                                  acb) != 0) {
395         scsi_free_scsi_task(acb->task);
396         qemu_aio_release(acb);
397         return NULL;
398     }
399
400     for (i = 0; i < acb->qiov->niov; i++) {
401         scsi_task_add_data_in_buffer(acb->task,
402                 acb->qiov->iov[i].iov_len,
403                 acb->qiov->iov[i].iov_base);
404     }
405
406     iscsi_set_events(iscsilun);
407
408     return &acb->common;
409 }
410
411
412 static void
413 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
414                      void *command_data, void *opaque)
415 {
416     IscsiAIOCB *acb = opaque;
417
418     if (acb->canceled != 0) {
419         qemu_aio_release(acb);
420         scsi_free_scsi_task(acb->task);
421         acb->task = NULL;
422         return;
423     }
424
425     acb->status = 0;
426     if (status < 0) {
427         error_report("Failed to sync10 data on iSCSI lun. %s",
428                      iscsi_get_error(iscsi));
429         acb->status = -EIO;
430     }
431
432     iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
433     scsi_free_scsi_task(acb->task);
434     acb->task = NULL;
435 }
436
437 static BlockDriverAIOCB *
438 iscsi_aio_flush(BlockDriverState *bs,
439                 BlockDriverCompletionFunc *cb, void *opaque)
440 {
441     IscsiLun *iscsilun = bs->opaque;
442     struct iscsi_context *iscsi = iscsilun->iscsi;
443     IscsiAIOCB *acb;
444
445     acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
446
447     acb->iscsilun = iscsilun;
448     acb->canceled   = 0;
449
450     acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun,
451                                          0, 0, 0, 0,
452                                          iscsi_synccache10_cb,
453                                          acb);
454     if (acb->task == NULL) {
455         error_report("iSCSI: Failed to send synchronizecache10 command. %s",
456                      iscsi_get_error(iscsi));
457         qemu_aio_release(acb);
458         return NULL;
459     }
460
461     iscsi_set_events(iscsilun);
462
463     return &acb->common;
464 }
465
466 static void
467 iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
468                      void *command_data, void *opaque)
469 {
470     IscsiAIOCB *acb = opaque;
471
472     if (acb->canceled != 0) {
473         qemu_aio_release(acb);
474         scsi_free_scsi_task(acb->task);
475         acb->task = NULL;
476         return;
477     }
478
479     acb->status = 0;
480     if (status < 0) {
481         error_report("Failed to unmap data on iSCSI lun. %s",
482                      iscsi_get_error(iscsi));
483         acb->status = -EIO;
484     }
485
486     iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
487     scsi_free_scsi_task(acb->task);
488     acb->task = NULL;
489 }
490
491 static BlockDriverAIOCB *
492 iscsi_aio_discard(BlockDriverState *bs,
493                   int64_t sector_num, int nb_sectors,
494                   BlockDriverCompletionFunc *cb, void *opaque)
495 {
496     IscsiLun *iscsilun = bs->opaque;
497     struct iscsi_context *iscsi = iscsilun->iscsi;
498     IscsiAIOCB *acb;
499     struct unmap_list list[1];
500
501     acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
502
503     acb->iscsilun = iscsilun;
504     acb->canceled   = 0;
505
506     list[0].lba = sector_qemu2lun(sector_num, iscsilun);
507     list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size;
508
509     acb->task = iscsi_unmap_task(iscsi, iscsilun->lun,
510                                  0, 0, &list[0], 1,
511                                  iscsi_unmap_cb,
512                                  acb);
513     if (acb->task == NULL) {
514         error_report("iSCSI: Failed to send unmap command. %s",
515                      iscsi_get_error(iscsi));
516         qemu_aio_release(acb);
517         return NULL;
518     }
519
520     iscsi_set_events(iscsilun);
521
522     return &acb->common;
523 }
524
525 #ifdef __linux__
526 static void
527 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
528                      void *command_data, void *opaque)
529 {
530     IscsiAIOCB *acb = opaque;
531
532     if (acb->canceled != 0) {
533         qemu_aio_release(acb);
534         scsi_free_scsi_task(acb->task);
535         acb->task = NULL;
536         return;
537     }
538
539     acb->status = 0;
540     if (status < 0) {
541         error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
542                      iscsi_get_error(iscsi));
543         acb->status = -EIO;
544     }
545
546     acb->ioh->driver_status = 0;
547     acb->ioh->host_status   = 0;
548     acb->ioh->resid         = 0;
549
550 #define SG_ERR_DRIVER_SENSE    0x08
551
552     if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
553         int ss;
554
555         acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
556
557         acb->ioh->sb_len_wr = acb->task->datain.size - 2;
558         ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
559              acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
560         memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
561     }
562
563     iscsi_schedule_bh(iscsi_readv_writev_bh_cb, acb);
564     scsi_free_scsi_task(acb->task);
565     acb->task = NULL;
566 }
567
568 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
569         unsigned long int req, void *buf,
570         BlockDriverCompletionFunc *cb, void *opaque)
571 {
572     IscsiLun *iscsilun = bs->opaque;
573     struct iscsi_context *iscsi = iscsilun->iscsi;
574     struct iscsi_data data;
575     IscsiAIOCB *acb;
576
577     assert(req == SG_IO);
578
579     acb = qemu_aio_get(&iscsi_aio_pool, bs, cb, opaque);
580
581     acb->iscsilun = iscsilun;
582     acb->canceled    = 0;
583     acb->buf         = NULL;
584     acb->ioh         = buf;
585
586     acb->task = malloc(sizeof(struct scsi_task));
587     if (acb->task == NULL) {
588         error_report("iSCSI: Failed to allocate task for scsi command. %s",
589                      iscsi_get_error(iscsi));
590         qemu_aio_release(acb);
591         return NULL;
592     }
593     memset(acb->task, 0, sizeof(struct scsi_task));
594
595     switch (acb->ioh->dxfer_direction) {
596     case SG_DXFER_TO_DEV:
597         acb->task->xfer_dir = SCSI_XFER_WRITE;
598         break;
599     case SG_DXFER_FROM_DEV:
600         acb->task->xfer_dir = SCSI_XFER_READ;
601         break;
602     default:
603         acb->task->xfer_dir = SCSI_XFER_NONE;
604         break;
605     }
606
607     acb->task->cdb_size = acb->ioh->cmd_len;
608     memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
609     acb->task->expxferlen = acb->ioh->dxfer_len;
610
611     if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
612         data.data = acb->ioh->dxferp;
613         data.size = acb->ioh->dxfer_len;
614     }
615     if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
616                                  iscsi_aio_ioctl_cb,
617                                  (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
618                                      &data : NULL,
619                                  acb) != 0) {
620         scsi_free_scsi_task(acb->task);
621         qemu_aio_release(acb);
622         return NULL;
623     }
624
625     /* tell libiscsi to read straight into the buffer we got from ioctl */
626     if (acb->task->xfer_dir == SCSI_XFER_READ) {
627         scsi_task_add_data_in_buffer(acb->task,
628                                      acb->ioh->dxfer_len,
629                                      acb->ioh->dxferp);
630     }
631
632     iscsi_set_events(iscsilun);
633
634     return &acb->common;
635 }
636
637 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
638 {
639     IscsiLun *iscsilun = bs->opaque;
640
641     switch (req) {
642     case SG_GET_VERSION_NUM:
643         *(int *)buf = 30000;
644         break;
645     case SG_GET_SCSI_ID:
646         ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
647         break;
648     default:
649         return -1;
650     }
651     return 0;
652 }
653 #endif
654
655 static int64_t
656 iscsi_getlength(BlockDriverState *bs)
657 {
658     IscsiLun *iscsilun = bs->opaque;
659     int64_t len;
660
661     len  = iscsilun->num_blocks;
662     len *= iscsilun->block_size;
663
664     return len;
665 }
666
667 static void
668 iscsi_readcapacity16_cb(struct iscsi_context *iscsi, int status,
669                         void *command_data, void *opaque)
670 {
671     struct IscsiTask *itask = opaque;
672     struct scsi_readcapacity16 *rc16;
673     struct scsi_task *task = command_data;
674
675     if (status != 0) {
676         error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
677                      iscsi_get_error(iscsi));
678         itask->status   = 1;
679         itask->complete = 1;
680         scsi_free_scsi_task(task);
681         return;
682     }
683
684     rc16 = scsi_datain_unmarshall(task);
685     if (rc16 == NULL) {
686         error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
687         itask->status   = 1;
688         itask->complete = 1;
689         scsi_free_scsi_task(task);
690         return;
691     }
692
693     itask->iscsilun->block_size = rc16->block_length;
694     itask->iscsilun->num_blocks = rc16->returned_lba + 1;
695     itask->bs->total_sectors    = itask->iscsilun->num_blocks *
696                                itask->iscsilun->block_size / BDRV_SECTOR_SIZE ;
697
698     itask->status   = 0;
699     itask->complete = 1;
700     scsi_free_scsi_task(task);
701 }
702
703 static void
704 iscsi_readcapacity10_cb(struct iscsi_context *iscsi, int status,
705                         void *command_data, void *opaque)
706 {
707     struct IscsiTask *itask = opaque;
708     struct scsi_readcapacity10 *rc10;
709     struct scsi_task *task = command_data;
710
711     if (status != 0) {
712         error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
713                      iscsi_get_error(iscsi));
714         itask->status   = 1;
715         itask->complete = 1;
716         scsi_free_scsi_task(task);
717         return;
718     }
719
720     rc10 = scsi_datain_unmarshall(task);
721     if (rc10 == NULL) {
722         error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
723         itask->status   = 1;
724         itask->complete = 1;
725         scsi_free_scsi_task(task);
726         return;
727     }
728
729     itask->iscsilun->block_size = rc10->block_size;
730     itask->iscsilun->num_blocks = rc10->lba + 1;
731     itask->bs->total_sectors    = itask->iscsilun->num_blocks *
732                                itask->iscsilun->block_size / BDRV_SECTOR_SIZE ;
733
734     itask->status   = 0;
735     itask->complete = 1;
736     scsi_free_scsi_task(task);
737 }
738
739 static void
740 iscsi_inquiry_cb(struct iscsi_context *iscsi, int status, void *command_data,
741                  void *opaque)
742 {
743     struct IscsiTask *itask = opaque;
744     struct scsi_task *task = command_data;
745     struct scsi_inquiry_standard *inq;
746
747     if (status != 0) {
748         itask->status   = 1;
749         itask->complete = 1;
750         scsi_free_scsi_task(task);
751         return;
752     }
753
754     inq = scsi_datain_unmarshall(task);
755     if (inq == NULL) {
756         error_report("iSCSI: Failed to unmarshall inquiry data.");
757         itask->status   = 1;
758         itask->complete = 1;
759         scsi_free_scsi_task(task);
760         return;
761     }
762
763     itask->iscsilun->type = inq->periperal_device_type;
764
765     scsi_free_scsi_task(task);
766
767     switch (itask->iscsilun->type) {
768     case TYPE_DISK:
769         task = iscsi_readcapacity16_task(iscsi, itask->iscsilun->lun,
770                                    iscsi_readcapacity16_cb, opaque);
771         if (task == NULL) {
772             error_report("iSCSI: failed to send readcapacity16 command.");
773             itask->status   = 1;
774             itask->complete = 1;
775             return;
776         }
777         break;
778     case TYPE_ROM:
779         task = iscsi_readcapacity10_task(iscsi, itask->iscsilun->lun,
780                                    0, 0,
781                                    iscsi_readcapacity10_cb, opaque);
782         if (task == NULL) {
783             error_report("iSCSI: failed to send readcapacity16 command.");
784             itask->status   = 1;
785             itask->complete = 1;
786             return;
787         }
788         break;
789     default:
790         itask->status   = 0;
791         itask->complete = 1;
792     }
793 }
794
795 static void
796 iscsi_connect_cb(struct iscsi_context *iscsi, int status, void *command_data,
797                  void *opaque)
798 {
799     struct IscsiTask *itask = opaque;
800     struct scsi_task *task;
801
802     if (status != 0) {
803         itask->status   = 1;
804         itask->complete = 1;
805         return;
806     }
807
808     task = iscsi_inquiry_task(iscsi, itask->iscsilun->lun,
809                               0, 0, 36,
810                               iscsi_inquiry_cb, opaque);
811     if (task == NULL) {
812         error_report("iSCSI: failed to send inquiry command.");
813         itask->status   = 1;
814         itask->complete = 1;
815         return;
816     }
817 }
818
819 static int parse_chap(struct iscsi_context *iscsi, const char *target)
820 {
821     QemuOptsList *list;
822     QemuOpts *opts;
823     const char *user = NULL;
824     const char *password = NULL;
825
826     list = qemu_find_opts("iscsi");
827     if (!list) {
828         return 0;
829     }
830
831     opts = qemu_opts_find(list, target);
832     if (opts == NULL) {
833         opts = QTAILQ_FIRST(&list->head);
834         if (!opts) {
835             return 0;
836         }
837     }
838
839     user = qemu_opt_get(opts, "user");
840     if (!user) {
841         return 0;
842     }
843
844     password = qemu_opt_get(opts, "password");
845     if (!password) {
846         error_report("CHAP username specified but no password was given");
847         return -1;
848     }
849
850     if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
851         error_report("Failed to set initiator username and password");
852         return -1;
853     }
854
855     return 0;
856 }
857
858 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
859 {
860     QemuOptsList *list;
861     QemuOpts *opts;
862     const char *digest = NULL;
863
864     list = qemu_find_opts("iscsi");
865     if (!list) {
866         return;
867     }
868
869     opts = qemu_opts_find(list, target);
870     if (opts == NULL) {
871         opts = QTAILQ_FIRST(&list->head);
872         if (!opts) {
873             return;
874         }
875     }
876
877     digest = qemu_opt_get(opts, "header-digest");
878     if (!digest) {
879         return;
880     }
881
882     if (!strcmp(digest, "CRC32C")) {
883         iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
884     } else if (!strcmp(digest, "NONE")) {
885         iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
886     } else if (!strcmp(digest, "CRC32C-NONE")) {
887         iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
888     } else if (!strcmp(digest, "NONE-CRC32C")) {
889         iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
890     } else {
891         error_report("Invalid header-digest setting : %s", digest);
892     }
893 }
894
895 static char *parse_initiator_name(const char *target)
896 {
897     QemuOptsList *list;
898     QemuOpts *opts;
899     const char *name = NULL;
900
901     list = qemu_find_opts("iscsi");
902     if (!list) {
903         return g_strdup("iqn.2008-11.org.linux-kvm");
904     }
905
906     opts = qemu_opts_find(list, target);
907     if (opts == NULL) {
908         opts = QTAILQ_FIRST(&list->head);
909         if (!opts) {
910             return g_strdup("iqn.2008-11.org.linux-kvm");
911         }
912     }
913
914     name = qemu_opt_get(opts, "initiator-name");
915     if (!name) {
916         return g_strdup("iqn.2008-11.org.linux-kvm");
917     }
918
919     return g_strdup(name);
920 }
921
922 /*
923  * We support iscsi url's on the form
924  * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
925  */
926 static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)
927 {
928     IscsiLun *iscsilun = bs->opaque;
929     struct iscsi_context *iscsi = NULL;
930     struct iscsi_url *iscsi_url = NULL;
931     struct IscsiTask task;
932     char *initiator_name = NULL;
933     int ret;
934
935     if ((BDRV_SECTOR_SIZE % 512) != 0) {
936         error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
937                      "BDRV_SECTOR_SIZE(%lld) is not a multiple "
938                      "of 512", BDRV_SECTOR_SIZE);
939         return -EINVAL;
940     }
941
942     iscsi_url = iscsi_parse_full_url(iscsi, filename);
943     if (iscsi_url == NULL) {
944         error_report("Failed to parse URL : %s %s", filename,
945                      iscsi_get_error(iscsi));
946         ret = -EINVAL;
947         goto failed;
948     }
949
950     memset(iscsilun, 0, sizeof(IscsiLun));
951
952     initiator_name = parse_initiator_name(iscsi_url->target);
953
954     iscsi = iscsi_create_context(initiator_name);
955     if (iscsi == NULL) {
956         error_report("iSCSI: Failed to create iSCSI context.");
957         ret = -ENOMEM;
958         goto failed;
959     }
960
961     if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
962         error_report("iSCSI: Failed to set target name.");
963         ret = -EINVAL;
964         goto failed;
965     }
966
967     if (iscsi_url->user != NULL) {
968         ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
969                                               iscsi_url->passwd);
970         if (ret != 0) {
971             error_report("Failed to set initiator username and password");
972             ret = -EINVAL;
973             goto failed;
974         }
975     }
976
977     /* check if we got CHAP username/password via the options */
978     if (parse_chap(iscsi, iscsi_url->target) != 0) {
979         error_report("iSCSI: Failed to set CHAP user/password");
980         ret = -EINVAL;
981         goto failed;
982     }
983
984     if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
985         error_report("iSCSI: Failed to set session type to normal.");
986         ret = -EINVAL;
987         goto failed;
988     }
989
990     iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
991
992     /* check if we got HEADER_DIGEST via the options */
993     parse_header_digest(iscsi, iscsi_url->target);
994
995     task.iscsilun = iscsilun;
996     task.status = 0;
997     task.complete = 0;
998     task.bs = bs;
999
1000     iscsilun->iscsi = iscsi;
1001     iscsilun->lun   = iscsi_url->lun;
1002
1003     if (iscsi_full_connect_async(iscsi, iscsi_url->portal, iscsi_url->lun,
1004                                  iscsi_connect_cb, &task)
1005         != 0) {
1006         error_report("iSCSI: Failed to start async connect.");
1007         ret = -EINVAL;
1008         goto failed;
1009     }
1010
1011     while (!task.complete) {
1012         iscsi_set_events(iscsilun);
1013         qemu_aio_wait();
1014     }
1015     if (task.status != 0) {
1016         error_report("iSCSI: Failed to connect to LUN : %s",
1017                      iscsi_get_error(iscsi));
1018         ret = -EINVAL;
1019         goto failed;
1020     }
1021
1022     if (iscsi_url != NULL) {
1023         iscsi_destroy_url(iscsi_url);
1024     }
1025
1026     /* Medium changer or tape. We dont have any emulation for this so this must
1027      * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1028      * to read from the device to guess the image format.
1029      */
1030     if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1031         iscsilun->type == TYPE_TAPE) {
1032         bs->sg = 1;
1033     }
1034
1035     return 0;
1036
1037 failed:
1038     if (initiator_name != NULL) {
1039         g_free(initiator_name);
1040     }
1041     if (iscsi_url != NULL) {
1042         iscsi_destroy_url(iscsi_url);
1043     }
1044     if (iscsi != NULL) {
1045         iscsi_destroy_context(iscsi);
1046     }
1047     memset(iscsilun, 0, sizeof(IscsiLun));
1048     return ret;
1049 }
1050
1051 static void iscsi_close(BlockDriverState *bs)
1052 {
1053     IscsiLun *iscsilun = bs->opaque;
1054     struct iscsi_context *iscsi = iscsilun->iscsi;
1055
1056     qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
1057     iscsi_destroy_context(iscsi);
1058     memset(iscsilun, 0, sizeof(IscsiLun));
1059 }
1060
1061 static BlockDriver bdrv_iscsi = {
1062     .format_name     = "iscsi",
1063     .protocol_name   = "iscsi",
1064
1065     .instance_size   = sizeof(IscsiLun),
1066     .bdrv_file_open  = iscsi_open,
1067     .bdrv_close      = iscsi_close,
1068
1069     .bdrv_getlength  = iscsi_getlength,
1070
1071     .bdrv_aio_readv  = iscsi_aio_readv,
1072     .bdrv_aio_writev = iscsi_aio_writev,
1073     .bdrv_aio_flush  = iscsi_aio_flush,
1074
1075     .bdrv_aio_discard = iscsi_aio_discard,
1076
1077 #ifdef __linux__
1078     .bdrv_ioctl       = iscsi_ioctl,
1079     .bdrv_aio_ioctl   = iscsi_aio_ioctl,
1080 #endif
1081 };
1082
1083 static void iscsi_block_init(void)
1084 {
1085     bdrv_register(&bdrv_iscsi);
1086 }
1087
1088 block_init(iscsi_block_init);
This page took 0.084944 seconds and 4 git commands to generate.