]> git.proxmox.com Git - mirror_qemu.git/blame - block/iscsi.c
iscsi: do not leak acb->buf when commands are aborted
[mirror_qemu.git] / block / iscsi.c
CommitLineData
c589b249
RS
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>
f4dfa67f 28#include <arpa/inet.h>
c589b249 29#include "qemu-common.h"
1de7afc9
PB
30#include "qemu/config-file.h"
31#include "qemu/error-report.h"
737e150e 32#include "block/block_int.h"
c589b249 33#include "trace.h"
dbfff6d7 34#include "hw/scsi-defs.h"
c589b249
RS
35
36#include <iscsi/iscsi.h>
37#include <iscsi/scsi-lowlevel.h>
38
98392453
RS
39#ifdef __linux__
40#include <scsi/sg.h>
41#include <hw/scsi-defs.h>
42#endif
c589b249
RS
43
44typedef struct IscsiLun {
45 struct iscsi_context *iscsi;
46 int lun;
dbfff6d7 47 enum scsi_inquiry_peripheral_device_type type;
c589b249 48 int block_size;
c7b4a952 49 uint64_t num_blocks;
c9b9f682 50 int events;
5b5d34ec 51 QEMUTimer *nop_timer;
c589b249
RS
52} IscsiLun;
53
54typedef struct IscsiAIOCB {
55 BlockDriverAIOCB common;
56 QEMUIOVector *qiov;
57 QEMUBH *bh;
58 IscsiLun *iscsilun;
59 struct scsi_task *task;
60 uint8_t *buf;
61 int status;
62 int canceled;
63 size_t read_size;
64 size_t read_offset;
98392453
RS
65#ifdef __linux__
66 sg_io_hdr_t *ioh;
67#endif
c589b249
RS
68} IscsiAIOCB;
69
5b5d34ec
PL
70#define NOP_INTERVAL 5000
71#define MAX_NOP_FAILURES 3
72
27cbd828 73static void
cfb3f506 74iscsi_bh_cb(void *p)
27cbd828
PB
75{
76 IscsiAIOCB *acb = p;
77
78 qemu_bh_delete(acb->bh);
79
4790b03d
PB
80 g_free(acb->buf);
81 acb->buf = NULL;
82
27cbd828
PB
83 if (acb->canceled == 0) {
84 acb->common.cb(acb->common.opaque, acb->status);
85 }
86
1bd075f2
PB
87 if (acb->task != NULL) {
88 scsi_free_scsi_task(acb->task);
89 acb->task = NULL;
90 }
91
27cbd828
PB
92 qemu_aio_release(acb);
93}
94
cfb3f506
PB
95static void
96iscsi_schedule_bh(IscsiAIOCB *acb)
27cbd828 97{
1bd075f2
PB
98 if (acb->bh) {
99 return;
100 }
cfb3f506 101 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
27cbd828 102 qemu_bh_schedule(acb->bh);
27cbd828
PB
103}
104
105
c589b249
RS
106static void
107iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
108 void *private_data)
109{
1bd075f2
PB
110 IscsiAIOCB *acb = private_data;
111
112 acb->status = -ECANCELED;
113 iscsi_schedule_bh(acb);
c589b249
RS
114}
115
116static void
117iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
118{
119 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
120 IscsiLun *iscsilun = acb->iscsilun;
121
1bd075f2
PB
122 if (acb->status != -EINPROGRESS) {
123 return;
124 }
125
b2090919 126 acb->canceled = 1;
c589b249 127
b2090919 128 /* send a task mgmt call to the target to cancel the task on the target */
64e69e80 129 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
1bd075f2 130 iscsi_abort_task_cb, acb);
b2090919 131
1bd075f2
PB
132 while (acb->status == -EINPROGRESS) {
133 qemu_aio_wait();
134 }
c589b249
RS
135}
136
d7331bed 137static const AIOCBInfo iscsi_aiocb_info = {
c589b249
RS
138 .aiocb_size = sizeof(IscsiAIOCB),
139 .cancel = iscsi_aio_cancel,
140};
141
142
143static void iscsi_process_read(void *arg);
144static void iscsi_process_write(void *arg);
145
146static int iscsi_process_flush(void *arg)
147{
148 IscsiLun *iscsilun = arg;
149
150 return iscsi_queue_length(iscsilun->iscsi) > 0;
151}
152
153static void
154iscsi_set_events(IscsiLun *iscsilun)
155{
156 struct iscsi_context *iscsi = iscsilun->iscsi;
c9b9f682
RS
157 int ev;
158
159 /* We always register a read handler. */
160 ev = POLLIN;
161 ev |= iscsi_which_events(iscsi);
162 if (ev != iscsilun->events) {
163 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
164 iscsi_process_read,
165 (ev & POLLOUT) ? iscsi_process_write : NULL,
166 iscsi_process_flush,
167 iscsilun);
168
169 }
170
c9b9f682 171 iscsilun->events = ev;
c589b249
RS
172}
173
174static void
175iscsi_process_read(void *arg)
176{
177 IscsiLun *iscsilun = arg;
178 struct iscsi_context *iscsi = iscsilun->iscsi;
179
180 iscsi_service(iscsi, POLLIN);
181 iscsi_set_events(iscsilun);
182}
183
184static void
185iscsi_process_write(void *arg)
186{
187 IscsiLun *iscsilun = arg;
188 struct iscsi_context *iscsi = iscsilun->iscsi;
189
190 iscsi_service(iscsi, POLLOUT);
191 iscsi_set_events(iscsilun);
192}
193
194
c589b249 195static void
f4dfa67f 196iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
c589b249
RS
197 void *command_data, void *opaque)
198{
199 IscsiAIOCB *acb = opaque;
200
f4dfa67f 201 trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
c589b249
RS
202
203 g_free(acb->buf);
4790b03d 204 acb->buf = NULL;
c589b249 205
b2090919 206 if (acb->canceled != 0) {
c589b249
RS
207 return;
208 }
209
210 acb->status = 0;
211 if (status < 0) {
f4dfa67f 212 error_report("Failed to write16 data to iSCSI lun. %s",
c589b249
RS
213 iscsi_get_error(iscsi));
214 acb->status = -EIO;
215 }
216
cfb3f506 217 iscsi_schedule_bh(acb);
c589b249
RS
218}
219
220static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
221{
222 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
223}
224
225static BlockDriverAIOCB *
226iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
227 QEMUIOVector *qiov, int nb_sectors,
228 BlockDriverCompletionFunc *cb,
229 void *opaque)
230{
231 IscsiLun *iscsilun = bs->opaque;
232 struct iscsi_context *iscsi = iscsilun->iscsi;
233 IscsiAIOCB *acb;
234 size_t size;
f4dfa67f
RS
235 uint32_t num_sectors;
236 uint64_t lba;
237 struct iscsi_data data;
c589b249 238
d7331bed 239 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
c589b249
RS
240 trace_iscsi_aio_writev(iscsi, sector_num, nb_sectors, opaque, acb);
241
242 acb->iscsilun = iscsilun;
243 acb->qiov = qiov;
244
245 acb->canceled = 0;
1bd075f2
PB
246 acb->bh = NULL;
247 acb->status = -EINPROGRESS;
4790b03d 248 acb->buf = NULL;
c589b249 249
f4dfa67f 250 /* XXX we should pass the iovec to write16 to avoid the extra copy */
c589b249
RS
251 /* this will allow us to get rid of 'buf' completely */
252 size = nb_sectors * BDRV_SECTOR_SIZE;
4cc841b5
PL
253 data.size = MIN(size, acb->qiov->size);
254
255 /* if the iovec only contains one buffer we can pass it directly */
256 if (acb->qiov->niov == 1) {
4cc841b5
PL
257 data.data = acb->qiov->iov[0].iov_base;
258 } else {
259 acb->buf = g_malloc(data.size);
260 qemu_iovec_to_buf(acb->qiov, 0, acb->buf, data.size);
261 data.data = acb->buf;
262 }
f4dfa67f
RS
263
264 acb->task = malloc(sizeof(struct scsi_task));
c589b249 265 if (acb->task == NULL) {
f4dfa67f
RS
266 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
267 "command. %s", iscsi_get_error(iscsi));
268 qemu_aio_release(acb);
269 return NULL;
270 }
271 memset(acb->task, 0, sizeof(struct scsi_task));
272
273 acb->task->xfer_dir = SCSI_XFER_WRITE;
274 acb->task->cdb_size = 16;
275 acb->task->cdb[0] = 0x8a;
f4dfa67f
RS
276 lba = sector_qemu2lun(sector_num, iscsilun);
277 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
278 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
279 num_sectors = size / iscsilun->block_size;
280 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
281 acb->task->expxferlen = size;
282
f4dfa67f
RS
283 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
284 iscsi_aio_write16_cb,
285 &data,
286 acb) != 0) {
287 scsi_free_scsi_task(acb->task);
c589b249
RS
288 g_free(acb->buf);
289 qemu_aio_release(acb);
290 return NULL;
291 }
292
293 iscsi_set_events(iscsilun);
294
295 return &acb->common;
296}
297
298static void
f4dfa67f 299iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
c589b249
RS
300 void *command_data, void *opaque)
301{
302 IscsiAIOCB *acb = opaque;
303
f4dfa67f 304 trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
c589b249 305
b2090919 306 if (acb->canceled != 0) {
c589b249
RS
307 return;
308 }
309
310 acb->status = 0;
311 if (status != 0) {
f4dfa67f 312 error_report("Failed to read16 data from iSCSI lun. %s",
c589b249
RS
313 iscsi_get_error(iscsi));
314 acb->status = -EIO;
315 }
316
cfb3f506 317 iscsi_schedule_bh(acb);
c589b249
RS
318}
319
320static BlockDriverAIOCB *
321iscsi_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;
f4dfa67f 329 size_t qemu_read_size;
c589b249 330 int i;
f4dfa67f
RS
331 uint64_t lba;
332 uint32_t num_sectors;
c589b249
RS
333
334 qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
335
d7331bed 336 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
c589b249
RS
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;
1bd075f2
PB
343 acb->bh = NULL;
344 acb->status = -EINPROGRESS;
c589b249
RS
345 acb->read_size = qemu_read_size;
346 acb->buf = NULL;
347
348 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
349 * may be misaligned to the LUN, so we may need to read some extra
350 * data.
351 */
352 acb->read_offset = 0;
353 if (iscsilun->block_size > BDRV_SECTOR_SIZE) {
354 uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num;
355
356 acb->read_offset = bdrv_offset % iscsilun->block_size;
357 }
358
f4dfa67f
RS
359 num_sectors = (qemu_read_size + iscsilun->block_size
360 + acb->read_offset - 1)
361 / iscsilun->block_size;
362
363 acb->task = malloc(sizeof(struct scsi_task));
c589b249 364 if (acb->task == NULL) {
f4dfa67f
RS
365 error_report("iSCSI: Failed to allocate task for scsi READ16 "
366 "command. %s", iscsi_get_error(iscsi));
367 qemu_aio_release(acb);
368 return NULL;
369 }
370 memset(acb->task, 0, sizeof(struct scsi_task));
371
372 acb->task->xfer_dir = SCSI_XFER_READ;
373 lba = sector_qemu2lun(sector_num, iscsilun);
374 acb->task->expxferlen = qemu_read_size;
375
376 switch (iscsilun->type) {
377 case TYPE_DISK:
378 acb->task->cdb_size = 16;
379 acb->task->cdb[0] = 0x88;
380 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
381 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
382 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
383 break;
384 default:
385 acb->task->cdb_size = 10;
386 acb->task->cdb[0] = 0x28;
387 *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
388 *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
389 break;
390 }
e829b0bb 391
f4dfa67f
RS
392 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
393 iscsi_aio_read16_cb,
394 NULL,
395 acb) != 0) {
396 scsi_free_scsi_task(acb->task);
c589b249
RS
397 qemu_aio_release(acb);
398 return NULL;
399 }
400
401 for (i = 0; i < acb->qiov->niov; i++) {
402 scsi_task_add_data_in_buffer(acb->task,
403 acb->qiov->iov[i].iov_len,
404 acb->qiov->iov[i].iov_base);
405 }
406
407 iscsi_set_events(iscsilun);
408
409 return &acb->common;
410}
411
412
413static void
414iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
415 void *command_data, void *opaque)
416{
417 IscsiAIOCB *acb = opaque;
418
b2090919 419 if (acb->canceled != 0) {
c589b249
RS
420 return;
421 }
422
423 acb->status = 0;
424 if (status < 0) {
425 error_report("Failed to sync10 data on iSCSI lun. %s",
426 iscsi_get_error(iscsi));
427 acb->status = -EIO;
428 }
429
cfb3f506 430 iscsi_schedule_bh(acb);
c589b249
RS
431}
432
433static BlockDriverAIOCB *
434iscsi_aio_flush(BlockDriverState *bs,
435 BlockDriverCompletionFunc *cb, void *opaque)
436{
437 IscsiLun *iscsilun = bs->opaque;
438 struct iscsi_context *iscsi = iscsilun->iscsi;
439 IscsiAIOCB *acb;
440
d7331bed 441 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
c589b249
RS
442
443 acb->iscsilun = iscsilun;
444 acb->canceled = 0;
1bd075f2
PB
445 acb->bh = NULL;
446 acb->status = -EINPROGRESS;
4790b03d 447 acb->buf = NULL;
c589b249
RS
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
fa6acb0c
RS
465static void
466iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
467 void *command_data, void *opaque)
468{
469 IscsiAIOCB *acb = opaque;
470
b2090919 471 if (acb->canceled != 0) {
fa6acb0c
RS
472 return;
473 }
474
475 acb->status = 0;
476 if (status < 0) {
477 error_report("Failed to unmap data on iSCSI lun. %s",
478 iscsi_get_error(iscsi));
479 acb->status = -EIO;
480 }
481
cfb3f506 482 iscsi_schedule_bh(acb);
fa6acb0c
RS
483}
484
485static BlockDriverAIOCB *
486iscsi_aio_discard(BlockDriverState *bs,
487 int64_t sector_num, int nb_sectors,
488 BlockDriverCompletionFunc *cb, void *opaque)
489{
490 IscsiLun *iscsilun = bs->opaque;
491 struct iscsi_context *iscsi = iscsilun->iscsi;
492 IscsiAIOCB *acb;
493 struct unmap_list list[1];
494
d7331bed 495 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
fa6acb0c
RS
496
497 acb->iscsilun = iscsilun;
498 acb->canceled = 0;
1bd075f2
PB
499 acb->bh = NULL;
500 acb->status = -EINPROGRESS;
4790b03d 501 acb->buf = NULL;
fa6acb0c
RS
502
503 list[0].lba = sector_qemu2lun(sector_num, iscsilun);
504 list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size;
505
506 acb->task = iscsi_unmap_task(iscsi, iscsilun->lun,
507 0, 0, &list[0], 1,
508 iscsi_unmap_cb,
509 acb);
510 if (acb->task == NULL) {
511 error_report("iSCSI: Failed to send unmap command. %s",
512 iscsi_get_error(iscsi));
513 qemu_aio_release(acb);
514 return NULL;
515 }
516
517 iscsi_set_events(iscsilun);
518
519 return &acb->common;
520}
521
98392453
RS
522#ifdef __linux__
523static void
524iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
525 void *command_data, void *opaque)
526{
527 IscsiAIOCB *acb = opaque;
528
b2090919 529 if (acb->canceled != 0) {
98392453
RS
530 return;
531 }
532
533 acb->status = 0;
534 if (status < 0) {
535 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
536 iscsi_get_error(iscsi));
537 acb->status = -EIO;
538 }
539
540 acb->ioh->driver_status = 0;
541 acb->ioh->host_status = 0;
542 acb->ioh->resid = 0;
543
544#define SG_ERR_DRIVER_SENSE 0x08
545
546 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
547 int ss;
548
549 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
550
551 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
552 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
553 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
554 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
555 }
556
cfb3f506 557 iscsi_schedule_bh(acb);
98392453
RS
558}
559
560static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
561 unsigned long int req, void *buf,
562 BlockDriverCompletionFunc *cb, void *opaque)
563{
564 IscsiLun *iscsilun = bs->opaque;
565 struct iscsi_context *iscsi = iscsilun->iscsi;
566 struct iscsi_data data;
567 IscsiAIOCB *acb;
568
569 assert(req == SG_IO);
570
d7331bed 571 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
98392453
RS
572
573 acb->iscsilun = iscsilun;
574 acb->canceled = 0;
1bd075f2
PB
575 acb->bh = NULL;
576 acb->status = -EINPROGRESS;
98392453
RS
577 acb->buf = NULL;
578 acb->ioh = buf;
579
580 acb->task = malloc(sizeof(struct scsi_task));
581 if (acb->task == NULL) {
582 error_report("iSCSI: Failed to allocate task for scsi command. %s",
583 iscsi_get_error(iscsi));
584 qemu_aio_release(acb);
585 return NULL;
586 }
587 memset(acb->task, 0, sizeof(struct scsi_task));
588
589 switch (acb->ioh->dxfer_direction) {
590 case SG_DXFER_TO_DEV:
591 acb->task->xfer_dir = SCSI_XFER_WRITE;
592 break;
593 case SG_DXFER_FROM_DEV:
594 acb->task->xfer_dir = SCSI_XFER_READ;
595 break;
596 default:
597 acb->task->xfer_dir = SCSI_XFER_NONE;
598 break;
599 }
600
601 acb->task->cdb_size = acb->ioh->cmd_len;
602 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
603 acb->task->expxferlen = acb->ioh->dxfer_len;
604
605 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
606 data.data = acb->ioh->dxferp;
607 data.size = acb->ioh->dxfer_len;
608 }
609 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
610 iscsi_aio_ioctl_cb,
611 (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
612 &data : NULL,
613 acb) != 0) {
614 scsi_free_scsi_task(acb->task);
615 qemu_aio_release(acb);
616 return NULL;
617 }
618
619 /* tell libiscsi to read straight into the buffer we got from ioctl */
620 if (acb->task->xfer_dir == SCSI_XFER_READ) {
621 scsi_task_add_data_in_buffer(acb->task,
622 acb->ioh->dxfer_len,
623 acb->ioh->dxferp);
624 }
625
626 iscsi_set_events(iscsilun);
627
628 return &acb->common;
629}
630
f1a12821
RS
631
632static void ioctl_cb(void *opaque, int status)
633{
634 int *p_status = opaque;
635 *p_status = status;
636}
637
98392453
RS
638static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
639{
640 IscsiLun *iscsilun = bs->opaque;
f1a12821 641 int status;
98392453
RS
642
643 switch (req) {
644 case SG_GET_VERSION_NUM:
645 *(int *)buf = 30000;
646 break;
647 case SG_GET_SCSI_ID:
648 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
649 break;
f1a12821
RS
650 case SG_IO:
651 status = -EINPROGRESS;
652 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
653
654 while (status == -EINPROGRESS) {
655 qemu_aio_wait();
656 }
657
658 return 0;
98392453
RS
659 default:
660 return -1;
661 }
662 return 0;
663}
664#endif
665
c589b249
RS
666static int64_t
667iscsi_getlength(BlockDriverState *bs)
668{
669 IscsiLun *iscsilun = bs->opaque;
670 int64_t len;
671
672 len = iscsilun->num_blocks;
673 len *= iscsilun->block_size;
674
675 return len;
676}
677
f9dadc98
RS
678static int parse_chap(struct iscsi_context *iscsi, const char *target)
679{
680 QemuOptsList *list;
681 QemuOpts *opts;
682 const char *user = NULL;
683 const char *password = NULL;
684
685 list = qemu_find_opts("iscsi");
686 if (!list) {
687 return 0;
688 }
689
690 opts = qemu_opts_find(list, target);
691 if (opts == NULL) {
692 opts = QTAILQ_FIRST(&list->head);
693 if (!opts) {
694 return 0;
695 }
696 }
697
698 user = qemu_opt_get(opts, "user");
699 if (!user) {
700 return 0;
701 }
702
703 password = qemu_opt_get(opts, "password");
704 if (!password) {
705 error_report("CHAP username specified but no password was given");
706 return -1;
707 }
708
709 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
710 error_report("Failed to set initiator username and password");
711 return -1;
712 }
713
714 return 0;
715}
716
717static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
718{
719 QemuOptsList *list;
720 QemuOpts *opts;
721 const char *digest = NULL;
722
723 list = qemu_find_opts("iscsi");
724 if (!list) {
725 return;
726 }
727
728 opts = qemu_opts_find(list, target);
729 if (opts == NULL) {
730 opts = QTAILQ_FIRST(&list->head);
731 if (!opts) {
732 return;
733 }
734 }
735
736 digest = qemu_opt_get(opts, "header-digest");
737 if (!digest) {
738 return;
739 }
740
741 if (!strcmp(digest, "CRC32C")) {
742 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
743 } else if (!strcmp(digest, "NONE")) {
744 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
745 } else if (!strcmp(digest, "CRC32C-NONE")) {
746 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
747 } else if (!strcmp(digest, "NONE-CRC32C")) {
748 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
749 } else {
750 error_report("Invalid header-digest setting : %s", digest);
751 }
752}
753
754static char *parse_initiator_name(const char *target)
755{
756 QemuOptsList *list;
757 QemuOpts *opts;
758 const char *name = NULL;
31459f46 759 const char *iscsi_name = qemu_get_vm_name();
f9dadc98
RS
760
761 list = qemu_find_opts("iscsi");
f2ef4a6d
PB
762 if (list) {
763 opts = qemu_opts_find(list, target);
f9dadc98 764 if (!opts) {
f2ef4a6d
PB
765 opts = QTAILQ_FIRST(&list->head);
766 }
767 if (opts) {
768 name = qemu_opt_get(opts, "initiator-name");
f9dadc98
RS
769 }
770 }
771
f2ef4a6d
PB
772 if (name) {
773 return g_strdup(name);
774 } else {
31459f46
RS
775 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
776 iscsi_name ? ":" : "",
777 iscsi_name ? iscsi_name : "");
f9dadc98 778 }
f9dadc98
RS
779}
780
5b5d34ec
PL
781#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
782static void iscsi_nop_timed_event(void *opaque)
783{
784 IscsiLun *iscsilun = opaque;
785
786 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
787 error_report("iSCSI: NOP timeout. Reconnecting...");
788 iscsi_reconnect(iscsilun->iscsi);
789 }
790
791 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
792 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
793 return;
794 }
795
796 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
797 iscsi_set_events(iscsilun);
798}
799#endif
800
c589b249
RS
801/*
802 * We support iscsi url's on the form
803 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
804 */
805static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)
806{
807 IscsiLun *iscsilun = bs->opaque;
808 struct iscsi_context *iscsi = NULL;
809 struct iscsi_url *iscsi_url = NULL;
e829b0bb
PL
810 struct scsi_task *task = NULL;
811 struct scsi_inquiry_standard *inq = NULL;
812 struct scsi_readcapacity10 *rc10 = NULL;
813 struct scsi_readcapacity16 *rc16 = NULL;
f9dadc98 814 char *initiator_name = NULL;
c589b249
RS
815 int ret;
816
817 if ((BDRV_SECTOR_SIZE % 512) != 0) {
818 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
819 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
820 "of 512", BDRV_SECTOR_SIZE);
821 return -EINVAL;
822 }
823
c589b249
RS
824 iscsi_url = iscsi_parse_full_url(iscsi, filename);
825 if (iscsi_url == NULL) {
8da1e18b 826 error_report("Failed to parse URL : %s", filename);
c589b249 827 ret = -EINVAL;
b93c94f7 828 goto out;
c589b249
RS
829 }
830
f9dadc98
RS
831 memset(iscsilun, 0, sizeof(IscsiLun));
832
833 initiator_name = parse_initiator_name(iscsi_url->target);
834
835 iscsi = iscsi_create_context(initiator_name);
836 if (iscsi == NULL) {
837 error_report("iSCSI: Failed to create iSCSI context.");
838 ret = -ENOMEM;
b93c94f7 839 goto out;
f9dadc98
RS
840 }
841
c589b249
RS
842 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
843 error_report("iSCSI: Failed to set target name.");
844 ret = -EINVAL;
b93c94f7 845 goto out;
c589b249
RS
846 }
847
848 if (iscsi_url->user != NULL) {
849 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
850 iscsi_url->passwd);
851 if (ret != 0) {
852 error_report("Failed to set initiator username and password");
853 ret = -EINVAL;
b93c94f7 854 goto out;
c589b249
RS
855 }
856 }
f9dadc98
RS
857
858 /* check if we got CHAP username/password via the options */
859 if (parse_chap(iscsi, iscsi_url->target) != 0) {
860 error_report("iSCSI: Failed to set CHAP user/password");
861 ret = -EINVAL;
b93c94f7 862 goto out;
f9dadc98
RS
863 }
864
c589b249
RS
865 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
866 error_report("iSCSI: Failed to set session type to normal.");
867 ret = -EINVAL;
b93c94f7 868 goto out;
c589b249
RS
869 }
870
871 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
872
f9dadc98
RS
873 /* check if we got HEADER_DIGEST via the options */
874 parse_header_digest(iscsi, iscsi_url->target);
875
e829b0bb
PL
876 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
877 error_report("iSCSI: Failed to connect to LUN : %s",
878 iscsi_get_error(iscsi));
879 ret = -EINVAL;
880 goto out;
881 }
c589b249
RS
882
883 iscsilun->iscsi = iscsi;
884 iscsilun->lun = iscsi_url->lun;
885
e829b0bb
PL
886 task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
887
888 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
889 error_report("iSCSI: failed to send inquiry command.");
c589b249 890 ret = -EINVAL;
b93c94f7 891 goto out;
c589b249
RS
892 }
893
e829b0bb
PL
894 inq = scsi_datain_unmarshall(task);
895 if (inq == NULL) {
896 error_report("iSCSI: Failed to unmarshall inquiry data.");
c589b249 897 ret = -EINVAL;
b93c94f7 898 goto out;
c589b249 899 }
622695a4 900
e829b0bb
PL
901 iscsilun->type = inq->periperal_device_type;
902
903 scsi_free_scsi_task(task);
904
905 switch (iscsilun->type) {
906 case TYPE_DISK:
907 task = iscsi_readcapacity16_sync(iscsi, iscsilun->lun);
908 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
909 error_report("iSCSI: failed to send readcapacity16 command.");
910 ret = -EINVAL;
911 goto out;
912 }
913 rc16 = scsi_datain_unmarshall(task);
914 if (rc16 == NULL) {
915 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
916 ret = -EINVAL;
917 goto out;
918 }
919 iscsilun->block_size = rc16->block_length;
920 iscsilun->num_blocks = rc16->returned_lba + 1;
921 break;
922 case TYPE_ROM:
923 task = iscsi_readcapacity10_sync(iscsi, iscsilun->lun, 0, 0);
924 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
925 error_report("iSCSI: failed to send readcapacity10 command.");
926 ret = -EINVAL;
927 goto out;
928 }
929 rc10 = scsi_datain_unmarshall(task);
930 if (rc10 == NULL) {
931 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
932 ret = -EINVAL;
933 goto out;
934 }
935 iscsilun->block_size = rc10->block_size;
936 if (rc10->lba == 0) {
937 /* blank disk loaded */
938 iscsilun->num_blocks = 0;
939 } else {
940 iscsilun->num_blocks = rc10->lba + 1;
941 }
942 break;
943 default:
944 break;
945 }
946
947 bs->total_sectors = iscsilun->num_blocks *
948 iscsilun->block_size / BDRV_SECTOR_SIZE ;
949
622695a4
RS
950 /* Medium changer or tape. We dont have any emulation for this so this must
951 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
952 * to read from the device to guess the image format.
953 */
954 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
955 iscsilun->type == TYPE_TAPE) {
956 bs->sg = 1;
957 }
958
b93c94f7 959 ret = 0;
c589b249 960
5b5d34ec
PL
961#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
962 /* Set up a timer for sending out iSCSI NOPs */
963 iscsilun->nop_timer = qemu_new_timer_ms(rt_clock, iscsi_nop_timed_event, iscsilun);
964 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
965#endif
966
b93c94f7 967out:
f9dadc98
RS
968 if (initiator_name != NULL) {
969 g_free(initiator_name);
970 }
c589b249
RS
971 if (iscsi_url != NULL) {
972 iscsi_destroy_url(iscsi_url);
973 }
e829b0bb
PL
974 if (task != NULL) {
975 scsi_free_scsi_task(task);
976 }
b93c94f7
PB
977
978 if (ret) {
979 if (iscsi != NULL) {
980 iscsi_destroy_context(iscsi);
981 }
982 memset(iscsilun, 0, sizeof(IscsiLun));
c589b249 983 }
c589b249
RS
984 return ret;
985}
986
987static void iscsi_close(BlockDriverState *bs)
988{
989 IscsiLun *iscsilun = bs->opaque;
990 struct iscsi_context *iscsi = iscsilun->iscsi;
991
5b5d34ec
PL
992 if (iscsilun->nop_timer) {
993 qemu_del_timer(iscsilun->nop_timer);
994 qemu_free_timer(iscsilun->nop_timer);
995 }
bafbd6a1 996 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
c589b249
RS
997 iscsi_destroy_context(iscsi);
998 memset(iscsilun, 0, sizeof(IscsiLun));
999}
1000
f807ecd5
PL
1001static int iscsi_has_zero_init(BlockDriverState *bs)
1002{
1003 return 0;
1004}
1005
de8864e5
PL
1006static int iscsi_create(const char *filename, QEMUOptionParameter *options)
1007{
1008 int ret = 0;
1009 int64_t total_size = 0;
1010 BlockDriverState bs;
1011 IscsiLun *iscsilun = NULL;
1012
1013 memset(&bs, 0, sizeof(BlockDriverState));
1014
1015 /* Read out options */
1016 while (options && options->name) {
1017 if (!strcmp(options->name, "size")) {
1018 total_size = options->value.n / BDRV_SECTOR_SIZE;
1019 }
1020 options++;
1021 }
1022
1023 bs.opaque = g_malloc0(sizeof(struct IscsiLun));
1024 iscsilun = bs.opaque;
1025
1026 ret = iscsi_open(&bs, filename, 0);
1027 if (ret != 0) {
1028 goto out;
1029 }
5b5d34ec
PL
1030 if (iscsilun->nop_timer) {
1031 qemu_del_timer(iscsilun->nop_timer);
1032 qemu_free_timer(iscsilun->nop_timer);
1033 }
de8864e5
PL
1034 if (iscsilun->type != TYPE_DISK) {
1035 ret = -ENODEV;
1036 goto out;
1037 }
1038 if (bs.total_sectors < total_size) {
1039 ret = -ENOSPC;
1040 }
1041
1042 ret = 0;
1043out:
1044 if (iscsilun->iscsi != NULL) {
1045 iscsi_destroy_context(iscsilun->iscsi);
1046 }
1047 g_free(bs.opaque);
1048 return ret;
1049}
1050
1051static QEMUOptionParameter iscsi_create_options[] = {
1052 {
1053 .name = BLOCK_OPT_SIZE,
1054 .type = OPT_SIZE,
1055 .help = "Virtual disk size"
1056 },
1057 { NULL }
1058};
1059
c589b249
RS
1060static 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,
de8864e5
PL
1067 .bdrv_create = iscsi_create,
1068 .create_options = iscsi_create_options,
c589b249
RS
1069
1070 .bdrv_getlength = iscsi_getlength,
1071
1072 .bdrv_aio_readv = iscsi_aio_readv,
1073 .bdrv_aio_writev = iscsi_aio_writev,
1074 .bdrv_aio_flush = iscsi_aio_flush,
fa6acb0c
RS
1075
1076 .bdrv_aio_discard = iscsi_aio_discard,
f807ecd5 1077 .bdrv_has_zero_init = iscsi_has_zero_init,
98392453
RS
1078
1079#ifdef __linux__
1080 .bdrv_ioctl = iscsi_ioctl,
1081 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1082#endif
c589b249
RS
1083};
1084
4d454574
PB
1085static QemuOptsList qemu_iscsi_opts = {
1086 .name = "iscsi",
1087 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1088 .desc = {
1089 {
1090 .name = "user",
1091 .type = QEMU_OPT_STRING,
1092 .help = "username for CHAP authentication to target",
1093 },{
1094 .name = "password",
1095 .type = QEMU_OPT_STRING,
1096 .help = "password for CHAP authentication to target",
1097 },{
1098 .name = "header-digest",
1099 .type = QEMU_OPT_STRING,
1100 .help = "HeaderDigest setting. "
1101 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1102 },{
1103 .name = "initiator-name",
1104 .type = QEMU_OPT_STRING,
1105 .help = "Initiator iqn name to use when connecting",
1106 },
1107 { /* end of list */ }
1108 },
1109};
1110
c589b249
RS
1111static void iscsi_block_init(void)
1112{
1113 bdrv_register(&bdrv_iscsi);
4d454574 1114 qemu_add_opts(&qemu_iscsi_opts);
c589b249
RS
1115}
1116
1117block_init(iscsi_block_init);