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