Statistics
| Branch: | Revision:

root / block / iscsi.c @ 7572ddc8

History | View | Annotate | Download (45.8 kB)

1
/*
2
 * QEMU Block driver for iSCSI images
3
 *
4
 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5
 * Copyright (c) 2012-2013 Peter Lieven <pl@kamp.de>
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25

    
26
#include "config-host.h"
27

    
28
#include <poll.h>
29
#include <arpa/inet.h>
30
#include "qemu-common.h"
31
#include "qemu/config-file.h"
32
#include "qemu/error-report.h"
33
#include "block/block_int.h"
34
#include "trace.h"
35
#include "block/scsi.h"
36
#include "qemu/iov.h"
37
#include "sysemu/sysemu.h"
38
#include "qmp-commands.h"
39

    
40
#include <iscsi/iscsi.h>
41
#include <iscsi/scsi-lowlevel.h>
42

    
43
#ifdef __linux__
44
#include <scsi/sg.h>
45
#include <block/scsi.h>
46
#endif
47

    
48
typedef struct IscsiLun {
49
    struct iscsi_context *iscsi;
50
    int lun;
51
    enum scsi_inquiry_peripheral_device_type type;
52
    int block_size;
53
    uint64_t num_blocks;
54
    int events;
55
    QEMUTimer *nop_timer;
56
    uint8_t lbpme;
57
    uint8_t lbprz;
58
    uint8_t has_write_same;
59
    struct scsi_inquiry_logical_block_provisioning lbp;
60
    struct scsi_inquiry_block_limits bl;
61
    unsigned char *zeroblock;
62
} IscsiLun;
63

    
64
typedef struct IscsiTask {
65
    int status;
66
    int complete;
67
    int retries;
68
    int do_retry;
69
    struct scsi_task *task;
70
    Coroutine *co;
71
} IscsiTask;
72

    
73
typedef struct IscsiAIOCB {
74
    BlockDriverAIOCB common;
75
    QEMUIOVector *qiov;
76
    QEMUBH *bh;
77
    IscsiLun *iscsilun;
78
    struct scsi_task *task;
79
    uint8_t *buf;
80
    int status;
81
    int canceled;
82
    int retries;
83
    int64_t sector_num;
84
    int nb_sectors;
85
#ifdef __linux__
86
    sg_io_hdr_t *ioh;
87
#endif
88
} IscsiAIOCB;
89

    
90
#define NOP_INTERVAL 5000
91
#define MAX_NOP_FAILURES 3
92
#define ISCSI_CMD_RETRIES 5
93

    
94
static void
95
iscsi_bh_cb(void *p)
96
{
97
    IscsiAIOCB *acb = p;
98

    
99
    qemu_bh_delete(acb->bh);
100

    
101
    g_free(acb->buf);
102
    acb->buf = NULL;
103

    
104
    if (acb->canceled == 0) {
105
        acb->common.cb(acb->common.opaque, acb->status);
106
    }
107

    
108
    if (acb->task != NULL) {
109
        scsi_free_scsi_task(acb->task);
110
        acb->task = NULL;
111
    }
112

    
113
    qemu_aio_release(acb);
114
}
115

    
116
static void
117
iscsi_schedule_bh(IscsiAIOCB *acb)
118
{
119
    if (acb->bh) {
120
        return;
121
    }
122
    acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
123
    qemu_bh_schedule(acb->bh);
124
}
125

    
126
static void
127
iscsi_co_generic_cb(struct iscsi_context *iscsi, int status,
128
                        void *command_data, void *opaque)
129
{
130
    struct IscsiTask *iTask = opaque;
131
    struct scsi_task *task = command_data;
132

    
133
    iTask->complete = 1;
134
    iTask->status = status;
135
    iTask->do_retry = 0;
136
    iTask->task = task;
137

    
138
    if (iTask->retries-- > 0 && status == SCSI_STATUS_CHECK_CONDITION
139
        && task->sense.key == SCSI_SENSE_UNIT_ATTENTION) {
140
        iTask->do_retry = 1;
141
        goto out;
142
    }
143

    
144
    if (status != SCSI_STATUS_GOOD) {
145
        error_report("iSCSI: Failure. %s", iscsi_get_error(iscsi));
146
    }
147

    
148
out:
149
    if (iTask->co) {
150
        qemu_coroutine_enter(iTask->co, NULL);
151
    }
152
}
153

    
154
static void iscsi_co_init_iscsitask(IscsiLun *iscsilun, struct IscsiTask *iTask)
155
{
156
    *iTask = (struct IscsiTask) {
157
        .co         = qemu_coroutine_self(),
158
        .retries    = ISCSI_CMD_RETRIES,
159
    };
160
}
161

    
162
static void
163
iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
164
                    void *private_data)
165
{
166
    IscsiAIOCB *acb = private_data;
167

    
168
    acb->status = -ECANCELED;
169
    iscsi_schedule_bh(acb);
170
}
171

    
172
static void
173
iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
174
{
175
    IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
176
    IscsiLun *iscsilun = acb->iscsilun;
177

    
178
    if (acb->status != -EINPROGRESS) {
179
        return;
180
    }
181

    
182
    acb->canceled = 1;
183

    
184
    /* send a task mgmt call to the target to cancel the task on the target */
185
    iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
186
                                     iscsi_abort_task_cb, acb);
187

    
188
    while (acb->status == -EINPROGRESS) {
189
        qemu_aio_wait();
190
    }
191
}
192

    
193
static const AIOCBInfo iscsi_aiocb_info = {
194
    .aiocb_size         = sizeof(IscsiAIOCB),
195
    .cancel             = iscsi_aio_cancel,
196
};
197

    
198

    
199
static void iscsi_process_read(void *arg);
200
static void iscsi_process_write(void *arg);
201

    
202
static void
203
iscsi_set_events(IscsiLun *iscsilun)
204
{
205
    struct iscsi_context *iscsi = iscsilun->iscsi;
206
    int ev;
207

    
208
    /* We always register a read handler.  */
209
    ev = POLLIN;
210
    ev |= iscsi_which_events(iscsi);
211
    if (ev != iscsilun->events) {
212
        qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
213
                      iscsi_process_read,
214
                      (ev & POLLOUT) ? iscsi_process_write : NULL,
215
                      iscsilun);
216

    
217
    }
218

    
219
    iscsilun->events = ev;
220
}
221

    
222
static void
223
iscsi_process_read(void *arg)
224
{
225
    IscsiLun *iscsilun = arg;
226
    struct iscsi_context *iscsi = iscsilun->iscsi;
227

    
228
    iscsi_service(iscsi, POLLIN);
229
    iscsi_set_events(iscsilun);
230
}
231

    
232
static void
233
iscsi_process_write(void *arg)
234
{
235
    IscsiLun *iscsilun = arg;
236
    struct iscsi_context *iscsi = iscsilun->iscsi;
237

    
238
    iscsi_service(iscsi, POLLOUT);
239
    iscsi_set_events(iscsilun);
240
}
241

    
242
static int
243
iscsi_aio_writev_acb(IscsiAIOCB *acb);
244

    
245
static void
246
iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
247
                     void *command_data, void *opaque)
248
{
249
    IscsiAIOCB *acb = opaque;
250

    
251
    trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
252

    
253
    g_free(acb->buf);
254
    acb->buf = NULL;
255

    
256
    if (acb->canceled != 0) {
257
        return;
258
    }
259

    
260
    acb->status = 0;
261
    if (status != 0) {
262
        if (status == SCSI_STATUS_CHECK_CONDITION
263
            && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
264
            && acb->retries-- > 0) {
265
            scsi_free_scsi_task(acb->task);
266
            acb->task = NULL;
267
            if (iscsi_aio_writev_acb(acb) == 0) {
268
                iscsi_set_events(acb->iscsilun);
269
                return;
270
            }
271
        }
272
        error_report("Failed to write16 data to iSCSI lun. %s",
273
                     iscsi_get_error(iscsi));
274
        acb->status = -EIO;
275
    }
276

    
277
    iscsi_schedule_bh(acb);
278
}
279

    
280
static int64_t sector_lun2qemu(int64_t sector, IscsiLun *iscsilun)
281
{
282
    return sector * iscsilun->block_size / BDRV_SECTOR_SIZE;
283
}
284

    
285
static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
286
{
287
    return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
288
}
289

    
290
static bool is_request_lun_aligned(int64_t sector_num, int nb_sectors,
291
                                      IscsiLun *iscsilun)
292
{
293
    if ((sector_num * BDRV_SECTOR_SIZE) % iscsilun->block_size ||
294
        (nb_sectors * BDRV_SECTOR_SIZE) % iscsilun->block_size) {
295
            error_report("iSCSI misaligned request: "
296
                         "iscsilun->block_size %u, sector_num %" PRIi64
297
                         ", nb_sectors %d",
298
                         iscsilun->block_size, sector_num, nb_sectors);
299
            return 0;
300
    }
301
    return 1;
302
}
303

    
304
static int
305
iscsi_aio_writev_acb(IscsiAIOCB *acb)
306
{
307
    struct iscsi_context *iscsi = acb->iscsilun->iscsi;
308
    size_t size;
309
    uint32_t num_sectors;
310
    uint64_t lba;
311
#if !defined(LIBISCSI_FEATURE_IOVECTOR)
312
    struct iscsi_data data;
313
#endif
314
    int ret;
315

    
316
    acb->canceled   = 0;
317
    acb->bh         = NULL;
318
    acb->status     = -EINPROGRESS;
319
    acb->buf        = NULL;
320

    
321
    /* this will allow us to get rid of 'buf' completely */
322
    size = acb->nb_sectors * BDRV_SECTOR_SIZE;
323

    
324
#if !defined(LIBISCSI_FEATURE_IOVECTOR)
325
    data.size = MIN(size, acb->qiov->size);
326

    
327
    /* if the iovec only contains one buffer we can pass it directly */
328
    if (acb->qiov->niov == 1) {
329
        data.data = acb->qiov->iov[0].iov_base;
330
    } else {
331
        acb->buf = g_malloc(data.size);
332
        qemu_iovec_to_buf(acb->qiov, 0, acb->buf, data.size);
333
        data.data = acb->buf;
334
    }
335
#endif
336

    
337
    acb->task = malloc(sizeof(struct scsi_task));
338
    if (acb->task == NULL) {
339
        error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
340
                     "command. %s", iscsi_get_error(iscsi));
341
        return -1;
342
    }
343
    memset(acb->task, 0, sizeof(struct scsi_task));
344

    
345
    acb->task->xfer_dir = SCSI_XFER_WRITE;
346
    acb->task->cdb_size = 16;
347
    acb->task->cdb[0] = 0x8a;
348
    lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
349
    *(uint32_t *)&acb->task->cdb[2]  = htonl(lba >> 32);
350
    *(uint32_t *)&acb->task->cdb[6]  = htonl(lba & 0xffffffff);
351
    num_sectors = sector_qemu2lun(acb->nb_sectors, acb->iscsilun);
352
    *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
353
    acb->task->expxferlen = size;
354

    
355
#if defined(LIBISCSI_FEATURE_IOVECTOR)
356
    ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
357
                                   iscsi_aio_write16_cb,
358
                                   NULL,
359
                                   acb);
360
#else
361
    ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
362
                                   iscsi_aio_write16_cb,
363
                                   &data,
364
                                   acb);
365
#endif
366
    if (ret != 0) {
367
        scsi_free_scsi_task(acb->task);
368
        g_free(acb->buf);
369
        return -1;
370
    }
371

    
372
#if defined(LIBISCSI_FEATURE_IOVECTOR)
373
    scsi_task_set_iov_out(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
374
#endif
375

    
376
    return 0;
377
}
378

    
379
static BlockDriverAIOCB *
380
iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
381
                 QEMUIOVector *qiov, int nb_sectors,
382
                 BlockDriverCompletionFunc *cb,
383
                 void *opaque)
384
{
385
    IscsiLun *iscsilun = bs->opaque;
386
    IscsiAIOCB *acb;
387

    
388
    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
389
        return NULL;
390
    }
391

    
392
    acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
393
    trace_iscsi_aio_writev(iscsilun->iscsi, sector_num, nb_sectors, opaque, acb);
394

    
395
    acb->iscsilun    = iscsilun;
396
    acb->qiov        = qiov;
397
    acb->nb_sectors  = nb_sectors;
398
    acb->sector_num  = sector_num;
399
    acb->retries     = ISCSI_CMD_RETRIES;
400

    
401
    if (iscsi_aio_writev_acb(acb) != 0) {
402
        qemu_aio_release(acb);
403
        return NULL;
404
    }
405

    
406
    iscsi_set_events(iscsilun);
407
    return &acb->common;
408
}
409

    
410
static int
411
iscsi_aio_readv_acb(IscsiAIOCB *acb);
412

    
413
static void
414
iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
415
                    void *command_data, void *opaque)
416
{
417
    IscsiAIOCB *acb = opaque;
418

    
419
    trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
420

    
421
    if (acb->canceled != 0) {
422
        return;
423
    }
424

    
425
    acb->status = 0;
426
    if (status != 0) {
427
        if (status == SCSI_STATUS_CHECK_CONDITION
428
            && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
429
            && acb->retries-- > 0) {
430
            scsi_free_scsi_task(acb->task);
431
            acb->task = NULL;
432
            if (iscsi_aio_readv_acb(acb) == 0) {
433
                iscsi_set_events(acb->iscsilun);
434
                return;
435
            }
436
        }
437
        error_report("Failed to read16 data from iSCSI lun. %s",
438
                     iscsi_get_error(iscsi));
439
        acb->status = -EIO;
440
    }
441

    
442
    iscsi_schedule_bh(acb);
443
}
444

    
445
static int
446
iscsi_aio_readv_acb(IscsiAIOCB *acb)
447
{
448
    struct iscsi_context *iscsi = acb->iscsilun->iscsi;
449
    size_t size;
450
    uint64_t lba;
451
    uint32_t num_sectors;
452
    int ret;
453
#if !defined(LIBISCSI_FEATURE_IOVECTOR)
454
    int i;
455
#endif
456

    
457
    acb->canceled    = 0;
458
    acb->bh          = NULL;
459
    acb->status      = -EINPROGRESS;
460
    acb->buf         = NULL;
461

    
462
    size = acb->nb_sectors * BDRV_SECTOR_SIZE;
463

    
464
    acb->task = malloc(sizeof(struct scsi_task));
465
    if (acb->task == NULL) {
466
        error_report("iSCSI: Failed to allocate task for scsi READ16 "
467
                     "command. %s", iscsi_get_error(iscsi));
468
        return -1;
469
    }
470
    memset(acb->task, 0, sizeof(struct scsi_task));
471

    
472
    acb->task->xfer_dir = SCSI_XFER_READ;
473
    acb->task->expxferlen = size;
474
    lba = sector_qemu2lun(acb->sector_num, acb->iscsilun);
475
    num_sectors = sector_qemu2lun(acb->nb_sectors, acb->iscsilun);
476

    
477
    switch (acb->iscsilun->type) {
478
    case TYPE_DISK:
479
        acb->task->cdb_size = 16;
480
        acb->task->cdb[0]  = 0x88;
481
        *(uint32_t *)&acb->task->cdb[2]  = htonl(lba >> 32);
482
        *(uint32_t *)&acb->task->cdb[6]  = htonl(lba & 0xffffffff);
483
        *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
484
        break;
485
    default:
486
        acb->task->cdb_size = 10;
487
        acb->task->cdb[0]  = 0x28;
488
        *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
489
        *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
490
        break;
491
    }
492

    
493
    ret = iscsi_scsi_command_async(iscsi, acb->iscsilun->lun, acb->task,
494
                                   iscsi_aio_read16_cb,
495
                                   NULL,
496
                                   acb);
497
    if (ret != 0) {
498
        scsi_free_scsi_task(acb->task);
499
        return -1;
500
    }
501

    
502
#if defined(LIBISCSI_FEATURE_IOVECTOR)
503
    scsi_task_set_iov_in(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
504
#else
505
    for (i = 0; i < acb->qiov->niov; i++) {
506
        scsi_task_add_data_in_buffer(acb->task,
507
                acb->qiov->iov[i].iov_len,
508
                acb->qiov->iov[i].iov_base);
509
    }
510
#endif
511
    return 0;
512
}
513

    
514
static BlockDriverAIOCB *
515
iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
516
                QEMUIOVector *qiov, int nb_sectors,
517
                BlockDriverCompletionFunc *cb,
518
                void *opaque)
519
{
520
    IscsiLun *iscsilun = bs->opaque;
521
    IscsiAIOCB *acb;
522

    
523
    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
524
        return NULL;
525
    }
526

    
527
    acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
528
    trace_iscsi_aio_readv(iscsilun->iscsi, sector_num, nb_sectors, opaque, acb);
529

    
530
    acb->nb_sectors  = nb_sectors;
531
    acb->sector_num  = sector_num;
532
    acb->iscsilun    = iscsilun;
533
    acb->qiov        = qiov;
534
    acb->retries     = ISCSI_CMD_RETRIES;
535

    
536
    if (iscsi_aio_readv_acb(acb) != 0) {
537
        qemu_aio_release(acb);
538
        return NULL;
539
    }
540

    
541
    iscsi_set_events(iscsilun);
542
    return &acb->common;
543
}
544

    
545
static int
546
iscsi_aio_flush_acb(IscsiAIOCB *acb);
547

    
548
static void
549
iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
550
                     void *command_data, void *opaque)
551
{
552
    IscsiAIOCB *acb = opaque;
553

    
554
    if (acb->canceled != 0) {
555
        return;
556
    }
557

    
558
    acb->status = 0;
559
    if (status != 0) {
560
        if (status == SCSI_STATUS_CHECK_CONDITION
561
            && acb->task->sense.key == SCSI_SENSE_UNIT_ATTENTION
562
            && acb->retries-- > 0) {
563
            scsi_free_scsi_task(acb->task);
564
            acb->task = NULL;
565
            if (iscsi_aio_flush_acb(acb) == 0) {
566
                iscsi_set_events(acb->iscsilun);
567
                return;
568
            }
569
        }
570
        error_report("Failed to sync10 data on iSCSI lun. %s",
571
                     iscsi_get_error(iscsi));
572
        acb->status = -EIO;
573
    }
574

    
575
    iscsi_schedule_bh(acb);
576
}
577

    
578
static int
579
iscsi_aio_flush_acb(IscsiAIOCB *acb)
580
{
581
    struct iscsi_context *iscsi = acb->iscsilun->iscsi;
582

    
583
    acb->canceled   = 0;
584
    acb->bh         = NULL;
585
    acb->status     = -EINPROGRESS;
586
    acb->buf        = NULL;
587

    
588
    acb->task = iscsi_synchronizecache10_task(iscsi, acb->iscsilun->lun,
589
                                         0, 0, 0, 0,
590
                                         iscsi_synccache10_cb,
591
                                         acb);
592
    if (acb->task == NULL) {
593
        error_report("iSCSI: Failed to send synchronizecache10 command. %s",
594
                     iscsi_get_error(iscsi));
595
        return -1;
596
    }
597

    
598
    return 0;
599
}
600

    
601
static BlockDriverAIOCB *
602
iscsi_aio_flush(BlockDriverState *bs,
603
                BlockDriverCompletionFunc *cb, void *opaque)
604
{
605
    IscsiLun *iscsilun = bs->opaque;
606

    
607
    IscsiAIOCB *acb;
608

    
609
    acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
610

    
611
    acb->iscsilun    = iscsilun;
612
    acb->retries     = ISCSI_CMD_RETRIES;
613

    
614
    if (iscsi_aio_flush_acb(acb) != 0) {
615
        qemu_aio_release(acb);
616
        return NULL;
617
    }
618

    
619
    iscsi_set_events(iscsilun);
620

    
621
    return &acb->common;
622
}
623

    
624
#ifdef __linux__
625
static void
626
iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
627
                     void *command_data, void *opaque)
628
{
629
    IscsiAIOCB *acb = opaque;
630

    
631
    g_free(acb->buf);
632
    acb->buf = NULL;
633

    
634
    if (acb->canceled != 0) {
635
        return;
636
    }
637

    
638
    acb->status = 0;
639
    if (status < 0) {
640
        error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
641
                     iscsi_get_error(iscsi));
642
        acb->status = -EIO;
643
    }
644

    
645
    acb->ioh->driver_status = 0;
646
    acb->ioh->host_status   = 0;
647
    acb->ioh->resid         = 0;
648

    
649
#define SG_ERR_DRIVER_SENSE    0x08
650

    
651
    if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
652
        int ss;
653

    
654
        acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
655

    
656
        acb->ioh->sb_len_wr = acb->task->datain.size - 2;
657
        ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
658
             acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
659
        memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
660
    }
661

    
662
    iscsi_schedule_bh(acb);
663
}
664

    
665
static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
666
        unsigned long int req, void *buf,
667
        BlockDriverCompletionFunc *cb, void *opaque)
668
{
669
    IscsiLun *iscsilun = bs->opaque;
670
    struct iscsi_context *iscsi = iscsilun->iscsi;
671
    struct iscsi_data data;
672
    IscsiAIOCB *acb;
673

    
674
    assert(req == SG_IO);
675

    
676
    acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
677

    
678
    acb->iscsilun = iscsilun;
679
    acb->canceled    = 0;
680
    acb->bh          = NULL;
681
    acb->status      = -EINPROGRESS;
682
    acb->buf         = NULL;
683
    acb->ioh         = buf;
684

    
685
    acb->task = malloc(sizeof(struct scsi_task));
686
    if (acb->task == NULL) {
687
        error_report("iSCSI: Failed to allocate task for scsi command. %s",
688
                     iscsi_get_error(iscsi));
689
        qemu_aio_release(acb);
690
        return NULL;
691
    }
692
    memset(acb->task, 0, sizeof(struct scsi_task));
693

    
694
    switch (acb->ioh->dxfer_direction) {
695
    case SG_DXFER_TO_DEV:
696
        acb->task->xfer_dir = SCSI_XFER_WRITE;
697
        break;
698
    case SG_DXFER_FROM_DEV:
699
        acb->task->xfer_dir = SCSI_XFER_READ;
700
        break;
701
    default:
702
        acb->task->xfer_dir = SCSI_XFER_NONE;
703
        break;
704
    }
705

    
706
    acb->task->cdb_size = acb->ioh->cmd_len;
707
    memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
708
    acb->task->expxferlen = acb->ioh->dxfer_len;
709

    
710
    data.size = 0;
711
    if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
712
        if (acb->ioh->iovec_count == 0) {
713
            data.data = acb->ioh->dxferp;
714
            data.size = acb->ioh->dxfer_len;
715
        } else {
716
#if defined(LIBISCSI_FEATURE_IOVECTOR)
717
            scsi_task_set_iov_out(acb->task,
718
                                 (struct scsi_iovec *) acb->ioh->dxferp,
719
                                 acb->ioh->iovec_count);
720
#else
721
            struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
722

    
723
            acb->buf = g_malloc(acb->ioh->dxfer_len);
724
            data.data = acb->buf;
725
            data.size = iov_to_buf(iov, acb->ioh->iovec_count, 0,
726
                                   acb->buf, acb->ioh->dxfer_len);
727
#endif
728
        }
729
    }
730

    
731
    if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
732
                                 iscsi_aio_ioctl_cb,
733
                                 (data.size > 0) ? &data : NULL,
734
                                 acb) != 0) {
735
        scsi_free_scsi_task(acb->task);
736
        qemu_aio_release(acb);
737
        return NULL;
738
    }
739

    
740
    /* tell libiscsi to read straight into the buffer we got from ioctl */
741
    if (acb->task->xfer_dir == SCSI_XFER_READ) {
742
        if (acb->ioh->iovec_count == 0) {
743
            scsi_task_add_data_in_buffer(acb->task,
744
                                         acb->ioh->dxfer_len,
745
                                         acb->ioh->dxferp);
746
        } else {
747
#if defined(LIBISCSI_FEATURE_IOVECTOR)
748
            scsi_task_set_iov_in(acb->task,
749
                                 (struct scsi_iovec *) acb->ioh->dxferp,
750
                                 acb->ioh->iovec_count);
751
#else
752
            int i;
753
            for (i = 0; i < acb->ioh->iovec_count; i++) {
754
                struct iovec *iov = (struct iovec *)acb->ioh->dxferp;
755

    
756
                scsi_task_add_data_in_buffer(acb->task,
757
                    iov[i].iov_len,
758
                    iov[i].iov_base);
759
            }
760
#endif
761
        }
762
    }
763

    
764
    iscsi_set_events(iscsilun);
765

    
766
    return &acb->common;
767
}
768

    
769

    
770
static void ioctl_cb(void *opaque, int status)
771
{
772
    int *p_status = opaque;
773
    *p_status = status;
774
}
775

    
776
static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
777
{
778
    IscsiLun *iscsilun = bs->opaque;
779
    int status;
780

    
781
    switch (req) {
782
    case SG_GET_VERSION_NUM:
783
        *(int *)buf = 30000;
784
        break;
785
    case SG_GET_SCSI_ID:
786
        ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
787
        break;
788
    case SG_IO:
789
        status = -EINPROGRESS;
790
        iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
791

    
792
        while (status == -EINPROGRESS) {
793
            qemu_aio_wait();
794
        }
795

    
796
        return 0;
797
    default:
798
        return -1;
799
    }
800
    return 0;
801
}
802
#endif
803

    
804
static int64_t
805
iscsi_getlength(BlockDriverState *bs)
806
{
807
    IscsiLun *iscsilun = bs->opaque;
808
    int64_t len;
809

    
810
    len  = iscsilun->num_blocks;
811
    len *= iscsilun->block_size;
812

    
813
    return len;
814
}
815

    
816
#if defined(LIBISCSI_FEATURE_IOVECTOR)
817

    
818
static int64_t coroutine_fn iscsi_co_get_block_status(BlockDriverState *bs,
819
                                                  int64_t sector_num,
820
                                                  int nb_sectors, int *pnum)
821
{
822
    IscsiLun *iscsilun = bs->opaque;
823
    struct scsi_get_lba_status *lbas = NULL;
824
    struct scsi_lba_status_descriptor *lbasd = NULL;
825
    struct IscsiTask iTask;
826
    int64_t ret;
827

    
828
    iscsi_co_init_iscsitask(iscsilun, &iTask);
829

    
830
    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
831
        ret = -EINVAL;
832
        goto out;
833
    }
834

    
835
    /* default to all sectors allocated */
836
    ret = BDRV_BLOCK_DATA;
837
    ret |= (sector_num << BDRV_SECTOR_BITS) | BDRV_BLOCK_OFFSET_VALID;
838
    *pnum = nb_sectors;
839

    
840
    /* LUN does not support logical block provisioning */
841
    if (iscsilun->lbpme == 0) {
842
        goto out;
843
    }
844

    
845
retry:
846
    if (iscsi_get_lba_status_task(iscsilun->iscsi, iscsilun->lun,
847
                                  sector_qemu2lun(sector_num, iscsilun),
848
                                  8 + 16, iscsi_co_generic_cb,
849
                                  &iTask) == NULL) {
850
        ret = -EIO;
851
        goto out;
852
    }
853

    
854
    while (!iTask.complete) {
855
        iscsi_set_events(iscsilun);
856
        qemu_coroutine_yield();
857
    }
858

    
859
    if (iTask.do_retry) {
860
        if (iTask.task != NULL) {
861
            scsi_free_scsi_task(iTask.task);
862
            iTask.task = NULL;
863
        }
864
        goto retry;
865
    }
866

    
867
    if (iTask.status != SCSI_STATUS_GOOD) {
868
        /* in case the get_lba_status_callout fails (i.e.
869
         * because the device is busy or the cmd is not
870
         * supported) we pretend all blocks are allocated
871
         * for backwards compatibility */
872
        goto out;
873
    }
874

    
875
    lbas = scsi_datain_unmarshall(iTask.task);
876
    if (lbas == NULL) {
877
        ret = -EIO;
878
        goto out;
879
    }
880

    
881
    lbasd = &lbas->descriptors[0];
882

    
883
    if (sector_qemu2lun(sector_num, iscsilun) != lbasd->lba) {
884
        ret = -EIO;
885
        goto out;
886
    }
887

    
888
    *pnum = sector_lun2qemu(lbasd->num_blocks, iscsilun);
889
    if (*pnum > nb_sectors) {
890
        *pnum = nb_sectors;
891
    }
892

    
893
    if (lbasd->provisioning == SCSI_PROVISIONING_TYPE_DEALLOCATED ||
894
        lbasd->provisioning == SCSI_PROVISIONING_TYPE_ANCHORED) {
895
        ret &= ~BDRV_BLOCK_DATA;
896
        if (iscsilun->lbprz) {
897
            ret |= BDRV_BLOCK_ZERO;
898
        }
899
    }
900

    
901
out:
902
    if (iTask.task != NULL) {
903
        scsi_free_scsi_task(iTask.task);
904
    }
905
    return ret;
906
}
907

    
908
#endif /* LIBISCSI_FEATURE_IOVECTOR */
909

    
910
static int
911
coroutine_fn iscsi_co_discard(BlockDriverState *bs, int64_t sector_num,
912
                                   int nb_sectors)
913
{
914
    IscsiLun *iscsilun = bs->opaque;
915
    struct IscsiTask iTask;
916
    struct unmap_list list;
917

    
918
    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
919
        return -EINVAL;
920
    }
921

    
922
    if (!iscsilun->lbp.lbpu) {
923
        /* UNMAP is not supported by the target */
924
        return 0;
925
    }
926

    
927
    list.lba = sector_qemu2lun(sector_num, iscsilun);
928
    list.num = sector_qemu2lun(nb_sectors, iscsilun);
929

    
930
    iscsi_co_init_iscsitask(iscsilun, &iTask);
931
retry:
932
    if (iscsi_unmap_task(iscsilun->iscsi, iscsilun->lun, 0, 0, &list, 1,
933
                     iscsi_co_generic_cb, &iTask) == NULL) {
934
        return -EIO;
935
    }
936

    
937
    while (!iTask.complete) {
938
        iscsi_set_events(iscsilun);
939
        qemu_coroutine_yield();
940
    }
941

    
942
    if (iTask.task != NULL) {
943
        scsi_free_scsi_task(iTask.task);
944
        iTask.task = NULL;
945
    }
946

    
947
    if (iTask.do_retry) {
948
        goto retry;
949
    }
950

    
951
    if (iTask.status == SCSI_STATUS_CHECK_CONDITION) {
952
        /* the target might fail with a check condition if it
953
           is not happy with the alignment of the UNMAP request
954
           we silently fail in this case */
955
        return 0;
956
    }
957

    
958
    if (iTask.status != SCSI_STATUS_GOOD) {
959
        return -EIO;
960
    }
961

    
962
    return 0;
963
}
964

    
965
#if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
966

    
967
static int
968
coroutine_fn iscsi_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
969
                                   int nb_sectors, BdrvRequestFlags flags)
970
{
971
    IscsiLun *iscsilun = bs->opaque;
972
    struct IscsiTask iTask;
973
    uint64_t lba;
974
    uint32_t nb_blocks;
975

    
976
    if (!is_request_lun_aligned(sector_num, nb_sectors, iscsilun)) {
977
        return -EINVAL;
978
    }
979

    
980
    if (!(flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->has_write_same) {
981
        /* WRITE SAME without UNMAP is not supported by the target */
982
        return -ENOTSUP;
983
    }
984

    
985
    if ((flags & BDRV_REQ_MAY_UNMAP) && !iscsilun->lbp.lbpws) {
986
        /* WRITE SAME with UNMAP is not supported by the target */
987
        return -ENOTSUP;
988
    }
989

    
990
    lba = sector_qemu2lun(sector_num, iscsilun);
991
    nb_blocks = sector_qemu2lun(nb_sectors, iscsilun);
992

    
993
    if (iscsilun->zeroblock == NULL) {
994
        iscsilun->zeroblock = g_malloc0(iscsilun->block_size);
995
    }
996

    
997
    iscsi_co_init_iscsitask(iscsilun, &iTask);
998
retry:
999
    if (iscsi_writesame16_task(iscsilun->iscsi, iscsilun->lun, lba,
1000
                               iscsilun->zeroblock, iscsilun->block_size,
1001
                               nb_blocks, 0, !!(flags & BDRV_REQ_MAY_UNMAP),
1002
                               0, 0, iscsi_co_generic_cb, &iTask) == NULL) {
1003
        return -EIO;
1004
    }
1005

    
1006
    while (!iTask.complete) {
1007
        iscsi_set_events(iscsilun);
1008
        qemu_coroutine_yield();
1009
    }
1010

    
1011
    if (iTask.task != NULL) {
1012
        scsi_free_scsi_task(iTask.task);
1013
        iTask.task = NULL;
1014
    }
1015

    
1016
    if (iTask.do_retry) {
1017
        goto retry;
1018
    }
1019

    
1020
    if (iTask.status != SCSI_STATUS_GOOD) {
1021
        if (iTask.status == SCSI_STATUS_CHECK_CONDITION &&
1022
            iTask.task->sense.key == SCSI_SENSE_ILLEGAL_REQUEST &&
1023
            iTask.task->sense.ascq == SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE) {
1024
            /* WRITE SAME is not supported by the target */
1025
            iscsilun->has_write_same = false;
1026
            return -ENOTSUP;
1027
        }
1028

    
1029
        return -EIO;
1030
    }
1031

    
1032
    return 0;
1033
}
1034

    
1035
#endif /* SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED */
1036

    
1037
static int parse_chap(struct iscsi_context *iscsi, const char *target)
1038
{
1039
    QemuOptsList *list;
1040
    QemuOpts *opts;
1041
    const char *user = NULL;
1042
    const char *password = NULL;
1043

    
1044
    list = qemu_find_opts("iscsi");
1045
    if (!list) {
1046
        return 0;
1047
    }
1048

    
1049
    opts = qemu_opts_find(list, target);
1050
    if (opts == NULL) {
1051
        opts = QTAILQ_FIRST(&list->head);
1052
        if (!opts) {
1053
            return 0;
1054
        }
1055
    }
1056

    
1057
    user = qemu_opt_get(opts, "user");
1058
    if (!user) {
1059
        return 0;
1060
    }
1061

    
1062
    password = qemu_opt_get(opts, "password");
1063
    if (!password) {
1064
        error_report("CHAP username specified but no password was given");
1065
        return -1;
1066
    }
1067

    
1068
    if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
1069
        error_report("Failed to set initiator username and password");
1070
        return -1;
1071
    }
1072

    
1073
    return 0;
1074
}
1075

    
1076
static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
1077
{
1078
    QemuOptsList *list;
1079
    QemuOpts *opts;
1080
    const char *digest = NULL;
1081

    
1082
    list = qemu_find_opts("iscsi");
1083
    if (!list) {
1084
        return;
1085
    }
1086

    
1087
    opts = qemu_opts_find(list, target);
1088
    if (opts == NULL) {
1089
        opts = QTAILQ_FIRST(&list->head);
1090
        if (!opts) {
1091
            return;
1092
        }
1093
    }
1094

    
1095
    digest = qemu_opt_get(opts, "header-digest");
1096
    if (!digest) {
1097
        return;
1098
    }
1099

    
1100
    if (!strcmp(digest, "CRC32C")) {
1101
        iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
1102
    } else if (!strcmp(digest, "NONE")) {
1103
        iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
1104
    } else if (!strcmp(digest, "CRC32C-NONE")) {
1105
        iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
1106
    } else if (!strcmp(digest, "NONE-CRC32C")) {
1107
        iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1108
    } else {
1109
        error_report("Invalid header-digest setting : %s", digest);
1110
    }
1111
}
1112

    
1113
static char *parse_initiator_name(const char *target)
1114
{
1115
    QemuOptsList *list;
1116
    QemuOpts *opts;
1117
    const char *name;
1118
    char *iscsi_name;
1119
    UuidInfo *uuid_info;
1120

    
1121
    list = qemu_find_opts("iscsi");
1122
    if (list) {
1123
        opts = qemu_opts_find(list, target);
1124
        if (!opts) {
1125
            opts = QTAILQ_FIRST(&list->head);
1126
        }
1127
        if (opts) {
1128
            name = qemu_opt_get(opts, "initiator-name");
1129
            if (name) {
1130
                return g_strdup(name);
1131
            }
1132
        }
1133
    }
1134

    
1135
    uuid_info = qmp_query_uuid(NULL);
1136
    if (strcmp(uuid_info->UUID, UUID_NONE) == 0) {
1137
        name = qemu_get_vm_name();
1138
    } else {
1139
        name = uuid_info->UUID;
1140
    }
1141
    iscsi_name = g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
1142
                                 name ? ":" : "", name ? name : "");
1143
    qapi_free_UuidInfo(uuid_info);
1144
    return iscsi_name;
1145
}
1146

    
1147
#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1148
static void iscsi_nop_timed_event(void *opaque)
1149
{
1150
    IscsiLun *iscsilun = opaque;
1151

    
1152
    if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
1153
        error_report("iSCSI: NOP timeout. Reconnecting...");
1154
        iscsi_reconnect(iscsilun->iscsi);
1155
    }
1156

    
1157
    if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
1158
        error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
1159
        return;
1160
    }
1161

    
1162
    timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1163
    iscsi_set_events(iscsilun);
1164
}
1165
#endif
1166

    
1167
static int iscsi_readcapacity_sync(IscsiLun *iscsilun)
1168
{
1169
    struct scsi_task *task = NULL;
1170
    struct scsi_readcapacity10 *rc10 = NULL;
1171
    struct scsi_readcapacity16 *rc16 = NULL;
1172
    int ret = 0;
1173
    int retries = ISCSI_CMD_RETRIES; 
1174

    
1175
    do {
1176
        if (task != NULL) {
1177
            scsi_free_scsi_task(task);
1178
            task = NULL;
1179
        }
1180

    
1181
        switch (iscsilun->type) {
1182
        case TYPE_DISK:
1183
            task = iscsi_readcapacity16_sync(iscsilun->iscsi, iscsilun->lun);
1184
            if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1185
                rc16 = scsi_datain_unmarshall(task);
1186
                if (rc16 == NULL) {
1187
                    error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
1188
                    ret = -EINVAL;
1189
                } else {
1190
                    iscsilun->block_size = rc16->block_length;
1191
                    iscsilun->num_blocks = rc16->returned_lba + 1;
1192
                    iscsilun->lbpme = rc16->lbpme;
1193
                    iscsilun->lbprz = rc16->lbprz;
1194
                }
1195
            }
1196
            break;
1197
        case TYPE_ROM:
1198
            task = iscsi_readcapacity10_sync(iscsilun->iscsi, iscsilun->lun, 0, 0);
1199
            if (task != NULL && task->status == SCSI_STATUS_GOOD) {
1200
                rc10 = scsi_datain_unmarshall(task);
1201
                if (rc10 == NULL) {
1202
                    error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
1203
                    ret = -EINVAL;
1204
                } else {
1205
                    iscsilun->block_size = rc10->block_size;
1206
                    if (rc10->lba == 0) {
1207
                        /* blank disk loaded */
1208
                        iscsilun->num_blocks = 0;
1209
                    } else {
1210
                        iscsilun->num_blocks = rc10->lba + 1;
1211
                    }
1212
                }
1213
            }
1214
            break;
1215
        default:
1216
            return 0;
1217
        }
1218
    } while (task != NULL && task->status == SCSI_STATUS_CHECK_CONDITION
1219
             && task->sense.key == SCSI_SENSE_UNIT_ATTENTION
1220
             && retries-- > 0);
1221

    
1222
    if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1223
        error_report("iSCSI: failed to send readcapacity10 command.");
1224
        ret = -EINVAL;
1225
    }
1226
    if (task) {
1227
        scsi_free_scsi_task(task);
1228
    }
1229
    return ret;
1230
}
1231

    
1232
/* TODO Convert to fine grained options */
1233
static QemuOptsList runtime_opts = {
1234
    .name = "iscsi",
1235
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
1236
    .desc = {
1237
        {
1238
            .name = "filename",
1239
            .type = QEMU_OPT_STRING,
1240
            .help = "URL to the iscsi image",
1241
        },
1242
        { /* end of list */ }
1243
    },
1244
};
1245

    
1246
static struct scsi_task *iscsi_do_inquiry(struct iscsi_context *iscsi,
1247
                                          int lun, int evpd, int pc) {
1248
        int full_size;
1249
        struct scsi_task *task = NULL;
1250
        task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, 64);
1251
        if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1252
            goto fail;
1253
        }
1254
        full_size = scsi_datain_getfullsize(task);
1255
        if (full_size > task->datain.size) {
1256
            scsi_free_scsi_task(task);
1257

    
1258
            /* we need more data for the full list */
1259
            task = iscsi_inquiry_sync(iscsi, lun, evpd, pc, full_size);
1260
            if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1261
                goto fail;
1262
            }
1263
        }
1264

    
1265
        return task;
1266

    
1267
fail:
1268
        error_report("iSCSI: Inquiry command failed : %s",
1269
                     iscsi_get_error(iscsi));
1270
        if (task) {
1271
            scsi_free_scsi_task(task);
1272
            return NULL;
1273
        }
1274
        return NULL;
1275
}
1276

    
1277
/*
1278
 * We support iscsi url's on the form
1279
 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1280
 */
1281
static int iscsi_open(BlockDriverState *bs, QDict *options, int flags,
1282
                      Error **errp)
1283
{
1284
    IscsiLun *iscsilun = bs->opaque;
1285
    struct iscsi_context *iscsi = NULL;
1286
    struct iscsi_url *iscsi_url = NULL;
1287
    struct scsi_task *task = NULL;
1288
    struct scsi_inquiry_standard *inq = NULL;
1289
    char *initiator_name = NULL;
1290
    QemuOpts *opts;
1291
    Error *local_err = NULL;
1292
    const char *filename;
1293
    int ret;
1294

    
1295
    if ((BDRV_SECTOR_SIZE % 512) != 0) {
1296
        error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1297
                     "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1298
                     "of 512", BDRV_SECTOR_SIZE);
1299
        return -EINVAL;
1300
    }
1301

    
1302
    opts = qemu_opts_create_nofail(&runtime_opts);
1303
    qemu_opts_absorb_qdict(opts, options, &local_err);
1304
    if (error_is_set(&local_err)) {
1305
        qerror_report_err(local_err);
1306
        error_free(local_err);
1307
        ret = -EINVAL;
1308
        goto out;
1309
    }
1310

    
1311
    filename = qemu_opt_get(opts, "filename");
1312

    
1313

    
1314
    iscsi_url = iscsi_parse_full_url(iscsi, filename);
1315
    if (iscsi_url == NULL) {
1316
        error_report("Failed to parse URL : %s", filename);
1317
        ret = -EINVAL;
1318
        goto out;
1319
    }
1320

    
1321
    memset(iscsilun, 0, sizeof(IscsiLun));
1322

    
1323
    initiator_name = parse_initiator_name(iscsi_url->target);
1324

    
1325
    iscsi = iscsi_create_context(initiator_name);
1326
    if (iscsi == NULL) {
1327
        error_report("iSCSI: Failed to create iSCSI context.");
1328
        ret = -ENOMEM;
1329
        goto out;
1330
    }
1331

    
1332
    if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
1333
        error_report("iSCSI: Failed to set target name.");
1334
        ret = -EINVAL;
1335
        goto out;
1336
    }
1337

    
1338
    if (iscsi_url->user != NULL) {
1339
        ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
1340
                                              iscsi_url->passwd);
1341
        if (ret != 0) {
1342
            error_report("Failed to set initiator username and password");
1343
            ret = -EINVAL;
1344
            goto out;
1345
        }
1346
    }
1347

    
1348
    /* check if we got CHAP username/password via the options */
1349
    if (parse_chap(iscsi, iscsi_url->target) != 0) {
1350
        error_report("iSCSI: Failed to set CHAP user/password");
1351
        ret = -EINVAL;
1352
        goto out;
1353
    }
1354

    
1355
    if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
1356
        error_report("iSCSI: Failed to set session type to normal.");
1357
        ret = -EINVAL;
1358
        goto out;
1359
    }
1360

    
1361
    iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
1362

    
1363
    /* check if we got HEADER_DIGEST via the options */
1364
    parse_header_digest(iscsi, iscsi_url->target);
1365

    
1366
    if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
1367
        error_report("iSCSI: Failed to connect to LUN : %s",
1368
            iscsi_get_error(iscsi));
1369
        ret = -EINVAL;
1370
        goto out;
1371
    }
1372

    
1373
    iscsilun->iscsi = iscsi;
1374
    iscsilun->lun   = iscsi_url->lun;
1375

    
1376
    task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
1377

    
1378
    if (task == NULL || task->status != SCSI_STATUS_GOOD) {
1379
        error_report("iSCSI: failed to send inquiry command.");
1380
        ret = -EINVAL;
1381
        goto out;
1382
    }
1383

    
1384
    inq = scsi_datain_unmarshall(task);
1385
    if (inq == NULL) {
1386
        error_report("iSCSI: Failed to unmarshall inquiry data.");
1387
        ret = -EINVAL;
1388
        goto out;
1389
    }
1390

    
1391
    iscsilun->type = inq->periperal_device_type;
1392
    iscsilun->has_write_same = true;
1393

    
1394
    if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1395
        goto out;
1396
    }
1397
    bs->total_sectors = sector_lun2qemu(iscsilun->num_blocks, iscsilun);
1398

    
1399
    /* Medium changer or tape. We dont have any emulation for this so this must
1400
     * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1401
     * to read from the device to guess the image format.
1402
     */
1403
    if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
1404
        iscsilun->type == TYPE_TAPE) {
1405
        bs->sg = 1;
1406
    }
1407

    
1408
    if (iscsilun->lbpme) {
1409
        struct scsi_inquiry_logical_block_provisioning *inq_lbp;
1410
        task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1411
                                SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING);
1412
        if (task == NULL) {
1413
            ret = -EINVAL;
1414
            goto out;
1415
        }
1416
        inq_lbp = scsi_datain_unmarshall(task);
1417
        if (inq_lbp == NULL) {
1418
            error_report("iSCSI: failed to unmarshall inquiry datain blob");
1419
            ret = -EINVAL;
1420
            goto out;
1421
        }
1422
        memcpy(&iscsilun->lbp, inq_lbp,
1423
               sizeof(struct scsi_inquiry_logical_block_provisioning));
1424
        scsi_free_scsi_task(task);
1425
        task = NULL;
1426
    }
1427

    
1428
    if (iscsilun->lbp.lbpu || iscsilun->lbp.lbpws) {
1429
        struct scsi_inquiry_block_limits *inq_bl;
1430
        task = iscsi_do_inquiry(iscsilun->iscsi, iscsilun->lun, 1,
1431
                                SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS);
1432
        if (task == NULL) {
1433
            ret = -EINVAL;
1434
            goto out;
1435
        }
1436
        inq_bl = scsi_datain_unmarshall(task);
1437
        if (inq_bl == NULL) {
1438
            error_report("iSCSI: failed to unmarshall inquiry datain blob");
1439
            ret = -EINVAL;
1440
            goto out;
1441
        }
1442
        memcpy(&iscsilun->bl, inq_bl,
1443
               sizeof(struct scsi_inquiry_block_limits));
1444
        scsi_free_scsi_task(task);
1445
        task = NULL;
1446

    
1447
        if (iscsilun->bl.max_unmap < 0xffffffff) {
1448
            bs->bl.max_discard = sector_lun2qemu(iscsilun->bl.max_unmap,
1449
                                                 iscsilun);
1450
        }
1451
        bs->bl.discard_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1452
                                                   iscsilun);
1453

    
1454
        if (iscsilun->bl.max_ws_len < 0xffffffff) {
1455
            bs->bl.max_write_zeroes = sector_lun2qemu(iscsilun->bl.max_ws_len,
1456
                                                      iscsilun);
1457
        }
1458
        bs->bl.write_zeroes_alignment = sector_lun2qemu(iscsilun->bl.opt_unmap_gran,
1459
                                                        iscsilun);
1460

    
1461
        bs->bl.opt_transfer_length = sector_lun2qemu(iscsilun->bl.opt_xfer_len,
1462
                                                     iscsilun);
1463
    }
1464

    
1465
#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1466
    /* Set up a timer for sending out iSCSI NOPs */
1467
    iscsilun->nop_timer = timer_new_ms(QEMU_CLOCK_REALTIME, iscsi_nop_timed_event, iscsilun);
1468
    timer_mod(iscsilun->nop_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + NOP_INTERVAL);
1469
#endif
1470

    
1471
out:
1472
    qemu_opts_del(opts);
1473
    if (initiator_name != NULL) {
1474
        g_free(initiator_name);
1475
    }
1476
    if (iscsi_url != NULL) {
1477
        iscsi_destroy_url(iscsi_url);
1478
    }
1479
    if (task != NULL) {
1480
        scsi_free_scsi_task(task);
1481
    }
1482

    
1483
    if (ret) {
1484
        if (iscsi != NULL) {
1485
            iscsi_destroy_context(iscsi);
1486
        }
1487
        memset(iscsilun, 0, sizeof(IscsiLun));
1488
    }
1489
    return ret;
1490
}
1491

    
1492
static void iscsi_close(BlockDriverState *bs)
1493
{
1494
    IscsiLun *iscsilun = bs->opaque;
1495
    struct iscsi_context *iscsi = iscsilun->iscsi;
1496

    
1497
    if (iscsilun->nop_timer) {
1498
        timer_del(iscsilun->nop_timer);
1499
        timer_free(iscsilun->nop_timer);
1500
    }
1501
    qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL);
1502
    iscsi_destroy_context(iscsi);
1503
    g_free(iscsilun->zeroblock);
1504
    memset(iscsilun, 0, sizeof(IscsiLun));
1505
}
1506

    
1507
static int iscsi_truncate(BlockDriverState *bs, int64_t offset)
1508
{
1509
    IscsiLun *iscsilun = bs->opaque;
1510
    int ret = 0;
1511

    
1512
    if (iscsilun->type != TYPE_DISK) {
1513
        return -ENOTSUP;
1514
    }
1515

    
1516
    if ((ret = iscsi_readcapacity_sync(iscsilun)) != 0) {
1517
        return ret;
1518
    }
1519

    
1520
    if (offset > iscsi_getlength(bs)) {
1521
        return -EINVAL;
1522
    }
1523

    
1524
    return 0;
1525
}
1526

    
1527
static int iscsi_create(const char *filename, QEMUOptionParameter *options,
1528
                        Error **errp)
1529
{
1530
    int ret = 0;
1531
    int64_t total_size = 0;
1532
    BlockDriverState *bs;
1533
    IscsiLun *iscsilun = NULL;
1534
    QDict *bs_options;
1535

    
1536
    bs = bdrv_new("");
1537

    
1538
    /* Read out options */
1539
    while (options && options->name) {
1540
        if (!strcmp(options->name, "size")) {
1541
            total_size = options->value.n / BDRV_SECTOR_SIZE;
1542
        }
1543
        options++;
1544
    }
1545

    
1546
    bs->opaque = g_malloc0(sizeof(struct IscsiLun));
1547
    iscsilun = bs->opaque;
1548

    
1549
    bs_options = qdict_new();
1550
    qdict_put(bs_options, "filename", qstring_from_str(filename));
1551
    ret = iscsi_open(bs, bs_options, 0, NULL);
1552
    QDECREF(bs_options);
1553

    
1554
    if (ret != 0) {
1555
        goto out;
1556
    }
1557
    if (iscsilun->nop_timer) {
1558
        timer_del(iscsilun->nop_timer);
1559
        timer_free(iscsilun->nop_timer);
1560
    }
1561
    if (iscsilun->type != TYPE_DISK) {
1562
        ret = -ENODEV;
1563
        goto out;
1564
    }
1565
    if (bs->total_sectors < total_size) {
1566
        ret = -ENOSPC;
1567
        goto out;
1568
    }
1569

    
1570
    ret = 0;
1571
out:
1572
    if (iscsilun->iscsi != NULL) {
1573
        iscsi_destroy_context(iscsilun->iscsi);
1574
    }
1575
    g_free(bs->opaque);
1576
    bs->opaque = NULL;
1577
    bdrv_unref(bs);
1578
    return ret;
1579
}
1580

    
1581
static int iscsi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1582
{
1583
    IscsiLun *iscsilun = bs->opaque;
1584
    bdi->unallocated_blocks_are_zero = !!iscsilun->lbprz;
1585
    bdi->can_write_zeroes_with_unmap = iscsilun->lbprz && iscsilun->lbp.lbpws;
1586
    /* Guess the internal cluster (page) size of the iscsi target by the means
1587
     * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1588
     * reasonable size for bdi->cluster_size */
1589
    if (iscsilun->bl.opt_unmap_gran * iscsilun->block_size >= 64 * 1024 &&
1590
        iscsilun->bl.opt_unmap_gran * iscsilun->block_size <= 16 * 1024 * 1024) {
1591
        bdi->cluster_size = iscsilun->bl.opt_unmap_gran * iscsilun->block_size;
1592
    }
1593
    return 0;
1594
}
1595

    
1596
static QEMUOptionParameter iscsi_create_options[] = {
1597
    {
1598
        .name = BLOCK_OPT_SIZE,
1599
        .type = OPT_SIZE,
1600
        .help = "Virtual disk size"
1601
    },
1602
    { NULL }
1603
};
1604

    
1605
static BlockDriver bdrv_iscsi = {
1606
    .format_name     = "iscsi",
1607
    .protocol_name   = "iscsi",
1608

    
1609
    .instance_size   = sizeof(IscsiLun),
1610
    .bdrv_needs_filename = true,
1611
    .bdrv_file_open  = iscsi_open,
1612
    .bdrv_close      = iscsi_close,
1613
    .bdrv_create     = iscsi_create,
1614
    .create_options  = iscsi_create_options,
1615

    
1616
    .bdrv_getlength  = iscsi_getlength,
1617
    .bdrv_get_info   = iscsi_get_info,
1618
    .bdrv_truncate   = iscsi_truncate,
1619

    
1620
#if defined(LIBISCSI_FEATURE_IOVECTOR)
1621
    .bdrv_co_get_block_status = iscsi_co_get_block_status,
1622
#endif
1623
    .bdrv_co_discard      = iscsi_co_discard,
1624
#if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
1625
    .bdrv_co_write_zeroes = iscsi_co_write_zeroes,
1626
#endif
1627

    
1628
    .bdrv_aio_readv  = iscsi_aio_readv,
1629
    .bdrv_aio_writev = iscsi_aio_writev,
1630
    .bdrv_aio_flush  = iscsi_aio_flush,
1631

    
1632
#ifdef __linux__
1633
    .bdrv_ioctl       = iscsi_ioctl,
1634
    .bdrv_aio_ioctl   = iscsi_aio_ioctl,
1635
#endif
1636
};
1637

    
1638
static QemuOptsList qemu_iscsi_opts = {
1639
    .name = "iscsi",
1640
    .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1641
    .desc = {
1642
        {
1643
            .name = "user",
1644
            .type = QEMU_OPT_STRING,
1645
            .help = "username for CHAP authentication to target",
1646
        },{
1647
            .name = "password",
1648
            .type = QEMU_OPT_STRING,
1649
            .help = "password for CHAP authentication to target",
1650
        },{
1651
            .name = "header-digest",
1652
            .type = QEMU_OPT_STRING,
1653
            .help = "HeaderDigest setting. "
1654
                    "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1655
        },{
1656
            .name = "initiator-name",
1657
            .type = QEMU_OPT_STRING,
1658
            .help = "Initiator iqn name to use when connecting",
1659
        },
1660
        { /* end of list */ }
1661
    },
1662
};
1663

    
1664
static void iscsi_block_init(void)
1665
{
1666
    bdrv_register(&bdrv_iscsi);
1667
    qemu_add_opts(&qemu_iscsi_opts);
1668
}
1669

    
1670
block_init(iscsi_block_init);