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