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