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