]> git.proxmox.com Git - mirror_qemu.git/blame - block/iscsi.c
block: implement reference count for BlockDriverState
[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"
0d09e41a 34#include "block/scsi.h"
0a53f010 35#include "qemu/iov.h"
c589b249
RS
36
37#include <iscsi/iscsi.h>
38#include <iscsi/scsi-lowlevel.h>
39
98392453
RS
40#ifdef __linux__
41#include <scsi/sg.h>
0d09e41a 42#include <block/scsi.h>
98392453 43#endif
c589b249
RS
44
45typedef struct IscsiLun {
46 struct iscsi_context *iscsi;
47 int lun;
dbfff6d7 48 enum scsi_inquiry_peripheral_device_type type;
c589b249 49 int block_size;
c7b4a952 50 uint64_t num_blocks;
c9b9f682 51 int events;
5b5d34ec 52 QEMUTimer *nop_timer;
c589b249
RS
53} IscsiLun;
54
55typedef struct IscsiAIOCB {
56 BlockDriverAIOCB common;
57 QEMUIOVector *qiov;
58 QEMUBH *bh;
59 IscsiLun *iscsilun;
60 struct scsi_task *task;
61 uint8_t *buf;
62 int status;
63 int canceled;
1dde716e 64 int retries;
1dde716e
PL
65 int64_t sector_num;
66 int nb_sectors;
98392453
RS
67#ifdef __linux__
68 sg_io_hdr_t *ioh;
69#endif
c589b249
RS
70} IscsiAIOCB;
71
5b5d34ec
PL
72#define NOP_INTERVAL 5000
73#define MAX_NOP_FAILURES 3
1dde716e 74#define ISCSI_CMD_RETRIES 5
5b5d34ec 75
27cbd828 76static void
cfb3f506 77iscsi_bh_cb(void *p)
27cbd828
PB
78{
79 IscsiAIOCB *acb = p;
80
81 qemu_bh_delete(acb->bh);
82
4790b03d
PB
83 g_free(acb->buf);
84 acb->buf = NULL;
85
27cbd828
PB
86 if (acb->canceled == 0) {
87 acb->common.cb(acb->common.opaque, acb->status);
88 }
89
1bd075f2
PB
90 if (acb->task != NULL) {
91 scsi_free_scsi_task(acb->task);
92 acb->task = NULL;
93 }
94
27cbd828
PB
95 qemu_aio_release(acb);
96}
97
cfb3f506
PB
98static void
99iscsi_schedule_bh(IscsiAIOCB *acb)
27cbd828 100{
1bd075f2
PB
101 if (acb->bh) {
102 return;
103 }
cfb3f506 104 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
27cbd828 105 qemu_bh_schedule(acb->bh);
27cbd828
PB
106}
107
108
c589b249
RS
109static void
110iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
111 void *private_data)
112{
1bd075f2
PB
113 IscsiAIOCB *acb = private_data;
114
115 acb->status = -ECANCELED;
116 iscsi_schedule_bh(acb);
c589b249
RS
117}
118
119static void
120iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
121{
122 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
123 IscsiLun *iscsilun = acb->iscsilun;
124
1bd075f2
PB
125 if (acb->status != -EINPROGRESS) {
126 return;
127 }
128
b2090919 129 acb->canceled = 1;
c589b249 130
b2090919 131 /* send a task mgmt call to the target to cancel the task on the target */
64e69e80 132 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
1bd075f2 133 iscsi_abort_task_cb, acb);
b2090919 134
1bd075f2
PB
135 while (acb->status == -EINPROGRESS) {
136 qemu_aio_wait();
137 }
c589b249
RS
138}
139
d7331bed 140static const AIOCBInfo iscsi_aiocb_info = {
c589b249
RS
141 .aiocb_size = sizeof(IscsiAIOCB),
142 .cancel = iscsi_aio_cancel,
143};
144
145
146static void iscsi_process_read(void *arg);
147static void iscsi_process_write(void *arg);
148
c589b249
RS
149static void
150iscsi_set_events(IscsiLun *iscsilun)
151{
152 struct iscsi_context *iscsi = iscsilun->iscsi;
c9b9f682
RS
153 int ev;
154
155 /* We always register a read handler. */
156 ev = POLLIN;
157 ev |= iscsi_which_events(iscsi);
158 if (ev != iscsilun->events) {
159 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
160 iscsi_process_read,
161 (ev & POLLOUT) ? iscsi_process_write : NULL,
c9b9f682
RS
162 iscsilun);
163
164 }
165
c9b9f682 166 iscsilun->events = ev;
c589b249
RS
167}
168
169static void
170iscsi_process_read(void *arg)
171{
172 IscsiLun *iscsilun = arg;
173 struct iscsi_context *iscsi = iscsilun->iscsi;
174
175 iscsi_service(iscsi, POLLIN);
176 iscsi_set_events(iscsilun);
177}
178
179static void
180iscsi_process_write(void *arg)
181{
182 IscsiLun *iscsilun = arg;
183 struct iscsi_context *iscsi = iscsilun->iscsi;
184
185 iscsi_service(iscsi, POLLOUT);
186 iscsi_set_events(iscsilun);
187}
188
1dde716e
PL
189static int
190iscsi_aio_writev_acb(IscsiAIOCB *acb);
c589b249 191
c589b249 192static void
f4dfa67f 193iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
c589b249
RS
194 void *command_data, void *opaque)
195{
196 IscsiAIOCB *acb = opaque;
197
f4dfa67f 198 trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
c589b249
RS
199
200 g_free(acb->buf);
4790b03d 201 acb->buf = NULL;
c589b249 202
b2090919 203 if (acb->canceled != 0) {
c589b249
RS
204 return;
205 }
206
207 acb->status = 0;
1dde716e
PL
208 if (status != 0) {
209 if (status == SCSI_STATUS_CHECK_CONDITION
210 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
211 && acb->retries-- > 0) {
f0d2a4d4
PB
212 scsi_free_scsi_task(acb->task);
213 acb->task = NULL;
1dde716e
PL
214 if (iscsi_aio_writev_acb(acb) == 0) {
215 iscsi_set_events(acb->iscsilun);
216 return;
217 }
218 }
f4dfa67f 219 error_report("Failed to write16 data to iSCSI lun. %s",
c589b249
RS
220 iscsi_get_error(iscsi));
221 acb->status = -EIO;
222 }
223
cfb3f506 224 iscsi_schedule_bh(acb);
c589b249
RS
225}
226
0777b5dd
PL
227static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
228{
229 return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
230}
231
c589b249
RS
232static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
233{
234 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
235}
236
91bea4e2
PL
237static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
238 IscsiLun *iscsilun)
239{
240 if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
241 (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
f5075224
RJ
242 error_report("iSCSI misaligned request: "
243 "iscsilun->block_size %u, sector_num %" PRIi64
244 ", nb_sectors %d",
91bea4e2
PL
245 iscsilun->block_size, sector_num, nb_sectors);
246 return 0;
247 }
248 return 1;
249}
250
1dde716e
PL
251static int
252iscsi_aio_writev_acb(IscsiAIOCB *acb)
c589b249 253{
1dde716e 254 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
c589b249 255 size_t size;
f4dfa67f
RS
256 uint32_t num_sectors;
257 uint64_t lba;
7371d56f 258#if !defined(LIBISCSI_FEATURE_IOVECTOR)
f4dfa67f 259 struct iscsi_data data;
7371d56f
PL
260#endif
261 int ret;
c589b249 262
c589b249 263 acb->canceled = 0;
1bd075f2
PB
264 acb->bh = NULL;
265 acb->status = -EINPROGRESS;
4790b03d 266 acb->buf = NULL;
c589b249 267
c589b249 268 /* this will allow us to get rid of 'buf' completely */
1dde716e 269 size = acb->nb_sectors * BDRV_SECTOR_SIZE;
7371d56f
PL
270
271#if !defined(LIBISCSI_FEATURE_IOVECTOR)
4cc841b5
PL
272 data.size = MIN(size, acb->qiov->size);
273
274 /* if the iovec only contains one buffer we can pass it directly */
275 if (acb->qiov->niov == 1) {
4cc841b5
PL
276 data.data = acb->qiov->iov[0].iov_base;
277 } else {
278 acb->buf = g_malloc(data.size);
279 qemu_iovec_to_buf(acb->qiov, 0, acb->buf, data.size);
280 data.data = acb->buf;
281 }
7371d56f 282#endif
f4dfa67f
RS
283
284 acb->task = malloc(sizeof(struct scsi_task));
c589b249 285 if (acb->task == NULL) {
f4dfa67f
RS
286 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
287 "command. %s", iscsi_get_error(iscsi));
1dde716e 288 return -1;
f4dfa67f
RS
289 }
290 memset(acb->task, 0, sizeof(struct scsi_task));
291
292 acb->task->xfer_dir = SCSI_XFER_WRITE;
293 acb->task->cdb_size = 16;
294 acb->task->cdb[0] = 0x8a;
1dde716e 295 lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
f4dfa67f
RS
296 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
297 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
0777b5dd 298 num_sectors = sector_qemu2lun(acb->nb_sectors, acb->iscsilun);
f4dfa67f
RS
299 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
300 acb->task->expxferlen = size;
301
7371d56f 302#if defined(LIBISCSI_FEATURE_IOVECTOR)
1dde716e 303 ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
7371d56f
PL
304 iscsi_aio_write16_cb,
305 NULL,
306 acb);
307#else
1dde716e 308 ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
7371d56f
PL
309 iscsi_aio_write16_cb,
310 &data,
311 acb);
312#endif
313 if (ret != 0) {
f0d2a4d4 314 scsi_free_scsi_task(acb->task);
c589b249 315 g_free(acb->buf);
1dde716e 316 return -1;
c589b249
RS
317 }
318
7371d56f
PL
319#if defined(LIBISCSI_FEATURE_IOVECTOR)
320 scsi_task_set_iov_out(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
321#endif
322
1dde716e
PL
323 return 0;
324}
325
326static BlockDriverAIOCB *
327iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
328 QEMUIOVector *qiov, int nb_sectors,
329 BlockDriverCompletionFunc *cb,
330 void *opaque)
331{
332 IscsiLun *iscsilun = bs->opaque;
333 IscsiAIOCB *acb;
c589b249 334
91bea4e2
PL
335 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
336 return NULL;
337 }
338
1dde716e
PL
339 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
340 trace_iscsi_aio_writev(iscsilun->iscsi, sector_num, nb_sectors, opaque, acb);
341
342 acb->iscsilun = iscsilun;
343 acb->qiov = qiov;
344 acb->nb_sectors = nb_sectors;
345 acb->sector_num = sector_num;
346 acb->retries = ISCSI_CMD_RETRIES;
347
348 if (iscsi_aio_writev_acb(acb) != 0) {
1dde716e
PL
349 qemu_aio_release(acb);
350 return NULL;
351 }
352
353 iscsi_set_events(iscsilun);
c589b249
RS
354 return &acb->common;
355}
356
1dde716e
PL
357static int
358iscsi_aio_readv_acb(IscsiAIOCB *acb);
359
c589b249 360static void
f4dfa67f 361iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
c589b249
RS
362 void *command_data, void *opaque)
363{
364 IscsiAIOCB *acb = opaque;
365
f4dfa67f 366 trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
c589b249 367
b2090919 368 if (acb->canceled != 0) {
c589b249
RS
369 return;
370 }
371
372 acb->status = 0;
373 if (status != 0) {
1dde716e
PL
374 if (status == SCSI_STATUS_CHECK_CONDITION
375 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
376 && acb->retries-- > 0) {
f0d2a4d4
PB
377 scsi_free_scsi_task(acb->task);
378 acb->task = NULL;
1dde716e
PL
379 if (iscsi_aio_readv_acb(acb) == 0) {
380 iscsi_set_events(acb->iscsilun);
381 return;
382 }
383 }
f4dfa67f 384 error_report("Failed to read16 data from iSCSI lun. %s",
c589b249
RS
385 iscsi_get_error(iscsi));
386 acb->status = -EIO;
387 }
388
cfb3f506 389 iscsi_schedule_bh(acb);
c589b249
RS
390}
391
1dde716e
PL
392static int
393iscsi_aio_readv_acb(IscsiAIOCB *acb)
c589b249 394{
1dde716e 395 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
7e4d5a9f 396 size_t size;
1dde716e
PL
397 uint64_t lba;
398 uint32_t num_sectors;
399 int ret;
7371d56f 400#if !defined(LIBISCSI_FEATURE_IOVECTOR)
c589b249 401 int i;
7371d56f 402#endif
c589b249
RS
403
404 acb->canceled = 0;
1bd075f2
PB
405 acb->bh = NULL;
406 acb->status = -EINPROGRESS;
c589b249
RS
407 acb->buf = NULL;
408
7e4d5a9f 409 size = acb->nb_sectors * BDRV_SECTOR_SIZE;
f4dfa67f
RS
410
411 acb->task = malloc(sizeof(struct scsi_task));
c589b249 412 if (acb->task == NULL) {
f4dfa67f
RS
413 error_report("iSCSI: Failed to allocate task for scsi READ16 "
414 "command. %s", iscsi_get_error(iscsi));
1dde716e 415 return -1;
f4dfa67f
RS
416 }
417 memset(acb->task, 0, sizeof(struct scsi_task));
418
419 acb->task->xfer_dir = SCSI_XFER_READ;
7e4d5a9f 420 acb->task->expxferlen = size;
1dde716e 421 lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
7e4d5a9f 422 num_sectors = sector_qemu2lun(acb->nb_sectors, acb->iscsilun);
f4dfa67f 423
1dde716e 424 switch (acb->iscsilun->type) {
f4dfa67f
RS
425 case TYPE_DISK:
426 acb->task->cdb_size = 16;
427 acb->task->cdb[0] = 0x88;
428 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
429 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
430 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
431 break;
432 default:
433 acb->task->cdb_size = 10;
434 acb->task->cdb[0] = 0x28;
435 *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
436 *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
437 break;
438 }
e829b0bb 439
1dde716e 440 ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
7371d56f
PL
441 iscsi_aio_read16_cb,
442 NULL,
443 acb);
444 if (ret != 0) {
f0d2a4d4 445 scsi_free_scsi_task(acb->task);
1dde716e 446 return -1;
c589b249
RS
447 }
448
7371d56f
PL
449#if defined(LIBISCSI_FEATURE_IOVECTOR)
450 scsi_task_set_iov_in(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
451#else
c589b249
RS
452 for (i = 0; i < acb->qiov->niov; i++) {
453 scsi_task_add_data_in_buffer(acb->task,
454 acb->qiov->iov[i].iov_len,
455 acb->qiov->iov[i].iov_base);
456 }
7371d56f 457#endif
1dde716e
PL
458 return 0;
459}
c589b249 460
1dde716e
PL
461static BlockDriverAIOCB *
462iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
463 QEMUIOVector *qiov, int nb_sectors,
464 BlockDriverCompletionFunc *cb,
465 void *opaque)
466{
467 IscsiLun *iscsilun = bs->opaque;
468 IscsiAIOCB *acb;
469
91bea4e2
PL
470 if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
471 return NULL;
472 }
473
1dde716e
PL
474 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
475 trace_iscsi_aio_readv(iscsilun->iscsi, sector_num, nb_sectors, opaque, acb);
476
477 acb->nb_sectors = nb_sectors;
478 acb->sector_num = sector_num;
479 acb->iscsilun = iscsilun;
480 acb->qiov = qiov;
1dde716e
PL
481 acb->retries = ISCSI_CMD_RETRIES;
482
483 if (iscsi_aio_readv_acb(acb) != 0) {
1dde716e
PL
484 qemu_aio_release(acb);
485 return NULL;
486 }
c589b249 487
1dde716e 488 iscsi_set_events(iscsilun);
c589b249
RS
489 return &acb->common;
490}
491
1dde716e
PL
492static int
493iscsi_aio_flush_acb(IscsiAIOCB *acb);
c589b249
RS
494
495static void
496iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
497 void *command_data, void *opaque)
498{
499 IscsiAIOCB *acb = opaque;
500
b2090919 501 if (acb->canceled != 0) {
c589b249
RS
502 return;
503 }
504
505 acb->status = 0;
1dde716e
PL
506 if (status != 0) {
507 if (status == SCSI_STATUS_CHECK_CONDITION
508 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
509 && acb->retries-- > 0) {
f0d2a4d4
PB
510 scsi_free_scsi_task(acb->task);
511 acb->task = NULL;
1dde716e
PL
512 if (iscsi_aio_flush_acb(acb) == 0) {
513 iscsi_set_events(acb->iscsilun);
514 return;
515 }
516 }
c589b249
RS
517 error_report("Failed to sync10 data on iSCSI lun. %s",
518 iscsi_get_error(iscsi));
519 acb->status = -EIO;
520 }
521
cfb3f506 522 iscsi_schedule_bh(acb);
c589b249
RS
523}
524
1dde716e
PL
525static int
526iscsi_aio_flush_acb(IscsiAIOCB *acb)
c589b249 527{
1dde716e 528 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
c589b249 529
c589b249 530 acb->canceled = 0;
1bd075f2
PB
531 acb->bh = NULL;
532 acb->status = -EINPROGRESS;
4790b03d 533 acb->buf = NULL;
c589b249 534
1dde716e 535 acb->task = iscsi_synchronizecache10_task(iscsi, acb->iscsilun->lun,
c589b249
RS
536 0, 0, 0, 0,
537 iscsi_synccache10_cb,
538 acb);
539 if (acb->task == NULL) {
540 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
541 iscsi_get_error(iscsi));
1dde716e
PL
542 return -1;
543 }
544
545 return 0;
546}
547
548static BlockDriverAIOCB *
549iscsi_aio_flush(BlockDriverState *bs,
550 BlockDriverCompletionFunc *cb, void *opaque)
551{
552 IscsiLun *iscsilun = bs->opaque;
553
554 IscsiAIOCB *acb;
555
556 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
557
558 acb->iscsilun = iscsilun;
559 acb->retries = ISCSI_CMD_RETRIES;
560
561 if (iscsi_aio_flush_acb(acb) != 0) {
c589b249
RS
562 qemu_aio_release(acb);
563 return NULL;
564 }
565
566 iscsi_set_events(iscsilun);
567
568 return &acb->common;
569}
570
1dde716e
PL
571static int iscsi_aio_discard_acb(IscsiAIOCB *acb);
572
fa6acb0c
RS
573static void
574iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
575 void *command_data, void *opaque)
576{
577 IscsiAIOCB *acb = opaque;
578
b2090919 579 if (acb->canceled != 0) {
fa6acb0c
RS
580 return;
581 }
582
583 acb->status = 0;
1dde716e
PL
584 if (status != 0) {
585 if (status == SCSI_STATUS_CHECK_CONDITION
586 && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
587 && acb->retries-- > 0) {
f0d2a4d4
PB
588 scsi_free_scsi_task(acb->task);
589 acb->task = NULL;
1dde716e
PL
590 if (iscsi_aio_discard_acb(acb) == 0) {
591 iscsi_set_events(acb->iscsilun);
592 return;
593 }
594 }
fa6acb0c
RS
595 error_report("Failed to unmap data on iSCSI lun. %s",
596 iscsi_get_error(iscsi));
597 acb->status = -EIO;
598 }
599
cfb3f506 600 iscsi_schedule_bh(acb);
fa6acb0c
RS
601}
602
1dde716e
PL
603static int iscsi_aio_discard_acb(IscsiAIOCB *acb) {
604 struct iscsi_context *iscsi = acb->iscsilun->iscsi;
fa6acb0c
RS
605 struct unmap_list list[1];
606
fa6acb0c 607 acb->canceled = 0;
1bd075f2
PB
608 acb->bh = NULL;
609 acb->status = -EINPROGRESS;
4790b03d 610 acb->buf = NULL;
fa6acb0c 611
1dde716e
PL
612 list[0].lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
613 list[0].num = acb->nb_sectors * BDRV_SECTOR_SIZE / acb->iscsilun->block_size;
fa6acb0c 614
1dde716e 615 acb->task = iscsi_unmap_task(iscsi, acb->iscsilun->lun,
fa6acb0c
RS
616 0, 0, &list[0], 1,
617 iscsi_unmap_cb,
618 acb);
619 if (acb->task == NULL) {
620 error_report("iSCSI: Failed to send unmap command. %s",
621 iscsi_get_error(iscsi));
1dde716e
PL
622 return -1;
623 }
624
625 return 0;
626}
627
628static BlockDriverAIOCB *
629iscsi_aio_discard(BlockDriverState *bs,
630 int64_t sector_num, int nb_sectors,
631 BlockDriverCompletionFunc *cb, void *opaque)
632{
633 IscsiLun *iscsilun = bs->opaque;
634 IscsiAIOCB *acb;
635
636 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
637
638 acb->iscsilun = iscsilun;
639 acb->nb_sectors = nb_sectors;
640 acb->sector_num = sector_num;
641 acb->retries = ISCSI_CMD_RETRIES;
642
643 if (iscsi_aio_discard_acb(acb) != 0) {
fa6acb0c
RS
644 qemu_aio_release(acb);
645 return NULL;
646 }
647
648 iscsi_set_events(iscsilun);
649
650 return &acb->common;
651}
652
98392453
RS
653#ifdef __linux__
654static void
655iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
656 void *command_data, void *opaque)
657{
658 IscsiAIOCB *acb = opaque;
659
0a53f010
RS
660 g_free(acb->buf);
661 acb->buf = NULL;
662
b2090919 663 if (acb->canceled != 0) {
98392453
RS
664 return;
665 }
666
667 acb->status = 0;
668 if (status < 0) {
669 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
670 iscsi_get_error(iscsi));
671 acb->status = -EIO;
672 }
673
674 acb->ioh->driver_status = 0;
675 acb->ioh->host_status = 0;
676 acb->ioh->resid = 0;
677
678#define SG_ERR_DRIVER_SENSE 0x08
679
680 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
681 int ss;
682
683 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
684
685 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
686 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
687 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
688 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
689 }
690
cfb3f506 691 iscsi_schedule_bh(acb);
98392453
RS
692}
693
694static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
695 unsigned long int req, void *buf,
696 BlockDriverCompletionFunc *cb, void *opaque)
697{
698 IscsiLun *iscsilun = bs->opaque;
699 struct iscsi_context *iscsi = iscsilun->iscsi;
700 struct iscsi_data data;
701 IscsiAIOCB *acb;
702
703 assert(req == SG_IO);
704
d7331bed 705 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
98392453
RS
706
707 acb->iscsilun = iscsilun;
708 acb->canceled = 0;
1bd075f2
PB
709 acb->bh = NULL;
710 acb->status = -EINPROGRESS;
98392453
RS
711 acb->buf = NULL;
712 acb->ioh = buf;
713
714 acb->task = malloc(sizeof(struct scsi_task));
715 if (acb->task == NULL) {
716 error_report("iSCSI: Failed to allocate task for scsi command. %s",
717 iscsi_get_error(iscsi));
718 qemu_aio_release(acb);
719 return NULL;
720 }
721 memset(acb->task, 0, sizeof(struct scsi_task));
722
723 switch (acb->ioh->dxfer_direction) {
724 case SG_DXFER_TO_DEV:
725 acb->task->xfer_dir = SCSI_XFER_WRITE;
726 break;
727 case SG_DXFER_FROM_DEV:
728 acb->task->xfer_dir = SCSI_XFER_READ;
729 break;
730 default:
731 acb->task->xfer_dir = SCSI_XFER_NONE;
732 break;
733 }
734
735 acb->task->cdb_size = acb->ioh->cmd_len;
736 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
737 acb->task->expxferlen = acb->ioh->dxfer_len;
738
0a53f010 739 data.size = 0;
98392453 740 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
0a53f010
RS
741 if (acb->ioh->iovec_count == 0) {
742 data.data = acb->ioh->dxferp;
743 data.size = acb->ioh->dxfer_len;
744 } else {
745#if defined(LIBISCSI_FEATURE_IOVECTOR)
746 scsi_task_set_iov_out(acb->task,
747 (struct scsi_iovec *) acb->ioh->dxferp,
748 acb->ioh->iovec_count);
749#else
750 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
751
752 acb->buf = g_malloc(acb->ioh->dxfer_len);
753 data.data = acb->buf;
754 data.size = iov_to_buf(iov, acb->ioh->iovec_count, 0,
755 acb->buf, acb->ioh->dxfer_len);
756#endif
757 }
98392453 758 }
0a53f010 759
98392453
RS
760 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
761 iscsi_aio_ioctl_cb,
0a53f010 762 (data.size > 0) ? &data : NULL,
98392453
RS
763 acb) != 0) {
764 scsi_free_scsi_task(acb->task);
765 qemu_aio_release(acb);
766 return NULL;
767 }
768
769 /* tell libiscsi to read straight into the buffer we got from ioctl */
770 if (acb->task->xfer_dir == SCSI_XFER_READ) {
0a53f010
RS
771 if (acb->ioh->iovec_count == 0) {
772 scsi_task_add_data_in_buffer(acb->task,
773 acb->ioh->dxfer_len,
774 acb->ioh->dxferp);
775 } else {
776#if defined(LIBISCSI_FEATURE_IOVECTOR)
777 scsi_task_set_iov_in(acb->task,
778 (struct scsi_iovec *) acb->ioh->dxferp,
779 acb->ioh->iovec_count);
780#else
781 int i;
782 for (i = 0; i < acb->ioh->iovec_count; i++) {
783 struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
784
785 scsi_task_add_data_in_buffer(acb->task,
786 iov[i].iov_len,
787 iov[i].iov_base);
788 }
789#endif
790 }
98392453
RS
791 }
792
793 iscsi_set_events(iscsilun);
794
795 return &acb->common;
796}
797
f1a12821
RS
798
799static void ioctl_cb(void *opaque, int status)
800{
801 int *p_status = opaque;
802 *p_status = status;
803}
804
98392453
RS
805static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
806{
807 IscsiLun *iscsilun = bs->opaque;
f1a12821 808 int status;
98392453
RS
809
810 switch (req) {
811 case SG_GET_VERSION_NUM:
812 *(int *)buf = 30000;
813 break;
814 case SG_GET_SCSI_ID:
815 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
816 break;
f1a12821
RS
817 case SG_IO:
818 status = -EINPROGRESS;
819 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
820
821 while (status == -EINPROGRESS) {
822 qemu_aio_wait();
823 }
824
825 return 0;
98392453
RS
826 default:
827 return -1;
828 }
829 return 0;
830}
831#endif
832
c589b249
RS
833static int64_t
834iscsi_getlength(BlockDriverState *bs)
835{
836 IscsiLun *iscsilun = bs->opaque;
837 int64_t len;
838
839 len = iscsilun->num_blocks;
840 len *= iscsilun->block_size;
841
842 return len;
843}
844
f9dadc98
RS
845static int parse_chap(struct iscsi_context *iscsi, const char *target)
846{
847 QemuOptsList *list;
848 QemuOpts *opts;
849 const char *user = NULL;
850 const char *password = NULL;
851
852 list = qemu_find_opts("iscsi");
853 if (!list) {
854 return 0;
855 }
856
857 opts = qemu_opts_find(list, target);
858 if (opts == NULL) {
859 opts = QTAILQ_FIRST(&list->head);
860 if (!opts) {
861 return 0;
862 }
863 }
864
865 user = qemu_opt_get(opts, "user");
866 if (!user) {
867 return 0;
868 }
869
870 password = qemu_opt_get(opts, "password");
871 if (!password) {
872 error_report("CHAP username specified but no password was given");
873 return -1;
874 }
875
876 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
877 error_report("Failed to set initiator username and password");
878 return -1;
879 }
880
881 return 0;
882}
883
884static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
885{
886 QemuOptsList *list;
887 QemuOpts *opts;
888 const char *digest = NULL;
889
890 list = qemu_find_opts("iscsi");
891 if (!list) {
892 return;
893 }
894
895 opts = qemu_opts_find(list, target);
896 if (opts == NULL) {
897 opts = QTAILQ_FIRST(&list->head);
898 if (!opts) {
899 return;
900 }
901 }
902
903 digest = qemu_opt_get(opts, "header-digest");
904 if (!digest) {
905 return;
906 }
907
908 if (!strcmp(digest, "CRC32C")) {
909 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
910 } else if (!strcmp(digest, "NONE")) {
911 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
912 } else if (!strcmp(digest, "CRC32C-NONE")) {
913 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
914 } else if (!strcmp(digest, "NONE-CRC32C")) {
915 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
916 } else {
917 error_report("Invalid header-digest setting : %s", digest);
918 }
919}
920
921static char *parse_initiator_name(const char *target)
922{
923 QemuOptsList *list;
924 QemuOpts *opts;
925 const char *name = NULL;
31459f46 926 const char *iscsi_name = qemu_get_vm_name();
f9dadc98
RS
927
928 list = qemu_find_opts("iscsi");
f2ef4a6d
PB
929 if (list) {
930 opts = qemu_opts_find(list, target);
f9dadc98 931 if (!opts) {
f2ef4a6d
PB
932 opts = QTAILQ_FIRST(&list->head);
933 }
934 if (opts) {
935 name = qemu_opt_get(opts, "initiator-name");
f9dadc98
RS
936 }
937 }
938
f2ef4a6d
PB
939 if (name) {
940 return g_strdup(name);
941 } else {
31459f46
RS
942 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
943 iscsi_name ? ":" : "",
944 iscsi_name ? iscsi_name : "");
f9dadc98 945 }
f9dadc98
RS
946}
947
5b5d34ec
PL
948#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
949static void iscsi_nop_timed_event(void *opaque)
950{
951 IscsiLun *iscsilun = opaque;
952
953 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
954 error_report("iSCSI: NOP timeout. Reconnecting...");
955 iscsi_reconnect(iscsilun->iscsi);
956 }
957
958 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
959 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
960 return;
961 }
962
bc72ad67 963 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
5b5d34ec
PL
964 iscsi_set_events(iscsilun);
965}
966#endif
967
cb1b83e7
PL
968static int iscsi_readcapacity_sync(IscsiLun *iscsilun)
969{
970 struct scsi_task *task = NULL;
971 struct scsi_readcapacity10 *rc10 = NULL;
972 struct scsi_readcapacity16 *rc16 = NULL;
973 int ret = 0;
974 int retries = ISCSI_CMD_RETRIES;
975
1288844e
PB
976 do {
977 if (task != NULL) {
978 scsi_free_scsi_task(task);
979 task = NULL;
cb1b83e7 980 }
1288844e
PB
981
982 switch (iscsilun->type) {
983 case TYPE_DISK:
984 task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
985 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
986 rc16 = scsi_datain_unmarshall(task);
987 if (rc16 == NULL) {
988 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
989 ret = -EINVAL;
990 } else {
991 iscsilun->block_size = rc16->block_length;
992 iscsilun->num_blocks = rc16->returned_lba + 1;
993 }
994 }
995 break;
996 case TYPE_ROM:
997 task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
998 if (task != NULL && task->status == SCSI_STATUS_GOOD) {
999 rc10 = scsi_datain_unmarshall(task);
1000 if (rc10 == NULL) {
1001 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
1002 ret = -EINVAL;
1003 } else {
1004 iscsilun->block_size = rc10->block_size;
1005 if (rc10->lba == 0) {
1006 /* blank disk loaded */
1007 iscsilun->num_blocks = 0;
1008 } else {
1009 iscsilun->num_blocks = rc10->lba + 1;
1010 }
1011 }
1012 }
1013 break;
1014 default:
1015 return 0;
cb1b83e7 1016 }
1288844e
PB
1017 } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1018 && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1019 && retries-- > 0);
cb1b83e7 1020
1288844e
PB
1021 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1022 error_report("iSCSI: failed to send readcapacity10 command.");
1023 ret = -EINVAL;
1024 }
cb1b83e7
PL
1025 if (task) {
1026 scsi_free_scsi_task(task);
1027 }
cb1b83e7
PL
1028 return ret;
1029}
1030
60beb341
KW
1031/* TODO Convert to fine grained options */
1032static QemuOptsList runtime_opts = {
1033 .name = "iscsi",
1034 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1035 .desc = {
1036 {
1037 .name = "filename",
1038 .type = QEMU_OPT_STRING,
1039 .help = "URL to the iscsi image",
1040 },
1041 { /* end of list */ }
1042 },
1043};
1044
c589b249
RS
1045/*
1046 * We support iscsi url's on the form
1047 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1048 */
56d1b4d2 1049static int iscsi_open(BlockDriverState *bs, QDict *options, int flags)
c589b249
RS
1050{
1051 IscsiLun *iscsilun = bs->opaque;
1052 struct iscsi_context *iscsi = NULL;
1053 struct iscsi_url *iscsi_url = NULL;
e829b0bb
PL
1054 struct scsi_task *task = NULL;
1055 struct scsi_inquiry_standard *inq = NULL;
f9dadc98 1056 char *initiator_name = NULL;
60beb341
KW
1057 QemuOpts *opts;
1058 Error *local_err = NULL;
1059 const char *filename;
c589b249
RS
1060 int ret;
1061
1062 if ((BDRV_SECTOR_SIZE % 512) != 0) {
1063 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1064 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1065 "of 512", BDRV_SECTOR_SIZE);
1066 return -EINVAL;
1067 }
1068
60beb341
KW
1069 opts = qemu_opts_create_nofail(&runtime_opts);
1070 qemu_opts_absorb_qdict(opts, options, &local_err);
1071 if (error_is_set(&local_err)) {
1072 qerror_report_err(local_err);
1073 error_free(local_err);
1074 ret = -EINVAL;
1075 goto out;
1076 }
1077
1078 filename = qemu_opt_get(opts, "filename");
1079
1080
c589b249
RS
1081 iscsi_url = iscsi_parse_full_url(iscsi, filename);
1082 if (iscsi_url == NULL) {
8da1e18b 1083 error_report("Failed to parse URL : %s", filename);
c589b249 1084 ret = -EINVAL;
b93c94f7 1085 goto out;
c589b249
RS
1086 }
1087
f9dadc98
RS
1088 memset(iscsilun, 0, sizeof(IscsiLun));
1089
1090 initiator_name = parse_initiator_name(iscsi_url->target);
1091
1092 iscsi = iscsi_create_context(initiator_name);
1093 if (iscsi == NULL) {
1094 error_report("iSCSI: Failed to create iSCSI context.");
1095 ret = -ENOMEM;
b93c94f7 1096 goto out;
f9dadc98
RS
1097 }
1098
c589b249
RS
1099 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1100 error_report("iSCSI: Failed to set target name.");
1101 ret = -EINVAL;
b93c94f7 1102 goto out;
c589b249
RS
1103 }
1104
1105 if (iscsi_url->user != NULL) {
1106 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1107 iscsi_url->passwd);
1108 if (ret != 0) {
1109 error_report("Failed to set initiator username and password");
1110 ret = -EINVAL;
b93c94f7 1111 goto out;
c589b249
RS
1112 }
1113 }
f9dadc98
RS
1114
1115 /* check if we got CHAP username/password via the options */
1116 if (parse_chap(iscsi, iscsi_url->target) != 0) {
1117 error_report("iSCSI: Failed to set CHAP user/password");
1118 ret = -EINVAL;
b93c94f7 1119 goto out;
f9dadc98
RS
1120 }
1121
c589b249
RS
1122 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1123 error_report("iSCSI: Failed to set session type to normal.");
1124 ret = -EINVAL;
b93c94f7 1125 goto out;
c589b249
RS
1126 }
1127
1128 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1129
f9dadc98
RS
1130 /* check if we got HEADER_DIGEST via the options */
1131 parse_header_digest(iscsi, iscsi_url->target);
1132
e829b0bb
PL
1133 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1134 error_report("iSCSI: Failed to connect to LUN : %s",
1135 iscsi_get_error(iscsi));
1136 ret = -EINVAL;
1137 goto out;
1138 }
c589b249
RS
1139
1140 iscsilun->iscsi = iscsi;
1141 iscsilun->lun = iscsi_url->lun;
1142
e829b0bb
PL
1143 task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
1144
1145 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1146 error_report("iSCSI: failed to send inquiry command.");
c589b249 1147 ret = -EINVAL;
b93c94f7 1148 goto out;
c589b249
RS
1149 }
1150
e829b0bb
PL
1151 inq = scsi_datain_unmarshall(task);
1152 if (inq == NULL) {
1153 error_report("iSCSI: Failed to unmarshall inquiry data.");
c589b249 1154 ret = -EINVAL;
b93c94f7 1155 goto out;
c589b249 1156 }
622695a4 1157
e829b0bb
PL
1158 iscsilun->type = inq->periperal_device_type;
1159
cb1b83e7
PL
1160 if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1161 goto out;
e829b0bb 1162 }
0777b5dd 1163 bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
e829b0bb 1164
622695a4
RS
1165 /* Medium changer or tape. We dont have any emulation for this so this must
1166 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1167 * to read from the device to guess the image format.
1168 */
1169 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1170 iscsilun->type == TYPE_TAPE) {
1171 bs->sg = 1;
1172 }
1173
5b5d34ec
PL
1174#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1175 /* Set up a timer for sending out iSCSI NOPs */
bc72ad67
AB
1176 iscsilun->nop_timer = timer_new_ms(QEMU_CLOCK_REALTIME, iscsi_nop_timed_event, iscsilun);
1177 timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
5b5d34ec
PL
1178#endif
1179
b93c94f7 1180out:
60beb341 1181 qemu_opts_del(opts);
f9dadc98
RS
1182 if (initiator_name != NULL) {
1183 g_free(initiator_name);
1184 }
c589b249
RS
1185 if (iscsi_url != NULL) {
1186 iscsi_destroy_url(iscsi_url);
1187 }
e829b0bb
PL
1188 if (task != NULL) {
1189 scsi_free_scsi_task(task);
1190 }
b93c94f7
PB
1191
1192 if (ret) {
1193 if (iscsi != NULL) {
1194 iscsi_destroy_context(iscsi);
1195 }
1196 memset(iscsilun, 0, sizeof(IscsiLun));
c589b249 1197 }
c589b249
RS
1198 return ret;
1199}
1200
1201static void iscsi_close(BlockDriverState *bs)
1202{
1203 IscsiLun *iscsilun = bs->opaque;
1204 struct iscsi_context *iscsi = iscsilun->iscsi;
1205
5b5d34ec 1206 if (iscsilun->nop_timer) {
bc72ad67
AB
1207 timer_del(iscsilun->nop_timer);
1208 timer_free(iscsilun->nop_timer);
5b5d34ec 1209 }
f2e5dca4 1210 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL);
c589b249
RS
1211 iscsi_destroy_context(iscsi);
1212 memset(iscsilun, 0, sizeof(IscsiLun));
1213}
1214
cb1b83e7
PL
1215static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1216{
1217 IscsiLun *iscsilun = bs->opaque;
1218 int ret = 0;
1219
1220 if (iscsilun->type != TYPE_DISK) {
1221 return -ENOTSUP;
1222 }
1223
1224 if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1225 return ret;
1226 }
1227
1228 if (offset > iscsi_getlength(bs)) {
1229 return -EINVAL;
1230 }
1231
1232 return 0;
1233}
1234
f807ecd5
PL
1235static int iscsi_has_zero_init(BlockDriverState *bs)
1236{
1237 return 0;
1238}
1239
de8864e5
PL
1240static int iscsi_create(const char *filename, QEMUOptionParameter *options)
1241{
1242 int ret = 0;
1243 int64_t total_size = 0;
13c91cb7 1244 BlockDriverState *bs;
de8864e5 1245 IscsiLun *iscsilun = NULL;
60beb341 1246 QDict *bs_options;
de8864e5 1247
13c91cb7 1248 bs = bdrv_new("");
de8864e5
PL
1249
1250 /* Read out options */
1251 while (options && options->name) {
1252 if (!strcmp(options->name, "size")) {
1253 total_size = options->value.n / BDRV_SECTOR_SIZE;
1254 }
1255 options++;
1256 }
1257
13c91cb7
FZ
1258 bs->opaque = g_malloc0(sizeof(struct IscsiLun));
1259 iscsilun = bs->opaque;
de8864e5 1260
60beb341
KW
1261 bs_options = qdict_new();
1262 qdict_put(bs_options, "filename", qstring_from_str(filename));
13c91cb7 1263 ret = iscsi_open(bs, bs_options, 0);
60beb341
KW
1264 QDECREF(bs_options);
1265
de8864e5
PL
1266 if (ret != 0) {
1267 goto out;
1268 }
5b5d34ec 1269 if (iscsilun->nop_timer) {
bc72ad67
AB
1270 timer_del(iscsilun->nop_timer);
1271 timer_free(iscsilun->nop_timer);
5b5d34ec 1272 }
de8864e5
PL
1273 if (iscsilun->type != TYPE_DISK) {
1274 ret = -ENODEV;
1275 goto out;
1276 }
13c91cb7 1277 if (bs->total_sectors < total_size) {
de8864e5 1278 ret = -ENOSPC;
d3bda7bc 1279 goto out;
de8864e5
PL
1280 }
1281
1282 ret = 0;
1283out:
1284 if (iscsilun->iscsi != NULL) {
1285 iscsi_destroy_context(iscsilun->iscsi);
1286 }
13c91cb7
FZ
1287 g_free(bs->opaque);
1288 bs->opaque = NULL;
1289 bdrv_delete(bs);
de8864e5
PL
1290 return ret;
1291}
1292
1293static QEMUOptionParameter iscsi_create_options[] = {
1294 {
1295 .name = BLOCK_OPT_SIZE,
1296 .type = OPT_SIZE,
1297 .help = "Virtual disk size"
1298 },
1299 { NULL }
1300};
1301
c589b249
RS
1302static BlockDriver bdrv_iscsi = {
1303 .format_name = "iscsi",
1304 .protocol_name = "iscsi",
1305
1306 .instance_size = sizeof(IscsiLun),
1307 .bdrv_file_open = iscsi_open,
1308 .bdrv_close = iscsi_close,
de8864e5
PL
1309 .bdrv_create = iscsi_create,
1310 .create_options = iscsi_create_options,
c589b249
RS
1311
1312 .bdrv_getlength = iscsi_getlength,
cb1b83e7 1313 .bdrv_truncate = iscsi_truncate,
c589b249
RS
1314
1315 .bdrv_aio_readv = iscsi_aio_readv,
1316 .bdrv_aio_writev = iscsi_aio_writev,
1317 .bdrv_aio_flush = iscsi_aio_flush,
fa6acb0c
RS
1318
1319 .bdrv_aio_discard = iscsi_aio_discard,
f807ecd5 1320 .bdrv_has_zero_init = iscsi_has_zero_init,
98392453
RS
1321
1322#ifdef __linux__
1323 .bdrv_ioctl = iscsi_ioctl,
1324 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1325#endif
c589b249
RS
1326};
1327
4d454574
PB
1328static QemuOptsList qemu_iscsi_opts = {
1329 .name = "iscsi",
1330 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1331 .desc = {
1332 {
1333 .name = "user",
1334 .type = QEMU_OPT_STRING,
1335 .help = "username for CHAP authentication to target",
1336 },{
1337 .name = "password",
1338 .type = QEMU_OPT_STRING,
1339 .help = "password for CHAP authentication to target",
1340 },{
1341 .name = "header-digest",
1342 .type = QEMU_OPT_STRING,
1343 .help = "HeaderDigest setting. "
1344 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1345 },{
1346 .name = "initiator-name",
1347 .type = QEMU_OPT_STRING,
1348 .help = "Initiator iqn name to use when connecting",
1349 },
1350 { /* end of list */ }
1351 },
1352};
1353
c589b249
RS
1354static void iscsi_block_init(void)
1355{
1356 bdrv_register(&bdrv_iscsi);
4d454574 1357 qemu_add_opts(&qemu_iscsi_opts);
c589b249
RS
1358}
1359
1360block_init(iscsi_block_init);