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